Repository navigation
Expand file tree
/
Copy pathunhtml.c
More file actions
347 lines (299 loc) · 8.71 KB
/
Copy pathunhtml.c
File metadata and controls
347 lines (299 loc) · 8.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* SPDX-License-Identifier: MIT */
/* SPDX-FileCopyrightText: (c) Copyright 2024 Andrew Bower <andrew@bower.uk> */
/* unhtml: remove HTML tags from document, emitting plain text.
*
* Version 3.0 onwards of unhtml is a complete rewrite in 2024 of v2.3.9 of
* unhtml written by Kevin Swan in 1998. No portion of the original software
* remains but the user interface and behaviour is compatible.
*/
#include <fcntl.h>
#include <getopt.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include "unhtml.h"
#include "load.h"
#include "config.h"
#include "parse-gumbo.h"
#include "parse-libxml2.h"
enum opt:int {
OPT_VERSION = 0x1000,
OPT_HELP,
OPT_COMMENT,
OPT_CDATA,
OPT_PARSER,
OPT_VERBOSE,
OPT_CONFDIR,
OPT_RENDER,
};
struct options opt;
const char *render_mode_names[RENDER_MODE_MAX] = {
[RENDER_MODE_LITERAL] = "literal",
[RENDER_MODE_SMART_SPACE] = "smart-space",
};
const struct parser_defn *parser_defs[] = {
LIBXML2_PARSERS
GUMBO_PARSERS
};
static constexpr size_t num_parsers = sizeof parser_defs/sizeof *parser_defs;
static struct parser parsers[num_parsers];
static const char *version_str = STRINGIFY(UNHTML_VERSION);
static void usage(FILE *out) {
fprintf(out,
"usage: %s -version show version information\n"
" %s -help show help\n"
" %s [OPTIONS] [FILENAME] process FILENAME or stdin\n\n"
"OPTIONS\n"
" -verbose show verbose output\n"
" -comment include comments\n"
" -cdata=comment treat CDATA sections as comment\n"
" -cdata=text treat CDATA sections as text (default)\n"
" -parser=PARSER use PARSER parser\n"
" -confdir=CONFDIR set configuration search path; subsequently prepend to it\n"
" -render=MODE set rendering mode\n"
,
program_invocation_short_name,
program_invocation_short_name,
program_invocation_short_name);
}
static void version(FILE *out) {
fprintf(out,
"unhtml-%s (c) Copyright 2024, Andrew Bower <andrew@bower.uk>\n"
"Remove html tags from document and render as UTF-8 plain text.\n"
"Compatible replacement inspired by unhtml-2.3.9, Kevin Swan 1998.\n",
version_str);
}
void list_parsers(FILE *stream) {
fprintf(stream, "Parsers:\n");
for (int i = 0; i < num_parsers; i++)
fprintf(stream, " %s\n", parser_defs[i]->name);
}
void list_render_modes(FILE *stream) {
fprintf(stream, "Render modes:\n");
for (int i = 0; i < RENDER_MODE_MAX; i++)
fprintf(stream, " %s\n", render_mode_names[i]);
}
int find_parser(const char *name) {
int i;
for (i = 0; i < num_parsers && strcmp(parser_defs[i]->name, name); i++);
return i == num_parsers ? -1 : i;
}
void init_parsers(void) {
int i;
memset(&parsers, '\0', sizeof parsers);
for (i = 0; i < num_parsers; i++) {
struct parser *p = parsers + i;
int rc;
p->def = parser_defs[i];
if (p->def->imatch_pat) {
rc = regcomp(&p->match_re, p->def->imatch_pat, REG_EXTENDED | REG_ICASE);
if (rc != 0)
logv("error compiling regex for choosing %s parser\n", p->def->name);
else
p->has_matcher = true;
}
}
}
void parser_match(struct mapped_buffer *input) {
int i;
for (i = 0; i < num_parsers; i++) {
struct parser *p = parsers + i;
regmatch_t matches[1] = {
{ 0, input->length - 1 }
};
/* Require the DOCTYPE or <?xml> prolog to be within the first 1KB of
* the input. These should occur before any other content anyway but
* comments can often be found beforehand so let's match liberally. */
if (matches[0].rm_eo > 1024)
matches[0].rm_eo = 1024;
if (p->has_matcher &&
regexec(&p->match_re,
input->data,
0, matches,
REG_STARTEND) == 0)
break;
}
if (i != num_parsers) {
opt.parser = i;
logv("selected '%s' parser based on content\n",
parsers[i].def->name);
} else {
opt.parser = -1;
logv("no parser matched, using default\n");
}
}
void free_parsers(void) {
for (int i = 0; i < num_parsers; i++) {
if (parsers[i].has_matcher)
regfree(&parsers[i].match_re);
}
memset(parsers, '\0', sizeof parsers);
}
static void parse_options(int argc, char *argv[]) {
const struct option options[] = {
{ "version", no_argument, 0, OPT_VERSION },
{ "help", no_argument, 0, OPT_HELP },
{ "comment", no_argument, 0, OPT_COMMENT },
{ "cdata", required_argument, 0, OPT_CDATA },
{ "parser", required_argument, 0, OPT_PARSER },
{ "verbose", no_argument, 0, OPT_VERBOSE },
{ "confdir", required_argument, 0, OPT_CONFDIR },
{ "render", required_argument, 0, OPT_RENDER },
{ nullptr }
};
int option_index;
int c;
memset(&opt, '\0', sizeof opt);
opt.parser = -1;
do {
c = getopt_long_only(argc, argv, "", options, &option_index);
switch (c) {
case OPT_VERSION:
opt.version = true;
break;
case OPT_HELP:
opt.help = true;
break;
case OPT_COMMENT:
opt.comment = true;
break;
case OPT_CDATA:
if (!strcmp(optarg, "comment"))
opt.cdata_is_comment = true;
else if (!strcmp(optarg, "text"))
opt.cdata_is_comment = false;
else
opt.error = true;
break;
case OPT_PARSER:
if ((opt.parser = find_parser(optarg)) == -1) {
fprintf(stderr, "no such parser: %s\n", optarg);
list_parsers(stderr);
opt.error = true;
}
break;
case OPT_VERBOSE:
opt.verbosity++;
break;
case OPT_CONFDIR:
{
struct config_dir *cd = calloc(1, sizeof *cd);
if (cd == NULL) {
fprintf(stderr, "could not allocate node: %s\n", strerror(errno));
opt.error = true;
} else {
if (optarg[0] == '+' && !opt.confdirs) {
cd->dir = optarg + 1;
cd->next = get_defconf();
} else {
cd->dir = optarg;
cd->next = opt.confdirs;
}
cd->node_needs_free = true;
opt.confdirs = cd;
}
}
break;
case OPT_RENDER:
for (opt.render_mode = 0;
opt.render_mode < RENDER_MODE_MAX &&
strcmp(optarg, render_mode_names[opt.render_mode]);
opt.render_mode++);
if (opt.render_mode == RENDER_MODE_MAX)
opt.error = true;
break;
case -1:
/* EOF */
break;
default:
opt.error = true;
}
} while (c != -1 && c != '?' && c != ':');
if (c != -1)
opt.error = true;
if (optind < argc)
opt.file = argv[optind++];
if (optind < argc)
opt.error = true;
}
static void free_options(void) {
struct config_dir **cdp, *cd;
for (cdp = &opt.confdirs; (cd = *cdp);) {
*cdp = cd->next;
if (cd->name_needs_free)
free(cd->dir);
if (cd->node_needs_free)
free(cd);
}
}
static size_t max_input_buffer(void) {
struct rlimit max_mem;
size_t max_buf;
int rc;
rc = getrlimit(RLIMIT_AS, &max_mem);
if (rc == -1) {
fprintf(stderr, "could not get limits with getrlimit(), %s\n",
strerror(errno));
max_mem.rlim_cur = 0x4000'0000ul;
}
/* The html parser will use at least as much memory as the input buffer so
* limit to a quarter of available virtual memory. */
max_buf = max_mem.rlim_cur == RLIM_INFINITY ? SIZE_MAX : max_mem.rlim_cur;
max_buf >>= 2;
/* According to gumbo.h, the maximum input buffer size is 4GB */
if (max_buf > 0xFFFF'FFFFul)
max_buf = 0xFFFF'FFFFul;
return max_buf;
}
int main(int argc, char *argv[]) {
struct mapped_buffer input;
size_t max_buf;
int rc;
max_buf = max_input_buffer();
init_parsers();
parse_options(argc, argv);
if (opt.error) {
usage(stderr);
free_options();
return EXIT_FAILURE;
}
if (opt.help) {
usage(stdout);
puts("");
version(stdout);
fprintf(stdout,
"\nMaximum input size: %.1fMB\n",
max_buf / (1024.0 * 1024.0));
list_parsers(stdout);
list_render_modes(stdout);
}
if (opt.version)
version(stdout);
if (opt.help || opt.version)
goto finish;
load_config(opt.confdirs ? opt.confdirs : get_defconf());
if (opt.file) {
rc = map_file(&input, max_buf, opt.file);
} else {
rc = map_stream(&input, max_buf, stdin);
}
if (rc != 0)
return EXIT_FAILURE;
/* Attempt to determine HTML type */
if (opt.parser < 0) {
parser_match(&input);
}
/* Choose default parser */
if (opt.parser < 0) {
opt.parser = 0;
}
rc = parser_defs[opt.parser]->parse_fn(&input);
free_map(&input);
free_parsers();
finish:
free_options();
return EXIT_SUCCESS;
}