2017-11-20 19:49:47 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
1994-05-27 12:33:43 +00:00
|
|
|
* Copyright (c) 1980, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2017-02-28 23:42:47 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-27 12:33:43 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
2001-03-25 04:57:05 +00:00
|
|
|
#if 0
|
1994-05-27 12:33:43 +00:00
|
|
|
static char sccsid[] = "@(#)collect.c 8.2 (Berkeley) 4/19/94";
|
2001-03-25 04:57:05 +00:00
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
#endif /* not lint */
|
2002-06-30 05:25:07 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Mail -- a mail program
|
|
|
|
*
|
|
|
|
* Collect input from standard input, handling
|
|
|
|
* ~ escapes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "rcv.h"
|
2001-12-22 22:16:48 +00:00
|
|
|
#include <fcntl.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include "extern.h"
|
|
|
|
|
|
|
|
/*
|
2016-07-11 00:03:39 +00:00
|
|
|
* Read a message from standard input and return a read file to it
|
1994-05-27 12:33:43 +00:00
|
|
|
* or NULL on error.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The following hokiness with global variables is so that on
|
|
|
|
* receipt of an interrupt signal, the partial message can be salted
|
|
|
|
* away on dead.letter.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static sig_t saveint; /* Previous SIGINT value */
|
|
|
|
static sig_t savehup; /* Previous SIGHUP value */
|
|
|
|
static sig_t savetstp; /* Previous SIGTSTP value */
|
|
|
|
static sig_t savettou; /* Previous SIGTTOU value */
|
|
|
|
static sig_t savettin; /* Previous SIGTTIN value */
|
|
|
|
static FILE *collf; /* File for saving away */
|
|
|
|
static int hadintr; /* Have seen one SIGINT so far */
|
|
|
|
|
|
|
|
static jmp_buf colljmp; /* To get back to work */
|
|
|
|
static int colljmp_p; /* whether to long jump */
|
|
|
|
static jmp_buf collabort; /* To end collection with error */
|
|
|
|
|
|
|
|
FILE *
|
2010-12-19 16:25:23 +00:00
|
|
|
collect(struct header *hp, int printheaders)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
FILE *fbuf;
|
2001-05-27 20:26:22 +00:00
|
|
|
int lc, cc, escape, eofcount, fd, c, t;
|
|
|
|
char linebuf[LINESIZE], tempname[PATHSIZE], *cp, getsub;
|
2001-12-18 20:52:09 +00:00
|
|
|
sigset_t nset;
|
|
|
|
int longline, lastlong, rc; /* So we don't make 2 or more lines
|
|
|
|
out of a long input line. */
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
collf = NULL;
|
|
|
|
/*
|
|
|
|
* Start catching signals from here, but we're still die on interrupts
|
|
|
|
* until we're in the main loop.
|
|
|
|
*/
|
2001-12-18 20:52:09 +00:00
|
|
|
(void)sigemptyset(&nset);
|
|
|
|
(void)sigaddset(&nset, SIGINT);
|
|
|
|
(void)sigaddset(&nset, SIGHUP);
|
|
|
|
(void)sigprocmask(SIG_BLOCK, &nset, NULL);
|
1994-05-27 12:33:43 +00:00
|
|
|
if ((saveint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)signal(SIGINT, collint);
|
1994-05-27 12:33:43 +00:00
|
|
|
if ((savehup = signal(SIGHUP, SIG_IGN)) != SIG_IGN)
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)signal(SIGHUP, collhup);
|
1994-05-27 12:33:43 +00:00
|
|
|
savetstp = signal(SIGTSTP, collstop);
|
|
|
|
savettou = signal(SIGTTOU, collstop);
|
|
|
|
savettin = signal(SIGTTIN, collstop);
|
|
|
|
if (setjmp(collabort) || setjmp(colljmp)) {
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)rm(tempname);
|
1994-05-27 12:33:43 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2001-12-18 20:52:09 +00:00
|
|
|
(void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
noreset++;
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)snprintf(tempname, sizeof(tempname),
|
|
|
|
"%s/mail.RsXXXXXXXXXX", tmpdir);
|
2001-03-25 04:57:05 +00:00
|
|
|
if ((fd = mkstemp(tempname)) == -1 ||
|
|
|
|
(collf = Fdopen(fd, "w+")) == NULL) {
|
|
|
|
warn("%s", tempname);
|
1994-05-27 12:33:43 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)rm(tempname);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we are going to prompt for a subject,
|
|
|
|
* refrain from printing a newline after
|
|
|
|
* the headers (since some people mind).
|
|
|
|
*/
|
|
|
|
t = GTO|GSUBJECT|GCC|GNL;
|
|
|
|
getsub = 0;
|
2001-05-27 20:26:22 +00:00
|
|
|
if (hp->h_subject == NULL && value("interactive") != NULL &&
|
|
|
|
(value("ask") != NULL || value("asksub") != NULL))
|
1994-05-27 12:33:43 +00:00
|
|
|
t &= ~GNL, getsub++;
|
|
|
|
if (printheaders) {
|
|
|
|
puthead(hp, stdout, t);
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)fflush(stdout);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2001-05-27 20:26:22 +00:00
|
|
|
if ((cp = value("escape")) != NULL)
|
1994-05-27 12:33:43 +00:00
|
|
|
escape = *cp;
|
|
|
|
else
|
|
|
|
escape = ESCAPE;
|
|
|
|
eofcount = 0;
|
|
|
|
hadintr = 0;
|
2001-12-18 20:52:09 +00:00
|
|
|
longline = 0;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if (!setjmp(colljmp)) {
|
|
|
|
if (getsub)
|
|
|
|
grabh(hp, GSUBJECT);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Come here for printing the after-signal message.
|
|
|
|
* Duplicate messages won't be printed because
|
|
|
|
* the write is aborted if we get a SIGTTOU.
|
|
|
|
*/
|
|
|
|
cont:
|
|
|
|
if (hadintr) {
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)fflush(stdout);
|
1994-05-27 12:33:43 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"\n(Interrupt -- one more to kill letter)\n");
|
|
|
|
} else {
|
|
|
|
printf("(continue)\n");
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)fflush(stdout);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
colljmp_p = 1;
|
|
|
|
c = readline(stdin, linebuf, LINESIZE);
|
|
|
|
colljmp_p = 0;
|
|
|
|
if (c < 0) {
|
2001-05-27 20:26:22 +00:00
|
|
|
if (value("interactive") != NULL &&
|
|
|
|
value("ignoreeof") != NULL && ++eofcount < 25) {
|
1994-05-27 12:33:43 +00:00
|
|
|
printf("Use \".\" to terminate letter\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2001-12-18 20:52:09 +00:00
|
|
|
lastlong = longline;
|
|
|
|
longline = c == LINESIZE - 1;
|
1994-05-27 12:33:43 +00:00
|
|
|
eofcount = 0;
|
|
|
|
hadintr = 0;
|
|
|
|
if (linebuf[0] == '.' && linebuf[1] == '\0' &&
|
2001-12-18 20:52:09 +00:00
|
|
|
value("interactive") != NULL && !lastlong &&
|
2001-05-27 20:26:22 +00:00
|
|
|
(value("dot") != NULL || value("ignoreeof") != NULL))
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
2001-12-18 20:52:09 +00:00
|
|
|
if (linebuf[0] != escape || value("interactive") == NULL ||
|
|
|
|
lastlong) {
|
|
|
|
if (putline(collf, linebuf, !longline) < 0)
|
1994-05-27 12:33:43 +00:00
|
|
|
goto err;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
c = linebuf[1];
|
|
|
|
switch (c) {
|
|
|
|
default:
|
|
|
|
/*
|
|
|
|
* On double escape, just send the single one.
|
|
|
|
* Otherwise, it's an error.
|
|
|
|
*/
|
|
|
|
if (c == escape) {
|
2001-12-18 20:52:09 +00:00
|
|
|
if (putline(collf, &linebuf[1], !longline) < 0)
|
1994-05-27 12:33:43 +00:00
|
|
|
goto err;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
printf("Unknown tilde escape.\n");
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
/*
|
|
|
|
* Dump core.
|
|
|
|
*/
|
|
|
|
core();
|
|
|
|
break;
|
|
|
|
case '!':
|
|
|
|
/*
|
|
|
|
* Shell escape, send the balance of the
|
|
|
|
* line to sh -c.
|
|
|
|
*/
|
|
|
|
shell(&linebuf[2]);
|
|
|
|
break;
|
|
|
|
case ':':
|
2001-12-22 22:16:48 +00:00
|
|
|
case '_':
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* Escape to command mode, but be nice!
|
|
|
|
*/
|
|
|
|
execute(&linebuf[2], 1);
|
|
|
|
goto cont;
|
|
|
|
case '.':
|
|
|
|
/*
|
|
|
|
* Simulate end of file on input.
|
|
|
|
*/
|
|
|
|
goto out;
|
|
|
|
case 'q':
|
|
|
|
/*
|
|
|
|
* Force a quit of sending mail.
|
|
|
|
* Act like an interrupt happened.
|
|
|
|
*/
|
|
|
|
hadintr++;
|
|
|
|
collint(SIGINT);
|
|
|
|
exit(1);
|
2001-12-22 22:16:48 +00:00
|
|
|
case 'x':
|
|
|
|
/*
|
|
|
|
* Exit, do not save in dead.letter.
|
|
|
|
*/
|
|
|
|
goto err;
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'h':
|
|
|
|
/*
|
|
|
|
* Grab a bunch of headers.
|
|
|
|
*/
|
|
|
|
grabh(hp, GTO|GSUBJECT|GCC|GBCC);
|
|
|
|
goto cont;
|
|
|
|
case 't':
|
|
|
|
/*
|
|
|
|
* Add to the To list.
|
|
|
|
*/
|
|
|
|
hp->h_to = cat(hp->h_to, extract(&linebuf[2], GTO));
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
/*
|
1998-01-02 16:44:13 +00:00
|
|
|
* Set the Subject line.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
|
|
|
cp = &linebuf[2];
|
2001-12-19 21:50:22 +00:00
|
|
|
while (isspace((unsigned char)*cp))
|
1994-05-27 12:33:43 +00:00
|
|
|
cp++;
|
|
|
|
hp->h_subject = savestr(cp);
|
|
|
|
break;
|
1998-01-02 16:44:13 +00:00
|
|
|
case 'R':
|
|
|
|
/*
|
|
|
|
* Set the Reply-To line.
|
|
|
|
*/
|
|
|
|
cp = &linebuf[2];
|
2001-12-19 21:50:22 +00:00
|
|
|
while (isspace((unsigned char)*cp))
|
1998-01-02 16:44:13 +00:00
|
|
|
cp++;
|
|
|
|
hp->h_replyto = savestr(cp);
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'c':
|
|
|
|
/*
|
|
|
|
* Add to the CC list.
|
|
|
|
*/
|
|
|
|
hp->h_cc = cat(hp->h_cc, extract(&linebuf[2], GCC));
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
/*
|
2001-12-22 22:16:48 +00:00
|
|
|
* Add to the BCC list.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
|
|
|
hp->h_bcc = cat(hp->h_bcc, extract(&linebuf[2], GBCC));
|
2001-12-22 22:16:48 +00:00
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
case 'A':
|
|
|
|
case 'a':
|
|
|
|
/*
|
2001-12-23 06:12:41 +00:00
|
|
|
* Insert named variable in message.
|
2001-12-22 22:16:48 +00:00
|
|
|
*/
|
|
|
|
switch(c) {
|
|
|
|
case 'i':
|
|
|
|
cp = &linebuf[2];
|
|
|
|
while(isspace((unsigned char)*cp))
|
|
|
|
cp++;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
cp = "sign";
|
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
cp = "Sign";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(*cp != '\0' && (cp = value(cp)) != NULL) {
|
|
|
|
printf("%s\n", cp);
|
|
|
|
if(putline(collf, cp, 1) < 0)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
case 'd':
|
2001-12-22 22:16:48 +00:00
|
|
|
/*
|
|
|
|
* Read in the dead letter file.
|
|
|
|
*/
|
|
|
|
if (strlcpy(linebuf + 2, getdeadletter(),
|
|
|
|
sizeof(linebuf) - 2)
|
2001-03-25 04:57:05 +00:00
|
|
|
>= sizeof(linebuf) - 2) {
|
|
|
|
printf("Line buffer overflow\n");
|
|
|
|
break;
|
|
|
|
}
|
2001-12-22 22:16:48 +00:00
|
|
|
/* FALLTHROUGH */
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'r':
|
2001-12-22 22:16:48 +00:00
|
|
|
case '<':
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* Invoke a file:
|
|
|
|
* Search for the file name,
|
|
|
|
* then open it and copy the contents to collf.
|
|
|
|
*/
|
|
|
|
cp = &linebuf[2];
|
2001-12-19 21:50:22 +00:00
|
|
|
while (isspace((unsigned char)*cp))
|
1994-05-27 12:33:43 +00:00
|
|
|
cp++;
|
|
|
|
if (*cp == '\0') {
|
|
|
|
printf("Interpolate what file?\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cp = expand(cp);
|
2001-05-27 20:26:22 +00:00
|
|
|
if (cp == NULL)
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
2001-12-22 22:16:48 +00:00
|
|
|
if (*cp == '!') {
|
|
|
|
/*
|
|
|
|
* Insert stdout of command.
|
|
|
|
*/
|
|
|
|
char *sh;
|
|
|
|
int nullfd, tempfd, rc;
|
|
|
|
char tempname2[PATHSIZE];
|
|
|
|
|
2016-07-11 05:44:58 +00:00
|
|
|
if ((nullfd = open(_PATH_DEVNULL, O_RDONLY, 0))
|
2001-12-22 22:16:48 +00:00
|
|
|
== -1) {
|
2016-07-11 05:44:58 +00:00
|
|
|
warn(_PATH_DEVNULL);
|
2001-12-22 22:16:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)snprintf(tempname2, sizeof(tempname2),
|
|
|
|
"%s/mail.ReXXXXXXXXXX", tmpdir);
|
|
|
|
if ((tempfd = mkstemp(tempname2)) == -1 ||
|
|
|
|
(fbuf = Fdopen(tempfd, "w+")) == NULL) {
|
|
|
|
warn("%s", tempname2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
(void)unlink(tempname2);
|
|
|
|
|
|
|
|
if ((sh = value("SHELL")) == NULL)
|
|
|
|
sh = _PATH_CSHELL;
|
|
|
|
|
|
|
|
rc = run_command(sh, 0, nullfd, fileno(fbuf),
|
|
|
|
"-c", cp+1, NULL);
|
|
|
|
|
|
|
|
close(nullfd);
|
|
|
|
|
|
|
|
if (rc < 0) {
|
|
|
|
(void)Fclose(fbuf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fsize(fbuf) == 0) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"No bytes from command \"%s\"\n",
|
|
|
|
cp+1);
|
|
|
|
(void)Fclose(fbuf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rewind(fbuf);
|
|
|
|
} else if (isdir(cp)) {
|
1994-05-27 12:33:43 +00:00
|
|
|
printf("%s: Directory\n", cp);
|
|
|
|
break;
|
2001-12-22 22:16:48 +00:00
|
|
|
} else if ((fbuf = Fopen(cp, "r")) == NULL) {
|
2001-03-25 04:57:05 +00:00
|
|
|
warn("%s", cp);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
printf("\"%s\" ", cp);
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)fflush(stdout);
|
1994-05-27 12:33:43 +00:00
|
|
|
lc = 0;
|
|
|
|
cc = 0;
|
2001-12-18 20:52:09 +00:00
|
|
|
while ((rc = readline(fbuf, linebuf, LINESIZE)) >= 0) {
|
|
|
|
if (rc != LINESIZE - 1)
|
|
|
|
lc++;
|
|
|
|
if ((t = putline(collf, linebuf,
|
|
|
|
rc != LINESIZE - 1)) < 0) {
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)Fclose(fbuf);
|
1994-05-27 12:33:43 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
cc += t;
|
|
|
|
}
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)Fclose(fbuf);
|
1994-05-27 12:33:43 +00:00
|
|
|
printf("%d/%d\n", lc, cc);
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
/*
|
|
|
|
* Write the message on a file.
|
|
|
|
*/
|
|
|
|
cp = &linebuf[2];
|
|
|
|
while (*cp == ' ' || *cp == '\t')
|
|
|
|
cp++;
|
|
|
|
if (*cp == '\0') {
|
|
|
|
fprintf(stderr, "Write what file!?\n");
|
|
|
|
break;
|
|
|
|
}
|
2001-05-27 20:26:22 +00:00
|
|
|
if ((cp = expand(cp)) == NULL)
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
rewind(collf);
|
|
|
|
exwrite(cp, collf, 1);
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
case 'M':
|
|
|
|
case 'f':
|
|
|
|
case 'F':
|
|
|
|
/*
|
|
|
|
* Interpolate the named messages, if we
|
|
|
|
* are in receiving mail mode. Does the
|
|
|
|
* standard list processing garbage.
|
|
|
|
* If ~f is given, we don't shift over.
|
|
|
|
*/
|
2001-03-25 04:57:05 +00:00
|
|
|
if (forward(linebuf + 2, collf, tempname, c) < 0)
|
1994-05-27 12:33:43 +00:00
|
|
|
goto err;
|
|
|
|
goto cont;
|
|
|
|
case '?':
|
|
|
|
if ((fbuf = Fopen(_PATH_TILDE, "r")) == NULL) {
|
2001-03-25 04:57:05 +00:00
|
|
|
warn("%s", _PATH_TILDE);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
while ((t = getc(fbuf)) != EOF)
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)putchar(t);
|
|
|
|
(void)Fclose(fbuf);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
/*
|
|
|
|
* Print out the current state of the
|
|
|
|
* message without altering anything.
|
|
|
|
*/
|
|
|
|
rewind(collf);
|
|
|
|
printf("-------\nMessage contains:\n");
|
|
|
|
puthead(hp, stdout, GTO|GSUBJECT|GCC|GBCC|GNL);
|
|
|
|
while ((t = getc(collf)) != EOF)
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)putchar(t);
|
1994-05-27 12:33:43 +00:00
|
|
|
goto cont;
|
|
|
|
case '|':
|
|
|
|
/*
|
|
|
|
* Pipe message through command.
|
|
|
|
* Collect output as new message.
|
|
|
|
*/
|
|
|
|
rewind(collf);
|
|
|
|
mespipe(collf, &linebuf[2]);
|
|
|
|
goto cont;
|
|
|
|
case 'v':
|
|
|
|
case 'e':
|
|
|
|
/*
|
|
|
|
* Edit the current message.
|
|
|
|
* 'e' means to use EDITOR
|
|
|
|
* 'v' means to use VISUAL
|
|
|
|
*/
|
|
|
|
rewind(collf);
|
|
|
|
mesedit(collf, c);
|
|
|
|
goto cont;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
goto out;
|
|
|
|
err:
|
|
|
|
if (collf != NULL) {
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)Fclose(collf);
|
1994-05-27 12:33:43 +00:00
|
|
|
collf = NULL;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
if (collf != NULL)
|
|
|
|
rewind(collf);
|
|
|
|
noreset--;
|
2001-12-18 20:52:09 +00:00
|
|
|
(void)sigprocmask(SIG_BLOCK, &nset, NULL);
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)signal(SIGINT, saveint);
|
|
|
|
(void)signal(SIGHUP, savehup);
|
|
|
|
(void)signal(SIGTSTP, savetstp);
|
|
|
|
(void)signal(SIGTTOU, savettou);
|
|
|
|
(void)signal(SIGTTIN, savettin);
|
2001-12-18 20:52:09 +00:00
|
|
|
(void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
|
2001-05-27 20:26:22 +00:00
|
|
|
return (collf);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write a file, ex-like if f set.
|
|
|
|
*/
|
|
|
|
int
|
2010-12-19 16:25:23 +00:00
|
|
|
exwrite(char name[], FILE *fp, int f)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-05-27 20:26:22 +00:00
|
|
|
FILE *of;
|
|
|
|
int c, lc;
|
1994-05-27 12:33:43 +00:00
|
|
|
long cc;
|
|
|
|
struct stat junk;
|
|
|
|
|
|
|
|
if (f) {
|
|
|
|
printf("\"%s\" ", name);
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)fflush(stdout);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2001-03-25 04:57:05 +00:00
|
|
|
if (stat(name, &junk) >= 0 && S_ISREG(junk.st_mode)) {
|
1994-05-27 12:33:43 +00:00
|
|
|
if (!f)
|
|
|
|
fprintf(stderr, "%s: ", name);
|
|
|
|
fprintf(stderr, "File exists\n");
|
2001-05-27 20:26:22 +00:00
|
|
|
return (-1);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
if ((of = Fopen(name, "w")) == NULL) {
|
2001-05-27 20:26:22 +00:00
|
|
|
warn((char *)NULL);
|
|
|
|
return (-1);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
lc = 0;
|
|
|
|
cc = 0;
|
|
|
|
while ((c = getc(fp)) != EOF) {
|
|
|
|
cc++;
|
|
|
|
if (c == '\n')
|
|
|
|
lc++;
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)putc(c, of);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (ferror(of)) {
|
2001-03-25 04:57:05 +00:00
|
|
|
warnx("%s", name);
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)Fclose(of);
|
|
|
|
return (-1);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)Fclose(of);
|
1994-05-27 12:33:43 +00:00
|
|
|
printf("%d/%ld\n", lc, cc);
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)fflush(stdout);
|
|
|
|
return (0);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Edit the message being collected on fp.
|
|
|
|
* On return, make the edit file the new temp file.
|
|
|
|
*/
|
|
|
|
void
|
2010-12-19 16:25:23 +00:00
|
|
|
mesedit(FILE *fp, int c)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
sig_t sigint = signal(SIGINT, SIG_IGN);
|
|
|
|
FILE *nf = run_editor(fp, (off_t)-1, c, 0);
|
|
|
|
|
|
|
|
if (nf != NULL) {
|
2001-09-02 14:40:51 +00:00
|
|
|
(void)fseeko(nf, (off_t)0, SEEK_END);
|
1994-05-27 12:33:43 +00:00
|
|
|
collf = nf;
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)Fclose(fp);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)signal(SIGINT, sigint);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pipe the message through the command.
|
|
|
|
* Old message is on stdin of command;
|
|
|
|
* New message collected from stdout.
|
|
|
|
* Sh -c must return 0 to accept the new message.
|
|
|
|
*/
|
|
|
|
void
|
2010-12-19 16:25:23 +00:00
|
|
|
mespipe(FILE *fp, char cmd[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
FILE *nf;
|
2001-03-25 04:57:05 +00:00
|
|
|
int fd;
|
1994-05-27 12:33:43 +00:00
|
|
|
sig_t sigint = signal(SIGINT, SIG_IGN);
|
2001-05-27 20:26:22 +00:00
|
|
|
char *sh, tempname[PATHSIZE];
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)snprintf(tempname, sizeof(tempname),
|
|
|
|
"%s/mail.ReXXXXXXXXXX", tmpdir);
|
2001-03-25 04:57:05 +00:00
|
|
|
if ((fd = mkstemp(tempname)) == -1 ||
|
|
|
|
(nf = Fdopen(fd, "w+")) == NULL) {
|
|
|
|
warn("%s", tempname);
|
1994-05-27 12:33:43 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)rm(tempname);
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* stdin = current message.
|
|
|
|
* stdout = new message.
|
|
|
|
*/
|
2001-05-27 20:26:22 +00:00
|
|
|
if ((sh = value("SHELL")) == NULL)
|
|
|
|
sh = _PATH_CSHELL;
|
|
|
|
if (run_command(sh,
|
|
|
|
0, fileno(fp), fileno(nf), "-c", cmd, NULL) < 0) {
|
|
|
|
(void)Fclose(nf);
|
1994-05-27 12:33:43 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (fsize(nf) == 0) {
|
|
|
|
fprintf(stderr, "No bytes from \"%s\" !?\n", cmd);
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)Fclose(nf);
|
1994-05-27 12:33:43 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Take new files.
|
|
|
|
*/
|
2001-09-02 14:40:51 +00:00
|
|
|
(void)fseeko(nf, (off_t)0, SEEK_END);
|
1994-05-27 12:33:43 +00:00
|
|
|
collf = nf;
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)Fclose(fp);
|
1994-05-27 12:33:43 +00:00
|
|
|
out:
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)signal(SIGINT, sigint);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Interpolate the named messages into the current
|
|
|
|
* message, preceding each line with a tab.
|
|
|
|
* Return a count of the number of characters now in
|
|
|
|
* the message, or -1 if an error is encountered writing
|
|
|
|
* the message temporary. The flag argument is 'm' if we
|
|
|
|
* should shift over and 'f' if not.
|
|
|
|
*/
|
|
|
|
int
|
2010-12-19 16:25:23 +00:00
|
|
|
forward(char ms[], FILE *fp, char *fn, int f)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-05-27 20:26:22 +00:00
|
|
|
int *msgvec;
|
1994-05-27 12:33:43 +00:00
|
|
|
struct ignoretab *ig;
|
|
|
|
char *tabst;
|
|
|
|
|
2001-05-27 20:26:22 +00:00
|
|
|
msgvec = (int *)salloc((msgCount+1) * sizeof(*msgvec));
|
|
|
|
if (msgvec == NULL)
|
|
|
|
return (0);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (getmsglist(ms, msgvec, 0) < 0)
|
2001-05-27 20:26:22 +00:00
|
|
|
return (0);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (*msgvec == 0) {
|
|
|
|
*msgvec = first(0, MMNORM);
|
1997-09-18 14:08:40 +00:00
|
|
|
if (*msgvec == 0) {
|
1994-05-27 12:33:43 +00:00
|
|
|
printf("No appropriate messages\n");
|
2001-05-27 20:26:22 +00:00
|
|
|
return (0);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
1997-09-18 14:08:40 +00:00
|
|
|
msgvec[1] = 0;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
if (f == 'f' || f == 'F')
|
2001-05-27 20:26:22 +00:00
|
|
|
tabst = NULL;
|
|
|
|
else if ((tabst = value("indentprefix")) == NULL)
|
1994-05-27 12:33:43 +00:00
|
|
|
tabst = "\t";
|
2001-12-19 21:50:22 +00:00
|
|
|
ig = isupper((unsigned char)f) ? NULL : ignore;
|
1994-05-27 12:33:43 +00:00
|
|
|
printf("Interpolating:");
|
|
|
|
for (; *msgvec != 0; msgvec++) {
|
|
|
|
struct message *mp = message + *msgvec - 1;
|
|
|
|
|
|
|
|
touch(mp);
|
|
|
|
printf(" %d", *msgvec);
|
2001-03-25 04:57:05 +00:00
|
|
|
if (sendmessage(mp, fp, ig, tabst) < 0) {
|
|
|
|
warnx("%s", fn);
|
2001-05-27 20:26:22 +00:00
|
|
|
return (-1);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
2001-05-27 20:26:22 +00:00
|
|
|
return (0);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print (continue) when continued after ^Z.
|
|
|
|
*/
|
|
|
|
/*ARGSUSED*/
|
|
|
|
void
|
2010-12-19 16:25:23 +00:00
|
|
|
collstop(int s)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
sig_t old_action = signal(s, SIG_DFL);
|
2001-12-18 20:52:09 +00:00
|
|
|
sigset_t nset;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2001-12-18 20:52:09 +00:00
|
|
|
(void)sigemptyset(&nset);
|
|
|
|
(void)sigaddset(&nset, s);
|
|
|
|
(void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)kill(0, s);
|
2001-12-18 20:52:09 +00:00
|
|
|
(void)sigprocmask(SIG_BLOCK, &nset, NULL);
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)signal(s, old_action);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (colljmp_p) {
|
|
|
|
colljmp_p = 0;
|
|
|
|
hadintr = 0;
|
|
|
|
longjmp(colljmp, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* On interrupt, come here to save the partial message in ~/dead.letter.
|
|
|
|
* Then jump out of the collection loop.
|
|
|
|
*/
|
|
|
|
/*ARGSUSED*/
|
|
|
|
void
|
2010-12-19 16:25:23 +00:00
|
|
|
collint(int s __unused)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* the control flow is subtle, because we can be called from ~q.
|
|
|
|
*/
|
|
|
|
if (!hadintr) {
|
2001-05-27 20:26:22 +00:00
|
|
|
if (value("ignore") != NULL) {
|
|
|
|
printf("@");
|
|
|
|
(void)fflush(stdout);
|
1994-05-27 12:33:43 +00:00
|
|
|
clearerr(stdin);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
hadintr = 1;
|
|
|
|
longjmp(colljmp, 1);
|
|
|
|
}
|
|
|
|
rewind(collf);
|
2001-05-27 20:26:22 +00:00
|
|
|
if (value("nosave") == NULL)
|
1994-05-27 12:33:43 +00:00
|
|
|
savedeadletter(collf);
|
|
|
|
longjmp(collabort, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*ARGSUSED*/
|
|
|
|
void
|
2010-12-19 16:25:23 +00:00
|
|
|
collhup(int s __unused)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
rewind(collf);
|
|
|
|
savedeadletter(collf);
|
|
|
|
/*
|
|
|
|
* Let's pretend nobody else wants to clean up,
|
|
|
|
* a true statement at this time.
|
|
|
|
*/
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-12-19 16:25:23 +00:00
|
|
|
savedeadletter(FILE *fp)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-05-27 20:26:22 +00:00
|
|
|
FILE *dbuf;
|
|
|
|
int c;
|
1994-05-27 12:33:43 +00:00
|
|
|
char *cp;
|
|
|
|
|
|
|
|
if (fsize(fp) == 0)
|
|
|
|
return;
|
|
|
|
cp = getdeadletter();
|
|
|
|
c = umask(077);
|
|
|
|
dbuf = Fopen(cp, "a");
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)umask(c);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (dbuf == NULL)
|
|
|
|
return;
|
|
|
|
while ((c = getc(fp)) != EOF)
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)putc(c, dbuf);
|
|
|
|
(void)Fclose(dbuf);
|
1994-05-27 12:33:43 +00:00
|
|
|
rewind(fp);
|
|
|
|
}
|