]>
diplodocus.org Git - nmh/blob - uip/forwsbr.c
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
)
52 char *line
, tmpfil
[BUFSIZ
], name
[NAMESZ
], **ap
;
54 register struct comp
*cptr
;
57 m_getfld_state_t gstate
= 0;
60 * Open the message we'll be scanning for components
63 if ((tmp
= fopen(inputfile
, "r")) == NULL
)
64 adios (inputfile
, "Unable to open");
66 /* Get new format string */
67 nfs
= new_fs (form
, NULL
, NULL
);
68 fmtsize
= strlen (nfs
) + 256;
70 /* Compile format string */
71 (void) fmt_compile (nfs
, &fmt
, 1);
74 * Mark any components tagged as address components
77 for (ap
= addrcomps
; *ap
; ap
++) {
78 cptr
= fmt_findcomp (*ap
);
80 cptr
->c_type
|= CT_ADDR
;
84 * Process our message and save all relevant components
86 * A lot of this is taken from replsbr.c; should we try to merge
91 int msg_count
= sizeof msgbuf
;
92 state
= m_getfld (&gstate
, name
, msgbuf
, &msg_count
, tmp
);
97 * If we find a component that we're interested in, save
98 * a copy. We don't do all of that weird buffer switching
102 i
= fmt_addcomptext(name
, msgbuf
);
104 while (state
== FLDPLUS
) {
105 msg_count
= sizeof msgbuf
;
106 state
= m_getfld (&gstate
, name
, msgbuf
, &msg_count
, tmp
);
107 fmt_appendcomp(i
, name
, msgbuf
);
110 while (state
== FLDPLUS
) {
111 msg_count
= sizeof msgbuf
;
112 state
= m_getfld (&gstate
, name
, msgbuf
, &msg_count
, tmp
);
123 adios(NULL
, "m_getfld() returned %d", state
);
128 * Override any components just in case they were included in the
129 * input message. Also include command-line components given here
131 * With the memory rework I've changed things so we always get copies
132 * of these strings; I don't like the idea that the caller of this
133 * function has to know to pass in already-allocated memory (and that
134 * it will be free()'d by us).
138 m_getfld_state_destroy (&gstate
);
140 cptr
= fmt_findcomp ("digest");
143 cptr
->c_text
= getcpy(digest
);
145 cptr
= fmt_findcomp ("nmh-date");
148 cptr
->c_text
= getcpy(dtimenow (0));
150 cptr
= fmt_findcomp ("nmh-from");
153 cptr
->c_text
= getcpy(from
);
155 cptr
= fmt_findcomp ("nmh-to");
158 cptr
->c_text
= getcpy(to
);
160 cptr
= fmt_findcomp ("nmh-cc");
163 cptr
->c_text
= getcpy(cc
);
165 cptr
= fmt_findcomp ("nmh-subject");
168 cptr
->c_text
= getcpy(subject
);
170 cptr
= fmt_findcomp ("fcc");
173 cptr
->c_text
= getcpy(fcc
);
176 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &tmp
);
177 if (cp
== NULL
) adios("forw", "unable to create temporary file");
178 strncpy (tmpfil
, cp
, sizeof(tmpfil
));
180 if ((in
= dup (fileno (tmp
))) == NOTOK
)
181 adios ("dup", "unable to");
183 line
= mh_xmalloc ((unsigned) fmtsize
);
184 fmt_scan (fmt
, line
, fmtsize
- 1, fmtsize
, dat
, NULL
);
188 adios (tmpfil
, "error writing");
190 lseek (in
, (off_t
) 0, SEEK_SET
);
193 * Free any component buffers that we allocated