Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __SUBCMD_PARSE_OPTIONS_H
0003 #define __SUBCMD_PARSE_OPTIONS_H
0004 
0005 #include <linux/kernel.h>
0006 #include <stdbool.h>
0007 #include <stdint.h>
0008 
0009 #ifndef NORETURN
0010 #define NORETURN __attribute__((__noreturn__))
0011 #endif
0012 
0013 enum parse_opt_type {
0014     /* special types */
0015     OPTION_END,
0016     OPTION_ARGUMENT,
0017     OPTION_GROUP,
0018     /* options with no arguments */
0019     OPTION_BIT,
0020     OPTION_BOOLEAN,
0021     OPTION_INCR,
0022     OPTION_SET_UINT,
0023     OPTION_SET_PTR,
0024     /* options with arguments (usually) */
0025     OPTION_STRING,
0026     OPTION_INTEGER,
0027     OPTION_LONG,
0028     OPTION_ULONG,
0029     OPTION_CALLBACK,
0030     OPTION_U64,
0031     OPTION_UINTEGER,
0032 };
0033 
0034 enum parse_opt_flags {
0035     PARSE_OPT_KEEP_DASHDASH = 1,
0036     PARSE_OPT_STOP_AT_NON_OPTION = 2,
0037     PARSE_OPT_KEEP_ARGV0 = 4,
0038     PARSE_OPT_KEEP_UNKNOWN = 8,
0039     PARSE_OPT_NO_INTERNAL_HELP = 16,
0040 };
0041 
0042 enum parse_opt_option_flags {
0043     PARSE_OPT_OPTARG  = 1,
0044     PARSE_OPT_NOARG   = 2,
0045     PARSE_OPT_NONEG   = 4,
0046     PARSE_OPT_HIDDEN  = 8,
0047     PARSE_OPT_LASTARG_DEFAULT = 16,
0048     PARSE_OPT_DISABLED = 32,
0049     PARSE_OPT_EXCLUSIVE = 64,
0050     PARSE_OPT_NOEMPTY  = 128,
0051     PARSE_OPT_NOBUILD  = 256,
0052     PARSE_OPT_CANSKIP  = 512,
0053 };
0054 
0055 struct option;
0056 typedef int parse_opt_cb(const struct option *, const char *arg, int unset);
0057 
0058 /*
0059  * `type`::
0060  *   holds the type of the option, you must have an OPTION_END last in your
0061  *   array.
0062  *
0063  * `short_name`::
0064  *   the character to use as a short option name, '\0' if none.
0065  *
0066  * `long_name`::
0067  *   the long option name, without the leading dashes, NULL if none.
0068  *
0069  * `value`::
0070  *   stores pointers to the values to be filled.
0071  *
0072  * `argh`::
0073  *   token to explain the kind of argument this option wants. Keep it
0074  *   homogeneous across the repository.
0075  *
0076  * `help`::
0077  *   the short help associated to what the option does.
0078  *   Must never be NULL (except for OPTION_END).
0079  *   OPTION_GROUP uses this pointer to store the group header.
0080  *
0081  * `flags`::
0082  *   mask of parse_opt_option_flags.
0083  *   PARSE_OPT_OPTARG: says that the argument is optional (not for BOOLEANs)
0084  *   PARSE_OPT_NOARG: says that this option takes no argument, for CALLBACKs
0085  *   PARSE_OPT_NONEG: says that this option cannot be negated
0086  *   PARSE_OPT_HIDDEN this option is skipped in the default usage, showed in
0087  *                    the long one.
0088  *
0089  * `callback`::
0090  *   pointer to the callback to use for OPTION_CALLBACK.
0091  *
0092  * `defval`::
0093  *   default value to fill (*->value) with for PARSE_OPT_OPTARG.
0094  *   OPTION_{BIT,SET_UINT,SET_PTR} store the {mask,integer,pointer} to put in
0095  *   the value when met.
0096  *   CALLBACKS can use it like they want.
0097  *
0098  * `set`::
0099  *   whether an option was set by the user
0100  */
0101 struct option {
0102     enum parse_opt_type type;
0103     int short_name;
0104     const char *long_name;
0105     void *value;
0106     const char *argh;
0107     const char *help;
0108     const char *build_opt;
0109 
0110     int flags;
0111     parse_opt_cb *callback;
0112     intptr_t defval;
0113     bool *set;
0114     void *data;
0115     const struct option *parent;
0116 };
0117 
0118 #define check_vtype(v, type) ( BUILD_BUG_ON_ZERO(!__builtin_types_compatible_p(typeof(v), type)) + v )
0119 
0120 #define OPT_END()                   { .type = OPTION_END }
0121 #define OPT_PARENT(p)               { .type = OPTION_END, .parent = (p) }
0122 #define OPT_ARGUMENT(l, h)          { .type = OPTION_ARGUMENT, .long_name = (l), .help = (h) }
0123 #define OPT_GROUP(h)                { .type = OPTION_GROUP, .help = (h) }
0124 #define OPT_BIT(s, l, v, h, b)      { .type = OPTION_BIT, .short_name = (s), .long_name = (l), .value = check_vtype(v, int *), .help = (h), .defval = (b) }
0125 #define OPT_BOOLEAN(s, l, v, h)     { .type = OPTION_BOOLEAN, .short_name = (s), .long_name = (l), .value = check_vtype(v, bool *), .help = (h) }
0126 #define OPT_BOOLEAN_FLAG(s, l, v, h, f)     { .type = OPTION_BOOLEAN, .short_name = (s), .long_name = (l), .value = check_vtype(v, bool *), .help = (h), .flags = (f) }
0127 #define OPT_BOOLEAN_SET(s, l, v, os, h) \
0128     { .type = OPTION_BOOLEAN, .short_name = (s), .long_name = (l), \
0129     .value = check_vtype(v, bool *), .help = (h), \
0130     .set = check_vtype(os, bool *)}
0131 #define OPT_INCR(s, l, v, h)        { .type = OPTION_INCR, .short_name = (s), .long_name = (l), .value = check_vtype(v, int *), .help = (h) }
0132 #define OPT_SET_UINT(s, l, v, h, i)  { .type = OPTION_SET_UINT, .short_name = (s), .long_name = (l), .value = check_vtype(v, unsigned int *), .help = (h), .defval = (i) }
0133 #define OPT_SET_PTR(s, l, v, h, p)  { .type = OPTION_SET_PTR, .short_name = (s), .long_name = (l), .value = (v), .help = (h), .defval = (p) }
0134 #define OPT_INTEGER(s, l, v, h)     { .type = OPTION_INTEGER, .short_name = (s), .long_name = (l), .value = check_vtype(v, int *), .help = (h) }
0135 #define OPT_UINTEGER(s, l, v, h)    { .type = OPTION_UINTEGER, .short_name = (s), .long_name = (l), .value = check_vtype(v, unsigned int *), .help = (h) }
0136 #define OPT_UINTEGER_OPTARG(s, l, v, d, h)    { .type = OPTION_UINTEGER, .short_name = (s), .long_name = (l), .value = check_vtype(v, unsigned int *), .help = (h), .flags = PARSE_OPT_OPTARG, .defval = (intptr_t)(d) }
0137 #define OPT_LONG(s, l, v, h)        { .type = OPTION_LONG, .short_name = (s), .long_name = (l), .value = check_vtype(v, long *), .help = (h) }
0138 #define OPT_ULONG(s, l, v, h)        { .type = OPTION_ULONG, .short_name = (s), .long_name = (l), .value = check_vtype(v, unsigned long *), .help = (h) }
0139 #define OPT_U64(s, l, v, h)         { .type = OPTION_U64, .short_name = (s), .long_name = (l), .value = check_vtype(v, u64 *), .help = (h) }
0140 #define OPT_STRING(s, l, v, a, h)   { .type = OPTION_STRING,  .short_name = (s), .long_name = (l), .value = check_vtype(v, const char **), .argh = (a), .help = (h) }
0141 #define OPT_STRING_OPTARG(s, l, v, a, h, d) \
0142     { .type = OPTION_STRING,  .short_name = (s), .long_name = (l), \
0143       .value = check_vtype(v, const char **), .argh =(a), .help = (h), \
0144       .flags = PARSE_OPT_OPTARG, .defval = (intptr_t)(d) }
0145 #define OPT_STRING_OPTARG_SET(s, l, v, os, a, h, d) \
0146     { .type = OPTION_STRING, .short_name = (s), .long_name = (l), \
0147       .value = check_vtype(v, const char **), .argh = (a), .help = (h), \
0148       .flags = PARSE_OPT_OPTARG, .defval = (intptr_t)(d), \
0149       .set = check_vtype(os, bool *)}
0150 #define OPT_STRING_NOEMPTY(s, l, v, a, h)   { .type = OPTION_STRING,  .short_name = (s), .long_name = (l), .value = check_vtype(v, const char **), .argh = (a), .help = (h), .flags = PARSE_OPT_NOEMPTY}
0151 #define OPT_DATE(s, l, v, h) \
0152     { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), .argh = "time", .help = (h), .callback = parse_opt_approxidate_cb }
0153 #define OPT_CALLBACK(s, l, v, a, h, f) \
0154     { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), .argh = (a), .help = (h), .callback = (f) }
0155 #define OPT_CALLBACK_SET(s, l, v, os, a, h, f) \
0156     { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), .argh = (a), .help = (h), .callback = (f), .set = check_vtype(os, bool *)}
0157 #define OPT_CALLBACK_NOOPT(s, l, v, a, h, f) \
0158     { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), .argh = (a), .help = (h), .callback = (f), .flags = PARSE_OPT_NOARG }
0159 #define OPT_CALLBACK_DEFAULT(s, l, v, a, h, f, d) \
0160     { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), .argh = (a), .help = (h), .callback = (f), .defval = (intptr_t)d, .flags = PARSE_OPT_LASTARG_DEFAULT }
0161 #define OPT_CALLBACK_DEFAULT_NOOPT(s, l, v, a, h, f, d) \
0162     { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l),\
0163     .value = (v), .arg = (a), .help = (h), .callback = (f), .defval = (intptr_t)d,\
0164     .flags = PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NOARG}
0165 #define OPT_CALLBACK_OPTARG(s, l, v, d, a, h, f) \
0166     { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), \
0167       .value = (v), .argh = (a), .help = (h), .callback = (f), \
0168       .flags = PARSE_OPT_OPTARG, .data = (d) }
0169 
0170 /* parse_options() will filter out the processed options and leave the
0171  * non-option argments in argv[].
0172  * Returns the number of arguments left in argv[].
0173  *
0174  * NOTE: parse_options() and parse_options_subcommand() may call exit() in the
0175  * case of an error (or for 'special' options like --list-cmds or --list-opts).
0176  */
0177 extern int parse_options(int argc, const char **argv,
0178                          const struct option *options,
0179                          const char * const usagestr[], int flags);
0180 
0181 extern int parse_options_subcommand(int argc, const char **argv,
0182                 const struct option *options,
0183                 const char *const subcommands[],
0184                 const char *usagestr[], int flags);
0185 
0186 extern NORETURN void usage_with_options(const char * const *usagestr,
0187                                         const struct option *options);
0188 extern NORETURN __attribute__((format(printf,3,4)))
0189 void usage_with_options_msg(const char * const *usagestr,
0190                 const struct option *options,
0191                 const char *fmt, ...);
0192 
0193 /*----- incremantal advanced APIs -----*/
0194 
0195 enum {
0196     PARSE_OPT_HELP = -1,
0197     PARSE_OPT_DONE,
0198     PARSE_OPT_LIST_OPTS,
0199     PARSE_OPT_LIST_SUBCMDS,
0200     PARSE_OPT_UNKNOWN,
0201 };
0202 
0203 /*
0204  * It's okay for the caller to consume argv/argc in the usual way.
0205  * Other fields of that structure are private to parse-options and should not
0206  * be modified in any way.
0207  */
0208 struct parse_opt_ctx_t {
0209     const char **argv;
0210     const char **out;
0211     int argc, cpidx;
0212     const char *opt;
0213     const struct option *excl_opt;
0214     int flags;
0215 };
0216 
0217 extern int parse_options_usage(const char * const *usagestr,
0218                    const struct option *opts,
0219                    const char *optstr,
0220                    bool short_opt);
0221 
0222 
0223 /*----- some often used options -----*/
0224 extern int parse_opt_abbrev_cb(const struct option *, const char *, int);
0225 extern int parse_opt_approxidate_cb(const struct option *, const char *, int);
0226 extern int parse_opt_verbosity_cb(const struct option *, const char *, int);
0227 
0228 #define OPT__VERBOSE(var)  OPT_BOOLEAN('v', "verbose", (var), "be verbose")
0229 #define OPT__QUIET(var)    OPT_BOOLEAN('q', "quiet",   (var), "be quiet")
0230 #define OPT__VERBOSITY(var) \
0231     { OPTION_CALLBACK, 'v', "verbose", (var), NULL, "be more verbose", \
0232       PARSE_OPT_NOARG, &parse_opt_verbosity_cb, 0 }, \
0233     { OPTION_CALLBACK, 'q', "quiet", (var), NULL, "be more quiet", \
0234       PARSE_OPT_NOARG, &parse_opt_verbosity_cb, 0 }
0235 #define OPT__DRY_RUN(var)  OPT_BOOLEAN('n', "dry-run", (var), "dry run")
0236 #define OPT__ABBREV(var)  \
0237     { OPTION_CALLBACK, 0, "abbrev", (var), "n", \
0238       "use <n> digits to display SHA-1s", \
0239       PARSE_OPT_OPTARG, &parse_opt_abbrev_cb, 0 }
0240 
0241 extern const char *parse_options_fix_filename(const char *prefix, const char *file);
0242 
0243 void set_option_flag(struct option *opts, int sopt, const char *lopt, int flag);
0244 void set_option_nobuild(struct option *opts, int shortopt, const char *longopt,
0245             const char *build_opt, bool can_skip);
0246 
0247 #endif /* __SUBCMD_PARSE_OPTIONS_H */