/* * Copyright (c) 2014, Juniper Networks, Inc. * All rights reserved. * This SOFTWARE is licensed under the LICENSE provided in the * ../Copyright file. By downloading, installing, copying, or otherwise * using the SOFTWARE, you agree to be bound by the terms of that * LICENSE. * Phil Shafer, July 2014 */ #include #include #include #include #include "xo_config.h" #include "xo.h" #include /* Include after xo.h for testing */ #ifndef UNUSED #define UNUSED __attribute__ ((__unused__)) #endif /* UNUSED */ static int opt_warn; /* Enable warnings */ static char **save_argv; static char **checkpoint_argv; static char * next_arg (void) { char *cp = *save_argv; if (cp == NULL) xo_errx(1, "missing argument"); save_argv += 1; return cp; } static void prep_arg (char *fmt) { char *cp, *fp; for (cp = fp = fmt; *cp; cp++, fp++) { if (*cp != '\\') { if (cp != fp) *fp = *cp; continue; } switch (*++cp) { case 'n': *fp = '\n'; break; case 'r': *fp = '\r'; break; case 'b': *fp = '\b'; break; case 'e': *fp = '\e'; break; default: *fp = *cp; } } *fp = '\0'; } static void checkpoint (xo_handle_t *xop UNUSED, va_list vap UNUSED, int restore) { if (restore) save_argv = checkpoint_argv; else checkpoint_argv = save_argv; } /* * Our custom formatter is responsible for combining format string pieces * with our command line arguments to build strings. This involves faking * some printf-style logic. */ static xo_ssize_t formatter (xo_handle_t *xop, char *buf, xo_ssize_t bufsiz, const char *fmt, va_list vap UNUSED) { int lflag UNUSED = 0; /* Parse long flag, though currently ignored */ int hflag = 0, jflag = 0, tflag = 0, zflag = 0, qflag = 0, star1 = 0, star2 = 0; int rc = 0; int w1 = 0, w2 = 0; const char *cp; for (cp = fmt + 1; *cp; cp++) { if (*cp == 'l') lflag += 1; else if (*cp == 'h') hflag += 1; else if (*cp == 'j') jflag += 1; else if (*cp == 't') tflag += 1; else if (*cp == 'z') zflag += 1; else if (*cp == 'q') qflag += 1; else if (*cp == '*') { if (star1 == 0) star1 = 1; else star2 = 1; } else if (strchr("diouxXDOUeEfFgGaAcCsSp", *cp) != NULL) break; else if (*cp == 'n' || *cp == 'v') { if (opt_warn) xo_error_h(xop, "unsupported format: '%s'", fmt); return -1; } } char fc = *cp; /* Handle "%*.*s" */ if (star1) w1 = strtol(next_arg(), NULL, 0); if (star2 > 1) w2 = strtol(next_arg(), NULL, 0); if (fc == 'D' || fc == 'O' || fc == 'U') lflag = 1; if (strchr("diD", fc) != NULL) { long long value = strtoll(next_arg(), NULL, 0); if (star1 && star2) rc = snprintf(buf, bufsiz, fmt, w1, w2, value); else if (star1) rc = snprintf(buf, bufsiz, fmt, w1, value); else rc = snprintf(buf, bufsiz, fmt, value); } else if (strchr("ouxXOUp", fc) != NULL) { unsigned long long value = strtoull(next_arg(), NULL, 0); if (star1 && star2) rc = snprintf(buf, bufsiz, fmt, w1, w2, value); else if (star1) rc = snprintf(buf, bufsiz, fmt, w1, value); else rc = snprintf(buf, bufsiz, fmt, value); } else if (strchr("eEfFgGaA", fc) != NULL) { double value = strtold(next_arg(), NULL); if (star1 && star2) rc = snprintf(buf, bufsiz, fmt, w1, w2, value); else if (star1) rc = snprintf(buf, bufsiz, fmt, w1, value); else rc = snprintf(buf, bufsiz, fmt, value); } else if (fc == 'C' || fc == 'c' || fc == 'S' || fc == 's') { char *value = next_arg(); if (star1 && star2) rc = snprintf(buf, bufsiz, fmt, w1, w2, value); else if (star1) rc = snprintf(buf, bufsiz, fmt, w1, value); else rc = snprintf(buf, bufsiz, fmt, value); } return rc; } static void print_version (void) { fprintf(stderr, "libxo version %s%s\n", xo_version, xo_version_extra); fprintf(stderr, "xo version %s%s\n", LIBXO_VERSION, LIBXO_VERSION_EXTRA); } static void print_help (void) { fprintf(stderr, "Usage: xo [options] format [fields]\n" " --close Close tags for the given path\n" " --depth Set the depth for pretty printing\n" " --help Display this help text\n" " --html OR -H Generate HTML output\n" " --json OR -J Generate JSON output\n" " --leading-xpath OR -l " "Add a prefix to generated XPaths (HTML)\n" " --open Open tags for the given path\n" " --option -or -O Give formatting options\n" " --pretty OR -p Make 'pretty' output (add indent, newlines)\n" " --style