2 * b2m - a filter for Babyl -> Unix mail files
4 * usage: b2m < babyl > mailbox
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!
11 * it's not much but it's free!
14 * E.Wilkinson@massey.ac.nz
15 * Mon Nov 7 15:54:06 PDT 1988
18 /* Made conformant to the GNU coding standards January, 1995
19 by Francesco Potorti` <pot@cnuce.cnr.it>. */
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. */
32 #include <sys/types.h>
42 /* Exit codes for success and failure. */
51 #define streq(s,t) (strcmp (s, t) == 0)
52 #define strneq(s,t,n) (strncmp (s, t, n) == 0)
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.
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 *);
75 * xnew -- allocate storage. SYNOPSIS: Type *xnew (int n, Type);
77 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type)))
84 main (int argc, char *argv[])
86 logical labels_saved, printing, header;
88 char *labels = NULL, *p, *today;
89 struct linebuffer data;
92 _fmode = O_BINARY; /* all of files are treated as binary files */
94 if (!isatty (fileno (stdout)))
95 setmode (fileno (stdout), O_BINARY);
96 if (!isatty (fileno (stdin)))
97 setmode (fileno (stdin), O_BINARY);
98 #else /* not __DJGPP__ > 1 */
99 (stdout)->_flag &= ~_IOTEXT;
100 (stdin)->_flag &= ~_IOTEXT;
101 #endif /* not __DJGPP__ > 1 */
107 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
110 labels_saved = printing = header = FALSE;
112 today = ctime (<oday);
114 data.buffer = xnew (200, char);
116 if (readline (&data, stdin) == 0
117 || !strneq (data.buffer, "BABYL OPTIONS:", 14))
118 fatal ("standard input is not a Babyl mailfile.");
120 while (readline (&data, stdin) > 0)
122 if (streq (data.buffer, "*** EOOH ***") && !printing)
124 printing = header = TRUE;
125 printf ("From \"Babyl to mail by %s\" %s", progname, today);
129 if (data.buffer[0] == '\037')
131 if (data.buffer[1] == '\0')
133 else if (data.buffer[1] == '\f')
136 readline (&data, stdin);
137 p = strtok (data.buffer, " ,\r\n\t");
138 labels = "X-Babyl-Labels: ";
140 while ((p = strtok (NULL, " ,\r\n\t")))
141 labels = concat (labels, p, ", ");
143 p = &labels[strlen (labels) - 2];
146 printing = header = FALSE;
152 if ((data.buffer[0] == '\0') && header)
168 * Return a newly-allocated string whose contents
169 * concatenate those of s1, s2, s3.
172 concat (char *s1, char *s2, char *s3)
174 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
175 char *result = xnew (len1 + len2 + len3 + 1, char);
178 strcpy (result + len1, s2);
179 strcpy (result + len1 + len2, s3);
180 result[len1 + len2 + len3] = '\0';
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.
191 readline (struct linebuffer *linebuffer, FILE *stream)
193 char *buffer = linebuffer->buffer;
194 register char *p = linebuffer->buffer;
198 pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */
202 register int c = getc (stream);
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;
218 if (p[-1] == '\r' && p > buffer)
233 return (p - buffer + chars_deleted);
237 * Like malloc but get fatal error if memory is exhausted.
240 xmalloc (unsigned int size)
242 long *result = (long *) malloc (size);
244 fatal ("virtual memory exhausted");
249 xrealloc (void *ptr, unsigned int size)
251 long *result = (long *) realloc (ptr, size);
253 fatal ("virtual memory exhausted");
258 fatal (char *message)
260 fprintf (stderr, "%s: %s\n", progname, message);