Thu, 10 Nov 2016 18:44:48 +0100
removes span around line number
55 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
4 | * Copyright 2016 Mike Becker. All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | * | |
28 | */ | |
29 | ||
30 | #include <stdio.h> | |
31 | #include <stdlib.h> | |
32 | #include <unistd.h> | |
33 | #include <string.h> | |
34 | ||
35 | #include "c2html.h" | |
36 | #include "ucx/utils.h" | |
37 | ||
38 | #define FILEBUF_SIZE 4096 | |
39 | ||
40 | typedef struct { | |
41 | char* outfilename; | |
42 | char* headerfile; | |
43 | char* footerfile; | |
44 | char* infilename; | |
45 | int showlinenumbers; | |
46 | } Settings; | |
47 | ||
48 | static int appendfile(const char *filename, FILE *fout, | |
49 | char *copybuf, size_t copybuflen, const char *errmsg) { | |
50 | FILE *headerfile = fopen(filename, "r"); | |
51 | if (!headerfile) { | |
52 | perror(errmsg); | |
53 | if (fout != stdout) { | |
54 | fclose(fout); | |
55 | } | |
56 | return 1; | |
57 | } | |
58 | ucx_stream_copy(headerfile, fout, | |
59 | (read_func) fread, (write_func) fwrite, | |
60 | copybuf, copybuflen, (size_t)-1); | |
61 | fclose(headerfile); | |
62 | return 0; | |
63 | } | |
64 | ||
65 | static void printhelp() { | |
66 | printf("Formats source code using HTML.\n\nUsage:\n" | |
67 | " c2html [Options] FILE\n\n" | |
68 | " Options:\n" | |
69 | " -h Prints this help message\n" | |
70 | " -j Highlight Java instead of C source code\n" | |
71 | " -o <output> Output file (stdout, if not specified)\n" | |
72 | " -H <header> Prepend header file\n" | |
73 | " -F <footer> Append footer file\n" | |
74 | " -p Disable highlighting (plain text)\n" | |
75 | " -l Disable line numbers\n" | |
76 | " -V, -v Prints version and exits\n" | |
77 | "\n"); | |
78 | } | |
79 | ||
80 | int main(int argc, char** argv) { | |
81 | ||
82 | /* Default settings */ | |
83 | Settings settings; | |
84 | memset(&settings, 0, sizeof(settings)); | |
85 | settings.showlinenumbers = 1; | |
86 | c2html_highlighter_func hltr = c2html_c_highlighter; | |
87 | ||
88 | /* Parse command line */ | |
89 | char optc; | |
90 | while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) { | |
91 | switch (optc) { | |
92 | case 'o': | |
93 | if (!(optarg[0] == '-' && optarg[1] == 0)) { | |
94 | settings.outfilename = optarg; | |
95 | } | |
96 | break; | |
97 | case 'F': | |
98 | settings.footerfile = optarg; | |
99 | break; | |
100 | case 'H': | |
101 | settings.headerfile = optarg; | |
102 | break; | |
103 | case 'j': | |
104 | hltr = c2html_java_highlighter; | |
105 | break; | |
106 | case 'p': | |
107 | hltr = c2html_plain_highlighter; | |
108 | break; | |
109 | case 'l': | |
110 | settings.showlinenumbers = 0; | |
111 | break; | |
112 | case 'h': | |
113 | printhelp(); | |
114 | return EXIT_SUCCESS; | |
115 | case 'v': | |
116 | case 'V': | |
117 | #ifdef VERSION_DEVELOP | |
118 | printf("%d.%d (unstable)\n", VERSION_MAJOR, VERSION_MINOR); | |
119 | #else | |
120 | printf("%d.%d\n", VERSION_MAJOR, VERSION_MINOR); | |
121 | #endif | |
122 | return EXIT_SUCCESS; | |
123 | default: | |
124 | return EXIT_FAILURE; | |
125 | } | |
126 | } | |
127 | ||
128 | if (optind != argc-1) { | |
129 | printhelp(); | |
130 | return EXIT_FAILURE; | |
131 | } else { | |
132 | /* Open output file */ | |
133 | settings.infilename = argv[optind]; | |
134 | FILE *fout; | |
135 | if (settings.outfilename) { | |
136 | fout = fopen(settings.outfilename, "w"); | |
137 | if (!fout) { | |
138 | perror("Error opening output file"); | |
139 | return EXIT_FAILURE; | |
140 | } | |
141 | } else { | |
142 | fout = stdout; | |
143 | } | |
144 | ||
145 | /* Allocate file buffer */ | |
146 | char *filebuf = malloc(FILEBUF_SIZE); | |
147 | if (!filebuf) { | |
148 | perror("Error allocating file buffer"); | |
149 | return EXIT_FAILURE; | |
150 | } | |
151 | ||
152 | /* Prepend header file */ | |
153 | if (appendfile(settings.headerfile, fout, filebuf, FILEBUF_SIZE, | |
154 | "Error opening header file")) { | |
155 | return EXIT_FAILURE; | |
156 | } | |
157 | ||
158 | /* Process input file */ | |
159 | FILE *inputfile = fopen(settings.infilename, "r"); | |
160 | if (inputfile) { | |
57
eba880c1705c
improves API and adds functions for strings
Mike Becker <universe@uap-core.de>
parents:
55
diff
changeset
|
161 | c2html_fformatf( |
55 | 162 | inputfile, filebuf, FILEBUF_SIZE, |
163 | fout, hltr, settings.showlinenumbers | |
164 | ); | |
165 | fclose(inputfile); | |
166 | } else { | |
167 | perror("Error opening input file"); | |
168 | if (fout != stdout) { | |
169 | fclose(fout); | |
170 | } | |
171 | return EXIT_FAILURE; | |
172 | } | |
173 | ||
174 | /* Append footer file */ | |
175 | if (appendfile(settings.footerfile, fout, filebuf, FILEBUF_SIZE, | |
176 | "Error opening footer file")) { | |
177 | return EXIT_FAILURE; | |
178 | } | |
179 | ||
180 | free(filebuf); | |
181 | ||
182 | return EXIT_SUCCESS; | |
183 | } | |
184 | } | |
185 |