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 | |
f5565804 | 29 | #include <limits.h> |
4ee9629e PE |
30 | #include <stdio.h> |
31 | #include <time.h> | |
32 | #include <sys/types.h> | |
33 | #include <getopt.h> | |
34 | #ifdef MSDOS | |
35 | #include <fcntl.h> | |
1f8c2f55 AS |
36 | #endif |
37 | ||
e19bdc14 RS |
38 | #undef TRUE |
39 | #define TRUE 1 | |
40 | #undef FALSE | |
41 | #define FALSE 0 | |
cc9940c7 | 42 | |
e19bdc14 RS |
43 | #define streq(s,t) (strcmp (s, t) == 0) |
44 | #define strneq(s,t,n) (strncmp (s, t, n) == 0) | |
45 | ||
46 | typedef int logical; | |
47 | ||
f5565804 PE |
48 | /* True if TM_YEAR is a struct tm's tm_year value that is acceptable |
49 | to asctime. Glibc asctime returns a useful string unless TM_YEAR | |
50 | is nearly INT_MAX, but the C Standard lets C libraries overrun a | |
51 | buffer if TM_YEAR needs more than 4 bytes. */ | |
52 | #ifdef __GLIBC__ | |
53 | # define TM_YEAR_IN_ASCTIME_RANGE(tm_year) ((tm_year) <= INT_MAX - 1900) | |
54 | #else | |
55 | # define TM_YEAR_IN_ASCTIME_RANGE(tm_year) \ | |
56 | (-999 - 1900 <= (tm_year) && (tm_year) <= 9999 - 1900) | |
57 | #endif | |
58 | ||
e19bdc14 RS |
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; | |
f5565804 | 102 | struct tm *tm; |
e19bdc14 RS |
103 | char *labels, *p, *today; |
104 | struct linebuffer data; | |
105 | ||
c6880c90 | 106 | #ifdef MSDOS |
e3515d10 | 107 | _fmode = O_BINARY; /* all of files are treated as binary files */ |
18198bb2 RS |
108 | #if __DJGPP__ > 1 |
109 | if (!isatty (fileno (stdout))) | |
110 | setmode (fileno (stdout), O_BINARY); | |
111 | if (!isatty (fileno (stdin))) | |
112 | setmode (fileno (stdin), O_BINARY); | |
113 | #else /* not __DJGPP__ > 1 */ | |
c6880c90 RS |
114 | (stdout)->_flag &= ~_IOTEXT; |
115 | (stdin)->_flag &= ~_IOTEXT; | |
18198bb2 | 116 | #endif /* not __DJGPP__ > 1 */ |
c6880c90 | 117 | #endif |
d4843060 RS |
118 | progname = argv[0]; |
119 | ||
e4b34a85 KH |
120 | while (1) |
121 | { | |
122 | int opt = getopt_long (argc, argv, "hV", longopts, 0); | |
123 | if (opt == EOF) | |
124 | break; | |
125 | ||
126 | switch (opt) | |
127 | { | |
128 | case 'V': | |
129 | printf ("%s (GNU Emacs %s)\n", "b2m", VERSION); | |
130 | puts ("b2m is in the public domain."); | |
3f0656ff | 131 | exit (EXIT_SUCCESS); |
e4b34a85 KH |
132 | |
133 | case 'h': | |
134 | fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname); | |
3f0656ff | 135 | exit (EXIT_SUCCESS); |
e4b34a85 KH |
136 | } |
137 | } | |
138 | ||
139 | if (optind != argc) | |
88d00c7e | 140 | { |
e19bdc14 | 141 | fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname); |
3f0656ff | 142 | exit (EXIT_SUCCESS); |
88d00c7e | 143 | } |
e4b34a85 | 144 | |
e19bdc14 | 145 | labels_saved = printing = header = FALSE; |
e3515d10 | 146 | ltoday = time (0); |
f5565804 PE |
147 | /* Convert to a string, checking for out-of-range time stamps. |
148 | Don't use 'ctime', as that might dump core if the hardware clock | |
149 | is set to a bizarre value. */ | |
150 | tm = localtime (<oday); | |
151 | if (! (tm && TM_YEAR_IN_ASCTIME_RANGE (tm->tm_year))) | |
152 | fatal ("current time is out of range"); | |
153 | today = asctime (tm); | |
e19bdc14 RS |
154 | data.size = 200; |
155 | data.buffer = xnew (200, char); | |
cc9940c7 | 156 | |
e19bdc14 RS |
157 | if (readline (&data, stdin) == 0 |
158 | || !strneq (data.buffer, "BABYL OPTIONS:", 14)) | |
159 | fatal ("standard input is not a Babyl mailfile."); | |
cc9940c7 | 160 | |
e19bdc14 | 161 | while (readline (&data, stdin) > 0) |
e3515d10 | 162 | { |
e19bdc14 | 163 | if (streq (data.buffer, "*** EOOH ***") && !printing) |
e3515d10 RS |
164 | { |
165 | printing = header = TRUE; | |
d4843060 | 166 | printf ("From \"Babyl to mail by %s\" %s", progname, today); |
e3515d10 RS |
167 | continue; |
168 | } | |
88d00c7e | 169 | |
e19bdc14 | 170 | if (data.buffer[0] == '\037') |
e3515d10 | 171 | { |
e19bdc14 RS |
172 | if (data.buffer[1] == '\0') |
173 | continue; | |
174 | else if (data.buffer[1] == '\f') | |
e3515d10 | 175 | { |
e19bdc14 RS |
176 | /* Save labels. */ |
177 | readline (&data, stdin); | |
178 | p = strtok (data.buffer, " ,\r\n\t"); | |
179 | labels = "X-Babyl-Labels: "; | |
cc9940c7 | 180 | |
25b18337 | 181 | while ((p = strtok (NULL, " ,\r\n\t"))) |
e19bdc14 RS |
182 | labels = concat (labels, p, ", "); |
183 | ||
7a804c76 KH |
184 | p = &labels[strlen (labels) - 2]; |
185 | if (*p == ',') | |
186 | *p = '\0'; | |
e19bdc14 RS |
187 | printing = header = FALSE; |
188 | labels_saved = TRUE; | |
189 | continue; | |
190 | } | |
e3515d10 | 191 | } |
cc9940c7 | 192 | |
e19bdc14 | 193 | if ((data.buffer[0] == '\0') && header) |
e3515d10 RS |
194 | { |
195 | header = FALSE; | |
e19bdc14 | 196 | if (labels_saved) |
e3515d10 RS |
197 | puts (labels); |
198 | } | |
e19bdc14 | 199 | |
e3515d10 | 200 | if (printing) |
e19bdc14 RS |
201 | puts (data.buffer); |
202 | } | |
3a213356 | 203 | |
3f0656ff | 204 | return EXIT_SUCCESS; |
e19bdc14 RS |
205 | } |
206 | ||
207 | ||
208 | ||
209 | /* | |
210 | * Return a newly-allocated string whose contents | |
211 | * concatenate those of s1, s2, s3. | |
212 | */ | |
213 | char * | |
214 | concat (s1, s2, s3) | |
215 | char *s1, *s2, *s3; | |
216 | { | |
217 | int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3); | |
218 | char *result = xnew (len1 + len2 + len3 + 1, char); | |
219 | ||
220 | strcpy (result, s1); | |
221 | strcpy (result + len1, s2); | |
222 | strcpy (result + len1 + len2, s3); | |
223 | result[len1 + len2 + len3] = '\0'; | |
224 | ||
225 | return result; | |
226 | } | |
227 | ||
228 | /* | |
229 | * Read a line of text from `stream' into `linebuffer'. | |
230 | * Return the number of characters read from `stream', | |
231 | * which is the length of the line including the newline, if any. | |
232 | */ | |
233 | long | |
234 | readline (linebuffer, stream) | |
235 | struct linebuffer *linebuffer; | |
236 | register FILE *stream; | |
237 | { | |
238 | char *buffer = linebuffer->buffer; | |
239 | register char *p = linebuffer->buffer; | |
240 | register char *pend; | |
241 | int chars_deleted; | |
242 | ||
243 | pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */ | |
244 | ||
245 | while (1) | |
246 | { | |
247 | register int c = getc (stream); | |
248 | if (p == pend) | |
249 | { | |
250 | linebuffer->size *= 2; | |
251 | buffer = (char *) xrealloc (buffer, linebuffer->size); | |
252 | p += buffer - linebuffer->buffer; | |
253 | pend = buffer + linebuffer->size; | |
254 | linebuffer->buffer = buffer; | |
255 | } | |
256 | if (c == EOF) | |
257 | { | |
5326c1d6 | 258 | *p = '\0'; |
e19bdc14 RS |
259 | chars_deleted = 0; |
260 | break; | |
261 | } | |
262 | if (c == '\n') | |
263 | { | |
5326c1d6 | 264 | if (p > buffer && p[-1] == '\r') |
e19bdc14 RS |
265 | { |
266 | *--p = '\0'; | |
267 | chars_deleted = 2; | |
268 | } | |
269 | else | |
270 | { | |
271 | *p = '\0'; | |
272 | chars_deleted = 1; | |
273 | } | |
274 | break; | |
275 | } | |
276 | *p++ = c; | |
e3515d10 | 277 | } |
e19bdc14 RS |
278 | |
279 | return (p - buffer + chars_deleted); | |
cc9940c7 | 280 | } |
e19bdc14 RS |
281 | |
282 | /* | |
283 | * Like malloc but get fatal error if memory is exhausted. | |
284 | */ | |
0404b62c | 285 | long * |
e19bdc14 RS |
286 | xmalloc (size) |
287 | unsigned int size; | |
288 | { | |
0404b62c | 289 | long *result = (long *) malloc (size); |
e19bdc14 RS |
290 | if (result == NULL) |
291 | fatal ("virtual memory exhausted"); | |
292 | return result; | |
293 | } | |
294 | ||
0404b62c | 295 | long * |
e19bdc14 RS |
296 | xrealloc (ptr, size) |
297 | char *ptr; | |
298 | unsigned int size; | |
299 | { | |
0404b62c | 300 | long *result = (long *) realloc (ptr, size); |
e19bdc14 RS |
301 | if (result == NULL) |
302 | fatal ("virtual memory exhausted"); | |
303 | return result; | |
304 | } | |
305 | ||
306 | void | |
307 | fatal (message) | |
5c0b76c1 | 308 | char *message; |
e19bdc14 RS |
309 | { |
310 | fprintf (stderr, "%s: %s\n", progname, message); | |
3f0656ff | 311 | exit (EXIT_FAILURE); |
e19bdc14 RS |
312 | } |
313 | ||
ab5796a9 MB |
314 | /* arch-tag: 5a3ad2af-a802-408f-83cc-e7cf5e98653e |
315 | (do not change this comment) */ | |
3f0656ff TTN |
316 | |
317 | /* b2m.c ends here */ |