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 .
20 #include <sal/types.h>
27 /*ER evaluate macros to pDefOut */
30 * skipnl() skips over input text to the end of the line.
31 * skipws() skips over "whitespace" (spaces or tabs), but
32 * not skip over the end of the line. It skips over
33 * TOK_SEP, however (though that shouldn't happen).
34 * scanid() reads the next token (C identifier) into token[].
35 * The caller has already read the first character of
36 * the identifier. Unlike macroid(), the token is
38 * macroid() reads the next token (C identifier) into token[].
39 * If it is a #defined macro, it is expanded, and
40 * macroid() returns TRUE, otherwise, FALSE.
41 * catenate() Does the dirty work of token concatenation, TRUE if it did.
42 * scanstring() Reads a string from the input stream, calling
43 * a user-supplied function for each character.
44 * This function may be output() to write the
45 * string to the output file, or save() to save
46 * the string in the work buffer.
47 * scannumber() Reads a C numeric constant from the input stream,
48 * calling the user-supplied function for each
49 * character. (output() or save() as noted above.)
50 * save() Save one character in the work[] buffer.
51 * savestring() Saves a string in malloc() memory.
52 * getfile() Initialize a new FILEINFO structure, called when
53 * #include opens a new file, or a macro is to be
55 * getmem() Get a specified number of bytes from malloc memory.
56 * output() Write one character to stdout (calling PUTCHAR) --
57 * implemented as a function so its address may be
58 * passed to scanstring() and scannumber().
59 * lookid() Scans the next token (identifier) from the input
60 * stream. Looks for it in the #defined symbol table.
61 * Returns a pointer to the definition, if found, or NULL
62 * if not present. The identifier is stored in token[].
63 * defnedel() Define enter/delete subroutine. Updates the
65 * get() Read the next byte from the current input stream,
66 * handling end of (macro/file) input and embedded
67 * comments appropriately. Note that the global
68 * instring is -- essentially -- a parameter to get().
69 * cget() Like get(), but skip over TOK_SEP.
70 * unget() Push last gotten character back on the input stream.
71 * cerror(), cwarn(), cfatal(), cierror(), ciwarn()
72 * These routines format an print messages to the user.
73 * cerror & cwarn take a format and a single string argument.
74 * cierror & ciwarn take a format and a single int (char) argument.
75 * cfatal takes a format and a single string argument.
79 * This table must be rewritten for a non-Ascii machine.
81 * Note that several "non-visible" characters have special meaning:
82 * Hex 1D DEF_MAGIC -- a flag to prevent #define recursion.
83 * Hex 1E TOK_SEP -- a delimiter for token concatenation
84 * Hex 1F COM_SEP -- a zero-width whitespace for comment concatenation
86 #if TOK_SEP != 0x1E || COM_SEP != 0x1F || DEF_MAGIC != 0x1D
87 << error type table is
not correct
>>
93 char type
[256] = { /* Character type codes Hex */
94 END
, 000, 000, 000, 000, 000, 000, 000, /* 00 */
95 000, SPA
, 000, 000, 000, 000, 000, 000, /* 08 */
96 000, 000, 000, 000, 000, 000, 000, 000, /* 10 */
97 000, 000, 000, 000, 000, LET
, 000, SPA
, /* 18 */
98 SPA
,OP_NOT
, QUO
, 000, DOL
,OP_MOD
,OP_AND
, QUO
, /* 20 !"#$%&' */
99 OP_LPA
,OP_RPA
,OP_MUL
,OP_ADD
, 000,OP_SUB
, DOT
,OP_DIV
, /* 28 ()*+,-./ */
100 DIG
, DIG
, DIG
, DIG
, DIG
, DIG
, DIG
, DIG
, /* 30 01234567 */
101 DIG
, DIG
,OP_COL
, 000, OP_LT
, OP_EQ
, OP_GT
,OP_QUE
, /* 38 89:;<=>? */
102 000, LET
, LET
, LET
, LET
, LET
, LET
, LET
, /* 40 @ABCDEFG */
103 LET
, LET
, LET
, LET
, LET
, LET
, LET
, LET
, /* 48 HIJKLMNO */
104 LET
, LET
, LET
, LET
, LET
, LET
, LET
, LET
, /* 50 PQRSTUVW */
105 LET
, LET
, LET
, 000, BSH
, 000,OP_XOR
, LET
, /* 58 XYZ[\]^_ */
106 000, LET
, LET
, LET
, LET
, LET
, LET
, LET
, /* 60 `abcdefg */
107 LET
, LET
, LET
, LET
, LET
, LET
, LET
, LET
, /* 68 hijklmno */
108 LET
, LET
, LET
, LET
, LET
, LET
, LET
, LET
, /* 70 pqrstuvw */
109 LET
, LET
, LET
, 000, OP_OR
, 000,OP_NOT
, 000, /* 78 xyz{|}~ */
110 000, 000, 000, 000, 000, 000, 000, 000, /* 80 .. FF */
111 000, 000, 000, 000, 000, 000, 000, 000, /* 80 .. FF */
112 000, 000, 000, 000, 000, 000, 000, 000, /* 80 .. FF */
113 000, 000, 000, 000, 000, 000, 000, 000, /* 80 .. FF */
114 000, 000, 000, 000, 000, 000, 000, 000, /* 80 .. FF */
115 000, 000, 000, 000, 000, 000, 000, 000, /* 80 .. FF */
116 000, 000, 000, 000, 000, 000, 000, 000, /* 80 .. FF */
117 000, 000, 000, 000, 000, 000, 000, 000, /* 80 .. FF */
122 * C P P S y m b o l T a b l e s
126 * SBSIZE defines the number of hash-table slots for the symbol table.
127 * It must be a power of 2.
132 #define SBMASK (SBSIZE - 1)
133 #if (SBSIZE ^ SBMASK) != ((SBSIZE * 2) - 1)
134 << error
, SBSIZE must be a power of
2 >>
138 static DEFBUF
*symtab
[SBSIZE
]; /* Symbol table queue headers */
143 for( i
= 0; i
< SBSIZE
; i
++ )
150 * Skip to the end of the current input line.
157 { /* Skip to newline */
160 while (c
!= '\n' && c
!= EOF_CHAR
);
164 * Skip over whitespace
170 do { /* Skip whitespace */
172 } while (type
[c
] == SPA
);
177 * Get the next token (an id) into the token buffer.
178 * Note: this code is duplicated in lookid().
179 * Change one, change both.
185 if (c
== DEF_MAGIC
) /* Eat the magic token */
186 c
= get(); /* undefiner. */
190 if (bp
< &token
[IDMAX
]) /* token dim is IDMAX+1 */
194 while (type
[c
] == LET
|| type
[c
] == DIG
);
200 * If c is a letter, scan the id. if it's #defined, expand it and scan
201 * the next character and try again.
203 * Else, return the character. If type[c] is a LET, the token is in token.
209 if (infile
!= NULL
&& infile
->fp
!= NULL
)
211 while (type
[c
] == LET
&& (dp
= lookid(c
)) != NULL
)
220 * A token was just read (via macroid).
221 * If the next character is TOK_SEP, concatenate the next token
222 * return TRUE -- which should recall macroid after refreshing
223 * macroid's argument. If it is not TOK_SEP, unget() the character
231 if (get() != TOK_SEP
) /* Token concatenation */
238 token1
= savestring(token
); /* Save first token */
239 c
= macroid(get()); /* Scan next token */
240 switch(type
[c
]) /* What was it? */
242 case LET
: /* An identifier, ... */
243 if (strlen(token1
) + strlen(token
) >= NWORK
)
244 cfatal("work buffer overflow doing %s #", token1
);
245 sprintf(work
, "%s%s", token1
, token
);
248 case DIG
: /* A digit string */
249 strcpy(work
, token1
);
250 workp
= work
+ strlen(work
);
255 while ((c
= get()) != TOK_SEP
);
257 * The trailing TOK_SEP is no longer needed.
262 default: /* An error, ... */
264 cierror("Strange character '%c' after #", c
);
266 cierror("Strange character (%d.) after #", c
);
267 strcpy(work
, token1
);
272 * work has the concatenated token and token1 has
273 * the first token (no longer needed). Unget the
274 * new (concatenated) token after freeing token1.
275 * Finally, setup to read the new token.
277 free(token1
); /* Free up memory */
278 ungetstring(work
); /* Unget the new thing, */
284 * Scan off a string. Warning if terminated by newline or EOF.
285 * outfun() outputs the character -- to a buffer if in a macro.
286 * TRUE if ok, FALSE if error.
288 int scanstring(int delim
,
290 void (*outfun
)( int ) /* BP */ /* Output function */
292 void (*outfun
)() /* BP */
298 instring
= TRUE
; /* Don't strip comments */
300 while ((c
= get()) != delim
&&
317 cerror("Unterminated string", NULLST
);
324 * Process a number. We know that c is from 0 to 9 or dot.
325 * Algorithm from Dave Conroy's Decus C.
327 void scannumber(int c
,
329 void (*outfun
)( int ) /* BP */ /* Output/store func */
331 void (*outfun
)() /* BP */
335 int radix
; /* 8, 10, or 16 */
336 int expseen
; /* 'e' seen in floater */
337 int signseen
; /* '+' or '-' seen */
338 int octal89
; /* For bad octal test */
339 int dotflag
; /* TRUE if '.' was seen */
341 expseen
= FALSE
; /* No exponent seen yet */
342 signseen
= TRUE
; /* No +/- allowed yet */
343 octal89
= FALSE
; /* No bad octal yet */
344 radix
= 10; /* Assume decimal */
345 if ((dotflag
= (c
== '.')) != FALSE
) /* . something? */
347 (*outfun
)('.'); /* Always out the dot */
348 if (type
[(c
= get())] != DIG
) /* If not a float numb, */
350 unget(); /* Rescan strange char */
351 return; /* All done for now */
353 } /* End of float test */
354 else if (c
== '0') /* Octal or hex? */
356 (*outfun
)(c
); /* Stuff initial zero */
357 radix
= 8; /* Assume it's octal */
358 c
= get(); /* Look for an 'x' */
359 if (c
== 'x' || c
== 'X') /* Did we get one? */
361 radix
= 16; /* Remember new radix */
362 (*outfun
)(c
); /* Stuff the 'x' */
363 c
= get(); /* Get next character */
366 for (;;) /* Process curr. char. */
369 * Note that this algorithm accepts "012e4" and "03.4"
370 * as legitimate floating-point numbers.
372 if (radix
!= 16 && (c
== 'e' || c
== 'E'))
374 if (expseen
) /* Already saw 'E'? */
375 break; /* Exit loop, bad nbr. */
376 expseen
= TRUE
; /* Set exponent seen */
377 signseen
= FALSE
; /* We can read '+' now */
378 radix
= 10; /* Decimal exponent */
380 else if (radix
!= 16 && c
== '.')
382 if (dotflag
) /* Saw dot already? */
383 break; /* Exit loop, two dots */
384 dotflag
= TRUE
; /* Remember the dot */
385 radix
= 10; /* Decimal fraction */
387 else if (c
== '+' || c
== '-') /* 1.0e+10 */
389 if (signseen
) /* Sign in wrong place? */
390 break; /* Exit loop, not nbr. */
391 /* signseen = TRUE; */ /* Remember we saw it */
393 else /* Check the digit */
397 case '8': case '9': /* Sometimes wrong */
398 octal89
= TRUE
; /* Do check later */
399 case '0': case '1': case '2': case '3':
400 case '4': case '5': case '6': case '7':
401 break; /* Always ok */
403 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
404 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
405 if (radix
== 16) /* Alpha's are ok only */
406 break; /* if reading hex. */
407 default: /* At number end */
408 goto done
; /* Break from for loop */
409 } /* End of switch */
410 } /* End general case */
411 (*outfun
)(c
); /* Accept the character */
412 signseen
= TRUE
; /* Don't read sign now */
413 c
= get(); /* Read another char */
414 } /* End of scan loop */
416 * When we break out of the scan loop, c contains the first
417 * character (maybe) not in the number. If the number is an
418 * integer, allow a trailing 'L' for long and/or a trailing 'U'
419 * for unsigned. If not those, push the trailing character back
420 * on the input stream. Floating point numbers accept a trailing
421 * 'L' for "long double".
424 if (dotflag
|| expseen
) /* Floating point? */
426 if (c
== 'l' || c
== 'L')
429 get(); /* Ungotten later */
432 else /* Else it's an integer */
435 * We know that dotflag and expseen are both zero, now:
436 * dotflag signals "saw 'L'", and
437 * expseen signals "saw 'U'".
460 (*outfun
)(c
); /* Got 'L' or 'U'. */
461 c
= get(); /* Look at next, too. */
465 unget(); /* Not part of a number */
466 if (octal89
&& radix
== 8)
467 cwarn("Illegal digit in octal number", NULLST
);
472 if (workp
>= &work
[NWORK
])
474 work
[NWORK
-1] = '\0';
475 cfatal("Work buffer overflow: %s", work
);
482 * Store a string into free memory.
484 char* savestring(char* text
)
488 size_t size
= strlen(text
) + 1;
489 result
= getmem(size
);
490 strcpy(result
, text
);
495 * Common FILEINFO buffer initialization for a new file or macro.
497 FILEINFO
* getfile(size_t bufsize
, char* name
)
502 size
= strlen(name
); /* File/macro name */
503 file
= (FILEINFO
*) getmem(sizeof (FILEINFO
) + bufsize
+ size
);
504 file
->parent
= infile
; /* Chain files together */
505 file
->fp
= NULL
; /* No file yet */
506 file
->filename
= savestring(name
); /* Save file/macro name */
507 file
->progname
= NULL
; /* No #line seen yet */
508 file
->unrecur
= 0; /* No macro fixup */
509 file
->bptr
= file
->buffer
; /* Initialize line ptr */
510 file
->buffer
[0] = EOS
; /* Force first read */
511 file
->line
= 0; /* (Not used just yet) */
512 if (infile
!= NULL
) /* If #include file */
513 infile
->line
= line
; /* Save current line */
514 infile
= file
; /* New current file */
515 line
= 1; /* Note first line */
516 return file
; /* All done. */
520 * Get a block of free memory.
522 char* getmem(size_t size
)
526 if ((result
= malloc((unsigned) size
)) == NULL
)
527 cfatal("Out of memory", NULLST
);
532 * Look for the next token in the symbol table. Returns token in "token".
533 * If found, returns the table pointer; Else returns NULL.
535 DEFBUF
* lookid(int c
)
541 int isrecurse
; /* For #define foo foo */
545 if (0 != (isrecurse
= (c
== DEF_MAGIC
)))/* If recursive macro */
546 c
= get(); /* hack, skip DEF_MAGIC */
549 if (np
< &token
[IDMAX
]) /* token dim is IDMAX+1 */
551 *np
++ = (char)c
; /* Store token byte */
552 nhash
+= c
; /* Update hash value */
554 c
= get(); /* And get another byte */
556 while (type
[c
] == LET
|| type
[c
] == DIG
);
557 unget(); /* Rescan terminator */
558 *np
= EOS
; /* Terminate token */
559 if (isrecurse
) /* Recursive definition */
560 return NULL
; /* undefined just now */
561 nhash
+= (np
- token
); /* Fix hash value */
562 dp
= symtab
[nhash
& SBMASK
]; /* Starting bucket */
563 while (dp
!= (DEFBUF
*) NULL
) /* Search symbol table */
565 if (dp
->hash
== nhash
&& /* Fast precheck */
566 (temp
= strcmp(dp
->name
, token
)) >= 0)
570 dp
= dp
->link
; /* Nope, try next one */
572 return ((temp
== 0) ? dp
: NULL
);
576 * Enter this name in the lookup table (delete = FALSE)
577 * or delete this name (delete = TRUE).
578 * Returns a pointer to the define block (delete = FALSE)
579 * Returns NULL if the symbol wasn't defined (delete = TRUE).
581 DEFBUF
* defendel(char* name
, int delete)
590 for (nhash
= 0, np
= name
; *np
!= EOS
;)
594 prevp
= &symtab
[nhash
& SBMASK
];
595 while ((dp
= *prevp
) != (DEFBUF
*) NULL
)
597 if (dp
->hash
== nhash
&&
598 (temp
= strcmp(dp
->name
, name
)) >= 0)
601 dp
= NULL
; /* Not found */
604 *prevp
= dp
->link
; /* Found, unlink and */
605 if (dp
->repl
!= NULL
) /* Free the replacement */
606 free(dp
->repl
); /* if any, and then */
607 free((char*) dp
); /* Free the symbol */
616 dp
= (DEFBUF
*) getmem(sizeof (DEFBUF
) + size
);
622 strcpy(dp
->name
, name
);
627 #if OSL_DEBUG_LEVEL > 1
629 void dumpdef(char* why
)
633 FILE* pRememberOut
= NULL
;
635 if ( bDumpDefs
) /*ER */
637 pRememberOut
= pCppOut
;
640 fprintf( pCppOut
, "CPP symbol table dump %s\n", why
);
641 for (syp
= symtab
; syp
< &symtab
[SBSIZE
]; syp
++)
643 if ((dp
= *syp
) != (DEFBUF
*) NULL
)
645 fprintf( pCppOut
, "symtab[%" SAL_PRI_PTRDIFFT
"d]\n", (syp
- symtab
));
648 dumpadef((char*) NULL
, dp
);
650 while ((dp
= dp
->link
) != (DEFBUF
*) NULL
);
655 fprintf( pCppOut
, "\n");
656 pCppOut
= pRememberOut
;
660 void dumpadef(char* why
, DEFBUF
* dp
)
664 FILE* pRememberOut
= NULL
;
666 /*ER dump #define's to pDefOut */
669 pRememberOut
= pCppOut
;
672 fprintf( pCppOut
, " \"%s\" [%d]", dp
->name
, dp
->nargs
);
674 fprintf( pCppOut
, " (%s)", why
);
675 if (dp
->repl
!= NULL
)
677 fprintf( pCppOut
, " => ");
678 for (cp
= dp
->repl
; (c
= *cp
++ & 0xFF) != EOS
;)
684 if( c
== EOS
) break;
685 fprintf( pCppOut
, "<%%%d>", c
- MAC_PARM
);
688 if (c
>= MAC_PARM
&& c
<= (MAC_PARM
+ PAR_MAC
))
689 fprintf( pCppOut
, "<%%%d>", c
- MAC_PARM
);
691 else if (isprint(c
) || c
== '\n' || c
== '\t')
694 fprintf( pCppOut
, "<^%c>", c
+ '@');
696 fprintf( pCppOut
, "<\\0%o>", c
);
698 /*ER evaluate macros to pDefOut */
700 if ( bDumpDefs
&& !bIsInEval
&& dp
->nargs
<= 0 )
702 FILEINFO
* infileSave
= infile
;
703 char* tokenSave
= savestring( token
);
704 char* workSave
= savestring( work
);
706 int wronglineSave
= wrongline
;
707 int recursionSave
= recursion
;
712 infile
= NULL
; /* start from scrap */
718 file
= getfile( strlen( dp
->repl
), dp
->name
);
719 strcpy( file
->buffer
, dp
->repl
);
720 fprintf( pCppOut
, " ===> ");
722 cppmain(); /* get() frees also *file */
724 if ( 0 == evaluate( EvalBuf
, &valEval
) )
727 if ( valEval
!= (EVALTYPE
)((long)valEval
) )
728 fprintf( pCppOut
, " ==eval=> %f", valEval
);
731 fprintf( pCppOut
, " ==eval=> %ld", (long)valEval
);
733 recursion
= recursionSave
;
734 wrongline
= wronglineSave
;
736 strcpy( work
, workSave
);
738 strcpy( token
, tokenSave
);
747 fprintf( pCppOut
, ", no replacement.");
751 pCppOut
= pRememberOut
;
760 * Return the next character from a macro or the current file.
761 * Handle end of file from #include files.
767 int popped
; /* Recursion fixup */
771 if ((file
= infile
) == NULL
)
776 * Read a character from the current input line or macro.
777 * At EOS, either finish the current macro (freeing temp.
778 * storage) or read another line from the current input file.
779 * At EOF, exit the current file (#include) or, at EOF from
780 * the cpp input file, return EOF_CHAR to finish processing.
782 if ((c
= *file
->bptr
++ & 0xFF) == EOS
)
785 * Nothing in current line or macro. Get next line (if
786 * input from a file), or do end of file/macro processing.
787 * In the latter case, jump back to restart from the top.
789 if (file
->fp
== NULL
) /* NULL if macro */
792 recursion
-= file
->unrecur
;
795 infile
= file
->parent
; /* Unwind file chain */
797 else /* Else get from a file */
799 if ((file
->bptr
= fgets(file
->buffer
, NBUFF
, file
->fp
)) != NULL
)
801 #if OSL_DEBUG_LEVEL > 1
802 if (debug
> 1) /* Dump it to stdout */
804 fprintf( pCppOut
, "\n#line %d (%s), %s",
805 line
, file
->filename
, file
->buffer
);
808 goto newline
; /* process the line */
812 if( file
->fp
!= stdin
)
813 fclose(file
->fp
); /* Close finished file */
814 if ((infile
= file
->parent
) != NULL
)
817 * There is an "ungotten" newline in the current
818 * infile buffer (set there by doinclude() in
819 * cpp1.c). Thus, we know that the mainline code
820 * is skipping over blank lines and will do a
821 * #line at its convenience.
823 wrongline
= TRUE
; /* Need a #line now */
828 * Free up space used by the (finished) file or macro and
829 * restart input from the parent file/macro, if any.
831 free(file
->filename
); /* Free name and */
832 if (file
->progname
!= NULL
) /* if a #line was seen, */
833 free(file
->progname
); /* free it, too. */
834 free((char*) file
); /* Free file space */
835 if (infile
== NULL
) /* If at end of file */
836 return EOF_CHAR
; /* Return end of file */
837 line
= infile
->line
; /* Reset line number */
838 goto get_from_file
; /* Get from the top. */
841 * Common processing for the new character.
843 if (c
== DEF_MAGIC
&& file
->fp
!= NULL
) /* Don't allow delete */
844 goto newline
; /* from a file */
845 if (file
->parent
!= NULL
) /* Macro or #include */
848 file
->parent
->unrecur
+= popped
;
851 recursion
-= file
->parent
->unrecur
;
854 file
->parent
->unrecur
= 0;
857 #if (HOST == SYS_UNIX)
859 return get(); /* DOS fuck */
861 if (c
== '\n') /* Maintain current */
862 ++line
; /* line counter */
863 if (instring
) /* Strings just return */
864 return c
; /* the character. */
865 else if (c
== '/') /* Comment? */
867 instring
= TRUE
; /* So get() won't loop */
870 if ((c
!= '*') && (c
!= '/')) /* Next byte '*'? */
872 instring
= FALSE
; /* Nope, no comment */
873 unget(); /* Push the char. back */
874 return '/'; /* Return the slash */
876 if (keepcomments
) /* If writing comments */
878 PUTCHAR('/'); /* Write out the */
887 for (;;) /* Eat a comment */
891 if (keepcomments
&& c
!= EOF_CHAR
)
896 cerror("EOF in comment", NULLST
);
900 if ((c
= get()) != '*') /* Don't let comments */
901 goto test
; /* Nest. */
902 #ifdef STRICT_COMMENTS
903 cwarn("Nested comments", NULLST
);
905 /* Fall into * stuff */
907 if ((c
= get()) != '/') /* If comment doesn't */
908 goto test
; /* end, look at next */
909 instring
= FALSE
; /* End of comment, */
910 if (keepcomments
) /* Put out the comment */
912 cput(c
); /* terminator, too */
915 * A comment is syntactically "whitespace" --
916 * however, there are certain strange sequences
918 * #define foo(x) (something)
919 * foo|* comment *|(123)
921 * where just returning space (or COM_SEP) will cause
922 * problems. This can be "fixed" by overwriting the
923 * '/' in the input line buffer with ' ' (or COM_SEP)
924 * but that may mess up an error message.
925 * So, we peek ahead -- if the next character is
926 * "whitespace" we just get another character, if not,
927 * we modify the buffer. All in the name of purity.
929 if (*file
->bptr
== '\n' || type
[*file
->bptr
& 0xFF] == SPA
)
931 return (file
->bptr
[-1] = ' ');
933 case '\n': /* we'll need a #line */
935 wrongline
= TRUE
; /* later... */
936 default: /* Anything else is */
937 break; /* Just a character */
939 } /* End comment loop */
941 else /* c++ comment */
943 for (;;) /* Eat a comment */
946 if (keepcomments
&& c
!= EOF_CHAR
)
952 instring
= FALSE
; /* End of comment, */
957 } /* End if in comment */
958 else if (!inmacro
&& c
== '\\') /* If backslash, peek */
960 if ((c
= get()) == '\n') /* for a <nl>. If so, */
965 else /* Backslash anything */
967 unget(); /* Get it later */
968 return '\\'; /* Return the backslash */
971 else if (c
== '\f' || c
== VT
) /* Form Feed, Vertical */
973 c
= ' '; /* Tab are whitespace */
975 else if (c
== 0xef) /* eat up UTF-8 BOM */
977 if((c
= get()) == 0xbb)
979 if((c
= get()) == 0xbf)
997 return c
; /* Just return the char */
1001 * Backup the pointer to reread the last character. Fatal error
1002 * (code bug) if we backup too far. unget() may be called,
1003 * without problems, at end of file. Only one character may
1004 * be ungotten. If you need to unget more, call ungetstring().
1010 if ((file
= infile
) == NULL
)
1011 return; /* Unget after EOF */
1012 if (--file
->bptr
< file
->buffer
)
1013 cfatal("Too much pushback", NULLST
);
1014 if (*file
->bptr
== '\n') /* Ungetting a newline? */
1015 --line
; /* Unget the line number, too */
1019 * Push a string back on the input stream. This is done by treating
1020 * the text as if it were a macro.
1022 void ungetstring(char* text
)
1025 file
= getfile(strlen(text
) + 1, "");
1026 strcpy(file
->buffer
, text
);
1030 * Get one character, absorb "funny space" after comments or
1031 * token concatenation
1041 while (c
== TOK_SEP
);
1046 * Error messages and other hacks. The first byte of severity
1047 * is 'S' for string arguments and 'I' for int arguments. This
1048 * is needed for portability with machines that have int's that
1049 * are shorter than char *'s.
1053 * Print filenames, macro names, and line numbers for error messages.
1055 static void domsg(char* severity
, char* format
, void* arg
)
1060 fprintf(stderr
, "%sline %d, %s: ", MSG_PREFIX
, line
, &severity
[1]);
1061 if (*severity
== 'S')
1062 fprintf(stderr
, format
, (char*)arg
);
1064 fprintf(stderr
, format
, *((int*)arg
) );
1066 if ((file
= infile
) == NULL
)
1067 return; /* At end of file */
1068 if (file
->fp
!= NULL
)
1070 tp
= file
->buffer
; /* Print current file */
1071 fprintf(stderr
, "%s", tp
); /* name, making sure */
1072 if (tp
[strlen(tp
) - 1] != '\n') /* there's a newline */
1075 while ((file
= file
->parent
) != NULL
) /* Print #includes, too */
1077 if (file
->fp
== NULL
)
1078 fprintf(stderr
, "from macro %s\n", file
->filename
);
1082 fprintf(stderr
, "from file %s, line %d:\n%s",
1083 (file
->progname
!= NULL
)
1084 ? file
->progname
: file
->filename
,
1086 if (tp
[strlen(tp
) - 1] != '\n')
1093 * Print a normal error message, string argument.
1095 void cerror(char* format
, char* sarg
)
1097 domsg("SError", format
, sarg
);
1102 * Print a normal error message, numeric argument.
1104 void cierror(char* format
, int narg
)
1106 domsg("IError", format
, &narg
);
1113 void cfatal(char* format
, char* sarg
)
1115 domsg("SFatal error", format
, sarg
);
1120 * A non-fatal error, string argument.
1122 void cwarn(char* format
, char* sarg
)
1124 domsg("SWarning", format
, sarg
);
1128 * A non-fatal error, numeric argument.
1130 void ciwarn(char* format
, int narg
)
1132 domsg("IWarning", format
, &narg
);
1135 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */