6 extern int getopt(int, char **, char *);
25 "ifdef", KIFDEF, ISKW,
26 "ifndef", KIFNDEF, ISKW,
29 "endif", KENDIF, ISKW,
30 "include", KINCLUDE, ISKW,
31 "define", KDEFINE, ISKW,
32 "undef", KUNDEF, ISKW,
34 "error", KERROR, ISKW,
35 "warning", KWARNING, ISKW, // extension to ANSI
36 "pragma", KPRAGMA, ISKW,
38 "defined", KDEFINED, ISDEFINED+ISUNCHANGE,
39 "__LINE__", KLINENO, ISMAC+ISUNCHANGE,
40 "__FILE__", KFILE, ISMAC+ISUNCHANGE,
41 "__DATE__", KDATE, ISMAC+ISUNCHANGE,
42 "__TIME__", KTIME, ISMAC+ISUNCHANGE,
43 "__STDC__", KSTDC, ISUNCHANGE,
47 unsigned long namebit[077+1];
51 setup(int argc, char **argv)
63 static Token deftoken[1] = {{ NAME, 0, 0, 0, 7, (uchar*)"defined" }};
64 static Tokenrow deftr = { deftoken, deftoken, deftoken+1, 1 };
69 for (kp=kwtab; kp->kw; kp++) {
71 t.len = strlen(kp->kw);
75 if (np->val == KDEFINED) {
83 * For Plan 9, search /objtype/include, then /sys/include
84 * (Note that includelist is searched from high end to low)
86 if ((objtype = getenv("objtype"))){
87 snprintf(nbuf, sizeof nbuf, "/%s/include", objtype);
88 includelist[1].file = nbuf;
89 includelist[1].always = 1;
91 includelist[1].file = NULL;
92 error(WARNING, "Unknown $objtype");
94 if (getwd(wd, sizeof(wd))==0)
96 includelist[0].file = "/sys/include";
97 includelist[0].always = 1;
98 firstinclude = NINCLUDE-2;
99 if ((includeenv=getenv("include")) != NULL) {
101 includeenv = strdup(includeenv);
102 for (;firstinclude>0; firstinclude--) {
103 cp = strtok(includeenv, " ");
106 includelist[firstinclude].file = cp;
107 includelist[firstinclude].always = 1;
111 setsource("", -1, 0);
114 for (i=0; i<NINCLUDE; i++)
115 if (includelist[i].always==1)
116 includelist[i].deleted = 1;
119 for (i=firstinclude; i>=0; i--) {
120 if (includelist[i].file==NULL) {
121 includelist[i].always = 1;
122 includelist[i].file = ARGF();
127 error(WARNING, "Too many -I directives");
131 setsource("<cmdarg>", -1, ARGF());
132 maketokenrow(3, &tr);
134 doadefine(&tr, ARGC());
144 /* Ignored for compatibility */
157 error(FATAL, "Unknown argument '%s'", xx);
164 if ((fp = strrchr(argv[0], '/')) != NULL) {
165 int len = fp - argv[0];
166 dp = (char*)newstring((uchar*)argv[0], len+1, 0);
169 fp = (char*)newstring((uchar*)argv[0], strlen(argv[0]), 0);
170 if ((fd = open(fp, 0)) < 0)
171 error(FATAL, "Can't open input file %s", fp);
174 int fdo = create(argv[1], 1, 0666);
176 error(FATAL, "Can't open output file %s", argv[1]);
181 includelist[NINCLUDE-1].always = 0;
182 includelist[NINCLUDE-1].file = dp;
184 includelist[NINCLUDE-1].deleted = 1;
185 setsource(fp, fd, NULL);
187 for (i=0; i<NINCLUDE; i++)
188 if (includelist[i].file && includelist[i].deleted==0)
189 error(WARNING, "Include: %s", includelist[i].file);
194 lookup(Token *tp, int install)
201 for (cp=tp->t, cpe=cp+tp->len; cp<cpe; )
206 if (*tp->t==*np->name && tp->len==np->len
207 && strncmp((char*)tp->t, (char*)np->name, tp->len)==0)
218 np->name = newstring(tp->t, tp->len, 0);
221 quickset(tp->t[0], tp->len>1? tp->t[1]:0);