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 */
93 if (!isatty (fileno (stdout)))
94 setmode (fileno (stdout), O_BINARY);
95 if (!isatty (fileno (stdin)))
96 setmode (fileno (stdin), O_BINARY);
102 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
105 labels_saved = printing = header = FALSE;
107 today = ctime (<oday);
109 data.buffer = xnew (200, char);
111 if (readline (&data, stdin) == 0
112 || !strneq (data.buffer, "BABYL OPTIONS:", 14))
113 fatal ("standard input is not a Babyl mailfile.");
115 while (readline (&data, stdin) > 0)
117 if (streq (data.buffer, "*** EOOH ***") && !printing)
119 printing = header = TRUE;
120 printf ("From \"Babyl to mail by %s\" %s", progname, today);
124 if (data.buffer[0] == '\037')
126 if (data.buffer[1] == '\0')
128 else if (data.buffer[1] == '\f')
131 readline (&data, stdin);
132 p = strtok (data.buffer, " ,\r\n\t");
133 labels = "X-Babyl-Labels: ";
135 while ((p = strtok (NULL, " ,\r\n\t")))
136 labels = concat (labels, p, ", ");
138 p = &labels[strlen (labels) - 2];
141 printing = header = FALSE;
147 if ((data.buffer[0] == '\0') && header)
163 * Return a newly-allocated string whose contents
164 * concatenate those of s1, s2, s3.
167 concat (char *s1, char *s2, char *s3)
169 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
170 char *result = xnew (len1 + len2 + len3 + 1, char);
173 strcpy (result + len1, s2);
174 strcpy (result + len1 + len2, s3);
175 result[len1 + len2 + len3] = '\0';
181 * Read a line of text from `stream' into `linebuffer'.
182 * Return the number of characters read from `stream',
183 * which is the length of the line including the newline, if any.
186 readline (struct linebuffer *linebuffer, FILE *stream)
188 char *buffer = linebuffer->buffer;
189 register char *p = linebuffer->buffer;
193 pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */
197 register int c = getc (stream);
200 linebuffer->size *= 2;
201 buffer = (char *) xrealloc (buffer, linebuffer->size);
202 p += buffer - linebuffer->buffer;
203 pend = buffer + linebuffer->size;
204 linebuffer->buffer = buffer;
213 if (p[-1] == '\r' && p > buffer)
228 return (p - buffer + chars_deleted);
232 * Like malloc but get fatal error if memory is exhausted.
235 xmalloc (unsigned int size)
237 long *result = (long *) malloc (size);
239 fatal ("virtual memory exhausted");
244 xrealloc (void *ptr, unsigned int size)
246 long *result = (long *) realloc (ptr, size);
248 fatal ("virtual memory exhausted");
253 fatal (char *message)
255 fprintf (stderr, "%s: %s\n", progname, message);