X-Git-Url: http://git.openwrt.org/?a=blobdiff_plain;f=main.c;h=85a53f4af40e4d02429ee2fdfda597780b7e1f96;hb=dea067ad67d977c247c300c06676a06adf21e0c7;hp=0f1a39b30c507e5a775e6dfa538e74716645e165;hpb=cdc760c58077f44fc40adbbe41e1556a67c1b9a9;p=project%2Fjsonpath.git diff --git a/main.c b/main.c index 0f1a39b..85a53f4 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013-2014 Jo-Philipp Wich + * Copyright (C) 2013-2014 Jo-Philipp Wich * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -16,6 +16,7 @@ #include #include +#include #include #include @@ -37,6 +38,42 @@ struct match_item { struct list_head list; }; +static void +print_usage(char *app) +{ + printf( + "== Usage ==\n\n" + " # %s [-i | -s \"json...\"] {-t | -e }\n" + " -q Quiet, no errors are printed\n" + " -h, --help Print this help\n" + " -i path Specify a JSON file to parse\n" + " -s \"json\" Specify a JSON string to parse\n" + " -l limit Specify max number of results to show\n" + " -F separator Specify a field separator when using export\n" + " -t Print the type of values matched by pattern\n" + " -e Print the values matched by pattern\n" + " -e VAR= Serialize matched value for shell \"eval\"\n\n" + "== Patterns ==\n\n" + " Patterns are JsonPath: http://goessner.net/articles/JsonPath/\n" + " This tool implements $, @, [], * and the union operator ','\n" + " plus the usual expressions and literals.\n" + " It does not support the recursive child search operator '..' or\n" + " the '?()' and '()' filter expressions as those would require a\n" + " complete JavaScript engine to support them.\n\n" + "== Examples ==\n\n" + " Display the first IPv4 address on lan:\n" + " # ifstatus lan | %s -e '@[\"ipv4-address\"][0].address'\n\n" + " Extract the release string from the board information:\n" + " # ubus call system board | %s -e '@.release.description'\n\n" + " Find all interfaces which are up:\n" + " # ubus call network.interface dump | \\\n" + " %s -e '@.interface[@.up=true].interface'\n\n" + " Export br-lan traffic counters for shell eval:\n" + " # devstatus br-lan | %s -e 'RX=@.statistics.rx_bytes' \\\n" + " -e 'TX=@.statistics.tx_bytes'\n", + app, app, app, app, app); +} + static struct json_object * parse_json(FILE *fd, const char *source, const char **error) { @@ -175,7 +212,7 @@ export_value(struct list_head *matches, const char *prefix, const char *sep, case json_type_int: print_separator(sep, &sc, sl); - printf("%d", json_object_get_int(item->jsobj)); + printf("%" PRId64, json_object_get_int64(item->jsobj)); break; case json_type_double: @@ -382,10 +419,20 @@ int main(int argc, char **argv) struct json_object *jsobj = NULL; const char *jserr = NULL, *source = NULL, *separator = " "; - while ((opt = getopt(argc, argv, "i:s:e:t:F:l:q")) != -1) + if (argc == 1) + { + print_usage(argv[0]); + goto out; + } + + while ((opt = getopt(argc, argv, "hi:s:e:t:F:l:q")) != -1) { switch (opt) { + case 'h': + print_usage(argv[0]); + goto out; + case 'i': input = fopen(optarg, "r"); @@ -444,7 +491,7 @@ out: if (jsobj) json_object_put(jsobj); - if (input != stdin) + if (input && input != stdin) fclose(input); return rv;