17c478bd9Sstevel@tonic-gate /*
270a587ddSchin * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
37c478bd9Sstevel@tonic-gate * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate */
57c478bd9Sstevel@tonic-gate
67c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
77c478bd9Sstevel@tonic-gate /* All Rights Reserved */
87c478bd9Sstevel@tonic-gate
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California.
117c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley Software License Agreement
127c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate */
147c478bd9Sstevel@tonic-gate
157c478bd9Sstevel@tonic-gate #include <unistd.h> /* for lseek prototype */
167c478bd9Sstevel@tonic-gate #include "sh.h"
177c478bd9Sstevel@tonic-gate #include "sh.tconst.h"
187c478bd9Sstevel@tonic-gate #include <sys/filio.h>
197c478bd9Sstevel@tonic-gate #include <sys/ttold.h>
2065b0c20eSnakanon #define RAW O_RAW
217c478bd9Sstevel@tonic-gate /*
227c478bd9Sstevel@tonic-gate * C shell
237c478bd9Sstevel@tonic-gate */
247c478bd9Sstevel@tonic-gate
25*258f91c6SToomas Soome bool justpr;
26*258f91c6SToomas Soome static int lastev;
27*258f91c6SToomas Soome int onelflg;
28*258f91c6SToomas Soome tchar **alvec;
29*258f91c6SToomas Soome struct wordent *alhistp;
30*258f91c6SToomas Soome struct wordent *alhistt;
31*258f91c6SToomas Soome struct wordent paraml;
32*258f91c6SToomas Soome
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate * These lexical routines read input and form lists of words.
357c478bd9Sstevel@tonic-gate * There is some involved processing here, because of the complications
367c478bd9Sstevel@tonic-gate * of input buffering, and especially because of history substitution.
377c478bd9Sstevel@tonic-gate */
387c478bd9Sstevel@tonic-gate
396c02b4a4Smuffin tchar *word(void);
406c02b4a4Smuffin tchar getC1(int);
416c02b4a4Smuffin tchar *subword(tchar *, int, bool *);
426c02b4a4Smuffin void getdol(void);
436c02b4a4Smuffin void addla(tchar *);
446c02b4a4Smuffin void getexcl(tchar);
456c02b4a4Smuffin void noev(tchar *);
466c02b4a4Smuffin void setexclp(tchar *);
476c02b4a4Smuffin void unreadc(tchar);
486c02b4a4Smuffin int readc(bool);
496c02b4a4Smuffin struct wordent *dosub(int, struct wordent *, bool);
506c02b4a4Smuffin struct Hist *findev(tchar *, bool);
516c02b4a4Smuffin struct wordent *gethent(int);
526c02b4a4Smuffin struct wordent *getsub(struct wordent *);
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate * Peekc is a peek characer for getC, peekread for readc.
567c478bd9Sstevel@tonic-gate * There is a subtlety here in many places... history routines
577c478bd9Sstevel@tonic-gate * will read ahead and then insert stuff into the input stream.
587c478bd9Sstevel@tonic-gate * If they push back a character then they must push it behind
597c478bd9Sstevel@tonic-gate * the text substituted by the history substitution. On the other
607c478bd9Sstevel@tonic-gate * hand in several places we need 2 peek characters. To make this
617c478bd9Sstevel@tonic-gate * all work, the history routines read with getC, and make use both
627c478bd9Sstevel@tonic-gate * of ungetC and unreadc. The key observation is that the state
637c478bd9Sstevel@tonic-gate * of getC at the call of a history reference is such that calls
647c478bd9Sstevel@tonic-gate * to getC from the history routines will always yield calls of
657c478bd9Sstevel@tonic-gate * readc, unless this peeking is involved. That is to say that during
667c478bd9Sstevel@tonic-gate * getexcl the variables lap, exclp, and exclnxt are all zero.
677c478bd9Sstevel@tonic-gate *
687c478bd9Sstevel@tonic-gate * Getdol invokes history substitution, hence the extra peek, peekd,
697c478bd9Sstevel@tonic-gate * which it can ungetD to be before history substitutions.
707c478bd9Sstevel@tonic-gate */
717c478bd9Sstevel@tonic-gate tchar peekc, peekd;
727c478bd9Sstevel@tonic-gate tchar peekread;
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate tchar *exclp; /* (Tail of) current word from ! subst */
757c478bd9Sstevel@tonic-gate struct wordent *exclnxt; /* The rest of the ! subst words */
767c478bd9Sstevel@tonic-gate int exclc; /* Count of remainig words in ! subst */
777c478bd9Sstevel@tonic-gate tchar *alvecp; /* "Globp" for alias resubstitution */
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate * Lex returns to its caller not only a wordlist (as a "var" parameter)
817c478bd9Sstevel@tonic-gate * but also whether a history substitution occurred. This is used in
827c478bd9Sstevel@tonic-gate * the main (process) routine to determine whether to echo, and also
837c478bd9Sstevel@tonic-gate * when called by the alias routine to determine whether to keep the
847c478bd9Sstevel@tonic-gate * argument list.
857c478bd9Sstevel@tonic-gate */
867c478bd9Sstevel@tonic-gate bool hadhist;
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate tchar getCtmp;
8965b0c20eSnakanon #define getC(f) ((getCtmp = peekc) ? (peekc = 0, getCtmp) : getC1(f))
907c478bd9Sstevel@tonic-gate #define ungetC(c) peekc = c
917c478bd9Sstevel@tonic-gate #define ungetD(c) peekd = c
927c478bd9Sstevel@tonic-gate
936c02b4a4Smuffin bool
lex(struct wordent * hp)946c02b4a4Smuffin lex(struct wordent *hp)
957c478bd9Sstevel@tonic-gate {
966c02b4a4Smuffin struct wordent *wdp;
977c478bd9Sstevel@tonic-gate int c;
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate #ifdef TRACE
1007c478bd9Sstevel@tonic-gate tprintf("TRACE- lex()\n");
1017c478bd9Sstevel@tonic-gate #endif
1027c478bd9Sstevel@tonic-gate lineloc = btell();
1037c478bd9Sstevel@tonic-gate hp->next = hp->prev = hp;
10465b0c20eSnakanon hp->word = S_ /* "" */;
1057c478bd9Sstevel@tonic-gate alvecp = 0, hadhist = 0;
1067c478bd9Sstevel@tonic-gate do
1077c478bd9Sstevel@tonic-gate c = readc(0);
1087c478bd9Sstevel@tonic-gate while (issp(c));
1097c478bd9Sstevel@tonic-gate /* make sure history is enabled */
1107c478bd9Sstevel@tonic-gate if (HIST && c == HISTSUB && intty)
1117c478bd9Sstevel@tonic-gate /* ^lef^rit from tty is short !:s^lef^rit */
1127c478bd9Sstevel@tonic-gate getexcl(c);
1137c478bd9Sstevel@tonic-gate else
1147c478bd9Sstevel@tonic-gate unreadc(c);
1157c478bd9Sstevel@tonic-gate wdp = hp;
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate * The following loop is written so that the links needed
1187c478bd9Sstevel@tonic-gate * by freelex will be ready and rarin to go even if it is
1197c478bd9Sstevel@tonic-gate * interrupted.
1207c478bd9Sstevel@tonic-gate */
1217c478bd9Sstevel@tonic-gate do {
12265b0c20eSnakanon struct wordent *new = (struct wordent *)xalloc(sizeof *wdp);
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate new->word = 0;
1257c478bd9Sstevel@tonic-gate new->prev = wdp;
1267c478bd9Sstevel@tonic-gate new->next = hp;
1277c478bd9Sstevel@tonic-gate wdp->next = new;
1287c478bd9Sstevel@tonic-gate wdp = new;
1297c478bd9Sstevel@tonic-gate wdp->word = word();
1307c478bd9Sstevel@tonic-gate } while (wdp->word[0] != '\n');
1317c478bd9Sstevel@tonic-gate #ifdef TRACE
1327c478bd9Sstevel@tonic-gate tprintf("Exiting lex()\n");
1337c478bd9Sstevel@tonic-gate #endif
1347c478bd9Sstevel@tonic-gate hp->prev = wdp;
1357c478bd9Sstevel@tonic-gate return (hadhist);
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate
1386c02b4a4Smuffin void
prlex(struct wordent * sp0)1396c02b4a4Smuffin prlex(struct wordent *sp0)
1407c478bd9Sstevel@tonic-gate {
1416c02b4a4Smuffin struct wordent *sp = sp0->next;
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate #ifdef TRACE
1447c478bd9Sstevel@tonic-gate tprintf("TRACE- prlex()\n");
1457c478bd9Sstevel@tonic-gate #endif
1467c478bd9Sstevel@tonic-gate for (;;) {
1477c478bd9Sstevel@tonic-gate printf("%t", sp->word);
1487c478bd9Sstevel@tonic-gate sp = sp->next;
1497c478bd9Sstevel@tonic-gate if (sp == sp0)
1507c478bd9Sstevel@tonic-gate break;
1517c478bd9Sstevel@tonic-gate if (sp->word[0] != '\n')
1527c478bd9Sstevel@tonic-gate Putchar(' ');
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate
1566c02b4a4Smuffin void
copylex(struct wordent * hp,struct wordent * fp)1576c02b4a4Smuffin copylex(struct wordent *hp, struct wordent *fp)
1587c478bd9Sstevel@tonic-gate {
1596c02b4a4Smuffin struct wordent *wdp;
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate #ifdef TRACE
1627c478bd9Sstevel@tonic-gate tprintf("TRACE- copylex()\n");
1637c478bd9Sstevel@tonic-gate #endif
1647c478bd9Sstevel@tonic-gate wdp = hp;
1657c478bd9Sstevel@tonic-gate fp = fp->next;
1667c478bd9Sstevel@tonic-gate do {
16765b0c20eSnakanon struct wordent *new = (struct wordent *)xalloc(sizeof *wdp);
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate new->prev = wdp;
1707c478bd9Sstevel@tonic-gate new->next = hp;
1717c478bd9Sstevel@tonic-gate wdp->next = new;
1727c478bd9Sstevel@tonic-gate wdp = new;
1737c478bd9Sstevel@tonic-gate wdp->word = savestr(fp->word);
1747c478bd9Sstevel@tonic-gate fp = fp->next;
1757c478bd9Sstevel@tonic-gate } while (wdp->word[0] != '\n');
1767c478bd9Sstevel@tonic-gate hp->prev = wdp;
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate
1796c02b4a4Smuffin void
freelex(struct wordent * vp)1806c02b4a4Smuffin freelex(struct wordent *vp)
1817c478bd9Sstevel@tonic-gate {
1826c02b4a4Smuffin struct wordent *fp;
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate #ifdef TRACE
1857c478bd9Sstevel@tonic-gate tprintf("TRACE- freelex()\n");
1867c478bd9Sstevel@tonic-gate #endif
1877c478bd9Sstevel@tonic-gate while (vp->next != vp) {
1887c478bd9Sstevel@tonic-gate fp = vp->next;
1897c478bd9Sstevel@tonic-gate vp->next = fp->next;
19065b0c20eSnakanon xfree(fp->word);
19165b0c20eSnakanon xfree(fp);
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate vp->prev = vp;
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate tchar *
word(void)1976c02b4a4Smuffin word(void)
1987c478bd9Sstevel@tonic-gate {
1996c02b4a4Smuffin tchar c, c1;
2006c02b4a4Smuffin tchar *wp;
2017c478bd9Sstevel@tonic-gate tchar wbuf[BUFSIZ];
2026c02b4a4Smuffin bool dolflg;
2036c02b4a4Smuffin int i;
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate #ifdef TRACE
2067c478bd9Sstevel@tonic-gate tprintf("TRACE- word()\n");
2077c478bd9Sstevel@tonic-gate #endif
2087c478bd9Sstevel@tonic-gate wp = wbuf;
2097c478bd9Sstevel@tonic-gate i = BUFSIZ - 4;
2107c478bd9Sstevel@tonic-gate loop:
2117c478bd9Sstevel@tonic-gate while (issp(c = getC(DOALL)))
2127c478bd9Sstevel@tonic-gate ;
2137c478bd9Sstevel@tonic-gate if (cmap(c, _META|_ESC)||isauxsp(c))
2147c478bd9Sstevel@tonic-gate switch (c) {
2157c478bd9Sstevel@tonic-gate case '&':
2167c478bd9Sstevel@tonic-gate case '|':
2177c478bd9Sstevel@tonic-gate case '<':
2187c478bd9Sstevel@tonic-gate case '>':
2197c478bd9Sstevel@tonic-gate *wp++ = c;
2207c478bd9Sstevel@tonic-gate c1 = getC(DOALL);
2217c478bd9Sstevel@tonic-gate if (c1 == c)
2227c478bd9Sstevel@tonic-gate *wp++ = c1;
2237c478bd9Sstevel@tonic-gate else
2247c478bd9Sstevel@tonic-gate ungetC(c1);
2257c478bd9Sstevel@tonic-gate goto ret;
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate case '#':
2287c478bd9Sstevel@tonic-gate if (intty)
2297c478bd9Sstevel@tonic-gate break;
2307c478bd9Sstevel@tonic-gate c = 0;
2317c478bd9Sstevel@tonic-gate do {
2327c478bd9Sstevel@tonic-gate c1 = c;
2337c478bd9Sstevel@tonic-gate c = getC(0);
2347c478bd9Sstevel@tonic-gate } while (c != '\n');
2357c478bd9Sstevel@tonic-gate if (c1 == '\\')
2367c478bd9Sstevel@tonic-gate goto loop;
2377c478bd9Sstevel@tonic-gate /* fall into ... */
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate case ';':
2407c478bd9Sstevel@tonic-gate case '(':
2417c478bd9Sstevel@tonic-gate case ')':
2427c478bd9Sstevel@tonic-gate case '\n':
2437c478bd9Sstevel@tonic-gate *wp++ = c;
2447c478bd9Sstevel@tonic-gate goto ret;
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate case '\\':
2477c478bd9Sstevel@tonic-gate c = getC(0);
2487c478bd9Sstevel@tonic-gate if (c == '\n') {
2497c478bd9Sstevel@tonic-gate if (onelflg == 1)
2507c478bd9Sstevel@tonic-gate onelflg = 2;
2517c478bd9Sstevel@tonic-gate goto loop;
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate if (c != HIST)
2547c478bd9Sstevel@tonic-gate *wp++ = '\\', --i;
2557c478bd9Sstevel@tonic-gate c |= QUOTE;
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate c1 = 0;
2587c478bd9Sstevel@tonic-gate dolflg = DOALL;
2597c478bd9Sstevel@tonic-gate for (;;) {
2607c478bd9Sstevel@tonic-gate if (c1) {
2617c478bd9Sstevel@tonic-gate if (c == c1) {
2627c478bd9Sstevel@tonic-gate c1 = 0;
2637c478bd9Sstevel@tonic-gate dolflg = DOALL;
2647c478bd9Sstevel@tonic-gate } else if (c == '\\') {
2657c478bd9Sstevel@tonic-gate c = getC(0);
2667c478bd9Sstevel@tonic-gate if (c == HIST)
2677c478bd9Sstevel@tonic-gate c |= QUOTE;
2687c478bd9Sstevel@tonic-gate else {
2697c478bd9Sstevel@tonic-gate if (c == '\n')
27065b0c20eSnakanon #if 0
2717c478bd9Sstevel@tonic-gate if (c1 == '`')
2727c478bd9Sstevel@tonic-gate c = ' ';
2737c478bd9Sstevel@tonic-gate else
27465b0c20eSnakanon #endif
2757c478bd9Sstevel@tonic-gate c |= QUOTE;
2767c478bd9Sstevel@tonic-gate ungetC(c);
2777c478bd9Sstevel@tonic-gate c = '\\';
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate } else if (c == '\n') {
2807c478bd9Sstevel@tonic-gate seterrc(gettext("Unmatched "), c1);
2817c478bd9Sstevel@tonic-gate ungetC(c);
2827c478bd9Sstevel@tonic-gate break;
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate } else if (cmap(c, _META|_Q|_Q1|_ESC)||isauxsp(c)) {
2857c478bd9Sstevel@tonic-gate if (c == '\\') {
2867c478bd9Sstevel@tonic-gate c = getC(0);
2877c478bd9Sstevel@tonic-gate if (c == '\n') {
2887c478bd9Sstevel@tonic-gate if (onelflg == 1)
2897c478bd9Sstevel@tonic-gate onelflg = 2;
2907c478bd9Sstevel@tonic-gate break;
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate if (c != HIST)
2937c478bd9Sstevel@tonic-gate *wp++ = '\\', --i;
2947c478bd9Sstevel@tonic-gate c |= QUOTE;
2957c478bd9Sstevel@tonic-gate } else if (cmap(c, _Q|_Q1)) { /* '"` */
2967c478bd9Sstevel@tonic-gate c1 = c;
2977c478bd9Sstevel@tonic-gate dolflg = c == '"' ? DOALL : DOEXCL;
2987c478bd9Sstevel@tonic-gate } else if (c != '#' || !intty) {
2997c478bd9Sstevel@tonic-gate ungetC(c);
3007c478bd9Sstevel@tonic-gate break;
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate if (--i > 0) {
3047c478bd9Sstevel@tonic-gate *wp++ = c;
3057c478bd9Sstevel@tonic-gate c = getC(dolflg);
3067c478bd9Sstevel@tonic-gate } else {
3077c478bd9Sstevel@tonic-gate seterr("Word too long");
3087c478bd9Sstevel@tonic-gate wp = &wbuf[1];
3097c478bd9Sstevel@tonic-gate break;
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate ret:
3137c478bd9Sstevel@tonic-gate *wp = 0;
3147c478bd9Sstevel@tonic-gate #ifdef TRACE
3157c478bd9Sstevel@tonic-gate tprintf("word() returning:%t\n", wbuf);
3167c478bd9Sstevel@tonic-gate #endif
3177c478bd9Sstevel@tonic-gate return (savestr(wbuf));
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate
3206c02b4a4Smuffin tchar
getC1(int flag)3216c02b4a4Smuffin getC1(int flag)
3227c478bd9Sstevel@tonic-gate {
3236c02b4a4Smuffin tchar c;
3247c478bd9Sstevel@tonic-gate
3257c478bd9Sstevel@tonic-gate top:
3267c478bd9Sstevel@tonic-gate if (c = peekc) {
3277c478bd9Sstevel@tonic-gate peekc = 0;
3287c478bd9Sstevel@tonic-gate return (c);
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate if (lap) {
3317c478bd9Sstevel@tonic-gate if ((c = *lap++) == 0)
3327c478bd9Sstevel@tonic-gate lap = 0;
3337c478bd9Sstevel@tonic-gate else {
3347c478bd9Sstevel@tonic-gate /*
33565b0c20eSnakanon * don't quote things if there was an error (err!=0)
33665b0c20eSnakanon * the input is original, not from a substitution and
33765b0c20eSnakanon * therefore should not be quoted
3387c478bd9Sstevel@tonic-gate */
339*258f91c6SToomas Soome if (!err_msg && cmap(c, _META|_Q|_Q1)||isauxsp(c))
3407c478bd9Sstevel@tonic-gate c |= QUOTE;
3417c478bd9Sstevel@tonic-gate return (c);
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate if (c = peekd) {
3457c478bd9Sstevel@tonic-gate peekd = 0;
3467c478bd9Sstevel@tonic-gate return (c);
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate if (exclp) {
3497c478bd9Sstevel@tonic-gate if (c = *exclp++)
3507c478bd9Sstevel@tonic-gate return (c);
3517c478bd9Sstevel@tonic-gate if (exclnxt && --exclc >= 0) {
3527c478bd9Sstevel@tonic-gate exclnxt = exclnxt->next;
3537c478bd9Sstevel@tonic-gate setexclp(exclnxt->word);
3547c478bd9Sstevel@tonic-gate return (' ');
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate exclp = 0;
3577c478bd9Sstevel@tonic-gate exclnxt = 0;
3587c478bd9Sstevel@tonic-gate }
3597c478bd9Sstevel@tonic-gate if (exclnxt) {
3607c478bd9Sstevel@tonic-gate exclnxt = exclnxt->next;
3617c478bd9Sstevel@tonic-gate if (--exclc < 0)
3627c478bd9Sstevel@tonic-gate exclnxt = 0;
3637c478bd9Sstevel@tonic-gate else
3647c478bd9Sstevel@tonic-gate setexclp(exclnxt->word);
3657c478bd9Sstevel@tonic-gate goto top;
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate c = readc(0);
3687c478bd9Sstevel@tonic-gate if (c == '$' && (flag & DODOL)) {
3697c478bd9Sstevel@tonic-gate getdol();
3707c478bd9Sstevel@tonic-gate goto top;
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate if (c == HIST && (flag & DOEXCL)) {
3737c478bd9Sstevel@tonic-gate getexcl(0);
3747c478bd9Sstevel@tonic-gate goto top;
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate return (c);
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate
3796c02b4a4Smuffin void
getdol(void)3806c02b4a4Smuffin getdol(void)
3817c478bd9Sstevel@tonic-gate {
3826c02b4a4Smuffin tchar *np, *p;
3837c478bd9Sstevel@tonic-gate tchar name[MAX_VREF_LEN];
3846c02b4a4Smuffin int c;
3857c478bd9Sstevel@tonic-gate int sc;
3867c478bd9Sstevel@tonic-gate bool special = 0;
3877c478bd9Sstevel@tonic-gate
3887c478bd9Sstevel@tonic-gate #ifdef TRACE
3897c478bd9Sstevel@tonic-gate tprintf("TRACE- getdol()\n");
3907c478bd9Sstevel@tonic-gate #endif
3917c478bd9Sstevel@tonic-gate np = name, *np++ = '$';
3927c478bd9Sstevel@tonic-gate c = sc = getC(DOEXCL);
3937c478bd9Sstevel@tonic-gate if (isspnl(c)) {
3947c478bd9Sstevel@tonic-gate ungetD(c);
3957c478bd9Sstevel@tonic-gate ungetC('$' | QUOTE);
3967c478bd9Sstevel@tonic-gate return;
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate if (c == '{')
3997c478bd9Sstevel@tonic-gate *np++ = c, c = getC(DOEXCL);
4007c478bd9Sstevel@tonic-gate if (c == '#' || c == '?')
4017c478bd9Sstevel@tonic-gate special++, *np++ = c, c = getC(DOEXCL);
4027c478bd9Sstevel@tonic-gate *np++ = c;
4037c478bd9Sstevel@tonic-gate switch (c) {
40465b0c20eSnakanon
4057c478bd9Sstevel@tonic-gate case '<':
4067c478bd9Sstevel@tonic-gate case '$':
4077c478bd9Sstevel@tonic-gate case '*':
4087c478bd9Sstevel@tonic-gate if (special)
4097c478bd9Sstevel@tonic-gate goto vsyn;
4107c478bd9Sstevel@tonic-gate goto ret;
4117c478bd9Sstevel@tonic-gate
4127c478bd9Sstevel@tonic-gate case '\n':
4137c478bd9Sstevel@tonic-gate ungetD(c);
4147c478bd9Sstevel@tonic-gate np--;
4157c478bd9Sstevel@tonic-gate goto vsyn;
4167c478bd9Sstevel@tonic-gate
4177c478bd9Sstevel@tonic-gate default:
4187c478bd9Sstevel@tonic-gate p = np;
4197c478bd9Sstevel@tonic-gate if (digit(c)) {
4207c478bd9Sstevel@tonic-gate /* make sure the variable names are MAX_VAR_LEN chars or less */
42165b0c20eSnakanon while (digit(c = getC(DOEXCL)) && (np - p) < MAX_VAR_LEN) {
4227c478bd9Sstevel@tonic-gate *np++ = c;
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate } else if (letter(c)) {
4257c478bd9Sstevel@tonic-gate while ((letter(c = getC(DOEXCL)) || digit(c)) &&
42665b0c20eSnakanon (np - p) < MAX_VAR_LEN) {
4277c478bd9Sstevel@tonic-gate *np++ = c;
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate else
4317c478bd9Sstevel@tonic-gate goto vsyn;
4327c478bd9Sstevel@tonic-gate
43365b0c20eSnakanon if ((np - p) > MAX_VAR_LEN)
4347c478bd9Sstevel@tonic-gate {
4357c478bd9Sstevel@tonic-gate seterr("Variable name too long");
4367c478bd9Sstevel@tonic-gate goto ret;
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate }
4397c478bd9Sstevel@tonic-gate if (c == '[') {
4407c478bd9Sstevel@tonic-gate *np++ = c;
4417c478bd9Sstevel@tonic-gate do {
4427c478bd9Sstevel@tonic-gate c = getC(DOEXCL);
4437c478bd9Sstevel@tonic-gate if (c == '\n') {
4447c478bd9Sstevel@tonic-gate ungetD(c);
4457c478bd9Sstevel@tonic-gate np--;
4467c478bd9Sstevel@tonic-gate goto vsyn;
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate /* need to leave space for possible modifiers */
4497c478bd9Sstevel@tonic-gate if (np >= &name[MAX_VREF_LEN - 8])
4507c478bd9Sstevel@tonic-gate {
4517c478bd9Sstevel@tonic-gate seterr("Variable reference too long");
4527c478bd9Sstevel@tonic-gate goto ret;
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate *np++ = c;
4557c478bd9Sstevel@tonic-gate } while (c != ']');
4567c478bd9Sstevel@tonic-gate c = getC(DOEXCL);
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate if (c == ':') {
4597c478bd9Sstevel@tonic-gate *np++ = c, c = getC(DOEXCL);
4607c478bd9Sstevel@tonic-gate if (c == 'g')
4617c478bd9Sstevel@tonic-gate *np++ = c, c = getC(DOEXCL);
4627c478bd9Sstevel@tonic-gate *np++ = c;
4637c478bd9Sstevel@tonic-gate if (!any(c, S_htrqxe))
4647c478bd9Sstevel@tonic-gate goto vsyn;
4657c478bd9Sstevel@tonic-gate } else
4667c478bd9Sstevel@tonic-gate ungetD(c);
4677c478bd9Sstevel@tonic-gate if (sc == '{') {
4687c478bd9Sstevel@tonic-gate c = getC(DOEXCL);
4697c478bd9Sstevel@tonic-gate if (c != '}') {
4707c478bd9Sstevel@tonic-gate ungetC(c);
4717c478bd9Sstevel@tonic-gate goto vsyn;
4727c478bd9Sstevel@tonic-gate }
4737c478bd9Sstevel@tonic-gate *np++ = c;
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate ret:
4767c478bd9Sstevel@tonic-gate *np = 0;
4777c478bd9Sstevel@tonic-gate addla(name);
4787c478bd9Sstevel@tonic-gate return;
4797c478bd9Sstevel@tonic-gate
4807c478bd9Sstevel@tonic-gate vsyn:
4817c478bd9Sstevel@tonic-gate seterr("Variable syntax");
4827c478bd9Sstevel@tonic-gate goto ret;
4837c478bd9Sstevel@tonic-gate }
4847c478bd9Sstevel@tonic-gate
4856c02b4a4Smuffin void
addla(tchar * cp)4866c02b4a4Smuffin addla(tchar *cp)
4877c478bd9Sstevel@tonic-gate {
4887c478bd9Sstevel@tonic-gate tchar *buf;
489*258f91c6SToomas Soome static tchar *labuf = NULL;
4907c478bd9Sstevel@tonic-gate int len = 0;
4917c478bd9Sstevel@tonic-gate
4927c478bd9Sstevel@tonic-gate #ifdef TRACE
4937c478bd9Sstevel@tonic-gate tprintf("TRACE- addla()\n");
4947c478bd9Sstevel@tonic-gate #endif
4957c478bd9Sstevel@tonic-gate if (lap) {
4967c478bd9Sstevel@tonic-gate len = strlen_(lap);
4977c478bd9Sstevel@tonic-gate buf = xalloc((len+1) * sizeof (tchar));
4987c478bd9Sstevel@tonic-gate (void) strcpy_(buf, lap);
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate len += strlen_(cp);
5017c478bd9Sstevel@tonic-gate
5027c478bd9Sstevel@tonic-gate /* len+5 is allow 4 additional charecters just to be safe */
5037c478bd9Sstevel@tonic-gate labuf = xrealloc(labuf, (len+5) * sizeof (tchar));
5047c478bd9Sstevel@tonic-gate (void) strcpy_(labuf, cp);
5057c478bd9Sstevel@tonic-gate if (lap) {
5067c478bd9Sstevel@tonic-gate (void) strcat_(labuf, buf);
50765b0c20eSnakanon xfree(buf);
5087c478bd9Sstevel@tonic-gate }
5097c478bd9Sstevel@tonic-gate lap = labuf;
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate
51270a587ddSchin tchar lhsb[256];
51370a587ddSchin tchar slhs[256];
51470a587ddSchin tchar rhsb[512];
5157c478bd9Sstevel@tonic-gate int quesarg;
5167c478bd9Sstevel@tonic-gate
5176c02b4a4Smuffin void
getexcl(tchar sc)5186c02b4a4Smuffin getexcl(tchar sc)
5197c478bd9Sstevel@tonic-gate {
5206c02b4a4Smuffin struct wordent *hp, *ip;
5217c478bd9Sstevel@tonic-gate int left, right, dol;
5226c02b4a4Smuffin int c;
5237c478bd9Sstevel@tonic-gate
5247c478bd9Sstevel@tonic-gate #ifdef TRACE
5257c478bd9Sstevel@tonic-gate tprintf("TRACE- getexcl()\n");
5267c478bd9Sstevel@tonic-gate #endif
5277c478bd9Sstevel@tonic-gate if (sc == 0) {
5287c478bd9Sstevel@tonic-gate sc = getC(0);
5297c478bd9Sstevel@tonic-gate if (sc != '{') {
5307c478bd9Sstevel@tonic-gate ungetC(sc);
5317c478bd9Sstevel@tonic-gate sc = 0;
5327c478bd9Sstevel@tonic-gate }
5337c478bd9Sstevel@tonic-gate }
5347c478bd9Sstevel@tonic-gate quesarg = -1;
5357c478bd9Sstevel@tonic-gate lastev = eventno;
5367c478bd9Sstevel@tonic-gate hp = gethent(sc);
5377c478bd9Sstevel@tonic-gate if (hp == 0)
5387c478bd9Sstevel@tonic-gate return;
5397c478bd9Sstevel@tonic-gate hadhist = 1;
5407c478bd9Sstevel@tonic-gate dol = 0;
5417c478bd9Sstevel@tonic-gate if (hp == alhistp)
5427c478bd9Sstevel@tonic-gate for (ip = hp->next->next; ip != alhistt; ip = ip->next)
5437c478bd9Sstevel@tonic-gate dol++;
5447c478bd9Sstevel@tonic-gate else
5457c478bd9Sstevel@tonic-gate for (ip = hp->next->next; ip != hp->prev; ip = ip->next)
5467c478bd9Sstevel@tonic-gate dol++;
5477c478bd9Sstevel@tonic-gate left = 0, right = dol;
5487c478bd9Sstevel@tonic-gate if (sc == HISTSUB) {
5497c478bd9Sstevel@tonic-gate ungetC('s'), unreadc(HISTSUB), c = ':';
5507c478bd9Sstevel@tonic-gate goto subst;
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate c = getC(0);
55365b0c20eSnakanon /* if (!any(c, ":^$*-%")) */ /* change needed for char -> tchar */
5547c478bd9Sstevel@tonic-gate if (! (c == ':' || c == '^' || c == '$' || c == '*' ||
55565b0c20eSnakanon c == '-' || c == '%'))
5567c478bd9Sstevel@tonic-gate goto subst;
5577c478bd9Sstevel@tonic-gate left = right = -1;
5587c478bd9Sstevel@tonic-gate if (c == ':') {
5597c478bd9Sstevel@tonic-gate c = getC(0);
5607c478bd9Sstevel@tonic-gate unreadc(c);
5617c478bd9Sstevel@tonic-gate if (letter(c) || c == '&') {
5627c478bd9Sstevel@tonic-gate c = ':';
5637c478bd9Sstevel@tonic-gate left = 0, right = dol;
5647c478bd9Sstevel@tonic-gate goto subst;
5657c478bd9Sstevel@tonic-gate }
5667c478bd9Sstevel@tonic-gate } else
5677c478bd9Sstevel@tonic-gate ungetC(c);
5687c478bd9Sstevel@tonic-gate if (!getsel(&left, &right, dol))
5697c478bd9Sstevel@tonic-gate return;
5707c478bd9Sstevel@tonic-gate c = getC(0);
5717c478bd9Sstevel@tonic-gate if (c == '*')
5727c478bd9Sstevel@tonic-gate ungetC(c), c = '-';
5737c478bd9Sstevel@tonic-gate if (c == '-') {
5747c478bd9Sstevel@tonic-gate if (!getsel(&left, &right, dol))
5757c478bd9Sstevel@tonic-gate return;
5767c478bd9Sstevel@tonic-gate c = getC(0);
5777c478bd9Sstevel@tonic-gate }
5787c478bd9Sstevel@tonic-gate subst:
5797c478bd9Sstevel@tonic-gate exclc = right - left + 1;
5807c478bd9Sstevel@tonic-gate while (--left >= 0)
5817c478bd9Sstevel@tonic-gate hp = hp->next;
5827c478bd9Sstevel@tonic-gate if (sc == HISTSUB || c == ':') {
5837c478bd9Sstevel@tonic-gate do {
5847c478bd9Sstevel@tonic-gate hp = getsub(hp);
5857c478bd9Sstevel@tonic-gate c = getC(0);
5867c478bd9Sstevel@tonic-gate } while (c == ':');
5877c478bd9Sstevel@tonic-gate }
5887c478bd9Sstevel@tonic-gate unreadc(c);
5897c478bd9Sstevel@tonic-gate if (sc == '{') {
5907c478bd9Sstevel@tonic-gate c = getC(0);
5917c478bd9Sstevel@tonic-gate if (c != '}')
5927c478bd9Sstevel@tonic-gate seterr("Bad ! form");
5937c478bd9Sstevel@tonic-gate }
5947c478bd9Sstevel@tonic-gate exclnxt = hp;
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate
5977c478bd9Sstevel@tonic-gate struct wordent *
getsub(struct wordent * en)5986c02b4a4Smuffin getsub(struct wordent *en)
5997c478bd9Sstevel@tonic-gate {
6006c02b4a4Smuffin tchar *cp;
6017c478bd9Sstevel@tonic-gate int delim;
6026c02b4a4Smuffin int c;
6037c478bd9Sstevel@tonic-gate int sc;
6047c478bd9Sstevel@tonic-gate bool global = 0;
6057c478bd9Sstevel@tonic-gate tchar orhsb[(sizeof rhsb)/(sizeof rhsb[0])];
6067c478bd9Sstevel@tonic-gate
6077c478bd9Sstevel@tonic-gate #ifdef TRACE
6087c478bd9Sstevel@tonic-gate tprintf("TRACE- getsub()\n");
6097c478bd9Sstevel@tonic-gate #endif
6107c478bd9Sstevel@tonic-gate exclnxt = 0;
6117c478bd9Sstevel@tonic-gate sc = c = getC(0);
6127c478bd9Sstevel@tonic-gate if (c == 'g')
6137c478bd9Sstevel@tonic-gate global++, c = getC(0);
6147c478bd9Sstevel@tonic-gate switch (c) {
6157c478bd9Sstevel@tonic-gate
6167c478bd9Sstevel@tonic-gate case 'p':
6177c478bd9Sstevel@tonic-gate justpr++;
6187c478bd9Sstevel@tonic-gate goto ret;
6197c478bd9Sstevel@tonic-gate
6207c478bd9Sstevel@tonic-gate case 'x':
6217c478bd9Sstevel@tonic-gate case 'q':
6227c478bd9Sstevel@tonic-gate global++;
6237c478bd9Sstevel@tonic-gate /* fall into ... */
6247c478bd9Sstevel@tonic-gate
6257c478bd9Sstevel@tonic-gate case 'h':
6267c478bd9Sstevel@tonic-gate case 'r':
6277c478bd9Sstevel@tonic-gate case 't':
6287c478bd9Sstevel@tonic-gate case 'e':
6297c478bd9Sstevel@tonic-gate break;
6307c478bd9Sstevel@tonic-gate
6317c478bd9Sstevel@tonic-gate case '&':
6327c478bd9Sstevel@tonic-gate if (slhs[0] == 0) {
6337c478bd9Sstevel@tonic-gate seterr("No prev sub");
6347c478bd9Sstevel@tonic-gate goto ret;
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate (void) strcpy_(lhsb, slhs);
6377c478bd9Sstevel@tonic-gate break;
6387c478bd9Sstevel@tonic-gate
63965b0c20eSnakanon #if 0
6407c478bd9Sstevel@tonic-gate case '~':
6417c478bd9Sstevel@tonic-gate if (lhsb[0] == 0)
6427c478bd9Sstevel@tonic-gate goto badlhs;
6437c478bd9Sstevel@tonic-gate break;
64465b0c20eSnakanon #endif
6457c478bd9Sstevel@tonic-gate
6467c478bd9Sstevel@tonic-gate case 's':
6477c478bd9Sstevel@tonic-gate delim = getC(0);
64865b0c20eSnakanon if (alnum(delim) || isspnl(delim)) {
6497c478bd9Sstevel@tonic-gate unreadc(delim);
6507c478bd9Sstevel@tonic-gate bads:
6517c478bd9Sstevel@tonic-gate lhsb[0] = 0;
6527c478bd9Sstevel@tonic-gate seterr("Bad substitute");
6537c478bd9Sstevel@tonic-gate goto ret;
6547c478bd9Sstevel@tonic-gate }
6557c478bd9Sstevel@tonic-gate cp = lhsb;
6567c478bd9Sstevel@tonic-gate for (;;) {
6577c478bd9Sstevel@tonic-gate c = getC(0);
6587c478bd9Sstevel@tonic-gate if (c == '\n') {
6597c478bd9Sstevel@tonic-gate unreadc(c);
6607c478bd9Sstevel@tonic-gate break;
6617c478bd9Sstevel@tonic-gate }
6627c478bd9Sstevel@tonic-gate if (c == delim)
6637c478bd9Sstevel@tonic-gate break;
6647c478bd9Sstevel@tonic-gate if (cp > &lhsb[(sizeof lhsb)/(sizeof lhsb[0]) - 2])
6657c478bd9Sstevel@tonic-gate goto bads;
6667c478bd9Sstevel@tonic-gate if (c == '\\') {
6677c478bd9Sstevel@tonic-gate c = getC(0);
6687c478bd9Sstevel@tonic-gate if (c != delim && c != '\\')
6697c478bd9Sstevel@tonic-gate *cp++ = '\\';
6707c478bd9Sstevel@tonic-gate }
6717c478bd9Sstevel@tonic-gate *cp++ = c;
6727c478bd9Sstevel@tonic-gate }
6737c478bd9Sstevel@tonic-gate if (cp != lhsb)
6747c478bd9Sstevel@tonic-gate *cp++ = 0;
6757c478bd9Sstevel@tonic-gate else if (lhsb[0] == 0) {
67665b0c20eSnakanon /* badlhs: */
6777c478bd9Sstevel@tonic-gate seterr("No prev lhs");
6787c478bd9Sstevel@tonic-gate goto ret;
6797c478bd9Sstevel@tonic-gate }
6807c478bd9Sstevel@tonic-gate cp = rhsb;
6817c478bd9Sstevel@tonic-gate (void) strcpy_(orhsb, cp);
6827c478bd9Sstevel@tonic-gate for (;;) {
6837c478bd9Sstevel@tonic-gate c = getC(0);
6847c478bd9Sstevel@tonic-gate if (c == '\n') {
6857c478bd9Sstevel@tonic-gate unreadc(c);
6867c478bd9Sstevel@tonic-gate break;
6877c478bd9Sstevel@tonic-gate }
6887c478bd9Sstevel@tonic-gate if (c == delim)
6897c478bd9Sstevel@tonic-gate break;
69065b0c20eSnakanon #if 0
6917c478bd9Sstevel@tonic-gate if (c == '~') {
69265b0c20eSnakanon if (&cp[strlen_(orhsb)]
6937c478bd9Sstevel@tonic-gate > &rhsb[(sizeof rhsb)/(sizeof rhsb[0]) - 2])
6947c478bd9Sstevel@tonic-gate goto toorhs;
6957c478bd9Sstevel@tonic-gate (void) strcpy_(cp, orhsb);
6967c478bd9Sstevel@tonic-gate cp = strend(cp);
6977c478bd9Sstevel@tonic-gate continue;
6987c478bd9Sstevel@tonic-gate }
69965b0c20eSnakanon #endif
7007c478bd9Sstevel@tonic-gate if (cp > &rhsb[(sizeof rhsb)/(sizeof rhsb[0]) - 2]) {
70165b0c20eSnakanon /* toorhs: */
7027c478bd9Sstevel@tonic-gate seterr("Rhs too long");
7037c478bd9Sstevel@tonic-gate goto ret;
7047c478bd9Sstevel@tonic-gate }
7057c478bd9Sstevel@tonic-gate if (c == '\\') {
7067c478bd9Sstevel@tonic-gate c = getC(0);
7077c478bd9Sstevel@tonic-gate if (c != delim /* && c != '~' */)
7087c478bd9Sstevel@tonic-gate *cp++ = '\\';
7097c478bd9Sstevel@tonic-gate }
7107c478bd9Sstevel@tonic-gate *cp++ = c;
7117c478bd9Sstevel@tonic-gate }
7127c478bd9Sstevel@tonic-gate *cp++ = 0;
7137c478bd9Sstevel@tonic-gate break;
7147c478bd9Sstevel@tonic-gate
7157c478bd9Sstevel@tonic-gate default:
7167c478bd9Sstevel@tonic-gate if (c == '\n')
7177c478bd9Sstevel@tonic-gate unreadc(c);
7187c478bd9Sstevel@tonic-gate seterrc(gettext("Bad ! modifier: "), c);
7197c478bd9Sstevel@tonic-gate goto ret;
7207c478bd9Sstevel@tonic-gate }
7217c478bd9Sstevel@tonic-gate (void) strcpy_(slhs, lhsb);
7227c478bd9Sstevel@tonic-gate if (exclc)
7237c478bd9Sstevel@tonic-gate en = dosub(sc, en, global);
7247c478bd9Sstevel@tonic-gate ret:
7257c478bd9Sstevel@tonic-gate return (en);
7267c478bd9Sstevel@tonic-gate }
7277c478bd9Sstevel@tonic-gate
7287c478bd9Sstevel@tonic-gate struct wordent *
dosub(int sc,struct wordent * en,bool global)7296c02b4a4Smuffin dosub(int sc, struct wordent *en, bool global)
7307c478bd9Sstevel@tonic-gate {
7317c478bd9Sstevel@tonic-gate struct wordent lex;
7327c478bd9Sstevel@tonic-gate bool didsub = 0;
7337c478bd9Sstevel@tonic-gate struct wordent *hp = &lex;
7346c02b4a4Smuffin struct wordent *wdp;
7356c02b4a4Smuffin int i = exclc;
7367c478bd9Sstevel@tonic-gate
7377c478bd9Sstevel@tonic-gate #ifdef TRACE
7387c478bd9Sstevel@tonic-gate tprintf("TRACE- dosub()\n");
7397c478bd9Sstevel@tonic-gate #endif
7407c478bd9Sstevel@tonic-gate wdp = hp;
7417c478bd9Sstevel@tonic-gate while (--i >= 0) {
74265b0c20eSnakanon struct wordent *new = (struct wordent *)xcalloc(1, sizeof *wdp);
7437c478bd9Sstevel@tonic-gate
7447c478bd9Sstevel@tonic-gate new->prev = wdp;
7457c478bd9Sstevel@tonic-gate new->next = hp;
7467c478bd9Sstevel@tonic-gate wdp->next = new;
7477c478bd9Sstevel@tonic-gate wdp = new;
7487c478bd9Sstevel@tonic-gate en = en->next;
7497c478bd9Sstevel@tonic-gate wdp->word = global || didsub == 0 ?
7507c478bd9Sstevel@tonic-gate subword(en->word, sc, &didsub) : savestr(en->word);
7517c478bd9Sstevel@tonic-gate }
7527c478bd9Sstevel@tonic-gate if (didsub == 0)
7537c478bd9Sstevel@tonic-gate seterr("Modifier failed");
7547c478bd9Sstevel@tonic-gate hp->prev = wdp;
7557c478bd9Sstevel@tonic-gate return (&enthist(-1000, &lex, 0)->Hlex);
7567c478bd9Sstevel@tonic-gate }
7577c478bd9Sstevel@tonic-gate
7587c478bd9Sstevel@tonic-gate tchar *
subword(tchar * cp,int type,bool * adid)7596c02b4a4Smuffin subword(tchar *cp, int type, bool *adid)
7607c478bd9Sstevel@tonic-gate {
7617c478bd9Sstevel@tonic-gate tchar wbuf[BUFSIZ];
7626c02b4a4Smuffin tchar *wp, *mp, *np;
7636c02b4a4Smuffin int i;
7647c478bd9Sstevel@tonic-gate
7657c478bd9Sstevel@tonic-gate #ifdef TRACE
7667c478bd9Sstevel@tonic-gate tprintf("TRACE- subword()\n");
7677c478bd9Sstevel@tonic-gate #endif
7687c478bd9Sstevel@tonic-gate switch (type) {
7697c478bd9Sstevel@tonic-gate
7707c478bd9Sstevel@tonic-gate case 'r':
7717c478bd9Sstevel@tonic-gate case 'e':
7727c478bd9Sstevel@tonic-gate case 'h':
7737c478bd9Sstevel@tonic-gate case 't':
7747c478bd9Sstevel@tonic-gate case 'q':
7757c478bd9Sstevel@tonic-gate case 'x':
7767c478bd9Sstevel@tonic-gate wp = domod(cp, type);
7777c478bd9Sstevel@tonic-gate if (wp == 0)
7787c478bd9Sstevel@tonic-gate return (savestr(cp));
7797c478bd9Sstevel@tonic-gate *adid = 1;
7807c478bd9Sstevel@tonic-gate return (wp);
7817c478bd9Sstevel@tonic-gate
7827c478bd9Sstevel@tonic-gate default:
7837c478bd9Sstevel@tonic-gate wp = wbuf;
7847c478bd9Sstevel@tonic-gate i = BUFSIZ - 4;
7857c478bd9Sstevel@tonic-gate for (mp = cp; *mp; mp++)
7867c478bd9Sstevel@tonic-gate if (matchs(mp, lhsb)) {
78765b0c20eSnakanon for (np = cp; np < mp; )
7887c478bd9Sstevel@tonic-gate *wp++ = *np++, --i;
7897c478bd9Sstevel@tonic-gate for (np = rhsb; *np; np++) switch (*np) {
7907c478bd9Sstevel@tonic-gate
7917c478bd9Sstevel@tonic-gate case '\\':
7927c478bd9Sstevel@tonic-gate if (np[1] == '&')
7937c478bd9Sstevel@tonic-gate np++;
7947c478bd9Sstevel@tonic-gate /* fall into ... */
7957c478bd9Sstevel@tonic-gate
7967c478bd9Sstevel@tonic-gate default:
7977c478bd9Sstevel@tonic-gate if (--i < 0)
7987c478bd9Sstevel@tonic-gate goto ovflo;
7997c478bd9Sstevel@tonic-gate *wp++ = *np;
8007c478bd9Sstevel@tonic-gate continue;
8017c478bd9Sstevel@tonic-gate
8027c478bd9Sstevel@tonic-gate case '&':
8037c478bd9Sstevel@tonic-gate i -= strlen_(lhsb);
8047c478bd9Sstevel@tonic-gate if (i < 0)
8057c478bd9Sstevel@tonic-gate goto ovflo;
8067c478bd9Sstevel@tonic-gate *wp = 0;
8077c478bd9Sstevel@tonic-gate (void) strcat_(wp, lhsb);
8087c478bd9Sstevel@tonic-gate wp = strend(wp);
8097c478bd9Sstevel@tonic-gate continue;
8107c478bd9Sstevel@tonic-gate }
8117c478bd9Sstevel@tonic-gate mp += strlen_(lhsb);
8127c478bd9Sstevel@tonic-gate i -= strlen_(mp);
8137c478bd9Sstevel@tonic-gate if (i < 0) {
8147c478bd9Sstevel@tonic-gate ovflo:
8157c478bd9Sstevel@tonic-gate seterr("Subst buf ovflo");
81665b0c20eSnakanon return (S_ /* "" */);
8177c478bd9Sstevel@tonic-gate }
8187c478bd9Sstevel@tonic-gate *wp = 0;
8197c478bd9Sstevel@tonic-gate (void) strcat_(wp, mp);
8207c478bd9Sstevel@tonic-gate *adid = 1;
8217c478bd9Sstevel@tonic-gate return (savestr(wbuf));
8227c478bd9Sstevel@tonic-gate }
8237c478bd9Sstevel@tonic-gate return (savestr(cp));
8247c478bd9Sstevel@tonic-gate }
8257c478bd9Sstevel@tonic-gate }
8267c478bd9Sstevel@tonic-gate
8277c478bd9Sstevel@tonic-gate tchar *
domod(tchar * cp,int type)8286c02b4a4Smuffin domod(tchar *cp, int type)
8297c478bd9Sstevel@tonic-gate {
8306c02b4a4Smuffin tchar *wp, *xp;
8316c02b4a4Smuffin int c;
8327c478bd9Sstevel@tonic-gate
8337c478bd9Sstevel@tonic-gate #ifdef TRACE
8347c478bd9Sstevel@tonic-gate tprintf("TRACE- domod()\n");
8357c478bd9Sstevel@tonic-gate #endif
8367c478bd9Sstevel@tonic-gate switch (type) {
8377c478bd9Sstevel@tonic-gate
8387c478bd9Sstevel@tonic-gate case 'x':
8397c478bd9Sstevel@tonic-gate case 'q':
8407c478bd9Sstevel@tonic-gate wp = savestr(cp);
8417c478bd9Sstevel@tonic-gate for (xp = wp; c = *xp; xp++)
8427c478bd9Sstevel@tonic-gate if (!issp(c) || type == 'q')
8437c478bd9Sstevel@tonic-gate *xp |= QUOTE;
8447c478bd9Sstevel@tonic-gate return (wp);
8457c478bd9Sstevel@tonic-gate
8467c478bd9Sstevel@tonic-gate case 'h':
8477c478bd9Sstevel@tonic-gate case 't':
8487c478bd9Sstevel@tonic-gate if (!any('/', cp))
8497c478bd9Sstevel@tonic-gate return (type == 't' ? savestr(cp) : 0);
8507c478bd9Sstevel@tonic-gate wp = strend(cp);
8517c478bd9Sstevel@tonic-gate while (*--wp != '/')
8527c478bd9Sstevel@tonic-gate continue;
8537c478bd9Sstevel@tonic-gate if (type == 'h')
8547c478bd9Sstevel@tonic-gate xp = savestr(cp), xp[wp - cp] = 0;
8557c478bd9Sstevel@tonic-gate else
8567c478bd9Sstevel@tonic-gate xp = savestr(wp + 1);
8577c478bd9Sstevel@tonic-gate return (xp);
8587c478bd9Sstevel@tonic-gate
8597c478bd9Sstevel@tonic-gate case 'e':
8607c478bd9Sstevel@tonic-gate case 'r':
8617c478bd9Sstevel@tonic-gate wp = strend(cp);
8627c478bd9Sstevel@tonic-gate for (wp--; wp >= cp && *wp != '/'; wp--)
8637c478bd9Sstevel@tonic-gate if (*wp == '.') {
8647c478bd9Sstevel@tonic-gate if (type == 'e')
8657c478bd9Sstevel@tonic-gate xp = savestr(wp + 1);
8667c478bd9Sstevel@tonic-gate else
8677c478bd9Sstevel@tonic-gate xp = savestr(cp), xp[wp - cp] = 0;
8687c478bd9Sstevel@tonic-gate return (xp);
8697c478bd9Sstevel@tonic-gate }
87065b0c20eSnakanon return (savestr(type == 'e' ? S_ /* "" */ : cp));
8717c478bd9Sstevel@tonic-gate }
8727c478bd9Sstevel@tonic-gate return (0);
8737c478bd9Sstevel@tonic-gate }
8747c478bd9Sstevel@tonic-gate
8756c02b4a4Smuffin int
matchs(tchar * str,tchar * pat)8766c02b4a4Smuffin matchs(tchar *str, tchar *pat)
8777c478bd9Sstevel@tonic-gate {
8787c478bd9Sstevel@tonic-gate
8797c478bd9Sstevel@tonic-gate #ifdef TRACE
8807c478bd9Sstevel@tonic-gate tprintf("TRACE- matchs()\n");
8817c478bd9Sstevel@tonic-gate #endif
8827c478bd9Sstevel@tonic-gate while (*str && *pat && *str == *pat)
8837c478bd9Sstevel@tonic-gate str++, pat++;
8847c478bd9Sstevel@tonic-gate return (*pat == 0);
8857c478bd9Sstevel@tonic-gate }
8867c478bd9Sstevel@tonic-gate
8876c02b4a4Smuffin int
getsel(int * al,int * ar,int dol)8886c02b4a4Smuffin getsel(int *al, int *ar, int dol)
8897c478bd9Sstevel@tonic-gate {
8906c02b4a4Smuffin int c = getC(0);
8916c02b4a4Smuffin int i;
8927c478bd9Sstevel@tonic-gate bool first = *al < 0;
8937c478bd9Sstevel@tonic-gate
8947c478bd9Sstevel@tonic-gate #ifdef TRACE
8957c478bd9Sstevel@tonic-gate tprintf("TRACE- getsel()\n");
8967c478bd9Sstevel@tonic-gate #endif
8977c478bd9Sstevel@tonic-gate switch (c) {
8987c478bd9Sstevel@tonic-gate
8997c478bd9Sstevel@tonic-gate case '%':
9007c478bd9Sstevel@tonic-gate if (quesarg == -1)
9017c478bd9Sstevel@tonic-gate goto bad;
9027c478bd9Sstevel@tonic-gate if (*al < 0)
9037c478bd9Sstevel@tonic-gate *al = quesarg;
9047c478bd9Sstevel@tonic-gate *ar = quesarg;
9057c478bd9Sstevel@tonic-gate break;
9067c478bd9Sstevel@tonic-gate
9077c478bd9Sstevel@tonic-gate case '-':
9087c478bd9Sstevel@tonic-gate if (*al < 0) {
9097c478bd9Sstevel@tonic-gate *al = 0;
9107c478bd9Sstevel@tonic-gate *ar = dol - 1;
9117c478bd9Sstevel@tonic-gate unreadc(c);
9127c478bd9Sstevel@tonic-gate }
9137c478bd9Sstevel@tonic-gate return (1);
9147c478bd9Sstevel@tonic-gate
9157c478bd9Sstevel@tonic-gate case '^':
9167c478bd9Sstevel@tonic-gate if (*al < 0)
9177c478bd9Sstevel@tonic-gate *al = 1;
9187c478bd9Sstevel@tonic-gate *ar = 1;
9197c478bd9Sstevel@tonic-gate break;
9207c478bd9Sstevel@tonic-gate
9217c478bd9Sstevel@tonic-gate case '$':
9227c478bd9Sstevel@tonic-gate if (*al < 0)
9237c478bd9Sstevel@tonic-gate *al = dol;
9247c478bd9Sstevel@tonic-gate *ar = dol;
9257c478bd9Sstevel@tonic-gate break;
9267c478bd9Sstevel@tonic-gate
9277c478bd9Sstevel@tonic-gate case '*':
9287c478bd9Sstevel@tonic-gate if (*al < 0)
9297c478bd9Sstevel@tonic-gate *al = 1;
9307c478bd9Sstevel@tonic-gate *ar = dol;
9317c478bd9Sstevel@tonic-gate if (*ar < *al) {
9327c478bd9Sstevel@tonic-gate *ar = 0;
9337c478bd9Sstevel@tonic-gate *al = 1;
9347c478bd9Sstevel@tonic-gate return (1);
9357c478bd9Sstevel@tonic-gate }
9367c478bd9Sstevel@tonic-gate break;
9377c478bd9Sstevel@tonic-gate
9387c478bd9Sstevel@tonic-gate default:
9397c478bd9Sstevel@tonic-gate if (digit(c)) {
9407c478bd9Sstevel@tonic-gate i = 0;
9417c478bd9Sstevel@tonic-gate while (digit(c)) {
9427c478bd9Sstevel@tonic-gate i = i * 10 + c - '0';
9437c478bd9Sstevel@tonic-gate c = getC(0);
9447c478bd9Sstevel@tonic-gate }
9457c478bd9Sstevel@tonic-gate if (i < 0)
9467c478bd9Sstevel@tonic-gate i = dol + 1;
9477c478bd9Sstevel@tonic-gate if (*al < 0)
9487c478bd9Sstevel@tonic-gate *al = i;
9497c478bd9Sstevel@tonic-gate *ar = i;
9507c478bd9Sstevel@tonic-gate } else
9517c478bd9Sstevel@tonic-gate if (*al < 0)
9527c478bd9Sstevel@tonic-gate *al = 0, *ar = dol;
9537c478bd9Sstevel@tonic-gate else
9547c478bd9Sstevel@tonic-gate *ar = dol - 1;
9557c478bd9Sstevel@tonic-gate unreadc(c);
9567c478bd9Sstevel@tonic-gate break;
9577c478bd9Sstevel@tonic-gate }
9587c478bd9Sstevel@tonic-gate if (first) {
9597c478bd9Sstevel@tonic-gate c = getC(0);
9607c478bd9Sstevel@tonic-gate unreadc(c);
9617c478bd9Sstevel@tonic-gate /* if (any(c, "-$*")) */ /* char -> tchar */
9627c478bd9Sstevel@tonic-gate if (c == '-' || c == '$' || c == '*')
9637c478bd9Sstevel@tonic-gate return (1);
9647c478bd9Sstevel@tonic-gate }
9657c478bd9Sstevel@tonic-gate if (*al > *ar || *ar > dol) {
9667c478bd9Sstevel@tonic-gate bad:
9677c478bd9Sstevel@tonic-gate seterr("Bad ! arg selector");
9687c478bd9Sstevel@tonic-gate return (0);
9697c478bd9Sstevel@tonic-gate }
9707c478bd9Sstevel@tonic-gate return (1);
9717c478bd9Sstevel@tonic-gate
9727c478bd9Sstevel@tonic-gate }
9737c478bd9Sstevel@tonic-gate
9747c478bd9Sstevel@tonic-gate struct wordent *
gethent(int sc)9756c02b4a4Smuffin gethent(int sc)
9767c478bd9Sstevel@tonic-gate {
9776c02b4a4Smuffin struct Hist *hp;
9786c02b4a4Smuffin tchar *np;
9796c02b4a4Smuffin int c;
9807c478bd9Sstevel@tonic-gate int event;
9817c478bd9Sstevel@tonic-gate bool back = 0;
9827c478bd9Sstevel@tonic-gate
9837c478bd9Sstevel@tonic-gate #ifdef TRACE
9847c478bd9Sstevel@tonic-gate tprintf("TRACE- gethent()\n");
9857c478bd9Sstevel@tonic-gate #endif
9867c478bd9Sstevel@tonic-gate c = sc == HISTSUB ? HIST : getC(0);
9877c478bd9Sstevel@tonic-gate if (c == HIST) {
9887c478bd9Sstevel@tonic-gate if (alhistp)
9897c478bd9Sstevel@tonic-gate return (alhistp);
9907c478bd9Sstevel@tonic-gate event = eventno;
9917c478bd9Sstevel@tonic-gate goto skip;
9927c478bd9Sstevel@tonic-gate }
9937c478bd9Sstevel@tonic-gate switch (c) {
9947c478bd9Sstevel@tonic-gate
9957c478bd9Sstevel@tonic-gate case ':':
9967c478bd9Sstevel@tonic-gate case '^':
9977c478bd9Sstevel@tonic-gate case '$':
9987c478bd9Sstevel@tonic-gate case '*':
9997c478bd9Sstevel@tonic-gate case '%':
10007c478bd9Sstevel@tonic-gate ungetC(c);
10017c478bd9Sstevel@tonic-gate if (lastev == eventno && alhistp)
10027c478bd9Sstevel@tonic-gate return (alhistp);
10037c478bd9Sstevel@tonic-gate event = lastev;
10047c478bd9Sstevel@tonic-gate break;
10057c478bd9Sstevel@tonic-gate
10067c478bd9Sstevel@tonic-gate case '-':
10077c478bd9Sstevel@tonic-gate back = 1;
10087c478bd9Sstevel@tonic-gate c = getC(0);
10097c478bd9Sstevel@tonic-gate goto number;
10107c478bd9Sstevel@tonic-gate
10117c478bd9Sstevel@tonic-gate case '#': /* !# is command being typed in (mrh) */
101265b0c20eSnakanon return (¶ml);
10137c478bd9Sstevel@tonic-gate
10147c478bd9Sstevel@tonic-gate default:
101565b0c20eSnakanon /* if (any(c, "(=~")) { */
10167c478bd9Sstevel@tonic-gate if (c == '(' || c == '=' || c == '~') {
10177c478bd9Sstevel@tonic-gate unreadc(c);
10187c478bd9Sstevel@tonic-gate ungetC(HIST);
10197c478bd9Sstevel@tonic-gate return (0);
10207c478bd9Sstevel@tonic-gate }
10217c478bd9Sstevel@tonic-gate if (digit(c))
10227c478bd9Sstevel@tonic-gate goto number;
10237c478bd9Sstevel@tonic-gate np = lhsb;
10247c478bd9Sstevel@tonic-gate /* while (!any(c, ": \t\\\n}")) { */
10257c478bd9Sstevel@tonic-gate while (! (c == ':' || c == '\\' || isspnl(c) || c == '}')) {
10267c478bd9Sstevel@tonic-gate if (np < &lhsb[(sizeof lhsb)/(sizeof lhsb[0]) - 2])
10277c478bd9Sstevel@tonic-gate *np++ = c;
10287c478bd9Sstevel@tonic-gate c = getC(0);
10297c478bd9Sstevel@tonic-gate }
10307c478bd9Sstevel@tonic-gate unreadc(c);
10317c478bd9Sstevel@tonic-gate if (np == lhsb) {
10327c478bd9Sstevel@tonic-gate ungetC(HIST);
10337c478bd9Sstevel@tonic-gate return (0);
10347c478bd9Sstevel@tonic-gate }
10357c478bd9Sstevel@tonic-gate *np++ = 0;
10367c478bd9Sstevel@tonic-gate hp = findev(lhsb, 0);
10377c478bd9Sstevel@tonic-gate if (hp)
10387c478bd9Sstevel@tonic-gate lastev = hp->Hnum;
10397c478bd9Sstevel@tonic-gate return (&hp->Hlex);
10407c478bd9Sstevel@tonic-gate
10417c478bd9Sstevel@tonic-gate case '?':
10427c478bd9Sstevel@tonic-gate np = lhsb;
10437c478bd9Sstevel@tonic-gate for (;;) {
10447c478bd9Sstevel@tonic-gate c = getC(0);
10457c478bd9Sstevel@tonic-gate if (c == '\n') {
10467c478bd9Sstevel@tonic-gate unreadc(c);
10477c478bd9Sstevel@tonic-gate break;
10487c478bd9Sstevel@tonic-gate }
10497c478bd9Sstevel@tonic-gate if (c == '?')
10507c478bd9Sstevel@tonic-gate break;
10517c478bd9Sstevel@tonic-gate if (np < &lhsb[(sizeof lhsb)/(sizeof lhsb[0]) - 2])
10527c478bd9Sstevel@tonic-gate *np++ = c;
10537c478bd9Sstevel@tonic-gate }
10547c478bd9Sstevel@tonic-gate if (np == lhsb) {
10557c478bd9Sstevel@tonic-gate if (lhsb[0] == 0) {
10567c478bd9Sstevel@tonic-gate seterr("No prev search");
10577c478bd9Sstevel@tonic-gate return (0);
10587c478bd9Sstevel@tonic-gate }
10597c478bd9Sstevel@tonic-gate } else
10607c478bd9Sstevel@tonic-gate *np++ = 0;
10617c478bd9Sstevel@tonic-gate hp = findev(lhsb, 1);
10627c478bd9Sstevel@tonic-gate if (hp)
10637c478bd9Sstevel@tonic-gate lastev = hp->Hnum;
10647c478bd9Sstevel@tonic-gate return (&hp->Hlex);
10657c478bd9Sstevel@tonic-gate
10667c478bd9Sstevel@tonic-gate number:
10677c478bd9Sstevel@tonic-gate event = 0;
10687c478bd9Sstevel@tonic-gate while (digit(c)) {
10697c478bd9Sstevel@tonic-gate event = event * 10 + c - '0';
10707c478bd9Sstevel@tonic-gate c = getC(0);
10717c478bd9Sstevel@tonic-gate }
10727c478bd9Sstevel@tonic-gate if (back)
10737c478bd9Sstevel@tonic-gate event = eventno + (alhistp == 0) - (event ? event : 0);
10747c478bd9Sstevel@tonic-gate unreadc(c);
10757c478bd9Sstevel@tonic-gate break;
10767c478bd9Sstevel@tonic-gate }
10777c478bd9Sstevel@tonic-gate skip:
10787c478bd9Sstevel@tonic-gate for (hp = Histlist.Hnext; hp; hp = hp->Hnext)
10797c478bd9Sstevel@tonic-gate if (hp->Hnum == event) {
10807c478bd9Sstevel@tonic-gate hp->Href = eventno;
10817c478bd9Sstevel@tonic-gate lastev = hp->Hnum;
10827c478bd9Sstevel@tonic-gate return (&hp->Hlex);
10837c478bd9Sstevel@tonic-gate }
10847c478bd9Sstevel@tonic-gate np = putn(event);
10857c478bd9Sstevel@tonic-gate noev(np);
10867c478bd9Sstevel@tonic-gate return (0);
10877c478bd9Sstevel@tonic-gate }
10887c478bd9Sstevel@tonic-gate
10897c478bd9Sstevel@tonic-gate struct Hist *
findev(tchar * cp,bool anyarg)10906c02b4a4Smuffin findev(tchar *cp, bool anyarg)
10917c478bd9Sstevel@tonic-gate {
10926c02b4a4Smuffin struct Hist *hp;
10937c478bd9Sstevel@tonic-gate
10947c478bd9Sstevel@tonic-gate #ifdef TRACE
10957c478bd9Sstevel@tonic-gate tprintf("TRACE- findev()\n");
10967c478bd9Sstevel@tonic-gate #endif
10977c478bd9Sstevel@tonic-gate for (hp = Histlist.Hnext; hp; hp = hp->Hnext) {
10986c02b4a4Smuffin tchar *dp;
10996c02b4a4Smuffin tchar *p, *q;
11006c02b4a4Smuffin struct wordent *lp = hp->Hlex.next;
11017c478bd9Sstevel@tonic-gate int argno = 0;
11027c478bd9Sstevel@tonic-gate
11037c478bd9Sstevel@tonic-gate if (lp->word[0] == '\n')
11047c478bd9Sstevel@tonic-gate continue;
11057c478bd9Sstevel@tonic-gate if (!anyarg) {
11067c478bd9Sstevel@tonic-gate p = cp;
11077c478bd9Sstevel@tonic-gate q = lp->word;
11087c478bd9Sstevel@tonic-gate do
11097c478bd9Sstevel@tonic-gate if (!*p)
11107c478bd9Sstevel@tonic-gate return (hp);
11117c478bd9Sstevel@tonic-gate while (*p++ == *q++);
11127c478bd9Sstevel@tonic-gate continue;
11137c478bd9Sstevel@tonic-gate }
11147c478bd9Sstevel@tonic-gate do {
11157c478bd9Sstevel@tonic-gate for (dp = lp->word; *dp; dp++) {
11167c478bd9Sstevel@tonic-gate p = cp;
11177c478bd9Sstevel@tonic-gate q = dp;
11187c478bd9Sstevel@tonic-gate do
11197c478bd9Sstevel@tonic-gate if (!*p) {
11207c478bd9Sstevel@tonic-gate quesarg = argno;
11217c478bd9Sstevel@tonic-gate return (hp);
11227c478bd9Sstevel@tonic-gate }
11237c478bd9Sstevel@tonic-gate while (*p++ == *q++);
11247c478bd9Sstevel@tonic-gate }
11257c478bd9Sstevel@tonic-gate lp = lp->next;
11267c478bd9Sstevel@tonic-gate argno++;
11277c478bd9Sstevel@tonic-gate } while (lp->word[0] != '\n');
11287c478bd9Sstevel@tonic-gate }
11297c478bd9Sstevel@tonic-gate noev(cp);
11307c478bd9Sstevel@tonic-gate return (0);
11317c478bd9Sstevel@tonic-gate }
11327c478bd9Sstevel@tonic-gate
11336c02b4a4Smuffin void
noev(tchar * cp)11346c02b4a4Smuffin noev(tchar *cp)
11357c478bd9Sstevel@tonic-gate {
11367c478bd9Sstevel@tonic-gate
11377c478bd9Sstevel@tonic-gate #ifdef TRACE
11387c478bd9Sstevel@tonic-gate tprintf("TRACE- noev()\n");
11397c478bd9Sstevel@tonic-gate #endif
11407c478bd9Sstevel@tonic-gate seterr2(cp, ": Event not found");
11417c478bd9Sstevel@tonic-gate }
11427c478bd9Sstevel@tonic-gate
11436c02b4a4Smuffin void
setexclp(tchar * cp)11446c02b4a4Smuffin setexclp(tchar *cp)
11457c478bd9Sstevel@tonic-gate {
11467c478bd9Sstevel@tonic-gate
11477c478bd9Sstevel@tonic-gate #ifdef TRACE
11487c478bd9Sstevel@tonic-gate tprintf("TRACE- setexclp()\n");
11497c478bd9Sstevel@tonic-gate #endif
11507c478bd9Sstevel@tonic-gate if (cp && cp[0] == '\n')
11517c478bd9Sstevel@tonic-gate return;
11527c478bd9Sstevel@tonic-gate exclp = cp;
11537c478bd9Sstevel@tonic-gate }
11547c478bd9Sstevel@tonic-gate
11556c02b4a4Smuffin void
unreadc(tchar c)11566c02b4a4Smuffin unreadc(tchar c)
11577c478bd9Sstevel@tonic-gate {
11587c478bd9Sstevel@tonic-gate
11597c478bd9Sstevel@tonic-gate peekread = c;
11607c478bd9Sstevel@tonic-gate }
11617c478bd9Sstevel@tonic-gate
11626c02b4a4Smuffin int
readc(bool wanteof)11636c02b4a4Smuffin readc(bool wanteof)
11647c478bd9Sstevel@tonic-gate {
11656c02b4a4Smuffin int c;
11666c02b4a4Smuffin static int sincereal;
11677c478bd9Sstevel@tonic-gate
11687c478bd9Sstevel@tonic-gate if (c = peekread) {
11697c478bd9Sstevel@tonic-gate peekread = 0;
11707c478bd9Sstevel@tonic-gate return (c);
11717c478bd9Sstevel@tonic-gate }
11727c478bd9Sstevel@tonic-gate top:
11737c478bd9Sstevel@tonic-gate if (alvecp) {
11747c478bd9Sstevel@tonic-gate if (c = *alvecp++)
11757c478bd9Sstevel@tonic-gate return (c);
11767c478bd9Sstevel@tonic-gate if (*alvec) {
11777c478bd9Sstevel@tonic-gate alvecp = *alvec++;
11787c478bd9Sstevel@tonic-gate return (' ');
11797c478bd9Sstevel@tonic-gate }
11807c478bd9Sstevel@tonic-gate }
11817c478bd9Sstevel@tonic-gate if (alvec) {
11827c478bd9Sstevel@tonic-gate if (alvecp = *alvec) {
11837c478bd9Sstevel@tonic-gate alvec++;
11847c478bd9Sstevel@tonic-gate goto top;
11857c478bd9Sstevel@tonic-gate }
11867c478bd9Sstevel@tonic-gate /* Infinite source! */
11877c478bd9Sstevel@tonic-gate return ('\n');
11887c478bd9Sstevel@tonic-gate }
11897c478bd9Sstevel@tonic-gate if (evalp) {
11907c478bd9Sstevel@tonic-gate if (c = *evalp++)
11917c478bd9Sstevel@tonic-gate return (c);
11927c478bd9Sstevel@tonic-gate if (*evalvec) {
11937c478bd9Sstevel@tonic-gate evalp = *evalvec++;
11947c478bd9Sstevel@tonic-gate return (' ');
11957c478bd9Sstevel@tonic-gate }
11967c478bd9Sstevel@tonic-gate evalp = 0;
11977c478bd9Sstevel@tonic-gate }
11987c478bd9Sstevel@tonic-gate if (evalvec) {
11997c478bd9Sstevel@tonic-gate if (evalvec == (tchar **)1) {
12007c478bd9Sstevel@tonic-gate doneinp = 1;
12017c478bd9Sstevel@tonic-gate reset();
12027c478bd9Sstevel@tonic-gate }
12037c478bd9Sstevel@tonic-gate if (evalp = *evalvec) {
12047c478bd9Sstevel@tonic-gate evalvec++;
12057c478bd9Sstevel@tonic-gate goto top;
12067c478bd9Sstevel@tonic-gate }
12077c478bd9Sstevel@tonic-gate evalvec = (tchar **)1;
12087c478bd9Sstevel@tonic-gate return ('\n');
12097c478bd9Sstevel@tonic-gate }
12107c478bd9Sstevel@tonic-gate do {
12117c478bd9Sstevel@tonic-gate if (arginp == (tchar *) 1 || onelflg == 1) {
12127c478bd9Sstevel@tonic-gate if (wanteof)
12137c478bd9Sstevel@tonic-gate return (-1);
12147c478bd9Sstevel@tonic-gate exitstat();
12157c478bd9Sstevel@tonic-gate }
12167c478bd9Sstevel@tonic-gate if (arginp) {
12177c478bd9Sstevel@tonic-gate if ((c = *arginp++) == 0) {
12187c478bd9Sstevel@tonic-gate arginp = (tchar *) 1;
12197c478bd9Sstevel@tonic-gate return ('\n');
12207c478bd9Sstevel@tonic-gate }
12217c478bd9Sstevel@tonic-gate return (c);
12227c478bd9Sstevel@tonic-gate }
12237c478bd9Sstevel@tonic-gate reread:
12247c478bd9Sstevel@tonic-gate c = bgetc();
12257c478bd9Sstevel@tonic-gate if (c < 0) {
12267c478bd9Sstevel@tonic-gate struct sgttyb tty;
12277c478bd9Sstevel@tonic-gate
12287c478bd9Sstevel@tonic-gate if (wanteof)
12297c478bd9Sstevel@tonic-gate return (-1);
12307c478bd9Sstevel@tonic-gate /* was isatty but raw with ignoreeof yields problems */
12317c478bd9Sstevel@tonic-gate if (ioctl(SHIN, TIOCGETP, (char *)&tty) == 0 &&
12327c478bd9Sstevel@tonic-gate (tty.sg_flags & RAW) == 0) {
12337c478bd9Sstevel@tonic-gate /* was 'short' for FILEC */
12347c478bd9Sstevel@tonic-gate int ctpgrp;
12357c478bd9Sstevel@tonic-gate
12367c478bd9Sstevel@tonic-gate if (++sincereal > 25)
12377c478bd9Sstevel@tonic-gate goto oops;
12387c478bd9Sstevel@tonic-gate if (tpgrp != -1 &&
123965b0c20eSnakanon ioctl(FSHTTY, TIOCGPGRP, (char *)&ctpgrp) == 0 &&
12407c478bd9Sstevel@tonic-gate tpgrp != ctpgrp) {
12417c478bd9Sstevel@tonic-gate (void) ioctl(FSHTTY, TIOCSPGRP,
124265b0c20eSnakanon (char *)&tpgrp);
12437c478bd9Sstevel@tonic-gate (void) killpg(ctpgrp, SIGHUP);
12447c478bd9Sstevel@tonic-gate printf("Reset tty pgrp from %d to %d\n", ctpgrp, tpgrp);
12457c478bd9Sstevel@tonic-gate goto reread;
12467c478bd9Sstevel@tonic-gate }
124765b0c20eSnakanon if (adrof(S_ignoreeof /* "ignoreeof" */)) {
12487c478bd9Sstevel@tonic-gate if (loginsh)
124965b0c20eSnakanon printf("\nUse \"logout\" to logout.\n");
12507c478bd9Sstevel@tonic-gate else
125165b0c20eSnakanon printf("\nUse \"exit\" to leave csh.\n");
12527c478bd9Sstevel@tonic-gate reset();
12537c478bd9Sstevel@tonic-gate }
12547c478bd9Sstevel@tonic-gate if (chkstop == 0) {
12557c478bd9Sstevel@tonic-gate panystop(1);
12567c478bd9Sstevel@tonic-gate }
12577c478bd9Sstevel@tonic-gate }
12587c478bd9Sstevel@tonic-gate oops:
12597c478bd9Sstevel@tonic-gate doneinp = 1;
12607c478bd9Sstevel@tonic-gate reset();
12617c478bd9Sstevel@tonic-gate }
12627c478bd9Sstevel@tonic-gate sincereal = 0;
12637c478bd9Sstevel@tonic-gate if (c == '\n' && onelflg)
12647c478bd9Sstevel@tonic-gate onelflg--;
12657c478bd9Sstevel@tonic-gate } while (c == 0);
12667c478bd9Sstevel@tonic-gate return (c);
12677c478bd9Sstevel@tonic-gate }
12687c478bd9Sstevel@tonic-gate
126965b0c20eSnakanon static void
expand_fbuf(void)127065b0c20eSnakanon expand_fbuf(void)
127165b0c20eSnakanon {
127265b0c20eSnakanon tchar **nfbuf =
127365b0c20eSnakanon (tchar **)xcalloc((unsigned)(fblocks + 2), sizeof (tchar **));
127465b0c20eSnakanon
127565b0c20eSnakanon if (fbuf) {
127665b0c20eSnakanon (void) blkcpy(nfbuf, fbuf);
127765b0c20eSnakanon xfree((char *)fbuf);
127865b0c20eSnakanon }
127965b0c20eSnakanon fbuf = nfbuf;
128065b0c20eSnakanon fbuf[fblocks] = (tchar *)xcalloc(BUFSIZ + MB_LEN_MAX,
128165b0c20eSnakanon sizeof (tchar));
128265b0c20eSnakanon fblocks++;
128365b0c20eSnakanon }
128465b0c20eSnakanon
12856c02b4a4Smuffin int
bgetc(void)12866c02b4a4Smuffin bgetc(void)
12877c478bd9Sstevel@tonic-gate {
12886c02b4a4Smuffin int buf, off, c;
12897c478bd9Sstevel@tonic-gate #ifdef FILEC
129065b0c20eSnakanon tchar ttyline[BUFSIZ + MB_LEN_MAX]; /* read_() can return extra bytes */
129165b0c20eSnakanon int roomleft;
12927c478bd9Sstevel@tonic-gate #endif
12937c478bd9Sstevel@tonic-gate
12947c478bd9Sstevel@tonic-gate #ifdef TELL
12957c478bd9Sstevel@tonic-gate if (cantell) {
12967c478bd9Sstevel@tonic-gate if (fseekp < fbobp || fseekp > feobp) {
12977c478bd9Sstevel@tonic-gate fbobp = feobp = fseekp;
12987c478bd9Sstevel@tonic-gate (void) lseek(SHIN, fseekp, 0);
12997c478bd9Sstevel@tonic-gate }
13007c478bd9Sstevel@tonic-gate if (fseekp == feobp) {
13017c478bd9Sstevel@tonic-gate fbobp = feobp;
13027c478bd9Sstevel@tonic-gate do
13037c478bd9Sstevel@tonic-gate c = read_(SHIN, fbuf[0], BUFSIZ);
13047c478bd9Sstevel@tonic-gate while (c < 0 && errno == EINTR);
13057c478bd9Sstevel@tonic-gate if (c <= 0)
13067c478bd9Sstevel@tonic-gate return (-1);
13077c478bd9Sstevel@tonic-gate feobp += c;
13087c478bd9Sstevel@tonic-gate }
13097c478bd9Sstevel@tonic-gate c = fbuf[0][fseekp - fbobp];
13107c478bd9Sstevel@tonic-gate fseekp++;
13117c478bd9Sstevel@tonic-gate return (c);
13127c478bd9Sstevel@tonic-gate }
13137c478bd9Sstevel@tonic-gate #endif
13147c478bd9Sstevel@tonic-gate again:
131565b0c20eSnakanon buf = (int)fseekp / BUFSIZ;
13167c478bd9Sstevel@tonic-gate if (buf >= fblocks) {
131765b0c20eSnakanon expand_fbuf();
13187c478bd9Sstevel@tonic-gate goto again;
13197c478bd9Sstevel@tonic-gate }
13207c478bd9Sstevel@tonic-gate if (fseekp >= feobp) {
132165b0c20eSnakanon buf = (int)feobp / BUFSIZ;
132265b0c20eSnakanon off = (int)feobp % BUFSIZ;
13237c478bd9Sstevel@tonic-gate #ifndef FILEC
13247c478bd9Sstevel@tonic-gate for (;;) {
13257c478bd9Sstevel@tonic-gate c = read_(SHIN, fbuf[buf] + off, BUFSIZ - off);
13267c478bd9Sstevel@tonic-gate #else
13277c478bd9Sstevel@tonic-gate roomleft = BUFSIZ - off;
13287c478bd9Sstevel@tonic-gate for (;;) {
13297c478bd9Sstevel@tonic-gate if (filec && intty) {
133065b0c20eSnakanon c = tenex(ttyline, BUFSIZ);
13317c478bd9Sstevel@tonic-gate if (c > roomleft) {
133265b0c20eSnakanon expand_fbuf();
133365b0c20eSnakanon copy(fbuf[buf] + off, ttyline,
133465b0c20eSnakanon roomleft * sizeof (tchar));
133565b0c20eSnakanon copy(fbuf[buf + 1], ttyline + roomleft,
133665b0c20eSnakanon (c - roomleft) * sizeof (tchar));
133765b0c20eSnakanon } else if (c > 0) {
133865b0c20eSnakanon copy(fbuf[buf] + off, ttyline,
133965b0c20eSnakanon c * sizeof (tchar));
13407c478bd9Sstevel@tonic-gate }
134165b0c20eSnakanon } else {
13427c478bd9Sstevel@tonic-gate c = read_(SHIN, fbuf[buf] + off, roomleft);
134365b0c20eSnakanon if (c > roomleft) {
134465b0c20eSnakanon expand_fbuf();
134565b0c20eSnakanon copy(fbuf[buf + 1],
134665b0c20eSnakanon fbuf[buf] + off + roomleft,
134765b0c20eSnakanon (c - roomleft) * sizeof (tchar));
134865b0c20eSnakanon }
134965b0c20eSnakanon }
13507c478bd9Sstevel@tonic-gate #endif
13517c478bd9Sstevel@tonic-gate if (c >= 0)
13527c478bd9Sstevel@tonic-gate break;
13537c478bd9Sstevel@tonic-gate if (errno == EWOULDBLOCK) {
13547c478bd9Sstevel@tonic-gate int off = 0;
13557c478bd9Sstevel@tonic-gate
13567c478bd9Sstevel@tonic-gate (void) ioctl(SHIN, FIONBIO, (char *)&off);
13577c478bd9Sstevel@tonic-gate } else if (errno != EINTR)
13587c478bd9Sstevel@tonic-gate break;
13597c478bd9Sstevel@tonic-gate }
13607c478bd9Sstevel@tonic-gate if (c <= 0)
13617c478bd9Sstevel@tonic-gate return (-1);
13627c478bd9Sstevel@tonic-gate feobp += c;
13637c478bd9Sstevel@tonic-gate #ifndef FILEC
13647c478bd9Sstevel@tonic-gate goto again;
13657c478bd9Sstevel@tonic-gate #else
13667c478bd9Sstevel@tonic-gate if (filec && !intty)
13677c478bd9Sstevel@tonic-gate goto again;
13687c478bd9Sstevel@tonic-gate #endif
13697c478bd9Sstevel@tonic-gate }
137065b0c20eSnakanon c = fbuf[buf][(int)fseekp % BUFSIZ];
13717c478bd9Sstevel@tonic-gate fseekp++;
13727c478bd9Sstevel@tonic-gate return (c);
13737c478bd9Sstevel@tonic-gate }
13747c478bd9Sstevel@tonic-gate
137565b0c20eSnakanon void
13766c02b4a4Smuffin bfree(void)
13777c478bd9Sstevel@tonic-gate {
13786c02b4a4Smuffin int sb, i;
13797c478bd9Sstevel@tonic-gate
13807c478bd9Sstevel@tonic-gate #ifdef TELL
13817c478bd9Sstevel@tonic-gate if (cantell)
13827c478bd9Sstevel@tonic-gate return;
13837c478bd9Sstevel@tonic-gate #endif
13847c478bd9Sstevel@tonic-gate if (whyles)
13857c478bd9Sstevel@tonic-gate return;
138665b0c20eSnakanon sb = (int)(fseekp - 1) / BUFSIZ;
13877c478bd9Sstevel@tonic-gate if (sb > 0) {
13887c478bd9Sstevel@tonic-gate for (i = 0; i < sb; i++)
13897c478bd9Sstevel@tonic-gate xfree(fbuf[i]);
13907c478bd9Sstevel@tonic-gate (void) blkcpy(fbuf, &fbuf[sb]);
13917c478bd9Sstevel@tonic-gate fseekp -= BUFSIZ * sb;
13927c478bd9Sstevel@tonic-gate feobp -= BUFSIZ * sb;
13937c478bd9Sstevel@tonic-gate fblocks -= sb;
13947c478bd9Sstevel@tonic-gate }
13957c478bd9Sstevel@tonic-gate }
13967c478bd9Sstevel@tonic-gate
13976c02b4a4Smuffin void
13986c02b4a4Smuffin bseek(off_t l)
13997c478bd9Sstevel@tonic-gate {
14006c02b4a4Smuffin struct whyle *wp;
14017c478bd9Sstevel@tonic-gate
14027c478bd9Sstevel@tonic-gate fseekp = l;
14037c478bd9Sstevel@tonic-gate #ifdef TELL
14047c478bd9Sstevel@tonic-gate if (!cantell) {
14057c478bd9Sstevel@tonic-gate #endif
14067c478bd9Sstevel@tonic-gate if (!whyles)
14077c478bd9Sstevel@tonic-gate return;
14087c478bd9Sstevel@tonic-gate for (wp = whyles; wp->w_next; wp = wp->w_next)
14097c478bd9Sstevel@tonic-gate continue;
14107c478bd9Sstevel@tonic-gate if (wp->w_start > l)
14117c478bd9Sstevel@tonic-gate l = wp->w_start;
14127c478bd9Sstevel@tonic-gate #ifdef TELL
14137c478bd9Sstevel@tonic-gate }
14147c478bd9Sstevel@tonic-gate #endif
14157c478bd9Sstevel@tonic-gate }
14167c478bd9Sstevel@tonic-gate
14177c478bd9Sstevel@tonic-gate /* any similarity to bell telephone is purely accidental */
14187c478bd9Sstevel@tonic-gate #ifndef btell
14197c478bd9Sstevel@tonic-gate off_t
14206c02b4a4Smuffin btell(void)
14217c478bd9Sstevel@tonic-gate {
14227c478bd9Sstevel@tonic-gate
14237c478bd9Sstevel@tonic-gate return (fseekp);
14247c478bd9Sstevel@tonic-gate }
14257c478bd9Sstevel@tonic-gate #endif
14267c478bd9Sstevel@tonic-gate
14276c02b4a4Smuffin void
14286c02b4a4Smuffin btoeof(void)
14297c478bd9Sstevel@tonic-gate {
14307c478bd9Sstevel@tonic-gate
14317c478bd9Sstevel@tonic-gate (void) lseek(SHIN, (off_t)0, 2);
14327c478bd9Sstevel@tonic-gate fseekp = feobp;
14337c478bd9Sstevel@tonic-gate wfree();
14347c478bd9Sstevel@tonic-gate bfree();
14357c478bd9Sstevel@tonic-gate }
14367c478bd9Sstevel@tonic-gate
14377c478bd9Sstevel@tonic-gate #ifdef TELL
14386c02b4a4Smuffin void
14396c02b4a4Smuffin settell(void)
14407c478bd9Sstevel@tonic-gate {
14417c478bd9Sstevel@tonic-gate
14427c478bd9Sstevel@tonic-gate cantell = 0;
14437c478bd9Sstevel@tonic-gate if (arginp || onelflg || intty)
14447c478bd9Sstevel@tonic-gate return;
14457c478bd9Sstevel@tonic-gate if (lseek(SHIN, (off_t)0, 1) < 0 || errno == ESPIPE)
14467c478bd9Sstevel@tonic-gate return;
144765b0c20eSnakanon fbuf = (tchar **)xcalloc(2, sizeof (tchar **));
14487c478bd9Sstevel@tonic-gate fblocks = 1;
144965b0c20eSnakanon fbuf[0] = (tchar *)xcalloc(BUFSIZ + MB_LEN_MAX, sizeof (tchar));
14507c478bd9Sstevel@tonic-gate fseekp = fbobp = feobp = lseek(SHIN, (off_t)0, 1);
14517c478bd9Sstevel@tonic-gate cantell = 1;
14527c478bd9Sstevel@tonic-gate }
14537c478bd9Sstevel@tonic-gate #endif
1454