5 * Id: 8700c8e91e8094c455392c691d9b6a7d62222240
6 * Time-stamp: "2009-07-20 20:12:24 bkorb"
8 * This file contains all of the routines that must be linked into
9 * an executable to use the generated option processing. The optional
10 * routines are in separately compiled modules so that they will not
11 * necessarily be linked in.
13 * This file is part of AutoOpts, a companion to AutoGen.
14 * AutoOpts is free software.
15 * AutoOpts is copyright (c) 1992-2009 by Bruce Korb - all rights reserved
17 * AutoOpts is available under any one of two licenses. The license
18 * in use must be one of these two and the choice is under the control
19 * of the user of the license.
21 * The GNU Lesser General Public License, version 3 or later
22 * See the files "COPYING.lgplv3" and "COPYING.gplv3"
24 * The Modified Berkeley Software Distribution License
25 * See the file "COPYING.mbsd"
27 * These files have the following md5sums:
29 * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
30 * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
31 * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
34 /* = = = START-STATIC-FORWARD = = = */
35 /* static forward declarations maintained by mk-fwd */
37 checkEnvOpt(tOptState
* os
, char * env_name
,
38 tOptions
* pOpts
, teEnvPresetType type
);
39 /* = = = END-STATIC-FORWARD = = = */
42 * doPrognameEnv - check for preset values from the ${PROGNAME}
43 * environment variable. This is accomplished by parsing the text into
44 * tokens, temporarily replacing the arg vector and calling
45 * doImmediateOpts and/or doRegularOpts.
48 doPrognameEnv( tOptions
* pOpts
, teEnvPresetType type
)
50 char const* pczOptStr
= getenv( pOpts
->pzPROGNAME
);
57 * No such beast? Then bail now.
59 if (pczOptStr
== NULL
)
63 * Tokenize the string. If there's nothing of interest, we'll bail
66 pTL
= ao_string_tokenize( pczOptStr
);
71 * Substitute our $PROGNAME argument list for the real one
73 sv_argc
= pOpts
->origArgCt
;
74 sv_argv
= pOpts
->origArgVect
;
75 sv_flag
= pOpts
->fOptSet
;
78 * We add a bogus pointer to the start of the list. The program name
79 * has already been pulled from "argv", so it won't get dereferenced.
80 * The option scanning code will skip the "program name" at the start
81 * of this list of tokens, so we accommodate this way ....
83 pOpts
->origArgVect
= (char**)(pTL
->tkn_list
- 1);
84 pOpts
->origArgCt
= pTL
->tkn_ct
+ 1;
85 pOpts
->fOptSet
&= ~OPTPROC_ERRSTOP
;
88 pOpts
->pzCurOpt
= NULL
;
92 (void)doImmediateOpts( pOpts
);
96 (void)doImmediateOpts( pOpts
);
98 pOpts
->pzCurOpt
= NULL
;
102 (void)doRegularOpts( pOpts
);
106 * Free up the temporary arg vector and restore the original program args.
109 pOpts
->origArgVect
= sv_argv
;
110 pOpts
->origArgCt
= sv_argc
;
111 pOpts
->fOptSet
= sv_flag
;
115 checkEnvOpt(tOptState
* os
, char * env_name
,
116 tOptions
* pOpts
, teEnvPresetType type
)
118 os
->pzOptArg
= getenv( env_name
);
119 if (os
->pzOptArg
== NULL
)
122 os
->flags
= OPTST_PRESET
| OPTST_ALLOC_ARG
| os
->pOD
->fOptState
;
123 os
->optType
= TOPT_UNDEFINED
;
125 if ( (os
->pOD
->pz_DisablePfx
!= NULL
)
126 && (streqvcmp( os
->pzOptArg
, os
->pOD
->pz_DisablePfx
) == 0)) {
127 os
->flags
|= OPTST_DISABLED
;
134 * Process only immediate actions
136 if (DO_IMMEDIATELY(os
->flags
))
142 * Process only NON immediate actions
144 if (DO_NORMALLY(os
->flags
) || DO_SECOND_TIME(os
->flags
))
148 default: /* process everything */
153 * Make sure the option value string is persistent and consistent.
155 * The interpretation of the option value depends
156 * on the type of value argument the option takes
158 if (os
->pzOptArg
!= NULL
) {
159 if (OPTST_GET_ARGTYPE(os
->pOD
->fOptState
) == OPARG_TYPE_NONE
) {
161 } else if ( (os
->pOD
->fOptState
& OPTST_ARG_OPTIONAL
)
162 && (*os
->pzOptArg
== NUL
)) {
164 } else if (*os
->pzOptArg
== NUL
) {
167 AGDUPSTR( os
->pzOptArg
, os
->pzOptArg
, "option argument" );
168 os
->flags
|= OPTST_ALLOC_ARG
;
172 handleOption( pOpts
, os
);
176 * doEnvPresets - check for preset values from the envrionment
177 * This routine should process in all, immediate or normal modes....
180 doEnvPresets( tOptions
* pOpts
, teEnvPresetType type
)
186 char zEnvName
[ AO_NAME_SIZE
];
189 * Finally, see if we are to look at the environment
190 * variables for initial values.
192 if ((pOpts
->fOptSet
& OPTPROC_ENVIRON
) == 0)
195 doPrognameEnv( pOpts
, type
);
197 ct
= pOpts
->presetOptCt
;
198 st
.pOD
= pOpts
->pOptDesc
;
200 pzFlagName
= zEnvName
201 + snprintf( zEnvName
, sizeof( zEnvName
), "%s_", pOpts
->pzPROGNAME
);
202 spaceLeft
= AO_NAME_SIZE
- (pzFlagName
- zEnvName
) - 1;
204 for (;ct
-- > 0; st
.pOD
++) {
206 * If presetting is disallowed, then skip this entry
208 if ( ((st
.pOD
->fOptState
& OPTST_NO_INIT
) != 0)
209 || (st
.pOD
->optEquivIndex
!= NO_EQUIVALENT
) )
213 * IF there is no such environment variable,
214 * THEN skip this entry, too.
216 if (strlen( st
.pOD
->pz_NAME
) >= spaceLeft
)
220 * Set up the option state
222 strcpy( pzFlagName
, st
.pOD
->pz_NAME
);
223 checkEnvOpt(&st
, zEnvName
, pOpts
, type
);
227 * Special handling for ${PROGNAME_LOAD_OPTS}
229 if ( (pOpts
->specOptIdx
.save_opts
!= NO_EQUIVALENT
)
230 && (pOpts
->specOptIdx
.save_opts
!= 0)) {
231 st
.pOD
= pOpts
->pOptDesc
+ pOpts
->specOptIdx
.save_opts
+ 1;
232 strcpy( pzFlagName
, st
.pOD
->pz_NAME
);
233 checkEnvOpt(&st
, zEnvName
, pOpts
, type
);
240 * c-file-style: "stroustrup"
241 * indent-tabs-mode: nil
243 * end of autoopts/environment.c */