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));
121 return 0; /* dead code to satisfy the compiler */
124 /* very similar to routine in replsbr.c */
128 static int outputlinelen = OUTPUTLINELEN;
130 static struct format *fmt;
132 static int ncomps = 0;
133 static char **compbuffers = 0;
134 static struct comp **used_buf = 0;
138 static char *addrcomps[] = {
156 rcvdistout (FILE *inb, char *form, char *addrs)
158 register int char_read = 0, format_len, i, state;
159 register char *tmpbuf, **nxtbuf, **ap;
160 char *cp, *scanl, name[NAMESZ];
161 register struct comp *cptr, **savecomp;
164 if (!(out = fopen (drft, "w")))
165 adios (drft, "unable to create");
167 /* get new format string */
168 cp = new_fs (form ? form : rcvdistcomps, NULL, NULL);
169 format_len = strlen (cp);
170 ncomps = fmt_compile (cp, &fmt) + 1;
171 if (!(nxtbuf = compbuffers = (char **) calloc ((size_t) ncomps, sizeof(char *))))
172 adios (NULL, "unable to allocate component buffers");
173 if (!(savecomp = used_buf = (struct comp **) calloc ((size_t) (ncomps + 1), sizeof(struct comp *))))
174 adios (NULL, "unable to allocate component buffer stack");
175 savecomp += ncomps + 1;
178 for (i = ncomps; i--;)
179 if (!(*nxtbuf++ = malloc (SBUFSIZ)))
180 adios (NULL, "unable to allocate component buffer");
181 nxtbuf = compbuffers;
184 for (ap = addrcomps; *ap; ap++) {
185 FINDCOMP (cptr, *ap);
187 cptr->c_type |= CT_ADDR;
190 FINDCOMP (cptr, "addresses");
192 cptr->c_text = addrs;
194 for (state = FLD;;) {
195 switch (state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb)) {
198 if ((cptr = wantcomp[CHASH (name)]))
200 if (!strcasecmp (name, cptr->c_name)) {
201 char_read += msg_count;
203 cptr->c_text = tmpbuf;
208 i = strlen (cp = cptr->c_text) - 1;
210 if (cptr->c_type & CT_ADDR) {
212 cp = add (",\n\t", cp);
217 cptr->c_text = add (tmpbuf, cp);
219 while (state == FLDPLUS) {
220 state = m_getfld (state, name, tmpbuf,
222 cptr->c_text = add (tmpbuf, cptr->c_text);
223 char_read += msg_count;
227 } while ((cptr = cptr->c_next));
229 while (state == FLDPLUS)
230 state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
240 adios (NULL, "m_getfld() returned %d", state);
245 i = format_len + char_read + 256;
246 scanl = malloc ((size_t) i + 2);
247 dat[0] = dat[1] = dat[2] = dat[4] = 0;
248 dat[3] = outputlinelen;
249 fmt_scan (fmt, scanl, i, dat);
253 adios (drft, "error writing");
257 for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++); nxtbuf++, i--)
261 free ((char *) compbuffers);
262 free ((char *) used_buf);
276 exit (status ? RCV_MBX : RCV_MOK);
277 return 1; /* dead code to satisfy the compiler */