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