17 #define MAXDEFS (1 << 12)
18 #define MACROLEN (1 << 10)
19 #define MAXARGS (1 << 5)
20 #define NBUCKET (MAXDEFS << 1)
25 char args
[MAXARGS
][NAMELEN
];
30 /* macro hash table */
31 static struct tab mtab
;
33 #define MAXBUFS (1 << 5)
49 char args
[MAXARGS
][MACROLEN
]; /* arguments passed to a macro */
51 int arg_buf
; /* the bufs index of the owning macro */
57 write(2, msg
, strlen(msg
));
61 static void buf_new(int type
, char *dat
, int dlen
)
64 bufs
[nbufs
- 1].buf
= buf
;
65 bufs
[nbufs
- 1].cur
= cur
;
66 bufs
[nbufs
- 1].len
= len
;
69 die("nomem: MAXBUFS reached!\n");
74 bufs
[nbufs
- 1].type
= type
;
77 static void buf_file(char *path
, char *dat
, int dlen
)
79 buf_new(BUF_FILE
, dat
, dlen
);
80 strcpy(bufs
[nbufs
- 1].path
, path
? path
: "");
83 static void buf_macro(struct macro
*m
)
85 buf_new(BUF_MACRO
, m
->def
, strlen(m
->def
));
86 bufs
[nbufs
- 1].macro
= m
;
89 static void buf_arg(char *arg
, int mbuf
)
91 buf_new(BUF_ARG
, arg
, strlen(arg
));
92 bufs
[nbufs
- 1].arg_buf
= mbuf
;
95 static void buf_pop(void)
99 cur
= bufs
[nbufs
- 1].cur
;
100 len
= bufs
[nbufs
- 1].len
;
101 buf
= bufs
[nbufs
- 1].buf
;
105 static int buf_iseval(void)
108 for (i
= nbufs
- 1; i
>= 0; i
--)
109 if (bufs
[i
].type
== BUF_EVAL
)
114 static size_t file_size(int fd
)
122 static int include_file(char *path
)
124 int fd
= open(path
, O_RDONLY
);
130 size
= file_size(fd
) + 1;
132 while ((n
= read(fd
, dat
+ nr
, size
- nr
)) > 0)
136 buf_file(path
, dat
, nr
);
140 int cpp_init(char *path
)
142 cpp_define("__STDC__", "");
143 cpp_define("__i386__", "");
144 cpp_define("__linux__", "");
145 return include_file(path
);
148 static void jumpws(void)
150 while (cur
< len
&& isspace(buf
[cur
]))
154 static void read_word(char *dst
)
157 while (cur
< len
&& (isalnum(buf
[cur
]) || buf
[cur
] == '_'))
162 static void jumpcomment(void)
164 while (++cur
< len
) {
165 if (buf
[cur
] == '*' && buf
[cur
+ 1] == '/') {
172 static void read_tilleol(char *dst
)
174 while (cur
< len
&& isspace(buf
[cur
]) && buf
[cur
] != '\n')
176 while (cur
< len
&& buf
[cur
] != '\n') {
177 if (buf
[cur
] == '\\' && buf
[cur
+ 1] == '\n')
179 else if (buf
[cur
] == '/' && buf
[cur
+ 1] == '*')
187 static char *putstr(char *d
, char *s
)
195 #define MAXLOCS (1 << 10)
197 static char *locs
[MAXLOCS
] = {"/usr/include"};
198 static int nlocs
= 1;
200 void cpp_addpath(char *s
)
205 static int include_find(char *name
, int std
)
208 for (i
= std
? nlocs
- 1 : nlocs
; i
>= 0; i
--) {
213 s
= putstr(s
, locs
[i
]);
217 if (!include_file(path
))
223 static void jumpstr(void)
225 if (buf
[cur
] == '\'') {
226 while (cur
< len
&& buf
[++cur
] != '\'')
227 if (buf
[cur
] == '\\')
232 if (buf
[cur
] == '"') {
233 while (cur
< len
&& buf
[++cur
] != '"')
234 if (buf
[cur
] == '\\')
241 static void readarg(char *s
)
245 while (cur
< len
&& (depth
|| buf
[cur
] != ',' && buf
[cur
] != ')')) {
264 if (buf
[cur
] == '/' && buf
[cur
+ 1] == '*')
271 memcpy(s
, buf
+ beg
, cur
- beg
);
276 static int macro_find(char *name
)
278 char *n
= tab_get(&mtab
, name
);
281 return container(n
, struct macro
, name
) - macros
;
284 static void macro_undef(char *name
)
286 int i
= macro_find(name
);
288 tab_del(&mtab
, macros
[i
].name
);
291 static int macro_new(char *name
)
293 int i
= macro_find(name
);
296 if (nmacros
>= MAXDEFS
)
297 die("nomem: MAXDEFS reached!\n");
299 strcpy(macros
[i
].name
, name
);
300 tab_add(&mtab
, macros
[i
].name
);
304 static void macro_define(void)
309 d
= ¯os
[macro_new(name
)];
312 if (buf
[cur
] == '(') {
315 while (cur
< len
&& buf
[cur
] != ')') {
316 readarg(d
->args
[d
->nargs
++]);
318 if (buf
[cur
++] != ',')
324 read_tilleol(d
->def
);
327 int cpp_read(char *buf
);
329 static char ebuf
[BUFSIZE
];
333 static long evalexpr(void);
335 static int cpp_eval(void)
339 char evalbuf
[BUFSIZE
];
340 read_tilleol(evalbuf
);
341 buf_new(BUF_EVAL
, evalbuf
, strlen(evalbuf
));
345 while (bufid
< nbufs
|| (bufid
== nbufs
&& cur
< len
))
346 elen
+= cpp_read(ebuf
+ elen
);
352 static void jumpifs(int jumpelse
)
356 if (buf
[cur
] == '#') {
360 if (!strcmp("else", cmd
))
361 if (!depth
&& !jumpelse
)
363 if (!strcmp("elif", cmd
))
364 if (!depth
&& !jumpelse
&& cpp_eval())
366 if (!strcmp("endif", cmd
)) {
372 if (!strcmp("ifdef", cmd
) || !strcmp("ifndef", cmd
) ||
377 if (buf
[cur
] == '/' && buf
[cur
+ 1] == '*') {
381 if (buf
[cur
] == '\'' || buf
[cur
] == '"') {
389 static int cpp_cmd(void)
394 if (!strcmp("define", cmd
)) {
398 if (!strcmp("undef", cmd
)) {
404 if (!strcmp("ifdef", cmd
) || !strcmp("ifndef", cmd
) ||
405 !strcmp("if", cmd
)) {
409 int not = cmd
[2] == 'n';
411 matched
= not ? macro_find(name
) < 0 :
412 macro_find(name
) >= 0;
414 matched
= cpp_eval();
420 if (!strcmp("else", cmd
) || !strcmp("elif", cmd
)) {
424 if (!strcmp("endif", cmd
))
426 if (!strcmp("include", cmd
)) {
431 e
= strchr(buf
+ cur
+ 1, buf
[cur
] == '"' ? '"' : '>');
432 memcpy(file
, s
, e
- s
);
435 if (include_find(file
, *e
== '>') == -1)
436 die("cannot include file\n");
442 static int macro_arg(struct macro
*m
, char *arg
)
445 for (i
= 0; i
< m
->nargs
; i
++)
446 if (!strcmp(arg
, m
->args
[i
]))
451 static int buf_arg_find(char *name
)
454 for (i
= nbufs
- 1; i
>= 0; i
--) {
455 struct buf
*mbuf
= &bufs
[i
];
456 struct macro
*m
= mbuf
->macro
;
457 if (mbuf
->type
== BUF_MACRO
&& macro_arg(m
, name
) >= 0)
459 if (mbuf
->type
== BUF_ARG
)
465 static void macro_expand(void)
471 if ((mbuf
= buf_arg_find(name
)) >= 0) {
472 int arg
= macro_arg(bufs
[mbuf
].macro
, name
);
473 char *dat
= bufs
[mbuf
].args
[arg
];
477 m
= ¯os
[macro_find(name
)];
483 if (buf
[cur
] == '(') {
485 struct buf
*mbuf
= &bufs
[nbufs
];
488 while (cur
< len
&& buf
[cur
] != ')') {
489 readarg(mbuf
->args
[i
++]);
497 mbuf
->args
[i
++][0] = '\0';
503 static int buf_expanding(char *macro
)
506 for (i
= nbufs
- 1; i
>= 0; i
--) {
507 if (bufs
[i
].type
== BUF_ARG
)
509 if (bufs
[i
].type
== BUF_MACRO
&&
510 !strcmp(macro
, bufs
[i
].macro
->name
))
516 static int expandable(char *word
)
518 if (buf_arg_find(word
) >= 0)
520 return !buf_expanding(word
) && macro_find(word
) != -1;
523 void cpp_define(char *name
, char *def
)
525 char tmp_buf
[MACROLEN
];
530 buf_new(BUF_TEMP
, tmp_buf
, s
- tmp_buf
);
535 static int seen_macro
;
540 int cpp_read(char *s
)
548 struct buf
*cbuf
= &bufs
[nbufs
- 1];
551 if (cbuf
->type
& BUF_FILE
)
562 if (buf
[cur
] == '/' && buf
[cur
+ 1] == '*') {
566 if (buf
[cur
] == '\'' || buf
[cur
] == '"') {
570 if (isalpha(buf
[cur
]) || buf
[cur
] == '_') {
573 if (expandable(word
)) {
578 if (buf_iseval() && !strcmp("defined", word
)) {
581 if (buf
[cur
] == '(') {
595 memcpy(s
, buf
+ old
, cur
- old
);
598 hunk_off
+= hunk_len
;
599 hunk_len
= cur
- old
;
604 /* preprocessor constant expression evaluation */
606 static char etok
[NAMELEN
];
609 static char *tok2
[] = {
610 "<<", ">>", "&&", "||", "==", "!=", "<=", ">="
613 static int eval_tok(void)
617 while (ecur
< elen
) {
618 while (ecur
< elen
&& isspace(ebuf
[ecur
]))
620 if (ebuf
[ecur
] == '/' && ebuf
[ecur
+ 1] == '*') {
621 while (ecur
< elen
&& (ebuf
[ecur
- 2] != '*' ||
622 ebuf
[ecur
- 1] != '/'))
630 if (isalpha(ebuf
[ecur
]) || ebuf
[ecur
] == '_') {
631 while (isalnum(ebuf
[ecur
]) || ebuf
[ecur
] == '_')
636 if (isdigit(ebuf
[ecur
])) {
637 while (isdigit(ebuf
[ecur
]))
639 while (tolower(ebuf
[ecur
]) == 'u' || tolower(ebuf
[ecur
]) == 'l')
643 for (i
= 0; i
< ARRAY_SIZE(tok2
); i
++)
644 if (TOK2(tok2
[i
]) == TOK2(ebuf
+ ecur
)) {
645 int ret
= TOK2(tok2
[i
]);
652 static int eval_see(void)
659 static int eval_get(void)
669 static long eval_num(void)
674 static int eval_jmp(int tok
)
676 if (eval_see() == tok
) {
683 static void eval_expect(int tok
)
688 static char *eval_id(void)
693 static long evalcexpr(void);
695 static long evalatom(void)
697 if (!eval_jmp(TOK_NUM
))
699 if (!eval_jmp(TOK_NAME
)) {
700 int parens
= !eval_jmp('(');
702 eval_expect(TOK_NAME
);
703 ret
= macro_find(eval_id()) >= 0;
708 if (!eval_jmp('(')) {
709 long ret
= evalcexpr();
716 static long evalpre(void)
727 static long evalmul(void)
729 long ret
= evalpre();
731 if (!eval_jmp('*')) {
735 if (!eval_jmp('/')) {
739 if (!eval_jmp('%')) {
748 static long evaladd(void)
750 long ret
= evalmul();
752 if (!eval_jmp('+')) {
756 if (!eval_jmp('-')) {
765 static long evalshift(void)
767 long ret
= evaladd();
769 if (!eval_jmp(TOK2("<<"))) {
773 if (!eval_jmp(TOK2(">>"))) {
782 static long evalcmp(void)
784 long ret
= evalshift();
786 if (!eval_jmp('<')) {
787 ret
= ret
< evalshift();
790 if (!eval_jmp('>')) {
791 ret
= ret
> evalshift();
794 if (!eval_jmp(TOK2("<="))) {
795 ret
= ret
<= evalshift();
798 if (!eval_jmp(TOK2(">="))) {
799 ret
= ret
>= evalshift();
807 static long evaleq(void)
809 long ret
= evalcmp();
811 if (!eval_jmp(TOK2("=="))) {
812 ret
= ret
== evalcmp();
815 if (!eval_jmp(TOK2("!="))) {
816 ret
= ret
!= evalcmp();
824 static long evalbitand(void)
827 while (!eval_jmp('&'))
832 static long evalxor(void)
834 long ret
= evalbitand();
835 while (!eval_jmp('^'))
840 static long evalbitor(void)
842 long ret
= evalxor();
843 while (!eval_jmp('|'))
848 static long evaland(void)
850 long ret
= evalbitor();
851 while (!eval_jmp(TOK2("&&")))
852 ret
= ret
&& evalbitor();
856 static long evalor(void)
858 long ret
= evaland();
859 while (!eval_jmp(TOK2("||")))
860 ret
= ret
|| evaland();
864 static long evalcexpr(void)
871 while (eval_get() != ':')
876 static long evalexpr(void)
882 static int buf_loc(char *s
, int off
)
886 while ((s
= strchr(s
, '\n')) && s
< e
) {
893 int cpp_loc(char *s
, long addr
)
897 for (i
= nbufs
- 1; i
> 0; i
--)
898 if (bufs
[i
].type
== BUF_FILE
)
900 if (addr
>= hunk_off
&& i
== nbufs
- 1)
901 line
= buf_loc(buf
, (cur
- hunk_len
) + (addr
- hunk_off
));
903 line
= buf_loc(bufs
[i
].buf
, bufs
[i
].cur
);
904 sprintf(s
, "%s:%d: ", bufs
[i
].path
, line
);