]>
diplodocus.org Git - nmh/blob - uip/rcvdist.c
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>
18 static struct swit switches
[] = {
20 { "form formfile", 4 },
28 static char backup
[BUFSIZ
] = "";
29 static char drft
[BUFSIZ
] = "";
30 static char tmpfil
[BUFSIZ
] = "";
35 static void rcvdistout (FILE *, char *, char *);
40 main (int argc
, char **argv
)
44 char *addrs
= NULL
, *cp
, *form
= NULL
, buf
[BUFSIZ
];
45 char **argp
, **arguments
, *vec
[MAXARGS
];
49 setlocale(LC_ALL
, "");
51 invo_name
= r1bindex (argv
[0], '/');
53 /* read user profile/context */
57 arguments
= getarguments (invo_name
, argc
, argv
, 1);
60 while ((cp
= *argp
++)) {
62 switch (smatch (++cp
, switches
)) {
64 ambigsw (cp
, switches
);
71 snprintf (buf
, sizeof(buf
),
72 "%s [switches] [switches for postproc] address ...",
74 print_help (buf
, switches
, 1);
77 print_version(invo_name
);
81 if (!(form
= *argp
++) || *form
== '-')
82 adios (NULL
, "missing argument to %s", argp
[-2]);
86 addrs
= addrs
? add (cp
, add (", ", addrs
)) : getcpy (cp
);
90 adios (NULL
, "usage: %s [switches] [switches for postproc] address ...",
94 strncpy (tmpfil
, m_tmpfil (invo_name
), sizeof(tmpfil
));
95 if ((fp
= fopen (tmpfil
, "w+")) == NULL
)
96 adios (tmpfil
, "unable to create");
97 cpydata (fileno (stdin
), fileno (fp
), "message", tmpfil
);
98 fseek (fp
, 0L, SEEK_SET
);
99 strncpy (drft
, m_tmpfil (invo_name
), sizeof(drft
));
100 rcvdistout (fp
, form
, addrs
);
103 if (distout (drft
, tmpfil
, backup
) == NOTOK
)
106 vec
[0] = r1bindex (postproc
, '/');
107 vec
[vecp
++] = "-dist";
111 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
115 admonish (NULL
, "unable to fork");/* fall */
117 execvp (postproc
, vec
);
118 fprintf (stderr
, "unable to exec ");
123 done (pidXwait(child_id
, postproc
));
126 return 0; /* dead code to satisfy the compiler */
129 /* very similar to routine in replsbr.c */
133 static int outputlinelen
= OUTPUTLINELEN
;
135 static struct format
*fmt
;
137 static int ncomps
= 0;
138 static char **compbuffers
= 0;
139 static struct comp
**used_buf
= 0;
143 static char *addrcomps
[] = {
161 rcvdistout (FILE *inb
, char *form
, char *addrs
)
163 register int char_read
= 0, format_len
, i
, state
;
164 register char *tmpbuf
, **nxtbuf
, **ap
;
165 char *cp
, *scanl
, name
[NAMESZ
];
166 register struct comp
*cptr
, **savecomp
;
169 if (!(out
= fopen (drft
, "w")))
170 adios (drft
, "unable to create");
172 /* get new format string */
173 cp
= new_fs (form
? form
: rcvdistcomps
, NULL
, NULL
);
174 format_len
= strlen (cp
);
175 ncomps
= fmt_compile (cp
, &fmt
) + 1;
176 if (!(nxtbuf
= compbuffers
= (char **) calloc ((size_t) ncomps
, sizeof(char *))))
177 adios (NULL
, "unable to allocate component buffers");
178 if (!(savecomp
= used_buf
= (struct comp
**) calloc ((size_t) (ncomps
+ 1), sizeof(struct comp
*))))
179 adios (NULL
, "unable to allocate component buffer stack");
180 savecomp
+= ncomps
+ 1;
183 for (i
= ncomps
; i
--;)
184 if (!(*nxtbuf
++ = malloc (SBUFSIZ
)))
185 adios (NULL
, "unable to allocate component buffer");
186 nxtbuf
= compbuffers
;
189 for (ap
= addrcomps
; *ap
; ap
++) {
190 FINDCOMP (cptr
, *ap
);
192 cptr
->c_type
|= CT_ADDR
;
195 FINDCOMP (cptr
, "addresses");
197 cptr
->c_text
= addrs
;
199 for (state
= FLD
;;) {
200 switch (state
= m_getfld (state
, name
, tmpbuf
, SBUFSIZ
, inb
)) {
203 if ((cptr
= wantcomp
[CHASH (name
)]))
205 if (!strcasecmp (name
, cptr
->c_name
)) {
206 char_read
+= msg_count
;
208 cptr
->c_text
= tmpbuf
;
213 i
= strlen (cp
= cptr
->c_text
) - 1;
215 if (cptr
->c_type
& CT_ADDR
) {
217 cp
= add (",\n\t", cp
);
222 cptr
->c_text
= add (tmpbuf
, cp
);
224 while (state
== FLDPLUS
) {
225 state
= m_getfld (state
, name
, tmpbuf
,
227 cptr
->c_text
= add (tmpbuf
, cptr
->c_text
);
228 char_read
+= msg_count
;
232 } while ((cptr
= cptr
->c_next
));
234 while (state
== FLDPLUS
)
235 state
= m_getfld (state
, name
, tmpbuf
, SBUFSIZ
, inb
);
245 adios (NULL
, "m_getfld() returned %d", state
);
250 i
= format_len
+ char_read
+ 256;
251 scanl
= malloc ((size_t) i
+ 2);
252 dat
[0] = dat
[1] = dat
[2] = dat
[4] = 0;
253 dat
[3] = outputlinelen
;
254 fmt_scan (fmt
, scanl
, i
, dat
);
258 adios (drft
, "error writing");
262 for (nxtbuf
= compbuffers
, i
= ncomps
; (cptr
= *savecomp
++); nxtbuf
++, i
--)
266 free ((char *) compbuffers
);
267 free ((char *) used_buf
);
281 exit (status
? RCV_MBX
: RCV_MOK
);
282 return 1; /* dead code to satisfy the compiler */