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 = mhbasename(argv[0]);
54 /* read user profile/context */
57 arguments = getarguments(invo_name, argc, argv, 1);
60 while ((cp = *argp++)) {
62 switch (smatch(++cp, switches)) {
64 ambigsw(cp, switches);
71 snprintf(buf, sizeof(buf), "%s [switches] [switches for postproc] address ...", invo_name);
72 print_help(buf, switches, 1);
75 print_version(invo_name);
79 if (!(form = *argp++) || *form == '-')
80 adios(NULL, "missing argument to %s",
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] = mhbasename(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 struct format *fmt;
139 static int ncomps = 0;
140 static char **compbuffers = 0;
141 static struct comp **used_buf = 0;
145 static char *addrcomps[] = {
163 rcvdistout(FILE *inb, char *form, char *addrs)
165 register int char_read = 0, format_len, i, state;
166 register char *tmpbuf, **nxtbuf, **ap;
167 char *cp, *scanl, name[NAMESZ];
168 register struct comp *cptr, **savecomp;
171 if (!(out = fopen(drft, "w")))
172 adios(drft, "unable to create");
174 /* get new format string */
175 cp = new_fs(form ? form : rcvdistcomps, NULL, NULL);
176 format_len = strlen(cp);
177 ncomps = fmt_compile(cp, &fmt) + 1;
178 if (!(nxtbuf = compbuffers =
179 (char **) calloc((size_t) ncomps, sizeof(char *))))
180 adios(NULL, "unable to allocate component buffers");
181 if (!(savecomp = used_buf =
182 (struct comp **) calloc((size_t) (ncomps + 1),
183 sizeof(struct comp *))))
184 adios(NULL, "unable to allocate component buffer stack");
185 savecomp += ncomps + 1;
188 for (i = ncomps; i--;)
189 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
190 nxtbuf = compbuffers;
193 for (ap = addrcomps; *ap; ap++) {
196 cptr->c_type |= CT_ADDR;
199 FINDCOMP(cptr, "addresses");
201 cptr->c_text = addrs;
203 for (state = FLD;;) {
204 switch (state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb)) {
207 if ((cptr = wantcomp[CHASH(name)]))
209 if (!mh_strcasecmp(name, cptr->c_name)) {
210 char_read += msg_count;
212 cptr->c_text = tmpbuf;
216 i = strlen(cp = cptr->c_text) - 1;
218 if (cptr->c_type & CT_ADDR) {
220 cp = add(",\n\t", cp);
224 cptr->c_text = add(tmpbuf, cp);
226 while (state == FLDPLUS) {
227 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
228 cptr->c_text = add(tmpbuf, cptr->c_text);
229 char_read += msg_count;
233 } while ((cptr = cptr->c_next));
235 while (state == FLDPLUS)
236 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
246 adios(NULL, "m_getfld() returned %d", state);
251 i = format_len + char_read + 256;
252 scanl = mh_xmalloc((size_t) i + 2);
253 dat[0] = dat[1] = dat[2] = dat[4] = 0;
254 dat[3] = OUTPUTLINELEN;
255 fmt_scan(fmt, scanl, i, dat);
259 adios(drft, "error writing");
263 for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++);
268 free((char *) compbuffers);
269 free((char *) used_buf);
274 unlink_done(int status)
283 exit(status ? RCV_MBX : RCV_MOK);