* Copyright (c) 1980 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
static char *sccsid
= "@(#)collect.c 5.4 (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 */
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 ((fp
= 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 (intty
&& sflag
== NOSTR
&& hp
->h_subject
== NOSTR
&& value("ask"))
if ((cp
= value("escape")) != NOSTR
)
* We can put the setjmp here because register variable
* needs to be saved in the loop.
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 (intty
&& value("ignoreeof") != NOSTR
) {
printf("Use \".\" to terminate letter\n");
if (intty
&& equal(".", linebuf
) &&
(value("dot") != NOSTR
|| value("ignoreeof") != NOSTR
))
if (linebuf
[0] != escape
|| rflag
!= NOSTR
) {
if (putline(fp
, linebuf
) < 0)
* On double escape, just send the single one.
* Otherwise, it's an error.
if (putline(fp
, &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.
printf("~h: no can do!?\n");
grabh(hp
, GTO
|GSUBJECT
|GCC
|GBCC
);
hp
->h_to
= addto(hp
->h_to
, &linebuf
[2]);
hp
->h_subject
= savestr(cp
);
hp
->h_cc
= addto(hp
->h_cc
, &linebuf
[2]);
* Add stuff to blind carbon copies list.
hp
->h_bcc
= addto(hp
->h_bcc
, &linebuf
[2]);
strcpy(linebuf
+ 2, deadletter
);
* Search for the file name,
* then open it and copy the contents to fp.
printf("Interpolate what file?\n");
printf("%s: Directory\n", cp
);
if ((fbuf
= fopen(cp
, "r")) == NULL
) {
while (readline(fbuf
, linebuf
) >= 0) {
if ((t
= putline(fp
, linebuf
)) < 0) {
printf("%d/%d\n", lc
, cc
);
* Write the message on a file.
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(cp
, fp
, c
) < 0)
if ((fbuf
= fopen(THELPFILE
, "r")) == NULL
) {
while ((t
= getc(fp
)) != 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(fp
)) != EOF
)
* Pipe message through command.
* Collect output as new message.
fp
= mespipe(fp
, &linebuf
[2]);
* Edit the current message.
* 'e' means to use EDITOR
* 'v' means to use VISUAL
if ((fp
= mesedit(fp
, c
)) == NULL
)
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.
* Write the message out onto some poorly-named temp file
* and point an editor at it.
* On return, make the edit file the new temp file.
int (*sigint
)(), (*sigcont
)();
sigint
= signal(SIGINT
, SIG_IGN
);
sigcont
= signal(SIGCONT
, SIG_DFL
);
if (stat(tempEdit
, &sbuf
) >= 0) {
printf("%s: file exists\n", tempEdit
);
close(creat(tempEdit
, 0600));
if ((fbuf
= fopen(tempEdit
, "w")) == NULL
) {
while ((t
= getc(fp
)) != EOF
)
if ((edit
= value(c
== 'e' ? "EDITOR" : "VISUAL")) == NOSTR
)
edit
= c
== 'e' ? EDITOR
: VISUAL
;
execl(edit
, edit
, tempEdit
, 0);
printf("Fatal error in \"%s\"\n", edit
);
* Now switch to new file.
if ((fbuf
= fopen(tempEdit
, "a+")) == NULL
) {
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.
if ((nf
= fopen(tempEdit
, "w+")) == NULL
) {
saveint
= signal(SIGINT
, SIG_IGN
);
if ((Shell
= value("SHELL")) == NULL
)
if ((pid
= vfork()) == -1) {
* stdin = current message.
for (fd
= getdtablesize(); --fd
> 2;)
execl(Shell
, Shell
, "-c", cmd
, 0);
if (s
.w_status
!= 0 || pid
== -1) {
fprintf(stderr
, "\"%s\" failed!?\n", cmd
);
fprintf(stderr
, "No bytes from \"%s\" !?\n", cmd
);
* 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.
register int *msgvec
, *ip
;
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");
printf("Interpolating:");
for (ip
= msgvec
; *ip
!= NULL
; ip
++) {
if (transmit(&message
[*ip
-1], fp
) < 0L) {
if (send(&message
[*ip
-1], fp
, 0) < 0) {
* Send message described by the passed pointer to the
* passed output buffer. Insert a tab in front of each
* line. Return a count of the characters sent, or -1
register struct message
*mp
;
* 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
|| fsize(collf
) == 0)
if ((dbuf
= fopen(deadletter
, "w")) == NULL
)
while ((c
= getc(collf
)) != EOF
)
signal(SIGCONT
, savecont
);
* Acknowledge an interrupt signal from the tty by typing an @
* Add a string to the end of a header entry field.
register char *cp
, *cp2
, *linebuf
;
linebuf
= salloc(strlen(hf
) + strlen(news
) + 2);
for (cp
= hf
; any(*cp
, " \t"); cp
++)
for (cp2
= linebuf
; *cp
;)
for (cp
= news
; any(*cp
, " \t"); cp
++)