xref: /titanic_53/usr/src/cmd/mailx/collect.c (revision 6c83d09f819e9de7126c8539546eca2e276df44a)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 1998 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
337c478bd9Sstevel@tonic-gate  * The Regents of the University of California
347c478bd9Sstevel@tonic-gate  * All Rights Reserved
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
377c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
387c478bd9Sstevel@tonic-gate  * contributors.
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * mailx -- a modified version of a University of California at Berkeley
457c478bd9Sstevel@tonic-gate  *	mail program
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  * Collect input from standard input, handling
487c478bd9Sstevel@tonic-gate  * ~ escapes.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #include "rcv.h"
527c478bd9Sstevel@tonic-gate #include <locale.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #ifdef SIGCONT
557c478bd9Sstevel@tonic-gate static void	collcont(int);
567c478bd9Sstevel@tonic-gate #endif
577c478bd9Sstevel@tonic-gate static void	collrub(int s);
587c478bd9Sstevel@tonic-gate static void	cpout(char *str, FILE *ofd);
597c478bd9Sstevel@tonic-gate static int	exwrite(char name[], FILE *ibuf);
607c478bd9Sstevel@tonic-gate static int	forward(char ms[], FILE *obuf, int f);
617c478bd9Sstevel@tonic-gate static void	intack(int);
627c478bd9Sstevel@tonic-gate static int	forward(char ms[], FILE *obuf, int f);
637c478bd9Sstevel@tonic-gate static FILE	*mesedit(FILE *ibuf, FILE *obuf, int c, struct header *hp);
647c478bd9Sstevel@tonic-gate static FILE	*mespipe(FILE *ibuf, FILE *obuf, char cmd[]);
657c478bd9Sstevel@tonic-gate static void	resetsigs(int resethup);
667c478bd9Sstevel@tonic-gate static int	stripnulls(register char *linebuf, register int nread);
677c478bd9Sstevel@tonic-gate static void	xhalt(void);
687c478bd9Sstevel@tonic-gate static char	**Xaddone(char **hf, char news[]);
697c478bd9Sstevel@tonic-gate static int	tabputs(const char *line, FILE *obuf);
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate  * Read a message from standard output and return a read file to it
737c478bd9Sstevel@tonic-gate  * or NULL on error.
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate  * The following hokiness with global variables is so that on
787c478bd9Sstevel@tonic-gate  * receipt of an interrupt signal, the partial message can be salted
797c478bd9Sstevel@tonic-gate  * away on dead.letter.  The output file must be available to flush,
807c478bd9Sstevel@tonic-gate  * and the input to read.  Several open files could be saved all through
817c478bd9Sstevel@tonic-gate  * mailx if stdio allowed simultaneous read/write access.
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate static void		(*savesig)(int);	/* Previous SIGINT value */
857c478bd9Sstevel@tonic-gate static void		(*savehup)(int);	/* Previous SIGHUP value */
867c478bd9Sstevel@tonic-gate #ifdef SIGCONT
877c478bd9Sstevel@tonic-gate static void		(*savecont)(int);	/* Previous SIGCONT value */
887c478bd9Sstevel@tonic-gate #endif
897c478bd9Sstevel@tonic-gate static FILE		*newi;		/* File for saving away */
907c478bd9Sstevel@tonic-gate static FILE		*newo;		/* Output side of same */
917c478bd9Sstevel@tonic-gate static int		ignintr;	/* Ignore interrups */
927c478bd9Sstevel@tonic-gate static int		hadintr;	/* Have seen one SIGINT so far */
937c478bd9Sstevel@tonic-gate static struct header	*savehp;
947c478bd9Sstevel@tonic-gate static jmp_buf		coljmp;		/* To get back to work */
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate FILE *
977c478bd9Sstevel@tonic-gate collect(struct header *hp)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	FILE *ibuf, *fbuf, *obuf;
1007c478bd9Sstevel@tonic-gate 	int escape, eof;
1017c478bd9Sstevel@tonic-gate 	long lc, cc;
1027c478bd9Sstevel@tonic-gate 	register int c, t;
1037c478bd9Sstevel@tonic-gate 	int hdrs;
1047c478bd9Sstevel@tonic-gate 	char linebuf[LINESIZE+1], *cp;
1057c478bd9Sstevel@tonic-gate 	char *iprompt;
1067c478bd9Sstevel@tonic-gate 	int inhead;
1077c478bd9Sstevel@tonic-gate 	void (*sigpipe)(int), (*sigint)(int);
1087c478bd9Sstevel@tonic-gate 	int fd = -1;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	noreset++;
1117c478bd9Sstevel@tonic-gate 	ibuf = obuf = NULL;
1127c478bd9Sstevel@tonic-gate 	newi = newo = NULL;
1137c478bd9Sstevel@tonic-gate 	if ((fd = open(tempMail, O_RDWR|O_CREAT|O_EXCL, 0600)) < 0 ||
1147c478bd9Sstevel@tonic-gate 	(obuf = fdopen(fd, "w")) == NULL) {
1157c478bd9Sstevel@tonic-gate 		perror(tempMail);
1167c478bd9Sstevel@tonic-gate 		goto err;
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 	newo = obuf;
1197c478bd9Sstevel@tonic-gate 	if ((ibuf = fopen(tempMail, "r")) == NULL) {
1207c478bd9Sstevel@tonic-gate 		perror(tempMail);
1217c478bd9Sstevel@tonic-gate 		newo = NULL;
1227c478bd9Sstevel@tonic-gate 		fclose(obuf);
1237c478bd9Sstevel@tonic-gate 		goto err;
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 	newi = ibuf;
1267c478bd9Sstevel@tonic-gate 	removefile(tempMail);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	ignintr = (int)value("ignore");
1297c478bd9Sstevel@tonic-gate 	hadintr = 1;
1307c478bd9Sstevel@tonic-gate 	inhead = 1;
1317c478bd9Sstevel@tonic-gate 	savehp = hp;
1327c478bd9Sstevel@tonic-gate # ifdef VMUNIX
1337c478bd9Sstevel@tonic-gate 	if ((savesig = sigset(SIGINT, SIG_IGN)) != SIG_IGN)
1347c478bd9Sstevel@tonic-gate 		sigset(SIGINT, ignintr ? intack : collrub), sigblock(sigmask(SIGINT));
1357c478bd9Sstevel@tonic-gate 	if ((savehup = sigset(SIGHUP, SIG_IGN)) != SIG_IGN)
1367c478bd9Sstevel@tonic-gate 		sigset(SIGHUP, collrub), sigblock(sigmask(SIGHUP));
137*6c83d09fSrobbin # else /* VMUNIX */
1387c478bd9Sstevel@tonic-gate # ifdef OLD_BSD_SIGS
1397c478bd9Sstevel@tonic-gate 	if ((savesig = sigset(SIGINT, SIG_IGN)) != SIG_IGN)
1407c478bd9Sstevel@tonic-gate 		sigset(SIGINT, ignintr ? intack : collrub);
1417c478bd9Sstevel@tonic-gate 	if ((savehup = sigset(SIGHUP, SIG_IGN)) != SIG_IGN)
1427c478bd9Sstevel@tonic-gate 		sigset(SIGHUP, collrub);
1437c478bd9Sstevel@tonic-gate # else
1447c478bd9Sstevel@tonic-gate 	if ((savesig = sigset(SIGINT, SIG_IGN)) != SIG_IGN) {
1457c478bd9Sstevel@tonic-gate 		sigset_t mask;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 		sigemptyset(&mask);
1487c478bd9Sstevel@tonic-gate 		sigaddset(&mask, SIGINT);
1497c478bd9Sstevel@tonic-gate 		sigset(SIGINT, ignintr ? intack : collrub);
1507c478bd9Sstevel@tonic-gate 		sigprocmask(SIG_BLOCK, &mask, NULL);
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 	if ((savehup = sigset(SIGHUP, SIG_IGN)) != SIG_IGN) {
1537c478bd9Sstevel@tonic-gate 		sigset_t mask;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 		sigemptyset(&mask);
1567c478bd9Sstevel@tonic-gate 		sigaddset(&mask, SIGHUP);
1577c478bd9Sstevel@tonic-gate 		sigset(SIGHUP, collrub);
1587c478bd9Sstevel@tonic-gate 		sigprocmask(SIG_BLOCK, &mask, NULL);
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate # endif
161*6c83d09fSrobbin # endif /* VMUNIX */
1627c478bd9Sstevel@tonic-gate #ifdef SIGCONT
1637c478bd9Sstevel@tonic-gate 	savecont = sigset(SIGCONT, collcont);
1647c478bd9Sstevel@tonic-gate #endif
1657c478bd9Sstevel@tonic-gate 	/*
1667c478bd9Sstevel@tonic-gate 	 * If we are going to prompt for subject/cc/bcc,
1677c478bd9Sstevel@tonic-gate 	 * refrain from printing a newline after
1687c478bd9Sstevel@tonic-gate 	 * the headers (since some people mind).
1697c478bd9Sstevel@tonic-gate 	 */
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	if (hp->h_subject == NOSTR) {
1727c478bd9Sstevel@tonic-gate 		hp->h_subject = sflag;
1737c478bd9Sstevel@tonic-gate 		sflag = NOSTR;
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 	if (hp->h_cc == NOSTR) {
1767c478bd9Sstevel@tonic-gate 		hp->h_cc = cflag;
1777c478bd9Sstevel@tonic-gate 		cflag = NOSTR;
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate 	if (hp->h_bcc == NOSTR) {
1807c478bd9Sstevel@tonic-gate 		hp->h_bcc = bflag;
1817c478bd9Sstevel@tonic-gate 		bflag = NOSTR;
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate 	t = GMASK;
1847c478bd9Sstevel@tonic-gate 	hdrs = 0;
1857c478bd9Sstevel@tonic-gate 	if (intty && !tflag) {
1867c478bd9Sstevel@tonic-gate 		if (hp->h_to == NOSTR)
1877c478bd9Sstevel@tonic-gate 			hdrs |= GTO;
1887c478bd9Sstevel@tonic-gate 		if (hp->h_subject == NOSTR && value("asksub"))
1897c478bd9Sstevel@tonic-gate 			hdrs |= GSUBJECT;
1907c478bd9Sstevel@tonic-gate 		if (hp->h_cc == NOSTR && value("askcc"))
1917c478bd9Sstevel@tonic-gate 			hdrs |= GCC;
1927c478bd9Sstevel@tonic-gate 		if (hp->h_bcc == NOSTR && value("askbcc"))
1937c478bd9Sstevel@tonic-gate 			hdrs |= GBCC;
1947c478bd9Sstevel@tonic-gate 		if (hdrs)
1957c478bd9Sstevel@tonic-gate 			t &= ~GNL;
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate 	if (hp->h_seq != 0) {
1987c478bd9Sstevel@tonic-gate 		puthead(hp, stdout, t, 0);
1997c478bd9Sstevel@tonic-gate 		fflush(stdout);
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 	if (setjmp(coljmp))
2027c478bd9Sstevel@tonic-gate 		goto err;
2037c478bd9Sstevel@tonic-gate 	escape = SENDESC;
2047c478bd9Sstevel@tonic-gate 	if ((cp = value("escape")) != NOSTR)
2057c478bd9Sstevel@tonic-gate 		escape = *cp;
2067c478bd9Sstevel@tonic-gate 	eof = 0;
2077c478bd9Sstevel@tonic-gate 	if ((cp = value("MAILX_HEAD")) != NOSTR) {
2087c478bd9Sstevel@tonic-gate 	      cpout( cp, obuf);
2097c478bd9Sstevel@tonic-gate 	      if (isatty(fileno(stdin)))
2107c478bd9Sstevel@tonic-gate 		    cpout( cp, stdout);
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 	iprompt = value("iprompt");
2137c478bd9Sstevel@tonic-gate 	fflush(obuf);
2147c478bd9Sstevel@tonic-gate 	hadintr = 0;
2157c478bd9Sstevel@tonic-gate 	for (;;) {
2167c478bd9Sstevel@tonic-gate 		int nread, hasnulls;
2177c478bd9Sstevel@tonic-gate # ifdef VMUNIX
2187c478bd9Sstevel@tonic-gate 		int omask = sigblock(0) &~ (sigmask(SIGINT)|sigmask(SIGHUP));
2197c478bd9Sstevel@tonic-gate # else
2207c478bd9Sstevel@tonic-gate # ifndef OLD_BSD_SIGS
2217c478bd9Sstevel@tonic-gate 		sigset_t omask;
2227c478bd9Sstevel@tonic-gate 		sigprocmask(0, NULL, &omask);
2237c478bd9Sstevel@tonic-gate 		sigdelset(&omask, SIGINT);
2247c478bd9Sstevel@tonic-gate 		sigdelset(&omask, SIGHUP);
2257c478bd9Sstevel@tonic-gate # endif
2267c478bd9Sstevel@tonic-gate # endif
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 		setjmp(coljmp);
2297c478bd9Sstevel@tonic-gate # ifdef VMUNIX
2307c478bd9Sstevel@tonic-gate 		sigsetmask(omask);
231*6c83d09fSrobbin # else /* VMUNIX */
2327c478bd9Sstevel@tonic-gate # ifdef OLD_BSD_SIGS
2337c478bd9Sstevel@tonic-gate 		sigrelse(SIGINT);
2347c478bd9Sstevel@tonic-gate 		sigrelse(SIGHUP);
2357c478bd9Sstevel@tonic-gate # else
2367c478bd9Sstevel@tonic-gate 		sigprocmask(SIG_SETMASK, &omask, NULL);
2377c478bd9Sstevel@tonic-gate # endif
238*6c83d09fSrobbin # endif /* VMUNIX */
2397c478bd9Sstevel@tonic-gate 		if (intty && !tflag && outtty && iprompt)
2407c478bd9Sstevel@tonic-gate 			fputs(iprompt, stdout);
2417c478bd9Sstevel@tonic-gate 		flush();
2427c478bd9Sstevel@tonic-gate 		if (hdrs) {
2437c478bd9Sstevel@tonic-gate 			grabh(hp, hdrs, 1);
2447c478bd9Sstevel@tonic-gate 			hdrs = 0;
2457c478bd9Sstevel@tonic-gate 			continue;
2467c478bd9Sstevel@tonic-gate 		}
2477c478bd9Sstevel@tonic-gate 		if ((nread = getline(linebuf,LINESIZE,stdin,&hasnulls)) == NULL) {
2487c478bd9Sstevel@tonic-gate 			if (intty && value("ignoreeof") != NOSTR) {
2497c478bd9Sstevel@tonic-gate 				if (++eof > 35)
2507c478bd9Sstevel@tonic-gate 					break;
2517c478bd9Sstevel@tonic-gate 				printf(gettext(
2527c478bd9Sstevel@tonic-gate 				    "Use \".\" to terminate letter\n"));
2537c478bd9Sstevel@tonic-gate 				continue;
2547c478bd9Sstevel@tonic-gate 			}
2557c478bd9Sstevel@tonic-gate 			break;
2567c478bd9Sstevel@tonic-gate 		}
2577c478bd9Sstevel@tonic-gate 		eof = 0;
2587c478bd9Sstevel@tonic-gate 		hadintr = 0;
2597c478bd9Sstevel@tonic-gate 		if (intty && equal(".\n", linebuf) &&
2607c478bd9Sstevel@tonic-gate 		    (value("dot") != NOSTR || value("ignoreeof") != NOSTR))
2617c478bd9Sstevel@tonic-gate 			break;
2627c478bd9Sstevel@tonic-gate 		/*
2637c478bd9Sstevel@tonic-gate 		 * If -t, scan text for headers.
2647c478bd9Sstevel@tonic-gate 		 */
2657c478bd9Sstevel@tonic-gate 		if (tflag) {
2667c478bd9Sstevel@tonic-gate 			char *cp2;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 			if (!inhead) {
2697c478bd9Sstevel@tonic-gate 			writeit:
2707c478bd9Sstevel@tonic-gate 				if (write(fileno(obuf),linebuf,nread) != nread)
2717c478bd9Sstevel@tonic-gate 					goto werr;
2727c478bd9Sstevel@tonic-gate 				continue;
2737c478bd9Sstevel@tonic-gate 			}
2747c478bd9Sstevel@tonic-gate 			if (linebuf[0] == '\n') {
2757c478bd9Sstevel@tonic-gate 				/* got blank line after header, ignore it */
2767c478bd9Sstevel@tonic-gate 				inhead = 0;
2777c478bd9Sstevel@tonic-gate 				continue;
2787c478bd9Sstevel@tonic-gate 			}
2797c478bd9Sstevel@tonic-gate 			if (!headerp(linebuf)) {
2807c478bd9Sstevel@tonic-gate 				/* got non-header line, save it */
2817c478bd9Sstevel@tonic-gate 				inhead = 0;
2827c478bd9Sstevel@tonic-gate 				goto writeit;
2837c478bd9Sstevel@tonic-gate 			}
2847c478bd9Sstevel@tonic-gate 			if (hasnulls)
2857c478bd9Sstevel@tonic-gate 				nread = stripnulls(linebuf, nread);
2867c478bd9Sstevel@tonic-gate 			for (;;) {
2877c478bd9Sstevel@tonic-gate 				char line2[LINESIZE];
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 				c = getc(stdin);
2907c478bd9Sstevel@tonic-gate 				ungetc(c, stdin);
2917c478bd9Sstevel@tonic-gate 				if (!isspace(c) || c == '\n')
2927c478bd9Sstevel@tonic-gate 					break;
2937c478bd9Sstevel@tonic-gate 				if (readline(stdin, line2) < 0)
2947c478bd9Sstevel@tonic-gate 					break;
2957c478bd9Sstevel@tonic-gate 				for (cp2 = line2; *cp2 != 0 && isspace(*cp2);
2967c478bd9Sstevel@tonic-gate 				    cp2++)
2977c478bd9Sstevel@tonic-gate 					;
2987c478bd9Sstevel@tonic-gate 				if (strlen(linebuf) + strlen(cp2) >=
2997c478bd9Sstevel@tonic-gate 				    (unsigned)LINESIZE-2)
3007c478bd9Sstevel@tonic-gate 					break;
3017c478bd9Sstevel@tonic-gate 				cp = &linebuf[strlen(linebuf)];
3027c478bd9Sstevel@tonic-gate 				while (cp > linebuf &&
3037c478bd9Sstevel@tonic-gate 				    (isspace(cp[-1]) || cp[-1] == '\\'))
3047c478bd9Sstevel@tonic-gate 					cp--;
3057c478bd9Sstevel@tonic-gate 				*cp++ = ' ';
3067c478bd9Sstevel@tonic-gate 				strcpy(cp, cp2);
3077c478bd9Sstevel@tonic-gate 			}
3087c478bd9Sstevel@tonic-gate 			if ((c = strlen(linebuf)) > 0) {
3097c478bd9Sstevel@tonic-gate 				cp = &linebuf[c-1];
3107c478bd9Sstevel@tonic-gate 				while (cp > linebuf && isspace(*cp))
3117c478bd9Sstevel@tonic-gate 					cp--;
3127c478bd9Sstevel@tonic-gate 				*++cp = 0;
3137c478bd9Sstevel@tonic-gate 			}
3147c478bd9Sstevel@tonic-gate 			if (ishfield(linebuf, "to"))
3157c478bd9Sstevel@tonic-gate 				hp->h_to = addto(hp->h_to, hcontents(linebuf));
3167c478bd9Sstevel@tonic-gate 			else if (ishfield(linebuf, "subject"))
3177c478bd9Sstevel@tonic-gate 				hp->h_subject =
3187c478bd9Sstevel@tonic-gate 				    addone(hp->h_subject, hcontents(linebuf));
3197c478bd9Sstevel@tonic-gate 			else if (ishfield(linebuf, "cc"))
3207c478bd9Sstevel@tonic-gate 				hp->h_cc = addto(hp->h_cc, hcontents(linebuf));
3217c478bd9Sstevel@tonic-gate 			else if (ishfield(linebuf, "bcc"))
3227c478bd9Sstevel@tonic-gate 				hp->h_bcc =
3237c478bd9Sstevel@tonic-gate 				    addto(hp->h_bcc, hcontents(linebuf));
3247c478bd9Sstevel@tonic-gate 			else if (ishfield(linebuf, "default-options"))
3257c478bd9Sstevel@tonic-gate 				hp->h_defopt =
3267c478bd9Sstevel@tonic-gate 				    addone(hp->h_defopt, hcontents(linebuf));
3277c478bd9Sstevel@tonic-gate 			else
3287c478bd9Sstevel@tonic-gate 				hp->h_others = Xaddone(hp->h_others, linebuf);
3297c478bd9Sstevel@tonic-gate 			hp->h_seq++;
3307c478bd9Sstevel@tonic-gate 			continue;
3317c478bd9Sstevel@tonic-gate 		}
3327c478bd9Sstevel@tonic-gate 		if ((linebuf[0] != escape) || (rflag != NOSTR) ||
3337c478bd9Sstevel@tonic-gate 		    (!intty && !(int)value("escapeok"))) {
3347c478bd9Sstevel@tonic-gate 			if (write(fileno(obuf),linebuf,nread) != nread)
3357c478bd9Sstevel@tonic-gate 				goto werr;
3367c478bd9Sstevel@tonic-gate 			continue;
3377c478bd9Sstevel@tonic-gate 		}
3387c478bd9Sstevel@tonic-gate 		/*
3397c478bd9Sstevel@tonic-gate 		 * On double escape, just send the single one.
3407c478bd9Sstevel@tonic-gate 		 */
3417c478bd9Sstevel@tonic-gate 		if ((nread > 1) && (linebuf[1] == escape)) {
3427c478bd9Sstevel@tonic-gate 			if (write(fileno(obuf),linebuf+1,nread-1) != (nread-1))
3437c478bd9Sstevel@tonic-gate 				goto werr;
3447c478bd9Sstevel@tonic-gate 			continue;
3457c478bd9Sstevel@tonic-gate 		}
3467c478bd9Sstevel@tonic-gate 		if (hasnulls)
3477c478bd9Sstevel@tonic-gate 			nread = stripnulls(linebuf, nread);
3487c478bd9Sstevel@tonic-gate 		c = linebuf[1];
3497c478bd9Sstevel@tonic-gate 		linebuf[nread - 1] = '\0';
3507c478bd9Sstevel@tonic-gate 		switch (c) {
3517c478bd9Sstevel@tonic-gate 		default:
3527c478bd9Sstevel@tonic-gate 			/*
3537c478bd9Sstevel@tonic-gate 			 * Otherwise, it's an error.
3547c478bd9Sstevel@tonic-gate 			 */
3557c478bd9Sstevel@tonic-gate 			printf(gettext("Unknown tilde escape.\n"));
3567c478bd9Sstevel@tonic-gate 			break;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 		case 'a':
3597c478bd9Sstevel@tonic-gate 		case 'A':
3607c478bd9Sstevel@tonic-gate 			/*
3617c478bd9Sstevel@tonic-gate 			 * autograph; sign the letter.
3627c478bd9Sstevel@tonic-gate 			 */
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 			if (cp = value(c=='a' ? "sign":"Sign")) {
3657c478bd9Sstevel@tonic-gate 			      if (*cp)
3667c478bd9Sstevel@tonic-gate 			          cpout( cp, obuf);
3677c478bd9Sstevel@tonic-gate 			      if (isatty(fileno(stdin))) {
3687c478bd9Sstevel@tonic-gate 			          if (*cp)
3697c478bd9Sstevel@tonic-gate 				      cpout( cp, stdout);
3707c478bd9Sstevel@tonic-gate 			    }
3717c478bd9Sstevel@tonic-gate 			}
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 			break;
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 		case 'i':
3767c478bd9Sstevel@tonic-gate 			/*
3777c478bd9Sstevel@tonic-gate 			 * insert string
3787c478bd9Sstevel@tonic-gate 			 */
3797c478bd9Sstevel@tonic-gate 			for (cp = &linebuf[2]; any(*cp, " \t"); cp++)
3807c478bd9Sstevel@tonic-gate 				;
3817c478bd9Sstevel@tonic-gate 			if (*cp)
3827c478bd9Sstevel@tonic-gate 				cp = value(cp);
3837c478bd9Sstevel@tonic-gate 			if (cp != NOSTR) {
3847c478bd9Sstevel@tonic-gate 				if (*cp)
3857c478bd9Sstevel@tonic-gate 				    cpout(cp, obuf);
3867c478bd9Sstevel@tonic-gate 				if (isatty(fileno(stdout))) {
3877c478bd9Sstevel@tonic-gate 					if (*cp)
3887c478bd9Sstevel@tonic-gate 					    cpout(cp, stdout);
3897c478bd9Sstevel@tonic-gate 				}
3907c478bd9Sstevel@tonic-gate 			}
3917c478bd9Sstevel@tonic-gate 			break;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 		case '!':
3947c478bd9Sstevel@tonic-gate 			/*
3957c478bd9Sstevel@tonic-gate 			 * Shell escape, send the balance of the
3967c478bd9Sstevel@tonic-gate 			 * line to sh -c.
3977c478bd9Sstevel@tonic-gate 			 */
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 			shell(&linebuf[2]);
4007c478bd9Sstevel@tonic-gate 			break;
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 		case ':':
4037c478bd9Sstevel@tonic-gate 		case '_':
4047c478bd9Sstevel@tonic-gate 			/*
4057c478bd9Sstevel@tonic-gate 			 * Escape to command mode, but be nice!
4067c478bd9Sstevel@tonic-gate 			 */
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 			execute(&linebuf[2], 1);
4097c478bd9Sstevel@tonic-gate 			iprompt = value("iprompt");
4107c478bd9Sstevel@tonic-gate 			if (cp = value("escape"))
4117c478bd9Sstevel@tonic-gate 				escape = *cp;
4127c478bd9Sstevel@tonic-gate 			printf(gettext("(continue)\n"));
4137c478bd9Sstevel@tonic-gate 			break;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 		case '.':
4167c478bd9Sstevel@tonic-gate 			/*
4177c478bd9Sstevel@tonic-gate 			 * Simulate end of file on input.
4187c478bd9Sstevel@tonic-gate 			 */
4197c478bd9Sstevel@tonic-gate 			goto eofl;
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 		case 'q':
4227c478bd9Sstevel@tonic-gate 		case 'Q':
4237c478bd9Sstevel@tonic-gate 			/*
4247c478bd9Sstevel@tonic-gate 			 * Force a quit of sending mail.
4257c478bd9Sstevel@tonic-gate 			 * Act like an interrupt happened.
4267c478bd9Sstevel@tonic-gate 			 */
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 			hadintr++;
4297c478bd9Sstevel@tonic-gate 			collrub(SIGINT);
4307c478bd9Sstevel@tonic-gate 			exit(1);
4317c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 		case 'x':
4347c478bd9Sstevel@tonic-gate 			xhalt();
4357c478bd9Sstevel@tonic-gate 			break; 	/* not reached */
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 		case 'h':
4387c478bd9Sstevel@tonic-gate 			/*
4397c478bd9Sstevel@tonic-gate 			 * Grab a bunch of headers.
4407c478bd9Sstevel@tonic-gate 			 */
4417c478bd9Sstevel@tonic-gate 			if (!intty || !outtty) {
4427c478bd9Sstevel@tonic-gate 				printf(gettext("~h: no can do!?\n"));
4437c478bd9Sstevel@tonic-gate 				break;
4447c478bd9Sstevel@tonic-gate 			}
4457c478bd9Sstevel@tonic-gate 			grabh(hp, GMASK, (int)value("bsdcompat"));
4467c478bd9Sstevel@tonic-gate 			printf(gettext("(continue)\n"));
4477c478bd9Sstevel@tonic-gate 			break;
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 		case 't':
4507c478bd9Sstevel@tonic-gate 			/*
4517c478bd9Sstevel@tonic-gate 			 * Add to the To list.
4527c478bd9Sstevel@tonic-gate 			 */
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 			hp->h_to = addto(hp->h_to, &linebuf[2]);
4557c478bd9Sstevel@tonic-gate 			hp->h_seq++;
4567c478bd9Sstevel@tonic-gate 			break;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 		case 's':
4597c478bd9Sstevel@tonic-gate 			/*
4607c478bd9Sstevel@tonic-gate 			 * Set the Subject list.
4617c478bd9Sstevel@tonic-gate 			 */
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 			cp = &linebuf[2];
4647c478bd9Sstevel@tonic-gate 			while (any(*cp, " \t"))
4657c478bd9Sstevel@tonic-gate 				cp++;
4667c478bd9Sstevel@tonic-gate 			hp->h_subject = savestr(cp);
4677c478bd9Sstevel@tonic-gate 			hp->h_seq++;
4687c478bd9Sstevel@tonic-gate 			break;
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 		case 'c':
4717c478bd9Sstevel@tonic-gate 			/*
4727c478bd9Sstevel@tonic-gate 			 * Add to the CC list.
4737c478bd9Sstevel@tonic-gate 			 */
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 			hp->h_cc = addto(hp->h_cc, &linebuf[2]);
4767c478bd9Sstevel@tonic-gate 			hp->h_seq++;
4777c478bd9Sstevel@tonic-gate 			break;
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 		case 'b':
4807c478bd9Sstevel@tonic-gate 			/*
4817c478bd9Sstevel@tonic-gate 			 * Add stuff to blind carbon copies list.
4827c478bd9Sstevel@tonic-gate 			 */
4837c478bd9Sstevel@tonic-gate 			hp->h_bcc = addto(hp->h_bcc, &linebuf[2]);
4847c478bd9Sstevel@tonic-gate 			hp->h_seq++;
4857c478bd9Sstevel@tonic-gate 			break;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 		case 'R':
4887c478bd9Sstevel@tonic-gate 			hp->h_defopt = addone(hp->h_defopt, myname);
4897c478bd9Sstevel@tonic-gate 			hp->h_seq++;
4907c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("Return receipt marked.\n"));
4917c478bd9Sstevel@tonic-gate 			receipt_flg = 1;
4927c478bd9Sstevel@tonic-gate 			break;
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 		case 'd':
4957c478bd9Sstevel@tonic-gate 			copy(Getf("DEAD"), &linebuf[2]);
4967c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 		case '<':
4997c478bd9Sstevel@tonic-gate 		case 'r': {
5007c478bd9Sstevel@tonic-gate 			int	ispip;
5017c478bd9Sstevel@tonic-gate 			/*
5027c478bd9Sstevel@tonic-gate 			 * Invoke a file:
5037c478bd9Sstevel@tonic-gate 			 * Search for the file name,
5047c478bd9Sstevel@tonic-gate 			 * then open it and copy the contents to obuf.
5057c478bd9Sstevel@tonic-gate 			 *
5067c478bd9Sstevel@tonic-gate 			 * if name begins with '!', read from a command
5077c478bd9Sstevel@tonic-gate 			 */
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 			cp = &linebuf[2];
5107c478bd9Sstevel@tonic-gate 			while (any(*cp, " \t"))
5117c478bd9Sstevel@tonic-gate 				cp++;
5127c478bd9Sstevel@tonic-gate 			if (*cp == '\0') {
5137c478bd9Sstevel@tonic-gate 				printf(gettext("Interpolate what file?\n"));
5147c478bd9Sstevel@tonic-gate 				break;
5157c478bd9Sstevel@tonic-gate 			}
5167c478bd9Sstevel@tonic-gate 			if (*cp=='!') {
5177c478bd9Sstevel@tonic-gate 				/* take input from a command */
5187c478bd9Sstevel@tonic-gate 				ispip = 1;
5197c478bd9Sstevel@tonic-gate 				if ((fbuf = npopen(++cp, "r"))==NULL) {
5207c478bd9Sstevel@tonic-gate 					perror("");
5217c478bd9Sstevel@tonic-gate 					break;
5227c478bd9Sstevel@tonic-gate 				}
5237c478bd9Sstevel@tonic-gate 				sigint = sigset(SIGINT, SIG_IGN);
5247c478bd9Sstevel@tonic-gate 			} else {
5257c478bd9Sstevel@tonic-gate 				ispip = 0;
5267c478bd9Sstevel@tonic-gate 				cp = expand(cp);
5277c478bd9Sstevel@tonic-gate 				if (cp == NOSTR)
5287c478bd9Sstevel@tonic-gate 					break;
5297c478bd9Sstevel@tonic-gate 				if (isdir(cp)) {
5307c478bd9Sstevel@tonic-gate 					printf(gettext("%s: directory\n"), cp);
5317c478bd9Sstevel@tonic-gate 					break;
5327c478bd9Sstevel@tonic-gate 				}
5337c478bd9Sstevel@tonic-gate 				if ((fbuf = fopen(cp, "r")) == NULL) {
5347c478bd9Sstevel@tonic-gate 					perror(cp);
5357c478bd9Sstevel@tonic-gate 					break;
5367c478bd9Sstevel@tonic-gate 				}
5377c478bd9Sstevel@tonic-gate 			}
5387c478bd9Sstevel@tonic-gate 			printf("\"%s\" ", cp);
5397c478bd9Sstevel@tonic-gate 			flush();
5407c478bd9Sstevel@tonic-gate 			lc = cc = 0;
5417c478bd9Sstevel@tonic-gate 			while ((t = getc(fbuf)) != EOF) {
5427c478bd9Sstevel@tonic-gate 				if (t == '\n')
5437c478bd9Sstevel@tonic-gate 					lc++;
5447c478bd9Sstevel@tonic-gate 				if (putc(t, obuf) == EOF) {
5457c478bd9Sstevel@tonic-gate 					if (ispip) {
5467c478bd9Sstevel@tonic-gate 						npclose(fbuf);
5477c478bd9Sstevel@tonic-gate 						sigset(SIGINT, sigint);
5487c478bd9Sstevel@tonic-gate 					} else
5497c478bd9Sstevel@tonic-gate 						fclose(fbuf);
5507c478bd9Sstevel@tonic-gate 					goto werr;
5517c478bd9Sstevel@tonic-gate 				}
5527c478bd9Sstevel@tonic-gate 				cc++;
5537c478bd9Sstevel@tonic-gate 			}
5547c478bd9Sstevel@tonic-gate 			if (ispip) {
5557c478bd9Sstevel@tonic-gate 				npclose(fbuf);
5567c478bd9Sstevel@tonic-gate 				sigset(SIGINT, sigint);
5577c478bd9Sstevel@tonic-gate 			} else
5587c478bd9Sstevel@tonic-gate 				fclose(fbuf);
5597c478bd9Sstevel@tonic-gate 			printf("%ld/%ld\n", lc, cc);
5607c478bd9Sstevel@tonic-gate 			fflush(obuf);
5617c478bd9Sstevel@tonic-gate 			break;
5627c478bd9Sstevel@tonic-gate 			}
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 		case 'w':
5657c478bd9Sstevel@tonic-gate 			/*
5667c478bd9Sstevel@tonic-gate 			 * Write the message on a file.
5677c478bd9Sstevel@tonic-gate 			 */
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 			cp = &linebuf[2];
5707c478bd9Sstevel@tonic-gate 			while (any(*cp, " \t"))
5717c478bd9Sstevel@tonic-gate 				cp++;
5727c478bd9Sstevel@tonic-gate 			if (*cp == '\0') {
5737c478bd9Sstevel@tonic-gate 				fprintf(stderr, gettext("Write what file!?\n"));
5747c478bd9Sstevel@tonic-gate 				break;
5757c478bd9Sstevel@tonic-gate 			}
5767c478bd9Sstevel@tonic-gate 			if ((cp = expand(cp)) == NOSTR)
5777c478bd9Sstevel@tonic-gate 				break;
5787c478bd9Sstevel@tonic-gate 			fflush(obuf);
5797c478bd9Sstevel@tonic-gate 			rewind(ibuf);
5807c478bd9Sstevel@tonic-gate 			exwrite(cp, ibuf);
5817c478bd9Sstevel@tonic-gate 			break;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 		case 'm':
5847c478bd9Sstevel@tonic-gate 		case 'M':
5857c478bd9Sstevel@tonic-gate 		case 'f':
5867c478bd9Sstevel@tonic-gate 		case 'F':
5877c478bd9Sstevel@tonic-gate 			/*
5887c478bd9Sstevel@tonic-gate 			 * Interpolate the named messages, if we
5897c478bd9Sstevel@tonic-gate 			 * are in receiving mail mode.  Does the
5907c478bd9Sstevel@tonic-gate 			 * standard list processing garbage.
5917c478bd9Sstevel@tonic-gate 			 * If ~f or ~F is given, we don't shift over.
5927c478bd9Sstevel@tonic-gate 			 */
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 			if (!rcvmode) {
5957c478bd9Sstevel@tonic-gate 				printf(gettext(
5967c478bd9Sstevel@tonic-gate 				    "No messages to send from!?!\n"));
5977c478bd9Sstevel@tonic-gate 				break;
5987c478bd9Sstevel@tonic-gate 			}
5997c478bd9Sstevel@tonic-gate 			cp = &linebuf[2];
6007c478bd9Sstevel@tonic-gate 			while (any(*cp, " \t"))
6017c478bd9Sstevel@tonic-gate 				cp++;
6027c478bd9Sstevel@tonic-gate 			if (forward(cp, obuf, c) < 0)
6037c478bd9Sstevel@tonic-gate 				goto werr;
6047c478bd9Sstevel@tonic-gate 			fflush(obuf);
6057c478bd9Sstevel@tonic-gate 			printf(gettext("(continue)\n"));
6067c478bd9Sstevel@tonic-gate 			break;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 		case '?':
6097c478bd9Sstevel@tonic-gate 			if ((fbuf = fopen(THELPFILE, "r")) == NULL) {
6107c478bd9Sstevel@tonic-gate 				printf(gettext("No help just now.\n"));
6117c478bd9Sstevel@tonic-gate 				break;
6127c478bd9Sstevel@tonic-gate 			}
6137c478bd9Sstevel@tonic-gate 			t = getc(fbuf);
6147c478bd9Sstevel@tonic-gate 			while (t != -1) {
6157c478bd9Sstevel@tonic-gate 				putchar(t);
6167c478bd9Sstevel@tonic-gate 				t = getc(fbuf);
6177c478bd9Sstevel@tonic-gate 			}
6187c478bd9Sstevel@tonic-gate 			fclose(fbuf);
6197c478bd9Sstevel@tonic-gate 			break;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 		case 'p': {
6227c478bd9Sstevel@tonic-gate 			/*
6237c478bd9Sstevel@tonic-gate 			 * Print out the current state of the
6247c478bd9Sstevel@tonic-gate 			 * message without altering anything.
6257c478bd9Sstevel@tonic-gate 			 */
6267c478bd9Sstevel@tonic-gate 			int nlines;
6277c478bd9Sstevel@tonic-gate 			extern jmp_buf pipestop;
6287c478bd9Sstevel@tonic-gate 			extern void brokpipe(int);
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 			fflush(obuf);
6317c478bd9Sstevel@tonic-gate 			rewind(ibuf);
6327c478bd9Sstevel@tonic-gate 			fbuf = stdout;
6337c478bd9Sstevel@tonic-gate 			if (setjmp(pipestop))
6347c478bd9Sstevel@tonic-gate 				goto ret0;
6357c478bd9Sstevel@tonic-gate 			if (intty && outtty && (cp = value("crt")) != NOSTR) {
6367c478bd9Sstevel@tonic-gate 				nlines =
6377c478bd9Sstevel@tonic-gate 				    (*cp == '\0' ? screensize() : atoi(cp)) - 7;
6387c478bd9Sstevel@tonic-gate 				    /* 7 for hdr lines */
6397c478bd9Sstevel@tonic-gate 				while ((t = getc(ibuf)) != EOF) {
6407c478bd9Sstevel@tonic-gate 					if (t == '\n')
6417c478bd9Sstevel@tonic-gate 						if (--nlines <= 0)
6427c478bd9Sstevel@tonic-gate 							break;
6437c478bd9Sstevel@tonic-gate 				}
6447c478bd9Sstevel@tonic-gate 				rewind(ibuf);
6457c478bd9Sstevel@tonic-gate 				if (nlines <= 0) {
6467c478bd9Sstevel@tonic-gate 					fbuf = npopen(MORE, "w");
6477c478bd9Sstevel@tonic-gate 					if (fbuf == NULL) {
6487c478bd9Sstevel@tonic-gate 						perror(MORE);
6497c478bd9Sstevel@tonic-gate 						fbuf = stdout;
6507c478bd9Sstevel@tonic-gate 					} else {
6517c478bd9Sstevel@tonic-gate 						sigint = sigset(SIGINT, SIG_IGN);
6527c478bd9Sstevel@tonic-gate 						sigpipe = sigset(SIGPIPE, brokpipe);
6537c478bd9Sstevel@tonic-gate 					}
6547c478bd9Sstevel@tonic-gate 				}
6557c478bd9Sstevel@tonic-gate 			}
6567c478bd9Sstevel@tonic-gate 			fprintf(fbuf, gettext("-------\nMessage contains:\n"));
6577c478bd9Sstevel@tonic-gate 			puthead(hp, fbuf, GMASK, 0);
6587c478bd9Sstevel@tonic-gate 			while ((t = getc(ibuf))!=EOF)
6597c478bd9Sstevel@tonic-gate 				putc(t, fbuf);
6607c478bd9Sstevel@tonic-gate 		ret0:
6617c478bd9Sstevel@tonic-gate 			if (fbuf != stdout) {
6627c478bd9Sstevel@tonic-gate 				npclose(fbuf);
6637c478bd9Sstevel@tonic-gate 				sigset(SIGPIPE, sigpipe);
6647c478bd9Sstevel@tonic-gate 				sigset(SIGINT, sigint);
6657c478bd9Sstevel@tonic-gate 			}
6667c478bd9Sstevel@tonic-gate 			printf(gettext("(continue)\n"));
6677c478bd9Sstevel@tonic-gate 			break;
6687c478bd9Sstevel@tonic-gate 		}
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 		case '^':
6717c478bd9Sstevel@tonic-gate 		case '|':
6727c478bd9Sstevel@tonic-gate 			/*
6737c478bd9Sstevel@tonic-gate 			 * Pipe message through command.
6747c478bd9Sstevel@tonic-gate 			 * Collect output as new message.
6757c478bd9Sstevel@tonic-gate 			 */
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 			obuf = mespipe(ibuf, obuf, &linebuf[2]);
6787c478bd9Sstevel@tonic-gate 			newo = obuf;
6797c478bd9Sstevel@tonic-gate 			ibuf = newi;
6807c478bd9Sstevel@tonic-gate 			newi = ibuf;
6817c478bd9Sstevel@tonic-gate 			printf(gettext("(continue)\n"));
6827c478bd9Sstevel@tonic-gate 			break;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 		case 'v':
6857c478bd9Sstevel@tonic-gate 		case 'e':
6867c478bd9Sstevel@tonic-gate 			/*
6877c478bd9Sstevel@tonic-gate 			 * Edit the current message.
6887c478bd9Sstevel@tonic-gate 			 * 'e' means to use EDITOR
6897c478bd9Sstevel@tonic-gate 			 * 'v' means to use VISUAL
6907c478bd9Sstevel@tonic-gate 			 */
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 			if ((obuf = mesedit(ibuf, obuf, c, hp)) == NULL)
6937c478bd9Sstevel@tonic-gate 				goto err;
6947c478bd9Sstevel@tonic-gate 			newo = obuf;
6957c478bd9Sstevel@tonic-gate 			ibuf = newi;
6967c478bd9Sstevel@tonic-gate 			printf(gettext("(continue)\n"));
6977c478bd9Sstevel@tonic-gate 			break;
6987c478bd9Sstevel@tonic-gate 		}
6997c478bd9Sstevel@tonic-gate 		fflush(obuf);
7007c478bd9Sstevel@tonic-gate 	}
7017c478bd9Sstevel@tonic-gate eofl:
7027c478bd9Sstevel@tonic-gate 	fflush(obuf);
7037c478bd9Sstevel@tonic-gate 	if ((cp = value("MAILX_TAIL")) != NOSTR) {
7047c478bd9Sstevel@tonic-gate 	      cpout( cp, obuf);
7057c478bd9Sstevel@tonic-gate 	      if (isatty(fileno(stdin)))
7067c478bd9Sstevel@tonic-gate 		    cpout( cp, stdout);
7077c478bd9Sstevel@tonic-gate 	}
7087c478bd9Sstevel@tonic-gate 	fclose(obuf);
7097c478bd9Sstevel@tonic-gate 	rewind(ibuf);
7107c478bd9Sstevel@tonic-gate 	resetsigs(0);
7117c478bd9Sstevel@tonic-gate 	noreset = 0;
7127c478bd9Sstevel@tonic-gate 	return(ibuf);
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate werr:
7157c478bd9Sstevel@tonic-gate 	/*
7167c478bd9Sstevel@tonic-gate 	 * Write error occurred on tmp file, save partial
7177c478bd9Sstevel@tonic-gate 	 * message in dead.letter.
7187c478bd9Sstevel@tonic-gate 	 */
7197c478bd9Sstevel@tonic-gate 	perror(tempMail);
7207c478bd9Sstevel@tonic-gate 	fflush(obuf);
7217c478bd9Sstevel@tonic-gate 	rewind(ibuf);
7227c478bd9Sstevel@tonic-gate 	if (fsize(ibuf) > 0) {
7237c478bd9Sstevel@tonic-gate 		char *deadletter;
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 		deadletter = Getf("DEAD");
7267c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Saving partial message in %s\n"),
7277c478bd9Sstevel@tonic-gate 		    deadletter);
7287c478bd9Sstevel@tonic-gate 		if ((fbuf = fopen(deadletter,
7297c478bd9Sstevel@tonic-gate 		    value("appenddeadletter") == NOSTR ? "w" : "a")) != NULL) {
7307c478bd9Sstevel@tonic-gate 			chmod(deadletter, DEADPERM);
7317c478bd9Sstevel@tonic-gate 			puthead(hp, fbuf, GMASK|GCLEN, fsize(ibuf));
7327c478bd9Sstevel@tonic-gate 			lcwrite(deadletter, ibuf, fbuf, value("appenddeadletter") != NOSTR);
7337c478bd9Sstevel@tonic-gate 			fclose(fbuf);
7347c478bd9Sstevel@tonic-gate 		} else
7357c478bd9Sstevel@tonic-gate 			perror(deadletter);
7367c478bd9Sstevel@tonic-gate 	}
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate err:
7397c478bd9Sstevel@tonic-gate 	if (ibuf != NULL)
7407c478bd9Sstevel@tonic-gate 		fclose(ibuf);
7417c478bd9Sstevel@tonic-gate 	if (obuf != NULL)
7427c478bd9Sstevel@tonic-gate 		fclose(obuf);
7437c478bd9Sstevel@tonic-gate 	resetsigs(0);
7447c478bd9Sstevel@tonic-gate 	noreset = 0;
7457c478bd9Sstevel@tonic-gate 	return(NULL);
7467c478bd9Sstevel@tonic-gate }
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate static void
7497c478bd9Sstevel@tonic-gate resetsigs(int resethup)
7507c478bd9Sstevel@tonic-gate {
7517c478bd9Sstevel@tonic-gate 	(void) sigset(SIGINT, savesig);
7527c478bd9Sstevel@tonic-gate 	if (resethup)
7537c478bd9Sstevel@tonic-gate 		(void) sigset(SIGHUP, savehup);
7547c478bd9Sstevel@tonic-gate #ifdef SIGCONT
7557c478bd9Sstevel@tonic-gate # ifdef preSVr4
7567c478bd9Sstevel@tonic-gate 	(void) sigset(SIGCONT, savecont);
7577c478bd9Sstevel@tonic-gate # else
7587c478bd9Sstevel@tonic-gate 	{
7597c478bd9Sstevel@tonic-gate 	struct sigaction nsig;
7607c478bd9Sstevel@tonic-gate 	nsig.sa_handler = (void (*)())savecont;
7617c478bd9Sstevel@tonic-gate 	sigemptyset(&nsig.sa_mask);
7627c478bd9Sstevel@tonic-gate 	nsig.sa_flags = SA_RESTART;
7637c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGCONT, &nsig, (struct sigaction*)0);
7647c478bd9Sstevel@tonic-gate 	}
7657c478bd9Sstevel@tonic-gate # endif
7667c478bd9Sstevel@tonic-gate #endif
7677c478bd9Sstevel@tonic-gate }
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate /*
7707c478bd9Sstevel@tonic-gate  * Write a file ex-like.
7717c478bd9Sstevel@tonic-gate  */
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate static int
7747c478bd9Sstevel@tonic-gate exwrite(char name[], FILE *ibuf)
7757c478bd9Sstevel@tonic-gate {
7767c478bd9Sstevel@tonic-gate 	register FILE *of;
7777c478bd9Sstevel@tonic-gate 	struct stat junk;
7787c478bd9Sstevel@tonic-gate 	void (*sigint)(int), (*sigpipe)(int);
7797c478bd9Sstevel@tonic-gate 	int pi = (*name == '!');
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	if ((of = pi ? npopen(++name, "w") : fopen(name, "a")) == NULL) {
7827c478bd9Sstevel@tonic-gate 		perror(name);
7837c478bd9Sstevel@tonic-gate 		return(-1);
7847c478bd9Sstevel@tonic-gate 	}
7857c478bd9Sstevel@tonic-gate 	if (pi) {
7867c478bd9Sstevel@tonic-gate 		sigint = sigset(SIGINT, SIG_IGN);
7877c478bd9Sstevel@tonic-gate 		sigpipe = sigset(SIGPIPE, SIG_IGN);
7887c478bd9Sstevel@tonic-gate 	}
7897c478bd9Sstevel@tonic-gate 	lcwrite(name, ibuf, of, 0);
7907c478bd9Sstevel@tonic-gate 	pi ? npclose(of) : fclose(of);
7917c478bd9Sstevel@tonic-gate 	if (pi) {
7927c478bd9Sstevel@tonic-gate 		sigset(SIGPIPE, sigpipe);
7937c478bd9Sstevel@tonic-gate 		sigset(SIGINT, sigint);
7947c478bd9Sstevel@tonic-gate 	}
7957c478bd9Sstevel@tonic-gate 	return(0);
7967c478bd9Sstevel@tonic-gate }
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate void
7997c478bd9Sstevel@tonic-gate lcwrite(char *fn, FILE *fi, FILE *fo, int addnl)
8007c478bd9Sstevel@tonic-gate {
8017c478bd9Sstevel@tonic-gate 	register int c;
8027c478bd9Sstevel@tonic-gate 	long lc, cc;
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	printf("\"%s\" ", fn);
8057c478bd9Sstevel@tonic-gate 	fflush(stdout);
8067c478bd9Sstevel@tonic-gate 	lc = cc = 0;
8077c478bd9Sstevel@tonic-gate 	while ((c = getc(fi)) != EOF) {
8087c478bd9Sstevel@tonic-gate 		cc++;
8097c478bd9Sstevel@tonic-gate 		if (putc(c, fo) == '\n')
8107c478bd9Sstevel@tonic-gate 			lc++;
8117c478bd9Sstevel@tonic-gate 		if (ferror(fo)) {
8127c478bd9Sstevel@tonic-gate 			perror("");
8137c478bd9Sstevel@tonic-gate 			return;
8147c478bd9Sstevel@tonic-gate 		}
8157c478bd9Sstevel@tonic-gate 	}
8167c478bd9Sstevel@tonic-gate 	if (addnl) {
8177c478bd9Sstevel@tonic-gate 		putc('\n', fo);
8187c478bd9Sstevel@tonic-gate 		lc++;
8197c478bd9Sstevel@tonic-gate 		cc++;
8207c478bd9Sstevel@tonic-gate 	}
8217c478bd9Sstevel@tonic-gate 	fflush(fo);
8227c478bd9Sstevel@tonic-gate 	if (fferror(fo)) {
8237c478bd9Sstevel@tonic-gate 		perror("");
8247c478bd9Sstevel@tonic-gate 		return;
8257c478bd9Sstevel@tonic-gate 	}
8267c478bd9Sstevel@tonic-gate 	printf("%ld/%ld\n", lc, cc);
8277c478bd9Sstevel@tonic-gate 	fflush(stdout);
8287c478bd9Sstevel@tonic-gate }
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate /*
8317c478bd9Sstevel@tonic-gate  * Edit the message being collected on ibuf and obuf.
8327c478bd9Sstevel@tonic-gate  * Write the message out onto some poorly-named temp file
8337c478bd9Sstevel@tonic-gate  * and point an editor at it.
8347c478bd9Sstevel@tonic-gate  *
8357c478bd9Sstevel@tonic-gate  * On return, make the edit file the new temp file.
8367c478bd9Sstevel@tonic-gate  */
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate static FILE *
8397c478bd9Sstevel@tonic-gate mesedit(FILE *ibuf, FILE *obuf, int c, struct header *hp)
8407c478bd9Sstevel@tonic-gate {
8417c478bd9Sstevel@tonic-gate 	pid_t pid;
8427c478bd9Sstevel@tonic-gate 	FILE *fbuf;
8437c478bd9Sstevel@tonic-gate 	register int t;
8447c478bd9Sstevel@tonic-gate 	void (*sigint)(int);
8457c478bd9Sstevel@tonic-gate #ifdef SIGCONT
8467c478bd9Sstevel@tonic-gate 	void (*sigcont)(int);
8477c478bd9Sstevel@tonic-gate #endif
8487c478bd9Sstevel@tonic-gate 	struct stat sbuf;
8497c478bd9Sstevel@tonic-gate 	register char *edit;
8507c478bd9Sstevel@tonic-gate 	char hdr[LINESIZE];
8517c478bd9Sstevel@tonic-gate 	char *oto, *osubject, *occ, *obcc, **oothers;
8527c478bd9Sstevel@tonic-gate 	int fd = -1;
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	if (stat(tempEdit, &sbuf) >= 0) {
8557c478bd9Sstevel@tonic-gate 		printf(gettext("%s: file exists\n"), tempEdit);
8567c478bd9Sstevel@tonic-gate 		goto out;
8577c478bd9Sstevel@tonic-gate 	}
8587c478bd9Sstevel@tonic-gate 	if ((fd = open(tempEdit, O_RDWR|O_CREAT|O_EXCL, 0600)) < 0 ||
8597c478bd9Sstevel@tonic-gate 	(fbuf = fdopen(fd, "w")) == NULL) {
8607c478bd9Sstevel@tonic-gate 		perror(tempEdit);
8617c478bd9Sstevel@tonic-gate 		goto out;
8627c478bd9Sstevel@tonic-gate 	}
8637c478bd9Sstevel@tonic-gate 	fflush(obuf);
8647c478bd9Sstevel@tonic-gate 	rewind(ibuf);
8657c478bd9Sstevel@tonic-gate 	puthead(hp, fbuf, GMASK, 0);
8667c478bd9Sstevel@tonic-gate 	while ((t = getc(ibuf)) != EOF)
8677c478bd9Sstevel@tonic-gate 		putc(t, fbuf);
8687c478bd9Sstevel@tonic-gate 	fflush(fbuf);
8697c478bd9Sstevel@tonic-gate 	if (fferror(fbuf)) {
8707c478bd9Sstevel@tonic-gate 		perror(tempEdit);
8717c478bd9Sstevel@tonic-gate 		removefile(tempEdit);
8727c478bd9Sstevel@tonic-gate 		goto out;
8737c478bd9Sstevel@tonic-gate 	}
8747c478bd9Sstevel@tonic-gate 	fclose(fbuf);
8757c478bd9Sstevel@tonic-gate 	if ((edit = value(c == 'e' ? "EDITOR" : "VISUAL")) == NOSTR ||
8767c478bd9Sstevel@tonic-gate 	    *edit == '\0')
8777c478bd9Sstevel@tonic-gate 		edit = c == 'e' ? EDITOR : VISUAL;
8787c478bd9Sstevel@tonic-gate 	edit = safeexpand(edit);
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	/*
8817c478bd9Sstevel@tonic-gate 	 * Fork/execlp the editor on the edit file
8827c478bd9Sstevel@tonic-gate 	*/
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	pid = vfork();
8857c478bd9Sstevel@tonic-gate 	if (pid == (pid_t)-1) {
8867c478bd9Sstevel@tonic-gate 		perror("fork");
8877c478bd9Sstevel@tonic-gate 		removefile(tempEdit);
8887c478bd9Sstevel@tonic-gate 		goto out;
8897c478bd9Sstevel@tonic-gate 	}
8907c478bd9Sstevel@tonic-gate 	if (pid == 0) {
8917c478bd9Sstevel@tonic-gate 		char ecmd[BUFSIZ];
8927c478bd9Sstevel@tonic-gate 		char *Shell;
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 		sigchild();
8957c478bd9Sstevel@tonic-gate 		execlp(edit, edit, tempEdit, (char *)0);
8967c478bd9Sstevel@tonic-gate 		/*
8977c478bd9Sstevel@tonic-gate 		 * If execlp fails, "edit" might really be a complete
8987c478bd9Sstevel@tonic-gate 		 * shell command, not a simple pathname.  Try using
8997c478bd9Sstevel@tonic-gate 		 * the shell to run it.
9007c478bd9Sstevel@tonic-gate 		 */
9017c478bd9Sstevel@tonic-gate 		snprintf(ecmd, sizeof (ecmd), "exec %s %s", edit, tempEdit);
9027c478bd9Sstevel@tonic-gate 		if ((Shell = value("SHELL")) == NULL || *Shell=='\0')
9037c478bd9Sstevel@tonic-gate 			Shell = SHELL;
9047c478bd9Sstevel@tonic-gate 		execlp(Shell, Shell, "-c", ecmd, NULL);
9057c478bd9Sstevel@tonic-gate 		perror(edit);
9067c478bd9Sstevel@tonic-gate 		_exit(1);
9077c478bd9Sstevel@tonic-gate 	}
9087c478bd9Sstevel@tonic-gate 	sigint = sigset(SIGINT, SIG_IGN);
9097c478bd9Sstevel@tonic-gate #ifdef SIGCONT
9107c478bd9Sstevel@tonic-gate 	sigcont = sigset(SIGCONT, SIG_DFL);
9117c478bd9Sstevel@tonic-gate #endif
9127c478bd9Sstevel@tonic-gate 	while (wait((int *)0) != pid)
9137c478bd9Sstevel@tonic-gate 		;
9147c478bd9Sstevel@tonic-gate 	sigset(SIGINT, sigint);
9157c478bd9Sstevel@tonic-gate #ifdef SIGCONT
9167c478bd9Sstevel@tonic-gate 	sigset(SIGCONT, sigcont);
9177c478bd9Sstevel@tonic-gate #endif
9187c478bd9Sstevel@tonic-gate 	/*
9197c478bd9Sstevel@tonic-gate 	 * Now switch to new file.
9207c478bd9Sstevel@tonic-gate 	 */
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	if ((fbuf = fopen(tempEdit, "r")) == NULL) {
9237c478bd9Sstevel@tonic-gate 		perror(tempEdit);
9247c478bd9Sstevel@tonic-gate 		removefile(tempEdit);
9257c478bd9Sstevel@tonic-gate 		goto out;
9267c478bd9Sstevel@tonic-gate 	}
9277c478bd9Sstevel@tonic-gate 	removefile(tempEdit);
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	/* save the old headers, in case they are accidentally deleted */
9307c478bd9Sstevel@tonic-gate 	osubject = hp->h_subject;
9317c478bd9Sstevel@tonic-gate 	oto = hp->h_to;
9327c478bd9Sstevel@tonic-gate 	occ = hp->h_cc;
9337c478bd9Sstevel@tonic-gate 	obcc = hp->h_bcc;
9347c478bd9Sstevel@tonic-gate 	oothers = hp->h_others;
9357c478bd9Sstevel@tonic-gate 	hp->h_to = hp->h_subject = hp->h_cc = hp->h_bcc = hp->h_defopt = NOSTR;
9367c478bd9Sstevel@tonic-gate 	hp->h_others = NOSTRPTR;
9377c478bd9Sstevel@tonic-gate 	hp->h_seq = 0;
9387c478bd9Sstevel@tonic-gate 	while (gethfield(fbuf, hdr, 9999L) > 0) {
9397c478bd9Sstevel@tonic-gate 		if (ishfield(hdr, "to"))
9407c478bd9Sstevel@tonic-gate 			hp->h_to = addto(hp->h_to, hcontents(hdr));
9417c478bd9Sstevel@tonic-gate 		else if (ishfield(hdr, "subject"))
9427c478bd9Sstevel@tonic-gate 			hp->h_subject = addone(hp->h_subject, hcontents(hdr));
9437c478bd9Sstevel@tonic-gate 		else if (ishfield(hdr, "cc"))
9447c478bd9Sstevel@tonic-gate 			hp->h_cc = addto(hp->h_cc, hcontents(hdr));
9457c478bd9Sstevel@tonic-gate 		else if (ishfield(hdr, "bcc"))
9467c478bd9Sstevel@tonic-gate 			hp->h_bcc = addto(hp->h_bcc, hcontents(hdr));
9477c478bd9Sstevel@tonic-gate 		else if (ishfield(hdr, "default-options"))
9487c478bd9Sstevel@tonic-gate 			hp->h_defopt = addone(hp->h_defopt, hcontents(hdr));
9497c478bd9Sstevel@tonic-gate 		else
9507c478bd9Sstevel@tonic-gate 			hp->h_others = Xaddone(hp->h_others, hdr);
9517c478bd9Sstevel@tonic-gate 		hp->h_seq++;
9527c478bd9Sstevel@tonic-gate 	}
9537c478bd9Sstevel@tonic-gate 	if (hp->h_seq == 0) {
9547c478bd9Sstevel@tonic-gate 		/* if we didn't see any headers, restore the original headers */
9557c478bd9Sstevel@tonic-gate 		hp->h_subject = osubject;
9567c478bd9Sstevel@tonic-gate 		hp->h_to = oto;
9577c478bd9Sstevel@tonic-gate 		hp->h_cc = occ;
9587c478bd9Sstevel@tonic-gate 		hp->h_bcc = obcc;
9597c478bd9Sstevel@tonic-gate 		hp->h_others = oothers;
9607c478bd9Sstevel@tonic-gate 		printf(gettext(
9617c478bd9Sstevel@tonic-gate 		    "(Deleted headers restored to original values)\n"));
9627c478bd9Sstevel@tonic-gate 	}
9637c478bd9Sstevel@tonic-gate 	if ((fd = open(tempMail, O_RDWR|O_CREAT|O_EXCL, 0600)) < 0 ||
9647c478bd9Sstevel@tonic-gate 	(obuf = fdopen(fd, "w")) == NULL) {
9657c478bd9Sstevel@tonic-gate 		perror(tempMail);
9667c478bd9Sstevel@tonic-gate 		fclose(fbuf);
9677c478bd9Sstevel@tonic-gate 		goto out;
9687c478bd9Sstevel@tonic-gate 	}
9697c478bd9Sstevel@tonic-gate 	if ((ibuf = fopen(tempMail, "r")) == NULL) {
9707c478bd9Sstevel@tonic-gate 		perror(tempMail);
9717c478bd9Sstevel@tonic-gate 		removefile(tempMail);
9727c478bd9Sstevel@tonic-gate 		fclose(fbuf);
9737c478bd9Sstevel@tonic-gate 		fclose(obuf);
9747c478bd9Sstevel@tonic-gate 		goto out;
9757c478bd9Sstevel@tonic-gate 	}
9767c478bd9Sstevel@tonic-gate 	removefile(tempMail);
9777c478bd9Sstevel@tonic-gate 	if (strlen(hdr) != 0) {
9787c478bd9Sstevel@tonic-gate 		fputs(hdr, obuf);
9797c478bd9Sstevel@tonic-gate 		putc('\n', obuf);
9807c478bd9Sstevel@tonic-gate 	}
9817c478bd9Sstevel@tonic-gate 	while ((t = getc(fbuf)) != EOF)
9827c478bd9Sstevel@tonic-gate 		putc(t, obuf);
9837c478bd9Sstevel@tonic-gate 	fclose(fbuf);
9847c478bd9Sstevel@tonic-gate 	fclose(newo);
9857c478bd9Sstevel@tonic-gate 	fclose(newi);
9867c478bd9Sstevel@tonic-gate 	newo = obuf;
9877c478bd9Sstevel@tonic-gate 	newi = ibuf;
9887c478bd9Sstevel@tonic-gate out:
9897c478bd9Sstevel@tonic-gate 	return(newo);
9907c478bd9Sstevel@tonic-gate }
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate /*
9937c478bd9Sstevel@tonic-gate  * Pipe the message through the command.
9947c478bd9Sstevel@tonic-gate  * Old message is on stdin of command;
9957c478bd9Sstevel@tonic-gate  * New message collected from stdout.
9967c478bd9Sstevel@tonic-gate  * Sh -c must return 0 to accept the new message.
9977c478bd9Sstevel@tonic-gate  */
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate static FILE *
10007c478bd9Sstevel@tonic-gate mespipe(FILE *ibuf, FILE *obuf, char cmd[])
10017c478bd9Sstevel@tonic-gate {
10027c478bd9Sstevel@tonic-gate 	register FILE *ni, *no;
10037c478bd9Sstevel@tonic-gate 	pid_t pid;
10047c478bd9Sstevel@tonic-gate 	int s;
10057c478bd9Sstevel@tonic-gate 	void (*sigint)(int);
10067c478bd9Sstevel@tonic-gate 	char *Shell;
10077c478bd9Sstevel@tonic-gate 	int fd = -1;
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	newi = ibuf;
10107c478bd9Sstevel@tonic-gate 	if ((fd = open(tempEdit, O_RDWR|O_CREAT|O_EXCL, 0600)) < 0 ||
10117c478bd9Sstevel@tonic-gate 	(no = fdopen(fd, "w")) == NULL) {
10127c478bd9Sstevel@tonic-gate 		perror(tempEdit);
10137c478bd9Sstevel@tonic-gate 		return(obuf);
10147c478bd9Sstevel@tonic-gate 	}
10157c478bd9Sstevel@tonic-gate 	if ((ni = fopen(tempEdit, "r")) == NULL) {
10167c478bd9Sstevel@tonic-gate 		perror(tempEdit);
10177c478bd9Sstevel@tonic-gate 		fclose(no);
10187c478bd9Sstevel@tonic-gate 		removefile(tempEdit);
10197c478bd9Sstevel@tonic-gate 		return(obuf);
10207c478bd9Sstevel@tonic-gate 	}
10217c478bd9Sstevel@tonic-gate 	removefile(tempEdit);
10227c478bd9Sstevel@tonic-gate 	fflush(obuf);
10237c478bd9Sstevel@tonic-gate 	rewind(ibuf);
10247c478bd9Sstevel@tonic-gate 	if ((Shell = value("SHELL")) == NULL || *Shell=='\0')
10257c478bd9Sstevel@tonic-gate 		Shell = SHELL;
10267c478bd9Sstevel@tonic-gate 	if ((pid = vfork()) == (pid_t)-1) {
10277c478bd9Sstevel@tonic-gate 		perror("fork");
10287c478bd9Sstevel@tonic-gate 		goto err;
10297c478bd9Sstevel@tonic-gate 	}
10307c478bd9Sstevel@tonic-gate 	if (pid == 0) {
10317c478bd9Sstevel@tonic-gate 		/*
10327c478bd9Sstevel@tonic-gate 		 * stdin = current message.
10337c478bd9Sstevel@tonic-gate 		 * stdout = new message.
10347c478bd9Sstevel@tonic-gate 		 */
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 		sigchild();
10377c478bd9Sstevel@tonic-gate 		close(0);
10387c478bd9Sstevel@tonic-gate 		dup(fileno(ibuf));
10397c478bd9Sstevel@tonic-gate 		close(1);
10407c478bd9Sstevel@tonic-gate 		dup(fileno(no));
10417c478bd9Sstevel@tonic-gate 		for (s = 4; s < 15; s++)
10427c478bd9Sstevel@tonic-gate 			close(s);
10437c478bd9Sstevel@tonic-gate 		execlp(Shell, Shell, "-c", cmd, (char *)0);
10447c478bd9Sstevel@tonic-gate 		perror(Shell);
10457c478bd9Sstevel@tonic-gate 		_exit(1);
10467c478bd9Sstevel@tonic-gate 	}
10477c478bd9Sstevel@tonic-gate 	sigint = sigset(SIGINT, SIG_IGN);
10487c478bd9Sstevel@tonic-gate 	while (wait(&s) != pid)
10497c478bd9Sstevel@tonic-gate 		;
10507c478bd9Sstevel@tonic-gate 	sigset(SIGINT, sigint);
10517c478bd9Sstevel@tonic-gate 	if (s != 0 || pid == (pid_t)-1) {
10527c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("\"%s\" failed!?\n"), cmd);
10537c478bd9Sstevel@tonic-gate 		goto err;
10547c478bd9Sstevel@tonic-gate 	}
10557c478bd9Sstevel@tonic-gate 	if (fsize(ni) == 0) {
10567c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("No bytes from \"%s\" !?\n"), cmd);
10577c478bd9Sstevel@tonic-gate 		goto err;
10587c478bd9Sstevel@tonic-gate 	}
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	/*
10617c478bd9Sstevel@tonic-gate 	 * Take new files.
10627c478bd9Sstevel@tonic-gate 	 */
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	newi = ni;
10657c478bd9Sstevel@tonic-gate 	fclose(ibuf);
10667c478bd9Sstevel@tonic-gate 	fclose(obuf);
10677c478bd9Sstevel@tonic-gate 	return(no);
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate err:
10707c478bd9Sstevel@tonic-gate 	fclose(no);
10717c478bd9Sstevel@tonic-gate 	fclose(ni);
10727c478bd9Sstevel@tonic-gate 	return(obuf);
10737c478bd9Sstevel@tonic-gate }
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate static char *indentprefix;	/* used instead of tab by tabputs */
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate /*
10787c478bd9Sstevel@tonic-gate  * Interpolate the named messages into the current
10797c478bd9Sstevel@tonic-gate  * message, preceding each line with a tab.
10807c478bd9Sstevel@tonic-gate  * Return a count of the number of characters now in
10817c478bd9Sstevel@tonic-gate  * the message, or -1 if an error is encountered writing
10827c478bd9Sstevel@tonic-gate  * the message temporary.  The flag argument is 'm' if we
10837c478bd9Sstevel@tonic-gate  * should shift over and 'f' if not.
10847c478bd9Sstevel@tonic-gate  */
10857c478bd9Sstevel@tonic-gate static int
10867c478bd9Sstevel@tonic-gate forward(char ms[], FILE *obuf, int f)
10877c478bd9Sstevel@tonic-gate {
10887c478bd9Sstevel@tonic-gate 	register int *msgvec, *ip;
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	msgvec = (int *) salloc((msgCount+1) * sizeof *msgvec);
10917c478bd9Sstevel@tonic-gate 	if (msgvec == NOINTPTR)
10927c478bd9Sstevel@tonic-gate 		return(0);
10937c478bd9Sstevel@tonic-gate 	if (getmsglist(ms, msgvec, 0) < 0)
10947c478bd9Sstevel@tonic-gate 		return(0);
10957c478bd9Sstevel@tonic-gate 	if (*msgvec == NULL) {
10967c478bd9Sstevel@tonic-gate 		*msgvec = first(0, MMNORM);
10977c478bd9Sstevel@tonic-gate 		if (*msgvec == NULL) {
10987c478bd9Sstevel@tonic-gate 			printf(gettext("No appropriate messages\n"));
10997c478bd9Sstevel@tonic-gate 			return(0);
11007c478bd9Sstevel@tonic-gate 		}
11017c478bd9Sstevel@tonic-gate 		msgvec[1] = NULL;
11027c478bd9Sstevel@tonic-gate 	}
11037c478bd9Sstevel@tonic-gate 	if (tolower(f) == 'm')
11047c478bd9Sstevel@tonic-gate 		indentprefix = value("indentprefix");
11057c478bd9Sstevel@tonic-gate 	printf(gettext("Interpolating:"));
11067c478bd9Sstevel@tonic-gate 	for (ip = msgvec; *ip != NULL; ip++) {
11077c478bd9Sstevel@tonic-gate 		touch(*ip);
11087c478bd9Sstevel@tonic-gate 		printf(" %d", *ip);
11097c478bd9Sstevel@tonic-gate 		if (msend(&message[*ip-1], obuf, islower(f) ? M_IGNORE : 0,
11107c478bd9Sstevel@tonic-gate 		    tolower(f) == 'm' ? tabputs : fputs) < 0) {
11117c478bd9Sstevel@tonic-gate 			perror(tempMail);
11127c478bd9Sstevel@tonic-gate 			return(-1);
11137c478bd9Sstevel@tonic-gate 		}
11147c478bd9Sstevel@tonic-gate 	}
11157c478bd9Sstevel@tonic-gate 	fflush(obuf);
11167c478bd9Sstevel@tonic-gate 	if (fferror(obuf)) {
11177c478bd9Sstevel@tonic-gate 		perror(tempMail);
11187c478bd9Sstevel@tonic-gate 		return(-1);
11197c478bd9Sstevel@tonic-gate 	}
11207c478bd9Sstevel@tonic-gate 	printf("\n");
11217c478bd9Sstevel@tonic-gate 	return(0);
11227c478bd9Sstevel@tonic-gate }
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate static int
11257c478bd9Sstevel@tonic-gate tabputs(const char *line, FILE *obuf)
11267c478bd9Sstevel@tonic-gate {
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	if (indentprefix)
11297c478bd9Sstevel@tonic-gate 		fputs(indentprefix, obuf);
11307c478bd9Sstevel@tonic-gate 	/* Don't create lines with only a tab on them */
11317c478bd9Sstevel@tonic-gate 	else if (line[0] != '\n')
11327c478bd9Sstevel@tonic-gate 		fputc('\t', obuf);
11337c478bd9Sstevel@tonic-gate 	return (fputs(line, obuf));
11347c478bd9Sstevel@tonic-gate }
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate /*
11377c478bd9Sstevel@tonic-gate  * Print (continue) when continued after ^Z.
11387c478bd9Sstevel@tonic-gate  */
11397c478bd9Sstevel@tonic-gate #ifdef SIGCONT
11407c478bd9Sstevel@tonic-gate static void
11417c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
11427c478bd9Sstevel@tonic-gate collcont(int)
11437c478bd9Sstevel@tonic-gate #else
11447c478bd9Sstevel@tonic-gate /* ARGSUSED */
11457c478bd9Sstevel@tonic-gate collcont(int s)
11467c478bd9Sstevel@tonic-gate #endif
11477c478bd9Sstevel@tonic-gate {
11487c478bd9Sstevel@tonic-gate 	printf(gettext("(continue)\n"));
11497c478bd9Sstevel@tonic-gate 	fflush(stdout);
11507c478bd9Sstevel@tonic-gate }
11517c478bd9Sstevel@tonic-gate #endif /* SIGCONT */
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate /*
11547c478bd9Sstevel@tonic-gate  * On interrupt, go here to save the partial
11557c478bd9Sstevel@tonic-gate  * message on ~/dead.letter.
11567c478bd9Sstevel@tonic-gate  * Then restore signals and execute the normal
11577c478bd9Sstevel@tonic-gate  * signal routine.  We only come here if signals
11587c478bd9Sstevel@tonic-gate  * were previously set anyway.
11597c478bd9Sstevel@tonic-gate  */
11607c478bd9Sstevel@tonic-gate static void
11617c478bd9Sstevel@tonic-gate collrub(int s)
11627c478bd9Sstevel@tonic-gate {
11637c478bd9Sstevel@tonic-gate 	register FILE *dbuf;
11647c478bd9Sstevel@tonic-gate 	register char *deadletter;
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate # ifdef OLD_BSD_SIGS
11677c478bd9Sstevel@tonic-gate 	if (s == SIGHUP)
11687c478bd9Sstevel@tonic-gate 		sigignore(SIGHUP);
11697c478bd9Sstevel@tonic-gate # endif
11707c478bd9Sstevel@tonic-gate 	if (s == SIGINT && hadintr == 0) {
11717c478bd9Sstevel@tonic-gate 		hadintr++;
11727c478bd9Sstevel@tonic-gate 		fflush(stdout);
11737c478bd9Sstevel@tonic-gate 		fprintf(stderr,
11747c478bd9Sstevel@tonic-gate 		    gettext("\n(Interrupt -- one more to kill letter)\n"));
11757c478bd9Sstevel@tonic-gate # ifdef OLD_BSD_SIGS
11767c478bd9Sstevel@tonic-gate 		sigrelse(s);
11777c478bd9Sstevel@tonic-gate # endif
11787c478bd9Sstevel@tonic-gate 		longjmp(coljmp, 1);
11797c478bd9Sstevel@tonic-gate 	}
11807c478bd9Sstevel@tonic-gate 	fclose(newo);
11817c478bd9Sstevel@tonic-gate 	rewind(newi);
11827c478bd9Sstevel@tonic-gate 	if (s == SIGINT && value("save")==NOSTR || fsize(newi) == 0)
11837c478bd9Sstevel@tonic-gate 		goto done;
11847c478bd9Sstevel@tonic-gate 	deadletter = Getf("DEAD");
11857c478bd9Sstevel@tonic-gate 	if ((dbuf = fopen(deadletter,
11867c478bd9Sstevel@tonic-gate 	    (value("appenddeadletter") == NOSTR ? "w" : "a"))) == NULL) {
11877c478bd9Sstevel@tonic-gate 		perror(deadletter);
11887c478bd9Sstevel@tonic-gate 		goto done;
11897c478bd9Sstevel@tonic-gate 	}
11907c478bd9Sstevel@tonic-gate 	chmod(deadletter, DEADPERM);
11917c478bd9Sstevel@tonic-gate 	puthead(savehp, dbuf, GMASK|GCLEN, fsize(newi));
11927c478bd9Sstevel@tonic-gate 	lcwrite(deadletter, newi, dbuf, value("appenddeadletter") != NOSTR);
11937c478bd9Sstevel@tonic-gate 	fclose(dbuf);
11947c478bd9Sstevel@tonic-gate done:
11957c478bd9Sstevel@tonic-gate 	fclose(newi);
11967c478bd9Sstevel@tonic-gate 	resetsigs(1);
11977c478bd9Sstevel@tonic-gate 	if (rcvmode) {
11987c478bd9Sstevel@tonic-gate 		if (s == SIGHUP)
11997c478bd9Sstevel@tonic-gate 			hangup(s);
12007c478bd9Sstevel@tonic-gate 		else
12017c478bd9Sstevel@tonic-gate 			stop(s);
12027c478bd9Sstevel@tonic-gate 	}
12037c478bd9Sstevel@tonic-gate 	else
12047c478bd9Sstevel@tonic-gate 		exit(1);
12057c478bd9Sstevel@tonic-gate }
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate /*
12087c478bd9Sstevel@tonic-gate  * Acknowledge an interrupt signal from the tty by typing an @
12097c478bd9Sstevel@tonic-gate  */
12107c478bd9Sstevel@tonic-gate static void
12117c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
12127c478bd9Sstevel@tonic-gate intack(int)
12137c478bd9Sstevel@tonic-gate #else
12147c478bd9Sstevel@tonic-gate /* ARGSUSED */
12157c478bd9Sstevel@tonic-gate intack(int s)
12167c478bd9Sstevel@tonic-gate #endif
12177c478bd9Sstevel@tonic-gate {
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 	puts("@");
12207c478bd9Sstevel@tonic-gate 	fflush(stdout);
12217c478bd9Sstevel@tonic-gate 	clearerr(stdin);
12227c478bd9Sstevel@tonic-gate 	longjmp(coljmp,1);
12237c478bd9Sstevel@tonic-gate }
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate /* Read line from stdin, noting any NULL characters.
12267c478bd9Sstevel@tonic-gate    Return the number of characters read. Note that the buffer
12277c478bd9Sstevel@tonic-gate    passed must be 1 larger than "size" for the trailing NUL byte.
12287c478bd9Sstevel@tonic-gate  */
12297c478bd9Sstevel@tonic-gate int
12307c478bd9Sstevel@tonic-gate getline(char *line, int size, FILE *f, int *hasnulls)
12317c478bd9Sstevel@tonic-gate {
12327c478bd9Sstevel@tonic-gate 	register int i, ch;
12337c478bd9Sstevel@tonic-gate 	for (i = 0; (i < size) && ((ch=getc(f)) != EOF); ) {
12347c478bd9Sstevel@tonic-gate 		if ( ch == '\0' )
12357c478bd9Sstevel@tonic-gate 			*hasnulls = 1;
12367c478bd9Sstevel@tonic-gate 		if ((line[i++] = (char)ch) == '\n') break;
12377c478bd9Sstevel@tonic-gate 	}
12387c478bd9Sstevel@tonic-gate 	line[i] = '\0';
12397c478bd9Sstevel@tonic-gate 	return(i);
12407c478bd9Sstevel@tonic-gate }
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate void
12437c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
12447c478bd9Sstevel@tonic-gate savedead(int)
12457c478bd9Sstevel@tonic-gate #else
12467c478bd9Sstevel@tonic-gate /* ARGSUSED */
12477c478bd9Sstevel@tonic-gate savedead(int s)
12487c478bd9Sstevel@tonic-gate #endif
12497c478bd9Sstevel@tonic-gate {
12507c478bd9Sstevel@tonic-gate 	collrub(SIGINT);
12517c478bd9Sstevel@tonic-gate 	exit(1);
12527c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
12537c478bd9Sstevel@tonic-gate }
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate /*
12567c478bd9Sstevel@tonic-gate  * Add a list of addresses to the end of a header entry field.
12577c478bd9Sstevel@tonic-gate  */
12587c478bd9Sstevel@tonic-gate char *
12597c478bd9Sstevel@tonic-gate addto(char hf[], char news[])
12607c478bd9Sstevel@tonic-gate {
12617c478bd9Sstevel@tonic-gate 	char name[LINESIZE];
12627c478bd9Sstevel@tonic-gate 	int comma = docomma(news);
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 	while (news = yankword(news, name, sizeof (name), comma)) {
12657c478bd9Sstevel@tonic-gate 		nstrcat(name, sizeof (name), ", ");
12667c478bd9Sstevel@tonic-gate 		hf = addone(hf, name);
12677c478bd9Sstevel@tonic-gate 	}
12687c478bd9Sstevel@tonic-gate 	return hf;
12697c478bd9Sstevel@tonic-gate }
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate /*
12727c478bd9Sstevel@tonic-gate  * Add a string to the end of a header entry field.
12737c478bd9Sstevel@tonic-gate  */
12747c478bd9Sstevel@tonic-gate char *
12757c478bd9Sstevel@tonic-gate addone(char hf[], char news[])
12767c478bd9Sstevel@tonic-gate {
12777c478bd9Sstevel@tonic-gate 	register char *cp, *cp2, *linebuf;
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 	if (hf == NOSTR)
12807c478bd9Sstevel@tonic-gate 		hf = savestr("");
12817c478bd9Sstevel@tonic-gate 	if (*news == '\0')
12827c478bd9Sstevel@tonic-gate 		return(hf);
12837c478bd9Sstevel@tonic-gate 	linebuf = (char *)srealloc(hf, (unsigned)(strlen(hf) + strlen(news) + 2));
12847c478bd9Sstevel@tonic-gate 	cp2 = strchr(linebuf, '\0');
12857c478bd9Sstevel@tonic-gate 	if (cp2 > linebuf && cp2[-1] != ' ')
12867c478bd9Sstevel@tonic-gate 		*cp2++ = ' ';
12877c478bd9Sstevel@tonic-gate 	for (cp = news; any(*cp, " \t"); cp++)
12887c478bd9Sstevel@tonic-gate 		;
12897c478bd9Sstevel@tonic-gate 	while (*cp != '\0')
12907c478bd9Sstevel@tonic-gate 		*cp2++ = *cp++;
12917c478bd9Sstevel@tonic-gate 	*cp2 = '\0';
12927c478bd9Sstevel@tonic-gate 	return(linebuf);
12937c478bd9Sstevel@tonic-gate }
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate static int
12967c478bd9Sstevel@tonic-gate nptrs(char **hf)
12977c478bd9Sstevel@tonic-gate {
12987c478bd9Sstevel@tonic-gate 	register int i;
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 	if (!hf)
13017c478bd9Sstevel@tonic-gate 		return(0);
13027c478bd9Sstevel@tonic-gate 	for (i = 0; *hf; hf++)
13037c478bd9Sstevel@tonic-gate 		i++;
13047c478bd9Sstevel@tonic-gate 	return(i);
13057c478bd9Sstevel@tonic-gate }
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate /*
13087c478bd9Sstevel@tonic-gate  * Add a non-standard header to the end of the non-standard headers.
13097c478bd9Sstevel@tonic-gate  */
13107c478bd9Sstevel@tonic-gate static char **
13117c478bd9Sstevel@tonic-gate Xaddone(char **hf, char news[])
13127c478bd9Sstevel@tonic-gate {
13137c478bd9Sstevel@tonic-gate 	register char *linebuf;
13147c478bd9Sstevel@tonic-gate 	char **ohf = hf;
13157c478bd9Sstevel@tonic-gate 	int nhf = nptrs(hf);
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate 	if (hf == NOSTRPTR)
13187c478bd9Sstevel@tonic-gate 		hf = (char**)salloc(sizeof(char*) * 2);
13197c478bd9Sstevel@tonic-gate 	else
13207c478bd9Sstevel@tonic-gate 		hf = (char**)srealloc(hf, sizeof(char*) * (nhf + 2));
13217c478bd9Sstevel@tonic-gate 	if (hf == NOSTRPTR) {
13227c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("No room, header lost: %s\n"), news);
13237c478bd9Sstevel@tonic-gate 		return(ohf);
13247c478bd9Sstevel@tonic-gate 	}
13257c478bd9Sstevel@tonic-gate 	linebuf = (char *)salloc((unsigned)(strlen(news) + 1));
13267c478bd9Sstevel@tonic-gate 	strcpy(linebuf, news);
13277c478bd9Sstevel@tonic-gate 	hf[nhf++] = linebuf;
13287c478bd9Sstevel@tonic-gate 	hf[nhf] = NOSTR;
13297c478bd9Sstevel@tonic-gate 	return(hf);
13307c478bd9Sstevel@tonic-gate }
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate static void
13337c478bd9Sstevel@tonic-gate cpout(char *str, FILE *ofd)
13347c478bd9Sstevel@tonic-gate {
13357c478bd9Sstevel@tonic-gate 	register char *cp = str;
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 	while (*cp) {
13387c478bd9Sstevel@tonic-gate 		if (*cp == '\\') {
13397c478bd9Sstevel@tonic-gate 			switch (*(cp+1)) {
13407c478bd9Sstevel@tonic-gate 			case 'n':
13417c478bd9Sstevel@tonic-gate 				putc('\n', ofd);
13427c478bd9Sstevel@tonic-gate 				cp++;
13437c478bd9Sstevel@tonic-gate 				break;
13447c478bd9Sstevel@tonic-gate 			case 't':
13457c478bd9Sstevel@tonic-gate 				putc('\t', ofd);
13467c478bd9Sstevel@tonic-gate 				cp++;
13477c478bd9Sstevel@tonic-gate 				break;
13487c478bd9Sstevel@tonic-gate 			default:
13497c478bd9Sstevel@tonic-gate 				putc('\\', ofd);
13507c478bd9Sstevel@tonic-gate 			}
13517c478bd9Sstevel@tonic-gate 		} else {
13527c478bd9Sstevel@tonic-gate 			putc(*cp, ofd);
13537c478bd9Sstevel@tonic-gate 		}
13547c478bd9Sstevel@tonic-gate 		cp++;
13557c478bd9Sstevel@tonic-gate 	}
13567c478bd9Sstevel@tonic-gate 	putc('\n', ofd);
13577c478bd9Sstevel@tonic-gate 	fflush(ofd);
13587c478bd9Sstevel@tonic-gate }
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate static void
13617c478bd9Sstevel@tonic-gate xhalt(void)
13627c478bd9Sstevel@tonic-gate {
13637c478bd9Sstevel@tonic-gate 	fclose(newo);
13647c478bd9Sstevel@tonic-gate 	fclose(newi);
13657c478bd9Sstevel@tonic-gate 	sigset(SIGINT, savesig);
13667c478bd9Sstevel@tonic-gate 	sigset(SIGHUP, savehup);
13677c478bd9Sstevel@tonic-gate 	if (rcvmode)
13687c478bd9Sstevel@tonic-gate 		stop(0);
13697c478bd9Sstevel@tonic-gate 	exit(1);
13707c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
13717c478bd9Sstevel@tonic-gate }
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate /*
13747c478bd9Sstevel@tonic-gate  * Strip the nulls from a buffer of length n
13757c478bd9Sstevel@tonic-gate  */
13767c478bd9Sstevel@tonic-gate static int
13777c478bd9Sstevel@tonic-gate stripnulls(register char *linebuf, register int nread)
13787c478bd9Sstevel@tonic-gate {
13797c478bd9Sstevel@tonic-gate 	register int i, j;
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 	for (i = 0; i < nread; i++)
13827c478bd9Sstevel@tonic-gate 		if (linebuf[i] == '\0')
13837c478bd9Sstevel@tonic-gate 			break;
13847c478bd9Sstevel@tonic-gate 	for (j = i; j < nread; j++)
13857c478bd9Sstevel@tonic-gate 		if (linebuf[j] != '\0')
13867c478bd9Sstevel@tonic-gate 			linebuf[i++] = linebuf[j];
13877c478bd9Sstevel@tonic-gate 	linebuf[i] = '\0';
13887c478bd9Sstevel@tonic-gate 	return(i);
13897c478bd9Sstevel@tonic-gate }
1390