Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <sal/types.h>
21 : #include <stdio.h>
22 : #include <ctype.h>
23 : #include <string.h>
24 : #include "cppdef.h"
25 : #include "cpp.h"
26 :
27 : /*ER evaluate macros to pDefOut */
28 :
29 : /*
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.
76 : */
77 :
78 : /*
79 : * This table must be rewritten for a non-Ascii machine.
80 : *
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
85 : */
86 : #if TOK_SEP != 0x1E || COM_SEP != 0x1F || DEF_MAGIC != 0x1D
87 : << error type table is not correct >>
88 : #endif
89 :
90 : #if OK_DOLLAR
91 : #define DOL LET
92 : #else
93 : #define DOL 000
94 : #endif
95 :
96 : #ifdef EBCDIC
97 :
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 */
131 : };
132 :
133 : #else
134 :
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 */
160 : };
161 :
162 : #endif
163 :
164 : /*
165 : * C P P S y m b o l T a b l e s
166 : */
167 :
168 : /*
169 : * SBSIZE defines the number of hash-table slots for the symbol table.
170 : * It must be a power of 2.
171 : */
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
179 :
180 :
181 : static DEFBUF *symtab[SBSIZE]; /* Symbol table queue headers */
182 :
183 0 : void InitCpp6()
184 : {
185 : int i;
186 0 : for( i = 0; i < SBSIZE; i++ )
187 0 : symtab[ i ] = NULL;
188 0 : }
189 :
190 :
191 :
192 0 : void skipnl()
193 : /*
194 : * Skip to the end of the current input line.
195 : */
196 : {
197 : register int c;
198 :
199 : do { /* Skip to newline */
200 0 : c = get();
201 0 : } while (c != '\n' && c != EOF_CHAR);
202 0 : }
203 :
204 : int
205 0 : skipws()
206 : /*
207 : * Skip over whitespace
208 : */
209 : {
210 : register int c;
211 :
212 : do { /* Skip whitespace */
213 0 : c = get();
214 : #if COMMENT_INVISIBLE
215 : } while (type[c] == SPA || c == COM_SEP);
216 : #else
217 0 : } while (type[c] == SPA);
218 : #endif
219 0 : return (c);
220 : }
221 :
222 0 : void scanid(int c)
223 : /*
224 : * Get the next token (an id) into the token buffer.
225 : * Note: this code is duplicated in lookid().
226 : * Change one, change both.
227 : */
228 : {
229 : register char *bp;
230 :
231 0 : if (c == DEF_MAGIC) /* Eat the magic token */
232 0 : c = get(); /* undefiner. */
233 0 : bp = token;
234 : do {
235 0 : if (bp < &token[IDMAX]) /* token dim is IDMAX+1 */
236 0 : *bp++ = (char)c;
237 0 : c = get();
238 0 : } while (type[c] == LET || type[c] == DIG);
239 0 : unget();
240 0 : *bp = EOS;
241 0 : }
242 :
243 : int
244 0 : macroid(int c)
245 : /*
246 : * If c is a letter, scan the id. if it's #defined, expand it and scan
247 : * the next character and try again.
248 : *
249 : * Else, return the character. If type[c] is a LET, the token is in token.
250 : */
251 : {
252 : register DEFBUF *dp;
253 :
254 0 : if (infile != NULL && infile->fp != NULL)
255 0 : recursion = 0;
256 0 : while (type[c] == LET && (dp = lookid(c)) != NULL) {
257 0 : expand(dp);
258 0 : c = get();
259 : }
260 0 : return (c);
261 : }
262 :
263 : int
264 0 : catenate()
265 : /*
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.
271 : */
272 : {
273 : register int c;
274 : register char *token1;
275 :
276 : #if OK_CONCAT
277 0 : if (get() != TOK_SEP) { /* Token concatenation */
278 0 : unget();
279 0 : return (FALSE);
280 : }
281 : else {
282 0 : token1 = savestring(token); /* Save first token */
283 0 : c = macroid(get()); /* Scan next token */
284 0 : switch(type[c]) { /* What was it? */
285 : case LET: /* An identifier, ... */
286 0 : if (strlen(token1) + strlen(token) >= NWORK)
287 0 : cfatal("work buffer overflow doing %s #", token1);
288 0 : sprintf(work, "%s%s", token1, token);
289 0 : break;
290 :
291 : case DIG: /* A digit string */
292 0 : strcpy(work, token1);
293 0 : workp = work + strlen(work);
294 : do {
295 0 : save(c);
296 0 : } while ((c = get()) != TOK_SEP);
297 : /*
298 : * The trailing TOK_SEP is no longer needed.
299 : */
300 0 : save(EOS);
301 0 : break;
302 :
303 : default: /* An error, ... */
304 : #if ! COMMENT_INVISIBLE
305 0 : if (isprint(c))
306 0 : cierror("Strange character '%c' after #", c);
307 : else
308 0 : cierror("Strange character (%d.) after #", c);
309 : #endif
310 0 : strcpy(work, token1);
311 0 : unget();
312 0 : break;
313 : }
314 : /*
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.
319 : */
320 0 : free(token1); /* Free up memory */
321 0 : ungetstring(work); /* Unget the new thing, */
322 0 : return (TRUE);
323 : }
324 : #else
325 : return (FALSE); /* Not supported */
326 : #endif
327 : }
328 :
329 : int
330 0 : scanstring(int delim,
331 : #ifndef _NO_PROTO
332 : void (*outfun)( int ) /* BP */ /* Output function */
333 : #else
334 : void (*outfun)() /* BP */
335 : #endif
336 : )
337 : /*
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.
341 : */
342 : {
343 : register int c;
344 :
345 0 : instring = TRUE; /* Don't strip comments */
346 0 : (*outfun)(delim);
347 0 : while ((c = get()) != delim
348 0 : && c != '\n'
349 0 : && c != EOF_CHAR) {
350 :
351 0 : if (c != DEF_MAGIC)
352 0 : (*outfun)(c);
353 0 : if (c == '\\')
354 0 : (*outfun)(get());
355 : }
356 0 : instring = FALSE;
357 0 : if (c == delim) {
358 0 : (*outfun)(c);
359 0 : return (TRUE);
360 : }
361 : else {
362 0 : cerror("Unterminated string", NULLST);
363 0 : unget();
364 0 : return (FALSE);
365 : }
366 : }
367 :
368 0 : 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
374 : )
375 : /*
376 : * Process a number. We know that c is from 0 to 9 or dot.
377 : * Algorithm from Dave Conroy's Decus C.
378 : */
379 : {
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 */
385 :
386 0 : expseen = FALSE; /* No exponent seen yet */
387 0 : signseen = TRUE; /* No +/- allowed yet */
388 0 : octal89 = FALSE; /* No bad octal yet */
389 0 : radix = 10; /* Assume decimal */
390 0 : if ((dotflag = (c == '.')) != FALSE) { /* . something? */
391 0 : (*outfun)('.'); /* Always out the dot */
392 0 : if (type[(c = get())] != DIG) { /* If not a float numb, */
393 0 : unget(); /* Rescan strange char */
394 0 : return; /* All done for now */
395 : }
396 : } /* End of float test */
397 0 : else if (c == '0') { /* Octal or hex? */
398 0 : (*outfun)(c); /* Stuff initial zero */
399 0 : radix = 8; /* Assume it's octal */
400 0 : c = get(); /* Look for an 'x' */
401 0 : if (c == 'x' || c == 'X') { /* Did we get one? */
402 0 : radix = 16; /* Remember new radix */
403 0 : (*outfun)(c); /* Stuff the 'x' */
404 0 : c = get(); /* Get next character */
405 : }
406 : }
407 : for (;;) { /* Process curr. char. */
408 : /*
409 : * Note that this algorithm accepts "012e4" and "03.4"
410 : * as legitimate floating-point numbers.
411 : */
412 0 : if (radix != 16 && (c == 'e' || c == 'E')) {
413 0 : if (expseen) /* Already saw 'E'? */
414 0 : break; /* Exit loop, bad nbr. */
415 0 : expseen = TRUE; /* Set exponent seen */
416 0 : signseen = FALSE; /* We can read '+' now */
417 0 : radix = 10; /* Decimal exponent */
418 : }
419 0 : else if (radix != 16 && c == '.') {
420 0 : if (dotflag) /* Saw dot already? */
421 0 : break; /* Exit loop, two dots */
422 0 : dotflag = TRUE; /* Remember the dot */
423 0 : radix = 10; /* Decimal fraction */
424 : }
425 0 : else if (c == '+' || c == '-') { /* 1.0e+10 */
426 0 : if (signseen) /* Sign in wrong place? */
427 0 : break; /* Exit loop, not nbr. */
428 : /* signseen = TRUE; */ /* Remember we saw it */
429 : }
430 : else { /* Check the digit */
431 0 : switch (c) {
432 : case '8': case '9': /* Sometimes wrong */
433 0 : octal89 = TRUE; /* Do check later */
434 : case '0': case '1': case '2': case '3':
435 : case '4': case '5': case '6': case '7':
436 0 : break; /* Always ok */
437 :
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 0 : if (radix == 16) /* Alpha's are ok only */
441 0 : break; /* if reading hex. */
442 : default: /* At number end */
443 0 : goto done; /* Break from for loop */
444 : } /* End of switch */
445 : } /* End general case */
446 0 : (*outfun)(c); /* Accept the character */
447 0 : signseen = TRUE; /* Don't read sign now */
448 0 : c = get(); /* Read another char */
449 0 : } /* End of scan loop */
450 : /*
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".
457 : */
458 0 : done: if (dotflag || expseen) { /* Floating point? */
459 0 : if (c == 'l' || c == 'L') {
460 0 : (*outfun)(c);
461 0 : get(); /* Ungotten later */
462 : }
463 : }
464 : else { /* Else it's an integer */
465 : /*
466 : * We know that dotflag and expseen are both zero, now:
467 : * dotflag signals "saw 'L'", and
468 : * expseen signals "saw 'U'".
469 : */
470 : for (;;) {
471 0 : switch (c) {
472 : case 'l':
473 : case 'L':
474 0 : if (dotflag)
475 0 : goto nomore;
476 0 : dotflag = TRUE;
477 0 : break;
478 :
479 : case 'u':
480 : case 'U':
481 0 : if (expseen)
482 0 : goto nomore;
483 0 : expseen = TRUE;
484 0 : break;
485 :
486 : default:
487 0 : goto nomore;
488 : }
489 0 : (*outfun)(c); /* Got 'L' or 'U'. */
490 0 : c = get(); /* Look at next, too. */
491 0 : }
492 : }
493 0 : nomore: unget(); /* Not part of a number */
494 0 : if (octal89 && radix == 8)
495 0 : cwarn("Illegal digit in octal number", NULLST);
496 : }
497 :
498 0 : void save(int c)
499 : {
500 0 : if (workp >= &work[NWORK]) {
501 0 : work[NWORK-1] = '\0';
502 0 : cfatal("Work buffer overflow: %s", work);
503 : }
504 0 : else *workp++ = (char)c;
505 0 : }
506 :
507 : char *
508 0 : savestring(char* text)
509 : /*
510 : * Store a string into free memory.
511 : */
512 : {
513 : register char *result;
514 :
515 0 : result = getmem(strlen(text) + 1);
516 0 : strcpy(result, text);
517 0 : return (result);
518 : }
519 :
520 : FILEINFO *
521 0 : getfile(int bufsize, char* name)
522 : /*
523 : * Common FILEINFO buffer initialization for a new file or macro.
524 : */
525 : {
526 : register FILEINFO *file;
527 : register int size;
528 :
529 0 : size = strlen(name); /* File/macro name */
530 0 : file = (FILEINFO *) getmem(sizeof (FILEINFO) + bufsize + size);
531 0 : file->parent = infile; /* Chain files together */
532 0 : file->fp = NULL; /* No file yet */
533 0 : file->filename = savestring(name); /* Save file/macro name */
534 0 : file->progname = NULL; /* No #line seen yet */
535 0 : file->unrecur = 0; /* No macro fixup */
536 0 : file->bptr = file->buffer; /* Initialize line ptr */
537 0 : file->buffer[0] = EOS; /* Force first read */
538 0 : file->line = 0; /* (Not used just yet) */
539 0 : if (infile != NULL) /* If #include file */
540 0 : infile->line = line; /* Save current line */
541 0 : infile = file; /* New current file */
542 0 : line = 1; /* Note first line */
543 0 : return (file); /* All done. */
544 : }
545 :
546 : char *
547 0 : getmem(int size)
548 : /*
549 : * Get a block of free memory.
550 : */
551 : {
552 : register char *result;
553 :
554 0 : if ((result = malloc((unsigned) size)) == NULL)
555 0 : cfatal("Out of memory", NULLST);
556 0 : return (result);
557 : }
558 :
559 : DEFBUF *
560 0 : lookid(int c)
561 : /*
562 : * Look for the next token in the symbol table. Returns token in "token".
563 : * If found, returns the table pointer; Else returns NULL.
564 : */
565 : {
566 : register int nhash;
567 : register DEFBUF *dp;
568 : register char *np;
569 0 : int temp = 0;
570 : int isrecurse; /* For #define foo foo */
571 :
572 0 : np = token;
573 0 : nhash = 0;
574 0 : if (0 != (isrecurse = (c == DEF_MAGIC))) /* If recursive macro */
575 0 : c = get(); /* hack, skip DEF_MAGIC */
576 : do {
577 0 : if (np < &token[IDMAX]) { /* token dim is IDMAX+1 */
578 0 : *np++ = (char)c; /* Store token byte */
579 0 : nhash += c; /* Update hash value */
580 : }
581 0 : c = get(); /* And get another byte */
582 0 : } while (type[c] == LET || type[c] == DIG);
583 0 : unget(); /* Rescan terminator */
584 0 : *np = EOS; /* Terminate token */
585 0 : if (isrecurse) /* Recursive definition */
586 0 : return (NULL); /* undefined just now */
587 0 : nhash += (np - token); /* Fix hash value */
588 0 : dp = symtab[nhash & SBMASK]; /* Starting bucket */
589 0 : while (dp != (DEFBUF *) NULL) { /* Search symbol table */
590 0 : if (dp->hash == nhash /* Fast precheck */
591 0 : && (temp = strcmp(dp->name, token)) >= 0)
592 0 : break;
593 0 : dp = dp->link; /* Nope, try next one */
594 : }
595 0 : return ((temp == 0) ? dp : NULL);
596 : }
597 :
598 : DEFBUF *
599 0 : defendel(char* name, int delete)
600 : /*
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).
605 : */
606 : {
607 : register DEFBUF *dp;
608 : register DEFBUF **prevp;
609 : register char *np;
610 : int nhash;
611 : int temp;
612 : int size;
613 :
614 0 : for (nhash = 0, np = name; *np != EOS;)
615 0 : nhash += *np++;
616 0 : size = (np - name);
617 0 : nhash += size;
618 0 : prevp = &symtab[nhash & SBMASK];
619 0 : while ((dp = *prevp) != (DEFBUF *) NULL) {
620 0 : if (dp->hash == nhash
621 0 : && (temp = strcmp(dp->name, name)) >= 0) {
622 0 : if (temp > 0)
623 0 : dp = NULL; /* Not found */
624 : else {
625 0 : *prevp = dp->link; /* Found, unlink and */
626 0 : if (dp->repl != NULL) /* Free the replacement */
627 0 : free(dp->repl); /* if any, and then */
628 0 : free((char *) dp); /* Free the symbol */
629 0 : dp = NULL;
630 : }
631 0 : break;
632 : }
633 0 : prevp = &dp->link;
634 : }
635 0 : if (!delete) {
636 0 : dp = (DEFBUF *) getmem(sizeof (DEFBUF) + size);
637 0 : dp->link = *prevp;
638 0 : *prevp = dp;
639 0 : dp->hash = nhash;
640 0 : dp->repl = NULL;
641 0 : dp->nargs = 0;
642 0 : strcpy(dp->name, name);
643 : }
644 0 : return (dp);
645 : }
646 :
647 : #if OSL_DEBUG_LEVEL > 1
648 :
649 : void dumpdef(char *why)
650 : {
651 : register DEFBUF *dp;
652 : register DEFBUF **syp;
653 : FILE *pRememberOut = NULL;
654 :
655 : if ( bDumpDefs ) /*ER */
656 : {
657 : pRememberOut = pCppOut;
658 : pCppOut = pDefOut;
659 : }
660 : fprintf( pCppOut, "CPP symbol table dump %s\n", why);
661 : for (syp = symtab; syp < &symtab[SBSIZE]; syp++) {
662 : if ((dp = *syp) != (DEFBUF *) NULL) {
663 : fprintf( pCppOut, "symtab[%" SAL_PRI_PTRDIFFT "d]\n", (syp - symtab));
664 : do {
665 : dumpadef((char *) NULL, dp);
666 : } while ((dp = dp->link) != (DEFBUF *) NULL);
667 : }
668 : }
669 : if ( bDumpDefs )
670 : {
671 : fprintf( pCppOut, "\n");
672 : pCppOut = pRememberOut;
673 : }
674 : }
675 :
676 : void dumpadef(char *why, register DEFBUF *dp)
677 : {
678 : register char *cp;
679 : register int c;
680 : FILE *pRememberOut = NULL;
681 :
682 : /*ER dump #define's to pDefOut */
683 : if ( bDumpDefs )
684 : {
685 : pRememberOut = pCppOut;
686 : pCppOut = pDefOut;
687 : }
688 : fprintf( pCppOut, " \"%s\" [%d]", dp->name, dp->nargs);
689 : if (why != NULL)
690 : fprintf( pCppOut, " (%s)", why);
691 : if (dp->repl != NULL) {
692 : fprintf( pCppOut, " => ");
693 : for (cp = dp->repl; (c = *cp++ & 0xFF) != EOS;) {
694 : #ifdef SOLAR
695 : if (c == DEL) {
696 : c = *cp++ & 0xFF;
697 : if( c == EOS ) break;
698 : fprintf( pCppOut, "<%%%d>", c - MAC_PARM);
699 : }
700 : #else
701 : if (c >= MAC_PARM && c <= (MAC_PARM + PAR_MAC))
702 : fprintf( pCppOut, "<%%%d>", c - MAC_PARM);
703 : #endif
704 : else if (isprint(c) || c == '\n' || c == '\t')
705 : PUTCHAR(c);
706 : else if (c < ' ')
707 : fprintf( pCppOut, "<^%c>", c + '@');
708 : else
709 : fprintf( pCppOut, "<\\0%o>", c);
710 : }
711 : /*ER evaluate macros to pDefOut */
712 : #ifdef EVALDEFS
713 : if ( bDumpDefs && !bIsInEval && dp->nargs <= 0 )
714 : {
715 : FILEINFO *infileSave = infile;
716 : char *tokenSave = savestring( token );
717 : char *workSave = savestring( work );
718 : int lineSave = line;
719 : int wronglineSave = wrongline;
720 : int recursionSave = recursion;
721 : FILEINFO *file;
722 : EVALTYPE valEval;
723 :
724 : bIsInEval = 1;
725 : infile = NULL; /* start from scrap */
726 : line = 0;
727 : wrongline = 0;
728 : *token = EOS;
729 : *work = EOS;
730 : recursion = 0;
731 : file = getfile( strlen( dp->repl ), dp->name );
732 : strcpy( file->buffer, dp->repl );
733 : fprintf( pCppOut, " ===> ");
734 : nEvalOff = 0;
735 : cppmain(); /* get() frees also *file */
736 : valEval = 0;
737 : if ( 0 == evaluate( EvalBuf, &valEval ) )
738 : {
739 : #ifdef EVALFLOATS
740 : if ( valEval != (EVALTYPE)((long)valEval ) )
741 : fprintf( pCppOut, " ==eval=> %f", valEval );
742 : else
743 : #endif
744 : fprintf( pCppOut, " ==eval=> %ld", (long)valEval );
745 : }
746 : recursion = recursionSave;
747 : wrongline = wronglineSave;
748 : line = lineSave;
749 : strcpy( work, workSave );
750 : free( workSave );
751 : strcpy( token, tokenSave );
752 : free( tokenSave );
753 : infile = infileSave;
754 : bIsInEval = 0;
755 : }
756 : #endif
757 : }
758 : else {
759 : fprintf( pCppOut, ", no replacement.");
760 : }
761 : PUTCHAR('\n');
762 : if ( bDumpDefs )
763 : pCppOut = pRememberOut;
764 : }
765 : #endif
766 :
767 : /*
768 : * G E T
769 : */
770 :
771 : int
772 0 : get()
773 : /*
774 : * Return the next character from a macro or the current file.
775 : * Handle end of file from #include files.
776 : */
777 : {
778 : register int c;
779 : register FILEINFO *file;
780 : register int popped; /* Recursion fixup */
781 :
782 0 : popped = 0;
783 : get_from_file:
784 0 : if ((file = infile) == NULL)
785 0 : return (EOF_CHAR);
786 : newline:
787 :
788 : /*
789 : * Read a character from the current input line or macro.
790 : * At EOS, either finish the current macro (freeing temp.
791 : * storage) or read another line from the current input file.
792 : * At EOF, exit the current file (#include) or, at EOF from
793 : * the cpp input file, return EOF_CHAR to finish processing.
794 : */
795 0 : if ((c = *file->bptr++ & 0xFF) == EOS) {
796 : /*
797 : * Nothing in current line or macro. Get next line (if
798 : * input from a file), or do end of file/macro processing.
799 : * In the latter case, jump back to restart from the top.
800 : */
801 0 : if (file->fp == NULL) { /* NULL if macro */
802 0 : popped++;
803 0 : recursion -= file->unrecur;
804 0 : if (recursion < 0)
805 0 : recursion = 0;
806 0 : infile = file->parent; /* Unwind file chain */
807 : }
808 : else { /* Else get from a file */
809 0 : if ((file->bptr = fgets(file->buffer, NBUFF, file->fp))
810 : != NULL) {
811 : #if OSL_DEBUG_LEVEL > 1
812 : if (debug > 1) { /* Dump it to stdout */
813 : fprintf( pCppOut, "\n#line %d (%s), %s",
814 : line, file->filename, file->buffer);
815 : }
816 : #endif
817 0 : goto newline; /* process the line */
818 : }
819 : else {
820 0 : if( file->fp != stdin )
821 0 : fclose(file->fp); /* Close finished file */
822 0 : if ((infile = file->parent) != NULL) {
823 : /*
824 : * There is an "ungotten" newline in the current
825 : * infile buffer (set there by doinclude() in
826 : * cpp1.c). Thus, we know that the mainline code
827 : * is skipping over blank lines and will do a
828 : * #line at its convenience.
829 : */
830 0 : wrongline = TRUE; /* Need a #line now */
831 : }
832 : }
833 : }
834 : /*
835 : * Free up space used by the (finished) file or macro and
836 : * restart input from the parent file/macro, if any.
837 : */
838 0 : free(file->filename); /* Free name and */
839 0 : if (file->progname != NULL) /* if a #line was seen, */
840 0 : free(file->progname); /* free it, too. */
841 0 : free((char *) file); /* Free file space */
842 0 : if (infile == NULL) /* If at end of file */
843 0 : return (EOF_CHAR); /* Return end of file */
844 0 : line = infile->line; /* Reset line number */
845 0 : goto get_from_file; /* Get from the top. */
846 : }
847 : /*
848 : * Common processing for the new character.
849 : */
850 0 : if (c == DEF_MAGIC && file->fp != NULL) /* Don't allow delete */
851 0 : goto newline; /* from a file */
852 0 : if (file->parent != NULL) { /* Macro or #include */
853 0 : if (popped != 0)
854 0 : file->parent->unrecur += popped;
855 : else {
856 0 : recursion -= file->parent->unrecur;
857 0 : if (recursion < 0)
858 0 : recursion = 0;
859 0 : file->parent->unrecur = 0;
860 : }
861 : }
862 : #if (HOST == SYS_UNIX)
863 0 : /*ER*/ if (c == '\r')
864 0 : /*ER*/ return get(); /* DOS fuck */
865 : #endif
866 0 : if (c == '\n') /* Maintain current */
867 0 : ++line; /* line counter */
868 0 : if (instring) /* Strings just return */
869 0 : return (c); /* the character. */
870 0 : else if (c == '/') { /* Comment? */
871 0 : instring = TRUE; /* So get() won't loop */
872 : /*MM c++ comments */
873 0 : /*MM*/ c = get();
874 0 : /*MM*/ if ((c != '*') && (c != '/')) { /* Next byte '*'? */
875 0 : instring = FALSE; /* Nope, no comment */
876 0 : unget(); /* Push the char. back */
877 0 : return ('/'); /* Return the slash */
878 : }
879 0 : if (keepcomments) { /* If writing comments */
880 0 : PUTCHAR('/'); /* Write out the */
881 : /* initializer */
882 0 : /*MM*/ if( '*' == c )
883 0 : PUTCHAR('*');
884 : /*MM*/ else
885 0 : /*MM*/ PUTCHAR('/');
886 :
887 : }
888 0 : /*MM*/ if( '*' == c ){
889 : for (;;) { /* Eat a comment */
890 0 : c = get();
891 0 : test: if (keepcomments && c != EOF_CHAR)
892 0 : cput(c);
893 0 : switch (c) {
894 : case EOF_CHAR:
895 0 : cerror("EOF in comment", NULLST);
896 0 : return (EOF_CHAR);
897 :
898 : case '/':
899 0 : if ((c = get()) != '*') /* Don't let comments */
900 0 : goto test; /* Nest. */
901 : #ifdef STRICT_COMMENTS
902 : cwarn("Nested comments", NULLST);
903 : #endif
904 : /* Fall into * stuff */
905 : case '*':
906 0 : if ((c = get()) != '/') /* If comment doesn't */
907 0 : goto test; /* end, look at next */
908 0 : instring = FALSE; /* End of comment, */
909 0 : if (keepcomments) { /* Put out the comment */
910 0 : cput(c); /* terminator, too */
911 : }
912 : /*
913 : * A comment is syntactically "whitespace" --
914 : * however, there are certain strange sequences
915 : * such as
916 : * #define foo(x) (something)
917 : * foo|* comment *|(123)
918 : * these are '/' ^ ^
919 : * where just returning space (or COM_SEP) will cause
920 : * problems. This can be "fixed" by overwriting the
921 : * '/' in the input line buffer with ' ' (or COM_SEP)
922 : * but that may mess up an error message.
923 : * So, we peek ahead -- if the next character is
924 : * "whitespace" we just get another character, if not,
925 : * we modify the buffer. All in the name of purity.
926 : */
927 0 : if (*file->bptr == '\n'
928 0 : || type[*file->bptr & 0xFF] == SPA)
929 : goto newline;
930 : #if COMMENT_INVISIBLE
931 : /*
932 : * Return magic (old-fashioned) syntactic space.
933 : */
934 : return ((file->bptr[-1] = COM_SEP));
935 : #else
936 0 : return ((file->bptr[-1] = ' '));
937 : #endif
938 :
939 : case '\n': /* we'll need a #line */
940 0 : if (!keepcomments)
941 0 : wrongline = TRUE; /* later... */
942 : default: /* Anything else is */
943 0 : break; /* Just a character */
944 : } /* End switch */
945 0 : } /* End comment loop */
946 : }
947 : else{ /* c++ comment */
948 : /*MM c++ comment*/
949 : for (;;) { /* Eat a comment */
950 0 : c = get();
951 0 : if (keepcomments && c != EOF_CHAR)
952 0 : cput(c);
953 0 : if( EOF_CHAR == c )
954 0 : return (EOF_CHAR);
955 0 : else if( '\n' == c ){
956 0 : instring = FALSE; /* End of comment, */
957 0 : return( c );
958 : }
959 0 : }
960 : }
961 : } /* End if in comment */
962 0 : else if (!inmacro && c == '\\') { /* If backslash, peek */
963 0 : if ((c = get()) == '\n') { /* for a <nl>. If so, */
964 0 : wrongline = TRUE;
965 0 : goto newline;
966 : }
967 : else { /* Backslash anything */
968 0 : unget(); /* Get it later */
969 0 : return ('\\'); /* Return the backslash */
970 : }
971 : }
972 0 : else if (c == '\f' || c == VT) /* Form Feed, Vertical */
973 0 : c = ' '; /* Tab are whitespace */
974 0 : else if (c == 0xef) /* eat up UTF-8 BOM */
975 : {
976 0 : if((c = get()) == 0xbb)
977 : {
978 0 : if((c = get()) == 0xbf)
979 : {
980 0 : c = get();
981 0 : return c;
982 : }
983 : else
984 : {
985 0 : unget();
986 0 : unget();
987 0 : return 0xef;
988 : }
989 : }
990 : else
991 : {
992 0 : unget();
993 0 : return 0xef;
994 : }
995 : }
996 0 : return (c); /* Just return the char */
997 : }
998 :
999 0 : void unget()
1000 : /*
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().
1005 : */
1006 : {
1007 : register FILEINFO *file;
1008 :
1009 0 : if ((file = infile) == NULL)
1010 0 : return; /* Unget after EOF */
1011 0 : if (--file->bptr < file->buffer)
1012 0 : cfatal("Too much pushback", NULLST);
1013 0 : if (*file->bptr == '\n') /* Ungetting a newline? */
1014 0 : --line; /* Unget the line number, too */
1015 : }
1016 :
1017 0 : void ungetstring(char* text)
1018 : /*
1019 : * Push a string back on the input stream. This is done by treating
1020 : * the text as if it were a macro.
1021 : */
1022 : {
1023 : register FILEINFO *file;
1024 : extern FILEINFO *getfile();
1025 0 : file = getfile(strlen(text) + 1, "");
1026 0 : strcpy(file->buffer, text);
1027 0 : }
1028 :
1029 : int
1030 0 : cget()
1031 : /*
1032 : * Get one character, absorb "funny space" after comments or
1033 : * token concatenation
1034 : */
1035 : {
1036 : register int c;
1037 :
1038 : do {
1039 0 : c = get();
1040 : #if COMMENT_INVISIBLE
1041 : } while (c == TOK_SEP || c == COM_SEP);
1042 : #else
1043 0 : } while (c == TOK_SEP);
1044 : #endif
1045 0 : return (c);
1046 : }
1047 :
1048 : /*
1049 : * Error messages and other hacks. The first byte of severity
1050 : * is 'S' for string arguments and 'I' for int arguments. This
1051 : * is needed for portability with machines that have int's that
1052 : * are shorter than char *'s.
1053 : */
1054 :
1055 0 : static void domsg(char* severity, char* format, void* arg)
1056 : /*
1057 : * Print filenames, macro names, and line numbers for error messages.
1058 : */
1059 : {
1060 : register char *tp;
1061 : register FILEINFO *file;
1062 :
1063 0 : fprintf(stderr, "%sline %d, %s: ", MSG_PREFIX, line, &severity[1]);
1064 0 : if (*severity == 'S')
1065 0 : fprintf(stderr, format, (char *)arg);
1066 : else
1067 0 : fprintf(stderr, format, *((int *)arg) );
1068 0 : putc('\n', stderr);
1069 0 : if ((file = infile) == NULL)
1070 0 : return; /* At end of file */
1071 0 : if (file->fp != NULL) {
1072 0 : tp = file->buffer; /* Print current file */
1073 0 : fprintf(stderr, "%s", tp); /* name, making sure */
1074 0 : if (tp[strlen(tp) - 1] != '\n') /* there's a newline */
1075 0 : putc('\n', stderr);
1076 : }
1077 0 : while ((file = file->parent) != NULL) { /* Print #includes, too */
1078 0 : if (file->fp == NULL)
1079 0 : fprintf(stderr, "from macro %s\n", file->filename);
1080 : else {
1081 0 : tp = file->buffer;
1082 0 : fprintf(stderr, "from file %s, line %d:\n%s",
1083 0 : (file->progname != NULL)
1084 : ? file->progname : file->filename,
1085 : file->line, tp);
1086 0 : if (tp[strlen(tp) - 1] != '\n')
1087 0 : putc('\n', stderr);
1088 : }
1089 : }
1090 : }
1091 :
1092 0 : void cerror(char* format, char* sarg)
1093 : /*
1094 : * Print a normal error message, string argument.
1095 : */
1096 : {
1097 0 : domsg("SError", format, sarg);
1098 0 : errors++;
1099 0 : }
1100 :
1101 0 : void cierror(char* format, int narg)
1102 : /*
1103 : * Print a normal error message, numeric argument.
1104 : */
1105 : {
1106 0 : domsg("IError", format, &narg);
1107 0 : errors++;
1108 0 : }
1109 :
1110 0 : void cfatal(char* format, char* sarg)
1111 : /*
1112 : * A real disaster
1113 : */
1114 : {
1115 0 : domsg("SFatal error", format, sarg);
1116 0 : exit(IO_ERROR);
1117 : }
1118 :
1119 0 : void cwarn(char* format, char* sarg)
1120 : /*
1121 : * A non-fatal error, string argument.
1122 : */
1123 : {
1124 0 : domsg("SWarning", format, sarg);
1125 0 : }
1126 :
1127 0 : void ciwarn(char* format, int narg)
1128 : /*
1129 : * A non-fatal error, numeric argument.
1130 : */
1131 : {
1132 0 : domsg("IWarning", format, &narg);
1133 0 : }
1134 :
1135 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|