]>
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>
14 #include "sbr/m_mktemp.h"
16 #define RCVDIST_SWITCHES \
17 X("form formfile", 4, FORMSW) \
18 X("version", 0, VERSIONSW) \
19 X("help", 0, HELPSW) \
21 #define X(sw, minchars, id) id,
22 DEFINE_SWITCH_ENUM(RCVDIST
);
25 #define X(sw, minchars, id) { sw, minchars, id },
26 DEFINE_SWITCH_ARRAY(RCVDIST
, switches
);
29 static char backup
[BUFSIZ
] = "";
30 static char drft
[BUFSIZ
] = "";
31 static char tmpfil
[BUFSIZ
] = "";
36 static void rcvdistout (FILE *, char *, char *);
37 static void unlink_done (int) NORETURN
;
41 main (int argc
, char **argv
)
45 char *addrs
= NULL
, *cp
, *form
= NULL
, buf
[BUFSIZ
], *program
;
46 char **argp
, **arguments
, **vec
;
50 if (nmh_init(argv
[0], 2)) { return 1; }
55 * Configure this now, since any unknown switches to rcvdist get
59 vec
= argsplit(postproc
, &program
, &vecp
);
62 arguments
= getarguments (invo_name
, argc
, argv
, 1);
65 while ((cp
= *argp
++)) {
67 switch (smatch (++cp
, switches
)) {
69 ambigsw (cp
, switches
);
76 snprintf (buf
, sizeof(buf
),
77 "%s [switches] [switches for postproc] address ...",
79 print_help (buf
, switches
, 1);
82 print_version(invo_name
);
86 if (!(form
= *argp
++) || *form
== '-')
87 adios (NULL
, "missing argument to %s", argp
[-2]);
91 addrs
= addrs
? add (cp
, add (", ", addrs
)) : mh_xstrdup(cp
);
95 adios (NULL
, "usage: %s [switches] [switches for postproc] address ...",
100 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
101 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
103 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
105 cpydata (fileno (stdin
), fileno (fp
), "message", tmpfil
);
106 fseek (fp
, 0L, SEEK_SET
);
108 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
109 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
111 strncpy (drft
, tfile
, sizeof(tmpfil
));
113 rcvdistout (fp
, form
, addrs
);
116 if (distout (drft
, tmpfil
, backup
) == NOTOK
)
119 vec
[vecp
++] = "-dist";
120 if ((cp
= context_find ("mhlproc"))) {
121 vec
[vecp
++] = "-mhlproc";
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
;
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
;
199 gstate
= m_getfld_state_init(inb
);
201 int msg_count
= sizeof tmpbuf
;
202 switch (state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
)) {
205 i
= fmt_addcomptext(name
, tmpbuf
);
207 char_read
+= msg_count
;
208 while (state
== FLDPLUS
) {
209 msg_count
= sizeof tmpbuf
;
210 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
211 fmt_appendcomp(i
, name
, tmpbuf
);
212 char_read
+= msg_count
;
216 while (state
== FLDPLUS
) {
217 msg_count
= sizeof tmpbuf
;
218 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
229 adios (NULL
, "m_getfld2() returned %d", state
);
233 m_getfld_state_destroy (&gstate
);
235 i
= format_len
+ char_read
+ 256;
236 scanl
= charstring_create (i
+ 2);
237 dat
[0] = dat
[1] = dat
[2] = dat
[4] = 0;
238 dat
[3] = outputlinelen
;
239 fmt_scan (fmt
, scanl
, i
, dat
, NULL
);
240 fputs (charstring_buffer (scanl
), out
);
243 adios (drft
, "error writing");
246 charstring_free (scanl
);
252 unlink_done (int status
)
255 (void) m_unlink (backup
);
257 (void) m_unlink (drft
);
259 (void) m_unlink (tmpfil
);
261 exit (status
? RCV_MBX
: RCV_MOK
);