diff options
author | Karel Zak | 2015-02-24 12:04:22 +0100 |
---|---|---|
committer | Karel Zak | 2015-02-24 12:04:22 +0100 |
commit | 30b294c491b4577d5f0e5c94f17cf9e36a8ecb72 (patch) | |
tree | 087407622ab14e28164f8f6447e036eadcc2fb43 /lib/strutils.c | |
parent | lib/strutils: move parse_switch() from setterm(1) to library (diff) | |
download | kernel-qcow2-util-linux-30b294c491b4577d5f0e5c94f17cf9e36a8ecb72.tar.gz kernel-qcow2-util-linux-30b294c491b4577d5f0e5c94f17cf9e36a8ecb72.tar.xz kernel-qcow2-util-linux-30b294c491b4577d5f0e5c94f17cf9e36a8ecb72.zip |
lib/strutils: extend parse_switch() to accept more options
* allow to specify more 0|1 pairs
* allow to specify error message
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'lib/strutils.c')
-rw-r--r-- | lib/strutils.c | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/lib/strutils.c b/lib/strutils.c index f6154aaa4..dd776e7ab 100644 --- a/lib/strutils.c +++ b/lib/strutils.c @@ -181,13 +181,31 @@ int isdigit_string(const char *str) return p && p > str && !*p; } -int parse_switch(const char *arg, const char *a, const char *b) +/* + * parse_switch(argv[i], "on", "off", "yes", "no", NULL); + */ +int parse_switch(const char *arg, const char *errmesg, ...) { - if (strcmp(arg, a) == 0) - return 1; - else if (strcmp(arg, b) == 0) - return 0; - errx(STRTOXX_EXIT_CODE, _("argument error: %s"), arg); + const char *a, *b; + va_list ap; + + va_start(ap, errmesg); + do { + a = va_arg(ap, char *); + if (!a) + break; + b = va_arg(ap, char *); + if (!b) + break; + + if (strcmp(arg, a) == 0) + return 1; + else if (strcmp(arg, b) == 0) + return 0; + } while (1); + va_end(ap); + + errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, arg); } #ifndef HAVE_MEMPCPY |