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