]>
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.
10 #include "sbr/m_gmprot.h"
11 #include "sbr/m_getfld.h"
12 #include "sbr/getarguments.h"
13 #include "sbr/smatch.h"
14 #include "sbr/cpydata.h"
15 #include "sbr/context_find.h"
16 #include "sbr/ambigsw.h"
17 #include "sbr/pidstatus.h"
18 #include "sbr/print_version.h"
19 #include "sbr/print_help.h"
20 #include "sbr/arglist.h"
21 #include "sbr/error.h"
22 #include "h/fmt_scan.h"
27 #include "sbr/m_mktemp.h"
29 #define RCVDIST_SWITCHES \
30 X("form formfile", 4, FORMSW) \
31 X("version", 0, VERSIONSW) \
32 X("help", 0, HELPSW) \
34 #define X(sw, minchars, id) id,
35 DEFINE_SWITCH_ENUM(RCVDIST
);
38 #define X(sw, minchars, id) { sw, minchars, id },
39 DEFINE_SWITCH_ARRAY(RCVDIST
, switches
);
42 static char backup
[BUFSIZ
] = "";
43 static char drft
[BUFSIZ
] = "";
44 static char tmpfil
[BUFSIZ
] = "";
49 static void rcvdistout (FILE *, char *, char *);
50 static void unlink_done (int) NORETURN
;
54 main (int argc
, char **argv
)
58 char *addrs
= NULL
, *cp
, *form
= NULL
, buf
[BUFSIZ
], *program
;
59 char **argp
, **arguments
, **vec
;
63 if (nmh_init(argv
[0], true, false)) { return 1; }
65 set_done(unlink_done
);
68 * Configure this now, since any unknown switches to rcvdist get
72 vec
= argsplit(postproc
, &program
, &vecp
);
75 arguments
= getarguments (invo_name
, argc
, argv
, 1);
78 while ((cp
= *argp
++)) {
80 switch (smatch (++cp
, switches
)) {
82 ambigsw (cp
, switches
);
89 snprintf (buf
, sizeof(buf
),
90 "%s [switches] [switches for postproc] address ...",
92 print_help (buf
, switches
, 1);
95 print_version(invo_name
);
99 if (!(form
= *argp
++) || *form
== '-')
100 die("missing argument to %s", argp
[-2]);
104 addrs
= addrs
? add (cp
, add (", ", addrs
)) : mh_xstrdup(cp
);
108 die("usage: %s [switches] [switches for postproc] address ...",
111 umask (~m_gmprot ());
113 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
114 die("unable to create temporary file in %s", get_temp_dir());
116 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
118 cpydata (fileno (stdin
), fileno (fp
), "message", tmpfil
);
119 fseek (fp
, 0L, SEEK_SET
);
121 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
122 die("unable to create temporary file in %s", get_temp_dir());
124 strncpy (drft
, tfile
, sizeof(tmpfil
));
126 rcvdistout (fp
, form
, addrs
);
129 if (distout (drft
, tmpfil
, backup
) == NOTOK
)
132 vec
[vecp
++] = "-dist";
133 if ((cp
= context_find ("mhlproc"))) {
134 vec
[vecp
++] = "-mhlproc";
143 adios("fork", "failed:");
146 execvp (program
, vec
);
147 fprintf (stderr
, "unable to exec ");
152 done (pidXwait(child_id
, postproc
));
155 return 0; /* dead code to satisfy the compiler */
158 /* very similar to routine in replsbr.c */
160 static int outputlinelen
= OUTPUTLINELEN
;
162 static struct format
*fmt
;
166 static char *addrcomps
[] = {
184 rcvdistout (FILE *inb
, char *form
, char *addrs
)
186 int char_read
= 0, format_len
, i
, state
;
188 char *cp
, name
[NAMESZ
], tmpbuf
[NMH_BUFSIZ
];
192 m_getfld_state_t gstate
;
194 if (!(out
= fopen (drft
, "w")))
195 adios (drft
, "unable to create");
197 /* get new format string */
198 cp
= new_fs (form
? form
: rcvdistcomps
, NULL
, NULL
);
199 format_len
= strlen (cp
);
200 fmt_compile (cp
, &fmt
, 1);
202 for (ap
= addrcomps
; *ap
; ap
++) {
203 cptr
= fmt_findcomp (*ap
);
205 cptr
->c_type
|= CT_ADDR
;
208 cptr
= fmt_findcomp ("addresses");
210 cptr
->c_text
= addrs
;
212 gstate
= m_getfld_state_init(inb
);
214 int msg_count
= sizeof tmpbuf
;
215 switch (state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
)) {
218 i
= fmt_addcomptext(name
, tmpbuf
);
220 char_read
+= msg_count
;
221 while (state
== FLDPLUS
) {
222 msg_count
= sizeof tmpbuf
;
223 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
224 fmt_appendcomp(i
, name
, tmpbuf
);
225 char_read
+= msg_count
;
229 while (state
== FLDPLUS
) {
230 msg_count
= sizeof tmpbuf
;
231 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
242 die("m_getfld2() returned %d", state
);
246 m_getfld_state_destroy (&gstate
);
248 i
= format_len
+ char_read
+ 256;
249 scanl
= charstring_create (i
+ 2);
250 dat
[0] = dat
[1] = dat
[2] = dat
[4] = 0;
251 dat
[3] = outputlinelen
;
252 fmt_scan (fmt
, scanl
, i
, dat
, NULL
);
253 fputs (charstring_buffer (scanl
), out
);
256 adios (drft
, "error writing");
259 charstring_free (scanl
);
265 unlink_done (int status
)
268 (void) m_unlink (backup
);
270 (void) m_unlink (drft
);
272 (void) m_unlink (tmpfil
);
274 exit(status
? 1 : 0);