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"
35 #include "wpp_private.h"
37 struct pp_status pp_status;
41 typedef struct pp_def_state
43 struct pp_def_state *next;
44 pp_entry_t *defines[HASHKEY];
47 static pp_def_state_t *pp_def_state;
50 static pp_if_state_t if_stack[MAXIFSTACK];
51 static int if_stack_idx = 0;
54 void pp_print_status(void) __attribute__((destructor));
55 void pp_print_status(void)
62 fprintf(stderr, "Defines statistics:\n");
63 for(i = 0; i < HASHKEY; i++)
66 for(ppp = pp_def_state->defines[i]; ppp; ppp = ppp->next)
69 if (sum) fprintf(stderr, "%4d, %3d\n", i, sum);
71 fprintf(stderr, "Total defines: %d\n", total);
75 void *pp_xmalloc(size_t size)
83 fprintf(stderr, "Virtual memory exhausted.\n");
89 void *pp_xrealloc(void *p, size_t size)
94 res = realloc(p, size);
97 fprintf(stderr, "Virtual memory exhausted.\n");
103 char *pp_xstrdup(const char *str)
108 s = pp_xmalloc(strlen(str)+1);
109 return strcpy(s, str);
112 /* Don't comment on the hash, its primitive but functional... */
113 static int pphash(const char *str)
118 return sum % HASHKEY;
121 pp_entry_t *pplookup(const char *ident)
123 int idx = pphash(ident);
126 for(ppp = pp_def_state->defines[idx]; ppp; ppp = ppp->next)
128 if(!strcmp(ident, ppp->ident))
134 static void free_pp_entry( pp_entry_t *ppp, int idx )
138 if(ppp->iep == pp_includelogiclist)
140 pp_includelogiclist = ppp->iep->next;
141 if(pp_includelogiclist)
142 pp_includelogiclist->prev = NULL;
146 ppp->iep->prev->next = ppp->iep->next;
148 ppp->iep->next->prev = ppp->iep->prev;
150 free(ppp->iep->filename);
154 if(pp_def_state->defines[idx] == ppp)
156 pp_def_state->defines[idx] = ppp->next;
157 if(pp_def_state->defines[idx])
158 pp_def_state->defines[idx]->prev = NULL;
162 ppp->prev->next = ppp->next;
164 ppp->next->prev = ppp->prev;
170 /* push a new (empty) define state */
171 void pp_push_define_state(void)
173 pp_def_state_t *state = pp_xmalloc( sizeof(*state) );
175 memset( state->defines, 0, sizeof(state->defines) );
176 state->next = pp_def_state;
177 pp_def_state = state;
180 /* pop the current define state */
181 void pp_pop_define_state(void)
185 pp_def_state_t *state;
187 for (i = 0; i < HASHKEY; i++)
189 while ((ppp = pp_def_state->defines[i]) != NULL) free_pp_entry( ppp, i );
191 state = pp_def_state;
192 pp_def_state = state->next;
196 void pp_del_define(const char *name)
200 if((ppp = pplookup(name)) == NULL)
202 if(pp_status.pedantic)
203 ppwarning("%s was not defined", name);
207 free_pp_entry( ppp, pphash(name) );
210 printf("Deleted (%s, %d) <%s>\n", pp_status.input, pp_status.line_number, name);
213 pp_entry_t *pp_add_define(char *def, char *text)
217 int idx = pphash(def);
220 if((ppp = pplookup(def)) != NULL)
222 if(pp_status.pedantic)
223 ppwarning("Redefinition of %s\n\tPrevious definition: %s:%d", def, ppp->filename, ppp->linenumber);
226 ppp = pp_xmalloc(sizeof(pp_entry_t));
227 memset( ppp, 0, sizeof(*ppp) );
229 ppp->type = def_define;
230 ppp->subst.text = text;
231 ppp->filename = pp_xstrdup(pp_status.input ? pp_status.input : "<internal or cmdline>");
232 ppp->linenumber = pp_status.input ? pp_status.line_number : 0;
233 ppp->next = pp_def_state->defines[idx];
234 pp_def_state->defines[idx] = ppp;
236 ppp->next->prev = ppp;
239 /* Strip trailing white space from subst text */
241 while(len && strchr(" \t\r\n", text[len-1]))
245 /* Strip leading white space from subst text */
246 for(cptr = text; *cptr && strchr(" \t\r", *cptr); cptr++)
249 memmove(text, cptr, strlen(cptr)+1);
252 printf("Added define (%s, %d) <%s> to <%s>\n", pp_status.input, pp_status.line_number, ppp->ident, text ? text : "(null)");
257 pp_entry_t *pp_add_macro(char *id, marg_t *args[], int nargs, mtext_t *exp)
259 int idx = pphash(id);
262 if((ppp = pplookup(id)) != NULL)
264 if(pp_status.pedantic)
265 ppwarning("Redefinition of %s\n\tPrevious definition: %s:%d", id, ppp->filename, ppp->linenumber);
268 ppp = pp_xmalloc(sizeof(pp_entry_t));
269 memset( ppp, 0, sizeof(*ppp) );
271 ppp->type = def_macro;
274 ppp->subst.mtext= exp;
275 ppp->filename = pp_xstrdup(pp_status.input ? pp_status.input : "<internal or cmdline>");
276 ppp->linenumber = pp_status.input ? pp_status.line_number : 0;
277 ppp->next = pp_def_state->defines[idx];
278 pp_def_state->defines[idx] = ppp;
280 ppp->next->prev = ppp;
284 fprintf(stderr, "Added macro (%s, %d) <%s(%d)> to <", pp_status.input, pp_status.line_number, ppp->ident, nargs);
285 for(; exp; exp = exp->next)
290 fprintf(stderr, " \"%s\" ", exp->subst.text);
293 fprintf(stderr, " #(%d) ", exp->subst.argidx);
296 fprintf(stderr, "##");
299 fprintf(stderr, " <%d> ", exp->subst.argidx);
303 fprintf(stderr, ">\n");
310 *-------------------------------------------------------------------------
312 *-------------------------------------------------------------------------
314 #if defined(_Windows) || defined(__MSDOS__)
315 #define INCLUDESEPARATOR ";"
317 #define INCLUDESEPARATOR ":"
320 static char **includepath;
321 static int nincludepath = 0;
323 void wpp_add_include_path(const char *path)
326 char *cpy = pp_xstrdup(path);
328 tok = strtok(cpy, INCLUDESEPARATOR);
335 dir = pp_xstrdup(tok);
336 for(cptr = dir; *cptr; cptr++)
338 /* Convert to forward slash */
342 /* Kill eventual trailing '/' */
343 if(*(cptr = dir + strlen(dir)-1) == '/')
348 includepath = pp_xrealloc(includepath, nincludepath * sizeof(*includepath));
349 includepath[nincludepath-1] = dir;
350 tok = strtok(NULL, INCLUDESEPARATOR);
355 char *wpp_find_include(const char *name, int search)
357 char *cpy = pp_xstrdup(name);
361 for(cptr = cpy; *cptr; cptr++)
363 /* kill double backslash */
364 if(*cptr == '\\' && *(cptr+1) == '\\')
365 memmove(cptr, cptr+1, strlen(cptr));
366 /* Convert to forward slash */
373 /* Search current dir and then -I path */
374 fd = open( cpy, O_RDONLY );
382 for(i = 0; i < nincludepath; i++)
385 path = pp_xmalloc(strlen(includepath[i]) + strlen(cpy) + 2);
386 strcpy(path, includepath[i]);
389 fd = open( path, O_RDONLY );
402 FILE *pp_open_include(const char *name, int search, char **newpath)
407 if (!(path = wpp_find_include( name, search ))) return NULL;
408 fp = fopen(path, "rt");
413 printf("Going to include <%s>\n", path);
414 if (newpath) *newpath = path;
423 *-------------------------------------------------------------------------
424 * #if, #ifdef, #ifndef, #else, #elif and #endif state management
426 * #if state transitions are made on basis of the current TOS and the next
427 * required state. The state transitions are required to housekeep because
428 * #if:s can be nested. The ignore case is activated to prevent output from
429 * within a false clause.
430 * Some special cases come from the fact that the #elif cases are not
431 * binary, but three-state. The problem is that all other elif-cases must
432 * be false when one true one has been found. A second problem is that the
433 * #else clause is a final clause. No extra #else:s may follow.
436 * if_true Process input to output
437 * if_false Process input but no output
438 * if_ignore Process input but no output
439 * if_elif Process input but no output
440 * if_elsefalse Process input but no output
441 * if_elsettrue Process input to output
443 * The possible state-sequences are [state(stack depth)] (rest can be deduced):
444 * TOS #if 1 #else #endif
445 * if_true(n) if_true(n+1) if_elsefalse(n+1)
446 * if_false(n) if_ignore(n+1) if_ignore(n+1)
447 * if_elsetrue(n) if_true(n+1) if_elsefalse(n+1)
448 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1)
449 * if_elif(n) if_ignore(n+1) if_ignore(n+1)
450 * if_ignore(n) if_ignore(n+1) if_ignore(n+1)
452 * TOS #if 1 #elif 0 #else #endif
453 * if_true(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
454 * if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
455 * if_elsetrue(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
456 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
457 * if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
458 * if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
460 * TOS #if 0 #elif 1 #else #endif
461 * if_true(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
462 * if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
463 * if_elsetrue(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
464 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
465 * if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
466 * if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
468 *-------------------------------------------------------------------------
470 static const char * const pp_if_state_str[] = {
479 void pp_push_if(pp_if_state_t s)
481 if(if_stack_idx >= MAXIFSTACK)
482 pp_internal_error(__FILE__, __LINE__, "#if-stack overflow; #{if,ifdef,ifndef} nested too deeply (> %d)", MAXIFSTACK);
485 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);
487 if_stack[if_stack_idx++] = s;
498 pp_push_ignore_state();
503 pp_if_state_t pp_pop_if(void)
505 if(if_stack_idx <= 0)
506 pperror("#{endif,else,elif} without #{if,ifdef,ifndef} (#if-stack underflow)");
508 switch(pp_if_state())
517 pp_pop_ignore_state();
522 fprintf(stderr, "Pop if %s:%d: %s(%d) -> %s(%d)\n",
524 pp_status.line_number,
525 pp_if_state_str[pp_if_state()],
527 pp_if_state_str[if_stack[if_stack_idx <= 1 ? if_true : if_stack_idx-2]],
530 return if_stack[--if_stack_idx];
533 pp_if_state_t pp_if_state(void)
538 return if_stack[if_stack_idx-1];
542 void pp_next_if_state(int i)
544 switch(pp_if_state())
548 pp_push_if(i ? if_true : if_false);
554 pp_push_if(if_ignore);
557 pp_internal_error(__FILE__, __LINE__, "Invalid pp_if_state (%d) in #{if,ifdef,ifndef} directive", (int)pp_if_state());
561 int pp_get_if_depth(void)
566 /* #define WANT_NEAR_INDICATION */
568 static void generic_msg(const char *s, const char *t, const char *n, va_list ap)
570 fprintf(stderr, "%s:%d:%d: %s: ", pp_status.input ? pp_status.input : "stdin",
571 pp_status.line_number, pp_status.char_number, t);
572 vfprintf(stderr, s, ap);
573 #ifdef WANT_NEAR_INDICATION
579 for (p = cpy; *p; p++) if(!isprint(*p)) *p = ' ';
580 fprintf(stderr, " near '%s'", cpy);
585 fprintf(stderr, "\n");
588 int pperror(const char *s, ...)
592 generic_msg(s, "Error", pptext, ap);
598 int ppwarning(const char *s, ...)
602 generic_msg(s, "Warning", pptext, ap);
607 void pp_internal_error(const char *file, int line, const char *s, ...)
611 fprintf(stderr, "Internal error (please report) %s %d: ", file, line);
612 vfprintf(stderr, s, ap);
613 fprintf(stderr, "\n");