2 * b2m - a filter for Babyl -> Unix mail files
3 * The copyright on this file has been disclaimed.
5 * usage: b2m < babyl > mailbox
7 * I find this useful whenever I have to use a
8 * system which - shock horror! - doesn't run
9 * GNU Emacs. At least now I can read all my
10 * GNU Emacs Babyl format mail files!
12 * it's not much but it's free!
15 * E.Wilkinson@massey.ac.nz
16 * Mon Nov 7 15:54:06 PDT 1988
19 /* Made conformant to the GNU coding standards January, 1995
20 by Francesco Potorti` <pot@cnuce.cnr.it>. */
24 /* On some systems, Emacs defines static as nothing for the sake
25 of unexec. We don't want that here since we don't use unexec. */
31 #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.
67 extern char *strtok();
69 long *xmalloc (), *xrealloc ();
75 * xnew -- allocate storage. SYNOPSIS: Type *xnew (int n, Type);
77 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type)))
83 struct option longopts
[] =
85 { "help", no_argument
, NULL
, 'h' },
86 { "version", no_argument
, NULL
, 'V' },
97 logical labels_saved
, printing
, header
;
99 char *labels
, *p
, *today
;
100 struct linebuffer data
;
103 _fmode
= O_BINARY
; /* all of files are treated as binary files */
105 if (!isatty (fileno (stdout
)))
106 setmode (fileno (stdout
), O_BINARY
);
107 if (!isatty (fileno (stdin
)))
108 setmode (fileno (stdin
), O_BINARY
);
109 #else /* not __DJGPP__ > 1 */
110 (stdout
)->_flag
&= ~_IOTEXT
;
111 (stdin
)->_flag
&= ~_IOTEXT
;
112 #endif /* not __DJGPP__ > 1 */
118 int opt
= getopt_long (argc
, argv
, "hV", longopts
, 0);
125 printf ("%s (GNU Emacs %s)\n", "b2m", VERSION
);
126 puts ("b2m is in the public domain.");
130 fprintf (stderr
, "Usage: %s <babylmailbox >unixmailbox\n", progname
);
137 fprintf (stderr
, "Usage: %s <babylmailbox >unixmailbox\n", progname
);
141 labels_saved
= printing
= header
= FALSE
;
143 today
= ctime (<oday
);
145 data
.buffer
= xnew (200, char);
147 if (readline (&data
, stdin
) == 0
148 || !strneq (data
.buffer
, "BABYL OPTIONS:", 14))
149 fatal ("standard input is not a Babyl mailfile.");
151 while (readline (&data
, stdin
) > 0)
153 if (streq (data
.buffer
, "*** EOOH ***") && !printing
)
155 printing
= header
= TRUE
;
156 printf ("From \"Babyl to mail by %s\" %s", progname
, today
);
160 if (data
.buffer
[0] == '\037')
162 if (data
.buffer
[1] == '\0')
164 else if (data
.buffer
[1] == '\f')
167 readline (&data
, stdin
);
168 p
= strtok (data
.buffer
, " ,\r\n\t");
169 labels
= "X-Babyl-Labels: ";
171 while ((p
= strtok (NULL
, " ,\r\n\t")))
172 labels
= concat (labels
, p
, ", ");
174 p
= &labels
[strlen (labels
) - 2];
177 printing
= header
= FALSE
;
183 if ((data
.buffer
[0] == '\0') && header
)
200 * Return a newly-allocated string whose contents
201 * concatenate those of s1, s2, s3.
207 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
208 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
211 strcpy (result
+ len1
, s2
);
212 strcpy (result
+ len1
+ len2
, s3
);
213 result
[len1
+ len2
+ len3
] = '\0';
219 * Read a line of text from `stream' into `linebuffer'.
220 * Return the number of characters read from `stream',
221 * which is the length of the line including the newline, if any.
224 readline (linebuffer
, stream
)
225 struct linebuffer
*linebuffer
;
226 register FILE *stream
;
228 char *buffer
= linebuffer
->buffer
;
229 register char *p
= linebuffer
->buffer
;
233 pend
= p
+ linebuffer
->size
; /* Separate to avoid 386/IX compiler bug. */
237 register int c
= getc (stream
);
240 linebuffer
->size
*= 2;
241 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
242 p
+= buffer
- linebuffer
->buffer
;
243 pend
= buffer
+ linebuffer
->size
;
244 linebuffer
->buffer
= buffer
;
254 if (p
> buffer
&& p
[-1] == '\r')
269 return (p
- buffer
+ chars_deleted
);
273 * Like malloc but get fatal error if memory is exhausted.
279 long *result
= (long *) malloc (size
);
281 fatal ("virtual memory exhausted");
290 long *result
= (long *) realloc (ptr
, size
);
292 fatal ("virtual memory exhausted");
300 fprintf (stderr
, "%s: %s\n", progname
, message
);