autoopts.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Time-stamp: "2005-10-29 15:06:44 bkorb"
  3. *
  4. * autoopts.h $Id: autoopts.h,v 4.17 2006/03/25 19:24:56 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-2006 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 (AO_NAME_LIMIT + 1)
  57. #ifndef MAXPATHLEN
  58. # ifdef PATH_MAX
  59. # define MAXPATHLEN PATH_MAX
  60. # else
  61. # define MAXPATHLEN 4096
  62. # endif
  63. #else
  64. # if defined(PATH_MAX) && (PATH_MAX > MAXPATHLEN)
  65. # undef MAXPATHLEN
  66. # define MAXPATHLEN PATH_MAX
  67. # endif
  68. #endif
  69. #undef EXPORT
  70. #define EXPORT
  71. /*
  72. * Convert the number to a list usable in a printf call
  73. */
  74. #define NUM_TO_VER(n) ((n) >> 12), ((n) >> 7) & 0x001F, (n) & 0x007F
  75. #define NAMED_OPTS(po) \
  76. (((po)->fOptSet & (OPTPROC_SHORTOPT | OPTPROC_LONGOPT)) == 0)
  77. #define SKIP_OPT(p) (((p)->fOptState & (OPTST_DOCUMENT|OPTST_OMITTED)) != 0)
  78. typedef int tDirection;
  79. #define DIRECTION_PRESET -1
  80. #define DIRECTION_PROCESS 1
  81. #define DIRECTION_CALLED 0
  82. #define PROCESSING(d) ((d)>0)
  83. #define PRESETTING(d) ((d)<0)
  84. #define ISNAMECHAR( c ) (isalnum(c) || ((c) == '_') || ((c) == '-'))
  85. /*
  86. * Procedure success codes
  87. *
  88. * USAGE: define procedures to return "tSuccess". Test their results
  89. * with the SUCCEEDED, FAILED and HADGLITCH macros.
  90. */
  91. #define SUCCESS ((tSuccess) 0)
  92. #define FAILURE ((tSuccess)-1)
  93. #define PROBLEM ((tSuccess) 1)
  94. typedef int tSuccess;
  95. #define SUCCEEDED( p ) ((p) == SUCCESS)
  96. #define SUCCESSFUL( p ) SUCCEEDED( p )
  97. #define FAILED( p ) ((p) < SUCCESS)
  98. #define HADGLITCH( p ) ((p) > SUCCESS)
  99. /*
  100. * The pager state is used by optionPagedUsage() procedure.
  101. * When it runs, it sets itself up to be called again on exit.
  102. * If, however, a routine needs a child process to do some work
  103. * before it is done, then 'pagerState' must be set to
  104. * 'PAGER_STATE_CHILD' so that optionPagedUsage() will not try
  105. * to run the pager program before its time.
  106. */
  107. typedef enum {
  108. PAGER_STATE_INITIAL,
  109. PAGER_STATE_READY,
  110. PAGER_STATE_CHILD
  111. } tePagerState;
  112. extern tePagerState pagerState;
  113. typedef enum {
  114. ENV_ALL,
  115. ENV_IMM,
  116. ENV_NON_IMM
  117. } teEnvPresetType;
  118. typedef enum {
  119. TOPT_UNDEFINED = 0,
  120. TOPT_SHORT,
  121. TOPT_LONG,
  122. TOPT_DEFAULT
  123. } teOptType;
  124. typedef struct {
  125. tOptDesc* pOD;
  126. tCC* pzOptArg;
  127. tAoUL flags;
  128. teOptType optType;
  129. } tOptState;
  130. #define OPTSTATE_INITIALIZER(st) \
  131. { NULL, NULL, OPTST_ ## st, TOPT_UNDEFINED }
  132. #define TEXTTO_TABLE \
  133. _TT_( LONGUSAGE ) \
  134. _TT_( USAGE ) \
  135. _TT_( VERSION )
  136. #define _TT_(n) \
  137. TT_ ## n ,
  138. typedef enum { TEXTTO_TABLE COUNT_TT } teTextTo;
  139. #undef _TT_
  140. typedef struct {
  141. tCC* pzStr;
  142. tCC* pzReq;
  143. tCC* pzNum;
  144. tCC* pzKey;
  145. tCC* pzKeyL;
  146. tCC* pzBool;
  147. tCC* pzNest;
  148. tCC* pzOpt;
  149. tCC* pzNo;
  150. tCC* pzBrk;
  151. tCC* pzNoF;
  152. tCC* pzSpc;
  153. tCC* pzOptFmt;
  154. } arg_types_t;
  155. # define AGALOC( c, w ) malloc( c )
  156. # define AGREALOC( p, c, w ) realloc( p, c )
  157. # define AGFREE( p ) free( p )
  158. # define AGDUPSTR( p, s, w ) p = strdup( s )
  159. # define TAGMEM( m, t )
  160. #ifdef AUTOGEN_BUILD
  161. # include <snprintfv/printf.h>
  162. #endif /* AUTOGEN_BUILD */
  163. /*
  164. * DO option handling?
  165. *
  166. * Options are examined at two times: at immediate handling time and at
  167. * normal handling time. If an option is disabled, the timing may be
  168. * different from the handling of the undisabled option. The OPTST_DIABLED
  169. * bit indicates the state of the currently discovered option.
  170. * So, here's how it works:
  171. *
  172. * A) handling at "immediate" time, either 1 or 2:
  173. *
  174. * 1. OPTST_DISABLED is not set:
  175. * IMM must be set
  176. * DISABLE_IMM don't care
  177. * TWICE don't care
  178. * DISABLE_TWICE don't care
  179. * 0 -and- 1 x x x
  180. *
  181. * 2. OPTST_DISABLED is set:
  182. * IMM don't care
  183. * DISABLE_IMM must be set
  184. * TWICE don't care
  185. * DISABLE_TWICE don't care
  186. * 1 -and- x 1 x x
  187. */
  188. #define DO_IMMEDIATELY(_flg) \
  189. ( (((_flg) & (OPTST_DISABLED|OPTST_IMM)) == OPTST_IMM) \
  190. || ( ((_flg) & (OPTST_DISABLED|OPTST_DISABLE_IMM)) \
  191. == (OPTST_DISABLED|OPTST_DISABLE_IMM) ))
  192. /* B) handling at "regular" time because it was not immediate
  193. *
  194. * 1. OPTST_DISABLED is not set:
  195. * IMM must *NOT* be set
  196. * DISABLE_IMM don't care
  197. * TWICE don't care
  198. * DISABLE_TWICE don't care
  199. * 0 -and- 0 x x x
  200. *
  201. * 2. OPTST_DISABLED is set:
  202. * IMM don't care
  203. * DISABLE_IMM don't care
  204. * TWICE must be set
  205. * DISABLE_TWICE don't care
  206. * 1 -and- x x 1 x
  207. */
  208. #define DO_NORMALLY(_flg) ( \
  209. (((_flg) & (OPTST_DISABLED|OPTST_IMM)) == 0) \
  210. || (((_flg) & (OPTST_DISABLED|OPTST_DISABLE_IMM)) == \
  211. OPTST_DISABLED) )
  212. /* C) handling at "regular" time because it is to be handled twice.
  213. * The immediate bit was already tested and found to be set:
  214. *
  215. * 3. OPTST_DISABLED is not set:
  216. * IMM is set (but don't care)
  217. * DISABLE_IMM don't care
  218. * TWICE must be set
  219. * DISABLE_TWICE don't care
  220. * 0 -and- ? x 1 x
  221. *
  222. * 4. OPTST_DISABLED is set:
  223. * IMM don't care
  224. * DISABLE_IMM is set (but don't care)
  225. * TWICE don't care
  226. * DISABLE_TWICE must be set
  227. * 1 -and- x ? x 1
  228. */
  229. #define DO_SECOND_TIME(_flg) ( \
  230. (((_flg) & (OPTST_DISABLED|OPTST_TWICE)) == \
  231. OPTST_TWICE) \
  232. || (((_flg) & (OPTST_DISABLED|OPTST_DISABLE_TWICE)) == \
  233. (OPTST_DISABLED|OPTST_DISABLE_TWICE) ))
  234. /*
  235. * text_mmap structure. Only active on platforms with mmap(2).
  236. */
  237. #ifdef HAVE_SYS_MMAN_H
  238. # include <sys/mman.h>
  239. #else
  240. # ifndef PROT_READ
  241. # define PROT_READ 0x01
  242. # endif
  243. # ifndef PROT_WRITE
  244. # define PROT_WRITE 0x02
  245. # endif
  246. # ifndef MAP_SHARED
  247. # define MAP_SHARED 0x01
  248. # endif
  249. # ifndef MAP_PRIVATE
  250. # define MAP_PRIVATE 0x02
  251. # endif
  252. #endif
  253. #ifndef MAP_FAILED
  254. # define MAP_FAILED ((void*)-1)
  255. #endif
  256. #ifndef _SC_PAGESIZE
  257. # ifdef _SC_PAGE_SIZE
  258. # define _SC_PAGESIZE _SC_PAGE_SIZE
  259. # endif
  260. #endif
  261. /*
  262. * Define and initialize all the user visible strings.
  263. * We do not do translations. If translations are to be done, then
  264. * the client will provide a callback for that purpose.
  265. */
  266. #undef DO_TRANSLATIONS
  267. #include "autoopts/usage-txt.h"
  268. /*
  269. * File pointer for usage output
  270. */
  271. extern FILE* option_usage_fp;
  272. extern tOptProc optionPrintVersion, optionPagedUsage, optionLoadOpt;
  273. #define LOCAL static
  274. #include "proto.h"
  275. #endif /* AUTOGEN_AUTOOPTS_H */
  276. /*
  277. * Local Variables:
  278. * mode: C
  279. * c-file-style: "stroustrup"
  280. * tab-width: 4
  281. * indent-tabs-mode: nil
  282. * End:
  283. * end of autoopts/autoopts.h */