[WPP]
[reactos.git] / reactos / tools / wpp / preproc.c
1 /*
2 * Copyright 1998 Bertho A. Stultiens (BS)
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include "config.h"
20 #include "wine/port.h"
21
22 #include <assert.h>
23 #include <ctype.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #ifdef HAVE_IO_H
33 # include <io.h>
34 #endif
35
36 #include "wine/wpp.h"
37 #include "wpp_private.h"
38
39 struct pp_status pp_status;
40
41 #define HASHKEY 2039
42
43 typedef struct pp_def_state
44 {
45 struct pp_def_state *next;
46 pp_entry_t *defines[HASHKEY];
47 } pp_def_state_t;
48
49 static pp_def_state_t *pp_def_state;
50
51 #define MAXIFSTACK 64
52 static pp_if_state_t if_stack[MAXIFSTACK];
53 static int if_stack_idx = 0;
54
55 #if 0
56 void pp_print_status(void) __attribute__((destructor));
57 void pp_print_status(void)
58 {
59 int i;
60 int sum;
61 int total = 0;
62 pp_entry_t *ppp;
63
64 fprintf(stderr, "Defines statistics:\n");
65 for(i = 0; i < HASHKEY; i++)
66 {
67 sum = 0;
68 for(ppp = pp_def_state->defines[i]; ppp; ppp = ppp->next)
69 sum++;
70 total += sum;
71 if (sum) fprintf(stderr, "%4d, %3d\n", i, sum);
72 }
73 fprintf(stderr, "Total defines: %d\n", total);
74 }
75 #endif
76
77 void *pp_xmalloc(size_t size)
78 {
79 void *res;
80
81 assert(size > 0);
82 res = malloc(size);
83 if(res == NULL)
84 {
85 /* Set the error flag */
86 pp_status.state = 1;
87 }
88 return res;
89 }
90
91 void *pp_xrealloc(void *p, size_t size)
92 {
93 void *res;
94
95 assert(size > 0);
96 res = realloc(p, size);
97 if(res == NULL)
98 {
99 /* Set the error flag */
100 pp_status.state = 1;
101 }
102 return res;
103 }
104
105 char *pp_xstrdup(const char *str)
106 {
107 char *s;
108 int len;
109
110 assert(str != NULL);
111 len = strlen(str)+1;
112 s = pp_xmalloc(len);
113 if(!s)
114 return NULL;
115 return memcpy(s, str, len);
116 }
117
118 static char *wpp_default_lookup(const char *name, const char *parent_name,
119 char **include_path, int include_path_count)
120 {
121 char *cpy;
122 char *cptr;
123 char *path;
124 const char *ccptr;
125 int i, fd;
126
127 cpy = pp_xmalloc(strlen(name)+1);
128 if(!cpy)
129 return NULL;
130 cptr = cpy;
131
132 for(ccptr = name; *ccptr; ccptr++)
133 {
134 /* Convert to forward slash */
135 if(*ccptr == '\\') {
136 /* kill double backslash */
137 if(ccptr[1] == '\\')
138 ccptr++;
139 *cptr = '/';
140 }else {
141 *cptr = *ccptr;
142 }
143 cptr++;
144 }
145 *cptr = '\0';
146
147 if(parent_name)
148 {
149 /* Search directory of parent include and then -I path */
150 const char *p;
151
152 if ((p = strrchr( parent_name, '/' ))) p++;
153 else p = parent_name;
154 path = pp_xmalloc( (p - parent_name) + strlen(cpy) + 1 );
155 if(!path)
156 {
157 free(cpy);
158 return NULL;
159 }
160 memcpy( path, parent_name, p - parent_name );
161 strcpy( path + (p - parent_name), cpy );
162 fd = open( path, O_RDONLY );
163 if (fd != -1)
164 {
165 close( fd );
166 free( cpy );
167 return path;
168 }
169 free( path );
170 }
171 /* Search -I path */
172 for(i = 0; i < include_path_count; i++)
173 {
174 path = pp_xmalloc(strlen(include_path[i]) + strlen(cpy) + 2);
175 if(!path)
176 {
177 free(cpy);
178 return NULL;
179 }
180 strcpy(path, include_path[i]);
181 strcat(path, "/");
182 strcat(path, cpy);
183 fd = open( path, O_RDONLY );
184 if (fd != -1)
185 {
186 close( fd );
187 free( cpy );
188 return path;
189 }
190 free( path );
191 }
192 free( cpy );
193 return NULL;
194 }
195
196 static void *wpp_default_open(const char *filename, int type) {
197 return fopen(filename,"rt");
198 }
199
200 static void wpp_default_close(void *file) {
201 fclose(file);
202 }
203
204 static int wpp_default_read(void *file, char *buffer, unsigned int len){
205 return fread(buffer, 1, len, file);
206 }
207
208 static void wpp_default_write( const char *buffer, unsigned int len ) {
209 fwrite(buffer, 1, len, ppy_out);
210 }
211
212 /* Don't comment on the hash, its primitive but functional... */
213 static int pphash(const char *str)
214 {
215 int sum = 0;
216 while(*str)
217 sum += *str++;
218 return sum % HASHKEY;
219 }
220
221 pp_entry_t *pplookup(const char *ident)
222 {
223 int idx;
224 pp_entry_t *ppp;
225
226 if(!ident)
227 return NULL;
228 idx = pphash(ident);
229 for(ppp = pp_def_state->defines[idx]; ppp; ppp = ppp->next)
230 {
231 if(!strcmp(ident, ppp->ident))
232 return ppp;
233 }
234 return NULL;
235 }
236
237 static void free_pp_entry( pp_entry_t *ppp, int idx )
238 {
239 if(ppp->iep)
240 {
241 if(ppp->iep == pp_includelogiclist)
242 {
243 pp_includelogiclist = ppp->iep->next;
244 if(pp_includelogiclist)
245 pp_includelogiclist->prev = NULL;
246 }
247 else
248 {
249 ppp->iep->prev->next = ppp->iep->next;
250 if(ppp->iep->next)
251 ppp->iep->next->prev = ppp->iep->prev;
252 }
253 free(ppp->iep->filename);
254 free(ppp->iep);
255 }
256
257 if(pp_def_state->defines[idx] == ppp)
258 {
259 pp_def_state->defines[idx] = ppp->next;
260 if(pp_def_state->defines[idx])
261 pp_def_state->defines[idx]->prev = NULL;
262 }
263 else
264 {
265 ppp->prev->next = ppp->next;
266 if(ppp->next)
267 ppp->next->prev = ppp->prev;
268 }
269
270 free(ppp);
271 }
272
273 /* push a new (empty) define state */
274 int pp_push_define_state(void)
275 {
276 pp_def_state_t *state = pp_xmalloc( sizeof(*state) );
277 if(!state)
278 return 1;
279
280 memset( state->defines, 0, sizeof(state->defines) );
281 state->next = pp_def_state;
282 pp_def_state = state;
283 return 0;
284 }
285
286 /* pop the current define state */
287 void pp_pop_define_state(void)
288 {
289 int i;
290 pp_entry_t *ppp;
291 pp_def_state_t *state;
292
293 for (i = 0; i < HASHKEY; i++)
294 {
295 while ((ppp = pp_def_state->defines[i]) != NULL) free_pp_entry( ppp, i );
296 }
297 state = pp_def_state;
298 pp_def_state = state->next;
299 free( state );
300 }
301
302 void pp_del_define(const char *name)
303 {
304 pp_entry_t *ppp;
305
306 if((ppp = pplookup(name)) == NULL)
307 {
308 if(pp_status.pedantic)
309 ppy_warning("%s was not defined", name);
310 return;
311 }
312
313 free( ppp->ident );
314 free( ppp->subst.text );
315 free( ppp->filename );
316 free_pp_entry( ppp, pphash(name) );
317
318 if(pp_status.debug)
319 printf("Deleted (%s, %d) <%s>\n", pp_status.input, pp_status.line_number, name);
320 }
321
322 pp_entry_t *pp_add_define(const char *def, const char *text)
323 {
324 int len;
325 char *cptr;
326 int idx;
327 pp_entry_t *ppp;
328
329 if(!def)
330 return NULL;
331 idx = pphash(def);
332 if((ppp = pplookup(def)) != NULL)
333 {
334 if(pp_status.pedantic)
335 ppy_warning("Redefinition of %s\n\tPrevious definition: %s:%d", def, ppp->filename, ppp->linenumber);
336 pp_del_define(def);
337 }
338 ppp = pp_xmalloc(sizeof(pp_entry_t));
339 if(!ppp)
340 return NULL;
341 memset( ppp, 0, sizeof(*ppp) );
342 ppp->ident = pp_xstrdup(def);
343 if(!ppp->ident)
344 goto error;
345 ppp->type = def_define;
346 ppp->subst.text = text ? pp_xstrdup(text) : NULL;
347 if(text && !ppp->subst.text)
348 goto error;
349 ppp->filename = pp_xstrdup(pp_status.input ? pp_status.input : "<internal or cmdline>");
350 if(!ppp->filename)
351 goto error;
352 ppp->linenumber = pp_status.input ? pp_status.line_number : 0;
353 ppp->next = pp_def_state->defines[idx];
354 pp_def_state->defines[idx] = ppp;
355 if(ppp->next)
356 ppp->next->prev = ppp;
357 if(ppp->subst.text)
358 {
359 /* Strip trailing white space from subst text */
360 len = strlen(ppp->subst.text);
361 while(len && strchr(" \t\r\n", ppp->subst.text[len-1]))
362 {
363 ppp->subst.text[--len] = '\0';
364 }
365 /* Strip leading white space from subst text */
366 for(cptr = ppp->subst.text; *cptr && strchr(" \t\r", *cptr); cptr++)
367 ;
368 if(ppp->subst.text != cptr)
369 memmove(ppp->subst.text, cptr, strlen(cptr)+1);
370 }
371 if(pp_status.debug)
372 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)");
373
374 return ppp;
375
376 error:
377 free(ppp->ident);
378 free(ppp->subst.text);
379 free(ppp);
380 return NULL;
381 }
382
383 pp_entry_t *pp_add_macro(char *id, marg_t *args[], int nargs, mtext_t *exp)
384 {
385 int idx;
386 pp_entry_t *ppp;
387
388 if(!id)
389 return NULL;
390 idx = pphash(id);
391 if((ppp = pplookup(id)) != NULL)
392 {
393 if(pp_status.pedantic)
394 ppy_warning("Redefinition of %s\n\tPrevious definition: %s:%d", id, ppp->filename, ppp->linenumber);
395 pp_del_define(id);
396 }
397 ppp = pp_xmalloc(sizeof(pp_entry_t));
398 if(!ppp)
399 return NULL;
400 memset( ppp, 0, sizeof(*ppp) );
401 ppp->ident = id;
402 ppp->type = def_macro;
403 ppp->margs = args;
404 ppp->nargs = nargs;
405 ppp->subst.mtext= exp;
406 ppp->filename = pp_xstrdup(pp_status.input ? pp_status.input : "<internal or cmdline>");
407 if(!ppp->filename)
408 {
409 free(ppp);
410 return NULL;
411 }
412 ppp->linenumber = pp_status.input ? pp_status.line_number : 0;
413 ppp->next = pp_def_state->defines[idx];
414 pp_def_state->defines[idx] = ppp;
415 if(ppp->next)
416 ppp->next->prev = ppp;
417
418 if(pp_status.debug)
419 {
420 fprintf(stderr, "Added macro (%s, %d) <%s(%d)> to <", pp_status.input, pp_status.line_number, ppp->ident, nargs);
421 for(; exp; exp = exp->next)
422 {
423 switch(exp->type)
424 {
425 case exp_text:
426 fprintf(stderr, " \"%s\" ", exp->subst.text);
427 break;
428 case exp_stringize:
429 fprintf(stderr, " #(%d) ", exp->subst.argidx);
430 break;
431 case exp_concat:
432 fprintf(stderr, "##");
433 break;
434 case exp_subst:
435 fprintf(stderr, " <%d> ", exp->subst.argidx);
436 break;
437 }
438 }
439 fprintf(stderr, ">\n");
440 }
441 return ppp;
442 }
443
444
445 /*
446 *-------------------------------------------------------------------------
447 * Include management
448 *-------------------------------------------------------------------------
449 */
450 #if defined(_WIN32) || defined(__MSDOS__)
451 #define INCLUDESEPARATOR ";"
452 #else
453 #define INCLUDESEPARATOR ":"
454 #endif
455
456 static char **includepath;
457 static int nincludepath = 0;
458
459 int wpp_add_include_path(const char *path)
460 {
461 char *tok;
462 char *cpy = pp_xstrdup(path);
463 if(!cpy)
464 return 1;
465
466 tok = strtok(cpy, INCLUDESEPARATOR);
467 while(tok)
468 {
469 if(*tok) {
470 char *dir;
471 char *cptr;
472 char **new_path;
473
474 dir = pp_xstrdup(tok);
475 if(!dir)
476 {
477 free(cpy);
478 return 1;
479 }
480 for(cptr = dir; *cptr; cptr++)
481 {
482 /* Convert to forward slash */
483 if(*cptr == '\\')
484 *cptr = '/';
485 }
486 /* Kill eventual trailing '/' */
487 if(*(cptr = dir + strlen(dir)-1) == '/')
488 *cptr = '\0';
489
490 /* Add to list */
491 new_path = pp_xrealloc(includepath, (nincludepath+1) * sizeof(*includepath));
492 if(!new_path)
493 {
494 free(dir);
495 free(cpy);
496 return 1;
497 }
498 includepath = new_path;
499 includepath[nincludepath] = dir;
500 nincludepath++;
501 }
502 tok = strtok(NULL, INCLUDESEPARATOR);
503 }
504 free(cpy);
505 return 0;
506 }
507
508 char *wpp_find_include(const char *name, const char *parent_name)
509 {
510 return wpp_default_lookup(name, parent_name, includepath, nincludepath);
511 }
512
513 void *pp_open_include(const char *name, const char *parent_name, char **newpath)
514 {
515 char *path;
516 void *fp;
517
518 if (!(path = wpp_callbacks->lookup(name, parent_name, includepath,
519 nincludepath))) return NULL;
520 fp = wpp_callbacks->open(path, parent_name == NULL ? 1 : 0);
521
522 if (fp)
523 {
524 if (pp_status.debug)
525 printf("Going to include <%s>\n", path);
526 if (newpath) *newpath = path;
527 else free( path );
528 return fp;
529 }
530 free( path );
531 return NULL;
532 }
533
534 /*
535 *-------------------------------------------------------------------------
536 * #if, #ifdef, #ifndef, #else, #elif and #endif state management
537 *
538 * #if state transitions are made on basis of the current TOS and the next
539 * required state. The state transitions are required to housekeep because
540 * #if:s can be nested. The ignore case is activated to prevent output from
541 * within a false clause.
542 * Some special cases come from the fact that the #elif cases are not
543 * binary, but three-state. The problem is that all other elif-cases must
544 * be false when one true one has been found. A second problem is that the
545 * #else clause is a final clause. No extra #else:s may follow.
546 *
547 * The states mean:
548 * if_true Process input to output
549 * if_false Process input but no output
550 * if_ignore Process input but no output
551 * if_elif Process input but no output
552 * if_elsefalse Process input but no output
553 * if_elsettrue Process input to output
554 *
555 * The possible state-sequences are [state(stack depth)] (rest can be deduced):
556 * TOS #if 1 #else #endif
557 * if_true(n) if_true(n+1) if_elsefalse(n+1)
558 * if_false(n) if_ignore(n+1) if_ignore(n+1)
559 * if_elsetrue(n) if_true(n+1) if_elsefalse(n+1)
560 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1)
561 * if_elif(n) if_ignore(n+1) if_ignore(n+1)
562 * if_ignore(n) if_ignore(n+1) if_ignore(n+1)
563 *
564 * TOS #if 1 #elif 0 #else #endif
565 * if_true(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
566 * if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
567 * if_elsetrue(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
568 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
569 * if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
570 * if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
571 *
572 * TOS #if 0 #elif 1 #else #endif
573 * if_true(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
574 * if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
575 * if_elsetrue(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
576 * if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
577 * if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
578 * if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
579 *
580 *-------------------------------------------------------------------------
581 */
582 static const char * const pp_if_state_str[] = {
583 "if_false",
584 "if_true",
585 "if_elif",
586 "if_elsefalse",
587 "if_elsetrue",
588 "if_ignore"
589 };
590
591 void pp_push_if(pp_if_state_t s)
592 {
593 if(if_stack_idx >= MAXIFSTACK)
594 pp_internal_error(__FILE__, __LINE__, "#if-stack overflow; #{if,ifdef,ifndef} nested too deeply (> %d)", MAXIFSTACK);
595
596 if(pp_flex_debug)
597 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);
598
599 if_stack[if_stack_idx++] = s;
600
601 switch(s)
602 {
603 case if_true:
604 case if_elsetrue:
605 break;
606 case if_false:
607 case if_elsefalse:
608 case if_elif:
609 case if_ignore:
610 pp_push_ignore_state();
611 break;
612 default:
613 pp_internal_error(__FILE__, __LINE__, "Invalid pp_if_state (%d)", (int)pp_if_state());
614 }
615 }
616
617 pp_if_state_t pp_pop_if(void)
618 {
619 if(if_stack_idx <= 0)
620 {
621 ppy_error("#{endif,else,elif} without #{if,ifdef,ifndef} (#if-stack underflow)");
622 return if_error;
623 }
624
625 switch(pp_if_state())
626 {
627 case if_true:
628 case if_elsetrue:
629 break;
630 case if_false:
631 case if_elsefalse:
632 case if_elif:
633 case if_ignore:
634 pp_pop_ignore_state();
635 break;
636 default:
637 pp_internal_error(__FILE__, __LINE__, "Invalid pp_if_state (%d)", (int)pp_if_state());
638 }
639
640 if(pp_flex_debug)
641 fprintf(stderr, "Pop if %s:%d: %s(%d) -> %s(%d)\n",
642 pp_status.input,
643 pp_status.line_number,
644 pp_if_state_str[pp_if_state()],
645 if_stack_idx,
646 pp_if_state_str[if_stack[if_stack_idx <= 1 ? if_true : if_stack_idx-2]],
647 if_stack_idx-1);
648
649 return if_stack[--if_stack_idx];
650 }
651
652 pp_if_state_t pp_if_state(void)
653 {
654 if(!if_stack_idx)
655 return if_true;
656 else
657 return if_stack[if_stack_idx-1];
658 }
659
660
661 void pp_next_if_state(int i)
662 {
663 switch(pp_if_state())
664 {
665 case if_true:
666 case if_elsetrue:
667 pp_push_if(i ? if_true : if_false);
668 break;
669 case if_false:
670 case if_elsefalse:
671 case if_elif:
672 case if_ignore:
673 pp_push_if(if_ignore);
674 break;
675 default:
676 pp_internal_error(__FILE__, __LINE__, "Invalid pp_if_state (%d) in #{if,ifdef,ifndef} directive", (int)pp_if_state());
677 }
678 }
679
680 int pp_get_if_depth(void)
681 {
682 return if_stack_idx;
683 }
684
685 /* #define WANT_NEAR_INDICATION */
686
687 static void generic_msg(const char *s, const char *t, const char *n, va_list ap)
688 {
689 fprintf(stderr, "%s:%d:%d: %s: ", pp_status.input ? pp_status.input : "stdin",
690 pp_status.line_number, pp_status.char_number, t);
691 vfprintf(stderr, s, ap);
692 #ifdef WANT_NEAR_INDICATION
693 {
694 char *cpy, *p;
695 if(n)
696 {
697 cpy = pp_xstrdup(n);
698 if(!cpy)
699 goto end;
700 for (p = cpy; *p; p++) if(!isprint(*p)) *p = ' ';
701 fprintf(stderr, " near '%s'", cpy);
702 free(cpy);
703 }
704 }
705 end:
706 #endif
707 fprintf(stderr, "\n");
708 }
709
710 static void wpp_default_error(const char *file, int line, int col, const char *near, const char *msg, va_list ap)
711 {
712 generic_msg(msg, "Error", near, ap);
713 exit(1);
714 }
715
716 static void wpp_default_warning(const char *file, int line, int col, const char *near, const char *msg, va_list ap)
717 {
718 generic_msg(msg, "Warning", near, ap);
719 }
720
721 static const struct wpp_callbacks default_callbacks =
722 {
723 wpp_default_lookup,
724 wpp_default_open,
725 wpp_default_close,
726 wpp_default_read,
727 wpp_default_write,
728 wpp_default_error,
729 wpp_default_warning,
730 };
731
732 const struct wpp_callbacks *wpp_callbacks = &default_callbacks;
733
734 int ppy_error(const char *s, ...)
735 {
736 va_list ap;
737 va_start(ap, s);
738 wpp_callbacks->error(pp_status.input, pp_status.line_number, pp_status.char_number, ppy_text, s, ap);
739 va_end(ap);
740 pp_status.state = 1;
741 return 1;
742 }
743
744 int ppy_warning(const char *s, ...)
745 {
746 va_list ap;
747 va_start(ap, s);
748 wpp_callbacks->warning(pp_status.input, pp_status.line_number, pp_status.char_number, ppy_text, s, ap);
749 va_end(ap);
750 return 0;
751 }
752
753 void pp_internal_error(const char *file, int line, const char *s, ...)
754 {
755 va_list ap;
756 va_start(ap, s);
757 fprintf(stderr, "Internal error (please report) %s %d: ", file, line);
758 vfprintf(stderr, s, ap);
759 fprintf(stderr, "\n");
760 va_end(ap);
761 exit(3);
762 }