2 ** mhbuild.c -- expand/translate MIME composition files
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
10 ** This code was originally part of mhn.c. I split it into
11 ** a separate program (mhbuild.c). But the code still has some of
12 ** the mhn.c code in it. This program needs additional
13 ** streamlining and removal of unneeded code.
21 #include <h/mhparse.h>
29 #ifdef HAVE_SYS_TIME_H
30 # include <sys/time.h>
34 static struct swit switches[] = {
50 ** Directory to place tmp files. This must
51 ** be set before these routines are called.
57 static char prefix[] = "----- =_aaaaaaaaaa";
65 int make_intermediates(char *);
66 void content_error(char *, CT, char *, ...);
69 void free_content(CT);
71 void free_encoding(CT, int);
76 static int init_decoded_content(CT);
77 static char *fgetstr(char *, int, FILE *);
78 static int user_content(FILE *, char *, char *, CT *);
79 static void set_id(CT, int);
80 static int compose_content(CT);
81 static int scan_content(CT);
82 static int build_headers(CT);
83 static CT build_mime(char *);
92 static char infile[BUFSIZ];
93 static int unlink_infile = 0;
95 static char outfile[BUFSIZ];
96 static int unlink_outfile = 0;
101 int output_message(CT, char *);
102 int output_message_fp(CT, FILE *, char*);
105 void set_endian(void);
108 void free_content(CT);
112 main(int argc, char **argv)
114 char *cp, buf[BUFSIZ];
115 char buffer[BUFSIZ], *compfile = NULL;
116 char **argp, **arguments;
121 if (atexit(unlink_done) != 0) {
122 adios(EX_OSERR, NULL, "atexit failed");
125 setlocale(LC_ALL, "");
126 invo_name = mhbasename(argv[0]);
128 /* read user profile/context */
131 arguments = getarguments(invo_name, argc, argv, 1);
134 while ((cp = *argp++)) {
135 if (cp[0] == '-' && cp[1] == '\0') {
137 adios(EX_USAGE, NULL, "cannot specify both standard input and a file");
140 verbosw = 0; /* turn off -verbose listings */
144 switch (smatch(++cp, switches)) {
146 ambigsw(cp, switches);
149 adios(EX_USAGE, NULL, "-%s unknown", cp);
152 snprintf(buf, sizeof(buf), "%s [switches] file", invo_name);
153 print_help(buf, switches, 1);
154 exit(argc == 2 ? EX_OK : EX_USAGE);
156 print_version(invo_name);
157 exit(argc == 2 ? EX_OK : EX_USAGE);
171 adios(EX_USAGE, NULL, "only one composition file allowed");
179 ** Check if we've specified an additional profile
181 if ((cp = getenv("MHBUILD"))) {
182 if ((fp = fopen(cp, "r"))) {
183 readconfig((struct node **) 0, fp, cp, 0);
186 admonish("", "unable to read $MHBUILD profile (%s)",
192 ** Read the standard profile setup
194 if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
195 readconfig((struct node **) 0, fp, cp, 0);
200 ** Check for storage directory. If defined, we
201 ** will store temporary files there. Else we
202 ** store them in standard nmh directory.
204 if ((cp = context_find(nmhstorage)) && *cp)
205 tmp = concat(cp, "/", invo_name, NULL);
207 tmp = getcpy(toabsdir(invo_name));
209 /* Check if we have a file to process */
211 adios(EX_USAGE, NULL, "need to specify a %s composition file",
215 ** Process the composition file from standard input.
217 if (compfile[0] == '-' && compfile[1] == '\0') {
218 /* copy standard input to temporary file */
219 strncpy(infile, m_mktemp(invo_name, NULL, &fp),
221 while (fgets(buffer, BUFSIZ, stdin))
226 /* build the content structures for MIME message */
227 ct = build_mime(infile);
229 /* output MIME message to this temporary file */
230 strncpy(outfile, m_mktemp(invo_name, NULL, &fp_out),
234 /* output the message */
235 output_message_fp(ct, fp_out, outfile);
238 /* output the temp file to standard output */
239 if ((fp = fopen(outfile, "r")) == NULL)
240 adios(EX_IOERR, outfile, "unable to open");
241 while (fgets(buffer, BUFSIZ, fp))
242 fputs(buffer, stdout);
256 ** Process the composition file from a file.
259 /* build the content structures for MIME message */
260 ct = build_mime(compfile);
262 /* output MIME message to this temporary file */
263 strncpy(outfile, m_mktemp2(compfile, invo_name, NULL, &fp_out),
267 /* output the message */
268 output_message_fp(ct, fp_out, outfile);
271 /* Rename composition draft */
272 snprintf(buffer, sizeof(buffer), "%s.orig", compfile);
273 if (rename(compfile, buffer) == NOTOK) {
274 adios(EX_IOERR, buffer, "unable to rename draft %s to", compfile);
277 /* Rename output file to take its place */
278 if (rename(outfile, compfile) == NOTOK) {
279 advise(compfile, "unable to rename output %s to", outfile);
280 rename(buffer, compfile);
294 ** Check if we need to remove stray temporary files.
299 if (unlink_outfile) {
305 ** Main routine for translating composition file
306 ** into valid MIME message. It translates the draft
307 ** into a content structure (actually a tree of content
308 ** structures). This message then can be manipulated
309 ** in various ways, including being output via
313 build_mime(char *infile)
316 char buf[BUFSIZ], name[NAMESZ];
326 /* open the composition draft */
327 if ((in = fopen(infile, "r")) == NULL)
328 adios(EX_IOERR, infile, "unable to open for reading");
331 ** Allocate space for primary (outside) content
333 if ((ct = (CT) mh_xcalloc(1, sizeof(*ct))) == NULL)
334 adios(EX_OSERR, NULL, "out of memory");
337 ** Allocate structure for handling decoded content
338 ** for this part. We don't really need this, but
339 ** allocate it to remain consistent.
341 init_decoded_content(ct);
344 ** Parse some of the header fields in the composition
345 ** draft into the linked list of header fields for
346 ** the new MIME message.
348 for (compnum = 1, state = FLD;;) {
349 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
355 /* abort if draft has Mime-Version header field */
356 if (!mh_strcasecmp(name, VRSN_FIELD))
357 adios(EX_CONFIG, NULL, "draft shouldn't contain %s: field", VRSN_FIELD);
360 ** abort if draft has Content-Transfer-Encoding
363 if (!mh_strcasecmp(name, ENCODING_FIELD))
364 adios(EX_CONFIG, NULL, "draft shouldn't contain %s: field", ENCODING_FIELD);
366 /* ignore any Content-Type fields in the header */
367 if (!mh_strcasecmp(name, TYPE_FIELD)) {
368 while (state == FLDPLUS)
369 state = m_getfld(state, name, buf,
374 /* get copies of the buffers */
378 /* if necessary, get rest of field */
379 while (state == FLDPLUS) {
380 state = m_getfld(state, name, buf,
382 vp = add(buf, vp); /* add to prev value */
385 /* Now add the header data to the list */
386 add_header(ct, np, vp);
389 /* if this wasn't the last hdr field, then continue */
395 adios(EX_CONFIG, NULL, "draft has empty body -- no directives!");
400 fseek(in, (long) (-strlen(buf)), SEEK_CUR);
405 adios(EX_CONFIG, NULL, "message format error in component #%d",
409 adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
415 ** Iterate through the list of headers and call the function to
416 ** MIME-ify them if required.
418 for (hp = ct->c_first_hf; hp != NULL; hp = hp->next) {
419 if (encode_rfc2047(hp->name, &hp->value, NULL)) {
420 adios(EX_DATAERR, NULL, "Unable to encode header \"%s\"", hp->name);
425 ** Now add the MIME-Version header field
426 ** to the list of header fields.
428 np = getcpy(VRSN_FIELD);
429 vp = concat(" ", VRSN_VALUE, "\n", NULL);
430 add_header(ct, np, vp);
433 ** We initally assume we will find multiple contents in the
434 ** draft. So create a multipart/mixed content to hold everything.
435 ** We can remove this later, if it is not needed.
437 if (get_ctinfo("multipart/mixed", ct, 0) == NOTOK) {
440 ct->c_type = CT_MULTIPART;
441 ct->c_subtype = MULTI_MIXED;
442 ct->c_file = getcpy(infile);
444 if ((m = (struct multipart *) mh_xcalloc(1, sizeof(*m))) == NULL)
445 adios(EX_OSERR, NULL, "out of memory");
446 ct->c_ctparams = (void *) m;
450 ** read and parse the composition file
451 ** and the directives it contains.
453 while (fgetstr(buf, sizeof(buf) - 1, in)) {
457 if (user_content(in, infile, buf, &p) == DONE) {
458 admonish(NULL, "ignoring spurious #end");
464 if ((part = (struct part *) mh_xcalloc(1, sizeof(*part))) == NULL)
465 adios(EX_OSERR, NULL, "out of memory");
472 ** close the composition draft since
473 ** it's not needed any longer.
477 /* check if any contents were found */
479 adios(EX_OSERR, NULL, "no content directives found");
482 ** If only one content was found, then remove and
483 ** free the outer multipart content.
485 if (!m->mp_parts->mp_next) {
488 p = m->mp_parts->mp_part;
489 m->mp_parts->mp_part = NULL;
491 /* move header fields */
492 p->c_first_hf = ct->c_first_hf;
493 p->c_last_hf = ct->c_last_hf;
494 ct->c_first_hf = NULL;
495 ct->c_last_hf = NULL;
504 ** Fill out, or expand directives. Parse and execute
505 ** commands specified by profile composition strings.
509 if ((cp = strchr(prefix, 'a')) == NULL)
510 adios(EX_SOFTWARE, NULL, "internal error(4)");
513 ** Scan the contents. Choose a transfer encoding, and
514 ** check if prefix for multipart boundary clashes with
515 ** any of the contents.
517 while (scan_content(ct) == NOTOK) {
522 adios(EX_SOFTWARE, NULL, "giving up trying to find a unique delimiter string");
528 /* Build the rest of the header field structures */
536 ** Set up structures for placing unencoded
537 ** content when building parts.
541 init_decoded_content(CT ct)
545 if ((ce = (CE) mh_xcalloc(1, sizeof(*ce))) == NULL)
546 adios(EX_OSERR, NULL, "out of memory");
549 ct->c_ceopenfnx = open7Bit; /* since unencoded */
550 ct->c_ceclosefnx = close_encoding;
551 ct->c_cesizefnx = NULL; /* since unencoded */
558 fgetstr(char *s, int n, FILE *stream)
562 for (ep = (cp = s) + n; cp < ep; ) {
565 if (!fgets(cp, n, stream))
566 return (cp != s ? s : NULL);
567 if (cp == s && *cp != '#')
570 cp += (i = strlen(cp)) - 1;
571 if (i <= 1 || *cp-- != '\n' || *cp != '\\')
582 ** Parse the composition draft for text and directives.
583 ** Do initial setup of Content structure.
587 user_content(FILE *in, char *file, char *buf, CT *ctp)
595 struct str2init *s2i;
600 if (buf[0] == '\n' || strcmp(buf, "#\n") == 0) {
605 /* allocate basic Content structure */
606 if ((ct = (CT) mh_xcalloc(1, sizeof(*ct))) == NULL)
607 adios(EX_OSERR, NULL, "out of memory");
610 /* allocate basic structure for handling decoded content */
611 init_decoded_content(ct);
618 ** Handle inline text. Check if line
619 ** is one of the following forms:
621 ** 1) doesn't begin with '#' (implicit directive)
622 ** 2) begins with "##" (implicit directive)
623 ** 3) begins with "#<"
625 if (buf[0] != '#' || buf[1] == '#' || buf[1] == '<') {
629 char content[BUFSIZ];
633 cp = m_mktemp2(NULL, invo_name, NULL, &out);
635 adios(EX_CANTCREAT, "mhbuild", "unable to create temporary file");
637 /* use a temp file to collect the plain text lines */
638 ce->ce_file = getcpy(cp);
641 if (buf[0] == '#' && buf[1] == '<') {
642 strncpy(content, buf + 2, sizeof(content));
649 /* the directive is implicit */
650 strncpy(content, "text/plain", sizeof(content));
652 strncpy(buffer, buf[0] != '#' ? buf : buf + 1, sizeof(buffer));
656 if (headers >= 0 && uprf(buffer, DESCR_FIELD) &&
657 buffer[i=strlen(DESCR_FIELD)] == ':') {
661 ct->c_descr = add(buffer + i + 1, ct->c_descr);
662 if (!fgetstr(buffer, sizeof(buffer) - 1, in))
663 adios(EX_DATAERR, NULL, "end-of-file after %s: field in plaintext", DESCR_FIELD);
671 adios(EX_DATAERR, NULL, "#-directive after %s: field in plaintext", DESCR_FIELD);
679 if (headers >= 0 && uprf(buffer, DISPO_FIELD)
680 && buffer[i = strlen(DISPO_FIELD)] == ':') {
684 ct->c_dispo = add(buffer + i + 1, ct->c_dispo);
685 if (!fgetstr(buffer, sizeof(buffer) - 1, in))
686 adios(EX_DATAERR, NULL, "end-of-file after %s: field in plaintext", DISPO_FIELD);
694 adios(EX_DATAERR, NULL, "#-directive after %s: field in plaintext", DISPO_FIELD);
702 if (headers != 1 || buffer[0] != '\n')
708 if ((cp = fgetstr(buffer, sizeof(buffer) - 1, in))
711 if (buffer[0] == '#') {
714 if (buffer[1] != '#')
716 for (cp = (bp = buffer) + 1; *cp; cp++)
724 /* parse content type */
725 if (get_ctinfo(content, ct, inlineD) == NOTOK)
728 for (s2i = str2cts; s2i->si_key; s2i++)
729 if (!mh_strcasecmp(ci->ci_type, s2i->si_key))
731 if (!s2i->si_key && !uprf(ci->ci_type, "X-"))
735 ** check type specified (possibly implicitly)
737 switch (ct->c_type = s2i->si_val) {
739 if (!mh_strcasecmp(ci->ci_subtype, "rfc822")) {
740 ct->c_encoding = CE_7BIT;
745 adios(EX_DATAERR, NULL, "it doesn't make sense to define an in-line %s content",
746 ct->c_type == CT_MESSAGE ? "message" :
752 if ((ct->c_ctinitfnx = s2i->si_init))
753 (*ct->c_ctinitfnx) (ct);
758 fseek(in, pos, SEEK_SET);
763 ** If we've reached this point, the next line
764 ** must be some type of explicit directive.
768 adios(EX_DATAERR, NULL, "The #@ directive i.e. message/external-body "
769 "is not supported anymore.");
772 /* parse directive */
773 if (get_ctinfo(buf+1, ct, 1) == NOTOK)
776 /* check directive against the list of MIME types */
777 for (s2i = str2cts; s2i->si_key; s2i++)
778 if (!mh_strcasecmp(ci->ci_type, s2i->si_key))
782 ** Check if the directive specified a valid type.
783 ** This will happen if it was one of the following forms:
789 adios(EX_DATAERR, NULL, "missing subtype in \"#%s\"", ci->ci_type);
791 switch (ct->c_type = s2i->si_val) {
793 adios(EX_DATAERR, NULL, "use \"#begin ... #end\" instead of \"#%s/%s\"", ci->ci_type, ci->ci_subtype);
797 if (!mh_strcasecmp(ci->ci_subtype, "partial") ||
798 !mh_strcasecmp(ci->ci_subtype,
800 adios(EX_DATAERR, NULL, "sorry, \"#%s/%s\" isn't supported", ci->ci_type, ci->ci_subtype);
803 admonish(NULL, "use \"#forw [+folder] [msgs]\" instead of \"#%s/%s\"", ci->ci_type, ci->ci_subtype);
807 if ((ct->c_ctinitfnx = s2i->si_init))
808 (*ct->c_ctinitfnx) (ct);
812 /* Handle [file] argument */
814 /* check if specifies command to execute */
815 if (*ci->ci_magic == '|' || *ci->ci_magic == '!') {
816 for (cp = ci->ci_magic + 1; isspace(*cp); cp++)
819 adios(EX_DATAERR, NULL, "empty pipe command for #%s directive", ci->ci_type);
824 /* record filename of decoded contents */
825 ce->ce_file = ci->ci_magic;
826 if (access(ce->ce_file, R_OK) == NOTOK)
827 adios(EX_IOERR, "reading", "unable to access %s for", ce->ce_file);
834 ** No [file] argument, so check profile for
835 ** method to compose content.
837 snprintf(buffer, sizeof(buffer), "%s-compose-%s/%s",
838 invo_name, ci->ci_type, ci->ci_subtype);
839 if ((cp = context_find(buffer)) == NULL || *cp == '\0') {
840 snprintf(buffer, sizeof(buffer), "%s-compose-%s",
841 invo_name, ci->ci_type);
842 if ((cp = context_find(buffer)) == NULL ||
844 content_error(NULL, ct, "don't know how to compose content");
848 ci->ci_magic = getcpy(cp);
854 ** #forw [+folder] [msgs]
856 if (!mh_strcasecmp(ci->ci_type, "forw")) {
858 char *folder, *arguments[MAXARGS];
864 ap = brkstring(ci->ci_magic, " ", "\n");
865 for (i=0; ap[i] && i<MAXARGS-1; i++) {
866 arguments[i] = ap[i];
871 arguments[0] = seq_cur;
876 /* search the arguments for a folder name */
877 for (ap = arguments; *ap; ap++) {
879 if (*cp == '+' || *cp == '@') {
881 adios(EX_USAGE, NULL, "only one folder per #forw directive");
883 folder = getcpy(expandfol(cp));
887 /* else, use the current folder */
889 folder = getcpy(getcurfol());
891 if (!(mp = folder_read(folder)))
892 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
893 for (ap = arguments; *ap; ap++) {
895 if (*cp != '+' && *cp != '@')
896 if (!m_convert(mp, cp))
903 ** If there is more than one message to include, make this
904 ** a content of type "multipart/digest" and insert each message
905 ** as a subpart. If there is only one message, then make this
906 ** a content of type "message/rfc822".
908 if (mp->numsel > 1) {
909 /* we are forwarding multiple messages */
910 if (get_ctinfo("multipart/digest", ct, 0) == NOTOK)
912 ct->c_type = CT_MULTIPART;
913 ct->c_subtype = MULTI_DIGEST;
915 if ((m = (struct multipart *)
916 mh_xcalloc(1, sizeof(*m))) == NULL)
917 adios(EX_OSERR, NULL, "out of memory");
918 ct->c_ctparams = (void *) m;
921 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
922 if (is_selected(mp, msgnum)) {
927 if ((p = (CT) mh_xcalloc(1, sizeof(*p)))
929 adios(EX_OSERR, NULL, "out of memory");
930 init_decoded_content(p);
932 if (get_ctinfo("message/rfc822", p, 0)
935 p->c_type = CT_MESSAGE;
936 p->c_subtype = MESSAGE_RFC822;
938 snprintf(buffer, sizeof(buffer),
939 "%s/%d", mp->foldpath,
941 pe->ce_file = getcpy(buffer);
943 if ((part = (struct part *) mh_xcalloc(1, sizeof(*part))) == NULL)
944 adios(EX_OSERR, NULL, "out of memory");
951 /* we are forwarding one message */
952 if (get_ctinfo("message/rfc822", ct, 0) == NOTOK)
954 ct->c_type = CT_MESSAGE;
955 ct->c_subtype = MESSAGE_RFC822;
958 snprintf(buffer, sizeof(buffer), "%s/%d",
959 mp->foldpath, msgnum);
960 ce->ce_file = getcpy(buffer);
963 folder_free(mp); /* free folder/message structure */
970 if (!mh_strcasecmp(ci->ci_type, "end")) {
977 ** #begin [ alternative | parallel ]
979 if (!mh_strcasecmp(ci->ci_type, "begin")) {
982 cp = SubMultiPart[vrsn - 1].kv_key;
983 } else if (!mh_strcasecmp(ci->ci_magic, "alternative")) {
984 vrsn = MULTI_ALTERNATE;
985 cp = SubMultiPart[vrsn - 1].kv_key;
986 } else if (!mh_strcasecmp(ci->ci_magic, "parallel")) {
987 vrsn = MULTI_PARALLEL;
988 cp = SubMultiPart[vrsn - 1].kv_key;
989 } else if (uprf(ci->ci_magic, "digest")) {
992 vrsn = MULTI_UNKNOWN;
997 snprintf(buffer, sizeof(buffer), "multipart/%s", cp);
998 if (get_ctinfo(buffer, ct, 0) == NOTOK)
1000 ct->c_type = CT_MULTIPART;
1001 ct->c_subtype = vrsn;
1003 if ((m = (struct multipart *) mh_xcalloc(1, sizeof(*m))) == NULL)
1004 adios(EX_OSERR, NULL, "out of memory");
1005 ct->c_ctparams = (void *) m;
1008 while (fgetstr(buffer, sizeof(buffer) - 1, in)) {
1012 if (user_content(in, file, buffer, &p) == DONE) {
1014 adios(EX_DATAERR, NULL, "empty \"#begin ... #end\" sequence");
1020 if ((part = (struct part *)
1021 mh_xcalloc(1, sizeof(*part))) == NULL)
1022 adios(EX_OSERR, NULL, "out of memory");
1024 pp = &part->mp_next;
1027 admonish(NULL, "premature end-of-file, missing #end");
1032 ** Unknown directive
1034 adios(EX_DATAERR, NULL, "unknown directive \"#%s\"", ci->ci_type);
1035 return NOTOK; /* NOT REACHED */
1040 set_id(CT ct, int top)
1044 static time_t clock = 0;
1045 static char *msgfmt;
1049 snprintf(msgid, sizeof(msgid), "<%d.%ld.%%d@%s>\n",
1050 (int) getpid(), (long) clock, LocalName());
1052 msgfmt = getcpy(msgid);
1054 snprintf(msgid, sizeof(msgid), msgfmt, top ? 0 : ++partno);
1055 ct->c_id = getcpy(msgid);
1060 ** Fill out, or expand the various contents in the composition
1061 ** draft. Read-in any necessary files. Parse and execute any
1062 ** commands specified by profile composition strings.
1066 compose_content(CT ct)
1068 CE ce = ct->c_cefile;
1070 switch (ct->c_type) {
1075 char partnam[BUFSIZ];
1076 struct multipart *m = (struct multipart *) ct->c_ctparams;
1080 snprintf(partnam, sizeof(partnam), "%s.",
1082 pp = partnam + strlen(partnam);
1087 /* first, we call compose_content on all the subparts */
1088 for (part = m->mp_parts, partnum = 1; part;
1089 part = part->mp_next, partnum++) {
1090 CT p = part->mp_part;
1092 sprintf(pp, "%d", partnum);
1093 p->c_partno = getcpy(partnam);
1094 if (compose_content(p) == NOTOK)
1101 /* Nothing to do for type message */
1105 ** Discrete types (text/application/audio/image/video)
1110 int xstdout, len, buflen;
1111 char *bp, **ap, *cp;
1112 char *vec[4], buffer[BUFSIZ];
1114 CI ci = &ct->c_ctinfo;
1117 if (!(cp = ci->ci_magic))
1118 adios(EX_SOFTWARE, NULL, "internal error(5)");
1120 tfile = m_mktemp2(NULL, invo_name, NULL, NULL);
1121 if (tfile == NULL) {
1122 adios(EX_CANTCREAT, "mhbuild", "unable to create temporary file");
1124 ce->ce_file = getcpy(tfile);
1129 /* Get buffer ready to go */
1132 buflen = sizeof(buffer);
1135 ** Parse composition string into buffer
1137 for ( ; *cp; cp++) {
1143 ** insert parameters from
1149 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1150 snprintf(bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
1160 /* %f, and stdout is not-redirected */
1166 ** insert temporary filename
1167 ** where content should be
1170 snprintf(bp, buflen, "%s", ce->ce_file);
1174 /* insert content subtype */
1175 strncpy(bp, ci->ci_subtype, buflen);
1179 /* insert character % */
1200 printf("composing content %s/%s from command\n\t%s\n", ci->ci_type, ci->ci_subtype, buffer);
1202 fflush(stdout); /* not sure if need for -noverbose */
1209 if ((out = fopen(ce->ce_file, "w")) == NULL)
1210 adios(EX_IOERR, ce->ce_file, "unable to open for writing");
1212 switch (child_id = fork()) {
1214 adios(EX_OSERR, "fork", "unable to fork");
1219 dup2(fileno(out), 1);
1221 execvp("/bin/sh", vec);
1222 fprintf(stderr, "unable to exec ");
1229 if (pidXwait(child_id, NULL))
1242 ** Scan the content.
1244 ** 1) choose a transfer encoding.
1245 ** 2) check for clashes with multipart boundary string.
1246 ** 3) for text content, figure out which character set is being used.
1248 ** If there is a clash with one of the contents and the multipart boundary,
1249 ** this function will exit with NOTOK. This will cause the scanning process
1250 ** to be repeated with a different multipart boundary. It is possible
1251 ** (although highly unlikely) that this scan will be repeated multiple times.
1258 int check8bit = 0, contains8bit = 0;
1259 int checklinelen = 0, linelen = 0;
1260 int checkboundary = 0, boundaryclash = 0;
1261 int checklinespace = 0, linespace = 0; /* trailing whitespace */
1262 unsigned char *cp = NULL, buffer[BUFSIZ];
1263 struct text *t = NULL;
1265 CE ce = ct->c_cefile;
1268 ** handle multipart by scanning all subparts
1269 ** and then checking their encoding.
1271 if (ct->c_type == CT_MULTIPART) {
1272 struct multipart *m = (struct multipart *) ct->c_ctparams;
1275 /* initially mark the domain of enclosing multipart as 7bit */
1276 ct->c_encoding = CE_7BIT;
1278 for (part = m->mp_parts; part; part = part->mp_next) {
1279 CT p = part->mp_part;
1281 if (scan_content(p) == NOTOK) {
1282 /* choose encoding for subpart */
1287 ** if necessary, enlarge encoding for enclosing
1290 if (p->c_encoding == CE_BINARY)
1291 ct->c_encoding = CE_BINARY;
1292 if (p->c_encoding == CE_8BIT &&
1293 ct->c_encoding != CE_BINARY)
1294 ct->c_encoding = CE_8BIT;
1301 ** Decide what to check while scanning this content.
1303 switch (ct->c_type) {
1307 if (ct->c_subtype == TEXT_PLAIN) {
1323 case CT_APPLICATION:
1335 ** Scan the unencoded content
1337 if (check8bit || checklinelen || checklinespace || checkboundary) {
1338 if ((in = fopen(ce->ce_file, "r")) == NULL)
1339 adios(EX_IOERR, ce->ce_file, "unable to open for reading");
1340 len = strlen(prefix);
1342 while (fgets(buffer, sizeof(buffer) - 1, in)) {
1344 for (cp = buffer; *cp; cp++) {
1345 if (!isascii(*cp)) {
1347 /* no need to keep checking */
1353 if (checklinelen && (strlen(buffer) > CPERLIN + 1)) {
1355 checklinelen = 0; /* no need to keep checking */
1358 if (checklinespace &&
1359 (cp = buffer + strlen(buffer) - 2) >
1360 buffer && isspace(*cp)) {
1362 /* no need to keep checking */
1367 ** Check if content contains a line that clashes
1368 ** with our standard boundary for multipart messages.
1370 if (checkboundary && buffer[0] == '-' &&
1372 for (cp = buffer + strlen(buffer) - 1;
1377 if (strncmp(buffer + 2, prefix, len)==0 &&
1378 isdigit(buffer[2 + len])) {
1380 /* no need to keep checking */
1389 ** Decide which transfer encoding to use.
1391 switch (ct->c_type) {
1394 ** If the text content didn't specify a character
1395 ** set, we need to figure out which one was used.
1397 t = (struct text *) ct->c_ctparams;
1398 if (t->tx_charset == CHARSET_UNSPECIFIED) {
1399 CI ci = &ct->c_ctinfo;
1402 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
1406 t->tx_charset = CHARSET_UNKNOWN;
1407 *ap = concat("charset=", write_charset_8bit(),
1410 t->tx_charset = CHARSET_USASCII;
1411 *ap = getcpy("charset=us-ascii");
1414 cp = strchr(*ap++, '=');
1420 if (contains8bit || linelen || linespace)
1421 ct->c_encoding = CE_QUOTED;
1423 ct->c_encoding = CE_7BIT;
1427 ct->c_encoding = CE_7BIT;
1430 case CT_APPLICATION:
1435 ** Forcing use of base64, because these types likely
1436 ** contain binary data and NUL bytes. Don't care about
1437 ** files that would be clean.
1439 ct->c_encoding = CE_BASE64;
1443 return (boundaryclash ? NOTOK : OK);
1448 ** Scan the content structures, and build header
1449 ** fields that will need to be output into the
1454 build_headers(CT ct)
1458 char *np, *vp, buffer[BUFSIZ];
1459 CI ci = &ct->c_ctinfo;
1462 ** If message is type multipart, then add the multipart
1463 ** boundary to the list of attribute/value pairs.
1465 if (ct->c_type == CT_MULTIPART) {
1467 static int level = 0; /* store nesting level */
1471 snprintf(buffer, sizeof(buffer), "boundary=%s%d",
1473 cp = strchr(*ap++ = getcpy(buffer), '=');
1480 ** output the content type and subtype
1482 np = getcpy(TYPE_FIELD);
1483 vp = concat(" ", ci->ci_type, "/", ci->ci_subtype, NULL);
1485 /* keep track of length of line */
1486 len = strlen(TYPE_FIELD) + strlen(ci->ci_type) +
1487 strlen(ci->ci_subtype) + 3;
1490 ** Append the attribute/value pairs to
1491 ** the end of the Content-Type line.
1493 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1497 snprintf(buffer, sizeof(buffer), "%s=\"%s\"", *ap, *ep);
1498 if (len + 1 + (cc = strlen(buffer)) >= CPERLIN) {
1499 vp = add("\n\t", vp);
1505 vp = add(buffer, vp);
1510 ** Append any RFC-822 comment to the end of
1511 ** the Content-Type line.
1513 if (ci->ci_comment) {
1514 snprintf(buffer, sizeof(buffer), "(%s)", ci->ci_comment);
1515 if (len + 1 + (cc = 2 + strlen(ci->ci_comment)) >= CPERLIN) {
1516 vp = add("\n\t", vp);
1522 vp = add(buffer, vp);
1526 add_header(ct, np, vp);
1529 ** output the Content-ID
1532 np = getcpy(ID_FIELD);
1533 vp = concat(" ", ct->c_id, NULL);
1534 add_header(ct, np, vp);
1538 ** output the Content-Description
1541 np = getcpy(DESCR_FIELD);
1542 vp = concat(" ", ct->c_descr, NULL);
1543 if (encode_rfc2047(DESCR_FIELD, &vp, NULL)) {
1544 adios(EX_DATAERR, NULL, "Unable to encode %s header", DESCR_FIELD);
1546 add_header(ct, np, vp);
1550 ** output the Content-Disposition
1553 np = getcpy(DISPO_FIELD);
1554 vp = concat(" ", ct->c_dispo, NULL);
1555 add_header(ct, np, vp);
1559 ** output the Content-Transfer-Encoding
1561 switch (ct->c_encoding) {
1563 /* Nothing to output */
1567 if (ct->c_type == CT_MESSAGE)
1568 adios(EX_DATAERR, NULL, "internal error, invalid encoding");
1570 np = getcpy(ENCODING_FIELD);
1571 vp = concat(" ", "8bit", "\n", NULL);
1572 add_header(ct, np, vp);
1576 if (ct->c_type == CT_MESSAGE || ct->c_type == CT_MULTIPART)
1577 adios(EX_DATAERR, NULL, "internal error, invalid encoding");
1579 np = getcpy(ENCODING_FIELD);
1580 vp = concat(" ", "quoted-printable", "\n", NULL);
1581 add_header(ct, np, vp);
1585 if (ct->c_type == CT_MESSAGE || ct->c_type == CT_MULTIPART)
1586 adios(EX_DATAERR, NULL, "internal error, invalid encoding");
1588 np = getcpy(ENCODING_FIELD);
1589 vp = concat(" ", "base64", "\n", NULL);
1590 add_header(ct, np, vp);
1594 if (ct->c_type == CT_MESSAGE)
1595 adios(EX_DATAERR, NULL, "internal error, invalid encoding");
1597 np = getcpy(ENCODING_FIELD);
1598 vp = concat(" ", "binary", "\n", NULL);
1599 add_header(ct, np, vp);
1603 adios(EX_DATAERR, NULL, "unknown transfer encoding in content");
1608 ** Additional content specific header processing
1610 switch (ct->c_type) {
1613 struct multipart *m;
1616 m = (struct multipart *) ct->c_ctparams;
1617 for (part = m->mp_parts; part; part = part->mp_next) {