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