1 /* |
1 #include <errno.h> |
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
2 |
3 * |
3 #include "c2html.h" |
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 */ |
|
29 |
|
30 #include <stdio.h> |
|
31 #include <stdlib.h> |
|
32 #include <string.h> |
|
33 #include <fcntl.h> |
|
34 #include <unistd.h> |
|
35 #include <ctype.h> |
|
36 |
|
37 #include "javacodegen.h" |
|
38 #include "ccodegen.h" |
|
39 |
|
40 #define INPUTBUF_SIZE 2048 |
|
41 |
|
42 typedef struct { |
|
43 char* outfilename; |
|
44 char* infilename; |
|
45 int highlight; |
|
46 } settings_t; |
|
47 |
|
48 typedef struct { |
|
49 size_t count; |
|
50 size_t capacity; |
|
51 size_t maxlinewidth; |
|
52 char** lines; |
|
53 } inputfile_t; |
|
54 |
4 |
55 inputfile_t *inputfilebuffer(size_t capacity) { |
5 inputfile_t *inputfilebuffer(size_t capacity) { |
56 inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t)); |
6 inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t)); |
57 inputfile->lines = (char**) malloc(capacity * sizeof(char*)); |
7 inputfile->lines = (char**) malloc(capacity * sizeof(char*)); |
58 inputfile->capacity = capacity; |
8 inputfile->capacity = capacity; |
128 " c2html [Options] FILE\n\n" |
78 " c2html [Options] FILE\n\n" |
129 " Options:\n" |
79 " Options:\n" |
130 " -h Prints this help message\n" |
80 " -h Prints this help message\n" |
131 " -j Highlight Java instead of C source code\n" |
81 " -j Highlight Java instead of C source code\n" |
132 " -o <output> Output file (stdout, if not specified)\n" |
82 " -o <output> Output file (stdout, if not specified)\n" |
|
83 " -H <header> Prepend header file\n" |
|
84 " -F <footer> Append footer file\n" |
133 " -p Disable highlighting (plain text)\n" |
85 " -p Disable highlighting (plain text)\n" |
134 "\n"); |
86 "\n"); |
135 |
87 |
136 |
88 |
137 } |
89 } |
140 int w = 1, p = 1; |
92 int w = 1, p = 1; |
141 while ((p*=10) < lnc) w++; |
93 while ((p*=10) < lnc) w++; |
142 return w; |
94 return w; |
143 } |
95 } |
144 |
96 |
|
97 int copyfile(char *filename, FILE *dest) { |
|
98 if (!filename) { |
|
99 return 0; |
|
100 } |
|
101 |
|
102 FILE *src = fopen(filename, "r"); |
|
103 if (src) { |
|
104 char buf[4096]; |
|
105 int r; |
|
106 while ((r = fread(buf, 1, 4096, src)) > 0) { |
|
107 fwrite(buf, 1, r, dest); |
|
108 } |
|
109 fclose(src); |
|
110 return 0; |
|
111 } else { |
|
112 return errno; |
|
113 } |
|
114 } |
|
115 |
145 int main(int argc, char** argv) { |
116 int main(int argc, char** argv) { |
|
117 int retcode = EXIT_SUCCESS; |
|
118 |
146 settings_t settings; |
119 settings_t settings; |
147 settings.outfilename = NULL; |
120 memset(&settings, 0, sizeof(settings)); |
148 settings.highlight = 1; |
121 settings.highlight = 1; |
149 |
122 |
150 highlighter_t highlighter; |
123 highlighter_t highlighter; |
151 memset(&highlighter, 0, sizeof(highlighter)); |
124 memset(&highlighter, 0, sizeof(highlighter)); |
152 highlighter.isdirective = iscdirective; |
125 highlighter.isdirective = iscdirective; |
153 highlighter.istype = isctype; |
126 highlighter.istype = isctype; |
154 highlighter.keywords = ckeywords; |
127 highlighter.keywords = ckeywords; |
155 highlighter.parser = cparseline; |
128 highlighter.parser = cparseline; |
156 |
129 |
157 char optc; |
130 char optc; |
158 while ((optc = getopt(argc, argv, "hjo:p")) != -1) { |
131 while ((optc = getopt(argc, argv, "hjo:pH:F:")) != -1) { |
159 switch (optc) { |
132 switch (optc) { |
160 case 'o': |
133 case 'o': |
161 if (!(optarg[0] == '-' && optarg[1] == 0)) { |
134 if (!(optarg[0] == '-' && optarg[1] == 0)) { |
162 settings.outfilename = optarg; |
135 settings.outfilename = optarg; |
163 } |
136 } |
164 break; |
137 break; |
|
138 case 'F': |
|
139 settings.footerfile = optarg; |
|
140 break; |
|
141 case 'H': |
|
142 settings.headerfile = optarg; |
|
143 break; |
165 case 'j': |
144 case 'j': |
166 highlighter.isdirective = isjdirective; |
145 highlighter.isdirective = isjdirective; |
167 highlighter.istype = isjtype; |
146 highlighter.istype = isjtype; |
168 highlighter.keywords = jkeywords; |
147 highlighter.keywords = jkeywords; |
169 highlighter.parser = jparseline; |
148 highlighter.parser = jparseline; |
182 if (optind != argc-1) { |
161 if (optind != argc-1) { |
183 printhelp(); |
162 printhelp(); |
184 return 1; |
163 return 1; |
185 } else { |
164 } else { |
186 settings.infilename = argv[optind]; |
165 settings.infilename = argv[optind]; |
|
166 FILE *fout; |
|
167 if (settings.outfilename) { |
|
168 fout = fopen(settings.outfilename, "w"); |
|
169 if (!fout) { |
|
170 perror("Error opening output file"); |
|
171 return errno; |
|
172 } |
|
173 } else { |
|
174 fout = stdout; |
|
175 } |
|
176 |
|
177 if (copyfile(settings.headerfile, fout)) { |
|
178 perror("Error opening header file"); |
|
179 retcode = errno; |
|
180 goto prog_end; |
|
181 } |
187 |
182 |
188 inputfile_t *inputfile = readinput(settings.infilename); |
183 inputfile_t *inputfile = readinput(settings.infilename); |
189 if (inputfile) { |
184 if (inputfile) { |
190 FILE *fout; |
|
191 char *line; |
185 char *line; |
192 if (settings.highlight) { |
186 if (settings.highlight) { |
193 line = (char*) malloc(inputfile->maxlinewidth*64); |
187 line = (char*) malloc(inputfile->maxlinewidth*64); |
194 } else { |
188 } else { |
195 line = NULL; |
189 line = NULL; |
196 } |
|
197 if (settings.outfilename) { |
|
198 fout = fopen(settings.outfilename, "w"); |
|
199 } else { |
|
200 fout = stdout; |
|
201 } |
190 } |
202 fprintf(fout, "<pre>\n"); |
191 fprintf(fout, "<pre>\n"); |
203 int lnw = lnint(inputfile->count); |
192 int lnw = lnint(inputfile->count); |
204 for (int i = 0 ; i < inputfile->count ; i++) { |
193 for (int i = 0 ; i < inputfile->count ; i++) { |
205 if (settings.highlight) { |
194 if (settings.highlight) { |
213 if (settings.highlight) { |
202 if (settings.highlight) { |
214 free(line); |
203 free(line); |
215 } |
204 } |
216 fprintf(fout, "</pre>\n"); |
205 fprintf(fout, "</pre>\n"); |
217 |
206 |
218 if (fout != stdout) { |
|
219 fclose(fout); |
|
220 } |
|
221 |
|
222 freeinputfilebuffer(inputfile); |
207 freeinputfilebuffer(inputfile); |
223 } |
208 |
224 |
209 if (copyfile(settings.footerfile, fout)) { |
225 return 0; |
210 perror("Error opening footer file"); |
226 } |
211 retcode = errno; |
227 } |
212 } |
228 |
213 } else { |
|
214 perror("Error opening input file"); |
|
215 retcode = errno; |
|
216 } |
|
217 |
|
218 prog_end: |
|
219 if (fout != stdout) { |
|
220 fclose(fout); |
|
221 } |
|
222 |
|
223 return retcode; |
|
224 } |
|
225 } |
|
226 |