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 0 /*!< no arg */
21 #define POPT_ARG_STRING 1 /*!< arg will be saved as string */
22 #define POPT_ARG_INT 2 /*!< arg will be converted to int */
23 #define POPT_ARG_LONG 3 /*!< arg will be converted to long */
24 #define POPT_ARG_INCLUDE_TABLE 4 /*!< arg points to table */
25 #define POPT_ARG_CALLBACK 5 /*!< 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 6 /*!< 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 7 /*!< arg should take value val */
34 #define POPT_ARG_FLOAT 8 /*!< arg will be converted to float */
35 #define POPT_ARG_DOUBLE 9 /*!< arg will be converted to double */
37 #define POPT_ARG_MASK 0x0000FFFF
44 #define POPT_ARGFLAG_ONEDASH 0x80000000 /*!< allow -longoption */
45 #define POPT_ARGFLAG_DOC_HIDDEN 0x40000000 /*!< don't show in help/usage */
46 #define POPT_ARGFLAG_STRIP 0x20000000 /*!< strip this arg from argv(only applies to long args) */
47 #define POPT_ARGFLAG_OPTIONAL 0x10000000 /*!< arg may be missing */
49 #define POPT_ARGFLAG_OR 0x08000000 /*!< arg will be or'ed */
50 #define POPT_ARGFLAG_NOR 0x09000000 /*!< arg will be nor'ed */
51 #define POPT_ARGFLAG_AND 0x04000000 /*!< arg will be and'ed */
52 #define POPT_ARGFLAG_NAND 0x05000000 /*!< arg will be nand'ed */
53 #define POPT_ARGFLAG_XOR 0x02000000 /*!< arg will be xor'ed */
54 #define POPT_ARGFLAG_NOT 0x01000000 /*!< arg will be negated */
55 #define POPT_ARGFLAG_LOGICALOPS \
56 (POPT_ARGFLAG_OR|POPT_ARGFLAG_AND|POPT_ARGFLAG_XOR)
58 #define POPT_BIT_SET (POPT_ARG_VAL|POPT_ARGFLAG_OR)
59 /*!< set arg bit(s) */
60 #define POPT_BIT_CLR (POPT_ARG_VAL|POPT_ARGFLAG_NAND)
61 /*!< clear arg bit(s) */
63 #define POPT_ARGFLAG_SHOW_DEFAULT 0x00800000 /*!< show default value in --help */
68 * \name Callback modifiers
71 #define POPT_CBFLAG_PRE 0x80000000 /*!< call the callback before parse */
72 #define POPT_CBFLAG_POST 0x40000000 /*!< call the callback after parse */
73 #define POPT_CBFLAG_INC_DATA 0x20000000 /*!< use data from the include line,
75 #define POPT_CBFLAG_SKIPOPTION 0x10000000 /*!< don't callback with option */
76 #define POPT_CBFLAG_CONTINUE 0x08000000 /*!< continue callbacks with option */
80 * \name Error return values
83 #define POPT_ERROR_NOARG -10 /*!< missing argument */
84 #define POPT_ERROR_BADOPT -11 /*!< unknown option */
85 #define POPT_ERROR_OPTSTOODEEP -13 /*!< aliases nested too deeply */
86 #define POPT_ERROR_BADQUOTE -15 /*!< error in paramter quoting */
87 #define POPT_ERROR_ERRNO -16 /*!< errno set, use strerror(errno) */
88 #define POPT_ERROR_BADNUMBER -17 /*!< invalid numeric value */
89 #define POPT_ERROR_OVERFLOW -18 /*!< number too large or too small */
90 #define POPT_ERROR_BADOPERATION -19 /*!< mutually exclusive logical operations requested */
91 #define POPT_ERROR_NULLARG -20 /*!< opt->arg should not be NULL */
92 #define POPT_ERROR_MALLOC -21 /*!< memory allocation failed */
96 * \name poptBadOption() flags
99 #define POPT_BADOPTION_NOALIAS (1 << 0) /*!< don't go into an alias */
103 * \name poptGetContext() flags
106 #define POPT_CONTEXT_NO_EXEC (1 << 0) /*!< ignore exec expansions */
107 #define POPT_CONTEXT_KEEP_FIRST (1 << 1) /*!< pay attention to argv[0] */
108 #define POPT_CONTEXT_POSIXMEHARDER (1 << 2) /*!< options can't follow args */
109 #define POPT_CONTEXT_ARG_OPTS (1 << 4) /*!< return args as options with value 0 */
115 /*@observer@*/ /*@null@*/ const char * longName
; /*!< may be NULL */
116 char shortName
; /*!< may be '\0' */
118 /*@shared@*/ /*@null@*/ void * arg
; /*!< depends on argInfo */
119 int val
; /*!< 0 means don't return, just update flag */
120 /*@observer@*/ /*@null@*/ const char * descrip
; /*!< description for autohelp -- may be NULL */
121 /*@observer@*/ /*@null@*/ const char * argDescrip
; /*!< argument description for autohelp */
125 * A popt alias argument for poptAddAlias().
128 /*@owned@*/ /*@null@*/ const char * longName
; /*!< may be NULL */
129 char shortName
; /*!< may be '\0' */
131 /*@owned@*/ const char ** argv
; /*!< must be free()able */
135 * A popt alias or exec argument for poptAddItem().
137 typedef struct poptItem_s
{
138 struct poptOption option
; /*!< alias/exec name(s) and description. */
139 int argc
; /*!< (alias) no. of args. */
140 /*@owned@*/ const char ** argv
; /*!< (alias) args, must be free()able. */
144 * \name Auto-generated help/usage
149 * Empty table marker to enable displaying popt alias/exec options.
151 /*@observer@*/ /*@checked@*/
152 extern struct poptOption poptAliasOptions
[];
153 #define POPT_AUTOALIAS { NULL, '\0', POPT_ARG_INCLUDE_TABLE, poptAliasOptions, \
154 0, "Options implemented via popt alias/exec:", NULL },
157 * Auto help table options.
159 /*@observer@*/ /*@checked@*/
160 extern struct poptOption poptHelpOptions
[];
161 #define POPT_AUTOHELP { NULL, '\0', POPT_ARG_INCLUDE_TABLE, poptHelpOptions, \
162 0, "Help options:", NULL },
164 #define POPT_TABLEEND { NULL, '\0', 0, 0, 0, NULL, NULL }
169 typedef /*@abstract@*/ struct poptContext_s
* poptContext
;
175 typedef struct poptOption
* poptOption
;
179 enum poptCallbackReason
{ POPT_CALLBACK_REASON_PRE
,
180 POPT_CALLBACK_REASON_POST
,
181 POPT_CALLBACK_REASON_OPTION
};
189 * Table callback prototype.
191 * @param reason reason for callback
192 * @param opt option that triggered callback
193 * @param arg @todo Document.
194 * @param data @todo Document.
196 typedef void (*poptCallbackType
) (poptContext con
,
197 enum poptCallbackReason reason
,
198 /*@null@*/ const struct poptOption
* opt
,
199 /*@null@*/ const char * arg
,
200 /*@null@*/ const void * data
)
204 * Initialize popt context.
206 * @param argc no. of arguments
207 * @param argv argument array
208 * @param options address of popt option table
209 * @param flags or'd POPT_CONTEXT_* bits
210 * @return initialized popt context
212 /*@only@*/ /*@null@*/ poptContext
poptGetContext(
213 /*@dependent@*/ /*@keep@*/ const char * name
,
214 int argc
, /*@dependent@*/ /*@keep@*/ const char ** argv
,
215 /*@dependent@*/ /*@keep@*/ const struct poptOption
* options
,
220 * Reinitialize popt context.
223 void poptResetContext(/*@null@*/poptContext con
)
227 * Return value of next option found.
229 * @return next option val, -1 on last item, POPT_ERROR_* on error
231 int poptGetNextOpt(/*@null@*/poptContext con
)
232 /*@globals fileSystem@*/
233 /*@modifies con, fileSystem @*/;
237 * Return next option argument (if any).
239 * @return option argument, NULL if no more options are available
241 /*@observer@*/ /*@null@*/ const char * poptGetOptArg(/*@null@*/poptContext con
)
245 * Return current option's argument.
247 * @return option argument, NULL if no more options are available
249 /*@observer@*/ /*@null@*/ const char * poptGetArg(/*@null@*/poptContext con
)
253 * Peek at current option's argument.
255 * @return option argument
257 /*@observer@*/ /*@null@*/ const char * poptPeekArg(/*@null@*/poptContext con
)
261 * Return remaining arguments.
263 * @return argument array, terminated with NULL
265 /*@observer@*/ /*@null@*/ const char ** poptGetArgs(/*@null@*/poptContext con
)
269 * Return the option which caused the most recent error.
271 * @return offending option
273 /*@observer@*/ const char * poptBadOption(/*@null@*/poptContext con
, int flags
)
280 * @return NULL always
282 /*@null@*/ poptContext
poptFreeContext( /*@only@*/ /*@null@*/ poptContext con
)
286 * Add arguments to context.
288 * @param argv argument array, NULL terminated
289 * @return 0 on success, POPT_ERROR_OPTSTOODEEP on failure
291 int poptStuffArgs(poptContext con
, /*@keep@*/ const char ** argv
)
295 * Add alias to context.
296 * @todo Pass alias by reference, not value.
297 * @deprecated Use poptAddItem instead.
299 * @param alias alias to add
300 * @param flags (unused)
301 * @return 0 on success
304 int poptAddAlias(poptContext con
, struct poptAlias alias
, int flags
)
308 * Add alias/exec item to context.
310 * @param item alias/exec item to add
311 * @param flags 0 for alias, 1 for exec
312 * @return 0 on success
314 int poptAddItem(poptContext con
, poptItem newItem
, int flags
)
318 * Read configuration file.
320 * @param fn file name to read
321 * @return 0 on success, POPT_ERROR_ERRNO on failure
323 int poptReadConfigFile(poptContext con
, const char * fn
)
324 /*@globals fileSystem@*/
325 /*@modifies fileSystem,
326 con->execs, con->numExecs @*/;
329 * Read default configuration from /etc/popt and $HOME/.popt.
331 * @param useEnv (unused)
332 * @return 0 on success, POPT_ERROR_ERRNO on failure
334 int poptReadDefaultConfig(poptContext con
, /*@unused@*/ int useEnv
)
335 /*@globals fileSystem@*/
336 /*@modifies fileSystem,
337 con->execs, con->numExecs @*/;
340 * Duplicate an argument array.
341 * @note: The argument array is malloc'd as a single area, so only argv must
344 * @param argc no. of arguments
345 * @param argv argument array
346 * @retval argcPtr address of returned no. of arguments
347 * @retval argvPtr address of returned argument array
348 * @return 0 on success, POPT_ERROR_NOARG on failure
350 int poptDupArgv(int argc
, /*@null@*/ const char **argv
,
351 /*@null@*/ /*@out@*/ int * argcPtr
,
352 /*@null@*/ /*@out@*/ const char *** argvPtr
)
353 /*@modifies *argcPtr, *argvPtr @*/;
356 * Parse a string into an argument array.
357 * The parse allows ', ", and \ quoting, but ' is treated the same as " and
358 * both may include \ quotes.
359 * @note: The argument array is malloc'd as a single area, so only argv must
362 * @param s string to parse
363 * @retval argcPtr address of returned no. of arguments
364 * @retval argvPtr address of returned argument array
366 int poptParseArgvString(const unsigned char * s
,
367 /*@out@*/ int * argcPtr
, /*@out@*/ const char *** argvPtr
)
368 /*@modifies *argcPtr, *argvPtr @*/;
371 * Return formatted error string for popt failure.
372 * @param error popt error
373 * @return error string
376 /*@observer@*/ const char * poptStrerror(const int error
)
381 * Limit search for executables.
383 * @param path single path to search for executables
384 * @param allowAbsolute absolute paths only?
386 void poptSetExecPath(poptContext con
, const char * path
, int allowAbsolute
)
390 * Print detailed description of options.
392 * @param fp ouput file handle
393 * @param flags (unused)
395 void poptPrintHelp(poptContext con
, FILE * fp
, /*@unused@*/ int flags
)
396 /*@globals fileSystem @*/
397 /*@modifies *fp, fileSystem @*/;
400 * Print terse description of options.
402 * @param fp ouput file handle
403 * @param flags (unused)
405 void poptPrintUsage(poptContext con
, FILE * fp
, /*@unused@*/ int flags
)
406 /*@globals fileSystem @*/
407 /*@modifies *fp, fileSystem @*/;
410 * Provide text to replace default "[OPTION...]" in help/usage output.
412 * @param text replacement text
415 void poptSetOtherOptionHelp(poptContext con
, const char * text
)
420 * Return argv[0] from context.
424 /*@-redecl -fcnuse@*/
425 /*@observer@*/ const char * poptGetInvocationName(poptContext con
)
427 /*@=redecl =fcnuse@*/
430 * Shuffle argv pointers to remove stripped args, returns new argc.
432 * @param argc no. of args
433 * @param argv arg vector
437 int poptStrippedArgv(poptContext con
, int argc
, char ** argv
)
438 /*@modifies *argv @*/;