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