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, it's 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
) pp_del_define( ppp
->ident
);
297 state
= pp_def_state
;
298 pp_def_state
= state
->next
;
302 void pp_del_define(const char *name
)
305 int idx
= pphash(name
);
307 if((ppp
= pplookup(name
)) == NULL
)
309 if(pp_status
.pedantic
)
310 ppy_warning("%s was not defined", name
);
315 printf("Deleting (%s, %d) <%s>\n", pp_status
.input
, pp_status
.line_number
, name
);
318 free( ppp
->subst
.text
);
319 free( ppp
->filename
);
320 free_pp_entry( ppp
, idx
);
323 pp_entry_t
*pp_add_define(const char *def
, const char *text
)
333 if((ppp
= pplookup(def
)) != NULL
)
335 if(pp_status
.pedantic
)
336 ppy_warning("Redefinition of %s\n\tPrevious definition: %s:%d", def
, ppp
->filename
, ppp
->linenumber
);
339 ppp
= pp_xmalloc(sizeof(pp_entry_t
));
342 memset( ppp
, 0, sizeof(*ppp
) );
343 ppp
->ident
= pp_xstrdup(def
);
346 ppp
->type
= def_define
;
347 ppp
->subst
.text
= text
? pp_xstrdup(text
) : NULL
;
348 if(text
&& !ppp
->subst
.text
)
350 ppp
->filename
= pp_xstrdup(pp_status
.input
? pp_status
.input
: "<internal or cmdline>");
353 ppp
->linenumber
= pp_status
.input
? pp_status
.line_number
: 0;
354 ppp
->next
= pp_def_state
->defines
[idx
];
355 pp_def_state
->defines
[idx
] = ppp
;
357 ppp
->next
->prev
= ppp
;
360 /* Strip trailing white space from subst text */
361 len
= strlen(ppp
->subst
.text
);
362 while(len
&& strchr(" \t\r\n", ppp
->subst
.text
[len
-1]))
364 ppp
->subst
.text
[--len
] = '\0';
366 /* Strip leading white space from subst text */
367 for(cptr
= ppp
->subst
.text
; *cptr
&& strchr(" \t\r", *cptr
); cptr
++)
369 if(ppp
->subst
.text
!= cptr
)
370 memmove(ppp
->subst
.text
, cptr
, strlen(cptr
)+1);
373 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)");
379 free(ppp
->subst
.text
);
384 pp_entry_t
*pp_add_macro(char *id
, marg_t
*args
[], int nargs
, mtext_t
*exp
)
392 if((ppp
= pplookup(id
)) != NULL
)
394 if(pp_status
.pedantic
)
395 ppy_warning("Redefinition of %s\n\tPrevious definition: %s:%d", id
, ppp
->filename
, ppp
->linenumber
);
398 ppp
= pp_xmalloc(sizeof(pp_entry_t
));
401 memset( ppp
, 0, sizeof(*ppp
) );
403 ppp
->type
= def_macro
;
406 ppp
->subst
.mtext
= exp
;
407 ppp
->filename
= pp_xstrdup(pp_status
.input
? pp_status
.input
: "<internal or cmdline>");
413 ppp
->linenumber
= pp_status
.input
? pp_status
.line_number
: 0;
414 ppp
->next
= pp_def_state
->defines
[idx
];
415 pp_def_state
->defines
[idx
] = ppp
;
417 ppp
->next
->prev
= ppp
;
421 fprintf(stderr
, "Added macro (%s, %d) <%s(%d)> to <", pp_status
.input
, pp_status
.line_number
, ppp
->ident
, nargs
);
422 for(; exp
; exp
= exp
->next
)
427 fprintf(stderr
, " \"%s\" ", exp
->subst
.text
);
430 fprintf(stderr
, " #(%d) ", exp
->subst
.argidx
);
433 fprintf(stderr
, "##");
436 fprintf(stderr
, " <%d> ", exp
->subst
.argidx
);
440 fprintf(stderr
, ">\n");
447 *-------------------------------------------------------------------------
449 *-------------------------------------------------------------------------
451 #if defined(_WIN32) || defined(__MSDOS__)
452 #define INCLUDESEPARATOR ";"
454 #define INCLUDESEPARATOR ":"
457 static char **includepath
;
458 static int nincludepath
= 0;
460 int wpp_add_include_path(const char *path
)
463 char *cpy
= pp_xstrdup(path
);
467 tok
= strtok(cpy
, INCLUDESEPARATOR
);
475 dir
= pp_xstrdup(tok
);
481 for(cptr
= dir
; *cptr
; cptr
++)
483 /* Convert to forward slash */
487 /* Kill eventual trailing '/' */
488 if(*(cptr
= dir
+ strlen(dir
)-1) == '/')
492 new_path
= pp_xrealloc(includepath
, (nincludepath
+1) * sizeof(*includepath
));
499 includepath
= new_path
;
500 includepath
[nincludepath
] = dir
;
503 tok
= strtok(NULL
, INCLUDESEPARATOR
);
509 char *wpp_find_include(const char *name
, const char *parent_name
)
511 return wpp_default_lookup(name
, !!parent_name
, parent_name
, includepath
, nincludepath
);
514 void *pp_open_include(const char *name
, int type
, const char *parent_name
, char **newpath
)
519 if (!(path
= wpp_callbacks
->lookup(name
, type
, parent_name
, includepath
,
520 nincludepath
))) return NULL
;
521 fp
= wpp_callbacks
->open(path
, type
);
526 printf("Going to include <%s>\n", path
);
527 if (newpath
) *newpath
= path
;
536 *-------------------------------------------------------------------------
537 * #if, #ifdef, #ifndef, #else, #elif and #endif state management
539 * #if state transitions are made on basis of the current TOS and the next
540 * required state. The state transitions are required to housekeep because
541 * #if:s can be nested. The ignore case is activated to prevent output from
542 * within a false clause.
543 * Some special cases come from the fact that the #elif cases are not
544 * binary, but three-state. The problem is that all other elif-cases must
545 * be false when one true one has been found. A second problem is that the
546 * #else clause is a final clause. No extra #else:s may follow.
549 * if_true Process input to output
550 * if_false Process input but no output
551 * if_ignore Process input but no output
552 * if_elif Process input but no output
553 * if_elsefalse Process input but no output
554 * if_elsettrue Process input to output
556 * The possible state-sequences are [state(stack depth)] (rest can be deduced):
557 * TOS #if 1 #else #endif
558 * if_true(n) if_true(n+1) if_elsefalse(n+1)
559 * if_false(n) if_ignore(n+1) if_ignore(n+1)
560 * if_elsetrue(n) if_true(n+1) if_elsefalse(n+1)
561 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1)
562 * if_elif(n) if_ignore(n+1) if_ignore(n+1)
563 * if_ignore(n) if_ignore(n+1) if_ignore(n+1)
565 * TOS #if 1 #elif 0 #else #endif
566 * if_true(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
567 * if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
568 * if_elsetrue(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
569 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
570 * if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
571 * if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
573 * TOS #if 0 #elif 1 #else #endif
574 * if_true(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
575 * if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
576 * if_elsetrue(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
577 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
578 * if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
579 * if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
581 *-------------------------------------------------------------------------
583 static const char * const pp_if_state_str
[] = {
592 void pp_push_if(pp_if_state_t s
)
594 if(if_stack_idx
>= MAXIFSTACK
)
595 pp_internal_error(__FILE__
, __LINE__
, "#if-stack overflow; #{if,ifdef,ifndef} nested too deeply (> %d)", MAXIFSTACK
);
598 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);
600 if_stack
[if_stack_idx
++] = s
;
611 pp_push_ignore_state();
614 pp_internal_error(__FILE__
, __LINE__
, "Invalid pp_if_state (%d)", (int)pp_if_state());
618 pp_if_state_t
pp_pop_if(void)
620 if(if_stack_idx
<= 0)
622 ppy_error("#{endif,else,elif} without #{if,ifdef,ifndef} (#if-stack underflow)");
626 switch(pp_if_state())
635 pp_pop_ignore_state();
638 pp_internal_error(__FILE__
, __LINE__
, "Invalid pp_if_state (%d)", (int)pp_if_state());
642 fprintf(stderr
, "Pop if %s:%d: %s(%d) -> %s(%d)\n",
644 pp_status
.line_number
,
645 pp_if_state_str
[pp_if_state()],
647 pp_if_state_str
[if_stack
[if_stack_idx
<= 1 ? if_true
: if_stack_idx
-2]],
650 return if_stack
[--if_stack_idx
];
653 pp_if_state_t
pp_if_state(void)
658 return if_stack
[if_stack_idx
-1];
662 void pp_next_if_state(int i
)
664 switch(pp_if_state())
668 pp_push_if(i
? if_true
: if_false
);
674 pp_push_if(if_ignore
);
677 pp_internal_error(__FILE__
, __LINE__
, "Invalid pp_if_state (%d) in #{if,ifdef,ifndef} directive", (int)pp_if_state());
681 int pp_get_if_depth(void)
686 /* #define WANT_NEAR_INDICATION */
688 static void generic_msg(const char *s
, const char *t
, const char *n
, va_list ap
)
690 fprintf(stderr
, "%s:%d:%d: %s: ", pp_status
.input
? pp_status
.input
: "stdin",
691 pp_status
.line_number
, pp_status
.char_number
, t
);
692 vfprintf(stderr
, s
, ap
);
693 #ifdef WANT_NEAR_INDICATION
701 for (p
= cpy
; *p
; p
++) if(!isprint(*p
)) *p
= ' ';
702 fprintf(stderr
, " near '%s'", cpy
);
708 fprintf(stderr
, "\n");
711 static void wpp_default_error(const char *file
, int line
, int col
, const char *near
, const char *msg
, va_list ap
)
713 generic_msg(msg
, "Error", near
, ap
);
717 static void wpp_default_warning(const char *file
, int line
, int col
, const char *near
, const char *msg
, va_list ap
)
719 generic_msg(msg
, "Warning", near
, ap
);
722 static const struct wpp_callbacks default_callbacks
=
733 const struct wpp_callbacks
*wpp_callbacks
= &default_callbacks
;
735 int ppy_error(const char *s
, ...)
739 wpp_callbacks
->error(pp_status
.input
, pp_status
.line_number
, pp_status
.char_number
, ppy_text
, s
, ap
);
745 int ppy_warning(const char *s
, ...)
749 wpp_callbacks
->warning(pp_status
.input
, pp_status
.line_number
, pp_status
.char_number
, ppy_text
, s
, ap
);
754 void pp_internal_error(const char *file
, int line
, const char *s
, ...)
758 fprintf(stderr
, "Internal error (please report) %s %d: ", file
, line
);
759 vfprintf(stderr
, s
, ap
);
760 fprintf(stderr
, "\n");