NeoMutt  2020-04-24
Teaching an old dog new tricks
DOXYGEN
enriched.h File Reference

Rich text handler. More...

+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int text_enriched_handler (struct Body *a, struct State *s)
 Handler for enriched text - Implements handler_t. More...
 

Detailed Description

Rich text handler.

Authors
  • Richard Russon

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/.

Definition in file enriched.h.

Function Documentation

◆ text_enriched_handler()

int text_enriched_handler ( struct Body a,
struct State s 
)

Handler for enriched text - Implements handler_t.

Return values
0Always

Definition at line 460 of file enriched.c.

461 {
462  enum
463  {
464  TEXT,
465  LANGLE,
466  TAG,
467  BOGUS_TAG,
468  NEWLINE,
469  ST_EOF,
470  DONE
471  } state = TEXT;
472 
473  long bytes = a->length;
474  struct EnrichedState stte = { 0 };
475  wint_t wc = 0;
476  int tag_len = 0;
477  wchar_t tag[1024 + 1];
478 
479  stte.s = s;
480  stte.wrap_margin =
481  ((s->wraplen > 4) && ((s->flags & MUTT_DISPLAY) || (s->wraplen < 76))) ?
482  s->wraplen - 4 :
483  72;
484  stte.line_max = stte.wrap_margin * 4;
485  stte.line = mutt_mem_calloc((stte.line_max + 1), sizeof(wchar_t));
486  stte.param = mutt_mem_calloc(256, sizeof(wchar_t));
487 
488  stte.param_len = 256;
489  stte.param_used = 0;
490 
491  if (s->prefix)
492  {
493  state_puts(s, s->prefix);
494  stte.indent_len += mutt_str_strlen(s->prefix);
495  }
496 
497  while (state != DONE)
498  {
499  if (state != ST_EOF)
500  {
501  if (!bytes || ((wc = fgetwc(s->fp_in)) == WEOF))
502  state = ST_EOF;
503  else
504  bytes--;
505  }
506 
507  switch (state)
508  {
509  case TEXT:
510  switch (wc)
511  {
512  case '<':
513  state = LANGLE;
514  break;
515 
516  case '\n':
517  if (stte.tag_level[RICH_NOFILL])
518  {
519  enriched_flush(&stte, true);
520  }
521  else
522  {
523  enriched_putwc((wchar_t) ' ', &stte);
524  state = NEWLINE;
525  }
526  break;
527 
528  default:
529  enriched_putwc(wc, &stte);
530  }
531  break;
532 
533  case LANGLE:
534  if (wc == (wchar_t) '<')
535  {
536  enriched_putwc(wc, &stte);
537  state = TEXT;
538  break;
539  }
540  else
541  {
542  tag_len = 0;
543  state = TAG;
544  }
545  /* Yes, (it wasn't a <<, so this char is first in TAG) */
546  /* fallthrough */
547  case TAG:
548  if (wc == (wchar_t) '>')
549  {
550  tag[tag_len] = (wchar_t) '\0';
551  enriched_set_flags(tag, &stte);
552  state = TEXT;
553  }
554  else if (tag_len < 1024) /* ignore overly long tags */
555  tag[tag_len++] = wc;
556  else
557  state = BOGUS_TAG;
558  break;
559 
560  case BOGUS_TAG:
561  if (wc == (wchar_t) '>')
562  state = TEXT;
563  break;
564 
565  case NEWLINE:
566  if (wc == (wchar_t) '\n')
567  enriched_flush(&stte, true);
568  else
569  {
570  ungetwc(wc, s->fp_in);
571  bytes++;
572  state = TEXT;
573  }
574  break;
575 
576  case ST_EOF:
577  enriched_putwc((wchar_t) '\0', &stte);
578  enriched_flush(&stte, true);
579  state = DONE;
580  break;
581 
582  case DONE:
583  /* not reached */
584  break;
585  }
586  }
587 
588  state_putc(s, '\n'); /* add a final newline */
589 
590  FREE(&(stte.buffer));
591  FREE(&(stte.line));
592  FREE(&(stte.param));
593 
594  return 0;
595 }
#define MUTT_DISPLAY
Output is displayed to the user.
Definition: state.h:32
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
State of enriched-text parser.
Definition: enriched.c:98
#define state_puts(STATE, STR)
Definition: state.h:55
char * prefix
String to add to the beginning of each output line.
Definition: state.h:48
Text will not be reformatted.
Definition: enriched.c:55
size_t mutt_str_strlen(const char *a)
Calculate the length of a string, safely.
Definition: string.c:692
size_t line_max
Definition: enriched.c:106
FILE * fp_in
File to read from.
Definition: state.h:46
StateFlags flags
Flags, e.g. MUTT_DISPLAY.
Definition: state.h:49
wchar_t * param
Definition: enriched.c:102
static void enriched_putwc(wchar_t c, struct EnrichedState *stte)
Write one wide character to the state.
Definition: enriched.c:272
int wraplen
Width to wrap lines to (when flags & MUTT_DISPLAY)
Definition: state.h:50
int wrap_margin
Definition: enriched.c:113
int tag_level[RICH_MAX]
Definition: enriched.c:112
LOFF_T length
length (in bytes) of attachment
Definition: body.h:45
static void enriched_flush(struct EnrichedState *stte, bool wrap)
Write enriched text to the State.
Definition: enriched.c:236
size_t indent_len
Definition: enriched.c:107
size_t param_len
Definition: enriched.c:111
#define FREE(x)
Definition: memory.h:40
wchar_t * line
Definition: enriched.c:101
#define state_putc(STATE, STR)
Definition: state.h:56
size_t param_used
Definition: enriched.c:110
struct State * s
Definition: enriched.c:114
static void enriched_set_flags(const wchar_t *tag, struct EnrichedState *stte)
Set flags on the enriched text state.
Definition: enriched.c:371
wchar_t * buffer
Definition: enriched.c:100
+ Here is the call graph for this function:
+ Here is the caller graph for this function: