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 fmtsize, state, char_read = 0;
32 char *line, tmpfil[BUFSIZ], name[NAMESZ];
34 register struct comp *cptr;
39 * Open the message we'll be scanning for components
42 if ((tmp = fopen(inputfile, "r")) == NULL)
43 adios (inputfile, "Unable to open");
45 /* Get new format string */
46 nfs = new_fs (form, NULL, NULL);
47 fmtsize = strlen (nfs) + 256;
49 /* Compile format string */
50 (void) fmt_compile (nfs, &fmt);
53 * Process our message and save all relevant components
55 * A lot of this is taken from replsbr.c; should we try to merge
60 state = m_getfld(state, name, msgbuf, sizeof(msgbuf), tmp);
65 * If we find a component that we're interested in, save
66 * a copy. We don't do all of that weird buffer switching
69 if ((cptr = wantcomp[CHASH(name)]))
71 if (mh_strcasecmp(name, cptr->c_name) == 0) {
72 char_read += msg_count;
74 i = strlen(cptr->c_text = strdup(msgbuf)) - 1;
75 if (cptr->c_text[i] == '\n')
76 cptr->c_text[i] = '\0';
78 i = strlen(cptr->c_text) - 1;
79 if (cptr->c_text[i] == '\n') {
80 if (cptr->c_type & CT_ADDR) {
81 cptr->c_text[i] = '\0';
82 cptr->c_text = add(",\n\t",
85 cptr->c_text = add ("\t", cptr->c_text);
88 cptr->c_text = add(msgbuf, cptr->c_text);
90 while (state == FLDPLUS) {
91 state = m_getfld(state, name, msgbuf,
93 cptr->c_text = add(msgbuf, cptr->c_text);
94 char_read += msg_count;
98 } while ((cptr = cptr->c_next));
100 while (state == FLDPLUS)
101 state = m_getfld(state, name, msgbuf, sizeof(msgbuf), tmp);
111 adios(NULL, "m_getfld() returned %d", state);
116 * Override any components just in case they were included in the
117 * input message. Also include command-line components given here
122 FINDCOMP (cptr, "digest");
125 cptr->c_text = digest;
127 FINDCOMP (cptr, "nmh-date");
130 cptr->c_text = getcpy(dtimenow (0));
132 FINDCOMP (cptr, "nmh-from");
137 FINDCOMP (cptr, "nmh-to");
142 FINDCOMP (cptr, "nmh-cc");
147 FINDCOMP (cptr, "nmh-subject");
150 cptr->c_text = subject;
152 FINDCOMP (cptr, "fcc");
158 cp = m_mktemp2(NULL, invo_name, NULL, &tmp);
159 if (cp == NULL) adios("forw", "unable to create temporary file");
160 strncpy (tmpfil, cp, sizeof(tmpfil));
162 if ((in = dup (fileno (tmp))) == NOTOK)
163 adios ("dup", "unable to");
165 line = mh_xmalloc ((unsigned) fmtsize);
166 fmt_scan (fmt, line, fmtsize, dat);
170 adios (tmpfil, "error writing");
172 lseek (in, (off_t) 0, SEEK_SET);
175 * Free any component buffers that we allocated
178 for (i = 0; i < (sizeof(wantcomp) / sizeof(struct comp)); i++)
179 for (cptr = wantcomp[i]; cptr != NULL; cptr = cptr->c_next)