summaryrefslogtreecommitdiffstats
path: root/commands/let.c
diff options
context:
space:
mode:
authorJan Luebbe <jlu@pengutronix.de>2012-10-04 15:27:55 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2013-01-09 10:32:54 +0100
commit964893b34b829760d81988af79a0cb0766ddc119 (patch)
tree58b6def4844a4ccbb7491f62bb9bfb9fbb783ddb /commands/let.c
parentb46b57f35a27090cfbac4900d1a3d522d7898e53 (diff)
downloadbarebox-964893b34b829760d81988af79a0cb0766ddc119.tar.gz
commands: add let command which supports proper arithmetic
This command works like the corresponding Unix shell command and is used for adding, multiplying and much more. Signed-off-by: Jan Luebbe <jlu@pengutronix.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'commands/let.c')
-rw-r--r--commands/let.c93
1 files changed, 93 insertions, 0 deletions
diff --git a/commands/let.c b/commands/let.c
new file mode 100644
index 0000000..644ede5
--- /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