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
},
44 static char *tok3
[] = {
45 "<<", ">>", "++", "--", "<<=", ">>=", "...", "+=", "-=", "*=", "/=",
46 "%=", "|=", "&=", "^=", "&&", "||", "==", "!=", "<=", ">=", "->", "/*"
49 static int get_tok3(int num
)
52 for (i
= 0; i
< ARRAY_SIZE(tok3
); i
++)
53 if (num
== TOK3(tok3
[i
]))
58 static char *esc_code
= "abefnrtv";
59 static char *esc
= "\a\b\e\f\n\r\t\v";
61 static int esc_char(int *c
, char *s
)
67 if (strchr(esc_code
, s
[1])) {
68 *c
= esc
[strchr(esc_code
, s
[1]) - esc_code
];
77 if (buf
[cur
] == '0' && buf
[cur
+ 1] == 'x') {
80 while (isalnum(buf
[cur
])) {
83 if (c
>= '0' && c
<= '9')
86 result
|= 10 + tolower(c
) - 'a';
91 if (isdigit(buf
[cur
])) {
93 while (isdigit(buf
[cur
])) {
95 result
+= buf
[cur
++] - '0';
99 if (buf
[cur
] == '\'') {
101 cur
+= 2 + esc_char(&ret
, buf
+ cur
+ 1);
107 int tok_str(char *out
)
113 while (r
< e
&& *r
!= '"') {
116 r
+= esc_char(&c
, r
);
127 static int id_char(int c
)
129 return isalnum(c
) || c
== '_';
132 static int skipws(void)
135 while (cur
< len
&& isspace(buf
[cur
]))
139 if (TOK2(buf
+ cur
) != TOK2("/*"))
141 while (++cur
< len
) {
142 if (buf
[cur
] == '*' && buf
[cur
+ 1] == '/') {
163 if (isdigit(buf
[cur
]) || buf
[cur
] == '\'')
165 if (id_char(buf
[cur
])) {
168 while (cur
< len
&& id_char(buf
[cur
]))
171 for (i
= 0; i
< ARRAY_SIZE(kwds
); i
++)
172 if (!strcmp(kwds
[i
].name
, name
))
176 if ((num
= get_tok3(TOK3(buf
+ cur
)))) {
180 if ((num
= get_tok3(TOK2(buf
+ cur
)))) {
184 if (strchr(";,{}()[]<>*&!=+-/%?:|^~.", buf
[cur
]))
196 void tok_init(int fd
)
199 while ((n
= read(fd
, buf
+ len
, sizeof(buf
) - len
)) > 0)