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 *);
33 static void unlink_done(int) NORETURN;
37 main(int argc, char **argv)
40 char *addrs = NULL, *cp, *form = NULL, buf[BUFSIZ];
41 char **argp, **arguments, *vec[MAXARGS];
47 setlocale(LC_ALL, "");
48 invo_name = mhbasename(argv[0]);
50 /* read user profile/context */
53 arguments = getarguments(invo_name, argc, argv, 1);
56 while ((cp = *argp++)) {
58 switch (smatch(++cp, switches)) {
60 ambigsw(cp, switches);
67 snprintf(buf, sizeof(buf), "%s [switches] [switches for spost] address ...", invo_name);
68 print_help(buf, switches, 1);
71 print_version(invo_name);
75 if (!(form = *argp++) || *form == '-') {
76 adios(NULL, "missing argument to %s",
82 addrs = addrs ? add(cp, add(", ", addrs)) : getcpy(cp);
86 adios(NULL, "usage: %s [switches] [switches for spost] address ...", invo_name);
91 tfile = m_mktemp2(NULL, invo_name, NULL, &fp);
92 if (tfile == NULL) adios("rcvdist", "unable to create temporary file");
93 strncpy(tmpfil, tfile, sizeof(tmpfil));
95 cpydata(fileno(stdin), fileno(fp), "message", tmpfil);
96 fseek(fp, 0L, SEEK_SET);
98 tfile = m_mktemp2(NULL, invo_name, NULL, NULL);
99 if (tfile == NULL) adios("forw", "unable to create temporary file");
100 strncpy(drft, tfile, sizeof(tmpfil));
102 rcvdistout(fp, form, addrs);
105 if (distout(drft, tmpfil, backup) == NOTOK) {
110 vec[vecp++] = "-dist";
115 fprintf(stderr, "unable to exec ");
118 return 0; /* dead code to satisfy the compiler */
121 /* very similar to routine in replsbr.c */
125 static struct format *fmt;
127 static int ncomps = 0;
128 static char **compbuffers = 0;
129 static struct comp **used_buf = 0;
133 static char *addrcomps[] = {
151 rcvdistout(FILE *inb, char *form, char *addrs)
153 register int char_read = 0, format_len, i, state;
154 register char *tmpbuf, **nxtbuf, **ap;
155 char *cp, *scanl, name[NAMESZ];
156 register struct comp *cptr, **savecomp;
159 if (!(out = fopen(drft, "w"))) {
160 adios(drft, "unable to create");
163 /* get new format string */
164 cp = new_fs(form ? form : rcvdistcomps, NULL);
165 format_len = strlen(cp);
166 ncomps = fmt_compile(cp, &fmt) + 1;
167 if (!(nxtbuf = compbuffers =
168 (char **) calloc((size_t) ncomps, sizeof(char *)))) {
169 adios(NULL, "unable to allocate component buffers");
171 if (!(savecomp = used_buf =
172 (struct comp **) calloc((size_t) (ncomps + 1),
173 sizeof(struct comp *)))) {
174 adios(NULL, "unable to allocate component buffer stack");
176 savecomp += ncomps + 1;
179 for (i = ncomps; i--;) {
180 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
182 nxtbuf = compbuffers;
185 for (ap = addrcomps; *ap; ap++) {
188 cptr->c_type |= CT_ADDR;
192 FINDCOMP(cptr, "addresses");
194 cptr->c_text = addrs;
198 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
202 if ((cptr = wantcomp[CHASH(name)])) {
204 if (mh_strcasecmp(name, cptr->c_name)!=0) {
207 char_read += msg_count;
209 cptr->c_text = tmpbuf;
216 if (cptr->c_type & CT_ADDR) {
218 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;
231 } while ((cptr = cptr->c_next));
234 while (state == FLDPLUS) {
235 state = m_getfld(state, name, tmpbuf,
247 adios(NULL, "m_getfld() returned %d", state);
252 i = format_len + char_read + 256;
253 scanl = mh_xmalloc((size_t) i + 2);
254 dat[0] = dat[1] = dat[2] = dat[4] = 0;
255 dat[3] = OUTPUTLINELEN;
256 fmt_scan(fmt, scanl, i, dat);
260 adios(drft, "error writing");
265 for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++);
278 unlink_done(int status)
289 exit(status ? RCV_MBX : RCV_MOK);