3 * forwsbr.c -- subroutine to build a draft from a component file
5 * This code is Copyright (c) 2012, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/fmt_scan.h>
17 * Take from replsbr.c - a buffer big enough to read in data header lines
18 * in reasonable chunks but not enough to slurp in the whole message
21 static char msgbuf[256];
22 #define COMPFREE(c) if (c->c_text) free(c->c_text)
25 * A list of components we treat as addresses
28 static char *addrcomps[] = {
45 build_form (char *form, char *digest, int *dat, char *from, char *to,
46 char *cc, char *fcc, char *subject, char *inputfile)
49 int fmtsize, state, char_read = 0;
52 char *line, tmpfil[BUFSIZ], name[NAMESZ], **ap;
54 register struct comp *cptr;
59 * Open the message we'll be scanning for components
62 if ((tmp = fopen(inputfile, "r")) == NULL)
63 adios (inputfile, "Unable to open");
65 /* Get new format string */
66 nfs = new_fs (form, NULL, NULL);
67 fmtsize = strlen (nfs) + 256;
69 /* Compile format string */
70 (void) fmt_compile (nfs, &fmt, 1);
73 * Mark any components tagged as address components
76 for (ap = addrcomps; *ap; ap++) {
77 cptr = fmt_findcomp (*ap);
79 cptr->c_type |= CT_ADDR;
83 * Process our message and save all relevant components
85 * A lot of this is taken from replsbr.c; should we try to merge
90 state = m_getfld(state, name, msgbuf, sizeof(msgbuf), tmp);
95 * If we find a component that we're interested in, save
96 * a copy. We don't do all of that weird buffer switching
100 i = fmt_addcomptext(name, msgbuf);
102 char_read += msg_count;
103 while (state == FLDPLUS) {
104 state = m_getfld(state, name, msgbuf,
105 sizeof(msgbuf), tmp);
106 fmt_appendcomp(i, name, msgbuf);
107 char_read += msg_count;
110 while (state == FLDPLUS)
111 state = m_getfld(state, name, msgbuf, sizeof(msgbuf), tmp);
121 adios(NULL, "m_getfld() returned %d", state);
126 * Override any components just in case they were included in the
127 * input message. Also include command-line components given here
129 * With the memory rework I've changed things so we always get copies
130 * of these strings; I don't like the idea that the caller of this
131 * function has to know to pass in already-allocated memory (and that
132 * it will be free()'d by us).
137 cptr = fmt_findcomp ("digest");
140 cptr->c_text = getcpy(digest);
142 cptr = fmt_findcomp ("nmh-date");
145 cptr->c_text = getcpy(dtimenow (0));
147 cptr = fmt_findcomp ("nmh-from");
150 cptr->c_text = getcpy(from);
152 cptr = fmt_findcomp ("nmh-to");
155 cptr->c_text = getcpy(to);
157 cptr = fmt_findcomp ("nmh-cc");
160 cptr->c_text = getcpy(cc);
162 cptr = fmt_findcomp ("nmh-subject");
165 cptr->c_text = getcpy(subject);
167 cptr = fmt_findcomp ("fcc");
170 cptr->c_text = getcpy(fcc);
173 cp = m_mktemp2(NULL, invo_name, NULL, &tmp);
174 if (cp == NULL) adios("forw", "unable to create temporary file");
175 strncpy (tmpfil, cp, sizeof(tmpfil));
177 if ((in = dup (fileno (tmp))) == NOTOK)
178 adios ("dup", "unable to");
180 line = mh_xmalloc ((unsigned) fmtsize);
181 fmt_scan (fmt, line, fmtsize - 1, fmtsize, dat);
185 adios (tmpfil, "error writing");
187 lseek (in, (off_t) 0, SEEK_SET);
190 * Free any component buffers that we allocated