-
Notifications
You must be signed in to change notification settings - Fork 0
/
cccp.c
5841 lines (5086 loc) · 148 KB
/
cccp.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 Compatible Compiler Preprocessor (CCCP)
Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
Written by Paul Rubin, June 1986
Adapted to ANSI C, Richard Stallman, Jan 1987
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 1, or (at your option) any
later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
In other words, you are welcome to use, share and improve this program.
You are forbidden to forbid anyone else to use, share and improve
what you give them. Help stamp out software-hoarding! */
typedef unsigned char U_CHAR;
#ifdef EMACS
#define NO_SHORTNAMES
#include "../src/config.h"
#ifdef open
#undef open
#undef read
#undef write
#endif /* open */
#endif /* EMACS */
#ifndef EMACS
#include "config.h"
#endif /* not EMACS */
/* In case config.h defines these. */
#undef bcopy
#undef bzero
#undef bcmp
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <stdio.h>
#include <signal.h>
#ifndef VMS
#include <sys/file.h>
#ifndef USG
#include <sys/time.h> /* for __DATE__ and __TIME__ */
#include <sys/resource.h>
#else
#define index strchr
#define rindex strrchr
#include <time.h>
#include <fcntl.h>
#endif /* USG */
#endif /* not VMS */
/* VMS-specific definitions */
#ifdef VMS
#include <time.h>
#include <errno.h> /* This defines "errno" properly */
#include <perror.h> /* This defines sys_errlist/sys_nerr properly */
#define O_RDONLY 0 /* Open arg for Read/Only */
#define O_WRONLY 1 /* Open arg for Write/Only */
#define read(fd,buf,size) VAX11_C_read(fd,buf,size)
#define write(fd,buf,size) VAX11_C_write(fd,buf,size)
#ifdef __GNUC__
#define BSTRING /* VMS/GCC supplies the bstring routines */
#endif /* __GNUC__ */
#endif /* VMS */
#define max(a,b) ((a) > (b) ? (a) : (b))
/* External declarations. */
void bcopy (), bzero ();
int bcmp ();
extern char *getenv ();
extern char *version_string;
/* Forward declarations. */
struct directive;
struct file_buf;
struct arglist;
struct argdata;
int do_define (), do_line (), do_include (), do_undef (), do_error (),
do_pragma (), do_if (), do_xifdef (), do_else (),
do_elif (), do_endif (), do_sccs (), do_once (), do_ident();
struct hashnode *install ();
struct hashnode *lookup ();
char *xmalloc (), *xrealloc (), *xcalloc (), *savestring ();
void fatal (), fancy_abort (), pfatal_with_name (), perror_with_name ();
void macroexpand ();
void dump_all_macros ();
void conditional_skip ();
void skip_if_group ();
void output_line_command ();
/* Last arg to output_line_command. */
enum file_change_code {same_file, enter_file, leave_file};
int grow_outbuf ();
int handle_directive ();
void memory_full ();
U_CHAR *macarg1 ();
char *macarg ();
U_CHAR *skip_to_end_of_comment ();
U_CHAR *skip_quoted_string ();
#ifndef FATAL_EXIT_CODE
#define FATAL_EXIT_CODE 33 /* gnu cc command understands this */
#endif
#ifndef SUCCESS_EXIT_CODE
#define SUCCESS_EXIT_CODE 0 /* 0 means success on Unix. */
#endif
/* Name under which this program was invoked. */
char *progname;
/* Nonzero means handle C++ comment syntax and use
extra default include directories for C++. */
int cplusplus;
/* Current maximum length of directory names in the search path
for include files. (Altered as we get more of them.) */
int max_include_len;
/* Nonzero means copy comments into the output file. */
int put_out_comments = 0;
/* Nonzero means don't process the ANSI trigraph sequences. */
int no_trigraphs = 0;
/* Nonzero means print the names of included files rather than
the preprocessed output. 1 means just the #include "...",
2 means #include <...> as well. */
int print_deps = 0;
/* Nonzero means don't output line number information. */
int no_line_commands;
/* Nonzero means inhibit output of the preprocessed text
and instead output the definitions of all user-defined macros
in a form suitable for use as input to cccp. */
int dump_macros;
/* Nonzero means give all the error messages the ANSI standard requires. */
int pedantic;
/* Nonzero means warn if slash-star appears in a comment. */
int warn_comments;
/* Nonzero means warn if there are any trigraphs. */
int warn_trigraphs;
/* Nonzero means try to imitate old fashioned non-ANSI preprocessor. */
int traditional;
#ifdef ti1500
/* added a special preprocessor mode for ti1500 */
int timode=0;
#endif
/* Nonzero causes output not to be done,
but directives such as #define that have side effects
are still obeyed. */
int no_output;
/* Flag is set if the program is compiled with -g option */
int gdb_flag = 0;
/* I/O buffer structure.
The `fname' field is nonzero for source files and #include files
and for the dummy text used for -D and -U.
It is zero for rescanning results of macro expansion
and for expanding macro arguments. */
#define INPUT_STACK_MAX 200
struct file_buf {
char *fname;
int lineno;
int length;
U_CHAR *buf;
U_CHAR *bufp;
/* Macro that this level is the expansion of.
Included so that we can reenable the macro
at the end of this level. */
struct hashnode *macro;
/* Value of if_stack at start of this file.
Used to prohibit unmatched #endif (etc) in an include file. */
struct if_stack *if_stack;
/* Object to be freed at end of input at this level. */
U_CHAR *free_ptr;
} instack[INPUT_STACK_MAX];
/* Current nesting level of input sources.
`instack[indepth]' is the level currently being read. */
int indepth = -1;
#define CHECK_DEPTH(code) \
if (indepth >= (INPUT_STACK_MAX - 1)) \
{ \
error_with_line (line_for_error (instack[indepth].lineno), \
"macro or #include recursion too deep"); \
code; \
}
/* Current depth in #include directives that use <...>. */
int system_include_depth = 0;
typedef struct file_buf FILE_BUF;
/* The output buffer. Its LENGTH field is the amount of room allocated
for the buffer, not the number of chars actually present. To get
that, subtract outbuf.buf from outbuf.bufp. */
#define OUTBUF_SIZE 10 /* initial size of output buffer */
FILE_BUF outbuf;
/* Grow output buffer OBUF points at
so it can hold at least NEEDED more chars. */
#define check_expand(OBUF, NEEDED) \
(((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
? grow_outbuf ((OBUF), (NEEDED)) : 0)
struct file_name_list
{
struct file_name_list *next;
char *fname;
};
/* #include "file" looks in source file dir, then stack. */
/* #include <file> just looks in the stack. */
/* -I directories are added to the end, then the defaults are added. */
struct file_name_list include_defaults[] =
{
#ifndef VMS
{ &include_defaults[1], GCC_INCLUDE_DIR },
{ &include_defaults[2], "/usr/include" },
{ 0, "/usr/local/include" }
#else
{ &include_defaults[1], "GNU_CC_INCLUDE:" }, /* GNU includes */
{ &include_defaults[2], "SYS$SYSROOT:[SYSLIB.]" }, /* VAX-11 "C" includes */
{ 0, "" }, /* This makes normal VMS filespecs work OK */
#endif /* VMS */
};
/* These are used instead of the above, for C++. */
struct file_name_list cplusplus_include_defaults[] =
{
#ifndef VMS
/* Pick up GNU C++ specific include files. */
{ &cplusplus_include_defaults[1], GPLUSPLUS_INCLUDE_DIR },
/* Use GNU CC specific header files. */
{ &cplusplus_include_defaults[2], GCC_INCLUDE_DIR },
{ 0, "/usr/include" }
#else
{ &cplusplus_include_defaults[1], "GNU_GXX_INCLUDE:" },
{ &cplusplus_include_defaults[2], "GNU_CC_INCLUDE:" },
/* VAX-11 C includes */
{ &cplusplus_include_defaults[3], "SYS$SYSROOT:[SYSLIB.]" },
{ 0, "" }, /* This makes normal VMS filespecs work OK */
#endif /* VMS */
};
struct file_name_list *include = 0; /* First dir to search */
/* First dir to search for <file> */
struct file_name_list *first_bracket_include = 0;
struct file_name_list *last_include = 0; /* Last in chain */
/* List of included files that contained #once. */
struct file_name_list *dont_repeat_files = 0;
/* List of other included files. */
struct file_name_list *all_include_files = 0;
/* Structure allocated for every #define. For a simple replacement
such as
#define foo bar ,
nargs = -1, the `pattern' list is null, and the expansion is just
the replacement text. Nargs = 0 means a functionlike macro with no args,
e.g.,
#define getchar() getc (stdin) .
When there are args, the expansion is the replacement text with the
args squashed out, and the reflist is a list describing how to
build the output from the input: e.g., "3 chars, then the 1st arg,
then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
The chars here come from the expansion. Whatever is left of the
expansion after the last arg-occurrence is copied after that arg.
Note that the reflist can be arbitrarily long---
its length depends on the number of times the arguments appear in
the replacement text, not how many args there are. Example:
#define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
pattern list
{ (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
where (x, y) means (nchars, argno). */
typedef struct definition DEFINITION;
struct definition {
int nargs;
int length; /* length of expansion string */
U_CHAR *expansion;
struct reflist {
struct reflist *next;
char stringify; /* nonzero if this arg was preceded by a
# operator. */
#ifdef ti1500
int stringify1; /* 1 if arg is preceded by # and also have traditional string processing */
#endif
char raw_before; /* Nonzero if a ## operator before arg. */
char raw_after; /* Nonzero if a ## operator after arg. */
int nchars; /* Number of literal chars to copy before
this arg occurrence. */
int argno; /* Number of arg to substitute (origin-0) */
} *pattern;
/* Names of macro args, concatenated in reverse order
with comma-space between them.
The only use of this is that we warn on redefinition
if this differs between the old and new definitions. */
U_CHAR *argnames;
};
/* different kinds of things that can appear in the value field
of a hash node. Actually, this may be useless now. */
union hashval {
int ival;
char *cpval;
DEFINITION *defn;
};
/* The structure of a node in the hash table. The hash table
has entries for all tokens defined by #define commands (type T_MACRO),
plus some special tokens like __LINE__ (these each have their own
type, and the appropriate code is run when that type of node is seen.
It does not contain control words like "#define", which are recognized
by a separate piece of code. */
/* different flavors of hash nodes --- also used in keyword table */
enum node_type {
T_DEFINE = 1, /* the `#define' keyword */
T_INCLUDE, /* the `#include' keyword */
T_IFDEF, /* the `#ifdef' keyword */
T_IFNDEF, /* the `#ifndef' keyword */
T_IF, /* the `#if' keyword */
T_ELSE, /* `#else' */
T_PRAGMA, /* `#pragma' */
T_ELIF, /* `#else' */
T_UNDEF, /* `#undef' */
T_LINE, /* `#line' */
T_ERROR, /* `#error' */
T_ENDIF, /* `#endif' */
T_SCCS, /* `#sccs', used on system V. */
T_IDENT, /* `#ident', used on system V. */
T_SPECLINE, /* special symbol `__LINE__' */
T_DATE, /* `__DATE__' */
T_FILE, /* `__FILE__' */
T_BASE_FILE, /* `__BASE_FILE__' */
T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
T_VERSION, /* `__VERSION__' */
T_TIME, /* `__TIME__' */
T_CONST, /* Constant value, used by `__STDC__' */
T_MACRO, /* macro defined by `#define' */
T_DISABLED, /* macro temporarily turned off for rescan */
T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
T_UNUSED /* Used for something not defined. */
};
struct hashnode {
struct hashnode *next; /* double links for easy deletion */
struct hashnode *prev;
struct hashnode **bucket_hdr; /* also, a back pointer to this node's hash
chain is kept, in case the node is the head
of the chain and gets deleted. */
enum node_type type; /* type of special token */
int length; /* length of token, for quick comparison */
U_CHAR *name; /* the actual name */
union hashval value; /* pointer to expansion, or whatever */
};
typedef struct hashnode HASHNODE;
/* Some definitions for the hash table. The hash function MUST be
computed as shown in hashf () below. That is because the rescan
loop computes the hash value `on the fly' for most tokens,
in order to avoid the overhead of a lot of procedure calls to
the hashf () function. Hashf () only exists for the sake of
politeness, for use when speed isn't so important. */
#define HASHSIZE 1403
HASHNODE *hashtab[HASHSIZE];
#define HASHSTEP(old, c) ((old << 2) + c)
#define MAKE_POS(v) (v & ~0x80000000) /* make number positive */
/* Symbols to predefine. */
#ifdef CPP_PREDEFINES
char *predefs = CPP_PREDEFINES;
#else
char *predefs = "";
#endif
/* `struct directive' defines one #-directive, including how to handle it. */
struct directive {
int length; /* Length of name */
int (*func)(); /* Function to handle directive */
char *name; /* Name of directive */
enum node_type type; /* Code which describes which directive. */
char angle_brackets; /* Nonzero => <...> is special. */
char traditional_comments; /* Nonzero: keep comments if -traditional. */
char pass_thru; /* Copy preprocessed directive to output file. */
};
/* Here is the actual list of #-directives, most-often-used first. */
struct directive directive_table[] = {
{ 6, do_define, "define", T_DEFINE, 0, 1},
{ 2, do_if, "if", T_IF},
{ 5, do_xifdef, "ifdef", T_IFDEF},
{ 6, do_xifdef, "ifndef", T_IFNDEF},
{ 5, do_endif, "endif", T_ENDIF},
{ 4, do_else, "else", T_ELSE},
{ 4, do_elif, "elif", T_ELIF},
{ 4, do_line, "line", T_LINE},
{ 7, do_include, "include", T_INCLUDE, 1},
{ 5, do_undef, "undef", T_UNDEF},
{ 5, do_error, "error", T_ERROR},
#ifdef SCCS_DIRECTIVE
{ 4, do_sccs, "sccs", T_SCCS},
#endif
#ifdef ti1500
/* added for #ident directive for TI S1500 */
{ 5, do_ident, "ident", T_IDENT,0,0,1},
#else
{ 5, do_ident, "ident", T_IDENT},
#endif
{ 6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
{ -1, 0, "", T_UNUSED},
};
/* table to tell if char can be part of a C identifier. */
U_CHAR is_idchar[256];
/* table to tell if char can be first char of a c identifier. */
U_CHAR is_idstart[256];
/* table to tell if c is horizontal space. */
U_CHAR is_hor_space[256];
/* table to tell if c is horizontal or vertical space. */
U_CHAR is_space[256];
#define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
#define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
int errors = 0; /* Error counter for exit code */
/* Zero means dollar signs are punctuation.
-$ stores 0; -traditional, stores 1. Default is 1 for VMS, 0 otherwise.
This must be 0 for correct processing of this ANSI C program:
#define foo(a) #a
#define lose(b) foo(b)
#define test$
lose(test) */
#ifndef DOLLARS_IN_IDENTIFIERS
#define DOLLARS_IN_IDENTIFIERS 0
#endif
int dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
FILE_BUF expand_to_temp_buffer ();
DEFINITION *collect_expansion ();
/* Stack of conditionals currently in progress
(including both successful and failing conditionals). */
struct if_stack {
struct if_stack *next; /* for chaining to the next stack frame */
char *fname; /* copied from input when frame is made */
int lineno; /* similarly */
int if_succeeded; /* true if a leg of this if-group
has been passed through rescan */
enum node_type type; /* type of last directive seen in this group */
};
typedef struct if_stack IF_STACK_FRAME;
IF_STACK_FRAME *if_stack = NULL;
/* Buffer of -M output. */
char *deps_buffer;
/* Number of bytes allocated in above. */
int deps_allocated_size;
/* Number of bytes used. */
int deps_size;
/* Number of bytes since the last newline. */
int deps_column;
/* Nonzero means -I- has been seen,
so don't look for #include "foo" the source-file directory. */
int ignore_srcdir;
/* Handler for SIGPIPE. */
static void
pipe_closed ()
{
fatal ("output pipe has been closed");
}
int
main (argc, argv)
int argc;
char **argv;
{
int st_mode;
long st_size;
char *in_fname, *out_fname;
int f, i;
FILE_BUF *fp;
char **pend_files = (char **) xmalloc (argc * sizeof (char *));
char **pend_defs = (char **) xmalloc (argc * sizeof (char *));
char **pend_undefs = (char **) xmalloc (argc * sizeof (char *));
int inhibit_predefs = 0;
int no_standard_includes = 0;
/* Non-0 means don't output the preprocessed program. */
int inhibit_output = 0;
/* Stream on which to print the dependency information. */
FILE *deps_stream = 0;
/* Target-name to write with the dependency information. */
char *deps_target = 0;
#ifdef RLIMIT_STACK
/* Get rid of any avoidable limit on stack size. */
{
struct rlimit rlim;
/* Set the stack limit huge so that alloca (particularly stringtab
* in dbxread.c) does not fail. */
getrlimit (RLIMIT_STACK, &rlim);
rlim.rlim_cur = rlim.rlim_max;
setrlimit (RLIMIT_STACK, &rlim);
}
#endif /* RLIMIT_STACK defined */
progname = argv[0];
#ifdef VMS
{
/* Remove directories from PROGNAME. */
char *s;
extern char *rindex ();
progname = savestring (argv[0]);
if (!(s = rindex (progname, ']')))
s = rindex (progname, ':');
if (s)
strcpy (progname, s+1);
if (s = rindex (progname, '.'))
*s = '\0';
}
#endif
in_fname = NULL;
out_fname = NULL;
/* Initialize is_idchar to allow $. */
dollars_in_ident = 1;
initialize_char_syntax ();
dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
no_line_commands = 0;
no_trigraphs = 1;
dump_macros = 0;
no_output = 0;
cplusplus = 0;
#ifdef CPLUSPLUS
cplusplus = 1;
#endif
signal (SIGPIPE, pipe_closed);
#ifndef VMS
max_include_len
= max (max (sizeof (GCC_INCLUDE_DIR),
sizeof (GPLUSPLUS_INCLUDE_DIR)),
sizeof ("/usr/include/CC"));
#else /* VMS */
max_include_len
= sizeof("SYS$SYSROOT:[SYSLIB.]");
#endif /* VMS */
bzero (pend_files, argc * sizeof (char *));
bzero (pend_defs, argc * sizeof (char *));
bzero (pend_undefs, argc * sizeof (char *));
/* Process switches and find input file name. */
for (i = 1; i < argc; i++) {
if (argv[i][0] != '-') {
if (out_fname != NULL)
fatal ("Usage: %s [switches] input output", argv[0]);
else if (in_fname != NULL)
out_fname = argv[i];
else
in_fname = argv[i];
} else {
switch (argv[i][1]) {
case 'i':
if (argv[i][2] != 0)
pend_files[i] = argv[i] + 2;
else if (i + 1 == argc)
fatal ("Filename missing after -i option");
else
pend_files[i] = argv[i+1], i++;
break;
case 'o':
if (out_fname != NULL)
fatal ("Output filename specified twice");
if (i + 1 == argc)
fatal ("Filename missing after -o option");
out_fname = argv[++i];
if (!strcmp (out_fname, "-"))
out_fname = "";
break;
case 'p':
pedantic = 1;
break;
case 't':
if (!strcmp (argv[i], "-traditional")) {
traditional = 1;
dollars_in_ident = 1;
} else if (!strcmp (argv[i], "-trigraphs")) {
no_trigraphs = 0;
}
break;
case '+':
cplusplus = 1;
break;
case 'W':
if (!strcmp (argv[i], "-Wtrigraphs")) {
warn_trigraphs = 1;
}
if (!strcmp (argv[i], "-Wcomments"))
warn_comments = 1;
if (!strcmp (argv[i], "-Wcomment"))
warn_comments = 1;
if (!strcmp (argv[i], "-Wall")) {
warn_trigraphs = 1;
warn_comments = 1;
}
break;
case 'M':
if (!strcmp (argv[i], "-M"))
print_deps = 2;
else if (!strcmp (argv[i], "-MM"))
print_deps = 1;
inhibit_output = 1;
break;
case 'd':
dump_macros = 1;
no_output = 1;
break;
case 'v':
fprintf (stderr, "GNU CPP version %s\n", version_string);
break;
case 'D':
{
char *p, *p1;
if (argv[i][2] != 0)
p = argv[i] + 2;
else if (i + 1 == argc)
fatal ("Macro name missing after -D option");
else
p = argv[++i];
if ((p1 = (char *) index (p, '=')) != NULL)
*p1 = ' ';
pend_defs[i] = p;
}
break;
case 'U': /* JF #undef something */
if (argv[i][2] != 0)
pend_undefs[i] = argv[i] + 2;
else if (i + 1 == argc)
fatal ("Macro name missing after -U option");
else
pend_undefs[i] = argv[i+1], i++;
break;
case 'C':
put_out_comments = 1;
break;
case 'E': /* -E comes from cc -E; ignore it. */
break;
case 'P':
no_line_commands = 1;
break;
case '$': /* Don't include $ in identifiers. */
dollars_in_ident = 0;
break;
case 'I': /* Add directory to path for includes. */
{
struct file_name_list *dirtmp;
if (! ignore_srcdir && !strcmp (argv[i] + 2, "-"))
ignore_srcdir = 1;
else {
dirtmp = (struct file_name_list *)
xmalloc (sizeof (struct file_name_list));
dirtmp->next = 0; /* New one goes on the end */
if (include == 0)
include = dirtmp;
else
last_include->next = dirtmp;
last_include = dirtmp; /* Tail follows the last one */
if (argv[i][2] != 0)
dirtmp->fname = argv[i] + 2;
else if (i + 1 == argc)
fatal ("Directory name missing after -I option");
else
dirtmp->fname = argv[++i];
if (strlen (dirtmp->fname) > max_include_len)
max_include_len = strlen (dirtmp->fname);
if (ignore_srcdir && first_bracket_include == 0)
first_bracket_include = dirtmp;
}
}
break;
case 'n':
/* -nostdinc causes no default include directories.
You must specify all include-file directories with -I. */
no_standard_includes = 1;
break;
case 'u':
/* Sun compiler passes undocumented switch "-undef".
Let's assume it means to inhibit the predefined symbols. */
inhibit_predefs = 1;
break;
case '\0': /* JF handle '-' as file name meaning stdin or stdout */
if (in_fname == NULL) {
in_fname = "";
break;
} else if (out_fname == NULL) {
out_fname = "";
break;
} /* else fall through into error */
case 'g': /* put code for flushing out the stdout when debugging
the program */
/* changing it for compling bsh */
/*gdb_flag = 1;*/
gdb_flag = 0;
break;
default:
fatal ("Invalid option `%s'", argv[i]);
}
}
}
/* Added for TI S1500 OS and com software compilation -- perform
traditional preprocessing unless -ansi or -pedantic option
are given -- Shawn Islam */
#ifdef ti1500
if ((no_trigraphs == 1) && (pedantic == 0))
timode = 1;
#endif
/* Now that dollars_in_ident is known, initialize is_idchar. */
initialize_char_syntax ();
/* Install __LINE__, etc. Must follow initialize_char_syntax
and option processing. */
initialize_builtins ();
/* Do standard #defines that identify processor type. */
if (!inhibit_predefs) {
char *p = (char *) alloca (strlen (predefs) + 1);
strcpy (p, predefs);
while (*p) {
char *q;
if (p[0] != '-' || p[1] != 'D')
abort ();
q = &p[2];
while (*p && *p != ' ') p++;
if (*p != 0)
*p++= 0;
make_definition (q);
}
}
/* Do defines specified with -D. */
for (i = 1; i < argc; i++)
if (pend_defs[i])
make_definition (pend_defs[i]);
/* Do undefines specified with -U. */
for (i = 1; i < argc; i++)
if (pend_undefs[i])
make_undef (pend_undefs[i]);
/* Unless -fnostdinc,
tack on the standard include file dirs to the specified list */
if (!no_standard_includes) {
if (include == 0)
include = (cplusplus ? cplusplus_include_defaults : include_defaults);
else
last_include->next
= (cplusplus ? cplusplus_include_defaults : include_defaults);
/* Make sure the list for #include <...> also has the standard dirs. */
if (ignore_srcdir && first_bracket_include == 0)
first_bracket_include
= (cplusplus ? cplusplus_include_defaults : include_defaults);
}
/* Initialize output buffer */
outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
outbuf.bufp = outbuf.buf;
outbuf.length = OUTBUF_SIZE;
/* Scan the -i files before the main input.
Much like #including them, but with no_output set
so that only their macro definitions matter. */
no_output++;
for (i = 1; i < argc; i++)
if (pend_files[i]) {
int fd = open (pend_files[i], O_RDONLY, 0666);
if (fd < 0) {
perror_with_name (pend_files[i]);
return FATAL_EXIT_CODE;
}
finclude (fd, pend_files[i], &outbuf);
}
no_output--;
/* Create an input stack level for the main input file
and copy the entire contents of the file into it. */
fp = &instack[++indepth];
/* JF check for stdin */
if (in_fname == NULL || *in_fname == 0) {
in_fname = "";
f = 0;
} else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
goto perror;
/* Either of two environment variables can specify output of deps.
Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
where OUTPUT_FILE is the file to write deps info to
and DEPS_TARGET is the target to mention in the deps. */
if (print_deps == 0
&& (getenv ("SUNPRO_DEPENDENCIES") != 0
|| getenv ("DEPENDENCIES_OUTPUT") != 0))
{
char *spec = getenv ("DEPENDENCIES_OUTPUT");
char *s;
char *output_file;
if (spec == 0)
{
spec = getenv ("SUNPRO_DEPENDENCIES");
print_deps = 2;
}
else
print_deps = 1;
s = spec;
/* Find the space before the DEPS_TARGET, if there is one. */
/* Don't use `index'; that causes trouble on USG. */
while (*s != 0 && *s != ' ') s++;
if (*s != 0)
{
deps_target = s + 1;
output_file = (char *) xmalloc (s - spec + 1);
bcopy (spec, output_file, s - spec);
output_file[s - spec] = 0;
}
else
{
deps_target = 0;
output_file = spec;
}
deps_stream = fopen (output_file, "a");
if (deps_stream == 0)
pfatal_with_name (output_file);
}
/* If the -M option was used, output the deps to standard output. */
else if (print_deps)
deps_stream = stdout;
/* For -M, print the expected object file name
as the target of this Make-rule. */
if (print_deps) {
deps_allocated_size = 200;
deps_buffer = (char *) xmalloc (deps_allocated_size);
deps_buffer[0] = 0;
deps_size = 0;
deps_column = 0;
if (deps_target) {
deps_output (deps_target, 0);
deps_output (":", 0);
} else if (*in_fname == 0)
deps_output ("-: ", 0);
else {
int len;
char *p = in_fname;
char *p1 = p;
/* Discard all directory prefixes from P. */
while (*p1) {
if (*p1 == '/')
p = p1 + 1;
p1++;
}
/* Output P, but remove known suffixes. */
len = strlen (p);
if (p[len - 2] == '.' && (p[len - 1] == 'c' || p[len - 1] == 'C'))
deps_output (p, len - 2);
else if (p[len - 3] == '.'
&& p[len - 2] == 'c'
&& p[len - 1] == 'c')
deps_output (p, len - 3);
else
deps_output (p, 0);
/* Supply our own suffix. */
deps_output (".o : ", 0);
deps_output (in_fname, 0);
deps_output (" ", 0);
}
}
file_size_and_mode (f, &st_mode, &st_size);
fp->fname = in_fname;
fp->lineno = 1;
/* JF all this is mine about reading pipes and ttys */
if ((st_mode & S_IFMT) != S_IFREG) {
/* Read input from a file that is not a normal disk file.
We cannot preallocate a buffer with the correct size,
so we must read in the file a piece at the time and make it bigger. */
int size;
int bsize;
int cnt;
U_CHAR *bufp;