cli: indent call result data
[project/ubus.git] / cli.c
1 /*
2  * Copyright (C) 2011 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 Lesser General Public License version 2.1
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
14 #include <unistd.h>
15
16 #include <libubox/blobmsg_json.h>
17 #include "libubus.h"
18
19 static struct blob_buf b;
20 static int timeout = 30;
21 static bool simple_output = false;
22
23 static const char *format_type(void *priv, struct blob_attr *attr)
24 {
25         static const char * const attr_types[] = {
26                 [BLOBMSG_TYPE_INT8] = "\"Boolean\"",
27                 [BLOBMSG_TYPE_INT32] = "\"Integer\"",
28                 [BLOBMSG_TYPE_STRING] = "\"String\"",
29         };
30         const char *type = NULL;
31         int typeid;
32
33         if (blob_id(attr) != BLOBMSG_TYPE_INT32)
34                 return NULL;
35
36         typeid = blobmsg_get_u32(attr);
37         if (typeid < ARRAY_SIZE(attr_types))
38                 type = attr_types[typeid];
39         if (!type)
40                 type = "\"(unknown)\"";
41
42         return type;
43 }
44
45 static void receive_list_result(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
46 {
47         struct blob_attr *cur;
48         char *s;
49         int rem;
50
51         if (simple_output) {
52                 printf("%s\n", obj->path);
53                 return;
54         }
55
56         printf("'%s' @%08x\n", obj->path, obj->id);
57
58         if (!obj->signature)
59                 return;
60
61         blob_for_each_attr(cur, obj->signature, rem) {
62                 s = blobmsg_format_json_with_cb(cur, false, format_type, NULL, -1);
63                 printf("\t%s\n", s);
64                 free(s);
65         }
66 }
67
68 static void receive_call_result_data(struct ubus_request *req, int type, struct blob_attr *msg)
69 {
70         char *str;
71         if (!msg)
72                 return;
73
74         str = blobmsg_format_json_indent(msg, true, 0);
75         printf("%s\n", str);
76         free(str);
77 }
78
79 static void receive_event(struct ubus_context *ctx, struct ubus_event_handler *ev,
80                           const char *type, struct blob_attr *msg)
81 {
82         char *str;
83
84         str = blobmsg_format_json(msg, true);
85         printf("{ \"%s\": %s }\n", type, str);
86         free(str);
87 }
88
89 static int ubus_cli_list(struct ubus_context *ctx, int argc, char **argv)
90 {
91         const char *path = NULL;
92
93         if (argc > 1)
94                 return -2;
95
96         if (argc == 1)
97                 path = argv[0];
98
99         return ubus_lookup(ctx, path, receive_list_result, NULL);
100 }
101
102 static int ubus_cli_call(struct ubus_context *ctx, int argc, char **argv)
103 {
104         uint32_t id;
105         int ret;
106
107         if (argc < 2 || argc > 3)
108                 return -2;
109
110         blob_buf_init(&b, 0);
111         if (argc == 3 && !blobmsg_add_json_from_string(&b, argv[2])) {
112                 if (!simple_output)
113                         fprintf(stderr, "Failed to parse message data\n");
114                 return -1;
115         }
116
117         ret = ubus_lookup_id(ctx, argv[0], &id);
118         if (ret)
119                 return ret;
120
121         return ubus_invoke(ctx, id, argv[1], b.head, receive_call_result_data, NULL, timeout * 1000);
122 }
123
124 static int ubus_cli_listen(struct ubus_context *ctx, int argc, char **argv)
125 {
126         static struct ubus_event_handler listener;
127         const char *event;
128         int ret = 0;
129
130         memset(&listener, 0, sizeof(listener));
131         listener.cb = receive_event;
132
133         if (argc > 0) {
134                 event = argv[0];
135         } else {
136                 event = "*";
137                 argc = 1;
138         }
139
140         do {
141                 ret = ubus_register_event_handler(ctx, &listener, event);
142                 if (ret)
143                         break;
144
145                 argv++;
146                 argc--;
147                 if (argc <= 0)
148                         break;
149
150                 event = argv[0];
151         } while (1);
152
153         if (ret) {
154                 if (!simple_output)
155                         fprintf(stderr, "Error while registering for event '%s': %s\n",
156                                 event, ubus_strerror(ret));
157                 return -1;
158         }
159
160         uloop_init();
161         ubus_add_uloop(ctx);
162         uloop_run();
163         uloop_done();
164
165         return 0;
166 }
167
168 static int ubus_cli_send(struct ubus_context *ctx, int argc, char **argv)
169 {
170         if (argc < 1 || argc > 2)
171                 return -2;
172
173         blob_buf_init(&b, 0);
174
175         if (argc == 2 && !blobmsg_add_json_from_string(&b, argv[1])) {
176                 if (!simple_output)
177                         fprintf(stderr, "Failed to parse message data\n");
178                 return -1;
179         }
180
181         return ubus_send_event(ctx, argv[0], b.head);
182 }
183
184 static int usage(const char *prog)
185 {
186         fprintf(stderr,
187                 "Usage: %s [<options>] <command> [arguments...]\n"
188                 "Options:\n"
189                 " -s <socket>:          Set the unix domain socket to connect to\n"
190                 " -t <timeout>:         Set the timeout (in seconds) for a command to complete\n"
191                 " -S:                   Use simplified output (for scripts)\n"
192                 "\n"
193                 "Commands:\n"
194                 " - list [<path>]                       List objects\n"
195                 " - call <path> <method> [<message>]    Call an object method\n"
196                 " - listen [<path>...]                  Listen for events\n"
197                 " - send <type> [<message>]             Send an event\n"
198                 "\n", prog);
199         return 1;
200 }
201
202
203 struct {
204         const char *name;
205         int (*cb)(struct ubus_context *ctx, int argc, char **argv);
206 } commands[] = {
207         { "list", ubus_cli_list },
208         { "call", ubus_cli_call },
209         { "listen", ubus_cli_listen },
210         { "send", ubus_cli_send },
211 };
212
213 int main(int argc, char **argv)
214 {
215         const char *progname, *ubus_socket = NULL;
216         static struct ubus_context *ctx;
217         char *cmd;
218         int ret = 0;
219         int i, ch;
220
221         progname = argv[0];
222
223         while ((ch = getopt(argc, argv, "s:t:S")) != -1) {
224                 switch (ch) {
225                 case 's':
226                         ubus_socket = optarg;
227                         break;
228                 case 't':
229                         timeout = atoi(optarg);
230                         break;
231                 case 'S':
232                         simple_output = true;
233                         break;
234                 default:
235                         return usage(progname);
236                 }
237         }
238
239         argc -= optind;
240         argv += optind;
241
242         cmd = argv[0];
243         if (argc < 1)
244                 return usage(progname);
245
246         ctx = ubus_connect(ubus_socket);
247         if (!ctx) {
248                 if (!simple_output)
249                         fprintf(stderr, "Failed to connect to ubus\n");
250                 return -1;
251         }
252
253         argv++;
254         argc--;
255
256         ret = -2;
257         for (i = 0; i < ARRAY_SIZE(commands); i++) {
258                 if (strcmp(commands[i].name, cmd) != 0)
259                         continue;
260
261                 ret = commands[i].cb(ctx, argc, argv);
262                 break;
263         }
264
265         if (ret > 0 && !simple_output)
266                 fprintf(stderr, "Command failed: %s\n", ubus_strerror(ret));
267         else if (ret == -2)
268                 usage(progname);
269
270         ubus_free(ctx);
271         return ret;
272 }