xref: /csrg-svn/old/roff/common_source/n9.c (revision 29546)
17073Srrh #ifndef lint
2*29546Smckusick static char sccsid[] = "@(#)n9.c	4.2 07/07/86";
37073Srrh #endif lint
47073Srrh 
57073Srrh #include "tdef.h"
67073Srrh extern
77073Srrh #include "d.h"
87073Srrh extern
97073Srrh #include "v.h"
107073Srrh #ifdef NROFF
117073Srrh extern
127073Srrh #include "tw.h"
137073Srrh #endif
147073Srrh /*
157073Srrh troff9.c
167073Srrh 
177073Srrh misc functions
187073Srrh */
197073Srrh 
207073Srrh extern int cbuf[];
217073Srrh extern int *cp;
227073Srrh extern int ch;
237073Srrh extern int chbits;
247073Srrh extern int dfact;
257073Srrh extern int vflag;
267073Srrh extern int pts;
277073Srrh extern int fc;
287073Srrh extern int padc;
297073Srrh extern int tabtab[];
307073Srrh extern int nlflg;
317073Srrh extern int lss;
327073Srrh extern int tabch, ldrch;
337073Srrh extern int tabc, dotc;
347073Srrh extern int nchar, rchar;
357073Srrh extern int xxx;
367073Srrh 
377073Srrh setz(){
387073Srrh 	register i;
397073Srrh 
407073Srrh 	if(!((i = getch()) & MOT))i |= ZBIT;
417073Srrh 	return(i);
427073Srrh }
437073Srrh setline(){
447073Srrh 	register *i, length, c;
457073Srrh 	int w, cnt, delim, rem, temp;
467073Srrh 
477073Srrh 	if((delim = getch()) & MOT)return;
487073Srrh 		else delim &= CMASK;
497073Srrh 	vflag = 0;
507073Srrh 	dfact = EM;
517073Srrh 	length = quant(atoi(),HOR);
527073Srrh 	dfact = 1;
537073Srrh 	if(!length){
547073Srrh 		eat(delim);
557073Srrh 		return;
567073Srrh 	}
577073Srrh s0:
587073Srrh 	if(((c = getch()) & CMASK) == delim){
597073Srrh 		ch = c;
607073Srrh 		c = 0204 | chbits;
617073Srrh 	}else if((c & CMASK) == FILLER)goto s0;
627073Srrh 	w = width(c);
637073Srrh 	i = cbuf;
647073Srrh 	if(length < 0){
657073Srrh 		*i++ = makem(length);
667073Srrh 		length = -length;
677073Srrh 	}
687073Srrh 	if(!(cnt = length/w)){
697073Srrh 		*i++ = makem(-(temp = ((w-length)/2)));
707073Srrh 		*i++ = c;
717073Srrh 		*i++ = makem(-(w - length - temp));
727073Srrh 		goto s1;
737073Srrh 	}
747073Srrh 	if(rem = length%w){
757073Srrh 		switch(c & CMASK){
767073Srrh 			case 0204: /*rule*/
777073Srrh 			case 0224: /*underrule*/
787073Srrh 			case 0276: /*root en*/
797073Srrh 				*i++ = c | ZBIT;
807073Srrh 			default:
817073Srrh 				*i++ = makem(rem);
827073Srrh 		}
837073Srrh 	}
847073Srrh 	if(cnt){
857073Srrh 		*i++ = RPT;
867073Srrh 		*i++ = cnt;
877073Srrh 		*i++ = c;
887073Srrh 	}
897073Srrh s1:
907073Srrh 	*i++ = 0;
917073Srrh 	eat(delim);
927073Srrh 	cp = cbuf;
937073Srrh }
947073Srrh eat(c)
957073Srrh int c;
967073Srrh {
977073Srrh 	register i;
987073Srrh 
997073Srrh 	while(((i = getch() & CMASK) != c) &&
1007073Srrh 		(i != '\n'));
1017073Srrh 	return(i);
1027073Srrh }
1037073Srrh setov(){
1047073Srrh 	register i, j, k;
105*29546Smckusick 	int *p, delim, o[NOV+1], w[NOV+1];
1067073Srrh 
1077073Srrh 	if((delim = getch()) & MOT)return;
1087073Srrh 		else delim &= CMASK;
1097073Srrh 	for(k=0; (k<NOV) && ((j=(i = getch()) & CMASK) != delim) &&
1107073Srrh 		(j != '\n'); k++){
1117073Srrh 			o[k] = i;
1127073Srrh 			w[k] = width(i);
1137073Srrh 	}
1147073Srrh 	o[k] = w[k] = 0;
1157073Srrh 	if(o[0])for(j=1; j;){
1167073Srrh 		j = 0;
1177073Srrh 		for(k=1; o[k] ; k++){
1187073Srrh 			if(w[k-1] < w[k]){
1197073Srrh 				j++;
1207073Srrh 				i = w[k];
1217073Srrh 				w[k] = w[k-1];
1227073Srrh 				w[k-1] = i;
1237073Srrh 				i = o[k];
1247073Srrh 				o[k] = o[k-1];
1257073Srrh 				o[k-1] = i;
1267073Srrh 			}
1277073Srrh 		}
1287073Srrh 	}else return;
1297073Srrh 	p = cbuf;
1307073Srrh 	for(k=0; o[k]; k++){
1317073Srrh 		*p++ = o[k];
1327073Srrh 		*p++ = makem(-((w[k]+w[k+1])/2));
1337073Srrh 	}
1347073Srrh 	*p++ = makem(w[0]/2);
1357073Srrh 	*p = 0;
1367073Srrh 	cp = cbuf;
1377073Srrh }
1387073Srrh setbra(){
1397073Srrh 	register i, *j, k;
1407073Srrh 	int cnt, delim, dwn;
1417073Srrh 
1427073Srrh 	if((delim = getch()) & MOT)return;
1437073Srrh 		else delim &= CMASK;
1447073Srrh 	j = cbuf + 1;
1457073Srrh 	cnt = 0;
1467073Srrh #ifdef NROFF
1477073Srrh 	dwn = (2*t.Halfline) | MOT | VMOT;
1487073Srrh #endif
1497073Srrh #ifndef NROFF
1507073Srrh 	dwn = EM | MOT | VMOT;
1517073Srrh #endif
1527073Srrh 	while(((k = (i = getch()) & CMASK) != delim) && (k != '\n') &&
1537073Srrh 		(j <= (cbuf+NC-4))){
1547073Srrh 		*j++ = i | ZBIT;
1557073Srrh 		*j++ = dwn;
1567073Srrh 		cnt++;
1577073Srrh 	}
1587073Srrh 	if(--cnt < 0)return;
1597073Srrh 		else if (!cnt){
1607073Srrh 			ch = *(j-2);
1617073Srrh 			return;
1627073Srrh 	}
1637073Srrh 	*j = 0;
1647073Srrh #ifdef NROFF
1657073Srrh 	*--j = *cbuf = (cnt*t.Halfline) | MOT | NMOT | VMOT;
1667073Srrh #endif
1677073Srrh #ifndef NROFF
1687073Srrh 	*--j = *cbuf = (cnt*EM)/2 | MOT | NMOT | VMOT;
1697073Srrh #endif
1707073Srrh 	*--j &= ~ZBIT;
1717073Srrh 	cp = cbuf;
1727073Srrh }
1737073Srrh setvline(){
1747073Srrh 	register i, c, *k;
1757073Srrh 	int cnt, neg, rem, ver, delim;
1767073Srrh 
1777073Srrh 	if((delim = getch()) & MOT)return;
1787073Srrh 		else delim &= CMASK;
1797073Srrh 	dfact = lss;
1807073Srrh 	vflag++;
1817073Srrh 	i = quant(atoi(),VERT);
1827073Srrh 	dfact = 1;
1837073Srrh 	if(!i){
1847073Srrh 		eat(delim);
1857073Srrh 		vflag = 0;
1867073Srrh 		return;
1877073Srrh 	}
1887073Srrh 	if(((c = getch()) & CMASK) == delim){
1897073Srrh 		c = 0337 | chbits;	/*default box rule*/
1907073Srrh 	}else getch();
1917073Srrh 	c |= ZBIT;
1927073Srrh 	neg = 0;
1937073Srrh 	if(i < 0){
1947073Srrh 		i = -i;
1957073Srrh 		neg = NMOT;
1967073Srrh 	}
1977073Srrh #ifdef NROFF
1987073Srrh 	ver = 2*t.Halfline;
1997073Srrh #endif
2007073Srrh #ifndef NROFF
2017073Srrh 	ver = EM;
2027073Srrh #endif
2037073Srrh 	cnt = i/ver;
2047073Srrh 	rem = makem(i%ver) | neg;
2057073Srrh 	ver = makem(ver) | neg;
2067073Srrh 	k = cbuf;
2077073Srrh 	if(!neg)*k++ = ver;
2087073Srrh 	if(rem & ~MOTV){
2097073Srrh 		*k++ = c;
2107073Srrh 		*k++ = rem;
2117073Srrh 	}
2127073Srrh 	while((k < (cbuf+NC-3)) && cnt--){
2137073Srrh 		*k++ = c;
2147073Srrh 		*k++ = ver;
2157073Srrh 	}
2167073Srrh 	*(k-2) &= ~ZBIT;
2177073Srrh 	if(!neg)k--;
2187073Srrh 	*k = 0;
2197073Srrh 	cp = cbuf;
2207073Srrh 	vflag = 0;
2217073Srrh }
2227073Srrh casefc(){
2237073Srrh 	register i;
2247073Srrh 
2257073Srrh 	fc = IMP;
2267073Srrh 	padc = ' ';
2277073Srrh 	if(skip() ||
2287073Srrh 	   ((i = getch()) & MOT) ||
2297073Srrh 	   ((i &= CMASK) == '\n'))return;
2307073Srrh 	fc = i;
2317073Srrh 	if(skip() || (ch & MOT) || ((ch &= CMASK) == fc))return;
2327073Srrh 	padc = ch;
2337073Srrh }
2347073Srrh setfield(x)
2357073Srrh int x;
2367073Srrh {
2377073Srrh 	register i, j, *fp;
2387073Srrh 	int length, ws, npad, temp, type;
2397073Srrh 	int **pp, *padptr[NPP];
2407073Srrh 	static int fbuf[FBUFSZ];
2417073Srrh 	int savfc, savtc, savlc;
2427073Srrh 
2437073Srrh 	if(x == tabch) rchar = tabc | chbits;
2447073Srrh 	else if(x ==  ldrch) rchar = dotc | chbits;
2457073Srrh 	temp = npad = ws = 0;
2467073Srrh 	savfc = fc; savtc = tabch; savlc = ldrch;
2477073Srrh 	tabch = ldrch = fc = IMP;
2487073Srrh 	for(j=0;;j++){
2497073Srrh 		if((tabtab[j] & TMASK)== 0){
2507073Srrh 			if(x==savfc)prstr("Zero field width.\n");
2517073Srrh 			j = 0;
2527073Srrh 			goto rtn;
2537073Srrh 		}
2547073Srrh 		v.hp = sumhp();	/* XXX */
2557073Srrh 		if((length = ((tabtab[j] & TMASK) - v.hp)) > 0 )break;
2567073Srrh 	}
2577073Srrh 	type = tabtab[j] & (~TMASK);
2587073Srrh 	fp = fbuf;
2597073Srrh 	pp = padptr;
2607073Srrh 	if(x == savfc){while(1){
2617073Srrh 		if(((j = (i = getch()) & CMASK)) == padc){
2627073Srrh 			npad++;
2637073Srrh 			*pp++ = fp;
2647073Srrh 			if(pp > (padptr + NPP - 1))break;
2657073Srrh 			goto s1;
2667073Srrh 		}else if(j == savfc) break;
2677073Srrh 			else if(j == '\n'){
2687073Srrh 				temp = j;
2697073Srrh 				nlflg = 0;
2707073Srrh 				break;
2717073Srrh 			}
2727073Srrh 		ws += width(i);
2737073Srrh 	s1:
2747073Srrh 		*fp++ = i;
2757073Srrh 		if(fp > (fbuf + FBUFSZ -3))break;
2767073Srrh 	}
2777073Srrh 	if(!npad){
2787073Srrh 		npad++;
2797073Srrh 		*pp++ = fp;
2807073Srrh 		*fp++ = 0;
2817073Srrh 	}
2827073Srrh 	*fp++ = temp;
2837073Srrh 	*fp++ = 0;
2847073Srrh 	temp = i = (j = length-ws)/npad;
2857073Srrh 	i = (i/HOR)*HOR;
2867073Srrh 	if((j -= i*npad) <0)j = -j;
2877073Srrh 	i = makem(i);
2887073Srrh 	if(temp <0)i |= NMOT;
2897073Srrh 	for(;npad > 0; npad--){
2907073Srrh 		*(*--pp) = i;
2917073Srrh 		if(j){
2927073Srrh 			j -= HOR;
2937073Srrh 			(*(*pp)) += HOR;
2947073Srrh 		}
2957073Srrh 	}
2967073Srrh 	cp = fbuf;
2977073Srrh 	j = 0;
2987073Srrh 	}else if(type == 0){
2997073Srrh 	/*plain tab or leader*/
3007073Srrh 		if((j = width(rchar)) == 0)nchar = 0;
3017073Srrh 		else{
3027073Srrh 			nchar = length /j;
3037073Srrh 			length %= j;
3047073Srrh 		}
3057073Srrh 		if(length)j = length | MOT;
3067073Srrh 		else j = getch0();
3077073Srrh 	}else{
3087073Srrh 	/*center tab*/
3097073Srrh 	/*right tab*/
3107073Srrh 		while(((j = (i = getch()) & CMASK) != savtc) &&
3117073Srrh 			(j != '\n') && (j != savlc)){
3127073Srrh 			ws += width(i);
3137073Srrh 			*fp++ = i;
3147073Srrh 			if(fp > (fbuf +FBUFSZ - 3)) break;
3157073Srrh 		}
3167073Srrh 		*fp++ = i;
3177073Srrh 		*fp++ = 0;
3187073Srrh 		if(type == RTAB)length -= ws;
3197073Srrh 		else length -= ws/2; /*CTAB*/
3207073Srrh 		if(((j = width(rchar)) == 0) || (length <= 0))nchar = 0;
3217073Srrh 		else{
3227073Srrh 			nchar = length/j;
3237073Srrh 			length %= j;
3247073Srrh 		}
3257073Srrh 		length = (length/HOR)*HOR;
3267073Srrh 		j = makem(length);
3277073Srrh 		cp = fbuf;
3287073Srrh 		nlflg = 0;
3297073Srrh 	}
3307073Srrh rtn:
3317073Srrh 	fc = savfc; tabch = savtc; ldrch = savlc;
3327073Srrh 	return(j);
3337073Srrh }
334