JSON updates

This commit is contained in:
Olof hagsand 2019-01-20 19:13:40 +01:00
parent 19f26e6838
commit 34d7f60ca4
6 changed files with 81 additions and 17 deletions

View file

@ -70,7 +70,13 @@
static int
usage(char *argv0)
{
fprintf(stderr, "usage:%s.\n\tInput on stdin\n", argv0);
fprintf(stderr, "usage:%s [options]\n"
"where options are\n"
"\t-h \t\tHelp\n"
"\t-D <level> \tDebug\n"
"\t-j \t\tOutput as JSON\n"
"\t-l <s|e|o> \tLog on (s)yslog, std(e)rr, std(o)ut (stderr is default)\n",
argv0);
exit(0);
}
@ -78,25 +84,52 @@ int
main(int argc,
char **argv)
{
int retval = -1;
cxobj *xt = NULL;
cxobj *xc;
cbuf *cb = cbuf_new();
int c;
int logdst = CLICON_LOG_STDERR;
int json = 0;
if (argc != 1){
usage(argv[0]);
return 0;
}
clicon_log_init(__FILE__, LOG_INFO, CLICON_LOG_STDERR);
optind = 1;
opterr = 0;
while ((c = getopt(argc, argv, "hD:jl:")) != -1)
switch (c) {
case 'h':
usage(argv[0]);
break;
case 'D':
if (sscanf(optarg, "%d", &debug) != 1)
usage(argv[0]);
break;
case 'j':
json++;
break;
case 'l': /* Log destination: s|e|o|f */
if ((logdst = clicon_log_opt(optarg[0])) < 0)
usage(argv[0]);
break;
default:
usage(argv[0]);
break;
}
clicon_log_init(__FILE__, debug?LOG_DEBUG:LOG_INFO, logdst);
if (json_parse_file(0, NULL, &xt) < 0)
goto done;
xc = NULL;
while ((xc = xml_child_each(xt, xc, -1)) != NULL)
clicon_xml2cbuf(cb, xc, 0, 0); /* print xml */
if (json)
xml2json_cbuf(cb, xc, 0); /* print xml */
else
clicon_xml2cbuf(cb, xc, 0, 0); /* print xml */
fprintf(stdout, "%s", cbuf_get(cb));
fflush(stdout);
retval = 0;
done:
if (xt)
xml_free(xt);
if (cb)
cbuf_free(cb);
return 0;
return retval;
}