diff options
author | Karel Zak | 2012-07-26 09:04:55 +0200 |
---|---|---|
committer | Karel Zak | 2012-07-26 09:22:35 +0200 |
commit | 217d20a73c6ad502292af8a4e9e1158949b62346 (patch) | |
tree | 7207135a7309342d37fe0169a78ed2370f4bafb7 /include/optutils.h | |
parent | lsblk: fix dm_name usage (diff) | |
download | kernel-qcow2-util-linux-217d20a73c6ad502292af8a4e9e1158949b62346.tar.gz kernel-qcow2-util-linux-217d20a73c6ad502292af8a4e9e1158949b62346.tar.xz kernel-qcow2-util-linux-217d20a73c6ad502292af8a4e9e1158949b62346.zip |
include/optutils: add err_exclusive_options()
This is improved implementation of exclusive_option(). The new
implementation:
- uses regular struct option to compose error messages
- relationships between options are described in one const static array
- the current status is stored in one int array
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'include/optutils.h')
-rw-r--r-- | include/optutils.h | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/include/optutils.h b/include/optutils.h index fae28fd0d..7819ab360 100644 --- a/include/optutils.h +++ b/include/optutils.h @@ -17,6 +17,8 @@ static inline const char *option_to_longopt(int c, const struct option *opts) #ifndef OPTUTILS_EXIT_CODE # define OPTUTILS_EXIT_CODE EXIT_FAILURE #endif + +/* deprecated */ static inline void exclusive_option(int *what, const int how, const char *errmesg) { @@ -30,5 +32,78 @@ static inline void exclusive_option(int *what, const int how, _("options %s are mutually exclusive"), errmesg); } +/* + * Check collisions between options. + * + * The conflicts between options are described in ul_excl_t array. The + * array contains groups of mutually exclusive options. For example + * + * static const ul_excl_t excl[] = { + * { 'Z','b','c' }, // first group + * { 'b','x' }, // second group + * { 0 } + * }; + * + * int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; + * + * while ((c = getopt_long(argc, argv, "Zbcx", longopts, NULL)) != -1) { + * + * err_exclusive_options(c, longopts, excl, excl_st); + * + * switch (c) { + * case 'Z': + * .... + * } + * } + * + * The array excl[] defines two groups of the mutually exclusive options. The + * option '-b' is in the both groups. + * + * Note that the options in the group have to be in ASCII order (ABC..abc..) and + * groups have to be also in ASCII order. + * + * The current status of options is stored in excl_st array. The size of the array + * must be the same as number of the groups in the ul_excl_t array. + * + * If you're unsure then see sys-utils/mount.c or misc-utils/findmnt.c. + */ +#define UL_EXCL_STATUS_INIT { 0 } +typedef int ul_excl_t[16]; + +static inline void err_exclusive_options( + int c, + const struct option *opts, + const ul_excl_t *excl, + int *status) +{ + int e; + + for (e = 0; excl[e][0] && excl[e][0] <= c; e++) { + const int *op = excl[e]; + + for (; *op && *op <= c; op++) { + if (*op != c) + continue; + if (status[e] == 0) + status[e] = c; + else if (status[e] != c) { + fprintf(stderr, _("%s: options "), + program_invocation_short_name); + for (op = excl[e]; *op; op++) { + if (opts) + fprintf(stderr, "--%s ", + option_to_longopt(*op, opts)); + else + fprintf(stderr, "-%c ", *op); + } + fprintf(stderr, _("are mutually exclusive.")); + fputc('\n', stderr); + exit(OPTUTILS_EXIT_CODE); + } + break; + } + } +} + #endif |