]>
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) mh_xfree(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 tmpfil
[BUFSIZ
], name
[NAMESZ
], **ap
;
58 m_getfld_state_t gstate
= 0;
61 * Open the message we'll be scanning for components
64 if ((tmp
= fopen(inputfile
, "r")) == NULL
)
65 adios (inputfile
, "Unable to open");
67 /* Get new format string */
68 nfs
= new_fs (form
, NULL
, NULL
);
69 fmtsize
= strlen (nfs
) + 256;
71 /* Compile format string */
72 (void) fmt_compile (nfs
, &fmt
, 1);
75 * Mark any components tagged as address components
78 for (ap
= addrcomps
; *ap
; ap
++) {
79 cptr
= fmt_findcomp (*ap
);
81 cptr
->c_type
|= CT_ADDR
;
85 * Process our message and save all relevant components
87 * A lot of this is taken from replsbr.c; should we try to merge
92 int msg_count
= sizeof msgbuf
;
93 state
= m_getfld (&gstate
, name
, msgbuf
, &msg_count
, tmp
);
98 * If we find a component that we're interested in, save
99 * a copy. We don't do all of that weird buffer switching
103 i
= fmt_addcomptext(name
, msgbuf
);
105 while (state
== FLDPLUS
) {
106 msg_count
= sizeof msgbuf
;
107 state
= m_getfld (&gstate
, name
, msgbuf
, &msg_count
, tmp
);
108 fmt_appendcomp(i
, name
, msgbuf
);
111 while (state
== FLDPLUS
) {
112 msg_count
= sizeof msgbuf
;
113 state
= m_getfld (&gstate
, name
, msgbuf
, &msg_count
, tmp
);
124 adios(NULL
, "m_getfld() returned %d", state
);
129 * Override any components just in case they were included in the
130 * input message. Also include command-line components given here
132 * With the memory rework I've changed things so we always get copies
133 * of these strings; I don't like the idea that the caller of this
134 * function has to know to pass in already-allocated memory (and that
135 * it will be free()'d by us).
139 m_getfld_state_destroy (&gstate
);
141 cptr
= fmt_findcomp ("digest");
144 cptr
->c_text
= getcpy(digest
);
146 cptr
= fmt_findcomp ("nmh-date");
149 cptr
->c_text
= getcpy(dtimenow (0));
151 cptr
= fmt_findcomp ("nmh-from");
154 cptr
->c_text
= getcpy(from
);
156 cptr
= fmt_findcomp ("nmh-to");
159 cptr
->c_text
= getcpy(to
);
161 cptr
= fmt_findcomp ("nmh-cc");
164 cptr
->c_text
= getcpy(cc
);
166 cptr
= fmt_findcomp ("nmh-subject");
169 cptr
->c_text
= getcpy(subject
);
171 cptr
= fmt_findcomp ("fcc");
174 cptr
->c_text
= getcpy(fcc
);
177 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &tmp
);
179 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
181 strncpy (tmpfil
, cp
, sizeof(tmpfil
));
182 (void) m_unlink (tmpfil
);
183 if ((in
= dup (fileno (tmp
))) == NOTOK
)
184 adios ("dup", "unable to");
186 line
= charstring_create (fmtsize
);
187 fmt_scan (fmt
, line
, fmtsize
, dat
, NULL
);
188 fputs (charstring_buffer (line
), tmp
);
189 charstring_free (line
);
191 adios (tmpfil
, "error writing");
193 lseek (in
, (off_t
) 0, SEEK_SET
);
196 * Free any component buffers that we allocated