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"
34 #include "wpp_private.h"
36 struct pp_status pp_status
;
40 typedef struct pp_def_state
42 struct pp_def_state
*next
;
43 pp_entry_t
*defines
[HASHKEY
];
46 static pp_def_state_t
*pp_def_state
;
49 static pp_if_state_t if_stack
[MAXIFSTACK
];
50 static int if_stack_idx
= 0;
53 void pp_print_status(void) __attribute__((destructor
));
54 void pp_print_status(void)
61 fprintf(stderr
, "Defines statistics:\n");
62 for(i
= 0; i
< HASHKEY
; i
++)
65 for(ppp
= pp_def_state
->defines
[i
]; ppp
; ppp
= ppp
->next
)
68 if (sum
) fprintf(stderr
, "%4d, %3d\n", i
, sum
);
70 fprintf(stderr
, "Total defines: %d\n", total
);
74 void *pp_xmalloc(size_t size
)
82 /* Set the error flag */
88 void *pp_xrealloc(void *p
, size_t size
)
93 res
= realloc(p
, size
);
96 /* Set the error flag */
102 char *pp_xstrdup(const char *str
)
112 return memcpy(s
, str
, len
);
115 static char *wpp_default_lookup(const char *name
, int type
, const char *parent_name
,
116 char **include_path
, int include_path_count
)
124 cpy
= pp_xmalloc(strlen(name
)+1);
129 for(ccptr
= name
; *ccptr
; ccptr
++)
131 /* Convert to forward slash */
133 /* kill double backslash */
144 if(type
&& parent_name
)
146 /* Search directory of parent include and then -I path */
149 if ((p
= strrchr( parent_name
, '/' ))) p
++;
150 else p
= parent_name
;
151 path
= pp_xmalloc( (p
- parent_name
) + strlen(cpy
) + 1 );
157 memcpy( path
, parent_name
, p
- parent_name
);
158 strcpy( path
+ (p
- parent_name
), cpy
);
159 fd
= open( path
, O_RDONLY
);
169 for(i
= 0; i
< include_path_count
; i
++)
171 path
= pp_xmalloc(strlen(include_path
[i
]) + strlen(cpy
) + 2);
177 strcpy(path
, include_path
[i
]);
180 fd
= open( path
, O_RDONLY
);
193 static void *wpp_default_open(const char *filename
, int type
) {
194 return fopen(filename
,"rt");
197 static void wpp_default_close(void *file
) {
201 static int wpp_default_read(void *file
, char *buffer
, unsigned int len
){
202 return fread(buffer
, 1, len
, file
);
205 static void wpp_default_write( const char *buffer
, unsigned int len
) {
206 fwrite(buffer
, 1, len
, ppy_out
);
209 /* Don't comment on the hash, it's primitive but functional... */
210 static int pphash(const char *str
)
215 return sum
% HASHKEY
;
218 pp_entry_t
*pplookup(const char *ident
)
226 for(ppp
= pp_def_state
->defines
[idx
]; ppp
; ppp
= ppp
->next
)
228 if(!strcmp(ident
, ppp
->ident
))
234 static void free_pp_entry( pp_entry_t
*ppp
, int idx
)
238 if(ppp
->iep
== pp_includelogiclist
)
240 pp_includelogiclist
= ppp
->iep
->next
;
241 if(pp_includelogiclist
)
242 pp_includelogiclist
->prev
= NULL
;
246 ppp
->iep
->prev
->next
= ppp
->iep
->next
;
248 ppp
->iep
->next
->prev
= ppp
->iep
->prev
;
250 free(ppp
->iep
->filename
);
254 if(pp_def_state
->defines
[idx
] == ppp
)
256 pp_def_state
->defines
[idx
] = ppp
->next
;
257 if(pp_def_state
->defines
[idx
])
258 pp_def_state
->defines
[idx
]->prev
= NULL
;
262 ppp
->prev
->next
= ppp
->next
;
264 ppp
->next
->prev
= ppp
->prev
;
270 /* push a new (empty) define state */
271 int pp_push_define_state(void)
273 pp_def_state_t
*state
= pp_xmalloc( sizeof(*state
) );
277 memset( state
->defines
, 0, sizeof(state
->defines
) );
278 state
->next
= pp_def_state
;
279 pp_def_state
= state
;
283 /* pop the current define state */
284 void pp_pop_define_state(void)
288 pp_def_state_t
*state
;
290 for (i
= 0; i
< HASHKEY
; i
++)
292 while ((ppp
= pp_def_state
->defines
[i
]) != NULL
) pp_del_define( ppp
->ident
);
294 state
= pp_def_state
;
295 pp_def_state
= state
->next
;
299 void pp_del_define(const char *name
)
302 int idx
= pphash(name
);
304 if((ppp
= pplookup(name
)) == NULL
)
306 if(pp_status
.pedantic
)
307 ppy_warning("%s was not defined", name
);
312 printf("Deleting (%s, %d) <%s>\n", pp_status
.input
, pp_status
.line_number
, name
);
315 free( ppp
->subst
.text
);
316 free( ppp
->filename
);
317 free_pp_entry( ppp
, idx
);
320 pp_entry_t
*pp_add_define(const char *def
, const char *text
)
330 if((ppp
= pplookup(def
)) != NULL
)
332 if(pp_status
.pedantic
)
333 ppy_warning("Redefinition of %s\n\tPrevious definition: %s:%d", def
, ppp
->filename
, ppp
->linenumber
);
336 ppp
= pp_xmalloc(sizeof(pp_entry_t
));
339 memset( ppp
, 0, sizeof(*ppp
) );
340 ppp
->ident
= pp_xstrdup(def
);
343 ppp
->type
= def_define
;
344 ppp
->subst
.text
= text
? pp_xstrdup(text
) : NULL
;
345 if(text
&& !ppp
->subst
.text
)
347 ppp
->filename
= pp_xstrdup(pp_status
.input
? pp_status
.input
: "<internal or cmdline>");
350 ppp
->linenumber
= pp_status
.input
? pp_status
.line_number
: 0;
351 ppp
->next
= pp_def_state
->defines
[idx
];
352 pp_def_state
->defines
[idx
] = ppp
;
354 ppp
->next
->prev
= ppp
;
357 /* Strip trailing white space from subst text */
358 len
= strlen(ppp
->subst
.text
);
359 while(len
&& strchr(" \t\r\n", ppp
->subst
.text
[len
-1]))
361 ppp
->subst
.text
[--len
] = '\0';
363 /* Strip leading white space from subst text */
364 for(cptr
= ppp
->subst
.text
; *cptr
&& strchr(" \t\r", *cptr
); cptr
++)
366 if(ppp
->subst
.text
!= cptr
)
367 memmove(ppp
->subst
.text
, cptr
, strlen(cptr
)+1);
370 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)");
376 free(ppp
->subst
.text
);
381 pp_entry_t
*pp_add_macro(char *id
, marg_t
*args
[], int nargs
, mtext_t
*exp
)
389 if((ppp
= pplookup(id
)) != NULL
)
391 if(pp_status
.pedantic
)
392 ppy_warning("Redefinition of %s\n\tPrevious definition: %s:%d", id
, ppp
->filename
, ppp
->linenumber
);
395 ppp
= pp_xmalloc(sizeof(pp_entry_t
));
398 memset( ppp
, 0, sizeof(*ppp
) );
400 ppp
->type
= def_macro
;
403 ppp
->subst
.mtext
= exp
;
404 ppp
->filename
= pp_xstrdup(pp_status
.input
? pp_status
.input
: "<internal or cmdline>");
410 ppp
->linenumber
= pp_status
.input
? pp_status
.line_number
: 0;
411 ppp
->next
= pp_def_state
->defines
[idx
];
412 pp_def_state
->defines
[idx
] = ppp
;
414 ppp
->next
->prev
= ppp
;
418 fprintf(stderr
, "Added macro (%s, %d) <%s(%d)> to <", pp_status
.input
, pp_status
.line_number
, ppp
->ident
, nargs
);
419 for(; exp
; exp
= exp
->next
)
424 fprintf(stderr
, " \"%s\" ", exp
->subst
.text
);
427 fprintf(stderr
, " #(%d) ", exp
->subst
.argidx
);
430 fprintf(stderr
, "##");
433 fprintf(stderr
, " <%d> ", exp
->subst
.argidx
);
437 fprintf(stderr
, ">\n");
444 *-------------------------------------------------------------------------
446 *-------------------------------------------------------------------------
448 #if defined(_WIN32) || defined(__MSDOS__)
449 #define INCLUDESEPARATOR ";"
451 #define INCLUDESEPARATOR ":"
454 static char **includepath
;
455 static int nincludepath
= 0;
457 int wpp_add_include_path(const char *path
)
460 char *cpy
= pp_xstrdup(path
);
464 tok
= strtok(cpy
, INCLUDESEPARATOR
);
472 dir
= pp_xstrdup(tok
);
478 for(cptr
= dir
; *cptr
; cptr
++)
480 /* Convert to forward slash */
484 /* Kill eventual trailing '/' */
485 if(*(cptr
= dir
+ strlen(dir
)-1) == '/')
489 new_path
= pp_xrealloc(includepath
, (nincludepath
+1) * sizeof(*includepath
));
496 includepath
= new_path
;
497 includepath
[nincludepath
] = dir
;
500 tok
= strtok(NULL
, INCLUDESEPARATOR
);
506 char *wpp_find_include(const char *name
, const char *parent_name
)
508 return wpp_default_lookup(name
, !!parent_name
, parent_name
, includepath
, nincludepath
);
511 void *pp_open_include(const char *name
, int type
, const char *parent_name
, char **newpath
)
516 if (!(path
= wpp_callbacks
->lookup(name
, type
, parent_name
, includepath
,
517 nincludepath
))) return NULL
;
518 fp
= wpp_callbacks
->open(path
, type
);
523 printf("Going to include <%s>\n", path
);
524 if (newpath
) *newpath
= path
;
533 *-------------------------------------------------------------------------
534 * #if, #ifdef, #ifndef, #else, #elif and #endif state management
536 * #if state transitions are made on basis of the current TOS and the next
537 * required state. The state transitions are required to housekeep because
538 * #if:s can be nested. The ignore case is activated to prevent output from
539 * within a false clause.
540 * Some special cases come from the fact that the #elif cases are not
541 * binary, but three-state. The problem is that all other elif-cases must
542 * be false when one true one has been found. A second problem is that the
543 * #else clause is a final clause. No extra #else:s may follow.
546 * if_true Process input to output
547 * if_false Process input but no output
548 * if_ignore Process input but no output
549 * if_elif Process input but no output
550 * if_elsefalse Process input but no output
551 * if_elsettrue Process input to output
553 * The possible state-sequences are [state(stack depth)] (rest can be deduced):
554 * TOS #if 1 #else #endif
555 * if_true(n) if_true(n+1) if_elsefalse(n+1)
556 * if_false(n) if_ignore(n+1) if_ignore(n+1)
557 * if_elsetrue(n) if_true(n+1) if_elsefalse(n+1)
558 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1)
559 * if_elif(n) if_ignore(n+1) if_ignore(n+1)
560 * if_ignore(n) if_ignore(n+1) if_ignore(n+1)
562 * TOS #if 1 #elif 0 #else #endif
563 * if_true(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
564 * if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
565 * if_elsetrue(n) if_true(n+1) if_elif(n+1) if_elif(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 * TOS #if 0 #elif 1 #else #endif
571 * if_true(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
572 * if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
573 * if_elsetrue(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
574 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
575 * if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
576 * if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
578 *-------------------------------------------------------------------------
580 static const char * const pp_if_state_str
[] = {
589 void pp_push_if(pp_if_state_t s
)
591 if(if_stack_idx
>= MAXIFSTACK
)
592 pp_internal_error(__FILE__
, __LINE__
, "#if-stack overflow; #{if,ifdef,ifndef} nested too deeply (> %d)", MAXIFSTACK
);
595 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);
597 if_stack
[if_stack_idx
++] = s
;
608 pp_push_ignore_state();
611 pp_internal_error(__FILE__
, __LINE__
, "Invalid pp_if_state (%d)", (int)pp_if_state());
615 pp_if_state_t
pp_pop_if(void)
617 if(if_stack_idx
<= 0)
619 ppy_error("#{endif,else,elif} without #{if,ifdef,ifndef} (#if-stack underflow)");
623 switch(pp_if_state())
632 pp_pop_ignore_state();
635 pp_internal_error(__FILE__
, __LINE__
, "Invalid pp_if_state (%d)", (int)pp_if_state());
639 fprintf(stderr
, "Pop if %s:%d: %s(%d) -> %s(%d)\n",
641 pp_status
.line_number
,
642 pp_if_state_str
[pp_if_state()],
644 pp_if_state_str
[if_stack
[if_stack_idx
<= 1 ? if_true
: if_stack_idx
-2]],
647 return if_stack
[--if_stack_idx
];
650 pp_if_state_t
pp_if_state(void)
655 return if_stack
[if_stack_idx
-1];
659 void pp_next_if_state(int i
)
661 switch(pp_if_state())
665 pp_push_if(i
? if_true
: if_false
);
671 pp_push_if(if_ignore
);
674 pp_internal_error(__FILE__
, __LINE__
, "Invalid pp_if_state (%d) in #{if,ifdef,ifndef} directive", (int)pp_if_state());
678 int pp_get_if_depth(void)
683 /* #define WANT_NEAR_INDICATION */
685 static void generic_msg(const char *s
, const char *t
, const char *n
, va_list ap
)
687 fprintf(stderr
, "%s:%d:%d: %s: ", pp_status
.input
? pp_status
.input
: "stdin",
688 pp_status
.line_number
, pp_status
.char_number
, t
);
689 vfprintf(stderr
, s
, ap
);
690 #ifdef WANT_NEAR_INDICATION
698 for (p
= cpy
; *p
; p
++) if(!isprint(*p
)) *p
= ' ';
699 fprintf(stderr
, " near '%s'", cpy
);
705 fprintf(stderr
, "\n");
708 static void wpp_default_error(const char *file
, int line
, int col
, const char *near
, const char *msg
, va_list ap
)
710 generic_msg(msg
, "Error", near
, ap
);
714 static void wpp_default_warning(const char *file
, int line
, int col
, const char *near
, const char *msg
, va_list ap
)
716 generic_msg(msg
, "Warning", near
, ap
);
719 static const struct wpp_callbacks default_callbacks
=
730 const struct wpp_callbacks
*wpp_callbacks
= &default_callbacks
;
732 int ppy_error(const char *s
, ...)
736 wpp_callbacks
->error(pp_status
.input
, pp_status
.line_number
, pp_status
.char_number
, ppy_text
, s
, ap
);
742 int ppy_warning(const char *s
, ...)
746 wpp_callbacks
->warning(pp_status
.input
, pp_status
.line_number
, pp_status
.char_number
, ppy_text
, s
, ap
);
751 void pp_internal_error(const char *file
, int line
, const char *s
, ...)
755 fprintf(stderr
, "Internal error (please report) %s %d: ", file
, line
);
756 vfprintf(stderr
, s
, ap
);
757 fprintf(stderr
, "\n");