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)) {
354 /* abort if draft has Mime-Version header field */
355 if (!mh_strcasecmp(name, VRSN_FIELD))
356 adios(EX_CONFIG, NULL, "draft shouldn't contain %s: field", VRSN_FIELD);
359 ** abort if draft has Content-Transfer-Encoding
362 if (!mh_strcasecmp(name, ENCODING_FIELD))
363 adios(EX_CONFIG, NULL, "draft shouldn't contain %s: field", ENCODING_FIELD);
365 /* ignore any Content-Type fields in the header */
366 if (!mh_strcasecmp(name, TYPE_FIELD)) {
367 while (state == FLDPLUS)
368 state = m_getfld(state, name, buf,
373 /* get copies of the buffers */
377 /* if necessary, get rest of field */
378 while (state == FLDPLUS) {
379 state = m_getfld(state, name, buf,
381 vp = add(buf, vp); /* add to prev value */
384 /* Now add the header data to the list */
385 add_header(ct, np, vp);
390 adios(EX_CONFIG, NULL, "draft has empty body -- no directives!");
394 fseek(in, (long) (-strlen(buf)), SEEK_CUR);
399 adios(EX_CONFIG, NULL, "message format error in component #%d",
403 adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
409 ** Iterate through the list of headers and call the function to
410 ** MIME-ify them if required.
412 for (hp = ct->c_first_hf; hp != NULL; hp = hp->next) {
413 if (encode_rfc2047(hp->name, &hp->value, NULL)) {
414 adios(EX_DATAERR, NULL, "Unable to encode header \"%s\"", hp->name);
419 ** Now add the MIME-Version header field
420 ** to the list of header fields.
422 np = getcpy(VRSN_FIELD);
423 vp = concat(" ", VRSN_VALUE, "\n", NULL);
424 add_header(ct, np, vp);
427 ** We initally assume we will find multiple contents in the
428 ** draft. So create a multipart/mixed content to hold everything.
429 ** We can remove this later, if it is not needed.
431 if (get_ctinfo("multipart/mixed", ct, 0) == NOTOK) {
434 ct->c_type = CT_MULTIPART;
435 ct->c_subtype = MULTI_MIXED;
436 ct->c_file = getcpy(infile);
438 if ((m = (struct multipart *) mh_xcalloc(1, sizeof(*m))) == NULL)
439 adios(EX_OSERR, NULL, "out of memory");
440 ct->c_ctparams = (void *) m;
444 ** read and parse the composition file
445 ** and the directives it contains.
447 while (fgetstr(buf, sizeof(buf) - 1, in)) {
451 if (user_content(in, infile, buf, &p) == DONE) {
452 admonish(NULL, "ignoring spurious #end");
458 if ((part = (struct part *) mh_xcalloc(1, sizeof(*part))) == NULL)
459 adios(EX_OSERR, NULL, "out of memory");
466 ** close the composition draft since
467 ** it's not needed any longer.
471 /* check if any contents were found */
473 adios(EX_OSERR, NULL, "no content directives found");
476 ** If only one content was found, then remove and
477 ** free the outer multipart content.
479 if (!m->mp_parts->mp_next) {
482 p = m->mp_parts->mp_part;
483 m->mp_parts->mp_part = NULL;
485 /* move header fields */
486 p->c_first_hf = ct->c_first_hf;
487 p->c_last_hf = ct->c_last_hf;
488 ct->c_first_hf = NULL;
489 ct->c_last_hf = NULL;
498 ** Fill out, or expand directives. Parse and execute
499 ** commands specified by profile composition strings.
503 if ((cp = strchr(prefix, 'a')) == NULL)
504 adios(EX_SOFTWARE, NULL, "internal error(4)");
507 ** Scan the contents. Choose a transfer encoding, and
508 ** check if prefix for multipart boundary clashes with
509 ** any of the contents.
511 while (scan_content(ct) == NOTOK) {
516 adios(EX_SOFTWARE, NULL, "giving up trying to find a unique delimiter string");
522 /* Build the rest of the header field structures */
530 ** Set up structures for placing unencoded
531 ** content when building parts.
535 init_decoded_content(CT ct)
539 if ((ce = (CE) mh_xcalloc(1, sizeof(*ce))) == NULL)
540 adios(EX_OSERR, NULL, "out of memory");
543 ct->c_ceopenfnx = open7Bit; /* since unencoded */
544 ct->c_ceclosefnx = close_encoding;
545 ct->c_cesizefnx = NULL; /* since unencoded */
552 fgetstr(char *s, int n, FILE *stream)
556 for (ep = (cp = s) + n; cp < ep; ) {
559 if (!fgets(cp, n, stream))
560 return (cp != s ? s : NULL);
561 if (cp == s && *cp != '#')
564 cp += (i = strlen(cp)) - 1;
565 if (i <= 1 || *cp-- != '\n' || *cp != '\\')
576 ** Parse the composition draft for text and directives.
577 ** Do initial setup of Content structure.
581 user_content(FILE *in, char *file, char *buf, CT *ctp)
589 struct str2init *s2i;
594 if (buf[0] == '\n' || strcmp(buf, "#\n") == 0) {
599 /* allocate basic Content structure */
600 if ((ct = (CT) mh_xcalloc(1, sizeof(*ct))) == NULL)
601 adios(EX_OSERR, NULL, "out of memory");
604 /* allocate basic structure for handling decoded content */
605 init_decoded_content(ct);
612 ** Handle inline text. Check if line
613 ** is one of the following forms:
615 ** 1) doesn't begin with '#' (implicit directive)
616 ** 2) begins with "##" (implicit directive)
617 ** 3) begins with "#<"
619 if (buf[0] != '#' || buf[1] == '#' || buf[1] == '<') {
623 char content[BUFSIZ];
627 cp = m_mktemp2(NULL, invo_name, NULL, &out);
629 adios(EX_CANTCREAT, "mhbuild", "unable to create temporary file");
631 /* use a temp file to collect the plain text lines */
632 ce->ce_file = getcpy(cp);
635 if (buf[0] == '#' && buf[1] == '<') {
636 strncpy(content, buf + 2, sizeof(content));
643 /* the directive is implicit */
644 strncpy(content, "text/plain", sizeof(content));
646 strncpy(buffer, buf[0] != '#' ? buf : buf + 1, sizeof(buffer));
650 if (headers >= 0 && uprf(buffer, DESCR_FIELD) &&
651 buffer[i=strlen(DESCR_FIELD)] == ':') {
655 ct->c_descr = add(buffer + i + 1, ct->c_descr);
656 if (!fgetstr(buffer, sizeof(buffer) - 1, in))
657 adios(EX_DATAERR, NULL, "end-of-file after %s: field in plaintext", DESCR_FIELD);
665 adios(EX_DATAERR, NULL, "#-directive after %s: field in plaintext", DESCR_FIELD);
673 if (headers >= 0 && uprf(buffer, DISPO_FIELD)
674 && buffer[i = strlen(DISPO_FIELD)] == ':') {
678 ct->c_dispo = add(buffer + i + 1, ct->c_dispo);
679 if (!fgetstr(buffer, sizeof(buffer) - 1, in))
680 adios(EX_DATAERR, NULL, "end-of-file after %s: field in plaintext", DISPO_FIELD);
688 adios(EX_DATAERR, NULL, "#-directive after %s: field in plaintext", DISPO_FIELD);
696 if (headers != 1 || buffer[0] != '\n')
702 if ((cp = fgetstr(buffer, sizeof(buffer) - 1, in))
705 if (buffer[0] == '#') {
708 if (buffer[1] != '#')
710 for (cp = (bp = buffer) + 1; *cp; cp++)
718 /* parse content type */
719 if (get_ctinfo(content, ct, inlineD) == NOTOK)
722 for (s2i = str2cts; s2i->si_key; s2i++)
723 if (!mh_strcasecmp(ci->ci_type, s2i->si_key))
725 if (!s2i->si_key && !uprf(ci->ci_type, "X-"))
729 ** check type specified (possibly implicitly)
731 switch (ct->c_type = s2i->si_val) {
733 if (!mh_strcasecmp(ci->ci_subtype, "rfc822")) {
734 ct->c_encoding = CE_7BIT;
739 adios(EX_DATAERR, NULL, "it doesn't make sense to define an in-line %s content",
740 ct->c_type == CT_MESSAGE ? "message" :
746 if ((ct->c_ctinitfnx = s2i->si_init))
747 (*ct->c_ctinitfnx) (ct);
752 fseek(in, pos, SEEK_SET);
757 ** If we've reached this point, the next line
758 ** must be some type of explicit directive.
762 adios(EX_DATAERR, NULL, "The #@ directive i.e. message/external-body "
763 "is not supported anymore.");
766 /* parse directive */
767 if (get_ctinfo(buf+1, ct, 1) == NOTOK)
770 /* check directive against the list of MIME types */
771 for (s2i = str2cts; s2i->si_key; s2i++)
772 if (!mh_strcasecmp(ci->ci_type, s2i->si_key))
776 ** Check if the directive specified a valid type.
777 ** This will happen if it was one of the following forms:
783 adios(EX_DATAERR, NULL, "missing subtype in \"#%s\"", ci->ci_type);
785 switch (ct->c_type = s2i->si_val) {
787 adios(EX_DATAERR, NULL, "use \"#begin ... #end\" instead of \"#%s/%s\"", ci->ci_type, ci->ci_subtype);
791 if (!mh_strcasecmp(ci->ci_subtype, "partial") ||
792 !mh_strcasecmp(ci->ci_subtype,
794 adios(EX_DATAERR, NULL, "sorry, \"#%s/%s\" isn't supported", ci->ci_type, ci->ci_subtype);
797 admonish(NULL, "use \"#forw [+folder] [msgs]\" instead of \"#%s/%s\"", ci->ci_type, ci->ci_subtype);
801 if ((ct->c_ctinitfnx = s2i->si_init))
802 (*ct->c_ctinitfnx) (ct);
806 /* Handle [file] argument */
808 /* check if specifies command to execute */
809 if (*ci->ci_magic == '|' || *ci->ci_magic == '!') {
810 for (cp = ci->ci_magic + 1; isspace(*cp); cp++)
813 adios(EX_DATAERR, NULL, "empty pipe command for #%s directive", ci->ci_type);
818 /* record filename of decoded contents */
819 ce->ce_file = ci->ci_magic;
820 if (access(ce->ce_file, R_OK) == NOTOK)
821 adios(EX_IOERR, "reading", "unable to access %s for", ce->ce_file);
828 ** No [file] argument, so check profile for
829 ** method to compose content.
831 snprintf(buffer, sizeof(buffer), "%s-compose-%s/%s",
832 invo_name, ci->ci_type, ci->ci_subtype);
833 if ((cp = context_find(buffer)) == NULL || *cp == '\0') {
834 snprintf(buffer, sizeof(buffer), "%s-compose-%s",
835 invo_name, ci->ci_type);
836 if ((cp = context_find(buffer)) == NULL ||
838 content_error(NULL, ct, "don't know how to compose content");
842 ci->ci_magic = getcpy(cp);
848 ** #forw [+folder] [msgs]
850 if (!mh_strcasecmp(ci->ci_type, "forw")) {
852 char *folder, *arguments[MAXARGS];
858 ap = brkstring(ci->ci_magic, " ", "\n");
859 for (i=0; ap[i] && i<MAXARGS-1; i++) {
860 arguments[i] = ap[i];
865 arguments[0] = seq_cur;
870 /* search the arguments for a folder name */
871 for (ap = arguments; *ap; ap++) {
873 if (*cp == '+' || *cp == '@') {
875 adios(EX_USAGE, NULL, "only one folder per #forw directive");
877 folder = getcpy(expandfol(cp));
881 /* else, use the current folder */
883 folder = getcpy(getcurfol());
885 if (!(mp = folder_read(folder)))
886 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
887 for (ap = arguments; *ap; ap++) {
889 if (*cp != '+' && *cp != '@')
890 if (!m_convert(mp, cp))
897 ** If there is more than one message to include, make this
898 ** a content of type "multipart/digest" and insert each message
899 ** as a subpart. If there is only one message, then make this
900 ** a content of type "message/rfc822".
902 if (mp->numsel > 1) {
903 /* we are forwarding multiple messages */
904 if (get_ctinfo("multipart/digest", ct, 0) == NOTOK)
906 ct->c_type = CT_MULTIPART;
907 ct->c_subtype = MULTI_DIGEST;
909 if ((m = (struct multipart *)
910 mh_xcalloc(1, sizeof(*m))) == NULL)
911 adios(EX_OSERR, NULL, "out of memory");
912 ct->c_ctparams = (void *) m;
915 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
916 if (is_selected(mp, msgnum)) {
921 if ((p = (CT) mh_xcalloc(1, sizeof(*p)))
923 adios(EX_OSERR, NULL, "out of memory");
924 init_decoded_content(p);
926 if (get_ctinfo("message/rfc822", p, 0)
929 p->c_type = CT_MESSAGE;
930 p->c_subtype = MESSAGE_RFC822;
932 snprintf(buffer, sizeof(buffer),
933 "%s/%d", mp->foldpath,
935 pe->ce_file = getcpy(buffer);
937 if ((part = (struct part *) mh_xcalloc(1, sizeof(*part))) == NULL)
938 adios(EX_OSERR, NULL, "out of memory");
945 /* we are forwarding one message */
946 if (get_ctinfo("message/rfc822", ct, 0) == NOTOK)
948 ct->c_type = CT_MESSAGE;
949 ct->c_subtype = MESSAGE_RFC822;
952 snprintf(buffer, sizeof(buffer), "%s/%d",
953 mp->foldpath, msgnum);
954 ce->ce_file = getcpy(buffer);
957 folder_free(mp); /* free folder/message structure */
964 if (!mh_strcasecmp(ci->ci_type, "end")) {
971 ** #begin [ alternative | parallel ]
973 if (!mh_strcasecmp(ci->ci_type, "begin")) {
976 cp = SubMultiPart[vrsn - 1].kv_key;
977 } else if (!mh_strcasecmp(ci->ci_magic, "alternative")) {
978 vrsn = MULTI_ALTERNATE;
979 cp = SubMultiPart[vrsn - 1].kv_key;
980 } else if (!mh_strcasecmp(ci->ci_magic, "parallel")) {
981 vrsn = MULTI_PARALLEL;
982 cp = SubMultiPart[vrsn - 1].kv_key;
983 } else if (uprf(ci->ci_magic, "digest")) {
986 vrsn = MULTI_UNKNOWN;
991 snprintf(buffer, sizeof(buffer), "multipart/%s", cp);
992 if (get_ctinfo(buffer, ct, 0) == NOTOK)
994 ct->c_type = CT_MULTIPART;
995 ct->c_subtype = vrsn;
997 if ((m = (struct multipart *) mh_xcalloc(1, sizeof(*m))) == NULL)
998 adios(EX_OSERR, NULL, "out of memory");
999 ct->c_ctparams = (void *) m;
1002 while (fgetstr(buffer, sizeof(buffer) - 1, in)) {
1006 if (user_content(in, file, buffer, &p) == DONE) {
1008 adios(EX_DATAERR, NULL, "empty \"#begin ... #end\" sequence");
1014 if ((part = (struct part *)
1015 mh_xcalloc(1, sizeof(*part))) == NULL)
1016 adios(EX_OSERR, NULL, "out of memory");
1018 pp = &part->mp_next;
1021 admonish(NULL, "premature end-of-file, missing #end");
1026 ** Unknown directive
1028 adios(EX_DATAERR, NULL, "unknown directive \"#%s\"", ci->ci_type);
1029 return NOTOK; /* NOT REACHED */
1034 set_id(CT ct, int top)
1038 static time_t clock = 0;
1039 static char *msgfmt;
1043 snprintf(msgid, sizeof(msgid), "<%d.%ld.%%d@%s>\n",
1044 (int) getpid(), (long) clock, LocalName());
1046 msgfmt = getcpy(msgid);
1048 snprintf(msgid, sizeof(msgid), msgfmt, top ? 0 : ++partno);
1049 ct->c_id = getcpy(msgid);
1054 ** Fill out, or expand the various contents in the composition
1055 ** draft. Read-in any necessary files. Parse and execute any
1056 ** commands specified by profile composition strings.
1060 compose_content(CT ct)
1062 CE ce = ct->c_cefile;
1064 switch (ct->c_type) {
1069 char partnam[BUFSIZ];
1070 struct multipart *m = (struct multipart *) ct->c_ctparams;
1074 snprintf(partnam, sizeof(partnam), "%s.",
1076 pp = partnam + strlen(partnam);
1081 /* first, we call compose_content on all the subparts */
1082 for (part = m->mp_parts, partnum = 1; part;
1083 part = part->mp_next, partnum++) {
1084 CT p = part->mp_part;
1086 sprintf(pp, "%d", partnum);
1087 p->c_partno = getcpy(partnam);
1088 if (compose_content(p) == NOTOK)
1095 /* Nothing to do for type message */
1099 ** Discrete types (text/application/audio/image/video)
1104 int xstdout, len, buflen;
1105 char *bp, **ap, *cp;
1106 char *vec[4], buffer[BUFSIZ];
1108 CI ci = &ct->c_ctinfo;
1111 if (!(cp = ci->ci_magic))
1112 adios(EX_SOFTWARE, NULL, "internal error(5)");
1114 tfile = m_mktemp2(NULL, invo_name, NULL, NULL);
1115 if (tfile == NULL) {
1116 adios(EX_CANTCREAT, "mhbuild", "unable to create temporary file");
1118 ce->ce_file = getcpy(tfile);
1123 /* Get buffer ready to go */
1126 buflen = sizeof(buffer);
1129 ** Parse composition string into buffer
1131 for ( ; *cp; cp++) {
1137 ** insert parameters from
1143 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1144 snprintf(bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
1154 /* %f, and stdout is not-redirected */
1160 ** insert temporary filename
1161 ** where content should be
1164 snprintf(bp, buflen, "%s", ce->ce_file);
1168 /* insert content subtype */
1169 strncpy(bp, ci->ci_subtype, buflen);
1173 /* insert character % */
1194 printf("composing content %s/%s from command\n\t%s\n", ci->ci_type, ci->ci_subtype, buffer);
1196 fflush(stdout); /* not sure if need for -noverbose */
1203 if ((out = fopen(ce->ce_file, "w")) == NULL)
1204 adios(EX_IOERR, ce->ce_file, "unable to open for writing");
1206 switch (child_id = fork()) {
1208 adios(EX_OSERR, "fork", "unable to fork");
1213 dup2(fileno(out), 1);
1215 execvp("/bin/sh", vec);
1216 fprintf(stderr, "unable to exec ");
1223 if (pidXwait(child_id, NULL))
1236 ** Scan the content.
1238 ** 1) choose a transfer encoding.
1239 ** 2) check for clashes with multipart boundary string.
1240 ** 3) for text content, figure out which character set is being used.
1242 ** If there is a clash with one of the contents and the multipart boundary,
1243 ** this function will exit with NOTOK. This will cause the scanning process
1244 ** to be repeated with a different multipart boundary. It is possible
1245 ** (although highly unlikely) that this scan will be repeated multiple times.
1252 int check8bit = 0, contains8bit = 0;
1253 int checklinelen = 0, linelen = 0;
1254 int checkboundary = 0, boundaryclash = 0;
1255 int checklinespace = 0, linespace = 0; /* trailing whitespace */
1256 unsigned char *cp = NULL, buffer[BUFSIZ];
1257 struct text *t = NULL;
1259 CE ce = ct->c_cefile;
1262 ** handle multipart by scanning all subparts
1263 ** and then checking their encoding.
1265 if (ct->c_type == CT_MULTIPART) {
1266 struct multipart *m = (struct multipart *) ct->c_ctparams;
1269 /* initially mark the domain of enclosing multipart as 7bit */
1270 ct->c_encoding = CE_7BIT;
1272 for (part = m->mp_parts; part; part = part->mp_next) {
1273 CT p = part->mp_part;
1275 if (scan_content(p) == NOTOK) {
1276 /* choose encoding for subpart */
1281 ** if necessary, enlarge encoding for enclosing
1284 if (p->c_encoding == CE_BINARY)
1285 ct->c_encoding = CE_BINARY;
1286 if (p->c_encoding == CE_8BIT &&
1287 ct->c_encoding != CE_BINARY)
1288 ct->c_encoding = CE_8BIT;
1295 ** Decide what to check while scanning this content.
1297 switch (ct->c_type) {
1301 if (ct->c_subtype == TEXT_PLAIN) {
1317 case CT_APPLICATION:
1329 ** Scan the unencoded content
1331 if (check8bit || checklinelen || checklinespace || checkboundary) {
1332 if ((in = fopen(ce->ce_file, "r")) == NULL)
1333 adios(EX_IOERR, ce->ce_file, "unable to open for reading");
1334 len = strlen(prefix);
1336 while (fgets(buffer, sizeof(buffer) - 1, in)) {
1338 for (cp = buffer; *cp; cp++) {
1339 if (!isascii(*cp)) {
1341 /* no need to keep checking */
1347 if (checklinelen && (strlen(buffer) > CPERLIN + 1)) {
1349 checklinelen = 0; /* no need to keep checking */
1352 if (checklinespace &&
1353 (cp = buffer + strlen(buffer) - 2) >
1354 buffer && isspace(*cp)) {
1356 /* no need to keep checking */
1361 ** Check if content contains a line that clashes
1362 ** with our standard boundary for multipart messages.
1364 if (checkboundary && buffer[0] == '-' &&
1366 for (cp = buffer + strlen(buffer) - 1;
1371 if (strncmp(buffer + 2, prefix, len)==0 &&
1372 isdigit(buffer[2 + len])) {
1374 /* no need to keep checking */
1383 ** Decide which transfer encoding to use.
1385 switch (ct->c_type) {
1388 ** If the text content didn't specify a character
1389 ** set, we need to figure out which one was used.
1391 t = (struct text *) ct->c_ctparams;
1392 if (t->tx_charset == CHARSET_UNSPECIFIED) {
1393 CI ci = &ct->c_ctinfo;
1396 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
1400 t->tx_charset = CHARSET_UNKNOWN;
1401 *ap = concat("charset=", write_charset_8bit(),
1404 t->tx_charset = CHARSET_USASCII;
1405 *ap = getcpy("charset=us-ascii");
1408 cp = strchr(*ap++, '=');
1414 if (contains8bit || linelen || linespace)
1415 ct->c_encoding = CE_QUOTED;
1417 ct->c_encoding = CE_7BIT;
1421 ct->c_encoding = CE_7BIT;
1424 case CT_APPLICATION:
1429 ** Forcing use of base64, because these types likely
1430 ** contain binary data and NUL bytes. Don't care about
1431 ** files that would be clean.
1433 ct->c_encoding = CE_BASE64;
1437 return (boundaryclash ? NOTOK : OK);
1442 ** Scan the content structures, and build header
1443 ** fields that will need to be output into the
1448 build_headers(CT ct)
1452 char *np, *vp, buffer[BUFSIZ];
1453 CI ci = &ct->c_ctinfo;
1456 ** If message is type multipart, then add the multipart
1457 ** boundary to the list of attribute/value pairs.
1459 if (ct->c_type == CT_MULTIPART) {
1461 static int level = 0; /* store nesting level */
1465 snprintf(buffer, sizeof(buffer), "boundary=%s%d",
1467 cp = strchr(*ap++ = getcpy(buffer), '=');
1474 ** output the content type and subtype
1476 np = getcpy(TYPE_FIELD);
1477 vp = concat(" ", ci->ci_type, "/", ci->ci_subtype, NULL);
1479 /* keep track of length of line */
1480 len = strlen(TYPE_FIELD) + strlen(ci->ci_type) +
1481 strlen(ci->ci_subtype) + 3;
1484 ** Append the attribute/value pairs to
1485 ** the end of the Content-Type line.
1487 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1491 snprintf(buffer, sizeof(buffer), "%s=\"%s\"", *ap, *ep);
1492 if (len + 1 + (cc = strlen(buffer)) >= CPERLIN) {
1493 vp = add("\n\t", vp);
1499 vp = add(buffer, vp);
1504 ** Append any RFC-822 comment to the end of
1505 ** the Content-Type line.
1507 if (ci->ci_comment) {
1508 snprintf(buffer, sizeof(buffer), "(%s)", ci->ci_comment);
1509 if (len + 1 + (cc = 2 + strlen(ci->ci_comment)) >= CPERLIN) {
1510 vp = add("\n\t", vp);
1516 vp = add(buffer, vp);
1520 add_header(ct, np, vp);
1523 ** output the Content-ID
1526 np = getcpy(ID_FIELD);
1527 vp = concat(" ", ct->c_id, NULL);
1528 add_header(ct, np, vp);
1532 ** output the Content-Description
1535 np = getcpy(DESCR_FIELD);
1536 vp = concat(" ", ct->c_descr, NULL);
1537 if (encode_rfc2047(DESCR_FIELD, &vp, NULL)) {
1538 adios(EX_DATAERR, NULL, "Unable to encode %s header", DESCR_FIELD);
1540 add_header(ct, np, vp);
1544 ** output the Content-Disposition
1547 np = getcpy(DISPO_FIELD);
1548 vp = concat(" ", ct->c_dispo, NULL);
1549 add_header(ct, np, vp);
1553 ** output the Content-Transfer-Encoding
1555 switch (ct->c_encoding) {
1557 /* Nothing to output */
1561 if (ct->c_type == CT_MESSAGE)
1562 adios(EX_DATAERR, NULL, "internal error, invalid encoding");
1564 np = getcpy(ENCODING_FIELD);
1565 vp = concat(" ", "8bit", "\n", NULL);
1566 add_header(ct, np, vp);
1570 if (ct->c_type == CT_MESSAGE || ct->c_type == CT_MULTIPART)
1571 adios(EX_DATAERR, NULL, "internal error, invalid encoding");
1573 np = getcpy(ENCODING_FIELD);
1574 vp = concat(" ", "quoted-printable", "\n", NULL);
1575 add_header(ct, np, vp);
1579 if (ct->c_type == CT_MESSAGE || ct->c_type == CT_MULTIPART)
1580 adios(EX_DATAERR, NULL, "internal error, invalid encoding");
1582 np = getcpy(ENCODING_FIELD);
1583 vp = concat(" ", "base64", "\n", NULL);
1584 add_header(ct, np, vp);
1588 if (ct->c_type == CT_MESSAGE)
1589 adios(EX_DATAERR, NULL, "internal error, invalid encoding");
1591 np = getcpy(ENCODING_FIELD);
1592 vp = concat(" ", "binary", "\n", NULL);
1593 add_header(ct, np, vp);
1597 adios(EX_DATAERR, NULL, "unknown transfer encoding in content");
1602 ** Additional content specific header processing
1604 switch (ct->c_type) {
1607 struct multipart *m;
1610 m = (struct multipart *) ct->c_ctparams;
1611 for (part = m->mp_parts; part; part = part->mp_next) {