Home | History | Annotate | Line # | Download | only in mail
collect.c revision 1.40
      1  1.40  christos /*	$NetBSD: collect.c,v 1.40 2007/10/23 14:58:43 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.40  christos __RCSID("$NetBSD: collect.c,v 1.40 2007/10/23 14:58:43 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.40  christos 	for (/*EMPTY*/; *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 	(void)printf("\n");
    267  1.37  christos 	return 0;
    268  1.37  christos }
    269  1.37  christos 
    270  1.37  christos /*
    271  1.37  christos  * Print (continue) when continued after ^Z.
    272  1.37  christos  */
    273  1.37  christos /*ARGSUSED*/
    274  1.37  christos static void
    275  1.37  christos collstop(int s)
    276  1.37  christos {
    277  1.37  christos 	sig_t old_action = signal(s, SIG_DFL);
    278  1.37  christos 	sigset_t nset;
    279  1.37  christos 
    280  1.37  christos 	(void)sigemptyset(&nset);
    281  1.37  christos 	(void)sigaddset(&nset, s);
    282  1.37  christos 	(void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
    283  1.37  christos 	(void)kill(0, s);
    284  1.37  christos 	(void)sigprocmask(SIG_BLOCK, &nset, NULL);
    285  1.37  christos 	(void)signal(s, old_action);
    286  1.37  christos 	if (colljmp_p) {
    287  1.37  christos 		colljmp_p = 0;
    288  1.37  christos 		hadintr = 0;
    289  1.37  christos 		longjmp(colljmp, 1);
    290  1.37  christos 	}
    291  1.37  christos }
    292  1.37  christos 
    293  1.37  christos /*
    294  1.37  christos  * Append the contents of the file to the end of the deadletter file.
    295  1.37  christos  */
    296  1.37  christos PUBLIC void
    297  1.37  christos savedeadletter(FILE *fp)
    298  1.37  christos {
    299  1.37  christos 	FILE *dbuf;
    300  1.37  christos 	mode_t m;
    301  1.37  christos 	int c;
    302  1.37  christos 	const char *cp;
    303  1.37  christos 
    304  1.37  christos 	if (fsize(fp) == 0)
    305  1.37  christos 		return;
    306  1.37  christos 	cp = getdeadletter();
    307  1.37  christos 	m = umask(077);
    308  1.37  christos 	dbuf = Fopen(cp, "a");
    309  1.37  christos 	(void)umask(m);
    310  1.37  christos 	if (dbuf == NULL)
    311  1.37  christos 		return;
    312  1.37  christos 	while ((c = getc(fp)) != EOF)
    313  1.37  christos 		(void)putc(c, dbuf);
    314  1.37  christos 	(void)Fclose(dbuf);
    315  1.37  christos 	rewind(fp);
    316  1.37  christos }
    317  1.37  christos 
    318  1.37  christos /*
    319  1.37  christos  * On interrupt, come here to save the partial message in ~/dead.letter.
    320  1.37  christos  * Then jump out of the collection loop.
    321  1.37  christos  */
    322  1.37  christos /*ARGSUSED*/
    323  1.37  christos static void
    324  1.37  christos collint(int s __unused)
    325  1.37  christos {
    326  1.37  christos 	/*
    327  1.37  christos 	 * the control flow is subtle, because we can be called from ~q.
    328  1.37  christos 	 */
    329  1.37  christos 	if (!hadintr) {
    330  1.37  christos 		if (value(ENAME_IGNORE) != NULL) {
    331  1.37  christos 			(void)puts("@");
    332  1.37  christos 			(void)fflush(stdout);
    333  1.37  christos 			clearerr(stdin);
    334  1.37  christos 			return;
    335  1.37  christos 		}
    336  1.37  christos 		hadintr = 1;
    337  1.37  christos 		longjmp(colljmp, 1);
    338  1.37  christos 	}
    339  1.37  christos 	rewind(collf);
    340  1.37  christos 	if (value(ENAME_NOSAVE) == NULL)
    341  1.37  christos 		savedeadletter(collf);
    342  1.37  christos 	longjmp(collabort, 1);
    343  1.37  christos }
    344  1.37  christos 
    345  1.37  christos /*ARGSUSED*/
    346  1.37  christos static void
    347  1.37  christos collhup(int s __unused)
    348  1.37  christos {
    349  1.37  christos 	rewind(collf);
    350  1.37  christos 	savedeadletter(collf);
    351  1.37  christos 	/*
    352  1.37  christos 	 * Let's pretend nobody else wants to clean up,
    353  1.37  christos 	 * a true statement at this time.
    354  1.37  christos 	 */
    355  1.37  christos 	exit(1);
    356  1.37  christos }
    357  1.37  christos 
    358  1.37  christos PUBLIC FILE *
    359  1.22       wiz collect(struct header *hp, int printheaders)
    360   1.1       cgd {
    361   1.1       cgd 	FILE *fbuf;
    362  1.34  christos 	int lc, cc;
    363  1.27       wiz 	int c, fd, t;
    364  1.31  christos 	char linebuf[LINESIZE];
    365  1.31  christos 	const char *cp;
    366  1.27       wiz 	char tempname[PATHSIZE];
    367  1.28       wiz 	char mailtempname[PATHSIZE];
    368  1.34  christos 	int lastlong, rc;	/* So we don't make 2 or more lines
    369   1.8      phil 					   out of a long input line. */
    370  1.35  christos 	int eofcount;
    371  1.35  christos 	int longline;
    372  1.35  christos 	sigset_t  nset;
    373  1.34  christos 
    374  1.34  christos 	/* The following are declared volatile to avoid longjmp clobbering. */
    375  1.35  christos 	char volatile getsub;
    376  1.35  christos 	int volatile escape;
    377   1.1       cgd 
    378  1.32  christos 	(void)memset(mailtempname, 0, sizeof(mailtempname));
    379   1.1       cgd 	collf = NULL;
    380   1.1       cgd 	/*
    381   1.1       cgd 	 * Start catching signals from here, but we're still die on interrupts
    382   1.1       cgd 	 * until we're in the main loop.
    383   1.1       cgd 	 */
    384  1.35  christos 	(void)sigemptyset(&nset);
    385  1.35  christos 	(void)sigaddset(&nset, SIGINT);
    386  1.35  christos 	(void)sigaddset(&nset, SIGHUP);
    387  1.35  christos 	(void)sigprocmask(SIG_BLOCK, &nset, NULL);
    388   1.1       cgd 	if ((saveint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
    389  1.32  christos 		(void)signal(SIGINT, collint);
    390   1.1       cgd 	if ((savehup = signal(SIGHUP, SIG_IGN)) != SIG_IGN)
    391  1.32  christos 		(void)signal(SIGHUP, collhup);
    392   1.1       cgd 	savetstp = signal(SIGTSTP, collstop);
    393   1.1       cgd 	savettou = signal(SIGTTOU, collstop);
    394   1.1       cgd 	savettin = signal(SIGTTIN, collstop);
    395   1.1       cgd 	if (setjmp(collabort) || setjmp(colljmp)) {
    396  1.28       wiz 		(void)rm(mailtempname);
    397   1.1       cgd 		goto err;
    398   1.1       cgd 	}
    399  1.35  christos 	(void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
    400   1.1       cgd 
    401   1.1       cgd 	noreset++;
    402  1.28       wiz 	(void)snprintf(mailtempname, sizeof(mailtempname),
    403  1.28       wiz 	    "%s/mail.RsXXXXXXXXXX", tmpdir);
    404  1.28       wiz 	if ((fd = mkstemp(mailtempname)) == -1 ||
    405  1.28       wiz 	    (collf = Fdopen(fd, "w+")) == NULL) {
    406  1.28       wiz 		if (fd != -1)
    407  1.32  christos 			(void)close(fd);
    408  1.28       wiz 		warn("%s", mailtempname);
    409   1.1       cgd 		goto err;
    410   1.1       cgd 	}
    411  1.28       wiz 	(void)rm(mailtempname);
    412   1.1       cgd 
    413   1.1       cgd 	/*
    414   1.1       cgd 	 * If we are going to prompt for a subject,
    415   1.1       cgd 	 * refrain from printing a newline after
    416   1.1       cgd 	 * the headers (since some people mind).
    417   1.1       cgd 	 */
    418  1.33  christos 	t = GTO|GSUBJECT|GCC|GNL|GSMOPTS;
    419   1.1       cgd 	getsub = 0;
    420  1.37  christos 	if (hp->h_subject == NULL && value(ENAME_INTERACTIVE) != NULL &&
    421  1.37  christos 	    (value(ENAME_ASK) != NULL || value(ENAME_ASKSUB) != NULL))
    422   1.1       cgd 		t &= ~GNL, getsub++;
    423   1.1       cgd 	if (printheaders) {
    424  1.32  christos 		(void)puthead(hp, stdout, t);
    425  1.32  christos 		(void)fflush(stdout);
    426   1.1       cgd 	}
    427  1.37  christos 	if ((cp = value(ENAME_ESCAPE)) != NULL)
    428   1.1       cgd 		escape = *cp;
    429   1.1       cgd 	else
    430   1.1       cgd 		escape = ESCAPE;
    431  1.35  christos 	hadintr = 0;	/* static - no longjmp problem */
    432   1.1       cgd 	if (!setjmp(colljmp)) {
    433   1.1       cgd 		if (getsub)
    434  1.32  christos 			(void)grabh(hp, GSUBJECT);
    435   1.1       cgd 	} else {
    436   1.1       cgd 		/*
    437   1.1       cgd 		 * Come here for printing the after-signal message.
    438   1.1       cgd 		 * Duplicate messages won't be printed because
    439   1.1       cgd 		 * the write is aborted if we get a SIGTTOU.
    440   1.1       cgd 		 */
    441   1.1       cgd cont:
    442   1.1       cgd 		if (hadintr) {
    443  1.32  christos 			(void)fflush(stdout);
    444  1.32  christos 			(void)fprintf(stderr,
    445   1.1       cgd 			"\n(Interrupt -- one more to kill letter)\n");
    446   1.1       cgd 		} else {
    447  1.32  christos 			(void)printf("(continue)\n");
    448  1.32  christos 			(void)fflush(stdout);
    449   1.1       cgd 		}
    450   1.1       cgd 	}
    451  1.35  christos 	eofcount = 0;	/* reset after possible longjmp */
    452  1.35  christos 	longline = 0;	/* reset after possible longjmp */
    453   1.1       cgd 	for (;;) {
    454   1.1       cgd 		colljmp_p = 1;
    455  1.35  christos 		c = mail_readline(stdin, linebuf, LINESIZE);
    456   1.1       cgd 		colljmp_p = 0;
    457  1.35  christos #ifdef USE_EDITLINE
    458  1.33  christos 		if (c < 0) {
    459  1.33  christos 			char *p;
    460  1.37  christos 			if (value(ENAME_INTERACTIVE) != NULL &&
    461  1.37  christos 			    (p = value(ENAME_IGNOREEOF)) != NULL &&
    462  1.33  christos 			    ++eofcount < (*p == 0 ? 25 : atoi(p))) {
    463  1.33  christos 				(void)printf("Use \".\" to terminate letter\n");
    464  1.33  christos 				continue;
    465  1.33  christos 			}
    466  1.33  christos 			break;
    467  1.33  christos 		}
    468  1.33  christos #else
    469   1.1       cgd 		if (c < 0) {
    470  1.37  christos 			if (value(ENAME_INTERACTIVE) != NULL &&
    471  1.37  christos 			    value(ENAME_IGNOREEOF) != NULL && ++eofcount < 25) {
    472  1.32  christos 				(void)printf("Use \".\" to terminate letter\n");
    473   1.1       cgd 				continue;
    474   1.1       cgd 			}
    475   1.1       cgd 			break;
    476   1.1       cgd 		}
    477  1.33  christos #endif
    478   1.8      phil 		lastlong = longline;
    479   1.8      phil 		longline = c == LINESIZE-1;
    480   1.1       cgd 		eofcount = 0;
    481   1.1       cgd 		hadintr = 0;
    482   1.1       cgd 		if (linebuf[0] == '.' && linebuf[1] == '\0' &&
    483  1.37  christos 		    value(ENAME_INTERACTIVE) != NULL && !lastlong &&
    484  1.37  christos 		    (value(ENAME_DOT) != NULL || value(ENAME_IGNOREEOF) != NULL))
    485   1.1       cgd 			break;
    486  1.37  christos 		if (linebuf[0] != escape || value(ENAME_INTERACTIVE) == NULL ||
    487   1.8      phil 		    lastlong) {
    488   1.8      phil 			if (putline(collf, linebuf, !longline) < 0)
    489   1.1       cgd 				goto err;
    490   1.1       cgd 			continue;
    491   1.1       cgd 		}
    492   1.1       cgd 		c = linebuf[1];
    493   1.1       cgd 		switch (c) {
    494   1.1       cgd 		default:
    495   1.1       cgd 			/*
    496   1.1       cgd 			 * On double escape, just send the single one.
    497   1.1       cgd 			 * Otherwise, it's an error.
    498   1.1       cgd 			 */
    499   1.1       cgd 			if (c == escape) {
    500   1.8      phil 				if (putline(collf, &linebuf[1], !longline) < 0)
    501   1.1       cgd 					goto err;
    502   1.1       cgd 				else
    503   1.1       cgd 					break;
    504   1.1       cgd 			}
    505  1.32  christos 			(void)printf("Unknown tilde escape.\n");
    506   1.1       cgd 			break;
    507  1.35  christos #ifdef MIME_SUPPORT
    508  1.35  christos 		case '@':
    509  1.36  christos 			hp->h_attach = mime_attach_files(hp->h_attach, &linebuf[2]);
    510  1.35  christos 			break;
    511  1.35  christos #endif
    512   1.1       cgd 		case 'C':
    513   1.1       cgd 			/*
    514   1.1       cgd 			 * Dump core.
    515   1.1       cgd 			 */
    516  1.32  christos 			(void)core(NULL);
    517   1.1       cgd 			break;
    518   1.1       cgd 		case '!':
    519   1.1       cgd 			/*
    520   1.1       cgd 			 * Shell escape, send the balance of the
    521   1.1       cgd 			 * line to sh -c.
    522   1.1       cgd 			 */
    523  1.32  christos 			(void)shell(&linebuf[2]);
    524   1.1       cgd 			break;
    525   1.1       cgd 		case ':':
    526   1.5       jtc 		case '_':
    527   1.1       cgd 			/*
    528   1.1       cgd 			 * Escape to command mode, but be nice!
    529   1.1       cgd 			 */
    530  1.38  christos 			(void)execute(&linebuf[2], ec_composing);
    531   1.1       cgd 			goto cont;
    532   1.1       cgd 		case '.':
    533   1.1       cgd 			/*
    534   1.1       cgd 			 * Simulate end of file on input.
    535   1.1       cgd 			 */
    536   1.1       cgd 			goto out;
    537   1.1       cgd 		case 'q':
    538   1.1       cgd 			/*
    539   1.1       cgd 			 * Force a quit of sending mail.
    540   1.1       cgd 			 * Act like an interrupt happened.
    541   1.1       cgd 			 */
    542   1.1       cgd 			hadintr++;
    543   1.1       cgd 			collint(SIGINT);
    544   1.1       cgd 			exit(1);
    545  1.32  christos 			/*NOTREACHED*/
    546  1.19       mjl 
    547  1.19       mjl 		case 'x':	/* exit, do not save in dead.letter */
    548  1.19       mjl 			goto err;
    549  1.19       mjl 
    550   1.1       cgd 		case 'h':
    551   1.1       cgd 			/*
    552   1.1       cgd 			 * Grab a bunch of headers.
    553   1.1       cgd 			 */
    554  1.33  christos 			(void)grabh(hp, GTO|GSUBJECT|GCC|GBCC|GSMOPTS);
    555   1.1       cgd 			goto cont;
    556   1.1       cgd 		case 't':
    557   1.1       cgd 			/*
    558   1.1       cgd 			 * Add to the To list.
    559   1.1       cgd 			 */
    560   1.1       cgd 			hp->h_to = cat(hp->h_to, extract(&linebuf[2], GTO));
    561   1.1       cgd 			break;
    562   1.1       cgd 		case 's':
    563   1.1       cgd 			/*
    564   1.1       cgd 			 * Set the Subject list.
    565   1.1       cgd 			 */
    566  1.40  christos 			cp = skip_WSP(&linebuf[2]);
    567   1.1       cgd 			hp->h_subject = savestr(cp);
    568   1.1       cgd 			break;
    569   1.1       cgd 		case 'c':
    570   1.1       cgd 			/*
    571   1.1       cgd 			 * Add to the CC list.
    572   1.1       cgd 			 */
    573   1.1       cgd 			hp->h_cc = cat(hp->h_cc, extract(&linebuf[2], GCC));
    574   1.1       cgd 			break;
    575   1.1       cgd 		case 'b':
    576   1.1       cgd 			/*
    577   1.1       cgd 			 * Add stuff to blind carbon copies list.
    578   1.1       cgd 			 */
    579   1.1       cgd 			hp->h_bcc = cat(hp->h_bcc, extract(&linebuf[2], GBCC));
    580   1.1       cgd 			break;
    581  1.19       mjl 		case 'i':
    582  1.19       mjl 		case 'A':
    583  1.19       mjl 		case 'a':
    584  1.19       mjl 			/*
    585  1.19       mjl 			 * Insert named variable in message
    586  1.19       mjl 			 */
    587  1.19       mjl 
    588  1.19       mjl 			switch(c) {
    589  1.40  christos 				case 'i':
    590  1.40  christos 					cp = skip_WSP(&linebuf[2]);
    591  1.19       mjl 					break;
    592  1.19       mjl 				case 'a':
    593  1.19       mjl 					cp = "sign";
    594  1.19       mjl 					break;
    595  1.19       mjl 				case 'A':
    596  1.19       mjl 					cp = "Sign";
    597  1.19       mjl 					break;
    598  1.19       mjl 				default:
    599  1.19       mjl 					goto err;
    600  1.19       mjl 			}
    601  1.19       mjl 
    602  1.37  christos 			if (*cp && (cp = value(cp)) != NULL) {
    603  1.40  christos 				(void)printf("%s\n", cp);
    604  1.37  christos 				if (putline(collf, cp, 1) < 0)
    605  1.19       mjl 					goto err;
    606  1.19       mjl 			}
    607  1.19       mjl 
    608  1.19       mjl 			break;
    609  1.19       mjl 
    610   1.1       cgd 		case 'd':
    611  1.32  christos 			(void)strcpy(linebuf + 2, getdeadletter());
    612  1.32  christos 			/* FALLTHROUGH */
    613   1.1       cgd 		case 'r':
    614   1.5       jtc 		case '<':
    615   1.1       cgd 			/*
    616   1.1       cgd 			 * Invoke a file:
    617   1.1       cgd 			 * Search for the file name,
    618   1.1       cgd 			 * then open it and copy the contents to collf.
    619   1.1       cgd 			 */
    620  1.40  christos 			cp = skip_WSP(&linebuf[2]);
    621   1.1       cgd 			if (*cp == '\0') {
    622  1.32  christos 				(void)printf("Interpolate what file?\n");
    623   1.1       cgd 				break;
    624   1.1       cgd 			}
    625  1.19       mjl 
    626   1.1       cgd 			cp = expand(cp);
    627  1.24       wiz 			if (cp == NULL)
    628   1.1       cgd 				break;
    629  1.19       mjl 
    630  1.19       mjl 			if (*cp == '!') {	/* insert stdout of command */
    631  1.31  christos 				const char *shellcmd;
    632  1.19       mjl 				int nullfd;
    633  1.23       wiz 				int rc2;
    634  1.19       mjl 
    635  1.37  christos 				if ((nullfd = open("/dev/null", O_RDONLY, 0)) == -1) {
    636  1.26       wiz 					warn("/dev/null");
    637  1.19       mjl 					break;
    638  1.19       mjl 				}
    639  1.19       mjl 
    640  1.27       wiz 				(void)snprintf(tempname, sizeof(tempname),
    641  1.28       wiz 				    "%s/mail.ReXXXXXXXXXX", tmpdir);
    642  1.27       wiz 				if ((fd = mkstemp(tempname)) == -1 ||
    643  1.27       wiz 				    (fbuf = Fdopen(fd, "w+")) == NULL) {
    644  1.27       wiz 					if (fd != -1)
    645  1.32  christos 						(void)close(fd);
    646  1.27       wiz 					warn("%s", tempname);
    647  1.19       mjl 					break;
    648  1.19       mjl 				}
    649  1.27       wiz 				(void)unlink(tempname);
    650  1.19       mjl 
    651  1.37  christos 				if ((shellcmd = value(ENAME_SHELL)) == NULL)
    652  1.23       wiz 					shellcmd = _PATH_CSHELL;
    653  1.19       mjl 
    654  1.35  christos 				rc2 = run_command(shellcmd, 0, nullfd, fileno(fbuf), "-c", cp + 1, NULL);
    655  1.19       mjl 
    656  1.32  christos 				(void)close(nullfd);
    657  1.19       mjl 
    658  1.23       wiz 				if (rc2 < 0) {
    659  1.25       wiz 					(void)Fclose(fbuf);
    660  1.19       mjl 					break;
    661  1.19       mjl 				}
    662  1.19       mjl 
    663  1.19       mjl 				if (fsize(fbuf) == 0) {
    664  1.35  christos 					(void)fprintf(stderr, "No bytes from command \"%s\"\n", cp + 1);
    665  1.25       wiz 					(void)Fclose(fbuf);
    666  1.19       mjl 					break;
    667  1.19       mjl 				}
    668  1.19       mjl 
    669  1.19       mjl 				rewind(fbuf);
    670  1.19       mjl 			}
    671  1.19       mjl 			else if (isdir(cp)) {
    672  1.32  christos 				(void)printf("%s: Directory\n", cp);
    673   1.1       cgd 				break;
    674   1.1       cgd 			}
    675  1.19       mjl 			else if ((fbuf = Fopen(cp, "r")) == NULL) {
    676  1.26       wiz 				warn("%s", cp);
    677   1.1       cgd 				break;
    678   1.1       cgd 			}
    679  1.32  christos 			(void)printf("\"%s\" ", cp);
    680  1.32  christos 			(void)fflush(stdout);
    681   1.1       cgd 			lc = 0;
    682   1.1       cgd 			cc = 0;
    683  1.35  christos 			while ((rc = mail_readline(fbuf, linebuf, LINESIZE)) >= 0) {
    684   1.8      phil 				if (rc != LINESIZE-1) lc++;
    685   1.8      phil 				if ((t = putline(collf, linebuf,
    686   1.8      phil 						 rc != LINESIZE-1)) < 0) {
    687  1.32  christos 					(void)Fclose(fbuf);
    688   1.1       cgd 					goto err;
    689   1.1       cgd 				}
    690   1.1       cgd 				cc += t;
    691   1.1       cgd 			}
    692  1.32  christos 			(void)Fclose(fbuf);
    693  1.32  christos 			(void)printf("%d/%d\n", lc, cc);
    694   1.1       cgd 			break;
    695   1.1       cgd 		case 'w':
    696   1.1       cgd 			/*
    697   1.1       cgd 			 * Write the message on a file.
    698   1.1       cgd 			 */
    699  1.40  christos 			cp = skip_WSP(&linebuf[2]);
    700   1.1       cgd 			if (*cp == '\0') {
    701  1.32  christos 				(void)fprintf(stderr, "Write what file!?\n");
    702   1.1       cgd 				break;
    703   1.1       cgd 			}
    704  1.24       wiz 			if ((cp = expand(cp)) == NULL)
    705   1.1       cgd 				break;
    706   1.1       cgd 			rewind(collf);
    707  1.32  christos 			(void)exwrite(cp, collf, 1);
    708   1.1       cgd 			break;
    709   1.1       cgd 		case 'm':
    710   1.1       cgd 		case 'M':
    711   1.1       cgd 		case 'f':
    712   1.1       cgd 		case 'F':
    713   1.1       cgd 			/*
    714   1.1       cgd 			 * Interpolate the named messages, if we
    715   1.1       cgd 			 * are in receiving mail mode.  Does the
    716   1.1       cgd 			 * standard list processing garbage.
    717   1.1       cgd 			 * If ~f is given, we don't shift over.
    718   1.1       cgd 			 */
    719  1.28       wiz 			if (forward(linebuf + 2, collf, mailtempname, c) < 0)
    720   1.1       cgd 				goto err;
    721   1.1       cgd 			goto cont;
    722   1.1       cgd 		case '?':
    723  1.39  christos 			cathelp(_PATH_TILDE);
    724   1.1       cgd 			break;
    725   1.1       cgd 		case 'p':
    726   1.1       cgd 			/*
    727   1.1       cgd 			 * Print out the current state of the
    728   1.1       cgd 			 * message without altering anything.
    729   1.1       cgd 			 */
    730   1.1       cgd 			rewind(collf);
    731  1.32  christos 			(void)printf("-------\nMessage contains:\n");
    732  1.32  christos 			(void)puthead(hp, stdout, GTO|GSUBJECT|GCC|GBCC|GNL);
    733   1.1       cgd 			while ((t = getc(collf)) != EOF)
    734  1.25       wiz 				(void)putchar(t);
    735   1.1       cgd 			goto cont;
    736   1.1       cgd 		case '|':
    737   1.1       cgd 			/*
    738   1.1       cgd 			 * Pipe message through command.
    739   1.1       cgd 			 * Collect output as new message.
    740   1.1       cgd 			 */
    741   1.1       cgd 			rewind(collf);
    742   1.1       cgd 			mespipe(collf, &linebuf[2]);
    743   1.1       cgd 			goto cont;
    744   1.1       cgd 		case 'v':
    745   1.1       cgd 		case 'e':
    746   1.1       cgd 			/*
    747   1.1       cgd 			 * Edit the current message.
    748   1.1       cgd 			 * 'e' means to use EDITOR
    749   1.1       cgd 			 * 'v' means to use VISUAL
    750   1.1       cgd 			 */
    751   1.1       cgd 			rewind(collf);
    752   1.1       cgd 			mesedit(collf, c);
    753   1.1       cgd 			goto cont;
    754   1.1       cgd 		}
    755   1.1       cgd 	}
    756   1.1       cgd 	goto out;
    757   1.1       cgd err:
    758   1.1       cgd 	if (collf != NULL) {
    759  1.32  christos 		(void)Fclose(collf);
    760   1.1       cgd 		collf = NULL;
    761   1.1       cgd 	}
    762   1.1       cgd out:
    763   1.1       cgd 	if (collf != NULL)
    764   1.1       cgd 		rewind(collf);
    765   1.1       cgd 	noreset--;
    766  1.35  christos 	(void)sigprocmask(SIG_BLOCK, &nset, NULL);
    767  1.32  christos 	(void)signal(SIGINT, saveint);
    768  1.32  christos 	(void)signal(SIGHUP, savehup);
    769  1.32  christos 	(void)signal(SIGTSTP, savetstp);
    770  1.32  christos 	(void)signal(SIGTTOU, savettou);
    771  1.32  christos 	(void)signal(SIGTTIN, savettin);
    772  1.35  christos 	(void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
    773   1.1       cgd 	return collf;
    774   1.1       cgd }
    775