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