20 char name
[NAMELEN
]; /* macro name */
21 char def
[MDEFLEN
]; /* macro definition */
22 char args
[NARGS
][NAMELEN
];
23 int nargs
; /* number of arguments */
24 int isfunc
; /* macro is a function */
25 int undef
; /* macro is removed */
27 static int mcount
= 1; /* number of macros */
28 static int mhead
[256]; /* macro hash table heads */
29 static int mnext
[NDEFS
]; /* macro hash table next entries */
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 /* find a macro; if undef is nonzero, search #undef-ed macros too */
274 static int macro_find(char *name
, int undef
)
276 int i
= mhead
[(unsigned char) name
[0]];
278 if (!strcmp(name
, macros
[i
].name
))
279 if (!macros
[i
].undef
|| undef
)
286 static void macro_undef(char *name
)
288 int i
= macro_find(name
, 0);
293 static int macro_new(char *name
)
295 int i
= macro_find(name
, 1);
299 die("nomem: NDEFS reached!\n");
301 strcpy(macros
[i
].name
, name
);
302 mnext
[i
] = mhead
[(unsigned char) name
[0]];
303 mhead
[(unsigned char) name
[0]] = i
;
307 static void macro_define(void)
312 d
= ¯os
[macro_new(name
)];
315 if (buf
[cur
] == '(') {
318 while (cur
< len
&& buf
[cur
] != ')') {
319 readarg(d
->args
[d
->nargs
++]);
329 read_tilleol(d
->def
);
332 static char ebuf
[MARGLEN
];
336 static long evalexpr(void);
338 static int cpp_eval(void)
340 char evalbuf
[MARGLEN
];
344 read_tilleol(evalbuf
);
345 buf_new(BUF_EVAL
, evalbuf
, strlen(evalbuf
));
348 old_limit
= bufs_limit
;
350 while (!cpp_read(&cbuf
, &clen
)) {
351 memcpy(ebuf
+ elen
, cbuf
, clen
);
354 bufs_limit
= old_limit
;
360 static void jumpifs(int jumpelse
)
364 if (buf
[cur
] == '#') {
368 if (!strcmp("else", cmd
))
369 if (!depth
&& !jumpelse
)
371 if (!strcmp("elif", cmd
))
372 if (!depth
&& !jumpelse
&& cpp_eval())
374 if (!strcmp("endif", cmd
)) {
380 if (!strcmp("ifdef", cmd
) || !strcmp("ifndef", cmd
) ||
393 static int cpp_cmd(void)
398 if (!strcmp("define", cmd
)) {
402 if (!strcmp("undef", cmd
)) {
408 if (!strcmp("ifdef", cmd
) || !strcmp("ifndef", cmd
) ||
409 !strcmp("if", cmd
)) {
413 int not = cmd
[2] == 'n';
415 matched
= not ? macro_find(name
, 0) < 0 :
416 macro_find(name
, 0) >= 0;
418 matched
= cpp_eval();
424 if (!strcmp("else", cmd
) || !strcmp("elif", cmd
)) {
428 if (!strcmp("endif", cmd
))
430 if (!strcmp("include", cmd
)) {
435 e
= strchr(buf
+ cur
+ 1, buf
[cur
] == '"' ? '"' : '>');
436 memcpy(file
, s
, e
- s
);
439 if (include_find(file
, *e
== '>') == -1)
440 err("cannot include <%s>\n", file
);
446 static int macro_arg(struct macro
*m
, char *arg
)
449 for (i
= 0; i
< m
->nargs
; i
++)
450 if (!strcmp(arg
, m
->args
[i
]))
455 static int buf_arg_find(char *name
)
458 for (i
= nbufs
- 1; i
>= 0; i
--) {
459 struct buf
*mbuf
= &bufs
[i
];
460 struct macro
*m
= mbuf
->macro
;
461 if (mbuf
->type
== BUF_MACRO
&& macro_arg(m
, name
) >= 0)
463 if (mbuf
->type
== BUF_ARG
)
469 static void macro_expand(char *name
)
473 if ((mbuf
= buf_arg_find(name
)) >= 0) {
474 int arg
= macro_arg(bufs
[mbuf
].macro
, name
);
475 char *dat
= bufs
[mbuf
].args
[arg
];
479 m
= ¯os
[macro_find(name
, 0)];
485 if (buf
[cur
] == '(') {
487 struct buf
*mbuf
= &bufs
[nbufs
];
490 while (cur
< len
&& buf
[cur
] != ')') {
491 readarg(mbuf
->args
[i
++]);
499 mbuf
->args
[i
++][0] = '\0';
505 static int buf_expanding(char *macro
)
508 for (i
= nbufs
- 1; i
>= 0; i
--) {
509 if (bufs
[i
].type
== BUF_ARG
)
511 if (bufs
[i
].type
== BUF_MACRO
&&
512 !strcmp(macro
, bufs
[i
].macro
->name
))
518 /* return 1 for plain macros and arguments and 2 for function macros */
519 static int expandable(char *word
)
522 if (buf_arg_find(word
) >= 0)
524 if (buf_expanding(word
))
526 i
= macro_find(word
, 0);
527 return i
>= 0 ? macros
[i
].isfunc
+ 1 : 0;
530 void cpp_define(char *name
, char *def
)
532 char tmp_buf
[MDEFLEN
];
533 sprintf(tmp_buf
, "%s\t%s", name
, def
);
534 buf_new(BUF_TEMP
, tmp_buf
, strlen(tmp_buf
));
539 static int seen_macro
; /* seen a macro; 2 if a function macro */
540 static char seen_name
[NAMELEN
]; /* the name of the last macro */
545 int cpp_read(char **obuf
, int *olen
)
551 if (seen_macro
== 1) {
552 macro_expand(seen_name
);
556 struct buf
*cbuf
= &bufs
[nbufs
- 1];
557 if (nbufs
< bufs_limit
+ 1)
559 if (cbuf
->type
== BUF_FILE
)
574 if (seen_macro
== 2) {
576 macro_expand(seen_name
);
583 if (isalnum(buf
[cur
]) || buf
[cur
] == '_') {
586 seen_macro
= expandable(word
);
588 strcpy(seen_name
, word
);
592 if (buf_iseval() && !strcmp("defined", word
)) {
595 if (buf
[cur
] == '(') {
609 /* macros are expanded later; ignoring their names */
610 end
= jump_name
? cur
- strlen(seen_name
) : cur
;
612 hunk_off
+= hunk_len
;
613 hunk_len
= end
- old
;
620 /* preprocessor constant expression evaluation */
622 static char etok
[NAMELEN
];
625 static char *tok2
[] = {
626 "<<", ">>", "&&", "||", "==", "!=", "<=", ">="
629 static int eval_tok(void)
633 while (ecur
< elen
) {
634 while (ecur
< elen
&& isspace(ebuf
[ecur
]))
636 if (ebuf
[ecur
] == '/' && ebuf
[ecur
+ 1] == '*') {
637 while (ecur
< elen
&& (ebuf
[ecur
- 2] != '*' ||
638 ebuf
[ecur
- 1] != '/'))
646 if (isalpha(ebuf
[ecur
]) || ebuf
[ecur
] == '_') {
647 while (isalnum(ebuf
[ecur
]) || ebuf
[ecur
] == '_')
652 if (isdigit(ebuf
[ecur
])) {
653 while (isdigit(ebuf
[ecur
]))
655 while (tolower(ebuf
[ecur
]) == 'u' || tolower(ebuf
[ecur
]) == 'l')
660 for (i
= 0; i
< LEN(tok2
); i
++)
661 if (TOK2(tok2
[i
]) == TOK2(ebuf
+ ecur
)) {
662 int ret
= TOK2(tok2
[i
]);
669 static int eval_see(void)
676 static int eval_get(void)
686 static long eval_num(void)
691 static int eval_jmp(int tok
)
693 if (eval_see() == tok
) {
700 static void eval_expect(int tok
)
705 static char *eval_id(void)
710 static long evalcexpr(void);
712 static long evalatom(void)
714 if (!eval_jmp(TOK_NUM
))
716 if (!eval_jmp(TOK_NAME
)) {
717 int parens
= !eval_jmp('(');
719 eval_expect(TOK_NAME
);
720 ret
= macro_find(eval_id(), 0) >= 0;
725 if (!eval_jmp('(')) {
726 long ret
= evalcexpr();
733 static long evalpre(void)
744 static long evalmul(void)
746 long ret
= evalpre();
748 if (!eval_jmp('*')) {
752 if (!eval_jmp('/')) {
756 if (!eval_jmp('%')) {
765 static long evaladd(void)
767 long ret
= evalmul();
769 if (!eval_jmp('+')) {
773 if (!eval_jmp('-')) {
782 static long evalshift(void)
784 long ret
= evaladd();
786 if (!eval_jmp(TOK2("<<"))) {
790 if (!eval_jmp(TOK2(">>"))) {
799 static long evalcmp(void)
801 long ret
= evalshift();
803 if (!eval_jmp('<')) {
804 ret
= ret
< evalshift();
807 if (!eval_jmp('>')) {
808 ret
= ret
> evalshift();
811 if (!eval_jmp(TOK2("<="))) {
812 ret
= ret
<= evalshift();
815 if (!eval_jmp(TOK2(">="))) {
816 ret
= ret
>= evalshift();
824 static long evaleq(void)
826 long ret
= evalcmp();
828 if (!eval_jmp(TOK2("=="))) {
829 ret
= ret
== evalcmp();
832 if (!eval_jmp(TOK2("!="))) {
833 ret
= ret
!= evalcmp();
841 static long evalbitand(void)
844 while (!eval_jmp('&'))
849 static long evalxor(void)
851 long ret
= evalbitand();
852 while (!eval_jmp('^'))
857 static long evalbitor(void)
859 long ret
= evalxor();
860 while (!eval_jmp('|'))
865 static long evaland(void)
867 long ret
= evalbitor();
868 while (!eval_jmp(TOK2("&&")))
869 ret
= ret
&& evalbitor();
873 static long evalor(void)
875 long ret
= evaland();
876 while (!eval_jmp(TOK2("||")))
877 ret
= ret
|| evaland();
881 static long evalcexpr(void)
888 while (eval_get() != ':')
893 static long evalexpr(void)
899 static int buf_loc(char *s
, int off
)
903 while ((s
= strchr(s
, '\n')) && s
< e
) {
910 char *cpp_loc(long addr
)
912 static char loc
[256];
915 for (i
= nbufs
- 1; i
> 0; i
--)
916 if (bufs
[i
].type
== BUF_FILE
)
918 if (addr
>= hunk_off
&& i
== nbufs
- 1)
919 line
= buf_loc(buf
, (cur
- hunk_len
) + (addr
- hunk_off
));
921 line
= buf_loc(bufs
[i
].buf
, bufs
[i
].cur
);
922 sprintf(loc
, "%s:%d", bufs
[i
].path
, line
);