forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enriched.c
595 lines (541 loc) · 13.6 KB
/
enriched.c
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/**
* @file
* Rich text handler
*
* @authors
* Copyright (C) 2018 Richard Russon <rich@flatcap.org>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page enriched Rich text handler
*
* Rich text handler
*/
/**
* A (not so) minimal implementation of RFC1563.
*/
#include "config.h"
#include <stddef.h>
#include <stdbool.h>
#include <stdio.h>
#include <wchar.h>
#include <wctype.h>
#include "mutt/mutt.h"
#include "email/lib.h"
#include "mutt_window.h"
#include "state.h"
#define INDENT_SIZE 4
/**
* enum RichAttribs - Rich text attributes
*/
enum RichAttribs
{
RICH_PARAM = 0, ///< Parameter label
RICH_BOLD, ///< Bold text
RICH_UNDERLINE, ///< Underlined text
RICH_ITALIC, ///< Italic text
RICH_NOFILL, ///< Text will not be reformatted
RICH_INDENT, ///< Indented text
RICH_INDENT_RIGHT, ///< Right-indented text
RICH_EXCERPT, ///< Excerpt text
RICH_CENTER, ///< Centred text
RICH_FLUSHLEFT, ///< Left-justified text
RICH_FLUSHRIGHT, ///< Right-justified text
RICH_COLOR, ///< Coloured text
RICH_MAX,
};
/**
* struct Etags - Enriched text tags
*/
struct Etags
{
const wchar_t *tag_name;
int index;
};
// clang-format off
static const struct Etags EnrichedTags[] = {
{ L"param", RICH_PARAM },
{ L"bold", RICH_BOLD },
{ L"italic", RICH_ITALIC },
{ L"underline", RICH_UNDERLINE },
{ L"nofill", RICH_NOFILL },
{ L"excerpt", RICH_EXCERPT },
{ L"indent", RICH_INDENT },
{ L"indentright", RICH_INDENT_RIGHT },
{ L"center", RICH_CENTER },
{ L"flushleft", RICH_FLUSHLEFT },
{ L"flushright", RICH_FLUSHRIGHT },
{ L"flushboth", RICH_FLUSHLEFT },
{ L"color", RICH_COLOR },
{ L"x-color", RICH_COLOR },
{ NULL, -1 },
};
// clang-format on
/**
* struct EnrichedState - State of enriched-text parser
*/
struct EnrichedState
{
wchar_t *buffer;
wchar_t *line;
wchar_t *param;
size_t buf_len;
size_t line_len;
size_t line_used;
size_t line_max;
size_t indent_len;
size_t word_len;
size_t buf_used;
size_t param_used;
size_t param_len;
int tag_level[RICH_MAX];
int wrap_margin;
struct State *s;
};
/**
* enriched_wrap - Wrap enriched text
* @param stte State of enriched text
*/
static void enriched_wrap(struct EnrichedState *stte)
{
if (!stte)
return;
int x;
if (stte->line_len)
{
if (stte->tag_level[RICH_CENTER] || stte->tag_level[RICH_FLUSHRIGHT])
{
/* Strip trailing white space */
size_t y = stte->line_used - 1;
while (y && iswspace(stte->line[y]))
{
stte->line[y] = (wchar_t) '\0';
y--;
stte->line_used--;
stte->line_len--;
}
if (stte->tag_level[RICH_CENTER])
{
/* Strip leading whitespace */
y = 0;
while (stte->line[y] && iswspace(stte->line[y]))
y++;
if (y)
{
for (size_t z = y; z <= stte->line_used; z++)
{
stte->line[z - y] = stte->line[z];
}
stte->line_len -= y;
stte->line_used -= y;
}
}
}
const int extra = stte->wrap_margin - stte->line_len - stte->indent_len -
(stte->tag_level[RICH_INDENT_RIGHT] * INDENT_SIZE);
if (extra > 0)
{
if (stte->tag_level[RICH_CENTER])
{
x = extra / 2;
while (x)
{
state_putc(' ', stte->s);
x--;
}
}
else if (stte->tag_level[RICH_FLUSHRIGHT])
{
x = extra - 1;
while (x)
{
state_putc(' ', stte->s);
x--;
}
}
}
state_putws((const wchar_t *) stte->line, stte->s);
}
state_putc('\n', stte->s);
stte->line[0] = (wchar_t) '\0';
stte->line_len = 0;
stte->line_used = 0;
stte->indent_len = 0;
if (stte->s->prefix)
{
state_puts(stte->s->prefix, stte->s);
stte->indent_len += mutt_str_strlen(stte->s->prefix);
}
if (stte->tag_level[RICH_EXCERPT])
{
x = stte->tag_level[RICH_EXCERPT];
while (x)
{
if (stte->s->prefix)
{
state_puts(stte->s->prefix, stte->s);
stte->indent_len += mutt_str_strlen(stte->s->prefix);
}
else
{
state_puts("> ", stte->s);
stte->indent_len += mutt_str_strlen("> ");
}
x--;
}
}
else
stte->indent_len = 0;
if (stte->tag_level[RICH_INDENT])
{
x = stte->tag_level[RICH_INDENT] * INDENT_SIZE;
stte->indent_len += x;
while (x)
{
state_putc(' ', stte->s);
x--;
}
}
}
/**
* enriched_flush - Write enriched text to the State
* @param stte State of Enriched text
* @param wrap true if the text should be wrapped
*/
static void enriched_flush(struct EnrichedState *stte, bool wrap)
{
if (!stte)
return;
if (!stte->tag_level[RICH_NOFILL] &&
((stte->line_len + stte->word_len) >
(stte->wrap_margin - (stte->tag_level[RICH_INDENT_RIGHT] * INDENT_SIZE) - stte->indent_len)))
{
enriched_wrap(stte);
}
if (stte->buf_used)
{
stte->buffer[stte->buf_used] = (wchar_t) '\0';
stte->line_used += stte->buf_used;
if (stte->line_used > stte->line_max)
{
stte->line_max = stte->line_used;
mutt_mem_realloc(&stte->line, (stte->line_max + 1) * sizeof(wchar_t));
}
wcscat(stte->line, stte->buffer);
stte->line_len += stte->word_len;
stte->word_len = 0;
stte->buf_used = 0;
}
if (wrap)
enriched_wrap(stte);
fflush(stte->s->fp_out);
}
/**
* enriched_putwc - Write one wide character to the state
* @param c Character to write
* @param stte State of Enriched text
*/
static void enriched_putwc(wchar_t c, struct EnrichedState *stte)
{
if (!stte)
return;
if (stte->tag_level[RICH_PARAM])
{
if (stte->tag_level[RICH_COLOR])
{
if (stte->param_used + 1 >= stte->param_len)
mutt_mem_realloc(&stte->param, (stte->param_len += 256) * sizeof(wchar_t));
stte->param[stte->param_used++] = c;
}
return; /* nothing to do */
}
/* see if more space is needed (plus extra for possible rich characters) */
if (stte->buf_len < (stte->buf_used + 3))
{
stte->buf_len += 1024;
mutt_mem_realloc(&stte->buffer, (stte->buf_len + 1) * sizeof(wchar_t));
}
if ((!stte->tag_level[RICH_NOFILL] && iswspace(c)) || (c == (wchar_t) '\0'))
{
if (c == (wchar_t) '\t')
stte->word_len += 8 - (stte->line_len + stte->word_len) % 8;
else
stte->word_len++;
stte->buffer[stte->buf_used++] = c;
enriched_flush(stte, false);
}
else
{
if (stte->s->flags & MUTT_DISPLAY)
{
if (stte->tag_level[RICH_BOLD])
{
stte->buffer[stte->buf_used++] = c;
stte->buffer[stte->buf_used++] = (wchar_t) '\010';
stte->buffer[stte->buf_used++] = c;
}
else if (stte->tag_level[RICH_UNDERLINE])
{
stte->buffer[stte->buf_used++] = '_';
stte->buffer[stte->buf_used++] = (wchar_t) '\010';
stte->buffer[stte->buf_used++] = c;
}
else if (stte->tag_level[RICH_ITALIC])
{
stte->buffer[stte->buf_used++] = c;
stte->buffer[stte->buf_used++] = (wchar_t) '\010';
stte->buffer[stte->buf_used++] = '_';
}
else
{
stte->buffer[stte->buf_used++] = c;
}
}
else
{
stte->buffer[stte->buf_used++] = c;
}
stte->word_len++;
}
}
/**
* enriched_puts - Write an enriched text string to the State
* @param s String to write
* @param stte State of Enriched text
*/
static void enriched_puts(const char *s, struct EnrichedState *stte)
{
if (!stte)
return;
const char *c = NULL;
if (stte->buf_len < (stte->buf_used + mutt_str_strlen(s)))
{
stte->buf_len += 1024;
mutt_mem_realloc(&stte->buffer, (stte->buf_len + 1) * sizeof(wchar_t));
}
c = s;
while (*c)
{
stte->buffer[stte->buf_used++] = (wchar_t) *c;
c++;
}
}
/**
* enriched_set_flags - Set flags on the enriched text state
* @param tag Tag to set
* @param stte State of Enriched text
*/
static void enriched_set_flags(const wchar_t *tag, struct EnrichedState *stte)
{
if (!stte)
return;
const wchar_t *tagptr = tag;
int i, j;
if (*tagptr == (wchar_t) '/')
tagptr++;
for (i = 0, j = -1; EnrichedTags[i].tag_name; i++)
{
if (wcscasecmp(EnrichedTags[i].tag_name, tagptr) == 0)
{
j = EnrichedTags[i].index;
break;
}
}
if (j != -1)
{
if ((j == RICH_CENTER) || (j == RICH_FLUSHLEFT) || (j == RICH_FLUSHRIGHT))
enriched_flush(stte, true);
if (*tag == (wchar_t) '/')
{
if (stte->tag_level[j]) /* make sure not to go negative */
stte->tag_level[j]--;
if ((stte->s->flags & MUTT_DISPLAY) && (j == RICH_PARAM) && stte->tag_level[RICH_COLOR])
{
stte->param[stte->param_used] = (wchar_t) '\0';
if (wcscasecmp(L"black", stte->param) == 0)
{
enriched_puts("\033[30m", stte);
}
else if (wcscasecmp(L"red", stte->param) == 0)
{
enriched_puts("\033[31m", stte);
}
else if (wcscasecmp(L"green", stte->param) == 0)
{
enriched_puts("\033[32m", stte);
}
else if (wcscasecmp(L"yellow", stte->param) == 0)
{
enriched_puts("\033[33m", stte);
}
else if (wcscasecmp(L"blue", stte->param) == 0)
{
enriched_puts("\033[34m", stte);
}
else if (wcscasecmp(L"magenta", stte->param) == 0)
{
enriched_puts("\033[35m", stte);
}
else if (wcscasecmp(L"cyan", stte->param) == 0)
{
enriched_puts("\033[36m", stte);
}
else if (wcscasecmp(L"white", stte->param) == 0)
{
enriched_puts("\033[37m", stte);
}
}
if ((stte->s->flags & MUTT_DISPLAY) && (j == RICH_COLOR))
{
enriched_puts("\033[0m", stte);
}
/* flush parameter buffer when closing the tag */
if (j == RICH_PARAM)
{
stte->param_used = 0;
stte->param[0] = (wchar_t) '\0';
}
}
else
stte->tag_level[j]++;
if (j == RICH_EXCERPT)
enriched_flush(stte, true);
}
}
/**
* text_enriched_handler - Handler for enriched text - Implements ::handler_t
* @retval 0 Always
*/
int text_enriched_handler(struct Body *a, struct State *s)
{
enum
{
TEXT,
LANGLE,
TAG,
BOGUS_TAG,
NEWLINE,
ST_EOF,
DONE
} state = TEXT;
long bytes = a->length;
struct EnrichedState stte = { 0 };
wchar_t wc = 0;
int tag_len = 0;
wchar_t tag[1024 + 1];
stte.s = s;
stte.wrap_margin =
((s->flags & MUTT_DISPLAY) ?
(MuttIndexWindow->cols - 4) :
((MuttIndexWindow->cols - 4) < 72) ? (MuttIndexWindow->cols - 4) : 72);
stte.line_max = stte.wrap_margin * 4;
stte.line = mutt_mem_calloc(1, (stte.line_max + 1) * sizeof(wchar_t));
stte.param = mutt_mem_calloc(1, 256 * sizeof(wchar_t));
stte.param_len = 256;
stte.param_used = 0;
if (s->prefix)
{
state_puts(s->prefix, s);
stte.indent_len += mutt_str_strlen(s->prefix);
}
while (state != DONE)
{
if (state != ST_EOF)
{
if (!bytes || ((wc = fgetwc(s->fp_in)) == WEOF))
state = ST_EOF;
else
bytes--;
}
switch (state)
{
case TEXT:
switch (wc)
{
case '<':
state = LANGLE;
break;
case '\n':
if (stte.tag_level[RICH_NOFILL])
{
enriched_flush(&stte, true);
}
else
{
enriched_putwc((wchar_t) ' ', &stte);
state = NEWLINE;
}
break;
default:
enriched_putwc(wc, &stte);
}
break;
case LANGLE:
if (wc == (wchar_t) '<')
{
enriched_putwc(wc, &stte);
state = TEXT;
break;
}
else
{
tag_len = 0;
state = TAG;
}
/* Yes, (it wasn't a <<, so this char is first in TAG) */
/* fallthrough */
case TAG:
if (wc == (wchar_t) '>')
{
tag[tag_len] = (wchar_t) '\0';
enriched_set_flags(tag, &stte);
state = TEXT;
}
else if (tag_len < 1024) /* ignore overly long tags */
tag[tag_len++] = wc;
else
state = BOGUS_TAG;
break;
case BOGUS_TAG:
if (wc == (wchar_t) '>')
state = TEXT;
break;
case NEWLINE:
if (wc == (wchar_t) '\n')
enriched_flush(&stte, true);
else
{
ungetwc(wc, s->fp_in);
bytes++;
state = TEXT;
}
break;
case ST_EOF:
enriched_putwc((wchar_t) '\0', &stte);
enriched_flush(&stte, true);
state = DONE;
break;
case DONE:
/* not reached */
break;
}
}
state_putc('\n', s); /* add a final newline */
FREE(&(stte.buffer));
FREE(&(stte.line));
FREE(&(stte.param));
return 0;
}