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
|
/*
* Copyright (c) 1986 by The Governing Council of the University of Toronto.
* Authored by Rayan Zachariassen for University of Toronto Computing Services.
*/
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#ifndef TRUE
#define TRUE 1
#endif
/*
* Copy the message on stdin to the fp output.
*
* This routine should:
* - fold message header lines according to rfc822, by prepending a tab
* to each continuation line and breaking it at the right place.
* - fold message body lines, prepending a '+ ' to each continuation line.
* - ensure message terminates with a newline.
* - escape '.'s in the first column of body lines by prepending a '.'
*/
copydata(fp)
FILE *fp;
{
register char *cp, *s;
register int n;
char buf[BUFSIZ], linebuf[BUFSIZ];
s = linebuf;
while ((n = fread(buf, 1, sizeof buf, stdin)) > 0) {
for (cp = buf; n-- > 0; ++cp) {
if (*cp == '\n' || s - linebuf >= BUFSIZ - 5) {
*s = '\0';
doline(linebuf, fp);
s = linebuf;
continue;
} else if (*cp != '\t' && (*cp < ' ' || *cp > '\176')) {
if (*cp & 0200) {
*s++ = 'M';
*s++ = '-';
*cp &= ~0200;
}
if (*cp != '\t' && (*cp < ' ' || *cp > '\176')){
*s++ = '~';
*cp = (*cp + 0100)&~0200;
}
}
*s++ = *cp;
}
}
if (s > linebuf) {
*s = '\0';
doline(linebuf, fp);
}
}
doline(buf, fp)
char buf[];
FILE *fp;
{
register char *cp, *s;
register int col;
int maxwidth;
char *ocp, *cutoff, *semicolon, *nonalnum;
static int inheader = 1;
/* find end of line, not including trailing blanks */
for (cp = buf; *cp != NULL; cp++)
continue;
while (*--cp == ' ')
continue;
*++cp = '\0';
/* a blank line separates header from body */
if (inheader && cp == buf)
inheader = 0;
maxwidth = 80;
ocp = buf;
if (inheader) {
do {
col = 1;
semicolon = nonalnum = NULL;
for (s = ocp; s < cp && col <= maxwidth; s++) {
if (*s == ';')
semicolon = s;
else if (isascii(*s) && !isalnum(*s))
nonalnum = s;
if (*s == '\t')
col = ((col + 7)>>3)<<3;
col++;
}
if (s >= cp) {
fputs(ocp, fp);
fputs("\n", fp);
return;
}
if (semicolon > ocp)
cutoff = ++semicolon;
else if (nonalnum)
cutoff = ++nonalnum;
else
cutoff = (ocp + maxwidth > cp) ? cp :
ocp + maxwidth;
fwrite(ocp, 1, cutoff - ocp, fp);
ocp = cutoff;
while (*ocp == ' ')
ocp++;
fputs("\n\t", fp);
maxwidth = 72; /* 80 - tab */
} while (TRUE);
} else {
/* escape dot in first column by adding another one */
if (*ocp == '.') {
fputs(".", fp);
--maxwidth;
}
/* handle line continuation for lines >80 columns */
do {
cutoff = NULL;
col = 1;
for (s = ocp; s<cp && col <= maxwidth; s++) {
if (isascii(*s) && !isalnum(*s))
cutoff = s;
if (*s == '\t')
col = ((col + 7)>>3)<<3;
col++;
}
/* line done? */
if (s == cp) {
fputs(ocp, fp);
fputs("\n", fp);
return;
}
if (cutoff < ocp)
cutoff = (ocp + maxwidth > cp) ? cp :
ocp + maxwidth;
else
cutoff++;
fwrite(ocp, 1, cutoff - ocp, fp);
ocp = cutoff;
/* '+ ' indicates line continuation */
fputs("\n+ ", fp);
maxwidth = 78; /* 80 - '+ ' */
} while (TRUE);
}
}
|