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