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 #include <sys/types.h>
30 /* On some systems, Emacs defines static as nothing for the sake
31 of unexec. We don't want that here since we don't use unexec. */
40 /* Exit codes for success and failure. */
49 #define streq(s,t) (strcmp (s, t) == 0)
50 #define strneq(s,t,n) (strncmp (s, t, n) == 0)
55 * A `struct linebuffer' is a structure which holds a line of text.
56 * `readline' reads a line from a stream into a linebuffer and works
57 * regardless of the length of the line.
65 extern char *strtok();
67 char *xmalloc (), *xrealloc ();
73 * xnew -- allocate storage. SYNOPSIS: Type *xnew (int n, Type);
75 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type)))
85 logical labels_saved
, printing
, header
;
87 char *labels
, *p
, *today
;
88 struct linebuffer data
;
91 _fmode
= O_BINARY
; /* all of files are treated as binary files */
92 (stdout
)->_flag
&= ~_IOTEXT
;
93 (stdin
)->_flag
&= ~_IOTEXT
;
97 fprintf (stderr
, "Usage: %s <babylmailbox >unixmailbox\n", progname
);
100 labels_saved
= printing
= header
= FALSE
;
103 today
= ctime (<oday
);
105 data
.buffer
= xnew (200, char);
107 if (readline (&data
, stdin
) == 0
108 || !strneq (data
.buffer
, "BABYL OPTIONS:", 14))
109 fatal ("standard input is not a Babyl mailfile.");
111 while (readline (&data
, stdin
) > 0)
113 if (streq (data
.buffer
, "*** EOOH ***") && !printing
)
115 printing
= header
= TRUE
;
116 printf ("From Babyl to mail by %s %s", progname
, today
);
120 if (data
.buffer
[0] == '\037')
122 if (data
.buffer
[1] == '\0')
124 else if (data
.buffer
[1] == '\f')
127 readline (&data
, stdin
);
128 p
= strtok (data
.buffer
, " ,\r\n\t");
129 labels
= "X-Babyl-Labels: ";
131 while (p
= strtok (NULL
, " ,\r\n\t"))
132 labels
= concat (labels
, p
, ", ");
134 labels
[strlen (labels
) - 2] = '\0';
135 printing
= header
= FALSE
;
141 if ((data
.buffer
[0] == '\0') && header
)
156 * Return a newly-allocated string whose contents
157 * concatenate those of s1, s2, s3.
163 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
164 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
167 strcpy (result
+ len1
, s2
);
168 strcpy (result
+ len1
+ len2
, s3
);
169 result
[len1
+ len2
+ len3
] = '\0';
175 * Read a line of text from `stream' into `linebuffer'.
176 * Return the number of characters read from `stream',
177 * which is the length of the line including the newline, if any.
180 readline (linebuffer
, stream
)
181 struct linebuffer
*linebuffer
;
182 register FILE *stream
;
184 char *buffer
= linebuffer
->buffer
;
185 register char *p
= linebuffer
->buffer
;
189 pend
= p
+ linebuffer
->size
; /* Separate to avoid 386/IX compiler bug. */
193 register int c
= getc (stream
);
196 linebuffer
->size
*= 2;
197 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
198 p
+= buffer
- linebuffer
->buffer
;
199 pend
= buffer
+ linebuffer
->size
;
200 linebuffer
->buffer
= buffer
;
209 if (p
[-1] == '\r' && p
> buffer
)
224 return (p
- buffer
+ chars_deleted
);
228 * Like malloc but get fatal error if memory is exhausted.
234 char *result
= (char *) malloc (size
);
236 fatal ("virtual memory exhausted");
245 char *result
= (char *) realloc (ptr
, size
);
247 fatal ("virtual memory exhausted");
254 fprintf (stderr
, "%s: %s\n", progname
, message
);