322d170d3a613e70c18483eb89ed0fb7fcf86378
[project/uci.git] / cli.c
1 /*
2  * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2
6  * as published by the Free Software Foundation
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13 #include <strings.h>
14 #include <stdlib.h>
15 #include "uci.h"
16
17 static struct uci_context *ctx;
18
19 static void uci_usage(int argc, char **argv)
20 {
21         fprintf(stderr,
22                 "Usage: %s [options] <command> [arguments]\n\n"
23                 "Commands:\n"
24                 "\tshow [<config>[.<section>[.<option>]]]\n"
25                 "\n",
26                 argv[0]
27         );
28         exit(255);
29 }
30
31 static void uci_show_section(struct uci_section *p)
32 {
33         struct uci_option *o;
34         const char *cname, *sname;
35
36         cname = p->config->name;
37         sname = p->name;
38         printf("%s.%s=%s\n", cname, sname, p->type);
39         uci_foreach_entry(option, &p->options, o) {
40                 printf("%s.%s.%s=%s\n", cname, sname, o->name, o->value);
41         }
42 }
43
44 static void uci_show_file(const char *name)
45 {
46         struct uci_config *cfg;
47         struct uci_section *p;
48
49         if (uci_load(ctx, name, &cfg) != UCI_OK) {
50                 uci_perror(ctx, "uci_load");
51                 return;
52         }
53
54         uci_list_empty(&cfg->sections);
55         uci_foreach_entry(section, &cfg->sections, p) {
56                 uci_show_section(p);
57         }
58         uci_unload(ctx, name);
59 }
60
61 static int uci_show(int argc, char **argv)
62 {
63         char **configs = uci_list_configs();
64         char **p;
65
66         if (!configs)
67                 return 0;
68
69         for (p = configs; *p; p++) {
70                 fprintf(stderr, "# config: %s\n", *p);
71                 uci_show_file(*p);
72         }
73
74         return 0;
75 }
76
77 static int uci_cmd(int argc, char **argv)
78 {
79         if (!strcasecmp(argv[0], "show"))
80                 uci_show(argc, argv);
81         return 0;
82 }
83
84 int main(int argc, char **argv)
85 {
86         int ret;
87
88         ctx = uci_alloc();
89         if (argc < 2)
90                 uci_usage(argc, argv);
91         ret = uci_cmd(argc - 1, argv + 1);
92         uci_free(ctx);
93
94         return ret;
95 }