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