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
|
412
|
22 #include <../src/config.h>
|
155
|
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>
|
412
|
33 #ifdef MSDOS
|
0
|
34 #include <fcntl.h>
|
|
35 #endif
|
|
36
|
146
|
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
|
|
49 #endif
|
|
50
|
|
51 #define streq(s,t) (strcmp (s, t) == 0)
|
|
52 #define strneq(s,t,n) (strncmp (s, t, n) == 0)
|
0
|
53
|
146
|
54 typedef int logical;
|
0
|
55
|
146
|
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
|
173
|
68 static long *xmalloc (unsigned int);
|
|
69 static long *xrealloc (void *, unsigned int);
|
|
70 static char *concat (char *s1, char *s2, char *s3);
|
|
71 static long readline (struct linebuffer *, FILE *);
|
|
72 static void fatal (char *);
|
146
|
73
|
|
74 /*
|
|
75 * xnew -- allocate storage. SYNOPSIS: Type *xnew (int n, Type);
|
|
76 */
|
|
77 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type)))
|
0
|
78
|
|
79
|
146
|
80
|
|
81 char *progname;
|
0
|
82
|
155
|
83 int
|
173
|
84 main (int argc, char *argv[])
|
146
|
85 {
|
|
86 logical labels_saved, printing, header;
|
|
87 time_t ltoday;
|
173
|
88 char *labels = NULL, *p, *today;
|
146
|
89 struct linebuffer data;
|
0
|
90
|
412
|
91 #ifdef MSDOS
|
0
|
92 _fmode = O_BINARY; /* all of files are treated as binary files */
|
412
|
93 #if __DJGPP__ > 1
|
146
|
94 if (!isatty (fileno (stdout)))
|
|
95 setmode (fileno (stdout), O_BINARY);
|
|
96 if (!isatty (fileno (stdin)))
|
|
97 setmode (fileno (stdin), O_BINARY);
|
412
|
98 #else /* not __DJGPP__ > 1 */
|
|
99 (stdout)->_flag &= ~_IOTEXT;
|
|
100 (stdin)->_flag &= ~_IOTEXT;
|
|
101 #endif /* not __DJGPP__ > 1 */
|
0
|
102 #endif
|
146
|
103 progname = argv[0];
|
|
104
|
|
105 if (argc != 1)
|
0
|
106 {
|
146
|
107 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
|
|
108 exit (GOOD);
|
0
|
109 }
|
146
|
110 labels_saved = printing = header = FALSE;
|
0
|
111 ltoday = time (0);
|
|
112 today = ctime (<oday);
|
146
|
113 data.size = 200;
|
|
114 data.buffer = xnew (200, char);
|
0
|
115
|
146
|
116 if (readline (&data, stdin) == 0
|
|
117 || !strneq (data.buffer, "BABYL OPTIONS:", 14))
|
|
118 fatal ("standard input is not a Babyl mailfile.");
|
|
119
|
|
120 while (readline (&data, stdin) > 0)
|
0
|
121 {
|
146
|
122 if (streq (data.buffer, "*** EOOH ***") && !printing)
|
0
|
123 {
|
|
124 printing = header = TRUE;
|
146
|
125 printf ("From \"Babyl to mail by %s\" %s", progname, today);
|
0
|
126 continue;
|
|
127 }
|
|
128
|
146
|
129 if (data.buffer[0] == '\037')
|
0
|
130 {
|
146
|
131 if (data.buffer[1] == '\0')
|
|
132 continue;
|
|
133 else if (data.buffer[1] == '\f')
|
|
134 {
|
|
135 /* Save labels. */
|
|
136 readline (&data, stdin);
|
|
137 p = strtok (data.buffer, " ,\r\n\t");
|
|
138 labels = "X-Babyl-Labels: ";
|
|
139
|
173
|
140 while ((p = strtok (NULL, " ,\r\n\t")))
|
146
|
141 labels = concat (labels, p, ", ");
|
0
|
142
|
146
|
143 p = &labels[strlen (labels) - 2];
|
|
144 if (*p == ',')
|
|
145 *p = '\0';
|
|
146 printing = header = FALSE;
|
|
147 labels_saved = TRUE;
|
|
148 continue;
|
0
|
149 }
|
146
|
150 }
|
0
|
151
|
146
|
152 if ((data.buffer[0] == '\0') && header)
|
|
153 {
|
|
154 header = FALSE;
|
|
155 if (labels_saved)
|
|
156 puts (labels);
|
0
|
157 }
|
|
158
|
|
159 if (printing)
|
146
|
160 puts (data.buffer);
|
0
|
161 }
|
155
|
162 return 0;
|
0
|
163 }
|
146
|
164
|
|
165
|
|
166
|
|
167 /*
|
|
168 * Return a newly-allocated string whose contents
|
|
169 * concatenate those of s1, s2, s3.
|
|
170 */
|
173
|
171 static char *
|
155
|
172 concat (char *s1, char *s2, char *s3)
|
146
|
173 {
|
|
174 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
|
|
175 char *result = xnew (len1 + len2 + len3 + 1, char);
|
|
176
|
|
177 strcpy (result, s1);
|
|
178 strcpy (result + len1, s2);
|
|
179 strcpy (result + len1 + len2, s3);
|
|
180 result[len1 + len2 + len3] = '\0';
|
|
181
|
|
182 return result;
|
|
183 }
|
|
184
|
|
185 /*
|
|
186 * Read a line of text from `stream' into `linebuffer'.
|
|
187 * Return the number of characters read from `stream',
|
|
188 * which is the length of the line including the newline, if any.
|
|
189 */
|
173
|
190 static long
|
|
191 readline (struct linebuffer *linebuffer, FILE *stream)
|
146
|
192 {
|
|
193 char *buffer = linebuffer->buffer;
|
|
194 register char *p = linebuffer->buffer;
|
|
195 register char *pend;
|
|
196 int chars_deleted;
|
|
197
|
|
198 pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */
|
|
199
|
|
200 while (1)
|
|
201 {
|
|
202 register int c = getc (stream);
|
|
203 if (p == pend)
|
|
204 {
|
|
205 linebuffer->size *= 2;
|
|
206 buffer = (char *) xrealloc (buffer, linebuffer->size);
|
|
207 p += buffer - linebuffer->buffer;
|
|
208 pend = buffer + linebuffer->size;
|
|
209 linebuffer->buffer = buffer;
|
|
210 }
|
|
211 if (c == EOF)
|
|
212 {
|
|
213 chars_deleted = 0;
|
|
214 break;
|
|
215 }
|
|
216 if (c == '\n')
|
|
217 {
|
|
218 if (p[-1] == '\r' && p > buffer)
|
|
219 {
|
|
220 *--p = '\0';
|
|
221 chars_deleted = 2;
|
|
222 }
|
|
223 else
|
|
224 {
|
|
225 *p = '\0';
|
|
226 chars_deleted = 1;
|
|
227 }
|
|
228 break;
|
|
229 }
|
|
230 *p++ = c;
|
|
231 }
|
|
232
|
|
233 return (p - buffer + chars_deleted);
|
|
234 }
|
|
235
|
|
236 /*
|
|
237 * Like malloc but get fatal error if memory is exhausted.
|
|
238 */
|
173
|
239 static long *
|
155
|
240 xmalloc (unsigned int size)
|
146
|
241 {
|
|
242 long *result = (long *) malloc (size);
|
|
243 if (result == NULL)
|
|
244 fatal ("virtual memory exhausted");
|
|
245 return result;
|
|
246 }
|
|
247
|
173
|
248 static long *
|
|
249 xrealloc (void *ptr, unsigned int size)
|
146
|
250 {
|
|
251 long *result = (long *) realloc (ptr, size);
|
|
252 if (result == NULL)
|
|
253 fatal ("virtual memory exhausted");
|
|
254 return result;
|
|
255 }
|
|
256
|
173
|
257 static void
|
155
|
258 fatal (char *message)
|
146
|
259 {
|
|
260 fprintf (stderr, "%s: %s\n", progname, message);
|
|
261 exit (BAD);
|
|
262 }
|
|
263
|