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
20 #include "wine/port.h"
32 #include "wpp_private.h"
34 struct pp_status pp_status;
38 typedef struct pp_def_state
40 struct pp_def_state *next;
41 pp_entry_t *defines[HASHKEY];
44 static pp_def_state_t *pp_def_state;
47 static pp_if_state_t if_stack[MAXIFSTACK];
48 static int if_stack_idx = 0;
51 void pp_print_status(void) __attribute__((destructor));
52 void pp_print_status(void)
59 fprintf(stderr, "Defines statistics:\n");
60 for(i = 0; i < HASHKEY; i++)
63 for(ppp = pp_def_state->defines[i]; ppp; ppp = ppp->next)
66 if (sum) fprintf(stderr, "%4d, %3d\n", i, sum);
68 fprintf(stderr, "Total defines: %d\n", total);
72 void *pp_xmalloc(size_t size)
80 fprintf(stderr, "Virtual memory exhausted.\n");
86 void *pp_xrealloc(void *p, size_t size)
91 res = realloc(p, size);
94 fprintf(stderr, "Virtual memory exhausted.\n");
100 char *pp_xstrdup(const char *str)
105 s = pp_xmalloc(strlen(str)+1);
106 return strcpy(s, str);
109 /* Don't comment on the hash, its primitive but functional... */
110 static int pphash(const char *str)
115 return sum % HASHKEY;
118 pp_entry_t *pplookup(const char *ident)
120 int idx = pphash(ident);
123 for(ppp = pp_def_state->defines[idx]; ppp; ppp = ppp->next)
125 if(!strcmp(ident, ppp->ident))
131 static void free_pp_entry( pp_entry_t *ppp, int idx )
135 if(ppp->iep == pp_includelogiclist)
137 pp_includelogiclist = ppp->iep->next;
138 if(pp_includelogiclist)
139 pp_includelogiclist->prev = NULL;
143 ppp->iep->prev->next = ppp->iep->next;
145 ppp->iep->next->prev = ppp->iep->prev;
147 free(ppp->iep->filename);
151 if(pp_def_state->defines[idx] == ppp)
153 pp_def_state->defines[idx] = ppp->next;
154 if(pp_def_state->defines[idx])
155 pp_def_state->defines[idx]->prev = NULL;
159 ppp->prev->next = ppp->next;
161 ppp->next->prev = ppp->prev;
167 /* push a new (empty) define state */
168 void pp_push_define_state(void)
170 pp_def_state_t *state = pp_xmalloc( sizeof(*state) );
172 memset( state->defines, 0, sizeof(state->defines) );
173 state->next = pp_def_state;
174 pp_def_state = state;
177 /* pop the current define state */
178 void pp_pop_define_state(void)
182 pp_def_state_t *state;
184 for (i = 0; i < HASHKEY; i++)
186 while ((ppp = pp_def_state->defines[i]) != NULL) free_pp_entry( ppp, i );
188 state = pp_def_state;
189 pp_def_state = state->next;
193 void pp_del_define(const char *name)
197 if((ppp = pplookup(name)) == NULL)
199 if(pp_status.pedantic)
200 ppwarning("%s was not defined", name);
204 free_pp_entry( ppp, pphash(name) );
207 printf("Deleted (%s, %d) <%s>\n", pp_status.input, pp_status.line_number, name);
210 pp_entry_t *pp_add_define(char *def, char *text)
214 int idx = pphash(def);
217 if((ppp = pplookup(def)) != NULL)
219 if(pp_status.pedantic)
220 ppwarning("Redefinition of %s\n\tPrevious definition: %s:%d", def, ppp->filename, ppp->linenumber);
223 ppp = pp_xmalloc(sizeof(pp_entry_t));
224 memset( ppp, 0, sizeof(*ppp) );
226 ppp->type = def_define;
227 ppp->subst.text = text;
228 ppp->filename = pp_status.input ? pp_xstrdup(pp_status.input) : "<internal or cmdline>";
229 ppp->linenumber = pp_status.input ? pp_status.line_number : 0;
230 ppp->next = pp_def_state->defines[idx];
231 pp_def_state->defines[idx] = ppp;
233 ppp->next->prev = ppp;
236 /* Strip trailing white space from subst text */
238 while(len && strchr(" \t\r\n", text[len-1]))
242 /* Strip leading white space from subst text */
243 for(cptr = text; *cptr && strchr(" \t\r", *cptr); cptr++)
246 memmove(text, cptr, strlen(cptr)+1);
249 printf("Added define (%s, %d) <%s> to <%s>\n", pp_status.input, pp_status.line_number, ppp->ident, text ? text : "(null)");
254 pp_entry_t *pp_add_macro(char *id, marg_t *args[], int nargs, mtext_t *exp)
256 int idx = pphash(id);
259 if((ppp = pplookup(id)) != NULL)
261 if(pp_status.pedantic)
262 ppwarning("Redefinition of %s\n\tPrevious definition: %s:%d", id, ppp->filename, ppp->linenumber);
265 ppp = pp_xmalloc(sizeof(pp_entry_t));
266 memset( ppp, 0, sizeof(*ppp) );
268 ppp->type = def_macro;
271 ppp->subst.mtext= exp;
272 ppp->filename = pp_status.input ? pp_xstrdup(pp_status.input) : "<internal or cmdline>";
273 ppp->linenumber = pp_status.input ? pp_status.line_number : 0;
274 ppp->next = pp_def_state->defines[idx];
275 pp_def_state->defines[idx] = ppp;
277 ppp->next->prev = ppp;
281 fprintf(stderr, "Added macro (%s, %d) <%s(%d)> to <", pp_status.input, pp_status.line_number, ppp->ident, nargs);
282 for(; exp; exp = exp->next)
287 fprintf(stderr, " \"%s\" ", exp->subst.text);
290 fprintf(stderr, " #(%d) ", exp->subst.argidx);
293 fprintf(stderr, "##");
296 fprintf(stderr, " <%d> ", exp->subst.argidx);
300 fprintf(stderr, ">\n");
307 *-------------------------------------------------------------------------
309 *-------------------------------------------------------------------------
311 #if defined(_Windows) || defined(__MSDOS__)
312 #define INCLUDESEPARATOR ";"
314 #define INCLUDESEPARATOR ":"
317 static char **includepath;
318 static int nincludepath = 0;
320 void wpp_add_include_path(const char *path)
323 char *cpy = pp_xstrdup(path);
325 tok = strtok(cpy, INCLUDESEPARATOR);
332 dir = pp_xstrdup(tok);
333 for(cptr = dir; *cptr; cptr++)
335 /* Convert to forward slash */
339 /* Kill eventual trailing '/' */
340 if(*(cptr = dir + strlen(dir)-1) == '/')
345 includepath = pp_xrealloc(includepath, nincludepath * sizeof(*includepath));
346 includepath[nincludepath-1] = dir;
347 tok = strtok(NULL, INCLUDESEPARATOR);
352 char *wpp_find_include(const char *name, int search)
354 char *cpy = pp_xstrdup(name);
358 for(cptr = cpy; *cptr; cptr++)
360 /* kill double backslash */
361 if(*cptr == '\\' && *(cptr+1) == '\\')
362 memmove(cptr, cptr+1, strlen(cptr));
363 /* Convert to forward slash */
370 /* Search current dir and then -I path */
371 fd = open( cpy, O_RDONLY );
379 for(i = 0; i < nincludepath; i++)
382 path = pp_xmalloc(strlen(includepath[i]) + strlen(cpy) + 2);
383 strcpy(path, includepath[i]);
386 fd = open( path, O_RDONLY );
399 FILE *pp_open_include(const char *name, int search, char **newpath)
404 if (!(path = wpp_find_include( name, search ))) return NULL;
405 fp = fopen(path, "rt");
410 printf("Going to include <%s>\n", path);
411 if (newpath) *newpath = path;
420 *-------------------------------------------------------------------------
421 * #if, #ifdef, #ifndef, #else, #elif and #endif state management
423 * #if state transitions are made on basis of the current TOS and the next
424 * required state. The state transitions are required to housekeep because
425 * #if:s can be nested. The ignore case is activated to prevent output from
426 * within a false clause.
427 * Some special cases come from the fact that the #elif cases are not
428 * binary, but three-state. The problem is that all other elif-cases must
429 * be false when one true one has been found. A second problem is that the
430 * #else clause is a final clause. No extra #else:s may follow.
433 * if_true Process input to output
434 * if_false Process input but no output
435 * if_ignore Process input but no output
436 * if_elif Process input but no output
437 * if_elsefalse Process input but no output
438 * if_elsettrue Process input to output
440 * The possible state-sequences are [state(stack depth)] (rest can be deduced):
441 * TOS #if 1 #else #endif
442 * if_true(n) if_true(n+1) if_elsefalse(n+1)
443 * if_false(n) if_ignore(n+1) if_ignore(n+1)
444 * if_elsetrue(n) if_true(n+1) if_elsefalse(n+1)
445 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1)
446 * if_elif(n) if_ignore(n+1) if_ignore(n+1)
447 * if_ignore(n) if_ignore(n+1) if_ignore(n+1)
449 * TOS #if 1 #elif 0 #else #endif
450 * if_true(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
451 * if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
452 * if_elsetrue(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
453 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
454 * if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
455 * if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
457 * TOS #if 0 #elif 1 #else #endif
458 * if_true(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
459 * if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
460 * if_elsetrue(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
461 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
462 * if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
463 * if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
465 *-------------------------------------------------------------------------
467 static char *pp_if_state_str[] = {
476 void pp_push_if(pp_if_state_t s)
478 if(if_stack_idx >= MAXIFSTACK)
479 pp_internal_error(__FILE__, __LINE__, "#if-stack overflow; #{if,ifdef,ifndef} nested too deeply (> %d)", MAXIFSTACK);
482 fprintf(stderr, "Push if %s:%d: %s(%d) -> %s(%d)\n", pp_status.input, pp_status.line_number, pp_if_state_str[pp_if_state()], if_stack_idx, pp_if_state_str[s], if_stack_idx+1);
484 if_stack[if_stack_idx++] = s;
495 pp_push_ignore_state();
500 pp_if_state_t pp_pop_if(void)
502 if(if_stack_idx <= 0)
503 pperror("#{endif,else,elif} without #{if,ifdef,ifndef} (#if-stack underflow)");
505 switch(pp_if_state())
514 pp_pop_ignore_state();
519 fprintf(stderr, "Pop if %s:%d: %s(%d) -> %s(%d)\n",
521 pp_status.line_number,
522 pp_if_state_str[pp_if_state()],
524 pp_if_state_str[if_stack[if_stack_idx <= 1 ? if_true : if_stack_idx-2]],
527 return if_stack[--if_stack_idx];
530 pp_if_state_t pp_if_state(void)
535 return if_stack[if_stack_idx-1];
539 void pp_next_if_state(int i)
541 switch(pp_if_state())
545 pp_push_if(i ? if_true : if_false);
551 pp_push_if(if_ignore);
554 pp_internal_error(__FILE__, __LINE__, "Invalid pp_if_state (%d) in #{if,ifdef,ifndef} directive", (int)pp_if_state());
558 int pp_get_if_depth(void)
563 /* #define WANT_NEAR_INDICATION */
565 static void generic_msg(const char *s, const char *t, const char *n, va_list ap)
567 fprintf(stderr, "%s:%d:%d: %s: ", pp_status.input ? pp_status.input : "stdin",
568 pp_status.line_number, pp_status.char_number, t);
569 vfprintf(stderr, s, ap);
570 #ifdef WANT_NEAR_INDICATION
576 for (p = cpy; *p; p++) if(!isprint(*p)) *p = ' ';
577 fprintf(stderr, " near '%s'", cpy);
582 fprintf(stderr, "\n");
585 int pperror(const char *s, ...)
589 generic_msg(s, "Error", pptext, ap);
595 int ppwarning(const char *s, ...)
599 generic_msg(s, "Warning", pptext, ap);
604 void pp_internal_error(const char *file, int line, const char *s, ...)
608 fprintf(stderr, "Internal error (please report) %s %d: ", file, line);
609 vfprintf(stderr, s, ap);
610 fprintf(stderr, "\n");