arith_token.c revision 1.1 1 1.1 kre /* $NetBSD: arith_token.c,v 1.1 2017/03/20 11:26:07 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.1 kre __RCSID("$NetBSD: arith_token.c,v 1.1 2017/03/20 11:26:07 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.1 kre switch (token) {
83 1.1 kre case ' ':
84 1.1 kre case '\t':
85 1.1 kre case '\n':
86 1.1 kre buf++;
87 1.1 kre continue;
88 1.1 kre
89 1.1 kre default:
90 1.1 kre error("arithmetic: unexpected '%c' (%#x) in expression",
91 1.1 kre token, token);
92 1.1 kre /* NOTREACHED */
93 1.1 kre
94 1.1 kre case '0': case '1': case '2': case '3': case '4':
95 1.1 kre case '5': case '6': case '7': case '8': case '9':
96 1.1 kre a_t_val.val = strtoimax(buf, &end, 0);
97 1.1 kre arith_buf = end;
98 1.1 kre return ARITH_NUM;
99 1.1 kre
100 1.1 kre case 'A': case 'B': case 'C': case 'D': case 'E':
101 1.1 kre case 'F': case 'G': case 'H': case 'I': case 'J':
102 1.1 kre case 'K': case 'L': case 'M': case 'N': case 'O':
103 1.1 kre case 'P': case 'Q': case 'R': case 'S': case 'T':
104 1.1 kre case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
105 1.1 kre case 'a': case 'b': case 'c': case 'd': case 'e':
106 1.1 kre case 'f': case 'g': case 'h': case 'i': case 'j':
107 1.1 kre case 'k': case 'l': case 'm': case 'n': case 'o':
108 1.1 kre case 'p': case 'q': case 'r': case 's': case 't':
109 1.1 kre case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
110 1.1 kre case '_':
111 1.1 kre p = buf;
112 1.1 kre while (buf++, is_in_name(*buf))
113 1.1 kre ;
114 1.1 kre a_t_val.name = stalloc(buf - p + 1);
115 1.1 kre memcpy(a_t_val.name, p, buf - p);
116 1.1 kre a_t_val.name[buf - p] = '\0';
117 1.1 kre arith_buf = buf;
118 1.1 kre return ARITH_VAR;
119 1.1 kre
120 1.1 kre case '=':
121 1.1 kre token = ARITH_ASS;
122 1.1 kre checkeq:
123 1.1 kre buf++;
124 1.1 kre checkeqcur:
125 1.1 kre if (*buf != '=')
126 1.1 kre goto out;
127 1.1 kre token += ARITH_ASS_GAP;
128 1.1 kre break;
129 1.1 kre
130 1.1 kre case '>':
131 1.1 kre switch (*++buf) {
132 1.1 kre case '=':
133 1.1 kre token = ARITH_GE;
134 1.1 kre break;
135 1.1 kre case '>':
136 1.1 kre token = ARITH_RSHIFT;
137 1.1 kre goto checkeq;
138 1.1 kre default:
139 1.1 kre token = ARITH_GT;
140 1.1 kre goto out;
141 1.1 kre }
142 1.1 kre break;
143 1.1 kre
144 1.1 kre case '<':
145 1.1 kre switch (*++buf) {
146 1.1 kre case '=':
147 1.1 kre token = ARITH_LE;
148 1.1 kre break;
149 1.1 kre case '<':
150 1.1 kre token = ARITH_LSHIFT;
151 1.1 kre goto checkeq;
152 1.1 kre default:
153 1.1 kre token = ARITH_LT;
154 1.1 kre goto out;
155 1.1 kre }
156 1.1 kre break;
157 1.1 kre
158 1.1 kre case '|':
159 1.1 kre if (*++buf != '|') {
160 1.1 kre token = ARITH_BOR;
161 1.1 kre goto checkeqcur;
162 1.1 kre }
163 1.1 kre token = ARITH_OR;
164 1.1 kre break;
165 1.1 kre
166 1.1 kre case '&':
167 1.1 kre if (*++buf != '&') {
168 1.1 kre token = ARITH_BAND;
169 1.1 kre goto checkeqcur;
170 1.1 kre }
171 1.1 kre token = ARITH_AND;
172 1.1 kre break;
173 1.1 kre
174 1.1 kre case '!':
175 1.1 kre if (*++buf != '=') {
176 1.1 kre token = ARITH_NOT;
177 1.1 kre goto out;
178 1.1 kre }
179 1.1 kre token = ARITH_NE;
180 1.1 kre break;
181 1.1 kre
182 1.1 kre case 0:
183 1.1 kre goto out;
184 1.1 kre
185 1.1 kre case '(':
186 1.1 kre token = ARITH_LPAREN;
187 1.1 kre break;
188 1.1 kre case ')':
189 1.1 kre token = ARITH_RPAREN;
190 1.1 kre break;
191 1.1 kre
192 1.1 kre case '*':
193 1.1 kre token = ARITH_MUL;
194 1.1 kre goto checkeq;
195 1.1 kre case '/':
196 1.1 kre token = ARITH_DIV;
197 1.1 kre goto checkeq;
198 1.1 kre case '%':
199 1.1 kre token = ARITH_REM;
200 1.1 kre goto checkeq;
201 1.1 kre
202 1.1 kre case '+':
203 1.1 kre if (buf[1] == '+')
204 1.1 kre error("arithmetic: ++ operator unsupported");
205 1.1 kre token = ARITH_ADD;
206 1.1 kre goto checkeq;
207 1.1 kre case '-':
208 1.1 kre if (buf[1] == '-')
209 1.1 kre error("arithmetic: -- operator unsupported");
210 1.1 kre token = ARITH_SUB;
211 1.1 kre goto checkeq;
212 1.1 kre case '~':
213 1.1 kre token = ARITH_BNOT;
214 1.1 kre break;
215 1.1 kre case '^':
216 1.1 kre token = ARITH_BXOR;
217 1.1 kre goto checkeq;
218 1.1 kre
219 1.1 kre case '?':
220 1.1 kre token = ARITH_QMARK;
221 1.1 kre break;
222 1.1 kre case ':':
223 1.1 kre token = ARITH_COLON;
224 1.1 kre break;
225 1.1 kre }
226 1.1 kre break;
227 1.1 kre }
228 1.1 kre buf++;
229 1.1 kre out:
230 1.1 kre arith_buf = buf;
231 1.1 kre return token;
232 1.1 kre }
233