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