10 char *outptr
= outbuf
;
13 struct token nltoken
= {NL
, 0, 0, 1, (uchar
*) "\n", 0};
20 char rcsid
[] = "$Version 1.2 $ $Revision: 1.5 $ $Date: 2006-06-20 05:05:46 $";
26 main(int argc
, char **argv
)
49 process(Tokenrow
* trp
)
55 if (trp
->tp
>= trp
->lp
)
57 trp
->tp
= trp
->lp
= trp
->bp
;
59 anymacros
|= gettokens(trp
, 1);
62 if (trp
->tp
->type
== END
)
66 if (cursource
->ifdepth
)
68 "Unterminated conditional in #include");
70 cursource
->line
+= cursource
->lineinc
;
77 error(ERROR
, "Unterminated #if/#ifdef/#ifndef");
80 if (trp
->tp
->type
== SHARP
)
86 if (!skipping
&& anymacros
)
92 cursource
->line
+= cursource
->lineinc
;
93 if (cursource
->lineinc
> 1)
102 control(Tokenrow
* trp
)
108 if (tp
->type
!= NAME
)
110 if (tp
->type
== NUMBER
)
113 error(ERROR
, "Unidentifiable control line");
114 return; /* else empty line */
116 if ((np
= lookup(tp
, 0)) == NULL
|| ((np
->flag
& ISKW
) == 0 && !skipping
))
118 error(WARNING
, "Unknown preprocessor control %t", tp
);
126 if (--ifdepth
< skipping
)
128 --cursource
->ifdepth
;
135 if (++ifdepth
>= NIF
)
136 error(FATAL
, "#if too deeply nested");
137 ++cursource
->ifdepth
;
142 if (ifdepth
<= skipping
)
158 if (tp
->type
!= NAME
|| trp
->lp
- trp
->bp
!= 4)
160 error(ERROR
, "Syntax error in #undef");
163 if ((np
= lookup(tp
, 0)) != NULL
)
165 np
->flag
&= ~ISDEFINED
;
170 error(INFO
, "Macro deletion of %s(%r)", np
->name
, np
->ap
);
172 error(INFO
, "Macro deletion of %s", np
->name
);
179 for (tp
= trp
->tp
- 1; ((tp
->type
!= NL
) && (tp
< trp
->lp
)); tp
++)
186 if (++ifdepth
>= NIF
)
187 error(FATAL
, "#if too deeply nested");
188 ++cursource
->ifdepth
;
189 ifsatisfied
[ifdepth
] = 0;
190 if (eval(trp
, np
->val
))
191 ifsatisfied
[ifdepth
] = 1;
199 error(ERROR
, "#elif with no #if");
202 if (ifsatisfied
[ifdepth
] == 2)
203 error(ERROR
, "#elif after #else");
204 if (eval(trp
, np
->val
))
206 if (ifsatisfied
[ifdepth
])
211 ifsatisfied
[ifdepth
] = 1;
219 if (ifdepth
== 0 || cursource
->ifdepth
== 0)
221 error(ERROR
, "#else with no #if");
224 if (ifsatisfied
[ifdepth
] == 2)
225 error(ERROR
, "#else after #else");
226 if (trp
->lp
- trp
->bp
!= 3)
227 error(ERROR
, "Syntax error in #else");
228 skipping
= ifsatisfied
[ifdepth
] ? ifdepth
: 0;
229 ifsatisfied
[ifdepth
] = 2;
233 if (ifdepth
== 0 || cursource
->ifdepth
== 0)
235 error(ERROR
, "#endif with no #if");
239 --cursource
->ifdepth
;
240 if (trp
->lp
- trp
->bp
!= 3)
241 error(WARNING
, "Syntax error in #endif");
246 error(WARNING
, "#error directive: %r", trp
);
251 expandrow(trp
, "<line>");
254 if (tp
+ 1 >= trp
->lp
|| tp
->type
!= NUMBER
|| tp
+ 3 < trp
->lp
255 || (tp
+ 3 == trp
->lp
256 && ((tp
+ 1)->type
!= STRING
|| *(tp
+ 1)->t
== 'L')))
258 error(ERROR
, "Syntax error in #line");
261 cursource
->line
= atol((char *) tp
->t
) - 1;
262 if (cursource
->line
< 0 || cursource
->line
>= 32768)
263 error(WARNING
, "#line specifies number out of range");
265 if (tp
+ 1 < trp
->lp
)
266 cursource
->filename
= (char *) newstring(tp
->t
+ 1, tp
->len
- 2, 0);
270 error(ERROR
, "Bad syntax for control line");
274 doinclude(trp
, -1, 1);
279 doinclude(trp
, -1, 0);
284 doinclude(trp
, cursource
->pathdepth
, 0);
293 error(ERROR
, "Preprocessor control `%t' not yet implemented", tp
);
303 void *p
= malloc(size
);
306 error(FATAL
, "Out of memory from malloc");
317 error(enum errtype type
, char *string
,...)
326 fprintf(stderr
, "cpp: ");
327 for (s
= cursource
; s
; s
= s
->next
)
329 fprintf(stderr
, "%s:%d ", s
->filename
, s
->line
);
330 va_start(ap
, string
);
331 for (ep
= string
; *ep
; ep
++)
339 c
= (char) va_arg(ap
, int);
340 fprintf(stderr
, "%c", c
);
344 cp
= va_arg(ap
, char *);
345 fprintf(stderr
, "%s", cp
);
350 fprintf(stderr
, "%d", i
);
354 tp
= va_arg(ap
, Token
*);
355 fprintf(stderr
, "%.*s", (int)tp
->len
, tp
->t
);
359 trp
= va_arg(ap
, Tokenrow
*);
360 for (tp
= trp
->tp
; tp
< trp
->lp
&& tp
->type
!= NL
; tp
++)
362 if (tp
> trp
->tp
&& tp
->wslen
)
364 fprintf(stderr
, "%.*s", (int)tp
->len
, tp
->t
);