3 * rcvdist.c -- asynchronously redistribute messages
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 #include <h/fmt_scan.h>
12 #include <h/rcvmail.h>
17 static struct swit switches[] = {
19 { "form formfile", 4 },
27 static char backup[BUFSIZ] = "";
28 static char drft[BUFSIZ] = "";
29 static char tmpfil[BUFSIZ] = "";
34 static void rcvdistout (FILE *, char *, char *);
35 static void unlink_done (int) NORETURN;
39 main (int argc, char **argv)
43 char *addrs = NULL, *cp, *form = NULL, buf[BUFSIZ];
44 char **argp, **arguments, *vec[MAXARGS];
51 setlocale(LC_ALL, "");
53 invo_name = r1bindex (argv[0], '/');
55 /* read user profile/context */
59 arguments = getarguments (invo_name, argc, argv, 1);
62 while ((cp = *argp++)) {
64 switch (smatch (++cp, switches)) {
66 ambigsw (cp, switches);
73 snprintf (buf, sizeof(buf),
74 "%s [switches] [switches for postproc] address ...",
76 print_help (buf, switches, 1);
79 print_version(invo_name);
83 if (!(form = *argp++) || *form == '-')
84 adios (NULL, "missing argument to %s", argp[-2]);
88 addrs = addrs ? add (cp, add (", ", addrs)) : getcpy (cp);
92 adios (NULL, "usage: %s [switches] [switches for postproc] address ...",
97 tfile = m_mktemp2(NULL, invo_name, NULL, &fp);
98 if (tfile == NULL) adios("rcvdist", "unable to create temporary file");
99 strncpy (tmpfil, tfile, sizeof(tmpfil));
101 cpydata (fileno (stdin), fileno (fp), "message", tmpfil);
102 fseek (fp, 0L, SEEK_SET);
104 tfile = m_mktemp2(NULL, invo_name, NULL, NULL);
105 if (tfile == NULL) adios("forw", "unable to create temporary file");
106 strncpy (drft, tfile, sizeof(tmpfil));
108 rcvdistout (fp, form, addrs);
111 if (distout (drft, tmpfil, backup) == NOTOK)
114 vec[0] = r1bindex (postproc, '/');
115 vec[vecp++] = "-dist";
119 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
123 admonish (NULL, "unable to fork");/* fall */
125 execvp (postproc, vec);
126 fprintf (stderr, "unable to exec ");
131 done (pidXwait(child_id, postproc));
134 return 0; /* dead code to satisfy the compiler */
137 /* very similar to routine in replsbr.c */
141 static int outputlinelen = OUTPUTLINELEN;
143 static struct format *fmt;
145 static int ncomps = 0;
146 static char **compbuffers = 0;
147 static struct comp **used_buf = 0;
151 static char *addrcomps[] = {
169 rcvdistout (FILE *inb, char *form, char *addrs)
171 register int char_read = 0, format_len, i, state;
172 register char *tmpbuf, **nxtbuf, **ap;
173 char *cp, *scanl, name[NAMESZ];
174 register struct comp *cptr, **savecomp;
177 if (!(out = fopen (drft, "w")))
178 adios (drft, "unable to create");
180 /* get new format string */
181 cp = new_fs (form ? form : rcvdistcomps, NULL, NULL);
182 format_len = strlen (cp);
183 ncomps = fmt_compile (cp, &fmt) + 1;
184 if (!(nxtbuf = compbuffers = (char **) calloc ((size_t) ncomps, sizeof(char *))))
185 adios (NULL, "unable to allocate component buffers");
186 if (!(savecomp = used_buf = (struct comp **) calloc ((size_t) (ncomps + 1), sizeof(struct comp *))))
187 adios (NULL, "unable to allocate component buffer stack");
188 savecomp += ncomps + 1;
191 for (i = ncomps; i--;)
192 *nxtbuf++ = mh_xmalloc (SBUFSIZ);
193 nxtbuf = compbuffers;
196 for (ap = addrcomps; *ap; ap++) {
197 FINDCOMP (cptr, *ap);
199 cptr->c_type |= CT_ADDR;
202 FINDCOMP (cptr, "addresses");
204 cptr->c_text = addrs;
206 for (state = FLD;;) {
207 switch (state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb)) {
210 if ((cptr = wantcomp[CHASH (name)]))
212 if (!mh_strcasecmp (name, cptr->c_name)) {
213 char_read += msg_count;
215 cptr->c_text = tmpbuf;
220 i = strlen (cp = cptr->c_text) - 1;
222 if (cptr->c_type & CT_ADDR) {
224 cp = add (",\n\t", cp);
229 cptr->c_text = add (tmpbuf, cp);
231 while (state == FLDPLUS) {
232 state = m_getfld (state, name, tmpbuf,
234 cptr->c_text = add (tmpbuf, cptr->c_text);
235 char_read += msg_count;
239 } while ((cptr = cptr->c_next));
241 while (state == FLDPLUS)
242 state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
252 adios (NULL, "m_getfld() returned %d", state);
257 i = format_len + char_read + 256;
258 scanl = mh_xmalloc ((size_t) i + 2);
259 dat[0] = dat[1] = dat[2] = dat[4] = 0;
260 dat[3] = outputlinelen;
261 fmt_scan (fmt, scanl, i, dat);
265 adios (drft, "error writing");
269 for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++); nxtbuf++, i--)
273 free ((char *) compbuffers);
274 free ((char *) used_buf);
279 unlink_done (int status)
288 exit (status ? RCV_MBX : RCV_MOK);