[U-Boot-Users] [PATCH] 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: &, |, ^, +, -, *, /.
Signed-off-by: Kumar Gala galak@kernel.crashing.org ---
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
diff --git a/common/cmd_setexpr.c b/common/cmd_setexpr.c new file mode 100644 index 0000000..2e49b6d --- /dev/null +++ b/common/cmd_setexpr.c @@ -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" +);

In message Pine.LNX.4.64.0802131648030.23661@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: &, |, ^, +, -, *, /.
Signed-off-by: Kumar Gala galak@kernel.crashing.org
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

On Feb 13, 2008, at 5:07 PM, Wolfgang Denk wrote:
In message <Pine.LNX. 4.64.0802131648030.23661@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: &, |, ^, +, -, *, /.
Signed-off-by: Kumar Gala galak@kernel.crashing.org
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 :)
</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

In message ECA572BB-0720-4BD4-9A91-AEBC1056DCA4@kernel.crashing.org you wrote:
Well, *my* command was *not* to rename it.
Can't even type any more. I intended to write "comment".
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

Kumar Gala wrote:
On Feb 13, 2008, at 5:07 PM, Wolfgang Denk wrote:
In message <Pine.LNX. 4.64.0802131648030.23661@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: &, |, ^, +, -, *, /.
Signed-off-by: Kumar Gala galak@kernel.crashing.org
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 :)
</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
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

On Feb 14, 2008, at 6:41 AM, Jerry Van Baren wrote:
Kumar Gala wrote:
On Feb 13, 2008, at 5:07 PM, Wolfgang Denk wrote:
In message <Pine.LNX. 4.64.0802131648030.23661@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: &, |, ^, +, -, *, /.
Signed-off-by: Kumar Gala galak@kernel.crashing.org
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 :)
</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
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

In message 47B43701.1070102@ge.com 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 :-) ]
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

Hi Kumar,
nice idea. Can you at a little prose to the README (perhaps with a little usage example).
Matthias
On Wednesday 13 February 2008 23:53, Kumar Gala 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: &, |, ^, +, -, *, /.
Signed-off-by: Kumar Gala galak@kernel.crashing.org

On Feb 14, 2008, at 3:14 AM, Matthias Fuchs wrote:
Hi Kumar,
nice idea. Can you at a little prose to the README (perhaps with a little usage example).
I can do this.
- k
Matthias
On Wednesday 13 February 2008 23:53, Kumar Gala 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: &, |, ^, +, -, *, /.
Signed-off-by: Kumar Gala galak@kernel.crashing.org

In message 200802141014.21407.matthias.fuchs@esd-electronics.com 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

In message Pine.LNX.4.64.0802131648030.23661@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: &, |, ^, +, -, *, /.
Signed-off-by: Kumar Gala galak@kernel.crashing.org
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
Applied, thanks.
Best regards,
Wolfgang Denk
participants (4)
-
Jerry Van Baren
-
Kumar Gala
-
Matthias Fuchs
-
Wolfgang Denk