arith_token.c revision 1.1 1 /* $NetBSD: arith_token.c,v 1.1 2017/03/20 11:26:07 kre Exp $ */
2
3 /*-
4 * Copyright (c) 2002
5 * Herbert Xu.
6 * Copyright (c) 1991, 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * Kenneth Almquist.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * From FreeBSD, from dash
37 */
38
39 #include <sys/cdefs.h>
40
41 #ifndef lint
42 __RCSID("$NetBSD: arith_token.c,v 1.1 2017/03/20 11:26:07 kre Exp $");
43 #endif /* not lint */
44
45 #include <inttypes.h>
46 #include <limits.h>
47 #include <stdlib.h>
48 #include <string.h>
49
50 #include "shell.h"
51 #include "arith_tokens.h"
52 #include "expand.h"
53 #include "error.h"
54 #include "memalloc.h"
55 #include "parser.h"
56 #include "syntax.h"
57
58 #if ARITH_BOR + ARITH_ASS_GAP != ARITH_BORASS || \
59 ARITH_ASS + ARITH_ASS_GAP != ARITH_EQ
60 #error Arithmetic tokens are out of order.
61 #endif
62
63 /*
64 * Scan next arithmetic token, return its type,
65 * leave its value (when applicable) in (global) a_t_val.
66 *
67 * Input text is in (global) arith_buf which is updated to
68 * refer to the next char after the token returned, except
69 * on error (ARITH_BAD returned) where arith_buf is not altered.
70 */
71 int
72 arith_token(void)
73 {
74 int token;
75 const char *buf = arith_buf;
76 char *end;
77 const char *p;
78
79 for (;;) {
80 token = *buf;
81
82 switch (token) {
83 case ' ':
84 case '\t':
85 case '\n':
86 buf++;
87 continue;
88
89 default:
90 error("arithmetic: unexpected '%c' (%#x) in expression",
91 token, token);
92 /* NOTREACHED */
93
94 case '0': case '1': case '2': case '3': case '4':
95 case '5': case '6': case '7': case '8': case '9':
96 a_t_val.val = strtoimax(buf, &end, 0);
97 arith_buf = end;
98 return ARITH_NUM;
99
100 case 'A': case 'B': case 'C': case 'D': case 'E':
101 case 'F': case 'G': case 'H': case 'I': case 'J':
102 case 'K': case 'L': case 'M': case 'N': case 'O':
103 case 'P': case 'Q': case 'R': case 'S': case 'T':
104 case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
105 case 'a': case 'b': case 'c': case 'd': case 'e':
106 case 'f': case 'g': case 'h': case 'i': case 'j':
107 case 'k': case 'l': case 'm': case 'n': case 'o':
108 case 'p': case 'q': case 'r': case 's': case 't':
109 case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
110 case '_':
111 p = buf;
112 while (buf++, is_in_name(*buf))
113 ;
114 a_t_val.name = stalloc(buf - p + 1);
115 memcpy(a_t_val.name, p, buf - p);
116 a_t_val.name[buf - p] = '\0';
117 arith_buf = buf;
118 return ARITH_VAR;
119
120 case '=':
121 token = ARITH_ASS;
122 checkeq:
123 buf++;
124 checkeqcur:
125 if (*buf != '=')
126 goto out;
127 token += ARITH_ASS_GAP;
128 break;
129
130 case '>':
131 switch (*++buf) {
132 case '=':
133 token = ARITH_GE;
134 break;
135 case '>':
136 token = ARITH_RSHIFT;
137 goto checkeq;
138 default:
139 token = ARITH_GT;
140 goto out;
141 }
142 break;
143
144 case '<':
145 switch (*++buf) {
146 case '=':
147 token = ARITH_LE;
148 break;
149 case '<':
150 token = ARITH_LSHIFT;
151 goto checkeq;
152 default:
153 token = ARITH_LT;
154 goto out;
155 }
156 break;
157
158 case '|':
159 if (*++buf != '|') {
160 token = ARITH_BOR;
161 goto checkeqcur;
162 }
163 token = ARITH_OR;
164 break;
165
166 case '&':
167 if (*++buf != '&') {
168 token = ARITH_BAND;
169 goto checkeqcur;
170 }
171 token = ARITH_AND;
172 break;
173
174 case '!':
175 if (*++buf != '=') {
176 token = ARITH_NOT;
177 goto out;
178 }
179 token = ARITH_NE;
180 break;
181
182 case 0:
183 goto out;
184
185 case '(':
186 token = ARITH_LPAREN;
187 break;
188 case ')':
189 token = ARITH_RPAREN;
190 break;
191
192 case '*':
193 token = ARITH_MUL;
194 goto checkeq;
195 case '/':
196 token = ARITH_DIV;
197 goto checkeq;
198 case '%':
199 token = ARITH_REM;
200 goto checkeq;
201
202 case '+':
203 if (buf[1] == '+')
204 error("arithmetic: ++ operator unsupported");
205 token = ARITH_ADD;
206 goto checkeq;
207 case '-':
208 if (buf[1] == '-')
209 error("arithmetic: -- operator unsupported");
210 token = ARITH_SUB;
211 goto checkeq;
212 case '~':
213 token = ARITH_BNOT;
214 break;
215 case '^':
216 token = ARITH_BXOR;
217 goto checkeq;
218
219 case '?':
220 token = ARITH_QMARK;
221 break;
222 case ':':
223 token = ARITH_COLON;
224 break;
225 }
226 break;
227 }
228 buf++;
229 out:
230 arith_buf = buf;
231 return token;
232 }
233