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 ************************************************************************/
37 /*ER evaluate macros to pDefOut */
40 * skipnl() skips over input text to the end of the line.
41 * skipws() skips over "whitespace" (spaces or tabs), but
42 * not skip over the end of the line. It skips over
43 * TOK_SEP, however (though that shouldn't happen).
44 * scanid() reads the next token (C identifier) into token[].
45 * The caller has already read the first character of
46 * the identifier. Unlike macroid(), the token is
48 * macroid() reads the next token (C identifier) into token[].
49 * If it is a #defined macro, it is expanded, and
50 * macroid() returns TRUE, otherwise, FALSE.
51 * catenate() Does the dirty work of token concatenation, TRUE if it did.
52 * scanstring() Reads a string from the input stream, calling
53 * a user-supplied function for each character.
54 * This function may be output() to write the
55 * string to the output file, or save() to save
56 * the string in the work buffer.
57 * scannumber() Reads a C numeric constant from the input stream,
58 * calling the user-supplied function for each
59 * character. (output() or save() as noted above.)
60 * save() Save one character in the work[] buffer.
61 * savestring() Saves a string in malloc() memory.
62 * getfile() Initialize a new FILEINFO structure, called when
63 * #include opens a new file, or a macro is to be
65 * getmem() Get a specified number of bytes from malloc memory.
66 * output() Write one character to stdout (calling PUTCHAR) --
67 * implemented as a function so its address may be
68 * passed to scanstring() and scannumber().
69 * lookid() Scans the next token (identifier) from the input
70 * stream. Looks for it in the #defined symbol table.
71 * Returns a pointer to the definition, if found, or NULL
72 * if not present. The identifier is stored in token[].
73 * defnedel() Define enter/delete subroutine. Updates the
75 * get() Read the next byte from the current input stream,
76 * handling end of (macro/file) input and embedded
77 * comments appropriately. Note that the global
78 * instring is -- essentially -- a parameter to get().
79 * cget() Like get(), but skip over TOK_SEP.
80 * unget() Push last gotten character back on the input stream.
81 * cerror(), cwarn(), cfatal(), cierror(), ciwarn()
82 * These routines format an print messages to the user.
83 * cerror & cwarn take a format and a single string argument.
84 * cierror & ciwarn take a format and a single int (char) argument.
85 * cfatal takes a format and a single string argument.
89 * This table must be rewritten for a non-Ascii machine.
91 * Note that several "non-visible" characters have special meaning:
92 * Hex 1D DEF_MAGIC -- a flag to prevent #define recursion.
93 * Hex 1E TOK_SEP -- a delimiter for token concatenation
94 * Hex 1F COM_SEP -- a zero-width whitespace for comment concatenation
96 #if TOK_SEP != 0x1E || COM_SEP != 0x1F || DEF_MAGIC != 0x1D
97 << error type table is
not correct
>>
108 char type
[256] = { /* Character type codes Hex */
109 END
, 000, 000, 000, 000, SPA
, 000, 000, /* 00 */
110 000, 000, 000, 000, 000, 000, 000, 000, /* 08 */
111 000, 000, 000, 000, 000, 000, 000, 000, /* 10 */
112 000, 000, 000, 000, 000, LET
, 000, SPA
, /* 18 */
113 000, 000, 000, 000, 000, 000, 000, 000, /* 20 */
114 000, 000, 000, 000, 000, 000, 000, 000, /* 28 */
115 000, 000, 000, 000, 000, 000, 000, 000, /* 30 */
116 000, 000, 000, 000, 000, 000, 000, 000, /* 38 */
117 SPA
, 000, 000, 000, 000, 000, 000, 000, /* 40 */
118 000, 000, 000, DOT
, OP_LT
,OP_LPA
,OP_ADD
, OP_OR
, /* 48 .<(+| */
119 OP_AND
, 000, 000, 000, 000, 000, 000, 000, /* 50 & */
120 000, 000,OP_NOT
, DOL
,OP_MUL
,OP_RPA
, 000,OP_XOR
, /* 58 !$*);^ */
121 OP_SUB
,OP_DIV
, 000, 000, 000, 000, 000, 000, /* 60 -/ */
122 000, 000, 000, 000,OP_MOD
, LET
, OP_GT
,OP_QUE
, /* 68 ,%_>? */
123 000, 000, 000, 000, 000, 000, 000, 000, /* 70 */
124 000, 000,OP_COL
, 000, 000, QUO
, OP_EQ
, QUO
, /* 78 `:#@'=" */
125 000, LET
, LET
, LET
, LET
, LET
, LET
, LET
, /* 80 abcdefg */
126 LET
, LET
, 000, 000, 000, 000, 000, 000, /* 88 hi */
127 000, LET
, LET
, LET
, LET
, LET
, LET
, LET
, /* 90 jklmnop */
128 LET
, LET
, 000, 000, 000, 000, 000, 000, /* 98 qr */
129 000,OP_NOT
, LET
, LET
, LET
, LET
, LET
, LET
, /* A0 ~stuvwx */
130 LET
, LET
, 000, 000, 000, 000, 000, 000, /* A8 yz [ */
131 000, 000, 000, 000, 000, 000, 000, 000, /* B0 */
132 000, 000, 000, 000, 000, 000, 000, 000, /* B8 ] */
133 000, LET
, LET
, LET
, LET
, LET
, LET
, LET
, /* C0 {ABCDEFG */
134 LET
, LET
, 000, 000, 000, 000, 000, 000, /* C8 HI */
135 000, LET
, LET
, LET
, LET
, LET
, LET
, LET
, /* D0 }JKLMNOP */
136 LET
, LET
, 000, 000, 000, 000, 000, 000, /* D8 QR */
137 BSH
, 000, LET
, LET
, LET
, LET
, LET
, LET
, /* E0 \ STUVWX */
138 LET
, LET
, 000, 000, 000, 000, 000, 000, /* E8 YZ */
139 DIG
, DIG
, DIG
, DIG
, DIG
, DIG
, DIG
, DIG
, /* F0 01234567 */
140 DIG
, DIG
, 000, 000, 000, 000, 000, 000, /* F8 89 */
145 char type
[256] = { /* Character type codes Hex */
146 END
, 000, 000, 000, 000, 000, 000, 000, /* 00 */
147 000, SPA
, 000, 000, 000, 000, 000, 000, /* 08 */
148 000, 000, 000, 000, 000, 000, 000, 000, /* 10 */
149 000, 000, 000, 000, 000, LET
, 000, SPA
, /* 18 */
150 SPA
,OP_NOT
, QUO
, 000, DOL
,OP_MOD
,OP_AND
, QUO
, /* 20 !"#$%&' */
151 OP_LPA
,OP_RPA
,OP_MUL
,OP_ADD
, 000,OP_SUB
, DOT
,OP_DIV
, /* 28 ()*+,-./ */
152 DIG
, DIG
, DIG
, DIG
, DIG
, DIG
, DIG
, DIG
, /* 30 01234567 */
153 DIG
, DIG
,OP_COL
, 000, OP_LT
, OP_EQ
, OP_GT
,OP_QUE
, /* 38 89:;<=>? */
154 000, LET
, LET
, LET
, LET
, LET
, LET
, LET
, /* 40 @ABCDEFG */
155 LET
, LET
, LET
, LET
, LET
, LET
, LET
, LET
, /* 48 HIJKLMNO */
156 LET
, LET
, LET
, LET
, LET
, LET
, LET
, LET
, /* 50 PQRSTUVW */
157 LET
, LET
, LET
, 000, BSH
, 000,OP_XOR
, LET
, /* 58 XYZ[\]^_ */
158 000, LET
, LET
, LET
, LET
, LET
, LET
, LET
, /* 60 `abcdefg */
159 LET
, LET
, LET
, LET
, LET
, LET
, LET
, LET
, /* 68 hijklmno */
160 LET
, LET
, LET
, LET
, LET
, LET
, LET
, LET
, /* 70 pqrstuvw */
161 LET
, LET
, LET
, 000, OP_OR
, 000,OP_NOT
, 000, /* 78 xyz{|}~ */
162 000, 000, 000, 000, 000, 000, 000, 000, /* 80 .. FF */
163 000, 000, 000, 000, 000, 000, 000, 000, /* 80 .. FF */
164 000, 000, 000, 000, 000, 000, 000, 000, /* 80 .. FF */
165 000, 000, 000, 000, 000, 000, 000, 000, /* 80 .. FF */
166 000, 000, 000, 000, 000, 000, 000, 000, /* 80 .. FF */
167 000, 000, 000, 000, 000, 000, 000, 000, /* 80 .. FF */
168 000, 000, 000, 000, 000, 000, 000, 000, /* 80 .. FF */
169 000, 000, 000, 000, 000, 000, 000, 000, /* 80 .. FF */
176 * C P P S y m b o l T a b l e s
180 * SBSIZE defines the number of hash-table slots for the symbol table.
181 * It must be a power of 2.
186 #define SBMASK (SBSIZE - 1)
187 #if (SBSIZE ^ SBMASK) != ((SBSIZE * 2) - 1)
188 << error
, SBSIZE must be a power of
2 >>
192 static DEFBUF
*symtab
[SBSIZE
]; /* Symbol table queue headers */
197 for( i
= 0; i
< SBSIZE
; i
++ )
205 * Skip to the end of the current input line.
210 do { /* Skip to newline */
212 } while (c
!= '\n' && c
!= EOF_CHAR
);
218 * Skip over whitespace
223 do { /* Skip whitespace */
225 #if COMMENT_INVISIBLE
226 } while (type
[c
] == SPA
|| c
== COM_SEP
);
228 } while (type
[c
] == SPA
);
235 * Get the next token (an id) into the token buffer.
236 * Note: this code is duplicated in lookid().
237 * Change one, change both.
242 if (c
== DEF_MAGIC
) /* Eat the magic token */
243 c
= get(); /* undefiner. */
246 if (bp
< &token
[IDMAX
]) /* token dim is IDMAX+1 */
249 } while (type
[c
] == LET
|| type
[c
] == DIG
);
257 * If c is a letter, scan the id. if it's #defined, expand it and scan
258 * the next character and try again.
260 * Else, return the character. If type[c] is a LET, the token is in token.
265 if (infile
!= NULL
&& infile
->fp
!= NULL
)
267 while (type
[c
] == LET
&& (dp
= lookid(c
)) != NULL
) {
277 * A token was just read (via macroid).
278 * If the next character is TOK_SEP, concatenate the next token
279 * return TRUE -- which should recall macroid after refreshing
280 * macroid's argument. If it is not TOK_SEP, unget() the character
285 register char *token1
;
288 if (get() != TOK_SEP
) { /* Token concatenation */
293 token1
= savestring(token
); /* Save first token */
294 c
= macroid(get()); /* Scan next token */
295 switch(type
[c
]) { /* What was it? */
296 case LET
: /* An identifier, ... */
297 if (strlen(token1
) + strlen(token
) >= NWORK
)
298 cfatal("work buffer overflow doing %s #", token1
);
299 sprintf(work
, "%s%s", token1
, token
);
302 case DIG
: /* A digit string */
303 strcpy(work
, token1
);
304 workp
= work
+ strlen(work
);
307 } while ((c
= get()) != TOK_SEP
);
309 * The trailing TOK_SEP is no longer needed.
314 default: /* An error, ... */
315 #if ! COMMENT_INVISIBLE
317 cierror("Strange character '%c' after #", c
);
319 cierror("Strange character (%d.) after #", c
);
321 strcpy(work
, token1
);
326 * work has the concatenated token and token1 has
327 * the first token (no longer needed). Unget the
328 * new (concatenated) token after freeing token1.
329 * Finally, setup to read the new token.
331 free(token1
); /* Free up memory */
332 ungetstring(work
); /* Unget the new thing, */
336 return (FALSE
); /* Not supported */
341 scanstring(int delim
,
343 void (*outfun
)( int ) /* BP */ /* Output function */
345 void (*outfun
)() /* BP */
349 * Scan off a string. Warning if terminated by newline or EOF.
350 * outfun() outputs the character -- to a buffer if in a macro.
351 * TRUE if ok, FALSE if error.
356 instring
= TRUE
; /* Don't strip comments */
358 while ((c
= get()) != delim
373 cerror("Unterminated string", NULLST
);
379 void scannumber(int c
,
381 register void (*outfun
)( int ) /* BP */ /* Output/store func */
383 register void (*outfun
)() /* BP */
387 * Process a number. We know that c is from 0 to 9 or dot.
388 * Algorithm from Dave Conroy's Decus C.
391 register int radix
; /* 8, 10, or 16 */
392 int expseen
; /* 'e' seen in floater */
393 int signseen
; /* '+' or '-' seen */
394 int octal89
; /* For bad octal test */
395 int dotflag
; /* TRUE if '.' was seen */
397 expseen
= FALSE
; /* No exponent seen yet */
398 signseen
= TRUE
; /* No +/- allowed yet */
399 octal89
= FALSE
; /* No bad octal yet */
400 radix
= 10; /* Assume decimal */
401 if ((dotflag
= (c
== '.')) != FALSE
) { /* . something? */
402 (*outfun
)('.'); /* Always out the dot */
403 if (type
[(c
= get())] != DIG
) { /* If not a float numb, */
404 unget(); /* Rescan strange char */
405 return; /* All done for now */
407 } /* End of float test */
408 else if (c
== '0') { /* Octal or hex? */
409 (*outfun
)(c
); /* Stuff initial zero */
410 radix
= 8; /* Assume it's octal */
411 c
= get(); /* Look for an 'x' */
412 if (c
== 'x' || c
== 'X') { /* Did we get one? */
413 radix
= 16; /* Remember new radix */
414 (*outfun
)(c
); /* Stuff the 'x' */
415 c
= get(); /* Get next character */
418 for (;;) { /* Process curr. char. */
420 * Note that this algorithm accepts "012e4" and "03.4"
421 * as legitimate floating-point numbers.
423 if (radix
!= 16 && (c
== 'e' || c
== 'E')) {
424 if (expseen
) /* Already saw 'E'? */
425 break; /* Exit loop, bad nbr. */
426 expseen
= TRUE
; /* Set exponent seen */
427 signseen
= FALSE
; /* We can read '+' now */
428 radix
= 10; /* Decimal exponent */
430 else if (radix
!= 16 && c
== '.') {
431 if (dotflag
) /* Saw dot already? */
432 break; /* Exit loop, two dots */
433 dotflag
= TRUE
; /* Remember the dot */
434 radix
= 10; /* Decimal fraction */
436 else if (c
== '+' || c
== '-') { /* 1.0e+10 */
437 if (signseen
) /* Sign in wrong place? */
438 break; /* Exit loop, not nbr. */
439 /* signseen = TRUE; */ /* Remember we saw it */
441 else { /* Check the digit */
443 case '8': case '9': /* Sometimes wrong */
444 octal89
= TRUE
; /* Do check later */
445 case '0': case '1': case '2': case '3':
446 case '4': case '5': case '6': case '7':
447 break; /* Always ok */
449 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
450 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
451 if (radix
== 16) /* Alpha's are ok only */
452 break; /* if reading hex. */
453 default: /* At number end */
454 goto done
; /* Break from for loop */
455 } /* End of switch */
456 } /* End general case */
457 (*outfun
)(c
); /* Accept the character */
458 signseen
= TRUE
; /* Don't read sign now */
459 c
= get(); /* Read another char */
460 } /* End of scan loop */
462 * When we break out of the scan loop, c contains the first
463 * character (maybe) not in the number. If the number is an
464 * integer, allow a trailing 'L' for long and/or a trailing 'U'
465 * for unsigned. If not those, push the trailing character back
466 * on the input stream. Floating point numbers accept a trailing
467 * 'L' for "long double".
469 done
: if (dotflag
|| expseen
) { /* Floating point? */
470 if (c
== 'l' || c
== 'L') {
472 c
= get(); /* Ungotten later */
475 else { /* Else it's an integer */
477 * We know that dotflag and expseen are both zero, now:
478 * dotflag signals "saw 'L'", and
479 * expseen signals "saw 'U'".
500 (*outfun
)(c
); /* Got 'L' or 'U'. */
501 c
= get(); /* Look at next, too. */
504 nomore
: unget(); /* Not part of a number */
505 if (octal89
&& radix
== 8)
506 cwarn("Illegal digit in octal number", NULLST
);
511 if (workp
>= &work
[NWORK
]) {
512 work
[NWORK
-1] = '\0';
513 cfatal("Work buffer overflow: %s", work
);
515 else *workp
++ = (char)c
;
519 savestring(char* text
)
521 * Store a string into free memory.
524 register char *result
;
526 result
= getmem(strlen(text
) + 1);
527 strcpy(result
, text
);
532 getfile(int bufsize
, char* name
)
534 * Common FILEINFO buffer initialization for a new file or macro.
537 register FILEINFO
*file
;
540 size
= strlen(name
); /* File/macro name */
541 file
= (FILEINFO
*) getmem(sizeof (FILEINFO
) + bufsize
+ size
);
542 file
->parent
= infile
; /* Chain files together */
543 file
->fp
= NULL
; /* No file yet */
544 file
->filename
= savestring(name
); /* Save file/macro name */
545 file
->progname
= NULL
; /* No #line seen yet */
546 file
->unrecur
= 0; /* No macro fixup */
547 file
->bptr
= file
->buffer
; /* Initialize line ptr */
548 file
->buffer
[0] = EOS
; /* Force first read */
549 file
->line
= 0; /* (Not used just yet) */
550 if (infile
!= NULL
) /* If #include file */
551 infile
->line
= line
; /* Save current line */
552 infile
= file
; /* New current file */
553 line
= 1; /* Note first line */
554 return (file
); /* All done. */
560 * Get a block of free memory.
563 register char *result
;
565 if ((result
= malloc((unsigned) size
)) == NULL
)
566 cfatal("Out of memory", NULLST
);
574 * Look for the next token in the symbol table. Returns token in "token".
575 * If found, returns the table pointer; Else returns NULL.
582 int isrecurse
; /* For #define foo foo */
586 if (0 != (isrecurse
= (c
== DEF_MAGIC
))) /* If recursive macro */
587 c
= get(); /* hack, skip DEF_MAGIC */
589 if (np
< &token
[IDMAX
]) { /* token dim is IDMAX+1 */
590 *np
++ = (char)c
; /* Store token byte */
591 nhash
+= c
; /* Update hash value */
593 c
= get(); /* And get another byte */
594 } while (type
[c
] == LET
|| type
[c
] == DIG
);
595 unget(); /* Rescan terminator */
596 *np
= EOS
; /* Terminate token */
597 if (isrecurse
) /* Recursive definition */
598 return (NULL
); /* undefined just now */
599 nhash
+= (np
- token
); /* Fix hash value */
600 dp
= symtab
[nhash
& SBMASK
]; /* Starting bucket */
601 while (dp
!= (DEFBUF
*) NULL
) { /* Search symbol table */
602 if (dp
->hash
== nhash
/* Fast precheck */
603 && (temp
= strcmp(dp
->name
, token
)) >= 0)
605 dp
= dp
->link
; /* Nope, try next one */
607 return ((temp
== 0) ? dp
: NULL
);
611 defendel(char* name
, int delete)
613 * Enter this name in the lookup table (delete = FALSE)
614 * or delete this name (delete = TRUE).
615 * Returns a pointer to the define block (delete = FALSE)
616 * Returns NULL if the symbol wasn't defined (delete = TRUE).
620 register DEFBUF
**prevp
;
626 for (nhash
= 0, np
= name
; *np
!= EOS
;)
630 prevp
= &symtab
[nhash
& SBMASK
];
631 while ((dp
= *prevp
) != (DEFBUF
*) NULL
) {
632 if (dp
->hash
== nhash
633 && (temp
= strcmp(dp
->name
, name
)) >= 0) {
635 dp
= NULL
; /* Not found */
637 *prevp
= dp
->link
; /* Found, unlink and */
638 if (dp
->repl
!= NULL
) /* Free the replacement */
639 free(dp
->repl
); /* if any, and then */
640 free((char *) dp
); /* Free the symbol */
647 dp
= (DEFBUF
*) getmem(sizeof (DEFBUF
) + size
);
653 strcpy(dp
->name
, name
);
658 #if OSL_DEBUG_LEVEL > 1
660 void dumpdef(char *why
)
663 register DEFBUF
**syp
;
664 FILE *pRememberOut
= NULL
;
666 if ( bDumpDefs
) /*ER */
668 pRememberOut
= pCppOut
;
671 fprintf( pCppOut
, "CPP symbol table dump %s\n", why
);
672 for (syp
= symtab
; syp
< &symtab
[SBSIZE
]; syp
++) {
673 if ((dp
= *syp
) != (DEFBUF
*) NULL
) {
674 fprintf( pCppOut
, "symtab[%d]\n", (syp
- symtab
));
676 dumpadef((char *) NULL
, dp
);
677 } while ((dp
= dp
->link
) != (DEFBUF
*) NULL
);
682 fprintf( pCppOut
, "\n");
683 pCppOut
= pRememberOut
;
687 void dumpadef(char *why
, register DEFBUF
*dp
)
691 FILE *pRememberOut
= NULL
;
693 /*ER dump #define's to pDefOut */
696 pRememberOut
= pCppOut
;
699 fprintf( pCppOut
, " \"%s\" [%d]", dp
->name
, dp
->nargs
);
701 fprintf( pCppOut
, " (%s)", why
);
702 if (dp
->repl
!= NULL
) {
703 fprintf( pCppOut
, " => ");
704 for (cp
= dp
->repl
; (c
= *cp
++ & 0xFF) != EOS
;) {
708 if( c
== EOS
) break;
709 fprintf( pCppOut
, "<%%%d>", c
- MAC_PARM
);
712 if (c
>= MAC_PARM
&& c
<= (MAC_PARM
+ PAR_MAC
))
713 fprintf( pCppOut
, "<%%%d>", c
- MAC_PARM
);
715 else if (isprint(c
) || c
== '\n' || c
== '\t')
718 fprintf( pCppOut
, "<^%c>", c
+ '@');
720 fprintf( pCppOut
, "<\\0%o>", c
);
722 /*ER evaluate macros to pDefOut */
724 if ( bDumpDefs
&& !bIsInEval
&& dp
->nargs
<= 0 )
726 FILEINFO
*infileSave
= infile
;
727 char *tokenSave
= savestring( token
);
728 char *workSave
= savestring( work
);
730 int wronglineSave
= wrongline
;
731 int recursionSave
= recursion
;
736 infile
= NULL
; /* start from scrap */
742 file
= getfile( strlen( dp
->repl
), dp
->name
);
743 strcpy( file
->buffer
, dp
->repl
);
744 fprintf( pCppOut
, " ===> ");
746 cppmain(); /* get() frees also *file */
748 if ( 0 == evaluate( EvalBuf
, &valEval
) )
751 if ( valEval
!= (EVALTYPE
)((long)valEval
) )
752 fprintf( pCppOut
, " ==eval=> %f", valEval
);
755 fprintf( pCppOut
, " ==eval=> %ld", (long)valEval
);
757 recursion
= recursionSave
;
758 wrongline
= wronglineSave
;
760 strcpy( work
, workSave
);
762 strcpy( token
, tokenSave
);
770 fprintf( pCppOut
, ", no replacement.");
774 pCppOut
= pRememberOut
;
785 * Return the next character from a macro or the current file.
786 * Handle end of file from #include files.
790 register FILEINFO
*file
;
791 register int popped
; /* Recursion fixup */
795 if ((file
= infile
) == NULL
)
799 fprintf( pCppOut
, "get(%s), recursion %d, line %d, bptr = %d, buffer \"%s\"\n",
800 file
->filename
, recursion
, line
,
801 file
->bptr
- file
->buffer
, file
->buffer
);
804 * Read a character from the current input line or macro.
805 * At EOS, either finish the current macro (freeing temp.
806 * storage) or read another line from the current input file.
807 * At EOF, exit the current file (#include) or, at EOF from
808 * the cpp input file, return EOF_CHAR to finish processing.
810 if ((c
= *file
->bptr
++ & 0xFF) == EOS
) {
812 * Nothing in current line or macro. Get next line (if
813 * input from a file), or do end of file/macro processing.
814 * In the latter case, jump back to restart from the top.
816 if (file
->fp
== NULL
) { /* NULL if macro */
818 recursion
-= file
->unrecur
;
821 infile
= file
->parent
; /* Unwind file chain */
823 else { /* Else get from a file */
824 if ((file
->bptr
= fgets(file
->buffer
, NBUFF
, file
->fp
))
826 #if OSL_DEBUG_LEVEL > 1
827 if (debug
> 1) { /* Dump it to stdout */
828 fprintf( pCppOut
, "\n#line %d (%s), %s",
829 line
, file
->filename
, file
->buffer
);
832 goto newline
; /* process the line */
835 if( file
->fp
!= stdin
)
836 fclose(file
->fp
); /* Close finished file */
837 if ((infile
= file
->parent
) != NULL
) {
839 * There is an "ungotten" newline in the current
840 * infile buffer (set there by doinclude() in
841 * cpp1.c). Thus, we know that the mainline code
842 * is skipping over blank lines and will do a
843 * #line at its convenience.
845 wrongline
= TRUE
; /* Need a #line now */
850 * Free up space used by the (finished) file or macro and
851 * restart input from the parent file/macro, if any.
853 free(file
->filename
); /* Free name and */
854 if (file
->progname
!= NULL
) /* if a #line was seen, */
855 free(file
->progname
); /* free it, too. */
856 free((char *) file
); /* Free file space */
857 if (infile
== NULL
) /* If at end of file */
858 return (EOF_CHAR
); /* Return end of file */
859 line
= infile
->line
; /* Reset line number */
860 goto get_from_file
; /* Get from the top. */
863 * Common processing for the new character.
865 if (c
== DEF_MAGIC
&& file
->fp
!= NULL
) /* Don't allow delete */
866 goto newline
; /* from a file */
867 if (file
->parent
!= NULL
) { /* Macro or #include */
869 file
->parent
->unrecur
+= popped
;
871 recursion
-= file
->parent
->unrecur
;
874 file
->parent
->unrecur
= 0;
877 #if (HOST == SYS_UNIX)
878 /*ER*/ if (c
== '\r')
879 /*ER*/ return get(); /* DOS fuck */
881 if (c
== '\n') /* Maintain current */
882 ++line
; /* line counter */
883 if (instring
) /* Strings just return */
884 return (c
); /* the character. */
885 else if (c
== '/') { /* Comment? */
886 instring
= TRUE
; /* So get() won't loop */
889 /*MM*/ if ((c
!= '*') && (c
!= '/')) { /* Next byte '*'? */
890 instring
= FALSE
; /* Nope, no comment */
891 unget(); /* Push the char. back */
892 return ('/'); /* Return the slash */
894 if (keepcomments
) { /* If writing comments */
895 PUTCHAR('/'); /* Write out the */
897 /*MM*/ if( '*' == c
)
903 /*MM*/ if( '*' == c
){
904 for (;;) { /* Eat a comment */
906 test
: if (keepcomments
&& c
!= EOF_CHAR
)
910 cerror("EOF in comment", NULLST
);
914 if ((c
= get()) != '*') /* Don't let comments */
915 goto test
; /* Nest. */
916 #ifdef STRICT_COMMENTS
917 cwarn("Nested comments", NULLST
);
919 /* Fall into * stuff */
921 if ((c
= get()) != '/') /* If comment doesn't */
922 goto test
; /* end, look at next */
923 instring
= FALSE
; /* End of comment, */
924 if (keepcomments
) { /* Put out the comment */
925 cput(c
); /* terminator, too */
928 * A comment is syntactically "whitespace" --
929 * however, there are certain strange sequences
931 * #define foo(x) (something)
932 * foo|* comment *|(123)
934 * where just returning space (or COM_SEP) will cause
935 * problems. This can be "fixed" by overwriting the
936 * '/' in the input line buffer with ' ' (or COM_SEP)
937 * but that may mess up an error message.
938 * So, we peek ahead -- if the next character is
939 * "whitespace" we just get another character, if not,
940 * we modify the buffer. All in the name of purity.
942 if (*file
->bptr
== '\n'
943 || type
[*file
->bptr
& 0xFF] == SPA
)
945 #if COMMENT_INVISIBLE
947 * Return magic (old-fashioned) syntactic space.
949 return ((file
->bptr
[-1] = COM_SEP
));
951 return ((file
->bptr
[-1] = ' '));
954 case '\n': /* we'll need a #line */
956 wrongline
= TRUE
; /* later... */
957 default: /* Anything else is */
958 break; /* Just a character */
960 } /* End comment loop */
962 else{ /* c++ comment */
964 for (;;) { /* Eat a comment */
966 if (keepcomments
&& c
!= EOF_CHAR
)
970 else if( '\n' == c
){
971 instring
= FALSE
; /* End of comment, */
976 } /* End if in comment */
977 else if (!inmacro
&& c
== '\\') { /* If backslash, peek */
978 if ((c
= get()) == '\n') { /* for a <nl>. If so, */
982 else { /* Backslash anything */
983 unget(); /* Get it later */
984 return ('\\'); /* Return the backslash */
987 else if (c
== '\f' || c
== VT
) /* Form Feed, Vertical */
988 c
= ' '; /* Tab are whitespace */
989 else if (c
== 0xef) /* eat up UTF-8 BOM */
991 if((c
= get()) == 0xbb)
993 if((c
= get()) == 0xbf)
1011 return (c
); /* Just return the char */
1016 * Backup the pointer to reread the last character. Fatal error
1017 * (code bug) if we backup too far. unget() may be called,
1018 * without problems, at end of file. Only one character may
1019 * be ungotten. If you need to unget more, call ungetstring().
1022 register FILEINFO
*file
;
1024 if ((file
= infile
) == NULL
)
1025 return; /* Unget after EOF */
1026 if (--file
->bptr
< file
->buffer
)
1027 cfatal("Too much pushback", NULLST
);
1028 if (*file
->bptr
== '\n') /* Ungetting a newline? */
1029 --line
; /* Unget the line number, too */
1032 void ungetstring(char* text
)
1034 * Push a string back on the input stream. This is done by treating
1035 * the text as if it were a macro.
1038 register FILEINFO
*file
;
1039 #ifndef ZTC /* BP */
1040 extern FILEINFO
*getfile();
1042 file
= getfile(strlen(text
) + 1, "");
1043 strcpy(file
->buffer
, text
);
1049 * Get one character, absorb "funny space" after comments or
1050 * token concatenation
1057 #if COMMENT_INVISIBLE
1058 } while (c
== TOK_SEP
|| c
== COM_SEP
);
1060 } while (c
== TOK_SEP
);
1066 * Error messages and other hacks. The first byte of severity
1067 * is 'S' for string arguments and 'I' for int arguments. This
1068 * is needed for portability with machines that have int's that
1069 * are shorter than char *'s.
1072 static void domsg(char* severity
, char* format
, void* arg
)
1074 * Print filenames, macro names, and line numbers for error messages.
1078 register FILEINFO
*file
;
1080 fprintf(stderr
, "%sline %d, %s: ", MSG_PREFIX
, line
, &severity
[1]);
1081 if (*severity
== 'S')
1082 fprintf(stderr
, format
, (char *)arg
);
1084 fprintf(stderr
, format
, *((int *)arg
) );
1086 if ((file
= infile
) == NULL
)
1087 return; /* At end of file */
1088 if (file
->fp
!= NULL
) {
1089 tp
= file
->buffer
; /* Print current file */
1090 fprintf(stderr
, "%s", tp
); /* name, making sure */
1091 if (tp
[strlen(tp
) - 1] != '\n') /* there's a newline */
1094 while ((file
= file
->parent
) != NULL
) { /* Print #includes, too */
1095 if (file
->fp
== NULL
)
1096 fprintf(stderr
, "from macro %s\n", file
->filename
);
1099 fprintf(stderr
, "from file %s, line %d:\n%s",
1100 (file
->progname
!= NULL
)
1101 ? file
->progname
: file
->filename
,
1103 if (tp
[strlen(tp
) - 1] != '\n')
1109 void cerror(char* format
, char* sarg
)
1111 * Print a normal error message, string argument.
1114 domsg("SError", format
, sarg
);
1118 void cierror(char* format
, int narg
)
1120 * Print a normal error message, numeric argument.
1123 domsg("IError", format
, &narg
);
1127 void cfatal(char* format
, char* sarg
)
1132 domsg("SFatal error", format
, sarg
);
1136 void cwarn(char* format
, char* sarg
)
1138 * A non-fatal error, string argument.
1141 domsg("SWarning", format
, sarg
);
1144 void ciwarn(char* format
, int narg
)
1146 * A non-fatal error, numeric argument.
1149 domsg("IWarning", format
, &narg
);