moreproc will always be set in config/config.c, thus eliminated the ISTTY case.
[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);
629                 cp = (ct->c_showproc = getcpy(buffer));
630                 return show_content_aux(ct, serial, alternate, cp, NULL);
631         }
632
633         return NOTOK;
634 }
635
636
637 /*
638 ** show message body of type "multipart"
639 */
640
641 static int
642 show_multi(CT ct, int serial, int alternate)
643 {
644         char *cp, buffer[BUFSIZ];
645         CI ci = &ct->c_ctinfo;
646
647         /* Check for mhshow-show-type/subtype */
648         snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
649                         invo_name, ci->ci_type, ci->ci_subtype);
650         if ((cp = context_find(buffer)) && *cp != '\0')
651                 return show_multi_aux(ct, serial, alternate, cp);
652
653         /* Check for mhshow-show-type */
654         snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
655         if ((cp = context_find(buffer)) && *cp != '\0')
656                 return show_multi_aux(ct, serial, alternate, cp);
657
658         if ((cp = ct->c_showproc))
659                 return show_multi_aux(ct, serial, alternate, cp);
660
661         /*
662         ** Use default method to display this multipart content
663         ** if it is not a (nested) part of a multipart/alternative,
664         ** or if it is one of the known subtypes of multipart.
665         */
666         if (!alternate || ct->c_subtype != MULTI_UNKNOWN)
667                 return show_multi_internal(ct, serial, alternate);
668
669         return NOTOK;
670 }
671
672
673 /*
674 ** show message body of subtypes of multipart that
675 ** we understand directly (mixed, alternate, etc...)
676 */
677
678 static int
679 show_multi_internal(CT ct, int serial, int alternate)
680 {
681         int alternating, nowalternate, nowserial, result;
682         struct multipart *m = (struct multipart *) ct->c_ctparams;
683         struct part *part;
684         CT p;
685         sigset_t set, oset;
686
687         alternating = 0;
688         nowalternate = alternate;
689
690         if (ct->c_subtype == MULTI_PARALLEL) {
691                 nowserial = serialsw;
692         } else if (ct->c_subtype == MULTI_ALTERNATE) {
693                 nowalternate = 1;
694                 alternating  = 1;
695                 nowserial = serial;
696         } else {
697                 /*
698                 ** multipart/mixed
699                 ** mutlipart/digest
700                 ** unknown subtypes of multipart (treat as mixed per rfc2046)
701                 */
702                 nowserial = serial;
703         }
704
705         /* block a few signals */
706         if (!nowserial) {
707                 sigemptyset(&set);
708                 sigaddset(&set, SIGHUP);
709                 sigaddset(&set, SIGINT);
710                 sigaddset(&set, SIGQUIT);
711                 sigaddset(&set, SIGTERM);
712                 SIGPROCMASK(SIG_BLOCK, &set, &oset);
713         }
714
715 /*
716 ** alternate   -> we are a part inside an multipart/alternative
717 ** alternating -> we are a multipart/alternative
718 */
719
720         result = alternate ? NOTOK : OK;
721
722         for (part = m->mp_parts; part; part = part->mp_next) {
723                 p = part->mp_part;
724
725                 if (part_ok(p, 0) && type_ok(p, 0)) {
726                         int inneresult;
727
728                         inneresult = show_switch(p, nowserial, nowalternate);
729                         switch (inneresult) {
730                         case NOTOK:
731                                 if (alternate && !alternating) {
732                                         result = NOTOK;
733                                         goto out;
734                                 }
735                                 continue;
736
737                         case OK:
738                         case DONE:
739                                 if (alternating) {
740                                         result = DONE;
741                                         break;
742                                 }
743                                 if (alternate) {
744                                         alternate = nowalternate = 0;
745                                         if (result == NOTOK)
746                                                 result = inneresult;
747                                 }
748                                 continue;
749                         }
750                         break;
751                 }
752         }
753
754         if (alternating && !part) {
755                 if (!alternate)
756                         content_error(NULL, ct, "don't know how to display any of the contents");
757                 result = NOTOK;
758                 goto out;
759         }
760
761         if (serial && !nowserial) {
762                 pid_t pid;
763                 int kids;
764 #ifdef HAVE_UNION_WAIT
765                 union wait status;
766 #else
767                 int status;
768 #endif
769
770                 kids = 0;
771                 for (part = m->mp_parts; part; part = part->mp_next) {
772                         p = part->mp_part;
773
774                         if (p->c_pid > OK) {
775                                 if (kill(p->c_pid, 0) == NOTOK)
776                                         p->c_pid = 0;
777                                 else
778                                         kids++;
779                         }
780                 }
781
782                 while (kids > 0 && (pid = wait(&status)) != NOTOK) {
783 #ifdef HAVE_UNION_WAIT
784                         pidcheck(status.w_status);
785 #else
786                         pidcheck(status);
787 #endif
788
789                         for (part = m->mp_parts; part; part = part->mp_next) {
790                                 p = part->mp_part;
791
792                                 if (xpid == pid)
793                                         xpid = 0;
794                                 if (p->c_pid == pid) {
795                                         p->c_pid = 0;
796                                         kids--;
797                                         break;
798                                 }
799                         }
800                 }
801         }
802
803 out:
804         if (!nowserial) {
805                 /* reset the signal mask */
806                 SIGPROCMASK(SIG_SETMASK, &oset, &set);
807         }
808
809         return result;
810 }
811
812
813 /*
814 ** Parse display string for multipart content
815 ** and use external program to display it.
816 */
817
818 static int
819 show_multi_aux(CT ct, int serial, int alternate, char *cp)
820 {
821         int len, buflen, quoted;
822         int xlist, xtty;
823         char *bp, *pp, *file, buffer[BUFSIZ];
824         struct multipart *m = (struct multipart *) ct->c_ctparams;
825         struct part *part;
826         CI ci = &ct->c_ctinfo;
827         CT p;
828
829         for (part = m->mp_parts; part; part = part->mp_next) {
830                 p = part->mp_part;
831
832                 if (!p->c_ceopenfnx) {
833                         if (!alternate)
834                                 content_error(NULL, p, "don't know how to decode content");
835                         return NOTOK;
836                 }
837
838                 if (p->c_storage == NULL) {
839                         file = NULL;
840                         if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
841                                 return NOTOK;
842
843                         /* I'm not sure if this is necessary? */
844                         p->c_storage = getcpy(file);
845
846                         if (p->c_showproc && strcmp(p->c_showproc, "true")==0)
847                                 return (alternate ? DONE : OK);
848                         (*p->c_ceclosefnx) (p);
849                 }
850         }
851
852         xlist = 0;
853         xtty = 0;
854
855         /* get buffer ready to go */
856         bp = buffer;
857         buflen = sizeof(buffer) - 1;
858         bp[0] = bp[buflen] = '\0';
859         quoted = 0;
860
861         /* Now parse display string */
862         for ( ; *cp && buflen > 0; cp++) {
863                 if (*cp == '%') {
864                         pp = bp;
865                         switch (*++cp) {
866                         case 'a':
867                                 /* insert parameters from Content-Type field */
868                                 {
869                                 char **ap, **ep;
870                                 char *s = "";
871
872                                 for (ap = ci->ci_attrs, ep = ci->ci_values;
873                                                 *ap; ap++, ep++) {
874                                         snprintf(bp, buflen, "%s%s=\"%s\"",
875                                                         s, *ap, *ep);
876                                         len = strlen(bp);
877                                         bp += len;
878                                         buflen -= len;
879                                         s = " ";
880                                 }
881                                 }
882                                 break;
883
884                         case 'd':
885                                 /* insert content description */
886                                 if (ct->c_descr) {
887                                         char *s;
888
889                                         s = trimcpy(ct->c_descr);
890                                         strncpy(bp, s, buflen);
891                                         free(s);
892                                 }
893                                 break;
894
895                         case 'e':
896                                 /* exclusive execution */
897                                 xtty = 1;
898                                 break;
899
900                         case 'F':
901                                 /* %e and %f */
902                                 xtty = 1;
903                                 /* and fall... */
904
905                         case 'f':
906                                 /* insert filename(s) containing content */
907                         {
908                                 char *s = "";
909
910                                 for (part = m->mp_parts; part;
911                                                 part = part->mp_next) {
912                                         p = part->mp_part;
913
914                                         snprintf(bp, buflen, "%s'%s'",
915                                                         s, p->c_storage);
916                                         len = strlen(bp);
917                                         bp += len;
918                                         buflen -= len;
919                                         s = " ";
920                                 }
921                                 /*
922                                 ** set our starting pointer back to bp,
923                                 ** to avoid requoting the filenames we
924                                 ** just added
925                                 */
926                                 pp = bp;
927                         }
928                         break;
929
930                         case 'p':
931                         case 'l':
932                                 /*
933                                 ** display listing prior to displaying content
934                                 */
935                                 xlist = !nolist;
936                                 break;
937
938                         case 's':
939                                 /* insert subtype of content */
940                                 strncpy(bp, ci->ci_subtype, buflen);
941                                 break;
942
943                         case '%':
944                                 /* insert character % */
945                                 goto raw;
946
947                         default:
948                                 *bp++ = *--cp;
949                                 *bp = '\0';
950                                 buflen--;
951                                 continue;
952                         }
953                         len = strlen(bp);
954                         bp += len;
955                         buflen -= len;
956
957                         /* Did we actually insert something? */
958                         if (bp != pp) {
959                                 /*
960                                 ** Insert single quote if not inside quotes
961                                 ** already
962                                 */
963                                 if (!quoted && buflen) {
964                                         len = strlen(pp);
965                                         memmove(pp + 1, pp, len);
966                                         *pp++ = '\'';
967                                         buflen--;
968                                         bp++;
969                                 }
970                                 /* Escape existing quotes */
971                                 while ((pp = strchr(pp, '\'')) && buflen > 3) {
972                                         len = strlen(pp++);
973                                         memmove(pp + 3, pp, len);
974                                         *pp++ = '\\';
975                                         *pp++ = '\'';
976                                         *pp++ = '\'';
977                                         buflen -= 3;
978                                         bp += 3;
979                                 }
980                                 /*
981                                 ** If pp is still set, that means we ran
982                                 ** out of space.
983                                 */
984                                 if (pp)
985                                         buflen = 0;
986                                 if (!quoted && buflen) {
987                                         *bp++ = '\'';
988                                         *bp = '\0';
989                                         buflen--;
990                                 }
991                         }
992                 } else {
993 raw:
994                         *bp++ = *cp;
995                         *bp = '\0';
996                         buflen--;
997
998                         if (*cp == '\'')
999                                 quoted = !quoted;
1000                 }
1001         }
1002
1003         if (buflen <= 0 ||
1004                         (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
1005                 /*
1006                 ** content_error would provide a more useful error message
1007                 ** here, except that if we got overrun, it probably would
1008                 ** too.
1009                 */
1010                 fprintf(stderr, "Buffer overflow constructing show command!\n");
1011                 return NOTOK;
1012         }
1013
1014         /* use charset string to modify display method */
1015         if (ct->c_termproc) {
1016                 char term[BUFSIZ];
1017
1018                 strncpy(term, buffer, sizeof(term));
1019                 snprintf(buffer, sizeof(buffer), ct->c_termproc, term);
1020         }
1021
1022         return show_content_aux2(ct, serial, alternate, NULL, buffer,
1023                         NOTOK, xlist, 0, xtty);
1024 }
1025
1026
1027 /*
1028 ** show content of type "message/rfc822"
1029 */
1030
1031 static int
1032 show_message_rfc822(CT ct, int serial, int alternate)
1033 {
1034         char *cp, buffer[BUFSIZ];
1035         CI ci = &ct->c_ctinfo;
1036
1037         /* Check for mhshow-show-type/subtype */
1038         snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
1039                                 invo_name, ci->ci_type, ci->ci_subtype);
1040         if ((cp = context_find(buffer)) && *cp != '\0')
1041                 return show_content_aux(ct, serial, alternate, cp, NULL);
1042
1043         /* Check for mhshow-show-type */
1044         snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
1045         if ((cp = context_find(buffer)) && *cp != '\0')
1046                 return show_content_aux(ct, serial, alternate, cp, NULL);
1047
1048         if ((cp = ct->c_showproc))
1049                 return show_content_aux(ct, serial, alternate, cp, NULL);
1050
1051         /* default method for message/rfc822 */
1052         if (ct->c_subtype == MESSAGE_RFC822) {
1053                 cp = (ct->c_showproc = getcpy("%pshow -file '%F'"));
1054                 return show_content_aux(ct, serial, alternate, cp, NULL);
1055         }
1056
1057         /* complain if we are not a part of a multipart/alternative */
1058         if (!alternate)
1059                 content_error(NULL, ct, "don't know how to display content");
1060
1061         return NOTOK;
1062 }
1063
1064
1065 /*
1066 ** Show content of type "message/partial".
1067 */
1068
1069 static int
1070 show_partial(CT ct, int serial, int alternate)
1071 {
1072         content_error(NULL, ct,
1073                 "in order to display this message, you must reassemble it");
1074         return NOTOK;
1075 }
1076
1077
1078 /*
1079 ** Show content of type "message/external".
1080 **
1081 ** THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1082 */
1083
1084 static int
1085 show_external(CT ct, int serial, int alternate)
1086 {
1087         struct exbody *e = (struct exbody *) ct->c_ctparams;
1088         CT p = e->eb_content;
1089
1090         if (!type_ok(p, 0))
1091                 return OK;
1092
1093         return show_switch(p, serial, alternate);
1094
1095 #if 0
1096         content_error(NULL, p, "don't know how to display content");
1097         return NOTOK;
1098 #endif
1099 }