* Collect input from standard input, handling
static char *SccsId
= "@(#)collect.c 1.2 %G%";
* 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
* away on dead.letter. The output file must be available to flush,
* and the input to read. Several open files could be saved all through
* Mail if stdio allowed simultaneous read/write access.
static int (*savesig
)(); /* Previous SIGINT value */
static FILE *newi
; /* File for saving away */
static FILE *newo
; /* Output side of same */
static int hf
; /* Ignore interrups */
static int nofault
; /* Soft signal if set */
static int hadintr
; /* Have seen one SIGINT so far */
static jmp_buf coljmp
; /* To get back to work */
FILE *ibuf
, *fbuf
, *obuf
;
int lc
, cc
, escape
, collrub(), intack(), stopdot
;
char linebuf
[LINESIZE
], *cp
;
stopdot
= (value("dot") != NOSTR
) && intty
;
if (value("ignore") != NOSTR
)
if ((savesig
= signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
signal(SIGINT
, hf
? intack
: collrub
);
if ((obuf
= fopen(tempMail
, "w")) == NULL
) {
if ((ibuf
= fopen(tempMail
, "r")) == 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
)
if (readline(stdin
, linebuf
) <= 0)
if (stopdot
&& equal(".", linebuf
))
if (linebuf
[0] != escape
||
(!intty
&& value("henry") == NOSTR
)) {
if ((t
= putline(obuf
, linebuf
)) < 0)
* On double escape, just send the single one.
* Otherwise, it's an error.
if (putline(obuf
, &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]);
copy(deadletter
, &linebuf
[2]);
* Search for the file name,
* then open it and copy the contents to obuf.
printf("Interpolate what file?\n");
printf("%s: directory\n");
if ((fbuf
= fopen(cp
, "r")) == NULL
) {
while (readline(fbuf
, linebuf
) > 0) {
if ((t
= putline(obuf
, 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
, obuf
, c
) < 0)
if ((fbuf
= fopen(THELPFILE
, "r")) == NULL
) {
printf("No help just now.\n");
* Print out the current state of the
* message without altering anything.
printf("-------\nMessage contains:\n");
puthead(hp
, stdout
, GTO
|GSUBJECT
|GCC
|GBCC
|GNL
);
* Pipe message through command.
* Collect output as new message.
obuf
= mespipe(ibuf
, obuf
, &linebuf
[2]);
* Edit the current message.
* 'e' means to use EDITOR
* 'v' means to use VISUAL
if ((obuf
= mesedit(ibuf
, obuf
, c
)) == NULL
)
* Non destructively interrogate the value of the given signal.
wassig
= signal(n
, SIG_IGN
);
* Write a file, ex-like if f set.
if (stat(name
, &junk
) >= 0) {
fprintf(stderr
, "%s: ", name
);
fprintf(stderr
, "File exists\n", name
);
if ((of
= fopen(name
, "w")) == NULL
) {
while ((c
= getc(ibuf
)) != EOF
) {
printf("%d/%ld\n", lc
, cc
);
* Edit the message being collected on ibuf and obuf.
* 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.
extern char tempMail
[], tempEdit
[];
sig
= signal(SIGINT
, SIG_IGN
);
if (stat(tempEdit
, &sbuf
) >= 0) {
printf("%s: file exists\n", tempEdit
);
close(creat(tempEdit
, 0600));
if ((fbuf
= fopen(tempEdit
, "w")) == NULL
) {
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
) {
if ((ibuf
= fopen(tempEdit
, "r")) == NULL
) {
* 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 ((no
= fopen(tempEdit
, "w")) == NULL
) {
if ((ni
= fopen(tempEdit
, "r")) == NULL
) {
savesig
= signal(SIGINT
, SIG_IGN
);
if ((Shell
= value("SHELL")) == NULL
)
if ((pid
= vfork()) == -1) {
* stdin = current message.
execl(Shell
, Shell
, "-c", cmd
, 0);
if (s
!= 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], obuf
) < 0) {
if (send(&message
[*ip
-1], obuf
) < 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
;
* 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.
printf("\n(Interrupt -- one more to kill letter)\n");
if (value("nosave") != NOSTR
|| fsize(newi
) == 0)
if ((dbuf
= fopen(deadletter
, "w")) == NULL
)
while ((c
= getc(newi
)) != EOF
)
* 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
++)