2 ** rcvdist.c -- asynchronously redistribute messages
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
10 #include <h/fmt_scan.h>
11 #include <h/rcvmail.h>
16 static struct swit switches[] = {
18 { "form formfile", 4 },
26 static char backup[BUFSIZ] = "";
27 static char drft[BUFSIZ] = "";
28 static char tmpfil[BUFSIZ] = "";
33 static void rcvdistout (FILE *, char *, char *);
34 static void unlink_done (int) NORETURN;
38 main (int argc, char **argv)
42 char *addrs = NULL, *cp, *form = NULL, buf[BUFSIZ];
43 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), "%s [switches] [switches for postproc] address ...", invo_name);
73 print_help (buf, switches, 1);
76 print_version(invo_name);
80 if (!(form = *argp++) || *form == '-')
81 adios (NULL, "missing argument to %s", argp[-2]);
85 addrs = addrs ? add (cp, add (", ", addrs)) : getcpy (cp);
89 adios (NULL, "usage: %s [switches] [switches for postproc] address ...", invo_name);
93 tfile = m_mktemp2(NULL, invo_name, NULL, &fp);
94 if (tfile == NULL) adios("rcvdist", "unable to create temporary file");
95 strncpy (tmpfil, tfile, sizeof(tmpfil));
97 cpydata (fileno (stdin), fileno (fp), "message", tmpfil);
98 fseek (fp, 0L, SEEK_SET);
100 tfile = m_mktemp2(NULL, invo_name, NULL, NULL);
101 if (tfile == NULL) adios("forw", "unable to create temporary file");
102 strncpy (drft, tfile, sizeof(tmpfil));
104 rcvdistout (fp, form, addrs);
107 if (distout (drft, tmpfil, backup) == NOTOK)
110 vec[0] = r1bindex (postproc, '/');
111 vec[vecp++] = "-dist";
115 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
119 admonish (NULL, "unable to fork");/* fall */
121 execvp (postproc, vec);
122 fprintf (stderr, "unable to exec ");
127 done (pidXwait(child_id, postproc));
130 return 0; /* dead code to satisfy the compiler */
133 /* very similar to routine in replsbr.c */
137 static int outputlinelen = OUTPUTLINELEN;
139 static struct format *fmt;
141 static int ncomps = 0;
142 static char **compbuffers = 0;
143 static struct comp **used_buf = 0;
147 static char *addrcomps[] = {
165 rcvdistout (FILE *inb, char *form, char *addrs)
167 register int char_read = 0, format_len, i, state;
168 register char *tmpbuf, **nxtbuf, **ap;
169 char *cp, *scanl, name[NAMESZ];
170 register struct comp *cptr, **savecomp;
173 if (!(out = fopen (drft, "w")))
174 adios (drft, "unable to create");
176 /* get new format string */
177 cp = new_fs (form ? form : rcvdistcomps, NULL, NULL);
178 format_len = strlen (cp);
179 ncomps = fmt_compile (cp, &fmt) + 1;
180 if (!(nxtbuf = compbuffers =
181 (char **) calloc ((size_t) ncomps, sizeof(char *))))
182 adios (NULL, "unable to allocate component buffers");
183 if (!(savecomp = used_buf =
184 (struct comp **) calloc ((size_t) (ncomps + 1),
185 sizeof(struct comp *))))
186 adios (NULL, "unable to allocate component buffer stack");
187 savecomp += ncomps + 1;
190 for (i = ncomps; i--;)
191 *nxtbuf++ = mh_xmalloc (SBUFSIZ);
192 nxtbuf = compbuffers;
195 for (ap = addrcomps; *ap; ap++) {
196 FINDCOMP (cptr, *ap);
198 cptr->c_type |= CT_ADDR;
201 FINDCOMP (cptr, "addresses");
203 cptr->c_text = addrs;
205 for (state = FLD;;) {
206 switch (state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb)) {
209 if ((cptr = wantcomp[CHASH (name)]))
211 if (!mh_strcasecmp (name, cptr->c_name)) {
212 char_read += msg_count;
214 cptr->c_text = tmpbuf;
218 i = strlen (cp = cptr->c_text) - 1;
220 if (cptr->c_type & CT_ADDR) {
222 cp = add (",\n\t", cp);
226 cptr->c_text = add (tmpbuf, cp);
228 while (state == FLDPLUS) {
229 state = m_getfld (state, name, tmpbuf,
231 cptr->c_text = add (tmpbuf, cptr->c_text);
232 char_read += msg_count;
236 } while ((cptr = cptr->c_next));
238 while (state == FLDPLUS)
239 state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
249 adios (NULL, "m_getfld() returned %d", state);
254 i = format_len + char_read + 256;
255 scanl = mh_xmalloc ((size_t) i + 2);
256 dat[0] = dat[1] = dat[2] = dat[4] = 0;
257 dat[3] = outputlinelen;
258 fmt_scan (fmt, scanl, i, dat);
262 adios (drft, "error writing");
266 for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++);
271 free ((char *) compbuffers);
272 free ((char *) used_buf);
277 unlink_done (int status)
286 exit (status ? RCV_MBX : RCV_MOK);