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