1*3379Seric # include "useful.h"
2*3379Seric 
3*3379Seric static char SccsId[] = "@(#)macro.c	1.1	03/27/81";
4*3379Seric 
5*3379Seric char	*Macro[128];
6*3379Seric extern bool	Debug;
7*3379Seric 
8*3379Seric /*
9*3379Seric **  EXPAND -- macro expand a string using $x escapes.
10*3379Seric **
11*3379Seric **	Parameters:
12*3379Seric **		s -- the string to expand.
13*3379Seric **		buf -- the place to put the expansion.
14*3379Seric **		buflim -- the buffer limit, i.e., the address
15*3379Seric **			of the last usable position in buf.
16*3379Seric **
17*3379Seric **	Returns:
18*3379Seric **		buf.
19*3379Seric **
20*3379Seric **	Side Effects:
21*3379Seric **		none.
22*3379Seric */
23*3379Seric 
24*3379Seric char *
25*3379Seric expand(s, buf, buflim)
26*3379Seric 	register char *s;
27*3379Seric 	register char *buf;
28*3379Seric 	char *buflim;
29*3379Seric {
30*3379Seric 	register char *q;
31*3379Seric 	register char *bp;
32*3379Seric 
33*3379Seric # ifdef DEBUG
34*3379Seric 	if (Debug)
35*3379Seric 		printf("expand(%s)\n", s);
36*3379Seric # endif DEBUG
37*3379Seric 
38*3379Seric 	for (bp = buf; *s != '\0'; s++)
39*3379Seric 	{
40*3379Seric 		/* q will be the interpolated quantity */
41*3379Seric 		q = NULL;
42*3379Seric 		if (*s == '$')
43*3379Seric 			q = Macro[*++s & 0177];
44*3379Seric 
45*3379Seric 		/*
46*3379Seric 		**  Interpolate q or output one character
47*3379Seric 		*/
48*3379Seric 
49*3379Seric 		if (q != NULL)
50*3379Seric 			bp = expand(q, bp, buflim);
51*3379Seric 		else if (bp < buflim - 1)
52*3379Seric 			*bp++ = *s;
53*3379Seric 	}
54*3379Seric 	*bp = '\0';
55*3379Seric 
56*3379Seric # ifdef DEBUG
57*3379Seric 	if (Debug)
58*3379Seric 		printf("expand ==> '%s'\n", buf);
59*3379Seric # endif DEBUG
60*3379Seric 
61*3379Seric 	return (bp);
62*3379Seric }
63*3379Seric /*
64*3379Seric **  DEFINE -- define a macro.
65*3379Seric **
66*3379Seric **	this would be better done using a #define macro.
67*3379Seric **
68*3379Seric **	Parameters:
69*3379Seric **		n -- the macro name.
70*3379Seric **		v -- the macro value.
71*3379Seric **
72*3379Seric **	Returns:
73*3379Seric **		none.
74*3379Seric **
75*3379Seric **	Side Effects:
76*3379Seric **		Macro[n] is defined.
77*3379Seric */
78*3379Seric 
79*3379Seric define(n, v)
80*3379Seric 	char n;
81*3379Seric 	char *v;
82*3379Seric {
83*3379Seric # ifdef DEBUG
84*3379Seric 	if (Debug)
85*3379Seric 		printf("define(%c as %s)\n", n, v);
86*3379Seric # endif DEBUG
87*3379Seric 	Macro[n & 0177] = v;
88*3379Seric }
89