]> mj.ucw.cz Git - libucw.git/blob - config.c
e00ca8f4f595f8ad944ccbdd8150a88580cb7354
[libucw.git] / config.c
1 /*
2  *      Sherlock Library -- Shell Interface to Configuration Files
3  *
4  *      (c) 2002 Martin Mares <mj@ucw.cz>
5  *
6  *      Once we were using this beautiful Shell version, but it turned out
7  *      that it doesn't work with nested config files:
8  *
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; }; };
12  *              d;'`
13  *
14  *      This software may be freely distributed and used according to the terms
15  *      of the GNU Lesser General Public License.
16  */
17
18 #include "lib/lib.h"
19 #include "lib/conf.h"
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <alloca.h>
25
26 static struct cfitem *items;
27 static byte *flags;
28
29 static void
30 help(void)
31 {
32   die("Usage: config [-C<configfile>] [-S<section>.<option>=<value>] <section> [@]<item>[=<default>] <item2> ... [*]");
33 }
34
35 static byte *
36 report(struct cfitem *item, byte *value)
37 {
38   if (flags[item-items])
39     printf("CF_%s[${#CF_%s[*]}]='", item->name, item->name);
40   else
41     printf("CF_%s='", item->name);
42   while (*value)
43     {
44       if (*value == '\'')
45         die("Apostrophes are not supported in config of scripts");
46       putchar(*value++);
47     }
48   puts("'");
49   return NULL;
50 }
51
52 int main(int argc, char **argv)
53 {
54   int i = 1;
55   int start;
56   struct cfitem *c;
57
58   log_init("config");
59   while (i < argc && argv[i][0] == '-')
60     i++;
61   if (i + 1 >= argc)
62     help();
63   start = i;
64   c = items = alloca(sizeof(struct cfitem) * (argc-i+1));
65   flags = alloca(argc);
66   bzero(flags, argc);
67   c->name = argv[i];
68   c->type = CT_SECTION;
69   c->var = NULL;
70   c++;
71   while (++i < argc)
72     {
73       if (!strcmp(argv[i], "*"))
74         items->type = CT_INCOMPLETE_SECTION;
75       else
76         {
77           char *e = strchr(argv[i], '=');
78           c->name = argv[i];
79           c->type = CT_FUNCTION;
80           c->var = report;
81           if (e)
82             *e++ = 0;
83           if (*c->name == '@')
84             {
85               c->name++;
86               printf("declare -a CF_%s ; CF_%s=()\n", c->name, c->name);
87               flags[c-items] = 1;
88             }
89           if (e)
90             report(c, e);
91           c++;
92         }
93     }
94   c->type = CT_STOP;
95   cf_register(items);
96   if (cf_getopt(start, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) != -1)
97     help();
98   return 0;
99 }