reset.c revision 258945
15897Sjmz
25897Sjmz/**
35897Sjmz * \file reset.c
45897Sjmz *
55897Sjmz *  Time-stamp:      "2010-07-10 10:56:34 bkorb"
65897Sjmz *
75897Sjmz *  This file is part of AutoOpts, a companion to AutoGen.
85897Sjmz *  AutoOpts is free software.
95897Sjmz *  AutoOpts is Copyright (c) 1992-2011 by Bruce Korb - all rights reserved
105897Sjmz *
115897Sjmz *  AutoOpts is available under any one of two licenses.  The license
125897Sjmz *  in use must be one of these two and the choice is under the control
135897Sjmz *  of the user of the license.
145897Sjmz *
1597748Sschweikh *   The GNU Lesser General Public License, version 3 or later
165897Sjmz *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
175897Sjmz *
185897Sjmz *   The Modified Berkeley Software Distribution License
195897Sjmz *      See the file "COPYING.mbsd"
205897Sjmz *
215897Sjmz *  These files have the following md5sums:
225897Sjmz *
235897Sjmz *  43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
245897Sjmz *  06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
255897Sjmz *  66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
265897Sjmz */
275897Sjmz
285897Sjmzstatic void
295897SjmzoptionReset( tOptions* pOpts, tOptDesc* pOD )
30119418Sobrien{
31119418Sobrien    pOD->fOptState &= OPTST_PERSISTENT_MASK;
32119418Sobrien    pOD->fOptState |= OPTST_RESET;
337430Sbde    if (pOD->pOptProc != NULL)
347430Sbde        pOD->pOptProc(pOpts, pOD);
3512675Sjulian    pOD->optArg.argString =
3634924Sbde        pOpts->originalOptArgArray[ pOD->optIndex ].argString;
3754156Speter    pOD->optCookie = pOpts->originalOptArgCookie[ pOD->optIndex ];
3854156Speter    pOD->fOptState &= OPTST_PERSISTENT_MASK;
3954156Speter}
4054156Speter
4154156Speter
4254156Speterstatic void
4354156SpeteroptionResetEverything(tOptions * pOpts)
4454156Speter{
4587384Simp    tOptDesc * pOD = pOpts->pOptDesc;
466734Sbde    int        ct  = pOpts->presetOptCt;
478876Srgrimes
485897Sjmz    for (;;) {
495897Sjmz        optionReset(pOpts, pOD);
505897Sjmz
515897Sjmz        if (--ct <= 0)
525897Sjmz            break;
535897Sjmz        pOD++;
545897Sjmz    }
555897Sjmz}
565897Sjmz
57183397Sed
585897Sjmz/*=export_func  optionResetOpt
595897Sjmz * private:
605897Sjmz *
615897Sjmz * what:  Reset the value of an option
6212675Sjulian * arg:   + tOptions* + pOpts    + program options descriptor  +
6312675Sjulian * arg:   + tOptDesc* + pOptDesc + the descriptor for this arg +
6412675Sjulian *
6512675Sjulian * doc:
6612675Sjulian *  This code will cause another option to be reset to its initial state.
6747625Sphk *  For example, --reset=foo will cause the --foo option to be reset.
68126080Sphk=*/
69126080Sphkvoid
70111815SphkoptionResetOpt( tOptions* pOpts, tOptDesc* pOD )
71111815Sphk{
72111815Sphk    static ag_bool reset_active = AG_FALSE;
73111815Sphk
74111815Sphk    tOptState opt_state = OPTSTATE_INITIALIZER(DEFINED);
7547625Sphk    char const * pzArg = pOD->optArg.argString;
7612675Sjulian    tSuccess     succ;
7789086Simp
785897Sjmz    if (reset_active)
7987384Simp        return;
8087384Simp
815897Sjmz    if (  (! HAS_originalOptArgArray(pOpts))
825897Sjmz       || (pOpts->originalOptArgCookie == NULL)) {
8354156Speter        fputs(zResetNotConfig, stderr);
8487384Simp        _exit(EX_SOFTWARE);
8587384Simp    }
8687384Simp
8787384Simp    if ((pzArg == NULL) || (*pzArg == NUL)) {
8887384Simp        fputs(zNoResetArg, stderr);
8954156Speter        pOpts->pUsageProc(pOpts, EXIT_FAILURE);
905897Sjmz        /* NOTREACHED */
9187384Simp        assert(0 == 1);
925897Sjmz    }
935897Sjmz
945897Sjmz    reset_active = AG_TRUE;
9587384Simp
9687384Simp    if (pzArg[1] == NUL) {
975897Sjmz        if (*pzArg == '*') {
9887384Simp            optionResetEverything(pOpts);
9987384Simp            reset_active = AG_FALSE;
1005897Sjmz            return;
10187384Simp        }
102127135Snjl
103152249Sjylefort        succ = shortOptionFind(pOpts, (tAoUC)*pzArg, &opt_state);
10487384Simp        if (! SUCCESSFUL(succ)) {
10587384Simp            fprintf(stderr, zIllOptChr, pOpts->pzProgPath, *pzArg);
10687384Simp            pOpts->pUsageProc(pOpts, EXIT_FAILURE);
10787384Simp            /* NOTREACHED */
10887384Simp            assert(0 == 1);
109152249Sjylefort        }
110191054Sed    } else {
11187384Simp        succ = longOptionFind(pOpts, (char *)pzArg, &opt_state);
1125897Sjmz        if (! SUCCESSFUL(succ)) {
1135897Sjmz            fprintf(stderr, zIllOptStr, pOpts->pzProgPath, pzArg);
11487384Simp            pOpts->pUsageProc(pOpts, EXIT_FAILURE);
11587384Simp            /* NOTREACHED */
11687384Simp            assert(0 == 1);
11787384Simp        }
11854156Speter    }
11987384Simp
12087384Simp    /*
12187384Simp     *  We've found the indicated option.  Turn off all non-persistent
12287384Simp     *  flags because we're forcing the option back to its initialized state.
12387384Simp     *  Call any callout procedure to handle whatever it needs to.
12487384Simp     *  Finally, clear the reset flag, too.
12554156Speter     */
12654156Speter    optionReset(pOpts, opt_state.pOD);
12754156Speter    reset_active = AG_FALSE;
128130585Sphk}
1295897Sjmz/*
13087384Simp * Local Variables:
131191054Sed * mode: C
1325897Sjmz * c-file-style: "stroustrup"
13387384Simp * indent-tabs-mode: nil
13487384Simp * End:
13587384Simp * end of autoopts/reset.c */
13687384Simp