xref: /freebsd/contrib/ntp/sntp/libopts/init.c (revision 788ca347b816afd83b2885e0c79aeeb88649b2ab)
1 /**
2  * \file initialize.c
3  *
4  *  initialize the libopts data structures.
5  *
6  * @addtogroup autoopts
7  * @{
8  */
9 /*
10  *  This file is part of AutoOpts, a companion to AutoGen.
11  *  AutoOpts is free software.
12  *  AutoOpts is Copyright (C) 1992-2014 by Bruce Korb - all rights reserved
13  *
14  *  AutoOpts is available under any one of two licenses.  The license
15  *  in use must be one of these two and the choice is under the control
16  *  of the user of the license.
17  *
18  *   The GNU Lesser General Public License, version 3 or later
19  *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
20  *
21  *   The Modified Berkeley Software Distribution License
22  *      See the file "COPYING.mbsd"
23  *
24  *  These files have the following sha256 sums:
25  *
26  *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
27  *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
28  *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
29  */
30 
31 /* = = = START-STATIC-FORWARD = = = */
32 static tSuccess
33 do_presets(tOptions * opts);
34 /* = = = END-STATIC-FORWARD = = = */
35 
36 /**
37  *  Make sure the option descriptor is there and that we understand it.
38  *  This should be called from any user entry point where one needs to
39  *  worry about validity.  (Some entry points are free to assume that
40  *  the call is not the first to the library and, thus, that this has
41  *  already been called.)
42  *
43  *  Upon successful completion, pzProgName and pzProgPath are set.
44  *
45  *  @param[in,out] opts   program options descriptor
46  *  @param[in]     pname  name of program, from argv[]
47  *  @returns SUCCESS or FAILURE
48  */
49 LOCAL tSuccess
50 validate_struct(tOptions * opts, char const * pname)
51 {
52     if (opts == NULL) {
53         fputs(zno_opt_arg, stderr);
54         return FAILURE;
55     }
56     print_exit = ((opts->fOptSet & OPTPROC_SHELL_OUTPUT) != 0);
57 
58     /*
59      *  IF the client has enabled translation and the translation procedure
60      *  is available, then go do it.
61      */
62     if (  ((opts->fOptSet & OPTPROC_TRANSLATE) != 0)
63        && (opts->pTransProc != NULL)
64        && (option_xlateable_txt.field_ct != 0) ) {
65         /*
66          *  If option names are not to be translated at all, then do not do
67          *  it for configuration parsing either.  (That is the bit that really
68          *  gets tested anyway.)
69          */
70         if ((opts->fOptSet & OPTPROC_NO_XLAT_MASK) == OPTPROC_NXLAT_OPT)
71             opts->fOptSet |= OPTPROC_NXLAT_OPT_CFG;
72         (*opts->pTransProc)();
73     }
74 
75     /*
76      *  IF the struct version is not the current, and also
77      *     either too large (?!) or too small,
78      *  THEN emit error message and fail-exit
79      */
80     if (  ( opts->structVersion  != OPTIONS_STRUCT_VERSION  )
81        && (  (opts->structVersion > OPTIONS_STRUCT_VERSION  )
82           || (opts->structVersion < OPTIONS_MINIMUM_VERSION )
83        )  )  {
84 
85         static char const ao_ver_string[] =
86             STR(AO_CURRENT)":"STR(AO_REVISION)":"STR(AO_AGE)"\n";
87 
88         fprintf(stderr, zwrong_ver, pname, NUM_TO_VER(opts->structVersion));
89         if (opts->structVersion > OPTIONS_STRUCT_VERSION )
90             fputs(ztoo_new, stderr);
91         else
92             fputs(ztoo_old, stderr);
93 
94         fwrite(ao_ver_string, sizeof(ao_ver_string) - 1, 1, stderr);
95         return FAILURE;
96     }
97 
98     /*
99      *  If the program name hasn't been set, then set the name and the path
100      *  and the set of equivalent characters.
101      */
102     if (opts->pzProgName == NULL) {
103         char const *  pz = strrchr(pname, DIRCH);
104 
105         if (pz != NULL)
106             opts->pzProgName = pz+1;
107         else
108             opts->pzProgName = pname;
109 
110         pz = pathfind(getenv("PATH"), (char *)(intptr_t)pname, "rx");
111         if (pz != NULL)
112             pname = (void *)(intptr_t)pz;
113 
114         opts->pzProgPath = 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