updated
[gnutls.git] / src / libopts / env.c
blob4dd18faabaf8e10d541d93311a6811d7f1095cc8
2 /**
3 * \file environment.c
5 * Time-stamp: "2012-04-01 05:59:15 bkorb"
7 * This file contains all of the routines that must be linked into
8 * an executable to use the generated option processing. The optional
9 * routines are in separately compiled modules so that they will not
10 * necessarily be linked in.
12 * This file is part of AutoOpts, a companion to AutoGen.
13 * AutoOpts is free software.
14 * AutoOpts is Copyright (c) 1992-2012 by Bruce Korb - all rights reserved
16 * AutoOpts is available under any one of two licenses. The license
17 * in use must be one of these two and the choice is under the control
18 * of the user of the license.
20 * The GNU Lesser General Public License, version 3 or later
21 * See the files "COPYING.lgplv3" and "COPYING.gplv3"
23 * The Modified Berkeley Software Distribution License
24 * See the file "COPYING.mbsd"
26 * These files have the following md5sums:
28 * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
29 * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
30 * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
33 /* = = = START-STATIC-FORWARD = = = */
34 static void
35 do_env_opt(tOptState * os, char * env_name,
36 tOptions * pOpts, teEnvPresetType type);
37 /* = = = END-STATIC-FORWARD = = = */
40 * doPrognameEnv - check for preset values from the ${PROGNAME}
41 * environment variable. This is accomplished by parsing the text into
42 * tokens, temporarily replacing the arg vector and calling
43 * immediate_opts and/or regular_opts.
45 LOCAL void
46 doPrognameEnv(tOptions * pOpts, teEnvPresetType type)
48 char const * pczOptStr = getenv(pOpts->pzPROGNAME);
49 token_list_t* pTL;
50 int sv_argc;
51 tAoUI sv_flag;
52 char ** sv_argv;
55 * No such beast? Then bail now.
57 if (pczOptStr == NULL)
58 return;
61 * Tokenize the string. If there's nothing of interest, we'll bail
62 * here immediately.
64 pTL = ao_string_tokenize(pczOptStr);
65 if (pTL == NULL)
66 return;
69 * Substitute our $PROGNAME argument list for the real one
71 sv_argc = pOpts->origArgCt;
72 sv_argv = pOpts->origArgVect;
73 sv_flag = pOpts->fOptSet;
76 * We add a bogus pointer to the start of the list. The program name
77 * has already been pulled from "argv", so it won't get dereferenced.
78 * The option scanning code will skip the "program name" at the start
79 * of this list of tokens, so we accommodate this way ....
82 uintptr_t v = (uintptr_t)(pTL->tkn_list);
83 pOpts->origArgVect = (void *)(v - sizeof(char *));
85 pOpts->origArgCt = pTL->tkn_ct + 1;
86 pOpts->fOptSet &= ~OPTPROC_ERRSTOP;
88 pOpts->curOptIdx = 1;
89 pOpts->pzCurOpt = NULL;
91 switch (type) {
92 case ENV_IMM:
93 (void)immediate_opts(pOpts);
94 break;
96 case ENV_ALL:
97 (void)immediate_opts(pOpts);
98 pOpts->curOptIdx = 1;
99 pOpts->pzCurOpt = NULL;
100 /* FALLTHROUGH */
102 case ENV_NON_IMM:
103 (void)regular_opts(pOpts);
107 * Free up the temporary arg vector and restore the original program args.
109 free(pTL);
110 pOpts->origArgVect = sv_argv;
111 pOpts->origArgCt = sv_argc;
112 pOpts->fOptSet = sv_flag;
115 static void
116 do_env_opt(tOptState * os, char * env_name,
117 tOptions * pOpts, teEnvPresetType type)
119 os->pzOptArg = getenv(env_name);
120 if (os->pzOptArg == NULL)
121 return;
123 os->flags = OPTST_PRESET | OPTST_ALLOC_ARG | os->pOD->fOptState;
124 os->optType = TOPT_UNDEFINED;
126 if ( (os->pOD->pz_DisablePfx != NULL)
127 && (streqvcmp(os->pzOptArg, os->pOD->pz_DisablePfx) == 0)) {
128 os->flags |= OPTST_DISABLED;
129 os->pzOptArg = NULL;
132 switch (type) {
133 case ENV_IMM:
135 * Process only immediate actions
137 if (DO_IMMEDIATELY(os->flags))
138 break;
139 return;
141 case ENV_NON_IMM:
143 * Process only NON immediate actions
145 if (DO_NORMALLY(os->flags) || DO_SECOND_TIME(os->flags))
146 break;
147 return;
149 default: /* process everything */
150 break;
154 * Make sure the option value string is persistent and consistent.
156 * The interpretation of the option value depends
157 * on the type of value argument the option takes
159 if (OPTST_GET_ARGTYPE(os->pOD->fOptState) == OPARG_TYPE_NONE) {
161 * Ignore any value.
163 os->pzOptArg = NULL;
165 } else if (os->pzOptArg[0] == NUL) {
167 * If the argument is the empty string and the argument is
168 * optional, then treat it as if the option was not specified.
170 if ((os->pOD->fOptState & OPTST_ARG_OPTIONAL) == 0)
171 return;
172 os->pzOptArg = NULL;
174 } else {
175 AGDUPSTR(os->pzOptArg, os->pzOptArg, "option argument");
176 os->flags |= OPTST_ALLOC_ARG;
179 handle_opt(pOpts, os);
183 * env_presets - check for preset values from the envrionment
184 * This routine should process in all, immediate or normal modes....
186 LOCAL void
187 env_presets(tOptions * pOpts, teEnvPresetType type)
189 int ct;
190 tOptState st;
191 char* pzFlagName;
192 size_t spaceLeft;
193 char zEnvName[ AO_NAME_SIZE ];
196 * Finally, see if we are to look at the environment
197 * variables for initial values.
199 if ((pOpts->fOptSet & OPTPROC_ENVIRON) == 0)
200 return;
202 doPrognameEnv(pOpts, type);
204 ct = pOpts->presetOptCt;
205 st.pOD = pOpts->pOptDesc;
207 pzFlagName = zEnvName
208 + snprintf(zEnvName, sizeof(zEnvName), "%s_", pOpts->pzPROGNAME);
209 spaceLeft = AO_NAME_SIZE - (pzFlagName - zEnvName) - 1;
211 for (;ct-- > 0; st.pOD++) {
212 size_t nln;
215 * If presetting is disallowed, then skip this entry
217 if ( ((st.pOD->fOptState & OPTST_NO_INIT) != 0)
218 || (st.pOD->optEquivIndex != NO_EQUIVALENT) )
219 continue;
222 * IF there is no such environment variable,
223 * THEN skip this entry, too.
225 nln = strlen(st.pOD->pz_NAME) + 1;
226 if (nln <= spaceLeft) {
228 * Set up the option state
230 memcpy(pzFlagName, st.pOD->pz_NAME, nln);
231 do_env_opt(&st, zEnvName, pOpts, type);
236 * Special handling for ${PROGNAME_LOAD_OPTS}
238 if ( (pOpts->specOptIdx.save_opts != NO_EQUIVALENT)
239 && (pOpts->specOptIdx.save_opts != 0)) {
240 size_t nln;
241 st.pOD = pOpts->pOptDesc + pOpts->specOptIdx.save_opts + 1;
243 if (st.pOD->pz_NAME == NULL)
244 return;
246 nln = strlen(st.pOD->pz_NAME) + 1;
248 if (nln > spaceLeft)
249 return;
251 memcpy(pzFlagName, st.pOD->pz_NAME, nln);
252 do_env_opt(&st, zEnvName, pOpts, type);
257 * Local Variables:
258 * mode: C
259 * c-file-style: "stroustrup"
260 * indent-tabs-mode: nil
261 * End:
262 * end of autoopts/environment.c */