12 #define MAXDEFS (1 << 10)
13 #define MACROLEN (1 << 10)
14 #define MAXARGS (1 << 5)
16 static struct define
{
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 void macro_define(void)
194 struct define
*d
= &defines
[ndefines
++];
197 if (buf
[cur
++] == '(') {
199 while (cur
< len
&& buf
[cur
] != ')') {
200 readarg(d
->args
[d
->nargs
++]);
210 read_tilleol(d
->def
);
213 static void cpp_cmd(void)
218 if (!strcmp("define", cmd
)) {
222 if (!strcmp("include", cmd
)) {
228 e
= strchr(buf
+ cur
+ 1, buf
[cur
] == '"' ? '"' : '>');
229 memcpy(file
, s
, e
- s
);
232 fd
= include_find(file
, *e
== '>');
241 static int macro_find(char *name
)
244 for (i
= 0; i
< ndefines
; i
++)
245 if (!strcmp(name
, defines
[i
].name
))
250 static int macro_arg(struct define
*m
, char *arg
)
253 for (i
= 0; i
< m
->nargs
; i
++)
254 if (!strcmp(arg
, m
->args
[i
]))
259 static void macro_expand(void)
262 char args
[MAXARGS
][MACROLEN
];
269 m
= &defines
[macro_find(name
)];
273 len
= strlen(m
->def
);
276 if (buf
[cur
] == '(') {
279 while (cur
< len
&& buf
[cur
] != ')') {
280 readarg(args
[nargs
++]);
292 len
= strlen(m
->def
);
295 if (buf
[cur
] == '/' && buf
[cur
+ 1] == '*') {
299 if (strchr("'\"", buf
[cur
])) {
303 if (isalpha(buf
[cur
]) || buf
[cur
] == '_') {
307 if ((arg
= macro_arg(m
, word
)) != -1) {
308 int len
= cur
- beg
- strlen(word
);
309 char *argstr
= arg
> nargs
? "" : args
[arg
];
310 int arglen
= strlen(argstr
);
311 memcpy(dst
+ dstlen
, buf
+ beg
, len
);
313 memcpy(dst
+ dstlen
, argstr
, arglen
);
321 memcpy(dst
+ dstlen
, buf
+ beg
, len
- beg
);
329 void cpp_define(char *name
, char *def
)
342 static int definedword
;
344 int cpp_read(char *s
)
357 if (buf
[cur
] == '#') {
364 if (buf
[cur
] == '/' && buf
[cur
+ 1] == '*') {
368 if (buf
[cur
] == '\'' || buf
[cur
] == '"') {
372 if (isalpha(buf
[cur
]) || buf
[cur
] == '_') {
375 if (macro_find(word
) != -1) {
384 memcpy(s
, buf
+ old
, cur
- old
);