xref: /csrg-svn/old/roff/common_source/n9.c (revision 7073)
1*7073Srrh #ifndef lint
2*7073Srrh static char sccsid[] = "@(#)n9.c	4.1 06/07/82";
3*7073Srrh #endif lint
4*7073Srrh 
5*7073Srrh #include "tdef.h"
6*7073Srrh extern
7*7073Srrh #include "d.h"
8*7073Srrh extern
9*7073Srrh #include "v.h"
10*7073Srrh #ifdef NROFF
11*7073Srrh extern
12*7073Srrh #include "tw.h"
13*7073Srrh #endif
14*7073Srrh /*
15*7073Srrh troff9.c
16*7073Srrh 
17*7073Srrh misc functions
18*7073Srrh */
19*7073Srrh 
20*7073Srrh extern int cbuf[];
21*7073Srrh extern int *cp;
22*7073Srrh extern int ch;
23*7073Srrh extern int chbits;
24*7073Srrh extern int dfact;
25*7073Srrh extern int vflag;
26*7073Srrh extern int pts;
27*7073Srrh extern int fc;
28*7073Srrh extern int padc;
29*7073Srrh extern int tabtab[];
30*7073Srrh extern int nlflg;
31*7073Srrh extern int lss;
32*7073Srrh extern int tabch, ldrch;
33*7073Srrh extern int tabc, dotc;
34*7073Srrh extern int nchar, rchar;
35*7073Srrh extern int xxx;
36*7073Srrh 
37*7073Srrh setz(){
38*7073Srrh 	register i;
39*7073Srrh 
40*7073Srrh 	if(!((i = getch()) & MOT))i |= ZBIT;
41*7073Srrh 	return(i);
42*7073Srrh }
43*7073Srrh setline(){
44*7073Srrh 	register *i, length, c;
45*7073Srrh 	int w, cnt, delim, rem, temp;
46*7073Srrh 
47*7073Srrh 	if((delim = getch()) & MOT)return;
48*7073Srrh 		else delim &= CMASK;
49*7073Srrh 	vflag = 0;
50*7073Srrh 	dfact = EM;
51*7073Srrh 	length = quant(atoi(),HOR);
52*7073Srrh 	dfact = 1;
53*7073Srrh 	if(!length){
54*7073Srrh 		eat(delim);
55*7073Srrh 		return;
56*7073Srrh 	}
57*7073Srrh s0:
58*7073Srrh 	if(((c = getch()) & CMASK) == delim){
59*7073Srrh 		ch = c;
60*7073Srrh 		c = 0204 | chbits;
61*7073Srrh 	}else if((c & CMASK) == FILLER)goto s0;
62*7073Srrh 	w = width(c);
63*7073Srrh 	i = cbuf;
64*7073Srrh 	if(length < 0){
65*7073Srrh 		*i++ = makem(length);
66*7073Srrh 		length = -length;
67*7073Srrh 	}
68*7073Srrh 	if(!(cnt = length/w)){
69*7073Srrh 		*i++ = makem(-(temp = ((w-length)/2)));
70*7073Srrh 		*i++ = c;
71*7073Srrh 		*i++ = makem(-(w - length - temp));
72*7073Srrh 		goto s1;
73*7073Srrh 	}
74*7073Srrh 	if(rem = length%w){
75*7073Srrh 		switch(c & CMASK){
76*7073Srrh 			case 0204: /*rule*/
77*7073Srrh 			case 0224: /*underrule*/
78*7073Srrh 			case 0276: /*root en*/
79*7073Srrh 				*i++ = c | ZBIT;
80*7073Srrh 			default:
81*7073Srrh 				*i++ = makem(rem);
82*7073Srrh 		}
83*7073Srrh 	}
84*7073Srrh 	if(cnt){
85*7073Srrh 		*i++ = RPT;
86*7073Srrh 		*i++ = cnt;
87*7073Srrh 		*i++ = c;
88*7073Srrh 	}
89*7073Srrh s1:
90*7073Srrh 	*i++ = 0;
91*7073Srrh 	eat(delim);
92*7073Srrh 	cp = cbuf;
93*7073Srrh }
94*7073Srrh eat(c)
95*7073Srrh int c;
96*7073Srrh {
97*7073Srrh 	register i;
98*7073Srrh 
99*7073Srrh 	while(((i = getch() & CMASK) != c) &&
100*7073Srrh 		(i != '\n'));
101*7073Srrh 	return(i);
102*7073Srrh }
103*7073Srrh setov(){
104*7073Srrh 	register i, j, k;
105*7073Srrh 	int *p, delim, o[NOV], w[NOV];
106*7073Srrh 
107*7073Srrh 	if((delim = getch()) & MOT)return;
108*7073Srrh 		else delim &= CMASK;
109*7073Srrh 	for(k=0; (k<NOV) && ((j=(i = getch()) & CMASK) != delim) &&
110*7073Srrh 		(j != '\n'); k++){
111*7073Srrh 			o[k] = i;
112*7073Srrh 			w[k] = width(i);
113*7073Srrh 	}
114*7073Srrh 	o[k] = w[k] = 0;
115*7073Srrh 	if(o[0])for(j=1; j;){
116*7073Srrh 		j = 0;
117*7073Srrh 		for(k=1; o[k] ; k++){
118*7073Srrh 			if(w[k-1] < w[k]){
119*7073Srrh 				j++;
120*7073Srrh 				i = w[k];
121*7073Srrh 				w[k] = w[k-1];
122*7073Srrh 				w[k-1] = i;
123*7073Srrh 				i = o[k];
124*7073Srrh 				o[k] = o[k-1];
125*7073Srrh 				o[k-1] = i;
126*7073Srrh 			}
127*7073Srrh 		}
128*7073Srrh 	}else return;
129*7073Srrh 	p = cbuf;
130*7073Srrh 	for(k=0; o[k]; k++){
131*7073Srrh 		*p++ = o[k];
132*7073Srrh 		*p++ = makem(-((w[k]+w[k+1])/2));
133*7073Srrh 	}
134*7073Srrh 	*p++ = makem(w[0]/2);
135*7073Srrh 	*p = 0;
136*7073Srrh 	cp = cbuf;
137*7073Srrh }
138*7073Srrh setbra(){
139*7073Srrh 	register i, *j, k;
140*7073Srrh 	int cnt, delim, dwn;
141*7073Srrh 
142*7073Srrh 	if((delim = getch()) & MOT)return;
143*7073Srrh 		else delim &= CMASK;
144*7073Srrh 	j = cbuf + 1;
145*7073Srrh 	cnt = 0;
146*7073Srrh #ifdef NROFF
147*7073Srrh 	dwn = (2*t.Halfline) | MOT | VMOT;
148*7073Srrh #endif
149*7073Srrh #ifndef NROFF
150*7073Srrh 	dwn = EM | MOT | VMOT;
151*7073Srrh #endif
152*7073Srrh 	while(((k = (i = getch()) & CMASK) != delim) && (k != '\n') &&
153*7073Srrh 		(j <= (cbuf+NC-4))){
154*7073Srrh 		*j++ = i | ZBIT;
155*7073Srrh 		*j++ = dwn;
156*7073Srrh 		cnt++;
157*7073Srrh 	}
158*7073Srrh 	if(--cnt < 0)return;
159*7073Srrh 		else if (!cnt){
160*7073Srrh 			ch = *(j-2);
161*7073Srrh 			return;
162*7073Srrh 	}
163*7073Srrh 	*j = 0;
164*7073Srrh #ifdef NROFF
165*7073Srrh 	*--j = *cbuf = (cnt*t.Halfline) | MOT | NMOT | VMOT;
166*7073Srrh #endif
167*7073Srrh #ifndef NROFF
168*7073Srrh 	*--j = *cbuf = (cnt*EM)/2 | MOT | NMOT | VMOT;
169*7073Srrh #endif
170*7073Srrh 	*--j &= ~ZBIT;
171*7073Srrh 	cp = cbuf;
172*7073Srrh }
173*7073Srrh setvline(){
174*7073Srrh 	register i, c, *k;
175*7073Srrh 	int cnt, neg, rem, ver, delim;
176*7073Srrh 
177*7073Srrh 	if((delim = getch()) & MOT)return;
178*7073Srrh 		else delim &= CMASK;
179*7073Srrh 	dfact = lss;
180*7073Srrh 	vflag++;
181*7073Srrh 	i = quant(atoi(),VERT);
182*7073Srrh 	dfact = 1;
183*7073Srrh 	if(!i){
184*7073Srrh 		eat(delim);
185*7073Srrh 		vflag = 0;
186*7073Srrh 		return;
187*7073Srrh 	}
188*7073Srrh 	if(((c = getch()) & CMASK) == delim){
189*7073Srrh 		c = 0337 | chbits;	/*default box rule*/
190*7073Srrh 	}else getch();
191*7073Srrh 	c |= ZBIT;
192*7073Srrh 	neg = 0;
193*7073Srrh 	if(i < 0){
194*7073Srrh 		i = -i;
195*7073Srrh 		neg = NMOT;
196*7073Srrh 	}
197*7073Srrh #ifdef NROFF
198*7073Srrh 	ver = 2*t.Halfline;
199*7073Srrh #endif
200*7073Srrh #ifndef NROFF
201*7073Srrh 	ver = EM;
202*7073Srrh #endif
203*7073Srrh 	cnt = i/ver;
204*7073Srrh 	rem = makem(i%ver) | neg;
205*7073Srrh 	ver = makem(ver) | neg;
206*7073Srrh 	k = cbuf;
207*7073Srrh 	if(!neg)*k++ = ver;
208*7073Srrh 	if(rem & ~MOTV){
209*7073Srrh 		*k++ = c;
210*7073Srrh 		*k++ = rem;
211*7073Srrh 	}
212*7073Srrh 	while((k < (cbuf+NC-3)) && cnt--){
213*7073Srrh 		*k++ = c;
214*7073Srrh 		*k++ = ver;
215*7073Srrh 	}
216*7073Srrh 	*(k-2) &= ~ZBIT;
217*7073Srrh 	if(!neg)k--;
218*7073Srrh 	*k = 0;
219*7073Srrh 	cp = cbuf;
220*7073Srrh 	vflag = 0;
221*7073Srrh }
222*7073Srrh casefc(){
223*7073Srrh 	register i;
224*7073Srrh 
225*7073Srrh 	fc = IMP;
226*7073Srrh 	padc = ' ';
227*7073Srrh 	if(skip() ||
228*7073Srrh 	   ((i = getch()) & MOT) ||
229*7073Srrh 	   ((i &= CMASK) == '\n'))return;
230*7073Srrh 	fc = i;
231*7073Srrh 	if(skip() || (ch & MOT) || ((ch &= CMASK) == fc))return;
232*7073Srrh 	padc = ch;
233*7073Srrh }
234*7073Srrh setfield(x)
235*7073Srrh int x;
236*7073Srrh {
237*7073Srrh 	register i, j, *fp;
238*7073Srrh 	int length, ws, npad, temp, type;
239*7073Srrh 	int **pp, *padptr[NPP];
240*7073Srrh 	static int fbuf[FBUFSZ];
241*7073Srrh 	int savfc, savtc, savlc;
242*7073Srrh 
243*7073Srrh 	if(x == tabch) rchar = tabc | chbits;
244*7073Srrh 	else if(x ==  ldrch) rchar = dotc | chbits;
245*7073Srrh 	temp = npad = ws = 0;
246*7073Srrh 	savfc = fc; savtc = tabch; savlc = ldrch;
247*7073Srrh 	tabch = ldrch = fc = IMP;
248*7073Srrh 	for(j=0;;j++){
249*7073Srrh 		if((tabtab[j] & TMASK)== 0){
250*7073Srrh 			if(x==savfc)prstr("Zero field width.\n");
251*7073Srrh 			j = 0;
252*7073Srrh 			goto rtn;
253*7073Srrh 		}
254*7073Srrh 		v.hp = sumhp();	/* XXX */
255*7073Srrh 		if((length = ((tabtab[j] & TMASK) - v.hp)) > 0 )break;
256*7073Srrh 	}
257*7073Srrh 	type = tabtab[j] & (~TMASK);
258*7073Srrh 	fp = fbuf;
259*7073Srrh 	pp = padptr;
260*7073Srrh 	if(x == savfc){while(1){
261*7073Srrh 		if(((j = (i = getch()) & CMASK)) == padc){
262*7073Srrh 			npad++;
263*7073Srrh 			*pp++ = fp;
264*7073Srrh 			if(pp > (padptr + NPP - 1))break;
265*7073Srrh 			goto s1;
266*7073Srrh 		}else if(j == savfc) break;
267*7073Srrh 			else if(j == '\n'){
268*7073Srrh 				temp = j;
269*7073Srrh 				nlflg = 0;
270*7073Srrh 				break;
271*7073Srrh 			}
272*7073Srrh 		ws += width(i);
273*7073Srrh 	s1:
274*7073Srrh 		*fp++ = i;
275*7073Srrh 		if(fp > (fbuf + FBUFSZ -3))break;
276*7073Srrh 	}
277*7073Srrh 	if(!npad){
278*7073Srrh 		npad++;
279*7073Srrh 		*pp++ = fp;
280*7073Srrh 		*fp++ = 0;
281*7073Srrh 	}
282*7073Srrh 	*fp++ = temp;
283*7073Srrh 	*fp++ = 0;
284*7073Srrh 	temp = i = (j = length-ws)/npad;
285*7073Srrh 	i = (i/HOR)*HOR;
286*7073Srrh 	if((j -= i*npad) <0)j = -j;
287*7073Srrh 	i = makem(i);
288*7073Srrh 	if(temp <0)i |= NMOT;
289*7073Srrh 	for(;npad > 0; npad--){
290*7073Srrh 		*(*--pp) = i;
291*7073Srrh 		if(j){
292*7073Srrh 			j -= HOR;
293*7073Srrh 			(*(*pp)) += HOR;
294*7073Srrh 		}
295*7073Srrh 	}
296*7073Srrh 	cp = fbuf;
297*7073Srrh 	j = 0;
298*7073Srrh 	}else if(type == 0){
299*7073Srrh 	/*plain tab or leader*/
300*7073Srrh 		if((j = width(rchar)) == 0)nchar = 0;
301*7073Srrh 		else{
302*7073Srrh 			nchar = length /j;
303*7073Srrh 			length %= j;
304*7073Srrh 		}
305*7073Srrh 		if(length)j = length | MOT;
306*7073Srrh 		else j = getch0();
307*7073Srrh 	}else{
308*7073Srrh 	/*center tab*/
309*7073Srrh 	/*right tab*/
310*7073Srrh 		while(((j = (i = getch()) & CMASK) != savtc) &&
311*7073Srrh 			(j != '\n') && (j != savlc)){
312*7073Srrh 			ws += width(i);
313*7073Srrh 			*fp++ = i;
314*7073Srrh 			if(fp > (fbuf +FBUFSZ - 3)) break;
315*7073Srrh 		}
316*7073Srrh 		*fp++ = i;
317*7073Srrh 		*fp++ = 0;
318*7073Srrh 		if(type == RTAB)length -= ws;
319*7073Srrh 		else length -= ws/2; /*CTAB*/
320*7073Srrh 		if(((j = width(rchar)) == 0) || (length <= 0))nchar = 0;
321*7073Srrh 		else{
322*7073Srrh 			nchar = length/j;
323*7073Srrh 			length %= j;
324*7073Srrh 		}
325*7073Srrh 		length = (length/HOR)*HOR;
326*7073Srrh 		j = makem(length);
327*7073Srrh 		cp = fbuf;
328*7073Srrh 		nlflg = 0;
329*7073Srrh 	}
330*7073Srrh rtn:
331*7073Srrh 	fc = savfc; tabch = savtc; ldrch = savlc;
332*7073Srrh 	return(j);
333*7073Srrh }
334