7 static char buf
[BUFSIZE
];
10 static char name
[NAMELEN
];
19 {"static", TOK_STATIC
},
20 {"extern", TOK_EXTERN
},
21 {"return", TOK_RETURN
},
22 {"unsigned", TOK_UNSIGNED
},
23 {"signed", TOK_SIGNED
},
28 {"struct", TOK_STRUCT
},
31 {"typedef", TOK_TYPEDEF
},
37 {"switch", TOK_SWITCH
},
39 {"sizeof", TOK_SIZEOF
},
41 {"continue", TOK_CONTINUE
},
42 {"default", TOK_DEFAULT
},
46 static char *tok3
[] = {
47 "<<", ">>", "++", "--", "<<=", ">>=", "...", "+=", "-=", "*=", "/=",
48 "%=", "|=", "&=", "^=", "&&", "||", "==", "!=", "<=", ">=", "->", "/*"
51 static int get_tok3(int num
)
54 for (i
= 0; i
< ARRAY_SIZE(tok3
); i
++)
55 if (num
== TOK3(tok3
[i
]))
60 static char *esc_code
= "abefnrtv";
61 static char *esc
= "\a\b\e\f\n\r\t\v";
63 static int esc_char(int *c
, char *s
)
69 if (strchr(esc_code
, s
[1])) {
70 *c
= esc
[strchr(esc_code
, s
[1]) - esc_code
];
84 static void readnum(void)
86 if (buf
[cur
] == '0' && buf
[cur
+ 1] == 'x') {
89 while (isalnum(buf
[cur
])) {
92 if (c
>= '0' && c
<= '9')
95 result
|= 10 + tolower(c
) - 'a';
101 if (isdigit(buf
[cur
])) {
103 while (isdigit(buf
[cur
])) {
105 result
+= buf
[cur
++] - '0';
110 if (buf
[cur
] == '\'') {
112 cur
+= 2 + esc_char(&ret
, buf
+ cur
+ 1);
119 static char str
[BUFSIZE
];
122 int tok_str(char *buf
)
125 memcpy(buf
, str
, str_len
);
129 static int readstr(char *out
)
135 while (r
< e
&& *r
!= '"') {
138 r
+= esc_char(&c
, r
);
149 static int id_char(int c
)
151 return isalnum(c
) || c
== '_';
154 static int skipws(void)
157 while (cur
< len
&& isspace(buf
[cur
]))
161 if (TOK2(buf
+ cur
) != TOK2("/*"))
163 while (++cur
< len
) {
164 if (buf
[cur
] == '*' && buf
[cur
+ 1] == '/') {
184 if (buf
[cur
] == '"') {
186 while (buf
[cur
] == '"') {
187 str_len
+= readstr(str
+ str_len
);
194 if (isdigit(buf
[cur
]) || buf
[cur
] == '\'') {
198 if (id_char(buf
[cur
])) {
201 while (cur
< len
&& id_char(buf
[cur
]))
204 for (i
= 0; i
< ARRAY_SIZE(kwds
); i
++)
205 if (!strcmp(kwds
[i
].name
, name
))
209 if ((num
= get_tok3(TOK3(buf
+ cur
)))) {
213 if ((num
= get_tok3(TOK2(buf
+ cur
)))) {
217 if (strchr(";,{}()[]<>*&!=+-/%?:|^~.", buf
[cur
]))
229 void tok_init(int fd
)
232 while ((n
= read(fd
, buf
+ len
, sizeof(buf
) - len
)) > 0)
244 return next
== -1 ? cur
: pre
;
247 void tok_jump(long addr
)