summaryrefslogtreecommitdiffstats
path: root/commands/let.c
blob: 644ede5e2edda57df42a28f9ecfb7aa3d1b30ae5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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