440
|
1 /*
|
|
2 * b2m - a filter for Babyl -> Unix mail files
|
42260
|
3 * The copyright on this file has been disclaimed.
|
440
|
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
|
40067
|
9 * GNU Emacs. At least now I can read all my
|
|
10 * GNU Emacs Babyl format mail files!
|
440
|
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
|
10370
|
19 /* Made conformant to the GNU coding standards January, 1995
|
|
20 by Francesco Potorti` <pot@cnuce.cnr.it>. */
|
|
21
|
|
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
|
440
|
28
|
26083
|
29 #include <stdio.h>
|
|
30 #include <time.h>
|
|
31 #include <sys/types.h>
|
|
32 #include <getopt.h>
|
|
33 #ifdef MSDOS
|
|
34 #include <fcntl.h>
|
21386
|
35 #endif
|
|
36
|
10370
|
37 #undef TRUE
|
|
38 #define TRUE 1
|
|
39 #undef FALSE
|
|
40 #define FALSE 0
|
|
41
|
|
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
|
4572
|
49 #endif
|
440
|
50
|
10370
|
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 };
|
8957
|
66
|
10370
|
67 extern char *strtok();
|
|
68
|
11674
|
69 long *xmalloc (), *xrealloc ();
|
10370
|
70 char *concat ();
|
|
71 long readline ();
|
|
72 void fatal ();
|
440
|
73
|
10370
|
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;
|
|
82
|
20328
|
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
|
21386
|
92 int
|
2101
|
93 main (argc, argv)
|
|
94 int argc;
|
|
95 char **argv;
|
440
|
96 {
|
10370
|
97 logical labels_saved, printing, header;
|
|
98 time_t ltoday;
|
|
99 char *labels, *p, *today;
|
|
100 struct linebuffer data;
|
|
101
|
5448
|
102 #ifdef MSDOS
|
6215
|
103 _fmode = O_BINARY; /* all of files are treated as binary files */
|
14968
|
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 */
|
5448
|
110 (stdout)->_flag &= ~_IOTEXT;
|
|
111 (stdin)->_flag &= ~_IOTEXT;
|
14968
|
112 #endif /* not __DJGPP__ > 1 */
|
5448
|
113 #endif
|
14850
|
114 progname = argv[0];
|
|
115
|
20328
|
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)
|
6173
|
136 {
|
10370
|
137 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
|
|
138 exit (GOOD);
|
6173
|
139 }
|
20328
|
140
|
10370
|
141 labels_saved = printing = header = FALSE;
|
6215
|
142 ltoday = time (0);
|
|
143 today = ctime (<oday);
|
10370
|
144 data.size = 200;
|
|
145 data.buffer = xnew (200, char);
|
440
|
146
|
10370
|
147 if (readline (&data, stdin) == 0
|
|
148 || !strneq (data.buffer, "BABYL OPTIONS:", 14))
|
|
149 fatal ("standard input is not a Babyl mailfile.");
|
|
150
|
|
151 while (readline (&data, stdin) > 0)
|
6215
|
152 {
|
10370
|
153 if (streq (data.buffer, "*** EOOH ***") && !printing)
|
6215
|
154 {
|
|
155 printing = header = TRUE;
|
14850
|
156 printf ("From \"Babyl to mail by %s\" %s", progname, today);
|
6215
|
157 continue;
|
|
158 }
|
6173
|
159
|
10370
|
160 if (data.buffer[0] == '\037')
|
6215
|
161 {
|
10370
|
162 if (data.buffer[1] == '\0')
|
|
163 continue;
|
|
164 else if (data.buffer[1] == '\f')
|
|
165 {
|
|
166 /* Save labels. */
|
|
167 readline (&data, stdin);
|
|
168 p = strtok (data.buffer, " ,\r\n\t");
|
|
169 labels = "X-Babyl-Labels: ";
|
440
|
170
|
42471
4234f54994ef
(main): Parenthesize assignment when used as truth value to prevent gcc
Pavel Janík <Pavel@Janik.cz>
diff
changeset
|
171 while ((p = strtok (NULL, " ,\r\n\t")))
|
10370
|
172 labels = concat (labels, p, ", ");
|
|
173
|
11423
|
174 p = &labels[strlen (labels) - 2];
|
|
175 if (*p == ',')
|
|
176 *p = '\0';
|
10370
|
177 printing = header = FALSE;
|
|
178 labels_saved = TRUE;
|
|
179 continue;
|
6215
|
180 }
|
10370
|
181 }
|
440
|
182
|
10370
|
183 if ((data.buffer[0] == '\0') && header)
|
|
184 {
|
|
185 header = FALSE;
|
|
186 if (labels_saved)
|
|
187 puts (labels);
|
6215
|
188 }
|
440
|
189
|
10370
|
190 if (printing)
|
|
191 puts (data.buffer);
|
|
192 }
|
37163
|
193
|
|
194 return 0;
|
10370
|
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)
|
6215
|
239 {
|
10370
|
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;
|
6215
|
245 }
|
10370
|
246 if (c == EOF)
|
|
247 {
|
18238
|
248 *p = '\0';
|
10370
|
249 chars_deleted = 0;
|
|
250 break;
|
|
251 }
|
|
252 if (c == '\n')
|
|
253 {
|
18238
|
254 if (p > buffer && p[-1] == '\r')
|
10370
|
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;
|
440
|
267 }
|
10370
|
268
|
|
269 return (p - buffer + chars_deleted);
|
|
270 }
|
|
271
|
|
272 /*
|
|
273 * Like malloc but get fatal error if memory is exhausted.
|
|
274 */
|
11674
|
275 long *
|
10370
|
276 xmalloc (size)
|
|
277 unsigned int size;
|
|
278 {
|
11674
|
279 long *result = (long *) malloc (size);
|
10370
|
280 if (result == NULL)
|
|
281 fatal ("virtual memory exhausted");
|
|
282 return result;
|
440
|
283 }
|
10370
|
284
|
11674
|
285 long *
|
10370
|
286 xrealloc (ptr, size)
|
|
287 char *ptr;
|
|
288 unsigned int size;
|
|
289 {
|
11674
|
290 long *result = (long *) realloc (ptr, size);
|
10370
|
291 if (result == NULL)
|
|
292 fatal ("virtual memory exhausted");
|
|
293 return result;
|
|
294 }
|
|
295
|
|
296 void
|
|
297 fatal (message)
|
21354
|
298 char *message;
|
10370
|
299 {
|
|
300 fprintf (stderr, "%s: %s\n", progname, message);
|
|
301 exit (BAD);
|
|
302 }
|
|
303
|