2 * Copyright 1998 Bertho A. Stultiens (BS)
17 extern void set_pp_ignore(int); /* From parser.l */
19 static char *current_define;
22 static struct pp_entry *pp_defines[HASHKEY];
25 static struct if_state ifstack[MAXIFSTACK];
26 static int ifstackidx = 0;
36 printf("Defines statistics:\n");
37 for(i = 0; i < HASHKEY; i++)
40 for(ppp = pp_defines[i]; ppp; ppp = ppp->next)
43 printf("%4d, %3d\n", i, sum);
45 printf("Total defines: %d\n", total);
47 #pragma exit pp_status
50 /* Don't comment on the hash, its primitive but functional... */
51 int pp_hash(char *str)
59 struct pp_entry *pp_lookup(char *ident)
61 int index = pp_hash(ident);
63 for(ppp = pp_defines[index]; ppp; ppp = ppp->next)
65 if(!strcmp(ident, ppp->ident))
71 void set_define(char *name)
73 current_define = xstrdup(name);
76 void del_define(char *name)
81 if((ppp = pp_lookup(name)) == NULL)
84 yywarning("%s was not defined", name);
88 index = pp_hash(name);
89 if(pp_defines[index] == ppp)
91 pp_defines[index] = ppp->next;
93 pp_defines[index]->prev = NULL;
97 ppp->prev->next = ppp->next;
99 ppp->next->prev = ppp->prev;
104 void add_define(char *text)
108 int index = pp_hash(current_define);
109 struct pp_entry *ppp;
110 if(pp_lookup(current_define) != NULL)
113 yywarning("Redefinition of %s", current_define);
114 del_define(current_define);
116 ppp = (struct pp_entry *)xmalloc(sizeof(struct pp_entry));
117 ppp->ident = current_define;
118 ppp->subst = xstrdup(text);
119 ppp->next = pp_defines[index];
120 pp_defines[index] = ppp;
122 ppp->next->prev = ppp;
123 /* Strip trailing white space from subst text */
124 len = strlen(ppp->subst);
125 while(len && strchr(" \t\r\n", ppp->subst[len-1]))
127 ppp->subst[--len] = '\0';
129 /* Strip leading white space from subst text */
130 for(cptr = ppp->subst; *cptr && strchr(" \t\r", *cptr); cptr++)
132 if(ppp->subst != cptr)
133 memmove(ppp->subst, cptr, strlen(cptr)+1);
135 printf("Added (%s, %d) <%s> to <%s>\n", input_name, line_number, ppp->ident, ppp->subst);
138 void add_cmdline_define(char *set)
140 char *cpy = xstrdup(set); /* Because gcc passes a R/O string */
141 char *cptr = strchr(cpy, '=');
145 add_define(cptr ? cptr+1 : "");
149 #if defined(_Windows) || defined(__MSDOS__)
150 #define INCLUDESEPARATOR ";"
152 #define INCLUDESEPARATOR ":"
155 static char **includepath;
156 static int nincludepath = 0;
158 void add_include_path(char *path)
161 char *cpy = xstrdup(path);
163 tok = strtok(cpy, INCLUDESEPARATOR);
171 for(cptr = dir; *cptr; cptr++)
173 /* Convert to forward slash */
177 /* Kill eventual trailing '/' */
178 if(*(cptr = dir + strlen(dir)-1) == '/')
183 includepath = (char **)xrealloc(includepath, nincludepath * sizeof(*includepath));
184 includepath[nincludepath-1] = dir;
185 tok = strtok(NULL, INCLUDESEPARATOR);
190 FILE *open_include(const char *name, int search)
192 char *cpy = xstrdup(name);
197 for(cptr = cpy; *cptr; cptr++)
199 /* kill double backslash */
200 if(*cptr == '\\' && *(cptr+1) == '\\')
201 memmove(cptr, cptr+1, strlen(cptr));
202 /* Convert to forward slash */
209 /* Search current dir and then -I path */
210 fp = fopen(name, "rt");
214 printf("Going to include <%s>\n", name);
220 for(i = 0; i < nincludepath; i++)
223 path = (char *)xmalloc(strlen(includepath[i]) + strlen(cpy) + 2);
224 strcpy(path, includepath[i]);
227 fp = fopen(path, "rt");
229 printf("Going to include <%s>\n", path);
242 void push_if(int truecase, int wastrue, int nevertrue)
244 if(ifstackidx >= MAXIFSTACK-1)
245 internal_error(__FILE__, __LINE__, "#if stack overflow");
246 ifstack[ifstackidx].current = truecase && !wastrue;
247 ifstack[ifstackidx].hasbeentrue = wastrue;
248 ifstack[ifstackidx].nevertrue = nevertrue;
249 if(nevertrue || !(truecase && !wastrue))
252 printf("push_if: %d %d %d (%d %d %d)\n",
256 ifstack[ifstackidx].current,
257 ifstack[ifstackidx].hasbeentrue,
258 ifstack[ifstackidx].nevertrue);
265 yyerror("#endif without #if|#ifdef|#ifndef (#if stack underflow)");
268 printf("pop_if: %d %d %d\n",
269 ifstack[ifstackidx].current,
270 ifstack[ifstackidx].hasbeentrue,
271 ifstack[ifstackidx].nevertrue);
272 if(ifstack[ifstackidx].nevertrue || !ifstack[ifstackidx].current)
274 return ifstack[ifstackidx].hasbeentrue || ifstack[ifstackidx].current;
277 int isnevertrue_if(void)
279 return ifstackidx > 0 && ifstack[ifstackidx-1].nevertrue;