autoopts.h 10 KB

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