1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
39 char *outptr
= outbuf
;
42 struct token nltoken
= {NL
, 0, 0, 1, (uchar
*) "\n"};
49 char rcsid
[] = "$Version 1.2 $ $Revision: 1.10 $ $Date: 2008-07-11 14:50:24 $";
54 void checkCommandFile(char* cmdfile
)
59 commandfile
= fopen(cmdfile
+1, "r");
60 if( commandfile
== NULL
)
62 error(FATAL
, "Can't open command file %s", cmdfile
);
68 while ( fscanf(commandfile
, "%c", &c
) != EOF
)
79 if (found
|| c
!=' ') {
90 if (option
[0]== '@' || option
[1]== '@')
92 checkCommandFile(option
);
95 realargv
[realargc
]= strdup(option
);
100 if (option
[0] != '\0') {
102 realargv
[realargc
]= strdup(option
);
109 void checkCommandArgs(int argc
, char **argv
)
113 for (i
=0; i
<argc
; i
++)
115 if (argv
[i
][0]== '@')
117 checkCommandFile(argv
[i
]);
120 realargv
[i
]= strdup(argv
[i
]);
126 void cleanCommandArgs()
130 for (i
=0; i
<realargc
; i
++)
137 int __cdecl
main(int argc
, char **argv
)
139 int main(int argc
, char **argv
)
146 checkCommandArgs(argc
, argv
);
148 setbuf(stderr
, ebuf
);
151 maketokenrow(3, &tr
);
153 setup(realargc
, realargv
);
165 process(Tokenrow
* trp
)
171 if (trp
->tp
>= trp
->lp
)
173 trp
->tp
= trp
->lp
= trp
->bp
;
175 anymacros
|= gettokens(trp
, 1);
178 if (trp
->tp
->type
== END
)
182 if (cursource
->ifdepth
)
184 "Unterminated conditional in #include");
186 cursource
->line
+= cursource
->lineinc
;
193 error(ERROR
, "Unterminated #if/#ifdef/#ifndef");
196 if (trp
->tp
->type
== SHARP
)
202 if (!skipping
&& anymacros
)
203 expandrow(trp
, NULL
);
208 cursource
->line
+= cursource
->lineinc
;
209 if (cursource
->lineinc
> 1)
218 control(Tokenrow
* trp
)
224 if (tp
->type
!= NAME
)
226 if (tp
->type
== NUMBER
)
229 error(ERROR
, "Unidentifiable control line");
230 return; /* else empty line */
232 if ((np
= lookup(tp
, 0)) == NULL
|| ((np
->flag
& ISKW
) == 0 && !skipping
))
234 error(WARNING
, "Unknown preprocessor control %t", tp
);
242 if (--ifdepth
< skipping
)
244 --cursource
->ifdepth
;
251 if (++ifdepth
>= NIF
)
252 error(FATAL
, "#if too deeply nested");
253 ++cursource
->ifdepth
;
258 if (ifdepth
<= skipping
)
274 if (tp
->type
!= NAME
|| trp
->lp
- trp
->bp
!= 4)
276 error(ERROR
, "Syntax error in #undef");
279 if ((np
= lookup(tp
, 0)) != NULL
)
281 np
->flag
&= ~ISDEFINED
;
286 error(INFO
, "Macro deletion of %s(%r)", np
->name
, np
->ap
);
288 error(INFO
, "Macro deletion of %s", np
->name
);
295 for (tp
= trp
->tp
- 1; ((tp
->type
!= NL
) && (tp
< trp
->lp
)); tp
++)
302 if (++ifdepth
>= NIF
)
303 error(FATAL
, "#if too deeply nested");
304 ++cursource
->ifdepth
;
305 ifsatisfied
[ifdepth
] = 0;
306 if (eval(trp
, np
->val
))
307 ifsatisfied
[ifdepth
] = 1;
315 error(ERROR
, "#elif with no #if");
318 if (ifsatisfied
[ifdepth
] == 2)
319 error(ERROR
, "#elif after #else");
320 if (eval(trp
, np
->val
))
322 if (ifsatisfied
[ifdepth
])
327 ifsatisfied
[ifdepth
] = 1;
335 if (ifdepth
== 0 || cursource
->ifdepth
== 0)
337 error(ERROR
, "#else with no #if");
340 if (ifsatisfied
[ifdepth
] == 2)
341 error(ERROR
, "#else after #else");
342 if (trp
->lp
- trp
->bp
!= 3)
343 error(ERROR
, "Syntax error in #else");
344 skipping
= ifsatisfied
[ifdepth
] ? ifdepth
: 0;
345 ifsatisfied
[ifdepth
] = 2;
349 if (ifdepth
== 0 || cursource
->ifdepth
== 0)
351 error(ERROR
, "#endif with no #if");
355 --cursource
->ifdepth
;
356 if (trp
->lp
- trp
->bp
!= 3)
357 error(WARNING
, "Syntax error in #endif");
362 error(WARNING
, "#error directive: %r", trp
);
367 expandrow(trp
, "<line>");
370 if (tp
+ 1 >= trp
->lp
|| tp
->type
!= NUMBER
|| tp
+ 3 < trp
->lp
371 || (tp
+ 3 == trp
->lp
372 && ((tp
+ 1)->type
!= STRING
|| *(tp
+ 1)->t
== 'L')))
374 error(ERROR
, "Syntax error in #line");
377 cursource
->line
= atol((char *) tp
->t
) - 1;
378 if (cursource
->line
< 0 || cursource
->line
>= 32768)
379 error(WARNING
, "#line specifies number out of range");
381 if (tp
+ 1 < trp
->lp
)
382 cursource
->filename
= (char *) newstring(tp
->t
+ 1, tp
->len
- 2, 0);
386 error(ERROR
, "Bad syntax for control line");
390 doinclude(trp
, -1, 1);
395 doinclude(trp
, -1, 0);
400 doinclude(trp
, cursource
->pathdepth
, 0);
409 error(ERROR
, "Preprocessor control `%t' not yet implemented", tp
);
419 void *p
= malloc(size
);
422 error(FATAL
, "Out of memory from malloc");
433 error(enum errtype type
, char *string
,...)
442 fprintf(stderr
, "cpp: ");
443 for (s
= cursource
; s
; s
= s
->next
)
445 fprintf(stderr
, "%s:%d ", s
->filename
, s
->line
);
446 va_start(ap
, string
);
447 for (ep
= string
; *ep
; ep
++)
455 /* need a cast here since va_arg only
456 takes fully promoted types */
457 c
= (char) va_arg(ap
, int);
458 fprintf(stderr
, "%c", c
);
462 cp
= va_arg(ap
, char *);
463 fprintf(stderr
, "%s", cp
);
468 fprintf(stderr
, "%d", i
);
472 tp
= va_arg(ap
, Token
*);
473 fprintf(stderr
, "%.*s", tp
->len
, tp
->t
);
477 trp
= va_arg(ap
, Tokenrow
*);
478 for (tp
= trp
->tp
; tp
< trp
->lp
&& tp
->type
!= NL
; tp
++)
480 if (tp
> trp
->tp
&& tp
->wslen
)
482 fprintf(stderr
, "%.*s", tp
->len
, tp
->t
);