afe96e3c12589100d6800d6b28eb31b92147b8af
[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 static char *buf = NULL;
19 static int buflen = 256;
20
21 static void uci_usage(int argc, char **argv)
22 {
23         fprintf(stderr,
24                 "Usage: %s [<options>] <command> [<arguments>]\n\n"
25                 "Commands:\n"
26                 "\tshow [<config>[.<section>[.<option>]]]\n"
27                 "\texport [<config>]\n"
28                 "\n",
29                 argv[0]
30         );
31         exit(255);
32 }
33
34 static void uci_show_section(struct uci_section *p)
35 {
36         struct uci_option *o;
37         const char *cname, *sname;
38
39         cname = p->config->name;
40         sname = p->name;
41         printf("%s.%s=%s\n", cname, sname, p->type);
42         uci_foreach_entry(option, &p->options, o) {
43                 printf("%s.%s.%s=%s\n", cname, sname, o->name, o->value);
44         }
45 }
46
47 static void foreach_section(const char *configname, const char *section, void (*callback)(struct uci_section *))
48 {
49         struct uci_config *cfg;
50         struct uci_section *p;
51
52         if (uci_load(ctx, configname, &cfg) != UCI_OK) {
53                 uci_perror(ctx, "uci_load");
54                 return;
55         }
56
57         uci_foreach_entry(section, &cfg->sections, p) {
58                 if (!section || !strcmp(p->name, section))
59                         callback(p);
60         }
61         uci_unload(ctx, configname);
62 }
63
64 static int uci_show(int argc, char **argv)
65 {
66         char **configs = uci_list_configs();
67         char **p;
68
69         if (!configs)
70                 return 0;
71
72         for (p = configs; *p; p++) {
73                 if ((argc < 2) || !strcmp(argv[1], *p))
74                         foreach_section(*p, (argc > 2 ? argv[2] : NULL), uci_show_section);
75         }
76
77         return 0;
78 }
79
80 static int uci_do_export(int argc, char **argv)
81 {
82         char **configs = uci_list_configs();
83         char **p;
84
85         if (!configs)
86                 return 0;
87
88         for (p = configs; *p; p++) {
89                 if ((argc < 2) || !strcmp(argv[1], *p)) {
90                         struct uci_config *cfg = NULL;
91                         int ret;
92
93                         ret = uci_load(ctx, *p, &cfg);
94                         if (ret)
95                                 continue;
96                         uci_export(ctx, stdout, cfg);
97                         uci_unload(ctx, *p);
98                 }
99         }
100         return 0;
101 }
102
103 static int uci_cmd(int argc, char **argv)
104 {
105         if (!strcasecmp(argv[0], "show"))
106                 return uci_show(argc, argv);
107         if (!strcasecmp(argv[0], "export"))
108                 return uci_do_export(argc, argv);
109         return 255;
110 }
111
112 int main(int argc, char **argv)
113 {
114         int ret;
115
116         ctx = uci_alloc();
117         if (argc < 2)
118                 uci_usage(argc, argv);
119         ret = uci_cmd(argc - 1, argv + 1);
120         if (ret == 255)
121                 uci_usage(argc, argv);
122         uci_free(ctx);
123
124         return ret;
125 }