123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- static unsigned int tab_skip_ct = 0;
- #ifndef HAVE_PATHFIND
- # define pathfind(_p, _n, _m) option_pathfind(_p, _n, _m)
- # include "compat/pathfind.c"
- #endif
- #ifndef HAVE_SNPRINTF
- # define vsnprintf option_vsnprintf
- # define snprintf option_snprintf
- # include "compat/snprintf.c"
- #endif
- #ifndef HAVE_STRDUP
- # define strdup(_s) option_strdup(_s)
- # include "compat/strdup.c"
- #endif
- #ifndef HAVE_STRCHR
- # define strrchr(_s, _c) option_strrchr(_s, _c)
- # define strchr(_s, _c) option_strchr(_s, _c)
- # include "compat/strchr.c"
- #endif
- static void *
- ao_malloc(size_t sz)
- {
- void * res = malloc(sz);
- if (res == NULL) {
- fprintf(stderr, zalloc_fail, (int)sz);
- option_exits(EXIT_FAILURE);
- }
- return res;
- }
- static void *
- ao_realloc(void *p, size_t sz)
- {
- void * res = (p == NULL) ? malloc(sz) : realloc(p, sz);
- if (res == NULL) {
- fprintf(stderr, zrealloc_fail, (int)sz, p);
- option_exits(EXIT_FAILURE);
- }
- return res;
- }
- static char *
- ao_strdup(char const *str)
- {
- char * res = strdup(str);
- if (res == NULL) {
- fprintf(stderr, zalloc_fail, (int)strlen(str));
- option_exits(EXIT_FAILURE);
- }
- return res;
- }
- static tSuccess
- handle_opt(tOptions * opts, tOptState * o_st)
- {
-
- tOptDesc * od = o_st->pOD;
- tOptProc * opt_proc = od->pOptProc;
- if (od->fOptState & OPTST_ALLOC_ARG)
- AGFREE(od->optArg.argString);
- od->optArg.argString = o_st->pzOptArg;
-
- if ( ((opts->fOptSet & OPTPROC_PRESETTING) != 0)
- && ((od->fOptState & OPTST_NO_INIT) != 0)
- )
- return PROBLEM;
-
- if (od->optEquivIndex != NO_EQUIVALENT) {
- tOptDesc * eqv_od = opts->pOptDesc + od->optEquivIndex;
-
- if ((od->fOptState & OPTST_DEFINED) != 0) {
-
- if (eqv_od->optActualIndex != od->optIndex) {
- fprintf(stderr, zmultiway_bug, eqv_od->pz_Name, od->pz_Name,
- (opts->pOptDesc + eqv_od->optActualIndex)->pz_Name);
- return FAILURE;
- }
- } else {
-
- eqv_od->optActualIndex = NO_EQUIVALENT;
- }
- if (eqv_od->optActualIndex != od->optIndex) {
-
- eqv_od->optActualValue = od->optValue;
- eqv_od->optActualIndex = od->optIndex;
- o_st->flags |= OPTST_EQUIVALENCE;
- }
-
- eqv_od->optArg.argString = od->optArg.argString;
- od = eqv_od;
- } else {
- od->optActualValue = od->optValue;
- od->optActualIndex = od->optIndex;
- }
- od->fOptState &= OPTST_PERSISTENT_MASK;
- od->fOptState |= (o_st->flags & ~OPTST_PERSISTENT_MASK);
-
- if ( (od->fOptState & OPTST_DEFINED)
- && (++od->optOccCt > od->optMaxCt) )
- return too_many_occurrences(opts, od);
-
- if (opt_proc != NULL)
- (*opt_proc)(opts, od);
- return SUCCESS;
- }
- static tSuccess
- next_opt(tOptions * opts, tOptState * o_st)
- {
- {
- tSuccess res = find_opt(opts, o_st);
- if (! SUCCESSFUL(res))
- return res;
- }
- if ( ((o_st->flags & OPTST_DEFINED) != 0)
- && ((o_st->pOD->fOptState & OPTST_NO_COMMAND) != 0)) {
- fprintf(stderr, zNotCmdOpt, o_st->pOD->pz_Name);
- return FAILURE;
- }
- return get_opt_arg(opts, o_st);
- }
- static tSuccess
- regular_opts(tOptions * opts)
- {
-
- for (;;) {
- tOptState opt_st = OPTSTATE_INITIALIZER(DEFINED);
- switch (next_opt(opts, &opt_st)) {
- case FAILURE: goto failed_option;
- case PROBLEM: return SUCCESS;
- case SUCCESS: break;
- }
-
- if (! DO_NORMALLY(opt_st.flags)) {
- if (! DO_SECOND_TIME(opt_st.flags))
- continue;
- opt_st.pOD->optOccCt--;
- }
- if (! SUCCESSFUL(handle_opt(opts, &opt_st)))
- break;
- } failed_option:;
- if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
- (*opts->pUsageProc)(opts, EXIT_FAILURE);
- return FAILURE;
- }
- int
- optionProcess(tOptions * opts, int a_ct, char ** a_v)
- {
- if (! SUCCESSFUL(validate_struct(opts, a_v[0])))
- ao_bug(zbad_data_msg);
-
-
- if (! ao_initialize(opts, a_ct, a_v))
- return 0;
-
- if (opts->curOptIdx <= 0) {
- opts->curOptIdx = 1;
- opts->pzCurOpt = NULL;
- }
- if (! SUCCESSFUL(regular_opts(opts)))
- return (int)opts->origArgCt;
-
- switch (opts->specOptIdx.save_opts) {
- case 0:
- case NO_EQUIVALENT:
- break;
- default:
- {
- tOptDesc * od = opts->pOptDesc + opts->specOptIdx.save_opts;
- if (SELECTED_OPT(od)) {
- optionSaveFile(opts);
- option_exits(EXIT_SUCCESS);
- }
- }
- }
-
- if (((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
- && (! is_consistent(opts)))
- (*opts->pUsageProc)(opts, EXIT_FAILURE);
- return (int)opts->curOptIdx;
- }
|