|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* \file initialize.c
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* initialize the libopts data structures.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* @addtogroup autoopts
|
|
Packit Service |
4684c1 |
* @{
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* This file is part of AutoOpts, a companion to AutoGen.
|
|
Packit Service |
4684c1 |
* AutoOpts is free software.
|
|
Packit Service |
4684c1 |
* AutoOpts is Copyright (C) 1992-2018 by Bruce Korb - all rights reserved
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* AutoOpts is available under any one of two licenses. The license
|
|
Packit Service |
4684c1 |
* in use must be one of these two and the choice is under the control
|
|
Packit Service |
4684c1 |
* of the user of the license.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* The GNU Lesser General Public License, version 3 or later
|
|
Packit Service |
4684c1 |
* See the files "COPYING.lgplv3" and "COPYING.gplv3"
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* The Modified Berkeley Software Distribution License
|
|
Packit Service |
4684c1 |
* See the file "COPYING.mbsd"
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* These files have the following sha256 sums:
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
|
|
Packit Service |
4684c1 |
* 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
|
|
Packit Service |
4684c1 |
* 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* Make sure the option descriptor is there and that we understand it.
|
|
Packit Service |
4684c1 |
* This should be called from any user entry point where one needs to
|
|
Packit Service |
4684c1 |
* worry about validity. (Some entry points are free to assume that
|
|
Packit Service |
4684c1 |
* the call is not the first to the library and, thus, that this has
|
|
Packit Service |
4684c1 |
* already been called.)
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Upon successful completion, pzProgName and pzProgPath are set.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* @param[in,out] opts program options descriptor
|
|
Packit Service |
4684c1 |
* @param[in] pname name of program, from argv[]
|
|
Packit Service |
4684c1 |
* @returns SUCCESS or FAILURE
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
static tSuccess
|
|
Packit Service |
4684c1 |
validate_struct(tOptions * opts, char const * pname)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
if (opts == NULL) {
|
|
Packit Service |
4684c1 |
fputs(zno_opt_arg, stderr);
|
|
Packit Service |
4684c1 |
return FAILURE;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
print_exit = ((opts->fOptSet & OPTPROC_SHELL_OUTPUT) != 0);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* IF the client has enabled translation and the translation procedure
|
|
Packit Service |
4684c1 |
* is available, then go do it.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
if ( ((opts->fOptSet & OPTPROC_TRANSLATE) != 0)
|
|
Packit Service |
4684c1 |
&& (opts->pTransProc != NULL)
|
|
Packit Service |
4684c1 |
&& (option_xlateable_txt.field_ct != 0) ) {
|
|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* If option names are not to be translated at all, then do not do
|
|
Packit Service |
4684c1 |
* it for configuration parsing either. (That is the bit that really
|
|
Packit Service |
4684c1 |
* gets tested anyway.)
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
if ((opts->fOptSet & OPTPROC_NO_XLAT_MASK) == OPTPROC_NXLAT_OPT)
|
|
Packit Service |
4684c1 |
opts->fOptSet |= OPTPROC_NXLAT_OPT_CFG;
|
|
Packit Service |
4684c1 |
opts->pTransProc();
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* IF the struct version is not the current, and also
|
|
Packit Service |
4684c1 |
* either too large (?!) or too small,
|
|
Packit Service |
4684c1 |
* THEN emit error message and fail-exit
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
if ( ( opts->structVersion != OPTIONS_STRUCT_VERSION )
|
|
Packit Service |
4684c1 |
&& ( (opts->structVersion > OPTIONS_STRUCT_VERSION )
|
|
Packit Service |
4684c1 |
|| (opts->structVersion < OPTIONS_MINIMUM_VERSION )
|
|
Packit Service |
4684c1 |
) ) {
|
|
Packit Service |
4684c1 |
fprintf(stderr, zwrong_ver, pname, NUM_TO_VER(opts->structVersion));
|
|
Packit Service |
4684c1 |
if (opts->structVersion > OPTIONS_STRUCT_VERSION )
|
|
Packit Service |
4684c1 |
fputs(ztoo_new, stderr);
|
|
Packit Service |
4684c1 |
else
|
|
Packit Service |
4684c1 |
fputs(ztoo_old, stderr);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
fwrite(ao_ver_string, sizeof(ao_ver_string) - 1, 1, stderr);
|
|
Packit Service |
4684c1 |
return FAILURE;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* If the program name hasn't been set, then set the name and the path
|
|
Packit Service |
4684c1 |
* and the set of equivalent characters.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
if (opts->pzProgName == NULL) {
|
|
Packit Service |
4684c1 |
char const * pz = strrchr(pname, DIRCH);
|
|
Packit Service |
4684c1 |
char const ** pp =
|
|
Packit Service |
4684c1 |
(char const **)(void **)&(opts->pzProgName);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (pz != NULL)
|
|
Packit Service |
4684c1 |
*pp = pz+1;
|
|
Packit Service |
4684c1 |
else
|
|
Packit Service |
4684c1 |
*pp = pname;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
pz = pathfind(getenv("PATH"), (char *)pname, "rx");
|
|
Packit Service |
4684c1 |
if (pz != NULL)
|
|
Packit Service |
4684c1 |
pname = VOIDP(pz);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
pp = (char const **)VOIDP(&(opts->pzProgPath));
|
|
Packit Service |
4684c1 |
*pp = pname;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* when comparing long names, these are equivalent
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
strequate(zSepChars);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return SUCCESS;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* DO PRESETS
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* The next several routines do the immediate action pass on the command
|
|
Packit Service |
4684c1 |
* line options, then the environment variables, then the config files in
|
|
Packit Service |
4684c1 |
* reverse order. Once done with that, the order is reversed and all
|
|
Packit Service |
4684c1 |
* the config files and environment variables are processed again, this
|
|
Packit Service |
4684c1 |
* time only processing the non-immediate action options. do_presets()
|
|
Packit Service |
4684c1 |
* will then return for optionProcess() to do the final pass on the command
|
|
Packit Service |
4684c1 |
* line arguments.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* scan the command line for immediate action options.
|
|
Packit Service |
4684c1 |
* This is only called the first time through.
|
|
Packit Service |
4684c1 |
* While this procedure is active, the OPTPROC_IMMEDIATE is true.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* @param pOpts program options descriptor
|
|
Packit Service |
4684c1 |
* @returns SUCCESS or FAILURE
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
static tSuccess
|
|
Packit Service |
4684c1 |
immediate_opts(tOptions * opts)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
tSuccess res;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
opts->fOptSet |= OPTPROC_IMMEDIATE;
|
|
Packit Service |
4684c1 |
opts->curOptIdx = 1; /* start by skipping program name */
|
|
Packit Service |
4684c1 |
opts->pzCurOpt = NULL;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* Examine all the options from the start. We process any options that
|
|
Packit Service |
4684c1 |
* are marked for immediate processing.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
for (;;) {
|
|
Packit Service |
4684c1 |
tOptState opt_st = OPTSTATE_INITIALIZER(PRESET);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
res = next_opt(opts, &opt_st);
|
|
Packit Service |
4684c1 |
switch (res) {
|
|
Packit Service |
4684c1 |
case FAILURE: goto failed_option;
|
|
Packit Service |
4684c1 |
case PROBLEM: res = SUCCESS; goto leave;
|
|
Packit Service |
4684c1 |
case SUCCESS: break;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* IF this is an immediate-attribute option, then do it.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
if (! DO_IMMEDIATELY(opt_st.flags))
|
|
Packit Service |
4684c1 |
continue;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (! SUCCESSFUL(handle_opt(opts, &opt_st)))
|
|
Packit Service |
4684c1 |
break;
|
|
Packit Service |
4684c1 |
} failed_option:;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
|
|
Packit Service |
4684c1 |
(*opts->pUsageProc)(opts, EXIT_FAILURE);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
leave:
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
opts->fOptSet &= ~OPTPROC_IMMEDIATE;
|
|
Packit Service |
4684c1 |
return res;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* check for preset values from a config files or envrionment variables
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* @param[in,out] opts the structure with the option names to check
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
static tSuccess
|
|
Packit Service |
4684c1 |
do_presets(tOptions * opts)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
tOptDesc * od = NULL;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (! SUCCESSFUL(immediate_opts(opts)))
|
|
Packit Service |
4684c1 |
return FAILURE;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* IF this option set has a --save-opts option, then it also
|
|
Packit Service |
4684c1 |
* has a --load-opts option. See if a command line option has disabled
|
|
Packit Service |
4684c1 |
* option presetting.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
if ( (opts->specOptIdx.save_opts != NO_EQUIVALENT)
|
|
Packit Service |
4684c1 |
&& (opts->specOptIdx.save_opts != 0)) {
|
|
Packit Service |
4684c1 |
od = opts->pOptDesc + opts->specOptIdx.save_opts + 1;
|
|
Packit Service |
4684c1 |
if (DISABLED_OPT(od))
|
|
Packit Service |
4684c1 |
return SUCCESS;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* Until we return from this procedure, disable non-presettable opts
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
opts->fOptSet |= OPTPROC_PRESETTING;
|
|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* IF there are no config files,
|
|
Packit Service |
4684c1 |
* THEN do any environment presets and leave.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
if (opts->papzHomeList == NULL) {
|
|
Packit Service |
4684c1 |
env_presets(opts, ENV_ALL);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
else {
|
|
Packit Service |
4684c1 |
env_presets(opts, ENV_IMM);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* Check to see if environment variables have disabled presetting.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
if ((od != NULL) && ! DISABLED_OPT(od))
|
|
Packit Service |
4684c1 |
intern_file_load(opts);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* ${PROGRAM_LOAD_OPTS} value of "no" cannot disable other environment
|
|
Packit Service |
4684c1 |
* variable options. Only the loading of .rc files.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
env_presets(opts, ENV_NON_IMM);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
opts->fOptSet &= ~OPTPROC_PRESETTING;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return SUCCESS;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* AutoOpts initialization
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* @param[in,out] opts the structure to initialize
|
|
Packit Service |
4684c1 |
* @param[in] a_ct program argument count
|
|
Packit Service |
4684c1 |
* @param[in] a_v program argument vector
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
static bool
|
|
Packit Service |
4684c1 |
ao_initialize(tOptions * opts, int a_ct, char ** a_v)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
if ((opts->fOptSet & OPTPROC_INITDONE) != 0)
|
|
Packit Service |
4684c1 |
return true;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
opts->origArgCt = (unsigned int)a_ct;
|
|
Packit Service |
4684c1 |
opts->origArgVect = a_v;
|
|
Packit Service |
4684c1 |
opts->fOptSet |= OPTPROC_INITDONE;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (HAS_pzPkgDataDir(opts))
|
|
Packit Service |
4684c1 |
program_pkgdatadir = opts->pzPkgDataDir;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (! SUCCESSFUL(do_presets(opts)))
|
|
Packit Service |
4684c1 |
return false;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* IF option name conversion was suppressed but it is not suppressed
|
|
Packit Service |
4684c1 |
* for the command line, then it's time to translate option names.
|
|
Packit Service |
4684c1 |
* Usage text will not get retranslated.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
if ( ((opts->fOptSet & OPTPROC_TRANSLATE) != 0)
|
|
Packit Service |
4684c1 |
&& (opts->pTransProc != NULL)
|
|
Packit Service |
4684c1 |
&& ((opts->fOptSet & OPTPROC_NO_XLAT_MASK) == OPTPROC_NXLAT_OPT_CFG)
|
|
Packit Service |
4684c1 |
) {
|
|
Packit Service |
4684c1 |
opts->fOptSet &= ~OPTPROC_NXLAT_OPT_CFG;
|
|
Packit Service |
4684c1 |
(*opts->pTransProc)();
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if ((opts->fOptSet & OPTPROC_REORDER) != 0)
|
|
Packit Service |
4684c1 |
optionSort(opts);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
opts->curOptIdx = 1;
|
|
Packit Service |
4684c1 |
opts->pzCurOpt = NULL;
|
|
Packit Service |
4684c1 |
return true;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/** @}
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Local Variables:
|
|
Packit Service |
4684c1 |
* mode: C
|
|
Packit Service |
4684c1 |
* c-file-style: "stroustrup"
|
|
Packit Service |
4684c1 |
* indent-tabs-mode: nil
|
|
Packit Service |
4684c1 |
* End:
|
|
Packit Service |
4684c1 |
* end of autoopts/initialize.c */
|