xref: /netbsd-src/external/bsd/ntp/dist/sntp/libopts/init.c (revision ccd9df534e375a4366c5b55f23782053c7a98d82)
1 /*	$NetBSD: init.c,v 1.9 2020/05/25 20:47:34 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-2015 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         fprintf(stderr, zwrong_ver, pname, NUM_TO_VER(opts->structVersion));
87         if (opts->structVersion > OPTIONS_STRUCT_VERSION )
88             fputs(ztoo_new, stderr);
89         else
90             fputs(ztoo_old, stderr);
91 
92         fwrite(ao_ver_string, sizeof(ao_ver_string) - 1, 1, stderr);
93         return FAILURE;
94     }
95 
96     /*
97      *  If the program name hasn't been set, then set the name and the path
98      *  and the set of equivalent characters.
99      */
100     if (opts->pzProgName == NULL) {
101         char const *  pz = strrchr(pname, DIRCH);
102         char const ** pp = VOIDP(&(opts->pzProgName));
103 
104         if (pz != NULL)
105             *pp = pz+1;
106         else
107             *pp = pname;
108 
109         pz = pathfind(getenv("PATH"), pname, "rx");
110         if (pz != NULL)
111             pname = VOIDP(pz);
112 
113         pp  = (char const **)VOIDP(&(opts->pzProgPath));
114         *pp = pname;
115 
116         /*
117          *  when comparing long names, these are equivalent
118          */
119         strequate(zSepChars);
120     }
121 
122     return SUCCESS;
123 }
124 
125 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
126  *
127  *  DO PRESETS
128  *
129  *  The next several routines do the immediate action pass on the command
130  *  line options, then the environment variables, then the config files in
131  *  reverse order.  Once done with that, the order is reversed and all
132  *  the config files and environment variables are processed again, this
133  *  time only processing the non-immediate action options.  do_presets()
134  *  will then return for optionProcess() to do the final pass on the command
135  *  line arguments.
136  */
137 
138 /**
139  *  scan the command line for immediate action options.
140  *  This is only called the first time through.
141  *  While this procedure is active, the OPTPROC_IMMEDIATE is true.
142  *
143  *  @param pOpts   program options descriptor
144  *  @returns SUCCESS or FAILURE
145  */
146 LOCAL tSuccess
147 immediate_opts(tOptions * opts)
148 {
149     tSuccess  res;
150 
151     opts->fOptSet  |= OPTPROC_IMMEDIATE;
152     opts->curOptIdx = 1;     /* start by skipping program name */
153     opts->pzCurOpt  = NULL;
154 
155     /*
156      *  Examine all the options from the start.  We process any options that
157      *  are marked for immediate processing.
158      */
159     for (;;) {
160         tOptState opt_st = OPTSTATE_INITIALIZER(PRESET);
161 
162         res = next_opt(opts, &opt_st);
163         switch (res) {
164         case FAILURE: goto   failed_option;
165         case PROBLEM: res = SUCCESS; goto leave;
166         case SUCCESS: break;
167         }
168 
169         /*
170          *  IF this is an immediate-attribute option, then do it.
171          */
172         if (! DO_IMMEDIATELY(opt_st.flags))
173             continue;
174 
175         if (! SUCCESSFUL(handle_opt(opts, &opt_st)))
176             break;
177     } failed_option:;
178 
179     if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
180         (*opts->pUsageProc)(opts, EXIT_FAILURE);
181 
182  leave:
183 
184     opts->fOptSet &= ~OPTPROC_IMMEDIATE;
185     return res;
186 }
187 
188 /**
189  *  check for preset values from a config files or envrionment variables
190  *
191  * @param[in,out] opts  the structure with the option names to check
192  */
193 static tSuccess
194 do_presets(tOptions * opts)
195 {
196     tOptDesc * od = NULL;
197 
198     if (! SUCCESSFUL(immediate_opts(opts)))
199         return FAILURE;
200 
201     /*
202      *  IF this option set has a --save-opts option, then it also
203      *  has a --load-opts option.  See if a command line option has disabled
204      *  option presetting.
205      */
206     if (  (opts->specOptIdx.save_opts != NO_EQUIVALENT)
207        && (opts->specOptIdx.save_opts != 0)) {
208         od = opts->pOptDesc + opts->specOptIdx.save_opts + 1;
209         if (DISABLED_OPT(od))
210             return SUCCESS;
211     }
212 
213     /*
214      *  Until we return from this procedure, disable non-presettable opts
215      */
216     opts->fOptSet |= OPTPROC_PRESETTING;
217     /*
218      *  IF there are no config files,
219      *  THEN do any environment presets and leave.
220      */
221     if (opts->papzHomeList == NULL) {
222         env_presets(opts, ENV_ALL);
223     }
224     else {
225         env_presets(opts, ENV_IMM);
226 
227         /*
228          *  Check to see if environment variables have disabled presetting.
229          */
230         if ((od != NULL) && ! DISABLED_OPT(od))
231             intern_file_load(opts);
232 
233         /*
234          *  ${PROGRAM_LOAD_OPTS} value of "no" cannot disable other environment
235          *  variable options.  Only the loading of .rc files.
236          */
237         env_presets(opts, ENV_NON_IMM);
238     }
239     opts->fOptSet &= ~OPTPROC_PRESETTING;
240 
241     return SUCCESS;
242 }
243 
244 /**
245  * AutoOpts initialization
246  *
247  * @param[in,out] opts  the structure to initialize
248  * @param[in]     a_ct  program argument count
249  * @param[in]     a_v   program argument vector
250  */
251 LOCAL bool
252 ao_initialize(tOptions * opts, int a_ct, char ** a_v)
253 {
254     if ((opts->fOptSet & OPTPROC_INITDONE) != 0)
255         return true;
256 
257     opts->origArgCt   = (unsigned int)a_ct;
258     opts->origArgVect = a_v;
259     opts->fOptSet    |= OPTPROC_INITDONE;
260 
261     if (HAS_pzPkgDataDir(opts))
262         program_pkgdatadir = opts->pzPkgDataDir;
263 
264     if (! SUCCESSFUL(do_presets(opts)))
265         return false;
266 
267     /*
268      *  IF option name conversion was suppressed but it is not suppressed
269      *  for the command line, then it's time to translate option names.
270      *  Usage text will not get retranslated.
271      */
272     if (  ((opts->fOptSet & OPTPROC_TRANSLATE) != 0)
273        && (opts->pTransProc != NULL)
274        && ((opts->fOptSet & OPTPROC_NO_XLAT_MASK) == OPTPROC_NXLAT_OPT_CFG)
275        )  {
276         opts->fOptSet &= ~OPTPROC_NXLAT_OPT_CFG;
277         (*opts->pTransProc)();
278     }
279 
280     if ((opts->fOptSet & OPTPROC_REORDER) != 0)
281         optionSort(opts);
282 
283     opts->curOptIdx   = 1;
284     opts->pzCurOpt    = NULL;
285     return true;
286 }
287 
288 /** @}
289  *
290  * Local Variables:
291  * mode: C
292  * c-file-style: "stroustrup"
293  * indent-tabs-mode: nil
294  * End:
295  * end of autoopts/initialize.c */
296