# HG changeset patch # User Matti Hamalainen # Date 1411656084 -10800 # Node ID d4ec69beb16050a4d5541d5597939347a493d8d3 # Parent 6240788fe172c500894949a97684da98cc4a72a4 Add one line output format. diff -r 6240788fe172 -r d4ec69beb160 sidinfo.c --- a/sidinfo.c Thu Sep 25 05:32:42 2014 +0300 +++ b/sidinfo.c Thu Sep 25 17:41:24 2014 +0300 @@ -75,7 +75,8 @@ // Option variables BOOL optParsable = FALSE, optNoNamePrefix = FALSE, - optHexadecimal = FALSE; + optHexadecimal = FALSE, + optOneLine = FALSE; uint32_t optFields = PSF_ALL; int optNFiles = 0; @@ -87,6 +88,7 @@ // { 1, 'v', "verbose", "Be more verbose", OPT_NONE }, { 2, 'p', "parsable", "Output in script-parsable format", OPT_NONE }, { 5, 'n', "noprefix", "Output without field name prefix", OPT_NONE }, + { 6, 'l', "oneline", "Output in one line format", OPT_NONE }, { 3, 'f', "fields", "Show only specified field(s)", OPT_ARGREQ }, { 4, 'x', "hex", "Use hexadecimal values", OPT_NONE }, }; @@ -215,6 +217,10 @@ optNoNamePrefix = TRUE; break; + case 6: + optOneLine = TRUE; + break; + default: THERR("Unknown option '%s'.\n", currArg); return FALSE; @@ -403,43 +409,44 @@ } -static void siPrintLinePrefix(FILE *outFile, const BOOL parsable, const char *name) +static void siPrintLinePrefix(FILE *outFile, const char *name) { if (!optNoNamePrefix) - fprintf(outFile, parsable ? "%s=" : "%-20s : ", name); + fprintf(outFile, optParsable ? "%s=" : "%-20s : ", name); } -static void siPrintPSIDInfoLine(FILE *outFile, const BOOL parsable, const BOOL hex, - const uint32_t flags, const int xindex, const char *xfmt, const char *xaltfmt, ...) +static void siPrintPSIDInfoLine(FILE *outFile, BOOL *shown, const int xindex, const char *xfmt, const char *xaltfmt, ...) { const PSFOption *opt = &optPSFlags[xindex]; - if (flags & opt->flag) + if (optFields & opt->flag) { va_list ap; - const char *fmt = hex ? (xaltfmt != NULL ? xaltfmt : xfmt) : xfmt; + const char *fmt = optHexadecimal ? (xaltfmt != NULL ? xaltfmt : xfmt) : xfmt; - siPrintLinePrefix(outFile, parsable, (parsable || opt->lname == NULL) ? opt->name : opt->lname); + siPrintLinePrefix(outFile, (optParsable || opt->lname == NULL) ? opt->name : opt->lname); va_start(ap, xaltfmt); - if (parsable) + if (optParsable) vfprintf(outFile, fmt, ap); else vfprintf(outFile, fmt, ap); va_end(ap); - fprintf(outFile, "\n"); + fprintf(outFile, optOneLine ? "|" : "\n"); + *shown = TRUE; } } -#define PR(xindex, xfmt, xaltfmt, ...) siPrintPSIDInfoLine(outFile, parsable, hex, flags, xindex, xfmt, xaltfmt, __VA_ARGS__ ) +#define PR(xindex, xfmt, xaltfmt, ...) siPrintPSIDInfoLine(outFile, &shown, xindex, xfmt, xaltfmt, __VA_ARGS__ ) -void siPrintPSIDInformation(FILE *outFile, const BOOL parsable, const BOOL hex, - const uint32_t flags, const char *filename, const PSIDHeader *psid) +void siPrintPSIDInformation(FILE *outFile, const char *filename, const PSIDHeader *psid) { + BOOL shown = FALSE; + PR(13, "%s", NULL, filename); PR( 0, "%s", NULL, psid->magic); @@ -456,12 +463,15 @@ PR(10, "%s", NULL, psid->sidAuthor); PR(11, "%s", NULL, psid->sidCopyright); - if (flags & PSF_HASH) + if (optFields & PSF_HASH) { - siPrintLinePrefix(outFile, parsable, "Hash"); + siPrintLinePrefix(outFile, "Hash"); th_md5_print(outFile, psid->hash); + fprintf(outFile, optOneLine ? "|" : "\n"); + } + + if (shown) fprintf(outFile, "\n"); - } } @@ -485,7 +495,7 @@ } // Output - siPrintPSIDInformation(stdout, optParsable, optHexadecimal, optFields, filename, &psid); + siPrintPSIDInformation(stdout, filename, &psid); // Shutdown error: @@ -507,6 +517,12 @@ argHandleOpt, argHandleFile, OPTH_ONLY_OPTS)) return -1; + if (optOneLine) + { + optParsable = FALSE; + optNoNamePrefix = TRUE; + } + // Process files if (!th_args_process(argc, argv, optList, optListN, argHandleOpt, argHandleFile, OPTH_ONLY_OTHER))