fix null string test
[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 char *uci_escape(char *str)
35 {
36         char *s, *p, *t;
37         int pos = 0;
38
39         if (!buf)
40                 buf = malloc(buflen);
41
42         s = str;
43         p = strchr(str, '\'');
44         if (!p)
45                 return str;
46
47         do {
48                 int len = p - s;
49                 if (len > 0) {
50                         if (p + 3 - str >= buflen) {
51                                 buflen *= 2;
52                                 buf = realloc(buf, buflen);
53                                 if (!buf) {
54                                         fprintf(stderr, "Out of memory\n");
55                                         exit(255);
56                                 }
57                         }
58                         memcpy(&buf[pos], s, len);
59                         pos += len;
60                 }
61                 strcpy(&buf[pos], "'\\''");
62                 pos += 3;
63                 s = p + 1;
64         } while ((p = strchr(s, '\'')));
65
66         return buf;
67 }
68
69 static void uci_show_section(struct uci_section *p)
70 {
71         struct uci_option *o;
72         const char *cname, *sname;
73
74         cname = p->config->name;
75         sname = p->name;
76         printf("%s.%s=%s\n", cname, sname, p->type);
77         uci_foreach_entry(option, &p->options, o) {
78                 printf("%s.%s.%s=%s\n", cname, sname, o->name, o->value);
79         }
80 }
81
82 static void uci_export_section(struct uci_section *p)
83 {
84         struct uci_option *o;
85         const char *name;
86
87         printf("\nconfig '%s'", uci_escape(p->type));
88         printf(" '%s'\n", uci_escape(p->name));
89         uci_foreach_entry(option, &p->options, o) {
90                 printf("\toption '%s'", uci_escape(o->name));
91                 printf(" '%s'\n", uci_escape(o->value));
92         }
93 }
94
95 static void foreach_section(const char *configname, const char *section, void (*callback)(struct uci_section *))
96 {
97         struct uci_config *cfg;
98         struct uci_section *p;
99
100         if (uci_load(ctx, configname, &cfg) != UCI_OK) {
101                 uci_perror(ctx, "uci_load");
102                 return;
103         }
104
105         uci_list_empty(&cfg->sections);
106         uci_foreach_entry(section, &cfg->sections, p) {
107                 if (!section || !strcmp(p->name, section))
108                         callback(p);
109         }
110         uci_unload(ctx, configname);
111 }
112
113 static int uci_show(int argc, char **argv)
114 {
115         char **configs = uci_list_configs();
116         char **p;
117
118         if (!configs)
119                 return 0;
120
121         for (p = configs; *p; p++) {
122                 if ((argc < 2) || !strcmp(argv[1], *p))
123                         foreach_section(*p, (argc > 2 ? argv[2] : NULL), uci_show_section);
124         }
125
126         return 0;
127 }
128
129 static int uci_export(int argc, char **argv)
130 {
131         char **configs = uci_list_configs();
132         char **p;
133
134         if (!configs)
135                 return 0;
136
137         for (p = configs; *p; p++) {
138                 if ((argc < 2) || !strcmp(argv[1], *p)) {
139                         printf("package '%s'\n", uci_escape(*p));
140                         foreach_section(*p, NULL, uci_export_section);
141                         printf("\n");
142                 }
143         }
144         return 0;
145 }
146
147 static int uci_cmd(int argc, char **argv)
148 {
149         if (!strcasecmp(argv[0], "show"))
150                 return uci_show(argc, argv);
151         if (!strcasecmp(argv[0], "export"))
152                 return uci_export(argc, argv);
153         return 255;
154 }
155
156 int main(int argc, char **argv)
157 {
158         int ret;
159
160         ctx = uci_alloc();
161         if (argc < 2)
162                 uci_usage(argc, argv);
163         ret = uci_cmd(argc - 1, argv + 1);
164         if (ret == 255)
165                 uci_usage(argc, argv);
166         uci_free(ctx);
167
168         return ret;
169 }