0
|
1 /*
|
|
2 * b2m - a filter for Babyl -> Unix mail files
|
|
3 *
|
|
4 * usage: b2m < babyl > mailbox
|
|
5 *
|
|
6 * I find this useful whenever I have to use a
|
|
7 * system which - shock horror! - doesn't run
|
|
8 * Gnu emacs. At least now I can read all my
|
|
9 * Gnumacs Babyl format mail files!
|
|
10 *
|
|
11 * it's not much but it's free!
|
|
12 *
|
|
13 * Ed Wilkinson
|
|
14 * E.Wilkinson@massey.ac.nz
|
|
15 * Mon Nov 7 15:54:06 PDT 1988
|
|
16 */
|
|
17
|
146
|
18 /* Made conformant to the GNU coding standards January, 1995
|
|
19 by Francesco Potorti` <pot@cnuce.cnr.it>. */
|
0
|
20
|
155
|
21 #ifdef HAVE_CONFIG_H
|
|
22 #include <config.h>
|
|
23 /* On some systems, Emacs defines static as nothing for the sake
|
|
24 of unexec. We don't want that here since we don't use unexec. */
|
|
25 #undef static
|
|
26 #endif
|
|
27
|
0
|
28 #include <stdio.h>
|
151
|
29 #include <stdlib.h>
|
|
30 #include <string.h>
|
0
|
31 #include <time.h>
|
|
32 #include <sys/types.h>
|
|
33 #ifdef MSDOS
|
|
34 #include <fcntl.h>
|
|
35 #endif
|
|
36
|
146
|
37 #ifdef HAVE_CONFIG_H
|
|
38 #include <config.h>
|
|
39 /* On some systems, Emacs defines static as nothing for the sake
|
|
40 of unexec. We don't want that here since we don't use unexec. */
|
|
41 #undef static
|
|
42 #endif
|
|
43 #undef TRUE
|
|
44 #define TRUE 1
|
|
45 #undef FALSE
|
|
46 #define FALSE 0
|
|
47
|
|
48 /* Exit codes for success and failure. */
|
|
49 #ifdef VMS
|
|
50 #define GOOD 1
|
|
51 #define BAD 0
|
|
52 #else
|
|
53 #define GOOD 0
|
|
54 #define BAD 1
|
|
55 #endif
|
|
56
|
|
57 #define streq(s,t) (strcmp (s, t) == 0)
|
|
58 #define strneq(s,t,n) (strncmp (s, t, n) == 0)
|
0
|
59
|
146
|
60 typedef int logical;
|
0
|
61
|
146
|
62 /*
|
|
63 * A `struct linebuffer' is a structure which holds a line of text.
|
|
64 * `readline' reads a line from a stream into a linebuffer and works
|
|
65 * regardless of the length of the line.
|
|
66 */
|
|
67 struct linebuffer
|
|
68 {
|
|
69 long size;
|
|
70 char *buffer;
|
|
71 };
|
|
72
|
|
73 extern char *strtok();
|
|
74
|
|
75 long *xmalloc (), *xrealloc ();
|
|
76 char *concat ();
|
|
77 long readline ();
|
155
|
78 void fatal (char *);
|
146
|
79
|
|
80 /*
|
|
81 * xnew -- allocate storage. SYNOPSIS: Type *xnew (int n, Type);
|
|
82 */
|
|
83 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type)))
|
0
|
84
|
|
85
|
146
|
86
|
|
87 char *progname;
|
0
|
88
|
155
|
89 int
|
|
90 main (int argc, char **argv)
|
146
|
91 {
|
|
92 logical labels_saved, printing, header;
|
|
93 time_t ltoday;
|
|
94 char *labels, *p, *today;
|
|
95 struct linebuffer data;
|
0
|
96
|
|
97 #ifdef MSDOS
|
|
98 _fmode = O_BINARY; /* all of files are treated as binary files */
|
146
|
99 #if __DJGPP__ > 1
|
|
100 if (!isatty (fileno (stdout)))
|
|
101 setmode (fileno (stdout), O_BINARY);
|
|
102 if (!isatty (fileno (stdin)))
|
|
103 setmode (fileno (stdin), O_BINARY);
|
|
104 #else /* not __DJGPP__ > 1 */
|
0
|
105 (stdout)->_flag &= ~_IOTEXT;
|
|
106 (stdin)->_flag &= ~_IOTEXT;
|
146
|
107 #endif /* not __DJGPP__ > 1 */
|
0
|
108 #endif
|
146
|
109 progname = argv[0];
|
|
110
|
|
111 if (argc != 1)
|
0
|
112 {
|
146
|
113 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
|
|
114 exit (GOOD);
|
0
|
115 }
|
146
|
116 labels_saved = printing = header = FALSE;
|
0
|
117 ltoday = time (0);
|
|
118 today = ctime (<oday);
|
146
|
119 data.size = 200;
|
|
120 data.buffer = xnew (200, char);
|
0
|
121
|
146
|
122 if (readline (&data, stdin) == 0
|
|
123 || !strneq (data.buffer, "BABYL OPTIONS:", 14))
|
|
124 fatal ("standard input is not a Babyl mailfile.");
|
|
125
|
|
126 while (readline (&data, stdin) > 0)
|
0
|
127 {
|
146
|
128 if (streq (data.buffer, "*** EOOH ***") && !printing)
|
0
|
129 {
|
|
130 printing = header = TRUE;
|
146
|
131 printf ("From \"Babyl to mail by %s\" %s", progname, today);
|
0
|
132 continue;
|
|
133 }
|
|
134
|
146
|
135 if (data.buffer[0] == '\037')
|
0
|
136 {
|
146
|
137 if (data.buffer[1] == '\0')
|
|
138 continue;
|
|
139 else if (data.buffer[1] == '\f')
|
|
140 {
|
|
141 /* Save labels. */
|
|
142 readline (&data, stdin);
|
|
143 p = strtok (data.buffer, " ,\r\n\t");
|
|
144 labels = "X-Babyl-Labels: ";
|
|
145
|
|
146 while (p = strtok (NULL, " ,\r\n\t"))
|
|
147 labels = concat (labels, p, ", ");
|
0
|
148
|
146
|
149 p = &labels[strlen (labels) - 2];
|
|
150 if (*p == ',')
|
|
151 *p = '\0';
|
|
152 printing = header = FALSE;
|
|
153 labels_saved = TRUE;
|
|
154 continue;
|
0
|
155 }
|
146
|
156 }
|
0
|
157
|
146
|
158 if ((data.buffer[0] == '\0') && header)
|
|
159 {
|
|
160 header = FALSE;
|
|
161 if (labels_saved)
|
|
162 puts (labels);
|
0
|
163 }
|
|
164
|
|
165 if (printing)
|
146
|
166 puts (data.buffer);
|
0
|
167 }
|
155
|
168 return 0;
|
0
|
169 }
|
146
|
170
|
|
171
|
|
172
|
|
173 /*
|
|
174 * Return a newly-allocated string whose contents
|
|
175 * concatenate those of s1, s2, s3.
|
|
176 */
|
|
177 char *
|
155
|
178 concat (char *s1, char *s2, char *s3)
|
146
|
179 {
|
|
180 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
|
|
181 char *result = xnew (len1 + len2 + len3 + 1, char);
|
|
182
|
|
183 strcpy (result, s1);
|
|
184 strcpy (result + len1, s2);
|
|
185 strcpy (result + len1 + len2, s3);
|
|
186 result[len1 + len2 + len3] = '\0';
|
|
187
|
|
188 return result;
|
|
189 }
|
|
190
|
|
191 /*
|
|
192 * Read a line of text from `stream' into `linebuffer'.
|
|
193 * Return the number of characters read from `stream',
|
|
194 * which is the length of the line including the newline, if any.
|
|
195 */
|
|
196 long
|
155
|
197 readline (struct linebuffer *linebuffer, register FILE *stream)
|
146
|
198 {
|
|
199 char *buffer = linebuffer->buffer;
|
|
200 register char *p = linebuffer->buffer;
|
|
201 register char *pend;
|
|
202 int chars_deleted;
|
|
203
|
|
204 pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */
|
|
205
|
|
206 while (1)
|
|
207 {
|
|
208 register int c = getc (stream);
|
|
209 if (p == pend)
|
|
210 {
|
|
211 linebuffer->size *= 2;
|
|
212 buffer = (char *) xrealloc (buffer, linebuffer->size);
|
|
213 p += buffer - linebuffer->buffer;
|
|
214 pend = buffer + linebuffer->size;
|
|
215 linebuffer->buffer = buffer;
|
|
216 }
|
|
217 if (c == EOF)
|
|
218 {
|
|
219 chars_deleted = 0;
|
|
220 break;
|
|
221 }
|
|
222 if (c == '\n')
|
|
223 {
|
|
224 if (p[-1] == '\r' && p > buffer)
|
|
225 {
|
|
226 *--p = '\0';
|
|
227 chars_deleted = 2;
|
|
228 }
|
|
229 else
|
|
230 {
|
|
231 *p = '\0';
|
|
232 chars_deleted = 1;
|
|
233 }
|
|
234 break;
|
|
235 }
|
|
236 *p++ = c;
|
|
237 }
|
|
238
|
|
239 return (p - buffer + chars_deleted);
|
|
240 }
|
|
241
|
|
242 /*
|
|
243 * Like malloc but get fatal error if memory is exhausted.
|
|
244 */
|
|
245 long *
|
155
|
246 xmalloc (unsigned int size)
|
146
|
247 {
|
|
248 long *result = (long *) malloc (size);
|
|
249 if (result == NULL)
|
|
250 fatal ("virtual memory exhausted");
|
|
251 return result;
|
|
252 }
|
|
253
|
|
254 long *
|
155
|
255 xrealloc (char *ptr, unsigned int size)
|
146
|
256 {
|
|
257 long *result = (long *) realloc (ptr, size);
|
|
258 if (result == NULL)
|
|
259 fatal ("virtual memory exhausted");
|
|
260 return result;
|
|
261 }
|
|
262
|
|
263 void
|
155
|
264 fatal (char *message)
|
146
|
265 {
|
|
266 fprintf (stderr, "%s: %s\n", progname, message);
|
|
267 exit (BAD);
|
|
268 }
|
|
269
|