2 * Copyright 1998 Bertho A. Stultiens (BS)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 static pp_entry_t *pp_defines[HASHKEY];
35 static if_state_t if_stack[MAXIFSTACK];
36 static int if_stack_idx = 0;
39 void pp_status(void) __attribute__((destructor));
47 fprintf(stderr, "Defines statistics:\n");
48 for(i = 0; i < HASHKEY; i++)
51 for(ppp = pp_defines[i]; ppp; ppp = ppp->next)
54 fprintf(stderr, "%4d, %3d\n", i, sum);
56 fprintf(stderr, "Total defines: %d\n", total);
60 /* Don't comment on the hash, its primitive but functional... */
69 pp_entry_t *pplookup(char *ident)
71 int idx = pphash(ident);
74 for(ppp = pp_defines[idx]; ppp; ppp = ppp->next)
76 if(!strcmp(ident, ppp->ident))
82 void del_define(char *name)
87 if((ppp = pplookup(name)) == NULL)
90 yywarning("%s was not defined", name);
96 if(debuglevel & DEBUGLEVEL_PPMSG)
97 fprintf(stderr, "del_define: %s:%d: includelogic removed, include_ppp='%s', file=%s\n", input_name, line_number, name, ppp->iep->filename);
98 if(ppp->iep == includelogiclist)
100 includelogiclist = ppp->iep->next;
102 includelogiclist->prev = NULL;
106 ppp->iep->prev->next = ppp->iep->next;
108 ppp->iep->next->prev = ppp->iep->prev;
110 free(ppp->iep->filename);
115 if(pp_defines[idx] == ppp)
117 pp_defines[idx] = ppp->next;
119 pp_defines[idx]->prev = NULL;
123 ppp->prev->next = ppp->next;
125 ppp->next->prev = ppp->prev;
130 if(debuglevel & DEBUGLEVEL_PPMSG)
131 printf("Deleted (%s, %d) <%s>\n", input_name, line_number, name);
134 pp_entry_t *add_define(char *def, char *text)
138 int idx = pphash(def);
141 if((ppp = pplookup(def)) != NULL)
144 yywarning("Redefinition of %s\n\tPrevious definition: %s:%d", def, ppp->filename, ppp->linenumber);
147 ppp = (pp_entry_t *)xmalloc(sizeof(pp_entry_t));
149 ppp->type = def_define;
150 ppp->subst.text = text;
151 ppp->filename = input_name ? xstrdup(input_name) : "<internal or cmdline>";
152 ppp->linenumber = input_name ? line_number : 0;
153 ppp->next = pp_defines[idx];
154 pp_defines[idx] = ppp;
156 ppp->next->prev = ppp;
159 /* Strip trailing white space from subst text */
161 while(len && strchr(" \t\r\n", text[len-1]))
165 /* Strip leading white space from subst text */
166 for(cptr = text; *cptr && strchr(" \t\r", *cptr); cptr++)
169 memmove(text, cptr, strlen(cptr)+1);
171 if(debuglevel & DEBUGLEVEL_PPMSG)
172 printf("Added define (%s, %d) <%s> to <%s>\n", input_name, line_number, ppp->ident, text ? text : "(null)");
177 pp_entry_t *add_cmdline_define(char *set)
179 char *cpy = xstrdup(set); /* Because gcc passes a R/O string */
180 char *cptr = strchr(cpy, '=');
183 return add_define(cpy, xstrdup(cptr ? cptr+1 : ""));
186 pp_entry_t *add_special_define(char *id)
188 pp_entry_t *ppp = add_define(xstrdup(id), xstrdup(""));
189 ppp->type = def_special;
193 pp_entry_t *add_macro(char *id, marg_t *args[], int nargs, mtext_t *exp)
195 int idx = pphash(id);
198 if((ppp = pplookup(id)) != NULL)
201 yywarning("Redefinition of %s\n\tPrevious definition: %s:%d", id, ppp->filename, ppp->linenumber);
204 ppp = (pp_entry_t *)xmalloc(sizeof(pp_entry_t));
206 ppp->type = def_macro;
209 ppp->subst.mtext= exp;
210 ppp->filename = input_name ? xstrdup(input_name) : "<internal or cmdline>";
211 ppp->linenumber = input_name ? line_number : 0;
212 ppp->next = pp_defines[idx];
213 pp_defines[idx] = ppp;
215 ppp->next->prev = ppp;
217 if(debuglevel & DEBUGLEVEL_PPMSG)
219 fprintf(stderr, "Added macro (%s, %d) <%s(%d)> to <", input_name, line_number, ppp->ident, nargs);
220 for(; exp; exp = exp->next)
225 fprintf(stderr, " \"%s\" ", exp->subst.text);
228 fprintf(stderr, " #(%d) ", exp->subst.argidx);
231 fprintf(stderr, "##");
234 fprintf(stderr, " <%d> ", exp->subst.argidx);
238 fprintf(stderr, ">\n");
245 *-------------------------------------------------------------------------
247 *-------------------------------------------------------------------------
249 #if defined(_Windows) || defined(__MSDOS__)
250 #define INCLUDESEPARATOR ";"
252 #define INCLUDESEPARATOR ":"
255 static char **includepath;
256 static int nincludepath = 0;
258 void add_include_path(char *path)
261 char *cpy = xstrdup(path);
263 tok = strtok(cpy, INCLUDESEPARATOR);
271 for(cptr = dir; *cptr; cptr++)
273 /* Convert to forward slash */
277 /* Kill eventual trailing '/' */
278 if(*(cptr = dir + strlen(dir)-1) == '/')
283 includepath = (char **)xrealloc(includepath, nincludepath * sizeof(*includepath));
284 includepath[nincludepath-1] = dir;
285 tok = strtok(NULL, INCLUDESEPARATOR);
290 FILE *open_include(const char *name, int search, char **newpath)
292 char *cpy = xstrdup(name);
297 for(cptr = cpy; *cptr; cptr++)
299 /* kill double backslash */
300 if(*cptr == '\\' && *(cptr+1) == '\\')
301 memmove(cptr, cptr+1, strlen(cptr));
302 /* Convert to forward slash */
309 /* Search current dir and then -I path */
310 fp = fopen(cpy, "rt");
313 if(debuglevel & DEBUGLEVEL_PPMSG)
314 printf("Going to include <%s>\n", name);
323 for(i = 0; i < nincludepath; i++)
326 path = (char *)xmalloc(strlen(includepath[i]) + strlen(cpy) + 2);
327 strcpy(path, includepath[i]);
330 fp = fopen(path, "rt");
331 if(fp && (debuglevel & DEBUGLEVEL_PPMSG))
332 printf("Going to include <%s>\n", path);
351 *-------------------------------------------------------------------------
352 * #if, #ifdef, #ifndef, #else, #elif and #endif state management
354 * #if state transitions are made on basis of the current TOS and the next
355 * required state. The state transitions are required to housekeep because
356 * #if:s can be nested. The ignore case is activated to prevent output from
357 * within a false clause.
358 * Some special cases come from the fact that the #elif cases are not
359 * binary, but three-state. The problem is that all other elif-cases must
360 * be false when one true one has been found. A second problem is that the
361 * #else clause is a final clause. No extra #else:s may follow.
364 * if_true Process input to output
365 * if_false Process input but no output
366 * if_ignore Process input but no output
367 * if_elif Process input but no output
368 * if_elsefalse Process input but no output
369 * if_elsettrue Process input to output
371 * The possible state-sequences are [state(stack depth)] (rest can be deduced):
372 * TOS #if 1 #else #endif
373 * if_true(n) if_true(n+1) if_elsefalse(n+1)
374 * if_false(n) if_ignore(n+1) if_ignore(n+1)
375 * if_elsetrue(n) if_true(n+1) if_elsefalse(n+1)
376 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1)
377 * if_elif(n) if_ignore(n+1) if_ignore(n+1)
378 * if_ignore(n) if_ignore(n+1) if_ignore(n+1)
380 * TOS #if 1 #elif 0 #else #endif
381 * if_true(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
382 * if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
383 * if_elsetrue(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
384 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
385 * if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
386 * if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
388 * TOS #if 0 #elif 1 #else #endif
389 * if_true(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
390 * if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
391 * if_elsetrue(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
392 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
393 * if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
394 * if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
396 *-------------------------------------------------------------------------
398 static char *if_state_str[] = {
407 void push_if(if_state_t s)
409 if(if_stack_idx >= MAXIFSTACK)
410 internal_error(__FILE__, __LINE__, "#if-stack overflow; #{if,ifdef,ifndef} nested too deeply (> %d)", MAXIFSTACK);
412 if(debuglevel & DEBUGLEVEL_PPLEX)
413 fprintf(stderr, "Push if %s:%d: %s(%d) -> %s(%d)\n", input_name, line_number, if_state_str[if_state()], if_stack_idx, if_state_str[s], if_stack_idx+1);
415 if_stack[if_stack_idx++] = s;
431 if_state_t pop_if(void)
433 if(if_stack_idx <= 0)
434 yyerror("#{endif,else,elif} without #{if,ifdef,ifndef} (#if-stack underflow)");
449 if(debuglevel & DEBUGLEVEL_PPLEX)
450 fprintf(stderr, "Pop if %s:%d: %s(%d) -> %s(%d)\n",
453 if_state_str[if_state()],
455 if_state_str[if_stack[if_stack_idx <= 1 ? if_true : if_stack_idx-2]],
458 return if_stack[--if_stack_idx];
461 if_state_t if_state(void)
466 return if_stack[if_stack_idx-1];
470 void next_if_state(int i)
476 push_if(i ? if_true : if_false);
485 internal_error(__FILE__, __LINE__, "Invalid if_state (%d) in #{if,ifdef,ifndef} directive", (int)if_state());
489 int get_if_depth(void)