71f55a4335d9afbf423124c83256767e7b594166
[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, quoted;
329     int xstdin, xlist, xpause, xtty;
330     char *bp, *pp, *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     buflen = sizeof(buffer) - 1;
359     bp[0] = bp[buflen] = '\0';
360     quoted = 0;
361
362     /* Now parse display string */
363     for ( ; *cp && buflen > 0; cp++) {
364         if (*cp == '%') {
365             pp = bp;
366
367             switch (*++cp) {
368             case 'a':
369                 /* insert parameters from Content-Type field */
370             {
371                 char **ap, **ep;
372                 char *s = "";
373
374                 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
375                     snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
376                     len = strlen (bp);
377                     bp += len;
378                     buflen -= len;
379                     s = " ";
380                 }
381             }
382             break;
383
384             case 'd':
385                 /* insert content description */
386                 if (ct->c_descr) {
387                     char *s;
388
389                     s = trimcpy (ct->c_descr);
390                     strncpy (bp, s, buflen);
391                     free (s);
392                 }
393                 break;
394
395             case 'e':
396                 /* exclusive execution */
397                 xtty = 1;
398                 break;
399
400             case 'F':
401                 /* %e, %f, and stdin is terminal not content */
402                 xstdin = 1;
403                 xtty = 1;
404                 /* and fall... */
405
406             case 'f':
407                 /* insert filename containing content */
408                 snprintf (bp, buflen, "'%s'", file);
409                 /* since we've quoted the file argument, set things up
410                  * to look past it, to avoid problems with the quoting
411                  * logic below.  (I know, I should figure out what's
412                  * broken with the quoting logic, but..)
413                  */
414                 len = strlen(bp);
415                 buflen -= len;
416                 bp += len;
417                 pp = bp;
418                 break;
419
420             case 'p':
421                 /* %l, and pause prior to displaying content */
422                 xpause = pausesw;
423                 /* and fall... */
424
425             case 'l':
426                 /* display listing prior to displaying content */
427                 xlist = !nolist;
428                 break;
429
430             case 's':
431                 /* insert subtype of content */
432                 strncpy (bp, ci->ci_subtype, buflen);
433                 break;
434
435             case '%':
436                 /* insert character % */
437                 goto raw;
438
439             default:
440                 *bp++ = *--cp;
441                 *bp = '\0';
442                 buflen--;
443                 continue;
444             }
445             len = strlen (bp);
446             bp += len;
447             buflen -= len;
448
449             /* Did we actually insert something? */
450             if (bp != pp) {
451                 /* Insert single quote if not inside quotes already */
452                 if (!quoted && buflen) {
453                     len = strlen (pp);
454                     memmove (pp + 1, pp, len);
455                     *pp++ = '\'';
456                     buflen--;
457                     bp++;
458                 }
459                 /* Escape existing quotes */
460                 while ((pp = strchr (pp, '\'')) && buflen > 3) {
461                     len = strlen (pp++);
462                     memmove (pp + 3, pp, len);
463                     *pp++ = '\\';
464                     *pp++ = '\'';
465                     *pp++ = '\'';
466                     buflen -= 3;
467                     bp += 3;
468                 }
469                 /* If pp is still set, that means we ran out of space. */
470                 if (pp)
471                     buflen = 0;
472                 if (!quoted && buflen) {
473                     *bp++ = '\'';
474                     *bp = '\0';
475                     buflen--;
476                 }
477             }
478         } else {
479 raw:
480             *bp++ = *cp;
481             *bp = '\0';
482             buflen--;
483
484             if (*cp == '\'')
485                 quoted = !quoted;
486         }
487     }
488
489     if (buflen <= 0 || (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
490         /* content_error would provide a more useful error message
491          * here, except that if we got overrun, it probably would
492          * too.
493          */
494         fprintf(stderr, "Buffer overflow constructing show command!\n");
495         return NOTOK;
496     }
497
498     /* use charset string to modify display method */
499     if (ct->c_termproc) {
500         char term[BUFSIZ];
501
502         strncpy (term, buffer, sizeof(term));
503         snprintf (buffer, sizeof(buffer), ct->c_termproc, term);
504     }
505
506 got_command:
507     return show_content_aux2 (ct, serial, alternate, cracked, buffer,
508                               fd, xlist, xpause, xstdin, xtty);
509 }
510
511
512 /*
513  * Routine to actually display the content
514  */
515
516 static int
517 show_content_aux2 (CT ct, int serial, int alternate, char *cracked, char *buffer,
518                    int fd, int xlist, int xpause, int xstdin, int xtty)
519 {
520     pid_t child_id;
521     int i;
522     char *vec[4], exec[BUFSIZ + sizeof "exec "];
523     
524     if (debugsw || cracked) {
525         fflush (stdout);
526
527         fprintf (stderr, "%s msg %s", cracked ? "storing" : "show",
528                  ct->c_file);
529         if (ct->c_partno)
530             fprintf (stderr, " part %s", ct->c_partno);
531         if (cracked)
532             fprintf (stderr, " using command (cd %s; %s)\n", cracked, buffer);
533         else
534             fprintf (stderr, " using command %s\n", buffer);
535     }
536
537     if (xpid < 0 || (xtty && xpid)) {
538         if (xpid < 0)
539             xpid = -xpid;
540         pidcheck(pidwait (xpid, NOTOK));
541         xpid = 0;
542     }
543
544     if (xlist) {
545         char prompt[BUFSIZ];
546
547         if (ct->c_type == CT_MULTIPART)
548             list_content (ct, -1, 1, 0, 0);
549         else
550             list_switch (ct, -1, 1, 0, 0);
551
552         if (xpause && SOprintf ("Press <return> to show content..."))
553             printf ("Press <return> to show content...");
554
555         if (xpause) {
556             int intr;
557             SIGNAL_HANDLER istat;
558
559             istat = SIGNAL (SIGINT, intrser);
560             if ((intr = sigsetjmp (intrenv, 1)) == OK) {
561                 fflush (stdout);
562                 prompt[0] = 0;
563                 read (fileno (stdout), prompt, sizeof(prompt));
564             }
565             SIGNAL (SIGINT, istat);
566             if (intr != OK || prompt[0] == 'n') {
567                 (*ct->c_ceclosefnx) (ct);
568                 return (alternate ? DONE : NOTOK);
569             }
570             if (prompt[0] == 'q') done(OK);
571         }
572     }
573
574     snprintf (exec, sizeof(exec), "exec %s", buffer);
575
576     vec[0] = "/bin/sh";
577     vec[1] = "-c";
578     vec[2] = exec;
579     vec[3] = NULL;
580
581     fflush (stdout);
582
583     for (i = 0; (child_id = vfork ()) == NOTOK && i < 5; i++)
584         sleep (5);
585     switch (child_id) {
586         case NOTOK:
587             advise ("fork", "unable to");
588             (*ct->c_ceclosefnx) (ct);
589             return NOTOK;
590
591         case OK:
592             if (cracked)
593                 chdir (cracked);
594             if (!xstdin)
595                 dup2 (fd, 0);
596             close (fd);
597             execvp ("/bin/sh", vec);
598             fprintf (stderr, "unable to exec ");
599             perror ("/bin/sh");
600             _exit (-1);
601             /* NOTREACHED */
602
603         default:
604             if (!serial) {
605                 ct->c_pid = child_id;
606                 if (xtty)
607                     xpid = child_id;
608             } else {
609                 pidcheck (pidXwait (child_id, NULL));
610             }
611
612             if (fd != NOTOK)
613                 (*ct->c_ceclosefnx) (ct);
614             return (alternate ? DONE : OK);
615     }
616 }
617
618
619 /*
620  * show content of type "text"
621  */
622
623 static int
624 show_text (CT ct, int serial, int alternate)
625 {
626     char *cp, buffer[BUFSIZ];
627     CI ci = &ct->c_ctinfo;
628
629     /* Check for mhn-show-type/subtype */
630     snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
631                 invo_name, ci->ci_type, ci->ci_subtype);
632     if ((cp = context_find (buffer)) && *cp != '\0')
633         return show_content_aux (ct, serial, alternate, cp, NULL);
634
635     /* Check for mhn-show-type */
636     snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
637     if ((cp = context_find (buffer)) && *cp != '\0')
638         return show_content_aux (ct, serial, alternate, cp, NULL);
639
640     /*
641      * Use default method if content is text/plain, or if
642      * if it is not a text part of a multipart/alternative
643      */
644     if (!alternate || ct->c_subtype == TEXT_PLAIN) {
645         snprintf (buffer, sizeof(buffer), "%%p%s '%%F'", progsw ? progsw :
646                 moreproc && *moreproc ? moreproc : "more");
647         cp = (ct->c_showproc = add (buffer, NULL));
648         return show_content_aux (ct, serial, alternate, cp, NULL);
649     }
650
651     return NOTOK;
652 }
653
654
655 /*
656  * show message body of type "multipart"
657  */
658
659 static int
660 show_multi (CT ct, int serial, int alternate)
661 {
662     char *cp, buffer[BUFSIZ];
663     CI ci = &ct->c_ctinfo;
664
665     /* Check for mhn-show-type/subtype */
666     snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
667                 invo_name, ci->ci_type, ci->ci_subtype);
668     if ((cp = context_find (buffer)) && *cp != '\0')
669         return show_multi_aux (ct, serial, alternate, cp);
670
671     /* Check for mhn-show-type */
672     snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
673     if ((cp = context_find (buffer)) && *cp != '\0')
674         return show_multi_aux (ct, serial, alternate, cp);
675
676     if ((cp = ct->c_showproc))
677         return show_multi_aux (ct, serial, alternate, cp);
678
679     /*
680      * Use default method to display this multipart content
681      * if it is not a (nested) part of a multipart/alternative,
682      * or if it is one of the known subtypes of multipart.
683      */
684     if (!alternate || ct->c_subtype != MULTI_UNKNOWN)
685         return show_multi_internal (ct, serial, alternate);
686
687     return NOTOK;
688 }
689
690
691 /*
692  * show message body of subtypes of multipart that
693  * we understand directly (mixed, alternate, etc...)
694  */
695
696 static int
697 show_multi_internal (CT ct, int serial, int alternate)
698 {
699     int alternating, nowalternate, nowserial, result;
700     struct multipart *m = (struct multipart *) ct->c_ctparams;
701     struct part *part;
702     CT p;
703     sigset_t set, oset;
704
705     alternating = 0;
706     nowalternate = alternate;
707
708     if (ct->c_subtype == MULTI_PARALLEL) {
709         nowserial = serialsw;
710     } else if (ct->c_subtype == MULTI_ALTERNATE) {
711         nowalternate = 1;
712         alternating  = 1;
713         nowserial = serial;
714     } else {
715         /*
716          * multipart/mixed
717          * mutlipart/digest
718          * unknown subtypes of multipart (treat as mixed per rfc2046)
719          */
720         nowserial = serial;
721     }
722
723     /* block a few signals */
724     if (!nowserial) {
725         sigemptyset (&set);
726         sigaddset (&set, SIGHUP);
727         sigaddset (&set, SIGINT);
728         sigaddset (&set, SIGQUIT);
729         sigaddset (&set, SIGTERM);
730         SIGPROCMASK (SIG_BLOCK, &set, &oset);
731     }
732
733 /*
734  * alternate   -> we are a part inside an multipart/alternative
735  * alternating -> we are a multipart/alternative 
736  */
737
738     result = alternate ? NOTOK : OK;
739
740     for (part = m->mp_parts; part; part = part->mp_next) {
741         p = part->mp_part;
742
743         if (part_ok (p, 0) && type_ok (p, 0)) {
744             int inneresult;
745
746             inneresult = show_switch (p, nowserial, nowalternate);
747             switch (inneresult) {
748                 case NOTOK:
749                     if (alternate && !alternating) {
750                         result = NOTOK;
751                         goto out;
752                     }
753                     continue;
754
755                 case OK:
756                 case DONE:
757                     if (alternating) {
758                         result = DONE;
759                         break;
760                     }
761                     if (alternate) {
762                         alternate = nowalternate = 0;
763                         if (result == NOTOK)
764                             result = inneresult;
765                     }
766                     continue;
767             }
768             break;
769         }
770     }
771
772     if (alternating && !part) {
773         if (!alternate)
774             content_error (NULL, ct, "don't know how to display any of the contents");
775         result = NOTOK;
776         goto out;
777     }
778
779     if (serial && !nowserial) {
780         pid_t pid;
781         int kids;
782 #ifdef WAITINT
783         int status;
784 #else
785         union wait status;
786 #endif
787
788         kids = 0;
789         for (part = m->mp_parts; part; part = part->mp_next) {
790             p = part->mp_part;
791
792             if (p->c_pid > OK) {
793                 if (kill (p->c_pid, 0) == NOTOK)
794                     p->c_pid = 0;
795                 else
796                     kids++;
797             }
798         }
799
800         while (kids > 0 && (pid = wait (&status)) != NOTOK) {
801 #ifdef WAITINT
802             pidcheck (status);
803 #else
804             pidcheck (status.w_status);
805 #endif
806
807             for (part = m->mp_parts; part; part = part->mp_next) {
808                 p = part->mp_part;
809
810                 if (xpid == pid)
811                     xpid = 0;
812                 if (p->c_pid == pid) {
813                     p->c_pid = 0;
814                     kids--;
815                     break;
816                 }
817             }
818         }
819     }
820
821 out:
822     if (!nowserial) {
823         /* reset the signal mask */
824         SIGPROCMASK (SIG_SETMASK, &oset, &set);
825     }
826
827     return result;
828 }
829
830
831 /*
832  * Parse display string for multipart content
833  * and use external program to display it.
834  */
835
836 static int
837 show_multi_aux (CT ct, int serial, int alternate, char *cp)
838 {
839     int len, buflen, quoted;
840     int xlist, xpause, xtty;
841     char *bp, *pp, *file, buffer[BUFSIZ];
842     struct multipart *m = (struct multipart *) ct->c_ctparams;
843     struct part *part;
844     CI ci = &ct->c_ctinfo;
845     CT p;
846
847     for (part = m->mp_parts; part; part = part->mp_next) {
848         p = part->mp_part;
849
850         if (!p->c_ceopenfnx) {
851             if (!alternate)
852                 content_error (NULL, p, "don't know how to decode content");
853             return NOTOK;
854         }
855
856         if (p->c_storage == NULL) {
857             file = NULL;
858             if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
859                 return NOTOK;
860
861             /* I'm not sure if this is necessary? */
862             p->c_storage = add (file, NULL);
863
864             if (p->c_showproc && !strcmp (p->c_showproc, "true"))
865                 return (alternate ? DONE : OK);
866             (*p->c_ceclosefnx) (p);
867         }
868     }
869
870     xlist     = 0;
871     xpause    = 0;
872     xtty      = 0;
873
874     /* get buffer ready to go */
875     bp = buffer;
876     buflen = sizeof(buffer) - 1;
877     bp[0] = bp[buflen] = '\0';
878     quoted = 0;
879
880     /* Now parse display string */
881     for ( ; *cp && buflen > 0; cp++) {
882         if (*cp == '%') {
883             pp = bp;
884             switch (*++cp) {
885             case 'a':
886                 /* insert parameters from Content-Type field */
887             {
888                 char **ap, **ep;
889                 char *s = "";
890
891                 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
892                     snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
893                     len = strlen (bp);
894                     bp += len;
895                     buflen -= len;
896                     s = " ";
897                 }
898             }
899             break;
900
901             case 'd':
902                 /* insert content description */
903                 if (ct->c_descr) {
904                     char *s;
905
906                     s = trimcpy (ct->c_descr);
907                     strncpy (bp, s, buflen);
908                     free (s);
909                 }
910                 break;
911
912             case 'e':
913                 /* exclusive execution */
914                 xtty = 1;
915                 break;
916
917             case 'F':
918                 /* %e and %f */
919                 xtty = 1;
920                 /* and fall... */
921
922             case 'f':
923                 /* insert filename(s) containing content */
924             {
925                 char *s = "";
926                         
927                 for (part = m->mp_parts; part; part = part->mp_next) {
928                     p = part->mp_part;
929
930                     snprintf (bp, buflen, "%s'%s'", s, p->c_storage);
931                     len = strlen (bp);
932                     bp += len;
933                     buflen -= len;
934                     s = " ";
935                 }
936                 /* set our starting pointer back to bp, to avoid
937                  * requoting the filenames we just added
938                  */
939                 pp = bp;
940             }
941             break;
942
943             case 'p':
944                 /* %l, and pause prior to displaying content */
945                 xpause = pausesw;
946                 /* and fall... */
947
948             case 'l':
949                 /* display listing prior to displaying content */
950                 xlist = !nolist;
951                 break;
952
953             case 's':
954                 /* insert subtype of content */
955                 strncpy (bp, ci->ci_subtype, buflen);
956                 break;
957
958             case '%':
959                 /* insert character % */
960                 goto raw;
961
962             default:
963                 *bp++ = *--cp;
964                 *bp = '\0';
965                 buflen--;
966                 continue;
967             }
968             len = strlen (bp);
969             bp += len;
970             buflen -= len;
971
972             /* Did we actually insert something? */
973             if (bp != pp) {
974                 /* Insert single quote if not inside quotes already */
975                 if (!quoted && buflen) {
976                     len = strlen (pp);
977                     memmove (pp + 1, pp, len);
978                     *pp++ = '\'';
979                     buflen--;
980                     bp++;
981                 }
982                 /* Escape existing quotes */
983                 while ((pp = strchr (pp, '\'')) && buflen > 3) {
984                     len = strlen (pp++);
985                     memmove (pp + 3, pp, len);
986                     *pp++ = '\\';
987                     *pp++ = '\'';
988                     *pp++ = '\'';
989                     buflen -= 3;
990                     bp += 3;
991                 }
992                 /* If pp is still set, that means we ran out of space. */
993                 if (pp)
994                     buflen = 0;
995                 if (!quoted && buflen) {
996                     *bp++ = '\'';
997                     *bp = '\0';
998                     buflen--;
999                 }
1000             }
1001         } else {
1002 raw:
1003             *bp++ = *cp;
1004             *bp = '\0';
1005             buflen--;
1006
1007             if (*cp == '\'')
1008                 quoted = !quoted;
1009         }
1010     }
1011
1012     if (buflen <= 0 || (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
1013         /* content_error would provide a more useful error message
1014          * here, except that if we got overrun, it probably would
1015          * too.
1016          */
1017         fprintf(stderr, "Buffer overflow constructing show command!\n");
1018         return NOTOK;
1019     }
1020
1021     /* use charset string to modify display method */
1022     if (ct->c_termproc) {
1023         char term[BUFSIZ];
1024
1025         strncpy (term, buffer, sizeof(term));
1026         snprintf (buffer, sizeof(buffer), ct->c_termproc, term);
1027     }
1028
1029     return show_content_aux2 (ct, serial, alternate, NULL, buffer,
1030                               NOTOK, xlist, xpause, 0, xtty);
1031 }
1032
1033
1034 /*
1035  * show content of type "message/rfc822"
1036  */
1037
1038 static int
1039 show_message_rfc822 (CT ct, int serial, int alternate)
1040 {
1041     char *cp, buffer[BUFSIZ];
1042     CI ci = &ct->c_ctinfo;
1043
1044     /* Check for mhn-show-type/subtype */
1045     snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
1046                 invo_name, ci->ci_type, ci->ci_subtype);
1047     if ((cp = context_find (buffer)) && *cp != '\0')
1048         return show_content_aux (ct, serial, alternate, cp, NULL);
1049
1050     /* Check for mhn-show-type */
1051     snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
1052     if ((cp = context_find (buffer)) && *cp != '\0')
1053         return show_content_aux (ct, serial, alternate, cp, NULL);
1054
1055     if ((cp = ct->c_showproc))
1056         return show_content_aux (ct, serial, alternate, cp, NULL);
1057
1058     /* default method for message/rfc822 */
1059     if (ct->c_subtype == MESSAGE_RFC822) {
1060         cp = (ct->c_showproc = add ("%pshow -file '%F'", NULL));
1061         return show_content_aux (ct, serial, alternate, cp, NULL);
1062     }
1063
1064     /* complain if we are not a part of a multipart/alternative */
1065     if (!alternate)
1066         content_error (NULL, ct, "don't know how to display content");
1067
1068     return NOTOK;
1069 }
1070
1071
1072 /*
1073  * Show content of type "message/partial".
1074  */
1075
1076 static int
1077 show_partial (CT ct, int serial, int alternate)
1078 {
1079     content_error (NULL, ct,
1080         "in order to display this message, you must reassemble it");
1081     return NOTOK;
1082 }
1083
1084
1085 /*
1086  * Show content of type "message/external".
1087  *
1088  * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1089  */
1090
1091 static int
1092 show_external (CT ct, int serial, int alternate)
1093 {
1094     struct exbody *e = (struct exbody *) ct->c_ctparams;
1095     CT p = e->eb_content;
1096
1097     if (!type_ok (p, 0))
1098         return OK;
1099
1100     return show_switch (p, serial, alternate);
1101
1102 #if 0
1103     content_error (NULL, p, "don't know how to display content");
1104     return NOTOK;
1105 #endif
1106 }
1107
1108
1109 static RETSIGTYPE
1110 intrser (int i)
1111 {
1112 #ifndef RELIABLE_SIGNALS
1113     SIGNAL (SIGINT, intrser);
1114 #endif
1115
1116     putchar ('\n');
1117     siglongjmp (intrenv, DONE);
1118 }