/* * get next character stripping escaped newlines * the flag specifies whether escaped newlines are to be elided or * replaced with a blank. */ int nextrune(Biobuf *bp, int elide) { int c, c2; static int savec; if(savec){ c = savec; savec = 0; return c; } for (;;) { c = Bgetrune(bp); if (c == '\\') { c2 = Bgetrune(bp); if(c2 == '\r'){ savec = c2; c2 = Bgetrune(bp); } if (c2 == '\n') { savec = 0; mkinline++; if (elide) continue; return ' '; } Bungetrune(bp); } if (c == '\n') mkinline++; return c; } }
int getrune(void) { Rune r; int c; top: if(istack == nil) return -1; if(istack->nunget) return istack->unget[--istack->nunget]; else if(istack->p){ if(istack->p >= istack->ep){ popinput(); goto top; } r = *istack->p++; }else if(istack->b){ if((c = Bgetrune(istack->b)) < 0){ popinput(); goto top; } r = c; }else{ r = 0; sysfatal("getrune - can't happen"); } if(r == '\n') istack->lineno++; return r; }
/* * Input an escaped token. Possible escape chars are single-quote, * double-quote and backslash. Only the first is a valid escape for * rc; the others are just inserted into the receiving buffer. */ int escapetoken(Biobuf *bp, Bufblock *buf, int preserve, int esc) { int c, line; if(esc != '\'') return 1; line = mkinline; while((c = nextrune(bp, 0)) > 0){ if(c == '\''){ if(preserve) rinsert(buf, c); c = Bgetrune(bp); if (c < 0) break; if(c != '\''){ Bungetrune(bp); return 1; } } rinsert(buf, c); } SYNERR(line); fprint(2, "missing closing %c\n", esc); return 0; }
int getfile(void) { int c; Rune *lp; lp = linebuf; do { c = Bgetrune(&iobuf); if(c < 0) { if(lp > linebuf) { putst("'\\n' appended"); c = '\n'; } else return EOF; } if(lp >= &linebuf[LBSIZE]) { lastc = '\n'; error(Q); } *lp++ = c; count++; } while(c != '\n'); lp[-1] = 0; return 0; }
int nextc(Biobuf *in) { prevc = thisc; thisc = Bgetrune(in); return thisc; }
static char * rbody(Biobuf *in) { Bufblock *buf; int r, lastr; char *p; lastr = '\n'; buf = newbuf(); for(;;) { r = Bgetrune(in); if (r < 0) break; if (lastr == '\n') { if (r == '#') rinsert(buf, r); else if (r != ' ' && r != '\t') { Bungetrune(in); break; } } else rinsert(buf, r); lastr = r; if (r == '\n') mkinline++; } insert(buf, 0); p = strdup(buf->start); freebuf(buf); return p; }
/* * Defer message about failure to open file to prevent messing up * alignment of page with tear perforations or form markers. * Treat empty file as special case and report as diagnostic. */ Biobuf* mustopen(char *s, Fils *f) { char *tmp; if(*s == '\0') { f->f_name = STDINNAME(); f->f_f = malloc(sizeof(Biobuf)); if(f->f_f == 0) cerror("no memory"); Binit(f->f_f, 0, OREAD); } else if((f->f_f = Bopen(f->f_name = s, OREAD)) == 0) { tmp = ffiler(f->f_name); s = strcpy((char*)getspace(strlen(tmp) + 1), tmp); free(tmp); } if(f->f_f != 0) { if((f->f_nextc = Bgetrune(f->f_f)) >= 0 || Multi == 'm') return f->f_f; sprint(s = (char*)getspace(strlen(f->f_name) + 1 + EMPTY), "%s -- empty file\n", f->f_name); Bterm(f->f_f); } error = 1; cerror(s); fprint(2, "\n"); return 0; }
void draw(Biobufhdr *Bp) { int r, x1, y1, x2, y2, i; int d1, d2; drawflag = TRUE; r = Bgetrune(Bp); switch(r) { case 'l': if (Bgetfield(Bp, 'd', &x1, 0)<=0 || Bgetfield(Bp, 'd', &y1, 0)<=0 || Bgetfield(Bp, 'r', &i, 0)<=0) error(FATAL, "draw line function, destination coordinates not found.\n"); endstring(); if (pageon()) Bprint(Bstdout, "%d %d %d %d Dl\n", hpos, vpos, hpos+x1, vpos+y1); hpos += x1; vpos += y1; break; case 'c': if (Bgetfield(Bp, 'd', &d1, 0)<=0) error(FATAL, "draw circle function, diameter coordinates not found.\n"); endstring(); if (pageon()) Bprint(Bstdout, "%d %d %d %d De\n", hpos, vpos, d1, d1); hpos += d1; break; case 'e': if (Bgetfield(Bp, 'd', &d1, 0)<=0 || Bgetfield(Bp, 'd', &d2, 0)<=0) error(FATAL, "draw ellipse function, diameter coordinates not found.\n"); endstring(); if (pageon()) Bprint(Bstdout, "%d %d %d %d De\n", hpos, vpos, d1, d2); hpos += d1; break; case 'a': if (Bgetfield(Bp, 'd', &x1, 0)<=0 || Bgetfield(Bp, 'd', &y1, 0)<=0 || Bgetfield(Bp, 'd', &x2, 0)<=0 || Bgetfield(Bp, 'd', &y2, 0)<=0) error(FATAL, "draw arc function, coordinates not found.\n"); endstring(); if (pageon()) Bprint(Bstdout, "%d %d %d %d %d %d Da\n", hpos, vpos, x1, y1, x2, y2); hpos += x1 + x2; vpos += y1 + y2; break; case 'q': drawspline(Bp, 1); break; case '~': drawspline(Bp, 2); break; default: error(FATAL, "unknown draw function <%c>\n", r); break; } }
/* * assemble a back-quoted shell command into a buffer */ static int bquote(Biobuf *bp, Bufblock *buf) { int c, line, term, depth; int start; line = mkinline; while((c = Bgetrune(bp)) == ' ' || c == '\t') ; if(c == '{'){ term = '}'; /* rc style */ while((c = Bgetrune(bp)) == ' ' || c == '\t') ; } else term = '`'; /* sh style */ depth = 1; start = buf->current-buf->start; for(;c > 0; c = nextrune(bp, 0)){ if(c == '{' && term == '}') depth++; if(c == term && --depth == 0){ insert(buf, '\n'); insert(buf,0); buf->current = buf->start+start; execinit(); execsh(0, buf->current, buf, envy); return 1; } if(c == '\n') break; if(c == '\'' || c == '"' || c == '\\'){ insert(buf, c); if(!escapetoken(bp, buf, 1, c)) return 0; continue; } rinsert(buf, c); } SYNERR(line); fprint(2, "missing closing %c after `\n", term); return 0; }
void main(void) { int c; Binit(&bin, 0, OREAD); while((c = Bgetrune(&bin)) >= 0) print("0x%ux\n", c); exits(0); }
int fC1(void) { c = Bgetrune(&(infile->Biobufhdr)); if(c == Beof) return eof(); if(c == '\n') linect++; return c; }
int fC1(void) { c = Bgetrune(infile); if(c == Beof) return eof(); if(c == '\n') linect++; return c; }
int locate(void) { vlong top, bot, mid; long c; int n; bot = 0; top = Bseek(dfile, 0, 2); for(;;) { mid = (top+bot) / 2; Bseek(dfile, mid, 0); do c = Bgetrune(dfile); while(c>=0 && c!='\n'); mid = Boffset(dfile); if(!getword(dfile, entry, sizeof(entry)/sizeof(entry[0]))) break; rcanon(entry, word); n = compare(key, word); switch(n) { case -2: case -1: case 0: if(top <= mid) break; top = mid; continue; case 1: case 2: bot = mid; continue; } break; } Bseek(dfile, bot, 0); while(getword(dfile, entry, sizeof(entry)/sizeof(entry[0]))) { rcanon(entry, word); n = compare(key, word); switch(n) { case -2: return 0; case -1: if(exact) return 0; case 0: return 1; case 1: case 2: continue; } } return 0; }
int moreinput(Biobufhdr *fin) { int c; if ((c=Bgetrune(fin)) != Beof) { if (c == ' ' || c == '\t') return 1; /* skip a space */ Bungetrune(fin); } return 0; }
int fC(void) { c = Bgetrune(&(infile->Biobufhdr)); if(c < 0) return eof(); if(c == ldelim && filesp == files) return skeqn(); if(c == '\n') linect++; return c; }
/* * get next character stripping escaped newlines * the flag specifies whether escaped newlines are to be elided or * replaced with a blank. */ int nextrune(Biobuf *bp, int elide) { int c; for (;;) { c = Bgetrune(bp); if (c == '\\') { if (Bgetrune(bp) == '\n') { mkinline++; if (elide) continue; return ' '; } Bungetrune(bp); } if (c == '\n') mkinline++; return c; } }
int get(int colno) { static int peekc = 0; Colp p; Fils *q; long c; if(peekc) { peekc = 0; c = Etabc; } else if(Buffer) { p = &Colpts[colno]; if(p->c_ptr >= (p+1)->c_ptr0) c = -1; else if((c = *p->c_ptr) != -1) p->c_ptr++; } else if((c = (q = &Files[Multi == 'a'? 0: colno])->f_nextc) == -1) { for(q = &Files[Nfiles]; --q >= Files && q->f_nextc == -1;) ; if(q >= Files) c = '\n'; } else q->f_nextc = Bgetrune(q->f_f); if(Etabn != 0 && c == Etabc) { Inpos++; peekc = ETABS; c = ' '; } else if(ISPRINT(c)) Inpos++; else switch(c) { case '\b': case ESC: if(Inpos > 0) Inpos--; break; case '\f': if(Ncols == 1) break; c = '\n'; case '\n': case '\r': Inpos = 0; } return C = c; }
/* * Input an escaped token. Possible escape chars are single-quote, * double-quote and backslash. */ static int shescapetoken(Biobuf *bp, Bufblock *buf, int preserve, int esc) { int c, line; if(esc == '\\') { c = Bgetrune(bp); if(c == '\r') c = Bgetrune(bp); if (c == '\n') mkinline++; rinsert(buf, c); return 1; } line = mkinline; while((c = nextrune(bp, 0)) >= 0){ if(c == esc){ if(preserve) rinsert(buf, c); return 1; } if(c == '\\') { rinsert(buf, c); c = Bgetrune(bp); if(c == '\r') c = Bgetrune(bp); if (c < 0) break; if (c == '\n') mkinline++; } rinsert(buf, c); } SYNERR(line); fprint(2, "missing closing %c\n", esc); return 0; }
Rune * getline(Rune *buf, long n, Biobufhdr *fin) { int i, c; i = 0; do { while (i < n-1 && (c=Bgetrune(fin)) != Beof) { buf[i++] = c; if (c == '\n') break; } if (c != Beof && c != '\n') while ((c=Bgetrune(fin)) != Beof && c != '\n') ; } while (c != Beof && moreinput(fin)); if (i == 0) return nil; else { buf[i-1] = '\0'; return buf; } }
void freq(int f, char *s) { Biobuf bin; long c, i; memset(count, 0, sizeof(count)); Binit(&bin, f, OREAD); if(flag & Frune) { for(;;) { c = Bgetrune(&bin); if(c < 0) break; count[c]++; } } else { for(;;) { c = Bgetc(&bin); if(c < 0) break; count[c]++; } } Bterm(&bin); if(c != Beof) fprint(2, "freq: read error on %s\n", s); for(i=0; i<nelem(count); i++) { if(count[i] == 0) continue; if(flag & Fdec) Bprint(&bout, "%3ld ", i); if(flag & Foct) Bprint(&bout, "%.3lo ", i); if(flag & Fhex) Bprint(&bout, "%.2lx ", i); if(flag & Fchar) { if(i <= 0x20 || i >= 0x7f && i < 0xa0 || i > 0xff && !(flag & Frune)) Bprint(&bout, "- "); else Bprint(&bout, "%C ", (int)i); } Bprint(&bout, "%8ld\n", count[i]); } Bflush(&bout); }
int getchr(void) { if(lastc = peekc) { peekc = 0; return lastc; } if(globp) { if((lastc=*globp++) != 0) return lastc; globp = 0; return EOF; } lastc = Bgetrune(&bcons); return lastc; }
int main(void) { Rune r; char *s; int n = 0; Biobuf *i = Bfdopen(0,O_RDONLY); print("enum codepoints {"); while((r=Bgetrune(i))>0 && r != '\n') { s = Brdline(i,'\n'); if(!Blinelen(i)) break; s[Blinelen(i)-1] = '\0'; *s = 'U'; print("%s\n\t%-8s = 0x%X",n?",":"", s,r); n = 1; } print("\n};\n"); Bterm(i); return 0; }
void readbuf(int fd) { int lastwascolon = 0; long c; int linesiz = 0; Binit(&bin, fd, OREAD); do{ if(nchars++ >= nalloc) morechars(); *cbufp++ = c = Bgetrune(&bin); linesiz++; if(c == '\t') { cbufp[-1] = L' '; while(linesiz%TAB != 0) { if(nchars++ >= nalloc) morechars(); *cbufp++ = L' '; linesiz++; } } if(colonflag && c == ':') lastwascolon++; else if(lastwascolon){ if(c == '\n'){ --nchars; /* skip newline */ *cbufp = L'\0'; while(nchars > 0 && cbuf[--nchars] != '\n') ; if(nchars) nchars++; columnate(); if (nchars) Bputc(&bout, '\n'); Bprint(&bout, "%S", cbuf+nchars); nchars = 0; cbufp = cbuf; } lastwascolon = 0; } if(c == '\n') linesiz = 0; }while(c >= 0); }
void stringit(char *str) { long posn, start; int cnt = 0; long c; Rune buf[BUFSIZE]; if ((fin = Bopen(str, OREAD)) == 0) { perror("open"); return; } start = 0; posn = Boffset(fin); while((c = Bgetrune(fin)) >= 0) { if(isprint(c)) { if(start == 0) start = posn; buf[cnt++] = c; if(cnt == BUFSIZE-1) { buf[cnt] = 0; Bprint(&fout, "%8ld: %S ...\n", start, buf); start = 0; cnt = 0; } } else { if(cnt >= minspan) { buf[cnt] = 0; Bprint(&fout, "%8ld: %S\n", start, buf); } start = 0; cnt = 0; } posn = Boffset(fin); } if(cnt >= minspan){ buf[cnt] = 0; Bprint(&fout, "%8ld: %S\n", start, buf); } Bterm(fin); }
void nexbuf(void) { Rune *s = Buffer; Colp p = Colpts; int j, c, bline = 0; for(;;) { p->c_ptr0 = p->c_ptr = s; if(p == &Colpts[Ncols]) return; (p++)->c_lno = Lnumb + bline; for(j = (Len - Margin)/Dblspace; --j >= 0; bline++) for(Inpos = 0;;) { if((c = Bgetrune(Files->f_f)) == -1) { for(*s = -1; p <= &Colpts[Ncols]; p++) p->c_ptr0 = p->c_ptr = s; if(Balance) balance(bline); return; } if(ISPRINT(c)) Inpos++; if(Inpos <= Colw || c == '\n') { *s = c; if(++s >= Bufend) die("page-buffer overflow"); } if(c == '\n') break; switch(c) { case '\b': if(Inpos == 0) s--; case ESC: if(Inpos > 0) Inpos--; } } } }
void main(void) { int c, n; char str[100], *args[10]; int jfont, curfont; if(initdraw(0, fontfile, 0) < 0){ fprint(2, "mnihongo: can't initialize display: %r\n"); exits("open"); } Binit(&bin, 0, OREAD); Binit(&bout, 1, OWRITE); jfont = -1; curfont = 1; while ((c = Bgetc(&bin)) >= 0) { switch (c) { case '\n': /* when input is text */ case ' ': case '\0': /* occasional noise creeps in */ putchar(c); break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': /* two motion digits plus a character */ putchar(c); /* digit 1 */ n = (c-'0')*10; c = Bgetc(&bin); putchar(c); /* digit 2 */ n += c - '0'; hmot(n); putchar(Bgetc(&bin)); /* char itself */ break; case 'c': /* single character */ c = Bgetrune(&bin); if(c==' ') /* why does this happen? it's troff - bwk */ break; else if(jfont == curfont){ Bungetrune(&bin); Bgetstr(&bin, str); kanji(str); }else{ putchar('c'); putchar(c); } break; case 'C': Bgetstr(&bin, str); Bprint(&bout, "C%s", str); break; case 'f': Bgetstr(&bin, str); curfont = atoi(str); if(curfont < 0 || curfont > 20) curfont = 1; /* sanity */ Bprint(&bout, "%c%s", c, str); break; case 'N': /* absolute character number */ case 's': case 'p': /* new page */ Bgetint(&bin, &n); Bprint(&bout, "%c%d", c, n); break; case 'H': /* absolute horizontal motion */ Bgetint(&bin, &n); Bprint(&bout, "%c%d", c, n); hgoto(n); break; case 'h': /* relative horizontal motion */ Bgetint(&bin, &n); Bprint(&bout, "%c%d", c, n); hmot(n); break; case 'V': Bgetint(&bin, &n); Bprint(&bout, "%c%d", c, n); vgoto(n); break; case 'v': Bgetint(&bin, &n); Bprint(&bout, "%c%d", c, n); vmot(n); break; case 'w': /* word space */ putchar(c); break; case 'x': /* device control */ Bgetline(&bin, str); Bprint(&bout, "%c%s", c, str); if(tokenize(str, args, 10)>2 && args[0][0]=='f' && ('0'<=args[1][0] && args[1][0]<='9')){ if(strncmp(args[2], "Jp", 2) == 0) jfont = atoi(args[1]); else if(atoi(args[1]) == jfont) jfont = -1; } break; case 'D': /* draw function */ case 'n': /* end of line */ case '#': /* comment */ Bgetline(&bin, str); Bprint(&bout, "%c%s", c, str); break; default: fprint(2, "mnihongo: unknown input character %o %c\n", c, c); exits("error"); } } }
void tune_in(int fd, long *x, struct convert *out) { Biobuf b; Rune rbuf[N]; Rune *r, *er, tr; int c, i; USED(x); r = rbuf; er = rbuf+N-3; Binit(&b, fd, OREAD); while((c = Bgetrune(&b)) != Beof){ ninput += b.runesize; if(r >= er){ OUT(out, rbuf, r-rbuf); r = rbuf; } if(c>=0xe210/**/ && c <= 0xe38c/**/ && (i = c%16) < nelem(t2)){ if(c >= 0xe380/**/){ *r++ = 0x0b95/*க*/; *r++ = 0x0bcd/*்*/; *r++ = 0x0bb7/*ஷ*/; }else *r++ = findbytune(t3, nelem(t3), c-i+1); if(i != 1) *r++ = t2[i]; }else if((tr = findbytune(t1, nelem(t1), c)) != Runeerror) *r++ = tr; else switch(c){ case 0xe3d0/**/: *r++ = 0x0ba3/*ண*/; *r++ = 0x0bbe/*ா*/; break; case 0xe3d1/**/: *r++ = 0x0bb1/*ற*/; *r++ = 0x0bbe/*ா*/; break; case 0xe3d2/**/: *r++ = 0x0ba9/*ன*/; *r++ = 0x0bbe/*ா*/; break; case 0xe3d4/**/: *r++ = 0x0ba3/*ண*/; *r++ = 0x0bc8/*ை*/; break; case 0xe3d5/**/: *r++ = 0x0bb2/*ல*/; *r++ = 0x0bc8/*ை*/; break; case 0xe3d6/**/: *r++ = 0x0bb3/*ள*/; *r++ = 0x0bc8/*ை*/; break; case 0xe3d7/**/: *r++ = 0x0ba9/*ன*/; *r++ = 0x0bc8/*ை*/; break; case 0xe38d/**/: *r++ = 0x0bb6/*ஶ*/; *r++ = 0x0bcd/*்*/; *r++ = 0x0bb0/*ர*/; *r++ = 0x0bc0/*ீ*/; break; default: if(c >= 0xe200 && c <= 0xe3ff){ if(squawk) EPR "%s: rune 0x%x not in output cs\n", argv0, c); nerrors++; if(clean) break; c = BADMAP; } *r++ = c; break; } } if(r > rbuf) OUT(out, rbuf, r-rbuf); OUT(out, rbuf, 0); }
void conv(Biobuf *Bp) { long n; int r; char special[10]; int save; inputlineno = 1; if (debug) Bprint(Bstderr, "conv(Biobufhdr *Bp=0x%x)\n", Bp); while ((r = Bgetrune(Bp)) >= 0) { /* Bprint(Bstderr, "r=<%c>,0x%x\n", r, r); */ /* Bflush(Bstderr); */ switch (r) { case 's': /* set point size */ Bgetfield(Bp, 'd', &fontsize, 0); break; case 'f': /* set font to postion */ Bgetfield(Bp, 'd', &fontpos, 0); save = inputlineno; settrfont(); inputlineno = save; /* ugh */ break; case 'c': /* print rune */ r = Bgetrune(Bp); runeout(r); break; case 'C': /* print special character */ Bgetfield(Bp, 's', special, 10); specialout(special); break; case 'N': /* print character with numeric value from current font */ Bgetfield(Bp, 'd', &n, 0); break; case 'H': /* go to absolute horizontal position */ Bgetfield(Bp, 'd', &n, 0); hgoto(n); break; case 'V': /* go to absolute vertical position */ Bgetfield(Bp, 'd', &n, 0); vgoto(n); break; case 'h': /* go to relative horizontal position */ Bgetfield(Bp, 'd', &n, 0); hmot(n); break; case 'v': /* go to relative vertical position */ Bgetfield(Bp, 'd', &n, 0); vmot(n); break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': /* move right nn units, then print character c */ n = (r - '0') * 10; r = Bgetrune(Bp); if (r < 0) error(FATAL, "EOF or error reading input\n"); else if (r < '0' || r > '9') error(FATAL, "integer expected\n"); n += r - '0'; r = Bgetrune(Bp); hmot(n); runeout(r); break; case 'p': /* begin page */ Bgetfield(Bp, 'd', &n, 0); endpage(); startpage(); break; case 'n': /* end of line (information only 'b a' follows) */ Brdline(Bp, '\n'); /* toss rest of line */ inputlineno++; break; case 'w': /* paddable word space (information only) */ break; case 'D': /* graphics function */ draw(Bp); break; case 'x': /* device control functions */ devcntl(Bp); break; case '#': /* comment */ Brdline(Bp, '\n'); /* toss rest of line */ case '\n': inputlineno++; break; default: error(WARNING, "unknown troff function <%c>\n", r); break; } } endpage(); if (debug) Bprint(Bstderr, "r=0x%x\n", r); if (debug) Bprint(Bstderr, "leaving conv\n"); }
/* * get a character from the console */ int getc(void) { return Bgetrune(&bin); }
int getc(Biobuf *b) { cno++; return Bgetrune(&(b->Biobufhdr)); }