2 * (C) Copyright 10/17/86 by Carnegie Mellon University
9 #define maxinputdepth 16
10 #define maxlinesize 1024
11 #define macrohashsize 1023
12 #define macronamesize 32
13 #define maxnestdepth 128
15 #define inline xxinline
29 enum macromode { rdwr, rdonly } mode;
34 static stripcomments = 0;
35 static specialchar = '%';
36 static struct nest nests[maxnestdepth];
37 static struct nest *nestp = &nests[0];
38 static struct nest *lastnestp = &nests[maxnestdepth - 1];
40 static struct file files[maxinputdepth];
41 static struct file *lastfilep = &files[maxinputdepth - 1];
42 static struct file *filep = &files[0];
44 static char inline[maxlinesize];
45 static char outline[maxlinesize];
46 static struct macro *macrohashtable[macrohashsize];
49 error(char *a0, char *a1)
51 fprintf(stderr, a0, a1);
57 fileerror(char *a0, char *a1);
59 fprintf(stderr, "%s; line %d: ", filep->name, filep->lineno);
63 static struct macro **
64 macrolookup(char *name)
66 register struct macro **mpp, *mp;
70 for (cp = name, hv = 0; *cp; hv += *cp++);
71 mpp = ¯ohashtable[hv % macrohashsize];
72 while ((mp = *mpp) && strcmp(mp->name, name))
78 macroundefine(char *name)
80 register struct macro **mpp, *mp;
82 mpp = macrolookup(name);
92 macrodefine(char *name, char *value, enum macromode mode)
94 register struct macro **mpp, *mp;
96 mpp = macrolookup(name);
98 if (mp->mode == rdonly)
102 if ((mp = (struct macro *)malloc(sizeof(struct macro))) == 0)
103 error("Out of memory");
104 mp->name = strdup(name);
109 mp->value = strdup(value);
114 macroexpand(register char *dst, register char *src)
116 char name[macronamesize];
118 register struct macro *mp;
132 if (*src == '{' || *src == '(') {
135 if (*src == '}' || *src == ')') {
139 if (np >= &name[macronamesize])
148 if (mp = *macrolookup(name))
149 dst = macroexpand(dst, mp->value);
160 while (filep >= &files[0]) {
162 if (fgets(line, maxlinesize, filep->stream) != NULL)
164 if (fclose(filep->stream) == EOF)
165 error("Error closing %s", filep->name);
167 if (filep == &files[0])
175 writeline(char *line)
182 directive(char *what)
187 if (*what++ != specialchar)
188 return nestp->status;
189 if (cp = strrchr(what, '\n'))
191 for (n = 0; n < 2; n++) {
192 while (*what == ' ' || *what == '\t')
195 while (*what != ' ' && *what != '\t' && *what != 0)
200 while (*what == ' ' || *what == '\t')
203 if (strcmp(arg[0], "ifdef") == 0) {
204 if (nestp == lastnestp)
205 fileerror("If Depth overflow");
206 if (nestp->status == 2 || nestp->status == 1) {
212 nestp->status = (*macrolookup(arg[1])) ? 0 : 1;
215 if (strcmp(arg[0], "ifndef") == 0) {
216 if (nestp == lastnestp)
217 fileerror("If Depth overflow");
218 if (nestp->status == 2 || nestp->status == 1) {
224 nestp->status = (*macrolookup(arg[1])) ? 1 : 0;
227 if (strcmp(arg[0], "else") == 0) {
228 if (nestp->status == 2)
230 if (nestp == &nests[0])
231 fileerror("If less else");
232 nestp->status = nestp->status ? 0 : 1;
235 if (strcmp(arg[0], "endif") == 0) {
236 if (nestp == &nests[0])
237 fileerror("If less endif");
243 if (strcmp(arg[0], "include") == 0) {
244 if (filep == lastfilep)
245 fileerror("Include file overflow");
247 if ((filep->stream = fopen(arg[1], "r")) == NULL) {
249 fileerror("Can't open %s", arg[1]);
251 filep->name = strdup(arg[1]);
255 if (strcmp(arg[0], "define") == 0) {
256 macrodefine(arg[1], arg[2], rdwr);
259 if (strcmp(arg[0], "undef") == 0) {
260 macroundefine(arg[1]);
263 fileerror("Unknown directive %s", arg[0]);
267 expandfile(char *name)
269 if (strcmp(name, "-") == 0) {
270 filep->stream = stdin;
271 filep->name = strdup("(stdin)");
273 if ((filep->stream = fopen(name, "r")) == NULL) {
274 fileerror("Can't open %s", name);
277 filep->name = strdup(name);
280 while (readline(inline)) {
283 for (cp = inline; *cp != 0 && *cp != '#'; cp++)
289 (void)macroexpand(outline, inline);
290 if (directive(outline))
299 fprintf(stderr, "Usage: mpp [-cC][-s][-Dname=value][-Uname][-][files]\n");
303 #include "AFS_component_version_number.c"
306 main(int argc, char **argv)
313 if (strcmp(*argv, "-s") == 0) {
318 if (strncmp(*argv, "-c", sizeof("-c") - 1) == 0) {
319 specialchar = argv[0][sizeof("-c")];
323 if (strncmp(*argv, "-U", sizeof("-U") - 1) == 0) {
324 macroundefine(&argv[0][sizeof("-U")]);
328 if (strncmp(*argv, "-D", sizeof("-D") - 1) == 0) {
331 cp = &argv[0][sizeof("-D") - 1];
332 if (cp2 = strrchr(cp, '='))
336 macrodefine(cp, cp2, rdonly);
340 if (strcmp(*argv, "-"))