7 static char buf
[BUFSIZE
];
10 static char name
[NAMELEN
];
18 {"static", TOK_STATIC
},
19 {"return", TOK_RETURN
},
20 {"unsigned", TOK_UNSIGNED
},
21 {"signed", TOK_SIGNED
},
26 {"struct", TOK_STRUCT
},
29 {"typedef", TOK_TYPEDEF
},
35 {"switch", TOK_SWITCH
},
37 {"sizeof", TOK_SIZEOF
},
39 {"continue", TOK_CONTINUE
},
40 {"default", TOK_DEFAULT
},
43 static char *tok3
[] = {
44 "<<", ">>", "++", "--", "<<=", ">>=", "...", "+=", "-=", "*=", "/=",
45 "%=", "|=", "&=", "^=", "&&", "||", "==", "!=", "<=", ">=", "->", "/*"
48 static int get_tok3(int num
)
51 for (i
= 0; i
< ARRAY_SIZE(tok3
); i
++)
52 if (num
== TOK3(tok3
[i
]))
57 static char *esc_code
= "abefnrtv";
58 static char *esc
= "\a\b\e\f\n\r\t\v";
60 static int esc_char(int *c
, char *s
)
66 if (strchr(esc_code
, s
[1])) {
67 *c
= esc
[strchr(esc_code
, s
[1]) - esc_code
];
76 if (buf
[cur
] == '0' && buf
[cur
+ 1] == 'x') {
79 while (isalnum(buf
[cur
])) {
82 if (c
>= '0' && c
<= '9')
85 result
|= 10 + tolower(c
) - 'a';
90 if (isdigit(buf
[cur
])) {
92 while (isdigit(buf
[cur
])) {
94 result
+= buf
[cur
++] - '0';
98 if (buf
[cur
] == '\'') {
100 cur
+= 2 + esc_char(&ret
, buf
+ cur
+ 1);
106 int tok_str(char *out
)
112 while (r
< e
&& *r
!= '"') {
115 r
+= esc_char(&c
, r
);
126 static int id_char(int c
)
128 return isalnum(c
) || c
== '_';
131 static int skipws(void)
134 while (cur
< len
&& isspace(buf
[cur
]))
138 if (TOK2(buf
+ cur
) != TOK2("/*"))
140 while (++cur
< len
) {
141 if (buf
[cur
] == '*' && buf
[cur
+ 1] == '/') {
162 if (isdigit(buf
[cur
]) || buf
[cur
] == '\'')
164 if (id_char(buf
[cur
])) {
167 while (cur
< len
&& id_char(buf
[cur
]))
170 for (i
= 0; i
< ARRAY_SIZE(kwds
); i
++)
171 if (!strcmp(kwds
[i
].name
, name
))
175 if ((num
= get_tok3(TOK3(buf
+ cur
)))) {
179 if ((num
= get_tok3(TOK2(buf
+ cur
)))) {
183 if (strchr(";,{}()[]<>*&!=+-/%?:|^~.", buf
[cur
]))
195 void tok_init(int fd
)
198 while ((n
= read(fd
, buf
+ len
, sizeof(buf
) - len
)) > 0)