/*=export_func optionResetOpt * private: * * what: Reset the value of an option * arg: + tOptions* + pOpts + program options descriptor + * arg: + tOptDesc* + pOptDesc + the descriptor for this arg + * * doc: * This code will cause another option to be reset to its initial state. * For example, --reset=foo will cause the --foo option to be reset. =*/ void optionResetOpt( tOptions* pOpts, tOptDesc* pOD ) { static ag_bool reset_active = AG_FALSE; tOptState opt_state = OPTSTATE_INITIALIZER(DEFINED); char const * pzArg = pOD->optArg.argString; tSuccess succ; if (reset_active) return; if ( (! HAS_originalOptArgArray(pOpts)) || (pOpts->originalOptArgCookie == NULL)) { fputs(zResetNotConfig, stderr); _exit(EX_SOFTWARE); } if ((pzArg == NULL) || (*pzArg == NUL)) { fputs(zNoResetArg, stderr); pOpts->pUsageProc(pOpts, EXIT_FAILURE); /* NOTREACHED */ assert(0 == 1); } reset_active = AG_TRUE; if (pzArg[1] == NUL) { if (*pzArg == '*') { optionResetEverything(pOpts); reset_active = AG_FALSE; return; } succ = shortOptionFind(pOpts, (tAoUC)*pzArg, &opt_state); if (! SUCCESSFUL(succ)) { fprintf(stderr, zIllOptChr, pOpts->pzProgPath, *pzArg); pOpts->pUsageProc(pOpts, EXIT_FAILURE); /* NOTREACHED */ assert(0 == 1); } } else { succ = longOptionFind(pOpts, (char *)pzArg, &opt_state); if (! SUCCESSFUL(succ)) { fprintf(stderr, zIllOptStr, pOpts->pzProgPath, pzArg); pOpts->pUsageProc(pOpts, EXIT_FAILURE); /* NOTREACHED */ assert(0 == 1); } } /* * We've found the indicated option. Turn off all non-persistent * flags because we're forcing the option back to its initialized state. * Call any callout procedure to handle whatever it needs to. * Finally, clear the reset flag, too. */ optionReset(pOpts, opt_state.pOD); reset_active = AG_FALSE; }
/* * Process a string of short options glued together. If the last one * does or may take an argument, the do the argument processing and leave. */ static tSuccess checkShortOpts( tOptions* pOpts, char* pzArg, tOptState* pOS, char** ppzOpts, int* pOptsIdx ) { while (*pzArg != NUL) { if (FAILED( shortOptionFind( pOpts, (tAoUC)*pzArg, pOS ))) return FAILURE; /* * See if we can have an arg. */ if (OPTST_GET_ARGTYPE(pOS->pOD->fOptState) == OPARG_TYPE_NONE) { pzArg++; } else if (pOS->pOD->fOptState & OPTST_ARG_OPTIONAL) { /* * Take an argument if it is not attached and it does not * start with a hyphen. */ if (pzArg[1] != NUL) return SUCCESS; pzArg = pOpts->origArgVect[ pOpts->curOptIdx ]; if (*pzArg != '-') ppzOpts[ (*pOptsIdx)++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ]; return SUCCESS; } else { /* * IF we need another argument, be sure it is there and * take it. */ if (pzArg[1] == NUL) { if (pOpts->curOptIdx >= pOpts->origArgCt) return FAILURE; ppzOpts[ (*pOptsIdx)++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ]; } return SUCCESS; } } return SUCCESS; }
/* * findOptDesc * * Find the option descriptor for the current option */ static tSuccess findOptDesc(tOptions* pOpts, tOptState* pOptState) { /* * IF we are continuing a short option list (e.g. -xyz...) * THEN continue a single flag option. * OTHERWISE see if there is room to advance and then do so. */ if ((pOpts->pzCurOpt != NULL) && (*pOpts->pzCurOpt != NUL)) return shortOptionFind(pOpts, (tAoUC)*(pOpts->pzCurOpt), pOptState); if (pOpts->curOptIdx >= pOpts->origArgCt) return PROBLEM; /* NORMAL COMPLETION */ pOpts->pzCurOpt = pOpts->origArgVect[ pOpts->curOptIdx ]; /* * IF all arguments must be named options, ... */ if (NAMED_OPTS(pOpts)) { char * pz = pOpts->pzCurOpt; int def; tSuccess res; tAoUS * def_opt; pOpts->curOptIdx++; if (*pz != '-') return longOptionFind(pOpts, pz, pOptState); /* * The name is prefixed with one or more hyphens. Strip them off * and disable the "default_opt" setting. Use heavy recasting to * strip off the "const" quality of the "default_opt" field. */ while (*(++pz) == '-') ; def_opt = (void *)&(pOpts->specOptIdx.default_opt); def = *def_opt; *def_opt = NO_EQUIVALENT; res = longOptionFind(pOpts, pz, pOptState); *def_opt = def; return res; } /* * Note the kind of flag/option marker */ if (*((pOpts->pzCurOpt)++) != '-') return PROBLEM; /* NORMAL COMPLETION - this + rest are operands */ /* * Special hack for a hyphen by itself */ if (*(pOpts->pzCurOpt) == NUL) return PROBLEM; /* NORMAL COMPLETION - this + rest are operands */ /* * The current argument is to be processed as an option argument */ pOpts->curOptIdx++; /* * We have an option marker. * Test the next character for long option indication */ if (pOpts->pzCurOpt[0] == '-') { if (*++(pOpts->pzCurOpt) == NUL) /* * NORMAL COMPLETION - NOT this arg, but rest are operands */ return PROBLEM; /* * We do not allow the hyphen to be used as a flag value. * Therefore, if long options are not to be accepted, we punt. */ if ((pOpts->fOptSet & OPTPROC_LONGOPT) == 0) { fprintf(stderr, zIllOptStr, pOpts->pzProgPath, zIllegal, pOpts->pzCurOpt-2); return FAILURE; } return longOptionFind(pOpts, pOpts->pzCurOpt, pOptState); } /* * If short options are not allowed, then do long * option processing. Otherwise the character must be a * short (i.e. single character) option. */ if ((pOpts->fOptSet & OPTPROC_SHORTOPT) != 0) return shortOptionFind(pOpts, (tAoUC)*(pOpts->pzCurOpt), pOptState); return longOptionFind(pOpts, pOpts->pzCurOpt, pOptState); }
/* * If the program wants sorted options (separated operands and options), * then this routine will to the trick. */ LOCAL void optionSort( tOptions* pOpts ) { char** ppzOpts; char** ppzOpds; int optsIdx = 0; int opdsIdx = 0; tOptState os = OPTSTATE_INITIALIZER(DEFINED); /* * Disable for POSIX conformance, or if there are no operands. */ if ( (getenv( "POSIXLY_CORRECT" ) != NULL) || NAMED_OPTS(pOpts)) return; /* * Make sure we can allocate two full-sized arg vectors. */ ppzOpts = malloc( pOpts->origArgCt * sizeof( char* )); if (ppzOpts == NULL) goto exit_no_mem; ppzOpds = malloc( pOpts->origArgCt * sizeof( char* )); if (ppzOpds == NULL) { free( ppzOpts ); goto exit_no_mem; } pOpts->curOptIdx = 1; pOpts->pzCurOpt = NULL; /* * Now, process all the options from our current position onward. * (This allows interspersed options and arguments for the few * non-standard programs that require it.) */ for (;;) { char* pzArg; tSuccess res; /* * If we're out of arguments, we're done. Join the option and * operand lists into the original argument vector. */ if (pOpts->curOptIdx >= pOpts->origArgCt) { errno = 0; goto joinLists; } pzArg = pOpts->origArgVect[ pOpts->curOptIdx ]; if (*pzArg != '-') { ppzOpds[ opdsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ]; continue; } switch (pzArg[1]) { case NUL: /* * A single hyphen is an operand. */ ppzOpds[ opdsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ]; continue; case '-': /* * Two consecutive hypens. Put them on the options list and then * _always_ force the remainder of the arguments to be operands. */ if (pzArg[2] == NUL) { ppzOpts[ optsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ]; goto restOperands; } res = longOptionFind( pOpts, pzArg+2, &os ); break; default: /* * If short options are not allowed, then do long * option processing. Otherwise the character must be a * short (i.e. single character) option. */ if ((pOpts->fOptSet & OPTPROC_SHORTOPT) == 0) { res = longOptionFind( pOpts, pzArg+1, &os ); } else { res = shortOptionFind( pOpts, (tAoUC)pzArg[1], &os ); } break; } if (FAILED( res )) { errno = EINVAL; goto freeTemps; } /* * We've found an option. Add the argument to the option list. * Next, we have to see if we need to pull another argument to be * used as the option argument. */ ppzOpts[ optsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ]; if (OPTST_GET_ARGTYPE(os.pOD->fOptState) == OPARG_TYPE_NONE) { /* * No option argument. If we have a short option here, * then scan for short options until we get to the end * of the argument string. */ if ( (os.optType == TOPT_SHORT) && FAILED( checkShortOpts( pOpts, pzArg+2, &os, ppzOpts, &optsIdx )) ) { errno = EINVAL; goto freeTemps; } } else if (os.pOD->fOptState & OPTST_ARG_OPTIONAL) { switch (mayHandleArg( pOpts, pzArg+2, &os, ppzOpts, &optsIdx )) { case FAILURE: errno = EIO; goto freeTemps; case PROBLEM: errno = 0; goto joinLists; } } else { switch (mustHandleArg( pOpts, pzArg+2, &os, ppzOpts, &optsIdx )) { case PROBLEM: case FAILURE: errno = EIO; goto freeTemps; } } } /* for (;;) */ restOperands: while (pOpts->curOptIdx < pOpts->origArgCt) ppzOpds[ opdsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ]; joinLists: if (optsIdx > 0) memcpy( pOpts->origArgVect + 1, ppzOpts, optsIdx * sizeof( char* )); if (opdsIdx > 0) memcpy( pOpts->origArgVect + 1 + optsIdx, ppzOpds, opdsIdx * sizeof( char* )); freeTemps: free( ppzOpts ); free( ppzOpds ); return; exit_no_mem: errno = ENOMEM; return; }