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
16 #include "xo_config.h"
19 #include <getopt.h> /* Include after xo.h for testing */
22 #define UNUSED __attribute__ ((__unused__))
25 static int opt_warn; /* Enable warnings */
27 static char **save_argv;
28 static char **checkpoint_argv;
33 char *cp = *save_argv;
36 xo_errx(1, "missing argument");
47 for (cp = fp = fmt; *cp; cp++, fp++) {
80 checkpoint (xo_handle_t *xop UNUSED, va_list vap UNUSED, int restore)
83 save_argv = checkpoint_argv;
85 checkpoint_argv = save_argv;
89 * Our custom formatter is responsible for combining format string pieces
90 * with our command line arguments to build strings. This involves faking
91 * some printf-style logic.
94 formatter (xo_handle_t *xop, char *buf, int bufsiz,
95 const char *fmt, va_list vap UNUSED)
97 int lflag UNUSED = 0; /* Parse long flag, though currently ignored */
98 int 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 " --option <opts> -or -O <opts> Give formatting options\n"
205 " --pretty OR -p Make 'pretty' output (add indent, newlines)\n"
206 " --style <style> OR -s <style> "
207 "Generate given style (xml, json, text, html)\n"
208 " --text OR -T Generate text output (the default style)\n"
209 " --version Display version information\n"
210 " --warn OR -W Display warnings in text on stderr\n"
211 " --warn-xml Display warnings in xml on stdout\n"
212 " --wrap <path> Wrap output in a set of containers\n"
213 " --xml OR -X Generate XML output\n"
214 " --xpath Add XPath data to HTML output\n");
227 static struct option long_opts[] = {
228 { "close", required_argument, NULL, 'c' },
229 { "depth", required_argument, &opts.o_depth, 1 },
230 { "help", no_argument, &opts.o_help, 1 },
231 { "html", no_argument, NULL, 'H' },
232 { "json", no_argument, NULL, 'J' },
233 { "leading-xpath", required_argument, NULL, 'l' },
234 { "not-first", no_argument, &opts.o_not_first, 1 },
235 { "open", required_argument, NULL, 'o' },
236 { "option", required_argument, NULL, 'O' },
237 { "pretty", no_argument, NULL, 'p' },
238 { "style", required_argument, NULL, 's' },
239 { "text", no_argument, NULL, 'T' },
240 { "xml", no_argument, NULL, 'X' },
241 { "xpath", no_argument, &opts.o_xpath, 1 },
242 { "version", no_argument, &opts.o_version, 1 },
243 { "warn", no_argument, NULL, 'W' },
244 { "warn-xml", no_argument, &opts.o_warn_xml, 1 },
245 { "wrap", required_argument, &opts.o_wrap, 1 },
250 main (int argc UNUSED, char **argv)
252 char *fmt = NULL, *cp, *np;
253 char *opt_opener = NULL, *opt_closer = NULL, *opt_wrapper = NULL;
254 char *opt_options = NULL;
256 int opt_not_first = 0;
259 argc = xo_parse_args(argc, argv);
263 while ((rc = getopt_long(argc, argv, "c:HJl:O:o:ps:TXW",
264 long_opts, NULL)) != -1) {
268 xo_set_flags(NULL, XOF_IGNORE_CLOSE);
272 xo_set_style(NULL, XO_STYLE_HTML);
276 xo_set_style(NULL, XO_STYLE_JSON);
280 xo_set_leading_xpath(NULL, optarg);
284 opt_options = optarg;
292 xo_set_flags(NULL, XOF_PRETTY);
296 if (xo_set_style_name(NULL, optarg) < 0)
297 xo_errx(1, "unknown style: %s", optarg);
301 xo_set_style(NULL, XO_STYLE_TEXT);
305 xo_set_style(NULL, XO_STYLE_XML);
310 xo_set_flags(NULL, XOF_WARN);
314 xo_errx(1, "missing argument");
319 opt_depth = atoi(optarg);
321 } else if (opts.o_help) {
325 } else if (opts.o_not_first) {
328 } else if (opts.o_xpath) {
329 xo_set_flags(NULL, XOF_XPATH);
331 } else if (opts.o_version) {
335 } else if (opts.o_warn_xml) {
337 xo_set_flags(NULL, XOF_WARN | XOF_WARN_XML);
339 } else if (opts.o_wrap) {
340 opt_wrapper = optarg;
347 bzero(&opts, sizeof(opts)); /* Reset all the options */
360 rc = xo_set_options(NULL, opt_options);
362 xo_errx(1, "invalid options: %s", opt_options);
365 xo_set_formatter(NULL, formatter, checkpoint);
366 xo_set_flags(NULL, XOF_NO_VA_ARG | XOF_NO_TOP | XOF_NO_CLOSE);
369 if (opt_opener == NULL && opt_closer == NULL && fmt == NULL) {
375 xo_set_flags(NULL, XOF_NOT_FIRST);
379 for (cp = opt_closer; cp && *cp; cp = np) {
380 np = strchr(cp, '/');
389 xo_set_depth(NULL, opt_depth);
392 for (cp = opt_opener; cp && *cp; cp = np) {
393 np = strchr(cp, '/');
396 xo_open_container(cp);
403 for (cp = opt_wrapper; cp && *cp; cp = np) {
404 np = strchr(cp, '/');
407 xo_open_container(cp);
419 while (opt_wrapper) {
420 np = strrchr(opt_wrapper, '/');
421 xo_close_container(np ? np + 1 : opt_wrapper);
429 np = strrchr(opt_closer, '/');
430 xo_close_container(np ? np + 1 : opt_closer);