]>
diplodocus.org Git - nmh/blob - uip/rcvdist.c
4f7a83f7b30635993d17e9eadc97ea900364891e
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
;
54 setlocale(LC_ALL
, "");
56 invo_name
= r1bindex (argv
[0], '/');
58 /* read user profile/context */
62 * Configure this now, since any unknown switches to rcvdist get
66 vec
= argsplit(postproc
, &program
, &vecp
);
69 arguments
= getarguments (invo_name
, argc
, argv
, 1);
72 while ((cp
= *argp
++)) {
74 switch (smatch (++cp
, switches
)) {
76 ambigsw (cp
, switches
);
83 snprintf (buf
, sizeof(buf
),
84 "%s [switches] [switches for postproc] address ...",
86 print_help (buf
, switches
, 1);
89 print_version(invo_name
);
93 if (!(form
= *argp
++) || *form
== '-')
94 adios (NULL
, "missing argument to %s", argp
[-2]);
98 addrs
= addrs
? add (cp
, add (", ", addrs
)) : getcpy (cp
);
102 adios (NULL
, "usage: %s [switches] [switches for postproc] address ...",
105 umask (~m_gmprot ());
107 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
108 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
110 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
112 cpydata (fileno (stdin
), fileno (fp
), "message", tmpfil
);
113 fseek (fp
, 0L, SEEK_SET
);
115 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
116 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
118 strncpy (drft
, tfile
, sizeof(tmpfil
));
120 rcvdistout (fp
, form
, addrs
);
123 if (distout (drft
, tmpfil
, backup
) == NOTOK
)
126 vec
[vecp
++] = "-dist";
128 if ((cp
= context_find ("mhlproc"))) {
129 vec
[vecp
++] = "-mhlproc";
134 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
138 admonish (NULL
, "unable to fork");/* fall */
140 execvp (program
, vec
);
141 fprintf (stderr
, "unable to exec ");
146 done (pidXwait(child_id
, postproc
));
149 return 0; /* dead code to satisfy the compiler */
152 /* very similar to routine in replsbr.c */
156 static int outputlinelen
= OUTPUTLINELEN
;
158 static struct format
*fmt
;
162 static char *addrcomps
[] = {
180 rcvdistout (FILE *inb
, char *form
, char *addrs
)
182 register int char_read
= 0, format_len
, i
, state
;
184 char *cp
, *scanl
, name
[NAMESZ
], tmpbuf
[SBUFSIZ
];
185 register struct comp
*cptr
;
187 m_getfld_state_t gstate
= 0;
189 if (!(out
= fopen (drft
, "w")))
190 adios (drft
, "unable to create");
192 /* get new format string */
193 cp
= new_fs (form
? form
: rcvdistcomps
, NULL
, NULL
);
194 format_len
= strlen (cp
);
195 fmt_compile (cp
, &fmt
, 1);
197 for (ap
= addrcomps
; *ap
; ap
++) {
198 cptr
= fmt_findcomp (*ap
);
200 cptr
->c_type
|= CT_ADDR
;
203 cptr
= fmt_findcomp ("addresses");
205 cptr
->c_text
= addrs
;
208 int msg_count
= SBUFSIZ
;
209 switch (state
= m_getfld (&gstate
, name
, tmpbuf
, &msg_count
, inb
)) {
212 i
= fmt_addcomptext(name
, tmpbuf
);
214 char_read
+= msg_count
;
215 while (state
== FLDPLUS
) {
217 state
= m_getfld (&gstate
, name
, tmpbuf
, &msg_count
, inb
);
218 fmt_appendcomp(i
, name
, tmpbuf
);
219 char_read
+= msg_count
;
223 while (state
== FLDPLUS
) {
225 state
= m_getfld (&gstate
, name
, tmpbuf
, &msg_count
, inb
);
236 adios (NULL
, "m_getfld() returned %d", state
);
240 m_getfld_state_destroy (&gstate
);
242 i
= format_len
+ char_read
+ 256;
243 scanl
= mh_xmalloc ((size_t) i
+ 2);
244 dat
[0] = dat
[1] = dat
[2] = dat
[4] = 0;
245 dat
[3] = outputlinelen
;
246 fmt_scan (fmt
, scanl
, i
+ 1, i
, dat
, NULL
);
250 adios (drft
, "error writing");
259 unlink_done (int status
)
268 exit (status
? RCV_MBX
: RCV_MOK
);