40b5d9115193519e5361f88a0002465f346924f1
[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("config '%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 *name, void (*callback)(struct uci_section *))
96 {
97         struct uci_config *cfg;
98         struct uci_section *p;
99
100         if (uci_load(ctx, name, &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                 callback(p);
108         }
109         uci_unload(ctx, name);
110 }
111
112 static int uci_show(int argc, char **argv)
113 {
114         char **configs = uci_list_configs();
115         char **p;
116
117         if (!configs)
118                 return 0;
119
120         for (p = configs; *p; p++) {
121                 fprintf(stderr, "# config: %s\n", *p);
122                 foreach_section(*p, uci_show_section);
123         }
124
125         return 0;
126 }
127
128 static int uci_export(int argc, char **argv)
129 {
130         char **configs = uci_list_configs();
131         char **p;
132
133         if (!configs)
134                 return 0;
135
136         for (p = configs; *p; p++) {
137                 foreach_section(*p, uci_export_section);
138         }
139         return 0;
140 }
141
142 static int uci_cmd(int argc, char **argv)
143 {
144         if (!strcasecmp(argv[0], "show"))
145                 return uci_show(argc, argv);
146         if (!strcasecmp(argv[0], "export"))
147                 return uci_export(argc, argv);
148         return 255;
149 }
150
151 int main(int argc, char **argv)
152 {
153         int ret;
154
155         ctx = uci_alloc();
156         if (argc < 2)
157                 uci_usage(argc, argv);
158         ret = uci_cmd(argc - 1, argv + 1);
159         if (ret == 255)
160                 uci_usage(argc, argv);
161         uci_free(ctx);
162
163         return ret;
164 }