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