Wed, 10 Jul 2013 13:54:15 +0200
changes merged
src/c2html.c | file | annotate | diff | comparison | revisions |
1.1 --- a/src/c2html.c Wed Jul 10 13:45:26 2013 +0200 1.2 +++ b/src/c2html.c Wed Jul 10 13:54:15 2013 +0200 1.3 @@ -37,6 +37,9 @@ 1.4 #define INPUTBUF_SIZE 2048 1.5 #define WORDBUF_SIZE 16 1.6 1.7 +#define istype(word, len) (word[len-2] == '_' && word[len-1] == 't') 1.8 +#define isdirective(word) (word[0] == '#') 1.9 + 1.10 const char* keywords[] = { 1.11 "auto", "break", "case", "char", "const", "continue", "default", "do", 1.12 "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", 1.13 @@ -151,7 +154,14 @@ 1.14 return 0; 1.15 } 1.16 1.17 -#define istype(word, len) (word[len-2] == '_' && word[len-1] == 't') 1.18 +int iscapsonly(char *word, size_t wp) { 1.19 + for (size_t i = 0 ; i < wp ; i++) { 1.20 + if (!isupper(word[i]) && word[i] != '_') { 1.21 + return 0; 1.22 + } 1.23 + } 1.24 + return 1; 1.25 +} 1.26 1.27 void parseline(char *src, char *dest) { 1.28 size_t sp = 0, dp = 0; 1.29 @@ -159,49 +169,143 @@ 1.30 while (isspace(src[sp])) { 1.31 dest[dp++] = src[sp++]; 1.32 } 1.33 - char word[WORDBUF_SIZE]; 1.34 + 1.35 + static char word[WORDBUF_SIZE]; 1.36 + static char includefile[FILENAME_MAX]; 1.37 + 1.38 memset(word, 0, WORDBUF_SIZE); 1.39 - size_t wp = 0; 1.40 - int closespan; 1.41 + size_t wp = 0, ifp = 0; 1.42 + int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0; 1.43 + static int iscommentml; 1.44 + int isescaping = 0; 1.45 + 1.46 + if (iscommentml) { 1.47 + iscomment = 1; 1.48 + memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); 1.49 + dp += 29; 1.50 + } 1.51 + 1.52 for (char c = src[sp] ; c ; c=src[++sp]) { 1.53 - if (!isalnum(c) && c != '_') { 1.54 - /* interpret word int_t */ 1.55 - if (wp > 0 && wp < WORDBUF_SIZE) { 1.56 - if (iskeyword(word)) { 1.57 - memcpy(&(dest[dp]), "<span class=\"c2html-keyword\">", 29); 1.58 - dp += 29; 1.59 - closespan = 1; 1.60 - } else if (istype(word, wp)) { 1.61 - memcpy(&(dest[dp]), "<span class=\"c2html-type\">", 26); 1.62 - dp += 26; 1.63 - closespan = 1; 1.64 + /* comments */ 1.65 + if (c == '/') { 1.66 + if (iscommentml && sp > 0 && src[sp-1] == '*') { 1.67 + iscomment = 0; 1.68 + iscommentml = 0; 1.69 + memcpy(&(dest[dp]), "/</span>", 8); 1.70 + dp += 8; 1.71 + continue; 1.72 + } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { 1.73 + iscomment = 1; 1.74 + iscommentml = (src[sp+1] == '*'); 1.75 + memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); 1.76 + dp += 29; 1.77 + } 1.78 + } 1.79 + 1.80 + if (iscomment) { 1.81 + if (c == '\n') { 1.82 + memcpy(&(dest[dp]), "</span>", 7); 1.83 + dp += 7; 1.84 + } 1.85 + dp = writeescapedchar(dest, dp, c); 1.86 + } else if (isinclude) { 1.87 + if (c == '<') { 1.88 + memcpy(&(dest[dp]), "<span class=\"c2html-stdinclude\">", 32); 1.89 + dp += 32; 1.90 + dp = writeescapedchar(dest, dp, c); 1.91 + } else if (c == '\"') { 1.92 + if (parseinclude) { 1.93 + dest[dp++] = '\"'; 1.94 + dest[dp++] = '>'; 1.95 + memcpy(&(dest[dp]), includefile, ifp); 1.96 + dp += ifp; 1.97 + 1.98 + dp = writeescapedchar(dest, dp, c); 1.99 + memcpy(&(dest[dp]), "</a>", 4); 1.100 + dp += 4; 1.101 + parseinclude = 0; 1.102 } else { 1.103 - closespan = 0; 1.104 + memcpy(&(dest[dp]), "<a class=\"c2html-userinclude\" href=", 35); 1.105 + dp += 35; 1.106 + dp = writeescapedchar(dest, dp, c); 1.107 + ifp = 0; 1.108 + includefile[ifp++] = '\"'; 1.109 + parseinclude = 1; 1.110 } 1.111 - for (int i = 0 ; i < wp ; i++) { 1.112 - dp = writeescapedchar(dest, dp, word[i]); 1.113 + } else if (c == '>') { 1.114 + dp = writeescapedchar(dest, dp, c); 1.115 + memcpy(&(dest[dp]), "</span>", 7); 1.116 + dp += 7; 1.117 + } else { 1.118 + if (parseinclude) { 1.119 + includefile[ifp++] = c; 1.120 } 1.121 - if (closespan) { 1.122 + dp = writeescapedchar(dest, dp, c); 1.123 + } 1.124 + } else { 1.125 + /* strings */ 1.126 + if (!isescaping && (c == '\'' || c == '\"')) { 1.127 + isstring ^= 1; 1.128 + if (isstring) { 1.129 + memcpy(&(dest[dp]), "<span class=\"c2html-string\">", 28); 1.130 + dp += 28; 1.131 + dp = writeescapedchar(dest, dp, c); 1.132 + } else { 1.133 + dp = writeescapedchar(dest, dp, c); 1.134 memcpy(&(dest[dp]), "</span>", 7); 1.135 dp += 7; 1.136 } 1.137 - memset(word, 0, WORDBUF_SIZE); 1.138 - wp = 0; 1.139 + } else { 1.140 + if (isstring) { 1.141 + dp = writeescapedchar(dest, dp, c); 1.142 + } else if (!isalnum(c) && c != '_' && c != '#' && c != '.') { 1.143 + /* interpret word int_t */ 1.144 + if (wp > 0 && wp < WORDBUF_SIZE) { 1.145 + int closespan = 1; 1.146 + if (iskeyword(word)) { 1.147 + memcpy(&(dest[dp]), "<span class=\"c2html-keyword\">", 29); 1.148 + dp += 29; 1.149 + } else if (istype(word, wp)) { 1.150 + memcpy(&(dest[dp]), "<span class=\"c2html-type\">", 26); 1.151 + dp += 26; 1.152 + } else if (isdirective(word)) { 1.153 + isinclude = !strncmp("#include", word, WORDBUF_SIZE); 1.154 + memcpy(&(dest[dp]), "<span class=\"c2html-directive\">", 31); 1.155 + dp += 31; 1.156 + } else if (iscapsonly(word, wp)) { 1.157 + memcpy(&(dest[dp]), "<span class=\"c2html-macroconst\">", 32); 1.158 + dp += 32; 1.159 + } else { 1.160 + closespan = 0; 1.161 + } 1.162 + for (int i = 0 ; i < wp ; i++) { 1.163 + dp = writeescapedchar(dest, dp, word[i]); 1.164 + } 1.165 + if (closespan) { 1.166 + memcpy(&(dest[dp]), "</span>", 7); 1.167 + dp += 7; 1.168 + } 1.169 + } 1.170 + memset(word, 0, WORDBUF_SIZE); 1.171 + wp = 0; 1.172 + dp = writeescapedchar(dest, dp, c); 1.173 + } else { 1.174 + /* read word */ 1.175 + if (wp < WORDBUF_SIZE) { 1.176 + word[wp++] = c; 1.177 + } else if (wp == WORDBUF_SIZE) { 1.178 + for (int i = 0 ; i < WORDBUF_SIZE ; i++) { 1.179 + dp = writeescapedchar(dest, dp, word[i]); 1.180 + } 1.181 + wp++; 1.182 + dp = writeescapedchar(dest, dp, c); 1.183 + } else { 1.184 + dp = writeescapedchar(dest, dp, c); 1.185 + } 1.186 + } 1.187 } 1.188 - dp = writeescapedchar(dest, dp, c); 1.189 - } else { 1.190 - /* read word */ 1.191 - if (wp < WORDBUF_SIZE) { 1.192 - word[wp++] = c; 1.193 - } else if (wp == WORDBUF_SIZE) { 1.194 - for (int i = 0 ; i < WORDBUF_SIZE ; i++) { 1.195 - dp = writeescapedchar(dest, dp, word[i]); 1.196 - } 1.197 - wp++; 1.198 - dp = writeescapedchar(dest, dp, c); 1.199 - } else { 1.200 - dp = writeescapedchar(dest, dp, c); 1.201 - } 1.202 + 1.203 + isescaping = !isescaping & (c == '\\'); 1.204 } 1.205 } 1.206 dest[dp] = 0; 1.207 @@ -274,7 +378,7 @@ 1.208 line = inputfile->lines[i]; 1.209 } 1.210 fprintf(fout, "<span class=\"c2html-lineno\">%*d:</span> %s", 1.211 - lnw, i, line); 1.212 + lnw, i+1, line); 1.213 } 1.214 free(line); 1.215 fprintf(fout, "</pre>\n");
2.1 --- a/test/header.html Wed Jul 10 13:45:26 2013 +0200 2.2 +++ b/test/header.html Wed Jul 10 13:54:15 2013 +0200 2.3 @@ -10,9 +10,31 @@ 2.4 span.c2html-keyword { 2.5 color: blue; 2.6 } 2.7 + span.c2html-macroconst { 2.8 + color: cornflowerblue; 2.9 + } 2.10 span.c2html-type { 2.11 color: cornflowerblue; 2.12 } 2.13 + span.c2html-directive { 2.14 + color: green; 2.15 + } 2.16 + span.c2html-string { 2.17 + color: darkorange; 2.18 + } 2.19 + span.c2html-comment { 2.20 + color: grey; 2.21 + } 2.22 + span.c2html-stdinclude { 2.23 + color: darkorange; 2.24 + } 2.25 + span.c2html-userinclude { 2.26 + color: darkorange; 2.27 + } 2.28 + a.c2html-userinclude { 2.29 + color: darkorange; 2.30 + text-decoration: underline; 2.31 + } 2.32 </style> 2.33 </head> 2.34 <body>