Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-06-20 08:50:15

0001 /** @file
0002  */
0003 
0004 /* (C) 1998-2000 Red Hat, Inc. -- Licensing details are in the COPYING
0005    file accompanying popt source distributions, available from 
0006    ftp://ftp.rpm.org/pub/rpm/dist. */
0007 
0008 #ifndef H_POPT
0009 #define H_POPT
0010 
0011 #include <stdio.h>          /* for FILE * */
0012 
0013 #define POPT_OPTION_DEPTH   10
0014 
0015 /**
0016  * \name Arg type identifiers
0017  */
0018 #define POPT_ARG_NONE        0U /*!< no arg */
0019 #define POPT_ARG_STRING      1U /*!< arg will be saved as string */
0020 #define POPT_ARG_INT         2U /*!< arg ==> int */
0021 #define POPT_ARG_LONG        3U /*!< arg ==> long */
0022 #define POPT_ARG_INCLUDE_TABLE   4U /*!< arg points to table */
0023 #define POPT_ARG_CALLBACK    5U /*!< table-wide callback... must be
0024                        set first in table; arg points 
0025                        to callback, descrip points to 
0026                        callback data to pass */
0027 #define POPT_ARG_INTL_DOMAIN     6U /*!< set the translation domain
0028                        for this table and any
0029                        included tables; arg points
0030                        to the domain string */
0031 #define POPT_ARG_VAL         7U /*!< arg should take value val */
0032 #define POPT_ARG_FLOAT       8U /*!< arg ==> float */
0033 #define POPT_ARG_DOUBLE      9U /*!< arg ==> double */
0034 #define POPT_ARG_LONGLONG    10U    /*!< arg ==> long long */
0035 
0036 #define POPT_ARG_MAINCALL   (16U+11U)   /*!< EXPERIMENTAL: return (*arg) (argc, argv) */
0037 #define POPT_ARG_ARGV       12U /*!< dupe'd arg appended to realloc'd argv array. */
0038 #define POPT_ARG_SHORT      13U /*!< arg ==> short */
0039 #define POPT_ARG_BITSET     (16U+14U)   /*!< arg ==> bit set */
0040 
0041 #define POPT_ARG_MASK       0x000000FFU
0042 #define POPT_GROUP_MASK     0x0000FF00U
0043 
0044 /**
0045  * \name Arg modifiers
0046  */
0047 #define POPT_ARGFLAG_ONEDASH    0x80000000U  /*!< allow -longoption */
0048 #define POPT_ARGFLAG_DOC_HIDDEN 0x40000000U  /*!< don't show in help/usage */
0049 #define POPT_ARGFLAG_STRIP  0x20000000U  /*!< strip this arg from argv(only applies to long args) */
0050 #define POPT_ARGFLAG_OPTIONAL   0x10000000U  /*!< arg may be missing */
0051 
0052 #define POPT_ARGFLAG_OR     0x08000000U  /*!< arg will be or'ed */
0053 #define POPT_ARGFLAG_NOR    0x09000000U  /*!< arg will be nor'ed */
0054 #define POPT_ARGFLAG_AND    0x04000000U  /*!< arg will be and'ed */
0055 #define POPT_ARGFLAG_NAND   0x05000000U  /*!< arg will be nand'ed */
0056 #define POPT_ARGFLAG_XOR    0x02000000U  /*!< arg will be xor'ed */
0057 #define POPT_ARGFLAG_NOT    0x01000000U  /*!< arg will be negated */
0058 #define POPT_ARGFLAG_LOGICALOPS \
0059         (POPT_ARGFLAG_OR|POPT_ARGFLAG_AND|POPT_ARGFLAG_XOR)
0060 
0061 #define POPT_BIT_SET    (POPT_ARG_VAL|POPT_ARGFLAG_OR)
0062                     /*!< set arg bit(s) */
0063 #define POPT_BIT_CLR    (POPT_ARG_VAL|POPT_ARGFLAG_NAND)
0064                     /*!< clear arg bit(s) */
0065 
0066 #define POPT_ARGFLAG_SHOW_DEFAULT 0x00800000U /*!< show default value in --help */
0067 #define POPT_ARGFLAG_RANDOM 0x00400000U  /*!< random value in [1,arg] */
0068 #define POPT_ARGFLAG_TOGGLE 0x00200000U  /*!< permit --[no]opt prefix toggle */
0069 
0070 /**
0071  * \name Callback modifiers
0072  */
0073 #define POPT_CBFLAG_PRE     0x80000000U  /*!< call the callback before parse */
0074 #define POPT_CBFLAG_POST    0x40000000U  /*!< call the callback after parse */
0075 #define POPT_CBFLAG_INC_DATA    0x20000000U  /*!< use data from the include line,
0076                            not the subtable */
0077 #define POPT_CBFLAG_SKIPOPTION  0x10000000U  /*!< don't callback with option */
0078 #define POPT_CBFLAG_CONTINUE    0x08000000U  /*!< continue callbacks with option */
0079 
0080 /**
0081  * \name Error return values
0082  */
0083 #define POPT_ERROR_NOARG    -10 /*!< missing argument */
0084 #define POPT_ERROR_BADOPT   -11 /*!< unknown option */
0085 #define POPT_ERROR_UNWANTEDARG  -12 /*!< option does not take an argument */
0086 #define POPT_ERROR_OPTSTOODEEP  -13 /*!< aliases nested too deeply */
0087 #define POPT_ERROR_BADQUOTE -15 /*!< error in parameter quoting */
0088 #define POPT_ERROR_ERRNO    -16 /*!< errno set, use strerror(errno) */
0089 #define POPT_ERROR_BADNUMBER    -17 /*!< invalid numeric value */
0090 #define POPT_ERROR_OVERFLOW -18 /*!< number too large or too small */
0091 #define POPT_ERROR_BADOPERATION -19 /*!< mutually exclusive logical operations requested */
0092 #define POPT_ERROR_NULLARG  -20 /*!< opt->arg should not be NULL */
0093 #define POPT_ERROR_MALLOC   -21 /*!< memory allocation failed */
0094 #define POPT_ERROR_BADCONFIG    -22 /*!< config file failed sanity test */
0095 
0096 /**
0097  * \name poptBadOption() flags
0098  */
0099 #define POPT_BADOPTION_NOALIAS  (1U << 0)  /*!< don't go into an alias */
0100 
0101 /**
0102  * \name poptGetContext() flags
0103  */
0104 #define POPT_CONTEXT_NO_EXEC    (1U << 0)  /*!< ignore exec expansions */
0105 #define POPT_CONTEXT_KEEP_FIRST (1U << 1)  /*!< pay attention to argv[0] */
0106 #define POPT_CONTEXT_POSIXMEHARDER (1U << 2) /*!< options can't follow args */
0107 #define POPT_CONTEXT_ARG_OPTS   (1U << 4) /*!< return args as options with value 0 */
0108 
0109 /**
0110  */
0111 struct poptOption {
0112     const char * longName;  /*!< may be NULL */
0113     char shortName;     /*!< may be '\0' */
0114     unsigned int argInfo;   /*!< type of argument expected after the option */
0115     void * arg;         /*!< depends on argInfo */
0116     int val;            /*!< 0 means don't return, just update arg */
0117     const char * descrip;   /*!< description for autohelp -- may be NULL */
0118     const char * argDescrip;    /*!< argument description for autohelp -- may be NULL */
0119 };
0120 
0121 /**
0122  * A popt alias argument for poptAddAlias().
0123  */
0124 struct poptAlias {
0125     const char * longName;  /*!< may be NULL */
0126     char shortName;     /*!< may be NUL */
0127     int argc;
0128     const char ** argv;     /*!< must be free()able */
0129 };
0130 
0131 /**
0132  * A popt alias or exec argument for poptAddItem().
0133  */
0134 typedef struct poptItem_s {
0135     struct poptOption option;   /*!< alias/exec name(s) and description. */
0136     int argc;           /*!< (alias) no. of args. */
0137     const char ** argv;     /*!< (alias) args, must be free()able. */
0138 } * poptItem;
0139 
0140 /**
0141  * \name Auto-generated help/usage
0142  */
0143 
0144 /**
0145  * Empty table marker to enable displaying popt alias/exec options.
0146  */
0147 extern struct poptOption poptAliasOptions[];
0148 #define POPT_AUTOALIAS { NULL, '\0', POPT_ARG_INCLUDE_TABLE, poptAliasOptions, \
0149             0, "Options implemented via popt alias/exec:", NULL },
0150 
0151 /**
0152  * Auto help table options.
0153  */
0154 extern struct poptOption poptHelpOptions[];
0155 
0156 extern struct poptOption * poptHelpOptionsI18N;
0157 
0158 #define POPT_AUTOHELP { NULL, '\0', POPT_ARG_INCLUDE_TABLE, poptHelpOptions, \
0159             0, "Help options:", NULL },
0160 
0161 #define POPT_TABLEEND { NULL, '\0', 0, NULL, 0, NULL, NULL }
0162 
0163 /**
0164  */
0165 typedef struct poptContext_s * poptContext;
0166 
0167 /**
0168  */
0169 #ifndef __cplusplus
0170 typedef struct poptOption * poptOption;
0171 #endif
0172 
0173 /**
0174  */
0175 enum poptCallbackReason {
0176     POPT_CALLBACK_REASON_PRE    = 0, 
0177     POPT_CALLBACK_REASON_POST   = 1,
0178     POPT_CALLBACK_REASON_OPTION = 2
0179 };
0180 
0181 #ifdef __cplusplus
0182 extern "C" {
0183 #endif
0184 
0185 /**
0186  * Table callback prototype.
0187  * @param con       context
0188  * @param reason    reason for callback
0189  * @param opt       option that triggered callback
0190  * @param arg       @todo Document.
0191  * @param data      @todo Document.
0192  */
0193 typedef void (*poptCallbackType) (poptContext con, 
0194         enum poptCallbackReason reason,
0195         const struct poptOption * opt,
0196         const char * arg,
0197         const void * data);
0198 
0199 /**
0200  * Destroy context.
0201  * @param con       context
0202  * @return      NULL always
0203  */
0204 poptContext poptFreeContext( poptContext con);
0205 
0206 /**
0207  * Initialize popt context.
0208  * @param name      context name (usually argv[0] program name)
0209  * @param argc      no. of arguments
0210  * @param argv      argument array
0211  * @param options   address of popt option table
0212  * @param flags     or'd POPT_CONTEXT_* bits
0213  * @return      initialized popt context
0214  */
0215 poptContext poptGetContext(
0216         const char * name,
0217         int argc, const char ** argv,
0218         const struct poptOption * options,
0219         unsigned int flags);
0220 
0221 /**
0222  * Destroy context (alternative implementation).
0223  * @param con       context
0224  * @return      NULL always
0225  */
0226 poptContext poptFini( poptContext con);
0227 
0228 /**
0229  * Initialize popt context (alternative implementation).
0230  * This routine does poptGetContext() and then poptReadConfigFiles().
0231  * @param argc      no. of arguments
0232  * @param argv      argument array
0233  * @param options   address of popt option table
0234  * @param configPaths   colon separated file path(s) to read.
0235  * @return      initialized popt context (NULL on error).
0236  */
0237 poptContext poptInit(int argc, const char ** argv,
0238         const struct poptOption * options,
0239         const char * configPaths);
0240 
0241 /**
0242  * Reinitialize popt context.
0243  * @param con       context
0244  */
0245 void poptResetContext(poptContext con);
0246 
0247 /**
0248  * Return value of next option found.
0249  * @param con       context
0250  * @return      next option val, -1 on last item, POPT_ERROR_* on error
0251  */
0252 int poptGetNextOpt(poptContext con);
0253 
0254 /**
0255  * Return next option argument (if any).
0256  * @param con       context
0257  * @return      option argument, NULL if no argument is available
0258  */
0259 char * poptGetOptArg(poptContext con);
0260 
0261 /**
0262  * Return next argument.
0263  * @param con       context
0264  * @return      next argument, NULL if no argument is available
0265  */
0266 const char * poptGetArg(poptContext con);
0267 
0268 /**
0269  * Peek at current argument.
0270  * @param con       context
0271  * @return      current argument, NULL if no argument is available
0272  */
0273 const char * poptPeekArg(poptContext con);
0274 
0275 /**
0276  * Return remaining arguments.
0277  * @param con       context
0278  * @return      argument array, NULL terminated
0279  */
0280 const char ** poptGetArgs(poptContext con);
0281 
0282 /**
0283  * Return the option which caused the most recent error.
0284  * @param con       context
0285  * @param flags
0286  * @return      offending option
0287  */
0288 const char * poptBadOption(poptContext con, unsigned int flags);
0289 
0290 /**
0291  * Add arguments to context.
0292  * @param con       context
0293  * @param argv      argument array, NULL terminated
0294  * @return      0 on success, POPT_ERROR_OPTSTOODEEP on failure
0295  */
0296 int poptStuffArgs(poptContext con, const char ** argv);
0297 
0298 /**
0299  * Add alias to context.
0300  * @todo Pass alias by reference, not value.
0301  * @deprecated Use poptAddItem instead.
0302  * @param con       context
0303  * @param alias     alias to add
0304  * @param flags     (unused)
0305  * @return      0 on success
0306  */
0307 int poptAddAlias(poptContext con, struct poptAlias alias, int flags);
0308 
0309 /**
0310  * Add alias/exec item to context.
0311  * @param con       context
0312  * @param newItem   alias/exec item to add
0313  * @param flags     0 for alias, 1 for exec
0314  * @return      0 on success
0315  */
0316 int poptAddItem(poptContext con, poptItem newItem, int flags);
0317 
0318 /**
0319  * Test path/file for config file sanity (regular file, permissions etc)
0320  * @param fn        file name
0321  * @return      1 on OK, 0 on NOTOK.
0322  */
0323 int poptSaneFile(const char * fn);
0324 
0325 /**
0326  * Read a file into a buffer.
0327  * @param fn        file name
0328  * @retval *bp      buffer (malloc'd) (or NULL)
0329  * @retval *nbp     no. of bytes in buffer (including final NUL) (or NULL)
0330  * @param flags     1 to trim escaped newlines
0331  * return       0 on success
0332  */
0333 int poptReadFile(const char * fn, char ** bp,
0334         size_t * nbp, int flags);
0335 #define POPT_READFILE_TRIMNEWLINES  1
0336 
0337 /**
0338  * Read configuration file.
0339  * @param con       context
0340  * @param fn        file name to read
0341  * @return      0 on success, POPT_ERROR_ERRNO on failure
0342  */
0343 int poptReadConfigFile(poptContext con, const char * fn);
0344 
0345 /**
0346  * Read configuration file(s).
0347  * Colon separated files to read, looping over poptReadConfigFile().
0348  * Note that an '@' character preceding a path in the list will
0349  * also perform additional sanity checks on the file before reading.
0350  * @param con       context
0351  * @param paths     colon separated file name(s) to read
0352  * @return      0 on success, POPT_ERROR_BADCONFIG on failure
0353  */
0354 int poptReadConfigFiles(poptContext con, const char * paths);
0355 
0356 /**
0357  * Read default configuration from /etc/popt and $HOME/.popt.
0358  * @param con       context
0359  * @param useEnv    (unused)
0360  * @return      0 on success, POPT_ERROR_ERRNO on failure
0361  */
0362 int poptReadDefaultConfig(poptContext con, int useEnv);
0363 
0364 /**
0365  * Duplicate an argument array.
0366  * @note: The argument array is malloc'd as a single area, so only argv must
0367  * be free'd.
0368  *
0369  * @param argc      no. of arguments
0370  * @param argv      argument array
0371  * @retval argcPtr  address of returned no. of arguments
0372  * @retval argvPtr  address of returned argument array
0373  * @return      0 on success, POPT_ERROR_NOARG on failure
0374  */
0375 int poptDupArgv(int argc, const char **argv,
0376         int * argcPtr,
0377         const char *** argvPtr);
0378 
0379 /**
0380  * Parse a string into an argument array.
0381  * The parse allows ', ", and \ quoting, but ' is treated the same as " and
0382  * both may include \ quotes.
0383  * @note: The argument array is malloc'd as a single area, so only argv must
0384  * be free'd.
0385  *
0386  * @param s     string to parse
0387  * @retval argcPtr  address of returned no. of arguments
0388  * @retval argvPtr  address of returned argument array
0389  */
0390 int poptParseArgvString(const char * s,
0391         int * argcPtr, const char *** argvPtr);
0392 
0393 /**
0394  * Parses an input configuration file and returns an string that is a 
0395  * command line.  For use with popt.  You must free the return value when done.
0396  *
0397  * Given the file:
0398 \verbatim
0399 # this line is ignored
0400     #   this one too
0401 aaa
0402   bbb
0403     ccc   
0404 bla=bla
0405 
0406 this_is   =   fdsafdas
0407      bad_line=        
0408   really bad line
0409   really bad line  = again
0410 5555=   55555   
0411   test = with lots of spaces
0412 \endverbatim
0413 *
0414 * The result is:
0415 \verbatim
0416 --aaa --bbb --ccc --bla="bla" --this_is="fdsafdas" --5555="55555" --test="with lots of spaces"
0417 \endverbatim
0418 *
0419 * Passing this to poptParseArgvString() yields an argv of:
0420 \verbatim
0421 '--aaa'
0422 '--bbb' 
0423 '--ccc' 
0424 '--bla=bla' 
0425 '--this_is=fdsafdas' 
0426 '--5555=55555' 
0427 '--test=with lots of spaces' 
0428 \endverbatim
0429  *
0430  * @bug NULL is returned if file line is too long.
0431  * @bug Silently ignores invalid lines.
0432  *
0433  * @param fp        file handle to read
0434  * @param *argstrp  return string of options (malloc'd)
0435  * @param flags     unused
0436  * @return      0 on success
0437  * @see         poptParseArgvString
0438  */
0439 int poptConfigFileToString(FILE *fp, char ** argstrp, int flags);
0440 
0441 /**
0442  * Return formatted error string for popt failure.
0443  * @param error     popt error
0444  * @return      error string
0445  */
0446 const char * poptStrerror(const int error);
0447 
0448 /**
0449  * Limit search for executables.
0450  * @param con       context
0451  * @param path      single path to search for executables
0452  * @param allowAbsolute absolute paths only?
0453  */
0454 void poptSetExecPath(poptContext con, const char * path, int allowAbsolute);
0455 
0456 /**
0457  * Print detailed description of options.
0458  * @param con       context
0459  * @param fp        output file handle
0460  * @param flags     (unused)
0461  */
0462 void poptPrintHelp(poptContext con, FILE * fp, int flags);
0463 
0464 /**
0465  * Print terse description of options.
0466  * @param con       context
0467  * @param fp        output file handle
0468  * @param flags     (unused)
0469  */
0470 void poptPrintUsage(poptContext con, FILE * fp, int flags);
0471 
0472 /**
0473  * Provide text to replace default "[OPTION...]" in help/usage output.
0474  * @param con       context
0475  * @param text      replacement text
0476  */
0477 void poptSetOtherOptionHelp(poptContext con, const char * text);
0478 
0479 /**
0480  * Return argv[0] from context.
0481  * @param con       context
0482  * @return      argv[0]
0483  */
0484 const char * poptGetInvocationName(poptContext con);
0485 
0486 /**
0487  * Shuffle argv pointers to remove stripped args, returns new argc.
0488  * @param con       context
0489  * @param argc      no. of args
0490  * @param argv      arg vector
0491  * @return      new argc
0492  */
0493 int poptStrippedArgv(poptContext con, int argc, char ** argv);
0494 
0495 /**
0496  * Add a string to an argv array.
0497  * @retval *argvp   argv array
0498  * @param argInfo   (unused)
0499  * @param val       string arg to add (using strdup)
0500  * @return      0 on success, POPT_ERROR_NULLARG/POPT_ERROR_BADOPERATION
0501  */
0502 int poptSaveString(const char *** argvp, unsigned int argInfo,
0503         const char * val);
0504 
0505 /**
0506  * Save a long long, performing logical operation with value.
0507  * @warning Alignment check may be too strict on certain platorms.
0508  * @param arg       integer pointer, aligned on int boundary.
0509  * @param argInfo   logical operation (see POPT_ARGFLAG_*)
0510  * @param aLongLong value to use
0511  * @return      0 on success, POPT_ERROR_NULLARG/POPT_ERROR_BADOPERATION
0512  */
0513 int poptSaveLongLong(long long * arg, unsigned int argInfo,
0514         long long aLongLong);
0515 
0516 /**
0517  * Save a long, performing logical operation with value.
0518  * @warning Alignment check may be too strict on certain platorms.
0519  * @param arg       integer pointer, aligned on int boundary.
0520  * @param argInfo   logical operation (see POPT_ARGFLAG_*)
0521  * @param aLong     value to use
0522  * @return      0 on success, POPT_ERROR_NULLARG/POPT_ERROR_BADOPERATION
0523  */
0524 int poptSaveLong(long * arg, unsigned int argInfo, long aLong);
0525 
0526 /**
0527  * Save a short integer, performing logical operation with value.
0528  * @warning Alignment check may be too strict on certain platorms.
0529  * @param arg       short pointer, aligned on short boundary.
0530  * @param argInfo   logical operation (see POPT_ARGFLAG_*)
0531  * @param aLong     value to use
0532  * @return      0 on success, POPT_ERROR_NULLARG/POPT_ERROR_BADOPERATION
0533  */
0534 int poptSaveShort(short * arg, unsigned int argInfo, long aLong);
0535 
0536 /**
0537  * Save an integer, performing logical operation with value.
0538  * @warning Alignment check may be too strict on certain platorms.
0539  * @param arg       integer pointer, aligned on int boundary.
0540  * @param argInfo   logical operation (see POPT_ARGFLAG_*)
0541  * @param aLong     value to use
0542  * @return      0 on success, POPT_ERROR_NULLARG/POPT_ERROR_BADOPERATION
0543  */
0544 int poptSaveInt(int * arg, unsigned int argInfo, long aLong);
0545 
0546 /* The bit set typedef. */
0547 typedef struct poptBits_s {
0548     unsigned int bits[1];
0549 } * poptBits;
0550 
0551 #define _POPT_BITS_N    1024U   /*!< estimated population */
0552 #define _POPT_BITS_M    ((3U * _POPT_BITS_N) / 2U)
0553 #define _POPT_BITS_K    16U /*!< no. of linear hash combinations */
0554 
0555 extern unsigned int _poptBitsN;
0556 extern  unsigned int _poptBitsM;
0557 extern  unsigned int _poptBitsK;
0558 
0559 int poptBitsAdd(poptBits bits, const char * s);
0560 int poptBitsChk(poptBits bits, const char * s);
0561 int poptBitsClr(poptBits bits);
0562 int poptBitsDel(poptBits bits, const char * s);
0563 int poptBitsIntersect(poptBits * ap, const poptBits b);
0564 int poptBitsUnion(poptBits * ap, const poptBits b);
0565 int poptBitsArgs(poptContext con, poptBits * ap);
0566 
0567 /**
0568  * Save a string into a bit set (experimental).
0569  * @retval *bits    bit set (lazily malloc'd if NULL)
0570  * @param argInfo   logical operation (see POPT_ARGFLAG_*)
0571  * @param s     string to add to bit set
0572  * @return      0 on success, POPT_ERROR_NULLARG/POPT_ERROR_BADOPERATION
0573  */
0574 int poptSaveBits(poptBits * bitsp, unsigned int argInfo,
0575         const char * s);
0576 
0577 
0578 #ifdef  __cplusplus
0579 }
0580 #endif
0581 
0582 #endif