3 * rcvdist.c -- asynchronously redistribute messages
9 #include <h/fmt_scan.h>
10 #include <h/rcvmail.h>
11 #include <zotnet/tws/tws.h>
13 static struct swit switches[] = {
15 { "form formfile", 4 },
23 static char backup[BUFSIZ] = "";
24 static char drft[BUFSIZ] = "";
25 static char tmpfil[BUFSIZ] = "";
30 static void rcvdistout (FILE *, char *, char *);
35 main (int argc, char **argv)
39 char *addrs = NULL, *cp, *form = NULL, buf[BUFSIZ];
40 char **argp, **arguments, *vec[MAXARGS];
44 setlocale(LC_ALL, "");
46 invo_name = r1bindex (argv[0], '/');
48 /* read user profile/context */
52 arguments = getarguments (invo_name, argc, argv, 1);
55 while ((cp = *argp++)) {
57 switch (smatch (++cp, switches)) {
59 ambigsw (cp, switches);
66 snprintf (buf, sizeof(buf),
67 "%s [switches] [switches for postproc] address ...",
69 print_help (buf, switches, 1);
72 print_version(invo_name);
76 if (!(form = *argp++) || *form == '-')
77 adios (NULL, "missing argument to %s", argp[-2]);
81 addrs = addrs ? add (cp, add (", ", addrs)) : getcpy (cp);
85 adios (NULL, "usage: %s [switches] [switches for postproc] address ...",
89 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
90 if ((fp = fopen (tmpfil, "w+")) == NULL)
91 adios (tmpfil, "unable to create");
92 cpydata (fileno (stdin), fileno (fp), "message", tmpfil);
93 fseek (fp, 0L, SEEK_SET);
94 strncpy (drft, m_tmpfil (invo_name), sizeof(drft));
95 rcvdistout (fp, form, addrs);
98 if (distout (drft, tmpfil, backup) == NOTOK)
101 vec[0] = r1bindex (postproc, '/');
102 vec[vecp++] = "-dist";
106 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
110 admonish (NULL, "unable to fork");/* fall */
112 execvp (postproc, vec);
113 fprintf (stderr, "unable to exec ");
118 done (pidXwait(child_id, postproc));
123 /* very similar to routine in replsbr.c */
127 static int outputlinelen = OUTPUTLINELEN;
129 static struct format *fmt;
131 static int ncomps = 0;
132 static char **compbuffers = 0;
133 static struct comp **used_buf = 0;
137 static char *addrcomps[] = {
155 rcvdistout (FILE *inb, char *form, char *addrs)
157 register int char_read = 0, format_len, i, state;
158 register char *tmpbuf, **nxtbuf, **ap;
159 char *cp, *scanl, name[NAMESZ];
160 register struct comp *cptr, **savecomp;
163 if (!(out = fopen (drft, "w")))
164 adios (drft, "unable to create");
166 /* get new format string */
167 cp = new_fs (form ? form : rcvdistcomps, NULL, NULL);
168 format_len = strlen (cp);
169 ncomps = fmt_compile (cp, &fmt) + 1;
170 if (!(nxtbuf = compbuffers = (char **) calloc ((size_t) ncomps, sizeof(char *))))
171 adios (NULL, "unable to allocate component buffers");
172 if (!(savecomp = used_buf = (struct comp **) calloc ((size_t) (ncomps + 1), sizeof(struct comp *))))
173 adios (NULL, "unable to allocate component buffer stack");
174 savecomp += ncomps + 1;
177 for (i = ncomps; i--;)
178 if (!(*nxtbuf++ = malloc (SBUFSIZ)))
179 adios (NULL, "unable to allocate component buffer");
180 nxtbuf = compbuffers;
183 for (ap = addrcomps; *ap; ap++) {
184 FINDCOMP (cptr, *ap);
186 cptr->c_type |= CT_ADDR;
189 FINDCOMP (cptr, "addresses");
191 cptr->c_text = addrs;
193 for (state = FLD;;) {
194 switch (state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb)) {
197 if ((cptr = wantcomp[CHASH (name)]))
199 if (!strcasecmp (name, cptr->c_name)) {
200 char_read += msg_count;
202 cptr->c_text = tmpbuf;
207 i = strlen (cp = cptr->c_text) - 1;
209 if (cptr->c_type & CT_ADDR) {
211 cp = add (",\n\t", cp);
215 cptr->c_text = add (tmpbuf, cp);
217 while (state == FLDPLUS) {
218 state = m_getfld (state, name, tmpbuf,
220 cptr->c_text = add (tmpbuf, cptr->c_text);
221 char_read += msg_count;
225 } while ((cptr = cptr->c_next));
227 while (state == FLDPLUS)
228 state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
238 adios (NULL, "m_getfld() returned %d", state);
243 i = format_len + char_read + 256;
244 scanl = malloc ((size_t) i + 2);
245 dat[0] = dat[1] = dat[2] = dat[4] = 0;
246 dat[3] = outputlinelen;
247 fmt_scan (fmt, scanl, i, dat);
251 adios (drft, "error writing");
255 for (nxtbuf = compbuffers, i = ncomps; cptr = *savecomp++; nxtbuf++, i--)
259 free ((char *) compbuffers);
260 free ((char *) used_buf);
274 exit (status ? RCV_MBX : RCV_MOK);