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 %.0s0x%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, NULL, (long)delim, NULL); /* _W1_ */
865                     goto  done;
866                 } else {
867                     cerror( unterm_char, NULL, (long)delim, 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, NULL, (long)delim, skip);     /* _W1_ */
879             } else {
880                 cerror( empty_const, NULL, (long)delim, 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 (temp == tp
1751                        || ! (char_type[ *(tp - 1) & UCHARMAX] & HSP)) {
1752                 *tp++ = ' ';                /* Squeeze white spaces */
1753             } else if (mcpp_mode == OLD_PREP && tp > temp
1754                        && *(tp - 1) == COM_SEP) {
1755                 *(tp - 1) = ' ';    /* Replace COM_SEP with ' '     */
1756             }
1757             break;
1758         case '"':                           /* String literal       */
1759         case '\'':                          /* Character constant   */
1760             infile->bptr = sp;
1761             if (standard) {
1762                 tp = scan_quote( c, tp, limit, TRUE);
1763             } else {
1764                 in_string = TRUE;   /* Enable line splicing by scan_quote() */
1765                 tp = scan_quote( c, tp, limit, TRUE);   /* (not by get_ch())*/
1766                 in_string = FALSE;
1767             }
1768             if (tp == NULL) {
1769                 free( temp);                /* Unbalanced quotation */
1770                 return  parse_line();       /* Skip the line        */
1771             }
1772             sp = infile->bptr;
1773             break;
1774         default:
1775             if (iscntrl( c)) {
1776                 cerror(             /* Skip the control character   */
1777     "Illegal control character %.0s0x%02x, skipped the character"    /* _E_  */
1778                         , NULL, (long) c, NULL);
1779             } else {                        /* Any valid character  */
1780                 *tp++ = c;
1781             }
1782             break;
1783         }
1784 
1785         if (limit < tp) {
1786             *tp = EOS;
1787             cfatal( "Too long line spliced by comments"     /* _F_  */
1788                     , NULL, 0L, NULL);
1789         }
1790     }
1791 
1792 end_line:
1793     if (temp < tp && (char_type[ *(tp - 1) & UCHARMAX] & HSP))
1794         tp--;                       /* Remove trailing white space  */
1795     *tp++ = '\n';
1796     *tp = EOS;
1797     infile->bptr = strcpy( infile->buffer, temp);   /* Write back to buffer */
1798     free( temp);
1799     if (macro_line != 0 && macro_line != MACRO_ERROR) { /* Expanding macro  */
1800         temp = infile->buffer;
1801         while (char_type[ *temp & UCHARMAX] & HSP)
1802             temp++;
1803         if (*temp == '#'        /* This line starts with # token    */
1804                 || (mcpp_mode == STD && *temp == '%' && *(temp + 1) == ':'))
1805             if (warn_level & 1)
1806                 cwarn(
1807     "Macro started at line %.0s%ld swallowed directive-like line"   /* _W1_ */
1808                     , NULL, macro_line, NULL);
1809     }
1810     return  infile->buffer;
1811 }
1812 
read_a_comment(char * sp,size_t * sizp)1813 static char *   read_a_comment(
1814     char *      sp,                         /* Source               */
1815     size_t *    sizp                        /* Size of the comment  */
1816 )
1817 /*
1818  * Read over a comment (which may cross the lines).
1819  */
1820 {
1821     int         c;
1822     char *      saved_sp;
1823     int         cat_line = 0;       /* Number of catenated lines    */
1824 
1825     if (keep_spaces) {
1826         saved_sp = sp - 2;          /* '-2' for beginning / and *   */
1827         *sizp = 0;
1828     }
1829     if (keep_comments)                      /* If writing comments  */
1830         mcpp_fputs( "/*", OUT);             /* Write the initializer*/
1831     c = *sp++;
1832 
1833     while (1) {                             /* Eat a comment        */
1834         if (keep_comments)
1835             mcpp_fputc( c, OUT);
1836 
1837         switch (c) {
1838         case '/':
1839             if ((c = *sp++) != '*')         /* Don't let comments   */
1840                 continue;                   /*   nest.              */
1841             if (warn_level & 1)
1842                 cwarn( "\"/*\" within comment", NULL, 0L, NULL);    /* _W1_ */
1843             if (keep_comments)
1844                 mcpp_fputc( c, OUT);
1845                                             /* Fall into * stuff    */
1846         case '*':
1847             if ((c = *sp++) != '/')         /* If comment doesn't   */
1848                 continue;                   /*   end, look at next. */
1849             if (keep_comments) {            /* Put out comment      */
1850                 mcpp_fputc( c, OUT);        /*   terminator, too.   */
1851                 mcpp_fputc( '\n', OUT);     /* Append '\n' to avoid */
1852                     /*  trouble on some other tools such as rpcgen. */
1853                 wrong_line = TRUE;
1854             }
1855             if (keep_spaces)                /* Save the length      */
1856                 *sizp = *sizp + (sp - saved_sp);
1857             if ((mcpp_debug & MACRO_CALL) && compiling) {
1858                 if (cat_line) {
1859                     cat_line++;
1860                     com_cat_line.len[ cat_line]         /* Catenated length */
1861                             = com_cat_line.len[ cat_line - 1]
1862                                 + strlen( infile->buffer) - 1;
1863                                             /* '-1' for '\n'        */
1864                     com_cat_line.last_line = src_line;
1865                 }
1866             }
1867             return  sp;                     /* End of comment       */
1868         case '\n':                          /* Line-crossing comment*/
1869             if (keep_spaces)                /* Save the length      */
1870                 *sizp = *sizp + (sp - saved_sp) - 1;    /* '-1' for '\n'    */
1871             if ((mcpp_debug & MACRO_CALL) && compiling) {
1872                                     /* Save location informations   */
1873                 if (cat_line == 0)  /* First line of catenation     */
1874                     com_cat_line.start_line = src_line;
1875                 if (cat_line >= MAX_CAT_LINE - 1) {
1876                     *sizp = 0;      /* Discard the too long comment */
1877                     cat_line = 0;
1878                     if (warn_level & 4)
1879                         cwarn(
1880                         "Too long comment, discarded up to here"    /* _W4_ */
1881                                 , NULL, 0L, NULL);
1882                 }
1883                 cat_line++;
1884                 com_cat_line.len[ cat_line]
1885                         = com_cat_line.len[ cat_line - 1]
1886                             + strlen( infile->buffer) - 1;
1887             }
1888             if ((saved_sp = sp = get_line( TRUE)) == NULL)
1889                 return  NULL;       /* End of file within comment   */
1890                 /* Never happen, because at_eof() supplement closing*/
1891             wrong_line = TRUE;      /* We'll need a #line later     */
1892             break;
1893         default:                            /* Anything else is     */
1894             break;                          /*   just a character   */
1895         }                                   /* End switch           */
1896 
1897         c = *sp++;
1898     }                                       /* End comment loop     */
1899 
1900     return  sp;                             /* Never reach here     */
1901 }
1902 
mcpp_fgets(char * s,int size,FILE * stream)1903 static char *   mcpp_fgets(
1904     char *  s,
1905     int     size,
1906     FILE *  stream
1907 )
1908 {
1909     return fgets( s, size, stream);
1910 }
1911 
get_line(int in_comment)1912 static char *   get_line(
1913     int     in_comment
1914 )
1915 /*
1916  * ANSI (ISO) C: translation phase 1, 2.
1917  * Get the next logical line from source file.
1918  * Convert [CR+LF] to [LF].
1919  */
1920 {
1921 #if COMPILER == INDEPENDENT
1922 #define cr_warn_level 1
1923 #else
1924 #define cr_warn_level 2
1925 #endif
1926     static int  cr_converted;
1927     int     converted = FALSE;
1928     int     len;                            /* Line length - alpha  */
1929     char *  ptr;
1930     int     cat_line = 0;           /* Number of catenated lines    */
1931 
1932     if (infile == NULL)                     /* End of a source file */
1933         return  NULL;
1934     ptr = infile->bptr = infile->buffer;
1935     if ((mcpp_debug & MACRO_CALL) && src_line == 0) /* Initialize   */
1936         com_cat_line.last_line = bsl_cat_line.last_line = 0L;
1937 
1938     while (mcpp_fgets( ptr, (int) (infile->buffer + NBUFF - ptr), infile->fp)
1939             != NULL) {
1940         /* Translation phase 1  */
1941         src_line++;                 /* Gotten next physical line    */
1942         if (standard && src_line == std_limits.line_num + 1
1943                 && (warn_level & 1))
1944             cwarn( "Line number %.0s\"%ld\" got beyond range"       /* _W1_ */
1945                     , NULL, src_line, NULL);
1946         if (mcpp_debug & (TOKEN | GETC)) {  /* Dump it to DBG       */
1947             mcpp_fprintf( DBG, "\n#line %ld (%s)", src_line, cur_fullname);
1948             dump_string( NULL, ptr);
1949         }
1950         len = strlen( ptr);
1951         if (len == 0)
1952                 cwarn( "null character ignored", NULL, 0L, NULL);
1953 
1954         if (NBUFF - 1 <= ptr - infile->buffer + len
1955                 && *(ptr + len - 1) != '\n') {
1956                 /* The line does not yet end, though the buffer is full.    */
1957             if (NBUFF - 1 <= len)
1958                 cfatal( "Too long source line"              /* _F_  */
1959                         , NULL, 0L, NULL);
1960             else
1961                 cfatal( "Too long logical line"             /* _F_  */
1962                         , NULL, 0L, NULL);
1963         }
1964         if (*(ptr + len - 1) != '\n')   /* Unterminated source line */
1965             break;
1966         if (len >= 2 && *(ptr + len - 2) == '\r') {         /* [CR+LF]      */
1967             *(ptr + len - 2) = '\n';
1968             *(ptr + --len) = EOS;
1969             if (! cr_converted && (warn_level & cr_warn_level)) {
1970                 cwarn( "Converted [CR+LF] to [LF]"  /* _W1_ _W2_    */
1971                         , NULL, 0L, NULL);
1972                 cr_converted = TRUE;
1973             }
1974         }
1975         if (standard) {
1976             if (option_flags.trig)
1977                 converted = cnv_trigraph( ptr);
1978             if (mcpp_mode == POST_STD && option_flags.dig)
1979                 converted += cnv_digraph( ptr);
1980             if (converted)
1981                 len = strlen( ptr);
1982             /* Translation phase 2  */
1983             len -= 2;
1984             if (len >= 0) {
1985                 if ((*(ptr + len) == '\\') && ! last_is_mbchar( ptr, len)) {
1986                             /* <backslash><newline> (not MBCHAR)    */
1987                     ptr = infile->bptr += len;  /* Splice the lines */
1988                     wrong_line = TRUE;
1989                     if ((mcpp_debug & MACRO_CALL) && compiling) {
1990                                     /* Save location informations   */
1991                         if (cat_line == 0)      /* First line of catenation */
1992                             bsl_cat_line.start_line = src_line;
1993                         if (cat_line < MAX_CAT_LINE)
1994                                     /* Record the catenated length  */
1995                             bsl_cat_line.len[ ++cat_line]
1996                                     = strlen( infile->buffer) - 2;
1997                         /* Else ignore  */
1998                     }
1999                     continue;
2000                 }
2001             }
2002 #if NBUFF-2 > SLEN90MIN
2003             if (ptr - infile->buffer + len + 2 > std_limits.str_len + 1
2004                     && (warn_level & 4))    /* +1 for '\n'          */
2005             cwarn( "Logical source line longer than %.0s%ld bytes"  /* _W4_ */
2006                         , NULL, std_limits.str_len, NULL);
2007 #endif
2008         }
2009         if ((mcpp_debug & MACRO_CALL) && compiling) {
2010             if (cat_line && cat_line < MAX_CAT_LINE) {
2011                 bsl_cat_line.len[ ++cat_line] = strlen( infile->buffer) - 1;
2012                                 /* Catenated length: '-1' for '\n'  */
2013                 bsl_cat_line.last_line = src_line;
2014             }
2015         }
2016         return  infile->bptr = infile->buffer;      /* Logical line */
2017     }
2018 
2019     /* End of a (possibly included) source file */
2020     if (ferror( infile->fp))
2021         cfatal( "File read error", NULL, 0L, NULL);         /* _F_  */
2022     if ((ptr = at_eof( in_comment)) != NULL)        /* Check at end of file */
2023         return  ptr;                        /* Partial line supplemented    */
2024     if (option_flags.z) {
2025         no_output--;                        /* End of included file */
2026         keep_comments = option_flags.c && compiling && !no_output;
2027     }
2028     return  NULL;
2029 }
2030 
2031 #define TRIOFFSET       10
2032 
cnv_trigraph(char * in)2033 int     cnv_trigraph(
2034     char *      in
2035 )
2036 /*
2037  * Perform in-place trigraph replacement on a physical line.  This was added
2038  * to the C90.  In an input text line, the sequence ??[something] is
2039  * transformed to a character (which might not appear on the input keyboard).
2040  */
2041 {
2042     const char * const  tritext = "=(/)'<!>-\0#[\\]^{|}~";
2043     /*                             ^          ^
2044      *                             +----------+
2045      *                             this becomes this
2046      */
2047     int     count = 0;
2048     const char *    tp;
2049 
2050     while ((in = strchr( in, '?')) != NULL) {
2051         if (*++in != '?')
2052             continue;
2053         while (*++in == '?')
2054             ;
2055         if ((tp = strchr( tritext, *in)) == NULL)
2056             continue;
2057         *(in - 2) = *(tp + TRIOFFSET);
2058         in--;
2059         memmove( in, in + 2, strlen( in + 1));
2060         count++;
2061     }
2062 
2063     if (count && (warn_level & 16))
2064         cwarn( "%.0s%ld trigraph(s) converted"          /* _W16_    */
2065                 , NULL, (long) count, NULL);
2066     return  count;
2067 }
2068 
cnv_digraph(char * in)2069 int     cnv_digraph(
2070     char *      in
2071 )
2072 /*
2073  * Perform in-place digraph replacement on a physical line.
2074  * Called only in POST_STD mode.
2075  */
2076 {
2077     int     count = 0;
2078     int     i;
2079     int     c1, c2;
2080 
2081     while ((i = strcspn( in, "%:<")), (c1 = *(in + i)) != '\0') {
2082         in += i + 1;
2083         c2 = *in;
2084         switch (c1) {
2085         case '%'    :
2086             switch (c2) {
2087             case ':'    :   *(in - 1) = '#';    break;
2088             case '>'    :   *(in - 1) = '}';    break;
2089             default     :   continue;
2090             }
2091             break;
2092         case ':'    :
2093             switch (c2) {
2094             case '>'    :   *(in - 1) = ']';    break;
2095             default     :   continue;
2096             }
2097             break;
2098         case '<'    :
2099             switch (c2) {
2100             case '%'    :   *(in - 1) = '{';    break;
2101             case ':'    :   *(in - 1) = '[';    break;
2102             default     :   continue;
2103             }
2104             break;
2105         }
2106         memmove( in, in + 1, strlen( in));
2107         count++;
2108     }
2109 
2110     if (count && (warn_level & 16))
2111         cwarn( "%.0s%ld digraph(s) converted"           /* _W16_    */
2112                 , NULL, (long) count, NULL);
2113     return  count;
2114 }
2115 
at_eof(int in_comment)2116 static char *   at_eof(
2117     int     in_comment
2118 )
2119 /*
2120  * Check the partial line, unterminated comment, unbalanced #if block,
2121  * uncompleted macro call at end of a file or at end of input.
2122  * Supplement the line terminator, if possible.
2123  * Return the supplemented line or NULL on unrecoverable error.
2124  */
2125 {
2126     const char * const  format
2127             = "End of %s with %.0ld%s";                 /* _E_ _W1_ */
2128     const char * const  unterm_if_format
2129 = "End of %s within #if (#ifdef) section started at line %ld";  /* _E_ _W1_ */
2130     const char * const  unterm_macro_format
2131             = "End of %s within macro call started at line %ld";/* _E_ _W1_ */
2132     const char * const  input
2133             = infile->parent ? "file" : "input";        /* _E_ _W1_ */
2134     const char * const  no_newline
2135             = "no newline, supplemented newline";       /* _W1_     */
2136     const char * const  unterm_com
2137             = "unterminated comment, terminated the comment";   /* _W1_     */
2138     const char * const  backsl = "\\, deleted the \\";  /* _W1_     */
2139     const char * const  unterm_asm_format
2140 = "End of %s with unterminated #asm block started at line %ld"; /* _E_ _W1_ */
2141     size_t  len;
2142     char *  cp;
2143 
2144     cp = infile->buffer;
2145     len = strlen( cp);
2146     if (len && *(cp += (len - 1)) != '\n') {
2147         *++cp = '\n';                       /* Supplement <newline> */
2148         *++cp = EOS;
2149         if (mcpp_mode != OLD_PREP && (warn_level & 1))
2150             cwarn( format, input, 0L, no_newline);
2151         return  infile->bptr = infile->buffer;
2152     }
2153     if (standard && infile->buffer < infile->bptr) {
2154                             /* No line after <backslash><newline>   */
2155         cp = infile->bptr;
2156         *cp++ = '\n';                       /* Delete the \\        */
2157         *cp = EOS;
2158         if (warn_level & 1)
2159             cwarn( format, input, 0L, backsl);
2160         return  infile->bptr = infile->buffer;
2161     }
2162     if (in_comment) {               /* End of file within a comment */
2163         if (mcpp_mode != OLD_PREP && (warn_level & 1))
2164             cwarn( format, input, 0L, unterm_com);
2165         /* The partial comment line has been already read by        */
2166         /* read_a_comment(), so supplement the  next line.          */
2167         strcpy( infile->buffer, "*/\n");
2168         return  infile->bptr = infile->buffer;
2169     }
2170 
2171     if (infile->initif < ifptr) {
2172         IFINFO *    ifp = infile->initif + 1;
2173         if (standard) {
2174             cerror( unterm_if_format, input, ifp->ifline, NULL);
2175             ifptr = infile->initif;         /* Clear information of */
2176             compiling = ifptr->stat;        /*   erroneous grouping */
2177         } else if (mcpp_mode == KR && (warn_level & 1)) {
2178             cwarn( unterm_if_format, input, ifp->ifline, NULL);
2179         }
2180     }
2181 
2182     if (macro_line != 0 && macro_line != MACRO_ERROR
2183             && ((mcpp_mode == STD && in_getarg) || ! standard)) {
2184         if (standard) {
2185             cerror( unterm_macro_format, input, macro_line, NULL);
2186             macro_line = MACRO_ERROR;
2187         } else if (warn_level & 1) {
2188             cwarn( unterm_macro_format, input, macro_line, NULL);
2189         }
2190     }
2191 
2192     if (in_asm && mcpp_mode == KR && (warn_level & 1))
2193         cwarn( unterm_asm_format, input, in_asm, NULL);
2194 
2195     return  NULL;
2196 }
2197 
unget_ch(void)2198 void    unget_ch( void)
2199 /*
2200  * Back the pointer to reread the last character.  Fatal error (code bug)
2201  * if we back too far.  unget_ch() may be called, without problems, at end of
2202  * file.  Only one character may be ungotten.  If you need to unget more,
2203  * call unget_string().
2204  */
2205 {
2206     if (in_token) {
2207         infile->bptr--;
2208         return;
2209     }
2210 
2211     if (infile != NULL) {
2212         if (mcpp_mode == POST_STD && infile->fp) {
2213             switch (insert_sep) {
2214             case INSERTED_SEP:  /* Have just read an inserted separator */
2215                 insert_sep = INSERT_SEP;
2216                 return;
2217             case INSERT_SEP:
2218                 cfatal( "Bug: unget_ch() just after scan_token()"   /* _F_  */
2219                         , NULL, 0L, NULL);
2220                 break;
2221             default:
2222                 break;
2223             }
2224         }
2225         --infile->bptr;
2226         if (infile->bptr < infile->buffer)      /* Shouldn't happen */
2227             cfatal( "Bug: Too much pushback", NULL, 0L, NULL);      /* _F_  */
2228     }
2229 
2230     if (mcpp_debug & GETC)
2231         dump_unget( "after unget");
2232 }
2233 
unget_string(const char * text,const char * name)2234 FILEINFO *  unget_string(
2235     const char *    text,               /* Text to unget            */
2236     const char *    name                /* Name of the macro, if any*/
2237 )
2238 /*
2239  * Push a string back on the input stream.  This is done by treating
2240  * the text as if it were a macro or a file.
2241  */
2242 {
2243     FILEINFO *      file;
2244     size_t          size;
2245 
2246     if (text)
2247         size = strlen( text) + 1;
2248     else
2249         size = 1;
2250     file = get_file( name, NULL, NULL, size, FALSE);
2251     if (text)
2252         memcpy( file->buffer, text, size);
2253     else
2254         *file->buffer = EOS;
2255     return  file;
2256 }
2257 
save_string(const char * text)2258 char *  save_string(
2259     const char *      text
2260 )
2261 /*
2262  * Store a string into free memory.
2263  */
2264 {
2265     char *      result;
2266     size_t      size;
2267 
2268     size = strlen( text) + 1;
2269     result = xmalloc( size);
2270     memcpy( result, text, size);
2271     return  result;
2272 }
2273 
get_file(const char * name,const char * src_dir,char * fullname,size_t bufsize,int include_opt)2274 FILEINFO *  get_file(
2275     const char *    name,                   /* File or macro name   */
2276     const char *    src_dir,                /* Source file directory*/
2277     char *          fullname,               /* Full path list       */
2278     size_t      bufsize,                    /* Line buffer size     */
2279     int         include_opt         /* Specified by -include opt (for GCC)  */
2280 )
2281 /*
2282  * Common FILEINFO buffer initialization for a new file or macro.
2283  */
2284 {
2285     FILEINFO *  file;
2286 
2287     file = (FILEINFO *) xmalloc( sizeof (FILEINFO));
2288     file->buffer = xmalloc( bufsize);
2289     file->bptr = file->buffer;              /* Initialize line ptr  */
2290     file->buffer[ 0] = EOS;                 /* Force first read     */
2291     file->line = 0L;                        /* (Not used just yet)  */
2292     file->fp = NULL;                        /* No file yet          */
2293     file->pos = 0L;                         /* No pos to remember   */
2294     file->parent = infile;                  /* Chain files together */
2295     file->initif = ifptr;                   /* Initial ifstack      */
2296     file->include_opt = include_opt;        /* Specified by -include*/
2297     file->dirp = NULL;                      /* No include dir yet   */
2298     file->real_fname = name;                /* Save file/macro name */
2299     file->full_fname = fullname;            /* Full path list       */
2300     if (name) {
2301         file->filename = xmalloc( strlen( name) + 1);
2302         strcpy( file->filename, name);      /* Copy for #line       */
2303     } else {
2304         file->filename = NULL;
2305     }
2306     if (src_dir) {
2307         file->src_dir = xmalloc( strlen( src_dir) + 1);
2308         strcpy( file->src_dir, src_dir);
2309     } else {
2310         file->src_dir = NULL;
2311     }
2312 #if MCPP_LIB
2313     file->last_fputc = mcpp_lib_fputc;
2314     file->last_fputs = mcpp_lib_fputs;
2315     file->last_fprintf = mcpp_lib_fprintf;
2316 #endif
2317     if (infile != NULL) {                   /* If #include file     */
2318         infile->line = src_line;            /* Save current line    */
2319 #if MCPP_LIB
2320         infile->last_fputc = mcpp_fputc;
2321         infile->last_fputs = mcpp_fputs;
2322         infile->last_fprintf = mcpp_fprintf;
2323 #endif
2324     }
2325     infile = file;                          /* New current file     */
2326 
2327     return  file;                           /* All done.            */
2328 }
2329 
2330 static const char * const   out_of_memory
2331     = "Out of memory (required size is %.0s0x%lx bytes)";   /* _F_  */
2332 
2333 char *
2334 (xmalloc)(
2335     size_t      size
2336 )
2337 /*
2338  * Get a block of free memory.
2339  */
2340 {
2341     char *      result;
2342 
2343     if ((result = (char *) malloc( size)) == NULL) {
2344         if (mcpp_debug & MEMORY)
2345             print_heap();
2346        cfatal( out_of_memory, NULL, (long) size, NULL);
2347     }
2348     return  result;
2349 }
2350 
2351 char *  (xrealloc)(
2352     char *      ptr,
2353     size_t      size
2354 )
2355 /*
2356  * Reallocate malloc()ed memory.
2357  */
2358 {
2359     char *      result;
2360 
2361     if ((result = (char *) realloc( ptr, size)) == NULL && size != 0) {
2362         /* 'size != 0' is necessary to cope with some               */
2363         /*   implementation of realloc( ptr, 0) which returns NULL. */
2364         if (mcpp_debug & MEMORY)
2365             print_heap();
2366         cfatal( out_of_memory, NULL, (long) size, NULL);
2367     }
2368     return  result;
2369 }
2370 
get_src_location(LINE_COL * p_line_col)2371 LINE_COL *  get_src_location(
2372     LINE_COL *  p_line_col          /* Line and column on phase 4   */
2373 )
2374 /*
2375  * Convert line-column datum of just after translation phase 3 into that of
2376  * phase 2, tracing back line splicing by a comment and <backslash><newline>.
2377  * Note: This conversion does not give correct datum on a line catenated by
2378  * both of <backslash><newline> and line-crossing-comment at the same time.
2379  *
2380  * com_cat_line and bsl_cat_line have data only on last catenated line.
2381  * com_cat_line.len[] and bsl_cat_line.len[] have the length of catenated
2382  * line, and len[ 0] is always 0, followed by len[ 1], len[ 2], ..., as
2383  * accumulated length of successively catenated lines.
2384  */
2385 {
2386     long        line;
2387     size_t      col;
2388     size_t *    cols;
2389     CAT_LINE *  l_col_p;
2390     int         i;
2391 
2392     line = p_line_col->line;
2393     col = p_line_col->col;
2394 
2395     for (i = 0; i <= 1; i++) {
2396         l_col_p = i ? & bsl_cat_line : & com_cat_line;
2397         if (l_col_p->last_line != line)
2398             continue;
2399         /* Else just catenated line */
2400         cols = l_col_p->len + 1;
2401         while (*cols < col)
2402             cols++;
2403         if (col <= *cols) {
2404             cols--;
2405             col -= *cols;
2406         }
2407         line = l_col_p->start_line + (cols - l_col_p->len);
2408     }
2409 
2410     p_line_col->line = line;
2411     p_line_col->col = col + 1;
2412                     /* col internally start at 0, output start at 1 */
2413 
2414     return  p_line_col;
2415 }
2416 
put_line(char * out,FILE * fp)2417 static void put_line(
2418     char *  out,
2419     FILE *  fp
2420 )
2421 /*
2422  * Put out a logical source line.
2423  * This routine is called only in OLD_PREP mode.
2424  */
2425 {
2426     int     c;
2427 
2428     while ((c = *out++) != EOS) {
2429         if (c != COM_SEP)           /* Skip 0-length comment        */
2430             mcpp_fputc( c, FP2DEST( fp));
2431     }
2432 }
2433 
do_msg(const char * severity,const char * format,const char * arg1,long arg2,const char * arg3)2434 static void do_msg(
2435     const char *    severity,       /* "fatal", "error", "warning"  */
2436     const char *    format,         /* Format for the error message */
2437     const char *    arg1,           /* String arg. for the message  */
2438     long            arg2,           /* Integer argument             */
2439     const char *    arg3            /* Second string argument       */
2440 )
2441 /*
2442  * Print filenames, macro names, line numbers and error messages.
2443  * Also print macro definitions on macro expansion problems.
2444  */
2445 {
2446     FILEINFO *  file;
2447     DEFBUF *    defp;
2448     int         i;
2449     size_t      slen;
2450     const char *    arg_s[ 2];
2451     char *      arg_t[ 2];
2452     char *      tp;
2453     const char *    sp;
2454     int         c;
2455     int         ind;
2456 
2457     fflush( fp_out);                /* Synchronize output and diagnostics   */
2458     arg_s[ 0] = arg1;  arg_s[ 1] = arg3;
2459 
2460     for (i = 0; i < 2; i++) {   /* Convert special characters to visible    */
2461         sp = arg_s[ i];
2462         if (sp != NULL)
2463             slen = strlen( sp) + 1;
2464         else
2465             slen = 1;
2466         tp = arg_t[ i] = (char *) malloc( slen);
2467             /* Don't use xmalloc() so as not to cause infinite recursion    */
2468         if (sp == NULL || *sp == EOS) {
2469             *tp = EOS;
2470             continue;
2471         }
2472 
2473         while ((c = *sp++) != EOS) {
2474             switch (c) {
2475             case TOK_SEP:
2476                 if (mcpp_mode == OLD_PREP)      /* COM_SEP          */
2477                     break;              /* Skip magic characters    */
2478                 /* Else fall through    */
2479             case RT_END:
2480             case CAT:
2481             case ST_QUOTE:
2482             case DEF_MAGIC:
2483                 if (! standard)
2484                     *tp++ = ' ';
2485                 break;                  /* Skip the magic characters*/
2486             case IN_SRC:
2487                 if (! standard)
2488                     *tp++ = ' ';
2489                 if ((mcpp_debug & MACRO_CALL) && ! in_directive)
2490                     sp += 2;            /* Skip two more bytes      */
2491                 break;
2492             case MAC_INF:
2493                 if (mcpp_mode != STD) {
2494                     *tp++ = ' ';
2495                     /* Illegal control character, convert to a space*/
2496                 } else {
2497                     switch (*sp++) {    /* Skip the magic characters*/
2498                     case MAC_ARG_START  :
2499                         sp++;
2500                         /* Fall through */
2501                     case MAC_CALL_START :
2502                         sp += 2;
2503                         break;
2504                     case MAC_ARG_END    :
2505                         if (! option_flags.v)
2506                             break;
2507                         else
2508                             sp++;
2509                             /* Fall through */
2510                     case MAC_CALL_END   :
2511                         if (option_flags.v)
2512                             sp += 2;
2513                         break;
2514                     }
2515                 }
2516                 break;
2517             case '\n':
2518                 *tp++ = ' ';            /* Convert '\n' to a space  */
2519                 break;
2520             default:
2521                 *tp++ = c;
2522                 break;
2523             }
2524         }
2525 
2526         if (*(sp - 2) == '\n')
2527             tp--;
2528         *tp = EOS;
2529     }
2530 
2531     /* Print source location and diagnostic */
2532     file = infile;
2533     while (file != NULL && (file->fp == NULL || file->fp == (FILE *)-1))
2534         file = file->parent;                        /* Skip macro   */
2535     if (file != NULL) {
2536         file->line = src_line;
2537         mcpp_fprintf( ERR, "%s:%ld: %s: ", cur_fullname, src_line, severity);
2538     }
2539     mcpp_fprintf( ERR, format, arg_t[ 0], arg2, arg_t[ 1]);
2540     mcpp_fputc( '\n', ERR);
2541     if (option_flags.no_source_line)
2542         goto  free_arg;
2543 
2544     /* Print source line, includers and expanding macros    */
2545     file = infile;
2546     if (file != NULL && file->fp != NULL) {
2547         if (mcpp_mode == OLD_PREP) {
2548             mcpp_fputs( "    ", ERR);
2549             put_line( file->buffer, fp_err);
2550         } else {
2551             mcpp_fprintf( ERR, "    %s", file->buffer);
2552                                             /* Current source line  */
2553         }
2554         file = file->parent;
2555     }
2556     while (file != NULL) {                  /* Print #includes, too */
2557         if (file->fp == NULL) {             /* Macro                */
2558             if (file->filename) {
2559                 defp = look_id( file->filename);
2560                 if ((defp->nargs > DEF_NOARGS_STANDARD)
2561                     && ! (file->parent && file->parent->filename
2562                         && str_eq( file->filename, file->parent->filename)))
2563                         /* If the name is not duplicate of parent   */
2564                     dump_a_def( "    macro", defp, FALSE, TRUE, fp_err);
2565             }
2566         } else {                            /* Source file          */
2567             if (file->buffer[ 0] == '\0')
2568                 strcpy( file->buffer, "\n");
2569             if (mcpp_mode != OLD_PREP) {
2570                 mcpp_fprintf( ERR, "    from %s: %ld:    %s",
2571                     file->line ? file->full_fname       /* Full-path-list   */
2572                         : "<stdin>",        /* Included by -include */
2573                     file->line,             /* Current line number  */
2574                     file->buffer);          /* The source line      */
2575             } else {
2576                 mcpp_fprintf( ERR, "    from %s: %ld:    ", file->full_fname
2577                         , file->line);
2578                 put_line( file->buffer, fp_err);
2579             }
2580         }
2581         file = file->parent;
2582     }
2583 
2584     if (! macro_name)
2585         goto  free_arg;
2586     /* Additional information of macro definitions  */
2587     expanding_macro[ 0].name = macro_name;
2588     for (ind = 0; ind <= exp_mac_ind; ind++) {
2589         int         ind_done;
2590 
2591         for (ind_done = 0; ind_done < ind; ind_done++)
2592             if (str_eq( expanding_macro[ ind].name
2593                     , expanding_macro[ ind_done].name))
2594                 break;                      /* Already reported     */
2595         if (ind_done < ind)
2596             continue;
2597         for (file = infile; file; file = file->parent)
2598             if (file->fp == NULL && file->filename
2599                     && str_eq( expanding_macro[ ind].name, file->filename))
2600                 break;                      /* Already reported     */
2601         if (file)
2602             continue;
2603         if ((defp = look_id( expanding_macro[ ind].name)) != NULL) {
2604             if (defp->nargs <= DEF_NOARGS_STANDARD)
2605                 continue;                   /* Standard predefined  */
2606             dump_a_def( "    macro", defp, FALSE, TRUE, fp_err);
2607             /* Macro already read over  */
2608         }
2609     }
2610 
2611 free_arg:
2612     for (i = 0; i < 2; i++)
2613         free( arg_t[ i]);
2614 }
2615 
cfatal(const char * format,const char * arg1,long arg2,const char * arg3)2616 void    cfatal(
2617     const char *    format,
2618     const char *    arg1,
2619     long    arg2,
2620     const char *    arg3
2621 )
2622 /*
2623  * A real disaster.
2624  */
2625 {
2626     do_msg( "fatal error", format, arg1, arg2, arg3);
2627     longjmp( error_exit, -1);
2628 }
2629 
cerror(const char * format,const char * arg1,long arg2,const char * arg3)2630 void    cerror(
2631     const char *    format,
2632     const char *    arg1,
2633     long    arg2,
2634     const char *    arg3
2635 )
2636 /*
2637  * Print a error message.
2638  */
2639 {
2640     do_msg( "error", format, arg1, arg2, arg3);
2641     errors++;
2642 }
2643 
cwarn(const char * format,const char * arg1,long arg2,const char * arg3)2644 void    cwarn(
2645     const char *    format,
2646     const char *    arg1,
2647     long    arg2,
2648     const char *    arg3
2649 )
2650 /*
2651  * Maybe an error.
2652  */
2653 {
2654     do_msg( "warning", format, arg1, arg2, arg3);
2655 }
2656 
dump_string(const char * why,const char * text)2657 void    dump_string(
2658     const char *    why,
2659     const char *    text
2660 )
2661 /*
2662  * Dump text readably.
2663  * Bug: macro argument number may be putout as a control character or any
2664  * other character, just after MAC_PARM has been read away.
2665  */
2666 {
2667     const char *    cp;
2668     const char *    chr;
2669     int     c, c1, c2;
2670 
2671     if (why != NULL)
2672         mcpp_fprintf( DBG, " (%s)", why);
2673     mcpp_fputs( " => ", DBG);
2674 
2675     if (text == NULL) {
2676         mcpp_fputs( "NULL", DBG);
2677         return;
2678     }
2679 
2680     for (cp = text; (c = *cp++ & UCHARMAX) != EOS; ) {
2681         chr = NULL;
2682 
2683         switch (c) {
2684         case MAC_PARM:
2685             c = *cp++ & UCHARMAX;       /* Macro parameter number   */
2686             mcpp_fprintf( DBG, "<%d>", c);
2687             break;
2688         case MAC_INF:
2689             if (! (mcpp_mode == STD && (mcpp_debug & MACRO_CALL)))
2690                 goto  no_magic;
2691             /* Macro informations inserted by -K option */
2692             c2 = *cp++ & UCHARMAX;
2693             if (option_flags.v || c2 == MAC_CALL_START
2694                     || c2 == MAC_ARG_START) {
2695                 c = ((*cp++ & UCHARMAX) - 1) * UCHARMAX;
2696                 c += (*cp++ & UCHARMAX) - 1;
2697             }
2698             switch (c2) {
2699             case MAC_CALL_START:
2700                 mcpp_fprintf( DBG, "<MAC%d>", c);
2701                 break;
2702             case MAC_CALL_END:
2703                 if (option_flags.v)
2704                     mcpp_fprintf( DBG, "<MAC_END%d>", c);
2705                 else
2706                     chr = "<MAC_END>";
2707                 break;
2708             case MAC_ARG_START:
2709                 c1 = *cp++ & UCHARMAX;
2710                 mcpp_fprintf( DBG, "<MAC%d:ARG%d>", c, c1 - 1);
2711                 break;
2712             case MAC_ARG_END:
2713                 if (option_flags.v) {
2714                     c1 = *cp++ & UCHARMAX;
2715                     mcpp_fprintf( DBG, "<ARG_END%d-%d>", c, c1 - 1);
2716                 } else {
2717                     chr = "<ARG_END>";
2718                 }
2719                 break;
2720             }
2721             break;
2722         case DEF_MAGIC:
2723             if (standard) {
2724                 chr = "<MAGIC>";
2725                 break;
2726             }       /* Else fall through    */
2727         case CAT:
2728             if (standard) {
2729                 chr = "##";
2730                 break;
2731             }       /* Else fall through    */
2732         case ST_QUOTE:
2733             if (standard) {
2734                 chr = "#";
2735                 break;
2736             }       /* Else fall through    */
2737         case RT_END:
2738             if (standard) {
2739                 chr = "<RT_END>";
2740                 break;
2741             }       /* Else fall through    */
2742         case IN_SRC:
2743             if (standard) {
2744                 if ((mcpp_debug & MACRO_CALL) && ! in_directive) {
2745                     int     num;
2746                     num = ((*cp++ & UCHARMAX) - 1) * UCHARMAX;
2747                     num += (*cp++ & UCHARMAX) - 1;
2748                     mcpp_fprintf( DBG, "<SRC%d>", num);
2749                 } else {
2750                     chr = "<SRC>";
2751                 }
2752             } else {                        /* Control character    */
2753                 mcpp_fprintf( DBG, "<^%c>", c + '@');
2754             }
2755             break;
2756         case TOK_SEP:
2757             if (mcpp_mode == STD) {
2758                 chr = "<TSEP>";
2759                 break;
2760             } else if (mcpp_mode == OLD_PREP) {     /* COM_SEP      */
2761                 chr = "<CSEP>";
2762                 break;
2763             }       /* Else fall through    */
2764         default:
2765 no_magic:
2766             if (c < ' ')
2767                 mcpp_fprintf( DBG, "<^%c>", c + '@');
2768             else
2769                 mcpp_fputc( c, DBG);
2770             break;
2771         }
2772 
2773         if (chr)
2774             mcpp_fputs( chr, DBG);
2775     }
2776 
2777     mcpp_fputc( '\n', DBG);
2778 }
2779 
dump_unget(const char * why)2780 void    dump_unget(
2781     const char *    why
2782 )
2783 /*
2784  * Dump all ungotten junk (pending macros and current input lines).
2785  */
2786 {
2787     const FILEINFO *    file;
2788 
2789     mcpp_fputs( "dump of pending input text", DBG);
2790     if (why != NULL) {
2791         mcpp_fputs( "-- ", DBG);
2792         mcpp_fputs( why, DBG);
2793     }
2794     mcpp_fputc( '\n', DBG);
2795 
2796     for (file = infile; file != NULL; file = file->parent)
2797         dump_string( file->real_fname ? file->real_fname
2798                 : file->filename ? file->filename : "NULL", file->bptr);
2799 }
2800 
dump_token(int token_type,const char * cp)2801 static void dump_token(
2802     int     token_type,
2803     const char *    cp                              /* Token        */
2804 )
2805 /*
2806  * Dump a token.
2807  */
2808 {
2809     static const char * const   t_type[]
2810             = { "NAM", "NUM", "STR", "WSTR", "CHR", "WCHR", "OPE", "SPE"
2811             , "SEP", };
2812 
2813     mcpp_fputs( "token", DBG);
2814     dump_string( t_type[ token_type - NAM], cp);
2815 }
2816 
2817