autoopts.c 12 KB

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