(cl-do-arglist): Revert change of
[emacs.git] / lib-src / b2m.c
blob044ebedb21149cda2ecd120dd111a2944060423f
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 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 /* On some systems, Emacs defines static as nothing for the sake
24 of unexec. We don't want that here since we don't use unexec. */
25 #undef static
26 #endif
28 #include <stdio.h>
29 #include <time.h>
30 #include <sys/types.h>
31 #include <getopt.h>
32 #ifdef MSDOS
33 #include <fcntl.h>
34 #endif
36 #undef TRUE
37 #define TRUE 1
38 #undef FALSE
39 #define FALSE 0
41 /* Exit codes for success and failure. */
42 #ifdef VMS
43 #define GOOD 1
44 #define BAD 0
45 #else
46 #define GOOD 0
47 #define BAD 1
48 #endif
50 #define streq(s,t) (strcmp (s, t) == 0)
51 #define strneq(s,t,n) (strncmp (s, t, n) == 0)
53 typedef int logical;
56 * A `struct linebuffer' is a structure which holds a line of text.
57 * `readline' reads a line from a stream into a linebuffer and works
58 * regardless of the length of the line.
60 struct linebuffer
62 long size;
63 char *buffer;
66 extern char *strtok();
68 long *xmalloc (), *xrealloc ();
69 char *concat ();
70 long readline ();
71 void fatal ();
74 * xnew -- allocate storage. SYNOPSIS: Type *xnew (int n, Type);
76 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type)))
80 char *progname;
82 struct option longopts[] =
84 { "help", no_argument, NULL, 'h' },
85 { "version", no_argument, NULL, 'V' },
86 { 0 }
89 extern int optind;
91 int
92 main (argc, argv)
93 int argc;
94 char **argv;
96 logical labels_saved, printing, header;
97 time_t ltoday;
98 char *labels, *p, *today;
99 struct linebuffer data;
101 #ifdef MSDOS
102 _fmode = O_BINARY; /* all of files are treated as binary files */
103 #if __DJGPP__ > 1
104 if (!isatty (fileno (stdout)))
105 setmode (fileno (stdout), O_BINARY);
106 if (!isatty (fileno (stdin)))
107 setmode (fileno (stdin), O_BINARY);
108 #else /* not __DJGPP__ > 1 */
109 (stdout)->_flag &= ~_IOTEXT;
110 (stdin)->_flag &= ~_IOTEXT;
111 #endif /* not __DJGPP__ > 1 */
112 #endif
113 progname = argv[0];
115 while (1)
117 int opt = getopt_long (argc, argv, "hV", longopts, 0);
118 if (opt == EOF)
119 break;
121 switch (opt)
123 case 'V':
124 printf ("%s (GNU Emacs %s)\n", "b2m", VERSION);
125 puts ("b2m is in the public domain.");
126 exit (GOOD);
128 case 'h':
129 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
130 exit (GOOD);
134 if (optind != argc)
136 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
137 exit (GOOD);
140 labels_saved = printing = header = FALSE;
141 ltoday = time (0);
142 today = ctime (&ltoday);
143 data.size = 200;
144 data.buffer = xnew (200, char);
146 if (readline (&data, stdin) == 0
147 || !strneq (data.buffer, "BABYL OPTIONS:", 14))
148 fatal ("standard input is not a Babyl mailfile.");
150 while (readline (&data, stdin) > 0)
152 if (streq (data.buffer, "*** EOOH ***") && !printing)
154 printing = header = TRUE;
155 printf ("From \"Babyl to mail by %s\" %s", progname, today);
156 continue;
159 if (data.buffer[0] == '\037')
161 if (data.buffer[1] == '\0')
162 continue;
163 else if (data.buffer[1] == '\f')
165 /* Save labels. */
166 readline (&data, stdin);
167 p = strtok (data.buffer, " ,\r\n\t");
168 labels = "X-Babyl-Labels: ";
170 while (p = strtok (NULL, " ,\r\n\t"))
171 labels = concat (labels, p, ", ");
173 p = &labels[strlen (labels) - 2];
174 if (*p == ',')
175 *p = '\0';
176 printing = header = FALSE;
177 labels_saved = TRUE;
178 continue;
182 if ((data.buffer[0] == '\0') && header)
184 header = FALSE;
185 if (labels_saved)
186 puts (labels);
189 if (printing)
190 puts (data.buffer);
193 return 0;
199 * Return a newly-allocated string whose contents
200 * concatenate those of s1, s2, s3.
202 char *
203 concat (s1, s2, s3)
204 char *s1, *s2, *s3;
206 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
207 char *result = xnew (len1 + len2 + len3 + 1, char);
209 strcpy (result, s1);
210 strcpy (result + len1, s2);
211 strcpy (result + len1 + len2, s3);
212 result[len1 + len2 + len3] = '\0';
214 return result;
218 * Read a line of text from `stream' into `linebuffer'.
219 * Return the number of characters read from `stream',
220 * which is the length of the line including the newline, if any.
222 long
223 readline (linebuffer, stream)
224 struct linebuffer *linebuffer;
225 register FILE *stream;
227 char *buffer = linebuffer->buffer;
228 register char *p = linebuffer->buffer;
229 register char *pend;
230 int chars_deleted;
232 pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */
234 while (1)
236 register int c = getc (stream);
237 if (p == pend)
239 linebuffer->size *= 2;
240 buffer = (char *) xrealloc (buffer, linebuffer->size);
241 p += buffer - linebuffer->buffer;
242 pend = buffer + linebuffer->size;
243 linebuffer->buffer = buffer;
245 if (c == EOF)
247 *p = '\0';
248 chars_deleted = 0;
249 break;
251 if (c == '\n')
253 if (p > buffer && p[-1] == '\r')
255 *--p = '\0';
256 chars_deleted = 2;
258 else
260 *p = '\0';
261 chars_deleted = 1;
263 break;
265 *p++ = c;
268 return (p - buffer + chars_deleted);
272 * Like malloc but get fatal error if memory is exhausted.
274 long *
275 xmalloc (size)
276 unsigned int size;
278 long *result = (long *) malloc (size);
279 if (result == NULL)
280 fatal ("virtual memory exhausted");
281 return result;
284 long *
285 xrealloc (ptr, size)
286 char *ptr;
287 unsigned int size;
289 long *result = (long *) realloc (ptr, size);
290 if (result == NULL)
291 fatal ("virtual memory exhausted");
292 return result;
295 void
296 fatal (message)
297 char *message;
299 fprintf (stderr, "%s: %s\n", progname, message);
300 exit (BAD);