autoopts.h 13 KB

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