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