]> diplodocus.org Git - nmh/blob - uip/mhshowsbr.c
posh didn't like use of file wildcard in common.sh.in.
[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 = OK;
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 = NULL, 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 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
342 return NOTOK;
343 if (ct->c_showproc && !strcmp (ct->c_showproc, "true"))
344 return (alternate ? DONE : OK);
345
346 if (! strcmp(invo_name, "mhshow") &&
347 ct->c_type == CT_TEXT && ct->c_subtype == TEXT_PLAIN) {
348 /* This has to be done after calling c_ceopenfnx, so
349 unfortunately the type checks are necessary without
350 some code rearrangement. And to make this really ugly,
351 only do it in mhshow, not mhfixmsg, mhn, or mhstore. */
352 if (convert_content_charset (ct, &file) == OK) {
353 (*ct->c_ceclosefnx) (ct);
354 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
355 return NOTOK;
356 } else {
357 char *charset = content_charset (ct);
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 charset);
362 free (charset);
363 }
364 }
365
366 if (cracked) {
367 strncpy (buffer, cp, sizeof(buffer));
368 goto got_command;
369 }
370
371 if (parse_display_string (ct, cp, &xstdin, &xlist, file, buffer,
372 sizeof(buffer) - 1, 0)) {
373 admonish (NULL, "Buffer overflow constructing show command!\n");
374 return NOTOK;
375 }
376
377 got_command:
378 return show_content_aux2 (ct, alternate, cracked, buffer,
379 fd, xlist, xstdin, fmt);
380 }
381
382
383 /*
384 * Routine to actually display the content
385 */
386
387 static int
388 show_content_aux2 (CT ct, int alternate, char *cracked, char *buffer,
389 int fd, int xlist, int xstdin, struct format *fmt)
390 {
391 pid_t child_id;
392 int i, vecp;
393 char **vec, *file;
394
395 if (debugsw || cracked) {
396 fflush (stdout);
397
398 fprintf (stderr, "%s msg %s", cracked ? "storing" : "show",
399 ct->c_file);
400 if (ct->c_partno)
401 fprintf (stderr, " part %s", ct->c_partno);
402 if (cracked)
403 fprintf (stderr, " using command (cd %s; %s)\n", cracked, buffer);
404 else
405 fprintf (stderr, " using command %s\n", buffer);
406 }
407
408 if (xlist && fmt) {
409 output_marker(ct, fmt, 0);
410 }
411
412 /*
413 * If the command is a zero-length string, just write the output on
414 * stdout.
415 */
416
417 if (buffer[0] == '\0') {
418 char readbuf[BUFSIZ];
419 ssize_t cc;
420 char lastchar = '\n';
421
422 if (fd == NOTOK) {
423 advise(NULL, "Cannot use NULL command to display content-type "
424 "%s/%s", ct->c_ctinfo.ci_type, ct->c_ctinfo.ci_subtype);
425 return NOTOK;
426 }
427
428 while ((cc = read(fd, readbuf, sizeof(readbuf))) > 0) {
429 if ((ssize_t) fwrite(readbuf, sizeof(char), cc, stdout) < cc) {
430 advise ("putline", "fwrite");
431 }
432 lastchar = readbuf[cc - 1];
433 }
434
435 if (cc < 0) {
436 advise("read", "while reading text content");
437 return NOTOK;
438 }
439
440 /*
441 * The MIME standards allow content to not have a trailing newline.
442 * But because we are (presumably) sending this to stdout, include
443 * a newline for text content if the final character was not a
444 * newline. Only do this for mhshow.
445 */
446
447 if (strcmp(invo_name, "mhshow") == 0 && ct->c_type == CT_TEXT &&
448 ct->c_subtype == TEXT_PLAIN && lastchar != '\n') {
449 putc('\n', stdout);
450 }
451
452 fflush(stdout);
453
454 return OK;
455 }
456
457 vec = argsplit(buffer, &file, &vecp);
458 vec[vecp++] = NULL;
459
460 fflush (stdout);
461
462 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
463 sleep (5);
464 switch (child_id) {
465 case NOTOK:
466 advise ("fork", "unable to");
467 (*ct->c_ceclosefnx) (ct);
468 return NOTOK;
469
470 case OK:
471 if (cracked) {
472 if (chdir (cracked) < 0) {
473 advise (cracked, "chdir");
474 }
475 }
476 if (!xstdin)
477 dup2 (fd, 0);
478 close (fd);
479 execvp (file, vec);
480 fprintf (stderr, "unable to exec ");
481 perror (buffer);
482 _exit (-1);
483 /* NOTREACHED */
484
485 default:
486 arglist_free(file, vec);
487
488 pidcheck (pidXwait (child_id, NULL));
489
490 if (fd != NOTOK)
491 (*ct->c_ceclosefnx) (ct);
492 return (alternate ? DONE : OK);
493 }
494 }
495
496
497 /*
498 * show content of type "text"
499 */
500
501 static int
502 show_text (CT ct, int alternate, int concatsw, struct format *fmt)
503 {
504 char *cp, buffer[BUFSIZ];
505 CI ci = &ct->c_ctinfo;
506
507 /* Check for invo_name-show-type[/subtype] */
508 if ((cp = context_find_by_type ("show", ci->ci_type, ci->ci_subtype)))
509 return show_content_aux (ct, alternate, cp, NULL, fmt);
510
511 /*
512 * Use default method if content is text/plain, or if
513 * if it is not a text part of a multipart/alternative
514 */
515 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
516 if (concatsw) {
517 if (ct->c_termproc)
518 snprintf(buffer, sizeof(buffer), "%%lcat");
519 else
520 snprintf(buffer, sizeof(buffer), "%%l");
521 } else
522 snprintf (buffer, sizeof(buffer), "%%l%s %%F", progsw ? progsw :
523 moreproc && *moreproc ? moreproc : DEFAULT_PAGER);
524 cp = (ct->c_showproc = add (buffer, NULL));
525 return show_content_aux (ct, alternate, cp, NULL, fmt);
526 }
527
528 return NOTOK;
529 }
530
531
532 /*
533 * show message body of type "multipart"
534 */
535
536 static int
537 show_multi (CT ct, int alternate, int concatsw, int textonly, int inlineonly,
538 struct format *fmt)
539 {
540 char *cp;
541 CI ci = &ct->c_ctinfo;
542
543 /* Check for invo_name-show-type[/subtype] */
544 if ((cp = context_find_by_type ("show", ci->ci_type, ci->ci_subtype)))
545 return show_multi_aux (ct, alternate, cp, fmt);
546
547 if ((cp = ct->c_showproc))
548 return show_multi_aux (ct, alternate, cp, fmt);
549
550 /*
551 * Use default method to display this multipart content. Even
552 * unknown types are displayable, since they're treated as mixed
553 * per RFC 2046.
554 */
555 return show_multi_internal (ct, alternate, concatsw, textonly,
556 inlineonly, fmt);
557 }
558
559
560 /*
561 * show message body of subtypes of multipart that
562 * we understand directly (mixed, alternate, etc...)
563 */
564
565 static int
566 show_multi_internal (CT ct, int alternate, int concatsw, int textonly,
567 int inlineonly, struct format *fmt)
568 {
569 int alternating, nowalternate, result;
570 struct multipart *m = (struct multipart *) ct->c_ctparams;
571 struct part *part;
572 int request_matched;
573 CT p;
574
575 alternating = 0;
576 nowalternate = alternate;
577
578 if (ct->c_subtype == MULTI_ALTERNATE) {
579 nowalternate = 1;
580 alternating = 1;
581 }
582
583 /*
584 * alternate -> we are a part inside an multipart/alternative
585 * alternating -> we are a multipart/alternative
586 */
587
588 result = alternate ? NOTOK : OK;
589 request_matched = 0;
590
591 for (part = m->mp_parts; part; part = part->mp_next) {
592 p = part->mp_part;
593
594 if (part_ok (p, 1) && type_ok (p, 1)) {
595 int inneresult;
596
597 request_matched = 1;
598
599 inneresult = show_switch (p, nowalternate, concatsw, textonly,
600 inlineonly, fmt);
601 switch (inneresult) {
602 case NOTOK:
603 if (alternate && !alternating) {
604 result = NOTOK;
605 goto out;
606 }
607 continue;
608
609 case OK:
610 case DONE:
611 if (alternating) {
612 result = DONE;
613 break;
614 }
615 if (alternate) {
616 alternate = nowalternate = 0;
617 if (result == NOTOK)
618 result = inneresult;
619 }
620 continue;
621 }
622 break;
623 }
624 }
625
626 if (alternating && !part && request_matched) {
627 if (!alternate)
628 content_error (NULL, ct, "don't know how to display any of the contents");
629 result = NOTOK;
630 goto out;
631 }
632
633 out:
634 /* if no parts matched what was requested, there can't have been
635 * any display errors, so we report OK. */
636 return request_matched ? result : OK;
637 }
638
639
640 /*
641 * Parse display string for multipart content
642 * and use external program to display it.
643 */
644
645 static int
646 show_multi_aux (CT ct, int alternate, char *cp, struct format *fmt)
647 {
648 /* xstdin is only used in the call to parse_display_string():
649 its value is ignored in the function. */
650 int xstdin = 0, xlist = 0;
651 char *file = NULL, buffer[BUFSIZ];
652 struct multipart *m = (struct multipart *) ct->c_ctparams;
653 struct part *part;
654 CT p;
655
656 for (part = m->mp_parts; part; part = part->mp_next) {
657 p = part->mp_part;
658
659 if (!p->c_ceopenfnx) {
660 if (!alternate)
661 content_error (NULL, p, "don't know how to decode content");
662 return NOTOK;
663 }
664
665 if (p->c_storage == NULL) {
666 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
667 return NOTOK;
668
669 p->c_storage = add (file, NULL);
670
671 if (p->c_showproc && !strcmp (p->c_showproc, "true"))
672 return (alternate ? DONE : OK);
673 (*p->c_ceclosefnx) (p);
674 }
675 }
676
677 if (parse_display_string (ct, cp, &xstdin, &xlist, file,
678 buffer, sizeof(buffer) - 1, 1)) {
679 admonish (NULL, "Buffer overflow constructing show command!\n");
680 return NOTOK;
681 }
682
683 return show_content_aux2 (ct, alternate, NULL, buffer, NOTOK, xlist, 0, fmt);
684 }
685
686
687 /*
688 * show content of type "message/rfc822"
689 */
690
691 static int
692 show_message_rfc822 (CT ct, int alternate, struct format *fmt)
693 {
694 char *cp;
695 CI ci = &ct->c_ctinfo;
696
697 /* Check for invo_name-show-type[/subtype] */
698 if ((cp = context_find_by_type ("show", ci->ci_type, ci->ci_subtype)))
699 return show_content_aux (ct, alternate, cp, NULL, fmt);
700
701 if ((cp = ct->c_showproc))
702 return show_content_aux (ct, alternate, cp, NULL, fmt);
703
704 /* default method for message/rfc822 */
705 if (ct->c_subtype == MESSAGE_RFC822) {
706 cp = (ct->c_showproc = add ("%pshow -file %F", NULL));
707 return show_content_aux (ct, alternate, cp, NULL, fmt);
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 concatsw, 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, concatsw, 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 if (strcasecmp (src_charset, dest_charset)) {
1026 #ifdef HAVE_ICONV
1027 iconv_t conv_desc = NULL;
1028 char *dest;
1029 int fd = -1;
1030 char **file = NULL;
1031 FILE **fp = NULL;
1032 size_t begin;
1033 size_t end;
1034 int opened_input_file = 0;
1035 char src_buffer[BUFSIZ];
1036 size_t dest_buffer_size = BUFSIZ;
1037 char *dest_buffer = mh_xmalloc(dest_buffer_size);
1038 HF hf;
1039 char *tempfile;
1040
1041 if ((conv_desc = iconv_open (dest_charset, src_charset)) ==
1042 (iconv_t) -1) {
1043 advise (NULL, "Can't convert %s to %s", src_charset, dest_charset);
1044 free (src_charset);
1045 return NOTOK;
1046 }
1047
1048 if ((tempfile = m_mktemp2 (NULL, invo_name, &fd, NULL)) == NULL) {
1049 adios (NULL, "unable to create temporary file in %s",
1050 get_temp_dir());
1051 }
1052 dest = add (tempfile, NULL);
1053
1054 if (ct->c_cefile.ce_file) {
1055 file = &ct->c_cefile.ce_file;
1056 fp = &ct->c_cefile.ce_fp;
1057 begin = end = 0;
1058 } else if (ct->c_file) {
1059 file = &ct->c_file;
1060 fp = &ct->c_fp;
1061 begin = (size_t) ct->c_begin;
1062 end = (size_t) ct->c_end;
1063 } /* else no input file: shouldn't happen */
1064
1065 if (file && *file && fp) {
1066 if (! *fp) {
1067 if ((*fp = fopen (*file, "r")) == NULL) {
1068 advise (*file, "unable to open for reading");
1069 status = NOTOK;
1070 } else {
1071 opened_input_file = 1;
1072 }
1073 }
1074 }
1075
1076 if (fp && *fp) {
1077 size_t inbytes;
1078 size_t bytes_to_read =
1079 end > 0 && end > begin ? end - begin : sizeof src_buffer;
1080
1081 fseeko (*fp, begin, SEEK_SET);
1082 while ((inbytes = fread (src_buffer, 1,
1083 min (bytes_to_read, sizeof src_buffer),
1084 *fp)) > 0) {
1085 ICONV_CONST char *ib = src_buffer;
1086 char *ob = dest_buffer;
1087 size_t outbytes = dest_buffer_size;
1088 size_t outbytes_before = outbytes;
1089
1090 if (end > 0) bytes_to_read -= inbytes;
1091
1092 iconv_start:
1093 if (iconv (conv_desc, &ib, &inbytes, &ob, &outbytes) ==
1094 (size_t) -1) {
1095 if (errno == E2BIG) {
1096 /*
1097 * Bump up the buffer by at least a factor of 2
1098 * over what we need.
1099 */
1100 size_t bumpup = inbytes * 2, ob_off = ob - dest_buffer;
1101 dest_buffer_size += bumpup;
1102 dest_buffer = mh_xrealloc(dest_buffer,
1103 dest_buffer_size);
1104 ob = dest_buffer + ob_off;
1105 outbytes += bumpup;
1106 outbytes_before += bumpup;
1107 goto iconv_start;
1108 }
1109 status = NOTOK;
1110 break;
1111 } else {
1112 if (write (fd, dest_buffer, outbytes_before - outbytes)
1113 < 0) {
1114 advise (dest, "write");
1115 }
1116 }
1117 }
1118
1119 if (opened_input_file) {
1120 fclose (*fp);
1121 *fp = NULL;
1122 }
1123 }
1124
1125 iconv_close (conv_desc);
1126 close (fd);
1127
1128 if (status == OK) {
1129 /* Replace the decoded file with the converted one. */
1130 if (ct->c_cefile.ce_file) {
1131 if (ct->c_cefile.ce_unlink) {
1132 (void) m_unlink (ct->c_cefile.ce_file);
1133 }
1134 free (ct->c_cefile.ce_file);
1135 }
1136 ct->c_cefile.ce_file = dest;
1137 ct->c_cefile.ce_unlink = 1;
1138
1139 ++*message_mods;
1140
1141 /* Update ct->c_ctline. */
1142 if (ct->c_ctline) {
1143 char *ctline = concat(" ", ct->c_ctinfo.ci_type, "/",
1144 ct->c_ctinfo.ci_subtype, NULL);
1145 char *outline;
1146
1147 replace_param(&ct->c_ctinfo.ci_first_pm,
1148 &ct->c_ctinfo.ci_last_pm, "charset",
1149 dest_charset, 0);
1150 outline = output_params(strlen(TYPE_FIELD) + 1 + strlen(ctline),
1151 ct->c_ctinfo.ci_first_pm, NULL, 0);
1152 if (outline) {
1153 ctline = add(outline, ctline);
1154 free(outline);
1155 }
1156
1157 free (ct->c_ctline);
1158 ct->c_ctline = ctline;
1159 } /* else no CT line, which is odd */
1160
1161 /* Update Content-Type header field. */
1162 for (hf = ct->c_first_hf; hf; hf = hf->next) {
1163 if (! strcasecmp (TYPE_FIELD, hf->name)) {
1164 char *ctline = concat (ct->c_ctline, "\n", NULL);
1165
1166 free (hf->value);
1167 hf->value = ctline;
1168 break;
1169 }
1170 }
1171 } else {
1172 (void) m_unlink (dest);
1173 }
1174 free(dest_buffer);
1175 #else /* ! HAVE_ICONV */
1176 NMH_UNUSED (message_mods);
1177
1178 advise (NULL, "Can't convert %s to %s without iconv", src_charset,
1179 dest_charset);
1180 errno = ENOSYS;
1181 status = NOTOK;
1182 #endif /* ! HAVE_ICONV */
1183 }
1184
1185 free (src_charset);
1186 return status;
1187 }
1188
1189
1190 static int
1191 convert_content_charset (CT ct, char **file) {
1192 int status = OK;
1193
1194 #ifdef HAVE_ICONV
1195 /* Using current locale, see if the content needs to be converted. */
1196
1197 /* content_charset() cannot return NULL. */
1198 char *charset = content_charset (ct);
1199
1200 if (! check_charset (charset, strlen (charset))) {
1201 int unused = 0;
1202
1203 char *charset = getcpy (get_charset ());
1204
1205 if (convert_charset (ct, charset, &unused) == 0) {
1206 *file = ct->c_cefile.ce_file;
1207 } else {
1208 status = NOTOK;
1209 }
1210 }
1211 free (charset);
1212 #else /* ! HAVE_ICONV */
1213 NMH_UNUSED (ct);
1214 NMH_UNUSED (file);
1215 #endif /* ! HAVE_ICONV */
1216
1217 return status;
1218 }
1219
1220 /*
1221 * Compile our format string and save any parameters we care about.
1222 */
1223
1224 #define DEFAULT_MARKER "[ part %{part} - %{content-type} - " \
1225 "%<{description}%{description}" \
1226 "%?{cdispo-filename}%{cdispo-filename}" \
1227 "%|%{ctype-name}%> " \
1228 "%(kilo(size))B %<(unseen)\\(suppressed\\)%> ]"
1229
1230 static struct format *
1231 compile_marker(char *markerform)
1232 {
1233 struct format *fmt;
1234 char *fmtstring;
1235 struct comp *comp = NULL;
1236 unsigned int bucket;
1237 struct param_comp_list *pc_entry;
1238
1239 fmtstring = new_fs(markerform, NULL, DEFAULT_MARKER);
1240
1241 (void) fmt_compile(fmtstring, &fmt, 1);
1242 free(fmtstring);
1243
1244 /*
1245 * Things we care about:
1246 *
1247 * part - Part name (e.g., 1.1)
1248 * content-type - Content-Type
1249 * description - Content-Description
1250 * disposition - Content-Disposition (inline, attachment)
1251 * ctype-<param> - Content-Type parameter
1252 * cdispo-<param> - Content-Disposition parameter
1253 */
1254
1255 while ((comp = fmt_nextcomp(comp, &bucket)) != NULL) {
1256 if (strcasecmp(comp->c_name, "part") == 0) {
1257 part_comp = comp;
1258 } else if (strcasecmp(comp->c_name, "content-type") == 0) {
1259 ctype_comp = comp;
1260 } else if (strcasecmp(comp->c_name, "description") == 0) {
1261 description_comp = comp;
1262 } else if (strcasecmp(comp->c_name, "disposition") == 0) {
1263 dispo_comp = comp;
1264 } else if (strncasecmp(comp->c_name, "ctype-", 6) == 0 &&
1265 strlen(comp->c_name) > 6) {
1266 pc_entry = mh_xmalloc(sizeof(*pc_entry));
1267 pc_entry->param = getcpy(comp->c_name + 6);
1268 pc_entry->comp = comp;
1269 pc_entry->next = ctype_pc_list;
1270 ctype_pc_list = pc_entry;
1271 } else if (strncasecmp(comp->c_name, "cdispo-", 7) == 0 &&
1272 strlen(comp->c_name) > 7) {
1273 pc_entry = mh_xmalloc(sizeof(*pc_entry));
1274 pc_entry->param = getcpy(comp->c_name + 7);
1275 pc_entry->comp = comp;
1276 pc_entry->next = dispo_pc_list;
1277 dispo_pc_list = pc_entry;
1278 }
1279 }
1280
1281 return fmt;
1282 }
1283
1284 /*
1285 * Output on stdout an appropriate marker for this content, using mh-format
1286 */
1287
1288 static void
1289 output_marker(CT ct, struct format *fmt, int hidden)
1290 {
1291 charstring_t outbuf = charstring_create (BUFSIZ);
1292 struct param_comp_list *pcentry;
1293 int partsize;
1294 int dat[5];
1295
1296 /*
1297 * Grab any items we care about.
1298 */
1299
1300 if (ctype_comp && ct->c_ctinfo.ci_type) {
1301 ctype_comp->c_text = concat(ct->c_ctinfo.ci_type, "/",
1302 ct->c_ctinfo.ci_subtype, NULL);
1303 }
1304
1305 if (part_comp && ct->c_partno) {
1306 part_comp->c_text = getcpy(ct->c_partno);
1307 }
1308
1309 if (description_comp && ct->c_descr) {
1310 description_comp->c_text = getcpy(ct->c_descr);
1311 }
1312
1313 if (dispo_comp && ct->c_dispo_type) {
1314 dispo_comp->c_text = getcpy(ct->c_dispo_type);
1315 }
1316
1317 for (pcentry = ctype_pc_list; pcentry != NULL; pcentry = pcentry->next) {
1318 pcentry->comp->c_text = get_param(ct->c_ctinfo.ci_first_pm,
1319 pcentry->param, '?', 0);
1320 }
1321
1322 for (pcentry = dispo_pc_list; pcentry != NULL; pcentry = pcentry->next) {
1323 pcentry->comp->c_text = get_param(ct->c_dispo_first,
1324 pcentry->param, '?', 0);
1325 }
1326
1327 if (ct->c_cesizefnx)
1328 partsize = (*ct->c_cesizefnx) (ct);
1329 else
1330 partsize = ct->c_end - ct->c_begin;
1331
1332 /* make the part's hidden aspect available by overloading the
1333 * %(unseen) function. make the part's size available via %(size).
1334 * see comments in h/fmt_scan.h.
1335 */
1336 dat[2] = partsize;
1337 dat[4] = hidden;
1338 dat[0] = dat[1] = dat[3] = 0;
1339
1340 fmt_scan(fmt, outbuf, BUFSIZ, dat, NULL);
1341
1342 fputs(charstring_buffer (outbuf), stdout);
1343 charstring_free (outbuf);
1344
1345 fmt_freecomptext();
1346 }
1347
1348 /*
1349 * Reset (and free) any of the saved marker text
1350 */
1351
1352 static void
1353 free_markercomps(void)
1354 {
1355 struct param_comp_list *pc_entry, *pc2;
1356
1357 part_comp = NULL;
1358 ctype_comp = NULL;
1359 description_comp = NULL;
1360 dispo_comp = NULL;
1361
1362 for (pc_entry = ctype_pc_list; pc_entry != NULL; ) {
1363 free(pc_entry->param);
1364 pc2 = pc_entry->next;
1365 free(pc_entry);
1366 pc_entry = pc2;
1367 }
1368
1369 for (pc_entry = dispo_pc_list; pc_entry != NULL; ) {
1370 free(pc_entry->param);
1371 pc2 = pc_entry->next;
1372 free(pc_entry);
1373 pc_entry = pc2;
1374 }
1375 }
1376
1377 /*
1378 * Exit if the display process returned with a nonzero exit code, or terminated
1379 * with a SIGQUIT signal.
1380 */
1381
1382 static int
1383 pidcheck (int status)
1384 {
1385 if ((status & 0xff00) == 0xff00 || (status & 0x007f) != SIGQUIT)
1386 return status;
1387
1388 fflush (stdout);
1389 fflush (stderr);
1390 done (1);
1391 return 1;
1392 }