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') |
40 const char* ckeywords[] = { |
41 #define isdirective(word) (word[0] == '#') |
|
42 |
|
43 const char* keywords[] = { |
|
44 "auto", "break", "case", "char", "const", "continue", "default", "do", |
41 "auto", "break", "case", "char", "const", "continue", "default", "do", |
45 "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", |
42 "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", |
46 "long", "register", "return", "short", "signed", "sizeof", "static", "struct", |
43 "long", "register", "return", "short", "signed", "sizeof", "static", |
47 "switch", "typedef", "union", "unsigned", "void", "volatile", "while", NULL |
44 "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", |
|
45 "while", NULL |
48 }; |
46 }; |
|
47 |
|
48 int istype(char *word, size_t len) { |
|
49 return (word[len-2] == '_' && word[len-1] == 't'); |
|
50 } |
|
51 |
|
52 int isdirective(char *word) { |
|
53 return (word[0] == '#'); |
|
54 } |
|
55 |
|
56 int notypes(char *word, size_t len) { |
|
57 return 0; |
|
58 } |
|
59 |
|
60 int nodirectives(char *word) { |
|
61 return 0; |
|
62 } |
|
63 |
|
64 typedef struct { |
|
65 const char** keywords; |
|
66 int(*istype)(char*,size_t); |
|
67 int(*isdirective)(char*); |
|
68 } highlighter_t; |
49 |
69 |
50 typedef struct { |
70 typedef struct { |
51 char* outfilename; |
71 char* outfilename; |
52 char* infilename; |
72 char* infilename; |
53 int highlight; |
73 int highlight; |
260 dp = writeescapedchar(dest, dp, c); |
280 dp = writeescapedchar(dest, dp, c); |
261 } else if (!isalnum(c) && c != '_' && c != '#' && c != '.') { |
281 } else if (!isalnum(c) && c != '_' && c != '#' && c != '.') { |
262 /* interpret word int_t */ |
282 /* interpret word int_t */ |
263 if (wp > 0 && wp < WORDBUF_SIZE) { |
283 if (wp > 0 && wp < WORDBUF_SIZE) { |
264 int closespan = 1; |
284 int closespan = 1; |
265 if (iskeyword(word)) { |
285 if (iskeyword(word, highlighter->keywords)) { |
266 memcpy(&(dest[dp]), "<span class=\"c2html-keyword\">", 29); |
286 memcpy(&(dest[dp]), "<span class=\"c2html-keyword\">", 29); |
267 dp += 29; |
287 dp += 29; |
268 } else if (istype(word, wp)) { |
288 } else if (highlighter->istype(word, wp)) { |
269 memcpy(&(dest[dp]), "<span class=\"c2html-type\">", 26); |
289 memcpy(&(dest[dp]), "<span class=\"c2html-type\">", 26); |
270 dp += 26; |
290 dp += 26; |
271 } else if (isdirective(word)) { |
291 } else if (highlighter->isdirective(word)) { |
272 isinclude = !strncmp("#include", word, WORDBUF_SIZE); |
292 isinclude = !strncmp("#include", word, WORDBUF_SIZE); |
273 memcpy(&(dest[dp]), "<span class=\"c2html-directive\">", 31); |
293 memcpy(&(dest[dp]), "<span class=\"c2html-directive\">", 31); |
274 dp += 31; |
294 dp += 31; |
275 } else if (iscapsonly(word, wp)) { |
295 } else if (iscapsonly(word, wp)) { |
276 memcpy(&(dest[dp]), "<span class=\"c2html-macroconst\">", 32); |
296 memcpy(&(dest[dp]), "<span class=\"c2html-macroconst\">", 32); |
333 |
353 |
334 settings_t settings; |
354 settings_t settings; |
335 settings.outfilename = NULL; |
355 settings.outfilename = NULL; |
336 settings.highlight = 1; |
356 settings.highlight = 1; |
337 |
357 |
|
358 highlighter_t highlighter; |
|
359 highlighter.isdirective = isdirective; |
|
360 highlighter.istype = istype; |
|
361 highlighter.keywords = ckeywords; |
|
362 |
338 char optc; |
363 char optc; |
339 while ((optc = getopt(argc, argv, "ho:p")) != -1) { |
364 while ((optc = getopt(argc, argv, "ho:p")) != -1) { |
340 switch (optc) { |
365 switch (optc) { |
341 case 'o': |
366 case 'o': |
342 if (!(optarg[0] == '-' && optarg[1] == 0)) { |
367 if (!(optarg[0] == '-' && optarg[1] == 0)) { |
376 } |
401 } |
377 fprintf(fout, "<pre>\n"); |
402 fprintf(fout, "<pre>\n"); |
378 int lnw = lnint(inputfile->count); |
403 int lnw = lnint(inputfile->count); |
379 for (int i = 0 ; i < inputfile->count ; i++) { |
404 for (int i = 0 ; i < inputfile->count ; i++) { |
380 if (settings.highlight) { |
405 if (settings.highlight) { |
381 parseline(inputfile->lines[i], line); |
406 parseline(inputfile->lines[i], line, &highlighter); |
382 } else { |
407 } else { |
383 line = inputfile->lines[i]; |
408 line = inputfile->lines[i]; |
384 } |
409 } |
385 fprintf(fout, "<span class=\"c2html-lineno\">%*d:</span> %s", |
410 fprintf(fout, "<span class=\"c2html-lineno\">%*d:</span> %s", |
386 lnw, i+1, line); |
411 lnw, i+1, line); |