arith_token.c revision 1.3 1 1.3 kre /* $NetBSD: arith_token.c,v 1.3 2017/03/20 13:12:35 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.3 kre __RCSID("$NetBSD: arith_token.c,v 1.3 2017/03/20 13:12:35 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.1 kre
58 1.1 kre #if ARITH_BOR + ARITH_ASS_GAP != ARITH_BORASS || \
59 1.1 kre ARITH_ASS + ARITH_ASS_GAP != ARITH_EQ
60 1.1 kre #error Arithmetic tokens are out of order.
61 1.1 kre #endif
62 1.1 kre
63 1.1 kre /*
64 1.1 kre * Scan next arithmetic token, return its type,
65 1.1 kre * leave its value (when applicable) in (global) a_t_val.
66 1.1 kre *
67 1.1 kre * Input text is in (global) arith_buf which is updated to
68 1.1 kre * refer to the next char after the token returned, except
69 1.1 kre * on error (ARITH_BAD returned) where arith_buf is not altered.
70 1.1 kre */
71 1.1 kre int
72 1.1 kre arith_token(void)
73 1.1 kre {
74 1.1 kre int token;
75 1.1 kre const char *buf = arith_buf;
76 1.1 kre char *end;
77 1.1 kre const char *p;
78 1.1 kre
79 1.1 kre for (;;) {
80 1.1 kre token = *buf;
81 1.1 kre
82 1.2 kre if (isdigit(token)) {
83 1.2 kre /*
84 1.2 kre * Numbers all start with a digit, and nothing
85 1.2 kre * else does, the number ends wherever
86 1.2 kre * strtoimax() stops...
87 1.2 kre */
88 1.1 kre a_t_val.val = strtoimax(buf, &end, 0);
89 1.1 kre arith_buf = end;
90 1.1 kre return ARITH_NUM;
91 1.1 kre
92 1.3 kre } else if (is_name(token)) {
93 1.2 kre /*
94 1.2 kre * Variable names all start with an alpha (or '_')
95 1.2 kre * and nothing else does. They continue for the
96 1.2 kre * longest unbroken sequence of alphanumerics ( + _ )
97 1.2 kre */
98 1.1 kre p = buf;
99 1.1 kre while (buf++, is_in_name(*buf))
100 1.1 kre ;
101 1.1 kre a_t_val.name = stalloc(buf - p + 1);
102 1.1 kre memcpy(a_t_val.name, p, buf - p);
103 1.1 kre a_t_val.name[buf - p] = '\0';
104 1.1 kre arith_buf = buf;
105 1.1 kre return ARITH_VAR;
106 1.1 kre
107 1.2 kre } else switch (token) {
108 1.2 kre /*
109 1.2 kre * everything else must be some kind of
110 1.2 kre * operator, white space, or an error.
111 1.2 kre */
112 1.2 kre case ' ':
113 1.2 kre case '\t':
114 1.2 kre case '\n':
115 1.2 kre buf++;
116 1.2 kre continue;
117 1.2 kre
118 1.2 kre default:
119 1.2 kre error("arithmetic: unexpected '%c' (%#x) in expression",
120 1.2 kre token, token);
121 1.2 kre /* NOTREACHED */
122 1.2 kre
123 1.1 kre case '=':
124 1.1 kre token = ARITH_ASS;
125 1.1 kre checkeq:
126 1.1 kre buf++;
127 1.1 kre checkeqcur:
128 1.1 kre if (*buf != '=')
129 1.1 kre goto out;
130 1.1 kre token += ARITH_ASS_GAP;
131 1.1 kre break;
132 1.1 kre
133 1.1 kre case '>':
134 1.1 kre switch (*++buf) {
135 1.1 kre case '=':
136 1.1 kre token = ARITH_GE;
137 1.1 kre break;
138 1.1 kre case '>':
139 1.1 kre token = ARITH_RSHIFT;
140 1.1 kre goto checkeq;
141 1.1 kre default:
142 1.1 kre token = ARITH_GT;
143 1.1 kre goto out;
144 1.1 kre }
145 1.1 kre break;
146 1.1 kre
147 1.1 kre case '<':
148 1.1 kre switch (*++buf) {
149 1.1 kre case '=':
150 1.1 kre token = ARITH_LE;
151 1.1 kre break;
152 1.1 kre case '<':
153 1.1 kre token = ARITH_LSHIFT;
154 1.1 kre goto checkeq;
155 1.1 kre default:
156 1.1 kre token = ARITH_LT;
157 1.1 kre goto out;
158 1.1 kre }
159 1.1 kre break;
160 1.1 kre
161 1.1 kre case '|':
162 1.1 kre if (*++buf != '|') {
163 1.1 kre token = ARITH_BOR;
164 1.1 kre goto checkeqcur;
165 1.1 kre }
166 1.1 kre token = ARITH_OR;
167 1.1 kre break;
168 1.1 kre
169 1.1 kre case '&':
170 1.1 kre if (*++buf != '&') {
171 1.1 kre token = ARITH_BAND;
172 1.1 kre goto checkeqcur;
173 1.1 kre }
174 1.1 kre token = ARITH_AND;
175 1.1 kre break;
176 1.1 kre
177 1.1 kre case '!':
178 1.1 kre if (*++buf != '=') {
179 1.1 kre token = ARITH_NOT;
180 1.1 kre goto out;
181 1.1 kre }
182 1.1 kre token = ARITH_NE;
183 1.1 kre break;
184 1.1 kre
185 1.1 kre case 0:
186 1.1 kre goto out;
187 1.1 kre
188 1.1 kre case '(':
189 1.1 kre token = ARITH_LPAREN;
190 1.1 kre break;
191 1.1 kre case ')':
192 1.1 kre token = ARITH_RPAREN;
193 1.1 kre break;
194 1.1 kre
195 1.1 kre case '*':
196 1.1 kre token = ARITH_MUL;
197 1.1 kre goto checkeq;
198 1.1 kre case '/':
199 1.1 kre token = ARITH_DIV;
200 1.1 kre goto checkeq;
201 1.1 kre case '%':
202 1.1 kre token = ARITH_REM;
203 1.1 kre goto checkeq;
204 1.1 kre
205 1.1 kre case '+':
206 1.1 kre if (buf[1] == '+')
207 1.1 kre error("arithmetic: ++ operator unsupported");
208 1.1 kre token = ARITH_ADD;
209 1.1 kre goto checkeq;
210 1.1 kre case '-':
211 1.1 kre if (buf[1] == '-')
212 1.1 kre error("arithmetic: -- operator unsupported");
213 1.1 kre token = ARITH_SUB;
214 1.1 kre goto checkeq;
215 1.1 kre case '~':
216 1.1 kre token = ARITH_BNOT;
217 1.1 kre break;
218 1.1 kre case '^':
219 1.1 kre token = ARITH_BXOR;
220 1.1 kre goto checkeq;
221 1.1 kre
222 1.1 kre case '?':
223 1.1 kre token = ARITH_QMARK;
224 1.1 kre break;
225 1.1 kre case ':':
226 1.1 kre token = ARITH_COLON;
227 1.1 kre break;
228 1.1 kre }
229 1.1 kre break;
230 1.1 kre }
231 1.1 kre buf++;
232 1.1 kre out:
233 1.1 kre arith_buf = buf;
234 1.1 kre return token;
235 1.1 kre }
236