00001 /** \file popt/popt.h 00002 * \ingroup popt 00003 */ 00004 00005 /* (C) 1998-2000 Red Hat, Inc. -- Licensing details are in the COPYING 00006 file accompanying popt source distributions, available from 00007 ftp://ftp.rpm.org/pub/rpm/dist. */ 00008 00009 #ifndef H_POPT 00010 #define H_POPT 00011 00012 #include <stdio.h> /* for FILE * */ 00013 00014 #define POPT_OPTION_DEPTH 10 00015 00016 /** \ingroup popt 00017 * \name Arg type identifiers 00018 */ 00019 /*@{*/ 00020 #define POPT_ARG_NONE 0 /*!< no arg */ 00021 #define POPT_ARG_STRING 1 /*!< arg will be saved as string */ 00022 #define POPT_ARG_INT 2 /*!< arg will be converted to int */ 00023 #define POPT_ARG_LONG 3 /*!< arg will be converted to long */ 00024 #define POPT_ARG_INCLUDE_TABLE 4 /*!< arg points to table */ 00025 #define POPT_ARG_CALLBACK 5 /*!< table-wide callback... must be 00026 set first in table; arg points 00027 to callback, descrip points to 00028 callback data to pass */ 00029 #define POPT_ARG_INTL_DOMAIN 6 /*!< set the translation domain 00030 for this table and any 00031 included tables; arg points 00032 to the domain string */ 00033 #define POPT_ARG_VAL 7 /*!< arg should take value val */ 00034 #define POPT_ARG_FLOAT 8 /*!< arg will be converted to float */ 00035 #define POPT_ARG_DOUBLE 9 /*!< arg will be converted to double */ 00036 00037 #define POPT_ARG_MASK 0x0000FFFF 00038 /*@}*/ 00039 00040 /** \ingroup popt 00041 * \name Arg modifiers 00042 */ 00043 /*@{*/ 00044 #define POPT_ARGFLAG_ONEDASH 0x80000000 /*!< allow -longoption */ 00045 #define POPT_ARGFLAG_DOC_HIDDEN 0x40000000 /*!< don't show in help/usage */ 00046 #define POPT_ARGFLAG_STRIP 0x20000000 /*!< strip this arg from argv(only applies to long args) */ 00047 #define POPT_ARGFLAG_OPTIONAL 0x10000000 /*!< arg may be missing */ 00048 00049 #define POPT_ARGFLAG_OR 0x08000000 /*!< arg will be or'ed */ 00050 #define POPT_ARGFLAG_NOR 0x09000000 /*!< arg will be nor'ed */ 00051 #define POPT_ARGFLAG_AND 0x04000000 /*!< arg will be and'ed */ 00052 #define POPT_ARGFLAG_NAND 0x05000000 /*!< arg will be nand'ed */ 00053 #define POPT_ARGFLAG_XOR 0x02000000 /*!< arg will be xor'ed */ 00054 #define POPT_ARGFLAG_NOT 0x01000000 /*!< arg will be negated */ 00055 #define POPT_ARGFLAG_LOGICALOPS \ 00056 (POPT_ARGFLAG_OR|POPT_ARGFLAG_AND|POPT_ARGFLAG_XOR) 00057 00058 #define POPT_BIT_SET (POPT_ARG_VAL|POPT_ARGFLAG_OR) 00059 /*!< set arg bit(s) */ 00060 #define POPT_BIT_CLR (POPT_ARG_VAL|POPT_ARGFLAG_NAND) 00061 /*!< clear arg bit(s) */ 00062 00063 #define POPT_ARGFLAG_SHOW_DEFAULT 0x00800000 /*!< show default value in --help */ 00064 00065 /*@}*/ 00066 00067 /** \ingroup popt 00068 * \name Callback modifiers 00069 */ 00070 /*@{*/ 00071 #define POPT_CBFLAG_PRE 0x80000000 /*!< call the callback before parse */ 00072 #define POPT_CBFLAG_POST 0x40000000 /*!< call the callback after parse */ 00073 #define POPT_CBFLAG_INC_DATA 0x20000000 /*!< use data from the include line, 00074 not the subtable */ 00075 #define POPT_CBFLAG_SKIPOPTION 0x10000000 /*!< don't callback with option */ 00076 #define POPT_CBFLAG_CONTINUE 0x08000000 /*!< continue callbacks with option */ 00077 /*@}*/ 00078 00079 /** \ingroup popt 00080 * \name Error return values 00081 */ 00082 /*@{*/ 00083 #define POPT_ERROR_NOARG -10 /*!< missing argument */ 00084 #define POPT_ERROR_BADOPT -11 /*!< unknown option */ 00085 #define POPT_ERROR_OPTSTOODEEP -13 /*!< aliases nested too deeply */ 00086 #define POPT_ERROR_BADQUOTE -15 /*!< error in paramter quoting */ 00087 #define POPT_ERROR_ERRNO -16 /*!< errno set, use strerror(errno) */ 00088 #define POPT_ERROR_BADNUMBER -17 /*!< invalid numeric value */ 00089 #define POPT_ERROR_OVERFLOW -18 /*!< number too large or too small */ 00090 #define POPT_ERROR_BADOPERATION -19 /*!< mutually exclusive logical operations requested */ 00091 #define POPT_ERROR_NULLARG -20 /*!< opt->arg should not be NULL */ 00092 #define POPT_ERROR_MALLOC -21 /*!< memory allocation failed */ 00093 /*@}*/ 00094 00095 /** \ingroup popt 00096 * \name poptBadOption() flags 00097 */ 00098 /*@{*/ 00099 #define POPT_BADOPTION_NOALIAS (1 << 0) /*!< don't go into an alias */ 00100 /*@}*/ 00101 00102 /** \ingroup popt 00103 * \name poptGetContext() flags 00104 */ 00105 /*@{*/ 00106 #define POPT_CONTEXT_NO_EXEC (1 << 0) /*!< ignore exec expansions */ 00107 #define POPT_CONTEXT_KEEP_FIRST (1 << 1) /*!< pay attention to argv[0] */ 00108 #define POPT_CONTEXT_POSIXMEHARDER (1 << 2) /*!< options can't follow args */ 00109 #define POPT_CONTEXT_ARG_OPTS (1 << 4) /*!< return args as options with value 0 */ 00110 /*@}*/ 00111 00112 /** \ingroup popt 00113 */ 00114 struct poptOption { 00115 /*@observer@*/ /*@null@*/ 00116 const char * longName; /*!< may be NULL */ 00117 char shortName; /*!< may be '\0' */ 00118 int argInfo; 00119 /*@shared@*/ /*@null@*/ 00120 void * arg; /*!< depends on argInfo */ 00121 int val; /*!< 0 means don't return, just update flag */ 00122 /*@observer@*/ /*@null@*/ 00123 const char * descrip; /*!< description for autohelp -- may be NULL */ 00124 /*@observer@*/ /*@null@*/ 00125 const char * argDescrip; /*!< argument description for autohelp */ 00126 }; 00127 00128 /** \ingroup popt 00129 * A popt alias argument for poptAddAlias(). 00130 */ 00131 struct poptAlias { 00132 /*@owned@*/ /*@null@*/ 00133 const char * longName; /*!< may be NULL */ 00134 char shortName; /*!< may be '\0' */ 00135 int argc; 00136 /*@owned@*/ 00137 const char ** argv; /*!< must be free()able */ 00138 }; 00139 00140 /** \ingroup popt 00141 * A popt alias or exec argument for poptAddItem(). 00142 */ 00143 /*@-exporttype@*/ 00144 typedef struct poptItem_s { 00145 struct poptOption option; /*!< alias/exec name(s) and description. */ 00146 int argc; /*!< (alias) no. of args. */ 00147 /*@owned@*/ 00148 const char ** argv; /*!< (alias) args, must be free()able. */ 00149 } * poptItem; 00150 /*@=exporttype@*/ 00151 00152 /** \ingroup popt 00153 * \name Auto-generated help/usage 00154 */ 00155 /*@{*/ 00156 00157 /** 00158 * Empty table marker to enable displaying popt alias/exec options. 00159 */ 00160 /*@-exportvar@*/ 00161 /*@unchecked@*/ /*@observer@*/ 00162 extern struct poptOption poptAliasOptions[]; 00163 /*@=exportvar@*/ 00164 #define POPT_AUTOALIAS { NULL, '\0', POPT_ARG_INCLUDE_TABLE, poptAliasOptions, \ 00165 0, "Options implemented via popt alias/exec:", NULL }, 00166 00167 /** 00168 * Auto help table options. 00169 */ 00170 /*@-exportvar@*/ 00171 /*@unchecked@*/ /*@observer@*/ 00172 extern struct poptOption poptHelpOptions[]; 00173 /*@=exportvar@*/ 00174 #define POPT_AUTOHELP { NULL, '\0', POPT_ARG_INCLUDE_TABLE, poptHelpOptions, \ 00175 0, "Help options:", NULL }, 00176 00177 #define POPT_TABLEEND { NULL, '\0', 0, 0, 0, NULL, NULL } 00178 /*@}*/ 00179 00180 /** \ingroup popt 00181 */ 00182 /*@-exporttype@*/ 00183 typedef /*@abstract@*/ struct poptContext_s * poptContext; 00184 /*@=exporttype@*/ 00185 00186 /** \ingroup popt 00187 */ 00188 #ifndef __cplusplus 00189 /*@-exporttype -typeuse@*/ 00190 typedef struct poptOption * poptOption; 00191 /*@=exporttype =typeuse@*/ 00192 #endif 00193 00194 /*@-exportconst@*/ 00195 enum poptCallbackReason { 00196 POPT_CALLBACK_REASON_PRE = 0, 00197 POPT_CALLBACK_REASON_POST = 1, 00198 POPT_CALLBACK_REASON_OPTION = 2 00199 }; 00200 /*@=exportconst@*/ 00201 00202 #ifdef __cplusplus 00203 extern "C" { 00204 #endif 00205 /*@-type@*/ 00206 00207 /** \ingroup popt 00208 * Table callback prototype. 00209 * @param con context 00210 * @param reason reason for callback 00211 * @param opt option that triggered callback 00212 * @param arg @todo Document. 00213 * @param data @todo Document. 00214 */ 00215 typedef void (*poptCallbackType) (poptContext con, 00216 enum poptCallbackReason reason, 00217 /*@null@*/ const struct poptOption * opt, 00218 /*@null@*/ const char * arg, 00219 /*@null@*/ const void * data) 00220 /*@*/; 00221 00222 /** \ingroup popt 00223 * Initialize popt context. 00224 * @param name context name (usually argv[0] program name) 00225 * @param argc no. of arguments 00226 * @param argv argument array 00227 * @param options address of popt option table 00228 * @param flags or'd POPT_CONTEXT_* bits 00229 * @return initialized popt context 00230 */ 00231 /*@only@*/ /*@null@*/ poptContext poptGetContext( 00232 /*@dependent@*/ /*@keep@*/ const char * name, 00233 int argc, /*@dependent@*/ /*@keep@*/ const char ** argv, 00234 /*@dependent@*/ /*@keep@*/ const struct poptOption * options, 00235 int flags) 00236 /*@*/; 00237 00238 /** \ingroup popt 00239 * Reinitialize popt context. 00240 * @param con context 00241 */ 00242 /*@unused@*/ 00243 void poptResetContext(/*@null@*/poptContext con) 00244 /*@modifies con @*/; 00245 00246 /** \ingroup popt 00247 * Return value of next option found. 00248 * @param con context 00249 * @return next option val, -1 on last item, POPT_ERROR_* on error 00250 */ 00251 int poptGetNextOpt(/*@null@*/poptContext con) 00252 /*@globals fileSystem, internalState @*/ 00253 /*@modifies con, fileSystem, internalState @*/; 00254 00255 /** \ingroup popt 00256 * Return next option argument (if any). 00257 * @param con context 00258 * @return option argument, NULL if no argument is available 00259 */ 00260 /*@observer@*/ /*@null@*/ const char * poptGetOptArg(/*@null@*/poptContext con) 00261 /*@modifies con @*/; 00262 00263 /** \ingroup popt 00264 * Return next argument. 00265 * @param con context 00266 * @return next argument, NULL if no argument is available 00267 */ 00268 /*@observer@*/ /*@null@*/ const char * poptGetArg(/*@null@*/poptContext con) 00269 /*@modifies con @*/; 00270 00271 /** \ingroup popt 00272 * Peek at current argument. 00273 * @param con context 00274 * @return current argument, NULL if no argument is available 00275 */ 00276 /*@observer@*/ /*@null@*/ const char * poptPeekArg(/*@null@*/poptContext con) 00277 /*@*/; 00278 00279 /** \ingroup popt 00280 * Return remaining arguments. 00281 * @param con context 00282 * @return argument array, NULL terminated 00283 */ 00284 /*@observer@*/ /*@null@*/ const char ** poptGetArgs(/*@null@*/poptContext con) 00285 /*@modifies con @*/; 00286 00287 /** \ingroup popt 00288 * Return the option which caused the most recent error. 00289 * @param con context 00290 * @param flags 00291 * @return offending option 00292 */ 00293 /*@observer@*/ const char * poptBadOption(/*@null@*/poptContext con, int flags) 00294 /*@*/; 00295 00296 /** \ingroup popt 00297 * Destroy context. 00298 * @param con context 00299 * @return NULL always 00300 */ 00301 /*@null@*/ poptContext poptFreeContext( /*@only@*/ /*@null@*/ poptContext con) 00302 /*@modifies con @*/; 00303 00304 /** \ingroup popt 00305 * Add arguments to context. 00306 * @param con context 00307 * @param argv argument array, NULL terminated 00308 * @return 0 on success, POPT_ERROR_OPTSTOODEEP on failure 00309 */ 00310 int poptStuffArgs(poptContext con, /*@keep@*/ const char ** argv) 00311 /*@modifies con @*/; 00312 00313 /** \ingroup popt 00314 * Add alias to context. 00315 * @todo Pass alias by reference, not value. 00316 * @deprecated Use poptAddItem instead. 00317 * @param con context 00318 * @param alias alias to add 00319 * @param flags (unused) 00320 * @return 0 on success 00321 */ 00322 /*@unused@*/ 00323 int poptAddAlias(poptContext con, struct poptAlias alias, int flags) 00324 /*@modifies con @*/; 00325 00326 /** \ingroup popt 00327 * Add alias/exec item to context. 00328 * @param con context 00329 * @param newItem alias/exec item to add 00330 * @param flags 0 for alias, 1 for exec 00331 * @return 0 on success 00332 */ 00333 int poptAddItem(poptContext con, poptItem newItem, int flags) 00334 /*@modifies con @*/; 00335 00336 /** \ingroup popt 00337 * Read configuration file. 00338 * @param con context 00339 * @param fn file name to read 00340 * @return 0 on success, POPT_ERROR_ERRNO on failure 00341 */ 00342 int poptReadConfigFile(poptContext con, const char * fn) 00343 /*@globals fileSystem, internalState @*/ 00344 /*@modifies con->execs, con->numExecs, 00345 fileSystem, internalState @*/; 00346 00347 /** \ingroup popt 00348 * Read default configuration from /etc/popt and $HOME/.popt. 00349 * @param con context 00350 * @param useEnv (unused) 00351 * @return 0 on success, POPT_ERROR_ERRNO on failure 00352 */ 00353 int poptReadDefaultConfig(poptContext con, /*@unused@*/ int useEnv) 00354 /*@globals fileSystem, internalState @*/ 00355 /*@modifies con->execs, con->numExecs, 00356 fileSystem, internalState @*/; 00357 00358 /** \ingroup popt 00359 * Duplicate an argument array. 00360 * @note: The argument array is malloc'd as a single area, so only argv must 00361 * be free'd. 00362 * 00363 * @param argc no. of arguments 00364 * @param argv argument array 00365 * @retval argcPtr address of returned no. of arguments 00366 * @retval argvPtr address of returned argument array 00367 * @return 0 on success, POPT_ERROR_NOARG on failure 00368 */ 00369 int poptDupArgv(int argc, /*@null@*/ const char **argv, 00370 /*@null@*/ /*@out@*/ int * argcPtr, 00371 /*@null@*/ /*@out@*/ const char *** argvPtr) 00372 /*@modifies *argcPtr, *argvPtr @*/; 00373 00374 /** \ingroup popt 00375 * Parse a string into an argument array. 00376 * The parse allows ', ", and \ quoting, but ' is treated the same as " and 00377 * both may include \ quotes. 00378 * @note: The argument array is malloc'd as a single area, so only argv must 00379 * be free'd. 00380 * 00381 * @param s string to parse 00382 * @retval argcPtr address of returned no. of arguments 00383 * @retval argvPtr address of returned argument array 00384 */ 00385 int poptParseArgvString(const char * s, 00386 /*@out@*/ int * argcPtr, /*@out@*/ const char *** argvPtr) 00387 /*@modifies *argcPtr, *argvPtr @*/; 00388 00389 /** \ingroup popt 00390 * Parses an input configuration file and returns an string that is a 00391 * command line. For use with popt. You must free the return value when done. 00392 * 00393 * Given the file: 00394 \verbatim 00395 # this line is ignored 00396 # this one too 00397 aaa 00398 bbb 00399 ccc 00400 bla=bla 00401 00402 this_is = fdsafdas 00403 bad_line= 00404 reall bad line 00405 reall bad line = again 00406 5555= 55555 00407 test = with lots of spaces 00408 \endverbatim 00409 * 00410 * The result is: 00411 \verbatim 00412 --aaa --bbb --ccc --bla="bla" --this_is="fdsafdas" --5555="55555" --test="with lots of spaces" 00413 \endverbatim 00414 * 00415 * Passing this to poptParseArgvString() yields an argv of: 00416 \verbatim 00417 '--aaa' 00418 '--bbb' 00419 '--ccc' 00420 '--bla=bla' 00421 '--this_is=fdsafdas' 00422 '--5555=55555' 00423 '--test=with lots of spaces' 00424 \endverbatim 00425 * 00426 * @bug NULL is returned if file line is too long. 00427 * @bug Silently ignores invalid lines. 00428 * 00429 * @param fp file handle to read 00430 * @param *argstrp return string of options (malloc'd) 00431 * @param flags unused 00432 * @return 0 on success 00433 * @see poptParseArgvString 00434 */ 00435 /*@-fcnuse@*/ 00436 int poptConfigFileToString(FILE *fp, /*@out@*/ char ** argstrp, int flags) 00437 /*@globals fileSystem @*/ 00438 /*@modifies *fp, *argstrp, fileSystem @*/; 00439 /*@=fcnuse@*/ 00440 00441 /** \ingroup popt 00442 * Return formatted error string for popt failure. 00443 * @param error popt error 00444 * @return error string 00445 */ 00446 /*@observer@*/ const char* poptStrerror(const int error) 00447 /*@*/; 00448 00449 /** \ingroup popt 00450 * Limit search for executables. 00451 * @param con context 00452 * @param path single path to search for executables 00453 * @param allowAbsolute absolute paths only? 00454 */ 00455 void poptSetExecPath(poptContext con, const char * path, int allowAbsolute) 00456 /*@modifies con @*/; 00457 00458 /** \ingroup popt 00459 * Print detailed description of options. 00460 * @param con context 00461 * @param fp ouput file handle 00462 * @param flags (unused) 00463 */ 00464 void poptPrintHelp(poptContext con, FILE * fp, /*@unused@*/ int flags) 00465 /*@globals fileSystem @*/ 00466 /*@modifies *fp, fileSystem @*/; 00467 00468 /** \ingroup popt 00469 * Print terse description of options. 00470 * @param con context 00471 * @param fp ouput file handle 00472 * @param flags (unused) 00473 */ 00474 void poptPrintUsage(poptContext con, FILE * fp, /*@unused@*/ int flags) 00475 /*@globals fileSystem @*/ 00476 /*@modifies *fp, fileSystem @*/; 00477 00478 /** \ingroup popt 00479 * Provide text to replace default "[OPTION...]" in help/usage output. 00480 * @param con context 00481 * @param text replacement text 00482 */ 00483 /*@-fcnuse@*/ 00484 void poptSetOtherOptionHelp(poptContext con, const char * text) 00485 /*@modifies con @*/; 00486 /*@=fcnuse@*/ 00487 00488 /** \ingroup popt 00489 * Return argv[0] from context. 00490 * @param con context 00491 * @return argv[0] 00492 */ 00493 /*@-fcnuse@*/ 00494 /*@observer@*/ const char * poptGetInvocationName(poptContext con) 00495 /*@*/; 00496 /*@=fcnuse@*/ 00497 00498 /** \ingroup popt 00499 * Shuffle argv pointers to remove stripped args, returns new argc. 00500 * @param con context 00501 * @param argc no. of args 00502 * @param argv arg vector 00503 * @return new argc 00504 */ 00505 /*@-fcnuse@*/ 00506 int poptStrippedArgv(poptContext con, int argc, char ** argv) 00507 /*@modifies *argv @*/; 00508 /*@=fcnuse@*/ 00509 00510 /** 00511 * Save a long, performing logical operation with value. 00512 * @warning Alignment check may be too strict on certain platorms. 00513 * @param arg integer pointer, aligned on int boundary. 00514 * @param argInfo logical operation (see POPT_ARGFLAG_*) 00515 * @param aLong value to use 00516 * @return 0 on success, POPT_ERROR_NULLARG/POPT_ERROR_BADOPERATION 00517 */ 00518 /*@-incondefs@*/ 00519 /*@unused@*/ 00520 int poptSaveLong(/*@null@*/ long * arg, int argInfo, long aLong) 00521 /*@modifies *arg @*/ 00522 /*@requires maxSet(arg) >= 0 /\ maxRead(arg) == 0 @*/; 00523 /*@=incondefs@*/ 00524 00525 /** 00526 * Save an integer, performing logical operation with value. 00527 * @warning Alignment check may be too strict on certain platorms. 00528 * @param arg integer pointer, aligned on int boundary. 00529 * @param argInfo logical operation (see POPT_ARGFLAG_*) 00530 * @param aLong value to use 00531 * @return 0 on success, POPT_ERROR_NULLARG/POPT_ERROR_BADOPERATION 00532 */ 00533 /*@-incondefs@*/ 00534 /*@unused@*/ 00535 int poptSaveInt(/*@null@*/ int * arg, int argInfo, long aLong) 00536 /*@modifies *arg @*/ 00537 /*@requires maxSet(arg) >= 0 /\ maxRead(arg) == 0 @*/; 00538 /*@=incondefs@*/ 00539 00540 /*@=type@*/ 00541 #ifdef __cplusplus 00542 } 00543 #endif 00544 00545 #endif
1.4.7