static int compsub(int ch) { register int seof, c; int gsubf; gsubf = 0; xflag = 0; switch (ch) { case 's': ignore(skipwh()); seof = ex_getchar(); if (endcmd(seof)) error("Substitute needs re|Missing regular expression for substitute"); seof = compile(seof, 1); savere(subre); comprhs(seof); break; case '&': if (subre.Expbuf[0] == 0) error("No previous substitute re|No previous substitute to repeat"); resre(subre); break; case '~': if (re.Expbuf[0] == 0) error("No previous re|No previous regular expression"); savere(subre); break; } for (;;) { c = ex_getchar(); switch (c) { case 'g': gsubf++; continue; case 'c': xflag++; continue; default: ungetchar(c); setcount(); ex_newline(); return (gsubf); } } }
set() { register char *cp; register struct option *op; register int c; bool no; extern short ospeed; setnoaddr(); if (skipend()) { if (peekchar() != EOF) ignchar(); propts(); return; } do { cp = optname; do { if (cp < &optname[ONMSZ - 2]) *cp++ = getchar(); } while (isalnum(peekchar())); *cp = 0; cp = optname; if (eq("all", cp)) { if (inopen) pofix(); prall(); goto next; } no = 0; if (cp[0] == 'n' && cp[1] == 'o') { cp += 2; no++; } /* Implement w300, w1200, and w9600 specially */ if (eq(cp, "w300")) { if (ospeed >= B1200) { dontset: ignore(getchar()); /* = */ ignore(getnum()); /* value */ continue; } cp = "window"; } else if (eq(cp, "w1200")) { if (ospeed < B1200 || ospeed >= B2400) goto dontset; cp = "window"; } else if (eq(cp, "w9600")) { if (ospeed < B2400) goto dontset; cp = "window"; } for (op = options; op < &options[NOPTS]; op++) if (eq(op->oname, cp) || op->oabbrev && eq(op->oabbrev, cp)) break; if (op->oname == 0) serror("%s: No such option@- 'set all' gives all option values", cp); c = skipwh(); if (peekchar() == '?') { ignchar(); printone: propt(op); noonl(); goto next; } if (op->otype == ONOFF) { op->ovalue = 1 - no; if (op == &options[PROMPT]) oprompt = 1 - no; goto next; } if (no) serror("Option %s is not a toggle", op->oname); if (c != 0 || setend()) goto printone; if (getchar() != '=') serror("Missing =@in assignment to option %s", op->oname); switch (op->otype) { case NUMERIC: if (!isdigit(peekchar())) error("Digits required@after ="); op->ovalue = getnum(); if (value(TABSTOP) <= 0) value(TABSTOP) = TABS; if (op == &options[WINDOW]) { if (value(WINDOW) >= LINES) value(WINDOW) = LINES-1; vsetsiz(value(WINDOW)); } break; case STRING: case OTERM: cp = optname; while (!setend()) { if (cp >= &optname[ONMSZ]) error("String too long@in option assignment"); /* adb change: allow whitepace in strings */ if( (*cp = getchar()) == '\\') if( peekchar() != EOF) *cp = getchar(); cp++; } *cp = 0; if (op->otype == OTERM) { /* * At first glance it seems like we shouldn't care if the terminal type * is changed inside visual mode, as long as we assume the screen is * a mess and redraw it. However, it's a much harder problem than that. * If you happen to change from 1 crt to another that both have the same * size screen, it's OK. But if the screen size if different, the stuff * that gets initialized in vop() will be wrong. This could be overcome * by redoing the initialization, e.g. making the first 90% of vop into * a subroutine. However, the most useful case is where you forgot to do * a setenv before you went into the editor and it thinks you're on a dumb * terminal. Ex treats this like hardcopy and goes into HARDOPEN mode. * This loses because the first part of vop calls oop in this case. * The problem is so hard I gave up. I'm not saying it can't be done, * but I am saying it probably isn't worth the effort. */ if (inopen) error("Can't change type of terminal from within open/visual"); setterm(optname); } else { CP(op->osvalue, optname); op->odefault = 1; } break; } next: flush(); } while (!skipend()); eol(); }
/* * Write a file. */ void wop(bool dofname) /* bool dofname; / * if 1 call filename, else use savedfile */ { register int c, exclam, nonexist; line *saddr1, *saddr2; struct stat stbuf; c = 0; exclam = 0; if (dofname) { if (peekchar() == '!') exclam++, ignchar(); ignore(skipwh()); while (peekchar() == '>') ignchar(), c++, ignore(skipwh()); if (c != 0 && c != 2) error("Write forms are 'w' and 'w>>'"); filename('w'); } else { if (savedfile[0] == 0) error("No file|No current filename"); saddr1=addr1; saddr2=addr2; addr1=one; addr2=dol; CP(file, savedfile); if (inopen) { vclrech(0); splitw++; } lprintf("\"%s\"", file); } nonexist = stat(file, &stbuf); switch (c) { case 0: if (!exclam && (!value(WRITEANY) || value(READONLY))) switch (edfile()) { case NOTEDF: if (nonexist) break; if ((stbuf.st_mode & S_IFMT) == S_IFCHR) { if (samei(&stbuf, "/dev/null")) break; if (samei(&stbuf, "/dev/tty")) break; } io = open(file, O_WRONLY); if (io < 0) syserror(); if (!isatty(io)) serror(" File exists| File exists - use \"w! %s\" to overwrite", file); close(io); break; case EDF: if (value(READONLY)) error(" File is read only"); break; case PARTBUF: if (value(READONLY)) error(" File is read only"); error(" Use \"w!\" to write partial buffer"); } cre: /* synctmp(); */ #ifdef V6 io = creat(file, 0644); #else io = creat(file, 0666); #endif if (io < 0) syserror(); writing = 1; if (hush == 0) { if (nonexist) ex_printf(" [New file]"); else if (value(WRITEANY) && edfile() != EDF) ex_printf(" [Existing file]"); } break; case 2: io = open(file, O_WRONLY); if (io < 0) { if (exclam || value(WRITEANY)) goto cre; syserror(); } lseek(io, 0l, SEEK_END); break; } putfile(); ignore(iostats()); if (c != 2 && addr1 == one && addr2 == dol) { if (eq(file, savedfile)) edited = 1; ex_sync(); } if (!dofname) { addr1 = saddr1; addr2 = saddr2; } writing = 0; }
/* * Main loop for command mode command decoding. * A few commands are executed here, but main function * is to strip command addresses, do a little address oriented * processing and call command routines to do the real work. */ void commands(bool noprompt, bool exitoneof) { register line *addr; register int c; register int lchng; int given; int seensemi; int cnt; bool hadpr; resetflav(); nochng(); for (;;) { /* * If dot at last command * ended up at zero, advance to one if there is a such. */ if (dot <= zero) { dot = zero; if (dol > zero) dot = one; } shudclob = 0; /* * If autoprint or trailing print flags, * print the line at the specified offset * before the next command. */ if (pflag || (lchng != chng && value(AUTOPRINT) && !inglobal && !inopen && endline)) { pflag = 0; nochng(); if (dol != zero) { addr1 = addr2 = dot + poffset; if (addr1 < one || addr1 > dol) error("Offset out-of-bounds|Offset after command too large"); setdot1(); goto print; } } nochng(); /* * Print prompt if appropriate. * If not in global flush output first to prevent * going into pfast mode unreasonably. */ if (inglobal == 0) { flush(); if (!hush && value(PROMPT) && !globp && !noprompt && endline) { ex_putchar(':'); hadpr = 1; } TSYNC(); } /* * Gobble up the address. * Degenerate addresses yield ".". */ addr2 = 0; given = seensemi = 0; do { addr1 = addr2; addr = address(0); c = getcd(); if (addr == 0) { if (c == ',') addr = dot; else if (addr1 != 0) { addr2 = dot; break; } else break; } addr2 = addr; given++; if (c == ';') { c = ','; dot = addr; seensemi = 1; } } while (c == ','); if (c == '%') { /* %: same as 1,$ */ addr1 = one; addr2 = dol; given = 2; c = ex_getchar(); } if (addr1 == 0) addr1 = addr2; if (c == ':') c = ex_getchar(); /* * Set command name for special character commands. */ tailspec(c); /* * If called via : escape from open or visual, limit * the set of available commands here to save work below. */ if (inopen) { if (c=='\n' || c=='\r' || c==CTRL('d') || c==EOF) { if (addr2) dot = addr2; if (c == EOF) return; continue; } if (any(c, "o")) notinvis: tailprim(Command, 1, 1); } switch (c) { case 'a': switch(peekchar()) { case 'b': /* abbreviate */ tail("abbreviate"); setnoaddr(); mapcmd(0, 1); anyabbrs = 1; continue; case 'r': /* args */ tail("args"); setnoaddr(); eol(); pargs(); continue; } /* append */ if (inopen) goto notinvis; tail("append"); setdot(); aiflag = exclam(); ex_newline(); vmacchng(0); deletenone(); setin(addr2); inappend = 1; ignore(append(gettty, addr2)); inappend = 0; nochng(); continue; case 'c': switch (peekchar()) { /* copy */ case 'o': tail("copy"); vmacchng(0); move(); continue; #ifdef CHDIR /* cd */ case 'd': tail("cd"); goto changdir; /* chdir */ case 'h': ignchar(); if (peekchar() == 'd') { register char *p; tail2of("chdir"); changdir: if (savedfile[0] == '/' || !value(WARN)) ignore(exclam()); else ignore(quickly()); if (skipend()) { p = getenv("HOME"); if (p == NULL) error("Home directory unknown"); } else getone(), p = file; eol(); if (chdir(p) < 0) filioerr(p); if (savedfile[0] != '/') edited = 0; continue; } if (inopen) tailprim("change", 2, 1); tail2of("change"); break; #endif default: if (inopen) goto notinvis; tail("change"); break; } /* change */ aiflag = exclam(); setCNL(); vmacchng(0); setin(addr1); delete(0); inappend = 1; ignore(append(gettty, addr1 - 1)); inappend = 0; nochng(); continue; /* delete */ case 'd': /* * Caution: dp and dl have special meaning already. */ tail("delete"); c = cmdreg(); setCNL(); vmacchng(0); if (c) YANKreg(c); delete(0); appendnone(); continue; /* edit */ /* ex */ case 'e': tail(peekchar() == 'x' ? "ex" : "edit"); editcmd: if (!exclam() && chng) c = 'E'; filename(c); if (c == 'E') { ungetchar(lastchar()); ignore(quickly()); } setnoaddr(); doecmd: init(); addr2 = zero; laste++; ex_sync(); rop(c); nochng(); continue; /* file */ case 'f': tail("file"); setnoaddr(); filename(c); noonl(); /* synctmp(); */ continue; /* global */ case 'g': tail("global"); global(!exclam()); nochng(); continue; /* insert */ case 'i': if (inopen) goto notinvis; tail("insert"); setdot(); nonzero(); aiflag = exclam(); ex_newline(); vmacchng(0); deletenone(); setin(addr2); inappend = 1; ignore(append(gettty, addr2 - 1)); inappend = 0; if (dot == zero && dol > zero) dot = one; nochng(); continue; /* join */ case 'j': tail("join"); c = exclam(); setcount(); nonzero(); ex_newline(); vmacchng(0); if (given < 2 && addr2 != dol) addr2++; join(c); continue; /* k */ case 'k': casek: pastwh(); c = ex_getchar(); if (endcmd(c)) serror("Mark what?|%s requires following letter", Command); ex_newline(); if (!islower(c)) error("Bad mark|Mark must specify a letter"); setdot(); nonzero(); names[c - 'a'] = *addr2 &~ 01; anymarks = 1; continue; /* list */ case 'l': tail("list"); setCNL(); ignorf(setlist(1)); pflag = 0; goto print; case 'm': if (peekchar() == 'a') { ignchar(); if (peekchar() == 'p') { /* map */ tail2of("map"); setnoaddr(); mapcmd(0, 0); continue; } /* mark */ tail2of("mark"); goto casek; } /* move */ tail("move"); vmacchng(0); move(); continue; case 'n': if (peekchar() == 'u') { tail("number"); goto numberit; } /* next */ tail("next"); setnoaddr(); ckaw(); ignore(quickly()); if (getargs()) makargs(); next(); c = 'e'; filename(c); goto doecmd; /* open */ case 'o': tail("open"); oop(); pflag = 0; nochng(); continue; case 'p': case 'P': switch (peekchar()) { /* put */ case 'u': tail("put"); setdot(); c = cmdreg(); eol(); vmacchng(0); if (c) putreg(c); else put(); continue; case 'r': ignchar(); if (peekchar() == 'e') { /* preserve */ tail2of("preserve"); eol(); if (preserve() == 0) error("Preserve failed!"); else error("File preserved."); } tail2of("print"); break; default: tail("print"); break; } /* print */ setCNL(); pflag = 0; print: nonzero(); if (CL && span() > EX_LINES) { flush1(); vclear(); } plines(addr1, addr2, 1); continue; /* quit */ case 'q': tail("quit"); setnoaddr(); c = quickly(); eol(); if (!c) quit: nomore(); if (inopen) { vgoto(WECHO, 0); if (!ateopr()) vnfl(); else { tostop(); } flush(); setty(normf); } cleanup(1); ex_exit(0); case 'r': if (peekchar() == 'e') { ignchar(); switch (peekchar()) { /* rewind */ case 'w': tail2of("rewind"); setnoaddr(); if (!exclam()) { ckaw(); if (chng && dol > zero) error("No write@since last chage (:rewind! overrides)"); } eol(); erewind(); next(); c = 'e'; ungetchar(lastchar()); filename(c); goto doecmd; /* recover */ case 'c': tail2of("recover"); setnoaddr(); c = 'e'; if (!exclam() && chng) c = 'E'; filename(c); if (c == 'E') { ungetchar(lastchar()); ignore(quickly()); } init(); addr2 = zero; laste++; ex_sync(); recover(); rop2(); revocer(); if (status == 0) rop3(c); if (dol != zero) change(); nochng(); continue; } tail2of("read"); } else tail("read"); /* read */ if (savedfile[0] == 0 && dol == zero) c = 'e'; pastwh(); vmacchng(0); if (peekchar() == '!') { setdot(); ignchar(); unix0(0); filter(0); continue; } filename(c); rop(c); nochng(); if (inopen && endline && addr1 > zero && addr1 < dol) dot = addr1 + 1; continue; case 's': switch (peekchar()) { /* * Caution: 2nd char cannot be c, g, or r * because these have meaning to substitute. */ /* set */ case 'e': tail("set"); setnoaddr(); set(); continue; /* shell */ case 'h': tail("shell"); setNAEOL(); vnfl(); putpad(TE); flush(); unixwt(1, unixex("-i", (char *) 0, 0, 0)); vcontin(0); continue; /* source */ case 'o': #ifdef notdef if (inopen) goto notinvis; #endif tail("source"); setnoaddr(); getone(); eol(); source(file, 0); continue; #ifdef SIGTSTP /* stop, suspend */ case 't': tail("stop"); goto suspend; case 'u': tail("suspend"); suspend: if (!dosusp) error("Old tty driver|Not using new tty driver/shell"); c = exclam(); eol(); if (!c) ckaw(); onsusp(0); continue; #endif } /* fall into ... */ /* & */ /* ~ */ /* substitute */ case '&': case '~': Command = "substitute"; if (c == 's') tail(Command); vmacchng(0); if (!substitute(c)) pflag = 0; continue; /* t */ case 't': if (peekchar() == 'a') { tail("tag"); tagfind(exclam()); if (!inopen) lchng = chng - 1; else nochng(); continue; } tail("t"); vmacchng(0); move(); continue; case 'u': if (peekchar() == 'n') { ignchar(); switch(peekchar()) { /* unmap */ case 'm': tail2of("unmap"); setnoaddr(); mapcmd(1, 0); continue; /* unabbreviate */ case 'a': tail2of("unabbreviate"); setnoaddr(); mapcmd(1, 1); anyabbrs = 1; continue; } /* undo */ tail2of("undo"); } else tail("undo"); setnoaddr(); markDOT(); c = exclam(); ex_newline(); undo(c); continue; case 'v': switch (peekchar()) { case 'e': /* version */ tail("version"); setNAEOL(); ex_printf("@(#) Version 3.6, 11/3/80" " (4.0BSD). git " "160803 14:24" +5); noonl(); continue; /* visual */ case 'i': tail("visual"); if (inopen) { c = 'e'; goto editcmd; } vop(); pflag = 0; nochng(); continue; } /* v */ tail("v"); global(0); nochng(); continue; /* write */ case 'w': c = peekchar(); tail(c == 'q' ? "wq" : "write"); wq: if (skipwh() && peekchar() == '!') { pofix(); ignchar(); setall(); unix0(0); filter(1); } else { setall(); wop(1); nochng(); } if (c == 'q') goto quit; continue; /* xit */ case 'x': tail("xit"); if (!chng) goto quit; c = 'q'; goto wq; /* yank */ case 'y': tail("yank"); c = cmdreg(); setcount(); eol(); vmacchng(0); if (c) YANKreg(c); else yank(); continue; /* z */ case 'z': zop(0); pflag = 0; continue; /* * */ /* @ */ case '*': case '@': c = ex_getchar(); if (c=='\n' || c=='\r') ungetchar(c); if (any(c, "@*\n\r")) c = lastmac; if (isupper(c)) c = tolower(c); if (!islower(c)) error("Bad register"); ex_newline(); setdot(); cmdmac(c); continue; /* | */ case '|': endline = 0; goto caseline; /* \n */ case '\n': endline = 1; caseline: notempty(); if (addr2 == 0) { if (UP != NOSTR && c == '\n' && !inglobal) c = CTRL('k'); if (inglobal) addr1 = addr2 = dot; else { if (dot == dol) error("At EOF|At end-of-file"); addr1 = addr2 = dot + 1; } } setdot(); nonzero(); if (seensemi) addr1 = addr2; ex_getline(*addr1); if (c == CTRL('k')) { flush1(); destline--; if (hadpr) shudclob = 1; } plines(addr1, addr2, 1); continue; /* " */ case '"': comment(); continue; /* # */ case '#': numberit: setCNL(); ignorf(setnumb(1)); pflag = 0; goto print; /* = */ case '=': ex_newline(); setall(); if (inglobal == 2) pofix(); ex_printf("%d", lineno(addr2)); noonl(); continue; /* ! */ case '!': if (addr2 != 0) { vmacchng(0); unix0(0); setdot(); filter(2); } else { unix0(1); pofix(); putpad(TE); flush(); unixwt(1, unixex("-c", uxb, 0, 0)); vclrech(1); /* vcontin(0); */ nochng(); } continue; /* < */ /* > */ case '<': case '>': for (cnt = 1; peekchar() == c; cnt++) ignchar(); setCNL(); vmacchng(0); shift(c, cnt); continue; /* ^D */ /* EOF */ case CTRL('d'): case EOF: if (exitoneof) { if (addr2 != 0) dot = addr2; return; } if (!isatty(0)) { if (intty) /* * Chtty sys call at UCB may cause a * input which was a tty to suddenly be * turned into /dev/null. */ onhup(0); return; } if (addr2 != 0) { setlastchar('\n'); putnl(); } if (dol == zero) { if (addr2 == 0) putnl(); notempty(); } ungetchar(EOF); zop(hadpr); continue; default: if (!isalpha(c)) break; ungetchar(c); tailprim("", 0, 0); } ierror("What?|Unknown command character '%c'", c); } }
/* * Write a file. */ void wop(bool dofname) /* bool dofname; / * if 1 call filename, else use savedfile */ { register int c, exclam, nonexist; line *saddr1, *saddr2; struct stat stbuf; #ifdef FLOCKFILE int *lp, *iop ; #endif c = 0; exclam = 0; saddr1=addr1; saddr2=addr2; if (dofname) { if (peekchar() == '!') exclam++, ignchar(); ignore(skipwh()); while (peekchar() == '>') ignchar(), c++, ignore(skipwh()); if (c != 0 && c != 2) error("Write forms are 'w' and 'w>>'"); filename('w'); } else { if (savedfile[0] == 0) error("No file|No current filename"); addr1=one; addr2=dol; CP(file, savedfile); if (inopen) { vclrech(0); splitw++; } lprintf("\"%s\"", file); } nonexist = stat(file, &stbuf); #ifdef FLOCKFILE /* * if this is either the saved or alternate file or current file, * point to the appropriate descriptor and file lock status. */ if (strcmp (file,savedfile) == 0) { lp = &lock_savedfile ; iop = &io_savedfile ; } else if (strcmp (file,altfile) == 0) { lp = &lock_altfile ; iop = &io_altfile ; } else { lp = &lock_curr ; iop = &io_curr ; } if (!*iop && !nonexist){ *lp = 0 ; if ((*iop = open(file, O_WRONLY)) < 0) *iop = 0 ; } #endif switch (c) { case 0: if (!exclam && (!value(WRITEANY) || value(READONLY))) switch (edfile()) { case NOTEDF: if (nonexist) break; if ((stbuf.st_mode & S_IFMT) == S_IFCHR) { if (samei(&stbuf, _PATH_DEVNULL)) break; if (samei(&stbuf, _PATH_TTY)) break; } io = open(file, O_WRONLY); if (io < 0) syserror(); if (!isatty(io)) serror(" File exists| File exists - use \"w! %s\" to overwrite", file); close(io); break; case EDF: if (value(READONLY)) error(" File is read only"); break; case PARTBUF: if (value(READONLY)) error(" File is read only"); error(" Use \"w!\" to write partial buffer"); } cre: /* synctmp(); */ #ifdef FLOCKFILE if (*iop && !*lp != LOCK_EX && !exclam) { /* * upgrade to a exclusive lock. if can't get, someone else * has the exclusive lock. bitch to the user. */ if (flock(*iop, LOCK_EX|LOCK_NB) < 0 && errno == EWOULDBLOCK) error (" File being modified by another process - use \"w!\" to write"); else *lp = LOCK_EX ; } #endif #ifdef V6 io = creat(file, 0644); #else io = creat(file, 0666); #ifdef vms /* to retain file protection modes on newer version of file */ if (!nonexist) chmod(file, stbuf.st_mode & 0777); #endif #endif if (io < 0) syserror(); writing = 1; if (hush == 0) { if (nonexist) ex_printf(" [New file]"); else if (value(WRITEANY) && edfile() != EDF) ex_printf(" [Existing file]"); } #ifdef FLOCKFILE if (!*iop) *iop = dup(io) ; #endif break; case 2: io = open(file, O_WRONLY); if (io < 0) { if (exclam || value(WRITEANY)) goto cre; syserror(); } lseek(io, 0l, SEEK_END); #ifdef FLOCKFILE if (!*iop) *iop = dup(io) ; if (*lp != LOCK_EX && !exclam) { /* * upgrade to a exclusive lock. if can't get, * someone else has the exclusive lock. * bitch to the user. */ if (flock(*iop, LOCK_SH|LOCK_NB) < 0 && errno == EWOULDBLOCK) error ( " File being modified by another process - use \"w!>>\" to write"); else *lp = LOCK_EX ; } #endif break; } #ifdef FLOCKFILE if (flock(*iop, LOCK_EX|LOCK_NB) >= 0) *lp = LOCK_EX ; #endif putfile(0); #ifndef vms (void) fsync(io); #endif ignore(iostats()); if (c != 2 && addr1 == one && addr2 == dol) { if (eq(file, savedfile)) edited = 1; ex_sync(); } if (!dofname) { addr1 = saddr1; addr2 = saddr2; } writing = 0; }