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. */
32 #include <sys/types.h>
43 #define streq(s,t) (strcmp (s, t) == 0)
44 #define strneq(s,t,n) (strncmp (s, t, n) == 0)
48 /* True if TM_YEAR is a struct tm's tm_year value that is acceptable
49 to asctime. Glibc asctime returns a useful string unless TM_YEAR
50 is nearly INT_MAX, but the C Standard lets C libraries overrun a
51 buffer if TM_YEAR needs more than 4 bytes. */
53 # define TM_YEAR_IN_ASCTIME_RANGE(tm_year) ((tm_year) <= INT_MAX - 1900)
55 # define TM_YEAR_IN_ASCTIME_RANGE(tm_year) \
56 (-999 - 1900 <= (tm_year) && (tm_year) <= 9999 - 1900)
60 * A `struct linebuffer' is a structure which holds a line of text.
61 * `readline' reads a line from a stream into a linebuffer and works
62 * regardless of the length of the line.
70 extern char *strtok();
72 long *xmalloc (), *xrealloc ();
78 * xnew -- allocate storage. SYNOPSIS: Type *xnew (int n, Type);
80 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type)))
86 struct option longopts
[] =
88 { "help", no_argument
, NULL
, 'h' },
89 { "version", no_argument
, NULL
, 'V' },
100 logical labels_saved
, printing
, header
;
103 char *labels
, *p
, *today
;
104 struct linebuffer data
;
107 _fmode
= O_BINARY
; /* all of files are treated as binary files */
109 if (!isatty (fileno (stdout
)))
110 setmode (fileno (stdout
), O_BINARY
);
111 if (!isatty (fileno (stdin
)))
112 setmode (fileno (stdin
), O_BINARY
);
113 #else /* not __DJGPP__ > 1 */
114 (stdout
)->_flag
&= ~_IOTEXT
;
115 (stdin
)->_flag
&= ~_IOTEXT
;
116 #endif /* not __DJGPP__ > 1 */
122 int opt
= getopt_long (argc
, argv
, "hV", longopts
, 0);
129 printf ("%s (GNU Emacs %s)\n", "b2m", VERSION
);
130 puts ("b2m is in the public domain.");
134 fprintf (stderr
, "Usage: %s <babylmailbox >unixmailbox\n", progname
);
141 fprintf (stderr
, "Usage: %s <babylmailbox >unixmailbox\n", progname
);
145 labels_saved
= printing
= header
= FALSE
;
147 /* Convert to a string, checking for out-of-range time stamps.
148 Don't use 'ctime', as that might dump core if the hardware clock
149 is set to a bizarre value. */
150 tm
= localtime (<oday
);
151 if (! (tm
&& TM_YEAR_IN_ASCTIME_RANGE (tm
->tm_year
)))
152 fatal ("current time is out of range");
153 today
= asctime (tm
);
155 data
.buffer
= xnew (200, char);
157 if (readline (&data
, stdin
) == 0
158 || !strneq (data
.buffer
, "BABYL OPTIONS:", 14))
159 fatal ("standard input is not a Babyl mailfile.");
161 while (readline (&data
, stdin
) > 0)
163 if (streq (data
.buffer
, "*** EOOH ***") && !printing
)
165 printing
= header
= TRUE
;
166 printf ("From \"Babyl to mail by %s\" %s", progname
, today
);
170 if (data
.buffer
[0] == '\037')
172 if (data
.buffer
[1] == '\0')
174 else if (data
.buffer
[1] == '\f')
177 readline (&data
, stdin
);
178 p
= strtok (data
.buffer
, " ,\r\n\t");
179 labels
= "X-Babyl-Labels: ";
181 while ((p
= strtok (NULL
, " ,\r\n\t")))
182 labels
= concat (labels
, p
, ", ");
184 p
= &labels
[strlen (labels
) - 2];
187 printing
= header
= FALSE
;
193 if ((data
.buffer
[0] == '\0') && header
)
210 * Return a newly-allocated string whose contents
211 * concatenate those of s1, s2, s3.
217 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
218 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
221 strcpy (result
+ len1
, s2
);
222 strcpy (result
+ len1
+ len2
, s3
);
223 result
[len1
+ len2
+ len3
] = '\0';
229 * Read a line of text from `stream' into `linebuffer'.
230 * Return the number of characters read from `stream',
231 * which is the length of the line including the newline, if any.
234 readline (linebuffer
, stream
)
235 struct linebuffer
*linebuffer
;
236 register FILE *stream
;
238 char *buffer
= linebuffer
->buffer
;
239 register char *p
= linebuffer
->buffer
;
243 pend
= p
+ linebuffer
->size
; /* Separate to avoid 386/IX compiler bug. */
247 register int c
= getc (stream
);
250 linebuffer
->size
*= 2;
251 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
252 p
+= buffer
- linebuffer
->buffer
;
253 pend
= buffer
+ linebuffer
->size
;
254 linebuffer
->buffer
= buffer
;
264 if (p
> buffer
&& p
[-1] == '\r')
279 return (p
- buffer
+ chars_deleted
);
283 * Like malloc but get fatal error if memory is exhausted.
289 long *result
= (long *) malloc (size
);
291 fatal ("virtual memory exhausted");
300 long *result
= (long *) realloc (ptr
, size
);
302 fatal ("virtual memory exhausted");
310 fprintf (stderr
, "%s: %s\n", progname
, message
);
314 /* arch-tag: 5a3ad2af-a802-408f-83cc-e7cf5e98653e
315 (do not change this comment) */
317 /* b2m.c ends here */