From bb56f9a6f319b10b39ef5f8f02dd88d3968c3b11 Mon Sep 17 00:00:00 2001 Message-Id: In-Reply-To: References: From: Kevin Wolf Date: Fri, 21 Feb 2014 16:24:03 +0100 Subject: [CHANGE 06/11] qemu-option: has_help_option() and is_valid_option_list() To: rhvirt-patches@redhat.com, jen@redhat.com has_help_option() checks if any help option ('help' or '?') occurs anywhere in an option string, so that things like 'cluster_size=4k,help' are recognised. is_valid_option_list() ensures that the option list doesn't have options with leading commas or trailing unescaped commas. Signed-off-by: Kevin Wolf Reviewed-by: Jeff Cody Reviewed-by: Eric Blake (cherry picked from commit 7cc07ab8daa01f100f36ab63382d491f2d278c64) RHEL 6 doesn't have commit c8057f95 ('Support 'help' as a synonym for '?' in command line options'), which introduced is_help_option(), so the call is replaced by an open-coded comparison with '?' like elsewhere. Signed-off-by: Kevin Wolf Signed-off-by: Jeff E. Nelson --- qemu-option.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ qemu-option.h | 2 ++ 2 files changed, 51 insertions(+) diff --git a/qemu-option.c b/qemu-option.c index 39ecec2..98b3302 100644 --- a/qemu-option.c +++ b/qemu-option.c @@ -467,6 +467,55 @@ fail: return NULL; } +bool has_help_option(const char *param) +{ + size_t buflen = strlen(param) + 1; + char *buf = g_malloc0(buflen); + const char *p = param; + bool result = false; + + while (*p) { + p = get_opt_value(buf, buflen, p); + if (*p) { + p++; + } + + if (!strcmp(buf, "?")) { + result = true; + goto out; + } + } + +out: + free(buf); + return result; +} + +bool is_valid_option_list(const char *param) +{ + size_t buflen = strlen(param) + 1; + char *buf = g_malloc0(buflen); + const char *p = param; + bool result = true; + + while (*p) { + p = get_opt_value(buf, buflen, p); + if (*p && !*++p) { + result = false; + goto out; + } + + if (!*buf || *buf == ',') { + result = false; + goto out; + } + } + +out: + free(buf); + return result; +} + /* * Prints all options of a list that have a value to stdout */ diff --git a/qemu-option.h b/qemu-option.h index 4f65730..afb4e0c 100644 --- a/qemu-option.h +++ b/qemu-option.h @@ -77,6 +77,8 @@ QEMUOptionParameter *parse_option_parameters(const char *param, void free_option_parameters(QEMUOptionParameter *list); void print_option_parameters(QEMUOptionParameter *list); void print_option_help(QEMUOptionParameter *list); +bool has_help_option(const char *param); +bool is_valid_option_list(const char *param); /* ------------------------------------------------------------------ */ -- 2.1.0