From: Mike Frysinger Date: Thu, 24 Dec 2015 04:38:24 +0000 (-0500) Subject: standardize usage behavior X-Git-Url: https://git.wh0rd.org/?a=commitdiff_plain;h=1ad3ad1717ffd0a71afb3d4f97c458ef3fe9b576;p=elf2flt.git standardize usage behavior Make sure the tools respect the -h (help) option, and exit 0 & use stdout when called that way. For all other cases, exit 1 & use stderr. --- diff --git a/elf2flt.c b/elf2flt.c index 74e0f21..60f5207 100644 --- a/elf2flt.c +++ b/elf2flt.c @@ -1582,25 +1582,26 @@ printf("%s(%d): symbol name=%s address=0x%x section=%s -> RELOC=0x%x\n", -static void usage(void) +static void usage(int status) { - fprintf(stderr, "Usage: %s [vrzd] [-p ] [-s stack-size] " - "[-o ] \n\n" - " -v : verbose operation\n" - " -r : force load to RAM\n" - " -k : enable kernel trace on load (for debug)\n" - " -z : compress code/data/relocs\n" - " -d : compress data/relocs\n" - " -a : use existing symbol references\n" - " instead of recalculating from\n" - " relocation info\n" - " -R reloc-file : read relocations from a separate file\n" - " -p abs-pic-file : GOT/PIC processing with files\n" - " -s stacksize : set application stack size\n" - " -o output-file : output file name\n\n", - elf2flt_progname); - fprintf(stderr, "Compiled for " ARCH " architecture\n\n"); - exit(2); + fprintf(status ? stderr : stdout, + "Usage: %s [vrzd] [-p ] [-s stack-size] " + "[-o ] \n\n" + " -v : verbose operation\n" + " -r : force load to RAM\n" + " -k : enable kernel trace on load (for debug)\n" + " -z : compress code/data/relocs\n" + " -d : compress data/relocs\n" + " -a : use existing symbol references\n" + " instead of recalculating from\n" + " relocation info\n" + " -R reloc-file : read relocations from a separate file\n" + " -p abs-pic-file : GOT/PIC processing with files\n" + " -s stacksize : set application stack size\n" + " -o output-file : output file name\n\n" + "Compiled for " ARCH " architecture\n\n", + elf2flt_progname); + exit(status); } @@ -1658,7 +1659,7 @@ int main(int argc, char *argv[]) xmalloc_set_program_name(elf2flt_progname); if (argc < 2) - usage(); + usage(1); if (sizeof(hdr) != 64) fatal( @@ -1672,7 +1673,7 @@ int main(int argc, char *argv[]) stack = 0x2020; #endif - while ((opt = getopt(argc, argv, "avzdrkp:s:o:R:")) != -1) { + while ((opt = getopt(argc, argv, "havzdrkp:s:o:R:")) != -1) { switch (opt) { case 'v': verbose++; @@ -1701,15 +1702,18 @@ int main(int argc, char *argv[]) case 's': if (sscanf(optarg, "%i", &stack) != 1) { fprintf(stderr, "%s invalid stack size %s\n", argv[0], optarg); - usage(); + usage(1); } break; case 'R': rel_file = optarg; break; + case 'h': + usage(0); + break; default: fprintf(stderr, "%s Unknown option\n", argv[0]); - usage(); + usage(1); break; } } diff --git a/flthdr.c b/flthdr.c index 645585b..8a8b97c 100644 --- a/flthdr.c +++ b/flthdr.c @@ -300,25 +300,28 @@ process_file(const char *ifile, const char *ofile) void usage(const char *s) { + FILE *out = s ? stderr : stdout; if (s) - fprintf(stderr, "%s\n", s); - fprintf(stderr, "usage: %s [options] flat-file\n", elf2flt_progname); - fprintf(stderr, " Allows you to change an existing flat file\n\n"); - fprintf(stderr, " -p : print current settings\n"); - fprintf(stderr, " -P : print relocations\n"); - fprintf(stderr, " -z : compressed flat file\n"); - fprintf(stderr, " -d : compressed data-only flat file\n"); - fprintf(stderr, " -Z : un-compressed flat file\n"); - fprintf(stderr, " -r : ram load\n"); - fprintf(stderr, " -R : do not RAM load\n"); - fprintf(stderr, " -k : kernel traced load (for debug)\n"); - fprintf(stderr, " -K : normal non-kernel traced load\n"); - fprintf(stderr, " -u : place stack in L1 scratchpad memory\n"); - fprintf(stderr, " -U : place stack in normal SDRAM memory\n"); - fprintf(stderr, " -s size : stack size\n"); - fprintf(stderr, " -o file : output-file\n" - " (default is to modify input file)\n"); - exit(1); + fprintf(out, "%s\n", s); + fprintf(out, + "Usage: %s [options] flat-file\n" + " Allows you to change an existing flat file\n\n" + " -p : print current settings\n" + " -P : print relocations\n" + " -z : compressed flat file\n" + " -d : compressed data-only flat file\n" + " -Z : un-compressed flat file\n" + " -r : ram load\n" + " -R : do not RAM load\n" + " -k : kernel traced load (for debug)\n" + " -K : normal non-kernel traced load\n" + " -u : place stack in L1 scratchpad memory\n" + " -U : place stack in normal SDRAM memory\n" + " -s size : stack size\n" + " -o file : output-file\n" + " (default is to modify input file)\n", + elf2flt_progname); + exit(s ? 1 : 0); } /****************************************************************************/ @@ -332,7 +335,7 @@ main(int argc, char *argv[]) elf2flt_progname = argv[0]; noargs = 1; - while ((c = getopt(argc, argv, "pPdzZrRuUkKs:o:")) != EOF) { + while ((c = getopt(argc, argv, "hpPdzZrRuUkKs:o:")) != EOF) { switch (c) { case 'p': print = 1; break; case 'P': print_relocs = 1; break; @@ -350,6 +353,9 @@ main(int argc, char *argv[]) if (sscanf(optarg, "%i", &stacksize) != 1) usage("invalid stack size"); break; + case 'h': + usage(NULL); + break; default: usage("invalid option"); break;