dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libpp / common / ppop.c
blobbb498b42d5eaad0cec78754fb1100177f8782227
1 /***********************************************************************
2 * *
3 * This software is part of the ast package *
4 * Copyright (c) 1986-2009 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
8 * *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12 * *
13 * Information and Software Systems Research *
14 * AT&T Research *
15 * Florham Park NJ *
16 * *
17 * Glenn Fowler <gsf@research.att.com> *
18 * *
19 ***********************************************************************/
20 #pragma prototyped
22 * Glenn Fowler
23 * AT&T Research
25 * preprocessor library control interface
28 #include "pplib.h"
29 #include "pptab.h"
31 #include <ls.h>
33 #define REFONE (pp.truncate?(Hash_table_t*)0:pp.symtab)
34 #define REFALL (pp.truncate?pp.dirtab:pp.symtab)
36 #define ppiskey(t,v,p) (p=t,v>=p->value&&value<=(p+elementsof(t)-2)->value)
39 * set option value
40 * initialization files have lowest precedence
43 static void
44 set(register long* p, register long op, int val)
46 long* r;
48 r = p == &pp.state ? &pp.ro_state : p == &pp.mode ? &pp.ro_mode : &pp.ro_option;
49 if (!(pp.mode & INIT) || !(pp.in->type == IN_FILE) || !(*r & op))
51 if (!pp.initialized && !(pp.mode & INIT))
52 *r |= op;
53 if (val)
54 *p |= op;
55 else
56 *p &= ~op;
58 debug((-7, "set(%s)=%s", p == &pp.state ? "state" : p == &pp.mode ? "mode" : "option", p == &pp.state ? ppstatestr(*p) : p == &pp.mode ? ppmodestr(*p) : ppoptionstr(*p)));
62 * initialize hash table with keywords from key
65 static void
66 inithash(register Hash_table_t* tab, register struct ppkeyword* key)
68 register char* s;
70 for (; s = key->name; key++)
72 if (!ppisid(*s))
73 s++;
74 hashput(tab, s, key->value);
79 * return ppkeyword table name given value
82 char*
83 ppkeyname(register int value, int dir)
85 register char* s;
86 register struct ppkeyword* p;
88 if (dir && ppiskey(directives, value, p) || !dir && (ppiskey(options, value, p) || ppiskey(predicates, value, p) || ppiskey(variables, value, p)))
90 s = (p + (value - p->value))->name;
91 return s + !ppisid(*s);
93 #if DEBUG
94 error(PANIC, "no keyword table name for value=%d", value);
95 #endif
96 return "UNKNOWN";
100 * add to the include maps
103 void
104 ppmapinclude(char* file, register char* s)
106 register int c;
107 register struct ppdirs* dp;
108 int fd;
109 int flags;
110 int index;
111 int token;
112 char* t;
113 char* old_file;
114 long old_state;
115 struct ppfile* fp;
116 struct ppfile* mp;
118 old_file = error_info.file;
119 old_state = pp.state;
120 if (s)
121 PUSH_BUFFER("mapinclude", s, 1);
122 else if (file)
124 if (*file == '-')
126 if (!error_info.file)
128 error(1, "%s: input file name required for %s ignore", file, dirname(INCLUDE));
129 return;
131 s = t = strcopy(pp.tmpbuf, error_info.file);
132 c = *++file;
133 for (;;)
135 if (s <= pp.tmpbuf || *s == '/')
137 s = t;
138 break;
140 else if (*s == c)
141 break;
142 s--;
144 strcpy(s, file);
145 file = pp.tmpbuf;
147 if ((fd = ppsearch(file, INC_LOCAL, SEARCH_INCLUDE)) < 0)
148 return;
149 PUSH_FILE(file, fd);
151 else
152 return;
153 #if CATSTRINGS
154 pp.state |= (COMPILE|FILEPOP|HEADER|JOINING|STRIP|NOSPACE|PASSEOF);
155 #else
156 pp.state |= (COMPILE|FILEPOP|HEADER|STRIP|NOSPACE|PASSEOF);
157 #endif
158 pp.level++;
159 flags = INC_MAPALL;
160 fp = mp = 0;
161 for (;;)
163 switch (token = pplex())
165 case 0:
166 case T_STRING:
167 case T_HEADER:
168 if (fp)
170 fp->guard = INC_IGNORE;
171 for (dp = pp.firstdir->next; dp; dp = dp->next)
172 if (dp->name && (c = strlen(dp->name)) && !strncmp(dp->name, fp->name, c) && fp->name[c] == '/')
174 ppsetfile(fp->name + c + 1)->guard = INC_IGNORE;
175 break;
178 if (!token)
179 break;
180 pathcanon(pp.token, 0);
181 fp = ppsetfile(pp.token);
182 if (mp)
184 mp->flags |= flags;
185 if (streq(fp->name, "."))
186 mp->flags |= INC_MAPNOLOCAL;
187 else
188 mp->bound[index] = fp;
190 fp = mp = 0;
192 else
193 index = token == T_HEADER ? INC_STANDARD : INC_LOCAL;
194 continue;
195 case '=':
196 if (!(mp = fp))
197 error(3, "%s: \"name\" = \"binding\" expected");
198 fp = 0;
199 continue;
200 case '\n':
201 continue;
202 case T_ID:
203 if (streq(pp.token, "all"))
205 flags = INC_MAPALL;
206 continue;
208 else if (streq(pp.token, "hosted"))
210 flags = INC_MAPHOSTED;
211 continue;
213 else if (streq(pp.token, "nohosted"))
215 flags = INC_MAPNOHOSTED;
216 continue;
218 /*FALLTHROUGH*/
219 default:
220 error(3, "%s unexpected in %s map list", pptokstr(pp.token, 0), dirname(INCLUDE));
221 break;
223 break;
225 pp.level--;
226 error_info.file = old_file;
227 pp.state = old_state;
231 * return non-0 if file is identical to fd
234 static int
235 identical(char* file, int fd)
237 struct stat a;
238 struct stat b;
240 return !stat(file, &a) && !fstat(fd, &b) && a.st_dev == b.st_dev && a.st_ino == b.st_ino;
244 * compare up to pp.truncate chars
246 * NOTE: __STD* and symbols containing ' ' are not truncated
249 static int
250 trunccomp(register char* a, register char* b)
252 return !strchr(b, ' ') && !strneq(b, "__STD", 5) ? strncmp(a, b, pp.truncate) : strcmp(a, b);
256 * hash up to pp.truncate chars
258 * NOTE: __STD* and symbols containing ' ' are not truncated
261 static unsigned int
262 trunchash(char* a)
264 int n;
266 return memhash(a, (n = strlen(a)) > pp.truncate && !strchr(a, ' ') && !strneq(a, "__STD", 5) ? pp.truncate : n);
269 #if DEBUG & TRACE_debug
271 * append context to debug trace
274 static int
275 context(Sfio_t* sp, int level, int flags)
277 static int state;
279 NoP(level);
280 NoP(flags);
281 if (error_info.trace <= -10 && pp.state != state)
283 state = pp.state;
284 sfprintf(sp, " %s", ppstatestr(pp.state));
286 return 1;
288 #endif
291 * reset include guard
294 static int
295 unguard(const char* name, char* v, void* handle)
297 register struct ppfile* fp = (struct ppfile*)v;
299 fp->guard = 0;
300 return 0;
304 * reset macro definition
307 static void
308 undefine(void* p)
310 struct ppmacro* mac = ((struct ppsymbol*)p)->macro;
312 if (mac)
314 free(mac->formals);
315 free(mac->value);
316 free(mac);
321 * pp operations
323 * NOTE: PP_INIT must be done before the first pplex() call
324 * PP_DONE must be done after the last pplex() call
325 * PP_INIT-PP_DONE must be done for each new PP_INPUT
328 void
329 ppop(int op, ...)
331 va_list ap;
332 register char* p;
333 register struct ppkeyword* kp;
334 register char* s;
335 int c;
336 long n;
337 char* t;
338 struct ppdirs* dp;
339 struct ppdirs* hp;
340 struct ppsymkey* key;
341 struct oplist* xp;
342 Sfio_t* sp;
343 struct stat st;
344 PPCOMMENT ppcomment;
345 PPLINESYNC pplinesync;
347 static int initialized;
349 va_start(ap, op);
350 switch (op)
352 case PP_ASSERT:
353 case PP_DEFINE:
354 case PP_DIRECTIVE:
355 case PP_OPTION:
356 case PP_READ:
357 case PP_UNDEF:
358 if (pp.initialized)
359 goto before;
360 if ((p = va_arg(ap, char*)) && *p)
362 if (pp.lastop)
363 pp.lastop = (pp.lastop->next = newof(0, struct oplist, 1, 0));
364 else
365 pp.firstop = pp.lastop = newof(0, struct oplist, 1, 0);
366 pp.lastop->op = op;
367 pp.lastop->value = p;
369 break;
370 case PP_BUILTIN:
371 pp.builtin = va_arg(ap, PPBUILTIN);
372 break;
373 case PP_CDIR:
374 p = va_arg(ap, char*);
375 c = va_arg(ap, int);
376 pp.cdir.path = 0;
377 if (!p)
378 pp.c = c;
379 else if (streq(p, "-"))
381 pp.c = c;
382 for (dp = pp.firstdir; dp; dp = dp->next)
383 dp->c = c;
385 else if (!pp.c)
387 if (!*p || stat((pathcanon(p, 0), p), &st))
388 pp.c = c;
389 else
391 for (dp = pp.firstdir; dp; dp = dp->next)
393 if (!pp.c && (dp->c || dp->name && SAMEID(&dp->id, &st)))
394 pp.c = 1;
395 dp->c = pp.c == 1;
397 if (!pp.c)
399 pp.cdir.path = p;
400 SAVEID(&pp.cdir.id, &st);
404 break;
405 case PP_CHOP:
406 if (p = va_arg(ap, char*))
408 c = strlen(p);
409 xp = newof(0, struct oplist, 1, c + 1);
410 xp->value = ((char*)xp) + sizeof(struct oplist);
411 s = xp->value;
412 c = *p++;
413 while (*p && *p != c)
414 *s++ = *p++;
415 *s++ = '/';
416 xp->op = s - xp->value;
417 *s++ = 0;
418 if (*p && *++p && *p != c)
420 while (*p && *p != c)
421 *s++ = *p++;
422 *s++ = '/';
424 *s = 0;
425 xp->next = pp.chop;
426 pp.chop = xp;
428 break;
429 case PP_COMMENT:
430 if (pp.comment = va_arg(ap, PPCOMMENT))
431 pp.flags |= PP_comment;
432 else
433 pp.flags &= ~PP_comment;
434 break;
435 case PP_COMPATIBILITY:
436 set(&pp.state, COMPATIBILITY, va_arg(ap, int));
437 #if COMPATIBLE
438 if (pp.initialized)
439 ppfsm(FSM_COMPATIBILITY, NiL);
440 #else
441 if (pp.state & COMPATIBILITY)
442 error(3, "preprocessor not compiled with compatibility dialect enabled [COMPATIBLE]");
443 #endif
444 if (pp.state & COMPATIBILITY)
445 pp.flags |= PP_compatibility;
446 else
447 pp.flags &= ~PP_compatibility;
448 break;
449 case PP_COMPILE:
450 if (pp.initialized)
451 goto before;
452 pp.state |= COMPILE;
453 if (!pp.symtab)
454 pp.symtab = hashalloc(NiL, HASH_name, "symbols", 0);
455 if (kp = va_arg(ap, struct ppkeyword*))
456 for (; s = kp->name; kp++)
458 n = SYM_LEX;
459 switch (*s)
461 case '-':
462 s++;
463 break;
464 case '+':
465 s++;
466 if (!(pp.option & PLUSPLUS))
467 break;
468 /*FALLTHROUGH*/
469 default:
470 n |= SYM_KEYWORD;
471 break;
473 if (key = ppkeyset(pp.symtab, s))
475 key->sym.flags = n;
476 key->lex = kp->value;
479 break;
480 case PP_DEBUG:
481 error_info.trace = va_arg(ap, int);
482 break;
483 case PP_DEFAULT:
484 if (p = va_arg(ap, char*))
485 p = strdup(p);
486 free(pp.ppdefault);
487 pp.ppdefault = p;
488 break;
489 case PP_DONE:
490 #if CHECKPOINT
491 if (pp.mode & DUMP)
492 ppdump();
493 #endif
494 if (pp.mode & FILEDEPS)
496 sfputc(pp.filedeps.sp, '\n');
497 if (pp.filedeps.sp == sfstdout)
498 sfsync(pp.filedeps.sp);
499 else
500 sfclose(pp.filedeps.sp);
502 if (pp.state & STANDALONE)
504 if ((pp.state & (NOTEXT|HIDDEN)) == HIDDEN && pplastout() != '\n')
505 ppputchar('\n');
506 ppflushout();
508 error_info.file = 0;
509 break;
510 case PP_DUMP:
511 set(&pp.mode, DUMP, va_arg(ap, int));
512 #if !CHECKPOINT
513 if (pp.mode & DUMP)
514 error(3, "preprocessor not compiled with checkpoint enabled [CHECKPOINT]");
515 #endif
516 break;
517 case PP_FILEDEPS:
518 if (n = va_arg(ap, int))
519 pp.filedeps.flags |= n;
520 else
521 pp.filedeps.flags = 0;
522 break;
523 case PP_FILENAME:
524 error_info.file = va_arg(ap, char*);
525 break;
526 case PP_HOSTDIR:
527 if (!(pp.mode & INIT))
528 pp.ro_mode |= HOSTED;
529 else if (pp.ro_mode & HOSTED)
530 break;
531 pp.ro_mode |= INIT;
532 p = va_arg(ap, char*);
533 c = va_arg(ap, int);
534 pp.hostdir.path = 0;
535 if (!p)
536 pp.hosted = c;
537 else if (streq(p, "-"))
539 if (pp.initialized)
540 set(&pp.mode, HOSTED, c);
541 else
543 pp.hosted = c ? 1 : 2;
544 for (dp = pp.firstdir; dp; dp = dp->next)
545 if (pp.hosted == 1)
546 dp->type |= TYPE_HOSTED;
547 else
548 dp->type &= ~TYPE_HOSTED;
551 else if (!pp.hosted)
553 if (!*p || stat((pathcanon(p, 0), p), &st))
554 pp.hosted = 1;
555 else
557 for (dp = pp.firstdir; dp; dp = dp->next)
559 if (!pp.hosted && ((dp->type & TYPE_HOSTED) || dp->name && SAMEID(&dp->id, &st)))
560 pp.hosted = 1;
561 if (pp.hosted == 1)
562 dp->type |= TYPE_HOSTED;
563 else
564 dp->type &= ~TYPE_HOSTED;
566 if (!pp.hosted)
568 pp.hostdir.path = p;
569 SAVEID(&pp.hostdir.id, &st);
573 break;
574 case PP_ID:
575 p = va_arg(ap, char*);
576 c = va_arg(ap, int);
577 if (p)
578 ppfsm(c ? FSM_IDADD : FSM_IDDEL, p);
579 break;
580 case PP_IGNORE:
581 if (p = va_arg(ap, char*))
583 pathcanon(p, 0);
584 ppsetfile(p)->guard = INC_IGNORE;
585 message((-3, "%s: ignore", p));
587 break;
588 case PP_IGNORELIST:
589 if (pp.initialized)
590 goto before;
591 pp.ignore = va_arg(ap, char*);
592 break;
593 case PP_INCLUDE:
594 if ((p = va_arg(ap, char*)) && *p)
596 pathcanon(p, 0);
597 if (stat(p, &st))
598 break;
599 for (dp = pp.stddirs; dp = dp->next;)
600 if (dp->name && SAMEID(&dp->id, &st))
601 break;
602 if (pp.cdir.path && SAMEID(&pp.cdir.id, &st))
604 pp.cdir.path = 0;
605 pp.c = 1;
607 if (pp.hostdir.path && SAMEID(&pp.hostdir.id, &st))
609 pp.hostdir.path = 0;
610 pp.hosted = 1;
612 if ((pp.mode & INIT) && !(pp.ro_mode & INIT))
613 pp.hosted = 1;
614 c = dp && dp->c || pp.c == 1;
615 n = dp && (dp->type & TYPE_HOSTED) || pp.hosted == 1;
616 if (!dp || dp == pp.lastdir->next)
618 if (dp)
620 c = dp->c;
621 n = dp->type & TYPE_HOSTED;
623 dp = newof(0, struct ppdirs, 1, 0);
624 dp->name = p;
625 SAVEID(&dp->id, &st);
626 dp->type |= TYPE_INCLUDE;
627 dp->index = INC_LOCAL + pp.ignoresrc != 0;
628 dp->next = pp.lastdir->next;
629 pp.lastdir = pp.lastdir->next = dp;
631 dp->c = c;
632 if (n)
633 dp->type |= TYPE_HOSTED;
634 else
635 dp->type &= ~TYPE_HOSTED;
637 break;
638 case PP_INCREF:
639 pp.incref = va_arg(ap, PPINCREF);
640 break;
641 case PP_RESET:
642 pp.reset.on = 1;
643 break;
644 case PP_INIT:
645 if (pp.initialized)
647 error_info.errors = 0;
648 error_info.warnings = 0;
650 else
653 * context initialization
656 if (!initialized)
659 * out of malloc is fatal
662 memfatal();
665 * initialize the error message interface
668 error_info.version = (char*)pp.version;
669 #if DEBUG & TRACE_debug
670 error_info.auxilliary = context;
671 pptrace(0);
672 #endif
675 * initialize pplex tables
678 ppfsm(FSM_INIT, NiL);
681 * fixed macro stack size -- room for improvement
684 pp.macp = newof(0, struct ppmacstk, DEFMACSTACK, 0);
685 pp.macp->next = pp.macp + 1;
686 pp.maxmac = (char*)pp.macp + DEFMACSTACK;
687 initialized = 1;
690 * initial include/if control stack
693 pp.control = newof(0, long, pp.constack, 0);
694 pp.maxcon = pp.control + pp.constack - 1;
698 * validate modes
701 switch (pp.arg_mode)
703 case 'a':
704 case 'C':
705 ppop(PP_COMPATIBILITY, 0);
706 ppop(PP_TRANSITION, 1);
707 break;
708 case 'A':
709 case 'c':
710 ppop(PP_COMPATIBILITY, 0);
711 ppop(PP_STRICT, 1);
712 break;
713 case 'f':
714 ppop(PP_COMPATIBILITY, 1);
715 ppop(PP_PLUSPLUS, 1);
716 ppop(PP_TRANSITION, 1);
717 break;
718 case 'F':
719 ppop(PP_COMPATIBILITY, 0);
720 ppop(PP_PLUSPLUS, 1);
721 break;
722 case 'k':
723 case 's':
724 ppop(PP_COMPATIBILITY, 1);
725 ppop(PP_STRICT, 1);
726 break;
727 case 'o':
728 case 'O':
729 ppop(PP_COMPATIBILITY, 1);
730 ppop(PP_TRANSITION, 0);
731 break;
732 case 't':
733 ppop(PP_COMPATIBILITY, 1);
734 ppop(PP_TRANSITION, 1);
735 break;
737 if (!(pp.state & WARN) && !(pp.arg_style & STYLE_gnu))
738 ppop(PP_PEDANTIC, 1);
739 if (pp.state & PASSTHROUGH)
741 if (pp.state & COMPILE)
743 pp.state &= ~PASSTHROUGH;
744 error(1, "passthrough ignored for compile");
746 else
748 ppop(PP_COMPATIBILITY, 1);
749 ppop(PP_HOSTDIR, "-", 1);
750 ppop(PP_SPACEOUT, 1);
751 set(&pp.state, DISABLE, va_arg(ap, int));
756 * create the hash tables
759 if (!pp.symtab)
760 pp.symtab = hashalloc(NiL, HASH_name, "symbols", 0);
761 if (!pp.dirtab)
763 pp.dirtab = hashalloc(REFONE, HASH_name, "directives", 0);
764 inithash(pp.dirtab, directives);
766 if (!pp.filtab)
767 pp.filtab = hashalloc(REFALL, HASH_name, "files", 0);
768 if (!pp.prdtab)
769 pp.prdtab = hashalloc(REFALL, HASH_name, "predicates", 0);
770 if (!pp.strtab)
772 pp.strtab = hashalloc(REFALL, HASH_name, "strings", 0);
773 inithash(pp.strtab, options);
774 inithash(pp.strtab, predicates);
775 inithash(pp.strtab, variables);
777 pp.optflags[X_PROTOTYPED] = OPT_GLOBAL;
778 pp.optflags[X_SYSTEM_HEADER] = OPT_GLOBAL|OPT_PASS;
781 * mark macros that are builtin predicates
784 for (kp = predicates; s = kp->name; kp++)
786 if (!ppisid(*s))
787 s++;
788 ppassert(DEFINE, s, 0);
792 * the remaining entry names must be allocated
795 hashset(pp.dirtab, HASH_ALLOCATE);
796 hashset(pp.filtab, HASH_ALLOCATE);
797 hashset(pp.prdtab, HASH_ALLOCATE);
798 hashset(pp.strtab, HASH_ALLOCATE);
799 hashset(pp.symtab, HASH_ALLOCATE);
800 if (pp.test & TEST_nonoise)
802 c = error_info.trace;
803 error_info.trace = 0;
805 #if DEBUG
806 if (!(pp.test & TEST_noinit))
808 #endif
811 * compose, push and read the builtin initialization script
814 if (!(sp = sfstropen()))
815 error(3, "temporary buffer allocation error");
816 sfprintf(sp,
818 #%s %s:%s \"/#<assert> /\" \"/assert /%s #/\"\n\
819 #%s %s:%s \"/#<unassert> /\" \"/unassert /%s #/\"\n\
821 dirname(PRAGMA),
822 pp.pass,
823 keyname(X_MAP),
824 dirname(DEFINE),
825 dirname(PRAGMA),
826 pp.pass,
827 keyname(X_MAP),
828 dirname(UNDEF));
829 if (pp.ppdefault && *pp.ppdefault)
831 if (pp.probe)
833 c = pp.lastdir->next->type;
834 pp.lastdir->next->type = 0;
836 if (ppsearch(pp.ppdefault, T_STRING, SEARCH_EXISTS) < 0)
838 free(pp.ppdefault);
839 if (!(pp.ppdefault = pathprobe(pp.path, NiL, "C", pp.pass, pp.probe ? pp.probe : PPPROBE, 0)))
840 error(1, "cannot determine default definitions for %s", pp.probe ? pp.probe : PPPROBE);
842 if (pp.probe)
843 pp.lastdir->next->type = c;
845 while (pp.firstop)
847 switch (pp.firstop->op)
849 case PP_ASSERT:
850 sfprintf(sp, "#%s #%s\n", dirname(DEFINE), pp.firstop->value);
851 break;
852 case PP_DEFINE:
853 if (*pp.firstop->value == '#')
854 sfprintf(sp, "#%s %s\n", dirname(DEFINE), pp.firstop->value);
855 else
857 if (s = strchr(pp.firstop->value, '='))
858 sfprintf(sp, "#%s %-.*s %s\n", dirname(DEFINE), s - pp.firstop->value, pp.firstop->value, s + 1);
859 else
860 sfprintf(sp, "#%s %s 1\n", dirname(DEFINE), pp.firstop->value);
862 break;
863 case PP_DIRECTIVE:
864 sfprintf(sp, "#%s\n", pp.firstop->value);
865 break;
866 case PP_OPTION:
867 if (s = strchr(pp.firstop->value, '='))
868 sfprintf(sp, "#%s %s:%-.*s %s\n", dirname(PRAGMA), pp.pass, s - pp.firstop->value, pp.firstop->value, s + 1);
869 else
870 sfprintf(sp, "#%s %s:%s\n", dirname(PRAGMA), pp.pass, pp.firstop->value);
871 break;
872 case PP_READ:
873 sfprintf(sp, "#%s \"%s\"\n", dirname(INCLUDE), pp.firstop->value);
874 break;
875 case PP_UNDEF:
876 sfprintf(sp, "#%s %s\n", dirname(UNDEF), pp.firstop->value);
877 break;
879 pp.lastop = pp.firstop;
880 pp.firstop = pp.firstop->next;
881 free(pp.lastop);
883 sfprintf(sp,
885 #%s %s:%s\n\
886 #%s %s:%s\n\
887 #%s !#%s(%s)\n\
888 #%s !#%s(%s) || #%s(%s)\n\
890 , dirname(PRAGMA)
891 , pp.pass
892 , keyname(X_BUILTIN)
893 , dirname(PRAGMA)
894 , pp.pass
895 , keyname(X_PREDEFINED)
896 , dirname(IF)
897 , keyname(X_OPTION)
898 , keyname(X_PLUSPLUS)
899 , dirname(IF)
900 , keyname(X_OPTION)
901 , keyname(X_COMPATIBILITY)
902 , keyname(X_OPTION)
903 , keyname(X_TRANSITION)
905 sfprintf(sp,
907 #%s #%s(%s)\n\
908 #%s %s:%s\n\
909 #%s %s:%s\n\
910 #%s __STRICT__ 1\n\
911 #%s\n\
912 #%s\n\
914 , dirname(IF)
915 , keyname(X_OPTION)
916 , keyname(X_STRICT)
917 , dirname(PRAGMA)
918 , pp.pass
919 , keyname(X_ALLMULTIPLE)
920 , dirname(PRAGMA)
921 , pp.pass
922 , keyname(X_READONLY)
923 , dirname(DEFINE)
924 , dirname(ENDIF)
925 , dirname(ENDIF)
927 for (kp = readonlys; s = kp->name; kp++)
929 if (!ppisid(*s))
930 s++;
931 sfprintf(sp, "#%s %s\n", dirname(UNDEF), s);
933 sfprintf(sp,
935 #%s\n\
936 #%s __STDPP__ 1\n\
937 #%s %s:no%s\n\
939 , dirname(ENDIF)
940 , dirname(DEFINE)
941 , dirname(PRAGMA)
942 , pp.pass
943 , keyname(X_PREDEFINED)
945 if (!pp.truncate)
946 sfprintf(sp,
948 #%s __STDPP__directive #(%s)\n\
950 , dirname(DEFINE)
951 , keyname(V_DIRECTIVE)
953 for (kp = variables; s = kp->name; kp++)
954 if (ppisid(*s) || *s++ == '+')
956 t = *s == '_' ? "" : "__";
957 sfprintf(sp, "#%s %s%s%s #(%s)\n" , dirname(DEFINE), t, s, t, s);
959 sfprintf(sp,
961 #%s %s:no%s\n\
962 #%s %s:no%s\n\
964 , dirname(PRAGMA)
965 , pp.pass
966 , keyname(X_READONLY)
967 , dirname(PRAGMA)
968 , pp.pass
969 , keyname(X_BUILTIN)
971 if (pp.ppdefault && *pp.ppdefault)
972 sfprintf(sp, "#%s \"%s\"\n", dirname(INCLUDE), pp.ppdefault);
973 sfprintf(sp,
975 #%s !defined(__STDC__) && (!#option(compatibility) || #option(transition))\n\
976 #%s __STDC__ #(STDC)\n\
977 #%s\n\
979 , dirname(IF)
980 , dirname(DEFINE)
981 , dirname(ENDIF)
983 t = sfstruse(sp);
984 debug((-9, "\n/* begin initialization */\n%s/* end initialization */", t));
985 ppcomment = pp.comment;
986 pp.comment = 0;
987 pplinesync = pp.linesync;
988 pp.linesync = 0;
989 PUSH_INIT(pp.pass, t);
990 pp.mode |= INIT;
991 while (pplex());
992 pp.mode &= ~INIT;
993 pp.comment = ppcomment;
994 pp.linesync = pplinesync;
995 pp.prefix = 0;
996 sfstrclose(sp);
997 if (error_info.trace)
998 for (dp = pp.firstdir; dp; dp = dp->next)
999 message((-1, "include directory %s%s%s%s", dp->name, (dp->type & TYPE_VENDOR) ? " [VENDOR]" : "", (dp->type & TYPE_HOSTED) ? " [HOSTED]" : "", dp->c ? " [C]" : ""));
1000 #if DEBUG
1002 if (pp.test & TEST_nonoise)
1003 error_info.trace = c;
1004 #endif
1007 * this is sleazy but at least it's
1008 * hidden in the library
1010 #include <preroot.h>
1011 #if FS_PREROOT
1012 struct pplist* preroot;
1014 if ((preroot = (struct pplist*)hashget(pp.prdtab, "preroot")))
1015 setpreroot(NiL, preroot->value);
1016 #endif
1018 if (pp.ignoresrc)
1020 if (pp.ignoresrc > 1 && pp.stddirs != pp.firstdir)
1021 error(1, "directories up to and including %s are for \"...\" include files only", pp.stddirs->name);
1022 pp.lcldirs = pp.lcldirs->next;
1024 if (pp.ignore)
1026 if (*pp.ignore)
1027 ppmapinclude(pp.ignore, NiL);
1028 else
1029 pp.ignore = 0;
1031 if (pp.standalone)
1032 pp.state |= STANDALONE;
1033 #if COMPATIBLE
1034 ppfsm(FSM_COMPATIBILITY, NiL);
1035 #endif
1036 ppfsm(FSM_PLUSPLUS, NiL);
1037 pp.initialized = 1;
1038 if (pp.reset.on)
1040 pp.reset.symtab = pp.symtab;
1041 pp.symtab = 0;
1042 pp.reset.ro_state = pp.ro_state;
1043 pp.reset.ro_mode = pp.ro_mode;
1044 pp.reset.ro_option = pp.ro_option;
1047 if (pp.reset.on)
1049 if (pp.symtab)
1051 hashwalk(pp.filtab, 0, unguard, NiL);
1052 hashfree(pp.symtab);
1054 pp.symtab = hashalloc(NiL, HASH_name, "symbols", HASH_free, undefine, HASH_set, HASH_ALLOCATE|HASH_BUCKET, 0);
1055 hashview(pp.symtab, pp.reset.symtab);
1056 pp.ro_state = pp.reset.ro_state;
1057 pp.ro_mode = pp.reset.ro_mode;
1058 pp.ro_option = pp.reset.ro_option;
1060 #if CHECKPOINT
1061 if (pp.mode & DUMP)
1063 if (!pp.pragma)
1064 error(3, "#%s must be enabled for checkpoints", dirname(PRAGMA));
1065 (*pp.pragma)(dirname(PRAGMA), pp.pass, keyname(X_CHECKPOINT), pp.checkpoint, 1);
1067 #endif
1068 if (n = pp.filedeps.flags)
1070 if (!(n & PP_deps_file))
1072 pp.state |= NOTEXT;
1073 pp.option |= KEEPNOTEXT;
1074 pp.linesync = 0;
1076 if (n & PP_deps_generated)
1077 pp.mode |= GENDEPS;
1078 if (n & PP_deps_local)
1079 pp.mode &= ~HEADERDEPS;
1080 else if (!(pp.mode & FILEDEPS))
1081 pp.mode |= HEADERDEPS;
1082 pp.mode |= FILEDEPS;
1086 * push the main input file -- special case for hosted mark
1089 if (pp.firstdir->type & TYPE_HOSTED)
1090 pp.mode |= MARKHOSTED;
1091 else
1092 pp.mode &= ~MARKHOSTED;
1093 #if CHECKPOINT
1094 if (!(pp.mode & DUMP))
1095 #endif
1097 if (!(p = error_info.file))
1098 p = "";
1099 else
1101 error_info.file = 0;
1102 if (*p)
1104 pathcanon(p, 0);
1105 p = ppsetfile(p)->name;
1108 PUSH_FILE(p, 0);
1110 if (pp.mode & FILEDEPS)
1112 if (s = strrchr(error_info.file, '/'))
1113 s++;
1114 else
1115 s = error_info.file;
1116 if (!*s)
1117 s = "-";
1118 s = strcpy(pp.tmpbuf, s);
1119 if ((t = p = strrchr(s, '.')) && (*++p == 'c' || *p == 'C'))
1121 if (c = *++p)
1122 while (*++p == c);
1123 if (*p)
1124 t = 0;
1125 else
1126 t++;
1128 if (!t)
1130 t = s + strlen(s);
1131 *t++ = '.';
1133 *(t + 1) = 0;
1134 if (pp.state & NOTEXT)
1135 pp.filedeps.sp = sfstdout;
1136 else
1138 *t = 'd';
1139 if (!(pp.filedeps.sp = sfopen(NiL, s, "w")))
1140 error(ERROR_SYSTEM|3, "%s: cannot create", s);
1142 *t = 'o';
1143 pp.column = sfprintf(pp.filedeps.sp, "%s :", s);
1144 if (*error_info.file)
1145 pp.column += sfprintf(pp.filedeps.sp, " %s", error_info.file);
1147 if (xp = pp.firsttx)
1149 if (!(sp = sfstropen()))
1150 error(3, "temporary buffer allocation error");
1151 while (xp)
1153 sfprintf(sp, "#%s \"%s\"\n", dirname(INCLUDE), xp->value);
1154 xp = xp->next;
1156 t = sfstruse(sp);
1157 PUSH_BUFFER("options", t, 1);
1158 sfstrclose(sp);
1160 break;
1161 case PP_INPUT:
1162 #if CHECKPOINT && POOL
1163 if (!(pp.mode & DUMP) || pp.pool.input)
1164 #else
1165 #if CHECKPOINT
1166 if (!(pp.mode & DUMP))
1167 #else
1168 #if POOL
1169 if (pp.pool.input)
1170 #endif
1171 #endif
1172 #endif
1174 p = va_arg(ap, char*);
1175 if (!error_info.file)
1176 error_info.file = p;
1177 close(0);
1178 if (open(p, O_RDONLY) != 0)
1179 error(ERROR_SYSTEM|3, "%s: cannot read", p);
1180 if (strmatch(p, "*.(s|S|as|AS|asm|ASM)"))
1182 set(&pp.mode, CATLITERAL, 0);
1183 ppop(PP_SPACEOUT, 1);
1185 break;
1187 /*FALLTHROUGH*/
1188 case PP_TEXT:
1189 if (pp.initialized)
1190 goto before;
1191 if ((p = va_arg(ap, char*)) && *p)
1193 if (pp.lasttx)
1194 pp.lasttx = pp.lasttx->next = newof(0, struct oplist, 1, 0);
1195 else
1196 pp.firsttx = pp.lasttx = newof(0, struct oplist, 1, 0);
1197 pp.lasttx->op = op;
1198 pp.lasttx->value = p;
1200 break;
1201 case PP_KEYARGS:
1202 if (pp.initialized)
1203 goto before;
1204 set(&pp.option, KEYARGS, va_arg(ap, int));
1205 if (pp.option & KEYARGS)
1206 #if MACKEYARGS
1207 set(&pp.mode, CATLITERAL, 1);
1208 #else
1209 error(3, "preprocessor not compiled with macro keyword arguments enabled [MACKEYARGS]");
1210 #endif
1211 break;
1212 case PP_LINE:
1213 pp.linesync = va_arg(ap, PPLINESYNC);
1214 break;
1215 case PP_LINEBASE:
1216 if (va_arg(ap, int))
1217 pp.flags |= PP_linebase;
1218 else
1219 pp.flags &= ~PP_linebase;
1220 break;
1221 case PP_LINEFILE:
1222 if (va_arg(ap, int))
1223 pp.flags |= PP_linefile;
1224 else
1225 pp.flags &= ~PP_linefile;
1226 break;
1227 case PP_LINEID:
1228 if (!(p = va_arg(ap, char*)))
1229 pp.lineid = "";
1230 else if (*p != '-')
1231 pp.lineid = strdup(p);
1232 else
1233 pp.option |= IGNORELINE;
1234 break;
1235 case PP_LINETYPE:
1236 if ((n = va_arg(ap, int)) >= 1)
1237 pp.flags |= PP_linetype;
1238 else
1239 pp.flags &= ~PP_linetype;
1240 if (n >= 2)
1241 pp.flags |= PP_linehosted;
1242 else
1243 pp.flags &= ~PP_linehosted;
1244 break;
1245 case PP_LOCAL:
1246 if (pp.initialized)
1247 goto before;
1248 pp.ignoresrc++;
1249 pp.stddirs = pp.lastdir;
1250 if (!(pp.ro_option & PREFIX))
1251 pp.option &= ~PREFIX;
1252 break;
1253 case PP_MACREF:
1254 pp.macref = va_arg(ap, PPMACREF);
1255 break;
1256 case PP_MULTIPLE:
1257 set(&pp.mode, ALLMULTIPLE, va_arg(ap, int));
1258 break;
1259 case PP_NOHASH:
1260 set(&pp.option, NOHASH, va_arg(ap, int));
1261 break;
1262 case PP_NOISE:
1263 op = va_arg(ap, int);
1264 set(&pp.option, NOISE, op);
1265 set(&pp.option, NOISEFILTER, op < 0);
1266 break;
1267 case PP_OPTARG:
1268 pp.optarg = va_arg(ap, PPOPTARG);
1269 break;
1270 case PP_OUTPUT:
1271 pp.outfile = va_arg(ap, char*);
1272 if (identical(pp.outfile, 0))
1273 error(3, "%s: identical to input", pp.outfile);
1274 close(1);
1275 if (open(pp.outfile, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) != 1)
1276 error(ERROR_SYSTEM|3, "%s: cannot create", pp.outfile);
1277 break;
1278 case PP_PASSTHROUGH:
1279 if (!(pp.state & COMPILE))
1280 set(&pp.state, PASSTHROUGH, va_arg(ap, int));
1281 break;
1282 case PP_PEDANTIC:
1283 set(&pp.mode, PEDANTIC, va_arg(ap, int));
1284 break;
1285 case PP_PLUSCOMMENT:
1286 set(&pp.option, PLUSCOMMENT, va_arg(ap, int));
1287 if (pp.initialized)
1288 ppfsm(FSM_PLUSPLUS, NiL);
1289 break;
1290 case PP_PLUSPLUS:
1291 set(&pp.option, PLUSPLUS, va_arg(ap, int));
1292 set(&pp.option, PLUSCOMMENT, va_arg(ap, int));
1293 if (pp.initialized)
1294 ppfsm(FSM_PLUSPLUS, NiL);
1295 break;
1296 case PP_POOL:
1297 if (pp.initialized)
1298 goto before;
1299 if (va_arg(ap, int))
1301 #if POOL
1302 pp.pool.input = dup(0);
1303 pp.pool.output = dup(1);
1304 p = "/dev/null";
1305 if (!identical(p, 0))
1307 if (!identical(p, 1))
1308 ppop(PP_OUTPUT, p);
1309 ppop(PP_INPUT, p);
1311 #else
1312 error(3, "preprocessor not compiled with input pool enabled [POOL]");
1313 #endif
1315 break;
1316 case PP_PRAGMA:
1317 pp.pragma = va_arg(ap, PPPRAGMA);
1318 break;
1319 case PP_PRAGMAFLAGS:
1320 if (p = va_arg(ap, char*))
1322 n = OPT_GLOBAL;
1323 if (*p == '-')
1324 p++;
1325 else
1326 n |= OPT_PASS;
1327 if ((c = (int)hashref(pp.strtab, p)) > 0 && c <= X_last_option)
1328 pp.optflags[c] = n;
1330 break;
1331 case PP_PROBE:
1332 pp.probe = va_arg(ap, char*);
1333 break;
1334 case PP_QUOTE:
1335 p = va_arg(ap, char*);
1336 c = va_arg(ap, int);
1337 if (p)
1338 ppfsm(c ? FSM_QUOTADD : FSM_QUOTDEL, p);
1339 break;
1340 case PP_REGUARD:
1341 set(&pp.option, REGUARD, va_arg(ap, int));
1342 break;
1343 case PP_RESERVED:
1344 if ((pp.state & COMPILE) && (p = va_arg(ap, char*)))
1346 if (!(sp = sfstropen()))
1347 error(3, "temporary buffer allocation error");
1348 sfputr(sp, p, -1);
1349 p = sfstruse(sp);
1350 if (s = strchr(p, '='))
1351 *s++ = 0;
1352 else
1353 s = p;
1354 while (*s == '_')
1355 s++;
1356 for (t = s + strlen(s); t > s && *(t - 1) == '_'; t--);
1357 if (*t == '_')
1358 *t = 0;
1359 else
1360 t = 0;
1361 op = ((key = ppkeyref(pp.symtab, s)) && (key->sym.flags & SYM_LEX)) ? key->lex : T_NOISE;
1362 if (pp.test & 0x0400)
1363 error(1, "reserved#1 `%s' %d", s, op);
1364 if (t)
1365 *t = '_';
1366 if (!(key = ppkeyget(pp.symtab, p)))
1367 key = ppkeyset(pp.symtab, NiL);
1368 else if (!(key->sym.flags & SYM_LEX))
1370 struct ppsymbol tmp;
1372 tmp = key->sym;
1373 hashlook(pp.symtab, p, HASH_DELETE, NiL);
1374 key = ppkeyset(pp.symtab, NiL);
1375 key->sym.flags = tmp.flags;
1376 key->sym.macro = tmp.macro;
1377 key->sym.value = tmp.value;
1378 key->sym.hidden = tmp.hidden;
1380 if (!(key->sym.flags & SYM_KEYWORD))
1382 key->sym.flags |= SYM_KEYWORD|SYM_LEX;
1383 key->lex = op;
1384 if (pp.test & 0x0400)
1385 error(1, "reserved#2 `%s' %d", p, op);
1387 sfstrclose(sp);
1389 break;
1390 case PP_SPACEOUT:
1391 set(&pp.state, SPACEOUT, va_arg(ap, int));
1392 break;
1393 case PP_STANDALONE:
1394 if (pp.initialized)
1395 goto before;
1396 pp.standalone = 1;
1397 break;
1398 case PP_STANDARD:
1399 if ((pp.lastdir->next->name = ((p = va_arg(ap, char*)) && *p) ? p : NiL) && !stat(p, &st))
1400 SAVEID(&pp.lastdir->next->id, &st);
1401 for (dp = pp.firstdir; dp; dp = dp->next)
1402 if (dp->name)
1403 for (hp = pp.firstdir; hp != dp; hp = hp->next)
1404 if (hp->name && SAMEID(&hp->id, &dp->id))
1406 hp->c = dp->c;
1407 if (dp->type & TYPE_HOSTED)
1408 hp->type |= TYPE_HOSTED;
1409 else
1410 hp->type &= ~TYPE_HOSTED;
1412 break;
1413 case PP_STRICT:
1414 set(&pp.state, TRANSITION, 0);
1415 pp.flags &= ~PP_transition;
1416 set(&pp.state, STRICT, va_arg(ap, int));
1417 if (pp.state & STRICT)
1418 pp.flags |= PP_strict;
1419 else
1420 pp.flags &= ~PP_strict;
1421 break;
1422 case PP_TEST:
1423 if (p = va_arg(ap, char*))
1424 for (;;)
1426 while (*p == ' ' || *p == '\t') p++;
1427 for (s = p; n = *s; s++)
1428 if (n == ',' || n == ' ' || n == '\t')
1430 *s++ = 0;
1431 break;
1433 if (!*p)
1434 break;
1435 n = 0;
1436 if (*p == 'n' && *(p + 1) == 'o')
1438 p += 2;
1439 op = 0;
1441 else
1442 op = 1;
1443 if (streq(p, "count"))
1444 n = TEST_count;
1445 else if (streq(p, "hashcount"))
1446 n = TEST_hashcount;
1447 else if (streq(p, "hashdump"))
1448 n = TEST_hashdump;
1449 else if (streq(p, "hit"))
1450 n = TEST_hit;
1451 else if (streq(p, "init"))
1452 n = TEST_noinit|TEST_INVERT;
1453 else if (streq(p, "noise"))
1454 n = TEST_nonoise|TEST_INVERT;
1455 else if (streq(p, "proto"))
1456 n = TEST_noproto|TEST_INVERT;
1457 else if (*p >= '0' && *p <= '9')
1458 n = strtoul(p, NiL, 0);
1459 else
1461 error(1, "%s: unknown test", p);
1462 break;
1464 if (n & TEST_INVERT)
1466 n &= ~TEST_INVERT;
1467 op = !op;
1469 if (op)
1470 pp.test |= n;
1471 else
1472 pp.test &= ~n;
1473 p = s;
1474 debug((-4, "test = 0%o", pp.test));
1476 break;
1477 case PP_TRANSITION:
1478 set(&pp.state, STRICT, 0);
1479 pp.flags &= ~PP_strict;
1480 set(&pp.state, TRANSITION, va_arg(ap, int));
1481 if (pp.state & TRANSITION)
1482 pp.flags |= PP_transition;
1483 else
1484 pp.flags &= ~PP_transition;
1485 break;
1486 case PP_TRUNCATE:
1487 if (pp.initialized)
1488 goto before;
1489 if ((op = va_arg(ap, int)) < 0)
1490 op = 0;
1491 set(&pp.option, TRUNCATE, op);
1492 if (pp.option & TRUNCATE)
1494 Hash_bucket_t* b;
1495 Hash_bucket_t* p;
1496 Hash_position_t* pos;
1497 Hash_table_t* tab;
1499 pp.truncate = op;
1500 tab = pp.symtab;
1501 pp.symtab = hashalloc(NiL, HASH_set, tab ? HASH_ALLOCATE : 0, HASH_compare, trunccomp, HASH_hash, trunchash, HASH_name, "truncate", 0);
1502 if (tab && (pos = hashscan(tab, 0)))
1504 if (p = hashnext(pos))
1507 b = hashnext(pos);
1508 hashlook(pp.symtab, (char*)p, HASH_BUCKET|HASH_INSTALL, NiL);
1509 } while (p = b);
1510 hashdone(pos);
1513 else
1514 pp.truncate = 0;
1515 break;
1516 case PP_VENDOR:
1517 p = va_arg(ap, char*);
1518 c = va_arg(ap, int) != 0;
1519 if (!p || !*p)
1520 for (dp = pp.firstdir; dp; dp = dp->next)
1521 dp->type &= ~TYPE_VENDOR;
1522 else if (streq(p, "-"))
1524 for (dp = pp.firstdir; dp; dp = dp->next)
1525 if (c)
1526 dp->type |= TYPE_VENDOR;
1527 else
1528 dp->type &= ~TYPE_VENDOR;
1530 else if (!stat((pathcanon(p, 0), p), &st))
1532 c = 0;
1533 for (dp = pp.firstdir; dp; dp = dp->next)
1535 if (!c && ((dp->type & TYPE_VENDOR) || dp->name && SAMEID(&dp->id, &st)))
1536 c = 1;
1537 if (c)
1538 dp->type |= TYPE_VENDOR;
1539 else
1540 dp->type &= ~TYPE_VENDOR;
1543 break;
1544 case PP_WARN:
1545 set(&pp.state, WARN, va_arg(ap, int));
1546 break;
1547 before:
1548 error(3, "ppop(%d): preprocessor operation must be done before PP_INIT", op);
1549 break;
1550 default:
1551 error(3, "ppop(%d): invalid preprocessor operation", op);
1552 break;
1554 va_end(ap);