xref: /netbsd-src/external/bsd/ntp/dist/sntp/libopts/init.c (revision f89f6560d453f5e37386cc7938c072d2f528b9fa)
1 /*	$NetBSD: init.c,v 1.5 2015/04/07 17:34:20 christos Exp $	*/
2 
3 /**
4  * \file initialize.c
5  *
6  *  initialize the libopts data structures.
7  *
8  * @addtogroup autoopts
9  * @{
10  */
11 /*
12  *  This file is part of AutoOpts, a companion to AutoGen.
13  *  AutoOpts is free software.
14  *  AutoOpts is Copyright (C) 1992-2014 by Bruce Korb - all rights reserved
15  *
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.
19  *
20  *   The GNU Lesser General Public License, version 3 or later
21  *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
22  *
23  *   The Modified Berkeley Software Distribution License
24  *      See the file "COPYING.mbsd"
25  *
26  *  These files have the following sha256 sums:
27  *
28  *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
29  *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
30  *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
31  */
32 
33 /* = = = START-STATIC-FORWARD = = = */
34 static tSuccess
35 do_presets(tOptions * opts);
36 /* = = = END-STATIC-FORWARD = = = */
37 
38 /**
39  *  Make sure the option descriptor is there and that we understand it.
40  *  This should be called from any user entry point where one needs to
41  *  worry about validity.  (Some entry points are free to assume that
42  *  the call is not the first to the library and, thus, that this has
43  *  already been called.)
44  *
45  *  Upon successful completion, pzProgName and pzProgPath are set.
46  *
47  *  @param[in,out] opts   program options descriptor
48  *  @param[in]     pname  name of program, from argv[]
49  *  @returns SUCCESS or FAILURE
50  */
51 LOCAL tSuccess
52 validate_struct(tOptions * opts, char const * pname)
53 {
54     if (opts == NULL) {
55         fputs(zno_opt_arg, stderr);
56         return FAILURE;
57     }
58     print_exit = ((opts->fOptSet & OPTPROC_SHELL_OUTPUT) != 0);
59 
60     /*
61      *  IF the client has enabled translation and the translation procedure
62      *  is available, then go do it.
63      */
64     if (  ((opts->fOptSet & OPTPROC_TRANSLATE) != 0)
65        && (opts->pTransProc != NULL)
66        && (option_xlateable_txt.field_ct != 0) ) {
67         /*
68          *  If option names are not to be translated at all, then do not do
69          *  it for configuration parsing either.  (That is the bit that really
70          *  gets tested anyway.)
71          */
72         if ((opts->fOptSet & OPTPROC_NO_XLAT_MASK) == OPTPROC_NXLAT_OPT)
73             opts->fOptSet |= OPTPROC_NXLAT_OPT_CFG;
74         (*opts->pTransProc)();
75     }
76 
77     /*
78      *  IF the struct version is not the current, and also
79      *     either too large (?!) or too small,
80      *  THEN emit error message and fail-exit
81      */
82     if (  ( opts->structVersion  != OPTIONS_STRUCT_VERSION  )
83        && (  (opts->structVersion > OPTIONS_STRUCT_VERSION  )
84           || (opts->structVersion < OPTIONS_MINIMUM_VERSION )
85        )  )  {
86 
87         static char const ao_ver_string[] =
88             STR(AO_CURRENT)":"STR(AO_REVISION)":"STR(AO_AGE)"\n";
89 
90         fprintf(stderr, zwrong_ver, pname, NUM_TO_VER(opts->structVersion));
91         if (opts->structVersion > OPTIONS_STRUCT_VERSION )
92             fputs(ztoo_new, stderr);
93         else
94             fputs(ztoo_old, stderr);
95 
96         fwrite(ao_ver_string, sizeof(ao_ver_string) - 1, 1, stderr);
97         return FAILURE;
98     }
99 
100     /*
101      *  If the program name hasn't been set, then set the name and the path
102      *  and the set of equivalent characters.
103      */
104     if (opts->pzProgName == NULL) {
105         char const *  pz = strrchr(pname, DIRCH);
106 
107         if (pz != NULL)
108             opts->pzProgName = pz+1;
109         else
110             opts->pzProgName = pname;
111 
112         pz = pathfind(getenv("PATH"), (char *)(intptr_t)pname, "rx");
113         if (pz != NULL)
114             pname = (void *)(intptr_t)pz;
115 
116         opts->pzProgPath = pname;
117 
118         /*
119          *  when comparing long names, these are equivalent
120          */
121         strequate(zSepChars);
122     }
123 
124     return SUCCESS;
125 }
126 
127 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
128  *
129  *  DO PRESETS
130  *
131  *  The next several routines do the immediate action pass on the command
132  *  line options, then the environment variables, then the config files in
133  *  reverse order.  Once done with that, the order is reversed and all
134  *  the config files and environment variables are processed again, this
135  *  time only processing the non-immediate action options.  do_presets()
136  *  will then return for optionProcess() to do the final pass on the command
137  *  line arguments.
138  */
139 
140 /**
141  *  scan the command line for immediate action options.
142  *  This is only called the first time through.
143  *  While this procedure is active, the OPTPROC_IMMEDIATE is true.
144  *
145  *  @param pOpts   program options descriptor
146  *  @returns SUCCESS or FAILURE
147  */
148 LOCAL tSuccess
149 immediate_opts(tOptions * opts)
150 {
151     tSuccess  res;
152 
153     opts->fOptSet  |= OPTPROC_IMMEDIATE;
154     opts->curOptIdx = 1;     /* start by skipping program name */
155     opts->pzCurOpt  = NULL;
156 
157     /*
158      *  Examine all the options from the start.  We process any options that
159      *  are marked for immediate processing.
160      */
161     for (;;) {
162         tOptState opt_st = OPTSTATE_INITIALIZER(PRESET);
163 
164         res = next_opt(opts, &opt_st);
165         switch (res) {
166         case FAILURE: goto   failed_option;
167         case PROBLEM: res = SUCCESS; goto leave;
168         case SUCCESS: break;
169         }
170 
171         /*
172          *  IF this is an immediate-attribute option, then do it.
173          */
174         if (! DO_IMMEDIATELY(opt_st.flags))
175             continue;
176 
177         if (! SUCCESSFUL(handle_opt(opts, &opt_st)))
178             break;
179     } failed_option:;
180 
181     if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
182         (*opts->pUsageProc)(opts, EXIT_FAILURE);
183 
184  leave:
185 
186     opts->fOptSet &= ~OPTPROC_IMMEDIATE;
187     return res;
188 }
189 
190 /**
191  *  check for preset values from a config files or envrionment variables
192  *
193  * @param[in,out] opts  the structure with the option names to check
194  */
195 static tSuccess
196 do_presets(tOptions * opts)
197 {
198     tOptDesc * od = NULL;
199 
200     if (! SUCCESSFUL(immediate_opts(opts)))
201         return FAILURE;
202 
203     /*
204      *  IF this option set has a --save-opts option, then it also
205      *  has a --load-opts option.  See if a command line option has disabled
206      *  option presetting.
207      */
208     if (  (opts->specOptIdx.save_opts != NO_EQUIVALENT)
209        && (opts->specOptIdx.save_opts != 0)) {
210         od = opts->pOptDesc + opts->specOptIdx.save_opts + 1;
211         if (DISABLED_OPT(od))
212             return SUCCESS;
213     }
214 
215     /*
216      *  Until we return from this procedure, disable non-presettable opts
217      */
218     opts->fOptSet |= OPTPROC_PRESETTING;
219     /*
220      *  IF there are no config files,
221      *  THEN do any environment presets and leave.
222      */
223     if (opts->papzHomeList == NULL) {
224         env_presets(opts, ENV_ALL);
225     }
226     else {
227         env_presets(opts, ENV_IMM);
228 
229         /*
230          *  Check to see if environment variables have disabled presetting.
231          */
232         if ((od != NULL) && ! DISABLED_OPT(od))
233             intern_file_load(opts);
234 
235         /*
236          *  ${PROGRAM_LOAD_OPTS} value of "no" cannot disable other environment
237          *  variable options.  Only the loading of .rc files.
238          */
239         env_presets(opts, ENV_NON_IMM);
240     }
241     opts->fOptSet &= ~OPTPROC_PRESETTING;
242 
243     return SUCCESS;
244 }
245 
246 /**
247  * AutoOpts initialization
248  *
249  * @param[in,out] opts  the structure to initialize
250  * @param[in]     a_ct  program argument count
251  * @param[in]     a_v   program argument vector
252  */
253 LOCAL bool
254 ao_initialize(tOptions * opts, int a_ct, char ** a_v)
255 {
256     if ((opts->fOptSet & OPTPROC_INITDONE) != 0)
257         return true;
258 
259     opts->origArgCt   = (unsigned int)a_ct;
260     opts->origArgVect = a_v;
261     opts->fOptSet    |= OPTPROC_INITDONE;
262 
263     if (HAS_pzPkgDataDir(opts))
264         program_pkgdatadir = opts->pzPkgDataDir;
265 
266     if (! SUCCESSFUL(do_presets(opts)))
267         return false;
268 
269     /*
270      *  IF option name conversion was suppressed but it is not suppressed
271      *  for the command line, then it's time to translate option names.
272      *  Usage text will not get retranslated.
273      */
274     if (  ((opts->fOptSet & OPTPROC_TRANSLATE) != 0)
275        && (opts->pTransProc != NULL)
276        && ((opts->fOptSet & OPTPROC_NO_XLAT_MASK) == OPTPROC_NXLAT_OPT_CFG)
277        )  {
278         opts->fOptSet &= ~OPTPROC_NXLAT_OPT_CFG;
279         (*opts->pTransProc)();
280     }
281 
282     if ((opts->fOptSet & OPTPROC_REORDER) != 0)
283         optionSort(opts);
284 
285     opts->curOptIdx   = 1;
286     opts->pzCurOpt    = NULL;
287     return true;
288 }
289 
290 /** @}
291  *
292  * Local Variables:
293  * mode: C
294  * c-file-style: "stroustrup"
295  * indent-tabs-mode: nil
296  * End:
297  * end of autoopts/initialize.c */
298