X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fubus.git;a=blobdiff_plain;f=cli.c;h=0690e2acb1bfccf05c533b8a5c468689160bbba7;hp=0af5b64630b1ad3ffae9c4f2643ec6fcf7a6928c;hb=a93b044d6a5a4bed51d776cb0950e7d20fb17b84;hpb=8c81142828a58e661d91da4f513a32ca5a9038bf diff --git a/cli.c b/cli.c index 0af5b64..0690e2a 100644 --- a/cli.c +++ b/cli.c @@ -1,8 +1,32 @@ +#include + #include #include "libubus.h" static struct blob_buf b; +static const char *attr_types[] = { + [BLOBMSG_TYPE_INT32] = "\"Integer\"", + [BLOBMSG_TYPE_STRING] = "\"String\"", +}; + +static const char *format_type(void *priv, struct blob_attr *attr) +{ + const char *type = NULL; + int typeid; + + if (blob_id(attr) != BLOBMSG_TYPE_INT32) + return NULL; + + typeid = blobmsg_get_u32(attr); + if (typeid < ARRAY_SIZE(attr_types)) + type = attr_types[typeid]; + if (!type) + type = "\"(unknown)\""; + + return type; +} + static void receive_lookup(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv) { struct blob_attr *cur; @@ -15,7 +39,7 @@ static void receive_lookup(struct ubus_context *ctx, struct ubus_object_data *ob return; blob_for_each_attr(cur, obj->signature, rem) { - s = blobmsg_format_json(cur, false); + s = blobmsg_format_json_with_cb(cur, false, format_type, NULL); fprintf(stderr, "\t%s\n", s); free(s); } @@ -33,18 +57,6 @@ static void receive_data(struct ubus_request *req, int type, struct blob_attr *m } -static int usage(char *prog) -{ - fprintf(stderr, - "Usage: %s [arguments...]\n" - "Commands:\n" - " - list [] List objects\n" - " - call [] Call an object method\n" - " - listen [...] Listen for events\n" - "\n", prog); - return 1; -} - static void receive_event(struct ubus_context *ctx, struct ubus_event_handler *ev, const char *type, struct blob_attr *msg) { @@ -93,48 +105,83 @@ static int ubus_cli_listen(struct ubus_context *ctx, int argc, char **argv) return 0; } +static int usage(const char *prog) +{ + fprintf(stderr, + "Usage: %s [] [arguments...]\n" + "Options:\n" + " -s : Set the unix domain socket to connect to\n" + "\n" + "Commands:\n" + " - list [] List objects\n" + " - call [] Call an object method\n" + " - listen [...] Listen for events\n" + "\n", prog); + return 1; +} + int main(int argc, char **argv) { + const char *progname, *ubus_socket = NULL; static struct ubus_context *ctx; char *cmd; int ret = 0; + int ch; + + progname = argv[0]; + + while ((ch = getopt(argc, argv, "s:")) != -1) { + switch (ch) { + case 's': + ubus_socket = optarg; + break; + default: + return usage(progname); + } + } - ctx = ubus_connect(NULL); + argc -= optind; + argv += optind; + + ctx = ubus_connect(ubus_socket); if (!ctx) { fprintf(stderr, "Failed to connect to ubus\n"); return -1; } - cmd = argv[1]; - if (argc < 2) - return usage(argv[0]); + cmd = argv[0]; + if (argc < 1) + return usage(progname); + + argv++; + argc--; if (!strcmp(cmd, "list")) { const char *path = NULL; - if (argc == 3) - path = argv[2]; + if (argc == 1) + path = argv[0]; ret = ubus_lookup(ctx, path, receive_lookup, NULL); } else if (!strcmp(cmd, "call")) { uint32_t id; - if (argc < 4 || argc > 5) - return usage(argv[0]); + if (argc < 2 || argc > 3) + return usage(progname); blob_buf_init(&b, 0); - if (argc == 5 && !blobmsg_add_json_from_string(&b, argv[4])) { + if (argc == 3 && !blobmsg_add_json_from_string(&b, argv[2])) { fprintf(stderr, "Failed to parse message data\n"); goto out; } - ret = ubus_lookup_id(ctx, argv[2], &id); + ret = ubus_lookup_id(ctx, argv[0], &id); if (!ret) - ret = ubus_invoke(ctx, id, argv[3], b.head, receive_data, NULL); + ret = ubus_invoke(ctx, id, argv[1], b.head, receive_data, NULL); } else if (!strcmp(cmd, "listen")) { - ret = ubus_cli_listen(ctx, argc - 2, argv + 2); + ret = ubus_cli_listen(ctx, argc, argv); } else { - return usage(argv[0]); + return usage(progname); } if (ret)