autoopts.h 9.8 KB

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