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 #define streq(s,t) (strcmp (s, t) == 0)
43 #define strneq(s,t,n) (strncmp (s, t, n) == 0)
48 * A `struct linebuffer' is a structure which holds a line of text.
49 * `readline' reads a line from a stream into a linebuffer and works
50 * regardless of the length of the line.
58 extern char *strtok();
60 long *xmalloc (), *xrealloc ();
66 * xnew -- allocate storage. SYNOPSIS: Type *xnew (int n, Type);
68 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type)))
74 struct option longopts
[] =
76 { "help", no_argument
, NULL
, 'h' },
77 { "version", no_argument
, NULL
, 'V' },
88 logical labels_saved
, printing
, header
;
90 char *labels
, *p
, *today
;
91 struct linebuffer data
;
94 _fmode
= O_BINARY
; /* all of files are treated as binary files */
96 if (!isatty (fileno (stdout
)))
97 setmode (fileno (stdout
), O_BINARY
);
98 if (!isatty (fileno (stdin
)))
99 setmode (fileno (stdin
), O_BINARY
);
100 #else /* not __DJGPP__ > 1 */
101 (stdout
)->_flag
&= ~_IOTEXT
;
102 (stdin
)->_flag
&= ~_IOTEXT
;
103 #endif /* not __DJGPP__ > 1 */
109 int opt
= getopt_long (argc
, argv
, "hV", longopts
, 0);
116 printf ("%s (GNU Emacs %s)\n", "b2m", VERSION
);
117 puts ("b2m is in the public domain.");
121 fprintf (stderr
, "Usage: %s <babylmailbox >unixmailbox\n", progname
);
128 fprintf (stderr
, "Usage: %s <babylmailbox >unixmailbox\n", progname
);
132 labels_saved
= printing
= header
= FALSE
;
134 today
= ctime (<oday
);
136 data
.buffer
= xnew (200, char);
138 if (readline (&data
, stdin
) == 0
139 || !strneq (data
.buffer
, "BABYL OPTIONS:", 14))
140 fatal ("standard input is not a Babyl mailfile.");
142 while (readline (&data
, stdin
) > 0)
144 if (streq (data
.buffer
, "*** EOOH ***") && !printing
)
146 printing
= header
= TRUE
;
147 printf ("From \"Babyl to mail by %s\" %s", progname
, today
);
151 if (data
.buffer
[0] == '\037')
153 if (data
.buffer
[1] == '\0')
155 else if (data
.buffer
[1] == '\f')
158 readline (&data
, stdin
);
159 p
= strtok (data
.buffer
, " ,\r\n\t");
160 labels
= "X-Babyl-Labels: ";
162 while ((p
= strtok (NULL
, " ,\r\n\t")))
163 labels
= concat (labels
, p
, ", ");
165 p
= &labels
[strlen (labels
) - 2];
168 printing
= header
= FALSE
;
174 if ((data
.buffer
[0] == '\0') && header
)
191 * Return a newly-allocated string whose contents
192 * concatenate those of s1, s2, s3.
198 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
199 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
202 strcpy (result
+ len1
, s2
);
203 strcpy (result
+ len1
+ len2
, s3
);
204 result
[len1
+ len2
+ len3
] = '\0';
210 * Read a line of text from `stream' into `linebuffer'.
211 * Return the number of characters read from `stream',
212 * which is the length of the line including the newline, if any.
215 readline (linebuffer
, stream
)
216 struct linebuffer
*linebuffer
;
217 register FILE *stream
;
219 char *buffer
= linebuffer
->buffer
;
220 register char *p
= linebuffer
->buffer
;
224 pend
= p
+ linebuffer
->size
; /* Separate to avoid 386/IX compiler bug. */
228 register int c
= getc (stream
);
231 linebuffer
->size
*= 2;
232 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
233 p
+= buffer
- linebuffer
->buffer
;
234 pend
= buffer
+ linebuffer
->size
;
235 linebuffer
->buffer
= buffer
;
245 if (p
> buffer
&& p
[-1] == '\r')
260 return (p
- buffer
+ chars_deleted
);
264 * Like malloc but get fatal error if memory is exhausted.
270 long *result
= (long *) malloc (size
);
272 fatal ("virtual memory exhausted");
281 long *result
= (long *) realloc (ptr
, size
);
283 fatal ("virtual memory exhausted");
291 fprintf (stderr
, "%s: %s\n", progname
, message
);
295 /* arch-tag: 5a3ad2af-a802-408f-83cc-e7cf5e98653e
296 (do not change this comment) */
298 /* b2m.c ends here */