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