]>
diplodocus.org Git - nmh/blob - uip/rcvdist.c
f065ad1de40d598ea971d4ee091f7ff43a01d037
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/fmt_new.h"
11 #include "sbr/m_gmprot.h"
12 #include "sbr/m_getfld.h"
13 #include "sbr/getarguments.h"
14 #include "sbr/smatch.h"
15 #include "sbr/cpydata.h"
16 #include "sbr/context_find.h"
17 #include "sbr/ambigsw.h"
18 #include "sbr/pidstatus.h"
19 #include "sbr/print_version.h"
20 #include "sbr/print_help.h"
21 #include "sbr/arglist.h"
22 #include "sbr/error.h"
23 #include "h/fmt_scan.h"
28 #include "sbr/m_mktemp.h"
30 #define RCVDIST_SWITCHES \
31 X("form formfile", 4, FORMSW) \
32 X("version", 0, VERSIONSW) \
33 X("help", 0, HELPSW) \
35 #define X(sw, minchars, id) id,
36 DEFINE_SWITCH_ENUM(RCVDIST
);
39 #define X(sw, minchars, id) { sw, minchars, id },
40 DEFINE_SWITCH_ARRAY(RCVDIST
, switches
);
43 static char backup
[BUFSIZ
] = "";
44 static char drft
[BUFSIZ
] = "";
45 static char tmpfil
[BUFSIZ
] = "";
50 static void rcvdistout (FILE *, char *, char *);
51 static void unlink_done (int) NORETURN
;
55 main (int argc
, char **argv
)
59 char *addrs
= NULL
, *cp
, *form
= NULL
, buf
[BUFSIZ
], *program
;
60 char **argp
, **arguments
, **vec
;
64 if (nmh_init(argv
[0], true, false)) { return 1; }
66 set_done(unlink_done
);
69 * Configure this now, since any unknown switches to rcvdist get
73 vec
= argsplit(postproc
, &program
, &vecp
);
76 arguments
= getarguments (invo_name
, argc
, argv
, 1);
79 while ((cp
= *argp
++)) {
81 switch (smatch (++cp
, switches
)) {
83 ambigsw (cp
, switches
);
90 snprintf (buf
, sizeof(buf
),
91 "%s [switches] [switches for postproc] address ...",
93 print_help (buf
, switches
, 1);
96 print_version(invo_name
);
100 if (!(form
= *argp
++) || *form
== '-')
101 die("missing argument to %s", argp
[-2]);
105 addrs
= addrs
? add (cp
, add (", ", addrs
)) : mh_xstrdup(cp
);
109 die("usage: %s [switches] [switches for postproc] address ...",
112 umask (~m_gmprot ());
114 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
115 die("unable to create temporary file in %s", get_temp_dir());
117 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
119 cpydata (fileno (stdin
), fileno (fp
), "message", tmpfil
);
120 fseek (fp
, 0L, SEEK_SET
);
122 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
123 die("unable to create temporary file in %s", get_temp_dir());
125 strncpy (drft
, tfile
, sizeof(tmpfil
));
127 rcvdistout (fp
, form
, addrs
);
130 if (distout (drft
, tmpfil
, backup
) == NOTOK
)
133 vec
[vecp
++] = "-dist";
134 if ((cp
= context_find ("mhlproc"))) {
135 vec
[vecp
++] = "-mhlproc";
144 adios("fork", "failed:");
147 execvp (program
, vec
);
148 fprintf (stderr
, "unable to exec ");
153 done (pidXwait(child_id
, postproc
));
156 return 0; /* dead code to satisfy the compiler */
159 /* very similar to routine in replsbr.c */
161 static int outputlinelen
= OUTPUTLINELEN
;
163 static struct format
*fmt
;
167 static char *addrcomps
[] = {
185 rcvdistout (FILE *inb
, char *form
, char *addrs
)
187 int char_read
= 0, format_len
, i
, state
;
189 char *cp
, name
[NAMESZ
], tmpbuf
[NMH_BUFSIZ
];
193 m_getfld_state_t gstate
;
195 if (!(out
= fopen (drft
, "w")))
196 adios (drft
, "unable to create");
198 /* get new format string */
199 cp
= new_fs (form
? form
: rcvdistcomps
, NULL
, NULL
);
200 format_len
= strlen (cp
);
201 fmt_compile (cp
, &fmt
, 1);
203 for (ap
= addrcomps
; *ap
; ap
++) {
204 cptr
= fmt_findcomp (*ap
);
206 cptr
->c_type
|= CT_ADDR
;
209 cptr
= fmt_findcomp ("addresses");
211 cptr
->c_text
= addrs
;
213 gstate
= m_getfld_state_init(inb
);
215 int msg_count
= sizeof tmpbuf
;
216 switch (state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
)) {
219 i
= fmt_addcomptext(name
, tmpbuf
);
221 char_read
+= msg_count
;
222 while (state
== FLDPLUS
) {
223 msg_count
= sizeof tmpbuf
;
224 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
225 fmt_appendcomp(i
, name
, tmpbuf
);
226 char_read
+= msg_count
;
230 while (state
== FLDPLUS
) {
231 msg_count
= sizeof tmpbuf
;
232 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
243 die("m_getfld2() returned %d", state
);
247 m_getfld_state_destroy (&gstate
);
249 i
= format_len
+ char_read
+ 256;
250 scanl
= charstring_create (i
+ 2);
251 dat
[0] = dat
[1] = dat
[2] = dat
[4] = 0;
252 dat
[3] = outputlinelen
;
253 fmt_scan (fmt
, scanl
, i
, dat
, NULL
);
254 fputs (charstring_buffer (scanl
), out
);
257 adios (drft
, "error writing");
260 charstring_free (scanl
);
266 unlink_done (int status
)
269 (void) m_unlink (backup
);
271 (void) m_unlink (drft
);
273 (void) m_unlink (tmpfil
);
275 exit(status
? 1 : 0);