Thu, 23 Jan 2014 09:19:37 +0100
structured source code
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
1 | /* |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
3 | * |
21 | 4 | * Copyright 2014 Mike Becker. All rights reserved. |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
5 | * |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
8 | * |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
11 | * |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
15 | * |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
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 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
30 | #include <stdio.h> |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
31 | #include <stdlib.h> |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
32 | #include <string.h> |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
33 | #include <fcntl.h> |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
34 | #include <unistd.h> |
4 | 35 | #include <ctype.h> |
36 | ||
21 | 37 | #include "javacodegen.h" |
38 | #include "ccodegen.h" | |
18
5085b57e3fd6
fixed highlighting for java
Mike Becker <universe@uap-core.de>
parents:
17
diff
changeset
|
39 | |
21 | 40 | #define INPUTBUF_SIZE 2048 |
16
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
41 | |
11 | 42 | typedef struct { |
19 | 43 | char* outfilename; |
44 | char* infilename; | |
45 | int highlight; | |
11 | 46 | } settings_t; |
4 | 47 | |
48 | typedef struct { | |
19 | 49 | size_t count; |
50 | size_t capacity; | |
51 | size_t maxlinewidth; | |
52 | char** lines; | |
4 | 53 | } inputfile_t; |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
54 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
55 | inputfile_t *inputfilebuffer(size_t capacity) { |
19 | 56 | inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t)); |
57 | inputfile->lines = (char**) malloc(capacity * sizeof(char*)); | |
58 | inputfile->capacity = capacity; | |
59 | inputfile->count = 0; | |
60 | inputfile->maxlinewidth = 0; | |
61 | ||
62 | return inputfile; | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
63 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
64 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
65 | void addline(inputfile_t *inputfile, char* line, size_t width) { |
19 | 66 | char *l = (char*) malloc(width+1); |
67 | memcpy(l, line, width); | |
68 | l[width] = 0; | |
69 | if (inputfile->count >= inputfile->capacity) { | |
70 | inputfile->capacity <<= 1; | |
71 | inputfile->lines = realloc(inputfile->lines, inputfile->capacity); | |
72 | } | |
73 | inputfile->lines[inputfile->count] = l; | |
74 | inputfile->maxlinewidth = | |
75 | width > inputfile->maxlinewidth ? width : inputfile->maxlinewidth; | |
76 | inputfile->count++; | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
77 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
78 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
79 | void freeinputfilebuffer(inputfile_t *inputfile) { |
19 | 80 | for (int i = 0 ; i < inputfile->count ; i++) { |
81 | free(inputfile->lines[i]); | |
82 | } | |
83 | free(inputfile->lines); | |
84 | free(inputfile); | |
0 | 85 | } |
86 | ||
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
87 | inputfile_t *readinput(char *filename) { |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
88 | |
19 | 89 | int fd = open(filename, O_RDONLY); |
90 | if (fd == -1) return NULL; | |
91 | ||
92 | inputfile_t *inputfile = inputfilebuffer(512); | |
93 | ||
94 | char buf[INPUTBUF_SIZE]; | |
95 | ssize_t r; | |
96 | ||
97 | size_t maxlinewidth = 256; | |
98 | char *line = (char*) malloc(maxlinewidth); | |
99 | size_t col = 0; | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
100 | |
19 | 101 | while ((r = read(fd, buf, INPUTBUF_SIZE)) > 0) { |
102 | for (size_t i = 0 ; i < r ; i++) { | |
103 | if (col >= maxlinewidth-4) { | |
104 | maxlinewidth <<= 1; | |
105 | line = realloc(line, maxlinewidth); | |
106 | } | |
107 | ||
108 | if (buf[i] == '\n') { | |
109 | line[col++] = '\n'; | |
110 | line[col] = 0; | |
111 | addline(inputfile, line, col); | |
112 | col = 0; | |
113 | } else { | |
114 | line[col++] = buf[i]; | |
115 | } | |
116 | } | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
117 | } |
19 | 118 | |
119 | free(line); | |
120 | ||
121 | close(fd); | |
122 | ||
123 | return inputfile; | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
124 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
125 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
126 | void printhelp() { |
19 | 127 | printf("Formats source code using HTML.\n\nUsage:\n" |
128 | " c2html [Options] FILE\n\n" | |
129 | " Options:\n" | |
130 | " -h Prints this help message\n" | |
131 | " -j Highlight Java instead of C source code\n" | |
132 | " -o <output> Output file (stdout, if not specified)\n" | |
133 | " -p Disable highlighting (plain text)\n" | |
134 | "\n"); | |
135 | ||
136 | ||
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
137 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
138 | |
4 | 139 | int lnint(size_t lnc) { |
19 | 140 | int w = 1, p = 1; |
141 | while ((p*=10) < lnc) w++; | |
142 | return w; | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
143 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
144 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
145 | int main(int argc, char** argv) { |
19 | 146 | settings_t settings; |
147 | settings.outfilename = NULL; | |
148 | settings.highlight = 1; | |
149 | ||
150 | highlighter_t highlighter; | |
20
ebbf0776c1bc
replaced function static variables with struct members
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
151 | memset(&highlighter, 0, sizeof(highlighter)); |
19 | 152 | highlighter.isdirective = iscdirective; |
153 | highlighter.istype = isctype; | |
154 | highlighter.keywords = ckeywords; | |
21 | 155 | highlighter.parser = cparseline; |
19 | 156 | |
157 | char optc; | |
158 | while ((optc = getopt(argc, argv, "hjo:p")) != -1) { | |
159 | switch (optc) { | |
160 | case 'o': | |
161 | if (!(optarg[0] == '-' && optarg[1] == 0)) { | |
162 | settings.outfilename = optarg; | |
163 | } | |
164 | break; | |
165 | case 'j': | |
166 | highlighter.isdirective = isjdirective; | |
167 | highlighter.istype = isjtype; | |
168 | highlighter.keywords = jkeywords; | |
21 | 169 | highlighter.parser = jparseline; |
19 | 170 | break; |
171 | case 'p': | |
172 | settings.highlight = 0; | |
173 | break; | |
174 | case 'h': | |
175 | printhelp(); | |
176 | return 0; | |
177 | default: | |
178 | return 1; | |
11 | 179 | } |
19 | 180 | } |
181 | ||
182 | if (optind != argc-1) { | |
11 | 183 | printhelp(); |
184 | return 1; | |
19 | 185 | } else { |
186 | settings.infilename = argv[optind]; | |
11 | 187 | |
19 | 188 | inputfile_t *inputfile = readinput(settings.infilename); |
189 | if (inputfile) { | |
190 | FILE *fout; | |
191 | char *line; | |
192 | if (settings.highlight) { | |
193 | line = (char*) malloc(inputfile->maxlinewidth*64); | |
194 | } else { | |
195 | line = NULL; | |
196 | } | |
197 | if (settings.outfilename) { | |
198 | fout = fopen(settings.outfilename, "w"); | |
199 | } else { | |
200 | fout = stdout; | |
201 | } | |
202 | fprintf(fout, "<pre>\n"); | |
203 | int lnw = lnint(inputfile->count); | |
204 | for (int i = 0 ; i < inputfile->count ; i++) { | |
205 | if (settings.highlight) { | |
21 | 206 | highlighter.parser(inputfile->lines[i], line, &highlighter); |
19 | 207 | } else { |
208 | line = inputfile->lines[i]; | |
209 | } | |
210 | fprintf(fout, "<span class=\"c2html-lineno\">%*d:</span> %s", | |
211 | lnw, i+1, line); | |
212 | } | |
213 | if (settings.highlight) { | |
214 | free(line); | |
215 | } | |
216 | fprintf(fout, "</pre>\n"); | |
217 | ||
218 | if (fout != stdout) { | |
219 | fclose(fout); | |
220 | } | |
221 | ||
222 | freeinputfilebuffer(inputfile); | |
12 | 223 | } |
19 | 224 | |
225 | return 0; | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
226 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
227 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
228 |