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