]>
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 "sbr/pidstatus.h"
10 #include "sbr/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/arglist.h"
13 #include "sbr/error.h"
14 #include "h/fmt_scan.h"
15 #include "h/rcvmail.h"
20 #include "sbr/m_mktemp.h"
22 #define RCVDIST_SWITCHES \
23 X("form formfile", 4, FORMSW) \
24 X("version", 0, VERSIONSW) \
25 X("help", 0, HELPSW) \
27 #define X(sw, minchars, id) id,
28 DEFINE_SWITCH_ENUM(RCVDIST
);
31 #define X(sw, minchars, id) { sw, minchars, id },
32 DEFINE_SWITCH_ARRAY(RCVDIST
, switches
);
35 static char backup
[BUFSIZ
] = "";
36 static char drft
[BUFSIZ
] = "";
37 static char tmpfil
[BUFSIZ
] = "";
42 static void rcvdistout (FILE *, char *, char *);
43 static void unlink_done (int) NORETURN
;
47 main (int argc
, char **argv
)
51 char *addrs
= NULL
, *cp
, *form
= NULL
, buf
[BUFSIZ
], *program
;
52 char **argp
, **arguments
, **vec
;
56 if (nmh_init(argv
[0], true, false)) { return 1; }
58 set_done(unlink_done
);
61 * Configure this now, since any unknown switches to rcvdist get
65 vec
= argsplit(postproc
, &program
, &vecp
);
68 arguments
= getarguments (invo_name
, argc
, argv
, 1);
71 while ((cp
= *argp
++)) {
73 switch (smatch (++cp
, switches
)) {
75 ambigsw (cp
, switches
);
82 snprintf (buf
, sizeof(buf
),
83 "%s [switches] [switches for postproc] address ...",
85 print_help (buf
, switches
, 1);
88 print_version(invo_name
);
92 if (!(form
= *argp
++) || *form
== '-')
93 die("missing argument to %s", argp
[-2]);
97 addrs
= addrs
? add (cp
, add (", ", addrs
)) : mh_xstrdup(cp
);
101 die("usage: %s [switches] [switches for postproc] address ...",
104 umask (~m_gmprot ());
106 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
107 die("unable to create temporary file in %s", get_temp_dir());
109 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
111 cpydata (fileno (stdin
), fileno (fp
), "message", tmpfil
);
112 fseek (fp
, 0L, SEEK_SET
);
114 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
115 die("unable to create temporary file in %s", get_temp_dir());
117 strncpy (drft
, tfile
, sizeof(tmpfil
));
119 rcvdistout (fp
, form
, addrs
);
122 if (distout (drft
, tmpfil
, backup
) == NOTOK
)
125 vec
[vecp
++] = "-dist";
126 if ((cp
= context_find ("mhlproc"))) {
127 vec
[vecp
++] = "-mhlproc";
136 adios("fork", "failed:");
139 execvp (program
, vec
);
140 fprintf (stderr
, "unable to exec ");
145 done (pidXwait(child_id
, postproc
));
148 return 0; /* dead code to satisfy the compiler */
151 /* very similar to routine in replsbr.c */
153 static int outputlinelen
= OUTPUTLINELEN
;
155 static struct format
*fmt
;
159 static char *addrcomps
[] = {
177 rcvdistout (FILE *inb
, char *form
, char *addrs
)
179 int char_read
= 0, format_len
, i
, state
;
181 char *cp
, name
[NAMESZ
], tmpbuf
[NMH_BUFSIZ
];
185 m_getfld_state_t gstate
;
187 if (!(out
= fopen (drft
, "w")))
188 adios (drft
, "unable to create");
190 /* get new format string */
191 cp
= new_fs (form
? form
: rcvdistcomps
, NULL
, NULL
);
192 format_len
= strlen (cp
);
193 fmt_compile (cp
, &fmt
, 1);
195 for (ap
= addrcomps
; *ap
; ap
++) {
196 cptr
= fmt_findcomp (*ap
);
198 cptr
->c_type
|= CT_ADDR
;
201 cptr
= fmt_findcomp ("addresses");
203 cptr
->c_text
= addrs
;
205 gstate
= m_getfld_state_init(inb
);
207 int msg_count
= sizeof tmpbuf
;
208 switch (state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
)) {
211 i
= fmt_addcomptext(name
, tmpbuf
);
213 char_read
+= msg_count
;
214 while (state
== FLDPLUS
) {
215 msg_count
= sizeof tmpbuf
;
216 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
217 fmt_appendcomp(i
, name
, tmpbuf
);
218 char_read
+= msg_count
;
222 while (state
== FLDPLUS
) {
223 msg_count
= sizeof tmpbuf
;
224 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
235 die("m_getfld2() returned %d", state
);
239 m_getfld_state_destroy (&gstate
);
241 i
= format_len
+ char_read
+ 256;
242 scanl
= charstring_create (i
+ 2);
243 dat
[0] = dat
[1] = dat
[2] = dat
[4] = 0;
244 dat
[3] = outputlinelen
;
245 fmt_scan (fmt
, scanl
, i
, dat
, NULL
);
246 fputs (charstring_buffer (scanl
), out
);
249 adios (drft
, "error writing");
252 charstring_free (scanl
);
258 unlink_done (int status
)
261 (void) m_unlink (backup
);
263 (void) m_unlink (drft
);
265 (void) m_unlink (tmpfil
);
267 exit (status
? RCV_MBX
: RCV_MOK
);