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