5 * This file defines all the global structures and special values
6 * used in the automated option processing library.
12 * This file is part of AutoOpts, a companion to AutoGen.
13 * AutoOpts is free software.
14 * AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
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.
20 * The GNU Lesser General Public License, version 3 or later
21 * See the files "COPYING.lgplv3" and "COPYING.gplv3"
23 * The Modified Berkeley Software Distribution License
24 * See the file "COPYING.mbsd"
26 * These files have the following sha256 sums:
28 * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
29 * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
30 * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
33 #ifndef AUTOGEN_AUTOOPTS_H
34 #define AUTOGEN_AUTOOPTS_H
35 #include <stdnoreturn.h>
37 #define AO_NAME_LIMIT 127
38 #define AO_NAME_SIZE ((size_t)(AO_NAME_LIMIT + 1))
42 # define AG_PATH_MAX ((size_t)PATH_MAX)
44 # define AG_PATH_MAX ((size_t)4096)
47 # if defined(PATH_MAX) && (PATH_MAX > MAXPATHLEN)
49 # define AG_PATH_MAX ((size_t)PATH_MAX)
67 #if defined(_WIN32) && !defined(__CYGWIN__)
75 * Command line usage problem
81 * The input data was incorrect in some way.
83 # define EX_DATAERR 64
87 * option state was requested from a file that cannot be loaded.
89 # define EX_NOINPUT 66
93 * AutoOpts Software failure.
95 # define EX_SOFTWARE 70
99 * Command line usage problem
107 * Coercive cast. Compel an address to be interpreted as the type
108 * of the first argument. No complaints, just do it.
110 #define C(_t,_p) ((_t)VOIDP(_p))
113 /* The __attribute__((__warn_unused_result__)) feature
114 is available in gcc versions 3.4 and newer,
115 while the typeof feature has been available since 2.7 at least. */
116 # if __GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ < 4)
117 # define ignore_val(x) ((void) (x))
119 # define ignore_val(x) (({ __typeof__ (x) __x = (x); (void) __x; }))
123 * Convert the number to a list usable in a printf call
125 #define NUM_TO_VER(n) ((n) >> 12), ((n) >> 7) & 0x001F, (n) & 0x007F
127 #define NAMED_OPTS(po) \
128 (((po)->fOptSet & (OPTPROC_SHORTOPT | OPTPROC_LONGOPT)) == 0)
130 #define SKIP_OPT(p) (((p)->fOptState & OPTST_IMMUTABLE_MASK) != 0)
132 typedef int tDirection;
134 * handling option presets. Start with command line and work through
135 * config settings in reverse order.
137 #define DIRECTION_PRESET -1
139 * handling normal options. Start with first config file, then environment
140 * variables and finally the command line.
142 #define DIRECTION_PROCESS 1
144 * An initialzation phase or an option being loaded from program sources.
146 #define DIRECTION_CALLED 0
148 #define PROCESSING(d) ((d)>0)
149 #define PRESETTING(d) ((d)<0)
150 #define CALLED(d) ((d)==0)
153 * When loading a line (or block) of text as an option, the value can
154 * be processed in any of several modes.
158 * If the value looks like a quoted string, then process it. Double
159 * quoted strings are processed the way strings are in "C" programs,
160 * except they are treated as regular characters if the following
161 * character is not a well-established escape sequence. Single quoted
162 * strings (quoted with apostrophies) are handled the way strings are
163 * handled in shell scripts, *except* that backslash escapes are
164 * honored before backslash escapes and apostrophies.
169 * Even if the value begins with quote characters, do not do quote
170 * processing. Strip leading and trailing white space.
172 OPTION_LOAD_UNCOOKED,
175 * Keep every part of the value between the delimiters.
180 static tOptionLoadMode option_load_mode;
183 * The pager state is used by optionPagedUsage() procedure.
184 * When it runs, it sets itself up to be called again on exit.
185 * If, however, a routine needs a child process to do some work
186 * before it is done, then 'pagerState' must be set to
187 * 'PAGER_STATE_CHILD' so that optionPagedUsage() will not try
188 * to run the pager program before its time.
191 PAGER_STATE_INITIAL, //@< initial option paging state
194 * temp file created and optionPagedUsage is scheduled to run at exit
199 * This is a child process used in creating shell script usage.
219 char const * pzOptArg;
220 opt_state_mask_t flags;
223 #define OPTSTATE_INITIALIZER(st) \
224 { NULL, NULL, OPTST_ ## st, TOPT_UNDEFINED }
226 #define TEXTTO_TABLE \
233 typedef enum { TEXTTO_TABLE COUNT_TT } teTextTo;
238 * option argument types. Used to create usage information for
239 * particular options.
255 char const * pzOptFmt;
259 #define AGALOC(_c, _w) ao_malloc((size_t)_c)
260 #define AGREALOC(_p, _c, _w) ao_realloc(VOIDP(_p), (size_t)_c)
261 #define AGFREE(_p) free(VOIDP(_p))
262 #define AGDUPSTR(_p, _s, _w) (_p = ao_strdup(_s))
265 ao_malloc(size_t sz);
268 ao_realloc(void *p, size_t sz);
270 #define ao_free(_p) free(VOIDP(_p))
273 ao_strdup(char const * str);
276 * DO option handling?
278 * Options are examined at two times: at immediate handling time and at
279 * normal handling time. If an option is disabled, the timing may be
280 * different from the handling of the undisabled option. The OPTST_DIABLED
281 * bit indicates the state of the currently discovered option.
282 * So, here's how it works:
284 * A) handling at "immediate" time, either 1 or 2:
286 * 1. OPTST_DISABLED is not set:
288 * DISABLE_IMM don't care
290 * DISABLE_TWICE don't care
293 * 2. OPTST_DISABLED is set:
295 * DISABLE_IMM must be set
297 * DISABLE_TWICE don't care
300 #define DO_IMMEDIATELY(_flg) \
301 ( (((_flg) & (OPTST_DISABLED|OPTST_IMM)) == OPTST_IMM) \
302 || ( ((_flg) & (OPTST_DISABLED|OPTST_DISABLE_IMM)) \
303 == (OPTST_DISABLED|OPTST_DISABLE_IMM) ))
306 * B) handling at "regular" time because it was not immediate
308 * 1. OPTST_DISABLED is not set:
309 * IMM must *NOT* be set
310 * DISABLE_IMM don't care
312 * DISABLE_TWICE don't care
315 * 2. OPTST_DISABLED is set:
317 * DISABLE_IMM don't care
319 * DISABLE_TWICE don't care
322 #define DO_NORMALLY(_flg) ( \
323 (((_flg) & (OPTST_DISABLED|OPTST_IMM)) == 0) \
324 || (((_flg) & (OPTST_DISABLED|OPTST_DISABLE_IMM)) == \
328 * C) handling at "regular" time because it is to be handled twice.
329 * The immediate bit was already tested and found to be set:
331 * 3. OPTST_DISABLED is not set:
332 * IMM is set (but don't care)
333 * DISABLE_IMM don't care
335 * DISABLE_TWICE don't care
338 * 4. OPTST_DISABLED is set:
340 * DISABLE_IMM is set (but don't care)
342 * DISABLE_TWICE must be set
345 #define DO_SECOND_TIME(_flg) ( \
346 (((_flg) & (OPTST_DISABLED|OPTST_TWICE)) == \
348 || (((_flg) & (OPTST_DISABLED|OPTST_DISABLE_TWICE)) == \
349 (OPTST_DISABLED|OPTST_DISABLE_TWICE) ))
352 * text_mmap structure. Only active on platforms with mmap(2).
354 #ifdef HAVE_SYS_MMAN_H
355 # include <sys/mman.h>
358 # define PROT_READ 0x01
361 # define PROT_WRITE 0x02
364 # define MAP_SHARED 0x01
367 # define MAP_PRIVATE 0x02
372 # define MAP_FAILED VOIDP(-1)
376 # ifdef _SC_PAGE_SIZE
377 # define _SC_PAGESIZE _SC_PAGE_SIZE
382 extern char * strchr(char const * s, int c);
383 extern char * strrchr(char const * s, int c);
387 * INQUERY_CALL() tests whether the option handling function has been
388 * called by an inquery (help text needed, or option being reset),
389 * or called by a set-the-option operation.
391 #define INQUERY_CALL(_o, _d) ( \
392 ((_o) <= OPTPROC_EMIT_LIMIT) \
394 || (((_d)->fOptState & OPTST_RESET) != 0) )
397 * Define and initialize all the user visible strings.
398 * We do not do translations. If translations are to be done, then
399 * the client will provide a callback for that purpose.
401 #undef DO_TRANSLATIONS
402 #include "autoopts/usage-txt.h"
405 * File pointer for usage output
407 FILE * option_usage_fp;
409 * If provided in the option structure
411 static char const * program_pkgdatadir;
413 * privately exported functions
415 extern tOptProc optionPrintVersion, optionPagedUsage, optionLoadOpt;
417 #ifdef AUTOOPTS_INTERNAL
420 # define PKGDATADIR ""
422 #define APOSTROPHE '\''
424 #define OPTPROC_L_N_S (OPTPROC_LONGOPT | OPTPROC_SHORTOPT)
425 #if defined(ENABLE_NLS) && defined(HAVE_LIBINTL_H)
426 # include <libintl.h>
432 char const * fnm_name;
436 * Automated Options Usage Flags.
437 * NB: no entry may be a prefix of another entry
439 #define AOFLAG_TABLE \
440 _aof_(gnu, OPTPROC_GNUUSAGE ) \
441 _aof_(autoopts, ~OPTPROC_GNUUSAGE) \
442 _aof_(no_misuse_usage, OPTPROC_MISUSE ) \
443 _aof_(misuse_usage, ~OPTPROC_MISUSE ) \
444 _aof_(compute, OPTPROC_COMPUTE )
446 #define _aof_(_n, _f) AOUF_ ## _n ## _ID,
447 typedef enum { AOFLAG_TABLE AOUF_COUNT } ao_flag_id_t;
450 #define _aof_(_n, _f) AOUF_ ## _n = (1 << AOUF_ ## _n ## _ID),
451 typedef enum { AOFLAG_TABLE } ao_flags_t;
454 static char const zNil[] = "";
455 static arg_types_t argTypes = { NULL };
456 static char line_fmt_buf[32];
457 static bool displayEnum = false;
458 static char const pkgdatadir_default[] = PKGDATADIR;
459 static char const * program_pkgdatadir = pkgdatadir_default;
460 static tOptionLoadMode option_load_mode = OPTION_LOAD_UNCOOKED;
461 static tePagerState pagerState = PAGER_STATE_INITIAL;
463 FILE * option_usage_fp = NULL;
465 static char const * pz_enum_err_fmt;
467 tOptions * optionParseShellOptions = NULL;
469 static char const * shell_prog = NULL;
470 static char * script_leader = NULL;
471 static char * script_trailer = NULL;
472 static char * script_text = NULL;
473 static bool print_exit = false;
474 #endif /* AUTOOPTS_INTERNAL */
476 #endif /* AUTOGEN_AUTOOPTS_H */
481 * c-file-style: "stroustrup"
482 * indent-tabs-mode: nil
484 * end of autoopts/autoopts.h */