This repository has been archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlex.l
1059 lines (943 loc) · 24.9 KB
/
lex.l
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
%p 3000
%{
/* $Id: lex.l,v 4.23 2014/01/01 16:01:46 tom Exp $
*
* Lexical analyzer for C function prototype generator
*
* This is designed to parse lexically at the top level (e.g., of extern
* objects such as procedures). The corresponding yacc-grammar expects
* that curly-braces (for function bodies) are recognized as a single
* token, BRACES. Similarly, square-brackets and their contents are
* passed back as BRACKETS.
*
* Assignments at the top level are data-initialization statements.
* These are returned as INITIALIZER.
*
* The logic here recognizes tokens inside curly-braces, but does not
* pass them back to the grammar.
*
* Apollo extensions:
* "&" is ignored when creating lint-libraries, because we ignore
* expressions of all kinds. Note that function-prototypes may use "&" to
* denote reference-parameters. By ignoring that as well, we make the
* output compatible with lint (kludge).
*
* Similarly, ignore "std_$call", since it is not compatible with lint.
*
* CPP_INLINE handles a special case of the Apollo CC 6.7 compiler that
* uses inline attribute declarations, e.g.
*
* int foo #attribute[aligned(1)];
*
* In CC 6.8 this behavior is hidden by a macro.
*
* VAX/VMS extensions:
* Treat the keywords 'globalref', etc., as 'extern'.
*
* The keywords 'noshare' and 'readonly' are type-qualifiers.
*
* GCC extensions:
* The keywords '__attribute__', '__inline', '__inline__', '__signed',
* '__signed__'. and '__extension__'
*/
#define result(nn) count(); if (!brackets && unnested()) return(nn)
#define is_IDENTIFIER save_text_offset();\
return type_of_name(yytext);
#if !OPT_LINTLIBRARY
#define gcc_attribute absorb_special /* otherwise, we don't care */
#endif
#ifdef apollo
#define apollo_keyword
#define apollo_special absorb_special()
#else
#define apollo_keyword is_IDENTIFIER
#define apollo_special is_IDENTIFIER
#endif
#ifdef vms
#define vms_extern save_text_offset(); return(T_EXTERN);
#define vms_keyword save_text_offset();
#else /* unix */
#define vms_extern is_IDENTIFIER
#define vms_keyword is_IDENTIFIER
#endif /* vms/unix */
char *varargs_str; /* save printflike/scanflike text */
int varargs_num; /* code to save "VARARGS" */
int debug_trace; /* true if we trace token-level stuff */
char base_file[BUFSIZ]; /* top-level file name */
static int asm_level; /* parenthesis level for "asm" parsing */
static int save_cpp; /* true if cpp-text within curly braces */
static int in_cpp; /* true while we are within cpp-text */
static int curly; /* number of curly brace nesting levels */
static int ly_count; /* number of occurances of %% */
#ifdef FLEX_SCANNER
/* flex scanner state */
static YY_BUFFER_STATE *buffer_stack;
#ifdef __cplusplus
#define LexInput() yyinput()
#endif
#endif /* FLEX_SCANNER */
#ifndef LexInput
#define LexInput() input()
#endif
static unsigned inc_limit; /* stack size */
static int inc_depth; /* include nesting level */
static IncludeStack *inc_stack; /* stack of included files */
static SymbolTable *included_files; /* files already included */
static int type_of_name(char *name);
static void startCpp(int level);
static void finishCpp(void);
#if defined(apollo) || !OPT_LINTLIBRARY
static void absorb_special(void);
#endif
#if OPT_LINTLIBRARY
static void gcc_attribute(void);
#endif
static void update_line_num(void);
static void save_text(void);
static void save_text_offset(void);
static void get_quoted(void);
static void get_comment(void);
static void get_cpp_directive(int copy);
static void parsing_file_name(unsigned need);
static void do_include(char *f);
static void include_file(char *name, int convert);
static void put_file(FILE *outf);
static void put_quoted(int c);
#if OPT_LINTLIBRARY
static int decipher_comment(char *keyword, int len);
#endif
%}
WS [ \t]
LETTER [A-Za-z$_]
DIGIT [0-9]
ID {LETTER}({LETTER}|{DIGIT})*
QUOTE [\"\']
%s CPP1 INIT1 INIT2 CURLY LEXYACC ASM CPP_INLINE
%%
\n { save_text(); cur_file->line_num++;
cur_declarator = NULL; }
"/*" { save_text(); get_comment(); }
"//".*$ save_text();
<INITIAL>"&" { save_text(); return '&'; /* C++ ref-variable? */ }
<LEXYACC>^"%%" { save_text(); if (++ly_count >= 2) BEGIN INITIAL; }
<LEXYACC>^"%{" { save_text(); BEGIN INITIAL; }
<LEXYACC>{QUOTE} get_quoted();
<LEXYACC>. save_text();
<INITIAL>^"%}" { save_text(); BEGIN LEXYACC; }
<INITIAL>#{WS}* { save_text(); startCpp(0); }
<INITIAL>"??="{WS}* { save_text(); startCpp(0); }
<CPP1>attribute { BEGIN CPP_INLINE; /* apollo */}
<CPP1>options { BEGIN CPP_INLINE; /* apollo */}
<CPP_INLINE>[^;]* finishCpp();
<CPP1>define{WS}+{ID} {
char *name;
char *value;
save_text();
*(name = (char *) xmalloc((size_t) (yyleng + 1))) = '\0';
sscanf(yytext, "define %s", name);
get_cpp_directive(1);
*(value = (char *) xmalloc(1 + strlen(temp_buf))) = '\0';
sscanf(temp_buf, "%s", value);
new_symbol(define_names, name, value, DS_NONE);
free(name);
free(value);
}
<CPP1>include{WS}* {
save_text();
get_cpp_directive(1);
if (temp_buf[0] != '"' && temp_buf[0] != '<') {
Symbol *sym = find_symbol(define_names, temp_buf);
if (sym != NULL && sym->value != NULL) {
need_temp(strlen(sym->value));
strcpy(temp_buf, sym->value);
} else {
temp_buf[0] = '\0';
}
}
if (temp_buf[0] != '\0')
do_include(temp_buf);
}
<CPP1>line{WS}+[0-9]+{WS}+\".*$ {
save_text();
parsing_file_name((unsigned) yyleng);
sscanf(yytext, "line %u \"%[^\"]\"",
&cur_file->line_num,
cur_file->file_name);
cur_file->line_num--;
track_in();
finishCpp();
}
<CPP1>[0-9]+{WS}+\".*$ {
save_text();
parsing_file_name((unsigned) yyleng);
sscanf(yytext, "%u \"%[^\"]\"",
&cur_file->line_num,
cur_file->file_name);
cur_file->line_num--;
track_in();
finishCpp();
}
<CPP1>[0-9]+.*$ {
save_text();
sscanf(yytext, "%u ", &cur_file->line_num);
cur_file->line_num--;
track_in();
finishCpp();
}
<CPP1>. { save_text(); get_cpp_directive(0); }
<INITIAL>"(" { save_text_offset(); return '('; }
<INITIAL>")" {
save_text();
if (cur_file->convert)
cur_file->begin_comment =
ftell(cur_file->tmp_file);
return ')';
}
<INITIAL>"*" { save_text_offset(); return '*'; }
<INITIAL>[,;] {
save_text();
if (cur_file->convert)
cur_file->begin_comment =
ftell(cur_file->tmp_file);
return yytext[0];
}
<INITIAL>"..." { save_text(); return T_ELLIPSIS; }
<INITIAL>\" {
get_quoted();
return T_STRING_LITERAL;
}
<INITIAL>"__asm__" { save_text(); BEGIN ASM; return T_ASM; }
<INITIAL>asm { save_text(); BEGIN ASM; return T_ASM; }
<ASM>"(" { ++asm_level; save_text(); }
<ASM>")" { --asm_level; save_text(); if (asm_level <= 0) { asm_level = 0; BEGIN INITIAL; return T_ASMARG; } }
<ASM>{QUOTE} get_quoted();
<ASM>. save_text();
<INITIAL>__?based[^(]*\([^)]*\) { save_text_offset(); return T_TYPE_QUALIFIER; }
<INITIAL>auto { save_text_offset(); return T_AUTO; }
<INITIAL>extern { save_text_offset(); return T_EXTERN; }
<INITIAL>register { save_text_offset(); return T_REGISTER; }
<INITIAL>static { save_text_offset(); return T_STATIC; }
<INITIAL>typedef { save_text_offset(); return T_TYPEDEF; }
<INITIAL>inline { save_text_offset(); return T_INLINE; }
<INITIAL>_Bool { save_text_offset(); return T_Bool; }
<INITIAL>_Complex { save_text_offset(); return T_Complex; }
<INITIAL>_Imaginary { save_text_offset(); return T_Imaginary; }
<INITIAL>char { save_text_offset(); return T_CHAR; }
<INITIAL>double { save_text_offset(); return T_DOUBLE; }
<INITIAL>float { save_text_offset(); return T_FLOAT; }
<INITIAL>int { save_text_offset(); return T_INT; }
<INITIAL>void { save_text_offset(); return T_VOID; }
<INITIAL>long { save_text_offset(); return T_LONG; }
<INITIAL>short { save_text_offset(); return T_SHORT; }
<INITIAL>signed { save_text_offset(); return T_SIGNED; }
<INITIAL>unsigned { save_text_offset(); return T_UNSIGNED; }
<INITIAL>enum { save_text_offset(); return T_ENUM; }
<INITIAL>struct { save_text_offset(); return T_STRUCT; }
<INITIAL>union { save_text_offset(); return T_UNION; }
<INITIAL>va_dcl { save_text_offset(); return T_VA_DCL; }
<INITIAL>__signed { save_text_offset(); return T_SIGNED; }
<INITIAL>__signed__ { save_text_offset(); return T_SIGNED; }
<INITIAL>__inline { save_text_offset(); return T_INLINE; }
<INITIAL>__inline__ { save_text_offset(); return T_INLINE; }
<INITIAL>__extension__ { save_text_offset(); return T_EXTENSION; }
<INITIAL>__attribute__ { gcc_attribute(); }
<INITIAL>globalvalue { vms_extern; }
<INITIAL>globalref { vms_extern; }
<INITIAL>globaldef { vms_extern; }
<INITIAL>"std_$call" { apollo_keyword; }
<INITIAL>"__attribute" { apollo_special; }
<INITIAL>{ID} { is_IDENTIFIER }
<INITIAL>\[[^\]]*\] {
/* This can't handle the case where a comment
* containing a ] appears between the brackets.
*/
save_text_offset();
update_line_num();
return T_BRACKETS;
}
<INITIAL>"??("[^?]*"??)" {
save_text_offset();
update_line_num();
return T_BRACKETS;
}
<INITIAL>"=" { save_text(); BEGIN INIT1; return '='; }
<INIT1>"{" { save_text(); curly = 1; BEGIN INIT2; }
<INIT1>[,;] {
unput(yytext[yyleng-1]);
BEGIN INITIAL;
return T_INITIALIZER;
}
<INIT1>{QUOTE} get_quoted();
<INIT1>. save_text();
<INIT2>"{" { save_text(); ++curly; }
<INIT2>"}" {
save_text();
if (--curly == 0) {
BEGIN INITIAL;
return T_INITIALIZER;
}
}
<INIT2>{QUOTE} get_quoted();
<INIT2>. save_text();
<INITIAL>"{" {
save_text();
curly = 1;
return_val =
returned_at = FALSE;
BEGIN CURLY;
return T_LBRACE;
}
<CURLY>"{" { save_text(); ++curly; }
<CURLY>"}" {
save_text();
if (--curly == 0) {
BEGIN INITIAL;
return T_MATCHRBRACE;
}
}
<CURLY>{QUOTE} get_quoted();
<CURLY>"return" { save_text(); returned_at = TRUE; }
<CURLY>";" { save_text(); returned_at = FALSE; }
<CURLY>#{WS}* { save_text(); startCpp(1); }
<CURLY>"??="{WS}* { save_text(); startCpp(1); }
<CURLY>. { save_text(); return_val |= returned_at; }
[ \r\t\f]+ save_text();
. {
save_text();
put_error();
fprintf(stderr, "bad character '%c'\n", yytext[0]);
}
%%
static void
startCpp(int level)
{
save_cpp = level;
in_cpp = TRUE;
BEGIN CPP1;
}
static void
finishCpp(void)
{
in_cpp = FALSE;
if (save_cpp)
BEGIN CURLY;
else
BEGIN INITIAL;
}
/*
* Skip over embedded __attribute/__attribute_ syntax.
*/
#if defined(apollo) || !OPT_LINTLIBRARY
static void
absorb_special(void)
{
int c;
int nest = 0;
while ((c = input()) > 0) {
if (c == '(')
nest++;
else if (c == ')') {
if (--nest <= 0)
break;
}
}
}
#endif
#if OPT_LINTLIBRARY
/*
* This recognizes some of the special attribute macros defined by gcc:
* noreturn
* format(printf,n,m)
* format(scanf,n,m)
* and uses that information to construct equivalent lint-library text.
* (It's a distinct piece of code from the 'absorb_special()' function to
* avoid spurious matches with non-gcc compilers).
*/
static void
gcc_attribute(void)
{
int c, num1, num2;
int nest = 0;
unsigned len = 0;
char bfr[BUFSIZ];
while ((c = LexInput()) > 0) {
if (len < sizeof(bfr) - 1 && !isspace(c))
bfr[len++] = (char) c;
if (c == '(')
nest++;
else if (c == ')') {
if (--nest <= 0)
break;
}
}
bfr[len] = '\0';
if (!strcmp(bfr, "((noreturn))")) {
exitlike_func = TRUE;
} else if (sscanf(bfr, "((format(printf,%d,%d)))", &num1, &num2) == 2) {
(void) sprintf(bfr, "PRINTFLIKE%d", varargs_num = num1);
varargs_str = xstrdup(bfr);
} else if (sscanf(bfr, "((format(scanf,%d,%d)))", &num1, &num2) == 2) {
(void) sprintf(bfr, "SCANFLIKE%d", varargs_num = num1);
varargs_str = xstrdup(bfr);
}
}
#endif
/* Decode the current token according to the type-of-name
*/
static int
type_of_name(char *name)
{
if (find_symbol(type_qualifiers, name) != NULL)
return T_TYPE_QUALIFIER;
else if (find_symbol(typedef_names, name) != NULL)
return T_TYPEDEF_NAME;
else if (find_symbol(define_names, name) != NULL)
return T_DEFINE_NAME;
else
return T_IDENTIFIER;
}
boolean
is_typedef_name(char *name)
{
return (boolean) (find_symbol(typedef_names, name) != NULL);
}
/* If the matched text contains any new line characters, then update the
* current line number.
*/
static void
update_line_num(void)
{
char *p = yytext;
while (*p != '\0') {
if (*p++ == '\n')
cur_file->line_num++;
}
}
/* Save the matched text in the temporary file.
*/
static void
save_text(void)
{
#if OPT_LINTLIBRARY
if (!in_cpp)
copy_typedef(yytext);
#endif
if (cur_file->convert) {
fputs(yytext, cur_file->tmp_file);
}
}
/* Record the current position in the temporary file and write the matched text
* to the file.
*/
static void
save_text_offset(void)
{
(void) strcpy(yylval.text.text, yytext);
#if OPT_LINTLIBRARY
copy_typedef(yytext);
#endif
if (cur_file->convert) {
yylval.text.begin = ftell(cur_file->tmp_file);
fputs(yytext, cur_file->tmp_file);
} else
yylval.text.begin = 0;
}
#if OPT_LINTLIBRARY
/* Decipher comments that are useful for lint (and making lint-libraries)
*/
static struct {
int varText;
int varargs;
int externs;
int preproz;
} cmtVal;
static int
decipher_comment(char *keyword, int len)
{
if (len != 0) {
int value;
keyword[len] = '\0';
/* these are recognized by some lint-programs */
if (!strcmp(keyword, "VARARGS")) {
cmtVal.varargs = -1;
} else if (sscanf(keyword, "VARARGS%d", &value) == 1) {
cmtVal.varargs = value;
} else if (!strcmp(keyword, "PRINTFLIKE")) {
cmtVal.varargs = 1;
cmtVal.varText = TRUE;
} else if (sscanf(keyword, "PRINTFLIKE%d", &value) == 1) {
cmtVal.varargs = value;
cmtVal.varText = TRUE;
} else if (!strcmp(keyword, "SCANFLIKE")) {
cmtVal.varargs = 2;
cmtVal.varText = TRUE;
} else if (sscanf(keyword, "SCANFLIKE%d", &value) == 1) {
cmtVal.varargs = value;
cmtVal.varText = TRUE;
/* these are extensions added to simplify library-generation */
} else if (!strcmp(keyword, "LINT_EXTERN")) {
cmtVal.externs = MAX_INC_DEPTH;
} else if (sscanf(keyword, "LINT_EXTERN%d", &value) == 1) {
cmtVal.externs = value;
} else if (!strcmp(keyword, "LINT_PREPRO")) {
cmtVal.preproz = -1; /* the whole comment */
} else if (sscanf(keyword, "LINT_PREPRO%d", &value) == 1) {
cmtVal.preproz = value;
} else if (!strcmp(keyword, "LINT_SHADOWED")) {
lint_shadowed = TRUE;
}
}
return 0;
}
#endif
static void
put_quoted(int c)
{
/* Modifying 'yytext[]' doesn't work well with FLEX, which simply
* maintains 'yytext' as a pointer into its input buffer. LEX copies
* characters into the 'yytext[]' array.
*/
#if defined(FLEX_SCANNER) || !defined(YYLMAX)
if (c != 0) {
static char temp[2];
temp[0] = (char) c;
/* save_text */
# if OPT_LINTLIBRARY
if (!in_cpp)
copy_typedef(temp);
# endif
if (cur_file->convert) {
fputs(temp, cur_file->tmp_file);
}
/* update_line_num */
if (c == '\n')
cur_file->line_num++;
}
#else /* this works fine on LEX (e.g., on SunOS 4.x) */
if ((c == 0) || (yyleng + 1 >= YYLMAX)) {
save_text();
update_line_num();
yyleng = 0;
}
if (c != 0) {
yytext[yyleng++] = c;
yytext[yyleng] = 0;
}
#endif /* LEX/FLEX */
}
/*
* Scan past the characters in a backslash sequence
*/
/* Scan past quoted string. Note that some strings may overflow 'yytext[]', so
* we don't try to eat them in the lexical rules.
*/
static void
get_quoted(void)
{
int delim = *yytext;
int c;
#if defined(FLEX_SCANNER) || !defined(YYLMAX)
put_quoted(delim);
#endif
while ((c = LexInput()) != 0) {
if (c == '\\') {
put_quoted(c);
if ((c = LexInput()) == 0)
break;
put_quoted(c);
} else {
put_quoted(c);
if (c == delim)
break;
if (c == '\n') { /* recover from unbalanced */
put_error();
fprintf(stderr, "unbalanced quote character '%c'\n", delim);
break;
}
}
}
put_quoted(0);
}
/* Scan to end of comment.
*/
static void
get_comment(void)
{
int c, lastc = '\0';
#if OPT_LINTLIBRARY
unsigned len = 0;
char keyword[BUFSIZ];
keyword[len] = '\0';
cmtVal.varText = 0;
cmtVal.varargs = 0;
cmtVal.externs = -1;
cmtVal.preproz = 0;
#endif
while ((c = LexInput()) != 0) {
if (cur_file->convert)
fputc(c, cur_file->tmp_file);
#if OPT_LINTLIBRARY
if (!(isalnum(c) || c == '_' || c == '$')) {
int flag = cmtVal.preproz;
len = (unsigned) decipher_comment(keyword, (int) len);
if (flag != cmtVal.preproz)
lastc = '\0';
} else if (len + 1 < sizeof(keyword)) {
keyword[len++] = (char) c;
}
#endif
switch (c) {
case '\n':
cur_file->line_num++;
#if OPT_LINTLIBRARY
if (cmtVal.preproz != 0 && lastc != '\0')
fputc(lastc, stdout);
if (cmtVal.preproz > 0) /* if negative, we pass everything */
cmtVal.preproz -= 1;
#endif
break;
case '/':
if (lastc == '*') {
if (cur_file->convert) {
if (func_params && cur_declarator) {
cur_declarator->begin_comment = cur_file->begin_comment;
cur_file->begin_comment = ftell(cur_file->tmp_file);
cur_declarator->end_comment = cur_file->begin_comment;
cur_declarator = NULL;
} else {
cur_file->end_comment = ftell(cur_file->tmp_file);
}
}
#if OPT_LINTLIBRARY
(void) decipher_comment(keyword, (int) len);
if (cmtVal.varargs != 0) {
if ((varargs_num = cmtVal.varargs) != 0
&& cmtVal.varText != 0) {
if (varargs_str != 0)
free(varargs_str);
varargs_str = xstrdup(keyword);
}
}
if (cmtVal.externs >= 0)
extern_in = (unsigned) cmtVal.externs;
if (cmtVal.preproz != 0)
fputc('\n', stdout);
#endif
return;
}
/* FALLTHRU */
default:
#if OPT_LINTLIBRARY
if (cmtVal.preproz != 0 && lastc != '\0')
fputc(lastc, stdout);
#endif
break;
}
lastc = c;
}
}
/* Scan rest of preprocessor directive. If copy is true, then store the text
* in temp_buf.
*/
static void
get_cpp_directive(int copy)
{
unsigned used = 0;
char c, lastc[4];
lastc[0] = lastc[1] = lastc[2] = lastc[3] = '\0';
if (copy) {
need_temp((size_t) 2);
*temp_buf = '\0';
}
while ((c = (char) LexInput()) != 0) {
if (cur_file->convert)
fputc(c, cur_file->tmp_file);
switch (c) {
case '\n':
cur_file->line_num++;
if (lastc[2] != '\\' && strcmp(lastc, "?\?/") != 0) {
finishCpp();
return;
}
break;
case '*':
if (lastc[2] == '/')
get_comment();
break;
}
lastc[0] = lastc[1];
lastc[1] = lastc[2];
lastc[2] = c;
if (copy) {
if (used + 2 >= temp_len)
need_temp(temp_len + MAX_TEXT_SIZE);
temp_buf[used++] = c;
temp_buf[used] = '\0';
}
}
}
/*
* Ensure that the filename buffer is large enough to hold yytext, e.g., if
* the sscanf gave the whole buffer.
*/
static void
parsing_file_name(unsigned need)
{
need += 2;
if (cur_file->len_file_name < need) {
cur_file->len_file_name += need;
cur_file->file_name = (char *) xrealloc(cur_file->file_name, cur_file->len_file_name);
}
cur_file->file_name[0] = 0;
}
/* Return a pointer to the current file name.
*/
char *
cur_file_name(void)
{
return cur_file->file_name;
}
/* Return the current line number.
*/
unsigned
cur_line_num(void)
{
return cur_file->line_num;
}
/* Return the current temporary output file.
*/
FILE *
cur_tmp_file(void)
{
return cur_file->tmp_file;
}
/* Set the modify flag for the current file.
*/
void
cur_file_changed(void)
{
cur_file->changed = TRUE;
}
/* Return the temporary file offset of beginning of the current comment.
*/
long
cur_begin_comment(void)
{
return cur_file->begin_comment;
}
/* Return the text of the current lexical token.
*/
char *
cur_text(void)
{
return yytext;
}
#if !HAVE_TMPFILE
/*
* tmpfile() - return a FILE* for a temporary file that will be
* removed automatically when the program exits.
*
* Not all systems have the ANSI tmpfile() function yet...
*
* David W. Sanderson ([email protected])
*
* note - this was in version 3.10 from 1993 - TD
*/
FILE *
tmpfile(void)
{
char *name;
char *tmpdir;
FILE *f;
if ((tmpdir = getenv("TMPDIR")) == (char *) 0) {
tmpdir = "/tmp";
}
name = xmalloc(strlen(tmpdir) + 20);
sprintf(name, "%s/TfXXXXXX", tmpdir);
call_mktemp(name);
if ((f = fopen(name, "w+")) != 0) {
if (unlink(name) == -1) {
fclose(f);
f = 0;
}
}
free(name);
return f;
}
#endif /* !HAVE_TMPFILE */
/* Push a file onto the include stack. The stream yyin must already
* point to the file.
*/
static void
include_file(char *name, /* file name */
int convert) /* if TRUE, convert function definitions */
{
if (++inc_depth >= (int) inc_limit) {
unsigned need = (inc_limit | 31) + 1;
#ifdef FLEX_SCANNER
buffer_stack = type_realloc(YY_BUFFER_STATE, buffer_stack, need);
#endif
inc_stack = type_realloc(IncludeStack, inc_stack, need);
while (inc_limit < need) {
#ifdef FLEX_SCANNER
buffer_stack[inc_limit] = 0;
#endif
memset(inc_stack + inc_limit, 0, sizeof(*inc_stack));
++inc_limit;
}
}
cur_file = inc_stack + inc_depth;
cur_file->file = yyin;
cur_file->base_name = xstrdup(name);
cur_file->len_file_name = strlen(name) + MAX_TEXT_SIZE;
cur_file->file_name = strcpy((char *) xmalloc(cur_file->len_file_name), name);
cur_file->line_num = 1;
cur_file->convert = (boolean) convert;
cur_file->changed = FALSE;
#ifdef FLEX_SCANNER
buffer_stack[inc_depth] = yy_create_buffer(yyin, YY_BUF_SIZE);
yy_switch_to_buffer(buffer_stack[inc_depth]);
#endif
if (convert) {
cur_file->begin_comment = cur_file->end_comment = 0;
cur_file->tmp_file = tmpfile();
if (cur_file->tmp_file == NULL) {
fprintf(stderr, "%s: cannot create temporary file\n", progname);
cur_file->convert = FALSE;
}
}
}
#define BLOCK_SIZE 2048
/* Copy converted C source from the temporary file to the output stream.
*/
static void
put_file(FILE *outf)
{
char block[BLOCK_SIZE];
long filesize;
size_t nread, count;
filesize = ftell(cur_file->tmp_file);
fseek(cur_file->tmp_file, 0L, 0);
while (filesize > 0) {
count = (filesize < BLOCK_SIZE) ? (size_t) filesize : BLOCK_SIZE;
nread = fread(block, sizeof(char), count, cur_file->tmp_file);
if (nread == 0)
break;
fwrite(block, sizeof(char), nread, outf);
filesize -= (long) nread;
}
}
/* Remove the top of the include stack.
*/
void
pop_file(int closed)
{
FILE *outf;
if (!closed && (yyin != stdin))
fclose(yyin);
if (cur_file->convert) {
if (yyin == stdin) {
put_file(stdout);
} else if (cur_file->changed) {
if ((outf = fopen(cur_file->base_name, "w")) != NULL) {
put_file(outf);
fclose(outf);
} else {
fprintf(stderr, "%s: cannot create file %s\n", progname,
cur_file->base_name);
}
}
fclose(cur_file->tmp_file);
}
free(cur_file->base_name);
free(cur_file->file_name);
#ifdef FLEX_SCANNER
yy_delete_buffer(YY_CURRENT_BUFFER);
#endif
if (--inc_depth >= 0) {
cur_file = inc_stack + inc_depth;
yyin = cur_file->file;
#ifdef FLEX_SCANNER
yy_switch_to_buffer(buffer_stack[inc_depth]);
#endif
}
}
/* Process include directive.
*/
static void
do_include(char *file_spec) /* path surrounded by "" or <> */
{
unsigned stdinc; /* 1 = path surrounded by <> */
char *file;
char *path;
char match, *s;
unsigned i;
unsigned n;
FILE *fp;
if (file_spec[0] == '"') {
match = '"';
stdinc = 0;
} else if (file_spec[0] == '<') {
match = '>';
stdinc = 1;
} else {
return;
}
s = (strchr) (file_spec + 1, match);
n = (s != NULL) ? (unsigned) (s - file_spec - 1) : 0;
file = xstrdup(file_spec + 1);
file[n] = '\0';