i-bash/builtins/let.def

129 lines
3.3 KiB
Modula-2
Raw 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.
2002-07-17 14:10:11 +00:00
Copyright (C) 1987-2002 Free Software Foundation, Inc.
1996-08-26 18:22:31 +00:00
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
2000-03-17 21:46:59 +00:00
Software Foundation; either version 2, or (at your option) any later
1996-08-26 18:22:31 +00:00
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
2000-03-17 21:46:59 +00:00
Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
1996-08-26 18:22:31 +00:00
$BUILTIN let
$FUNCTION let_builtin
$PRODUCES let.c
$SHORT_DOC let arg [arg ...]
Each ARG is an arithmetic expression to be evaluated. Evaluation
2002-07-17 14:10:11 +00:00
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.
1996-08-26 18:22:31 +00:00
The levels are listed in order of decreasing precedence.
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.
If the last ARG evaluates to 0, let returns 1; 0 is returned
otherwise.
$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
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