1 /*-
2 * Copyright (c) 1998, 2002-2008 Kiyoshi Matsui <kmatsui@t3.rim.or.jp>
3 * All rights reserved.
4 *
5 * Some parts of this code are derived from the public domain software
6 * DECUS cpp (1984,1985) written by Martin Minow.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 /*
31 * S U P P O R T . C
32 * S u p p o r t R o u t i n e s
33 *
34 * The common routines used by several source files are placed here.
35 */
36
37 /*
38 * The following are global functions.
39 *
40 * get_unexpandable() Gets the next unexpandable token in the line, expanding
41 * macros.
42 * Called from #if, #line and #include processing routines.
43 * skip_nl() Skips over a line.
44 * skip_ws() Skips over white spaces but not skip over the end of the line.
45 * skip_ws() skips also COM_SEP and TOK_SEP.
46 * scan_token() Reads the next token of any type into the specified output
47 * pointer, advances the pointer, returns the type of token.
48 * scan_quote() Reads a string literal, character constant or header-name from
49 * the input stream, writes out to the specified buffer and
50 * returns the advanced output pointer.
51 * get_ch() Reads the next byte from the current input stream, handling
52 * end of (macro/file) input and embedded comments appropriately.
53 * cnv_trigraph() Maps trigraph sequence to C character.
54 * cnv_digraph() Maps digraph sequence to C character.
55 * id_operator() See whether the identifier is an operator in C++.
56 * unget_ch() Pushs last gotten character back on the input stream.
57 * unget_string() Pushs sequence on the input stream.
58 * save_string() Saves a string in malloc() memory.
59 * get_file() Initializes a new FILEINFO structure, called when #include
60 * opens a new file, or from unget_string().
61 * xmalloc() Gets a specified number of bytes from heap memory.
62 * If malloc() returns NULL, exits with a message.
63 * xrealloc() realloc(). If it fails, exits with a message.
64 * get_src_location() Trace back line-column datum into pre-line-splicing
65 * phase. A function for -K option.
66 * cfatal(), cerror(), cwarn()
67 * These routines format print messages to the user.
68 * mcpp_fputc(), mcpp_fputs(), mcpp_fprintf()
69 * Wrap library functions to support alternate output to memory
70 * buffer.
71 */
72
73 #if PREPROCESSED
74 #include "mcpp.H"
75 #else
76 #include "system.H"
77 #include "internal.H"
78 #endif
79
80 static void scan_id( int c);
81 /* Scan an identifier */
82 static char * scan_number( int c, char * out, char * out_end);
83 /* Scan a preprocessing number */
84 static char * scan_number_prestd( int c, char * out, char * out_end);
85 /* scan_number() for pre-Standard mode */
86 #if OK_UCN
87 static char * scan_ucn( int cnt, char * out);
88 /* Scan an UCN sequence */
89 #endif
90 static char * scan_op( int c, char * out);
91 /* Scan an operator or a punctuator */
92 static char * parse_line( void);
93 /* Parse a logical line and convert comments */
94 static char * read_a_comment( char * sp, size_t * sizp);
95 /* Read over a comment */
96 static char * get_line( int in_comment);
97 /* Get a logical line from file, handle line-splicing */
98 static char * at_eof( int in_comment);
99 /* Check erroneous end of file */
100 static void do_msg( const char * severity, const char * format
101 , const char * arg1, long arg2, const char * arg3);
102 /* Putout diagnostic message */
103 static char * cat_line( int del_bsl);
104 /* Splice the line */
105 static void put_line( char * out, FILE * fp);
106 /* Put out a logical line */
107 static void dump_token( int token_type, const char * cp);
108 /* Dump a token and its type */
109
110 #define EXP_MAC_IND_MAX 16
111 /* Information of current expanding macros for diagnostic */
112 static struct {
113 const char * name; /* Name of the macro just expanded */
114 int to_be_freed; /* Name should be freed later */
115 } expanding_macro[ EXP_MAC_IND_MAX];
116 static int exp_mac_ind = 0; /* Index into expanding_macro[] */
117
118 static int in_token = FALSE; /* For token scanning functions */
119 static int in_string = FALSE; /* For get_ch() and parse_line()*/
120 static int squeezews = FALSE;
121
122 #define MAX_CAT_LINE 256
123 /* Information on line catenated by <backslash><newline> */
124 /* and by line-crossing comment. This is for -K option. */
125 typedef struct catenated_line {
126 long start_line; /* Starting line of catenation */
127 long last_line; /* Ending line of catanation */
128 size_t len[ MAX_CAT_LINE + 1];
129 /* Length of successively catenated lines */
130 } CAT_LINE;
131 static CAT_LINE bsl_cat_line;
132 /* Datum on the last catenated line by <backslash><newline> */
133 static CAT_LINE com_cat_line;
134 /* Datum on the last catenated line by a line-crossing comment */
135
136 #if MCPP_LIB
137 static int use_mem_buffers = FALSE;
138
init_support(void)139 void init_support( void)
140 {
141 in_token = in_string = squeezews = FALSE;
142 bsl_cat_line.len[ 0] = com_cat_line.len[ 0] = 0;
143 clear_exp_mac();
144 }
145
146 typedef struct mem_buf {
147 char * buffer;
148 char * entry_pt;
149 size_t size;
150 size_t bytes_avail;
151 } MEMBUF;
152
153 static MEMBUF mem_buffers[ NUM_OUTDEST];
154
mcpp_use_mem_buffers(int tf)155 void mcpp_use_mem_buffers(
156 int tf
157 )
158 {
159 int i;
160
161 use_mem_buffers = tf ? TRUE : FALSE;
162
163 for (i = 0; i < NUM_OUTDEST; ++i) {
164 if (mem_buffers[ i].buffer)
165 /* Free previously allocated memory buffer */
166 free( mem_buffers[ i].buffer);
167 if (use_mem_buffers) {
168 /* Output to memory buffers instead of files */
169 mem_buffers[ i].buffer = NULL;
170 mem_buffers[ i].entry_pt = NULL;
171 mem_buffers[ i].size = 0;
172 mem_buffers[ i].bytes_avail = 0;
173 }
174 }
175 }
176
using_mem_buffers(void)177 int using_mem_buffers( void)
178 {
179 return use_mem_buffers;
180 }
181
182 #define BUF_INCR_SIZE (NWORK * 2)
183 #define MAX( a, b) (((a) > (b)) ? (a) : (b))
184
append_to_buffer(MEMBUF * mem_buf_p,const char * string,size_t length)185 static char * append_to_buffer(
186 MEMBUF * mem_buf_p,
187 const char * string,
188 size_t length
189 )
190 {
191 if (mem_buf_p->bytes_avail < length + 1) { /* Need to allocate more memory */
192 size_t size = MAX( BUF_INCR_SIZE, length);
193
194 if (mem_buf_p->buffer == NULL) { /* 1st append */
195 mem_buf_p->size = size;
196 mem_buf_p->bytes_avail = size;
197 mem_buf_p->buffer = xmalloc( mem_buf_p->size);
198 mem_buf_p->entry_pt = mem_buf_p->buffer;
199 } else {
200 mem_buf_p->size += size;
201 mem_buf_p->bytes_avail += size;
202 mem_buf_p->buffer = xrealloc( mem_buf_p->buffer, mem_buf_p->size);
203 mem_buf_p->entry_pt = mem_buf_p->buffer + mem_buf_p->size
204 - mem_buf_p->bytes_avail;
205 }
206 }
207
208 /* Append the string to the tail of the buffer */
209 memcpy( mem_buf_p->entry_pt, string, length);
210 mem_buf_p->entry_pt += length;
211 mem_buf_p->entry_pt[ 0] = '\0'; /* Terminate the string buffer */
212 mem_buf_p->bytes_avail -= length;
213
214 return mem_buf_p->buffer;
215 }
216
mem_putc(int c,OUTDEST od)217 static int mem_putc(
218 int c,
219 OUTDEST od
220 )
221 {
222 char string[ 1];
223
224 string[ 0] = (char) c;
225
226 if (append_to_buffer( &(mem_buffers[ od]), string, 1) != NULL)
227 return 0;
228 else
229 return !0;
230 }
231
mem_puts(const char * s,OUTDEST od)232 static int mem_puts(
233 const char * s,
234 OUTDEST od
235 )
236 {
237 if (append_to_buffer( &(mem_buffers[od]), s, strlen(s)) != NULL)
238 return 0;
239 else
240 return !0;
241 }
242
mcpp_get_mem_buffer(OUTDEST od)243 char * mcpp_get_mem_buffer(
244 OUTDEST od
245 )
246 {
247 return mem_buffers[ od].buffer;
248 }
249
250 #endif /* MCPP_LIB */
251
252 #define DEST2FP(od) \
253 (od == OUT) ? fp_out : \
254 ((od == ERR) ? fp_err : \
255 ((od == DBG) ? fp_debug : \
256 (NULL)))
257
258 /*
259 * The following mcpp_*() wrapper functions are intended to centralize
260 * the output generated by MCPP. They support memory buffer alternates to
261 * each of the primary output streams: out, err, debug. The memory buffer
262 * output option would be used in a setup where MCPP has been built as a
263 * function call - i.e. mcpp_lib_main().
264 */
265
mcpp_lib_fputc(int c,OUTDEST od)266 int mcpp_lib_fputc(
267 int c,
268 OUTDEST od
269 )
270 {
271 #if MCPP_LIB
272 if (use_mem_buffers) {
273 return mem_putc( c, od);
274 } else {
275 #endif
276 FILE * stream = DEST2FP( od);
277
278 return (stream != NULL) ? fputc( c, stream) : EOF;
279 #if MCPP_LIB
280 }
281 #endif
282 }
283
284 int (* mcpp_fputc)( int c, OUTDEST od) = mcpp_lib_fputc;
285
mcpp_lib_fputs(const char * s,OUTDEST od)286 int mcpp_lib_fputs(
287 const char * s,
288 OUTDEST od
289 )
290 {
291 #if MCPP_LIB
292 if (use_mem_buffers) {
293 return mem_puts( s, od);
294 } else {
295 #endif
296 FILE * stream = DEST2FP( od);
297
298 return (stream != NULL) ? fputs( s, stream) : EOF;
299 #if MCPP_LIB
300 }
301 #endif
302 }
303
304 int (* mcpp_fputs)( const char * s, OUTDEST od) = mcpp_lib_fputs;
305
306 #include <stdarg.h>
307
mcpp_lib_fprintf(OUTDEST od,const char * format,...)308 int mcpp_lib_fprintf(
309 OUTDEST od,
310 const char * format,
311 ...
312 )
313 {
314 va_list ap;
315 FILE * stream = DEST2FP( od);
316
317 if (stream != NULL) {
318 int rc;
319
320 va_start( ap, format);
321 #if MCPP_LIB
322 if (use_mem_buffers) {
323 static char mem_buffer[ NWORK];
324
325 rc = vsprintf( mem_buffer, format, ap);
326
327 if (rc != 0) {
328 rc = mem_puts( mem_buffer, od);
329 }
330 } else {
331 #endif
332 rc = vfprintf( stream, format, ap);
333 #if MCPP_LIB
334 }
335 #endif
336 va_end( ap);
337
338 return rc;
339
340 } else {
341 return EOF;
342 }
343 }
344
345 int (* mcpp_fprintf)( OUTDEST od, const char * format, ...) = mcpp_lib_fprintf;
346
347 #if MCPP_LIB
mcpp_reset_def_out_func(void)348 void mcpp_reset_def_out_func( void)
349 {
350 mcpp_fputc = mcpp_lib_fputc;
351 mcpp_fputs = mcpp_lib_fputs;
352 mcpp_fprintf = mcpp_lib_fprintf;
353 }
354
mcpp_set_out_func(int (* func_fputc)(int c,OUTDEST od),int (* func_fputs)(const char * s,OUTDEST od),int (* func_fprintf)(OUTDEST od,const char * format,...))355 void mcpp_set_out_func(
356 int (* func_fputc)( int c, OUTDEST od),
357 int (* func_fputs)( const char * s, OUTDEST od),
358 int (* func_fprintf)( OUTDEST od, const char * format, ...)
359 )
360 {
361 mcpp_fputc = func_fputc;
362 mcpp_fputs = func_fputs;
363 mcpp_fprintf = func_fprintf;
364 }
365 #endif
366
get_unexpandable(int c,int diag)367 int get_unexpandable(
368 int c, /* First char of token */
369 int diag /* Flag of diagnosis */
370 )
371 /*
372 * Get the next unexpandable token in the line, expanding macros.
373 * Return the token type. The token is written in work_buf[].
374 * The once expanded macro is never expanded again.
375 * Called only from the routines processing #if (#elif, #assert), #line and
376 * #include directives in order to diagnose some subtle macro expansions.
377 */
378 {
379 DEFBUF * defp = NULL;
380 FILEINFO * file;
381 FILE * fp = NULL;
382 LINE_COL line_col = { 0L, 0};
383 int token_type = NO_TOKEN;
384 int has_pragma;
385
386 while (c != EOS && c != '\n' /* In a line */
387 && (fp = infile->fp /* Preserve current state */
388 , (token_type
389 = scan_token( c, (workp = work_buf, &workp), work_end))
390 == NAM) /* Identifier */
391 && fp != NULL /* In source ! */
392 && (defp = is_macro( NULL)) != NULL) { /* Macro */
393 expand_macro( defp, work_buf, work_end, line_col, & has_pragma);
394 /* Expand macro */
395 if (has_pragma)
396 cerror( "_Pragma operator found in directive line" /* _E_ */
397 , NULL, 0L, NULL);
398 file = unget_string( work_buf, defp->name); /* Stack to re-read */
399 c = skip_ws(); /* Skip TOK_SEP */
400 if (file != infile && macro_line != MACRO_ERROR && (warn_level & 1)) {
401 /* This diagnostic is issued even if "diag" is FALSE. */
402 cwarn( "Macro \"%s\" is expanded to 0 token" /* _W1_ */
403 , defp->name, 0L, NULL);
404 if (! option_flags.no_source_line)
405 dump_a_def( " macro", defp, FALSE, TRUE, fp_err);
406 }
407 }
408
409 if (c == '\n' || c == EOS) {
410 unget_ch();
411 return NO_TOKEN;
412 }
413
414 if (diag && fp == NULL && defp && (warn_level & 1)) {
415 char tmp[ NWORK + 16];
416 char * tmp_end = tmp + NWORK;
417 char * tmp_p;
418 file = unget_string( infile->buffer, defp->name); /* To diagnose */
419 c = get_ch();
420 while (file == infile) { /* Search the expanded macro */
421 if (scan_token( c, (tmp_p = tmp, &tmp_p), tmp_end) != NAM) {
422 c = get_ch();
423 continue;
424 }
425 if (standard && str_eq( identifier, "defined")) {
426 cwarn( "Macro \"%s\" is expanded to \"defined\"" /* _W1_ */
427 , defp->name, 0L, NULL);
428 break;
429 }
430 if (! standard && str_eq( identifier, "sizeof")) {
431 cwarn( "Macro \"%s\" is expanded to \"sizeof\"" /* _W1_ */
432 , defp->name, 0L, NULL);
433 break;
434 }
435 c = get_ch();
436 }
437 if (file == infile) {
438 infile->bptr += strlen( infile->bptr);
439 get_ch();
440 }
441 unget_ch();
442 if (token_type == OPE) {
443 unget_string( work_buf, NULL); /* Set again 'openum' */
444 scan_token( get_ch(), (workp = work_buf, &workp), work_end);
445 }
446 }
447
448 return token_type;
449 }
450
skip_nl(void)451 void skip_nl( void)
452 /*
453 * Skip to the end of the current input line.
454 */
455 {
456 insert_sep = NO_SEP;
457 while (infile && infile->fp == NULL) { /* Stacked text */
458 infile->bptr += strlen( infile->bptr);
459 get_ch(); /* To the parent */
460 }
461 if (infile)
462 infile->bptr += strlen( infile->bptr); /* Source line */
463 }
464
skip_ws(void)465 int skip_ws( void)
466 /*
467 * Skip over horizontal whitespaces.
468 */
469 {
470 int c;
471
472 do {
473 c = get_ch();
474 } while (char_type[ c] & HSP);
475
476 return c;
477 }
478
479 #define MBMASK 0xFF /* Mask to hide multibyte char */
480
scan_token(int c,char ** out_pp,char * out_end)481 int scan_token(
482 int c, /* The first character of the token */
483 char ** out_pp, /* Pointer to pointer to output buf */
484 char * out_end /* End of output buffer */
485 )
486 /*
487 * Scan the next token of any type.
488 * The token is written out to the specified buffer and the output pointer
489 * is advanced. Token is terminated by EOS. Return the type of token.
490 * If the token is an identifier, the token is also in identifier[].
491 * If the token is a operator or punctuator, return OPE.
492 * If 'c' is token separator, then return SEP.
493 * If 'c' is not the first character of any known token and not a token
494 * separator, return SPE.
495 * In POST_STD mode, inserts token separator (a space) between any tokens of
496 * source.
497 */
498 {
499 char * out = *out_pp; /* Output pointer */
500 int ch_type; /* Type of character */
501 int token_type = 0; /* Type of token */
502 int ch;
503
504 if (standard)
505 in_token = TRUE; /* While a token is scanned */
506 c = c & UCHARMAX;
507 ch_type = char_type[ c] & MBMASK;
508
509 switch (ch_type) {
510 case LET: /* Probably an identifier */
511 switch (c) {
512 case 'L':
513 if (! standard)
514 goto ident;
515 ch = get_ch();
516 if (char_type[ ch] & QUO) { /* char_type[ ch] == QUO */
517 if (ch == '"')
518 token_type = WSTR; /* Wide-char string literal */
519 else
520 token_type = WCHR; /* Wide-char constant */
521 c = ch;
522 *out++ = 'L';
523 break; /* Fall down to "case QUO:" */
524 } else {
525 unget_ch();
526 } /* Fall through */
527 default: /* An identifier */
528 ident:
529 scan_id( c);
530 out = stpcpy( out, identifier);
531 token_type = NAM;
532 break;
533 }
534 if (token_type == NAM)
535 break;
536 /* Else fall through -- i.e. WSTR, WCHR */
537 case QUO: /* String or character constant */
538 out = scan_quote( c, out, out_end, FALSE);
539 if (token_type == 0) { /* Without prefix L */
540 if (c == '"')
541 token_type = STR;
542 else
543 token_type = CHR;
544 } /* Else WSTR or WCHR */
545 break;
546 case DOT:
547 ch = get_ch();
548 unget_ch();
549 if ((char_type[ ch] & DIG) == 0) /* Operator '.' or '...' */
550 goto operat;
551 /* Else fall through */
552 case DIG: /* Preprocessing number */
553 out = (standard ? scan_number( c, out, out_end)
554 : scan_number_prestd( c, out, out_end));
555 token_type = NUM;
556 break;
557 case PUNC:
558 operat: out = scan_op( c, out); /* Operator or punctuator */
559 token_type = OPE; /* Number is set in global "openum" */
560 break;
561 default: /* Special tokens or special characters */
562 #if OK_UCN
563 if (mcpp_mode == STD && c == '\\' && stdc2) {
564 ch = get_ch();
565 unget_ch();
566 if (ch == 'U' || ch == 'u')
567 goto ident; /* Universal-Characte-Name */
568 }
569 #endif
570 #if OK_MBIDENT
571 if (mcpp_mode == STD && (char_type[ c] & mbchk) && stdc3) {
572 char * bptr = infile->bptr;
573 mb_read( c, &infile->bptr, &out);
574 infile->bptr = bptr;
575 out = *out_pp;
576 goto ident; /* An identifier with multi-byte characters */
577 /* Mbchar cheking has been done in scan_quote() and others. */
578 }
579 #endif
580 if ((standard && (c == CAT || c == ST_QUOTE)) || (char_type[ c] & SPA))
581 token_type = SEP; /* Token separator or magic char*/
582 else
583 token_type = SPE;
584 /* Unkown token ($, @, multi-byte character or Latin */
585 *out++ = c;
586 *out = EOS;
587 break;
588 }
589
590 if (out_end < out)
591 cfatal( "Buffer overflow scanning token \"%s\"" /* _F_ */
592 , *out_pp, 0L, NULL);
593 if (mcpp_debug & TOKEN)
594 dump_token( token_type, *out_pp);
595 if (mcpp_mode == POST_STD && token_type != SEP && infile->fp != NULL
596 && (char_type[ *infile->bptr & UCHARMAX] & SPA) == 0)
597 insert_sep = INSERT_SEP; /* Insert token separator */
598 *out_pp = out;
599
600 in_token = FALSE; /* Token scanning has been done */
601 return token_type;
602 }
603
scan_id(int c)604 static void scan_id(
605 int c /* First char of id */
606 )
607 /*
608 * Reads the next identifier and put it into identifier[].
609 * The caller has already read the first character of the identifier.
610 */
611 {
612 static char * const limit = &identifier[ IDMAX];
613 static int dollar_diagnosed = FALSE; /* Flag of diagnosing '$' */
614 #if OK_UCN
615 int uc2 = 0, uc4 = 0; /* Count of UCN16, UCN32 */
616 #endif
617 #if OK_MBIDENT
618 int mb = 0; /* Count of MBCHAR */
619 #endif
620 size_t len; /* Length of identifier */
621 char * bp = identifier;
622
623 if (c == IN_SRC) { /* Magic character */
624 *bp++ = c;
625 if ((mcpp_debug & MACRO_CALL) && ! in_directive) {
626 *bp++ = get_ch(); /* Its 2-bytes */
627 *bp++ = get_ch(); /* argument */
628 }
629 c = get_ch();
630 }
631
632 do {
633 if (bp < limit)
634 *bp++ = c;
635 #if OK_UCN
636 if (mcpp_mode == STD && c == '\\' && stdc2) {
637 int cnt;
638 char * tp = bp;
639
640 if ((c = get_ch()) == 'u') {
641 cnt = 4;
642 } else if (c == 'U') {
643 cnt = 8;
644 } else {
645 unget_ch();
646 bp--;
647 break;
648 }
649 *bp++ = c;
650 if ((bp = scan_ucn( cnt, bp)) == NULL) /* Error */
651 return;
652 if (cnt == 4)
653 uc2++;
654 else if (cnt == 8)
655 uc4++;
656 if (limit <= tp) /* Too long identifier */
657 bp = tp; /* Back the pointer */
658 goto next_c;
659 }
660 #endif /* OK_UCN */
661 #if OK_MBIDENT
662 if (mcpp_mode == STD && (char_type[ c] & mbchk) && stdc3) {
663 len = mb_read( c, &infile->bptr, &bp);
664 if (len & MB_ERROR) {
665 if (infile->fp)
666 cerror(
667 "Illegal multi-byte character sequence." /* _E_ */
668 , NULL, 0L, NULL);
669 } else {
670 mb += len;
671 }
672 }
673 #endif /* OK_MBIDENT */
674 #if OK_UCN
675 next_c:
676 #endif
677 c = get_ch();
678 } while ((char_type[ c] & (LET | DIG)) /* Letter or digit */
679 #if OK_UCN
680 || (mcpp_mode == STD && c == '\\' && stdc2)
681 #endif
682 #if OK_MBIDENT
683 || (mcpp_mode == STD && (char_type[ c] & mbchk) && stdc3)
684 #endif
685 );
686
687 unget_ch();
688 *bp = EOS;
689
690 if (bp >= limit && (warn_level & 1)) /* Limit of token */
691 cwarn( "Too long identifier truncated to \"%s\"" /* _W1_ */
692 , identifier, 0L, NULL);
693
694 len = bp - identifier;
695 #if IDMAX > IDLEN90MIN
696 /* UCN16, UCN32, MBCHAR are counted as one character for each. */
697 #if OK_UCN
698 if (mcpp_mode == STD)
699 len -= (uc2 * 5) - (uc4 * 9);
700 #endif
701 #if OK_MBIDENT
702 if (mcpp_mode == STD)
703 len -= mb;
704 #endif
705 if (standard && infile->fp && len > std_limits.id_len && (warn_level & 4))
706 cwarn( "Identifier longer than %.0s%ld characters \"%s\"" /* _W4_ */
707 , NULL, (long) std_limits.id_len, identifier);
708 #endif /* IDMAX > IDLEN90MIN */
709
710 if (option_flags.dollar_in_name && dollar_diagnosed == FALSE
711 && (warn_level & 2) && strchr( identifier, '$') != NULL) {
712 cwarn( "'$' in identifier \"%s\"", identifier, 0L, NULL); /* _W2_ */
713 dollar_diagnosed = TRUE; /* Diagnose only once */
714 }
715 }
716
scan_quote(int delim,char * out,char * out_end,int diag)717 char * scan_quote(
718 int delim, /* ', " or < (header-name) */
719 char * out, /* Output buffer */
720 char * out_end, /* End of output buffer */
721 int diag /* Diagnostic should be output */
722 )
723 /*
724 * Scan off a string literal or character constant to the output buffer.
725 * Report diagnosis if the quotation is terminated by newline or character
726 * constant is empty (provided 'diag' is TRUE).
727 * Return the next output pointer or NULL (on error).
728 */
729 {
730 const char * const skip_line = ", skipped the line"; /* _E_ */
731 const char * const unterm_string
732 = "Unterminated string literal%s";
733 const char * const unterm_char
734 = "Unterminated character constant %s%.0ld%s";
735 const char * const empty_const
736 = "Empty character constant %s%.0ld%s";
737 const char * skip;
738 size_t len;
739 int c;
740 char * out_p = out;
741
742 /* Set again in case of called from routines other than scan_token(). */
743 if (standard)
744 in_token = TRUE;
745 *out_p++ = delim;
746 if (delim == '<')
747 delim = '>';
748
749 scan:
750 while ((c = get_ch()) != EOS) {
751
752 #if MBCHAR
753 if (char_type[ c] & mbchk) {
754 /* First of multi-byte character (or shift-sequence) */
755 char * bptr = infile->bptr;
756 len = mb_read( c, &infile->bptr, (*out_p++ = c, &out_p));
757 if (len & MB_ERROR) {
758 if (infile->fp != NULL && compiling && diag) {
759 if (warn_level & 1) {
760 char * buf;
761 size_t chlen;
762 buf = xmalloc( chlen = infile->bptr - bptr + 2);
763 memcpy( buf, bptr, chlen - 1);
764 buf[ chlen - 1] = EOS;
765 cwarn(
766 "Illegal multi-byte character sequence \"%s\" in quotation", /* _W1_ */
767 buf, 0L, NULL);
768 free( buf);
769 }
770 }
771 continue;
772 } else { /* Valid multi-byte character (or sequence) */
773 goto chk_limit;
774 }
775 }
776 #endif
777 if (c == delim) {
778 break;
779 } else if (c == '\\' && delim != '>') { /* In string literal */
780 #if OK_UCN
781 if (mcpp_mode == STD && stdc2) {
782 int cnt;
783 char * tp;
784
785 *out_p++ = c;
786 if ((c = get_ch()) == 'u') {
787 cnt = 4;
788 } else if (c == 'U') {
789 cnt = 8;
790 } else {
791 goto escape;
792 }
793 *out_p++ = c;
794 if ((tp = scan_ucn( cnt, out_p)) != NULL)
795 out_p = tp;
796 /* Else error */
797 continue; /* Error or not, anyway continue */
798 }
799 #endif /* OK_UCN */
800 *out_p++ = c; /* Escape sequence */
801 c = get_ch();
802 escape:
803 #if MBCHAR
804 if (char_type[ c] & mbchk) {
805 /* '\\' followed by multi-byte char */
806 unget_ch();
807 continue;
808 }
809 #endif
810 if (! standard && c == '\n') { /* <backslash><newline> */
811 out_p--; /* Splice the lines */
812 if (cat_line( TRUE) == NULL) /* End of file */
813 break;
814 c = get_ch();
815 }
816 } else if (mcpp_mode == POST_STD && c == ' ' && delim == '>'
817 && infile->fp == NULL) {
818 continue; /* Skip space possibly inserted by macro expansion */
819 } else if (c == '\n') {
820 break;
821 }
822 if (diag && iscntrl( c) && ((char_type[ c] & SPA) == 0)
823 && (warn_level & 1))
824 cwarn(
825 "Illegal control character %.0s0lx%02x in quotation" /* _W1_ */
826 , NULL, (long) c, NULL);
827 *out_p++ = c;
828 chk_limit:
829 if (out_end < out_p) {
830 *out_end = EOS;
831 cfatal( "Too long quotation", NULL, 0L, NULL); /* _F_ */
832 }
833 }
834
835 if (c == '\n' || c == EOS)
836 unget_ch();
837 if (c == delim)
838 *out_p++ = delim;
839 *out_p = EOS;
840 if (diag) { /* At translation phase 3 */
841 skip = (infile->fp == NULL) ? NULL : skip_line;
842 if (c != delim) {
843 if (mcpp_mode == OLD_PREP /* Implicit closing of quote*/
844 && (delim == '"' || delim == '\''))
845 goto done;
846 if (delim == '"') {
847 if (mcpp_mode != POST_STD && option_flags.lang_asm) {
848 /* STD, KR */
849 /* Concatenate the unterminated string to the next line */
850 if (warn_level & 1)
851 cwarn( unterm_string
852 , ", catenated to the next line" /* _W1_ */
853 , 0L, NULL);
854 if (cat_line( FALSE) != NULL)
855 goto scan; /* Splice the lines */
856 /* Else end of file */
857 } else {
858 cerror( unterm_string, skip, 0L, NULL); /* _E_ */
859 }
860 } else if (delim == '\'') {
861 if (mcpp_mode != POST_STD && option_flags.lang_asm) {
862 /* STD, KR */
863 if (warn_level & 1)
864 cwarn( unterm_char, out, 0L, NULL); /* _W1_ */
865 goto done;
866 } else {
867 cerror( unterm_char, out, 0L, skip); /* _E_ */
868 }
869 } else {
870 cerror( "Unterminated header name %s%.0ld%s" /* _E_ */
871 , out, 0L, skip);
872 }
873 out_p = NULL;
874 } else if (delim == '\'' && out_p - out <= 2) {
875 if (mcpp_mode != POST_STD && option_flags.lang_asm) {
876 /* STD, KR */
877 if (warn_level & 1)
878 cwarn( empty_const, out, 0L, skip); /* _W1_ */
879 } else {
880 cerror( empty_const, out, 0L, skip); /* _E_ */
881 out_p = NULL;
882 goto done;
883 }
884 } else if (mcpp_mode == POST_STD && delim == '>' && (warn_level & 2)) {
885 cwarn(
886 "Header-name enclosed by <, > is an obsolescent feature %s" /* _W2_ */
887 , out, 0L, skip);
888 }
889 #if NWORK-2 > SLEN90MIN
890 if (standard && out_p - out > std_limits.str_len && (warn_level & 4))
891 cwarn( "Quotation longer than %.0s%ld bytes" /* _W4_ */
892 , NULL, std_limits.str_len, NULL);
893 #endif
894 }
895
896 done:
897 in_token = FALSE;
898 return out_p;
899 }
900
cat_line(int del_bsl)901 static char * cat_line(
902 int del_bsl /* Delete the <backslash><newline> ? */
903 )
904 /*
905 * If del_bsl == TRUE:
906 * Delete <backslash><newline> sequence in string literal.
907 * FALSE: Overwrite the <newline> with <backslash>'n'.
908 * Return NULL on end of file. Called only from scan_quote().
909 * This routine is never called in POST_STD mode.
910 */
911 {
912 size_t len;
913 char * save1, * save2;
914
915 if (del_bsl) { /* Delete the <backslash><newline> */
916 infile->bptr -= 2;
917 len = infile->bptr - infile->buffer;
918 } else { /* Overwrite the <newline> with <backslash>'n' */
919 strcpy( infile->bptr, "\\n");
920 len = strlen( infile->buffer);
921 }
922 save1 = save_string( infile->buffer);
923 save2 = get_line( FALSE); /* infile->buffer is overwritten */
924 if (save2 == NULL) {
925 free( save1);
926 return NULL;
927 }
928 save2 = save_string( infile->buffer);
929 memcpy( infile->buffer, save1, len);
930 strcpy( infile->buffer + len, save2); /* Catenate */
931 free( save1);
932 free( save2);
933 if (! del_bsl)
934 len -= 2;
935 infile->bptr = infile->buffer + len;
936 return infile->bptr;
937 }
938
scan_number(int c,char * out,char * out_end)939 static char * scan_number(
940 int c, /* First char of number */
941 char * out, /* Output buffer */
942 char * out_end /* Limit of output buffer */
943 )
944 /*
945 * Read a preprocessing number.
946 * By scan_token() we know already that the first c is from 0 to 9 or dot,
947 * and if c is dot then the second character is digit.
948 * Returns the advanced output pointer.
949 * Note: preprocessing number permits non-numeric forms such as 3E+xy,
950 * which are used in stringization or token-concatenation.
951 */
952 {
953 char * out_p = out; /* Current output pointer */
954
955 do {
956 *out_p++ = c;
957 if (c == 'E' || c == 'e' /* Sign should follow 'E', 'e', */
958 || (stdc3 && (c == 'P' || c == 'p'))
959 /* 'P' or 'p'. */
960 ) {
961 c = get_ch();
962 if (c == '+' || c == '-') {
963 *out_p++ = c;
964 c = get_ch();
965 }
966 #if OK_UCN
967 } else if (mcpp_mode == STD && c == '\\' && stdc3) {
968 int cnt;
969 char * tp;
970
971 if ((c = get_ch()) == 'u') {
972 cnt = 4;
973 } else if (c == 'U') {
974 cnt = 8;
975 } else {
976 unget_ch();
977 out_p--;
978 break;
979 }
980 *out_p++ = c;
981 if ((tp = scan_ucn( cnt, out_p)) == NULL) /* Error */
982 break;
983 else
984 out_p = tp;
985 c = get_ch();
986 #endif /* OK_UCN */
987 #if OK_MBIDENT
988 } else if (mcpp_mode == STD && (char_type[ c] & mbchk) && stdc3) {
989 len = mb_read( c, &infile->bptr, &out_p);
990 if (len & MB_ERROR) {
991 if (infile->fp)
992 cerror(
993 "Illegal multi-byte character sequence." /* _E_ */
994 , NULL, 0L, NULL);
995 }
996 #endif /* OK_MBIDENT */
997 } else {
998 c = get_ch();
999 }
1000 } while ((char_type[ c] & (DIG | DOT | LET)) /* Digit, dot or letter */
1001 #if OK_UCN
1002 || (mcpp_mode == STD && c == '\\' && stdc3)
1003 #endif
1004 #if OK_MBIDENT
1005 || (mcpp_mode == STD && (char_type[ c] & mbchk) && stdc3)
1006 #endif
1007 );
1008
1009 *out_p = EOS;
1010 if (out_end < out_p)
1011 cfatal( "Too long pp-number token \"%s\"" /* _F_ */
1012 , out, 0L, NULL);
1013 unget_ch();
1014 return out_p;
1015 }
1016
1017 /* Original version of DECUS CPP with slight modifications, */
1018 /* too exact for Standard preprocessing. */
scan_number_prestd(int c,char * out,char * out_end)1019 static char * scan_number_prestd(
1020 int c, /* First char of number */
1021 char * out, /* Output buffer */
1022 char * out_end /* Limit of output buffer */
1023 )
1024 /*
1025 * Process a number. We know that c is from 0 to 9 or dot.
1026 * Algorithm from Dave Conroy's Decus C.
1027 * Returns the advanced output pointer.
1028 */
1029 {
1030 char * const out_s = out; /* For diagnostics */
1031 int radix; /* 8, 10, or 16 */
1032 int expseen; /* 'e' seen in floater */
1033 int octal89; /* For bad octal test */
1034 int dotflag; /* TRUE if '.' was seen */
1035
1036 expseen = FALSE; /* No exponent seen yet */
1037 octal89 = FALSE; /* No bad octal yet */
1038 radix = 10; /* Assume decimal */
1039 if ((dotflag = (c == '.')) != FALSE) { /* . something? */
1040 *out++ = '.'; /* Always out the dot */
1041 if ((char_type[(c = get_ch())] & DIG) == 0) {
1042 /* If not a float numb, */
1043 goto nomore; /* All done for now */
1044 }
1045 } /* End of float test */
1046 else if (c == '0') { /* Octal or hex? */
1047 *out++ = c; /* Stuff initial zero */
1048 radix = 8; /* Assume it's octal */
1049 c = get_ch(); /* Look for an 'x' */
1050 if (c == 'x' || c == 'X') { /* Did we get one? */
1051 radix = 16; /* Remember new radix */
1052 *out++ = c; /* Stuff the 'x' */
1053 c = get_ch(); /* Get next character */
1054 }
1055 }
1056 while (1) { /* Process curr. char. */
1057 /*
1058 * Note that this algorithm accepts "012e4" and "03.4"
1059 * as legitimate floating-point numbers.
1060 */
1061 if (radix != 16 && (c == 'e' || c == 'E')) {
1062 if (expseen) /* Already saw 'E'? */
1063 break; /* Exit loop, bad nbr. */
1064 expseen = TRUE; /* Set exponent seen */
1065 radix = 10; /* Decimal exponent */
1066 *out++ = c; /* Output the 'e' */
1067 if ((c = get_ch()) != '+' && c != '-')
1068 continue;
1069 }
1070 else if (radix != 16 && c == '.') {
1071 if (dotflag) /* Saw dot already? */
1072 break; /* Exit loop, two dots */
1073 dotflag = TRUE; /* Remember the dot */
1074 radix = 10; /* Decimal fraction */
1075 }
1076 else { /* Check the digit */
1077 switch (c) {
1078 case '8': case '9': /* Sometimes wrong */
1079 octal89 = TRUE; /* Do check later */
1080 case '0': case '1': case '2': case '3':
1081 case '4': case '5': case '6': case '7':
1082 break; /* Always ok */
1083
1084 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1085 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1086 if (radix == 16) /* Alpha's are ok only */
1087 break; /* if reading hex. */
1088 default: /* At number end */
1089 goto done; /* Break from for loop */
1090 } /* End of switch */
1091 } /* End general case */
1092 *out++ = c; /* Accept the character */
1093 c = get_ch(); /* Read another char */
1094 } /* End of scan loop */
1095
1096 if (out_end < out) /* Buffer overflow */
1097 goto nomore;
1098 /*
1099 * When we break out of the scan loop, c contains the first
1100 * character (maybe) not in the number. If the number is an
1101 * integer, allow a trailing 'L' for long. If not those, push
1102 * the trailing character back on the input stream.
1103 * Floating point numbers accept a trailing 'L' for "long double".
1104 */
1105 done:
1106 if (! (dotflag || expseen)) { /* Not floating point */
1107 /*
1108 * We know that dotflag and expseen are both zero, now:
1109 * dotflag signals "saw 'L'".
1110 */
1111 for (;;) {
1112 switch (c) {
1113 case 'l':
1114 case 'L':
1115 if (dotflag)
1116 goto nomore;
1117 dotflag = TRUE;
1118 break;
1119 default:
1120 goto nomore;
1121 }
1122 *out++ = c; /* Got 'L' . */
1123 c = get_ch(); /* Look at next, too. */
1124 }
1125 }
1126
1127 nomore: *out = EOS;
1128 if (out_end < out)
1129 goto overflow;
1130 unget_ch(); /* Not part of a number */
1131 if (octal89 && radix == 8 && (warn_level & 1))
1132 cwarn( "Illegal digit in octal number \"%s\"" /* _W1_ */
1133 , out_s, 0L, NULL);
1134 return out;
1135
1136 overflow:
1137 cfatal( "Too long number token \"%s\"", out_s, 0L, NULL); /* _F_ */
1138 return out;
1139 }
1140
1141 #if OK_UCN
scan_ucn(int cnt,char * out)1142 static char * scan_ucn(
1143 int cnt, /* Bytes of sequence */
1144 char * out /* Output buffer */
1145 )
1146 /*
1147 * Scan an UCN sequence and put the sequence to 'out'.
1148 * Return the advanced pointer or NULL on failure.
1149 * This routine is never called in POST_STD mode.
1150 */
1151 {
1152 uexpr_t value; /* Value of UCN */
1153 int i, c;
1154
1155 value = 0L;
1156 for (i = 0; i < cnt; i++) {
1157 c = get_ch();
1158 if (! isxdigit( c)) {
1159 if (infile->fp)
1160 cerror( "Illegal UCN sequence" /* _E_ */
1161 , NULL, 0L, NULL);
1162 *out = EOS;
1163 unget_ch();
1164 return NULL;
1165 }
1166 c = tolower( c);
1167 *out++ = c;
1168 c = (isdigit( c) ? (c - '0') : (c - 'a' + 10));
1169 value = (value << 4) | c;
1170 }
1171 if (infile->fp /* In source */
1172 && ((value >= 0L && value <= 0x9FL
1173 && value != 0x24L && value != 0x40L && value != 0x60L)
1174 /* Basic source character */
1175 || (stdc3 && (value >= 0xD800L && value <= 0xDFFFL))))
1176 /* Reserved for special chars */
1177 cerror( "UCN cannot specify the value %.0s\"%08lx\"" /* _E_ */
1178 , NULL, (long) value, NULL);
1179 return out;
1180 }
1181 #endif /* OK_UCN */
1182
scan_op(int c,char * out)1183 static char * scan_op(
1184 int c, /* First char of the token */
1185 char * out /* Output buffer */
1186 )
1187 /*
1188 * Scan C operator or punctuator into the specified buffer.
1189 * Return the advanced output pointer.
1190 * The code-number of the operator is stored to global variable 'openum'.
1191 * Note: '#' is not an operator nor a punctuator in other than directive line,
1192 * nevertheless is handled as a punctuator in this cpp for convenience.
1193 */
1194 {
1195 int c2, c3, c4;
1196
1197 *out++ = c;
1198
1199 switch (c) {
1200 case '~': openum = OP_COM; break;
1201 case '(': openum = OP_LPA; break;
1202 case ')': openum = OP_RPA; break;
1203 case '?': openum = OP_QUE; break;
1204 case ';': case '[': case ']': case '{':
1205 case '}': case ',':
1206 openum = OP_1;
1207 break;
1208 default:
1209 openum = OP_2; /* Tentative guess */
1210 }
1211
1212 if (openum != OP_2) { /* Single byte operators */
1213 *out = EOS;
1214 return out;
1215 }
1216
1217 c2 = get_ch(); /* Possibly two bytes ops */
1218 *out++ = c2;
1219
1220 switch (c) {
1221 case '=':
1222 openum = ((c2 == '=') ? OP_EQ : OP_1); /* ==, = */
1223 break;
1224 case '!':
1225 openum = ((c2 == '=') ? OP_NE : OP_NOT); /* !=, ! */
1226 break;
1227 case '&':
1228 switch (c2) {
1229 case '&': openum = OP_ANA; break; /* && */
1230 case '=': /* openum = OP_2; */ break; /* &= */
1231 default : openum = OP_AND; break; /* & */
1232 }
1233 break;
1234 case '|':
1235 switch (c2) {
1236 case '|': openum = OP_ORO; break; /* || */
1237 case '=': /* openum = OP_2; */ break; /* |= */
1238 default : openum = OP_OR; break; /* | */
1239 }
1240 break;
1241 case '<':
1242 switch (c2) {
1243 case '<': c3 = get_ch();
1244 if (c3 == '=') {
1245 openum = OP_3; /* <<= */
1246 *out++ = c3;
1247 } else {
1248 openum = OP_SL; /* << */
1249 unget_ch();
1250 }
1251 break;
1252 case '=': openum = OP_LE; break; /* <= */
1253 case ':': /* <: i.e. [ */
1254 if (mcpp_mode == STD && option_flags.dig)
1255 openum = OP_LBRCK_D;
1256 else
1257 openum = OP_LT;
1258 break;
1259 case '%': /* <% i.e. { */
1260 if (mcpp_mode == STD && option_flags.dig)
1261 openum = OP_LBRACE_D;
1262 else
1263 openum = OP_LT;
1264 break;
1265 default : openum = OP_LT; break; /* < */
1266 }
1267 break;
1268 case '>':
1269 switch (c2) {
1270 case '>': c3 = get_ch();
1271 if (c3 == '=') {
1272 openum = OP_3; /* >>= */
1273 *out++ = c3;
1274 } else {
1275 openum = OP_SR; /* >> */
1276 unget_ch();
1277 }
1278 break;
1279 case '=': openum = OP_GE; break; /* >= */
1280 default : openum = OP_GT; break; /* > */
1281 }
1282 break;
1283 case '#':
1284 if (standard && (in_define || macro_line)) /* in #define or macro */
1285 openum = ((c2 == '#') ? OP_CAT : OP_STR); /* ##, # */
1286 else
1287 openum = OP_1; /* # */
1288 break;
1289 case '+':
1290 switch (c2) {
1291 case '+': /* ++ */
1292 case '=': /* openum = OP_2; */ break; /* += */
1293 default : openum = OP_ADD; break; /* + */
1294 }
1295 break;
1296 case '-':
1297 switch (c2) {
1298 case '-': /* -- */
1299 case '=': /* -= */
1300 /* openum = OP_2; */
1301 break;
1302 case '>':
1303 if (cplus_val) {
1304 if ((c3 = get_ch()) == '*') { /* ->* */
1305 openum = OP_3;
1306 *out++ = c3;
1307 } else {
1308 /* openum = OP_2; */
1309 unget_ch();
1310 }
1311 } /* else openum = OP_2; */ /* -> */
1312 /* else openum = OP_2; */
1313 break;
1314 default : openum = OP_SUB; break; /* - */
1315 }
1316 break;
1317 case '%':
1318 switch (c2) {
1319 case '=': break; /* %= */
1320 case '>': /* %> i.e. } */
1321 if (mcpp_mode == STD && option_flags.dig)
1322 openum = OP_RBRACE_D;
1323 else
1324 openum = OP_MOD;
1325 break;
1326 case ':':
1327 if (mcpp_mode == STD && option_flags.dig) {
1328 if ((c3 = get_ch()) == '%') {
1329 if ((c4 = get_ch()) == ':') { /* %:%: i.e. ## */
1330 openum = OP_DSHARP_D;
1331 *out++ = c3;
1332 *out++ = c4;
1333 } else {
1334 unget_ch();
1335 unget_ch();
1336 openum = OP_SHARP_D; /* %: i.e. # */
1337 }
1338 } else {
1339 unget_ch();
1340 openum = OP_SHARP_D; /* %: i.e. # */
1341 }
1342 if (in_define) { /* in #define */
1343 if (openum == OP_DSHARP_D)
1344 openum = OP_CAT;
1345 else
1346 openum = OP_STR;
1347 }
1348 } else {
1349 openum = OP_MOD;
1350 }
1351 break;
1352 default : openum = OP_MOD; break; /* % */
1353 }
1354 break;
1355 case '*':
1356 if (c2 != '=') /* * */
1357 openum = OP_MUL;
1358 /* else openum = OP_2; */ /* *= */
1359 break;
1360 case '/':
1361 if (c2 != '=') /* / */
1362 openum = OP_DIV;
1363 /* else openum = OP_2; */ /* /= */
1364 break;
1365 case '^':
1366 if (c2 != '=') /* ^ */
1367 openum = OP_XOR;
1368 /* else openum = OP_2; */ /* ^= */
1369 break;
1370 case '.':
1371 if (standard) {
1372 if (c2 == '.') {
1373 c3 = get_ch();
1374 if (c3 == '.') {
1375 openum = OP_ELL; /* ... */
1376 *out++ = c3;
1377 break;
1378 } else {
1379 unget_ch();
1380 openum = OP_1;
1381 }
1382 } else if (cplus_val && c2 == '*') { /* .* */
1383 /* openum = OP_2 */ ;
1384 } else { /* . */
1385 openum = OP_1;
1386 }
1387 } else {
1388 openum = OP_1;
1389 }
1390 break;
1391 case ':':
1392 if (cplus_val && c2 == ':') /* :: */
1393 /* openum = OP_2 */ ;
1394 else if (mcpp_mode == STD && c2 == '>' && option_flags.dig)
1395 openum = OP_RBRCK_D; /* :> i.e. ] */
1396 else /* : */
1397 openum = OP_COL;
1398 break;
1399 default: /* Never reach here */
1400 cfatal( "Bug: Punctuator is mis-implemented %.0s0lx%x" /* _F_ */
1401 , NULL, (long) c, NULL);
1402 openum = OP_1;
1403 break;
1404 }
1405
1406 switch (openum) {
1407 case OP_STR:
1408 if (mcpp_mode == STD && c == '%') break; /* %: */
1409 case OP_1:
1410 case OP_NOT: case OP_AND: case OP_OR: case OP_LT:
1411 case OP_GT: case OP_ADD: case OP_SUB: case OP_MOD:
1412 case OP_MUL: case OP_DIV: case OP_XOR: case OP_COM:
1413 case OP_COL: /* Any single byte operator or punctuator */
1414 unget_ch();
1415 out--;
1416 break;
1417 default: /* Two or more bytes operators or punctuators */
1418 break;
1419 }
1420
1421 *out = EOS;
1422 return out;
1423 }
1424
id_operator(const char * name)1425 int id_operator(
1426 const char * name
1427 )
1428 /*
1429 * Check whether the name is identifier-like operator in C++.
1430 * Return the operator number if matched, return 0 if not matched.
1431 * Note: these identifiers are defined as macros in <iso646.h> in C95.
1432 * This routine is never called in POST_STD mode.
1433 */
1434 {
1435 typedef struct id_op {
1436 const char * name;
1437 int op_num;
1438 } ID_OP;
1439
1440 ID_OP id_ops[] = {
1441 { "and", OP_ANA},
1442 { "and_eq", OP_2},
1443 { "bitand", OP_AND},
1444 { "bitor", OP_OR},
1445 { "compl", OP_COM},
1446 { "not", OP_NOT},
1447 { "not_eq", OP_NE},
1448 { "or", OP_ORO},
1449 { "or_eq", OP_2},
1450 { "xor", OP_XOR},
1451 { "xor_eq", OP_2},
1452 { NULL, 0},
1453 };
1454
1455 ID_OP * id_p = id_ops;
1456
1457 while (id_p->name != NULL) {
1458 if (str_eq( name, id_p->name))
1459 return id_p->op_num;
1460 id_p++;
1461 }
1462 return 0;
1463 }
1464
expanding(const char * name,int to_be_freed)1465 void expanding(
1466 const char * name, /* The name of (nested) macro just expanded. */
1467 int to_be_freed /* The name should be freed later. */
1468 )
1469 /*
1470 * Remember used macro name for diagnostic.
1471 */
1472 {
1473 if (exp_mac_ind < EXP_MAC_IND_MAX - 1) {
1474 exp_mac_ind++;
1475 } else {
1476 clear_exp_mac();
1477 exp_mac_ind++;
1478 }
1479 expanding_macro[ exp_mac_ind].name = name;
1480 expanding_macro[ exp_mac_ind].to_be_freed = to_be_freed;
1481 }
1482
clear_exp_mac(void)1483 void clear_exp_mac( void)
1484 /*
1485 * Initialize expanding_macro[] freeing names registered in
1486 * name_to_be_freed[].
1487 */
1488 {
1489 int i;
1490
1491 for (i = 1; i < EXP_MAC_IND_MAX; i++) {
1492 if (expanding_macro[ i].to_be_freed) {
1493 free( (void *) expanding_macro[ i].name);
1494 expanding_macro[ i].to_be_freed = FALSE;
1495 }
1496 }
1497 exp_mac_ind = 0;
1498 }
1499
get_ch(void)1500 int get_ch( void)
1501 /*
1502 * Return the next character from a macro or the current file.
1503 * Always return the value representable by unsigned char.
1504 */
1505 {
1506 int len;
1507 int c;
1508 FILEINFO * file;
1509
1510 /*
1511 * 'in_token' is set to TRUE while scan_token() is executed (and
1512 * scan_id(), scan_quote(), scan_number(), scan_ucn() and scan_op()
1513 * via scan_token()) in Standard mode to simplify tokenization.
1514 * Any token cannot cross "file"s.
1515 */
1516 if (in_token)
1517 return (*infile->bptr++ & UCHARMAX);
1518
1519 if ((file = infile) == NULL)
1520 return CHAR_EOF; /* End of all input */
1521
1522 if (mcpp_mode == POST_STD && file->fp) { /* In a source file */
1523 switch (insert_sep) {
1524 case NO_SEP:
1525 break;
1526 case INSERT_SEP: /* Insert a token separator */
1527 insert_sep = INSERTED_SEP; /* Remember this fact */
1528 return ' '; /* for unget_ch(). */
1529 case INSERTED_SEP: /* Has just inserted */
1530 insert_sep = NO_SEP; /* Clear the flag */
1531 break;
1532 }
1533 }
1534 if (! standard && squeezews) {
1535 if (*file->bptr == ' ')
1536 file->bptr++; /* Squeeze white spaces */
1537 squeezews = FALSE;
1538 }
1539
1540 if (mcpp_debug & GETC) {
1541 mcpp_fprintf( DBG, "get_ch(%s) '%c' line %ld, bptr = %d, buffer"
1542 , file->fp ? cur_fullname : file->real_fname ? file->real_fname
1543 : file->filename ? file->filename : "NULL"
1544 , *file->bptr & UCHARMAX
1545 , src_line, (int) (file->bptr - file->buffer));
1546 dump_string( NULL, file->buffer);
1547 dump_unget( "get entrance");
1548 }
1549
1550 /*
1551 * Read a character from the current input logical line or macro.
1552 * At EOS, either finish the current macro (freeing temporary storage)
1553 * or get another logical line by parse_line().
1554 * At EOF, exit the current file (#included) or, at EOF from the MCPP input
1555 * file, return CHAR_EOF to finish processing.
1556 * The character is converted to int with no sign-extension.
1557 */
1558 if ((c = (*file->bptr++ & UCHARMAX)) != EOS) {
1559 if (standard)
1560 return c; /* Just a character */
1561 if (! in_string && c == '\\' && *file->bptr == '\n'
1562 && in_define /* '\\''\n' is deleted in #define line, */
1563 /* provided the '\\' is not the 2nd byte of mbchar. */
1564 && ! last_is_mbchar( file->buffer, strlen( file->buffer) - 2
1565 && ! keep_spaces)
1566 ) {
1567 if (*(file->bptr - 2) == ' ')
1568 squeezews = TRUE;
1569 } else {
1570 return c;
1571 }
1572 }
1573
1574 /*
1575 * Nothing in current line or macro. Get next line (if input from a
1576 * file), or do end of file/macro processing, and reenter get_ch() to
1577 * restart from the top.
1578 */
1579 if (file->fp && /* In source file */
1580 parse_line() != NULL) /* Get line from file */
1581 return get_ch();
1582 /*
1583 * Free up space used by the (finished) file or macro and restart
1584 * input from the parent file/macro, if any.
1585 */
1586 infile = file->parent; /* Unwind file chain */
1587 free( file->buffer); /* Free buffer */
1588 if (infile == NULL) { /* If at end of input */
1589 free( file->filename);
1590 free( file->src_dir);
1591 free( file); /* full_fname is the same with filename for main file*/
1592 return CHAR_EOF; /* Return end of file */
1593 }
1594 if (file->fp) { /* Source file included */
1595 free( file->filename); /* Free filename */
1596 free( file->src_dir); /* Free src_dir */
1597 fclose( file->fp); /* Close finished file */
1598 /* Do not free file->real_fname and file->full_fname */
1599 cur_fullname = infile->full_fname;
1600 cur_fname = infile->real_fname; /* Restore current fname*/
1601 if (infile->pos != 0L) { /* Includer was closed */
1602 infile->fp = fopen( cur_fullname, "r");
1603 fseek( infile->fp, infile->pos, SEEK_SET);
1604 } /* Re-open the includer and restore the file-position */
1605 len = (int) (infile->bptr - infile->buffer);
1606 infile->buffer = xrealloc( infile->buffer, NBUFF);
1607 /* Restore full size buffer to get the next line */
1608 infile->bptr = infile->buffer + len;
1609 src_line = infile->line; /* Reset line number */
1610 inc_dirp = infile->dirp; /* Includer's directory */
1611 #if MCPP_LIB
1612 mcpp_set_out_func( infile->last_fputc, infile->last_fputs,
1613 infile->last_fprintf);
1614 #endif
1615 include_nest--;
1616 src_line++; /* Next line to #include*/
1617 sharp( NULL, infile->include_opt ? 1 : (file->include_opt ? 0 : 2));
1618 /* Need a #line now. Marker depends on include_opt. */
1619 /* The file of include_opt should be marked as 1. */
1620 /* Else if returned from include_opt file, it is the */
1621 /* main input file, and should not be marked. */
1622 /* Else, it is normal includer file, and marked as 2. */
1623 src_line--;
1624 newlines = 0; /* Clear the blank lines*/
1625 if (mcpp_debug & MACRO_CALL) /* Should be re-initialized */
1626 com_cat_line.last_line = bsl_cat_line.last_line = 0L;
1627 } else if (file->filename) { /* Expanding macro */
1628 if (macro_name) /* file->filename should be freed later */
1629 expanding( file->filename, TRUE);
1630 else
1631 free( file->filename);
1632 }
1633 free( file); /* Free file space */
1634 return get_ch(); /* Get from the parent */
1635 }
1636
parse_line(void)1637 static char * parse_line( void)
1638 /*
1639 * ANSI (ISO) C: translation phase 3.
1640 * Parse a logical line.
1641 * Check illegal control characters.
1642 * Check unterminated string literal, character constant or comment.
1643 * Convert each comment to one space (or spaces of the comment length on
1644 * 'keep_spaces' mode)..
1645 * Squeeze succeding white spaces other than <newline> (including comments) to
1646 * one space (unless keep_spaces == TRUE).
1647 * The lines might be spliced by comments which cross the lines.
1648 */
1649 {
1650 char * temp; /* Temporary buffer */
1651 char * limit; /* Buffer end */
1652 char * tp; /* Current pointer into temporary buffer */
1653 char * sp; /* Pointer into input buffer */
1654 size_t com_size;
1655 int c;
1656
1657 if ((sp = get_line( FALSE)) == NULL) /* Next logical line */
1658 return NULL; /* End of a file */
1659 if (in_asm) { /* In #asm block */
1660 while (char_type[ *sp++ & UCHARMAX] & SPA)
1661 ;
1662 if (*--sp == '#') /* Directive line */
1663 infile->bptr = sp;
1664 return infile->bptr; /* Don't tokenize */
1665 }
1666 tp = temp = xmalloc( (size_t) NBUFF);
1667 limit = temp + NBUFF - 2;
1668
1669 while (char_type[ c = *sp++ & UCHARMAX] & HSP) {
1670 if (mcpp_mode != POST_STD)
1671 /* Preserve line top horizontal white spaces */
1672 /* as they are for human-readability */
1673 *tp++ = c;
1674 /* Else skip the line top spaces */
1675 }
1676 sp--;
1677
1678 while ((c = *sp++ & UCHARMAX) != '\n') {
1679
1680 switch (c) {
1681 case '/':
1682 switch (*sp++) {
1683 case '*': /* Start of a comment */
1684 com_start:
1685 if ((sp = read_a_comment( sp, &com_size)) == NULL) {
1686 free( temp); /* End of file with un- */
1687 return NULL; /* terminated comment */
1688 }
1689 if (keep_spaces && mcpp_mode != OLD_PREP) {
1690 if (tp + com_size >= limit - 1) /* Too long comment */
1691 com_size = limit - tp - 1; /* Truncate */
1692 while (com_size--)
1693 *tp++ = ' '; /* Spaces of the comment length */
1694 break;
1695 }
1696 switch (mcpp_mode) {
1697 case POST_STD:
1698 if (temp < tp && *(tp - 1) != ' ')
1699 *tp++ = ' '; /* Squeeze white spaces */
1700 break;
1701 case OLD_PREP:
1702 if (temp == tp
1703 || ! (char_type[ *(tp - 1) & UCHARMAX] & HSP))
1704 *tp++ = COM_SEP; /* Convert to magic character */
1705 break;
1706 default:
1707 if (temp == tp ||
1708 ! (char_type[ *(tp - 1) & UCHARMAX] & HSP))
1709 *tp++ = ' '; /* Squeeze white spaces */
1710 break;
1711 }
1712 break;
1713 case '/': /* // */
1714 if (! standard)
1715 goto not_comment;
1716 /* Comment when C++ or __STDC_VERSION__ >= 199901L */
1717 /* Need not to convert to a space because '\n' follows */
1718 if (! stdc2 && (warn_level & 2))
1719 cwarn( "Parsed \"//\" as comment" /* _W2_ */
1720 , NULL, 0L, NULL);
1721 if (keep_comments) {
1722 sp -= 2;
1723 while (*sp != '\n') /* Until end of line */
1724 mcpp_fputc( *sp++, OUT);
1725 mcpp_fputc( '\n', OUT);
1726 wrong_line = TRUE;
1727 }
1728 goto end_line;
1729 default: /* Not a comment */
1730 not_comment:
1731 *tp++ = '/';
1732 sp--; /* To re-read */
1733 break;
1734 }
1735 break;
1736 case '\r': /* Vertical white spaces*/
1737 /* Note that [CR+LF] is already converted to [LF]. */
1738 case '\f':
1739 case '\v':
1740 if (warn_level & 4)
1741 cwarn( "Converted %.0s0x%02lx to a space" /* _W4_ */
1742 , NULL, (long) c, NULL);
1743 case '\t': /* Horizontal space */
1744 case ' ':
1745 if (keep_spaces) {
1746 if (c == '\t')
1747 *tp++ = '\t';
1748 else
1749 *tp++ = ' '; /* Convert to ' ' */
1750 } else if (! (char_type[ *(tp - 1) & UCHARMAX] & HSP)) {
1751 *tp++ = ' '; /* Squeeze white spaces */
1752 } else if (mcpp_mode == OLD_PREP && *(tp - 1) == COM_SEP) {
1753 *(tp - 1) = ' '; /* Replace COM_SEP with ' ' */
1754 }
1755 break;
1756 case '"': /* String literal */
1757 case '\'': /* Character constant */
1758 infile->bptr = sp;
1759 if (standard) {
1760 tp = scan_quote( c, tp, limit, TRUE);
1761 } else {
1762 in_string = TRUE; /* Enable line splicing by scan_quote() */
1763 tp = scan_quote( c, tp, limit, TRUE); /* (not by get_ch())*/
1764 in_string = FALSE;
1765 }
1766 if (tp == NULL) {
1767 free( temp); /* Unbalanced quotation */
1768 return parse_line(); /* Skip the line */
1769 }
1770 sp = infile->bptr;
1771 break;
1772 default:
1773 if (iscntrl( c)) {
1774 cerror( /* Skip the control character */
1775 "Illegal control character %.0s0x%lx, skipped the character" /* _E_ */
1776 , NULL, (long) c, NULL);
1777 } else { /* Any valid character */
1778 *tp++ = c;
1779 }
1780 break;
1781 }
1782
1783 if (limit < tp) {
1784 *tp = EOS;
1785 cfatal( "Too long line spliced by comments" /* _F_ */
1786 , NULL, 0L, NULL);
1787 }
1788 }
1789
1790 end_line:
1791 if (temp < tp && (char_type[ *(tp - 1) & UCHARMAX] & HSP))
1792 tp--; /* Remove trailing white space */
1793 *tp++ = '\n';
1794 *tp = EOS;
1795 infile->bptr = strcpy( infile->buffer, temp); /* Write back to buffer */
1796 free( temp);
1797 if (macro_line != 0 && macro_line != MACRO_ERROR) { /* Expanding macro */
1798 temp = infile->buffer;
1799 while (char_type[ *temp & UCHARMAX] & HSP)
1800 temp++;
1801 if (*temp == '#' /* This line starts with # token */
1802 || (mcpp_mode == STD && *temp == '%' && *(temp + 1) == ':'))
1803 if (warn_level & 1)
1804 cwarn(
1805 "Macro started at line %.0s%ld swallowed directive-like line" /* _W1_ */
1806 , NULL, macro_line, NULL);
1807 }
1808 return infile->buffer;
1809 }
1810
read_a_comment(char * sp,size_t * sizp)1811 static char * read_a_comment(
1812 char * sp, /* Source */
1813 size_t * sizp /* Size of the comment */
1814 )
1815 /*
1816 * Read over a comment (which may cross the lines).
1817 */
1818 {
1819 int c;
1820 char * saved_sp;
1821 int cat_line = 0; /* Number of catenated lines */
1822
1823 if (keep_spaces) {
1824 saved_sp = sp - 2; /* '-2' for beginning / and * */
1825 *sizp = 0;
1826 }
1827 if (keep_comments) /* If writing comments */
1828 mcpp_fputs( "/*", OUT); /* Write the initializer*/
1829 c = *sp++;
1830
1831 while (1) { /* Eat a comment */
1832 if (keep_comments)
1833 mcpp_fputc( c, OUT);
1834
1835 switch (c) {
1836 case '/':
1837 if ((c = *sp++) != '*') /* Don't let comments */
1838 continue; /* nest. */
1839 if (warn_level & 1)
1840 cwarn( "\"/*\" within comment", NULL, 0L, NULL); /* _W1_ */
1841 if (keep_comments)
1842 mcpp_fputc( c, OUT);
1843 /* Fall into * stuff */
1844 case '*':
1845 if ((c = *sp++) != '/') /* If comment doesn't */
1846 continue; /* end, look at next. */
1847 if (keep_comments) { /* Put out comment */
1848 mcpp_fputc( c, OUT); /* terminator, too. */
1849 mcpp_fputc( '\n', OUT); /* Append '\n' to avoid */
1850 /* trouble on some other tools such as rpcgen. */
1851 wrong_line = TRUE;
1852 }
1853 if (keep_spaces) /* Save the length */
1854 *sizp = *sizp + (sp - saved_sp);
1855 if ((mcpp_debug & MACRO_CALL) && compiling) {
1856 if (cat_line) {
1857 cat_line++;
1858 com_cat_line.len[ cat_line] /* Catenated length */
1859 = com_cat_line.len[ cat_line - 1]
1860 + strlen( infile->buffer) - 1;
1861 /* '-1' for '\n' */
1862 com_cat_line.last_line = src_line;
1863 }
1864 }
1865 return sp; /* End of comment */
1866 case '\n': /* Line-crossing comment*/
1867 if (keep_spaces) /* Save the length */
1868 *sizp = *sizp + (sp - saved_sp) - 1; /* '-1' for '\n' */
1869 if ((mcpp_debug & MACRO_CALL) && compiling) {
1870 /* Save location informations */
1871 if (cat_line == 0) /* First line of catenation */
1872 com_cat_line.start_line = src_line;
1873 if (cat_line >= MAX_CAT_LINE - 1) {
1874 *sizp = 0; /* Discard the too long comment */
1875 cat_line = 0;
1876 if (warn_level & 4)
1877 cwarn(
1878 "Too long comment, discarded up to here" /* _W4_ */
1879 , NULL, 0L, NULL);
1880 }
1881 cat_line++;
1882 com_cat_line.len[ cat_line]
1883 = com_cat_line.len[ cat_line - 1]
1884 + strlen( infile->buffer) - 1;
1885 }
1886 if ((saved_sp = sp = get_line( TRUE)) == NULL)
1887 return NULL; /* End of file within comment */
1888 /* Never happen, because at_eof() supplement closing*/
1889 wrong_line = TRUE; /* We'll need a #line later */
1890 break;
1891 default: /* Anything else is */
1892 break; /* just a character */
1893 } /* End switch */
1894
1895 c = *sp++;
1896 } /* End comment loop */
1897
1898 return sp; /* Never reach here */
1899 }
1900
mcpp_fgets(char * s,int size,FILE * stream)1901 static char * mcpp_fgets(
1902 char * s,
1903 int size,
1904 FILE * stream
1905 )
1906 {
1907 return fgets( s, size, stream);
1908 }
1909
get_line(int in_comment)1910 static char * get_line(
1911 int in_comment
1912 )
1913 /*
1914 * ANSI (ISO) C: translation phase 1, 2.
1915 * Get the next logical line from source file.
1916 * Convert [CR+LF] to [LF].
1917 */
1918 {
1919 #if COMPILER == INDEPENDENT
1920 #define cr_warn_level 1
1921 #else
1922 #define cr_warn_level 2
1923 #endif
1924 static int cr_converted;
1925 int converted = FALSE;
1926 int len; /* Line length - alpha */
1927 char * ptr;
1928 int cat_line = 0; /* Number of catenated lines */
1929
1930 if (infile == NULL) /* End of a source file */
1931 return NULL;
1932 ptr = infile->bptr = infile->buffer;
1933 if ((mcpp_debug & MACRO_CALL) && src_line == 0) /* Initialize */
1934 com_cat_line.last_line = bsl_cat_line.last_line = 0L;
1935
1936 while (mcpp_fgets( ptr, (int) (infile->buffer + NBUFF - ptr), infile->fp)
1937 != NULL) {
1938 /* Translation phase 1 */
1939 src_line++; /* Gotten next physical line */
1940 if (standard && src_line == std_limits.line_num + 1
1941 && (warn_level & 1))
1942 cwarn( "Line number %.0s\"%ld\" got beyond range" /* _W1_ */
1943 , NULL, src_line, NULL);
1944 if (mcpp_debug & (TOKEN | GETC)) { /* Dump it to DBG */
1945 mcpp_fprintf( DBG, "\n#line %ld (%s)", src_line, cur_fullname);
1946 dump_string( NULL, ptr);
1947 }
1948 len = strlen( ptr);
1949 if (NBUFF - 1 <= ptr - infile->buffer + len
1950 && *(ptr + len - 1) != '\n') {
1951 /* The line does not yet end, though the buffer is full. */
1952 if (NBUFF - 1 <= len)
1953 cfatal( "Too long source line" /* _F_ */
1954 , NULL, 0L, NULL);
1955 else
1956 cfatal( "Too long logical line" /* _F_ */
1957 , NULL, 0L, NULL);
1958 }
1959 if (*(ptr + len - 1) != '\n') /* Unterminated source line */
1960 break;
1961 if (len >= 2 && *(ptr + len - 2) == '\r') { /* [CR+LF] */
1962 *(ptr + len - 2) = '\n';
1963 *(ptr + --len) = EOS;
1964 if (! cr_converted && (warn_level & cr_warn_level)) {
1965 cwarn( "Converted [CR+LF] to [LF]" /* _W1_ _W2_ */
1966 , NULL, 0L, NULL);
1967 cr_converted = TRUE;
1968 }
1969 }
1970 if (standard) {
1971 if (option_flags.trig)
1972 converted = cnv_trigraph( ptr);
1973 if (mcpp_mode == POST_STD && option_flags.dig)
1974 converted += cnv_digraph( ptr);
1975 if (converted)
1976 len = strlen( ptr);
1977 /* Translation phase 2 */
1978 len -= 2;
1979 if (len >= 0) {
1980 if ((*(ptr + len) == '\\') && ! last_is_mbchar( ptr, len)) {
1981 /* <backslash><newline> (not MBCHAR) */
1982 ptr = infile->bptr += len; /* Splice the lines */
1983 wrong_line = TRUE;
1984 if ((mcpp_debug & MACRO_CALL) && compiling) {
1985 /* Save location informations */
1986 if (cat_line == 0) /* First line of catenation */
1987 bsl_cat_line.start_line = src_line;
1988 if (cat_line < MAX_CAT_LINE)
1989 /* Record the catenated length */
1990 bsl_cat_line.len[ ++cat_line]
1991 = strlen( infile->buffer) - 2;
1992 /* Else ignore */
1993 }
1994 continue;
1995 }
1996 }
1997 #if NBUFF-2 > SLEN90MIN
1998 if (ptr - infile->buffer + len + 2 > std_limits.str_len + 1
1999 && (warn_level & 4)) /* +1 for '\n' */
2000 cwarn( "Logical source line longer than %.0s%ld bytes" /* _W4_ */
2001 , NULL, std_limits.str_len, NULL);
2002 #endif
2003 }
2004 if ((mcpp_debug & MACRO_CALL) && compiling) {
2005 if (cat_line && cat_line < MAX_CAT_LINE) {
2006 bsl_cat_line.len[ ++cat_line] = strlen( infile->buffer) - 1;
2007 /* Catenated length: '-1' for '\n' */
2008 bsl_cat_line.last_line = src_line;
2009 }
2010 }
2011 return infile->bptr = infile->buffer; /* Logical line */
2012 }
2013
2014 /* End of a (possibly included) source file */
2015 if (ferror( infile->fp))
2016 cfatal( "File read error", NULL, 0L, NULL); /* _F_ */
2017 if ((ptr = at_eof( in_comment)) != NULL) /* Check at end of file */
2018 return ptr; /* Partial line supplemented */
2019 if (option_flags.z) {
2020 no_output--; /* End of included file */
2021 keep_comments = option_flags.c && compiling && !no_output;
2022 }
2023 return NULL;
2024 }
2025
2026 #define TRIOFFSET 10
2027
cnv_trigraph(char * in)2028 int cnv_trigraph(
2029 char * in
2030 )
2031 /*
2032 * Perform in-place trigraph replacement on a physical line. This was added
2033 * to the C90. In an input text line, the sequence ??[something] is
2034 * transformed to a character (which might not appear on the input keyboard).
2035 */
2036 {
2037 const char * const tritext = "=(/)'<!>-\0#[\\]^{|}~";
2038 /* ^ ^
2039 * +----------+
2040 * this becomes this
2041 */
2042 int count = 0;
2043 const char * tp;
2044
2045 while ((in = strchr( in, '?')) != NULL) {
2046 if (*++in != '?')
2047 continue;
2048 while (*++in == '?')
2049 ;
2050 if ((tp = strchr( tritext, *in)) == NULL)
2051 continue;
2052 *(in - 2) = *(tp + TRIOFFSET);
2053 in--;
2054 memmove( in, in + 2, strlen( in + 1));
2055 count++;
2056 }
2057
2058 if (count && (warn_level & 16))
2059 cwarn( "%.0s%ld trigraph(s) converted" /* _W16_ */
2060 , NULL, (long) count, NULL);
2061 return count;
2062 }
2063
cnv_digraph(char * in)2064 int cnv_digraph(
2065 char * in
2066 )
2067 /*
2068 * Perform in-place digraph replacement on a physical line.
2069 * Called only in POST_STD mode.
2070 */
2071 {
2072 int count = 0;
2073 int i;
2074 int c1, c2;
2075
2076 while ((i = strcspn( in, "%:<")), (c1 = *(in + i)) != '\0') {
2077 in += i + 1;
2078 c2 = *in;
2079 switch (c1) {
2080 case '%' :
2081 switch (c2) {
2082 case ':' : *(in - 1) = '#'; break;
2083 case '>' : *(in - 1) = '}'; break;
2084 default : continue;
2085 }
2086 break;
2087 case ':' :
2088 switch (c2) {
2089 case '>' : *(in - 1) = ']'; break;
2090 default : continue;
2091 }
2092 break;
2093 case '<' :
2094 switch (c2) {
2095 case '%' : *(in - 1) = '{'; break;
2096 case ':' : *(in - 1) = '['; break;
2097 default : continue;
2098 }
2099 break;
2100 }
2101 memmove( in, in + 1, strlen( in));
2102 count++;
2103 }
2104
2105 if (count && (warn_level & 16))
2106 cwarn( "%.0s%ld digraph(s) converted" /* _W16_ */
2107 , NULL, (long) count, NULL);
2108 return count;
2109 }
2110
at_eof(int in_comment)2111 static char * at_eof(
2112 int in_comment
2113 )
2114 /*
2115 * Check the partial line, unterminated comment, unbalanced #if block,
2116 * uncompleted macro call at end of a file or at end of input.
2117 * Supplement the line terminator, if possible.
2118 * Return the supplemented line or NULL on unrecoverable error.
2119 */
2120 {
2121 const char * const format
2122 = "End of %s with %.0ld%s"; /* _E_ _W1_ */
2123 const char * const unterm_if_format
2124 = "End of %s within #if (#ifdef) section started at line %ld"; /* _E_ _W1_ */
2125 const char * const unterm_macro_format
2126 = "End of %s within macro call started at line %ld";/* _E_ _W1_ */
2127 const char * const input
2128 = infile->parent ? "file" : "input"; /* _E_ _W1_ */
2129 const char * const no_newline
2130 = "no newline, supplemented newline"; /* _W1_ */
2131 const char * const unterm_com
2132 = "unterminated comment, terminated the comment"; /* _W1_ */
2133 const char * const backsl = "\\, deleted the \\"; /* _W1_ */
2134 const char * const unterm_asm_format
2135 = "End of %s with unterminated #asm block started at line %ld"; /* _E_ _W1_ */
2136 size_t len;
2137 char * cp;
2138
2139 cp = infile->buffer;
2140 len = strlen( cp);
2141 if (len && *(cp += (len - 1)) != '\n') {
2142 *++cp = '\n'; /* Supplement <newline> */
2143 *++cp = EOS;
2144 if (mcpp_mode != OLD_PREP && (warn_level & 1))
2145 cwarn( format, input, 0L, no_newline);
2146 return infile->bptr = infile->buffer;
2147 }
2148 if (standard && infile->buffer < infile->bptr) {
2149 /* No line after <backslash><newline> */
2150 cp = infile->bptr;
2151 *cp++ = '\n'; /* Delete the \\ */
2152 *cp = EOS;
2153 if (warn_level & 1)
2154 cwarn( format, input, 0L, backsl);
2155 return infile->bptr = infile->buffer;
2156 }
2157 if (in_comment) { /* End of file within a comment */
2158 if (mcpp_mode != OLD_PREP && (warn_level & 1))
2159 cwarn( format, input, 0L, unterm_com);
2160 /* The partial comment line has been already read by */
2161 /* read_a_comment(), so supplement the next line. */
2162 strcpy( infile->buffer, "*/\n");
2163 return infile->bptr = infile->buffer;
2164 }
2165
2166 if (infile->initif < ifptr) {
2167 IFINFO * ifp = infile->initif + 1;
2168 if (standard) {
2169 cerror( unterm_if_format, input, ifp->ifline, NULL);
2170 ifptr = infile->initif; /* Clear information of */
2171 compiling = ifptr->stat; /* erroneous grouping */
2172 } else if (mcpp_mode == KR && (warn_level & 1)) {
2173 cwarn( unterm_if_format, input, ifp->ifline, NULL);
2174 }
2175 }
2176
2177 if (macro_line != 0 && macro_line != MACRO_ERROR
2178 && ((mcpp_mode == STD && in_getarg) || ! standard)) {
2179 if (standard) {
2180 cerror( unterm_macro_format, input, macro_line, NULL);
2181 macro_line = MACRO_ERROR;
2182 } else if (warn_level & 1) {
2183 cwarn( unterm_macro_format, input, macro_line, NULL);
2184 }
2185 }
2186
2187 if (in_asm && mcpp_mode == KR && (warn_level & 1))
2188 cwarn( unterm_asm_format, input, in_asm, NULL);
2189
2190 return NULL;
2191 }
2192
unget_ch(void)2193 void unget_ch( void)
2194 /*
2195 * Back the pointer to reread the last character. Fatal error (code bug)
2196 * if we back too far. unget_ch() may be called, without problems, at end of
2197 * file. Only one character may be ungotten. If you need to unget more,
2198 * call unget_string().
2199 */
2200 {
2201 if (in_token) {
2202 infile->bptr--;
2203 return;
2204 }
2205
2206 if (infile != NULL) {
2207 if (mcpp_mode == POST_STD && infile->fp) {
2208 switch (insert_sep) {
2209 case INSERTED_SEP: /* Have just read an inserted separator */
2210 insert_sep = INSERT_SEP;
2211 return;
2212 case INSERT_SEP:
2213 cfatal( "Bug: unget_ch() just after scan_token()" /* _F_ */
2214 , NULL, 0L, NULL);
2215 break;
2216 default:
2217 break;
2218 }
2219 }
2220 --infile->bptr;
2221 if (infile->bptr < infile->buffer) /* Shouldn't happen */
2222 cfatal( "Bug: Too much pushback", NULL, 0L, NULL); /* _F_ */
2223 }
2224
2225 if (mcpp_debug & GETC)
2226 dump_unget( "after unget");
2227 }
2228
unget_string(const char * text,const char * name)2229 FILEINFO * unget_string(
2230 const char * text, /* Text to unget */
2231 const char * name /* Name of the macro, if any*/
2232 )
2233 /*
2234 * Push a string back on the input stream. This is done by treating
2235 * the text as if it were a macro or a file.
2236 */
2237 {
2238 FILEINFO * file;
2239 size_t size;
2240
2241 if (text)
2242 size = strlen( text) + 1;
2243 else
2244 size = 1;
2245 file = get_file( name, NULL, NULL, size, FALSE);
2246 if (text)
2247 memcpy( file->buffer, text, size);
2248 else
2249 *file->buffer = EOS;
2250 return file;
2251 }
2252
save_string(const char * text)2253 char * save_string(
2254 const char * text
2255 )
2256 /*
2257 * Store a string into free memory.
2258 */
2259 {
2260 char * result;
2261 size_t size;
2262
2263 size = strlen( text) + 1;
2264 result = xmalloc( size);
2265 memcpy( result, text, size);
2266 return result;
2267 }
2268
get_file(const char * name,const char * src_dir,const char * fullname,size_t bufsize,int include_opt)2269 FILEINFO * get_file(
2270 const char * name, /* File or macro name */
2271 const char * src_dir, /* Source file directory*/
2272 const char * fullname, /* Full path list */
2273 size_t bufsize, /* Line buffer size */
2274 int include_opt /* Specified by -include opt (for GCC) */
2275 )
2276 /*
2277 * Common FILEINFO buffer initialization for a new file or macro.
2278 */
2279 {
2280 FILEINFO * file;
2281
2282 file = (FILEINFO *) xmalloc( sizeof (FILEINFO));
2283 file->buffer = xmalloc( bufsize);
2284 file->bptr = file->buffer; /* Initialize line ptr */
2285 file->buffer[ 0] = EOS; /* Force first read */
2286 file->line = 0L; /* (Not used just yet) */
2287 file->fp = NULL; /* No file yet */
2288 file->pos = 0L; /* No pos to remember */
2289 file->parent = infile; /* Chain files together */
2290 file->initif = ifptr; /* Initial ifstack */
2291 file->include_opt = include_opt; /* Specified by -include*/
2292 file->dirp = NULL; /* No include dir yet */
2293 file->real_fname = name; /* Save file/macro name */
2294 file->full_fname = fullname; /* Full path list */
2295 if (name) {
2296 file->filename = xmalloc( strlen( name) + 1);
2297 strcpy( file->filename, name); /* Copy for #line */
2298 } else {
2299 file->filename = NULL;
2300 }
2301 if (src_dir) {
2302 file->src_dir = xmalloc( strlen( src_dir) + 1);
2303 strcpy( file->src_dir, src_dir);
2304 } else {
2305 file->src_dir = NULL;
2306 }
2307 #if MCPP_LIB
2308 file->last_fputc = mcpp_lib_fputc;
2309 file->last_fputs = mcpp_lib_fputs;
2310 file->last_fprintf = mcpp_lib_fprintf;
2311 #endif
2312 if (infile != NULL) { /* If #include file */
2313 infile->line = src_line; /* Save current line */
2314 #if MCPP_LIB
2315 infile->last_fputc = mcpp_fputc;
2316 infile->last_fputs = mcpp_fputs;
2317 infile->last_fprintf = mcpp_fprintf;
2318 #endif
2319 }
2320 infile = file; /* New current file */
2321
2322 return file; /* All done. */
2323 }
2324
2325 static const char * const out_of_memory
2326 = "Out of memory (required size is %.0s0x%lx bytes)"; /* _F_ */
2327
2328 char *
2329 (xmalloc)(
2330 size_t size
2331 )
2332 /*
2333 * Get a block of free memory.
2334 */
2335 {
2336 char * result;
2337
2338 if ((result = (char *) malloc( size)) == NULL) {
2339 if (mcpp_debug & MEMORY)
2340 print_heap();
2341 cfatal( out_of_memory, NULL, (long) size, NULL);
2342 }
2343 return result;
2344 }
2345
2346 char * (xrealloc)(
2347 char * ptr,
2348 size_t size
2349 )
2350 /*
2351 * Reallocate malloc()ed memory.
2352 */
2353 {
2354 char * result;
2355
2356 if ((result = (char *) realloc( ptr, size)) == NULL && size != 0) {
2357 /* 'size != 0' is necessary to cope with some */
2358 /* implementation of realloc( ptr, 0) which returns NULL. */
2359 if (mcpp_debug & MEMORY)
2360 print_heap();
2361 cfatal( out_of_memory, NULL, (long) size, NULL);
2362 }
2363 return result;
2364 }
2365
get_src_location(LINE_COL * p_line_col)2366 LINE_COL * get_src_location(
2367 LINE_COL * p_line_col /* Line and column on phase 4 */
2368 )
2369 /*
2370 * Convert line-column datum of just after translation phase 3 into that of
2371 * phase 2, tracing back line splicing by a comment and <backslash><newline>.
2372 * Note: This conversion does not give correct datum on a line catenated by
2373 * both of <backslash><newline> and line-crossing-comment at the same time.
2374 *
2375 * com_cat_line and bsl_cat_line have data only on last catenated line.
2376 * com_cat_line.len[] and bsl_cat_line.len[] have the length of catenated
2377 * line, and len[ 0] is always 0, followed by len[ 1], len[ 2], ..., as
2378 * accumulated length of successively catenated lines.
2379 */
2380 {
2381 long line;
2382 size_t col;
2383 size_t * cols;
2384 CAT_LINE * l_col_p;
2385 int i;
2386
2387 line = p_line_col->line;
2388 col = p_line_col->col;
2389
2390 for (i = 0; i <= 1; i++) {
2391 l_col_p = i ? & bsl_cat_line : & com_cat_line;
2392 if (l_col_p->last_line != line)
2393 continue;
2394 /* Else just catenated line */
2395 cols = l_col_p->len + 1;
2396 while (*cols < col)
2397 cols++;
2398 if (col <= *cols) {
2399 cols--;
2400 col -= *cols;
2401 }
2402 line = l_col_p->start_line + (cols - l_col_p->len);
2403 }
2404
2405 p_line_col->line = line;
2406 p_line_col->col = col + 1;
2407 /* col internally start at 0, output start at 1 */
2408
2409 return p_line_col;
2410 }
2411
put_line(char * out,FILE * fp)2412 static void put_line(
2413 char * out,
2414 FILE * fp
2415 )
2416 /*
2417 * Put out a logical source line.
2418 * This routine is called only in OLD_PREP mode.
2419 */
2420 {
2421 int c;
2422
2423 while ((c = *out++) != EOS) {
2424 if (c != COM_SEP) /* Skip 0-length comment */
2425 mcpp_fputc( c, FP2DEST( fp));
2426 }
2427 }
2428
do_msg(const char * severity,const char * format,const char * arg1,long arg2,const char * arg3)2429 static void do_msg(
2430 const char * severity, /* "fatal", "error", "warning" */
2431 const char * format, /* Format for the error message */
2432 const char * arg1, /* String arg. for the message */
2433 long arg2, /* Integer argument */
2434 const char * arg3 /* Second string argument */
2435 )
2436 /*
2437 * Print filenames, macro names, line numbers and error messages.
2438 * Also print macro definitions on macro expansion problems.
2439 */
2440 {
2441 FILEINFO * file;
2442 DEFBUF * defp;
2443 int i;
2444 size_t slen;
2445 const char * arg_s[ 2];
2446 char * arg_t[ 2];
2447 char * tp;
2448 const char * sp;
2449 int c;
2450 int ind;
2451
2452 fflush( fp_out); /* Synchronize output and diagnostics */
2453 arg_s[ 0] = arg1; arg_s[ 1] = arg3;
2454
2455 for (i = 0; i < 2; i++) { /* Convert special characters to visible */
2456 sp = arg_s[ i];
2457 if (sp != NULL)
2458 slen = strlen( sp) + 1;
2459 else
2460 slen = 1;
2461 tp = arg_t[ i] = (char *) malloc( slen);
2462 /* Don't use xmalloc() so as not to cause infinite recursion */
2463 if (sp == NULL || *sp == EOS) {
2464 *tp = EOS;
2465 continue;
2466 }
2467
2468 while ((c = *sp++) != EOS) {
2469 switch (c) {
2470 case TOK_SEP:
2471 if (mcpp_mode == OLD_PREP) /* COM_SEP */
2472 break; /* Skip magic characters */
2473 /* Else fall through */
2474 case RT_END:
2475 case CAT:
2476 case ST_QUOTE:
2477 case DEF_MAGIC:
2478 if (! standard)
2479 *tp++ = ' ';
2480 break; /* Skip the magic characters*/
2481 case IN_SRC:
2482 if (! standard)
2483 *tp++ = ' ';
2484 if ((mcpp_debug & MACRO_CALL) && ! in_directive)
2485 sp += 2; /* Skip two more bytes */
2486 break;
2487 case MAC_INF:
2488 if (mcpp_mode != STD) {
2489 *tp++ = ' ';
2490 /* Illegal control character, convert to a space*/
2491 } else {
2492 switch (*sp++) { /* Skip the magic characters*/
2493 case MAC_ARG_START :
2494 sp++;
2495 /* Fall through */
2496 case MAC_CALL_START :
2497 sp += 2;
2498 break;
2499 case MAC_ARG_END :
2500 if (! option_flags.v)
2501 break;
2502 else
2503 sp++;
2504 /* Fall through */
2505 case MAC_CALL_END :
2506 if (option_flags.v)
2507 sp += 2;
2508 break;
2509 }
2510 }
2511 break;
2512 case '\n':
2513 *tp++ = ' '; /* Convert '\n' to a space */
2514 break;
2515 default:
2516 *tp++ = c;
2517 break;
2518 }
2519 }
2520
2521 if (*(sp - 2) == '\n')
2522 tp--;
2523 *tp = EOS;
2524 }
2525
2526 /* Print source location and diagnostic */
2527 file = infile;
2528 while (file != NULL && (file->fp == NULL || file->fp == (FILE *)-1))
2529 file = file->parent; /* Skip macro */
2530 if (file != NULL) {
2531 file->line = src_line;
2532 mcpp_fprintf( ERR, "%s:%ld: %s: ", cur_fullname, src_line, severity);
2533 }
2534 mcpp_fprintf( ERR, format, arg_t[ 0], arg2, arg_t[ 1]);
2535 mcpp_fputc( '\n', ERR);
2536 if (option_flags.no_source_line)
2537 goto free_arg;
2538
2539 /* Print source line, includers and expanding macros */
2540 file = infile;
2541 if (file != NULL && file->fp != NULL) {
2542 if (mcpp_mode == OLD_PREP) {
2543 mcpp_fputs( " ", ERR);
2544 put_line( file->buffer, fp_err);
2545 } else {
2546 mcpp_fprintf( ERR, " %s", file->buffer);
2547 /* Current source line */
2548 }
2549 file = file->parent;
2550 }
2551 while (file != NULL) { /* Print #includes, too */
2552 if (file->fp == NULL) { /* Macro */
2553 if (file->filename) {
2554 defp = look_id( file->filename);
2555 if ((defp->nargs > DEF_NOARGS_STANDARD)
2556 && ! (file->parent && file->parent->filename
2557 && str_eq( file->filename, file->parent->filename)))
2558 /* If the name is not duplicate of parent */
2559 dump_a_def( " macro", defp, FALSE, TRUE, fp_err);
2560 }
2561 } else { /* Source file */
2562 if (file->buffer[ 0] == '\0')
2563 strcpy( file->buffer, "\n");
2564 if (mcpp_mode != OLD_PREP) {
2565 mcpp_fprintf( ERR, " from %s: %ld: %s",
2566 file->line ? file->full_fname /* Full-path-list */
2567 : "<stdin>", /* Included by -include */
2568 file->line, /* Current line number */
2569 file->buffer); /* The source line */
2570 } else {
2571 mcpp_fprintf( ERR, " from %s: %ld: ", file->full_fname
2572 , file->line);
2573 put_line( file->buffer, fp_err);
2574 }
2575 }
2576 file = file->parent;
2577 }
2578
2579 if (! macro_name)
2580 goto free_arg;
2581 /* Additional information of macro definitions */
2582 expanding_macro[ 0].name = macro_name;
2583 for (ind = 0; ind <= exp_mac_ind; ind++) {
2584 int ind_done;
2585
2586 for (ind_done = 0; ind_done < ind; ind_done++)
2587 if (str_eq( expanding_macro[ ind].name
2588 , expanding_macro[ ind_done].name))
2589 break; /* Already reported */
2590 if (ind_done < ind)
2591 continue;
2592 for (file = infile; file; file = file->parent)
2593 if (file->fp == NULL && file->filename
2594 && str_eq( expanding_macro[ ind].name, file->filename))
2595 break; /* Already reported */
2596 if (file)
2597 continue;
2598 if ((defp = look_id( expanding_macro[ ind].name)) != NULL) {
2599 if (defp->nargs <= DEF_NOARGS_STANDARD)
2600 continue; /* Standard predefined */
2601 dump_a_def( " macro", defp, FALSE, TRUE, fp_err);
2602 /* Macro already read over */
2603 }
2604 }
2605
2606 free_arg:
2607 for (i = 0; i < 2; i++)
2608 free( arg_t[ i]);
2609 }
2610
cfatal(const char * format,const char * arg1,long arg2,const char * arg3)2611 void cfatal(
2612 const char * format,
2613 const char * arg1,
2614 long arg2,
2615 const char * arg3
2616 )
2617 /*
2618 * A real disaster.
2619 */
2620 {
2621 do_msg( "fatal error", format, arg1, arg2, arg3);
2622 longjmp( error_exit, -1);
2623 }
2624
cerror(const char * format,const char * arg1,long arg2,const char * arg3)2625 void cerror(
2626 const char * format,
2627 const char * arg1,
2628 long arg2,
2629 const char * arg3
2630 )
2631 /*
2632 * Print a error message.
2633 */
2634 {
2635 do_msg( "error", format, arg1, arg2, arg3);
2636 errors++;
2637 }
2638
cwarn(const char * format,const char * arg1,long arg2,const char * arg3)2639 void cwarn(
2640 const char * format,
2641 const char * arg1,
2642 long arg2,
2643 const char * arg3
2644 )
2645 /*
2646 * Maybe an error.
2647 */
2648 {
2649 do_msg( "warning", format, arg1, arg2, arg3);
2650 }
2651
dump_string(const char * why,const char * text)2652 void dump_string(
2653 const char * why,
2654 const char * text
2655 )
2656 /*
2657 * Dump text readably.
2658 * Bug: macro argument number may be putout as a control character or any
2659 * other character, just after MAC_PARM has been read away.
2660 */
2661 {
2662 const char * cp;
2663 const char * chr;
2664 int c, c1, c2;
2665
2666 if (why != NULL)
2667 mcpp_fprintf( DBG, " (%s)", why);
2668 mcpp_fputs( " => ", DBG);
2669
2670 if (text == NULL) {
2671 mcpp_fputs( "NULL", DBG);
2672 return;
2673 }
2674
2675 for (cp = text; (c = *cp++ & UCHARMAX) != EOS; ) {
2676 chr = NULL;
2677
2678 switch (c) {
2679 case MAC_PARM:
2680 c = *cp++ & UCHARMAX; /* Macro parameter number */
2681 mcpp_fprintf( DBG, "<%d>", c);
2682 break;
2683 case MAC_INF:
2684 if (! (mcpp_mode == STD && (mcpp_debug & MACRO_CALL)))
2685 goto no_magic;
2686 /* Macro informations inserted by -K option */
2687 c2 = *cp++ & UCHARMAX;
2688 if (option_flags.v || c2 == MAC_CALL_START
2689 || c2 == MAC_ARG_START) {
2690 c = ((*cp++ & UCHARMAX) - 1) * UCHARMAX;
2691 c += (*cp++ & UCHARMAX) - 1;
2692 }
2693 switch (c2) {
2694 case MAC_CALL_START:
2695 mcpp_fprintf( DBG, "<MAC%d>", c);
2696 break;
2697 case MAC_CALL_END:
2698 if (option_flags.v)
2699 mcpp_fprintf( DBG, "<MAC_END%d>", c);
2700 else
2701 chr = "<MAC_END>";
2702 break;
2703 case MAC_ARG_START:
2704 c1 = *cp++ & UCHARMAX;
2705 mcpp_fprintf( DBG, "<MAC%d:ARG%d>", c, c1 - 1);
2706 break;
2707 case MAC_ARG_END:
2708 if (option_flags.v) {
2709 c1 = *cp++ & UCHARMAX;
2710 mcpp_fprintf( DBG, "<ARG_END%d-%d>", c, c1 - 1);
2711 } else {
2712 chr = "<ARG_END>";
2713 }
2714 break;
2715 }
2716 break;
2717 case DEF_MAGIC:
2718 if (standard) {
2719 chr = "<MAGIC>";
2720 break;
2721 } /* Else fall through */
2722 case CAT:
2723 if (standard) {
2724 chr = "##";
2725 break;
2726 } /* Else fall through */
2727 case ST_QUOTE:
2728 if (standard) {
2729 chr = "#";
2730 break;
2731 } /* Else fall through */
2732 case RT_END:
2733 if (standard) {
2734 chr = "<RT_END>";
2735 break;
2736 } /* Else fall through */
2737 case IN_SRC:
2738 if (standard) {
2739 if ((mcpp_debug & MACRO_CALL) && ! in_directive) {
2740 int num;
2741 num = ((*cp++ & UCHARMAX) - 1) * UCHARMAX;
2742 num += (*cp++ & UCHARMAX) - 1;
2743 mcpp_fprintf( DBG, "<SRC%d>", num);
2744 } else {
2745 chr = "<SRC>";
2746 }
2747 } else { /* Control character */
2748 mcpp_fprintf( DBG, "<^%c>", c + '@');
2749 }
2750 break;
2751 case TOK_SEP:
2752 if (mcpp_mode == STD) {
2753 chr = "<TSEP>";
2754 break;
2755 } else if (mcpp_mode == OLD_PREP) { /* COM_SEP */
2756 chr = "<CSEP>";
2757 break;
2758 } /* Else fall through */
2759 default:
2760 no_magic:
2761 if (c < ' ')
2762 mcpp_fprintf( DBG, "<^%c>", c + '@');
2763 else
2764 mcpp_fputc( c, DBG);
2765 break;
2766 }
2767
2768 if (chr)
2769 mcpp_fputs( chr, DBG);
2770 }
2771
2772 mcpp_fputc( '\n', DBG);
2773 }
2774
dump_unget(const char * why)2775 void dump_unget(
2776 const char * why
2777 )
2778 /*
2779 * Dump all ungotten junk (pending macros and current input lines).
2780 */
2781 {
2782 const FILEINFO * file;
2783
2784 mcpp_fputs( "dump of pending input text", DBG);
2785 if (why != NULL) {
2786 mcpp_fputs( "-- ", DBG);
2787 mcpp_fputs( why, DBG);
2788 }
2789 mcpp_fputc( '\n', DBG);
2790
2791 for (file = infile; file != NULL; file = file->parent)
2792 dump_string( file->real_fname ? file->real_fname
2793 : file->filename ? file->filename : "NULL", file->bptr);
2794 }
2795
dump_token(int token_type,const char * cp)2796 static void dump_token(
2797 int token_type,
2798 const char * cp /* Token */
2799 )
2800 /*
2801 * Dump a token.
2802 */
2803 {
2804 static const char * const t_type[]
2805 = { "NAM", "NUM", "STR", "WSTR", "CHR", "WCHR", "OPE", "SPE"
2806 , "SEP", };
2807
2808 mcpp_fputs( "token", DBG);
2809 dump_string( t_type[ token_type - NAM], cp);
2810 }
2811
2812