forked from zeroc-ice/mcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupport.c
1985 lines (1825 loc) · 67.1 KB
/
support.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* Copyright (c) 1998, 2002-2008 Kiyoshi Matsui <kmatsui@t3.rim.or.jp>
* All rights reserved.
*
* Some parts of this code are derived from the public domain software
* DECUS cpp (1984,1985) written by Martin Minow.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* S U P P O R T . C
* S u p p o r t R o u t i n e s
*
* The common routines used by several source files are placed here.
*/
/*
* The following are global functions.
*
* get_unexpandable() Gets the next unexpandable token in the line, expanding
* macros.
* Called from #if, #line and #include processing routines.
* skip_nl() Skips over a line.
* skip_ws() Skips over white spaces but not skip over the end of the line.
* skip_ws() skips also COM_SEP and TOK_SEP.
* scan_token() Reads the next token of any type into the specified output
* pointer, advances the pointer, returns the type of token.
* scan_quote() Reads a string literal, character constant or header-name from
* the input stream, writes out to the specified buffer and
* returns the advanced output pointer.
* get_ch() Reads the next byte from the current input stream, handling
* end of (macro/file) input and embedded comments appropriately.
* unget_ch() Pushs last gotten character back on the input stream.
* unget_string() Pushs sequence on the input stream.
* save_string() Saves a string in malloc() memory.
* get_file() Initializes a new FILEINFO structure, called when #include
* opens a new file, or from unget_string().
* xmalloc() Gets a specified number of bytes from heap memory.
* If malloc() returns NULL, exits with a message.
* xrealloc() realloc(). If it fails, exits with a message.
* get_src_location() Trace back line-column datum into pre-line-splicing
* phase. A function for -K option.
* cfatal(), cerror(), cwarn()
* These routines format print messages to the user.
* mcpp_fputc(), mcpp_fputs(), mcpp_fprintf()
* Wrap library functions to support alternate output to memory
* buffer.
*/
#include "system.H"
#include "internal.H"
static void scan_id( int c);
/* Scan an identifier */
static char * scan_number( int c, char * out, char * out_end);
/* Scan a preprocessing number */
static char * scan_op( int c, char * out);
/* Scan an operator or a punctuator */
static char * parse_line( void);
/* Parse a logical line and convert comments */
static char * read_a_comment( char * sp, size_t * sizp);
/* Read over a comment */
static char * get_line( int in_comment);
/* Get a logical line from file, handle line-splicing */
static char * at_eof( int in_comment);
/* Check erroneous end of file */
static void do_msg( const char * severity, const char * format
, const char * arg1, long arg2, const char * arg3);
/* Putout diagnostic message */
static void dump_token( int token_type, const char * cp);
/* Dump a token and its type */
#define EXP_MAC_IND_MAX 16
/* Information of current expanding macros for diagnostic */
static struct {
const char * name; /* Name of the macro just expanded */
int to_be_freed; /* Name should be freed later */
} expanding_macro[ EXP_MAC_IND_MAX];
static int exp_mac_ind = 0; /* Index into expanding_macro[] */
static int in_token = FALSE; /* For token scanning functions */
static int in_string = FALSE; /* For get_ch() and parse_line()*/
static int squeezews = FALSE;
#define MAX_CAT_LINE 256
/* Information on line catenated by <backslash><newline> */
/* and by line-crossing comment. This is for -K option. */
typedef struct catenated_line {
long start_line; /* Starting line of catenation */
long last_line; /* Ending line of catanation */
size_t len[ MAX_CAT_LINE + 1];
/* Length of successively catenated lines */
} CAT_LINE;
static CAT_LINE bsl_cat_line;
/* Datum on the last catenated line by <backslash><newline> */
static CAT_LINE com_cat_line;
/* Datum on the last catenated line by a line-crossing comment */
static int use_mem_buffers = FALSE;
void init_support( void)
{
in_token = in_string = squeezews = FALSE;
bsl_cat_line.len[ 0] = com_cat_line.len[ 0] = 0;
clear_exp_mac();
}
typedef struct mem_buf {
char * buffer;
char * entry_pt;
size_t size;
size_t bytes_avail;
} MEMBUF;
static MEMBUF mem_buffers[ NUM_OUTDEST];
void mcpp_use_mem_buffers(
int tf
)
{
int i;
use_mem_buffers = tf ? TRUE : FALSE;
for (i = 0; i < NUM_OUTDEST; ++i) {
if (mem_buffers[ i].buffer)
/* Free previously allocated memory buffer */
free( mem_buffers[ i].buffer);
if (use_mem_buffers) {
/* Output to memory buffers instead of files */
mem_buffers[ i].buffer = NULL;
mem_buffers[ i].entry_pt = NULL;
mem_buffers[ i].size = 0;
mem_buffers[ i].bytes_avail = 0;
}
}
}
#define BUF_INCR_SIZE (NWORK * 2)
#define MAX( a, b) (((a) > (b)) ? (a) : (b))
static char * append_to_buffer(
MEMBUF * mem_buf_p,
const char * string,
size_t length
)
{
if (mem_buf_p->bytes_avail < length + 1) { /* Need to allocate more memory */
size_t size = MAX( BUF_INCR_SIZE, length);
if (mem_buf_p->buffer == NULL) { /* 1st append */
mem_buf_p->size = size;
mem_buf_p->bytes_avail = size;
mem_buf_p->buffer = xmalloc( mem_buf_p->size);
mem_buf_p->entry_pt = mem_buf_p->buffer;
} else {
mem_buf_p->size += size;
mem_buf_p->bytes_avail += size;
mem_buf_p->buffer = xrealloc( mem_buf_p->buffer, mem_buf_p->size);
mem_buf_p->entry_pt = mem_buf_p->buffer + mem_buf_p->size
- mem_buf_p->bytes_avail;
}
}
/* Append the string to the tail of the buffer */
memcpy( mem_buf_p->entry_pt, string, length);
mem_buf_p->entry_pt += length;
mem_buf_p->entry_pt[ 0] = '\0'; /* Terminate the string buffer */
mem_buf_p->bytes_avail -= length;
return mem_buf_p->buffer;
}
static int mem_putc(
int c,
OUTDEST od
)
{
char string[ 1];
string[ 0] = (char) c;
if (append_to_buffer( &(mem_buffers[ od]), string, 1) != NULL)
return 0;
else
return !0;
}
static int mem_puts(
const char * s,
OUTDEST od
)
{
if (append_to_buffer( &(mem_buffers[od]), s, strlen(s)) != NULL)
return 0;
else
return !0;
}
char * mcpp_get_mem_buffer(
OUTDEST od
)
{
return mem_buffers[ od].buffer;
}
#define DEST2FP(od) \
(od == OUT) ? fp_out : \
((od == ERR) ? fp_err : \
((od == DBG) ? fp_debug : \
(NULL)))
/*
* The following mcpp_*() wrapper functions are intended to centralize
* the output generated by MCPP. They support memory buffer alternates to
* each of the primary output streams: out, err, debug. The memory buffer
* output option would be used in a setup where MCPP has been built as a
* function call - i.e. mcpp_lib_main().
*/
int mcpp_lib_fputc(
int c,
OUTDEST od
)
{
if (use_mem_buffers) {
return mem_putc( c, od);
} else {
FILE * stream = DEST2FP( od);
return (stream != NULL) ? fputc( c, stream) : EOF;
}
}
int (* mcpp_fputc)( int c, OUTDEST od) = mcpp_lib_fputc;
int mcpp_lib_fputs(
const char * s,
OUTDEST od
)
{
if (use_mem_buffers) {
return mem_puts( s, od);
} else {
FILE * stream = DEST2FP( od);
return (stream != NULL) ? fputs( s, stream) : EOF;
}
}
int (* mcpp_fputs)( const char * s, OUTDEST od) = mcpp_lib_fputs;
#include <stdarg.h>
int mcpp_lib_fprintf(
OUTDEST od,
const char * format,
...
)
{
va_list ap;
FILE * stream = DEST2FP( od);
if (stream != NULL) {
int rc;
va_start( ap, format);
if (use_mem_buffers) {
static char mem_buffer[ NWORK];
rc = vsprintf( mem_buffer, format, ap);
if (rc != 0) {
rc = mem_puts( mem_buffer, od);
}
} else {
rc = vfprintf( stream, format, ap);
}
va_end( ap);
return rc;
} else {
return EOF;
}
}
int (* mcpp_fprintf)( OUTDEST od, const char * format, ...) = mcpp_lib_fprintf;
void mcpp_reset_def_out_func( void)
{
mcpp_fputc = mcpp_lib_fputc;
mcpp_fputs = mcpp_lib_fputs;
mcpp_fprintf = mcpp_lib_fprintf;
}
void 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, ...)
)
{
mcpp_fputc = func_fputc;
mcpp_fputs = func_fputs;
mcpp_fprintf = func_fprintf;
}
int get_unexpandable(
int c, /* First char of token */
int diag /* Flag of diagnosis */
)
/*
* Get the next unexpandable token in the line, expanding macros.
* Return the token type. The token is written in work_buf[].
* The once expanded macro is never expanded again.
* Called only from the routines processing #if (#elif, #assert), #line and
* #include directives in order to diagnose some subtle macro expansions.
*/
{
DEFBUF * defp = NULL;
FILEINFO * file;
FILE * fp = NULL;
LINE_COL line_col = { 0L, 0};
int token_type = NO_TOKEN;
int has_pragma;
while (c != EOS && c != '\n' /* In a line */
&& (fp = infile->fp /* Preserve current state */
, (token_type
= scan_token( c, (workp = work_buf, &workp), work_end))
== NAM) /* Identifier */
&& fp != NULL /* In source ! */
&& (defp = is_macro( NULL)) != NULL) { /* Macro */
expand_macro( defp, work_buf, work_end, line_col, & has_pragma);
/* Expand macro */
if (has_pragma)
cerror( "_Pragma operator found in directive line" /* _E_ */
, NULL, 0L, NULL);
file = unget_string( work_buf, defp->name); /* Stack to re-read */
c = skip_ws(); /* Skip TOK_SEP */
if (file != infile && macro_line != MACRO_ERROR && (warn_level & 1)) {
/* This diagnostic is issued even if "diag" is FALSE. */
cwarn( "Macro \"%s\" is expanded to 0 token" /* _W1_ */
, defp->name, 0L, NULL);
dump_a_def( " macro", defp, FALSE, TRUE, fp_err);
}
}
if (c == '\n' || c == EOS) {
unget_ch();
return NO_TOKEN;
}
if (diag && fp == NULL && defp && (warn_level & 1)) {
char tmp[ NWORK + 16];
char * tmp_end = tmp + NWORK;
char * tmp_p;
file = unget_string( infile->buffer, defp->name); /* To diagnose */
c = get_ch();
while (file == infile) { /* Search the expanded macro */
if (scan_token( c, (tmp_p = tmp, &tmp_p), tmp_end) != NAM) {
c = get_ch();
continue;
}
if (str_eq( identifier, "defined")) {
cwarn( "Macro \"%s\" is expanded to \"defined\"" /* _W1_ */
, defp->name, 0L, NULL);
break;
}
c = get_ch();
}
if (file == infile) {
infile->bptr += strlen( infile->bptr);
get_ch();
}
unget_ch();
if (token_type == OPE) {
unget_string( work_buf, NULL); /* Set again 'openum' */
scan_token( get_ch(), (workp = work_buf, &workp), work_end);
}
}
return token_type;
}
void skip_nl( void)
/*
* Skip to the end of the current input line.
*/
{
insert_sep = NO_SEP;
while (infile && infile->fp == NULL) { /* Stacked text */
infile->bptr += strlen( infile->bptr);
get_ch(); /* To the parent */
}
if (infile)
infile->bptr += strlen( infile->bptr); /* Source line */
}
int skip_ws( void)
/*
* Skip over horizontal whitespaces.
*/
{
int c;
do {
c = get_ch();
} while (char_type[ c] & HSP);
return c;
}
#define MBMASK 0xFF /* Mask to hide multibyte char */
int scan_token(
int c, /* The first character of the token */
char ** out_pp, /* Pointer to pointer to output buf */
char * out_end /* End of output buffer */
)
/*
* Scan the next token of any type.
* The token is written out to the specified buffer and the output pointer
* is advanced. Token is terminated by EOS. Return the type of token.
* If the token is an identifier, the token is also in identifier[].
* If the token is a operator or punctuator, return OPE.
* If 'c' is token separator, then return SEP.
* If 'c' is not the first character of any known token and not a token
* separator, return SPE.
* In POST_STD mode, inserts token separator (a space) between any tokens of
* source.
*/
{
char * out = *out_pp; /* Output pointer */
int ch_type; /* Type of character */
int token_type = 0; /* Type of token */
int ch;
in_token = TRUE; /* While a token is scanned */
c = c & UCHARMAX;
ch_type = char_type[ c] & MBMASK;
switch (ch_type) {
case LET: /* Probably an identifier */
switch (c) {
case 'L':
ch = get_ch();
if (char_type[ ch] & QUO) { /* char_type[ ch] == QUO */
if (ch == '"')
token_type = WSTR; /* Wide-char string literal */
else
token_type = WCHR; /* Wide-char constant */
c = ch;
*out++ = 'L';
break; /* Fall down to "case QUO:" */
} else {
unget_ch();
} /* Fall through */
default: /* An identifier */
scan_id( c);
out = stpcpy( out, identifier);
token_type = NAM;
break;
}
if (token_type == NAM)
break;
/* Else fall through -- i.e. WSTR, WCHR */
case QUO: /* String or character constant */
out = scan_quote( c, out, out_end, FALSE);
if (token_type == 0) { /* Without prefix L */
if (c == '"')
token_type = STR;
else
token_type = CHR;
} /* Else WSTR or WCHR */
break;
case DOT:
ch = get_ch();
unget_ch();
if ((char_type[ ch] & DIG) == 0) /* Operator '.' or '...' */
goto operat;
/* Else fall through */
case DIG: /* Preprocessing number */
out = scan_number( c, out, out_end);
token_type = NUM;
break;
case PUNC:
operat: out = scan_op( c, out); /* Operator or punctuator */
token_type = OPE; /* Number is set in global "openum" */
break;
default: /* Special tokens or special characters */
if (((c == CAT || c == ST_QUOTE)) || (char_type[ c] & SPA))
token_type = SEP; /* Token separator or magic char*/
else
token_type = SPE;
/* Unkown token ($, @, multi-byte character or Latin */
*out++ = c;
*out = EOS;
break;
}
if (out_end < out)
cfatal( "Buffer overflow scanning token \"%s\"" /* _F_ */
, *out_pp, 0L, NULL);
if (mcpp_debug & TOKEN)
dump_token( token_type, *out_pp);
*out_pp = out;
in_token = FALSE; /* Token scanning has been done */
return token_type;
}
static void scan_id(
int c /* First char of id */
)
/*
* Reads the next identifier and put it into identifier[].
* The caller has already read the first character of the identifier.
*/
{
static char * const limit = &identifier[ IDMAX];
size_t len; /* Length of identifier */
char * bp = identifier;
if (c == IN_SRC) { /* Magic character */
*bp++ = c;
if ((mcpp_debug & MACRO_CALL) && ! in_directive) {
*bp++ = get_ch(); /* Its 2-bytes */
*bp++ = get_ch(); /* argument */
}
c = get_ch();
}
do {
if (bp < limit)
*bp++ = c;
c = get_ch();
} while ((char_type[ c] & (LET | DIG)) /* Letter or digit */
);
unget_ch();
*bp = EOS;
if (bp >= limit && (warn_level & 1)) /* Limit of token */
cwarn( "Too long identifier truncated to \"%s\"" /* _W1_ */
, identifier, 0L, NULL);
len = bp - identifier;
#if IDMAX > IDLEN90MIN
/* UCN16, UCN32, MBCHAR are counted as one character for each. */
if (infile->fp && len > std_limits.id_len && (warn_level & 4))
cwarn( "Identifier longer than %.0s%ld characters \"%s\"" /* _W4_ */
, NULL, (long) std_limits.id_len, identifier);
#endif /* IDMAX > IDLEN90MIN */
}
char * scan_quote(
int delim, /* ', " or < (header-name) */
char * out, /* Output buffer */
char * out_end, /* End of output buffer */
int diag /* Diagnostic should be output */
)
/*
* Scan off a string literal or character constant to the output buffer.
* Report diagnosis if the quotation is terminated by newline or character
* constant is empty (provided 'diag' is TRUE).
* Return the next output pointer or NULL (on error).
*/
{
const char * const skip_line = ", skipped the line"; /* _E_ */
const char * const unterm_string
= "Unterminated string literal%s";
const char * const unterm_char
= "Unterminated character constant %s%.0ld%s";
const char * const empty_const
= "Empty character constant %s%.0ld%s";
const char * skip;
size_t len;
int c;
char * out_p = out;
/* Set again in case of called from routines other than scan_token(). */
in_token = TRUE;
*out_p++ = delim;
if (delim == '<')
delim = '>';
while ((c = get_ch()) != EOS) {
#if MBCHAR
if (char_type[ c] & mbchk) {
/* First of multi-byte character (or shift-sequence) */
char * bptr = infile->bptr;
len = mb_read( c, &infile->bptr, (*out_p++ = c, &out_p));
if (len & MB_ERROR) {
if (infile->fp != NULL && compiling && diag) {
if (warn_level & 1) {
char * buf;
size_t chlen;
buf = xmalloc( chlen = infile->bptr - bptr + 2);
memcpy( buf, bptr, chlen - 1);
buf[ chlen - 1] = EOS;
cwarn(
"Illegal multi-byte character sequence \"%s\" in quotation", /* _W1_ */
buf, 0L, NULL);
free( buf);
}
}
continue;
} else { /* Valid multi-byte character (or sequence) */
goto chk_limit;
}
}
#endif
if (c == delim) {
break;
} else if (c == '\\' && delim != '>') { /* In string literal */
*out_p++ = c; /* Escape sequence */
c = get_ch();
#if MBCHAR
if (char_type[ c] & mbchk) {
/* '\\' followed by multi-byte char */
unget_ch();
continue;
}
#endif
} else if (c == '\n') {
break;
}
if (diag && iscntrl( c) && ((char_type[ c] & SPA) == 0)
&& (warn_level & 1))
cwarn(
"Illegal control character %.0s0lx%02x in quotation" /* _W1_ */
, NULL, (long) c, NULL);
*out_p++ = c;
chk_limit:
if (out_end < out_p) {
*out_end = EOS;
cfatal( "Too long quotation", NULL, 0L, NULL); /* _F_ */
}
}
if (c == '\n' || c == EOS)
unget_ch();
if (c == delim)
*out_p++ = delim;
*out_p = EOS;
if (diag) { /* At translation phase 3 */
skip = (infile->fp == NULL) ? NULL : skip_line;
if (c != delim) {
if (delim == '"') {
cerror( unterm_string, skip, 0L, NULL); /* _E_ */
} else if (delim == '\'') {
cerror( unterm_char, out, 0L, skip); /* _E_ */
} else {
cerror( "Unterminated header name %s%.0ld%s" /* _E_ */
, out, 0L, skip);
}
out_p = NULL;
} else if (delim == '\'' && out_p - out <= 2) {
cerror( empty_const, out, 0L, skip); /* _E_ */
out_p = NULL;
goto done;
}
#if NWORK-2 > SLEN90MIN
if (out_p - out > std_limits.str_len && (warn_level & 4))
cwarn( "Quotation longer than %.0s%ld bytes" /* _W4_ */
, NULL, std_limits.str_len, NULL);
#endif
}
done:
in_token = FALSE;
return out_p;
}
static char * scan_number(
int c, /* First char of number */
char * out, /* Output buffer */
char * out_end /* Limit of output buffer */
)
/*
* Read a preprocessing number.
* By scan_token() we know already that the first c is from 0 to 9 or dot,
* and if c is dot then the second character is digit.
* Returns the advanced output pointer.
* Note: preprocessing number permits non-numeric forms such as 3E+xy,
* which are used in stringization or token-concatenation.
*/
{
char * out_p = out; /* Current output pointer */
do {
*out_p++ = c;
if (c == 'E' || c == 'e' /* Sign should follow 'E', 'e', */
) {
c = get_ch();
if (c == '+' || c == '-') {
*out_p++ = c;
c = get_ch();
}
} else {
c = get_ch();
}
} while ((char_type[ c] & (DIG | DOT | LET)) /* Digit, dot or letter */
);
*out_p = EOS;
if (out_end < out_p)
cfatal( "Too long pp-number token \"%s\"" /* _F_ */
, out, 0L, NULL);
unget_ch();
return out_p;
}
static char * scan_op(
int c, /* First char of the token */
char * out /* Output buffer */
)
/*
* Scan C operator or punctuator into the specified buffer.
* Return the advanced output pointer.
* The code-number of the operator is stored to global variable 'openum'.
* Note: '#' is not an operator nor a punctuator in other than directive line,
* nevertheless is handled as a punctuator in this cpp for convenience.
*/
{
int c2, c3;
*out++ = c;
switch (c) {
case '~': openum = OP_COM; break;
case '(': openum = OP_LPA; break;
case ')': openum = OP_RPA; break;
case '?': openum = OP_QUE; break;
case ';': case '[': case ']': case '{':
case '}': case ',':
openum = OP_1;
break;
default:
openum = OP_2; /* Tentative guess */
}
if (openum != OP_2) { /* Single byte operators */
*out = EOS;
return out;
}
c2 = get_ch(); /* Possibly two bytes ops */
*out++ = c2;
switch (c) {
case '=':
openum = ((c2 == '=') ? OP_EQ : OP_1); /* ==, = */
break;
case '!':
openum = ((c2 == '=') ? OP_NE : OP_NOT); /* !=, ! */
break;
case '&':
switch (c2) {
case '&': openum = OP_ANA; break; /* && */
case '=': /* openum = OP_2; */ break; /* &= */
default : openum = OP_AND; break; /* & */
}
break;
case '|':
switch (c2) {
case '|': openum = OP_ORO; break; /* || */
case '=': /* openum = OP_2; */ break; /* |= */
default : openum = OP_OR; break; /* | */
}
break;
case '<':
switch (c2) {
case '<': c3 = get_ch();
if (c3 == '=') {
openum = OP_3; /* <<= */
*out++ = c3;
} else {
openum = OP_SL; /* << */
unget_ch();
}
break;
case '=': openum = OP_LE; break; /* <= */
case ':': /* <: i.e. [ */
openum = OP_LT;
break;
case '%': /* <% i.e. { */
openum = OP_LT;
break;
default : openum = OP_LT; break; /* < */
}
break;
case '>':
switch (c2) {
case '>': c3 = get_ch();
if (c3 == '=') {
openum = OP_3; /* >>= */
*out++ = c3;
} else {
openum = OP_SR; /* >> */
unget_ch();
}
break;
case '=': openum = OP_GE; break; /* >= */
default : openum = OP_GT; break; /* > */
}
break;
case '#':
if ((in_define || macro_line)) /* in #define or macro */
openum = ((c2 == '#') ? OP_CAT : OP_STR); /* ##, # */
else
openum = OP_1; /* # */
break;
case '+':
switch (c2) {
case '+': /* ++ */
case '=': /* openum = OP_2; */ break; /* += */
default : openum = OP_ADD; break; /* + */
}
break;
case '-':
switch (c2) {
case '-': /* -- */
case '=': /* -= */
/* openum = OP_2; */
break;
case '>':
/* else openum = OP_2; */ /* -> */
/* else openum = OP_2; */
break;
default : openum = OP_SUB; break; /* - */
}
break;
case '%':
switch (c2) {
case '=': break; /* %= */
case '>': /* %> i.e. } */
openum = OP_MOD;
break;
case ':':
openum = OP_MOD;
break;
default : openum = OP_MOD; break; /* % */
}
break;
case '*':
if (c2 != '=') /* * */
openum = OP_MUL;
/* else openum = OP_2; */ /* *= */
break;
case '/':
if (c2 != '=') /* / */
openum = OP_DIV;
/* else openum = OP_2; */ /* /= */
break;
case '^':
if (c2 != '=') /* ^ */
openum = OP_XOR;
/* else openum = OP_2; */ /* ^= */
break;
case '.':
if (c2 == '.') {
c3 = get_ch();
if (c3 == '.') {
openum = OP_ELL; /* ... */
*out++ = c3;
break;
} else {
unget_ch();
openum = OP_1;
}
} else { /* . */
openum = OP_1;
}
break;
case ':':
openum = OP_COL;
break;
default: /* Never reach here */
cfatal( "Bug: Punctuator is mis-implemented %.0s0lx%x" /* _F_ */
, NULL, (long) c, NULL);
openum = OP_1;
break;
}
switch (openum) {
case OP_STR:
if (c == '%') break; /* %: */
case OP_1:
case OP_NOT: case OP_AND: case OP_OR: case OP_LT:
case OP_GT: case OP_ADD: case OP_SUB: case OP_MOD:
case OP_MUL: case OP_DIV: case OP_XOR: case OP_COM:
case OP_COL: /* Any single byte operator or punctuator */
unget_ch();
out--;
break;
default: /* Two or more bytes operators or punctuators */
break;
}
*out = EOS;
return out;
}
void expanding(
const char * name, /* The name of (nested) macro just expanded. */
int to_be_freed /* The name should be freed later. */
)
/*
* Remember used macro name for diagnostic.
*/
{
if (exp_mac_ind < EXP_MAC_IND_MAX - 1) {
exp_mac_ind++;
} else {
clear_exp_mac();
exp_mac_ind++;
}
expanding_macro[ exp_mac_ind].name = name;
expanding_macro[ exp_mac_ind].to_be_freed = to_be_freed;
}
void clear_exp_mac( void)
/*
* Initialize expanding_macro[] freeing names registered in
* name_to_be_freed[].
*/
{
int i;
for (i = 1; i < EXP_MAC_IND_MAX; i++) {
if (expanding_macro[ i].to_be_freed) {
free( (void *) expanding_macro[ i].name);
expanding_macro[ i].to_be_freed = FALSE;
}
}
exp_mac_ind = 0;
}
int get_ch( void)
/*
* Return the next character from a macro or the current file.
* Always return the value representable by unsigned char.
*/
{
int len;
int c;
FILEINFO * file;
/*
* 'in_token' is set to TRUE while scan_token() is executed (and
* scan_id(), scan_quote(), scan_number(), scan_ucn() and scan_op()
* via scan_token()) in Standard mode to simplify tokenization.
* Any token cannot cross "file"s.
*/
if (in_token)
return (*infile->bptr++ & UCHARMAX);
if ((file = infile) == NULL)
return CHAR_EOF; /* End of all input */
if (mcpp_debug & GETC) {
mcpp_fprintf( DBG, "get_ch(%s) '%c' line %ld, bptr = %d, buffer"
, file->fp ? cur_fullname : file->real_fname ? file->real_fname
: file->filename ? file->filename : "NULL"
, *file->bptr & UCHARMAX
, src_line, (int) (file->bptr - file->buffer));
dump_string( NULL, file->buffer);
dump_unget( "get entrance");
}
/*
* Read a character from the current input logical line or macro.
* At EOS, either finish the current macro (freeing temporary storage)
* or get another logical line by parse_line().
* At EOF, exit the current file (#included) or, at EOF from the MCPP input