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