Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / ntp / dist / sntp / libopts / environment.c
blob37ffaabaa1c97bb040284310b52c8cd31668369f
1 /* $NetBSD$ */
4 /*
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 */
36 static void
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.
47 LOCAL void
48 doPrognameEnv( tOptions* pOpts, teEnvPresetType type )
50 char const* pczOptStr = getenv( pOpts->pzPROGNAME );
51 token_list_t* pTL;
52 int sv_argc;
53 tAoUI sv_flag;
54 char** sv_argv;
57 * No such beast? Then bail now.
59 if (pczOptStr == NULL)
60 return;
63 * Tokenize the string. If there's nothing of interest, we'll bail
64 * here immediately.
66 pTL = ao_string_tokenize( pczOptStr );
67 if (pTL == NULL)
68 return;
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;
87 pOpts->curOptIdx = 1;
88 pOpts->pzCurOpt = NULL;
90 switch (type) {
91 case ENV_IMM:
92 (void)doImmediateOpts( pOpts );
93 break;
95 case ENV_ALL:
96 (void)doImmediateOpts( pOpts );
97 pOpts->curOptIdx = 1;
98 pOpts->pzCurOpt = NULL;
99 /* FALLTHROUGH */
101 case ENV_NON_IMM:
102 (void)doRegularOpts( pOpts );
106 * Free up the temporary arg vector and restore the original program args.
108 free( pTL );
109 pOpts->origArgVect = sv_argv;
110 pOpts->origArgCt = sv_argc;
111 pOpts->fOptSet = sv_flag;
114 static void
115 checkEnvOpt(tOptState * os, char * env_name,
116 tOptions* pOpts, teEnvPresetType type)
118 os->pzOptArg = getenv( env_name );
119 if (os->pzOptArg == NULL)
120 return;
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;
128 os->pzOptArg = NULL;
131 switch (type) {
132 case ENV_IMM:
134 * Process only immediate actions
136 if (DO_IMMEDIATELY(os->flags))
137 break;
138 return;
140 case ENV_NON_IMM:
142 * Process only NON immediate actions
144 if (DO_NORMALLY(os->flags) || DO_SECOND_TIME(os->flags))
145 break;
146 return;
148 default: /* process everything */
149 break;
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) {
160 os->pzOptArg = NULL;
161 } else if ( (os->pOD->fOptState & OPTST_ARG_OPTIONAL)
162 && (*os->pzOptArg == NUL)) {
163 os->pzOptArg = NULL;
164 } else if (*os->pzOptArg == NUL) {
165 os->pzOptArg = zNil;
166 } else {
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....
179 LOCAL void
180 doEnvPresets( tOptions* pOpts, teEnvPresetType type )
182 int ct;
183 tOptState st;
184 char* pzFlagName;
185 size_t spaceLeft;
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)
193 return;
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) )
210 continue;
213 * IF there is no such environment variable,
214 * THEN skip this entry, too.
216 if (strlen( st.pOD->pz_NAME ) >= spaceLeft)
217 continue;
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);
238 * Local Variables:
239 * mode: C
240 * c-file-style: "stroustrup"
241 * indent-tabs-mode: nil
242 * End:
243 * end of autoopts/environment.c */