| 1 | This file is let.def, from which is created let.c.
|
|---|
| 2 | It implements the builtin "let" in Bash.
|
|---|
| 3 |
|
|---|
| 4 | Copyright (C) 1987-2002 Free Software Foundation, Inc.
|
|---|
| 5 |
|
|---|
| 6 | This file is part of GNU Bash, the Bourne Again SHell.
|
|---|
| 7 |
|
|---|
| 8 | Bash is free software; you can redistribute it and/or modify it under
|
|---|
| 9 | the terms of the GNU General Public License as published by the Free
|
|---|
| 10 | Software Foundation; either version 2, or (at your option) any later
|
|---|
| 11 | version.
|
|---|
| 12 |
|
|---|
| 13 | Bash is distributed in the hope that it will be useful, but WITHOUT ANY
|
|---|
| 14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|---|
| 15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|---|
| 16 | for more details.
|
|---|
| 17 |
|
|---|
| 18 | You should have received a copy of the GNU General Public License along
|
|---|
| 19 | with Bash; see the file COPYING. If not, write to the Free Software
|
|---|
| 20 | Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
|
|---|
| 21 |
|
|---|
| 22 | $BUILTIN let
|
|---|
| 23 | $FUNCTION let_builtin
|
|---|
| 24 | $PRODUCES let.c
|
|---|
| 25 | $SHORT_DOC let arg [arg ...]
|
|---|
| 26 | Each ARG is an arithmetic expression to be evaluated. Evaluation
|
|---|
| 27 | is done in fixed-width integers with no check for overflow, though
|
|---|
| 28 | division by 0 is trapped and flagged as an error. The following
|
|---|
| 29 | list of operators is grouped into levels of equal-precedence operators.
|
|---|
| 30 | The levels are listed in order of decreasing precedence.
|
|---|
| 31 |
|
|---|
| 32 | id++, id-- variable post-increment, post-decrement
|
|---|
| 33 | ++id, --id variable pre-increment, pre-decrement
|
|---|
| 34 | -, + unary minus, plus
|
|---|
| 35 | !, ~ logical and bitwise negation
|
|---|
| 36 | ** exponentiation
|
|---|
| 37 | *, /, % multiplication, division, remainder
|
|---|
| 38 | +, - addition, subtraction
|
|---|
| 39 | <<, >> left and right bitwise shifts
|
|---|
| 40 | <=, >=, <, > comparison
|
|---|
| 41 | ==, != equality, inequality
|
|---|
| 42 | & bitwise AND
|
|---|
| 43 | ^ bitwise XOR
|
|---|
| 44 | | bitwise OR
|
|---|
| 45 | && logical AND
|
|---|
| 46 | || logical OR
|
|---|
| 47 | expr ? expr : expr
|
|---|
| 48 | conditional operator
|
|---|
| 49 | =, *=, /=, %=,
|
|---|
| 50 | +=, -=, <<=, >>=,
|
|---|
| 51 | &=, ^=, |= assignment
|
|---|
| 52 |
|
|---|
| 53 | Shell variables are allowed as operands. The name of the variable
|
|---|
| 54 | is replaced by its value (coerced to a fixed-width integer) within
|
|---|
| 55 | an expression. The variable need not have its integer attribute
|
|---|
| 56 | turned on to be used in an expression.
|
|---|
| 57 |
|
|---|
| 58 | Operators are evaluated in order of precedence. Sub-expressions in
|
|---|
| 59 | parentheses are evaluated first and may override the precedence
|
|---|
| 60 | rules above.
|
|---|
| 61 |
|
|---|
| 62 | If the last ARG evaluates to 0, let returns 1; 0 is returned
|
|---|
| 63 | otherwise.
|
|---|
| 64 | $END
|
|---|
| 65 |
|
|---|
| 66 | #include <config.h>
|
|---|
| 67 |
|
|---|
| 68 | #if defined (HAVE_UNISTD_H)
|
|---|
| 69 | # ifdef _MINIX
|
|---|
| 70 | # include <sys/types.h>
|
|---|
| 71 | # endif
|
|---|
| 72 | # include <unistd.h>
|
|---|
| 73 | #endif
|
|---|
| 74 |
|
|---|
| 75 | #include "../bashintl.h"
|
|---|
| 76 |
|
|---|
| 77 | #include "../shell.h"
|
|---|
| 78 | #include "common.h"
|
|---|
| 79 |
|
|---|
| 80 | /* Arithmetic LET function. */
|
|---|
| 81 | int
|
|---|
| 82 | let_builtin (list)
|
|---|
| 83 | WORD_LIST *list;
|
|---|
| 84 | {
|
|---|
| 85 | intmax_t ret;
|
|---|
| 86 | int expok;
|
|---|
| 87 |
|
|---|
| 88 | /* Skip over leading `--' argument. */
|
|---|
| 89 | if (list && list->word && ISOPTION (list->word->word, '-'))
|
|---|
| 90 | list = list->next;
|
|---|
| 91 |
|
|---|
| 92 | if (list == 0)
|
|---|
| 93 | {
|
|---|
| 94 | builtin_error (_("expression expected"));
|
|---|
| 95 | return (EXECUTION_FAILURE);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | for (; list; list = list->next)
|
|---|
| 99 | {
|
|---|
| 100 | ret = evalexp (list->word->word, &expok);
|
|---|
| 101 | if (expok == 0)
|
|---|
| 102 | return (EXECUTION_FAILURE);
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | return ((ret == 0) ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | #ifdef INCLUDE_UNUSED
|
|---|
| 109 | int
|
|---|
| 110 | exp_builtin (list)
|
|---|
| 111 | WORD_LIST *list;
|
|---|
| 112 | {
|
|---|
| 113 | char *exp;
|
|---|
| 114 | intmax_t ret;
|
|---|
| 115 | int expok;
|
|---|
| 116 |
|
|---|
| 117 | if (list == 0)
|
|---|
| 118 | {
|
|---|
| 119 | builtin_error (_("expression expected"));
|
|---|
| 120 | return (EXECUTION_FAILURE);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | exp = string_list (list);
|
|---|
| 124 | ret = evalexp (exp, &expok);
|
|---|
| 125 | (void)free (exp);
|
|---|
| 126 | return (((ret == 0) || (expok == 0)) ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
|
|---|
| 127 | }
|
|---|
| 128 | #endif
|
|---|