-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcc1.c
168 lines (149 loc) · 3.34 KB
/
cc1.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
/*
* this file is the compiler first pass.
* it takes source code consisting of unpreprocessed c, include files,
* and command line defines and outputs parse trees and static data
* it does basically all of the semantic processing, like operator precedence
* and type resolution
*/
#include "ccc.h"
// #include <fcntl.h>
#include "debugtags.c"
char *progname;
int verbose;
/*
* each file on the command line gets this treatment
*/
void
process(char *f)
{
int i;
char *s;
if (VERBOSE(V_TRACE)) {
printf("process %s\n", f);
}
if (write_cpp_file) {
if (cpp_file) {
close(cpp_file);
cpp_file = 0;
free(cpp_file_name);
}
i = strlen(f);
if (f[i-2] == '.' && f[i-1] == 'c') {
i -= 2;
}
cpp_file_name = malloc(i+2);
strncpy(cpp_file_name, f, i);
strcat(cpp_file_name, ".i");
cpp_file = creat(cpp_file_name, 0777);
if (cpp_file == -1) {
perror(cpp_file_name);
}
s = "/* preprocessed file */\n";
cpp_out(s, strlen(s));
}
insertfile(f, 0);
ioinit();
lexinit();
#ifdef LEXTEST
while (cur.type) {
gettoken();
}
#else
parse();
#endif
if (write_cpp_file) {
cpp_flush();
}
}
void
usage(char *complaint, char *p)
{
int i;
printf("%s", complaint);
printf("usage: %s [<options>] [program [<program options>]]\n", p);
printf("\t-I<include dir>\n");
printf("\t-D<variable>[=<definition>]\n");
printf("\t-v <verbosity>\n");
printf("\t-E\n");
for (i = 0; vopts[i]; i++) {
printf("\t%x %s\n", 1 << i, vopts[i]);
}
exit(1);
}
int
main(int argc, char **argv)
{
char *s;
int i;
progname = *argv++;
argc--;
while (argc) {
s = *argv;
/* end of flagged options */
if (*s++ != '-')
break;
argv++;
argc--;
/* s is the flagged arg string */
while (*s) {
switch (*s++) {
case 'h':
usage("", progname);
break;
case 'I':
add_include(s);
s="";
break;
case 'D':
add_define(s);
s="";
break;
case 'E':
write_cpp_file++;
break;
case 'v':
if (!argc--) {
usage("verbosity not specified \n", progname);
}
verbose = strtol(*argv++, 0, 0);
break;
default:
printf("bad flag %c\n", (*s));
break;
}
}
}
if (verbose) {
int j = 0;
for (i = 0; i < 32; i++) {
if (!vopts[i])
break;
if (verbose & (1 << i))
j |= (1 <<i);
}
printf("verbose: %x (", j);
for (i = 0; vopts[i]; i++) {
if (j & (1 << i)) {
printf("%s", vopts[i]);
j ^= (1 << i);
if (j) {
printf(" ");
}
}
}
printf(")\n");
}
#ifdef __UNIX__
setvbuf(stdout, 0, _IONBF, 0);
#endif
/*
* handle each source file on the command line
*/
while (argc--) {
process(*argv++);
}
return 0;
}
/*
* vim: tabstop=4 shiftwidth=4 expandtab:
*/