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