17063Srrh #ifndef lint 2*13262Srrh static char sccsid[] = "@(#)n1.c 4.4 06/22/83"; 37063Srrh #endif lint 47063Srrh 57171Smckusick #include "tdef.h" 6*13262Srrh #include <sys/types.h> 77063Srrh #include <sys/stat.h> 87063Srrh extern 97063Srrh #include "d.h" 107063Srrh extern 117063Srrh #include "v.h" 127063Srrh #ifdef NROFF 137063Srrh extern 147063Srrh #include "tw.h" 157063Srrh #endif 167063Srrh #include "sdef.h" 177063Srrh #include <setjmp.h> 187063Srrh jmp_buf sjbuf; 197063Srrh #include <sgtty.h> 207063Srrh /* 217063Srrh troff1.c 227063Srrh 237063Srrh consume options, initialization, main loop, 247063Srrh input routines, escape function calling 257063Srrh */ 267063Srrh 277063Srrh int inchar[LNSIZE], *pinchar = inchar; /* XXX */ 287063Srrh extern struct s *frame, *stk, *nxf; 297063Srrh extern struct s *ejl, *litlev; 307063Srrh extern filep ip; 317063Srrh extern filep offset; 327063Srrh extern filep nextb; 337063Srrh 347063Srrh 357063Srrh extern int stdi; 367063Srrh extern int waitf; 377063Srrh extern int nofeed; 387063Srrh extern int quiet; 397063Srrh extern int ptid; 407063Srrh extern int ascii; 417063Srrh extern int npn; 427063Srrh extern int xflg; 437063Srrh extern int stop; 447063Srrh extern char ibuf[IBUFSZ]; 457063Srrh extern char xbuf[IBUFSZ]; 467063Srrh extern char *ibufp; 477063Srrh extern char *xbufp; 487063Srrh extern char *eibuf; 497063Srrh extern char *xeibuf; 507063Srrh extern int cbuf[NC]; 517063Srrh extern int *cp; 527063Srrh extern int *vlist; 537063Srrh extern int nx; 547063Srrh extern int mflg; 557063Srrh extern int ch; 567063Srrh extern int pto; 577063Srrh extern int pfrom; 587063Srrh extern int cps; 597063Srrh extern int chbits; 607063Srrh extern int ibf; 617063Srrh extern int ttyod; 627063Srrh extern struct sgttyb ttys; 637063Srrh extern int iflg; 647063Srrh extern int init; 657063Srrh extern int rargc; 667063Srrh extern char **argp; 677063Srrh extern char trtab[256]; 687063Srrh extern int lgf; 697063Srrh extern int copyf; 707063Srrh extern int eschar; 717063Srrh extern int ch0; 727063Srrh extern int cwidth; 737063Srrh extern int nlflg; 747063Srrh extern int *ap; 757063Srrh extern int donef; 767063Srrh extern int nflush; 777063Srrh extern int nchar; 787063Srrh extern int rchar; 797063Srrh extern int nfo; 807063Srrh extern int ifile; 817063Srrh extern int fc; 827063Srrh extern int padc; 837063Srrh extern int tabc; 847063Srrh extern int dotc; 857063Srrh extern int raw; 867063Srrh extern int tabtab[NTAB]; 877063Srrh extern char nextf[]; 887063Srrh extern int nfi; 897063Srrh #ifdef NROFF 907063Srrh extern char termtab[]; 917063Srrh extern int tti; 927063Srrh #endif 937063Srrh extern int ifl[NSO]; 947063Srrh extern int ifi; 957063Srrh extern int pendt; 967063Srrh extern int flss; 977063Srrh extern int fi; 987063Srrh extern int lg; 997063Srrh extern char ptname[]; 1007063Srrh extern int print; 1017063Srrh extern int nonumb; 1027063Srrh extern int pnlist[]; 1037063Srrh extern int *pnp; 1047063Srrh extern int nb; 1057063Srrh extern int trap; 1067063Srrh extern int tflg; 1077063Srrh extern int ejf; 1087063Srrh extern int lit; 1097063Srrh extern int cc; 1107063Srrh extern int c2; 1117063Srrh extern int spread; 1127063Srrh extern int gflag; 1137063Srrh extern int oline[]; 1147063Srrh extern int *olinep; 1157063Srrh extern int dpn; 1167063Srrh extern int noscale; 1177063Srrh extern char *unlkp; 1187063Srrh extern int pts; 1197063Srrh extern int level; 1207063Srrh extern int ttysave; 1217063Srrh extern int tdelim; 1227063Srrh extern int dotT; 1237063Srrh extern int tabch, ldrch; 1247063Srrh extern int eqflg; 1257063Srrh extern no_out; 1267063Srrh extern int hflg; 1277063Srrh #ifndef NROFF 1287063Srrh extern char codetab[]; 1297063Srrh extern int spbits; 1307063Srrh #endif 1317063Srrh extern int xxx; 1327063Srrh int stopmesg; 1337063Srrh filep ipl[NSO]; 1347063Srrh long offl[NSO]; 1357063Srrh long ioff; 1367063Srrh char *ttyp; 1377063Srrh extern struct contab { 1387063Srrh int rq; 1397063Srrh union { 1407063Srrh int (*f)(); 1417063Srrh unsigned mx; 1427063Srrh }x; 1437063Srrh }contab[NM]; 1447063Srrh int ms[] = {31,28,31,30,31,30,31,31,30,31,30,31}; 1457063Srrh #ifndef NROFF 1467063Srrh int acctf; 1477063Srrh #endif 1487063Srrh 1497063Srrh main(argc,argv) 1507063Srrh int argc; 1517063Srrh char **argv; 1527063Srrh { 1537063Srrh char *p, *q; 1547063Srrh register i, j; 1557063Srrh extern catch(), fpecatch(), kcatch(); 1567063Srrh 1577063Srrh signal(SIGHUP,catch); 1587063Srrh if(signal(SIGINT,catch) == SIG_IGN){ 1597063Srrh signal(SIGHUP,SIG_IGN); 1607063Srrh signal(SIGINT,SIG_IGN); 1617063Srrh signal(SIGQUIT,SIG_IGN); 1627063Srrh } 1637063Srrh signal(SIGFPE,fpecatch); 1647063Srrh signal(SIGPIPE,catch); 1657063Srrh signal(SIGTERM,kcatch); 1667063Srrh init1(argv[0][0]); 1677063Srrh options: 1687063Srrh while(--argc > 0 && (++argv)[0][0]=='-') 1697063Srrh switch(argv[0][1]){ 1707063Srrh 1717063Srrh case 0: 1727063Srrh goto start; 1737063Srrh case 'i': 1747063Srrh stdi++; 1757063Srrh continue; 1767063Srrh case 'q': 1777063Srrh quiet++; 1787063Srrh if(gtty(0, &ttys) >= 0) 1797063Srrh ttysave = ttys.sg_flags; 1807063Srrh continue; 1817063Srrh case 'n': 1827063Srrh npn = cnum(&argv[0][2]); 1837063Srrh continue; 1847063Srrh case 'p': 1857063Srrh xflg = 0; 1867063Srrh cps = cnum(&argv[0][2]); 1877063Srrh continue; 1887063Srrh case 'S': 1897063Srrh stopmesg++; 1907063Srrh continue; 1917063Srrh case 's': 1927063Srrh if(!(stop = cnum(&argv[0][2])))stop++; 1937063Srrh continue; 1947063Srrh case 'r': 1957063Srrh vlist[findr(argv[0][2])] = cnum(&argv[0][3]); 1967063Srrh continue; 1977063Srrh case 'm': 1987063Srrh p = &nextf[nfi]; 1997063Srrh q = &argv[0][2]; 2007063Srrh while((*p++ = *q++) != 0); 2017063Srrh if (access(nextf, 4) < 0) { 2027063Srrh char *local = "/usr/local/lib/tmac/tmac.\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; 2037063Srrh strcat(local, &argv[0][2]); 2047063Srrh if (access(local, 4) == 0) 2057063Srrh strcpy(nextf, local); 2067063Srrh } 2077063Srrh mflg++; 2087063Srrh continue; 2097063Srrh case 'o': 2107063Srrh getpn(&argv[0][2]); 2117063Srrh continue; 2127063Srrh #ifdef NROFF 2137063Srrh case 'h': 2147063Srrh hflg++; 2157063Srrh continue; 2167063Srrh case 'z': 2177063Srrh no_out++; 2187063Srrh continue; 2197063Srrh case 'e': 2207063Srrh eqflg++; 2217063Srrh continue; 2227063Srrh case 'T': 2237063Srrh p = &termtab[tti]; 2247063Srrh q = &argv[0][2]; 2257063Srrh if(!((*q) & 0177))continue; 2267063Srrh while((*p++ = *q++) != 0); 2277063Srrh dotT++; 2287063Srrh continue; 2297063Srrh #endif 2307063Srrh #ifndef NROFF 2317063Srrh case 'z': 2327063Srrh no_out++; 2337063Srrh case 'a': 2347063Srrh ascii = 1; 2357063Srrh nofeed++; 2367063Srrh case 't': 2377063Srrh ptid = 1; 2387063Srrh continue; 2397063Srrh case 'w': 2407063Srrh waitf = 1; 2417063Srrh continue; 2427063Srrh case 'f': 2437063Srrh nofeed++; 2447063Srrh continue; 2457063Srrh case 'x': 2467063Srrh xflg = 0; 2477063Srrh continue; 2487063Srrh case 'b': 2497063Srrh if(open(ptname,1) < 0)prstr("Busy.\n"); 2507063Srrh else prstr("Available.\n"); 2517063Srrh done3(0); 2527063Srrh case 'g': 2537063Srrh stop = ptid = gflag = 1; 2547063Srrh dpn = 0; 2557063Srrh continue; 25611247Sshannon case 'F': 25711247Sshannon { 25811247Sshannon extern char *fontfile; 25911247Sshannon fontfile = &argv[0][2]; 26011247Sshannon } 26111247Sshannon continue; 2627063Srrh #endif 2637063Srrh default: 2647063Srrh pto = cnum(&argv[0][1]); 2657063Srrh continue; 2667063Srrh } 2677063Srrh 2687063Srrh if(argv[0][0] == '+'){ 2697063Srrh pfrom = cnum(&argv[0][1]); 2707063Srrh print = 0; 2717063Srrh if(argc > 0)goto options; 2727063Srrh } 2737063Srrh 2747063Srrh start: 2757063Srrh argp = argv; 2767063Srrh rargc = argc; 2777063Srrh init2(); 2787063Srrh setjmp(sjbuf); 2797063Srrh loop: 2807063Srrh copyf = lgf = nb = nflush = nlflg = 0; 2817063Srrh if(ip && (rbf0(ip)==0) && ejf && (frame->pframe <= ejl)){ 2827063Srrh nflush++; 2837063Srrh trap = 0; 2847063Srrh eject((struct s *)0); 2857063Srrh goto loop; 2867063Srrh } 2877063Srrh i = getch(); 2887063Srrh if(pendt)goto lt; 2897063Srrh if(lit && (frame <= litlev)){ 2907063Srrh lit--; 2917063Srrh goto lt; 2927063Srrh } 2937063Srrh if((j = (i & CMASK)) == XPAR){ 2947063Srrh copyf++; 2957063Srrh tflg++; 2967063Srrh for(;(i & CMASK) != '\n';)pchar(i = getch()); 2977063Srrh tflg = 0; 2987063Srrh copyf--; 2997063Srrh goto loop; 3007063Srrh } 3017063Srrh if((j == cc) || (j == c2)){ 3027063Srrh if(j == c2)nb++; 3037063Srrh copyf++; 3047063Srrh while(((j=((i=getch()) & CMASK)) == ' ') || 3057063Srrh (j == '\t')); 3067063Srrh ch = i; 3077063Srrh copyf--; 3087063Srrh control(getrq(),1); 3097063Srrh flushi(); 3107063Srrh goto loop; 3117063Srrh } 3127063Srrh lt: 3137063Srrh ch = i; 3147063Srrh text(); 3157063Srrh goto loop; 3167063Srrh } 3177063Srrh catch(){ 3187063Srrh /* 3197063Srrh prstr("Interrupt\n"); 3207063Srrh */ 3217063Srrh done3(01); 3227063Srrh } 3237063Srrh fpecatch(){ 3247063Srrh prstrfl("Floating Exception.\n"); 3257063Srrh signal(SIGFPE,fpecatch); 3267063Srrh } 3277063Srrh kcatch(){ 3287063Srrh signal(SIGTERM,SIG_IGN); 3297063Srrh done3(01); 3307063Srrh } 3317063Srrh #ifndef NROFF 3327063Srrh acctg() { 3337063Srrh static char *acct_file = "/usr/adm/tracct"; 3347063Srrh acctf = open(acct_file,1); 3357063Srrh setuid(getuid()); 3367063Srrh } 3377063Srrh #endif 3387063Srrh init1(a) 3397063Srrh char a; 3407063Srrh { 3417063Srrh register char *p; 3427063Srrh char *mktemp(); 3437063Srrh register i; 3447063Srrh 3457063Srrh #ifndef NROFF 3467063Srrh acctg();/*open troff actg file while mode 4755*/ 3477063Srrh #endif 3487063Srrh p = mktemp("/tmp/taXXXXX"); 3497063Srrh if(a == 'a')p = &p[5]; 3507063Srrh if((close(creat(p, 0600))) < 0){ 3517063Srrh prstr("Cannot create temp file.\n"); 3527063Srrh exit(-1); 3537063Srrh } 3547063Srrh ibf = open(p, 2); 3557063Srrh for(i=256; --i;)trtab[i]=i; 3567063Srrh trtab[UNPAD] = ' '; 3577063Srrh mchbits(); 3587063Srrh if(a != 'a')unlkp = p; 3597063Srrh } 3607063Srrh init2() 3617063Srrh { 3627063Srrh register i,j; 3637063Srrh extern int block; 3647063Srrh extern char *setbrk(); 3657063Srrh extern char *ttyname(); 3667063Srrh 3677063Srrh ttyod = 2; 3687063Srrh if(((ttyp=ttyname(j=0)) != (char *)0) || 3697063Srrh ((ttyp=ttyname(j=1)) != (char *)0) || 3707063Srrh ((ttyp=ttyname(j=2)) != (char *)0) 3717063Srrh );else ttyp = "notty"; 3727063Srrh iflg = j; 3737063Srrh if(ascii)mesg(0); 3747063Srrh 3757063Srrh if((!ptid) && (!waitf)){ 3767063Srrh if((ptid = open(ptname,1)) < 0){ 3777063Srrh prstr("Typesetter busy.\n"); 3787063Srrh done3(-2); 3797063Srrh } 3807063Srrh } 3817063Srrh ptinit(); 3827063Srrh for(i=NEV; i--;)write(ibf, (char *)&block, EVS*sizeof(int)); 3837063Srrh olinep = oline; 3847063Srrh ibufp = eibuf = ibuf; 3857063Srrh v.hp = init = 0; 3867063Srrh pinchar = inchar; /* XXX */ 3877063Srrh ioff = 0; 3887063Srrh v.nl = -1; 3897063Srrh cvtime(); 3907063Srrh frame = stk = (struct s *)setbrk(DELTA); 3917063Srrh dip = &d[0]; 3927063Srrh nxf = frame + 1; 3937063Srrh nx = mflg; 3947063Srrh } 3957063Srrh cvtime(){ 3967063Srrh 3977063Srrh long tt; 3987063Srrh register i; 3997063Srrh 4007063Srrh time(&tt); 4017063Srrh tt -= 3600*ZONE; /*5hrs for EST*/ 4027063Srrh v.dy = (tt/86400L) + 1; 4037063Srrh v.dw = (v.dy + 3)%7 + 1; 4047063Srrh for(v.yr=70;; v.yr++){ 4057063Srrh if((v.yr)%4)ms[1]=28;else ms[1]=29; 4067063Srrh for(i=0;i<12;){ 4077063Srrh if(v.dy<=ms[i]){ 4087063Srrh v.mo = i+1; 4097063Srrh return; 4107063Srrh } 4117063Srrh v.dy -= ms[i++]; 4127063Srrh } 4137063Srrh } 4147063Srrh } 4157063Srrh cnum(a) 4167063Srrh char *a; 4177063Srrh { 4187063Srrh register i; 4197063Srrh 4207063Srrh ibufp = a; 4217063Srrh eibuf = MAXPTR; 4227063Srrh i = atoi(); 4237063Srrh ch = 0; 4247063Srrh return(i); 4257063Srrh } 4267063Srrh mesg(f) 4277063Srrh int f; 4287063Srrh { 4297063Srrh static int mode; 4307063Srrh 4317063Srrh if (ttyp==0) 4327063Srrh return; 4337063Srrh if(!f){ 4347063Srrh stat(ttyp,cbuf); 4357063Srrh mode = ((struct stat *)(cbuf))->st_mode; 4367063Srrh chmod(ttyp,mode & ~022); 4377063Srrh }else{ 4387063Srrh chmod(ttyp,mode); 4397063Srrh } 4407063Srrh } 4417063Srrh prstrfl(s) 4427063Srrh char *s; 4437063Srrh { 4447063Srrh flusho(); 4457063Srrh prstr(s); 4467063Srrh } 4477063Srrh prstr(s) 4487063Srrh char *s; 4497063Srrh { 4507063Srrh register i; 4517063Srrh register char *j; 4527063Srrh 4537063Srrh j = s; 4547063Srrh for(i=0;*s;i++)s++; 4557063Srrh write(ttyod,j,i); 4567063Srrh } 4577063Srrh control(a,b) 4587063Srrh int a,b; 4597063Srrh { 4607063Srrh register i,j; 4617063Srrh extern filep boff(); 4627063Srrh 4637063Srrh i = a; 4647063Srrh if((i == 0) || ((j = findmn(i)) == -1))return(0); 4657063Srrh if(contab[j].rq & MMASK){ 4667063Srrh nxf->nargs = 0; 4677063Srrh if(b)collect(); 4687063Srrh flushi(); 4697063Srrh return(pushi(((filep)contab[j].x.mx)<<BLKBITS)); 4707063Srrh }else{ 4717063Srrh if(!b)return(0); 4727063Srrh return((*contab[j].x.f)(0)); 4737063Srrh } 4747063Srrh } 4757063Srrh 4767063Srrh getrq(){ 4777063Srrh register i,j; 4787063Srrh 4797063Srrh if(((i=getach()) == 0) || 4807063Srrh ((j=getach()) == 0))goto rtn; 4817063Srrh i = PAIR(i,j); 4827063Srrh rtn: 4837063Srrh return(i); 4847063Srrh } 4857063Srrh getch(){ 4867063Srrh register int i, j, k; 4877063Srrh 4887063Srrh level++; 4897063Srrh g0: 4907063Srrh if(ch){ 4917063Srrh if(((i = ch) & CMASK) == '\n')nlflg++; 4927063Srrh ch = 0; 4937063Srrh level--; 4947063Srrh return(i); 4957063Srrh } 4967063Srrh 4977063Srrh if(nlflg){ 4987063Srrh level--; 4997063Srrh return('\n'); 5007063Srrh } 5017063Srrh 5027063Srrh if((k = (i = getch0()) & CMASK) != ESC){ 5037063Srrh if(i & MOT)goto g2; 5047063Srrh if(k == FLSS){ 5057063Srrh copyf++; raw++; 5067063Srrh i = getch0(); 5077063Srrh if(!fi)flss = i; 5087063Srrh copyf--; raw--; 5097063Srrh goto g0; 5107063Srrh } 5117063Srrh if(k == RPT){ 5127063Srrh setrpt(); 5137063Srrh goto g0; 5147063Srrh } 5157063Srrh if(!copyf){ 5167063Srrh if((k == 'f') && lg && !lgf){ 5177063Srrh i = getlg(i); 5187063Srrh goto g2; 5197063Srrh } 5207063Srrh if((k == fc) || (k == tabch) || (k == ldrch)){ 5217063Srrh if((i=setfield(k)) == 0)goto g0; else goto g2; 5227063Srrh } 5237063Srrh if(k == 010){ 5247063Srrh i = makem(-width(' ' | chbits)); 5257063Srrh goto g2; 5267063Srrh } 5277063Srrh } 5287063Srrh goto g2; 5297063Srrh } 5307063Srrh k = (j = getch0()) & CMASK; 5317063Srrh if(j & MOT){ 5327063Srrh i = j; 5337063Srrh goto g2; 5347063Srrh } 5357063Srrh /* 5367063Srrh if(k == tdelim){ 5377063Srrh i = TDELIM; 5387063Srrh tdelim = IMP; 5397063Srrh goto g2; 5407063Srrh } 5417063Srrh */ 5427063Srrh switch(k){ 5437063Srrh 5447063Srrh case '\n': /*concealed newline*/ 5457063Srrh goto g0; 5467063Srrh case 'n': /*number register*/ 5477063Srrh setn(); 5487063Srrh goto g0; 5497063Srrh case '*': /*string indicator*/ 5507063Srrh setstr(); 5517063Srrh goto g0; 5527063Srrh case '$': /*argument indicator*/ 5537063Srrh seta(); 5547063Srrh goto g0; 5557063Srrh case '{': /*LEFT*/ 5567063Srrh i = LEFT; 5577063Srrh goto gx; 5587063Srrh case '}': /*RIGHT*/ 5597063Srrh i = RIGHT; 5607063Srrh goto gx; 5617063Srrh case '"': /*comment*/ 5627063Srrh while(((i=getch0()) & CMASK ) != '\n'); 5637063Srrh goto g2; 5647063Srrh case ESC: /*double backslash*/ 5657063Srrh i = eschar; 5667063Srrh goto gx; 5677063Srrh case 'e': /*printable version of current eschar*/ 5687063Srrh i = PRESC; 5697063Srrh goto gx; 5707063Srrh case ' ': /*unpaddable space*/ 5717063Srrh i = UNPAD; 5727063Srrh goto gx; 5737063Srrh case '|': /*narrow space*/ 5747063Srrh i = NARSP; 5757063Srrh goto gx; 5767063Srrh case '^': /*half of narrow space*/ 5777063Srrh i = HNSP; 5787063Srrh goto gx; 5797063Srrh case '\'': /*\(aa*/ 5807063Srrh i = 0222; 5817063Srrh goto gx; 5827063Srrh case '`': /*\(ga*/ 5837063Srrh i = 0223; 5847063Srrh goto gx; 5857063Srrh case '_': /*\(ul*/ 5867063Srrh i = 0224; 5877063Srrh goto gx; 5887063Srrh case '-': /*current font minus*/ 5897063Srrh i = 0210; 5907063Srrh goto gx; 5917063Srrh case '&': /*filler*/ 5927063Srrh i = FILLER; 5937063Srrh goto gx; 5947063Srrh case 'c': /*to be continued*/ 5957063Srrh i = CONT; 5967063Srrh goto gx; 5977063Srrh case ':': /*lem's char*/ 5987063Srrh i = COLON; 5997063Srrh goto gx; 6007063Srrh case '!': /*transparent indicator*/ 6017063Srrh i = XPAR; 6027063Srrh goto gx; 6037063Srrh case 't': /*tab*/ 6047063Srrh i = '\t'; 6057063Srrh goto g2; 6067063Srrh case 'a': /*leader (SOH)*/ 6077063Srrh i = LEADER; 6087063Srrh goto g2; 6097063Srrh case '%': /*ohc*/ 6107063Srrh i = OHC; 6117063Srrh goto g2; 6127063Srrh case '.': /*.*/ 6137063Srrh i = '.'; 6147063Srrh gx: 6157063Srrh i = (j & ~CMASK) | i; 6167063Srrh goto g2; 6177063Srrh } 6187063Srrh if(!copyf) 6197063Srrh switch(k){ 6207063Srrh 6217063Srrh case 'p': /*spread*/ 6227063Srrh spread++; 6237063Srrh goto g0; 6247063Srrh case '(': /*special char name*/ 6257063Srrh if((i=setch()) == 0)goto g0; 6267063Srrh break; 6277063Srrh case 's': /*size indicator*/ 6287063Srrh setps(); 6297063Srrh goto g0; 6307063Srrh case 'f': /*font indicator*/ 6317063Srrh setfont(0); 6327063Srrh goto g0; 6337063Srrh case 'w': /*width function*/ 6347063Srrh setwd(); 6357063Srrh goto g0; 6367063Srrh case 'v': /*vert mot*/ 6377063Srrh if(i = vmot())break; 6387063Srrh goto g0; 6397063Srrh case 'h': /*horiz mot*/ 6407063Srrh if(i = hmot())break; 6417063Srrh goto g0; 6427063Srrh case 'z': /*zero with char*/ 6437063Srrh i = setz(); 6447063Srrh break; 6457063Srrh case 'l': /*hor line*/ 6467063Srrh setline(); 6477063Srrh goto g0; 6487063Srrh case 'L': /*vert line*/ 6497063Srrh setvline(); 6507063Srrh goto g0; 6517063Srrh case 'b': /*bracket*/ 6527063Srrh setbra(); 6537063Srrh goto g0; 6547063Srrh case 'o': /*overstrike*/ 6557063Srrh setov(); 6567063Srrh goto g0; 6577063Srrh case 'k': /*mark hor place*/ 6587063Srrh if((i=findr(getsn())) == -1)goto g0; 6597063Srrh vlist[i] = v.hp = sumhp(); /* XXX */ 6607063Srrh goto g0; 6617063Srrh case 'j': /*mark output hor place*/ 6627063Srrh if(!(i=getach()))goto g0; 6637063Srrh i = (i<<BYTE) | JREG; 6647063Srrh break; 6657063Srrh case '0': /*number space*/ 6667063Srrh i = makem(width('0' | chbits)); 6677063Srrh break; 6687063Srrh case 'x': /*extra line space*/ 6697063Srrh if(i = xlss())break; 6707063Srrh goto g0; 6717063Srrh case 'u': /*half em up*/ 6727063Srrh case 'r': /*full em up*/ 6737063Srrh case 'd': /*half em down*/ 6747063Srrh i = sethl(k); 6757063Srrh break; 6767063Srrh default: 6777063Srrh i = j; 6787063Srrh } 6797063Srrh else{ 6807063Srrh ch0 = j; 6817063Srrh i = eschar; 6827063Srrh } 6837063Srrh g2: 6847063Srrh if((i & CMASK) == '\n'){ 6857063Srrh nlflg++; 6867063Srrh v.hp = 0; 6877063Srrh pinchar = inchar; /* XXX */ 6887063Srrh if(ip == 0)v.cd++; 6897063Srrh } 6907063Srrh if(!--level){ 6917063Srrh /* j = width(i); */ 6927063Srrh /* v.hp += j; */ 6937063Srrh /* cwidth = j; */ 6947063Srrh if (pinchar >= inchar + LNSIZE) { /* XXX */ 6957063Srrh inchar[0] = makem(sumhp()); 6967063Srrh pinchar = &inchar[1]; 6977063Srrh } 6987063Srrh *pinchar++ = i; /* XXX */ 6997063Srrh } 7007063Srrh return(i); 7017063Srrh } 7027063Srrh 7037063Srrh sumhp() /* XXX - add up widths in inchar array */ 7047063Srrh { 7057063Srrh register int n; 7067063Srrh register int *p; 7077063Srrh 7087063Srrh n = 0; 7097063Srrh for (p = inchar; p < pinchar; p++) 7107063Srrh n += width(*p); 7117063Srrh return(n); 7127063Srrh } 7137063Srrh char ifilt[32] = {0,001,002,003,0,005,006,007,010,011,012}; 7147063Srrh getch0(){ 7157063Srrh register int i, j; 7167063Srrh 7177063Srrh if(ch0){i=ch0; ch0=0; return(i);} 7187063Srrh if(nchar){nchar--; return(rchar);} 7197063Srrh 7207063Srrh again: 7217063Srrh if(cp){ 7227063Srrh if((i = *cp++) == 0){ 7237063Srrh cp = 0; 7247063Srrh goto again; 7257063Srrh } 7267063Srrh }else if(ap){ 7277063Srrh if((i = *ap++) == 0){ 7287063Srrh ap = 0; 7297063Srrh goto again; 7307063Srrh } 7317063Srrh }else if(ip){ 7327063Srrh if(ip == -1)i = rdtty(); 7337063Srrh else i = rbf(); 7347063Srrh }else{ 7357063Srrh if(donef)done(0); 7367063Srrh if(nx || ((ibufp >= eibuf) && (ibufp != MAXPTR))){ 7377063Srrh if(nfo)goto g1; 7387063Srrh g0: 7397063Srrh if(nextfile()){ 7407063Srrh if(ip)goto again; 7417063Srrh if(ibufp < eibuf)goto g2; 7427063Srrh } 7437063Srrh g1: 7447063Srrh nx = 0; 7457063Srrh if((j=read(ifile,ibuf,IBUFSZ)) <= 0)goto g0; 7467063Srrh ibufp = ibuf; 7477063Srrh eibuf = ibuf + j; 7487063Srrh if(ip)goto again; 7497063Srrh } 7507063Srrh g2: 7517063Srrh i = *ibufp++ & 0177; 7527063Srrh ioff++; 7537063Srrh if(i >= 040)goto g4; else i = ifilt[i]; 7547063Srrh } 7557063Srrh if(raw)return(i); 7567063Srrh if((j = i & CMASK) == IMP)goto again; 7577063Srrh if((i == 0) && !init)goto again; 7587063Srrh g4: 7597063Srrh if((copyf == 0) && ((i & ~BMASK) == 0) && ((i & CMASK) < 0370)) 7607063Srrh #ifndef NROFF 7617063Srrh if(spbits && (i>31) && ((codetab[i-32] & 0200))) i |= spbits; 7627063Srrh else 7637063Srrh #endif 7647063Srrh i |= chbits; 7657063Srrh if((i & CMASK) == eschar)i = (i & ~CMASK) | ESC; 7667063Srrh return(i); 7677063Srrh } 7687063Srrh nextfile(){ 7697063Srrh register char *p; 7707063Srrh 7717063Srrh n0: 7727063Srrh if(ifile)close(ifile); 7737063Srrh if(nx){ 7747063Srrh p = nextf; 7757063Srrh if(*p != 0)goto n1; 7767063Srrh } 7777063Srrh if(ifi > 0){ 7787063Srrh if(popf())goto n0; /*popf error*/ 7797063Srrh return(1); /*popf ok*/ 7807063Srrh } 7817063Srrh if(rargc-- <= 0)goto n2; 7827063Srrh p = (argp++)[0]; 7837063Srrh n1: 7847063Srrh if((p[0] == '-') && (p[1] == 0)){ 7857063Srrh ifile = 0; 7867063Srrh }else if((ifile=open(p,0)) < 0){ 7877063Srrh prstr("Cannot open "); 7887063Srrh prstr(p); 7897063Srrh prstr("\n"); 7907063Srrh nfo -= mflg; 7917063Srrh done(02); 7927063Srrh } 7937063Srrh nfo++; 7947063Srrh v.cd = 0; 7957063Srrh ioff = 0; 7967063Srrh return(0); 7977063Srrh n2: 7987063Srrh if((nfo -= mflg) && !stdi)done(0); 7997063Srrh nfo++; 8007063Srrh v.cd = ifile = stdi = mflg = 0; 8017063Srrh ioff = 0; 8027063Srrh return(0); 8037063Srrh } 8047063Srrh popf(){ 8057063Srrh register i; 8067063Srrh register char *p, *q; 8077063Srrh extern char *ttyname(); 8087063Srrh 8097063Srrh ioff = offl[--ifi]; 8107063Srrh ip = ipl[ifi]; 8117063Srrh if((ifile = ifl[ifi]) == 0){ 8127063Srrh p = xbuf; 8137063Srrh q = ibuf; 8147063Srrh ibufp = xbufp; 8157063Srrh eibuf = xeibuf; 8167063Srrh while(q < eibuf)*q++ = *p++; 8177063Srrh return(0); 8187063Srrh } 8197063Srrh if((lseek(ifile,(long)(ioff & ~(IBUFSZ-1)),0) < 0) || 8207063Srrh ((i = read(ifile,ibuf,IBUFSZ)) < 0))return(1); 8217063Srrh eibuf = ibuf + i; 8227063Srrh ibufp = ibuf; 8237063Srrh if(ttyname(ifile) == (char *)0) 8247063Srrh if((ibufp = ibuf + (int)(ioff & (IBUFSZ-1))) >= eibuf)return(1); 8257063Srrh return(0); 8267063Srrh } 8277063Srrh flushi(){ 8287063Srrh if(nflush)return; 8297063Srrh ch = 0; 8307063Srrh if((ch0 & CMASK) == '\n')nlflg++; 8317063Srrh ch0 = 0; 8327063Srrh copyf++; 8337063Srrh while(!nlflg){ 8347063Srrh if(donef && (frame == stk))break; 8357063Srrh getch(); 8367063Srrh } 8377063Srrh copyf--; 8387063Srrh v.hp = 0; 8397063Srrh pinchar = inchar; /* XXX */ 8407063Srrh } 8417063Srrh getach(){ 8427063Srrh register i; 8437063Srrh 8447063Srrh lgf++; 8457063Srrh if(((i = getch()) & MOT) || 8467063Srrh ((i&CMASK) == ' ') || 8477063Srrh ((i&CMASK) == '\n')|| 8487063Srrh (i & 0200)){ 8497063Srrh ch = i; 8507063Srrh i = 0; 8517063Srrh } 8527063Srrh lgf--; 8537063Srrh return(i & 0177); 8547063Srrh } 8557063Srrh casenx(){ 8567063Srrh lgf++; 8577063Srrh skip(); 8587063Srrh getname(); 8597063Srrh nx++; 8607063Srrh nextfile(); 8617063Srrh nlflg++; 8627063Srrh ip = 0; 8637063Srrh ap = 0; 8647063Srrh nchar = pendt = 0; 8657063Srrh frame = stk; 8667063Srrh nxf = frame + 1; 8677063Srrh } 8687063Srrh getname(){ 8697063Srrh register int i, j, k; 8707063Srrh 8717063Srrh lgf++; 8727063Srrh for(k=0; k < (NS-1); k++){ 8737063Srrh if(((j=(i=getch()) & CMASK) <= ' ') || 8747063Srrh (j > 0176))break; 8757063Srrh nextf[k] = j; 8767063Srrh } 8777063Srrh nextf[k] = 0; 8787063Srrh ch = i; 8797063Srrh lgf--; 8807063Srrh return(nextf[0]); 8817063Srrh } 8827063Srrh caseso(){ 8837063Srrh register i; 8847063Srrh register char *p, *q; 8857063Srrh 8867063Srrh lgf++; 8877063Srrh nextf[0] = 0; 8887063Srrh if(skip() || !getname() || ((i=open(nextf,0)) <0) || (ifi >= NSO)) { 8897063Srrh prstr("can't open file "); 8907063Srrh prstr(nextf); 8917063Srrh prstr("\n"); 8927063Srrh done(02); 8937063Srrh } 8947063Srrh flushi(); 8957063Srrh ifl[ifi] = ifile; 8967063Srrh ifile = i; 8977063Srrh offl[ifi] = ioff; 8987063Srrh ioff = 0; 8997063Srrh ipl[ifi] = ip; 9007063Srrh ip = 0; 9017063Srrh nx++; 9027063Srrh nflush++; 9037063Srrh if(!ifl[ifi++]){ 9047063Srrh p = ibuf; 9057063Srrh q = xbuf; 9067063Srrh xbufp = ibufp; 9077063Srrh xeibuf = eibuf; 9087063Srrh while(p < eibuf)*q++ = *p++; 9097063Srrh } 9107063Srrh } 9117063Srrh 9127063Srrh casecf(){ /* copy file without change */ 9137063Srrh int fd, i, n; 9147063Srrh char buf[OBUFSZ]; 9157063Srrh 9167063Srrh flusho(); 9177063Srrh lgf++; 9187063Srrh nextf[0] = 0; 9197063Srrh if(skip() || !getname() || ((fd=open(nextf,0)) <0) || (ifi >= NSO)) { 9207063Srrh prstr("can't open file "); 9217063Srrh prstr(nextf); 9227063Srrh prstr("\n"); 9237063Srrh done(02); 9247063Srrh } 9257063Srrh while ((n = read(fd, buf, OBUFSZ)) > 0) 9267063Srrh for (i = 0; i < n; i++) 9277063Srrh oput(buf[i]); 9287063Srrh flusho(); 9297063Srrh close(fd); 9307063Srrh } 9317063Srrh getpn(a) 9327063Srrh char *a; 9337063Srrh { 9347063Srrh register i, neg; 9357063Srrh long atoi1(); 9367063Srrh 9377063Srrh if((*a & 0177) == 0)return; 9387063Srrh neg = 0; 9397063Srrh ibufp = a; 9407063Srrh eibuf = MAXPTR; 9417063Srrh noscale++; 9427063Srrh while((i = getch() & CMASK) != 0)switch(i){ 9437063Srrh case '+': 9447063Srrh case ',': 9457063Srrh continue; 9467063Srrh case '-': 9477063Srrh neg = MOT; 9487063Srrh goto d2; 9497063Srrh default: 9507063Srrh ch = i; 9517063Srrh d2: 9527063Srrh i = atoi1(); 9537063Srrh if(nonumb)goto fini; 9547063Srrh else{ 9557063Srrh *pnp++ = i | neg; 9567063Srrh neg = 0; 9577063Srrh if(pnp >= &pnlist[NPN-2]){ 9587063Srrh prstr("Too many page numbers\n"); 9597063Srrh done3(-3); 9607063Srrh } 9617063Srrh } 9627063Srrh } 9637063Srrh fini: 9647063Srrh if(neg)*pnp++ = -2; 9657063Srrh *pnp = -1; 9667063Srrh ch = noscale = print = 0; 9677063Srrh pnp = pnlist; 9687063Srrh if(*pnp != -1)chkpn(); 9697063Srrh } 9707063Srrh setrpt(){ 9717063Srrh register i, j; 9727063Srrh 9737063Srrh copyf++;raw++; 9747063Srrh i = getch0(); 9757063Srrh copyf--;raw--; 9767063Srrh if((i < 0) || 9777063Srrh (((j = getch0()) & CMASK) == RPT))return; 9787063Srrh rchar = j; 9797063Srrh nchar = i & BMASK; 9807063Srrh } 981