/** * An option was not found. Check for default option and set it * if there is one. Otherwise, handle the error. * * @param opts option data * @param name name of option to look for * @param arg option argument * @param st state about current option * * @return success status */ static tSuccess opt_unknown(tOptions * opts, char const * name, char * arg, tOptState * st) { /* * IF there is no equal sign * *AND* we are using named arguments * *AND* there is a default named option, * THEN return that option. */ if ( (arg == NULL) && NAMED_OPTS(opts) && (opts->specOptIdx.default_opt != NO_EQUIVALENT)) { st->pOD = opts->pOptDesc + opts->specOptIdx.default_opt; st->pzOptArg = name; st->optType = TOPT_DEFAULT; return SUCCESS; } if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0) { fprintf(stderr, zIllOptStr, opts->pzProgPath, name); (*opts->pUsageProc)(opts, EXIT_FAILURE); /* NOTREACHED */ _exit(EXIT_FAILURE); /* to be certain */ } return FAILURE; }
/** * print out the options that match the given name. * * @param pOpts option data * @param opt_name name of option to look for */ static void opt_ambiguities(tOptions * opts, char const * name, int nm_len) { char const * const hyph = NAMED_OPTS(opts) ? "" : LONG_OPT_MARKER; tOptDesc * pOD = opts->pOptDesc; int idx = 0; fputs(zambig_list_msg, stderr); do { if (pOD->pz_Name == NULL) continue; /* doc option */ if (strneqvcmp(name, pOD->pz_Name, nm_len) == 0) fprintf(stderr, zambig_file, hyph, pOD->pz_Name); else if ( (pOD->pz_DisableName != NULL) && (strneqvcmp(name, pOD->pz_DisableName, nm_len) == 0) ) fprintf(stderr, zambig_file, hyph, pOD->pz_DisableName); } while (pOD++, (++idx < opts->optCt)); }
/** * Print extended usage. Usage/help was requested. * * @param opts the program option descriptor * @param od the option descriptor * @param title the title for the options */ static void prt_extd_usage(tOptions * opts, tOptDesc * od, char const * title) { if ( ((opts->fOptSet & OPTPROC_VENDOR_OPT) != 0) && (od->optActualValue == VENDOR_OPTION_VALUE)) { prt_vendor_opts(opts, title); return; } /* * IF there are option conflicts or dependencies, * THEN print them here. */ if ((od->pOptMust != NULL) || (od->pOptCant != NULL)) prt_conflicts(opts, od); /* * IF there is a disablement string * THEN print the disablement info */ if (od->pz_DisableName != NULL ) fprintf(option_usage_fp, zDis + tab_skip_ct, od->pz_DisableName); /* * Check for argument types that have callbacks with magical properties */ switch (OPTST_GET_ARGTYPE(od->fOptState)) { case OPARG_TYPE_NUMERIC: /* * IF the numeric option has a special callback, * THEN call it, requesting the range or other special info */ if ( (od->pOptProc != NULL) && (od->pOptProc != optionNumericVal) ) { (*(od->pOptProc))(OPTPROC_EMIT_USAGE, od); } break; case OPARG_TYPE_FILE: (*(od->pOptProc))(OPTPROC_EMIT_USAGE, od); break; } /* * IF the option defaults to being enabled, * THEN print that out */ if (od->fOptState & OPTST_INITENABLED) fputs(zEnab + tab_skip_ct, option_usage_fp); /* * IF the option is in an equivalence class * AND not the designated lead * THEN print equivalence and leave it at that. */ if ( (od->optEquivIndex != NO_EQUIVALENT) && (od->optEquivIndex != od->optActualIndex ) ) { fprintf(option_usage_fp, zalt_opt + tab_skip_ct, opts->pOptDesc[ od->optEquivIndex ].pz_Name); return; } /* * IF this particular option can NOT be preset * AND some form of presetting IS allowed, * AND it is not an auto-managed option (e.g. --help, et al.) * THEN advise that this option may not be preset. */ if ( ((od->fOptState & OPTST_NO_INIT) != 0) && ( (opts->papzHomeList != NULL) || (opts->pzPROGNAME != NULL) ) && (od->optIndex < opts->presetOptCt) ) fputs(zNoPreset + tab_skip_ct, option_usage_fp); /* * Print the appearance requirements. */ if (OPTST_GET_ARGTYPE(od->fOptState) == OPARG_TYPE_MEMBERSHIP) fputs(zMembers + tab_skip_ct, option_usage_fp); else switch (od->optMinCt) { case 1: case 0: switch (od->optMaxCt) { case 0: fputs(zPreset + tab_skip_ct, option_usage_fp); break; case NOLIMIT: fputs(zNoLim + tab_skip_ct, option_usage_fp); break; case 1: break; /* * IF the max is more than one but limited, print "UP TO" message */ default: fprintf(option_usage_fp, zUpTo + tab_skip_ct, od->optMaxCt); break; } break; default: /* * More than one is required. Print the range. */ fprintf(option_usage_fp, zMust + tab_skip_ct, od->optMinCt, od->optMaxCt); } if ( NAMED_OPTS(opts) && (opts->specOptIdx.default_opt == od->optIndex)) fputs(zDefaultOpt + tab_skip_ct, option_usage_fp); }
static tSuccess next_opt_arg_may(tOptions* pOpts, tOptState* pOptState) { /* * An option argument is optional. */ switch (pOptState->optType) { case TOPT_SHORT: if (*++pOpts->pzCurOpt != NUL) pOptState->pzOptArg = pOpts->pzCurOpt; else { char* pzLA = pOpts->origArgVect[ pOpts->curOptIdx ]; /* * BECAUSE it is optional, we must make sure * we did not find another flag and that there * is such an argument. */ if ((pzLA == NULL) || (*pzLA == '-')) pOptState->pzOptArg = NULL; else { pOpts->curOptIdx++; /* argument found */ pOptState->pzOptArg = pzLA; } } break; case TOPT_LONG: /* * Look for an argument if we don't already have one (glued on * with a `=' character) *AND* we are not in named argument mode */ if ( (pOptState->pzOptArg == NULL) && (! NAMED_OPTS(pOpts))) { char* pzLA = pOpts->origArgVect[ pOpts->curOptIdx ]; /* * BECAUSE it is optional, we must make sure * we did not find another flag and that there * is such an argument. */ if ((pzLA == NULL) || (*pzLA == '-')) pOptState->pzOptArg = NULL; else { pOpts->curOptIdx++; /* argument found */ pOptState->pzOptArg = pzLA; } } break; default: case TOPT_DEFAULT: fputs(zAO_Woops, stderr ); exit(EX_SOFTWARE); } /* * After an option with an optional argument, we will * *always* start with the next option because if there * were any characters following the option name/flag, * they would be interpreted as the argument. */ pOpts->pzCurOpt = NULL; 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); }
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * HUNT FOR OPTIONS IN THE ARGUMENT LIST * * The next four procedures are "private" to nextOption(). * nextOption() uses findOptDesc() to find the next descriptor and it, in * turn, uses longOptionFind() and shortOptionFind() to actually do the hunt. * * longOptionFind * * Find the long option descriptor for the current option */ LOCAL tSuccess longOptionFind(tOptions* pOpts, char* pzOptName, tOptState* pOptState) { ag_bool disable = AG_FALSE; char* pzEq = strchr(pzOptName, '='); tOptDesc* pOD = pOpts->pOptDesc; int idx = 0; int idxLim = pOpts->optCt; int matchCt = 0; int matchIdx = 0; int nameLen; char opt_name_buf[128]; /* * IF the value is attached to the name, * copy it off so we can NUL terminate. */ if (pzEq != NULL) { nameLen = (int)(pzEq - pzOptName); if (nameLen >= sizeof(opt_name_buf)) return FAILURE; memcpy(opt_name_buf, pzOptName, nameLen); opt_name_buf[nameLen] = NUL; pzOptName = opt_name_buf; pzEq++; } else nameLen = strlen(pzOptName); do { /* * If option disabled or a doc option, skip to next */ if (pOD->pz_Name == NULL) continue; if ( SKIP_OPT(pOD) && (pOD->fOptState != (OPTST_OMITTED | OPTST_NO_INIT))) continue; if (strneqvcmp(pzOptName, pOD->pz_Name, nameLen) == 0) { /* * IF we have a complete match * THEN it takes priority over any already located partial */ if (pOD->pz_Name[ nameLen ] == NUL) { matchCt = 1; matchIdx = idx; break; } } /* * IF there is a disable name * *AND* no argument value has been supplied * (disabled options may have no argument) * *AND* the option name matches the disable name * THEN ... */ else if ( (pOD->pz_DisableName != NULL) && (strneqvcmp(pzOptName, pOD->pz_DisableName, nameLen) == 0) ) { disable = AG_TRUE; /* * IF we have a complete match * THEN it takes priority over any already located partial */ if (pOD->pz_DisableName[ nameLen ] == NUL) { matchCt = 1; matchIdx = idx; break; } } else continue; /* * We found a partial match, either regular or disabling. * Remember the index for later. */ matchIdx = idx; if (++matchCt > 1) break; } while (pOD++, (++idx < idxLim)); /* * Make sure we either found an exact match or found only one partial */ if (matchCt == 1) { pOD = pOpts->pOptDesc + matchIdx; if (SKIP_OPT(pOD)) { fprintf(stderr, zDisabledErr, pOpts->pzProgName, pOD->pz_Name); if (pOD->pzText != NULL) fprintf(stderr, " -- %s", pOD->pzText); fputc('\n', stderr); (*pOpts->pUsageProc)(pOpts, EXIT_FAILURE); /* NOTREACHED */ } /* * IF we found a disablement name, * THEN set the bit in the callers' flag word */ if (disable) pOptState->flags |= OPTST_DISABLED; pOptState->pOD = pOD; pOptState->pzOptArg = pzEq; pOptState->optType = TOPT_LONG; return SUCCESS; } /* * IF there is no equal sign * *AND* we are using named arguments * *AND* there is a default named option, * THEN return that option. */ if ( (pzEq == NULL) && NAMED_OPTS(pOpts) && (pOpts->specOptIdx.default_opt != NO_EQUIVALENT)) { pOptState->pOD = pOpts->pOptDesc + pOpts->specOptIdx.default_opt; pOptState->pzOptArg = pzOptName; pOptState->optType = TOPT_DEFAULT; return SUCCESS; } /* * IF we are to stop on errors (the default, actually) * THEN call the usage procedure. */ if ((pOpts->fOptSet & OPTPROC_ERRSTOP) != 0) { fprintf(stderr, (matchCt == 0) ? zIllOptStr : zAmbigOptStr, pOpts->pzProgPath, pzOptName); (*pOpts->pUsageProc)(pOpts, EXIT_FAILURE); } return FAILURE; }
/* * 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; }
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * PER OPTION TYPE USAGE INFORMATION */ static void printExtendedUsage( tOptions* pOptions, tOptDesc* pOD, arg_types_t* pAT ) { /* * IF there are option conflicts or dependencies, * THEN print them here. */ if ( (pOD->pOptMust != NULL) || (pOD->pOptCant != NULL) ) { fputs( zTabHyp, option_usage_fp ); /* * DEPENDENCIES: */ if (pOD->pOptMust != NULL) { const int* pOptNo = pOD->pOptMust; fputs( zReqThese, option_usage_fp ); for (;;) { fprintf( option_usage_fp, zTabout, pOptions->pOptDesc[ *pOptNo ].pz_Name ); if (*++pOptNo == NO_EQUIVALENT) break; } if (pOD->pOptCant != NULL) fputs( zTabHypAnd, option_usage_fp ); } /* * CONFLICTS: */ if (pOD->pOptCant != NULL) { const int* pOptNo = pOD->pOptCant; fputs( zProhib, option_usage_fp ); for (;;) { fprintf( option_usage_fp, zTabout, pOptions->pOptDesc[ *pOptNo ].pz_Name ); if (*++pOptNo == NO_EQUIVALENT) break; } } } /* * IF there is a disablement string * THEN print the disablement info */ if (pOD->pz_DisableName != NULL ) fprintf( option_usage_fp, zDis, pOD->pz_DisableName ); /* * IF the numeric option has a special callback, * THEN call it, requesting the range or other special info */ if ( (OPTST_GET_ARGTYPE(pOD->fOptState) == OPARG_TYPE_NUMERIC) && (pOD->pOptProc != NULL) && (pOD->pOptProc != optionNumericVal) ) { (*(pOD->pOptProc))( pOptions, NULL ); } /* * IF the option defaults to being enabled, * THEN print that out */ if (pOD->fOptState & OPTST_INITENABLED) fputs( zEnab, option_usage_fp ); /* * IF the option is in an equivalence class * AND not the designated lead * THEN print equivalence and leave it at that. */ if ( (pOD->optEquivIndex != NO_EQUIVALENT) && (pOD->optEquivIndex != pOD->optActualIndex ) ) { fprintf( option_usage_fp, zAlt, pOptions->pOptDesc[ pOD->optEquivIndex ].pz_Name ); return; } /* * IF this particular option can NOT be preset * AND some form of presetting IS allowed, * AND it is not an auto-managed option (e.g. --help, et al.) * THEN advise that this option may not be preset. */ if ( ((pOD->fOptState & OPTST_NO_INIT) != 0) && ( (pOptions->papzHomeList != NULL) || (pOptions->pzPROGNAME != NULL) ) && (pOD->optIndex < pOptions->presetOptCt) ) fputs( zNoPreset, option_usage_fp ); /* * Print the appearance requirements. */ if (OPTST_GET_ARGTYPE(pOD->fOptState) == OPARG_TYPE_MEMBERSHIP) fputs( zMembers, option_usage_fp ); else switch (pOD->optMinCt) { case 1: case 0: switch (pOD->optMaxCt) { case 0: fputs( zPreset, option_usage_fp ); break; case NOLIMIT: fputs( zNoLim, option_usage_fp ); break; case 1: break; /* * IF the max is more than one but limited, print "UP TO" message */ default: fprintf( option_usage_fp, zUpTo, pOD->optMaxCt ); break; } break; default: /* * More than one is required. Print the range. */ fprintf( option_usage_fp, zMust, pOD->optMinCt, pOD->optMaxCt ); } if ( NAMED_OPTS( pOptions ) && (pOptions->specOptIdx.default_opt == pOD->optIndex)) fputs( zDefaultOpt, option_usage_fp ); }