Formatting tweaks

This commit is contained in:
Steve Underwood 2013-08-19 23:00:56 +08:00
parent d434bfa86f
commit d79d28fb4c
1 changed files with 176 additions and 176 deletions

View File

@ -1,177 +1,177 @@
/***************************************************************************** /*****************************************************************************
* *
* MODULE NAME : GETOPT.C * MODULE NAME : GETOPT.C
* *
* COPYRIGHTS: * COPYRIGHTS:
* This module contains code made available by IBM * This module contains code made available by IBM
* Corporation on an AS IS basis. Any one receiving the * Corporation on an AS IS basis. Any one receiving the
* module is considered to be licensed under IBM copyrights * module is considered to be licensed under IBM copyrights
* to use the IBM-provided source code in any way he or she * to use the IBM-provided source code in any way he or she
* deems fit, including copying it, compiling it, modifying * deems fit, including copying it, compiling it, modifying
* it, and redistributing it, with or without * it, and redistributing it, with or without
* modifications. No license under any IBM patents or * modifications. No license under any IBM patents or
* patent applications is to be implied from this copyright * patent applications is to be implied from this copyright
* license. * license.
* *
* A user of the module should understand that IBM cannot * A user of the module should understand that IBM cannot
* provide technical support for the module and will not be * provide technical support for the module and will not be
* responsible for any consequences of use of the program. * responsible for any consequences of use of the program.
* *
* Any notices, including this one, are not to be removed * Any notices, including this one, are not to be removed
* from the module without the prior written consent of * from the module without the prior written consent of
* IBM. * IBM.
* *
* AUTHOR: Original author: * AUTHOR: Original author:
* G. R. Blair (BOBBLAIR at AUSVM1) * G. R. Blair (BOBBLAIR at AUSVM1)
* Internet: bobblair@bobblair.austin.ibm.com * Internet: bobblair@bobblair.austin.ibm.com
* *
* Extensively revised by: * Extensively revised by:
* John Q. Walker II, Ph.D. (JOHHQ at RALVM6) * John Q. Walker II, Ph.D. (JOHHQ at RALVM6)
* Internet: johnq@ralvm6.vnet.ibm.com * Internet: johnq@ralvm6.vnet.ibm.com
* *
*****************************************************************************/ *****************************************************************************/
/****************************************************************************** /******************************************************************************
* getopt() * getopt()
* *
* The getopt() function is a command line parser. It returns the next * The getopt() function is a command line parser. It returns the next
* option character in argv that matches an option character in opstring. * option character in argv that matches an option character in opstring.
* *
* The argv argument points to an array of argc+1 elements containing argc * The argv argument points to an array of argc+1 elements containing argc
* pointers to character strings followed by a null pointer. * pointers to character strings followed by a null pointer.
* *
* The opstring argument points to a string of option characters; if an * The opstring argument points to a string of option characters; if an
* option character is followed by a colon, the option is expected to have * option character is followed by a colon, the option is expected to have
* an argument that may or may not be separated from it by white space. * an argument that may or may not be separated from it by white space.
* The external variable optarg is set to point to the start of the option * The external variable optarg is set to point to the start of the option
* argument on return from getopt(). * argument on return from getopt().
* *
* The getopt() function places in optind the argv index of the next argument * The getopt() function places in optind the argv index of the next argument
* to be processed. The system initializes the external variable optind to * to be processed. The system initializes the external variable optind to
* 1 before the first call to getopt(). * 1 before the first call to getopt().
* *
* When all options have been processed (that is, up to the first nonoption * When all options have been processed (that is, up to the first nonoption
* argument), getopt() returns EOF. The special option "--" may be used to * argument), getopt() returns EOF. The special option "--" may be used to
* delimit the end of the options; EOF will be returned, and "--" will be * delimit the end of the options; EOF will be returned, and "--" will be
* skipped. * skipped.
* *
* The getopt() function returns a question mark (?) when it encounters an * The getopt() function returns a question mark (?) when it encounters an
* option character not included in opstring. This error message can be * option character not included in opstring. This error message can be
* disabled by setting opterr to zero. Otherwise, it returns the option * disabled by setting opterr to zero. Otherwise, it returns the option
* character that was detected. * character that was detected.
* *
* If the special option "--" is detected, or all options have been * If the special option "--" is detected, or all options have been
* processed, EOF is returned. * processed, EOF is returned.
* *
* Options are marked by either a minus sign (-) or a slash (/). * Options are marked by either a minus sign (-) or a slash (/).
* *
* No errors are defined. * No errors are defined.
*****************************************************************************/ *****************************************************************************/
#include <stdio.h> /* for EOF */ #include <stdio.h> /* for EOF */
#include <string.h> /* for strchr() */ #include <string.h> /* for strchr() */
/* static (global) variables that are specified as exported by getopt() */ /* static (global) variables that are specified as exported by getopt() */
char *optarg = NULL; /* pointer to the start of the option argument */ char *optarg = NULL; /* pointer to the start of the option argument */
int optind = 1; /* number of the next argv[] to be evaluated */ int optind = 1; /* number of the next argv[] to be evaluated */
int opterr = 1; /* non-zero if a question mark should be returned int opterr = 1; /* non-zero if a question mark should be returned
when a non-valid option character is detected */ when a non-valid option character is detected */
/* handle possible future character set concerns by putting this in a macro */ /* handle possible future character set concerns by putting this in a macro */
#define _next_char(string) (char)(*(string+1)) #define _next_char(string) (char)(*(string+1))
int getopt(int argc, char *argv[], char *opstring) int getopt(int argc, char *argv[], char *opstring)
{ {
static char *pIndexPosition = NULL; /* place inside current argv string */ static char *pIndexPosition = NULL; /* place inside current argv string */
char *pArgString = NULL; /* where to start from next */ char *pArgString = NULL; /* where to start from next */
char *pOptString; /* the string in our program */ char *pOptString; /* the string in our program */
if (pIndexPosition != NULL) { if (pIndexPosition != NULL) {
/* we last left off inside an argv string */ /* we last left off inside an argv string */
if (*(++pIndexPosition)) { if (*(++pIndexPosition)) {
/* there is more to come in the most recent argv */ /* there is more to come in the most recent argv */
pArgString = pIndexPosition; pArgString = pIndexPosition;
} }
} }
if (pArgString == NULL) { if (pArgString == NULL) {
/* we didn't leave off in the middle of an argv string */ /* we didn't leave off in the middle of an argv string */
if (optind >= argc) { if (optind >= argc) {
/* more command-line arguments than the argument count */ /* more command-line arguments than the argument count */
pIndexPosition = NULL; /* not in the middle of anything */ pIndexPosition = NULL; /* not in the middle of anything */
return EOF; /* used up all command-line arguments */ return EOF; /* used up all command-line arguments */
} }
/*--------------------------------------------------------------------- /*---------------------------------------------------------------------
* If the next argv[] is not an option, there can be no more options. * If the next argv[] is not an option, there can be no more options.
*-------------------------------------------------------------------*/ *-------------------------------------------------------------------*/
pArgString = argv[optind++]; /* set this to the next argument ptr */ pArgString = argv[optind++]; /* set this to the next argument ptr */
if (('/' != *pArgString) && /* doesn't start with a slash or a dash? */ if (('/' != *pArgString) && /* doesn't start with a slash or a dash? */
('-' != *pArgString)) { ('-' != *pArgString)) {
--optind; /* point to current arg once we're done */ --optind; /* point to current arg once we're done */
optarg = NULL; /* no argument follows the option */ optarg = NULL; /* no argument follows the option */
pIndexPosition = NULL; /* not in the middle of anything */ pIndexPosition = NULL; /* not in the middle of anything */
return EOF; /* used up all the command-line flags */ return EOF; /* used up all the command-line flags */
} }
/* check for special end-of-flags markers */ /* check for special end-of-flags markers */
if ((strcmp(pArgString, "-") == 0) || if ((strcmp(pArgString, "-") == 0) ||
(strcmp(pArgString, "--") == 0)) { (strcmp(pArgString, "--") == 0)) {
optarg = NULL; /* no argument follows the option */ optarg = NULL; /* no argument follows the option */
pIndexPosition = NULL; /* not in the middle of anything */ pIndexPosition = NULL; /* not in the middle of anything */
return EOF; /* encountered the special flag */ return EOF; /* encountered the special flag */
} }
pArgString++; /* look past the / or - */ pArgString++; /* look past the / or - */
} }
if (':' == *pArgString) { /* is it a colon? */ if (':' == *pArgString) { /* is it a colon? */
/*--------------------------------------------------------------------- /*---------------------------------------------------------------------
* Rare case: if opterr is non-zero, return a question mark; * Rare case: if opterr is non-zero, return a question mark;
* otherwise, just return the colon we're on. * otherwise, just return the colon we're on.
*-------------------------------------------------------------------*/ *-------------------------------------------------------------------*/
return (opterr ? (int)'?' : (int)':'); return (opterr ? (int)'?' : (int)':');
} }
else if ((pOptString = strchr(opstring, *pArgString)) == 0) { else if ((pOptString = strchr(opstring, *pArgString)) == 0) {
/*--------------------------------------------------------------------- /*---------------------------------------------------------------------
* The letter on the command-line wasn't any good. * The letter on the command-line wasn't any good.
*-------------------------------------------------------------------*/ *-------------------------------------------------------------------*/
optarg = NULL; /* no argument follows the option */ optarg = NULL; /* no argument follows the option */
pIndexPosition = NULL; /* not in the middle of anything */ pIndexPosition = NULL; /* not in the middle of anything */
return (opterr ? (int)'?' : (int)*pArgString); return (opterr ? (int)'?' : (int)*pArgString);
} }
else { else {
/*--------------------------------------------------------------------- /*---------------------------------------------------------------------
* The letter on the command-line matches one we expect to see * The letter on the command-line matches one we expect to see
*-------------------------------------------------------------------*/ *-------------------------------------------------------------------*/
if (':' == _next_char(pOptString)) { /* is the next letter a colon? */ if (':' == _next_char(pOptString)) { /* is the next letter a colon? */
/* It is a colon. Look for an argument string. */ /* It is a colon. Look for an argument string. */
if ('\0' != _next_char(pArgString)) { /* argument in this argv? */ if ('\0' != _next_char(pArgString)) { /* argument in this argv? */
optarg = &pArgString[1]; /* Yes, it is */ optarg = &pArgString[1]; /* Yes, it is */
} }
else { else {
/*------------------------------------------------------------- /*-------------------------------------------------------------
* The argument string must be in the next argv. * The argument string must be in the next argv.
* But, what if there is none (bad input from the user)? * But, what if there is none (bad input from the user)?
* In that case, return the letter, and optarg as NULL. * In that case, return the letter, and optarg as NULL.
*-----------------------------------------------------------*/ *-----------------------------------------------------------*/
if (optind < argc) if (optind < argc)
optarg = argv[optind++]; optarg = argv[optind++];
else { else {
optarg = NULL; optarg = NULL;
return (opterr ? (int)'?' : (int)*pArgString); return (opterr ? (int)'?' : (int)*pArgString);
} }
} }
pIndexPosition = NULL; /* not in the middle of anything */ pIndexPosition = NULL; /* not in the middle of anything */
} }
else { else {
/* it's not a colon, so just return the letter */ /* it's not a colon, so just return the letter */
optarg = NULL; /* no argument follows the option */ optarg = NULL; /* no argument follows the option */
pIndexPosition = pArgString; /* point to the letter we're on */ pIndexPosition = pArgString; /* point to the letter we're on */
} }
return (int)*pArgString; /* return the letter that matched */ return (int)*pArgString; /* return the letter that matched */
} }
} }