440
|
1 /*
|
|
2 * b2m - a filter for Babyl -> Unix mail files
|
42260
|
3 * The copyright on this file has been disclaimed.
|
440
|
4 *
|
|
5 * usage: b2m < babyl > mailbox
|
|
6 *
|
|
7 * I find this useful whenever I have to use a
|
|
8 * system which - shock horror! - doesn't run
|
40067
|
9 * GNU Emacs. At least now I can read all my
|
|
10 * GNU Emacs Babyl format mail files!
|
440
|
11 *
|
|
12 * it's not much but it's free!
|
|
13 *
|
|
14 * Ed Wilkinson
|
|
15 * E.Wilkinson@massey.ac.nz
|
|
16 * Mon Nov 7 15:54:06 PDT 1988
|
|
17 */
|
|
18
|
10370
|
19 /* Made conformant to the GNU coding standards January, 1995
|
|
20 by Francesco Potorti` <pot@cnuce.cnr.it>. */
|
|
21
|
|
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
|
440
|
28
|
26083
|
29 #include <stdio.h>
|
|
30 #include <time.h>
|
|
31 #include <sys/types.h>
|
|
32 #include <getopt.h>
|
|
33 #ifdef MSDOS
|
|
34 #include <fcntl.h>
|
21386
|
35 #endif
|
|
36
|
10370
|
37 #undef TRUE
|
|
38 #define TRUE 1
|
|
39 #undef FALSE
|
|
40 #define FALSE 0
|
|
41
|
|
42 #define streq(s,t) (strcmp (s, t) == 0)
|
|
43 #define strneq(s,t,n) (strncmp (s, t, n) == 0)
|
|
44
|
|
45 typedef int logical;
|
|
46
|
|
47 /*
|
|
48 * A `struct linebuffer' is a structure which holds a line of text.
|
|
49 * `readline' reads a line from a stream into a linebuffer and works
|
|
50 * regardless of the length of the line.
|
|
51 */
|
|
52 struct linebuffer
|
|
53 {
|
|
54 long size;
|
|
55 char *buffer;
|
|
56 };
|
8957
|
57
|
10370
|
58 extern char *strtok();
|
|
59
|
11674
|
60 long *xmalloc (), *xrealloc ();
|
10370
|
61 char *concat ();
|
|
62 long readline ();
|
|
63 void fatal ();
|
440
|
64
|
10370
|
65 /*
|
|
66 * xnew -- allocate storage. SYNOPSIS: Type *xnew (int n, Type);
|
|
67 */
|
|
68 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type)))
|
|
69
|
|
70
|
|
71
|
|
72 char *progname;
|
|
73
|
20328
|
74 struct option longopts[] =
|
|
75 {
|
|
76 { "help", no_argument, NULL, 'h' },
|
|
77 { "version", no_argument, NULL, 'V' },
|
|
78 { 0 }
|
|
79 };
|
|
80
|
|
81 extern int optind;
|
|
82
|
21386
|
83 int
|
2101
|
84 main (argc, argv)
|
|
85 int argc;
|
|
86 char **argv;
|
440
|
87 {
|
10370
|
88 logical labels_saved, printing, header;
|
|
89 time_t ltoday;
|
|
90 char *labels, *p, *today;
|
|
91 struct linebuffer data;
|
|
92
|
5448
|
93 #ifdef MSDOS
|
6215
|
94 _fmode = O_BINARY; /* all of files are treated as binary files */
|
14968
|
95 #if __DJGPP__ > 1
|
|
96 if (!isatty (fileno (stdout)))
|
|
97 setmode (fileno (stdout), O_BINARY);
|
|
98 if (!isatty (fileno (stdin)))
|
|
99 setmode (fileno (stdin), O_BINARY);
|
|
100 #else /* not __DJGPP__ > 1 */
|
5448
|
101 (stdout)->_flag &= ~_IOTEXT;
|
|
102 (stdin)->_flag &= ~_IOTEXT;
|
14968
|
103 #endif /* not __DJGPP__ > 1 */
|
5448
|
104 #endif
|
14850
|
105 progname = argv[0];
|
|
106
|
20328
|
107 while (1)
|
|
108 {
|
|
109 int opt = getopt_long (argc, argv, "hV", longopts, 0);
|
|
110 if (opt == EOF)
|
|
111 break;
|
|
112
|
|
113 switch (opt)
|
|
114 {
|
|
115 case 'V':
|
|
116 printf ("%s (GNU Emacs %s)\n", "b2m", VERSION);
|
|
117 puts ("b2m is in the public domain.");
|
55408
|
118 exit (EXIT_SUCCESS);
|
20328
|
119
|
|
120 case 'h':
|
|
121 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
|
55408
|
122 exit (EXIT_SUCCESS);
|
20328
|
123 }
|
|
124 }
|
|
125
|
|
126 if (optind != argc)
|
6173
|
127 {
|
10370
|
128 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
|
55408
|
129 exit (EXIT_SUCCESS);
|
6173
|
130 }
|
20328
|
131
|
10370
|
132 labels_saved = printing = header = FALSE;
|
6215
|
133 ltoday = time (0);
|
|
134 today = ctime (<oday);
|
10370
|
135 data.size = 200;
|
|
136 data.buffer = xnew (200, char);
|
440
|
137
|
10370
|
138 if (readline (&data, stdin) == 0
|
|
139 || !strneq (data.buffer, "BABYL OPTIONS:", 14))
|
|
140 fatal ("standard input is not a Babyl mailfile.");
|
|
141
|
|
142 while (readline (&data, stdin) > 0)
|
6215
|
143 {
|
10370
|
144 if (streq (data.buffer, "*** EOOH ***") && !printing)
|
6215
|
145 {
|
|
146 printing = header = TRUE;
|
14850
|
147 printf ("From \"Babyl to mail by %s\" %s", progname, today);
|
6215
|
148 continue;
|
|
149 }
|
6173
|
150
|
10370
|
151 if (data.buffer[0] == '\037')
|
6215
|
152 {
|
10370
|
153 if (data.buffer[1] == '\0')
|
|
154 continue;
|
|
155 else if (data.buffer[1] == '\f')
|
|
156 {
|
|
157 /* Save labels. */
|
|
158 readline (&data, stdin);
|
|
159 p = strtok (data.buffer, " ,\r\n\t");
|
|
160 labels = "X-Babyl-Labels: ";
|
440
|
161
|
42471
4234f54994ef
(main): Parenthesize assignment when used as truth value to prevent gcc
Pavel Janík <Pavel@Janik.cz>
diff
changeset
|
162 while ((p = strtok (NULL, " ,\r\n\t")))
|
10370
|
163 labels = concat (labels, p, ", ");
|
|
164
|
11423
|
165 p = &labels[strlen (labels) - 2];
|
|
166 if (*p == ',')
|
|
167 *p = '\0';
|
10370
|
168 printing = header = FALSE;
|
|
169 labels_saved = TRUE;
|
|
170 continue;
|
6215
|
171 }
|
10370
|
172 }
|
440
|
173
|
10370
|
174 if ((data.buffer[0] == '\0') && header)
|
|
175 {
|
|
176 header = FALSE;
|
|
177 if (labels_saved)
|
|
178 puts (labels);
|
6215
|
179 }
|
440
|
180
|
10370
|
181 if (printing)
|
|
182 puts (data.buffer);
|
|
183 }
|
37163
|
184
|
55408
|
185 return EXIT_SUCCESS;
|
10370
|
186 }
|
|
187
|
|
188
|
|
189
|
|
190 /*
|
|
191 * Return a newly-allocated string whose contents
|
|
192 * concatenate those of s1, s2, s3.
|
|
193 */
|
|
194 char *
|
|
195 concat (s1, s2, s3)
|
|
196 char *s1, *s2, *s3;
|
|
197 {
|
|
198 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
|
|
199 char *result = xnew (len1 + len2 + len3 + 1, char);
|
|
200
|
|
201 strcpy (result, s1);
|
|
202 strcpy (result + len1, s2);
|
|
203 strcpy (result + len1 + len2, s3);
|
|
204 result[len1 + len2 + len3] = '\0';
|
|
205
|
|
206 return result;
|
|
207 }
|
|
208
|
|
209 /*
|
|
210 * Read a line of text from `stream' into `linebuffer'.
|
|
211 * Return the number of characters read from `stream',
|
|
212 * which is the length of the line including the newline, if any.
|
|
213 */
|
|
214 long
|
|
215 readline (linebuffer, stream)
|
|
216 struct linebuffer *linebuffer;
|
|
217 register FILE *stream;
|
|
218 {
|
|
219 char *buffer = linebuffer->buffer;
|
|
220 register char *p = linebuffer->buffer;
|
|
221 register char *pend;
|
|
222 int chars_deleted;
|
|
223
|
|
224 pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */
|
|
225
|
|
226 while (1)
|
|
227 {
|
|
228 register int c = getc (stream);
|
|
229 if (p == pend)
|
6215
|
230 {
|
10370
|
231 linebuffer->size *= 2;
|
|
232 buffer = (char *) xrealloc (buffer, linebuffer->size);
|
|
233 p += buffer - linebuffer->buffer;
|
|
234 pend = buffer + linebuffer->size;
|
|
235 linebuffer->buffer = buffer;
|
6215
|
236 }
|
10370
|
237 if (c == EOF)
|
|
238 {
|
18238
|
239 *p = '\0';
|
10370
|
240 chars_deleted = 0;
|
|
241 break;
|
|
242 }
|
|
243 if (c == '\n')
|
|
244 {
|
18238
|
245 if (p > buffer && p[-1] == '\r')
|
10370
|
246 {
|
|
247 *--p = '\0';
|
|
248 chars_deleted = 2;
|
|
249 }
|
|
250 else
|
|
251 {
|
|
252 *p = '\0';
|
|
253 chars_deleted = 1;
|
|
254 }
|
|
255 break;
|
|
256 }
|
|
257 *p++ = c;
|
440
|
258 }
|
10370
|
259
|
|
260 return (p - buffer + chars_deleted);
|
|
261 }
|
|
262
|
|
263 /*
|
|
264 * Like malloc but get fatal error if memory is exhausted.
|
|
265 */
|
11674
|
266 long *
|
10370
|
267 xmalloc (size)
|
|
268 unsigned int size;
|
|
269 {
|
11674
|
270 long *result = (long *) malloc (size);
|
10370
|
271 if (result == NULL)
|
|
272 fatal ("virtual memory exhausted");
|
|
273 return result;
|
440
|
274 }
|
10370
|
275
|
11674
|
276 long *
|
10370
|
277 xrealloc (ptr, size)
|
|
278 char *ptr;
|
|
279 unsigned int size;
|
|
280 {
|
11674
|
281 long *result = (long *) realloc (ptr, size);
|
10370
|
282 if (result == NULL)
|
|
283 fatal ("virtual memory exhausted");
|
|
284 return result;
|
|
285 }
|
|
286
|
|
287 void
|
|
288 fatal (message)
|
21354
|
289 char *message;
|
10370
|
290 {
|
|
291 fprintf (stderr, "%s: %s\n", progname, message);
|
55408
|
292 exit (EXIT_FAILURE);
|
10370
|
293 }
|
|
294
|
52401
|
295 /* arch-tag: 5a3ad2af-a802-408f-83cc-e7cf5e98653e
|
|
296 (do not change this comment) */
|
55408
|
297
|
|
298 /* b2m.c ends here */
|