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