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
31 #include "wpp_private.h"
33 struct pp_status pp_status
;
36 static pp_entry_t
*pp_defines
[HASHKEY
];
39 static pp_if_state_t if_stack
[MAXIFSTACK
];
40 static int if_stack_idx
= 0;
43 void pp_status(void) __attribute__((destructor
));
51 fprintf(stderr
, "Defines statistics:\n");
52 for(i
= 0; i
< HASHKEY
; i
++)
55 for(ppp
= pp_defines
[i
]; ppp
; ppp
= ppp
->next
)
58 fprintf(stderr
, "%4d, %3d\n", i
, sum
);
60 fprintf(stderr
, "Total defines: %d\n", total
);
64 void *pp_xmalloc(size_t size
)
72 fprintf(stderr
, "Virtual memory exhausted.\n");
78 void *pp_xrealloc(void *p
, size_t size
)
83 res
= realloc(p
, size
);
86 fprintf(stderr
, "Virtual memory exhausted.\n");
92 char *pp_xstrdup(const char *str
)
97 s
= pp_xmalloc(strlen(str
)+1);
98 return strcpy(s
, str
);
101 /* Don't comment on the hash, its primitive but functional... */
102 static int pphash(const char *str
)
107 return sum
% HASHKEY
;
110 pp_entry_t
*pplookup(const char *ident
)
112 int idx
= pphash(ident
);
115 for(ppp
= pp_defines
[idx
]; ppp
; ppp
= ppp
->next
)
117 if(!strcmp(ident
, ppp
->ident
))
123 void pp_del_define(const char *name
)
128 if((ppp
= pplookup(name
)) == NULL
)
130 if(pp_status
.pedantic
)
131 ppwarning("%s was not defined", name
);
138 fprintf(stderr
, "pp_del_define: %s:%d: includelogic removed, include_ppp='%s', file=%s\n", pp_status
.input
, pp_status
.line_number
, name
, ppp
->iep
->filename
);
139 if(ppp
->iep
== pp_includelogiclist
)
141 pp_includelogiclist
= ppp
->iep
->next
;
142 if(pp_includelogiclist
)
143 pp_includelogiclist
->prev
= NULL
;
147 ppp
->iep
->prev
->next
= ppp
->iep
->next
;
149 ppp
->iep
->next
->prev
= ppp
->iep
->prev
;
151 free(ppp
->iep
->filename
);
156 if(pp_defines
[idx
] == ppp
)
158 pp_defines
[idx
] = ppp
->next
;
160 pp_defines
[idx
]->prev
= NULL
;
164 ppp
->prev
->next
= ppp
->next
;
166 ppp
->next
->prev
= ppp
->prev
;
172 printf("Deleted (%s, %d) <%s>\n", pp_status
.input
, pp_status
.line_number
, name
);
175 pp_entry_t
*pp_add_define(char *def
, char *text
)
179 int idx
= pphash(def
);
182 if((ppp
= pplookup(def
)) != NULL
)
184 if(pp_status
.pedantic
)
185 ppwarning("Redefinition of %s\n\tPrevious definition: %s:%d", def
, ppp
->filename
, ppp
->linenumber
);
188 ppp
= pp_xmalloc(sizeof(pp_entry_t
));
189 memset( ppp
, 0, sizeof(*ppp
) );
191 ppp
->type
= def_define
;
192 ppp
->subst
.text
= text
;
193 ppp
->filename
= pp_status
.input
? pp_xstrdup(pp_status
.input
) : "<internal or cmdline>";
194 ppp
->linenumber
= pp_status
.input
? pp_status
.line_number
: 0;
195 ppp
->next
= pp_defines
[idx
];
196 pp_defines
[idx
] = ppp
;
198 ppp
->next
->prev
= ppp
;
201 /* Strip trailing white space from subst text */
203 while(len
&& strchr(" \t\r\n", text
[len
-1]))
207 /* Strip leading white space from subst text */
208 for(cptr
= text
; *cptr
&& strchr(" \t\r", *cptr
); cptr
++)
211 memmove(text
, cptr
, strlen(cptr
)+1);
214 printf("Added define (%s, %d) <%s> to <%s>\n", pp_status
.input
, pp_status
.line_number
, ppp
->ident
, text
? text
: "(null)");
219 pp_entry_t
*pp_add_macro(char *id
, marg_t
*args
[], int nargs
, mtext_t
*exp
)
221 int idx
= pphash(id
);
224 if((ppp
= pplookup(id
)) != NULL
)
226 if(pp_status
.pedantic
)
227 ppwarning("Redefinition of %s\n\tPrevious definition: %s:%d", id
, ppp
->filename
, ppp
->linenumber
);
230 ppp
= pp_xmalloc(sizeof(pp_entry_t
));
231 memset( ppp
, 0, sizeof(*ppp
) );
233 ppp
->type
= def_macro
;
236 ppp
->subst
.mtext
= exp
;
237 ppp
->filename
= pp_status
.input
? pp_xstrdup(pp_status
.input
) : "<internal or cmdline>";
238 ppp
->linenumber
= pp_status
.input
? pp_status
.line_number
: 0;
239 ppp
->next
= pp_defines
[idx
];
240 pp_defines
[idx
] = ppp
;
242 ppp
->next
->prev
= ppp
;
246 fprintf(stderr
, "Added macro (%s, %d) <%s(%d)> to <", pp_status
.input
, pp_status
.line_number
, ppp
->ident
, nargs
);
247 for(; exp
; exp
= exp
->next
)
252 fprintf(stderr
, " \"%s\" ", exp
->subst
.text
);
255 fprintf(stderr
, " #(%d) ", exp
->subst
.argidx
);
258 fprintf(stderr
, "##");
261 fprintf(stderr
, " <%d> ", exp
->subst
.argidx
);
265 fprintf(stderr
, ">\n");
272 *-------------------------------------------------------------------------
274 *-------------------------------------------------------------------------
276 #if defined(_Windows) || defined(__MSDOS__)
277 #define INCLUDESEPARATOR ";"
279 #define INCLUDESEPARATOR ":"
282 static char **includepath
;
283 static int nincludepath
= 0;
285 void wpp_add_include_path(const char *path
)
288 char *cpy
= pp_xstrdup(path
);
290 tok
= strtok(cpy
, INCLUDESEPARATOR
);
297 dir
= pp_xstrdup(tok
);
298 for(cptr
= dir
; *cptr
; cptr
++)
300 /* Convert to forward slash */
304 /* Kill eventual trailing '/' */
305 if(*(cptr
= dir
+ strlen(dir
)-1) == '/')
310 includepath
= pp_xrealloc(includepath
, nincludepath
* sizeof(*includepath
));
311 includepath
[nincludepath
-1] = dir
;
312 tok
= strtok(NULL
, INCLUDESEPARATOR
);
317 char *wpp_find_include(const char *name
, int search
)
319 char *cpy
= pp_xstrdup(name
);
323 for(cptr
= cpy
; *cptr
; cptr
++)
325 /* kill double backslash */
326 if(*cptr
== '\\' && *(cptr
+1) == '\\')
327 memmove(cptr
, cptr
+1, strlen(cptr
));
328 /* Convert to forward slash */
335 /* Search current dir and then -I path */
336 fd
= open( cpy
, O_RDONLY
);
344 for(i
= 0; i
< nincludepath
; i
++)
347 path
= pp_xmalloc(strlen(includepath
[i
]) + strlen(cpy
) + 2);
348 strcpy(path
, includepath
[i
]);
351 fd
= open( path
, O_RDONLY
);
364 FILE *pp_open_include(const char *name
, int search
, char **newpath
)
369 if (!(path
= wpp_find_include( name
, search
))) return NULL
;
370 fp
= fopen(path
, "rt");
375 printf("Going to include <%s>\n", path
);
376 if (newpath
) *newpath
= path
;
385 *-------------------------------------------------------------------------
386 * #if, #ifdef, #ifndef, #else, #elif and #endif state management
388 * #if state transitions are made on basis of the current TOS and the next
389 * required state. The state transitions are required to housekeep because
390 * #if:s can be nested. The ignore case is activated to prevent output from
391 * within a false clause.
392 * Some special cases come from the fact that the #elif cases are not
393 * binary, but three-state. The problem is that all other elif-cases must
394 * be false when one true one has been found. A second problem is that the
395 * #else clause is a final clause. No extra #else:s may follow.
398 * if_true Process input to output
399 * if_false Process input but no output
400 * if_ignore Process input but no output
401 * if_elif Process input but no output
402 * if_elsefalse Process input but no output
403 * if_elsettrue Process input to output
405 * The possible state-sequences are [state(stack depth)] (rest can be deduced):
406 * TOS #if 1 #else #endif
407 * if_true(n) if_true(n+1) if_elsefalse(n+1)
408 * if_false(n) if_ignore(n+1) if_ignore(n+1)
409 * if_elsetrue(n) if_true(n+1) if_elsefalse(n+1)
410 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1)
411 * if_elif(n) if_ignore(n+1) if_ignore(n+1)
412 * if_ignore(n) if_ignore(n+1) if_ignore(n+1)
414 * TOS #if 1 #elif 0 #else #endif
415 * if_true(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
416 * if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
417 * if_elsetrue(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
418 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
419 * if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
420 * if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
422 * TOS #if 0 #elif 1 #else #endif
423 * if_true(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
424 * if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
425 * if_elsetrue(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
426 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
427 * if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
428 * if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
430 *-------------------------------------------------------------------------
432 static char *pp_if_state_str
[] = {
441 void pp_push_if(pp_if_state_t s
)
443 if(if_stack_idx
>= MAXIFSTACK
)
444 pp_internal_error(__FILE__
, __LINE__
, "#if-stack overflow; #{if,ifdef,ifndef} nested too deeply (> %d)", MAXIFSTACK
);
447 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);
449 if_stack
[if_stack_idx
++] = s
;
460 pp_push_ignore_state();
465 pp_if_state_t
pp_pop_if(void)
467 if(if_stack_idx
<= 0)
468 pperror("#{endif,else,elif} without #{if,ifdef,ifndef} (#if-stack underflow)");
470 switch(pp_if_state())
479 pp_pop_ignore_state();
484 fprintf(stderr
, "Pop if %s:%d: %s(%d) -> %s(%d)\n",
486 pp_status
.line_number
,
487 pp_if_state_str
[pp_if_state()],
489 pp_if_state_str
[if_stack
[if_stack_idx
<= 1 ? if_true
: if_stack_idx
-2]],
492 return if_stack
[--if_stack_idx
];
495 pp_if_state_t
pp_if_state(void)
500 return if_stack
[if_stack_idx
-1];
504 void pp_next_if_state(int i
)
506 switch(pp_if_state())
510 pp_push_if(i
? if_true
: if_false
);
516 pp_push_if(if_ignore
);
519 pp_internal_error(__FILE__
, __LINE__
, "Invalid pp_if_state (%d) in #{if,ifdef,ifndef} directive", (int)pp_if_state());
523 int pp_get_if_depth(void)
528 /* #define WANT_NEAR_INDICATION */
530 static void generic_msg(const char *s
, const char *t
, const char *n
, va_list ap
)
532 fprintf(stderr
, "%s:%d:%d: %s: ", pp_status
.input
? pp_status
.input
: "stdin",
533 pp_status
.line_number
, pp_status
.char_number
, t
);
534 vfprintf(stderr
, s
, ap
);
535 #ifdef WANT_NEAR_INDICATION
541 for (p
= cpy
; *p
; p
++) if(!isprint(*p
)) *p
= ' ';
542 fprintf(stderr
, " near '%s'", cpy
);
547 fprintf(stderr
, "\n");
550 int pperror(const char *s
, ...)
554 generic_msg(s
, "Error", pptext
, ap
);
560 int ppwarning(const char *s
, ...)
564 generic_msg(s
, "Warning", pptext
, ap
);
569 void pp_internal_error(const char *file
, int line
, const char *s
, ...)
573 fprintf(stderr
, "Internal error (please report) %s %d: ", file
, line
);
574 vfprintf(stderr
, s
, ap
);
575 fprintf(stderr
, "\n");