Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #define NOMAIN
21 :
22 : #include <stdio.h>
23 : #include <ctype.h>
24 : #include "cppdef.h"
25 : #include "cpp.h"
26 :
27 : FILE *pCppOut = NULL;
28 : FILE *pCppIn = NULL;
29 :
30 : #if OSL_DEBUG_LEVEL > 1
31 : FILE *pDefOut = NULL; /* ER evtl. #define's dump */
32 : #endif
33 :
34 : #ifdef B200
35 : /* einzige Moeglichkeit unter BC Stack und Heap festzusetzen */
36 : extern unsigned _stklen = 24000;
37 : extern unsigned _heaplen = 30000;
38 : #endif
39 :
40 :
41 :
42 : /*
43 : * Commonly used global variables:
44 : * line is the current input line number.
45 : * wrongline is set in many places when the actual output
46 : * line is out of sync with the numbering, e.g,
47 : * when expanding a macro with an embedded newline.
48 : *
49 : * token holds the last identifier scanned (which might
50 : * be a candidate for macro expansion).
51 : * errors is the running cpp error counter.
52 : * infile is the head of a linked list of input files (extended by
53 : * #include and macros being expanded). infile always points
54 : * to the current file/macro. infile->parent to the includer,
55 : * etc. infile->fd is NULL if this input stream is a macro.
56 : */
57 : int line; /* Current line number */
58 : int wrongline; /* Force #line to compiler */
59 : char token[IDMAX + 1]; /* Current input token */
60 : int errors; /* cpp error counter */
61 : FILEINFO *infile = NULL; /* Current input file */
62 : #if OSL_DEBUG_LEVEL > 1
63 : int debug; /* TRUE if debugging now */
64 : int bDumpDefs; /* TRUE if #define's dump req. */
65 : #ifdef EVALDEFS
66 : int bIsInEval; /* TRUE if #define eval now */
67 : char EvalBuf[NEVALBUF + 1]; /* evaluation buffer */
68 : int nEvalOff = 0; /* offset to free buffer pos */
69 : #endif
70 : #endif
71 : /*
72 : * This counter is incremented when a macro expansion is initiated.
73 : * If it exceeds a built-in value, the expansion stops -- this tests
74 : * for a runaway condition:
75 : * #define X Y
76 : * #define Y X
77 : * X
78 : * This can be disabled by falsifying rec_recover. (Nothing does this
79 : * currently: it is a hook for an eventual invocation flag.)
80 : */
81 : int recursion; /* Infinite recursion counter */
82 : int rec_recover = TRUE; /* Unwind recursive macros */
83 :
84 : /*
85 : * instring is set TRUE when a string is scanned. It modifies the
86 : * behavior of the "get next character" routine, causing all characters
87 : * to be passed to the caller (except <DEF_MAGIC>). Note especially that
88 : * comments and \<newline> are not removed from the source. (This
89 : * prevents cpp output lines from being arbitrarily long).
90 : *
91 : * inmacro is set by #define -- it absorbs comments and converts
92 : * form-feed and vertical-tab to space, but returns \<newline>
93 : * to the caller. Strictly speaking, this is a bug as \<newline>
94 : * shouldn't delimit tokens, but we'll worry about that some other
95 : * time -- it is more important to prevent infinitly long output lines.
96 : *
97 : * instring and inmarcor are parameters to the get() routine which
98 : * were made global for speed.
99 : */
100 : int instring = FALSE; /* TRUE if scanning string */
101 : int inmacro = FALSE; /* TRUE if #defining a macro */
102 :
103 : /*
104 : * work[] and workp are used to store one piece of text in a temporay
105 : * buffer. To initialize storage, set workp = work. To store one
106 : * character, call save(c); (This will fatally exit if there isn't
107 : * room.) To terminate the string, call save(EOS). Note that
108 : * the work buffer is used by several subroutines -- be sure your
109 : * data won't be overwritten. The extra byte in the allocation is
110 : * needed for string formal replacement.
111 : */
112 : char work[NWORK + 1]; /* Work buffer */
113 : char *workp; /* Work buffer pointer */
114 :
115 : /*
116 : * keepcomments is set TRUE by the -C option. If TRUE, comments
117 : * are written directly to the output stream. This is needed if
118 : * the output from cpp is to be passed to lint (which uses commands
119 : * embedded in comments). cflag contains the permanent state of the
120 : * -C flag. keepcomments is always falsified when processing #control
121 : * commands and when compilation is supressed by a false #if
122 : *
123 : * If eflag is set, CPP returns "success" even if non-fatal errors
124 : * were detected.
125 : *
126 : * If nflag is non-zero, no symbols are predefined except __LINE__.
127 : * __FILE__, and __DATE__. If nflag > 1, absolutely no symbols
128 : * are predefined.
129 : */
130 : int keepcomments = FALSE; /* Write out comments flag */
131 : int cflag = FALSE; /* -C option (keep comments) */
132 : int eflag = FALSE; /* -E option (never fail) */
133 : int nflag = 0; /* -N option (no predefines) */
134 :
135 : /*
136 : * ifstack[] holds information about nested #if's. It is always
137 : * accessed via *ifptr. The information is as follows:
138 : * WAS_COMPILING state of compiling flag at outer level.
139 : * ELSE_SEEN set TRUE when #else seen to prevent 2nd #else.
140 : * TRUE_SEEN set TRUE when #if or #elif succeeds
141 : * ifstack[0] holds the compiling flag. It is TRUE if compilation
142 : * is currently enabled. Note that this must be initialized TRUE.
143 : */
144 : char ifstack[BLK_NEST] = { TRUE }; /* #if information */
145 : char *ifptr = ifstack; /* -> current ifstack[] */
146 :
147 : /*
148 : * incdir[] stores the -i directories (and the system-specific
149 : * #include <...> directories.
150 : */
151 : char *incdir[NINCLUDE]; /* -i directories */
152 : char **incend = incdir; /* -> free space in incdir[] */
153 :
154 : /*
155 : * This is the table used to predefine target machine and operating
156 : * system designators. It may need hacking for specific circumstances.
157 : * Note: it is not clear that this is part of the Ansi Standard.
158 : * The -N option supresses preset definitions.
159 : */
160 : char *preset[] = { /* names defined at cpp start */
161 : #ifdef MACHINE
162 : MACHINE,
163 : #endif
164 : #ifdef SYSTEM
165 : SYSTEM,
166 : #endif
167 : #ifdef COMPILER
168 : COMPILER,
169 : #endif
170 : #if OSL_DEBUG_LEVEL > 1
171 : "decus_cpp", /* Ourselves! */
172 : #endif
173 : NULL /* Must be last */
174 : };
175 :
176 : /*
177 : * The value of these predefined symbols must be recomputed whenever
178 : * they are evaluated. The order must not be changed.
179 : */
180 : char *magic[] = { /* Note: order is important */
181 : "__LINE__",
182 : "__FILE__",
183 : NULL /* Must be last */
184 : };
185 :
186 : static char *sharpfilename = NULL;
187 :
188 : int nRunde = 0;
189 :
190 630 : void InitCpp1()
191 : {
192 : int i;
193 : /* in der LIB-Version muessen alle Variablen initialisiert werden */
194 :
195 630 : line = wrongline = errors = recursion = 0;
196 80640 : for( i = 0; i < IDMAX; i++ )
197 80010 : token[ i ] = 0;
198 :
199 80640630 : for( i = 0; i < NWORK; i++ )
200 80640000 : work[ i ] = 0;
201 :
202 63630 : for( i = 0; i < NINCLUDE; i++ )
203 63000 : incdir[ i ] = NULL;
204 :
205 630 : workp = NULL;
206 20790 : for( i = 0; i < BLK_NEST; i++ )
207 20160 : ifstack[ i ] = TRUE;
208 630 : ifptr = ifstack;
209 :
210 630 : pCppOut = stdout;
211 630 : pCppIn = stdin;
212 : #if OSL_DEBUG_LEVEL > 1
213 : debug = 0;
214 : bDumpDefs = 0;
215 : pDefOut = stdout;
216 : #ifdef EVALDEFS
217 : bIsInEval = 0;
218 : for( i = 0; i < NEVALBUF; i++ )
219 : EvalBuf[ i ] = 0;
220 : nEvalOff = 0;
221 : #endif
222 : #endif
223 630 : rec_recover = TRUE;
224 630 : infile = NULL;
225 630 : instring = inmacro = keepcomments = cflag = eflag = FALSE;
226 630 : nflag = 0;
227 630 : incend = incdir;
228 630 : sharpfilename = NULL;
229 630 : }
230 :
231 630 : int MAIN(int argc, char** argv)
232 : {
233 : register int i;
234 : char **useargv, **pfargv;
235 :
236 :
237 630 : if( nRunde == 0 )
238 : {
239 630 : pCppIn = stdin;
240 630 : pCppOut = stdout;
241 : }
242 :
243 630 : nRunde++;
244 630 : InitCpp1();
245 630 : InitCpp2();
246 630 : InitCpp3();
247 630 : InitCpp4();
248 630 : InitCpp5();
249 630 : InitCpp6();
250 :
251 : #if HOST == SYS_VMS
252 : argc = getredirection(argc, argv); /* vms >file and <file */
253 : #endif
254 630 : initdefines(); /* O.S. specific def's */
255 630 : if ( argv[argc-1][0] == '@' )
256 : {
257 630 : i = readoptions( argv[1], &pfargv ); /* Command file */
258 630 : useargv=pfargv;
259 : }
260 : else
261 : {
262 0 : i = dooptions(argc, argv); /* Command line -flags */
263 0 : useargv=argv;
264 : }
265 630 : switch (i) {
266 : #if OSL_DEBUG_LEVEL > 1
267 : case 4:
268 : if ( bDumpDefs )
269 : {
270 : /*
271 : * Get defBase file, "-" means use stdout.
272 : */
273 : if (!streq(useargv[3], "-")) {
274 : #if HOST == SYS_VMS
275 : /*
276 : * On vms, reopen stdout with "vanilla rms" attributes.
277 : */
278 : if ((i = creat(useargv[3], 0, "rat=cr", "rfm=var")) == -1
279 : || dup2(i, fileno(stdout)) == -1) {
280 : #else
281 : pDefOut = fopen( useargv[3], "w" );
282 : if( pDefOut == NULL ) {
283 : #endif
284 : perror(useargv[3]);
285 : cerror("Can't open output file \"%s\"", useargv[3]);
286 : exit(IO_ERROR);
287 : }
288 : } /* Continue by opening output */
289 : }
290 : #endif
291 : case 3:
292 : /*
293 : * Get output file, "-" means use stdout.
294 : */
295 630 : if (!streq(useargv[2], "-")) {
296 : #if HOST == SYS_VMS
297 : /*
298 : * On vms, reopen stdout with "vanilla rms" attributes.
299 : */
300 : if ((i = creat(useargv[2], 0, "rat=cr", "rfm=var")) == -1
301 : || dup2(i, fileno(stdout)) == -1) {
302 : #else
303 630 : pCppOut = fopen( useargv[2], "w" );
304 630 : if( pCppOut == NULL ) {
305 : #endif
306 0 : perror(useargv[2]);
307 0 : cerror("Can't open output file \"%s\"", useargv[2]);
308 0 : exit(IO_ERROR);
309 : }
310 : } /* Continue by opening input */
311 : case 2: /* One file -> stdin */
312 : /*
313 : * Open input file, "-" means use stdin.
314 : */
315 630 : if (!streq(useargv[1], "-")) {
316 630 : pCppIn = fopen( useargv[1], "r" );
317 630 : if( pCppIn == NULL) {
318 0 : perror(useargv[1]);
319 0 : cerror("Can't open input file \"%s\"", useargv[1]);
320 0 : exit(IO_ERROR);
321 : }
322 630 : strcpy(work, useargv[1]); /* Remember input filename */
323 630 : break;
324 : } /* Else, just get stdin */
325 : case 0: /* No args? */
326 : case 1: /* No files, stdin -> stdout */
327 : #if (HOST == SYS_UNIX) || (HOST == SYS_UNKNOWN)
328 0 : work[0] = EOS; /* Unix can't find stdin name */
329 : #else
330 : fgetname(stdin, work); /* Vax-11C, Decus C know name */
331 : #endif
332 0 : break;
333 :
334 : default:
335 0 : exit(IO_ERROR); /* Can't happen */
336 : }
337 :
338 630 : setincdirs(); /* Setup -I include directories */
339 630 : addfile( pCppIn, work); /* "open" main input file */
340 : #if OSL_DEBUG_LEVEL > 1
341 : if (debug > 0 || bDumpDefs)
342 : dumpdef("preset #define symbols");
343 : #endif
344 630 : if( pCppIn != stdin )
345 630 : rewind( pCppIn );
346 :
347 630 : cppmain(); /* Process main file */
348 :
349 630 : if ((i = (ifptr - &ifstack[0])) != 0) {
350 : #if OLD_PREPROCESSOR
351 : ciwarn("Inside #ifdef block at end of input, depth = %d", i);
352 : #else
353 0 : cierror("Inside #ifdef block at end of input, depth = %d", i);
354 : #endif
355 : }
356 : #if OSL_DEBUG_LEVEL > 1
357 : if( pDefOut != stdout && pDefOut != stderr )
358 : fclose( pDefOut );
359 : #endif
360 630 : if( pCppOut != stdout && pCppOut != stderr )
361 630 : fclose( pCppOut );
362 :
363 630 : if (errors > 0) {
364 0 : fprintf(stderr, (errors == 1)
365 : ? "%d error in preprocessor\n"
366 : : "%d errors in preprocessor\n", errors);
367 0 : if (!eflag)
368 0 : exit(IO_ERROR);
369 : }
370 : #ifdef NOMAIN /* BP */ /* kein exit im der LIB-Version */
371 630 : return( IO_NORMAL );
372 : #else
373 : exit(IO_NORMAL); /* No errors or -E option set */
374 : #endif
375 :
376 : }
377 :
378 : FILE_LOCAL
379 630 : void cppmain()
380 : /*
381 : * Main process for cpp -- copies tokens from the current input
382 : * stream (main file, include file, or a macro) to the output
383 : * file.
384 : */
385 : {
386 : register int c; /* Current character */
387 : register int counter; /* newlines and spaces */
388 :
389 : /*
390 : * Explicitly output a #line at the start of cpp output so
391 : * that lint (etc.) knows the name of the original source
392 : * file. If we don't do this explicitly, we may get
393 : * the name of the first #include file instead.
394 : * We also seem to need a blank line following that first #line.
395 : */
396 : #ifdef EVALDEFS
397 : if ( !bIsInEval )
398 : #endif
399 : {
400 630 : sharp();
401 630 : PUTCHAR('\n');
402 : }
403 : /*
404 : * This loop is started "from the top" at the beginning of each line
405 : * wrongline is set TRUE in many places if it is necessary to write
406 : * a #line record. (But we don't write them when expanding macros.)
407 : *
408 : * The counter variable has two different uses: at
409 : * the start of a line, it counts the number of blank lines that
410 : * have been skipped over. These are then either output via
411 : * #line records or by outputting explicit blank lines.
412 : * When expanding tokens within a line, the counter remembers
413 : * whether a blank/tab has been output. These are dropped
414 : * at the end of the line, and replaced by a single blank
415 : * within lines.
416 : */
417 : for (;;) {
418 141144 : counter = 0; /* Count empty lines */
419 : for (;;) { /* For each line, ... */
420 2250364 : while (type[(c = get())] == SPA) /* Skip leading blanks */
421 : ; /* in this line. */
422 2250364 : if (c == '\n') /* If line's all blank, */
423 622457 : ++counter; /* Do nothing now */
424 1627907 : else if (c == '#') { /* Is 1st non-space '#' */
425 1486300 : keepcomments = FALSE; /* Don't pass comments */
426 1486300 : counter = control(counter); /* Yes, do a #command */
427 1486300 : keepcomments = (cflag && compiling);
428 : }
429 141607 : else if (c == EOF_CHAR) /* At end of file? */
430 : {
431 630 : break;
432 : }
433 140977 : else if (!compiling) { /* #ifdef false? */
434 463 : skipnl(); /* Skip to newline */
435 463 : counter++; /* Count it, too. */
436 : }
437 : else {
438 140514 : break; /* Actual token */
439 : }
440 2109220 : }
441 141144 : if (c == EOF_CHAR) /* Exit process at */
442 630 : break; /* End of file */
443 : /*
444 : * If the loop didn't terminate because of end of file, we
445 : * know there is a token to compile. First, clean up after
446 : * absorbing newlines. counter has the number we skipped.
447 : */
448 140514 : if ((wrongline && infile->fp != NULL) || counter > 4)
449 2214 : sharp(); /* Output # line number */
450 : else { /* If just a few, stuff */
451 286636 : while (--counter >= 0) /* them out ourselves */
452 10036 : PUTCHAR('\n');
453 : }
454 : /*
455 : * Process each token on this line.
456 : */
457 140514 : unget(); /* Reread the char. */
458 : for (;;) { /* For the whole line, */
459 : do { /* Token concat. loop */
460 2433925 : for (counter = 0; type[(c = get())] == SPA;) {
461 : #if COMMENT_INVISIBLE
462 : if (c != COM_SEP)
463 : counter++;
464 : #else
465 487437 : counter++; /* Skip over blanks */
466 : #endif
467 : }
468 973244 : if (c == EOF_CHAR || c == '\n')
469 : goto end_line; /* Exit line loop */
470 832730 : else if (counter > 0) /* If we got any spaces */
471 457250 : PUTCHAR(' '); /* Output one space */
472 832730 : c = macroid(c); /* Grab the token */
473 832730 : } while (type[c] == LET && catenate());
474 832634 : if (c == EOF_CHAR || c == '\n') /* From macro exp error */
475 : goto end_line; /* Exit line loop */
476 832634 : switch (type[c]) {
477 : case LET:
478 173703 : fputs(token, pCppOut); /* Quite ordinary token */
479 : #ifdef EVALDEFS
480 : {
481 : int len;
482 : if ( bIsInEval
483 : && nEvalOff + (len=strlen(token)) < NEVALBUF )
484 : {
485 : strcpy( &EvalBuf[nEvalOff], token );
486 : nEvalOff += len;
487 : }
488 : }
489 : #endif
490 173703 : break;
491 :
492 :
493 : case DIG: /* Output a number */
494 : case DOT: /* Dot may begin floats */
495 : #ifdef EVALDEFS
496 : if ( bIsInEval )
497 : scannumber(c, outputEval);
498 : else
499 : scannumber(c, output);
500 : #else
501 118222 : scannumber(c, output);
502 : #endif
503 118222 : break;
504 :
505 : case QUO: /* char or string const */
506 34112 : scanstring(c, output); /* Copy it to output */
507 34112 : break;
508 :
509 : default: /* Some other character */
510 506597 : cput(c); /* Just output it */
511 : #ifdef EVALDEFS
512 : if ( bIsInEval && nEvalOff < NEVALBUF )
513 : EvalBuf[nEvalOff++] = c;
514 : #endif
515 506597 : break;
516 : } /* Switch ends */
517 832634 : } /* Line for loop */
518 140514 : end_line: if (c == '\n') { /* Compiling at EOL? */
519 140513 : PUTCHAR('\n'); /* Output newline, if */
520 140513 : if (infile->fp == NULL) /* Expanding a macro, */
521 0 : wrongline = TRUE; /* Output # line later */
522 : }
523 140514 : } /* Continue until EOF */
524 : #ifdef EVALDEFS
525 : if ( bIsInEval )
526 : EvalBuf[nEvalOff++] = '\0';
527 : #endif
528 630 : }
529 :
530 1090203 : void output(int c)
531 : /*
532 : * Output one character to stdout -- output() is passed as an
533 : * argument to scanstring()
534 : */
535 : {
536 : #if COMMENT_INVISIBLE
537 : if (c != TOK_SEP && c != COM_SEP)
538 : #else
539 1090203 : if (c != TOK_SEP)
540 : #endif
541 1090203 : PUTCHAR(c);
542 1090203 : }
543 :
544 : #ifdef EVALDEFS
545 : outputEval(c)
546 : int c;
547 : /*
548 : * Output one character to stdout -- output() is passed as an
549 : * argument to scanstring()
550 : */
551 : {
552 : #if COMMENT_INVISIBLE
553 : if (c != TOK_SEP && c != COM_SEP)
554 : #else
555 : if (c != TOK_SEP)
556 : #endif
557 : {
558 : PUTCHAR(c);
559 : if ( bIsInEval && nEvalOff < NEVALBUF )
560 : EvalBuf[nEvalOff++] = c;
561 : }
562 : }
563 : #endif
564 :
565 :
566 : FILE_LOCAL
567 2844 : void sharp()
568 : /*
569 : * Output a line number line.
570 : */
571 : {
572 : register char *name;
573 :
574 2844 : if (keepcomments) /* Make sure # comes on */
575 0 : PUTCHAR('\n'); /* a fresh, new line. */
576 2844 : fprintf( pCppOut, "#%s %d", LINE_PREFIX, line);
577 2844 : if (infile->fp != NULL) {
578 5688 : name = (infile->progname != NULL)
579 2844 : ? infile->progname : infile->filename;
580 2844 : if (sharpfilename == NULL
581 2214 : || (sharpfilename != NULL && !streq(name, sharpfilename)) ) {
582 632 : if (sharpfilename != NULL)
583 2 : free(sharpfilename);
584 632 : sharpfilename = savestring(name);
585 632 : fprintf( pCppOut, " \"%s\"", name);
586 : }
587 : }
588 2844 : PUTCHAR('\n');
589 2844 : wrongline = FALSE;
590 2844 : }
591 :
592 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|