emerge.el: Move the `defvar's to the top level.
[emacs.git] / lib-src / b2m.c
blob5bebe560e2a6d187512b2d4904758f87b08e823f
1 /*
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!
14 * Ed Wilkinson
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>. */
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
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. */
26 #undef static
27 #endif
29 #include <stdio.h>
30 #include <time.h>
31 #include <sys/types.h>
32 #include <getopt.h>
33 #ifdef MSDOS
34 #include <fcntl.h>
35 #endif
37 #undef TRUE
38 #define TRUE 1
39 #undef FALSE
40 #define FALSE 0
42 #define streq(s,t) (strcmp (s, t) == 0)
43 #define strneq(s,t,n) (strncmp (s, t, n) == 0)
45 typedef int logical;
48 * A `struct linebuffer' is a structure which holds a line of text.
49 * `readline' reads a line from a stream into a linebuffer and works
50 * regardless of the length of the line.
52 struct linebuffer
54 long size;
55 char *buffer;
58 extern char *strtok();
60 long *xmalloc (), *xrealloc ();
61 char *concat ();
62 long readline ();
63 void fatal ();
66 * xnew -- allocate storage. SYNOPSIS: Type *xnew (int n, Type);
68 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type)))
72 char *progname;
74 struct option longopts[] =
76 { "help", no_argument, NULL, 'h' },
77 { "version", no_argument, NULL, 'V' },
78 { 0 }
81 extern int optind;
83 int
84 main (argc, argv)
85 int argc;
86 char **argv;
88 logical labels_saved, printing, header;
89 time_t ltoday;
90 char *labels, *p, *today;
91 struct linebuffer data;
93 #ifdef MSDOS
94 _fmode = O_BINARY; /* all of files are treated as binary files */
95 #if __DJGPP__ > 1
96 if (!isatty (fileno (stdout)))
97 setmode (fileno (stdout), O_BINARY);
98 if (!isatty (fileno (stdin)))
99 setmode (fileno (stdin), O_BINARY);
100 #else /* not __DJGPP__ > 1 */
101 (stdout)->_flag &= ~_IOTEXT;
102 (stdin)->_flag &= ~_IOTEXT;
103 #endif /* not __DJGPP__ > 1 */
104 #endif
105 progname = argv[0];
107 while (1)
109 int opt = getopt_long (argc, argv, "hV", longopts, 0);
110 if (opt == EOF)
111 break;
113 switch (opt)
115 case 'V':
116 printf ("%s (GNU Emacs %s)\n", "b2m", VERSION);
117 puts ("b2m is in the public domain.");
118 exit (EXIT_SUCCESS);
120 case 'h':
121 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
122 exit (EXIT_SUCCESS);
126 if (optind != argc)
128 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
129 exit (EXIT_SUCCESS);
132 labels_saved = printing = header = FALSE;
133 ltoday = time (0);
134 today = ctime (&ltoday);
135 data.size = 200;
136 data.buffer = xnew (200, char);
138 if (readline (&data, stdin) == 0
139 || !strneq (data.buffer, "BABYL OPTIONS:", 14))
140 fatal ("standard input is not a Babyl mailfile.");
142 while (readline (&data, stdin) > 0)
144 if (streq (data.buffer, "*** EOOH ***") && !printing)
146 printing = header = TRUE;
147 printf ("From \"Babyl to mail by %s\" %s", progname, today);
148 continue;
151 if (data.buffer[0] == '\037')
153 if (data.buffer[1] == '\0')
154 continue;
155 else if (data.buffer[1] == '\f')
157 /* Save labels. */
158 readline (&data, stdin);
159 p = strtok (data.buffer, " ,\r\n\t");
160 labels = "X-Babyl-Labels: ";
162 while ((p = strtok (NULL, " ,\r\n\t")))
163 labels = concat (labels, p, ", ");
165 p = &labels[strlen (labels) - 2];
166 if (*p == ',')
167 *p = '\0';
168 printing = header = FALSE;
169 labels_saved = TRUE;
170 continue;
174 if ((data.buffer[0] == '\0') && header)
176 header = FALSE;
177 if (labels_saved)
178 puts (labels);
181 if (printing)
182 puts (data.buffer);
185 return EXIT_SUCCESS;
191 * Return a newly-allocated string whose contents
192 * concatenate those of s1, s2, s3.
194 char *
195 concat (s1, s2, s3)
196 char *s1, *s2, *s3;
198 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
199 char *result = xnew (len1 + len2 + len3 + 1, char);
201 strcpy (result, s1);
202 strcpy (result + len1, s2);
203 strcpy (result + len1 + len2, s3);
204 result[len1 + len2 + len3] = '\0';
206 return result;
210 * Read a line of text from `stream' into `linebuffer'.
211 * Return the number of characters read from `stream',
212 * which is the length of the line including the newline, if any.
214 long
215 readline (linebuffer, stream)
216 struct linebuffer *linebuffer;
217 register FILE *stream;
219 char *buffer = linebuffer->buffer;
220 register char *p = linebuffer->buffer;
221 register char *pend;
222 int chars_deleted;
224 pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */
226 while (1)
228 register int c = getc (stream);
229 if (p == pend)
231 linebuffer->size *= 2;
232 buffer = (char *) xrealloc (buffer, linebuffer->size);
233 p += buffer - linebuffer->buffer;
234 pend = buffer + linebuffer->size;
235 linebuffer->buffer = buffer;
237 if (c == EOF)
239 *p = '\0';
240 chars_deleted = 0;
241 break;
243 if (c == '\n')
245 if (p > buffer && p[-1] == '\r')
247 *--p = '\0';
248 chars_deleted = 2;
250 else
252 *p = '\0';
253 chars_deleted = 1;
255 break;
257 *p++ = c;
260 return (p - buffer + chars_deleted);
264 * Like malloc but get fatal error if memory is exhausted.
266 long *
267 xmalloc (size)
268 unsigned int size;
270 long *result = (long *) malloc (size);
271 if (result == NULL)
272 fatal ("virtual memory exhausted");
273 return result;
276 long *
277 xrealloc (ptr, size)
278 char *ptr;
279 unsigned int size;
281 long *result = (long *) realloc (ptr, size);
282 if (result == NULL)
283 fatal ("virtual memory exhausted");
284 return result;
287 void
288 fatal (message)
289 char *message;
291 fprintf (stderr, "%s: %s\n", progname, message);
292 exit (EXIT_FAILURE);
295 /* arch-tag: 5a3ad2af-a802-408f-83cc-e7cf5e98653e
296 (do not change this comment) */
298 /* b2m.c ends here */