View | Details | Raw Unified | Return to bug 52972
Collapse All | Expand All

(-)./FreeBSD_ASH_HEAD/arith.h (-1 / +2 lines)
Lines 31-38 Link Here
31
 * SUCH DAMAGE.
31
 * SUCH DAMAGE.
32
 *
32
 *
33
 *	@(#)arith.h	1.1 (Berkeley) 5/4/95
33
 *	@(#)arith.h	1.1 (Berkeley) 5/4/95
34
 * $FreeBSD: src/bin/sh/arith.h,v 1.6 2002/02/02 06:50:45 imp Exp $
34
 * $FreeBSD: src/bin/sh/arith.h,v 1.5.2.1 2002/07/19 04:38:51 tjr Exp $
35
 */
35
 */
36
36
37
int arith_assign(char *, arith_t);
37
int arith(char *);
38
int arith(char *);
38
int expcmd(int , char **);
39
int expcmd(int , char **);
(-)./FreeBSD_ASH_HEAD/arith.y (-47 / +183 lines)
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)
(-)./FreeBSD_ASH_HEAD/arith_lex.l (-3 / +45 lines)
Lines 41-52 Link Here
41
#endif
41
#endif
42
#endif /* not lint */
42
#endif /* not lint */
43
#include <sys/cdefs.h>
43
#include <sys/cdefs.h>
44
__FBSDID("$FreeBSD: src/bin/sh/arith_lex.l,v 1.18 2003/05/01 16:58:56 obrien Exp $");
44
__FBSDID("$FreeBSD: src/bin/sh/arith_lex.l,v 1.14.2.2 2002/07/19 04:38:51 tjr Exp $");
45
45
46
#include "shell.h"
46
#include "y.tab.h"
47
#include "y.tab.h"
47
#include "error.h"
48
#include "error.h"
49
#include "var.h"
50
#include "memalloc.h"
48
51
49
extern int yylval;
50
extern char *arith_buf, *arith_startbuf;
52
extern char *arith_buf, *arith_startbuf;
51
#undef YY_INPUT
53
#undef YY_INPUT
52
#define YY_INPUT(buf,result,max) \
54
#define YY_INPUT(buf,result,max) \
Lines 56-62 Link Here
56
58
57
%%
59
%%
58
[ \t\n]	{ ; }
60
[ \t\n]	{ ; }
59
[0-9]+	{ yylval = atol(yytext); return(ARITH_NUM); }
61
62
0x[a-fA-F0-9]+	{
63
		yylval.l_value = strtoarith_t(yytext, NULL, 16);
64
		return(ARITH_NUM);
65
		}
66
67
0[0-7]+	{
68
		yylval.l_value = strtoarith_t(yytext, NULL, 8);
69
		return(ARITH_NUM);
70
		}
71
72
[0-9]+		{
73
		yylval.l_value = strtoarith_t(yytext, NULL, 10);
74
		return(ARITH_NUM);
75
		}
76
77
78
[A-Za-z][A-Za-z0-9_]*	{
79
			/*
80
			 * If variable doesn't exist, we should  initialize
81
			 * it to zero.
82
			 */
83
			char *temp;
84
			if (lookupvar(yytext) == NULL)
85
				setvarsafe(yytext, "0", 0);
86
			temp = (char *)ckmalloc(strlen(yytext) + 1);
87
			yylval.s_value = strcpy(temp, yytext);
88
89
			return(ARITH_VAR);
90
		}
60
"("	{ return(ARITH_LPAREN); }
91
"("	{ return(ARITH_LPAREN); }
61
")"	{ return(ARITH_RPAREN); }
92
")"	{ return(ARITH_RPAREN); }
62
"||"	{ return(ARITH_OR); }
93
"||"	{ return(ARITH_OR); }
Lines 79-84 Link Here
79
"-"	{ return(ARITH_SUB); }
110
"-"	{ return(ARITH_SUB); }
80
"~"	{ return(ARITH_BNOT); }
111
"~"	{ return(ARITH_BNOT); }
81
"!"	{ return(ARITH_NOT); }
112
"!"	{ return(ARITH_NOT); }
113
"="	{ return(ARITH_ASSIGN); }
114
"+="	{ return(ARITH_ADDASSIGN); }
115
"-="	{ return(ARITH_SUBASSIGN); }
116
"*="	{ return(ARITH_MULASSIGN); }
117
"/="	{ return(ARITH_DIVASSIGN); }
118
"%="	{ return(ARITH_REMASSIGN); }
119
">>="	{ return(ARITH_RSHASSIGN); }
120
"<<="	{ return(ARITH_LSHASSIGN); }
121
"&="	{ return(ARITH_BANDASSIGN); }
122
"^="	{ return(ARITH_BXORASSIGN); }
123
"|="	{ return(ARITH_BORASSIGN); }
82
.	{ error("arith: syntax error: \"%s\"\n", arith_startbuf); }
124
.	{ error("arith: syntax error: \"%s\"\n", arith_startbuf); }
83
%%
125
%%
84
126
(-)./FreeBSD_ASH_HEAD/shell.h (+8 lines)
Lines 51-56 Link Here
51
#define JOBS 1
51
#define JOBS 1
52
/* #define DEBUG 1 */
52
/* #define DEBUG 1 */
53
53
54
/*
55
 * Type of used arithmetics. SUSv3 requires us to have at least signed long.
56
 */
57
typedef long arith_t;
58
#define strtoarith_t(nptr, endptr, base)	strtol(nptr, endptr, base)
59
#define atoarith_t(arg)				strtol(arg, NULL, 0)
60
#define ARITH_FORMAT_STR					"%ld"
61
54
typedef void *pointer;
62
typedef void *pointer;
55
#define STATIC  static
63
#define STATIC  static
56
#define MKINIT	/* empty */
64
#define MKINIT	/* empty */

Return to bug 52972