NeoMutt  2024-11-14-138-ge5ca67
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
enriched.c
Go to the documentation of this file.
1
33#include "config.h"
34#include <stdbool.h>
35#include <stddef.h>
36#include <stdio.h>
37#include <wchar.h>
38#include <wctype.h>
39#include "mutt/lib.h"
40#include "email/lib.h"
41#include "enriched.h"
42
43#define INDENT_SIZE 4
44
49{
63};
64
68struct Etags
69{
70 const wchar_t *tag_name;
71 int index;
72};
73
75static const struct Etags EnrichedTags[] = {
76 // clang-format off
77 { L"param", RICH_PARAM },
78 { L"bold", RICH_BOLD },
79 { L"italic", RICH_ITALIC },
80 { L"underline", RICH_UNDERLINE },
81 { L"nofill", RICH_NOFILL },
82 { L"excerpt", RICH_EXCERPT },
83 { L"indent", RICH_INDENT },
84 { L"indentright", RICH_INDENT_RIGHT },
85 { L"center", RICH_CENTER },
86 { L"flushleft", RICH_FLUSHLEFT },
87 { L"flushright", RICH_FLUSHRIGHT },
88 { L"flushboth", RICH_FLUSHLEFT },
89 { L"color", RICH_COLOR },
90 { L"x-color", RICH_COLOR },
91 { NULL, -1 },
92 // clang-format on
93};
94
99{
100 wchar_t *buffer;
101 wchar_t *line;
102 wchar_t *param;
103 size_t buf_len;
104 size_t line_len;
105 size_t line_used;
106 size_t line_max;
108 size_t word_len;
109 size_t buf_used;
111 size_t param_len;
114 struct State *state;
115};
116
121static void enriched_wrap(struct EnrichedState *enriched)
122{
123 if (!enriched)
124 return;
125
126 int x;
127
128 if (enriched->line_len)
129 {
130 if (enriched->tag_level[RICH_CENTER] || enriched->tag_level[RICH_FLUSHRIGHT])
131 {
132 /* Strip trailing white space */
133 size_t y = enriched->line_used - 1;
134
135 while (y && iswspace(enriched->line[y]))
136 {
137 enriched->line[y] = (wchar_t) '\0';
138 y--;
139 enriched->line_used--;
140 enriched->line_len--;
141 }
142 if (enriched->tag_level[RICH_CENTER])
143 {
144 /* Strip leading whitespace */
145 y = 0;
146
147 while (enriched->line[y] && iswspace(enriched->line[y]))
148 y++;
149 if (y)
150 {
151 for (size_t z = y; z <= enriched->line_used; z++)
152 {
153 enriched->line[z - y] = enriched->line[z];
154 }
155
156 enriched->line_len -= y;
157 enriched->line_used -= y;
158 }
159 }
160 }
161
162 const int extra = enriched->wrap_margin - enriched->line_len - enriched->indent_len -
164 if (extra > 0)
165 {
166 if (enriched->tag_level[RICH_CENTER])
167 {
168 x = extra / 2;
169 while (x)
170 {
171 state_putc(enriched->state, ' ');
172 x--;
173 }
174 }
175 else if (enriched->tag_level[RICH_FLUSHRIGHT])
176 {
177 x = extra - 1;
178 while (x)
179 {
180 state_putc(enriched->state, ' ');
181 x--;
182 }
183 }
184 }
185 state_putws(enriched->state, (const wchar_t *) enriched->line);
186 }
187
188 state_putc(enriched->state, '\n');
189 enriched->line[0] = (wchar_t) '\0';
190 enriched->line_len = 0;
191 enriched->line_used = 0;
192 enriched->indent_len = 0;
193 if (enriched->state->prefix)
194 {
195 state_puts(enriched->state, enriched->state->prefix);
196 enriched->indent_len += mutt_str_len(enriched->state->prefix);
197 }
198
199 if (enriched->tag_level[RICH_EXCERPT])
200 {
201 x = enriched->tag_level[RICH_EXCERPT];
202 while (x)
203 {
204 if (enriched->state->prefix)
205 {
206 state_puts(enriched->state, enriched->state->prefix);
207 enriched->indent_len += mutt_str_len(enriched->state->prefix);
208 }
209 else
210 {
211 state_puts(enriched->state, "> ");
212 enriched->indent_len += mutt_str_len("> ");
213 }
214 x--;
215 }
216 }
217 else
218 {
219 enriched->indent_len = 0;
220 }
221 if (enriched->tag_level[RICH_INDENT])
222 {
223 x = enriched->tag_level[RICH_INDENT] * INDENT_SIZE;
224 enriched->indent_len += x;
225 while (x)
226 {
227 state_putc(enriched->state, ' ');
228 x--;
229 }
230 }
231}
232
238static void enriched_flush(struct EnrichedState *enriched, bool wrap)
239{
240 if (!enriched || !enriched->buffer)
241 return;
242
243 if (!enriched->tag_level[RICH_NOFILL] &&
244 ((enriched->line_len + enriched->word_len) >
245 (enriched->wrap_margin - (enriched->tag_level[RICH_INDENT_RIGHT] * INDENT_SIZE) -
246 enriched->indent_len)))
247 {
248 enriched_wrap(enriched);
249 }
250
251 if (enriched->buf_used)
252 {
253 enriched->buffer[enriched->buf_used] = (wchar_t) '\0';
254 enriched->line_used += enriched->buf_used;
255 if (enriched->line_used > enriched->line_max)
256 {
257 enriched->line_max = enriched->line_used;
258 MUTT_MEM_REALLOC(&enriched->line, enriched->line_max + 1, wchar_t);
259 }
260 wcscat(enriched->line, enriched->buffer);
261 enriched->line_len += enriched->word_len;
262 enriched->word_len = 0;
263 enriched->buf_used = 0;
264 }
265 if (wrap)
266 enriched_wrap(enriched);
267 fflush(enriched->state->fp_out);
268}
269
275static void enriched_putwc(wchar_t c, struct EnrichedState *enriched)
276{
277 if (!enriched)
278 return;
279
280 if (enriched->tag_level[RICH_PARAM])
281 {
282 if (enriched->tag_level[RICH_COLOR])
283 {
284 if ((enriched->param_used + 1) >= enriched->param_len)
285 {
286 enriched->param_len += 256;
287 MUTT_MEM_REALLOC(&enriched->param, enriched->param_len, wchar_t);
288 }
289
290 enriched->param[enriched->param_used++] = c;
291 }
292 return; /* nothing to do */
293 }
294
295 /* see if more space is needed (plus extra for possible rich characters) */
296 if ((enriched->buf_len < (enriched->buf_used + 3)) || !enriched->buffer)
297 {
298 enriched->buf_len += 1024;
299 MUTT_MEM_REALLOC(&enriched->buffer, enriched->buf_len + 1, wchar_t);
300 }
301
302 if ((!enriched->tag_level[RICH_NOFILL] && iswspace(c)) || (c == (wchar_t) '\0'))
303 {
304 if (c == (wchar_t) '\t')
305 enriched->word_len += 8 - (enriched->line_len + enriched->word_len) % 8;
306 else
307 enriched->word_len++;
308
309 enriched->buffer[enriched->buf_used++] = c;
310 enriched_flush(enriched, false);
311 }
312 else
313 {
314 if (enriched->state->flags & STATE_DISPLAY)
315 {
316 if (enriched->tag_level[RICH_BOLD])
317 {
318 enriched->buffer[enriched->buf_used++] = c;
319 enriched->buffer[enriched->buf_used++] = (wchar_t) '\010'; // Ctrl-H (backspace)
320 enriched->buffer[enriched->buf_used++] = c;
321 }
322 else if (enriched->tag_level[RICH_UNDERLINE])
323 {
324 enriched->buffer[enriched->buf_used++] = '_';
325 enriched->buffer[enriched->buf_used++] = (wchar_t) '\010'; // Ctrl-H (backspace)
326 enriched->buffer[enriched->buf_used++] = c;
327 }
328 else if (enriched->tag_level[RICH_ITALIC])
329 {
330 enriched->buffer[enriched->buf_used++] = c;
331 enriched->buffer[enriched->buf_used++] = (wchar_t) '\010'; // Ctrl-H (backspace)
332 enriched->buffer[enriched->buf_used++] = '_';
333 }
334 else
335 {
336 enriched->buffer[enriched->buf_used++] = c;
337 }
338 }
339 else
340 {
341 enriched->buffer[enriched->buf_used++] = c;
342 }
343 enriched->word_len++;
344 }
345}
346
352static void enriched_puts(const char *s, struct EnrichedState *enriched)
353{
354 if (!enriched)
355 return;
356
357 const char *c = NULL;
358
359 if ((enriched->buf_len < (enriched->buf_used + mutt_str_len(s))) || !enriched->buffer)
360 {
361 enriched->buf_len += 1024;
362 MUTT_MEM_REALLOC(&enriched->buffer, enriched->buf_len + 1, wchar_t);
363 }
364 c = s;
365 while (*c)
366 {
367 enriched->buffer[enriched->buf_used++] = (wchar_t) *c;
368 c++;
369 }
370}
371
377static void enriched_set_flags(const wchar_t *tag, struct EnrichedState *enriched)
378{
379 if (!enriched)
380 return;
381
382 const wchar_t *tagptr = tag;
383 int i, j;
384
385 if (*tagptr == (wchar_t) '/')
386 tagptr++;
387
388 for (i = 0, j = -1; EnrichedTags[i].tag_name; i++)
389 {
390 if (wcscasecmp(EnrichedTags[i].tag_name, tagptr) == 0)
391 {
392 j = EnrichedTags[i].index;
393 break;
394 }
395 }
396
397 if (j != -1)
398 {
399 if ((j == RICH_CENTER) || (j == RICH_FLUSHLEFT) || (j == RICH_FLUSHRIGHT))
400 enriched_flush(enriched, true);
401
402 if (*tag == (wchar_t) '/')
403 {
404 if (enriched->tag_level[j]) /* make sure not to go negative */
405 enriched->tag_level[j]--;
406 if ((enriched->state->flags & STATE_DISPLAY) && (j == RICH_PARAM) &&
407 enriched->tag_level[RICH_COLOR])
408 {
409 enriched->param[enriched->param_used] = (wchar_t) '\0';
410 if (wcscasecmp(L"black", enriched->param) == 0)
411 {
412 enriched_puts("\033[30m", enriched); // Escape
413 }
414 else if (wcscasecmp(L"red", enriched->param) == 0)
415 {
416 enriched_puts("\033[31m", enriched); // Escape
417 }
418 else if (wcscasecmp(L"green", enriched->param) == 0)
419 {
420 enriched_puts("\033[32m", enriched); // Escape
421 }
422 else if (wcscasecmp(L"yellow", enriched->param) == 0)
423 {
424 enriched_puts("\033[33m", enriched); // Escape
425 }
426 else if (wcscasecmp(L"blue", enriched->param) == 0)
427 {
428 enriched_puts("\033[34m", enriched); // Escape
429 }
430 else if (wcscasecmp(L"magenta", enriched->param) == 0)
431 {
432 enriched_puts("\033[35m", enriched); // Escape
433 }
434 else if (wcscasecmp(L"cyan", enriched->param) == 0)
435 {
436 enriched_puts("\033[36m", enriched); // Escape
437 }
438 else if (wcscasecmp(L"white", enriched->param) == 0)
439 {
440 enriched_puts("\033[37m", enriched); // Escape
441 }
442 }
443 if ((enriched->state->flags & STATE_DISPLAY) && (j == RICH_COLOR))
444 {
445 enriched_puts("\033[0m", enriched); // Escape
446 }
447
448 /* flush parameter buffer when closing the tag */
449 if (j == RICH_PARAM)
450 {
451 enriched->param_used = 0;
452 enriched->param[0] = (wchar_t) '\0';
453 }
454 }
455 else
456 {
457 enriched->tag_level[j]++;
458 }
459
460 if (j == RICH_EXCERPT)
461 enriched_flush(enriched, true);
462 }
463}
464
469int text_enriched_handler(struct Body *b_email, struct State *state)
470{
471 enum
472 {
473 TEXT,
474 LANGLE,
475 TAG,
476 BOGUS_TAG,
477 NEWLINE,
478 ST_EOF,
479 DONE
480 } text_state = TEXT;
481
482 long bytes = b_email->length;
483 struct EnrichedState enriched = { 0 };
484 wint_t wc = 0;
485 int tag_len = 0;
486 wchar_t tag[1024 + 1];
487
488 enriched.state = state;
489 enriched.wrap_margin = ((state->wraplen > 4) &&
490 ((state->flags & STATE_DISPLAY) || (state->wraplen < 76))) ?
491 state->wraplen - 4 :
492 72;
493 enriched.line_max = enriched.wrap_margin * 4;
494 enriched.line = MUTT_MEM_CALLOC(enriched.line_max + 1, wchar_t);
495 enriched.param = MUTT_MEM_CALLOC(256, wchar_t);
496
497 enriched.param_len = 256;
498 enriched.param_used = 0;
499
500 if (state->prefix)
501 {
503 enriched.indent_len += mutt_str_len(state->prefix);
504 }
505
506 while (text_state != DONE)
507 {
508 if (text_state != ST_EOF)
509 {
510 if (!bytes || ((wc = fgetwc(state->fp_in)) == WEOF))
511 text_state = ST_EOF;
512 else
513 bytes--;
514 }
515
516 switch (text_state)
517 {
518 case TEXT:
519 switch (wc)
520 {
521 case '<':
522 text_state = LANGLE;
523 break;
524
525 case '\n':
526 if (enriched.tag_level[RICH_NOFILL])
527 {
528 enriched_flush(&enriched, true);
529 }
530 else
531 {
532 enriched_putwc((wchar_t) ' ', &enriched);
533 text_state = NEWLINE;
534 }
535 break;
536
537 default:
538 enriched_putwc(wc, &enriched);
539 }
540 break;
541
542 case LANGLE:
543 if (wc == (wchar_t) '<')
544 {
545 enriched_putwc(wc, &enriched);
546 text_state = TEXT;
547 break;
548 }
549 else
550 {
551 tag_len = 0;
552 text_state = TAG;
553 }
554 /* Yes, (it wasn't a <<, so this char is first in TAG) */
556
557 case TAG:
558 if (wc == (wchar_t) '>')
559 {
560 tag[tag_len] = (wchar_t) '\0';
561 enriched_set_flags(tag, &enriched);
562 text_state = TEXT;
563 }
564 else if (tag_len < 1024) /* ignore overly long tags */
565 {
566 tag[tag_len++] = wc;
567 }
568 else
569 {
570 text_state = BOGUS_TAG;
571 }
572 break;
573
574 case BOGUS_TAG:
575 if (wc == (wchar_t) '>')
576 text_state = TEXT;
577 break;
578
579 case NEWLINE:
580 if (wc == (wchar_t) '\n')
581 {
582 enriched_flush(&enriched, true);
583 }
584 else
585 {
586 ungetwc(wc, state->fp_in);
587 bytes++;
588 text_state = TEXT;
589 }
590 break;
591
592 case ST_EOF:
593 enriched_putwc((wchar_t) '\0', &enriched);
594 enriched_flush(&enriched, true);
595 text_state = DONE;
596 break;
597
598 default:
599 /* not reached */
600 break;
601 }
602 }
603
604 state_putc(state, '\n'); /* add a final newline */
605
606 FREE(&(enriched.buffer));
607 FREE(&(enriched.line));
608 FREE(&(enriched.param));
609
610 return 0;
611}
Structs that make up an email.
static void enriched_set_flags(const wchar_t *tag, struct EnrichedState *enriched)
Set flags on the enriched text state.
Definition: enriched.c:377
static void enriched_wrap(struct EnrichedState *enriched)
Wrap enriched text.
Definition: enriched.c:121
RichAttribs
Rich text attributes.
Definition: enriched.c:49
@ RICH_FLUSHRIGHT
Right-justified text.
Definition: enriched.c:60
@ RICH_NOFILL
Text will not be reformatted.
Definition: enriched.c:54
@ RICH_COLOR
Coloured text.
Definition: enriched.c:61
@ RICH_PARAM
Parameter label.
Definition: enriched.c:50
@ RICH_INDENT
Indented text.
Definition: enriched.c:55
@ RICH_BOLD
Bold text.
Definition: enriched.c:51
@ RICH_CENTER
Centred text.
Definition: enriched.c:58
@ RICH_ITALIC
Italic text.
Definition: enriched.c:53
@ RICH_UNDERLINE
Underlined text.
Definition: enriched.c:52
@ RICH_EXCERPT
Excerpt text.
Definition: enriched.c:57
@ RICH_INDENT_RIGHT
Right-indented text.
Definition: enriched.c:56
@ RICH_MAX
Definition: enriched.c:62
@ RICH_FLUSHLEFT
Left-justified text.
Definition: enriched.c:59
static void enriched_putwc(wchar_t c, struct EnrichedState *enriched)
Write one wide character to the state.
Definition: enriched.c:275
static const struct Etags EnrichedTags[]
EnrichedTags - Lookup table of tags allowed in enriched text.
Definition: enriched.c:75
#define INDENT_SIZE
A (not so) minimal implementation of RFC1563.
Definition: enriched.c:43
static void enriched_flush(struct EnrichedState *enriched, bool wrap)
Write enriched text to the State.
Definition: enriched.c:238
static void enriched_puts(const char *s, struct EnrichedState *enriched)
Write an enriched text string to the State.
Definition: enriched.c:352
Rich text handler.
int text_enriched_handler(struct Body *b_email, struct State *state)
Handler for enriched text - Implements handler_t -.
Definition: enriched.c:469
#define FREE(x)
Definition: memory.h:55
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
#define MUTT_MEM_REALLOC(pptr, n, type)
Definition: memory.h:43
Convenience wrapper for the library headers.
#define FALLTHROUGH
Definition: lib.h:111
int state_putws(struct State *state, const wchar_t *ws)
Write a wide string to the state.
Definition: state.c:147
#define state_puts(STATE, STR)
Definition: state.h:58
#define STATE_DISPLAY
Output is displayed to the user.
Definition: state.h:33
#define state_putc(STATE, STR)
Definition: state.h:59
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:496
int wcscasecmp(const wchar_t *a, const wchar_t *b)
Compare two wide-character strings, ignoring case.
Definition: wcscasecmp.c:41
The body of an email.
Definition: body.h:36
LOFF_T length
length (in bytes) of attachment
Definition: body.h:53
State of enriched-text parser.
Definition: enriched.c:99
wchar_t * buffer
Definition: enriched.c:100
size_t buf_len
Definition: enriched.c:103
size_t param_used
Definition: enriched.c:110
wchar_t * param
Definition: enriched.c:102
wchar_t * line
Definition: enriched.c:101
int tag_level[RICH_MAX]
Definition: enriched.c:112
size_t buf_used
Definition: enriched.c:109
size_t line_used
Definition: enriched.c:105
struct State * state
Definition: enriched.c:114
size_t word_len
Definition: enriched.c:108
size_t param_len
Definition: enriched.c:111
size_t line_len
Definition: enriched.c:104
size_t line_max
Definition: enriched.c:106
int wrap_margin
Definition: enriched.c:113
size_t indent_len
Definition: enriched.c:107
Enriched text tags.
Definition: enriched.c:69
int index
Index number.
Definition: enriched.c:71
const wchar_t * tag_name
Tag name.
Definition: enriched.c:70
Keep track when processing files.
Definition: state.h:48
int wraplen
Width to wrap lines to (when flags & STATE_DISPLAY)
Definition: state.h:53
StateFlags flags
Flags, e.g. STATE_DISPLAY.
Definition: state.h:52
FILE * fp_out
File to write to.
Definition: state.h:50
FILE * fp_in
File to read from.
Definition: state.h:49
const char * prefix
String to add to the beginning of each output line.
Definition: state.h:51