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