3
* $Id: environment.c,v 4.13 2007/04/15 19:01:18 bkorb Exp $
4
* Time-stamp: "2007-04-15 11:50:35 bkorb"
6
* This file contains all of the routines that must be linked into
7
* an executable to use the generated option processing. The optional
8
* routines are in separately compiled modules so that they will not
9
* necessarily be linked in.
13
* Automated Options copyright 1992-2007 Bruce Korb
15
* Automated Options is free software.
16
* You may redistribute it and/or modify it under the terms of the
17
* GNU General Public License, as published by the Free Software
18
* Foundation; either version 2, or (at your option) any later version.
20
* Automated Options is distributed in the hope that it will be useful,
21
* but WITHOUT ANY WARRANTY; without even the implied warranty of
22
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
* GNU General Public License for more details.
25
* You should have received a copy of the GNU General Public License
26
* along with Automated Options. See the file "COPYING". If not,
27
* write to: The Free Software Foundation, Inc.,
28
* 51 Franklin Street, Fifth Floor,
29
* Boston, MA 02110-1301, USA.
31
* As a special exception, Bruce Korb gives permission for additional
32
* uses of the text contained in his release of AutoOpts.
34
* The exception is that, if you link the AutoOpts library with other
35
* files to produce an executable, this does not by itself cause the
36
* resulting executable to be covered by the GNU General Public License.
37
* Your use of that executable is in no way restricted on account of
38
* linking the AutoOpts library code into it.
40
* This exception does not however invalidate any other reasons why
41
* the executable file might be covered by the GNU General Public License.
43
* This exception applies only to the code released by Bruce Korb under
44
* the name AutoOpts. If you copy code from other sources under the
45
* General Public License into a copy of AutoOpts, as the General Public
46
* License permits, the exception does not apply to the code that you add
47
* in this way. To avoid misleading anyone as to the status of such
48
* modified files, you must delete this exception notice from them.
50
* If you write modifications of your own for AutoOpts, it is your choice
51
* whether to permit this exception to apply to your modifications.
52
* If you do not wish that, delete this exception notice.
55
/* = = = START-STATIC-FORWARD = = = */
56
/* static forward declarations maintained by :mkfwd */
58
checkEnvOpt(tOptState * os, char * env_name,
59
tOptions* pOpts, teEnvPresetType type);
60
/* = = = END-STATIC-FORWARD = = = */
63
* doPrognameEnv - check for preset values from the ${PROGNAME}
64
* environment variable. This is accomplished by parsing the text into
65
* tokens, temporarily replacing the arg vector and calling
66
* doImmediateOpts and/or doRegularOpts.
69
doPrognameEnv( tOptions* pOpts, teEnvPresetType type )
71
char const* pczOptStr = getenv( pOpts->pzPROGNAME );
78
* IF there is no such environment variable
79
* *or* there is, but we are doing immediate opts and there are
80
* no immediate opts to do (--help inside $PROGNAME is silly,
81
* but --no-load-defs is not, so that is marked)
82
* THEN bail out now. (
84
if ( (pczOptStr == NULL)
85
|| ( (type == ENV_IMM)
86
&& ((pOpts->fOptSet & OPTPROC_HAS_IMMED) == 0) ) )
90
* Tokenize the string. If there's nothing of interest, we'll bail
93
pTL = ao_string_tokenize( pczOptStr );
98
* Substitute our $PROGNAME argument list for the real one
100
sv_argc = pOpts->origArgCt;
101
sv_argv = pOpts->origArgVect;
102
sv_flag = pOpts->fOptSet;
105
* We add a bogus pointer to the start of the list. The program name
106
* has already been pulled from "argv", so it won't get dereferenced.
107
* The option scanning code will skip the "program name" at the start
108
* of this list of tokens, so we accommodate this way ....
110
pOpts->origArgVect = (char**)(pTL->tkn_list - 1);
111
pOpts->origArgCt = pTL->tkn_ct + 1;
112
pOpts->fOptSet &= ~OPTPROC_ERRSTOP;
114
pOpts->curOptIdx = 1;
115
pOpts->pzCurOpt = NULL;
120
* We know the OPTPROC_HAS_IMMED bit is set.
122
(void)doImmediateOpts( pOpts );
126
(void)doRegularOpts( pOpts );
131
* Only to immediate opts if the OPTPROC_HAS_IMMED bit is set.
133
if (pOpts->fOptSet & OPTPROC_HAS_IMMED) {
134
(void)doImmediateOpts( pOpts );
135
pOpts->curOptIdx = 1;
136
pOpts->pzCurOpt = NULL;
138
(void)doRegularOpts( pOpts );
143
* Free up the temporary arg vector and restore the original program args.
146
pOpts->origArgVect = sv_argv;
147
pOpts->origArgCt = sv_argc;
148
pOpts->fOptSet = sv_flag;
152
checkEnvOpt(tOptState * os, char * env_name,
153
tOptions* pOpts, teEnvPresetType type)
155
os->pzOptArg = getenv( env_name );
156
if (os->pzOptArg == NULL)
159
os->flags = OPTST_PRESET | OPTST_ALLOC_ARG | os->pOD->fOptState;
160
os->optType = TOPT_UNDEFINED;
162
if ( (os->pOD->pz_DisablePfx != NULL)
163
&& (streqvcmp( os->pzOptArg, os->pOD->pz_DisablePfx ) == 0)) {
164
os->flags |= OPTST_DISABLED;
171
* Process only immediate actions
173
if (DO_IMMEDIATELY(os->flags))
179
* Process only NON immediate actions
181
if (DO_NORMALLY(os->flags) || DO_SECOND_TIME(os->flags))
185
default: /* process everything */
190
* Make sure the option value string is persistent and consistent.
192
* The interpretation of the option value depends
193
* on the type of value argument the option takes
195
if (os->pzOptArg != NULL) {
196
if (OPTST_GET_ARGTYPE(os->pOD->fOptState) == OPARG_TYPE_NONE) {
198
} else if ( (os->pOD->fOptState & OPTST_ARG_OPTIONAL)
199
&& (*os->pzOptArg == NUL)) {
201
} else if (*os->pzOptArg == NUL) {
204
AGDUPSTR( os->pzOptArg, os->pzOptArg, "option argument" );
205
os->flags |= OPTST_ALLOC_ARG;
209
handleOption( pOpts, os );
213
* doEnvPresets - check for preset values from the envrionment
214
* This routine should process in all, immediate or normal modes....
217
doEnvPresets( tOptions* pOpts, teEnvPresetType type )
223
char zEnvName[ AO_NAME_SIZE ];
226
* Finally, see if we are to look at the environment
227
* variables for initial values.
229
if ((pOpts->fOptSet & OPTPROC_ENVIRON) == 0)
232
doPrognameEnv( pOpts, type );
234
ct = pOpts->presetOptCt;
235
st.pOD = pOpts->pOptDesc;
237
pzFlagName = zEnvName
238
+ snprintf( zEnvName, sizeof( zEnvName ), "%s_", pOpts->pzPROGNAME );
239
spaceLeft = AO_NAME_SIZE - (pzFlagName - zEnvName) - 1;
241
for (;ct-- > 0; st.pOD++) {
243
* If presetting is disallowed, then skip this entry
245
if ( ((st.pOD->fOptState & OPTST_NO_INIT) != 0)
246
|| (st.pOD->optEquivIndex != NO_EQUIVALENT) )
250
* IF there is no such environment variable,
251
* THEN skip this entry, too.
253
if (strlen( st.pOD->pz_NAME ) >= spaceLeft)
257
* Set up the option state
259
strcpy( pzFlagName, st.pOD->pz_NAME );
260
checkEnvOpt(&st, zEnvName, pOpts, type);
264
* Special handling for ${PROGNAME_LOAD_OPTS}
266
if (pOpts->specOptIdx.save_opts != 0) {
267
st.pOD = pOpts->pOptDesc + pOpts->specOptIdx.save_opts + 1;
268
strcpy( pzFlagName, st.pOD->pz_NAME );
269
checkEnvOpt(&st, zEnvName, pOpts, type);
276
* c-file-style: "stroustrup"
277
* indent-tabs-mode: nil
279
* end of autoopts/environment.c */