Tue, 23 Aug 2016 12:05:41 +0200
adds version number and changes build system
23 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
35 | 4 | * Copyright 2016 Mike Becker. All rights reserved. |
23 | 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 "c2html.h" |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
30 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
31 | inputfile_t *inputfilebuffer(size_t capacity) { |
19 | 32 | inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t)); |
33 | inputfile->lines = (char**) malloc(capacity * sizeof(char*)); | |
34 | inputfile->capacity = capacity; | |
35 | inputfile->count = 0; | |
36 | inputfile->maxlinewidth = 0; | |
37 | ||
38 | return inputfile; | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
39 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
40 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
41 | void addline(inputfile_t *inputfile, char* line, size_t width) { |
19 | 42 | char *l = (char*) malloc(width+1); |
43 | memcpy(l, line, width); | |
44 | l[width] = 0; | |
45 | if (inputfile->count >= inputfile->capacity) { | |
46 | inputfile->capacity <<= 1; | |
25
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
24
diff
changeset
|
47 | inputfile->lines = realloc(inputfile->lines, |
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
24
diff
changeset
|
48 | sizeof(char*)*inputfile->capacity); |
19 | 49 | } |
50 | inputfile->lines[inputfile->count] = l; | |
51 | inputfile->maxlinewidth = | |
52 | width > inputfile->maxlinewidth ? width : inputfile->maxlinewidth; | |
53 | inputfile->count++; | |
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 | |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
56 | void freeinputfilebuffer(inputfile_t *inputfile) { |
19 | 57 | for (int i = 0 ; i < inputfile->count ; i++) { |
58 | free(inputfile->lines[i]); | |
59 | } | |
60 | free(inputfile->lines); | |
61 | free(inputfile); | |
0 | 62 | } |
63 | ||
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
64 | inputfile_t *readinput(char *filename) { |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
65 | |
19 | 66 | int fd = open(filename, O_RDONLY); |
67 | if (fd == -1) return NULL; | |
68 | ||
69 | inputfile_t *inputfile = inputfilebuffer(512); | |
70 | ||
71 | char buf[INPUTBUF_SIZE]; | |
72 | ssize_t r; | |
73 | ||
74 | size_t maxlinewidth = 256; | |
75 | char *line = (char*) malloc(maxlinewidth); | |
76 | size_t col = 0; | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
77 | |
19 | 78 | while ((r = read(fd, buf, INPUTBUF_SIZE)) > 0) { |
79 | for (size_t i = 0 ; i < r ; i++) { | |
80 | if (col >= maxlinewidth-4) { | |
81 | maxlinewidth <<= 1; | |
82 | line = realloc(line, maxlinewidth); | |
83 | } | |
84 | ||
85 | if (buf[i] == '\n') { | |
86 | line[col++] = '\n'; | |
87 | line[col] = 0; | |
88 | addline(inputfile, line, col); | |
89 | col = 0; | |
90 | } else { | |
91 | line[col++] = buf[i]; | |
92 | } | |
93 | } | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
94 | } |
19 | 95 | |
96 | free(line); | |
97 | ||
98 | close(fd); | |
99 | ||
100 | return inputfile; | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
101 | } |
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 | void printhelp() { |
19 | 104 | printf("Formats source code using HTML.\n\nUsage:\n" |
105 | " c2html [Options] FILE\n\n" | |
106 | " Options:\n" | |
107 | " -h Prints this help message\n" | |
108 | " -j Highlight Java instead of C source code\n" | |
109 | " -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
|
110 | " -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
|
111 | " -F <footer> Append footer file\n" |
19 | 112 | " -p Disable highlighting (plain text)\n" |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
113 | " -l Disable line numbers\n" |
37
1a67185e5496
adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
114 | " -V, -v Prints version and exits\n" |
19 | 115 | "\n"); |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
116 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
117 | |
4 | 118 | int lnint(size_t lnc) { |
19 | 119 | int w = 1, p = 1; |
120 | while ((p*=10) < lnc) w++; | |
121 | return w; | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
122 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
123 | |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
124 | 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
|
125 | if (!filename) { |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
126 | return 0; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
127 | } |
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 | 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
|
130 | if (src) { |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
131 | char buf[4096]; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
132 | int r; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
133 | 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
|
134 | 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
|
135 | } |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
136 | fclose(src); |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
137 | return 0; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
138 | } else { |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
139 | return -1; |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
140 | } |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
141 | } |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
142 | |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
143 | #define WRITECONST(stream, out, cstr) out(cstr, 1, sizeof(cstr)-1, stream) |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
144 | int formatfile( |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
145 | highlighter_t *highlighter, |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
146 | inputfile_t *in, |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
147 | fmt_write_func out, |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
148 | void *stream, |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
149 | _Bool showln) { |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
150 | // formats an input file and writes the result to out |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
151 | |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
152 | char *line = malloc(in->maxlinewidth*64); |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
153 | if(!line) { |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
154 | return 1; |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
155 | } |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
156 | WRITECONST(stream, out, "<pre>\n"); |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
157 | |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
158 | int lnw = lnint(in->count); |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
159 | for (int i = 0 ; i < in->count ; i++) { |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
160 | if (highlighter) { |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
161 | highlighter->parser(in->lines[i], line, highlighter); |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
162 | } else { |
30
0bfd4d6f086a
added HTML escaping for plain text files
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
163 | char *c = in->lines[i]; |
0bfd4d6f086a
added HTML escaping for plain text files
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
164 | size_t dp = 0; |
0bfd4d6f086a
added HTML escaping for plain text files
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
165 | while (*c) { |
0bfd4d6f086a
added HTML escaping for plain text files
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
166 | dp = writeescapedchar(line, dp, *c); |
0bfd4d6f086a
added HTML escaping for plain text files
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
167 | c++; |
0bfd4d6f086a
added HTML escaping for plain text files
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
168 | } |
0bfd4d6f086a
added HTML escaping for plain text files
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
169 | line[dp] = '\0'; |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
170 | } |
30
0bfd4d6f086a
added HTML escaping for plain text files
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
171 | |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
172 | // write line number |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
173 | if (showln) { |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
174 | WRITECONST(stream, out, "<span class=\"c2html-lineno\">"); |
27
53fd8595378c
added links for line numbers
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
175 | char lnbuf[128]; |
53fd8595378c
added links for line numbers
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
176 | int len; |
53fd8595378c
added links for line numbers
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
177 | // line number link |
53fd8595378c
added links for line numbers
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
178 | len = snprintf(lnbuf, 128, "<a name=\"l%d\" href=\"#l%d\">", |
53fd8595378c
added links for line numbers
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
179 | i+1, i+1); |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
180 | out(lnbuf, 1, len, stream); |
27
53fd8595378c
added links for line numbers
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
181 | // justified line number |
53fd8595378c
added links for line numbers
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
182 | len = snprintf(lnbuf, 128, "%*d ", lnw, i+1); |
53fd8595378c
added links for line numbers
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
183 | out(lnbuf, 1, len, stream); |
53fd8595378c
added links for line numbers
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
184 | WRITECONST(stream, out, "</a></span> "); |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
185 | } |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
186 | |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
187 | // write formated (or plain) code line |
30
0bfd4d6f086a
added HTML escaping for plain text files
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
188 | out(line, 1, strlen(line), stream); |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
189 | } |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
190 | |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
191 | WRITECONST(stream, out, "</pre>\n"); |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
192 | free(line); |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
193 | return 0; |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
194 | } |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
195 | |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
196 | void init_c_highlighter(highlighter_t *highlighter) { |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
197 | memset(highlighter, 0, sizeof(highlighter_t)); |
36
be60c22cddfe
fixed possible naming conflicts with is.* functions
Mike Becker <universe@uap-core.de>
parents:
35
diff
changeset
|
198 | highlighter->isdirective = check_cdirective; |
be60c22cddfe
fixed possible naming conflicts with is.* functions
Mike Becker <universe@uap-core.de>
parents:
35
diff
changeset
|
199 | highlighter->istype = check_ctype; |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
200 | highlighter->keywords = ckeywords; |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
201 | highlighter->parser = cparseline; |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
202 | } |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
203 | |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
204 | void init_java_highlighter(highlighter_t *highlighter) { |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
205 | memset(highlighter, 0, sizeof(highlighter_t)); |
36
be60c22cddfe
fixed possible naming conflicts with is.* functions
Mike Becker <universe@uap-core.de>
parents:
35
diff
changeset
|
206 | highlighter->isdirective = check_jdirective; |
be60c22cddfe
fixed possible naming conflicts with is.* functions
Mike Becker <universe@uap-core.de>
parents:
35
diff
changeset
|
207 | highlighter->istype = check_jtype; |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
208 | highlighter->keywords = jkeywords; |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
209 | highlighter->parser = jparseline; |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
210 | } |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
211 | |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
212 | 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
|
213 | int retcode = EXIT_SUCCESS; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
214 | |
19 | 215 | settings_t settings; |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
216 | memset(&settings, 0, sizeof(settings)); |
19 | 217 | settings.highlight = 1; |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
218 | settings.showlinenumbers = 1; |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
219 | |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
220 | int lang = C2HTML_C; |
19 | 221 | |
222 | char optc; | |
37
1a67185e5496
adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
223 | while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) { |
19 | 224 | switch (optc) { |
225 | case 'o': | |
226 | if (!(optarg[0] == '-' && optarg[1] == 0)) { | |
227 | settings.outfilename = optarg; | |
228 | } | |
229 | break; | |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
230 | case 'F': |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
231 | settings.footerfile = optarg; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
232 | break; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
233 | case 'H': |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
234 | settings.headerfile = optarg; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
235 | break; |
19 | 236 | case 'j': |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
237 | lang = C2HTML_JAVA; |
19 | 238 | break; |
239 | case 'p': | |
240 | settings.highlight = 0; | |
241 | break; | |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
242 | case 'l': |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
243 | settings.showlinenumbers = 0; |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
244 | break; |
19 | 245 | case 'h': |
246 | printhelp(); | |
247 | return 0; | |
37
1a67185e5496
adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
248 | case 'v': |
1a67185e5496
adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
249 | case 'V': |
1a67185e5496
adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
250 | #ifdef VERSION_DEVELOP |
1a67185e5496
adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
251 | printf("%d.%d (unstable)\n", VERSION_MAJOR, VERSION_MINOR); |
1a67185e5496
adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
252 | #else |
1a67185e5496
adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
253 | printf("%d.%d\n", VERSION_MAJOR, VERSION_MINOR); |
1a67185e5496
adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
254 | #endif |
1a67185e5496
adds version number and changes build system
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
255 | return 0; |
19 | 256 | default: |
257 | return 1; | |
11 | 258 | } |
19 | 259 | } |
260 | ||
261 | if (optind != argc-1) { | |
11 | 262 | printhelp(); |
263 | return 1; | |
19 | 264 | } else { |
265 | 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
|
266 | FILE *fout; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
267 | if (settings.outfilename) { |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
268 | 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
|
269 | if (!fout) { |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
270 | perror("Error opening output file"); |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
271 | return -1; |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
272 | } |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
273 | } else { |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
274 | fout = stdout; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
275 | } |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
276 | |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
277 | 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
|
278 | perror("Error opening header file"); |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
279 | retcode = -1; |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
280 | goto prog_end; |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
281 | } |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
282 | |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
283 | highlighter_t highlighter; |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
284 | highlighter_t *hptr = &highlighter; |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
285 | switch (lang) { |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
286 | case C2HTML_C: |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
287 | init_c_highlighter(&highlighter); |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
288 | break; |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
289 | case C2HTML_JAVA: |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
290 | init_java_highlighter(&highlighter); |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
291 | break; |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
292 | default: |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
293 | hptr = NULL; |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
294 | break; |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
295 | } |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
296 | if (!settings.highlight) { |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
297 | hptr = NULL; |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
298 | } |
11 | 299 | |
19 | 300 | inputfile_t *inputfile = readinput(settings.infilename); |
301 | if (inputfile) { | |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
302 | formatfile( |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
303 | hptr, |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
304 | inputfile, |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
305 | (fmt_write_func)fwrite, |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
306 | fout, |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
307 | settings.showlinenumbers); |
25
f82aa7afe872
more and better test cases + fixed memory leak introduced by changeset e43dee5892f4
Mike Becker <universe@uap-core.de>
parents:
24
diff
changeset
|
308 | freeinputfilebuffer(inputfile); |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
309 | } else { |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
310 | perror("Error opening input file"); |
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
311 | retcode = -1; |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
312 | } |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
313 | |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
314 | if (copyfile(settings.footerfile, fout)) { |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
315 | perror("Error opening footer file"); |
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
316 | retcode = -1; |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
317 | } |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
318 | |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
319 | prog_end: |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
320 | if (fout != stdout) { |
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
321 | fclose(fout); |
12 | 322 | } |
19 | 323 | |
22
f463693b5eeb
added command line parameters for header and footer file
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
324 | return retcode; |
1
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
325 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
326 | } |
12c482ea4fc4
first prototype: creates unformatted output with line numbers
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
327 |