2 * Copyright (c) 2013 Diane Bruce
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 /* calendar fake cpp does a very limited cpp version */
31 #include <sys/param.h>
46 #include "pathnames.h"
50 static FILE *fpstack[MAXFPSTACK];
52 static void pushfp(FILE *fp);
53 static FILE *popfp(void);
54 static int tokenscpp(char *buf, char *string);
63 static char *symtable[MAXSYMS];
64 static void addsym(const char *name);
65 static int findsym(const char *name);
68 fincludegets(char *buf, int size, FILE *fp)
70 char name[MAXPATHLEN];
78 if (fgets(buf, size, fp) == NULL) {
84 if ((p = strchr(buf, '\n')) != NULL)
88 while ((ch = fgetc(fp)) != '\n' && ch != EOF);
96 switch (tokenscpp(buf, name)) {
99 if ((nfp = fopen(name, "r")) != NULL) {
124 tokenscpp(char *buf, char *string)
129 if ((p = strstr(buf, "#define")) != NULL) {
131 while (isspace((unsigned char)*p))
134 while(!isspace((unsigned char)*p))
136 strncpy(string, s, MAXPATHLEN);
138 } else if ((p = strstr(buf, "#ifndef")) != NULL) {
140 while (isspace((unsigned char)*p))
143 while(!isspace((unsigned char)*p))
146 strncpy(string, s, MAXPATHLEN);
148 } else if ((p = strstr(buf, "#endif")) != NULL) {
150 } if ((p = strstr(buf, "#include")) != NULL) {
152 while (isspace((unsigned char)*p))
156 if ((p = strchr(s, '>')) != NULL)
158 snprintf (string, MAXPATHLEN, "%s/%s",
160 } else if (*p == '(') {
162 if ((p = strchr(p, '>')) != NULL)
164 snprintf (string, MAXPATHLEN, "%s", s);
175 if (curfpi == MAXFPSTACK)
176 errx(1, "Max #include reached");
177 fpstack[curfpi] = fp;
186 tmp = fpstack[curfpi];
196 for (i=0; i < MAXSYMS; i++)
204 addsym(const char *name)
209 for (i=0; i < MAXSYMS; i++) {
210 if (symtable[i] == NULL) {
211 symtable[i] = strdup(name);
212 if (symtable[i] == NULL)
213 errx(1, "malloc error in addsym");
217 errx(1, "symbol table full\n");
221 findsym(const char *name)
225 for (i=0; i < MAXSYMS; i++)
226 if (symtable[i] != NULL && strcmp(symtable[i],name) == 0)