autoopts.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * \file autoopts.h
  3. *
  4. * This file defines all the global structures and special values
  5. * used in the automated option processing library.
  6. *
  7. * @group autoopts
  8. * @{
  9. */
  10. /*
  11. * This file is part of AutoOpts, a companion to AutoGen.
  12. * AutoOpts is free software.
  13. * AutoOpts is Copyright (C) 1992-2016 by Bruce Korb - all rights reserved
  14. *
  15. * AutoOpts is available under any one of two licenses. The license
  16. * in use must be one of these two and the choice is under the control
  17. * of the user of the license.
  18. *
  19. * The GNU Lesser General Public License, version 3 or later
  20. * See the files "COPYING.lgplv3" and "COPYING.gplv3"
  21. *
  22. * The Modified Berkeley Software Distribution License
  23. * See the file "COPYING.mbsd"
  24. *
  25. * These files have the following sha256 sums:
  26. *
  27. * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
  28. * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
  29. * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
  30. */
  31. #ifndef AUTOGEN_AUTOOPTS_H
  32. #define AUTOGEN_AUTOOPTS_H
  33. #include <stdnoreturn.h>
  34. #define AO_NAME_LIMIT 127
  35. #define AO_NAME_SIZE ((size_t)(AO_NAME_LIMIT + 1))
  36. #ifndef AG_PATH_MAX
  37. # ifdef PATH_MAX
  38. # define AG_PATH_MAX ((size_t)PATH_MAX)
  39. # else
  40. # ifdef __gnu_hurd__
  41. # define size_t unsigned long
  42. # endif
  43. # define AG_PATH_MAX ((size_t)4096)
  44. # endif
  45. #else
  46. # if defined(PATH_MAX) && (PATH_MAX > MAXPATHLEN)
  47. # undef AG_PATH_MAX
  48. # define AG_PATH_MAX ((size_t)PATH_MAX)
  49. # endif
  50. #endif
  51. #undef EXPORT
  52. #define EXPORT
  53. #ifndef NUL
  54. #define NUL '\0'
  55. #endif
  56. #define BEL '\a'
  57. #define BS '\b'
  58. #define HT '\t'
  59. #define LF '\n'
  60. #define VT '\v'
  61. #define FF '\f'
  62. #define CR '\r'
  63. #if defined(_WIN32) && !defined(__CYGWIN__)
  64. # define DIRCH '\\'
  65. #else
  66. # define DIRCH '/'
  67. #endif
  68. #ifndef EX_USAGE
  69. /**
  70. * Command line usage problem
  71. */
  72. # define EX_USAGE 64
  73. #endif
  74. #ifndef EX_DATAERR
  75. /**
  76. * The input data was incorrect in some way.
  77. */
  78. # define EX_DATAERR 64
  79. #endif
  80. #ifndef EX_NOINPUT
  81. /**
  82. * option state was requested from a file that cannot be loaded.
  83. */
  84. # define EX_NOINPUT 66
  85. #endif
  86. #ifndef EX_SOFTWARE
  87. /**
  88. * AutoOpts Software failure.
  89. */
  90. # define EX_SOFTWARE 70
  91. #endif
  92. #ifndef EX_OSERR
  93. /**
  94. * Command line usage problem
  95. */
  96. # define EX_OSERR 71
  97. #endif
  98. #define NL '\n'
  99. #ifndef C
  100. /**
  101. * Coercive cast. Compel an address to be interpreted as the type
  102. * of the first argument. No complaints, just do it.
  103. */
  104. #define C(_t,_p) ((_t)VOIDP(_p))
  105. #endif
  106. /* The __attribute__((__warn_unused_result__)) feature
  107. is available in gcc versions 3.4 and newer,
  108. while the typeof feature has been available since 2.7 at least. */
  109. # if __GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ < 4)
  110. # define ignore_val(x) ((void) (x))
  111. # else
  112. # define ignore_val(x) (({ __typeof__ (x) __x = (x); (void) __x; }))
  113. # endif
  114. /*
  115. * Convert the number to a list usable in a printf call
  116. */
  117. #define NUM_TO_VER(n) ((n) >> 12), ((n) >> 7) & 0x001F, (n) & 0x007F
  118. #define NAMED_OPTS(po) \
  119. (((po)->fOptSet & (OPTPROC_SHORTOPT | OPTPROC_LONGOPT)) == 0)
  120. #define SKIP_OPT(p) (((p)->fOptState & OPTST_IMMUTABLE_MASK) != 0)
  121. typedef int tDirection;
  122. /**
  123. * handling option presets. Start with command line and work through
  124. * config settings in reverse order.
  125. */
  126. #define DIRECTION_PRESET -1
  127. /**
  128. * handling normal options. Start with first config file, then environment
  129. * variables and finally the command line.
  130. */
  131. #define DIRECTION_PROCESS 1
  132. /**
  133. * An initialzation phase or an option being loaded from program sources.
  134. */
  135. #define DIRECTION_CALLED 0
  136. #define PROCESSING(d) ((d)>0)
  137. #define PRESETTING(d) ((d)<0)
  138. #define CALLED(d) ((d)==0)
  139. /**
  140. * When loading a line (or block) of text as an option, the value can
  141. * be processed in any of several modes.
  142. */
  143. typedef enum {
  144. /**
  145. * If the value looks like a quoted string, then process it. Double
  146. * quoted strings are processed the way strings are in "C" programs,
  147. * except they are treated as regular characters if the following
  148. * character is not a well-established escape sequence. Single quoted
  149. * strings (quoted with apostrophies) are handled the way strings are
  150. * handled in shell scripts, *except* that backslash escapes are
  151. * honored before backslash escapes and apostrophies.
  152. */
  153. OPTION_LOAD_COOKED,
  154. /**
  155. * Even if the value begins with quote characters, do not do quote
  156. * processing. Strip leading and trailing white space.
  157. */
  158. OPTION_LOAD_UNCOOKED,
  159. /**
  160. * Keep every part of the value between the delimiters.
  161. */
  162. OPTION_LOAD_KEEP
  163. } tOptionLoadMode;
  164. static tOptionLoadMode option_load_mode;
  165. /**
  166. * The pager state is used by optionPagedUsage() procedure.
  167. * When it runs, it sets itself up to be called again on exit.
  168. * If, however, a routine needs a child process to do some work
  169. * before it is done, then 'pagerState' must be set to
  170. * 'PAGER_STATE_CHILD' so that optionPagedUsage() will not try
  171. * to run the pager program before its time.
  172. */
  173. typedef enum {
  174. PAGER_STATE_INITIAL, //@< initial option paging state
  175. /**
  176. * temp file created and optionPagedUsage is scheduled to run at exit
  177. */
  178. PAGER_STATE_READY,
  179. /**
  180. * This is a child process used in creating shell script usage.
  181. */
  182. PAGER_STATE_CHILD
  183. } tePagerState;
  184. typedef enum {
  185. ENV_ALL,
  186. ENV_IMM,
  187. ENV_NON_IMM
  188. } teEnvPresetType;
  189. typedef enum {
  190. TOPT_UNDEFINED = 0,
  191. TOPT_SHORT,
  192. TOPT_LONG,
  193. TOPT_DEFAULT
  194. } teOptType;
  195. typedef struct {
  196. tOptDesc * pOD;
  197. char const * pzOptArg;
  198. opt_state_mask_t flags;
  199. teOptType optType;
  200. } tOptState;
  201. #define OPTSTATE_INITIALIZER(st) \
  202. { NULL, NULL, OPTST_ ## st, TOPT_UNDEFINED }
  203. #define TEXTTO_TABLE \
  204. _TT_(LONGUSAGE) \
  205. _TT_(USAGE) \
  206. _TT_(VERSION)
  207. #define _TT_(n) \
  208. TT_ ## n ,
  209. typedef enum { TEXTTO_TABLE COUNT_TT } teTextTo;
  210. #undef _TT_
  211. /**
  212. * option argument types. Used to create usage information for
  213. * particular options.
  214. */
  215. typedef struct {
  216. char const * pzStr;
  217. char const * pzReq;
  218. char const * pzNum;
  219. char const * pzFile;
  220. char const * pzKey;
  221. char const * pzKeyL;
  222. char const * pzBool;
  223. char const * pzNest;
  224. char const * pzOpt;
  225. char const * pzNo;
  226. char const * pzBrk;
  227. char const * pzNoF;
  228. char const * pzSpc;
  229. char const * pzOptFmt;
  230. char const * pzTime;
  231. } arg_types_t;
  232. #define AGALOC(_c, _w) ao_malloc((size_t)_c)
  233. #define AGREALOC(_p, _c, _w) ao_realloc(VOIDP(_p), (size_t)_c)
  234. #define AGFREE(_p) free(VOIDP(_p))
  235. #define AGDUPSTR(_p, _s, _w) (_p = ao_strdup(_s))
  236. static void *
  237. ao_malloc(size_t sz);
  238. static void *
  239. ao_realloc(void *p, size_t sz);
  240. #define ao_free(_p) free(VOIDP(_p))
  241. static char *
  242. ao_strdup(char const * str);
  243. /**
  244. * DO option handling?
  245. *
  246. * Options are examined at two times: at immediate handling time and at
  247. * normal handling time. If an option is disabled, the timing may be
  248. * different from the handling of the undisabled option. The OPTST_DIABLED
  249. * bit indicates the state of the currently discovered option.
  250. * So, here's how it works:
  251. *
  252. * A) handling at "immediate" time, either 1 or 2:
  253. *
  254. * 1. OPTST_DISABLED is not set:
  255. * IMM must be set
  256. * DISABLE_IMM don't care
  257. * TWICE don't care
  258. * DISABLE_TWICE don't care
  259. * 0 -and- 1 x x x
  260. *
  261. * 2. OPTST_DISABLED is set:
  262. * IMM don't care
  263. * DISABLE_IMM must be set
  264. * TWICE don't care
  265. * DISABLE_TWICE don't care
  266. * 1 -and- x 1 x x
  267. */
  268. #define DO_IMMEDIATELY(_flg) \
  269. ( (((_flg) & (OPTST_DISABLED|OPTST_IMM)) == OPTST_IMM) \
  270. || ( ((_flg) & (OPTST_DISABLED|OPTST_DISABLE_IMM)) \
  271. == (OPTST_DISABLED|OPTST_DISABLE_IMM) ))
  272. /**
  273. * B) handling at "regular" time because it was not immediate
  274. *
  275. * 1. OPTST_DISABLED is not set:
  276. * IMM must *NOT* be set
  277. * DISABLE_IMM don't care
  278. * TWICE don't care
  279. * DISABLE_TWICE don't care
  280. * 0 -and- 0 x x x
  281. *
  282. * 2. OPTST_DISABLED is set:
  283. * IMM don't care
  284. * DISABLE_IMM don't care
  285. * TWICE must be set
  286. * DISABLE_TWICE don't care
  287. * 1 -and- x x 1 x
  288. */
  289. #define DO_NORMALLY(_flg) ( \
  290. (((_flg) & (OPTST_DISABLED|OPTST_IMM)) == 0) \
  291. || (((_flg) & (OPTST_DISABLED|OPTST_DISABLE_IMM)) == \
  292. OPTST_DISABLED) )
  293. /**
  294. * C) handling at "regular" time because it is to be handled twice.
  295. * The immediate bit was already tested and found to be set:
  296. *
  297. * 3. OPTST_DISABLED is not set:
  298. * IMM is set (but don't care)
  299. * DISABLE_IMM don't care
  300. * TWICE must be set
  301. * DISABLE_TWICE don't care
  302. * 0 -and- ? x 1 x
  303. *
  304. * 4. OPTST_DISABLED is set:
  305. * IMM don't care
  306. * DISABLE_IMM is set (but don't care)
  307. * TWICE don't care
  308. * DISABLE_TWICE must be set
  309. * 1 -and- x ? x 1
  310. */
  311. #define DO_SECOND_TIME(_flg) ( \
  312. (((_flg) & (OPTST_DISABLED|OPTST_TWICE)) == \
  313. OPTST_TWICE) \
  314. || (((_flg) & (OPTST_DISABLED|OPTST_DISABLE_TWICE)) == \
  315. (OPTST_DISABLED|OPTST_DISABLE_TWICE) ))
  316. /*
  317. * text_mmap structure. Only active on platforms with mmap(2).
  318. */
  319. #ifdef HAVE_SYS_MMAN_H
  320. # include <sys/mman.h>
  321. #else
  322. # ifndef PROT_READ
  323. # define PROT_READ 0x01
  324. # endif
  325. # ifndef PROT_WRITE
  326. # define PROT_WRITE 0x02
  327. # endif
  328. # ifndef MAP_SHARED
  329. # define MAP_SHARED 0x01
  330. # endif
  331. # ifndef MAP_PRIVATE
  332. # define MAP_PRIVATE 0x02
  333. # endif
  334. #endif
  335. #ifndef MAP_FAILED
  336. # define MAP_FAILED VOIDP(-1)
  337. #endif
  338. #ifndef _SC_PAGESIZE
  339. # ifdef _SC_PAGE_SIZE
  340. # define _SC_PAGESIZE _SC_PAGE_SIZE
  341. # endif
  342. #endif
  343. #ifndef HAVE_STRCHR
  344. extern char * strchr(char const * s, int c);
  345. extern char * strrchr(char const * s, int c);
  346. #endif
  347. /**
  348. * INQUERY_CALL() tests whether the option handling function has been
  349. * called by an inquery (help text needed, or option being reset),
  350. * or called by a set-the-option operation.
  351. */
  352. #define INQUERY_CALL(_o, _d) ( \
  353. ((_o) <= OPTPROC_EMIT_LIMIT) \
  354. || ((_d) == NULL) \
  355. || (((_d)->fOptState & OPTST_RESET) != 0) )
  356. /**
  357. * Define and initialize all the user visible strings.
  358. * We do not do translations. If translations are to be done, then
  359. * the client will provide a callback for that purpose.
  360. */
  361. #undef DO_TRANSLATIONS
  362. #include "autoopts/usage-txt.h"
  363. /**
  364. * File pointer for usage output
  365. */
  366. FILE * option_usage_fp;
  367. /**
  368. * If provided in the option structure
  369. */
  370. static char const * program_pkgdatadir;
  371. /**
  372. * privately exported functions
  373. */
  374. extern tOptProc optionPrintVersion, optionPagedUsage, optionLoadOpt;
  375. #ifdef AUTOOPTS_INTERNAL
  376. #ifndef PKGDATADIR
  377. # define PKGDATADIR ""
  378. #endif
  379. #define APOSTROPHE '\''
  380. #define OPTPROC_L_N_S (OPTPROC_LONGOPT | OPTPROC_SHORTOPT)
  381. #if defined(ENABLE_NLS) && defined(HAVE_LIBINTL_H)
  382. # include <libintl.h>
  383. #endif
  384. typedef struct {
  385. size_t fnm_len;
  386. uint32_t fnm_mask;
  387. char const * fnm_name;
  388. } ao_flag_names_t;
  389. /**
  390. * Automated Options Usage Flags.
  391. * NB: no entry may be a prefix of another entry
  392. */
  393. #define AOFLAG_TABLE \
  394. _aof_(gnu, OPTPROC_GNUUSAGE ) \
  395. _aof_(autoopts, ~OPTPROC_GNUUSAGE) \
  396. _aof_(no_misuse_usage, OPTPROC_MISUSE ) \
  397. _aof_(misuse_usage, ~OPTPROC_MISUSE ) \
  398. _aof_(compute, OPTPROC_COMPUTE )
  399. #define _aof_(_n, _f) AOUF_ ## _n ## _ID,
  400. typedef enum { AOFLAG_TABLE AOUF_COUNT } ao_flag_id_t;
  401. #undef _aof_
  402. #define _aof_(_n, _f) AOUF_ ## _n = (1 << AOUF_ ## _n ## _ID),
  403. typedef enum { AOFLAG_TABLE } ao_flags_t;
  404. #undef _aof_
  405. static char const zNil[] = "";
  406. static arg_types_t argTypes = { NULL };
  407. static char line_fmt_buf[32];
  408. static bool displayEnum = false;
  409. static char const pkgdatadir_default[] = PKGDATADIR;
  410. static char const * program_pkgdatadir = pkgdatadir_default;
  411. static tOptionLoadMode option_load_mode = OPTION_LOAD_UNCOOKED;
  412. static tePagerState pagerState = PAGER_STATE_INITIAL;
  413. FILE * option_usage_fp = NULL;
  414. static char const * pz_enum_err_fmt;
  415. tOptions * optionParseShellOptions = NULL;
  416. static char const * shell_prog = NULL;
  417. static char * script_leader = NULL;
  418. static char * script_trailer = NULL;
  419. static char * script_text = NULL;
  420. static bool print_exit = false;
  421. #endif /* AUTOOPTS_INTERNAL */
  422. #endif /* AUTOGEN_AUTOOPTS_H */
  423. /**
  424. * @}
  425. * Local Variables:
  426. * mode: C
  427. * c-file-style: "stroustrup"
  428. * indent-tabs-mode: nil
  429. * End:
  430. * end of autoopts/autoopts.h */