diff --git a/lib/src/clixon_json_parse.y b/lib/src/clixon_json_parse.y index 9471942b..0279fc55 100644 --- a/lib/src/clixon_json_parse.y +++ b/lib/src/clixon_json_parse.y @@ -259,6 +259,7 @@ valuelist : value /* quoted string */ string : J_DQ ustring J_DQ { clicon_debug(2,"string->\" ustring \"");$$=$2; } + | J_DQ J_DQ { clicon_debug(2,"string->\" ustring \"");$$=strdup(""); } ; /* unquoted string */ diff --git a/lib/src/json_xpath.c b/lib/src/json_xpath.c new file mode 100644 index 00000000..d0574f4c --- /dev/null +++ b/lib/src/json_xpath.c @@ -0,0 +1,126 @@ +/* + * Utility debug tool + * Static Compile: gcc -Wall json_xpath.c -o json_xpath -l clixon +gcc -O2 -o json_xpath json_xpath.c clixon_log.o clixon_err.o clixon_json.o clixon_xml.o clixon_xsl.o clixon_json_parse.tab.o clixon_xml_parse.tab.o lex.clixon_json_parse.o lex.clixon_xml_parse.o ../../../cligen/cligen_buf.o ../../../cligen/cligen_var.o ../../../cligen/cligen_gen.o ../../../cligen/cligen_cvec.o ../../../cligen/cligen_handle.o ../../../cligen/getline.o ../../../cligen/cligen_read.o ../../../cligen/cligen_match.o ../../../cligen/cligen_expand.o ../../../cligen/cligen_print.o + */ +#include +#include +#include +#include + +/* cligen */ +#include + +/* clicon */ +#include + +static int +usage(char *argv0) +{ + fprintf(stderr, "usage:%s # XML/JSON expected on stdin\n" + "\t-h Help\n" + "\t-b Strip to body value (eg \"val\" --> \"val\"\n" + "\t-j json input (not xml)\n", + argv0); + exit(0); +} + +int +main(int argc, char **argv) +{ + int i; + cxobj **xv; + cxobj *x; + cxobj *xn; + cxobj *xb; + char *xpath; + int body = 0; + int json = 0; + size_t xlen = 0; + size_t len; + size_t buflen = 128; + char *buf; + char *p; + int retval; + char c; + + while ((c = getopt(argc, argv, "?hbj")) != -1) + switch (c) { + case '?': + case 'h': + usage(argv[0]); + break; + case 'b': + body++; + break; + case 'j': + json++; + break; + default: + usage(argv[0]); + } + if (optind >= argc) + usage(argv[0]); + xpath=argv[optind]; + + clicon_log_init("xpath", 0, CLICON_LOG_STDERR); + if ((buf = malloc(buflen)) == NULL){ + perror("malloc"); + return -1; + } + /* |---------------|-------------| + * buf p buf+buflen + */ + p = buf; + memset(p, 0, buflen); + while (1){ + if ((retval = read(0, p, buflen-(p-buf))) < 0){ + perror("read"); + return -1; + } + if (retval == 0) + break; + p += retval; + len = p-buf; + + if (buf+buflen-p < 10){ /* allocate more */ + buflen *= 2; + if ((buf = realloc(buf, buflen)) == NULL){ + perror("realloc"); + return -1; + } + p = buf+len; + memset(p, 0, (buf+buflen)-p); + } + } + if (json){ + if (json_parse_str(buf, &x) < 0) + return -1; + } + else + if (clicon_xml_parse_str(buf, &x) < 0) + return -1; + + if (xpath_vec(x, xpath, &xv, &xlen) < 0) + return -1; + if (xv){ + for (i=0; i