lex.c revision 1.20 1 1.20 lukem /* $NetBSD: lex.c,v 1.20 2001/11/03 13:35:39 lukem Exp $ */
2 1.7 cgd
3 1.1 cgd /*-
4 1.5 mycroft * Copyright (c) 1980, 1991, 1993
5 1.5 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.11 christos #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.7 cgd #if 0
39 1.7 cgd static char sccsid[] = "@(#)lex.c 8.1 (Berkeley) 5/31/93";
40 1.7 cgd #else
41 1.20 lukem __RCSID("$NetBSD: lex.c,v 1.20 2001/11/03 13:35:39 lukem Exp $");
42 1.7 cgd #endif
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.19 wiz #include <sys/ioctl.h>
46 1.1 cgd #include <sys/types.h>
47 1.19 wiz
48 1.1 cgd #include <errno.h>
49 1.1 cgd #include <stdlib.h>
50 1.1 cgd #include <string.h>
51 1.19 wiz #include <termios.h>
52 1.1 cgd #include <unistd.h>
53 1.19 wiz
54 1.1 cgd #if __STDC__
55 1.1 cgd # include <stdarg.h>
56 1.1 cgd #else
57 1.1 cgd # include <varargs.h>
58 1.1 cgd #endif
59 1.1 cgd
60 1.1 cgd #include "csh.h"
61 1.1 cgd #include "extern.h"
62 1.1 cgd
63 1.1 cgd /*
64 1.1 cgd * These lexical routines read input and form lists of words.
65 1.1 cgd * There is some involved processing here, because of the complications
66 1.1 cgd * of input buffering, and especially because of history substitution.
67 1.1 cgd */
68 1.1 cgd
69 1.19 wiz static Char *word(void);
70 1.19 wiz static int getC1(int);
71 1.19 wiz static void getdol(void);
72 1.19 wiz static void getexcl(int);
73 1.19 wiz static struct Hist *findev(Char *, bool);
74 1.19 wiz static void setexclp(Char *);
75 1.19 wiz static int bgetc(void);
76 1.19 wiz static void bfree(void);
77 1.19 wiz static struct wordent *gethent(int);
78 1.19 wiz static int matchs(Char *, Char *);
79 1.19 wiz static int getsel(int *, int *, int);
80 1.19 wiz static struct wordent *getsub(struct wordent *);
81 1.19 wiz static Char *subword(Char *, int, bool *);
82 1.19 wiz static struct wordent *dosub(int, struct wordent *, bool);
83 1.1 cgd
84 1.1 cgd /*
85 1.1 cgd * Peekc is a peek character for getC, peekread for readc.
86 1.1 cgd * There is a subtlety here in many places... history routines
87 1.1 cgd * will read ahead and then insert stuff into the input stream.
88 1.1 cgd * If they push back a character then they must push it behind
89 1.1 cgd * the text substituted by the history substitution. On the other
90 1.1 cgd * hand in several places we need 2 peek characters. To make this
91 1.1 cgd * all work, the history routines read with getC, and make use both
92 1.1 cgd * of ungetC and unreadc. The key observation is that the state
93 1.1 cgd * of getC at the call of a history reference is such that calls
94 1.1 cgd * to getC from the history routines will always yield calls of
95 1.1 cgd * readc, unless this peeking is involved. That is to say that during
96 1.1 cgd * getexcl the variables lap, exclp, and exclnxt are all zero.
97 1.1 cgd *
98 1.1 cgd * Getdol invokes history substitution, hence the extra peek, peekd,
99 1.1 cgd * which it can ungetD to be before history substitutions.
100 1.1 cgd */
101 1.1 cgd static Char peekc = 0, peekd = 0;
102 1.1 cgd static Char peekread = 0;
103 1.1 cgd
104 1.1 cgd /* (Tail of) current word from ! subst */
105 1.1 cgd static Char *exclp = NULL;
106 1.1 cgd
107 1.1 cgd /* The rest of the ! subst words */
108 1.1 cgd static struct wordent *exclnxt = NULL;
109 1.1 cgd
110 1.1 cgd /* Count of remaining words in ! subst */
111 1.1 cgd static int exclc = 0;
112 1.1 cgd
113 1.1 cgd /* "Globp" for alias resubstitution */
114 1.18 simonb Char **alvec, *alvecp;
115 1.5 mycroft int aret = F_SEEK;
116 1.1 cgd
117 1.1 cgd /*
118 1.1 cgd * Labuf implements a general buffer for lookahead during lexical operations.
119 1.1 cgd * Text which is to be placed in the input stream can be stuck here.
120 1.1 cgd * We stick parsed ahead $ constructs during initial input,
121 1.1 cgd * process id's from `$$', and modified variable values (from qualifiers
122 1.1 cgd * during expansion in sh.dol.c) here.
123 1.1 cgd */
124 1.17 christos static Char labuf[BUFSIZE];
125 1.1 cgd
126 1.1 cgd /*
127 1.1 cgd * Lex returns to its caller not only a wordlist (as a "var" parameter)
128 1.1 cgd * but also whether a history substitution occurred. This is used in
129 1.1 cgd * the main (process) routine to determine whether to echo, and also
130 1.1 cgd * when called by the alias routine to determine whether to keep the
131 1.1 cgd * argument list.
132 1.1 cgd */
133 1.1 cgd static bool hadhist = 0;
134 1.1 cgd
135 1.1 cgd /*
136 1.1 cgd * Avoid alias expansion recursion via \!#
137 1.1 cgd */
138 1.1 cgd int hleft;
139 1.1 cgd
140 1.1 cgd static Char getCtmp;
141 1.1 cgd
142 1.19 wiz #define getC(f) ((getCtmp = peekc) ? (peekc = 0, getCtmp) : getC1(f))
143 1.19 wiz #define ungetC(c) peekc = c
144 1.19 wiz #define ungetD(c) peekd = c
145 1.1 cgd
146 1.1 cgd int
147 1.19 wiz lex(struct wordent *hp)
148 1.1 cgd {
149 1.10 tls struct wordent *wdp;
150 1.19 wiz int c;
151 1.1 cgd
152 1.5 mycroft btell(&lineloc);
153 1.1 cgd hp->next = hp->prev = hp;
154 1.1 cgd hp->word = STRNULL;
155 1.5 mycroft hadhist = 0;
156 1.1 cgd do
157 1.1 cgd c = readc(0);
158 1.1 cgd while (c == ' ' || c == '\t');
159 1.1 cgd if (c == HISTSUB && intty)
160 1.1 cgd /* ^lef^rit from tty is short !:s^lef^rit */
161 1.1 cgd getexcl(c);
162 1.1 cgd else
163 1.1 cgd unreadc(c);
164 1.1 cgd wdp = hp;
165 1.1 cgd /*
166 1.1 cgd * The following loop is written so that the links needed by freelex will
167 1.1 cgd * be ready and rarin to go even if it is interrupted.
168 1.1 cgd */
169 1.1 cgd do {
170 1.10 tls struct wordent *new;
171 1.1 cgd
172 1.19 wiz new = (struct wordent *)xmalloc((size_t)sizeof(*wdp));
173 1.1 cgd new->word = 0;
174 1.1 cgd new->prev = wdp;
175 1.1 cgd new->next = hp;
176 1.1 cgd wdp->next = new;
177 1.1 cgd wdp = new;
178 1.1 cgd wdp->word = word();
179 1.1 cgd } while (wdp->word[0] != '\n');
180 1.1 cgd hp->prev = wdp;
181 1.1 cgd return (hadhist);
182 1.1 cgd }
183 1.1 cgd
184 1.1 cgd void
185 1.19 wiz prlex(FILE *fp, struct wordent *sp0)
186 1.1 cgd {
187 1.19 wiz struct wordent *sp;
188 1.1 cgd
189 1.19 wiz sp = sp0->next;
190 1.1 cgd for (;;) {
191 1.19 wiz (void)fprintf(fp, "%s", vis_str(sp->word));
192 1.1 cgd sp = sp->next;
193 1.1 cgd if (sp == sp0)
194 1.1 cgd break;
195 1.1 cgd if (sp->word[0] != '\n')
196 1.5 mycroft (void) fputc(' ', fp);
197 1.1 cgd }
198 1.1 cgd }
199 1.1 cgd
200 1.1 cgd void
201 1.19 wiz copylex(struct wordent *hp, struct wordent *fp)
202 1.1 cgd {
203 1.10 tls struct wordent *wdp;
204 1.1 cgd
205 1.1 cgd wdp = hp;
206 1.1 cgd fp = fp->next;
207 1.1 cgd do {
208 1.10 tls struct wordent *new;
209 1.1 cgd
210 1.19 wiz new = (struct wordent *)xmalloc((size_t)sizeof(*wdp));
211 1.1 cgd new->prev = wdp;
212 1.1 cgd new->next = hp;
213 1.1 cgd wdp->next = new;
214 1.1 cgd wdp = new;
215 1.1 cgd wdp->word = Strsave(fp->word);
216 1.1 cgd fp = fp->next;
217 1.1 cgd } while (wdp->word[0] != '\n');
218 1.1 cgd hp->prev = wdp;
219 1.1 cgd }
220 1.1 cgd
221 1.1 cgd void
222 1.19 wiz freelex(struct wordent *vp)
223 1.1 cgd {
224 1.10 tls struct wordent *fp;
225 1.1 cgd
226 1.1 cgd while (vp->next != vp) {
227 1.1 cgd fp = vp->next;
228 1.1 cgd vp->next = fp->next;
229 1.1 cgd xfree((ptr_t) fp->word);
230 1.1 cgd xfree((ptr_t) fp);
231 1.1 cgd }
232 1.1 cgd vp->prev = vp;
233 1.1 cgd }
234 1.1 cgd
235 1.1 cgd static Char *
236 1.19 wiz word(void)
237 1.1 cgd {
238 1.19 wiz Char wbuf[BUFSIZE], *wp;
239 1.19 wiz int i;
240 1.10 tls Char c, c1;
241 1.10 tls bool dolflg;
242 1.1 cgd
243 1.1 cgd wp = wbuf;
244 1.17 christos i = BUFSIZE - 4;
245 1.1 cgd loop:
246 1.5 mycroft while ((c = getC(DOALL)) == ' ' || c == '\t')
247 1.5 mycroft continue;
248 1.1 cgd if (cmap(c, _META | _ESC))
249 1.1 cgd switch (c) {
250 1.1 cgd case '&':
251 1.1 cgd case '|':
252 1.1 cgd case '<':
253 1.1 cgd case '>':
254 1.1 cgd *wp++ = c;
255 1.1 cgd c1 = getC(DOALL);
256 1.1 cgd if (c1 == c)
257 1.1 cgd *wp++ = c1;
258 1.1 cgd else
259 1.1 cgd ungetC(c1);
260 1.1 cgd goto ret;
261 1.1 cgd
262 1.1 cgd case '#':
263 1.1 cgd if (intty)
264 1.1 cgd break;
265 1.1 cgd c = 0;
266 1.1 cgd do {
267 1.1 cgd c1 = c;
268 1.1 cgd c = getC(0);
269 1.1 cgd } while (c != '\n');
270 1.1 cgd if (c1 == '\\')
271 1.1 cgd goto loop;
272 1.13 mycroft /* FALLTHROUGH */
273 1.1 cgd
274 1.1 cgd case ';':
275 1.1 cgd case '(':
276 1.1 cgd case ')':
277 1.1 cgd case '\n':
278 1.1 cgd *wp++ = c;
279 1.1 cgd goto ret;
280 1.1 cgd
281 1.1 cgd case '\\':
282 1.1 cgd c = getC(0);
283 1.1 cgd if (c == '\n') {
284 1.1 cgd if (onelflg == 1)
285 1.1 cgd onelflg = 2;
286 1.1 cgd goto loop;
287 1.1 cgd }
288 1.1 cgd if (c != HIST)
289 1.1 cgd *wp++ = '\\', --i;
290 1.1 cgd c |= QUOTE;
291 1.13 mycroft break;
292 1.1 cgd }
293 1.1 cgd c1 = 0;
294 1.1 cgd dolflg = DOALL;
295 1.1 cgd for (;;) {
296 1.1 cgd if (c1) {
297 1.1 cgd if (c == c1) {
298 1.1 cgd c1 = 0;
299 1.1 cgd dolflg = DOALL;
300 1.1 cgd }
301 1.1 cgd else if (c == '\\') {
302 1.1 cgd c = getC(0);
303 1.1 cgd if (c == HIST)
304 1.1 cgd c |= QUOTE;
305 1.1 cgd else {
306 1.1 cgd if (c == '\n')
307 1.1 cgd /*
308 1.1 cgd * if (c1 == '`') c = ' '; else
309 1.1 cgd */
310 1.1 cgd c |= QUOTE;
311 1.1 cgd ungetC(c);
312 1.1 cgd c = '\\';
313 1.1 cgd }
314 1.1 cgd }
315 1.1 cgd else if (c == '\n') {
316 1.1 cgd seterror(ERR_UNMATCHED, c1);
317 1.1 cgd ungetC(c);
318 1.1 cgd break;
319 1.1 cgd }
320 1.1 cgd }
321 1.5 mycroft else if (cmap(c, _META | _QF | _QB | _ESC)) {
322 1.1 cgd if (c == '\\') {
323 1.1 cgd c = getC(0);
324 1.1 cgd if (c == '\n') {
325 1.1 cgd if (onelflg == 1)
326 1.1 cgd onelflg = 2;
327 1.1 cgd break;
328 1.1 cgd }
329 1.1 cgd if (c != HIST)
330 1.1 cgd *wp++ = '\\', --i;
331 1.1 cgd c |= QUOTE;
332 1.1 cgd }
333 1.5 mycroft else if (cmap(c, _QF | _QB)) { /* '"` */
334 1.1 cgd c1 = c;
335 1.1 cgd dolflg = c == '"' ? DOALL : DOEXCL;
336 1.1 cgd }
337 1.1 cgd else if (c != '#' || !intty) {
338 1.1 cgd ungetC(c);
339 1.1 cgd break;
340 1.1 cgd }
341 1.1 cgd }
342 1.1 cgd if (--i > 0) {
343 1.1 cgd *wp++ = c;
344 1.1 cgd c = getC(dolflg);
345 1.1 cgd }
346 1.1 cgd else {
347 1.1 cgd seterror(ERR_WTOOLONG);
348 1.1 cgd wp = &wbuf[1];
349 1.1 cgd break;
350 1.1 cgd }
351 1.1 cgd }
352 1.1 cgd ret:
353 1.1 cgd *wp = 0;
354 1.1 cgd return (Strsave(wbuf));
355 1.1 cgd }
356 1.1 cgd
357 1.1 cgd static int
358 1.19 wiz getC1(int flag)
359 1.1 cgd {
360 1.10 tls Char c;
361 1.1 cgd
362 1.13 mycroft for (;;) {
363 1.5 mycroft if ((c = peekc) != '\0') {
364 1.1 cgd peekc = 0;
365 1.1 cgd return (c);
366 1.1 cgd }
367 1.1 cgd if (lap) {
368 1.1 cgd if ((c = *lap++) == 0)
369 1.1 cgd lap = 0;
370 1.1 cgd else {
371 1.5 mycroft if (cmap(c, _META | _QF | _QB))
372 1.1 cgd c |= QUOTE;
373 1.1 cgd return (c);
374 1.1 cgd }
375 1.1 cgd }
376 1.5 mycroft if ((c = peekd) != '\0') {
377 1.1 cgd peekd = 0;
378 1.1 cgd return (c);
379 1.1 cgd }
380 1.1 cgd if (exclp) {
381 1.5 mycroft if ((c = *exclp++) != '\0')
382 1.1 cgd return (c);
383 1.1 cgd if (exclnxt && --exclc >= 0) {
384 1.1 cgd exclnxt = exclnxt->next;
385 1.1 cgd setexclp(exclnxt->word);
386 1.1 cgd return (' ');
387 1.1 cgd }
388 1.1 cgd exclp = 0;
389 1.1 cgd exclnxt = 0;
390 1.1 cgd }
391 1.1 cgd if (exclnxt) {
392 1.1 cgd exclnxt = exclnxt->next;
393 1.1 cgd if (--exclc < 0)
394 1.1 cgd exclnxt = 0;
395 1.1 cgd else
396 1.1 cgd setexclp(exclnxt->word);
397 1.1 cgd continue;
398 1.1 cgd }
399 1.1 cgd c = readc(0);
400 1.1 cgd if (c == '$' && (flag & DODOL)) {
401 1.1 cgd getdol();
402 1.1 cgd continue;
403 1.1 cgd }
404 1.1 cgd if (c == HIST && (flag & DOEXCL)) {
405 1.1 cgd getexcl(0);
406 1.1 cgd continue;
407 1.1 cgd }
408 1.1 cgd break;
409 1.1 cgd }
410 1.1 cgd return (c);
411 1.1 cgd }
412 1.1 cgd
413 1.1 cgd static void
414 1.19 wiz getdol(void)
415 1.1 cgd {
416 1.19 wiz Char name[4*MAXVARLEN+1], *ep, *np;
417 1.19 wiz int c, sc;
418 1.19 wiz bool special, toolong;
419 1.1 cgd
420 1.19 wiz special = 0;
421 1.1 cgd np = name, *np++ = '$';
422 1.1 cgd c = sc = getC(DOEXCL);
423 1.1 cgd if (any("\t \n", c)) {
424 1.1 cgd ungetD(c);
425 1.1 cgd ungetC('$' | QUOTE);
426 1.1 cgd return;
427 1.1 cgd }
428 1.1 cgd if (c == '{')
429 1.1 cgd *np++ = c, c = getC(DOEXCL);
430 1.1 cgd if (c == '#' || c == '?')
431 1.1 cgd special++, *np++ = c, c = getC(DOEXCL);
432 1.1 cgd *np++ = c;
433 1.1 cgd switch (c) {
434 1.1 cgd case '<':
435 1.1 cgd case '$':
436 1.5 mycroft case '!':
437 1.1 cgd if (special)
438 1.1 cgd seterror(ERR_SPDOLLT);
439 1.1 cgd *np = 0;
440 1.1 cgd addla(name);
441 1.1 cgd return;
442 1.1 cgd case '\n':
443 1.1 cgd ungetD(c);
444 1.1 cgd np--;
445 1.1 cgd seterror(ERR_NEWLINE);
446 1.1 cgd *np = 0;
447 1.1 cgd addla(name);
448 1.1 cgd return;
449 1.1 cgd case '*':
450 1.1 cgd if (special)
451 1.1 cgd seterror(ERR_SPSTAR);
452 1.1 cgd *np = 0;
453 1.1 cgd addla(name);
454 1.1 cgd return;
455 1.1 cgd default:
456 1.1 cgd toolong = 0;
457 1.1 cgd if (Isdigit(c)) {
458 1.1 cgd #ifdef notdef
459 1.1 cgd /* let $?0 pass for now */
460 1.1 cgd if (special) {
461 1.1 cgd seterror(ERR_DIGIT);
462 1.1 cgd *np = 0;
463 1.1 cgd addla(name);
464 1.1 cgd return;
465 1.1 cgd }
466 1.1 cgd #endif
467 1.1 cgd /* we know that np < &name[4] */
468 1.1 cgd ep = &np[MAXVARLEN];
469 1.5 mycroft while ((c = getC(DOEXCL)) != '\0'){
470 1.1 cgd if (!Isdigit(c))
471 1.1 cgd break;
472 1.1 cgd if (np < ep)
473 1.1 cgd *np++ = c;
474 1.1 cgd else
475 1.1 cgd toolong = 1;
476 1.1 cgd }
477 1.1 cgd }
478 1.1 cgd else if (letter(c)) {
479 1.1 cgd /* we know that np < &name[4] */
480 1.1 cgd ep = &np[MAXVARLEN];
481 1.1 cgd toolong = 0;
482 1.5 mycroft while ((c = getC(DOEXCL)) != '\0') {
483 1.1 cgd /* Bugfix for ${v123x} from Chris Torek, DAS DEC-90. */
484 1.1 cgd if (!letter(c) && !Isdigit(c))
485 1.1 cgd break;
486 1.1 cgd if (np < ep)
487 1.1 cgd *np++ = c;
488 1.1 cgd else
489 1.1 cgd toolong = 1;
490 1.1 cgd }
491 1.1 cgd }
492 1.1 cgd else {
493 1.1 cgd *np = 0;
494 1.1 cgd seterror(ERR_VARILL);
495 1.1 cgd addla(name);
496 1.1 cgd return;
497 1.1 cgd }
498 1.1 cgd if (toolong) {
499 1.1 cgd seterror(ERR_VARTOOLONG);
500 1.1 cgd *np = 0;
501 1.1 cgd addla(name);
502 1.1 cgd return;
503 1.1 cgd }
504 1.1 cgd break;
505 1.1 cgd }
506 1.1 cgd if (c == '[') {
507 1.1 cgd *np++ = c;
508 1.1 cgd /*
509 1.1 cgd * Name up to here is a max of MAXVARLEN + 8.
510 1.1 cgd */
511 1.1 cgd ep = &np[2 * MAXVARLEN + 8];
512 1.1 cgd do {
513 1.1 cgd /*
514 1.1 cgd * Michael Greim: Allow $ expansion to take place in selector
515 1.1 cgd * expressions. (limits the number of characters returned)
516 1.1 cgd */
517 1.1 cgd c = getC(DOEXCL | DODOL);
518 1.1 cgd if (c == '\n') {
519 1.1 cgd ungetD(c);
520 1.1 cgd np--;
521 1.1 cgd seterror(ERR_NLINDEX);
522 1.1 cgd *np = 0;
523 1.1 cgd addla(name);
524 1.1 cgd return;
525 1.1 cgd }
526 1.1 cgd if (np < ep)
527 1.1 cgd *np++ = c;
528 1.1 cgd } while (c != ']');
529 1.1 cgd *np = '\0';
530 1.1 cgd if (np >= ep) {
531 1.1 cgd seterror(ERR_SELOVFL);
532 1.1 cgd addla(name);
533 1.1 cgd return;
534 1.1 cgd }
535 1.1 cgd c = getC(DOEXCL);
536 1.1 cgd }
537 1.1 cgd /*
538 1.1 cgd * Name up to here is a max of 2 * MAXVARLEN + 8.
539 1.1 cgd */
540 1.1 cgd if (c == ':') {
541 1.1 cgd /*
542 1.1 cgd * if the :g modifier is followed by a newline, then error right away!
543 1.1 cgd * -strike
544 1.1 cgd */
545 1.19 wiz int amodflag, gmodflag;
546 1.1 cgd
547 1.19 wiz amodflag = 0;
548 1.19 wiz gmodflag = 0;
549 1.5 mycroft do {
550 1.5 mycroft *np++ = c, c = getC(DOEXCL);
551 1.5 mycroft if (c == 'g' || c == 'a') {
552 1.5 mycroft if (c == 'g')
553 1.5 mycroft gmodflag++;
554 1.5 mycroft else
555 1.5 mycroft amodflag++;
556 1.5 mycroft *np++ = c; c = getC(DOEXCL);
557 1.5 mycroft }
558 1.5 mycroft if ((c == 'g' && !gmodflag) || (c == 'a' && !amodflag)) {
559 1.5 mycroft if (c == 'g')
560 1.5 mycroft gmodflag++;
561 1.5 mycroft else
562 1.5 mycroft amodflag++;
563 1.5 mycroft *np++ = c; c = getC(DOEXCL);
564 1.5 mycroft }
565 1.5 mycroft *np++ = c;
566 1.5 mycroft /* scan s// [eichin:19910926.0512EST] */
567 1.5 mycroft if (c == 's') {
568 1.5 mycroft int delimcnt = 2;
569 1.5 mycroft int delim = getC(0);
570 1.5 mycroft *np++ = delim;
571 1.5 mycroft
572 1.5 mycroft if (!delim || letter(delim)
573 1.5 mycroft || Isdigit(delim) || any(" \t\n", delim)) {
574 1.5 mycroft seterror(ERR_BADSUBST);
575 1.5 mycroft break;
576 1.5 mycroft }
577 1.5 mycroft while ((c = getC(0)) != (-1)) {
578 1.5 mycroft *np++ = c;
579 1.5 mycroft if(c == delim) delimcnt--;
580 1.5 mycroft if(!delimcnt) break;
581 1.5 mycroft }
582 1.5 mycroft if(delimcnt) {
583 1.5 mycroft seterror(ERR_BADSUBST);
584 1.5 mycroft break;
585 1.5 mycroft }
586 1.5 mycroft c = 's';
587 1.5 mycroft }
588 1.5 mycroft if (!any("htrqxes", c)) {
589 1.15 mycroft if ((amodflag || gmodflag) && c == '\n')
590 1.5 mycroft stderror(ERR_VARSYN); /* strike */
591 1.5 mycroft seterror(ERR_VARMOD, c);
592 1.5 mycroft *np = 0;
593 1.5 mycroft addla(name);
594 1.5 mycroft return;
595 1.5 mycroft }
596 1.1 cgd }
597 1.5 mycroft while ((c = getC(DOEXCL)) == ':');
598 1.5 mycroft ungetD(c);
599 1.1 cgd }
600 1.1 cgd else
601 1.1 cgd ungetD(c);
602 1.1 cgd if (sc == '{') {
603 1.1 cgd c = getC(DOEXCL);
604 1.1 cgd if (c != '}') {
605 1.1 cgd ungetD(c);
606 1.1 cgd seterror(ERR_MISSING, '}');
607 1.1 cgd *np = 0;
608 1.1 cgd addla(name);
609 1.1 cgd return;
610 1.1 cgd }
611 1.1 cgd *np++ = c;
612 1.1 cgd }
613 1.1 cgd *np = 0;
614 1.1 cgd addla(name);
615 1.1 cgd return;
616 1.1 cgd }
617 1.1 cgd
618 1.1 cgd void
619 1.19 wiz addla(Char *cp)
620 1.1 cgd {
621 1.19 wiz Char buf[BUFSIZE];
622 1.1 cgd
623 1.1 cgd if (Strlen(cp) + (lap ? Strlen(lap) : 0) >=
624 1.1 cgd (sizeof(labuf) - 4) / sizeof(Char)) {
625 1.1 cgd seterror(ERR_EXPOVFL);
626 1.1 cgd return;
627 1.1 cgd }
628 1.1 cgd if (lap)
629 1.19 wiz (void)Strcpy(buf, lap);
630 1.19 wiz (void)Strcpy(labuf, cp);
631 1.1 cgd if (lap)
632 1.19 wiz (void)Strcat(labuf, buf);
633 1.1 cgd lap = labuf;
634 1.1 cgd }
635 1.1 cgd
636 1.1 cgd static Char lhsb[32];
637 1.1 cgd static Char slhs[32];
638 1.1 cgd static Char rhsb[64];
639 1.1 cgd static int quesarg;
640 1.1 cgd
641 1.1 cgd static void
642 1.19 wiz getexcl(int sc)
643 1.1 cgd {
644 1.10 tls struct wordent *hp, *ip;
645 1.19 wiz int c, dol, left, right;
646 1.1 cgd
647 1.1 cgd if (sc == 0) {
648 1.1 cgd sc = getC(0);
649 1.1 cgd if (sc != '{') {
650 1.1 cgd ungetC(sc);
651 1.1 cgd sc = 0;
652 1.1 cgd }
653 1.1 cgd }
654 1.1 cgd quesarg = -1;
655 1.1 cgd lastev = eventno;
656 1.1 cgd hp = gethent(sc);
657 1.1 cgd if (hp == 0)
658 1.1 cgd return;
659 1.1 cgd hadhist = 1;
660 1.1 cgd dol = 0;
661 1.1 cgd if (hp == alhistp)
662 1.1 cgd for (ip = hp->next->next; ip != alhistt; ip = ip->next)
663 1.1 cgd dol++;
664 1.1 cgd else
665 1.1 cgd for (ip = hp->next->next; ip != hp->prev; ip = ip->next)
666 1.1 cgd dol++;
667 1.1 cgd left = 0, right = dol;
668 1.1 cgd if (sc == HISTSUB) {
669 1.1 cgd ungetC('s'), unreadc(HISTSUB), c = ':';
670 1.1 cgd goto subst;
671 1.1 cgd }
672 1.1 cgd c = getC(0);
673 1.1 cgd if (!any(":^$*-%", c))
674 1.1 cgd goto subst;
675 1.1 cgd left = right = -1;
676 1.1 cgd if (c == ':') {
677 1.1 cgd c = getC(0);
678 1.1 cgd unreadc(c);
679 1.1 cgd if (letter(c) || c == '&') {
680 1.1 cgd c = ':';
681 1.1 cgd left = 0, right = dol;
682 1.1 cgd goto subst;
683 1.1 cgd }
684 1.1 cgd }
685 1.1 cgd else
686 1.1 cgd ungetC(c);
687 1.1 cgd if (!getsel(&left, &right, dol))
688 1.1 cgd return;
689 1.1 cgd c = getC(0);
690 1.1 cgd if (c == '*')
691 1.1 cgd ungetC(c), c = '-';
692 1.1 cgd if (c == '-') {
693 1.1 cgd if (!getsel(&left, &right, dol))
694 1.1 cgd return;
695 1.1 cgd c = getC(0);
696 1.1 cgd }
697 1.1 cgd subst:
698 1.1 cgd exclc = right - left + 1;
699 1.1 cgd while (--left >= 0)
700 1.1 cgd hp = hp->next;
701 1.1 cgd if (sc == HISTSUB || c == ':') {
702 1.1 cgd do {
703 1.1 cgd hp = getsub(hp);
704 1.1 cgd c = getC(0);
705 1.1 cgd } while (c == ':');
706 1.1 cgd }
707 1.1 cgd unreadc(c);
708 1.1 cgd if (sc == '{') {
709 1.1 cgd c = getC(0);
710 1.1 cgd if (c != '}')
711 1.1 cgd seterror(ERR_BADBANG);
712 1.1 cgd }
713 1.1 cgd exclnxt = hp;
714 1.1 cgd }
715 1.1 cgd
716 1.1 cgd static struct wordent *
717 1.19 wiz getsub(struct wordent *en)
718 1.1 cgd {
719 1.19 wiz Char orhsb[sizeof(rhsb) / sizeof(Char)];
720 1.10 tls Char *cp;
721 1.19 wiz int c, delim, sc;
722 1.5 mycroft bool global;
723 1.1 cgd
724 1.5 mycroft do {
725 1.5 mycroft exclnxt = 0;
726 1.5 mycroft global = 0;
727 1.5 mycroft sc = c = getC(0);
728 1.5 mycroft if (c == 'g' || c == 'a') {
729 1.5 mycroft global |= (c == 'g') ? 1 : 2;
730 1.5 mycroft sc = c = getC(0);
731 1.5 mycroft }
732 1.5 mycroft if (((c =='g') && !(global & 1)) || ((c == 'a') && !(global & 2))) {
733 1.5 mycroft global |= (c == 'g') ? 1 : 2;
734 1.5 mycroft sc = c = getC(0);
735 1.5 mycroft }
736 1.1 cgd
737 1.5 mycroft switch (c) {
738 1.5 mycroft case 'p':
739 1.5 mycroft justpr++;
740 1.5 mycroft return (en);
741 1.5 mycroft case 'x':
742 1.5 mycroft case 'q':
743 1.5 mycroft global |= 1;
744 1.13 mycroft /* FALLTHROUGH */
745 1.5 mycroft case 'h':
746 1.5 mycroft case 'r':
747 1.5 mycroft case 't':
748 1.5 mycroft case 'e':
749 1.5 mycroft break;
750 1.5 mycroft case '&':
751 1.5 mycroft if (slhs[0] == 0) {
752 1.5 mycroft seterror(ERR_NOSUBST);
753 1.5 mycroft return (en);
754 1.5 mycroft }
755 1.5 mycroft (void) Strcpy(lhsb, slhs);
756 1.5 mycroft break;
757 1.1 cgd #ifdef notdef
758 1.5 mycroft case '~':
759 1.5 mycroft if (lhsb[0] == 0)
760 1.5 mycroft goto badlhs;
761 1.5 mycroft break;
762 1.1 cgd #endif
763 1.5 mycroft case 's':
764 1.5 mycroft delim = getC(0);
765 1.5 mycroft if (letter(delim) || Isdigit(delim) || any(" \t\n", delim)) {
766 1.5 mycroft unreadc(delim);
767 1.1 cgd lhsb[0] = 0;
768 1.1 cgd seterror(ERR_BADSUBST);
769 1.1 cgd return (en);
770 1.1 cgd }
771 1.5 mycroft cp = lhsb;
772 1.5 mycroft for (;;) {
773 1.1 cgd c = getC(0);
774 1.5 mycroft if (c == '\n') {
775 1.5 mycroft unreadc(c);
776 1.5 mycroft break;
777 1.5 mycroft }
778 1.5 mycroft if (c == delim)
779 1.5 mycroft break;
780 1.5 mycroft if (cp > &lhsb[sizeof(lhsb) / sizeof(Char) - 2]) {
781 1.5 mycroft lhsb[0] = 0;
782 1.5 mycroft seterror(ERR_BADSUBST);
783 1.5 mycroft return (en);
784 1.5 mycroft }
785 1.5 mycroft if (c == '\\') {
786 1.5 mycroft c = getC(0);
787 1.5 mycroft if (c != delim && c != '\\')
788 1.5 mycroft *cp++ = '\\';
789 1.5 mycroft }
790 1.5 mycroft *cp++ = c;
791 1.1 cgd }
792 1.5 mycroft if (cp != lhsb)
793 1.5 mycroft *cp++ = 0;
794 1.5 mycroft else if (lhsb[0] == 0) {
795 1.5 mycroft seterror(ERR_LHS);
796 1.5 mycroft return (en);
797 1.1 cgd }
798 1.5 mycroft cp = rhsb;
799 1.19 wiz (void)Strcpy(orhsb, cp);
800 1.5 mycroft for (;;) {
801 1.5 mycroft c = getC(0);
802 1.5 mycroft if (c == '\n') {
803 1.5 mycroft unreadc(c);
804 1.5 mycroft break;
805 1.5 mycroft }
806 1.5 mycroft if (c == delim)
807 1.5 mycroft break;
808 1.1 cgd #ifdef notdef
809 1.5 mycroft if (c == '~') {
810 1.5 mycroft if (&cp[Strlen(orhsb)] > &rhsb[sizeof(rhsb) /
811 1.5 mycroft sizeof(Char) - 2])
812 1.5 mycroft goto toorhs;
813 1.19 wiz (void)Strcpy(cp, orhsb);
814 1.5 mycroft cp = Strend(cp);
815 1.5 mycroft continue;
816 1.5 mycroft }
817 1.1 cgd #endif
818 1.5 mycroft if (cp > &rhsb[sizeof(rhsb) / sizeof(Char) - 2]) {
819 1.5 mycroft seterror(ERR_RHSLONG);
820 1.5 mycroft return (en);
821 1.5 mycroft }
822 1.5 mycroft if (c == '\\') {
823 1.5 mycroft c = getC(0);
824 1.5 mycroft if (c != delim /* && c != '~' */ )
825 1.5 mycroft *cp++ = '\\';
826 1.5 mycroft }
827 1.5 mycroft *cp++ = c;
828 1.1 cgd }
829 1.5 mycroft *cp++ = 0;
830 1.5 mycroft break;
831 1.5 mycroft default:
832 1.5 mycroft if (c == '\n')
833 1.5 mycroft unreadc(c);
834 1.5 mycroft seterror(ERR_BADBANGMOD, c);
835 1.5 mycroft return (en);
836 1.1 cgd }
837 1.19 wiz (void)Strcpy(slhs, lhsb);
838 1.5 mycroft if (exclc)
839 1.5 mycroft en = dosub(sc, en, global);
840 1.1 cgd }
841 1.5 mycroft while ((c = getC(0)) == ':');
842 1.5 mycroft unreadc(c);
843 1.1 cgd return (en);
844 1.1 cgd }
845 1.1 cgd
846 1.1 cgd static struct wordent *
847 1.19 wiz dosub(int sc, struct wordent *en, bool global)
848 1.1 cgd {
849 1.19 wiz struct wordent lexi, *hp, *wdp;
850 1.19 wiz int i;
851 1.19 wiz bool didone, didsub;
852 1.19 wiz
853 1.19 wiz didone = 0;
854 1.19 wiz didsub = 0;
855 1.19 wiz i = exclc;
856 1.19 wiz hp = &lexi;
857 1.1 cgd
858 1.1 cgd wdp = hp;
859 1.1 cgd while (--i >= 0) {
860 1.19 wiz struct wordent *new = (struct wordent *)xcalloc(1, sizeof *wdp);
861 1.1 cgd
862 1.1 cgd new->word = 0;
863 1.1 cgd new->prev = wdp;
864 1.1 cgd new->next = hp;
865 1.1 cgd wdp->next = new;
866 1.1 cgd wdp = new;
867 1.1 cgd en = en->next;
868 1.5 mycroft if (en->word) {
869 1.5 mycroft Char *tword, *otword;
870 1.5 mycroft
871 1.5 mycroft if ((global & 1) || didsub == 0) {
872 1.5 mycroft tword = subword(en->word, sc, &didone);
873 1.5 mycroft if (didone)
874 1.5 mycroft didsub = 1;
875 1.5 mycroft if (global & 2) {
876 1.5 mycroft while (didone && tword != STRNULL) {
877 1.5 mycroft otword = tword;
878 1.5 mycroft tword = subword(otword, sc, &didone);
879 1.5 mycroft if (Strcmp(tword, otword) == 0) {
880 1.5 mycroft xfree((ptr_t) otword);
881 1.5 mycroft break;
882 1.5 mycroft }
883 1.5 mycroft else
884 1.19 wiz xfree((ptr_t)otword);
885 1.5 mycroft }
886 1.5 mycroft }
887 1.5 mycroft }
888 1.5 mycroft else
889 1.5 mycroft tword = Strsave(en->word);
890 1.5 mycroft wdp->word = tword;
891 1.5 mycroft }
892 1.1 cgd }
893 1.1 cgd if (didsub == 0)
894 1.1 cgd seterror(ERR_MODFAIL);
895 1.1 cgd hp->prev = wdp;
896 1.1 cgd return (&enthist(-1000, &lexi, 0)->Hlex);
897 1.1 cgd }
898 1.1 cgd
899 1.1 cgd static Char *
900 1.19 wiz subword(Char *cp, int type, bool *adid)
901 1.1 cgd {
902 1.19 wiz Char wbuf[BUFSIZE];
903 1.19 wiz Char *mp, *np, *wp;
904 1.10 tls int i;
905 1.1 cgd
906 1.5 mycroft *adid = 0;
907 1.1 cgd switch (type) {
908 1.1 cgd case 'r':
909 1.1 cgd case 'e':
910 1.1 cgd case 'h':
911 1.1 cgd case 't':
912 1.1 cgd case 'q':
913 1.1 cgd case 'x':
914 1.1 cgd wp = domod(cp, type);
915 1.1 cgd if (wp == 0)
916 1.1 cgd return (Strsave(cp));
917 1.1 cgd *adid = 1;
918 1.1 cgd return (wp);
919 1.1 cgd default:
920 1.1 cgd wp = wbuf;
921 1.17 christos i = BUFSIZE - 4;
922 1.1 cgd for (mp = cp; *mp; mp++)
923 1.1 cgd if (matchs(mp, lhsb)) {
924 1.1 cgd for (np = cp; np < mp;)
925 1.1 cgd *wp++ = *np++, --i;
926 1.1 cgd for (np = rhsb; *np; np++)
927 1.1 cgd switch (*np) {
928 1.1 cgd case '\\':
929 1.1 cgd if (np[1] == '&')
930 1.1 cgd np++;
931 1.13 mycroft /* FALLTHROUGH */
932 1.1 cgd default:
933 1.1 cgd if (--i < 0) {
934 1.1 cgd seterror(ERR_SUBOVFL);
935 1.1 cgd return (STRNULL);
936 1.1 cgd }
937 1.1 cgd *wp++ = *np;
938 1.1 cgd continue;
939 1.1 cgd case '&':
940 1.1 cgd i -= Strlen(lhsb);
941 1.1 cgd if (i < 0) {
942 1.1 cgd seterror(ERR_SUBOVFL);
943 1.1 cgd return (STRNULL);
944 1.1 cgd }
945 1.1 cgd *wp = 0;
946 1.1 cgd (void) Strcat(wp, lhsb);
947 1.1 cgd wp = Strend(wp);
948 1.1 cgd continue;
949 1.1 cgd }
950 1.1 cgd mp += Strlen(lhsb);
951 1.1 cgd i -= Strlen(mp);
952 1.1 cgd if (i < 0) {
953 1.1 cgd seterror(ERR_SUBOVFL);
954 1.1 cgd return (STRNULL);
955 1.1 cgd }
956 1.1 cgd *wp = 0;
957 1.1 cgd (void) Strcat(wp, mp);
958 1.1 cgd *adid = 1;
959 1.1 cgd return (Strsave(wbuf));
960 1.1 cgd }
961 1.1 cgd return (Strsave(cp));
962 1.1 cgd }
963 1.1 cgd }
964 1.1 cgd
965 1.19 wiz Char *
966 1.19 wiz domod(Char *cp, int type)
967 1.1 cgd {
968 1.10 tls Char *wp, *xp;
969 1.10 tls int c;
970 1.1 cgd
971 1.1 cgd switch (type) {
972 1.1 cgd case 'x':
973 1.1 cgd case 'q':
974 1.1 cgd wp = Strsave(cp);
975 1.5 mycroft for (xp = wp; (c = *xp) != '\0'; xp++)
976 1.1 cgd if ((c != ' ' && c != '\t') || type == 'q')
977 1.1 cgd *xp |= QUOTE;
978 1.1 cgd return (wp);
979 1.1 cgd case 'h':
980 1.1 cgd case 't':
981 1.1 cgd if (!any(short2str(cp), '/'))
982 1.1 cgd return (type == 't' ? Strsave(cp) : 0);
983 1.1 cgd wp = Strend(cp);
984 1.1 cgd while (*--wp != '/')
985 1.1 cgd continue;
986 1.1 cgd if (type == 'h')
987 1.1 cgd xp = Strsave(cp), xp[wp - cp] = 0;
988 1.1 cgd else
989 1.1 cgd xp = Strsave(wp + 1);
990 1.1 cgd return (xp);
991 1.1 cgd case 'e':
992 1.1 cgd case 'r':
993 1.1 cgd wp = Strend(cp);
994 1.1 cgd for (wp--; wp >= cp && *wp != '/'; wp--)
995 1.1 cgd if (*wp == '.') {
996 1.1 cgd if (type == 'e')
997 1.1 cgd xp = Strsave(wp + 1);
998 1.1 cgd else
999 1.1 cgd xp = Strsave(cp), xp[wp - cp] = 0;
1000 1.1 cgd return (xp);
1001 1.1 cgd }
1002 1.1 cgd return (Strsave(type == 'e' ? STRNULL : cp));
1003 1.5 mycroft default:
1004 1.5 mycroft break;
1005 1.1 cgd }
1006 1.1 cgd return (0);
1007 1.1 cgd }
1008 1.1 cgd
1009 1.1 cgd static int
1010 1.19 wiz matchs(Char *str, Char *pat)
1011 1.1 cgd {
1012 1.1 cgd while (*str && *pat && *str == *pat)
1013 1.1 cgd str++, pat++;
1014 1.1 cgd return (*pat == 0);
1015 1.1 cgd }
1016 1.1 cgd
1017 1.1 cgd static int
1018 1.19 wiz getsel(int *al, int *ar, int dol)
1019 1.1 cgd {
1020 1.19 wiz int c, i;
1021 1.19 wiz bool first;
1022 1.19 wiz
1023 1.19 wiz c = getC(0);
1024 1.19 wiz first = *al < 0;
1025 1.1 cgd
1026 1.1 cgd switch (c) {
1027 1.1 cgd case '%':
1028 1.1 cgd if (quesarg == -1) {
1029 1.1 cgd seterror(ERR_BADBANGARG);
1030 1.1 cgd return (0);
1031 1.1 cgd }
1032 1.1 cgd if (*al < 0)
1033 1.1 cgd *al = quesarg;
1034 1.1 cgd *ar = quesarg;
1035 1.1 cgd break;
1036 1.1 cgd case '-':
1037 1.1 cgd if (*al < 0) {
1038 1.1 cgd *al = 0;
1039 1.1 cgd *ar = dol - 1;
1040 1.1 cgd unreadc(c);
1041 1.1 cgd }
1042 1.1 cgd return (1);
1043 1.1 cgd case '^':
1044 1.1 cgd if (*al < 0)
1045 1.1 cgd *al = 1;
1046 1.1 cgd *ar = 1;
1047 1.1 cgd break;
1048 1.1 cgd case '$':
1049 1.1 cgd if (*al < 0)
1050 1.1 cgd *al = dol;
1051 1.1 cgd *ar = dol;
1052 1.1 cgd break;
1053 1.1 cgd case '*':
1054 1.1 cgd if (*al < 0)
1055 1.1 cgd *al = 1;
1056 1.1 cgd *ar = dol;
1057 1.1 cgd if (*ar < *al) {
1058 1.1 cgd *ar = 0;
1059 1.1 cgd *al = 1;
1060 1.1 cgd return (1);
1061 1.1 cgd }
1062 1.1 cgd break;
1063 1.1 cgd default:
1064 1.1 cgd if (Isdigit(c)) {
1065 1.1 cgd i = 0;
1066 1.1 cgd while (Isdigit(c)) {
1067 1.1 cgd i = i * 10 + c - '0';
1068 1.1 cgd c = getC(0);
1069 1.1 cgd }
1070 1.1 cgd if (i < 0)
1071 1.1 cgd i = dol + 1;
1072 1.1 cgd if (*al < 0)
1073 1.1 cgd *al = i;
1074 1.1 cgd *ar = i;
1075 1.1 cgd }
1076 1.1 cgd else if (*al < 0)
1077 1.1 cgd *al = 0, *ar = dol;
1078 1.1 cgd else
1079 1.1 cgd *ar = dol - 1;
1080 1.1 cgd unreadc(c);
1081 1.1 cgd break;
1082 1.1 cgd }
1083 1.1 cgd if (first) {
1084 1.1 cgd c = getC(0);
1085 1.1 cgd unreadc(c);
1086 1.1 cgd if (any("-$*", c))
1087 1.1 cgd return (1);
1088 1.1 cgd }
1089 1.1 cgd if (*al > *ar || *ar > dol) {
1090 1.1 cgd seterror(ERR_BADBANGARG);
1091 1.1 cgd return (0);
1092 1.1 cgd }
1093 1.1 cgd return (1);
1094 1.1 cgd
1095 1.1 cgd }
1096 1.1 cgd
1097 1.1 cgd static struct wordent *
1098 1.19 wiz gethent(int sc)
1099 1.1 cgd {
1100 1.10 tls struct Hist *hp;
1101 1.10 tls Char *np;
1102 1.19 wiz int c, event;
1103 1.19 wiz bool back;
1104 1.1 cgd
1105 1.19 wiz back = 0;
1106 1.1 cgd c = sc == HISTSUB ? HIST : getC(0);
1107 1.1 cgd if (c == HIST) {
1108 1.1 cgd if (alhistp)
1109 1.1 cgd return (alhistp);
1110 1.1 cgd event = eventno;
1111 1.1 cgd }
1112 1.1 cgd else
1113 1.1 cgd switch (c) {
1114 1.1 cgd case ':':
1115 1.1 cgd case '^':
1116 1.1 cgd case '$':
1117 1.1 cgd case '*':
1118 1.1 cgd case '%':
1119 1.1 cgd ungetC(c);
1120 1.1 cgd if (lastev == eventno && alhistp)
1121 1.1 cgd return (alhistp);
1122 1.1 cgd event = lastev;
1123 1.1 cgd break;
1124 1.1 cgd case '#': /* !# is command being typed in (mrh) */
1125 1.1 cgd if (--hleft == 0) {
1126 1.1 cgd seterror(ERR_HISTLOOP);
1127 1.1 cgd return (0);
1128 1.1 cgd }
1129 1.1 cgd else
1130 1.1 cgd return (¶ml);
1131 1.1 cgd /* NOTREACHED */
1132 1.1 cgd case '-':
1133 1.1 cgd back = 1;
1134 1.1 cgd c = getC(0);
1135 1.13 mycroft /* FALLTHROUGH */
1136 1.1 cgd default:
1137 1.1 cgd if (any("(=~", c)) {
1138 1.1 cgd unreadc(c);
1139 1.1 cgd ungetC(HIST);
1140 1.1 cgd return (0);
1141 1.1 cgd }
1142 1.1 cgd np = lhsb;
1143 1.1 cgd event = 0;
1144 1.5 mycroft while (!cmap(c, _ESC | _META | _QF | _QB) && !any("${}:", c)) {
1145 1.1 cgd if (event != -1 && Isdigit(c))
1146 1.1 cgd event = event * 10 + c - '0';
1147 1.1 cgd else
1148 1.1 cgd event = -1;
1149 1.1 cgd if (np < &lhsb[sizeof(lhsb) / sizeof(Char) - 2])
1150 1.1 cgd *np++ = c;
1151 1.1 cgd c = getC(0);
1152 1.1 cgd }
1153 1.1 cgd unreadc(c);
1154 1.1 cgd if (np == lhsb) {
1155 1.1 cgd ungetC(HIST);
1156 1.1 cgd return (0);
1157 1.1 cgd }
1158 1.1 cgd *np++ = 0;
1159 1.1 cgd if (event != -1) {
1160 1.1 cgd /*
1161 1.1 cgd * History had only digits
1162 1.1 cgd */
1163 1.1 cgd if (back)
1164 1.1 cgd event = eventno + (alhistp == 0) - (event ? event : 0);
1165 1.1 cgd break;
1166 1.1 cgd }
1167 1.1 cgd hp = findev(lhsb, 0);
1168 1.1 cgd if (hp)
1169 1.1 cgd lastev = hp->Hnum;
1170 1.1 cgd return (&hp->Hlex);
1171 1.1 cgd case '?':
1172 1.1 cgd np = lhsb;
1173 1.1 cgd for (;;) {
1174 1.1 cgd c = getC(0);
1175 1.1 cgd if (c == '\n') {
1176 1.1 cgd unreadc(c);
1177 1.1 cgd break;
1178 1.1 cgd }
1179 1.1 cgd if (c == '?')
1180 1.1 cgd break;
1181 1.1 cgd if (np < &lhsb[sizeof(lhsb) / sizeof(Char) - 2])
1182 1.1 cgd *np++ = c;
1183 1.1 cgd }
1184 1.1 cgd if (np == lhsb) {
1185 1.1 cgd if (lhsb[0] == 0) {
1186 1.1 cgd seterror(ERR_NOSEARCH);
1187 1.1 cgd return (0);
1188 1.1 cgd }
1189 1.1 cgd }
1190 1.1 cgd else
1191 1.1 cgd *np++ = 0;
1192 1.1 cgd hp = findev(lhsb, 1);
1193 1.1 cgd if (hp)
1194 1.1 cgd lastev = hp->Hnum;
1195 1.1 cgd return (&hp->Hlex);
1196 1.1 cgd }
1197 1.1 cgd
1198 1.1 cgd for (hp = Histlist.Hnext; hp; hp = hp->Hnext)
1199 1.1 cgd if (hp->Hnum == event) {
1200 1.1 cgd hp->Href = eventno;
1201 1.1 cgd lastev = hp->Hnum;
1202 1.1 cgd return (&hp->Hlex);
1203 1.1 cgd }
1204 1.1 cgd np = putn(event);
1205 1.5 mycroft seterror(ERR_NOEVENT, vis_str(np));
1206 1.1 cgd return (0);
1207 1.1 cgd }
1208 1.1 cgd
1209 1.1 cgd static struct Hist *
1210 1.19 wiz findev(Char *cp, bool anyarg)
1211 1.1 cgd {
1212 1.10 tls struct Hist *hp;
1213 1.1 cgd
1214 1.1 cgd for (hp = Histlist.Hnext; hp; hp = hp->Hnext) {
1215 1.19 wiz Char *dp, *p, *q;
1216 1.19 wiz struct wordent *lp;
1217 1.19 wiz int argno;
1218 1.19 wiz
1219 1.19 wiz lp = hp->Hlex.next;
1220 1.19 wiz argno = 0;
1221 1.1 cgd
1222 1.1 cgd /*
1223 1.1 cgd * The entries added by alias substitution don't have a newline but do
1224 1.1 cgd * have a negative event number. Savehist() trims off these entries,
1225 1.1 cgd * but it happens before alias expansion, too early to delete those
1226 1.1 cgd * from the previous command.
1227 1.1 cgd */
1228 1.1 cgd if (hp->Hnum < 0)
1229 1.1 cgd continue;
1230 1.1 cgd if (lp->word[0] == '\n')
1231 1.1 cgd continue;
1232 1.1 cgd if (!anyarg) {
1233 1.1 cgd p = cp;
1234 1.1 cgd q = lp->word;
1235 1.1 cgd do
1236 1.1 cgd if (!*p)
1237 1.1 cgd return (hp);
1238 1.1 cgd while (*p++ == *q++);
1239 1.1 cgd continue;
1240 1.1 cgd }
1241 1.1 cgd do {
1242 1.1 cgd for (dp = lp->word; *dp; dp++) {
1243 1.1 cgd p = cp;
1244 1.1 cgd q = dp;
1245 1.1 cgd do
1246 1.1 cgd if (!*p) {
1247 1.1 cgd quesarg = argno;
1248 1.1 cgd return (hp);
1249 1.1 cgd }
1250 1.1 cgd while (*p++ == *q++);
1251 1.1 cgd }
1252 1.1 cgd lp = lp->next;
1253 1.1 cgd argno++;
1254 1.1 cgd } while (lp->word[0] != '\n');
1255 1.1 cgd }
1256 1.5 mycroft seterror(ERR_NOEVENT, vis_str(cp));
1257 1.1 cgd return (0);
1258 1.1 cgd }
1259 1.1 cgd
1260 1.1 cgd
1261 1.1 cgd static void
1262 1.19 wiz setexclp(Char *cp)
1263 1.1 cgd {
1264 1.1 cgd if (cp && cp[0] == '\n')
1265 1.1 cgd return;
1266 1.1 cgd exclp = cp;
1267 1.1 cgd }
1268 1.1 cgd
1269 1.1 cgd void
1270 1.19 wiz unreadc(int c)
1271 1.1 cgd {
1272 1.1 cgd peekread = c;
1273 1.1 cgd }
1274 1.1 cgd
1275 1.1 cgd int
1276 1.19 wiz readc(bool wanteof)
1277 1.1 cgd {
1278 1.19 wiz static int sincereal;
1279 1.10 tls int c;
1280 1.1 cgd
1281 1.5 mycroft aret = F_SEEK;
1282 1.5 mycroft if ((c = peekread) != '\0') {
1283 1.1 cgd peekread = 0;
1284 1.1 cgd return (c);
1285 1.1 cgd }
1286 1.1 cgd top:
1287 1.5 mycroft aret = F_SEEK;
1288 1.1 cgd if (alvecp) {
1289 1.5 mycroft aret = A_SEEK;
1290 1.5 mycroft if ((c = *alvecp++) != '\0')
1291 1.1 cgd return (c);
1292 1.5 mycroft if (alvec && *alvec) {
1293 1.5 mycroft alvecp = *alvec++;
1294 1.5 mycroft return (' ');
1295 1.5 mycroft }
1296 1.5 mycroft else {
1297 1.5 mycroft aret = F_SEEK;
1298 1.5 mycroft alvecp = NULL;
1299 1.5 mycroft return('\n');
1300 1.1 cgd }
1301 1.1 cgd }
1302 1.1 cgd if (alvec) {
1303 1.5 mycroft if ((alvecp = *alvec) != '\0') {
1304 1.1 cgd alvec++;
1305 1.1 cgd goto top;
1306 1.1 cgd }
1307 1.1 cgd /* Infinite source! */
1308 1.1 cgd return ('\n');
1309 1.1 cgd }
1310 1.1 cgd if (evalp) {
1311 1.5 mycroft aret = E_SEEK;
1312 1.5 mycroft if ((c = *evalp++) != '\0')
1313 1.1 cgd return (c);
1314 1.5 mycroft if (evalvec && *evalvec) {
1315 1.1 cgd evalp = *evalvec++;
1316 1.1 cgd return (' ');
1317 1.1 cgd }
1318 1.5 mycroft aret = F_SEEK;
1319 1.1 cgd evalp = 0;
1320 1.1 cgd }
1321 1.1 cgd if (evalvec) {
1322 1.1 cgd if (evalvec == (Char **) 1) {
1323 1.1 cgd doneinp = 1;
1324 1.1 cgd reset();
1325 1.1 cgd }
1326 1.5 mycroft if ((evalp = *evalvec) != '\0') {
1327 1.1 cgd evalvec++;
1328 1.1 cgd goto top;
1329 1.1 cgd }
1330 1.1 cgd evalvec = (Char **) 1;
1331 1.1 cgd return ('\n');
1332 1.1 cgd }
1333 1.1 cgd do {
1334 1.1 cgd if (arginp == (Char *) 1 || onelflg == 1) {
1335 1.1 cgd if (wanteof)
1336 1.1 cgd return (-1);
1337 1.1 cgd exitstat();
1338 1.1 cgd }
1339 1.1 cgd if (arginp) {
1340 1.1 cgd if ((c = *arginp++) == 0) {
1341 1.1 cgd arginp = (Char *) 1;
1342 1.1 cgd return ('\n');
1343 1.1 cgd }
1344 1.1 cgd return (c);
1345 1.1 cgd }
1346 1.1 cgd reread:
1347 1.1 cgd c = bgetc();
1348 1.1 cgd if (c < 0) {
1349 1.1 cgd struct termios tty;
1350 1.1 cgd if (wanteof)
1351 1.1 cgd return (-1);
1352 1.1 cgd /* was isatty but raw with ignoreeof yields problems */
1353 1.1 cgd if (tcgetattr(SHIN, &tty) == 0 && (tty.c_lflag & ICANON))
1354 1.1 cgd {
1355 1.1 cgd /* was 'short' for FILEC */
1356 1.8 mycroft pid_t ctpgrp;
1357 1.1 cgd
1358 1.1 cgd if (++sincereal > 25)
1359 1.1 cgd goto oops;
1360 1.1 cgd if (tpgrp != -1 &&
1361 1.1 cgd (ctpgrp = tcgetpgrp(FSHTTY)) != -1 &&
1362 1.1 cgd tpgrp != ctpgrp) {
1363 1.19 wiz (void)tcsetpgrp(FSHTTY, tpgrp);
1364 1.19 wiz (void)kill(-ctpgrp, SIGHUP);
1365 1.19 wiz (void)fprintf(csherr, "Reset tty pgrp from %ld to %ld\n",
1366 1.16 christos (long)ctpgrp, (long)tpgrp);
1367 1.1 cgd goto reread;
1368 1.1 cgd }
1369 1.1 cgd if (adrof(STRignoreeof)) {
1370 1.1 cgd if (loginsh)
1371 1.19 wiz (void)fprintf(csherr,"\nUse \"logout\" to logout.\n");
1372 1.1 cgd else
1373 1.19 wiz (void)fprintf(csherr,"\nUse \"exit\" to leave csh.\n");
1374 1.1 cgd reset();
1375 1.1 cgd }
1376 1.1 cgd if (chkstop == 0)
1377 1.1 cgd panystop(1);
1378 1.1 cgd }
1379 1.1 cgd oops:
1380 1.1 cgd doneinp = 1;
1381 1.1 cgd reset();
1382 1.1 cgd }
1383 1.1 cgd sincereal = 0;
1384 1.1 cgd if (c == '\n' && onelflg)
1385 1.1 cgd onelflg--;
1386 1.1 cgd } while (c == 0);
1387 1.1 cgd return (c);
1388 1.1 cgd }
1389 1.1 cgd
1390 1.1 cgd static int
1391 1.19 wiz bgetc(void)
1392 1.1 cgd {
1393 1.1 cgd #ifdef FILEC
1394 1.19 wiz char tbuf[BUFSIZE + 1];
1395 1.19 wiz Char ttyline[BUFSIZE];
1396 1.19 wiz int c, buf, numleft, off, roomleft;
1397 1.19 wiz
1398 1.19 wiz numleft = 0;
1399 1.19 wiz #else /* FILEC */
1400 1.19 wiz char tbuf[BUFSIZE + 1];
1401 1.19 wiz int c, buf, off;
1402 1.19 wiz #endif /* !FILEC */
1403 1.1 cgd
1404 1.1 cgd if (cantell) {
1405 1.1 cgd if (fseekp < fbobp || fseekp > feobp) {
1406 1.1 cgd fbobp = feobp = fseekp;
1407 1.19 wiz (void)lseek(SHIN, fseekp, SEEK_SET);
1408 1.1 cgd }
1409 1.1 cgd if (fseekp == feobp) {
1410 1.19 wiz int i;
1411 1.1 cgd
1412 1.1 cgd fbobp = feobp;
1413 1.1 cgd do
1414 1.17 christos c = read(SHIN, tbuf, BUFSIZE);
1415 1.1 cgd while (c < 0 && errno == EINTR);
1416 1.1 cgd if (c <= 0)
1417 1.1 cgd return (-1);
1418 1.1 cgd for (i = 0; i < c; i++)
1419 1.1 cgd fbuf[0][i] = (unsigned char) tbuf[i];
1420 1.1 cgd feobp += c;
1421 1.1 cgd }
1422 1.1 cgd c = fbuf[0][fseekp - fbobp];
1423 1.1 cgd fseekp++;
1424 1.1 cgd return (c);
1425 1.1 cgd }
1426 1.1 cgd
1427 1.1 cgd again:
1428 1.17 christos buf = (int) fseekp / BUFSIZE;
1429 1.1 cgd if (buf >= fblocks) {
1430 1.19 wiz Char **nfbuf;
1431 1.1 cgd
1432 1.19 wiz nfbuf = (Char **)xcalloc((size_t) (fblocks + 2), sizeof(char **));
1433 1.1 cgd if (fbuf) {
1434 1.19 wiz (void)blkcpy(nfbuf, fbuf);
1435 1.1 cgd xfree((ptr_t) fbuf);
1436 1.1 cgd }
1437 1.1 cgd fbuf = nfbuf;
1438 1.19 wiz fbuf[fblocks] = (Char *)xcalloc(BUFSIZE, sizeof(Char));
1439 1.1 cgd fblocks++;
1440 1.1 cgd if (!intty)
1441 1.1 cgd goto again;
1442 1.1 cgd }
1443 1.1 cgd if (fseekp >= feobp) {
1444 1.17 christos buf = (int) feobp / BUFSIZE;
1445 1.17 christos off = (int) feobp % BUFSIZE;
1446 1.17 christos roomleft = BUFSIZE - off;
1447 1.1 cgd
1448 1.1 cgd #ifdef FILEC
1449 1.17 christos roomleft = BUFSIZE - off;
1450 1.1 cgd for (;;) {
1451 1.1 cgd if (filec && intty) {
1452 1.17 christos c = numleft ? numleft : tenex(ttyline, BUFSIZE);
1453 1.1 cgd if (c > roomleft) {
1454 1.1 cgd /* start with fresh buffer */
1455 1.17 christos feobp = fseekp = fblocks * BUFSIZE;
1456 1.1 cgd numleft = c;
1457 1.1 cgd goto again;
1458 1.1 cgd }
1459 1.1 cgd if (c > 0)
1460 1.19 wiz (void)memcpy(fbuf[buf] + off, ttyline, c * sizeof(Char));
1461 1.1 cgd numleft = 0;
1462 1.1 cgd }
1463 1.1 cgd else {
1464 1.1 cgd #endif
1465 1.1 cgd c = read(SHIN, tbuf, roomleft);
1466 1.1 cgd if (c > 0) {
1467 1.1 cgd int i;
1468 1.1 cgd Char *ptr = fbuf[buf] + off;
1469 1.1 cgd
1470 1.1 cgd for (i = 0; i < c; i++)
1471 1.1 cgd ptr[i] = (unsigned char) tbuf[i];
1472 1.1 cgd }
1473 1.1 cgd #ifdef FILEC
1474 1.1 cgd }
1475 1.1 cgd #endif
1476 1.1 cgd if (c >= 0)
1477 1.1 cgd break;
1478 1.1 cgd if (errno == EWOULDBLOCK) {
1479 1.20 lukem int iooff = 0;
1480 1.1 cgd
1481 1.20 lukem (void)ioctl(SHIN, FIONBIO, (ioctl_t) & iooff);
1482 1.1 cgd }
1483 1.1 cgd else if (errno != EINTR)
1484 1.1 cgd break;
1485 1.1 cgd }
1486 1.1 cgd if (c <= 0)
1487 1.1 cgd return (-1);
1488 1.1 cgd feobp += c;
1489 1.1 cgd #ifndef FILEC
1490 1.1 cgd goto again;
1491 1.1 cgd #else
1492 1.1 cgd if (filec && !intty)
1493 1.1 cgd goto again;
1494 1.1 cgd #endif
1495 1.1 cgd }
1496 1.19 wiz c = fbuf[buf][(int)fseekp % BUFSIZE];
1497 1.1 cgd fseekp++;
1498 1.1 cgd return (c);
1499 1.1 cgd }
1500 1.1 cgd
1501 1.1 cgd static void
1502 1.19 wiz bfree(void)
1503 1.1 cgd {
1504 1.19 wiz int i, sb;
1505 1.1 cgd
1506 1.1 cgd if (cantell)
1507 1.1 cgd return;
1508 1.1 cgd if (whyles)
1509 1.1 cgd return;
1510 1.19 wiz sb = (int)(fseekp - 1) / BUFSIZE;
1511 1.1 cgd if (sb > 0) {
1512 1.1 cgd for (i = 0; i < sb; i++)
1513 1.1 cgd xfree((ptr_t) fbuf[i]);
1514 1.19 wiz (void)blkcpy(fbuf, &fbuf[sb]);
1515 1.17 christos fseekp -= BUFSIZE * sb;
1516 1.17 christos feobp -= BUFSIZE * sb;
1517 1.1 cgd fblocks -= sb;
1518 1.1 cgd }
1519 1.1 cgd }
1520 1.1 cgd
1521 1.1 cgd void
1522 1.19 wiz bseek(struct Ain *l)
1523 1.5 mycroft {
1524 1.5 mycroft switch (aret = l->type) {
1525 1.19 wiz case A_SEEK:
1526 1.19 wiz alvec = l->a_seek;
1527 1.19 wiz alvecp = l->c_seek;
1528 1.19 wiz return;
1529 1.5 mycroft case E_SEEK:
1530 1.5 mycroft evalvec = l->a_seek;
1531 1.5 mycroft evalp = l->c_seek;
1532 1.5 mycroft return;
1533 1.5 mycroft case F_SEEK:
1534 1.5 mycroft fseekp = l->f_seek;
1535 1.5 mycroft return;
1536 1.5 mycroft default:
1537 1.19 wiz (void)fprintf(csherr, "Bad seek type %d\n", aret);
1538 1.5 mycroft abort();
1539 1.5 mycroft }
1540 1.5 mycroft }
1541 1.1 cgd
1542 1.5 mycroft void
1543 1.19 wiz btell(struct Ain *l)
1544 1.1 cgd {
1545 1.5 mycroft switch (l->type = aret) {
1546 1.19 wiz case A_SEEK:
1547 1.19 wiz l->a_seek = alvec;
1548 1.19 wiz l->c_seek = alvecp;
1549 1.19 wiz return;
1550 1.5 mycroft case E_SEEK:
1551 1.5 mycroft l->a_seek = evalvec;
1552 1.5 mycroft l->c_seek = evalp;
1553 1.5 mycroft return;
1554 1.5 mycroft case F_SEEK:
1555 1.5 mycroft l->f_seek = fseekp;
1556 1.5 mycroft l->a_seek = NULL;
1557 1.5 mycroft return;
1558 1.5 mycroft default:
1559 1.19 wiz (void)fprintf(csherr, "Bad seek type %d\n", aret);
1560 1.5 mycroft abort();
1561 1.1 cgd }
1562 1.1 cgd }
1563 1.1 cgd
1564 1.1 cgd void
1565 1.19 wiz btoeof(void)
1566 1.1 cgd {
1567 1.19 wiz (void)lseek(SHIN, (off_t) 0, SEEK_END);
1568 1.5 mycroft aret = F_SEEK;
1569 1.1 cgd fseekp = feobp;
1570 1.5 mycroft alvec = NULL;
1571 1.5 mycroft alvecp = NULL;
1572 1.5 mycroft evalvec = NULL;
1573 1.5 mycroft evalp = NULL;
1574 1.1 cgd wfree();
1575 1.1 cgd bfree();
1576 1.1 cgd }
1577 1.1 cgd
1578 1.1 cgd void
1579 1.19 wiz settell(void)
1580 1.1 cgd {
1581 1.1 cgd cantell = 0;
1582 1.1 cgd if (arginp || onelflg || intty)
1583 1.1 cgd return;
1584 1.9 jtc if (lseek(SHIN, (off_t) 0, SEEK_CUR) < 0 || errno == ESPIPE)
1585 1.1 cgd return;
1586 1.19 wiz fbuf = (Char **)xcalloc(2, sizeof(Char **));
1587 1.1 cgd fblocks = 1;
1588 1.19 wiz fbuf[0] = (Char *)xcalloc(BUFSIZE, sizeof(Char));
1589 1.9 jtc fseekp = fbobp = feobp = lseek(SHIN, (off_t) 0, SEEK_CUR);
1590 1.1 cgd cantell = 1;
1591 1.1 cgd }
1592