* 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
* 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* 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
static char sccsid
[] = "@(#)collect.c 8.2 (Berkeley) 4/19/94";
* Collect input from standard input, handling
* Read a message from standard output and return a read file to it
* The following hokiness with global variables is so that on
* receipt of an interrupt signal, the partial message can be salted
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 */
collect(hp
, printheaders
)
int lc
, cc
, escape
, eofcount
;
char linebuf
[LINESIZE
], *cp
;
void collint(), collhup(), collstop();
* Start catching signals from here, but we're still die on interrupts
* until we're in the main loop.
omask
= sigblock(sigmask(SIGINT
) | sigmask(SIGHUP
));
if ((saveint
= signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
if ((savehup
= signal(SIGHUP
, SIG_IGN
)) != SIG_IGN
)
savetstp
= signal(SIGTSTP
, collstop
);
savettou
= signal(SIGTTOU
, collstop
);
savettin
= signal(SIGTTIN
, collstop
);
if (setjmp(collabort
) || setjmp(colljmp
)) {
sigsetmask(omask
& ~(sigmask(SIGINT
) | sigmask(SIGHUP
)));
if ((collf
= Fopen(tempMail
, "w+")) == NULL
) {
* 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
;
if (hp
->h_subject
== NOSTR
&& value("interactive") != NOSTR
&&
(value("ask") != NOSTR
|| value("asksub") != NOSTR
))
if ((cp
= value("escape")) != NOSTR
)
* Come here for printing the after-signal message.
* Duplicate messages won't be printed because
* the write is aborted if we get a SIGTTOU.
"\n(Interrupt -- one more to kill letter)\n");
c
= readline(stdin
, linebuf
, LINESIZE
);
if (value("interactive") != NOSTR
&&
value("ignoreeof") != NOSTR
&& ++eofcount
< 25) {
printf("Use \".\" to terminate letter\n");
if (linebuf
[0] == '.' && linebuf
[1] == '\0' &&
value("interactive") != NOSTR
&&
(value("dot") != NOSTR
|| value("ignoreeof") != NOSTR
))
if (linebuf
[0] != escape
|| value("interactive") == NOSTR
) {
if (putline(collf
, linebuf
) < 0)
* On double escape, just send the single one.
* Otherwise, it's an error.
if (putline(collf
, &linebuf
[1]) < 0)
printf("Unknown tilde escape.\n");
* Shell escape, send the balance of the
* Escape to command mode, but be nice!
* Simulate end of file on input.
* Force a quit of sending mail.
* Act like an interrupt happened.
* Grab a bunch of headers.
grabh(hp
, GTO
|GSUBJECT
|GCC
|GBCC
);
hp
->h_to
= cat(hp
->h_to
, extract(&linebuf
[2], GTO
));
hp
->h_subject
= savestr(cp
);
hp
->h_cc
= cat(hp
->h_cc
, extract(&linebuf
[2], GCC
));
* Add stuff to blind carbon copies list.
hp
->h_bcc
= cat(hp
->h_bcc
, extract(&linebuf
[2], GBCC
));
strcpy(linebuf
+ 2, getdeadletter());
* Search for the file name,
* then open it and copy the contents to collf.
printf("Interpolate what file?\n");
printf("%s: Directory\n", cp
);
if ((fbuf
= Fopen(cp
, "r")) == NULL
) {
while (readline(fbuf
, linebuf
, LINESIZE
) >= 0) {
if ((t
= putline(collf
, linebuf
)) < 0) {
printf("%d/%d\n", lc
, cc
);
* Write the message on a file.
while (*cp
== ' ' || *cp
== '\t')
fprintf(stderr
, "Write what file!?\n");
if ((cp
= expand(cp
)) == NOSTR
)
* 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.
if (forward(linebuf
+ 2, collf
, c
) < 0)
if ((fbuf
= Fopen(_PATH_TILDE
, "r")) == NULL
) {
while ((t
= getc(fbuf
)) != EOF
)
* Print out the current state of the
* message without altering anything.
printf("-------\nMessage contains:\n");
puthead(hp
, stdout
, GTO
|GSUBJECT
|GCC
|GBCC
|GNL
);
while ((t
= getc(collf
)) != EOF
)
* Pipe message through command.
* Collect output as new message.
mespipe(collf
, &linebuf
[2]);
* Edit the current message.
* 'e' means to use EDITOR
* 'v' means to use VISUAL
sigblock(sigmask(SIGINT
) | sigmask(SIGHUP
));
signal(SIGTSTP
, savetstp
);
signal(SIGTTOU
, savettou
);
signal(SIGTTIN
, savettin
);
* Write a file, ex-like if f set.
if (stat(name
, &junk
) >= 0 && (junk
.st_mode
& S_IFMT
) == S_IFREG
) {
fprintf(stderr
, "%s: ", name
);
fprintf(stderr
, "File exists\n");
if ((of
= Fopen(name
, "w")) == NULL
) {
while ((c
= getc(fp
)) != EOF
) {
printf("%d/%ld\n", lc
, cc
);
* Edit the message being collected on fp.
* On return, make the edit file the new temp file.
sig_t sigint
= signal(SIGINT
, SIG_IGN
);
FILE *nf
= run_editor(fp
, (off_t
)-1, c
, 0);
(void) signal(SIGINT
, sigint
);
* 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.
sig_t sigint
= signal(SIGINT
, SIG_IGN
);
if ((nf
= Fopen(tempEdit
, "w+")) == NULL
) {
* stdin = current message.
if ((shell
= value("SHELL")) == NOSTR
)
0, fileno(fp
), fileno(nf
), "-c", cmd
, NOSTR
) < 0) {
fprintf(stderr
, "No bytes from \"%s\" !?\n", cmd
);
(void) signal(SIGINT
, sigint
);
* 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.
msgvec
= (int *) salloc((msgCount
+1) * sizeof *msgvec
);
if (msgvec
== (int *) NOSTR
)
if (getmsglist(ms
, msgvec
, 0) < 0)
*msgvec
= first(0, MMNORM
);
printf("No appropriate messages\n");
if (f
== 'f' || f
== 'F')
else if ((tabst
= value("indentprefix")) == NOSTR
)
ig
= isupper(f
) ? NULL
: ignore
;
printf("Interpolating:");
for (; *msgvec
!= 0; msgvec
++) {
struct message
*mp
= message
+ *msgvec
- 1;
if (send(mp
, fp
, ig
, tabst
) < 0) {
* Print (continue) when continued after ^Z.
sig_t old_action
= signal(s
, SIG_DFL
);
sigsetmask(sigblock(0) & ~sigmask(s
));
* On interrupt, come here to save the partial message in ~/dead.letter.
* Then jump out of the collection loop.
* the control flow is subtle, because we can be called from ~q.
if (value("ignore") != NOSTR
) {
if (value("nosave") == NOSTR
)
* Let's pretend nobody else wants to clean up,
* a true statement at this time.
while ((c
= getc(fp
)) != EOF
)