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. */
41 #define POPT_ARG_MASK 0x000000FFU
42 #define POPT_GROUP_MASK 0x0000FF00U
50 #define POPT_ARGFLAG_ONEDASH 0x80000000U /*!< allow -longoption */
51 #define POPT_ARGFLAG_DOC_HIDDEN 0x40000000U /*!< don't show in help/usage */
52 #define POPT_ARGFLAG_STRIP 0x20000000U /*!< strip this arg from argv(only applies to long args) */
53 #define POPT_ARGFLAG_OPTIONAL 0x10000000U /*!< arg may be missing */
55 #define POPT_ARGFLAG_OR 0x08000000U /*!< arg will be or'ed */
56 #define POPT_ARGFLAG_NOR 0x09000000U /*!< arg will be nor'ed */
57 #define POPT_ARGFLAG_AND 0x04000000U /*!< arg will be and'ed */
58 #define POPT_ARGFLAG_NAND 0x05000000U /*!< arg will be nand'ed */
59 #define POPT_ARGFLAG_XOR 0x02000000U /*!< arg will be xor'ed */
60 #define POPT_ARGFLAG_NOT 0x01000000U /*!< arg will be negated */
61 #define POPT_ARGFLAG_LOGICALOPS \
62 (POPT_ARGFLAG_OR|POPT_ARGFLAG_AND|POPT_ARGFLAG_XOR)
64 #define POPT_BIT_SET (POPT_ARG_VAL|POPT_ARGFLAG_OR)
65 /*!< set arg bit(s) */
66 #define POPT_BIT_CLR (POPT_ARG_VAL|POPT_ARGFLAG_NAND)
67 /*!< clear arg bit(s) */
69 #define POPT_ARGFLAG_SHOW_DEFAULT 0x00800000U /*!< show default value in --help */
70 #define POPT_ARGFLAG_RANDOM 0x00400000U /*!< random value in [1,arg] */
71 #define POPT_ARGFLAG_TOGGLE 0x00200000U /*!< permit --[no]opt prefix toggle */
76 * \name Callback modifiers
79 #define POPT_CBFLAG_PRE 0x80000000U /*!< call the callback before parse */
80 #define POPT_CBFLAG_POST 0x40000000U /*!< call the callback after parse */
81 #define POPT_CBFLAG_INC_DATA 0x20000000U /*!< use data from the include line,
83 #define POPT_CBFLAG_SKIPOPTION 0x10000000U /*!< don't callback with option */
84 #define POPT_CBFLAG_CONTINUE 0x08000000U /*!< continue callbacks with option */
88 * \name Error return values
91 #define POPT_ERROR_NOARG -10 /*!< missing argument */
92 #define POPT_ERROR_BADOPT -11 /*!< unknown option */
93 #define POPT_ERROR_OPTSTOODEEP -13 /*!< aliases nested too deeply */
94 #define POPT_ERROR_BADQUOTE -15 /*!< error in paramter quoting */
95 #define POPT_ERROR_ERRNO -16 /*!< errno set, use strerror(errno) */
96 #define POPT_ERROR_BADNUMBER -17 /*!< invalid numeric value */
97 #define POPT_ERROR_OVERFLOW -18 /*!< number too large or too small */
98 #define POPT_ERROR_BADOPERATION -19 /*!< mutually exclusive logical operations requested */
99 #define POPT_ERROR_NULLARG -20 /*!< opt->arg should not be NULL */
100 #define POPT_ERROR_MALLOC -21 /*!< memory allocation failed */
101 #define POPT_ERROR_BADCONFIG -22 /*!< config file failed sanity test */
105 * \name poptBadOption() flags
108 #define POPT_BADOPTION_NOALIAS (1U << 0) /*!< don't go into an alias */
112 * \name poptGetContext() flags
115 #define POPT_CONTEXT_NO_EXEC (1U << 0) /*!< ignore exec expansions */
116 #define POPT_CONTEXT_KEEP_FIRST (1U << 1) /*!< pay attention to argv[0] */
117 #define POPT_CONTEXT_POSIXMEHARDER (1U << 2) /*!< options can't follow args */
118 #define POPT_CONTEXT_ARG_OPTS (1U << 4) /*!< return args as options with value 0 */
124 /*@observer@*/ /*@null@*/
125 const char * longName
; /*!< may be NULL */
126 char shortName
; /*!< may be NUL */
127 unsigned int argInfo
;
128 /*@shared@*/ /*@null@*/
129 void * arg
; /*!< depends on argInfo */
130 int val
; /*!< 0 means don't return, just update flag */
131 /*@observer@*/ /*@null@*/
132 const char * descrip
; /*!< description for autohelp -- may be NULL */
133 /*@observer@*/ /*@null@*/
134 const char * argDescrip
; /*!< argument description for autohelp */
138 * A popt alias argument for poptAddAlias().
141 /*@owned@*/ /*@null@*/
142 const char * longName
; /*!< may be NULL */
143 char shortName
; /*!< may be NUL */
146 const char ** argv
; /*!< must be free()able */
150 * A popt alias or exec argument for poptAddItem().
153 typedef struct poptItem_s
{
154 struct poptOption option
; /*!< alias/exec name(s) and description. */
155 int argc
; /*!< (alias) no. of args. */
157 const char ** argv
; /*!< (alias) args, must be free()able. */
162 * \name Auto-generated help/usage
167 * Empty table marker to enable displaying popt alias/exec options.
170 /*@unchecked@*/ /*@observer@*/
171 extern struct poptOption poptAliasOptions
[];
173 #define POPT_AUTOALIAS { NULL, '\0', POPT_ARG_INCLUDE_TABLE, poptAliasOptions, \
174 0, "Options implemented via popt alias/exec:", NULL },
177 * Auto help table options.
180 /*@unchecked@*/ /*@observer@*/
181 extern struct poptOption poptHelpOptions
[];
185 /*@unchecked@*/ /*@observer@*/
186 extern struct poptOption
* poptHelpOptionsI18N
;
189 #define POPT_AUTOHELP { NULL, '\0', POPT_ARG_INCLUDE_TABLE, poptHelpOptions, \
190 0, "Help options:", NULL },
192 #define POPT_TABLEEND { NULL, '\0', 0, NULL, 0, NULL, NULL }
198 typedef /*@abstract@*/ struct poptContext_s
* poptContext
;
204 /*@-exporttype -typeuse@*/
205 typedef struct poptOption
* poptOption
;
206 /*@=exporttype =typeuse@*/
212 enum poptCallbackReason
{
213 POPT_CALLBACK_REASON_PRE
= 0,
214 POPT_CALLBACK_REASON_POST
= 1,
215 POPT_CALLBACK_REASON_OPTION
= 2
225 * Table callback prototype.
227 * @param reason reason for callback
228 * @param opt option that triggered callback
229 * @param arg @todo Document.
230 * @param data @todo Document.
232 typedef void (*poptCallbackType
) (poptContext con
,
233 enum poptCallbackReason reason
,
234 /*@null@*/ const struct poptOption
* opt
,
235 /*@null@*/ const char * arg
,
236 /*@null@*/ const void * data
)
237 /*@globals internalState @*/
238 /*@modifies internalState @*/;
243 * @return NULL always
246 poptContext
poptFreeContext( /*@only@*/ /*@null@*/ poptContext con
)
250 * Initialize popt context.
251 * @param name context name (usually argv[0] program name)
252 * @param argc no. of arguments
253 * @param argv argument array
254 * @param options address of popt option table
255 * @param flags or'd POPT_CONTEXT_* bits
256 * @return initialized popt context
258 /*@only@*/ /*@null@*/
259 poptContext
poptGetContext(
260 /*@dependent@*/ /*@keep@*/ const char * name
,
261 int argc
, /*@dependent@*/ /*@keep@*/ const char ** argv
,
262 /*@dependent@*/ /*@keep@*/ const struct poptOption
* options
,
264 /*@globals internalState @*/
265 /*@modifies internalState @*/;
268 * Destroy context (alternative implementation).
270 * @return NULL always
273 poptContext
poptFini( /*@only@*/ /*@null@*/ poptContext con
)
277 * Initialize popt context (alternative implementation).
278 * This routine does poptGetContext() and then poptReadConfigFiles().
279 * @param argc no. of arguments
280 * @param argv argument array
281 * @param options address of popt option table
282 * @param configPaths colon separated file path(s) to read.
283 * @return initialized popt context (NULL on error).
285 /*@only@*/ /*@null@*/ /*@unused@*/
286 poptContext
poptInit(int argc
, /*@dependent@*/ /*@keep@*/ const char ** argv
,
287 /*@dependent@*/ /*@keep@*/ const struct poptOption
* options
,
288 /*@null@*/ const char * configPaths
)
289 /*@globals fileSystem, internalState @*/
290 /*@modifies fileSystem, internalState @*/;
293 * Reinitialize popt context.
297 void poptResetContext(/*@null@*/poptContext con
)
301 * Return value of next option found.
303 * @return next option val, -1 on last item, POPT_ERROR_* on error
305 int poptGetNextOpt(/*@null@*/poptContext con
)
306 /*@globals fileSystem, internalState @*/
307 /*@modifies con, fileSystem, internalState @*/;
310 * Return next option argument (if any).
312 * @return option argument, NULL if no argument is available
314 /*@observer@*/ /*@null@*/ /*@unused@*/
315 char * poptGetOptArg(/*@null@*/poptContext con
)
319 * Return next argument.
321 * @return next argument, NULL if no argument is available
323 /*@observer@*/ /*@null@*/ /*@unused@*/
324 const char * poptGetArg(/*@null@*/poptContext con
)
328 * Peek at current argument.
330 * @return current argument, NULL if no argument is available
332 /*@observer@*/ /*@null@*/ /*@unused@*/
333 const char * poptPeekArg(/*@null@*/poptContext con
)
337 * Return remaining arguments.
339 * @return argument array, NULL terminated
341 /*@observer@*/ /*@null@*/
342 const char ** poptGetArgs(/*@null@*/poptContext con
)
346 * Return the option which caused the most recent error.
349 * @return offending option
352 const char * poptBadOption(/*@null@*/poptContext con
, unsigned int flags
)
356 * Add arguments to context.
358 * @param argv argument array, NULL terminated
359 * @return 0 on success, POPT_ERROR_OPTSTOODEEP on failure
362 int poptStuffArgs(poptContext con
, /*@keep@*/ const char ** argv
)
366 * Add alias to context.
367 * @todo Pass alias by reference, not value.
368 * @deprecated Use poptAddItem instead.
370 * @param alias alias to add
371 * @param flags (unused)
372 * @return 0 on success
375 int poptAddAlias(poptContext con
, struct poptAlias alias
, int flags
)
379 * Add alias/exec item to context.
381 * @param newItem alias/exec item to add
382 * @param flags 0 for alias, 1 for exec
383 * @return 0 on success
385 int poptAddItem(poptContext con
, poptItem newItem
, int flags
)
389 * Perform sanity checks on a file path.
390 * @param fn file name
391 * @return 0 on OK, 1 on NOTOK.
393 int poptSaneFile(const char * fn
)
394 /*@globals errno, internalState @*/
395 /*@modifies errno, internalState @*/;
398 * Read a file into a buffer.
399 * @param fn file name
400 * @retval *bp buffer (malloc'd) (or NULL)
401 * @retval *nbp no. of bytes in buffer (including final NUL) (or NULL)
402 * @param flags 1 to trim escaped newlines
403 * return 0 on success
405 int poptReadFile(const char * fn
, /*@null@*/ /*@out@*/ char ** bp
,
406 /*@null@*/ /*@out@*/ size_t * nbp
, int flags
)
407 /*@globals errno, fileSystem, internalState @*/
408 /*@modifies *bp, *nbp, errno, fileSystem, internalState @*/;
409 #define POPT_READFILE_TRIMNEWLINES 1
412 * Read configuration file.
414 * @param fn file name to read
415 * @return 0 on success, POPT_ERROR_ERRNO on failure
417 int poptReadConfigFile(poptContext con
, const char * fn
)
418 /*@globals errno, fileSystem, internalState @*/
419 /*@modifies con->execs, con->numExecs,
420 errno, fileSystem, internalState @*/;
423 * Read configuration file(s).
424 * Colon separated files to read, looping over poptReadConfigFile().
425 * Note that an '@' character preceeding a path in the list will
426 * also perform additional sanity checks on the file before reading.
428 * @param paths colon separated file name(s) to read
429 * @return 0 on success, POPT_ERROR_BADCONFIG on failure
431 int poptReadConfigFiles(poptContext con
, /*@null@*/ const char * paths
)
432 /*@globals errno, fileSystem, internalState @*/
433 /*@modifies con->execs, con->numExecs,
434 errno, fileSystem, internalState @*/;
437 * Read default configuration from /etc/popt and $HOME/.popt.
439 * @param useEnv (unused)
440 * @return 0 on success, POPT_ERROR_ERRNO on failure
443 int poptReadDefaultConfig(poptContext con
, /*@unused@*/ int useEnv
)
444 /*@globals fileSystem, internalState @*/
445 /*@modifies con->execs, con->numExecs,
446 fileSystem, internalState @*/;
449 * Duplicate an argument array.
450 * @note: The argument array is malloc'd as a single area, so only argv must
453 * @param argc no. of arguments
454 * @param argv argument array
455 * @retval argcPtr address of returned no. of arguments
456 * @retval argvPtr address of returned argument array
457 * @return 0 on success, POPT_ERROR_NOARG on failure
459 int poptDupArgv(int argc
, /*@null@*/ const char **argv
,
460 /*@null@*/ /*@out@*/ int * argcPtr
,
461 /*@null@*/ /*@out@*/ const char *** argvPtr
)
462 /*@modifies *argcPtr, *argvPtr @*/;
465 * Parse a string into an argument array.
466 * The parse allows ', ", and \ quoting, but ' is treated the same as " and
467 * both may include \ quotes.
468 * @note: The argument array is malloc'd as a single area, so only argv must
471 * @param s string to parse
472 * @retval argcPtr address of returned no. of arguments
473 * @retval argvPtr address of returned argument array
475 int poptParseArgvString(const char * s
,
476 /*@out@*/ int * argcPtr
, /*@out@*/ const char *** argvPtr
)
477 /*@modifies *argcPtr, *argvPtr @*/;
480 * Parses an input configuration file and returns an string that is a
481 * command line. For use with popt. You must free the return value when done.
485 # this line is ignored
495 reall bad line = again
497 test = with lots of spaces
502 --aaa --bbb --ccc --bla="bla" --this_is="fdsafdas" --5555="55555" --test="with lots of spaces"
505 * Passing this to poptParseArgvString() yields an argv of:
513 '--test=with lots of spaces'
516 * @bug NULL is returned if file line is too long.
517 * @bug Silently ignores invalid lines.
519 * @param fp file handle to read
520 * @param *argstrp return string of options (malloc'd)
521 * @param flags unused
522 * @return 0 on success
523 * @see poptParseArgvString
526 int poptConfigFileToString(FILE *fp
, /*@out@*/ char ** argstrp
, int flags
)
527 /*@globals fileSystem @*/
528 /*@modifies *fp, *argstrp, fileSystem @*/;
532 * Return formatted error string for popt failure.
533 * @param error popt error
534 * @return error string
537 const char * poptStrerror(const int error
)
541 * Limit search for executables.
543 * @param path single path to search for executables
544 * @param allowAbsolute absolute paths only?
547 void poptSetExecPath(poptContext con
, const char * path
, int allowAbsolute
)
551 * Print detailed description of options.
553 * @param fp ouput file handle
554 * @param flags (unused)
556 void poptPrintHelp(poptContext con
, FILE * fp
, /*@unused@*/ int flags
)
557 /*@globals fileSystem @*/
558 /*@modifies fp, fileSystem @*/;
561 * Print terse description of options.
563 * @param fp ouput file handle
564 * @param flags (unused)
566 void poptPrintUsage(poptContext con
, FILE * fp
, /*@unused@*/ int flags
)
567 /*@globals fileSystem @*/
568 /*@modifies fp, fileSystem @*/;
571 * Provide text to replace default "[OPTION...]" in help/usage output.
573 * @param text replacement text
576 void poptSetOtherOptionHelp(poptContext con
, const char * text
)
581 * Return argv[0] from context.
587 const char * poptGetInvocationName(poptContext con
)
592 * Shuffle argv pointers to remove stripped args, returns new argc.
594 * @param argc no. of args
595 * @param argv arg vector
599 int poptStrippedArgv(poptContext con
, int argc
, char ** argv
)
600 /*@modifies *argv @*/;
604 * Add a string to an argv array.
605 * @retval *argvp argv array
606 * @param argInfo (unused)
607 * @param val string arg to add (using strdup)
611 int poptSaveString(/*@null@*/ const char *** argvp
, unsigned int argInfo
,
613 /*@modifies *argvp @*/;
616 * Save a long long, performing logical operation with value.
617 * @warning Alignment check may be too strict on certain platorms.
618 * @param arg integer pointer, aligned on int boundary.
619 * @param argInfo logical operation (see POPT_ARGFLAG_*)
620 * @param aLongLong value to use
621 * @return 0 on success, POPT_ERROR_NULLARG/POPT_ERROR_BADOPERATION
625 int poptSaveLongLong(/*@null@*/ long long * arg
, unsigned int argInfo
,
627 /*@globals internalState @*/
628 /*@modifies *arg, internalState @*/
629 /*@requires maxSet(arg) >= 0 /\ maxRead(arg) == 0 @*/;
633 * Save a long, performing logical operation with value.
634 * @warning Alignment check may be too strict on certain platorms.
635 * @param arg integer pointer, aligned on int boundary.
636 * @param argInfo logical operation (see POPT_ARGFLAG_*)
637 * @param aLong value to use
638 * @return 0 on success, POPT_ERROR_NULLARG/POPT_ERROR_BADOPERATION
642 int poptSaveLong(/*@null@*/ long * arg
, unsigned int argInfo
, long aLong
)
643 /*@globals internalState @*/
644 /*@modifies *arg, internalState @*/
645 /*@requires maxSet(arg) >= 0 /\ maxRead(arg) == 0 @*/;
649 * Save an integer, performing logical operation with value.
650 * @warning Alignment check may be too strict on certain platorms.
651 * @param arg integer pointer, aligned on int boundary.
652 * @param argInfo logical operation (see POPT_ARGFLAG_*)
653 * @param aLong value to use
654 * @return 0 on success, POPT_ERROR_NULLARG/POPT_ERROR_BADOPERATION
658 int poptSaveInt(/*@null@*/ int * arg
, unsigned int argInfo
, long aLong
)
659 /*@globals internalState @*/
660 /*@modifies *arg, internalState @*/
661 /*@requires maxSet(arg) >= 0 /\ maxRead(arg) == 0 @*/;