17c478bd9Sstevel@tonic-gate /* 2*70a587ddSchin * 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 #pragma ident "%Z%%M% %I% %E% SMI" 167c478bd9Sstevel@tonic-gate 177c478bd9Sstevel@tonic-gate #include <unistd.h> /* for lseek prototype */ 187c478bd9Sstevel@tonic-gate #include "sh.h" 197c478bd9Sstevel@tonic-gate #include "sh.tconst.h" 207c478bd9Sstevel@tonic-gate #include <sys/filio.h> 217c478bd9Sstevel@tonic-gate #include <sys/ttold.h> 227c478bd9Sstevel@tonic-gate #define RAW O_RAW 237c478bd9Sstevel@tonic-gate /* 247c478bd9Sstevel@tonic-gate * C shell 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* 287c478bd9Sstevel@tonic-gate * These lexical routines read input and form lists of words. 297c478bd9Sstevel@tonic-gate * There is some involved processing here, because of the complications 307c478bd9Sstevel@tonic-gate * of input buffering, and especially because of history substitution. 317c478bd9Sstevel@tonic-gate */ 327c478bd9Sstevel@tonic-gate 336c02b4a4Smuffin tchar *word(void); 346c02b4a4Smuffin tchar getC1(int); 356c02b4a4Smuffin tchar *subword(tchar *, int, bool *); 366c02b4a4Smuffin void getdol(void); 376c02b4a4Smuffin void addla(tchar *); 386c02b4a4Smuffin void getexcl(tchar); 396c02b4a4Smuffin void noev(tchar *); 406c02b4a4Smuffin void setexclp(tchar *); 416c02b4a4Smuffin void unreadc(tchar); 426c02b4a4Smuffin int readc(bool); 436c02b4a4Smuffin struct wordent *dosub(int, struct wordent *, bool); 446c02b4a4Smuffin struct Hist *findev(tchar *, bool); 456c02b4a4Smuffin struct wordent *gethent(int); 466c02b4a4Smuffin struct wordent *getsub(struct wordent *); 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate /* 497c478bd9Sstevel@tonic-gate * Peekc is a peek characer for getC, peekread for readc. 507c478bd9Sstevel@tonic-gate * There is a subtlety here in many places... history routines 517c478bd9Sstevel@tonic-gate * will read ahead and then insert stuff into the input stream. 527c478bd9Sstevel@tonic-gate * If they push back a character then they must push it behind 537c478bd9Sstevel@tonic-gate * the text substituted by the history substitution. On the other 547c478bd9Sstevel@tonic-gate * hand in several places we need 2 peek characters. To make this 557c478bd9Sstevel@tonic-gate * all work, the history routines read with getC, and make use both 567c478bd9Sstevel@tonic-gate * of ungetC and unreadc. The key observation is that the state 577c478bd9Sstevel@tonic-gate * of getC at the call of a history reference is such that calls 587c478bd9Sstevel@tonic-gate * to getC from the history routines will always yield calls of 597c478bd9Sstevel@tonic-gate * readc, unless this peeking is involved. That is to say that during 607c478bd9Sstevel@tonic-gate * getexcl the variables lap, exclp, and exclnxt are all zero. 617c478bd9Sstevel@tonic-gate * 627c478bd9Sstevel@tonic-gate * Getdol invokes history substitution, hence the extra peek, peekd, 637c478bd9Sstevel@tonic-gate * which it can ungetD to be before history substitutions. 647c478bd9Sstevel@tonic-gate */ 657c478bd9Sstevel@tonic-gate tchar peekc, peekd; 667c478bd9Sstevel@tonic-gate tchar peekread; 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate tchar *exclp; /* (Tail of) current word from ! subst */ 697c478bd9Sstevel@tonic-gate struct wordent *exclnxt; /* The rest of the ! subst words */ 707c478bd9Sstevel@tonic-gate int exclc; /* Count of remainig words in ! subst */ 717c478bd9Sstevel@tonic-gate tchar *alvecp; /* "Globp" for alias resubstitution */ 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate /* 747c478bd9Sstevel@tonic-gate * Lex returns to its caller not only a wordlist (as a "var" parameter) 757c478bd9Sstevel@tonic-gate * but also whether a history substitution occurred. This is used in 767c478bd9Sstevel@tonic-gate * the main (process) routine to determine whether to echo, and also 777c478bd9Sstevel@tonic-gate * when called by the alias routine to determine whether to keep the 787c478bd9Sstevel@tonic-gate * argument list. 797c478bd9Sstevel@tonic-gate */ 807c478bd9Sstevel@tonic-gate bool hadhist; 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate tchar getCtmp; 837c478bd9Sstevel@tonic-gate #define getC(f) ((getCtmp = peekc) ? (peekc = 0, getCtmp) : getC1(f)) 847c478bd9Sstevel@tonic-gate #define ungetC(c) peekc = c 857c478bd9Sstevel@tonic-gate #define ungetD(c) peekd = c 867c478bd9Sstevel@tonic-gate 876c02b4a4Smuffin bool 886c02b4a4Smuffin lex(struct wordent *hp) 897c478bd9Sstevel@tonic-gate { 906c02b4a4Smuffin struct wordent *wdp; 917c478bd9Sstevel@tonic-gate int c; 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate #ifdef TRACE 947c478bd9Sstevel@tonic-gate tprintf("TRACE- lex()\n"); 957c478bd9Sstevel@tonic-gate #endif 967c478bd9Sstevel@tonic-gate lineloc = btell(); 977c478bd9Sstevel@tonic-gate hp->next = hp->prev = hp; 987c478bd9Sstevel@tonic-gate hp->word = S_ /* "" */; 997c478bd9Sstevel@tonic-gate alvecp = 0, hadhist = 0; 1007c478bd9Sstevel@tonic-gate do 1017c478bd9Sstevel@tonic-gate c = readc(0); 1027c478bd9Sstevel@tonic-gate while (issp(c)); 1037c478bd9Sstevel@tonic-gate /* make sure history is enabled */ 1047c478bd9Sstevel@tonic-gate if (HIST && c == HISTSUB && intty) 1057c478bd9Sstevel@tonic-gate /* ^lef^rit from tty is short !:s^lef^rit */ 1067c478bd9Sstevel@tonic-gate getexcl(c); 1077c478bd9Sstevel@tonic-gate else 1087c478bd9Sstevel@tonic-gate unreadc(c); 1097c478bd9Sstevel@tonic-gate wdp = hp; 1107c478bd9Sstevel@tonic-gate /* 1117c478bd9Sstevel@tonic-gate * The following loop is written so that the links needed 1127c478bd9Sstevel@tonic-gate * by freelex will be ready and rarin to go even if it is 1137c478bd9Sstevel@tonic-gate * interrupted. 1147c478bd9Sstevel@tonic-gate */ 1157c478bd9Sstevel@tonic-gate do { 1166c02b4a4Smuffin struct wordent *new = (struct wordent *)xalloc(sizeof *wdp); 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate new->word = 0; 1197c478bd9Sstevel@tonic-gate new->prev = wdp; 1207c478bd9Sstevel@tonic-gate new->next = hp; 1217c478bd9Sstevel@tonic-gate wdp->next = new; 1227c478bd9Sstevel@tonic-gate wdp = new; 1237c478bd9Sstevel@tonic-gate wdp->word = word(); 1247c478bd9Sstevel@tonic-gate } while (wdp->word[0] != '\n'); 1257c478bd9Sstevel@tonic-gate #ifdef TRACE 1267c478bd9Sstevel@tonic-gate tprintf("Exiting lex()\n"); 1277c478bd9Sstevel@tonic-gate #endif 1287c478bd9Sstevel@tonic-gate hp->prev = wdp; 1297c478bd9Sstevel@tonic-gate return (hadhist); 1307c478bd9Sstevel@tonic-gate } 1317c478bd9Sstevel@tonic-gate 1326c02b4a4Smuffin void 1336c02b4a4Smuffin prlex(struct wordent *sp0) 1347c478bd9Sstevel@tonic-gate { 1356c02b4a4Smuffin struct wordent *sp = sp0->next; 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate #ifdef TRACE 1387c478bd9Sstevel@tonic-gate tprintf("TRACE- prlex()\n"); 1397c478bd9Sstevel@tonic-gate #endif 1407c478bd9Sstevel@tonic-gate for (;;) { 1417c478bd9Sstevel@tonic-gate printf("%t", sp->word); 1427c478bd9Sstevel@tonic-gate sp = sp->next; 1437c478bd9Sstevel@tonic-gate if (sp == sp0) 1447c478bd9Sstevel@tonic-gate break; 1457c478bd9Sstevel@tonic-gate if (sp->word[0] != '\n') 1467c478bd9Sstevel@tonic-gate Putchar(' '); 1477c478bd9Sstevel@tonic-gate } 1487c478bd9Sstevel@tonic-gate } 1497c478bd9Sstevel@tonic-gate 1506c02b4a4Smuffin void 1516c02b4a4Smuffin copylex(struct wordent *hp, struct wordent *fp) 1527c478bd9Sstevel@tonic-gate { 1536c02b4a4Smuffin struct wordent *wdp; 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate #ifdef TRACE 1567c478bd9Sstevel@tonic-gate tprintf("TRACE- copylex()\n"); 1577c478bd9Sstevel@tonic-gate #endif 1587c478bd9Sstevel@tonic-gate wdp = hp; 1597c478bd9Sstevel@tonic-gate fp = fp->next; 1607c478bd9Sstevel@tonic-gate do { 1616c02b4a4Smuffin struct wordent *new = (struct wordent *)xalloc(sizeof *wdp); 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate new->prev = wdp; 1647c478bd9Sstevel@tonic-gate new->next = hp; 1657c478bd9Sstevel@tonic-gate wdp->next = new; 1667c478bd9Sstevel@tonic-gate wdp = new; 1677c478bd9Sstevel@tonic-gate wdp->word = savestr(fp->word); 1687c478bd9Sstevel@tonic-gate fp = fp->next; 1697c478bd9Sstevel@tonic-gate } while (wdp->word[0] != '\n'); 1707c478bd9Sstevel@tonic-gate hp->prev = wdp; 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate 1736c02b4a4Smuffin void 1746c02b4a4Smuffin freelex(struct wordent *vp) 1757c478bd9Sstevel@tonic-gate { 1766c02b4a4Smuffin struct wordent *fp; 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate #ifdef TRACE 1797c478bd9Sstevel@tonic-gate tprintf("TRACE- freelex()\n"); 1807c478bd9Sstevel@tonic-gate #endif 1817c478bd9Sstevel@tonic-gate while (vp->next != vp) { 1827c478bd9Sstevel@tonic-gate fp = vp->next; 1837c478bd9Sstevel@tonic-gate vp->next = fp->next; 18465b0c20eSnakanon xfree(fp->word); 18565b0c20eSnakanon xfree(fp); 1867c478bd9Sstevel@tonic-gate } 1877c478bd9Sstevel@tonic-gate vp->prev = vp; 1887c478bd9Sstevel@tonic-gate } 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate tchar * 1916c02b4a4Smuffin word(void) 1927c478bd9Sstevel@tonic-gate { 1936c02b4a4Smuffin tchar c, c1; 1946c02b4a4Smuffin tchar *wp; 1957c478bd9Sstevel@tonic-gate tchar wbuf[BUFSIZ]; 1966c02b4a4Smuffin bool dolflg; 1976c02b4a4Smuffin int i; 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate #ifdef TRACE 2007c478bd9Sstevel@tonic-gate tprintf("TRACE- word()\n"); 2017c478bd9Sstevel@tonic-gate #endif 2027c478bd9Sstevel@tonic-gate wp = wbuf; 2037c478bd9Sstevel@tonic-gate i = BUFSIZ - 4; 2047c478bd9Sstevel@tonic-gate loop: 2057c478bd9Sstevel@tonic-gate while (issp(c = getC(DOALL))) 2067c478bd9Sstevel@tonic-gate ; 2077c478bd9Sstevel@tonic-gate if (cmap(c, _META|_ESC)||isauxsp(c)) 2087c478bd9Sstevel@tonic-gate switch (c) { 2097c478bd9Sstevel@tonic-gate case '&': 2107c478bd9Sstevel@tonic-gate case '|': 2117c478bd9Sstevel@tonic-gate case '<': 2127c478bd9Sstevel@tonic-gate case '>': 2137c478bd9Sstevel@tonic-gate *wp++ = c; 2147c478bd9Sstevel@tonic-gate c1 = getC(DOALL); 2157c478bd9Sstevel@tonic-gate if (c1 == c) 2167c478bd9Sstevel@tonic-gate *wp++ = c1; 2177c478bd9Sstevel@tonic-gate else 2187c478bd9Sstevel@tonic-gate ungetC(c1); 2197c478bd9Sstevel@tonic-gate goto ret; 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate case '#': 2227c478bd9Sstevel@tonic-gate if (intty) 2237c478bd9Sstevel@tonic-gate break; 2247c478bd9Sstevel@tonic-gate c = 0; 2257c478bd9Sstevel@tonic-gate do { 2267c478bd9Sstevel@tonic-gate c1 = c; 2277c478bd9Sstevel@tonic-gate c = getC(0); 2287c478bd9Sstevel@tonic-gate } while (c != '\n'); 2297c478bd9Sstevel@tonic-gate if (c1 == '\\') 2307c478bd9Sstevel@tonic-gate goto loop; 2317c478bd9Sstevel@tonic-gate /* fall into ... */ 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate case ';': 2347c478bd9Sstevel@tonic-gate case '(': 2357c478bd9Sstevel@tonic-gate case ')': 2367c478bd9Sstevel@tonic-gate case '\n': 2377c478bd9Sstevel@tonic-gate *wp++ = c; 2387c478bd9Sstevel@tonic-gate goto ret; 2397c478bd9Sstevel@tonic-gate 2407c478bd9Sstevel@tonic-gate case '\\': 2417c478bd9Sstevel@tonic-gate c = getC(0); 2427c478bd9Sstevel@tonic-gate if (c == '\n') { 2437c478bd9Sstevel@tonic-gate if (onelflg == 1) 2447c478bd9Sstevel@tonic-gate onelflg = 2; 2457c478bd9Sstevel@tonic-gate goto loop; 2467c478bd9Sstevel@tonic-gate } 2477c478bd9Sstevel@tonic-gate if (c != HIST) 2487c478bd9Sstevel@tonic-gate *wp++ = '\\', --i; 2497c478bd9Sstevel@tonic-gate c |= QUOTE; 2507c478bd9Sstevel@tonic-gate } 2517c478bd9Sstevel@tonic-gate c1 = 0; 2527c478bd9Sstevel@tonic-gate dolflg = DOALL; 2537c478bd9Sstevel@tonic-gate for (;;) { 2547c478bd9Sstevel@tonic-gate if (c1) { 2557c478bd9Sstevel@tonic-gate if (c == c1) { 2567c478bd9Sstevel@tonic-gate c1 = 0; 2577c478bd9Sstevel@tonic-gate dolflg = DOALL; 2587c478bd9Sstevel@tonic-gate } else if (c == '\\') { 2597c478bd9Sstevel@tonic-gate c = getC(0); 2607c478bd9Sstevel@tonic-gate if (c == HIST) 2617c478bd9Sstevel@tonic-gate c |= QUOTE; 2627c478bd9Sstevel@tonic-gate else { 2637c478bd9Sstevel@tonic-gate if (c == '\n') 26465b0c20eSnakanon #if 0 2657c478bd9Sstevel@tonic-gate if (c1 == '`') 2667c478bd9Sstevel@tonic-gate c = ' '; 2677c478bd9Sstevel@tonic-gate else 26865b0c20eSnakanon #endif 2697c478bd9Sstevel@tonic-gate c |= QUOTE; 2707c478bd9Sstevel@tonic-gate ungetC(c); 2717c478bd9Sstevel@tonic-gate c = '\\'; 2727c478bd9Sstevel@tonic-gate } 2737c478bd9Sstevel@tonic-gate } else if (c == '\n') { 2747c478bd9Sstevel@tonic-gate seterrc(gettext("Unmatched "), c1); 2757c478bd9Sstevel@tonic-gate ungetC(c); 2767c478bd9Sstevel@tonic-gate break; 2777c478bd9Sstevel@tonic-gate } 2787c478bd9Sstevel@tonic-gate } else if (cmap(c, _META|_Q|_Q1|_ESC)||isauxsp(c)) { 2797c478bd9Sstevel@tonic-gate if (c == '\\') { 2807c478bd9Sstevel@tonic-gate c = getC(0); 2817c478bd9Sstevel@tonic-gate if (c == '\n') { 2827c478bd9Sstevel@tonic-gate if (onelflg == 1) 2837c478bd9Sstevel@tonic-gate onelflg = 2; 2847c478bd9Sstevel@tonic-gate break; 2857c478bd9Sstevel@tonic-gate } 2867c478bd9Sstevel@tonic-gate if (c != HIST) 2877c478bd9Sstevel@tonic-gate *wp++ = '\\', --i; 2887c478bd9Sstevel@tonic-gate c |= QUOTE; 2897c478bd9Sstevel@tonic-gate } else if (cmap(c, _Q|_Q1)) { /* '"` */ 2907c478bd9Sstevel@tonic-gate c1 = c; 2917c478bd9Sstevel@tonic-gate dolflg = c == '"' ? DOALL : DOEXCL; 2927c478bd9Sstevel@tonic-gate } else if (c != '#' || !intty) { 2937c478bd9Sstevel@tonic-gate ungetC(c); 2947c478bd9Sstevel@tonic-gate break; 2957c478bd9Sstevel@tonic-gate } 2967c478bd9Sstevel@tonic-gate } 2977c478bd9Sstevel@tonic-gate if (--i > 0) { 2987c478bd9Sstevel@tonic-gate *wp++ = c; 2997c478bd9Sstevel@tonic-gate c = getC(dolflg); 3007c478bd9Sstevel@tonic-gate } else { 3017c478bd9Sstevel@tonic-gate seterr("Word too long"); 3027c478bd9Sstevel@tonic-gate wp = &wbuf[1]; 3037c478bd9Sstevel@tonic-gate break; 3047c478bd9Sstevel@tonic-gate } 3057c478bd9Sstevel@tonic-gate } 3067c478bd9Sstevel@tonic-gate ret: 3077c478bd9Sstevel@tonic-gate *wp = 0; 3087c478bd9Sstevel@tonic-gate #ifdef TRACE 3097c478bd9Sstevel@tonic-gate tprintf("word() returning:%t\n", wbuf); 3107c478bd9Sstevel@tonic-gate #endif 3117c478bd9Sstevel@tonic-gate return (savestr(wbuf)); 3127c478bd9Sstevel@tonic-gate } 3137c478bd9Sstevel@tonic-gate 3146c02b4a4Smuffin tchar 3156c02b4a4Smuffin getC1(int flag) 3167c478bd9Sstevel@tonic-gate { 3176c02b4a4Smuffin tchar c; 3187c478bd9Sstevel@tonic-gate 3197c478bd9Sstevel@tonic-gate top: 3207c478bd9Sstevel@tonic-gate if (c = peekc) { 3217c478bd9Sstevel@tonic-gate peekc = 0; 3227c478bd9Sstevel@tonic-gate return (c); 3237c478bd9Sstevel@tonic-gate } 3247c478bd9Sstevel@tonic-gate if (lap) { 3257c478bd9Sstevel@tonic-gate if ((c = *lap++) == 0) 3267c478bd9Sstevel@tonic-gate lap = 0; 3277c478bd9Sstevel@tonic-gate else { 3287c478bd9Sstevel@tonic-gate /* 3297c478bd9Sstevel@tonic-gate * don't quote things if there was an error (err!=0) 3307c478bd9Sstevel@tonic-gate * the input is original, not from a substitution and 3317c478bd9Sstevel@tonic-gate * therefore should not be quoted 3327c478bd9Sstevel@tonic-gate */ 3337c478bd9Sstevel@tonic-gate if (!err && cmap(c, _META|_Q|_Q1)||isauxsp(c)) 3347c478bd9Sstevel@tonic-gate c |= QUOTE; 3357c478bd9Sstevel@tonic-gate return (c); 3367c478bd9Sstevel@tonic-gate } 3377c478bd9Sstevel@tonic-gate } 3387c478bd9Sstevel@tonic-gate if (c = peekd) { 3397c478bd9Sstevel@tonic-gate peekd = 0; 3407c478bd9Sstevel@tonic-gate return (c); 3417c478bd9Sstevel@tonic-gate } 3427c478bd9Sstevel@tonic-gate if (exclp) { 3437c478bd9Sstevel@tonic-gate if (c = *exclp++) 3447c478bd9Sstevel@tonic-gate return (c); 3457c478bd9Sstevel@tonic-gate if (exclnxt && --exclc >= 0) { 3467c478bd9Sstevel@tonic-gate exclnxt = exclnxt->next; 3477c478bd9Sstevel@tonic-gate setexclp(exclnxt->word); 3487c478bd9Sstevel@tonic-gate return (' '); 3497c478bd9Sstevel@tonic-gate } 3507c478bd9Sstevel@tonic-gate exclp = 0; 3517c478bd9Sstevel@tonic-gate exclnxt = 0; 3527c478bd9Sstevel@tonic-gate } 3537c478bd9Sstevel@tonic-gate if (exclnxt) { 3547c478bd9Sstevel@tonic-gate exclnxt = exclnxt->next; 3557c478bd9Sstevel@tonic-gate if (--exclc < 0) 3567c478bd9Sstevel@tonic-gate exclnxt = 0; 3577c478bd9Sstevel@tonic-gate else 3587c478bd9Sstevel@tonic-gate setexclp(exclnxt->word); 3597c478bd9Sstevel@tonic-gate goto top; 3607c478bd9Sstevel@tonic-gate } 3617c478bd9Sstevel@tonic-gate c = readc(0); 3627c478bd9Sstevel@tonic-gate if (c == '$' && (flag & DODOL)) { 3637c478bd9Sstevel@tonic-gate getdol(); 3647c478bd9Sstevel@tonic-gate goto top; 3657c478bd9Sstevel@tonic-gate } 3667c478bd9Sstevel@tonic-gate if (c == HIST && (flag & DOEXCL)) { 3677c478bd9Sstevel@tonic-gate getexcl(0); 3687c478bd9Sstevel@tonic-gate goto top; 3697c478bd9Sstevel@tonic-gate } 3707c478bd9Sstevel@tonic-gate return (c); 3717c478bd9Sstevel@tonic-gate } 3727c478bd9Sstevel@tonic-gate 3736c02b4a4Smuffin void 3746c02b4a4Smuffin getdol(void) 3757c478bd9Sstevel@tonic-gate { 3766c02b4a4Smuffin tchar *np, *p; 3777c478bd9Sstevel@tonic-gate tchar name[MAX_VREF_LEN]; 3786c02b4a4Smuffin int c; 3797c478bd9Sstevel@tonic-gate int sc; 3807c478bd9Sstevel@tonic-gate bool special = 0; 3817c478bd9Sstevel@tonic-gate 3827c478bd9Sstevel@tonic-gate #ifdef TRACE 3837c478bd9Sstevel@tonic-gate tprintf("TRACE- getdol()\n"); 3847c478bd9Sstevel@tonic-gate #endif 3857c478bd9Sstevel@tonic-gate np = name, *np++ = '$'; 3867c478bd9Sstevel@tonic-gate c = sc = getC(DOEXCL); 3877c478bd9Sstevel@tonic-gate if (isspnl(c)) { 3887c478bd9Sstevel@tonic-gate ungetD(c); 3897c478bd9Sstevel@tonic-gate ungetC('$' | QUOTE); 3907c478bd9Sstevel@tonic-gate return; 3917c478bd9Sstevel@tonic-gate } 3927c478bd9Sstevel@tonic-gate if (c == '{') 3937c478bd9Sstevel@tonic-gate *np++ = c, c = getC(DOEXCL); 3947c478bd9Sstevel@tonic-gate if (c == '#' || c == '?') 3957c478bd9Sstevel@tonic-gate special++, *np++ = c, c = getC(DOEXCL); 3967c478bd9Sstevel@tonic-gate *np++ = c; 3977c478bd9Sstevel@tonic-gate switch (c) { 3987c478bd9Sstevel@tonic-gate 3997c478bd9Sstevel@tonic-gate case '<': 4007c478bd9Sstevel@tonic-gate case '$': 4017c478bd9Sstevel@tonic-gate case '*': 4027c478bd9Sstevel@tonic-gate if (special) 4037c478bd9Sstevel@tonic-gate goto vsyn; 4047c478bd9Sstevel@tonic-gate goto ret; 4057c478bd9Sstevel@tonic-gate 4067c478bd9Sstevel@tonic-gate case '\n': 4077c478bd9Sstevel@tonic-gate ungetD(c); 4087c478bd9Sstevel@tonic-gate np--; 4097c478bd9Sstevel@tonic-gate goto vsyn; 4107c478bd9Sstevel@tonic-gate 4117c478bd9Sstevel@tonic-gate default: 4127c478bd9Sstevel@tonic-gate p = np; 4137c478bd9Sstevel@tonic-gate if (digit(c)) { 4147c478bd9Sstevel@tonic-gate /* make sure the variable names are MAX_VAR_LEN chars or less */ 4157c478bd9Sstevel@tonic-gate while (digit(c = getC(DOEXCL)) && (np - p) < MAX_VAR_LEN) { 4167c478bd9Sstevel@tonic-gate *np++ = c; 4177c478bd9Sstevel@tonic-gate } 4187c478bd9Sstevel@tonic-gate } else if (letter(c)) { 4197c478bd9Sstevel@tonic-gate while ((letter(c = getC(DOEXCL)) || digit(c)) && 4207c478bd9Sstevel@tonic-gate (np - p) < MAX_VAR_LEN) { 4217c478bd9Sstevel@tonic-gate *np++ = c; 4227c478bd9Sstevel@tonic-gate } 4237c478bd9Sstevel@tonic-gate } 4247c478bd9Sstevel@tonic-gate else 4257c478bd9Sstevel@tonic-gate goto vsyn; 4267c478bd9Sstevel@tonic-gate 4277c478bd9Sstevel@tonic-gate if ((np - p) > MAX_VAR_LEN) 4287c478bd9Sstevel@tonic-gate { 4297c478bd9Sstevel@tonic-gate seterr("Variable name too long"); 4307c478bd9Sstevel@tonic-gate goto ret; 4317c478bd9Sstevel@tonic-gate } 4327c478bd9Sstevel@tonic-gate } 4337c478bd9Sstevel@tonic-gate if (c == '[') { 4347c478bd9Sstevel@tonic-gate *np++ = c; 4357c478bd9Sstevel@tonic-gate do { 4367c478bd9Sstevel@tonic-gate c = getC(DOEXCL); 4377c478bd9Sstevel@tonic-gate if (c == '\n') { 4387c478bd9Sstevel@tonic-gate ungetD(c); 4397c478bd9Sstevel@tonic-gate np--; 4407c478bd9Sstevel@tonic-gate goto vsyn; 4417c478bd9Sstevel@tonic-gate } 4427c478bd9Sstevel@tonic-gate /* need to leave space for possible modifiers */ 4437c478bd9Sstevel@tonic-gate if (np >= &name[MAX_VREF_LEN - 8]) 4447c478bd9Sstevel@tonic-gate { 4457c478bd9Sstevel@tonic-gate seterr("Variable reference too long"); 4467c478bd9Sstevel@tonic-gate goto ret; 4477c478bd9Sstevel@tonic-gate } 4487c478bd9Sstevel@tonic-gate *np++ = c; 4497c478bd9Sstevel@tonic-gate } while (c != ']'); 4507c478bd9Sstevel@tonic-gate c = getC(DOEXCL); 4517c478bd9Sstevel@tonic-gate } 4527c478bd9Sstevel@tonic-gate if (c == ':') { 4537c478bd9Sstevel@tonic-gate *np++ = c, c = getC(DOEXCL); 4547c478bd9Sstevel@tonic-gate if (c == 'g') 4557c478bd9Sstevel@tonic-gate *np++ = c, c = getC(DOEXCL); 4567c478bd9Sstevel@tonic-gate *np++ = c; 4577c478bd9Sstevel@tonic-gate if (!any(c, S_htrqxe)) 4587c478bd9Sstevel@tonic-gate goto vsyn; 4597c478bd9Sstevel@tonic-gate } else 4607c478bd9Sstevel@tonic-gate ungetD(c); 4617c478bd9Sstevel@tonic-gate if (sc == '{') { 4627c478bd9Sstevel@tonic-gate c = getC(DOEXCL); 4637c478bd9Sstevel@tonic-gate if (c != '}') { 4647c478bd9Sstevel@tonic-gate ungetC(c); 4657c478bd9Sstevel@tonic-gate goto vsyn; 4667c478bd9Sstevel@tonic-gate } 4677c478bd9Sstevel@tonic-gate *np++ = c; 4687c478bd9Sstevel@tonic-gate } 4697c478bd9Sstevel@tonic-gate ret: 4707c478bd9Sstevel@tonic-gate *np = 0; 4717c478bd9Sstevel@tonic-gate addla(name); 4727c478bd9Sstevel@tonic-gate return; 4737c478bd9Sstevel@tonic-gate 4747c478bd9Sstevel@tonic-gate vsyn: 4757c478bd9Sstevel@tonic-gate seterr("Variable syntax"); 4767c478bd9Sstevel@tonic-gate goto ret; 4777c478bd9Sstevel@tonic-gate } 4787c478bd9Sstevel@tonic-gate 4796c02b4a4Smuffin void 4806c02b4a4Smuffin addla(tchar *cp) 4817c478bd9Sstevel@tonic-gate { 4827c478bd9Sstevel@tonic-gate tchar *buf; 4837c478bd9Sstevel@tonic-gate int len = 0; 4847c478bd9Sstevel@tonic-gate 4857c478bd9Sstevel@tonic-gate #ifdef TRACE 4867c478bd9Sstevel@tonic-gate tprintf("TRACE- addla()\n"); 4877c478bd9Sstevel@tonic-gate #endif 4887c478bd9Sstevel@tonic-gate if (lap) { 4897c478bd9Sstevel@tonic-gate len = strlen_(lap); 4907c478bd9Sstevel@tonic-gate buf = xalloc((len+1) * sizeof (tchar)); 4917c478bd9Sstevel@tonic-gate (void) strcpy_(buf, lap); 4927c478bd9Sstevel@tonic-gate } 4937c478bd9Sstevel@tonic-gate len += strlen_(cp); 4947c478bd9Sstevel@tonic-gate 4957c478bd9Sstevel@tonic-gate /* len+5 is allow 4 additional charecters just to be safe */ 4967c478bd9Sstevel@tonic-gate labuf = xrealloc(labuf, (len+5) * sizeof (tchar)); 4977c478bd9Sstevel@tonic-gate (void) strcpy_(labuf, cp); 4987c478bd9Sstevel@tonic-gate if (lap) { 4997c478bd9Sstevel@tonic-gate (void) strcat_(labuf, buf); 50065b0c20eSnakanon xfree(buf); 5017c478bd9Sstevel@tonic-gate } 5027c478bd9Sstevel@tonic-gate lap = labuf; 5037c478bd9Sstevel@tonic-gate } 5047c478bd9Sstevel@tonic-gate 505*70a587ddSchin tchar lhsb[256]; 506*70a587ddSchin tchar slhs[256]; 507*70a587ddSchin tchar rhsb[512]; 5087c478bd9Sstevel@tonic-gate int quesarg; 5097c478bd9Sstevel@tonic-gate 5106c02b4a4Smuffin void 5116c02b4a4Smuffin getexcl(tchar sc) 5127c478bd9Sstevel@tonic-gate { 5136c02b4a4Smuffin struct wordent *hp, *ip; 5147c478bd9Sstevel@tonic-gate int left, right, dol; 5156c02b4a4Smuffin int c; 5167c478bd9Sstevel@tonic-gate 5177c478bd9Sstevel@tonic-gate #ifdef TRACE 5187c478bd9Sstevel@tonic-gate tprintf("TRACE- getexcl()\n"); 5197c478bd9Sstevel@tonic-gate #endif 5207c478bd9Sstevel@tonic-gate if (sc == 0) { 5217c478bd9Sstevel@tonic-gate sc = getC(0); 5227c478bd9Sstevel@tonic-gate if (sc != '{') { 5237c478bd9Sstevel@tonic-gate ungetC(sc); 5247c478bd9Sstevel@tonic-gate sc = 0; 5257c478bd9Sstevel@tonic-gate } 5267c478bd9Sstevel@tonic-gate } 5277c478bd9Sstevel@tonic-gate quesarg = -1; 5287c478bd9Sstevel@tonic-gate lastev = eventno; 5297c478bd9Sstevel@tonic-gate hp = gethent(sc); 5307c478bd9Sstevel@tonic-gate if (hp == 0) 5317c478bd9Sstevel@tonic-gate return; 5327c478bd9Sstevel@tonic-gate hadhist = 1; 5337c478bd9Sstevel@tonic-gate dol = 0; 5347c478bd9Sstevel@tonic-gate if (hp == alhistp) 5357c478bd9Sstevel@tonic-gate for (ip = hp->next->next; ip != alhistt; ip = ip->next) 5367c478bd9Sstevel@tonic-gate dol++; 5377c478bd9Sstevel@tonic-gate else 5387c478bd9Sstevel@tonic-gate for (ip = hp->next->next; ip != hp->prev; ip = ip->next) 5397c478bd9Sstevel@tonic-gate dol++; 5407c478bd9Sstevel@tonic-gate left = 0, right = dol; 5417c478bd9Sstevel@tonic-gate if (sc == HISTSUB) { 5427c478bd9Sstevel@tonic-gate ungetC('s'), unreadc(HISTSUB), c = ':'; 5437c478bd9Sstevel@tonic-gate goto subst; 5447c478bd9Sstevel@tonic-gate } 5457c478bd9Sstevel@tonic-gate c = getC(0); 5467c478bd9Sstevel@tonic-gate /* if (!any(c, ":^$*-%")) */ /* change needed for char -> tchar */ 5477c478bd9Sstevel@tonic-gate if (! (c == ':' || c == '^' || c == '$' || c == '*' || 5487c478bd9Sstevel@tonic-gate c == '-' || c == '%')) 5497c478bd9Sstevel@tonic-gate goto subst; 5507c478bd9Sstevel@tonic-gate left = right = -1; 5517c478bd9Sstevel@tonic-gate if (c == ':') { 5527c478bd9Sstevel@tonic-gate c = getC(0); 5537c478bd9Sstevel@tonic-gate unreadc(c); 5547c478bd9Sstevel@tonic-gate if (letter(c) || c == '&') { 5557c478bd9Sstevel@tonic-gate c = ':'; 5567c478bd9Sstevel@tonic-gate left = 0, right = dol; 5577c478bd9Sstevel@tonic-gate goto subst; 5587c478bd9Sstevel@tonic-gate } 5597c478bd9Sstevel@tonic-gate } else 5607c478bd9Sstevel@tonic-gate ungetC(c); 5617c478bd9Sstevel@tonic-gate if (!getsel(&left, &right, dol)) 5627c478bd9Sstevel@tonic-gate return; 5637c478bd9Sstevel@tonic-gate c = getC(0); 5647c478bd9Sstevel@tonic-gate if (c == '*') 5657c478bd9Sstevel@tonic-gate ungetC(c), c = '-'; 5667c478bd9Sstevel@tonic-gate if (c == '-') { 5677c478bd9Sstevel@tonic-gate if (!getsel(&left, &right, dol)) 5687c478bd9Sstevel@tonic-gate return; 5697c478bd9Sstevel@tonic-gate c = getC(0); 5707c478bd9Sstevel@tonic-gate } 5717c478bd9Sstevel@tonic-gate subst: 5727c478bd9Sstevel@tonic-gate exclc = right - left + 1; 5737c478bd9Sstevel@tonic-gate while (--left >= 0) 5747c478bd9Sstevel@tonic-gate hp = hp->next; 5757c478bd9Sstevel@tonic-gate if (sc == HISTSUB || c == ':') { 5767c478bd9Sstevel@tonic-gate do { 5777c478bd9Sstevel@tonic-gate hp = getsub(hp); 5787c478bd9Sstevel@tonic-gate c = getC(0); 5797c478bd9Sstevel@tonic-gate } while (c == ':'); 5807c478bd9Sstevel@tonic-gate } 5817c478bd9Sstevel@tonic-gate unreadc(c); 5827c478bd9Sstevel@tonic-gate if (sc == '{') { 5837c478bd9Sstevel@tonic-gate c = getC(0); 5847c478bd9Sstevel@tonic-gate if (c != '}') 5857c478bd9Sstevel@tonic-gate seterr("Bad ! form"); 5867c478bd9Sstevel@tonic-gate } 5877c478bd9Sstevel@tonic-gate exclnxt = hp; 5887c478bd9Sstevel@tonic-gate } 5897c478bd9Sstevel@tonic-gate 5907c478bd9Sstevel@tonic-gate struct wordent * 5916c02b4a4Smuffin getsub(struct wordent *en) 5927c478bd9Sstevel@tonic-gate { 5936c02b4a4Smuffin tchar *cp; 5947c478bd9Sstevel@tonic-gate int delim; 5956c02b4a4Smuffin int c; 5967c478bd9Sstevel@tonic-gate int sc; 5977c478bd9Sstevel@tonic-gate bool global = 0; 5987c478bd9Sstevel@tonic-gate tchar orhsb[(sizeof rhsb)/(sizeof rhsb[0])]; 5997c478bd9Sstevel@tonic-gate 6007c478bd9Sstevel@tonic-gate #ifdef TRACE 6017c478bd9Sstevel@tonic-gate tprintf("TRACE- getsub()\n"); 6027c478bd9Sstevel@tonic-gate #endif 6037c478bd9Sstevel@tonic-gate exclnxt = 0; 6047c478bd9Sstevel@tonic-gate sc = c = getC(0); 6057c478bd9Sstevel@tonic-gate if (c == 'g') 6067c478bd9Sstevel@tonic-gate global++, c = getC(0); 6077c478bd9Sstevel@tonic-gate switch (c) { 6087c478bd9Sstevel@tonic-gate 6097c478bd9Sstevel@tonic-gate case 'p': 6107c478bd9Sstevel@tonic-gate justpr++; 6117c478bd9Sstevel@tonic-gate goto ret; 6127c478bd9Sstevel@tonic-gate 6137c478bd9Sstevel@tonic-gate case 'x': 6147c478bd9Sstevel@tonic-gate case 'q': 6157c478bd9Sstevel@tonic-gate global++; 6167c478bd9Sstevel@tonic-gate /* fall into ... */ 6177c478bd9Sstevel@tonic-gate 6187c478bd9Sstevel@tonic-gate case 'h': 6197c478bd9Sstevel@tonic-gate case 'r': 6207c478bd9Sstevel@tonic-gate case 't': 6217c478bd9Sstevel@tonic-gate case 'e': 6227c478bd9Sstevel@tonic-gate break; 6237c478bd9Sstevel@tonic-gate 6247c478bd9Sstevel@tonic-gate case '&': 6257c478bd9Sstevel@tonic-gate if (slhs[0] == 0) { 6267c478bd9Sstevel@tonic-gate seterr("No prev sub"); 6277c478bd9Sstevel@tonic-gate goto ret; 6287c478bd9Sstevel@tonic-gate } 6297c478bd9Sstevel@tonic-gate (void) strcpy_(lhsb, slhs); 6307c478bd9Sstevel@tonic-gate break; 6317c478bd9Sstevel@tonic-gate 63265b0c20eSnakanon #if 0 6337c478bd9Sstevel@tonic-gate case '~': 6347c478bd9Sstevel@tonic-gate if (lhsb[0] == 0) 6357c478bd9Sstevel@tonic-gate goto badlhs; 6367c478bd9Sstevel@tonic-gate break; 63765b0c20eSnakanon #endif 6387c478bd9Sstevel@tonic-gate 6397c478bd9Sstevel@tonic-gate case 's': 6407c478bd9Sstevel@tonic-gate delim = getC(0); 6417c478bd9Sstevel@tonic-gate if (alnum(delim) || isspnl(delim)) { 6427c478bd9Sstevel@tonic-gate unreadc(delim); 6437c478bd9Sstevel@tonic-gate bads: 6447c478bd9Sstevel@tonic-gate lhsb[0] = 0; 6457c478bd9Sstevel@tonic-gate seterr("Bad substitute"); 6467c478bd9Sstevel@tonic-gate goto ret; 6477c478bd9Sstevel@tonic-gate } 6487c478bd9Sstevel@tonic-gate cp = lhsb; 6497c478bd9Sstevel@tonic-gate for (;;) { 6507c478bd9Sstevel@tonic-gate c = getC(0); 6517c478bd9Sstevel@tonic-gate if (c == '\n') { 6527c478bd9Sstevel@tonic-gate unreadc(c); 6537c478bd9Sstevel@tonic-gate break; 6547c478bd9Sstevel@tonic-gate } 6557c478bd9Sstevel@tonic-gate if (c == delim) 6567c478bd9Sstevel@tonic-gate break; 6577c478bd9Sstevel@tonic-gate if (cp > &lhsb[(sizeof lhsb)/(sizeof lhsb[0]) - 2]) 6587c478bd9Sstevel@tonic-gate goto bads; 6597c478bd9Sstevel@tonic-gate if (c == '\\') { 6607c478bd9Sstevel@tonic-gate c = getC(0); 6617c478bd9Sstevel@tonic-gate if (c != delim && c != '\\') 6627c478bd9Sstevel@tonic-gate *cp++ = '\\'; 6637c478bd9Sstevel@tonic-gate } 6647c478bd9Sstevel@tonic-gate *cp++ = c; 6657c478bd9Sstevel@tonic-gate } 6667c478bd9Sstevel@tonic-gate if (cp != lhsb) 6677c478bd9Sstevel@tonic-gate *cp++ = 0; 6687c478bd9Sstevel@tonic-gate else if (lhsb[0] == 0) { 6697c478bd9Sstevel@tonic-gate /* badlhs: */ 6707c478bd9Sstevel@tonic-gate seterr("No prev lhs"); 6717c478bd9Sstevel@tonic-gate goto ret; 6727c478bd9Sstevel@tonic-gate } 6737c478bd9Sstevel@tonic-gate cp = rhsb; 6747c478bd9Sstevel@tonic-gate (void) strcpy_(orhsb, cp); 6757c478bd9Sstevel@tonic-gate for (;;) { 6767c478bd9Sstevel@tonic-gate c = getC(0); 6777c478bd9Sstevel@tonic-gate if (c == '\n') { 6787c478bd9Sstevel@tonic-gate unreadc(c); 6797c478bd9Sstevel@tonic-gate break; 6807c478bd9Sstevel@tonic-gate } 6817c478bd9Sstevel@tonic-gate if (c == delim) 6827c478bd9Sstevel@tonic-gate break; 68365b0c20eSnakanon #if 0 6847c478bd9Sstevel@tonic-gate if (c == '~') { 6857c478bd9Sstevel@tonic-gate if (&cp[strlen_(orhsb)] 6867c478bd9Sstevel@tonic-gate > &rhsb[(sizeof rhsb)/(sizeof rhsb[0]) - 2]) 6877c478bd9Sstevel@tonic-gate goto toorhs; 6887c478bd9Sstevel@tonic-gate (void) strcpy_(cp, orhsb); 6897c478bd9Sstevel@tonic-gate cp = strend(cp); 6907c478bd9Sstevel@tonic-gate continue; 6917c478bd9Sstevel@tonic-gate } 69265b0c20eSnakanon #endif 6937c478bd9Sstevel@tonic-gate if (cp > &rhsb[(sizeof rhsb)/(sizeof rhsb[0]) - 2]) { 6947c478bd9Sstevel@tonic-gate /* toorhs: */ 6957c478bd9Sstevel@tonic-gate seterr("Rhs too long"); 6967c478bd9Sstevel@tonic-gate goto ret; 6977c478bd9Sstevel@tonic-gate } 6987c478bd9Sstevel@tonic-gate if (c == '\\') { 6997c478bd9Sstevel@tonic-gate c = getC(0); 7007c478bd9Sstevel@tonic-gate if (c != delim /* && c != '~' */) 7017c478bd9Sstevel@tonic-gate *cp++ = '\\'; 7027c478bd9Sstevel@tonic-gate } 7037c478bd9Sstevel@tonic-gate *cp++ = c; 7047c478bd9Sstevel@tonic-gate } 7057c478bd9Sstevel@tonic-gate *cp++ = 0; 7067c478bd9Sstevel@tonic-gate break; 7077c478bd9Sstevel@tonic-gate 7087c478bd9Sstevel@tonic-gate default: 7097c478bd9Sstevel@tonic-gate if (c == '\n') 7107c478bd9Sstevel@tonic-gate unreadc(c); 7117c478bd9Sstevel@tonic-gate seterrc(gettext("Bad ! modifier: "), c); 7127c478bd9Sstevel@tonic-gate goto ret; 7137c478bd9Sstevel@tonic-gate } 7147c478bd9Sstevel@tonic-gate (void) strcpy_(slhs, lhsb); 7157c478bd9Sstevel@tonic-gate if (exclc) 7167c478bd9Sstevel@tonic-gate en = dosub(sc, en, global); 7177c478bd9Sstevel@tonic-gate ret: 7187c478bd9Sstevel@tonic-gate return (en); 7197c478bd9Sstevel@tonic-gate } 7207c478bd9Sstevel@tonic-gate 7217c478bd9Sstevel@tonic-gate struct wordent * 7226c02b4a4Smuffin dosub(int sc, struct wordent *en, bool global) 7237c478bd9Sstevel@tonic-gate { 7247c478bd9Sstevel@tonic-gate struct wordent lex; 7257c478bd9Sstevel@tonic-gate bool didsub = 0; 7267c478bd9Sstevel@tonic-gate struct wordent *hp = &lex; 7276c02b4a4Smuffin struct wordent *wdp; 7286c02b4a4Smuffin int i = exclc; 7297c478bd9Sstevel@tonic-gate 7307c478bd9Sstevel@tonic-gate #ifdef TRACE 7317c478bd9Sstevel@tonic-gate tprintf("TRACE- dosub()\n"); 7327c478bd9Sstevel@tonic-gate #endif 7337c478bd9Sstevel@tonic-gate wdp = hp; 7347c478bd9Sstevel@tonic-gate while (--i >= 0) { 73565b0c20eSnakanon struct wordent *new = (struct wordent *)xcalloc(1, sizeof *wdp); 7367c478bd9Sstevel@tonic-gate 7377c478bd9Sstevel@tonic-gate new->prev = wdp; 7387c478bd9Sstevel@tonic-gate new->next = hp; 7397c478bd9Sstevel@tonic-gate wdp->next = new; 7407c478bd9Sstevel@tonic-gate wdp = new; 7417c478bd9Sstevel@tonic-gate en = en->next; 7427c478bd9Sstevel@tonic-gate wdp->word = global || didsub == 0 ? 7437c478bd9Sstevel@tonic-gate subword(en->word, sc, &didsub) : savestr(en->word); 7447c478bd9Sstevel@tonic-gate } 7457c478bd9Sstevel@tonic-gate if (didsub == 0) 7467c478bd9Sstevel@tonic-gate seterr("Modifier failed"); 7477c478bd9Sstevel@tonic-gate hp->prev = wdp; 7487c478bd9Sstevel@tonic-gate return (&enthist(-1000, &lex, 0)->Hlex); 7497c478bd9Sstevel@tonic-gate } 7507c478bd9Sstevel@tonic-gate 7517c478bd9Sstevel@tonic-gate tchar * 7526c02b4a4Smuffin subword(tchar *cp, int type, bool *adid) 7537c478bd9Sstevel@tonic-gate { 7547c478bd9Sstevel@tonic-gate tchar wbuf[BUFSIZ]; 7556c02b4a4Smuffin tchar *wp, *mp, *np; 7566c02b4a4Smuffin int i; 7577c478bd9Sstevel@tonic-gate 7587c478bd9Sstevel@tonic-gate #ifdef TRACE 7597c478bd9Sstevel@tonic-gate tprintf("TRACE- subword()\n"); 7607c478bd9Sstevel@tonic-gate #endif 7617c478bd9Sstevel@tonic-gate switch (type) { 7627c478bd9Sstevel@tonic-gate 7637c478bd9Sstevel@tonic-gate case 'r': 7647c478bd9Sstevel@tonic-gate case 'e': 7657c478bd9Sstevel@tonic-gate case 'h': 7667c478bd9Sstevel@tonic-gate case 't': 7677c478bd9Sstevel@tonic-gate case 'q': 7687c478bd9Sstevel@tonic-gate case 'x': 7697c478bd9Sstevel@tonic-gate wp = domod(cp, type); 7707c478bd9Sstevel@tonic-gate if (wp == 0) 7717c478bd9Sstevel@tonic-gate return (savestr(cp)); 7727c478bd9Sstevel@tonic-gate *adid = 1; 7737c478bd9Sstevel@tonic-gate return (wp); 7747c478bd9Sstevel@tonic-gate 7757c478bd9Sstevel@tonic-gate default: 7767c478bd9Sstevel@tonic-gate wp = wbuf; 7777c478bd9Sstevel@tonic-gate i = BUFSIZ - 4; 7787c478bd9Sstevel@tonic-gate for (mp = cp; *mp; mp++) 7797c478bd9Sstevel@tonic-gate if (matchs(mp, lhsb)) { 7807c478bd9Sstevel@tonic-gate for (np = cp; np < mp; ) 7817c478bd9Sstevel@tonic-gate *wp++ = *np++, --i; 7827c478bd9Sstevel@tonic-gate for (np = rhsb; *np; np++) switch (*np) { 7837c478bd9Sstevel@tonic-gate 7847c478bd9Sstevel@tonic-gate case '\\': 7857c478bd9Sstevel@tonic-gate if (np[1] == '&') 7867c478bd9Sstevel@tonic-gate np++; 7877c478bd9Sstevel@tonic-gate /* fall into ... */ 7887c478bd9Sstevel@tonic-gate 7897c478bd9Sstevel@tonic-gate default: 7907c478bd9Sstevel@tonic-gate if (--i < 0) 7917c478bd9Sstevel@tonic-gate goto ovflo; 7927c478bd9Sstevel@tonic-gate *wp++ = *np; 7937c478bd9Sstevel@tonic-gate continue; 7947c478bd9Sstevel@tonic-gate 7957c478bd9Sstevel@tonic-gate case '&': 7967c478bd9Sstevel@tonic-gate i -= strlen_(lhsb); 7977c478bd9Sstevel@tonic-gate if (i < 0) 7987c478bd9Sstevel@tonic-gate goto ovflo; 7997c478bd9Sstevel@tonic-gate *wp = 0; 8007c478bd9Sstevel@tonic-gate (void) strcat_(wp, lhsb); 8017c478bd9Sstevel@tonic-gate wp = strend(wp); 8027c478bd9Sstevel@tonic-gate continue; 8037c478bd9Sstevel@tonic-gate } 8047c478bd9Sstevel@tonic-gate mp += strlen_(lhsb); 8057c478bd9Sstevel@tonic-gate i -= strlen_(mp); 8067c478bd9Sstevel@tonic-gate if (i < 0) { 8077c478bd9Sstevel@tonic-gate ovflo: 8087c478bd9Sstevel@tonic-gate seterr("Subst buf ovflo"); 8097c478bd9Sstevel@tonic-gate return (S_ /* "" */); 8107c478bd9Sstevel@tonic-gate } 8117c478bd9Sstevel@tonic-gate *wp = 0; 8127c478bd9Sstevel@tonic-gate (void) strcat_(wp, mp); 8137c478bd9Sstevel@tonic-gate *adid = 1; 8147c478bd9Sstevel@tonic-gate return (savestr(wbuf)); 8157c478bd9Sstevel@tonic-gate } 8167c478bd9Sstevel@tonic-gate return (savestr(cp)); 8177c478bd9Sstevel@tonic-gate } 8187c478bd9Sstevel@tonic-gate } 8197c478bd9Sstevel@tonic-gate 8207c478bd9Sstevel@tonic-gate tchar * 8216c02b4a4Smuffin domod(tchar *cp, int type) 8227c478bd9Sstevel@tonic-gate { 8236c02b4a4Smuffin tchar *wp, *xp; 8246c02b4a4Smuffin int c; 8257c478bd9Sstevel@tonic-gate 8267c478bd9Sstevel@tonic-gate #ifdef TRACE 8277c478bd9Sstevel@tonic-gate tprintf("TRACE- domod()\n"); 8287c478bd9Sstevel@tonic-gate #endif 8297c478bd9Sstevel@tonic-gate switch (type) { 8307c478bd9Sstevel@tonic-gate 8317c478bd9Sstevel@tonic-gate case 'x': 8327c478bd9Sstevel@tonic-gate case 'q': 8337c478bd9Sstevel@tonic-gate wp = savestr(cp); 8347c478bd9Sstevel@tonic-gate for (xp = wp; c = *xp; xp++) 8357c478bd9Sstevel@tonic-gate if (!issp(c) || type == 'q') 8367c478bd9Sstevel@tonic-gate *xp |= QUOTE; 8377c478bd9Sstevel@tonic-gate return (wp); 8387c478bd9Sstevel@tonic-gate 8397c478bd9Sstevel@tonic-gate case 'h': 8407c478bd9Sstevel@tonic-gate case 't': 8417c478bd9Sstevel@tonic-gate if (!any('/', cp)) 8427c478bd9Sstevel@tonic-gate return (type == 't' ? savestr(cp) : 0); 8437c478bd9Sstevel@tonic-gate wp = strend(cp); 8447c478bd9Sstevel@tonic-gate while (*--wp != '/') 8457c478bd9Sstevel@tonic-gate continue; 8467c478bd9Sstevel@tonic-gate if (type == 'h') 8477c478bd9Sstevel@tonic-gate xp = savestr(cp), xp[wp - cp] = 0; 8487c478bd9Sstevel@tonic-gate else 8497c478bd9Sstevel@tonic-gate xp = savestr(wp + 1); 8507c478bd9Sstevel@tonic-gate return (xp); 8517c478bd9Sstevel@tonic-gate 8527c478bd9Sstevel@tonic-gate case 'e': 8537c478bd9Sstevel@tonic-gate case 'r': 8547c478bd9Sstevel@tonic-gate wp = strend(cp); 8557c478bd9Sstevel@tonic-gate for (wp--; wp >= cp && *wp != '/'; wp--) 8567c478bd9Sstevel@tonic-gate if (*wp == '.') { 8577c478bd9Sstevel@tonic-gate if (type == 'e') 8587c478bd9Sstevel@tonic-gate xp = savestr(wp + 1); 8597c478bd9Sstevel@tonic-gate else 8607c478bd9Sstevel@tonic-gate xp = savestr(cp), xp[wp - cp] = 0; 8617c478bd9Sstevel@tonic-gate return (xp); 8627c478bd9Sstevel@tonic-gate } 8637c478bd9Sstevel@tonic-gate return (savestr(type == 'e' ? S_ /* "" */ : cp)); 8647c478bd9Sstevel@tonic-gate } 8657c478bd9Sstevel@tonic-gate return (0); 8667c478bd9Sstevel@tonic-gate } 8677c478bd9Sstevel@tonic-gate 8686c02b4a4Smuffin int 8696c02b4a4Smuffin matchs(tchar *str, tchar *pat) 8707c478bd9Sstevel@tonic-gate { 8717c478bd9Sstevel@tonic-gate 8727c478bd9Sstevel@tonic-gate #ifdef TRACE 8737c478bd9Sstevel@tonic-gate tprintf("TRACE- matchs()\n"); 8747c478bd9Sstevel@tonic-gate #endif 8757c478bd9Sstevel@tonic-gate while (*str && *pat && *str == *pat) 8767c478bd9Sstevel@tonic-gate str++, pat++; 8777c478bd9Sstevel@tonic-gate return (*pat == 0); 8787c478bd9Sstevel@tonic-gate } 8797c478bd9Sstevel@tonic-gate 8806c02b4a4Smuffin int 8816c02b4a4Smuffin getsel(int *al, int *ar, int dol) 8827c478bd9Sstevel@tonic-gate { 8836c02b4a4Smuffin int c = getC(0); 8846c02b4a4Smuffin int i; 8857c478bd9Sstevel@tonic-gate bool first = *al < 0; 8867c478bd9Sstevel@tonic-gate 8877c478bd9Sstevel@tonic-gate #ifdef TRACE 8887c478bd9Sstevel@tonic-gate tprintf("TRACE- getsel()\n"); 8897c478bd9Sstevel@tonic-gate #endif 8907c478bd9Sstevel@tonic-gate switch (c) { 8917c478bd9Sstevel@tonic-gate 8927c478bd9Sstevel@tonic-gate case '%': 8937c478bd9Sstevel@tonic-gate if (quesarg == -1) 8947c478bd9Sstevel@tonic-gate goto bad; 8957c478bd9Sstevel@tonic-gate if (*al < 0) 8967c478bd9Sstevel@tonic-gate *al = quesarg; 8977c478bd9Sstevel@tonic-gate *ar = quesarg; 8987c478bd9Sstevel@tonic-gate break; 8997c478bd9Sstevel@tonic-gate 9007c478bd9Sstevel@tonic-gate case '-': 9017c478bd9Sstevel@tonic-gate if (*al < 0) { 9027c478bd9Sstevel@tonic-gate *al = 0; 9037c478bd9Sstevel@tonic-gate *ar = dol - 1; 9047c478bd9Sstevel@tonic-gate unreadc(c); 9057c478bd9Sstevel@tonic-gate } 9067c478bd9Sstevel@tonic-gate return (1); 9077c478bd9Sstevel@tonic-gate 9087c478bd9Sstevel@tonic-gate case '^': 9097c478bd9Sstevel@tonic-gate if (*al < 0) 9107c478bd9Sstevel@tonic-gate *al = 1; 9117c478bd9Sstevel@tonic-gate *ar = 1; 9127c478bd9Sstevel@tonic-gate break; 9137c478bd9Sstevel@tonic-gate 9147c478bd9Sstevel@tonic-gate case '$': 9157c478bd9Sstevel@tonic-gate if (*al < 0) 9167c478bd9Sstevel@tonic-gate *al = dol; 9177c478bd9Sstevel@tonic-gate *ar = dol; 9187c478bd9Sstevel@tonic-gate break; 9197c478bd9Sstevel@tonic-gate 9207c478bd9Sstevel@tonic-gate case '*': 9217c478bd9Sstevel@tonic-gate if (*al < 0) 9227c478bd9Sstevel@tonic-gate *al = 1; 9237c478bd9Sstevel@tonic-gate *ar = dol; 9247c478bd9Sstevel@tonic-gate if (*ar < *al) { 9257c478bd9Sstevel@tonic-gate *ar = 0; 9267c478bd9Sstevel@tonic-gate *al = 1; 9277c478bd9Sstevel@tonic-gate return (1); 9287c478bd9Sstevel@tonic-gate } 9297c478bd9Sstevel@tonic-gate break; 9307c478bd9Sstevel@tonic-gate 9317c478bd9Sstevel@tonic-gate default: 9327c478bd9Sstevel@tonic-gate if (digit(c)) { 9337c478bd9Sstevel@tonic-gate i = 0; 9347c478bd9Sstevel@tonic-gate while (digit(c)) { 9357c478bd9Sstevel@tonic-gate i = i * 10 + c - '0'; 9367c478bd9Sstevel@tonic-gate c = getC(0); 9377c478bd9Sstevel@tonic-gate } 9387c478bd9Sstevel@tonic-gate if (i < 0) 9397c478bd9Sstevel@tonic-gate i = dol + 1; 9407c478bd9Sstevel@tonic-gate if (*al < 0) 9417c478bd9Sstevel@tonic-gate *al = i; 9427c478bd9Sstevel@tonic-gate *ar = i; 9437c478bd9Sstevel@tonic-gate } else 9447c478bd9Sstevel@tonic-gate if (*al < 0) 9457c478bd9Sstevel@tonic-gate *al = 0, *ar = dol; 9467c478bd9Sstevel@tonic-gate else 9477c478bd9Sstevel@tonic-gate *ar = dol - 1; 9487c478bd9Sstevel@tonic-gate unreadc(c); 9497c478bd9Sstevel@tonic-gate break; 9507c478bd9Sstevel@tonic-gate } 9517c478bd9Sstevel@tonic-gate if (first) { 9527c478bd9Sstevel@tonic-gate c = getC(0); 9537c478bd9Sstevel@tonic-gate unreadc(c); 9547c478bd9Sstevel@tonic-gate /* if (any(c, "-$*")) */ /* char -> tchar */ 9557c478bd9Sstevel@tonic-gate if (c == '-' || c == '$' || c == '*') 9567c478bd9Sstevel@tonic-gate return (1); 9577c478bd9Sstevel@tonic-gate } 9587c478bd9Sstevel@tonic-gate if (*al > *ar || *ar > dol) { 9597c478bd9Sstevel@tonic-gate bad: 9607c478bd9Sstevel@tonic-gate seterr("Bad ! arg selector"); 9617c478bd9Sstevel@tonic-gate return (0); 9627c478bd9Sstevel@tonic-gate } 9637c478bd9Sstevel@tonic-gate return (1); 9647c478bd9Sstevel@tonic-gate 9657c478bd9Sstevel@tonic-gate } 9667c478bd9Sstevel@tonic-gate 9677c478bd9Sstevel@tonic-gate struct wordent * 9686c02b4a4Smuffin gethent(int sc) 9697c478bd9Sstevel@tonic-gate { 9706c02b4a4Smuffin struct Hist *hp; 9716c02b4a4Smuffin tchar *np; 9726c02b4a4Smuffin int c; 9737c478bd9Sstevel@tonic-gate int event; 9747c478bd9Sstevel@tonic-gate bool back = 0; 9757c478bd9Sstevel@tonic-gate 9767c478bd9Sstevel@tonic-gate #ifdef TRACE 9777c478bd9Sstevel@tonic-gate tprintf("TRACE- gethent()\n"); 9787c478bd9Sstevel@tonic-gate #endif 9797c478bd9Sstevel@tonic-gate c = sc == HISTSUB ? HIST : getC(0); 9807c478bd9Sstevel@tonic-gate if (c == HIST) { 9817c478bd9Sstevel@tonic-gate if (alhistp) 9827c478bd9Sstevel@tonic-gate return (alhistp); 9837c478bd9Sstevel@tonic-gate event = eventno; 9847c478bd9Sstevel@tonic-gate goto skip; 9857c478bd9Sstevel@tonic-gate } 9867c478bd9Sstevel@tonic-gate switch (c) { 9877c478bd9Sstevel@tonic-gate 9887c478bd9Sstevel@tonic-gate case ':': 9897c478bd9Sstevel@tonic-gate case '^': 9907c478bd9Sstevel@tonic-gate case '$': 9917c478bd9Sstevel@tonic-gate case '*': 9927c478bd9Sstevel@tonic-gate case '%': 9937c478bd9Sstevel@tonic-gate ungetC(c); 9947c478bd9Sstevel@tonic-gate if (lastev == eventno && alhistp) 9957c478bd9Sstevel@tonic-gate return (alhistp); 9967c478bd9Sstevel@tonic-gate event = lastev; 9977c478bd9Sstevel@tonic-gate break; 9987c478bd9Sstevel@tonic-gate 9997c478bd9Sstevel@tonic-gate case '-': 10007c478bd9Sstevel@tonic-gate back = 1; 10017c478bd9Sstevel@tonic-gate c = getC(0); 10027c478bd9Sstevel@tonic-gate goto number; 10037c478bd9Sstevel@tonic-gate 10047c478bd9Sstevel@tonic-gate case '#': /* !# is command being typed in (mrh) */ 10057c478bd9Sstevel@tonic-gate return (¶ml); 10067c478bd9Sstevel@tonic-gate 10077c478bd9Sstevel@tonic-gate default: 10087c478bd9Sstevel@tonic-gate /* if (any(c, "(=~")) { */ 10097c478bd9Sstevel@tonic-gate if (c == '(' || c == '=' || c == '~') { 10107c478bd9Sstevel@tonic-gate unreadc(c); 10117c478bd9Sstevel@tonic-gate ungetC(HIST); 10127c478bd9Sstevel@tonic-gate return (0); 10137c478bd9Sstevel@tonic-gate } 10147c478bd9Sstevel@tonic-gate if (digit(c)) 10157c478bd9Sstevel@tonic-gate goto number; 10167c478bd9Sstevel@tonic-gate np = lhsb; 10177c478bd9Sstevel@tonic-gate /* while (!any(c, ": \t\\\n}")) { */ 10187c478bd9Sstevel@tonic-gate while (! (c == ':' || c == '\\' || isspnl(c) || c == '}')) { 10197c478bd9Sstevel@tonic-gate if (np < &lhsb[(sizeof lhsb)/(sizeof lhsb[0]) - 2]) 10207c478bd9Sstevel@tonic-gate *np++ = c; 10217c478bd9Sstevel@tonic-gate c = getC(0); 10227c478bd9Sstevel@tonic-gate } 10237c478bd9Sstevel@tonic-gate unreadc(c); 10247c478bd9Sstevel@tonic-gate if (np == lhsb) { 10257c478bd9Sstevel@tonic-gate ungetC(HIST); 10267c478bd9Sstevel@tonic-gate return (0); 10277c478bd9Sstevel@tonic-gate } 10287c478bd9Sstevel@tonic-gate *np++ = 0; 10297c478bd9Sstevel@tonic-gate hp = findev(lhsb, 0); 10307c478bd9Sstevel@tonic-gate if (hp) 10317c478bd9Sstevel@tonic-gate lastev = hp->Hnum; 10327c478bd9Sstevel@tonic-gate return (&hp->Hlex); 10337c478bd9Sstevel@tonic-gate 10347c478bd9Sstevel@tonic-gate case '?': 10357c478bd9Sstevel@tonic-gate np = lhsb; 10367c478bd9Sstevel@tonic-gate for (;;) { 10377c478bd9Sstevel@tonic-gate c = getC(0); 10387c478bd9Sstevel@tonic-gate if (c == '\n') { 10397c478bd9Sstevel@tonic-gate unreadc(c); 10407c478bd9Sstevel@tonic-gate break; 10417c478bd9Sstevel@tonic-gate } 10427c478bd9Sstevel@tonic-gate if (c == '?') 10437c478bd9Sstevel@tonic-gate break; 10447c478bd9Sstevel@tonic-gate if (np < &lhsb[(sizeof lhsb)/(sizeof lhsb[0]) - 2]) 10457c478bd9Sstevel@tonic-gate *np++ = c; 10467c478bd9Sstevel@tonic-gate } 10477c478bd9Sstevel@tonic-gate if (np == lhsb) { 10487c478bd9Sstevel@tonic-gate if (lhsb[0] == 0) { 10497c478bd9Sstevel@tonic-gate seterr("No prev search"); 10507c478bd9Sstevel@tonic-gate return (0); 10517c478bd9Sstevel@tonic-gate } 10527c478bd9Sstevel@tonic-gate } else 10537c478bd9Sstevel@tonic-gate *np++ = 0; 10547c478bd9Sstevel@tonic-gate hp = findev(lhsb, 1); 10557c478bd9Sstevel@tonic-gate if (hp) 10567c478bd9Sstevel@tonic-gate lastev = hp->Hnum; 10577c478bd9Sstevel@tonic-gate return (&hp->Hlex); 10587c478bd9Sstevel@tonic-gate 10597c478bd9Sstevel@tonic-gate number: 10607c478bd9Sstevel@tonic-gate event = 0; 10617c478bd9Sstevel@tonic-gate while (digit(c)) { 10627c478bd9Sstevel@tonic-gate event = event * 10 + c - '0'; 10637c478bd9Sstevel@tonic-gate c = getC(0); 10647c478bd9Sstevel@tonic-gate } 10657c478bd9Sstevel@tonic-gate if (back) 10667c478bd9Sstevel@tonic-gate event = eventno + (alhistp == 0) - (event ? event : 0); 10677c478bd9Sstevel@tonic-gate unreadc(c); 10687c478bd9Sstevel@tonic-gate break; 10697c478bd9Sstevel@tonic-gate } 10707c478bd9Sstevel@tonic-gate skip: 10717c478bd9Sstevel@tonic-gate for (hp = Histlist.Hnext; hp; hp = hp->Hnext) 10727c478bd9Sstevel@tonic-gate if (hp->Hnum == event) { 10737c478bd9Sstevel@tonic-gate hp->Href = eventno; 10747c478bd9Sstevel@tonic-gate lastev = hp->Hnum; 10757c478bd9Sstevel@tonic-gate return (&hp->Hlex); 10767c478bd9Sstevel@tonic-gate } 10777c478bd9Sstevel@tonic-gate np = putn(event); 10787c478bd9Sstevel@tonic-gate noev(np); 10797c478bd9Sstevel@tonic-gate return (0); 10807c478bd9Sstevel@tonic-gate } 10817c478bd9Sstevel@tonic-gate 10827c478bd9Sstevel@tonic-gate struct Hist * 10836c02b4a4Smuffin findev(tchar *cp, bool anyarg) 10847c478bd9Sstevel@tonic-gate { 10856c02b4a4Smuffin struct Hist *hp; 10867c478bd9Sstevel@tonic-gate 10877c478bd9Sstevel@tonic-gate #ifdef TRACE 10887c478bd9Sstevel@tonic-gate tprintf("TRACE- findev()\n"); 10897c478bd9Sstevel@tonic-gate #endif 10907c478bd9Sstevel@tonic-gate for (hp = Histlist.Hnext; hp; hp = hp->Hnext) { 10917c478bd9Sstevel@tonic-gate tchar *dp; 10926c02b4a4Smuffin tchar *p, *q; 10936c02b4a4Smuffin struct wordent *lp = hp->Hlex.next; 10947c478bd9Sstevel@tonic-gate int argno = 0; 10957c478bd9Sstevel@tonic-gate 10967c478bd9Sstevel@tonic-gate if (lp->word[0] == '\n') 10977c478bd9Sstevel@tonic-gate continue; 10987c478bd9Sstevel@tonic-gate if (!anyarg) { 10997c478bd9Sstevel@tonic-gate p = cp; 11007c478bd9Sstevel@tonic-gate q = lp->word; 11017c478bd9Sstevel@tonic-gate do 11027c478bd9Sstevel@tonic-gate if (!*p) 11037c478bd9Sstevel@tonic-gate return (hp); 11047c478bd9Sstevel@tonic-gate while (*p++ == *q++); 11057c478bd9Sstevel@tonic-gate continue; 11067c478bd9Sstevel@tonic-gate } 11077c478bd9Sstevel@tonic-gate do { 11087c478bd9Sstevel@tonic-gate for (dp = lp->word; *dp; dp++) { 11097c478bd9Sstevel@tonic-gate p = cp; 11107c478bd9Sstevel@tonic-gate q = dp; 11117c478bd9Sstevel@tonic-gate do 11127c478bd9Sstevel@tonic-gate if (!*p) { 11137c478bd9Sstevel@tonic-gate quesarg = argno; 11147c478bd9Sstevel@tonic-gate return (hp); 11157c478bd9Sstevel@tonic-gate } 11167c478bd9Sstevel@tonic-gate while (*p++ == *q++); 11177c478bd9Sstevel@tonic-gate } 11187c478bd9Sstevel@tonic-gate lp = lp->next; 11197c478bd9Sstevel@tonic-gate argno++; 11207c478bd9Sstevel@tonic-gate } while (lp->word[0] != '\n'); 11217c478bd9Sstevel@tonic-gate } 11227c478bd9Sstevel@tonic-gate noev(cp); 11237c478bd9Sstevel@tonic-gate return (0); 11247c478bd9Sstevel@tonic-gate } 11257c478bd9Sstevel@tonic-gate 11266c02b4a4Smuffin void 11276c02b4a4Smuffin noev(tchar *cp) 11287c478bd9Sstevel@tonic-gate { 11297c478bd9Sstevel@tonic-gate 11307c478bd9Sstevel@tonic-gate #ifdef TRACE 11317c478bd9Sstevel@tonic-gate tprintf("TRACE- noev()\n"); 11327c478bd9Sstevel@tonic-gate #endif 11337c478bd9Sstevel@tonic-gate seterr2(cp, ": Event not found"); 11347c478bd9Sstevel@tonic-gate } 11357c478bd9Sstevel@tonic-gate 11366c02b4a4Smuffin void 11376c02b4a4Smuffin setexclp(tchar *cp) 11387c478bd9Sstevel@tonic-gate { 11397c478bd9Sstevel@tonic-gate 11407c478bd9Sstevel@tonic-gate #ifdef TRACE 11417c478bd9Sstevel@tonic-gate tprintf("TRACE- setexclp()\n"); 11427c478bd9Sstevel@tonic-gate #endif 11437c478bd9Sstevel@tonic-gate if (cp && cp[0] == '\n') 11447c478bd9Sstevel@tonic-gate return; 11457c478bd9Sstevel@tonic-gate exclp = cp; 11467c478bd9Sstevel@tonic-gate } 11477c478bd9Sstevel@tonic-gate 11486c02b4a4Smuffin void 11496c02b4a4Smuffin unreadc(tchar c) 11507c478bd9Sstevel@tonic-gate { 11517c478bd9Sstevel@tonic-gate 11527c478bd9Sstevel@tonic-gate peekread = c; 11537c478bd9Sstevel@tonic-gate } 11547c478bd9Sstevel@tonic-gate 11556c02b4a4Smuffin int 11566c02b4a4Smuffin readc(bool wanteof) 11577c478bd9Sstevel@tonic-gate { 11586c02b4a4Smuffin int c; 11596c02b4a4Smuffin static int sincereal; 11607c478bd9Sstevel@tonic-gate 11617c478bd9Sstevel@tonic-gate if (c = peekread) { 11627c478bd9Sstevel@tonic-gate peekread = 0; 11637c478bd9Sstevel@tonic-gate return (c); 11647c478bd9Sstevel@tonic-gate } 11657c478bd9Sstevel@tonic-gate top: 11667c478bd9Sstevel@tonic-gate if (alvecp) { 11677c478bd9Sstevel@tonic-gate if (c = *alvecp++) 11687c478bd9Sstevel@tonic-gate return (c); 11697c478bd9Sstevel@tonic-gate if (*alvec) { 11707c478bd9Sstevel@tonic-gate alvecp = *alvec++; 11717c478bd9Sstevel@tonic-gate return (' '); 11727c478bd9Sstevel@tonic-gate } 11737c478bd9Sstevel@tonic-gate } 11747c478bd9Sstevel@tonic-gate if (alvec) { 11757c478bd9Sstevel@tonic-gate if (alvecp = *alvec) { 11767c478bd9Sstevel@tonic-gate alvec++; 11777c478bd9Sstevel@tonic-gate goto top; 11787c478bd9Sstevel@tonic-gate } 11797c478bd9Sstevel@tonic-gate /* Infinite source! */ 11807c478bd9Sstevel@tonic-gate return ('\n'); 11817c478bd9Sstevel@tonic-gate } 11827c478bd9Sstevel@tonic-gate if (evalp) { 11837c478bd9Sstevel@tonic-gate if (c = *evalp++) 11847c478bd9Sstevel@tonic-gate return (c); 11857c478bd9Sstevel@tonic-gate if (*evalvec) { 11867c478bd9Sstevel@tonic-gate evalp = *evalvec++; 11877c478bd9Sstevel@tonic-gate return (' '); 11887c478bd9Sstevel@tonic-gate } 11897c478bd9Sstevel@tonic-gate evalp = 0; 11907c478bd9Sstevel@tonic-gate } 11917c478bd9Sstevel@tonic-gate if (evalvec) { 11927c478bd9Sstevel@tonic-gate if (evalvec == (tchar **)1) { 11937c478bd9Sstevel@tonic-gate doneinp = 1; 11947c478bd9Sstevel@tonic-gate reset(); 11957c478bd9Sstevel@tonic-gate } 11967c478bd9Sstevel@tonic-gate if (evalp = *evalvec) { 11977c478bd9Sstevel@tonic-gate evalvec++; 11987c478bd9Sstevel@tonic-gate goto top; 11997c478bd9Sstevel@tonic-gate } 12007c478bd9Sstevel@tonic-gate evalvec = (tchar **)1; 12017c478bd9Sstevel@tonic-gate return ('\n'); 12027c478bd9Sstevel@tonic-gate } 12037c478bd9Sstevel@tonic-gate do { 12047c478bd9Sstevel@tonic-gate if (arginp == (tchar *) 1 || onelflg == 1) { 12057c478bd9Sstevel@tonic-gate if (wanteof) 12067c478bd9Sstevel@tonic-gate return (-1); 12077c478bd9Sstevel@tonic-gate exitstat(); 12087c478bd9Sstevel@tonic-gate } 12097c478bd9Sstevel@tonic-gate if (arginp) { 12107c478bd9Sstevel@tonic-gate if ((c = *arginp++) == 0) { 12117c478bd9Sstevel@tonic-gate arginp = (tchar *) 1; 12127c478bd9Sstevel@tonic-gate return ('\n'); 12137c478bd9Sstevel@tonic-gate } 12147c478bd9Sstevel@tonic-gate return (c); 12157c478bd9Sstevel@tonic-gate } 12167c478bd9Sstevel@tonic-gate reread: 12177c478bd9Sstevel@tonic-gate c = bgetc(); 12187c478bd9Sstevel@tonic-gate if (c < 0) { 12197c478bd9Sstevel@tonic-gate struct sgttyb tty; 12207c478bd9Sstevel@tonic-gate 12217c478bd9Sstevel@tonic-gate if (wanteof) 12227c478bd9Sstevel@tonic-gate return (-1); 12237c478bd9Sstevel@tonic-gate /* was isatty but raw with ignoreeof yields problems */ 12247c478bd9Sstevel@tonic-gate if (ioctl(SHIN, TIOCGETP, (char *)&tty) == 0 && 12257c478bd9Sstevel@tonic-gate (tty.sg_flags & RAW) == 0) { 12267c478bd9Sstevel@tonic-gate /* was 'short' for FILEC */ 12277c478bd9Sstevel@tonic-gate int ctpgrp; 12287c478bd9Sstevel@tonic-gate 12297c478bd9Sstevel@tonic-gate if (++sincereal > 25) 12307c478bd9Sstevel@tonic-gate goto oops; 12317c478bd9Sstevel@tonic-gate if (tpgrp != -1 && 12327c478bd9Sstevel@tonic-gate ioctl(FSHTTY, TIOCGPGRP, (char *)&ctpgrp) == 0 && 12337c478bd9Sstevel@tonic-gate tpgrp != ctpgrp) { 12347c478bd9Sstevel@tonic-gate (void) ioctl(FSHTTY, TIOCSPGRP, 12357c478bd9Sstevel@tonic-gate (char *)&tpgrp); 12367c478bd9Sstevel@tonic-gate (void) killpg(ctpgrp, SIGHUP); 12377c478bd9Sstevel@tonic-gate printf("Reset tty pgrp from %d to %d\n", ctpgrp, tpgrp); 12387c478bd9Sstevel@tonic-gate goto reread; 12397c478bd9Sstevel@tonic-gate } 12407c478bd9Sstevel@tonic-gate if (adrof(S_ignoreeof /* "ignoreeof" */)) { 12417c478bd9Sstevel@tonic-gate if (loginsh) 12427c478bd9Sstevel@tonic-gate printf("\nUse \"logout\" to logout.\n"); 12437c478bd9Sstevel@tonic-gate else 12447c478bd9Sstevel@tonic-gate printf("\nUse \"exit\" to leave csh.\n"); 12457c478bd9Sstevel@tonic-gate reset(); 12467c478bd9Sstevel@tonic-gate } 12477c478bd9Sstevel@tonic-gate if (chkstop == 0) { 12487c478bd9Sstevel@tonic-gate panystop(1); 12497c478bd9Sstevel@tonic-gate } 12507c478bd9Sstevel@tonic-gate } 12517c478bd9Sstevel@tonic-gate oops: 12527c478bd9Sstevel@tonic-gate doneinp = 1; 12537c478bd9Sstevel@tonic-gate reset(); 12547c478bd9Sstevel@tonic-gate } 12557c478bd9Sstevel@tonic-gate sincereal = 0; 12567c478bd9Sstevel@tonic-gate if (c == '\n' && onelflg) 12577c478bd9Sstevel@tonic-gate onelflg--; 12587c478bd9Sstevel@tonic-gate } while (c == 0); 12597c478bd9Sstevel@tonic-gate return (c); 12607c478bd9Sstevel@tonic-gate } 12617c478bd9Sstevel@tonic-gate 126265b0c20eSnakanon static void 126365b0c20eSnakanon expand_fbuf(void) 126465b0c20eSnakanon { 126565b0c20eSnakanon tchar **nfbuf = 126665b0c20eSnakanon (tchar **)xcalloc((unsigned)(fblocks + 2), sizeof (tchar **)); 126765b0c20eSnakanon 126865b0c20eSnakanon if (fbuf) { 126965b0c20eSnakanon (void) blkcpy(nfbuf, fbuf); 127065b0c20eSnakanon xfree((char *)fbuf); 127165b0c20eSnakanon } 127265b0c20eSnakanon fbuf = nfbuf; 127365b0c20eSnakanon fbuf[fblocks] = (tchar *)xcalloc(BUFSIZ + MB_LEN_MAX, 127465b0c20eSnakanon sizeof (tchar)); 127565b0c20eSnakanon fblocks++; 127665b0c20eSnakanon } 127765b0c20eSnakanon 12786c02b4a4Smuffin int 12796c02b4a4Smuffin bgetc(void) 12807c478bd9Sstevel@tonic-gate { 12816c02b4a4Smuffin int buf, off, c; 12827c478bd9Sstevel@tonic-gate #ifdef FILEC 128365b0c20eSnakanon tchar ttyline[BUFSIZ + MB_LEN_MAX]; /* read_() can return extra bytes */ 128465b0c20eSnakanon int roomleft; 12857c478bd9Sstevel@tonic-gate #endif 12867c478bd9Sstevel@tonic-gate 12877c478bd9Sstevel@tonic-gate #ifdef TELL 12887c478bd9Sstevel@tonic-gate if (cantell) { 12897c478bd9Sstevel@tonic-gate if (fseekp < fbobp || fseekp > feobp) { 12907c478bd9Sstevel@tonic-gate fbobp = feobp = fseekp; 12917c478bd9Sstevel@tonic-gate (void) lseek(SHIN, fseekp, 0); 12927c478bd9Sstevel@tonic-gate } 12937c478bd9Sstevel@tonic-gate if (fseekp == feobp) { 12947c478bd9Sstevel@tonic-gate fbobp = feobp; 12957c478bd9Sstevel@tonic-gate do 12967c478bd9Sstevel@tonic-gate c = read_(SHIN, fbuf[0], BUFSIZ); 12977c478bd9Sstevel@tonic-gate while (c < 0 && errno == EINTR); 12987c478bd9Sstevel@tonic-gate if (c <= 0) 12997c478bd9Sstevel@tonic-gate return (-1); 13007c478bd9Sstevel@tonic-gate feobp += c; 13017c478bd9Sstevel@tonic-gate } 13027c478bd9Sstevel@tonic-gate c = fbuf[0][fseekp - fbobp]; 13037c478bd9Sstevel@tonic-gate fseekp++; 13047c478bd9Sstevel@tonic-gate return (c); 13057c478bd9Sstevel@tonic-gate } 13067c478bd9Sstevel@tonic-gate #endif 13077c478bd9Sstevel@tonic-gate again: 13087c478bd9Sstevel@tonic-gate buf = (int)fseekp / BUFSIZ; 13097c478bd9Sstevel@tonic-gate if (buf >= fblocks) { 131065b0c20eSnakanon expand_fbuf(); 13117c478bd9Sstevel@tonic-gate goto again; 13127c478bd9Sstevel@tonic-gate } 13137c478bd9Sstevel@tonic-gate if (fseekp >= feobp) { 13147c478bd9Sstevel@tonic-gate buf = (int)feobp / BUFSIZ; 13157c478bd9Sstevel@tonic-gate off = (int)feobp % BUFSIZ; 13167c478bd9Sstevel@tonic-gate #ifndef FILEC 13177c478bd9Sstevel@tonic-gate for (;;) { 13187c478bd9Sstevel@tonic-gate c = read_(SHIN, fbuf[buf] + off, BUFSIZ - off); 13197c478bd9Sstevel@tonic-gate #else 13207c478bd9Sstevel@tonic-gate roomleft = BUFSIZ - off; 13217c478bd9Sstevel@tonic-gate for (;;) { 13227c478bd9Sstevel@tonic-gate if (filec && intty) { 132365b0c20eSnakanon c = tenex(ttyline, BUFSIZ); 13247c478bd9Sstevel@tonic-gate if (c > roomleft) { 132565b0c20eSnakanon expand_fbuf(); 132665b0c20eSnakanon copy(fbuf[buf] + off, ttyline, 132765b0c20eSnakanon roomleft * sizeof (tchar)); 132865b0c20eSnakanon copy(fbuf[buf + 1], ttyline + roomleft, 132965b0c20eSnakanon (c - roomleft) * sizeof (tchar)); 133065b0c20eSnakanon } else if (c > 0) { 133165b0c20eSnakanon copy(fbuf[buf] + off, ttyline, 133265b0c20eSnakanon c * sizeof (tchar)); 13337c478bd9Sstevel@tonic-gate } 133465b0c20eSnakanon } else { 13357c478bd9Sstevel@tonic-gate c = read_(SHIN, fbuf[buf] + off, roomleft); 133665b0c20eSnakanon if (c > roomleft) { 133765b0c20eSnakanon expand_fbuf(); 133865b0c20eSnakanon copy(fbuf[buf + 1], 133965b0c20eSnakanon fbuf[buf] + off + roomleft, 134065b0c20eSnakanon (c - roomleft) * sizeof (tchar)); 134165b0c20eSnakanon } 134265b0c20eSnakanon } 13437c478bd9Sstevel@tonic-gate #endif 13447c478bd9Sstevel@tonic-gate if (c >= 0) 13457c478bd9Sstevel@tonic-gate break; 13467c478bd9Sstevel@tonic-gate if (errno == EWOULDBLOCK) { 13477c478bd9Sstevel@tonic-gate int off = 0; 13487c478bd9Sstevel@tonic-gate 13497c478bd9Sstevel@tonic-gate (void) ioctl(SHIN, FIONBIO, (char *)&off); 13507c478bd9Sstevel@tonic-gate } else if (errno != EINTR) 13517c478bd9Sstevel@tonic-gate break; 13527c478bd9Sstevel@tonic-gate } 13537c478bd9Sstevel@tonic-gate if (c <= 0) 13547c478bd9Sstevel@tonic-gate return (-1); 13557c478bd9Sstevel@tonic-gate feobp += c; 13567c478bd9Sstevel@tonic-gate #ifndef FILEC 13577c478bd9Sstevel@tonic-gate goto again; 13587c478bd9Sstevel@tonic-gate #else 13597c478bd9Sstevel@tonic-gate if (filec && !intty) 13607c478bd9Sstevel@tonic-gate goto again; 13617c478bd9Sstevel@tonic-gate #endif 13627c478bd9Sstevel@tonic-gate } 13637c478bd9Sstevel@tonic-gate c = fbuf[buf][(int)fseekp % BUFSIZ]; 13647c478bd9Sstevel@tonic-gate fseekp++; 13657c478bd9Sstevel@tonic-gate return (c); 13667c478bd9Sstevel@tonic-gate } 13677c478bd9Sstevel@tonic-gate 13686c02b4a4Smuffin void 13696c02b4a4Smuffin bfree(void) 13707c478bd9Sstevel@tonic-gate { 13716c02b4a4Smuffin int sb, i; 13727c478bd9Sstevel@tonic-gate 13737c478bd9Sstevel@tonic-gate #ifdef TELL 13747c478bd9Sstevel@tonic-gate if (cantell) 13757c478bd9Sstevel@tonic-gate return; 13767c478bd9Sstevel@tonic-gate #endif 13777c478bd9Sstevel@tonic-gate if (whyles) 13787c478bd9Sstevel@tonic-gate return; 13797c478bd9Sstevel@tonic-gate sb = (int)(fseekp - 1) / BUFSIZ; 13807c478bd9Sstevel@tonic-gate if (sb > 0) { 13817c478bd9Sstevel@tonic-gate for (i = 0; i < sb; i++) 13827c478bd9Sstevel@tonic-gate xfree(fbuf[i]); 13837c478bd9Sstevel@tonic-gate (void) blkcpy(fbuf, &fbuf[sb]); 13847c478bd9Sstevel@tonic-gate fseekp -= BUFSIZ * sb; 13857c478bd9Sstevel@tonic-gate feobp -= BUFSIZ * sb; 13867c478bd9Sstevel@tonic-gate fblocks -= sb; 13877c478bd9Sstevel@tonic-gate } 13887c478bd9Sstevel@tonic-gate } 13897c478bd9Sstevel@tonic-gate 13906c02b4a4Smuffin void 13916c02b4a4Smuffin bseek(off_t l) 13927c478bd9Sstevel@tonic-gate { 13936c02b4a4Smuffin struct whyle *wp; 13947c478bd9Sstevel@tonic-gate 13957c478bd9Sstevel@tonic-gate fseekp = l; 13967c478bd9Sstevel@tonic-gate #ifdef TELL 13977c478bd9Sstevel@tonic-gate if (!cantell) { 13987c478bd9Sstevel@tonic-gate #endif 13997c478bd9Sstevel@tonic-gate if (!whyles) 14007c478bd9Sstevel@tonic-gate return; 14017c478bd9Sstevel@tonic-gate for (wp = whyles; wp->w_next; wp = wp->w_next) 14027c478bd9Sstevel@tonic-gate continue; 14037c478bd9Sstevel@tonic-gate if (wp->w_start > l) 14047c478bd9Sstevel@tonic-gate l = wp->w_start; 14057c478bd9Sstevel@tonic-gate #ifdef TELL 14067c478bd9Sstevel@tonic-gate } 14077c478bd9Sstevel@tonic-gate #endif 14087c478bd9Sstevel@tonic-gate } 14097c478bd9Sstevel@tonic-gate 14107c478bd9Sstevel@tonic-gate /* any similarity to bell telephone is purely accidental */ 14117c478bd9Sstevel@tonic-gate #ifndef btell 14127c478bd9Sstevel@tonic-gate off_t 14136c02b4a4Smuffin btell(void) 14147c478bd9Sstevel@tonic-gate { 14157c478bd9Sstevel@tonic-gate 14167c478bd9Sstevel@tonic-gate return (fseekp); 14177c478bd9Sstevel@tonic-gate } 14187c478bd9Sstevel@tonic-gate #endif 14197c478bd9Sstevel@tonic-gate 14206c02b4a4Smuffin void 14216c02b4a4Smuffin btoeof(void) 14227c478bd9Sstevel@tonic-gate { 14237c478bd9Sstevel@tonic-gate 14247c478bd9Sstevel@tonic-gate (void) lseek(SHIN, (off_t)0, 2); 14257c478bd9Sstevel@tonic-gate fseekp = feobp; 14267c478bd9Sstevel@tonic-gate wfree(); 14277c478bd9Sstevel@tonic-gate bfree(); 14287c478bd9Sstevel@tonic-gate } 14297c478bd9Sstevel@tonic-gate 14307c478bd9Sstevel@tonic-gate #ifdef TELL 14316c02b4a4Smuffin void 14326c02b4a4Smuffin settell(void) 14337c478bd9Sstevel@tonic-gate { 14347c478bd9Sstevel@tonic-gate 14357c478bd9Sstevel@tonic-gate cantell = 0; 14367c478bd9Sstevel@tonic-gate if (arginp || onelflg || intty) 14377c478bd9Sstevel@tonic-gate return; 14387c478bd9Sstevel@tonic-gate if (lseek(SHIN, (off_t)0, 1) < 0 || errno == ESPIPE) 14397c478bd9Sstevel@tonic-gate return; 144065b0c20eSnakanon fbuf = (tchar **)xcalloc(2, sizeof (tchar **)); 14417c478bd9Sstevel@tonic-gate fblocks = 1; 144265b0c20eSnakanon fbuf[0] = (tchar *)xcalloc(BUFSIZ + MB_LEN_MAX, sizeof (tchar)); 14437c478bd9Sstevel@tonic-gate fseekp = fbobp = feobp = lseek(SHIN, (off_t)0, 1); 14447c478bd9Sstevel@tonic-gate cantell = 1; 14457c478bd9Sstevel@tonic-gate } 14467c478bd9Sstevel@tonic-gate #endif 1447