Tue, 23 Aug 2016 16:34:02 +0200
words (token) are now stored as sstr_t
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, int* x) {
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 int multiline_comment = 0;
89 UCX_FOREACH(sourceline, in) {
90 /* increase line number and clean line buffer */
91 lineno++;
92 ucx_buffer_clear(line);
94 /* write line number */
95 if (showlineno) {
96 ucx_bprintf(line, "<span class=\"c2html-lineno\">"
97 "<a name=\"l%d\" href=\"#l%d\">%*d </a></span> ",
98 lineno, lineno, lnw, lineno);
99 }
101 /* process code line */
102 highlighter(sourceline->data, line, &multiline_comment);
104 /* write code line */
105 out(line->space, 1, line->size, stream);
106 }
108 /* end monospace formatting */
109 out("</pre>\n", 1, 7, stream);
111 /* cleanup and return */
112 ucx_buffer_free(line);
113 return 0;
114 }
116 #define FILEBUF_SIZE 4096
118 enum source_type {
119 SOURCE_C,
120 SOURCE_JAVA,
121 SOURCE_PLAIN
122 };
124 int main(int argc, char** argv) {
126 /* Default settings */
127 Settings settings;
128 memset(&settings, 0, sizeof(settings));
129 settings.showlinenumbers = 1;
130 enum source_type sourcetype = SOURCE_C;
132 /* Parse command line */
133 char optc;
134 while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) {
135 switch (optc) {
136 case 'o':
137 if (!(optarg[0] == '-' && optarg[1] == 0)) {
138 settings.outfilename = optarg;
139 }
140 break;
141 case 'F':
142 settings.footerfile = optarg;
143 break;
144 case 'H':
145 settings.headerfile = optarg;
146 break;
147 case 'j':
148 sourcetype = SOURCE_JAVA;
149 break;
150 case 'p':
151 sourcetype = SOURCE_PLAIN;
152 break;
153 case 'l':
154 settings.showlinenumbers = 0;
155 break;
156 case 'h':
157 printhelp();
158 return EXIT_SUCCESS;
159 case 'v':
160 case 'V':
161 #ifdef VERSION_DEVELOP
162 printf("%d.%d (unstable)\n", VERSION_MAJOR, VERSION_MINOR);
163 #else
164 printf("%d.%d\n", VERSION_MAJOR, VERSION_MINOR);
165 #endif
166 return EXIT_SUCCESS;
167 default:
168 return EXIT_FAILURE;
169 }
170 }
172 if (optind != argc-1) {
173 printhelp();
174 return EXIT_FAILURE;
175 } else {
176 /* Choose highlighter */
177 highlighter_func hltr = NULL;
178 switch (sourcetype) {
179 case SOURCE_C:
180 hltr = cparseline;
181 break;
182 case SOURCE_JAVA:
183 hltr = jparseline;
184 break;
185 case SOURCE_PLAIN:
186 hltr = plainparseline;
187 break;
188 default: /* should be unreachable */
189 fprintf(stderr, "error in enum source_type\n");
190 return EXIT_FAILURE;
191 }
193 /* Open output file */
194 settings.infilename = argv[optind];
195 FILE *fout;
196 if (settings.outfilename) {
197 fout = fopen(settings.outfilename, "w");
198 if (!fout) {
199 perror("Error opening output file");
200 return EXIT_FAILURE;
201 }
202 } else {
203 fout = stdout;
204 }
206 /* Allocate file buffer */
207 char *filebuf = malloc(FILEBUF_SIZE);
208 if (!filebuf) {
209 perror("Error allocating file buffer");
210 return EXIT_FAILURE;
211 }
213 /* Prepend header file */
214 {
215 FILE *headerfile = fopen(settings.headerfile, "r");
216 if (!headerfile) {
217 perror("Error opening header file");
218 if (fout != stdout) {
219 fclose(fout);
220 }
221 return EXIT_FAILURE;
222 }
223 ucx_stream_copy(headerfile, fout,
224 (read_func) fread, (write_func) fwrite,
225 filebuf, FILEBUF_SIZE, (size_t)-1);
226 fclose(headerfile);
227 }
229 /* Process input file */
230 FILE *inputfile = fopen(settings.infilename, "r");
231 if (inputfile) {
232 UcxBuffer *content = ucx_buffer_new(NULL,
233 FILEBUF_SIZE*2, UCX_BUFFER_AUTOEXTEND);
234 {
235 ucx_stream_copy(inputfile, content, (read_func) fread,
236 (write_func) ucx_buffer_write,
237 filebuf, FILEBUF_SIZE, (size_t)-1);
238 }
239 fclose(inputfile);
241 UcxList *inputlines = ucx_list_append(NULL, content->space);
242 for (size_t i = 1 ; i < content->size ; i++) {
243 if (content->space[i] == '\r') {
244 content->space[i] = '\n'; i++;
245 }
246 if (content->space[i] == '\n' && i+1 < content->size) {
247 ucx_list_append(inputlines, content->space+i+1);
248 }
249 }
251 formatlines(hltr, inputlines,
252 (write_func) fwrite, fout, settings.showlinenumbers);
254 ucx_buffer_free(content);
255 } else {
256 perror("Error opening input file");
257 if (fout != stdout) {
258 fclose(fout);
259 }
260 return EXIT_FAILURE;
261 }
263 /* Append footer file */
264 {
265 FILE *footerfile = fopen(settings.footerfile, "r");
266 if (!footerfile) {
267 perror("Error opening footer file");
268 if (fout != stdout) {
269 fclose(fout);
270 }
271 return EXIT_FAILURE;
272 }
273 ucx_stream_copy(footerfile, fout,
274 (read_func) fread, (write_func) fwrite,
275 filebuf, FILEBUF_SIZE, (size_t)-1);
276 fclose(footerfile);
277 }
280 free(filebuf);
282 return EXIT_SUCCESS;
283 }
284 }