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