comparison sidinfo.c @ 29:d4ec69beb160

Add one line output format.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 25 Sep 2014 17:41:24 +0300
parents 6240788fe172
children 727c012d5de4
comparison
equal deleted inserted replaced
28:6240788fe172 29:d4ec69beb160
73 73
74 74
75 // Option variables 75 // Option variables
76 BOOL optParsable = FALSE, 76 BOOL optParsable = FALSE,
77 optNoNamePrefix = FALSE, 77 optNoNamePrefix = FALSE,
78 optHexadecimal = FALSE; 78 optHexadecimal = FALSE,
79 optOneLine = FALSE;
79 uint32_t optFields = PSF_ALL; 80 uint32_t optFields = PSF_ALL;
80 int optNFiles = 0; 81 int optNFiles = 0;
81 82
82 83
83 // Define option arguments 84 // Define option arguments
85 { 86 {
86 { 0, '?', "help", "Show this help", OPT_NONE }, 87 { 0, '?', "help", "Show this help", OPT_NONE },
87 // { 1, 'v', "verbose", "Be more verbose", OPT_NONE }, 88 // { 1, 'v', "verbose", "Be more verbose", OPT_NONE },
88 { 2, 'p', "parsable", "Output in script-parsable format", OPT_NONE }, 89 { 2, 'p', "parsable", "Output in script-parsable format", OPT_NONE },
89 { 5, 'n', "noprefix", "Output without field name prefix", OPT_NONE }, 90 { 5, 'n', "noprefix", "Output without field name prefix", OPT_NONE },
91 { 6, 'l', "oneline", "Output in one line format", OPT_NONE },
90 { 3, 'f', "fields", "Show only specified field(s)", OPT_ARGREQ }, 92 { 3, 'f', "fields", "Show only specified field(s)", OPT_ARGREQ },
91 { 4, 'x', "hex", "Use hexadecimal values", OPT_NONE }, 93 { 4, 'x', "hex", "Use hexadecimal values", OPT_NONE },
92 }; 94 };
93 95
94 static const int optListN = (sizeof(optList) / sizeof(optList[0])); 96 static const int optListN = (sizeof(optList) / sizeof(optList[0]));
211 optHexadecimal = TRUE; 213 optHexadecimal = TRUE;
212 break; 214 break;
213 215
214 case 5: 216 case 5:
215 optNoNamePrefix = TRUE; 217 optNoNamePrefix = TRUE;
218 break;
219
220 case 6:
221 optOneLine = TRUE;
216 break; 222 break;
217 223
218 default: 224 default:
219 THERR("Unknown option '%s'.\n", currArg); 225 THERR("Unknown option '%s'.\n", currArg);
220 return FALSE; 226 return FALSE;
401 th_free(fileData); 407 th_free(fileData);
402 return ret; 408 return ret;
403 } 409 }
404 410
405 411
406 static void siPrintLinePrefix(FILE *outFile, const BOOL parsable, const char *name) 412 static void siPrintLinePrefix(FILE *outFile, const char *name)
407 { 413 {
408 if (!optNoNamePrefix) 414 if (!optNoNamePrefix)
409 fprintf(outFile, parsable ? "%s=" : "%-20s : ", name); 415 fprintf(outFile, optParsable ? "%s=" : "%-20s : ", name);
410 } 416 }
411 417
412 418
413 static void siPrintPSIDInfoLine(FILE *outFile, const BOOL parsable, const BOOL hex, 419 static void siPrintPSIDInfoLine(FILE *outFile, BOOL *shown, const int xindex, const char *xfmt, const char *xaltfmt, ...)
414 const uint32_t flags, const int xindex, const char *xfmt, const char *xaltfmt, ...)
415 { 420 {
416 const PSFOption *opt = &optPSFlags[xindex]; 421 const PSFOption *opt = &optPSFlags[xindex];
417 if (flags & opt->flag) 422 if (optFields & opt->flag)
418 { 423 {
419 va_list ap; 424 va_list ap;
420 const char *fmt = hex ? (xaltfmt != NULL ? xaltfmt : xfmt) : xfmt; 425 const char *fmt = optHexadecimal ? (xaltfmt != NULL ? xaltfmt : xfmt) : xfmt;
421 426
422 siPrintLinePrefix(outFile, parsable, (parsable || opt->lname == NULL) ? opt->name : opt->lname); 427 siPrintLinePrefix(outFile, (optParsable || opt->lname == NULL) ? opt->name : opt->lname);
423 428
424 va_start(ap, xaltfmt); 429 va_start(ap, xaltfmt);
425 430
426 if (parsable) 431 if (optParsable)
427 vfprintf(outFile, fmt, ap); 432 vfprintf(outFile, fmt, ap);
428 else 433 else
429 vfprintf(outFile, fmt, ap); 434 vfprintf(outFile, fmt, ap);
430 435
431 va_end(ap); 436 va_end(ap);
432 437
433 fprintf(outFile, "\n"); 438 fprintf(outFile, optOneLine ? "|" : "\n");
434 } 439 *shown = TRUE;
435 } 440 }
436 441 }
437 #define PR(xindex, xfmt, xaltfmt, ...) siPrintPSIDInfoLine(outFile, parsable, hex, flags, xindex, xfmt, xaltfmt, __VA_ARGS__ ) 442
438 443 #define PR(xindex, xfmt, xaltfmt, ...) siPrintPSIDInfoLine(outFile, &shown, xindex, xfmt, xaltfmt, __VA_ARGS__ )
439 444
440 void siPrintPSIDInformation(FILE *outFile, const BOOL parsable, const BOOL hex, 445
441 const uint32_t flags, const char *filename, const PSIDHeader *psid) 446 void siPrintPSIDInformation(FILE *outFile, const char *filename, const PSIDHeader *psid)
442 { 447 {
448 BOOL shown = FALSE;
449
443 PR(13, "%s", NULL, filename); 450 PR(13, "%s", NULL, filename);
444 451
445 PR( 0, "%s", NULL, psid->magic); 452 PR( 0, "%s", NULL, psid->magic);
446 PR( 1, "%d.%d", NULL, (psid->version & 0xff), (psid->version >> 8)); 453 PR( 1, "%d.%d", NULL, (psid->version & 0xff), (psid->version >> 8));
447 PR( 2, "%d", "$%08x", psid->dataOffset); 454 PR( 2, "%d", "$%08x", psid->dataOffset);
454 461
455 PR( 9, "%s", NULL, psid->sidName); 462 PR( 9, "%s", NULL, psid->sidName);
456 PR(10, "%s", NULL, psid->sidAuthor); 463 PR(10, "%s", NULL, psid->sidAuthor);
457 PR(11, "%s", NULL, psid->sidCopyright); 464 PR(11, "%s", NULL, psid->sidCopyright);
458 465
459 if (flags & PSF_HASH) 466 if (optFields & PSF_HASH)
460 { 467 {
461 siPrintLinePrefix(outFile, parsable, "Hash"); 468 siPrintLinePrefix(outFile, "Hash");
462 th_md5_print(outFile, psid->hash); 469 th_md5_print(outFile, psid->hash);
470 fprintf(outFile, optOneLine ? "|" : "\n");
471 }
472
473 if (shown)
463 fprintf(outFile, "\n"); 474 fprintf(outFile, "\n");
464 }
465 } 475 }
466 476
467 477
468 BOOL argHandleFile(char *filename) 478 BOOL argHandleFile(char *filename)
469 { 479 {
483 THERR("Error reading %s\n", filename); 493 THERR("Error reading %s\n", filename);
484 goto error; 494 goto error;
485 } 495 }
486 496
487 // Output 497 // Output
488 siPrintPSIDInformation(stdout, optParsable, optHexadecimal, optFields, filename, &psid); 498 siPrintPSIDInformation(stdout, filename, &psid);
489 499
490 // Shutdown 500 // Shutdown
491 error: 501 error:
492 if (inFile != NULL) 502 if (inFile != NULL)
493 fclose(inFile); 503 fclose(inFile);
505 // Parse command line arguments 515 // Parse command line arguments
506 if (!th_args_process(argc, argv, optList, optListN, 516 if (!th_args_process(argc, argv, optList, optListN,
507 argHandleOpt, argHandleFile, OPTH_ONLY_OPTS)) 517 argHandleOpt, argHandleFile, OPTH_ONLY_OPTS))
508 return -1; 518 return -1;
509 519
520 if (optOneLine)
521 {
522 optParsable = FALSE;
523 optNoNamePrefix = TRUE;
524 }
525
510 // Process files 526 // Process files
511 if (!th_args_process(argc, argv, optList, optListN, 527 if (!th_args_process(argc, argv, optList, optListN,
512 argHandleOpt, argHandleFile, OPTH_ONLY_OTHER)) 528 argHandleOpt, argHandleFile, OPTH_ONLY_OTHER))
513 return -2; 529 return -2;
514 530