lex.c revision 1.1.1.1 1 1.1 jtc /*
2 1.1 jtc * lexical analysis and source input
3 1.1 jtc */
4 1.1 jtc
5 1.1 jtc #include "sh.h"
6 1.1 jtc #include <ctype.h>
7 1.1 jtc
8 1.1 jtc static void readhere ARGS((struct ioword *iop));
9 1.1 jtc static int getsc_ ARGS((void));
10 1.1 jtc static void getsc_line ARGS((Source *s));
11 1.1 jtc static char *get_brace_var ARGS((XString *wsp, char *wp));
12 1.1 jtc static int arraysub ARGS((char **strp));
13 1.1 jtc static const char *ungetsc_ ARGS((int c));
14 1.1 jtc static int getsc_bn_ ARGS((void));
15 1.1 jtc
16 1.1 jtc static void gethere ARGS((void));
17 1.1 jtc
18 1.1 jtc /* optimized getsc_() */
19 1.1 jtc #define getsc() ((*source->str != '\0') ? *source->str++ : getsc_())
20 1.1 jtc #define getsc_bn() (*source->str != '\0' && *source->str != '\\' \
21 1.1 jtc ? *source->str++ : getsc_bn_())
22 1.1 jtc #define ungetsc(c) (source->str > source->start ? source->str-- : ungetsc_(c))
23 1.1 jtc
24 1.1 jtc
25 1.1 jtc /*
26 1.1 jtc * Lexical analyzer
27 1.1 jtc *
28 1.1 jtc * tokens are not regular expressions, they are LL(1).
29 1.1 jtc * for example, "${var:-${PWD}}", and "$(size $(whence ksh))".
30 1.1 jtc * hence the state stack.
31 1.1 jtc */
32 1.1 jtc
33 1.1 jtc int
34 1.1 jtc yylex(cf)
35 1.1 jtc int cf;
36 1.1 jtc {
37 1.1 jtc register int c, state;
38 1.1 jtc char states [64], *statep = states; /* XXX overflow check */
39 1.1 jtc XString ws; /* expandable output word */
40 1.1 jtc register char *wp; /* output word pointer */
41 1.1 jtc register char *sp, *dp;
42 1.1 jtc char UNINITIALIZED(*ddparen_start);
43 1.1 jtc int istate;
44 1.1 jtc int UNINITIALIZED(c2);
45 1.1 jtc int UNINITIALIZED(nparen), UNINITIALIZED(csstate);
46 1.1 jtc int UNINITIALIZED(ndparen);
47 1.1 jtc int UNINITIALIZED(indquotes);
48 1.1 jtc
49 1.1 jtc
50 1.1 jtc Again:
51 1.1 jtc Xinit(ws, wp, 64, ATEMP);
52 1.1 jtc
53 1.1 jtc if (cf&ONEWORD)
54 1.1 jtc istate = SWORD;
55 1.1 jtc else if (cf&LETEXPR) {
56 1.1 jtc *wp++ = OQUOTE; /* enclose arguments in (double) quotes */
57 1.1 jtc istate = SDPAREN;
58 1.1 jtc ndparen = 0;
59 1.1 jtc } else { /* normal lexing */
60 1.1 jtc istate = (cf & HEREDELIM) ? SHEREDELIM : SBASE;
61 1.1 jtc while ((c = getsc()) == ' ' || c == '\t')
62 1.1 jtc ;
63 1.1 jtc if (c == '#')
64 1.1 jtc while ((c = getsc()) != '\0' && c != '\n')
65 1.1 jtc ;
66 1.1 jtc ungetsc(c);
67 1.1 jtc }
68 1.1 jtc if (source->flags & SF_ALIAS) { /* trailing ' ' in alias definition */
69 1.1 jtc source->flags &= ~SF_ALIAS;
70 1.1 jtc /* In POSIX mode, a trailing space only counts if we are
71 1.1 jtc * parsing a simple command
72 1.1 jtc */
73 1.1 jtc if (!Flag(FPOSIX) || (cf & CMDWORD))
74 1.1 jtc cf |= ALIAS;
75 1.1 jtc }
76 1.1 jtc
77 1.1 jtc /* collect non-special or quoted characters to form word */
78 1.1 jtc for (*statep = state = istate;
79 1.1 jtc !((c = getsc()) == 0 || ((state == SBASE || state == SHEREDELIM)
80 1.1 jtc && ctype(c, C_LEX1))); )
81 1.1 jtc {
82 1.1 jtc Xcheck(ws, wp);
83 1.1 jtc switch (state) {
84 1.1 jtc case SBASE:
85 1.1 jtc if (c == '[' && (cf & (VARASN|ARRAYVAR))) {
86 1.1 jtc *wp = EOS; /* temporary */
87 1.1 jtc if (is_wdvarname(Xstring(ws, wp), FALSE))
88 1.1 jtc {
89 1.1 jtc char *p, *tmp;
90 1.1 jtc
91 1.1 jtc if (arraysub(&tmp)) {
92 1.1 jtc *wp++ = CHAR;
93 1.1 jtc *wp++ = c;
94 1.1 jtc for (p = tmp; *p; ) {
95 1.1 jtc Xcheck(ws, wp);
96 1.1 jtc *wp++ = CHAR;
97 1.1 jtc *wp++ = *p++;
98 1.1 jtc }
99 1.1 jtc afree(tmp, ATEMP);
100 1.1 jtc break;
101 1.1 jtc } else {
102 1.1 jtc Source *s;
103 1.1 jtc
104 1.1 jtc s = pushs(SREREAD,
105 1.1 jtc source->areap);
106 1.1 jtc s->start = s->str
107 1.1 jtc = s->u.freeme = tmp;
108 1.1 jtc s->next = source;
109 1.1 jtc source = s;
110 1.1 jtc }
111 1.1 jtc }
112 1.1 jtc *wp++ = CHAR;
113 1.1 jtc *wp++ = c;
114 1.1 jtc break;
115 1.1 jtc }
116 1.1 jtc /* fall through.. */
117 1.1 jtc Sbase1: /* includes *(...|...) pattern (*+?@!) */
118 1.1 jtc #ifdef KSH
119 1.1 jtc if (c == '*' || c == '@' || c == '+' || c == '?'
120 1.1 jtc || c == '!')
121 1.1 jtc {
122 1.1 jtc c2 = getsc();
123 1.1 jtc if (c2 == '(' /*)*/ ) {
124 1.1 jtc *wp++ = OPAT;
125 1.1 jtc *wp++ = c;
126 1.1 jtc *++statep = state = SPATTERN;
127 1.1 jtc break;
128 1.1 jtc }
129 1.1 jtc ungetsc(c2);
130 1.1 jtc }
131 1.1 jtc #endif /* KSH */
132 1.1 jtc /* fall through.. */
133 1.1 jtc Sbase2: /* doesn't include *(...|...) pattern (*+?@!) */
134 1.1 jtc switch (c) {
135 1.1 jtc case '\\':
136 1.1 jtc c = getsc();
137 1.1 jtc if (c != '\n') {
138 1.1 jtc #ifdef OS2
139 1.1 jtc if (isalnum(c)) {
140 1.1 jtc *wp++ = CHAR, *wp++ = '\\';
141 1.1 jtc *wp++ = CHAR, *wp++ = c;
142 1.1 jtc } else
143 1.1 jtc #endif
144 1.1 jtc *wp++ = QCHAR, *wp++ = c;
145 1.1 jtc } else
146 1.1 jtc if (wp == Xstring(ws, wp)) {
147 1.1 jtc Xfree(ws, wp); /* free word */
148 1.1 jtc goto Again;
149 1.1 jtc }
150 1.1 jtc break;
151 1.1 jtc case '\'':
152 1.1 jtc *++statep = state = SSQUOTE;
153 1.1 jtc *wp++ = OQUOTE;
154 1.1 jtc break;
155 1.1 jtc case '"':
156 1.1 jtc *++statep = state = SDQUOTE;
157 1.1 jtc *wp++ = OQUOTE;
158 1.1 jtc break;
159 1.1 jtc default:
160 1.1 jtc goto Subst;
161 1.1 jtc }
162 1.1 jtc break;
163 1.1 jtc
164 1.1 jtc Subst:
165 1.1 jtc switch (c) {
166 1.1 jtc case '\\':
167 1.1 jtc c = getsc();
168 1.1 jtc switch (c) {
169 1.1 jtc case '\n':
170 1.1 jtc break;
171 1.1 jtc case '"': case '\\':
172 1.1 jtc case '$': case '`':
173 1.1 jtc *wp++ = QCHAR, *wp++ = c;
174 1.1 jtc break;
175 1.1 jtc default:
176 1.1 jtc Xcheck(ws, wp);
177 1.1 jtc *wp++ = CHAR, *wp++ = '\\';
178 1.1 jtc *wp++ = CHAR, *wp++ = c;
179 1.1 jtc break;
180 1.1 jtc }
181 1.1 jtc break;
182 1.1 jtc case '$':
183 1.1 jtc c = getsc();
184 1.1 jtc if (c == '(') /*)*/ {
185 1.1 jtc c = getsc();
186 1.1 jtc if (c == '(') /*)*/ {
187 1.1 jtc *++statep = state = SDDPAREN;
188 1.1 jtc nparen = 2;
189 1.1 jtc ddparen_start = wp;
190 1.1 jtc *wp++ = EXPRSUB;
191 1.1 jtc } else {
192 1.1 jtc ungetsc(c);
193 1.1 jtc *++statep = state = SPAREN;
194 1.1 jtc nparen = 1;
195 1.1 jtc csstate = 0;
196 1.1 jtc *wp++ = COMSUB;
197 1.1 jtc }
198 1.1 jtc } else if (c == '{') /*}*/ {
199 1.1 jtc *wp++ = OSUBST;
200 1.1 jtc wp = get_brace_var(&ws, wp);
201 1.1 jtc /* If this is a trim operation,
202 1.1 jtc * wrap @(...) around the pattern
203 1.1 jtc * (allows easy handling of ${a#b|c})
204 1.1 jtc */
205 1.1 jtc c = getsc_bn();
206 1.1 jtc if (c == '#' || c == '%') {
207 1.1 jtc *wp++ = CHAR, *wp++ = c;
208 1.1 jtc if ((c2 = getsc_bn()) == c)
209 1.1 jtc *wp++ = CHAR, *wp++ = c;
210 1.1 jtc else
211 1.1 jtc ungetsc(c2);
212 1.1 jtc *wp++ = OPAT, *wp++ = '@';
213 1.1 jtc *++statep = state = STBRACE;
214 1.1 jtc } else {
215 1.1 jtc ungetsc(c);
216 1.1 jtc *++statep = state = SBRACE;
217 1.1 jtc }
218 1.1 jtc } else if (ctype(c, C_ALPHA)) {
219 1.1 jtc *wp++ = OSUBST;
220 1.1 jtc do {
221 1.1 jtc Xcheck(ws, wp);
222 1.1 jtc *wp++ = c;
223 1.1 jtc c = getsc();
224 1.1 jtc } while (ctype(c, C_ALPHA|C_DIGIT));
225 1.1 jtc *wp++ = '\0';
226 1.1 jtc *wp++ = CSUBST;
227 1.1 jtc ungetsc(c);
228 1.1 jtc } else if (ctype(c, C_DIGIT|C_VAR1)) {
229 1.1 jtc Xcheck(ws, wp);
230 1.1 jtc *wp++ = OSUBST;
231 1.1 jtc *wp++ = c;
232 1.1 jtc *wp++ = '\0';
233 1.1 jtc *wp++ = CSUBST;
234 1.1 jtc } else {
235 1.1 jtc *wp++ = CHAR, *wp++ = '$';
236 1.1 jtc ungetsc(c);
237 1.1 jtc }
238 1.1 jtc break;
239 1.1 jtc case '`':
240 1.1 jtc *++statep = state = SBQUOTE;
241 1.1 jtc *wp++ = COMSUB;
242 1.1 jtc /* Need to know if we are inside double quotes
243 1.1 jtc * since sh/at&t-ksh translate the \" to " in
244 1.1 jtc * "`..\"..`".
245 1.1 jtc */
246 1.1 jtc indquotes = 0;
247 1.1 jtc if (!Flag(FPOSIX))
248 1.1 jtc for (sp = statep; sp > states; --sp)
249 1.1 jtc if (*sp == SDQUOTE)
250 1.1 jtc indquotes = 1;
251 1.1 jtc break;
252 1.1 jtc default:
253 1.1 jtc *wp++ = CHAR, *wp++ = c;
254 1.1 jtc }
255 1.1 jtc break;
256 1.1 jtc
257 1.1 jtc case SSQUOTE:
258 1.1 jtc if (c == '\'') {
259 1.1 jtc state = *--statep;
260 1.1 jtc *wp++ = CQUOTE;
261 1.1 jtc } else
262 1.1 jtc *wp++ = QCHAR, *wp++ = c;
263 1.1 jtc break;
264 1.1 jtc
265 1.1 jtc case SDQUOTE:
266 1.1 jtc if (c == '"') {
267 1.1 jtc state = *--statep;
268 1.1 jtc *wp++ = CQUOTE;
269 1.1 jtc } else
270 1.1 jtc goto Subst;
271 1.1 jtc break;
272 1.1 jtc
273 1.1 jtc case SPAREN: /* $( .. ) */
274 1.1 jtc /* todo: deal with $(...) quoting properly
275 1.1 jtc * kludge to partly fake quoting inside $(..): doesn't
276 1.1 jtc * really work because nested $(..) or ${..} inside
277 1.1 jtc * double quotes aren't dealt with.
278 1.1 jtc */
279 1.1 jtc switch (csstate) {
280 1.1 jtc case 0: /* normal */
281 1.1 jtc switch (c) {
282 1.1 jtc case '(':
283 1.1 jtc nparen++;
284 1.1 jtc break;
285 1.1 jtc case ')':
286 1.1 jtc nparen--;
287 1.1 jtc break;
288 1.1 jtc case '\\':
289 1.1 jtc csstate = 1;
290 1.1 jtc break;
291 1.1 jtc case '"':
292 1.1 jtc csstate = 2;
293 1.1 jtc break;
294 1.1 jtc case '\'':
295 1.1 jtc csstate = 4;
296 1.1 jtc break;
297 1.1 jtc }
298 1.1 jtc break;
299 1.1 jtc
300 1.1 jtc case 1: /* backslash in normal mode */
301 1.1 jtc case 3: /* backslash in double quotes */
302 1.1 jtc --csstate;
303 1.1 jtc break;
304 1.1 jtc
305 1.1 jtc case 2: /* double quotes */
306 1.1 jtc if (c == '"')
307 1.1 jtc csstate = 0;
308 1.1 jtc else if (c == '\\')
309 1.1 jtc csstate = 3;
310 1.1 jtc break;
311 1.1 jtc
312 1.1 jtc case 4: /* single quotes */
313 1.1 jtc if (c == '\'')
314 1.1 jtc csstate = 0;
315 1.1 jtc break;
316 1.1 jtc }
317 1.1 jtc if (nparen == 0) {
318 1.1 jtc state = *--statep;
319 1.1 jtc *wp++ = 0; /* end of COMSUB */
320 1.1 jtc } else
321 1.1 jtc *wp++ = c;
322 1.1 jtc break;
323 1.1 jtc
324 1.1 jtc case SDDPAREN: /* $(( .. )) */
325 1.1 jtc /* todo: deal with $((...); (...)) properly */
326 1.1 jtc /* XXX should nest using existing state machine
327 1.1 jtc * (embed "..", $(...), etc.) */
328 1.1 jtc if (c == '(')
329 1.1 jtc nparen++;
330 1.1 jtc else if (c == ')') {
331 1.1 jtc nparen--;
332 1.1 jtc if (nparen == 1) {
333 1.1 jtc /*(*/
334 1.1 jtc if ((c2 = getsc()) == ')') {
335 1.1 jtc state = *--statep;
336 1.1 jtc *wp++ = 0; /* end of EXPRSUB */
337 1.1 jtc break;
338 1.1 jtc } else {
339 1.1 jtc ungetsc(c2);
340 1.1 jtc /* mismatched parenthesis -
341 1.1 jtc * assume we were really
342 1.1 jtc * parsing a $(..) expression
343 1.1 jtc */
344 1.1 jtc memmove(ddparen_start + 1,
345 1.1 jtc ddparen_start,
346 1.1 jtc wp - ddparen_start);
347 1.1 jtc *ddparen_start++ = COMSUB;
348 1.1 jtc *ddparen_start = '('; /*)*/
349 1.1 jtc wp++;
350 1.1 jtc csstate = 0;
351 1.1 jtc *statep = state = SPAREN;
352 1.1 jtc }
353 1.1 jtc }
354 1.1 jtc }
355 1.1 jtc *wp++ = c;
356 1.1 jtc break;
357 1.1 jtc
358 1.1 jtc case SBRACE:
359 1.1 jtc /*{*/
360 1.1 jtc if (c == '}') {
361 1.1 jtc state = *--statep;
362 1.1 jtc *wp++ = CSUBST;
363 1.1 jtc } else
364 1.1 jtc goto Sbase1;
365 1.1 jtc break;
366 1.1 jtc
367 1.1 jtc case STBRACE:
368 1.1 jtc /* same as SBRACE, except | is saved as SPAT and
369 1.1 jtc * CPAT is added at the end.
370 1.1 jtc */
371 1.1 jtc /*{*/
372 1.1 jtc if (c == '}') {
373 1.1 jtc state = *--statep;
374 1.1 jtc *wp++ = CPAT;
375 1.1 jtc *wp++ = CSUBST;
376 1.1 jtc } else if (c == '|') {
377 1.1 jtc *wp++ = SPAT;
378 1.1 jtc } else
379 1.1 jtc goto Sbase1;
380 1.1 jtc break;
381 1.1 jtc
382 1.1 jtc case SBQUOTE:
383 1.1 jtc if (c == '`') {
384 1.1 jtc *wp++ = 0;
385 1.1 jtc state = *--statep;
386 1.1 jtc } else if (c == '\\') {
387 1.1 jtc switch (c = getsc()) {
388 1.1 jtc case '\n':
389 1.1 jtc break;
390 1.1 jtc case '\\':
391 1.1 jtc case '$': case '`':
392 1.1 jtc *wp++ = c;
393 1.1 jtc break;
394 1.1 jtc case '"':
395 1.1 jtc if (indquotes) {
396 1.1 jtc *wp++ = c;
397 1.1 jtc break;
398 1.1 jtc }
399 1.1 jtc /* fall through.. */
400 1.1 jtc default:
401 1.1 jtc *wp++ = '\\';
402 1.1 jtc *wp++ = c;
403 1.1 jtc break;
404 1.1 jtc }
405 1.1 jtc } else
406 1.1 jtc *wp++ = c;
407 1.1 jtc break;
408 1.1 jtc
409 1.1 jtc case SWORD: /* ONEWORD */
410 1.1 jtc goto Subst;
411 1.1 jtc
412 1.1 jtc case SDPAREN: /* LETEXPR: (( ... )) */
413 1.1 jtc /*(*/
414 1.1 jtc if (c == ')') {
415 1.1 jtc if (ndparen > 0)
416 1.1 jtc --ndparen;
417 1.1 jtc /*(*/
418 1.1 jtc else if ((c2 = getsc()) == ')') {
419 1.1 jtc c = 0;
420 1.1 jtc *wp++ = CQUOTE;
421 1.1 jtc goto Done;
422 1.1 jtc } else
423 1.1 jtc ungetsc(c2);
424 1.1 jtc } else if (c == '(')
425 1.1 jtc /* parenthesis inside quotes and backslashes
426 1.1 jtc * are lost, but at&t ksh doesn't count them
427 1.1 jtc * either
428 1.1 jtc */
429 1.1 jtc ++ndparen;
430 1.1 jtc goto Sbase2;
431 1.1 jtc
432 1.1 jtc case SHEREDELIM: /* <<,<<- delimiter */
433 1.1 jtc /* XXX chuck this state (and the next) - use
434 1.1 jtc * the existing states ($ and \`..` should be
435 1.1 jtc * stripped of their specialness after the
436 1.1 jtc * fact).
437 1.1 jtc */
438 1.1 jtc /* here delimiters need a special case since
439 1.1 jtc * $ and `..` are not to be treated specially
440 1.1 jtc */
441 1.1 jtc if (c == '\\') {
442 1.1 jtc c = getsc();
443 1.1 jtc if (c != '\n') {
444 1.1 jtc *wp++ = QCHAR;
445 1.1 jtc *wp++ = c;
446 1.1 jtc }
447 1.1 jtc } else if (c == '\'') {
448 1.1 jtc *++statep = state = SSQUOTE;
449 1.1 jtc *wp++ = OQUOTE;
450 1.1 jtc } else if (c == '"') {
451 1.1 jtc state = SHEREDQUOTE;
452 1.1 jtc *wp++ = OQUOTE;
453 1.1 jtc } else {
454 1.1 jtc *wp++ = CHAR;
455 1.1 jtc *wp++ = c;
456 1.1 jtc }
457 1.1 jtc break;
458 1.1 jtc
459 1.1 jtc case SHEREDQUOTE: /* " in <<,<<- delimiter */
460 1.1 jtc if (c == '"') {
461 1.1 jtc *wp++ = CQUOTE;
462 1.1 jtc state = SHEREDELIM;
463 1.1 jtc } else {
464 1.1 jtc if (c == '\\' && (c = getsc()) == '\n')
465 1.1 jtc break;
466 1.1 jtc *wp++ = CHAR;
467 1.1 jtc *wp++ = c;
468 1.1 jtc }
469 1.1 jtc break;
470 1.1 jtc
471 1.1 jtc case SPATTERN: /* in *(...|...) pattern (*+?@!) */
472 1.1 jtc if ( /*(*/ c == ')') {
473 1.1 jtc *wp++ = CPAT;
474 1.1 jtc state = *--statep;
475 1.1 jtc } else if (c == '|')
476 1.1 jtc *wp++ = SPAT;
477 1.1 jtc else
478 1.1 jtc goto Sbase1;
479 1.1 jtc break;
480 1.1 jtc }
481 1.1 jtc }
482 1.1 jtc Done:
483 1.1 jtc Xcheck(ws, wp);
484 1.1 jtc if (state != istate)
485 1.1 jtc yyerror("no closing quote\n");
486 1.1 jtc
487 1.1 jtc /* This done to avoid tests for SHEREDELIM wherever SBASE tested */
488 1.1 jtc if (state == SHEREDELIM)
489 1.1 jtc state = SBASE;
490 1.1 jtc
491 1.1 jtc if ((c == '<' || c == '>') && state == SBASE) {
492 1.1 jtc char *cp = Xstring(ws, wp);
493 1.1 jtc if (Xlength(ws, wp) == 2 && cp[0] == CHAR && digit(cp[1])) {
494 1.1 jtc wp = cp; /* throw away word */
495 1.1 jtc c2/*unit*/ = cp[1] - '0';
496 1.1 jtc } else
497 1.1 jtc c2/*unit*/ = c == '>'; /* 0 for <, 1 for > */
498 1.1 jtc }
499 1.1 jtc
500 1.1 jtc if (wp == Xstring(ws, wp) && state == SBASE) {
501 1.1 jtc Xfree(ws, wp); /* free word */
502 1.1 jtc /* no word, process LEX1 character */
503 1.1 jtc switch (c) {
504 1.1 jtc default:
505 1.1 jtc return c;
506 1.1 jtc
507 1.1 jtc case '|':
508 1.1 jtc case '&':
509 1.1 jtc case ';':
510 1.1 jtc if ((c2 = getsc()) == c)
511 1.1 jtc c = (c == ';') ? BREAK :
512 1.1 jtc (c == '|') ? LOGOR :
513 1.1 jtc (c == '&') ? LOGAND :
514 1.1 jtc YYERRCODE;
515 1.1 jtc #ifdef KSH
516 1.1 jtc else if (c == '|' && c2 == '&')
517 1.1 jtc c = COPROC;
518 1.1 jtc #endif /* KSH */
519 1.1 jtc else
520 1.1 jtc ungetsc(c2);
521 1.1 jtc return c;
522 1.1 jtc
523 1.1 jtc case '>':
524 1.1 jtc case '<': {
525 1.1 jtc register struct ioword *iop;
526 1.1 jtc
527 1.1 jtc iop = (struct ioword *) alloc(sizeof(*iop), ATEMP);
528 1.1 jtc iop->unit = c2/*unit*/;
529 1.1 jtc
530 1.1 jtc c2 = getsc();
531 1.1 jtc /* <<, >>, <> are ok, >< is not */
532 1.1 jtc if (c == c2 || (c == '<' && c2 == '>')) {
533 1.1 jtc iop->flag = c == c2 ?
534 1.1 jtc (c == '>' ? IOCAT : IOHERE) : IORDWR;
535 1.1 jtc if (iop->flag == IOHERE)
536 1.1 jtc if (getsc() == '-')
537 1.1 jtc iop->flag |= IOSKIP;
538 1.1 jtc else
539 1.1 jtc ungetsc(c2);
540 1.1 jtc } else if (c2 == '&')
541 1.1 jtc iop->flag = IODUP | (c == '<' ? IORDUP : 0);
542 1.1 jtc else {
543 1.1 jtc iop->flag = c == '>' ? IOWRITE : IOREAD;
544 1.1 jtc if (c == '>' && c2 == '|')
545 1.1 jtc iop->flag |= IOCLOB;
546 1.1 jtc else
547 1.1 jtc ungetsc(c2);
548 1.1 jtc }
549 1.1 jtc
550 1.1 jtc iop->name = (char *) 0;
551 1.1 jtc iop->delim = (char *) 0;
552 1.1 jtc yylval.iop = iop;
553 1.1 jtc return REDIR;
554 1.1 jtc }
555 1.1 jtc case '\n':
556 1.1 jtc gethere();
557 1.1 jtc if (cf & CONTIN)
558 1.1 jtc goto Again;
559 1.1 jtc return c;
560 1.1 jtc
561 1.1 jtc case '(': /*)*/
562 1.1 jtc if ((c2 = getsc()) == '(') /*)*/
563 1.1 jtc c = MDPAREN;
564 1.1 jtc else
565 1.1 jtc ungetsc(c2);
566 1.1 jtc return c;
567 1.1 jtc /*(*/
568 1.1 jtc case ')':
569 1.1 jtc return c;
570 1.1 jtc }
571 1.1 jtc }
572 1.1 jtc
573 1.1 jtc *wp++ = EOS; /* terminate word */
574 1.1 jtc yylval.cp = Xclose(ws, wp);
575 1.1 jtc if (state == SWORD || state == SDPAREN) /* ONEWORD? */
576 1.1 jtc return LWORD;
577 1.1 jtc ungetsc(c); /* unget terminator */
578 1.1 jtc
579 1.1 jtc /* copy word to unprefixed string ident */
580 1.1 jtc for (sp = yylval.cp, dp = ident; dp < ident+IDENT && (c = *sp++) == CHAR; )
581 1.1 jtc *dp++ = *sp++;
582 1.1 jtc /* Make sure the ident array stays '\0' paded */
583 1.1 jtc memset(dp, 0, (ident+IDENT) - dp + 1);
584 1.1 jtc if (c != EOS)
585 1.1 jtc *ident = '\0'; /* word is not unquoted */
586 1.1 jtc
587 1.1 jtc if (*ident != '\0' && (cf&(KEYWORD|ALIAS))) {
588 1.1 jtc struct tbl *p;
589 1.1 jtc int h = hash(ident);
590 1.1 jtc
591 1.1 jtc /* { */
592 1.1 jtc if ((cf & KEYWORD) && (p = tsearch(&keywords, ident, h))
593 1.1 jtc && (!(cf & ESACONLY) || p->val.i == ESAC || p->val.i == '}'))
594 1.1 jtc {
595 1.1 jtc afree(yylval.cp, ATEMP);
596 1.1 jtc return p->val.i;
597 1.1 jtc }
598 1.1 jtc if ((cf & ALIAS) && (p = tsearch(&aliases, ident, h))
599 1.1 jtc && (p->flag & ISSET))
600 1.1 jtc {
601 1.1 jtc register Source *s;
602 1.1 jtc
603 1.1 jtc for (s = source; s->type == SALIAS; s = s->next)
604 1.1 jtc if (s->u.tblp == p)
605 1.1 jtc return LWORD;
606 1.1 jtc /* push alias expansion */
607 1.1 jtc s = pushs(SALIAS, source->areap);
608 1.1 jtc s->start = s->str = p->val.s;
609 1.1 jtc s->u.tblp = p;
610 1.1 jtc s->next = source;
611 1.1 jtc source = s;
612 1.1 jtc afree(yylval.cp, ATEMP);
613 1.1 jtc goto Again;
614 1.1 jtc }
615 1.1 jtc }
616 1.1 jtc
617 1.1 jtc return LWORD;
618 1.1 jtc }
619 1.1 jtc
620 1.1 jtc static void
621 1.1 jtc gethere()
622 1.1 jtc {
623 1.1 jtc register struct ioword **p;
624 1.1 jtc
625 1.1 jtc for (p = heres; p < herep; p++)
626 1.1 jtc readhere(*p);
627 1.1 jtc herep = heres;
628 1.1 jtc }
629 1.1 jtc
630 1.1 jtc /*
631 1.1 jtc * read "<<word" text into temp file
632 1.1 jtc */
633 1.1 jtc
634 1.1 jtc static void
635 1.1 jtc readhere(iop)
636 1.1 jtc register struct ioword *iop;
637 1.1 jtc {
638 1.1 jtc struct shf *volatile shf;
639 1.1 jtc struct temp *h;
640 1.1 jtc register int c;
641 1.1 jtc char *volatile eof;
642 1.1 jtc char *eofp;
643 1.1 jtc int skiptabs, bn;
644 1.1 jtc int i;
645 1.1 jtc
646 1.1 jtc eof = evalstr(iop->delim, 0);
647 1.1 jtc
648 1.1 jtc if (e->flags & EF_FUNC_PARSE) {
649 1.1 jtc h = maketemp(APERM);
650 1.1 jtc h->next = func_heredocs;
651 1.1 jtc func_heredocs = h;
652 1.1 jtc } else {
653 1.1 jtc h = maketemp(ATEMP);
654 1.1 jtc h->next = e->temps;
655 1.1 jtc e->temps = h;
656 1.1 jtc }
657 1.1 jtc iop->name = h->name;
658 1.1 jtc if (!(shf = h->shf))
659 1.1 jtc yyerror("cannot create temporary file %s - %s\n",
660 1.1 jtc h->name, strerror(errno));
661 1.1 jtc
662 1.1 jtc newenv(E_ERRH);
663 1.1 jtc i = ksh_sigsetjmp(e->jbuf, 0);
664 1.1 jtc if (i) {
665 1.1 jtc quitenv();
666 1.1 jtc shf_close(shf);
667 1.1 jtc unwind(i);
668 1.1 jtc }
669 1.1 jtc
670 1.1 jtc bn = iop->flag & IOEVAL;
671 1.1 jtc for (;;) {
672 1.1 jtc eofp = eof;
673 1.1 jtc skiptabs = iop->flag & IOSKIP;
674 1.1 jtc while ((c = (bn ? getsc_bn() : getsc())) != 0) {
675 1.1 jtc if (skiptabs) {
676 1.1 jtc if (c == '\t')
677 1.1 jtc continue;
678 1.1 jtc skiptabs = 0;
679 1.1 jtc }
680 1.1 jtc if (c != *eofp)
681 1.1 jtc break;
682 1.1 jtc eofp++;
683 1.1 jtc }
684 1.1 jtc /* Allow EOF here so commands with out trailing newlines
685 1.1 jtc * will work (eg, ksh -c '...', $(...), etc).
686 1.1 jtc */
687 1.1 jtc if (*eofp == '\0' && (c == 0 || c == '\n'))
688 1.1 jtc break;
689 1.1 jtc ungetsc(c);
690 1.1 jtc shf_write(eof, eofp - eof, shf);
691 1.1 jtc while ((c = (bn ? getsc_bn() : getsc())) != '\n') {
692 1.1 jtc if (c == 0)
693 1.1 jtc yyerror("here document `%s' unclosed\n", eof);
694 1.1 jtc shf_putc(c, shf);
695 1.1 jtc }
696 1.1 jtc shf_putc(c, shf);
697 1.1 jtc }
698 1.1 jtc shf_flush(shf);
699 1.1 jtc if (shf_error(shf))
700 1.1 jtc yyerror("error saving here document `%s': %s\n",
701 1.1 jtc eof, strerror(shf_errno(shf)));
702 1.1 jtc /*XXX add similar checks for write errors everywhere */
703 1.1 jtc quitenv();
704 1.1 jtc shf_close(shf);
705 1.1 jtc }
706 1.1 jtc
707 1.1 jtc void
708 1.1 jtc #ifdef HAVE_PROTOTYPES
709 1.1 jtc yyerror(const char *fmt, ...)
710 1.1 jtc #else
711 1.1 jtc yyerror(fmt, va_alist)
712 1.1 jtc const char *fmt;
713 1.1 jtc va_dcl
714 1.1 jtc #endif
715 1.1 jtc {
716 1.1 jtc va_list va;
717 1.1 jtc
718 1.1 jtc yynerrs++;
719 1.1 jtc /* pop aliases and re-reads */
720 1.1 jtc while (source->type == SALIAS || source->type == SREREAD)
721 1.1 jtc source = source->next;
722 1.1 jtc source->str = null; /* zap pending input */
723 1.1 jtc
724 1.1 jtc error_prefix(TRUE);
725 1.1 jtc SH_VA_START(va, fmt);
726 1.1 jtc shf_vfprintf(shl_out, fmt, va);
727 1.1 jtc va_end(va);
728 1.1 jtc errorf(null);
729 1.1 jtc }
730 1.1 jtc
731 1.1 jtc /*
732 1.1 jtc * input for yylex with alias expansion
733 1.1 jtc */
734 1.1 jtc
735 1.1 jtc Source *
736 1.1 jtc pushs(type, areap)
737 1.1 jtc int type;
738 1.1 jtc Area *areap;
739 1.1 jtc {
740 1.1 jtc register Source *s;
741 1.1 jtc
742 1.1 jtc s = (Source *) alloc(sizeof(Source), areap);
743 1.1 jtc s->type = type;
744 1.1 jtc s->str = null;
745 1.1 jtc s->start = NULL;
746 1.1 jtc s->line = 0;
747 1.1 jtc s->errline = 0;
748 1.1 jtc s->file = NULL;
749 1.1 jtc s->flags = 0;
750 1.1 jtc s->next = NULL;
751 1.1 jtc s->areap = areap;
752 1.1 jtc if (type == SFILE || type == SSTDIN) {
753 1.1 jtc char *dummy;
754 1.1 jtc Xinit(s->xs, dummy, 256, s->areap);
755 1.1 jtc } else
756 1.1 jtc memset(&s->xs, 0, sizeof(s->xs));
757 1.1 jtc return s;
758 1.1 jtc }
759 1.1 jtc
760 1.1 jtc static int
761 1.1 jtc getsc_()
762 1.1 jtc {
763 1.1 jtc register Source *s = source;
764 1.1 jtc register int c;
765 1.1 jtc
766 1.1 jtc while ((c = *s->str++) == 0) {
767 1.1 jtc s->str = NULL; /* return 0 for EOF by default */
768 1.1 jtc switch (s->type) {
769 1.1 jtc case SEOF:
770 1.1 jtc s->str = null;
771 1.1 jtc return 0;
772 1.1 jtc
773 1.1 jtc case SSTDIN:
774 1.1 jtc case SFILE:
775 1.1 jtc getsc_line(s);
776 1.1 jtc break;
777 1.1 jtc
778 1.1 jtc case SWSTR:
779 1.1 jtc break;
780 1.1 jtc
781 1.1 jtc case SSTRING:
782 1.1 jtc break;
783 1.1 jtc
784 1.1 jtc case SWORDS:
785 1.1 jtc s->start = s->str = *s->u.strv++;
786 1.1 jtc s->type = SWORDSEP;
787 1.1 jtc break;
788 1.1 jtc
789 1.1 jtc case SWORDSEP:
790 1.1 jtc if (*s->u.strv == NULL) {
791 1.1 jtc s->start = s->str = newline;
792 1.1 jtc s->type = SEOF;
793 1.1 jtc } else {
794 1.1 jtc s->start = s->str = space;
795 1.1 jtc s->type = SWORDS;
796 1.1 jtc }
797 1.1 jtc break;
798 1.1 jtc
799 1.1 jtc case SALIAS:
800 1.1 jtc if (s->flags & SF_ALIASEND) {
801 1.1 jtc /* pass on an unused SF_ALIAS flag */
802 1.1 jtc source = s->next;
803 1.1 jtc source->flags |= s->flags & SF_ALIAS;
804 1.1 jtc s = source;
805 1.1 jtc } else if (*s->u.tblp->val.s
806 1.1 jtc && isspace(strchr(s->u.tblp->val.s, 0)[-1]))
807 1.1 jtc {
808 1.1 jtc source = s = s->next; /* pop source stack */
809 1.1 jtc s->flags |= SF_ALIAS;
810 1.1 jtc } else {
811 1.1 jtc /* put a fake space at the end of the alias.
812 1.1 jtc * This keeps the current alias in the source
813 1.1 jtc * list so recursive aliases can be detected.
814 1.1 jtc * The addition of a space after an alias
815 1.1 jtc * never affects anything (I think).
816 1.1 jtc */
817 1.1 jtc s->flags |= SF_ALIASEND;
818 1.1 jtc s->start = s->str = space;
819 1.1 jtc }
820 1.1 jtc continue;
821 1.1 jtc
822 1.1 jtc case SREREAD:
823 1.1 jtc if (s->start != s->u.ugbuf) /* yuck */
824 1.1 jtc afree(s->u.freeme, ATEMP);
825 1.1 jtc source = s = s->next;
826 1.1 jtc continue;
827 1.1 jtc }
828 1.1 jtc if (s->str == NULL) {
829 1.1 jtc s->type = SEOF;
830 1.1 jtc s->start = s->str = null;
831 1.1 jtc return '\0';
832 1.1 jtc }
833 1.1 jtc if (s->flags & SF_ECHO) {
834 1.1 jtc shf_puts(s->str, shl_out);
835 1.1 jtc shf_flush(shl_out);
836 1.1 jtc }
837 1.1 jtc }
838 1.1 jtc return c;
839 1.1 jtc }
840 1.1 jtc
841 1.1 jtc static void
842 1.1 jtc getsc_line(s)
843 1.1 jtc Source *s;
844 1.1 jtc {
845 1.1 jtc char *xp = Xstring(s->xs, xp);
846 1.1 jtc int interactive = Flag(FTALKING) && s->type == SSTDIN;
847 1.1 jtc int have_tty = interactive && (s->flags & SF_TTY);
848 1.1 jtc
849 1.1 jtc /* Done here to ensure nothing odd happens when a timeout occurs */
850 1.1 jtc XcheckN(s->xs, xp, LINE);
851 1.1 jtc *xp = '\0';
852 1.1 jtc s->start = s->str = xp;
853 1.1 jtc
854 1.1 jtc #ifdef KSH
855 1.1 jtc if (have_tty && ksh_tmout) {
856 1.1 jtc ksh_tmout_state = TMOUT_READING;
857 1.1 jtc alarm(ksh_tmout);
858 1.1 jtc }
859 1.1 jtc #endif /* KSH */
860 1.1 jtc #ifdef EDIT
861 1.1 jtc if (have_tty && (0
862 1.1 jtc # ifdef VI
863 1.1 jtc || Flag(FVI)
864 1.1 jtc # endif /* VI */
865 1.1 jtc # ifdef EMACS
866 1.1 jtc || Flag(FEMACS) || Flag(FGMACS)
867 1.1 jtc # endif /* EMACS */
868 1.1 jtc ))
869 1.1 jtc {
870 1.1 jtc int nread;
871 1.1 jtc
872 1.1 jtc nread = x_read(xp, LINE);
873 1.1 jtc if (nread < 0) /* read error */
874 1.1 jtc nread = 0;
875 1.1 jtc xp[nread] = '\0';
876 1.1 jtc xp += nread;
877 1.1 jtc }
878 1.1 jtc else
879 1.1 jtc #endif /* EDIT */
880 1.1 jtc {
881 1.1 jtc if (interactive) {
882 1.1 jtc pprompt(prompt, 0);
883 1.1 jtc #ifdef OS2
884 1.1 jtc setmode (0, O_TEXT);
885 1.1 jtc #endif /* OS2 */
886 1.1 jtc } else
887 1.1 jtc s->line++;
888 1.1 jtc
889 1.1 jtc while (1) {
890 1.1 jtc char *p = shf_getse(xp, Xnleft(s->xs, xp), s->u.shf);
891 1.1 jtc
892 1.1 jtc if (!p && shf_error(s->u.shf)
893 1.1 jtc && shf_errno(s->u.shf) == EINTR)
894 1.1 jtc {
895 1.1 jtc shf_clearerr(s->u.shf);
896 1.1 jtc if (trap)
897 1.1 jtc runtraps(0);
898 1.1 jtc continue;
899 1.1 jtc }
900 1.1 jtc if (!p || (xp = p, xp[-1] == '\n'))
901 1.1 jtc break;
902 1.1 jtc /* double buffer size */
903 1.1 jtc xp++; /* move past null so doubling works... */
904 1.1 jtc XcheckN(s->xs, xp, Xlength(s->xs, xp));
905 1.1 jtc xp--; /* ...and move back again */
906 1.1 jtc }
907 1.1 jtc #ifdef OS2
908 1.1 jtc setmode(0, O_BINARY);
909 1.1 jtc #endif /* OS2 */
910 1.1 jtc /* flush any unwanted input so other programs/builtins
911 1.1 jtc * can read it. Not very optimal, but less error prone
912 1.1 jtc * than flushing else where, dealing with redirections,
913 1.1 jtc * etc..
914 1.1 jtc * todo: reduce size of shf buffer (~128?) if SSTDIN
915 1.1 jtc */
916 1.1 jtc if (s->type == SSTDIN)
917 1.1 jtc shf_flush(s->u.shf);
918 1.1 jtc }
919 1.1 jtc /* XXX: temporary kludge to restore source after a
920 1.1 jtc * trap may have been executed.
921 1.1 jtc */
922 1.1 jtc source = s;
923 1.1 jtc #ifdef KSH
924 1.1 jtc if (have_tty && ksh_tmout)
925 1.1 jtc {
926 1.1 jtc ksh_tmout_state = TMOUT_EXECUTING;
927 1.1 jtc alarm(0);
928 1.1 jtc }
929 1.1 jtc #endif /* KSH */
930 1.1 jtc s->start = s->str = Xstring(s->xs, xp);
931 1.1 jtc strip_nuls(Xstring(s->xs, xp), Xlength(s->xs, xp));
932 1.1 jtc /* Note: if input is all nulls, this is not eof */
933 1.1 jtc if (Xlength(s->xs, xp) == 0) { /* EOF */
934 1.1 jtc if (s->type == SFILE)
935 1.1 jtc shf_fdclose(s->u.shf);
936 1.1 jtc s->str = NULL;
937 1.1 jtc } else if (interactive) {
938 1.1 jtc #ifdef HISTORY
939 1.1 jtc char *p = Xstring(s->xs, xp);
940 1.1 jtc if (cur_prompt == PS1)
941 1.1 jtc while (*p && ctype(*p, C_IFS) && ctype(*p, C_IFSWS))
942 1.1 jtc p++;
943 1.1 jtc if (*p) {
944 1.1 jtc # ifdef EASY_HISTORY
945 1.1 jtc if (cur_prompt == PS2)
946 1.1 jtc histappend(Xstring(s->xs, xp), 1);
947 1.1 jtc else
948 1.1 jtc # endif /* EASY_HISTORY */
949 1.1 jtc {
950 1.1 jtc s->line++;
951 1.1 jtc histsave(s->line, s->str, 1);
952 1.1 jtc }
953 1.1 jtc }
954 1.1 jtc #endif /* HISTORY */
955 1.1 jtc }
956 1.1 jtc if (interactive)
957 1.1 jtc set_prompt(PS2, (Source *) 0);
958 1.1 jtc }
959 1.1 jtc
960 1.1 jtc void
961 1.1 jtc set_prompt(to, s)
962 1.1 jtc int to;
963 1.1 jtc Source *s;
964 1.1 jtc {
965 1.1 jtc cur_prompt = to;
966 1.1 jtc
967 1.1 jtc switch (to) {
968 1.1 jtc case PS1: /* command */
969 1.1 jtc #ifdef KSH
970 1.1 jtc /* Substitute ! and !! here, before substitutions are done
971 1.1 jtc * so ! in expanded variables are not expanded.
972 1.1 jtc * NOTE: this is not what at&t ksh does (it does it after
973 1.1 jtc * substitutions, POSIX doesn't say which is to be done.
974 1.1 jtc */
975 1.1 jtc {
976 1.1 jtc struct shf *shf;
977 1.1 jtc char *ps1;
978 1.1 jtc Area *saved_atemp;
979 1.1 jtc
980 1.1 jtc ps1 = str_val(global("PS1"));
981 1.1 jtc shf = shf_sopen((char *) 0, strlen(ps1) * 2,
982 1.1 jtc SHF_WR | SHF_DYNAMIC, (struct shf *) 0);
983 1.1 jtc while (*ps1) {
984 1.1 jtc if (*ps1 != '!' || *++ps1 == '!')
985 1.1 jtc shf_putchar(*ps1++, shf);
986 1.1 jtc else
987 1.1 jtc shf_fprintf(shf, "%d",
988 1.1 jtc s ? s->line + 1 : 0);
989 1.1 jtc }
990 1.1 jtc ps1 = shf_sclose(shf);
991 1.1 jtc saved_atemp = ATEMP;
992 1.1 jtc newenv(E_ERRH);
993 1.1 jtc if (ksh_sigsetjmp(e->jbuf, 0)) {
994 1.1 jtc prompt = safe_prompt;
995 1.1 jtc /* Don't print an error - assume it has already
996 1.1 jtc * been printed. Reason is we may have forked
997 1.1 jtc * to run a command and the child may be
998 1.1 jtc * unwinding its stack through this code as it
999 1.1 jtc * exits.
1000 1.1 jtc */
1001 1.1 jtc } else
1002 1.1 jtc prompt = str_save(substitute(ps1, 0),
1003 1.1 jtc saved_atemp);
1004 1.1 jtc quitenv();
1005 1.1 jtc }
1006 1.1 jtc #else /* KSH */
1007 1.1 jtc prompt = str_val(global("PS1"));
1008 1.1 jtc #endif /* KSH */
1009 1.1 jtc break;
1010 1.1 jtc
1011 1.1 jtc case PS2: /* command continuation */
1012 1.1 jtc prompt = str_val(global("PS2"));
1013 1.1 jtc break;
1014 1.1 jtc }
1015 1.1 jtc }
1016 1.1 jtc
1017 1.1 jtc /* See also related routine, promptlen() in edit.c */
1018 1.1 jtc void
1019 1.1 jtc pprompt(cp, ntruncate)
1020 1.1 jtc const char *cp;
1021 1.1 jtc int ntruncate;
1022 1.1 jtc {
1023 1.1 jtc #if 0
1024 1.1 jtc char nbuf[32];
1025 1.1 jtc int c;
1026 1.1 jtc
1027 1.1 jtc while (*cp != 0) {
1028 1.1 jtc if (*cp != '!')
1029 1.1 jtc c = *cp++;
1030 1.1 jtc else if (*++cp == '!')
1031 1.1 jtc c = *cp++;
1032 1.1 jtc else {
1033 1.1 jtc int len;
1034 1.1 jtc char *p;
1035 1.1 jtc
1036 1.1 jtc shf_snprintf(p = nbuf, sizeof(nbuf), "%d",
1037 1.1 jtc source->line + 1);
1038 1.1 jtc len = strlen(nbuf);
1039 1.1 jtc if (ntruncate) {
1040 1.1 jtc if (ntruncate >= len) {
1041 1.1 jtc ntruncate -= len;
1042 1.1 jtc continue;
1043 1.1 jtc }
1044 1.1 jtc p += ntruncate;
1045 1.1 jtc len -= ntruncate;
1046 1.1 jtc ntruncate = 0;
1047 1.1 jtc }
1048 1.1 jtc shf_write(p, len, shl_out);
1049 1.1 jtc continue;
1050 1.1 jtc }
1051 1.1 jtc if (ntruncate)
1052 1.1 jtc --ntruncate;
1053 1.1 jtc else
1054 1.1 jtc shf_putc(c, shl_out);
1055 1.1 jtc }
1056 1.1 jtc #endif /* 0 */
1057 1.1 jtc if (ntruncate)
1058 1.1 jtc shellf("%.*s", ntruncate, cp);
1059 1.1 jtc else {
1060 1.1 jtc shf_puts(cp, shl_out);
1061 1.1 jtc shf_flush(shl_out);
1062 1.1 jtc }
1063 1.1 jtc }
1064 1.1 jtc
1065 1.1 jtc /* Read the variable part of a ${...} expression (ie, up to but not including
1066 1.1 jtc * the :[-+?=#%] or close-brace.
1067 1.1 jtc */
1068 1.1 jtc static char *
1069 1.1 jtc get_brace_var(wsp, wp)
1070 1.1 jtc XString *wsp;
1071 1.1 jtc char *wp;
1072 1.1 jtc {
1073 1.1 jtc enum parse_state {
1074 1.1 jtc PS_INITIAL, PS_SAW_HASH, PS_IDENT,
1075 1.1 jtc PS_NUMBER, PS_VAR1, PS_END
1076 1.1 jtc }
1077 1.1 jtc state;
1078 1.1 jtc char c;
1079 1.1 jtc
1080 1.1 jtc state = PS_INITIAL;
1081 1.1 jtc while (1) {
1082 1.1 jtc c = getsc_bn();
1083 1.1 jtc /* State machine to figure out where the variable part ends. */
1084 1.1 jtc switch (state) {
1085 1.1 jtc case PS_INITIAL:
1086 1.1 jtc if (c == '#') {
1087 1.1 jtc state = PS_SAW_HASH;
1088 1.1 jtc break;
1089 1.1 jtc }
1090 1.1 jtc /* fall through.. */
1091 1.1 jtc case PS_SAW_HASH:
1092 1.1 jtc if (letter(c))
1093 1.1 jtc state = PS_IDENT;
1094 1.1 jtc else if (digit(c))
1095 1.1 jtc state = PS_NUMBER;
1096 1.1 jtc else if (ctype(c, C_VAR1))
1097 1.1 jtc state = PS_VAR1;
1098 1.1 jtc else
1099 1.1 jtc state = PS_END;
1100 1.1 jtc break;
1101 1.1 jtc case PS_IDENT:
1102 1.1 jtc if (!letnum(c)) {
1103 1.1 jtc state = PS_END;
1104 1.1 jtc if (c == '[') {
1105 1.1 jtc char *tmp, *p;
1106 1.1 jtc
1107 1.1 jtc if (!arraysub(&tmp))
1108 1.1 jtc yyerror("missing ]\n");
1109 1.1 jtc *wp++ = c;
1110 1.1 jtc for (p = tmp; *p; ) {
1111 1.1 jtc Xcheck(*wsp, wp);
1112 1.1 jtc *wp++ = *p++;
1113 1.1 jtc }
1114 1.1 jtc afree(tmp, ATEMP);
1115 1.1 jtc c = getsc();
1116 1.1 jtc }
1117 1.1 jtc }
1118 1.1 jtc break;
1119 1.1 jtc case PS_NUMBER:
1120 1.1 jtc if (!digit(c))
1121 1.1 jtc state = PS_END;
1122 1.1 jtc break;
1123 1.1 jtc case PS_VAR1:
1124 1.1 jtc state = PS_END;
1125 1.1 jtc break;
1126 1.1 jtc case PS_END: /* keep gcc happy */
1127 1.1 jtc break;
1128 1.1 jtc }
1129 1.1 jtc if (state == PS_END) {
1130 1.1 jtc *wp++ = '\0'; /* end of variable part */
1131 1.1 jtc ungetsc(c);
1132 1.1 jtc break;
1133 1.1 jtc }
1134 1.1 jtc Xcheck(*wsp, wp);
1135 1.1 jtc *wp++ = c;
1136 1.1 jtc }
1137 1.1 jtc return wp;
1138 1.1 jtc }
1139 1.1 jtc
1140 1.1 jtc /*
1141 1.1 jtc * Save an array subscript - returns true if matching bracket found, false
1142 1.1 jtc * if eof or newline was found.
1143 1.1 jtc * (Returned string double null terminated)
1144 1.1 jtc */
1145 1.1 jtc static int
1146 1.1 jtc arraysub(strp)
1147 1.1 jtc char **strp;
1148 1.1 jtc {
1149 1.1 jtc XString ws;
1150 1.1 jtc char *wp;
1151 1.1 jtc char c;
1152 1.1 jtc int depth = 1; /* we are just past the initial [ */
1153 1.1 jtc
1154 1.1 jtc Xinit(ws, wp, 32, ATEMP);
1155 1.1 jtc
1156 1.1 jtc do {
1157 1.1 jtc c = getsc_bn();
1158 1.1 jtc Xcheck(ws, wp);
1159 1.1 jtc *wp++ = c;
1160 1.1 jtc if (c == '[')
1161 1.1 jtc depth++;
1162 1.1 jtc else if (c == ']')
1163 1.1 jtc depth--;
1164 1.1 jtc } while (depth > 0 && c && c != '\n');
1165 1.1 jtc
1166 1.1 jtc *wp++ = '\0';
1167 1.1 jtc *strp = Xclose(ws, wp);
1168 1.1 jtc
1169 1.1 jtc return depth == 0 ? 1 : 0;
1170 1.1 jtc }
1171 1.1 jtc
1172 1.1 jtc /* Unget a char: handles case when we are already at the start of the buffer */
1173 1.1 jtc static const char *
1174 1.1 jtc ungetsc_(c)
1175 1.1 jtc int c;
1176 1.1 jtc {
1177 1.1 jtc /* Don't unget eof... */
1178 1.1 jtc if (source->str == null && c == '\0')
1179 1.1 jtc return source->str;
1180 1.1 jtc if (source->str > source->start)
1181 1.1 jtc source->str--;
1182 1.1 jtc else {
1183 1.1 jtc Source *s;
1184 1.1 jtc
1185 1.1 jtc s = pushs(SREREAD, source->areap);
1186 1.1 jtc s->u.ugbuf[0] = c; s->u.ugbuf[1] = '\0';
1187 1.1 jtc s->start = s->str = s->u.ugbuf;
1188 1.1 jtc s->next = source;
1189 1.1 jtc source = s;
1190 1.1 jtc }
1191 1.1 jtc return source->str;
1192 1.1 jtc }
1193 1.1 jtc
1194 1.1 jtc /* Called to get a char that isn't a \newline sequence. */
1195 1.1 jtc static int
1196 1.1 jtc getsc_bn_ ARGS((void))
1197 1.1 jtc {
1198 1.1 jtc int c;
1199 1.1 jtc
1200 1.1 jtc while (1) {
1201 1.1 jtc c = getsc_();
1202 1.1 jtc if (c != '\\')
1203 1.1 jtc return c;
1204 1.1 jtc c = getsc();
1205 1.1 jtc if (c != '\n') {
1206 1.1 jtc ungetsc(c);
1207 1.1 jtc return '\\';
1208 1.1 jtc }
1209 1.1 jtc /* ignore the \newline; get the next char... */
1210 1.1 jtc }
1211 1.1 jtc }
1212