📄 lex.c
字号:
if (*ar < *al) { *ar = 0; *al = 1; return (1); } break; default: if (Isdigit(c)) { i = 0; while (Isdigit(c)) { i = i * 10 + c - '0'; c = getC(0); } if (i < 0) i = dol + 1; if (*al < 0) *al = i; *ar = i; } else if (*al < 0) *al = 0, *ar = dol; else *ar = dol - 1; unreadc(c); break; } if (first) { c = getC(0); unreadc(c); if (any("-$*", c)) return (1); } if (*al > *ar || *ar > dol) { seterror(ERR_BADBANGARG); return (0); } return (1);}static struct wordent *gethent(sc) int sc;{ register struct Hist *hp; register Char *np; register int c; int event; bool back = 0; c = sc == HISTSUB ? HIST : getC(0); if (c == HIST) { if (alhistp) return (alhistp); event = eventno; } else switch (c) { case ':': case '^': case '$': case '*': case '%': ungetC(c); if (lastev == eventno && alhistp) return (alhistp); event = lastev; break; case '#': /* !# is command being typed in (mrh) */ if (--hleft == 0) { seterror(ERR_HISTLOOP); return (0); } else return (¶ml); /* NOTREACHED */ case '-': back = 1; c = getC(0); /* FALLSTHROUGH */ default: if (any("(=~", c)) { unreadc(c); ungetC(HIST); return (0); } np = lhsb; event = 0; while (!cmap(c, _ESC | _META | _QF | _QB) && !any("${}:", c)) { if (event != -1 && Isdigit(c)) event = event * 10 + c - '0'; else event = -1; if (np < &lhsb[sizeof(lhsb) / sizeof(Char) - 2]) *np++ = c; c = getC(0); } unreadc(c); if (np == lhsb) { ungetC(HIST); return (0); } *np++ = 0; if (event != -1) { /* * History had only digits */ if (back) event = eventno + (alhistp == 0) - (event ? event : 0); break; } hp = findev(lhsb, 0); if (hp) lastev = hp->Hnum; return (&hp->Hlex); case '?': np = lhsb; for (;;) { c = getC(0); if (c == '\n') { unreadc(c); break; } if (c == '?') break; if (np < &lhsb[sizeof(lhsb) / sizeof(Char) - 2]) *np++ = c; } if (np == lhsb) { if (lhsb[0] == 0) { seterror(ERR_NOSEARCH); return (0); } } else *np++ = 0; hp = findev(lhsb, 1); if (hp) lastev = hp->Hnum; return (&hp->Hlex); } for (hp = Histlist.Hnext; hp; hp = hp->Hnext) if (hp->Hnum == event) { hp->Href = eventno; lastev = hp->Hnum; return (&hp->Hlex); } np = putn(event); seterror(ERR_NOEVENT, vis_str(np)); return (0);}static struct Hist *findev(cp, anyarg) Char *cp; bool anyarg;{ register struct Hist *hp; for (hp = Histlist.Hnext; hp; hp = hp->Hnext) { Char *dp; register Char *p, *q; register struct wordent *lp = hp->Hlex.next; int argno = 0; /* * The entries added by alias substitution don't have a newline but do * have a negative event number. Savehist() trims off these entries, * but it happens before alias expansion, too early to delete those * from the previous command. */ if (hp->Hnum < 0) continue; if (lp->word[0] == '\n') continue; if (!anyarg) { p = cp; q = lp->word; do if (!*p) return (hp); while (*p++ == *q++); continue; } do { for (dp = lp->word; *dp; dp++) { p = cp; q = dp; do if (!*p) { quesarg = argno; return (hp); } while (*p++ == *q++); } lp = lp->next; argno++; } while (lp->word[0] != '\n'); } seterror(ERR_NOEVENT, vis_str(cp)); return (0);}static voidsetexclp(cp) register Char *cp;{ if (cp && cp[0] == '\n') return; exclp = cp;}voidunreadc(c) int c;{ peekread = c;}intreadc(wanteof) bool wanteof;{ register int c; static sincereal; aret = F_SEEK; if ((c = peekread) != '\0') { peekread = 0; return (c); }top: aret = F_SEEK; if (alvecp) { aret = A_SEEK; if ((c = *alvecp++) != '\0') return (c); if (alvec && *alvec) { alvecp = *alvec++; return (' '); } else { aret = F_SEEK; alvecp = NULL; return('\n'); } } if (alvec) { if ((alvecp = *alvec) != '\0') { alvec++; goto top; } /* Infinite source! */ return ('\n'); } if (evalp) { aret = E_SEEK; if ((c = *evalp++) != '\0') return (c); if (evalvec && *evalvec) { evalp = *evalvec++; return (' '); } aret = F_SEEK; evalp = 0; } if (evalvec) { if (evalvec == (Char **) 1) { doneinp = 1; reset(); } if ((evalp = *evalvec) != '\0') { evalvec++; goto top; } evalvec = (Char **) 1; return ('\n'); } do { if (arginp == (Char *) 1 || onelflg == 1) { if (wanteof) return (-1); exitstat(); } if (arginp) { if ((c = *arginp++) == 0) { arginp = (Char *) 1; return ('\n'); } return (c); }reread: c = bgetc(); if (c < 0) { struct termios tty; if (wanteof) return (-1); /* was isatty but raw with ignoreeof yields problems */ if (tcgetattr(SHIN, &tty) == 0 && (tty.c_lflag & ICANON)) { /* was 'short' for FILEC */ int ctpgrp; if (++sincereal > 25) goto oops; if (tpgrp != -1 && (ctpgrp = tcgetpgrp(FSHTTY)) != -1 && tpgrp != ctpgrp) { (void) tcsetpgrp(FSHTTY, tpgrp); (void) killpg((pid_t) ctpgrp, SIGHUP); (void) fprintf(csherr, "Reset tty pgrp from %d to %d\n", ctpgrp, tpgrp); goto reread; } if (adrof(STRignoreeof)) { if (loginsh) (void) fprintf(csherr,"\nUse \"logout\" to logout.\n"); else (void) fprintf(csherr,"\nUse \"exit\" to leave csh.\n"); reset(); } if (chkstop == 0) panystop(1); } oops: doneinp = 1; reset(); } sincereal = 0; if (c == '\n' && onelflg) onelflg--; } while (c == 0); return (c);}static intbgetc(){ register int buf, off, c;#ifdef FILEC register int numleft = 0, roomleft; Char ttyline[BUFSIZ];#endif char tbuf[BUFSIZ + 1]; if (cantell) { if (fseekp < fbobp || fseekp > feobp) { fbobp = feobp = fseekp; (void) lseek(SHIN, fseekp, L_SET); } if (fseekp == feobp) { int i; fbobp = feobp; do c = read(SHIN, tbuf, BUFSIZ); while (c < 0 && errno == EINTR); if (c <= 0) return (-1); for (i = 0; i < c; i++) fbuf[0][i] = (unsigned char) tbuf[i]; feobp += c; } c = fbuf[0][fseekp - fbobp]; fseekp++; return (c); }again: buf = (int) fseekp / BUFSIZ; if (buf >= fblocks) { register Char **nfbuf = (Char **) xcalloc((size_t) (fblocks + 2), sizeof(Char **)); if (fbuf) { (void) blkcpy(nfbuf, fbuf); xfree((ptr_t) fbuf); } fbuf = nfbuf; fbuf[fblocks] = (Char *) xcalloc(BUFSIZ, sizeof(Char)); fblocks++; if (!intty) goto again; } if (fseekp >= feobp) { buf = (int) feobp / BUFSIZ; off = (int) feobp % BUFSIZ; roomleft = BUFSIZ - off;#ifdef FILEC roomleft = BUFSIZ - off; for (;;) { if (filec && intty) { c = numleft ? numleft : tenex(ttyline, BUFSIZ); if (c > roomleft) { /* start with fresh buffer */ feobp = fseekp = fblocks * BUFSIZ; numleft = c; goto again; } if (c > 0) bcopy(ttyline, fbuf[buf] + off, c * sizeof(Char)); numleft = 0; } else {#endif c = read(SHIN, tbuf, roomleft); if (c > 0) { int i; Char *ptr = fbuf[buf] + off; for (i = 0; i < c; i++) ptr[i] = (unsigned char) tbuf[i]; }#ifdef FILEC }#endif if (c >= 0) break; if (errno == EWOULDBLOCK) { int off = 0; (void) ioctl(SHIN, FIONBIO, (ioctl_t) & off); } else if (errno != EINTR) break; } if (c <= 0) return (-1); feobp += c;#ifndef FILEC goto again;#else if (filec && !intty) goto again;#endif } c = fbuf[buf][(int) fseekp % BUFSIZ]; fseekp++; return (c);}static voidbfree(){ register int sb, i; if (cantell) return; if (whyles) return; sb = (int) (fseekp - 1) / BUFSIZ; if (sb > 0) { for (i = 0; i < sb; i++) xfree((ptr_t) fbuf[i]); (void) blkcpy(fbuf, &fbuf[sb]); fseekp -= BUFSIZ * sb; feobp -= BUFSIZ * sb; fblocks -= sb; }}voidbseek(l) struct Ain *l;{ switch (aret = l->type) { case E_SEEK: evalvec = l->a_seek; evalp = l->c_seek; return; case A_SEEK: alvec = l->a_seek; alvecp = l->c_seek; return; case F_SEEK: fseekp = l->f_seek; return; default: (void) fprintf(csherr, "Bad seek type %d\n", aret); abort(); }}voidbtell(l) struct Ain *l;{ switch (l->type = aret) { case E_SEEK: l->a_seek = evalvec; l->c_seek = evalp; return; case A_SEEK: l->a_seek = alvec; l->c_seek = alvecp; return; case F_SEEK: l->f_seek = fseekp; l->a_seek = NULL; return; default: (void) fprintf(csherr, "Bad seek type %d\n", aret); abort(); }}voidbtoeof(){ (void) lseek(SHIN, (off_t) 0, L_XTND); aret = F_SEEK; fseekp = feobp; alvec = NULL; alvecp = NULL; evalvec = NULL; evalp = NULL; wfree(); bfree();}voidsettell(){ cantell = 0; if (arginp || onelflg || intty) return; if (lseek(SHIN, (off_t) 0, L_INCR) < 0 || errno == ESPIPE) return; fbuf = (Char **) xcalloc(2, sizeof(Char **)); fblocks = 1; fbuf[0] = (Char *) xcalloc(BUFSIZ, sizeof(Char)); fseekp = fbobp = feobp = lseek(SHIN, (off_t) 0, L_INCR); cantell = 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -