23 char args
[NARGS
][NAMELEN
];
28 /* macro hash table */
29 static struct tab mtab
;
46 char args
[NARGS
][MARGLEN
]; /* arguments passed to a macro */
48 int arg_buf
; /* the bufs index of the owning macro */
51 static int bufs_limit
= 1; /* cpp_read() limit; useful in cpp_eval() */
53 void die(char *fmt
, ...)
58 vsprintf(msg
, fmt
, ap
);
60 write(2, msg
, strlen(msg
));
64 static void buf_new(int type
, char *dat
, int dlen
)
67 bufs
[nbufs
- 1].buf
= buf
;
68 bufs
[nbufs
- 1].cur
= cur
;
69 bufs
[nbufs
- 1].len
= len
;
72 die("nomem: NBUFS reached!\n");
77 bufs
[nbufs
- 1].type
= type
;
80 static void buf_file(char *path
, char *dat
, int dlen
)
82 buf_new(BUF_FILE
, dat
, dlen
);
83 strcpy(bufs
[nbufs
- 1].path
, path
? path
: "");
86 static void buf_macro(struct macro
*m
)
88 buf_new(BUF_MACRO
, m
->def
, strlen(m
->def
));
89 bufs
[nbufs
- 1].macro
= m
;
92 static void buf_arg(char *arg
, int mbuf
)
94 buf_new(BUF_ARG
, arg
, strlen(arg
));
95 bufs
[nbufs
- 1].arg_buf
= mbuf
;
98 static void buf_pop(void)
102 cur
= bufs
[nbufs
- 1].cur
;
103 len
= bufs
[nbufs
- 1].len
;
104 buf
= bufs
[nbufs
- 1].buf
;
108 static int buf_iseval(void)
111 for (i
= nbufs
- 1; i
>= 0; i
--)
112 if (bufs
[i
].type
== BUF_EVAL
)
117 static size_t file_size(int fd
)
125 static int include_file(char *path
)
127 int fd
= open(path
, O_RDONLY
);
133 size
= file_size(fd
) + 1;
135 while ((n
= read(fd
, dat
+ nr
, size
- nr
)) > 0)
139 buf_file(path
, dat
, nr
);
143 int cpp_init(char *path
)
145 return include_file(path
);
148 static int jumpws(void)
151 while (cur
< len
&& isspace(buf
[cur
]))
156 static void read_word(char *dst
)
159 while (cur
< len
&& (isalnum(buf
[cur
]) || buf
[cur
] == '_'))
164 static int jumpcomment(void)
166 if (buf
[cur
] == '/' && buf
[cur
+ 1] == '*') {
167 while (++cur
< len
) {
168 if (buf
[cur
] == '*' && buf
[cur
+ 1] == '/') {
174 if (buf
[cur
] == '/' && buf
[cur
+ 1] == '/') {
176 if (buf
[cur
] == '\n')
183 static int jumpstr(void)
185 if (buf
[cur
] == '\'') {
186 while (cur
< len
&& buf
[++cur
] != '\'')
187 if (buf
[cur
] == '\\')
192 if (buf
[cur
] == '"') {
193 while (cur
< len
&& buf
[++cur
] != '"')
194 if (buf
[cur
] == '\\')
202 static void read_tilleol(char *dst
)
204 while (cur
< len
&& isspace(buf
[cur
]) && buf
[cur
] != '\n')
206 while (cur
< len
&& buf
[cur
] != '\n') {
208 if (buf
[cur
] == '\\' && buf
[cur
+ 1] == '\n') {
213 memcpy(dst
, buf
+ last
, cur
- last
);
224 static char *locs
[NLOCS
] = {};
225 static int nlocs
= 0;
227 void cpp_addpath(char *s
)
232 static int include_find(char *name
, int std
)
235 for (i
= std
? nlocs
- 1 : nlocs
; i
>= 0; i
--) {
238 sprintf(path
, "%s/%s", locs
[i
], name
);
241 if (!include_file(path
))
247 static void readarg(char *s
)
251 while (cur
< len
&& (depth
|| (buf
[cur
] != ',' && buf
[cur
] != ')'))) {
252 if (!jumpstr() || !jumpcomment())
254 switch (buf
[cur
++]) {
268 memcpy(s
, buf
+ beg
, cur
- beg
);
273 static int macro_find(char *name
)
275 char *n
= tab_get(&mtab
, name
);
278 return container(n
, struct macro
, name
) - macros
;
281 static void macro_undef(char *name
)
283 int i
= macro_find(name
);
285 tab_del(&mtab
, macros
[i
].name
);
288 static int macro_new(char *name
)
290 int i
= macro_find(name
);
293 if (nmacros
>= NDEFS
)
294 die("nomem: NDEFS reached!\n");
296 strcpy(macros
[i
].name
, name
);
297 tab_add(&mtab
, macros
[i
].name
);
301 static void macro_define(void)
306 d
= ¯os
[macro_new(name
)];
309 if (buf
[cur
] == '(') {
312 while (cur
< len
&& buf
[cur
] != ')') {
313 readarg(d
->args
[d
->nargs
++]);
323 read_tilleol(d
->def
);
326 static char ebuf
[MARGLEN
];
330 static long evalexpr(void);
332 static int cpp_eval(void)
334 char evalbuf
[MARGLEN
];
338 read_tilleol(evalbuf
);
339 buf_new(BUF_EVAL
, evalbuf
, strlen(evalbuf
));
342 old_limit
= bufs_limit
;
344 while (!cpp_read(&cbuf
, &clen
)) {
345 memcpy(ebuf
+ elen
, cbuf
, clen
);
348 bufs_limit
= old_limit
;
354 static void jumpifs(int jumpelse
)
358 if (buf
[cur
] == '#') {
362 if (!strcmp("else", cmd
))
363 if (!depth
&& !jumpelse
)
365 if (!strcmp("elif", cmd
))
366 if (!depth
&& !jumpelse
&& cpp_eval())
368 if (!strcmp("endif", cmd
)) {
374 if (!strcmp("ifdef", cmd
) || !strcmp("ifndef", cmd
) ||
387 static int cpp_cmd(void)
392 if (!strcmp("define", cmd
)) {
396 if (!strcmp("undef", cmd
)) {
402 if (!strcmp("ifdef", cmd
) || !strcmp("ifndef", cmd
) ||
403 !strcmp("if", cmd
)) {
407 int not = cmd
[2] == 'n';
409 matched
= not ? macro_find(name
) < 0 :
410 macro_find(name
) >= 0;
412 matched
= cpp_eval();
418 if (!strcmp("else", cmd
) || !strcmp("elif", cmd
)) {
422 if (!strcmp("endif", cmd
))
424 if (!strcmp("include", cmd
)) {
429 e
= strchr(buf
+ cur
+ 1, buf
[cur
] == '"' ? '"' : '>');
430 memcpy(file
, s
, e
- s
);
433 if (include_find(file
, *e
== '>') == -1)
434 err("cannot include <%s>\n", file
);
440 static int macro_arg(struct macro
*m
, char *arg
)
443 for (i
= 0; i
< m
->nargs
; i
++)
444 if (!strcmp(arg
, m
->args
[i
]))
449 static int buf_arg_find(char *name
)
452 for (i
= nbufs
- 1; i
>= 0; i
--) {
453 struct buf
*mbuf
= &bufs
[i
];
454 struct macro
*m
= mbuf
->macro
;
455 if (mbuf
->type
== BUF_MACRO
&& macro_arg(m
, name
) >= 0)
457 if (mbuf
->type
== BUF_ARG
)
463 static void macro_expand(char *name
)
467 if ((mbuf
= buf_arg_find(name
)) >= 0) {
468 int arg
= macro_arg(bufs
[mbuf
].macro
, name
);
469 char *dat
= bufs
[mbuf
].args
[arg
];
473 m
= ¯os
[macro_find(name
)];
479 if (buf
[cur
] == '(') {
481 struct buf
*mbuf
= &bufs
[nbufs
];
484 while (cur
< len
&& buf
[cur
] != ')') {
485 readarg(mbuf
->args
[i
++]);
493 mbuf
->args
[i
++][0] = '\0';
499 static int buf_expanding(char *macro
)
502 for (i
= nbufs
- 1; i
>= 0; i
--) {
503 if (bufs
[i
].type
== BUF_ARG
)
505 if (bufs
[i
].type
== BUF_MACRO
&&
506 !strcmp(macro
, bufs
[i
].macro
->name
))
512 /* return 1 for plain macros and arguments and 2 for function macros */
513 static int expandable(char *word
)
516 if (buf_arg_find(word
) >= 0)
518 if (buf_expanding(word
))
520 i
= macro_find(word
);
521 return i
>= 0 ? macros
[i
].isfunc
+ 1 : 0;
524 void cpp_define(char *name
, char *def
)
526 char tmp_buf
[MDEFLEN
];
527 sprintf(tmp_buf
, "%s\t%s", name
, def
);
528 buf_new(BUF_TEMP
, tmp_buf
, strlen(tmp_buf
));
533 static int seen_macro
; /* seen a macro; 2 if a function macro */
534 static char seen_name
[NAMELEN
]; /* the name of the last macro */
539 int cpp_read(char **obuf
, int *olen
)
545 if (seen_macro
== 1) {
546 macro_expand(seen_name
);
550 struct buf
*cbuf
= &bufs
[nbufs
- 1];
551 if (nbufs
< bufs_limit
+ 1)
553 if (cbuf
->type
== BUF_FILE
)
568 if (seen_macro
== 2) {
570 macro_expand(seen_name
);
577 if (isalnum(buf
[cur
]) || buf
[cur
] == '_') {
580 seen_macro
= expandable(word
);
582 strcpy(seen_name
, word
);
586 if (buf_iseval() && !strcmp("defined", word
)) {
589 if (buf
[cur
] == '(') {
603 /* macros are expanded later; ignoring their names */
604 end
= jump_name
? cur
- strlen(seen_name
) : cur
;
606 hunk_off
+= hunk_len
;
607 hunk_len
= end
- old
;
614 /* preprocessor constant expression evaluation */
616 static char etok
[NAMELEN
];
619 static char *tok2
[] = {
620 "<<", ">>", "&&", "||", "==", "!=", "<=", ">="
623 static int eval_tok(void)
627 while (ecur
< elen
) {
628 while (ecur
< elen
&& isspace(ebuf
[ecur
]))
630 if (ebuf
[ecur
] == '/' && ebuf
[ecur
+ 1] == '*') {
631 while (ecur
< elen
&& (ebuf
[ecur
- 2] != '*' ||
632 ebuf
[ecur
- 1] != '/'))
640 if (isalpha(ebuf
[ecur
]) || ebuf
[ecur
] == '_') {
641 while (isalnum(ebuf
[ecur
]) || ebuf
[ecur
] == '_')
646 if (isdigit(ebuf
[ecur
])) {
647 while (isdigit(ebuf
[ecur
]))
649 while (tolower(ebuf
[ecur
]) == 'u' || tolower(ebuf
[ecur
]) == 'l')
654 for (i
= 0; i
< LEN(tok2
); i
++)
655 if (TOK2(tok2
[i
]) == TOK2(ebuf
+ ecur
)) {
656 int ret
= TOK2(tok2
[i
]);
663 static int eval_see(void)
670 static int eval_get(void)
680 static long eval_num(void)
685 static int eval_jmp(int tok
)
687 if (eval_see() == tok
) {
694 static void eval_expect(int tok
)
699 static char *eval_id(void)
704 static long evalcexpr(void);
706 static long evalatom(void)
708 if (!eval_jmp(TOK_NUM
))
710 if (!eval_jmp(TOK_NAME
)) {
711 int parens
= !eval_jmp('(');
713 eval_expect(TOK_NAME
);
714 ret
= macro_find(eval_id()) >= 0;
719 if (!eval_jmp('(')) {
720 long ret
= evalcexpr();
727 static long evalpre(void)
738 static long evalmul(void)
740 long ret
= evalpre();
742 if (!eval_jmp('*')) {
746 if (!eval_jmp('/')) {
750 if (!eval_jmp('%')) {
759 static long evaladd(void)
761 long ret
= evalmul();
763 if (!eval_jmp('+')) {
767 if (!eval_jmp('-')) {
776 static long evalshift(void)
778 long ret
= evaladd();
780 if (!eval_jmp(TOK2("<<"))) {
784 if (!eval_jmp(TOK2(">>"))) {
793 static long evalcmp(void)
795 long ret
= evalshift();
797 if (!eval_jmp('<')) {
798 ret
= ret
< evalshift();
801 if (!eval_jmp('>')) {
802 ret
= ret
> evalshift();
805 if (!eval_jmp(TOK2("<="))) {
806 ret
= ret
<= evalshift();
809 if (!eval_jmp(TOK2(">="))) {
810 ret
= ret
>= evalshift();
818 static long evaleq(void)
820 long ret
= evalcmp();
822 if (!eval_jmp(TOK2("=="))) {
823 ret
= ret
== evalcmp();
826 if (!eval_jmp(TOK2("!="))) {
827 ret
= ret
!= evalcmp();
835 static long evalbitand(void)
838 while (!eval_jmp('&'))
843 static long evalxor(void)
845 long ret
= evalbitand();
846 while (!eval_jmp('^'))
851 static long evalbitor(void)
853 long ret
= evalxor();
854 while (!eval_jmp('|'))
859 static long evaland(void)
861 long ret
= evalbitor();
862 while (!eval_jmp(TOK2("&&")))
863 ret
= ret
&& evalbitor();
867 static long evalor(void)
869 long ret
= evaland();
870 while (!eval_jmp(TOK2("||")))
871 ret
= ret
|| evaland();
875 static long evalcexpr(void)
882 while (eval_get() != ':')
887 static long evalexpr(void)
893 static int buf_loc(char *s
, int off
)
897 while ((s
= strchr(s
, '\n')) && s
< e
) {
904 char *cpp_loc(long addr
)
906 static char loc
[256];
909 for (i
= nbufs
- 1; i
> 0; i
--)
910 if (bufs
[i
].type
== BUF_FILE
)
912 if (addr
>= hunk_off
&& i
== nbufs
- 1)
913 line
= buf_loc(buf
, (cur
- hunk_len
) + (addr
- hunk_off
));
915 line
= buf_loc(bufs
[i
].buf
, bufs
[i
].cur
);
916 sprintf(loc
, "%s:%d", bufs
[i
].path
, line
);