3
* $Id: environment.c,v 4.9 2006/09/24 02:11:16 bkorb Exp $
4
* Time-stamp: "2005-10-29 13:23:59 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-2006 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 */
57
/* = = = END-STATIC-FORWARD = = = */
60
* doPrognameEnv - check for preset values from the ${PROGNAME}
61
* environment variable. This is accomplished by parsing the text into
62
* tokens, temporarily replacing the arg vector and calling
63
* doImmediateOpts and/or doRegularOpts.
66
doPrognameEnv( tOptions* pOpts, teEnvPresetType type )
68
char const* pczOptStr = getenv( pOpts->pzPROGNAME );
75
* IF there is no such environment variable
76
* *or* there is, but we are doing immediate opts and there are
77
* no immediate opts to do (--help inside $PROGNAME is silly,
78
* but --no-load-defs is not, so that is marked)
79
* THEN bail out now. (
81
if ( (pczOptStr == NULL)
82
|| ( (type == ENV_IMM)
83
&& ((pOpts->fOptSet & OPTPROC_HAS_IMMED) == 0) ) )
87
* Tokenize the string. If there's nothing of interest, we'll bail
90
pTL = ao_string_tokenize( pczOptStr );
95
* Substitute our $PROGNAME argument list for the real one
97
sv_argc = pOpts->origArgCt;
98
sv_argv = pOpts->origArgVect;
99
sv_flag = pOpts->fOptSet;
102
* We add a bogus pointer to the start of the list. The program name
103
* has already been pulled from "argv", so it won't get dereferenced.
104
* The option scanning code will skip the "program name" at the start
105
* of this list of tokens, so we accommodate this way ....
107
pOpts->origArgVect = (char**)(pTL->tkn_list - 1);
108
pOpts->origArgCt = pTL->tkn_ct + 1;
109
pOpts->fOptSet &= ~OPTPROC_ERRSTOP;
111
pOpts->curOptIdx = 1;
112
pOpts->pzCurOpt = NULL;
117
* We know the OPTPROC_HAS_IMMED bit is set.
119
(void)doImmediateOpts( pOpts );
123
(void)doRegularOpts( pOpts );
128
* Only to immediate opts if the OPTPROC_HAS_IMMED bit is set.
130
if (pOpts->fOptSet & OPTPROC_HAS_IMMED) {
131
(void)doImmediateOpts( pOpts );
132
pOpts->curOptIdx = 1;
133
pOpts->pzCurOpt = NULL;
135
(void)doRegularOpts( pOpts );
140
* Free up the temporary arg vector and restore the original program args.
143
pOpts->origArgVect = sv_argv;
144
pOpts->origArgCt = sv_argc;
145
pOpts->fOptSet = sv_flag;
150
* doEnvPresets - check for preset values from the envrionment
151
* This routine should process in all, immediate or normal modes....
154
doEnvPresets( tOptions* pOpts, teEnvPresetType type )
160
char zEnvName[ AO_NAME_SIZE ];
163
* Finally, see if we are to look at the environment
164
* variables for initial values.
166
if ((pOpts->fOptSet & OPTPROC_ENVIRON) == 0)
169
doPrognameEnv( pOpts, type );
171
ct = pOpts->presetOptCt;
172
st.pOD = pOpts->pOptDesc;
174
pzFlagName = zEnvName
175
+ snprintf( zEnvName, sizeof( zEnvName ), "%s_", pOpts->pzPROGNAME );
176
spaceLeft = AO_NAME_SIZE - (pzFlagName - zEnvName) - 1;
178
for (;ct-- > 0; st.pOD++) {
180
* If presetting is disallowed, then skip this entry
182
if ( ((st.pOD->fOptState & OPTST_NO_INIT) != 0)
183
|| (st.pOD->optEquivIndex != NO_EQUIVALENT) )
187
* IF there is no such environment variable,
188
* THEN skip this entry, too.
190
if (strlen( st.pOD->pz_NAME ) >= spaceLeft)
194
* Set up the option state
196
strcpy( pzFlagName, st.pOD->pz_NAME );
197
st.pzOptArg = getenv( zEnvName );
198
if (st.pzOptArg == NULL)
200
st.flags = OPTST_PRESET | st.pOD->fOptState;
201
st.optType = TOPT_UNDEFINED;
203
if ( (st.pOD->pz_DisablePfx != NULL)
204
&& (streqvcmp( st.pzOptArg, st.pOD->pz_DisablePfx ) == 0)) {
205
st.flags |= OPTST_DISABLED;
212
* Process only immediate actions
214
if (DO_IMMEDIATELY(st.flags))
220
* Process only NON immediate actions
222
if (DO_NORMALLY(st.flags) || DO_SECOND_TIME(st.flags))
226
default: /* process everything */
231
* Make sure the option value string is persistent and consistent.
232
* This may be a memory leak, but we cannot do anything about it.
234
* The interpretation of the option value depends
235
* on the type of value argument the option takes
237
if (st.pzOptArg != NULL) {
238
if (OPTST_GET_ARGTYPE(st.pOD->fOptState) == OPARG_TYPE_NONE) {
240
} else if ( (st.pOD->fOptState & OPTST_ARG_OPTIONAL)
241
&& (*st.pzOptArg == NUL)) {
243
} else if (*st.pzOptArg == NUL) {
246
AGDUPSTR( st.pzOptArg, st.pzOptArg, "option argument" );
250
handleOption( pOpts, &st );
257
* c-file-style: "stroustrup"
258
* indent-tabs-mode: nil
260
* end of autoopts/environment.c */