WvStreams
uni.cc
1 #include "wvautoconf.h"
2 #include "uniconfroot.h"
3 #include "wvlogrcv.h"
4 #include "strutils.h"
5 #include "wvstringmask.h"
6 #include "wvtclstring.h"
7 
8 #ifdef _WIN32
9 #pragma comment(linker, "/include:?UniRegistryGenMoniker@@3V?$WvMoniker@VIUniConfGen@@@@A")
10 #pragma comment(linker, "/include:?UniPStoreGenMoniker@@3V?$WvMoniker@VIUniConfGen@@@@A")
11 #pragma comment(linker, "/include:?UniIniGenMoniker@@3V?$WvMoniker@VIUniConfGen@@@@A")
12 #endif
13 
14 void usage()
15 {
16  fprintf(stderr,
17  "Usage: uni <cmd> <key> [extra stuff...]\n"
18  " where <cmd> is one of:\n"
19  " get - get the value of a key, with optional default\n"
20  " set - set a key to the given value from the command line\n"
21  " xset - set a key to the given value from stdin\n"
22  " keys - list the subkeys of a key\n"
23  " hkeys - list the subkeys of a key, their subkeys, etc\n"
24  " xkeys - list keys that match a wildcard\n"
25  " dump - list the subkeys/values of a key (key=value)\n"
26  " hdump - list the subkeys/values recursively\n"
27  " xdump - list keys/values that match a wildcard\n"
28  " del - delete all subkeys\n"
29  " help - this text\n"
30  "\n"
31  "You must set the UNICONF environment variable to a valid "
32  "UniConf moniker.\n"
33  "\n"
34  "Report bugs to <" WVPACKAGE_BUGREPORT ">.\n");
35 }
36 
37 int main(int argc, char **argv)
38 {
39  WvLogConsole logcon(2, WvLog::Info);
40 
41  if (argc < 3)
42  {
43  usage();
44  return 3;
45  }
46 
47  // note: we know cmd and arg1 are non-NULL, but arg2 may be the argv
48  // terminator, which is a NULL. That has a special meaning for some
49  // commands, like 'set', and is different from the empty string.
50  const char *_cmd = argv[1], *arg1 = argv[2],
51  *arg2 = argc > 3 ? argv[3] : NULL;
52  WvString cmd(_cmd);
53  strlwr(cmd.edit());
54 
55  if (cmd == "help")
56  {
57  usage();
58  return 0;
59  }
60 
61  const char *confuri = getenv("UNICONF");
62  if (!confuri)
63  {
64  fprintf(stderr, "%s: UNICONF environment variable not set!\n",
65  argv[0]);
66  return 2;
67  }
68 
69  UniConfRoot cfg(confuri);
70 
71  if (!cfg.whichmount() || !cfg.whichmount()->isok())
72  {
73  fprintf(stderr, "%s: can't connect to uniconf at '%s'\n",
74  argv[0], confuri);
75  return 5;
76  }
77 
78  static const WvStringMask nasties("\r\n[]=");
79  if (cmd == "get")
80  {
81  WvString val = cfg[arg1].getme(arg2);
82  if (!val.isnull())
83  {
84  fputs(val, stdout);
85  //fflush(stdout); // shouldn't be necessary!
86  return 0; // okay
87  }
88  else
89  return 1; // not found and no default given
90  }
91  else if (cmd == "set")
92  {
93  cfg[arg1].setme(arg2);
94  cfg.commit();
95  return 0; // always works
96  }
97  else if (cmd == "xset")
98  {
99  // like set, but read from stdin
100  WvDynBuf buf;
101  size_t len;
102  char *cptr;
103  while (wvcon->isok())
104  {
105  cptr = (char *)buf.alloc(10240);
106  len = wvcon->read(cptr, 10240);
107  buf.unalloc(10240 - len);
108  }
109  cfg[arg1].setme(buf.getstr());
110  cfg.commit();
111  return 0; // always works
112  }
113  else if (cmd == "keys")
114  {
115  UniConf::Iter i(cfg[arg1]);
116  for (i.rewind(); i.next(); )
117  wvcon->print("%s\n", wvtcl_escape(i->key(),
118  WVTCL_NASTY_NEWLINES));
119  }
120  else if (cmd == "hkeys")
121  {
122  UniConf sub(cfg[arg1]);
123  UniConf::RecursiveIter i(sub);
124  for (i.rewind(); i.next(); )
125  wvcon->print("%s\n", wvtcl_escape(i->fullkey(sub),
126  WVTCL_NASTY_NEWLINES));
127  }
128  else if (cmd == "xkeys")
129  {
130  UniConf::XIter i(cfg, arg1);
131  for (i.rewind(); i.next(); )
132  wvcon->print("%s\n", wvtcl_escape(i->fullkey(cfg),
133  WVTCL_NASTY_NEWLINES));
134  }
135  else if (cmd == "dump")
136  {
137  // note: the output of this command happens to be compatible with
138  // (can be read by) the 'ini' UniConf backend.
139  UniConf::Iter i(cfg[arg1]);
140  for (i.rewind(); i.next(); )
141  wvcon->print("%s = %s\n",
142  wvtcl_escape(i->key(), nasties),
143  wvtcl_escape(i->getme(""), nasties));
144  }
145  else if (cmd == "hdump")
146  {
147  // note: the output of this command happens to be compatible with
148  // (can be read by) the 'ini' UniConf backend.
149  UniConf sub(cfg[arg1]);
150  UniConf::RecursiveIter i(sub);
151  for (i.rewind(); i.next(); )
152  wvcon->print("%s = %s\n",
153  wvtcl_escape(i->fullkey(sub), nasties),
154  wvtcl_escape(i->getme(""), nasties));
155  }
156  else if (cmd == "xdump")
157  {
158  // note: the output of this command happens to be compatible with
159  // (can be read by) the 'ini' UniConf backend.
160  UniConf::XIter i(cfg, arg1);
161  for (i.rewind(); i.next(); )
162  wvcon->print("%s = %s\n",
163  wvtcl_escape(i->fullkey(cfg), nasties),
164  wvtcl_escape(i->getme(""), nasties));
165  }
166  else if (cmd == "del")
167  {
168  UniConf sub(cfg[arg1]);
169  sub.remove();
170  cfg.commit();
171  }
172  else
173  {
174  fprintf(stderr, "%s: unknown command '%s'!\n", argv[0], _cmd);
175  return 4;
176  }
177 }
WvLogConsole
Captures formatted log messages and outputs them to the specified file descriptor.
Definition: wvlogrcv.h:107
WvStringMask
A class used to provide a masked lookup for characters in a string.
Definition: wvstringmask.h:18
UniConf::XIter
This iterator walks over all children that match a wildcard pattern.
Definition: uniconf.h:511
WvBufBaseCommonImpl::alloc
T * alloc(size_t count)
Allocates exactly the specified number of elements and returns a pointer to an UNINITIALIZED storage ...
Definition: wvbufbase.h:379
wvtclstring.h
WvBufBaseCommonImpl::unalloc
void unalloc(size_t count)
Unallocates exactly the specified number of elements by removing them from the buffer and releasing t...
Definition: wvbufbase.h:421
WvBufBase< unsigned char >::getstr
WvString getstr()
Returns the entire buffer as a null-terminated WvString.
Definition: wvbuffer.cc:17
UniConf
UniConf instances function as handles to subtrees of a UniConf tree and expose a high-level interface...
Definition: uniconf.h:50
WvString
WvString is an implementation of a simple and efficient printable-string class.
Definition: wvstring.h:329
WvFastString::isnull
bool isnull() const
returns true if this string is null
Definition: wvstring.h:290
wvtcl_escape
WvString wvtcl_escape(WvStringParm s, const WvStringMask &nasties=WVTCL_NASTY_SPACES)
tcl-escape a string.
Definition: wvtclstring.cc:128
WvStream::read
virtual size_t read(void *buf, size_t count)
read a data block on the stream.
Definition: wvstream.cc:490
WvStream::isok
virtual bool isok() const
return true if the stream is actually usable right now
Definition: wvstream.cc:445
strlwr
char * strlwr(char *string)
In-place modify a character string so that all contained letters are in lower case.
Definition: strutils.cc:201
WvDynBufBase< unsigned char >
UniConfRoot
Represents the root of a hierarhical registry consisting of pairs of UniConfKeys and associated strin...
Definition: uniconfroot.h:73
UniConf::Iter
This iterator walks through all immediate children of a UniConf node.
Definition: uniconf.h:435
UniConf::RecursiveIter
This iterator performs depth-first traversal of a subtree.
Definition: uniconf.h:466