1
0

autoopts.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * Time-stamp: "2007-07-04 11:33:08 bkorb"
  3. *
  4. * autoopts.h $Id: autoopts.h,v 4.25 2007/07/04 21:36:36 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-2007 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. * 239588c55c22c60ffe159946a760a33e pkg/libopts/COPYING.gplv3
  26. * fa82ca978890795162346e661b47161a 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. #define AO_NAME_LIMIT 127
  33. #define AO_NAME_SIZE ((size_t)(AO_NAME_LIMIT + 1))
  34. #ifndef AG_PATH_MAX
  35. # ifdef PATH_MAX
  36. # define AG_PATH_MAX ((size_t)PATH_MAX)
  37. # else
  38. # define AG_PATH_MAX ((size_t)4096)
  39. # endif
  40. #else
  41. # if defined(PATH_MAX) && (PATH_MAX > MAXPATHLEN)
  42. # undef AG_PATH_MAX
  43. # define AG_PATH_MAX ((size_t)PATH_MAX)
  44. # endif
  45. #endif
  46. #undef EXPORT
  47. #define EXPORT
  48. #if defined(_WIN32) && !defined(__CYGWIN__)
  49. # define DIRCH '\\'
  50. #else
  51. # define DIRCH '/'
  52. #endif
  53. #ifndef EX_NOINPUT
  54. # define EX_NOINPUT 66
  55. #endif
  56. #ifndef EX_SOFTWARE
  57. # define EX_SOFTWARE 70
  58. #endif
  59. #ifndef EX_CONFIG
  60. # define EX_CONFIG 78
  61. #endif
  62. /*
  63. * Convert the number to a list usable in a printf call
  64. */
  65. #define NUM_TO_VER(n) ((n) >> 12), ((n) >> 7) & 0x001F, (n) & 0x007F
  66. #define NAMED_OPTS(po) \
  67. (((po)->fOptSet & (OPTPROC_SHORTOPT | OPTPROC_LONGOPT)) == 0)
  68. #define SKIP_OPT(p) (((p)->fOptState & (OPTST_DOCUMENT|OPTST_OMITTED)) != 0)
  69. typedef int tDirection;
  70. #define DIRECTION_PRESET -1
  71. #define DIRECTION_PROCESS 1
  72. #define DIRECTION_CALLED 0
  73. #define PROCESSING(d) ((d)>0)
  74. #define PRESETTING(d) ((d)<0)
  75. #define ISNAMECHAR( c ) (isalnum(c) || ((c) == '_') || ((c) == '-'))
  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* pzKey;
  173. tCC* pzKeyL;
  174. tCC* pzBool;
  175. tCC* pzNest;
  176. tCC* pzOpt;
  177. tCC* pzNo;
  178. tCC* pzBrk;
  179. tCC* pzNoF;
  180. tCC* pzSpc;
  181. tCC* pzOptFmt;
  182. } arg_types_t;
  183. #define AGALOC( c, w ) ao_malloc((size_t)c)
  184. #define AGREALOC( p, c, w ) ao_realloc((void*)p, (size_t)c)
  185. #define AGFREE( p ) ao_free((void*)p)
  186. #define AGDUPSTR( p, s, w ) (p = ao_strdup(s))
  187. static void *
  188. ao_malloc( size_t sz );
  189. static void *
  190. ao_realloc( void *p, size_t sz );
  191. static void
  192. ao_free( void *p );
  193. static char *
  194. ao_strdup( char const *str );
  195. #define TAGMEM( m, t )
  196. /*
  197. * DO option handling?
  198. *
  199. * Options are examined at two times: at immediate handling time and at
  200. * normal handling time. If an option is disabled, the timing may be
  201. * different from the handling of the undisabled option. The OPTST_DIABLED
  202. * bit indicates the state of the currently discovered option.
  203. * So, here's how it works:
  204. *
  205. * A) handling at "immediate" time, either 1 or 2:
  206. *
  207. * 1. OPTST_DISABLED is not set:
  208. * IMM must be set
  209. * DISABLE_IMM don't care
  210. * TWICE don't care
  211. * DISABLE_TWICE don't care
  212. * 0 -and- 1 x x x
  213. *
  214. * 2. OPTST_DISABLED is set:
  215. * IMM don't care
  216. * DISABLE_IMM must be set
  217. * TWICE don't care
  218. * DISABLE_TWICE don't care
  219. * 1 -and- x 1 x x
  220. */
  221. #define DO_IMMEDIATELY(_flg) \
  222. ( (((_flg) & (OPTST_DISABLED|OPTST_IMM)) == OPTST_IMM) \
  223. || ( ((_flg) & (OPTST_DISABLED|OPTST_DISABLE_IMM)) \
  224. == (OPTST_DISABLED|OPTST_DISABLE_IMM) ))
  225. /* B) handling at "regular" time because it was not immediate
  226. *
  227. * 1. OPTST_DISABLED is not set:
  228. * IMM must *NOT* be set
  229. * DISABLE_IMM don't care
  230. * TWICE don't care
  231. * DISABLE_TWICE don't care
  232. * 0 -and- 0 x x x
  233. *
  234. * 2. OPTST_DISABLED is set:
  235. * IMM don't care
  236. * DISABLE_IMM don't care
  237. * TWICE must be set
  238. * DISABLE_TWICE don't care
  239. * 1 -and- x x 1 x
  240. */
  241. #define DO_NORMALLY(_flg) ( \
  242. (((_flg) & (OPTST_DISABLED|OPTST_IMM)) == 0) \
  243. || (((_flg) & (OPTST_DISABLED|OPTST_DISABLE_IMM)) == \
  244. OPTST_DISABLED) )
  245. /* C) handling at "regular" time because it is to be handled twice.
  246. * The immediate bit was already tested and found to be set:
  247. *
  248. * 3. OPTST_DISABLED is not set:
  249. * IMM is set (but don't care)
  250. * DISABLE_IMM don't care
  251. * TWICE must be set
  252. * DISABLE_TWICE don't care
  253. * 0 -and- ? x 1 x
  254. *
  255. * 4. OPTST_DISABLED is set:
  256. * IMM don't care
  257. * DISABLE_IMM is set (but don't care)
  258. * TWICE don't care
  259. * DISABLE_TWICE must be set
  260. * 1 -and- x ? x 1
  261. */
  262. #define DO_SECOND_TIME(_flg) ( \
  263. (((_flg) & (OPTST_DISABLED|OPTST_TWICE)) == \
  264. OPTST_TWICE) \
  265. || (((_flg) & (OPTST_DISABLED|OPTST_DISABLE_TWICE)) == \
  266. (OPTST_DISABLED|OPTST_DISABLE_TWICE) ))
  267. /*
  268. * text_mmap structure. Only active on platforms with mmap(2).
  269. */
  270. #ifdef HAVE_SYS_MMAN_H
  271. # include <sys/mman.h>
  272. #else
  273. # ifndef PROT_READ
  274. # define PROT_READ 0x01
  275. # endif
  276. # ifndef PROT_WRITE
  277. # define PROT_WRITE 0x02
  278. # endif
  279. # ifndef MAP_SHARED
  280. # define MAP_SHARED 0x01
  281. # endif
  282. # ifndef MAP_PRIVATE
  283. # define MAP_PRIVATE 0x02
  284. # endif
  285. #endif
  286. #ifndef MAP_FAILED
  287. # define MAP_FAILED ((void*)-1)
  288. #endif
  289. #ifndef _SC_PAGESIZE
  290. # ifdef _SC_PAGE_SIZE
  291. # define _SC_PAGESIZE _SC_PAGE_SIZE
  292. # endif
  293. #endif
  294. #ifndef HAVE_STRCHR
  295. extern char* strchr( char const *s, int c);
  296. extern char* strrchr( char const *s, int c);
  297. #endif
  298. /*
  299. * Define and initialize all the user visible strings.
  300. * We do not do translations. If translations are to be done, then
  301. * the client will provide a callback for that purpose.
  302. */
  303. #undef DO_TRANSLATIONS
  304. #include "autoopts/usage-txt.h"
  305. /*
  306. * File pointer for usage output
  307. */
  308. extern FILE* option_usage_fp;
  309. extern tOptProc optionPrintVersion, optionPagedUsage, optionLoadOpt;
  310. #endif /* AUTOGEN_AUTOOPTS_H */
  311. /*
  312. * Local Variables:
  313. * mode: C
  314. * c-file-style: "stroustrup"
  315. * indent-tabs-mode: nil
  316. * End:
  317. * end of autoopts/autoopts.h */