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