]>
diplodocus.org Git - nmh/blob - uip/rcvdist.c
7d669109abd1a11a1a47d8fea02186fbaae1996e
3 * rcvdist.c -- asynchronously redistribute messages
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
13 #include <h/fmt_scan.h>
14 #include <h/rcvmail.h>
19 static struct swit switches
[] = {
21 { "form formfile", 4 },
29 static char backup
[BUFSIZ
] = "";
30 static char drft
[BUFSIZ
] = "";
31 static char tmpfil
[BUFSIZ
] = "";
36 static void rcvdistout (FILE *, char *, char *);
37 static void unlink_done (int) NORETURN
;
41 main (int argc
, char **argv
)
45 char *addrs
= NULL
, *cp
, *form
= NULL
, buf
[BUFSIZ
];
46 char **argp
, **arguments
, *vec
[MAXARGS
];
53 setlocale(LC_ALL
, "");
55 invo_name
= r1bindex (argv
[0], '/');
57 /* read user profile/context */
61 arguments
= getarguments (invo_name
, argc
, argv
, 1);
64 while ((cp
= *argp
++)) {
66 switch (smatch (++cp
, switches
)) {
68 ambigsw (cp
, switches
);
75 snprintf (buf
, sizeof(buf
),
76 "%s [switches] [switches for postproc] address ...",
78 print_help (buf
, switches
, 1);
81 print_version(invo_name
);
85 if (!(form
= *argp
++) || *form
== '-')
86 adios (NULL
, "missing argument to %s", argp
[-2]);
90 addrs
= addrs
? add (cp
, add (", ", addrs
)) : getcpy (cp
);
94 adios (NULL
, "usage: %s [switches] [switches for postproc] address ...",
99 tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
);
100 if (tfile
== NULL
) adios("rcvdist", "unable to create temporary file");
101 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
103 cpydata (fileno (stdin
), fileno (fp
), "message", tmpfil
);
104 fseek (fp
, 0L, SEEK_SET
);
106 tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
);
107 if (tfile
== NULL
) adios("forw", "unable to create temporary file");
108 strncpy (drft
, tfile
, sizeof(tmpfil
));
110 rcvdistout (fp
, form
, addrs
);
113 if (distout (drft
, tmpfil
, backup
) == NOTOK
)
116 vec
[0] = r1bindex (postproc
, '/');
117 vec
[vecp
++] = "-dist";
121 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
125 admonish (NULL
, "unable to fork");/* fall */
127 execvp (postproc
, vec
);
128 fprintf (stderr
, "unable to exec ");
133 done (pidXwait(child_id
, postproc
));
136 return 0; /* dead code to satisfy the compiler */
139 /* very similar to routine in replsbr.c */
143 static int outputlinelen
= OUTPUTLINELEN
;
145 static struct format
*fmt
;
147 static int ncomps
= 0;
148 static char **compbuffers
= 0;
149 static struct comp
**used_buf
= 0;
153 static char *addrcomps
[] = {
171 rcvdistout (FILE *inb
, char *form
, char *addrs
)
173 register int char_read
= 0, format_len
, i
, state
;
174 register char *tmpbuf
, **nxtbuf
, **ap
;
175 char *cp
, *scanl
, name
[NAMESZ
];
176 register struct comp
*cptr
, **savecomp
;
179 if (!(out
= fopen (drft
, "w")))
180 adios (drft
, "unable to create");
182 /* get new format string */
183 cp
= new_fs (form
? form
: rcvdistcomps
, NULL
, NULL
);
184 format_len
= strlen (cp
);
185 ncomps
= fmt_compile (cp
, &fmt
) + 1;
186 if (!(nxtbuf
= compbuffers
= (char **) calloc ((size_t) ncomps
, sizeof(char *))))
187 adios (NULL
, "unable to allocate component buffers");
188 if (!(savecomp
= used_buf
= (struct comp
**) calloc ((size_t) (ncomps
+ 1), sizeof(struct comp
*))))
189 adios (NULL
, "unable to allocate component buffer stack");
190 savecomp
+= ncomps
+ 1;
193 for (i
= ncomps
; i
--;)
194 *nxtbuf
++ = mh_xmalloc (SBUFSIZ
);
195 nxtbuf
= compbuffers
;
198 for (ap
= addrcomps
; *ap
; ap
++) {
199 FINDCOMP (cptr
, *ap
);
201 cptr
->c_type
|= CT_ADDR
;
204 FINDCOMP (cptr
, "addresses");
206 cptr
->c_text
= addrs
;
208 for (state
= FLD
;;) {
209 switch (state
= m_getfld (state
, name
, tmpbuf
, SBUFSIZ
, inb
)) {
212 if ((cptr
= wantcomp
[CHASH (name
)]))
214 if (!mh_strcasecmp (name
, cptr
->c_name
)) {
215 char_read
+= msg_count
;
217 cptr
->c_text
= tmpbuf
;
222 i
= strlen (cp
= cptr
->c_text
) - 1;
224 if (cptr
->c_type
& CT_ADDR
) {
226 cp
= add (",\n\t", cp
);
231 cptr
->c_text
= add (tmpbuf
, cp
);
233 while (state
== FLDPLUS
) {
234 state
= m_getfld (state
, name
, tmpbuf
,
236 cptr
->c_text
= add (tmpbuf
, cptr
->c_text
);
237 char_read
+= msg_count
;
241 } while ((cptr
= cptr
->c_next
));
243 while (state
== FLDPLUS
)
244 state
= m_getfld (state
, name
, tmpbuf
, SBUFSIZ
, inb
);
254 adios (NULL
, "m_getfld() returned %d", state
);
259 i
= format_len
+ char_read
+ 256;
260 scanl
= mh_xmalloc ((size_t) i
+ 2);
261 dat
[0] = dat
[1] = dat
[2] = dat
[4] = 0;
262 dat
[3] = outputlinelen
;
263 fmt_scan (fmt
, scanl
, i
, dat
);
267 adios (drft
, "error writing");
271 for (nxtbuf
= compbuffers
, i
= ncomps
; (cptr
= *savecomp
++); nxtbuf
++, i
--)
275 free ((char *) compbuffers
);
276 free ((char *) used_buf
);
281 unlink_done (int status
)
290 exit (status
? RCV_MBX
: RCV_MOK
);