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