Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / uip / RCS / rcvdist.c,v
1 head    1.10;
2 access;
3 symbols;
4 locks; strict;
5 comment @ * @;
6
7
8 1.10
9 date    92.12.15.00.20.22;      author jromine; state Exp;
10 branches;
11 next    1.9;
12
13 1.9
14 date    92.11.04.00.56.39;      author jromine; state Exp;
15 branches;
16 next    1.8;
17
18 1.8
19 date    92.02.05.07.26.30;      author jromine; state Exp;
20 branches;
21 next    1.7;
22
23 1.7
24 date    92.01.31.22.24.21;      author jromine; state Exp;
25 branches;
26 next    1.6;
27
28 1.6
29 date    90.11.05.13.50.14;      author mh;      state Exp;
30 branches;
31 next    1.5;
32
33 1.5
34 date    90.11.05.11.38.50;      author mh;      state Exp;
35 branches;
36 next    1.4;
37
38 1.4
39 date    90.04.05.14.59.35;      author sources; state Exp;
40 branches;
41 next    1.3;
42
43 1.3
44 date    90.02.06.13.28.19;      author sources; state Exp;
45 branches;
46 next    1.2;
47
48 1.2
49 date    90.01.30.10.42.40;      author sources; state Exp;
50 branches;
51 next    1.1;
52
53 1.1
54 date    90.01.30.10.41.00;      author sources; state Exp;
55 branches;
56 next    ;
57
58
59 desc
60 @@
61
62
63 1.10
64 log
65 @endif sugar
66 @
67 text
68 @/* rcvdist.c - a rcvmail program to distribute messages */
69 #ifndef lint
70 static char ident[] = "@@(#)$Id: rcvdist.c,v 1.9 1992/11/04 00:56:39 jromine Exp jromine $";
71 #endif  /* lint */
72
73 #include "../h/mh.h"
74 #include "../h/formatsbr.h"
75 #include "../h/rcvmail.h"
76 #include "../zotnet/tws.h"
77 #include <stdio.h>
78 #ifdef LOCALE
79 #include        <locale.h>
80 #endif
81
82 /* \f */
83
84 static struct swit switches[] = {
85 #define FORMSW  0
86     "form formfile",  4,
87
88 #define HELPSW  1
89     "help", 4,
90
91     NULL, 0
92 };
93
94 /* \f */
95
96 static char backup[BUFSIZ] = "";
97 static char drft[BUFSIZ] = "";
98 static char tmpfil[BUFSIZ] = "";
99
100 static rcvdistout();
101 /* \f */
102
103 /* ARGSUSED */
104
105 main (argc, argv)
106 int     argc;
107 char  **argv;
108 {
109     int     i,
110             child_id,
111             vecp = 1;
112     char   *addrs = NULL,
113            *cp,
114            *form = NULL,
115             buf[100],
116           **ap,
117           **argp,
118            *arguments[MAXARGS],
119            *vec[MAXARGS];
120     register    FILE * fp;
121
122 #ifdef LOCALE
123         setlocale(LC_ALL, "");
124 #endif
125     invo_name = r1bindex (argv[0], '/');
126     mts_init (invo_name);
127     if ((cp = m_find (invo_name)) != NULL) {
128         ap = brkstring (cp = getcpy (cp), " ", "\n");
129         ap = copyip (ap, arguments);
130     }
131     else
132         ap = arguments;
133     (void) copyip (argv + 1, ap);
134     argp = arguments;
135
136 /* \f */
137
138     while (cp = *argp++) {
139         if (*cp == '-')
140             switch (smatch (++cp, switches)) {
141                 case AMBIGSW: 
142                     ambigsw (cp, switches);
143                     done (1);
144                 case UNKWNSW: 
145                     vec[vecp++] = --cp;
146                     continue;
147                 case HELPSW: 
148                     (void) sprintf (buf,
149                             "%s [switches] [switches for postproc] address ...",
150                             invo_name);
151                     help (buf, switches);
152                     done (1);
153
154                 case FORMSW: 
155                     if (!(form = *argp++) || *form == '-')
156                         adios (NULLCP, "missing argument to %s", argp[-2]);
157                     continue;
158             }
159         addrs = addrs ? add (cp, add (", ", addrs)) : getcpy (cp);
160     }
161
162 /* \f */
163
164     if (addrs == NULL)
165         adios (NULLCP, "usage: %s [switches] [switches for postproc] address ...",
166             invo_name);
167
168     (void) umask (~m_gmprot ());
169     (void) strcpy (tmpfil, m_tmpfil (invo_name));
170     if ((fp = fopen (tmpfil, "w+")) == NULL)
171         adios (tmpfil, "unable to create");
172     (void) cpydata (fileno (stdin), fileno (fp), "message", tmpfil);
173     (void) fseek (fp, 0L, 0);
174     (void) strcpy (drft, m_tmpfil (invo_name));
175     rcvdistout (fp, form, addrs);
176     (void) fclose (fp);
177
178     if (distout (drft, tmpfil, backup) == NOTOK)
179         done (1);
180
181     vec[0] = r1bindex (postproc, '/');
182     vec[vecp++] = "-dist";
183     vec[vecp++] = drft;
184     vec[vecp] = NULL;
185
186     for (i = 0; (child_id = fork ()) == NOTOK && i < 5; i++)
187         sleep (5);
188     switch (child_id) {
189         case NOTOK: 
190             admonish (NULLCP, "unable to fork");/* fall */
191         case OK: 
192             execvp (postproc, vec);
193             fprintf (stderr, "unable to exec ");
194             perror (postproc);
195             _exit (1);
196
197         default: 
198             done (pidXwait (child_id, postproc));
199     }
200 /* NOTREACHED */
201 }
202
203 /* \f */
204
205 /* very similar to routine in replsbr.c */
206
207 #define SBUFSIZ 256
208
209 static int outputlinelen = OUTPUTLINELEN;
210
211 static struct format *fmt;
212
213 static int ncomps = 0;
214 static char **compbuffers = 0;
215 static struct comp **used_buf = 0;
216
217 static int dat[5];
218
219 static char *addrcomps[] = {
220     "from",
221     "sender",
222     "reply-to",
223     "to",
224     "cc",
225     "bcc",
226     "resent-from",
227     "resent-sender",
228     "resent-reply-to",
229     "resent-to",
230     "resent-cc",
231     "resent-bcc",
232     NULL
233 };
234
235
236 static  rcvdistout (inb, form, addrs)
237 register FILE *inb;
238 char   *form,
239        *addrs;
240 {
241     register int    char_read = 0,
242                     format_len,
243                     i,
244                     state;
245     register char  *tmpbuf,
246                   **nxtbuf,
247                   **ap;
248     char   *cp,
249            *scanl,
250             name[NAMESZ];
251     register struct comp   *cptr,
252                           **savecomp;
253     FILE   *out;
254
255     if ((out = fopen (drft, "w")) == NULL)
256         adios (drft, "unable to create");
257     
258     cp = new_fs (form ? form : rcvdistcomps, NULLCP, NULLCP);
259     format_len = strlen (cp);
260     ncomps = fmt_compile (cp, &fmt) + 1;
261     if ((nxtbuf = compbuffers = (char **)
262             calloc ((unsigned) ncomps, sizeof (char *)))
263             == (char **)NULL)
264         adios (NULLCP, "unable to allocate component buffers");
265     if ((savecomp = used_buf = (struct comp **)
266             calloc ((unsigned) (ncomps + 1), sizeof (struct comp *)))
267             == (struct comp **) NULL)
268         adios (NULLCP, "unable to allocate component buffer stack");
269     savecomp += ncomps + 1;
270     *--savecomp = 0;
271
272     for (i = ncomps; i--;)
273         if ((*nxtbuf++ = malloc (SBUFSIZ)) == NULL)
274             adios (NULLCP, "unable to allocate component buffer");
275     nxtbuf = compbuffers;
276     tmpbuf = *nxtbuf++;
277
278     for (ap = addrcomps; *ap; ap++) {
279         FINDCOMP (cptr, *ap);
280         if (cptr)
281             cptr -> c_type |= CT_ADDR;
282     }
283
284     FINDCOMP (cptr, "addresses");
285     if (cptr)
286         cptr -> c_text = addrs;
287
288     for (state = FLD;;) {
289         switch (state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb)) {
290             case FLD: 
291             case FLDPLUS: 
292                 if (cptr = wantcomp[CHASH (name)])
293                     do {
294                         if (uleq (name, cptr -> c_name)) {
295                             char_read += msg_count;
296                             if (!cptr -> c_text) {
297                                 cptr -> c_text = tmpbuf;
298                                 *--savecomp = cptr;
299                                 tmpbuf = *nxtbuf++;
300                             }
301                             else {
302                                 i = strlen (cp = cptr -> c_text) - 1;
303                                 if (cp[i] == '\n')
304                                     if (cptr -> c_type & CT_ADDR) {
305                                         cp[i] = 0;
306                                         cp = add (",\n\t", cp);
307                                     }
308                                     else
309                                         cp = add ("\t", cp);
310                                 cptr -> c_text = add (tmpbuf, cp);
311                             }
312                             while (state == FLDPLUS) {
313                                 state = m_getfld (state, name, tmpbuf,
314                                                   SBUFSIZ, inb);
315                                 cptr -> c_text = add (tmpbuf, cptr -> c_text);
316                                 char_read += msg_count;
317                             }
318                             break;
319                         }
320                     } while (cptr = cptr -> c_next);
321
322                 while (state == FLDPLUS)
323                     state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
324                 break;
325
326             case LENERR: 
327             case FMTERR: 
328             case BODY: 
329             case FILEEOF: 
330                 goto finished;
331
332             default: 
333                 adios (NULLCP, "m_getfld() returned %d", state);
334         }
335     }
336 finished: ;
337
338     i = format_len + char_read + 256;
339     scanl = malloc ((unsigned) i + 2);
340     dat[0] = dat[1] = dat[2] = dat[4] = 0;
341     dat[3] = outputlinelen;
342     (void) fmtscan (fmt, scanl, i, dat);
343     fputs (scanl, out);
344
345     if (ferror (out))
346         adios (drft, "error writing");
347     (void) fclose (out);
348
349     free (scanl);
350     for (nxtbuf = compbuffers, i = ncomps; cptr = *savecomp++; nxtbuf++, i--)
351         free (cptr -> c_text);
352     while (i-- > 0)
353         free (*nxtbuf++);
354     free ((char *) compbuffers);
355     free ((char *) used_buf);
356 }
357
358 /* \f */
359
360 void done (status)
361 register int     status;
362 {
363     if (backup[0])
364         (void) unlink (backup);
365     if (drft[0])
366         (void) unlink (drft);
367     if (tmpfil[0])
368         (void) unlink (tmpfil);
369
370     exit (status ? RCV_MBX : RCV_MOK);
371 }
372 @
373
374
375 1.9
376 log
377 @LOCALE
378 @
379 text
380 @d3 2
381 a4 2
382 static char ident[] = "@@(#)$Id: rcvdist.c,v 1.8 1992/02/05 07:26:30 jromine Exp jromine $";
383 #endif  lint
384 @
385
386
387 1.8
388 log
389 @put unseen sequence in mh-format
390 @
391 text
392 @d3 1
393 a3 1
394 static char ident[] = "@@(#)$Id: rcvdist.c,v 1.7 1992/01/31 22:24:21 jromine Exp jromine $";
395 d11 3
396 d55 3
397 @
398
399
400 1.7
401 log
402 @kerberos
403 @
404 text
405 @d3 1
406 a3 1
407 static char ident[] = "@@(#)$Id: rcvdist.c,v 1.6 1990/11/05 13:50:14 mh Exp jromine $";
408 d144 1
409 a144 1
410 static int dat[4];
411 d267 1
412 a267 1
413     dat[0] = dat[1] = dat[2] = 0;
414 @
415
416
417 1.6
418 log
419 @typo
420 @
421 text
422 @d3 1
423 a3 1
424 static char ident[] = "@@(#)$Id: rcvdist.c,v 1.5 90/11/05 11:38:50 mh Exp Locker: mh $";
425 d21 1
426 a21 1
427     NULL, NULL
428 d232 1
429 a232 1
430                                         cp[i] = NULL;
431 @
432
433
434 1.5
435 log
436 @fix freeing free buf problem
437 @
438 text
439 @d3 1
440 a3 1
441 static char ident[] = "@@(#)$Id: rcvdist.c,v 1.4 90/04/05 14:59:35 sources Exp Locker: mh $";
442 d282 1
443 a282 1
444     free ((char *) used_buf_fp);
445 @
446
447
448 1.4
449 log
450 @add ID
451 @
452 text
453 @d3 1
454 a3 1
455 static char ident[] = "@@(#)$Id:$";
456 a177 1
457     struct comp **used_buf_fp;
458 d188 3
459 a190 3
460     nxtbuf = compbuffers = (char **) calloc ((unsigned) ncomps,
461             sizeof (char *));
462     if (nxtbuf == NULL)
463 d192 3
464 a194 3
465     used_buf = (struct comp **) calloc ((unsigned) (ncomps + 1),
466                                 sizeof (struct comp *));
467     if (used_buf == NULL)
468 d196 3
469 a198 3
470     used_buf_fp = used_buf;
471     used_buf += ncomps + 1;
472     *--used_buf = 0;
473 a201 1
474
475 a202 1
476     savecomp = used_buf;
477 d277 1
478 a277 1
479     while (cptr = *savecomp++)
480 d279 1
481 a279 1
482     for (nxtbuf = compbuffers, i = ncomps; i--; )
483 @
484
485
486 1.3
487 log
488 @ANSI Compilance
489 @
490 text
491 @d2 3
492 @
493
494
495 1.2
496 log
497 @freeing incremented pointer - bug fix.
498 @
499 text
500 @d27 1
501 @
502
503
504 1.1
505 log
506 @Initial revision
507 @
508 text
509 @d174 1
510 d193 1
511 d281 1
512 a281 1
513     free ((char *) used_buf);
514 @