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);
73 * Mark any components tagged as address components
76 for (ap = addrcomps; *ap; 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
99 if ((cptr = wantcomp[CHASH(name)]))
101 if (mh_strcasecmp(name, cptr->c_name) == 0) {
102 char_read += msg_count;
103 if (! cptr->c_text) {
104 cptr->c_text = strdup(msgbuf);
106 i = strlen(cptr->c_text) - 1;
107 if (cptr->c_text[i] == '\n') {
108 if (cptr->c_type & CT_ADDR) {
109 cptr->c_text[i] = '\0';
110 cptr->c_text = add(",\n\t",
113 cptr->c_text = add ("\t", cptr->c_text);
116 cptr->c_text = add(msgbuf, cptr->c_text);
118 while (state == FLDPLUS) {
119 state = m_getfld(state, name, msgbuf,
120 sizeof(msgbuf), tmp);
121 cptr->c_text = add(msgbuf, cptr->c_text);
122 char_read += msg_count;
126 } while ((cptr = cptr->c_next));
128 while (state == FLDPLUS)
129 state = m_getfld(state, name, msgbuf, sizeof(msgbuf), tmp);
139 adios(NULL, "m_getfld() returned %d", state);
144 * Override any components just in case they were included in the
145 * input message. Also include command-line components given here
150 FINDCOMP (cptr, "digest");
153 cptr->c_text = digest;
155 FINDCOMP (cptr, "nmh-date");
158 cptr->c_text = getcpy(dtimenow (0));
160 FINDCOMP (cptr, "nmh-from");
165 FINDCOMP (cptr, "nmh-to");
170 FINDCOMP (cptr, "nmh-cc");
175 FINDCOMP (cptr, "nmh-subject");
178 cptr->c_text = subject;
180 FINDCOMP (cptr, "fcc");
186 cp = m_mktemp2(NULL, invo_name, NULL, &tmp);
187 if (cp == NULL) adios("forw", "unable to create temporary file");
188 strncpy (tmpfil, cp, sizeof(tmpfil));
190 if ((in = dup (fileno (tmp))) == NOTOK)
191 adios ("dup", "unable to");
193 line = mh_xmalloc ((unsigned) fmtsize);
194 fmt_scan (fmt, line, fmtsize, dat);
198 adios (tmpfil, "error writing");
200 lseek (in, (off_t) 0, SEEK_SET);
203 * Free any component buffers that we allocated
206 for (i = 0; i < (sizeof(wantcomp) / sizeof(struct comp)); i++)
207 for (cptr = wantcomp[i]; cptr != NULL; cptr = cptr->c_next)