From 134669088d5d4bb2d739add59f080bc2a8d1c450 Mon Sep 17 00:00:00 2001 From: khali Date: Thu, 10 Feb 2005 20:57:13 +0000 Subject: Copy command line-handling from dmidecode.c. --- vpddecode.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 79 insertions(+), 7 deletions(-) (limited to 'vpddecode.c') diff --git a/vpddecode.c b/vpddecode.c index 3994981..61e058a 100644 --- a/vpddecode.c +++ b/vpddecode.c @@ -1,7 +1,7 @@ /* * IBM Vital Product Data decoder * - * (C) 2003-2004 Jean Delvare + * (C) 2003-2005 Jean Delvare * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -36,12 +36,25 @@ #include #include #include +#include +#include #include "version.h" #include "config.h" #include "types.h" #include "util.h" +/* Options are global */ +struct opt +{ + const char* devmem; + unsigned int flags; +}; +static struct opt opt; + +#define FLAG_VERSION (1<<0) +#define FLAG_HELP (1<<1) + static const char *product_name(const char *id) { static const char *name[]={ @@ -221,25 +234,84 @@ static int decode(const u8 *p) return 1; } -int main(int argc, const char *argv[]) +/* Return -1 on error, 0 on success */ +static int parse_command_line(int argc, char * const argv[]) +{ + int option; + const char *optstring = "d:hV"; + struct option longopts[]={ + { "dev-mem", required_argument, NULL, 'd' }, + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'V' }, + { 0, 0, 0, 0 } + }; + + while((option=getopt_long(argc, argv, optstring, longopts, NULL))!=-1) + switch(option) + { + case 'd': + opt.devmem=optarg; + break; + case 'h': + opt.flags|=FLAG_HELP; + break; + case 'V': + opt.flags|=FLAG_VERSION; + break; + case ':': + case '?': + return -1; + } + + return 0; +} + +static void print_help(void) +{ + static const char *help= + "Usage: vpddecode [OPTIONS]\n" + "Options are:\n" + " -d, --dev-mem FILE Read memory from device FILE (default: " DEFAULT_MEM_DEV ")\n" + " -h, --help Display this help text and exit\n" + " -V, --version Display the version and exit\n"; + + printf("%s", help); +} + +int main(int argc, char * const argv[]) { u8 *buf; int found=0; off_t fp; - const char *devmem=DEFAULT_MEM_DEV; if(sizeof(u8)!=1) { - fprintf(stderr,"%s: compiler incompatibility\n", argv[0]); + fprintf(stderr, "%s: compiler incompatibility\n", argv[0]); exit(255); } - if(argc>=2) - devmem=argv[1]; + /* Set default option values */ + opt.devmem=DEFAULT_MEM_DEV; + opt.flags=0; + + if(parse_command_line(argc, argv)<0) + exit(2); + + if(opt.flags & FLAG_HELP) + { + print_help(); + return 0; + } + + if(opt.flags & FLAG_VERSION) + { + printf("%s\n", VERSION); + return 0; + } printf("# vpddecode %s\n", VERSION); - if((buf=mem_chunk(0xF0000, 0x10000, devmem))==NULL) + if((buf=mem_chunk(0xF0000, 0x10000, opt.devmem))==NULL) exit(1); for(fp=0; fp<=0xFFF0; fp+=16) -- cgit v1.2.1