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