static void linefeed(void) { int line = 0; /* current line - based on ovmi */ /* * * Adjust our current vertical position. If we've passed the bottom of the page * or exceeded the number of lines per page, print it and go to the upper left * corner of the next page. This routine is also called from carriage() if crislf * is ON. * */ vmot(vmi); if ( lfiscr == ON ) hgoto(leftmargin); if ( linespp > 0 ) /* means something so see where we are */ line = vpos / ovmi + 1; if ( vpos > bottommargin || line > linespp ) formfeed(); } /* End of linefeed */
static void escape(void) { int ch; /* control character */ /* * * Handles special codes that are expected to follow an escape character. The * initial escape character is followed by one or two bytes. * */ switch ( ch = getc(fp_in) ) { case 'T': /* top margin */ topmargin = vpos; break; case 'L': /* bottom margin */ bottommargin = vpos; break; case 'C': /* clear top and bottom margins */ bottommargin = BOTTOMMARGIN; topmargin = TOPMARGIN; break; case '9': /* left margin */ leftmargin = hpos; break; case '0': /* right margin */ rightmargin = hpos; break; case '1': /* set horizontal tab */ htabstops[hpos/ohmi] = ON; break; case '8': /* clear horizontal tab at hpos */ htabstops[hpos/ohmi] = OFF; break; case '-': /* set vertical tab */ vtabstops[vpos/ovmi] = ON; break; case '2': /* clear all tabs */ cleartabs(); break; case '\014': /* set lines per page */ linespp = getc(fp_in); break; case '\037': /* set hmi to next byte minus 1 */ hmi = HSCALE * (getc(fp_in) - 1); break; case 'S': /* reset hmi to default */ hmi = ohmi; break; case '\011': /* move to column given by next byte */ hgoto((getc(fp_in)-1) * ohmi); break; case '?': /* do carriage return after line feed */ lfiscr = ON; break; case '!': /* don't generate carriage return */ lfiscr = OFF; break; case '5': /* forward print mode */ advance = 1; break; case '6': /* backward print mode */ advance = -1; break; case '\036': /* set vmi to next byte minus 1 */ vmi = VSCALE * (getc(fp_in) - 1); break; case '\013': /* move to line given by next byte */ vgoto((getc(fp_in)-1) * ovmi); break; case 'U': /* positive half line feed */ vmot(vmi/2); break; case 'D': /* negative half line feed */ vmot(-vmi/2); break; case '\012': /* negative line feed */ vmot(-vmi); break; case '\015': /* clear all margins */ bottommargin = BOTTOMMARGIN; topmargin = TOPMARGIN; leftmargin = BOTTOMMARGIN; rightmargin = RIGHTMARGIN; break; case 'E': /* auto underscore - use italic font */ changefont("/Courier-Oblique"); break; case 'R': /* disable auto underscore */ changefont(fontname); break; case 'O': /* bold/shadow printing */ case 'W': changefont("/Courier-Bold"); shadowprint = ON; break; case '&': /* disable bold printing */ changefont(fontname); shadowprint = OFF; break; case '/': /* ignored 2 byte escapes */ case '\\': case '<': case '>': case '%': case '=': case '.': case '4': case 'A': case 'B': case 'M': case 'N': case 'P': case 'Q': case 'X': case '\010': break; case ',': /* ignored 3 byte escapes */ case '\016': case '\021': getc(fp_in); break; case '3': /* graphics mode - should quit! */ case '7': case 'G': case 'V': case 'Y': case 'Z': error(FATAL, "graphics mode is not implemented"); break; default: error(FATAL, "missing case for escape o%o\n", ch); break; } /* End switch */ } /* End of escape */
Tchar getch(void) { int k; Tchar i, j; g0: if (ch) { i = ch; if (cbits(i) == '\n') nlflg++; ch = 0; return(i); } if (nlflg) return('\n'); i = getch0(); if (ismot(i)) return(i); k = cbits(i); if (k >= sizeof(gchtab)/sizeof(gchtab[0]) || gchtab[k] == 0) /* nothing special */ return(i); if (k != ESC) { if (k == '\n') { nlflg++; if (ip == 0) numtabp[CD].val++; /* line number */ return(k); } if (k == FLSS) { copyf++; raw++; i = getch0(); if (!fi) flss = i; copyf--; raw--; goto g0; } if (k == RPT) { setrpt(); goto g0; } if (!copyf) { if (k == 'f' && lg && !lgf) { i = getlg(i); return(i); } if (k == fc || k == tabch || k == ldrch) { if ((i = setfield(k)) == 0) goto g0; else return(i); } if (k == '\b') { i = makem(-width(' ' | chbits)); return(i); } } return(i); } k = cbits(j = getch0()); if (ismot(j)) return(j); switch (k) { case 'n': /* number register */ setn(); goto g0; case '$': /* argument indicator */ seta(); goto g0; case '*': /* string indicator */ setstr(); goto g0; case '{': /* LEFT */ i = LEFT; goto gx; case '}': /* RIGHT */ i = RIGHT; goto gx; case '"': /* comment */ while (cbits(i = getch0()) != '\n') ; if (ip == 0) numtabp[CD].val++; /* line number */ nlflg++; return(i); /* experiment: put it here instead of copy mode */ case '(': /* special char name \(xx */ case 'C': /* \C'...' */ if ((i = setch(k)) == 0) goto g0; goto gx; case ESC: /* double backslash */ i = eschar; goto gx; case 'e': /* printable version of current eschar */ i = PRESC; goto gx; case '\n': /* concealed newline */ numtabp[CD].val++; goto g0; case ' ': /* unpaddable space */ i = UNPAD; goto gx; case '\'': /* \(aa */ i = ACUTE; goto gx; case '`': /* \(ga */ i = GRAVE; goto gx; case '_': /* \(ul */ i = UNDERLINE; goto gx; case '-': /* current font minus */ i = MINUS; goto gx; case '&': /* filler */ i = FILLER; goto gx; case 'c': /* to be continued */ i = CONT; goto gx; case '!': /* transparent indicator */ i = XPAR; goto gx; case 't': /* tab */ i = '\t'; return(i); case 'a': /* leader (SOH) */ /* old: *pbp++ = LEADER; goto g0; */ i = LEADER; return i; case '%': /* ohc */ i = OHC; return(i); case 'g': /* return format of a number register */ setaf(); /* should this really be in copy mode??? */ goto g0; case '.': /* . */ i = '.'; gx: setsfbits(i, sfbits(j)); return(i); } if (copyf) { *pbp++ = j; return(eschar); } switch (k) { case 'f': /* font indicator */ setfont(0); goto g0; case 's': /* size indicator */ setps(); goto g0; case 'v': /* vert mot */ numerr.type = numerr.escarg = 0; numerr.esc = k; if (i = vmot()) { return(i); } goto g0; case 'h': /* horiz mot */ numerr.type = numerr.escarg = 0; numerr.esc = k; if (i = hmot()) return(i); goto g0; case '|': /* narrow space */ if (NROFF) goto g0; return(makem((int)(EM)/6)); case '^': /* half narrow space */ if (NROFF) goto g0; return(makem((int)(EM)/12)); case 'w': /* width function */ setwd(); goto g0; case 'p': /* spread */ spread++; goto g0; case 'N': /* absolute character number */ numerr.type = numerr.escarg = 0; numerr.esc = k; if ((i = setabs()) == 0) goto g0; return i; case 'H': /* character height */ numerr.type = numerr.escarg = 0; numerr.esc = k; return(setht()); case 'S': /* slant */ numerr.type = numerr.escarg = 0; numerr.esc = k; return(setslant()); case 'z': /* zero with char */ return(setz()); case 'l': /* hor line */ numerr.type = numerr.escarg = 0; numerr.esc = k; setline(); goto g0; case 'L': /* vert line */ numerr.type = numerr.escarg = 0; numerr.esc = k; setvline(); goto g0; case 'D': /* drawing function */ numerr.type = numerr.escarg = 0; numerr.esc = k; setdraw(); goto g0; case 'X': /* \X'...' for copy through */ setxon(); goto g0; case 'b': /* bracket */ setbra(); goto g0; case 'o': /* overstrike */ setov(); goto g0; case 'k': /* mark hor place */ if ((k = findr(getsn())) != -1) { numtabp[k].val = numtabp[HP].val; } goto g0; case '0': /* number space */ return(makem(width('0' | chbits))); case 'x': /* extra line space */ numerr.type = numerr.escarg = 0; numerr.esc = k; if (i = xlss()) return(i); goto g0; case 'u': /* half em up */ case 'r': /* full em up */ case 'd': /* half em down */ return(sethl(k)); default: return(j); } /* NOTREACHED */ }
void drawline(int dx, int dy, char *s) /* draw line from here to dx, dy using s */ { int xd, yd; float val, slope; int i, numdots; int dirmot, perp; int motincr, perpincr; int ohpos, ovpos, osize; float incrway; int itemp; /*temp. storage for value returned byint function sgn*/ osize = size; setsize(t_size(pstab[osize-1] / drawsize)); ohpos = hpos; ovpos = vpos; xd = dx / DX; yd = dy / DX; if (xd == 0) { numdots = abs (yd); numdots = min(numdots, maxdots); motincr = DX * sgn (yd); for (i = 0; i < numdots; i++) { vmot(motincr); put1(drawdot); } vgoto(ovpos + dy); setsize(osize); return; } if (yd == 0) { numdots = abs (xd); motincr = DX * sgn (xd); for (i = 0; i < numdots; i++) { hmot(motincr); put1(drawdot); } hgoto(ohpos + dx); setsize(osize); return; } if (abs (xd) > abs (yd)) { val = slope = (float) xd/yd; numdots = abs (xd); numdots = min(numdots, maxdots); dirmot = 'h'; perp = 'v'; motincr = DX * sgn (xd); perpincr = DX * sgn (yd); } else { val = slope = (float) yd/xd; numdots = abs (yd); numdots = min(numdots, maxdots); dirmot = 'v'; perp = 'h'; motincr = DX * sgn (yd); perpincr = DX * sgn (xd); } incrway = itemp = sgn ((int) slope); for (i = 0; i < numdots; i++) { val -= incrway; if (dirmot == 'h') hmot(motincr); else vmot(motincr); if (val * slope < 0) { if (perp == 'h') hmot(perpincr); else vmot(perpincr); val += slope; } put1(drawdot); } hgoto(ohpos + dx); vgoto(ovpos + dy); setsize(osize); }
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"); }
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 conv(register FILE *fp) { register int c, k; int m, n, n1, m1; char str[4096], buf[4096]; while ((c = getc(fp)) != EOF) { switch (c) { case '\n': /* when input is text */ case ' ': case 0: /* occasional noise creeps in */ break; case '{': /* push down current environment */ t_push(); break; case '}': t_pop(); 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 */ hmot((c-'0')*10 + getc(fp)-'0'); put1(getc(fp)); break; case 'c': /* single ascii character */ put1(getc(fp)); break; case 'C': sget(str, sizeof str, fp); put1s(str); break; case 't': /* straight text */ fgets(buf, sizeof(buf), fp); t_text(buf); break; case 'D': /* draw function */ fgets(buf, sizeof(buf), fp); switch (buf[0]) { case 'l': /* draw a line */ sscanf(buf+1, "%d %d", &n, &m); drawline(n, m, "."); break; case 'c': /* circle */ sscanf(buf+1, "%d", &n); drawcirc(n); break; case 'e': /* ellipse */ sscanf(buf+1, "%d %d", &m, &n); drawellip(m, n); break; case 'a': /* arc */ sscanf(buf+1, "%d %d %d %d", &n, &m, &n1, &m1); drawarc(n, m, n1, m1); break; case '~': /* wiggly line */ drawwig(buf+1); break; default: error(FATAL, "unknown drawing function %s\n", buf); break; } break; case 's': fscanf(fp, "%d", &n); if (n == -23) { float f; fscanf(fp, "%f", &f); setsize(f); } else setsize(t_size(n));/* ignore fractional sizes */ break; case 'f': sget(str, sizeof str, fp); setfont(t_font(str)); break; case 'H': /* absolute horizontal motion */ /* fscanf(fp, "%d", &n); */ while ((c = getc(fp)) == ' ') ; k = 0; do { k = 10 * k + c - '0'; } while (isdigit(c = getc(fp))); ungetc(c, fp); hgoto(k); break; case 'h': /* relative horizontal motion */ /* fscanf(fp, "%d", &n); */ while ((c = getc(fp)) == ' ') ; k = 0; do { k = 10 * k + c - '0'; } while (isdigit(c = getc(fp))); ungetc(c, fp); hmot(k); break; case 'w': /* word space */ putc(' ', stdout); break; case 'V': fscanf(fp, "%d", &n); vgoto(n); break; case 'v': fscanf(fp, "%d", &n); vmot(n); break; case 'p': /* new page */ fscanf(fp, "%d", &n); t_page(n); break; case 'n': /* end of line */ while (getc(fp) != '\n') ; t_newline(); break; case '#': /* comment */ while (getc(fp) != '\n') ; break; case 'x': /* device control */ devcntrl(fp); break; default: error(!FATAL, "unknown input character %o %c\n", c, c); done(); } } }