/* doc2html - demonstrate getopt() usage * * This application shows you one way of using getopt() to * process your command-line options and store them in a * global structure for easy access. * * See: http://www.ibm.com/developerworks/aix/library/au-unix-getopt.html * * Modifications made by Bill Slough * -- minor reformatting of code * -- added output routine to display the argument vector * -- added output routine to display the global arguments * -- changed display_usage: output to standard error file * -- made optString a local variable * -- minor change to the globalArgs_t structure * * * The following command-line arguments are supported: * * -I - don't produce a keyword index * -l lang - produce output in the specified language, lang * -o outfile - write output to outfile instead of stdout * -v - be verbose; more -v means more diagnostics * additional file names are used as input files * * The optString variable, declared in main(), tells getopt() * which options are supported, and which options have arguments. */ #include #include #include struct globalArgs_t { int noIndex; /* -I option */ char *langCode; /* -l option */ char *outFileName; /* -o option */ FILE *outFile; int verbosity; /* -v option */ char **inputFiles; /* input files */ int numInputFiles; /* # of input files */ } globalArgs; /* Display program usage, and exit. */ void display_usage( void ) { fprintf( stderr, "doc2html - convert documents to HTML\n" ); /* ... */ exit( EXIT_FAILURE ); } /* Convert the input files to HTML, governed by globalArgs. */ void convert_document( void ) { /* ... function stub ... */ } /* Display the global arguments */ void display_args() { int i; printf("Global arguments:\n"); printf(" noIndex = %d\n", globalArgs.noIndex); printf(" langCode = %s\n", globalArgs.langCode); printf(" outFileName = %s\n", globalArgs.outFileName); printf(" verbosity = %d\n", globalArgs.verbosity); printf(" numInputFiles = %d\n", globalArgs.numInputFiles); for (i = 0; i < globalArgs.numInputFiles; i++) { printf(" inputFiles[%d] = %s\n", i, globalArgs.inputFiles[i]); } } /* Display the argument vector */ void display_arg_vector(char *argv[]) { int i; printf("Argument vector:\n"); i = 0; while (argv[i] != NULL) { printf(" argv[%d] = \"%s\"\n", i, argv[i]); i++; } } int main(int argc, char *argv[]) { int opt; /* getopt() result */ const char *optString = "Il:o:vh?"; /* supported command-line options */ /* Initialize globalArgs before we get to work. */ globalArgs.noIndex = 0; /* false */ globalArgs.langCode = NULL; globalArgs.outFileName = NULL; globalArgs.outFile = NULL; globalArgs.verbosity = 0; globalArgs.inputFiles = NULL; globalArgs.numInputFiles = 0; /* Process the arguments with getopt(), then * populate globalArgs. */ opt = getopt( argc, argv, optString ); /* get the first option */ while( opt != -1 ) { switch( opt ) { case 'I': globalArgs.noIndex = 1; /* true */ break; case 'l': globalArgs.langCode = optarg; break; case 'o': globalArgs.outFileName = optarg; break; case 'v': globalArgs.verbosity++; break; case 'h': /* fall-through is intentional */ case '?': display_usage(); break; default: /* You won't actually get here. */ break; } /* get the next option */ opt = getopt( argc, argv, optString ); } globalArgs.inputFiles = argv + optind; globalArgs.numInputFiles = argc - optind; display_arg_vector(argv); display_args(); convert_document(); return EXIT_SUCCESS; }