suppress stderr output with -S
[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                 if (!simple_output)
100                         fprintf(stderr, "Failed to parse message data\n");
101                 return -1;
102         }
103
104         ret = ubus_lookup_id(ctx, argv[0], &id);
105         if (ret)
106                 return ret;
107
108         return ubus_invoke(ctx, id, argv[1], b.head, receive_call_result_data, NULL, timeout * 1000);
109 }
110
111 static int ubus_cli_listen(struct ubus_context *ctx, int argc, char **argv)
112 {
113         static struct ubus_event_handler listener;
114         const char *event;
115         int ret = 0;
116
117         memset(&listener, 0, sizeof(listener));
118         listener.cb = receive_event;
119
120         if (argc > 0) {
121                 event = argv[0];
122         } else {
123                 event = "*";
124                 argc = 1;
125         }
126
127         do {
128                 ret = ubus_register_event_handler(ctx, &listener, event);
129                 if (ret)
130                         break;
131
132                 argv++;
133                 argc--;
134                 if (argc <= 0)
135                         break;
136
137                 event = argv[0];
138         } while (1);
139
140         if (ret) {
141                 if (!simple_output)
142                         fprintf(stderr, "Error while registering for event '%s': %s\n",
143                                 event, ubus_strerror(ret));
144                 return -1;
145         }
146
147         uloop_init();
148         ubus_add_uloop(ctx);
149         uloop_run();
150         uloop_done();
151
152         return 0;
153 }
154
155 static int ubus_cli_send(struct ubus_context *ctx, int argc, char **argv)
156 {
157         if (argc < 1 || argc > 2)
158                 return -2;
159
160         blob_buf_init(&b, 0);
161
162         if (argc == 2 && !blobmsg_add_json_from_string(&b, argv[1])) {
163                 if (!simple_output)
164                         fprintf(stderr, "Failed to parse message data\n");
165                 return -1;
166         }
167
168         return ubus_send_event(ctx, argv[0], b.head);
169 }
170
171 static int usage(const char *prog)
172 {
173         fprintf(stderr,
174                 "Usage: %s [<options>] <command> [arguments...]\n"
175                 "Options:\n"
176                 " -s <socket>:          Set the unix domain socket to connect to\n"
177                 " -t <timeout>:         Set the timeout (in seconds) for a command to complete\n"
178                 " -S:                   Use simplified output (for scripts)\n"
179                 "\n"
180                 "Commands:\n"
181                 " - list [<path>]                       List objects\n"
182                 " - call <path> <method> [<message>]    Call an object method\n"
183                 " - listen [<path>...]                  Listen for events\n"
184                 " - send <type> [<message>]             Send an event\n"
185                 "\n", prog);
186         return 1;
187 }
188
189
190 struct {
191         const char *name;
192         int (*cb)(struct ubus_context *ctx, int argc, char **argv);
193 } commands[] = {
194         { "list", ubus_cli_list },
195         { "call", ubus_cli_call },
196         { "listen", ubus_cli_listen },
197         { "send", ubus_cli_send },
198 };
199
200 int main(int argc, char **argv)
201 {
202         const char *progname, *ubus_socket = NULL;
203         static struct ubus_context *ctx;
204         char *cmd;
205         int ret = 0;
206         int i, ch;
207
208         progname = argv[0];
209
210         while ((ch = getopt(argc, argv, "s:t:S")) != -1) {
211                 switch (ch) {
212                 case 's':
213                         ubus_socket = optarg;
214                         break;
215                 case 't':
216                         timeout = atoi(optarg);
217                         break;
218                 case 'S':
219                         simple_output = true;
220                         break;
221                 default:
222                         return usage(progname);
223                 }
224         }
225
226         argc -= optind;
227         argv += optind;
228
229         cmd = argv[0];
230         if (argc < 1)
231                 return usage(progname);
232
233         ctx = ubus_connect(ubus_socket);
234         if (!ctx) {
235                 if (!simple_output)
236                         fprintf(stderr, "Failed to connect to ubus\n");
237                 return -1;
238         }
239
240         argv++;
241         argc--;
242
243         ret = -2;
244         for (i = 0; i < ARRAY_SIZE(commands); i++) {
245                 if (strcmp(commands[i].name, cmd) != 0)
246                         continue;
247
248                 ret = commands[i].cb(ctx, argc, argv);
249                 break;
250         }
251
252         if (ret > 0 && !simple_output)
253                 fprintf(stderr, "Command failed: %s\n", ubus_strerror(ret));
254         else if (ret == -2)
255                 usage(progname);
256
257         ubus_free(ctx);
258         return ret;
259 }