]> diplodocus.org Git - nmh/blob - uip/mhshowsbr.c
Cope with sasl_decode64() returning SASL_CONTINUE as well as SASL_OK.
[nmh] / uip / mhshowsbr.c
1
2 /*
3 * mhshowsbr.c -- routines to display the contents of MIME messages
4 *
5 * $Id$
6 *
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
10 */
11
12 #include <h/mh.h>
13 #include <fcntl.h>
14 #include <h/signals.h>
15 #include <h/md5.h>
16 #include <errno.h>
17 #include <setjmp.h>
18 #include <signal.h>
19 #include <h/mts.h>
20 #include <h/tws.h>
21 #include <h/mime.h>
22 #include <h/mhparse.h>
23 #include <h/utils.h>
24
25 #ifdef HAVE_SYS_WAIT_H
26 # include <sys/wait.h>
27 #endif
28
29 /*
30 * Just use sigjmp/longjmp on older machines that
31 * don't have sigsetjmp/siglongjmp.
32 */
33 #ifndef HAVE_SIGSETJMP
34 # define sigjmp_buf jmp_buf
35 # define sigsetjmp(env,mask) setjmp(env)
36 # define siglongjmp(env,val) longjmp(env,val)
37 #endif
38
39 extern int debugsw;
40
41 int pausesw = 1;
42 int serialsw = 0;
43 int nolist = 0;
44
45 char *progsw = NULL;
46
47 /* flags for moreproc/header display */
48 int nomore = 0;
49 char *formsw = NULL;
50
51 pid_t xpid = 0;
52
53 static sigjmp_buf intrenv;
54
55
56 /* termsbr.c */
57 int SOprintf (char *, ...);
58
59 /* mhparse.c */
60 int pidcheck (int);
61
62 /* mhmisc.c */
63 int part_ok (CT, int);
64 int type_ok (CT, int);
65 void content_error (char *, CT, char *, ...);
66 void flush_errors (void);
67
68 /* mhlistsbr.c */
69 int list_switch (CT, int, int, int, int);
70 int list_content (CT, int, int, int, int);
71
72 /*
73 * prototypes
74 */
75 void show_all_messages (CT *);
76 int show_content_aux (CT, int, int, char *, char *);
77
78 /*
79 * static prototypes
80 */
81 static void show_single_message (CT, char *);
82 static void DisplayMsgHeader (CT, char *);
83 static int show_switch (CT, int, int);
84 static int show_content (CT, int, int);
85 static int show_content_aux2 (CT, int, int, char *, char *, int, int, int, int, int);
86 static int show_text (CT, int, int);
87 static int show_multi (CT, int, int);
88 static int show_multi_internal (CT, int, int);
89 static int show_multi_aux (CT, int, int, char *);
90 static int show_message_rfc822 (CT, int, int);
91 static int show_partial (CT, int, int);
92 static int show_external (CT, int, int);
93 static RETSIGTYPE intrser (int);
94
95
96 /*
97 * Top level entry point to show/display a group of messages
98 */
99
100 void
101 show_all_messages (CT *cts)
102 {
103 CT ct, *ctp;
104
105 /*
106 * If form is not specified, then get default form
107 * for showing headers of MIME messages.
108 */
109 if (!formsw)
110 formsw = getcpy (etcpath ("mhl.headers"));
111
112 /*
113 * If form is "mhl.null", suppress display of header.
114 */
115 if (!strcmp (formsw, "mhl.null"))
116 formsw = NULL;
117
118 for (ctp = cts; *ctp; ctp++) {
119 ct = *ctp;
120
121 /* if top-level type is ok, then display message */
122 if (type_ok (ct, 1))
123 show_single_message (ct, formsw);
124 }
125 }
126
127
128 /*
129 * Entry point to show/display a single message
130 */
131
132 static void
133 show_single_message (CT ct, char *form)
134 {
135 sigset_t set, oset;
136
137 #ifdef HAVE_UNION_WAIT
138 union wait status;
139 #else
140 int status;
141 #endif
142
143 /* Allow user executable bit so that temporary directories created by
144 * the viewer (e.g., lynx) are going to be accessible */
145 umask (ct->c_umask & ~(0100));
146
147 /*
148 * If you have a format file, then display
149 * the message headers.
150 */
151 if (form)
152 DisplayMsgHeader(ct, form);
153 else
154 xpid = 0;
155
156 /* Show the body of the message */
157 show_switch (ct, 1, 0);
158
159 if (ct->c_fp) {
160 fclose (ct->c_fp);
161 ct->c_fp = NULL;
162 }
163 if (ct->c_ceclosefnx)
164 (*ct->c_ceclosefnx) (ct);
165
166 /* block a few signals */
167 sigemptyset (&set);
168 sigaddset (&set, SIGHUP);
169 sigaddset (&set, SIGINT);
170 sigaddset (&set, SIGQUIT);
171 sigaddset (&set, SIGTERM);
172 SIGPROCMASK (SIG_BLOCK, &set, &oset);
173
174 while (wait (&status) != NOTOK) {
175 #ifdef HAVE_UNION_WAIT
176 pidcheck (status.w_status);
177 #else
178 pidcheck (status);
179 #endif
180 continue;
181 }
182
183 /* reset the signal mask */
184 SIGPROCMASK (SIG_SETMASK, &oset, &set);
185
186 xpid = 0;
187 flush_errors ();
188 }
189
190
191 /*
192 * Use the mhlproc to show the header fields
193 */
194
195 static void
196 DisplayMsgHeader (CT ct, char *form)
197 {
198 pid_t child_id;
199 int i, vecp;
200 char *vec[8];
201
202 vecp = 0;
203 vec[vecp++] = r1bindex (mhlproc, '/');
204 vec[vecp++] = "-form";
205 vec[vecp++] = form;
206 vec[vecp++] = "-nobody";
207 vec[vecp++] = ct->c_file;
208
209 /*
210 * If we've specified -(no)moreproc,
211 * then just pass that along.
212 */
213 if (nomore) {
214 vec[vecp++] = "-nomoreproc";
215 } else if (progsw) {
216 vec[vecp++] = "-moreproc";
217 vec[vecp++] = progsw;
218 }
219 vec[vecp] = NULL;
220
221 fflush (stdout);
222
223 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
224 sleep (5);
225
226 switch (child_id) {
227 case NOTOK:
228 adios ("fork", "unable to");
229 /* NOTREACHED */
230
231 case OK:
232 execvp (mhlproc, vec);
233 fprintf (stderr, "unable to exec ");
234 perror (mhlproc);
235 _exit (-1);
236 /* NOTREACHED */
237
238 default:
239 xpid = -child_id;
240 break;
241 }
242 }
243
244
245 /*
246 * Switching routine. Call the correct routine
247 * based on content type.
248 */
249
250 static int
251 show_switch (CT ct, int serial, int alternate)
252 {
253 switch (ct->c_type) {
254 case CT_MULTIPART:
255 return show_multi (ct, serial, alternate);
256 break;
257
258 case CT_MESSAGE:
259 switch (ct->c_subtype) {
260 case MESSAGE_PARTIAL:
261 return show_partial (ct, serial, alternate);
262 break;
263
264 case MESSAGE_EXTERNAL:
265 return show_external (ct, serial, alternate);
266 break;
267
268 case MESSAGE_RFC822:
269 default:
270 return show_message_rfc822 (ct, serial, alternate);
271 break;
272 }
273 break;
274
275 case CT_TEXT:
276 return show_text (ct, serial, alternate);
277 break;
278
279 case CT_AUDIO:
280 case CT_IMAGE:
281 case CT_VIDEO:
282 case CT_APPLICATION:
283 return show_content (ct, serial, alternate);
284 break;
285
286 default:
287 adios (NULL, "unknown content type %d", ct->c_type);
288 break;
289 }
290
291 return 0; /* NOT REACHED */
292 }
293
294
295 /*
296 * Generic method for displaying content
297 */
298
299 static int
300 show_content (CT ct, int serial, int alternate)
301 {
302 char *cp, buffer[BUFSIZ];
303 CI ci = &ct->c_ctinfo;
304
305 /* Check for mhn-show-type/subtype */
306 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
307 invo_name, ci->ci_type, ci->ci_subtype);
308 if ((cp = context_find (buffer)) && *cp != '\0')
309 return show_content_aux (ct, serial, alternate, cp, NULL);
310
311 /* Check for mhn-show-type */
312 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
313 if ((cp = context_find (buffer)) && *cp != '\0')
314 return show_content_aux (ct, serial, alternate, cp, NULL);
315
316 if ((cp = ct->c_showproc))
317 return show_content_aux (ct, serial, alternate, cp, NULL);
318
319 /* complain if we are not a part of a multipart/alternative */
320 if (!alternate)
321 content_error (NULL, ct, "don't know how to display content");
322
323 return NOTOK;
324 }
325
326
327 /*
328 * Parse the display string for displaying generic content
329 */
330
331 int
332 show_content_aux (CT ct, int serial, int alternate, char *cp, char *cracked)
333 {
334 int fd, len, buflen, quoted;
335 int xstdin, xlist, xpause, xtty;
336 char *bp, *pp, *file, buffer[BUFSIZ];
337 CI ci = &ct->c_ctinfo;
338
339 if (!ct->c_ceopenfnx) {
340 if (!alternate)
341 content_error (NULL, ct, "don't know how to decode content");
342
343 return NOTOK;
344 }
345
346 file = NULL;
347 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
348 return NOTOK;
349 if (ct->c_showproc && !strcmp (ct->c_showproc, "true"))
350 return (alternate ? DONE : OK);
351
352 xlist = 0;
353 xpause = 0;
354 xstdin = 0;
355 xtty = 0;
356
357 if (cracked) {
358 strncpy (buffer, cp, sizeof(buffer));
359 goto got_command;
360 }
361
362 /* get buffer ready to go */
363 bp = buffer;
364 buflen = sizeof(buffer) - 1;
365 bp[0] = bp[buflen] = '\0';
366 quoted = 0;
367
368 /* Now parse display string */
369 for ( ; *cp && buflen > 0; cp++) {
370 if (*cp == '%') {
371 pp = bp;
372
373 switch (*++cp) {
374 case 'a':
375 /* insert parameters from Content-Type field */
376 {
377 char **ap, **ep;
378 char *s = "";
379
380 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
381 snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
382 len = strlen (bp);
383 bp += len;
384 buflen -= len;
385 s = " ";
386 }
387 }
388 break;
389
390 case 'd':
391 /* insert content description */
392 if (ct->c_descr) {
393 char *s;
394
395 s = trimcpy (ct->c_descr);
396 strncpy (bp, s, buflen);
397 free (s);
398 }
399 break;
400
401 case 'e':
402 /* exclusive execution */
403 xtty = 1;
404 break;
405
406 case 'F':
407 /* %e, %f, and stdin is terminal not content */
408 xstdin = 1;
409 xtty = 1;
410 /* and fall... */
411
412 case 'f':
413 /* insert filename containing content */
414 snprintf (bp, buflen, "'%s'", file);
415 /* since we've quoted the file argument, set things up
416 * to look past it, to avoid problems with the quoting
417 * logic below. (I know, I should figure out what's
418 * broken with the quoting logic, but..)
419 */
420 len = strlen(bp);
421 buflen -= len;
422 bp += len;
423 pp = bp;
424 break;
425
426 case 'p':
427 /* %l, and pause prior to displaying content */
428 xpause = pausesw;
429 /* and fall... */
430
431 case 'l':
432 /* display listing prior to displaying content */
433 xlist = !nolist;
434 break;
435
436 case 's':
437 /* insert subtype of content */
438 strncpy (bp, ci->ci_subtype, buflen);
439 break;
440
441 case '%':
442 /* insert character % */
443 goto raw;
444
445 default:
446 *bp++ = *--cp;
447 *bp = '\0';
448 buflen--;
449 continue;
450 }
451 len = strlen (bp);
452 bp += len;
453 buflen -= len;
454
455 /* Did we actually insert something? */
456 if (bp != pp) {
457 /* Insert single quote if not inside quotes already */
458 if (!quoted && buflen) {
459 len = strlen (pp);
460 memmove (pp + 1, pp, len);
461 *pp++ = '\'';
462 buflen--;
463 bp++;
464 }
465 /* Escape existing quotes */
466 while ((pp = strchr (pp, '\'')) && buflen > 3) {
467 len = strlen (pp++);
468 memmove (pp + 3, pp, len);
469 *pp++ = '\\';
470 *pp++ = '\'';
471 *pp++ = '\'';
472 buflen -= 3;
473 bp += 3;
474 }
475 /* If pp is still set, that means we ran out of space. */
476 if (pp)
477 buflen = 0;
478 if (!quoted && buflen) {
479 *bp++ = '\'';
480 *bp = '\0';
481 buflen--;
482 }
483 }
484 } else {
485 raw:
486 *bp++ = *cp;
487 *bp = '\0';
488 buflen--;
489
490 if (*cp == '\'')
491 quoted = !quoted;
492 }
493 }
494
495 if (buflen <= 0 || (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
496 /* content_error would provide a more useful error message
497 * here, except that if we got overrun, it probably would
498 * too.
499 */
500 fprintf(stderr, "Buffer overflow constructing show command!\n");
501 return NOTOK;
502 }
503
504 /* use charset string to modify display method */
505 if (ct->c_termproc) {
506 char term[BUFSIZ];
507
508 strncpy (term, buffer, sizeof(term));
509 snprintf (buffer, sizeof(buffer), ct->c_termproc, term);
510 }
511
512 got_command:
513 return show_content_aux2 (ct, serial, alternate, cracked, buffer,
514 fd, xlist, xpause, xstdin, xtty);
515 }
516
517
518 /*
519 * Routine to actually display the content
520 */
521
522 static int
523 show_content_aux2 (CT ct, int serial, int alternate, char *cracked, char *buffer,
524 int fd, int xlist, int xpause, int xstdin, int xtty)
525 {
526 pid_t child_id;
527 int i;
528 char *vec[4], exec[BUFSIZ + sizeof "exec "];
529
530 if (debugsw || cracked) {
531 fflush (stdout);
532
533 fprintf (stderr, "%s msg %s", cracked ? "storing" : "show",
534 ct->c_file);
535 if (ct->c_partno)
536 fprintf (stderr, " part %s", ct->c_partno);
537 if (cracked)
538 fprintf (stderr, " using command (cd %s; %s)\n", cracked, buffer);
539 else
540 fprintf (stderr, " using command %s\n", buffer);
541 }
542
543 if (xpid < 0 || (xtty && xpid)) {
544 if (xpid < 0)
545 xpid = -xpid;
546 pidcheck(pidwait (xpid, NOTOK));
547 xpid = 0;
548 }
549
550 if (xlist) {
551 char prompt[BUFSIZ];
552
553 if (ct->c_type == CT_MULTIPART)
554 list_content (ct, -1, 1, 0, 0);
555 else
556 list_switch (ct, -1, 1, 0, 0);
557
558 if (xpause && SOprintf ("Press <return> to show content..."))
559 printf ("Press <return> to show content...");
560
561 if (xpause) {
562 int intr;
563 SIGNAL_HANDLER istat;
564
565 istat = SIGNAL (SIGINT, intrser);
566 if ((intr = sigsetjmp (intrenv, 1)) == OK) {
567 fflush (stdout);
568 prompt[0] = 0;
569 read (fileno (stdout), prompt, sizeof(prompt));
570 }
571 SIGNAL (SIGINT, istat);
572 if (intr != OK || prompt[0] == 'n') {
573 (*ct->c_ceclosefnx) (ct);
574 return (alternate ? DONE : NOTOK);
575 }
576 if (prompt[0] == 'q') done(OK);
577 }
578 }
579
580 snprintf (exec, sizeof(exec), "exec %s", buffer);
581
582 vec[0] = "/bin/sh";
583 vec[1] = "-c";
584 vec[2] = exec;
585 vec[3] = NULL;
586
587 fflush (stdout);
588
589 for (i = 0; (child_id = vfork ()) == NOTOK && i < 5; i++)
590 sleep (5);
591 switch (child_id) {
592 case NOTOK:
593 advise ("fork", "unable to");
594 (*ct->c_ceclosefnx) (ct);
595 return NOTOK;
596
597 case OK:
598 if (cracked)
599 chdir (cracked);
600 if (!xstdin)
601 dup2 (fd, 0);
602 close (fd);
603 execvp ("/bin/sh", vec);
604 fprintf (stderr, "unable to exec ");
605 perror ("/bin/sh");
606 _exit (-1);
607 /* NOTREACHED */
608
609 default:
610 if (!serial) {
611 ct->c_pid = child_id;
612 if (xtty)
613 xpid = child_id;
614 } else {
615 pidcheck (pidXwait (child_id, NULL));
616 }
617
618 if (fd != NOTOK)
619 (*ct->c_ceclosefnx) (ct);
620 return (alternate ? DONE : OK);
621 }
622 }
623
624
625 /*
626 * show content of type "text"
627 */
628
629 static int
630 show_text (CT ct, int serial, int alternate)
631 {
632 char *cp, buffer[BUFSIZ];
633 CI ci = &ct->c_ctinfo;
634
635 /* Check for mhn-show-type/subtype */
636 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
637 invo_name, ci->ci_type, ci->ci_subtype);
638 if ((cp = context_find (buffer)) && *cp != '\0')
639 return show_content_aux (ct, serial, alternate, cp, NULL);
640
641 /* Check for mhn-show-type */
642 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
643 if ((cp = context_find (buffer)) && *cp != '\0')
644 return show_content_aux (ct, serial, alternate, cp, NULL);
645
646 /*
647 * Use default method if content is text/plain, or if
648 * if it is not a text part of a multipart/alternative
649 */
650 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
651 snprintf (buffer, sizeof(buffer), "%%p%s '%%F'", progsw ? progsw :
652 moreproc && *moreproc ? moreproc : "more");
653 cp = (ct->c_showproc = add (buffer, NULL));
654 return show_content_aux (ct, serial, alternate, cp, NULL);
655 }
656
657 return NOTOK;
658 }
659
660
661 /*
662 * show message body of type "multipart"
663 */
664
665 static int
666 show_multi (CT ct, int serial, int alternate)
667 {
668 char *cp, buffer[BUFSIZ];
669 CI ci = &ct->c_ctinfo;
670
671 /* Check for mhn-show-type/subtype */
672 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
673 invo_name, ci->ci_type, ci->ci_subtype);
674 if ((cp = context_find (buffer)) && *cp != '\0')
675 return show_multi_aux (ct, serial, alternate, cp);
676
677 /* Check for mhn-show-type */
678 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
679 if ((cp = context_find (buffer)) && *cp != '\0')
680 return show_multi_aux (ct, serial, alternate, cp);
681
682 if ((cp = ct->c_showproc))
683 return show_multi_aux (ct, serial, alternate, cp);
684
685 /*
686 * Use default method to display this multipart content
687 * if it is not a (nested) part of a multipart/alternative,
688 * or if it is one of the known subtypes of multipart.
689 */
690 if (!alternate || ct->c_subtype != MULTI_UNKNOWN)
691 return show_multi_internal (ct, serial, alternate);
692
693 return NOTOK;
694 }
695
696
697 /*
698 * show message body of subtypes of multipart that
699 * we understand directly (mixed, alternate, etc...)
700 */
701
702 static int
703 show_multi_internal (CT ct, int serial, int alternate)
704 {
705 int alternating, nowalternate, nowserial, result;
706 struct multipart *m = (struct multipart *) ct->c_ctparams;
707 struct part *part;
708 CT p;
709 sigset_t set, oset;
710
711 alternating = 0;
712 nowalternate = alternate;
713
714 if (ct->c_subtype == MULTI_PARALLEL) {
715 nowserial = serialsw;
716 } else if (ct->c_subtype == MULTI_ALTERNATE) {
717 nowalternate = 1;
718 alternating = 1;
719 nowserial = serial;
720 } else {
721 /*
722 * multipart/mixed
723 * mutlipart/digest
724 * unknown subtypes of multipart (treat as mixed per rfc2046)
725 */
726 nowserial = serial;
727 }
728
729 /* block a few signals */
730 if (!nowserial) {
731 sigemptyset (&set);
732 sigaddset (&set, SIGHUP);
733 sigaddset (&set, SIGINT);
734 sigaddset (&set, SIGQUIT);
735 sigaddset (&set, SIGTERM);
736 SIGPROCMASK (SIG_BLOCK, &set, &oset);
737 }
738
739 /*
740 * alternate -> we are a part inside an multipart/alternative
741 * alternating -> we are a multipart/alternative
742 */
743
744 result = alternate ? NOTOK : OK;
745
746 for (part = m->mp_parts; part; part = part->mp_next) {
747 p = part->mp_part;
748
749 if (part_ok (p, 0) && type_ok (p, 0)) {
750 int inneresult;
751
752 inneresult = show_switch (p, nowserial, nowalternate);
753 switch (inneresult) {
754 case NOTOK:
755 if (alternate && !alternating) {
756 result = NOTOK;
757 goto out;
758 }
759 continue;
760
761 case OK:
762 case DONE:
763 if (alternating) {
764 result = DONE;
765 break;
766 }
767 if (alternate) {
768 alternate = nowalternate = 0;
769 if (result == NOTOK)
770 result = inneresult;
771 }
772 continue;
773 }
774 break;
775 }
776 }
777
778 if (alternating && !part) {
779 if (!alternate)
780 content_error (NULL, ct, "don't know how to display any of the contents");
781 result = NOTOK;
782 goto out;
783 }
784
785 if (serial && !nowserial) {
786 pid_t pid;
787 int kids;
788 #ifdef HAVE_UNION_WAIT
789 union wait status;
790 #else
791 int status;
792 #endif
793
794 kids = 0;
795 for (part = m->mp_parts; part; part = part->mp_next) {
796 p = part->mp_part;
797
798 if (p->c_pid > OK) {
799 if (kill (p->c_pid, 0) == NOTOK)
800 p->c_pid = 0;
801 else
802 kids++;
803 }
804 }
805
806 while (kids > 0 && (pid = wait (&status)) != NOTOK) {
807 #ifdef HAVE_UNION_WAIT
808 pidcheck (status.w_status);
809 #else
810 pidcheck (status);
811 #endif
812
813 for (part = m->mp_parts; part; part = part->mp_next) {
814 p = part->mp_part;
815
816 if (xpid == pid)
817 xpid = 0;
818 if (p->c_pid == pid) {
819 p->c_pid = 0;
820 kids--;
821 break;
822 }
823 }
824 }
825 }
826
827 out:
828 if (!nowserial) {
829 /* reset the signal mask */
830 SIGPROCMASK (SIG_SETMASK, &oset, &set);
831 }
832
833 return result;
834 }
835
836
837 /*
838 * Parse display string for multipart content
839 * and use external program to display it.
840 */
841
842 static int
843 show_multi_aux (CT ct, int serial, int alternate, char *cp)
844 {
845 int len, buflen, quoted;
846 int xlist, xpause, xtty;
847 char *bp, *pp, *file, buffer[BUFSIZ];
848 struct multipart *m = (struct multipart *) ct->c_ctparams;
849 struct part *part;
850 CI ci = &ct->c_ctinfo;
851 CT p;
852
853 for (part = m->mp_parts; part; part = part->mp_next) {
854 p = part->mp_part;
855
856 if (!p->c_ceopenfnx) {
857 if (!alternate)
858 content_error (NULL, p, "don't know how to decode content");
859 return NOTOK;
860 }
861
862 if (p->c_storage == NULL) {
863 file = NULL;
864 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
865 return NOTOK;
866
867 /* I'm not sure if this is necessary? */
868 p->c_storage = add (file, NULL);
869
870 if (p->c_showproc && !strcmp (p->c_showproc, "true"))
871 return (alternate ? DONE : OK);
872 (*p->c_ceclosefnx) (p);
873 }
874 }
875
876 xlist = 0;
877 xpause = 0;
878 xtty = 0;
879
880 /* get buffer ready to go */
881 bp = buffer;
882 buflen = sizeof(buffer) - 1;
883 bp[0] = bp[buflen] = '\0';
884 quoted = 0;
885
886 /* Now parse display string */
887 for ( ; *cp && buflen > 0; cp++) {
888 if (*cp == '%') {
889 pp = bp;
890 switch (*++cp) {
891 case 'a':
892 /* insert parameters from Content-Type field */
893 {
894 char **ap, **ep;
895 char *s = "";
896
897 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
898 snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
899 len = strlen (bp);
900 bp += len;
901 buflen -= len;
902 s = " ";
903 }
904 }
905 break;
906
907 case 'd':
908 /* insert content description */
909 if (ct->c_descr) {
910 char *s;
911
912 s = trimcpy (ct->c_descr);
913 strncpy (bp, s, buflen);
914 free (s);
915 }
916 break;
917
918 case 'e':
919 /* exclusive execution */
920 xtty = 1;
921 break;
922
923 case 'F':
924 /* %e and %f */
925 xtty = 1;
926 /* and fall... */
927
928 case 'f':
929 /* insert filename(s) containing content */
930 {
931 char *s = "";
932
933 for (part = m->mp_parts; part; part = part->mp_next) {
934 p = part->mp_part;
935
936 snprintf (bp, buflen, "%s'%s'", s, p->c_storage);
937 len = strlen (bp);
938 bp += len;
939 buflen -= len;
940 s = " ";
941 }
942 /* set our starting pointer back to bp, to avoid
943 * requoting the filenames we just added
944 */
945 pp = bp;
946 }
947 break;
948
949 case 'p':
950 /* %l, and pause prior to displaying content */
951 xpause = pausesw;
952 /* and fall... */
953
954 case 'l':
955 /* display listing prior to displaying content */
956 xlist = !nolist;
957 break;
958
959 case 's':
960 /* insert subtype of content */
961 strncpy (bp, ci->ci_subtype, buflen);
962 break;
963
964 case '%':
965 /* insert character % */
966 goto raw;
967
968 default:
969 *bp++ = *--cp;
970 *bp = '\0';
971 buflen--;
972 continue;
973 }
974 len = strlen (bp);
975 bp += len;
976 buflen -= len;
977
978 /* Did we actually insert something? */
979 if (bp != pp) {
980 /* Insert single quote if not inside quotes already */
981 if (!quoted && buflen) {
982 len = strlen (pp);
983 memmove (pp + 1, pp, len);
984 *pp++ = '\'';
985 buflen--;
986 bp++;
987 }
988 /* Escape existing quotes */
989 while ((pp = strchr (pp, '\'')) && buflen > 3) {
990 len = strlen (pp++);
991 memmove (pp + 3, pp, len);
992 *pp++ = '\\';
993 *pp++ = '\'';
994 *pp++ = '\'';
995 buflen -= 3;
996 bp += 3;
997 }
998 /* If pp is still set, that means we ran out of space. */
999 if (pp)
1000 buflen = 0;
1001 if (!quoted && buflen) {
1002 *bp++ = '\'';
1003 *bp = '\0';
1004 buflen--;
1005 }
1006 }
1007 } else {
1008 raw:
1009 *bp++ = *cp;
1010 *bp = '\0';
1011 buflen--;
1012
1013 if (*cp == '\'')
1014 quoted = !quoted;
1015 }
1016 }
1017
1018 if (buflen <= 0 || (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
1019 /* content_error would provide a more useful error message
1020 * here, except that if we got overrun, it probably would
1021 * too.
1022 */
1023 fprintf(stderr, "Buffer overflow constructing show command!\n");
1024 return NOTOK;
1025 }
1026
1027 /* use charset string to modify display method */
1028 if (ct->c_termproc) {
1029 char term[BUFSIZ];
1030
1031 strncpy (term, buffer, sizeof(term));
1032 snprintf (buffer, sizeof(buffer), ct->c_termproc, term);
1033 }
1034
1035 return show_content_aux2 (ct, serial, alternate, NULL, buffer,
1036 NOTOK, xlist, xpause, 0, xtty);
1037 }
1038
1039
1040 /*
1041 * show content of type "message/rfc822"
1042 */
1043
1044 static int
1045 show_message_rfc822 (CT ct, int serial, int alternate)
1046 {
1047 char *cp, buffer[BUFSIZ];
1048 CI ci = &ct->c_ctinfo;
1049
1050 /* Check for mhn-show-type/subtype */
1051 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
1052 invo_name, ci->ci_type, ci->ci_subtype);
1053 if ((cp = context_find (buffer)) && *cp != '\0')
1054 return show_content_aux (ct, serial, alternate, cp, NULL);
1055
1056 /* Check for mhn-show-type */
1057 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
1058 if ((cp = context_find (buffer)) && *cp != '\0')
1059 return show_content_aux (ct, serial, alternate, cp, NULL);
1060
1061 if ((cp = ct->c_showproc))
1062 return show_content_aux (ct, serial, alternate, cp, NULL);
1063
1064 /* default method for message/rfc822 */
1065 if (ct->c_subtype == MESSAGE_RFC822) {
1066 cp = (ct->c_showproc = add ("%pshow -file '%F'", NULL));
1067 return show_content_aux (ct, serial, alternate, cp, NULL);
1068 }
1069
1070 /* complain if we are not a part of a multipart/alternative */
1071 if (!alternate)
1072 content_error (NULL, ct, "don't know how to display content");
1073
1074 return NOTOK;
1075 }
1076
1077
1078 /*
1079 * Show content of type "message/partial".
1080 */
1081
1082 static int
1083 show_partial (CT ct, int serial, int alternate)
1084 {
1085 content_error (NULL, ct,
1086 "in order to display this message, you must reassemble it");
1087 return NOTOK;
1088 }
1089
1090
1091 /*
1092 * Show content of type "message/external".
1093 *
1094 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1095 */
1096
1097 static int
1098 show_external (CT ct, int serial, int alternate)
1099 {
1100 struct exbody *e = (struct exbody *) ct->c_ctparams;
1101 CT p = e->eb_content;
1102
1103 if (!type_ok (p, 0))
1104 return OK;
1105
1106 return show_switch (p, serial, alternate);
1107
1108 #if 0
1109 content_error (NULL, p, "don't know how to display content");
1110 return NOTOK;
1111 #endif
1112 }
1113
1114
1115 static RETSIGTYPE
1116 intrser (int i)
1117 {
1118 #ifndef RELIABLE_SIGNALS
1119 SIGNAL (SIGINT, intrser);
1120 #endif
1121
1122 putchar ('\n');
1123 siglongjmp (intrenv, DONE);
1124 }