From: Cyrill Gorcunov <gorcunov@gmail.com>
To: penberg@kernel.org
Cc: asias.hejun@gmail.com, mingo@elte.hu, levinsasha928@gmail.com,
prasadjoshi124@gmail.com, kvm@vger.kernel.org,
Cyrill Gorcunov <gorcunov@gmail.com>
Subject: [patch 1/5] kvm tools: Options parser to handle hex numbers
Date: Tue, 07 Jun 2011 23:41:12 +0400 [thread overview]
Message-ID: <20110607194153.632684087@gmail.com> (raw)
In-Reply-To: 20110607194111.025052224@gmail.com
[-- Attachment #1: kvm-tools-hex-opts --]
[-- Type: text/plain, Size: 3658 bytes --]
Some kernel parameters are convenient if passed in
hex form so our options parser should handle even
such form of input.
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
---
tools/kvm/util/parse-options.c | 102 ++++++++++++++++++++++++++++++++---------
1 file changed, 82 insertions(+), 20 deletions(-)
Index: linux-2.6.git/tools/kvm/util/parse-options.c
===================================================================
--- linux-2.6.git.orig/tools/kvm/util/parse-options.c
+++ linux-2.6.git/tools/kvm/util/parse-options.c
@@ -39,6 +39,84 @@ static int get_arg(struct parse_opt_ctx_
return 0;
}
+#define numvalue(c) \
+ ((c) >= 'a' ? (c) - 'a' + 10 : \
+ (c) >= 'A' ? (c) - 'A' + 10 : (c) - '0')
+
+static u64 readhex(const char *str, bool *error)
+{
+ char *pos = strchr(str, 'x') + 1;
+ u64 res = 0;
+
+ while (*pos) {
+ unsigned int v = numvalue(*pos);
+ if (v > 16) {
+ *error = true;
+ return 0;
+ }
+
+ res = (res * 16) + v;
+ pos++;
+ }
+
+ *error = false;
+ return res;
+}
+
+static int readnum(const struct option *opt, int flags,
+ const char *str, char **end)
+{
+ if (strchr(str, 'x')) {
+ bool error;
+ u64 value;
+
+ value = readhex(str, &error);
+ if (error)
+ goto enotnum;
+
+ switch (opt->type) {
+ case OPTION_INTEGER:
+ *(int *)opt->value = value;
+ break;
+ case OPTION_UINTEGER:
+ *(unsigned int *)opt->value = value;
+ break;
+ case OPTION_LONG:
+ *(long *)opt->value = value;
+ break;
+ case OPTION_U64:
+ *(u64 *)opt->value = value;
+ break;
+ default:
+ goto invcall;
+ }
+ } else {
+ switch (opt->type) {
+ case OPTION_INTEGER:
+ *(int *)opt->value = strtol(str, end, 10);
+ break;
+ case OPTION_UINTEGER:
+ *(unsigned int *)opt->value = strtol(str, end, 10);
+ break;
+ case OPTION_LONG:
+ *(long *)opt->value = strtol(str, end, 10);
+ break;
+ case OPTION_U64:
+ *(u64 *)opt->value = strtoull(str, end, 10);
+ break;
+ default:
+ goto invcall;
+ }
+ }
+
+ return 0;
+
+enotnum:
+ return opterror(opt, "expects a numerical value", flags);
+invcall:
+ return opterror(opt, "invalid numeric conversion", flags);
+}
+
static int get_value(struct parse_opt_ctx_t *p,
const struct option *opt, int flags)
{
@@ -131,11 +209,7 @@ static int get_value(struct parse_opt_ct
}
if (get_arg(p, opt, flags, &arg))
return -1;
- *(int *)opt->value = strtol(arg, (char **)&s, 10);
- if (*s)
- return opterror(opt, "expects a numerical value",
- flags);
- return 0;
+ return readnum(opt, flags, arg, (char **)&s);
case OPTION_UINTEGER:
if (unset) {
@@ -148,11 +222,7 @@ static int get_value(struct parse_opt_ct
}
if (get_arg(p, opt, flags, &arg))
return -1;
- *(unsigned int *)opt->value = strtol(arg, (char **)&s, 10);
- if (*s)
- return opterror(opt,
- "expects a numerical value", flags);
- return 0;
+ return readnum(opt, flags, arg, (char **)&s);
case OPTION_LONG:
if (unset) {
@@ -165,11 +235,7 @@ static int get_value(struct parse_opt_ct
}
if (get_arg(p, opt, flags, &arg))
return -1;
- *(long *)opt->value = strtol(arg, (char **)&s, 10);
- if (*s)
- return opterror(opt,
- "expects a numerical value", flags);
- return 0;
+ return readnum(opt, flags, arg, (char **)&s);
case OPTION_U64:
if (unset) {
@@ -182,11 +248,7 @@ static int get_value(struct parse_opt_ct
}
if (get_arg(p, opt, flags, &arg))
return -1;
- *(u64 *)opt->value = strtoull(arg, (char **)&s, 10);
- if (*s)
- return opterror(opt,
- "expects a numerical value", flags);
- return 0;
+ return readnum(opt, flags, arg, (char **)&s);
case OPTION_END:
case OPTION_ARGUMENT:
next prev parent reply other threads:[~2011-06-07 19:41 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-06-07 19:41 [patch 0/5] kvm tools: A few fixes Cyrill Gorcunov
2011-06-07 19:41 ` Cyrill Gorcunov [this message]
2011-06-07 19:41 ` [patch 2/5] kvm tools: Introduce vidmode parmeter Cyrill Gorcunov
2011-06-07 19:53 ` Pekka Enberg
2011-06-07 20:03 ` Cyrill Gorcunov
2011-06-07 20:10 ` Cyrill Gorcunov
2011-06-07 20:22 ` Cyrill Gorcunov
2011-06-07 19:41 ` [patch 3/5] kvm tools: Delete dangling cursor from int10 Cyrill Gorcunov
2011-06-07 19:41 ` [patch 4/5] kvm tools: Get rid of spaces in ld script Cyrill Gorcunov
2011-06-07 19:41 ` [patch 5/5] kvm tools: Reform bios make fules Cyrill Gorcunov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20110607194153.632684087@gmail.com \
--to=gorcunov@gmail.com \
--cc=asias.hejun@gmail.com \
--cc=kvm@vger.kernel.org \
--cc=levinsasha928@gmail.com \
--cc=mingo@elte.hu \
--cc=penberg@kernel.org \
--cc=prasadjoshi124@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.