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