2 * (C) Copyright 10/17/86 by Carnegie Mellon University
8 #define maxinputdepth 16
9 #define maxlinesize 1024
10 #define macrohashsize 1023
11 #define macronamesize 32
12 #define maxnestdepth 128
14 #define inline xxinline
31 enum macromode { rdwr, rdonly } mode;
36 static stripcomments = 0;
37 static specialchar = '%';
38 static struct nest nests[maxnestdepth];
39 static struct nest *nestp = &nests[0];
40 static struct nest *lastnestp = &nests[maxnestdepth-1];
42 static struct file files[maxinputdepth];
43 static struct file *lastfilep = &files[maxinputdepth-1];
44 static struct file *filep = &files[0];
46 static char inline[maxlinesize];
47 static char outline[maxlinesize];
48 static struct macro *macrohashtable[macrohashsize];
53 fprintf(stderr,a0,a1);
57 static fileerror(a0,a1)
60 fprintf(stderr,"%s; line %d: ",filep->name,filep->lineno);
64 static char *strsav(s)
69 if ((p = malloc(strlen(s)+1)) == NULL)
70 error("Out of Memory");
75 static struct macro **macrolookup(name)
78 register struct macro **mpp, *mp;
82 for (cp = name, hv = 0; *cp; hv += *cp++)
84 mpp = ¯ohashtable[hv % macrohashsize];
85 while ((mp = *mpp) && strcmp(mp->name,name))
90 static macroundefine(name)
93 register struct macro **mpp, *mp;
95 mpp = macrolookup(name);
105 static macrodefine(name,value,mode)
110 register struct macro **mpp, *mp;
112 mpp = macrolookup(name);
115 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);
132 static char *macroexpand(dst,src)
133 register char *dst, *src;
135 char name[macronamesize];
137 register struct macro *mp;
154 if (*src == '{' || *src == '(')
159 if (*src == '}' || *src == ')')
164 if (np >= &name[macronamesize])
175 if (mp = *macrolookup(name))
176 dst = macroexpand(dst,mp->value);
184 static readline(line)
187 while (filep >= &files[0])
190 if (fgets(line,maxlinesize,filep->stream) != NULL)
192 if (fclose(filep->stream) == EOF)
193 error("Error closing %s",filep->name);
195 if (filep == &files[0])
202 static writeline(line)
209 static directive(what)
215 if (*what++ != specialchar)
216 return nestp->status;
217 if (cp = strrchr(what, '\n'))
219 for (n = 0; n < 2; n++)
221 while (*what == ' ' || *what == '\t')
224 while (*what != ' ' && *what != '\t' && *what != 0)
229 while (*what == ' ' || *what == '\t')
232 if (strcmp(arg[0],"ifdef") == 0)
234 if (nestp == lastnestp)
235 fileerror("If Depth overflow");
236 if (nestp->status == 2 || nestp->status == 1)
243 nestp->status = (*macrolookup(arg[1])) ? 0 : 1;
246 if (strcmp(arg[0],"ifndef") == 0)
248 if (nestp == lastnestp)
249 fileerror("If Depth overflow");
250 if (nestp->status == 2 || nestp->status == 1)
257 nestp->status = (*macrolookup(arg[1])) ? 1 : 0;
260 if (strcmp(arg[0],"else") == 0)
262 if (nestp->status == 2)
264 if (nestp == &nests[0])
265 fileerror("If less else");
266 nestp->status = nestp->status ? 0 : 1;
269 if (strcmp(arg[0],"endif") == 0)
271 if (nestp == &nests[0])
272 fileerror("If less endif");
278 if (strcmp(arg[0],"include") == 0)
280 if (filep == lastfilep)
281 fileerror("Include file overflow");
283 if ((filep->stream = fopen(arg[1],"r")) == NULL)
286 fileerror("Can't open %s",arg[1]);
288 filep->name = strsav(arg[1]);
292 if (strcmp(arg[0],"define") == 0)
294 macrodefine(arg[1],arg[2],rdwr);
297 if (strcmp(arg[0],"undef") == 0)
299 macroundefine(arg[1]);
302 fileerror("Unknown directive %s",arg[0]);
308 if (strcmp(name,"-") == 0)
310 filep->stream = stdin;
311 filep->name = strsav("(stdin)");
315 if ((filep->stream = fopen(name,"r")) == NULL)
317 fileerror("Can't open %s",name);
320 filep->name = strsav(name);
323 while (readline(inline))
328 for (cp = inline; *cp != 0 && *cp != '#'; cp++)
334 (void)macroexpand(outline,inline);
335 if (directive(outline))
344 "Usage: mpp [-cC][-s][-Dname=value][-Uname][-][files]\n");
348 #include "AFS_component_version_number.c"
361 if (strcmp(*argv,"-s") == 0)
367 if (strncmp(*argv,"-c",sizeof("-c")-1) == 0)
369 specialchar = argv[0][sizeof("-c")];
373 if (strncmp(*argv,"-U",sizeof("-U")-1) == 0)
375 macroundefine(&argv[0][sizeof("-U")]);
379 if (strncmp(*argv,"-D",sizeof("-D")-1) == 0)
383 cp = &argv[0][sizeof("-D")-1];
384 if (cp2 = strrchr(cp, '='))
388 macrodefine(cp,cp2,rdonly);
392 if (strcmp(*argv,"-"))