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