12 #define MAXDEFS (1 << 10)
13 #define MACROLEN (1 << 10)
15 static struct define
{
21 #define MAXBUFS (1 << 3)
30 static void buf_new(void)
32 bufs
[nbufs
- 1].cur
= cur
;
33 bufs
[nbufs
- 1].len
= len
;
37 buf
= bufs
[nbufs
- 1].buf
;
40 static void buf_pop(void)
43 cur
= bufs
[nbufs
- 1].cur
;
44 len
= bufs
[nbufs
- 1].len
;
45 buf
= bufs
[nbufs
- 1].buf
;
48 static void include(int fd
)
52 while ((n
= read(fd
, buf
+ len
, BUFSIZE
- len
)) > 0)
61 static void jumpws(void)
63 while (cur
< len
&& isspace(buf
[cur
]))
67 static void read_word(char *dst
)
70 while (cur
< len
&& isalnum(buf
[cur
]) || buf
[cur
] == '_')
75 static void read_tilleol(char *dst
)
77 while (cur
< len
&& buf
[cur
] != '\n') {
86 static char *putstr(char *d
, char *s
)
94 #define MAXLOCS (1 << 10)
96 static char *locs
[MAXLOCS
] = {".", NULL
, "/usr/include"};
99 void cpp_addpath(char *s
)
104 static int include_find(char *name
, int std
)
108 for (i
= std
? 1 : 0; i
< nlocs
; i
++) {
111 if (!locs
[i
] && *name
!= '/')
113 s
= putstr(path
, locs
[i
]);
117 fd
= open(path
, O_RDONLY
);
124 static void cpp_cmd(void)
129 if (!strcmp("define", cmd
)) {
130 struct define
*d
= &defines
[ndefines
++];
132 read_tilleol(d
->def
);
135 if (!strcmp("include", cmd
)) {
141 e
= strchr(buf
+ cur
+ 1, buf
[cur
] == '"' ? '"' : '>');
142 memcpy(file
, s
, e
- s
);
145 fd
= include_find(file
, *e
== '>');
154 static int macro_find(char *name
)
157 for (i
= 0; i
< ndefines
; i
++)
158 if (!strcmp(name
, defines
[i
].name
))
163 static void macro_expand(void)
169 m
= &defines
[macro_find(name
)];
171 len
= strlen(m
->def
);
174 static void jumpstr(void)
176 if (buf
[cur
] == '\'') {
177 while (cur
< len
&& buf
[++cur
] != '\'')
178 if (buf
[cur
] == '\\')
183 if (buf
[cur
] == '"') {
184 while (cur
< len
&& buf
[++cur
] != '"')
185 if (buf
[cur
] == '\\')
192 static void jumpcomment(void)
194 while (++cur
< len
) {
195 if (buf
[cur
] == '*' && buf
[cur
+ 1] == '/') {
202 static int definedword
;
204 int cpp_read(char *s
)
217 if (buf
[cur
] == '#') {
224 if (buf
[cur
] == '/' && buf
[cur
+ 1] == '*')
226 if (strchr("'\"", buf
[cur
]))
228 if (isalpha(buf
[cur
]) || buf
[cur
] == '_') {
231 if (macro_find(word
) != -1) {
239 memcpy(s
, buf
+ old
, cur
- old
);