1
0

init.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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-2018 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. * Make sure the option descriptor is there and that we understand it.
  32. * This should be called from any user entry point where one needs to
  33. * worry about validity. (Some entry points are free to assume that
  34. * the call is not the first to the library and, thus, that this has
  35. * already been called.)
  36. *
  37. * Upon successful completion, pzProgName and pzProgPath are set.
  38. *
  39. * @param[in,out] opts program options descriptor
  40. * @param[in] pname name of program, from argv[]
  41. * @returns SUCCESS or FAILURE
  42. */
  43. static tSuccess
  44. validate_struct(tOptions * opts, char const * pname)
  45. {
  46. if (opts == NULL) {
  47. fputs(zno_opt_arg, stderr);
  48. return FAILURE;
  49. }
  50. print_exit = ((opts->fOptSet & OPTPROC_SHELL_OUTPUT) != 0);
  51. /*
  52. * IF the client has enabled translation and the translation procedure
  53. * is available, then go do it.
  54. */
  55. if ( ((opts->fOptSet & OPTPROC_TRANSLATE) != 0)
  56. && (opts->pTransProc != NULL)
  57. && (option_xlateable_txt.field_ct != 0) ) {
  58. /*
  59. * If option names are not to be translated at all, then do not do
  60. * it for configuration parsing either. (That is the bit that really
  61. * gets tested anyway.)
  62. */
  63. if ((opts->fOptSet & OPTPROC_NO_XLAT_MASK) == OPTPROC_NXLAT_OPT)
  64. opts->fOptSet |= OPTPROC_NXLAT_OPT_CFG;
  65. opts->pTransProc();
  66. }
  67. /*
  68. * IF the struct version is not the current, and also
  69. * either too large (?!) or too small,
  70. * THEN emit error message and fail-exit
  71. */
  72. if ( ( opts->structVersion != OPTIONS_STRUCT_VERSION )
  73. && ( (opts->structVersion > OPTIONS_STRUCT_VERSION )
  74. || (opts->structVersion < OPTIONS_MINIMUM_VERSION )
  75. ) ) {
  76. fprintf(stderr, zwrong_ver, pname, NUM_TO_VER(opts->structVersion));
  77. if (opts->structVersion > OPTIONS_STRUCT_VERSION )
  78. fputs(ztoo_new, stderr);
  79. else
  80. fputs(ztoo_old, stderr);
  81. fwrite(ao_ver_string, sizeof(ao_ver_string) - 1, 1, stderr);
  82. return FAILURE;
  83. }
  84. /*
  85. * If the program name hasn't been set, then set the name and the path
  86. * and the set of equivalent characters.
  87. */
  88. if (opts->pzProgName == NULL) {
  89. char const * pz = strrchr(pname, DIRCH);
  90. char const ** pp =
  91. (char const **)(void **)&(opts->pzProgName);
  92. if (pz != NULL)
  93. *pp = pz+1;
  94. else
  95. *pp = pname;
  96. pz = pathfind(getenv("PATH"), (char *)pname, "rx");
  97. if (pz != NULL)
  98. pname = VOIDP(pz);
  99. pp = (char const **)VOIDP(&(opts->pzProgPath));
  100. *pp = pname;
  101. /*
  102. * when comparing long names, these are equivalent
  103. */
  104. strequate(zSepChars);
  105. }
  106. return SUCCESS;
  107. }
  108. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  109. *
  110. * DO PRESETS
  111. *
  112. * The next several routines do the immediate action pass on the command
  113. * line options, then the environment variables, then the config files in
  114. * reverse order. Once done with that, the order is reversed and all
  115. * the config files and environment variables are processed again, this
  116. * time only processing the non-immediate action options. do_presets()
  117. * will then return for optionProcess() to do the final pass on the command
  118. * line arguments.
  119. */
  120. /**
  121. * scan the command line for immediate action options.
  122. * This is only called the first time through.
  123. * While this procedure is active, the OPTPROC_IMMEDIATE is true.
  124. *
  125. * @param pOpts program options descriptor
  126. * @returns SUCCESS or FAILURE
  127. */
  128. static tSuccess
  129. immediate_opts(tOptions * opts)
  130. {
  131. tSuccess res;
  132. opts->fOptSet |= OPTPROC_IMMEDIATE;
  133. opts->curOptIdx = 1; /* start by skipping program name */
  134. opts->pzCurOpt = NULL;
  135. /*
  136. * Examine all the options from the start. We process any options that
  137. * are marked for immediate processing.
  138. */
  139. for (;;) {
  140. tOptState opt_st = OPTSTATE_INITIALIZER(PRESET);
  141. res = next_opt(opts, &opt_st);
  142. switch (res) {
  143. case FAILURE: goto failed_option;
  144. case PROBLEM: res = SUCCESS; goto leave;
  145. case SUCCESS: break;
  146. }
  147. /*
  148. * IF this is an immediate-attribute option, then do it.
  149. */
  150. if (! DO_IMMEDIATELY(opt_st.flags))
  151. continue;
  152. if (! SUCCESSFUL(handle_opt(opts, &opt_st)))
  153. break;
  154. } failed_option:;
  155. if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
  156. (*opts->pUsageProc)(opts, EXIT_FAILURE);
  157. leave:
  158. opts->fOptSet &= ~OPTPROC_IMMEDIATE;
  159. return res;
  160. }
  161. /**
  162. * check for preset values from a config files or envrionment variables
  163. *
  164. * @param[in,out] opts the structure with the option names to check
  165. */
  166. static tSuccess
  167. do_presets(tOptions * opts)
  168. {
  169. tOptDesc * od = NULL;
  170. if (! SUCCESSFUL(immediate_opts(opts)))
  171. return FAILURE;
  172. /*
  173. * IF this option set has a --save-opts option, then it also
  174. * has a --load-opts option. See if a command line option has disabled
  175. * option presetting.
  176. */
  177. if ( (opts->specOptIdx.save_opts != NO_EQUIVALENT)
  178. && (opts->specOptIdx.save_opts != 0)) {
  179. od = opts->pOptDesc + opts->specOptIdx.save_opts + 1;
  180. if (DISABLED_OPT(od))
  181. return SUCCESS;
  182. }
  183. /*
  184. * Until we return from this procedure, disable non-presettable opts
  185. */
  186. opts->fOptSet |= OPTPROC_PRESETTING;
  187. /*
  188. * IF there are no config files,
  189. * THEN do any environment presets and leave.
  190. */
  191. if (opts->papzHomeList == NULL) {
  192. env_presets(opts, ENV_ALL);
  193. }
  194. else {
  195. env_presets(opts, ENV_IMM);
  196. /*
  197. * Check to see if environment variables have disabled presetting.
  198. */
  199. if ((od != NULL) && ! DISABLED_OPT(od))
  200. intern_file_load(opts);
  201. /*
  202. * ${PROGRAM_LOAD_OPTS} value of "no" cannot disable other environment
  203. * variable options. Only the loading of .rc files.
  204. */
  205. env_presets(opts, ENV_NON_IMM);
  206. }
  207. opts->fOptSet &= ~OPTPROC_PRESETTING;
  208. return SUCCESS;
  209. }
  210. /**
  211. * AutoOpts initialization
  212. *
  213. * @param[in,out] opts the structure to initialize
  214. * @param[in] a_ct program argument count
  215. * @param[in] a_v program argument vector
  216. */
  217. static bool
  218. ao_initialize(tOptions * opts, int a_ct, char ** a_v)
  219. {
  220. if ((opts->fOptSet & OPTPROC_INITDONE) != 0)
  221. return true;
  222. opts->origArgCt = (unsigned int)a_ct;
  223. opts->origArgVect = a_v;
  224. opts->fOptSet |= OPTPROC_INITDONE;
  225. if (HAS_pzPkgDataDir(opts))
  226. program_pkgdatadir = opts->pzPkgDataDir;
  227. if (! SUCCESSFUL(do_presets(opts)))
  228. return false;
  229. /*
  230. * IF option name conversion was suppressed but it is not suppressed
  231. * for the command line, then it's time to translate option names.
  232. * Usage text will not get retranslated.
  233. */
  234. if ( ((opts->fOptSet & OPTPROC_TRANSLATE) != 0)
  235. && (opts->pTransProc != NULL)
  236. && ((opts->fOptSet & OPTPROC_NO_XLAT_MASK) == OPTPROC_NXLAT_OPT_CFG)
  237. ) {
  238. opts->fOptSet &= ~OPTPROC_NXLAT_OPT_CFG;
  239. (*opts->pTransProc)();
  240. }
  241. if ((opts->fOptSet & OPTPROC_REORDER) != 0)
  242. optionSort(opts);
  243. opts->curOptIdx = 1;
  244. opts->pzCurOpt = NULL;
  245. return true;
  246. }
  247. /** @}
  248. *
  249. * Local Variables:
  250. * mode: C
  251. * c-file-style: "stroustrup"
  252. * indent-tabs-mode: nil
  253. * End:
  254. * end of autoopts/initialize.c */