bump product version to 5.0.4.1
[LibreOffice.git] / rsc / source / rscpp / cpp6.c
blob74340ddc2b38d7f60611e488f66bb44e4a9fe723
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
21 #include <stdio.h>
22 #include <ctype.h>
23 #include <string.h>
24 #include "cppdef.h"
25 #include "cpp.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
37 * never expanded.
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
54 * expanded.
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
64 * symbol table.
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 >>
88 #endif
90 #define DOL LET
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.
129 #ifndef SBSIZE
130 #define SBSIZE 64
131 #endif
132 #define SBMASK (SBSIZE - 1)
133 #if (SBSIZE ^ SBMASK) != ((SBSIZE * 2) - 1)
134 << error, SBSIZE must be a power of 2 >>
135 #endif
138 static DEFBUF *symtab[SBSIZE]; /* Symbol table queue headers */
140 void InitCpp6()
142 int i;
143 for( i = 0; i < SBSIZE; i++ )
144 symtab[ i ] = NULL;
150 * Skip to the end of the current input line.
152 void skipnl()
154 int c;
157 { /* Skip to newline */
158 c = get();
160 while (c != '\n' && c != EOF_CHAR);
164 * Skip over whitespace
166 int skipws()
168 int c;
170 do { /* Skip whitespace */
171 c = get();
172 } while (type[c] == SPA);
173 return c;
177 * Get the next token (an id) into the token buffer.
178 * Note: this code is duplicated in lookid().
179 * Change one, change both.
181 void scanid(int c)
183 char* bp;
185 if (c == DEF_MAGIC) /* Eat the magic token */
186 c = get(); /* undefiner. */
187 bp = token;
190 if (bp < &token[IDMAX]) /* token dim is IDMAX+1 */
191 *bp++ = (char)c;
192 c = get();
194 while (type[c] == LET || type[c] == DIG);
195 unget();
196 *bp = EOS;
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.
205 int macroid(int c)
207 DEFBUF* dp;
209 if (infile != NULL && infile->fp != NULL)
210 recursion = 0;
211 while (type[c] == LET && (dp = lookid(c)) != NULL)
213 expand(dp);
214 c = get();
216 return c;
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
224 * and return FALSE.
226 int catenate()
228 int c;
229 char* token1;
231 if (get() != TOK_SEP) /* Token concatenation */
233 unget();
234 return FALSE;
236 else
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);
246 break;
248 case DIG: /* A digit string */
249 strcpy(work, token1);
250 workp = work + strlen(work);
253 save(c);
255 while ((c = get()) != TOK_SEP);
257 * The trailing TOK_SEP is no longer needed.
259 save(EOS);
260 break;
262 default: /* An error, ... */
263 if (isprint(c))
264 cierror("Strange character '%c' after #", c);
265 else
266 cierror("Strange character (%d.) after #", c);
267 strcpy(work, token1);
268 unget();
269 break;
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, */
279 return TRUE;
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,
289 #ifndef _NO_PROTO
290 void (*outfun)( int ) /* BP */ /* Output function */
291 #else
292 void (*outfun)() /* BP */
293 #endif
296 int c;
298 instring = TRUE; /* Don't strip comments */
299 (*outfun)(delim);
300 while ((c = get()) != delim &&
301 c != '\n' &&
302 c != EOF_CHAR)
304 if (c != DEF_MAGIC)
305 (*outfun)(c);
306 if (c == '\\')
307 (*outfun)(get());
309 instring = FALSE;
310 if (c == delim)
312 (*outfun)(c);
313 return TRUE;
315 else
317 cerror("Unterminated string", NULLST);
318 unget();
319 return FALSE;
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,
328 #ifndef _NO_PROTO
329 void (*outfun)( int ) /* BP */ /* Output/store func */
330 #else
331 void (*outfun)() /* BP */
332 #endif
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 */
395 switch (c)
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".
423 done:
424 if (dotflag || expseen) /* Floating point? */
426 if (c == 'l' || c == 'L')
428 (*outfun)(c);
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'".
439 for (;;)
441 switch (c)
443 case 'l':
444 case 'L':
445 if (dotflag)
446 goto nomore;
447 dotflag = TRUE;
448 break;
450 case 'u':
451 case 'U':
452 if (expseen)
453 goto nomore;
454 expseen = TRUE;
455 break;
457 default:
458 goto nomore;
460 (*outfun)(c); /* Got 'L' or 'U'. */
461 c = get(); /* Look at next, too. */
464 nomore:
465 unget(); /* Not part of a number */
466 if (octal89 && radix == 8)
467 cwarn("Illegal digit in octal number", NULLST);
470 void save(int c)
472 if (workp >= &work[NWORK])
474 work[NWORK-1] = '\0';
475 cfatal("Work buffer overflow: %s", work);
477 else
478 *workp++ = (char)c;
482 * Store a string into free memory.
484 char* savestring(char* text)
486 char* result;
488 size_t size = strlen(text) + 1;
489 result = getmem(size);
490 strcpy(result, text);
491 return result;
495 * Common FILEINFO buffer initialization for a new file or macro.
497 FILEINFO* getfile(size_t bufsize, char* name)
499 FILEINFO* file;
500 size_t size;
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)
524 char* result;
526 if ((result = malloc((unsigned) size)) == NULL)
527 cfatal("Out of memory", NULLST);
528 return result;
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)
537 int nhash;
538 DEFBUF* dp;
539 char* np;
540 int temp = 0;
541 int isrecurse; /* For #define foo foo */
543 np = token;
544 nhash = 0;
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)
568 break;
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)
583 DEFBUF* dp;
584 DEFBUF** prevp;
585 char* np;
586 int nhash;
587 int temp=0;
588 int size;
590 for (nhash = 0, np = name; *np != EOS;)
591 nhash += *np++;
592 size = (np - name);
593 nhash += size;
594 prevp = &symtab[nhash & SBMASK];
595 while ((dp = *prevp) != (DEFBUF*) NULL)
597 if (dp->hash == nhash &&
598 (temp = strcmp(dp->name, name)) >= 0)
600 if (temp > 0)
601 dp = NULL; /* Not found */
602 else
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 */
608 dp = NULL;
610 break;
612 prevp = &dp->link;
614 if (!delete)
616 dp = (DEFBUF*) getmem(sizeof (DEFBUF) + size);
617 dp->link = *prevp;
618 *prevp = dp;
619 dp->hash = nhash;
620 dp->repl = NULL;
621 dp->nargs = 0;
622 strcpy(dp->name, name);
624 return dp;
627 #if OSL_DEBUG_LEVEL > 1
629 void dumpdef(char* why)
631 DEFBUF* dp;
632 DEFBUF** syp;
633 FILE* pRememberOut = NULL;
635 if ( bDumpDefs ) /*ER */
637 pRememberOut = pCppOut;
638 pCppOut = pDefOut;
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);
653 if ( bDumpDefs )
655 fprintf( pCppOut, "\n");
656 pCppOut = pRememberOut;
660 void dumpadef(char* why, DEFBUF* dp)
662 char* cp;
663 int c;
664 FILE* pRememberOut = NULL;
666 /*ER dump #define's to pDefOut */
667 if ( bDumpDefs )
669 pRememberOut = pCppOut;
670 pCppOut = pDefOut;
672 fprintf( pCppOut, " \"%s\" [%d]", dp->name, dp->nargs);
673 if (why != NULL)
674 fprintf( pCppOut, " (%s)", why);
675 if (dp->repl != NULL)
677 fprintf( pCppOut, " => ");
678 for (cp = dp->repl; (c = *cp++ & 0xFF) != EOS;)
680 #ifdef SOLAR
681 if (c == DEL)
683 c = *cp++ & 0xFF;
684 if( c == EOS ) break;
685 fprintf( pCppOut, "<%%%d>", c - MAC_PARM);
687 #else
688 if (c >= MAC_PARM && c <= (MAC_PARM + PAR_MAC))
689 fprintf( pCppOut, "<%%%d>", c - MAC_PARM);
690 #endif
691 else if (isprint(c) || c == '\n' || c == '\t')
692 PUTCHAR(c);
693 else if (c < ' ')
694 fprintf( pCppOut, "<^%c>", c + '@');
695 else
696 fprintf( pCppOut, "<\\0%o>", c);
698 /*ER evaluate macros to pDefOut */
699 #ifdef EVALDEFS
700 if ( bDumpDefs && !bIsInEval && dp->nargs <= 0 )
702 FILEINFO* infileSave = infile;
703 char* tokenSave = savestring( token );
704 char* workSave = savestring( work );
705 int lineSave = line;
706 int wronglineSave = wrongline;
707 int recursionSave = recursion;
708 FILEINFO* file;
709 EVALTYPE valEval;
711 bIsInEval = 1;
712 infile = NULL; /* start from scrap */
713 line = 0;
714 wrongline = 0;
715 *token = EOS;
716 *work = EOS;
717 recursion = 0;
718 file = getfile( strlen( dp->repl ), dp->name );
719 strcpy( file->buffer, dp->repl );
720 fprintf( pCppOut, " ===> ");
721 nEvalOff = 0;
722 cppmain(); /* get() frees also *file */
723 valEval = 0;
724 if ( 0 == evaluate( EvalBuf, &valEval ) )
726 #ifdef EVALFLOATS
727 if ( valEval != (EVALTYPE)((long)valEval ) )
728 fprintf( pCppOut, " ==eval=> %f", valEval );
729 else
730 #endif
731 fprintf( pCppOut, " ==eval=> %ld", (long)valEval );
733 recursion = recursionSave;
734 wrongline = wronglineSave;
735 line = lineSave;
736 strcpy( work, workSave );
737 free( workSave );
738 strcpy( token, tokenSave );
739 free( tokenSave );
740 infile = infileSave;
741 bIsInEval = 0;
743 #endif
745 else
747 fprintf( pCppOut, ", no replacement.");
749 PUTCHAR('\n');
750 if ( bDumpDefs )
751 pCppOut = pRememberOut;
753 #endif
756 * G E T
760 * Return the next character from a macro or the current file.
761 * Handle end of file from #include files.
763 int get()
765 int c;
766 FILEINFO* file;
767 int popped; /* Recursion fixup */
769 popped = 0;
770 get_from_file:
771 if ((file = infile) == NULL)
772 return EOF_CHAR;
773 newline:
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 */
791 popped++;
792 recursion -= file->unrecur;
793 if (recursion < 0)
794 recursion = 0;
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);
807 #endif
808 goto newline; /* process the line */
810 else
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 */
847 if (popped != 0)
848 file->parent->unrecur += popped;
849 else
851 recursion -= file->parent->unrecur;
852 if (recursion < 0)
853 recursion = 0;
854 file->parent->unrecur = 0;
857 #if (HOST == SYS_UNIX)
858 if (c == '\r')
859 return get(); /* DOS fuck */
860 #endif
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 */
869 c = get();
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 */
879 /* initializer */
880 if( '*' == c )
881 PUTCHAR('*');
882 else
883 PUTCHAR('/');
885 if( '*' == c )
887 for (;;) /* Eat a comment */
889 c = get();
890 test:
891 if (keepcomments && c != EOF_CHAR)
892 cput(c);
893 switch (c)
895 case EOF_CHAR:
896 cerror("EOF in comment", NULLST);
897 return EOF_CHAR;
899 case '/':
900 if ((c = get()) != '*') /* Don't let comments */
901 goto test; /* Nest. */
902 #ifdef STRICT_COMMENTS
903 cwarn("Nested comments", NULLST);
904 #endif
905 /* Fall into * stuff */
906 case '*':
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
917 * such as
918 * #define foo(x) (something)
919 * foo|* comment *|(123)
920 * these are '/' ^ ^
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)
930 goto newline;
931 return (file->bptr[-1] = ' ');
933 case '\n': /* we'll need a #line */
934 if (!keepcomments)
935 wrongline = TRUE; /* later... */
936 default: /* Anything else is */
937 break; /* Just a character */
938 } /* End switch */
939 } /* End comment loop */
941 else /* c++ comment */
943 for (;;) /* Eat a comment */
945 c = get();
946 if (keepcomments && c != EOF_CHAR)
947 cput(c);
948 if( EOF_CHAR == c )
949 return EOF_CHAR;
950 else if( '\n' == c )
952 instring = FALSE; /* End of comment, */
953 return c;
957 } /* End if in comment */
958 else if (!inmacro && c == '\\') /* If backslash, peek */
960 if ((c = get()) == '\n') /* for a <nl>. If so, */
962 wrongline = TRUE;
963 goto newline;
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)
981 c = get();
982 return c;
984 else
986 unget();
987 unget();
988 return 0xef;
991 else
993 unget();
994 return 0xef;
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().
1006 void unget()
1008 FILEINFO* file;
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)
1024 FILEINFO* file;
1025 file = getfile(strlen(text) + 1, "");
1026 strcpy(file->buffer, text);
1030 * Get one character, absorb "funny space" after comments or
1031 * token concatenation
1033 int cget()
1035 int c;
1039 c = get();
1041 while (c == TOK_SEP);
1042 return c;
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)
1057 char* tp;
1058 FILEINFO* file;
1060 fprintf(stderr, "%sline %d, %s: ", MSG_PREFIX, line, &severity[1]);
1061 if (*severity == 'S')
1062 fprintf(stderr, format, (char*)arg);
1063 else
1064 fprintf(stderr, format, *((int*)arg) );
1065 putc('\n', stderr);
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 */
1073 putc('\n', stderr);
1075 while ((file = file->parent) != NULL) /* Print #includes, too */
1077 if (file->fp == NULL)
1078 fprintf(stderr, "from macro %s\n", file->filename);
1079 else
1081 tp = file->buffer;
1082 fprintf(stderr, "from file %s, line %d:\n%s",
1083 (file->progname != NULL)
1084 ? file->progname : file->filename,
1085 file->line, tp);
1086 if (tp[strlen(tp) - 1] != '\n')
1087 putc('\n', stderr);
1093 * Print a normal error message, string argument.
1095 void cerror(char* format, char* sarg)
1097 domsg("SError", format, sarg);
1098 errors++;
1102 * Print a normal error message, numeric argument.
1104 void cierror(char* format, int narg)
1106 domsg("IError", format, &narg);
1107 errors++;
1111 * A real disaster
1113 void cfatal(char* format, char* sarg)
1115 domsg("SFatal error", format, sarg);
1116 exit(IO_ERROR);
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: */