]> diplodocus.org Git - nmh/blob - uip/mhshowsbr.c
Fix typo in man page
[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/fmt_scan.h>
20 #include <h/utils.h>
21 #ifdef HAVE_ICONV
22 # include <iconv.h>
23 #endif /* ! HAVE_ICONV */
24
25 extern int debugsw;
26
27 int nolist = 0;
28
29 char *progsw = NULL;
30
31 /* flags for moreproc/header display */
32 int nomore = 0;
33 char *formsw = NULL;
34
35
36 /* mhmisc.c */
37 int part_ok (CT, int);
38 int type_ok (CT, int);
39 void content_error (char *, CT, char *, ...);
40 void flush_errors (void);
41
42 /*
43 * static prototypes
44 */
45 static void show_single_message (CT, char *, int, int, int, struct format *);
46 static void DisplayMsgHeader (CT, char *, int);
47 static int show_switch (CT, int, int, int, int, struct format *);
48 static int show_content (CT, int, int, int, struct format *);
49 static int show_content_aux2 (CT, int, char *, char *, int, int, int, struct format *);
50 static int show_text (CT, int, int, struct format *);
51 static int show_multi (CT, int, int, int, int, struct format *);
52 static int show_multi_internal (CT, int, int, int, int, struct format *);
53 static int show_multi_aux (CT, int, char *, struct format *);
54 static int show_message_rfc822 (CT, int, struct format *);
55 static int show_partial (CT, int);
56 static int show_external (CT, int, int, int, int, struct format *);
57 static int parse_display_string (CT, char *, int *, int *, char *, char *,
58 size_t, int multipart);
59 static int convert_content_charset (CT, char **);
60 static struct format *compile_marker(char *);
61 static void output_marker (CT, struct format *, int);
62 static void free_markercomps (void);
63 static int pidcheck(int);
64
65 /*
66 * Components (and list of parameters/components) we care about for the
67 * content marker display.
68 */
69
70 static struct comp *part_comp = NULL;
71 static struct comp *ctype_comp = NULL;
72 static struct comp *description_comp = NULL;
73 static struct comp *dispo_comp = NULL;
74
75 struct param_comp_list {
76 char *param;
77 struct comp *comp;
78 struct param_comp_list *next;
79 };
80
81 static struct param_comp_list *ctype_pc_list = NULL;
82 static struct param_comp_list *dispo_pc_list = NULL;
83
84
85 /*
86 * Top level entry point to show/display a group of messages
87 */
88
89 void
90 show_all_messages (CT *cts, int concatsw, int textonly, int inlineonly,
91 char *markerform)
92 {
93 CT ct, *ctp;
94 struct format *fmt;
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 * Compile the content marker format line
105 */
106 fmt = compile_marker(markerform);
107
108 /*
109 * If form is "mhl.null", suppress display of header.
110 */
111 if (!strcmp (formsw, "mhl.null"))
112 formsw = NULL;
113
114 for (ctp = cts; *ctp; ctp++) {
115 ct = *ctp;
116
117 /* if top-level type is ok, then display message */
118 if (type_ok (ct, 1))
119 show_single_message (ct, formsw, concatsw, textonly, inlineonly,
120 fmt);
121 }
122
123 free_markercomps();
124 fmt_free(fmt, 1);
125 }
126
127
128 /*
129 * Entry point to show/display a single message
130 */
131
132 static void
133 show_single_message (CT ct, char *form, int concatsw, int textonly,
134 int inlineonly, struct format *fmt)
135 {
136 sigset_t set, oset;
137
138 int status;
139
140 /* Allow user executable bit so that temporary directories created by
141 * the viewer (e.g., lynx) are going to be accessible */
142 umask (ct->c_umask & ~(0100));
143
144 /*
145 * If you have a format file, then display
146 * the message headers.
147 */
148 if (form)
149 DisplayMsgHeader(ct, form, concatsw);
150
151 /* Show the body of the message */
152 show_switch (ct, 0, concatsw, textonly, inlineonly, fmt);
153
154 if (ct->c_fp) {
155 fclose (ct->c_fp);
156 ct->c_fp = NULL;
157 }
158 if (ct->c_ceclosefnx)
159 (*ct->c_ceclosefnx) (ct);
160
161 /* block a few signals */
162 sigemptyset (&set);
163 sigaddset (&set, SIGHUP);
164 sigaddset (&set, SIGINT);
165 sigaddset (&set, SIGQUIT);
166 sigaddset (&set, SIGTERM);
167 sigprocmask (SIG_BLOCK, &set, &oset);
168
169 while (!concatsw && wait (&status) != NOTOK) {
170 pidcheck (status);
171 continue;
172 }
173
174 /* reset the signal mask */
175 sigprocmask (SIG_SETMASK, &oset, &set);
176
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, int concatsw)
187 {
188 pid_t child_id;
189 int i, vecp;
190 char **vec;
191 char *file;
192
193 vec = argsplit(mhlproc, &file, &vecp);
194 vec[vecp++] = getcpy("-form");
195 vec[vecp++] = getcpy(form);
196 vec[vecp++] = getcpy("-nobody");
197 vec[vecp++] = getcpy(ct->c_file);
198
199 /*
200 * If we've specified -(no)moreproc,
201 * then just pass that along.
202 */
203 if (nomore || concatsw) {
204 vec[vecp++] = getcpy("-nomoreproc");
205 } else if (progsw) {
206 vec[vecp++] = getcpy("-moreproc");
207 vec[vecp++] = getcpy(progsw);
208 }
209 vec[vecp] = NULL;
210
211 fflush (stdout);
212
213 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
214 sleep (5);
215
216 switch (child_id) {
217 case NOTOK:
218 adios ("fork", "unable to");
219 /* NOTREACHED */
220
221 case OK:
222 execvp (file, vec);
223 fprintf (stderr, "unable to exec ");
224 perror (mhlproc);
225 _exit (-1);
226 /* NOTREACHED */
227
228 default:
229 pidcheck(pidwait(child_id, NOTOK));
230 break;
231 }
232
233 arglist_free(file, vec);
234 }
235
236
237 /*
238 * Switching routine. Call the correct routine
239 * based on content type.
240 */
241
242 static int
243 show_switch (CT ct, int alternate, int concatsw, int textonly, int inlineonly,
244 struct format *fmt)
245 {
246 switch (ct->c_type) {
247 case CT_MULTIPART:
248 return show_multi (ct, alternate, concatsw, textonly,
249 inlineonly, fmt);
250
251 case CT_MESSAGE:
252 switch (ct->c_subtype) {
253 case MESSAGE_PARTIAL:
254 return show_partial (ct, alternate);
255
256 case MESSAGE_EXTERNAL:
257 return show_external (ct, alternate, concatsw, textonly,
258 inlineonly, fmt);
259
260 case MESSAGE_RFC822:
261 return show_message_rfc822 (ct, alternate, fmt);
262
263 /*
264 * Treat unknown message types as equivalent to
265 * application/octet-stream for now
266 */
267 default:
268 return show_content (ct, alternate, textonly,
269 inlineonly, fmt);
270 }
271
272 case CT_TEXT:
273 return show_text (ct, alternate, concatsw, fmt);
274
275 case CT_AUDIO:
276 case CT_IMAGE:
277 case CT_VIDEO:
278 case CT_APPLICATION:
279 default:
280 return show_content (ct, alternate, textonly, inlineonly, fmt);
281 }
282
283 return 0; /* NOT REACHED */
284 }
285
286
287 /*
288 * Generic method for displaying content
289 */
290
291 static int
292 show_content (CT ct, int alternate, int textonly, int inlineonly,
293 struct format *fmt)
294 {
295 char *cp;
296 CI ci = &ct->c_ctinfo;
297
298 /*
299 * If we're here, we are not a text type. So we don't need to check
300 * the content-type.
301 */
302
303 if (textonly || (inlineonly && !is_inline(ct))) {
304 output_marker(ct, fmt, 1);
305 return OK;
306 }
307
308 /* Check for invo_name-show-type[/subtype] */
309 if ((cp = context_find_by_type ("show", ci->ci_type, ci->ci_subtype)))
310 return show_content_aux (ct, alternate, cp, NULL, fmt);
311
312 if ((cp = ct->c_showproc))
313 return show_content_aux (ct, alternate, cp, NULL, fmt);
314
315 /* complain if we are not a part of a multipart/alternative */
316 if (!alternate)
317 content_error (NULL, ct, "don't know how to display content");
318
319 return NOTOK;
320 }
321
322
323 /*
324 * Parse the display string for displaying generic content
325 */
326
327 int
328 show_content_aux (CT ct, int alternate, char *cp, char *cracked, struct format *fmt)
329 {
330 int fd;
331 int xstdin = 0, xlist = 0;
332 char *file, buffer[BUFSIZ];
333
334 if (!ct->c_ceopenfnx) {
335 if (!alternate)
336 content_error (NULL, ct, "don't know how to decode content");
337
338 return NOTOK;
339 }
340
341 file = NULL;
342 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
343 return NOTOK;
344 if (ct->c_showproc && !strcmp (ct->c_showproc, "true"))
345 return (alternate ? DONE : OK);
346
347 if (! strcmp(invo_name, "mhshow") &&
348 ct->c_type == CT_TEXT && ct->c_subtype == TEXT_PLAIN) {
349 /* This has to be done after calling c_ceopenfnx, so
350 unfortunately the type checks are necessary without
351 some code rearrangement. And to make this really ugly,
352 only do it in mhshow, not mhfixmsg, mhn, or mhstore. */
353 if (convert_content_charset (ct, &file) == OK) {
354 (*ct->c_ceclosefnx) (ct);
355 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
356 return NOTOK;
357 } else {
358 admonish (NULL, "unable to convert character set%s%s to %s",
359 ct->c_partno ? " of part " : "",
360 ct->c_partno ? ct->c_partno : "",
361 content_charset (ct));
362 }
363 }
364
365 if (cracked) {
366 strncpy (buffer, cp, sizeof(buffer));
367 goto got_command;
368 }
369
370 if (parse_display_string (ct, cp, &xstdin, &xlist, file, buffer,
371 sizeof(buffer) - 1, 0)) {
372 admonish (NULL, "Buffer overflow constructing show command!\n");
373 return NOTOK;
374 }
375
376 got_command:
377 return show_content_aux2 (ct, alternate, cracked, buffer,
378 fd, xlist, xstdin, fmt);
379 }
380
381
382 /*
383 * Routine to actually display the content
384 */
385
386 static int
387 show_content_aux2 (CT ct, int alternate, char *cracked, char *buffer,
388 int fd, int xlist, int xstdin, struct format *fmt)
389 {
390 pid_t child_id;
391 int i, vecp;
392 char **vec, *file;
393
394 if (debugsw || cracked) {
395 fflush (stdout);
396
397 fprintf (stderr, "%s msg %s", cracked ? "storing" : "show",
398 ct->c_file);
399 if (ct->c_partno)
400 fprintf (stderr, " part %s", ct->c_partno);
401 if (cracked)
402 fprintf (stderr, " using command (cd %s; %s)\n", cracked, buffer);
403 else
404 fprintf (stderr, " using command %s\n", buffer);
405 }
406
407 if (xlist && fmt) {
408 output_marker(ct, fmt, 0);
409 }
410
411 /*
412 * If the command is a zero-length string, just write the output on
413 * stdout.
414 */
415
416 if (buffer[0] == '\0') {
417 char readbuf[BUFSIZ];
418 ssize_t cc;
419 char lastchar = '\n';
420
421 if (fd == NOTOK) {
422 advise(NULL, "Cannot use NULL command to display content-type "
423 "%s/%s", ct->c_ctinfo.ci_type, ct->c_ctinfo.ci_subtype);
424 return NOTOK;
425 }
426
427 while ((cc = read(fd, readbuf, sizeof(readbuf))) > 0) {
428 fwrite(readbuf, sizeof(char), cc, stdout);
429 lastchar = readbuf[cc - 1];
430 }
431
432 if (cc < 0) {
433 advise("read", "while reading text content");
434 return NOTOK;
435 }
436
437 /*
438 * The MIME standards allow content to not have a trailing newline.
439 * But because we are (presumably) sending this to stdout, include
440 * a newline for text content if the final character was not a
441 * newline. Only do this for mhshow.
442 */
443
444 if (strcmp(invo_name, "mhshow") == 0 && ct->c_type == CT_TEXT &&
445 ct->c_subtype == TEXT_PLAIN && lastchar != '\n') {
446 putc('\n', stdout);
447 }
448
449 fflush(stdout);
450
451 return OK;
452 }
453
454 vec = argsplit(buffer, &file, &vecp);
455 vec[vecp++] = NULL;
456
457 fflush (stdout);
458
459 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
460 sleep (5);
461 switch (child_id) {
462 case NOTOK:
463 advise ("fork", "unable to");
464 (*ct->c_ceclosefnx) (ct);
465 return NOTOK;
466
467 case OK:
468 if (cracked) {
469 if (chdir (cracked) < 0) {
470 advise (cracked, "chdir");
471 }
472 }
473 if (!xstdin)
474 dup2 (fd, 0);
475 close (fd);
476 execvp (file, vec);
477 fprintf (stderr, "unable to exec ");
478 perror (buffer);
479 _exit (-1);
480 /* NOTREACHED */
481
482 default:
483 arglist_free(file, vec);
484
485 pidcheck (pidXwait (child_id, NULL));
486
487 if (fd != NOTOK)
488 (*ct->c_ceclosefnx) (ct);
489 return (alternate ? DONE : OK);
490 }
491 }
492
493
494 /*
495 * show content of type "text"
496 */
497
498 static int
499 show_text (CT ct, int alternate, int concatsw, struct format *fmt)
500 {
501 char *cp, buffer[BUFSIZ];
502 CI ci = &ct->c_ctinfo;
503
504 /* Check for invo_name-show-type[/subtype] */
505 if ((cp = context_find_by_type ("show", ci->ci_type, ci->ci_subtype)))
506 return show_content_aux (ct, alternate, cp, NULL, fmt);
507
508 /*
509 * Use default method if content is text/plain, or if
510 * if it is not a text part of a multipart/alternative
511 */
512 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
513 if (concatsw) {
514 if (ct->c_termproc)
515 snprintf(buffer, sizeof(buffer), "%%lcat");
516 else
517 snprintf(buffer, sizeof(buffer), "%%l");
518 } else
519 snprintf (buffer, sizeof(buffer), "%%l%s %%F", progsw ? progsw :
520 moreproc && *moreproc ? moreproc : DEFAULT_PAGER);
521 cp = (ct->c_showproc = add (buffer, NULL));
522 return show_content_aux (ct, alternate, cp, NULL, fmt);
523 }
524
525 return NOTOK;
526 }
527
528
529 /*
530 * show message body of type "multipart"
531 */
532
533 static int
534 show_multi (CT ct, int alternate, int concatsw, int textonly, int inlineonly,
535 struct format *fmt)
536 {
537 char *cp;
538 CI ci = &ct->c_ctinfo;
539
540 /* Check for invo_name-show-type[/subtype] */
541 if ((cp = context_find_by_type ("show", ci->ci_type, ci->ci_subtype)))
542 return show_multi_aux (ct, alternate, cp, fmt);
543
544 if ((cp = ct->c_showproc))
545 return show_multi_aux (ct, alternate, cp, fmt);
546
547 /*
548 * Use default method to display this multipart content. Even
549 * unknown types are displayable, since they're treated as mixed
550 * per RFC 2046.
551 */
552 return show_multi_internal (ct, alternate, concatsw, textonly,
553 inlineonly, fmt);
554 }
555
556
557 /*
558 * show message body of subtypes of multipart that
559 * we understand directly (mixed, alternate, etc...)
560 */
561
562 static int
563 show_multi_internal (CT ct, int alternate, int concatsw, int textonly,
564 int inlineonly, struct format *fmt)
565 {
566 int alternating, nowalternate, result;
567 struct multipart *m = (struct multipart *) ct->c_ctparams;
568 struct part *part;
569 int any_part_ok;
570 CT p;
571
572 alternating = 0;
573 nowalternate = alternate;
574
575 if (ct->c_subtype == MULTI_ALTERNATE) {
576 nowalternate = 1;
577 alternating = 1;
578 }
579
580 /*
581 * alternate -> we are a part inside an multipart/alternative
582 * alternating -> we are a multipart/alternative
583 */
584
585 result = alternate ? NOTOK : OK;
586 any_part_ok = 0;
587
588 for (part = m->mp_parts; part; part = part->mp_next) {
589 p = part->mp_part;
590
591 if (part_ok (p, 1) && type_ok (p, 1)) {
592 int inneresult;
593
594 any_part_ok = 1;
595
596 inneresult = show_switch (p, nowalternate, concatsw, textonly,
597 inlineonly, fmt);
598 switch (inneresult) {
599 case NOTOK:
600 if (alternate && !alternating) {
601 result = NOTOK;
602 goto out;
603 }
604 continue;
605
606 case OK:
607 case DONE:
608 if (alternating) {
609 result = DONE;
610 break;
611 }
612 if (alternate) {
613 alternate = nowalternate = 0;
614 if (result == NOTOK)
615 result = inneresult;
616 }
617 continue;
618 }
619 break;
620 }
621 }
622
623 if (alternating && !part && any_part_ok) {
624 if (!alternate)
625 content_error (NULL, ct, "don't know how to display any of the contents");
626 result = NOTOK;
627 goto out;
628 }
629
630 out:
631 return result;
632 }
633
634
635 /*
636 * Parse display string for multipart content
637 * and use external program to display it.
638 */
639
640 static int
641 show_multi_aux (CT ct, int alternate, char *cp, struct format *fmt)
642 {
643 /* xstdin is only used in the call to parse_display_string():
644 its value is ignored in the function. */
645 int xstdin = 0, xlist = 0;
646 char *file, buffer[BUFSIZ];
647 struct multipart *m = (struct multipart *) ct->c_ctparams;
648 struct part *part;
649 CT p;
650
651 for (part = m->mp_parts; part; part = part->mp_next) {
652 p = part->mp_part;
653
654 if (!p->c_ceopenfnx) {
655 if (!alternate)
656 content_error (NULL, p, "don't know how to decode content");
657 return NOTOK;
658 }
659
660 if (p->c_storage == NULL) {
661 file = NULL;
662 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
663 return NOTOK;
664
665 p->c_storage = add (file, NULL);
666
667 if (p->c_showproc && !strcmp (p->c_showproc, "true"))
668 return (alternate ? DONE : OK);
669 (*p->c_ceclosefnx) (p);
670 }
671 }
672
673 if (parse_display_string (ct, cp, &xstdin, &xlist, file,
674 buffer, sizeof(buffer) - 1, 1)) {
675 admonish (NULL, "Buffer overflow constructing show command!\n");
676 return NOTOK;
677 }
678
679 return show_content_aux2 (ct, alternate, NULL, buffer, NOTOK, xlist, 0, fmt);
680 }
681
682
683 /*
684 * show content of type "message/rfc822"
685 */
686
687 static int
688 show_message_rfc822 (CT ct, int alternate, struct format *fmt)
689 {
690 char *cp;
691 CI ci = &ct->c_ctinfo;
692
693 /* Check for invo_name-show-type[/subtype] */
694 if ((cp = context_find_by_type ("show", ci->ci_type, ci->ci_subtype)))
695 return show_content_aux (ct, alternate, cp, NULL, fmt);
696
697 if ((cp = ct->c_showproc))
698 return show_content_aux (ct, alternate, cp, NULL, fmt);
699
700 /* default method for message/rfc822 */
701 if (ct->c_subtype == MESSAGE_RFC822) {
702 cp = (ct->c_showproc = add ("%pshow -file %F", NULL));
703 return show_content_aux (ct, alternate, cp, NULL, fmt);
704 }
705
706 /* complain if we are not a part of a multipart/alternative */
707 if (!alternate)
708 content_error (NULL, ct, "don't know how to display content");
709
710 return NOTOK;
711 }
712
713
714 /*
715 * Show content of type "message/partial".
716 */
717
718 static int
719 show_partial (CT ct, int alternate)
720 {
721 NMH_UNUSED (alternate);
722
723 content_error (NULL, ct,
724 "in order to display this message, you must reassemble it");
725 return NOTOK;
726 }
727
728
729 /*
730 * Show content of type "message/external".
731 *
732 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
733 */
734
735 static int
736 show_external (CT ct, int alternate, int concatsw, int textonly, int inlineonly,
737 struct format *fmt)
738 {
739 struct exbody *e = (struct exbody *) ct->c_ctparams;
740 CT p = e->eb_content;
741
742 if (!type_ok (p, 0))
743 return OK;
744
745 return show_switch (p, alternate, concatsw, textonly, inlineonly, fmt);
746 }
747
748
749 static int
750 parse_display_string (CT ct, char *cp, int *xstdin, int *xlist,
751 char *file, char *buffer, size_t buflen,
752 int multipart) {
753 int len, quoted = 0;
754 char *bp = buffer, *pp;
755 CI ci = &ct->c_ctinfo;
756
757 bp[0] = bp[buflen] = '\0';
758
759 for ( ; *cp && buflen > 0; cp++) {
760 if (*cp == '%') {
761 pp = bp;
762
763 switch (*++cp) {
764 case 'a':
765 /* insert parameters from Content-Type field */
766 {
767 PM pm;
768 char *s = "";
769
770 for (pm = ci->ci_first_pm; pm; pm = pm->pm_next) {
771 snprintf (bp, buflen, "%s%s=\"%s\"", s, pm->pm_name,
772 get_param_value(pm, '?'));
773 len = strlen (bp);
774 bp += len;
775 buflen -= len;
776 s = " ";
777 }
778 }
779 break;
780
781 case 'd':
782 /* insert content description */
783 if (ct->c_descr) {
784 char *s;
785
786 s = trimcpy (ct->c_descr);
787 strncpy (bp, s, buflen);
788 free (s);
789 }
790 break;
791
792 case 'e':
793 /* no longer implemented */
794 break;
795
796 case 'F':
797 /* %f, and stdin is terminal not content */
798 *xstdin = 1;
799 /* and fall... */
800
801 case 'f':
802 if (multipart) {
803 /* insert filename(s) containing content */
804 struct multipart *m = (struct multipart *) ct->c_ctparams;
805 struct part *part;
806 char *s = "";
807 CT p;
808
809 for (part = m->mp_parts; part; part = part->mp_next) {
810 p = part->mp_part;
811
812 snprintf (bp, buflen, "%s%s", s, p->c_storage);
813 len = strlen (bp);
814 bp += len;
815 buflen -= len;
816 s = " ";
817 }
818 } else {
819 /* insert filename containing content */
820 snprintf (bp, buflen, "%s", file);
821
822 /*
823 * Old comments below are left here for posterity.
824 * This was/is tricky.
825 */
826 /* since we've quoted the file argument, set things up
827 * to look past it, to avoid problems with the quoting
828 * logic below. (I know, I should figure out what's
829 * broken with the quoting logic, but..)
830 */
831 /*
832 * Here's the email that submitted the patch with
833 * the comment above:
834 * https://www.mail-archive.com/nmh-workers@mhost.com/
835 * msg00288.html
836 * I can't tell from that exactly what was broken,
837 * beyond misquoting of the filename. The profile
838 * had appearances of %F both with and without quotes.
839 * The unquoted ones should have been quoted by the
840 * code below.
841 * The fix was to always quote the filename. But
842 * that broke '%F' because it expanded to ''filename''.
843 */
844 /*
845 * Old comments above are left here for posterity.
846 * The quoting below should work properly now.
847 */
848 }
849 break;
850
851 case 'p':
852 /* No longer supported */
853 /* and fall... */
854
855 case 'l':
856 /* display listing prior to displaying content */
857 *xlist = !nolist;
858 break;
859
860 case 's':
861 /* insert subtype of content */
862 strncpy (bp, ci->ci_subtype, buflen);
863 break;
864
865 case '%':
866 /* insert character % */
867 goto raw;
868
869 case '{' : {
870 const char *closing_brace = strchr(cp, '}');
871
872 if (closing_brace) {
873 const size_t param_len = closing_brace - cp - 1;
874 char *param = mh_xmalloc(param_len + 1);
875 char *value;
876
877 (void) strncpy(param, cp + 1, param_len);
878 param[param_len] = '\0';
879 value = get_param(ci->ci_first_pm, param, '?', 0);
880 free(param);
881
882 cp += param_len + 1; /* Skip both braces, too. */
883
884 if (value) {
885 /* %{param} is set in the Content-Type header.
886 After the break below, quote it if necessary. */
887 (void) strncpy(bp, value, buflen);
888 free(value);
889 } else {
890 /* %{param} not found, so skip it completely. cp
891 was advanced above. */
892 continue;
893 }
894 } else {
895 /* This will get confused if there are multiple %{}'s,
896 but its real purpose is to avoid doing bad things
897 above if a closing brace wasn't found. */
898 admonish(NULL,
899 "no closing brace for display string escape %s",
900 cp);
901 }
902 break;
903 }
904
905 default:
906 *bp++ = *--cp;
907 *bp = '\0';
908 buflen--;
909 continue;
910 }
911 len = strlen (bp);
912 bp += len;
913 buflen -= len;
914 *bp = '\0';
915
916 /* Did we actually insert something? */
917 if (bp != pp) {
918 /* Insert single quote if not inside quotes already */
919 if (!quoted && buflen) {
920 len = strlen (pp);
921 memmove (pp + 1, pp, len+1);
922 *pp++ = '\'';
923 buflen--;
924 bp++;
925 quoted = 1;
926 }
927 /* Escape existing quotes */
928 while ((pp = strchr (pp, '\'')) && buflen > 3) {
929 len = strlen (pp++);
930 if (quoted) {
931 /* Quoted. Let this quote close that quoting.
932 Insert an escaped quote to replace it and
933 another quote to reopen quoting, which will be
934 closed below. */
935 memmove (pp + 2, pp, len);
936 *pp++ = '\\';
937 *pp++ = '\'';
938 buflen -= 2;
939 bp += 2;
940 quoted = 0;
941 } else {
942 /* Not quoted. This should not be reached with
943 the current code, but handle the condition
944 in case the code changes. Just escape the
945 quote. */
946 memmove (pp, pp-1, len+1);
947 *(pp++-1) = '\\';
948 buflen -= 1;
949 bp += 1;
950 }
951 }
952 /* If pp is still set, that means we ran out of space. */
953 if (pp)
954 buflen = 0;
955 /* Close quoting. */
956 if (quoted && buflen) {
957 /* See if we need to close the quote by looking
958 for an odd number of unescaped close quotes in
959 the remainder of the display string. */
960 int found_quote = 0, escaped = 0;
961 char *c;
962
963 for (c = cp+1; *c; ++c) {
964 if (*c == '\\') {
965 escaped = ! escaped;
966 } else {
967 if (escaped) {
968 escaped = 0;
969 } else {
970 if (*c == '\'') {
971 found_quote = ! found_quote;
972 }
973 }
974 }
975 }
976 if (! found_quote) {
977 *bp++ = '\'';
978 buflen--;
979 quoted = 0;
980 }
981 }
982 }
983 } else {
984 raw:
985 *bp++ = *cp;
986 buflen--;
987
988 if (*cp == '\'')
989 quoted = !quoted;
990 }
991
992 *bp = '\0';
993 }
994
995 if (buflen <= 0 ||
996 (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
997 /* content_error would provide a more useful error message
998 * here, except that if we got overrun, it probably would
999 * too.
1000 */
1001 return NOTOK;
1002 }
1003
1004 /* use charset string to modify display method */
1005 if (ct->c_termproc) {
1006 char term[BUFSIZ];
1007
1008 strncpy (term, buffer, sizeof(term));
1009 snprintf (buffer, buflen, ct->c_termproc, term);
1010 }
1011
1012 return OK;
1013 }
1014
1015
1016 int
1017 convert_charset (CT ct, char *dest_charset, int *message_mods) {
1018 char *src_charset = content_charset (ct);
1019 int status = OK;
1020
1021 if (strcasecmp (src_charset, dest_charset)) {
1022 #ifdef HAVE_ICONV
1023 iconv_t conv_desc = NULL;
1024 char *dest;
1025 int fd = -1;
1026 char **file = NULL;
1027 FILE **fp = NULL;
1028 size_t begin;
1029 size_t end;
1030 int opened_input_file = 0;
1031 char src_buffer[BUFSIZ];
1032 size_t dest_buffer_size = BUFSIZ;
1033 char *dest_buffer = mh_xmalloc(dest_buffer_size);
1034 HF hf;
1035 char *tempfile;
1036
1037 if ((conv_desc = iconv_open (dest_charset, src_charset)) ==
1038 (iconv_t) -1) {
1039 advise (NULL, "Can't convert %s to %s", src_charset, dest_charset);
1040 return NOTOK;
1041 }
1042
1043 if ((tempfile = m_mktemp2 (NULL, invo_name, &fd, NULL)) == NULL) {
1044 adios (NULL, "unable to create temporary file in %s",
1045 get_temp_dir());
1046 }
1047 dest = add (tempfile, NULL);
1048
1049 if (ct->c_cefile.ce_file) {
1050 file = &ct->c_cefile.ce_file;
1051 fp = &ct->c_cefile.ce_fp;
1052 begin = end = 0;
1053 } else if (ct->c_file) {
1054 file = &ct->c_file;
1055 fp = &ct->c_fp;
1056 begin = (size_t) ct->c_begin;
1057 end = (size_t) ct->c_end;
1058 } /* else no input file: shouldn't happen */
1059
1060 if (file && *file && fp) {
1061 if (! *fp) {
1062 if ((*fp = fopen (*file, "r")) == NULL) {
1063 advise (*file, "unable to open for reading");
1064 status = NOTOK;
1065 } else {
1066 opened_input_file = 1;
1067 }
1068 }
1069 }
1070
1071 if (fp && *fp) {
1072 size_t inbytes;
1073 size_t bytes_to_read =
1074 end > 0 && end > begin ? end - begin : sizeof src_buffer;
1075
1076 fseeko (*fp, begin, SEEK_SET);
1077 while ((inbytes = fread (src_buffer, 1,
1078 min (bytes_to_read, sizeof src_buffer),
1079 *fp)) > 0) {
1080 ICONV_CONST char *ib = src_buffer;
1081 char *ob = dest_buffer;
1082 size_t outbytes = dest_buffer_size;
1083 size_t outbytes_before = outbytes;
1084
1085 if (end > 0) bytes_to_read -= inbytes;
1086
1087 iconv_start:
1088 if (iconv (conv_desc, &ib, &inbytes, &ob, &outbytes) ==
1089 (size_t) -1) {
1090 if (errno == E2BIG) {
1091 /*
1092 * Bump up the buffer by at least a factor of 2
1093 * over what we need.
1094 */
1095 size_t bumpup = inbytes * 2, ob_off = ob - dest_buffer;
1096 dest_buffer_size += bumpup;
1097 dest_buffer = mh_xrealloc(dest_buffer,
1098 dest_buffer_size);
1099 ob = dest_buffer + ob_off;
1100 outbytes += bumpup;
1101 outbytes_before += bumpup;
1102 goto iconv_start;
1103 }
1104 status = NOTOK;
1105 break;
1106 } else {
1107 if (write (fd, dest_buffer, outbytes_before - outbytes)
1108 < 0) {
1109 advise (dest, "write");
1110 }
1111 }
1112 }
1113
1114 if (opened_input_file) {
1115 fclose (*fp);
1116 *fp = NULL;
1117 }
1118 }
1119
1120 iconv_close (conv_desc);
1121 close (fd);
1122
1123 if (status == OK) {
1124 /* Replace the decoded file with the converted one. */
1125 if (ct->c_cefile.ce_file) {
1126 if (ct->c_cefile.ce_unlink) {
1127 (void) m_unlink (ct->c_cefile.ce_file);
1128 }
1129 free (ct->c_cefile.ce_file);
1130 }
1131 ct->c_cefile.ce_file = dest;
1132 ct->c_cefile.ce_unlink = 1;
1133
1134 ++*message_mods;
1135
1136 /* Update ct->c_ctline. */
1137 if (ct->c_ctline) {
1138 char *ctline = concat(" ", ct->c_ctinfo.ci_type, "/",
1139 ct->c_ctinfo.ci_subtype, NULL);
1140 char *outline;
1141
1142 replace_param(&ct->c_ctinfo.ci_first_pm,
1143 &ct->c_ctinfo.ci_last_pm, "charset",
1144 dest_charset, 0);
1145 outline = output_params(strlen(TYPE_FIELD) + 1 + strlen(ctline),
1146 ct->c_ctinfo.ci_first_pm, NULL, 0);
1147 if (outline) {
1148 ctline = add(outline, ctline);
1149 free(outline);
1150 }
1151
1152 free (ct->c_ctline);
1153 ct->c_ctline = ctline;
1154 } /* else no CT line, which is odd */
1155
1156 /* Update Content-Type header field. */
1157 for (hf = ct->c_first_hf; hf; hf = hf->next) {
1158 if (! strcasecmp (TYPE_FIELD, hf->name)) {
1159 char *ctline = concat (ct->c_ctline, "\n", NULL);
1160
1161 free (hf->value);
1162 hf->value = ctline;
1163 break;
1164 }
1165 }
1166 } else {
1167 (void) m_unlink (dest);
1168 }
1169 free(dest_buffer);
1170 #else /* ! HAVE_ICONV */
1171 NMH_UNUSED (message_mods);
1172
1173 advise (NULL, "Can't convert %s to %s without iconv", src_charset,
1174 dest_charset);
1175 errno = ENOSYS;
1176 status = NOTOK;
1177 #endif /* ! HAVE_ICONV */
1178 }
1179
1180 return status;
1181 }
1182
1183
1184 static int
1185 convert_content_charset (CT ct, char **file) {
1186 int status = OK;
1187
1188 #ifdef HAVE_ICONV
1189 /* Using current locale, see if the content needs to be converted. */
1190
1191 /* content_charset() cannot return NULL. */
1192 char *charset = content_charset (ct);
1193
1194 if (! check_charset (charset, strlen (charset))) {
1195 int unused = 0;
1196
1197 char *charset = getcpy (get_charset ());
1198
1199 if (convert_charset (ct, charset, &unused) == 0) {
1200 *file = ct->c_cefile.ce_file;
1201 } else {
1202 status = NOTOK;
1203 }
1204
1205 free (charset);
1206 }
1207 #else /* ! HAVE_ICONV */
1208 NMH_UNUSED (ct);
1209 NMH_UNUSED (file);
1210 #endif /* ! HAVE_ICONV */
1211
1212 return status;
1213 }
1214
1215 /*
1216 * Compile our format string and save any parameters we care about.
1217 */
1218
1219 #define DEFAULT_MARKER "[ part %{part} - %{content-type} - " \
1220 "%<{description}%{description}" \
1221 "%?{cdispo-filename}%{cdispo-filename}" \
1222 "%|%{ctype-name}%> " \
1223 "%(kilo(size))B %<(unseen)\\(suppressed\\)%> ]"
1224
1225 static struct format *
1226 compile_marker(char *markerform)
1227 {
1228 struct format *fmt;
1229 char *fmtstring;
1230 struct comp *comp = NULL;
1231 unsigned int bucket;
1232 struct param_comp_list *pc_entry;
1233
1234 fmtstring = new_fs(markerform, NULL, DEFAULT_MARKER);
1235
1236 (void) fmt_compile(fmtstring, &fmt, 1);
1237 free(fmtstring);
1238
1239 /*
1240 * Things we care about:
1241 *
1242 * part - Part name (e.g., 1.1)
1243 * content-type - Content-Type
1244 * description - Content-Description
1245 * disposition - Content-Disposition (inline, attachment)
1246 * ctype-<param> - Content-Type parameter
1247 * cdispo-<param> - Content-Disposition parameter
1248 */
1249
1250 while ((comp = fmt_nextcomp(comp, &bucket)) != NULL) {
1251 if (strcasecmp(comp->c_name, "part") == 0) {
1252 part_comp = comp;
1253 } else if (strcasecmp(comp->c_name, "content-type") == 0) {
1254 ctype_comp = comp;
1255 } else if (strcasecmp(comp->c_name, "description") == 0) {
1256 description_comp = comp;
1257 } else if (strcasecmp(comp->c_name, "disposition") == 0) {
1258 dispo_comp = comp;
1259 } else if (strncasecmp(comp->c_name, "ctype-", 6) == 0 &&
1260 strlen(comp->c_name) > 6) {
1261 pc_entry = mh_xmalloc(sizeof(*pc_entry));
1262 pc_entry->param = getcpy(comp->c_name + 6);
1263 pc_entry->comp = comp;
1264 pc_entry->next = ctype_pc_list;
1265 ctype_pc_list = pc_entry;
1266 } else if (strncasecmp(comp->c_name, "cdispo-", 7) == 0 &&
1267 strlen(comp->c_name) > 7) {
1268 pc_entry = mh_xmalloc(sizeof(*pc_entry));
1269 pc_entry->param = getcpy(comp->c_name + 7);
1270 pc_entry->comp = comp;
1271 pc_entry->next = dispo_pc_list;
1272 dispo_pc_list = pc_entry;
1273 }
1274 }
1275
1276 return fmt;
1277 }
1278
1279 /*
1280 * Output on stdout an appropriate marker for this content, using mh-format
1281 */
1282
1283 static void
1284 output_marker(CT ct, struct format *fmt, int hidden)
1285 {
1286 charstring_t outbuf = charstring_create (BUFSIZ);
1287 struct param_comp_list *pcentry;
1288 int partsize;
1289 int dat[5];
1290
1291 /*
1292 * Grab any items we care about.
1293 */
1294
1295 if (ctype_comp && ct->c_ctinfo.ci_type) {
1296 ctype_comp->c_text = concat(ct->c_ctinfo.ci_type, "/",
1297 ct->c_ctinfo.ci_subtype, NULL);
1298 }
1299
1300 if (part_comp && ct->c_partno) {
1301 part_comp->c_text = getcpy(ct->c_partno);
1302 }
1303
1304 if (description_comp && ct->c_descr) {
1305 description_comp->c_text = getcpy(ct->c_descr);
1306 }
1307
1308 if (dispo_comp && ct->c_dispo_type) {
1309 dispo_comp->c_text = getcpy(ct->c_dispo_type);
1310 }
1311
1312 for (pcentry = ctype_pc_list; pcentry != NULL; pcentry = pcentry->next) {
1313 pcentry->comp->c_text = get_param(ct->c_ctinfo.ci_first_pm,
1314 pcentry->param, '?', 0);
1315 }
1316
1317 for (pcentry = dispo_pc_list; pcentry != NULL; pcentry = pcentry->next) {
1318 pcentry->comp->c_text = get_param(ct->c_dispo_first,
1319 pcentry->param, '?', 0);
1320 }
1321
1322 if (ct->c_cesizefnx)
1323 partsize = (*ct->c_cesizefnx) (ct);
1324 else
1325 partsize = ct->c_end - ct->c_begin;
1326
1327 /* make the part's hidden aspect available by overloading the
1328 * %(unseen) function. make the part's size available via %(size).
1329 * see comments in h/fmt_scan.h.
1330 */
1331 dat[2] = partsize;
1332 dat[4] = hidden;
1333 dat[0] = dat[1] = dat[3] = 0;
1334
1335 fmt_scan(fmt, outbuf, BUFSIZ, dat, NULL);
1336
1337 fputs(charstring_buffer (outbuf), stdout);
1338 charstring_free (outbuf);
1339
1340 fmt_freecomptext();
1341 }
1342
1343 /*
1344 * Reset (and free) any of the saved marker text
1345 */
1346
1347 static void
1348 free_markercomps(void)
1349 {
1350 struct param_comp_list *pc_entry, *pc2;
1351
1352 part_comp = NULL;
1353 ctype_comp = NULL;
1354 description_comp = NULL;
1355 dispo_comp = NULL;
1356
1357 for (pc_entry = ctype_pc_list; pc_entry != NULL; ) {
1358 free(pc_entry->param);
1359 pc2 = pc_entry->next;
1360 free(pc_entry);
1361 pc_entry = pc2;
1362 }
1363
1364 for (pc_entry = dispo_pc_list; pc_entry != NULL; ) {
1365 free(pc_entry->param);
1366 pc2 = pc_entry->next;
1367 free(pc_entry);
1368 pc_entry = pc2;
1369 }
1370 }
1371
1372 /*
1373 * Exit if the display process returned with a nonzero exit code, or terminated
1374 * with a SIGQUIT signal.
1375 */
1376
1377 static int
1378 pidcheck (int status)
1379 {
1380 if ((status & 0xff00) == 0xff00 || (status & 0x007f) != SIGQUIT)
1381 return status;
1382
1383 fflush (stdout);
1384 fflush (stderr);
1385 done (1);
1386 return 1;
1387 }