Fri, 21 Jun 2013 12:49:46 +0200
long words are correctly separated by non alpha numeric chars, underscore or hash + support for macro constants
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 Mike Becker. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <ctype.h>
37 #define INPUTBUF_SIZE 2048
38 #define WORDBUF_SIZE 16
40 #define istype(word, len) (word[len-2] == '_' && word[len-1] == 't')
41 #define isdirective(word) (word[0] == '#')
43 const char* keywords[] = {
44 "auto", "break", "case", "char", "const", "continue", "default", "do",
45 "double", "else", "enum", "extern", "float", "for", "goto", "if", "int",
46 "long", "register", "return", "short", "signed", "sizeof", "static", "struct",
47 "switch", "typedef", "union", "unsigned", "void", "volatile", "while", NULL
48 };
51 typedef struct {
52 size_t count;
53 size_t capacity;
54 size_t maxlinewidth;
55 char** lines;
56 } inputfile_t;
58 inputfile_t *inputfilebuffer(size_t capacity) {
59 inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t));
60 inputfile->lines = (char**) malloc(capacity * sizeof(char*));
61 inputfile->capacity = capacity;
62 inputfile->count = 0;
63 inputfile->maxlinewidth = 0;
65 return inputfile;
66 }
68 void addline(inputfile_t *inputfile, char* line, size_t width) {
69 char *l = (char*) malloc(width+1);
70 memcpy(l, line, width);
71 l[width] = 0;
72 if (inputfile->count >= inputfile->capacity) {
73 inputfile->capacity <<= 1;
74 inputfile->lines = realloc(inputfile->lines, inputfile->capacity);
75 }
76 inputfile->lines[inputfile->count] = l;
77 inputfile->maxlinewidth =
78 width > inputfile->maxlinewidth ? width : inputfile->maxlinewidth;
79 inputfile->count++;
80 }
82 void freeinputfilebuffer(inputfile_t *inputfile) {
83 for (int i = 0 ; i < inputfile->count ; i++) {
84 free(inputfile->lines[i]);
85 }
86 free(inputfile->lines);
87 free(inputfile);
88 }
90 inputfile_t *readinput(char *filename) {
92 int fd = open(filename, O_RDONLY);
93 if (fd == -1) return NULL;
95 inputfile_t *inputfile = inputfilebuffer(512);
97 char buf[INPUTBUF_SIZE];
98 ssize_t r;
100 size_t maxlinewidth = 256;
101 char *line = (char*) malloc(maxlinewidth);
102 size_t col = 0;
104 while ((r = read(fd, buf, INPUTBUF_SIZE)) > 0) {
105 for (size_t i = 0 ; i < r ; i++) {
106 if (col >= maxlinewidth-4) {
107 maxlinewidth <<= 1;
108 line = realloc(line, maxlinewidth);
109 }
111 if (buf[i] == '\n') {
112 line[col++] = '\n';
113 line[col] = 0;
114 addline(inputfile, line, col);
115 col = 0;
116 } else {
117 line[col++] = buf[i];
118 }
119 }
120 }
122 free(line);
124 close(fd);
126 return inputfile;
127 }
129 size_t writeescapedchar(char *dest, size_t dp, char c) {
130 if (c == '>') {
131 dest[dp++] = '&'; dest[dp++] = 'g';
132 dest[dp++] = 't'; dest[dp++] = ';';
133 } else if (c == '<') {
134 dest[dp++] = '&'; dest[dp++] = 'l';
135 dest[dp++] = 't'; dest[dp++] = ';';
136 } else {
137 dest[dp++] = c;
138 }
140 return dp;
141 }
143 int iskeyword(char *word) {
144 for (int i = 0 ; keywords[i] ; i++) {
145 if (strncmp(keywords[i], word, WORDBUF_SIZE) == 0) {
146 return 1;
147 }
148 }
149 return 0;
150 }
152 int iscapsonly(char *word, size_t wp) {
153 for (size_t i = 0 ; i < wp ; i++) {
154 if (!isupper(word[i]) && word[i] != '_') {
155 return 0;
156 }
157 }
158 return 1;
159 }
161 void parseline(char *src, char *dest) {
162 size_t sp = 0, dp = 0;
163 /* indent */
164 while (isspace(src[sp])) {
165 dest[dp++] = src[sp++];
166 }
167 char word[WORDBUF_SIZE];
168 memset(word, 0, WORDBUF_SIZE);
169 size_t wp = 0;
170 int isstring = 0, iscomment = 0;
171 static int iscommentml;
172 int isescaping = 0;
174 if (iscommentml) {
175 iscomment = 1;
176 memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29);
177 dp += 29;
178 }
180 for (char c = src[sp] ; c ; c=src[++sp]) {
181 /* comments */
182 if (c == '/') {
183 if (iscommentml && sp > 0 && src[sp-1] == '*') {
184 iscomment = 0;
185 iscommentml = 0;
186 memcpy(&(dest[dp]), "/</span>", 8);
187 dp += 8;
188 continue;
189 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
190 iscomment = 1;
191 iscommentml = (src[sp+1] == '*');
192 memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29);
193 dp += 29;
194 }
195 }
197 if (iscomment) {
198 if (c == '\n') {
199 memcpy(&(dest[dp]), "</span>", 7);
200 dp += 7;
201 }
202 dp = writeescapedchar(dest, dp, c);
203 } else {
204 /* strings */
205 if (!isescaping && (c == '\'' || c == '\"')) {
206 isstring ^= 1;
207 if (isstring) {
208 memcpy(&(dest[dp]), "<span class=\"c2html-string\">", 28);
209 dp += 28;
210 dp = writeescapedchar(dest, dp, c);
211 } else {
212 dp = writeescapedchar(dest, dp, c);
213 memcpy(&(dest[dp]), "</span>", 7);
214 dp += 7;
215 }
216 } else {
217 if (isstring) {
218 dp = writeescapedchar(dest, dp, c);
219 } else if (!isalnum(c) && c != '_' && c != '#') {
220 /* interpret word int_t */
221 if (wp > 0 && wp < WORDBUF_SIZE) {
222 int closespan = 1;
223 if (iskeyword(word)) {
224 memcpy(&(dest[dp]), "<span class=\"c2html-keyword\">", 29);
225 dp += 29;
226 } else if (istype(word, wp)) {
227 memcpy(&(dest[dp]), "<span class=\"c2html-type\">", 26);
228 dp += 26;
229 } else if (isdirective(word)) {
230 memcpy(&(dest[dp]), "<span class=\"c2html-directive\">", 31);
231 dp += 31;
232 } else if (iscapsonly(word, wp)) {
233 memcpy(&(dest[dp]), "<span class=\"c2html-macroconst\">", 32);
234 dp += 32;
235 } else {
236 closespan = 0;
237 }
238 for (int i = 0 ; i < wp ; i++) {
239 dp = writeescapedchar(dest, dp, word[i]);
240 }
241 if (closespan) {
242 memcpy(&(dest[dp]), "</span>", 7);
243 dp += 7;
244 }
245 }
246 memset(word, 0, WORDBUF_SIZE);
247 wp = 0;
248 dp = writeescapedchar(dest, dp, c);
249 } else {
250 /* read word */
251 if (wp < WORDBUF_SIZE) {
252 word[wp++] = c;
253 } else if (wp == WORDBUF_SIZE) {
254 for (int i = 0 ; i < WORDBUF_SIZE ; i++) {
255 dp = writeescapedchar(dest, dp, word[i]);
256 }
257 wp++;
258 dp = writeescapedchar(dest, dp, c);
259 } else {
260 dp = writeescapedchar(dest, dp, c);
261 }
262 }
263 }
265 isescaping = !isescaping & (c == '\\');
266 }
267 }
268 dest[dp] = 0;
269 }
271 void printhelp() {
272 printf("Formats source code using HTML.\n\nUsage:\n"
273 " c2html [FILE...]"
274 "\n");
277 }
279 int lnint(size_t lnc) {
280 int w = 1, p = 1;
281 while ((p*=10) < lnc) w++;
282 return w;
283 }
285 int main(int argc, char** argv) {
287 if (argc == 1) {
288 printhelp();
289 return 0;
290 } else {
292 inputfile_t *inputfile = readinput(argv[1]);
293 if (inputfile) {
294 printf("<pre>\n");
295 char *line = (char*) malloc(inputfile->maxlinewidth*64);
296 int lnw = lnint(inputfile->count);
297 for (int i = 0 ; i < inputfile->count ; i++) {
298 parseline(inputfile->lines[i], line);
299 printf("<span class=\"c2html-lineno\">%*d:</span> %s",
300 lnw, i+1, line);
301 }
302 free(line);
303 printf("</pre>\n");
304 freeinputfilebuffer(inputfile);
305 }
307 return 0;
308 }
309 }