autoopts.h 9.5 KB

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