/* Copyright (c) 1979 Regents of the University of California */
* 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
* 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 */
FILE *ibuf
, *fbuf
, *obuf
;
int lc
, cc
, escape
, collrub(), intack();
char linebuf
[LINESIZE
], *cp
;
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 (intty
&& hp
->h_subj
== NOSTR
&& value("ask"))
if ((cp
= value("escape")) != NOSTR
)
while (readline(stdin
, linebuf
) > 0) {
if (linebuf
[0] != escape
) {
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
* 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
|GSUBJ
|GCC
);
hp
->h_to
= addto(hp
->h_to
, &linebuf
[2]);
hp
->h_subj
= savestr(cp
);
hp
->h_cc
= addto(hp
->h_cc
, &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");
* Interpolate the named messages, if we
* are in receiving mail mode. Does the
* standard list processing garbage.
printf("No messages to send from!?!\n");
if (forward(cp
, obuf
) < 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");
* 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
)
* Write a file, ex-like if f set.
if (stat(name
, &junk
) >= 0) {
fprintf(stderr
, "%s: 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", EDITOR
);
* 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
= fork()) == -1) {
* stdin = current message.
execl(Shell
, Shell
, "-c", cmd
, 0);
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
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) {
* 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.
if (value("save") == NOSTR
)
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
) + 1);
for (cp
= hf
; any(*cp
, " \t"); cp
++)
for (cp2
= linebuf
; *cp
;)
for (cp
= news
; any(*cp
, " \t"); cp
++)