summaryrefslogtreecommitdiffstats
path: root/commands
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2013-02-04 15:48:45 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2013-02-04 15:48:45 +0100
commitdb63bd8e10abf033e0a49932e2319a76b0fa9c1b (patch)
treea969f6af13387e6bf3baaa1aba0171740da3e0f5 /commands
parent1b575024f67f43a3b325c1d3f021ac482b6fad81 (diff)
parent964893b34b829760d81988af79a0cb0766ddc119 (diff)
downloadbarebox-db63bd8e10abf033e0a49932e2319a76b0fa9c1b.tar.gz
barebox-db63bd8e10abf033e0a49932e2319a76b0fa9c1b.tar.xz
Merge branch 'for-next/commands'
Diffstat (limited to 'commands')
-rw-r--r--commands/Kconfig7
-rw-r--r--commands/Makefile1
-rw-r--r--commands/let.c93
3 files changed, 101 insertions, 0 deletions
diff --git a/commands/Kconfig b/commands/Kconfig
index 53cee5cbf9..c9a53b773e 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -57,6 +57,13 @@ config CMD_READLINE
tristate
prompt "readline"
+config CMD_LET
+ tristate
+ prompt "let"
+ help
+ the 'let' command is used for arithmetics. It works like the corresponding
+ Unix shell command.
+
config CMD_TRUE
tristate
default y
diff --git a/commands/Makefile b/commands/Makefile
index 359f566211..c4baf6a470 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -75,6 +75,7 @@ obj-$(CONFIG_CMD_GLOBAL) += global.o
obj-$(CONFIG_CMD_BASENAME) += basename.o
obj-$(CONFIG_CMD_DIRNAME) += dirname.o
obj-$(CONFIG_CMD_READLINK) += readlink.o
+obj-$(CONFIG_CMD_LET) += let.o
obj-$(CONFIG_CMD_LN) += ln.o
obj-$(CONFIG_CMD_CLK) += clk.o
obj-$(CONFIG_CMD_TFTP) += tftp.o
diff --git a/commands/let.c b/commands/let.c
new file mode 100644
index 0000000000..644ede5e2e
--- /dev/null
+++ b/commands/let.c
@@ -0,0 +1,93 @@
+/*
+ * Copyright (C) 2012 Jan Luebbe <j.luebbe@pengutronix.de>
+ *
+ * Partially based on code from BusyBox.
+ *
+ * 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.
+ *
+ */
+
+#include <common.h>
+#include <command.h>
+#include <environment.h>
+#include <errno.h>
+#include <init.h>
+#include <malloc.h>
+#include <xfuncs.h>
+#include <math.h>
+
+static void setvar(const char *name, const char *val)
+{
+ setenv(name, val); /* return value is always 0 */
+}
+
+static arith_t run_arith(const char *s)
+{
+ arith_state_t math_state;
+ arith_t result;
+
+ math_state.lookupvar = getenv;
+ math_state.setvar = setvar;
+ math_state.endofname = arith_endofname;
+
+ result = arith(&math_state, s);
+ if (math_state.errmsg)
+ printf("let: %s\n", math_state.errmsg);
+
+ return result;
+}
+
+static int do_let(int argc, char *argv[])
+{
+ arith_t i;
+
+ argv++;
+ if (!*argv) {
+ printf("let: expression expected\n");
+ return COMMAND_ERROR_USAGE;
+ }
+
+ do {
+ i = run_arith(*argv);
+ } while (*++argv);
+
+ return !i;
+}
+
+BAREBOX_CMD_HELP_START(let)
+BAREBOX_CMD_HELP_USAGE("let expr [expr ...]\n")
+BAREBOX_CMD_HELP_SHORT("evaluate arithmetic expressions\n")
+BAREBOX_CMD_HELP_TEXT ("supported operations are in order of decreasing precedence:\n")
+BAREBOX_CMD_HELP_TEXT (" X++, X--\n")
+BAREBOX_CMD_HELP_TEXT (" ++X, --X\n")
+BAREBOX_CMD_HELP_TEXT (" +X, -X\n")
+BAREBOX_CMD_HELP_TEXT (" !X, ~X\n")
+BAREBOX_CMD_HELP_TEXT (" X**Y\n")
+BAREBOX_CMD_HELP_TEXT (" X*Y, X/Y, X%Y\n")
+BAREBOX_CMD_HELP_TEXT (" X+Y, X-Y\n")
+BAREBOX_CMD_HELP_TEXT (" X<<Y, X>>Y\n")
+BAREBOX_CMD_HELP_TEXT (" X<Y, X<=Y, X>=Y, X>Y\n")
+BAREBOX_CMD_HELP_TEXT (" X==Y, X!=Y\n")
+BAREBOX_CMD_HELP_TEXT (" X&Y\n")
+BAREBOX_CMD_HELP_TEXT (" X^Y\n")
+BAREBOX_CMD_HELP_TEXT (" X|Y\n")
+BAREBOX_CMD_HELP_TEXT (" X&&Y\n")
+BAREBOX_CMD_HELP_TEXT (" X||Y\n")
+BAREBOX_CMD_HELP_TEXT (" X?Y:Z\n")
+BAREBOX_CMD_HELP_TEXT (" X*=Y, X/=Y, X%=Y\n")
+BAREBOX_CMD_HELP_TEXT (" X=Y, X&=Y, X|=Y, X^=Y, X+=Y, X-=Y, X<<=Y, X>>=Y\n")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(let)
+ .cmd = do_let,
+ .usage = "evaluate arithmetic expressions",
+ BAREBOX_CMD_HELP(cmd_let_help)
+BAREBOX_CMD_END