More or less completely rewrite mime_enc.c..
[s-mailx.git] / filter.c
blob6f4760d04ae722491b33addfe81b51fe437a5bd5
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Filter objects.
4 * Copyright (c) 2013 - 2016 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #undef n_FILE
19 #define n_FILE filter
21 #ifndef HAVE_AMALGAMATION
22 # include "nail.h"
23 #endif
26 * Quotation filter
30 * TODO quotation filter: anticipate in future data: don't break if only WS
31 * TODO or a LF escaping \ follows on the line (simply reuse the latter).
34 #ifdef HAVE_QUOTE_FOLD
35 n_CTAV(QUOTE_MAX > 3);
37 enum qf_state {
38 _QF_CLEAN,
39 _QF_PREFIX,
40 _QF_DATA
43 struct qf_vc {
44 struct quoteflt *self;
45 char const *buf;
46 size_t len;
49 /* Print out prefix and current quote */
50 static ssize_t _qf_dump_prefix(struct quoteflt *self);
52 /* Add one data character */
53 static ssize_t _qf_add_data(struct quoteflt *self, wchar_t wc);
55 /* State machine handlers */
56 static ssize_t _qf_state_prefix(struct qf_vc *vc);
57 static ssize_t _qf_state_data(struct qf_vc *vc);
59 static ssize_t
60 _qf_dump_prefix(struct quoteflt *self)
62 ssize_t rv;
63 size_t i;
64 NYD_ENTER;
66 if ((i = self->qf_pfix_len) > 0 && i != fwrite(self->qf_pfix, 1, i,
67 self->qf_os))
68 goto jerr;
69 rv = i;
71 if ((i = self->qf_currq.l) > 0 && i != fwrite(self->qf_currq.s, 1, i,
72 self->qf_os))
73 goto jerr;
74 rv += i;
75 jleave:
76 NYD_LEAVE;
77 return rv;
78 jerr:
79 rv = -1;
80 goto jleave;
83 static ssize_t
84 _qf_add_data(struct quoteflt *self, wchar_t wc)
86 char *save_b;
87 ui32_t save_l, save_w;
88 ssize_t rv = 0;
89 int w, l;
90 NYD_ENTER;
92 save_l = save_w = 0; /* silence cc */
93 save_b = NULL;
94 /* <newline> ends state */
95 if (wc == L'\n')
96 goto jflush;
97 if (wc == L'\r') /* TODO CR should be stripped in lower level!! */
98 goto jleave;
100 /* Unroll <tab> to spaces */
101 if (wc == L'\t') {
102 save_l = self->qf_datw;
103 save_w = (save_l + QUOTE_TAB_SPACES) & ~(QUOTE_TAB_SPACES - 1);
104 save_w -= save_l;
105 while (save_w-- > 0) {
106 ssize_t j = _qf_add_data(self, L' ');
107 if (j < 0) {
108 rv = j;
109 break;
111 rv += j;
113 goto jleave;
116 w = wcwidth(wc);
117 if (w == -1) {
118 jbad:
119 ++self->qf_datw;
120 self->qf_dat.s[self->qf_dat.l++] = '?';
121 } else {
122 l = wctomb(self->qf_dat.s + self->qf_dat.l, wc);
123 if (l < 0)
124 goto jbad;
125 self->qf_datw += (ui32_t)w;
126 self->qf_dat.l += (size_t)l;
129 /* TODO The last visual may excess (adjusted!) *qfold-max* if it's a wide;
130 * TODO place it on the next line, break before */
131 if (self->qf_datw >= self->qf_qfold_max) {
132 /* If we have seen a nice breakpoint during traversal, shuffle data
133 * around a bit so as to restore the trailing part after flushing */
134 if (self->qf_brkl > 0) {
135 save_w = self->qf_datw - self->qf_brkw;
136 save_l = self->qf_dat.l - self->qf_brkl;
137 save_b = self->qf_dat.s + self->qf_brkl + 2;
138 memmove(save_b, save_b - 2, save_l);
139 self->qf_dat.l = self->qf_brkl;
142 self->qf_dat.s[self->qf_dat.l++] = '\\';
143 jflush:
144 self->qf_dat.s[self->qf_dat.l++] = '\n';
145 rv = quoteflt_flush(self);
147 /* Restore takeovers, if any */
148 if (save_b != NULL) {
149 self->qf_brk_isws = FAL0;
150 self->qf_datw += save_w;
151 self->qf_dat.l = save_l;
152 memmove(self->qf_dat.s, save_b, save_l);
154 } else if (self->qf_datw >= self->qf_qfold_min && !self->qf_brk_isws) {
155 bool_t isws = (iswspace(wc) != 0);
157 if (isws || !self->qf_brk_isws || self->qf_brkl == 0) {
158 self->qf_brkl = self->qf_dat.l;
159 self->qf_brkw = self->qf_datw;
160 self->qf_brk_isws = isws;
164 /* If state changed to prefix, perform full reset (note this implies that
165 * quoteflt_flush() performs too much work..) */
166 if (wc == '\n') {
167 self->qf_state = _QF_PREFIX;
168 self->qf_wscnt = self->qf_datw = 0;
169 self->qf_currq.l = 0;
171 jleave:
172 NYD_LEAVE;
173 return rv;
176 static ssize_t
177 _qf_state_prefix(struct qf_vc *vc)
179 struct quoteflt *self;
180 ssize_t rv;
181 char const *buf;
182 size_t len, i;
183 wchar_t wc;
184 NYD_ENTER;
186 self = vc->self;
187 rv = 0;
189 for (buf = vc->buf, len = vc->len; len > 0;) {
190 /* xxx NULL BYTE! */
191 i = mbrtowc(&wc, buf, len, self->qf_mbps);
192 if (i == (size_t)-1) {
193 /* On hard error, don't modify mbstate_t and step one byte */
194 self->qf_mbps[0] = self->qf_mbps[1];
195 ++buf;
196 --len;
197 self->qf_wscnt = 0;
198 continue;
200 self->qf_mbps[1] = self->qf_mbps[0];
201 if (i == (size_t)-2) {
202 /* Redundant shift sequence, out of buffer */
203 len = 0;
204 break;
206 buf += i;
207 len -= i;
209 if (wc == L'\n')
210 goto jfin;
211 if (iswspace(wc)) {
212 ++self->qf_wscnt;
213 continue;
215 if (i == 1 && ISQUOTE(wc)) {
216 self->qf_wscnt = 0;
217 if (self->qf_currq.l >= QUOTE_MAX - 3) {
218 self->qf_currq.s[QUOTE_MAX - 3] = '.';
219 self->qf_currq.s[QUOTE_MAX - 2] = '.';
220 self->qf_currq.s[QUOTE_MAX - 1] = '.';
221 self->qf_currq.l = QUOTE_MAX;
222 } else
223 self->qf_currq.s[self->qf_currq.l++] = buf[-1];
224 continue;
227 /* The quote is parsed and compressed; dump it */
228 jfin:
229 self->qf_state = _QF_DATA;
230 /* Overtake WS to the current quote in order to preserve it for eventual
231 * necessary follow lines, too */
232 /* TODO we de-facto "normalize" to ASCII SP here which MESSES tabs!! */
233 while (self->qf_wscnt-- > 0 && self->qf_currq.l < QUOTE_MAX)
234 self->qf_currq.s[self->qf_currq.l++] = ' ';
235 self->qf_datw = self->qf_pfix_len + self->qf_currq.l;
236 self->qf_wscnt = 0;
237 rv = _qf_add_data(self, wc);
238 break;
241 vc->buf = buf;
242 vc->len = len;
243 NYD_LEAVE;
244 return rv;
247 static ssize_t
248 _qf_state_data(struct qf_vc *vc)
250 struct quoteflt *self;
251 ssize_t rv;
252 char const *buf;
253 size_t len, i;
254 wchar_t wc;
255 NYD_ENTER;
257 self = vc->self;
258 rv = 0;
260 for (buf = vc->buf, len = vc->len; len > 0;) {
261 /* xxx NULL BYTE! */
262 i = mbrtowc(&wc, buf, len, self->qf_mbps);
263 if (i == (size_t)-1) {
264 /* On hard error, don't modify mbstate_t and step one byte */
265 self->qf_mbps[0] = self->qf_mbps[1];
266 ++buf;
267 --len;
268 continue;
270 self->qf_mbps[1] = self->qf_mbps[0];
271 if (i == (size_t)-2) {
272 /* Redundant shift sequence, out of buffer */
273 len = 0;
274 break;
276 buf += i;
277 len -= i;
279 { ssize_t j = _qf_add_data(self, wc);
280 if (j < 0) {
281 rv = j;
282 break;
284 rv += j;
287 if (self->qf_state != _QF_DATA)
288 break;
291 vc->buf = buf;
292 vc->len = len;
293 NYD_LEAVE;
294 return rv;
296 #endif /* HAVE_QUOTE_FOLD */
298 FL struct quoteflt *
299 quoteflt_dummy(void) /* TODO LEGACY (until filters are plugged when needed) */
301 static struct quoteflt qf_i;
303 return &qf_i;
306 FL void
307 quoteflt_init(struct quoteflt *self, char const *prefix)
309 #ifdef HAVE_QUOTE_FOLD
310 char *xcp, *cp;
311 #endif
312 NYD_ENTER;
314 memset(self, 0, sizeof *self);
316 if ((self->qf_pfix = prefix) != NULL)
317 self->qf_pfix_len = (ui32_t)strlen(prefix);
319 /* Check whether the user wants the more fancy quoting algorithm */
320 /* TODO *quote-fold*: QUOTE_MAX may excess it! */
321 #ifdef HAVE_QUOTE_FOLD
322 if (self->qf_pfix_len > 0 && (cp = ok_vlook(quote_fold)) != NULL) {
323 ui32_t qmin, qmax = (ui32_t)strtol(cp, &xcp, 10);
324 /* These magic values ensure we don't bail :) */
325 if (qmax < self->qf_pfix_len + 6)
326 qmax = self->qf_pfix_len + 6;
327 --qmax; /* The newline escape */
328 if (cp == xcp || *xcp == '\0')
329 qmin = (qmax >> 1) + (qmax >> 2) + (qmax >> 5);
330 else {
331 qmin = (ui32_t)strtol(xcp + 1, NULL, 10);
332 if (qmin < qmax >> 1)
333 qmin = qmax >> 1;
334 else if (qmin > qmax - 2)
335 qmin = qmax - 2;
337 self->qf_qfold_min = qmin;
338 self->qf_qfold_max = qmax;
340 /* Add pad for takeover copies, backslash and newline */
341 self->qf_dat.s = salloc((qmax + 3) * mb_cur_max);
342 self->qf_currq.s = salloc((QUOTE_MAX + 1) * mb_cur_max);
344 #endif
345 NYD_LEAVE;
348 FL void
349 quoteflt_destroy(struct quoteflt *self) /* xxx inline */
351 NYD_ENTER;
352 n_UNUSED(self);
353 NYD_LEAVE;
356 FL void
357 quoteflt_reset(struct quoteflt *self, FILE *f) /* xxx inline */
359 NYD_ENTER;
360 self->qf_os = f;
361 #ifdef HAVE_QUOTE_FOLD
362 self->qf_state = _QF_CLEAN;
363 self->qf_dat.l =
364 self->qf_currq.l = 0;
365 memset(self->qf_mbps, 0, sizeof self->qf_mbps);
366 #endif
367 NYD_LEAVE;
370 FL ssize_t
371 quoteflt_push(struct quoteflt *self, char const *dat, size_t len)
373 /* (xxx Ideally the actual push() [and flush()] would be functions on their
374 * xxx own, via indirect vtbl call ..) */
375 ssize_t rv = 0;
376 NYD_ENTER;
378 self->qf_nl_last = (len > 0 && dat[len - 1] == '\n'); /* TODO HACK */
380 if (len == 0)
381 goto jleave;
383 /* Bypass? TODO Finally, this filter simply should not be used, then
384 * (TODO It supercedes prefix_write() or something) */
385 if (self->qf_pfix_len == 0) {
386 if (len != fwrite(dat, 1, len, self->qf_os))
387 goto jerr;
388 rv = len;
390 /* Normal: place *indentprefix* at every BOL */
391 else
392 #ifdef HAVE_QUOTE_FOLD
393 if (self->qf_qfold_max == 0)
394 #endif
396 void *vp;
397 size_t ll;
398 bool_t pxok = (self->qf_qfold_min != 0);
400 for (;;) {
401 if (!pxok) {
402 ll = self->qf_pfix_len;
403 if (ll != fwrite(self->qf_pfix, 1, ll, self->qf_os))
404 goto jerr;
405 rv += ll;
406 pxok = TRU1;
409 /* xxx Strictly speaking this is invalid, because only `/' and `.' are
410 * xxx mandated by POSIX.1-2008 as "invariant across all locales
411 * xxx supported"; though there is no charset known which uses this
412 * xxx control char as part of a multibyte character; note that S-nail
413 * XXX (and the Mail codebase as such) do not support EBCDIC */
414 if ((vp = memchr(dat, '\n', len)) == NULL)
415 ll = len;
416 else {
417 pxok = FAL0;
418 ll = PTR2SIZE((char*)vp - dat) + 1;
421 if (ll != fwrite(dat, sizeof *dat, ll, self->qf_os))
422 goto jerr;
423 rv += ll;
424 if ((len -= ll) == 0)
425 break;
426 dat += ll;
429 self->qf_qfold_min = pxok;
431 /* Overly complicated, though still only line-per-line: *quote-fold*.
432 * - If .qf_currq.l is 0, then we are in a clean state. Reset .qf_mbps;
433 * TODO note this means we assume that lines start with reset escape seq,
434 * TODO but i don't think this is any worse than what we currently do;
435 * TODO in 15.0, with the value carrier, we should carry conversion states
436 * TODO all along, only resetting on error (or at words for header =???=);
437 * TODO this still is weird for error handling, but we need to act more
438 * TODO stream-alike (though in practice i don't think cross-line states
439 * TODO can be found, because of compatibility reasons; however, being
440 * TODO a problem rather than a solution is not a good thing (tm))
441 * - Lookout for a newline */
442 #ifdef HAVE_QUOTE_FOLD
443 else {
444 struct qf_vc vc;
445 ssize_t i;
447 vc.self = self;
448 vc.buf = dat;
449 vc.len = len;
450 while (vc.len > 0) {
451 switch (self->qf_state) {
452 case _QF_CLEAN:
453 case _QF_PREFIX:
454 i = _qf_state_prefix(&vc);
455 break;
456 default: /* silence cc (`i' unused) */
457 case _QF_DATA:
458 i = _qf_state_data(&vc);
459 break;
461 if (i < 0)
462 goto jerr;
463 rv += i;
466 #endif /* HAVE_QUOTE_FOLD */
468 jleave:
469 NYD_LEAVE;
470 return rv;
471 jerr:
472 rv = -1;
473 goto jleave;
476 FL ssize_t
477 quoteflt_flush(struct quoteflt *self)
479 ssize_t rv = 0;
480 NYD_ENTER;
481 n_UNUSED(self);
483 #ifdef HAVE_QUOTE_FOLD
484 if (self->qf_dat.l > 0) {
485 rv = _qf_dump_prefix(self);
486 if (rv >= 0) {
487 size_t i = self->qf_dat.l;
488 if (i == fwrite(self->qf_dat.s, 1, i, self->qf_os))
489 rv += i;
490 else
491 rv = -1;
492 self->qf_dat.l = 0;
493 self->qf_brk_isws = FAL0;
494 self->qf_wscnt = self->qf_brkl = self->qf_brkw = 0;
495 self->qf_datw = self->qf_pfix_len + self->qf_currq.l;
498 #endif
499 NYD_LEAVE;
500 return rv;
504 * HTML tagsoup filter TODO rewrite wchar_t based (require HAVE_C90AMEND1)
505 * TODO . Numeric &#NO; entities should also be treated by struct hf_ent
506 * TODO . Yes, we COULD support CSS based quoting when we'd check type="quote"
507 * TODO (nonstandard) and watch out for style="gmail_quote" (or so, VERY
508 * TODO nonstandard) and tracking a stack of such elements (to be popped
509 * TODO once the closing element is seen). Then, after writing a newline,
510 * TODO place sizeof(stack) ">"s first. But aren't these HTML mails rude?
511 * TODO Interlocking and non-well-formed data will break us down
513 #ifdef HAVE_FILTER_HTML_TAGSOUP
515 enum hf_limits {
516 _HF_MINLEN = 10, /* Minimum line length (can't really be smaller) */
517 _HF_BRKSUB = 8 /* Start considering line break MAX - BRKSUB */
520 enum hf_flags {
521 _HF_UTF8 = 1<<0, /* Data is in UTF-8 */
522 _HF_ERROR = 1<<1, /* A hard error occurred, bail as soon as possible */
523 _HF_NOPUT = 1<<2, /* (In a tag,) Don't generate output */
524 _HF_IGN = 1<<3, /* Ignore mode on */
525 _HF_ANY = 1<<4, /* Yet seen just any output */
526 _HF_PRE = 1<<5, /* In <pre>formatted mode */
527 _HF_ENT = 1<<6, /* Currently parsing an entity */
528 _HF_BLANK = 1<<7, /* Whitespace last */
529 _HF_HREF = 1<<8, /* External <a href=> was the last href seen */
531 _HF_NL_1 = 1<<9, /* One \n seen */
532 _HF_NL_2 = 2<<9, /* We have produced an all empty line */
533 _HF_NL_MASK = _HF_NL_1 | _HF_NL_2
536 enum hf_special_actions {
537 _HFSA_NEEDSEP = -1, /* Need an empty line (paragraph separator) */
538 _HFSA_NEEDNL = -2, /* Need a new line start (table row) */
539 _HFSA_IGN = -3, /* Things like <style>..</style>, <script>.. */
540 _HFSA_PRE = -4, /* <pre>.. */
541 _HFSA_PRE_END = -5,
542 _HFSA_IMG = -6, /* <img> */
543 _HFSA_HREF = -7, /* <a>.. */
544 _HFSA_HREF_END = -8
547 enum hf_entity_flags {
548 _HFE_HAVE_UNI = 1<<6, /* Have a Unicode replacement character */
549 _HFE_HAVE_CSTR = 1<<7, /* Have a string replacement */
550 /* We store the length of the entity name in the flags, too */
551 _HFE_LENGTH_MASK = (1<<6) - 1
554 struct htmlflt_href {
555 struct htmlflt_href *hfh_next;
556 ui32_t hfh_no; /* Running sequence */
557 ui32_t hfh_len; /* of .hfh_dat */
558 char hfh_dat[n_VFIELD_SIZE(0)];
561 struct htmlflt_tag {
562 si32_t hft_act; /* char or hf_special_actions */
563 /* Not NUL: character to inject, with high bit set: place a space
564 * afterwards. Note: only recognized with _HFSA_NEEDSEP or _HFSA_NEEDNL */
565 char hft_injc;
566 ui8_t hft_len; /* Useful bytes in (NUL terminated) .hft_tag */
567 char const hft_tag[10]; /* Tag less < and > surroundings (TR, /TR, ..) */
569 n_CTA(n_SIZEOF_FIELD(struct htmlflt_tag, hft_tag) < LINESIZE,
570 "Structure field too large a size"); /* .hf_ign_tag */
572 struct hf_ent {
573 ui8_t hfe_flags; /* enum hf_entity_flags plus length of .hfe_ent */
574 char hfe_c; /* Plain replacement character */
575 ui16_t hfe_uni; /* Unicode codepoint if _HFE_HAVE_UNI */
576 char hfe_cstr[5]; /* _HFE_HAVE_CSTR (e.g., &hellip; -> ...) */
577 char const hfe_ent[7]; /* Entity less & and ; surroundings */
580 /* Tag list; not binary searched :(, so try to take care a bit */
581 static struct htmlflt_tag const _hf_tags[] = {
582 # undef _X
583 # undef _XC
584 # define _X(S,A) {A, '\0', sizeof(S) -1, S "\0"}
585 # define _XC(S,C,A) {A, C, sizeof(S) -1, S "\0"}
587 _X("P", _HFSA_NEEDSEP), _X("/P", _HFSA_NEEDNL),
588 _X("DIV", _HFSA_NEEDSEP), _X("/DIV", _HFSA_NEEDNL),
589 _X("TR", _HFSA_NEEDNL),
590 _X("/TH", '\t'),
591 _X("/TD", '\t'),
592 /* Let it stand out; also since we don't support implicit paragraphs after
593 * block elements, plain running text after a list (seen in Unicode
594 * announcement via Firefox) */
595 _X("UL", _HFSA_NEEDSEP), _X("/UL", _HFSA_NEEDSEP),
596 _XC("LI", (char)0x80 | '*', _HFSA_NEEDSEP),
597 _X("DL", _HFSA_NEEDSEP),
598 _X("DT", _HFSA_NEEDNL),
600 _X("A", _HFSA_HREF), _X("/A", _HFSA_HREF_END),
601 _X("IMG", _HFSA_IMG),
602 _X("BR", '\n'),
603 _X("PRE", _HFSA_PRE), _X("/PRE", _HFSA_PRE_END),
604 _X("TITLE", _HFSA_NEEDSEP), /*_X("/TITLE", '\n'),*/
605 _X("H1", _HFSA_NEEDSEP), /*_X("/H1", '\n'),*/
606 _X("H2", _HFSA_NEEDSEP), /*_X("/H2", '\n'),*/
607 _X("H3", _HFSA_NEEDSEP), /*_X("/H3", '\n'),*/
608 _X("H4", _HFSA_NEEDSEP), /*_X("/H4", '\n'),*/
609 _X("H5", _HFSA_NEEDSEP), /*_X("/H5", '\n'),*/
610 _X("H6", _HFSA_NEEDSEP), /*_X("/H6", '\n'),*/
612 _X("STYLE", _HFSA_IGN),
613 _X("SCRIPT", _HFSA_IGN),
615 # undef _X
618 /* Entity list; not binary searched.. */
619 static struct hf_ent const _hf_ents[] = {
620 # undef _X
621 # undef _XU
622 # undef _XS
623 # undef _XUS
624 # define _X(E,C) {(sizeof(E) -1), C, 0x0u, "", E "\0"}
625 # define _XU(E,C,U) {(sizeof(E) -1) | _HFE_HAVE_UNI, C, U, "", E "\0"}
626 # define _XS(E,S) {(sizeof(E) -1) | _HFE_HAVE_CSTR, '\0', 0x0u,S "\0",E "\0"}
627 # define _XSU(E,S,U) \
628 {(sizeof(E) -1) | _HFE_HAVE_UNI | _HFE_HAVE_CSTR, '\0', U, S "\0", E "\0"}
630 _X("quot", '"'),
631 _X("amp", '&'),
632 _X("lt", '<'), _X("gt", '>'),
634 _XU("nbsp", ' ', 0x0020 /* Note: not 0x00A0 seems to be better for us */),
635 _XU("middot", '.', 0x00B7),
636 _XSU("hellip", "...", 0x2026),
637 _XSU("mdash", "---", 0x2014), _XSU("ndash", "--", 0x2013),
638 _XSU("laquo", "<<", 0x00AB), _XSU("raquo", ">>", 0x00BB),
639 _XSU("lsaquo", "<", 0x2039), _XSU("rsaquo", ">", 0x203A),
640 _XSU("lsquo", "'", 0x2018), _XSU("rsquo", "'", 0x2019),
641 _XSU("ldquo", "\"", 0x201C), _XSU("rdquo", "\"", 0x201D),
642 _XSU("uarr", "^|", 0x2191), _XSU("darr", "|v", 0x2193),
644 _XSU("cent", "CENT", 0x00A2),
645 _XSU("copy", "(C)", 0x00A9),
646 _XSU("euro", "EUR", 0x20AC),
647 _XSU("infin", "INFY", 0x221E),
648 _XSU("pound", "GBP", 0x00A3),
649 _XSU("reg", "(R)", 0x00AE),
650 _XSU("sect", "S:", 0x00A7),
651 _XSU("yen", "JPY", 0x00A5),
653 /* German umlauts */
654 _XSU("Auml", "Ae", 0x00C4), _XSU("auml", "ae", 0x00E4),
655 _XSU("Ouml", "Oe", 0x00D6), _XSU("ouml", "oe", 0x00F6),
656 _XSU("Uuml", "Ue", 0x00DC), _XSU("uuml", "ue", 0x00FC),
657 _XSU("szlig", "ss", 0x00DF)
659 # undef _X
660 # undef _XU
661 # undef _XS
662 # undef _XSU
665 /* Real output */
666 static struct htmlflt * _hf_dump_hrefs(struct htmlflt *self);
667 static struct htmlflt * _hf_dump(struct htmlflt *self);
668 static struct htmlflt * _hf_store(struct htmlflt *self, char c);
669 # ifdef HAVE_NATCH_CHAR
670 static struct htmlflt * __hf_sync_mbstuff(struct htmlflt *self);
671 # endif
673 /* Virtual output */
674 static struct htmlflt * _hf_nl(struct htmlflt *self);
675 static struct htmlflt * _hf_nl_force(struct htmlflt *self);
676 static struct htmlflt * _hf_putc(struct htmlflt *self, char c);
677 static struct htmlflt * _hf_putc_premode(struct htmlflt *self, char c);
678 static struct htmlflt * _hf_puts(struct htmlflt *self, char const *cp);
679 static struct htmlflt * _hf_putbuf(struct htmlflt *self,
680 char const *cp, size_t len);
682 /* Try to locate a param'eter in >hf_bdat, store it (non-terminated!) or NULL */
683 static struct htmlflt * _hf_param(struct htmlflt *self, struct str *store,
684 char const *param);
686 /* Expand all entities in the given parameter */
687 static struct htmlflt * _hf_expand_all_ents(struct htmlflt *self,
688 struct str const *param);
690 /* Completely parsed over a tag / an entity, interpret that */
691 static struct htmlflt * _hf_check_tag(struct htmlflt *self, char const *s);
692 static struct htmlflt * _hf_check_ent(struct htmlflt *self, char const *s,
693 size_t l);
695 /* Input handler */
696 static ssize_t _hf_add_data(struct htmlflt *self,
697 char const *dat, size_t len);
699 static struct htmlflt *
700 _hf_dump_hrefs(struct htmlflt *self)
702 struct htmlflt_href *hhp;
703 NYD2_ENTER;
705 if (!(self->hf_flags & _HF_NL_2) && putc('\n', self->hf_os) == EOF) {
706 self->hf_flags |= _HF_ERROR;
707 goto jleave;
710 /* Reverse the list */
711 for (hhp = self->hf_hrefs, self->hf_hrefs = NULL; hhp != NULL;) {
712 struct htmlflt_href *tmp = hhp->hfh_next;
713 hhp->hfh_next = self->hf_hrefs;
714 self->hf_hrefs = hhp;
715 hhp = tmp;
718 /* Then dump it */
719 while ((hhp = self->hf_hrefs) != NULL) {
720 self->hf_hrefs = hhp->hfh_next;
722 if (!(self->hf_flags & _HF_ERROR)) {
723 int w = fprintf(self->hf_os, " [%u] %.*s\n",
724 hhp->hfh_no, (int)hhp->hfh_len, hhp->hfh_dat);
725 if (w < 0)
726 self->hf_flags |= _HF_ERROR;
728 free(hhp);
731 self->hf_flags |= (putc('\n', self->hf_os) == EOF)
732 ? _HF_ERROR : _HF_NL_1 | _HF_NL_2;
733 self->hf_href_dist = (ui32_t)realscreenheight >> 1;
734 jleave:
735 NYD2_LEAVE;
736 return self;
739 static struct htmlflt *
740 _hf_dump(struct htmlflt *self)
742 ui32_t f, l;
743 char c, *cp;
744 NYD2_ENTER;
746 f = self->hf_flags & ~_HF_BLANK;
747 l = self->hf_len;
748 cp = self->hf_line;
749 self->hf_mbwidth = self->hf_mboff = self->hf_last_ws = self->hf_len = 0;
751 for (c = '\0'; l > 0; --l) {
752 c = *cp++;
753 jput:
754 if (putc(c, self->hf_os) == EOF) {
755 self->hf_flags = (f |= _HF_ERROR);
756 goto jleave;
760 if (c != '\n') {
761 f |= (f & _HF_NL_1) ? _HF_NL_2 : _HF_NL_1;
762 l = 1;
763 c = '\n';
764 goto jput;
766 self->hf_flags = f;
768 /* Check whether there are HREFs to dump; there is so much messy tagsoup out
769 * there that it seems best not to simply dump HREFs in each _dump(), but
770 * only with some gap, let's say half the real screen height */
771 if (--self->hf_href_dist < 0 && (f & _HF_NL_2) && self->hf_hrefs != NULL)
772 self = _hf_dump_hrefs(self);
773 jleave:
774 NYD2_LEAVE;
775 return self;
778 static struct htmlflt *
779 _hf_store(struct htmlflt *self, char c)
781 ui32_t f, l, i;
782 NYD2_ENTER;
784 assert(c != '\n');
786 f = self->hf_flags;
787 l = self->hf_len;
788 self->hf_line[l] = (c == '\t' ? ' ' : c);
789 self->hf_len = ++l;
790 if (blankspacechar(c)) {
791 if (c == '\t') {
792 i = 8 - ((l - 1) & 7); /* xxx magic tab width of 8 */
793 if (i > 0) {
795 self = _hf_store(self, ' ');
796 while (--i > 0);
797 goto jleave;
800 self->hf_last_ws = l;
801 } else if (/*c == '.' ||*/ c == ',' || c == ';' || c == '-')
802 self->hf_last_ws = l;
804 i = l;
805 # ifdef HAVE_NATCH_CHAR /* XXX This code is really ridiculous! */
806 if (mb_cur_max > 1) { /* XXX should mbrtowc() and THEN store, at least.. */
807 wchar_t wc;
808 int w, x = mbtowc(&wc, self->hf_line + self->hf_mboff, l - self->hf_mboff);
810 if (x > 0) {
811 if ((w = wcwidth(wc)) == -1 ||
812 /* Actively filter out L-TO-R and R-TO-R marks TODO ctext */
813 (wc == 0x200E || wc == 0x200F ||
814 (wc >= 0x202A && wc <= 0x202E)) ||
815 /* And some zero-width messes */
816 wc == 0x00AD || (wc >= 0x200B && wc <= 0x200D) ||
817 /* Oh about the ISO C wide character interfaces, baby! */
818 (wc == 0xFEFF)){
819 self->hf_len -= x;
820 goto jleave;
821 } else if (iswspace(wc))
822 self->hf_last_ws = l;
823 self->hf_mboff += x;
824 i = (self->hf_mbwidth += w);
825 } else {
826 if (x < 0) {
827 mbtowc(&wc, NULL, mb_cur_max);
828 if (UICMP(32, l - self->hf_mboff, >=, mb_cur_max)) { /* XXX */
829 ++self->hf_mboff;
830 ++self->hf_mbwidth;
833 i = self->hf_mbwidth;
836 # endif
838 /* Do we need to break the line? */
839 if (i >= self->hf_lmax - _HF_BRKSUB) {
840 ui32_t lim = self->hf_lmax >> 1;
842 /* Let's hope we saw a sane place to break this line! */
843 if (self->hf_last_ws >= lim) {
844 jput:
845 i = self->hf_len = self->hf_last_ws;
846 self = _hf_dump(self);
847 if ((self->hf_len = (l -= i)) > 0) {
848 self->hf_flags &= ~_HF_NL_MASK;
849 memmove(self->hf_line, self->hf_line + i, l);
850 # ifdef HAVE_NATCH_CHAR
851 __hf_sync_mbstuff(self);
852 # endif
854 goto jleave;
857 /* Any 7-bit characters? */
858 for (i = l; i-- >= lim;)
859 if (asciichar((c = self->hf_line[i]))) {
860 self->hf_last_ws = ++i;
861 goto jput;
862 } else if ((f & _HF_UTF8) && ((ui8_t)c & 0xC0) != 0x80) {
863 self->hf_last_ws = i;
864 goto jput;
867 /* Hard break necessary! xxx really badly done */
868 if (l >= self->hf_lmax - 1)
869 self = _hf_dump(self);
871 jleave:
872 NYD2_LEAVE;
873 return self;
876 # ifdef HAVE_NATCH_CHAR
877 static struct htmlflt *
878 __hf_sync_mbstuff(struct htmlflt *self)
880 wchar_t wc;
881 char const *b;
882 ui32_t o, w, l;
883 NYD2_ENTER;
885 b = self->hf_line;
886 o = w = 0;
887 l = self->hf_len;
888 goto jumpin;
890 while (l > 0) {
891 int x = mbtowc(&wc, b, l);
893 if (x == 0)
894 break;
896 if (x > 0) {
897 b += x;
898 l -= x;
899 o += x;
900 if ((x = wcwidth(wc)) == -1)
901 x = 1;
902 w += x;
903 continue;
906 /* Bad, skip over a single character.. XXX very bad indeed */
907 ++b;
908 ++o;
909 ++w;
910 --l;
911 jumpin:
912 mbtowc(&wc, NULL, mb_cur_max);
915 self->hf_mboff = o;
916 self->hf_mbwidth = w;
918 NYD2_LEAVE;
919 return self;
921 # endif /* HAVE_NATCH_CHAR */
923 static struct htmlflt *
924 _hf_nl(struct htmlflt *self)
926 ui32_t f;
927 NYD2_ENTER;
929 if (!((f = self->hf_flags) & _HF_ERROR)) {
930 if (f & _HF_ANY) {
931 if ((f & _HF_NL_MASK) != _HF_NL_MASK)
932 self = _hf_dump(self);
933 } else
934 self->hf_flags = (f |= _HF_NL_MASK);
936 NYD2_LEAVE;
937 return self;
940 static struct htmlflt *
941 _hf_nl_force(struct htmlflt *self)
943 NYD2_ENTER;
944 if (!(self->hf_flags & _HF_ERROR))
945 self = _hf_dump(self);
946 NYD2_LEAVE;
947 return self;
950 static struct htmlflt *
951 _hf_putc(struct htmlflt *self, char c)
953 ui32_t f;
954 NYD2_ENTER;
956 if ((f = self->hf_flags) & _HF_ERROR)
957 goto jleave;
959 if (c == '\n') {
960 self = _hf_nl(self);
961 goto jleave;
962 } else if (c == ' ' || c == '\t') {
963 if ((f & _HF_BLANK) || self->hf_len == 0)
964 goto jleave;
965 f |= _HF_BLANK;
966 } else
967 f &= ~_HF_BLANK;
968 f &= ~_HF_NL_MASK;
969 self->hf_flags = (f |= _HF_ANY);
970 self = _hf_store(self, c);
971 jleave:
972 NYD2_LEAVE;
973 return self;
976 static struct htmlflt *
977 _hf_putc_premode(struct htmlflt *self, char c)
979 ui32_t f;
980 NYD2_ENTER;
982 if ((f = self->hf_flags) & _HF_ERROR) {
984 } else if (c == '\n')
985 self = _hf_nl_force(self);
986 else {
987 f &= ~_HF_NL_MASK;
988 self->hf_flags = (f |= _HF_ANY);
989 self = _hf_store(self, c);
991 NYD2_LEAVE;
992 return self;
995 static struct htmlflt *
996 _hf_puts(struct htmlflt *self, char const *cp)
998 char c;
999 NYD2_ENTER;
1001 while ((c = *cp++) != '\0')
1002 self = _hf_putc(self, c);
1003 NYD2_LEAVE;
1004 return self;
1007 static struct htmlflt *
1008 _hf_putbuf(struct htmlflt *self, char const *cp, size_t len)
1010 NYD2_ENTER;
1012 while (len-- > 0)
1013 self = _hf_putc(self, *cp++);
1014 NYD2_LEAVE;
1015 return self;
1018 static struct htmlflt *
1019 _hf_param(struct htmlflt *self, struct str *store, char const *param)
1021 char const *cp;
1022 char c, x, quote;
1023 size_t i;
1024 bool_t hot;
1025 NYD2_ENTER;
1027 store->s = NULL;
1028 store->l = 0;
1029 cp = self->hf_bdat;
1031 /* Skip over any non-WS first; be aware of soup, if it slipped through */
1032 for(;;){
1033 if((c = *cp++) == '\0' || c == '>')
1034 goto jleave;
1035 if(whitechar(c))
1036 break;
1039 /* Search for the parameter, take care of other quoting along the way */
1040 x = *param++;
1041 x = upperconv(x);
1042 quote = '\0';
1043 i = strlen(param);
1045 for(hot = TRU1;;){
1046 if((c = *cp++) == '\0' || c == '>')
1047 goto jleave;
1048 if(whitechar(c)){
1049 hot = TRU1;
1050 continue;
1053 /* Could it be a parameter? */
1054 if(hot){
1055 hot = FAL0;
1057 /* Is it the desired one? */
1058 if((c = upperconv(c)) == x && !ascncasecmp(param, cp, i)){
1059 char const *cp2 = cp + i;
1061 if((quote = *cp2++) != '='){
1062 if(quote == '\0' || quote == '>')
1063 goto jleave;
1064 while(whitechar(quote))
1065 quote = *cp2++;
1067 if(quote == '='){
1068 cp = cp2;
1069 break;
1071 continue; /* XXX Optimize: i bytes or even cp2 can't be it! */
1075 /* Not the desired one; but a parameter? */
1076 if(c != '=')
1077 continue;
1078 /* If so, properly skip over the value */
1079 if(c == '"' || c == '\''){
1080 /* TODO oops i have forgotten whether backslash quoting is allowed in
1081 * TODO quoted HTML parameter values? not supporting that for now.. */
1082 for(quote = c; (c = *cp) != '\0' && c != quote; ++cp)
1084 }else
1085 while((c = *cp) != '\0' && !whitechar(c) && c != '>')
1086 ++cp;
1087 if(c == '\0')
1088 goto jleave;
1091 /* Skip further whitespace */
1092 for(;;){
1093 if((c = *cp++) == '\0' || c == '>')
1094 goto jleave;
1095 if(!whitechar(c))
1096 break;
1099 if(c == '"' || c == '\''){
1100 /* TODO oops i have forgotten whether backslash quoting is allowed in
1101 * TODO quoted HTML parameter values? not supporting that for now.. */
1102 store->s = n_UNCONST(cp);
1103 for(quote = c; (c = *cp) != '\0' && c != quote; ++cp)
1105 /* XXX ... and we simply ignore a missing trailing " :> */
1106 }else{
1107 store->s = n_UNCONST(cp - 1);
1108 if(!whitechar(c))
1109 while((c = *cp) != '\0' && !whitechar(c) && c != '>')
1110 ++cp;
1112 i = PTR2SIZE(cp - store->s);
1114 /* Terrible tagsoup out there, e.g., groups.google.com produces href=""
1115 * parameter values prefixed and suffixed by newlines! Therefore trim the
1116 * value content TODO join into the parse step above! */
1117 for (cp = store->s; i > 0 && spacechar(*cp); ++cp, --i)
1119 store->s = n_UNCONST(cp);
1120 for (cp += i - 1; i > 0 && spacechar(*cp); --cp, --i)
1122 if ((store->l = i) == 0)
1123 store->s = NULL;
1124 jleave:
1125 NYD2_LEAVE;
1126 return self;
1129 static struct htmlflt *
1130 _hf_expand_all_ents(struct htmlflt *self, struct str const *param)
1132 char const *cp, *maxcp, *ep;
1133 char c;
1134 size_t i;
1135 NYD2_ENTER;
1137 for (cp = param->s, maxcp = cp + param->l; cp < maxcp;)
1138 if ((c = *cp++) != '&')
1139 jputc:
1140 self = _hf_putc(self, c);
1141 else {
1142 for (ep = cp--;;) {
1143 if (ep == maxcp || (c = *ep++) == '\0') {
1144 for (; cp < ep; ++cp)
1145 self = _hf_putc(self, *cp);
1146 goto jleave;
1147 } else if (c == ';') {
1148 if ((i = PTR2SIZE(ep - cp)) > 1) {
1149 self = _hf_check_ent(self, cp, i);
1150 break;
1151 } else {
1152 c = *cp++;
1153 goto jputc;
1157 cp = ep;
1159 jleave:
1160 NYD2_LEAVE;
1161 return self;
1164 static struct htmlflt *
1165 _hf_check_tag(struct htmlflt *self, char const *s)
1167 char nobuf[32], c;
1168 struct str param;
1169 size_t i;
1170 struct htmlflt_tag const *hftp;
1171 ui32_t f;
1172 NYD2_ENTER;
1174 /* Extra check only */
1175 assert(s != NULL);
1176 if (*s != '<') {
1177 DBG( n_alert("HTML tagsoup filter _hf_check_tag() called on soup!"); )
1178 jput_as_is:
1179 self = _hf_puts(self, self->hf_bdat);
1180 goto jleave;
1183 for (++s, i = 0; (c = s[i]) != '\0' && c != '>' && !whitechar(c); ++i)
1184 /* Special massage for things like <br/>: after the slash only whitespace
1185 * may separate us from the closing right angle! */
1186 if (c == '/') {
1187 size_t j = i + 1;
1189 while ((c = s[j]) != '\0' && c != '>' && whitechar(c))
1190 ++j;
1191 if (c == '>')
1192 break;
1195 for (hftp = _hf_tags;;) {
1196 if (i == hftp->hft_len && !ascncasecmp(s, hftp->hft_tag, i)) {
1197 c = s[hftp->hft_len];
1198 if (c == '>' || c == '/' || whitechar(c))
1199 break;
1201 if (PTRCMP(++hftp, >=, _hf_tags + n_NELEM(_hf_tags)))
1202 goto jnotknown;
1205 f = self->hf_flags;
1206 switch (hftp->hft_act) {
1207 case _HFSA_PRE_END:
1208 f &= ~_HF_PRE;
1209 if (0) {
1210 /* FALLTHRU */
1211 case _HFSA_PRE:
1212 f |= _HF_PRE;
1214 self->hf_flags = f;
1215 /* FALLTHRU */
1217 case _HFSA_NEEDSEP:
1218 if (!(self->hf_flags & _HF_NL_2))
1219 self = _hf_nl(self);
1220 /* FALLTHRU */
1221 case _HFSA_NEEDNL:
1222 if (!(f & _HF_NL_1))
1223 self = _hf_nl(self);
1224 if (hftp->hft_injc != '\0') {
1225 self = _hf_putc(self, hftp->hft_injc & 0x7F);
1226 if ((uc_i)hftp->hft_injc & 0x80)
1227 self = _hf_putc(self, ' ');
1229 break;
1231 case _HFSA_IGN:
1232 self->hf_ign_tag = hftp;
1233 self->hf_flags = (f |= _HF_IGN | _HF_NOPUT);
1234 break;
1236 case _HFSA_IMG:
1237 self = _hf_param(self, &param, "alt");
1238 self = _hf_putc(self, '[');
1239 if (param.s == NULL) {
1240 param.s = n_UNCONST("IMG");
1241 param.l = 3;
1242 goto jimg_put;
1243 } /* else */ if (memchr(param.s, '&', param.l) != NULL)
1244 self = _hf_expand_all_ents(self, &param);
1245 else
1246 jimg_put:
1247 self = _hf_putbuf(self, param.s, param.l);
1248 self = _hf_putc(self, ']');
1249 break;
1251 case _HFSA_HREF:
1252 self = _hf_param(self, &param, "href");
1253 /* Ignore non-external links */
1254 if (param.s != NULL && *param.s != '#') {
1255 struct htmlflt_href *hhp = smalloc(sizeof(*hhp) -
1256 n_VFIELD_SIZEOF(struct htmlflt_href, hfh_dat) + param.l +1);
1258 hhp->hfh_next = self->hf_hrefs;
1259 hhp->hfh_no = ++self->hf_href_no;
1260 hhp->hfh_len = (ui32_t)param.l;
1261 memcpy(hhp->hfh_dat, param.s, param.l);
1263 snprintf(nobuf, sizeof nobuf, "[%u]", hhp->hfh_no);
1264 self->hf_flags = (f |= _HF_HREF);
1265 self->hf_hrefs = hhp;
1266 self = _hf_puts(self, nobuf);
1267 } else
1268 self->hf_flags = (f &= ~_HF_HREF);
1269 break;
1270 case _HFSA_HREF_END:
1271 if (f & _HF_HREF) {
1272 snprintf(nobuf, sizeof nobuf, "[/%u]", self->hf_href_no);
1273 self = _hf_puts(self, nobuf);
1275 break;
1277 default:
1278 c = (char)(hftp->hft_act & 0xFF);
1279 self = _hf_putc(self, c);
1280 break;
1281 case '\0':
1282 break;
1284 jleave:
1285 NYD2_LEAVE;
1286 return self;
1288 /* The problem is that even invalid tagsoup is widely used, without real
1289 * searching i have seen e-mail address in <N@H.D> notation, and more.
1290 * To protect us a bit look around and possibly write the content as such */
1291 jnotknown:
1292 switch (*s) {
1293 case '!':
1294 case '?':
1295 /* Ignore <!DOCTYPE, <!-- comments, <? PIs.. */
1296 goto jleave;
1297 case '>':
1298 /* Print out an empty tag as such */
1299 if (s[1] == '\0') {
1300 --s;
1301 goto jput_as_is;
1303 break;
1304 case '/':
1305 ++s;
1306 break;
1307 default:
1308 break;
1311 /* Also skip over : in order to suppress v:roundrect, w:anchorlock.. */
1312 while ((c = *s++) != '\0' && c != '>' && !whitechar(c) && c != ':')
1313 if (!asciichar(c) || punctchar(c)) {
1314 self = _hf_puts(self, self->hf_bdat);
1315 break;
1317 goto jleave;
1320 static struct htmlflt *
1321 _hf_check_ent(struct htmlflt *self, char const *s, size_t l)
1323 char nobuf[32];
1324 char const *s_save;
1325 size_t l_save;
1326 struct hf_ent const *hfep;
1327 long i;
1328 NYD2_ENTER;
1330 s_save = s;
1331 l_save = l;
1332 assert(*s == '&');
1333 assert(l > 0);
1334 /* False entities seen in the wild assert(s[l - 1] == ';'); */
1335 ++s;
1336 l -= 2;
1338 /* Numeric entity, or try named search */
1339 if (*s == '#') {
1340 i = (*++s == 'x' ? 16 : 10);
1342 if ((i != 16 || (++s, --l) > 0) && l < sizeof(nobuf)) {
1343 memcpy(nobuf, s, l);
1344 nobuf[l] = '\0';
1345 i = strtol(nobuf, NULL, i);
1346 if (i <= 0x7F)
1347 self = _hf_putc(self, (char)i);
1348 else if (self->hf_flags & _HF_UTF8) {
1349 jputuni:
1350 l = n_utf32_to_utf8((ui32_t)i, nobuf);
1351 self = _hf_putbuf(self, nobuf, l);
1352 } else
1353 goto jeent;
1354 } else
1355 goto jeent;
1356 } else {
1357 ui32_t f = self->hf_flags, hf;
1359 for (hfep = _hf_ents; PTRCMP(hfep, <, _hf_ents + n_NELEM(_hf_ents));
1360 ++hfep)
1361 if (l == ((hf = hfep->hfe_flags) & _HFE_LENGTH_MASK) &&
1362 !strncmp(s, hfep->hfe_ent, l)) {
1363 if ((hf & _HFE_HAVE_UNI) && (f & _HF_UTF8)) {
1364 i = hfep->hfe_uni;
1365 goto jputuni;
1366 } else if (hf & _HFE_HAVE_CSTR)
1367 self = _hf_puts(self, hfep->hfe_cstr);
1368 else
1369 self = _hf_putc(self, hfep->hfe_c);
1370 goto jleave;
1372 jeent:
1373 self = _hf_putbuf(self, s_save, l_save);
1375 jleave:
1376 NYD2_LEAVE;
1377 return self;
1380 static ssize_t
1381 _hf_add_data(struct htmlflt *self, char const *dat, size_t len)
1383 char c, *cp, *cp_max;
1384 bool_t hot;
1385 ssize_t rv = 0;
1386 NYD_ENTER;
1388 /* Final put request? */
1389 if (dat == NULL) {
1390 if (self->hf_len > 0 || self->hf_hrefs != NULL) {
1391 self = _hf_dump(self);
1392 if (self->hf_hrefs != NULL)
1393 self = _hf_dump_hrefs(self);
1394 rv = 1;
1396 goto jleave;
1399 /* Always ensure some initial buffer */
1400 if ((cp = self->hf_curr) != NULL)
1401 cp_max = self->hf_bmax;
1402 else {
1403 cp = self->hf_curr = self->hf_bdat = smalloc(LINESIZE);
1404 cp_max = self->hf_bmax = cp + LINESIZE -1; /* (Always room for NUL!) */
1406 hot = (cp != self->hf_bdat);
1408 for (rv = (ssize_t)len; len > 0; --len) {
1409 ui32_t f = self->hf_flags;
1411 if (f & _HF_ERROR)
1412 break;
1413 c = *dat++;
1415 /* Soup is really weird, and scripts may contain almost anything (and
1416 * newer CSS standards are also cryptic): therefore prefix the _HF_IGN
1417 * test and walk until we see the required end tag */
1418 /* TODO For real safety _HF_IGN soup condome would also need to know
1419 * TODO about quoted strings so that 'var i = "</script>";' couldn't
1420 * TODO fool it! We really want this mode also for _HF_NOPUT to be
1421 * TODO able to *gracefully* detect the tag-closing '>', but then if
1422 * TODO that is a single mechanism we should have made it! */
1423 if (f & _HF_IGN) {
1424 struct htmlflt_tag const *hftp = self->hf_ign_tag;
1425 size_t i;
1427 if (c == '<') {
1428 hot = TRU1;
1429 jcp_reset:
1430 cp = self->hf_bdat;
1431 } else if (c == '>') {
1432 if (hot) {
1433 if ((i = PTR2SIZE(cp - self->hf_bdat)) > 1 &&
1434 --i == hftp->hft_len &&
1435 !ascncasecmp(self->hf_bdat + 1, hftp->hft_tag, i))
1436 self->hf_flags = (f &= ~(_HF_IGN | _HF_NOPUT));
1437 hot = FAL0;
1438 goto jcp_reset;
1440 } else if (hot) {
1441 *cp++ = c;
1442 i = PTR2SIZE(cp - self->hf_bdat);
1443 if ((i == 1 && c != '/') || --i > hftp->hft_len) {
1444 hot = FAL0;
1445 goto jcp_reset;
1448 } else switch (c) {
1449 case '<':
1450 /* People are using & without &amp;ing it, ditto <; be aware */
1451 if (f & (_HF_NOPUT | _HF_ENT)) {
1452 f &= ~_HF_ENT;
1453 /* Special case "<!--" buffer content to deal with really weird
1454 * things that can be done with "<!--[if gte mso 9]>" syntax */
1455 if (PTR2SIZE(cp - self->hf_bdat) != 4 ||
1456 memcmp(self->hf_bdat, "<!--", 4)) {
1457 self->hf_flags = f;
1458 *cp = '\0';
1459 self = _hf_puts(self, self->hf_bdat);
1460 f = self->hf_flags;
1463 cp = self->hf_bdat;
1464 *cp++ = c;
1465 self->hf_flags = (f |= _HF_NOPUT);
1466 break;
1467 case '>':
1468 /* Weird tagsoup around, do we actually parse a tag? */
1469 if (!(f & _HF_NOPUT))
1470 goto jdo_c;
1471 cp[0] = c;
1472 cp[1] = '\0';
1473 f &= ~(_HF_NOPUT | _HF_ENT);
1474 self->hf_flags = f;
1475 self = _hf_check_tag(self, self->hf_bdat);
1476 *(cp = self->hf_bdat) = '\0'; /* xxx extra safety */
1477 /* Quick hack to get rid of redundant newline after <pre> XXX */
1478 if (!(f & _HF_PRE) && (self->hf_flags & _HF_PRE) &&
1479 len > 1 && *dat == '\n')
1480 ++dat, --len;
1481 break;
1483 case '\r': /* TODO CR should be stripped in lower level!! (Only B64!?!) */
1484 break;
1485 case '\n':
1486 /* End of line is not considered unless we are in PRE section.
1487 * However, in _HF_NOPUT mode we must be aware of tagsoup which uses
1488 * newlines for separating parameters */
1489 if (f & _HF_NOPUT)
1490 goto jdo_c;
1491 self = (f & _HF_PRE) ? _hf_nl_force(self) : _hf_putc(self, ' ');
1492 break;
1494 case '\t':
1495 if (!(f & _HF_PRE))
1496 c = ' ';
1497 /* FALLTHRU */
1498 default:
1499 jdo_c:
1500 /* If not currently parsing a tag and bypassing normal output.. */
1501 if (!(f & _HF_NOPUT)) {
1502 if (cntrlchar(c))
1503 break;
1504 if (c == '&') {
1505 cp = self->hf_bdat;
1506 *cp++ = c;
1507 self->hf_flags = (f |= _HF_NOPUT | _HF_ENT);
1508 } else if (f & _HF_PRE) {
1509 self = _hf_putc_premode(self, c);
1510 self->hf_flags &= ~_HF_BLANK;
1511 } else
1512 self = _hf_putc(self, c);
1513 } else if ((f & _HF_ENT) && c == ';') {
1514 cp[0] = c;
1515 cp[1] = '\0';
1516 f &= ~(_HF_NOPUT | _HF_ENT);
1517 self->hf_flags = f;
1518 self = _hf_check_ent(self, self->hf_bdat,
1519 PTR2SIZE(cp + 1 - self->hf_bdat));
1520 } else {
1521 /* We may need to grow the buffer */
1522 if (PTRCMP(cp + 42/2, >=, cp_max)) {
1523 size_t i = PTR2SIZE(cp - self->hf_bdat),
1524 m = PTR2SIZE(self->hf_bmax - self->hf_bdat) + LINESIZE;
1526 cp = self->hf_bdat = srealloc(self->hf_bdat, m);
1527 self->hf_bmax = cp + m -1;
1528 self->hf_curr = (cp += i);
1530 *cp++ = c;
1534 self->hf_curr = cp;
1535 jleave:
1536 NYD_LEAVE;
1537 return (self->hf_flags & _HF_ERROR) ? -1 : rv;
1541 * TODO Because we don't support filter chains yet this filter will be run
1542 * TODO in a dedicated subprocess, driven via a special Popen() mode
1544 static bool_t __hf_hadpipesig;
1545 static void
1546 __hf_onpipe(int signo)
1548 NYD_X; /* Signal handler */
1549 n_UNUSED(signo);
1550 __hf_hadpipesig = TRU1;
1553 FL int
1554 htmlflt_process_main(void)
1556 char buf[BUFFER_SIZE];
1557 struct htmlflt hf;
1558 size_t i;
1559 int rv;
1560 NYD_ENTER;
1562 __hf_hadpipesig = FAL0;
1563 safe_signal(SIGPIPE, &__hf_onpipe);
1565 htmlflt_init(&hf);
1566 htmlflt_reset(&hf, stdout);
1568 for (;;) {
1569 if ((i = fread(buf, sizeof(buf[0]), n_NELEM(buf), stdin)) == 0) {
1570 rv = !feof(stdin);
1571 break;
1574 if ((rv = __hf_hadpipesig))
1575 break;
1576 /* Just use this directly.. */
1577 if (htmlflt_push(&hf, buf, i) < 0) {
1578 rv = 1;
1579 break;
1582 if (rv == 0 && htmlflt_flush(&hf) < 0)
1583 rv = 1;
1585 htmlflt_destroy(&hf);
1587 rv |= __hf_hadpipesig;
1588 NYD_LEAVE;
1589 return rv;
1592 FL void
1593 htmlflt_init(struct htmlflt *self)
1595 NYD_ENTER;
1596 /* (Rather redundant though) */
1597 memset(self, 0, sizeof *self);
1598 NYD_LEAVE;
1601 FL void
1602 htmlflt_destroy(struct htmlflt *self)
1604 NYD_ENTER;
1605 htmlflt_reset(self, NULL);
1606 NYD_LEAVE;
1609 FL void
1610 htmlflt_reset(struct htmlflt *self, FILE *f)
1612 struct htmlflt_href *hfhp;
1613 NYD_ENTER;
1615 while ((hfhp = self->hf_hrefs) != NULL) {
1616 self->hf_hrefs = hfhp->hfh_next;
1617 free(hfhp);
1620 if (self->hf_bdat != NULL)
1621 free(self->hf_bdat);
1622 if (self->hf_line != NULL)
1623 free(self->hf_line);
1625 memset(self, 0, sizeof *self);
1627 if (f != NULL) {
1628 ui32_t sw = n_MAX(_HF_MINLEN, (ui32_t)scrnwidth);
1630 self->hf_line = smalloc((size_t)sw * mb_cur_max +1);
1631 self->hf_lmax = sw;
1633 if (options & OPT_UNICODE) /* TODO not truly generic */
1634 self->hf_flags = _HF_UTF8;
1635 self->hf_os = f;
1637 NYD_LEAVE;
1640 FL ssize_t
1641 htmlflt_push(struct htmlflt *self, char const *dat, size_t len)
1643 ssize_t rv;
1644 NYD_ENTER;
1646 rv = _hf_add_data(self, dat, len);
1647 NYD_LEAVE;
1648 return rv;
1651 FL ssize_t
1652 htmlflt_flush(struct htmlflt *self)
1654 ssize_t rv;
1655 NYD_ENTER;
1657 rv = _hf_add_data(self, NULL, 0);
1658 rv |= !fflush(self->hf_os) ? 0 : -1;
1659 NYD_LEAVE;
1660 return rv;
1662 #endif /* HAVE_FILTER_HTML_TAGSOUP */
1664 /* s-it-mode */