* Copyright (c) 1980 Regents of the University of California.
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Berkeley. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
static char sccsid
[] = "@(#)collect.c 5.16 (Berkeley) %G%";
* 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 int (*saveint
)(); /* Previous SIGINT value */
static int (*savehup
)(); /* Previous SIGHUP value */
static int (*savecont
)(); /* Previous SIGCONT value */
static FILE *collf
; /* File for saving away */
static int hadintr
; /* Have seen one SIGINT so far */
static jmp_buf coljmp
; /* To get back to work */
collect(hp
, printheaders
)
int collrub(), intack(), collcont();
char linebuf
[LINESIZE
], *cp
;
* 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
)
signal(SIGINT
, value("ignore") != NOSTR
? intack
: collrub
);
if ((savehup
= signal(SIGHUP
, SIG_IGN
)) != SIG_IGN
)
savecont
= signal(SIGCONT
, SIG_DFL
);
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
)
signal(SIGCONT
, collcont
);
* 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");
if (readline(stdin
, linebuf
) < 0) {
if (value("interactive") != NOSTR
&&
value("ignoreeof") != NOSTR
) {
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
) >= 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.
printf("No messages to send from!?!\n");
if (forward(linebuf
+ 2, collf
, c
) < 0)
if ((fbuf
= fopen(THELPFILE
, "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
signal(SIGCONT
, savecont
);
* 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.
int (*sigint
)() = signal(SIGINT
, SIG_IGN
);
int (*sigcont
)() = signal(SIGCONT
, SIG_DFL
);
FILE *nf
= run_editor(fp
, (off_t
)-1, c
, 0);
(void) signal(SIGINT
, sigint
);
(void) signal(SIGCONT
, sigcont
);
* 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.
int (*sigint
)() = signal(SIGINT
, SIG_IGN
);
int (*sigcont
)() = signal(SIGCONT
, SIG_DFL
);
if ((nf
= fopen(tempEdit
, "w+")) == NULL
) {
* stdin = current message.
if (run_command(cmd
, 0, fileno(fp
), fileno(nf
), NOSTR
) < 0) {
fprintf(stderr
, "No bytes from \"%s\" !?\n", cmd
);
(void) signal(SIGINT
, sigint
);
(void) signal(SIGCONT
, sigcont
);
* 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("tabstr")) == 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.
* On interrupt, go here to save the partial
* message on ~/dead.letter.
* Then restore signals and execute the normal
* signal routine. We only come here if signals
* were previously set anyway.
if (s
== SIGINT
&& hadintr
== 0) {
if (s
!= SIGINT
|| value("nosave") == NOSTR
)
signal(SIGCONT
, savecont
);
while ((c
= getc(fp
)) != EOF
)
* Acknowledge an interrupt signal from the tty by typing an @