chiark / gitweb /
check fd watches; other bugfixes
[inn-innduct.git] / backends / cvtbatch.c
1 /*  $Id: cvtbatch.c 6135 2003-01-19 01:15:40Z rra $
2 **
3 **  Read file list on standard input and spew out batchfiles.
4 */
5
6 #include "config.h"
7 #include "clibrary.h"
8
9 #include "inn/innconf.h"
10 #include "inn/messages.h"
11 #include "inn/qio.h"
12 #include "inn/wire.h"
13 #include "libinn.h"
14 #include "paths.h"
15 #include "storage.h"
16
17
18 int
19 main(int ac, char *av[]) {
20     int         i;
21     QIOSTATE    *qp;
22     char        *line;
23     const char  *text;
24     char        *format;
25     char        *p, *q;
26     const char  *r;
27     bool        Dirty;
28     TOKEN       token;
29     ARTHANDLE   *art;
30     int         len;
31
32     /* First thing, set up our identity. */
33     message_program_name = "cvtbatch";
34     if (!innconf_read(NULL))
35         exit(1);
36
37     /* Parse JCL. */
38     format = xstrdup("nm");
39     while ((i = getopt(ac, av, "w:")) != EOF)
40         switch (i) {
41         default:
42             die("usage error");
43             break;
44         case 'w':
45             for (p = format = optarg; *p; p++) {
46                 switch (*p) {
47                 case FEED_BYTESIZE:
48                 case FEED_FULLNAME:
49                 case FEED_MESSAGEID:
50                 case FEED_NAME:
51                     continue;
52                 }
53                 warn("ignoring %c in -w flag", *p);
54             }
55         }
56     ac -= optind;
57     av += optind;
58     if (ac)
59         die("usage error");
60
61     if (!SMinit())
62         die("cannot initialize storage manager: %s", SMerrorstr);
63
64     /* Loop over all input. */
65     qp = QIOfdopen((int)fileno(stdin));
66     while ((line = QIOread(qp)) != NULL) {
67         for (p = line; *p; p++)
68             if (ISWHITE(*p)) {
69                 *p = '\0';
70                 break;
71             }
72
73         if (!IsToken(line))
74             continue;
75         token = TextToToken(line);
76         if ((art = SMretrieve(token, RETR_HEAD)) == NULL)
77             continue;
78         text = wire_findheader(art->data, art->len, "Message-ID");
79         if (text == NULL) {
80             SMfreearticle(art);
81             continue;
82         }
83         len = art->len;
84         for (r = text; r < art->data + art->len; r++) {
85             if (*r == '\r' || *r == '\n')
86                 break;
87         }
88         if (r == art->data + art->len) {
89             SMfreearticle(art);
90             continue;
91         }
92         q = xmalloc(r - text + 1);
93         memcpy(q, text, r - text);
94         SMfreearticle(art);
95         q[r - text] = '\0';
96
97         /* Write the desired info. */
98         for (Dirty = false, p = format; *p; p++) {
99             switch (*p) {
100             default:
101                 continue;
102             case FEED_BYTESIZE:
103                 if (Dirty)
104                     putchar(' ');
105                 printf("%d", len);
106                 break;
107             case FEED_FULLNAME:
108             case FEED_NAME:
109                 if (Dirty)
110                     putchar(' ');
111                 printf("%s", line);
112                 break;
113             case FEED_MESSAGEID:
114                 if (Dirty)
115                     putchar(' ');
116                 printf("%s", q);
117                 break;
118             }
119             Dirty = true;
120         }
121         free(q);
122         if (Dirty)
123             putchar('\n');
124     }
125
126     exit(0);
127     /* NOTREACHED */
128 }