]>
diplodocus.org Git - nmh/blob - uip/rcvdist.c
1 /* rcvdist.c -- asynchronously redistribute messages
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
9 #include <h/fmt_scan.h>
10 #include <h/rcvmail.h>
15 #define RCVDIST_SWITCHES \
16 X("form formfile", 4, FORMSW) \
17 X("version", 0, VERSIONSW) \
18 X("help", 0, HELPSW) \
20 #define X(sw, minchars, id) id,
21 DEFINE_SWITCH_ENUM(RCVDIST
);
24 #define X(sw, minchars, id) { sw, minchars, id },
25 DEFINE_SWITCH_ARRAY(RCVDIST
, switches
);
28 static char backup
[BUFSIZ
] = "";
29 static char drft
[BUFSIZ
] = "";
30 static char tmpfil
[BUFSIZ
] = "";
35 static void rcvdistout (FILE *, char *, char *);
36 static void unlink_done (int) NORETURN
;
40 main (int argc
, char **argv
)
44 char *addrs
= NULL
, *cp
, *form
= NULL
, buf
[BUFSIZ
], *program
;
45 char **argp
, **arguments
, **vec
;
49 if (nmh_init(argv
[0], 2)) { return 1; }
54 * Configure this now, since any unknown switches to rcvdist get
58 vec
= argsplit(postproc
, &program
, &vecp
);
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
)) : mh_xstrdup(cp
);
94 adios (NULL
, "usage: %s [switches] [switches for postproc] address ...",
99 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
100 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
102 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
104 cpydata (fileno (stdin
), fileno (fp
), "message", tmpfil
);
105 fseek (fp
, 0L, SEEK_SET
);
107 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
108 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
110 strncpy (drft
, tfile
, sizeof(tmpfil
));
112 rcvdistout (fp
, form
, addrs
);
115 if (distout (drft
, tmpfil
, backup
) == NOTOK
)
118 vec
[vecp
++] = "-dist";
119 if ((cp
= context_find ("mhlproc"))) {
120 vec
[vecp
++] = "-mhlproc";
126 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
130 inform("unable to fork, continuing...");
133 execvp (program
, vec
);
134 fprintf (stderr
, "unable to exec ");
139 done (pidXwait(child_id
, postproc
));
142 return 0; /* dead code to satisfy the compiler */
145 /* very similar to routine in replsbr.c */
147 static int outputlinelen
= OUTPUTLINELEN
;
149 static struct format
*fmt
;
153 static char *addrcomps
[] = {
171 rcvdistout (FILE *inb
, char *form
, char *addrs
)
173 int char_read
= 0, format_len
, i
, state
;
175 char *cp
, name
[NAMESZ
], tmpbuf
[NMH_BUFSIZ
];
179 m_getfld_state_t gstate
= 0;
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 fmt_compile (cp
, &fmt
, 1);
189 for (ap
= addrcomps
; *ap
; ap
++) {
190 cptr
= fmt_findcomp (*ap
);
192 cptr
->c_type
|= CT_ADDR
;
195 cptr
= fmt_findcomp ("addresses");
197 cptr
->c_text
= addrs
;
200 int msg_count
= sizeof tmpbuf
;
201 switch (state
= m_getfld (&gstate
, name
, tmpbuf
, &msg_count
, inb
)) {
204 i
= fmt_addcomptext(name
, tmpbuf
);
206 char_read
+= msg_count
;
207 while (state
== FLDPLUS
) {
208 msg_count
= sizeof tmpbuf
;
209 state
= m_getfld (&gstate
, name
, tmpbuf
, &msg_count
, inb
);
210 fmt_appendcomp(i
, name
, tmpbuf
);
211 char_read
+= msg_count
;
215 while (state
== FLDPLUS
) {
216 msg_count
= sizeof tmpbuf
;
217 state
= m_getfld (&gstate
, name
, tmpbuf
, &msg_count
, inb
);
228 adios (NULL
, "m_getfld() returned %d", state
);
232 m_getfld_state_destroy (&gstate
);
234 i
= format_len
+ char_read
+ 256;
235 scanl
= charstring_create (i
+ 2);
236 dat
[0] = dat
[1] = dat
[2] = dat
[4] = 0;
237 dat
[3] = outputlinelen
;
238 fmt_scan (fmt
, scanl
, i
, dat
, NULL
);
239 fputs (charstring_buffer (scanl
), out
);
242 adios (drft
, "error writing");
245 charstring_free (scanl
);
251 unlink_done (int status
)
254 (void) m_unlink (backup
);
256 (void) m_unlink (drft
);
258 (void) m_unlink (tmpfil
);
260 exit (status
? RCV_MBX
: RCV_MOK
);