autoopts.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * Time-stamp: "2008-11-01 20:08:06 bkorb"
  3. *
  4. * autoopts.h $Id: autoopts.h,v 4.29 2008/11/02 18:51:26 bkorb Exp $
  5. *
  6. * This file defines all the global structures and special values
  7. * used in the automated option processing library.
  8. *
  9. * This file is part of AutoOpts, a companion to AutoGen.
  10. * AutoOpts is free software.
  11. * AutoOpts is copyright (c) 1992-2008 by Bruce Korb - all rights reserved
  12. * AutoOpts is copyright (c) 1992-2008 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 md5sums:
  25. *
  26. * 239588c55c22c60ffe159946a760a33e pkg/libopts/COPYING.gplv3
  27. * fa82ca978890795162346e661b47161a pkg/libopts/COPYING.lgplv3
  28. * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
  29. */
  30. #ifndef AUTOGEN_AUTOOPTS_H
  31. #define AUTOGEN_AUTOOPTS_H
  32. #include "compat/compat.h"
  33. #include "ag-char-map.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 ((size_t)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. #if defined(_WIN32) && !defined(__CYGWIN__)
  51. # define DIRCH '\\'
  52. #else
  53. # define DIRCH '/'
  54. #endif
  55. #ifndef EX_NOINPUT
  56. # define EX_NOINPUT 66
  57. #endif
  58. #ifndef EX_SOFTWARE
  59. # define EX_SOFTWARE 70
  60. #endif
  61. #ifndef EX_CONFIG
  62. # define EX_CONFIG 78
  63. #endif
  64. /*
  65. * Convert the number to a list usable in a printf call
  66. */
  67. #define NUM_TO_VER(n) ((n) >> 12), ((n) >> 7) & 0x001F, (n) & 0x007F
  68. #define NAMED_OPTS(po) \
  69. (((po)->fOptSet & (OPTPROC_SHORTOPT | OPTPROC_LONGOPT)) == 0)
  70. #define SKIP_OPT(p) (((p)->fOptState & (OPTST_DOCUMENT|OPTST_OMITTED)) != 0)
  71. typedef int tDirection;
  72. #define DIRECTION_PRESET -1
  73. #define DIRECTION_PROCESS 1
  74. #define DIRECTION_CALLED 0
  75. #define PROCESSING(d) ((d)>0)
  76. #define PRESETTING(d) ((d)<0)
  77. /*
  78. * Procedure success codes
  79. *
  80. * USAGE: define procedures to return "tSuccess". Test their results
  81. * with the SUCCEEDED, FAILED and HADGLITCH macros.
  82. *
  83. * Microsoft sticks its nose into user space here, so for Windows' sake,
  84. * make sure all of these are undefined.
  85. */
  86. #undef SUCCESS
  87. #undef FAILURE
  88. #undef PROBLEM
  89. #undef SUCCEEDED
  90. #undef SUCCESSFUL
  91. #undef FAILED
  92. #undef HADGLITCH
  93. #define SUCCESS ((tSuccess) 0)
  94. #define FAILURE ((tSuccess)-1)
  95. #define PROBLEM ((tSuccess) 1)
  96. typedef int tSuccess;
  97. #define SUCCEEDED( p ) ((p) == SUCCESS)
  98. #define SUCCESSFUL( p ) SUCCEEDED( p )
  99. #define FAILED( p ) ((p) < SUCCESS)
  100. #define HADGLITCH( p ) ((p) > SUCCESS)
  101. /*
  102. * When loading a line (or block) of text as an option, the value can
  103. * be processed in any of several modes:
  104. *
  105. * @table @samp
  106. * @item keep
  107. * Every part of the value between the delimiters is saved.
  108. *
  109. * @item uncooked
  110. * Even if the value begins with quote characters, do not do quote processing.
  111. *
  112. * @item cooked
  113. * If the value looks like a quoted string, then process it.
  114. * Double quoted strings are processed the way strings are in "C" programs,
  115. * except they are treated as regular characters if the following character
  116. * is not a well-established escape sequence.
  117. * Single quoted strings (quoted with apostrophies) are handled the way
  118. * strings are handled in shell scripts, *except* that backslash escapes
  119. * are honored before backslash escapes and apostrophies.
  120. * @end table
  121. */
  122. typedef enum {
  123. OPTION_LOAD_COOKED,
  124. OPTION_LOAD_UNCOOKED,
  125. OPTION_LOAD_KEEP
  126. } tOptionLoadMode;
  127. extern tOptionLoadMode option_load_mode;
  128. /*
  129. * The pager state is used by optionPagedUsage() procedure.
  130. * When it runs, it sets itself up to be called again on exit.
  131. * If, however, a routine needs a child process to do some work
  132. * before it is done, then 'pagerState' must be set to
  133. * 'PAGER_STATE_CHILD' so that optionPagedUsage() will not try
  134. * to run the pager program before its time.
  135. */
  136. typedef enum {
  137. PAGER_STATE_INITIAL,
  138. PAGER_STATE_READY,
  139. PAGER_STATE_CHILD
  140. } tePagerState;
  141. extern tePagerState pagerState;
  142. typedef enum {
  143. ENV_ALL,
  144. ENV_IMM,
  145. ENV_NON_IMM
  146. } teEnvPresetType;
  147. typedef enum {
  148. TOPT_UNDEFINED = 0,
  149. TOPT_SHORT,
  150. TOPT_LONG,
  151. TOPT_DEFAULT
  152. } teOptType;
  153. typedef struct {
  154. tOptDesc* pOD;
  155. tCC* pzOptArg;
  156. tAoUL flags;
  157. teOptType optType;
  158. } tOptState;
  159. #define OPTSTATE_INITIALIZER(st) \
  160. { NULL, NULL, OPTST_ ## st, TOPT_UNDEFINED }
  161. #define TEXTTO_TABLE \
  162. _TT_( LONGUSAGE ) \
  163. _TT_( USAGE ) \
  164. _TT_( VERSION )
  165. #define _TT_(n) \
  166. TT_ ## n ,
  167. typedef enum { TEXTTO_TABLE COUNT_TT } teTextTo;
  168. #undef _TT_
  169. typedef struct {
  170. tCC* pzStr;
  171. tCC* pzReq;
  172. tCC* pzNum;
  173. tCC* pzFile;
  174. tCC* pzKey;
  175. tCC* pzKeyL;
  176. tCC* pzBool;
  177. tCC* pzNest;
  178. tCC* pzOpt;
  179. tCC* pzNo;
  180. tCC* pzBrk;
  181. tCC* pzNoF;
  182. tCC* pzSpc;
  183. tCC* pzOptFmt;
  184. tCC* pzTime;
  185. } arg_types_t;
  186. #define AGALOC( c, w ) ao_malloc((size_t)c)
  187. #define AGREALOC( p, c, w ) ao_realloc((void*)p, (size_t)c)
  188. #define AGFREE(_p) do{void*X=(void*)_p;ao_free(X);}while(0)
  189. #define AGDUPSTR( p, s, w ) (p = ao_strdup(s))
  190. static void *
  191. ao_malloc( size_t sz );
  192. static void *
  193. ao_realloc( void *p, size_t sz );
  194. static void
  195. ao_free( void *p );
  196. static char *
  197. ao_strdup( char const *str );
  198. #define TAGMEM( m, t )
  199. /*
  200. * DO option handling?
  201. *
  202. * Options are examined at two times: at immediate handling time and at
  203. * normal handling time. If an option is disabled, the timing may be
  204. * different from the handling of the undisabled option. The OPTST_DIABLED
  205. * bit indicates the state of the currently discovered option.
  206. * So, here's how it works:
  207. *
  208. * A) handling at "immediate" time, either 1 or 2:
  209. *
  210. * 1. OPTST_DISABLED is not set:
  211. * IMM must be set
  212. * DISABLE_IMM don't care
  213. * TWICE don't care
  214. * DISABLE_TWICE don't care
  215. * 0 -and- 1 x x x
  216. *
  217. * 2. OPTST_DISABLED is set:
  218. * IMM don't care
  219. * DISABLE_IMM must be set
  220. * TWICE don't care
  221. * DISABLE_TWICE don't care
  222. * 1 -and- x 1 x x
  223. */
  224. #define DO_IMMEDIATELY(_flg) \
  225. ( (((_flg) & (OPTST_DISABLED|OPTST_IMM)) == OPTST_IMM) \
  226. || ( ((_flg) & (OPTST_DISABLED|OPTST_DISABLE_IMM)) \
  227. == (OPTST_DISABLED|OPTST_DISABLE_IMM) ))
  228. /* B) handling at "regular" time because it was not immediate
  229. *
  230. * 1. OPTST_DISABLED is not set:
  231. * IMM must *NOT* be set
  232. * DISABLE_IMM don't care
  233. * TWICE don't care
  234. * DISABLE_TWICE don't care
  235. * 0 -and- 0 x x x
  236. *
  237. * 2. OPTST_DISABLED is set:
  238. * IMM don't care
  239. * DISABLE_IMM don't care
  240. * TWICE must be set
  241. * DISABLE_TWICE don't care
  242. * 1 -and- x x 1 x
  243. */
  244. #define DO_NORMALLY(_flg) ( \
  245. (((_flg) & (OPTST_DISABLED|OPTST_IMM)) == 0) \
  246. || (((_flg) & (OPTST_DISABLED|OPTST_DISABLE_IMM)) == \
  247. OPTST_DISABLED) )
  248. /* C) handling at "regular" time because it is to be handled twice.
  249. * The immediate bit was already tested and found to be set:
  250. *
  251. * 3. OPTST_DISABLED is not set:
  252. * IMM is set (but don't care)
  253. * DISABLE_IMM don't care
  254. * TWICE must be set
  255. * DISABLE_TWICE don't care
  256. * 0 -and- ? x 1 x
  257. *
  258. * 4. OPTST_DISABLED is set:
  259. * IMM don't care
  260. * DISABLE_IMM is set (but don't care)
  261. * TWICE don't care
  262. * DISABLE_TWICE must be set
  263. * 1 -and- x ? x 1
  264. */
  265. #define DO_SECOND_TIME(_flg) ( \
  266. (((_flg) & (OPTST_DISABLED|OPTST_TWICE)) == \
  267. OPTST_TWICE) \
  268. || (((_flg) & (OPTST_DISABLED|OPTST_DISABLE_TWICE)) == \
  269. (OPTST_DISABLED|OPTST_DISABLE_TWICE) ))
  270. /*
  271. * text_mmap structure. Only active on platforms with mmap(2).
  272. */
  273. #ifdef HAVE_SYS_MMAN_H
  274. # include <sys/mman.h>
  275. #else
  276. # ifndef PROT_READ
  277. # define PROT_READ 0x01
  278. # endif
  279. # ifndef PROT_WRITE
  280. # define PROT_WRITE 0x02
  281. # endif
  282. # ifndef MAP_SHARED
  283. # define MAP_SHARED 0x01
  284. # endif
  285. # ifndef MAP_PRIVATE
  286. # define MAP_PRIVATE 0x02
  287. # endif
  288. #endif
  289. #ifndef MAP_FAILED
  290. # define MAP_FAILED ((void*)-1)
  291. #endif
  292. #ifndef _SC_PAGESIZE
  293. # ifdef _SC_PAGE_SIZE
  294. # define _SC_PAGESIZE _SC_PAGE_SIZE
  295. # endif
  296. #endif
  297. #ifndef HAVE_STRCHR
  298. extern char* strchr( char const *s, int c);
  299. extern char* strrchr( char const *s, int c);
  300. #endif
  301. /*
  302. * Define and initialize all the user visible strings.
  303. * We do not do translations. If translations are to be done, then
  304. * the client will provide a callback for that purpose.
  305. */
  306. #undef DO_TRANSLATIONS
  307. #include "autoopts/usage-txt.h"
  308. /*
  309. * File pointer for usage output
  310. */
  311. extern FILE* option_usage_fp;
  312. extern tOptProc optionPrintVersion, optionPagedUsage, optionLoadOpt;
  313. #endif /* AUTOGEN_AUTOOPTS_H */
  314. /*
  315. * Local Variables:
  316. * mode: C
  317. * c-file-style: "stroustrup"
  318. * indent-tabs-mode: nil
  319. * End:
  320. * end of autoopts/autoopts.h */