(timeout-event-p): Function deleted.
[emacs.git] / lib-src / b2m.c
blobd0e41a6ae937683bdac6e431fa13d33ac5cd99e1
1 /*
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!
13 * Ed Wilkinson
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>. */
21 #include <stdio.h>
22 #include <time.h>
23 #include <sys/types.h>
24 #ifdef MSDOS
25 #include <fcntl.h>
26 #endif
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 /* On some systems, Emacs defines static as nothing for the sake
31 of unexec. We don't want that here since we don't use unexec. */
32 #undef static
33 #endif
35 #undef TRUE
36 #define TRUE 1
37 #undef FALSE
38 #define FALSE 0
40 /* Exit codes for success and failure. */
41 #ifdef VMS
42 #define GOOD 1
43 #define BAD 0
44 #else
45 #define GOOD 0
46 #define BAD 1
47 #endif
49 #define streq(s,t) (strcmp (s, t) == 0)
50 #define strneq(s,t,n) (strncmp (s, t, n) == 0)
52 typedef int logical;
55 * A `struct linebuffer' is a structure which holds a line of text.
56 * `readline' reads a line from a stream into a linebuffer and works
57 * regardless of the length of the line.
59 struct linebuffer
61 long size;
62 char *buffer;
65 extern char *strtok();
67 long *xmalloc (), *xrealloc ();
68 char *concat ();
69 long readline ();
70 void fatal ();
73 * xnew -- allocate storage. SYNOPSIS: Type *xnew (int n, Type);
75 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type)))
79 char *progname;
81 main (argc, argv)
82 int argc;
83 char **argv;
85 logical labels_saved, printing, header;
86 time_t ltoday;
87 char *labels, *p, *today;
88 struct linebuffer data;
90 #ifdef MSDOS
91 _fmode = O_BINARY; /* all of files are treated as binary files */
92 (stdout)->_flag &= ~_IOTEXT;
93 (stdin)->_flag &= ~_IOTEXT;
94 #endif
95 if (argc != 1)
97 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
98 exit (GOOD);
100 labels_saved = printing = header = FALSE;
101 progname = argv[0];
102 ltoday = time (0);
103 today = ctime (&ltoday);
104 data.size = 200;
105 data.buffer = xnew (200, char);
107 if (readline (&data, stdin) == 0
108 || !strneq (data.buffer, "BABYL OPTIONS:", 14))
109 fatal ("standard input is not a Babyl mailfile.");
111 while (readline (&data, stdin) > 0)
113 if (streq (data.buffer, "*** EOOH ***") && !printing)
115 printing = header = TRUE;
116 printf ("From Babyl to mail by %s %s", progname, today);
117 continue;
120 if (data.buffer[0] == '\037')
122 if (data.buffer[1] == '\0')
123 continue;
124 else if (data.buffer[1] == '\f')
126 /* Save labels. */
127 readline (&data, stdin);
128 p = strtok (data.buffer, " ,\r\n\t");
129 labels = "X-Babyl-Labels: ";
131 while (p = strtok (NULL, " ,\r\n\t"))
132 labels = concat (labels, p, ", ");
134 p = &labels[strlen (labels) - 2];
135 if (*p == ',')
136 *p = '\0';
137 printing = header = FALSE;
138 labels_saved = TRUE;
139 continue;
143 if ((data.buffer[0] == '\0') && header)
145 header = FALSE;
146 if (labels_saved)
147 puts (labels);
150 if (printing)
151 puts (data.buffer);
158 * Return a newly-allocated string whose contents
159 * concatenate those of s1, s2, s3.
161 char *
162 concat (s1, s2, s3)
163 char *s1, *s2, *s3;
165 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
166 char *result = xnew (len1 + len2 + len3 + 1, char);
168 strcpy (result, s1);
169 strcpy (result + len1, s2);
170 strcpy (result + len1 + len2, s3);
171 result[len1 + len2 + len3] = '\0';
173 return result;
177 * Read a line of text from `stream' into `linebuffer'.
178 * Return the number of characters read from `stream',
179 * which is the length of the line including the newline, if any.
181 long
182 readline (linebuffer, stream)
183 struct linebuffer *linebuffer;
184 register FILE *stream;
186 char *buffer = linebuffer->buffer;
187 register char *p = linebuffer->buffer;
188 register char *pend;
189 int chars_deleted;
191 pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */
193 while (1)
195 register int c = getc (stream);
196 if (p == pend)
198 linebuffer->size *= 2;
199 buffer = (char *) xrealloc (buffer, linebuffer->size);
200 p += buffer - linebuffer->buffer;
201 pend = buffer + linebuffer->size;
202 linebuffer->buffer = buffer;
204 if (c == EOF)
206 chars_deleted = 0;
207 break;
209 if (c == '\n')
211 if (p[-1] == '\r' && p > buffer)
213 *--p = '\0';
214 chars_deleted = 2;
216 else
218 *p = '\0';
219 chars_deleted = 1;
221 break;
223 *p++ = c;
226 return (p - buffer + chars_deleted);
230 * Like malloc but get fatal error if memory is exhausted.
232 long *
233 xmalloc (size)
234 unsigned int size;
236 long *result = (long *) malloc (size);
237 if (result == NULL)
238 fatal ("virtual memory exhausted");
239 return result;
242 long *
243 xrealloc (ptr, size)
244 char *ptr;
245 unsigned int size;
247 long *result = (long *) realloc (ptr, size);
248 if (result == NULL)
249 fatal ("virtual memory exhausted");
250 return result;
253 void
254 fatal (message)
256 fprintf (stderr, "%s: %s\n", progname, message);
257 exit (BAD);