2 * Copyright (c) 2014, 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
18 #include "xoversion.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, int bufsiz,
96 const char *fmt, va_list vap UNUSED)
98 int lflag = 0, hflag = 0, jflag = 0, tflag = 0,
99 zflag = 0, qflag = 0, star1 = 0, star2 = 0;
104 for (cp = fmt + 1; *cp; cp++) {
117 else if (*cp == '*') {
122 } else if (strchr("diouxXDOUeEfFgGaAcCsSp", *cp) != NULL)
124 else if (*cp == 'n' || *cp == 'v') {
126 xo_error_h(xop, "unsupported format: '%s'", fmt);
135 w1 = strtol(next_arg(), NULL, 0);
137 w2 = strtol(next_arg(), NULL, 0);
139 if (fc == 'D' || fc == 'O' || fc == 'U')
142 if (strchr("diD", fc) != NULL) {
143 long long value = strtoll(next_arg(), NULL, 0);
145 rc = snprintf(buf, bufsiz, fmt, w1, w2, value);
147 rc = snprintf(buf, bufsiz, fmt, w1, value);
149 rc = snprintf(buf, bufsiz, fmt, value);
151 } else if (strchr("ouxXOUp", fc) != NULL) {
152 unsigned long long value = strtoull(next_arg(), NULL, 0);
154 rc = snprintf(buf, bufsiz, fmt, w1, w2, value);
156 rc = snprintf(buf, bufsiz, fmt, w1, value);
158 rc = snprintf(buf, bufsiz, fmt, value);
160 } else if (strchr("eEfFgGaA", fc) != NULL) {
161 double value = strtold(next_arg(), NULL);
163 rc = snprintf(buf, bufsiz, fmt, w1, w2, value);
165 rc = snprintf(buf, bufsiz, fmt, w1, value);
167 rc = snprintf(buf, bufsiz, fmt, value);
169 } else if (fc == 'C' || fc == 'c' || fc == 'S' || fc == 's') {
170 char *value = next_arg();
172 rc = snprintf(buf, bufsiz, fmt, w1, w2, value);
174 rc = snprintf(buf, bufsiz, fmt, w1, value);
176 rc = snprintf(buf, bufsiz, fmt, value);
185 fprintf(stderr, "libxo version %s%s\n",
186 xo_version, xo_version_extra);
187 fprintf(stderr, "xo version %s%s\n",
188 LIBXO_VERSION, LIBXO_VERSION_EXTRA);
195 "Usage: xo [options] format [fields]\n"
196 " --close <path> Close tags for the given path\n"
197 " --depth <num> Set the depth for pretty printing\n"
198 " --help Display this help text\n"
199 " --html OR -H Generate HTML output\n"
200 " --json OR -J Generate JSON output\n"
201 " --leading-xpath <path> OR -l <path> "
202 "Add a prefix to generated XPaths (HTML)\n"
203 " --open <path> Open tags for the given path\n"
204 " --pretty OR -p Make 'pretty' output (add indent, newlines)\n"
205 " --style <style> OR -s <style> "
206 "Generate given style (xml, json, text, html)\n"
207 " --text OR -T Generate text output (the default style)\n"
208 " --version Display version information\n"
209 " --warn OR -W Display warnings in text on stderr\n"
210 " --warn-xml Display warnings in xml on stdout\n"
211 " --wrap <path> Wrap output in a set of containers\n"
212 " --xml OR -X Generate XML output\n"
213 " --xpath Add XPath data to HTML output\n");
226 static struct option long_opts[] = {
227 { "close", required_argument, NULL, 'c' },
228 { "depth", required_argument, &opts.o_depth, 1 },
229 { "help", no_argument, &opts.o_help, 1 },
230 { "html", no_argument, NULL, 'H' },
231 { "json", no_argument, NULL, 'J' },
232 { "leading-xpath", required_argument, NULL, 'l' },
233 { "not-first", no_argument, &opts.o_not_first, 1 },
234 { "open", required_argument, NULL, 'o' },
235 { "option", required_argument, NULL, 'O' },
236 { "pretty", no_argument, NULL, 'p' },
237 { "style", required_argument, NULL, 's' },
238 { "text", no_argument, NULL, 'T' },
239 { "xml", no_argument, NULL, 'X' },
240 { "xpath", no_argument, &opts.o_xpath, 1 },
241 { "version", no_argument, &opts.o_version, 1 },
242 { "warn", no_argument, NULL, 'W' },
243 { "warn-xml", no_argument, &opts.o_warn_xml, 1 },
244 { "wrap", required_argument, &opts.o_wrap, 1 },
249 main (int argc UNUSED, char **argv)
251 char *fmt = NULL, *cp, *np;
252 char *opt_opener = NULL, *opt_closer = NULL, *opt_wrapper = NULL;
253 char *opt_options = NULL;
255 int opt_not_first = 0;
258 argc = xo_parse_args(argc, argv);
262 while ((rc = getopt_long(argc, argv, "c:HJl:ps:TXW",
263 long_opts, NULL)) != -1) {
267 xo_set_flags(NULL, XOF_IGNORE_CLOSE);
271 xo_set_style(NULL, XO_STYLE_HTML);
275 xo_set_style(NULL, XO_STYLE_JSON);
279 xo_set_leading_xpath(NULL, optarg);
283 opt_options = optarg;
291 xo_set_flags(NULL, XOF_PRETTY);
295 if (xo_set_style_name(NULL, optarg) < 0)
296 xo_errx(1, "unknown style: %s", optarg);
300 xo_set_style(NULL, XO_STYLE_TEXT);
304 xo_set_style(NULL, XO_STYLE_XML);
309 xo_set_flags(NULL, XOF_WARN);
313 xo_errx(1, "missing argument");
318 opt_depth = atoi(optarg);
320 } else if (opts.o_help) {
324 } else if (opts.o_not_first) {
327 } else if (opts.o_xpath) {
328 xo_set_flags(NULL, XOF_XPATH);
330 } else if (opts.o_version) {
334 } else if (opts.o_warn_xml) {
336 xo_set_flags(NULL, XOF_WARN | XOF_WARN_XML);
338 } else if (opts.o_wrap) {
339 opt_wrapper = optarg;
346 bzero(&opts, sizeof(opts)); /* Reset all the options */
359 rc = xo_set_options(NULL, opt_options);
361 xo_errx(1, "invalid options: %s", opt_options);
364 xo_set_formatter(NULL, formatter, checkpoint);
365 xo_set_flags(NULL, XOF_NO_VA_ARG | XOF_NO_TOP | XOF_NO_CLOSE);
368 if (opt_opener == NULL && opt_closer == NULL && fmt == NULL) {
374 xo_set_flags(NULL, XOF_NOT_FIRST);
378 for (cp = opt_closer; cp && *cp; cp = np) {
379 np = strchr(cp, '/');
388 xo_set_depth(NULL, opt_depth);
391 for (cp = opt_opener; cp && *cp; cp = np) {
392 np = strchr(cp, '/');
395 xo_open_container(cp);
402 for (cp = opt_wrapper; cp && *cp; cp = np) {
403 np = strchr(cp, '/');
406 xo_open_container(cp);
418 while (opt_wrapper) {
419 np = strrchr(opt_wrapper, '/');
420 xo_close_container(np ? np + 1 : opt_wrapper);
428 np = strrchr(opt_closer, '/');
429 xo_close_container(np ? np + 1 : opt_closer);