Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

uni.cc

Go to the documentation of this file.
00001 #include "uniconfroot.h"
00002 #include "wvlogrcv.h"
00003 #include "strutils.h"
00004 #include "wvtclstring.h"
00005 
00006 int main(int argc, char **argv)
00007 {
00008     WvLogConsole logcon(2, WvLog::Info);
00009     
00010     const char *confuri = getenv("UNICONF");
00011     if (!confuri)
00012     {
00013         fprintf(stderr, "%s: UNICONF environment variable not set!\n",
00014                 argv[0]);
00015         return 2;
00016     }
00017     
00018     if (argc < 3)
00019     {
00020         fprintf(stderr,
00021                 "Usage: %s <cmd> <key> [extra stuff...]\n"
00022                 " where <cmd> is one of:\n"
00023                 "   get - get the value of a key, with optional default\n"
00024                 "   set - set a key to the given value from the command line\n"
00025                 "   xset - set a key to the given value from stdin\n"
00026                 "   keys - list the subkeys of a key\n"
00027                 "   hkeys - list the subkeys of a key, their subkeys, etc\n"
00028                 "   dump - list the subkeys/values of a key (key=value)\n"
00029                 "   hdump - list the subkeys/values recursively\n",
00030                 argv[0]);
00031         return 3;
00032     }
00033     
00034     UniConfRoot cfg(confuri);
00035     
00036     // note: we know cmd and arg1 are non-NULL, but arg2 may be the argv
00037     // terminator, which is a NULL.  That has a special meaning for some
00038     // commands, like 'set', and is different from the empty string.
00039     const char *_cmd = argv[1], *arg1 = argv[2],
00040                *arg2 = argc > 3 ? argv[3] : NULL;
00041     WvString cmd(_cmd);
00042     strlwr(cmd.edit());
00043     
00044     if (cmd == "get")
00045     {
00046         WvString val = cfg[arg1].get(arg2);
00047         if (!val.isnull())
00048         {
00049             fputs(val, stdout);
00050             //fflush(stdout); // shouldn't be necessary!
00051             return 0; // okay
00052         }
00053         else
00054             return 1; // not found and no default given
00055     }
00056     else if (cmd == "set")
00057     {
00058         cfg[arg1].set(arg2);
00059         cfg.commit();
00060         return 0; // always works
00061     }
00062     else if (cmd == "xset")
00063     {
00064         // like set, but read from stdin
00065         WvDynBuf buf;
00066         size_t len;
00067         char *cptr;
00068         while (wvcon->isok())
00069         {
00070             cptr = (char *)buf.alloc(10240);
00071             len = wvcon->read(cptr, 10240);
00072             buf.unalloc(10240 - len);
00073         }
00074         cfg[arg1].set(buf.getstr());
00075         cfg.commit();
00076         return 0; // always works
00077     }
00078     else if (cmd == "keys")
00079     {
00080         UniConf::Iter i(cfg[arg1]);
00081         for (i.rewind(); i.next(); )
00082             wvcon->print("%s\n", wvtcl_escape(i->key(), "\r\n"));
00083     }
00084     else if (cmd == "hkeys")
00085     {
00086         UniConf sub(cfg[arg1]);
00087         UniConf::RecursiveIter i(sub);
00088         for (i.rewind(); i.next(); )
00089             wvcon->print("%s\n", wvtcl_escape(i->fullkey(sub), "\r\n"));
00090     }
00091     else if (cmd == "dump")
00092     {
00093         // note: the output of this command happens to be compatible with
00094         // (can be read by) the 'ini' UniConf backend.
00095         UniConf::Iter i(cfg[arg1]);
00096         for (i.rewind(); i.next(); )
00097             wvcon->print("%s = %s\n",
00098                          wvtcl_escape(i->key(), "\r\n[]="),
00099                          wvtcl_escape(i->get(""), "\r\n[]="));
00100     }
00101     else if (cmd == "hdump")
00102     {
00103         // note: the output of this command happens to be compatible with
00104         // (can be read by) the 'ini' UniConf backend.
00105         UniConf sub(cfg[arg1]);
00106         UniConf::RecursiveIter i(sub);
00107         for (i.rewind(); i.next(); )
00108             wvcon->print("%s = %s\n",
00109                          wvtcl_escape(i->fullkey(sub), "\r\n[]="),
00110                          wvtcl_escape(i->get(""), "\r\n[]="));
00111     }
00112     else
00113     {
00114         fprintf(stderr, "%s: unknown command '%s'!\n", argv[0], _cmd);
00115         return 4;
00116     }
00117 }

Generated on Sat Feb 21 21:05:19 2004 for WvStreams by doxygen 1.3.5