|
Lines 1-5
Link Here
|
| 1 |
%token ARITH_NUM ARITH_LPAREN ARITH_RPAREN |
1 |
%{ |
|
|
2 |
/*- |
| 3 |
* Copyright (c) 1993 |
| 4 |
* The Regents of the University of California. All rights reserved. |
| 5 |
* |
| 6 |
* This code is derived from software contributed to Berkeley by |
| 7 |
* Kenneth Almquist. |
| 8 |
* |
| 9 |
* Redistribution and use in source and binary forms, with or without |
| 10 |
* modification, are permitted provided that the following conditions |
| 11 |
* are met: |
| 12 |
* 1. Redistributions of source code must retain the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer. |
| 14 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 15 |
* notice, this list of conditions and the following disclaimer in the |
| 16 |
* documentation and/or other materials provided with the distribution. |
| 17 |
* 3. All advertising materials mentioning features or use of this software |
| 18 |
* must display the following acknowledgement: |
| 19 |
* This product includes software developed by the University of |
| 20 |
* California, Berkeley and its contributors. |
| 21 |
* 4. Neither the name of the University nor the names of its contributors |
| 22 |
* may be used to endorse or promote products derived from this software |
| 23 |
* without specific prior written permission. |
| 24 |
* |
| 25 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 26 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 27 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 28 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 29 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 30 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 31 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 32 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 33 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 34 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 35 |
* SUCH DAMAGE. |
| 36 |
*/ |
| 2 |
|
37 |
|
|
|
38 |
#ifndef lint |
| 39 |
#if 0 |
| 40 |
static char sccsid[] = "@(#)arith.y 8.3 (Berkeley) 5/4/95"; |
| 41 |
#endif |
| 42 |
#endif /* not lint */ |
| 43 |
#include <sys/cdefs.h> |
| 44 |
__FBSDID("$FreeBSD: src/bin/sh/arith.y,v 1.10.2.2 2002/07/19 04:38:51 tjr Exp $"); |
| 45 |
|
| 46 |
#include <limits.h> |
| 47 |
#include "shell.h" |
| 48 |
#include "var.h" |
| 49 |
%} |
| 50 |
%union { |
| 51 |
arith_t l_value; |
| 52 |
char* s_value; |
| 53 |
} |
| 54 |
%token <l_value> ARITH_NUM ARITH_LPAREN ARITH_RPAREN |
| 55 |
%token <s_value> ARITH_VAR |
| 56 |
|
| 57 |
%type <l_value> expr |
| 58 |
%right ARITH_ASSIGN |
| 59 |
%right ARITH_ADDASSIGN ARITH_SUBASSIGN |
| 60 |
%right ARITH_MULASSIGN ARITH_DIVASSIGN ARITH_REMASSIGN |
| 61 |
%right ARITH_RSHASSIGN ARITH_LSHASSIGN |
| 62 |
%right ARITH_BANDASSIGN ARITH_BXORASSIGN ARITH_BORASSIGN |
| 3 |
%left ARITH_OR |
63 |
%left ARITH_OR |
| 4 |
%left ARITH_AND |
64 |
%left ARITH_AND |
| 5 |
%left ARITH_BOR |
65 |
%left ARITH_BOR |
|
Lines 18-24
Link Here
|
| 18 |
} |
78 |
} |
| 19 |
; |
79 |
; |
| 20 |
|
80 |
|
| 21 |
|
|
|
| 22 |
expr: ARITH_LPAREN expr ARITH_RPAREN = { $$ = $2; } |
81 |
expr: ARITH_LPAREN expr ARITH_RPAREN = { $$ = $2; } |
| 23 |
| expr ARITH_OR expr = { $$ = $1 ? $1 : $3 ? $3 : 0; } |
82 |
| expr ARITH_OR expr = { $$ = $1 ? $1 : $3 ? $3 : 0; } |
| 24 |
| expr ARITH_AND expr = { $$ = $1 ? ( $3 ? $3 : 0 ) : 0; } |
83 |
| expr ARITH_AND expr = { $$ = $1 ? ( $3 ? $3 : 0 ) : 0; } |
|
Lines 51-112
Link Here
|
| 51 |
| ARITH_SUB expr %prec ARITH_UNARYMINUS = { $$ = -($2); } |
110 |
| ARITH_SUB expr %prec ARITH_UNARYMINUS = { $$ = -($2); } |
| 52 |
| ARITH_ADD expr %prec ARITH_UNARYPLUS = { $$ = $2; } |
111 |
| ARITH_ADD expr %prec ARITH_UNARYPLUS = { $$ = $2; } |
| 53 |
| ARITH_NUM |
112 |
| ARITH_NUM |
|
|
113 |
| ARITH_VAR { |
| 114 |
char *p; |
| 115 |
arith_t arith_val; |
| 116 |
char *str_val; |
| 117 |
|
| 118 |
if (lookupvar($1) == NULL) |
| 119 |
setvarsafe($1, "0", 0); |
| 120 |
str_val = lookupvar($1); |
| 121 |
arith_val = strtoarith_t(str_val, &p, 0); |
| 122 |
/* |
| 123 |
* Conversion is successful only in case |
| 124 |
* we've converted _all_ characters. |
| 125 |
*/ |
| 126 |
if (*p != '\0') |
| 127 |
yyerror("variable conversion error"); |
| 128 |
$$ = arith_val; |
| 129 |
} |
| 130 |
| ARITH_VAR ARITH_ASSIGN expr { |
| 131 |
if (arith_assign($1, $3) != 1) |
| 132 |
yyerror("variable assignment error"); |
| 133 |
$$ = $3; |
| 134 |
} |
| 135 |
| ARITH_VAR ARITH_ADDASSIGN expr { |
| 136 |
arith_t value; |
| 137 |
|
| 138 |
value = atoarith_t(lookupvar($1)) + $3; |
| 139 |
if (arith_assign($1, value) != 0) |
| 140 |
yyerror("variable assignment error"); |
| 141 |
$$ = value; |
| 142 |
} |
| 143 |
| ARITH_VAR ARITH_SUBASSIGN expr { |
| 144 |
arith_t value; |
| 145 |
|
| 146 |
value = atoarith_t(lookupvar($1)) - $3; |
| 147 |
if (arith_assign($1, value) != 0) |
| 148 |
yyerror("variable assignment error"); |
| 149 |
$$ = value; |
| 150 |
} |
| 151 |
| ARITH_VAR ARITH_MULASSIGN expr { |
| 152 |
arith_t value; |
| 153 |
|
| 154 |
value = atoarith_t(lookupvar($1)) * $3; |
| 155 |
if (arith_assign($1, value) != 0) |
| 156 |
yyerror("variable assignment error"); |
| 157 |
$$ = value; |
| 158 |
} |
| 159 |
| ARITH_VAR ARITH_DIVASSIGN expr { |
| 160 |
arith_t value; |
| 161 |
|
| 162 |
if ($3 == 0) |
| 163 |
yyerror("division by zero"); |
| 164 |
|
| 165 |
value = atoarith_t(lookupvar($1)) / $3; |
| 166 |
if (arith_assign($1, value) != 0) |
| 167 |
yyerror("variable assignment error"); |
| 168 |
$$ = value; |
| 169 |
} |
| 170 |
| ARITH_VAR ARITH_REMASSIGN expr { |
| 171 |
arith_t value; |
| 172 |
|
| 173 |
if ($3 == 0) |
| 174 |
yyerror("division by zero"); |
| 175 |
|
| 176 |
value = atoarith_t(lookupvar($1)) % $3; |
| 177 |
if (arith_assign($1, value) != 0) |
| 178 |
yyerror("variable assignment error"); |
| 179 |
$$ = value; |
| 180 |
} |
| 181 |
| ARITH_VAR ARITH_RSHASSIGN expr { |
| 182 |
arith_t value; |
| 183 |
|
| 184 |
value = atoarith_t(lookupvar($1)) >> $3; |
| 185 |
if (arith_assign($1, value) != 0) |
| 186 |
yyerror("variable assignment error"); |
| 187 |
$$ = value; |
| 188 |
} |
| 189 |
| ARITH_VAR ARITH_LSHASSIGN expr { |
| 190 |
arith_t value; |
| 191 |
|
| 192 |
value = atoarith_t(lookupvar($1)) << $3; |
| 193 |
if (arith_assign($1, value) != 0) |
| 194 |
yyerror("variable assignment error"); |
| 195 |
$$ = value; |
| 196 |
} |
| 197 |
| ARITH_VAR ARITH_BANDASSIGN expr { |
| 198 |
arith_t value; |
| 199 |
|
| 200 |
value = atoarith_t(lookupvar($1)) & $3; |
| 201 |
if (arith_assign($1, value) != 0) |
| 202 |
yyerror("variable assignment error"); |
| 203 |
$$ = value; |
| 204 |
} |
| 205 |
| ARITH_VAR ARITH_BXORASSIGN expr { |
| 206 |
arith_t value; |
| 207 |
|
| 208 |
value = atoarith_t(lookupvar($1)) ^ $3; |
| 209 |
if (arith_assign($1, value) != 0) |
| 210 |
yyerror("variable assignment error"); |
| 211 |
$$ = value; |
| 212 |
} |
| 213 |
| ARITH_VAR ARITH_BORASSIGN expr { |
| 214 |
arith_t value; |
| 215 |
|
| 216 |
value = atoarith_t(lookupvar($1)) | $3; |
| 217 |
if (arith_assign($1, value) != 0) |
| 218 |
yyerror("variable assignment error"); |
| 219 |
$$ = value; |
| 220 |
} |
| 54 |
; |
221 |
; |
| 55 |
%% |
222 |
%% |
| 56 |
/*- |
|
|
| 57 |
* Copyright (c) 1993 |
| 58 |
* The Regents of the University of California. All rights reserved. |
| 59 |
* |
| 60 |
* This code is derived from software contributed to Berkeley by |
| 61 |
* Kenneth Almquist. |
| 62 |
* |
| 63 |
* Redistribution and use in source and binary forms, with or without |
| 64 |
* modification, are permitted provided that the following conditions |
| 65 |
* are met: |
| 66 |
* 1. Redistributions of source code must retain the above copyright |
| 67 |
* notice, this list of conditions and the following disclaimer. |
| 68 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 69 |
* notice, this list of conditions and the following disclaimer in the |
| 70 |
* documentation and/or other materials provided with the distribution. |
| 71 |
* 3. All advertising materials mentioning features or use of this software |
| 72 |
* must display the following acknowledgement: |
| 73 |
* This product includes software developed by the University of |
| 74 |
* California, Berkeley and its contributors. |
| 75 |
* 4. Neither the name of the University nor the names of its contributors |
| 76 |
* may be used to endorse or promote products derived from this software |
| 77 |
* without specific prior written permission. |
| 78 |
* |
| 79 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 80 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 81 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 82 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 83 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 84 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 85 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 86 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 87 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 88 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 89 |
* SUCH DAMAGE. |
| 90 |
*/ |
| 91 |
|
| 92 |
#ifndef lint |
| 93 |
#if 0 |
| 94 |
static char sccsid[] = "@(#)arith.y 8.3 (Berkeley) 5/4/95"; |
| 95 |
#endif |
| 96 |
#endif /* not lint */ |
| 97 |
#include <sys/cdefs.h> |
| 98 |
__FBSDID("$FreeBSD: src/bin/sh/arith.y,v 1.14 2003/05/01 16:58:56 obrien Exp $"); |
| 99 |
|
| 100 |
#include "shell.h" |
| 101 |
#include "error.h" |
223 |
#include "error.h" |
| 102 |
#include "output.h" |
224 |
#include "output.h" |
| 103 |
#include "memalloc.h" |
225 |
#include "memalloc.h" |
| 104 |
|
226 |
|
|
|
227 |
#define lstrlen(var) (3 + (2 + CHAR_BIT * sizeof((var))) / 3) |
| 228 |
|
| 105 |
char *arith_buf, *arith_startbuf; |
229 |
char *arith_buf, *arith_startbuf; |
| 106 |
extern void arith_lex_reset(); |
230 |
extern void arith_lex_reset(); |
| 107 |
|
231 |
|
| 108 |
int yylex(void); |
232 |
int yylex(void); |
| 109 |
int yyparse(void); |
233 |
int yyparse(void); |
|
|
234 |
|
| 235 |
int |
| 236 |
arith_assign(char *name, arith_t value) { |
| 237 |
char *str; |
| 238 |
int ret; |
| 239 |
|
| 240 |
str = (char *)ckmalloc(lstrlen(value)); |
| 241 |
sprintf(str, ARITH_FORMAT_STR, value); |
| 242 |
ret = setvarsafe(name, str, 0); |
| 243 |
free(str); |
| 244 |
return ret; |
| 245 |
} |
| 110 |
|
246 |
|
| 111 |
int |
247 |
int |
| 112 |
arith(char *s) |
248 |
arith(char *s) |