]>
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
];
47 char **argp
, **arguments
, *vec
[MAXARGS
];
54 setlocale(LC_ALL
, "");
56 invo_name
= r1bindex (argv
[0], '/');
58 /* read user profile/context */
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
)) : getcpy (cp
);
95 adios (NULL
, "usage: %s [switches] [switches for postproc] address ...",
100 tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
);
101 if (tfile
== NULL
) adios("rcvdist", "unable to create temporary file");
102 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
104 cpydata (fileno (stdin
), fileno (fp
), "message", tmpfil
);
105 fseek (fp
, 0L, SEEK_SET
);
107 tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
);
108 if (tfile
== NULL
) adios("forw", "unable to create temporary file");
109 strncpy (drft
, tfile
, sizeof(tmpfil
));
111 rcvdistout (fp
, form
, addrs
);
114 if (distout (drft
, tmpfil
, backup
) == NOTOK
)
117 vec
[0] = r1bindex (postproc
, '/');
118 vec
[vecp
++] = "-dist";
120 if ((cp
= context_find ("mhlproc"))) {
121 vec
[vecp
++] = "-mhlproc";
126 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
130 admonish (NULL
, "unable to fork");/* fall */
132 execvp (postproc
, vec
);
133 fprintf (stderr
, "unable to exec ");
138 done (pidXwait(child_id
, postproc
));
141 return 0; /* dead code to satisfy the compiler */
144 /* very similar to routine in replsbr.c */
148 static int outputlinelen
= OUTPUTLINELEN
;
150 static struct format
*fmt
;
154 static char *addrcomps
[] = {
172 rcvdistout (FILE *inb
, char *form
, char *addrs
)
174 register int char_read
= 0, format_len
, i
, state
;
176 char *cp
, *scanl
, name
[NAMESZ
], tmpbuf
[SBUFSIZ
];
177 register struct comp
*cptr
;
180 if (!(out
= fopen (drft
, "w")))
181 adios (drft
, "unable to create");
183 /* get new format string */
184 cp
= new_fs (form
? form
: rcvdistcomps
, NULL
, NULL
);
185 format_len
= strlen (cp
);
186 fmt_compile (cp
, &fmt
, 1);
188 for (ap
= addrcomps
; *ap
; ap
++) {
189 cptr
= fmt_findcomp (*ap
);
191 cptr
->c_type
|= CT_ADDR
;
194 cptr
= fmt_findcomp ("addresses");
196 cptr
->c_text
= addrs
;
198 for (state
= FLD
;;) {
199 switch (state
= m_getfld (state
, name
, tmpbuf
, SBUFSIZ
, inb
)) {
202 i
= fmt_addcomptext(name
, tmpbuf
);
204 char_read
+= msg_count
;
205 while (state
== FLDPLUS
) {
206 state
= m_getfld (state
, name
, tmpbuf
, SBUFSIZ
, inb
);
207 fmt_appendcomp(i
, name
, tmpbuf
);
208 char_read
+= msg_count
;
212 while (state
== FLDPLUS
)
213 state
= m_getfld (state
, name
, tmpbuf
, SBUFSIZ
, inb
);
223 adios (NULL
, "m_getfld() returned %d", state
);
228 i
= format_len
+ char_read
+ 256;
229 scanl
= mh_xmalloc ((size_t) i
+ 2);
230 dat
[0] = dat
[1] = dat
[2] = dat
[4] = 0;
231 dat
[3] = outputlinelen
;
232 fmt_scan (fmt
, scanl
, i
+ 1, i
, dat
);
236 adios (drft
, "error writing");
245 unlink_done (int status
)
254 exit (status
? RCV_MBX
: RCV_MOK
);