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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "wine/port.h"
37 #include "wpp_private.h"
39 struct pp_status pp_status;
43 typedef struct pp_def_state
45 struct pp_def_state *next;
46 pp_entry_t *defines[HASHKEY];
49 static pp_def_state_t *pp_def_state;
52 static pp_if_state_t if_stack[MAXIFSTACK];
53 static int if_stack_idx = 0;
56 void pp_print_status(void) __attribute__((destructor));
57 void pp_print_status(void)
64 fprintf(stderr, "Defines statistics:\n");
65 for(i = 0; i < HASHKEY; i++)
68 for(ppp = pp_def_state->defines[i]; ppp; ppp = ppp->next)
71 if (sum) fprintf(stderr, "%4d, %3d\n", i, sum);
73 fprintf(stderr, "Total defines: %d\n", total);
77 void *pp_xmalloc(size_t size)
85 /* Set the error flag */
91 void *pp_xrealloc(void *p, size_t size)
96 res = realloc(p, size);
99 /* Set the error flag */
105 char *pp_xstrdup(const char *str)
115 return memcpy(s, str, len);
118 static char *wpp_default_lookup(const char *name, int type, const char *parent_name,
119 char **include_path, int include_path_count)
127 cpy = pp_xmalloc(strlen(name)+1);
132 for(ccptr = name; *ccptr; ccptr++)
134 /* Convert to forward slash */
136 /* kill double backslash */
147 if(type && parent_name)
149 /* Search directory of parent include and then -I path */
152 if ((p = strrchr( parent_name, '/' ))) p++;
153 else p = parent_name;
154 path = pp_xmalloc( (p - parent_name) + strlen(cpy) + 1 );
160 memcpy( path, parent_name, p - parent_name );
161 strcpy( path + (p - parent_name), cpy );
162 fd = open( path, O_RDONLY );
172 for(i = 0; i < include_path_count; i++)
174 path = pp_xmalloc(strlen(include_path[i]) + strlen(cpy) + 2);
180 strcpy(path, include_path[i]);
183 fd = open( path, O_RDONLY );
196 static void *wpp_default_open(const char *filename, int type) {
197 return fopen(filename,"rt");
200 static void wpp_default_close(void *file) {
204 static int wpp_default_read(void *file, char *buffer, unsigned int len){
205 return fread(buffer, 1, len, file);
208 static void wpp_default_write( const char *buffer, unsigned int len ) {
209 fwrite(buffer, 1, len, ppy_out);
212 /* Don't comment on the hash, its primitive but functional... */
213 static int pphash(const char *str)
218 return sum % HASHKEY;
221 pp_entry_t *pplookup(const char *ident)
229 for(ppp = pp_def_state->defines[idx]; ppp; ppp = ppp->next)
231 if(!strcmp(ident, ppp->ident))
237 static void free_pp_entry( pp_entry_t *ppp, int idx )
241 if(ppp->iep == pp_includelogiclist)
243 pp_includelogiclist = ppp->iep->next;
244 if(pp_includelogiclist)
245 pp_includelogiclist->prev = NULL;
249 ppp->iep->prev->next = ppp->iep->next;
251 ppp->iep->next->prev = ppp->iep->prev;
253 free(ppp->iep->filename);
257 if(pp_def_state->defines[idx] == ppp)
259 pp_def_state->defines[idx] = ppp->next;
260 if(pp_def_state->defines[idx])
261 pp_def_state->defines[idx]->prev = NULL;
265 ppp->prev->next = ppp->next;
267 ppp->next->prev = ppp->prev;
273 /* push a new (empty) define state */
274 int pp_push_define_state(void)
276 pp_def_state_t *state = pp_xmalloc( sizeof(*state) );
280 memset( state->defines, 0, sizeof(state->defines) );
281 state->next = pp_def_state;
282 pp_def_state = state;
286 /* pop the current define state */
287 void pp_pop_define_state(void)
291 pp_def_state_t *state;
293 for (i = 0; i < HASHKEY; i++)
295 while ((ppp = pp_def_state->defines[i]) != NULL) free_pp_entry( ppp, i );
297 state = pp_def_state;
298 pp_def_state = state->next;
302 void pp_del_define(const char *name)
306 if((ppp = pplookup(name)) == NULL)
308 if(pp_status.pedantic)
309 ppy_warning("%s was not defined", name);
314 free( ppp->subst.text );
315 free( ppp->filename );
316 free_pp_entry( ppp, pphash(name) );
319 printf("Deleted (%s, %d) <%s>\n", pp_status.input, pp_status.line_number, name);
322 pp_entry_t *pp_add_define(const char *def, const char *text)
332 if((ppp = pplookup(def)) != NULL)
334 if(pp_status.pedantic)
335 ppy_warning("Redefinition of %s\n\tPrevious definition: %s:%d", def, ppp->filename, ppp->linenumber);
338 ppp = pp_xmalloc(sizeof(pp_entry_t));
341 memset( ppp, 0, sizeof(*ppp) );
342 ppp->ident = pp_xstrdup(def);
345 ppp->type = def_define;
346 ppp->subst.text = text ? pp_xstrdup(text) : NULL;
347 if(text && !ppp->subst.text)
349 ppp->filename = pp_xstrdup(pp_status.input ? pp_status.input : "<internal or cmdline>");
352 ppp->linenumber = pp_status.input ? pp_status.line_number : 0;
353 ppp->next = pp_def_state->defines[idx];
354 pp_def_state->defines[idx] = ppp;
356 ppp->next->prev = ppp;
359 /* Strip trailing white space from subst text */
360 len = strlen(ppp->subst.text);
361 while(len && strchr(" \t\r\n", ppp->subst.text[len-1]))
363 ppp->subst.text[--len] = '\0';
365 /* Strip leading white space from subst text */
366 for(cptr = ppp->subst.text; *cptr && strchr(" \t\r", *cptr); cptr++)
368 if(ppp->subst.text != cptr)
369 memmove(ppp->subst.text, cptr, strlen(cptr)+1);
372 printf("Added define (%s, %d) <%s> to <%s>\n", pp_status.input, pp_status.line_number, ppp->ident, ppp->subst.text ? ppp->subst.text : "(null)");
378 free(ppp->subst.text);
383 pp_entry_t *pp_add_macro(char *id, marg_t *args[], int nargs, mtext_t *exp)
391 if((ppp = pplookup(id)) != NULL)
393 if(pp_status.pedantic)
394 ppy_warning("Redefinition of %s\n\tPrevious definition: %s:%d", id, ppp->filename, ppp->linenumber);
397 ppp = pp_xmalloc(sizeof(pp_entry_t));
400 memset( ppp, 0, sizeof(*ppp) );
402 ppp->type = def_macro;
405 ppp->subst.mtext= exp;
406 ppp->filename = pp_xstrdup(pp_status.input ? pp_status.input : "<internal or cmdline>");
412 ppp->linenumber = pp_status.input ? pp_status.line_number : 0;
413 ppp->next = pp_def_state->defines[idx];
414 pp_def_state->defines[idx] = ppp;
416 ppp->next->prev = ppp;
420 fprintf(stderr, "Added macro (%s, %d) <%s(%d)> to <", pp_status.input, pp_status.line_number, ppp->ident, nargs);
421 for(; exp; exp = exp->next)
426 fprintf(stderr, " \"%s\" ", exp->subst.text);
429 fprintf(stderr, " #(%d) ", exp->subst.argidx);
432 fprintf(stderr, "##");
435 fprintf(stderr, " <%d> ", exp->subst.argidx);
439 fprintf(stderr, ">\n");
446 *-------------------------------------------------------------------------
448 *-------------------------------------------------------------------------
450 #if defined(_Windows) || defined(__MSDOS__)
451 #define INCLUDESEPARATOR ";"
453 #define INCLUDESEPARATOR ":"
456 static char **includepath;
457 static int nincludepath = 0;
459 int wpp_add_include_path(const char *path)
462 char *cpy = pp_xstrdup(path);
466 tok = strtok(cpy, INCLUDESEPARATOR);
474 dir = pp_xstrdup(tok);
480 for(cptr = dir; *cptr; cptr++)
482 /* Convert to forward slash */
486 /* Kill eventual trailing '/' */
487 if(*(cptr = dir + strlen(dir)-1) == '/')
491 new_path = pp_xrealloc(includepath, (nincludepath+1) * sizeof(*includepath));
498 includepath = new_path;
499 includepath[nincludepath] = dir;
502 tok = strtok(NULL, INCLUDESEPARATOR);
508 char *wpp_find_include(const char *name, const char *parent_name)
510 return wpp_default_lookup(name, !!parent_name, parent_name, includepath, nincludepath);
513 void *pp_open_include(const char *name, int type, const char *parent_name, char **newpath)
518 if (!(path = wpp_callbacks->lookup(name, type, parent_name, includepath,
519 nincludepath))) return NULL;
520 fp = wpp_callbacks->open(path, type);
525 printf("Going to include <%s>\n", path);
526 if (newpath) *newpath = path;
535 *-------------------------------------------------------------------------
536 * #if, #ifdef, #ifndef, #else, #elif and #endif state management
538 * #if state transitions are made on basis of the current TOS and the next
539 * required state. The state transitions are required to housekeep because
540 * #if:s can be nested. The ignore case is activated to prevent output from
541 * within a false clause.
542 * Some special cases come from the fact that the #elif cases are not
543 * binary, but three-state. The problem is that all other elif-cases must
544 * be false when one true one has been found. A second problem is that the
545 * #else clause is a final clause. No extra #else:s may follow.
548 * if_true Process input to output
549 * if_false Process input but no output
550 * if_ignore Process input but no output
551 * if_elif Process input but no output
552 * if_elsefalse Process input but no output
553 * if_elsettrue Process input to output
555 * The possible state-sequences are [state(stack depth)] (rest can be deduced):
556 * TOS #if 1 #else #endif
557 * if_true(n) if_true(n+1) if_elsefalse(n+1)
558 * if_false(n) if_ignore(n+1) if_ignore(n+1)
559 * if_elsetrue(n) if_true(n+1) if_elsefalse(n+1)
560 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1)
561 * if_elif(n) if_ignore(n+1) if_ignore(n+1)
562 * if_ignore(n) if_ignore(n+1) if_ignore(n+1)
564 * TOS #if 1 #elif 0 #else #endif
565 * if_true(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
566 * if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
567 * if_elsetrue(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
568 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
569 * if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
570 * if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
572 * TOS #if 0 #elif 1 #else #endif
573 * if_true(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
574 * if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
575 * if_elsetrue(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
576 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
577 * if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
578 * if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
580 *-------------------------------------------------------------------------
582 static const char * const pp_if_state_str[] = {
591 void pp_push_if(pp_if_state_t s)
593 if(if_stack_idx >= MAXIFSTACK)
594 pp_internal_error(__FILE__, __LINE__, "#if-stack overflow; #{if,ifdef,ifndef} nested too deeply (> %d)", MAXIFSTACK);
597 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);
599 if_stack[if_stack_idx++] = s;
610 pp_push_ignore_state();
613 pp_internal_error(__FILE__, __LINE__, "Invalid pp_if_state (%d)", (int)pp_if_state());
617 pp_if_state_t pp_pop_if(void)
619 if(if_stack_idx <= 0)
621 ppy_error("#{endif,else,elif} without #{if,ifdef,ifndef} (#if-stack underflow)");
625 switch(pp_if_state())
634 pp_pop_ignore_state();
637 pp_internal_error(__FILE__, __LINE__, "Invalid pp_if_state (%d)", (int)pp_if_state());
641 fprintf(stderr, "Pop if %s:%d: %s(%d) -> %s(%d)\n",
643 pp_status.line_number,
644 pp_if_state_str[pp_if_state()],
646 pp_if_state_str[if_stack[if_stack_idx <= 1 ? if_true : if_stack_idx-2]],
649 return if_stack[--if_stack_idx];
652 pp_if_state_t pp_if_state(void)
657 return if_stack[if_stack_idx-1];
661 void pp_next_if_state(int i)
663 switch(pp_if_state())
667 pp_push_if(i ? if_true : if_false);
673 pp_push_if(if_ignore);
676 pp_internal_error(__FILE__, __LINE__, "Invalid pp_if_state (%d) in #{if,ifdef,ifndef} directive", (int)pp_if_state());
680 int pp_get_if_depth(void)
685 /* #define WANT_NEAR_INDICATION */
687 static void generic_msg(const char *s, const char *t, const char *n, va_list ap)
689 fprintf(stderr, "%s:%d:%d: %s: ", pp_status.input ? pp_status.input : "stdin",
690 pp_status.line_number, pp_status.char_number, t);
691 vfprintf(stderr, s, ap);
692 #ifdef WANT_NEAR_INDICATION
700 for (p = cpy; *p; p++) if(!isprint(*p)) *p = ' ';
701 fprintf(stderr, " near '%s'", cpy);
707 fprintf(stderr, "\n");
710 static void wpp_default_error(const char *file, int line, int col, const char *near, const char *msg, va_list ap)
712 generic_msg(msg, "Error", near, ap);
716 static void wpp_default_warning(const char *file, int line, int col, const char *near, const char *msg, va_list ap)
718 generic_msg(msg, "Warning", near, ap);
721 static const struct wpp_callbacks default_callbacks =
732 const struct wpp_callbacks *wpp_callbacks = &default_callbacks;
734 int ppy_error(const char *s, ...)
738 wpp_callbacks->error(pp_status.input, pp_status.line_number, pp_status.char_number, ppy_text, s, ap);
744 int ppy_warning(const char *s, ...)
748 wpp_callbacks->warning(pp_status.input, pp_status.line_number, pp_status.char_number, ppy_text, s, ap);
753 void pp_internal_error(const char *file, int line, const char *s, ...)
757 fprintf(stderr, "Internal error (please report) %s %d: ", file, line);
758 vfprintf(stderr, s, ap);
759 fprintf(stderr, "\n");