2 * UCW Library -- Shell Interface to Configuration Files
4 * (c) 2002--2005 Martin Mares <mj@ucw.cz>
6 * Once we were using this beautiful Shell version, but it turned out
7 * that it doesn't work with nested config files:
9 * eval `sed <cf/sherlock '/^#/d;/^ *$/d;s/ \+$//;
10 * h;s@[^ ]*@@;x;s@[ ].*@@;y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/;G;s/\n//;
11 * /^\[SECTION\]/,/^\[/ {; /^[A-Z]/ { s/^\([^ ]\+\)[ ]*\(.*\)$/SH_\1="\2"/; p; }; };
14 * This software may be freely distributed and used according to the terms
15 * of the GNU Lesser General Public License.
26 static struct cfitem *items;
42 Usage: config [-C<configfile>] [-S<section>.<option>=<value>] <section> [@]<item><type>[=<default>] <item2> ... [*]\n\
46 #\t\t32-bit integer\n\
47 ##\t\t64-bit integer\n\
48 $\t\tFloating point number\n\
51 @\t\tMultiple occurences of the item are passed as an array (otherwise the last one wins)\n\
52 *\t\tIgnore unknown items instead of reporting them as errors\n\
58 report(struct cfitem *item, byte *value)
60 uns f = flags[item-items];
65 printf("CF_%s[${#CF_%s[*]}]='", item->name, item->name);
67 printf("CF_%s='", item->name);
69 switch (f & F_TYPE_MASK)
75 if (err = cf_parse_int(value, &val))
77 sprintf(buf, "%d", val);
82 if (err = cf_parse_u64(value, &val64))
84 sprintf(buf, "%Lu", val64);
89 if (err = cf_parse_double(value, &valf))
91 sprintf(buf, "%g", valf);
100 die("Apostrophes are not supported in config of scripts");
107 int main(int argc, char **argv)
114 while (i < argc && argv[i][0] == '-')
119 c = items = alloca(sizeof(struct cfitem) * (argc-i+1));
120 flags = alloca(argc);
123 c->type = CT_SECTION;
128 char *arg = xstrdup(argv[i]);
129 if (!strcmp(arg, "*"))
130 items->type = CT_INCOMPLETE_SECTION;
134 char *e = strchr(arg, '=');
138 char *t = arg + strlen(arg) - 1;
144 if (t > arg && *t == '#')
147 flags[id] |= F_INT64;
155 flags[id] |= F_DOUBLE;
162 printf("declare -a CF_%s ; CF_%s=()\n", arg, arg);
163 flags[id] |= F_ARRAY;
166 c->type = CT_FUNCTION;
176 if (cf_getopt(start, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) != -1)