(copy_overlays): Use EMACS_INT for positions.
[emacs.git] / lib-src / b2m.c
blob05caa1424e0046eb362f1f121d9b0b85c7c4ef10
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 /* Exit codes for success and failure. */
43 #ifdef VMS
44 #define GOOD 1
45 #define BAD 0
46 #else
47 #define GOOD 0
48 #define BAD 1
49 #endif
51 #define streq(s,t) (strcmp (s, t) == 0)
52 #define strneq(s,t,n) (strncmp (s, t, n) == 0)
54 typedef int logical;
57 * A `struct linebuffer' is a structure which holds a line of text.
58 * `readline' reads a line from a stream into a linebuffer and works
59 * regardless of the length of the line.
61 struct linebuffer
63 long size;
64 char *buffer;
67 extern char *strtok();
69 long *xmalloc (), *xrealloc ();
70 char *concat ();
71 long readline ();
72 void fatal ();
75 * xnew -- allocate storage. SYNOPSIS: Type *xnew (int n, Type);
77 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type)))
81 char *progname;
83 struct option longopts[] =
85 { "help", no_argument, NULL, 'h' },
86 { "version", no_argument, NULL, 'V' },
87 { 0 }
90 extern int optind;
92 int
93 main (argc, argv)
94 int argc;
95 char **argv;
97 logical labels_saved, printing, header;
98 time_t ltoday;
99 char *labels, *p, *today;
100 struct linebuffer data;
102 #ifdef MSDOS
103 _fmode = O_BINARY; /* all of files are treated as binary files */
104 #if __DJGPP__ > 1
105 if (!isatty (fileno (stdout)))
106 setmode (fileno (stdout), O_BINARY);
107 if (!isatty (fileno (stdin)))
108 setmode (fileno (stdin), O_BINARY);
109 #else /* not __DJGPP__ > 1 */
110 (stdout)->_flag &= ~_IOTEXT;
111 (stdin)->_flag &= ~_IOTEXT;
112 #endif /* not __DJGPP__ > 1 */
113 #endif
114 progname = argv[0];
116 while (1)
118 int opt = getopt_long (argc, argv, "hV", longopts, 0);
119 if (opt == EOF)
120 break;
122 switch (opt)
124 case 'V':
125 printf ("%s (GNU Emacs %s)\n", "b2m", VERSION);
126 puts ("b2m is in the public domain.");
127 exit (GOOD);
129 case 'h':
130 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
131 exit (GOOD);
135 if (optind != argc)
137 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
138 exit (GOOD);
141 labels_saved = printing = header = FALSE;
142 ltoday = time (0);
143 today = ctime (&ltoday);
144 data.size = 200;
145 data.buffer = xnew (200, char);
147 if (readline (&data, stdin) == 0
148 || !strneq (data.buffer, "BABYL OPTIONS:", 14))
149 fatal ("standard input is not a Babyl mailfile.");
151 while (readline (&data, stdin) > 0)
153 if (streq (data.buffer, "*** EOOH ***") && !printing)
155 printing = header = TRUE;
156 printf ("From \"Babyl to mail by %s\" %s", progname, today);
157 continue;
160 if (data.buffer[0] == '\037')
162 if (data.buffer[1] == '\0')
163 continue;
164 else if (data.buffer[1] == '\f')
166 /* Save labels. */
167 readline (&data, stdin);
168 p = strtok (data.buffer, " ,\r\n\t");
169 labels = "X-Babyl-Labels: ";
171 while ((p = strtok (NULL, " ,\r\n\t")))
172 labels = concat (labels, p, ", ");
174 p = &labels[strlen (labels) - 2];
175 if (*p == ',')
176 *p = '\0';
177 printing = header = FALSE;
178 labels_saved = TRUE;
179 continue;
183 if ((data.buffer[0] == '\0') && header)
185 header = FALSE;
186 if (labels_saved)
187 puts (labels);
190 if (printing)
191 puts (data.buffer);
194 return 0;
200 * Return a newly-allocated string whose contents
201 * concatenate those of s1, s2, s3.
203 char *
204 concat (s1, s2, s3)
205 char *s1, *s2, *s3;
207 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
208 char *result = xnew (len1 + len2 + len3 + 1, char);
210 strcpy (result, s1);
211 strcpy (result + len1, s2);
212 strcpy (result + len1 + len2, s3);
213 result[len1 + len2 + len3] = '\0';
215 return result;
219 * Read a line of text from `stream' into `linebuffer'.
220 * Return the number of characters read from `stream',
221 * which is the length of the line including the newline, if any.
223 long
224 readline (linebuffer, stream)
225 struct linebuffer *linebuffer;
226 register FILE *stream;
228 char *buffer = linebuffer->buffer;
229 register char *p = linebuffer->buffer;
230 register char *pend;
231 int chars_deleted;
233 pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */
235 while (1)
237 register int c = getc (stream);
238 if (p == pend)
240 linebuffer->size *= 2;
241 buffer = (char *) xrealloc (buffer, linebuffer->size);
242 p += buffer - linebuffer->buffer;
243 pend = buffer + linebuffer->size;
244 linebuffer->buffer = buffer;
246 if (c == EOF)
248 *p = '\0';
249 chars_deleted = 0;
250 break;
252 if (c == '\n')
254 if (p > buffer && p[-1] == '\r')
256 *--p = '\0';
257 chars_deleted = 2;
259 else
261 *p = '\0';
262 chars_deleted = 1;
264 break;
266 *p++ = c;
269 return (p - buffer + chars_deleted);
273 * Like malloc but get fatal error if memory is exhausted.
275 long *
276 xmalloc (size)
277 unsigned int size;
279 long *result = (long *) malloc (size);
280 if (result == NULL)
281 fatal ("virtual memory exhausted");
282 return result;
285 long *
286 xrealloc (ptr, size)
287 char *ptr;
288 unsigned int size;
290 long *result = (long *) realloc (ptr, size);
291 if (result == NULL)
292 fatal ("virtual memory exhausted");
293 return result;
296 void
297 fatal (message)
298 char *message;
300 fprintf (stderr, "%s: %s\n", progname, message);
301 exit (BAD);