2 * Copyright (c) 2014-2019, Juniper Networks, Inc.
4 * This SOFTWARE is licensed under the LICENSE provided in the
5 * ../Copyright file. By downloading, installing, copying, or otherwise
6 * using the SOFTWARE, you agree to be bound by the terms of that
8 * Phil Shafer, July 2014
16 #include "xo_config.h"
18 #include "xo_explicit.h"
20 #include <getopt.h> /* Include after xo.h for testing */
23 #define UNUSED __attribute__ ((__unused__))
26 static int opt_warn; /* Enable warnings */
28 static char **save_argv;
29 static char **checkpoint_argv;
34 char *cp = *save_argv;
37 xo_errx(1, "missing argument");
48 for (cp = fp = fmt; *cp; cp++, fp++) {
81 checkpoint (xo_handle_t *xop UNUSED, va_list vap UNUSED, int restore)
84 save_argv = checkpoint_argv;
86 checkpoint_argv = save_argv;
90 * Our custom formatter is responsible for combining format string pieces
91 * with our command line arguments to build strings. This involves faking
92 * some printf-style logic.
95 formatter (xo_handle_t *xop, char *buf, xo_ssize_t bufsiz,
96 const char *fmt, va_list vap UNUSED)
98 int lflag UNUSED = 0; /* Parse long flag, though currently ignored */
99 int hflag = 0, jflag = 0, tflag = 0,
100 zflag = 0, qflag = 0, star1 = 0, star2 = 0;
105 for (cp = fmt + 1; *cp; cp++) {
118 else if (*cp == '*') {
123 } else if (strchr("diouxXDOUeEfFgGaAcCsSp", *cp) != NULL)
125 else if (*cp == 'n' || *cp == 'v') {
127 xo_error_h(xop, "unsupported format: '%s'", fmt);
136 w1 = strtol(next_arg(), NULL, 0);
138 w2 = strtol(next_arg(), NULL, 0);
140 if (fc == 'D' || fc == 'O' || fc == 'U')
143 if (strchr("diD", fc) != NULL) {
144 long long value = strtoll(next_arg(), NULL, 0);
146 rc = snprintf(buf, bufsiz, fmt, w1, w2, value);
148 rc = snprintf(buf, bufsiz, fmt, w1, value);
150 rc = snprintf(buf, bufsiz, fmt, value);
152 } else if (strchr("ouxXOUp", fc) != NULL) {
153 unsigned long long value = strtoull(next_arg(), NULL, 0);
155 rc = snprintf(buf, bufsiz, fmt, w1, w2, value);
157 rc = snprintf(buf, bufsiz, fmt, w1, value);
159 rc = snprintf(buf, bufsiz, fmt, value);
161 } else if (strchr("eEfFgGaA", fc) != NULL) {
162 double value = strtold(next_arg(), NULL);
164 rc = snprintf(buf, bufsiz, fmt, w1, w2, value);
166 rc = snprintf(buf, bufsiz, fmt, w1, value);
168 rc = snprintf(buf, bufsiz, fmt, value);
170 } else if (fc == 'C' || fc == 'c' || fc == 'S' || fc == 's') {
171 char *value = next_arg();
173 rc = snprintf(buf, bufsiz, fmt, w1, w2, value);
175 rc = snprintf(buf, bufsiz, fmt, w1, value);
177 rc = snprintf(buf, bufsiz, fmt, value);
186 fprintf(stderr, "libxo version %s%s\n",
187 xo_version, xo_version_extra);
188 fprintf(stderr, "xo version %s%s\n",
189 LIBXO_VERSION, LIBXO_VERSION_EXTRA);
196 "Usage: xo [options] format [fields]\n"
197 " --close <path> Close tags for the given path\n"
198 " --close-instance <name> Close an open instance name\n"
199 " --close-list <name> Close an open list name\n"
200 " --continuation OR -C Output belongs on same line as previous output\n"
201 " --depth <num> Set the depth for pretty printing\n"
202 " --help Display this help text\n"
203 " --html OR -H Generate HTML output\n"
204 " --instance OR -I <name> Wrap in an instance of the given name\n"
205 " --json OR -J Generate JSON output\n"
206 " --leading-xpath <path> OR -l <path> "
207 "Add a prefix to generated XPaths (HTML)\n"
208 " --not-first Indicate this object is not the first (JSON)\n"
209 " --open <path> Open tags for the given path\n"
210 " --open-instance <name> Open an instance given by name\n"
211 " --open-list <name> Open a list given by name\n"
212 " --option <opts> -or -O <opts> Give formatting options\n"
213 " --pretty OR -p Make 'pretty' output (add indent, newlines)\n"
214 " --style <style> OR -s <style> "
215 "Generate given style (xml, json, text, html)\n"
216 " --text OR -T Generate text output (the default style)\n"
217 " --top-wrap Generate a top-level object wrapper (JSON)\n"
218 " --version Display version information\n"
219 " --warn OR -W Display warnings in text on stderr\n"
220 " --warn-xml Display warnings in xml on stdout\n"
221 " --wrap <path> Wrap output in a set of containers\n"
222 " --xml OR -X Generate XML output\n"
223 " --xpath Add XPath data to HTML output\n");
227 int o_close_instance;
241 static struct option long_opts[] = {
242 { "close", required_argument, NULL, 'c' },
243 { "close-instance", required_argument, &opts.o_close_instance, 1 },
244 { "close-list", required_argument, &opts.o_close_list, 1 },
245 { "continuation", no_argument, NULL, 'C' },
246 { "depth", required_argument, &opts.o_depth, 1 },
247 { "help", no_argument, &opts.o_help, 1 },
248 { "html", no_argument, NULL, 'H' },
249 { "instance", required_argument, NULL, 'I' },
250 { "json", no_argument, NULL, 'J' },
251 { "leading-xpath", required_argument, NULL, 'l' },
252 { "not-first", no_argument, &opts.o_not_first, 1 },
253 { "open", required_argument, NULL, 'o' },
254 { "open-instance", required_argument, &opts.o_open_instance, 1 },
255 { "open-list", required_argument, &opts.o_open_list, 1 },
256 { "option", required_argument, NULL, 'O' },
257 { "pretty", no_argument, NULL, 'p' },
258 { "style", required_argument, NULL, 's' },
259 { "text", no_argument, NULL, 'T' },
260 { "top-wrap", no_argument, &opts.o_top_wrap, 1 },
261 { "xml", no_argument, NULL, 'X' },
262 { "xpath", no_argument, &opts.o_xpath, 1 },
263 { "version", no_argument, &opts.o_version, 1 },
264 { "warn", no_argument, NULL, 'W' },
265 { "warn-xml", no_argument, &opts.o_warn_xml, 1 },
266 { "wrap", required_argument, &opts.o_wrap, 1 },
271 main (int argc UNUSED, char **argv)
273 char *fmt = NULL, *cp, *np;
274 char *opt_opener = NULL, *opt_closer = NULL, *opt_wrapper = NULL;
275 char *opt_options = NULL;
276 char *opt_instance = NULL;
277 char *opt_name = NULL;
278 xo_state_t new_state = 0;
280 int opt_not_first = 0;
281 int opt_top_wrap = 0;
284 argc = xo_parse_args(argc, argv);
288 while ((rc = getopt_long(argc, argv, "Cc:HJl:O:o:ps:TXW",
289 long_opts, NULL)) != -1) {
292 xo_set_flags(NULL, XOF_CONTINUATION);
297 xo_set_flags(NULL, XOF_IGNORE_CLOSE);
301 xo_set_style(NULL, XO_STYLE_HTML);
305 opt_instance = optarg;
309 xo_set_style(NULL, XO_STYLE_JSON);
313 xo_set_leading_xpath(NULL, optarg);
317 opt_options = optarg;
325 xo_set_flags(NULL, XOF_PRETTY);
329 if (xo_set_style_name(NULL, optarg) < 0)
330 xo_errx(1, "unknown style: %s", optarg);
334 xo_set_style(NULL, XO_STYLE_TEXT);
338 xo_set_style(NULL, XO_STYLE_XML);
343 xo_set_flags(NULL, XOF_WARN);
347 xo_errx(1, "missing argument");
352 opt_depth = atoi(optarg);
354 } else if (opts.o_help) {
358 } else if (opts.o_not_first) {
361 } else if (opts.o_xpath) {
362 xo_set_flags(NULL, XOF_XPATH);
364 } else if (opts.o_version) {
368 } else if (opts.o_warn_xml) {
370 xo_set_flags(NULL, XOF_WARN | XOF_WARN_XML);
372 } else if (opts.o_wrap) {
373 opt_wrapper = optarg;
375 } else if (opts.o_top_wrap) {
378 } else if (opts.o_open_list) {
380 xo_errx(1, "only one open/close list/instance allowed: %s",
384 new_state = XSS_OPEN_LIST;
386 } else if (opts.o_open_instance) {
388 xo_errx(1, "only one open/close list/instance allowed: %s",
392 new_state = XSS_OPEN_INSTANCE;
394 } else if (opts.o_close_list) {
396 xo_errx(1, "only one open/close list/instance allowed: %s",
400 new_state = XSS_CLOSE_LIST;
402 } else if (opts.o_close_instance) {
404 xo_errx(1, "only one open/close list/instance allowed: %s",
408 new_state = XSS_CLOSE_INSTANCE;
415 bzero(&opts, sizeof(opts)); /* Reset all the options */
428 rc = xo_set_options(NULL, opt_options);
430 xo_errx(1, "invalid options: %s", opt_options);
433 xo_set_formatter(NULL, formatter, checkpoint);
434 xo_set_flags(NULL, XOF_NO_VA_ARG | XOF_NO_TOP | XOF_NO_CLOSE);
437 * If we have some explicit state change, handle it
441 xo_set_depth(NULL, opt_depth);
444 xo_set_flags(NULL, XOF_NOT_FIRST);
446 xo_explicit_transition(NULL, new_state, opt_name, 0);
452 if (opt_opener == NULL && opt_closer == NULL && fmt == NULL) {
458 /* If we have a closing path, we'll be one extra level deeper */
459 if (opt_closer && xo_get_style(NULL) == XO_STYLE_JSON)
462 xo_clear_flags(NULL, XOF_NO_TOP);
467 for (cp = opt_closer; cp && *cp; cp = np) {
468 np = strchr(cp, '/');
477 xo_set_depth(NULL, opt_depth);
480 xo_set_flags(NULL, XOF_NOT_FIRST);
482 /* If there's an opening hierarchy, open each element as a container */
484 for (cp = opt_opener; cp && *cp; cp = np) {
485 np = strchr(cp, '/');
488 xo_open_container(cp);
494 /* If there's an wrapper hierarchy, open each element as a container */
496 for (cp = opt_wrapper; cp && *cp; cp = np) {
497 np = strchr(cp, '/');
500 xo_open_container(cp);
502 *np++ = '/'; /* Put it back */
507 xo_open_instance(opt_instance);
509 /* If there's a format string, call xo_emit to emit the contents */
513 xo_emit(fmt); /* This call does the real formatting */
517 xo_close_instance(opt_instance);
519 /* If there's an wrapper hierarchy, close each element's container */
520 while (opt_wrapper) {
521 np = strrchr(opt_wrapper, '/');
522 xo_close_container(np ? np + 1 : opt_wrapper);
529 /* Remember to undo the depth before calling xo_finish() */
530 opt_depth = (opt_closer && opt_top_wrap) ? -1 : 0;
532 /* If there's an closing hierarchy, close each element's container */
534 np = strrchr(opt_closer, '/');
535 xo_close_container(np ? np + 1 : opt_closer);
542 /* If there's a closer and a wrapper, we need to clean it up */
544 xo_set_depth(NULL, opt_depth);
545 xo_clear_flags(NULL, XOF_NO_TOP);
548 /* If we're wrapping the entire content, skip the closer */
549 if (opt_top_wrap && opt_opener)
550 xo_set_flags(NULL, XOF_NO_TOP);