Fix reverso in [0cd5c9e]
[s-mailx.git] / filter.c
blob492368f1fd0b99dada8e84bd86aa1f8b4660dbd5
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Filter objects.
4 * Copyright (c) 2013 - 2014 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
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.
19 #ifndef HAVE_AMALGAMATION
20 # include "nail.h"
21 #endif
24 * Quotation filter
28 * TODO quotation filter: anticipate in future data: don't break if only WS
29 * TODO or a LF escaping \ follows on the line (simply reuse the latter).
32 #ifdef HAVE_QUOTE_FOLD
33 CTA(QUOTE_MAX > 3);
35 enum qf_state {
36 _QF_CLEAN,
37 _QF_PREFIX,
38 _QF_DATA
41 struct qf_vc {
42 struct quoteflt *self;
43 char const *buf;
44 size_t len;
47 /* Print out prefix and current quote */
48 static ssize_t _qf_dump_prefix(struct quoteflt *self);
50 /* Add one data character */
51 static ssize_t _qf_add_data(struct quoteflt *self, wchar_t wc);
53 /* State machine handlers */
54 static ssize_t _qf_state_prefix(struct qf_vc *vc);
55 static ssize_t _qf_state_data(struct qf_vc *vc);
57 static ssize_t
58 _qf_dump_prefix(struct quoteflt *self)
60 ssize_t rv;
61 size_t i;
62 NYD_ENTER;
64 if ((i = self->qf_pfix_len) > 0 && i != fwrite(self->qf_pfix, 1, i,
65 self->qf_os))
66 goto jerr;
67 rv = i;
69 if ((i = self->qf_currq.l) > 0 && i != fwrite(self->qf_currq.s, 1, i,
70 self->qf_os))
71 goto jerr;
72 rv += i;
73 jleave:
74 NYD_LEAVE;
75 return rv;
76 jerr:
77 rv = -1;
78 goto jleave;
81 static ssize_t
82 _qf_add_data(struct quoteflt *self, wchar_t wc)
84 char *save_b;
85 ui32_t save_l, save_w;
86 ssize_t rv = 0;
87 int w, l;
88 NYD_ENTER;
90 save_l = save_w = 0; /* silence cc */
91 save_b = NULL;
92 /* <newline> ends state */
93 if (wc == L'\n')
94 goto jflush;
95 if (wc == L'\r') /* TODO CR should be stripped in lower level!! */
96 goto jleave;
98 /* Unroll <tab> to spaces */
99 if (wc == L'\t') {
100 save_l = self->qf_datw;
101 save_w = (save_l + QUOTE_TAB_SPACES) & ~(QUOTE_TAB_SPACES - 1);
102 save_w -= save_l;
103 while (save_w-- > 0) {
104 ssize_t j = _qf_add_data(self, L' ');
105 if (j < 0) {
106 rv = j;
107 break;
109 rv += j;
111 goto jleave;
114 w = wcwidth(wc);
115 if (w == -1) {
116 jbad:
117 ++self->qf_datw;
118 self->qf_dat.s[self->qf_dat.l++] = '?';
119 } else {
120 l = wctomb(self->qf_dat.s + self->qf_dat.l, wc);
121 if (l < 0)
122 goto jbad;
123 self->qf_datw += (ui32_t)w;
124 self->qf_dat.l += (size_t)l;
127 /* TODO The last visual may excess (adjusted!) *qfold-max* if it's a wide;
128 * TODO place it on the next line, break before */
129 if (self->qf_datw >= self->qf_qfold_max) {
130 /* If we have seen a nice breakpoint during traversal, shuffle data
131 * around a bit so as to restore the trailing part after flushing */
132 if (self->qf_brkl > 0) {
133 save_w = self->qf_datw - self->qf_brkw;
134 save_l = self->qf_dat.l - self->qf_brkl;
135 save_b = self->qf_dat.s + self->qf_brkl + 2;
136 memmove(save_b, save_b - 2, save_l);
137 self->qf_dat.l = self->qf_brkl;
140 self->qf_dat.s[self->qf_dat.l++] = '\\';
141 jflush:
142 self->qf_dat.s[self->qf_dat.l++] = '\n';
143 rv = quoteflt_flush(self);
145 /* Restore takeovers, if any */
146 if (save_b != NULL) {
147 self->qf_brk_isws = FAL0;
148 self->qf_datw += save_w;
149 self->qf_dat.l = save_l;
150 memmove(self->qf_dat.s, save_b, save_l);
152 } else if (self->qf_datw >= self->qf_qfold_min && !self->qf_brk_isws) {
153 bool_t isws = iswspace(wc);
155 if ((isws && !self->qf_brk_isws) || self->qf_brkl == 0) {
156 self->qf_brkl = self->qf_dat.l;
157 self->qf_brkw = self->qf_datw;
158 self->qf_brk_isws = isws;
162 /* If state changed to prefix, perform full reset (note this implies that
163 * quoteflt_flush() performs too much work..) */
164 if (wc == '\n') {
165 self->qf_state = _QF_PREFIX;
166 self->qf_wscnt = self->qf_datw = 0;
167 self->qf_currq.l = 0;
169 jleave:
170 NYD_LEAVE;
171 return rv;
174 static ssize_t
175 _qf_state_prefix(struct qf_vc *vc)
177 struct quoteflt *self;
178 ssize_t rv;
179 char const *buf;
180 size_t len, i;
181 wchar_t wc;
182 NYD_ENTER;
184 self = vc->self;
185 rv = 0;
187 for (buf = vc->buf, len = vc->len; len > 0;) {
188 /* xxx NULL BYTE! */
189 i = mbrtowc(&wc, buf, len, self->qf_mbps);
190 if (i == (size_t)-1) {
191 /* On hard error, don't modify mbstate_t and step one byte */
192 self->qf_mbps[0] = self->qf_mbps[1];
193 ++buf;
194 --len;
195 self->qf_wscnt = 0;
196 continue;
198 self->qf_mbps[1] = self->qf_mbps[0];
199 if (i == (size_t)-2) {
200 /* Redundant shift sequence, out of buffer */
201 len = 0;
202 break;
204 buf += i;
205 len -= i;
207 if (wc == L'\n')
208 goto jfin;
209 if (iswspace(wc)) {
210 ++self->qf_wscnt;
211 continue;
213 if (i == 1 && ISQUOTE(wc)) {
214 self->qf_wscnt = 0;
215 if (self->qf_currq.l >= QUOTE_MAX - 3) {
216 self->qf_currq.s[QUOTE_MAX - 3] = '.';
217 self->qf_currq.s[QUOTE_MAX - 2] = '.';
218 self->qf_currq.s[QUOTE_MAX - 1] = '.';
219 self->qf_currq.l = QUOTE_MAX;
220 } else
221 self->qf_currq.s[self->qf_currq.l++] = buf[-1];
222 continue;
225 /* The quote is parsed and compressed; dump it */
226 jfin:
227 self->qf_datw = self->qf_pfix_len + self->qf_currq.l;
228 self->qf_state = _QF_DATA;
229 /* Overtake WS (xxx but we de-facto "normalize" to ASCII SP here) */
230 while (self->qf_wscnt-- > 0 && self->qf_currq.l < QUOTE_MAX)
231 self->qf_currq.s[self->qf_currq.l++] = ' ';
232 self->qf_wscnt = 0;
233 rv = _qf_add_data(self, wc);
234 break;
237 vc->buf = buf;
238 vc->len = len;
239 NYD_LEAVE;
240 return rv;
243 static ssize_t
244 _qf_state_data(struct qf_vc *vc)
246 struct quoteflt *self;
247 ssize_t rv;
248 char const *buf;
249 size_t len, i;
250 wchar_t wc;
251 NYD_ENTER;
253 self = vc->self;
254 rv = 0;
256 for (buf = vc->buf, len = vc->len; len > 0;) {
257 /* xxx NULL BYTE! */
258 i = mbrtowc(&wc, buf, len, self->qf_mbps);
259 if (i == (size_t)-1) {
260 /* On hard error, don't modify mbstate_t and step one byte */
261 self->qf_mbps[0] = self->qf_mbps[1];
262 ++buf;
263 --len;
264 continue;
266 self->qf_mbps[1] = self->qf_mbps[0];
267 if (i == (size_t)-2) {
268 /* Redundant shift sequence, out of buffer */
269 len = 0;
270 break;
272 buf += i;
273 len -= i;
275 { ssize_t j = _qf_add_data(self, wc);
276 if (j < 0) {
277 rv = j;
278 break;
280 rv += j;
283 if (self->qf_state != _QF_DATA)
284 break;
287 vc->buf = buf;
288 vc->len = len;
289 NYD_LEAVE;
290 return rv;
292 #endif /* HAVE_QUOTE_FOLD */
294 FL struct quoteflt *
295 quoteflt_dummy(void) /* TODO LEGACY (until filters are plugged when needed) */
297 static struct quoteflt qf_i;
299 return &qf_i;
302 FL void
303 quoteflt_init(struct quoteflt *self, char const *prefix)
305 #ifdef HAVE_QUOTE_FOLD
306 char *xcp, *cp;
307 #endif
308 NYD_ENTER;
310 memset(self, 0, sizeof *self);
312 if ((self->qf_pfix = prefix) != NULL)
313 self->qf_pfix_len = (ui32_t)strlen(prefix);
315 /* Check wether the user wants the more fancy quoting algorithm */
316 /* TODO *quote-fold*: QUOTE_MAX may excess it! */
317 #ifdef HAVE_QUOTE_FOLD
318 if (self->qf_pfix_len > 0 && (cp = ok_vlook(quote_fold)) != NULL) {
319 ui32_t qmin, qmax = (ui32_t)strtol(cp, &xcp, 10);
320 /* These magic values ensure we don't bail :) */
321 if (qmax < self->qf_pfix_len + 6)
322 qmax = self->qf_pfix_len + 6;
323 --qmax; /* The newline escape */
324 if (cp == xcp || *xcp == '\0')
325 qmin = (qmax >> 1) + (qmax >> 2) + (qmax >> 5);
326 else {
327 qmin = (ui32_t)strtol(xcp + 1, NULL, 10);
328 if (qmin < qmax >> 1)
329 qmin = qmax >> 1;
330 else if (qmin > qmax - 2)
331 qmin = qmax - 2;
333 self->qf_qfold_min = qmin;
334 self->qf_qfold_max = qmax;
336 /* Add pad for takeover copies, backslash and newline */
337 self->qf_dat.s = salloc((qmax + 3) * mb_cur_max);
338 self->qf_currq.s = salloc((QUOTE_MAX + 1) * mb_cur_max);
340 #endif
341 NYD_LEAVE;
344 FL void
345 quoteflt_destroy(struct quoteflt *self) /* xxx inline */
347 NYD_ENTER;
348 UNUSED(self);
349 NYD_LEAVE;
352 FL void
353 quoteflt_reset(struct quoteflt *self, FILE *f) /* xxx inline */
355 NYD_ENTER;
356 self->qf_os = f;
357 #ifdef HAVE_QUOTE_FOLD
358 self->qf_state = _QF_CLEAN;
359 self->qf_dat.l =
360 self->qf_currq.l = 0;
361 memset(self->qf_mbps, 0, sizeof self->qf_mbps);
362 #endif
363 NYD_LEAVE;
366 FL ssize_t
367 quoteflt_push(struct quoteflt *self, char const *dat, size_t len)
369 /* (xxx Ideally the actual push() [and flush()] would be functions on their
370 * xxx own, via indirect vtbl call ..) */
371 ssize_t rv = 0;
372 NYD_ENTER;
374 if (len == 0)
375 goto jleave;
377 /* Bypass? XXX Finally, this filter simply should not be used, then */
378 if (self->qf_pfix_len == 0) {
379 if (len != fwrite(dat, 1, len, self->qf_os))
380 goto jerr;
381 rv = len;
383 /* Normal: place *indentprefix* at every BOL */
384 else
385 #ifdef HAVE_QUOTE_FOLD
386 if (self->qf_qfold_max == 0)
387 #endif
389 void *vp;
390 size_t ll;
391 bool_t pxok = (self->qf_qfold_min != 0);
393 for (;;) {
394 if (!pxok) {
395 ll = self->qf_pfix_len;
396 if (ll != fwrite(self->qf_pfix, 1, ll, self->qf_os))
397 goto jerr;
398 rv += ll;
399 pxok = TRU1;
402 /* xxx Strictly speaking this is invalid, because only `/' and `.' are
403 * xxx mandated by POSIX.1-2008 as "invariant across all locales
404 * xxx supported"; though there is no charset known which uses this
405 * xxx control char as part of a multibyte character; note that S-nail
406 * XXX (and the Mail codebase as such) do not support EBCDIC */
407 if ((vp = memchr(dat, '\n', len)) == NULL)
408 ll = len;
409 else {
410 pxok = FAL0;
411 ll = PTR2SIZE((char*)vp - dat) + 1;
414 if (ll != fwrite(dat, sizeof *dat, ll, self->qf_os))
415 goto jerr;
416 rv += ll;
417 if ((len -= ll) == 0)
418 break;
419 dat += ll;
422 self->qf_qfold_min = pxok;
424 /* Overly complicated, though still only line-per-line: *quote-fold*.
425 * - If .qf_currq.l is 0, then we are in a clean state. Reset .qf_mbps;
426 * TODO note this means we assume that lines start with reset escape seq,
427 * TODO but i don't think this is any worse than what we currently do;
428 * TODO in 15.0, with the value carrier, we should carry conversion states
429 * TODO all along, only resetting on error (or at words for header =???=);
430 * TODO this still is weird for error handling, but we need to act more
431 * TODO stream-alike (though in practice i don't think cross-line states
432 * TODO can be found, because of compatibility reasons; however, being
433 * TODO a problem rather than a solution is not a good thing (tm))
434 * - Lookout for a newline */
435 #ifdef HAVE_QUOTE_FOLD
436 else {
437 struct qf_vc vc;
438 ssize_t i;
440 vc.self = self;
441 vc.buf = dat;
442 vc.len = len;
443 while (vc.len > 0) {
444 switch (self->qf_state) {
445 case _QF_CLEAN:
446 case _QF_PREFIX:
447 i = _qf_state_prefix(&vc);
448 break;
449 default: /* silence cc (`i' unused) */
450 case _QF_DATA:
451 i = _qf_state_data(&vc);
452 break;
454 if (i < 0)
455 goto jerr;
456 rv += i;
459 #endif /* HAVE_QUOTE_FOLD */
461 jleave:
462 NYD_LEAVE;
463 return rv;
464 jerr:
465 rv = -1;
466 goto jleave;
469 FL ssize_t
470 quoteflt_flush(struct quoteflt *self)
472 ssize_t rv = 0;
473 NYD_ENTER;
474 UNUSED(self);
476 #ifdef HAVE_QUOTE_FOLD
477 if (self->qf_dat.l > 0) {
478 rv = _qf_dump_prefix(self);
479 if (rv >= 0) {
480 size_t i = self->qf_dat.l;
481 if (i == fwrite(self->qf_dat.s, 1, i, self->qf_os))
482 rv += i;
483 else
484 rv = -1;
485 self->qf_dat.l = 0;
486 self->qf_brk_isws = FAL0;
487 self->qf_wscnt = self->qf_brkl = self->qf_brkw = 0;
488 self->qf_datw = self->qf_pfix_len + self->qf_currq.l;
491 #endif
492 NYD_LEAVE;
493 return rv;
496 /* s-it-mode */