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.
18 #include <h/signals.h>
23 #include <h/mhparse.h>
26 #ifdef HAVE_SYS_TIME_H
27 # include <sys/time.h>
31 static struct swit switches[] = {
43 { "norfc934mode", 0 },
50 #define NCONTENTIDSW 9
63 ** Directory to place tmp files. This must
64 ** be set before these routines are called.
70 static char prefix[] = "----- =_aaaaaaaaaa";
78 int make_intermediates(char *);
79 void content_error(char *, CT, char *, ...);
82 void free_content(CT);
84 void free_encoding(CT, int);
89 static int init_decoded_content(CT);
90 static char *fgetstr(char *, int, FILE *);
91 static int user_content(FILE *, char *, char *, CT *);
92 static void set_id(CT, int);
93 static int compose_content(CT);
94 static int scan_content(CT);
95 static int build_headers(CT);
96 static CT build_mime(char *);
109 static char infile[BUFSIZ];
110 static int unlink_infile = 0;
112 static char outfile[BUFSIZ];
113 static int unlink_outfile = 0;
115 static void unlink_done(int) NORETURN;
118 int output_message(CT, char *);
119 int output_message_fp(CT, FILE *, char*);
122 int list_all_messages(CT *, int, int, int);
125 void set_endian(void);
128 void free_content(CT);
132 main(int argc, char **argv)
135 char *cp, buf[BUFSIZ];
136 char buffer[BUFSIZ], *compfile = NULL;
137 char **argp, **arguments;
145 setlocale(LC_ALL, "");
147 invo_name = mhbasename(argv[0]);
149 /* read user profile/context */
152 arguments = getarguments(invo_name, argc, argv, 1);
155 while ((cp = *argp++)) {
156 if (cp[0] == '-' && cp[1] == '\0') {
158 adios(NULL, "cannot specify both standard input and a file");
161 listsw = 0; /* turn off -list if using std in/out */
162 verbosw = 0; /* turn off -verbose listings */
166 switch (smatch(++cp, switches)) {
168 ambigsw(cp, switches);
171 adios(NULL, "-%s unknown", cp);
174 snprintf(buf, sizeof(buf), "%s [switches] file", invo_name);
175 print_help(buf, switches, 1);
178 print_version(invo_name);
221 adios(NULL, "only one composition file allowed");
228 if ((cp = getenv("MM_NOASK")) && strcmp(cp, "1")==0)
232 ** Check if we've specified an additional profile
234 if ((cp = getenv("MHBUILD"))) {
235 if ((fp = fopen(cp, "r"))) {
236 readconfig((struct node **) 0, fp, cp, 0);
239 admonish("", "unable to read $MHBUILD profile (%s)",
245 ** Read the standard profile setup
247 if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
248 readconfig((struct node **) 0, fp, cp, 0);
253 ** Check for storage directory. If defined, we
254 ** will store temporary files there. Else we
255 ** store them in standard nmh directory.
257 if ((cp = context_find(nmhstorage)) && *cp)
258 tmp = concat(cp, "/", invo_name, NULL);
260 tmp = getcpy(toabsdir(invo_name));
262 /* Check if we have a file to process */
264 adios(NULL, "need to specify a %s composition file",
268 ** Process the composition file from standard input.
270 if (compfile[0] == '-' && compfile[1] == '\0') {
271 /* copy standard input to temporary file */
272 strncpy(infile, m_mktemp(invo_name, NULL, &fp),
274 while (fgets(buffer, BUFSIZ, stdin))
279 /* build the content structures for MIME message */
280 ct = build_mime(infile);
284 /* output MIME message to this temporary file */
285 strncpy(outfile, m_mktemp(invo_name, NULL, &fp_out),
289 /* output the message */
290 output_message_fp(ct, fp_out, outfile);
293 /* output the temp file to standard output */
294 if ((fp = fopen(outfile, "r")) == NULL)
295 adios(outfile, "unable to open");
296 while (fgets(buffer, BUFSIZ, fp))
297 fputs(buffer, stdout);
311 ** Process the composition file from a file.
314 /* build the content structures for MIME message */
315 ct = build_mime(compfile);
319 /* output MIME message to this temporary file */
320 strncpy(outfile, m_mktemp2(compfile, invo_name, NULL, &fp_out),
324 /* output the message */
325 output_message_fp(ct, fp_out, outfile);
329 ** List the message info
332 list_all_messages(cts, headsw, verbosw, debugsw);
334 /* Rename composition draft */
335 snprintf(buffer, sizeof(buffer), "%s.orig", m_backup(compfile));
336 if (rename(compfile, buffer) == NOTOK) {
337 adios(compfile, "unable to rename comp draft %s to", buffer);
340 /* Rename output file to take its place */
341 if (rename(outfile, compfile) == NOTOK) {
342 advise(outfile, "unable to rename output %s to", compfile);
343 rename(buffer, compfile);
355 unlink_done(int status)
358 ** Check if we need to remove stray temporary files.
369 ** Main routine for translating composition file
370 ** into valid MIME message. It translates the draft
371 ** into a content structure (actually a tree of content
372 ** structures). This message then can be manipulated
373 ** in various ways, including being output via
377 build_mime(char *infile)
380 char buf[BUFSIZ], name[NAMESZ];
389 /* open the composition draft */
390 if ((in = fopen(infile, "r")) == NULL)
391 adios(infile, "unable to open for reading");
394 ** Allocate space for primary (outside) content
396 if ((ct = (CT) calloc(1, sizeof(*ct))) == NULL)
397 adios(NULL, "out of memory");
400 ** Allocate structure for handling decoded content
401 ** for this part. We don't really need this, but
402 ** allocate it to remain consistent.
404 init_decoded_content(ct);
407 ** Parse some of the header fields in the composition
408 ** draft into the linked list of header fields for
409 ** the new MIME message.
411 for (compnum = 1, state = FLD;;) {
412 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
418 /* abort if draft has Mime-Version header field */
419 if (!mh_strcasecmp(name, VRSN_FIELD))
420 adios(NULL, "draft shouldn't contain %s: field", VRSN_FIELD);
423 ** abort if draft has Content-Transfer-Encoding
426 if (!mh_strcasecmp(name, ENCODING_FIELD))
427 adios(NULL, "draft shouldn't contain %s: field", ENCODING_FIELD);
429 /* ignore any Content-Type fields in the header */
430 if (!mh_strcasecmp(name, TYPE_FIELD)) {
431 while (state == FLDPLUS)
432 state = m_getfld(state, name, buf,
437 /* get copies of the buffers */
441 /* if necessary, get rest of field */
442 while (state == FLDPLUS) {
443 state = m_getfld(state, name, buf,
445 vp = add(buf, vp); /* add to prev value */
448 /* Now add the header data to the list */
449 add_header(ct, np, vp);
452 /* if this wasn't the last hdr field, then continue */
458 adios(NULL, "draft has empty body -- no directives!");
463 fseek(in, (long) (-strlen(buf)), SEEK_CUR);
468 adios(NULL, "message format error in component #%d",
472 adios(NULL, "getfld() returned %d", state);
478 ** Now add the MIME-Version header field
479 ** to the list of header fields.
481 np = getcpy(VRSN_FIELD);
482 vp = concat(" ", VRSN_VALUE, "\n", NULL);
483 add_header(ct, np, vp);
486 ** We initally assume we will find multiple contents in the
487 ** draft. So create a multipart/mixed content to hold everything.
488 ** We can remove this later, if it is not needed.
490 if (get_ctinfo("multipart/mixed", ct, 0) == NOTOK)
492 ct->c_type = CT_MULTIPART;
493 ct->c_subtype = MULTI_MIXED;
494 ct->c_file = getcpy(infile);
496 if ((m = (struct multipart *) calloc(1, sizeof(*m))) == NULL)
497 adios(NULL, "out of memory");
498 ct->c_ctparams = (void *) m;
502 ** read and parse the composition file
503 ** and the directives it contains.
505 while (fgetstr(buf, sizeof(buf) - 1, in)) {
509 if (user_content(in, infile, buf, &p) == DONE) {
510 admonish(NULL, "ignoring spurious #end");
516 if ((part = (struct part *) calloc(1, sizeof(*part))) == NULL)
517 adios(NULL, "out of memory");
524 ** close the composition draft since
525 ** it's not needed any longer.
529 /* check if any contents were found */
531 adios(NULL, "no content directives found");
534 ** If only one content was found, then remove and
535 ** free the outer multipart content.
537 if (!m->mp_parts->mp_next) {
540 p = m->mp_parts->mp_part;
541 m->mp_parts->mp_part = NULL;
543 /* move header fields */
544 p->c_first_hf = ct->c_first_hf;
545 p->c_last_hf = ct->c_last_hf;
546 ct->c_first_hf = NULL;
547 ct->c_last_hf = NULL;
556 ** Fill out, or expand directives. Parse and execute
557 ** commands specified by profile composition strings.
561 if ((cp = strchr(prefix, 'a')) == NULL)
562 adios(NULL, "internal error(4)");
565 ** Scan the contents. Choose a transfer encoding, and
566 ** check if prefix for multipart boundary clashes with
567 ** any of the contents.
569 while (scan_content(ct) == NOTOK) {
574 adios(NULL, "giving up trying to find a unique delimiter string");
580 /* Build the rest of the header field structures */
588 ** Set up structures for placing unencoded
589 ** content when building parts.
593 init_decoded_content(CT ct)
597 if ((ce = (CE) calloc(1, sizeof(*ce))) == NULL)
598 adios(NULL, "out of memory");
601 ct->c_ceopenfnx = open7Bit; /* since unencoded */
602 ct->c_ceclosefnx = close_encoding;
603 ct->c_cesizefnx = NULL; /* since unencoded */
610 fgetstr(char *s, int n, FILE *stream)
614 for (ep = (cp = s) + n; cp < ep; ) {
617 if (!fgets(cp, n, stream))
618 return (cp != s ? s : NULL);
619 if (cp == s && *cp != '#')
622 cp += (i = strlen(cp)) - 1;
623 if (i <= 1 || *cp-- != '\n' || *cp != '\\')
634 ** Parse the composition draft for text and directives.
635 ** Do initial setup of Content structure.
639 user_content(FILE *in, char *file, char *buf, CT *ctp)
648 struct str2init *s2i;
653 if (buf[0] == '\n' || strcmp(buf, "#\n") == 0) {
658 /* allocate basic Content structure */
659 if ((ct = (CT) calloc(1, sizeof(*ct))) == NULL)
660 adios(NULL, "out of memory");
663 /* allocate basic structure for handling decoded content */
664 init_decoded_content(ct);
671 ** Handle inline text. Check if line
672 ** is one of the following forms:
674 ** 1) doesn't begin with '#' (implicit directive)
675 ** 2) begins with "##" (implicit directive)
676 ** 3) begins with "#<"
678 if (buf[0] != '#' || buf[1] == '#' || buf[1] == '<') {
682 char content[BUFSIZ];
686 cp = m_mktemp2(NULL, invo_name, NULL, &out);
688 adios("mhbuild", "unable to create temporary file");
690 /* use a temp file to collect the plain text lines */
691 ce->ce_file = getcpy(cp);
694 if (buf[0] == '#' && buf[1] == '<') {
695 strncpy(content, buf + 2, sizeof(content));
702 /* the directive is implicit */
703 strncpy(content, "text/plain", sizeof(content));
705 strncpy(buffer, buf[0] != '#' ? buf : buf + 1, sizeof(buffer));
709 if (headers >= 0 && uprf(buffer, DESCR_FIELD) &&
710 buffer[i=strlen(DESCR_FIELD)] == ':') {
714 ct->c_descr = add(buffer + i + 1, ct->c_descr);
715 if (!fgetstr(buffer, sizeof(buffer) - 1, in))
716 adios(NULL, "end-of-file after %s: field in plaintext", DESCR_FIELD);
724 adios(NULL, "#-directive after %s: field in plaintext", DESCR_FIELD);
732 if (headers >= 0 && uprf(buffer, DISPO_FIELD)
733 && buffer[i = strlen(DISPO_FIELD)] == ':') {
737 ct->c_dispo = add(buffer + i + 1, ct->c_dispo);
738 if (!fgetstr(buffer, sizeof(buffer) - 1, in))
739 adios(NULL, "end-of-file after %s: field in plaintext", DISPO_FIELD);
747 adios(NULL, "#-directive after %s: field in plaintext", DISPO_FIELD);
755 if (headers != 1 || buffer[0] != '\n')
761 if ((cp = fgetstr(buffer, sizeof(buffer) - 1, in))
764 if (buffer[0] == '#') {
767 if (buffer[1] != '#')
769 for (cp = (bp = buffer) + 1; *cp; cp++)
776 ct->c_end = ftell(out);
779 /* parse content type */
780 if (get_ctinfo(content, ct, inlineD) == NOTOK)
783 for (s2i = str2cts; s2i->si_key; s2i++)
784 if (!mh_strcasecmp(ci->ci_type, s2i->si_key))
786 if (!s2i->si_key && !uprf(ci->ci_type, "X-"))
790 ** check type specified (possibly implicitly)
792 switch (ct->c_type = s2i->si_val) {
794 if (!mh_strcasecmp(ci->ci_subtype, "rfc822")) {
795 ct->c_encoding = CE_7BIT;
800 adios(NULL, "it doesn't make sense to define an in-line %s content",
801 ct->c_type == CT_MESSAGE ? "message" :
807 if ((ct->c_ctinitfnx = s2i->si_init))
808 (*ct->c_ctinitfnx) (ct);
813 fseek(in, pos, SEEK_SET);
818 ** If we've reached this point, the next line
819 ** must be some type of explicit directive.
823 adios(NULL, "The #@ directive i.e. message/external-body "
824 "is not supported anymore.");
827 /* parse directive */
828 if (get_ctinfo(buf+1, ct, 1) == NOTOK)
831 /* check directive against the list of MIME types */
832 for (s2i = str2cts; s2i->si_key; s2i++)
833 if (!mh_strcasecmp(ci->ci_type, s2i->si_key))
837 ** Check if the directive specified a valid type.
838 ** This will happen if it was one of the following forms:
844 adios(NULL, "missing subtype in \"#%s\"", ci->ci_type);
846 switch (ct->c_type = s2i->si_val) {
848 adios(NULL, "use \"#begin ... #end\" instead of \"#%s/%s\"", ci->ci_type, ci->ci_subtype);
852 if (!mh_strcasecmp(ci->ci_subtype, "partial") ||
853 !mh_strcasecmp(ci->ci_subtype,
855 adios(NULL, "sorry, \"#%s/%s\" isn't supported", ci->ci_type, ci->ci_subtype);
858 adios(NULL, "use \"#forw [+folder] [msgs]\" instead of \"#%s/%s\"", ci->ci_type, ci->ci_subtype);
862 if ((ct->c_ctinitfnx = s2i->si_init))
863 (*ct->c_ctinitfnx) (ct);
867 /* Handle [file] argument */
869 /* check if specifies command to execute */
870 if (*ci->ci_magic == '|' || *ci->ci_magic == '!') {
871 for (cp = ci->ci_magic + 1; isspace(*cp); cp++)
874 adios(NULL, "empty pipe command for #%s directive", ci->ci_type);
879 /* record filename of decoded contents */
880 ce->ce_file = ci->ci_magic;
881 if (access(ce->ce_file, R_OK) == NOTOK)
882 adios("reading", "unable to access %s for", ce->ce_file);
883 if (listsw && stat(ce->ce_file, &st) != NOTOK)
884 ct->c_end = (long) st.st_size;
891 ** No [file] argument, so check profile for
892 ** method to compose content.
894 snprintf(buffer, sizeof(buffer), "%s-compose-%s/%s",
895 invo_name, ci->ci_type, ci->ci_subtype);
896 if ((cp = context_find(buffer)) == NULL || *cp == '\0') {
897 snprintf(buffer, sizeof(buffer), "%s-compose-%s",
898 invo_name, ci->ci_type);
899 if ((cp = context_find(buffer)) == NULL ||
901 content_error(NULL, ct, "don't know how to compose content");
905 ci->ci_magic = getcpy(cp);
911 ** #forw [+folder] [msgs]
913 if (!mh_strcasecmp(ci->ci_type, "forw")) {
915 char *folder, *arguments[MAXARGS];
921 ap = brkstring(ci->ci_magic, " ", "\n");
922 for (i=0; ap[i] && i<MAXARGS-1; i++) {
923 arguments[i] = ap[i];
928 arguments[0] = seq_cur;
933 /* search the arguments for a folder name */
934 for (ap = arguments; *ap; ap++) {
936 if (*cp == '+' || *cp == '@') {
938 adios(NULL, "only one folder per #forw directive");
940 folder = getcpy(expandfol(cp));
944 /* else, use the current folder */
946 folder = getcpy(getcurfol());
948 if (!(mp = folder_read(folder)))
949 adios(NULL, "unable to read folder %s", folder);
950 for (ap = arguments; *ap; ap++) {
952 if (*cp != '+' && *cp != '@')
953 if (!m_convert(mp, cp))
960 ** If there is more than one message to include, make this
961 ** a content of type "multipart/digest" and insert each message
962 ** as a subpart. If there is only one message, then make this
963 ** a content of type "message/rfc822".
965 if (mp->numsel > 1) {
966 /* we are forwarding multiple messages */
967 if (get_ctinfo("multipart/digest", ct, 0) == NOTOK)
969 ct->c_type = CT_MULTIPART;
970 ct->c_subtype = MULTI_DIGEST;
972 if ((m = (struct multipart *)
973 calloc(1, sizeof(*m))) == NULL)
974 adios(NULL, "out of memory");
975 ct->c_ctparams = (void *) m;
978 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
979 if (is_selected(mp, msgnum)) {
984 if ((p = (CT) calloc(1, sizeof(*p)))
986 adios(NULL, "out of memory");
987 init_decoded_content(p);
989 if (get_ctinfo("message/rfc822", p, 0)
992 p->c_type = CT_MESSAGE;
993 p->c_subtype = MESSAGE_RFC822;
995 snprintf(buffer, sizeof(buffer),
996 "%s/%d", mp->foldpath,
998 pe->ce_file = getcpy(buffer);
999 if (listsw && stat(pe->ce_file, &st)
1001 p->c_end = (long) st.st_size;
1003 if ((part = (struct part *) calloc(1, sizeof(*part))) == NULL)
1004 adios(NULL, "out of memory");
1006 pp = &part->mp_next;
1011 /* we are forwarding one message */
1012 if (get_ctinfo("message/rfc822", ct, 0) == NOTOK)
1014 ct->c_type = CT_MESSAGE;
1015 ct->c_subtype = MESSAGE_RFC822;
1017 msgnum = mp->lowsel;
1018 snprintf(buffer, sizeof(buffer), "%s/%d",
1019 mp->foldpath, msgnum);
1020 ce->ce_file = getcpy(buffer);
1021 if (listsw && stat(ce->ce_file, &st) != NOTOK)
1022 ct->c_end = (long) st.st_size;
1025 folder_free(mp); /* free folder/message structure */
1032 if (!mh_strcasecmp(ci->ci_type, "end")) {
1039 ** #begin [ alternative | parallel ]
1041 if (!mh_strcasecmp(ci->ci_type, "begin")) {
1042 if (!ci->ci_magic) {
1044 cp = SubMultiPart[vrsn - 1].kv_key;
1045 } else if (!mh_strcasecmp(ci->ci_magic, "alternative")) {
1046 vrsn = MULTI_ALTERNATE;
1047 cp = SubMultiPart[vrsn - 1].kv_key;
1048 } else if (!mh_strcasecmp(ci->ci_magic, "parallel")) {
1049 vrsn = MULTI_PARALLEL;
1050 cp = SubMultiPart[vrsn - 1].kv_key;
1051 } else if (uprf(ci->ci_magic, "digest")) {
1054 vrsn = MULTI_UNKNOWN;
1059 snprintf(buffer, sizeof(buffer), "multipart/%s", cp);
1060 if (get_ctinfo(buffer, ct, 0) == NOTOK)
1062 ct->c_type = CT_MULTIPART;
1063 ct->c_subtype = vrsn;
1065 if ((m = (struct multipart *) calloc(1, sizeof(*m))) == NULL)
1066 adios(NULL, "out of memory");
1067 ct->c_ctparams = (void *) m;
1070 while (fgetstr(buffer, sizeof(buffer) - 1, in)) {
1074 if (user_content(in, file, buffer, &p) == DONE) {
1076 adios(NULL, "empty \"#begin ... #end\" sequence");
1082 if ((part = (struct part *)
1083 calloc(1, sizeof(*part))) == NULL)
1084 adios(NULL, "out of memory");
1086 pp = &part->mp_next;
1089 admonish(NULL, "premature end-of-file, missing #end");
1094 ** Unknown directive
1096 adios(NULL, "unknown directive \"#%s\"", ci->ci_type);
1097 return NOTOK; /* NOT REACHED */
1102 set_id(CT ct, int top)
1106 static time_t clock = 0;
1107 static char *msgfmt;
1111 snprintf(msgid, sizeof(msgid), "<%d.%ld.%%d@%s>\n",
1112 (int) getpid(), (long) clock, LocalName());
1114 msgfmt = getcpy(msgid);
1116 snprintf(msgid, sizeof(msgid), msgfmt, top ? 0 : ++partno);
1117 ct->c_id = getcpy(msgid);
1122 ** Fill out, or expand the various contents in the composition
1123 ** draft. Read-in any necessary files. Parse and execute any
1124 ** commands specified by profile composition strings.
1128 compose_content(CT ct)
1130 CE ce = ct->c_cefile;
1132 switch (ct->c_type) {
1137 char partnam[BUFSIZ];
1138 struct multipart *m = (struct multipart *) ct->c_ctparams;
1142 snprintf(partnam, sizeof(partnam), "%s.",
1144 pp = partnam + strlen(partnam);
1149 /* first, we call compose_content on all the subparts */
1150 for (part = m->mp_parts, partnum = 1; part;
1151 part = part->mp_next, partnum++) {
1152 CT p = part->mp_part;
1154 sprintf(pp, "%d", partnum);
1155 p->c_partno = getcpy(partnam);
1156 if (compose_content(p) == NOTOK)
1161 ** If the -rfc934mode switch is given, then check all
1162 ** the subparts of a multipart/digest. If they are all
1163 ** message/rfc822, then mark this content and all
1164 ** subparts with the rfc934 compatibility mode flag.
1166 if (rfc934sw && ct->c_subtype == MULTI_DIGEST) {
1169 for (part = m->mp_parts; part; part = part->mp_next) {
1170 CT p = part->mp_part;
1172 if (p->c_subtype != MESSAGE_RFC822) {
1177 ct->c_rfc934 = is934;
1178 for (part = m->mp_parts; part; part = part->mp_next) {
1179 CT p = part->mp_part;
1181 if ((p->c_rfc934 = is934))
1187 ct->c_end = (partnum = strlen(prefix) + 2) + 2;
1191 for (part = m->mp_parts; part; part = part->mp_next)
1192 ct->c_end += part->mp_part->c_end + partnum;
1198 /* Nothing to do for type message */
1202 ** Discrete types (text/application/audio/image/video)
1207 int xstdout, len, buflen;
1208 char *bp, **ap, *cp;
1209 char *vec[4], buffer[BUFSIZ];
1211 CI ci = &ct->c_ctinfo;
1214 if (!(cp = ci->ci_magic))
1215 adios(NULL, "internal error(5)");
1217 tfile = m_mktemp2(NULL, invo_name, NULL, NULL);
1218 if (tfile == NULL) {
1219 adios("mhbuild", "unable to create temporary file");
1221 ce->ce_file = getcpy(tfile);
1226 /* Get buffer ready to go */
1229 buflen = sizeof(buffer);
1232 ** Parse composition string into buffer
1234 for ( ; *cp; cp++) {
1240 ** insert parameters from
1246 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1247 snprintf(bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
1257 /* %f, and stdout is not-redirected */
1263 ** insert temporary filename
1264 ** where content should be
1267 snprintf(bp, buflen, "%s", ce->ce_file);
1271 /* insert content subtype */
1272 strncpy(bp, ci->ci_subtype, buflen);
1276 /* insert character % */
1297 printf("composing content %s/%s from command\n\t%s\n", ci->ci_type, ci->ci_subtype, buffer);
1299 fflush(stdout); /* not sure if need for -noverbose */
1306 if ((out = fopen(ce->ce_file, "w")) == NULL)
1307 adios(ce->ce_file, "unable to open for writing");
1309 switch (child_id = fork()) {
1311 adios("fork", "unable to fork");
1316 dup2(fileno(out), 1);
1318 execvp("/bin/sh", vec);
1319 fprintf(stderr, "unable to exec ");
1326 if (pidXwait(child_id, NULL))
1332 /* Check size of file */
1333 if (listsw && ct->c_end == 0L) {
1336 if (stat(ce->ce_file, &st) != NOTOK)
1337 ct->c_end = (long) st.st_size;
1347 ** Scan the content.
1349 ** 1) choose a transfer encoding.
1350 ** 2) check for clashes with multipart boundary string.
1351 ** 3) for text content, figure out which character set is being used.
1353 ** If there is a clash with one of the contents and the multipart boundary,
1354 ** this function will exit with NOTOK. This will cause the scanning process
1355 ** to be repeated with a different multipart boundary. It is possible
1356 ** (although highly unlikely) that this scan will be repeated multiple times.
1363 int check8bit = 0, contains8bit = 0; /* check if contains 8bit data */
1364 int checklinelen = 0, linelen = 0; /* check for long lines */
1365 int checkboundary = 0, boundaryclash = 0; /* check if clashes with multipart boundary */
1366 int checklinespace = 0, linespace = 0; /* check if any line ends with space */
1367 unsigned char *cp = NULL, buffer[BUFSIZ];
1368 struct text *t = NULL;
1370 CE ce = ct->c_cefile;
1373 ** handle multipart by scanning all subparts
1374 ** and then checking their encoding.
1376 if (ct->c_type == CT_MULTIPART) {
1377 struct multipart *m = (struct multipart *) ct->c_ctparams;
1380 /* initially mark the domain of enclosing multipart as 7bit */
1381 ct->c_encoding = CE_7BIT;
1383 for (part = m->mp_parts; part; part = part->mp_next) {
1384 CT p = part->mp_part;
1386 if (scan_content(p) == NOTOK) {
1387 /* choose encoding for subpart */
1392 ** if necessary, enlarge encoding for enclosing
1395 if (p->c_encoding == CE_BINARY)
1396 ct->c_encoding = CE_BINARY;
1397 if (p->c_encoding == CE_8BIT &&
1398 ct->c_encoding != CE_BINARY)
1399 ct->c_encoding = CE_8BIT;
1406 ** Decide what to check while scanning this content.
1408 switch (ct->c_type) {
1412 if (ct->c_subtype == TEXT_PLAIN) {
1421 case CT_APPLICATION:
1439 ** Don't check anything for these types,
1440 ** since we are forcing use of base64.
1450 ** Scan the unencoded content
1452 if (check8bit || checklinelen || checklinespace || checkboundary) {
1453 if ((in = fopen(ce->ce_file, "r")) == NULL)
1454 adios(ce->ce_file, "unable to open for reading");
1455 len = strlen(prefix);
1457 while (fgets(buffer, sizeof(buffer) - 1, in)) {
1459 ** Check for 8bit data.
1462 for (cp = buffer; *cp; cp++) {
1463 if (!isascii(*cp)) {
1465 /* no need to keep checking */
1472 ** Check line length.
1474 if (checklinelen && (strlen(buffer) > CPERLIN + 1)) {
1476 checklinelen = 0; /* no need to keep checking */
1480 ** Check if line ends with a space.
1482 if (checklinespace &&
1483 (cp = buffer + strlen(buffer) - 2) >
1484 buffer && isspace(*cp)) {
1486 /* no need to keep checking */
1491 ** Check if content contains a line that clashes
1492 ** with our standard boundary for multipart messages.
1494 if (checkboundary && buffer[0] == '-' &&
1496 for (cp = buffer + strlen(buffer) - 1;
1501 if (strncmp(buffer + 2, prefix, len)==0 &&
1502 isdigit(buffer[2 + len])) {
1504 /* no need to keep checking */
1513 ** Decide which transfer encoding to use.
1515 switch (ct->c_type) {
1518 ** If the text content didn't specify a character
1519 ** set, we need to figure out which one was used.
1521 t = (struct text *) ct->c_ctparams;
1522 if (t->tx_charset == CHARSET_UNSPECIFIED) {
1523 CI ci = &ct->c_ctinfo;
1526 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
1530 t->tx_charset = CHARSET_UNKNOWN;
1531 *ap = concat("charset=", write_charset_8bit(),
1534 t->tx_charset = CHARSET_USASCII;
1535 *ap = getcpy("charset=us-ascii");
1538 cp = strchr(*ap++, '=');
1544 if (contains8bit || linelen || linespace)
1545 ct->c_encoding = CE_QUOTED;
1547 ct->c_encoding = CE_7BIT;
1550 case CT_APPLICATION:
1551 /* For application type, use base64, except when postscript */
1552 if (contains8bit || linelen || linespace)
1553 ct->c_encoding = (ct->c_subtype ==
1554 APPLICATION_POSTSCRIPT) ?
1555 CE_QUOTED : CE_BASE64;
1557 ct->c_encoding = CE_7BIT;
1561 ct->c_encoding = CE_7BIT;
1567 /* For audio, image, and video contents, just use base64 */
1568 ct->c_encoding = CE_BASE64;
1572 return (boundaryclash ? NOTOK : OK);
1577 ** Scan the content structures, and build header
1578 ** fields that will need to be output into the
1583 build_headers(CT ct)
1587 char *np, *vp, buffer[BUFSIZ];
1588 CI ci = &ct->c_ctinfo;
1591 ** If message is type multipart, then add the multipart
1592 ** boundary to the list of attribute/value pairs.
1594 if (ct->c_type == CT_MULTIPART) {
1596 static int level = 0; /* store nesting level */
1600 snprintf(buffer, sizeof(buffer), "boundary=%s%d",
1602 cp = strchr(*ap++ = getcpy(buffer), '=');
1609 ** Skip the output of Content-Type, parameters, content
1610 ** description and disposition, and Content-ID if the
1611 ** content is of type "message" and the rfc934 compatibility
1612 ** flag is set (which means we are inside multipart/digest
1613 ** and the switch -rfc934mode was given).
1615 if (ct->c_type == CT_MESSAGE && ct->c_rfc934)
1619 ** output the content type and subtype
1621 np = getcpy(TYPE_FIELD);
1622 vp = concat(" ", ci->ci_type, "/", ci->ci_subtype, NULL);
1624 /* keep track of length of line */
1625 len = strlen(TYPE_FIELD) + strlen(ci->ci_type) +
1626 strlen(ci->ci_subtype) + 3;
1629 ** Append the attribute/value pairs to
1630 ** the end of the Content-Type line.
1632 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1636 snprintf(buffer, sizeof(buffer), "%s=\"%s\"", *ap, *ep);
1637 if (len + 1 + (cc = strlen(buffer)) >= CPERLIN) {
1638 vp = add("\n\t", vp);
1644 vp = add(buffer, vp);
1649 ** Append any RFC-822 comment to the end of
1650 ** the Content-Type line.
1652 if (ci->ci_comment) {
1653 snprintf(buffer, sizeof(buffer), "(%s)", ci->ci_comment);
1654 if (len + 1 + (cc = 2 + strlen(ci->ci_comment)) >= CPERLIN) {
1655 vp = add("\n\t", vp);
1661 vp = add(buffer, vp);
1665 add_header(ct, np, vp);
1668 ** output the Content-ID, unless disabled by -nocontentid
1670 if (contentidsw && ct->c_id) {
1671 np = getcpy(ID_FIELD);
1672 vp = concat(" ", ct->c_id, NULL);
1673 add_header(ct, np, vp);
1677 ** output the Content-Description
1680 np = getcpy(DESCR_FIELD);
1681 vp = concat(" ", ct->c_descr, NULL);
1682 add_header(ct, np, vp);
1686 ** output the Content-Disposition
1689 np = getcpy(DISPO_FIELD);
1690 vp = concat(" ", ct->c_dispo, NULL);
1691 add_header(ct, np, vp);
1696 ** output the Content-Transfer-Encoding
1698 switch (ct->c_encoding) {
1700 /* Nothing to output */
1704 if (ct->c_type == CT_MESSAGE)
1705 adios(NULL, "internal error, invalid encoding");
1707 np = getcpy(ENCODING_FIELD);
1708 vp = concat(" ", "8bit", "\n", NULL);
1709 add_header(ct, np, vp);
1713 if (ct->c_type == CT_MESSAGE || ct->c_type == CT_MULTIPART)
1714 adios(NULL, "internal error, invalid encoding");
1716 np = getcpy(ENCODING_FIELD);
1717 vp = concat(" ", "quoted-printable", "\n", NULL);
1718 add_header(ct, np, vp);
1722 if (ct->c_type == CT_MESSAGE || ct->c_type == CT_MULTIPART)
1723 adios(NULL, "internal error, invalid encoding");
1725 np = getcpy(ENCODING_FIELD);
1726 vp = concat(" ", "base64", "\n", NULL);
1727 add_header(ct, np, vp);
1731 if (ct->c_type == CT_MESSAGE)
1732 adios(NULL, "internal error, invalid encoding");
1734 np = getcpy(ENCODING_FIELD);
1735 vp = concat(" ", "binary", "\n", NULL);
1736 add_header(ct, np, vp);
1740 adios(NULL, "unknown transfer encoding in content");
1745 ** Additional content specific header processing
1747 switch (ct->c_type) {
1750 struct multipart *m;
1753 m = (struct multipart *) ct->c_ctparams;
1754 for (part = m->mp_parts; part; part = part->mp_next) {