[bug #4302] errno is not always an extern int
[mmh] / uip / rcvdist.c
1
2 /*
3  * rcvdist.c -- asynchronously redistribute messages
4  *
5  * $Id$
6  *
7  * This code is Copyright (c) 2002, by the authors of nmh.  See the
8  * COPYRIGHT file in the root directory of the nmh distribution for
9  * complete copyright information.
10  */
11
12 #include <h/mh.h>
13 #include <h/fmt_scan.h>
14 #include <h/rcvmail.h>
15 #include <h/tws.h>
16 #include <h/mts.h>
17
18 static struct swit switches[] = {
19 #define FORMSW       0
20     { "form formfile",  4 },
21 #define VERSIONSW    1
22     { "version", 0 },
23 #define HELPSW       2
24     { "help", 0 },
25     { NULL, 0 }
26 };
27
28 static char backup[BUFSIZ] = "";
29 static char drft[BUFSIZ] = "";
30 static char tmpfil[BUFSIZ] = "";
31
32 /*
33  * prototypes
34  */
35 static void rcvdistout (FILE *, char *, char *);
36 int done (int);
37
38
39 int
40 main (int argc, char **argv)
41 {
42     pid_t child_id;
43     int i, vecp = 1;
44     char *addrs = NULL, *cp, *form = NULL, buf[BUFSIZ];
45     char **argp, **arguments, *vec[MAXARGS];
46     register FILE *fp;
47
48 #ifdef LOCALE
49     setlocale(LC_ALL, "");
50 #endif
51     invo_name = r1bindex (argv[0], '/');
52
53     /* read user profile/context */
54     context_read();
55
56     mts_init (invo_name);
57     arguments = getarguments (invo_name, argc, argv, 1);
58     argp = arguments;
59
60     while ((cp = *argp++)) {
61         if (*cp == '-') {
62             switch (smatch (++cp, switches)) {
63                 case AMBIGSW: 
64                     ambigsw (cp, switches);
65                     done (1);
66                 case UNKWNSW: 
67                     vec[vecp++] = --cp;
68                     continue;
69
70                 case HELPSW: 
71                     snprintf (buf, sizeof(buf),
72                         "%s [switches] [switches for postproc] address ...",
73                         invo_name);
74                     print_help (buf, switches, 1);
75                     done (1);
76                 case VERSIONSW:
77                     print_version(invo_name);
78                     done (1);
79
80                 case FORMSW: 
81                     if (!(form = *argp++) || *form == '-')
82                         adios (NULL, "missing argument to %s", argp[-2]);
83                     continue;
84             }
85         }
86         addrs = addrs ? add (cp, add (", ", addrs)) : getcpy (cp);
87     }
88
89     if (addrs == NULL)
90         adios (NULL, "usage: %s [switches] [switches for postproc] address ...",
91             invo_name);
92
93     umask (~m_gmprot ());
94     strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
95     if ((fp = fopen (tmpfil, "w+")) == NULL)
96         adios (tmpfil, "unable to create");
97     cpydata (fileno (stdin), fileno (fp), "message", tmpfil);
98     fseek (fp, 0L, SEEK_SET);
99     strncpy (drft, m_tmpfil (invo_name), sizeof(drft));
100     rcvdistout (fp, form, addrs);
101     fclose (fp);
102
103     if (distout (drft, tmpfil, backup) == NOTOK)
104         done (1);
105
106     vec[0] = r1bindex (postproc, '/');
107     vec[vecp++] = "-dist";
108     vec[vecp++] = drft;
109     vec[vecp] = NULL;
110
111     for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
112         sleep (5);
113     switch (child_id) {
114         case NOTOK: 
115             admonish (NULL, "unable to fork");/* fall */
116         case OK: 
117             execvp (postproc, vec);
118             fprintf (stderr, "unable to exec ");
119             perror (postproc);
120             _exit (1);
121
122         default: 
123             done (pidXwait(child_id, postproc));
124     }
125
126     return 0;  /* dead code to satisfy the compiler */
127 }
128
129 /* very similar to routine in replsbr.c */
130
131 #define SBUFSIZ 256
132
133 static int outputlinelen = OUTPUTLINELEN;
134
135 static struct format *fmt;
136
137 static int ncomps = 0;
138 static char **compbuffers = 0;
139 static struct comp **used_buf = 0;
140
141 static int dat[5];
142
143 static char *addrcomps[] = {
144     "from",
145     "sender",
146     "reply-to",
147     "to",
148     "cc",
149     "bcc",
150     "resent-from",
151     "resent-sender",
152     "resent-reply-to",
153     "resent-to",
154     "resent-cc",
155     "resent-bcc",
156     NULL
157 };
158
159
160 static void
161 rcvdistout (FILE *inb, char *form, char *addrs)
162 {
163     register int char_read = 0, format_len, i, state;
164     register char *tmpbuf, **nxtbuf, **ap;
165     char *cp, *scanl, name[NAMESZ];
166     register struct comp *cptr, **savecomp;
167     FILE *out;
168
169     if (!(out = fopen (drft, "w")))
170         adios (drft, "unable to create");
171
172     /* get new format string */
173     cp = new_fs (form ? form : rcvdistcomps, NULL, NULL);
174     format_len = strlen (cp);
175     ncomps = fmt_compile (cp, &fmt) + 1;
176     if (!(nxtbuf = compbuffers = (char **) calloc ((size_t) ncomps, sizeof(char *))))
177         adios (NULL, "unable to allocate component buffers");
178     if (!(savecomp = used_buf = (struct comp **) calloc ((size_t) (ncomps + 1), sizeof(struct comp *))))
179         adios (NULL, "unable to allocate component buffer stack");
180     savecomp += ncomps + 1;
181     *--savecomp = 0;
182
183     for (i = ncomps; i--;)
184         if (!(*nxtbuf++ = malloc (SBUFSIZ)))
185             adios (NULL, "unable to allocate component buffer");
186     nxtbuf = compbuffers;
187     tmpbuf = *nxtbuf++;
188
189     for (ap = addrcomps; *ap; ap++) {
190         FINDCOMP (cptr, *ap);
191         if (cptr)
192             cptr->c_type |= CT_ADDR;
193     }
194
195     FINDCOMP (cptr, "addresses");
196     if (cptr)
197         cptr->c_text = addrs;
198
199     for (state = FLD;;) {
200         switch (state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb)) {
201             case FLD: 
202             case FLDPLUS: 
203                 if ((cptr = wantcomp[CHASH (name)]))
204                     do {
205                         if (!strcasecmp (name, cptr->c_name)) {
206                             char_read += msg_count;
207                             if (!cptr->c_text) {
208                                 cptr->c_text = tmpbuf;
209                                 *--savecomp = cptr;
210                                 tmpbuf = *nxtbuf++;
211                             }
212                             else {
213                                 i = strlen (cp = cptr->c_text) - 1;
214                                 if (cp[i] == '\n') {
215                                     if (cptr->c_type & CT_ADDR) {
216                                         cp[i] = 0;
217                                         cp = add (",\n\t", cp);
218                                     }
219                                     else
220                                         cp = add ("\t", cp);
221                                 }
222                                 cptr->c_text = add (tmpbuf, cp);
223                             }
224                             while (state == FLDPLUS) {
225                                 state = m_getfld (state, name, tmpbuf,
226                                                   SBUFSIZ, inb);
227                                 cptr->c_text = add (tmpbuf, cptr->c_text);
228                                 char_read += msg_count;
229                             }
230                             break;
231                         }
232                     } while ((cptr = cptr->c_next));
233
234                 while (state == FLDPLUS)
235                     state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
236                 break;
237
238             case LENERR: 
239             case FMTERR: 
240             case BODY: 
241             case FILEEOF: 
242                 goto finished;
243
244             default: 
245                 adios (NULL, "m_getfld() returned %d", state);
246         }
247     }
248 finished: ;
249
250     i = format_len + char_read + 256;
251     scanl = malloc ((size_t) i + 2);
252     dat[0] = dat[1] = dat[2] = dat[4] = 0;
253     dat[3] = outputlinelen;
254     fmt_scan (fmt, scanl, i, dat);
255     fputs (scanl, out);
256
257     if (ferror (out))
258         adios (drft, "error writing");
259     fclose (out);
260
261     free (scanl);
262     for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++); nxtbuf++, i--)
263         free (cptr->c_text);
264     while (i-- > 0)
265         free (*nxtbuf++);
266     free ((char *) compbuffers);
267     free ((char *) used_buf);
268 }
269
270
271 int
272 done (int status)
273 {
274     if (backup[0])
275         unlink (backup);
276     if (drft[0])
277         unlink (drft);
278     if (tmpfil[0])
279         unlink (tmpfil);
280
281     exit (status ? RCV_MBX : RCV_MOK);
282     return 1;  /* dead code to satisfy the compiler */
283 }