1
0

autoopts.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /**
  2. * \file autoopts.c
  3. *
  4. * This file contains all of the routines that must be linked into
  5. * an executable to use the generated option processing. The optional
  6. * routines are in separately compiled modules so that they will not
  7. * necessarily be linked in.
  8. *
  9. * @addtogroup autoopts
  10. * @{
  11. */
  12. /*
  13. * This file is part of AutoOpts, a companion to AutoGen.
  14. * AutoOpts is free software.
  15. * AutoOpts is Copyright (C) 1992-2018 by Bruce Korb - all rights reserved
  16. *
  17. * AutoOpts is available under any one of two licenses. The license
  18. * in use must be one of these two and the choice is under the control
  19. * of the user of the license.
  20. *
  21. * The GNU Lesser General Public License, version 3 or later
  22. * See the files "COPYING.lgplv3" and "COPYING.gplv3"
  23. *
  24. * The Modified Berkeley Software Distribution License
  25. * See the file "COPYING.mbsd"
  26. *
  27. * These files have the following sha256 sums:
  28. *
  29. * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
  30. * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
  31. * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
  32. */
  33. /**
  34. * The number of tab characters to skip when printing continuation lines.
  35. */
  36. static unsigned int tab_skip_ct = 0;
  37. #ifndef HAVE_PATHFIND
  38. # define pathfind(_p, _n, _m) option_pathfind(_p, _n, _m)
  39. # include "compat/pathfind.c"
  40. #endif
  41. #ifndef HAVE_SNPRINTF
  42. # define vsnprintf option_vsnprintf
  43. # define snprintf option_snprintf
  44. # include "compat/snprintf.c"
  45. #endif
  46. #ifndef HAVE_STRDUP
  47. # define strdup(_s) option_strdup(_s)
  48. # include "compat/strdup.c"
  49. #endif
  50. #ifndef HAVE_STRCHR
  51. # define strrchr(_s, _c) option_strrchr(_s, _c)
  52. # define strchr(_s, _c) option_strchr(_s, _c)
  53. # include "compat/strchr.c"
  54. #endif
  55. static void *
  56. ao_malloc(size_t sz)
  57. {
  58. void * res = malloc(sz);
  59. if (res == NULL) {
  60. fprintf(stderr, zalloc_fail, (int)sz);
  61. option_exits(EXIT_FAILURE);
  62. }
  63. return res;
  64. }
  65. static void *
  66. ao_realloc(void *p, size_t sz)
  67. {
  68. void * res = (p == NULL) ? malloc(sz) : realloc(p, sz);
  69. if (res == NULL) {
  70. fprintf(stderr, zrealloc_fail, (int)sz, p);
  71. option_exits(EXIT_FAILURE);
  72. }
  73. return res;
  74. }
  75. static char *
  76. ao_strdup(char const *str)
  77. {
  78. char * res = strdup(str);
  79. if (res == NULL) {
  80. fprintf(stderr, zalloc_fail, (int)strlen(str));
  81. option_exits(EXIT_FAILURE);
  82. }
  83. return res;
  84. }
  85. /**
  86. * handle an option.
  87. *
  88. * This routine handles equivalencing, sets the option state flags and
  89. * invokes the handler procedure, if any.
  90. */
  91. static tSuccess
  92. handle_opt(tOptions * opts, tOptState * o_st)
  93. {
  94. /*
  95. * Save a copy of the option procedure pointer.
  96. * If this is an equivalence class option, we still want this proc.
  97. */
  98. tOptDesc * od = o_st->pOD;
  99. tOptProc * opt_proc = od->pOptProc;
  100. if (od->fOptState & OPTST_ALLOC_ARG)
  101. AGFREE(od->optArg.argString);
  102. od->optArg.argString = o_st->pzOptArg;
  103. /*
  104. * IF we are presetting options, then we will ignore any un-presettable
  105. * options. They are the ones either marked as such.
  106. */
  107. if ( ((opts->fOptSet & OPTPROC_PRESETTING) != 0)
  108. && ((od->fOptState & OPTST_NO_INIT) != 0)
  109. )
  110. return PROBLEM;
  111. /*
  112. * IF this is an equivalence class option,
  113. * THEN
  114. * Save the option value that got us to this option
  115. * entry. (It may not be od->optChar[0], if this is an
  116. * equivalence entry.)
  117. * set the pointer to the equivalence class base
  118. */
  119. if (od->optEquivIndex != NO_EQUIVALENT) {
  120. tOptDesc * eqv_od = opts->pOptDesc + od->optEquivIndex;
  121. /*
  122. * IF the current option state has not been defined (set on the
  123. * command line), THEN we will allow continued resetting of
  124. * the value. Once "defined", then it must not change.
  125. */
  126. if ((od->fOptState & OPTST_DEFINED) != 0) {
  127. /*
  128. * The equivalenced-to option has been found on the command
  129. * line before. Make sure new occurrences are the same type.
  130. *
  131. * IF this option has been previously equivalenced and
  132. * it was not the same equivalenced-to option,
  133. * THEN we have a usage problem.
  134. */
  135. if (eqv_od->optActualIndex != od->optIndex) {
  136. fprintf(stderr, zmultiway_bug, eqv_od->pz_Name, od->pz_Name,
  137. (opts->pOptDesc + eqv_od->optActualIndex)->pz_Name);
  138. return FAILURE;
  139. }
  140. } else {
  141. /*
  142. * Set the equivalenced-to actual option index to no-equivalent
  143. * so that we set all the entries below. This option may either
  144. * never have been selected before, or else it was selected by
  145. * some sort of "presetting" mechanism.
  146. */
  147. eqv_od->optActualIndex = NO_EQUIVALENT;
  148. }
  149. if (eqv_od->optActualIndex != od->optIndex) {
  150. /*
  151. * First time through, copy over the state
  152. * and add in the equivalence flag
  153. */
  154. eqv_od->optActualValue = od->optValue;
  155. eqv_od->optActualIndex = od->optIndex;
  156. o_st->flags |= OPTST_EQUIVALENCE;
  157. }
  158. /*
  159. * Copy the most recent option argument. set membership state
  160. * is kept in 'eqv_od->optCookie'. Do not overwrite.
  161. */
  162. eqv_od->optArg.argString = od->optArg.argString;
  163. od = eqv_od;
  164. } else {
  165. od->optActualValue = od->optValue;
  166. od->optActualIndex = od->optIndex;
  167. }
  168. od->fOptState &= OPTST_PERSISTENT_MASK;
  169. od->fOptState |= (o_st->flags & ~OPTST_PERSISTENT_MASK);
  170. /*
  171. * Keep track of count only for DEFINED (command line) options.
  172. * IF we have too many, build up an error message and bail.
  173. */
  174. if ( (od->fOptState & OPTST_DEFINED)
  175. && (++od->optOccCt > od->optMaxCt) )
  176. return too_many_occurrences(opts, od);
  177. /*
  178. * If provided a procedure to call, call it
  179. */
  180. if (opt_proc != NULL)
  181. (*opt_proc)(opts, od);
  182. return SUCCESS;
  183. }
  184. /**
  185. * Find the option descriptor and option argument (if any) for the
  186. * next command line argument. DO NOT modify the descriptor. Put
  187. * all the state in the state argument so that the option can be skipped
  188. * without consequence (side effect).
  189. *
  190. * @param opts the program option descriptor
  191. * @param o_st the state of the next found option
  192. */
  193. static tSuccess
  194. next_opt(tOptions * opts, tOptState * o_st)
  195. {
  196. {
  197. tSuccess res = find_opt(opts, o_st);
  198. if (! SUCCESSFUL(res))
  199. return res;
  200. }
  201. if ( ((o_st->flags & OPTST_DEFINED) != 0)
  202. && ((o_st->pOD->fOptState & OPTST_NO_COMMAND) != 0)) {
  203. fprintf(stderr, zNotCmdOpt, o_st->pOD->pz_Name);
  204. return FAILURE;
  205. }
  206. return get_opt_arg(opts, o_st);
  207. }
  208. /**
  209. * Process all the options from our current position onward. (This allows
  210. * interspersed options and arguments for the few non-standard programs that
  211. * require it.) Thus, do not rewind option indexes because some programs
  212. * choose to re-invoke after a non-option.
  213. *
  214. * @param[in,out] opts program options descriptor
  215. * @returns SUCCESS or FAILURE
  216. */
  217. static tSuccess
  218. regular_opts(tOptions * opts)
  219. {
  220. /* assert: opts->fOptSet & OPTPROC_IMMEDIATE == 0 */
  221. for (;;) {
  222. tOptState opt_st = OPTSTATE_INITIALIZER(DEFINED);
  223. switch (next_opt(opts, &opt_st)) {
  224. case FAILURE: goto failed_option;
  225. case PROBLEM: return SUCCESS; /* no more args */
  226. case SUCCESS: break;
  227. }
  228. /*
  229. * IF this is an immediate action option,
  230. * THEN skip it (unless we are supposed to do it a second time).
  231. */
  232. if (! DO_NORMALLY(opt_st.flags)) {
  233. if (! DO_SECOND_TIME(opt_st.flags))
  234. continue;
  235. opt_st.pOD->optOccCt--; /* don't count this repetition */
  236. }
  237. if (! SUCCESSFUL(handle_opt(opts, &opt_st)))
  238. break;
  239. } failed_option:;
  240. if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
  241. (*opts->pUsageProc)(opts, EXIT_FAILURE);
  242. return FAILURE;
  243. }
  244. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  245. *
  246. * THESE ROUTINES ARE CALLABLE FROM THE GENERATED OPTION PROCESSING CODE
  247. */
  248. /*=--subblock=arg=arg_type,arg_name,arg_desc =*/
  249. /*=*
  250. * library: opts
  251. * header: your-opts.h
  252. *
  253. * lib_description:
  254. *
  255. * These are the routines that libopts users may call directly from their
  256. * code. There are several other routines that can be called by code
  257. * generated by the libopts option templates, but they are not to be
  258. * called from any other user code. The @file{options.h} header is
  259. * fairly clear about this, too.
  260. =*/
  261. /*=export_func optionProcess
  262. *
  263. * what: this is the main option processing routine
  264. *
  265. * arg: + tOptions * + opts + program options descriptor +
  266. * arg: + int + a_ct + program arg count +
  267. * arg: + char ** + a_v + program arg vector +
  268. *
  269. * ret_type: int
  270. * ret_desc: the count of the arguments processed
  271. *
  272. * doc:
  273. *
  274. * This is the main entry point for processing options. It is intended
  275. * that this procedure be called once at the beginning of the execution of
  276. * a program. Depending on options selected earlier, it is sometimes
  277. * necessary to stop and restart option processing, or to select completely
  278. * different sets of options. This can be done easily, but you generally
  279. * do not want to do this.
  280. *
  281. * The number of arguments processed always includes the program name.
  282. * If one of the arguments is "--", then it is counted and the processing
  283. * stops. If an error was encountered and errors are to be tolerated, then
  284. * the returned value is the index of the argument causing the error.
  285. * A hyphen by itself ("-") will also cause processing to stop and will
  286. * @emph{not} be counted among the processed arguments. A hyphen by itself
  287. * is treated as an operand. Encountering an operand stops option
  288. * processing.
  289. *
  290. * err: Errors will cause diagnostics to be printed. @code{exit(3)} may
  291. * or may not be called. It depends upon whether or not the options
  292. * were generated with the "allow-errors" attribute, or if the
  293. * ERRSKIP_OPTERR or ERRSTOP_OPTERR macros were invoked.
  294. =*/
  295. int
  296. optionProcess(tOptions * opts, int a_ct, char ** a_v)
  297. {
  298. if (! SUCCESSFUL(validate_struct(opts, a_v[0])))
  299. ao_bug(zbad_data_msg);
  300. /*
  301. * Establish the real program name, the program full path,
  302. * and do all the presetting the first time thru only.
  303. */
  304. if (! ao_initialize(opts, a_ct, a_v))
  305. return 0;
  306. /*
  307. * IF we are (re)starting,
  308. * THEN reset option location
  309. */
  310. if (opts->curOptIdx <= 0) {
  311. opts->curOptIdx = 1;
  312. opts->pzCurOpt = NULL;
  313. }
  314. if (! SUCCESSFUL(regular_opts(opts)))
  315. return (int)opts->origArgCt;
  316. /*
  317. * IF there were no errors
  318. * AND we have RC/INI files
  319. * AND there is a request to save the files
  320. * THEN do that now before testing for conflicts.
  321. * (conflicts are ignored in preset options)
  322. */
  323. switch (opts->specOptIdx.save_opts) {
  324. case 0:
  325. case NO_EQUIVALENT:
  326. break;
  327. default:
  328. {
  329. tOptDesc * od = opts->pOptDesc + opts->specOptIdx.save_opts;
  330. if (SELECTED_OPT(od)) {
  331. optionSaveFile(opts);
  332. option_exits(EXIT_SUCCESS);
  333. }
  334. }
  335. }
  336. /*
  337. * IF we are checking for errors,
  338. * THEN look for too few occurrences of required options
  339. */
  340. if (((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
  341. && (! is_consistent(opts)))
  342. (*opts->pUsageProc)(opts, EXIT_FAILURE);
  343. return (int)opts->curOptIdx;
  344. }
  345. /** @}
  346. *
  347. * Local Variables:
  348. * mode: C
  349. * c-file-style: "stroustrup"
  350. * indent-tabs-mode: nil
  351. * End:
  352. * end of autoopts/autoopts.c */