i-bash/builtins/let.def

132 lines
3.3 KiB
Modula-2
Raw Permalink Normal View History

1996-08-26 18:22:31 +00:00
This file is let.def, from which is created let.c.
It implements the builtin "let" in Bash.
2009-01-12 13:36:28 +00:00
Copyright (C) 1987-2009 Free Software Foundation, Inc.
1996-08-26 18:22:31 +00:00
This file is part of GNU Bash, the Bourne Again SHell.
2009-01-12 13:36:28 +00:00
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 3 of the License, or
(at your option) any later version.
1996-08-26 18:22:31 +00:00
2009-01-12 13:36:28 +00:00
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.
1996-08-26 18:22:31 +00:00
2009-01-12 13:36:28 +00:00
You should have received a copy of the GNU General Public License
along with Bash. If not, see <http://www.gnu.org/licenses/>.
1996-08-26 18:22:31 +00:00
$BUILTIN let
$FUNCTION let_builtin
$PRODUCES let.c
$SHORT_DOC let arg [arg ...]
2009-01-12 13:36:28 +00:00
Evaluate arithmetic expressions.
Evaluate each ARG as an arithmetic expression. Evaluation is done in
fixed-width 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.
1996-08-26 18:22:31 +00:00
2002-07-17 14:10:11 +00:00
id++, id-- variable post-increment, post-decrement
++id, --id variable pre-increment, pre-decrement
1996-12-23 17:02:34 +00:00
-, + unary minus, plus
!, ~ logical and bitwise negation
2002-07-17 14:10:11 +00:00
** exponentiation
1996-12-23 17:02:34 +00:00
*, /, % 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
2004-07-27 13:29:18 +00:00
conditional operator
1996-12-23 17:02:34 +00:00
=, *=, /=, %=,
+=, -=, <<=, >>=,
&=, ^=, |= assignment
1996-08-26 18:22:31 +00:00
Shell variables are allowed as operands. The name of the variable
2002-07-17 14:10:11 +00:00
is replaced by its value (coerced to a fixed-width integer) within
1996-08-26 18:22:31 +00:00
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.
2009-01-12 13:36:28 +00:00
Exit Status:
If the last ARG evaluates to 0, let returns 1; let returns 0 otherwise.
1996-08-26 18:22:31 +00:00
$END
1996-12-23 17:02:34 +00:00
#include <config.h>
#if defined (HAVE_UNISTD_H)
1998-04-17 19:52:44 +00:00
# ifdef _MINIX
# include <sys/types.h>
# endif
1996-12-23 17:02:34 +00:00
# include <unistd.h>
#endif
2004-07-27 13:29:18 +00:00
#include "../bashintl.h"
1996-08-26 18:22:31 +00:00
#include "../shell.h"
1996-12-23 17:02:34 +00:00
#include "common.h"
1996-08-26 18:22:31 +00:00
/* Arithmetic LET function. */
1996-12-23 17:02:34 +00:00
int
1996-08-26 18:22:31 +00:00
let_builtin (list)
WORD_LIST *list;
{
2002-07-17 14:10:11 +00:00
intmax_t ret;
1997-06-05 14:59:13 +00:00
int expok;
1996-08-26 18:22:31 +00:00
CHECK_HELPOPT (list);
2002-07-17 14:10:11 +00:00
/* Skip over leading `--' argument. */
if (list && list->word && ISOPTION (list->word->word, '-'))
list = list->next;
1996-12-23 17:02:34 +00:00
if (list == 0)
1996-08-26 18:22:31 +00:00
{
2004-07-27 13:29:18 +00:00
builtin_error (_("expression expected"));
1996-08-26 18:22:31 +00:00
return (EXECUTION_FAILURE);
}
1996-12-23 17:02:34 +00:00
for (; list; list = list->next)
1997-06-05 14:59:13 +00:00
{
ret = evalexp (list->word->word, &expok);
if (expok == 0)
return (EXECUTION_FAILURE);
}
1996-12-23 17:02:34 +00:00
2001-11-13 17:56:06 +00:00
return ((ret == 0) ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
1996-12-23 17:02:34 +00:00
}
1997-06-05 14:59:13 +00:00
#ifdef INCLUDE_UNUSED
1996-12-23 17:02:34 +00:00
int
exp_builtin (list)
WORD_LIST *list;
{
char *exp;
2002-07-17 14:10:11 +00:00
intmax_t ret;
int expok;
1996-12-23 17:02:34 +00:00
if (list == 0)
1996-08-26 18:22:31 +00:00
{
2004-07-27 13:29:18 +00:00
builtin_error (_("expression expected"));
1996-12-23 17:02:34 +00:00
return (EXECUTION_FAILURE);
1996-08-26 18:22:31 +00:00
}
1996-12-23 17:02:34 +00:00
exp = string_list (list);
1997-06-05 14:59:13 +00:00
ret = evalexp (exp, &expok);
(void)free (exp);
2001-11-13 17:56:06 +00:00
return (((ret == 0) || (expok == 0)) ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
1996-08-26 18:22:31 +00:00
}
1997-06-05 14:59:13 +00:00
#endif