No empty .Rs/.Re
[netbsd-mini2440.git] / usr.bin / mail / collect.c
blobfc59bda094b2919c41244aba33f955756df0451b
1 /* $NetBSD: collect.c,v 1.42 2007/10/29 23:20:38 christos Exp $ */
3 /*
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)collect.c 8.2 (Berkeley) 4/19/94";
36 #else
37 __RCSID("$NetBSD: collect.c,v 1.42 2007/10/29 23:20:38 christos Exp $");
38 #endif
39 #endif /* not lint */
42 * Mail -- a mail program
44 * Collect input from standard input, handling
45 * ~ escapes.
48 #include <assert.h>
49 #include <util.h>
51 #include "rcv.h"
52 #include "extern.h"
53 #include "format.h"
54 #ifdef MIME_SUPPORT
55 #include "mime.h"
56 #endif
57 #include "sig.h"
58 #include "thread.h"
62 * Read a message from standard input and return a read file to it
63 * or NULL on error.
67 * The following hokiness with global variables is so that on
68 * receipt of an interrupt signal, the partial message can be salted
69 * away on dead.letter.
71 static FILE *collf; /* File for saving away */
72 static int hadintr; /* Have seen one SIGINT so far */
74 static jmp_buf abort_jmpbuf; /* To end collection with error */
75 static jmp_buf reset_jmpbuf; /* To get back to work */
76 static int reset_on_stop; /* To do job control longjmp. */
79 * Write a file, ex-like if f set.
81 static int
82 exwrite(const char name[], FILE *fp, int f)
84 FILE *of;
85 int c;
86 long cc;
87 int lc;
88 struct stat junk;
90 if (f) {
91 (void)printf("\"%s\" ", name);
92 (void)fflush(stdout);
94 if (stat(name, &junk) >= 0 && S_ISREG(junk.st_mode)) {
95 if (!f)
96 (void)fprintf(stderr, "%s: ", name);
97 (void)fprintf(stderr, "File exists\n");
98 return -1;
100 if ((of = Fopen(name, "w")) == NULL) {
101 warn("%s", name);
102 return -1;
104 lc = 0;
105 cc = 0;
106 while ((c = getc(fp)) != EOF) {
107 cc++;
108 if (c == '\n')
109 lc++;
110 (void)putc(c, of);
111 if (ferror(of)) {
112 warn("%s", name);
113 (void)Fclose(of);
114 return -1;
117 (void)Fclose(of);
118 (void)printf("%d/%ld\n", lc, cc);
119 (void)fflush(stdout);
120 return 0;
124 * Edit the message being collected on fp.
125 * On return, make the edit file the new temp file.
127 static void
128 mesedit(FILE *fp, int c)
130 struct sigaction osa;
131 sigset_t oset;
132 FILE *nf;
134 sig_check();
135 (void)sig_ignore(SIGINT, &osa, &oset);
136 nf = run_editor(fp, (off_t)-1, c, 0);
137 if (nf != NULL) {
138 (void)fseek(nf, 0L, 2);
139 collf = nf;
140 (void)Fclose(fp);
142 (void)sig_restore(SIGINT, &osa, &oset);
143 sig_check();
147 * Pipe the message through the command.
148 * Old message is on stdin of command;
149 * New message collected from stdout.
150 * Sh -c must return 0 to accept the new message.
152 static void
153 mespipe(FILE *fp, char cmd[])
155 FILE *nf;
156 struct sigaction osa;
157 sigset_t oset;
158 const char *shellcmd;
159 int fd;
160 char tempname[PATHSIZE];
162 sig_check();
163 (void)sig_ignore(SIGINT, &osa, &oset);
165 (void)snprintf(tempname, sizeof(tempname),
166 "%s/mail.ReXXXXXXXXXX", tmpdir);
167 if ((fd = mkstemp(tempname)) == -1 ||
168 (nf = Fdopen(fd, "w+")) == NULL) {
169 if (fd != -1)
170 (void)close(fd);
171 warn("%s", tempname);
172 goto out;
174 (void)unlink(tempname);
176 * stdin = current message.
177 * stdout = new message.
179 if ((shellcmd = value(ENAME_SHELL)) == NULL)
180 shellcmd = _PATH_CSHELL;
181 if (run_command(shellcmd,
182 NULL, fileno(fp), fileno(nf), "-c", cmd, NULL) < 0) {
183 (void)Fclose(nf);
184 goto out;
186 if (fsize(nf) == 0) {
187 (void)fprintf(stderr, "No bytes from \"%s\" !?\n", cmd);
188 (void)Fclose(nf);
189 goto out;
192 * Take new files.
194 (void)fseek(nf, 0L, 2);
195 collf = nf;
196 (void)Fclose(fp);
197 out:
198 (void)sig_restore(SIGINT, &osa, &oset);
199 sig_check();
203 * Interpolate the named messages into the current
204 * message, preceding each line with a tab.
205 * Return a count of the number of characters now in
206 * the message, or -1 if an error is encountered writing
207 * the message temporary. The flag argument is 'm' if we
208 * should shift over and 'f' if not.
210 static int
211 interpolate(char ms[], FILE *fp, char *fn, int f)
213 int *msgvec;
214 struct ignoretab *ig;
215 const char *tabst;
216 #ifdef MIME_SUPPORT
217 struct mime_info *mip;
218 int retval;
219 #endif
220 msgvec = salloc((get_msgCount() + 1) * sizeof(*msgvec));
221 if (msgvec == NULL)
222 return 0;
223 if (getmsglist(ms, msgvec, 0) < 0)
224 return 0;
225 if (*msgvec == 0) {
226 *msgvec = first(0, MMNORM);
227 if (*msgvec == 0) {
228 (void)printf("No appropriate messages\n");
229 return 0;
231 msgvec[1] = 0;
233 if (f == 'f' || f == 'F')
234 tabst = NULL;
235 else if ((tabst = value(ENAME_INDENTPREFIX)) == NULL)
236 tabst = "\t";
237 ig = isupper(f) ? NULL : ignore;
238 (void)printf("Interpolating:");
239 for (/*EMPTY*/; *msgvec != 0; msgvec++) {
240 struct message *mp;
241 char *fmtstr;
243 mp = get_message(*msgvec);
244 touch(mp);
245 (void)printf(" %d", *msgvec);
246 (void)fflush(stdout); /* flush stdout and the above */
248 if (tabst && (fmtstr = value(ENAME_INDENT_PREAMBLE)) != NULL)
249 fmsgprintf(collf, fmtstr, mp);
250 #ifdef MIME_SUPPORT
251 mip = NULL;
252 if (value(ENAME_MIME_DECODE_MSG)) {
253 if ((tabst == NULL && value(ENAME_MIME_DECODE_INSERT)) ||
254 (tabst != NULL && value(ENAME_MIME_DECODE_QUOTE)))
255 mip = mime_decode_open(mp);
257 retval = mime_sendmessage(mp, fp, ig, tabst, mip);
258 mime_decode_close(mip);
259 if (retval < 0)
260 #else
261 if (sendmessage(mp, fp, ig, tabst, NULL) < 0)
262 #endif
264 warn("%s", fn);
265 return -1;
267 if (tabst && (fmtstr = value(ENAME_INDENT_POSTSCRIPT)) != NULL)
268 fmsgprintf(collf, fmtstr, mp);
270 (void)printf("\n");
271 return 0;
275 * Append the contents of the file to the end of the deadletter file.
277 PUBLIC void
278 savedeadletter(FILE *fp)
280 FILE *dbuf;
281 mode_t m;
282 int c;
283 const char *cp;
285 if (fsize(fp) == 0)
286 return;
287 cp = getdeadletter();
288 m = umask(077);
289 dbuf = Fopen(cp, "a");
290 (void)umask(m);
291 if (dbuf == NULL)
292 return;
293 (void)printf("Saving message body to `%s'.\n", cp);
294 while ((c = getc(fp)) != EOF)
295 (void)putc(c, dbuf);
296 (void)Fclose(dbuf);
297 rewind(fp);
301 * On interrupt, come here to save the partial message in ~/dead.letter.
302 * Then jump out of the collection loop.
304 static void
305 coll_int(int signo)
309 * the control flow is subtle, because we can be called from ~q.
311 if (!hadintr) {
312 if (value(ENAME_IGNORE) != NULL) {
313 (void)puts("@");
314 (void)fflush(stdout);
315 clearerr(stdin);
316 return;
318 hadintr = 1;
319 longjmp(reset_jmpbuf, signo);
321 rewind(collf);
322 if (value(ENAME_NOSAVE) == NULL)
323 savedeadletter(collf);
324 longjmp(abort_jmpbuf, signo);
327 /*ARGSUSED*/
328 static void
329 coll_hup(int signo __unused)
332 rewind(collf);
333 savedeadletter(collf);
335 * Let's pretend nobody else wants to clean up,
336 * a true statement at this time.
338 exit(EXIT_FAILURE);
342 * Print (continue) when continued after ^Z.
344 static void
345 coll_stop(int signo)
348 if (reset_on_stop) {
349 reset_on_stop = 0;
350 hadintr = 0;
351 longjmp(reset_jmpbuf, signo);
355 PUBLIC FILE *
356 collect(struct header *hp, int printheaders)
358 volatile sig_t old_sigint;
359 volatile sig_t old_sighup;
360 volatile sig_t old_sigtstp;
361 volatile sig_t old_sigttin;
362 volatile sig_t old_sigttou;
363 FILE *fbuf;
364 int lc, cc;
365 int c, fd, t;
366 char linebuf[LINESIZE];
367 const char *cp;
368 char tempname[PATHSIZE];
369 char mailtempname[PATHSIZE];
370 int eofcount;
371 int longline;
372 int lastlong, rc; /* So we don't make 2 or more lines
373 out of a long input line. */
375 /* The following are declared volatile to avoid longjmp clobbering. */
376 char volatile getsub;
377 int volatile escape;
379 (void)memset(mailtempname, 0, sizeof(mailtempname));
380 collf = NULL;
382 if (setjmp(abort_jmpbuf) || setjmp(reset_jmpbuf)) {
383 (void)rm(mailtempname);
384 goto err;
386 sig_check();
388 sig_hold();
389 old_sigint = sig_signal(SIGINT, coll_int);
390 old_sighup = sig_signal(SIGHUP, coll_hup);
391 old_sigtstp = sig_signal(SIGTSTP, coll_stop);
392 old_sigttin = sig_signal(SIGTTIN, coll_stop);
393 old_sigttou = sig_signal(SIGTTOU, coll_stop);
394 sig_release();
396 noreset++;
397 (void)snprintf(mailtempname, sizeof(mailtempname),
398 "%s/mail.RsXXXXXXXXXX", tmpdir);
399 if ((fd = mkstemp(mailtempname)) == -1 ||
400 (collf = Fdopen(fd, "w+")) == NULL) {
401 if (fd != -1)
402 (void)close(fd);
403 warn("%s", mailtempname);
404 goto err;
406 (void)rm(mailtempname);
409 * If we are going to prompt for a subject,
410 * refrain from printing a newline after
411 * the headers (since some people mind).
413 t = GTO | GSUBJECT | GCC | GNL | GSMOPTS;
414 getsub = 0;
415 if (hp->h_subject == NULL && value(ENAME_INTERACTIVE) != NULL &&
416 (value(ENAME_ASK) != NULL || value(ENAME_ASKSUB) != NULL)) {
417 t &= ~GNL;
418 getsub++;
420 if (printheaders) {
421 (void)puthead(hp, stdout, t);
422 (void)fflush(stdout);
424 if ((cp = value(ENAME_ESCAPE)) != NULL)
425 escape = *cp;
426 else
427 escape = ESCAPE;
428 hadintr = 0;
429 if (setjmp(reset_jmpbuf) == 0) {
430 if (getsub)
431 (void)grabh(hp, GSUBJECT);
432 } else {
434 * Come here for printing the after-signal message.
435 * Duplicate messages won't be printed because
436 * the write is aborted if we get a SIGTTOU.
438 cont:
439 if (hadintr) {
440 (void)fflush(stdout);
441 (void)fprintf(stderr,
442 "\n(Interrupt -- one more to kill letter)\n");
443 } else {
444 (void)printf("(continue)\n");
445 (void)fflush(stdout);
448 eofcount = 0; /* reset after possible longjmp */
449 longline = 0; /* reset after possible longjmp */
450 for (;;) {
451 reset_on_stop = 1;
452 c = readline(stdin, linebuf, LINESIZE, reset_on_stop);
453 reset_on_stop = 0;
455 if (c < 0) {
456 char *p;
458 if (value(ENAME_INTERACTIVE) != NULL &&
459 (p = value(ENAME_IGNOREEOF)) != NULL &&
460 ++eofcount < (*p == 0 ? 25 : atoi(p))) {
461 (void)printf("Use \".\" to terminate letter\n");
462 continue;
464 break;
466 lastlong = longline;
467 longline = c == LINESIZE - 1;
468 eofcount = 0;
469 hadintr = 0;
470 if (linebuf[0] == '.' && linebuf[1] == '\0' &&
471 value(ENAME_INTERACTIVE) != NULL && !lastlong &&
472 (value(ENAME_DOT) != NULL || value(ENAME_IGNOREEOF) != NULL))
473 break;
474 if (linebuf[0] != escape || value(ENAME_INTERACTIVE) == NULL ||
475 lastlong) {
476 if (putline(collf, linebuf, !longline) < 0)
477 goto err;
478 continue;
480 c = linebuf[1];
481 switch (c) {
482 default:
484 * On double escape, just send the single one.
485 * Otherwise, it's an error.
487 if (c == escape) {
488 if (putline(collf, &linebuf[1], !longline) < 0)
489 goto err;
490 else
491 break;
493 (void)printf("Unknown tilde escape.\n");
494 break;
495 #ifdef MIME_SUPPORT
496 case '@':
497 hp->h_attach = mime_attach_files(hp->h_attach, &linebuf[2]);
498 break;
499 #endif
500 case 'C':
502 * Dump core.
504 (void)core(NULL);
505 break;
506 case '!':
508 * Shell escape, send the balance of the
509 * line to sh -c.
511 (void)shell(&linebuf[2]);
512 break;
513 case ':':
514 case '_':
516 * Escape to command mode, but be nice!
518 (void)execute(&linebuf[2], ec_composing);
519 goto cont;
520 case '.':
522 * Simulate end of file on input.
524 goto out;
525 case 'q':
527 * Force a quit of sending mail.
528 * Act like an interrupt happened.
530 hadintr++;
531 coll_int(SIGINT);
532 exit(1);
533 /*NOTREACHED*/
535 case 'x': /* exit, do not save in dead.letter */
536 goto err;
538 case 'h':
540 * Grab a bunch of headers.
542 (void)grabh(hp, GTO | GSUBJECT | GCC | GBCC | GSMOPTS);
543 goto cont;
544 case 't':
546 * Add to the To list.
548 hp->h_to = cat(hp->h_to, extract(&linebuf[2], GTO));
549 break;
550 case 's':
552 * Set the Subject list.
554 cp = skip_WSP(&linebuf[2]);
555 hp->h_subject = savestr(cp);
556 break;
557 case 'c':
559 * Add to the CC list.
561 hp->h_cc = cat(hp->h_cc, extract(&linebuf[2], GCC));
562 break;
563 case 'b':
565 * Add stuff to blind carbon copies list.
567 hp->h_bcc = cat(hp->h_bcc, extract(&linebuf[2], GBCC));
568 break;
569 case 'i':
570 case 'A':
571 case 'a':
573 * Insert named variable in message
576 switch(c) {
577 case 'i':
578 cp = skip_WSP(&linebuf[2]);
579 break;
580 case 'a':
581 cp = "sign";
582 break;
583 case 'A':
584 cp = "Sign";
585 break;
586 default:
587 goto err;
590 if (*cp && (cp = value(cp)) != NULL) {
591 (void)printf("%s\n", cp);
592 if (putline(collf, cp, 1) < 0)
593 goto err;
596 break;
598 case 'd':
599 (void)strcpy(linebuf + 2, getdeadletter());
600 /* FALLTHROUGH */
601 case 'r':
602 case '<':
604 * Invoke a file:
605 * Search for the file name,
606 * then open it and copy the contents to collf.
608 cp = skip_WSP(&linebuf[2]);
609 if (*cp == '\0') {
610 (void)printf("Interpolate what file?\n");
611 break;
614 cp = expand(cp);
615 if (cp == NULL)
616 break;
618 if (*cp == '!') { /* insert stdout of command */
619 const char *shellcmd;
620 int nullfd;
621 int rc2;
623 if ((nullfd = open("/dev/null", O_RDONLY, 0)) == -1) {
624 warn("/dev/null");
625 break;
628 (void)snprintf(tempname, sizeof(tempname),
629 "%s/mail.ReXXXXXXXXXX", tmpdir);
630 if ((fd = mkstemp(tempname)) == -1 ||
631 (fbuf = Fdopen(fd, "w+")) == NULL) {
632 if (fd != -1)
633 (void)close(fd);
634 warn("%s", tempname);
635 break;
637 (void)unlink(tempname);
639 if ((shellcmd = value(ENAME_SHELL)) == NULL)
640 shellcmd = _PATH_CSHELL;
642 rc2 = run_command(shellcmd, NULL, nullfd, fileno(fbuf), "-c", cp + 1, NULL);
644 (void)close(nullfd);
646 if (rc2 < 0) {
647 (void)Fclose(fbuf);
648 break;
651 if (fsize(fbuf) == 0) {
652 (void)fprintf(stderr, "No bytes from command \"%s\"\n", cp + 1);
653 (void)Fclose(fbuf);
654 break;
657 rewind(fbuf);
659 else if (isdir(cp)) {
660 (void)printf("%s: Directory\n", cp);
661 break;
663 else if ((fbuf = Fopen(cp, "r")) == NULL) {
664 warn("%s", cp);
665 break;
667 (void)printf("\"%s\" ", cp);
668 (void)fflush(stdout);
669 lc = 0;
670 cc = 0;
671 while ((rc = readline(fbuf, linebuf, LINESIZE, 0)) >= 0) {
672 if (rc != LINESIZE-1) lc++;
673 if ((t = putline(collf, linebuf,
674 rc != LINESIZE-1)) < 0) {
675 (void)Fclose(fbuf);
676 goto err;
678 cc += t;
680 (void)Fclose(fbuf);
681 (void)printf("%d/%d\n", lc, cc);
682 break;
683 case 'w':
685 * Write the message on a file.
687 cp = skip_WSP(&linebuf[2]);
688 if (*cp == '\0') {
689 (void)fprintf(stderr, "Write what file!?\n");
690 break;
692 if ((cp = expand(cp)) == NULL)
693 break;
694 rewind(collf);
695 (void)exwrite(cp, collf, 1);
696 break;
697 case 'm':
698 case 'M':
699 case 'f':
700 case 'F':
702 * Interpolate the named messages, if we
703 * are in receiving mail mode. Does the
704 * standard list processing garbage.
705 * If ~f is given, we don't shift over.
707 if (interpolate(linebuf + 2, collf, mailtempname, c) < 0)
708 goto err;
709 goto cont;
710 case '?':
711 cathelp(_PATH_TILDE);
712 break;
713 case 'p':
715 * Print out the current state of the
716 * message without altering anything.
718 rewind(collf);
719 (void)printf("-------\nMessage contains:\n");
720 (void)puthead(hp, stdout,
721 GTO | GSUBJECT | GCC | GBCC | GSMOPTS | GNL);
722 while ((t = getc(collf)) != EOF)
723 (void)putchar(t);
724 goto cont;
725 case '|':
727 * Pipe message through command.
728 * Collect output as new message.
730 rewind(collf);
731 mespipe(collf, &linebuf[2]);
732 goto cont;
733 case 'v':
734 case 'e':
736 * Edit the current message.
737 * 'e' means to use EDITOR
738 * 'v' means to use VISUAL
740 rewind(collf);
741 mesedit(collf, c);
742 goto cont;
745 goto out;
746 err:
747 if (collf != NULL) {
748 (void)Fclose(collf);
749 collf = NULL;
751 out:
752 if (collf != NULL)
753 rewind(collf);
754 noreset--;
756 sig_hold();
757 (void)sig_signal(SIGINT, old_sigint);
758 (void)sig_signal(SIGHUP, old_sighup);
759 (void)sig_signal(SIGTSTP, old_sigtstp);
760 (void)sig_signal(SIGTTIN, old_sigttin);
761 (void)sig_signal(SIGTTOU, old_sigttou);
762 sig_release();
764 sig_check();
765 return collf;