Skip to contentSkip to conversation

Add setexpr command

PATCH New Kumar Gala 4 participants 11 messages Feb 13, 2008 – Mar 25, 2008

lore
Share & tools
lore

Kumar Gala

author
#0 lore raw

PATCH 1/1 Add setexpr command

Add a simple expr style command that will set an env variable as the result of the command. This allows us to do simple math in shell. The following operations are supported: &, |, ^, +, -, *, /.

---

this was the expr patch, but renamed based on comments on the list.

Still open issue about should this be in config_cmd_default.h

- k

 common/cmd_setexpr.c |   70 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 70 insertions(+), 0 deletions(-)
 create mode 100644 common/cmd_setexpr.c
common/cmd_setexpr.c new file +70 -0
new file mode 100644
index 0000000..2e49b6d
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2008 Freescale Semiconductor, Inc.
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+/*
+ * This file provides a shell like 'expr' function to return.
+ */
+
+#include <common.h>
+#include <config.h>
+#include <command.h>
+
+int do_setexpr(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
+{
+	ulong a, b;
+	char buf[16];
+
+	/* Validate arguments */
+	if ((argc != 5) || (strlen(argv[3]) != 1)) {
+		printf("Usage:\n%s\n", cmdtp->usage);
+		return 1;
+	}
+
+	a = simple_strtoul(argv[2], NULL, 16);
+	b = simple_strtoul(argv[4], NULL, 16);
+
+	switch (argv[3][0]) {
+	case '|': sprintf(buf, "%lx", (a | b)); break;
+	case '&': sprintf(buf, "%lx", (a & b)); break;
+	case '+': sprintf(buf, "%lx", (a + b)); break;
+	case '^': sprintf(buf, "%lx", (a ^ b)); break;
+	case '-': sprintf(buf, "%lx", (a - b)); break;
+	case '*': sprintf(buf, "%lx", (a * b)); break;
+	case '/': sprintf(buf, "%lx", (a / b)); break;
+	case '%': sprintf(buf, "%lx", (a % b)); break;
+	default:
+		printf("invalid op\n");
+		return 1;
+	}
+
+	setenv(argv[1], buf);
+
+	return 0;
+}
+
+U_BOOT_CMD(
+	setexpr, 5, 0, do_setexpr,
+	"setexpr - set environment variable as the result of eval expression\n",
+	"name value1 <op> value2\n"
+	"    - set environment variable 'name' to the result of the evaluated\n"
+	"      express specified by <op>.  <op> can be &, |, ^, +, -, *, /, %\n"
+);
Signature · 1 line
1.5.3.8
#1 lore raw

PATCH 1/1 Add setexpr command

Quoted 8 lines · In message <[email protected]> you wrote:

Add a simple expr style command that will set an env variable as the result of the command. This allows us to do simple math in shell. The following operations are supported: &, |, ^, +, -, *, /.

---

this was the expr patch, but renamed based on comments on the list.

<grumble>
Well, *my* command was *not* to rename it.
</grumble>

At least, please don't make it a default command, then.

Best regards,

Wolfgang Denk

Signature · 4 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
1000 pains                                              = 1 Megahertz
author
#2 lore raw

PATCH 1/1 Add setexpr command

Quoted 8 lines · Wolfgang Denk

In message <Pine.LNX.

Quoted 6 lines · 4.64.0802131648030.23661 at blarg.am.freescale.net> you wrote:

Add a simple expr style command that will set an env variable as the result
of the command. This allows us to do simple math in shell. The following
operations are supported: &, |, ^, +, -, *, /.

Quoted 7 lines
Quoted 4 lines

---

this was the expr patch, but renamed based on comments on the list.

<grumble>
Well, *my* command was *not* to rename it.

sorry, saw your post after I sent this :)

Quoted 4 lines

</grumble>

At least, please don't make it a default command, then.

If you are good with calling it expr so am I. I'd prefer not confusing 'setenv' with 'setexpr'. If we call it expr add it to the default list?

- k

#3 lore raw

PATCH 1/1 Add setexpr command

Quoted 2 lines · In message <[email protected]> you wrote:
Quoted 2 lines

Well, *my* command was *not* to rename it.

Can't even type any more. I intended to write "comment".

Quoted 5 lines
Quoted 1 line

At least, please don't make it a default command, then.

If you are good with calling it expr so am I. I'd prefer not confusing 'setenv' with 'setexpr'. If we call it expr add it to the default list?

Well, given the fact that only very few boards need it but it adds to the memory footprint I recommend to not add it to the defaults list.

Best regards,

Wolfgang Denk

Signature · 4 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
"Any excuse will serve a tyrant."                             - Aesop
#4 lore raw

PATCH 1/1 Add setexpr command

In reply to Kumar Gala
Quoted 20 lines · Kumar Gala
Quoted 8 lines · Wolfgang Denk

In message <Pine.LNX.

Quoted 6 lines · 4.64.0802131648030.23661 at blarg.am.freescale.net> you wrote:

