Commit | Line | Data |
---|---|---|
cc9940c7 JB |
1 | /* |
2 | * b2m - a filter for Babyl -> Unix mail files | |
3 | * | |
4 | * usage: b2m < babyl > mailbox | |
5 | * | |
6 | * I find this useful whenever I have to use a | |
7 | * system which - shock horror! - doesn't run | |
8 | * Gnu emacs. At least now I can read all my | |
9 | * Gnumacs Babyl format mail files! | |
10 | * | |
11 | * it's not much but it's free! | |
12 | * | |
13 | * Ed Wilkinson | |
14 | * E.Wilkinson@massey.ac.nz | |
15 | * Mon Nov 7 15:54:06 PDT 1988 | |
16 | */ | |
17 | ||
e19bdc14 RS |
18 | /* Made conformant to the GNU coding standards January, 1995 |
19 | by Francesco Potorti` <pot@cnuce.cnr.it>. */ | |
20 | ||
cc9940c7 JB |
21 | #include <stdio.h> |
22 | #include <time.h> | |
312752df | 23 | #include <sys/types.h> |
e4b34a85 | 24 | #include <getopt.h> |
c6880c90 RS |
25 | #ifdef MSDOS |
26 | #include <fcntl.h> | |
27 | #endif | |
fbfed6f0 | 28 | |
e19bdc14 RS |
29 | #ifdef HAVE_CONFIG_H |
30 | #include <config.h> | |
31 | /* On some systems, Emacs defines static as nothing for the sake | |
32 | of unexec. We don't want that here since we don't use unexec. */ | |
33 | #undef static | |
34 | #endif | |
fbfed6f0 | 35 | |
1f8c2f55 AS |
36 | #ifdef STDC_HEADERS |
37 | #include <stdlib.h> | |
38 | #endif | |
39 | ||
e19bdc14 RS |
40 | #undef TRUE |
41 | #define TRUE 1 | |
42 | #undef FALSE | |
43 | #define FALSE 0 | |
cc9940c7 | 44 | |
e19bdc14 RS |
45 | /* Exit codes for success and failure. */ |
46 | #ifdef VMS | |
47 | #define GOOD 1 | |
48 | #define BAD 0 | |
49 | #else | |
50 | #define GOOD 0 | |
51 | #define BAD 1 | |
cbc61077 | 52 | #endif |
cc9940c7 | 53 | |
e19bdc14 RS |
54 | #define streq(s,t) (strcmp (s, t) == 0) |
55 | #define strneq(s,t,n) (strncmp (s, t, n) == 0) | |
56 | ||
57 | typedef int logical; | |
58 | ||
59 | /* | |
60 | * A `struct linebuffer' is a structure which holds a line of text. | |
61 | * `readline' reads a line from a stream into a linebuffer and works | |
62 | * regardless of the length of the line. | |
63 | */ | |
64 | struct linebuffer | |
65 | { | |
66 | long size; | |
67 | char *buffer; | |
68 | }; | |
69 | ||
70 | extern char *strtok(); | |
128ba46c | 71 | |
0404b62c | 72 | long *xmalloc (), *xrealloc (); |
e19bdc14 RS |
73 | char *concat (); |
74 | long readline (); | |
75 | void fatal (); | |
76 | ||
77 | /* | |
78 | * xnew -- allocate storage. SYNOPSIS: Type *xnew (int n, Type); | |
79 | */ | |
80 | #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type))) | |
81 | ||
82 | ||
83 | ||
84 | char *progname; | |
cc9940c7 | 85 | |
e4b34a85 KH |
86 | struct option longopts[] = |
87 | { | |
88 | { "help", no_argument, NULL, 'h' }, | |
89 | { "version", no_argument, NULL, 'V' }, | |
90 | { 0 } | |
91 | }; | |
92 | ||
93 | extern int optind; | |
94 | ||
1f8c2f55 | 95 | int |
5d13f393 JB |
96 | main (argc, argv) |
97 | int argc; | |
98 | char **argv; | |
cc9940c7 | 99 | { |
e19bdc14 RS |
100 | logical labels_saved, printing, header; |
101 | time_t ltoday; | |
102 | char *labels, *p, *today; | |
103 | struct linebuffer data; | |
104 | ||
c6880c90 | 105 | #ifdef MSDOS |
e3515d10 | 106 | _fmode = O_BINARY; /* all of files are treated as binary files */ |
18198bb2 RS |
107 | #if __DJGPP__ > 1 |
108 | if (!isatty (fileno (stdout))) | |
109 | setmode (fileno (stdout), O_BINARY); | |
110 | if (!isatty (fileno (stdin))) | |
111 | setmode (fileno (stdin), O_BINARY); | |
112 | #else /* not __DJGPP__ > 1 */ | |
c6880c90 RS |
113 | (stdout)->_flag &= ~_IOTEXT; |
114 | (stdin)->_flag &= ~_IOTEXT; | |
18198bb2 | 115 | #endif /* not __DJGPP__ > 1 */ |
c6880c90 | 116 | #endif |
d4843060 RS |
117 | progname = argv[0]; |
118 | ||
e4b34a85 KH |
119 | while (1) |
120 | { | |
121 | int opt = getopt_long (argc, argv, "hV", longopts, 0); | |
122 | if (opt == EOF) | |
123 | break; | |
124 | ||
125 | switch (opt) | |
126 | { | |
127 | case 'V': | |
128 | printf ("%s (GNU Emacs %s)\n", "b2m", VERSION); | |
129 | puts ("b2m is in the public domain."); | |
130 | exit (GOOD); | |
131 | ||
132 | case 'h': | |
133 | fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname); | |
134 | exit (GOOD); | |
135 | } | |
136 | } | |
137 | ||
138 | if (optind != argc) | |
88d00c7e | 139 | { |
e19bdc14 RS |
140 | fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname); |
141 | exit (GOOD); | |
88d00c7e | 142 | } |
e4b34a85 | 143 | |
e19bdc14 | 144 | labels_saved = printing = header = FALSE; |
e3515d10 RS |
145 | ltoday = time (0); |
146 | today = ctime (<oday); | |
e19bdc14 RS |
147 | data.size = 200; |
148 | data.buffer = xnew (200, char); | |
cc9940c7 | 149 | |
e19bdc14 RS |
150 | if (readline (&data, stdin) == 0 |
151 | || !strneq (data.buffer, "BABYL OPTIONS:", 14)) | |
152 | fatal ("standard input is not a Babyl mailfile."); | |
cc9940c7 | 153 | |
e19bdc14 | 154 | while (readline (&data, stdin) > 0) |
e3515d10 | 155 | { |
e19bdc14 | 156 | if (streq (data.buffer, "*** EOOH ***") && !printing) |
e3515d10 RS |
157 | { |
158 | printing = header = TRUE; | |
d4843060 | 159 | printf ("From \"Babyl to mail by %s\" %s", progname, today); |
e3515d10 RS |
160 | continue; |
161 | } | |
88d00c7e | 162 | |
e19bdc14 | 163 | if (data.buffer[0] == '\037') |
e3515d10 | 164 | { |
e19bdc14 RS |
165 | if (data.buffer[1] == '\0') |
166 | continue; | |
167 | else if (data.buffer[1] == '\f') | |
e3515d10 | 168 | { |
e19bdc14 RS |
169 | /* Save labels. */ |
170 | readline (&data, stdin); | |
171 | p = strtok (data.buffer, " ,\r\n\t"); | |
172 | labels = "X-Babyl-Labels: "; | |
cc9940c7 | 173 | |
e19bdc14 RS |
174 | while (p = strtok (NULL, " ,\r\n\t")) |
175 | labels = concat (labels, p, ", "); | |
176 | ||
7a804c76 KH |
177 | p = &labels[strlen (labels) - 2]; |
178 | if (*p == ',') | |
179 | *p = '\0'; | |
e19bdc14 RS |
180 | printing = header = FALSE; |
181 | labels_saved = TRUE; | |
182 | continue; | |
183 | } | |
e3515d10 | 184 | } |
cc9940c7 | 185 | |
e19bdc14 | 186 | if ((data.buffer[0] == '\0') && header) |
e3515d10 RS |
187 | { |
188 | header = FALSE; | |
e19bdc14 | 189 | if (labels_saved) |
e3515d10 RS |
190 | puts (labels); |
191 | } | |
e19bdc14 | 192 | |
e3515d10 | 193 | if (printing) |
e19bdc14 RS |
194 | puts (data.buffer); |
195 | } | |
196 | } | |
197 | ||
198 | ||
199 | ||
200 | /* | |
201 | * Return a newly-allocated string whose contents | |
202 | * concatenate those of s1, s2, s3. | |
203 | */ | |
204 | char * | |
205 | concat (s1, s2, s3) | |
206 | char *s1, *s2, *s3; | |
207 | { | |
208 | int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3); | |
209 | char *result = xnew (len1 + len2 + len3 + 1, char); | |
210 | ||
211 | strcpy (result, s1); | |
212 | strcpy (result + len1, s2); | |
213 | strcpy (result + len1 + len2, s3); | |
214 | result[len1 + len2 + len3] = '\0'; | |
215 | ||
216 | return result; | |
217 | } | |
218 | ||
219 | /* | |
220 | * Read a line of text from `stream' into `linebuffer'. | |
221 | * Return the number of characters read from `stream', | |
222 | * which is the length of the line including the newline, if any. | |
223 | */ | |
224 | long | |
225 | readline (linebuffer, stream) | |
226 | struct linebuffer *linebuffer; | |
227 | register FILE *stream; | |
228 | { | |
229 | char *buffer = linebuffer->buffer; | |
230 | register char *p = linebuffer->buffer; | |
231 | register char *pend; | |
232 | int chars_deleted; | |
233 | ||
234 | pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */ | |
235 | ||
236 | while (1) | |
237 | { | |
238 | register int c = getc (stream); | |
239 | if (p == pend) | |
240 | { | |
241 | linebuffer->size *= 2; | |
242 | buffer = (char *) xrealloc (buffer, linebuffer->size); | |
243 | p += buffer - linebuffer->buffer; | |
244 | pend = buffer + linebuffer->size; | |
245 | linebuffer->buffer = buffer; | |
246 | } | |
247 | if (c == EOF) | |
248 | { | |
5326c1d6 | 249 | *p = '\0'; |
e19bdc14 RS |
250 | chars_deleted = 0; |
251 | break; | |
252 | } | |
253 | if (c == '\n') | |
254 | { | |
5326c1d6 | 255 | if (p > buffer && p[-1] == '\r') |
e19bdc14 RS |
256 | { |
257 | *--p = '\0'; | |
258 | chars_deleted = 2; | |
259 | } | |
260 | else | |
261 | { | |
262 | *p = '\0'; | |
263 | chars_deleted = 1; | |
264 | } | |
265 | break; | |
266 | } | |
267 | *p++ = c; | |
e3515d10 | 268 | } |
e19bdc14 RS |
269 | |
270 | return (p - buffer + chars_deleted); | |
cc9940c7 | 271 | } |
e19bdc14 RS |
272 | |
273 | /* | |
274 | * Like malloc but get fatal error if memory is exhausted. | |
275 | */ | |
0404b62c | 276 | long * |
e19bdc14 RS |
277 | xmalloc (size) |
278 | unsigned int size; | |
279 | { | |
0404b62c | 280 | long *result = (long *) malloc (size); |
e19bdc14 RS |
281 | if (result == NULL) |
282 | fatal ("virtual memory exhausted"); | |
283 | return result; | |
284 | } | |
285 | ||
0404b62c | 286 | long * |
e19bdc14 RS |
287 | xrealloc (ptr, size) |
288 | char *ptr; | |
289 | unsigned int size; | |
290 | { | |
0404b62c | 291 | long *result = (long *) realloc (ptr, size); |
e19bdc14 RS |
292 | if (result == NULL) |
293 | fatal ("virtual memory exhausted"); | |
294 | return result; | |
295 | } | |
296 | ||
297 | void | |
298 | fatal (message) | |
5c0b76c1 | 299 | char *message; |
e19bdc14 RS |
300 | { |
301 | fprintf (stderr, "%s: %s\n", progname, message); | |
302 | exit (BAD); | |
303 | } | |
304 |