All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: u-boot@lists.denx.de
Subject: [PATCH 09/10] setexpr: Convert to use a struct for values
Date: Sun,  1 Nov 2020 14:15:43 -0700	[thread overview]
Message-ID: <20201101211544.3579850-10-sjg@chromium.org> (raw)
In-Reply-To: <20201101211544.3579850-1-sjg@chromium.org>

At present a ulong is used to hold operand values. This means that
strings cannot be used. While most operations are not useful for strings,
concatenation is. As a starting point to supporting strings, convert the
code to use a struct instead of a ulong for operands.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 cmd/setexpr.c | 111 ++++++++++++++++++++++++++++++--------------------
 1 file changed, 67 insertions(+), 44 deletions(-)

diff --git a/cmd/setexpr.c b/cmd/setexpr.c
index d364dbc2bc5..8a3654505da 100644
--- a/cmd/setexpr.c
+++ b/cmd/setexpr.c
@@ -15,8 +15,19 @@
 #include <log.h>
 #include <mapmem.h>
 
-static ulong get_arg(char *s, int w)
+/**
+ * struct expr_arg: Holds an argument to an expression
+ *
+ * @ival: Integer value (if width is not CMD_DATA_SIZE_STR)
+ */
+struct expr_arg {
+	ulong ival;
+};
+
+static int get_arg(char *s, int w, struct expr_arg *argp)
 {
+	struct expr_arg arg;
+
 	/*
 	 * If the parameter starts with a '*' then assume it is a pointer to
 	 * the value we want.
@@ -32,26 +43,33 @@ static ulong get_arg(char *s, int w)
 			p = map_sysmem(addr, sizeof(uchar));
 			val = (ulong)*(uchar *)p;
 			unmap_sysmem(p);
-			return val;
+			arg.ival = val;
+			break;
 		case 2:
 			p = map_sysmem(addr, sizeof(ushort));
 			val = (ulong)*(ushort *)p;
 			unmap_sysmem(p);
-			return val;
+			arg.ival = val;
+			break;
 		case 4:
 			p = map_sysmem(addr, sizeof(u32));
 			val = *(u32 *)p;
 			unmap_sysmem(p);
-			return val;
+			arg.ival = val;
+			break;
 		default:
 			p = map_sysmem(addr, sizeof(ulong));
 			val = *p;
 			unmap_sysmem(p);
-			return val;
+			arg.ival = val;
+			break;
 		}
 	} else {
-		return simple_strtoul(s, NULL, 16);
+		arg.ival = simple_strtoul(s, NULL, 16);
 	}
+	*argp = arg;
+
+	return 0;
 }
 
 #ifdef CONFIG_REGEX
@@ -321,7 +339,7 @@ static int regex_sub_var(const char *name, const char *r, const char *s,
 static int do_setexpr(struct cmd_tbl *cmdtp, int flag, int argc,
 		      char *const argv[])
 {
-	ulong a, b;
+	struct expr_arg aval, bval;
 	ulong value;
 	int w;
 
@@ -339,13 +357,12 @@ static int do_setexpr(struct cmd_tbl *cmdtp, int flag, int argc,
 
 	w = cmd_get_data_size(argv[0], 4);
 
-	a = get_arg(argv[2], w);
+	if (get_arg(argv[2], w, &aval))
+		return CMD_RET_FAILURE;
 
 	/* plain assignment: "setexpr name value" */
-	if (argc == 3) {
-		env_set_hex(argv[1], a);
-		return 0;
-	}
+	if (argc == 3)
+		return env_set_hex(argv[1], aval.ival);
 
 	/* 5 or 6 args (6 args only with [g]sub) */
 #ifdef CONFIG_REGEX
@@ -367,39 +384,45 @@ static int do_setexpr(struct cmd_tbl *cmdtp, int flag, int argc,
 	if (strlen(argv[3]) != 1)
 		return CMD_RET_USAGE;
 
-	b = get_arg(argv[4], w);
-
-	switch (argv[3][0]) {
-	case '|':
-		value = a | b;
-		break;
-	case '&':
-		value = a & b;
-		break;
-	case '+':
-		value = a + b;
-		break;
-	case '^':
-		value = a ^ b;
-		break;
-	case '-':
-		value = a - b;
-		break;
-	case '*':
-		value = a * b;
-		break;
-	case '/':
-		value = a / b;
-		break;
-	case '%':
-		value = a % b;
-		break;
-	default:
-		printf("invalid op\n");
-		return 1;
-	}
+	if (get_arg(argv[4], w, &bval))
+		return CMD_RET_FAILURE;
 
-	env_set_hex(argv[1], value);
+	if (w != CMD_DATA_SIZE_STR) {
+		ulong a = aval.ival;
+		ulong b = bval.ival;
+
+		switch (argv[3][0]) {
+		case '|':
+			value = a | b;
+			break;
+		case '&':
+			value = a & b;
+			break;
+		case '+':
+			value = a + b;
+			break;
+		case '^':
+			value = a ^ b;
+			break;
+		case '-':
+			value = a - b;
+			break;
+		case '*':
+			value = a * b;
+			break;
+		case '/':
+			value = a / b;
+			break;
+		case '%':
+			value = a % b;
+			break;
+		default:
+			printf("invalid op\n");
+			return 1;
+		}
+
+		env_set_hex(argv[1], value);
+	}
 
 	return 0;
 }
-- 
2.29.1.341.ge80a0c044ae-goog

  parent reply	other threads:[~2020-11-01 21:15 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-01 21:15 [PATCH 00/10] setexpr: Correct various bugs and add tests plus string support Simon Glass
2020-11-01 21:15 ` [PATCH 01/10] test: Add some tests for setexpr Simon Glass
2020-12-02 21:22   ` Tom Rini
2020-11-01 21:15 ` [PATCH 02/10] command: Add constants for cmd_get_data_size string / error Simon Glass
2020-12-02 21:22   ` Tom Rini
2020-11-01 21:15 ` [PATCH 03/10] setexpr: Add explicit support for 32- and 64-bit ints Simon Glass
2020-12-02 21:22   ` Tom Rini
2020-11-01 21:15 ` [PATCH 04/10] test: Add some setexpr regex tests Simon Glass
2020-12-02 21:22   ` Tom Rini
2020-11-01 21:15 ` [PATCH 05/10] setexpr: Split the core logic into its own function Simon Glass
2020-12-02 21:22   ` Tom Rini
2020-11-01 21:15 ` [PATCH 06/10] setexpr: Add some tests for buffer overflow and backref Simon Glass
2020-12-02 21:23   ` Tom Rini
2021-01-17 22:52   ` CRASH caused by: " Heinrich Schuchardt
2021-01-19 18:06     ` Simon Glass
2021-01-19 19:07       ` Heinrich Schuchardt
2021-01-19 19:45       ` Tom Rini
2021-01-20  0:17         ` Simon Glass
2020-11-01 21:15 ` [PATCH 07/10] setexpr: Correct dropping of final unmatched string Simon Glass
2020-12-02 21:23   ` Tom Rini
2020-11-01 21:15 ` [PATCH 08/10] setexpr: Correct buffer overflow bug and enable tests Simon Glass
2020-12-02 21:23   ` Tom Rini
2020-11-01 21:15 ` Simon Glass [this message]
2020-12-02 21:23   ` [PATCH 09/10] setexpr: Convert to use a struct for values Tom Rini
2020-11-01 21:15 ` [PATCH 10/10] setexpr: Add support for strings Simon Glass
2020-11-01 23:08   ` Marek Behun
2020-11-03 15:12     ` Simon Glass
2020-11-03 16:30       ` Marek Behun
2020-11-05 16:49         ` Wolfgang Denk
2020-11-05 17:27         ` Simon Glass
2020-11-06 20:58           ` Tom Rini
2020-11-06 21:48             ` Simon Glass
2020-11-05 16:47       ` Wolfgang Denk
2020-11-05 17:27         ` Simon Glass
2020-11-05 17:50           ` Marek Behun
2020-11-05 19:10           ` Wolfgang Denk
2020-11-05 20:15             ` Simon Glass
2020-12-02 21:23   ` Tom Rini

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=20201101211544.3579850-10-sjg@chromium.org \
    --to=sjg@chromium.org \
    --cc=u-boot@lists.denx.de \
    /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.