WvStreams
uni.cc
00001 #include "wvautoconf.h"
00002 #include "uniconfroot.h"
00003 #include "wvlogrcv.h"
00004 #include "strutils.h"
00005 #include "wvstringmask.h"
00006 #include "wvtclstring.h"
00007 
00008 #ifdef _WIN32
00009 #pragma comment(linker, "/include:?UniRegistryGenMoniker@@3V?$WvMoniker@VIUniConfGen@@@@A")
00010 #pragma comment(linker, "/include:?UniPStoreGenMoniker@@3V?$WvMoniker@VIUniConfGen@@@@A")
00011 #pragma comment(linker, "/include:?UniIniGenMoniker@@3V?$WvMoniker@VIUniConfGen@@@@A")
00012 #endif
00013 
00014 void usage()
00015 {
00016     fprintf(stderr,
00017             "Usage: uni <cmd> <key> [extra stuff...]\n"
00018             " where <cmd> is one of:\n"
00019             "   get   - get the value of a key, with optional default\n"
00020             "   set   - set a key to the given value from the command line\n"
00021             "   xset  - set a key to the given value from stdin\n"
00022             "   keys  - list the subkeys of a key\n"
00023             "   hkeys - list the subkeys of a key, their subkeys, etc\n"
00024             "   xkeys - list keys that match a wildcard\n"
00025             "   dump  - list the subkeys/values of a key (key=value)\n"
00026             "   hdump - list the subkeys/values recursively\n"
00027             "   xdump - list keys/values that match a wildcard\n"
00028             "   del   - delete all subkeys\n"
00029             "   help  - this text\n"
00030             "\n"
00031             "You must set the UNICONF environment variable to a valid "
00032             "UniConf moniker.\n"
00033             "\n"
00034             "Report bugs to <" WVPACKAGE_BUGREPORT ">.\n");
00035 }
00036 
00037 int main(int argc, char **argv)
00038 {
00039     WvLogConsole logcon(2, WvLog::Info);
00040     
00041     if (argc < 3)
00042     {
00043         usage();
00044         return 3;
00045     }
00046     
00047     // note: we know cmd and arg1 are non-NULL, but arg2 may be the argv
00048     // terminator, which is a NULL.  That has a special meaning for some
00049     // commands, like 'set', and is different from the empty string.
00050     const char *_cmd = argv[1], *arg1 = argv[2],
00051                *arg2 = argc > 3 ? argv[3] : NULL;
00052     WvString cmd(_cmd);
00053     strlwr(cmd.edit());
00054 
00055     if (cmd == "help")
00056     {
00057         usage();
00058         return 0;
00059     }
00060 
00061     const char *confuri = getenv("UNICONF");
00062     if (!confuri)
00063     {
00064         fprintf(stderr, "%s: UNICONF environment variable not set!\n",
00065                 argv[0]);
00066         return 2;
00067     }
00068     
00069     UniConfRoot cfg(confuri);
00070     
00071     if (!cfg.whichmount() || !cfg.whichmount()->isok())
00072     {
00073         fprintf(stderr, "%s: can't connect to uniconf at '%s'\n",
00074                 argv[0], confuri);
00075         return 5;
00076     }
00077     
00078     static const WvStringMask nasties("\r\n[]=");
00079     if (cmd == "get")
00080     {
00081         WvString val = cfg[arg1].getme(arg2);
00082         if (!val.isnull())
00083         {
00084             fputs(val, stdout);
00085             //fflush(stdout); // shouldn't be necessary!
00086             return 0; // okay
00087         }
00088         else
00089             return 1; // not found and no default given
00090     }
00091     else if (cmd == "set")
00092     {
00093         cfg[arg1].setme(arg2);
00094         cfg.commit();
00095         return 0; // always works
00096     }
00097     else if (cmd == "xset")
00098     {
00099         // like set, but read from stdin
00100         WvDynBuf buf;
00101         size_t len;
00102         char *cptr;
00103         while (wvcon->isok())
00104         {
00105             cptr = (char *)buf.alloc(10240);
00106             len = wvcon->read(cptr, 10240);
00107             buf.unalloc(10240 - len);
00108         }
00109         cfg[arg1].setme(buf.getstr());
00110         cfg.commit();
00111         return 0; // always works
00112     }
00113     else if (cmd == "keys")
00114     {
00115         UniConf::Iter i(cfg[arg1]);
00116         for (i.rewind(); i.next(); )
00117             wvcon->print("%s\n", wvtcl_escape(i->key(),
00118                                               WVTCL_NASTY_NEWLINES));
00119     }
00120     else if (cmd == "hkeys")
00121     {
00122         UniConf sub(cfg[arg1]);
00123         UniConf::RecursiveIter i(sub);
00124         for (i.rewind(); i.next(); )
00125             wvcon->print("%s\n", wvtcl_escape(i->fullkey(sub),
00126                                               WVTCL_NASTY_NEWLINES));
00127     }
00128     else if (cmd == "xkeys")
00129     {
00130         UniConf::XIter i(cfg, arg1);
00131         for (i.rewind(); i.next(); )
00132             wvcon->print("%s\n", wvtcl_escape(i->fullkey(cfg),
00133                                               WVTCL_NASTY_NEWLINES));
00134     }
00135     else if (cmd == "dump")
00136     {
00137         // note: the output of this command happens to be compatible with
00138         // (can be read by) the 'ini' UniConf backend.
00139         UniConf::Iter i(cfg[arg1]);
00140         for (i.rewind(); i.next(); )
00141             wvcon->print("%s = %s\n",
00142                          wvtcl_escape(i->key(), nasties),
00143                          wvtcl_escape(i->getme(""), nasties));
00144     }
00145     else if (cmd == "hdump")
00146     {
00147         // note: the output of this command happens to be compatible with
00148         // (can be read by) the 'ini' UniConf backend.
00149         UniConf sub(cfg[arg1]);
00150         UniConf::RecursiveIter i(sub);
00151         for (i.rewind(); i.next(); )
00152             wvcon->print("%s = %s\n",
00153                          wvtcl_escape(i->fullkey(sub), nasties),
00154                          wvtcl_escape(i->getme(""), nasties));
00155     }
00156     else if (cmd == "xdump")
00157     {
00158         // note: the output of this command happens to be compatible with
00159         // (can be read by) the 'ini' UniConf backend.
00160         UniConf::XIter i(cfg, arg1);
00161         for (i.rewind(); i.next(); )
00162             wvcon->print("%s = %s\n",
00163                          wvtcl_escape(i->fullkey(cfg), nasties),
00164                          wvtcl_escape(i->getme(""), nasties));
00165     }
00166     else if (cmd == "del")
00167     {
00168         UniConf sub(cfg[arg1]);
00169         sub.remove();
00170         cfg.commit();
00171     }
00172     else
00173     {
00174         fprintf(stderr, "%s: unknown command '%s'!\n", argv[0], _cmd);
00175         return 4;
00176     }
00177 }