(save-match-data): Use save-match-data-internal
[emacs.git] / lib-src / b2m.c
blob88d0acd5cd89549e328e2828e8f5bf2c7fb42e26
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 #if __DJGPP__ > 1
93 if (!isatty (fileno (stdout)))
94 setmode (fileno (stdout), O_BINARY);
95 if (!isatty (fileno (stdin)))
96 setmode (fileno (stdin), O_BINARY);
97 #else /* not __DJGPP__ > 1 */
98 (stdout)->_flag &= ~_IOTEXT;
99 (stdin)->_flag &= ~_IOTEXT;
100 #endif /* not __DJGPP__ > 1 */
101 #endif
102 progname = argv[0];
104 if (argc != 1)
106 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
107 exit (GOOD);
109 labels_saved = printing = header = FALSE;
110 ltoday = time (0);
111 today = ctime (&ltoday);
112 data.size = 200;
113 data.buffer = xnew (200, char);
115 if (readline (&data, stdin) == 0
116 || !strneq (data.buffer, "BABYL OPTIONS:", 14))
117 fatal ("standard input is not a Babyl mailfile.");
119 while (readline (&data, stdin) > 0)
121 if (streq (data.buffer, "*** EOOH ***") && !printing)
123 printing = header = TRUE;
124 printf ("From \"Babyl to mail by %s\" %s", progname, today);
125 continue;
128 if (data.buffer[0] == '\037')
130 if (data.buffer[1] == '\0')
131 continue;
132 else if (data.buffer[1] == '\f')
134 /* Save labels. */
135 readline (&data, stdin);
136 p = strtok (data.buffer, " ,\r\n\t");
137 labels = "X-Babyl-Labels: ";
139 while (p = strtok (NULL, " ,\r\n\t"))
140 labels = concat (labels, p, ", ");
142 p = &labels[strlen (labels) - 2];
143 if (*p == ',')
144 *p = '\0';
145 printing = header = FALSE;
146 labels_saved = TRUE;
147 continue;
151 if ((data.buffer[0] == '\0') && header)
153 header = FALSE;
154 if (labels_saved)
155 puts (labels);
158 if (printing)
159 puts (data.buffer);
166 * Return a newly-allocated string whose contents
167 * concatenate those of s1, s2, s3.
169 char *
170 concat (s1, s2, s3)
171 char *s1, *s2, *s3;
173 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
174 char *result = xnew (len1 + len2 + len3 + 1, char);
176 strcpy (result, s1);
177 strcpy (result + len1, s2);
178 strcpy (result + len1 + len2, s3);
179 result[len1 + len2 + len3] = '\0';
181 return result;
185 * Read a line of text from `stream' into `linebuffer'.
186 * Return the number of characters read from `stream',
187 * which is the length of the line including the newline, if any.
189 long
190 readline (linebuffer, stream)
191 struct linebuffer *linebuffer;
192 register FILE *stream;
194 char *buffer = linebuffer->buffer;
195 register char *p = linebuffer->buffer;
196 register char *pend;
197 int chars_deleted;
199 pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */
201 while (1)
203 register int c = getc (stream);
204 if (p == pend)
206 linebuffer->size *= 2;
207 buffer = (char *) xrealloc (buffer, linebuffer->size);
208 p += buffer - linebuffer->buffer;
209 pend = buffer + linebuffer->size;
210 linebuffer->buffer = buffer;
212 if (c == EOF)
214 chars_deleted = 0;
215 break;
217 if (c == '\n')
219 if (p[-1] == '\r' && p > buffer)
221 *--p = '\0';
222 chars_deleted = 2;
224 else
226 *p = '\0';
227 chars_deleted = 1;
229 break;
231 *p++ = c;
234 return (p - buffer + chars_deleted);
238 * Like malloc but get fatal error if memory is exhausted.
240 long *
241 xmalloc (size)
242 unsigned int size;
244 long *result = (long *) malloc (size);
245 if (result == NULL)
246 fatal ("virtual memory exhausted");
247 return result;
250 long *
251 xrealloc (ptr, size)
252 char *ptr;
253 unsigned int size;
255 long *result = (long *) realloc (ptr, size);
256 if (result == NULL)
257 fatal ("virtual memory exhausted");
258 return result;
261 void
262 fatal (message)
264 fprintf (stderr, "%s: %s\n", progname, message);
265 exit (BAD);