aboutsummaryrefslogtreecommitdiffstats
path: root/builtins/let.def
blob: 60b66751301dcf42a5791311107cf5b8e021b6c0 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
This file is let.def, from which is created let.c.
It implements the builtin "let" in Bash.

Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.

This file is part of GNU Bash, the Bourne Again SHell.

Bash 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 1, or (at your option) any later
version.

Bash 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 Bash; see the file COPYING.  If not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

$BUILTIN let
$FUNCTION let_builtin
$PRODUCES let.c
$SHORT_DOC let arg [arg ...]
Each ARG is an arithmetic expression to be evaluated.  Evaluation
is done in long integers with no check for overflow, though division
by 0 is trapped and flagged as an error.  The following list of
operators is grouped into levels of equal-precedence operators.
The levels are listed in order of decreasing precedence.

	-, +		unary minus, plus
	!, ~		logical and bitwise negation
	*, /, %		multiplication, division, remainder
	+, -		addition, subtraction
	<<, >>		left and right bitwise shifts
	<=, >=, <, >	comparison
	==, !=		equality, inequality
	&		bitwise AND
	^		bitwise XOR
	|		bitwise OR
	&&		logical AND
	||		logical OR
	expr ? expr : expr
			conditional expression
	=, *=, /=, %=,
	+=, -=, <<=, >>=,
	&=, ^=, |=	assignment

Shell variables are allowed as operands.  The name of the variable
is replaced by its value (coerced to a long integer) within
an expression.  The variable need not have its integer attribute
turned on to be used in an expression.

Operators are evaluated in order of precedence.  Sub-expressions in
parentheses are evaluated first and may override the precedence
rules above.

If the last ARG evaluates to 0, let returns 1; 0 is returned
otherwise.
$END

#include <config.h>

#if defined (HAVE_UNISTD_H)
#  include <unistd.h>
#endif

#include "../shell.h"
#include "common.h"

/* Arithmetic LET function. */
int
let_builtin (list)
     WORD_LIST *list;
{
  long ret;

  if (list == 0)
    {
      builtin_error ("expression expected");
      return (EXECUTION_FAILURE);
    }

  for (; list; list = list->next)
    ret = evalexp (list->word->word);

  return ((ret == 0L) ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
}

int
exp_builtin (list)
     WORD_LIST *list;
{
  char *exp;
  int ret;

  if (list == 0)
    {
      builtin_error ("expression expected");
      return (EXECUTION_FAILURE);
    }

  exp = string_list (list);
  ret = evalexp (exp);
  free (exp);
  return ((ret == 0L) ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
}