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];
52 fprintf(stderr, a0, a1);
61 fprintf(stderr, "%s; line %d: ", filep->name, filep->lineno);
71 if ((p = malloc(strlen(s) + 1)) == NULL)
72 error("Out of Memory");
77 static struct macro **
81 register struct macro **mpp, *mp;
85 for (cp = name, hv = 0; *cp; hv += *cp++);
86 mpp = ¯ohashtable[hv % macrohashsize];
87 while ((mp = *mpp) && strcmp(mp->name, name))
96 register struct macro **mpp, *mp;
98 mpp = macrolookup(name);
108 macrodefine(name, value, mode)
113 register struct macro **mpp, *mp;
115 mpp = macrolookup(name);
117 if (mp->mode == rdonly)
121 if ((mp = (struct macro *)malloc(sizeof(struct macro))) == 0)
122 error("Out of memory");
123 mp->name = strsav(name);
128 mp->value = strsav(value);
133 macroexpand(dst, src)
134 register char *dst, *src;
136 char name[macronamesize];
138 register struct macro *mp;
152 if (*src == '{' || *src == '(') {
155 if (*src == '}' || *src == ')') {
159 if (np >= &name[macronamesize])
168 if (mp = *macrolookup(name))
169 dst = macroexpand(dst, mp->value);
181 while (filep >= &files[0]) {
183 if (fgets(line, maxlinesize, filep->stream) != NULL)
185 if (fclose(filep->stream) == EOF)
186 error("Error closing %s", filep->name);
188 if (filep == &files[0])
210 if (*what++ != specialchar)
211 return nestp->status;
212 if (cp = strrchr(what, '\n'))
214 for (n = 0; n < 2; n++) {
215 while (*what == ' ' || *what == '\t')
218 while (*what != ' ' && *what != '\t' && *what != 0)
223 while (*what == ' ' || *what == '\t')
226 if (strcmp(arg[0], "ifdef") == 0) {
227 if (nestp == lastnestp)
228 fileerror("If Depth overflow");
229 if (nestp->status == 2 || nestp->status == 1) {
235 nestp->status = (*macrolookup(arg[1])) ? 0 : 1;
238 if (strcmp(arg[0], "ifndef") == 0) {
239 if (nestp == lastnestp)
240 fileerror("If Depth overflow");
241 if (nestp->status == 2 || nestp->status == 1) {
247 nestp->status = (*macrolookup(arg[1])) ? 1 : 0;
250 if (strcmp(arg[0], "else") == 0) {
251 if (nestp->status == 2)
253 if (nestp == &nests[0])
254 fileerror("If less else");
255 nestp->status = nestp->status ? 0 : 1;
258 if (strcmp(arg[0], "endif") == 0) {
259 if (nestp == &nests[0])
260 fileerror("If less endif");
266 if (strcmp(arg[0], "include") == 0) {
267 if (filep == lastfilep)
268 fileerror("Include file overflow");
270 if ((filep->stream = fopen(arg[1], "r")) == NULL) {
272 fileerror("Can't open %s", arg[1]);
274 filep->name = strsav(arg[1]);
278 if (strcmp(arg[0], "define") == 0) {
279 macrodefine(arg[1], arg[2], rdwr);
282 if (strcmp(arg[0], "undef") == 0) {
283 macroundefine(arg[1]);
286 fileerror("Unknown directive %s", arg[0]);
292 if (strcmp(name, "-") == 0) {
293 filep->stream = stdin;
294 filep->name = strsav("(stdin)");
296 if ((filep->stream = fopen(name, "r")) == NULL) {
297 fileerror("Can't open %s", name);
300 filep->name = strsav(name);
303 while (readline(inline)) {
306 for (cp = inline; *cp != 0 && *cp != '#'; cp++)
312 (void)macroexpand(outline, inline);
313 if (directive(outline))
322 fprintf(stderr, "Usage: mpp [-cC][-s][-Dname=value][-Uname][-][files]\n");
326 #include "AFS_component_version_number.c"
337 if (strcmp(*argv, "-s") == 0) {
342 if (strncmp(*argv, "-c", sizeof("-c") - 1) == 0) {
343 specialchar = argv[0][sizeof("-c")];
347 if (strncmp(*argv, "-U", sizeof("-U") - 1) == 0) {
348 macroundefine(&argv[0][sizeof("-U")]);
352 if (strncmp(*argv, "-D", sizeof("-D") - 1) == 0) {
355 cp = &argv[0][sizeof("-D") - 1];
356 if (cp2 = strrchr(cp, '='))
360 macrodefine(cp, cp2, rdonly);
364 if (strcmp(*argv, "-"))