Thu, 23 Jan 2014 14:44:20 +0100
fixed licenses
23 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
4 | * Copyright 2014 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 | */ | |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
29 | #include <errno.h> |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
30 | |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
31 | #include "c2html.h" |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
32 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
33 | inputfile_t *inputfilebuffer(size_t capacity) { |
19 | 34 | inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t)); |
35 | inputfile->lines = (char**) malloc(capacity * sizeof(char*)); | |
36 | inputfile->capacity = capacity; | |
37 | inputfile->count = 0; | |
38 | inputfile->maxlinewidth = 0; | |
39 | ||
40 | return inputfile; | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
41 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
42 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
43 | void addline(inputfile_t *inputfile, char* line, size_t width) { |
19 | 44 | char *l = (char*) malloc(width+1); |
45 | memcpy(l, line, width); | |
46 | l[width] = 0; | |
47 | if (inputfile->count >= inputfile->capacity) { | |
48 | inputfile->capacity <<= 1; | |
49 | inputfile->lines = realloc(inputfile->lines, inputfile->capacity); | |
50 | } | |
51 | inputfile->lines[inputfile->count] = l; | |
52 | inputfile->maxlinewidth = | |
53 | width > inputfile->maxlinewidth ? width : inputfile->maxlinewidth; | |
54 | inputfile->count++; | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
55 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
56 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
57 | void freeinputfilebuffer(inputfile_t *inputfile) { |
19 | 58 | for (int i = 0 ; i < inputfile->count ; i++) { |
59 | free(inputfile->lines[i]); | |
60 | } | |
61 | free(inputfile->lines); | |
62 | free(inputfile); | |
0 | 63 | } |
64 | ||
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
65 | inputfile_t *readinput(char *filename) { |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
66 | |
19 | 67 | int fd = open(filename, O_RDONLY); |
68 | if (fd == -1) return NULL; | |
69 | ||
70 | inputfile_t *inputfile = inputfilebuffer(512); | |
71 | ||
72 | char buf[INPUTBUF_SIZE]; | |
73 | ssize_t r; | |
74 | ||
75 | size_t maxlinewidth = 256; | |
76 | char *line = (char*) malloc(maxlinewidth); | |
77 | size_t col = 0; | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
78 | |
19 | 79 | while ((r = read(fd, buf, INPUTBUF_SIZE)) > 0) { |
80 | for (size_t i = 0 ; i < r ; i++) { | |
81 | if (col >= maxlinewidth-4) { | |
82 | maxlinewidth <<= 1; | |
83 | line = realloc(line, maxlinewidth); | |
84 | } | |
85 | ||
86 | if (buf[i] == '\n') { | |
87 | line[col++] = '\n'; | |
88 | line[col] = 0; | |
89 | addline(inputfile, line, col); | |
90 | col = 0; | |
91 | } else { | |
92 | line[col++] = buf[i]; | |
93 | } | |
94 | } | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
95 | } |
19 | 96 | |
97 | free(line); | |
98 | ||
99 | close(fd); | |
100 | ||
101 | return inputfile; | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
102 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
103 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
104 | void printhelp() { |
19 | 105 | printf("Formats source code using HTML.\n\nUsage:\n" |
106 | " c2html [Options] FILE\n\n" | |
107 | " Options:\n" | |
108 | " -h Prints this help message\n" | |
109 | " -j Highlight Java instead of C source code\n" | |
110 | " -o <output> Output file (stdout, if not specified)\n" | |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
111 | " -H <header> Prepend header file\n" |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
112 | " -F <footer> Append footer file\n" |
19 | 113 | " -p Disable highlighting (plain text)\n" |
114 | "\n"); | |
115 | ||
116 | ||
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
117 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
118 | |
4 | 119 | int lnint(size_t lnc) { |
19 | 120 | int w = 1, p = 1; |
121 | while ((p*=10) < lnc) w++; | |
122 | return w; | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
123 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
124 | |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
125 | int copyfile(char *filename, FILE *dest) { |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
126 | if (!filename) { |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
127 | return 0; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
128 | } |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
129 | |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
130 | FILE *src = fopen(filename, "r"); |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
131 | if (src) { |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
132 | char buf[4096]; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
133 | int r; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
134 | while ((r = fread(buf, 1, 4096, src)) > 0) { |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
135 | fwrite(buf, 1, r, dest); |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
136 | } |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
137 | fclose(src); |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
138 | return 0; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
139 | } else { |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
140 | return errno; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
141 | } |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
142 | } |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
143 | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
144 | int main(int argc, char** argv) { |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
145 | int retcode = EXIT_SUCCESS; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
146 | |
19 | 147 | settings_t settings; |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
148 | memset(&settings, 0, sizeof(settings)); |
19 | 149 | settings.highlight = 1; |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
150 | |
19 | 151 | highlighter_t highlighter; |
20
ebbf0776c1bc
replaced function static variables with struct members
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
152 | memset(&highlighter, 0, sizeof(highlighter)); |
19 | 153 | highlighter.isdirective = iscdirective; |
154 | highlighter.istype = isctype; | |
155 | highlighter.keywords = ckeywords; | |
21 | 156 | highlighter.parser = cparseline; |
19 | 157 | |
158 | char optc; | |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
159 | while ((optc = getopt(argc, argv, "hjo:pH:F:")) != -1) { |
19 | 160 | switch (optc) { |
161 | case 'o': | |
162 | if (!(optarg[0] == '-' && optarg[1] == 0)) { | |
163 | settings.outfilename = optarg; | |
164 | } | |
165 | break; | |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
166 | case 'F': |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
167 | settings.footerfile = optarg; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
168 | break; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
169 | case 'H': |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
170 | settings.headerfile = optarg; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
171 | break; |
19 | 172 | case 'j': |
173 | highlighter.isdirective = isjdirective; | |
174 | highlighter.istype = isjtype; | |
175 | highlighter.keywords = jkeywords; | |
21 | 176 | highlighter.parser = jparseline; |
19 | 177 | break; |
178 | case 'p': | |
179 | settings.highlight = 0; | |
180 | break; | |
181 | case 'h': | |
182 | printhelp(); | |
183 | return 0; | |
184 | default: | |
185 | return 1; | |
11 | 186 | } |
19 | 187 | } |
188 | ||
189 | if (optind != argc-1) { | |
11 | 190 | printhelp(); |
191 | return 1; | |
19 | 192 | } else { |
193 | settings.infilename = argv[optind]; | |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
194 | FILE *fout; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
195 | if (settings.outfilename) { |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
196 | fout = fopen(settings.outfilename, "w"); |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
197 | if (!fout) { |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
198 | perror("Error opening output file"); |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
199 | return errno; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
200 | } |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
201 | } else { |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
202 | fout = stdout; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
203 | } |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
204 | |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
205 | if (copyfile(settings.headerfile, fout)) { |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
206 | perror("Error opening header file"); |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
207 | retcode = errno; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
208 | goto prog_end; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
209 | } |
11 | 210 | |
19 | 211 | inputfile_t *inputfile = readinput(settings.infilename); |
212 | if (inputfile) { | |
213 | char *line; | |
214 | if (settings.highlight) { | |
215 | line = (char*) malloc(inputfile->maxlinewidth*64); | |
216 | } else { | |
217 | line = NULL; | |
218 | } | |
219 | fprintf(fout, "<pre>\n"); | |
220 | int lnw = lnint(inputfile->count); | |
221 | for (int i = 0 ; i < inputfile->count ; i++) { | |
222 | if (settings.highlight) { | |
21 | 223 | highlighter.parser(inputfile->lines[i], line, &highlighter); |
19 | 224 | } else { |
225 | line = inputfile->lines[i]; | |
226 | } | |
227 | fprintf(fout, "<span class=\"c2html-lineno\">%*d:</span> %s", | |
228 | lnw, i+1, line); | |
229 | } | |
230 | if (settings.highlight) { | |
231 | free(line); | |
232 | } | |
233 | fprintf(fout, "</pre>\n"); | |
234 | ||
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
235 | freeinputfilebuffer(inputfile); |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
236 | |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
237 | if (copyfile(settings.footerfile, fout)) { |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
238 | perror("Error opening footer file"); |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
239 | retcode = errno; |
19 | 240 | } |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
241 | } else { |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
242 | perror("Error opening input file"); |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
243 | retcode = errno; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
244 | } |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
245 | |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
246 | prog_end: |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
247 | if (fout != stdout) { |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
248 | fclose(fout); |
12 | 249 | } |
19 | 250 | |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
251 | return retcode; |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
252 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
253 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
254 |