35 #include <ctype.h> |
35 #include <ctype.h> |
36 |
36 |
37 #define INPUTBUF_SIZE 2048 |
37 #define INPUTBUF_SIZE 2048 |
38 #define WORDBUF_SIZE 16 |
38 #define WORDBUF_SIZE 16 |
39 |
39 |
|
40 #define istype(word, len) (word[len-2] == '_' && word[len-1] == 't') |
|
41 #define isdirective(word) (word[0] == '#') |
|
42 |
40 const char* keywords[] = { |
43 const char* keywords[] = { |
41 "auto", "break", "case", "char", "const", "continue", "default", "do", |
44 "auto", "break", "case", "char", "const", "continue", "default", "do", |
42 "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", |
45 "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", |
43 "long", "register", "return", "short", "signed", "sizeof", "static", "struct", |
46 "long", "register", "return", "short", "signed", "sizeof", "static", "struct", |
44 "switch", "typedef", "union", "unsigned", "void", "volatile", "while", NULL |
47 "switch", "typedef", "union", "unsigned", "void", "volatile", "while", NULL |
144 } |
147 } |
145 } |
148 } |
146 return 0; |
149 return 0; |
147 } |
150 } |
148 |
151 |
149 #define istype(word, len) (word[len-2] == '_' && word[len-1] == 't') |
|
150 |
|
151 void parseline(char *src, char *dest) { |
152 void parseline(char *src, char *dest) { |
152 size_t sp = 0, dp = 0; |
153 size_t sp = 0, dp = 0; |
153 /* indent */ |
154 /* indent */ |
154 while (isspace(src[sp])) { |
155 while (isspace(src[sp])) { |
155 dest[dp++] = src[sp++]; |
156 dest[dp++] = src[sp++]; |
156 } |
157 } |
157 char word[WORDBUF_SIZE]; |
158 char word[WORDBUF_SIZE]; |
158 memset(word, 0, WORDBUF_SIZE); |
159 memset(word, 0, WORDBUF_SIZE); |
159 size_t wp = 0; |
160 size_t wp = 0; |
160 int closespan; |
161 int isstring = 0, iscomment = 0; |
|
162 int isescaping = 0; |
161 for (char c = src[sp] ; c ; c=src[++sp]) { |
163 for (char c = src[sp] ; c ; c=src[++sp]) { |
162 if (!isalnum(c) && c != '_') { |
164 /* comments and strings */ |
163 /* interpret word int_t */ |
165 if (!isescaping && (c == '\'' || c == '\"')) { |
164 if (wp > 0 && wp < WORDBUF_SIZE) { |
166 isstring ^= 1; |
165 if (iskeyword(word)) { |
167 if (isstring) { |
166 memcpy(&(dest[dp]), "<span class=\"c2html-keyword\">", 29); |
168 memcpy(&(dest[dp]), "<span class=\"c2html-string\">", 28); |
167 dp += 29; |
169 dp += 28; |
168 closespan = 1; |
170 dp = writeescapedchar(dest, dp, c); |
169 } else if (istype(word, wp)) { |
171 } else { |
170 memcpy(&(dest[dp]), "<span class=\"c2html-type\">", 26); |
172 dp = writeescapedchar(dest, dp, c); |
171 dp += 26; |
173 memcpy(&(dest[dp]), "</span>", 7); |
172 closespan = 1; |
174 dp += 7; |
|
175 } |
|
176 } else { |
|
177 if (iscomment || isstring) { |
|
178 dp = writeescapedchar(dest, dp, c); |
|
179 } else if (!isalnum(c) && c != '_' && c != '#') { |
|
180 /* interpret word int_t */ |
|
181 if (wp > 0 && wp < WORDBUF_SIZE) { |
|
182 int closespan = 1; |
|
183 if (iskeyword(word)) { |
|
184 memcpy(&(dest[dp]), "<span class=\"c2html-keyword\">", 29); |
|
185 dp += 29; |
|
186 } else if (istype(word, wp)) { |
|
187 memcpy(&(dest[dp]), "<span class=\"c2html-type\">", 26); |
|
188 dp += 26; |
|
189 } else if (isdirective(word)) { |
|
190 memcpy(&(dest[dp]), "<span class=\"c2html-directive\">", 31); |
|
191 dp += 31; |
|
192 } else { |
|
193 closespan = 0; |
|
194 } |
|
195 for (int i = 0 ; i < wp ; i++) { |
|
196 dp = writeescapedchar(dest, dp, word[i]); |
|
197 } |
|
198 if (closespan) { |
|
199 memcpy(&(dest[dp]), "</span>", 7); |
|
200 dp += 7; |
|
201 } |
|
202 memset(word, 0, WORDBUF_SIZE); |
|
203 wp = 0; |
|
204 } |
|
205 dp = writeescapedchar(dest, dp, c); |
|
206 } else { |
|
207 /* read word */ |
|
208 if (wp < WORDBUF_SIZE) { |
|
209 word[wp++] = c; |
|
210 } else if (wp == WORDBUF_SIZE) { |
|
211 for (int i = 0 ; i < WORDBUF_SIZE ; i++) { |
|
212 dp = writeescapedchar(dest, dp, word[i]); |
|
213 } |
|
214 wp++; |
|
215 dp = writeescapedchar(dest, dp, c); |
173 } else { |
216 } else { |
174 closespan = 0; |
217 dp = writeescapedchar(dest, dp, c); |
175 } |
218 } |
176 for (int i = 0 ; i < wp ; i++) { |
219 } |
177 dp = writeescapedchar(dest, dp, word[i]); |
220 } |
178 } |
221 |
179 if (closespan) { |
222 isescaping = !isescaping & (c == '\\'); |
180 memcpy(&(dest[dp]), "</span>", 7); |
|
181 dp += 7; |
|
182 } |
|
183 memset(word, 0, WORDBUF_SIZE); |
|
184 wp = 0; |
|
185 } |
|
186 dp = writeescapedchar(dest, dp, c); |
|
187 } else { |
|
188 /* read word */ |
|
189 if (wp < WORDBUF_SIZE) { |
|
190 word[wp++] = c; |
|
191 } else if (wp == WORDBUF_SIZE) { |
|
192 for (int i = 0 ; i < WORDBUF_SIZE ; i++) { |
|
193 dp = writeescapedchar(dest, dp, word[i]); |
|
194 } |
|
195 wp++; |
|
196 dp = writeescapedchar(dest, dp, c); |
|
197 } else { |
|
198 dp = writeescapedchar(dest, dp, c); |
|
199 } |
|
200 } |
|
201 } |
223 } |
202 dest[dp] = 0; |
224 dest[dp] = 0; |
203 } |
225 } |
204 |
226 |
205 void printhelp() { |
227 void printhelp() { |