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