]>
diplodocus.org Git - nmh/blob - uip/rcvdist.c
3 * rcvdist.c -- asynchronously redistribute messages
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 #include <h/fmt_scan.h>
12 #include <h/rcvmail.h>
17 #define RCVDIST_SWITCHES \
18 X("form formfile", 4, FORMSW) \
19 X("version", 0, VERSIONSW) \
20 X("help", 0, HELPSW) \
22 #define X(sw, minchars, id) id,
23 DEFINE_SWITCH_ENUM(RCVDIST
);
26 #define X(sw, minchars, id) { sw, minchars, id },
27 DEFINE_SWITCH_ARRAY(RCVDIST
, switches
);
30 static char backup
[BUFSIZ
] = "";
31 static char drft
[BUFSIZ
] = "";
32 static char tmpfil
[BUFSIZ
] = "";
37 static void rcvdistout (FILE *, char *, char *);
38 static void unlink_done (int) NORETURN
;
42 main (int argc
, char **argv
)
46 char *addrs
= NULL
, *cp
, *form
= NULL
, buf
[BUFSIZ
], *program
;
47 char **argp
, **arguments
, **vec
;
51 if (nmh_init(argv
[0], 2)) { return 1; }
56 * Configure this now, since any unknown switches to rcvdist get
60 vec
= argsplit(postproc
, &program
, &vecp
);
63 arguments
= getarguments (invo_name
, argc
, argv
, 1);
66 while ((cp
= *argp
++)) {
68 switch (smatch (++cp
, switches
)) {
70 ambigsw (cp
, switches
);
77 snprintf (buf
, sizeof(buf
),
78 "%s [switches] [switches for postproc] address ...",
80 print_help (buf
, switches
, 1);
83 print_version(invo_name
);
87 if (!(form
= *argp
++) || *form
== '-')
88 adios (NULL
, "missing argument to %s", argp
[-2]);
92 addrs
= addrs
? add (cp
, add (", ", addrs
)) : mh_xstrdup(cp
);
96 adios (NULL
, "usage: %s [switches] [switches for postproc] address ...",
101 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
102 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
104 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
106 cpydata (fileno (stdin
), fileno (fp
), "message", tmpfil
);
107 fseek (fp
, 0L, SEEK_SET
);
109 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
110 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
112 strncpy (drft
, tfile
, sizeof(tmpfil
));
114 rcvdistout (fp
, form
, addrs
);
117 if (distout (drft
, tmpfil
, backup
) == NOTOK
)
120 vec
[vecp
++] = "-dist";
121 if ((cp
= context_find ("mhlproc"))) {
122 vec
[vecp
++] = "-mhlproc";
128 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
132 admonish (NULL
, "unable to fork");
135 execvp (program
, vec
);
136 fprintf (stderr
, "unable to exec ");
141 done (pidXwait(child_id
, postproc
));
144 return 0; /* dead code to satisfy the compiler */
147 /* very similar to routine in replsbr.c */
151 static int outputlinelen
= OUTPUTLINELEN
;
153 static struct format
*fmt
;
157 static char *addrcomps
[] = {
175 rcvdistout (FILE *inb
, char *form
, char *addrs
)
177 int char_read
= 0, format_len
, i
, state
;
179 char *cp
, name
[NAMESZ
], tmpbuf
[SBUFSIZ
];
183 m_getfld_state_t gstate
= 0;
185 if (!(out
= fopen (drft
, "w")))
186 adios (drft
, "unable to create");
188 /* get new format string */
189 cp
= new_fs (form
? form
: rcvdistcomps
, NULL
, NULL
);
190 format_len
= strlen (cp
);
191 fmt_compile (cp
, &fmt
, 1);
193 for (ap
= addrcomps
; *ap
; ap
++) {
194 cptr
= fmt_findcomp (*ap
);
196 cptr
->c_type
|= CT_ADDR
;
199 cptr
= fmt_findcomp ("addresses");
201 cptr
->c_text
= addrs
;
204 int msg_count
= SBUFSIZ
;
205 switch (state
= m_getfld (&gstate
, name
, tmpbuf
, &msg_count
, inb
)) {
208 i
= fmt_addcomptext(name
, tmpbuf
);
210 char_read
+= msg_count
;
211 while (state
== FLDPLUS
) {
213 state
= m_getfld (&gstate
, name
, tmpbuf
, &msg_count
, inb
);
214 fmt_appendcomp(i
, name
, tmpbuf
);
215 char_read
+= msg_count
;
219 while (state
== FLDPLUS
) {
221 state
= m_getfld (&gstate
, name
, tmpbuf
, &msg_count
, inb
);
232 adios (NULL
, "m_getfld() returned %d", state
);
236 m_getfld_state_destroy (&gstate
);
238 i
= format_len
+ char_read
+ 256;
239 scanl
= charstring_create (i
+ 2);
240 dat
[0] = dat
[1] = dat
[2] = dat
[4] = 0;
241 dat
[3] = outputlinelen
;
242 fmt_scan (fmt
, scanl
, i
, dat
, NULL
);
243 fputs (charstring_buffer (scanl
), out
);
246 adios (drft
, "error writing");
249 charstring_free (scanl
);
255 unlink_done (int status
)
258 (void) m_unlink (backup
);
260 (void) m_unlink (drft
);
262 (void) m_unlink (tmpfil
);
264 exit (status
? RCV_MBX
: RCV_MOK
);