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