1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
29 char *outptr
= outbuf
;
32 struct token nltoken
= {NL
, 0, 0, 1, (uchar
*) "\n", 0};
43 main(int argc
, char **argv
)
66 process(Tokenrow
* trp
)
72 if (trp
->tp
>= trp
->lp
)
74 trp
->tp
= trp
->lp
= trp
->bp
;
76 anymacros
|= gettokens(trp
, 1);
79 if (trp
->tp
->type
== END
)
83 if (cursource
->ifdepth
)
85 "Unterminated conditional in #include");
87 cursource
->line
+= cursource
->lineinc
;
94 error(ERROR
, "Unterminated #if/#ifdef/#ifndef");
97 if (trp
->tp
->type
== SHARP
)
103 if (!skipping
&& anymacros
)
104 expandrow(trp
, NULL
);
109 cursource
->line
+= cursource
->lineinc
;
110 if (cursource
->lineinc
> 1)
119 control(Tokenrow
* trp
)
125 if (tp
->type
!= NAME
)
127 if (tp
->type
== NUMBER
)
130 error(ERROR
, "Unidentifiable control line");
131 return; /* else empty line */
133 if ((np
= lookup(tp
, 0)) == NULL
|| ((np
->flag
& ISKW
) == 0 && !skipping
))
135 error(WARNING
, "Unknown preprocessor control %t", tp
);
143 if (--ifdepth
< skipping
)
145 --cursource
->ifdepth
;
152 if (++ifdepth
>= NIF
)
153 error(FATAL
, "#if too deeply nested");
154 ++cursource
->ifdepth
;
159 if (ifdepth
<= skipping
)
175 if (tp
->type
!= NAME
|| trp
->lp
- trp
->bp
!= 4)
177 error(ERROR
, "Syntax error in #undef");
180 if ((np
= lookup(tp
, 0)) != NULL
)
182 np
->flag
&= ~ISDEFINED
;
187 error(INFO
, "Macro deletion of %s(%r)", np
->name
, np
->ap
);
189 error(INFO
, "Macro deletion of %s", np
->name
);
196 for (tp
= trp
->tp
- 1; ((tp
->type
!= NL
) && (tp
< trp
->lp
)); tp
++)
203 if (++ifdepth
>= NIF
)
204 error(FATAL
, "#if too deeply nested");
205 ++cursource
->ifdepth
;
206 ifsatisfied
[ifdepth
] = 0;
207 if (eval(trp
, np
->val
))
208 ifsatisfied
[ifdepth
] = 1;
216 error(ERROR
, "#elif with no #if");
219 if (ifsatisfied
[ifdepth
] == 2)
220 error(ERROR
, "#elif after #else");
221 if (eval(trp
, np
->val
))
223 if (ifsatisfied
[ifdepth
])
228 ifsatisfied
[ifdepth
] = 1;
236 if (ifdepth
== 0 || cursource
->ifdepth
== 0)
238 error(ERROR
, "#else with no #if");
241 if (ifsatisfied
[ifdepth
] == 2)
242 error(ERROR
, "#else after #else");
243 if (trp
->lp
- trp
->bp
!= 3)
244 error(ERROR
, "Syntax error in #else");
245 skipping
= ifsatisfied
[ifdepth
] ? ifdepth
: 0;
246 ifsatisfied
[ifdepth
] = 2;
250 if (ifdepth
== 0 || cursource
->ifdepth
== 0)
252 error(ERROR
, "#endif with no #if");
256 --cursource
->ifdepth
;
257 if (trp
->lp
- trp
->bp
!= 3)
258 error(WARNING
, "Syntax error in #endif");
263 error(WARNING
, "#error directive: %r", trp
);
268 expandrow(trp
, "<line>");
271 if (tp
+ 1 >= trp
->lp
|| tp
->type
!= NUMBER
|| tp
+ 3 < trp
->lp
272 || (tp
+ 3 == trp
->lp
273 && ((tp
+ 1)->type
!= STRING
|| *(tp
+ 1)->t
== 'L')))
275 error(ERROR
, "Syntax error in #line");
278 cursource
->line
= atol((char *) tp
->t
) - 1;
279 if (cursource
->line
< 0 || cursource
->line
>= 32768)
280 error(WARNING
, "#line specifies number out of range");
282 if (tp
+ 1 < trp
->lp
)
283 cursource
->filename
= (char *) newstring(tp
->t
+ 1, tp
->len
- 2, 0);
287 error(ERROR
, "Bad syntax for control line");
291 doinclude(trp
, -1, 1);
296 doinclude(trp
, -1, 0);
301 doinclude(trp
, cursource
->pathdepth
, 0);
310 error(ERROR
, "Preprocessor control `%t' not yet implemented", tp
);
318 domalloc(size_t size
)
320 void *p
= malloc(size
);
323 error(FATAL
, "Out of memory from malloc");
334 error(enum errtype type
, char *string
,...)
343 fprintf(stderr
, "cpp: ");
344 for (s
= cursource
; s
; s
= s
->next
)
346 fprintf(stderr
, "%s:%d ", s
->filename
, s
->line
);
347 va_start(ap
, string
);
348 for (ep
= string
; *ep
; ep
++)
356 c
= (char) va_arg(ap
, int);
357 fprintf(stderr
, "%c", c
);
361 cp
= va_arg(ap
, char *);
362 fprintf(stderr
, "%s", cp
);
367 fprintf(stderr
, "%d", i
);
371 tp
= va_arg(ap
, Token
*);
372 fprintf(stderr
, "%.*s", (int)tp
->len
, tp
->t
);
376 trp
= va_arg(ap
, Tokenrow
*);
377 for (tp
= trp
->tp
; tp
< trp
->lp
&& tp
->type
!= NL
; tp
++)
379 if (tp
> trp
->tp
&& tp
->wslen
)
381 fprintf(stderr
, "%.*s", (int)tp
->len
, tp
->t
);
402 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */