]> mj.ucw.cz Git - libucw.git/blob - lib/shell/config.c
f0fd32c858097ee2e8eaf6856c6c2680290db6cc
[libucw.git] / lib / shell / 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
15 #include "lib/lib.h"
16 #include "lib/conf.h"
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21
22 static void
23 help(void)
24 {
25   die("Usage: config [-C<configfile>] [-S<section>.<option>=<value>] <section> <item>[=<default>] <item2> ... [*]");
26 }
27
28 int main(int argc, char **argv)
29 {
30   int i = 1;
31   int start;
32   struct cfitem *items, *c;
33   byte **vars;
34
35   log_init("config");
36   while (i < argc && argv[i][0] == '-')
37     i++;
38   if (i + 1 >= argc)
39     help();
40   start = i;
41   c = items = alloca(sizeof(struct cfitem) * (argc-i+1));
42   vars = alloca(sizeof(byte *) * argc);
43   c->name = argv[i];
44   c->type = CT_SECTION;
45   c->var = NULL;
46   c++;
47   while (++i < argc)
48     {
49       if (!strcmp(argv[i], "*"))
50         items->type = CT_INCOMPLETE_SECTION;
51       else
52         {
53           byte *e = strchr(argv[i], '=');
54           if (e)
55             *e++ = 0;
56           else
57             e = "";
58           c->name = argv[i];
59           c->type = CT_STRING;
60           c->var = &vars[i];
61           *(byte **)c->var = e;
62           c++;
63         }
64     }
65   c->type = CT_STOP;
66   cf_register(items);
67   if (cf_getopt(start, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) != -1)
68     help();
69
70   c = items+1;
71   while (c->type)
72     {
73       printf("CF_%s=\"%s\"\n", c->name, *(byte **)c->var);
74       c++;
75     }
76
77   return 0;
78 }