Add a simple expr style command that will set an env variable as the result
of the command. This allows us to do simple math in shell. The following
operations are supported: &, |, ^, +, -, *, /.

Quoted 6 lines
Quoted 4 lines

---

this was the expr patch, but renamed based on comments on the list.

<grumble>
Well, *my* command was *not* to rename it.

sorry, saw your post after I sent this :)

Quoted 2 lines

</grumble>

Quoted 7 lines
Quoted 1 line

At least, please don't make it a default command, then.

If you are good with calling it expr so am I. I'd prefer not confusing 'setenv' with 'setexpr'. If we call it expr add it to the default list?

- k

OK, time to educate Jerry since I'm too lazy to check myself. Does our hush shell support back ticks? If we have back tick support, or can add that support, we should call it expr and return a value which then gets placed properly. That would be IDEAL.
setenv foo expr 2 + 3

gvb

author
#5 lore raw

PATCH 1/1 Add setexpr command

Quoted 25 lines · Jerry Van Baren
Quoted 15 lines · Kumar Gala
Quoted 8 lines · Wolfgang Denk

In message <Pine.LNX. [email protected]

Quoted 7 lines

you wrote:
Add a simple expr style command that will set an env variable as the result
of the command. This allows us to do simple math in shell. The following
operations are supported: &, |, ^, +, -, *, /.

Quoted 6 lines
Quoted 4 lines

---

this was the expr patch, but renamed based on comments on the list.

<grumble>
Well, *my* command was *not* to rename it.

Quoted 8 lines

sorry, saw your post after I sent this :)

Quoted 3 lines

</grumble>

At least, please don't make it a default command, then.

If you are good with calling it expr so am I. I'd prefer not confusing 'setenv' with 'setexpr'. If we call it expr add it to the default list?
- k

Quoted 5 lines

OK, time to educate Jerry since I'm too lazy to check myself. Does our hush shell support back ticks? If we have back tick support, or can add that support, we should call it expr and return a value which then gets placed properly. That would be IDEAL.
setenv foo expr 2 + 3

no we dont' support back ticks like this.

- k

#6 lore raw

PATCH 1/1 Add setexpr command

In reply to Jerry Van Baren
Quoted 3 lines · In message <[email protected]> you wrote:

OK, time to educate Jerry since I'm too lazy to check myself. Does our hush shell support back ticks? If we have back tick support, or can add

No, we don't, and it's not trivial to add. [Feel free to prove me worng :-) ]

Quoted 3 lines

that support, we should call it expr and return a value which then gets placed properly. That would be IDEAL.
setenv foo expr 2 + 3

Agreed. BUt that's the very reason not to call it expr, because it doesn't work this way.

Best regards,

Wolfgang Denk

Signature · 4 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
You can observe a lot just by watchin'.                  - Yogi Berra
#7 lore raw

PATCH 1/1 Add setexpr command

In reply to Kumar Gala

Hi Kumar,

nice idea. Can you at a little prose to the README (perhaps with a little usage example).

Matthias

Quoted 5 lines · Kumar Gala

Add a simple expr style command that will set an env variable as the result of the command. This allows us to do simple math in shell. The following operations are supported: &, |, ^, +, -, *, /.

author
#8 lore raw

PATCH 1/1 Add setexpr command

Quoted 4 lines · Matthias Fuchs

Hi Kumar,

nice idea. Can you at a little prose to the README (perhaps with a little usage example).

I can do this.

- k

Quoted 11 lines

Matthias

Quoted 7 lines · Kumar Gala

Add a simple expr style command that will set an env variable as the result
of the command. This allows us to do simple math in shell. The following
operations are supported: &, |, ^, +, -, *, /.

#9 lore raw

PATCH 1/1 Add setexpr command

In reply to Matthias Fuchs
Quoted 3 lines · In message <[email protected]> you wrote:

nice idea. Can you at a little prose to the README (perhaps with a little usage example).

Or (and better *and*) to the manual, please?

Best regards,

Wolfgang Denk

Signature · 8 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
When a child is taught ... its programmed with simple instructions --
and at some point, if its mind develops properly, it exceeds the  sum
of what it was taught, thinks independently.
	-- Dr. Richard Daystrom, "The Ultimate Computer",
	   stardate 4731.3.
#10 lore raw

PATCH 1/1 Add setexpr command

In reply to Kumar Gala
Quoted 9 lines · In message <[email protected]> you wrote:

Add a simple expr style command that will set an env variable as the result of the command. This allows us to do simple math in shell. The following operations are supported: &, |, ^, +, -, *, /.

---

this was the expr patch, but renamed based on comments on the list.

Quoted 7 lines

Still open issue about should this be in config_cmd_default.h

- k

 common/cmd_setexpr.c |   70 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 70 insertions(+), 0 deletions(-)
 create mode 100644 common/cmd_setexpr.c

Applied, thanks.

Best regards,

Wolfgang Denk

Signature · 4 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
An armed society is a polite society.