(face-try-color-list): Treat `underline' as valid.
[bpt/emacs.git] / lib-src / b2m.c
1 /*
2 * b2m - a filter for Babyl -> Unix mail files
3 *
4 * usage: b2m < babyl > mailbox
5 *
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!
10 *
11 * it's not much but it's free!
12 *
13 * Ed Wilkinson
14 * E.Wilkinson@massey.ac.nz
15 * Mon Nov 7 15:54:06 PDT 1988
16 */
17
18 /* Made conformant to the GNU coding standards January, 1995
19 by Francesco Potorti` <pot@cnuce.cnr.it>. */
20
21 #include <stdio.h>
22 #include <time.h>
23 #include <sys/types.h>
24 #ifdef MSDOS
25 #include <fcntl.h>
26 #endif
27
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
34
35 #undef TRUE
36 #define TRUE 1
37 #undef FALSE
38 #define FALSE 0
39
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
48
49 #define streq(s,t) (strcmp (s, t) == 0)
50 #define strneq(s,t,n) (strncmp (s, t, n) == 0)
51
52 typedef int logical;
53
54 /*
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.
58 */
59 struct linebuffer
60 {
61 long size;
62 char *buffer;
63 };
64
65 extern char *strtok();
66
67 char *xmalloc (), *xrealloc ();
68 char *concat ();
69 long readline ();
70 void fatal ();
71
72 /*
73 * xnew -- allocate storage. SYNOPSIS: Type *xnew (int n, Type);
74 */
75 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type)))
76
77
78
79 char *progname;
80
81 main (argc, argv)
82 int argc;
83 char **argv;
84 {
85 logical labels_saved, printing, header;
86 time_t ltoday;
87 char *labels, *p, *today;
88 struct linebuffer data;
89
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)
96 {
97 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
98 exit (GOOD);
99 }
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);
106
107 if (readline (&data, stdin) == 0
108 || !strneq (data.buffer, "BABYL OPTIONS:", 14))
109 fatal ("standard input is not a Babyl mailfile.");
110
111 while (readline (&data, stdin) > 0)
112 {
113 if (streq (data.buffer, "*** EOOH ***") && !printing)
114 {
115 printing = header = TRUE;
116 printf ("From Babyl to mail by %s %s", progname, today);
117 continue;
118 }
119
120 if (data.buffer[0] == '\037')
121 {
122 if (data.buffer[1] == '\0')
123 continue;
124 else if (data.buffer[1] == '\f')
125 {
126 /* Save labels. */
127 readline (&data, stdin);
128 p = strtok (data.buffer, " ,\r\n\t");
129 labels = "X-Babyl-Labels: ";
130
131 while (p = strtok (NULL, " ,\r\n\t"))
132 labels = concat (labels, p, ", ");
133
134 labels[strlen (labels) - 2] = '\0';
135 printing = header = FALSE;
136 labels_saved = TRUE;
137 continue;
138 }
139 }
140
141 if ((data.buffer[0] == '\0') && header)
142 {
143 header = FALSE;
144 if (labels_saved)
145 puts (labels);
146 }
147
148 if (printing)
149 puts (data.buffer);
150 }
151 }
152
153
154
155 /*
156 * Return a newly-allocated string whose contents
157 * concatenate those of s1, s2, s3.
158 */
159 char *
160 concat (s1, s2, s3)
161 char *s1, *s2, *s3;
162 {
163 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
164 char *result = xnew (len1 + len2 + len3 + 1, char);
165
166 strcpy (result, s1);
167 strcpy (result + len1, s2);
168 strcpy (result + len1 + len2, s3);
169 result[len1 + len2 + len3] = '\0';
170
171 return result;
172 }
173
174 /*
175 * Read a line of text from `stream' into `linebuffer'.
176 * Return the number of characters read from `stream',
177 * which is the length of the line including the newline, if any.
178 */
179 long
180 readline (linebuffer, stream)
181 struct linebuffer *linebuffer;
182 register FILE *stream;
183 {
184 char *buffer = linebuffer->buffer;
185 register char *p = linebuffer->buffer;
186 register char *pend;
187 int chars_deleted;
188
189 pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */
190
191 while (1)
192 {
193 register int c = getc (stream);
194 if (p == pend)
195 {
196 linebuffer->size *= 2;
197 buffer = (char *) xrealloc (buffer, linebuffer->size);
198 p += buffer - linebuffer->buffer;
199 pend = buffer + linebuffer->size;
200 linebuffer->buffer = buffer;
201 }
202 if (c == EOF)
203 {
204 chars_deleted = 0;
205 break;
206 }
207 if (c == '\n')
208 {
209 if (p[-1] == '\r' && p > buffer)
210 {
211 *--p = '\0';
212 chars_deleted = 2;
213 }
214 else
215 {
216 *p = '\0';
217 chars_deleted = 1;
218 }
219 break;
220 }
221 *p++ = c;
222 }
223
224 return (p - buffer + chars_deleted);
225 }
226
227 /*
228 * Like malloc but get fatal error if memory is exhausted.
229 */
230 char *
231 xmalloc (size)
232 unsigned int size;
233 {
234 char *result = (char *) malloc (size);
235 if (result == NULL)
236 fatal ("virtual memory exhausted");
237 return result;
238 }
239
240 char *
241 xrealloc (ptr, size)
242 char *ptr;
243 unsigned int size;
244 {
245 char *result = (char *) realloc (ptr, size);
246 if (result == NULL)
247 fatal ("virtual memory exhausted");
248 return result;
249 }
250
251 void
252 fatal (message)
253 {
254 fprintf (stderr, "%s: %s\n", progname, message);
255 exit (BAD);
256 }
257