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 char *wpp_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 /* Don't comment on the hash, it's primitive but functional... */
194 static int pphash(const char *str
)
199 return sum
% HASHKEY
;
202 pp_entry_t
*pplookup(const char *ident
)
210 for(ppp
= pp_def_state
->defines
[idx
]; ppp
; ppp
= ppp
->next
)
212 if(!strcmp(ident
, ppp
->ident
))
218 static void free_pp_entry( pp_entry_t
*ppp
, int idx
)
222 if(ppp
->iep
== pp_includelogiclist
)
224 pp_includelogiclist
= ppp
->iep
->next
;
225 if(pp_includelogiclist
)
226 pp_includelogiclist
->prev
= NULL
;
230 ppp
->iep
->prev
->next
= ppp
->iep
->next
;
232 ppp
->iep
->next
->prev
= ppp
->iep
->prev
;
234 free(ppp
->iep
->filename
);
238 if(pp_def_state
->defines
[idx
] == ppp
)
240 pp_def_state
->defines
[idx
] = ppp
->next
;
241 if(pp_def_state
->defines
[idx
])
242 pp_def_state
->defines
[idx
]->prev
= NULL
;
246 ppp
->prev
->next
= ppp
->next
;
248 ppp
->next
->prev
= ppp
->prev
;
254 /* push a new (empty) define state */
255 int pp_push_define_state(void)
257 pp_def_state_t
*state
= pp_xmalloc( sizeof(*state
) );
261 memset( state
->defines
, 0, sizeof(state
->defines
) );
262 state
->next
= pp_def_state
;
263 pp_def_state
= state
;
267 /* pop the current define state */
268 void pp_pop_define_state(void)
272 pp_def_state_t
*state
;
274 for (i
= 0; i
< HASHKEY
; i
++)
276 while ((ppp
= pp_def_state
->defines
[i
]) != NULL
) pp_del_define( ppp
->ident
);
278 state
= pp_def_state
;
279 pp_def_state
= state
->next
;
283 void pp_del_define(const char *name
)
286 int idx
= pphash(name
);
288 if((ppp
= pplookup(name
)) == NULL
)
290 if(pp_status
.pedantic
)
291 ppy_warning("%s was not defined", name
);
296 printf("Deleting (%s, %d) <%s>\n", pp_status
.input
, pp_status
.line_number
, name
);
299 free( ppp
->subst
.text
);
300 free( ppp
->filename
);
301 free_pp_entry( ppp
, idx
);
304 pp_entry_t
*pp_add_define(const char *def
, const char *text
)
314 if((ppp
= pplookup(def
)) != NULL
)
316 if(pp_status
.pedantic
)
317 ppy_warning("Redefinition of %s\n\tPrevious definition: %s:%d", def
, ppp
->filename
, ppp
->linenumber
);
320 ppp
= pp_xmalloc(sizeof(pp_entry_t
));
323 memset( ppp
, 0, sizeof(*ppp
) );
324 ppp
->ident
= pp_xstrdup(def
);
327 ppp
->type
= def_define
;
328 ppp
->subst
.text
= text
? pp_xstrdup(text
) : NULL
;
329 if(text
&& !ppp
->subst
.text
)
331 ppp
->filename
= pp_xstrdup(pp_status
.input
? pp_status
.input
: "<internal or cmdline>");
334 ppp
->linenumber
= pp_status
.input
? pp_status
.line_number
: 0;
335 ppp
->next
= pp_def_state
->defines
[idx
];
336 pp_def_state
->defines
[idx
] = ppp
;
338 ppp
->next
->prev
= ppp
;
341 /* Strip trailing white space from subst text */
342 len
= strlen(ppp
->subst
.text
);
343 while(len
&& strchr(" \t\r\n", ppp
->subst
.text
[len
-1]))
345 ppp
->subst
.text
[--len
] = '\0';
347 /* Strip leading white space from subst text */
348 for(cptr
= ppp
->subst
.text
; *cptr
&& strchr(" \t\r", *cptr
); cptr
++)
350 if(ppp
->subst
.text
!= cptr
)
351 memmove(ppp
->subst
.text
, cptr
, strlen(cptr
)+1);
354 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)");
360 free(ppp
->subst
.text
);
365 pp_entry_t
*pp_add_macro(char *id
, marg_t
*args
[], int nargs
, mtext_t
*exp
)
373 if((ppp
= pplookup(id
)) != NULL
)
375 if(pp_status
.pedantic
)
376 ppy_warning("Redefinition of %s\n\tPrevious definition: %s:%d", id
, ppp
->filename
, ppp
->linenumber
);
379 ppp
= pp_xmalloc(sizeof(pp_entry_t
));
382 memset( ppp
, 0, sizeof(*ppp
) );
384 ppp
->type
= def_macro
;
387 ppp
->subst
.mtext
= exp
;
388 ppp
->filename
= pp_xstrdup(pp_status
.input
? pp_status
.input
: "<internal or cmdline>");
394 ppp
->linenumber
= pp_status
.input
? pp_status
.line_number
: 0;
395 ppp
->next
= pp_def_state
->defines
[idx
];
396 pp_def_state
->defines
[idx
] = ppp
;
398 ppp
->next
->prev
= ppp
;
402 fprintf(stderr
, "Added macro (%s, %d) <%s(%d)> to <", pp_status
.input
, pp_status
.line_number
, ppp
->ident
, nargs
);
403 for(; exp
; exp
= exp
->next
)
408 fprintf(stderr
, " \"%s\" ", exp
->subst
.text
);
411 fprintf(stderr
, " #(%d) ", exp
->subst
.argidx
);
414 fprintf(stderr
, "##");
417 fprintf(stderr
, " <%d> ", exp
->subst
.argidx
);
421 fprintf(stderr
, ">\n");
428 *-------------------------------------------------------------------------
430 *-------------------------------------------------------------------------
432 #if defined(_WIN32) || defined(__MSDOS__)
433 #define INCLUDESEPARATOR ";"
435 #define INCLUDESEPARATOR ":"
438 static char **includepath
;
439 static int nincludepath
= 0;
441 int wpp_add_include_path(const char *path
)
444 char *cpy
= pp_xstrdup(path
);
448 tok
= strtok(cpy
, INCLUDESEPARATOR
);
456 dir
= pp_xstrdup(tok
);
462 for(cptr
= dir
; *cptr
; cptr
++)
464 /* Convert to forward slash */
468 /* Kill eventual trailing '/' */
469 if(*(cptr
= dir
+ strlen(dir
)-1) == '/')
473 new_path
= pp_xrealloc(includepath
, (nincludepath
+1) * sizeof(*includepath
));
480 includepath
= new_path
;
481 includepath
[nincludepath
] = dir
;
484 tok
= strtok(NULL
, INCLUDESEPARATOR
);
490 char *wpp_find_include(const char *name
, const char *parent_name
)
492 return wpp_lookup(name
, !!parent_name
, parent_name
, includepath
, nincludepath
);
495 void *pp_open_include(const char *name
, int type
, const char *parent_name
, char **newpath
)
500 if (!(path
= wpp_lookup(name
, type
, parent_name
, includepath
, nincludepath
))) return NULL
;
501 fp
= fopen(path
, "rt");
506 printf("Going to include <%s>\n", path
);
507 if (newpath
) *newpath
= path
;
516 *-------------------------------------------------------------------------
517 * #if, #ifdef, #ifndef, #else, #elif and #endif state management
519 * #if state transitions are made on basis of the current TOS and the next
520 * required state. The state transitions are required to housekeep because
521 * #if:s can be nested. The ignore case is activated to prevent output from
522 * within a false clause.
523 * Some special cases come from the fact that the #elif cases are not
524 * binary, but three-state. The problem is that all other elif-cases must
525 * be false when one true one has been found. A second problem is that the
526 * #else clause is a final clause. No extra #else:s may follow.
529 * if_true Process input to output
530 * if_false Process input but no output
531 * if_ignore Process input but no output
532 * if_elif Process input but no output
533 * if_elsefalse Process input but no output
534 * if_elsettrue Process input to output
536 * The possible state-sequences are [state(stack depth)] (rest can be deduced):
537 * TOS #if 1 #else #endif
538 * if_true(n) if_true(n+1) if_elsefalse(n+1)
539 * if_false(n) if_ignore(n+1) if_ignore(n+1)
540 * if_elsetrue(n) if_true(n+1) if_elsefalse(n+1)
541 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1)
542 * if_elif(n) if_ignore(n+1) if_ignore(n+1)
543 * if_ignore(n) if_ignore(n+1) if_ignore(n+1)
545 * TOS #if 1 #elif 0 #else #endif
546 * if_true(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
547 * if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
548 * if_elsetrue(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
549 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
550 * if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
551 * if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
553 * TOS #if 0 #elif 1 #else #endif
554 * if_true(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
555 * if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
556 * if_elsetrue(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
557 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
558 * if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
559 * if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
561 *-------------------------------------------------------------------------
563 static const char * const pp_if_state_str
[] = {
572 void pp_push_if(pp_if_state_t s
)
574 if(if_stack_idx
>= MAXIFSTACK
)
575 pp_internal_error(__FILE__
, __LINE__
, "#if-stack overflow; #{if,ifdef,ifndef} nested too deeply (> %d)", MAXIFSTACK
);
578 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);
580 if_stack
[if_stack_idx
++] = s
;
591 pp_push_ignore_state();
594 pp_internal_error(__FILE__
, __LINE__
, "Invalid pp_if_state (%d)", (int)pp_if_state());
598 pp_if_state_t
pp_pop_if(void)
600 if(if_stack_idx
<= 0)
602 ppy_error("#{endif,else,elif} without #{if,ifdef,ifndef} (#if-stack underflow)");
606 switch(pp_if_state())
615 pp_pop_ignore_state();
618 pp_internal_error(__FILE__
, __LINE__
, "Invalid pp_if_state (%d)", (int)pp_if_state());
622 fprintf(stderr
, "Pop if %s:%d: %s(%d) -> %s(%d)\n",
624 pp_status
.line_number
,
625 pp_if_state_str
[pp_if_state()],
627 pp_if_state_str
[if_stack
[if_stack_idx
<= 1 ? if_true
: if_stack_idx
-2]],
630 return if_stack
[--if_stack_idx
];
633 pp_if_state_t
pp_if_state(void)
638 return if_stack
[if_stack_idx
-1];
642 void pp_next_if_state(int i
)
644 switch(pp_if_state())
648 pp_push_if(i
? if_true
: if_false
);
654 pp_push_if(if_ignore
);
657 pp_internal_error(__FILE__
, __LINE__
, "Invalid pp_if_state (%d) in #{if,ifdef,ifndef} directive", (int)pp_if_state());
661 int pp_get_if_depth(void)
666 /* #define WANT_NEAR_INDICATION */
668 static void generic_msg(const char *s
, const char *t
, const char *n
, va_list ap
)
670 fprintf(stderr
, "%s:%d:%d: %s: ", pp_status
.input
? pp_status
.input
: "stdin",
671 pp_status
.line_number
, pp_status
.char_number
, t
);
672 vfprintf(stderr
, s
, ap
);
673 #ifdef WANT_NEAR_INDICATION
681 for (p
= cpy
; *p
; p
++) if(!isprint(*p
)) *p
= ' ';
682 fprintf(stderr
, " near '%s'", cpy
);
688 fprintf(stderr
, "\n");
691 int ppy_error(const char *s
, ...)
695 generic_msg(s
, "error", ppy_text
, ap
);
700 int ppy_warning(const char *s
, ...)
704 generic_msg(s
, "warning", ppy_text
, ap
);
709 void pp_internal_error(const char *file
, int line
, const char *s
, ...)
713 fprintf(stderr
, "Internal error (please report) %s %d: ", file
, line
);
714 vfprintf(stderr
, s
, ap
);
715 fprintf(stderr
, "\n");