autoopts.h 11 KB

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