Research V7 development
[unix-history] / usr / src / cmd / troff / n7.c
CommitLineData
f10d52e6
JO
1#include "tdef.h"
2extern
3#include "d.h"
4extern
5#include "v.h"
6#ifdef NROFF
7extern
8#include "tw.h"
9#endif
10#include "s.h"
11#ifdef NROFF
12#define GETCH gettch
13#endif
14#ifndef NROFF
15#define GETCH getch
16#endif
17
18/*
19troff7.c
20
21text
22*/
23
24extern struct s *frame, *stk;
25extern struct s *ejl;
26
27extern int pl;
28extern int trap;
29extern int flss;
30extern int npnflg;
31extern int npn;
32extern int stop;
33extern int nflush;
34extern int ejf;
35extern int ascii;
36extern int donef;
37extern int nc;
38extern int wch;
39extern int dpn;
40extern int ndone;
41extern int lss;
42extern int pto;
43extern int pfrom;
44extern int print;
45extern int nlist[NTRAP];
46extern int mlist[NTRAP];
47extern int *pnp;
48extern int nb;
49extern int ic;
50extern int icf;
51extern int ics;
52extern int ne;
53extern int ll;
54extern int un;
55extern int un1;
56extern int in;
57extern int ls;
58extern int spread;
59extern int totout;
60extern int nwd;
61extern int *pendw;
62extern int *linep;
63extern int line[];
64extern int lastl;
65extern int ch;
66extern int ce;
67extern int fi;
68extern int nlflg;
69extern int pendt;
70extern int sps;
71extern int adsp;
72extern int pendnf;
73extern int over;
74extern int adrem;
75extern int nel;
76extern int ad;
77extern int ohc;
78extern int hyoff;
79extern int nhyp;
80extern int spflg;
81extern int word[];
82extern int *wordp;
83extern int wne;
84extern int chbits;
85extern int cwidth;
86extern int widthp;
87extern int hyf;
88extern int xbitf;
89extern int vflag;
90extern int ul;
91extern int cu;
92extern int font;
93extern int sfont;
94extern int it;
95extern int itmac;
96extern int *hyptr[NHYP];
97extern int **hyp;
98extern int *wdstart, *wdend;
99extern int lnmod;
100extern int admod;
101extern int nn;
102extern int nms;
103extern int ndf;
104extern int ni;
105extern int nform;
106extern int lnsize;
107extern int po;
108extern int ulbit;
109extern int *vlist;
110extern int nrbits;
111extern int nmbits;
112extern char trtab[];
113extern int xxx;
114int brflg;
115
116tbreak(){
117 register *i, j, pad;
118 int res;
119
120 trap = 0;
121 if(nb)return;
122 if((dip == d) && (v.nl == -1)){
123 newline(1);
124 return;
125 }
126 if(!nc){
127 setnel();
128 if(!wch)return;
129 if(pendw)getword(1);
130 movword();
131 }else if(pendw && !brflg){
132 getword(1);
133 movword();
134 }
135 *linep = dip->nls = 0;
136#ifdef NROFF
137 if(dip == d)horiz(po);
138#endif
139 if(lnmod)donum();
140 lastl = ne;
141 if(brflg != 1){
142 totout = 0;
143 }else if(ad){
144 if((lastl = (ll - un)) < ne)lastl = ne;
145 }
146 if(admod && ad && (brflg != 2)){
147 lastl = ne;
148 adsp = adrem = 0;
149#ifdef NROFF
150 if(admod == 1)un += quant(nel/2,t.Adj);
151#endif
152#ifndef NROFF
153 if(admod == 1)un += nel/2;
154#endif
155 else if(admod ==2)un += nel;
156 }
157 totout++;
158 brflg = 0;
159 if((lastl+un) > dip->maxl)dip->maxl = (lastl+un);
160 horiz(un);
161#ifdef NROFF
162 if(adrem%t.Adj)res = t.Hor; else res = t.Adj;
163#endif
164 for(i = line;nc > 0;){
165 if(((j = *i++) & CMASK) == ' '){
166 pad = 0;
167 do{
168 pad += width(j);
169 nc--;
170 }while(((j = *i++) & CMASK) == ' ');
171 i--;
172 pad += adsp;
173 --nwd;
174 if(adrem){
175 if(adrem < 0){
176#ifdef NROFF
177 pad -= res;
178 adrem += res;
179 }else if((totout&01) ||
180 ((adrem/res)>=(nwd))){
181 pad += res;
182 adrem -= res;
183#endif
184#ifndef NROFF
185 pad--;
186 adrem++;
187 }else{
188 pad++;
189 adrem--;
190#endif
191 }
192 }
193 horiz(pad);
194 }else{
195 pchar(j);
196 nc--;
197 }
198 }
199 if(ic){
200 if((j = ll - un - lastl + ics) > 0)horiz(j);
201 pchar(ic);
202 }
203 if(icf)icf++;
204 else ic = 0;
205 ne = nwd = 0;
206 un = in;
207 setnel();
208 newline(0);
209 if(dip != d){if(dip->dnl > dip->hnl)dip->hnl = dip->dnl;}
210 else{if(v.nl > dip->hnl)dip->hnl = v.nl;}
211 for(j=ls-1; (j >0) && !trap; j--)newline(0);
212 spread = 0;
213}
214donum(){
215 register i, nw;
216 extern pchar();
217
218 nrbits = nmbits;
219 nw = width('1' | nrbits);
220 if(nn){
221 nn--;
222 goto d1;
223 }
224 if(v.ln%ndf){
225 v.ln++;
226 d1:
227 un += nw*(3+nms+ni);
228 return;
229 }
230 i = 0;
231 if(v.ln<100)i++;
232 if(v.ln<10)i++;
233 horiz(nw*(ni+i));
234 nform = 0;
235 fnumb(v.ln,pchar);
236 un += nw*nms;
237 v.ln++;
238}
239text(){
240 register i;
241 static int spcnt;
242
243 nflush++;
244 if((dip == d) && (v.nl == -1)){newline(1); return;}
245 setnel();
246 if(ce || !fi){
247 nofill();
248 return;
249 }
250 if(pendw)goto t4;
251 if(pendt)if(spcnt)goto t2; else goto t3;
252 pendt++;
253 if(spcnt)goto t2;
254 while(((i = GETCH()) & CMASK) == ' ')spcnt++;
255 if(nlflg){
256 t1:
257 nflush = pendt = ch = spcnt = 0;
258 callsp();
259 return;
260 }
261 ch = i;
262 if(spcnt){
263 t2:
264 tbreak();
265 if(nc || wch)goto rtn;
266 un += spcnt*sps;
267 spcnt = 0;
268 setnel();
269 if(trap)goto rtn;
270 if(nlflg)goto t1;
271 }
272t3:
273 if(spread)goto t5;
274 if(pendw || !wch)
275 t4:
276 if(getword(0))goto t6;
277 if(!movword())goto t3;
278t5:
279 if(nlflg)pendt = 0;
280 adsp = adrem = 0;
281 if(ad){
282/* jfr */ if (nwd==1) adsp=nel; else adsp=nel/(nwd-1);
283#ifdef NROFF
284 adsp = (adsp/t.Adj)*t.Adj;
285#endif
286 adrem = nel - adsp*(nwd-1);
287 }
288 brflg = 1;
289 tbreak();
290 spread = 0;
291 if(!trap)goto t3;
292 if(!nlflg)goto rtn;
293t6:
294 pendt = 0;
295 ckul();
296rtn:
297 nflush = 0;
298}
299nofill(){
300 register i, j;
301
302 if(!pendnf){
303 over = 0;
304 tbreak();
305 if(trap)goto rtn;
306 if(nlflg){
307 ch = nflush = 0;
308 callsp();
309 return;
310 }
311 adsp = adrem = 0;
312 nwd = 10000;
313 }
314 while((j = ((i = GETCH()) & CMASK)) != '\n'){
315 if(j == ohc)continue;
316 if(j == CONT){
317 pendnf++;
318 nflush = 0;
319 flushi();
320 ckul();
321 return;
322 }
323 storeline(i,-1);
324 }
325 if(ce){
326 ce--;
327 if((i=quant(nel/2,HOR)) > 0)un += i;
328 }
329 if(!nc)storeline(FILLER,0);
330 brflg = 2;
331 tbreak();
332 ckul();
333rtn:
334 pendnf = nflush = 0;
335}
336callsp(){
337 register i;
338
339 if(flss)i = flss; else i = lss;
340 flss = 0;
341 casesp(i);
342}
343ckul(){
344 if(ul && (--ul == 0)){
345 cu = 0;
346 font = sfont;
347 mchbits();
348 }
349 if(it && (--it == 0) && itmac)control(itmac,0);
350}
351storeline(c,w){
352 register i;
353
354 if((c & CMASK) == JREG){
355 if((i=findr(c>>BYTE)) != -1)vlist[i] = ne;
356 return;
357 }
358 if(linep >= (line + lnsize - 1)){
359 if(!over){
360 prstrfl("Line overflow.\n");
361 over++;
362 c = 0343;
363 w = -1;
364 goto s1;
365 }
366 return;
367 }
368s1:
369 if(w == -1)w = width(c);
370 ne += w;
371 nel -= w;
372/*
373 * if( cu && !(c & MOT) && (trtab[(c & CMASK)] == ' '))
374 * c = ((c & ~ulbit) & ~CMASK) | '_';
375 */
376 *linep++ = c;
377 nc++;
378}
379newline(a)
380int a;
381{
382 register i, j, nlss;
383 int opn;
384
385 if(a)goto nl1;
386 if(dip != d){
387 j = lss;
388 pchar1(FLSS);
389 if(flss)lss = flss;
390 i = lss + dip->blss;
391 dip->dnl += i;
392 pchar1(i);
393 pchar1('\n');
394 lss = j;
395 dip->blss = flss = 0;
396 if(dip->alss){
397 pchar1(FLSS);
398 pchar1(dip->alss);
399 pchar1('\n');
400 dip->dnl += dip->alss;
401 dip->alss = 0;
402 }
403 if(dip->ditrap && !dip->ditf &&
404 (dip->dnl >= dip->ditrap) && dip->dimac)
405 if(control(dip->dimac,0)){trap++; dip->ditf++;}
406 return;
407 }
408 j = lss;
409 if(flss)lss = flss;
410 nlss = dip->alss + dip->blss + lss;
411 v.nl += nlss;
412#ifndef NROFF
413 if(ascii){dip->alss = dip->blss = 0;}
414#endif
415 pchar1('\n');
416 flss = 0;
417 lss = j;
418 if(v.nl < pl)goto nl2;
419nl1:
420 ejf = dip->hnl = v.nl = 0;
421 ejl = frame;
422 if(donef){
423 if((!nc && !wch) || ndone)done1(0);
424 ndone++;
425 donef = 0;
426 if(frame == stk)nflush++;
427 }
428 opn = v.pn;
429 v.pn++;
430 if(npnflg){
431 v.pn = npn;
432 npn = npnflg = 0;
433 }
434nlpn:
435 if(v.pn == pfrom){
436 print++;
437 pfrom = -1;
438 }else if(opn == pto){
439 print = 0;
440 opn = -1;
441 chkpn();
442 goto nlpn;
443 }
444 if(stop && print){
445 dpn++;
446 if(dpn >= stop){
447 dpn = 0;
448 dostop();
449 }
450 }
451nl2:
452 trap = 0;
453 if(v.nl == 0){
454 if((j = findn(0)) != NTRAP)
455 trap = control(mlist[j],0);
456 } else if((i = findt(v.nl-nlss)) <= nlss){
457 if((j = findn1(v.nl-nlss+i)) == NTRAP){
458 prstrfl("Trap botch.\n");
459 done2(-5);
460 }
461 trap = control(mlist[j],0);
462 }
463}
464findn1(a)
465int a;
466{
467 register i, j;
468
469 for(i=0; i<NTRAP; i++){
470 if(mlist[i]){
471 if((j = nlist[i]) < 0)j += pl;
472 if(j == a)break;
473 }
474 }
475 return(i);
476}
477chkpn(){
478 pto = *(pnp++);
479 pfrom = pto & ~MOT;
480 if(pto == -1){
481 flusho();
482 done1(0);
483 }
484 if(pto & MOT){
485 pto &= ~MOT;
486 print++;
487 pfrom = 0;
488 }
489}
490findt(a)
491int a;
492{
493 register i, j, k;
494
495 k = 32767;
496 if(dip != d){
497 if(dip->dimac && ((i = dip->ditrap -a) > 0))k = i;
498 return(k);
499 }
500 for(i=0; i<NTRAP; i++){
501 if(mlist[i]){
502 if((j = nlist[i]) < 0)j += pl;
503 if((j -= a) <= 0)continue;
504 if(j < k)k = j;
505 }
506 }
507 i = pl - a;
508 if(k > i)k = i;
509 return(k);
510}
511findt1(){
512 register i;
513
514 if(dip != d)i = dip->dnl;
515 else i = v.nl;
516 return(findt(i));
517}
518eject(a)
519struct s *a;
520{
521 register savlss;
522
523 if(dip != d)return;
524 ejf++;
525 if(a)ejl = a;
526 else ejl = frame;
527 if(trap)return;
528e1:
529 savlss = lss;
530 lss = findt(v.nl);
531 newline(0);
532 lss = savlss;
533 if(v.nl && !trap)goto e1;
534}
535movword(){
536 register i, w, *wp;
537 int savwch, hys;
538
539 over = 0;
540 wp = wordp;
541 if(!nwd){
542 while(((i = *wp++) & CMASK) == ' '){
543 wch--;
544 wne -= width(i);
545 }
546 wp--;
547 }
548 if((wne > nel) &&
549 !hyoff && hyf &&
550 (!nwd || (nel > 3*sps)) &&
551 (!(hyf & 02) || (findt1() > lss))
552 )hyphen(wp);
553 savwch = wch;
554 hyp = hyptr;
555 nhyp = 0;
556 while(*hyp && (*hyp <= wp))hyp++;
557 while(wch){
558 if((hyoff != 1) && (*hyp == wp)){
559 hyp++;
560 if(!wdstart ||
561 ((wp > (wdstart+1)) &&
562 (wp < wdend) &&
563 (!(hyf & 04) || (wp < (wdend-1))) &&
564 (!(hyf & 010) || (wp > (wdstart+2)))
565 )
566 ){
567 nhyp++;
568 storeline(IMP,0);
569 }
570 }
571 i = *wp++;
572 w = width(i);
573 wne -= w;
574 wch--;
575 storeline(i,w);
576 }
577 if(nel >= 0){
578 nwd++;
579 return(0);
580 }
581 xbitf = 1;
582 hys = width(0200); /*hyphen*/
583m1:
584 if(!nhyp){
585 if(!nwd)goto m3;
586 if(wch == savwch)goto m4;
587 }
588 if(*--linep != IMP)goto m5;
589 if(!(--nhyp))
590 if(!nwd)goto m2;
591 if(nel < hys){
592 nc--;
593 goto m1;
594 }
595m2:
596 if(((i = *(linep-1) & CMASK) != '-') &&
597 (i != 0203)
598 ){
599 *linep = (*(linep-1) & ~CMASK) | 0200;
600 w = width(*linep);
601 nel -= w;
602 ne += w;
603 linep++;
604/*
605 hsend();
606*/
607 }
608m3:
609 nwd++;
610m4:
611 wordp = wp;
612 return(1);
613m5:
614 nc--;
615 w = width(*linep);
616 ne -= w;
617 nel += w;
618 wne += w;
619 wch++;
620 wp--;
621 goto m1;
622}
623horiz(i)
624int i;
625{
626 vflag = 0;
627 if(i)pchar(makem(i));
628}
629setnel(){
630 if(!nc){
631 linep = line;
632 if(un1 >= 0){
633 un = un1;
634 un1 = -1;
635 }
636 nel = ll - un;
637 ne = adsp = adrem = 0;
638 }
639}
640getword(x)
641int x;
642{
643 register i, j, swp;
644 int noword;
645
646 noword = 0;
647 if(x)if(pendw){
648 *pendw = 0;
649 goto rtn;
650 }
651 if(wordp = pendw)goto g1;
652 hyp = hyptr;
653 wordp = word;
654 over = wne = wch = 0;
655 hyoff = 0;
656 while(1){
657 j = (i = GETCH()) & CMASK;
658 if(j == '\n'){
659 wne = wch = 0;
660 noword = 1;
661 goto rtn;
662 }
663 if(j == ohc){
664 hyoff = 1;
665 continue;
666 }
667 if(j == ' '){
668 storeword(i,cwidth);
669 continue;
670 }
671 break;
672 }
673 swp = widthp;
674 storeword(' ' | chbits, -1);
675 if(spflg){
676 storeword(' ' | chbits, -1);
677 spflg = 0;
678 }
679 widthp = swp;
680g0:
681 if(j == CONT){
682 pendw = wordp;
683 nflush = 0;
684 flushi();
685 return(1);
686 }
687 if(hyoff != 1){
688 if(j == ohc){
689 hyoff = 2;
690 *hyp++ = wordp;
691 if(hyp > (hyptr+NHYP-1))hyp = hyptr+NHYP-1;
692 goto g1;
693 }
694 if((j == '-') ||
695 (j == 0203) /*3/4 Em dash*/
696 )if(wordp > word+1){
697 hyoff = 2;
698 *hyp++ = wordp + 1;
699 if(hyp > (hyptr+NHYP-1))hyp = hyptr+NHYP-1;
700 }
701 }
702 storeword(i,cwidth);
703g1:
704 j = (i = GETCH()) & CMASK;
705 if(j != ' '){
706 if(j != '\n')goto g0;
707 j = *(wordp-1) & CMASK;
708 if((j == '.') ||
709 (j == '!') ||
710 (j == '?'))spflg++;
711 }
712 *wordp = 0;
713rtn:
714 wdstart = 0;
715 wordp = word;
716 pendw = 0;
717 *hyp++ = 0;
718 setnel();
719 return(noword);
720}
721storeword(c,w)
722int c, w;
723{
724
725 if(wordp >= &word[WDSIZE - 1]){
726 if(!over){
727 prstrfl("Word overflow.\n");
728 over++;
729 c = 0343;
730 w = -1;
731 goto s1;
732 }
733 return;
734 }
735s1:
736 if(w == -1)w = width(c);
737 wne += w;
738 *wordp++ = c;
739 wch++;
740}
741#ifdef NROFF
742extern char trtab[];
743gettch(){
744 register int i, j;
745
746 if(!((i = getch()) & MOT) && (i & ulbit)){
747 j = i&CMASK;
748 if(cu && (trtab[j] == ' '))
749 i = ((i & ~ulbit)& ~CMASK) | '_';
750 if(!cu && (j>32) && (j<0370) && !(*t.codetab[j-32] & 0200))
751 i &= ~ulbit;
752 }
753 return(i);
754}
755#endif