xref: /onnv-gate/usr/src/cmd/fmt/fmt.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #include <stdio.h>
33*0Sstevel@tonic-gate #include <stdlib.h>
34*0Sstevel@tonic-gate #include <ctype.h>
35*0Sstevel@tonic-gate #include <wctype.h>
36*0Sstevel@tonic-gate #include <widec.h>
37*0Sstevel@tonic-gate #include <dlfcn.h>
38*0Sstevel@tonic-gate #include <locale.h>
39*0Sstevel@tonic-gate #include <sys/param.h>
40*0Sstevel@tonic-gate #include <string.h>
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /*
43*0Sstevel@tonic-gate  * fmt -- format the concatenation of input files or standard input
44*0Sstevel@tonic-gate  * onto standard output.  Designed for use with Mail ~|
45*0Sstevel@tonic-gate  *
46*0Sstevel@tonic-gate  * Syntax: fmt [ -width | -w width ] [ -cs ] [ name ... ]
47*0Sstevel@tonic-gate  * Author: Kurt Shoens (UCB) 12/7/78
48*0Sstevel@tonic-gate  */
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate #define	NOSTR	((wchar_t *) 0)	/* Null string pointer for lint */
51*0Sstevel@tonic-gate #define	MAXLINES	100	/* maximum mail header lines to verify */
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate wchar_t	outbuf[BUFSIZ];			/* Sandbagged output line image */
54*0Sstevel@tonic-gate wchar_t	*outp;				/* Pointer in above */
55*0Sstevel@tonic-gate int	filler;				/* Filler amount in outbuf */
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate int	pfx;			/* Current leading blank count */
58*0Sstevel@tonic-gate int	width = 72;		/* Width that we will not exceed */
59*0Sstevel@tonic-gate int	nojoin = 0;		/* split lines only, don't join short ones */
60*0Sstevel@tonic-gate int	errs = 0;		/* Current number of errors */
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate enum crown_type	{c_none, c_reset, c_head, c_lead, c_fixup, c_body};
63*0Sstevel@tonic-gate enum crown_type	crown_state;	/* Crown margin state */
64*0Sstevel@tonic-gate int	crown_head;		/* The header offset */
65*0Sstevel@tonic-gate int	crown_body;		/* The body offset */
66*0Sstevel@tonic-gate 	/* currently-known initial strings found in mail headers */
67*0Sstevel@tonic-gate wchar_t	*headnames[] = {
68*0Sstevel@tonic-gate 	L"Apparently-To", L"Bcc", L"bcc", L"Cc", L"cc", L"Confirmed-By",
69*0Sstevel@tonic-gate 	L"Content", L"content-length", L"From", L"Date", L"id",
70*0Sstevel@tonic-gate 	L"Message-I", L"MIME-Version", L"Precedence", L"Return-Path",
71*0Sstevel@tonic-gate 	L"Received", L"Reply-To", L"Status", L"Subject", L"To", L"X-IMAP",
72*0Sstevel@tonic-gate 	L"X-Lines", L"X-Sender", L"X-Sun", L"X-Status", L"X-UID",
73*0Sstevel@tonic-gate 	0};
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate enum hdr_type {
76*0Sstevel@tonic-gate 	off,		/* mail header processing is off */
77*0Sstevel@tonic-gate 	not_in_hdr,	/* not currently processing a mail header */
78*0Sstevel@tonic-gate 	in_hdr, 	/* currently filling hdrbuf with potential hdr lines */
79*0Sstevel@tonic-gate 	flush_hdr,	/* flush hdrbuf; not a header, no special processing */
80*0Sstevel@tonic-gate 	do_hdr		/* process hdrbuf as a mail header */
81*0Sstevel@tonic-gate };
82*0Sstevel@tonic-gate 				/* current state of hdrbuf */
83*0Sstevel@tonic-gate enum hdr_type	hdr_state = not_in_hdr;
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate wchar_t *hdrbuf[MAXLINES];	/* buffer to hold potential mail header lines */
86*0Sstevel@tonic-gate int 	h_lines;		/* index into lines of hdrbuf */
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate int (*(split))();
89*0Sstevel@tonic-gate extern int scrwidth(wchar_t);
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate static void fill_hdrbuf(wchar_t line[]);
92*0Sstevel@tonic-gate static void header_chk(void);
93*0Sstevel@tonic-gate static void process_hdrbuf(void);
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate /*
96*0Sstevel@tonic-gate  * Drive the whole formatter by managing input files.  Also,
97*0Sstevel@tonic-gate  * cause initialization of the output stuff and flush it out
98*0Sstevel@tonic-gate  * at the end.
99*0Sstevel@tonic-gate  */
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate main(int argc, char **argv)
102*0Sstevel@tonic-gate {
103*0Sstevel@tonic-gate 	register FILE *fi;
104*0Sstevel@tonic-gate 	char sobuf[BUFSIZ];
105*0Sstevel@tonic-gate 	register char *cp;
106*0Sstevel@tonic-gate 	int nofile;
107*0Sstevel@tonic-gate 	char *locale;
108*0Sstevel@tonic-gate 	int csplit(), msplit();
109*0Sstevel@tonic-gate 	void _wckind_init();
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	outp = NOSTR;
112*0Sstevel@tonic-gate 	setbuf(stdout, sobuf);
113*0Sstevel@tonic-gate 	setlocale(LC_ALL, "");
114*0Sstevel@tonic-gate 	locale = setlocale(LC_CTYPE, "");
115*0Sstevel@tonic-gate 	if (strcmp(locale, "C") == 0) {
116*0Sstevel@tonic-gate 		split = csplit;
117*0Sstevel@tonic-gate 	} else {
118*0Sstevel@tonic-gate 		split = msplit;
119*0Sstevel@tonic-gate 		(void) _wckind_init();
120*0Sstevel@tonic-gate 	}
121*0Sstevel@tonic-gate 	if (argc < 2) {
122*0Sstevel@tonic-gate single:
123*0Sstevel@tonic-gate 		fmt(stdin);
124*0Sstevel@tonic-gate 		oflush();
125*0Sstevel@tonic-gate 		exit(0);
126*0Sstevel@tonic-gate 	}
127*0Sstevel@tonic-gate 	nofile = 1;
128*0Sstevel@tonic-gate 	while (--argc) {
129*0Sstevel@tonic-gate 		cp = *++argv;
130*0Sstevel@tonic-gate 		if (setopt(cp))
131*0Sstevel@tonic-gate 			continue;
132*0Sstevel@tonic-gate 		nofile = 0;
133*0Sstevel@tonic-gate 		if ((fi = fopen(cp, "r")) == NULL) {
134*0Sstevel@tonic-gate 			perror(cp);
135*0Sstevel@tonic-gate 			errs++;
136*0Sstevel@tonic-gate 			continue;
137*0Sstevel@tonic-gate 		}
138*0Sstevel@tonic-gate 		fmt(fi);
139*0Sstevel@tonic-gate 		fclose(fi);
140*0Sstevel@tonic-gate 	}
141*0Sstevel@tonic-gate 	if (nofile)
142*0Sstevel@tonic-gate 		goto single;
143*0Sstevel@tonic-gate 	oflush();
144*0Sstevel@tonic-gate 	exit(errs);
145*0Sstevel@tonic-gate 	/* NOTREACHED */
146*0Sstevel@tonic-gate }
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate /*
149*0Sstevel@tonic-gate  * Read up characters from the passed input file, forming lines,
150*0Sstevel@tonic-gate  * doing ^H processing, expanding tabs, stripping trailing blanks,
151*0Sstevel@tonic-gate  * and sending each line down for analysis.
152*0Sstevel@tonic-gate  */
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate fmt(FILE *fi)
155*0Sstevel@tonic-gate {
156*0Sstevel@tonic-gate 	wchar_t linebuf[BUFSIZ], canonb[BUFSIZ];
157*0Sstevel@tonic-gate 	register wchar_t *cp, *cp2;
158*0Sstevel@tonic-gate 	register int col;
159*0Sstevel@tonic-gate 	wchar_t	c;
160*0Sstevel@tonic-gate 	char	cbuf[BUFSIZ];	/* stores wchar_t string as char string */
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 	c = getwc(fi);
163*0Sstevel@tonic-gate 	while (c != EOF) {
164*0Sstevel@tonic-gate 		/*
165*0Sstevel@tonic-gate 		 * Collect a line, doing ^H processing.
166*0Sstevel@tonic-gate 		 * Leave tabs for now.
167*0Sstevel@tonic-gate 		 */
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 		cp = linebuf;
170*0Sstevel@tonic-gate 		while (c != L'\n' && c != EOF && cp-linebuf < BUFSIZ-1) {
171*0Sstevel@tonic-gate 			if (c == L'\b') {
172*0Sstevel@tonic-gate 				if (cp > linebuf)
173*0Sstevel@tonic-gate 					cp--;
174*0Sstevel@tonic-gate 				c = getwc(fi);
175*0Sstevel@tonic-gate 				continue;
176*0Sstevel@tonic-gate 			}
177*0Sstevel@tonic-gate 			if (!(iswprint(c)) && c != L'\t') {
178*0Sstevel@tonic-gate 				c = getwc(fi);
179*0Sstevel@tonic-gate 				continue;
180*0Sstevel@tonic-gate 			}
181*0Sstevel@tonic-gate 			*cp++ = c;
182*0Sstevel@tonic-gate 			c = getwc(fi);
183*0Sstevel@tonic-gate 		}
184*0Sstevel@tonic-gate 		*cp = L'\0';
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 		/*
187*0Sstevel@tonic-gate 		 * Toss anything remaining on the input line.
188*0Sstevel@tonic-gate 		 */
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 		while (c != L'\n' && c != EOF)
191*0Sstevel@tonic-gate 			c = getwc(fi);
192*0Sstevel@tonic-gate 		/*
193*0Sstevel@tonic-gate 		 * Expand tabs on the way to canonb.
194*0Sstevel@tonic-gate 		 */
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 		col = 0;
197*0Sstevel@tonic-gate 		cp = linebuf;
198*0Sstevel@tonic-gate 		cp2 = canonb;
199*0Sstevel@tonic-gate 		while (c = *cp++) {
200*0Sstevel@tonic-gate 			if (c != L'\t') {
201*0Sstevel@tonic-gate 				col += scrwidth(c);
202*0Sstevel@tonic-gate 				if (cp2-canonb < BUFSIZ-1)
203*0Sstevel@tonic-gate 					*cp2++ = c;
204*0Sstevel@tonic-gate 				continue;
205*0Sstevel@tonic-gate 			}
206*0Sstevel@tonic-gate 			do {
207*0Sstevel@tonic-gate 				if (cp2-canonb < BUFSIZ-1)
208*0Sstevel@tonic-gate 					*cp2++ = L' ';
209*0Sstevel@tonic-gate 				col++;
210*0Sstevel@tonic-gate 			} while ((col & 07) != 0);
211*0Sstevel@tonic-gate 		}
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 		/*
214*0Sstevel@tonic-gate 		 * Swipe trailing blanks from the line.
215*0Sstevel@tonic-gate 		 */
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 		for (cp2--; cp2 >= canonb && *cp2 == L' '; cp2--);
218*0Sstevel@tonic-gate 		*++cp2 = '\0';
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 			/* special processing to look for mail header lines */
221*0Sstevel@tonic-gate 		switch (hdr_state) {
222*0Sstevel@tonic-gate 		case off:
223*0Sstevel@tonic-gate 			prefix(canonb);
224*0Sstevel@tonic-gate 		case not_in_hdr:
225*0Sstevel@tonic-gate 			/* look for an initial mail header line */
226*0Sstevel@tonic-gate 			/* skip initial blanks */
227*0Sstevel@tonic-gate 			for (cp = canonb; *cp == L' '; cp++);
228*0Sstevel@tonic-gate 			/*
229*0Sstevel@tonic-gate 			 * Need to convert string from wchar_t to char,
230*0Sstevel@tonic-gate 			 * since this is what ishead() expects.  Since we
231*0Sstevel@tonic-gate 			 * only want to make sure cp points to a "From" line
232*0Sstevel@tonic-gate 			 * of the email, we don't have to alloc
233*0Sstevel@tonic-gate 			 * BUFSIZ * MB_LEN_MAX to cbuf.
234*0Sstevel@tonic-gate 			 */
235*0Sstevel@tonic-gate 			wcstombs(cbuf, cp, (BUFSIZ - 1));
236*0Sstevel@tonic-gate 			if (ishead(cbuf)) {
237*0Sstevel@tonic-gate 				hdr_state = in_hdr;
238*0Sstevel@tonic-gate 				fill_hdrbuf(canonb);
239*0Sstevel@tonic-gate 			} else {
240*0Sstevel@tonic-gate 				/* no mail header line; process normally */
241*0Sstevel@tonic-gate 				prefix(canonb);
242*0Sstevel@tonic-gate 			}
243*0Sstevel@tonic-gate 			break;
244*0Sstevel@tonic-gate 		case in_hdr:
245*0Sstevel@tonic-gate 			/* already saw 1st mail header line; look for more */
246*0Sstevel@tonic-gate 			if (canonb[0] == L'\0') {
247*0Sstevel@tonic-gate 				/*
248*0Sstevel@tonic-gate 				 * blank line means end of mail header;
249*0Sstevel@tonic-gate 				 * verify current mail header buffer
250*0Sstevel@tonic-gate 				 * then process it accordingly
251*0Sstevel@tonic-gate 				 */
252*0Sstevel@tonic-gate 				header_chk();
253*0Sstevel@tonic-gate 				process_hdrbuf();
254*0Sstevel@tonic-gate 				/* now process the current blank line */
255*0Sstevel@tonic-gate 				prefix(canonb);
256*0Sstevel@tonic-gate 			} else
257*0Sstevel@tonic-gate 				/*
258*0Sstevel@tonic-gate 				 * not a blank line--save this line as
259*0Sstevel@tonic-gate 				 * a potential mail header line
260*0Sstevel@tonic-gate 				 */
261*0Sstevel@tonic-gate 				fill_hdrbuf(canonb);
262*0Sstevel@tonic-gate 			break;
263*0Sstevel@tonic-gate 		}
264*0Sstevel@tonic-gate 		if (c != EOF)
265*0Sstevel@tonic-gate 			c = getwc(fi);
266*0Sstevel@tonic-gate 	}
267*0Sstevel@tonic-gate 	/*
268*0Sstevel@tonic-gate 	 * end of this file--make sure we process the stuff in
269*0Sstevel@tonic-gate 	 * hdrbuf before we're finished
270*0Sstevel@tonic-gate 	 */
271*0Sstevel@tonic-gate 	if (hdr_state == in_hdr) {
272*0Sstevel@tonic-gate 		header_chk();
273*0Sstevel@tonic-gate 		process_hdrbuf();
274*0Sstevel@tonic-gate 	}
275*0Sstevel@tonic-gate }
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate /*
278*0Sstevel@tonic-gate  * Take a line devoid of tabs and other garbage and determine its
279*0Sstevel@tonic-gate  * blank prefix.  If the indent changes, call for a linebreak.
280*0Sstevel@tonic-gate  * If the input line is blank, echo the blank line on the output.
281*0Sstevel@tonic-gate  * Finally, if the line minus the prefix is a mail header, try to keep
282*0Sstevel@tonic-gate  * it on a line by itself.
283*0Sstevel@tonic-gate  */
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate prefix(wchar_t line[])
286*0Sstevel@tonic-gate {
287*0Sstevel@tonic-gate 	register wchar_t *cp;
288*0Sstevel@tonic-gate 	register int np;
289*0Sstevel@tonic-gate 	register int i;
290*0Sstevel@tonic-gate 	int nosplit = 0;	/* flag set if line should not be split */
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 	if (line[0] == L'\0') {
293*0Sstevel@tonic-gate 		oflush();
294*0Sstevel@tonic-gate 		putchar('\n');
295*0Sstevel@tonic-gate 		if (crown_state != c_none)
296*0Sstevel@tonic-gate 			crown_state = c_reset;
297*0Sstevel@tonic-gate 		return;
298*0Sstevel@tonic-gate 	}
299*0Sstevel@tonic-gate 	for (cp = line; *cp == L' '; cp++);
300*0Sstevel@tonic-gate 	np = cp - line;
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 	/*
303*0Sstevel@tonic-gate 	 * The following horrible expression attempts to avoid linebreaks
304*0Sstevel@tonic-gate 	 * when the indent changes due to a paragraph.
305*0Sstevel@tonic-gate 	 */
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate 	if (crown_state == c_none && np != pfx && (np > pfx || abs(pfx-np) > 8))
308*0Sstevel@tonic-gate 		oflush();
309*0Sstevel@tonic-gate 	/*
310*0Sstevel@tonic-gate 	 * if this is a mail header line, don't split it; flush previous
311*0Sstevel@tonic-gate 	 * line, if any, so we don't join this line to it
312*0Sstevel@tonic-gate 	 */
313*0Sstevel@tonic-gate 	if (hdr_state == do_hdr) {
314*0Sstevel@tonic-gate 		nosplit = 1;
315*0Sstevel@tonic-gate 		oflush();
316*0Sstevel@tonic-gate 	}
317*0Sstevel@tonic-gate 	/* flush previous line so we don't join this one to it */
318*0Sstevel@tonic-gate 	if (nojoin)
319*0Sstevel@tonic-gate 		oflush();
320*0Sstevel@tonic-gate 	/* nroff-type lines starting with '.' are not split nor joined */
321*0Sstevel@tonic-gate 	if (!nosplit && (nosplit = (*cp == L'.')))
322*0Sstevel@tonic-gate 		oflush();
323*0Sstevel@tonic-gate 	pfx = np;
324*0Sstevel@tonic-gate 	switch (crown_state) {
325*0Sstevel@tonic-gate 	case c_reset:
326*0Sstevel@tonic-gate 		crown_head = pfx;
327*0Sstevel@tonic-gate 		crown_state = c_head;
328*0Sstevel@tonic-gate 		break;
329*0Sstevel@tonic-gate 	case c_lead:
330*0Sstevel@tonic-gate 		crown_body = pfx;
331*0Sstevel@tonic-gate 		crown_state = c_body;
332*0Sstevel@tonic-gate 		break;
333*0Sstevel@tonic-gate 	case c_fixup:
334*0Sstevel@tonic-gate 		crown_body = pfx;
335*0Sstevel@tonic-gate 		crown_state = c_body;
336*0Sstevel@tonic-gate 		if (outp) {
337*0Sstevel@tonic-gate 			wchar_t s[BUFSIZ];
338*0Sstevel@tonic-gate 
339*0Sstevel@tonic-gate 			*outp = L'\0';
340*0Sstevel@tonic-gate 			wscpy(s, &outbuf[crown_head]);
341*0Sstevel@tonic-gate 			outp = NOSTR;
342*0Sstevel@tonic-gate 			split(s);
343*0Sstevel@tonic-gate 		}
344*0Sstevel@tonic-gate 		break;
345*0Sstevel@tonic-gate 	}
346*0Sstevel@tonic-gate 	if (nosplit) {
347*0Sstevel@tonic-gate 		/* put whole input line onto outbuf and print it out */
348*0Sstevel@tonic-gate 		pack(cp);
349*0Sstevel@tonic-gate 		oflush();
350*0Sstevel@tonic-gate 	} else
351*0Sstevel@tonic-gate 		/*
352*0Sstevel@tonic-gate 		 * split puts current line onto outbuf, but splits it
353*0Sstevel@tonic-gate 		 * at word boundaries, if it exceeds desired length
354*0Sstevel@tonic-gate 		 */
355*0Sstevel@tonic-gate 		split(cp);
356*0Sstevel@tonic-gate 	if (nojoin)
357*0Sstevel@tonic-gate 		/*
358*0Sstevel@tonic-gate 		 * flush current line so next lines, if any,
359*0Sstevel@tonic-gate 		 * won't join to this one
360*0Sstevel@tonic-gate 		 */
361*0Sstevel@tonic-gate 		oflush();
362*0Sstevel@tonic-gate }
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate /*
365*0Sstevel@tonic-gate  * Split up the passed line into output "words" which are
366*0Sstevel@tonic-gate  * maximal strings of non-blanks with the blank separation
367*0Sstevel@tonic-gate  * attached at the end.  Pass these words along to the output
368*0Sstevel@tonic-gate  * line packer.
369*0Sstevel@tonic-gate  */
370*0Sstevel@tonic-gate 
371*0Sstevel@tonic-gate csplit(wchar_t line[])
372*0Sstevel@tonic-gate {
373*0Sstevel@tonic-gate 	register wchar_t *cp, *cp2;
374*0Sstevel@tonic-gate 	wchar_t word[BUFSIZ];
375*0Sstevel@tonic-gate 	static const wchar_t *srchlist = (const wchar_t *) L".:!?";
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 	cp = line;
378*0Sstevel@tonic-gate 	while (*cp) {
379*0Sstevel@tonic-gate 		cp2 = word;
380*0Sstevel@tonic-gate 
381*0Sstevel@tonic-gate 		/*
382*0Sstevel@tonic-gate 		 * Collect a 'word,' allowing it to contain escaped
383*0Sstevel@tonic-gate 		 * white space.
384*0Sstevel@tonic-gate 		 */
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate 		while (*cp && !(iswspace(*cp))) {
387*0Sstevel@tonic-gate 			if (*cp == '\\' && iswspace(cp[1]))
388*0Sstevel@tonic-gate 				*cp2++ = *cp++;
389*0Sstevel@tonic-gate 			*cp2++ = *cp++;
390*0Sstevel@tonic-gate 		}
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate 		/*
393*0Sstevel@tonic-gate 		 * Guarantee a space at end of line.
394*0Sstevel@tonic-gate 		 * Two spaces after end of sentence punctuation.
395*0Sstevel@tonic-gate 		 */
396*0Sstevel@tonic-gate 
397*0Sstevel@tonic-gate 		if (*cp == L'\0') {
398*0Sstevel@tonic-gate 			*cp2++ = L' ';
399*0Sstevel@tonic-gate 			if (wschr(srchlist, cp[-1]) != NULL)
400*0Sstevel@tonic-gate 				*cp2++ = L' ';
401*0Sstevel@tonic-gate 		}
402*0Sstevel@tonic-gate 		while (iswspace(*cp))
403*0Sstevel@tonic-gate 			*cp2++ = *cp++;
404*0Sstevel@tonic-gate 		*cp2 = L'\0';
405*0Sstevel@tonic-gate 		pack(word);
406*0Sstevel@tonic-gate 	}
407*0Sstevel@tonic-gate }
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate msplit(wchar_t line[])
410*0Sstevel@tonic-gate {
411*0Sstevel@tonic-gate 	register wchar_t *cp, *cp2, prev;
412*0Sstevel@tonic-gate 	wchar_t word[BUFSIZ];
413*0Sstevel@tonic-gate 	static const wchar_t *srchlist = (const wchar_t *) L".:!?";
414*0Sstevel@tonic-gate 
415*0Sstevel@tonic-gate 	cp = line;
416*0Sstevel@tonic-gate 	while (*cp) {
417*0Sstevel@tonic-gate 		cp2 = word;
418*0Sstevel@tonic-gate 		prev = *cp;
419*0Sstevel@tonic-gate 
420*0Sstevel@tonic-gate 		/*
421*0Sstevel@tonic-gate 		 * Collect a 'word,' allowing it to contain escaped
422*0Sstevel@tonic-gate 		 * white space.
423*0Sstevel@tonic-gate 		 */
424*0Sstevel@tonic-gate 
425*0Sstevel@tonic-gate 		while (*cp) {
426*0Sstevel@tonic-gate 			if (iswspace(*cp))
427*0Sstevel@tonic-gate 				break;
428*0Sstevel@tonic-gate 			if (_wckind(*cp) != _wckind(prev))
429*0Sstevel@tonic-gate 				if (wcsetno(*cp) != 0 || wcsetno(prev) != 0)
430*0Sstevel@tonic-gate 					break;
431*0Sstevel@tonic-gate 			if (*cp == '\\' && iswspace(cp[1]))
432*0Sstevel@tonic-gate 				*cp2++ = *cp++;
433*0Sstevel@tonic-gate 			prev = *cp;
434*0Sstevel@tonic-gate 			*cp2++ = *cp++;
435*0Sstevel@tonic-gate 		}
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate 		/*
438*0Sstevel@tonic-gate 		 * Guarantee a space at end of line.
439*0Sstevel@tonic-gate 		 * Two spaces after end of sentence punctuation.
440*0Sstevel@tonic-gate 		 */
441*0Sstevel@tonic-gate 
442*0Sstevel@tonic-gate 		if (*cp == L'\0') {
443*0Sstevel@tonic-gate 			*cp2++ = L' ';
444*0Sstevel@tonic-gate 			if (wschr(srchlist, cp[-1]) != NULL)
445*0Sstevel@tonic-gate 				*cp2++ = L' ';
446*0Sstevel@tonic-gate 		}
447*0Sstevel@tonic-gate 		while (iswspace(*cp))
448*0Sstevel@tonic-gate 			*cp2++ = *cp++;
449*0Sstevel@tonic-gate 		*cp2 = L'\0';
450*0Sstevel@tonic-gate 		pack(word);
451*0Sstevel@tonic-gate 	}
452*0Sstevel@tonic-gate }
453*0Sstevel@tonic-gate 
454*0Sstevel@tonic-gate /*
455*0Sstevel@tonic-gate  * Output section.
456*0Sstevel@tonic-gate  * Build up line images from the words passed in.  Prefix
457*0Sstevel@tonic-gate  * each line with correct number of blanks.  The buffer "outbuf"
458*0Sstevel@tonic-gate  * contains the current partial line image, including prefixed blanks.
459*0Sstevel@tonic-gate  * "outp" points to the next available space therein.  When outp is NOSTR,
460*0Sstevel@tonic-gate  * there ain't nothing in there yet.  At the bottom of this whole mess,
461*0Sstevel@tonic-gate  * leading tabs are reinserted.
462*0Sstevel@tonic-gate  */
463*0Sstevel@tonic-gate 
464*0Sstevel@tonic-gate /*
465*0Sstevel@tonic-gate  * Pack a word onto the output line.  If this is the beginning of
466*0Sstevel@tonic-gate  * the line, push on the appropriately-sized string of blanks first.
467*0Sstevel@tonic-gate  * If the word won't fit on the current line, flush and begin a new
468*0Sstevel@tonic-gate  * line.  If the word is too long to fit all by itself on a line,
469*0Sstevel@tonic-gate  * just give it its own and hope for the best.
470*0Sstevel@tonic-gate  */
471*0Sstevel@tonic-gate 
472*0Sstevel@tonic-gate pack(wchar_t word[])
473*0Sstevel@tonic-gate {
474*0Sstevel@tonic-gate 	register wchar_t *cp;
475*0Sstevel@tonic-gate 	register int s, t;
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate 	if (outp == NOSTR)
478*0Sstevel@tonic-gate 		leadin();
479*0Sstevel@tonic-gate 	t = wscol(word);
480*0Sstevel@tonic-gate 	*outp = L'\0';
481*0Sstevel@tonic-gate 	s = wscol(outbuf);
482*0Sstevel@tonic-gate 	if (t+s <= width) {
483*0Sstevel@tonic-gate 		for (cp = word; *cp; *outp++ = *cp++);
484*0Sstevel@tonic-gate 		return;
485*0Sstevel@tonic-gate 	}
486*0Sstevel@tonic-gate 	if (s > filler) {
487*0Sstevel@tonic-gate 		oflush();
488*0Sstevel@tonic-gate 		leadin();
489*0Sstevel@tonic-gate 	}
490*0Sstevel@tonic-gate 	for (cp = word; *cp; *outp++ = *cp++);
491*0Sstevel@tonic-gate }
492*0Sstevel@tonic-gate 
493*0Sstevel@tonic-gate /*
494*0Sstevel@tonic-gate  * If there is anything on the current output line, send it on
495*0Sstevel@tonic-gate  * its way.  Set outp to NOSTR to indicate the absence of the current
496*0Sstevel@tonic-gate  * line prefix.
497*0Sstevel@tonic-gate  */
498*0Sstevel@tonic-gate 
499*0Sstevel@tonic-gate oflush(void)
500*0Sstevel@tonic-gate {
501*0Sstevel@tonic-gate 	if (outp == NOSTR)
502*0Sstevel@tonic-gate 		return;
503*0Sstevel@tonic-gate 	*outp = L'\0';
504*0Sstevel@tonic-gate 	tabulate(outbuf);
505*0Sstevel@tonic-gate 	outp = NOSTR;
506*0Sstevel@tonic-gate }
507*0Sstevel@tonic-gate 
508*0Sstevel@tonic-gate /*
509*0Sstevel@tonic-gate  * Take the passed line buffer, insert leading tabs where possible, and
510*0Sstevel@tonic-gate  * output on standard output (finally).
511*0Sstevel@tonic-gate  */
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate tabulate(wchar_t line[])
514*0Sstevel@tonic-gate {
515*0Sstevel@tonic-gate 	register wchar_t *cp, *cp2;
516*0Sstevel@tonic-gate 	register int b, t;
517*0Sstevel@tonic-gate 
518*0Sstevel@tonic-gate 
519*0Sstevel@tonic-gate 	/* Toss trailing blanks in the output line */
520*0Sstevel@tonic-gate 	cp = line + wslen(line) - 1;
521*0Sstevel@tonic-gate 	while (cp >= line && *cp == L' ')
522*0Sstevel@tonic-gate 		cp--;
523*0Sstevel@tonic-gate 	*++cp = L'\0';
524*0Sstevel@tonic-gate 	/* Count the leading blank space and tabulate */
525*0Sstevel@tonic-gate 	for (cp = line; *cp == L' '; cp++);
526*0Sstevel@tonic-gate 	b = cp - line;
527*0Sstevel@tonic-gate 	t = b >> 3;
528*0Sstevel@tonic-gate 	b &= 07;
529*0Sstevel@tonic-gate 	if (t > 0)
530*0Sstevel@tonic-gate 		do
531*0Sstevel@tonic-gate 			putc('\t', stdout);
532*0Sstevel@tonic-gate 		while (--t);
533*0Sstevel@tonic-gate 	if (b > 0)
534*0Sstevel@tonic-gate 		do
535*0Sstevel@tonic-gate 			putc(' ', stdout);
536*0Sstevel@tonic-gate 		while (--b);
537*0Sstevel@tonic-gate 	while (*cp)
538*0Sstevel@tonic-gate 		putwc(*cp++, stdout);
539*0Sstevel@tonic-gate 	putc('\n', stdout);
540*0Sstevel@tonic-gate }
541*0Sstevel@tonic-gate 
542*0Sstevel@tonic-gate /*
543*0Sstevel@tonic-gate  * Initialize the output line with the appropriate number of
544*0Sstevel@tonic-gate  * leading blanks.
545*0Sstevel@tonic-gate  */
546*0Sstevel@tonic-gate 
547*0Sstevel@tonic-gate leadin()
548*0Sstevel@tonic-gate {
549*0Sstevel@tonic-gate 	register int b;
550*0Sstevel@tonic-gate 	register wchar_t *cp;
551*0Sstevel@tonic-gate 	register int l;
552*0Sstevel@tonic-gate 
553*0Sstevel@tonic-gate 	switch (crown_state) {
554*0Sstevel@tonic-gate 	case c_head:
555*0Sstevel@tonic-gate 		l = crown_head;
556*0Sstevel@tonic-gate 		crown_state = c_lead;
557*0Sstevel@tonic-gate 		break;
558*0Sstevel@tonic-gate 
559*0Sstevel@tonic-gate 	case c_lead:
560*0Sstevel@tonic-gate 	case c_fixup:
561*0Sstevel@tonic-gate 		l = crown_head;
562*0Sstevel@tonic-gate 		crown_state = c_fixup;
563*0Sstevel@tonic-gate 		break;
564*0Sstevel@tonic-gate 
565*0Sstevel@tonic-gate 	case c_body:
566*0Sstevel@tonic-gate 		l = crown_body;
567*0Sstevel@tonic-gate 		break;
568*0Sstevel@tonic-gate 
569*0Sstevel@tonic-gate 	default:
570*0Sstevel@tonic-gate 		l = pfx;
571*0Sstevel@tonic-gate 		break;
572*0Sstevel@tonic-gate 	}
573*0Sstevel@tonic-gate 	filler = l;
574*0Sstevel@tonic-gate 	for (b = 0, cp = outbuf; b < l; b++)
575*0Sstevel@tonic-gate 		*cp++ = L' ';
576*0Sstevel@tonic-gate 	outp = cp;
577*0Sstevel@tonic-gate }
578*0Sstevel@tonic-gate 
579*0Sstevel@tonic-gate /*
580*0Sstevel@tonic-gate  * Is s1 a prefix of s2??
581*0Sstevel@tonic-gate  */
582*0Sstevel@tonic-gate 
583*0Sstevel@tonic-gate ispref(wchar_t *s1, wchar_t *s2)
584*0Sstevel@tonic-gate {
585*0Sstevel@tonic-gate 
586*0Sstevel@tonic-gate 	while (*s1 != L'\0' && *s2 != L'\0')
587*0Sstevel@tonic-gate 		if (*s1++ != *s2++)
588*0Sstevel@tonic-gate 			return (0);
589*0Sstevel@tonic-gate 	return (1);
590*0Sstevel@tonic-gate }
591*0Sstevel@tonic-gate 
592*0Sstevel@tonic-gate /*
593*0Sstevel@tonic-gate  * Set an input option
594*0Sstevel@tonic-gate  */
595*0Sstevel@tonic-gate 
596*0Sstevel@tonic-gate setopt(cp)
597*0Sstevel@tonic-gate 	register char *cp;
598*0Sstevel@tonic-gate {
599*0Sstevel@tonic-gate 	static int ws = 0;
600*0Sstevel@tonic-gate 
601*0Sstevel@tonic-gate 	if (*cp == '-') {
602*0Sstevel@tonic-gate 		if (cp[1] == 'c' && cp[2] == '\0') {
603*0Sstevel@tonic-gate 			crown_state = c_reset;
604*0Sstevel@tonic-gate 			return (1);
605*0Sstevel@tonic-gate 		}
606*0Sstevel@tonic-gate 		if (cp[1] == 's' && cp[2] == '\0') {
607*0Sstevel@tonic-gate 			nojoin = 1;
608*0Sstevel@tonic-gate 			return (1);
609*0Sstevel@tonic-gate 		}
610*0Sstevel@tonic-gate 		if (cp[1] == 'w' && cp[2] == '\0') {
611*0Sstevel@tonic-gate 			ws++;
612*0Sstevel@tonic-gate 			return (1);
613*0Sstevel@tonic-gate 		}
614*0Sstevel@tonic-gate 		width = atoi(cp+1);
615*0Sstevel@tonic-gate 	} else if (ws) {
616*0Sstevel@tonic-gate 		width = atoi(cp);
617*0Sstevel@tonic-gate 		ws = 0;
618*0Sstevel@tonic-gate 	} else
619*0Sstevel@tonic-gate 		return (0);
620*0Sstevel@tonic-gate 	if (width <= 0 || width >= BUFSIZ-2) {
621*0Sstevel@tonic-gate 		fprintf(stderr, "fmt:  bad width: %d\n", width);
622*0Sstevel@tonic-gate 		exit(1);
623*0Sstevel@tonic-gate 	}
624*0Sstevel@tonic-gate 	return (1);
625*0Sstevel@tonic-gate }
626*0Sstevel@tonic-gate 
627*0Sstevel@tonic-gate 
628*0Sstevel@tonic-gate #define	LIB_WDRESOLVE	"/usr/lib/locale/%s/LC_CTYPE/wdresolve.so"
629*0Sstevel@tonic-gate #define	WCHKIND		"_wdchkind_"
630*0Sstevel@tonic-gate 
631*0Sstevel@tonic-gate static int	_wckind_c_locale();
632*0Sstevel@tonic-gate 
633*0Sstevel@tonic-gate static int	(*__wckind)() = _wckind_c_locale;
634*0Sstevel@tonic-gate static void	*dlhandle = NULL;
635*0Sstevel@tonic-gate 
636*0Sstevel@tonic-gate 
637*0Sstevel@tonic-gate void
638*0Sstevel@tonic-gate _wckind_init()
639*0Sstevel@tonic-gate {
640*0Sstevel@tonic-gate 	char	*locale;
641*0Sstevel@tonic-gate 	char	path[MAXPATHLEN + 1];
642*0Sstevel@tonic-gate 
643*0Sstevel@tonic-gate 
644*0Sstevel@tonic-gate 	if (dlhandle != NULL) {
645*0Sstevel@tonic-gate 		(void) dlclose(dlhandle);
646*0Sstevel@tonic-gate 		dlhandle = NULL;
647*0Sstevel@tonic-gate 	}
648*0Sstevel@tonic-gate 
649*0Sstevel@tonic-gate 	locale = setlocale(LC_CTYPE, NULL);
650*0Sstevel@tonic-gate 	if (strcmp(locale, "C") == 0)
651*0Sstevel@tonic-gate 		goto c_locale;
652*0Sstevel@tonic-gate 
653*0Sstevel@tonic-gate 	(void) sprintf(path, LIB_WDRESOLVE, locale);
654*0Sstevel@tonic-gate 
655*0Sstevel@tonic-gate 	if ((dlhandle = dlopen(path, RTLD_LAZY)) != NULL) {
656*0Sstevel@tonic-gate 		__wckind = (int (*)(int))dlsym(dlhandle, WCHKIND);
657*0Sstevel@tonic-gate 		if (__wckind != NULL)
658*0Sstevel@tonic-gate 			return;
659*0Sstevel@tonic-gate 		(void) dlclose(dlhandle);
660*0Sstevel@tonic-gate 		dlhandle = NULL;
661*0Sstevel@tonic-gate 	}
662*0Sstevel@tonic-gate 
663*0Sstevel@tonic-gate c_locale:
664*0Sstevel@tonic-gate 	__wckind = _wckind_c_locale;
665*0Sstevel@tonic-gate }
666*0Sstevel@tonic-gate 
667*0Sstevel@tonic-gate 
668*0Sstevel@tonic-gate int
669*0Sstevel@tonic-gate _wckind(wc)
670*0Sstevel@tonic-gate wchar_t	wc;
671*0Sstevel@tonic-gate {
672*0Sstevel@tonic-gate 	return (*__wckind) (wc);
673*0Sstevel@tonic-gate }
674*0Sstevel@tonic-gate 
675*0Sstevel@tonic-gate 
676*0Sstevel@tonic-gate static int
677*0Sstevel@tonic-gate _wckind_c_locale(wc)
678*0Sstevel@tonic-gate wchar_t	wc;
679*0Sstevel@tonic-gate {
680*0Sstevel@tonic-gate 	int	ret;
681*0Sstevel@tonic-gate 
682*0Sstevel@tonic-gate 	/*
683*0Sstevel@tonic-gate 	 * DEPEND_ON_ANSIC: L notion for the character is new in
684*0Sstevel@tonic-gate 	 * ANSI-C, k&r compiler won't work.
685*0Sstevel@tonic-gate 	 */
686*0Sstevel@tonic-gate 	if (iswascii(wc))
687*0Sstevel@tonic-gate 		ret = (iswalnum(wc) || wc == L'_') ? 0 : 1;
688*0Sstevel@tonic-gate 	else
689*0Sstevel@tonic-gate 		ret = wcsetno(wc) + 1;
690*0Sstevel@tonic-gate 
691*0Sstevel@tonic-gate 	return (ret);
692*0Sstevel@tonic-gate }
693*0Sstevel@tonic-gate 
694*0Sstevel@tonic-gate /*
695*0Sstevel@tonic-gate  * header_chk -
696*0Sstevel@tonic-gate  * Called when done looking for a set mail header lines.
697*0Sstevel@tonic-gate  * Either a blank line was seen, or EOF was reached.
698*0Sstevel@tonic-gate  *
699*0Sstevel@tonic-gate  * Verifies if current hdrbuf of potential mail header lines
700*0Sstevel@tonic-gate  * is really a mail header.  A mail header must be at least 2
701*0Sstevel@tonic-gate  * lines and more than half of them must start with one of the
702*0Sstevel@tonic-gate  * known mail header strings in headnames.
703*0Sstevel@tonic-gate  *
704*0Sstevel@tonic-gate  * header_chk sets hdr_state to do_hdr if hdrbuf contained a valid
705*0Sstevel@tonic-gate  * mail header.  Otherwise, it sets hdr_state to flush_hdr.
706*0Sstevel@tonic-gate  *
707*0Sstevel@tonic-gate  * h_lines = hdrbuf index for next line to be saved;
708*0Sstevel@tonic-gate  *	     also indicates current # of lines in potential header
709*0Sstevel@tonic-gate  */
710*0Sstevel@tonic-gate static void
711*0Sstevel@tonic-gate header_chk(void)
712*0Sstevel@tonic-gate {
713*0Sstevel@tonic-gate 	wchar_t  *cp; 		/* ptr to current char of line */
714*0Sstevel@tonic-gate 	wchar_t **hp; 		/* ptr to current char of a valid */
715*0Sstevel@tonic-gate 				/* mail header string */
716*0Sstevel@tonic-gate 	int	  l;		/* index */
717*0Sstevel@tonic-gate 				/*
718*0Sstevel@tonic-gate 				 * number of lines in hdrbuf that look
719*0Sstevel@tonic-gate 				 * like mail header lines (start with
720*0Sstevel@tonic-gate 				 * a known mail header prefix)
721*0Sstevel@tonic-gate 				 */
722*0Sstevel@tonic-gate 	int	 hdrcount = 0;
723*0Sstevel@tonic-gate 		/* header must have at least 2 lines (h_lines > 1) */
724*0Sstevel@tonic-gate 		if (h_lines < 2) {
725*0Sstevel@tonic-gate 			hdr_state = flush_hdr;
726*0Sstevel@tonic-gate 			return;
727*0Sstevel@tonic-gate 		}
728*0Sstevel@tonic-gate 		/*
729*0Sstevel@tonic-gate 		 * go through each line in hdrbuf and see how many
730*0Sstevel@tonic-gate 		 * look like mail header lines
731*0Sstevel@tonic-gate 		 */
732*0Sstevel@tonic-gate 		for (l = 0; l < h_lines; l++) {
733*0Sstevel@tonic-gate 			/* skip initial blanks */
734*0Sstevel@tonic-gate 			for (cp = hdrbuf[l]; *cp == L' '; cp++);
735*0Sstevel@tonic-gate 			for (hp = &headnames[0]; *hp != (wchar_t *) 0; hp++)
736*0Sstevel@tonic-gate 				if (ispref(*hp, cp)) {
737*0Sstevel@tonic-gate 					hdrcount++;
738*0Sstevel@tonic-gate 					break;
739*0Sstevel@tonic-gate 				}
740*0Sstevel@tonic-gate 		}
741*0Sstevel@tonic-gate 		/*
742*0Sstevel@tonic-gate 		 * if over half match, we'll assume this is a header;
743*0Sstevel@tonic-gate 		 * set hdr_state to indicate whether to treat
744*0Sstevel@tonic-gate 		 * these lines as mail header (do_hdr) or not (flush_hdr)
745*0Sstevel@tonic-gate 		 */
746*0Sstevel@tonic-gate 		if (hdrcount > h_lines / 2)
747*0Sstevel@tonic-gate 			hdr_state = do_hdr;
748*0Sstevel@tonic-gate 		else
749*0Sstevel@tonic-gate 			hdr_state = flush_hdr;
750*0Sstevel@tonic-gate }
751*0Sstevel@tonic-gate 
752*0Sstevel@tonic-gate /*
753*0Sstevel@tonic-gate  * fill_hdrbuf -
754*0Sstevel@tonic-gate  * Save given input line into next element of hdrbuf,
755*0Sstevel@tonic-gate  * as a potential mail header line, to be processed later
756*0Sstevel@tonic-gate  * once we decide whether or not the contents of hdrbuf is
757*0Sstevel@tonic-gate  * really a mail header, via header_chk().
758*0Sstevel@tonic-gate  *
759*0Sstevel@tonic-gate  * Does not allow hdrbuf to exceed MAXLINES lines.
760*0Sstevel@tonic-gate  * Dynamically allocates space for each line.  If we are unable
761*0Sstevel@tonic-gate  * to allocate space for the current string, stop special mail
762*0Sstevel@tonic-gate  * header preservation at this point and continue formatting
763*0Sstevel@tonic-gate  * without it.
764*0Sstevel@tonic-gate  */
765*0Sstevel@tonic-gate static void
766*0Sstevel@tonic-gate fill_hdrbuf(wchar_t line[])
767*0Sstevel@tonic-gate {
768*0Sstevel@tonic-gate 	wchar_t *cp;	/* pointer to characters in input line */
769*0Sstevel@tonic-gate 	int	 i;	/* index into characters a hdrbuf line */
770*0Sstevel@tonic-gate 
771*0Sstevel@tonic-gate 	if (h_lines >= MAXLINES) {
772*0Sstevel@tonic-gate 		/*
773*0Sstevel@tonic-gate 		 * if we run over MAXLINES potential mail header
774*0Sstevel@tonic-gate 		 * lines, stop checking--this is most likely NOT a
775*0Sstevel@tonic-gate 		 * mail header; flush out the hdrbuf, then process
776*0Sstevel@tonic-gate 		 * the current 'line' normally.
777*0Sstevel@tonic-gate 		 */
778*0Sstevel@tonic-gate 		hdr_state = flush_hdr;
779*0Sstevel@tonic-gate 		process_hdrbuf();
780*0Sstevel@tonic-gate 		prefix(line);
781*0Sstevel@tonic-gate 		return;
782*0Sstevel@tonic-gate 	}
783*0Sstevel@tonic-gate 	hdrbuf[h_lines] = (wchar_t *)malloc(sizeof (wchar_t) *
784*0Sstevel@tonic-gate 	    (wslen(line) + 1));
785*0Sstevel@tonic-gate 	if (hdrbuf[h_lines] == NULL) {
786*0Sstevel@tonic-gate 		perror("malloc");
787*0Sstevel@tonic-gate 		fprintf(stderr, "fmt: unable to do mail header preservation\n");
788*0Sstevel@tonic-gate 		errs++;
789*0Sstevel@tonic-gate 		/*
790*0Sstevel@tonic-gate 		 * Can't process mail header; flush current contents
791*0Sstevel@tonic-gate 		 * of mail header and continue with no more mail
792*0Sstevel@tonic-gate 		 * header processing
793*0Sstevel@tonic-gate 		 */
794*0Sstevel@tonic-gate 		if (h_lines == 0)
795*0Sstevel@tonic-gate 			/* hdrbuf is empty; process this line normally */
796*0Sstevel@tonic-gate 			prefix(line);
797*0Sstevel@tonic-gate 		else {
798*0Sstevel@tonic-gate 			hdr_state = flush_hdr;
799*0Sstevel@tonic-gate 			for (i = 0; i < h_lines; i++) {
800*0Sstevel@tonic-gate 				prefix(hdrbuf[i]);
801*0Sstevel@tonic-gate 				free(hdrbuf[i]);
802*0Sstevel@tonic-gate 			}
803*0Sstevel@tonic-gate 			h_lines = 0;
804*0Sstevel@tonic-gate 		}
805*0Sstevel@tonic-gate 		hdr_state = off;
806*0Sstevel@tonic-gate 		return;
807*0Sstevel@tonic-gate 	}
808*0Sstevel@tonic-gate 	/* save this line as a potential mail header line */
809*0Sstevel@tonic-gate 	for (i = 0, cp = line; (hdrbuf[h_lines][i] = *cp) != L'\0'; i++, cp++);
810*0Sstevel@tonic-gate 	h_lines++;
811*0Sstevel@tonic-gate }
812*0Sstevel@tonic-gate 
813*0Sstevel@tonic-gate /*
814*0Sstevel@tonic-gate  * process_hdrbuf -
815*0Sstevel@tonic-gate  * Outputs the lines currently stored in hdrbuf, according
816*0Sstevel@tonic-gate  * to the current hdr_state value, assumed to be either do_hdr
817*0Sstevel@tonic-gate  * or flush_hdr.
818*0Sstevel@tonic-gate  * This should be called after doing a header_chk() to verify
819*0Sstevel@tonic-gate  * the hdrbuf and set the hdr_state flag.
820*0Sstevel@tonic-gate  */
821*0Sstevel@tonic-gate static void
822*0Sstevel@tonic-gate process_hdrbuf(void)
823*0Sstevel@tonic-gate {
824*0Sstevel@tonic-gate int i;
825*0Sstevel@tonic-gate 
826*0Sstevel@tonic-gate 	for (i = 0; i < h_lines; i++) {
827*0Sstevel@tonic-gate 		prefix(hdrbuf[i]);
828*0Sstevel@tonic-gate 		free(hdrbuf[i]);
829*0Sstevel@tonic-gate 	}
830*0Sstevel@tonic-gate 	hdr_state = not_in_hdr;
831*0Sstevel@tonic-gate 	h_lines = 0;
832*0Sstevel@tonic-gate }
833