]>
diplodocus.org Git - nmh/blob - uip/rcvdist.c
85e877dcc7afd6d002e8cba6d591df5b26b2fb63
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 "sbr/getarguments.h"
10 #include "sbr/smatch.h"
11 #include "sbr/cpydata.h"
12 #include "sbr/context_find.h"
13 #include "sbr/ambigsw.h"
14 #include "sbr/pidstatus.h"
15 #include "sbr/print_version.h"
16 #include "sbr/print_help.h"
17 #include "sbr/arglist.h"
18 #include "sbr/error.h"
19 #include "h/fmt_scan.h"
20 #include "h/rcvmail.h"
25 #include "sbr/m_mktemp.h"
27 #define RCVDIST_SWITCHES \
28 X("form formfile", 4, FORMSW) \
29 X("version", 0, VERSIONSW) \
30 X("help", 0, HELPSW) \
32 #define X(sw, minchars, id) id,
33 DEFINE_SWITCH_ENUM(RCVDIST
);
36 #define X(sw, minchars, id) { sw, minchars, id },
37 DEFINE_SWITCH_ARRAY(RCVDIST
, switches
);
40 static char backup
[BUFSIZ
] = "";
41 static char drft
[BUFSIZ
] = "";
42 static char tmpfil
[BUFSIZ
] = "";
47 static void rcvdistout (FILE *, char *, char *);
48 static void unlink_done (int) NORETURN
;
52 main (int argc
, char **argv
)
56 char *addrs
= NULL
, *cp
, *form
= NULL
, buf
[BUFSIZ
], *program
;
57 char **argp
, **arguments
, **vec
;
61 if (nmh_init(argv
[0], true, false)) { return 1; }
63 set_done(unlink_done
);
66 * Configure this now, since any unknown switches to rcvdist get
70 vec
= argsplit(postproc
, &program
, &vecp
);
73 arguments
= getarguments (invo_name
, argc
, argv
, 1);
76 while ((cp
= *argp
++)) {
78 switch (smatch (++cp
, switches
)) {
80 ambigsw (cp
, switches
);
87 snprintf (buf
, sizeof(buf
),
88 "%s [switches] [switches for postproc] address ...",
90 print_help (buf
, switches
, 1);
93 print_version(invo_name
);
97 if (!(form
= *argp
++) || *form
== '-')
98 die("missing argument to %s", argp
[-2]);
102 addrs
= addrs
? add (cp
, add (", ", addrs
)) : mh_xstrdup(cp
);
106 die("usage: %s [switches] [switches for postproc] address ...",
109 umask (~m_gmprot ());
111 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
112 die("unable to create temporary file in %s", get_temp_dir());
114 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
116 cpydata (fileno (stdin
), fileno (fp
), "message", tmpfil
);
117 fseek (fp
, 0L, SEEK_SET
);
119 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
120 die("unable to create temporary file in %s", get_temp_dir());
122 strncpy (drft
, tfile
, sizeof(tmpfil
));
124 rcvdistout (fp
, form
, addrs
);
127 if (distout (drft
, tmpfil
, backup
) == NOTOK
)
130 vec
[vecp
++] = "-dist";
131 if ((cp
= context_find ("mhlproc"))) {
132 vec
[vecp
++] = "-mhlproc";
141 adios("fork", "failed:");
144 execvp (program
, vec
);
145 fprintf (stderr
, "unable to exec ");
150 done (pidXwait(child_id
, postproc
));
153 return 0; /* dead code to satisfy the compiler */
156 /* very similar to routine in replsbr.c */
158 static int outputlinelen
= OUTPUTLINELEN
;
160 static struct format
*fmt
;
164 static char *addrcomps
[] = {
182 rcvdistout (FILE *inb
, char *form
, char *addrs
)
184 int char_read
= 0, format_len
, i
, state
;
186 char *cp
, name
[NAMESZ
], tmpbuf
[NMH_BUFSIZ
];
190 m_getfld_state_t gstate
;
192 if (!(out
= fopen (drft
, "w")))
193 adios (drft
, "unable to create");
195 /* get new format string */
196 cp
= new_fs (form
? form
: rcvdistcomps
, NULL
, NULL
);
197 format_len
= strlen (cp
);
198 fmt_compile (cp
, &fmt
, 1);
200 for (ap
= addrcomps
; *ap
; ap
++) {
201 cptr
= fmt_findcomp (*ap
);
203 cptr
->c_type
|= CT_ADDR
;
206 cptr
= fmt_findcomp ("addresses");
208 cptr
->c_text
= addrs
;
210 gstate
= m_getfld_state_init(inb
);
212 int msg_count
= sizeof tmpbuf
;
213 switch (state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
)) {
216 i
= fmt_addcomptext(name
, tmpbuf
);
218 char_read
+= msg_count
;
219 while (state
== FLDPLUS
) {
220 msg_count
= sizeof tmpbuf
;
221 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
222 fmt_appendcomp(i
, name
, tmpbuf
);
223 char_read
+= msg_count
;
227 while (state
== FLDPLUS
) {
228 msg_count
= sizeof tmpbuf
;
229 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
240 die("m_getfld2() returned %d", state
);
244 m_getfld_state_destroy (&gstate
);
246 i
= format_len
+ char_read
+ 256;
247 scanl
= charstring_create (i
+ 2);
248 dat
[0] = dat
[1] = dat
[2] = dat
[4] = 0;
249 dat
[3] = outputlinelen
;
250 fmt_scan (fmt
, scanl
, i
, dat
, NULL
);
251 fputs (charstring_buffer (scanl
), out
);
254 adios (drft
, "error writing");
257 charstring_free (scanl
);
263 unlink_done (int status
)
266 (void) m_unlink (backup
);
268 (void) m_unlink (drft
);
270 (void) m_unlink (tmpfil
);
272 exit (status
? RCV_MBX
: RCV_MOK
);