xref: /freebsd-src/contrib/ntp/sntp/libopts/init.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
12b15cb3dSCy Schubert /**
22b15cb3dSCy Schubert  * \file initialize.c
32b15cb3dSCy Schubert  *
42b15cb3dSCy Schubert  *  initialize the libopts data structures.
52b15cb3dSCy Schubert  *
62b15cb3dSCy Schubert  * @addtogroup autoopts
72b15cb3dSCy Schubert  * @{
82b15cb3dSCy Schubert  */
92b15cb3dSCy Schubert /*
102b15cb3dSCy Schubert  *  This file is part of AutoOpts, a companion to AutoGen.
112b15cb3dSCy Schubert  *  AutoOpts is free software.
12*a466cc55SCy Schubert  *  AutoOpts is Copyright (C) 1992-2018 by Bruce Korb - all rights reserved
132b15cb3dSCy Schubert  *
142b15cb3dSCy Schubert  *  AutoOpts is available under any one of two licenses.  The license
152b15cb3dSCy Schubert  *  in use must be one of these two and the choice is under the control
162b15cb3dSCy Schubert  *  of the user of the license.
172b15cb3dSCy Schubert  *
182b15cb3dSCy Schubert  *   The GNU Lesser General Public License, version 3 or later
192b15cb3dSCy Schubert  *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
202b15cb3dSCy Schubert  *
212b15cb3dSCy Schubert  *   The Modified Berkeley Software Distribution License
222b15cb3dSCy Schubert  *      See the file "COPYING.mbsd"
232b15cb3dSCy Schubert  *
242b15cb3dSCy Schubert  *  These files have the following sha256 sums:
252b15cb3dSCy Schubert  *
262b15cb3dSCy Schubert  *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
272b15cb3dSCy Schubert  *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
282b15cb3dSCy Schubert  *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
292b15cb3dSCy Schubert  */
302b15cb3dSCy Schubert 
312b15cb3dSCy Schubert /**
322b15cb3dSCy Schubert  *  Make sure the option descriptor is there and that we understand it.
332b15cb3dSCy Schubert  *  This should be called from any user entry point where one needs to
342b15cb3dSCy Schubert  *  worry about validity.  (Some entry points are free to assume that
352b15cb3dSCy Schubert  *  the call is not the first to the library and, thus, that this has
362b15cb3dSCy Schubert  *  already been called.)
372b15cb3dSCy Schubert  *
382b15cb3dSCy Schubert  *  Upon successful completion, pzProgName and pzProgPath are set.
392b15cb3dSCy Schubert  *
402b15cb3dSCy Schubert  *  @param[in,out] opts   program options descriptor
412b15cb3dSCy Schubert  *  @param[in]     pname  name of program, from argv[]
422b15cb3dSCy Schubert  *  @returns SUCCESS or FAILURE
432b15cb3dSCy Schubert  */
44*a466cc55SCy Schubert static tSuccess
validate_struct(tOptions * opts,char const * pname)452b15cb3dSCy Schubert validate_struct(tOptions * opts, char const * pname)
462b15cb3dSCy Schubert {
472b15cb3dSCy Schubert     if (opts == NULL) {
482b15cb3dSCy Schubert         fputs(zno_opt_arg, stderr);
492b15cb3dSCy Schubert         return FAILURE;
502b15cb3dSCy Schubert     }
512b15cb3dSCy Schubert     print_exit = ((opts->fOptSet & OPTPROC_SHELL_OUTPUT) != 0);
522b15cb3dSCy Schubert 
532b15cb3dSCy Schubert     /*
542b15cb3dSCy Schubert      *  IF the client has enabled translation and the translation procedure
552b15cb3dSCy Schubert      *  is available, then go do it.
562b15cb3dSCy Schubert      */
572b15cb3dSCy Schubert     if (  ((opts->fOptSet & OPTPROC_TRANSLATE) != 0)
582b15cb3dSCy Schubert        && (opts->pTransProc != NULL)
592b15cb3dSCy Schubert        && (option_xlateable_txt.field_ct != 0) ) {
602b15cb3dSCy Schubert         /*
612b15cb3dSCy Schubert          *  If option names are not to be translated at all, then do not do
622b15cb3dSCy Schubert          *  it for configuration parsing either.  (That is the bit that really
632b15cb3dSCy Schubert          *  gets tested anyway.)
642b15cb3dSCy Schubert          */
652b15cb3dSCy Schubert         if ((opts->fOptSet & OPTPROC_NO_XLAT_MASK) == OPTPROC_NXLAT_OPT)
662b15cb3dSCy Schubert             opts->fOptSet |= OPTPROC_NXLAT_OPT_CFG;
67276da39aSCy Schubert         opts->pTransProc();
682b15cb3dSCy Schubert     }
692b15cb3dSCy Schubert 
702b15cb3dSCy Schubert     /*
712b15cb3dSCy Schubert      *  IF the struct version is not the current, and also
722b15cb3dSCy Schubert      *     either too large (?!) or too small,
732b15cb3dSCy Schubert      *  THEN emit error message and fail-exit
742b15cb3dSCy Schubert      */
752b15cb3dSCy Schubert     if (  ( opts->structVersion  != OPTIONS_STRUCT_VERSION  )
762b15cb3dSCy Schubert        && (  (opts->structVersion > OPTIONS_STRUCT_VERSION  )
772b15cb3dSCy Schubert           || (opts->structVersion < OPTIONS_MINIMUM_VERSION )
782b15cb3dSCy Schubert        )  )  {
792b15cb3dSCy Schubert         fprintf(stderr, zwrong_ver, pname, NUM_TO_VER(opts->structVersion));
802b15cb3dSCy Schubert         if (opts->structVersion > OPTIONS_STRUCT_VERSION )
812b15cb3dSCy Schubert             fputs(ztoo_new, stderr);
822b15cb3dSCy Schubert         else
832b15cb3dSCy Schubert             fputs(ztoo_old, stderr);
842b15cb3dSCy Schubert 
852b15cb3dSCy Schubert         fwrite(ao_ver_string, sizeof(ao_ver_string) - 1, 1, stderr);
862b15cb3dSCy Schubert         return FAILURE;
872b15cb3dSCy Schubert     }
882b15cb3dSCy Schubert 
892b15cb3dSCy Schubert     /*
902b15cb3dSCy Schubert      *  If the program name hasn't been set, then set the name and the path
912b15cb3dSCy Schubert      *  and the set of equivalent characters.
922b15cb3dSCy Schubert      */
932b15cb3dSCy Schubert     if (opts->pzProgName == NULL) {
942b15cb3dSCy Schubert         char const *  pz = strrchr(pname, DIRCH);
95*a466cc55SCy Schubert         char const ** pp =
96*a466cc55SCy Schubert             (char const **)(void **)&(opts->pzProgName);
972b15cb3dSCy Schubert 
982b15cb3dSCy Schubert         if (pz != NULL)
99276da39aSCy Schubert             *pp = pz+1;
1002b15cb3dSCy Schubert         else
101276da39aSCy Schubert             *pp = pname;
1022b15cb3dSCy Schubert 
103*a466cc55SCy Schubert         pz = pathfind(getenv("PATH"), (char *)pname, "rx");
1042b15cb3dSCy Schubert         if (pz != NULL)
105276da39aSCy Schubert             pname = VOIDP(pz);
1062b15cb3dSCy Schubert 
107276da39aSCy Schubert         pp  = (char const **)VOIDP(&(opts->pzProgPath));
108276da39aSCy Schubert         *pp = pname;
1092b15cb3dSCy Schubert 
1102b15cb3dSCy Schubert         /*
1112b15cb3dSCy Schubert          *  when comparing long names, these are equivalent
1122b15cb3dSCy Schubert          */
1132b15cb3dSCy Schubert         strequate(zSepChars);
1142b15cb3dSCy Schubert     }
1152b15cb3dSCy Schubert 
1162b15cb3dSCy Schubert     return SUCCESS;
1172b15cb3dSCy Schubert }
1182b15cb3dSCy Schubert 
1192b15cb3dSCy Schubert /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1202b15cb3dSCy Schubert  *
1212b15cb3dSCy Schubert  *  DO PRESETS
1222b15cb3dSCy Schubert  *
1232b15cb3dSCy Schubert  *  The next several routines do the immediate action pass on the command
1242b15cb3dSCy Schubert  *  line options, then the environment variables, then the config files in
1252b15cb3dSCy Schubert  *  reverse order.  Once done with that, the order is reversed and all
1262b15cb3dSCy Schubert  *  the config files and environment variables are processed again, this
1272b15cb3dSCy Schubert  *  time only processing the non-immediate action options.  do_presets()
1282b15cb3dSCy Schubert  *  will then return for optionProcess() to do the final pass on the command
1292b15cb3dSCy Schubert  *  line arguments.
1302b15cb3dSCy Schubert  */
1312b15cb3dSCy Schubert 
1322b15cb3dSCy Schubert /**
1332b15cb3dSCy Schubert  *  scan the command line for immediate action options.
1342b15cb3dSCy Schubert  *  This is only called the first time through.
1352b15cb3dSCy Schubert  *  While this procedure is active, the OPTPROC_IMMEDIATE is true.
1362b15cb3dSCy Schubert  *
1372b15cb3dSCy Schubert  *  @param pOpts   program options descriptor
1382b15cb3dSCy Schubert  *  @returns SUCCESS or FAILURE
1392b15cb3dSCy Schubert  */
140*a466cc55SCy Schubert static tSuccess
immediate_opts(tOptions * opts)1412b15cb3dSCy Schubert immediate_opts(tOptions * opts)
1422b15cb3dSCy Schubert {
1432b15cb3dSCy Schubert     tSuccess  res;
1442b15cb3dSCy Schubert 
1452b15cb3dSCy Schubert     opts->fOptSet  |= OPTPROC_IMMEDIATE;
1462b15cb3dSCy Schubert     opts->curOptIdx = 1;     /* start by skipping program name */
1472b15cb3dSCy Schubert     opts->pzCurOpt  = NULL;
1482b15cb3dSCy Schubert 
1492b15cb3dSCy Schubert     /*
1502b15cb3dSCy Schubert      *  Examine all the options from the start.  We process any options that
1512b15cb3dSCy Schubert      *  are marked for immediate processing.
1522b15cb3dSCy Schubert      */
1532b15cb3dSCy Schubert     for (;;) {
1542b15cb3dSCy Schubert         tOptState opt_st = OPTSTATE_INITIALIZER(PRESET);
1552b15cb3dSCy Schubert 
1562b15cb3dSCy Schubert         res = next_opt(opts, &opt_st);
1572b15cb3dSCy Schubert         switch (res) {
1582b15cb3dSCy Schubert         case FAILURE: goto   failed_option;
1592b15cb3dSCy Schubert         case PROBLEM: res = SUCCESS; goto leave;
1602b15cb3dSCy Schubert         case SUCCESS: break;
1612b15cb3dSCy Schubert         }
1622b15cb3dSCy Schubert 
1632b15cb3dSCy Schubert         /*
1642b15cb3dSCy Schubert          *  IF this is an immediate-attribute option, then do it.
1652b15cb3dSCy Schubert          */
1662b15cb3dSCy Schubert         if (! DO_IMMEDIATELY(opt_st.flags))
1672b15cb3dSCy Schubert             continue;
1682b15cb3dSCy Schubert 
1692b15cb3dSCy Schubert         if (! SUCCESSFUL(handle_opt(opts, &opt_st)))
1702b15cb3dSCy Schubert             break;
1712b15cb3dSCy Schubert     } failed_option:;
1722b15cb3dSCy Schubert 
1732b15cb3dSCy Schubert     if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
1742b15cb3dSCy Schubert         (*opts->pUsageProc)(opts, EXIT_FAILURE);
1752b15cb3dSCy Schubert 
1762b15cb3dSCy Schubert  leave:
1772b15cb3dSCy Schubert 
1782b15cb3dSCy Schubert     opts->fOptSet &= ~OPTPROC_IMMEDIATE;
1792b15cb3dSCy Schubert     return res;
1802b15cb3dSCy Schubert }
1812b15cb3dSCy Schubert 
1822b15cb3dSCy Schubert /**
1832b15cb3dSCy Schubert  *  check for preset values from a config files or envrionment variables
1842b15cb3dSCy Schubert  *
1852b15cb3dSCy Schubert  * @param[in,out] opts  the structure with the option names to check
1862b15cb3dSCy Schubert  */
1872b15cb3dSCy Schubert static tSuccess
do_presets(tOptions * opts)1882b15cb3dSCy Schubert do_presets(tOptions * opts)
1892b15cb3dSCy Schubert {
1902b15cb3dSCy Schubert     tOptDesc * od = NULL;
1912b15cb3dSCy Schubert 
1922b15cb3dSCy Schubert     if (! SUCCESSFUL(immediate_opts(opts)))
1932b15cb3dSCy Schubert         return FAILURE;
1942b15cb3dSCy Schubert 
1952b15cb3dSCy Schubert     /*
1962b15cb3dSCy Schubert      *  IF this option set has a --save-opts option, then it also
1972b15cb3dSCy Schubert      *  has a --load-opts option.  See if a command line option has disabled
1982b15cb3dSCy Schubert      *  option presetting.
1992b15cb3dSCy Schubert      */
2002b15cb3dSCy Schubert     if (  (opts->specOptIdx.save_opts != NO_EQUIVALENT)
2012b15cb3dSCy Schubert        && (opts->specOptIdx.save_opts != 0)) {
2022b15cb3dSCy Schubert         od = opts->pOptDesc + opts->specOptIdx.save_opts + 1;
2032b15cb3dSCy Schubert         if (DISABLED_OPT(od))
2042b15cb3dSCy Schubert             return SUCCESS;
2052b15cb3dSCy Schubert     }
2062b15cb3dSCy Schubert 
2072b15cb3dSCy Schubert     /*
2082b15cb3dSCy Schubert      *  Until we return from this procedure, disable non-presettable opts
2092b15cb3dSCy Schubert      */
2102b15cb3dSCy Schubert     opts->fOptSet |= OPTPROC_PRESETTING;
2112b15cb3dSCy Schubert     /*
2122b15cb3dSCy Schubert      *  IF there are no config files,
2132b15cb3dSCy Schubert      *  THEN do any environment presets and leave.
2142b15cb3dSCy Schubert      */
2152b15cb3dSCy Schubert     if (opts->papzHomeList == NULL) {
2162b15cb3dSCy Schubert         env_presets(opts, ENV_ALL);
2172b15cb3dSCy Schubert     }
2182b15cb3dSCy Schubert     else {
2192b15cb3dSCy Schubert         env_presets(opts, ENV_IMM);
2202b15cb3dSCy Schubert 
2212b15cb3dSCy Schubert         /*
2222b15cb3dSCy Schubert          *  Check to see if environment variables have disabled presetting.
2232b15cb3dSCy Schubert          */
2242b15cb3dSCy Schubert         if ((od != NULL) && ! DISABLED_OPT(od))
2252b15cb3dSCy Schubert             intern_file_load(opts);
2262b15cb3dSCy Schubert 
2272b15cb3dSCy Schubert         /*
2282b15cb3dSCy Schubert          *  ${PROGRAM_LOAD_OPTS} value of "no" cannot disable other environment
2292b15cb3dSCy Schubert          *  variable options.  Only the loading of .rc files.
2302b15cb3dSCy Schubert          */
2312b15cb3dSCy Schubert         env_presets(opts, ENV_NON_IMM);
2322b15cb3dSCy Schubert     }
2332b15cb3dSCy Schubert     opts->fOptSet &= ~OPTPROC_PRESETTING;
2342b15cb3dSCy Schubert 
2352b15cb3dSCy Schubert     return SUCCESS;
2362b15cb3dSCy Schubert }
2372b15cb3dSCy Schubert 
2382b15cb3dSCy Schubert /**
2392b15cb3dSCy Schubert  * AutoOpts initialization
2402b15cb3dSCy Schubert  *
2412b15cb3dSCy Schubert  * @param[in,out] opts  the structure to initialize
2422b15cb3dSCy Schubert  * @param[in]     a_ct  program argument count
2432b15cb3dSCy Schubert  * @param[in]     a_v   program argument vector
2442b15cb3dSCy Schubert  */
245*a466cc55SCy Schubert static bool
ao_initialize(tOptions * opts,int a_ct,char ** a_v)2462b15cb3dSCy Schubert ao_initialize(tOptions * opts, int a_ct, char ** a_v)
2472b15cb3dSCy Schubert {
2482b15cb3dSCy Schubert     if ((opts->fOptSet & OPTPROC_INITDONE) != 0)
2492b15cb3dSCy Schubert         return true;
2502b15cb3dSCy Schubert 
2512b15cb3dSCy Schubert     opts->origArgCt   = (unsigned int)a_ct;
2522b15cb3dSCy Schubert     opts->origArgVect = a_v;
2532b15cb3dSCy Schubert     opts->fOptSet    |= OPTPROC_INITDONE;
2542b15cb3dSCy Schubert 
2552b15cb3dSCy Schubert     if (HAS_pzPkgDataDir(opts))
2562b15cb3dSCy Schubert         program_pkgdatadir = opts->pzPkgDataDir;
2572b15cb3dSCy Schubert 
2582b15cb3dSCy Schubert     if (! SUCCESSFUL(do_presets(opts)))
2592b15cb3dSCy Schubert         return false;
2602b15cb3dSCy Schubert 
2612b15cb3dSCy Schubert     /*
2622b15cb3dSCy Schubert      *  IF option name conversion was suppressed but it is not suppressed
2632b15cb3dSCy Schubert      *  for the command line, then it's time to translate option names.
2642b15cb3dSCy Schubert      *  Usage text will not get retranslated.
2652b15cb3dSCy Schubert      */
2662b15cb3dSCy Schubert     if (  ((opts->fOptSet & OPTPROC_TRANSLATE) != 0)
2672b15cb3dSCy Schubert        && (opts->pTransProc != NULL)
2682b15cb3dSCy Schubert        && ((opts->fOptSet & OPTPROC_NO_XLAT_MASK) == OPTPROC_NXLAT_OPT_CFG)
2692b15cb3dSCy Schubert        )  {
2702b15cb3dSCy Schubert         opts->fOptSet &= ~OPTPROC_NXLAT_OPT_CFG;
2712b15cb3dSCy Schubert         (*opts->pTransProc)();
2722b15cb3dSCy Schubert     }
2732b15cb3dSCy Schubert 
2742b15cb3dSCy Schubert     if ((opts->fOptSet & OPTPROC_REORDER) != 0)
2752b15cb3dSCy Schubert         optionSort(opts);
2762b15cb3dSCy Schubert 
2772b15cb3dSCy Schubert     opts->curOptIdx   = 1;
2782b15cb3dSCy Schubert     opts->pzCurOpt    = NULL;
2792b15cb3dSCy Schubert     return true;
2802b15cb3dSCy Schubert }
2812b15cb3dSCy Schubert 
2822b15cb3dSCy Schubert /** @}
2832b15cb3dSCy Schubert  *
2842b15cb3dSCy Schubert  * Local Variables:
2852b15cb3dSCy Schubert  * mode: C
2862b15cb3dSCy Schubert  * c-file-style: "stroustrup"
2872b15cb3dSCy Schubert  * indent-tabs-mode: nil
2882b15cb3dSCy Schubert  * End:
2892b15cb3dSCy Schubert  * end of autoopts/initialize.c */
290