Home | History | Annotate | Line # | Download | only in mail
collect.c revision 1.35
      1  1.35  christos /*	$NetBSD: collect.c,v 1.35 2006/10/21 21:37:20 christos Exp $	*/
      2   1.6  christos 
      3   1.1       cgd /*
      4   1.3   deraadt  * Copyright (c) 1980, 1993
      5   1.3   deraadt  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.30       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.11     lukem #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34   1.6  christos #if 0
     35   1.6  christos static char sccsid[] = "@(#)collect.c	8.2 (Berkeley) 4/19/94";
     36   1.6  christos #else
     37  1.35  christos __RCSID("$NetBSD: collect.c,v 1.35 2006/10/21 21:37:20 christos Exp $");
     38   1.6  christos #endif
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41   1.1       cgd /*
     42   1.1       cgd  * Mail -- a mail program
     43   1.1       cgd  *
     44   1.1       cgd  * Collect input from standard input, handling
     45   1.1       cgd  * ~ escapes.
     46   1.1       cgd  */
     47   1.1       cgd 
     48   1.1       cgd #include "rcv.h"
     49   1.3   deraadt #include "extern.h"
     50  1.35  christos #ifdef MIME_SUPPORT
     51  1.35  christos #include "mime.h"
     52  1.35  christos #endif
     53   1.1       cgd 
     54  1.21  christos 
     55   1.1       cgd /*
     56  1.29       wiz  * Read a message from standard input and return a read file to it
     57   1.1       cgd  * or NULL on error.
     58   1.1       cgd  */
     59   1.1       cgd 
     60   1.1       cgd /*
     61   1.1       cgd  * The following hokiness with global variables is so that on
     62   1.1       cgd  * receipt of an interrupt signal, the partial message can be salted
     63   1.1       cgd  * away on dead.letter.
     64   1.1       cgd  */
     65   1.1       cgd 
     66   1.1       cgd static	sig_t	saveint;		/* Previous SIGINT value */
     67   1.1       cgd static	sig_t	savehup;		/* Previous SIGHUP value */
     68   1.1       cgd static	sig_t	savetstp;		/* Previous SIGTSTP value */
     69   1.1       cgd static	sig_t	savettou;		/* Previous SIGTTOU value */
     70   1.1       cgd static	sig_t	savettin;		/* Previous SIGTTIN value */
     71   1.1       cgd static	FILE	*collf;			/* File for saving away */
     72   1.1       cgd static	int	hadintr;		/* Have seen one SIGINT so far */
     73   1.1       cgd 
     74   1.1       cgd static	jmp_buf	colljmp;		/* To get back to work */
     75   1.1       cgd static	int	colljmp_p;		/* whether to long jump */
     76   1.1       cgd static	jmp_buf	collabort;		/* To end collection with error */
     77   1.1       cgd 
     78  1.33  christos 
     79   1.1       cgd FILE *
     80  1.22       wiz collect(struct header *hp, int printheaders)
     81   1.1       cgd {
     82   1.1       cgd 	FILE *fbuf;
     83  1.34  christos 	int lc, cc;
     84  1.27       wiz 	int c, fd, t;
     85  1.31  christos 	char linebuf[LINESIZE];
     86  1.31  christos 	const char *cp;
     87  1.27       wiz 	char tempname[PATHSIZE];
     88  1.28       wiz 	char mailtempname[PATHSIZE];
     89  1.34  christos 	int lastlong, rc;	/* So we don't make 2 or more lines
     90   1.8      phil 					   out of a long input line. */
     91  1.35  christos 	int eofcount;
     92  1.35  christos 	int longline;
     93  1.35  christos 	sigset_t  nset;
     94  1.34  christos 
     95  1.34  christos 	/* The following are declared volatile to avoid longjmp clobbering. */
     96  1.35  christos 	char volatile getsub;
     97  1.35  christos 	int volatile escape;
     98   1.1       cgd 
     99  1.32  christos 	(void)memset(mailtempname, 0, sizeof(mailtempname));
    100   1.1       cgd 	collf = NULL;
    101   1.1       cgd 	/*
    102   1.1       cgd 	 * Start catching signals from here, but we're still die on interrupts
    103   1.1       cgd 	 * until we're in the main loop.
    104   1.1       cgd 	 */
    105  1.35  christos 	(void)sigemptyset(&nset);
    106  1.35  christos 	(void)sigaddset(&nset, SIGINT);
    107  1.35  christos 	(void)sigaddset(&nset, SIGHUP);
    108  1.35  christos 	(void)sigprocmask(SIG_BLOCK, &nset, NULL);
    109   1.1       cgd 	if ((saveint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
    110  1.32  christos 		(void)signal(SIGINT, collint);
    111   1.1       cgd 	if ((savehup = signal(SIGHUP, SIG_IGN)) != SIG_IGN)
    112  1.32  christos 		(void)signal(SIGHUP, collhup);
    113   1.1       cgd 	savetstp = signal(SIGTSTP, collstop);
    114   1.1       cgd 	savettou = signal(SIGTTOU, collstop);
    115   1.1       cgd 	savettin = signal(SIGTTIN, collstop);
    116   1.1       cgd 	if (setjmp(collabort) || setjmp(colljmp)) {
    117  1.28       wiz 		(void)rm(mailtempname);
    118   1.1       cgd 		goto err;
    119   1.1       cgd 	}
    120  1.35  christos 	(void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
    121   1.1       cgd 
    122   1.1       cgd 	noreset++;
    123  1.28       wiz 	(void)snprintf(mailtempname, sizeof(mailtempname),
    124  1.28       wiz 	    "%s/mail.RsXXXXXXXXXX", tmpdir);
    125  1.28       wiz 	if ((fd = mkstemp(mailtempname)) == -1 ||
    126  1.28       wiz 	    (collf = Fdopen(fd, "w+")) == NULL) {
    127  1.28       wiz 		if (fd != -1)
    128  1.32  christos 			(void)close(fd);
    129  1.28       wiz 		warn("%s", mailtempname);
    130   1.1       cgd 		goto err;
    131   1.1       cgd 	}
    132  1.28       wiz 	(void)rm(mailtempname);
    133   1.1       cgd 
    134   1.1       cgd 	/*
    135   1.1       cgd 	 * If we are going to prompt for a subject,
    136   1.1       cgd 	 * refrain from printing a newline after
    137   1.1       cgd 	 * the headers (since some people mind).
    138   1.1       cgd 	 */
    139  1.33  christos 	t = GTO|GSUBJECT|GCC|GNL|GSMOPTS;
    140   1.1       cgd 	getsub = 0;
    141  1.24       wiz 	if (hp->h_subject == NULL && value("interactive") != NULL &&
    142  1.24       wiz 	    (value("ask") != NULL || value("asksub") != NULL))
    143   1.1       cgd 		t &= ~GNL, getsub++;
    144   1.1       cgd 	if (printheaders) {
    145  1.32  christos 		(void)puthead(hp, stdout, t);
    146  1.32  christos 		(void)fflush(stdout);
    147   1.1       cgd 	}
    148  1.24       wiz 	if ((cp = value("escape")) != NULL)
    149   1.1       cgd 		escape = *cp;
    150   1.1       cgd 	else
    151   1.1       cgd 		escape = ESCAPE;
    152  1.35  christos 	hadintr = 0;	/* static - no longjmp problem */
    153   1.1       cgd 	if (!setjmp(colljmp)) {
    154   1.1       cgd 		if (getsub)
    155  1.32  christos 			(void)grabh(hp, GSUBJECT);
    156   1.1       cgd 	} else {
    157   1.1       cgd 		/*
    158   1.1       cgd 		 * Come here for printing the after-signal message.
    159   1.1       cgd 		 * Duplicate messages won't be printed because
    160   1.1       cgd 		 * the write is aborted if we get a SIGTTOU.
    161   1.1       cgd 		 */
    162   1.1       cgd cont:
    163   1.1       cgd 		if (hadintr) {
    164  1.32  christos 			(void)fflush(stdout);
    165  1.32  christos 			(void)fprintf(stderr,
    166   1.1       cgd 			"\n(Interrupt -- one more to kill letter)\n");
    167   1.1       cgd 		} else {
    168  1.32  christos 			(void)printf("(continue)\n");
    169  1.32  christos 			(void)fflush(stdout);
    170   1.1       cgd 		}
    171   1.1       cgd 	}
    172  1.35  christos 	eofcount = 0;	/* reset after possible longjmp */
    173  1.35  christos 	longline = 0;	/* reset after possible longjmp */
    174   1.1       cgd 	for (;;) {
    175   1.1       cgd 		colljmp_p = 1;
    176  1.35  christos 		c = mail_readline(stdin, linebuf, LINESIZE);
    177   1.1       cgd 		colljmp_p = 0;
    178  1.35  christos #ifdef USE_EDITLINE
    179  1.33  christos 		if (c < 0) {
    180  1.33  christos 			char *p;
    181  1.33  christos 			if (value("interactive") != NULL &&
    182  1.33  christos 			    (p = value("ignoreeof")) != NULL &&
    183  1.33  christos 			    ++eofcount < (*p == 0 ? 25 : atoi(p))) {
    184  1.33  christos 				(void)printf("Use \".\" to terminate letter\n");
    185  1.33  christos 				continue;
    186  1.33  christos 			}
    187  1.33  christos 			break;
    188  1.33  christos 		}
    189  1.33  christos #else
    190   1.1       cgd 		if (c < 0) {
    191  1.24       wiz 			if (value("interactive") != NULL &&
    192  1.24       wiz 			    value("ignoreeof") != NULL && ++eofcount < 25) {
    193  1.32  christos 				(void)printf("Use \".\" to terminate letter\n");
    194   1.1       cgd 				continue;
    195   1.1       cgd 			}
    196   1.1       cgd 			break;
    197   1.1       cgd 		}
    198  1.33  christos #endif
    199   1.8      phil 		lastlong = longline;
    200   1.8      phil 		longline = c == LINESIZE-1;
    201   1.1       cgd 		eofcount = 0;
    202   1.1       cgd 		hadintr = 0;
    203   1.1       cgd 		if (linebuf[0] == '.' && linebuf[1] == '\0' &&
    204  1.24       wiz 		    value("interactive") != NULL && !lastlong &&
    205  1.24       wiz 		    (value("dot") != NULL || value("ignoreeof") != NULL))
    206   1.1       cgd 			break;
    207  1.24       wiz 		if (linebuf[0] != escape || value("interactive") == NULL ||
    208   1.8      phil 		    lastlong) {
    209   1.8      phil 			if (putline(collf, linebuf, !longline) < 0)
    210   1.1       cgd 				goto err;
    211   1.1       cgd 			continue;
    212   1.1       cgd 		}
    213   1.1       cgd 		c = linebuf[1];
    214   1.1       cgd 		switch (c) {
    215   1.1       cgd 		default:
    216   1.1       cgd 			/*
    217   1.1       cgd 			 * On double escape, just send the single one.
    218   1.1       cgd 			 * Otherwise, it's an error.
    219   1.1       cgd 			 */
    220   1.1       cgd 			if (c == escape) {
    221   1.8      phil 				if (putline(collf, &linebuf[1], !longline) < 0)
    222   1.1       cgd 					goto err;
    223   1.1       cgd 				else
    224   1.1       cgd 					break;
    225   1.1       cgd 			}
    226  1.32  christos 			(void)printf("Unknown tilde escape.\n");
    227   1.1       cgd 			break;
    228  1.35  christos #ifdef MIME_SUPPORT
    229  1.35  christos 		case '@':
    230  1.35  christos 			hp->h_attach = mime_attach_files(hp->h_attach,
    231  1.35  christos 			    &linebuf[2], ATTACH_FILE_CONTENT);
    232  1.35  christos 			break;
    233  1.35  christos #endif
    234   1.1       cgd 		case 'C':
    235   1.1       cgd 			/*
    236   1.1       cgd 			 * Dump core.
    237   1.1       cgd 			 */
    238  1.32  christos 			(void)core(NULL);
    239   1.1       cgd 			break;
    240   1.1       cgd 		case '!':
    241   1.1       cgd 			/*
    242   1.1       cgd 			 * Shell escape, send the balance of the
    243   1.1       cgd 			 * line to sh -c.
    244   1.1       cgd 			 */
    245  1.32  christos 			(void)shell(&linebuf[2]);
    246   1.1       cgd 			break;
    247   1.1       cgd 		case ':':
    248   1.5       jtc 		case '_':
    249   1.1       cgd 			/*
    250   1.1       cgd 			 * Escape to command mode, but be nice!
    251   1.1       cgd 			 */
    252  1.32  christos 			(void)execute(&linebuf[2], 1);
    253   1.1       cgd 			goto cont;
    254   1.1       cgd 		case '.':
    255   1.1       cgd 			/*
    256   1.1       cgd 			 * Simulate end of file on input.
    257   1.1       cgd 			 */
    258   1.1       cgd 			goto out;
    259   1.1       cgd 		case 'q':
    260   1.1       cgd 			/*
    261   1.1       cgd 			 * Force a quit of sending mail.
    262   1.1       cgd 			 * Act like an interrupt happened.
    263   1.1       cgd 			 */
    264   1.1       cgd 			hadintr++;
    265   1.1       cgd 			collint(SIGINT);
    266   1.1       cgd 			exit(1);
    267  1.32  christos 			/*NOTREACHED*/
    268  1.19       mjl 
    269  1.19       mjl 		case 'x':	/* exit, do not save in dead.letter */
    270  1.19       mjl 			goto err;
    271  1.19       mjl 
    272   1.1       cgd 		case 'h':
    273   1.1       cgd 			/*
    274   1.1       cgd 			 * Grab a bunch of headers.
    275   1.1       cgd 			 */
    276  1.33  christos 			(void)grabh(hp, GTO|GSUBJECT|GCC|GBCC|GSMOPTS);
    277   1.1       cgd 			goto cont;
    278   1.1       cgd 		case 't':
    279   1.1       cgd 			/*
    280   1.1       cgd 			 * Add to the To list.
    281   1.1       cgd 			 */
    282   1.1       cgd 			hp->h_to = cat(hp->h_to, extract(&linebuf[2], GTO));
    283   1.1       cgd 			break;
    284   1.1       cgd 		case 's':
    285   1.1       cgd 			/*
    286   1.1       cgd 			 * Set the Subject list.
    287   1.1       cgd 			 */
    288   1.1       cgd 			cp = &linebuf[2];
    289  1.18  christos 			while (isspace((unsigned char)*cp))
    290   1.1       cgd 				cp++;
    291   1.1       cgd 			hp->h_subject = savestr(cp);
    292   1.1       cgd 			break;
    293   1.1       cgd 		case 'c':
    294   1.1       cgd 			/*
    295   1.1       cgd 			 * Add to the CC list.
    296   1.1       cgd 			 */
    297   1.1       cgd 			hp->h_cc = cat(hp->h_cc, extract(&linebuf[2], GCC));
    298   1.1       cgd 			break;
    299   1.1       cgd 		case 'b':
    300   1.1       cgd 			/*
    301   1.1       cgd 			 * Add stuff to blind carbon copies list.
    302   1.1       cgd 			 */
    303   1.1       cgd 			hp->h_bcc = cat(hp->h_bcc, extract(&linebuf[2], GBCC));
    304   1.1       cgd 			break;
    305  1.19       mjl 		case 'i':
    306  1.19       mjl 		case 'A':
    307  1.19       mjl 		case 'a':
    308  1.19       mjl 			/*
    309  1.19       mjl 			 * Insert named variable in message
    310  1.19       mjl 			 */
    311  1.19       mjl 
    312  1.19       mjl 			switch(c) {
    313  1.19       mjl 				case 'i':
    314  1.19       mjl 					cp = &linebuf[2];
    315  1.19       mjl 					while(isspace((unsigned char) *cp))
    316  1.19       mjl 						cp++;
    317  1.19       mjl 
    318  1.19       mjl 					break;
    319  1.19       mjl 				case 'a':
    320  1.19       mjl 					cp = "sign";
    321  1.19       mjl 					break;
    322  1.19       mjl 				case 'A':
    323  1.19       mjl 					cp = "Sign";
    324  1.19       mjl 					break;
    325  1.19       mjl 				default:
    326  1.19       mjl 					goto err;
    327  1.19       mjl 			}
    328  1.19       mjl 
    329  1.24       wiz 			if(*cp && (cp = value(cp)) != NULL) {
    330  1.32  christos 				(void)printf("%s\n", cp);
    331  1.19       mjl 				if(putline(collf, cp, 1) < 0)
    332  1.19       mjl 					goto err;
    333  1.19       mjl 			}
    334  1.19       mjl 
    335  1.19       mjl 			break;
    336  1.19       mjl 
    337   1.1       cgd 		case 'd':
    338  1.32  christos 			(void)strcpy(linebuf + 2, getdeadletter());
    339  1.32  christos 			/* FALLTHROUGH */
    340   1.1       cgd 		case 'r':
    341   1.5       jtc 		case '<':
    342   1.1       cgd 			/*
    343   1.1       cgd 			 * Invoke a file:
    344   1.1       cgd 			 * Search for the file name,
    345   1.1       cgd 			 * then open it and copy the contents to collf.
    346   1.1       cgd 			 */
    347   1.1       cgd 			cp = &linebuf[2];
    348  1.18  christos 			while (isspace((unsigned char)*cp))
    349   1.1       cgd 				cp++;
    350   1.1       cgd 			if (*cp == '\0') {
    351  1.32  christos 				(void)printf("Interpolate what file?\n");
    352   1.1       cgd 				break;
    353   1.1       cgd 			}
    354  1.19       mjl 
    355   1.1       cgd 			cp = expand(cp);
    356  1.24       wiz 			if (cp == NULL)
    357   1.1       cgd 				break;
    358  1.19       mjl 
    359  1.19       mjl 			if (*cp == '!') {	/* insert stdout of command */
    360  1.31  christos 				const char *shellcmd;
    361  1.19       mjl 				int nullfd;
    362  1.23       wiz 				int rc2;
    363  1.19       mjl 
    364  1.19       mjl 				if((nullfd = open("/dev/null", O_RDONLY, 0)) == -1) {
    365  1.26       wiz 					warn("/dev/null");
    366  1.19       mjl 					break;
    367  1.19       mjl 				}
    368  1.19       mjl 
    369  1.27       wiz 				(void)snprintf(tempname, sizeof(tempname),
    370  1.28       wiz 				    "%s/mail.ReXXXXXXXXXX", tmpdir);
    371  1.27       wiz 				if ((fd = mkstemp(tempname)) == -1 ||
    372  1.27       wiz 				    (fbuf = Fdopen(fd, "w+")) == NULL) {
    373  1.27       wiz 					if (fd != -1)
    374  1.32  christos 						(void)close(fd);
    375  1.27       wiz 					warn("%s", tempname);
    376  1.19       mjl 					break;
    377  1.19       mjl 				}
    378  1.27       wiz 				(void)unlink(tempname);
    379  1.19       mjl 
    380  1.24       wiz 				if ((shellcmd = value("SHELL")) == NULL)
    381  1.23       wiz 					shellcmd = _PATH_CSHELL;
    382  1.19       mjl 
    383  1.35  christos 				rc2 = run_command(shellcmd, 0, nullfd, fileno(fbuf), "-c", cp + 1, NULL);
    384  1.19       mjl 
    385  1.32  christos 				(void)close(nullfd);
    386  1.19       mjl 
    387  1.23       wiz 				if (rc2 < 0) {
    388  1.25       wiz 					(void)Fclose(fbuf);
    389  1.19       mjl 					break;
    390  1.19       mjl 				}
    391  1.19       mjl 
    392  1.19       mjl 				if (fsize(fbuf) == 0) {
    393  1.35  christos 					(void)fprintf(stderr, "No bytes from command \"%s\"\n", cp + 1);
    394  1.25       wiz 					(void)Fclose(fbuf);
    395  1.19       mjl 					break;
    396  1.19       mjl 				}
    397  1.19       mjl 
    398  1.19       mjl 				rewind(fbuf);
    399  1.19       mjl 			}
    400  1.19       mjl 			else if (isdir(cp)) {
    401  1.32  christos 				(void)printf("%s: Directory\n", cp);
    402   1.1       cgd 				break;
    403   1.1       cgd 			}
    404  1.19       mjl 			else if ((fbuf = Fopen(cp, "r")) == NULL) {
    405  1.26       wiz 				warn("%s", cp);
    406   1.1       cgd 				break;
    407   1.1       cgd 			}
    408  1.32  christos 			(void)printf("\"%s\" ", cp);
    409  1.32  christos 			(void)fflush(stdout);
    410   1.1       cgd 			lc = 0;
    411   1.1       cgd 			cc = 0;
    412  1.35  christos 			while ((rc = mail_readline(fbuf, linebuf, LINESIZE)) >= 0) {
    413   1.8      phil 				if (rc != LINESIZE-1) lc++;
    414   1.8      phil 				if ((t = putline(collf, linebuf,
    415   1.8      phil 						 rc != LINESIZE-1)) < 0) {
    416  1.32  christos 					(void)Fclose(fbuf);
    417   1.1       cgd 					goto err;
    418   1.1       cgd 				}
    419   1.1       cgd 				cc += t;
    420   1.1       cgd 			}
    421  1.32  christos 			(void)Fclose(fbuf);
    422  1.32  christos 			(void)printf("%d/%d\n", lc, cc);
    423   1.1       cgd 			break;
    424   1.1       cgd 		case 'w':
    425   1.1       cgd 			/*
    426   1.1       cgd 			 * Write the message on a file.
    427   1.1       cgd 			 */
    428   1.1       cgd 			cp = &linebuf[2];
    429   1.1       cgd 			while (*cp == ' ' || *cp == '\t')
    430   1.1       cgd 				cp++;
    431   1.1       cgd 			if (*cp == '\0') {
    432  1.32  christos 				(void)fprintf(stderr, "Write what file!?\n");
    433   1.1       cgd 				break;
    434   1.1       cgd 			}
    435  1.24       wiz 			if ((cp = expand(cp)) == NULL)
    436   1.1       cgd 				break;
    437   1.1       cgd 			rewind(collf);
    438  1.32  christos 			(void)exwrite(cp, collf, 1);
    439   1.1       cgd 			break;
    440   1.1       cgd 		case 'm':
    441   1.1       cgd 		case 'M':
    442   1.1       cgd 		case 'f':
    443   1.1       cgd 		case 'F':
    444   1.1       cgd 			/*
    445   1.1       cgd 			 * Interpolate the named messages, if we
    446   1.1       cgd 			 * are in receiving mail mode.  Does the
    447   1.1       cgd 			 * standard list processing garbage.
    448   1.1       cgd 			 * If ~f is given, we don't shift over.
    449   1.1       cgd 			 */
    450  1.28       wiz 			if (forward(linebuf + 2, collf, mailtempname, c) < 0)
    451   1.1       cgd 				goto err;
    452   1.1       cgd 			goto cont;
    453   1.1       cgd 		case '?':
    454   1.1       cgd 			if ((fbuf = Fopen(_PATH_TILDE, "r")) == NULL) {
    455  1.26       wiz 				warn(_PATH_TILDE);
    456   1.1       cgd 				break;
    457   1.1       cgd 			}
    458   1.1       cgd 			while ((t = getc(fbuf)) != EOF)
    459  1.25       wiz 				(void)putchar(t);
    460  1.32  christos 			(void)Fclose(fbuf);
    461   1.1       cgd 			break;
    462   1.1       cgd 		case 'p':
    463   1.1       cgd 			/*
    464   1.1       cgd 			 * Print out the current state of the
    465   1.1       cgd 			 * message without altering anything.
    466   1.1       cgd 			 */
    467   1.1       cgd 			rewind(collf);
    468  1.32  christos 			(void)printf("-------\nMessage contains:\n");
    469  1.32  christos 			(void)puthead(hp, stdout, GTO|GSUBJECT|GCC|GBCC|GNL);
    470   1.1       cgd 			while ((t = getc(collf)) != EOF)
    471  1.25       wiz 				(void)putchar(t);
    472   1.1       cgd 			goto cont;
    473   1.1       cgd 		case '|':
    474   1.1       cgd 			/*
    475   1.1       cgd 			 * Pipe message through command.
    476   1.1       cgd 			 * Collect output as new message.
    477   1.1       cgd 			 */
    478   1.1       cgd 			rewind(collf);
    479   1.1       cgd 			mespipe(collf, &linebuf[2]);
    480   1.1       cgd 			goto cont;
    481   1.1       cgd 		case 'v':
    482   1.1       cgd 		case 'e':
    483   1.1       cgd 			/*
    484   1.1       cgd 			 * Edit the current message.
    485   1.1       cgd 			 * 'e' means to use EDITOR
    486   1.1       cgd 			 * 'v' means to use VISUAL
    487   1.1       cgd 			 */
    488   1.1       cgd 			rewind(collf);
    489   1.1       cgd 			mesedit(collf, c);
    490   1.1       cgd 			goto cont;
    491   1.1       cgd 		}
    492   1.1       cgd 	}
    493   1.1       cgd 	goto out;
    494   1.1       cgd err:
    495   1.1       cgd 	if (collf != NULL) {
    496  1.32  christos 		(void)Fclose(collf);
    497   1.1       cgd 		collf = NULL;
    498   1.1       cgd 	}
    499   1.1       cgd out:
    500   1.1       cgd 	if (collf != NULL)
    501   1.1       cgd 		rewind(collf);
    502   1.1       cgd 	noreset--;
    503  1.35  christos 	(void)sigprocmask(SIG_BLOCK, &nset, NULL);
    504  1.32  christos 	(void)signal(SIGINT, saveint);
    505  1.32  christos 	(void)signal(SIGHUP, savehup);
    506  1.32  christos 	(void)signal(SIGTSTP, savetstp);
    507  1.32  christos 	(void)signal(SIGTTOU, savettou);
    508  1.32  christos 	(void)signal(SIGTTIN, savettin);
    509  1.35  christos 	(void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
    510   1.1       cgd 	return collf;
    511   1.1       cgd }
    512   1.1       cgd 
    513   1.1       cgd /*
    514   1.1       cgd  * Write a file, ex-like if f set.
    515   1.1       cgd  */
    516   1.3   deraadt int
    517  1.31  christos exwrite(const char name[], FILE *fp, int f)
    518   1.1       cgd {
    519  1.11     lukem 	FILE *of;
    520  1.11     lukem 	int c;
    521   1.1       cgd 	long cc;
    522   1.1       cgd 	int lc;
    523   1.1       cgd 	struct stat junk;
    524   1.1       cgd 
    525   1.1       cgd 	if (f) {
    526  1.32  christos 		(void)printf("\"%s\" ", name);
    527  1.32  christos 		(void)fflush(stdout);
    528   1.1       cgd 	}
    529  1.12   mycroft 	if (stat(name, &junk) >= 0 && S_ISREG(junk.st_mode)) {
    530   1.1       cgd 		if (!f)
    531  1.32  christos 			(void)fprintf(stderr, "%s: ", name);
    532  1.32  christos 		(void)fprintf(stderr, "File exists\n");
    533   1.1       cgd 		return(-1);
    534   1.1       cgd 	}
    535   1.1       cgd 	if ((of = Fopen(name, "w")) == NULL) {
    536  1.26       wiz 		warn("%s", name);
    537   1.1       cgd 		return(-1);
    538   1.1       cgd 	}
    539   1.1       cgd 	lc = 0;
    540   1.1       cgd 	cc = 0;
    541   1.1       cgd 	while ((c = getc(fp)) != EOF) {
    542   1.1       cgd 		cc++;
    543   1.1       cgd 		if (c == '\n')
    544   1.1       cgd 			lc++;
    545  1.25       wiz 		(void)putc(c, of);
    546   1.1       cgd 		if (ferror(of)) {
    547  1.26       wiz 			warn("%s", name);
    548  1.32  christos 			(void)Fclose(of);
    549   1.1       cgd 			return(-1);
    550   1.1       cgd 		}
    551   1.1       cgd 	}
    552  1.32  christos 	(void)Fclose(of);
    553  1.32  christos 	(void)printf("%d/%ld\n", lc, cc);
    554  1.32  christos 	(void)fflush(stdout);
    555   1.1       cgd 	return(0);
    556   1.1       cgd }
    557   1.1       cgd 
    558   1.1       cgd /*
    559   1.1       cgd  * Edit the message being collected on fp.
    560   1.1       cgd  * On return, make the edit file the new temp file.
    561   1.1       cgd  */
    562   1.3   deraadt void
    563  1.22       wiz mesedit(FILE *fp, int c)
    564   1.1       cgd {
    565   1.1       cgd 	sig_t sigint = signal(SIGINT, SIG_IGN);
    566   1.1       cgd 	FILE *nf = run_editor(fp, (off_t)-1, c, 0);
    567   1.1       cgd 
    568   1.1       cgd 	if (nf != NULL) {
    569  1.32  christos 		(void)fseek(nf, 0L, 2);
    570   1.1       cgd 		collf = nf;
    571  1.32  christos 		(void)Fclose(fp);
    572   1.1       cgd 	}
    573  1.25       wiz 	(void)signal(SIGINT, sigint);
    574   1.1       cgd }
    575   1.1       cgd 
    576   1.1       cgd /*
    577   1.1       cgd  * Pipe the message through the command.
    578   1.1       cgd  * Old message is on stdin of command;
    579   1.1       cgd  * New message collected from stdout.
    580   1.1       cgd  * Sh -c must return 0 to accept the new message.
    581   1.1       cgd  */
    582   1.3   deraadt void
    583  1.22       wiz mespipe(FILE *fp, char cmd[])
    584   1.1       cgd {
    585   1.1       cgd 	FILE *nf;
    586   1.1       cgd 	sig_t sigint = signal(SIGINT, SIG_IGN);
    587  1.31  christos 	const char *shellcmd;
    588  1.27       wiz 	int fd;
    589  1.27       wiz 	char tempname[PATHSIZE];
    590   1.1       cgd 
    591  1.28       wiz 	(void)snprintf(tempname, sizeof(tempname),
    592  1.28       wiz 	    "%s/mail.ReXXXXXXXXXX", tmpdir);
    593  1.27       wiz 	if ((fd = mkstemp(tempname)) == -1 ||
    594  1.27       wiz 	    (nf = Fdopen(fd, "w+")) == NULL) {
    595  1.27       wiz 		if (fd != -1)
    596  1.32  christos 			(void)close(fd);
    597  1.27       wiz 		warn("%s", tempname);
    598   1.1       cgd 		goto out;
    599   1.1       cgd 	}
    600  1.27       wiz 	(void)unlink(tempname);
    601   1.1       cgd 	/*
    602   1.1       cgd 	 * stdin = current message.
    603   1.1       cgd 	 * stdout = new message.
    604   1.1       cgd 	 */
    605  1.24       wiz 	if ((shellcmd = value("SHELL")) == NULL)
    606  1.23       wiz 		shellcmd = _PATH_CSHELL;
    607  1.23       wiz 	if (run_command(shellcmd,
    608  1.24       wiz 	    0, fileno(fp), fileno(nf), "-c", cmd, NULL) < 0) {
    609  1.25       wiz 		(void)Fclose(nf);
    610   1.1       cgd 		goto out;
    611   1.1       cgd 	}
    612   1.1       cgd 	if (fsize(nf) == 0) {
    613  1.32  christos 		(void)fprintf(stderr, "No bytes from \"%s\" !?\n", cmd);
    614  1.25       wiz 		(void)Fclose(nf);
    615   1.1       cgd 		goto out;
    616   1.1       cgd 	}
    617   1.1       cgd 	/*
    618   1.1       cgd 	 * Take new files.
    619   1.1       cgd 	 */
    620  1.25       wiz 	(void)fseek(nf, 0L, 2);
    621   1.1       cgd 	collf = nf;
    622  1.25       wiz 	(void)Fclose(fp);
    623   1.1       cgd out:
    624  1.25       wiz 	(void)signal(SIGINT, sigint);
    625   1.1       cgd }
    626   1.1       cgd 
    627   1.1       cgd /*
    628   1.1       cgd  * Interpolate the named messages into the current
    629   1.1       cgd  * message, preceding each line with a tab.
    630   1.1       cgd  * Return a count of the number of characters now in
    631   1.1       cgd  * the message, or -1 if an error is encountered writing
    632   1.1       cgd  * the message temporary.  The flag argument is 'm' if we
    633   1.1       cgd  * should shift over and 'f' if not.
    634   1.1       cgd  */
    635   1.3   deraadt int
    636  1.28       wiz forward(char ms[], FILE *fp, char *fn, int f)
    637   1.1       cgd {
    638  1.11     lukem 	int *msgvec;
    639   1.1       cgd 	struct ignoretab *ig;
    640  1.31  christos 	const char *tabst;
    641  1.35  christos #ifdef MIME_SUPPORT
    642  1.35  christos 	struct mime_info *mip;
    643  1.35  christos 	int retval;
    644  1.35  christos #endif
    645   1.1       cgd 
    646  1.35  christos 	msgvec = salloc((msgCount + 1) * sizeof *msgvec);
    647  1.31  christos 	if (msgvec == NULL)
    648   1.1       cgd 		return(0);
    649   1.1       cgd 	if (getmsglist(ms, msgvec, 0) < 0)
    650   1.1       cgd 		return(0);
    651   1.1       cgd 	if (*msgvec == 0) {
    652   1.1       cgd 		*msgvec = first(0, MMNORM);
    653   1.7        pk 		if (*msgvec == 0) {
    654  1.32  christos 			(void)printf("No appropriate messages\n");
    655   1.1       cgd 			return(0);
    656   1.1       cgd 		}
    657   1.7        pk 		msgvec[1] = 0;
    658   1.1       cgd 	}
    659   1.1       cgd 	if (f == 'f' || f == 'F')
    660  1.24       wiz 		tabst = NULL;
    661  1.24       wiz 	else if ((tabst = value("indentprefix")) == NULL)
    662   1.1       cgd 		tabst = "\t";
    663   1.1       cgd 	ig = isupper(f) ? NULL : ignore;
    664  1.32  christos 	(void)printf("Interpolating:");
    665   1.1       cgd 	for (; *msgvec != 0; msgvec++) {
    666   1.1       cgd 		struct message *mp = message + *msgvec - 1;
    667   1.1       cgd 
    668   1.1       cgd 		touch(mp);
    669  1.32  christos 		(void)printf(" %d", *msgvec);
    670  1.35  christos #ifdef MIME_SUPPORT
    671  1.35  christos 		(void)fflush(stdout);	/* flush stdout and the above */
    672  1.35  christos 		mip = NULL;
    673  1.35  christos 		if (value(ENAME_MIME_DECODE_MSG)) {
    674  1.35  christos 			if ((tabst == NULL && value(ENAME_MIME_DECODE_INSERT)) ||
    675  1.35  christos 			    (tabst != NULL && value(ENAME_MIME_DECODE_QUOTE)))
    676  1.35  christos 				mip = mime_decode_open(mp);
    677  1.35  christos 		}
    678  1.35  christos 		retval = mime_sendmessage(mp, fp, ig, tabst, mip);
    679  1.35  christos 		mime_decode_close(mip);
    680  1.35  christos 		if (retval < 0) {
    681  1.35  christos #else
    682  1.20      tron 		if (sendmessage(mp, fp, ig, tabst) < 0) {
    683  1.35  christos #endif
    684  1.28       wiz 			warn("%s", fn);
    685   1.1       cgd 			return(-1);
    686   1.1       cgd 		}
    687   1.1       cgd 	}
    688  1.32  christos 	(void)printf("\n");
    689   1.1       cgd 	return(0);
    690   1.1       cgd }
    691   1.1       cgd 
    692   1.1       cgd /*
    693   1.1       cgd  * Print (continue) when continued after ^Z.
    694   1.1       cgd  */
    695   1.1       cgd /*ARGSUSED*/
    696   1.1       cgd void
    697  1.22       wiz collstop(int s)
    698   1.1       cgd {
    699   1.1       cgd 	sig_t old_action = signal(s, SIG_DFL);
    700   1.6  christos 	sigset_t nset;
    701   1.1       cgd 
    702  1.32  christos 	(void)sigemptyset(&nset);
    703  1.32  christos 	(void)sigaddset(&nset, s);
    704  1.32  christos 	(void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
    705  1.32  christos 	(void)kill(0, s);
    706  1.32  christos 	(void)sigprocmask(SIG_BLOCK, &nset, NULL);
    707  1.32  christos 	(void)signal(s, old_action);
    708   1.1       cgd 	if (colljmp_p) {
    709   1.1       cgd 		colljmp_p = 0;
    710   1.1       cgd 		hadintr = 0;
    711   1.1       cgd 		longjmp(colljmp, 1);
    712   1.1       cgd 	}
    713   1.1       cgd }
    714   1.1       cgd 
    715   1.1       cgd /*
    716   1.1       cgd  * On interrupt, come here to save the partial message in ~/dead.letter.
    717   1.1       cgd  * Then jump out of the collection loop.
    718   1.1       cgd  */
    719   1.1       cgd /*ARGSUSED*/
    720   1.1       cgd void
    721  1.35  christos collint(int s __unused)
    722   1.1       cgd {
    723   1.1       cgd 	/*
    724   1.1       cgd 	 * the control flow is subtle, because we can be called from ~q.
    725   1.1       cgd 	 */
    726   1.1       cgd 	if (!hadintr) {
    727  1.24       wiz 		if (value("ignore") != NULL) {
    728  1.32  christos 			(void)puts("@");
    729  1.32  christos 			(void)fflush(stdout);
    730   1.1       cgd 			clearerr(stdin);
    731   1.1       cgd 			return;
    732   1.1       cgd 		}
    733   1.1       cgd 		hadintr = 1;
    734   1.1       cgd 		longjmp(colljmp, 1);
    735   1.1       cgd 	}
    736   1.1       cgd 	rewind(collf);
    737  1.24       wiz 	if (value("nosave") == NULL)
    738   1.1       cgd 		savedeadletter(collf);
    739   1.1       cgd 	longjmp(collabort, 1);
    740   1.1       cgd }
    741   1.1       cgd 
    742   1.1       cgd /*ARGSUSED*/
    743   1.1       cgd void
    744  1.35  christos collhup(int s __unused)
    745   1.1       cgd {
    746   1.1       cgd 	rewind(collf);
    747   1.1       cgd 	savedeadletter(collf);
    748   1.1       cgd 	/*
    749   1.1       cgd 	 * Let's pretend nobody else wants to clean up,
    750   1.1       cgd 	 * a true statement at this time.
    751   1.1       cgd 	 */
    752   1.1       cgd 	exit(1);
    753   1.1       cgd }
    754   1.1       cgd 
    755   1.3   deraadt void
    756  1.22       wiz savedeadletter(FILE *fp)
    757   1.1       cgd {
    758  1.11     lukem 	FILE *dbuf;
    759  1.32  christos 	mode_t m;
    760  1.11     lukem 	int c;
    761  1.31  christos 	const char *cp;
    762   1.1       cgd 
    763   1.1       cgd 	if (fsize(fp) == 0)
    764   1.1       cgd 		return;
    765   1.1       cgd 	cp = getdeadletter();
    766  1.32  christos 	m = umask(077);
    767   1.1       cgd 	dbuf = Fopen(cp, "a");
    768  1.32  christos 	(void)umask(m);
    769   1.1       cgd 	if (dbuf == NULL)
    770   1.1       cgd 		return;
    771   1.1       cgd 	while ((c = getc(fp)) != EOF)
    772  1.25       wiz 		(void)putc(c, dbuf);
    773  1.32  christos 	(void)Fclose(dbuf);
    774   1.1       cgd 	rewind(fp);
    775   1.1       cgd }
    776