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>
15 static struct swit switches[] = {
17 { "form formfile", 4 },
25 static char backup[BUFSIZ] = "";
26 static char drft[BUFSIZ] = "";
27 static char tmpfil[BUFSIZ] = "";
32 static void rcvdistout(FILE *, char *, char *);
33 static void unlink_done(int) NORETURN;
37 main(int argc, char **argv)
41 char *addrs = NULL, *cp, *form = NULL, buf[BUFSIZ];
42 char **argp, **arguments, *vec[MAXARGS];
49 setlocale(LC_ALL, "");
51 invo_name = mhbasename(argv[0]);
53 /* read user profile/context */
56 arguments = getarguments(invo_name, argc, argv, 1);
59 while ((cp = *argp++)) {
61 switch (smatch(++cp, switches)) {
63 ambigsw(cp, switches);
70 snprintf(buf, sizeof(buf), "%s [switches] [switches for postproc] address ...", invo_name);
71 print_help(buf, switches, 1);
74 print_version(invo_name);
78 if (!(form = *argp++) || *form == '-')
79 adios(NULL, "missing argument to %s",
84 addrs = addrs ? add(cp, add(", ", addrs)) : getcpy(cp);
88 adios(NULL, "usage: %s [switches] [switches for postproc] address ...", invo_name);
92 tfile = m_mktemp2(NULL, invo_name, NULL, &fp);
93 if (tfile == NULL) adios("rcvdist", "unable to create temporary file");
94 strncpy(tmpfil, tfile, sizeof(tmpfil));
96 cpydata(fileno(stdin), fileno(fp), "message", tmpfil);
97 fseek(fp, 0L, SEEK_SET);
99 tfile = m_mktemp2(NULL, invo_name, NULL, NULL);
100 if (tfile == NULL) adios("forw", "unable to create temporary file");
101 strncpy(drft, tfile, sizeof(tmpfil));
103 rcvdistout(fp, form, addrs);
106 if (distout(drft, tmpfil, backup) == NOTOK)
109 vec[0] = mhbasename(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 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 =
178 (char **) calloc((size_t) ncomps, sizeof(char *))))
179 adios(NULL, "unable to allocate component buffers");
180 if (!(savecomp = used_buf =
181 (struct comp **) calloc((size_t) (ncomps + 1),
182 sizeof(struct comp *))))
183 adios(NULL, "unable to allocate component buffer stack");
184 savecomp += ncomps + 1;
187 for (i = ncomps; i--;)
188 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
189 nxtbuf = compbuffers;
192 for (ap = addrcomps; *ap; ap++) {
195 cptr->c_type |= CT_ADDR;
198 FINDCOMP(cptr, "addresses");
200 cptr->c_text = addrs;
202 for (state = FLD;;) {
203 switch (state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb)) {
206 if ((cptr = wantcomp[CHASH(name)]))
208 if (!mh_strcasecmp(name, cptr->c_name)) {
209 char_read += msg_count;
211 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);
223 cptr->c_text = add(tmpbuf, cp);
225 while (state == FLDPLUS) {
226 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
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++);
267 free((char *) compbuffers);
268 free((char *) used_buf);
273 unlink_done(int status)
282 exit(status ? RCV_MBX : RCV_MOK);