Add detailed instructions to unpack and install intlfonts on MS-DOS.
[emacs.git] / lib-src / b2m.c
blob6a2fcff9a219f3cbf9587187addf10100adc0212
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 #include <getopt.h>
25 #ifdef MSDOS
26 #include <fcntl.h>
27 #endif
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
36 #ifdef STDC_HEADERS
37 #include <stdlib.h>
38 #endif
40 #undef TRUE
41 #define TRUE 1
42 #undef FALSE
43 #define FALSE 0
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
54 #define streq(s,t) (strcmp (s, t) == 0)
55 #define strneq(s,t,n) (strncmp (s, t, n) == 0)
57 typedef int logical;
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.
64 struct linebuffer
66 long size;
67 char *buffer;
70 extern char *strtok();
72 long *xmalloc (), *xrealloc ();
73 char *concat ();
74 long readline ();
75 void fatal ();
78 * xnew -- allocate storage. SYNOPSIS: Type *xnew (int n, Type);
80 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type)))
84 char *progname;
86 struct option longopts[] =
88 { "help", no_argument, NULL, 'h' },
89 { "version", no_argument, NULL, 'V' },
90 { 0 }
93 extern int optind;
95 int
96 main (argc, argv)
97 int argc;
98 char **argv;
100 logical labels_saved, printing, header;
101 time_t ltoday;
102 char *labels, *p, *today;
103 struct linebuffer data;
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];
119 while (1)
121 int opt = getopt_long (argc, argv, "hV", longopts, 0);
122 if (opt == EOF)
123 break;
125 switch (opt)
127 case 'V':
128 printf ("%s (GNU Emacs %s)\n", "b2m", VERSION);
129 puts ("b2m is in the public domain.");
130 exit (GOOD);
132 case 'h':
133 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
134 exit (GOOD);
138 if (optind != argc)
140 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
141 exit (GOOD);
144 labels_saved = printing = header = FALSE;
145 ltoday = time (0);
146 today = ctime (&ltoday);
147 data.size = 200;
148 data.buffer = xnew (200, char);
150 if (readline (&data, stdin) == 0
151 || !strneq (data.buffer, "BABYL OPTIONS:", 14))
152 fatal ("standard input is not a Babyl mailfile.");
154 while (readline (&data, stdin) > 0)
156 if (streq (data.buffer, "*** EOOH ***") && !printing)
158 printing = header = TRUE;
159 printf ("From \"Babyl to mail by %s\" %s", progname, today);
160 continue;
163 if (data.buffer[0] == '\037')
165 if (data.buffer[1] == '\0')
166 continue;
167 else if (data.buffer[1] == '\f')
169 /* Save labels. */
170 readline (&data, stdin);
171 p = strtok (data.buffer, " ,\r\n\t");
172 labels = "X-Babyl-Labels: ";
174 while (p = strtok (NULL, " ,\r\n\t"))
175 labels = concat (labels, p, ", ");
177 p = &labels[strlen (labels) - 2];
178 if (*p == ',')
179 *p = '\0';
180 printing = header = FALSE;
181 labels_saved = TRUE;
182 continue;
186 if ((data.buffer[0] == '\0') && header)
188 header = FALSE;
189 if (labels_saved)
190 puts (labels);
193 if (printing)
194 puts (data.buffer);
201 * Return a newly-allocated string whose contents
202 * concatenate those of s1, s2, s3.
204 char *
205 concat (s1, s2, s3)
206 char *s1, *s2, *s3;
208 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
209 char *result = xnew (len1 + len2 + len3 + 1, char);
211 strcpy (result, s1);
212 strcpy (result + len1, s2);
213 strcpy (result + len1 + len2, s3);
214 result[len1 + len2 + len3] = '\0';
216 return result;
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.
224 long
225 readline (linebuffer, stream)
226 struct linebuffer *linebuffer;
227 register FILE *stream;
229 char *buffer = linebuffer->buffer;
230 register char *p = linebuffer->buffer;
231 register char *pend;
232 int chars_deleted;
234 pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */
236 while (1)
238 register int c = getc (stream);
239 if (p == pend)
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;
247 if (c == EOF)
249 *p = '\0';
250 chars_deleted = 0;
251 break;
253 if (c == '\n')
255 if (p > buffer && p[-1] == '\r')
257 *--p = '\0';
258 chars_deleted = 2;
260 else
262 *p = '\0';
263 chars_deleted = 1;
265 break;
267 *p++ = c;
270 return (p - buffer + chars_deleted);
274 * Like malloc but get fatal error if memory is exhausted.
276 long *
277 xmalloc (size)
278 unsigned int size;
280 long *result = (long *) malloc (size);
281 if (result == NULL)
282 fatal ("virtual memory exhausted");
283 return result;
286 long *
287 xrealloc (ptr, size)
288 char *ptr;
289 unsigned int size;
291 long *result = (long *) realloc (ptr, size);
292 if (result == NULL)
293 fatal ("virtual memory exhausted");
294 return result;
297 void
298 fatal (message)
299 char *message;
301 fprintf (stderr, "%s: %s\n", progname, message);
302 exit (BAD);