xref: /illumos-gate/usr/src/cmd/troff/n3.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate  * The Regents of the University of California
337c478bd9Sstevel@tonic-gate  * All Rights Reserved
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate  * contributors.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * troff3.c
42*2a8bcb4eSToomas Soome  *
437c478bd9Sstevel@tonic-gate  * macro and string routines, storage allocation
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #include "tdef.h"
487c478bd9Sstevel@tonic-gate #ifdef NROFF
497c478bd9Sstevel@tonic-gate #include "tw.h"
507c478bd9Sstevel@tonic-gate #endif
517c478bd9Sstevel@tonic-gate #include "ext.h"
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #define	MHASH(x)	((x>>6)^x)&0177
547c478bd9Sstevel@tonic-gate struct	contab *mhash[128];	/* 128 == the 0177 on line above */
557c478bd9Sstevel@tonic-gate #define	blisti(i)	(((i)-ENV_BLK*BLK) / BLK)
567c478bd9Sstevel@tonic-gate filep	blist[NBLIST];
577c478bd9Sstevel@tonic-gate tchar	*argtop;
587c478bd9Sstevel@tonic-gate int	pagech = '%';
597c478bd9Sstevel@tonic-gate int	strflg;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #ifdef	INCORE
627c478bd9Sstevel@tonic-gate 	tchar *wbuf;
637c478bd9Sstevel@tonic-gate 	tchar corebuf[(ENV_BLK + NBLIST + 1) * BLK];
647c478bd9Sstevel@tonic-gate #else
657c478bd9Sstevel@tonic-gate 	tchar wbuf[BLK];
667c478bd9Sstevel@tonic-gate 	tchar rbuf[BLK];
677c478bd9Sstevel@tonic-gate #endif
687c478bd9Sstevel@tonic-gate 
69e5190c10Smuffin int
caseig()707c478bd9Sstevel@tonic-gate caseig()
717c478bd9Sstevel@tonic-gate {
72e5190c10Smuffin 	int	i;
73e5190c10Smuffin 	filep oldoff;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	oldoff = offset;
767c478bd9Sstevel@tonic-gate 	offset = 0;
777c478bd9Sstevel@tonic-gate 	i = copyb();
787c478bd9Sstevel@tonic-gate 	offset = oldoff;
797c478bd9Sstevel@tonic-gate 	if (i != '.')
807c478bd9Sstevel@tonic-gate 		control(i, 1);
817c478bd9Sstevel@tonic-gate 
82e5190c10Smuffin 	return (0);
83e5190c10Smuffin }
847c478bd9Sstevel@tonic-gate 
85e5190c10Smuffin int
casern()867c478bd9Sstevel@tonic-gate casern()
877c478bd9Sstevel@tonic-gate {
88e5190c10Smuffin 	int	i, j;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	lgf++;
917c478bd9Sstevel@tonic-gate 	skip();
927c478bd9Sstevel@tonic-gate 	if ((i = getrq()) == 0 || (oldmn = findmn(i)) < 0)
93e5190c10Smuffin 		return (0);
947c478bd9Sstevel@tonic-gate 	skip();
957c478bd9Sstevel@tonic-gate 	clrmn(findmn(j = getrq()));
967c478bd9Sstevel@tonic-gate 	if (j) {
977c478bd9Sstevel@tonic-gate 		munhash(&contab[oldmn]);
987c478bd9Sstevel@tonic-gate 		contab[oldmn].rq = j;
997c478bd9Sstevel@tonic-gate 		maddhash(&contab[oldmn]);
1007c478bd9Sstevel@tonic-gate 	}
101e5190c10Smuffin 
102e5190c10Smuffin 	return (0);
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
105e5190c10Smuffin int
maddhash(rp)1067c478bd9Sstevel@tonic-gate maddhash(rp)
107e5190c10Smuffin struct contab *rp;
1087c478bd9Sstevel@tonic-gate {
109e5190c10Smuffin 	struct contab **hp;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	if (rp->rq == 0)
112e5190c10Smuffin 		return (0);
1137c478bd9Sstevel@tonic-gate 	hp = &mhash[MHASH(rp->rq)];
1147c478bd9Sstevel@tonic-gate 	rp->link = *hp;
1157c478bd9Sstevel@tonic-gate 	*hp = rp;
116e5190c10Smuffin 
117e5190c10Smuffin 	return (0);
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate 
120e5190c10Smuffin int
munhash(mp)1217c478bd9Sstevel@tonic-gate munhash(mp)
122e5190c10Smuffin struct contab *mp;
123*2a8bcb4eSToomas Soome {
124e5190c10Smuffin 	struct contab *p;
125e5190c10Smuffin 	struct contab **lp;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	if (mp->rq == 0)
128e5190c10Smuffin 		return (0);
1297c478bd9Sstevel@tonic-gate 	lp = &mhash[MHASH(mp->rq)];
1307c478bd9Sstevel@tonic-gate 	p = *lp;
1317c478bd9Sstevel@tonic-gate 	while (p) {
1327c478bd9Sstevel@tonic-gate 		if (p == mp) {
1337c478bd9Sstevel@tonic-gate 			*lp = p->link;
1347c478bd9Sstevel@tonic-gate 			p->link = 0;
135e5190c10Smuffin 			return (0);
1367c478bd9Sstevel@tonic-gate 		}
1377c478bd9Sstevel@tonic-gate 		lp = &p->link;
1387c478bd9Sstevel@tonic-gate 		p = p->link;
1397c478bd9Sstevel@tonic-gate 	}
140e5190c10Smuffin 
141e5190c10Smuffin 	return (0);
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate 
144e5190c10Smuffin int
mrehash()1457c478bd9Sstevel@tonic-gate mrehash()
1467c478bd9Sstevel@tonic-gate {
147e5190c10Smuffin 	struct contab *p;
148e5190c10Smuffin 	int	i;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	for (i=0; i<128; i++)
1517c478bd9Sstevel@tonic-gate 		mhash[i] = 0;
1527c478bd9Sstevel@tonic-gate 	for (p=contab; p < &contab[NM]; p++)
1537c478bd9Sstevel@tonic-gate 		p->link = 0;
1547c478bd9Sstevel@tonic-gate 	for (p=contab; p < &contab[NM]; p++) {
1557c478bd9Sstevel@tonic-gate 		if (p->rq == 0)
1567c478bd9Sstevel@tonic-gate 			continue;
1577c478bd9Sstevel@tonic-gate 		i = MHASH(p->rq);
1587c478bd9Sstevel@tonic-gate 		p->link = mhash[i];
1597c478bd9Sstevel@tonic-gate 		mhash[i] = p;
1607c478bd9Sstevel@tonic-gate 	}
161e5190c10Smuffin 
162e5190c10Smuffin 	return (0);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate 
165e5190c10Smuffin int
caserm()1667c478bd9Sstevel@tonic-gate caserm()
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate 	int j;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	lgf++;
1717c478bd9Sstevel@tonic-gate 	while (!skip() && (j = getrq()) != 0)
1727c478bd9Sstevel@tonic-gate 		clrmn(findmn(j));
1737c478bd9Sstevel@tonic-gate 	lgf--;
174e5190c10Smuffin 
175e5190c10Smuffin 	return (0);
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 
179e5190c10Smuffin int
caseas()1807c478bd9Sstevel@tonic-gate caseas()
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate 	app++;
1837c478bd9Sstevel@tonic-gate 	caseds();
184e5190c10Smuffin 
185e5190c10Smuffin 	return (0);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 
189e5190c10Smuffin int
caseds()1907c478bd9Sstevel@tonic-gate caseds()
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate 	ds++;
1937c478bd9Sstevel@tonic-gate 	casede();
194e5190c10Smuffin 
195e5190c10Smuffin 	return (0);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 
199e5190c10Smuffin int
caseam()2007c478bd9Sstevel@tonic-gate caseam()
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	app++;
2037c478bd9Sstevel@tonic-gate 	casede();
204e5190c10Smuffin 
205e5190c10Smuffin 	return (0);
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 
209e5190c10Smuffin int
casede()2107c478bd9Sstevel@tonic-gate casede()
2117c478bd9Sstevel@tonic-gate {
212e5190c10Smuffin 	int	i, req;
213e5190c10Smuffin 	filep savoff;
2147c478bd9Sstevel@tonic-gate 	extern filep finds();
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	if (dip != d)
2177c478bd9Sstevel@tonic-gate 		wbfl();
2187c478bd9Sstevel@tonic-gate 	req = '.';
2197c478bd9Sstevel@tonic-gate 	lgf++;
2207c478bd9Sstevel@tonic-gate 	skip();
2217c478bd9Sstevel@tonic-gate 	if ((i = getrq()) == 0)
2227c478bd9Sstevel@tonic-gate 		goto de1;
2237c478bd9Sstevel@tonic-gate 	if ((offset = finds(i)) == 0)
2247c478bd9Sstevel@tonic-gate 		goto de1;
2257c478bd9Sstevel@tonic-gate 	if (ds)
2267c478bd9Sstevel@tonic-gate 		copys();
227*2a8bcb4eSToomas Soome 	else
2287c478bd9Sstevel@tonic-gate 		req = copyb();
2297c478bd9Sstevel@tonic-gate 	wbfl();
2307c478bd9Sstevel@tonic-gate 	clrmn(oldmn);
2317c478bd9Sstevel@tonic-gate 	if (newmn) {
2327c478bd9Sstevel@tonic-gate 		if (contab[newmn].rq)
2337c478bd9Sstevel@tonic-gate 			munhash(&contab[newmn]);
2347c478bd9Sstevel@tonic-gate 		contab[newmn].rq = i;
2357c478bd9Sstevel@tonic-gate 		maddhash(&contab[newmn]);
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate 	if (apptr) {
2387c478bd9Sstevel@tonic-gate 		savoff = offset;
2397c478bd9Sstevel@tonic-gate 		offset = apptr;
2407c478bd9Sstevel@tonic-gate 		wbt((tchar) IMP);
2417c478bd9Sstevel@tonic-gate 		offset = savoff;
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate 	offset = dip->op;
2447c478bd9Sstevel@tonic-gate 	if (req != '.')
2457c478bd9Sstevel@tonic-gate 		control(req, 1);
2467c478bd9Sstevel@tonic-gate de1:
2477c478bd9Sstevel@tonic-gate 	ds = app = 0;
248e5190c10Smuffin 	return (0);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 
252e5190c10Smuffin int
findmn(i)2537c478bd9Sstevel@tonic-gate findmn(i)
254e5190c10Smuffin int	i;
2557c478bd9Sstevel@tonic-gate {
256e5190c10Smuffin 	struct contab *p;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	for (p = mhash[MHASH(i)]; p; p = p->link)
2597c478bd9Sstevel@tonic-gate 		if (i == p->rq)
2607c478bd9Sstevel@tonic-gate 			return(p - contab);
2617c478bd9Sstevel@tonic-gate 	return(-1);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 
265e5190c10Smuffin int
clrmn(i)2667c478bd9Sstevel@tonic-gate clrmn(i)
267e5190c10Smuffin int	i;
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate 	if (i >= 0) {
2707c478bd9Sstevel@tonic-gate 		if (contab[i].mx)
2717c478bd9Sstevel@tonic-gate 			ffree((filep)contab[i].mx);
2727c478bd9Sstevel@tonic-gate 		munhash(&contab[i]);
2737c478bd9Sstevel@tonic-gate 		contab[i].rq = 0;
2747c478bd9Sstevel@tonic-gate 		contab[i].mx = 0;
2757c478bd9Sstevel@tonic-gate 		contab[i].f = 0;
2767c478bd9Sstevel@tonic-gate 	}
277e5190c10Smuffin 
278e5190c10Smuffin 	return (0);
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 
finds(mn)2827c478bd9Sstevel@tonic-gate filep finds(mn)
283e5190c10Smuffin int	mn;
2847c478bd9Sstevel@tonic-gate {
285e5190c10Smuffin 	int	i;
286e5190c10Smuffin 	filep savip;
2877c478bd9Sstevel@tonic-gate 	extern filep alloc();
2887c478bd9Sstevel@tonic-gate 	extern filep incoff();
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	oldmn = findmn(mn);
2917c478bd9Sstevel@tonic-gate 	newmn = 0;
2927c478bd9Sstevel@tonic-gate 	apptr = (filep)0;
2937c478bd9Sstevel@tonic-gate 	if (app && oldmn >= 0 && contab[oldmn].mx) {
2947c478bd9Sstevel@tonic-gate 		savip = ip;
2957c478bd9Sstevel@tonic-gate 		ip = (filep)contab[oldmn].mx;
2967c478bd9Sstevel@tonic-gate 		oldmn = -1;
2977c478bd9Sstevel@tonic-gate 		while ((i = rbf()) != 0)
2987c478bd9Sstevel@tonic-gate 			;
2997c478bd9Sstevel@tonic-gate 		apptr = ip;
3007c478bd9Sstevel@tonic-gate 		if (!diflg)
3017c478bd9Sstevel@tonic-gate 			ip = incoff(ip);
3027c478bd9Sstevel@tonic-gate 		nextb = ip;
3037c478bd9Sstevel@tonic-gate 		ip = savip;
3047c478bd9Sstevel@tonic-gate 	} else {
3057c478bd9Sstevel@tonic-gate 		for (i = 0; i < NM; i++) {
3067c478bd9Sstevel@tonic-gate 			if (contab[i].rq == 0)
3077c478bd9Sstevel@tonic-gate 				break;
3087c478bd9Sstevel@tonic-gate 		}
3097c478bd9Sstevel@tonic-gate 		if (i == NM || (nextb = alloc()) == 0) {
3107c478bd9Sstevel@tonic-gate 			app = 0;
3117c478bd9Sstevel@tonic-gate 			if (macerr++ > 1)
3127c478bd9Sstevel@tonic-gate 				done2(02);
3137c478bd9Sstevel@tonic-gate 			errprint(gettext("Too many (%d) string/macro names"),
3147c478bd9Sstevel@tonic-gate 					 NM);
3157c478bd9Sstevel@tonic-gate 			edone(04);
3167c478bd9Sstevel@tonic-gate 			return(offset = 0);
3177c478bd9Sstevel@tonic-gate 		}
3187c478bd9Sstevel@tonic-gate 		contab[i].mx = (unsigned) nextb;
3197c478bd9Sstevel@tonic-gate 		if (!diflg) {
3207c478bd9Sstevel@tonic-gate 			newmn = i;
3217c478bd9Sstevel@tonic-gate 			if (oldmn == -1)
3227c478bd9Sstevel@tonic-gate 				contab[i].rq = -1;
3237c478bd9Sstevel@tonic-gate 		} else {
3247c478bd9Sstevel@tonic-gate 			contab[i].rq = mn;
3257c478bd9Sstevel@tonic-gate 			maddhash(&contab[i]);
3267c478bd9Sstevel@tonic-gate 		}
3277c478bd9Sstevel@tonic-gate 	}
3287c478bd9Sstevel@tonic-gate 	app = 0;
3297c478bd9Sstevel@tonic-gate 	return(offset = nextb);
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 
333e5190c10Smuffin int
skip()3347c478bd9Sstevel@tonic-gate skip()			/*skip over blanks; return nlflg*/
3357c478bd9Sstevel@tonic-gate {
336e5190c10Smuffin 	tchar i;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	while (cbits(i = getch()) == ' ')
3397c478bd9Sstevel@tonic-gate 		;
3407c478bd9Sstevel@tonic-gate 	ch = i;
3417c478bd9Sstevel@tonic-gate 	return(nlflg);
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 
345e5190c10Smuffin int
copyb()3467c478bd9Sstevel@tonic-gate copyb()
3477c478bd9Sstevel@tonic-gate {
348e5190c10Smuffin 	int	i, j, state;
349e5190c10Smuffin 	tchar ii;
3507c478bd9Sstevel@tonic-gate 	int	req, k;
3517c478bd9Sstevel@tonic-gate 	filep savoff;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	if (skip() || !(j = getrq()))
3547c478bd9Sstevel@tonic-gate 		j = '.';
3557c478bd9Sstevel@tonic-gate 	req = j;
3567c478bd9Sstevel@tonic-gate 	k = j >> BYTE;
3577c478bd9Sstevel@tonic-gate 	j &= BYTEMASK;
3587c478bd9Sstevel@tonic-gate 	copyf++;
3597c478bd9Sstevel@tonic-gate 	flushi();
3607c478bd9Sstevel@tonic-gate 	nlflg = 0;
3617c478bd9Sstevel@tonic-gate 	state = 1;
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate /* state 0	eat up
3647c478bd9Sstevel@tonic-gate  * state 1	look for .
3657c478bd9Sstevel@tonic-gate  * state 2	look for first char of end macro
3667c478bd9Sstevel@tonic-gate  * state 3	look for second char of end macro
3677c478bd9Sstevel@tonic-gate  */
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	while (1) {
3707c478bd9Sstevel@tonic-gate 		i = cbits(ii = getch());
3717c478bd9Sstevel@tonic-gate 		if (state == 3) {
3727c478bd9Sstevel@tonic-gate 			if (i == k)
3737c478bd9Sstevel@tonic-gate 				break;
3747c478bd9Sstevel@tonic-gate 			if (!k) {
3757c478bd9Sstevel@tonic-gate 				ch = ii;
3767c478bd9Sstevel@tonic-gate 				i = getach();
3777c478bd9Sstevel@tonic-gate 				ch = ii;
3787c478bd9Sstevel@tonic-gate 				if (!i)
3797c478bd9Sstevel@tonic-gate 					break;
3807c478bd9Sstevel@tonic-gate 			}
3817c478bd9Sstevel@tonic-gate 			state = 0;
3827c478bd9Sstevel@tonic-gate 			goto c0;
3837c478bd9Sstevel@tonic-gate 		}
3847c478bd9Sstevel@tonic-gate 		if (i == '\n') {
3857c478bd9Sstevel@tonic-gate 			state = 1;
3867c478bd9Sstevel@tonic-gate 			nlflg = 0;
3877c478bd9Sstevel@tonic-gate 			goto c0;
3887c478bd9Sstevel@tonic-gate 		}
3897c478bd9Sstevel@tonic-gate 		if (state == 1 && i == '.') {
3907c478bd9Sstevel@tonic-gate 			state++;
3917c478bd9Sstevel@tonic-gate 			savoff = offset;
3927c478bd9Sstevel@tonic-gate 			goto c0;
3937c478bd9Sstevel@tonic-gate 		}
3947c478bd9Sstevel@tonic-gate 		if ((state == 2) && (i == j)) {
3957c478bd9Sstevel@tonic-gate 			state++;
3967c478bd9Sstevel@tonic-gate 			goto c0;
3977c478bd9Sstevel@tonic-gate 		}
3987c478bd9Sstevel@tonic-gate 		state = 0;
3997c478bd9Sstevel@tonic-gate c0:
4007c478bd9Sstevel@tonic-gate 		if (offset)
4017c478bd9Sstevel@tonic-gate 			wbf(ii);
4027c478bd9Sstevel@tonic-gate 	}
4037c478bd9Sstevel@tonic-gate 	if (offset) {
4047c478bd9Sstevel@tonic-gate 		wbfl();
4057c478bd9Sstevel@tonic-gate 		offset = savoff;
4067c478bd9Sstevel@tonic-gate 		wbt((tchar)0);
4077c478bd9Sstevel@tonic-gate 	}
4087c478bd9Sstevel@tonic-gate 	copyf--;
4097c478bd9Sstevel@tonic-gate 	return(req);
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 
413e5190c10Smuffin int
copys()4147c478bd9Sstevel@tonic-gate copys()
4157c478bd9Sstevel@tonic-gate {
416e5190c10Smuffin 	tchar i;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	copyf++;
4197c478bd9Sstevel@tonic-gate 	if (skip())
4207c478bd9Sstevel@tonic-gate 		goto c0;
4217c478bd9Sstevel@tonic-gate 	if (cbits(i = getch()) != '"')
4227c478bd9Sstevel@tonic-gate 		wbf(i);
4237c478bd9Sstevel@tonic-gate 	while (cbits(i = getch()) != '\n')
4247c478bd9Sstevel@tonic-gate 		wbf(i);
4257c478bd9Sstevel@tonic-gate c0:
4267c478bd9Sstevel@tonic-gate 	wbt((tchar)0);
4277c478bd9Sstevel@tonic-gate 	copyf--;
428e5190c10Smuffin 
429e5190c10Smuffin 	return (0);
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 
alloc()4337c478bd9Sstevel@tonic-gate filep alloc()		/*return free blist[] block in nextb*/
4347c478bd9Sstevel@tonic-gate {
435e5190c10Smuffin 	int	i;
436e5190c10Smuffin 	filep j;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	for (i = 0; i < NBLIST; i++) {
4397c478bd9Sstevel@tonic-gate 		if (blist[i] == 0)
4407c478bd9Sstevel@tonic-gate 			break;
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate 	if (i == NBLIST) {
4437c478bd9Sstevel@tonic-gate 		j = 0;
4447c478bd9Sstevel@tonic-gate 	} else {
4457c478bd9Sstevel@tonic-gate 		blist[i] = -1;
4467c478bd9Sstevel@tonic-gate 		j = (filep)i * BLK + ENV_BLK * BLK;
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate #ifdef	DEBUG
4497c478bd9Sstevel@tonic-gate 	if (debug & DB_ALLC) {
4507c478bd9Sstevel@tonic-gate 		char cc1, cc2;
4517c478bd9Sstevel@tonic-gate 		fdprintf(stderr, "alloc: ");
4527c478bd9Sstevel@tonic-gate 		if (oldmn >= 0 && oldmn < NM) {
4537c478bd9Sstevel@tonic-gate 			cc1 = contab[oldmn].rq & 0177;
4547c478bd9Sstevel@tonic-gate 			if ((cc2 = (contab[oldmn].rq >> BYTE) & 0177) == 0)
4557c478bd9Sstevel@tonic-gate 				cc2 = ' ';
4567c478bd9Sstevel@tonic-gate 			fdprintf(stderr, "oldmn %d %c%c, ", oldmn, cc1, cc2);
4577c478bd9Sstevel@tonic-gate 		}
4587c478bd9Sstevel@tonic-gate 		fdprintf(stderr, "newmn %d; nextb was %x, will be %x\n",
4597c478bd9Sstevel@tonic-gate 			newmn, nextb, j);
4607c478bd9Sstevel@tonic-gate 	}
461e5190c10Smuffin #endif	/* DEBUG */
4627c478bd9Sstevel@tonic-gate 	return(nextb = j);
4637c478bd9Sstevel@tonic-gate }
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 
466e5190c10Smuffin int
ffree(i)4677c478bd9Sstevel@tonic-gate ffree(i)		/*free blist[i] and blocks pointed to*/
4687c478bd9Sstevel@tonic-gate filep i;
4697c478bd9Sstevel@tonic-gate {
470e5190c10Smuffin 	int	j;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	while (blist[j = blisti(i)] != (unsigned) ~0) {
4737c478bd9Sstevel@tonic-gate 		i = (filep) blist[j];
4747c478bd9Sstevel@tonic-gate 		blist[j] = 0;
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate 	blist[j] = 0;
477e5190c10Smuffin 
478e5190c10Smuffin 	return (0);
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate 
481e5190c10Smuffin int
wbt(i)4827c478bd9Sstevel@tonic-gate wbt(i)
4837c478bd9Sstevel@tonic-gate tchar i;
4847c478bd9Sstevel@tonic-gate {
4857c478bd9Sstevel@tonic-gate 	wbf(i);
4867c478bd9Sstevel@tonic-gate 	wbfl();
487e5190c10Smuffin 
488e5190c10Smuffin 	return (0);
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 
492e5190c10Smuffin int
wbf(i)4937c478bd9Sstevel@tonic-gate wbf(i)			/*store i into blist[offset] (?) */
494e5190c10Smuffin tchar i;
4957c478bd9Sstevel@tonic-gate {
496e5190c10Smuffin 	int	j;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	if (!offset)
499e5190c10Smuffin 		return (0);
5007c478bd9Sstevel@tonic-gate 	if (!woff) {
5017c478bd9Sstevel@tonic-gate 		woff = offset;
5027c478bd9Sstevel@tonic-gate #ifdef INCORE
5037c478bd9Sstevel@tonic-gate 		wbuf = &corebuf[woff];	/* INCORE only */
5047c478bd9Sstevel@tonic-gate #endif
5057c478bd9Sstevel@tonic-gate 		wbfi = 0;
5067c478bd9Sstevel@tonic-gate 	}
5077c478bd9Sstevel@tonic-gate 	wbuf[wbfi++] = i;
5087c478bd9Sstevel@tonic-gate 	if (!((++offset) & (BLK - 1))) {
5097c478bd9Sstevel@tonic-gate 		wbfl();
5107c478bd9Sstevel@tonic-gate 		j = blisti(--offset);
5117c478bd9Sstevel@tonic-gate 		if (j < 0 || j >= NBLIST) {
5127c478bd9Sstevel@tonic-gate 			errprint(gettext("Out of temp file space"));
5137c478bd9Sstevel@tonic-gate 			done2(01);
5147c478bd9Sstevel@tonic-gate 		}
5157c478bd9Sstevel@tonic-gate 		if (blist[j] == (unsigned) ~0) {
5167c478bd9Sstevel@tonic-gate 			if (alloc() == 0) {
5177c478bd9Sstevel@tonic-gate 				errprint(gettext("Out of temp file space"));
5187c478bd9Sstevel@tonic-gate 				done2(01);
5197c478bd9Sstevel@tonic-gate 			}
5207c478bd9Sstevel@tonic-gate 			blist[j] = (unsigned)(nextb);
5217c478bd9Sstevel@tonic-gate 		}
5227c478bd9Sstevel@tonic-gate 		offset = ((filep)blist[j]);
5237c478bd9Sstevel@tonic-gate 	}
5247c478bd9Sstevel@tonic-gate 	if (wbfi >= BLK)
5257c478bd9Sstevel@tonic-gate 		wbfl();
526e5190c10Smuffin 
527e5190c10Smuffin 	return (0);
5287c478bd9Sstevel@tonic-gate }
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 
531e5190c10Smuffin int
wbfl()5327c478bd9Sstevel@tonic-gate wbfl()			/*flush current blist[] block*/
5337c478bd9Sstevel@tonic-gate {
5347c478bd9Sstevel@tonic-gate 	if (woff == 0)
535e5190c10Smuffin 		return (0);
5367c478bd9Sstevel@tonic-gate #ifndef INCORE
5377c478bd9Sstevel@tonic-gate 	lseek(ibf, ((long)woff) * sizeof(tchar), 0);
5387c478bd9Sstevel@tonic-gate 	write(ibf, (char *)wbuf, wbfi * sizeof(tchar));
5397c478bd9Sstevel@tonic-gate #endif
5407c478bd9Sstevel@tonic-gate 	if ((woff & (~(BLK - 1))) == (roff & (~(BLK - 1))))
5417c478bd9Sstevel@tonic-gate 		roff = -1;
5427c478bd9Sstevel@tonic-gate 	woff = 0;
543e5190c10Smuffin 
544e5190c10Smuffin 	return (0);
5457c478bd9Sstevel@tonic-gate }
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 
rbf()5487c478bd9Sstevel@tonic-gate tchar rbf()		/*return next char from blist[] block*/
5497c478bd9Sstevel@tonic-gate {
550e5190c10Smuffin 	tchar i;
551e5190c10Smuffin 	filep j, p;
5527c478bd9Sstevel@tonic-gate 	extern filep incoff();
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	if (ip == NBLIST*BLK) {		/* for rdtty */
5557c478bd9Sstevel@tonic-gate 		if (j = rdtty())
5567c478bd9Sstevel@tonic-gate 			return(j);
5577c478bd9Sstevel@tonic-gate 		else
5587c478bd9Sstevel@tonic-gate 			return(popi());
5597c478bd9Sstevel@tonic-gate 	}
5607c478bd9Sstevel@tonic-gate 	/* this is an inline expansion of rbf0: dirty! */
5617c478bd9Sstevel@tonic-gate #ifndef INCORE
5627c478bd9Sstevel@tonic-gate 	j = ip & ~(BLK - 1);
5637c478bd9Sstevel@tonic-gate 	if (j != roff) {
5647c478bd9Sstevel@tonic-gate 		roff = j;
5657c478bd9Sstevel@tonic-gate 		lseek(ibf, (long)j * sizeof(tchar), 0);
5667c478bd9Sstevel@tonic-gate 		if (read(ibf, (char *)rbuf, BLK * sizeof(tchar)) <= 0)
5677c478bd9Sstevel@tonic-gate 			i = 0;
5687c478bd9Sstevel@tonic-gate 		else
5697c478bd9Sstevel@tonic-gate 			i = rbuf[ip & (BLK-1)];
5707c478bd9Sstevel@tonic-gate 	} else
5717c478bd9Sstevel@tonic-gate 		i = rbuf[ip & (BLK-1)];
5727c478bd9Sstevel@tonic-gate #else
5737c478bd9Sstevel@tonic-gate 	i = corebuf[ip];
5747c478bd9Sstevel@tonic-gate #endif
5757c478bd9Sstevel@tonic-gate 	/* end of rbf0 */
5767c478bd9Sstevel@tonic-gate 	if (i == 0) {
5777c478bd9Sstevel@tonic-gate 		if (!app)
5787c478bd9Sstevel@tonic-gate 			i = popi();
5797c478bd9Sstevel@tonic-gate 		return(i);
5807c478bd9Sstevel@tonic-gate 	}
5817c478bd9Sstevel@tonic-gate 	/* this is an inline expansion of incoff: also dirty */
5827c478bd9Sstevel@tonic-gate 	p = ++ip;
5837c478bd9Sstevel@tonic-gate 	if ((p & (BLK - 1)) == 0) {
5847c478bd9Sstevel@tonic-gate 		if ((ip = blist[blisti(p-1)]) == (unsigned) ~0) {
5857c478bd9Sstevel@tonic-gate 			errprint(gettext("Bad storage allocation"));
5867c478bd9Sstevel@tonic-gate 			ip = 0;
5877c478bd9Sstevel@tonic-gate 			done2(-5);
5887c478bd9Sstevel@tonic-gate 		}
5897c478bd9Sstevel@tonic-gate 		/* this was meant to protect against people removing
5907c478bd9Sstevel@tonic-gate 		 * the macro they were standing on, but it's too
5917c478bd9Sstevel@tonic-gate 		 * sensitive to block boundaries.
5927c478bd9Sstevel@tonic-gate 		 * if (ip == 0) {
5937c478bd9Sstevel@tonic-gate 		 *	errprint(gettext("Block removed while in use"));
5947c478bd9Sstevel@tonic-gate 		 *	done2(-6);
5957c478bd9Sstevel@tonic-gate 		 * }
5967c478bd9Sstevel@tonic-gate 		 */
5977c478bd9Sstevel@tonic-gate 	}
5987c478bd9Sstevel@tonic-gate 	return(i);
5997c478bd9Sstevel@tonic-gate }
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 
rbf0(p)6027c478bd9Sstevel@tonic-gate tchar rbf0(p)
603e5190c10Smuffin filep p;
6047c478bd9Sstevel@tonic-gate {
6057c478bd9Sstevel@tonic-gate #ifndef INCORE
606e5190c10Smuffin 	filep i;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	if ((i = p & ~(BLK - 1)) != roff) {
6097c478bd9Sstevel@tonic-gate 		roff = i;
6107c478bd9Sstevel@tonic-gate 		lseek(ibf, (long)roff * sizeof(tchar), 0);
6117c478bd9Sstevel@tonic-gate 		if (read(ibf, (char *)rbuf, BLK * sizeof(tchar)) == 0)
6127c478bd9Sstevel@tonic-gate 			return(0);
6137c478bd9Sstevel@tonic-gate 	}
6147c478bd9Sstevel@tonic-gate 	return(rbuf[p & (BLK-1)]);
6157c478bd9Sstevel@tonic-gate #else
6167c478bd9Sstevel@tonic-gate 	return(corebuf[p]);
6177c478bd9Sstevel@tonic-gate #endif
6187c478bd9Sstevel@tonic-gate }
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 
incoff(p)6217c478bd9Sstevel@tonic-gate filep incoff(p)		/*get next blist[] block*/
622e5190c10Smuffin filep p;
6237c478bd9Sstevel@tonic-gate {
6247c478bd9Sstevel@tonic-gate 	p++;
6257c478bd9Sstevel@tonic-gate 	if ((p & (BLK - 1)) == 0) {
6267c478bd9Sstevel@tonic-gate 		if ((p = blist[blisti(p-1)]) == (unsigned) ~0) {
6277c478bd9Sstevel@tonic-gate 			errprint(gettext("Bad storage allocation"));
6287c478bd9Sstevel@tonic-gate 			done2(-5);
6297c478bd9Sstevel@tonic-gate 		}
6307c478bd9Sstevel@tonic-gate 	}
6317c478bd9Sstevel@tonic-gate 	return(p);
6327c478bd9Sstevel@tonic-gate }
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 
popi()6357c478bd9Sstevel@tonic-gate tchar popi()
6367c478bd9Sstevel@tonic-gate {
637e5190c10Smuffin 	struct s *p;
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 	if (frame == stk)
6407c478bd9Sstevel@tonic-gate 		return(0);
6417c478bd9Sstevel@tonic-gate 	if (strflg)
6427c478bd9Sstevel@tonic-gate 		strflg--;
6437c478bd9Sstevel@tonic-gate 	p = nxf = frame;
6447c478bd9Sstevel@tonic-gate 	p->nargs = 0;
6457c478bd9Sstevel@tonic-gate 	frame = p->pframe;
6467c478bd9Sstevel@tonic-gate 	ip = p->pip;
6477c478bd9Sstevel@tonic-gate 	pendt = p->ppendt;
6487c478bd9Sstevel@tonic-gate 	lastpbp = p->lastpbp;
6497c478bd9Sstevel@tonic-gate 	return(p->pch);
6507c478bd9Sstevel@tonic-gate }
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate /*
6537c478bd9Sstevel@tonic-gate  *	test that the end of the allocation is above a certain location
6547c478bd9Sstevel@tonic-gate  *	in memory
6557c478bd9Sstevel@tonic-gate  */
6567c478bd9Sstevel@tonic-gate #define SPACETEST(base, size) while ((enda - (size)) <= (char *)(base)){setbrk(DELTA);}
6577c478bd9Sstevel@tonic-gate 
658e5190c10Smuffin int
pushi(newip,mname)6597c478bd9Sstevel@tonic-gate pushi(newip, mname)
6607c478bd9Sstevel@tonic-gate filep newip;
6617c478bd9Sstevel@tonic-gate int mname;
6627c478bd9Sstevel@tonic-gate {
663e5190c10Smuffin 	struct s *p;
6647c478bd9Sstevel@tonic-gate 	extern char *setbrk();
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	SPACETEST(nxf, sizeof(struct s));
6677c478bd9Sstevel@tonic-gate 	p = nxf;
6687c478bd9Sstevel@tonic-gate 	p->pframe = frame;
6697c478bd9Sstevel@tonic-gate 	p->pip = ip;
6707c478bd9Sstevel@tonic-gate 	p->ppendt = pendt;
6717c478bd9Sstevel@tonic-gate 	p->pch = ch;
6727c478bd9Sstevel@tonic-gate 	p->lastpbp = lastpbp;
6737c478bd9Sstevel@tonic-gate 	p->mname = mname;
6747c478bd9Sstevel@tonic-gate 	lastpbp = pbp;
6757c478bd9Sstevel@tonic-gate 	pendt = ch = 0;
6767c478bd9Sstevel@tonic-gate 	frame = nxf;
677*2a8bcb4eSToomas Soome 	if (nxf->nargs == 0)
6787c478bd9Sstevel@tonic-gate 		nxf += 1;
679*2a8bcb4eSToomas Soome 	else
6807c478bd9Sstevel@tonic-gate 		nxf = (struct s *)argtop;
6817c478bd9Sstevel@tonic-gate 	return(ip = newip);
6827c478bd9Sstevel@tonic-gate }
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 
setbrk(x)6857c478bd9Sstevel@tonic-gate char	*setbrk(x)
6867c478bd9Sstevel@tonic-gate int	x;
6877c478bd9Sstevel@tonic-gate {
688e5190c10Smuffin 	char	*i, *k;
689e5190c10Smuffin 	int	j;
6907c478bd9Sstevel@tonic-gate 	char	*sbrk();
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	if ((i = sbrk(x)) == (char *) -1) {
6937c478bd9Sstevel@tonic-gate 		errprint(gettext("Core limit reached"));
6947c478bd9Sstevel@tonic-gate 		edone(0100);
6957c478bd9Sstevel@tonic-gate 	}
6967c478bd9Sstevel@tonic-gate 	if (j = (unsigned)i % sizeof(int)) {	/*check alignment for 3B*/
6977c478bd9Sstevel@tonic-gate 		j = sizeof(int) - j;		/*only init calls should need this*/
6987c478bd9Sstevel@tonic-gate 		if ((k = sbrk(j)) == (char *) -1) {
6997c478bd9Sstevel@tonic-gate 			errprint("Core limit reached");
7007c478bd9Sstevel@tonic-gate 			edone(0100);
7017c478bd9Sstevel@tonic-gate 		}
7027c478bd9Sstevel@tonic-gate 		if (k != i + x) {	/*there must have been an intervening sbrk*/
7037c478bd9Sstevel@tonic-gate 			errprint ("internal error in setbrk: i=%x, j=%d, k=%x",
7047c478bd9Sstevel@tonic-gate 				i, j, k);
7057c478bd9Sstevel@tonic-gate 			edone(0100);
7067c478bd9Sstevel@tonic-gate 		}
7077c478bd9Sstevel@tonic-gate 		i += j;
7087c478bd9Sstevel@tonic-gate 	}
7097c478bd9Sstevel@tonic-gate 	enda = i + x;
7107c478bd9Sstevel@tonic-gate 	return(i);
7117c478bd9Sstevel@tonic-gate }
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 
714e5190c10Smuffin int
getsn()7157c478bd9Sstevel@tonic-gate getsn()
7167c478bd9Sstevel@tonic-gate {
717e5190c10Smuffin 	int	i;
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	if ((i = getach()) == 0)
7207c478bd9Sstevel@tonic-gate 		return(0);
7217c478bd9Sstevel@tonic-gate 	if (i == '(')
7227c478bd9Sstevel@tonic-gate 		return(getrq());
723*2a8bcb4eSToomas Soome 	else
7247c478bd9Sstevel@tonic-gate 		return(i);
7257c478bd9Sstevel@tonic-gate }
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 
728e5190c10Smuffin int
setstr()7297c478bd9Sstevel@tonic-gate setstr()
7307c478bd9Sstevel@tonic-gate {
731e5190c10Smuffin 	int	i, j;
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	lgf++;
7347c478bd9Sstevel@tonic-gate 	if ((i = getsn()) == 0 || (j = findmn(i)) == -1 || !contab[j].mx) {
7357c478bd9Sstevel@tonic-gate 		lgf--;
7367c478bd9Sstevel@tonic-gate 		return(0);
7377c478bd9Sstevel@tonic-gate 	} else {
7387c478bd9Sstevel@tonic-gate 		SPACETEST(nxf, sizeof(struct s));
7397c478bd9Sstevel@tonic-gate 		nxf->nargs = 0;
7407c478bd9Sstevel@tonic-gate 		strflg++;
7417c478bd9Sstevel@tonic-gate 		lgf--;
7427c478bd9Sstevel@tonic-gate 		return pushi((filep)contab[j].mx, i);
7437c478bd9Sstevel@tonic-gate 	}
7447c478bd9Sstevel@tonic-gate }
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 
747e5190c10Smuffin int
collect()7487c478bd9Sstevel@tonic-gate collect()
7497c478bd9Sstevel@tonic-gate {
750e5190c10Smuffin 	int	j;
751e5190c10Smuffin 	tchar i;
752e5190c10Smuffin 	tchar *strp;
7537c478bd9Sstevel@tonic-gate 	tchar * lim;
7547c478bd9Sstevel@tonic-gate 	tchar * *argpp, **argppend;
7557c478bd9Sstevel@tonic-gate 	int	quote;
7567c478bd9Sstevel@tonic-gate 	struct s *savnxf;
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	copyf++;
7597c478bd9Sstevel@tonic-gate 	nxf->nargs = 0;
7607c478bd9Sstevel@tonic-gate 	savnxf = nxf;
7617c478bd9Sstevel@tonic-gate 	if (skip())
7627c478bd9Sstevel@tonic-gate 		goto rtn;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	{
7657c478bd9Sstevel@tonic-gate 		char *memp;
7667c478bd9Sstevel@tonic-gate 		memp = (char *)savnxf;
7677c478bd9Sstevel@tonic-gate 		/*
7687c478bd9Sstevel@tonic-gate 		 *	1 s structure for the macro descriptor
7697c478bd9Sstevel@tonic-gate 		 *	APERMAC tchar *'s for pointers into the strings
7707c478bd9Sstevel@tonic-gate 		 *	space for the tchar's themselves
7717c478bd9Sstevel@tonic-gate 		 */
7727c478bd9Sstevel@tonic-gate 		memp += sizeof(struct s);
7737c478bd9Sstevel@tonic-gate 		/*
7747c478bd9Sstevel@tonic-gate 		 *	CPERMAC (the total # of characters for ALL arguments)
7757c478bd9Sstevel@tonic-gate 		 *	to a macros, has been carefully chosen
776*2a8bcb4eSToomas Soome 		 *	so that the distance between stack frames is < DELTA
7777c478bd9Sstevel@tonic-gate 		 */
7787c478bd9Sstevel@tonic-gate #define	CPERMAC	200
7797c478bd9Sstevel@tonic-gate #define	APERMAC	9
7807c478bd9Sstevel@tonic-gate 		memp += APERMAC * sizeof(tchar *);
7817c478bd9Sstevel@tonic-gate 		memp += CPERMAC * sizeof(tchar);
7827c478bd9Sstevel@tonic-gate 		nxf = (struct s*)memp;
7837c478bd9Sstevel@tonic-gate 	}
7847c478bd9Sstevel@tonic-gate 	lim = (tchar *)nxf;
7857c478bd9Sstevel@tonic-gate 	argpp = (tchar **)(savnxf + 1);
7867c478bd9Sstevel@tonic-gate 	argppend = &argpp[APERMAC];
7877c478bd9Sstevel@tonic-gate 	SPACETEST(argppend, sizeof(tchar *));
7887c478bd9Sstevel@tonic-gate 	strp = (tchar *)argppend;
7897c478bd9Sstevel@tonic-gate 	/*
7907c478bd9Sstevel@tonic-gate 	 *	Zero out all the string pointers before filling them in.
7917c478bd9Sstevel@tonic-gate 	 */
7927c478bd9Sstevel@tonic-gate 	for (j = 0; j < APERMAC; j++){
7937c478bd9Sstevel@tonic-gate 		argpp[j] = (tchar *)0;
7947c478bd9Sstevel@tonic-gate 	}
7957c478bd9Sstevel@tonic-gate #if 0
7967c478bd9Sstevel@tonic-gate 	errprint("savnxf=0x%x,nxf=0x%x,argpp=0x%x,strp=argppend=0x%x,lim=0x%x,enda=0x%x",
7977c478bd9Sstevel@tonic-gate 		savnxf, nxf, argpp, strp, lim, enda);
798e5190c10Smuffin #endif
7997c478bd9Sstevel@tonic-gate 	strflg = 0;
8007c478bd9Sstevel@tonic-gate 	while ((argpp != argppend) && (!skip())) {
8017c478bd9Sstevel@tonic-gate 		*argpp++ = strp;
8027c478bd9Sstevel@tonic-gate 		quote = 0;
8037c478bd9Sstevel@tonic-gate 		if (cbits(i = getch()) == '"')
8047c478bd9Sstevel@tonic-gate 			quote++;
805*2a8bcb4eSToomas Soome 		else
8067c478bd9Sstevel@tonic-gate 			ch = i;
8077c478bd9Sstevel@tonic-gate 		while (1) {
8087c478bd9Sstevel@tonic-gate 			i = getch();
8097c478bd9Sstevel@tonic-gate 			if (nlflg || (!quote && cbits(i) == ' '))
8107c478bd9Sstevel@tonic-gate 				break;
8117c478bd9Sstevel@tonic-gate 			if (   quote
8127c478bd9Sstevel@tonic-gate 			    && (cbits(i) == '"')
8137c478bd9Sstevel@tonic-gate 			    && (cbits(i = getch()) != '"')) {
8147c478bd9Sstevel@tonic-gate 				ch = i;
8157c478bd9Sstevel@tonic-gate 				break;
8167c478bd9Sstevel@tonic-gate 			}
8177c478bd9Sstevel@tonic-gate 			*strp++ = i;
8187c478bd9Sstevel@tonic-gate 			if (strflg && strp >= lim) {
8197c478bd9Sstevel@tonic-gate #if 0
8207c478bd9Sstevel@tonic-gate 				errprint("strp=0x%x, lim = 0x%x",
8217c478bd9Sstevel@tonic-gate 					strp, lim);
822e5190c10Smuffin #endif
8237c478bd9Sstevel@tonic-gate 				errprint(gettext("Macro argument too long"));
8247c478bd9Sstevel@tonic-gate 				copyf--;
8257c478bd9Sstevel@tonic-gate 				edone(004);
8267c478bd9Sstevel@tonic-gate 			}
8277c478bd9Sstevel@tonic-gate 			SPACETEST(strp, 3 * sizeof(tchar));
8287c478bd9Sstevel@tonic-gate 		}
8297c478bd9Sstevel@tonic-gate 		*strp++ = 0;
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate 	nxf = savnxf;
8327c478bd9Sstevel@tonic-gate 	nxf->nargs = argpp - (tchar **)(savnxf + 1);
8337c478bd9Sstevel@tonic-gate 	argtop = strp;
8347c478bd9Sstevel@tonic-gate rtn:
8357c478bd9Sstevel@tonic-gate 	copyf--;
836e5190c10Smuffin 
837e5190c10Smuffin 	return (0);
8387c478bd9Sstevel@tonic-gate }
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 
841e5190c10Smuffin int
seta()8427c478bd9Sstevel@tonic-gate seta()
8437c478bd9Sstevel@tonic-gate {
844e5190c10Smuffin 	int	i;
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	i = cbits(getch()) - '0';
8477c478bd9Sstevel@tonic-gate 	if (i > 0 && i <= APERMAC && i <= frame->nargs)
8487c478bd9Sstevel@tonic-gate 		pushback(*(((tchar **)(frame + 1)) + i - 1));
849e5190c10Smuffin 
850e5190c10Smuffin 	return (0);
8517c478bd9Sstevel@tonic-gate }
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 
854e5190c10Smuffin int
caseda()8557c478bd9Sstevel@tonic-gate caseda()
8567c478bd9Sstevel@tonic-gate {
8577c478bd9Sstevel@tonic-gate 	app++;
8587c478bd9Sstevel@tonic-gate 	casedi();
859e5190c10Smuffin 
860e5190c10Smuffin 	return (0);
8617c478bd9Sstevel@tonic-gate }
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 
864e5190c10Smuffin int
casedi()8657c478bd9Sstevel@tonic-gate casedi()
8667c478bd9Sstevel@tonic-gate {
867e5190c10Smuffin 	int	i, j;
868e5190c10Smuffin 	int	*k;
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	lgf++;
8717c478bd9Sstevel@tonic-gate 	if (skip() || (i = getrq()) == 0) {
8727c478bd9Sstevel@tonic-gate 		if (dip != d)
8737c478bd9Sstevel@tonic-gate 			wbt((tchar)0);
8747c478bd9Sstevel@tonic-gate 		if (dilev > 0) {
8757c478bd9Sstevel@tonic-gate 			numtab[DN].val = dip->dnl;
8767c478bd9Sstevel@tonic-gate 			numtab[DL].val = dip->maxl;
8777c478bd9Sstevel@tonic-gate 			dip = &d[--dilev];
8787c478bd9Sstevel@tonic-gate 			offset = dip->op;
8797c478bd9Sstevel@tonic-gate 		}
8807c478bd9Sstevel@tonic-gate 		goto rtn;
8817c478bd9Sstevel@tonic-gate 	}
8827c478bd9Sstevel@tonic-gate 	if (++dilev == NDI) {
8837c478bd9Sstevel@tonic-gate 		--dilev;
8847c478bd9Sstevel@tonic-gate 		errprint(gettext("Diversions nested too deep"));
8857c478bd9Sstevel@tonic-gate 		edone(02);
8867c478bd9Sstevel@tonic-gate 	}
8877c478bd9Sstevel@tonic-gate 	if (dip != d)
8887c478bd9Sstevel@tonic-gate 		wbt((tchar)0);
8897c478bd9Sstevel@tonic-gate 	diflg++;
8907c478bd9Sstevel@tonic-gate 	dip = &d[dilev];
8917c478bd9Sstevel@tonic-gate 	dip->op = finds(i);
8927c478bd9Sstevel@tonic-gate 	dip->curd = i;
8937c478bd9Sstevel@tonic-gate 	clrmn(oldmn);
8947c478bd9Sstevel@tonic-gate 	k = (int *) & dip->dnl;
8957c478bd9Sstevel@tonic-gate 	for (j = 0; j < 10; j++)
8967c478bd9Sstevel@tonic-gate 		k[j] = 0;	/*not op and curd*/
8977c478bd9Sstevel@tonic-gate rtn:
8987c478bd9Sstevel@tonic-gate 	app = 0;
8997c478bd9Sstevel@tonic-gate 	diflg = 0;
900e5190c10Smuffin 
901e5190c10Smuffin 	return (0);
9027c478bd9Sstevel@tonic-gate }
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 
905e5190c10Smuffin int
casedt()9067c478bd9Sstevel@tonic-gate casedt()
9077c478bd9Sstevel@tonic-gate {
9087c478bd9Sstevel@tonic-gate 	lgf++;
9097c478bd9Sstevel@tonic-gate 	dip->dimac = dip->ditrap = dip->ditf = 0;
9107c478bd9Sstevel@tonic-gate 	skip();
9117c478bd9Sstevel@tonic-gate 	dip->ditrap = vnumb((int *)0);
9127c478bd9Sstevel@tonic-gate 	if (nonumb)
913e5190c10Smuffin 		return (0);
9147c478bd9Sstevel@tonic-gate 	skip();
9157c478bd9Sstevel@tonic-gate 	dip->dimac = getrq();
916e5190c10Smuffin 
917e5190c10Smuffin 	return (0);
9187c478bd9Sstevel@tonic-gate }
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 
921e5190c10Smuffin int
casetl()9227c478bd9Sstevel@tonic-gate casetl()
9237c478bd9Sstevel@tonic-gate {
924e5190c10Smuffin 	int	j;
9257c478bd9Sstevel@tonic-gate 	int w[3];
9267c478bd9Sstevel@tonic-gate 	tchar buf[LNSIZE];
927e5190c10Smuffin 	tchar *tp;
9287c478bd9Sstevel@tonic-gate 	tchar i, delim;
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	dip->nls = 0;
9317c478bd9Sstevel@tonic-gate 	skip();
9327c478bd9Sstevel@tonic-gate 	if (ismot(delim = getch())) {
9337c478bd9Sstevel@tonic-gate 		ch = delim;
9347c478bd9Sstevel@tonic-gate 		delim = '\'';
935*2a8bcb4eSToomas Soome 	} else
9367c478bd9Sstevel@tonic-gate 		delim = cbits(delim);
9377c478bd9Sstevel@tonic-gate 	tp = buf;
9387c478bd9Sstevel@tonic-gate 	numtab[HP].val = 0;
9397c478bd9Sstevel@tonic-gate 	w[0] = w[1] = w[2] = 0;
9407c478bd9Sstevel@tonic-gate 	j = 0;
9417c478bd9Sstevel@tonic-gate 	while (cbits(i = getch()) != '\n') {
9427c478bd9Sstevel@tonic-gate 		if (cbits(i) == cbits(delim)) {
9437c478bd9Sstevel@tonic-gate 			if (j < 3)
9447c478bd9Sstevel@tonic-gate 				w[j] = numtab[HP].val;
9457c478bd9Sstevel@tonic-gate 			numtab[HP].val = 0;
9467c478bd9Sstevel@tonic-gate 			j++;
9477c478bd9Sstevel@tonic-gate 			*tp++ = 0;
9487c478bd9Sstevel@tonic-gate 		} else {
9497c478bd9Sstevel@tonic-gate 			if (cbits(i) == pagech) {
9507c478bd9Sstevel@tonic-gate 				setn1(numtab[PN].val, numtab[findr('%')].fmt,
9517c478bd9Sstevel@tonic-gate 				      i&SFMASK);
9527c478bd9Sstevel@tonic-gate 				continue;
9537c478bd9Sstevel@tonic-gate 			}
9547c478bd9Sstevel@tonic-gate 			numtab[HP].val += width(i);
9557c478bd9Sstevel@tonic-gate 			if (tp < &buf[LNSIZE-10])
9567c478bd9Sstevel@tonic-gate 				*tp++ = i;
9577c478bd9Sstevel@tonic-gate 		}
9587c478bd9Sstevel@tonic-gate 	}
9597c478bd9Sstevel@tonic-gate 	if (j<3)
9607c478bd9Sstevel@tonic-gate 		w[j] = numtab[HP].val;
9617c478bd9Sstevel@tonic-gate 	*tp++ = 0;
9627c478bd9Sstevel@tonic-gate 	*tp++ = 0;
9637c478bd9Sstevel@tonic-gate 	*tp++ = 0;
9647c478bd9Sstevel@tonic-gate 	tp = buf;
9657c478bd9Sstevel@tonic-gate #ifdef NROFF
9667c478bd9Sstevel@tonic-gate 	horiz(po);
9677c478bd9Sstevel@tonic-gate #endif
9687c478bd9Sstevel@tonic-gate 	while (i = *tp++)
9697c478bd9Sstevel@tonic-gate 		pchar(i);
9707c478bd9Sstevel@tonic-gate 	if (w[1] || w[2])
9717c478bd9Sstevel@tonic-gate 		horiz(j = quant((lt - w[1]) / 2 - w[0], HOR));
9727c478bd9Sstevel@tonic-gate 	while (i = *tp++)
9737c478bd9Sstevel@tonic-gate 		pchar(i);
9747c478bd9Sstevel@tonic-gate 	if (w[2]) {
9757c478bd9Sstevel@tonic-gate 		horiz(lt - w[0] - w[1] - w[2] - j);
9767c478bd9Sstevel@tonic-gate 		while (i = *tp++)
9777c478bd9Sstevel@tonic-gate 			pchar(i);
9787c478bd9Sstevel@tonic-gate 	}
9797c478bd9Sstevel@tonic-gate 	newline(0);
9807c478bd9Sstevel@tonic-gate 	if (dip != d) {
9817c478bd9Sstevel@tonic-gate 		if (dip->dnl > dip->hnl)
9827c478bd9Sstevel@tonic-gate 			dip->hnl = dip->dnl;
9837c478bd9Sstevel@tonic-gate 	} else {
9847c478bd9Sstevel@tonic-gate 		if (numtab[NL].val > dip->hnl)
9857c478bd9Sstevel@tonic-gate 			dip->hnl = numtab[NL].val;
9867c478bd9Sstevel@tonic-gate 	}
987e5190c10Smuffin 
988e5190c10Smuffin 	return (0);
9897c478bd9Sstevel@tonic-gate }
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 
992e5190c10Smuffin int
casepc()9937c478bd9Sstevel@tonic-gate casepc()
9947c478bd9Sstevel@tonic-gate {
9957c478bd9Sstevel@tonic-gate 	pagech = chget(IMP);
996e5190c10Smuffin 
997e5190c10Smuffin 	return (0);
9987c478bd9Sstevel@tonic-gate }
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 
1001e5190c10Smuffin int
casepm()10027c478bd9Sstevel@tonic-gate casepm()
10037c478bd9Sstevel@tonic-gate {
1004e5190c10Smuffin 	int	i, k;
1005e5190c10Smuffin 	char	*p;
10067c478bd9Sstevel@tonic-gate 	int	xx, cnt, tcnt, kk, tot;
10077c478bd9Sstevel@tonic-gate 	filep j;
10087c478bd9Sstevel@tonic-gate 	char	pmline[10];
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	kk = cnt = tcnt = 0;
10117c478bd9Sstevel@tonic-gate 	tot = !skip();
10127c478bd9Sstevel@tonic-gate 	for (i = 0; i < NM; i++) {
10137c478bd9Sstevel@tonic-gate 		if ((xx = contab[i].rq) == 0 || contab[i].mx == 0)
10147c478bd9Sstevel@tonic-gate 			continue;
10157c478bd9Sstevel@tonic-gate 		tcnt++;
10167c478bd9Sstevel@tonic-gate 		p = pmline;
10177c478bd9Sstevel@tonic-gate 		j = (filep) contab[i].mx;
10187c478bd9Sstevel@tonic-gate 		k = 1;
10197c478bd9Sstevel@tonic-gate 		while ((j = blist[blisti(j)]) != (unsigned) ~0) {
1020*2a8bcb4eSToomas Soome 			k++;
10217c478bd9Sstevel@tonic-gate 		}
10227c478bd9Sstevel@tonic-gate 		cnt++;
10237c478bd9Sstevel@tonic-gate 		kk += k;
10247c478bd9Sstevel@tonic-gate 		if (!tot) {
10257c478bd9Sstevel@tonic-gate 			*p++ = xx & 0177;
10267c478bd9Sstevel@tonic-gate 			if (!(*p++ = (xx >> BYTE) & 0177))
10277c478bd9Sstevel@tonic-gate 				*(p - 1) = ' ';
10287c478bd9Sstevel@tonic-gate 			*p++ = 0;
10297c478bd9Sstevel@tonic-gate 			fdprintf(stderr, "%s %d\n", pmline, k);
10307c478bd9Sstevel@tonic-gate 		}
10317c478bd9Sstevel@tonic-gate 	}
10327c478bd9Sstevel@tonic-gate 	fdprintf(stderr, "pm: total %d, macros %d, space %d\n", tcnt, cnt, kk);
1033e5190c10Smuffin 
1034e5190c10Smuffin 	return (0);
10357c478bd9Sstevel@tonic-gate }
10367c478bd9Sstevel@tonic-gate 
1037e5190c10Smuffin int
stackdump()10387c478bd9Sstevel@tonic-gate stackdump()	/* dumps stack of macros in process */
10397c478bd9Sstevel@tonic-gate {
10407c478bd9Sstevel@tonic-gate 	struct s *p;
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	if (frame != stk) {
10437c478bd9Sstevel@tonic-gate 		for (p = frame; p != stk; p = p->pframe)
10447c478bd9Sstevel@tonic-gate 			fdprintf(stderr, "%c%c ", p->mname&0177, (p->mname>>BYTE)&0177);
10457c478bd9Sstevel@tonic-gate 		fdprintf(stderr, "\n");
10467c478bd9Sstevel@tonic-gate 	}
1047e5190c10Smuffin 
1048e5190c10Smuffin 	return (0);
10497c478bd9Sstevel@tonic-gate }
1050