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];
48 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) {
112 vec[vecp++] = "-dist";
117 fprintf(stderr, "unable to exec ");
120 return 0; /* dead code to satisfy the compiler */
123 /* very similar to routine in replsbr.c */
127 static struct format *fmt;
129 static int ncomps = 0;
130 static char **compbuffers = 0;
131 static struct comp **used_buf = 0;
135 static char *addrcomps[] = {
153 rcvdistout(FILE *inb, char *form, char *addrs)
155 register int char_read = 0, format_len, i, state;
156 register char *tmpbuf, **nxtbuf, **ap;
157 char *cp, *scanl, name[NAMESZ];
158 register struct comp *cptr, **savecomp;
161 if (!(out = fopen(drft, "w"))) {
162 adios(drft, "unable to create");
165 /* get new format string */
166 cp = new_fs(form ? form : rcvdistcomps, NULL);
167 format_len = strlen(cp);
168 ncomps = fmt_compile(cp, &fmt) + 1;
169 if (!(nxtbuf = compbuffers =
170 (char **) calloc((size_t) ncomps, sizeof(char *)))) {
171 adios(NULL, "unable to allocate component buffers");
173 if (!(savecomp = used_buf =
174 (struct comp **) calloc((size_t) (ncomps + 1),
175 sizeof(struct comp *)))) {
176 adios(NULL, "unable to allocate component buffer stack");
178 savecomp += ncomps + 1;
181 for (i = ncomps; i--;) {
182 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
184 nxtbuf = compbuffers;
187 for (ap = addrcomps; *ap; ap++) {
190 cptr->c_type |= CT_ADDR;
194 FINDCOMP(cptr, "addresses");
196 cptr->c_text = addrs;
200 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
204 if ((cptr = wantcomp[CHASH(name)])) {
206 if (mh_strcasecmp(name, cptr->c_name)!=0) {
209 char_read += msg_count;
211 cptr->c_text = tmpbuf;
218 if (cptr->c_type & CT_ADDR) {
220 cp = add(",\n\t", cp);
225 cptr->c_text = add(tmpbuf, cp);
227 while (state == FLDPLUS) {
228 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
229 cptr->c_text = add(tmpbuf, cptr->c_text);
230 char_read += msg_count;
233 } while ((cptr = cptr->c_next));
236 while (state == FLDPLUS) {
237 state = m_getfld(state, name, tmpbuf,
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");
267 for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++);
280 unlink_done(int status)
291 exit(status ? RCV_MBX : RCV_MOK);