2 * Copyright 1998 Bertho A. Stultiens (BS)
19 static pp_entry_t *pp_defines[HASHKEY];
22 static if_state_t if_stack[MAXIFSTACK];
23 static int if_stack_idx = 0;
26 void pp_status(void) __attribute__((destructor));
34 fprintf(stderr, "Defines statistics:\n");
35 for(i = 0; i < HASHKEY; i++)
38 for(ppp = pp_defines[i]; ppp; ppp = ppp->next)
41 fprintf(stderr, "%4d, %3d\n", i, sum);
43 fprintf(stderr, "Total defines: %d\n", total);
47 /* Don't comment on the hash, its primitive but functional... */
56 pp_entry_t *pplookup(char *ident)
58 int idx = pphash(ident);
61 for(ppp = pp_defines[idx]; ppp; ppp = ppp->next)
63 if(!strcmp(ident, ppp->ident))
69 void del_define(char *name)
74 if((ppp = pplookup(name)) == NULL)
77 yywarning("%s was not defined", name);
83 if(debuglevel & DEBUGLEVEL_PPMSG)
84 fprintf(stderr, "del_define: %s:%d: includelogic removed, include_ppp='%s', file=%s\n", input_name, line_number, name, ppp->iep->filename);
85 if(ppp->iep == includelogiclist)
87 includelogiclist = ppp->iep->next;
89 includelogiclist->prev = NULL;
93 ppp->iep->prev->next = ppp->iep->next;
95 ppp->iep->next->prev = ppp->iep->prev;
97 free(ppp->iep->filename);
102 if(pp_defines[idx] == ppp)
104 pp_defines[idx] = ppp->next;
106 pp_defines[idx]->prev = NULL;
110 ppp->prev->next = ppp->next;
112 ppp->next->prev = ppp->prev;
117 if(debuglevel & DEBUGLEVEL_PPMSG)
118 printf("Deleted (%s, %d) <%s>\n", input_name, line_number, name);
121 pp_entry_t *add_define(char *def, char *text)
125 int idx = pphash(def);
128 if((ppp = pplookup(def)) != NULL)
131 yywarning("Redefinition of %s\n\tPrevious definition: %s:%d", def, ppp->filename, ppp->linenumber);
134 ppp = (pp_entry_t *)xmalloc(sizeof(pp_entry_t));
136 ppp->type = def_define;
137 ppp->subst.text = text;
138 ppp->filename = input_name ? xstrdup(input_name) : "<internal or cmdline>";
139 ppp->linenumber = input_name ? line_number : 0;
140 ppp->next = pp_defines[idx];
141 pp_defines[idx] = ppp;
143 ppp->next->prev = ppp;
146 /* Strip trailing white space from subst text */
148 while(len && strchr(" \t\r\n", text[len-1]))
152 /* Strip leading white space from subst text */
153 for(cptr = text; *cptr && strchr(" \t\r", *cptr); cptr++)
156 memmove(text, cptr, strlen(cptr)+1);
158 if(debuglevel & DEBUGLEVEL_PPMSG)
159 printf("Added define (%s, %d) <%s> to <%s>\n", input_name, line_number, ppp->ident, text ? text : "(null)");
164 pp_entry_t *add_cmdline_define(char *set)
166 char *cpy = xstrdup(set); /* Because gcc passes a R/O string */
167 char *cptr = strchr(cpy, '=');
170 return add_define(cpy, xstrdup(cptr ? cptr+1 : ""));
173 pp_entry_t *add_special_define(char *id)
175 pp_entry_t *ppp = add_define(xstrdup(id), xstrdup(""));
176 ppp->type = def_special;
180 pp_entry_t *add_macro(char *id, marg_t *args[], int nargs, mtext_t *exp)
182 int idx = pphash(id);
185 if((ppp = pplookup(id)) != NULL)
188 yywarning("Redefinition of %s\n\tPrevious definition: %s:%d", id, ppp->filename, ppp->linenumber);
191 ppp = (pp_entry_t *)xmalloc(sizeof(pp_entry_t));
193 ppp->type = def_macro;
196 ppp->subst.mtext= exp;
197 ppp->filename = input_name ? xstrdup(input_name) : "<internal or cmdline>";
198 ppp->linenumber = input_name ? line_number : 0;
199 ppp->next = pp_defines[idx];
200 pp_defines[idx] = ppp;
202 ppp->next->prev = ppp;
204 if(debuglevel & DEBUGLEVEL_PPMSG)
206 fprintf(stderr, "Added macro (%s, %d) <%s(%d)> to <", input_name, line_number, ppp->ident, nargs);
207 for(; exp; exp = exp->next)
212 fprintf(stderr, " \"%s\" ", exp->subst.text);
215 fprintf(stderr, " #(%d) ", exp->subst.argidx);
218 fprintf(stderr, "##");
221 fprintf(stderr, " <%d> ", exp->subst.argidx);
225 fprintf(stderr, ">\n");
232 *-------------------------------------------------------------------------
234 *-------------------------------------------------------------------------
236 #if defined(_Windows) || defined(__MSDOS__)
237 #define INCLUDESEPARATOR ";"
239 #define INCLUDESEPARATOR ":"
242 static char **includepath;
243 static int nincludepath = 0;
245 void add_include_path(char *path)
248 char *cpy = xstrdup(path);
250 tok = strtok(cpy, INCLUDESEPARATOR);
258 for(cptr = dir; *cptr; cptr++)
260 /* Convert to forward slash */
264 /* Kill eventual trailing '/' */
265 if(*(cptr = dir + strlen(dir)-1) == '/')
270 includepath = (char **)xrealloc(includepath, nincludepath * sizeof(*includepath));
271 includepath[nincludepath-1] = dir;
272 tok = strtok(NULL, INCLUDESEPARATOR);
277 FILE *open_include(const char *name, int search, char **newpath)
279 char *cpy = xstrdup(name);
284 for(cptr = cpy; *cptr; cptr++)
286 /* kill double backslash */
287 if(*cptr == '\\' && *(cptr+1) == '\\')
288 memmove(cptr, cptr+1, strlen(cptr));
289 /* Convert to forward slash */
296 /* Search current dir and then -I path */
297 fp = fopen(cpy, "rt");
300 if(debuglevel & DEBUGLEVEL_PPMSG)
301 printf("Going to include <%s>\n", name);
310 for(i = 0; i < nincludepath; i++)
313 path = (char *)xmalloc(strlen(includepath[i]) + strlen(cpy) + 2);
314 strcpy(path, includepath[i]);
317 fp = fopen(path, "rt");
318 if(fp && (debuglevel & DEBUGLEVEL_PPMSG))
319 printf("Going to include <%s>\n", path);
338 *-------------------------------------------------------------------------
339 * #if, #ifdef, #ifndef, #else, #elif and #endif state management
341 * #if state transitions are made on basis of the current TOS and the next
342 * required state. The state transitions are required to housekeep because
343 * #if:s can be nested. The ignore case is activated to prevent output from
344 * within a false clause.
345 * Some special cases come from the fact that the #elif cases are not
346 * binary, but three-state. The problem is that all other elif-cases must
347 * be false when one true one has been found. A second problem is that the
348 * #else clause is a final clause. No extra #else:s may follow.
351 * if_true Process input to output
352 * if_false Process input but no output
353 * if_ignore Process input but no output
354 * if_elif Process input but no output
355 * if_elsefalse Process input but no output
356 * if_elsettrue Process input to output
358 * The possible state-sequences are [state(stack depth)] (rest can be deduced):
359 * TOS #if 1 #else #endif
360 * if_true(n) if_true(n+1) if_elsefalse(n+1)
361 * if_false(n) if_ignore(n+1) if_ignore(n+1)
362 * if_elsetrue(n) if_true(n+1) if_elsefalse(n+1)
363 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1)
364 * if_elif(n) if_ignore(n+1) if_ignore(n+1)
365 * if_ignore(n) if_ignore(n+1) if_ignore(n+1)
367 * TOS #if 1 #elif 0 #else #endif
368 * if_true(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
369 * if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
370 * if_elsetrue(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
371 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
372 * if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
373 * if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
375 * TOS #if 0 #elif 1 #else #endif
376 * if_true(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
377 * if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
378 * if_elsetrue(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
379 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
380 * if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
381 * if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
383 *-------------------------------------------------------------------------
385 static char *if_state_str[] = {
394 void push_if(if_state_t s)
396 if(if_stack_idx >= MAXIFSTACK)
397 internal_error(__FILE__, __LINE__, "#if-stack overflow; #{if,ifdef,ifndef} nested too deeply (> %d)", MAXIFSTACK);
399 if(debuglevel & DEBUGLEVEL_PPLEX)
400 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);
402 if_stack[if_stack_idx++] = s;
418 if_state_t pop_if(void)
420 if(if_stack_idx <= 0)
421 yyerror("#{endif,else,elif} without #{if,ifdef,ifndef} (#if-stack underflow)");
436 if(debuglevel & DEBUGLEVEL_PPLEX)
437 fprintf(stderr, "Pop if %s:%d: %s(%d) -> %s(%d)\n",
440 if_state_str[if_state()],
442 if_state_str[if_stack[if_stack_idx <= 1 ? if_true : if_stack_idx-2]],
445 return if_stack[--if_stack_idx];
448 if_state_t if_state(void)
453 return if_stack[if_stack_idx-1];
457 void next_if_state(int i)
463 push_if(i ? if_true : if_false);
472 internal_error(__FILE__, __LINE__, "Invalid if_state (%d) in #{if,ifdef,ifndef} directive", (int)if_state());
476 int get_if_depth(void)