Thu, 11 Jul 2024 20:15:56 +0200
Added tag v3.1 for changeset 5343a527ae96
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" | |
66 | 36 | #include <cx/utils.h> |
55 | 37 | |
38 | typedef struct { | |
39 | char* outfilename; | |
40 | char* headerfile; | |
41 | char* footerfile; | |
42 | char* infilename; | |
43 | int showlinenumbers; | |
44 | } Settings; | |
45 | ||
66 | 46 | static int appendfile(const char *filename, FILE *fout, const char *errmsg) { |
82
ce67b1b2e979
hotfix: do not try to append file when none is specified
Mike Becker <universe@uap-core.de>
parents:
78
diff
changeset
|
47 | if (filename == NULL) return 0; // nothing to append |
66 | 48 | FILE *fin = fopen(filename, "r"); |
49 | if (!fin) { | |
55 | 50 | perror(errmsg); |
51 | if (fout != stdout) { | |
52 | fclose(fout); | |
53 | } | |
54 | return 1; | |
55 | } | |
66 | 56 | cx_stream_copy(fin, fout, (cx_read_func) fread, (cx_write_func) fwrite); |
57 | fclose(fin); | |
55 | 58 | return 0; |
59 | } | |
60 | ||
61 | static void printhelp() { | |
62 | printf("Formats source code using HTML.\n\nUsage:\n" | |
63 | " c2html [Options] FILE\n\n" | |
64 | " Options:\n" | |
65 | " -h Prints this help message\n" | |
66 | " -j Highlight Java instead of C source code\n" | |
67 | " -o <output> Output file (stdout, if not specified)\n" | |
68 | " -H <header> Prepend header file\n" | |
69 | " -F <footer> Append footer file\n" | |
70 | " -p Disable highlighting (plain text)\n" | |
71 | " -l Disable line numbers\n" | |
72 | " -V, -v Prints version and exits\n" | |
73 | "\n"); | |
74 | } | |
75 | ||
76 | int main(int argc, char** argv) { | |
77 | ||
78 | /* Default settings */ | |
79 | Settings settings; | |
80 | memset(&settings, 0, sizeof(settings)); | |
81 | settings.showlinenumbers = 1; | |
82 | c2html_highlighter_func hltr = c2html_c_highlighter; | |
83 | ||
84 | /* Parse command line */ | |
66 | 85 | int optc; |
55 | 86 | while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) { |
87 | switch (optc) { | |
88 | case 'o': | |
89 | if (!(optarg[0] == '-' && optarg[1] == 0)) { | |
90 | settings.outfilename = optarg; | |
91 | } | |
92 | break; | |
93 | case 'F': | |
94 | settings.footerfile = optarg; | |
95 | break; | |
96 | case 'H': | |
97 | settings.headerfile = optarg; | |
98 | break; | |
99 | case 'j': | |
100 | hltr = c2html_java_highlighter; | |
101 | break; | |
102 | case 'p': | |
103 | hltr = c2html_plain_highlighter; | |
104 | break; | |
105 | case 'l': | |
106 | settings.showlinenumbers = 0; | |
107 | break; | |
108 | case 'h': | |
109 | printhelp(); | |
110 | return EXIT_SUCCESS; | |
111 | case 'v': | |
112 | case 'V': | |
78 | 113 | #if VERSION_DEVELOP > 0 |
55 | 114 | printf("%d.%d (unstable)\n", VERSION_MAJOR, VERSION_MINOR); |
115 | #else | |
116 | printf("%d.%d\n", VERSION_MAJOR, VERSION_MINOR); | |
117 | #endif | |
118 | return EXIT_SUCCESS; | |
119 | default: | |
120 | return EXIT_FAILURE; | |
121 | } | |
122 | } | |
123 | ||
124 | if (optind != argc-1) { | |
125 | printhelp(); | |
126 | return EXIT_FAILURE; | |
127 | } else { | |
128 | /* Open output file */ | |
129 | settings.infilename = argv[optind]; | |
130 | FILE *fout; | |
131 | if (settings.outfilename) { | |
132 | fout = fopen(settings.outfilename, "w"); | |
133 | if (!fout) { | |
134 | perror("Error opening output file"); | |
135 | return EXIT_FAILURE; | |
136 | } | |
137 | } else { | |
138 | fout = stdout; | |
139 | } | |
140 | ||
141 | /* Prepend header file */ | |
66 | 142 | if (appendfile(settings.headerfile, fout, |
55 | 143 | "Error opening header file")) { |
144 | return EXIT_FAILURE; | |
145 | } | |
146 | ||
147 | /* Process input file */ | |
148 | FILE *inputfile = fopen(settings.infilename, "r"); | |
149 | if (inputfile) { | |
66 | 150 | CxBuffer fbuf; |
151 | cxBufferInit(&fbuf, NULL, 4096, NULL, CX_BUFFER_AUTO_EXTEND); | |
152 | cx_stream_copy(inputfile, &fbuf, (cx_read_func) fread, | |
153 | (cx_write_func) cxBufferWrite); | |
70
60cecca5e484
fix illegal memory access when input file does not end with line break
Mike Becker <universe@uap-core.de>
parents:
67
diff
changeset
|
154 | cxBufferPut(&fbuf, 0); |
66 | 155 | fclose(inputfile); |
70
60cecca5e484
fix illegal memory access when input file does not end with line break
Mike Becker <universe@uap-core.de>
parents:
67
diff
changeset
|
156 | c2html_textformat( |
60cecca5e484
fix illegal memory access when input file does not end with line break
Mike Becker <universe@uap-core.de>
parents:
67
diff
changeset
|
157 | fbuf.space, fout, (cx_write_func ) fwrite, hltr, |
66 | 158 | settings.showlinenumbers |
55 | 159 | ); |
66 | 160 | cxBufferDestroy(&fbuf); |
55 | 161 | } else { |
162 | perror("Error opening input file"); | |
163 | if (fout != stdout) { | |
164 | fclose(fout); | |
165 | } | |
166 | return EXIT_FAILURE; | |
167 | } | |
168 | ||
169 | /* Append footer file */ | |
66 | 170 | if (appendfile(settings.footerfile, fout, |
55 | 171 | "Error opening footer file")) { |
172 | return EXIT_FAILURE; | |
173 | } | |
174 | ||
175 | return EXIT_SUCCESS; | |
176 | } | |
177 | } | |
178 |