xref: /netbsd-src/external/bsd/ntp/dist/sntp/libopts/autoopts/options.h (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: options.h,v 1.6 2013/12/28 03:20:15 christos Exp $	*/
2 
3 /*   -*- buffer-read-only: t -*- vi: set ro:
4  *
5  *  DO NOT EDIT THIS FILE   (options.h)
6  *
7  *  It has been AutoGen-ed  November 10, 2013 at 01:00:42 PM by AutoGen 5.18.2
8  *  From the definitions    funcs.def
9  *  and the template file   options_h
10  *
11  *  This file defines all the global structures and special values
12  *  used in the automated option processing library.
13  *
14  *  Automated Options Copyright (C) 1992-2013 by Bruce Korb
15  *
16  *  This file is part of AutoOpts, a companion to AutoGen.
17  *  AutoOpts is free software.
18  *  AutoOpts is Copyright (C) 1992-2013 by Bruce Korb - all rights reserved
19  *
20  *  AutoOpts is available under any one of two licenses.  The license
21  *  in use must be one of these two and the choice is under the control
22  *  of the user of the license.
23  *
24  *   The GNU Lesser General Public License, version 3 or later
25  *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
26  *
27  *   The Modified Berkeley Software Distribution License
28  *      See the file "COPYING.mbsd"
29  *
30  *  These files have the following sha256 sums:
31  *
32  *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
33  *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
34  *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
35  */
36 #ifndef AUTOOPTS_OPTIONS_H_GUARD
37 #define AUTOOPTS_OPTIONS_H_GUARD 1
38 /** \file options.h
39  *
40  * @addtogroup autoopts
41  * @{
42  */
43 #include <sys/types.h>
44 #include <stdio.h>
45 
46 #ifndef COMPAT_H_GUARD
47 /*
48  * This is needed for test compilations where the "compat.h"
49  * header is not usually available.
50  */
51 #  if defined(HAVE_STDINT_H)
52 #    include <stdint.h>
53 #  elif defined(HAVE_INTTYPES_H)
54 #    include <inttypes.h>
55 #  endif /* HAVE_STDINT/INTTYPES_H */
56 
57 #  if defined(HAVE_LIMITS_H)
58 #    include <limits.h>
59 #  elif defined(HAVE_SYS_LIMITS_H)
60 #    include <sys/limits.h>
61 #  endif /* HAVE_LIMITS/SYS_LIMITS_H */
62 
63 #  if defined(HAVE_SYSEXITS_H)
64 #    include <sysexits.h>
65 #  endif /* HAVE_SYSEXITS_H */
66 
67 #  if defined(HAVE_STDBOOL_H)
68 #    include <stdbool.h>
69 #  else
70      typedef enum { false = 0, true = 1 } _Bool;
71 #    define bool _Bool
72 
73      /* The other macros must be usable in preprocessor directives.  */
74 #    define false 0
75 #    define true 1
76 #  endif /* HAVE_SYSEXITS_H */
77 #endif /* COMPAT_H_GUARD */
78 // END-CONFIGURED-HEADERS
79 
80 /**
81  * Defined to abnormal value of EX_USAGE.  Used to indicate that paged usage
82  * was requested.  It is used to distinguish a --usage from a --help request.
83  * --usage is abbreviated and --help gives as much help as possible.
84  */
85 #define AO_EXIT_REQ_USAGE 10064
86 
87 /**
88  *  PUBLIC DEFINES
89  *
90  *  The following defines may be used in applications that need to test the
91  *  state of an option.  To test against these masks and values, a pointer
92  *  to an option descriptor must be obtained.  There are two ways:
93  *
94  *  1. inside an option processing procedure, it is the second argument,
95  *     conventionally "tOptDesc* pOD".
96  *
97  *  2. Outside of an option procedure (or to reference a different option
98  *     descriptor), use either "&DESC( opt_name )" or "&pfx_DESC( opt_name )".
99  *
100  *  See the relevant generated header file to determine which and what
101  *  values for "opt_name" are available.
102  * @group version
103  * @{
104  */
105 /// autoopts structure version
106 #define OPTIONS_STRUCT_VERSION      163841
107 /// autoopts structure version string
108 #define OPTIONS_VERSION_STRING      "40:1:15"
109 /// minimum version the autoopts library supports
110 #define OPTIONS_MINIMUM_VERSION     102400
111 /// minimum version the autoopts library supports as a string
112 #define OPTIONS_MIN_VER_STRING      "25:0:0"
113 /// the display version of the autoopts library, as a string
114 #define OPTIONS_DOTTED_VERSION      "40.1"
115 /// convert a version/release number pair to an integer value
116 #define OPTIONS_VER_TO_NUM(_v, _r)  (((_v) * 4096) + (_r))
117 /// @}
118 
119 /**
120  * Option argument types.  This must fit in the OPTST_ARG_TYPE_MASK
121  * field of the fOptState field of an option descriptor (tOptDesc).
122  * It will be a problem to extend beyond 4 bits.
123  */
124 typedef enum {
125     OPARG_TYPE_NONE         =  0, ///< does not take an argument
126     OPARG_TYPE_STRING       =  1, ///< default type/ vanilla string
127     OPARG_TYPE_ENUMERATION  =  2, ///< opt arg is an enum (keyword list)
128     OPARG_TYPE_BOOLEAN      =  3, ///< opt arg is boolean-valued
129     OPARG_TYPE_MEMBERSHIP   =  4, ///< opt arg sets set membership bits
130     OPARG_TYPE_NUMERIC      =  5, ///< opt arg is a long int
131     OPARG_TYPE_HIERARCHY    =  6, ///< option arg is hierarchical value
132     OPARG_TYPE_FILE         =  7, ///< option arg names a file
133     OPARG_TYPE_TIME         =  8, ///< opt arg is a time duration
134     OPARG_TYPE_FLOAT        =  9, ///< opt arg is a floating point num
135     OPARG_TYPE_DOUBLE       = 10, ///< opt arg is a double prec. float
136     OPARG_TYPE_LONG_DOUBLE  = 11, ///< opt arg is a long double prec.
137     OPARG_TYPE_LONG_LONG    = 12  ///< opt arg is a long long int
138 } teOptArgType;
139 
140 /**
141  * value descriptor for sub options
142  */
143 typedef struct optionValue {
144     teOptArgType        valType;        ///< which argument type
145     char *              pzName;         ///< name of the sub-option
146     union {
147         char            strVal[1];      ///< OPARG_TYPE_STRING
148         unsigned int    enumVal;        ///< OPARG_TYPE_ENUMERATION
149         unsigned int    boolVal;        ///< OPARG_TYPE_BOOLEAN
150         unsigned long   setVal;         ///< OPARG_TYPE_MEMBERSHIP
151         long            longVal;        ///< OPARG_TYPE_NUMERIC
152         void*           nestVal;        ///< OPARG_TYPE_HIERARCHY
153     } v;
154 } tOptionValue;
155 
156 /**
157  * file argument state and handling.
158  */
159 typedef enum {
160     FTYPE_MODE_MAY_EXIST        = 0x00, ///< may or may not exist
161     FTYPE_MODE_MUST_EXIST       = 0x01, ///< must pre-exist
162     FTYPE_MODE_MUST_NOT_EXIST   = 0x02, ///< must *not* pre-exist
163     FTYPE_MODE_EXIST_MASK       = 0x03, ///< mask for these bits
164     FTYPE_MODE_NO_OPEN          = 0x00, ///< leave file closed
165     FTYPE_MODE_OPEN_FD          = 0x10, ///< call open(2)
166     FTYPE_MODE_FOPEN_FP         = 0x20, ///< call fopen(3)
167     FTYPE_MODE_OPEN_MASK        = 0x30  ///< open/fopen/not open
168 } teOptFileType;
169 
170 /**
171  * the open flag bits or the mode string, depending on the open type.
172  */
173 typedef union {
174     int             file_flags;  ///< open(2) flag bits
175     char const *    file_mode;   ///< fopen(3) mode string
176 } tuFileMode;
177 
178 /// initial number of option argument holders to allocate
179 #define MIN_ARG_ALLOC_CT   6
180 /// amount by which to increment the argument holder allocation.
181 #define INCR_ARG_ALLOC_CT  8
182 /**
183  * an argument list.  When an option appears multiple times and
184  * the values get "stacked".  \a apzArgs  holds 8 pointers initially
185  * and is incremented by \a INCR_ARG_ALLOC_CT as needed.
186  */
187 typedef struct {
188     int             useCt;  ///< elements in use
189 
190     /// allocated elements, mininum \a MIN_ARG_ALLOC_CT
191     /// steps by \a INCR_ARG_ALLOC_CT
192     int             allocCt;
193     char const *    apzArgs[MIN_ARG_ALLOC_CT]; ///< element array
194 } tArgList;
195 
196 /**
197  *  Bits in the fOptState option descriptor field.
198  * @{
199  */
200 
201 /** integral type for holding opt_state masks */
202 typedef uint32_t opt_state_mask_t;
203 
204 #define OPTST_ARG_TYPE_SHIFT 12
205 /** bits defined for opt_state_mask_t */
206 /** Set via the "SET_OPT()" macro */
207 #define OPTST_SET              0x0000001U
208 /** Set via an RC/INI file */
209 #define OPTST_PRESET           0x0000002U
210 /** Set via a command line option */
211 #define OPTST_DEFINED          0x0000004U
212 /** Reset via command line option */
213 #define OPTST_RESET            0x0000008U
214 /** selected by equiv'ed option */
215 #define OPTST_EQUIVALENCE      0x0000010U
216 /** option is in disabled state */
217 #define OPTST_DISABLED         0x0000020U
218 /** pzOptArg was allocated */
219 #define OPTST_ALLOC_ARG        0x0000040U
220 /** option cannot be preset */
221 #define OPTST_NO_INIT          0x0000100U
222 /** opt value (flag) is any digit */
223 #define OPTST_NUMBER_OPT       0x0000200U
224 /** opt uses optionStackArg proc */
225 #define OPTST_STACKED          0x0000400U
226 /** option defaults to enabled */
227 #define OPTST_INITENABLED      0x0000800U
228 /** bit 1 of arg type enum */
229 #define OPTST_ARG_TYPE_1       0x0001000U
230 /** bit 2 of arg type enum */
231 #define OPTST_ARG_TYPE_2       0x0002000U
232 /** bit 3 of arg type enum */
233 #define OPTST_ARG_TYPE_3       0x0004000U
234 /** bit 4 of arg type enum */
235 #define OPTST_ARG_TYPE_4       0x0008000U
236 /** the option arg not required */
237 #define OPTST_ARG_OPTIONAL     0x0010000U
238 /** process opt on first pass */
239 #define OPTST_IMM              0x0020000U
240 /** process disablement immed. */
241 #define OPTST_DISABLE_IMM      0x0040000U
242 /** compiled out of program */
243 #define OPTST_OMITTED          0x0080000U
244 /** must be set or pre-set */
245 #define OPTST_MUST_SET         0x0100000U
246 /** opt is for doc only */
247 #define OPTST_DOCUMENT         0x0200000U
248 /** process opt twice - imm + reg */
249 #define OPTST_TWICE            0x0400000U
250 /** process disabled option twice */
251 #define OPTST_DISABLE_TWICE    0x0800000U
252 /** scaled integer value */
253 #define OPTST_SCALED_NUM       0x1000000U
254 /** disable from cmd line */
255 #define OPTST_NO_COMMAND       0x2000000U
256 /** support is being removed */
257 #define OPTST_DEPRECATED       0x4000000U
258 /** alias for other option */
259 #define OPTST_ALIAS            0x8000000U
260 
261 /** bits in SET mask:
262  *  set     preset  reset   defined */
263 #define OPTST_SET_MASK         0x000000FU
264 
265 /** bits in MUTABLE mask:
266  *  set         preset      reset       defined     equivalence disabled
267  *  alloc_arg */
268 #define OPTST_MUTABLE_MASK     0x000007FU
269 
270 /** bits omitted from PERSISTENT mask:
271  *  mutable_mask */
272 #define OPTST_PERSISTENT_MASK  0xFFFFF00U
273 
274 /** bits in SELECTED mask:
275  *  set     defined */
276 #define OPTST_SELECTED_MASK    0x0000005U
277 
278 /** bits in ARG_TYPE mask:
279  *  arg_type_1 arg_type_2 arg_type_3 arg_type_4 */
280 #define OPTST_ARG_TYPE_MASK    0x000F000U
281 
282 /** bits in NO_USAGE mask:
283  *  omitted    no_command deprecated */
284 #define OPTST_NO_USAGE_MASK    0x6080000U
285 
286 /** bits in IMMUTABLE mask:
287  *  document omitted */
288 #define OPTST_IMMUTABLE_MASK   0x0280000U
289 
290 /** bits in DO_NOT_SAVE mask:
291  *  document omitted  no_init */
292 #define OPTST_DO_NOT_SAVE_MASK 0x0280100U
293 
294 /** bits in NO_OUTPUT mask:
295  *  document omitted  alias */
296 #define OPTST_NO_OUTPUT_MASK   0x8280000U
297 
298 /** all bits in opt_state_mask_t masks */
299 #define OPTST_MASK_ALL         0xFFFFF7FU
300 
301 /** no bits in opt_state_mask_t */
302 #define OPTST_INIT             0x0000000U
303 /** @} */
304 
305 #ifdef NO_OPTIONAL_OPT_ARGS
306 # undef  OPTST_ARG_OPTIONAL
307 # define OPTST_ARG_OPTIONAL   0
308 #endif
309 
310 #define VENDOR_OPTION_VALUE   'W'
311 
312 #define SELECTED_OPT(_od)     ((_od)->fOptState  & OPTST_SELECTED_MASK)
313 #define UNUSED_OPT(  _od)     (((_od)->fOptState & OPTST_SET_MASK) == 0)
314 #define DISABLED_OPT(_od)     ((_od)->fOptState  & OPTST_DISABLED)
315 #define OPTION_STATE(_od)     ((_od)->fOptState)
316 #define OPTST_SET_ARGTYPE(_n) ((_n) << OPTST_ARG_TYPE_SHIFT)
317 #define OPTST_GET_ARGTYPE(_f) \
318     (((_f)&OPTST_ARG_TYPE_MASK) >> OPTST_ARG_TYPE_SHIFT)
319 
320 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
321  *
322  *  PRIVATE INTERFACES
323  *
324  *  The following values are used in the generated code to communicate
325  *  with the option library procedures.  They are not for public use
326  *  and may be subject to change.
327  */
328 
329 /**
330  *  Define the processing state flags
331  * @{
332  */
333 
334 /** integral type for holding proc_state masks */
335 typedef uint32_t proc_state_mask_t;
336 
337 /** bits defined for proc_state_mask_t */
338 /** Process long style options */
339 #define OPTPROC_LONGOPT       0x000001U
340 /** Process short style "flags" */
341 #define OPTPROC_SHORTOPT      0x000002U
342 /** Stop on argument errors */
343 #define OPTPROC_ERRSTOP       0x000004U
344 /** Current option is disabled */
345 #define OPTPROC_DISABLEDOPT   0x000008U
346 /** no options are required */
347 #define OPTPROC_NO_REQ_OPT    0x000010U
348 /** there is a number option */
349 #define OPTPROC_NUM_OPT       0x000020U
350 /** have inits been done? */
351 #define OPTPROC_INITDONE      0x000040U
352 /** any negation options? */
353 #define OPTPROC_NEGATIONS     0x000080U
354 /** check environment? */
355 #define OPTPROC_ENVIRON       0x000100U
356 /** Disallow remaining arguments */
357 #define OPTPROC_NO_ARGS       0x000200U
358 /** Require args after options */
359 #define OPTPROC_ARGS_REQ      0x000400U
360 /** reorder operands after opts */
361 #define OPTPROC_REORDER       0x000800U
362 /** emit usage in GNU style */
363 #define OPTPROC_GNUUSAGE      0x001000U
364 /** Translate strings in tOptions */
365 #define OPTPROC_TRANSLATE     0x002000U
366 /** no usage on usage error */
367 #define OPTPROC_MISUSE        0x004000U
368 /** immediate options active */
369 #define OPTPROC_IMMEDIATE     0x008000U
370 /** suppress for config only */
371 #define OPTPROC_NXLAT_OPT_CFG 0x010000U
372 /** suppress xlation always */
373 #define OPTPROC_NXLAT_OPT     0x020000U
374 /** vendor options active */
375 #define OPTPROC_VENDOR_OPT    0x040000U
376 /** opt processing in preset state */
377 #define OPTPROC_PRESETTING    0x080000U
378 /** Ignore pzFullUsage, compute usage text */
379 #define OPTPROC_COMPUTE       0x100000U
380 /** Program outputs digested option state for shell scripts.  Usage text
381   * always written to stderr */
382 #define OPTPROC_SHELL_OUTPUT  0x200000U
383 
384 /** bits in NO_XLAT mask:
385  *  nxlat_opt_cfg nxlat_opt */
386 #define OPTPROC_NO_XLAT_MASK  0x030000U
387 
388 /** all bits in proc_state_mask_t masks */
389 #define OPTPROC_MASK_ALL      0x3FFFFFU
390 
391 /** no bits in proc_state_mask_t */
392 #define OPTPROC_NONE          0x000000U
393 /** @} */
394 
395 #define STMTS(s)  do { s; } while (false)
396 
397 /**
398  *  Abbreviation for const memory character.
399  */
400 #define tCC         char const
401 
402 /**
403  * Magical values for the program's option pointer
404  * @{
405  */
406 typedef enum {
407     OP_VAL_EMIT_USAGE       = 1,  ///< request for usage
408     OP_VAL_EMIT_SHELL       = 2,  ///< emit value for Bourne shell evaluation
409     OP_VAL_RETURN_VALNAME   = 3,  ///< return the value as a string
410     OP_VAL_EMIT_LIMIT       = 15  ///< limit for magic values
411 } opt_proc_vals_t;
412 
413 /// \a OPT_VAL_EMIT_USAGE cast as a pointer
414 #define OPTPROC_EMIT_USAGE      ((tOptions *)OP_VAL_EMIT_USAGE)
415 
416 /// \a OPT_VAL_EMIT_SHELL cast as a pointer
417 #define OPTPROC_EMIT_SHELL      ((tOptions *)OP_VAL_EMIT_SHELL)
418 
419 /// \a OPT_VAL_RETURN_VALNAME cast as a pointer
420 #define OPTPROC_RETURN_VALNAME  ((tOptions *)OP_VAL_RETURN_VALNAME)
421 
422 /// \a OPT_VAL_EMIT_LIMIT cast as a pointer
423 #define OPTPROC_EMIT_LIMIT      ((tOptions *)OP_VAL_EMIT_LIMIT)
424 /** @} */
425 
426 /** group option processing procedure types
427  * @{
428  */
429 /** forward declaration for tOptDesc */
430 typedef struct opt_desc tOptDesc;
431 /** forward declaration for tOptiond */
432 typedef struct options  tOptions;
433 
434 /**
435  *  The option procedures do the special processing for each
436  *  option flag that needs it.
437  */
438 typedef void (tOptProc)(tOptions * pOpts, tOptDesc * pOptDesc);
439 
440 /**
441  * a pointer to an option processing procedure
442  */
443 typedef tOptProc * tpOptProc;
444 
445 /**
446  *  The usage procedure will never return.  It calls "exit(2)"
447  *  with the "exitCode" argument passed to it.
448  */
449 // coverity[+kill]
450 typedef void (tUsageProc)(tOptions* pOpts, int exitCode);
451 
452 /**
453  * a pointer to a procedure that prints usage and exits.
454  */
455 typedef tUsageProc * tpUsageProc;
456 /** @} */
457 
458 /**
459  *  Special definitions.  "NOLIMIT" is the 'max' value to use when
460  *  a flag may appear multiple times without limit.  "NO_EQUIVALENT"
461  *  is an illegal value for 'optIndex' (option description index).
462  * @{
463  */
464 #define NOLIMIT          USHRT_MAX  ///< no occurrance count limit
465 #define OPTION_LIMIT     SHRT_MAX   ///< maximum number of option types
466 /// option index to indicate no equivalance or alias
467 #define NO_EQUIVALENT    (OPTION_LIMIT+1)
468 /** @} */
469 
470 /**
471  * Option argument value.  Which is valid is determined by:
472  * (fOptState & OPTST_ARG_TYPE_MASK) >> OPTST_ARG_TYPE_SHIFT
473  * which will yield one of the teOptArgType values.
474  */
475 typedef union {
476     char const *    argString;  ///< as a string
477     uintptr_t       argEnum;    ///< as an enumeration value
478     uintptr_t       argIntptr;  ///< as an integer big enough to hold pointer
479     long            argInt;     ///< as a long integer
480     unsigned long   argUint;    ///< as an unsigned long ingeger
481     unsigned int    argBool;    ///< as a boolean value
482     FILE *          argFp;      ///< as a FILE * pointer
483     int             argFd;      ///< as a file descriptor (int)
484 } opt_arg_union_t;
485 
486 /// Compatibility define: \a pzLastArg is now \a optArg.argString
487 #define             pzLastArg       optArg.argString
488 /// The old amorphous argument bucket is now the opt_arg_union_t union.
489 #define             optArgBucket_t  opt_arg_union_t
490 
491 /**
492  * Enumeration of AutoOpts defined options.  The enumeration is used in
493  * marking each option that is defined by AutoOpts so libopts can find
494  * the correct descriptor.  This renders \a option_spec_idx_t redundant.
495  */
496 typedef enum {
497     AOUSE_USER_DEFINED = 0,     ///< user specified option
498     AOUSE_RESET_OPTION,         ///< reset option state option
499     AOUSE_VERSION,              ///< request version
500     AOUSE_HELP,                 ///< request usage help
501     AOUSE_MORE_HELP,            ///< request paged usage
502     AOUSE_USAGE,                ///< request short usage
503     AOUSE_SAVE_OPTS,            ///< save option state
504     AOUSE_LOAD_OPTS,            ///< load options from file
505     AOUSE_VENDOR_OPT            ///< specify a vendor option
506 } opt_usage_t;
507 
508 /**
509  *  Descriptor structure for each option.
510  *  Only the fields marked "PUBLIC" are for public use.
511  */
512 struct opt_desc {
513     /// Public, the index of this descriptor
514     uint16_t const      optIndex;
515     /// Public, the flag character (value)
516     uint16_t const      optValue;
517     /// Public, the index of the option used to activate option
518     uint16_t            optActualIndex;
519     /// Public, the flag character of the activating option
520     uint16_t            optActualValue;
521 
522     /// Public, the index of the equivalenced-to option.
523     /// This is NO_EQUIVALENT unless activated.
524     uint16_t const      optEquivIndex;
525     /// Private, the minimum occurrance count
526     uint16_t const      optMinCt;
527     /// Private, the maximum occurrance count (NOLIMIT, if unlimited)
528     uint16_t const      optMaxCt;
529     /// Public, the actual occurrance count
530     uint16_t            optOccCt;
531 
532     /// Public, the option processing state
533     opt_state_mask_t    fOptState;
534     /// Private, how the option is used (opt_usage_t)
535     uint32_t            optUsage;
536     /// Public, The current option argument value
537     opt_arg_union_t     optArg;
538     /// Public, data that is actually private to the code that handles
539     /// this particular option.  It is public IFF you have your own
540     /// handling function.
541     void *              optCookie;
542 
543     /// Private, a list of options that must be specified when this option
544     /// has been specified
545     int const  * const  pOptMust;
546 
547     /// Private, a list of options that cannot be specified when this option
548     /// has been specified
549     int const  * const  pOptCant;
550 
551     /// Private, the function to call for handling this option
552     tpOptProc    const  pOptProc;
553 
554     /// Private, usage information about this option
555     char const * const  pzText;
556 
557     /// Public, the UPPER CASE, shell variable name syntax name of the option
558     char const * const  pz_NAME;
559 
560     /// the unmodified name of the option
561     char const * const  pz_Name;
562 
563     /// the option name to use to disable the option.  Long options names
564     /// must be active.
565     char const * const  pz_DisableName;
566 
567     /// the special prefix that makes the normal option name become the
568     /// disablement name.
569     char const * const  pz_DisablePfx;
570 };
571 
572 /**
573  *  Some options need special processing, so we store their
574  *  indexes in a known place.
575  */
576 typedef struct {
577     uint16_t const  more_help;      ///< passes help text through pager
578     uint16_t const  save_opts;      ///< stores option state to a file
579     uint16_t const  number_option;  ///< the option "name" is an integer
580     /// all arguments are options, this is the default option that must
581     /// take an argument.  That argument is the unrecognized option.
582     uint16_t const  default_opt;
583 } option_spec_idx_t;
584 
585 /**
586  *  The procedure generated for translating option text
587  */
588 typedef void (tOptionXlateProc)(void);
589 
590 /**
591  * Everything marked "PUBLIC" is also marked "const".  Public access is not
592  * a license to modify.  Other fields are used and modified by the library.
593  * They are also subject to change without any notice.
594  * Do not even look at these outside of libopts.
595  */
596 struct options {
597     int const                   structVersion; ///< The version of this struct
598     unsigned int                origArgCt;     ///< program argument count
599     char **                     origArgVect;   ///< program argument vector
600     proc_state_mask_t           fOptSet;       ///< option proc. state flags
601     unsigned int                curOptIdx;     ///< current option index
602     char *                      pzCurOpt;      ///< current option text
603 
604     /// Public, the full path of the program
605     char const * const          pzProgPath;
606     /// Public, the name of the executable, without any path
607     char const * const          pzProgName;
608     /// Public, the upper-cased, shell variable syntax-ed program name
609     char const * const          pzPROGNAME;
610     /// the name of the "rc file" (configuration file)
611     char const * const          pzRcName;
612     /// the copyright text
613     char const * const          pzCopyright;
614     /// the full copyright notice
615     char const * const          pzCopyNotice;
616     /// a string with the program name, project name and version
617     char const * const          pzFullVersion;
618     /// a list of pointers to directories to search for the config file
619     char const * const *        const papzHomeList;
620     /// the title line for usage
621     char const * const          pzUsageTitle;
622     /// some added explanation for what this program is trying to do
623     char const * const          pzExplain;
624     /// a detailed explanation of the program's purpose, for use when
625     /// full help has been requested
626     char const * const          pzDetail;
627     /// The public array of option descriptors
628     tOptDesc   * const          pOptDesc;
629     /// the email address for reporting bugs
630     char const * const          pzBugAddr;
631 
632     /// Reserved for future use
633     void *                      pExtensions;
634     /// A copy of the option state when optionSaveState was called.
635     void *                      pSavedState;
636 
637     /// The procedure to call to print usage text
638     // coverity[+kill]
639     tpUsageProc                 pUsageProc;
640     /// The procedure to call to translate translatable option messages
641     tOptionXlateProc *          pTransProc;
642 
643     /// Special option indexes.
644     option_spec_idx_t           specOptIdx;
645     /// the total number of options for the program
646     int const                   optCt;
647     /// The number of "presettable" options, though some may be marked
648     /// "no-preset".  Includes all user specified options, plus a few
649     /// that are specified by AutoOpts.
650     int const                   presetOptCt;
651     /// user specified full usage text
652     char const *                pzFullUsage;
653     /// user specifed short usage (usage error triggered) message
654     char const *                pzShortUsage;
655     /// The option argument settings active when optionSaveState was called
656     opt_arg_union_t const * const originalOptArgArray;
657     /// any saved cookie value
658     void * const * const        originalOptArgCookie;
659     /// the package data directory (e.g. global configuration files)
660     char const * const          pzPkgDataDir;
661     /// email address of the project packager
662     char const * const          pzPackager;
663 };
664 
665 /*
666  *  Versions where in various fields first appear:
667  *  ($AO_CURRENT * 4096 + $AO_REVISION, but $AO_REVISION must be zero)
668  */
669 /**
670  * The version that first stored the original argument vector
671  */
672 #define originalOptArgArray_STRUCT_VERSION  0x20000 /* AO_CURRENT = 32 */
673 #define HAS_originalOptArgArray(_opt) \
674     ((_opt)->structVersion >= originalOptArgArray_STRUCT_VERSION)
675 
676 /**
677  * The version that first stored the package data directory
678  */
679 #define pzPkgDataDir_STRUCT_VERSION  0x22000 /* AO_CURRENT = 34 */
680 #define HAS_pzPkgDataDir(_opt) \
681     ((_opt)->structVersion >= pzPkgDataDir_STRUCT_VERSION)
682 
683 /**
684  * The version that first stored the option usage in each option descriptor
685  */
686 #define opt_usage_t_STRUCT_VERSION  0x26000 /* AO_CURRENT = 38 */
687 #define HAS_opt_usage_t(_opt) \
688     ((_opt)->structVersion >= opt_usage_t_STRUCT_VERSION)
689 
690 /**
691  *  "token list" structure returned by "string_tokenize()"
692  */
693 typedef struct {
694     unsigned long   tkn_ct;      ///< number of tokens found
695     unsigned char*  tkn_list[1]; ///< array of pointers to tokens
696 } token_list_t;
697 
698 /*
699  *  Hide the interface - it pollutes a POSIX claim, but leave it for
700  *  anyone #include-ing this header
701  */
702 #define strneqvcmp      option_strneqvcmp
703 #define streqvcmp       option_streqvcmp
704 #define streqvmap       option_streqvmap
705 #define strequate       option_strequate
706 #define strtransform    option_strtransform
707 
708 /**
709  *  Everything needed to be known about an mmap-ed file.
710  *
711  *  This is an output only structure used by text_mmap and text_munmap.
712  *  Clients must not alter the contents and must provide it to both
713  *  the text_mmap and text_munmap procedures.  BE ADVISED: if you are
714  *  mapping the file with PROT_WRITE the NUL byte at the end MIGHT NOT
715  *  BE WRITABLE.  In any event, that byte is not be written back
716  *  to the source file.  ALSO: if "txt_data" is valid and "txt_errno"
717  *  is not zero, then there *may* not be a terminating NUL.
718  */
719 typedef struct {
720     void *      txt_data;      ///< text file data
721     size_t      txt_size;      ///< actual file size
722     size_t      txt_full_size; ///< mmaped mem size
723     int         txt_fd;        ///< file descriptor
724     int         txt_zero_fd;   ///< fd for /dev/zero
725     int         txt_errno;     ///< warning code
726     int         txt_prot;      ///< "prot" flags
727     int         txt_flags;     ///< mapping type
728 } tmap_info_t;
729 
730 /**
731  * mmap result wrapper that yields "true" when mmap has failed.
732  */
733 #define TEXT_MMAP_FAILED_ADDR(a)  ((void*)(a) ==  (void*)MAP_FAILED)
734 
735 #ifdef  __cplusplus
736 #define CPLUSPLUS_OPENER extern "C" {
737 CPLUSPLUS_OPENER
738 #define CPLUSPLUS_CLOSER }
739 #else
740 #define CPLUSPLUS_CLOSER
741 #endif
742 
743 /**
744  *  The following routines may be coded into AutoOpts client code:
745  */
746 
747 /**
748  * ao_string_tokenize - tokenize an input string
749  *
750  *  This function will convert one input string into a list of strings.
751  *  The list of strings is derived by separating the input based on
752  *  white space separation.  However, if the input contains either single
753  *  or double quote characters, then the text after that character up to
754  *  a matching quote will become the string in the list.
755  *
756  *  The returned pointer should be deallocated with @code{free(3C)} when
757  *  are done using the data.  The data are placed in a single block of
758  *  allocated memory.  Do not deallocate individual token/strings.
759  *
760  *  The structure pointed to will contain at least these two fields:
761  *  @table @samp
762  *  @item tkn_ct
763  *  The number of tokens found in the input string.
764  *  @item tok_list
765  *  An array of @code{tkn_ct + 1} pointers to substring tokens, with
766  *  the last pointer set to NULL.
767  *  @end table
768  *
769  *  There are two types of quoted strings: single quoted (@code{'}) and
770  *  double quoted (@code{"}).  Singly quoted strings are fairly raw in that
771  *  escape characters (@code{\\}) are simply another character, except when
772  *  preceding the following characters:
773  *  @example
774  *  @code{\\}  double backslashes reduce to one
775  *  @code{'}   incorporates the single quote into the string
776  *  @code{\n}  suppresses both the backslash and newline character
777  *  @end example
778  *
779  *  Double quote strings are formed according to the rules of string
780  *  constants in ANSI-C programs.
781  *
782  * @param string       string to be tokenized
783  *
784  * @return token_list_t* - pointer to a structure that lists each token
785  */
786 extern token_list_t* ao_string_tokenize(char const*);
787 
788 
789 /**
790  * configFileLoad - parse a configuration file
791  *
792  *  This routine will load a named configuration file and parse the
793  *  text as a hierarchically valued option.  The option descriptor
794  *  created from an option definition file is not used via this interface.
795  *  The returned value is "named" with the input file name and is of
796  *  type "@code{OPARG_TYPE_HIERARCHY}".  It may be used in calls to
797  *  @code{optionGetValue()}, @code{optionNextValue()} and
798  *  @code{optionUnloadNested()}.
799  *
800  * @param fname        the file to load
801  *
802  * @return const tOptionValue* - An allocated, compound value structure
803  */
804 extern const tOptionValue* configFileLoad(char const*);
805 
806 
807 /**
808  * optionFileLoad - Load the locatable config files, in order
809  *
810  *  This function looks in all the specified directories for a configuration
811  *  file ("rc" file or "ini" file) and processes any found twice.  The first
812  *  time through, they are processed in reverse order (last file first).  At
813  *  that time, only "immediate action" configurables are processed.  For
814  *  example, if the last named file specifies not processing any more
815  *  configuration files, then no more configuration files will be processed.
816  *  Such an option in the @strong{first} named directory will have no effect.
817  *
818  *  Once the immediate action configurables have been handled, then the
819  *  directories are handled in normal, forward order.  In that way, later
820  *  config files can override the settings of earlier config files.
821  *
822  *  See the AutoOpts documentation for a thorough discussion of the
823  *  config file format.
824  *
825  *  Configuration files not found or not decipherable are simply ignored.
826  *
827  * @param opts         program options descriptor
828  * @param prog         program name
829  *
830  * @return int - 0 -> SUCCESS, -1 -> FAILURE
831  */
832 extern int optionFileLoad(tOptions*, char const*);
833 
834 
835 /**
836  * optionFindNextValue - find a hierarcicaly valued option instance
837  *
838  *  This routine will find the next entry in a nested value option or
839  *  configurable.  It will search through the list and return the next entry
840  *  that matches the criteria.
841  *
842  * @param odesc        an option with a nested arg type
843  * @param pPrevVal     the last entry
844  * @param name         name of value to find
845  * @param value        the matching value
846  *
847  * @return const tOptionValue* - a compound value structure
848  */
849 extern const tOptionValue* optionFindNextValue(const tOptDesc*, const tOptionValue*, char const*, char const*);
850 
851 
852 /**
853  * optionFindValue - find a hierarcicaly valued option instance
854  *
855  *  This routine will find an entry in a nested value option or configurable.
856  *  It will search through the list and return a matching entry.
857  *
858  * @param odesc        an option with a nested arg type
859  * @param name         name of value to find
860  * @param val          the matching value
861  *
862  * @return const tOptionValue* - a compound value structure
863  */
864 extern const tOptionValue* optionFindValue(const tOptDesc*, char const*, char const*);
865 
866 
867 /**
868  * optionFree - free allocated option processing memory
869  *
870  *  AutoOpts sometimes allocates memory and puts pointers to it in the
871  *  option state structures.  This routine deallocates all such memory.
872  *
873  * @param pOpts        program options descriptor
874  */
875 extern void optionFree(tOptions*);
876 
877 
878 /**
879  * optionGetValue - get a specific value from a hierarcical list
880  *
881  *  This routine will find an entry in a nested value option or configurable.
882  *  If "valueName" is NULL, then the first entry is returned.  Otherwise,
883  *  the first entry with a name that exactly matches the argument will be
884  *  returned.  If there is no matching value, NULL is returned and errno is
885  *  set to ENOENT. If the provided option value is not a hierarchical value,
886  *  NULL is also returned and errno is set to EINVAL.
887  *
888  * @param pOptValue    a hierarchcal value
889  * @param valueName    name of value to get
890  *
891  * @return const tOptionValue* - a compound value structure
892  */
893 extern const tOptionValue* optionGetValue(const tOptionValue*, char const*);
894 
895 
896 /**
897  * optionLoadLine - process a string for an option name and value
898  *
899  *  This is a client program callable routine for setting options from, for
900  *  example, the contents of a file that they read in.  Only one option may
901  *  appear in the text.  It will be treated as a normal (non-preset) option.
902  *
903  *  When passed a pointer to the option struct and a string, it will find
904  *  the option named by the first token on the string and set the option
905  *  argument to the remainder of the string.  The caller must NUL terminate
906  *  the string.  The caller need not skip over any introductory hyphens.
907  *  Any embedded new lines will be included in the option
908  *  argument.  If the input looks like one or more quoted strings, then the
909  *  input will be "cooked".  The "cooking" is identical to the string
910  *  formation used in AutoGen definition files (@pxref{basic expression}),
911  *  except that you may not use backquotes.
912  *
913  * @param opts         program options descriptor
914  * @param line         NUL-terminated text
915  */
916 extern void optionLoadLine(tOptions*, char const*);
917 
918 
919 /**
920  * optionMemberList - Get the list of members of a bit mask set
921  *
922  *  This converts the OPT_VALUE_name mask value to a allocated string.
923  *  It is the caller's responsibility to free the string.
924  *
925  * @param od           the set membership option description
926  *
927  * @return char* - the names of the set bits
928  */
929 extern char* optionMemberList(tOptDesc *);
930 
931 
932 /**
933  * optionNextValue - get the next value from a hierarchical list
934  *
935  *  This routine will return the next entry after the entry passed in.  At the
936  *  end of the list, NULL will be returned.  If the entry is not found on the
937  *  list, NULL will be returned and "@var{errno}" will be set to EINVAL.
938  *  The "@var{pOldValue}" must have been gotten from a prior call to this
939  *  routine or to "@code{opitonGetValue()}".
940  *
941  * @param pOptValue    a hierarchcal list value
942  * @param pOldValue    a value from this list
943  *
944  * @return const tOptionValue* - a compound value structure
945  */
946 extern const tOptionValue* optionNextValue(const tOptionValue*, const tOptionValue*);
947 
948 
949 /**
950  * optionOnlyUsage - Print usage text for just the options
951  *
952  *  This routine will print only the usage for each option.
953  *  This function may be used when the emitted usage must incorporate
954  *  information not available to AutoOpts.
955  *
956  * @param pOpts        program options descriptor
957  * @param ex_code      exit code for calling exit(3)
958  */
959 extern void optionOnlyUsage(tOptions*, int);
960 
961 
962 /**
963  * optionProcess - this is the main option processing routine
964  *
965  *  This is the main entry point for processing options.  It is intended
966  *  that this procedure be called once at the beginning of the execution of
967  *  a program.  Depending on options selected earlier, it is sometimes
968  *  necessary to stop and restart option processing, or to select completely
969  *  different sets of options.  This can be done easily, but you generally
970  *  do not want to do this.
971  *
972  *  The number of arguments processed always includes the program name.
973  *  If one of the arguments is "--", then it is counted and the processing
974  *  stops.  If an error was encountered and errors are to be tolerated, then
975  *  the returned value is the index of the argument causing the error.
976  *  A hyphen by itself ("-") will also cause processing to stop and will
977  *  @emph{not} be counted among the processed arguments.  A hyphen by itself
978  *  is treated as an operand.  Encountering an operand stops option
979  *  processing.
980  *
981  * @param opts         program options descriptor
982  * @param a_ct         program arg count
983  * @param a_v          program arg vector
984  *
985  * @return int - the count of the arguments processed
986  */
987 extern int optionProcess(tOptions*, int, char**);
988 
989 
990 /**
991  * optionRestore - restore option state from memory copy
992  *
993  *  Copy back the option state from saved memory.
994  *  The allocated memory is left intact, so this routine can be
995  *  called repeatedly without having to call optionSaveState again.
996  *  If you are restoring a state that was saved before the first call
997  *  to optionProcess(3AO), then you may change the contents of the
998  *  argc/argv parameters to optionProcess.
999  *
1000  * @param pOpts        program options descriptor
1001  */
1002 extern void optionRestore(tOptions*);
1003 
1004 
1005 /**
1006  * optionSaveFile - saves the option state to a file
1007  *
1008  *  This routine will save the state of option processing to a file.  The name
1009  *  of that file can be specified with the argument to the @code{--save-opts}
1010  *  option, or by appending the @code{rcfile} attribute to the last
1011  *  @code{homerc} attribute.  If no @code{rcfile} attribute was specified, it
1012  *  will default to @code{.@i{programname}rc}.  If you wish to specify another
1013  *  file, you should invoke the @code{SET_OPT_SAVE_OPTS(@i{filename})} macro.
1014  *
1015  *  The recommend usage is as follows:
1016  *  @example
1017  *  optionProcess(&progOptions, argc, argv);
1018  *  if (i_want_a_non_standard_place_for_this)
1019  *  SET_OPT_SAVE_OPTS("myfilename");
1020  *  optionSaveFile(&progOptions);
1021  *  @end example
1022  *
1023  * @param opts         program options descriptor
1024  */
1025 extern void optionSaveFile(tOptions*);
1026 
1027 
1028 /**
1029  * optionSaveState - saves the option state to memory
1030  *
1031  *  This routine will allocate enough memory to save the current option
1032  *  processing state.  If this routine has been called before, that memory
1033  *  will be reused.  You may only save one copy of the option state.  This
1034  *  routine may be called before optionProcess(3AO).  If you do call it
1035  *  before the first call to optionProcess, then you may also change the
1036  *  contents of argc/argv after you call optionRestore(3AO)
1037  *
1038  *  In fact, more strongly put: it is safest to only use this function
1039  *  before having processed any options.  In particular, the saving and
1040  *  restoring of stacked string arguments and hierarchical values is
1041  *  disabled.  The values are not saved.
1042  *
1043  * @param pOpts        program options descriptor
1044  */
1045 extern void optionSaveState(tOptions*);
1046 
1047 
1048 /**
1049  * optionUnloadNested - Deallocate the memory for a nested value
1050  *
1051  *  A nested value needs to be deallocated.  The pointer passed in should
1052  *  have been gotten from a call to @code{configFileLoad()} (See
1053  *  @pxref{libopts-configFileLoad}).
1054  *
1055  * @param pOptVal      the hierarchical value
1056  */
1057 extern void optionUnloadNested(tOptionValue const *);
1058 
1059 
1060 /**
1061  * optionVersion - return the compiled AutoOpts version number
1062  *
1063  *  Returns the full version string compiled into the library.
1064  *  The returned string cannot be modified.
1065  *
1066  * @return char const* - the version string in constant memory
1067  */
1068 extern char const* optionVersion(void);
1069 
1070 
1071 /**
1072  * strequate - map a list of characters to the same value
1073  *
1074  *  Each character in the input string get mapped to the first character
1075  *  in the string.
1076  *  This function name is mapped to option_strequate so as to not conflict
1077  *  with the POSIX name space.
1078  *
1079  * @param ch_list      characters to equivalence
1080  */
1081 extern void strequate(char const*);
1082 
1083 
1084 /**
1085  * streqvcmp - compare two strings with an equivalence mapping
1086  *
1087  *  Using a character mapping, two strings are compared for "equivalence".
1088  *  Each input character is mapped to a comparison character and the
1089  *  mapped-to characters are compared for the two NUL terminated input strings.
1090  *  This function name is mapped to option_streqvcmp so as to not conflict
1091  *  with the POSIX name space.
1092  *
1093  * @param str1         first string
1094  * @param str2         second string
1095  *
1096  * @return int - the difference between two differing characters
1097  */
1098 extern int streqvcmp(char const*, char const*);
1099 
1100 
1101 /**
1102  * streqvmap - Set the character mappings for the streqv functions
1103  *
1104  *  Set the character mapping.  If the count (@code{ct}) is set to zero, then
1105  *  the map is cleared by setting all entries in the map to their index
1106  *  value.  Otherwise, the "@code{From}" character is mapped to the "@code{To}"
1107  *  character.  If @code{ct} is greater than 1, then @code{From} and @code{To}
1108  *  are incremented and the process repeated until @code{ct} entries have been
1109  *  set. For example,
1110  *  @example
1111  *  streqvmap('a', 'A', 26);
1112  *  @end example
1113  *  @noindent
1114  *  will alter the mapping so that all English lower case letters
1115  *  will map to upper case.
1116  *
1117  *  This function name is mapped to option_streqvmap so as to not conflict
1118  *  with the POSIX name space.
1119  *
1120  * @param from         Input character
1121  * @param to           Mapped-to character
1122  * @param ct           compare length
1123  */
1124 extern void streqvmap(char, char, int);
1125 
1126 
1127 /**
1128  * strneqvcmp - compare two strings with an equivalence mapping
1129  *
1130  *  Using a character mapping, two strings are compared for "equivalence".
1131  *  Each input character is mapped to a comparison character and the
1132  *  mapped-to characters are compared for the two NUL terminated input strings.
1133  *  The comparison is limited to @code{ct} bytes.
1134  *  This function name is mapped to option_strneqvcmp so as to not conflict
1135  *  with the POSIX name space.
1136  *
1137  * @param str1         first string
1138  * @param str2         second string
1139  * @param ct           compare length
1140  *
1141  * @return int - the difference between two differing characters
1142  */
1143 extern int strneqvcmp(char const*, char const*, int);
1144 
1145 
1146 /**
1147  * strtransform - convert a string into its mapped-to value
1148  *
1149  *  Each character in the input string is mapped and the mapped-to
1150  *  character is put into the output.
1151  *  This function name is mapped to option_strtransform so as to not conflict
1152  *  with the POSIX name space.
1153  *
1154  *  The source and destination may be the same.
1155  *
1156  * @param dest         output string
1157  * @param src          input string
1158  */
1159 extern void strtransform(char*, char const*);
1160 
1161 /*  AutoOpts PRIVATE FUNCTIONS:  */
1162 tOptProc optionStackArg, optionUnstackArg, optionBooleanVal, optionNumericVal;
1163 
1164 extern char* ao_string_cook(char*, int*);
1165 
1166 extern unsigned int ao_string_cook_escape_char(char const*, char*, unsigned int);
1167 
1168 extern void genshelloptUsage(tOptions*, int);
1169 
1170 extern int optionAlias(tOptions *, tOptDesc *, unsigned int);
1171 
1172 extern void optionBooleanVal(tOptions*, tOptDesc*);
1173 
1174 extern uintptr_t optionEnumerationVal(tOptions*, tOptDesc*, char const * const *, unsigned int);
1175 
1176 extern void optionFileCheck(tOptions*, tOptDesc*, teOptFileType, tuFileMode);
1177 
1178 extern char const * optionKeywordName(tOptDesc*, unsigned int);
1179 
1180 extern void optionLoadOpt(tOptions*, tOptDesc*);
1181 
1182 extern bool optionMakePath(char*, int, char const*, char const*);
1183 
1184 extern void optionNestedVal(tOptions*, tOptDesc*);
1185 
1186 extern void optionNumericVal(tOptions*, tOptDesc*);
1187 
1188 extern void optionPagedUsage(tOptions *, tOptDesc *);
1189 
1190 extern void optionParseShell(tOptions*);
1191 
1192 extern void optionPrintParagraphs(char const *, bool, FILE *);
1193 
1194 extern void optionPrintVersion(tOptions*, tOptDesc*);
1195 
1196 extern void optionPutShell(tOptions*);
1197 
1198 extern char const * optionQuoteString(char const *, char const *);
1199 
1200 extern void optionResetOpt(tOptions*, tOptDesc*);
1201 
1202 extern void optionSetMembers(tOptions*, tOptDesc*, char const * const *, unsigned int);
1203 
1204 extern void optionShowRange(tOptions*, tOptDesc*, const void *, int);
1205 
1206 extern void optionStackArg(tOptions*, tOptDesc*);
1207 
1208 extern void optionTimeDate(tOptions*, tOptDesc*);
1209 
1210 extern void optionTimeVal(tOptions*, tOptDesc*);
1211 
1212 extern void optionUnstackArg(tOptions*, tOptDesc*);
1213 
1214 extern void optionUsage(tOptions*, int);
1215 
1216 extern void optionVendorOption(tOptions *, tOptDesc *);
1217 
1218 extern void optionVersionStderr(tOptions*, tOptDesc*);
1219 
1220 extern void* text_mmap(char const*, int, int, tmap_info_t*);
1221 
1222 extern int text_munmap(tmap_info_t*);
1223 
1224 CPLUSPLUS_CLOSER
1225 #endif /* AUTOOPTS_OPTIONS_H_GUARD */
1226 /** @}
1227  *
1228  * Local Variables:
1229  * c-file-style: "stroustrup"
1230  * indent-tabs-mode: nil
1231  * End:
1232  * options.h ends here */
1233