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 build_form (char *form, char *digest, int *dat, char *from, char *to,
26 char *cc, char *fcc, char *subject, char *inputfile)
29 int i, fmtsize, ncomps, state, char_read = 0;
31 char *line, tmpfil[BUFSIZ], name[NAMESZ];
33 register struct comp *cptr;
38 * Open the message we'll be scanning for components
41 if ((tmp = fopen(inputfile, "r")) == NULL)
42 adios (inputfile, "Unable to open");
44 /* Get new format string */
45 nfs = new_fs (form, NULL, NULL);
46 fmtsize = strlen (nfs) + 256;
48 /* Compile format string */
49 ncomps = fmt_compile (nfs, &fmt);
52 * Process our message and save all relevant components
54 * A lot of this is taken from replsbr.c; should we try to merge
59 state = m_getfld(state, name, msgbuf, sizeof(msgbuf), tmp);
64 * If we find a component that we're interested in, save
65 * a copy. We don't do all of that weird buffer switching
68 if ((cptr = wantcomp[CHASH(name)]))
70 if (mh_strcasecmp(name, cptr->c_name) == 0) {
71 char_read += msg_count;
73 i = strlen(cptr->c_text = strdup(msgbuf)) - 1;
74 if (cptr->c_text[i] == '\n')
75 cptr->c_text[i] = '\0';
77 i = strlen(cptr->c_text) - 1;
78 if (cptr->c_text[i] == '\n') {
79 if (cptr->c_type & CT_ADDR) {
80 cptr->c_text[i] = '\0';
81 cptr->c_text = add(",\n\t",
84 cptr->c_text = add ("\t", cptr->c_text);
87 cptr->c_text = add(msgbuf, cptr->c_text);
89 while (state == FLDPLUS) {
90 state = m_getfld(state, name, msgbuf,
92 cptr->c_text = add(msgbuf, cptr->c_text);
93 char_read += msg_count;
97 } while ((cptr = cptr->c_next));
99 while (state == FLDPLUS)
100 state = m_getfld(state, name, msgbuf, sizeof(msgbuf), tmp);
110 adios(NULL, "m_getfld() returned %d", state);
115 * Override any components just in case they were included in the
116 * input message. Also include command-line components given here
121 FINDCOMP (cptr, "digest");
124 cptr->c_text = digest;
126 FINDCOMP (cptr, "nmh-date");
129 cptr->c_text = getcpy(dtimenow (0));
131 FINDCOMP (cptr, "nmh-from");
136 FINDCOMP (cptr, "nmh-to");
141 FINDCOMP (cptr, "nmh-cc");
146 FINDCOMP (cptr, "nmh-subject");
149 cptr->c_text = subject;
151 FINDCOMP (cptr, "fcc");
157 cp = m_mktemp2(NULL, invo_name, NULL, &tmp);
158 if (cp == NULL) adios("forw", "unable to create temporary file");
159 strncpy (tmpfil, cp, sizeof(tmpfil));
161 if ((in = dup (fileno (tmp))) == NOTOK)
162 adios ("dup", "unable to");
164 line = mh_xmalloc ((unsigned) fmtsize);
165 fmt_scan (fmt, line, fmtsize, dat);
169 adios (tmpfil, "error writing");
171 lseek (in, (off_t) 0, SEEK_SET);
174 * Free any component buffers that we allocated
177 for (i = 0; i < (sizeof(wantcomp) / sizeof(struct comp)); i++)
178 for (cptr = wantcomp[i]; cptr != NULL; cptr = cptr->c_next)