NeoMutt  2019-11-11
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 = ((s->flags & MUTT_DISPLAY) ?
481  (s->wraplen - 4) :
482  ((s->wraplen - 4) < 72) ? (s->wraplen - 4) : 72);
483  stte.line_max = stte.wrap_margin * 4;
484  stte.line = mutt_mem_calloc((stte.line_max + 1), sizeof(wchar_t));
485  stte.param = mutt_mem_calloc(256, sizeof(wchar_t));
486 
487  stte.param_len = 256;
488  stte.param_used = 0;
489 
490  if (s->prefix)
491  {
492  state_puts(s, s->prefix);
493  stte.indent_len += mutt_str_strlen(s->prefix);
494  }
495 
496  while (state != DONE)
497  {
498  if (state != ST_EOF)
499  {
500  if (!bytes || ((wc = fgetwc(s->fp_in)) == WEOF))
501  state = ST_EOF;
502  else
503  bytes--;
504  }
505 
506  switch (state)
507  {
508  case TEXT:
509  switch (wc)
510  {
511  case '<':
512  state = LANGLE;
513  break;
514 
515  case '\n':
516  if (stte.tag_level[RICH_NOFILL])
517  {
518  enriched_flush(&stte, true);
519  }
520  else
521  {
522  enriched_putwc((wchar_t) ' ', &stte);
523  state = NEWLINE;
524  }
525  break;
526 
527  default:
528  enriched_putwc(wc, &stte);
529  }
530  break;
531 
532  case LANGLE:
533  if (wc == (wchar_t) '<')
534  {
535  enriched_putwc(wc, &stte);
536  state = TEXT;
537  break;
538  }
539  else
540  {
541  tag_len = 0;
542  state = TAG;
543  }
544  /* Yes, (it wasn't a <<, so this char is first in TAG) */
545  /* fallthrough */
546  case TAG:
547  if (wc == (wchar_t) '>')
548  {
549  tag[tag_len] = (wchar_t) '\0';
550  enriched_set_flags(tag, &stte);
551  state = TEXT;
552  }
553  else if (tag_len < 1024) /* ignore overly long tags */
554  tag[tag_len++] = wc;
555  else
556  state = BOGUS_TAG;
557  break;
558 
559  case BOGUS_TAG:
560  if (wc == (wchar_t) '>')
561  state = TEXT;
562  break;
563 
564  case NEWLINE:
565  if (wc == (wchar_t) '\n')
566  enriched_flush(&stte, true);
567  else
568  {
569  ungetwc(wc, s->fp_in);
570  bytes++;
571  state = TEXT;
572  }
573  break;
574 
575  case ST_EOF:
576  enriched_putwc((wchar_t) '\0', &stte);
577  enriched_flush(&stte, true);
578  state = DONE;
579  break;
580 
581  case DONE:
582  /* not reached */
583  break;
584  }
585  }
586 
587  state_putc(s, '\n'); /* add a final newline */
588 
589  FREE(&(stte.buffer));
590  FREE(&(stte.line));
591  FREE(&(stte.param));
592 
593  return 0;
594 }
#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:666
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: