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
, 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 */
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
);
313 free_pp_entry( ppp
, pphash(name
) );
316 printf("Deleted (%s, %d) <%s>\n", pp_status
.input
, pp_status
.line_number
, name
);
319 pp_entry_t
*pp_add_define(char *def
, char *text
)
329 if((ppp
= pplookup(def
)) != NULL
)
331 if(pp_status
.pedantic
)
332 ppy_warning("Redefinition of %s\n\tPrevious definition: %s:%d", def
, ppp
->filename
, ppp
->linenumber
);
335 ppp
= pp_xmalloc(sizeof(pp_entry_t
));
338 memset( ppp
, 0, sizeof(*ppp
) );
340 ppp
->type
= def_define
;
341 ppp
->subst
.text
= text
;
342 ppp
->filename
= pp_xstrdup(pp_status
.input
? pp_status
.input
: "<internal or cmdline>");
348 ppp
->linenumber
= pp_status
.input
? pp_status
.line_number
: 0;
349 ppp
->next
= pp_def_state
->defines
[idx
];
350 pp_def_state
->defines
[idx
] = ppp
;
352 ppp
->next
->prev
= ppp
;
355 /* Strip trailing white space from subst text */
357 while(len
&& strchr(" \t\r\n", text
[len
-1]))
361 /* Strip leading white space from subst text */
362 for(cptr
= text
; *cptr
&& strchr(" \t\r", *cptr
); cptr
++)
365 memmove(text
, cptr
, strlen(cptr
)+1);
368 printf("Added define (%s, %d) <%s> to <%s>\n", pp_status
.input
, pp_status
.line_number
, ppp
->ident
, text
? text
: "(null)");
373 pp_entry_t
*pp_add_macro(char *id
, marg_t
*args
[], int nargs
, mtext_t
*exp
)
381 if((ppp
= pplookup(id
)) != NULL
)
383 if(pp_status
.pedantic
)
384 ppy_warning("Redefinition of %s\n\tPrevious definition: %s:%d", id
, ppp
->filename
, ppp
->linenumber
);
387 ppp
= pp_xmalloc(sizeof(pp_entry_t
));
390 memset( ppp
, 0, sizeof(*ppp
) );
392 ppp
->type
= def_macro
;
395 ppp
->subst
.mtext
= exp
;
396 ppp
->filename
= pp_xstrdup(pp_status
.input
? pp_status
.input
: "<internal or cmdline>");
402 ppp
->linenumber
= pp_status
.input
? pp_status
.line_number
: 0;
403 ppp
->next
= pp_def_state
->defines
[idx
];
404 pp_def_state
->defines
[idx
] = ppp
;
406 ppp
->next
->prev
= ppp
;
410 fprintf(stderr
, "Added macro (%s, %d) <%s(%d)> to <", pp_status
.input
, pp_status
.line_number
, ppp
->ident
, nargs
);
411 for(; exp
; exp
= exp
->next
)
416 fprintf(stderr
, " \"%s\" ", exp
->subst
.text
);
419 fprintf(stderr
, " #(%d) ", exp
->subst
.argidx
);
422 fprintf(stderr
, "##");
425 fprintf(stderr
, " <%d> ", exp
->subst
.argidx
);
429 fprintf(stderr
, ">\n");
436 *-------------------------------------------------------------------------
438 *-------------------------------------------------------------------------
440 #if defined(_Windows) || defined(__MSDOS__)
441 #define INCLUDESEPARATOR ";"
443 #define INCLUDESEPARATOR ":"
446 static char **includepath
;
447 static int nincludepath
= 0;
449 int wpp_add_include_path(const char *path
)
452 char *cpy
= pp_xstrdup(path
);
456 tok
= strtok(cpy
, INCLUDESEPARATOR
);
464 dir
= pp_xstrdup(tok
);
470 for(cptr
= dir
; *cptr
; cptr
++)
472 /* Convert to forward slash */
476 /* Kill eventual trailing '/' */
477 if(*(cptr
= dir
+ strlen(dir
)-1) == '/')
481 new_path
= pp_xrealloc(includepath
, (nincludepath
+1) * sizeof(*includepath
));
488 includepath
= new_path
;
489 includepath
[nincludepath
] = dir
;
492 tok
= strtok(NULL
, INCLUDESEPARATOR
);
498 char *wpp_find_include(const char *name
, const char *parent_name
)
500 return wpp_default_lookup(name
, parent_name
, includepath
, nincludepath
);
503 void *pp_open_include(const char *name
, const char *parent_name
, char **newpath
)
508 if (!(path
= wpp_callbacks
->lookup(name
, parent_name
, includepath
,
509 nincludepath
))) return NULL
;
510 fp
= wpp_callbacks
->open(path
, parent_name
== NULL
? 1 : 0);
515 printf("Going to include <%s>\n", path
);
516 if (newpath
) *newpath
= path
;
525 *-------------------------------------------------------------------------
526 * #if, #ifdef, #ifndef, #else, #elif and #endif state management
528 * #if state transitions are made on basis of the current TOS and the next
529 * required state. The state transitions are required to housekeep because
530 * #if:s can be nested. The ignore case is activated to prevent output from
531 * within a false clause.
532 * Some special cases come from the fact that the #elif cases are not
533 * binary, but three-state. The problem is that all other elif-cases must
534 * be false when one true one has been found. A second problem is that the
535 * #else clause is a final clause. No extra #else:s may follow.
538 * if_true Process input to output
539 * if_false Process input but no output
540 * if_ignore Process input but no output
541 * if_elif Process input but no output
542 * if_elsefalse Process input but no output
543 * if_elsettrue Process input to output
545 * The possible state-sequences are [state(stack depth)] (rest can be deduced):
546 * TOS #if 1 #else #endif
547 * if_true(n) if_true(n+1) if_elsefalse(n+1)
548 * if_false(n) if_ignore(n+1) if_ignore(n+1)
549 * if_elsetrue(n) if_true(n+1) if_elsefalse(n+1)
550 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1)
551 * if_elif(n) if_ignore(n+1) if_ignore(n+1)
552 * if_ignore(n) if_ignore(n+1) if_ignore(n+1)
554 * TOS #if 1 #elif 0 #else #endif
555 * if_true(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
556 * if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
557 * if_elsetrue(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
558 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
559 * if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
560 * if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
562 * TOS #if 0 #elif 1 #else #endif
563 * if_true(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
564 * if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
565 * if_elsetrue(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
566 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
567 * if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
568 * if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
570 *-------------------------------------------------------------------------
572 static const char * const pp_if_state_str
[] = {
581 void pp_push_if(pp_if_state_t s
)
583 if(if_stack_idx
>= MAXIFSTACK
)
584 pp_internal_error(__FILE__
, __LINE__
, "#if-stack overflow; #{if,ifdef,ifndef} nested too deeply (> %d)", MAXIFSTACK
);
587 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);
589 if_stack
[if_stack_idx
++] = s
;
600 pp_push_ignore_state();
603 pp_internal_error(__FILE__
, __LINE__
, "Invalid pp_if_state (%d)", (int)pp_if_state());
607 pp_if_state_t
pp_pop_if(void)
609 if(if_stack_idx
<= 0)
611 ppy_error("#{endif,else,elif} without #{if,ifdef,ifndef} (#if-stack underflow)");
615 switch(pp_if_state())
624 pp_pop_ignore_state();
627 pp_internal_error(__FILE__
, __LINE__
, "Invalid pp_if_state (%d)", (int)pp_if_state());
631 fprintf(stderr
, "Pop if %s:%d: %s(%d) -> %s(%d)\n",
633 pp_status
.line_number
,
634 pp_if_state_str
[pp_if_state()],
636 pp_if_state_str
[if_stack
[if_stack_idx
<= 1 ? if_true
: if_stack_idx
-2]],
639 return if_stack
[--if_stack_idx
];
642 pp_if_state_t
pp_if_state(void)
647 return if_stack
[if_stack_idx
-1];
651 void pp_next_if_state(int i
)
653 switch(pp_if_state())
657 pp_push_if(i
? if_true
: if_false
);
663 pp_push_if(if_ignore
);
666 pp_internal_error(__FILE__
, __LINE__
, "Invalid pp_if_state (%d) in #{if,ifdef,ifndef} directive", (int)pp_if_state());
670 int pp_get_if_depth(void)
675 /* #define WANT_NEAR_INDICATION */
677 static void generic_msg(const char *s
, const char *t
, const char *n
, va_list ap
)
679 fprintf(stderr
, "%s:%d:%d: %s: ", pp_status
.input
? pp_status
.input
: "stdin",
680 pp_status
.line_number
, pp_status
.char_number
, t
);
681 vfprintf(stderr
, s
, ap
);
682 #ifdef WANT_NEAR_INDICATION
690 for (p
= cpy
; *p
; p
++) if(!isprint(*p
)) *p
= ' ';
691 fprintf(stderr
, " near '%s'", cpy
);
697 fprintf(stderr
, "\n");
700 static void wpp_default_error(const char *file
, int line
, int col
, const char *near
, const char *msg
, va_list ap
)
702 generic_msg(msg
, "Error", near
, ap
);
706 static void wpp_default_warning(const char *file
, int line
, int col
, const char *near
, const char *msg
, va_list ap
)
708 generic_msg(msg
, "Warning", near
, ap
);
711 static const struct wpp_callbacks default_callbacks
=
722 const struct wpp_callbacks
*wpp_callbacks
= &default_callbacks
;
724 int ppy_error(const char *s
, ...)
728 wpp_callbacks
->error(pp_status
.input
, pp_status
.line_number
, pp_status
.char_number
, ppy_text
, s
, ap
);
734 int ppy_warning(const char *s
, ...)
738 wpp_callbacks
->warning(pp_status
.input
, pp_status
.line_number
, pp_status
.char_number
, ppy_text
, s
, ap
);
743 void pp_internal_error(const char *file
, int line
, const char *s
, ...)
747 fprintf(stderr
, "Internal error (please report) %s %d: ", file
, line
);
748 vfprintf(stderr
, s
, ap
);
749 fprintf(stderr
, "\n");