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", 0 },
25 static char backup[BUFSIZ] = "";
26 static char drft[BUFSIZ] = "";
27 static char tmpfil[BUFSIZ] = "";
32 static void rcvdistout(FILE *, char *, char *);
37 main(int argc, char **argv)
40 char *addrs = NULL, *cp, *form = NULL, buf[BUFSIZ];
41 char **argp, **arguments, *vec[MAXARGS];
45 if (atexit(unlink_done) != 0) {
46 adios(NULL, "atexit failed");
49 setlocale(LC_ALL, "");
50 invo_name = mhbasename(argv[0]);
52 /* read user profile/context */
55 arguments = getarguments(invo_name, argc, argv, 1);
58 while ((cp = *argp++)) {
60 switch (smatch(++cp, switches)) {
62 ambigsw(cp, switches);
69 snprintf(buf, sizeof(buf), "%s [switches] [switches for spost] address ...", invo_name);
70 print_help(buf, switches, 1);
73 print_version(invo_name);
77 if (!(form = *argp++) || *form == '-') {
78 adios(NULL, "missing argument to %s",
84 addrs = addrs ? add(cp, add(", ", addrs)) : getcpy(cp);
88 adios(NULL, "usage: %s [switches] [switches for spost] 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) {
108 /* sysexits.h EX_DATAERR */
113 vec[vecp++] = "-dist";
118 fprintf(stderr, "unable to exec ");
121 return 0; /* dead code to satisfy the compiler */
124 /* very similar to routine in replsbr.c */
128 static struct format *fmt;
130 static int ncomps = 0;
131 static char **compbuffers = 0;
132 static struct comp **used_buf = 0;
136 static char *addrcomps[] = {
154 rcvdistout(FILE *inb, char *form, char *addrs)
156 register int char_read = 0, format_len, i, state;
157 register char *tmpbuf, **nxtbuf, **ap;
158 char *cp, *scanl, name[NAMESZ];
159 register struct comp *cptr, **savecomp;
162 if (!(out = fopen(drft, "w"))) {
163 adios(drft, "unable to create");
166 /* get new format string */
167 cp = new_fs(form ? form : rcvdistcomps, NULL);
168 format_len = strlen(cp);
169 ncomps = fmt_compile(cp, &fmt) + 1;
170 if (!(nxtbuf = compbuffers =
171 (char **) calloc((size_t) ncomps, sizeof(char *)))) {
172 adios(NULL, "unable to allocate component buffers");
174 if (!(savecomp = used_buf =
175 (struct comp **) calloc((size_t) (ncomps + 1),
176 sizeof(struct comp *)))) {
177 adios(NULL, "unable to allocate component buffer stack");
179 savecomp += ncomps + 1;
182 for (i = ncomps; i--;) {
183 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
185 nxtbuf = compbuffers;
188 for (ap = addrcomps; *ap; ap++) {
191 cptr->c_type |= CT_ADDR;
195 FINDCOMP(cptr, "addresses");
197 cptr->c_text = addrs;
201 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
205 if ((cptr = wantcomp[CHASH(name)])) {
207 if (mh_strcasecmp(name, cptr->c_name)!=0) {
210 char_read += msg_count;
212 cptr->c_text = tmpbuf;
219 if (cptr->c_type & CT_ADDR) {
221 cp = add(",\n\t", cp);
226 cptr->c_text = add(tmpbuf, cp);
228 while (state == FLDPLUS) {
229 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
230 cptr->c_text = add(tmpbuf, cptr->c_text);
231 char_read += msg_count;
234 } while ((cptr = cptr->c_next));
237 while (state == FLDPLUS) {
238 state = m_getfld(state, name, tmpbuf,
250 adios(NULL, "m_getfld() returned %d", state);
255 i = format_len + char_read + 256;
256 scanl = mh_xmalloc((size_t) i + 2);
257 dat[0] = dat[1] = dat[2] = dat[4] = 0;
258 dat[3] = OUTPUTLINELEN;
259 fmt_scan(fmt, scanl, i, dat);
263 adios(drft, "error writing");
268 for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++);