X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fubus.git;a=blobdiff_plain;f=cli.c;h=de159473cb58077b2426a9c97eb5ebf9cd72b312;hp=d020b8e652cbaea953dcb92ea5d6c0b682c5ccd3;hb=c6f705451527e6e5e8f5a2715f11478eeb8799e4;hpb=fb45e383c2985c43a9aaf42050fef039473745ce diff --git a/cli.c b/cli.c index d020b8e..de15947 100644 --- a/cli.c +++ b/cli.c @@ -1,13 +1,30 @@ +/* + * Copyright (C) 2011 Felix Fietkau + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + #include #include #include "libubus.h" static struct blob_buf b; +static int timeout = 30; +static bool simple_output = false; +static int verbose = 0; static const char *format_type(void *priv, struct blob_attr *attr) { static const char * const attr_types[] = { + [BLOBMSG_TYPE_INT8] = "\"Boolean\"", [BLOBMSG_TYPE_INT32] = "\"Integer\"", [BLOBMSG_TYPE_STRING] = "\"String\"", }; @@ -32,13 +49,18 @@ static void receive_list_result(struct ubus_context *ctx, struct ubus_object_dat char *s; int rem; + if (simple_output || !verbose) { + printf("%s\n", obj->path); + return; + } + printf("'%s' @%08x\n", obj->path, obj->id); if (!obj->signature) return; blob_for_each_attr(cur, obj->signature, rem) { - s = blobmsg_format_json_with_cb(cur, false, format_type, NULL); + s = blobmsg_format_json_with_cb(cur, false, format_type, NULL, -1); printf("\t%s\n", s); free(s); } @@ -50,7 +72,7 @@ static void receive_call_result_data(struct ubus_request *req, int type, struct if (!msg) return; - str = blobmsg_format_json(msg, true); + str = blobmsg_format_json_indent(msg, true, simple_output ? -1 : 0); printf("%s\n", str); free(str); } @@ -61,7 +83,7 @@ static void receive_event(struct ubus_context *ctx, struct ubus_event_handler *e char *str; str = blobmsg_format_json(msg, true); - printf("\"%s\": %s\n", type, str); + printf("{ \"%s\": %s }\n", type, str); free(str); } @@ -88,7 +110,8 @@ static int ubus_cli_call(struct ubus_context *ctx, int argc, char **argv) blob_buf_init(&b, 0); if (argc == 3 && !blobmsg_add_json_from_string(&b, argv[2])) { - fprintf(stderr, "Failed to parse message data\n"); + if (!simple_output) + fprintf(stderr, "Failed to parse message data\n"); return -1; } @@ -96,7 +119,7 @@ static int ubus_cli_call(struct ubus_context *ctx, int argc, char **argv) if (ret) return ret; - return ubus_invoke(ctx, id, argv[1], b.head, receive_call_result_data, NULL); + return ubus_invoke(ctx, id, argv[1], b.head, receive_call_result_data, NULL, timeout * 1000); } static int ubus_cli_listen(struct ubus_context *ctx, int argc, char **argv) @@ -129,8 +152,9 @@ static int ubus_cli_listen(struct ubus_context *ctx, int argc, char **argv) } while (1); if (ret) { - fprintf(stderr, "Error while registering for event '%s': %s\n", - event, ubus_strerror(ret)); + if (!simple_output) + fprintf(stderr, "Error while registering for event '%s': %s\n", + event, ubus_strerror(ret)); return -1; } @@ -150,7 +174,8 @@ static int ubus_cli_send(struct ubus_context *ctx, int argc, char **argv) blob_buf_init(&b, 0); if (argc == 2 && !blobmsg_add_json_from_string(&b, argv[1])) { - fprintf(stderr, "Failed to parse message data\n"); + if (!simple_output) + fprintf(stderr, "Failed to parse message data\n"); return -1; } @@ -163,6 +188,9 @@ static int usage(const char *prog) "Usage: %s [] [arguments...]\n" "Options:\n" " -s : Set the unix domain socket to connect to\n" + " -t : Set the timeout (in seconds) for a command to complete\n" + " -S: Use simplified output (for scripts)\n" + " -v: More verbose output\n" "\n" "Commands:\n" " - list [] List objects\n" @@ -194,11 +222,20 @@ int main(int argc, char **argv) progname = argv[0]; - while ((ch = getopt(argc, argv, "s:")) != -1) { + while ((ch = getopt(argc, argv, "vs:t:S")) != -1) { switch (ch) { case 's': ubus_socket = optarg; break; + case 't': + timeout = atoi(optarg); + break; + case 'S': + simple_output = true; + break; + case 'v': + verbose++; + break; default: return usage(progname); } @@ -213,7 +250,8 @@ int main(int argc, char **argv) ctx = ubus_connect(ubus_socket); if (!ctx) { - fprintf(stderr, "Failed to connect to ubus\n"); + if (!simple_output) + fprintf(stderr, "Failed to connect to ubus\n"); return -1; } @@ -229,7 +267,7 @@ int main(int argc, char **argv) break; } - if (ret > 0) + if (ret > 0 && !simple_output) fprintf(stderr, "Command failed: %s\n", ubus_strerror(ret)); else if (ret == -2) usage(progname);