Fri, 30 Aug 2013 10:51:49 +0200
formatted with 4 spaces
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 | * |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
4 | * Copyright 2013 Mike Becker. All rights reserved. |
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 | ||
37 | #define INPUTBUF_SIZE 2048 | |
18
5085b57e3fd6
fixed highlighting for java
Mike Becker <universe@uap-core.de>
parents:
17
diff
changeset
|
38 | #define WORDBUF_SIZE 64 |
5
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
39 | |
16
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
40 | const char* ckeywords[] = { |
19 | 41 | "auto", "break", "case", "char", "const", "continue", "default", "do", |
42 | "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", | |
43 | "long", "register", "return", "short", "signed", "sizeof", "static", | |
44 | "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", | |
45 | "while", NULL | |
5
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
46 | }; |
4 | 47 | |
17
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
48 | const char* jkeywords[] = { |
19 | 49 | "abstract", "continue", "for", "new", "switch", "assert", "default", "goto", |
50 | "package", "synchronized", "boolean", "do", "if", "private", "this", | |
51 | "break", "double", "implements", "protected", "throw", "byte", "else", | |
52 | "import", "public", "throws", "case", "enum", "instanceof", "return", | |
53 | "transient", "catch", "extends", "int", "short", "try", "char", "final", | |
54 | "interface", "static", "void", "class", "finally", "long", "strictfp", | |
55 | "volatile", "const", "float", "native", "super", "while", NULL | |
17
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
56 | }; |
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
57 | |
18
5085b57e3fd6
fixed highlighting for java
Mike Becker <universe@uap-core.de>
parents:
17
diff
changeset
|
58 | #define iswordcharacter(c) (isalnum(c) || c=='_' || c=='#' || c=='@') |
5085b57e3fd6
fixed highlighting for java
Mike Becker <universe@uap-core.de>
parents:
17
diff
changeset
|
59 | |
17
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
60 | int isctype(char *word, size_t len) { |
19 | 61 | return (word[len-2] == '_' && word[len-1] == 't'); |
16
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
62 | } |
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
63 | |
17
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
64 | int iscdirective(char *word) { |
19 | 65 | return (word[0] == '#'); |
16
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
66 | } |
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
67 | |
17
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
68 | int isjtype(char *word, size_t len) { |
19 | 69 | return isupper(word[0]); |
16
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
70 | } |
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
71 | |
17
7ea86024aef0
implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
72 | int isjdirective(char *word) { |
19 | 73 | return word[0] == '@'; |
16
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
74 | } |
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
75 | |
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
76 | typedef struct { |
19 | 77 | const char** keywords; |
78 | int(*istype)(char*,size_t); | |
79 | int(*isdirective)(char*); | |
16
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
80 | } highlighter_t; |
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
81 | |
11 | 82 | typedef struct { |
19 | 83 | char* outfilename; |
84 | char* infilename; | |
85 | int highlight; | |
11 | 86 | } settings_t; |
4 | 87 | |
88 | typedef struct { | |
19 | 89 | size_t count; |
90 | size_t capacity; | |
91 | size_t maxlinewidth; | |
92 | char** lines; | |
4 | 93 | } inputfile_t; |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
94 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
95 | inputfile_t *inputfilebuffer(size_t capacity) { |
19 | 96 | inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t)); |
97 | inputfile->lines = (char**) malloc(capacity * sizeof(char*)); | |
98 | inputfile->capacity = capacity; | |
99 | inputfile->count = 0; | |
100 | inputfile->maxlinewidth = 0; | |
101 | ||
102 | return inputfile; | |
1
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 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
105 | void addline(inputfile_t *inputfile, char* line, size_t width) { |
19 | 106 | char *l = (char*) malloc(width+1); |
107 | memcpy(l, line, width); | |
108 | l[width] = 0; | |
109 | if (inputfile->count >= inputfile->capacity) { | |
110 | inputfile->capacity <<= 1; | |
111 | inputfile->lines = realloc(inputfile->lines, inputfile->capacity); | |
112 | } | |
113 | inputfile->lines[inputfile->count] = l; | |
114 | inputfile->maxlinewidth = | |
115 | width > inputfile->maxlinewidth ? width : inputfile->maxlinewidth; | |
116 | inputfile->count++; | |
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 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
119 | void freeinputfilebuffer(inputfile_t *inputfile) { |
19 | 120 | for (int i = 0 ; i < inputfile->count ; i++) { |
121 | free(inputfile->lines[i]); | |
122 | } | |
123 | free(inputfile->lines); | |
124 | free(inputfile); | |
0 | 125 | } |
126 | ||
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
127 | inputfile_t *readinput(char *filename) { |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
128 | |
19 | 129 | int fd = open(filename, O_RDONLY); |
130 | if (fd == -1) return NULL; | |
131 | ||
132 | inputfile_t *inputfile = inputfilebuffer(512); | |
133 | ||
134 | char buf[INPUTBUF_SIZE]; | |
135 | ssize_t r; | |
136 | ||
137 | size_t maxlinewidth = 256; | |
138 | char *line = (char*) malloc(maxlinewidth); | |
139 | size_t col = 0; | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
140 | |
19 | 141 | while ((r = read(fd, buf, INPUTBUF_SIZE)) > 0) { |
142 | for (size_t i = 0 ; i < r ; i++) { | |
143 | if (col >= maxlinewidth-4) { | |
144 | maxlinewidth <<= 1; | |
145 | line = realloc(line, maxlinewidth); | |
146 | } | |
147 | ||
148 | if (buf[i] == '\n') { | |
149 | line[col++] = '\n'; | |
150 | line[col] = 0; | |
151 | addline(inputfile, line, col); | |
152 | col = 0; | |
153 | } else { | |
154 | line[col++] = buf[i]; | |
155 | } | |
156 | } | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
157 | } |
19 | 158 | |
159 | free(line); | |
160 | ||
161 | close(fd); | |
162 | ||
163 | return inputfile; | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
164 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
165 | |
5
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
166 | size_t writeescapedchar(char *dest, size_t dp, char c) { |
19 | 167 | if (c == '>') { |
168 | dest[dp++] = '&'; dest[dp++] = 'g'; dest[dp++] = 't'; dest[dp++] = ';'; | |
169 | } else if (c == '<') { | |
170 | dest[dp++] = '&'; dest[dp++] = 'l'; dest[dp++] = 't'; dest[dp++] = ';'; | |
171 | } else { | |
172 | dest[dp++] = c; | |
173 | } | |
174 | ||
175 | return dp; | |
5
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
176 | } |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
177 | |
16
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
178 | int iskeyword(char *word, const char** keywords) { |
19 | 179 | for (int i = 0 ; keywords[i] ; i++) { |
180 | if (strncmp(keywords[i], word, WORDBUF_SIZE) == 0) { | |
181 | return 1; | |
182 | } | |
5
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
183 | } |
19 | 184 | return 0; |
5
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
185 | } |
412f1896874b
support for keywords and types
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
186 | |
9
6b1fba10c4cb
long words are correctly separated by non alpha numeric chars, underscore or hash + support for macro constants
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
187 | int iscapsonly(char *word, size_t wp) { |
19 | 188 | for (size_t i = 0 ; i < wp ; i++) { |
189 | if (!isupper(word[i]) && word[i] != '_') { | |
190 | return 0; | |
191 | } | |
9
6b1fba10c4cb
long words are correctly separated by non alpha numeric chars, underscore or hash + support for macro constants
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
192 | } |
19 | 193 | return 1; |
9
6b1fba10c4cb
long words are correctly separated by non alpha numeric chars, underscore or hash + support for macro constants
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
194 | } |
6b1fba10c4cb
long words are correctly separated by non alpha numeric chars, underscore or hash + support for macro constants
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
195 | |
16
fa0bcd0444eb
prepared java highlighting
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
196 | void parseline(char *src, char *dest, highlighter_t *highlighter) { |
19 | 197 | size_t sp = 0, dp = 0; |
198 | /* indent */ | |
199 | while (isspace(src[sp])) { | |
200 | dest[dp++] = src[sp++]; | |
201 | } | |
202 | ||
203 | static char word[WORDBUF_SIZE]; | |
204 | static char includefile[FILENAME_MAX]; | |
9
6b1fba10c4cb
long words are correctly separated by non alpha numeric chars, underscore or hash + support for macro constants
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
205 | |
19 | 206 | memset(word, 0, WORDBUF_SIZE); |
207 | size_t wp = 0, ifp = 0; | |
208 | int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0; | |
209 | static int iscommentml; | |
210 | int isescaping = 0; | |
211 | ||
212 | if (iscommentml) { | |
8 | 213 | iscomment = 1; |
214 | memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); | |
215 | dp += 29; | |
216 | } | |
19 | 217 | |
218 | for (char c = src[sp] ; c ; c=src[++sp]) { | |
219 | /* comments */ | |
220 | if (c == '/') { | |
221 | if (iscommentml && sp > 0 && src[sp-1] == '*') { | |
222 | iscomment = 0; | |
223 | iscommentml = 0; | |
224 | memcpy(&(dest[dp]), "/</span>", 8); | |
225 | dp += 8; | |
226 | continue; | |
227 | } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { | |
228 | iscomment = 1; | |
229 | iscommentml = (src[sp+1] == '*'); | |
230 | memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); | |
231 | dp += 29; | |
232 | } | |
10
925172e535a9
includes (with links in user includes)
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
233 | } |
19 | 234 | |
235 | if (iscomment) { | |
236 | if (c == '\n') { | |
237 | memcpy(&(dest[dp]), "</span>", 7); | |
238 | dp += 7; | |
239 | } | |
240 | dp = writeescapedchar(dest, dp, c); | |
241 | } else if (isinclude) { | |
242 | if (c == '<') { | |
243 | memcpy(&(dest[dp]), "<span class=\"c2html-stdinclude\">", 32); | |
244 | dp += 32; | |
245 | dp = writeescapedchar(dest, dp, c); | |
246 | } else if (c == '\"') { | |
247 | if (parseinclude) { | |
248 | dest[dp++] = '\"'; | |
249 | dest[dp++] = '>'; | |
250 | memcpy(&(dest[dp]), includefile, ifp); | |
251 | dp += ifp; | |
252 | ||
253 | dp = writeescapedchar(dest, dp, c); | |
254 | memcpy(&(dest[dp]), "</a>", 4); | |
255 | dp += 4; | |
256 | parseinclude = 0; | |
257 | } else { | |
258 | memcpy(&(dest[dp]), | |
259 | "<a class=\"c2html-userinclude\" href=", 35); | |
260 | dp += 35; | |
261 | dp = writeescapedchar(dest, dp, c); | |
262 | ifp = 0; | |
263 | includefile[ifp++] = '\"'; | |
264 | parseinclude = 1; | |
265 | } | |
266 | } else if (c == '>') { | |
267 | dp = writeescapedchar(dest, dp, c); | |
268 | memcpy(&(dest[dp]), "</span>", 7); | |
269 | dp += 7; | |
270 | } else { | |
271 | if (parseinclude) { | |
272 | includefile[ifp++] = c; | |
273 | } | |
274 | dp = writeescapedchar(dest, dp, c); | |
275 | } | |
7 | 276 | } else { |
19 | 277 | /* strings */ |
278 | if (!isescaping && (c == '\'' || c == '\"')) { | |
279 | isstring ^= 1; | |
280 | if (isstring) { | |
281 | memcpy(&(dest[dp]), "<span class=\"c2html-string\">", 28); | |
282 | dp += 28; | |
283 | dp = writeescapedchar(dest, dp, c); | |
284 | } else { | |
285 | dp = writeescapedchar(dest, dp, c); | |
286 | memcpy(&(dest[dp]), "</span>", 7); | |
287 | dp += 7; | |
288 | } | |
8 | 289 | } else { |
19 | 290 | if (isstring) { |
291 | dp = writeescapedchar(dest, dp, c); | |
292 | } else if (!iswordcharacter(c)) { | |
293 | /* interpret word int_t */ | |
294 | if (wp > 0 && wp < WORDBUF_SIZE) { | |
295 | int closespan = 1; | |
296 | if (iskeyword(word, highlighter->keywords)) { | |
297 | memcpy(&(dest[dp]), | |
298 | "<span class=\"c2html-keyword\">", 29); | |
299 | dp += 29; | |
300 | } else if (highlighter->istype(word, wp)) { | |
301 | memcpy(&(dest[dp]), | |
302 | "<span class=\"c2html-type\">", 26); | |
303 | dp += 26; | |
304 | } else if (highlighter->isdirective(word)) { | |
305 | isinclude = !strncmp( | |
306 | "#include", word, WORDBUF_SIZE); | |
307 | memcpy(&(dest[dp]), | |
308 | "<span class=\"c2html-directive\">", 31); | |
309 | dp += 31; | |
310 | } else if (iscapsonly(word, wp)) { | |
311 | memcpy(&(dest[dp]), | |
312 | "<span class=\"c2html-macroconst\">", 32); | |
313 | dp += 32; | |
314 | } else { | |
315 | closespan = 0; | |
316 | } | |
317 | for (int i = 0 ; i < wp ; i++) { | |
318 | dp = writeescapedchar(dest, dp, word[i]); | |
319 | } | |
320 | if (closespan) { | |
321 | memcpy(&(dest[dp]), "</span>", 7); | |
322 | dp += 7; | |
323 | } | |
324 | } | |
325 | memset(word, 0, WORDBUF_SIZE); | |
326 | wp = 0; | |
327 | dp = writeescapedchar(dest, dp, c); | |
328 | } else { | |
329 | /* read word */ | |
330 | if (wp < WORDBUF_SIZE) { | |
331 | word[wp++] = c; | |
332 | } else if (wp == WORDBUF_SIZE) { | |
333 | for (int i = 0 ; i < WORDBUF_SIZE ; i++) { | |
334 | dp = writeescapedchar(dest, dp, word[i]); | |
335 | } | |
336 | wp++; | |
337 | dp = writeescapedchar(dest, dp, c); | |
338 | } else { | |
339 | dp = writeescapedchar(dest, dp, c); | |
340 | } | |
341 | } | |
8 | 342 | } |
19 | 343 | |
344 | isescaping = !isescaping & (c == '\\'); | |
7 | 345 | } |
4 | 346 | } |
19 | 347 | dest[dp] = 0; |
4 | 348 | } |
349 | ||
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
350 | void printhelp() { |
19 | 351 | printf("Formats source code using HTML.\n\nUsage:\n" |
352 | " c2html [Options] FILE\n\n" | |
353 | " Options:\n" | |
354 | " -h Prints this help message\n" | |
355 | " -j Highlight Java instead of C source code\n" | |
356 | " -o <output> Output file (stdout, if not specified)\n" | |
357 | " -p Disable highlighting (plain text)\n" | |
358 | "\n"); | |
359 | ||
360 | ||
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
361 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
362 | |
4 | 363 | int lnint(size_t lnc) { |
19 | 364 | int w = 1, p = 1; |
365 | while ((p*=10) < lnc) w++; | |
366 | return w; | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
367 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
368 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
369 | int main(int argc, char** argv) { |
19 | 370 | |
371 | settings_t settings; | |
372 | settings.outfilename = NULL; | |
373 | settings.highlight = 1; | |
374 | ||
375 | highlighter_t highlighter; | |
376 | highlighter.isdirective = iscdirective; | |
377 | highlighter.istype = isctype; | |
378 | highlighter.keywords = ckeywords; | |
379 | ||
380 | char optc; | |
381 | while ((optc = getopt(argc, argv, "hjo:p")) != -1) { | |
382 | switch (optc) { | |
383 | case 'o': | |
384 | if (!(optarg[0] == '-' && optarg[1] == 0)) { | |
385 | settings.outfilename = optarg; | |
386 | } | |
387 | break; | |
388 | case 'j': | |
389 | highlighter.isdirective = isjdirective; | |
390 | highlighter.istype = isjtype; | |
391 | highlighter.keywords = jkeywords; | |
392 | break; | |
393 | case 'p': | |
394 | settings.highlight = 0; | |
395 | break; | |
396 | case 'h': | |
397 | printhelp(); | |
398 | return 0; | |
399 | default: | |
400 | return 1; | |
11 | 401 | } |
19 | 402 | } |
403 | ||
404 | if (optind != argc-1) { | |
11 | 405 | printhelp(); |
406 | return 1; | |
19 | 407 | } else { |
408 | settings.infilename = argv[optind]; | |
11 | 409 | |
19 | 410 | inputfile_t *inputfile = readinput(settings.infilename); |
411 | if (inputfile) { | |
412 | FILE *fout; | |
413 | char *line; | |
414 | if (settings.highlight) { | |
415 | line = (char*) malloc(inputfile->maxlinewidth*64); | |
416 | } else { | |
417 | line = NULL; | |
418 | } | |
419 | if (settings.outfilename) { | |
420 | fout = fopen(settings.outfilename, "w"); | |
421 | } else { | |
422 | fout = stdout; | |
423 | } | |
424 | fprintf(fout, "<pre>\n"); | |
425 | int lnw = lnint(inputfile->count); | |
426 | for (int i = 0 ; i < inputfile->count ; i++) { | |
427 | if (settings.highlight) { | |
428 | parseline(inputfile->lines[i], line, &highlighter); | |
429 | } else { | |
430 | line = inputfile->lines[i]; | |
431 | } | |
432 | fprintf(fout, "<span class=\"c2html-lineno\">%*d:</span> %s", | |
433 | lnw, i+1, line); | |
434 | } | |
435 | if (settings.highlight) { | |
436 | free(line); | |
437 | } | |
438 | fprintf(fout, "</pre>\n"); | |
439 | ||
440 | if (fout != stdout) { | |
441 | fclose(fout); | |
442 | } | |
443 | ||
444 | freeinputfilebuffer(inputfile); | |
12 | 445 | } |
19 | 446 | |
447 | return 0; | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
448 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
449 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
450 |