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. */
30 #include <sys/types.h>
41 /* Exit codes for success and failure. */
50 #define streq(s,t) (strcmp (s, t) == 0)
51 #define strneq(s,t,n) (strncmp (s, t, n) == 0)
56 * A `struct linebuffer' is a structure which holds a line of text.
57 * `readline' reads a line from a stream into a linebuffer and works
58 * regardless of the length of the line.
66 extern char *strtok();
68 long *xmalloc (), *xrealloc ();
74 * xnew -- allocate storage. SYNOPSIS: Type *xnew (int n, Type);
76 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type)))
82 struct option longopts
[] =
84 { "help", no_argument
, NULL
, 'h' },
85 { "version", no_argument
, NULL
, 'V' },
96 logical labels_saved
, printing
, header
;
98 char *labels
, *p
, *today
;
99 struct linebuffer data
;
102 _fmode
= O_BINARY
; /* all of files are treated as binary files */
104 if (!isatty (fileno (stdout
)))
105 setmode (fileno (stdout
), O_BINARY
);
106 if (!isatty (fileno (stdin
)))
107 setmode (fileno (stdin
), O_BINARY
);
108 #else /* not __DJGPP__ > 1 */
109 (stdout
)->_flag
&= ~_IOTEXT
;
110 (stdin
)->_flag
&= ~_IOTEXT
;
111 #endif /* not __DJGPP__ > 1 */
117 int opt
= getopt_long (argc
, argv
, "hV", longopts
, 0);
124 printf ("%s (GNU Emacs %s)\n", "b2m", VERSION
);
125 puts ("b2m is in the public domain.");
129 fprintf (stderr
, "Usage: %s <babylmailbox >unixmailbox\n", progname
);
136 fprintf (stderr
, "Usage: %s <babylmailbox >unixmailbox\n", progname
);
140 labels_saved
= printing
= header
= FALSE
;
142 today
= ctime (<oday
);
144 data
.buffer
= xnew (200, char);
146 if (readline (&data
, stdin
) == 0
147 || !strneq (data
.buffer
, "BABYL OPTIONS:", 14))
148 fatal ("standard input is not a Babyl mailfile.");
150 while (readline (&data
, stdin
) > 0)
152 if (streq (data
.buffer
, "*** EOOH ***") && !printing
)
154 printing
= header
= TRUE
;
155 printf ("From \"Babyl to mail by %s\" %s", progname
, today
);
159 if (data
.buffer
[0] == '\037')
161 if (data
.buffer
[1] == '\0')
163 else if (data
.buffer
[1] == '\f')
166 readline (&data
, stdin
);
167 p
= strtok (data
.buffer
, " ,\r\n\t");
168 labels
= "X-Babyl-Labels: ";
170 while (p
= strtok (NULL
, " ,\r\n\t"))
171 labels
= concat (labels
, p
, ", ");
173 p
= &labels
[strlen (labels
) - 2];
176 printing
= header
= FALSE
;
182 if ((data
.buffer
[0] == '\0') && header
)
197 * Return a newly-allocated string whose contents
198 * concatenate those of s1, s2, s3.
204 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
205 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
208 strcpy (result
+ len1
, s2
);
209 strcpy (result
+ len1
+ len2
, s3
);
210 result
[len1
+ len2
+ len3
] = '\0';
216 * Read a line of text from `stream' into `linebuffer'.
217 * Return the number of characters read from `stream',
218 * which is the length of the line including the newline, if any.
221 readline (linebuffer
, stream
)
222 struct linebuffer
*linebuffer
;
223 register FILE *stream
;
225 char *buffer
= linebuffer
->buffer
;
226 register char *p
= linebuffer
->buffer
;
230 pend
= p
+ linebuffer
->size
; /* Separate to avoid 386/IX compiler bug. */
234 register int c
= getc (stream
);
237 linebuffer
->size
*= 2;
238 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
239 p
+= buffer
- linebuffer
->buffer
;
240 pend
= buffer
+ linebuffer
->size
;
241 linebuffer
->buffer
= buffer
;
251 if (p
> buffer
&& p
[-1] == '\r')
266 return (p
- buffer
+ chars_deleted
);
270 * Like malloc but get fatal error if memory is exhausted.
276 long *result
= (long *) malloc (size
);
278 fatal ("virtual memory exhausted");
287 long *result
= (long *) realloc (ptr
, size
);
289 fatal ("virtual memory exhausted");
297 fprintf (stderr
, "%s: %s\n", progname
, message
);