This repository has been archived by the owner on Mar 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcpp.c
2565 lines (2394 loc) · 63.3 KB
/
cpp.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
/*
* C and T preprocessor, and integrated lexer
* (c) Thomas Pornin 1999 - 2002
*
* 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.
* 4. The name of the authors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT 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 AUTHORS OR CONTRIBUTORS 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.
*
*/
#define VERS_MAJ 1
#define VERS_MIN 3
/* uncomment the following if you cannot set it with a compiler flag */
/* #define STAND_ALONE */
#include "tune.h"
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <setjmp.h>
#include <stddef.h>
#include <limits.h>
#include <time.h>
#include "ucppi.h"
#include "mem.h"
#include "nhash.h"
#ifdef UCPP_MMAP
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#endif
/*
* The standard path where includes are looked for.
*/
#ifdef STAND_ALONE
static char *include_path_std[] = { STD_INCLUDE_PATH, 0 };
#endif
static char **include_path;
static size_t include_path_nb = 0;
int no_special_macros = 0;
int emit_dependencies = 0, emit_defines = 0, emit_assertions = 0;
FILE *emit_output;
#ifdef STAND_ALONE
static char *system_macros_def[] = { STD_MACROS, 0 };
static char *system_assertions_def[] = { STD_ASSERT, 0 };
#endif
char *current_filename = 0, *current_long_filename = 0;
static int current_incdir = -1;
#ifndef NO_UCPP_ERROR_FUNCTIONS
/*
* "ouch" is the name for an internal ucpp error. If AUDIT is not defined,
* no code calling this function will be generated; a "ouch" may still be
* emitted by getmem() (in mem.c) if MEM_CHECK is defined, but this "ouch"
* does not use this function.
*/
void ucpp_ouch(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "%s: ouch, ", current_filename);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
die();
}
/*
* report an error, with current_filename, line, and printf-like syntax
*/
void ucpp_error(long line, char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (line > 0)
fprintf(stderr, "%s: line %ld: ", current_filename, line);
else if (line == 0) fprintf(stderr, "%s: ", current_filename);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
if (line >= 0) {
struct stack_context *sc = report_context();
size_t i;
for (i = 0; sc[i].line >= 0; i ++)
fprintf(stderr, "\tincluded from %s:%ld\n",
sc[i].long_name ? sc[i].long_name : sc[i].name,
sc[i].line);
freemem(sc);
}
va_end(ap);
}
/*
* like error(), with the mention "warning"
*/
void ucpp_warning(long line, char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (line > 0)
fprintf(stderr, "%s: warning: line %ld: ",
current_filename, line);
else if (line == 0)
fprintf(stderr, "%s: warning: ", current_filename);
else fprintf(stderr, "warning: ");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
if (line >= 0) {
struct stack_context *sc = report_context();
size_t i;
for (i = 0; sc[i].line >= 0; i ++)
fprintf(stderr, "\tincluded from %s:%ld\n",
sc[i].long_name ? sc[i].long_name : sc[i].name,
sc[i].line);
freemem(sc);
}
va_end(ap);
}
#endif /* NO_UCPP_ERROR_FUNCTIONS */
/*
* Some memory allocations are manually garbage-collected; essentially,
* strings duplicated in the process of macro replacement. Each such
* string is referenced in the garbage_fifo, which is cleared when all
* nested macros have been resolved.
*/
struct garbage_fifo {
char **garbage;
size_t ngarb, memgarb;
};
/*
* throw_away() marks a string to be collected later
*/
void throw_away(struct garbage_fifo *gf, char *n)
{
wan(gf->garbage, gf->ngarb, n, gf->memgarb);
}
/*
* free marked strings
*/
void garbage_collect(struct garbage_fifo *gf)
{
size_t i;
for (i = 0; i < gf->ngarb; i ++) freemem(gf->garbage[i]);
gf->ngarb = 0;
}
static void init_garbage_fifo(struct garbage_fifo *gf)
{
gf->garbage = getmem((gf->memgarb = GARBAGE_LIST_MEMG)
* sizeof(char *));
gf->ngarb = 0;
}
static void free_garbage_fifo(struct garbage_fifo *gf)
{
garbage_collect(gf);
freemem(gf->garbage);
freemem(gf);
}
/*
* order is important: it must match the token-constants declared as an
* enum in the header file.
*/
char *operators_name[] = {
" ", "\n", " ",
"0000", "name", "bunch", "pragma", "context",
"\"dummy string\"", "'dummy char'",
"/", "/=", "-", "--", "-=", "->", "+", "++", "+=", "<", "<=", "<<",
"<<=", ">", ">=", ">>", ">>=", "=", "==",
#ifdef CAST_OP
"=>",
#endif
"~", "!=", "&", "&&", "&=", "|", "||", "|=", "%", "%=", "*", "*=",
"^", "^=", "!",
"{", "}", "[", "]", "(", ")", ",", "?", ";",
":", ".", "...", "#", "##", " ", "ouch", "<:", ":>", "<%", "%>",
"%:", "%:%:"
};
/* the ascii representation of a token */
#ifdef SEMPER_FIDELIS
#define tname(x) (ttWHI((x).type) ? " " : S_TOKEN((x).type) \
? (x).name : operators_name[(x).type])
#else
#define tname(x) (S_TOKEN((x).type) ? (x).name \
: operators_name[(x).type])
#endif
char *token_name(struct token *t)
{
return tname(*t);
}
/*
* To speed up deeply nested and repeated inclusions, we:
* -- use a hash table to remember where we found each file
* -- remember when the file is protected by a #ifndef/#define/#endif
* construction; we can then avoid including several times a file
* when this is not necessary.
* -- remember in which directory, in the include path, the file was found.
*/
struct found_file {
hash_item_header head; /* first field */
char *name;
char *protect;
};
/*
* For files from system include path.
*/
struct found_file_sys {
hash_item_header head; /* first field */
struct found_file *rff;
int incdir;
};
static HTT found_files, found_files_sys;
static int found_files_init_done = 0, found_files_sys_init_done = 0;
static struct found_file *new_found_file(void)
{
struct found_file *ff = getmem(sizeof(struct found_file));
ff->name = 0;
ff->protect = 0;
return ff;
}
static void del_found_file(void *m)
{
struct found_file *ff = (struct found_file *)m;
if (ff->name) freemem(ff->name);
if (ff->protect) freemem(ff->protect);
freemem(ff);
}
static struct found_file_sys *new_found_file_sys(void)
{
struct found_file_sys *ffs = getmem(sizeof(struct found_file_sys));
ffs->rff = 0;
ffs->incdir = -1;
return ffs;
}
static void del_found_file_sys(void *m)
{
struct found_file_sys *ffs = (struct found_file_sys *)m;
freemem(ffs);
}
/*
* To keep up with the #ifndef/#define/#endif protection mechanism
* detection.
*/
struct protect protect_detect;
static struct protect *protect_detect_stack = 0;
void set_init_filename(char *x, int real_file)
{
if (current_filename) freemem(current_filename);
current_filename = sdup(x);
current_long_filename = 0;
current_incdir = -1;
if (real_file) {
protect_detect.macro = 0;
protect_detect.state = 1;
protect_detect.ff = new_found_file();
protect_detect.ff->name = sdup(x);
HTT_put(&found_files, protect_detect.ff, x);
} else {
protect_detect.state = 0;
}
}
static void init_found_files(void)
{
if (found_files_init_done) HTT_kill(&found_files);
HTT_init(&found_files, del_found_file);
found_files_init_done = 1;
if (found_files_sys_init_done) HTT_kill(&found_files_sys);
HTT_init(&found_files_sys, del_found_file_sys);
found_files_sys_init_done = 1;
}
/*
* Set the lexer state at the beginning of a file.
*/
static void reinit_lexer_state(struct lexer_state *ls, int wb)
{
#ifndef NO_UCPP_BUF
ls->input_buf = wb ? getmem(INPUT_BUF_MEMG) : 0;
#ifdef UCPP_MMAP
ls->from_mmap = 0;
#endif
#endif
ls->input = 0;
ls->ebuf = ls->pbuf = 0;
ls->nlka = 0;
ls->macfile = 0;
ls->discard = 1;
ls->last = 0; /* we suppose '\n' is not 0 */
ls->line = 1;
ls->ltwnl = 1;
ls->oline = 1;
ls->pending_token = 0;
ls->cli = 0;
ls->copy_line[COPY_LINE_LENGTH - 1] = 0;
ls->ifnest = 0;
ls->condf[0] = ls->condf[1] = 0;
}
/*
* Initialize the struct lexer_state, with optional input and output buffers.
*/
void init_buf_lexer_state(struct lexer_state *ls, int wb)
{
reinit_lexer_state(ls, wb);
#ifndef NO_UCPP_BUF
ls->output_buf = wb ? getmem(OUTPUT_BUF_MEMG) : 0;
#endif
ls->sbuf = 0;
ls->output_fifo = 0;
ls->ctok = getmem(sizeof(struct token));
ls->ctok->name = getmem(ls->tknl = TOKEN_NAME_MEMG);
ls->pending_token = 0;
ls->flags = 0;
ls->count_trigraphs = 0;
ls->gf = getmem(sizeof(struct garbage_fifo));
init_garbage_fifo(ls->gf);
ls->condcomp = 1;
ls->condnest = 0;
#ifdef INMACRO_FLAG
ls->inmacro = 0;
ls->macro_count = 0;
#endif
}
/*
* Initialize the (complex) struct lexer_state.
*/
void init_lexer_state(struct lexer_state *ls)
{
init_buf_lexer_state(ls, 1);
ls->input = 0;
}
/*
* Restore what is needed from a lexer_state. This is used for #include.
*/
static void restore_lexer_state(struct lexer_state *ls,
struct lexer_state *lsbak)
{
#ifndef NO_UCPP_BUF
freemem(ls->input_buf);
ls->input_buf = lsbak->input_buf;
#ifdef UCPP_MMAP
ls->from_mmap = lsbak->from_mmap;
ls->input_buf_sav = lsbak->input_buf_sav;
#endif
#endif
ls->input = lsbak->input;
ls->ebuf = lsbak->ebuf;
ls->pbuf = lsbak->pbuf;
ls->nlka = lsbak->nlka;
ls->discard = lsbak->discard;
ls->line = lsbak->line;
ls->oline = lsbak->oline;
ls->ifnest = lsbak->ifnest;
ls->condf[0] = lsbak->condf[0];
ls->condf[1] = lsbak->condf[1];
}
/*
* close input file operations on a struct lexer_state
*/
static void close_input(struct lexer_state *ls)
{
#ifdef UCPP_MMAP
if (ls->from_mmap) {
munmap((void *)ls->input_buf, ls->ebuf);
ls->from_mmap = 0;
ls->input_buf = ls->input_buf_sav;
}
#endif
if (ls->input) {
fclose(ls->input);
ls->input = 0;
}
}
/*
* file_context (and the two functions push_ and pop_) are used to save
* all that is needed when including a file.
*/
static struct file_context {
struct lexer_state ls;
char *name, *long_name;
int incdir;
} *ls_stack;
static size_t ls_depth = 0;
static void push_file_context(struct lexer_state *ls)
{
struct file_context fc;
fc.name = current_filename;
fc.long_name = current_long_filename;
fc.incdir = current_incdir;
mmv(&(fc.ls), ls, sizeof(struct lexer_state));
aol(ls_stack, ls_depth, fc, LS_STACK_MEMG);
ls_depth --;
aol(protect_detect_stack, ls_depth, protect_detect, LS_STACK_MEMG);
protect_detect.macro = 0;
}
static void pop_file_context(struct lexer_state *ls)
{
#ifdef AUDIT
if (ls_depth <= 0) ouch("prepare to meet thy creator");
#endif
close_input(ls);
restore_lexer_state(ls, &(ls_stack[-- ls_depth].ls));
if (protect_detect.macro) freemem(protect_detect.macro);
protect_detect = protect_detect_stack[ls_depth];
if (current_filename) freemem(current_filename);
current_filename = ls_stack[ls_depth].name;
current_long_filename = ls_stack[ls_depth].long_name;
current_incdir = ls_stack[ls_depth].incdir;
if (ls_depth == 0) {
freemem(ls_stack);
freemem(protect_detect_stack);
}
}
/*
* report_context() returns the list of successive includers of the
* current file, ending with a dummy entry with a negative line number.
* The caller is responsible for freeing the returned pointer.
*/
struct stack_context *report_context(void)
{
struct stack_context *sc;
size_t i;
sc = getmem((ls_depth + 1) * sizeof(struct stack_context));
for (i = 0; i < ls_depth; i ++) {
sc[i].name = ls_stack[ls_depth - i - 1].name;
sc[i].long_name = ls_stack[ls_depth - i - 1].long_name;
sc[i].line = ls_stack[ls_depth - i - 1].ls.line - 1;
}
sc[ls_depth].line = -1;
return sc;
}
/*
* init_lexer_mode() is used to end initialization of a struct lexer_state
* if it must be used for a lexer
*/
void init_lexer_mode(struct lexer_state *ls)
{
ls->flags = DEFAULT_LEXER_FLAGS;
ls->output_fifo = getmem(sizeof(struct token_fifo));
ls->output_fifo->art = ls->output_fifo->nt = 0;
ls->toplevel_of = ls->output_fifo;
ls->save_ctok = ls->ctok;
}
/*
* release memory used by a struct lexer_state; this implies closing
* any input stream held by this structure.
*/
void free_lexer_state(struct lexer_state *ls)
{
close_input(ls);
#ifndef NO_UCPP_BUF
if (ls->input_buf) {
freemem(ls->input_buf);
ls->input_buf = 0;
}
if (ls->output_buf) {
freemem(ls->output_buf);
ls->output_buf = 0;
}
#endif
if (ls->ctok && (!ls->output_fifo || ls->output_fifo->nt == 0)) {
freemem(ls->ctok->name);
freemem(ls->ctok);
ls->ctok = 0;
}
if (ls->gf) {
free_garbage_fifo(ls->gf);
ls->gf = 0;
}
if (ls->output_fifo) {
freemem(ls->output_fifo);
ls->output_fifo = 0;
}
}
/*
* Print line information.
*/
static void print_line_info(struct lexer_state *ls, unsigned long flags)
{
char *fn = current_long_filename ?
current_long_filename : current_filename;
char *b, *d;
b = getmem(50 + strlen(fn));
if (flags & GCC_LINE_NUM) {
sprintf(b, "# %ld \"%s\"\n", ls->line, fn);
} else {
sprintf(b, "#line %ld \"%s\"\n", ls->line, fn);
}
for (d = b; *d; d ++) put_char(ls, (unsigned char)(*d));
freemem(b);
}
/*
* Enter a file; this implies the possible emission of a #line directive.
* The flags used are passed as second parameter instead of being
* extracted from the struct lexer_state.
*
* As a command-line option, gcc-like directives (with only a '#',
* without 'line') may be produced.
*
* enter_file() returns 1 if a (CONTEXT) token was produced, 0 otherwise.
*/
int enter_file(struct lexer_state *ls, unsigned long flags)
{
char *fn = current_long_filename ?
current_long_filename : current_filename;
if (!(flags & LINE_NUM)) return 0;
if ((flags & LEXER) && !(flags & TEXT_OUTPUT)) {
struct token t;
t.type = CONTEXT;
t.line = ls->line;
t.name = fn;
print_token(ls, &t, 0);
return 1;
}
print_line_info(ls, flags);
ls->oline --; /* emitted #line troubled oline */
return 0;
}
#ifdef UCPP_MMAP
/*
* We open() the file, then fdopen() it and fseek() to its end. If the
* fseek() worked, we try to mmap() the file, up to the point where we
* arrived.
* On an architecture where end-of-lines are multibytes and translated
* into single '\n', bad things could happen. We strongly hope that, if
* we could fseek() to the end but could not mmap(), then we can get back.
*/
static void *find_file_map;
static size_t map_length;
FILE *fopen_mmap_file(char *name)
{
FILE *f;
int fd;
long l;
find_file_map = 0;
fd = open(name, O_RDONLY, 0);
if (fd < 0) return 0;
l = lseek(fd, 0, SEEK_END);
f = fdopen(fd, "r");
if (!f) {
close(fd);
return 0;
}
if (l < 0) return f; /* not seekable */
map_length = l;
if ((find_file_map = mmap(0, map_length, PROT_READ,
MAP_PRIVATE, fd, 0)) == MAP_FAILED) {
/* we could not mmap() the file; get back */
find_file_map = 0;
if (fseek(f, 0, SEEK_SET)) {
/* bwaah... can't get back. This file is cursed. */
fclose(f);
return 0;
}
}
return f;
}
void set_input_file(struct lexer_state *ls, FILE *f)
{
ls->input = f;
if (find_file_map) {
ls->from_mmap = 1;
ls->input_buf_sav = ls->input_buf;
ls->input_buf = find_file_map;
ls->pbuf = 0;
ls->ebuf = map_length;
} else {
ls->from_mmap = 0;
}
}
#endif
/*
* Find a file by looking through the include path.
* return value: a FILE * on the file, opened in "r" mode, or 0.
*
* find_file_error will contain:
* FF_ERROR on error (file not found or impossible to read)
* FF_PROTECT file is protected and therefore useless to read
* FF_KNOWN file is already known
* FF_UNKNOWN file was not already known
*/
static int find_file_error;
enum { FF_ERROR, FF_PROTECT, FF_KNOWN, FF_UNKNOWN };
static FILE *find_file(char *name, int localdir)
{
FILE *f;
int i, incdir = -1;
size_t nl = strlen(name);
char *s = 0;
struct found_file *ff = 0, *nff;
int lf = 0;
int nffa = 0;
find_file_error = FF_ERROR;
protect_detect.state = -1;
protect_detect.macro = 0;
if (localdir) {
int i;
char *rfn = current_long_filename ? current_long_filename
: current_filename;
for (i = strlen(rfn) - 1; i >= 0; i --)
#ifdef MSDOS
if (rfn[i] == '\\') break;
#else
if (rfn[i] == '/') break;
#endif
#if defined MSDOS
if (i >= 0 && *name != '\\' && (nl < 2 || name[1] != ':'))
#elif defined AMIGA
if (i >= 0 && *name != '/' && (nl < 2 || name[1] != ':'))
#else
if (i >= 0 && *name != '/')
#endif
{
/*
* current file is somewhere else, and the provided
* file name is not absolute, so we must adjust the
* base for looking for the file; besides,
* found_files and found_files_loc are irrelevant
* for this search.
*/
s = getmem(i + 2 + nl);
mmv(s, rfn, i);
#ifdef MSDOS
s[i] = '\\';
#else
s[i] = '/';
#endif
mmv(s + i + 1, name, nl);
s[i + 1 + nl] = 0;
ff = HTT_get(&found_files, s);
} else ff = HTT_get(&found_files, name);
}
if (!ff) {
struct found_file_sys *ffs = HTT_get(&found_files_sys, name);
if (ffs) {
ff = ffs->rff;
incdir = ffs->incdir;
}
}
/*
* At that point: if the file was found in the cache, ff points to
* the cached descriptive structure; its name is s if s is not 0,
* name otherwise.
*/
if (ff) goto found_file_cache;
/*
* This is the first time we find the file, or it was not protected.
*/
protect_detect.ff = new_found_file();
nffa = 1;
if (localdir &&
#ifdef UCPP_MMAP
(f = fopen_mmap_file(s ? s : name))
#else
(f = fopen(s ? s : name, "r"))
#endif
) {
lf = 1;
goto found_file;
}
/*
* If s contains a name, that name is now irrelevant: it was a
* filename for a search in the current directory, and the file
* was not found.
*/
if (s) {
freemem(s);
s = 0;
}
for (i = 0; (size_t)i < include_path_nb; i ++) {
size_t ni = strlen(include_path[i]);
s = getmem(ni + nl + 2);
mmv(s, include_path[i], ni);
#ifdef AMIGA
/* contributed by Volker Barthelmann */
if (ni == 1 && *s == '.') {
*s = 0;
ni = 0;
}
if (ni > 0 && s[ni - 1] != ':' && s[ni - 1] != '/') {
s[ni] = '/';
mmv(s + ni + 1, name, nl + 1);
} else {
mmv(s + ni, name, nl + 1);
}
#else
s[ni] = '/';
mmv(s + ni + 1, name, nl + 1);
#endif
#ifdef MSDOS
/* on msdos systems, replace all / by \ */
{
char *c;
for (c = s; *c; c ++) if (*c == '/') *c = '\\';
}
#endif
incdir = i;
if ((ff = HTT_get(&found_files, s)) != 0) {
/*
* The file is known, but not as a system include
* file under the name provided.
*/
struct found_file_sys *ffs = new_found_file_sys();
ffs->rff = ff;
ffs->incdir = incdir;
HTT_put(&found_files_sys, ffs, name);
freemem(s);
s = 0;
if (nffa) {
del_found_file(protect_detect.ff);
protect_detect.ff = 0;
nffa = 0;
}
goto found_file_cache;
}
#ifdef UCPP_MMAP
f = fopen_mmap_file(s);
#else
f = fopen(s, "r");
#endif
if (f) goto found_file;
freemem(s);
s = 0;
}
zero_out:
if (s) freemem(s);
if (nffa) {
del_found_file(protect_detect.ff);
protect_detect.ff = 0;
nffa = 0;
}
return 0;
/*
* This part is invoked when the file was found in the
* cache.
*/
found_file_cache:
if (ff->protect) {
if (get_macro(ff->protect)) {
/* file is protected, do not include it */
find_file_error = FF_PROTECT;
goto zero_out;
}
/* file is protected but the guardian macro is
not available; disable guardian detection. */
protect_detect.state = 0;
}
protect_detect.ff = ff;
#ifdef UCPP_MMAP
f = fopen_mmap_file(HASH_ITEM_NAME(ff));
#else
f = fopen(HASH_ITEM_NAME(ff), "r");
#endif
if (!f) goto zero_out;
find_file_error = FF_KNOWN;
goto found_file_2;
/*
* This part is invoked when we found a new file, which was not
* yet referenced. If lf == 1, then the file was found directly,
* otherwise it was found in some system include directory.
* A new found_file structure has been allocated and is in
* protect_detect.ff
*/
found_file:
if (f && ((emit_dependencies == 1 && lf && current_incdir == -1)
|| emit_dependencies == 2)) {
fprintf(emit_output, " %s", s ? s : name);
}
nff = protect_detect.ff;
nff->name = sdup(name);
#ifdef AUDIT
if (
#endif
HTT_put(&found_files, nff, s ? s : name)
#ifdef AUDIT
) ouch("filename collided with a wraith")
#endif
;
if (!lf) {
struct found_file_sys *ffs = new_found_file_sys();
ffs->rff = nff;
ffs->incdir = incdir;
HTT_put(&found_files_sys, ffs, name);
}
if (s) freemem(s);
s = 0;
find_file_error = FF_UNKNOWN;
ff = nff;
found_file_2:
if (s) freemem(s);
current_long_filename = HASH_ITEM_NAME(ff);
#ifdef NO_LIBC_BUF
setbuf(f, 0);
#endif
current_incdir = incdir;
return f;
}
/*
* Find the named file by looking through the end of the include path.
* This is for #include_next directives.
* #include_next <foo> and #include_next "foo" are considered identical,
* for all practical purposes.
*/
static FILE *find_file_next(char *name)
{
int i;
size_t nl = strlen(name);
FILE *f;
struct found_file *ff;
find_file_error = FF_ERROR;
protect_detect.state = -1;
protect_detect.macro = 0;
for (i = current_incdir + 1; (size_t)i < include_path_nb; i ++) {
char *s;
size_t ni = strlen(include_path[i]);
s = getmem(ni + nl + 2);
mmv(s, include_path[i], ni);
s[ni] = '/';
mmv(s + ni + 1, name, nl + 1);
#ifdef MSDOS
/* on msdos systems, replace all / by \ */
{
char *c;
for (c = s; *c; c ++) if (*c == '/') *c = '\\';
}
#endif
ff = HTT_get(&found_files, s);
if (ff) {
/* file was found in the cache */
if (ff->protect) {
if (get_macro(ff->protect)) {
find_file_error = FF_PROTECT;
freemem(s);
return 0;
}
/* file is protected but the guardian macro is
not available; disable guardian detection. */
protect_detect.state = 0;
}
protect_detect.ff = ff;
#ifdef UCPP_MMAP
f = fopen_mmap_file(HASH_ITEM_NAME(ff));
#else
f = fopen(HASH_ITEM_NAME(ff), "r");
#endif
if (!f) {
/* file is referenced but yet unavailable. */
freemem(s);
return 0;
}
find_file_error = FF_KNOWN;
freemem(s);
s = HASH_ITEM_NAME(ff);
} else {
#ifdef UCPP_MMAP
f = fopen_mmap_file(s);
#else
f = fopen(s, "r");
#endif
if (f) {
if (emit_dependencies == 2) {
fprintf(emit_output, " %s", s);
}
ff = protect_detect.ff = new_found_file();
ff->name = sdup(s);
#ifdef AUDIT
if (
#endif
HTT_put(&found_files, ff, s)
#ifdef AUDIT
) ouch("filename collided with a wraith")
#endif
;
find_file_error = FF_UNKNOWN;
freemem(s);
s = HASH_ITEM_NAME(ff);
}
}
if (f) {
current_long_filename = s;
current_incdir = i;
return f;
}
freemem(s);
}
return 0;
}
/*
* The #if directive. This function parse the expression, performs macro
* expansion (and handles the "defined" operator), and call eval_expr.
* return value: 1 if the expression is true, 0 if it is false, -1 on error.
*/
static int handle_if(struct lexer_state *ls)
{
struct token_fifo tf, tf1, tf2, tf3, *save_tf;
long l = ls->line;
unsigned long z;
int ret = 0, ltww = 1;
/* first, get the whole line */
tf.art = tf.nt = 0;
while (!next_token(ls) && ls->ctok->type != NEWLINE) {
struct token t;
if (ltww && ttMWS(ls->ctok->type)) continue;
ltww = ttMWS(ls->ctok->type);
t.type = ls->ctok->type;
t.line = l;