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 | /* Exit codes for success and failure. */ |
43 | #ifdef VMS | |
44 | #define GOOD 1 | |
45 | #define BAD 0 | |
46 | #else | |
47 | #define GOOD 0 | |
48 | #define BAD 1 | |
cbc61077 | 49 | #endif |
cc9940c7 | 50 | |
e19bdc14 RS |
51 | #define streq(s,t) (strcmp (s, t) == 0) |
52 | #define strneq(s,t,n) (strncmp (s, t, n) == 0) | |
53 | ||
54 | typedef int logical; | |
55 | ||
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 | |
0404b62c | 69 | long *xmalloc (), *xrealloc (); |
e19bdc14 RS |
70 | char *concat (); |
71 | long readline (); | |
72 | void fatal (); | |
73 | ||
74 | /* | |
75 | * xnew -- allocate storage. SYNOPSIS: Type *xnew (int n, Type); | |
76 | */ | |
77 | #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type))) | |
78 | ||
79 | ||
80 | ||
81 | char *progname; | |
cc9940c7 | 82 | |
e4b34a85 KH |
83 | struct option longopts[] = |
84 | { | |
85 | { "help", no_argument, NULL, 'h' }, | |
86 | { "version", no_argument, NULL, 'V' }, | |
87 | { 0 } | |
88 | }; | |
89 | ||
90 | extern int optind; | |
91 | ||
1f8c2f55 | 92 | int |
5d13f393 JB |
93 | main (argc, argv) |
94 | int argc; | |
95 | char **argv; | |
cc9940c7 | 96 | { |
e19bdc14 RS |
97 | logical labels_saved, printing, header; |
98 | time_t ltoday; | |
99 | char *labels, *p, *today; | |
100 | struct linebuffer data; | |
101 | ||
c6880c90 | 102 | #ifdef MSDOS |
e3515d10 | 103 | _fmode = O_BINARY; /* all of files are treated as binary files */ |
18198bb2 RS |
104 | #if __DJGPP__ > 1 |
105 | if (!isatty (fileno (stdout))) | |
106 | setmode (fileno (stdout), O_BINARY); | |
107 | if (!isatty (fileno (stdin))) | |
108 | setmode (fileno (stdin), O_BINARY); | |
109 | #else /* not __DJGPP__ > 1 */ | |
c6880c90 RS |
110 | (stdout)->_flag &= ~_IOTEXT; |
111 | (stdin)->_flag &= ~_IOTEXT; | |
18198bb2 | 112 | #endif /* not __DJGPP__ > 1 */ |
c6880c90 | 113 | #endif |
d4843060 RS |
114 | progname = argv[0]; |
115 | ||
e4b34a85 KH |
116 | while (1) |
117 | { | |
118 | int opt = getopt_long (argc, argv, "hV", longopts, 0); | |
119 | if (opt == EOF) | |
120 | break; | |
121 | ||
122 | switch (opt) | |
123 | { | |
124 | case 'V': | |
125 | printf ("%s (GNU Emacs %s)\n", "b2m", VERSION); | |
126 | puts ("b2m is in the public domain."); | |
127 | exit (GOOD); | |
128 | ||
129 | case 'h': | |
130 | fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname); | |
131 | exit (GOOD); | |
132 | } | |
133 | } | |
134 | ||
135 | if (optind != argc) | |
88d00c7e | 136 | { |
e19bdc14 RS |
137 | fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname); |
138 | exit (GOOD); | |
88d00c7e | 139 | } |
e4b34a85 | 140 | |
e19bdc14 | 141 | labels_saved = printing = header = FALSE; |
e3515d10 RS |
142 | ltoday = time (0); |
143 | today = ctime (<oday); | |
e19bdc14 RS |
144 | data.size = 200; |
145 | data.buffer = xnew (200, char); | |
cc9940c7 | 146 | |
e19bdc14 RS |
147 | if (readline (&data, stdin) == 0 |
148 | || !strneq (data.buffer, "BABYL OPTIONS:", 14)) | |
149 | fatal ("standard input is not a Babyl mailfile."); | |
cc9940c7 | 150 | |
e19bdc14 | 151 | while (readline (&data, stdin) > 0) |
e3515d10 | 152 | { |
e19bdc14 | 153 | if (streq (data.buffer, "*** EOOH ***") && !printing) |
e3515d10 RS |
154 | { |
155 | printing = header = TRUE; | |
d4843060 | 156 | printf ("From \"Babyl to mail by %s\" %s", progname, today); |
e3515d10 RS |
157 | continue; |
158 | } | |
88d00c7e | 159 | |
e19bdc14 | 160 | if (data.buffer[0] == '\037') |
e3515d10 | 161 | { |
e19bdc14 RS |
162 | if (data.buffer[1] == '\0') |
163 | continue; | |
164 | else if (data.buffer[1] == '\f') | |
e3515d10 | 165 | { |
e19bdc14 RS |
166 | /* Save labels. */ |
167 | readline (&data, stdin); | |
168 | p = strtok (data.buffer, " ,\r\n\t"); | |
169 | labels = "X-Babyl-Labels: "; | |
cc9940c7 | 170 | |
25b18337 | 171 | while ((p = strtok (NULL, " ,\r\n\t"))) |
e19bdc14 RS |
172 | labels = concat (labels, p, ", "); |
173 | ||
7a804c76 KH |
174 | p = &labels[strlen (labels) - 2]; |
175 | if (*p == ',') | |
176 | *p = '\0'; | |
e19bdc14 RS |
177 | printing = header = FALSE; |
178 | labels_saved = TRUE; | |
179 | continue; | |
180 | } | |
e3515d10 | 181 | } |
cc9940c7 | 182 | |
e19bdc14 | 183 | if ((data.buffer[0] == '\0') && header) |
e3515d10 RS |
184 | { |
185 | header = FALSE; | |
e19bdc14 | 186 | if (labels_saved) |
e3515d10 RS |
187 | puts (labels); |
188 | } | |
e19bdc14 | 189 | |
e3515d10 | 190 | if (printing) |
e19bdc14 RS |
191 | puts (data.buffer); |
192 | } | |
3a213356 GM |
193 | |
194 | return 0; | |
e19bdc14 RS |
195 | } |
196 | ||
197 | ||
198 | ||
199 | /* | |
200 | * Return a newly-allocated string whose contents | |
201 | * concatenate those of s1, s2, s3. | |
202 | */ | |
203 | char * | |
204 | concat (s1, s2, s3) | |
205 | char *s1, *s2, *s3; | |
206 | { | |
207 | int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3); | |
208 | char *result = xnew (len1 + len2 + len3 + 1, char); | |
209 | ||
210 | strcpy (result, s1); | |
211 | strcpy (result + len1, s2); | |
212 | strcpy (result + len1 + len2, s3); | |
213 | result[len1 + len2 + len3] = '\0'; | |
214 | ||
215 | return result; | |
216 | } | |
217 | ||
218 | /* | |
219 | * Read a line of text from `stream' into `linebuffer'. | |
220 | * Return the number of characters read from `stream', | |
221 | * which is the length of the line including the newline, if any. | |
222 | */ | |
223 | long | |
224 | readline (linebuffer, stream) | |
225 | struct linebuffer *linebuffer; | |
226 | register FILE *stream; | |
227 | { | |
228 | char *buffer = linebuffer->buffer; | |
229 | register char *p = linebuffer->buffer; | |
230 | register char *pend; | |
231 | int chars_deleted; | |
232 | ||
233 | pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */ | |
234 | ||
235 | while (1) | |
236 | { | |
237 | register int c = getc (stream); | |
238 | if (p == pend) | |
239 | { | |
240 | linebuffer->size *= 2; | |
241 | buffer = (char *) xrealloc (buffer, linebuffer->size); | |
242 | p += buffer - linebuffer->buffer; | |
243 | pend = buffer + linebuffer->size; | |
244 | linebuffer->buffer = buffer; | |
245 | } | |
246 | if (c == EOF) | |
247 | { | |
5326c1d6 | 248 | *p = '\0'; |
e19bdc14 RS |
249 | chars_deleted = 0; |
250 | break; | |
251 | } | |
252 | if (c == '\n') | |
253 | { | |
5326c1d6 | 254 | if (p > buffer && p[-1] == '\r') |
e19bdc14 RS |
255 | { |
256 | *--p = '\0'; | |
257 | chars_deleted = 2; | |
258 | } | |
259 | else | |
260 | { | |
261 | *p = '\0'; | |
262 | chars_deleted = 1; | |
263 | } | |
264 | break; | |
265 | } | |
266 | *p++ = c; | |
e3515d10 | 267 | } |
e19bdc14 RS |
268 | |
269 | return (p - buffer + chars_deleted); | |
cc9940c7 | 270 | } |
e19bdc14 RS |
271 | |
272 | /* | |
273 | * Like malloc but get fatal error if memory is exhausted. | |
274 | */ | |
0404b62c | 275 | long * |
e19bdc14 RS |
276 | xmalloc (size) |
277 | unsigned int size; | |
278 | { | |
0404b62c | 279 | long *result = (long *) malloc (size); |
e19bdc14 RS |
280 | if (result == NULL) |
281 | fatal ("virtual memory exhausted"); | |
282 | return result; | |
283 | } | |
284 | ||
0404b62c | 285 | long * |
e19bdc14 RS |
286 | xrealloc (ptr, size) |
287 | char *ptr; | |
288 | unsigned int size; | |
289 | { | |
0404b62c | 290 | long *result = (long *) realloc (ptr, size); |
e19bdc14 RS |
291 | if (result == NULL) |
292 | fatal ("virtual memory exhausted"); | |
293 | return result; | |
294 | } | |
295 | ||
296 | void | |
297 | fatal (message) | |
5c0b76c1 | 298 | char *message; |
e19bdc14 RS |
299 | { |
300 | fprintf (stderr, "%s: %s\n", progname, message); | |
301 | exit (BAD); | |
302 | } | |
303 | ||
ab5796a9 MB |
304 | /* arch-tag: 5a3ad2af-a802-408f-83cc-e7cf5e98653e |
305 | (do not change this comment) */ |