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