// Gather data up to next start-of-tag or end-of-buffer. // Translate entity references (&). // Ignore non-whitespace control characters and get rid of \r's. // If find non-empty token, fill in a[*pai], bump *pai, and return Data. // Otherwise return -1; static int getdata(TokenSource* ts, int firstc, int starti, Token* a, int* pai) { Rune* s; int j; int c; Token* tok; Rune buf[SMALLBUFSIZE]; s = nil; j = 0; for(c = firstc; c >= 0; c = getchar(ts)){ if(c == '&') { c = ampersand(ts); if(c < 0) break; } else if(c < ' ') { if(isspace(c)) { if(c == '\r') { // ignore it unless no following '\n', // in which case treat it like '\n' c = getchar(ts); if(c != '\n') { if(c >= 0) ungetchar(ts, c); c = '\n'; } } } else { if(warn) fprint(2, "warning: non-whitespace control character %d ignored\n", c); c = 0; } } else if(c == '<') { ungetchar(ts, c); break; } if(c != 0) { buf[j++] = c; if(j == nelem(buf)-1) { s = buftostr(s, buf, j); j = 0; } } } s = buftostr(s, buf, j); if(s == nil) return -1; tok = &a[(*pai)++]; tok->tag = Data; tok->text = s; tok->attr = nil; tok->starti = starti; return Data; }
static void comprhs(int seof) { register char *rp, *orp; register int c; char orhsbuf[RHSSIZE]; rp = rhsbuf; CP(orhsbuf, rp); for (;;) { c = ex_getchar(); if (c == seof) break; switch (c) { case '\\': c = ex_getchar(); if (c == EOF) { ungetchar(c); break; } if (value(MAGIC)) { /* * When "magic", \& turns into a plain &, * and all other chars work fine quoted. */ if (c != '&') c |= RE_QUOTE; break; } magic: if (c == '~') { for (orp = orhsbuf; *orp; *rp++ = *orp++) if (rp >= &rhsbuf[RHSSIZE - 1]) goto toobig; continue; } c |= RE_QUOTE; break; case '\n': case EOF: ungetchar(c); goto endrhs; case '~': case '&': if (value(MAGIC)) goto magic; break; } if (rp >= &rhsbuf[RHSSIZE - 1]) toobig: error("Replacement pattern too long@- limit 256 characters"); *rp++ = c; } endrhs: *rp++ = 0; }
int readline() { SIGNAL in1, in2, out; OPTYPE op; int ch; ch = getchar(); if(ch == EOF) { return 0; } else if(ch <= 'z' && ch >= 'a') { ungetchar(ch); in1 = wire(); } else if(ch <= '9' && ch >= '0') { ungetchar(ch); in1 = number(); } else if(ch == 'N') { ungetchar(ch); in1.type = NONE; } ch = getchar(); switch(ch) { case '-': op = ASSIGN; break; case 'A': op = AND; break; case 'O': op = OR; break; case 'N': op = NOT; break; case 'L': op = LSHIFT; break; case 'R': op = RSHIFT; } while((ch = getchar()) != ' '); if(op == ASSIGN) out = wire(); else { ch = getchar(); if(ch <= 'z' && ch >= 'a') { ungetchar(ch); in2 = wire(); } else if(ch <= '9' && ch >= '0') { ungetchar(ch); in2 = number(); } while((ch = getchar()) != ' '); out = wire(); } printfunction(op, in1, in2, out); return 1; }
/******************************************* * Parsing functions * ******************************************/ void deal_with_function_args() { char c; while((c = getchar()) != ')') putchar(c); ungetchar(c); printf("is a function returning "); }
/* Read the next token into this.string if it is alphanumeric, classify_string else it must be a single character token this.type = the token itself; terminate this.string with a nul. */ void get_token() { char c = 'a'; while((c = getchar()) != EOF) { if(isspace(c)) continue; else break; } if(c == EOF) { curr_token.type = END; return; } if(isalnum(c) || c == '_') { int i = 0; while(isalnum(c) || c == '_') { curr_token.string[i++] = c; c = getchar(); } curr_token.string[i] = 0; ungetchar(c); curr_token.type = classify_string(); return; } curr_token.type = c; if(c == '*') { strcpy(curr_token.string, "pointer to "); } else { curr_token.string[0] = c; curr_token.string[1] = 0; } }
void deal_with_arrays() { char c; printf("an array["); while((c = getchar()) != ']') putchar(c); ungetchar(c); printf("] of "); }
static int peekchar(void) { int c; c = getchar(); if (c != EOF) ungetchar(c); return c; }
/* * Ignore a comment to the end of the line. * This routine eats the trailing newline so don't call donewline(). */ void comment(void) { int c; do { c = getchar(); } while (c != '\n' && c != EOF); if (c == EOF) ungetchar(c); }
void zop(char hadpr) { register int c, lines, op; zhadpr = hadpr; notempty(); znoclear = 0; zweight = 0; switch(c = op = ex_getchar()) { case '^': zweight = 1; case '-': case '+': while (peekchar() == op) { ex_getchar(); zweight++; } case '=': case '.': c = ex_getchar(); break; case EOF: znoclear++; break; default: op = 0; break; } if (digit(c)) { lines = c - '0'; for(;;) { c = ex_getchar(); if (!digit(c)) break; lines *= 10; lines += c - '0'; } if (lines < value(WINDOW)) znoclear++; if (op == '=') lines += 2; } else lines = op == EOF ? value(SCROLL) : value(WINDOW); if (c != EOF) { ungetchar(c); ex_newline(); } addr1 = addr2; setdot(); zop2(lines, op); }
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); } } }
// For case where source isn't HTML. // Just make data tokens, one per line (or partial line, // at end of buffer), ignoring non-whitespace control // characters and dumping \r's. // If find non-empty token, fill in a[*pai], bump *pai, and return Data. // Otherwise return -1; static int getplaindata(TokenSource* ts, Token* a, int* pai) { Rune* s; int j; int starti; int c; Token* tok; Rune buf[BIGBUFSIZE]; s = nil; j = 0; starti = ts->i; for(c = getchar(ts); c >= 0; c = getchar(ts)) { if(c < ' ') { if(isspace(c)) { if(c == '\r') { // ignore it unless no following '\n', // in which case treat it like '\n' c = getchar(ts); if(c != '\n') { if(c >= 0) ungetchar(ts, c); c = '\n'; } } } else c = 0; } if(c != 0) { buf[j++] = c; if(j == nelem(buf)-1) { s = buftostr(s, buf, j); j = 0; } } if(c == '\n') break; } s = buftostr(s, buf, j); if(s == nil) return -1; tok = &a[(*pai)++]; tok->tag = Data; tok->text = s; tok->attr = nil; tok->starti = starti; return Data; }
int getopt(char s[]) { int c, i = 0; while((s[0] = c = getch()) == SPACE || c == TAB); s[1] = EOL; if(!isdigit(c)) return c; while(isdigit(c = getch())) s[++i] = c; ungetchar(c); s[++i] = EOL; return NUMBER_TYPE; }
static int match(char *ocp) { char *cp; int c; for (cp = ocp + 1; *cp; cp++) { c = getchar(); if (c != *cp) { while (ocp < cp) (void) putchar(*ocp++); (void) ungetchar(c); return (0); } } return (1); }
int match(const char *ocp) { const char *cp; int c; for (cp = ocp + 1; *cp; cp++) { c = getchar(); if (c != *cp) { while (ocp < cp) putchar(*ocp++); ungetchar(c); return (0); } } return (1); }
int Next() { ungetchar(); switch (sstate) { case 1: sstate = 6; break; case 6: sstate = 9; break; case 9: printf("line %d: unrecognized token\n", line); sstate = 1; str_ptr--; break; default: printf("internal parser error: Next() in rc.c\n"); exit(-1); } return sstate; }
int getword(char *word, int lim) { char c; while(isspace(c = getch())) ; if (c != EOF) *word++ = c; if (!isalpha(c)) { *word = '\0'; return c; } for (; --lim > 0; word++) { if (!isalnum(*word = getch())) { ungetchar(*word); break; } } word[0] = '\0'; return word[0]; }
/* * Parse file name for command encoded by comm. * If comm is E then command is doomed and we are * parsing just so user won't have to retype the name. */ void filename(int comm) { int c = comm, d; int i; d = getchar(); if (endcmd(d)) { if (savedfile[0] == 0 && comm != 'f') error(value(vi_TERSE) ? gettext("No file") : gettext("No current filename")); CP(file, savedfile); wasalt = (isalt > 0) ? isalt-1 : 0; isalt = 0; oldadot = altdot; if (c == 'e' || c == 'E') altdot = lineDOT(); if (d == EOF) ungetchar(d); } else { ungetchar(d); getone(); eol(); if (savedfile[0] == 0 && c != 'E' && c != 'e') { c = 'e'; edited = 0; } wasalt = strcmp(file, altfile) == 0; oldadot = altdot; switch (c) { case 'f': edited = 0; /* FALLTHROUGH */ case 'e': if (savedfile[0]) { altdot = lineDOT(); CP(altfile, savedfile); } CP(savedfile, file); break; default: if (file[0]) { if (c != 'E') altdot = lineDOT(); CP(altfile, file); } break; } } if (hush && comm != 'f' || comm == 'E') return; if (file[0] != 0) { lprintf("\"%s\"", file); if (comm == 'f') { if (value(vi_READONLY)) viprintf(gettext(" [Read only]")); if (!edited) viprintf(gettext(" [Not edited]")); if (tchng) viprintf(gettext(" [Modified]")); } flush(); } else viprintf(gettext("No file ")); if (comm == 'f') { if (!(i = lineDOL())) i++; /* * TRANSLATION_NOTE * Reference order of arguments must not * be changed using '%digit$', since vi's * viprintf() does not support it. */ viprintf(gettext(" line %d of %d --%ld%%--"), lineDOT(), lineDOL(), (long)(100 * lineDOT() / i)); } }
/* * Parse file name for command encoded by comm. * If comm is E then command is doomed and we are * parsing just so user won't have to retype the name. */ void filename(int comm) { register int c = comm, d; register int i; d = ex_getchar(); if (endcmd(d)) { if (savedfile[0] == 0 && comm != 'f') error("No file|No current filename"); CP(file, savedfile); wasalt = (isalt > 0) ? isalt-1 : 0; isalt = 0; oldadot = altdot; if (c == 'e' || c == 'E') altdot = lineDOT(); if (d == EOF) ungetchar(d); } else { ungetchar(d); getone(); eol(); if (savedfile[0] == 0 && c != 'E' && c != 'e') { c = 'e'; edited = 0; } wasalt = strcmp(file, altfile) == 0; oldadot = altdot; switch (c) { case 'f': edited = 0; /* fall into ... */ case 'e': if (savedfile[0]) { altdot = lineDOT(); CP(altfile, savedfile); } CP(savedfile, file); break; default: if (file[0]) { if (c != 'E') altdot = lineDOT(); CP(altfile, file); } break; } } if ((hush && comm != 'f') || comm == 'E') return; if (file[0] != 0) { lprintf("\"%s\"", file); if (comm == 'f') { if (value(READONLY)) ex_printf(" [Read only]"); if (!edited) ex_printf(" [Not edited]"); if (tchng) ex_printf(" [Modified]"); } flush(); } else ex_printf("No file "); if (comm == 'f') { if (!(i = lineDOL())) i++; ex_printf(" line %d of %d --%ld%%--", lineDOT(), lineDOL(), (long) 100 * lineDOT() / i); } }
/* * Get the argument words for a command into genbuf * expanding # and %. */ int getargs(void) { register int c; register char *cp, *fp; static char fpatbuf[32]; /* hence limit on :next +/pat */ pastwh(); if (peekchar() == '+') { for (cp = fpatbuf;;) { c = *cp++ = ex_getchar(); if (cp >= &fpatbuf[sizeof(fpatbuf)]) error("Pattern too long"); if (c == '\\' && isspace(peekchar())) c = ex_getchar(); if (c == EOF || isspace(c)) { ungetchar(c); *--cp = 0; firstpat = &fpatbuf[1]; break; } } } if (skipend()) return (0); CP(genbuf, "echo "); cp = &genbuf[5]; for (;;) { c = ex_getchar(); if (endcmd(c)) { ungetchar(c); break; } switch (c) { case '\\': if (any(peekchar(), "#%|")) c = ex_getchar(); /* fall into... */ default: if (cp > &genbuf[LBSIZE - 2]) flong: error("Argument buffer overflow"); *cp++ = c; break; case '#': fp = altfile; if (*fp == 0) error("No alternate filename@to substitute for #"); goto filexp; case '%': fp = savedfile; if (*fp == 0) error("No current filename@to substitute for %%"); filexp: while (*fp) { if (cp > &genbuf[LBSIZE - 2]) goto flong; *cp++ = *fp++; } break; } } *cp = 0; return (1); }
vmain() { register int c, cnt, i; char esave[TUBECOLS]; char *oglobp; char d; line *addr; int ind, nlput; int shouldpo = 0; int onumber, olist, (*OPline)(), (*OPutchar)(); vch_mac = VC_NOTINMAC; /* * If we started as a vi command (on the command line) * then go process initial commands (recover, next or tag). */ if (initev) { oglobp = globp; globp = initev; hadcnt = cnt = 0; i = tchng; addr = dot; goto doinit; } /* * NB: * * The current line is always in the line buffer linebuf, * and the cursor at the position cursor. You should do * a vsave() before moving off the line to make sure the disk * copy is updated if it has changed, and a getDOT() to get * the line back if you mung linebuf. The motion * routines in ex_vwind.c handle most of this. */ for (;;) { /* * Decode a visual command. * First sync the temp file if there has been a reasonable * amount of change. Clear state for decoding of next * command. */ TSYNC(); vglobp = 0; vreg = 0; hold = 0; seenprompt = 1; wcursor = 0; Xhadcnt = hadcnt = 0; Xcnt = cnt = 1; splitw = 0; if (i = holdupd) { if (state == VISUAL) ignore(peekkey()); holdupd = 0; /* if (LINE(0) < ZERO) { vclear(); vcnt = 0; i = 3; } */ if (state != VISUAL) { vcnt = 0; vsave(); vrepaint(cursor); } else if (i == 3) vredraw(WTOP); else vsync(WTOP); vfixcurs(); } /* * Gobble up counts and named buffer specifications. */ for (;;) { looptop: #ifdef MDEBUG if (trace) fprintf(trace, "pc=%c",peekkey()); #endif if (isdigit(peekkey()) && peekkey() != '0') { hadcnt = 1; cnt = vgetcnt(); forbid (cnt <= 0); } if (peekkey() != '"') break; ignore(getkey()), c = getkey(); /* * Buffer names be letters or digits. * But not '0' as that is the source of * an 'empty' named buffer spec in the routine * kshift (see ex_temp.c). */ forbid (c == '0' || !isalpha(c) && !isdigit(c)); vreg = c; } reread: /* * Come to reread from below after some macro expansions. * The call to map allows use of function key pads * by performing a terminal dependent mapping of inputs. */ #ifdef MDEBUG if (trace) fprintf(trace,"pcb=%c,",peekkey()); #endif op = getkey(); maphopcnt = 0; do { /* * Keep mapping the char as long as it changes. * This allows for double mappings, e.g., q to #, * #1 to something else. */ c = op; op = map(c,arrows); #ifdef MDEBUG if (trace) fprintf(trace,"pca=%c,",c); #endif /* * Maybe the mapped to char is a count. If so, we have * to go back to the "for" to interpret it. Likewise * for a buffer name. */ if ((isdigit(c) && c!='0') || c == '"') { ungetkey(c); goto looptop; } if (!value(REMAP)) { c = op; break; } if (++maphopcnt > 256) error("Infinite macro loop"); } while (c != op); /* * Begin to build an image of this command for possible * later repeat in the buffer workcmd. It will be copied * to lastcmd by the routine setLAST * if/when completely specified. */ lastcp = workcmd; if (!vglobp) *lastcp++ = c; /* * First level command decode. */ switch (c) { /* * ^L Clear screen e.g. after transmission error. */ /* * ^R Retype screen, getting rid of @ lines. * If in open, equivalent to ^L. * On terminals where the right arrow key sends * ^L we make ^R act like ^L, since there is no * way to get ^L. These terminals (adm31, tvi) * are intelligent so ^R is useless. Soroc * will probably foul this up, but nobody has * one of them. */ case CTRL(l): case CTRL(r): if (c == CTRL(l) || (KR && *KR==CTRL(l))) { vclear(); vdirty(0, vcnt); } if (state != VISUAL) { /* * Get a clean line, throw away the * memory of what is displayed now, * and move back onto the current line. */ vclean(); vcnt = 0; vmoveto(dot, cursor, 0); continue; } vredraw(WTOP); /* * Weird glitch -- when we enter visual * in a very small window we may end up with * no lines on the screen because the line * at the top is too long. This forces the screen * to be expanded to make room for it (after * we have printed @'s ick showing we goofed). */ if (vcnt == 0) vrepaint(cursor); vfixcurs(); continue; /* * $ Escape just cancels the current command * with a little feedback. */ case ESCAPE: beep(); continue; /* * @ Macros. Bring in the macro and put it * in vmacbuf, point vglobp there and punt. */ case '@': c = getesc(); if (c == 0) continue; if (c == '@') c = lastmac; if (isupper(c)) c = tolower(c); forbid(!islower(c)); lastmac = c; vsave(); CATCH char tmpbuf[BUFSIZ]; regbuf(c,tmpbuf,sizeof(vmacbuf)); macpush(tmpbuf, 1); ONERR lastmac = 0; splitw = 0; getDOT(); vrepaint(cursor); continue; ENDCATCH vmacp = vmacbuf; goto reread; /* * . Repeat the last (modifying) open/visual command. */ case '.': /* * Check that there was a last command, and * take its count and named buffer unless they * were given anew. Special case if last command * referenced a numeric named buffer -- increment * the number and go to a named buffer again. * This allows a sequence like "1pu.u.u... * to successively look for stuff in the kill chain * much as one does in EMACS with C-Y and M-Y. */ forbid (lastcmd[0] == 0); if (hadcnt) lastcnt = cnt; if (vreg) lastreg = vreg; else if (isdigit(lastreg) && lastreg < '9') lastreg++; vreg = lastreg; cnt = lastcnt; hadcnt = lasthad; vglobp = lastcmd; goto reread; /* * ^U Scroll up. A count sticks around for * future scrolls as the scroll amount. * Attempt to hold the indentation from the * top of the screen (in logical lines). * * BUG: A ^U near the bottom of the screen * on a dumb terminal (which can't roll back) * causes the screen to be cleared and then * redrawn almost as it was. In this case * one should simply move the cursor. */ case CTRL(u): if (hadcnt) vSCROLL = cnt; cnt = vSCROLL; if (state == VISUAL) ind = vcline, cnt += ind; else ind = 0; vmoving = 0; vup(cnt, ind, 1); vnline(NOSTR); continue; /* * ^D Scroll down. Like scroll up. */ case CTRL(d): #ifdef TRACE if (trace) fprintf(trace, "before vdown in ^D, dot=%d, wdot=%d, dol=%d\n", lineno(dot), lineno(wdot), lineno(dol)); #endif if (hadcnt) vSCROLL = cnt; cnt = vSCROLL; if (state == VISUAL) ind = vcnt - vcline - 1, cnt += ind; else ind = 0; vmoving = 0; vdown(cnt, ind, 1); #ifdef TRACE if (trace) fprintf(trace, "before vnline in ^D, dot=%d, wdot=%d, dol=%d\n", lineno(dot), lineno(wdot), lineno(dol)); #endif vnline(NOSTR); #ifdef TRACE if (trace) fprintf(trace, "after vnline in ^D, dot=%d, wdot=%d, dol=%d\n", lineno(dot), lineno(wdot), lineno(dol)); #endif continue; /* * ^E Glitch the screen down (one) line. * Cursor left on same line in file. */ case CTRL(e): if (state != VISUAL) continue; if (!hadcnt) cnt = 1; /* Bottom line of file already on screen */ forbid(lineDOL()-lineDOT() <= vcnt-1-vcline); ind = vcnt - vcline - 1 + cnt; vdown(ind, ind, 1); vnline(cursor); continue; /* * ^Y Like ^E but up */ case CTRL(y): if (state != VISUAL) continue; if (!hadcnt) cnt = 1; forbid(lineDOT()-1<=vcline); /* line 1 already there */ ind = vcline + cnt; vup(ind, ind, 1); vnline(cursor); continue; /* * m Mark position in mark register given * by following letter. Return is * accomplished via ' or `; former * to beginning of line where mark * was set, latter to column where marked. */ case 'm': /* * Getesc is generally used when a character * is read as a latter part of a command * to allow one to hit rubout/escape to cancel * what you have typed so far. These characters * are mapped to 0 by the subroutine. */ c = getesc(); if (c == 0) continue; /* * Markreg checks that argument is a letter * and also maps ' and ` to the end of the range * to allow '' or `` to reference the previous * context mark. */ c = markreg(c); forbid (c == 0); vsave(); names[c - 'a'] = (*dot &~ 01); ncols[c - 'a'] = cursor; anymarks = 1; continue; /* * ^F Window forwards, with 2 lines of continuity. * Count repeats. */ case CTRL(f): vsave(); if (vcnt > 2) { addr = dot + (vcnt - vcline) - 2 + (cnt-1)*basWLINES; forbid(addr > dol); dot = addr; vcnt = vcline = 0; } vzop(0, 0, '+'); continue; /* * ^B Window backwards, with 2 lines of continuity. * Inverse of ^F. */ case CTRL(b): vsave(); if (one + vcline != dot && vcnt > 2) { addr = dot - vcline - 2 + (cnt-1)*basWLINES; forbid (addr <= zero); dot = addr; vcnt = vcline = 0; } vzop(0, 0, '^'); continue; /* * z Screen adjustment, taking a following character: * z<CR> current line to top * z<NL> like z<CR> * z- current line to bottom * also z+, z^ like ^F and ^B. * A preceding count is line to use rather * than current line. A count between z and * specifier character changes the screen size * for the redraw. * */ case 'z': if (state == VISUAL) { i = vgetcnt(); if (i > 0) vsetsiz(i); c = getesc(); if (c == 0) continue; } vsave(); vzop(hadcnt, cnt, c); continue; /* * Y Yank lines, abbreviation for y_ or yy. * Yanked lines can be put later if no * changes intervene, or can be put in named * buffers and put anytime in this session. */ case 'Y': ungetkey('_'); c = 'y'; break; /* * J Join lines, 2 by default. Count is number * of lines to join (no join operator sorry.) */ case 'J': forbid (dot == dol); if (cnt == 1) cnt = 2; if (cnt > (i = dol - dot + 1)) cnt = i; vsave(); vmacchng(1); setLAST(); cursor = strend(linebuf); vremote(cnt, join, 0); notenam = "join"; vmoving = 0; killU(); vreplace(vcline, cnt, 1); if (!*cursor && cursor > linebuf) cursor--; if (notecnt == 2) notecnt = 0; vrepaint(cursor); continue; /* * S Substitute text for whole lines, abbrev for c_. * Count is number of lines to change. */ case 'S': ungetkey('_'); c = 'c'; break; /* * O Create a new line above current and accept new * input text, to an escape, there. * A count specifies, for dumb terminals when * slowopen is not set, the number of physical * line space to open on the screen. * * o Like O, but opens lines below. */ case 'O': case 'o': vmacchng(1); voOpen(c, cnt); continue; /* * C Change text to end of line, short for c$. */ case 'C': if (*cursor) { ungetkey('$'), c = 'c'; break; } goto appnd; /* * ~ Switch case of letter under cursor */ case '~': { char mbuf[4]; setLAST(); mbuf[0] = 'r'; mbuf[1] = *cursor; mbuf[2] = cursor[1]==0 ? 0 : ' '; mbuf[3] = 0; if (isalpha(mbuf[1])) mbuf[1] ^= ' '; /* toggle the case */ macpush(mbuf, 1); } continue; /* * A Append at end of line, short for $a. */ case 'A': operate('$', 1); appnd: c = 'a'; /* fall into ... */ /* * a Appends text after cursor. Text can continue * through arbitrary number of lines. */ case 'a': if (*cursor) { if (state == HARDOPEN) putchar(*cursor); cursor++; } goto insrt; /* * I Insert at beginning of whitespace of line, * short for ^i. */ case 'I': operate('^', 1); c = 'i'; /* fall into ... */ /* * R Replace characters, one for one, by input * (logically), like repeated r commands. * * BUG: This is like the typeover mode of many other * editors, and is only rarely useful. Its * implementation is a hack in a low level * routine and it doesn't work very well, e.g. * you can't move around within a R, etc. */ case 'R': /* fall into... */ /* * i Insert text to an escape in the buffer. * Text is arbitrary. This command reminds of * the i command in bare teco. */ case 'i': insrt: /* * Common code for all the insertion commands. * Save for redo, position cursor, prepare for append * at command and in visual undo. Note that nothing * is doomed, unless R when all is, and save the * current line in a the undo temporary buffer. */ vmacchng(1); setLAST(); vcursat(cursor); prepapp(); vnoapp(); doomed = c == 'R' ? 10000 : 0; if(FIXUNDO) vundkind = VCHNG; vmoving = 0; CP(vutmp, linebuf); /* * If this is a repeated command, then suppress * fake insert mode on dumb terminals which looks * ridiculous and wastes lots of time even at 9600B. */ if (vglobp) hold = HOLDQIK; vappend(c, cnt, 0); continue; /* * ^? An attention, normally a ^?, just beeps. * If you are a vi command within ex, then * two ATTN's will drop you back to command mode. */ case ATTN: beep(); if (initev || peekkey() != ATTN) continue; /* fall into... */ /* * ^\ A quit always gets command mode. */ case QUIT: /* * Have to be careful if we were called * g/xxx/vi * since a return will just start up again. * So we simulate an interrupt. */ if (inglobal) onintr(); /* fall into... */ #ifdef notdef /* * q Quit back to command mode, unless called as * vi on command line in which case dont do it */ case 'q': /* quit */ if (initev) { vsave(); CATCH error("Q gets ex command mode, :q leaves vi"); ENDCATCH splitw = 0; getDOT(); vrepaint(cursor); continue; } #endif /* fall into... */ /* * Q Is like q, but always gets to command mode * even if command line invocation was as vi. */ case 'Q': vsave(); /* * If we are in the middle of a macro, throw away * the rest and fix up undo. * This code copied from getbr(). */ if (vmacp) { vmacp = 0; if (inopen == -1) /* don't screw up undo for esc esc */ vundkind = VMANY; inopen = 1; /* restore old setting now that macro done */ } return; /* * ZZ Like :x */ case 'Z': forbid(getkey() != 'Z'); oglobp = globp; globp = "x"; vclrech(0); goto gogo; /* * P Put back text before cursor or before current * line. If text was whole lines goes back * as whole lines. If part of a single line * or parts of whole lines splits up current * line to form many new lines. * May specify a named buffer, or the delete * saving buffers 1-9. * * p Like P but after rather than before. */ case 'P': case 'p': vmoving = 0; #ifdef notdef forbid (!vreg && value(UNDOMACRO) && inopen < 0); #endif /* * If previous delete was partial line, use an * append or insert to put it back so as to * use insert mode on intelligent terminals. */ if (!vreg && DEL[0]) { forbid ((DEL[0] & (QUOTE|TRIM)) == OVERBUF); vglobp = DEL; ungetkey(c == 'p' ? 'a' : 'i'); goto reread; } /* * If a register wasn't specified, then make * sure there is something to put back. */ forbid (!vreg && unddol == dol); /* * If we just did a macro the whole buffer is in * the undo save area. We don't want to put THAT. */ forbid (vundkind == VMANY && undkind==UNDALL); vsave(); vmacchng(1); setLAST(); i = 0; if (vreg && partreg(vreg) || !vreg && pkill[0]) { /* * Restoring multiple lines which were partial * lines; will leave cursor in middle * of line after shoving restored text in to * split the current line. */ i++; if (c == 'p' && *cursor) cursor++; } else { /* * In whole line case, have to back up dot * for P; also want to clear cursor so * cursor will eventually be positioned * at the beginning of the first put line. */ cursor = 0; if (c == 'P') { dot--, vcline--; c = 'p'; } } killU(); /* * The call to putreg can potentially * bomb since there may be nothing in a named buffer. * We thus put a catch in here. If we didn't and * there was an error we would end up in command mode. */ addr = dol; /* old dol */ CATCH vremote(1, vreg ? putreg : put, vreg); ONERR if (vreg == -1) { splitw = 0; if (op == 'P') dot++, vcline++; goto pfixup; } ENDCATCH splitw = 0; nlput = dol - addr + 1; if (!i) { /* * Increment undap1, undap2 to make up * for their incorrect initialization in the * routine vremote before calling put/putreg. */ if (FIXUNDO) undap1++, undap2++; vcline++; nlput--; /* * After a put want current line first line, * and dot was made the last line put in code * run so far. This is why we increment vcline * above and decrease dot here. */ dot -= nlput - 1; } #ifdef TRACE if (trace) fprintf(trace, "vreplace(%d, %d, %d), undap1=%d, undap2=%d, dot=%d\n", vcline, i, nlput, lineno(undap1), lineno(undap2), lineno(dot)); #endif vreplace(vcline, i, nlput); if (state != VISUAL) { /* * Special case in open mode. * Force action on the screen when a single * line is put even if it is identical to * the current line, e.g. on YP; otherwise * you can't tell anything happened. */ vjumpto(dot, cursor, '.'); continue; } pfixup: vrepaint(cursor); vfixcurs(); continue; /* * ^^ Return to previous file. * Like a :e #, and thus can be used after a * "No Write" diagnostic. */ case CTRL(^): forbid (hadcnt); vsave(); ckaw(); oglobp = globp; if (value(AUTOWRITE)) globp = "e! #"; else globp = "e #"; goto gogo; /* * ^] Takes word after cursor as tag, and then does * tag command. Read ``go right to''. */ case CTRL(]): grabtag(); oglobp = globp; globp = "tag"; goto gogo; /* * & Like :& */ case '&': oglobp = globp; globp = "&"; goto gogo; /* * ^G Bring up a status line at the bottom of * the screen, like a :file command. * * BUG: Was ^S but doesn't work in cbreak mode */ case CTRL(g): oglobp = globp; globp = "file"; gogo: addr = dot; vsave(); goto doinit; #ifdef SIGTSTP /* * ^Z: suspend editor session and temporarily return * to shell. Only works with Berkeley/IIASA process * control in kernel. */ case CTRL(z): forbid(dosusp == 0 || !ldisc); vsave(); oglobp = globp; globp = "stop"; goto gogo; #endif /* * : Read a command from the echo area and * execute it in command mode. */ case ':': forbid (hadcnt); vsave(); i = tchng; addr = dot; if (readecho(c)) { esave[0] = 0; goto fixup; } getDOT(); /* * Use the visual undo buffer to store the global * string for command mode, since it is idle right now. */ oglobp = globp; strcpy(vutmp, genbuf+1); globp = vutmp; doinit: esave[0] = 0; fixech(); /* * Have to finagle around not to lose last * character after this command (when run from ex * command mode). This is clumsy. */ d = peekc; ungetchar(0); if (shouldpo) { /* * So after a "Hit return..." ":", we do * another "Hit return..." the next time */ pofix(); shouldpo = 0; } CATCH /* * Save old values of options so we can * notice when they change; switch into * cooked mode so we are interruptible. */ onumber = value(NUMBER); olist = value(LIST); OPline = Pline; OPutchar = Putchar; #ifndef CBREAK vcook(); #endif commands(1, 1); if (dot == zero && dol > zero) dot = one; #ifndef CBREAK vraw(); #endif ONERR #ifndef CBREAK vraw(); #endif copy(esave, vtube[WECHO], TUBECOLS); ENDCATCH fixol(); Pline = OPline; Putchar = OPutchar; ungetchar(d); globp = oglobp; /* * If we ended up with no lines in the buffer, make * a line, and don't consider the buffer changed. */ if (dot == zero) { fixzero(); sync(); } splitw = 0; /* * Special case: did list/number options change? */ if (onumber != value(NUMBER)) setnumb(value(NUMBER)); if (olist != value(LIST)) setlist(value(LIST)); fixup: /* * If a change occurred, other than * a write which clears changes, then * we should allow an undo even if . * didn't move. * * BUG: You can make this wrong by * tricking around with multiple commands * on one line of : escape, and including * a write command there, but its not * worth worrying about. */ if (FIXUNDO && tchng && tchng != i) vundkind = VMANY, cursor = 0; /* * If we are about to do another :, hold off * updating of screen. */ if (vcnt < 0 && Peekkey == ':') { getDOT(); shouldpo = 1; continue; } shouldpo = 0; /* * In the case where the file being edited is * new; e.g. if the initial state hasn't been * saved yet, then do so now. */ if (unddol == truedol) { vundkind = VNONE; Vlines = lineDOL(); if (!inglobal) savevis(); addr = zero; vcnt = 0; if (esave[0] == 0) copy(esave, vtube[WECHO], TUBECOLS); } /* * If the current line moved reset the cursor position. */ if (dot != addr) { vmoving = 0; cursor = 0; } /* * If current line is not on screen or if we are * in open mode and . moved, then redraw. */ i = vcline + (dot - addr); if (i < 0 || i >= vcnt && i >= -vcnt || state != VISUAL && dot != addr) { if (state == CRTOPEN) vup1(); if (vcnt > 0) vcnt = 0; vjumpto(dot, (char *) 0, '.'); } else { /* * Current line IS on screen. * If we did a [Hit return...] then * restore vcnt and clear screen if in visual */ vcline = i; if (vcnt < 0) { vcnt = -vcnt; if (state == VISUAL) vclear(); else if (state == CRTOPEN) { vcnt = 0; } } /* * Limit max value of vcnt based on $ */ i = vcline + lineDOL() - lineDOT() + 1; if (i < vcnt) vcnt = i; /* * Dirty and repaint. */ vdirty(0, LINES); vrepaint(cursor); } /* * If in visual, put back the echo area * if it was clobberred. */ if (state == VISUAL) { int sdc = destcol, sdl = destline; splitw++; vigoto(WECHO, 0); for (i = 0; i < TUBECOLS - 1; i++) { if (esave[i] == 0) break; vputchar(esave[i]); } splitw = 0; vgoto(sdl, sdc); } continue; /* * u undo the last changing command. */ case 'u': vundo(1); continue; /* * U restore current line to initial state. */ case 'U': vUndo(); continue; fonfon: beep(); vmacp = 0; inopen = 1; /* might have been -1 */ continue; } /* * Rest of commands are decoded by the operate * routine. */ operate(c, cnt); } }
void global(bool k) { register char *gp; register int c; register line *a1; char globuf[GBSIZE], *Cwas; int lines = lineDOL(); char *oglobp = globp; Cwas = Command; if (inglobal) error("Global within global@not allowed"); markDOT(); setall(); nonzero(); if (skipend()) error("Global needs re|Missing regular expression for global"); c = ex_getchar(); ignore(compile(c, 0)); savere(scanre); gp = globuf; while ((c = ex_getchar()) != '\n') { switch (c) { case EOF: c = '\n'; goto brkwh; case '\\': c = ex_getchar(); switch (c) { case '\\': ungetchar(c); break; case '\n': break; default: *gp++ = '\\'; break; } break; } *gp++ = c; if (gp >= &globuf[GBSIZE - 2]) error("Global command too long"); } brkwh: ungetchar(c); ex_newline(); *gp++ = c; *gp++ = 0; inglobal = 1; for (a1 = one; a1 <= dol; a1++) { *a1 &= ~01; if (a1 >= addr1 && a1 <= addr2 && execute(0, a1) == k) *a1 |= 01; } /* should use gdelete from ed to avoid n**2 here on g/.../d */ saveall(); if (inopen) inopen = -1; for (a1 = one; a1 <= dol; a1++) { if (*a1 & 01) { *a1 &= ~01; dot = a1; globp = globuf; commands(1, 1); a1 = zero; } } globp = oglobp; inglobal = 0; endline = 1; Command = Cwas; netchHAD(lines); setlastchar(EOF); if (inopen) { ungetchar(EOF); inopen = 1; } }
void operate(int c, int cnt) { register int i; void (*moveop)(), (*deleteop)(); void (*opf)(); bool subop = 0; char *oglobp, *ocurs; register line *addr; static char lastFKND, lastFCHR; char d; moveop = vmove, deleteop = vdelete; wcursor = cursor; wdot = NOLINE; notecnt = 0; dir = 1; switch (c) { /* * d delete operator. */ case 'd': moveop = vdelete; deleteop = beep; break; /* * s substitute characters, like c\040, i.e. change space. */ case 's': ungetkey(' '); subop++; /* fall into ... */ /* * c Change operator. */ case 'c': if ((c == 'c' && workcmd[0] == 'C') || workcmd[0] == 'S') subop++; moveop = vchange; deleteop = beep; break; /* * ! Filter through a UNIX command. */ case '!': moveop = vfilter; deleteop = beep; break; /* * y Yank operator. Place specified text so that it * can be put back with p/P. Also yanks to named buffers. */ case 'y': moveop = vyankit; deleteop = beep; break; /* * = Reformat operator (for LISP). */ #ifdef LISP case '=': forbid(!value(LISP)); /* fall into ... */ #endif /* * > Right shift operator. * < Left shift operator. */ case '<': case '>': moveop = vshftop; deleteop = beep; break; /* * r Replace character under cursor with single following * character. */ case 'r': vrep(cnt); return; default: goto nocount; } /* * Had an operator, so accept another count. * Multiply counts together. */ if (isdigit(peekkey()) && peekkey() != '0') { cnt *= vgetcnt(); Xcnt = cnt; forbid (cnt <= 0); } /* * Get next character, mapping it and saving as * part of command for repeat. */ c = map(getesc()); if (c == 0) return; if (!subop) *lastcp++ = c; nocount: opf = moveop; switch (c) { /* * b Back up a word. * B Back up a word, liberal definition. */ case 'b': case 'B': dir = -1; /* fall into ... */ /* * w Forward a word. * W Forward a word, liberal definition. */ case 'W': case 'w': wdkind = c & ' '; if (edge()) { forbid (opf == vmove); wcursor = dir == -1 ? linebuf : strend(linebuf); } else while (cnt > 0 && !edge()) word(opf, cnt), cnt--; vmoving = 0; break; /* * E to end of following blank/nonblank word */ case 'E': wdkind = 0; goto ein; /* * e To end of following word. */ case 'e': wdkind = 1; ein: if (edge()) { forbid(opf == vmove); wcursor = dir == -1 ? linebuf : strend(linebuf); } else { while (cnt > 1 && !edge()) { word(opf, cnt); cnt--; } eend(opf, cnt); } vmoving = 0; break; /* * ( Back an s-expression. */ case '(': dir = -1; /* fall into... */ /* * ) Forward an s-expression. */ case ')': forbid(lfind(0, cnt, opf, (line *) 0) < 0); if (wdot) markpr(wdot); break; /* * { Back an s-expression, but don't stop on atoms. * In text mode, a paragraph. For C, a balanced set * of {}'s. */ case '{': dir = -1; /* fall into... */ /* * } Forward an s-expression, but don't stop on atoms. * In text mode, back paragraph. For C, back a balanced * set of {}'s. */ case '}': forbid(lfind(1, cnt, opf, (line *) 0) < 0); if (wdot) markpr(wdot); break; /* * % To matching () or {}. If not at ( or { scan for * first such after cursor on this line. */ case '%': vsave(); i = lmatchp((line *) 0); getDOT(); forbid(!i); if (opf != vmove) if (dir > 0) wcursor++; else cursor++; else if (wdot) markpr(wdot); break; /* * [ Back to beginning of defun, i.e. an ( in column 1. * For text, back to a section macro. * For C, back to a { in column 1 (~~ beg of function.) */ case '[': dir = -1; /* fall into ... */ /* * ] Forward to next defun, i.e. a ( in column 1. * For text, forward section. * For C, forward to a } in column 1 (if delete or such) * or if a move to a { in column 1. */ case ']': if (!vglobp) forbid(getkey() != c); if (Xhadcnt) vsetsiz(Xcnt); vsave(); i = lbrack(c, opf); getDOT(); forbid(!i); if (wdot) markpr(wdot); if (ex_ospeed > B300) hold |= HOLDWIG; break; /* * , Invert last find with f F t or T, like inverse * of ;. */ case ',': forbid (lastFKND == 0); c = isupper((int)lastFKND) ? tolower((int)lastFKND) : toupper((int)lastFKND); ungetkey(lastFCHR); if (vglobp == 0) vglobp = ""; subop++; goto nocount; /* * 0 To beginning of real line. */ case '0': wcursor = linebuf; vmoving = 0; break; /* * ; Repeat last find with f F t or T. */ case ';': forbid (lastFKND == 0); c = lastFKND; ungetkey(lastFCHR); subop++; goto nocount; /* * F Find single character before cursor in current line. * T Like F, but stops before character. */ case 'F': /* inverted find */ case 'T': dir = -1; /* fall into ... */ /* * f Find single character following cursor in current line. * t Like f, but stope before character. */ case 'f': /* find */ case 't': i = getesc(); if (i == 0) return; if (!subop) *lastcp++ = i; if (vglobp == 0) lastFKND = c, lastFCHR = i; for (; cnt > 0; cnt--) forbid (find(i) == 0); switch (c) { case 'T': wcursor++; break; case 't': wcursor--; case 'f': fixup: if (moveop != vmove) wcursor++; break; } vmoving = 0; break; /* * | Find specified print column in current line. */ case '|': if (Pline == numbline) cnt += 8; vmovcol = cnt; vmoving = 1; wcursor = vfindcol(cnt); break; /* * ^ To beginning of non-white space on line. */ case '^': wcursor = vskipwh(linebuf); vmoving = 0; break; /* * $ To end of line. */ case '$': if (cnt > 1) { if (opf == vmove) { wcursor = 0; vmoving = 1; vmovcol = 20000; cnt--; } else wcursor = linebuf; wdot = dot + cnt; break; } if (linebuf[0]) { wcursor = strend(linebuf) - 1; goto fixup; } wcursor = linebuf; vmoving = 0; break; /* * h Back a character. * ^H Back a character. */ case 'h': case CTRL('h'): dir = -1; /* fall into ... */ /* * space Forward a character. */ case ' ': forbid (margin() || (opf == vmove && edge())); while (cnt > 0 && !margin()) wcursor += dir, cnt--; if ((margin() && opf == vmove) || wcursor < linebuf) wcursor -= dir; vmoving = 0; break; /* * D Delete to end of line, short for d$. */ case 'D': cnt = INF; goto deleteit; /* * X Delete character before cursor. */ case 'X': dir = -1; /* fall into ... */ deleteit: /* * x Delete character at cursor, leaving cursor where it is. */ case 'x': if (margin()) goto errlab; while (cnt > 0 && !margin()) wcursor += dir, cnt--; opf = deleteop; vmoving = 0; break; default: /* * Stuttered operators are equivalent to the operator on * a line, thus turn dd into d_. */ if (opf == vmove || c != workcmd[0]) { errlab: beep(); return; } /* fall into ... */ /* * _ Target for a line or group of lines. * Stuttering is more convenient; this is mostly * for aesthetics. */ case '_': wdot = dot + cnt - 1; vmoving = 0; wcursor = 0; break; /* * H To first, home line on screen. * Count is for count'th line rather than first. */ case 'H': wdot = (dot - vcline) + cnt - 1; if (opf == vmove) markit(wdot); vmoving = 0; wcursor = 0; break; /* * - Backwards lines, to first non-white character. */ case '-': wdot = dot - cnt; vmoving = 0; wcursor = 0; break; /* * ^P To previous line same column. Ridiculous on the * console of the VAX since it puts console in LSI mode. */ case CTRL('p'): wdot = dot - cnt; if (vmoving == 0) vmoving = 1, vmovcol = column(cursor); wcursor = 0; break; /* * L To last line on screen, or count'th line from the * bottom. */ case 'L': wdot = dot + vcnt - vcline - cnt; if (opf == vmove) markit(wdot); vmoving = 0; wcursor = 0; break; /* * M To the middle of the screen. */ case 'M': wdot = dot + ((vcnt + 1) / 2) - vcline - 1; if (opf == vmove) markit(wdot); vmoving = 0; wcursor = 0; break; /* * + Forward line, to first non-white. * * CR Convenient synonym for +. */ case '+': case CR: wdot = dot + cnt; vmoving = 0; wcursor = 0; break; /* * ^N To next line, same column if possible. * * LF Linefeed is a convenient synonym for ^N. */ case CTRL('n'): case NL: wdot = dot + cnt; if (vmoving == 0) vmoving = 1, vmovcol = column(cursor); wcursor = 0; break; /* * n Search to next match of current pattern. */ case 'n': vglobp = vscandir; c = *vglobp++; goto nocount; /* * N Like n but in reverse direction. */ case 'N': vglobp = vscandir[0] == '/' ? "?" : "/"; c = *vglobp++; goto nocount; /* * ' Return to line specified by following mark, * first white position on line. * * ` Return to marked line at remembered column. */ case '\'': case '`': d = c; c = getesc(); if (c == 0) return; c = markreg(c); forbid (c == 0); wdot = getmark(c); forbid (wdot == NOLINE); if (Xhadcnt) vsetsiz(Xcnt); if (opf == vmove) markit(wdot); vmoving = 0; wcursor = d == '`' ? ncols[c - 'a'] : 0; if (wcursor) { vsave(); ex_getline(*wdot); if (wcursor > strend(linebuf)) wcursor = 0; getDOT(); } if (ex_ospeed > B300) hold |= HOLDWIG; break; /* * G Goto count'th line, or last line if no count * given. */ case 'G': if (!Xhadcnt) cnt = lineDOL(); wdot = zero + cnt; forbid (wdot < one || wdot > dol); if (opf == vmove) markit(wdot); vmoving = 0; wcursor = 0; break; /* * / Scan forward for following re. * ? Scan backward for following re. */ case '/': case '?': if (Xhadcnt) vsetsiz(Xcnt); vsave(); ocurs = cursor; wcursor = 0; if (readecho(c)) return; if (!vglobp) vscandir[0] = genbuf[0]; oglobp = globp; CP(vutmp, genbuf); globp = vutmp; d = peekc; ungetchar(0); fixech(); CATCH #ifdef V6 /* * Lose typeahead (ick). */ vcook(); #endif addr = address(); #ifdef V6 vraw(); #endif ONERR #ifdef V6 vraw(); #endif globp = oglobp; ungetchar(d); splitw = 0; vclean(); vjumpto(dot, ocurs, 0); return; ENDCATCH if (globp == 0) globp = ""; else if (peekc) --globp; ungetchar(d); c = 0; if (*globp == 'z') globp++, c = '\n'; if (any(*globp, "^+-.")) c = *globp++; i = 0; while (isdigit((int)*globp)) i = i * 10 + *globp++ - '0'; if (*globp) c = *globp++; globp = oglobp; splitw = 0; vmoving = 0; if (i != 0) vsetsiz(i); if (opf == vmove) { if (state == ONEOPEN || state == HARDOPEN) outline = destline = WBOT; markit(addr); if (loc1 > linebuf && *loc1 == 0) loc1--; if (c) vjumpto(addr, loc1, c); else { vmoving = 0; if (loc1) { vmoving++; vmovcol = column(loc1); } getDOT(); if (state == CRTOPEN && addr != dot) vup1(); vupdown(addr - dot, NOSTR); } return; } lastcp[-1] = 'n'; getDOT(); wdot = addr; break; } /* * Apply. */ if (vreg && wdot == 0) wdot = dot; (*opf)(c); wdot = NOLINE; }
// We've just seen a '<'. Gather up stuff to closing '>' (if buffer // ends before then, return -1). // If it's a tag, look up the name, gather the attributes, and return // the appropriate token. // Else it's either just plain data or some kind of ignorable stuff: // return Data or Comment as appropriate. // If it's not a Comment, put it in a[*pai] and bump *pai. static int gettag(TokenSource* ts, int starti, Token* a, int* pai) { int rbra; int ans; Attr* al; int nexti; int c; int ti; int afnd; int attid; int quote; Rune* val; int nv; int i; int tag; Token* tok; Rune buf[BIGBUFSIZE]; rbra = 0; nexti = ts->i; tok = &a[*pai]; tok->tag = Notfound; tok->text = nil; tok->attr = nil; tok->starti = starti; c = getchar(ts); if(c == '/') { rbra = RBRA; c = getchar(ts); } if(c < 0) goto eob_done; if(c >= 256 || !isalpha(c)) { // not a tag if(c == '!') { ans = comment(ts); if(ans != -1) return ans; goto eob_done; } else { backup(ts, nexti); tok->tag = Data; tok->text = _Strdup(L"<"); (*pai)++; return Data; } } // c starts a tagname buf[0] = c; i = 1; while(1) { c = getchar(ts); if(c < 0) goto eob_done; if(!ISNAMCHAR(c)) break; // if name is bigger than buf it won't be found anyway... if(i < BIGBUFSIZE) buf[i++] = c; } if(_lookup(tagtable, Numtags, buf, i, &tag)) tok->tag = tag + rbra; else tok->text = _Strndup(buf, i); // for warning print, in build // attribute gathering loop al = nil; while(1) { // look for "ws name" or "ws name ws = ws val" (ws=whitespace) // skip whitespace attrloop_continue: while(c < 256 && isspace(c)) { c = getchar(ts); if(c < 0) goto eob_done; } if(c == '>') goto attrloop_done; if(c == '<') { if(warn) fprint(2, "warning: unclosed tag\n"); ungetchar(ts, c); goto attrloop_done; } if(c >= 256 || !isalpha(c)) { if(warn) fprint(2, "warning: expected attribute name\n"); // skipt to next attribute name while(1) { c = getchar(ts); if(c < 0) goto eob_done; if(c < 256 && isalpha(c)) goto attrloop_continue; if(c == '<') { if(warn) fprint(2, "warning: unclosed tag\n"); ungetchar(ts, 60); goto attrloop_done; } if(c == '>') goto attrloop_done; } } // gather attribute name buf[0] = c; i = 1; while(1) { c = getchar(ts); if(c < 0) goto eob_done; if(!ISNAMCHAR(c)) break; if(i < BIGBUFSIZE-1) buf[i++] = c; } afnd = _lookup(attrtable, Numattrs, buf, i, &attid); if(warn && !afnd) { buf[i] = 0; fprint(2, "warning: unknown attribute name %S\n", buf); } // skip whitespace while(c < 256 && isspace(c)) { c = getchar(ts); if(c < 0) goto eob_done; } if(c != '=') { if(afnd) al = newattr(attid, nil, al); goto attrloop_continue; } //# c is '=' here; skip whitespace while(1) { c = getchar(ts); if(c < 0) goto eob_done; if(c >= 256 || !isspace(c)) break; } quote = 0; if(c == '\'' || c == '"') { quote = c; c = getchar(ts); if(c < 0) goto eob_done; } val = nil; nv = 0; while(1) { valloop_continue: if(c < 0) goto eob_done; if(c == '>') { if(quote) { // c might be part of string (though not good style) // but if line ends before close quote, assume // there was an unmatched quote ti = ts->i; while(1) { c = getchar(ts); if(c < 0) goto eob_done; if(c == quote) { backup(ts, ti); buf[nv++] = '>'; if(nv == BIGBUFSIZE-1) { val = buftostr(val, buf, nv); nv = 0; } c = getchar(ts); goto valloop_continue; } if(c == '\n') { if(warn) fprint(2, "warning: apparent unmatched quote\n"); backup(ts, ti); c = '>'; goto valloop_done; } } } else goto valloop_done; } if(quote) { if(c == quote) { c = getchar(ts); if(c < 0) goto eob_done; goto valloop_done; } if(c == '\r') { c = getchar(ts); goto valloop_continue; } if(c == '\t' || c == '\n') c = ' '; } else { if(c < 256 && isspace(c)) goto valloop_done; } if(c == '&') { c = ampersand(ts); if(c == -1) goto eob_done; } buf[nv++] = c; if(nv == BIGBUFSIZE-1) { val = buftostr(val, buf, nv); nv = 0; } c = getchar(ts); } valloop_done: if(afnd) { val = buftostr(val, buf, nv); al = newattr(attid, val, al); } } attrloop_done: tok->attr = al; (*pai)++; return tok->tag; eob_done: if(warn) fprint(2, "warning: incomplete tag at end of page\n"); backup(ts, nexti); tok->tag = Data; tok->text = _Strdup(L"<"); return Data; }
/* * 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); } }
int main() { // FLAGS int startF = 1; int pcstart = 0; int pcend = 0; int blockF = 0; int lineF = 0; int stringF = 0; int charF = 0; int whiteF = 0; int newlineF = 0; int c; while ((c = getchar()) != EOF) { // REMOVE LINE SPLICES if (c == '\\') { int d = getchar(); // ESCAPE CHARACTER CONSIDERATIONS if (d == '"' && stringF) { putchar(c); putchar(d); continue; } if (d == '\'' && charF) { putchar(c); putchar(d); continue; } else if (d == '\\' && (stringF || charF)) { putchar(c); putchar(d); continue; } else if (d != '\n') { ungetchar(d); } else { continue; } } // INSIDE BLOCK COMMENT if (blockF) { if (c == '*') { pcend = 1; } else if (c == '/' && pcend) { blockF = 0; whiteF = 1; } else { pcend = 0; } } // INSIDE LINE COMMENT else if (lineF) { if (c == '\n') { lineF = 0; whiteF = 1; newlineF = 1; } } // INSIDE STRING else if (stringF) { if (c == '"') { stringF = 0; } putchar(c); } // INSIDE CHAR CONSTANT else if (charF) { if (c == '\'') { charF = 0; } putchar(c); } // DETECT WHITESPACES else if (c == ' ') { whiteF = 1; } // DETECT NEWLINES else if (c == '\n') { newlineF = 1; } // DETECT BLOCK COMMENT else if (c == '*' && pcstart) { blockF = 1; pcstart = 0; } // DETECT LINE COMMENT else if (c == '/' && pcstart) { lineF = 1; pcstart = 0; } // DETECT POSSIBLE COMMENT else if (c == '/') { pcstart = 1; } // OTHER CASES else { // START OF FILE CONSIDERATION if (startF) { whiteF = 0; newlineF = 0; startF = 0; } else if (newlineF) { int d = '\n'; putchar(d); whiteF = 0; newlineF = 0; } else if (whiteF) { int d = ' '; putchar(d); whiteF = 0; } // DETECT STRING if (c == '"') { stringF = 1; } // DETECT CHAR CONSTANT if (c == '\'') { charF = 1; } // TURN OFF PCSTART if (pcstart) { pcstart = 0; int d = '/'; putchar(d); } putchar(c); } } // END OF FILE CONSIDERATIONS if (pcstart) { int d = '/'; putchar(d); } if (startF) { // EMPTY FILE CONSIDERATION } else if (newlineF) { int d = '\n'; putchar(d); } else if (whiteF) { int d = ' '; putchar(d); } else if (blockF || lineF) { int d = '\n'; putchar(d); } else { } }
/*ARGSUSED*/ void vfilter(int unused) { register line *addr; register int cnt; char *oglobp; short d; #ifdef BIT8 cell cuxb[UXBSIZE + 2]; #endif if ((cnt = xdw()) < 0) return; if (vglobp) #ifdef BIT8 vglobp = cuxb; #else vglobp = uxb; #endif if (readecho('!')) return; oglobp = globp; globp = genbuf + 1; d = peekc; ungetchar(0); CATCH fixech(); unix0(0); #ifdef BIT8 str2cell(cuxb, uxb); #endif ONERR splitw = 0; ungetchar(d); vrepaint(cursor); globp = oglobp; return; ENDCATCH ungetchar(d); globp = oglobp; addr = dot; CATCH vgoto(WECHO, 0); flusho(); vremote(cnt, filter, 2); ONERR vdirty(0, TLINES); ENDCATCH if (dot == zero && dol > zero) dot = one; splitw = 0; notenam = ""; /* * BUG: we shouldn't be depending on what undap2 and undap1 are, * since we may be inside a macro. What's really wanted is the * number of lines we read from the filter. However, the mistake * will be an overestimate so it only results in extra work, * it shouldn't cause any real screwups. */ vreplace(vcline, cnt, undap2 - undap1); dot = addr; if (dot > dol) { dot--; vcline--; } vrepaint(NOSTR); }
int compile(int eof, int oknl) { register int c; register char *ep; char *lastep; char bracket[NBRA], *bracketp, *rhsp; int cclcnt; if (isalpha(eof) || isdigit(eof)) error("Re delimiter must not be letter or digit|Regular expressions cannot be delimited by letters or digits"); lastep = ep = expbuf; c = ex_getchar(); if (eof == '\\') switch (c) { case '/': case '?': if (scanre.Expbuf[0] == 0) error("No previous scan re|No previous scanning regular expression"); resre(scanre); return (c); case '&': if (subre.Expbuf[0] == 0) error("No previous substitute re|No previous substitute regular expression"); resre(subre); return (c); default: error("Badly formed re|Regular expression \\ must be followed by / or ?"); } if (c == eof || c == '\n' || c == EOF) { if (*ep == 0) error("No previous re|No previous regular expression"); if (c == '\n' && oknl == 0) error("Missing closing delimiter@for regular expression"); if (c != eof) ungetchar(c); return (eof); } bracketp = bracket; nbra = 0; circfl = 0; if (c == '^') { c = ex_getchar(); circfl++; } ungetchar(c); for (;;) { if (ep >= &expbuf[ESIZE - 2]) complex: cerror("Re too complex|Regular expression too complicated"); c = ex_getchar(); if (c == eof || c == EOF) { if (bracketp != bracket) cerror("Unmatched \\(|More \\('s than \\)'s in regular expression"); *ep++ = EX_CEOF; if (c == EOF) ungetchar(c); return (eof); } if (value(MAGIC)) { if (c != '*' || ep == expbuf) lastep = ep; } else if (c != '\\' || peekchar() != '*' || ep == expbuf) lastep = ep; switch (c) { case '\\': if (!intag) c = ex_getchar(); switch (c) { case '(': if (nbra >= NBRA) cerror("Awash in \\('s!|Too many \\('d subexressions in a regular expression"); *bracketp++ = nbra; *ep++ = CBRA; *ep++ = nbra++; continue; case ')': if (bracketp <= bracket) cerror("Extra \\)|More \\)'s than \\('s in regular expression"); *ep++ = CKET; *ep++ = *--bracketp; continue; case '<': *ep++ = CBRC; continue; case '>': *ep++ = CLET; continue; } if (value(MAGIC) == 0 && !intag) magic: switch (c) { case '.': *ep++ = CDOT; continue; case '~': rhsp = rhsbuf; while (*rhsp) { if (*rhsp & RE_QUOTE) { c = *rhsp & TRIM; if (c == '&') error("Replacement pattern contains &@- cannot use in re"); if (c >= '1' && c <= '9') error("Replacement pattern contains \\d@- cannot use in re"); } if (ep >= &expbuf[ESIZE-2]) goto complex; *ep++ = CCHR; *ep++ = *rhsp++ & TRIM; } continue; case '*': if (ep == expbuf) break; if (*lastep == CBRA || *lastep == CKET) cerror("Illegal *|Can't * a \\( ... \\) in regular expression"); if (*lastep == CCHR && (lastep[1] & RE_QUOTE)) cerror("Illegal *|Can't * a \\n in regular expression"); *lastep |= STAR; continue; case '[': *ep++ = CCL; *ep++ = 0; cclcnt = 1; c = ex_getchar(); if (c == '^') { c = ex_getchar(); ep[-2] = NCCL; } if (c == ']') cerror("Bad character class|Empty character class '[]' or '[^]' cannot match"); while (c != ']') { if (c == '\\' && any(peekchar(), "]-^\\")) c = ex_getchar() | RE_QUOTE; if (c == '\n' || c == EOF) cerror("Missing ]"); *ep++ = c; cclcnt++; if (ep >= &expbuf[ESIZE]) goto complex; c = ex_getchar(); } lastep[1] = cclcnt; continue; } if (c == EOF) { ungetchar(EOF); c = '\\'; goto defchar; } *ep++ = CCHR; if (c == '\n') cerror("No newlines in re's|Can't escape newlines into regular expressions"); /* if (c < '1' || c > NBRA + '1') { */ *ep++ = c; continue; /* } c -= '1'; if (c >= nbra) cerror("Bad \\n|\\n in regular expression with n greater than the number of \\('s"); *ep++ = c | RE_QUOTE; continue; */ case '\n': if (oknl) { ungetchar(c); *ep++ = EX_CEOF; return (eof); } cerror("Badly formed re|Missing closing delimiter for regular expression"); case '$': if (peekchar() == eof || peekchar() == EOF || (oknl && peekchar() == '\n')) { *ep++ = CDOL; continue; } goto defchar; case '.': case '~': case '*': case '[': if (value(MAGIC) && !intag) goto magic; defchar: default: *ep++ = CCHR; *ep++ = c; continue; } } }
// We've just read an '&'; look for an entity reference // name, and if found, return translated char. // if there is a complete entity name but it isn't known, // back up to just past the '&' and return '&'. // If the entity can't be completed in the current buffer, back up // to the '&' and return -1. static int ampersand(TokenSource* ts) { int savei; int c; int fnd; int ans; int v; int k; Rune buf[25]; savei = ts->i; c = getchar(ts); fnd = 0; ans = -1; if(c == '#') { c = getchar(ts); v = 0; if(c == 'X' || c == 'x') for(c = getchar(ts); c < 256; c = getchar(ts)) if(c >= '0' && c <= '9') v = v*16+c-'0'; else if(c >= 'A' && c<= 'F') v = v*16+c-'A'+10; else if(c >= 'a' && c <= 'f') v = v*16+c-'a'+10; else break; else while(c >= 0) { if(!(c < 256 && isdigit(c))) break; v = v*10 + c - 48; c = getchar(ts); } if(c >= 0) { if(!(c == ';' || c == '\n' || c == '\r')) ungetchar(ts, c); c = v; if(c == 160) c = 160; if(c >= Winstart && c <= Winend) { c = winchars[c - Winstart]; } ans = c; fnd = 1; } } else if(c < 256 && isalpha(c)) { buf[0] = c; k = 1; while(1) { c = getchar(ts); if(c < 0) break; if(c < 256 && (isalpha(c) || isdigit(c))) { if(k < nelem(buf)-1) buf[k++] = c; } else { if(!(c == ';' || c == '\n' || c == '\r')) ungetchar(ts, c); break; } } if(c >= 256 || c != '=' && !(isalpha(c) || isdigit(c))) fnd = _lookup(chartab, NCHARTAB, buf, k, &ans); } if(!fnd) { backup(ts, savei); ans = '&'; } return ans; }
/* * Main procedure. Process arguments and then * transfer control to the main command processing loop * in the routine commands. We are entered as either "ex", "edit" or "vi" * and the distinction is made here. Actually, we are "vi" if * there is a 'v' in our name, and "edit" if there is a 'd' in our * name. For edit we just diddle options; for vi we actually * force an early visual command, setting the external initev so * the q command in visual doesn't give command mode. */ int main(int ac, char **av) { #if 0 char *erpath = EXSTRINGS; #endif register char *cp; register int c; bool recov = 0; bool ivis = any('v', av[0]); bool itag = 0; bool fast = 0; #ifdef TRACE register char *tracef; #endif /* * Immediately grab the tty modes so that we wont * get messed up if an interrupt comes in quickly. */ gTTY(1); normf = tty; #if 0 /* * For debugging take files out of . if name is a.out. * If a 'd' in our name, then set options for edit. */ if (av[0][0] == 'a') erpath += 9; #endif if (ivis) { options[MAGIC].odefault = value(MAGIC) = 0; options[BEAUTIFY].odefault = value(BEAUTIFY) = 1; } else if (any('d', av[0])) { value(OPEN) = 0; value(REPORT) = 1; value(MAGIC) = 0; } /* * Open the error message file. */ draino(); #if 0 erfile = open(erpath, 0); if (erfile < 0) { flush(); exit(1); } #endif pstop(); /* * Initialize interrupt handling. */ oldhup = signal(SIGHUP, SIG_IGN); if (oldhup == SIG_DFL) signal(SIGHUP, onhup); oldquit = signal(SIGQUIT, SIG_IGN); ruptible = signal(SIGINT, SIG_IGN) == SIG_DFL; if (signal(SIGTERM, SIG_IGN) == SIG_DFL) signal(SIGTERM, onhup); /* * Initialize end of core pointers. * Normally we avoid breaking back to fendcore after each * file since this can be expensive (much core-core copying). * If your system can scatter load processes you could do * this as ed does, saving a little core, but it will probably * not often make much difference. */ #ifdef UNIX_SBRK fendcore = (line *) sbrk(0); endcore = fendcore - 2; #else # define LINELIMIT 0x8000 fendcore = malloc(LINELIMIT * sizeof(line *)); endcore = fendcore + LINELIMIT - 1; #endif /* * Process flag arguments. */ ac--, av++; while (ac && av[0][0] == '-') { c = av[0][1]; if (c == 0) { hush = 1; value(AUTOPRINT) = 0; fast++; } else switch (c) { #ifdef TRACE case 'T': if (av[0][2] == 0) tracef = "trace"; else { tracef = tttrace; tracef[8] = av[0][2]; if (tracef[8]) tracef[9] = av[0][3]; else tracef[9] = 0; } trace = fopen(tracef, "w"); if (trace == NULL) ex_printf("Trace create error\n"); setbuf(trace, tracbuf); break; #endif #ifdef LISP case 'l': value(LISP) = 1; value(SHOWMATCH) = 1; break; #endif case 'r': recov++; break; case 't': if (ac > 1 && av[1][0] != '-') { ac--, av++; itag = 1; /* BUG: should check for too long tag. */ CP(lasttag, av[0]); } break; case 'v': globp = ""; ivis = 1; break; default: smerror("Unknown option %s\n", av[0]); break; } ac--, av++; } if (ac && av[0][0] == '+') { firstln = getn(av[0] + 1); if (firstln == 0) firstln = 20000; ac--, av++; } /* * If we are doing a recover and no filename * was given, then execute an exrecover command with * the -r option to type out the list of saved file names. * Otherwise set the remembered file name to the first argument * file name so the "recover" initial command will find it. */ if (recov) { if (ac == 0) { die++; setrupt(); execl(EXRECOVER, "exrecover", "-r", NULL); filioerr(EXRECOVER); exit(1); } CP(savedfile, *av); av++, ac--; } /* * Initialize the argument list. */ argv0 = av; argc0 = ac; args0 = av[0]; erewind(); /* * Initialize a temporary file (buffer) and * set up terminal environment. Read user startup commands. */ init(); if (setexit() == 0) { setrupt(); intty = isatty(0); if (fast || !intty) setterm("dumb"); else { gettmode(); if ((cp = getenv("TERM")) != 0) setterm(cp); if ((cp = getenv("HOME")) != 0) source(strcat(strcpy(genbuf, cp), "/.exrc"), 1); } } /* * Initial processing. Handle tag, recover, and file argument * implied next commands. If going in as 'vi', then don't do * anything, just set initev so we will do it later (from within * visual). */ if (setexit() == 0) { if (recov) globp = "recover"; else if (itag) globp = ivis ? "tag" : "tag|p"; else if (argc) globp = "next"; if (ivis) initev = globp; else if (globp) { inglobal = 1; commands(1, 1); inglobal = 0; } } /* * Vi command... go into visual. * Strange... everything in vi usually happens * before we ever "start". */ if (ivis) { /* * Don't have to be upward compatible with stupidity * of starting editing at line $. */ if (dol > zero) dot = one; globp = "visual"; if (setexit() == 0) commands(1, 1); } /* * Clear out trash in state accumulated by startup, * and then do the main command loop for a normal edit. * If you quit out of a 'vi' command by doing Q or ^\, * you also fall through to here. */ ungetchar(0); globp = 0; initev = 0; setlastchar('\n'); setexit(); commands(0, 0); cleanup(1); return 0; }
/* * Parse an address. * Just about any sequence of address characters is legal. * * If you are tricky you can use this routine and the = command * to do simple addition and subtraction of cardinals less * than the number of lines in the file. */ line * address(char *inputline) { register line *addr; register int offset, c; short lastsign; bigmove = 0; lastsign = 0; offset = 0; addr = 0; for (;;) { if (isdigit(peekcd())) { if (addr == 0) { addr = zero; bigmove = 1; } loc1 = 0; addr += offset; offset = getnum(); if (lastsign >= 0) addr += offset; else addr -= offset; lastsign = 0; offset = 0; } switch (c = getcd()) { case '?': case '/': case '$': case '\'': case '\\': bigmove++; case '.': if (addr || offset) error("Badly formed address"); } offset += lastsign; lastsign = 0; switch (c) { case ' ': case '\t': continue; case '+': lastsign = 1; if (addr == 0) addr = dot; continue; case '^': case '-': lastsign = -1; if (addr == 0) addr = dot; continue; case '\\': case '?': case '/': c = compile(c, 1); notempty(); savere(scanre); addr = dot; if (inputline && execute(0, dot)) { if (c == '/') { while (loc1 <= inputline) { if (loc1 == loc2) loc2++; if (!execute(1, NULL)) goto nope; } break; } else if (loc1 < inputline) { char *last; doques: do { last = loc1; if (loc1 == loc2) loc2++; if (!execute(1, NULL)) break; } while (loc1 < inputline); loc1 = last; break; } } nope: for (;;) { if (c == '/') { addr++; if (addr > dol) { if (value(WRAPSCAN) == 0) error("No match to BOTTOM|Address search hit BOTTOM without matching pattern"); addr = zero; } } else { addr--; if (addr < zero) { if (value(WRAPSCAN) == 0) error("No match to TOP|Address search hit TOP without matching pattern"); addr = dol; } } if (execute(0, addr)) { if (inputline && c == '?') { inputline = &linebuf[LBSIZE]; goto doques; } break; } if (addr == dot) error("Fail|Pattern not found"); } continue; case '$': addr = dol; continue; case '.': addr = dot; continue; case '\'': c = markreg(ex_getchar()); if (c == 0) error("Marks are ' and a-z"); addr = getmark(c); if (addr == 0) error("Undefined mark@referenced"); break; default: ungetchar(c); if (offset) { if (addr == 0) addr = dot; addr += offset; loc1 = 0; } if (addr == 0) { bigmove = 0; return (0); } if (addr != zero) notempty(); addr += lastsign; if (addr < zero) error("Negative address@- first buffer line is 1"); if (addr > dol) error("Not that many lines@in buffer"); return (addr); } } }