2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid[] = "@(#)collect.c 8.2 (Berkeley) 4/19/94";
39 * Mail -- a mail program
41 * Collect input from standard input, handling
49 * Read a message from standard output and return a read file to it
54 * The following hokiness with global variables is so that on
55 * receipt of an interrupt signal, the partial message can be salted
56 * away on dead.letter.
59 static sig_t saveint; /* Previous SIGINT value */
60 static sig_t savehup; /* Previous SIGHUP value */
61 static sig_t savetstp; /* Previous SIGTSTP value */
62 static sig_t savettou; /* Previous SIGTTOU value */
63 static sig_t savettin; /* Previous SIGTTIN value */
64 static FILE *collf; /* File for saving away */
65 static int hadintr; /* Have seen one SIGINT so far */
67 static jmp_buf colljmp; /* To get back to work */
68 static int colljmp_p; /* whether to long jump */
69 static jmp_buf collabort; /* To end collection with error */
72 collect(hp, printheaders)
77 int lc, cc, escape, eofcount;
79 char linebuf[LINESIZE], *cp;
80 extern char tempMail[];
83 void collint(), collhup(), collstop();
87 * Start catching signals from here, but we're still die on interrupts
88 * until we're in the main loop.
90 omask = sigblock(sigmask(SIGINT) | sigmask(SIGHUP));
91 if ((saveint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
92 signal(SIGINT, collint);
93 if ((savehup = signal(SIGHUP, SIG_IGN)) != SIG_IGN)
94 signal(SIGHUP, collhup);
95 savetstp = signal(SIGTSTP, collstop);
96 savettou = signal(SIGTTOU, collstop);
97 savettin = signal(SIGTTIN, collstop);
98 if (setjmp(collabort) || setjmp(colljmp)) {
102 sigsetmask(omask & ~(sigmask(SIGINT) | sigmask(SIGHUP)));
105 if ((collf = Fopen(tempMail, "w+")) == NULL) {
112 * If we are going to prompt for a subject,
113 * refrain from printing a newline after
114 * the headers (since some people mind).
116 t = GTO|GSUBJECT|GCC|GNL;
118 if (hp->h_subject == NOSTR && value("interactive") != NOSTR &&
119 (value("ask") != NOSTR || value("asksub") != NOSTR))
122 puthead(hp, stdout, t);
125 if ((cp = value("escape")) != NOSTR)
132 if (!setjmp(colljmp)) {
137 * Come here for printing the after-signal message.
138 * Duplicate messages won't be printed because
139 * the write is aborted if we get a SIGTTOU.
145 "\n(Interrupt -- one more to kill letter)\n");
147 printf("(continue)\n");
153 c = readline(stdin, linebuf, LINESIZE);
156 if (value("interactive") != NOSTR &&
157 value("ignoreeof") != NOSTR && ++eofcount < 25) {
158 printf("Use \".\" to terminate letter\n");
165 if (linebuf[0] == '.' && linebuf[1] == '\0' &&
166 value("interactive") != NOSTR &&
167 (value("dot") != NOSTR || value("ignoreeof") != NOSTR))
169 if (linebuf[0] != escape || value("interactive") == NOSTR) {
170 if (putline(collf, linebuf) < 0)
178 * On double escape, just send the single one.
179 * Otherwise, it's an error.
182 if (putline(collf, &linebuf[1]) < 0)
187 printf("Unknown tilde escape.\n");
197 * Shell escape, send the balance of the
204 * Escape to command mode, but be nice!
206 execute(&linebuf[2], 1);
210 * Simulate end of file on input.
215 * Force a quit of sending mail.
216 * Act like an interrupt happened.
223 * Grab a bunch of headers.
225 grabh(hp, GTO|GSUBJECT|GCC|GBCC);
229 * Add to the To list.
231 hp->h_to = cat(hp->h_to, extract(&linebuf[2], GTO));
235 * Set the Subject list.
240 hp->h_subject = savestr(cp);
244 * Add to the CC list.
246 hp->h_cc = cat(hp->h_cc, extract(&linebuf[2], GCC));
250 * Add stuff to blind carbon copies list.
252 hp->h_bcc = cat(hp->h_bcc, extract(&linebuf[2], GBCC));
255 strcpy(linebuf + 2, getdeadletter());
256 /* fall into . . . */
260 * Search for the file name,
261 * then open it and copy the contents to collf.
267 printf("Interpolate what file?\n");
274 printf("%s: Directory\n", cp);
277 if ((fbuf = Fopen(cp, "r")) == NULL) {
281 printf("\"%s\" ", cp);
285 while (readline(fbuf, linebuf, LINESIZE) >= 0) {
287 if ((t = putline(collf, linebuf)) < 0) {
294 printf("%d/%d\n", lc, cc);
298 * Write the message on a file.
301 while (*cp == ' ' || *cp == '\t')
304 fprintf(stderr, "Write what file!?\n");
307 if ((cp = expand(cp)) == NOSTR)
310 exwrite(cp, collf, 1);
317 * Interpolate the named messages, if we
318 * are in receiving mail mode. Does the
319 * standard list processing garbage.
320 * If ~f is given, we don't shift over.
322 if (forward(linebuf + 2, collf, c) < 0)
326 if ((fbuf = Fopen(_PATH_TILDE, "r")) == NULL) {
330 while ((t = getc(fbuf)) != EOF)
336 * Print out the current state of the
337 * message without altering anything.
340 printf("-------\nMessage contains:\n");
341 puthead(hp, stdout, GTO|GSUBJECT|GCC|GBCC|GNL);
342 while ((t = getc(collf)) != EOF)
347 * Pipe message through command.
348 * Collect output as new message.
351 mespipe(collf, &linebuf[2]);
356 * Edit the current message.
357 * 'e' means to use EDITOR
358 * 'v' means to use VISUAL
375 sigblock(sigmask(SIGINT) | sigmask(SIGHUP));
376 signal(SIGINT, saveint);
377 signal(SIGHUP, savehup);
378 signal(SIGTSTP, savetstp);
379 signal(SIGTTOU, savettou);
380 signal(SIGTTIN, savettin);
386 * Write a file, ex-like if f set.
401 printf("\"%s\" ", name);
404 if (stat(name, &junk) >= 0 && (junk.st_mode & S_IFMT) == S_IFREG) {
406 fprintf(stderr, "%s: ", name);
407 fprintf(stderr, "File exists\n");
410 if ((of = Fopen(name, "w")) == NULL) {
416 while ((c = getc(fp)) != EOF) {
428 printf("%d/%ld\n", lc, cc);
434 * Edit the message being collected on fp.
435 * On return, make the edit file the new temp file.
442 sig_t sigint = signal(SIGINT, SIG_IGN);
443 FILE *nf = run_editor(fp, (off_t)-1, c, 0);
450 (void) signal(SIGINT, sigint);
454 * Pipe the message through the command.
455 * Old message is on stdin of command;
456 * New message collected from stdout.
457 * Sh -c must return 0 to accept the new message.
465 sig_t sigint = signal(SIGINT, SIG_IGN);
466 extern char tempEdit[];
469 if ((nf = Fopen(tempEdit, "w+")) == NULL) {
473 (void) unlink(tempEdit);
475 * stdin = current message.
476 * stdout = new message.
478 if ((shell = value("SHELL")) == NOSTR)
479 shell = _PATH_CSHELL;
480 if (run_command(shell,
481 0, fileno(fp), fileno(nf), "-c", cmd, NOSTR) < 0) {
485 if (fsize(nf) == 0) {
486 fprintf(stderr, "No bytes from \"%s\" !?\n", cmd);
493 (void) fseek(nf, 0L, 2);
497 (void) signal(SIGINT, sigint);
501 * Interpolate the named messages into the current
502 * message, preceding each line with a tab.
503 * Return a count of the number of characters now in
504 * the message, or -1 if an error is encountered writing
505 * the message temporary. The flag argument is 'm' if we
506 * should shift over and 'f' if not.
514 register int *msgvec;
515 extern char tempMail[];
516 struct ignoretab *ig;
519 msgvec = (int *) salloc((msgCount+1) * sizeof *msgvec);
520 if (msgvec == (int *) NOSTR)
522 if (getmsglist(ms, msgvec, 0) < 0)
525 *msgvec = first(0, MMNORM);
526 if (*msgvec == NULL) {
527 printf("No appropriate messages\n");
532 if (f == 'f' || f == 'F')
534 else if ((tabst = value("indentprefix")) == NOSTR)
536 ig = isupper(f) ? NULL : ignore;
537 printf("Interpolating:");
538 for (; *msgvec != 0; msgvec++) {
539 struct message *mp = message + *msgvec - 1;
542 printf(" %d", *msgvec);
543 if (send(mp, fp, ig, tabst) < 0) {
553 * Print (continue) when continued after ^Z.
560 sig_t old_action = signal(s, SIG_DFL);
562 sigsetmask(sigblock(0) & ~sigmask(s));
564 sigblock(sigmask(s));
565 signal(s, old_action);
574 * On interrupt, come here to save the partial message in ~/dead.letter.
575 * Then jump out of the collection loop.
583 * the control flow is subtle, because we can be called from ~q.
586 if (value("ignore") != NOSTR) {
596 if (value("nosave") == NOSTR)
597 savedeadletter(collf);
598 longjmp(collabort, 1);
607 savedeadletter(collf);
609 * Let's pretend nobody else wants to clean up,
610 * a true statement at this time.
625 cp = getdeadletter();
627 dbuf = Fopen(cp, "a");
631 while ((c = getc(fp)) != EOF)
632 (void) putc(c, dbuf);