Coverage Report

Created: 2026-10-13 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/commands/let.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
// SPDX-FileCopyrightText: © 2012 Jan Luebbe <j.luebbe@pengutronix.de>
3
4
/* Partially based on code from BusyBox. */
5
6
#include <common.h>
7
#include <command.h>
8
#include <environment.h>
9
#include <errno.h>
10
#include <init.h>
11
#include <malloc.h>
12
#include <xfuncs.h>
13
#include <math.h>
14
15
static void setvar(const char *name, const char *val)
16
0
{
17
0
  setenv(name, val); /* return value is always 0 */
18
0
}
19
20
static arith_t run_arith(const char *s)
21
0
{
22
0
  arith_state_t math_state;
23
0
  arith_t result;
24
25
0
  math_state.lookupvar = getenv;
26
0
  math_state.setvar    = setvar;
27
0
  math_state.endofname = arith_endofname;
28
29
0
  result = arith(&math_state, s);
30
0
  if (math_state.errmsg)
31
0
    printf("let: %s\n", math_state.errmsg);
32
33
0
  return result;
34
0
}
35
36
static int do_let(int argc, char *argv[])
37
0
{
38
0
  arith_t i;
39
40
0
  argv++;
41
0
  if (!*argv) {
42
0
    printf("let: expression expected\n");
43
0
    return COMMAND_ERROR_USAGE;
44
0
  }
45
46
0
  do {
47
0
    i = run_arith(*argv);
48
0
  } while (*++argv);
49
50
0
  return !i;
51
0
}
52
53
BAREBOX_CMD_HELP_START(let)
54
BAREBOX_CMD_HELP_TEXT ("Supported operations are in order of decreasing precedence:")
55
BAREBOX_CMD_HELP_TEXT ("  X++, X--")
56
BAREBOX_CMD_HELP_TEXT ("  ++X, --X")
57
BAREBOX_CMD_HELP_TEXT ("  +X, -X")
58
BAREBOX_CMD_HELP_TEXT ("  !X, ~X")
59
BAREBOX_CMD_HELP_TEXT ("  X**Y")
60
BAREBOX_CMD_HELP_TEXT ("  X*Y, X/Y, X%Y")
61
BAREBOX_CMD_HELP_TEXT ("  X+Y, X-Y")
62
BAREBOX_CMD_HELP_TEXT ("  X<<Y, X>>Y")
63
BAREBOX_CMD_HELP_TEXT ("  X<Y, X<=Y, X>=Y, X>Y")
64
BAREBOX_CMD_HELP_TEXT ("  X==Y, X!=Y")
65
BAREBOX_CMD_HELP_TEXT ("  X&Y")
66
BAREBOX_CMD_HELP_TEXT ("  X^Y")
67
BAREBOX_CMD_HELP_TEXT ("  X|Y")
68
BAREBOX_CMD_HELP_TEXT ("  X&&Y")
69
BAREBOX_CMD_HELP_TEXT ("  X||Y")
70
BAREBOX_CMD_HELP_TEXT ("  X?Y:Z")
71
BAREBOX_CMD_HELP_TEXT ("  X*=Y, X/=Y, X%=Y")
72
BAREBOX_CMD_HELP_TEXT ("  X=Y, X&=Y, X|=Y, X^=Y, X+=Y, X-=Y, X<<=Y, X>>=Y")
73
BAREBOX_CMD_HELP_END
74
75
BAREBOX_CMD_START(let)
76
  .cmd            = do_let,
77
  BAREBOX_CMD_DESC("evaluate arithmetic expressions")
78
  BAREBOX_CMD_OPTS("EXPR [EXPR ...]")
79
  BAREBOX_CMD_GROUP(CMD_GRP_SCRIPT)
80
  BAREBOX_CMD_HELP(cmd_let_help)
81
BAREBOX_CMD_END