add a comment
[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                 "\texport [<config>]\n"
26                 "\n",
27                 argv[0]
28         );
29         exit(255);
30 }
31
32 static void uci_show_section(struct uci_section *p)
33 {
34         struct uci_element *e;
35         const char *cname, *sname;
36
37         cname = p->package->e.name;
38         sname = p->e.name;
39         printf("%s.%s=%s\n", cname, sname, p->type);
40         uci_foreach_element(&p->options, e) {
41                 printf("%s.%s.%s=%s\n", cname, sname, e->name, uci_to_option(e)->value);
42         }
43 }
44
45 static int uci_show(int argc, char **argv)
46 {
47         char *section = (argc > 2 ? argv[2] : NULL);
48         struct uci_package *package;
49         struct uci_element *e;
50         char **configs;
51         char **p;
52
53         configs = uci_list_configs(ctx);
54         if (!configs)
55                 return 0;
56
57         for (p = configs; *p; p++) {
58                 if ((argc < 2) || !strcmp(argv[1], *p)) {
59                         if (uci_load(ctx, *p, &package) != UCI_OK) {
60                                 uci_perror(ctx, "uci_load");
61                                 return 255;
62                         }
63                         uci_foreach_element( &package->sections, e) {
64                                 if (!section || !strcmp(e->name, section))
65                                         uci_show_section(uci_to_section(e));
66                         }
67                         uci_unload(ctx, *p);
68                 }
69         }
70
71         return 0;
72 }
73
74 static int uci_do_export(int argc, char **argv)
75 {
76         char **configs = uci_list_configs(ctx);
77         char **p;
78
79         if (!configs)
80                 return 0;
81
82         for (p = configs; *p; p++) {
83                 if ((argc < 2) || !strcmp(argv[1], *p)) {
84                         struct uci_package *package = NULL;
85                         int ret;
86
87                         ret = uci_load(ctx, *p, &package);
88                         if (ret)
89                                 continue;
90                         uci_export(ctx, stdout, package);
91                         uci_unload(ctx, *p);
92                 }
93         }
94         return 0;
95 }
96
97 static int uci_cmd(int argc, char **argv)
98 {
99         if (!strcasecmp(argv[0], "show"))
100                 return uci_show(argc, argv);
101         if (!strcasecmp(argv[0], "export"))
102                 return uci_do_export(argc, argv);
103         return 255;
104 }
105
106 int main(int argc, char **argv)
107 {
108         int ret;
109
110         ctx = uci_alloc_context();
111         if (argc < 2)
112                 uci_usage(argc, argv);
113         ret = uci_cmd(argc - 1, argv + 1);
114         if (ret == 255)
115                 uci_usage(argc, argv);
116         uci_free_context(ctx);
117
118         return ret;
119 }