1*48302Sbostic /*-
2*48302Sbostic * Copyright (c) 1991 The Regents of the University of California.
3*48302Sbostic * All rights reserved.
4*48302Sbostic *
5*48302Sbostic * %sccs.include.proprietary.c%
6*48302Sbostic */
7*48302Sbostic
87072Srrh #ifndef lint
9*48302Sbostic static char sccsid[] = "@(#)n8.c 4.2 (Berkeley) 04/18/91";
10*48302Sbostic #endif /* not lint */
117072Srrh
127072Srrh #include "tdef.h"
137072Srrh
147072Srrh /*
157072Srrh troff8.c
167072Srrh
177072Srrh hyphenation
187072Srrh */
197072Srrh
207072Srrh char hbuf[NHEX];
217072Srrh char *nexth = hbuf;
227072Srrh int *hyend;
237072Srrh extern int *wdstart, *wdend;
247072Srrh extern int *hyptr[];
257072Srrh extern int **hyp;
267072Srrh extern int hyoff;
277072Srrh extern int noscale;
287072Srrh extern int xxx;
297072Srrh #define THRESH 160 /*digram goodness threshold*/
307072Srrh int thresh = THRESH;
317072Srrh
hyphen(wp)327072Srrh hyphen(wp)
337072Srrh int *wp;
347072Srrh {
357072Srrh register *i, j;
367072Srrh
377072Srrh i = wp;
387072Srrh while(punct(*i++))
397072Srrh ;
407072Srrh if (!alph(*--i))
417072Srrh return;
427072Srrh wdstart = i++;
437072Srrh while(alph(*i++))
447072Srrh ;
457072Srrh hyend = wdend = --i-1;
467072Srrh while(punct(*i++))
477072Srrh ;
487072Srrh if (*--i)
497072Srrh return;
507072Srrh if ((wdend-wdstart-4) < 0)
517072Srrh return;
527072Srrh hyp = hyptr;
537072Srrh *hyp = 0;
547072Srrh hyoff = 2;
557072Srrh if (!exword() && !suffix())
567072Srrh digram();
577072Srrh *hyp++ = 0;
587072Srrh if (*hyptr) for(j = 1; j;) {
597072Srrh j = 0;
607072Srrh for(hyp = hyptr+1; *hyp != 0; hyp++) {
617072Srrh if (*(hyp-1) > *hyp) {
627072Srrh j++;
637072Srrh i = *hyp;
647072Srrh *hyp = *(hyp-1);
657072Srrh *(hyp-1) = i;
667072Srrh }
677072Srrh }
687072Srrh }
697072Srrh }
707072Srrh
punct(i)717072Srrh punct(i)
727072Srrh int i;
737072Srrh {
747072Srrh if (!i || alph(i))
757072Srrh return(0);
767072Srrh else
777072Srrh return(1);
787072Srrh }
797072Srrh
alph(i)807072Srrh alph(i)
817072Srrh int i;
827072Srrh {
837072Srrh register j;
847072Srrh
857072Srrh j = i & CMASK;
867072Srrh if (((j >= 'A') && (j <= 'Z')) || ((j >= 'a') && (j <= 'z')))
877072Srrh return(1);
887072Srrh else
897072Srrh return(0);
907072Srrh }
917072Srrh
caseht()927072Srrh caseht()
937072Srrh {
947072Srrh thresh = THRESH;
957072Srrh if (skip())
967072Srrh return;
977072Srrh noscale++;
987072Srrh thresh = atoi();
997072Srrh noscale = 0;
1007072Srrh }
1017072Srrh
casehw()1027072Srrh casehw()
1037072Srrh {
1047072Srrh register i, k;
1057072Srrh register char *j;
1067072Srrh
1077072Srrh k = 0;
1087072Srrh while(!skip()) {
1097072Srrh if ((j = nexth) >= (hbuf + NHEX - 2))
1107072Srrh goto full;
1117072Srrh for (;;) {
1127072Srrh if ((i = getch()) & MOT)
1137072Srrh continue;
1147072Srrh if (((i &= CMASK) == ' ') || (i == '\n')) {
1157072Srrh *j++ = 0;
1167072Srrh nexth = j;
1177072Srrh *j = 0;
1187072Srrh if (i == ' ')
1197072Srrh break;
1207072Srrh else
1217072Srrh return;
1227072Srrh }
1237072Srrh if (i == '-') {
1247072Srrh k = 0200;
1257072Srrh continue;
1267072Srrh }
1277072Srrh *j++ = maplow(i) | k;
1287072Srrh k = 0;
1297072Srrh if (j >= (hbuf + NHEX - 2))
1307072Srrh goto full;
1317072Srrh }
1327072Srrh }
1337072Srrh return;
1347072Srrh full:
1357072Srrh prstr("Exception word list full.\n");
1367072Srrh *nexth = 0;
1377072Srrh }
1387072Srrh
exword()1397072Srrh exword()
1407072Srrh {
1417072Srrh register int *w;
1427072Srrh register char *e;
1437072Srrh char *save;
1447072Srrh
1457072Srrh e = hbuf;
1467072Srrh while(1) {
1477072Srrh save = e;
1487072Srrh if (*e == 0)return(0);
1497072Srrh w = wdstart;
1507072Srrh while((*e && (w <= hyend)) &&
1517072Srrh ((*e & 0177) == maplow(*w & CMASK))) {e++; w++;};
1527072Srrh if (!*e) {
1537072Srrh if (((w-1) == hyend) ||
1547072Srrh ((w == wdend) && (maplow(*w & CMASK) == 's'))) {
1557072Srrh w = wdstart;
1567072Srrh for(e = save; *e; e++) {
1577072Srrh if (*e & 0200)*hyp++ = w;
1587072Srrh if (hyp > (hyptr+NHYP-1))
1597072Srrh hyp = hyptr+NHYP-1;
1607072Srrh w++;
1617072Srrh }
1627072Srrh return(1);
1637072Srrh }else{e++; continue;}
1647072Srrh }else while(*e++);
1657072Srrh }
1667072Srrh }
1677072Srrh
suffix()1687072Srrh suffix()
1697072Srrh {
1707072Srrh register int *w;
1717072Srrh register char *s, *s0;
1727072Srrh int i;
1737072Srrh extern char *suftab[];
1747072Srrh extern int *chkvow();
1757072Srrh
1767072Srrh again:
1777072Srrh if (!alph(i = *hyend & CMASK))
1787072Srrh return(0);
1797072Srrh if (i < 'a')
1807072Srrh i -= 'A'-'a';
1817072Srrh if ((s0 = suftab[i-'a']) == 0)
1827072Srrh return(0);
1837072Srrh for (;;) {
1847072Srrh if ((i = *s0 & 017) == 0)
1857072Srrh return(0);
1867072Srrh s = s0 + i - 1;
1877072Srrh w = hyend - 1;
1887072Srrh while(((s > s0) && (w >= wdstart)) &&
1897072Srrh ((*s & 0177) == maplow(*w))) {
1907072Srrh s--;
1917072Srrh w--;
1927072Srrh }
1937072Srrh if (s == s0)
1947072Srrh break;
1957072Srrh s0 += i;
1967072Srrh }
1977072Srrh s = s0 + i - 1;
1987072Srrh w = hyend;
1997072Srrh if (*s0 & 0200) goto mark;
2007072Srrh while(s > s0) {
2017072Srrh w--;
2027072Srrh if (*s-- & 0200) {
2037072Srrh mark:
2047072Srrh hyend = w - 1;
2057072Srrh if (*s0 & 0100)
2067072Srrh continue;
2077072Srrh if (!chkvow(w))
2087072Srrh return(0);
2097072Srrh *hyp++ = w;
2107072Srrh }
2117072Srrh }
2127072Srrh if (*s0 & 040)
2137072Srrh return(0);
2147072Srrh if (exword())
2157072Srrh return(1);
2167072Srrh goto again;
2177072Srrh }
2187072Srrh
maplow(i)2197072Srrh maplow(i)
2207072Srrh int i;
2217072Srrh {
2227072Srrh if ((i &= CMASK) < 'a')i += 'a' - 'A';
2237072Srrh return(i);
2247072Srrh }
2257072Srrh
vowel(i)2267072Srrh vowel(i)
2277072Srrh int i;
2287072Srrh {
2297072Srrh switch(maplow(i)) {
2307072Srrh case 'a':
2317072Srrh case 'e':
2327072Srrh case 'i':
2337072Srrh case 'o':
2347072Srrh case 'u':
2357072Srrh case 'y':
2367072Srrh return(1);
2377072Srrh default:
2387072Srrh return(0);
2397072Srrh }
2407072Srrh }
2417072Srrh
chkvow(w)2427072Srrh int *chkvow(w)
2437072Srrh int *w;
2447072Srrh {
2457072Srrh while(--w >= wdstart)if(vowel(*w & CMASK))return(w);
2467072Srrh return(0);
2477072Srrh }
2487072Srrh
digram()2497072Srrh digram() {
2507072Srrh register *w, val;
2517072Srrh int *nhyend, *maxw, maxval;
2527072Srrh extern char bxh[26][13], bxxh[26][13], xxh[26][13], xhx[26][13], hxx[26][13];
2537072Srrh
2547072Srrh again:
2557072Srrh if (!(w=chkvow(hyend+1)))return;
2567072Srrh hyend = w;
2577072Srrh if (!(w=chkvow(hyend)))return;
2587072Srrh nhyend = w;
2597072Srrh maxval = 0;
2607072Srrh w--;
2617072Srrh while((++w < hyend) && (w < (wdend-1))) {
2627072Srrh val = 1;
2637072Srrh if (w == wdstart)val *= dilook('a',*w,bxh);
2647072Srrh else if(w == wdstart+1)val *= dilook(*(w-1),*w,bxxh);
2657072Srrh else val *= dilook(*(w-1),*w,xxh);
2667072Srrh val *= dilook(*w, *(w+1), xhx);
2677072Srrh val *= dilook(*(w+1), *(w+2), hxx);
2687072Srrh if (val > maxval) {
2697072Srrh maxval = val;
2707072Srrh maxw = w + 1;
2717072Srrh }
2727072Srrh }
2737072Srrh hyend = nhyend;
2747072Srrh if (maxval > thresh)*hyp++ = maxw;
2757072Srrh goto again;
2767072Srrh }
2777072Srrh
dilook(a,b,t)2787072Srrh dilook(a,b,t)
2797072Srrh int a, b;
2807072Srrh char t[26][13];
2817072Srrh {
2827072Srrh register i, j;
2837072Srrh
2847072Srrh i = t[maplow(a)-'a'][(j = maplow(b)-'a')/2];
2857072Srrh if (!(j & 01))i >>= 4;
2867072Srrh return(i & 017);
2877072Srrh }
288