]>
diplodocus.org Git - nmh/blob - uip/rcvdist.c
3 * rcvdist.c -- asynchronously redistribute messages
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 #include <h/fmt_scan.h>
12 #include <h/rcvmail.h>
17 static struct swit switches
[] = {
19 { "form formfile", 4 },
27 static char backup
[BUFSIZ
] = "";
28 static char drft
[BUFSIZ
] = "";
29 static char tmpfil
[BUFSIZ
] = "";
34 static void rcvdistout (FILE *, char *, char *);
35 static void unlink_done (int) NORETURN
;
39 main (int argc
, char **argv
)
43 char *addrs
= NULL
, *cp
, *form
= NULL
, buf
[BUFSIZ
];
44 char **argp
, **arguments
, *vec
[MAXARGS
];
51 setlocale(LC_ALL
, "");
53 invo_name
= r1bindex (argv
[0], '/');
55 /* read user profile/context */
59 arguments
= getarguments (invo_name
, argc
, argv
, 1);
62 while ((cp
= *argp
++)) {
64 switch (smatch (++cp
, switches
)) {
66 ambigsw (cp
, switches
);
73 snprintf (buf
, sizeof(buf
),
74 "%s [switches] [switches for postproc] address ...",
76 print_help (buf
, switches
, 1);
79 print_version(invo_name
);
83 if (!(form
= *argp
++) || *form
== '-')
84 adios (NULL
, "missing argument to %s", argp
[-2]);
88 addrs
= addrs
? add (cp
, add (", ", addrs
)) : getcpy (cp
);
92 adios (NULL
, "usage: %s [switches] [switches for postproc] address ...",
97 tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
);
98 if (tfile
== NULL
) adios("rcvdist", "unable to create temporary file");
99 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
101 cpydata (fileno (stdin
), fileno (fp
), "message", tmpfil
);
102 fseek (fp
, 0L, SEEK_SET
);
104 tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
);
105 if (tfile
== NULL
) adios("forw", "unable to create temporary file");
106 strncpy (drft
, tfile
, sizeof(tmpfil
));
108 rcvdistout (fp
, form
, addrs
);
111 if (distout (drft
, tmpfil
, backup
) == NOTOK
)
114 vec
[0] = r1bindex (postproc
, '/');
115 vec
[vecp
++] = "-dist";
117 if ((cp
= context_find ("mhlproc"))) {
118 vec
[vecp
++] = "-mhlproc";
123 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
127 admonish (NULL
, "unable to fork");/* fall */
129 execvp (postproc
, vec
);
130 fprintf (stderr
, "unable to exec ");
135 done (pidXwait(child_id
, postproc
));
138 return 0; /* dead code to satisfy the compiler */
141 /* very similar to routine in replsbr.c */
145 static int outputlinelen
= OUTPUTLINELEN
;
147 static struct format
*fmt
;
149 static int ncomps
= 0;
150 static char **compbuffers
= 0;
151 static struct comp
**used_buf
= 0;
155 static char *addrcomps
[] = {
173 rcvdistout (FILE *inb
, char *form
, char *addrs
)
175 register int char_read
= 0, format_len
, i
, state
;
176 register char *tmpbuf
, **nxtbuf
, **ap
;
177 char *cp
, *scanl
, name
[NAMESZ
];
178 register struct comp
*cptr
, **savecomp
;
181 if (!(out
= fopen (drft
, "w")))
182 adios (drft
, "unable to create");
184 /* get new format string */
185 cp
= new_fs (form
? form
: rcvdistcomps
, NULL
, NULL
);
186 format_len
= strlen (cp
);
187 ncomps
= fmt_compile (cp
, &fmt
) + 1;
188 if (!(nxtbuf
= compbuffers
= (char **) calloc ((size_t) ncomps
, sizeof(char *))))
189 adios (NULL
, "unable to allocate component buffers");
190 if (!(savecomp
= used_buf
= (struct comp
**) calloc ((size_t) (ncomps
+ 1), sizeof(struct comp
*))))
191 adios (NULL
, "unable to allocate component buffer stack");
192 savecomp
+= ncomps
+ 1;
195 for (i
= ncomps
; i
--;)
196 *nxtbuf
++ = mh_xmalloc (SBUFSIZ
);
197 nxtbuf
= compbuffers
;
200 for (ap
= addrcomps
; *ap
; ap
++) {
201 FINDCOMP (cptr
, *ap
);
203 cptr
->c_type
|= CT_ADDR
;
206 FINDCOMP (cptr
, "addresses");
208 cptr
->c_text
= addrs
;
210 for (state
= FLD
;;) {
211 switch (state
= m_getfld (state
, name
, tmpbuf
, SBUFSIZ
, inb
)) {
214 if ((cptr
= wantcomp
[CHASH (name
)]))
216 if (!mh_strcasecmp (name
, cptr
->c_name
)) {
217 char_read
+= msg_count
;
219 cptr
->c_text
= tmpbuf
;
224 i
= strlen (cp
= cptr
->c_text
) - 1;
226 if (cptr
->c_type
& CT_ADDR
) {
228 cp
= add (",\n\t", cp
);
233 cptr
->c_text
= add (tmpbuf
, cp
);
235 while (state
== FLDPLUS
) {
236 state
= m_getfld (state
, name
, tmpbuf
,
238 cptr
->c_text
= add (tmpbuf
, cptr
->c_text
);
239 char_read
+= msg_count
;
243 } while ((cptr
= cptr
->c_next
));
245 while (state
== FLDPLUS
)
246 state
= m_getfld (state
, name
, tmpbuf
, SBUFSIZ
, inb
);
256 adios (NULL
, "m_getfld() returned %d", state
);
261 i
= format_len
+ char_read
+ 256;
262 scanl
= mh_xmalloc ((size_t) i
+ 2);
263 dat
[0] = dat
[1] = dat
[2] = dat
[4] = 0;
264 dat
[3] = outputlinelen
;
265 fmt_scan (fmt
, scanl
, i
, dat
);
269 adios (drft
, "error writing");
273 for (nxtbuf
= compbuffers
, i
= ncomps
; (cptr
= *savecomp
++); nxtbuf
++, i
--)
277 free ((char *) compbuffers
);
278 free ((char *) used_buf
);
283 unlink_done (int status
)
292 exit (status
? RCV_MBX
: RCV_MOK
);