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 */
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",
86 addrs = addrs ? add(cp, add(", ", addrs)) : getcpy(cp);
90 adios(NULL, "usage: %s [switches] [switches for postproc] address ...", invo_name);
94 tfile = m_mktemp2(NULL, invo_name, NULL, &fp);
95 if (tfile == NULL) adios("rcvdist", "unable to create temporary file");
96 strncpy(tmpfil, tfile, sizeof(tmpfil));
98 cpydata(fileno(stdin), fileno(fp), "message", tmpfil);
99 fseek(fp, 0L, SEEK_SET);
101 tfile = m_mktemp2(NULL, invo_name, NULL, NULL);
102 if (tfile == NULL) adios("forw", "unable to create temporary file");
103 strncpy(drft, tfile, sizeof(tmpfil));
105 rcvdistout(fp, form, addrs);
108 if (distout(drft, tmpfil, backup) == NOTOK)
111 vec[0] = mhbasename(postproc);
112 vec[vecp++] = "-dist";
116 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
120 admonish(NULL, "unable to fork");/* fall */
122 execvp(postproc, vec);
123 fprintf(stderr, "unable to exec ");
128 done(pidXwait(child_id, postproc));
131 return 0; /* dead code to satisfy the compiler */
134 /* very similar to routine in replsbr.c */
138 static int outputlinelen = OUTPUTLINELEN;
140 static struct format *fmt;
142 static int ncomps = 0;
143 static char **compbuffers = 0;
144 static struct comp **used_buf = 0;
148 static char *addrcomps[] = {
166 rcvdistout(FILE *inb, char *form, char *addrs)
168 register int char_read = 0, format_len, i, state;
169 register char *tmpbuf, **nxtbuf, **ap;
170 char *cp, *scanl, name[NAMESZ];
171 register struct comp *cptr, **savecomp;
174 if (!(out = fopen(drft, "w")))
175 adios(drft, "unable to create");
177 /* get new format string */
178 cp = new_fs(form ? form : rcvdistcomps, NULL, NULL);
179 format_len = strlen(cp);
180 ncomps = fmt_compile(cp, &fmt) + 1;
181 if (!(nxtbuf = compbuffers =
182 (char **) calloc((size_t) ncomps, sizeof(char *))))
183 adios(NULL, "unable to allocate component buffers");
184 if (!(savecomp = used_buf =
185 (struct comp **) calloc((size_t) (ncomps + 1),
186 sizeof(struct comp *))))
187 adios(NULL, "unable to allocate component buffer stack");
188 savecomp += ncomps + 1;
191 for (i = ncomps; i--;)
192 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
193 nxtbuf = compbuffers;
196 for (ap = addrcomps; *ap; ap++) {
199 cptr->c_type |= CT_ADDR;
202 FINDCOMP(cptr, "addresses");
204 cptr->c_text = addrs;
206 for (state = FLD;;) {
207 switch (state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb)) {
210 if ((cptr = wantcomp[CHASH(name)]))
212 if (!mh_strcasecmp(name, cptr->c_name)) {
213 char_read += msg_count;
215 cptr->c_text = tmpbuf;
219 i = strlen(cp = cptr->c_text) - 1;
221 if (cptr->c_type & CT_ADDR) {
223 cp = add(",\n\t", cp);
227 cptr->c_text = add(tmpbuf, cp);
229 while (state == FLDPLUS) {
230 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
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);