5 /* (C) 1998-2000 Red Hat, Inc. -- Licensing details are in the COPYING
6 file accompanying popt source distributions, available from
7 ftp://ftp.rpm.org/pub/rpm/dist. */
12 #include <stdio.h> /* for FILE * */
14 #define POPT_OPTION_DEPTH 10
17 * \name Arg type identifiers
20 #define POPT_ARG_NONE 0U /*!< no arg */
21 #define POPT_ARG_STRING 1U /*!< arg will be saved as string */
22 #define POPT_ARG_INT 2U /*!< arg ==> int */
23 #define POPT_ARG_LONG 3U /*!< arg ==> long */
24 #define POPT_ARG_INCLUDE_TABLE 4U /*!< arg points to table */
25 #define POPT_ARG_CALLBACK 5U /*!< table-wide callback... must be
26 set first in table; arg points
27 to callback, descrip points to
28 callback data to pass */
29 #define POPT_ARG_INTL_DOMAIN 6U /*!< set the translation domain
30 for this table and any
31 included tables; arg points
32 to the domain string */
33 #define POPT_ARG_VAL 7U /*!< arg should take value val */
34 #define POPT_ARG_FLOAT 8U /*!< arg ==> float */
35 #define POPT_ARG_DOUBLE 9U /*!< arg ==> double */
36 #define POPT_ARG_LONGLONG 10U /*!< arg ==> long long */
38 #define POPT_ARG_MAINCALL 16U+11U /*!< EXPERIMENTAL: return (*arg) (argc, argv) */
39 #define POPT_ARG_ARGV 12U /*!< dupe'd arg appended to realloc'd argv array. */
40 #define POPT_ARG_SHORT 13U /*!< arg ==> short */
41 #define POPT_ARG_BITSET 16U+14U /*!< arg ==> bit set */
43 #define POPT_ARG_MASK 0x000000FFU
44 #define POPT_GROUP_MASK 0x0000FF00U
52 #define POPT_ARGFLAG_ONEDASH 0x80000000U /*!< allow -longoption */
53 #define POPT_ARGFLAG_DOC_HIDDEN 0x40000000U /*!< don't show in help/usage */
54 #define POPT_ARGFLAG_STRIP 0x20000000U /*!< strip this arg from argv(only applies to long args) */
55 #define POPT_ARGFLAG_OPTIONAL 0x10000000U /*!< arg may be missing */
57 #define POPT_ARGFLAG_OR 0x08000000U /*!< arg will be or'ed */
58 #define POPT_ARGFLAG_NOR 0x09000000U /*!< arg will be nor'ed */
59 #define POPT_ARGFLAG_AND 0x04000000U /*!< arg will be and'ed */
60 #define POPT_ARGFLAG_NAND 0x05000000U /*!< arg will be nand'ed */
61 #define POPT_ARGFLAG_XOR 0x02000000U /*!< arg will be xor'ed */
62 #define POPT_ARGFLAG_NOT 0x01000000U /*!< arg will be negated */
63 #define POPT_ARGFLAG_LOGICALOPS \
64 (POPT_ARGFLAG_OR|POPT_ARGFLAG_AND|POPT_ARGFLAG_XOR)
66 #define POPT_BIT_SET (POPT_ARG_VAL|POPT_ARGFLAG_OR)
67 /*!< set arg bit(s) */
68 #define POPT_BIT_CLR (POPT_ARG_VAL|POPT_ARGFLAG_NAND)
69 /*!< clear arg bit(s) */
71 #define POPT_ARGFLAG_SHOW_DEFAULT 0x00800000U /*!< show default value in --help */
72 #define POPT_ARGFLAG_RANDOM 0x00400000U /*!< random value in [1,arg] */
73 #define POPT_ARGFLAG_TOGGLE 0x00200000U /*!< permit --[no]opt prefix toggle */
78 * \name Callback modifiers
81 #define POPT_CBFLAG_PRE 0x80000000U /*!< call the callback before parse */
82 #define POPT_CBFLAG_POST 0x40000000U /*!< call the callback after parse */
83 #define POPT_CBFLAG_INC_DATA 0x20000000U /*!< use data from the include line,
85 #define POPT_CBFLAG_SKIPOPTION 0x10000000U /*!< don't callback with option */
86 #define POPT_CBFLAG_CONTINUE 0x08000000U /*!< continue callbacks with option */
90 * \name Error return values
93 #define POPT_ERROR_NOARG -10 /*!< missing argument */
94 #define POPT_ERROR_BADOPT -11 /*!< unknown option */
95 #define POPT_ERROR_OPTSTOODEEP -13 /*!< aliases nested too deeply */
96 #define POPT_ERROR_BADQUOTE -15 /*!< error in paramter quoting */
97 #define POPT_ERROR_ERRNO -16 /*!< errno set, use strerror(errno) */
98 #define POPT_ERROR_BADNUMBER -17 /*!< invalid numeric value */
99 #define POPT_ERROR_OVERFLOW -18 /*!< number too large or too small */
100 #define POPT_ERROR_BADOPERATION -19 /*!< mutually exclusive logical operations requested */
101 #define POPT_ERROR_NULLARG -20 /*!< opt->arg should not be NULL */
102 #define POPT_ERROR_MALLOC -21 /*!< memory allocation failed */
103 #define POPT_ERROR_BADCONFIG -22 /*!< config file failed sanity test */
107 * \name poptBadOption() flags
110 #define POPT_BADOPTION_NOALIAS (1U << 0) /*!< don't go into an alias */
114 * \name poptGetContext() flags
117 #define POPT_CONTEXT_NO_EXEC (1U << 0) /*!< ignore exec expansions */
118 #define POPT_CONTEXT_KEEP_FIRST (1U << 1) /*!< pay attention to argv[0] */
119 #define POPT_CONTEXT_POSIXMEHARDER (1U << 2) /*!< options can't follow args */
120 #define POPT_CONTEXT_ARG_OPTS (1U << 4) /*!< return args as options with value 0 */
126 /*@observer@*/ /*@null@*/
127 const char * longName
; /*!< may be NULL */
128 char shortName
; /*!< may be NUL */
129 unsigned int argInfo
;
130 /*@shared@*/ /*@null@*/
131 void * arg
; /*!< depends on argInfo */
132 int val
; /*!< 0 means don't return, just update flag */
133 /*@observer@*/ /*@null@*/
134 const char * descrip
; /*!< description for autohelp -- may be NULL */
135 /*@observer@*/ /*@null@*/
136 const char * argDescrip
; /*!< argument description for autohelp */
140 * A popt alias argument for poptAddAlias().
143 /*@owned@*/ /*@null@*/
144 const char * longName
; /*!< may be NULL */
145 char shortName
; /*!< may be NUL */
148 const char ** argv
; /*!< must be free()able */
152 * A popt alias or exec argument for poptAddItem().
155 typedef struct poptItem_s
{
156 struct poptOption option
; /*!< alias/exec name(s) and description. */
157 int argc
; /*!< (alias) no. of args. */
159 const char ** argv
; /*!< (alias) args, must be free()able. */
164 * \name Auto-generated help/usage
169 * Empty table marker to enable displaying popt alias/exec options.
172 /*@unchecked@*/ /*@observer@*/
173 extern struct poptOption poptAliasOptions
[];
175 #define POPT_AUTOALIAS { NULL, '\0', POPT_ARG_INCLUDE_TABLE, poptAliasOptions, \
176 0, "Options implemented via popt alias/exec:", NULL },
179 * Auto help table options.
182 /*@unchecked@*/ /*@observer@*/
183 extern struct poptOption poptHelpOptions
[];
187 /*@unchecked@*/ /*@observer@*/
188 extern struct poptOption
* poptHelpOptionsI18N
;
191 #define POPT_AUTOHELP { NULL, '\0', POPT_ARG_INCLUDE_TABLE, poptHelpOptions, \
192 0, "Help options:", NULL },
194 #define POPT_TABLEEND { NULL, '\0', 0, NULL, 0, NULL, NULL }
200 typedef /*@abstract@*/ struct poptContext_s
* poptContext
;
206 /*@-exporttype -typeuse@*/
207 typedef struct poptOption
* poptOption
;
208 /*@=exporttype =typeuse@*/
214 enum poptCallbackReason
{
215 POPT_CALLBACK_REASON_PRE
= 0,
216 POPT_CALLBACK_REASON_POST
= 1,
217 POPT_CALLBACK_REASON_OPTION
= 2
227 * Table callback prototype.
229 * @param reason reason for callback
230 * @param opt option that triggered callback
231 * @param arg @todo Document.
232 * @param data @todo Document.
234 typedef void (*poptCallbackType
) (poptContext con
,
235 enum poptCallbackReason reason
,
236 /*@null@*/ const struct poptOption
* opt
,
237 /*@null@*/ const char * arg
,
238 /*@null@*/ const void * data
)
239 /*@globals internalState @*/
240 /*@modifies internalState @*/;
245 * @return NULL always
248 poptContext
poptFreeContext( /*@only@*/ /*@null@*/ poptContext con
)
252 * Initialize popt context.
253 * @param name context name (usually argv[0] program name)
254 * @param argc no. of arguments
255 * @param argv argument array
256 * @param options address of popt option table
257 * @param flags or'd POPT_CONTEXT_* bits
258 * @return initialized popt context
260 /*@only@*/ /*@null@*/
261 poptContext
poptGetContext(
262 /*@dependent@*/ /*@keep@*/ const char * name
,
263 int argc
, /*@dependent@*/ /*@keep@*/ const char ** argv
,
264 /*@dependent@*/ /*@keep@*/ const struct poptOption
* options
,
266 /*@globals internalState @*/
267 /*@modifies internalState @*/;
270 * Destroy context (alternative implementation).
272 * @return NULL always
275 poptContext
poptFini( /*@only@*/ /*@null@*/ poptContext con
)
279 * Initialize popt context (alternative implementation).
280 * This routine does poptGetContext() and then poptReadConfigFiles().
281 * @param argc no. of arguments
282 * @param argv argument array
283 * @param options address of popt option table
284 * @param configPaths colon separated file path(s) to read.
285 * @return initialized popt context (NULL on error).
287 /*@only@*/ /*@null@*/ /*@unused@*/
288 poptContext
poptInit(int argc
, /*@dependent@*/ /*@keep@*/ const char ** argv
,
289 /*@dependent@*/ /*@keep@*/ const struct poptOption
* options
,
290 /*@null@*/ const char * configPaths
)
291 /*@globals fileSystem, internalState @*/
292 /*@modifies fileSystem, internalState @*/;
295 * Reinitialize popt context.
299 void poptResetContext(/*@null@*/poptContext con
)
303 * Return value of next option found.
305 * @return next option val, -1 on last item, POPT_ERROR_* on error
307 int poptGetNextOpt(/*@null@*/poptContext con
)
308 /*@globals fileSystem, internalState @*/
309 /*@modifies con, fileSystem, internalState @*/;
312 * Return next option argument (if any).
314 * @return option argument, NULL if no argument is available
316 /*@observer@*/ /*@null@*/ /*@unused@*/
317 char * poptGetOptArg(/*@null@*/poptContext con
)
321 * Return next argument.
323 * @return next argument, NULL if no argument is available
325 /*@observer@*/ /*@null@*/ /*@unused@*/
326 const char * poptGetArg(/*@null@*/poptContext con
)
330 * Peek at current argument.
332 * @return current argument, NULL if no argument is available
334 /*@observer@*/ /*@null@*/ /*@unused@*/
335 const char * poptPeekArg(/*@null@*/poptContext con
)
339 * Return remaining arguments.
341 * @return argument array, NULL terminated
343 /*@observer@*/ /*@null@*/
344 const char ** poptGetArgs(/*@null@*/poptContext con
)
348 * Return the option which caused the most recent error.
351 * @return offending option
354 const char * poptBadOption(/*@null@*/poptContext con
, unsigned int flags
)
358 * Add arguments to context.
360 * @param argv argument array, NULL terminated
361 * @return 0 on success, POPT_ERROR_OPTSTOODEEP on failure
364 int poptStuffArgs(poptContext con
, /*@keep@*/ const char ** argv
)
368 * Add alias to context.
369 * @todo Pass alias by reference, not value.
370 * @deprecated Use poptAddItem instead.
372 * @param alias alias to add
373 * @param flags (unused)
374 * @return 0 on success
377 int poptAddAlias(poptContext con
, struct poptAlias alias
, int flags
)
381 * Add alias/exec item to context.
383 * @param newItem alias/exec item to add
384 * @param flags 0 for alias, 1 for exec
385 * @return 0 on success
387 int poptAddItem(poptContext con
, poptItem newItem
, int flags
)
391 * Perform sanity checks on a file path.
392 * @param fn file name
393 * @return 0 on OK, 1 on NOTOK.
395 int poptSaneFile(const char * fn
)
396 /*@globals errno, internalState @*/
397 /*@modifies errno, internalState @*/;
400 * Read a file into a buffer.
401 * @param fn file name
402 * @retval *bp buffer (malloc'd) (or NULL)
403 * @retval *nbp no. of bytes in buffer (including final NUL) (or NULL)
404 * @param flags 1 to trim escaped newlines
405 * return 0 on success
407 int poptReadFile(const char * fn
, /*@null@*/ /*@out@*/ char ** bp
,
408 /*@null@*/ /*@out@*/ size_t * nbp
, int flags
)
409 /*@globals errno, fileSystem, internalState @*/
410 /*@modifies *bp, *nbp, errno, fileSystem, internalState @*/;
411 #define POPT_READFILE_TRIMNEWLINES 1
414 * Read configuration file.
416 * @param fn file name to read
417 * @return 0 on success, POPT_ERROR_ERRNO on failure
419 int poptReadConfigFile(poptContext con
, const char * fn
)
420 /*@globals errno, fileSystem, internalState @*/
421 /*@modifies con->execs, con->numExecs,
422 errno, fileSystem, internalState @*/;
425 * Read configuration file(s).
426 * Colon separated files to read, looping over poptReadConfigFile().
427 * Note that an '@' character preceeding a path in the list will
428 * also perform additional sanity checks on the file before reading.
430 * @param paths colon separated file name(s) to read
431 * @return 0 on success, POPT_ERROR_BADCONFIG on failure
433 int poptReadConfigFiles(poptContext con
, /*@null@*/ const char * paths
)
434 /*@globals errno, fileSystem, internalState @*/
435 /*@modifies con->execs, con->numExecs,
436 errno, fileSystem, internalState @*/;
439 * Read default configuration from /etc/popt and $HOME/.popt.
441 * @param useEnv (unused)
442 * @return 0 on success, POPT_ERROR_ERRNO on failure
445 int poptReadDefaultConfig(poptContext con
, /*@unused@*/ int useEnv
)
446 /*@globals fileSystem, internalState @*/
447 /*@modifies con->execs, con->numExecs,
448 fileSystem, internalState @*/;
451 * Duplicate an argument array.
452 * @note: The argument array is malloc'd as a single area, so only argv must
455 * @param argc no. of arguments
456 * @param argv argument array
457 * @retval argcPtr address of returned no. of arguments
458 * @retval argvPtr address of returned argument array
459 * @return 0 on success, POPT_ERROR_NOARG on failure
461 int poptDupArgv(int argc
, /*@null@*/ const char **argv
,
462 /*@null@*/ /*@out@*/ int * argcPtr
,
463 /*@null@*/ /*@out@*/ const char *** argvPtr
)
464 /*@modifies *argcPtr, *argvPtr @*/;
467 * Parse a string into an argument array.
468 * The parse allows ', ", and \ quoting, but ' is treated the same as " and
469 * both may include \ quotes.
470 * @note: The argument array is malloc'd as a single area, so only argv must
473 * @param s string to parse
474 * @retval argcPtr address of returned no. of arguments
475 * @retval argvPtr address of returned argument array
477 int poptParseArgvString(const char * s
,
478 /*@out@*/ int * argcPtr
, /*@out@*/ const char *** argvPtr
)
479 /*@modifies *argcPtr, *argvPtr @*/;
482 * Parses an input configuration file and returns an string that is a
483 * command line. For use with popt. You must free the return value when done.
487 # this line is ignored
497 reall bad line = again
499 test = with lots of spaces
504 --aaa --bbb --ccc --bla="bla" --this_is="fdsafdas" --5555="55555" --test="with lots of spaces"
507 * Passing this to poptParseArgvString() yields an argv of:
515 '--test=with lots of spaces'
518 * @bug NULL is returned if file line is too long.
519 * @bug Silently ignores invalid lines.
521 * @param fp file handle to read
522 * @param *argstrp return string of options (malloc'd)
523 * @param flags unused
524 * @return 0 on success
525 * @see poptParseArgvString
528 int poptConfigFileToString(FILE *fp
, /*@out@*/ char ** argstrp
, int flags
)
529 /*@globals fileSystem @*/
530 /*@modifies *fp, *argstrp, fileSystem @*/;
534 * Return formatted error string for popt failure.
535 * @param error popt error
536 * @return error string
539 const char * poptStrerror(const int error
)
543 * Limit search for executables.
545 * @param path single path to search for executables
546 * @param allowAbsolute absolute paths only?
549 void poptSetExecPath(poptContext con
, const char * path
, int allowAbsolute
)
553 * Print detailed description of options.
555 * @param fp ouput file handle
556 * @param flags (unused)
558 void poptPrintHelp(poptContext con
, FILE * fp
, /*@unused@*/ int flags
)
559 /*@globals fileSystem @*/
560 /*@modifies fp, fileSystem @*/;
563 * Print terse description of options.
565 * @param fp ouput file handle
566 * @param flags (unused)
568 void poptPrintUsage(poptContext con
, FILE * fp
, /*@unused@*/ int flags
)
569 /*@globals fileSystem @*/
570 /*@modifies fp, fileSystem @*/;
573 * Provide text to replace default "[OPTION...]" in help/usage output.
575 * @param text replacement text
578 void poptSetOtherOptionHelp(poptContext con
, const char * text
)
583 * Return argv[0] from context.
589 const char * poptGetInvocationName(poptContext con
)
594 * Shuffle argv pointers to remove stripped args, returns new argc.
596 * @param argc no. of args
597 * @param argv arg vector
601 int poptStrippedArgv(poptContext con
, int argc
, char ** argv
)
602 /*@modifies *argv @*/;
606 * Add a string to an argv array.
607 * @retval *argvp argv array
608 * @param argInfo (unused)
609 * @param val string arg to add (using strdup)
610 * @return 0 on success, POPT_ERROR_NULLARG/POPT_ERROR_BADOPERATION
613 int poptSaveString(/*@null@*/ const char *** argvp
, unsigned int argInfo
,
614 /*@null@*/const char * val
)
615 /*@modifies *argvp @*/;
618 * Save a long long, performing logical operation with value.
619 * @warning Alignment check may be too strict on certain platorms.
620 * @param arg integer pointer, aligned on int boundary.
621 * @param argInfo logical operation (see POPT_ARGFLAG_*)
622 * @param aLongLong value to use
623 * @return 0 on success, POPT_ERROR_NULLARG/POPT_ERROR_BADOPERATION
627 int poptSaveLongLong(/*@null@*/ long long * arg
, unsigned int argInfo
,
629 /*@globals internalState @*/
630 /*@modifies *arg, internalState @*/
631 /*@requires maxSet(arg) >= 0 /\ maxRead(arg) == 0 @*/;
635 * Save a long, performing logical operation with value.
636 * @warning Alignment check may be too strict on certain platorms.
637 * @param arg integer pointer, aligned on int boundary.
638 * @param argInfo logical operation (see POPT_ARGFLAG_*)
639 * @param aLong value to use
640 * @return 0 on success, POPT_ERROR_NULLARG/POPT_ERROR_BADOPERATION
644 int poptSaveLong(/*@null@*/ long * arg
, unsigned int argInfo
, long aLong
)
645 /*@globals internalState @*/
646 /*@modifies *arg, internalState @*/
647 /*@requires maxSet(arg) >= 0 /\ maxRead(arg) == 0 @*/;
651 * Save a short integer, performing logical operation with value.
652 * @warning Alignment check may be too strict on certain platorms.
653 * @param arg short pointer, aligned on short boundary.
654 * @param argInfo logical operation (see POPT_ARGFLAG_*)
655 * @param aLong value to use
656 * @return 0 on success, POPT_ERROR_NULLARG/POPT_ERROR_BADOPERATION
660 int poptSaveShort(/*@null@*/ short * arg
, unsigned int argInfo
, long aLong
)
661 /*@globals internalState @*/
662 /*@modifies *arg, internalState @*/
663 /*@requires maxSet(arg) >= 0 /\ maxRead(arg) == 0 @*/;
667 * Save an integer, performing logical operation with value.
668 * @warning Alignment check may be too strict on certain platorms.
669 * @param arg integer pointer, aligned on int boundary.
670 * @param argInfo logical operation (see POPT_ARGFLAG_*)
671 * @param aLong value to use
672 * @return 0 on success, POPT_ERROR_NULLARG/POPT_ERROR_BADOPERATION
676 int poptSaveInt(/*@null@*/ int * arg
, unsigned int argInfo
, long aLong
)
677 /*@globals internalState @*/
678 /*@modifies *arg, internalState @*/
679 /*@requires maxSet(arg) >= 0 /\ maxRead(arg) == 0 @*/;
682 /* The bit set typedef. */
684 typedef struct poptBits_s
{
685 unsigned int bits
[1];
689 #define _POPT_BITS_N 1024U /* estimated population */
690 #define _POPT_BITS_M ((3U * _POPT_BITS_N) / 2U)
691 #define _POPT_BITS_K 16U /* no. of linear hash combinations */
693 /*@-exportlocal -exportvar -globuse @*/
695 extern unsigned int _poptBitsN
;
697 extern unsigned int _poptBitsM
;
699 extern unsigned int _poptBitsK
;
700 /*@=exportlocal =exportvar =globuse @*/
703 int poptBitsAdd(/*@null@*/poptBits bits
, /*@null@*/const char * s
)
704 /*@modifies bits @*/;
706 int poptBitsChk(/*@null@*/poptBits bits
, /*@null@*/const char * s
)
708 int poptBitsClr(/*@null@*/poptBits bits
)
709 /*@modifies bits @*/;
711 int poptBitsDel(/*@null@*/poptBits bits
, /*@null@*/const char * s
)
712 /*@modifies bits @*/;
714 int poptBitsIntersect(/*@null@*/ poptBits
* ap
, /*@null@*/ const poptBits b
)
716 int poptBitsUnion(/*@null@*/ poptBits
* ap
, /*@null@*/ const poptBits b
)
718 int poptBitsArgs(/*@null@*/ poptContext con
, /*@null@*/ poptBits
* ap
)
719 /*@modifies con, *ap @*/;
724 * Save a string into a bit set (experimental).
725 * @retval *bits bit set (lazily malloc'd if NULL)
726 * @param argInfo logical operation (see POPT_ARGFLAG_*)
727 * @param s string to add to bit set
728 * @return 0 on success, POPT_ERROR_NULLARG/POPT_ERROR_BADOPERATION
732 int poptSaveBits(/*@null@*/ poptBits
* bitsp
, unsigned int argInfo
,
733 /*@null@*/ const char * s
)
734 /*@globals _poptBitsN, _poptBitsM, _poptBitsK, internalState @*/
735 /*@modifies *bitsp, _poptBitsN, _poptBitsM, _poptBitsK, internalState @*/;