#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <wctype.h>
#include <widec.h>
#include <dlfcn.h>
#include <locale.h>
#include <sys/param.h>
#include <string.h>
#define NOSTR ((wchar_t *)0)
#define MAXLINES 100
wchar_t outbuf[BUFSIZ];
wchar_t *outp;
int filler;
char sobuf[BUFSIZ];
int pfx;
int width = 72;
int nojoin = 0;
int errs = 0;
enum crown_type {c_none, c_reset, c_head, c_lead, c_fixup, c_body};
enum crown_type crown_state;
int crown_head;
int crown_body;
wchar_t *headnames[] = {
L"Apparently-To", L"Bcc", L"bcc", L"Cc", L"cc", L"Confirmed-By",
L"Content", L"content-length", L"From", L"Date", L"id",
L"Message-I", L"MIME-Version", L"Precedence", L"Return-Path",
L"Received", L"Reply-To", L"Status", L"Subject", L"To", L"X-IMAP",
L"X-Lines", L"X-Sender", L"X-Sun", L"X-Status", L"X-UID",
0};
enum hdr_type {
off,
not_in_hdr,
in_hdr,
flush_hdr,
do_hdr
};
enum hdr_type hdr_state = not_in_hdr;
wchar_t *hdrbuf[MAXLINES];
int h_lines;
void (*(split))(wchar_t []);
extern int scrwidth(wchar_t);
extern boolean_t is_headline(const char *);
static void fill_hdrbuf(wchar_t []);
static void header_chk(void);
static void process_hdrbuf(void);
static void leadin(void);
static void tabulate(wchar_t []);
static void oflush(void);
static void pack(wchar_t []);
static void msplit(wchar_t []);
static void csplit(wchar_t []);
static void _wckind_init(void);
static void prefix(wchar_t []);
static void fmt(FILE *);
static int setopt(char *);
int _wckind(wchar_t);
int
main(int argc, char **argv)
{
FILE *fi;
char *cp;
int nofile;
char *locale;
outp = NOSTR;
setbuf(stdout, sobuf);
setlocale(LC_ALL, "");
locale = setlocale(LC_CTYPE, NULL);
if (strcmp(locale, "C") == 0) {
split = csplit;
} else {
split = msplit;
_wckind_init();
}
if (argc < 2) {
single:
fmt(stdin);
oflush();
exit(0);
}
nofile = 1;
while (--argc) {
cp = *++argv;
if (setopt(cp))
continue;
nofile = 0;
if ((fi = fopen(cp, "r")) == NULL) {
perror(cp);
errs++;
continue;
}
fmt(fi);
fclose(fi);
}
if (nofile)
goto single;
oflush();
fclose(stdout);
return (errs);
}
static void
fmt(FILE *fi)
{
wchar_t linebuf[BUFSIZ], canonb[BUFSIZ];
wchar_t *cp, *cp2;
int col;
wchar_t c;
char cbuf[BUFSIZ];
c = getwc(fi);
while (c != EOF) {
cp = linebuf;
while (c != L'\n' && c != EOF && cp-linebuf < BUFSIZ-1) {
if (c == L'\b') {
if (cp > linebuf)
cp--;
c = getwc(fi);
continue;
}
if (!(iswprint(c)) && c != L'\t') {
c = getwc(fi);
continue;
}
*cp++ = c;
c = getwc(fi);
}
*cp = L'\0';
while (c != L'\n' && c != EOF)
c = getwc(fi);
col = 0;
cp = linebuf;
cp2 = canonb;
while (c = *cp++) {
if (c != L'\t') {
col += scrwidth(c);
if (cp2-canonb < BUFSIZ-1)
*cp2++ = c;
continue;
}
do {
if (cp2-canonb < BUFSIZ-1)
*cp2++ = L' ';
col++;
} while ((col & 07) != 0);
}
for (cp2--; cp2 >= canonb && *cp2 == L' '; cp2--) {
}
*++cp2 = '\0';
switch (hdr_state) {
case off:
prefix(canonb);
case not_in_hdr:
for (cp = canonb; *cp == L' '; cp++) {
}
wcstombs(cbuf, cp, (BUFSIZ - 1));
if (is_headline(cbuf) == B_TRUE) {
hdr_state = in_hdr;
fill_hdrbuf(canonb);
} else {
prefix(canonb);
}
break;
case in_hdr:
if (canonb[0] == L'\0') {
header_chk();
process_hdrbuf();
prefix(canonb);
} else
fill_hdrbuf(canonb);
break;
}
if (c != EOF)
c = getwc(fi);
}
if (hdr_state == in_hdr) {
header_chk();
process_hdrbuf();
}
}
static void
prefix(wchar_t line[])
{
wchar_t *cp;
int np;
int nosplit = 0;
if (line[0] == L'\0') {
oflush();
putchar('\n');
if (crown_state != c_none)
crown_state = c_reset;
return;
}
for (cp = line; *cp == L' '; cp++) {
}
np = cp - line;
if (crown_state == c_none && np != pfx && (np > pfx || abs(pfx-np) > 8))
oflush();
if (hdr_state == do_hdr) {
nosplit = 1;
oflush();
}
if (nojoin)
oflush();
if (!nosplit && (nosplit = (*cp == L'.')))
oflush();
pfx = np;
switch (crown_state) {
case c_reset:
crown_head = pfx;
crown_state = c_head;
break;
case c_lead:
crown_body = pfx;
crown_state = c_body;
break;
case c_fixup:
crown_body = pfx;
crown_state = c_body;
if (outp) {
wchar_t s[BUFSIZ];
*outp = L'\0';
wscpy(s, &outbuf[crown_head]);
outp = NOSTR;
split(s);
}
break;
}
if (nosplit) {
pack(cp);
oflush();
} else
split(cp);
if (nojoin)
oflush();
}
static void
csplit(wchar_t line[])
{
wchar_t *cp, *cp2;
wchar_t word[BUFSIZ];
static const wchar_t *srchlist = (const wchar_t *) L".:!?";
cp = line;
while (*cp) {
cp2 = word;
while (*cp && !(iswspace(*cp))) {
if (*cp == '\\' && iswspace(cp[1]))
*cp2++ = *cp++;
*cp2++ = *cp++;
}
if (*cp == L'\0') {
*cp2++ = L' ';
if (wschr(srchlist, cp[-1]) != NULL)
*cp2++ = L' ';
}
while (iswspace(*cp))
*cp2++ = *cp++;
*cp2 = L'\0';
pack(word);
}
}
static void
msplit(wchar_t line[])
{
wchar_t *cp, *cp2, prev;
wchar_t word[BUFSIZ];
static const wchar_t *srchlist = (const wchar_t *) L".:!?";
cp = line;
while (*cp) {
cp2 = word;
prev = *cp;
while (*cp) {
if (iswspace(*cp))
break;
if (_wckind(*cp) != _wckind(prev))
if (wcsetno(*cp) != 0 || wcsetno(prev) != 0)
break;
if (*cp == '\\' && iswspace(cp[1]))
*cp2++ = *cp++;
prev = *cp;
*cp2++ = *cp++;
}
if (*cp == L'\0') {
*cp2++ = L' ';
if (wschr(srchlist, cp[-1]) != NULL)
*cp2++ = L' ';
}
while (iswspace(*cp))
*cp2++ = *cp++;
*cp2 = L'\0';
pack(word);
}
}
static void
pack(wchar_t word[])
{
wchar_t *cp;
int s, t;
if (outp == NOSTR)
leadin();
t = wscol(word);
*outp = L'\0';
s = wscol(outbuf);
if (t+s <= width) {
for (cp = word; *cp; *outp++ = *cp++) {
}
return;
}
if (s > filler) {
oflush();
leadin();
}
for (cp = word; *cp; *outp++ = *cp++) {
}
}
static void
oflush(void)
{
if (outp == NOSTR)
return;
*outp = L'\0';
tabulate(outbuf);
outp = NOSTR;
}
static void
tabulate(wchar_t line[])
{
wchar_t *cp;
int b, t;
cp = line + wslen(line) - 1;
while (cp >= line && *cp == L' ')
cp--;
*++cp = L'\0';
for (cp = line; *cp == L' '; cp++) {
}
b = cp - line;
t = b >> 3;
b &= 07;
if (t > 0)
do {
putc('\t', stdout);
} while (--t);
if (b > 0)
do {
putc(' ', stdout);
} while (--b);
while (*cp)
putwc(*cp++, stdout);
putc('\n', stdout);
}
static void
leadin(void)
{
int b;
wchar_t *cp;
int l;
switch (crown_state) {
case c_head:
l = crown_head;
crown_state = c_lead;
break;
case c_lead:
case c_fixup:
l = crown_head;
crown_state = c_fixup;
break;
case c_body:
l = crown_body;
break;
default:
l = pfx;
break;
}
filler = l;
for (b = 0, cp = outbuf; b < l; b++)
*cp++ = L' ';
outp = cp;
}
static int
ispref(wchar_t *s1, wchar_t *s2)
{
while (*s1 != L'\0' && *s2 != L'\0')
if (*s1++ != *s2++)
return (0);
return (1);
}
static int
setopt(char *cp)
{
static int ws = 0;
if (*cp == '-') {
if (cp[1] == 'c' && cp[2] == '\0') {
crown_state = c_reset;
return (1);
}
if (cp[1] == 's' && cp[2] == '\0') {
nojoin = 1;
return (1);
}
if (cp[1] == 'w' && cp[2] == '\0') {
ws++;
return (1);
}
width = atoi(cp+1);
} else if (ws) {
width = atoi(cp);
ws = 0;
} else
return (0);
if (width <= 0 || width >= BUFSIZ-2) {
fprintf(stderr, "fmt: bad width: %d\n", width);
exit(1);
}
return (1);
}
#define LIB_WDRESOLVE "/usr/lib/locale/%s/LC_CTYPE/wdresolve.so"
#define WCHKIND "_wdchkind_"
static int _wckind_c_locale(wchar_t);
static int (*__wckind)(wchar_t) = _wckind_c_locale;
static void *dlhandle = NULL;
static void
_wckind_init(void)
{
char *locale;
char path[MAXPATHLEN + 1];
if (dlhandle != NULL) {
(void) dlclose(dlhandle);
dlhandle = NULL;
}
locale = setlocale(LC_CTYPE, NULL);
if (strcmp(locale, "C") == 0)
goto c_locale;
(void) sprintf(path, LIB_WDRESOLVE, locale);
if ((dlhandle = dlopen(path, RTLD_LAZY)) != NULL) {
__wckind = (int (*)(wchar_t))dlsym(dlhandle, WCHKIND);
if (__wckind != NULL)
return;
(void) dlclose(dlhandle);
dlhandle = NULL;
}
c_locale:
__wckind = _wckind_c_locale;
}
int
_wckind(wchar_t wc)
{
return (*__wckind) (wc);
}
static int
_wckind_c_locale(wchar_t wc)
{
int ret;
if (iswascii(wc))
ret = (iswalnum(wc) || wc == L'_') ? 0 : 1;
else
ret = wcsetno(wc) + 1;
return (ret);
}
static void
header_chk(void)
{
wchar_t *cp;
wchar_t **hp;
int l;
int hdrcount = 0;
if (h_lines < 2) {
hdr_state = flush_hdr;
return;
}
for (l = 0; l < h_lines; l++) {
for (cp = hdrbuf[l]; *cp == L' '; cp++) {
}
for (hp = &headnames[0]; *hp != (wchar_t *)0; hp++)
if (ispref(*hp, cp)) {
hdrcount++;
break;
}
}
if (hdrcount > h_lines / 2)
hdr_state = do_hdr;
else
hdr_state = flush_hdr;
}
static void
fill_hdrbuf(wchar_t line[])
{
wchar_t *cp;
int i;
if (h_lines >= MAXLINES) {
hdr_state = flush_hdr;
process_hdrbuf();
prefix(line);
return;
}
hdrbuf[h_lines] = (wchar_t *)malloc(sizeof (wchar_t) *
(wslen(line) + 1));
if (hdrbuf[h_lines] == NULL) {
perror("malloc");
fprintf(stderr, "fmt: unable to do mail header preservation\n");
errs++;
if (h_lines == 0)
prefix(line);
else {
hdr_state = flush_hdr;
for (i = 0; i < h_lines; i++) {
prefix(hdrbuf[i]);
free(hdrbuf[i]);
}
h_lines = 0;
}
hdr_state = off;
return;
}
for (i = 0, cp = line; (hdrbuf[h_lines][i] = *cp) != L'\0'; i++, cp++) {
}
h_lines++;
}
static void
process_hdrbuf(void)
{
int i;
for (i = 0; i < h_lines; i++) {
prefix(hdrbuf[i]);
free(hdrbuf[i]);
}
hdr_state = not_in_hdr;
h_lines = 0;
}