fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / rsc / source / rscpp / cpp6.c
blob2e3be99c06734f2f4e031868d32283443bb28af0
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 #if OK_DOLLAR
91 #define DOL LET
92 #else
93 #define DOL 000
94 #endif
96 #ifdef EBCDIC
98 char type[256] = { /* Character type codes Hex */
99 END, 000, 000, 000, 000, SPA, 000, 000, /* 00 */
100 000, 000, 000, 000, 000, 000, 000, 000, /* 08 */
101 000, 000, 000, 000, 000, 000, 000, 000, /* 10 */
102 000, 000, 000, 000, 000, LET, 000, SPA, /* 18 */
103 000, 000, 000, 000, 000, 000, 000, 000, /* 20 */
104 000, 000, 000, 000, 000, 000, 000, 000, /* 28 */
105 000, 000, 000, 000, 000, 000, 000, 000, /* 30 */
106 000, 000, 000, 000, 000, 000, 000, 000, /* 38 */
107 SPA, 000, 000, 000, 000, 000, 000, 000, /* 40 */
108 000, 000, 000, DOT, OP_LT,OP_LPA,OP_ADD, OP_OR, /* 48 .<(+| */
109 OP_AND, 000, 000, 000, 000, 000, 000, 000, /* 50 & */
110 000, 000,OP_NOT, DOL,OP_MUL,OP_RPA, 000,OP_XOR, /* 58 !$*);^ */
111 OP_SUB,OP_DIV, 000, 000, 000, 000, 000, 000, /* 60 -/ */
112 000, 000, 000, 000,OP_MOD, LET, OP_GT,OP_QUE, /* 68 ,%_>? */
113 000, 000, 000, 000, 000, 000, 000, 000, /* 70 */
114 000, 000,OP_COL, 000, 000, QUO, OP_EQ, QUO, /* 78 `:#@'=" */
115 000, LET, LET, LET, LET, LET, LET, LET, /* 80 abcdefg */
116 LET, LET, 000, 000, 000, 000, 000, 000, /* 88 hi */
117 000, LET, LET, LET, LET, LET, LET, LET, /* 90 jklmnop */
118 LET, LET, 000, 000, 000, 000, 000, 000, /* 98 qr */
119 000,OP_NOT, LET, LET, LET, LET, LET, LET, /* A0 ~stuvwx */
120 LET, LET, 000, 000, 000, 000, 000, 000, /* A8 yz [ */
121 000, 000, 000, 000, 000, 000, 000, 000, /* B0 */
122 000, 000, 000, 000, 000, 000, 000, 000, /* B8 ] */
123 000, LET, LET, LET, LET, LET, LET, LET, /* C0 {ABCDEFG */
124 LET, LET, 000, 000, 000, 000, 000, 000, /* C8 HI */
125 000, LET, LET, LET, LET, LET, LET, LET, /* D0 }JKLMNOP */
126 LET, LET, 000, 000, 000, 000, 000, 000, /* D8 QR */
127 BSH, 000, LET, LET, LET, LET, LET, LET, /* E0 \ STUVWX */
128 LET, LET, 000, 000, 000, 000, 000, 000, /* E8 YZ */
129 DIG, DIG, DIG, DIG, DIG, DIG, DIG, DIG, /* F0 01234567 */
130 DIG, DIG, 000, 000, 000, 000, 000, 000, /* F8 89 */
133 #else
135 char type[256] = { /* Character type codes Hex */
136 END, 000, 000, 000, 000, 000, 000, 000, /* 00 */
137 000, SPA, 000, 000, 000, 000, 000, 000, /* 08 */
138 000, 000, 000, 000, 000, 000, 000, 000, /* 10 */
139 000, 000, 000, 000, 000, LET, 000, SPA, /* 18 */
140 SPA,OP_NOT, QUO, 000, DOL,OP_MOD,OP_AND, QUO, /* 20 !"#$%&' */
141 OP_LPA,OP_RPA,OP_MUL,OP_ADD, 000,OP_SUB, DOT,OP_DIV, /* 28 ()*+,-./ */
142 DIG, DIG, DIG, DIG, DIG, DIG, DIG, DIG, /* 30 01234567 */
143 DIG, DIG,OP_COL, 000, OP_LT, OP_EQ, OP_GT,OP_QUE, /* 38 89:;<=>? */
144 000, LET, LET, LET, LET, LET, LET, LET, /* 40 @ABCDEFG */
145 LET, LET, LET, LET, LET, LET, LET, LET, /* 48 HIJKLMNO */
146 LET, LET, LET, LET, LET, LET, LET, LET, /* 50 PQRSTUVW */
147 LET, LET, LET, 000, BSH, 000,OP_XOR, LET, /* 58 XYZ[\]^_ */
148 000, LET, LET, LET, LET, LET, LET, LET, /* 60 `abcdefg */
149 LET, LET, LET, LET, LET, LET, LET, LET, /* 68 hijklmno */
150 LET, LET, LET, LET, LET, LET, LET, LET, /* 70 pqrstuvw */
151 LET, LET, LET, 000, OP_OR, 000,OP_NOT, 000, /* 78 xyz{|}~ */
152 000, 000, 000, 000, 000, 000, 000, 000, /* 80 .. FF */
153 000, 000, 000, 000, 000, 000, 000, 000, /* 80 .. FF */
154 000, 000, 000, 000, 000, 000, 000, 000, /* 80 .. FF */
155 000, 000, 000, 000, 000, 000, 000, 000, /* 80 .. FF */
156 000, 000, 000, 000, 000, 000, 000, 000, /* 80 .. FF */
157 000, 000, 000, 000, 000, 000, 000, 000, /* 80 .. FF */
158 000, 000, 000, 000, 000, 000, 000, 000, /* 80 .. FF */
159 000, 000, 000, 000, 000, 000, 000, 000, /* 80 .. FF */
162 #endif
165 * C P P S y m b o l T a b l e s
169 * SBSIZE defines the number of hash-table slots for the symbol table.
170 * It must be a power of 2.
172 #ifndef SBSIZE
173 #define SBSIZE 64
174 #endif
175 #define SBMASK (SBSIZE - 1)
176 #if (SBSIZE ^ SBMASK) != ((SBSIZE * 2) - 1)
177 << error, SBSIZE must be a power of 2 >>
178 #endif
181 static DEFBUF *symtab[SBSIZE]; /* Symbol table queue headers */
183 void InitCpp6()
185 int i;
186 for( i = 0; i < SBSIZE; i++ )
187 symtab[ i ] = NULL;
192 void skipnl()
194 * Skip to the end of the current input line.
197 register int c;
199 do { /* Skip to newline */
200 c = get();
201 } while (c != '\n' && c != EOF_CHAR);
205 skipws()
207 * Skip over whitespace
210 register int c;
212 do { /* Skip whitespace */
213 c = get();
214 #if COMMENT_INVISIBLE
215 } while (type[c] == SPA || c == COM_SEP);
216 #else
217 } while (type[c] == SPA);
218 #endif
219 return (c);
222 void scanid(int c)
224 * Get the next token (an id) into the token buffer.
225 * Note: this code is duplicated in lookid().
226 * Change one, change both.
229 register char *bp;
231 if (c == DEF_MAGIC) /* Eat the magic token */
232 c = get(); /* undefiner. */
233 bp = token;
234 do {
235 if (bp < &token[IDMAX]) /* token dim is IDMAX+1 */
236 *bp++ = (char)c;
237 c = get();
238 } while (type[c] == LET || type[c] == DIG);
239 unget();
240 *bp = EOS;
244 macroid(int c)
246 * If c is a letter, scan the id. if it's #defined, expand it and scan
247 * the next character and try again.
249 * Else, return the character. If type[c] is a LET, the token is in token.
252 register DEFBUF *dp;
254 if (infile != NULL && infile->fp != NULL)
255 recursion = 0;
256 while (type[c] == LET && (dp = lookid(c)) != NULL) {
257 expand(dp);
258 c = get();
260 return (c);
264 catenate()
266 * A token was just read (via macroid).
267 * If the next character is TOK_SEP, concatenate the next token
268 * return TRUE -- which should recall macroid after refreshing
269 * macroid's argument. If it is not TOK_SEP, unget() the character
270 * and return FALSE.
273 register int c;
274 register char *token1;
276 #if OK_CONCAT
277 if (get() != TOK_SEP) { /* Token concatenation */
278 unget();
279 return (FALSE);
281 else {
282 token1 = savestring(token); /* Save first token */
283 c = macroid(get()); /* Scan next token */
284 switch(type[c]) { /* What was it? */
285 case LET: /* An identifier, ... */
286 if (strlen(token1) + strlen(token) >= NWORK)
287 cfatal("work buffer overflow doing %s #", token1);
288 sprintf(work, "%s%s", token1, token);
289 break;
291 case DIG: /* A digit string */
292 strcpy(work, token1);
293 workp = work + strlen(work);
294 do {
295 save(c);
296 } while ((c = get()) != TOK_SEP);
298 * The trailing TOK_SEP is no longer needed.
300 save(EOS);
301 break;
303 default: /* An error, ... */
304 #if ! COMMENT_INVISIBLE
305 if (isprint(c))
306 cierror("Strange character '%c' after #", c);
307 else
308 cierror("Strange character (%d.) after #", c);
309 #endif
310 strcpy(work, token1);
311 unget();
312 break;
315 * work has the concatenated token and token1 has
316 * the first token (no longer needed). Unget the
317 * new (concatenated) token after freeing token1.
318 * Finally, setup to read the new token.
320 free(token1); /* Free up memory */
321 ungetstring(work); /* Unget the new thing, */
322 return (TRUE);
324 #else
325 return (FALSE); /* Not supported */
326 #endif
330 scanstring(int delim,
331 #ifndef _NO_PROTO
332 void (*outfun)( int ) /* BP */ /* Output function */
333 #else
334 void (*outfun)() /* BP */
335 #endif
338 * Scan off a string. Warning if terminated by newline or EOF.
339 * outfun() outputs the character -- to a buffer if in a macro.
340 * TRUE if ok, FALSE if error.
343 register int c;
345 instring = TRUE; /* Don't strip comments */
346 (*outfun)(delim);
347 while ((c = get()) != delim
348 && c != '\n'
349 && c != EOF_CHAR) {
351 if (c != DEF_MAGIC)
352 (*outfun)(c);
353 if (c == '\\')
354 (*outfun)(get());
356 instring = FALSE;
357 if (c == delim) {
358 (*outfun)(c);
359 return (TRUE);
361 else {
362 cerror("Unterminated string", NULLST);
363 unget();
364 return (FALSE);
368 void scannumber(int c,
369 #ifndef _NO_PROTO
370 register void (*outfun)( int ) /* BP */ /* Output/store func */
371 #else
372 register void (*outfun)() /* BP */
373 #endif
376 * Process a number. We know that c is from 0 to 9 or dot.
377 * Algorithm from Dave Conroy's Decus C.
380 register int radix; /* 8, 10, or 16 */
381 int expseen; /* 'e' seen in floater */
382 int signseen; /* '+' or '-' seen */
383 int octal89; /* For bad octal test */
384 int dotflag; /* TRUE if '.' was seen */
386 expseen = FALSE; /* No exponent seen yet */
387 signseen = TRUE; /* No +/- allowed yet */
388 octal89 = FALSE; /* No bad octal yet */
389 radix = 10; /* Assume decimal */
390 if ((dotflag = (c == '.')) != FALSE) { /* . something? */
391 (*outfun)('.'); /* Always out the dot */
392 if (type[(c = get())] != DIG) { /* If not a float numb, */
393 unget(); /* Rescan strange char */
394 return; /* All done for now */
396 } /* End of float test */
397 else if (c == '0') { /* Octal or hex? */
398 (*outfun)(c); /* Stuff initial zero */
399 radix = 8; /* Assume it's octal */
400 c = get(); /* Look for an 'x' */
401 if (c == 'x' || c == 'X') { /* Did we get one? */
402 radix = 16; /* Remember new radix */
403 (*outfun)(c); /* Stuff the 'x' */
404 c = get(); /* Get next character */
407 for (;;) { /* Process curr. char. */
409 * Note that this algorithm accepts "012e4" and "03.4"
410 * as legitimate floating-point numbers.
412 if (radix != 16 && (c == 'e' || c == 'E')) {
413 if (expseen) /* Already saw 'E'? */
414 break; /* Exit loop, bad nbr. */
415 expseen = TRUE; /* Set exponent seen */
416 signseen = FALSE; /* We can read '+' now */
417 radix = 10; /* Decimal exponent */
419 else if (radix != 16 && c == '.') {
420 if (dotflag) /* Saw dot already? */
421 break; /* Exit loop, two dots */
422 dotflag = TRUE; /* Remember the dot */
423 radix = 10; /* Decimal fraction */
425 else if (c == '+' || c == '-') { /* 1.0e+10 */
426 if (signseen) /* Sign in wrong place? */
427 break; /* Exit loop, not nbr. */
428 /* signseen = TRUE; */ /* Remember we saw it */
430 else { /* Check the digit */
431 switch (c) {
432 case '8': case '9': /* Sometimes wrong */
433 octal89 = TRUE; /* Do check later */
434 case '0': case '1': case '2': case '3':
435 case '4': case '5': case '6': case '7':
436 break; /* Always ok */
438 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
439 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
440 if (radix == 16) /* Alpha's are ok only */
441 break; /* if reading hex. */
442 default: /* At number end */
443 goto done; /* Break from for loop */
444 } /* End of switch */
445 } /* End general case */
446 (*outfun)(c); /* Accept the character */
447 signseen = TRUE; /* Don't read sign now */
448 c = get(); /* Read another char */
449 } /* End of scan loop */
451 * When we break out of the scan loop, c contains the first
452 * character (maybe) not in the number. If the number is an
453 * integer, allow a trailing 'L' for long and/or a trailing 'U'
454 * for unsigned. If not those, push the trailing character back
455 * on the input stream. Floating point numbers accept a trailing
456 * 'L' for "long double".
458 done: if (dotflag || expseen) { /* Floating point? */
459 if (c == 'l' || c == 'L') {
460 (*outfun)(c);
461 get(); /* Ungotten later */
464 else { /* Else it's an integer */
466 * We know that dotflag and expseen are both zero, now:
467 * dotflag signals "saw 'L'", and
468 * expseen signals "saw 'U'".
470 for (;;) {
471 switch (c) {
472 case 'l':
473 case 'L':
474 if (dotflag)
475 goto nomore;
476 dotflag = TRUE;
477 break;
479 case 'u':
480 case 'U':
481 if (expseen)
482 goto nomore;
483 expseen = TRUE;
484 break;
486 default:
487 goto nomore;
489 (*outfun)(c); /* Got 'L' or 'U'. */
490 c = get(); /* Look at next, too. */
493 nomore: unget(); /* Not part of a number */
494 if (octal89 && radix == 8)
495 cwarn("Illegal digit in octal number", NULLST);
498 void save(int c)
500 if (workp >= &work[NWORK]) {
501 work[NWORK-1] = '\0';
502 cfatal("Work buffer overflow: %s", work);
504 else *workp++ = (char)c;
507 char *
508 savestring(char* text)
510 * Store a string into free memory.
513 register char *result;
515 result = getmem(strlen(text) + 1);
516 strcpy(result, text);
517 return (result);
520 FILEINFO *
521 getfile(int bufsize, char* name)
523 * Common FILEINFO buffer initialization for a new file or macro.
526 register FILEINFO *file;
527 register int size;
529 size = strlen(name); /* File/macro name */
530 file = (FILEINFO *) getmem(sizeof (FILEINFO) + bufsize + size);
531 file->parent = infile; /* Chain files together */
532 file->fp = NULL; /* No file yet */
533 file->filename = savestring(name); /* Save file/macro name */
534 file->progname = NULL; /* No #line seen yet */
535 file->unrecur = 0; /* No macro fixup */
536 file->bptr = file->buffer; /* Initialize line ptr */
537 file->buffer[0] = EOS; /* Force first read */
538 file->line = 0; /* (Not used just yet) */
539 if (infile != NULL) /* If #include file */
540 infile->line = line; /* Save current line */
541 infile = file; /* New current file */
542 line = 1; /* Note first line */
543 return (file); /* All done. */
546 char *
547 getmem(int size)
549 * Get a block of free memory.
552 register char *result;
554 if ((result = malloc((unsigned) size)) == NULL)
555 cfatal("Out of memory", NULLST);
556 return (result);
559 DEFBUF *
560 lookid(int c)
562 * Look for the next token in the symbol table. Returns token in "token".
563 * If found, returns the table pointer; Else returns NULL.
566 register int nhash;
567 register DEFBUF *dp;
568 register char *np;
569 int temp = 0;
570 int isrecurse; /* For #define foo foo */
572 np = token;
573 nhash = 0;
574 if (0 != (isrecurse = (c == DEF_MAGIC))) /* If recursive macro */
575 c = get(); /* hack, skip DEF_MAGIC */
576 do {
577 if (np < &token[IDMAX]) { /* token dim is IDMAX+1 */
578 *np++ = (char)c; /* Store token byte */
579 nhash += c; /* Update hash value */
581 c = get(); /* And get another byte */
582 } while (type[c] == LET || type[c] == DIG);
583 unget(); /* Rescan terminator */
584 *np = EOS; /* Terminate token */
585 if (isrecurse) /* Recursive definition */
586 return (NULL); /* undefined just now */
587 nhash += (np - token); /* Fix hash value */
588 dp = symtab[nhash & SBMASK]; /* Starting bucket */
589 while (dp != (DEFBUF *) NULL) { /* Search symbol table */
590 if (dp->hash == nhash /* Fast precheck */
591 && (temp = strcmp(dp->name, token)) >= 0)
592 break;
593 dp = dp->link; /* Nope, try next one */
595 return ((temp == 0) ? dp : NULL);
598 DEFBUF *
599 defendel(char* name, int delete)
601 * Enter this name in the lookup table (delete = FALSE)
602 * or delete this name (delete = TRUE).
603 * Returns a pointer to the define block (delete = FALSE)
604 * Returns NULL if the symbol wasn't defined (delete = TRUE).
607 register DEFBUF *dp;
608 register DEFBUF **prevp;
609 register char *np;
610 int nhash;
611 int temp;
612 int size;
614 for (nhash = 0, np = name; *np != EOS;)
615 nhash += *np++;
616 size = (np - name);
617 nhash += size;
618 prevp = &symtab[nhash & SBMASK];
619 while ((dp = *prevp) != (DEFBUF *) NULL) {
620 if (dp->hash == nhash
621 && (temp = strcmp(dp->name, name)) >= 0) {
622 if (temp > 0)
623 dp = NULL; /* Not found */
624 else {
625 *prevp = dp->link; /* Found, unlink and */
626 if (dp->repl != NULL) /* Free the replacement */
627 free(dp->repl); /* if any, and then */
628 free((char *) dp); /* Free the symbol */
630 break;
632 prevp = &dp->link;
634 if (!delete) {
635 dp = (DEFBUF *) getmem(sizeof (DEFBUF) + size);
636 dp->link = *prevp;
637 *prevp = dp;
638 dp->hash = nhash;
639 dp->repl = NULL;
640 dp->nargs = 0;
641 strcpy(dp->name, name);
643 return (dp);
646 #if OSL_DEBUG_LEVEL > 1
648 void dumpdef(char *why)
650 register DEFBUF *dp;
651 register DEFBUF **syp;
652 FILE *pRememberOut = NULL;
654 if ( bDumpDefs ) /*ER */
656 pRememberOut = pCppOut;
657 pCppOut = pDefOut;
659 fprintf( pCppOut, "CPP symbol table dump %s\n", why);
660 for (syp = symtab; syp < &symtab[SBSIZE]; syp++) {
661 if ((dp = *syp) != (DEFBUF *) NULL) {
662 fprintf( pCppOut, "symtab[%" SAL_PRI_PTRDIFFT "d]\n", (syp - symtab));
663 do {
664 dumpadef((char *) NULL, dp);
665 } while ((dp = dp->link) != (DEFBUF *) NULL);
668 if ( bDumpDefs )
670 fprintf( pCppOut, "\n");
671 pCppOut = pRememberOut;
675 void dumpadef(char *why, register DEFBUF *dp)
677 register char *cp;
678 register int c;
679 FILE *pRememberOut = NULL;
681 /*ER dump #define's to pDefOut */
682 if ( bDumpDefs )
684 pRememberOut = pCppOut;
685 pCppOut = pDefOut;
687 fprintf( pCppOut, " \"%s\" [%d]", dp->name, dp->nargs);
688 if (why != NULL)
689 fprintf( pCppOut, " (%s)", why);
690 if (dp->repl != NULL) {
691 fprintf( pCppOut, " => ");
692 for (cp = dp->repl; (c = *cp++ & 0xFF) != EOS;) {
693 #ifdef SOLAR
694 if (c == DEL) {
695 c = *cp++ & 0xFF;
696 if( c == EOS ) break;
697 fprintf( pCppOut, "<%%%d>", c - MAC_PARM);
699 #else
700 if (c >= MAC_PARM && c <= (MAC_PARM + PAR_MAC))
701 fprintf( pCppOut, "<%%%d>", c - MAC_PARM);
702 #endif
703 else if (isprint(c) || c == '\n' || c == '\t')
704 PUTCHAR(c);
705 else if (c < ' ')
706 fprintf( pCppOut, "<^%c>", c + '@');
707 else
708 fprintf( pCppOut, "<\\0%o>", c);
710 /*ER evaluate macros to pDefOut */
711 #ifdef EVALDEFS
712 if ( bDumpDefs && !bIsInEval && dp->nargs <= 0 )
714 FILEINFO *infileSave = infile;
715 char *tokenSave = savestring( token );
716 char *workSave = savestring( work );
717 int lineSave = line;
718 int wronglineSave = wrongline;
719 int recursionSave = recursion;
720 FILEINFO *file;
721 EVALTYPE valEval;
723 bIsInEval = 1;
724 infile = NULL; /* start from scrap */
725 line = 0;
726 wrongline = 0;
727 *token = EOS;
728 *work = EOS;
729 recursion = 0;
730 file = getfile( strlen( dp->repl ), dp->name );
731 strcpy( file->buffer, dp->repl );
732 fprintf( pCppOut, " ===> ");
733 nEvalOff = 0;
734 cppmain(); /* get() frees also *file */
735 valEval = 0;
736 if ( 0 == evaluate( EvalBuf, &valEval ) )
738 #ifdef EVALFLOATS
739 if ( valEval != (EVALTYPE)((long)valEval ) )
740 fprintf( pCppOut, " ==eval=> %f", valEval );
741 else
742 #endif
743 fprintf( pCppOut, " ==eval=> %ld", (long)valEval );
745 recursion = recursionSave;
746 wrongline = wronglineSave;
747 line = lineSave;
748 strcpy( work, workSave );
749 free( workSave );
750 strcpy( token, tokenSave );
751 free( tokenSave );
752 infile = infileSave;
753 bIsInEval = 0;
755 #endif
757 else {
758 fprintf( pCppOut, ", no replacement.");
760 PUTCHAR('\n');
761 if ( bDumpDefs )
762 pCppOut = pRememberOut;
764 #endif
767 * G E T
771 get()
773 * Return the next character from a macro or the current file.
774 * Handle end of file from #include files.
777 register int c;
778 register FILEINFO *file;
779 register int popped; /* Recursion fixup */
781 popped = 0;
782 get_from_file:
783 if ((file = infile) == NULL)
784 return (EOF_CHAR);
785 newline:
788 * Read a character from the current input line or macro.
789 * At EOS, either finish the current macro (freeing temp.
790 * storage) or read another line from the current input file.
791 * At EOF, exit the current file (#include) or, at EOF from
792 * the cpp input file, return EOF_CHAR to finish processing.
794 if ((c = *file->bptr++ & 0xFF) == EOS) {
796 * Nothing in current line or macro. Get next line (if
797 * input from a file), or do end of file/macro processing.
798 * In the latter case, jump back to restart from the top.
800 if (file->fp == NULL) { /* NULL if macro */
801 popped++;
802 recursion -= file->unrecur;
803 if (recursion < 0)
804 recursion = 0;
805 infile = file->parent; /* Unwind file chain */
807 else { /* Else get from a file */
808 if ((file->bptr = fgets(file->buffer, NBUFF, file->fp))
809 != NULL) {
810 #if OSL_DEBUG_LEVEL > 1
811 if (debug > 1) { /* Dump it to stdout */
812 fprintf( pCppOut, "\n#line %d (%s), %s",
813 line, file->filename, file->buffer);
815 #endif
816 goto newline; /* process the line */
818 else {
819 if( file->fp != stdin )
820 fclose(file->fp); /* Close finished file */
821 if ((infile = file->parent) != NULL) {
823 * There is an "ungotten" newline in the current
824 * infile buffer (set there by doinclude() in
825 * cpp1.c). Thus, we know that the mainline code
826 * is skipping over blank lines and will do a
827 * #line at its convenience.
829 wrongline = TRUE; /* Need a #line now */
834 * Free up space used by the (finished) file or macro and
835 * restart input from the parent file/macro, if any.
837 free(file->filename); /* Free name and */
838 if (file->progname != NULL) /* if a #line was seen, */
839 free(file->progname); /* free it, too. */
840 free((char *) file); /* Free file space */
841 if (infile == NULL) /* If at end of file */
842 return (EOF_CHAR); /* Return end of file */
843 line = infile->line; /* Reset line number */
844 goto get_from_file; /* Get from the top. */
847 * Common processing for the new character.
849 if (c == DEF_MAGIC && file->fp != NULL) /* Don't allow delete */
850 goto newline; /* from a file */
851 if (file->parent != NULL) { /* Macro or #include */
852 if (popped != 0)
853 file->parent->unrecur += popped;
854 else {
855 recursion -= file->parent->unrecur;
856 if (recursion < 0)
857 recursion = 0;
858 file->parent->unrecur = 0;
861 #if (HOST == SYS_UNIX)
862 /*ER*/ if (c == '\r')
863 /*ER*/ return get(); /* DOS fuck */
864 #endif
865 if (c == '\n') /* Maintain current */
866 ++line; /* line counter */
867 if (instring) /* Strings just return */
868 return (c); /* the character. */
869 else if (c == '/') { /* Comment? */
870 instring = TRUE; /* So get() won't loop */
871 /*MM c++ comments */
872 /*MM*/ c = get();
873 /*MM*/ if ((c != '*') && (c != '/')) { /* Next byte '*'? */
874 instring = FALSE; /* Nope, no comment */
875 unget(); /* Push the char. back */
876 return ('/'); /* Return the slash */
878 if (keepcomments) { /* If writing comments */
879 PUTCHAR('/'); /* Write out the */
880 /* initializer */
881 /*MM*/ if( '*' == c )
882 PUTCHAR('*');
883 /*MM*/ else
884 /*MM*/ PUTCHAR('/');
887 /*MM*/ if( '*' == c ){
888 for (;;) { /* Eat a comment */
889 c = get();
890 test: if (keepcomments && c != EOF_CHAR)
891 cput(c);
892 switch (c) {
893 case EOF_CHAR:
894 cerror("EOF in comment", NULLST);
895 return (EOF_CHAR);
897 case '/':
898 if ((c = get()) != '*') /* Don't let comments */
899 goto test; /* Nest. */
900 #ifdef STRICT_COMMENTS
901 cwarn("Nested comments", NULLST);
902 #endif
903 /* Fall into * stuff */
904 case '*':
905 if ((c = get()) != '/') /* If comment doesn't */
906 goto test; /* end, look at next */
907 instring = FALSE; /* End of comment, */
908 if (keepcomments) { /* Put out the comment */
909 cput(c); /* terminator, too */
912 * A comment is syntactically "whitespace" --
913 * however, there are certain strange sequences
914 * such as
915 * #define foo(x) (something)
916 * foo|* comment *|(123)
917 * these are '/' ^ ^
918 * where just returning space (or COM_SEP) will cause
919 * problems. This can be "fixed" by overwriting the
920 * '/' in the input line buffer with ' ' (or COM_SEP)
921 * but that may mess up an error message.
922 * So, we peek ahead -- if the next character is
923 * "whitespace" we just get another character, if not,
924 * we modify the buffer. All in the name of purity.
926 if (*file->bptr == '\n'
927 || type[*file->bptr & 0xFF] == SPA)
928 goto newline;
929 #if COMMENT_INVISIBLE
931 * Return magic (old-fashioned) syntactic space.
933 return ((file->bptr[-1] = COM_SEP));
934 #else
935 return ((file->bptr[-1] = ' '));
936 #endif
938 case '\n': /* we'll need a #line */
939 if (!keepcomments)
940 wrongline = TRUE; /* later... */
941 default: /* Anything else is */
942 break; /* Just a character */
943 } /* End switch */
944 } /* End comment loop */
946 else{ /* c++ comment */
947 /*MM c++ comment*/
948 for (;;) { /* Eat a comment */
949 c = get();
950 if (keepcomments && c != EOF_CHAR)
951 cput(c);
952 if( EOF_CHAR == c )
953 return (EOF_CHAR);
954 else if( '\n' == c ){
955 instring = FALSE; /* End of comment, */
956 return( c );
960 } /* End if in comment */
961 else if (!inmacro && c == '\\') { /* If backslash, peek */
962 if ((c = get()) == '\n') { /* for a <nl>. If so, */
963 wrongline = TRUE;
964 goto newline;
966 else { /* Backslash anything */
967 unget(); /* Get it later */
968 return ('\\'); /* Return the backslash */
971 else if (c == '\f' || c == VT) /* Form Feed, Vertical */
972 c = ' '; /* Tab are whitespace */
973 else if (c == 0xef) /* eat up UTF-8 BOM */
975 if((c = get()) == 0xbb)
977 if((c = get()) == 0xbf)
979 c = get();
980 return c;
982 else
984 unget();
985 unget();
986 return 0xef;
989 else
991 unget();
992 return 0xef;
995 return (c); /* Just return the char */
998 void unget()
1000 * Backup the pointer to reread the last character. Fatal error
1001 * (code bug) if we backup too far. unget() may be called,
1002 * without problems, at end of file. Only one character may
1003 * be ungotten. If you need to unget more, call ungetstring().
1006 register FILEINFO *file;
1008 if ((file = infile) == NULL)
1009 return; /* Unget after EOF */
1010 if (--file->bptr < file->buffer)
1011 cfatal("Too much pushback", NULLST);
1012 if (*file->bptr == '\n') /* Ungetting a newline? */
1013 --line; /* Unget the line number, too */
1016 void ungetstring(char* text)
1018 * Push a string back on the input stream. This is done by treating
1019 * the text as if it were a macro.
1022 register FILEINFO *file;
1023 extern FILEINFO *getfile();
1024 file = getfile(strlen(text) + 1, "");
1025 strcpy(file->buffer, text);
1029 cget()
1031 * Get one character, absorb "funny space" after comments or
1032 * token concatenation
1035 register int c;
1037 do {
1038 c = get();
1039 #if COMMENT_INVISIBLE
1040 } while (c == TOK_SEP || c == COM_SEP);
1041 #else
1042 } while (c == TOK_SEP);
1043 #endif
1044 return (c);
1048 * Error messages and other hacks. The first byte of severity
1049 * is 'S' for string arguments and 'I' for int arguments. This
1050 * is needed for portability with machines that have int's that
1051 * are shorter than char *'s.
1054 static void domsg(char* severity, char* format, void* arg)
1056 * Print filenames, macro names, and line numbers for error messages.
1059 register char *tp;
1060 register FILEINFO *file;
1062 fprintf(stderr, "%sline %d, %s: ", MSG_PREFIX, line, &severity[1]);
1063 if (*severity == 'S')
1064 fprintf(stderr, format, (char *)arg);
1065 else
1066 fprintf(stderr, format, *((int *)arg) );
1067 putc('\n', stderr);
1068 if ((file = infile) == NULL)
1069 return; /* At end of file */
1070 if (file->fp != NULL) {
1071 tp = file->buffer; /* Print current file */
1072 fprintf(stderr, "%s", tp); /* name, making sure */
1073 if (tp[strlen(tp) - 1] != '\n') /* there's a newline */
1074 putc('\n', stderr);
1076 while ((file = file->parent) != NULL) { /* Print #includes, too */
1077 if (file->fp == NULL)
1078 fprintf(stderr, "from macro %s\n", file->filename);
1079 else {
1080 tp = file->buffer;
1081 fprintf(stderr, "from file %s, line %d:\n%s",
1082 (file->progname != NULL)
1083 ? file->progname : file->filename,
1084 file->line, tp);
1085 if (tp[strlen(tp) - 1] != '\n')
1086 putc('\n', stderr);
1091 void cerror(char* format, char* sarg)
1093 * Print a normal error message, string argument.
1096 domsg("SError", format, sarg);
1097 errors++;
1100 void cierror(char* format, int narg)
1102 * Print a normal error message, numeric argument.
1105 domsg("IError", format, &narg);
1106 errors++;
1109 void cfatal(char* format, char* sarg)
1111 * A real disaster
1114 domsg("SFatal error", format, sarg);
1115 exit(IO_ERROR);
1118 void cwarn(char* format, char* sarg)
1120 * A non-fatal error, string argument.
1123 domsg("SWarning", format, sarg);
1126 void ciwarn(char* format, int narg)
1128 * A non-fatal error, numeric argument.
1131 domsg("IWarning", format, &narg);
1134 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */