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