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 fprintf(stderr
, "Virtual memory exhausted.\n");
91 void *pp_xrealloc(void *p
, size_t size
)
96 res
= realloc(p
, size
);
99 fprintf(stderr
, "Virtual memory exhausted.\n");
105 char *pp_xstrdup(const char *str
)
113 return memcpy(s
, str
, len
);
116 /* Don't comment on the hash, its primitive but functional... */
117 static int pphash(const char *str
)
122 return sum
% HASHKEY
;
125 pp_entry_t
*pplookup(const char *ident
)
127 int idx
= pphash(ident
);
130 for(ppp
= pp_def_state
->defines
[idx
]; ppp
; ppp
= ppp
->next
)
132 if(!strcmp(ident
, ppp
->ident
))
138 static void free_pp_entry( pp_entry_t
*ppp
, int idx
)
142 if(ppp
->iep
== pp_includelogiclist
)
144 pp_includelogiclist
= ppp
->iep
->next
;
145 if(pp_includelogiclist
)
146 pp_includelogiclist
->prev
= NULL
;
150 ppp
->iep
->prev
->next
= ppp
->iep
->next
;
152 ppp
->iep
->next
->prev
= ppp
->iep
->prev
;
154 free(ppp
->iep
->filename
);
158 if(pp_def_state
->defines
[idx
] == ppp
)
160 pp_def_state
->defines
[idx
] = ppp
->next
;
161 if(pp_def_state
->defines
[idx
])
162 pp_def_state
->defines
[idx
]->prev
= NULL
;
166 ppp
->prev
->next
= ppp
->next
;
168 ppp
->next
->prev
= ppp
->prev
;
174 /* push a new (empty) define state */
175 void pp_push_define_state(void)
177 pp_def_state_t
*state
= pp_xmalloc( sizeof(*state
) );
179 memset( state
->defines
, 0, sizeof(state
->defines
) );
180 state
->next
= pp_def_state
;
181 pp_def_state
= state
;
184 /* pop the current define state */
185 void pp_pop_define_state(void)
189 pp_def_state_t
*state
;
191 for (i
= 0; i
< HASHKEY
; i
++)
193 while ((ppp
= pp_def_state
->defines
[i
]) != NULL
) free_pp_entry( ppp
, i
);
195 state
= pp_def_state
;
196 pp_def_state
= state
->next
;
200 void pp_del_define(const char *name
)
204 if((ppp
= pplookup(name
)) == NULL
)
206 if(pp_status
.pedantic
)
207 ppy_warning("%s was not defined", name
);
211 free_pp_entry( ppp
, pphash(name
) );
214 printf("Deleted (%s, %d) <%s>\n", pp_status
.input
, pp_status
.line_number
, name
);
217 pp_entry_t
*pp_add_define(char *def
, char *text
)
221 int idx
= pphash(def
);
224 if((ppp
= pplookup(def
)) != NULL
)
226 if(pp_status
.pedantic
)
227 ppy_warning("Redefinition of %s\n\tPrevious definition: %s:%d", def
, ppp
->filename
, ppp
->linenumber
);
230 ppp
= pp_xmalloc(sizeof(pp_entry_t
));
231 memset( ppp
, 0, sizeof(*ppp
) );
233 ppp
->type
= def_define
;
234 ppp
->subst
.text
= text
;
235 ppp
->filename
= pp_xstrdup(pp_status
.input
? pp_status
.input
: "<internal or cmdline>");
236 ppp
->linenumber
= pp_status
.input
? pp_status
.line_number
: 0;
237 ppp
->next
= pp_def_state
->defines
[idx
];
238 pp_def_state
->defines
[idx
] = ppp
;
240 ppp
->next
->prev
= ppp
;
243 /* Strip trailing white space from subst text */
245 while(len
&& strchr(" \t\r\n", text
[len
-1]))
249 /* Strip leading white space from subst text */
250 for(cptr
= text
; *cptr
&& strchr(" \t\r", *cptr
); cptr
++)
253 memmove(text
, cptr
, strlen(cptr
)+1);
256 printf("Added define (%s, %d) <%s> to <%s>\n", pp_status
.input
, pp_status
.line_number
, ppp
->ident
, text
? text
: "(null)");
261 pp_entry_t
*pp_add_macro(char *id
, marg_t
*args
[], int nargs
, mtext_t
*exp
)
263 int idx
= pphash(id
);
266 if((ppp
= pplookup(id
)) != NULL
)
268 if(pp_status
.pedantic
)
269 ppy_warning("Redefinition of %s\n\tPrevious definition: %s:%d", id
, ppp
->filename
, ppp
->linenumber
);
272 ppp
= pp_xmalloc(sizeof(pp_entry_t
));
273 memset( ppp
, 0, sizeof(*ppp
) );
275 ppp
->type
= def_macro
;
278 ppp
->subst
.mtext
= exp
;
279 ppp
->filename
= pp_xstrdup(pp_status
.input
? pp_status
.input
: "<internal or cmdline>");
280 ppp
->linenumber
= pp_status
.input
? pp_status
.line_number
: 0;
281 ppp
->next
= pp_def_state
->defines
[idx
];
282 pp_def_state
->defines
[idx
] = ppp
;
284 ppp
->next
->prev
= ppp
;
288 fprintf(stderr
, "Added macro (%s, %d) <%s(%d)> to <", pp_status
.input
, pp_status
.line_number
, ppp
->ident
, nargs
);
289 for(; exp
; exp
= exp
->next
)
294 fprintf(stderr
, " \"%s\" ", exp
->subst
.text
);
297 fprintf(stderr
, " #(%d) ", exp
->subst
.argidx
);
300 fprintf(stderr
, "##");
303 fprintf(stderr
, " <%d> ", exp
->subst
.argidx
);
307 fprintf(stderr
, ">\n");
314 *-------------------------------------------------------------------------
316 *-------------------------------------------------------------------------
318 #if defined(_Windows) || defined(__MSDOS__)
319 #define INCLUDESEPARATOR ";"
321 #define INCLUDESEPARATOR ":"
324 static char **includepath
;
325 static int nincludepath
= 0;
327 void wpp_add_include_path(const char *path
)
330 char *cpy
= pp_xstrdup(path
);
332 tok
= strtok(cpy
, INCLUDESEPARATOR
);
338 dir
= pp_xstrdup(tok
);
339 for(cptr
= dir
; *cptr
; cptr
++)
341 /* Convert to forward slash */
345 /* Kill eventual trailing '/' */
346 if(*(cptr
= dir
+ strlen(dir
)-1) == '/')
351 includepath
= pp_xrealloc(includepath
, nincludepath
* sizeof(*includepath
));
352 includepath
[nincludepath
-1] = dir
;
354 tok
= strtok(NULL
, INCLUDESEPARATOR
);
359 char *wpp_find_include(const char *name
, const char *parent_name
)
367 cpy
= pp_xmalloc(strlen(name
)+1);
370 for(ccptr
= name
; *ccptr
; ccptr
++)
372 /* Convert to forward slash */
374 /* kill double backslash */
387 /* Search directory of parent include and then -I path */
390 if ((p
= strrchr( parent_name
, '/' ))) p
++;
391 else p
= parent_name
;
392 path
= pp_xmalloc( (p
- parent_name
) + strlen(cpy
) + 1 );
393 memcpy( path
, parent_name
, p
- parent_name
);
394 strcpy( path
+ (p
- parent_name
), cpy
);
395 fd
= open( path
, O_RDONLY
);
405 for(i
= 0; i
< nincludepath
; i
++)
407 path
= pp_xmalloc(strlen(includepath
[i
]) + strlen(cpy
) + 2);
408 strcpy(path
, includepath
[i
]);
411 fd
= open( path
, O_RDONLY
);
424 FILE *pp_open_include(const char *name
, const char *parent_name
, char **newpath
)
429 if (!(path
= wpp_find_include( name
, parent_name
))) return NULL
;
430 fp
= fopen(path
, "rt");
435 printf("Going to include <%s>\n", path
);
436 if (newpath
) *newpath
= path
;
445 *-------------------------------------------------------------------------
446 * #if, #ifdef, #ifndef, #else, #elif and #endif state management
448 * #if state transitions are made on basis of the current TOS and the next
449 * required state. The state transitions are required to housekeep because
450 * #if:s can be nested. The ignore case is activated to prevent output from
451 * within a false clause.
452 * Some special cases come from the fact that the #elif cases are not
453 * binary, but three-state. The problem is that all other elif-cases must
454 * be false when one true one has been found. A second problem is that the
455 * #else clause is a final clause. No extra #else:s may follow.
458 * if_true Process input to output
459 * if_false Process input but no output
460 * if_ignore Process input but no output
461 * if_elif Process input but no output
462 * if_elsefalse Process input but no output
463 * if_elsettrue Process input to output
465 * The possible state-sequences are [state(stack depth)] (rest can be deduced):
466 * TOS #if 1 #else #endif
467 * if_true(n) if_true(n+1) if_elsefalse(n+1)
468 * if_false(n) if_ignore(n+1) if_ignore(n+1)
469 * if_elsetrue(n) if_true(n+1) if_elsefalse(n+1)
470 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1)
471 * if_elif(n) if_ignore(n+1) if_ignore(n+1)
472 * if_ignore(n) if_ignore(n+1) if_ignore(n+1)
474 * TOS #if 1 #elif 0 #else #endif
475 * if_true(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
476 * if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
477 * if_elsetrue(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
478 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
479 * if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
480 * if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
482 * TOS #if 0 #elif 1 #else #endif
483 * if_true(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
484 * if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
485 * if_elsetrue(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
486 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
487 * if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
488 * if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
490 *-------------------------------------------------------------------------
492 static const char * const pp_if_state_str
[] = {
501 void pp_push_if(pp_if_state_t s
)
503 if(if_stack_idx
>= MAXIFSTACK
)
504 pp_internal_error(__FILE__
, __LINE__
, "#if-stack overflow; #{if,ifdef,ifndef} nested too deeply (> %d)", MAXIFSTACK
);
507 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);
509 if_stack
[if_stack_idx
++] = s
;
520 pp_push_ignore_state();
525 pp_if_state_t
pp_pop_if(void)
527 if(if_stack_idx
<= 0)
528 ppy_error("#{endif,else,elif} without #{if,ifdef,ifndef} (#if-stack underflow)");
530 switch(pp_if_state())
539 pp_pop_ignore_state();
544 fprintf(stderr
, "Pop if %s:%d: %s(%d) -> %s(%d)\n",
546 pp_status
.line_number
,
547 pp_if_state_str
[pp_if_state()],
549 pp_if_state_str
[if_stack
[if_stack_idx
<= 1 ? if_true
: if_stack_idx
-2]],
552 return if_stack
[--if_stack_idx
];
555 pp_if_state_t
pp_if_state(void)
560 return if_stack
[if_stack_idx
-1];
564 void pp_next_if_state(int i
)
566 switch(pp_if_state())
570 pp_push_if(i
? if_true
: if_false
);
576 pp_push_if(if_ignore
);
579 pp_internal_error(__FILE__
, __LINE__
, "Invalid pp_if_state (%d) in #{if,ifdef,ifndef} directive", (int)pp_if_state());
583 int pp_get_if_depth(void)
588 /* #define WANT_NEAR_INDICATION */
590 static void generic_msg(const char *s
, const char *t
, const char *n
, va_list ap
)
592 fprintf(stderr
, "%s:%d:%d: %s: ", pp_status
.input
? pp_status
.input
: "stdin",
593 pp_status
.line_number
, pp_status
.char_number
, t
);
594 vfprintf(stderr
, s
, ap
);
595 #ifdef WANT_NEAR_INDICATION
601 for (p
= cpy
; *p
; p
++) if(!isprint(*p
)) *p
= ' ';
602 fprintf(stderr
, " near '%s'", cpy
);
607 fprintf(stderr
, "\n");
610 int ppy_error(const char *s
, ...)
614 generic_msg(s
, "Error", ppy_text
, ap
);
620 int ppy_warning(const char *s
, ...)
624 generic_msg(s
, "Warning", ppy_text
, ap
);
629 void pp_internal_error(const char *file
, int line
, const char *s
, ...)
633 fprintf(stderr
, "Internal error (please report) %s %d: ", file
, line
);
634 vfprintf(stderr
, s
, ap
);
635 fprintf(stderr
, "\n");