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";
117 if ((cp = context_find ("mhlproc"))) {
118 vec[vecp++] = "-mhlproc";
123 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
127 admonish (NULL, "unable to fork");/* fall */
129 execvp (postproc, vec);
130 fprintf (stderr, "unable to exec ");
135 done (pidXwait(child_id, postproc));
138 return 0; /* dead code to satisfy the compiler */
141 /* very similar to routine in replsbr.c */
145 static int outputlinelen = OUTPUTLINELEN;
147 static struct format *fmt;
151 static char *addrcomps[] = {
169 rcvdistout (FILE *inb, char *form, char *addrs)
171 register int char_read = 0, format_len, i, state;
173 char *cp, *scanl, name[NAMESZ], tmpbuf[SBUFSIZ];
174 register struct comp *cptr;
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 fmt_compile (cp, &fmt, 1);
185 for (ap = addrcomps; *ap; ap++) {
186 cptr = fmt_findcomp (*ap);
188 cptr->c_type |= CT_ADDR;
191 cptr = fmt_findcomp ("addresses");
193 cptr->c_text = addrs;
195 for (state = FLD;;) {
196 switch (state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb)) {
199 i = fmt_addcomptext(name, tmpbuf);
201 char_read += msg_count;
202 while (state == FLDPLUS) {
203 state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
204 fmt_appendcomp(i, name, tmpbuf);
205 char_read += msg_count;
209 while (state == FLDPLUS)
210 state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
220 adios (NULL, "m_getfld() returned %d", state);
225 i = format_len + char_read + 256;
226 scanl = mh_xmalloc ((size_t) i + 2);
227 dat[0] = dat[1] = dat[2] = dat[4] = 0;
228 dat[3] = outputlinelen;
229 fmt_scan (fmt, scanl, i + 1, i, dat);
233 adios (drft, "error writing");
242 unlink_done (int status)
251 exit (status ? RCV_MBX : RCV_MOK);