Exemple #1
0
/*
 *  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
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;

        /*
         *  See if we can have an arg.
         */
        if (OPTST_GET_ARGTYPE(pOS->pOD->fOptState) == OPARG_TYPE_NONE) {
            arg_txt++;

        } 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 (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 we need another argument, be sure it is there and
             *  take it.
             */
            if (arg_txt[1] == NUL) {
                if (opts->curOptIdx >= opts->origArgCt)
                    return FAILURE;
                opt_txt[ (*opt_idx)++ ] =
                    opts->origArgVect[ (opts->curOptIdx)++ ];
            }
            return SUCCESS;
        }
    }
    return SUCCESS;
}
Exemple #2
0
/*=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 bool reset_active = false;

    tOptState opt_state = OPTSTATE_INITIALIZER(DEFINED);
    char const * pzArg = pOD->optArg.argString;
    tSuccess     succ;

    if (pOpts <= OPTPROC_EMIT_LIMIT)
        return;

    if (reset_active)
        return;

    if (  (! HAS_originalOptArgArray(pOpts))
       || (pOpts->originalOptArgCookie == NULL))
        ao_bug(zno_reset);

    if ((pzArg == NULL) || (*pzArg == NUL)) {
        fprintf(stderr, zreset_arg, pOpts->pzProgName, pOD->pz_Name);
        pOpts->pUsageProc(pOpts, EXIT_FAILURE);
        /* NOTREACHED */
        assert(0 == 1);
    }

    reset_active = true;

    if (pzArg[1] == NUL) {
        if (*pzArg == '*') {
            optionResetEverything(pOpts);
            reset_active = false;
            return;
        }

        succ = opt_find_short(pOpts, (uint8_t)*pzArg, &opt_state);
        if (! SUCCESSFUL(succ)) {
            fprintf(stderr, zIllOptChr, pOpts->pzProgPath, *pzArg);
            pOpts->pUsageProc(pOpts, EXIT_FAILURE);
            /* NOTREACHED */
            assert(0 == 1);
        }
    } else {
        succ = opt_find_long(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 = false;
}
Exemple #3
0
/*
 *  If the program wants sorted options (separated operands and options),
 *  then this routine will to the trick.
 */
LOCAL void
optionSort(tOptions * opts)
{
    char **  opt_txt;
    char **  ppzOpds;
    uint32_t optsIdx = 0;
    uint32_t opdsIdx = 0;

    tOptState os = OPTSTATE_INITIALIZER(DEFINED);

    /*
     *  Disable for POSIX conformance, or if there are no operands.
     */
    if (  (getenv("POSIXLY_CORRECT") != NULL)
       || NAMED_OPTS(opts))
        return;

    /*
     *  Make sure we can allocate two full-sized arg vectors.
     */
    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;

    /*
     *  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 * arg_txt;
        tSuccess res;

        /*
         *  If we're out of arguments, we're done.  Join the option and
         *  operand lists into the original argument vector.
         */
        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:
            /*
             *  A single hyphen is an operand.
             */
            ppzOpds[ opdsIdx++ ] = opts->origArgVect[ (opts->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 (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 short options are not allowed, then do long
             *  option processing.  Otherwise the character must be a
             *  short (i.e. single character) option.
             */
            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;
        }

        /*
         *  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.
         */
        opt_txt[ optsIdx++ ] = opts->origArgVect[ (opts->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(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;
            }
        }
    } /* for (;;) */

 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;
}
Exemple #4
0
/**
 *  Find the option descriptor for the current option.
 *
 *  @param[in,out] opts  the program option descriptor
 *  @param[in,out] o_st  the option processing state
 *  @returns SUCCESS or FAILURE
 */
LOCAL tSuccess
find_opt(tOptions * opts, tOptState * o_st)
{
    /*
     *  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 ((opts->pzCurOpt != NULL) && (*opts->pzCurOpt != NUL))
        return opt_find_short(opts, (uint8_t)*(opts->pzCurOpt), o_st);

    if (opts->curOptIdx >= opts->origArgCt)
        return PROBLEM; /* NORMAL COMPLETION */

    opts->pzCurOpt = opts->origArgVect[ opts->curOptIdx ];

    /*
     *  IF all arguments must be named options, ...
     */
    if (NAMED_OPTS(opts)) {
        char *      pz  = opts->pzCurOpt;
        int         def;
        tSuccess    res;
        uint16_t *  def_opt;

        opts->curOptIdx++;

        if (*pz != '-')
            return opt_find_long(opts, pz, o_st);

        /*
         *  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 *)(intptr_t)&(opts->specOptIdx.default_opt);
        def      = *def_opt;
        *def_opt = NO_EQUIVALENT;
        res      = opt_find_long(opts, pz, o_st);
        *def_opt = (uint16_t)def;
        return res;
    }

    /*
     *  Note the kind of flag/option marker
     */
    if (*((opts->pzCurOpt)++) != '-')
        return PROBLEM; /* NORMAL COMPLETION - this + rest are operands */

    /*
     *  Special hack for a hyphen by itself
     */
    if (*(opts->pzCurOpt) == NUL)
        return PROBLEM; /* NORMAL COMPLETION - this + rest are operands */

    /*
     *  The current argument is to be processed as an option argument
     */
    opts->curOptIdx++;

    /*
     *  We have an option marker.
     *  Test the next character for long option indication
     */
    if (opts->pzCurOpt[0] == '-') {
        if (*++(opts->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 ((opts->fOptSet & OPTPROC_LONGOPT) == 0) {
            fprintf(stderr, zIllOptStr, opts->pzProgPath, opts->pzCurOpt-2);
            return FAILURE;
        }

        return opt_find_long(opts, opts->pzCurOpt, o_st);
    }

    /*
     *  If short options are not allowed, then do long
     *  option processing.  Otherwise the character must be a
     *  short (i.e. single character) option.
     */
    if ((opts->fOptSet & OPTPROC_SHORTOPT) != 0)
        return opt_find_short(opts, (uint8_t)*(opts->pzCurOpt), o_st);

    return opt_find_long(opts, opts->pzCurOpt, o_st);
}
Exemple #5
0
/**
 *  Load an option from a block of text.  The text must start with the
 *  configurable/option name and be followed by its associated value.
 *  That value may be processed in any of several ways.  See "tOptionLoadMode"
 *  in autoopts.h.
 *
 * @param[in,out] opts       program options descriptor
 * @param[in,out] opt_state  option processing state
 * @param[in,out] line       source line with long option name in it
 * @param[in]     direction  current processing direction (preset or not)
 * @param[in]     load_mode  option loading mode (OPTION_LOAD_*)
 */
LOCAL void
load_opt_line(tOptions * opts, tOptState * opt_state, char * line,
              tDirection direction, tOptionLoadMode load_mode )
{
    /*
     * When parsing a stored line, we only look at the characters after
     * a hyphen.  Long names must always be at least two characters and
     * short options are always exactly one character long.
     */
    line = SPN_LOAD_LINE_SKIP_CHARS(line);

    {
        char * arg = assemble_arg_val(line, load_mode);

        if (IS_OPTION_NAME_CHAR(line[1])) {

            if (! SUCCESSFUL(opt_find_long(opts, line, opt_state)))
                return;

        } else if (! SUCCESSFUL(opt_find_short(opts, *line, opt_state)))
            return;

        if ((! CALLED(direction)) && (opt_state->flags & OPTST_NO_INIT))
            return;

        opt_state->pzOptArg = trim_quotes(arg);
    }

    if (! direction_ok(opt_state->flags, direction))
        return;

    /*
     *  Fix up the args.
     */
    if (OPTST_GET_ARGTYPE(opt_state->pOD->fOptState) == OPARG_TYPE_NONE) {
        if (*opt_state->pzOptArg != NUL)
            return;
        opt_state->pzOptArg = NULL;

    } else if (opt_state->pOD->fOptState & OPTST_ARG_OPTIONAL) {
        if (*opt_state->pzOptArg == NUL)
             opt_state->pzOptArg = NULL;
        else {
            AGDUPSTR(opt_state->pzOptArg, opt_state->pzOptArg, "opt arg");
            opt_state->flags |= OPTST_ALLOC_ARG;
        }

    } else {
        if (*opt_state->pzOptArg == NUL)
             opt_state->pzOptArg = zNil;
        else {
            AGDUPSTR(opt_state->pzOptArg, opt_state->pzOptArg, "opt arg");
            opt_state->flags |= OPTST_ALLOC_ARG;
        }
    }

    {
        tOptionLoadMode sv = option_load_mode;
        option_load_mode = load_mode;
        handle_opt(opts, opt_state);
        option_load_mode = sv;
    }
}