3 * rcvdist.c -- asynchronously redistribute messages
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
13 #include <h/fmt_scan.h>
14 #include <h/rcvmail.h>
19 static struct swit switches[] = {
21 { "form formfile", 4 },
29 static char backup[BUFSIZ] = "";
30 static char drft[BUFSIZ] = "";
31 static char tmpfil[BUFSIZ] = "";
36 static void rcvdistout (FILE *, char *, char *);
37 static void unlink_done (int);
41 main (int argc, char **argv)
45 char *addrs = NULL, *cp, *form = NULL, buf[BUFSIZ];
46 char **argp, **arguments, *vec[MAXARGS];
52 setlocale(LC_ALL, "");
54 invo_name = r1bindex (argv[0], '/');
56 /* read user profile/context */
60 arguments = getarguments (invo_name, argc, argv, 1);
63 while ((cp = *argp++)) {
65 switch (smatch (++cp, switches)) {
67 ambigsw (cp, switches);
74 snprintf (buf, sizeof(buf),
75 "%s [switches] [switches for postproc] address ...",
77 print_help (buf, switches, 1);
80 print_version(invo_name);
84 if (!(form = *argp++) || *form == '-')
85 adios (NULL, "missing argument to %s", argp[-2]);
89 addrs = addrs ? add (cp, add (", ", addrs)) : getcpy (cp);
93 adios (NULL, "usage: %s [switches] [switches for postproc] address ...",
97 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
98 if ((fp = fopen (tmpfil, "w+")) == NULL)
99 adios (tmpfil, "unable to create");
100 cpydata (fileno (stdin), fileno (fp), "message", tmpfil);
101 fseek (fp, 0L, SEEK_SET);
102 strncpy (drft, m_tmpfil (invo_name), sizeof(drft));
103 rcvdistout (fp, form, addrs);
106 if (distout (drft, tmpfil, backup) == NOTOK)
109 vec[0] = r1bindex (postproc, '/');
110 vec[vecp++] = "-dist";
114 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
118 admonish (NULL, "unable to fork");/* fall */
120 execvp (postproc, vec);
121 fprintf (stderr, "unable to exec ");
126 done (pidXwait(child_id, postproc));
129 return 0; /* dead code to satisfy the compiler */
132 /* very similar to routine in replsbr.c */
136 static int outputlinelen = OUTPUTLINELEN;
138 static struct format *fmt;
140 static int ncomps = 0;
141 static char **compbuffers = 0;
142 static struct comp **used_buf = 0;
146 static char *addrcomps[] = {
164 rcvdistout (FILE *inb, char *form, char *addrs)
166 register int char_read = 0, format_len, i, state;
167 register char *tmpbuf, **nxtbuf, **ap;
168 char *cp, *scanl, name[NAMESZ];
169 register struct comp *cptr, **savecomp;
172 if (!(out = fopen (drft, "w")))
173 adios (drft, "unable to create");
175 /* get new format string */
176 cp = new_fs (form ? form : rcvdistcomps, NULL, NULL);
177 format_len = strlen (cp);
178 ncomps = fmt_compile (cp, &fmt) + 1;
179 if (!(nxtbuf = compbuffers = (char **) calloc ((size_t) ncomps, sizeof(char *))))
180 adios (NULL, "unable to allocate component buffers");
181 if (!(savecomp = used_buf = (struct comp **) calloc ((size_t) (ncomps + 1), sizeof(struct comp *))))
182 adios (NULL, "unable to allocate component buffer stack");
183 savecomp += ncomps + 1;
186 for (i = ncomps; i--;)
187 *nxtbuf++ = mh_xmalloc (SBUFSIZ);
188 nxtbuf = compbuffers;
191 for (ap = addrcomps; *ap; ap++) {
192 FINDCOMP (cptr, *ap);
194 cptr->c_type |= CT_ADDR;
197 FINDCOMP (cptr, "addresses");
199 cptr->c_text = addrs;
201 for (state = FLD;;) {
202 switch (state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb)) {
205 if ((cptr = wantcomp[CHASH (name)]))
207 if (!mh_strcasecmp (name, cptr->c_name)) {
208 char_read += msg_count;
210 cptr->c_text = tmpbuf;
215 i = strlen (cp = cptr->c_text) - 1;
217 if (cptr->c_type & CT_ADDR) {
219 cp = add (",\n\t", cp);
224 cptr->c_text = add (tmpbuf, cp);
226 while (state == FLDPLUS) {
227 state = m_getfld (state, name, tmpbuf,
229 cptr->c_text = add (tmpbuf, cptr->c_text);
230 char_read += msg_count;
234 } while ((cptr = cptr->c_next));
236 while (state == FLDPLUS)
237 state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
247 adios (NULL, "m_getfld() returned %d", state);
252 i = format_len + char_read + 256;
253 scanl = mh_xmalloc ((size_t) i + 2);
254 dat[0] = dat[1] = dat[2] = dat[4] = 0;
255 dat[3] = outputlinelen;
256 fmt_scan (fmt, scanl, i, dat);
260 adios (drft, "error writing");
264 for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++); nxtbuf++, i--)
268 free ((char *) compbuffers);
269 free ((char *) used_buf);
274 unlink_done (int status)
283 exit (status ? RCV_MBX : RCV_MOK);