12 #define MAXDEFS (1 << 10)
13 #define MACROLEN (1 << 10)
14 #define MAXARGS (1 << 5)
19 char args
[MAXARGS
][NAMELEN
];
25 #define MAXBUFS (1 << 3)
34 static void buf_new(void)
37 bufs
[nbufs
- 1].cur
= cur
;
38 bufs
[nbufs
- 1].len
= len
;
43 buf
= bufs
[nbufs
- 1].buf
;
46 static void buf_pop(void)
50 cur
= bufs
[nbufs
- 1].cur
;
51 len
= bufs
[nbufs
- 1].len
;
52 buf
= bufs
[nbufs
- 1].buf
;
56 static void include(int fd
)
60 while ((n
= read(fd
, buf
+ len
, BUFSIZE
- len
)) > 0)
69 static void jumpws(void)
71 while (cur
< len
&& isspace(buf
[cur
]))
75 static void read_word(char *dst
)
78 while (cur
< len
&& isalnum(buf
[cur
]) || buf
[cur
] == '_')
83 static void read_tilleol(char *dst
)
85 while (cur
< len
&& buf
[cur
] != '\n')
93 static char *putstr(char *d
, char *s
)
101 #define MAXLOCS (1 << 10)
103 static char *locs
[MAXLOCS
] = {".", NULL
, "/usr/include"};
104 static int nlocs
= 3;
106 void cpp_addpath(char *s
)
111 static int include_find(char *name
, int std
)
115 for (i
= std
? 1 : 0; i
< nlocs
; i
++) {
118 if (!locs
[i
] && *name
!= '/')
120 s
= putstr(path
, locs
[i
]);
124 fd
= open(path
, O_RDONLY
);
131 static void jumpstr(void)
133 if (buf
[cur
] == '\'') {
134 while (cur
< len
&& buf
[++cur
] != '\'')
135 if (buf
[cur
] == '\\')
140 if (buf
[cur
] == '"') {
141 while (cur
< len
&& buf
[++cur
] != '"')
142 if (buf
[cur
] == '\\')
149 static void jumpcomment(void)
151 while (++cur
< len
) {
152 if (buf
[cur
] == '*' && buf
[cur
+ 1] == '/') {
159 static void readarg(char *s
)
163 while (cur
< len
&& (depth
|| buf
[cur
] != ',' && buf
[cur
] != ')')) {
182 if (buf
[cur
] == '/' && buf
[cur
+ 1] == '*')
188 memcpy(s
, buf
+ beg
, cur
- beg
);
192 static int macro_find(char *name
)
195 for (i
= 0; i
< nmacros
; i
++)
196 if (!strcmp(name
, macros
[i
].name
))
201 static int macro_new(char *name
)
204 for (i
= 0; i
< nmacros
; i
++) {
205 if (!strcmp(name
, macros
[i
].name
))
207 if (!*macros
[i
].name
) {
208 strcpy(macros
[i
].name
, name
);
212 strcpy(macros
[nmacros
++].name
, name
);
216 static void macro_define(void)
221 d
= ¯os
[macro_new(name
)];
223 if (buf
[cur
++] == '(') {
225 while (cur
< len
&& buf
[cur
] != ')') {
226 readarg(d
->args
[d
->nargs
++]);
236 read_tilleol(d
->def
);
239 static void cpp_cmd(void)
244 if (!strcmp("define", cmd
)) {
248 if (!strcmp("undef", cmd
)) {
252 idx
= macro_find(name
);
254 strcpy(macros
[idx
].name
, "");
257 if (!strcmp("include", cmd
)) {
263 e
= strchr(buf
+ cur
+ 1, buf
[cur
] == '"' ? '"' : '>');
264 memcpy(file
, s
, e
- s
);
267 fd
= include_find(file
, *e
== '>');
276 static int macro_arg(struct macro
*m
, char *arg
)
279 for (i
= 0; i
< m
->nargs
; i
++)
280 if (!strcmp(arg
, m
->args
[i
]))
285 static void macro_expand(void)
288 char args
[MAXARGS
][MACROLEN
];
295 m
= ¯os
[macro_find(name
)];
299 len
= strlen(m
->def
);
302 if (buf
[cur
] == '(') {
305 while (cur
< len
&& buf
[cur
] != ')') {
306 readarg(args
[nargs
++]);
318 len
= strlen(m
->def
);
321 if (buf
[cur
] == '/' && buf
[cur
+ 1] == '*') {
325 if (strchr("'\"", buf
[cur
])) {
329 if (isalpha(buf
[cur
]) || buf
[cur
] == '_') {
333 if ((arg
= macro_arg(m
, word
)) != -1) {
334 int len
= cur
- beg
- strlen(word
);
335 char *argstr
= arg
> nargs
? "" : args
[arg
];
336 int arglen
= strlen(argstr
);
337 memcpy(dst
+ dstlen
, buf
+ beg
, len
);
339 memcpy(dst
+ dstlen
, argstr
, arglen
);
347 memcpy(dst
+ dstlen
, buf
+ beg
, len
- beg
);
355 void cpp_define(char *name
, char *def
)
368 static int definedword
;
370 int cpp_read(char *s
)
383 if (buf
[cur
] == '#') {
390 if (buf
[cur
] == '/' && buf
[cur
+ 1] == '*') {
394 if (buf
[cur
] == '\'' || buf
[cur
] == '"') {
398 if (isalpha(buf
[cur
]) || buf
[cur
] == '_') {
401 if (macro_find(word
) != -1) {
410 memcpy(s
, buf
+ old
, cur
- old
);