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