Tue, 23 Aug 2016 15:55:02 +0200
refactors highlighter_t and removes abstraction overhead
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2016 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 */
29 #include "c2html.h"
31 #include "ucx/buffer.h"
32 #include "ucx/list.h"
33 #include "ucx/utils.h"
35 void printhelp() {
36 printf("Formats source code using HTML.\n\nUsage:\n"
37 " c2html [Options] FILE\n\n"
38 " Options:\n"
39 " -h Prints this help message\n"
40 " -j Highlight Java instead of C source code\n"
41 " -o <output> Output file (stdout, if not specified)\n"
42 " -H <header> Prepend header file\n"
43 " -F <footer> Append footer file\n"
44 " -p Disable highlighting (plain text)\n"
45 " -l Disable line numbers\n"
46 " -V, -v Prints version and exits\n"
47 "\n");
48 }
50 /* TODO: remove this workaround after refactoring highlighter structure */
51 static void plainparseline(char *src, UcxBuffer *dest, HighlighterData* hltr) {
52 size_t dp = 0;
53 char *buf = dest->space + dest->pos;
54 while (*src && *src != '\n') {
55 dp = writeescapedchar(buf, dp, *src);
56 src++;
57 }
58 buf[dp++] = '\n';
59 buf[dp] = '\0';
60 dest->pos += dp;
61 dest->size += dp;
62 }
64 int formatlines(highlighter_func highlighter,
65 UcxList *in, write_func out, void *stream, int showlineno) {
67 /* compute width of line numbering */
68 int lnw;
69 if (showlineno) {
70 size_t lines = ucx_list_size(in);
71 lnw = 1;
72 int p = 1;
73 while ((p*=10) < lines) lnw++;
74 }
76 /* allocate line buffer */
77 UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND);
78 if(!line) {
79 return 1;
80 }
82 /* start monospace formatting */
83 out("<pre>\n", 1, 6, stream);
85 /* process lines */
86 size_t lineno = 0;
87 HighlighterData highlighter_data;
88 memset(&highlighter_data, 0, sizeof(HighlighterData));
90 UCX_FOREACH(sourceline, in) {
91 /* increase line number and clean line buffer */
92 lineno++;
93 ucx_buffer_clear(line);
95 /* write line number */
96 if (showlineno) {
97 ucx_bprintf(line, "<span class=\"c2html-lineno\">"
98 "<a name=\"l%d\" href=\"#l%d\">%*d </a></span> ",
99 lineno, lineno, lnw, lineno);
100 }
102 /* process code line */
103 highlighter(sourceline->data, line, &highlighter_data);
105 /* write code line */
106 out(line->space, 1, line->size, stream);
107 }
109 /* end monospace formatting */
110 out("</pre>\n", 1, 7, stream);
112 /* cleanup and return */
113 ucx_buffer_free(line);
114 return 0;
115 }
117 #define FILEBUF_SIZE 4096
119 enum source_type {
120 SOURCE_C,
121 SOURCE_JAVA,
122 SOURCE_PLAIN
123 };
125 int main(int argc, char** argv) {
127 /* Default settings */
128 Settings settings;
129 memset(&settings, 0, sizeof(settings));
130 settings.showlinenumbers = 1;
131 enum source_type sourcetype = SOURCE_C;
133 /* Parse command line */
134 char optc;
135 while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) {
136 switch (optc) {
137 case 'o':
138 if (!(optarg[0] == '-' && optarg[1] == 0)) {
139 settings.outfilename = optarg;
140 }
141 break;
142 case 'F':
143 settings.footerfile = optarg;
144 break;
145 case 'H':
146 settings.headerfile = optarg;
147 break;
148 case 'j':
149 sourcetype = SOURCE_JAVA;
150 break;
151 case 'p':
152 sourcetype = SOURCE_PLAIN;
153 break;
154 case 'l':
155 settings.showlinenumbers = 0;
156 break;
157 case 'h':
158 printhelp();
159 return EXIT_SUCCESS;
160 case 'v':
161 case 'V':
162 #ifdef VERSION_DEVELOP
163 printf("%d.%d (unstable)\n", VERSION_MAJOR, VERSION_MINOR);
164 #else
165 printf("%d.%d\n", VERSION_MAJOR, VERSION_MINOR);
166 #endif
167 return EXIT_SUCCESS;
168 default:
169 return EXIT_FAILURE;
170 }
171 }
173 if (optind != argc-1) {
174 printhelp();
175 return EXIT_FAILURE;
176 } else {
177 /* Configure highlighter */
178 highlighter_func hltr = NULL;
179 switch (sourcetype) {
180 case SOURCE_C:
181 hltr = cparseline;
182 break;
183 case SOURCE_JAVA:
184 hltr = jparseline;
185 break;
186 case SOURCE_PLAIN:
187 hltr = plainparseline;
188 break;
189 default: /* should be unreachable */
190 fprintf(stderr, "error in enum source_type\n");
191 return EXIT_FAILURE;
192 }
194 /* Open output file */
195 settings.infilename = argv[optind];
196 FILE *fout;
197 if (settings.outfilename) {
198 fout = fopen(settings.outfilename, "w");
199 if (!fout) {
200 perror("Error opening output file");
201 return EXIT_FAILURE;
202 }
203 } else {
204 fout = stdout;
205 }
207 /* Allocate file buffer */
208 char *filebuf = malloc(FILEBUF_SIZE);
209 if (!filebuf) {
210 perror("Error allocating file buffer");
211 return EXIT_FAILURE;
212 }
214 /* Prepend header file */
215 {
216 FILE *headerfile = fopen(settings.headerfile, "r");
217 if (!headerfile) {
218 perror("Error opening header file");
219 if (fout != stdout) {
220 fclose(fout);
221 }
222 return EXIT_FAILURE;
223 }
224 ucx_stream_copy(headerfile, fout,
225 (read_func) fread, (write_func) fwrite,
226 filebuf, FILEBUF_SIZE, (size_t)-1);
227 fclose(headerfile);
228 }
230 /* Process input file */
231 FILE *inputfile = fopen(settings.infilename, "r");
232 if (inputfile) {
233 UcxBuffer *content = ucx_buffer_new(NULL,
234 FILEBUF_SIZE*2, UCX_BUFFER_AUTOEXTEND);
235 {
236 ucx_stream_copy(inputfile, content, (read_func) fread,
237 (write_func) ucx_buffer_write,
238 filebuf, FILEBUF_SIZE, (size_t)-1);
239 }
240 fclose(inputfile);
242 UcxList *inputlines = ucx_list_append(NULL, content->space);
243 for (size_t i = 1 ; i < content->size ; i++) {
244 if (content->space[i] == '\r') {
245 content->space[i] = '\n'; i++;
246 }
247 if (content->space[i] == '\n' && i+1 < content->size) {
248 ucx_list_append(inputlines, content->space+i+1);
249 }
250 }
252 formatlines(hltr, inputlines,
253 (write_func) fwrite, fout, settings.showlinenumbers);
255 ucx_buffer_free(content);
256 } else {
257 perror("Error opening input file");
258 if (fout != stdout) {
259 fclose(fout);
260 }
261 return EXIT_FAILURE;
262 }
264 /* Append footer file */
265 {
266 FILE *footerfile = fopen(settings.footerfile, "r");
267 if (!footerfile) {
268 perror("Error opening footer file");
269 if (fout != stdout) {
270 fclose(fout);
271 }
272 return EXIT_FAILURE;
273 }
274 ucx_stream_copy(footerfile, fout,
275 (read_func) fread, (write_func) fwrite,
276 filebuf, FILEBUF_SIZE, (size_t)-1);
277 fclose(footerfile);
278 }
281 free(filebuf);
283 return EXIT_SUCCESS;
284 }
285 }