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 *);
41 main (int argc, char **argv)
45 char *addrs = NULL, *cp, *form = NULL, buf[BUFSIZ];
46 char **argp, **arguments, *vec[MAXARGS];
50 setlocale(LC_ALL, "");
52 invo_name = r1bindex (argv[0], '/');
54 /* read user profile/context */
58 arguments = getarguments (invo_name, argc, argv, 1);
61 while ((cp = *argp++)) {
63 switch (smatch (++cp, switches)) {
65 ambigsw (cp, switches);
72 snprintf (buf, sizeof(buf),
73 "%s [switches] [switches for postproc] address ...",
75 print_help (buf, switches, 1);
78 print_version(invo_name);
82 if (!(form = *argp++) || *form == '-')
83 adios (NULL, "missing argument to %s", argp[-2]);
87 addrs = addrs ? add (cp, add (", ", addrs)) : getcpy (cp);
91 adios (NULL, "usage: %s [switches] [switches for postproc] address ...",
95 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
96 if ((fp = fopen (tmpfil, "w+")) == NULL)
97 adios (tmpfil, "unable to create");
98 cpydata (fileno (stdin), fileno (fp), "message", tmpfil);
99 fseek (fp, 0L, SEEK_SET);
100 strncpy (drft, m_tmpfil (invo_name), sizeof(drft));
101 rcvdistout (fp, form, addrs);
104 if (distout (drft, tmpfil, backup) == NOTOK)
107 vec[0] = r1bindex (postproc, '/');
108 vec[vecp++] = "-dist";
112 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
116 admonish (NULL, "unable to fork");/* fall */
118 execvp (postproc, vec);
119 fprintf (stderr, "unable to exec ");
124 done (pidXwait(child_id, postproc));
127 return 0; /* dead code to satisfy the compiler */
130 /* very similar to routine in replsbr.c */
134 static int outputlinelen = OUTPUTLINELEN;
136 static struct format *fmt;
138 static int ncomps = 0;
139 static char **compbuffers = 0;
140 static struct comp **used_buf = 0;
144 static char *addrcomps[] = {
162 rcvdistout (FILE *inb, char *form, char *addrs)
164 register int char_read = 0, format_len, i, state;
165 register char *tmpbuf, **nxtbuf, **ap;
166 char *cp, *scanl, name[NAMESZ];
167 register struct comp *cptr, **savecomp;
170 if (!(out = fopen (drft, "w")))
171 adios (drft, "unable to create");
173 /* get new format string */
174 cp = new_fs (form ? form : rcvdistcomps, NULL, NULL);
175 format_len = strlen (cp);
176 ncomps = fmt_compile (cp, &fmt) + 1;
177 if (!(nxtbuf = compbuffers = (char **) calloc ((size_t) ncomps, sizeof(char *))))
178 adios (NULL, "unable to allocate component buffers");
179 if (!(savecomp = used_buf = (struct comp **) calloc ((size_t) (ncomps + 1), sizeof(struct comp *))))
180 adios (NULL, "unable to allocate component buffer stack");
181 savecomp += ncomps + 1;
184 for (i = ncomps; i--;)
185 *nxtbuf++ = mh_xmalloc (SBUFSIZ);
186 nxtbuf = compbuffers;
189 for (ap = addrcomps; *ap; ap++) {
190 FINDCOMP (cptr, *ap);
192 cptr->c_type |= CT_ADDR;
195 FINDCOMP (cptr, "addresses");
197 cptr->c_text = addrs;
199 for (state = FLD;;) {
200 switch (state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb)) {
203 if ((cptr = wantcomp[CHASH (name)]))
205 if (!strcasecmp (name, cptr->c_name)) {
206 char_read += msg_count;
208 cptr->c_text = tmpbuf;
213 i = strlen (cp = cptr->c_text) - 1;
215 if (cptr->c_type & CT_ADDR) {
217 cp = add (",\n\t", cp);
222 cptr->c_text = add (tmpbuf, cp);
224 while (state == FLDPLUS) {
225 state = m_getfld (state, name, tmpbuf,
227 cptr->c_text = add (tmpbuf, cptr->c_text);
228 char_read += msg_count;
232 } while ((cptr = cptr->c_next));
234 while (state == FLDPLUS)
235 state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
245 adios (NULL, "m_getfld() returned %d", state);
250 i = format_len + char_read + 256;
251 scanl = mh_xmalloc ((size_t) i + 2);
252 dat[0] = dat[1] = dat[2] = dat[4] = 0;
253 dat[3] = outputlinelen;
254 fmt_scan (fmt, scanl, i, dat);
258 adios (drft, "error writing");
262 for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++); nxtbuf++, i--)
266 free ((char *) compbuffers);
267 free ((char *) used_buf);
281 exit (status ? RCV_MBX : RCV_MOK);
282 return 1; /* dead code to satisfy the compiler */