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