123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- static tSuccess
- must_arg(tOptions * opts, char * arg_txt, tOptState * pOS,
- char ** opt_txt, uint32_t * opt_idx)
- {
-
- switch (pOS->optType) {
- case TOPT_SHORT:
-
- if (*arg_txt != NUL)
- return SUCCESS;
- break;
- case TOPT_LONG:
-
- if (pOS->pzOptArg != NULL)
- return SUCCESS;
- break;
- default:
- return FAILURE;
- }
- if (opts->curOptIdx >= opts->origArgCt)
- return FAILURE;
- opt_txt[ (*opt_idx)++ ] = opts->origArgVect[ (opts->curOptIdx)++ ];
- return SUCCESS;
- }
- static tSuccess
- maybe_arg(tOptions * opts, char * arg_txt, tOptState * pOS,
- char ** opt_txt, uint32_t * opt_idx)
- {
-
- switch (pOS->optType) {
- case TOPT_SHORT:
-
- if (*arg_txt != NUL)
- return SUCCESS;
- break;
- case TOPT_LONG:
-
- if (pOS->pzOptArg != NULL)
- return SUCCESS;
- break;
- default:
- return FAILURE;
- }
- if (opts->curOptIdx >= opts->origArgCt)
- return PROBLEM;
- arg_txt = opts->origArgVect[ opts->curOptIdx ];
- if (*arg_txt != '-')
- opt_txt[ (*opt_idx)++ ] = opts->origArgVect[ (opts->curOptIdx)++ ];
- return SUCCESS;
- }
- static tSuccess
- short_opt_ck(tOptions * opts, char * arg_txt, tOptState * pOS,
- char ** opt_txt, uint32_t * opt_idx)
- {
- while (*arg_txt != NUL) {
- if (FAILED(opt_find_short(opts, (uint8_t)*arg_txt, pOS)))
- return FAILURE;
-
- if (OPTST_GET_ARGTYPE(pOS->pOD->fOptState) == OPARG_TYPE_NONE) {
- arg_txt++;
- } else if (pOS->pOD->fOptState & OPTST_ARG_OPTIONAL) {
-
- if (arg_txt[1] != NUL)
- return SUCCESS;
- arg_txt = opts->origArgVect[ opts->curOptIdx ];
- if (*arg_txt != '-')
- opt_txt[ (*opt_idx)++ ] =
- opts->origArgVect[ (opts->curOptIdx)++ ];
- return SUCCESS;
- } else {
-
- if (arg_txt[1] == NUL) {
- if (opts->curOptIdx >= opts->origArgCt)
- return FAILURE;
- opt_txt[ (*opt_idx)++ ] =
- opts->origArgVect[ (opts->curOptIdx)++ ];
- }
- return SUCCESS;
- }
- }
- return SUCCESS;
- }
- static void
- optionSort(tOptions * opts)
- {
- char ** opt_txt;
- char ** ppzOpds;
- uint32_t optsIdx = 0;
- uint32_t opdsIdx = 0;
- tOptState os = OPTSTATE_INITIALIZER(DEFINED);
-
- if ( (getenv("POSIXLY_CORRECT") != NULL)
- || NAMED_OPTS(opts))
- return;
-
- opt_txt = malloc(opts->origArgCt * sizeof(char *));
- if (opt_txt == NULL)
- goto exit_no_mem;
- ppzOpds = malloc(opts->origArgCt * sizeof(char *));
- if (ppzOpds == NULL) {
- free(opt_txt);
- goto exit_no_mem;
- }
- opts->curOptIdx = 1;
- opts->pzCurOpt = NULL;
-
- for (;;) {
- char * arg_txt;
- tSuccess res;
-
- if (opts->curOptIdx >= opts->origArgCt) {
- errno = 0;
- goto joinLists;
- }
- arg_txt = opts->origArgVect[ opts->curOptIdx ];
- if (*arg_txt != '-') {
- ppzOpds[ opdsIdx++ ] = opts->origArgVect[ (opts->curOptIdx)++ ];
- continue;
- }
- switch (arg_txt[1]) {
- case NUL:
-
- ppzOpds[ opdsIdx++ ] = opts->origArgVect[ (opts->curOptIdx)++ ];
- continue;
- case '-':
-
- if (arg_txt[2] == NUL) {
- opt_txt[ optsIdx++ ] =
- opts->origArgVect[ (opts->curOptIdx)++ ];
- goto restOperands;
- }
- res = opt_find_long(opts, arg_txt+2, &os);
- break;
- default:
-
- if ((opts->fOptSet & OPTPROC_SHORTOPT) == 0) {
- res = opt_find_long(opts, arg_txt+1, &os);
- } else {
- res = opt_find_short(opts, (uint8_t)arg_txt[1], &os);
- }
- break;
- }
- if (FAILED(res)) {
- errno = EINVAL;
- goto freeTemps;
- }
-
- opt_txt[ optsIdx++ ] = opts->origArgVect[ (opts->curOptIdx)++ ];
- if (OPTST_GET_ARGTYPE(os.pOD->fOptState) == OPARG_TYPE_NONE) {
-
- if ( (os.optType == TOPT_SHORT)
- && FAILED(short_opt_ck(opts, arg_txt+2, &os, opt_txt,
- &optsIdx)) ) {
- errno = EINVAL;
- goto freeTemps;
- }
- } else if (os.pOD->fOptState & OPTST_ARG_OPTIONAL) {
- switch (maybe_arg(opts, arg_txt+2, &os, opt_txt, &optsIdx)) {
- case FAILURE: errno = EIO; goto freeTemps;
- case PROBLEM: errno = 0; goto joinLists;
- }
- } else {
- switch (must_arg(opts, arg_txt+2, &os, opt_txt, &optsIdx)) {
- case PROBLEM:
- case FAILURE: errno = EIO; goto freeTemps;
- }
- }
- }
- restOperands:
- while (opts->curOptIdx < opts->origArgCt)
- ppzOpds[ opdsIdx++ ] = opts->origArgVect[ (opts->curOptIdx)++ ];
- joinLists:
- if (optsIdx > 0)
- memcpy(opts->origArgVect + 1, opt_txt,
- (size_t)optsIdx * sizeof(char *));
- if (opdsIdx > 0)
- memcpy(opts->origArgVect + 1 + optsIdx, ppzOpds,
- (size_t)opdsIdx * sizeof(char *));
- freeTemps:
- free(opt_txt);
- free(ppzOpds);
- return;
- exit_no_mem:
- errno = ENOMEM;
- return;
- }
|