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
)
115 return memcpy(s
, str
, len
);
118 /* Don't comment on the hash, its primitive but functional... */
119 static int pphash(const char *str
)
124 return sum
% HASHKEY
;
127 pp_entry_t
*pplookup(const char *ident
)
129 int idx
= pphash(ident
);
132 for(ppp
= pp_def_state
->defines
[idx
]; ppp
; ppp
= ppp
->next
)
134 if(!strcmp(ident
, ppp
->ident
))
140 static void free_pp_entry( pp_entry_t
*ppp
, int idx
)
144 if(ppp
->iep
== pp_includelogiclist
)
146 pp_includelogiclist
= ppp
->iep
->next
;
147 if(pp_includelogiclist
)
148 pp_includelogiclist
->prev
= NULL
;
152 ppp
->iep
->prev
->next
= ppp
->iep
->next
;
154 ppp
->iep
->next
->prev
= ppp
->iep
->prev
;
156 free(ppp
->iep
->filename
);
160 if(pp_def_state
->defines
[idx
] == ppp
)
162 pp_def_state
->defines
[idx
] = ppp
->next
;
163 if(pp_def_state
->defines
[idx
])
164 pp_def_state
->defines
[idx
]->prev
= NULL
;
168 ppp
->prev
->next
= ppp
->next
;
170 ppp
->next
->prev
= ppp
->prev
;
176 /* push a new (empty) define state */
177 void pp_push_define_state(void)
179 pp_def_state_t
*state
= pp_xmalloc( sizeof(*state
) );
181 memset( state
->defines
, 0, sizeof(state
->defines
) );
182 state
->next
= pp_def_state
;
183 pp_def_state
= state
;
186 /* pop the current define state */
187 void pp_pop_define_state(void)
191 pp_def_state_t
*state
;
193 for (i
= 0; i
< HASHKEY
; i
++)
195 while ((ppp
= pp_def_state
->defines
[i
]) != NULL
) free_pp_entry( ppp
, i
);
197 state
= pp_def_state
;
198 pp_def_state
= state
->next
;
202 void pp_del_define(const char *name
)
206 if((ppp
= pplookup(name
)) == NULL
)
208 if(pp_status
.pedantic
)
209 ppy_warning("%s was not defined", name
);
213 free_pp_entry( ppp
, pphash(name
) );
216 printf("Deleted (%s, %d) <%s>\n", pp_status
.input
, pp_status
.line_number
, name
);
219 pp_entry_t
*pp_add_define(char *def
, char *text
)
223 int idx
= pphash(def
);
226 if((ppp
= pplookup(def
)) != NULL
)
228 if(pp_status
.pedantic
)
229 ppy_warning("Redefinition of %s\n\tPrevious definition: %s:%d", def
, ppp
->filename
, ppp
->linenumber
);
232 ppp
= pp_xmalloc(sizeof(pp_entry_t
));
233 memset( ppp
, 0, sizeof(*ppp
) );
235 ppp
->type
= def_define
;
236 ppp
->subst
.text
= text
;
237 ppp
->filename
= pp_xstrdup(pp_status
.input
? pp_status
.input
: "<internal or cmdline>");
238 ppp
->linenumber
= pp_status
.input
? pp_status
.line_number
: 0;
239 ppp
->next
= pp_def_state
->defines
[idx
];
240 pp_def_state
->defines
[idx
] = ppp
;
242 ppp
->next
->prev
= ppp
;
245 /* Strip trailing white space from subst text */
247 while(len
&& strchr(" \t\r\n", text
[len
-1]))
251 /* Strip leading white space from subst text */
252 for(cptr
= text
; *cptr
&& strchr(" \t\r", *cptr
); cptr
++)
255 memmove(text
, cptr
, strlen(cptr
)+1);
258 printf("Added define (%s, %d) <%s> to <%s>\n", pp_status
.input
, pp_status
.line_number
, ppp
->ident
, text
? text
: "(null)");
263 pp_entry_t
*pp_add_macro(char *id
, marg_t
*args
[], int nargs
, mtext_t
*exp
)
265 int idx
= pphash(id
);
268 if((ppp
= pplookup(id
)) != NULL
)
270 if(pp_status
.pedantic
)
271 ppy_warning("Redefinition of %s\n\tPrevious definition: %s:%d", id
, ppp
->filename
, ppp
->linenumber
);
274 ppp
= pp_xmalloc(sizeof(pp_entry_t
));
275 memset( ppp
, 0, sizeof(*ppp
) );
277 ppp
->type
= def_macro
;
280 ppp
->subst
.mtext
= exp
;
281 ppp
->filename
= pp_xstrdup(pp_status
.input
? pp_status
.input
: "<internal or cmdline>");
282 ppp
->linenumber
= pp_status
.input
? pp_status
.line_number
: 0;
283 ppp
->next
= pp_def_state
->defines
[idx
];
284 pp_def_state
->defines
[idx
] = ppp
;
286 ppp
->next
->prev
= ppp
;
290 fprintf(stderr
, "Added macro (%s, %d) <%s(%d)> to <", pp_status
.input
, pp_status
.line_number
, ppp
->ident
, nargs
);
291 for(; exp
; exp
= exp
->next
)
296 fprintf(stderr
, " \"%s\" ", exp
->subst
.text
);
299 fprintf(stderr
, " #(%d) ", exp
->subst
.argidx
);
302 fprintf(stderr
, "##");
305 fprintf(stderr
, " <%d> ", exp
->subst
.argidx
);
309 fprintf(stderr
, ">\n");
316 *-------------------------------------------------------------------------
318 *-------------------------------------------------------------------------
320 #if defined(_Windows) || defined(__MSDOS__)
321 #define INCLUDESEPARATOR ";"
323 #define INCLUDESEPARATOR ":"
326 static char **includepath
;
327 static int nincludepath
= 0;
329 void wpp_add_include_path(const char *path
)
332 char *cpy
= pp_xstrdup(path
);
334 tok
= strtok(cpy
, INCLUDESEPARATOR
);
340 dir
= pp_xstrdup(tok
);
341 for(cptr
= dir
; *cptr
; cptr
++)
343 /* Convert to forward slash */
347 /* Kill eventual trailing '/' */
348 if(*(cptr
= dir
+ strlen(dir
)-1) == '/')
353 includepath
= pp_xrealloc(includepath
, nincludepath
* sizeof(*includepath
));
354 includepath
[nincludepath
-1] = dir
;
356 tok
= strtok(NULL
, INCLUDESEPARATOR
);
361 char *wpp_find_include(const char *name
, const char *parent_name
)
369 cpy
= pp_xmalloc(strlen(name
)+1);
372 for(ccptr
= name
; *ccptr
; ccptr
++)
374 /* Convert to forward slash */
376 /* kill double backslash */
389 /* Search directory of parent include and then -I path */
392 if ((p
= strrchr( parent_name
, '/' ))) p
++;
393 else p
= parent_name
;
394 path
= pp_xmalloc( (p
- parent_name
) + strlen(cpy
) + 1 );
395 memcpy( path
, parent_name
, p
- parent_name
);
396 strcpy( path
+ (p
- parent_name
), cpy
);
397 fd
= open( path
, O_RDONLY
);
407 for(i
= 0; i
< nincludepath
; i
++)
409 path
= pp_xmalloc(strlen(includepath
[i
]) + strlen(cpy
) + 2);
410 strcpy(path
, includepath
[i
]);
413 fd
= open( path
, O_RDONLY
);
426 FILE *pp_open_include(const char *name
, const char *parent_name
, char **newpath
)
431 if (!(path
= wpp_find_include( name
, parent_name
))) return NULL
;
432 fp
= fopen(path
, "rt");
437 printf("Going to include <%s>\n", path
);
438 if (newpath
) *newpath
= path
;
447 *-------------------------------------------------------------------------
448 * #if, #ifdef, #ifndef, #else, #elif and #endif state management
450 * #if state transitions are made on basis of the current TOS and the next
451 * required state. The state transitions are required to housekeep because
452 * #if:s can be nested. The ignore case is activated to prevent output from
453 * within a false clause.
454 * Some special cases come from the fact that the #elif cases are not
455 * binary, but three-state. The problem is that all other elif-cases must
456 * be false when one true one has been found. A second problem is that the
457 * #else clause is a final clause. No extra #else:s may follow.
460 * if_true Process input to output
461 * if_false Process input but no output
462 * if_ignore Process input but no output
463 * if_elif Process input but no output
464 * if_elsefalse Process input but no output
465 * if_elsettrue Process input to output
467 * The possible state-sequences are [state(stack depth)] (rest can be deduced):
468 * TOS #if 1 #else #endif
469 * if_true(n) if_true(n+1) if_elsefalse(n+1)
470 * if_false(n) if_ignore(n+1) if_ignore(n+1)
471 * if_elsetrue(n) if_true(n+1) if_elsefalse(n+1)
472 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1)
473 * if_elif(n) if_ignore(n+1) if_ignore(n+1)
474 * if_ignore(n) if_ignore(n+1) if_ignore(n+1)
476 * TOS #if 1 #elif 0 #else #endif
477 * if_true(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
478 * if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
479 * if_elsetrue(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
480 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
481 * if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
482 * if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
484 * TOS #if 0 #elif 1 #else #endif
485 * if_true(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
486 * if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
487 * if_elsetrue(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
488 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
489 * if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
490 * if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
492 *-------------------------------------------------------------------------
494 static const char * const pp_if_state_str
[] = {
503 void pp_push_if(pp_if_state_t s
)
505 if(if_stack_idx
>= MAXIFSTACK
)
506 pp_internal_error(__FILE__
, __LINE__
, "#if-stack overflow; #{if,ifdef,ifndef} nested too deeply (> %d)", MAXIFSTACK
);
509 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);
511 if_stack
[if_stack_idx
++] = s
;
522 pp_push_ignore_state();
525 pp_internal_error(__FILE__
, __LINE__
, "Invalid pp_if_state (%d)", (int)pp_if_state());
529 pp_if_state_t
pp_pop_if(void)
531 if(if_stack_idx
<= 0)
533 ppy_error("#{endif,else,elif} without #{if,ifdef,ifndef} (#if-stack underflow)");
537 switch(pp_if_state())
546 pp_pop_ignore_state();
549 pp_internal_error(__FILE__
, __LINE__
, "Invalid pp_if_state (%d)", (int)pp_if_state());
553 fprintf(stderr
, "Pop if %s:%d: %s(%d) -> %s(%d)\n",
555 pp_status
.line_number
,
556 pp_if_state_str
[pp_if_state()],
558 pp_if_state_str
[if_stack
[if_stack_idx
<= 1 ? if_true
: if_stack_idx
-2]],
561 return if_stack
[--if_stack_idx
];
564 pp_if_state_t
pp_if_state(void)
569 return if_stack
[if_stack_idx
-1];
573 void pp_next_if_state(int i
)
575 switch(pp_if_state())
579 pp_push_if(i
? if_true
: if_false
);
585 pp_push_if(if_ignore
);
588 pp_internal_error(__FILE__
, __LINE__
, "Invalid pp_if_state (%d) in #{if,ifdef,ifndef} directive", (int)pp_if_state());
592 int pp_get_if_depth(void)
597 /* #define WANT_NEAR_INDICATION */
599 static void generic_msg(const char *s
, const char *t
, const char *n
, va_list ap
)
601 fprintf(stderr
, "%s:%d:%d: %s: ", pp_status
.input
? pp_status
.input
: "stdin",
602 pp_status
.line_number
, pp_status
.char_number
, t
);
603 vfprintf(stderr
, s
, ap
);
604 #ifdef WANT_NEAR_INDICATION
610 for (p
= cpy
; *p
; p
++) if(!isprint(*p
)) *p
= ' ';
611 fprintf(stderr
, " near '%s'", cpy
);
616 fprintf(stderr
, "\n");
619 int ppy_error(const char *s
, ...)
623 generic_msg(s
, "Error", ppy_text
, ap
);
629 int ppy_warning(const char *s
, ...)
633 generic_msg(s
, "Warning", ppy_text
, ap
);
638 void pp_internal_error(const char *file
, int line
, const char *s
, ...)
642 fprintf(stderr
, "Internal error (please report) %s %d: ", file
, line
);
643 vfprintf(stderr
, s
, ap
);
644 fprintf(stderr
, "\n");