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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "wine/port.h"
36 #include "wpp_private.h"
38 struct pp_status pp_status
;
42 typedef struct pp_def_state
44 struct pp_def_state
*next
;
45 pp_entry_t
*defines
[HASHKEY
];
48 static pp_def_state_t
*pp_def_state
;
51 static pp_if_state_t if_stack
[MAXIFSTACK
];
52 static int if_stack_idx
= 0;
55 void pp_print_status(void) __attribute__((destructor
));
56 void pp_print_status(void)
63 fprintf(stderr
, "Defines statistics:\n");
64 for(i
= 0; i
< HASHKEY
; i
++)
67 for(ppp
= pp_def_state
->defines
[i
]; ppp
; ppp
= ppp
->next
)
70 if (sum
) fprintf(stderr
, "%4d, %3d\n", i
, sum
);
72 fprintf(stderr
, "Total defines: %d\n", total
);
76 void *pp_xmalloc(size_t size
)
84 fprintf(stderr
, "Virtual memory exhausted.\n");
90 void *pp_xrealloc(void *p
, size_t size
)
95 res
= realloc(p
, size
);
98 fprintf(stderr
, "Virtual memory exhausted.\n");
104 char *pp_xstrdup(const char *str
)
112 return memcpy(s
, str
, len
);
115 /* Don't comment on the hash, its primitive but functional... */
116 static int pphash(const char *str
)
121 return sum
% HASHKEY
;
124 pp_entry_t
*pplookup(const char *ident
)
126 int idx
= pphash(ident
);
129 for(ppp
= pp_def_state
->defines
[idx
]; ppp
; ppp
= ppp
->next
)
131 if(!strcmp(ident
, ppp
->ident
))
137 static void free_pp_entry( pp_entry_t
*ppp
, int idx
)
141 if(ppp
->iep
== pp_includelogiclist
)
143 pp_includelogiclist
= ppp
->iep
->next
;
144 if(pp_includelogiclist
)
145 pp_includelogiclist
->prev
= NULL
;
149 ppp
->iep
->prev
->next
= ppp
->iep
->next
;
151 ppp
->iep
->next
->prev
= ppp
->iep
->prev
;
153 free(ppp
->iep
->filename
);
157 if(pp_def_state
->defines
[idx
] == ppp
)
159 pp_def_state
->defines
[idx
] = ppp
->next
;
160 if(pp_def_state
->defines
[idx
])
161 pp_def_state
->defines
[idx
]->prev
= NULL
;
165 ppp
->prev
->next
= ppp
->next
;
167 ppp
->next
->prev
= ppp
->prev
;
173 /* push a new (empty) define state */
174 void pp_push_define_state(void)
176 pp_def_state_t
*state
= pp_xmalloc( sizeof(*state
) );
178 memset( state
->defines
, 0, sizeof(state
->defines
) );
179 state
->next
= pp_def_state
;
180 pp_def_state
= state
;
183 /* pop the current define state */
184 void pp_pop_define_state(void)
188 pp_def_state_t
*state
;
190 for (i
= 0; i
< HASHKEY
; i
++)
192 while ((ppp
= pp_def_state
->defines
[i
]) != NULL
) free_pp_entry( ppp
, i
);
194 state
= pp_def_state
;
195 pp_def_state
= state
->next
;
199 void pp_del_define(const char *name
)
203 if((ppp
= pplookup(name
)) == NULL
)
205 if(pp_status
.pedantic
)
206 ppwarning("%s was not defined", name
);
210 free_pp_entry( ppp
, pphash(name
) );
213 printf("Deleted (%s, %d) <%s>\n", pp_status
.input
, pp_status
.line_number
, name
);
216 pp_entry_t
*pp_add_define(char *def
, char *text
)
220 int idx
= pphash(def
);
223 if((ppp
= pplookup(def
)) != NULL
)
225 if(pp_status
.pedantic
)
226 ppwarning("Redefinition of %s\n\tPrevious definition: %s:%d", def
, ppp
->filename
, ppp
->linenumber
);
229 ppp
= pp_xmalloc(sizeof(pp_entry_t
));
230 memset( ppp
, 0, sizeof(*ppp
) );
232 ppp
->type
= def_define
;
233 ppp
->subst
.text
= text
;
234 ppp
->filename
= pp_xstrdup(pp_status
.input
? pp_status
.input
: "<internal or cmdline>");
235 ppp
->linenumber
= pp_status
.input
? pp_status
.line_number
: 0;
236 ppp
->next
= pp_def_state
->defines
[idx
];
237 pp_def_state
->defines
[idx
] = ppp
;
239 ppp
->next
->prev
= ppp
;
242 /* Strip trailing white space from subst text */
244 while(len
&& strchr(" \t\r\n", text
[len
-1]))
248 /* Strip leading white space from subst text */
249 for(cptr
= text
; *cptr
&& strchr(" \t\r", *cptr
); cptr
++)
252 memmove(text
, cptr
, strlen(cptr
)+1);
255 printf("Added define (%s, %d) <%s> to <%s>\n", pp_status
.input
, pp_status
.line_number
, ppp
->ident
, text
? text
: "(null)");
260 pp_entry_t
*pp_add_macro(char *id
, marg_t
*args
[], int nargs
, mtext_t
*exp
)
262 int idx
= pphash(id
);
265 if((ppp
= pplookup(id
)) != NULL
)
267 if(pp_status
.pedantic
)
268 ppwarning("Redefinition of %s\n\tPrevious definition: %s:%d", id
, ppp
->filename
, ppp
->linenumber
);
271 ppp
= pp_xmalloc(sizeof(pp_entry_t
));
272 memset( ppp
, 0, sizeof(*ppp
) );
274 ppp
->type
= def_macro
;
277 ppp
->subst
.mtext
= exp
;
278 ppp
->filename
= pp_xstrdup(pp_status
.input
? pp_status
.input
: "<internal or cmdline>");
279 ppp
->linenumber
= pp_status
.input
? pp_status
.line_number
: 0;
280 ppp
->next
= pp_def_state
->defines
[idx
];
281 pp_def_state
->defines
[idx
] = ppp
;
283 ppp
->next
->prev
= ppp
;
287 fprintf(stderr
, "Added macro (%s, %d) <%s(%d)> to <", pp_status
.input
, pp_status
.line_number
, ppp
->ident
, nargs
);
288 for(; exp
; exp
= exp
->next
)
293 fprintf(stderr
, " \"%s\" ", exp
->subst
.text
);
296 fprintf(stderr
, " #(%d) ", exp
->subst
.argidx
);
299 fprintf(stderr
, "##");
302 fprintf(stderr
, " <%d> ", exp
->subst
.argidx
);
306 fprintf(stderr
, ">\n");
313 *-------------------------------------------------------------------------
315 *-------------------------------------------------------------------------
317 #if defined(_Windows) || defined(__MSDOS__)
318 #define INCLUDESEPARATOR ";"
320 #define INCLUDESEPARATOR ":"
323 static char **includepath
;
324 static int nincludepath
= 0;
326 void wpp_add_include_path(const char *path
)
329 char *cpy
= pp_xstrdup(path
);
331 tok
= strtok(cpy
, INCLUDESEPARATOR
);
337 dir
= pp_xstrdup(tok
);
338 for(cptr
= dir
; *cptr
; cptr
++)
340 /* Convert to forward slash */
344 /* Kill eventual trailing '/' */
345 if(*(cptr
= dir
+ strlen(dir
)-1) == '/')
350 includepath
= pp_xrealloc(includepath
, nincludepath
* sizeof(*includepath
));
351 includepath
[nincludepath
-1] = dir
;
353 tok
= strtok(NULL
, INCLUDESEPARATOR
);
358 char *wpp_find_include(const char *name
, int search
)
365 cpy
= pp_xmalloc(strlen(name
)+1);
368 for(ccptr
= name
; *ccptr
; ccptr
++)
370 /* Convert to forward slash */
372 /* kill double backslash */
385 /* Search current dir and then -I path */
386 fd
= open( cpy
, O_RDONLY
);
394 for(i
= 0; i
< nincludepath
; i
++)
397 path
= pp_xmalloc(strlen(includepath
[i
]) + strlen(cpy
) + 2);
398 strcpy(path
, includepath
[i
]);
401 fd
= open( path
, O_RDONLY
);
414 FILE *pp_open_include(const char *name
, int search
, char **newpath
)
419 if (!(path
= wpp_find_include( name
, search
))) return NULL
;
420 fp
= fopen(path
, "rt");
425 printf("Going to include <%s>\n", path
);
426 if (newpath
) *newpath
= path
;
435 *-------------------------------------------------------------------------
436 * #if, #ifdef, #ifndef, #else, #elif and #endif state management
438 * #if state transitions are made on basis of the current TOS and the next
439 * required state. The state transitions are required to housekeep because
440 * #if:s can be nested. The ignore case is activated to prevent output from
441 * within a false clause.
442 * Some special cases come from the fact that the #elif cases are not
443 * binary, but three-state. The problem is that all other elif-cases must
444 * be false when one true one has been found. A second problem is that the
445 * #else clause is a final clause. No extra #else:s may follow.
448 * if_true Process input to output
449 * if_false Process input but no output
450 * if_ignore Process input but no output
451 * if_elif Process input but no output
452 * if_elsefalse Process input but no output
453 * if_elsettrue Process input to output
455 * The possible state-sequences are [state(stack depth)] (rest can be deduced):
456 * TOS #if 1 #else #endif
457 * if_true(n) if_true(n+1) if_elsefalse(n+1)
458 * if_false(n) if_ignore(n+1) if_ignore(n+1)
459 * if_elsetrue(n) if_true(n+1) if_elsefalse(n+1)
460 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1)
461 * if_elif(n) if_ignore(n+1) if_ignore(n+1)
462 * if_ignore(n) if_ignore(n+1) if_ignore(n+1)
464 * TOS #if 1 #elif 0 #else #endif
465 * if_true(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
466 * if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
467 * if_elsetrue(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
468 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
469 * if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
470 * if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
472 * TOS #if 0 #elif 1 #else #endif
473 * if_true(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
474 * if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
475 * if_elsetrue(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
476 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
477 * if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
478 * if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
480 *-------------------------------------------------------------------------
482 static const char * const pp_if_state_str
[] = {
491 void pp_push_if(pp_if_state_t s
)
493 if(if_stack_idx
>= MAXIFSTACK
)
494 pp_internal_error(__FILE__
, __LINE__
, "#if-stack overflow; #{if,ifdef,ifndef} nested too deeply (> %d)", MAXIFSTACK
);
497 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);
499 if_stack
[if_stack_idx
++] = s
;
510 pp_push_ignore_state();
515 pp_if_state_t
pp_pop_if(void)
517 if(if_stack_idx
<= 0)
518 pperror("#{endif,else,elif} without #{if,ifdef,ifndef} (#if-stack underflow)");
520 switch(pp_if_state())
529 pp_pop_ignore_state();
534 fprintf(stderr
, "Pop if %s:%d: %s(%d) -> %s(%d)\n",
536 pp_status
.line_number
,
537 pp_if_state_str
[pp_if_state()],
539 pp_if_state_str
[if_stack
[if_stack_idx
<= 1 ? if_true
: if_stack_idx
-2]],
542 return if_stack
[--if_stack_idx
];
545 pp_if_state_t
pp_if_state(void)
550 return if_stack
[if_stack_idx
-1];
554 void pp_next_if_state(int i
)
556 switch(pp_if_state())
560 pp_push_if(i
? if_true
: if_false
);
566 pp_push_if(if_ignore
);
569 pp_internal_error(__FILE__
, __LINE__
, "Invalid pp_if_state (%d) in #{if,ifdef,ifndef} directive", (int)pp_if_state());
573 int pp_get_if_depth(void)
578 /* #define WANT_NEAR_INDICATION */
580 static void generic_msg(const char *s
, const char *t
, const char *n
, va_list ap
)
582 fprintf(stderr
, "%s:%d:%d: %s: ", pp_status
.input
? pp_status
.input
: "stdin",
583 pp_status
.line_number
, pp_status
.char_number
, t
);
584 vfprintf(stderr
, s
, ap
);
585 #ifdef WANT_NEAR_INDICATION
591 for (p
= cpy
; *p
; p
++) if(!isprint(*p
)) *p
= ' ';
592 fprintf(stderr
, " near '%s'", cpy
);
597 fprintf(stderr
, "\n");
600 int pperror(const char *s
, ...)
604 generic_msg(s
, "Error", pptext
, ap
);
610 int ppwarning(const char *s
, ...)
614 generic_msg(s
, "Warning", pptext
, ap
);
619 void pp_internal_error(const char *file
, int line
, const char *s
, ...)
623 fprintf(stderr
, "Internal error (please report) %s %d: ", file
, line
);
624 vfprintf(stderr
, s
, ap
);
625 fprintf(stderr
, "\n");