implement uci_set, improve error handling
[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
98
99 static int uci_do_get(int argc, char **argv)
100 {
101         char *package = NULL;
102         char *section = NULL;
103         char *option = NULL;
104         struct uci_package *p = NULL;
105         struct uci_element *e = NULL;
106         char *value = NULL;
107
108         package = strtok(argv[1], ".");
109         if (!package)
110                 return 1;
111
112         section = strtok(NULL, ".");
113         if (section)
114                 option = strtok(NULL, ".");
115
116         if (uci_load(ctx, package, &p) != UCI_OK) {
117                 uci_perror(ctx, "uci");
118                 return 1;
119         }
120         if (uci_lookup(ctx, &e, package, section, option) != UCI_OK)
121                 return 1;
122         switch(e->type) {
123         case UCI_TYPE_SECTION:
124                 value = uci_to_section(e)->type;
125                 break;
126         case UCI_TYPE_OPTION:
127                 value = uci_to_option(e)->value;
128                 break;
129         default:
130                 /* should not happen */
131                 return 1;
132         }
133         printf("%s\n", value);
134         return 0;
135 }
136
137 static int uci_cmd(int argc, char **argv)
138 {
139         if (!strcasecmp(argv[0], "show"))
140                 return uci_show(argc, argv);
141         if (!strcasecmp(argv[0], "export"))
142                 return uci_do_export(argc, argv);
143         if (!strcasecmp(argv[0], "get"))
144                 return uci_do_get(argc, argv);
145         return 255;
146 }
147
148 int main(int argc, char **argv)
149 {
150         int ret;
151
152         ctx = uci_alloc_context();
153         if (argc < 2)
154                 uci_usage(argc, argv);
155         ret = uci_cmd(argc - 1, argv + 1);
156         if (ret == 255)
157                 uci_usage(argc, argv);
158         uci_free_context(ctx);
159
160         return ret;
161 }