From 34b59acc831e197978952dec7f503a20e1cf722f Mon Sep 17 00:00:00 2001 From: Pau Espin Pedrol Date: Mon, 12 Mar 2018 18:02:05 +0100 Subject: abisip-find: Add --format-json option This format outputs json format which can be more easily parsed if launched by scripting launguage with json support like python. Change-Id: Ib2d461c79fbc242141dc342578467c3e9c6ed5fc --- src/ipaccess/abisip-find.c | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) (limited to 'src/ipaccess') diff --git a/src/ipaccess/abisip-find.c b/src/ipaccess/abisip-find.c index defe2e4ac..03f579b2d 100644 --- a/src/ipaccess/abisip-find.c +++ b/src/ipaccess/abisip-find.c @@ -43,12 +43,14 @@ static struct { int send_interval; bool list_view; time_t list_view_timeout; + bool format_json; } cmdline_opts = { .ifname = NULL, .bind_ip = NULL, .send_interval = 5, .list_view = false, .list_view_timeout = 10, + .format_json = false, }; static void print_help() @@ -66,6 +68,7 @@ static void print_help() printf(" -t --timeout Drop base stations after seconds of\n" " receiving no more replies from it.\n" " Implies --list-view.\n"); + printf(" -j --format-json Print BTS information using json syntax.\n"); } static void handle_options(int argc, char **argv) @@ -78,10 +81,11 @@ static void handle_options(int argc, char **argv) {"send-interval", 1, 0, 'i'}, {"list-view", 0, 0, 'l'}, {"timeout", 1, 0, 't'}, + {"format-json", 0, 0, 'j'}, {0, 0, 0, 0} }; - c = getopt_long(argc, argv, "hb:i:lt:", + c = getopt_long(argc, argv, "hb:i:lt:j", long_options, &option_index); if (c == -1) break; @@ -112,6 +116,9 @@ static void handle_options(int argc, char **argv) case 'l': cmdline_opts.list_view = true; break; + case 'j': + cmdline_opts.format_json = true; + break; default: /* catch unknown options *as well as* missing arguments. */ fprintf(stderr, "Error in command line options. Exiting. Try --help.\n"); @@ -217,20 +224,34 @@ static int bcast_find(int fd) static char *parse_response(void *ctx, unsigned char *buf, int len) { + unsigned int out_len; uint8_t t_len; uint8_t t_tag; uint8_t *cur = buf; char *out = talloc_zero_size(ctx, 512); + if (cmdline_opts.format_json) + out = talloc_asprintf_append(out,"{ "); + while (cur < buf + len) { t_len = *cur++; t_tag = *cur++; - - out = talloc_asprintf_append(out, "%s='%s' ", ipa_ccm_idtag_name(t_tag), cur); + + if (cmdline_opts.format_json) + out = talloc_asprintf_append(out, "\"%s\": \"%s\", ", ipa_ccm_idtag_name(t_tag), cur); + else + out = talloc_asprintf_append(out, "%s='%s' ", ipa_ccm_idtag_name(t_tag), cur); cur += t_len; } + if (cmdline_opts.format_json) { + out_len = strlen(out); + if (out[out_len-2] == ',') + out[out_len-2] = ' '; + out[out_len-1] = '}'; + } + return out; } @@ -308,10 +329,23 @@ void base_stations_print() int count = 0; print_timestamp(); + if (cmdline_opts.format_json) + printf("["); + llist_for_each_entry(bs, &base_stations, entry) { - printf("%3d: %s\n", count, bs->line); + if (cmdline_opts.format_json) { + if (count) + printf(","); + printf("\n%s", bs->line); + } else { + printf("%3d: %s\n", count, bs->line); + } count++; } + + if (cmdline_opts.format_json) + printf("%c]\n", count ? '\n': ' '); + printf("\nTotal: %d\n", count); } -- cgit v1.2.3