src/c2html.c

changeset 16
fa0bcd0444eb
parent 15
398a7589297f
child 17
7ea86024aef0
equal deleted inserted replaced
15:398a7589297f 16:fa0bcd0444eb
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;
143 } 163 }
144 164
145 return dp; 165 return dp;
146 } 166 }
147 167
148 int iskeyword(char *word) { 168 int iskeyword(char *word, const char** keywords) {
149 for (int i = 0 ; keywords[i] ; i++) { 169 for (int i = 0 ; keywords[i] ; i++) {
150 if (strncmp(keywords[i], word, WORDBUF_SIZE) == 0) { 170 if (strncmp(keywords[i], word, WORDBUF_SIZE) == 0) {
151 return 1; 171 return 1;
152 } 172 }
153 } 173 }
161 } 181 }
162 } 182 }
163 return 1; 183 return 1;
164 } 184 }
165 185
166 void parseline(char *src, char *dest) { 186 void parseline(char *src, char *dest, highlighter_t *highlighter) {
167 size_t sp = 0, dp = 0; 187 size_t sp = 0, dp = 0;
168 /* indent */ 188 /* indent */
169 while (isspace(src[sp])) { 189 while (isspace(src[sp])) {
170 dest[dp++] = src[sp++]; 190 dest[dp++] = src[sp++];
171 } 191 }
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);

mercurial