Wed, 10 Jul 2013 13:45:26 +0200
option for plaintext
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 const char* keywords[] = {
41 "auto", "break", "case", "char", "const", "continue", "default", "do",
42 "double", "else", "enum", "extern", "float", "for", "goto", "if", "int",
43 "long", "register", "return", "short", "signed", "sizeof", "static", "struct",
44 "switch", "typedef", "union", "unsigned", "void", "volatile", "while", NULL
45 };
47 typedef struct {
48 char* outfilename;
49 char* infilename;
50 int highlight;
51 } settings_t;
53 typedef struct {
54 size_t count;
55 size_t capacity;
56 size_t maxlinewidth;
57 char** lines;
58 } inputfile_t;
60 inputfile_t *inputfilebuffer(size_t capacity) {
61 inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t));
62 inputfile->lines = (char**) malloc(capacity * sizeof(char*));
63 inputfile->capacity = capacity;
64 inputfile->count = 0;
65 inputfile->maxlinewidth = 0;
67 return inputfile;
68 }
70 void addline(inputfile_t *inputfile, char* line, size_t width) {
71 char *l = (char*) malloc(width+1);
72 memcpy(l, line, width);
73 l[width] = 0;
74 if (inputfile->count >= inputfile->capacity) {
75 inputfile->capacity <<= 1;
76 inputfile->lines = realloc(inputfile->lines, inputfile->capacity);
77 }
78 inputfile->lines[inputfile->count] = l;
79 inputfile->maxlinewidth =
80 width > inputfile->maxlinewidth ? width : inputfile->maxlinewidth;
81 inputfile->count++;
82 }
84 void freeinputfilebuffer(inputfile_t *inputfile) {
85 for (int i = 0 ; i < inputfile->count ; i++) {
86 free(inputfile->lines[i]);
87 }
88 free(inputfile->lines);
89 free(inputfile);
90 }
92 inputfile_t *readinput(char *filename) {
94 int fd = open(filename, O_RDONLY);
95 if (fd == -1) return NULL;
97 inputfile_t *inputfile = inputfilebuffer(512);
99 char buf[INPUTBUF_SIZE];
100 ssize_t r;
102 size_t maxlinewidth = 256;
103 char *line = (char*) malloc(maxlinewidth);
104 size_t col = 0;
106 while ((r = read(fd, buf, INPUTBUF_SIZE)) > 0) {
107 for (size_t i = 0 ; i < r ; i++) {
108 if (col >= maxlinewidth-4) {
109 maxlinewidth <<= 1;
110 line = realloc(line, maxlinewidth);
111 }
113 if (buf[i] == '\n') {
114 line[col++] = '\n';
115 line[col] = 0;
116 addline(inputfile, line, col);
117 col = 0;
118 } else {
119 line[col++] = buf[i];
120 }
121 }
122 }
124 free(line);
126 close(fd);
128 return inputfile;
129 }
131 size_t writeescapedchar(char *dest, size_t dp, char c) {
132 if (c == '>') {
133 dest[dp++] = '&'; dest[dp++] = 'g';
134 dest[dp++] = 't'; dest[dp++] = ';';
135 } else if (c == '<') {
136 dest[dp++] = '&'; dest[dp++] = 'l';
137 dest[dp++] = 't'; dest[dp++] = ';';
138 } else {
139 dest[dp++] = c;
140 }
142 return dp;
143 }
145 int iskeyword(char *word) {
146 for (int i = 0 ; keywords[i] ; i++) {
147 if (strncmp(keywords[i], word, WORDBUF_SIZE) == 0) {
148 return 1;
149 }
150 }
151 return 0;
152 }
154 #define istype(word, len) (word[len-2] == '_' && word[len-1] == 't')
156 void parseline(char *src, char *dest) {
157 size_t sp = 0, dp = 0;
158 /* indent */
159 while (isspace(src[sp])) {
160 dest[dp++] = src[sp++];
161 }
162 char word[WORDBUF_SIZE];
163 memset(word, 0, WORDBUF_SIZE);
164 size_t wp = 0;
165 int closespan;
166 for (char c = src[sp] ; c ; c=src[++sp]) {
167 if (!isalnum(c) && c != '_') {
168 /* interpret word int_t */
169 if (wp > 0 && wp < WORDBUF_SIZE) {
170 if (iskeyword(word)) {
171 memcpy(&(dest[dp]), "<span class=\"c2html-keyword\">", 29);
172 dp += 29;
173 closespan = 1;
174 } else if (istype(word, wp)) {
175 memcpy(&(dest[dp]), "<span class=\"c2html-type\">", 26);
176 dp += 26;
177 closespan = 1;
178 } else {
179 closespan = 0;
180 }
181 for (int i = 0 ; i < wp ; i++) {
182 dp = writeescapedchar(dest, dp, word[i]);
183 }
184 if (closespan) {
185 memcpy(&(dest[dp]), "</span>", 7);
186 dp += 7;
187 }
188 memset(word, 0, WORDBUF_SIZE);
189 wp = 0;
190 }
191 dp = writeescapedchar(dest, dp, c);
192 } else {
193 /* read word */
194 if (wp < WORDBUF_SIZE) {
195 word[wp++] = c;
196 } else if (wp == WORDBUF_SIZE) {
197 for (int i = 0 ; i < WORDBUF_SIZE ; i++) {
198 dp = writeescapedchar(dest, dp, word[i]);
199 }
200 wp++;
201 dp = writeescapedchar(dest, dp, c);
202 } else {
203 dp = writeescapedchar(dest, dp, c);
204 }
205 }
206 }
207 dest[dp] = 0;
208 }
210 void printhelp() {
211 printf("Formats source code using HTML.\n\nUsage:\n"
212 " c2html [Options] FILE\n\n"
213 " Options:\n"
214 " -h Prints this help message\n"
215 " -o <output> Output file (if not specified, stdout is used)\n"
216 "\n");
219 }
221 int lnint(size_t lnc) {
222 int w = 1, p = 1;
223 while ((p*=10) < lnc) w++;
224 return w;
225 }
227 int main(int argc, char** argv) {
229 settings_t settings;
230 settings.outfilename = NULL;
231 settings.highlight = 1;
233 char optc;
234 while ((optc = getopt(argc, argv, "ho:p")) != -1) {
235 switch (optc) {
236 case 'o':
237 if (!(optarg[0] == '-' && optarg[1] == 0)) {
238 settings.outfilename = optarg;
239 }
240 break;
241 case 'p':
242 settings.highlight = 0;
243 break;
244 case 'h':
245 printhelp();
246 return 0;
247 default:
248 return 1;
249 }
250 }
252 if (optind != argc-1) {
253 printhelp();
254 return 1;
255 } else {
256 settings.infilename = argv[optind];
258 inputfile_t *inputfile = readinput(settings.infilename);
259 if (inputfile) {
260 FILE *fout;
261 if (settings.outfilename) {
262 fout = fopen(settings.outfilename, "w");
263 } else {
264 fout = stdout;
265 }
266 fprintf(fout, "<pre>\n");
267 char *line = (char*) malloc(inputfile->maxlinewidth
268 * (settings.highlight?64:0));
269 int lnw = lnint(inputfile->count);
270 for (int i = 0 ; i < inputfile->count ; i++) {
271 if (settings.highlight) {
272 parseline(inputfile->lines[i], line);
273 } else {
274 line = inputfile->lines[i];
275 }
276 fprintf(fout, "<span class=\"c2html-lineno\">%*d:</span> %s",
277 lnw, i, line);
278 }
279 free(line);
280 fprintf(fout, "</pre>\n");
282 if (fout != stdout) {
283 fclose(fout);
284 }
286 freeinputfilebuffer(inputfile);
287 }
289 return 0;
290 }
291 }