More of previous.
[bpt/emacs.git] / lib-src / b2m.c
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
8 * Gnu emacs. At least now I can read all my
9 * Gnumacs Babyl format mail files!
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
18 /* Made conformant to the GNU coding standards January, 1995
19 by Francesco Potorti` <pot@cnuce.cnr.it>. */
20
21 #include <stdio.h>
22 #include <time.h>
23 #include <sys/types.h>
24 #include <getopt.h>
25 #ifdef MSDOS
26 #include <fcntl.h>
27 #endif
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.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. */
33 #undef static
34 #endif
35
36 #ifdef STDC_HEADERS
37 #include <stdlib.h>
38 #endif
39
40 #undef TRUE
41 #define TRUE 1
42 #undef FALSE
43 #define FALSE 0
44
45 /* Exit codes for success and failure. */
46 #ifdef VMS
47 #define GOOD 1
48 #define BAD 0
49 #else
50 #define GOOD 0
51 #define BAD 1
52 #endif
53
54 #define streq(s,t) (strcmp (s, t) == 0)
55 #define strneq(s,t,n) (strncmp (s, t, n) == 0)
56
57 typedef int logical;
58
59 /*
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.
63 */
64 struct linebuffer
65 {
66 long size;
67 char *buffer;
68 };
69
70 extern char *strtok();
71
72 long *xmalloc (), *xrealloc ();
73 char *concat ();
74 long readline ();
75 void fatal ();
76
77 /*
78 * xnew -- allocate storage. SYNOPSIS: Type *xnew (int n, Type);
79 */
80 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type)))
81
82
83
84 char *progname;
85
86 struct option longopts[] =
87 {
88 { "help", no_argument, NULL, 'h' },
89 { "version", no_argument, NULL, 'V' },
90 { 0 }
91 };
92
93 extern int optind;
94
95 int
96 main (argc, argv)
97 int argc;
98 char **argv;
99 {
100 logical labels_saved, printing, header;
101 time_t ltoday;
102 char *labels, *p, *today;
103 struct linebuffer data;
104
105 #ifdef MSDOS
106 _fmode = O_BINARY; /* all of files are treated as binary files */
107 #if __DJGPP__ > 1
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 */
116 #endif
117 progname = argv[0];
118
119 while (1)
120 {
121 int opt = getopt_long (argc, argv, "hV", longopts, 0);
122 if (opt == EOF)
123 break;
124
125 switch (opt)
126 {
127 case 'V':
128 printf ("%s (GNU Emacs %s)\n", "b2m", VERSION);
129 puts ("b2m is in the public domain.");
130 exit (GOOD);
131
132 case 'h':
133 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
134 exit (GOOD);
135 }
136 }
137
138 if (optind != argc)
139 {
140 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
141 exit (GOOD);
142 }
143
144 labels_saved = printing = header = FALSE;
145 ltoday = time (0);
146 today = ctime (&ltoday);
147 data.size = 200;
148 data.buffer = xnew (200, char);
149
150 if (readline (&data, stdin) == 0
151 || !strneq (data.buffer, "BABYL OPTIONS:", 14))
152 fatal ("standard input is not a Babyl mailfile.");
153
154 while (readline (&data, stdin) > 0)
155 {
156 if (streq (data.buffer, "*** EOOH ***") && !printing)
157 {
158 printing = header = TRUE;
159 printf ("From \"Babyl to mail by %s\" %s", progname, today);
160 continue;
161 }
162
163 if (data.buffer[0] == '\037')
164 {
165 if (data.buffer[1] == '\0')
166 continue;
167 else if (data.buffer[1] == '\f')
168 {
169 /* Save labels. */
170 readline (&data, stdin);
171 p = strtok (data.buffer, " ,\r\n\t");
172 labels = "X-Babyl-Labels: ";
173
174 while (p = strtok (NULL, " ,\r\n\t"))
175 labels = concat (labels, p, ", ");
176
177 p = &labels[strlen (labels) - 2];
178 if (*p == ',')
179 *p = '\0';
180 printing = header = FALSE;
181 labels_saved = TRUE;
182 continue;
183 }
184 }
185
186 if ((data.buffer[0] == '\0') && header)
187 {
188 header = FALSE;
189 if (labels_saved)
190 puts (labels);
191 }
192
193 if (printing)
194 puts (data.buffer);
195 }
196 }
197
198
199
200 /*
201 * Return a newly-allocated string whose contents
202 * concatenate those of s1, s2, s3.
203 */
204 char *
205 concat (s1, s2, s3)
206 char *s1, *s2, *s3;
207 {
208 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
209 char *result = xnew (len1 + len2 + len3 + 1, char);
210
211 strcpy (result, s1);
212 strcpy (result + len1, s2);
213 strcpy (result + len1 + len2, s3);
214 result[len1 + len2 + len3] = '\0';
215
216 return result;
217 }
218
219 /*
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.
223 */
224 long
225 readline (linebuffer, stream)
226 struct linebuffer *linebuffer;
227 register FILE *stream;
228 {
229 char *buffer = linebuffer->buffer;
230 register char *p = linebuffer->buffer;
231 register char *pend;
232 int chars_deleted;
233
234 pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */
235
236 while (1)
237 {
238 register int c = getc (stream);
239 if (p == pend)
240 {
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;
246 }
247 if (c == EOF)
248 {
249 *p = '\0';
250 chars_deleted = 0;
251 break;
252 }
253 if (c == '\n')
254 {
255 if (p > buffer && p[-1] == '\r')
256 {
257 *--p = '\0';
258 chars_deleted = 2;
259 }
260 else
261 {
262 *p = '\0';
263 chars_deleted = 1;
264 }
265 break;
266 }
267 *p++ = c;
268 }
269
270 return (p - buffer + chars_deleted);
271 }
272
273 /*
274 * Like malloc but get fatal error if memory is exhausted.
275 */
276 long *
277 xmalloc (size)
278 unsigned int size;
279 {
280 long *result = (long *) malloc (size);
281 if (result == NULL)
282 fatal ("virtual memory exhausted");
283 return result;
284 }
285
286 long *
287 xrealloc (ptr, size)
288 char *ptr;
289 unsigned int size;
290 {
291 long *result = (long *) realloc (ptr, size);
292 if (result == NULL)
293 fatal ("virtual memory exhausted");
294 return result;
295 }
296
297 void
298 fatal (message)
299 char *message;
300 {
301 fprintf (stderr, "%s: %s\n", progname, message);
302 exit (BAD);
303 }
304