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