blob: 811534e485ef212aa44e2a804fb4ecf4362927f5 [file] [log] [blame]
Jari Aalto726f6381996-08-26 18:22:31 +00001This file is let.def, from which is created let.c.
2It implements the builtin "let" in Bash.
3
Jari Aalto31859422009-01-12 13:36:28 +00004Copyright (C) 1987-2009 Free Software Foundation, Inc.
Jari Aalto726f6381996-08-26 18:22:31 +00005
6This file is part of GNU Bash, the Bourne Again SHell.
7
Jari Aalto31859422009-01-12 13:36:28 +00008Bash is free software: you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
Jari Aalto726f6381996-08-26 18:22:31 +000012
Jari Aalto31859422009-01-12 13:36:28 +000013Bash is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
Jari Aalto726f6381996-08-26 18:22:31 +000017
Jari Aalto31859422009-01-12 13:36:28 +000018You should have received a copy of the GNU General Public License
19along with Bash. If not, see <http://www.gnu.org/licenses/>.
Jari Aalto726f6381996-08-26 18:22:31 +000020
21$BUILTIN let
22$FUNCTION let_builtin
23$PRODUCES let.c
24$SHORT_DOC let arg [arg ...]
Jari Aalto31859422009-01-12 13:36:28 +000025Evaluate arithmetic expressions.
26
27Evaluate each ARG as an arithmetic expression. Evaluation is done in
28fixed-width integers with no check for overflow, though division by 0
29is trapped and flagged as an error. The following list of operators is
30grouped into levels of equal-precedence operators. The levels are listed
31in order of decreasing precedence.
Jari Aalto726f6381996-08-26 18:22:31 +000032
Jari Aalto7117c2d2002-07-17 14:10:11 +000033 id++, id-- variable post-increment, post-decrement
34 ++id, --id variable pre-increment, pre-decrement
Jari Aaltoccc6cda1996-12-23 17:02:34 +000035 -, + unary minus, plus
36 !, ~ logical and bitwise negation
Jari Aalto7117c2d2002-07-17 14:10:11 +000037 ** exponentiation
Jari Aaltoccc6cda1996-12-23 17:02:34 +000038 *, /, % multiplication, division, remainder
39 +, - addition, subtraction
40 <<, >> left and right bitwise shifts
41 <=, >=, <, > comparison
42 ==, != equality, inequality
43 & bitwise AND
44 ^ bitwise XOR
45 | bitwise OR
46 && logical AND
47 || logical OR
48 expr ? expr : expr
Jari Aaltob80f6442004-07-27 13:29:18 +000049 conditional operator
Jari Aaltoccc6cda1996-12-23 17:02:34 +000050 =, *=, /=, %=,
51 +=, -=, <<=, >>=,
52 &=, ^=, |= assignment
Jari Aalto726f6381996-08-26 18:22:31 +000053
54Shell variables are allowed as operands. The name of the variable
Jari Aalto7117c2d2002-07-17 14:10:11 +000055is replaced by its value (coerced to a fixed-width integer) within
Jari Aalto726f6381996-08-26 18:22:31 +000056an expression. The variable need not have its integer attribute
57turned on to be used in an expression.
58
59Operators are evaluated in order of precedence. Sub-expressions in
60parentheses are evaluated first and may override the precedence
61rules above.
62
Jari Aalto31859422009-01-12 13:36:28 +000063Exit Status:
Chet Ramey495aee42011-11-22 19:11:26 -050064If the last ARG evaluates to 0, let returns 1; let returns 0 otherwise.
Jari Aalto726f6381996-08-26 18:22:31 +000065$END
66
Jari Aaltoccc6cda1996-12-23 17:02:34 +000067#include <config.h>
68
69#if defined (HAVE_UNISTD_H)
Jari Aaltocce855b1998-04-17 19:52:44 +000070# ifdef _MINIX
71# include <sys/types.h>
72# endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +000073# include <unistd.h>
74#endif
75
Jari Aaltob80f6442004-07-27 13:29:18 +000076#include "../bashintl.h"
77
Jari Aalto726f6381996-08-26 18:22:31 +000078#include "../shell.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000079#include "common.h"
Jari Aalto726f6381996-08-26 18:22:31 +000080
81/* Arithmetic LET function. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +000082int
Jari Aalto726f6381996-08-26 18:22:31 +000083let_builtin (list)
84 WORD_LIST *list;
85{
Jari Aalto7117c2d2002-07-17 14:10:11 +000086 intmax_t ret;
Jari Aaltod166f041997-06-05 14:59:13 +000087 int expok;
Jari Aalto726f6381996-08-26 18:22:31 +000088
Jari Aalto7117c2d2002-07-17 14:10:11 +000089 /* Skip over leading `--' argument. */
90 if (list && list->word && ISOPTION (list->word->word, '-'))
91 list = list->next;
92
Jari Aaltoccc6cda1996-12-23 17:02:34 +000093 if (list == 0)
Jari Aalto726f6381996-08-26 18:22:31 +000094 {
Jari Aaltob80f6442004-07-27 13:29:18 +000095 builtin_error (_("expression expected"));
Jari Aalto726f6381996-08-26 18:22:31 +000096 return (EXECUTION_FAILURE);
97 }
98
Jari Aaltoccc6cda1996-12-23 17:02:34 +000099 for (; list; list = list->next)
Jari Aaltod166f041997-06-05 14:59:13 +0000100 {
101 ret = evalexp (list->word->word, &expok);
102 if (expok == 0)
103 return (EXECUTION_FAILURE);
104 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000105
Jari Aaltof73dda02001-11-13 17:56:06 +0000106 return ((ret == 0) ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000107}
108
Jari Aaltod166f041997-06-05 14:59:13 +0000109#ifdef INCLUDE_UNUSED
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000110int
111exp_builtin (list)
112 WORD_LIST *list;
113{
114 char *exp;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000115 intmax_t ret;
116 int expok;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000117
118 if (list == 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000119 {
Jari Aaltob80f6442004-07-27 13:29:18 +0000120 builtin_error (_("expression expected"));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000121 return (EXECUTION_FAILURE);
Jari Aalto726f6381996-08-26 18:22:31 +0000122 }
123
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000124 exp = string_list (list);
Jari Aaltod166f041997-06-05 14:59:13 +0000125 ret = evalexp (exp, &expok);
126 (void)free (exp);
Jari Aaltof73dda02001-11-13 17:56:06 +0000127 return (((ret == 0) || (expok == 0)) ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
Jari Aalto726f6381996-08-26 18:22:31 +0000128}
Jari Aaltod166f041997-06-05 14:59:13 +0000129#endif