collect.c revision 1.21 1 /* $NetBSD: collect.c,v 1.21 2001/02/05 02:07:53 christos Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)collect.c 8.2 (Berkeley) 4/19/94";
40 #else
41 __RCSID("$NetBSD: collect.c,v 1.21 2001/02/05 02:07:53 christos Exp $");
42 #endif
43 #endif /* not lint */
44
45 /*
46 * Mail -- a mail program
47 *
48 * Collect input from standard input, handling
49 * ~ escapes.
50 */
51
52 #include "rcv.h"
53 #include "extern.h"
54
55 extern char *tempMail;
56 extern char *tempEdit;
57
58 /*
59 * Read a message from standard output and return a read file to it
60 * or NULL on error.
61 */
62
63 /*
64 * The following hokiness with global variables is so that on
65 * receipt of an interrupt signal, the partial message can be salted
66 * away on dead.letter.
67 */
68
69 static sig_t saveint; /* Previous SIGINT value */
70 static sig_t savehup; /* Previous SIGHUP value */
71 static sig_t savetstp; /* Previous SIGTSTP value */
72 static sig_t savettou; /* Previous SIGTTOU value */
73 static sig_t savettin; /* Previous SIGTTIN value */
74 static FILE *collf; /* File for saving away */
75 static int hadintr; /* Have seen one SIGINT so far */
76
77 static jmp_buf colljmp; /* To get back to work */
78 static int colljmp_p; /* whether to long jump */
79 static jmp_buf collabort; /* To end collection with error */
80
81 FILE *
82 collect(hp, printheaders)
83 struct header *hp;
84 int printheaders;
85 {
86 FILE *fbuf;
87 int lc, cc, escape, eofcount;
88 int c, t;
89 char linebuf[LINESIZE], *cp;
90 char getsub;
91 sigset_t nset;
92 int longline, lastlong, rc; /* So we don't make 2 or more lines
93 out of a long input line. */
94 #if __GNUC__
95 /* Avoid longjmp clobbering */
96 (void) &escape;
97 (void) &eofcount;
98 (void) &getsub;
99 (void) &longline;
100 #endif
101
102 collf = NULL;
103 /*
104 * Start catching signals from here, but we're still die on interrupts
105 * until we're in the main loop.
106 */
107 sigemptyset(&nset);
108 sigaddset(&nset, SIGINT);
109 sigaddset(&nset, SIGHUP);
110 sigprocmask(SIG_BLOCK, &nset, NULL);
111 if ((saveint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
112 signal(SIGINT, collint);
113 if ((savehup = signal(SIGHUP, SIG_IGN)) != SIG_IGN)
114 signal(SIGHUP, collhup);
115 savetstp = signal(SIGTSTP, collstop);
116 savettou = signal(SIGTTOU, collstop);
117 savettin = signal(SIGTTIN, collstop);
118 if (setjmp(collabort) || setjmp(colljmp)) {
119 rm(tempMail);
120 goto err;
121 }
122 sigprocmask(SIG_UNBLOCK, &nset, NULL);
123
124 noreset++;
125 if ((collf = Fopen(tempMail, "w+")) == NULL) {
126 perror(tempMail);
127 goto err;
128 }
129 unlink(tempMail);
130
131 /*
132 * If we are going to prompt for a subject,
133 * refrain from printing a newline after
134 * the headers (since some people mind).
135 */
136 t = GTO|GSUBJECT|GCC|GNL;
137 getsub = 0;
138 if (hp->h_subject == NOSTR && value("interactive") != NOSTR &&
139 (value("ask") != NOSTR || value("asksub") != NOSTR))
140 t &= ~GNL, getsub++;
141 if (printheaders) {
142 puthead(hp, stdout, t);
143 fflush(stdout);
144 }
145 if ((cp = value("escape")) != NOSTR)
146 escape = *cp;
147 else
148 escape = ESCAPE;
149 eofcount = 0;
150 hadintr = 0;
151 lastlong = 0;
152 longline = 0;
153
154 if (!setjmp(colljmp)) {
155 if (getsub)
156 grabh(hp, GSUBJECT);
157 } else {
158 /*
159 * Come here for printing the after-signal message.
160 * Duplicate messages won't be printed because
161 * the write is aborted if we get a SIGTTOU.
162 */
163 cont:
164 if (hadintr) {
165 fflush(stdout);
166 fprintf(stderr,
167 "\n(Interrupt -- one more to kill letter)\n");
168 } else {
169 printf("(continue)\n");
170 fflush(stdout);
171 }
172 }
173 for (;;) {
174 colljmp_p = 1;
175 c = readline(stdin, linebuf, LINESIZE);
176 colljmp_p = 0;
177 if (c < 0) {
178 if (value("interactive") != NOSTR &&
179 value("ignoreeof") != NOSTR && ++eofcount < 25) {
180 printf("Use \".\" to terminate letter\n");
181 continue;
182 }
183 break;
184 }
185 lastlong = longline;
186 longline = c == LINESIZE-1;
187 eofcount = 0;
188 hadintr = 0;
189 if (linebuf[0] == '.' && linebuf[1] == '\0' &&
190 value("interactive") != NOSTR && !lastlong &&
191 (value("dot") != NOSTR || value("ignoreeof") != NOSTR))
192 break;
193 if (linebuf[0] != escape || value("interactive") == NOSTR ||
194 lastlong) {
195 if (putline(collf, linebuf, !longline) < 0)
196 goto err;
197 continue;
198 }
199 c = linebuf[1];
200 switch (c) {
201 default:
202 /*
203 * On double escape, just send the single one.
204 * Otherwise, it's an error.
205 */
206 if (c == escape) {
207 if (putline(collf, &linebuf[1], !longline) < 0)
208 goto err;
209 else
210 break;
211 }
212 printf("Unknown tilde escape.\n");
213 break;
214 case 'C':
215 /*
216 * Dump core.
217 */
218 core(NULL);
219 break;
220 case '!':
221 /*
222 * Shell escape, send the balance of the
223 * line to sh -c.
224 */
225 shell(&linebuf[2]);
226 break;
227 case ':':
228 case '_':
229 /*
230 * Escape to command mode, but be nice!
231 */
232 execute(&linebuf[2], 1);
233 goto cont;
234 case '.':
235 /*
236 * Simulate end of file on input.
237 */
238 goto out;
239 case 'q':
240 /*
241 * Force a quit of sending mail.
242 * Act like an interrupt happened.
243 */
244 hadintr++;
245 collint(SIGINT);
246 exit(1);
247
248 case 'x': /* exit, do not save in dead.letter */
249 goto err;
250
251 case 'h':
252 /*
253 * Grab a bunch of headers.
254 */
255 grabh(hp, GTO|GSUBJECT|GCC|GBCC);
256 goto cont;
257 case 't':
258 /*
259 * Add to the To list.
260 */
261 hp->h_to = cat(hp->h_to, extract(&linebuf[2], GTO));
262 break;
263 case 's':
264 /*
265 * Set the Subject list.
266 */
267 cp = &linebuf[2];
268 while (isspace((unsigned char)*cp))
269 cp++;
270 hp->h_subject = savestr(cp);
271 break;
272 case 'c':
273 /*
274 * Add to the CC list.
275 */
276 hp->h_cc = cat(hp->h_cc, extract(&linebuf[2], GCC));
277 break;
278 case 'b':
279 /*
280 * Add stuff to blind carbon copies list.
281 */
282 hp->h_bcc = cat(hp->h_bcc, extract(&linebuf[2], GBCC));
283 break;
284 case 'i':
285 case 'A':
286 case 'a':
287 /*
288 * Insert named variable in message
289 */
290
291 switch(c) {
292 case 'i':
293 cp = &linebuf[2];
294 while(isspace((unsigned char) *cp))
295 cp++;
296
297 break;
298 case 'a':
299 cp = "sign";
300 break;
301 case 'A':
302 cp = "Sign";
303 break;
304 default:
305 goto err;
306 }
307
308 if(*cp && (cp = value(cp)) != NOSTR) {
309 printf("%s\n", cp);
310 if(putline(collf, cp, 1) < 0)
311 goto err;
312 }
313
314 break;
315
316 case 'd':
317 strcpy(linebuf + 2, getdeadletter());
318 /* fall into . . . */
319 case 'r':
320 case '<':
321 /*
322 * Invoke a file:
323 * Search for the file name,
324 * then open it and copy the contents to collf.
325 */
326 cp = &linebuf[2];
327 while (isspace((unsigned char)*cp))
328 cp++;
329 if (*cp == '\0') {
330 printf("Interpolate what file?\n");
331 break;
332 }
333
334 cp = expand(cp);
335 if (cp == NOSTR)
336 break;
337
338 if (*cp == '!') { /* insert stdout of command */
339 char *shell;
340 int nullfd;
341 int rc;
342
343 if((nullfd = open("/dev/null", O_RDONLY, 0)) == -1) {
344 perror("/dev/null");
345 break;
346 }
347
348 if ((fbuf = Fopen(tempEdit, "w+")) == NULL) {
349 perror(tempEdit);
350 break;
351 }
352 (void) unlink(tempEdit);
353
354 if ((shell = value("SHELL")) == NOSTR)
355 shell = _PATH_CSHELL;
356
357 rc = run_command(shell, 0, nullfd, fileno(fbuf), "-c", cp+1, NOSTR);
358
359 close(nullfd);
360
361 if (rc < 0) {
362 (void) Fclose(fbuf);
363 break;
364 }
365
366 if (fsize(fbuf) == 0) {
367 fprintf(stderr, "No bytes from command \"%s\"\n", cp+1);
368 (void) Fclose(fbuf);
369 break;
370 }
371
372 rewind(fbuf);
373 }
374 else if (isdir(cp)) {
375 printf("%s: Directory\n", cp);
376 break;
377 }
378 else if ((fbuf = Fopen(cp, "r")) == NULL) {
379 perror(cp);
380 break;
381 }
382 printf("\"%s\" ", cp);
383 fflush(stdout);
384 lc = 0;
385 cc = 0;
386 while ((rc = readline(fbuf, linebuf, LINESIZE)) >= 0) {
387 if (rc != LINESIZE-1) lc++;
388 if ((t = putline(collf, linebuf,
389 rc != LINESIZE-1)) < 0) {
390 Fclose(fbuf);
391 goto err;
392 }
393 cc += t;
394 }
395 Fclose(fbuf);
396 printf("%d/%d\n", lc, cc);
397 break;
398 case 'w':
399 /*
400 * Write the message on a file.
401 */
402 cp = &linebuf[2];
403 while (*cp == ' ' || *cp == '\t')
404 cp++;
405 if (*cp == '\0') {
406 fprintf(stderr, "Write what file!?\n");
407 break;
408 }
409 if ((cp = expand(cp)) == NOSTR)
410 break;
411 rewind(collf);
412 exwrite(cp, collf, 1);
413 break;
414 case 'm':
415 case 'M':
416 case 'f':
417 case 'F':
418 /*
419 * Interpolate the named messages, if we
420 * are in receiving mail mode. Does the
421 * standard list processing garbage.
422 * If ~f is given, we don't shift over.
423 */
424 if (forward(linebuf + 2, collf, c) < 0)
425 goto err;
426 goto cont;
427 case '?':
428 if ((fbuf = Fopen(_PATH_TILDE, "r")) == NULL) {
429 perror(_PATH_TILDE);
430 break;
431 }
432 while ((t = getc(fbuf)) != EOF)
433 (void) putchar(t);
434 Fclose(fbuf);
435 break;
436 case 'p':
437 /*
438 * Print out the current state of the
439 * message without altering anything.
440 */
441 rewind(collf);
442 printf("-------\nMessage contains:\n");
443 puthead(hp, stdout, GTO|GSUBJECT|GCC|GBCC|GNL);
444 while ((t = getc(collf)) != EOF)
445 (void) putchar(t);
446 goto cont;
447 case '|':
448 /*
449 * Pipe message through command.
450 * Collect output as new message.
451 */
452 rewind(collf);
453 mespipe(collf, &linebuf[2]);
454 goto cont;
455 case 'v':
456 case 'e':
457 /*
458 * Edit the current message.
459 * 'e' means to use EDITOR
460 * 'v' means to use VISUAL
461 */
462 rewind(collf);
463 mesedit(collf, c);
464 goto cont;
465 }
466 }
467 goto out;
468 err:
469 if (collf != NULL) {
470 Fclose(collf);
471 collf = NULL;
472 }
473 out:
474 if (collf != NULL)
475 rewind(collf);
476 noreset--;
477 sigprocmask(SIG_BLOCK, &nset, NULL);
478 signal(SIGINT, saveint);
479 signal(SIGHUP, savehup);
480 signal(SIGTSTP, savetstp);
481 signal(SIGTTOU, savettou);
482 signal(SIGTTIN, savettin);
483 sigprocmask(SIG_UNBLOCK, &nset, NULL);
484 return collf;
485 }
486
487 /*
488 * Write a file, ex-like if f set.
489 */
490 int
491 exwrite(name, fp, f)
492 char name[];
493 FILE *fp;
494 int f;
495 {
496 FILE *of;
497 int c;
498 long cc;
499 int lc;
500 struct stat junk;
501
502 if (f) {
503 printf("\"%s\" ", name);
504 fflush(stdout);
505 }
506 if (stat(name, &junk) >= 0 && S_ISREG(junk.st_mode)) {
507 if (!f)
508 fprintf(stderr, "%s: ", name);
509 fprintf(stderr, "File exists\n");
510 return(-1);
511 }
512 if ((of = Fopen(name, "w")) == NULL) {
513 perror(name);
514 return(-1);
515 }
516 lc = 0;
517 cc = 0;
518 while ((c = getc(fp)) != EOF) {
519 cc++;
520 if (c == '\n')
521 lc++;
522 (void) putc(c, of);
523 if (ferror(of)) {
524 perror(name);
525 Fclose(of);
526 return(-1);
527 }
528 }
529 Fclose(of);
530 printf("%d/%ld\n", lc, cc);
531 fflush(stdout);
532 return(0);
533 }
534
535 /*
536 * Edit the message being collected on fp.
537 * On return, make the edit file the new temp file.
538 */
539 void
540 mesedit(fp, c)
541 FILE *fp;
542 int c;
543 {
544 sig_t sigint = signal(SIGINT, SIG_IGN);
545 FILE *nf = run_editor(fp, (off_t)-1, c, 0);
546
547 if (nf != NULL) {
548 fseek(nf, 0L, 2);
549 collf = nf;
550 Fclose(fp);
551 }
552 (void) signal(SIGINT, sigint);
553 }
554
555 /*
556 * Pipe the message through the command.
557 * Old message is on stdin of command;
558 * New message collected from stdout.
559 * Sh -c must return 0 to accept the new message.
560 */
561 void
562 mespipe(fp, cmd)
563 FILE *fp;
564 char cmd[];
565 {
566 FILE *nf;
567 sig_t sigint = signal(SIGINT, SIG_IGN);
568 char *shell;
569
570 if ((nf = Fopen(tempEdit, "w+")) == NULL) {
571 perror(tempEdit);
572 goto out;
573 }
574 (void) unlink(tempEdit);
575 /*
576 * stdin = current message.
577 * stdout = new message.
578 */
579 if ((shell = value("SHELL")) == NOSTR)
580 shell = _PATH_CSHELL;
581 if (run_command(shell,
582 0, fileno(fp), fileno(nf), "-c", cmd, NOSTR) < 0) {
583 (void) Fclose(nf);
584 goto out;
585 }
586 if (fsize(nf) == 0) {
587 fprintf(stderr, "No bytes from \"%s\" !?\n", cmd);
588 (void) Fclose(nf);
589 goto out;
590 }
591 /*
592 * Take new files.
593 */
594 (void) fseek(nf, 0L, 2);
595 collf = nf;
596 (void) Fclose(fp);
597 out:
598 (void) signal(SIGINT, sigint);
599 }
600
601 /*
602 * Interpolate the named messages into the current
603 * message, preceding each line with a tab.
604 * Return a count of the number of characters now in
605 * the message, or -1 if an error is encountered writing
606 * the message temporary. The flag argument is 'm' if we
607 * should shift over and 'f' if not.
608 */
609 int
610 forward(ms, fp, f)
611 char ms[];
612 FILE *fp;
613 int f;
614 {
615 int *msgvec;
616 struct ignoretab *ig;
617 char *tabst;
618
619 msgvec = (int *) salloc((msgCount+1) * sizeof *msgvec);
620 if (msgvec == (int *) NOSTR)
621 return(0);
622 if (getmsglist(ms, msgvec, 0) < 0)
623 return(0);
624 if (*msgvec == 0) {
625 *msgvec = first(0, MMNORM);
626 if (*msgvec == 0) {
627 printf("No appropriate messages\n");
628 return(0);
629 }
630 msgvec[1] = 0;
631 }
632 if (f == 'f' || f == 'F')
633 tabst = NOSTR;
634 else if ((tabst = value("indentprefix")) == NOSTR)
635 tabst = "\t";
636 ig = isupper(f) ? NULL : ignore;
637 printf("Interpolating:");
638 for (; *msgvec != 0; msgvec++) {
639 struct message *mp = message + *msgvec - 1;
640
641 touch(mp);
642 printf(" %d", *msgvec);
643 if (sendmessage(mp, fp, ig, tabst) < 0) {
644 perror(tempMail);
645 return(-1);
646 }
647 }
648 printf("\n");
649 return(0);
650 }
651
652 /*
653 * Print (continue) when continued after ^Z.
654 */
655 /*ARGSUSED*/
656 void
657 collstop(s)
658 int s;
659 {
660 sig_t old_action = signal(s, SIG_DFL);
661 sigset_t nset;
662
663 sigemptyset(&nset);
664 sigaddset(&nset, s);
665 sigprocmask(SIG_UNBLOCK, &nset, NULL);
666 kill(0, s);
667 sigprocmask(SIG_BLOCK, &nset, NULL);
668 signal(s, old_action);
669 if (colljmp_p) {
670 colljmp_p = 0;
671 hadintr = 0;
672 longjmp(colljmp, 1);
673 }
674 }
675
676 /*
677 * On interrupt, come here to save the partial message in ~/dead.letter.
678 * Then jump out of the collection loop.
679 */
680 /*ARGSUSED*/
681 void
682 collint(s)
683 int s;
684 {
685 /*
686 * the control flow is subtle, because we can be called from ~q.
687 */
688 if (!hadintr) {
689 if (value("ignore") != NOSTR) {
690 puts("@");
691 fflush(stdout);
692 clearerr(stdin);
693 return;
694 }
695 hadintr = 1;
696 longjmp(colljmp, 1);
697 }
698 rewind(collf);
699 if (value("nosave") == NOSTR)
700 savedeadletter(collf);
701 longjmp(collabort, 1);
702 }
703
704 /*ARGSUSED*/
705 void
706 collhup(s)
707 int s;
708 {
709 rewind(collf);
710 savedeadletter(collf);
711 /*
712 * Let's pretend nobody else wants to clean up,
713 * a true statement at this time.
714 */
715 exit(1);
716 }
717
718 void
719 savedeadletter(fp)
720 FILE *fp;
721 {
722 FILE *dbuf;
723 int c;
724 char *cp;
725
726 if (fsize(fp) == 0)
727 return;
728 cp = getdeadletter();
729 c = umask(077);
730 dbuf = Fopen(cp, "a");
731 (void) umask(c);
732 if (dbuf == NULL)
733 return;
734 while ((c = getc(fp)) != EOF)
735 (void) putc(c, dbuf);
736 Fclose(dbuf);
737 rewind(fp);
738 }
739