view f-demime/ptext_out.c @ 8:a92d0d59b669 default tip

f-demime: indicate X-backslash-escapes encoding in output
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 06 May 2023 17:00:23 +0000
parents 7e0d08176f32
children
line wrap: on
line source

/*
 * This module implements transformations that are specific to text/plain.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "defs.h"

extern FILE *tempfile;
extern int ptext_has_backslash, ptext_has_linesplit;
extern unsigned mp_nest_level;
extern char mp_boundaries[MAX_MP_NESTING][MAX_MP_BOUNDARY+1];

void
ptext_mark_transform(base_xform)
	char *base_xform;
{
	printf("X-Fdemime-Transform: %s", base_xform);
	if (ptext_has_backslash)
		fputs(", double-backslash", stdout);
	if (ptext_has_linesplit)
		fputs(", line-split", stdout);
	putchar('\n');
	puts("Content-Transfer-Encoding: X-backslash-escapes");
}

static int
boundary_hit(line)
	char *line;
{
	unsigned lev, bndlen;
	char *bnd;

	if (line[0] != '-' || line[1] != '-')
		return(0);
	for (lev = 0; lev < mp_nest_level; lev++) {
		bnd = mp_boundaries[lev];
		bndlen = strlen(bnd);
		if (strncmp(line+2, bnd, bndlen))
			continue;
		return(1);
	}
	return(0);
}

void
ptext_emit_output()
{
	char line[LINE_BUF_SIZE];

	rewind(tempfile);
	while (fgets(line, sizeof line, tempfile)) {
		if (!strncmp(line, "From ", 5) || boundary_hit(line) ||
		    !strcmp(line, ".\n")) {
			putchar('\\');
			putchar('&');
		}
		fputs(line, stdout);
	}
	fclose(tempfile);
}