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