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)
47 #define TM_YEAR_BASE 1900
49 /* Nonzero if TM_YEAR is a struct tm's tm_year value that causes
50 asctime to have well-defined behavior. */
51 #ifndef TM_YEAR_IN_ASCTIME_RANGE
52 # define TM_YEAR_IN_ASCTIME_RANGE(tm_year) \
53 (1000 - TM_YEAR_BASE <= (tm_year) && (tm_year) <= 9999 - TM_YEAR_BASE)
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 (unsigned int size
);
70 long *xrealloc (char *ptr
, unsigned int size
);
71 char *concat (char *s1
, char *s2
, char *s3
);
72 long readline (struct linebuffer
*linebuffer
, register FILE *stream
);
73 void fatal (char *message
);
76 * xnew -- allocate storage. SYNOPSIS: Type *xnew (int n, Type);
78 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type)))
84 struct option longopts
[] =
86 { "help", no_argument
, NULL
, 'h' },
87 { "version", no_argument
, NULL
, 'V' },
98 logical labels_saved
, printing
, header
, first
, last_was_blank_line
;
101 char *labels
, *p
, *today
;
102 struct linebuffer data
;
105 _fmode
= O_BINARY
; /* all of files are treated as binary files */
107 if (!isatty (fileno (stdout
)))
108 setmode (fileno (stdout
), O_BINARY
);
109 if (!isatty (fileno (stdin
)))
110 setmode (fileno (stdin
), O_BINARY
);
111 #else /* not __DJGPP__ > 1 */
112 (stdout
)->_flag
&= ~_IOTEXT
;
113 (stdin
)->_flag
&= ~_IOTEXT
;
114 #endif /* not __DJGPP__ > 1 */
120 int opt
= getopt_long (argc
, argv
, "hV", longopts
, 0);
127 printf ("%s (GNU Emacs %s)\n", "b2m", VERSION
);
128 puts ("b2m is in the public domain.");
132 fprintf (stderr
, "Usage: %s <babylmailbox >unixmailbox\n", progname
);
139 fprintf (stderr
, "Usage: %s <babylmailbox >unixmailbox\n", progname
);
143 labels_saved
= printing
= header
= last_was_blank_line
= FALSE
;
146 /* Convert to a string, checking for out-of-range time stamps.
147 Don't use 'ctime', as that might dump core if the hardware clock
148 is set to a bizarre value. */
149 tm
= localtime (<oday
);
150 if (! (tm
&& TM_YEAR_IN_ASCTIME_RANGE (tm
->tm_year
)
151 && (today
= asctime (tm
))))
152 fatal ("current time is out of range");
154 data
.buffer
= xnew (200, char);
156 if (readline (&data
, stdin
) == 0
157 || !strneq (data
.buffer
, "BABYL OPTIONS:", 14))
158 fatal ("standard input is not a Babyl mailfile.");
160 while (readline (&data
, stdin
) > 0)
162 if (streq (data
.buffer
, "*** EOOH ***") && !printing
)
164 printing
= header
= TRUE
;
165 printf ("From \"Babyl to mail by %s\" %s", progname
, today
);
169 if (data
.buffer
[0] == '\037')
171 if (data
.buffer
[1] == '\0')
173 else if (data
.buffer
[1] == '\f')
177 else if (! last_was_blank_line
)
180 readline (&data
, stdin
);
181 p
= strtok (data
.buffer
, " ,\r\n\t");
182 labels
= "X-Babyl-Labels: ";
184 while ((p
= strtok (NULL
, " ,\r\n\t")))
185 labels
= concat (labels
, p
, ", ");
187 p
= &labels
[strlen (labels
) - 2];
190 printing
= header
= FALSE
;
196 if ((data
.buffer
[0] == '\0') && header
)
206 if (data
.buffer
[0] == '\0')
207 last_was_blank_line
= TRUE
;
209 last_was_blank_line
= FALSE
;
219 * Return a newly-allocated string whose contents
220 * concatenate those of s1, s2, s3.
226 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
227 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
230 strcpy (result
+ len1
, s2
);
231 strcpy (result
+ len1
+ len2
, s3
);
232 result
[len1
+ len2
+ len3
] = '\0';
238 * Read a line of text from `stream' into `linebuffer'.
239 * Return the number of characters read from `stream',
240 * which is the length of the line including the newline, if any.
243 readline (linebuffer
, stream
)
244 struct linebuffer
*linebuffer
;
245 register FILE *stream
;
247 char *buffer
= linebuffer
->buffer
;
248 register char *p
= linebuffer
->buffer
;
252 pend
= p
+ linebuffer
->size
; /* Separate to avoid 386/IX compiler bug. */
256 register int c
= getc (stream
);
259 linebuffer
->size
*= 2;
260 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
261 p
+= buffer
- linebuffer
->buffer
;
262 pend
= buffer
+ linebuffer
->size
;
263 linebuffer
->buffer
= buffer
;
273 if (p
> buffer
&& p
[-1] == '\r')
288 return (p
- buffer
+ chars_deleted
);
292 * Like malloc but get fatal error if memory is exhausted.
298 long *result
= (long *) malloc (size
);
300 fatal ("virtual memory exhausted");
309 long *result
= (long *) realloc (ptr
, size
);
311 fatal ("virtual memory exhausted");
319 fprintf (stderr
, "%s: %s\n", progname
, message
);
323 /* arch-tag: 5a3ad2af-a802-408f-83cc-e7cf5e98653e
324 (do not change this comment) */
326 /* b2m.c ends here */