]>
diplodocus.org Git - nmh/blob - uip/rcvdist.c
3 * rcvdist.c -- asynchronously redistribute messages
9 #include <h/fmt_scan.h>
10 #include <h/rcvmail.h>
11 #include <zotnet/tws/tws.h>
12 #include <zotnet/mts/mts.h>
14 static struct swit switches
[] = {
16 { "form formfile", 4 },
24 static char backup
[BUFSIZ
] = "";
25 static char drft
[BUFSIZ
] = "";
26 static char tmpfil
[BUFSIZ
] = "";
31 static void rcvdistout (FILE *, char *, char *);
36 main (int argc
, char **argv
)
40 char *addrs
= NULL
, *cp
, *form
= NULL
, buf
[BUFSIZ
];
41 char **argp
, **arguments
, *vec
[MAXARGS
];
45 setlocale(LC_ALL
, "");
47 invo_name
= r1bindex (argv
[0], '/');
49 /* read user profile/context */
53 arguments
= getarguments (invo_name
, argc
, argv
, 1);
56 while ((cp
= *argp
++)) {
58 switch (smatch (++cp
, switches
)) {
60 ambigsw (cp
, switches
);
67 snprintf (buf
, sizeof(buf
),
68 "%s [switches] [switches for postproc] address ...",
70 print_help (buf
, switches
, 1);
73 print_version(invo_name
);
77 if (!(form
= *argp
++) || *form
== '-')
78 adios (NULL
, "missing argument to %s", argp
[-2]);
82 addrs
= addrs
? add (cp
, add (", ", addrs
)) : getcpy (cp
);
86 adios (NULL
, "usage: %s [switches] [switches for postproc] address ...",
90 strncpy (tmpfil
, m_tmpfil (invo_name
), sizeof(tmpfil
));
91 if ((fp
= fopen (tmpfil
, "w+")) == NULL
)
92 adios (tmpfil
, "unable to create");
93 cpydata (fileno (stdin
), fileno (fp
), "message", tmpfil
);
94 fseek (fp
, 0L, SEEK_SET
);
95 strncpy (drft
, m_tmpfil (invo_name
), sizeof(drft
));
96 rcvdistout (fp
, form
, addrs
);
99 if (distout (drft
, tmpfil
, backup
) == NOTOK
)
102 vec
[0] = r1bindex (postproc
, '/');
103 vec
[vecp
++] = "-dist";
107 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
111 admonish (NULL
, "unable to fork");/* fall */
113 execvp (postproc
, vec
);
114 fprintf (stderr
, "unable to exec ");
119 done (pidXwait(child_id
, postproc
));
122 return 0; /* dead code to satisfy the compiler */
125 /* very similar to routine in replsbr.c */
129 static int outputlinelen
= OUTPUTLINELEN
;
131 static struct format
*fmt
;
133 static int ncomps
= 0;
134 static char **compbuffers
= 0;
135 static struct comp
**used_buf
= 0;
139 static char *addrcomps
[] = {
157 rcvdistout (FILE *inb
, char *form
, char *addrs
)
159 register int char_read
= 0, format_len
, i
, state
;
160 register char *tmpbuf
, **nxtbuf
, **ap
;
161 char *cp
, *scanl
, name
[NAMESZ
];
162 register struct comp
*cptr
, **savecomp
;
165 if (!(out
= fopen (drft
, "w")))
166 adios (drft
, "unable to create");
168 /* get new format string */
169 cp
= new_fs (form
? form
: rcvdistcomps
, NULL
, NULL
);
170 format_len
= strlen (cp
);
171 ncomps
= fmt_compile (cp
, &fmt
) + 1;
172 if (!(nxtbuf
= compbuffers
= (char **) calloc ((size_t) ncomps
, sizeof(char *))))
173 adios (NULL
, "unable to allocate component buffers");
174 if (!(savecomp
= used_buf
= (struct comp
**) calloc ((size_t) (ncomps
+ 1), sizeof(struct comp
*))))
175 adios (NULL
, "unable to allocate component buffer stack");
176 savecomp
+= ncomps
+ 1;
179 for (i
= ncomps
; i
--;)
180 if (!(*nxtbuf
++ = malloc (SBUFSIZ
)))
181 adios (NULL
, "unable to allocate component buffer");
182 nxtbuf
= compbuffers
;
185 for (ap
= addrcomps
; *ap
; ap
++) {
186 FINDCOMP (cptr
, *ap
);
188 cptr
->c_type
|= CT_ADDR
;
191 FINDCOMP (cptr
, "addresses");
193 cptr
->c_text
= addrs
;
195 for (state
= FLD
;;) {
196 switch (state
= m_getfld (state
, name
, tmpbuf
, SBUFSIZ
, inb
)) {
199 if ((cptr
= wantcomp
[CHASH (name
)]))
201 if (!strcasecmp (name
, cptr
->c_name
)) {
202 char_read
+= msg_count
;
204 cptr
->c_text
= tmpbuf
;
209 i
= strlen (cp
= cptr
->c_text
) - 1;
211 if (cptr
->c_type
& CT_ADDR
) {
213 cp
= add (",\n\t", cp
);
218 cptr
->c_text
= add (tmpbuf
, cp
);
220 while (state
== FLDPLUS
) {
221 state
= m_getfld (state
, name
, tmpbuf
,
223 cptr
->c_text
= add (tmpbuf
, cptr
->c_text
);
224 char_read
+= msg_count
;
228 } while ((cptr
= cptr
->c_next
));
230 while (state
== FLDPLUS
)
231 state
= m_getfld (state
, name
, tmpbuf
, SBUFSIZ
, inb
);
241 adios (NULL
, "m_getfld() returned %d", state
);
246 i
= format_len
+ char_read
+ 256;
247 scanl
= malloc ((size_t) i
+ 2);
248 dat
[0] = dat
[1] = dat
[2] = dat
[4] = 0;
249 dat
[3] = outputlinelen
;
250 fmt_scan (fmt
, scanl
, i
, dat
);
254 adios (drft
, "error writing");
258 for (nxtbuf
= compbuffers
, i
= ncomps
; (cptr
= *savecomp
++); nxtbuf
++, i
--)
262 free ((char *) compbuffers
);
263 free ((char *) used_buf
);
277 exit (status
? RCV_MBX
: RCV_MOK
);
278 return 1; /* dead code to satisfy the compiler */