msvcr110: Add forwards to type_info::before function.
[wine.git] / libs / wpp / preproc.c
blobfd99167d9e698b35f89a56063efcc84556163a9a
1 /*
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
19 #include "config.h"
20 #include "wine/port.h"
22 #include <assert.h>
23 #include <ctype.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #ifdef HAVE_IO_H
33 # include <io.h>
34 #endif
36 #include "wine/wpp.h"
37 #include "wpp_private.h"
39 struct pp_status pp_status;
41 #define HASHKEY 2039
43 typedef struct pp_def_state
45 struct pp_def_state *next;
46 pp_entry_t *defines[HASHKEY];
47 } pp_def_state_t;
49 static pp_def_state_t *pp_def_state;
51 #define MAXIFSTACK 64
52 static pp_if_state_t if_stack[MAXIFSTACK];
53 static int if_stack_idx = 0;
55 #if 0
56 void pp_print_status(void) __attribute__((destructor));
57 void pp_print_status(void)
59 int i;
60 int sum;
61 int total = 0;
62 pp_entry_t *ppp;
64 fprintf(stderr, "Defines statistics:\n");
65 for(i = 0; i < HASHKEY; i++)
67 sum = 0;
68 for(ppp = pp_def_state->defines[i]; ppp; ppp = ppp->next)
69 sum++;
70 total += sum;
71 if (sum) fprintf(stderr, "%4d, %3d\n", i, sum);
73 fprintf(stderr, "Total defines: %d\n", total);
75 #endif
77 void *pp_xmalloc(size_t size)
79 void *res;
81 assert(size > 0);
82 res = malloc(size);
83 if(res == NULL)
85 /* Set the error flag */
86 pp_status.state = 1;
88 return res;
91 void *pp_xrealloc(void *p, size_t size)
93 void *res;
95 assert(size > 0);
96 res = realloc(p, size);
97 if(res == NULL)
99 /* Set the error flag */
100 pp_status.state = 1;
102 return res;
105 char *pp_xstrdup(const char *str)
107 char *s;
108 int len;
110 assert(str != NULL);
111 len = strlen(str)+1;
112 s = pp_xmalloc(len);
113 if(!s)
114 return NULL;
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)
121 char *cpy;
122 char *cptr;
123 char *path;
124 const char *ccptr;
125 int i, fd;
127 cpy = pp_xmalloc(strlen(name)+1);
128 if(!cpy)
129 return NULL;
130 cptr = cpy;
132 for(ccptr = name; *ccptr; ccptr++)
134 /* Convert to forward slash */
135 if(*ccptr == '\\') {
136 /* kill double backslash */
137 if(ccptr[1] == '\\')
138 ccptr++;
139 *cptr = '/';
140 }else {
141 *cptr = *ccptr;
143 cptr++;
145 *cptr = '\0';
147 if(type && parent_name)
149 /* Search directory of parent include and then -I path */
150 const char *p;
152 if ((p = strrchr( parent_name, '/' ))) p++;
153 else p = parent_name;
154 path = pp_xmalloc( (p - parent_name) + strlen(cpy) + 1 );
155 if(!path)
157 free(cpy);
158 return NULL;
160 memcpy( path, parent_name, p - parent_name );
161 strcpy( path + (p - parent_name), cpy );
162 fd = open( path, O_RDONLY );
163 if (fd != -1)
165 close( fd );
166 free( cpy );
167 return path;
169 free( path );
171 /* Search -I path */
172 for(i = 0; i < include_path_count; i++)
174 path = pp_xmalloc(strlen(include_path[i]) + strlen(cpy) + 2);
175 if(!path)
177 free(cpy);
178 return NULL;
180 strcpy(path, include_path[i]);
181 strcat(path, "/");
182 strcat(path, cpy);
183 fd = open( path, O_RDONLY );
184 if (fd != -1)
186 close( fd );
187 free( cpy );
188 return path;
190 free( path );
192 free( cpy );
193 return NULL;
196 static void *wpp_default_open(const char *filename, int type) {
197 return fopen(filename,"rt");
200 static void wpp_default_close(void *file) {
201 fclose(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)
215 int sum = 0;
216 while(*str)
217 sum += *str++;
218 return sum % HASHKEY;
221 pp_entry_t *pplookup(const char *ident)
223 int idx;
224 pp_entry_t *ppp;
226 if(!ident)
227 return NULL;
228 idx = pphash(ident);
229 for(ppp = pp_def_state->defines[idx]; ppp; ppp = ppp->next)
231 if(!strcmp(ident, ppp->ident))
232 return ppp;
234 return NULL;
237 static void free_pp_entry( pp_entry_t *ppp, int idx )
239 if(ppp->iep)
241 if(ppp->iep == pp_includelogiclist)
243 pp_includelogiclist = ppp->iep->next;
244 if(pp_includelogiclist)
245 pp_includelogiclist->prev = NULL;
247 else
249 ppp->iep->prev->next = ppp->iep->next;
250 if(ppp->iep->next)
251 ppp->iep->next->prev = ppp->iep->prev;
253 free(ppp->iep->filename);
254 free(ppp->iep);
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;
263 else
265 ppp->prev->next = ppp->next;
266 if(ppp->next)
267 ppp->next->prev = ppp->prev;
270 free(ppp);
273 /* push a new (empty) define state */
274 int pp_push_define_state(void)
276 pp_def_state_t *state = pp_xmalloc( sizeof(*state) );
277 if(!state)
278 return 1;
280 memset( state->defines, 0, sizeof(state->defines) );
281 state->next = pp_def_state;
282 pp_def_state = state;
283 return 0;
286 /* pop the current define state */
287 void pp_pop_define_state(void)
289 int i;
290 pp_entry_t *ppp;
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;
299 free( state );
302 void pp_del_define(const char *name)
304 pp_entry_t *ppp;
306 if((ppp = pplookup(name)) == NULL)
308 if(pp_status.pedantic)
309 ppy_warning("%s was not defined", name);
310 return;
313 free( ppp->ident );
314 free( ppp->subst.text );
315 free( ppp->filename );
316 free_pp_entry( ppp, pphash(name) );
318 if(pp_status.debug)
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)
324 int len;
325 char *cptr;
326 int idx;
327 pp_entry_t *ppp;
329 if(!def)
330 return NULL;
331 idx = pphash(def);
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);
336 pp_del_define(def);
338 ppp = pp_xmalloc(sizeof(pp_entry_t));
339 if(!ppp)
340 return NULL;
341 memset( ppp, 0, sizeof(*ppp) );
342 ppp->ident = pp_xstrdup(def);
343 if(!ppp->ident)
344 goto error;
345 ppp->type = def_define;
346 ppp->subst.text = text ? pp_xstrdup(text) : NULL;
347 if(text && !ppp->subst.text)
348 goto error;
349 ppp->filename = pp_xstrdup(pp_status.input ? pp_status.input : "<internal or cmdline>");
350 if(!ppp->filename)
351 goto error;
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;
355 if(ppp->next)
356 ppp->next->prev = ppp;
357 if(ppp->subst.text)
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);
371 if(pp_status.debug)
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)");
374 return ppp;
376 error:
377 free(ppp->ident);
378 free(ppp->subst.text);
379 free(ppp);
380 return NULL;
383 pp_entry_t *pp_add_macro(char *id, marg_t *args[], int nargs, mtext_t *exp)
385 int idx;
386 pp_entry_t *ppp;
388 if(!id)
389 return NULL;
390 idx = pphash(id);
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);
395 pp_del_define(id);
397 ppp = pp_xmalloc(sizeof(pp_entry_t));
398 if(!ppp)
399 return NULL;
400 memset( ppp, 0, sizeof(*ppp) );
401 ppp->ident = id;
402 ppp->type = def_macro;
403 ppp->margs = args;
404 ppp->nargs = nargs;
405 ppp->subst.mtext= exp;
406 ppp->filename = pp_xstrdup(pp_status.input ? pp_status.input : "<internal or cmdline>");
407 if(!ppp->filename)
409 free(ppp);
410 return NULL;
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;
415 if(ppp->next)
416 ppp->next->prev = ppp;
418 if(pp_status.debug)
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)
423 switch(exp->type)
425 case exp_text:
426 fprintf(stderr, " \"%s\" ", exp->subst.text);
427 break;
428 case exp_stringize:
429 fprintf(stderr, " #(%d) ", exp->subst.argidx);
430 break;
431 case exp_concat:
432 fprintf(stderr, "##");
433 break;
434 case exp_subst:
435 fprintf(stderr, " <%d> ", exp->subst.argidx);
436 break;
439 fprintf(stderr, ">\n");
441 return ppp;
446 *-------------------------------------------------------------------------
447 * Include management
448 *-------------------------------------------------------------------------
450 #if defined(_WIN32) || defined(__MSDOS__)
451 #define INCLUDESEPARATOR ";"
452 #else
453 #define INCLUDESEPARATOR ":"
454 #endif
456 static char **includepath;
457 static int nincludepath = 0;
459 int wpp_add_include_path(const char *path)
461 char *tok;
462 char *cpy = pp_xstrdup(path);
463 if(!cpy)
464 return 1;
466 tok = strtok(cpy, INCLUDESEPARATOR);
467 while(tok)
469 if(*tok) {
470 char *dir;
471 char *cptr;
472 char **new_path;
474 dir = pp_xstrdup(tok);
475 if(!dir)
477 free(cpy);
478 return 1;
480 for(cptr = dir; *cptr; cptr++)
482 /* Convert to forward slash */
483 if(*cptr == '\\')
484 *cptr = '/';
486 /* Kill eventual trailing '/' */
487 if(*(cptr = dir + strlen(dir)-1) == '/')
488 *cptr = '\0';
490 /* Add to list */
491 new_path = pp_xrealloc(includepath, (nincludepath+1) * sizeof(*includepath));
492 if(!new_path)
494 free(dir);
495 free(cpy);
496 return 1;
498 includepath = new_path;
499 includepath[nincludepath] = dir;
500 nincludepath++;
502 tok = strtok(NULL, INCLUDESEPARATOR);
504 free(cpy);
505 return 0;
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)
515 char *path;
516 void *fp;
518 if (!(path = wpp_callbacks->lookup(name, type, parent_name, includepath,
519 nincludepath))) return NULL;
520 fp = wpp_callbacks->open(path, type);
522 if (fp)
524 if (pp_status.debug)
525 printf("Going to include <%s>\n", path);
526 if (newpath) *newpath = path;
527 else free( path );
528 return fp;
530 free( path );
531 return NULL;
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.
547 * The states mean:
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[] = {
583 "if_false",
584 "if_true",
585 "if_elif",
586 "if_elsefalse",
587 "if_elsetrue",
588 "if_ignore"
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);
596 if(pp_flex_debug)
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;
601 switch(s)
603 case if_true:
604 case if_elsetrue:
605 break;
606 case if_false:
607 case if_elsefalse:
608 case if_elif:
609 case if_ignore:
610 pp_push_ignore_state();
611 break;
612 default:
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)");
622 return if_error;
625 switch(pp_if_state())
627 case if_true:
628 case if_elsetrue:
629 break;
630 case if_false:
631 case if_elsefalse:
632 case if_elif:
633 case if_ignore:
634 pp_pop_ignore_state();
635 break;
636 default:
637 pp_internal_error(__FILE__, __LINE__, "Invalid pp_if_state (%d)", (int)pp_if_state());
640 if(pp_flex_debug)
641 fprintf(stderr, "Pop if %s:%d: %s(%d) -> %s(%d)\n",
642 pp_status.input,
643 pp_status.line_number,
644 pp_if_state_str[pp_if_state()],
645 if_stack_idx,
646 pp_if_state_str[if_stack[if_stack_idx <= 1 ? if_true : if_stack_idx-2]],
647 if_stack_idx-1);
649 return if_stack[--if_stack_idx];
652 pp_if_state_t pp_if_state(void)
654 if(!if_stack_idx)
655 return if_true;
656 else
657 return if_stack[if_stack_idx-1];
661 void pp_next_if_state(int i)
663 switch(pp_if_state())
665 case if_true:
666 case if_elsetrue:
667 pp_push_if(i ? if_true : if_false);
668 break;
669 case if_false:
670 case if_elsefalse:
671 case if_elif:
672 case if_ignore:
673 pp_push_if(if_ignore);
674 break;
675 default:
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)
682 return if_stack_idx;
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
694 char *cpy, *p;
695 if(n)
697 cpy = pp_xstrdup(n);
698 if(!cpy)
699 goto end;
700 for (p = cpy; *p; p++) if(!isprint(*p)) *p = ' ';
701 fprintf(stderr, " near '%s'", cpy);
702 free(cpy);
705 end:
706 #endif
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);
713 exit(1);
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 =
723 wpp_default_lookup,
724 wpp_default_open,
725 wpp_default_close,
726 wpp_default_read,
727 wpp_default_write,
728 wpp_default_error,
729 wpp_default_warning,
732 const struct wpp_callbacks *wpp_callbacks = &default_callbacks;
734 int ppy_error(const char *s, ...)
736 va_list ap;
737 va_start(ap, s);
738 wpp_callbacks->error(pp_status.input, pp_status.line_number, pp_status.char_number, ppy_text, s, ap);
739 va_end(ap);
740 pp_status.state = 1;
741 return 1;
744 int ppy_warning(const char *s, ...)
746 va_list ap;
747 va_start(ap, s);
748 wpp_callbacks->warning(pp_status.input, pp_status.line_number, pp_status.char_number, ppy_text, s, ap);
749 va_end(ap);
750 return 0;
753 void pp_internal_error(const char *file, int line, const char *s, ...)
755 va_list ap;
756 va_start(ap, s);
757 fprintf(stderr, "Internal error (please report) %s %d: ", file, line);
758 vfprintf(stderr, s, ap);
759 fprintf(stderr, "\n");
760 va_end(ap);
761 exit(3);