Properly spell the name of Emacs.
[bpt/emacs.git] / lib-src / b2m.c
CommitLineData
cc9940c7
JB
1/*
2 * b2m - a filter for Babyl -> Unix mail files
3 *
4 * usage: b2m < babyl > mailbox
5 *
6 * I find this useful whenever I have to use a
7 * system which - shock horror! - doesn't run
37a9305e
PJ
8 * GNU Emacs. At least now I can read all my
9 * GNU Emacs Babyl format mail files!
cc9940c7
JB
10 *
11 * it's not much but it's free!
12 *
13 * Ed Wilkinson
14 * E.Wilkinson@massey.ac.nz
15 * Mon Nov 7 15:54:06 PDT 1988
16 */
17
e19bdc14
RS
18/* Made conformant to the GNU coding standards January, 1995
19 by Francesco Potorti` <pot@cnuce.cnr.it>. */
20
e19bdc14
RS
21#ifdef HAVE_CONFIG_H
22#include <config.h>
23/* On some systems, Emacs defines static as nothing for the sake
24 of unexec. We don't want that here since we don't use unexec. */
25#undef static
26#endif
fbfed6f0 27
4ee9629e
PE
28#include <stdio.h>
29#include <time.h>
30#include <sys/types.h>
31#include <getopt.h>
32#ifdef MSDOS
33#include <fcntl.h>
1f8c2f55
AS
34#endif
35
e19bdc14
RS
36#undef TRUE
37#define TRUE 1
38#undef FALSE
39#define FALSE 0
cc9940c7 40
e19bdc14
RS
41/* Exit codes for success and failure. */
42#ifdef VMS
43#define GOOD 1
44#define BAD 0
45#else
46#define GOOD 0
47#define BAD 1
cbc61077 48#endif
cc9940c7 49
e19bdc14
RS
50#define streq(s,t) (strcmp (s, t) == 0)
51#define strneq(s,t,n) (strncmp (s, t, n) == 0)
52
53typedef int logical;
54
55/*
56 * A `struct linebuffer' is a structure which holds a line of text.
57 * `readline' reads a line from a stream into a linebuffer and works
58 * regardless of the length of the line.
59 */
60struct linebuffer
61{
62 long size;
63 char *buffer;
64};
65
66extern char *strtok();
128ba46c 67
0404b62c 68long *xmalloc (), *xrealloc ();
e19bdc14
RS
69char *concat ();
70long readline ();
71void fatal ();
72
73/*
74 * xnew -- allocate storage. SYNOPSIS: Type *xnew (int n, Type);
75 */
76#define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type)))
77
78
79
80char *progname;
cc9940c7 81
e4b34a85
KH
82struct option longopts[] =
83{
84 { "help", no_argument, NULL, 'h' },
85 { "version", no_argument, NULL, 'V' },
86 { 0 }
87};
88
89extern int optind;
90
1f8c2f55 91int
5d13f393
JB
92main (argc, argv)
93 int argc;
94 char **argv;
cc9940c7 95{
e19bdc14
RS
96 logical labels_saved, printing, header;
97 time_t ltoday;
98 char *labels, *p, *today;
99 struct linebuffer data;
100
c6880c90 101#ifdef MSDOS
e3515d10 102 _fmode = O_BINARY; /* all of files are treated as binary files */
18198bb2
RS
103#if __DJGPP__ > 1
104 if (!isatty (fileno (stdout)))
105 setmode (fileno (stdout), O_BINARY);
106 if (!isatty (fileno (stdin)))
107 setmode (fileno (stdin), O_BINARY);
108#else /* not __DJGPP__ > 1 */
c6880c90
RS
109 (stdout)->_flag &= ~_IOTEXT;
110 (stdin)->_flag &= ~_IOTEXT;
18198bb2 111#endif /* not __DJGPP__ > 1 */
c6880c90 112#endif
d4843060
RS
113 progname = argv[0];
114
e4b34a85
KH
115 while (1)
116 {
117 int opt = getopt_long (argc, argv, "hV", longopts, 0);
118 if (opt == EOF)
119 break;
120
121 switch (opt)
122 {
123 case 'V':
124 printf ("%s (GNU Emacs %s)\n", "b2m", VERSION);
125 puts ("b2m is in the public domain.");
126 exit (GOOD);
127
128 case 'h':
129 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
130 exit (GOOD);
131 }
132 }
133
134 if (optind != argc)
88d00c7e 135 {
e19bdc14
RS
136 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
137 exit (GOOD);
88d00c7e 138 }
e4b34a85 139
e19bdc14 140 labels_saved = printing = header = FALSE;
e3515d10
RS
141 ltoday = time (0);
142 today = ctime (&ltoday);
e19bdc14
RS
143 data.size = 200;
144 data.buffer = xnew (200, char);
cc9940c7 145
e19bdc14
RS
146 if (readline (&data, stdin) == 0
147 || !strneq (data.buffer, "BABYL OPTIONS:", 14))
148 fatal ("standard input is not a Babyl mailfile.");
cc9940c7 149
e19bdc14 150 while (readline (&data, stdin) > 0)
e3515d10 151 {
e19bdc14 152 if (streq (data.buffer, "*** EOOH ***") && !printing)
e3515d10
RS
153 {
154 printing = header = TRUE;
d4843060 155 printf ("From \"Babyl to mail by %s\" %s", progname, today);
e3515d10
RS
156 continue;
157 }
88d00c7e 158
e19bdc14 159 if (data.buffer[0] == '\037')
e3515d10 160 {
e19bdc14
RS
161 if (data.buffer[1] == '\0')
162 continue;
163 else if (data.buffer[1] == '\f')
e3515d10 164 {
e19bdc14
RS
165 /* Save labels. */
166 readline (&data, stdin);
167 p = strtok (data.buffer, " ,\r\n\t");
168 labels = "X-Babyl-Labels: ";
cc9940c7 169
e19bdc14
RS
170 while (p = strtok (NULL, " ,\r\n\t"))
171 labels = concat (labels, p, ", ");
172
7a804c76
KH
173 p = &labels[strlen (labels) - 2];
174 if (*p == ',')
175 *p = '\0';
e19bdc14
RS
176 printing = header = FALSE;
177 labels_saved = TRUE;
178 continue;
179 }
e3515d10 180 }
cc9940c7 181
e19bdc14 182 if ((data.buffer[0] == '\0') && header)
e3515d10
RS
183 {
184 header = FALSE;
e19bdc14 185 if (labels_saved)
e3515d10
RS
186 puts (labels);
187 }
e19bdc14 188
e3515d10 189 if (printing)
e19bdc14
RS
190 puts (data.buffer);
191 }
3a213356
GM
192
193 return 0;
e19bdc14
RS
194}
195
196
197
198/*
199 * Return a newly-allocated string whose contents
200 * concatenate those of s1, s2, s3.
201 */
202char *
203concat (s1, s2, s3)
204 char *s1, *s2, *s3;
205{
206 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
207 char *result = xnew (len1 + len2 + len3 + 1, char);
208
209 strcpy (result, s1);
210 strcpy (result + len1, s2);
211 strcpy (result + len1 + len2, s3);
212 result[len1 + len2 + len3] = '\0';
213
214 return result;
215}
216
217/*
218 * Read a line of text from `stream' into `linebuffer'.
219 * Return the number of characters read from `stream',
220 * which is the length of the line including the newline, if any.
221 */
222long
223readline (linebuffer, stream)
224 struct linebuffer *linebuffer;
225 register FILE *stream;
226{
227 char *buffer = linebuffer->buffer;
228 register char *p = linebuffer->buffer;
229 register char *pend;
230 int chars_deleted;
231
232 pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */
233
234 while (1)
235 {
236 register int c = getc (stream);
237 if (p == pend)
238 {
239 linebuffer->size *= 2;
240 buffer = (char *) xrealloc (buffer, linebuffer->size);
241 p += buffer - linebuffer->buffer;
242 pend = buffer + linebuffer->size;
243 linebuffer->buffer = buffer;
244 }
245 if (c == EOF)
246 {
5326c1d6 247 *p = '\0';
e19bdc14
RS
248 chars_deleted = 0;
249 break;
250 }
251 if (c == '\n')
252 {
5326c1d6 253 if (p > buffer && p[-1] == '\r')
e19bdc14
RS
254 {
255 *--p = '\0';
256 chars_deleted = 2;
257 }
258 else
259 {
260 *p = '\0';
261 chars_deleted = 1;
262 }
263 break;
264 }
265 *p++ = c;
e3515d10 266 }
e19bdc14
RS
267
268 return (p - buffer + chars_deleted);
cc9940c7 269}
e19bdc14
RS
270
271/*
272 * Like malloc but get fatal error if memory is exhausted.
273 */
0404b62c 274long *
e19bdc14
RS
275xmalloc (size)
276 unsigned int size;
277{
0404b62c 278 long *result = (long *) malloc (size);
e19bdc14
RS
279 if (result == NULL)
280 fatal ("virtual memory exhausted");
281 return result;
282}
283
0404b62c 284long *
e19bdc14
RS
285xrealloc (ptr, size)
286 char *ptr;
287 unsigned int size;
288{
0404b62c 289 long *result = (long *) realloc (ptr, size);
e19bdc14
RS
290 if (result == NULL)
291 fatal ("virtual memory exhausted");
292 return result;
293}
294
295void
296fatal (message)
5c0b76c1 297 char *message;
e19bdc14
RS
298{
299 fprintf (stderr, "%s: %s\n", progname, message);
300 exit (BAD);
301}
302