]>
code.delx.au - gnu-emacs/blob - lib-src/b2m.c
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>
31 /* On some systems, Emacs defines static as nothing for the sake
32 of unexec. We don't want that here since we don't use unexec. */
45 /* Exit codes for success and failure. */
54 #define streq(s,t) (strcmp (s, t) == 0)
55 #define strneq(s,t,n) (strncmp (s, t, n) == 0)
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
;
102 char *labels
, *p
, *today
;
103 struct linebuffer data
;
106 _fmode
= O_BINARY
; /* all of files are treated as binary files */
108 if (!isatty (fileno (stdout
)))
109 setmode (fileno (stdout
), O_BINARY
);
110 if (!isatty (fileno (stdin
)))
111 setmode (fileno (stdin
), O_BINARY
);
112 #else /* not __DJGPP__ > 1 */
113 (stdout
)->_flag
&= ~_IOTEXT
;
114 (stdin
)->_flag
&= ~_IOTEXT
;
115 #endif /* not __DJGPP__ > 1 */
121 int opt
= getopt_long (argc
, argv
, "hV", longopts
, 0);
128 printf ("%s (GNU Emacs %s)\n", "b2m", VERSION
);
129 puts ("b2m is in the public domain.");
133 fprintf (stderr
, "Usage: %s <babylmailbox >unixmailbox\n", progname
);
140 fprintf (stderr
, "Usage: %s <babylmailbox >unixmailbox\n", progname
);
144 labels_saved
= printing
= header
= FALSE
;
146 today
= ctime (<oday
);
148 data
.buffer
= xnew (200, char);
150 if (readline (&data
, stdin
) == 0
151 || !strneq (data
.buffer
, "BABYL OPTIONS:", 14))
152 fatal ("standard input is not a Babyl mailfile.");
154 while (readline (&data
, stdin
) > 0)
156 if (streq (data
.buffer
, "*** EOOH ***") && !printing
)
158 printing
= header
= TRUE
;
159 printf ("From \"Babyl to mail by %s\" %s", progname
, today
);
163 if (data
.buffer
[0] == '\037')
165 if (data
.buffer
[1] == '\0')
167 else if (data
.buffer
[1] == '\f')
170 readline (&data
, stdin
);
171 p
= strtok (data
.buffer
, " ,\r\n\t");
172 labels
= "X-Babyl-Labels: ";
174 while (p
= strtok (NULL
, " ,\r\n\t"))
175 labels
= concat (labels
, p
, ", ");
177 p
= &labels
[strlen (labels
) - 2];
180 printing
= header
= FALSE
;
186 if ((data
.buffer
[0] == '\0') && header
)
201 * Return a newly-allocated string whose contents
202 * concatenate those of s1, s2, s3.
208 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
209 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
212 strcpy (result
+ len1
, s2
);
213 strcpy (result
+ len1
+ len2
, s3
);
214 result
[len1
+ len2
+ len3
] = '\0';
220 * Read a line of text from `stream' into `linebuffer'.
221 * Return the number of characters read from `stream',
222 * which is the length of the line including the newline, if any.
225 readline (linebuffer
, stream
)
226 struct linebuffer
*linebuffer
;
227 register FILE *stream
;
229 char *buffer
= linebuffer
->buffer
;
230 register char *p
= linebuffer
->buffer
;
234 pend
= p
+ linebuffer
->size
; /* Separate to avoid 386/IX compiler bug. */
238 register int c
= getc (stream
);
241 linebuffer
->size
*= 2;
242 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
243 p
+= buffer
- linebuffer
->buffer
;
244 pend
= buffer
+ linebuffer
->size
;
245 linebuffer
->buffer
= buffer
;
255 if (p
> buffer
&& p
[-1] == '\r')
270 return (p
- buffer
+ chars_deleted
);
274 * Like malloc but get fatal error if memory is exhausted.
280 long *result
= (long *) malloc (size
);
282 fatal ("virtual memory exhausted");
291 long *result
= (long *) realloc (ptr
, size
);
293 fatal ("virtual memory exhausted");
301 fprintf (stderr
, "%s: %s\n", progname
, message
);