]> code.delx.au - gnu-emacs/blob - lib-src/b2m.c
Merge changes from emacs-23 branch.
[gnu-emacs] / lib-src / b2m.c
1 /*
2 * b2m - a filter for Babyl -> Unix mail files
3 * The copyright on this file has been disclaimed.
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
9 * GNU Emacs. At least now I can read all my
10 * GNU Emacs Babyl format mail files!
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
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
28
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
36
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 #define TM_YEAR_BASE 1900
48
49 /* Nonzero if TM_YEAR is a struct tm's tm_year value that causes
50 asctime to have well-defined behavior. */
51 #ifndef TM_YEAR_IN_ASCTIME_RANGE
52 # define TM_YEAR_IN_ASCTIME_RANGE(tm_year) \
53 (1000 - TM_YEAR_BASE <= (tm_year) && (tm_year) <= 9999 - TM_YEAR_BASE)
54 #endif
55
56 /*
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.
60 */
61 struct linebuffer
62 {
63 long size;
64 char *buffer;
65 };
66
67 extern char *strtok(char *, const char *);
68
69 long *xmalloc (unsigned int size);
70 long *xrealloc (char *ptr, unsigned int size);
71 char *concat (char *s1, char *s2, char *s3);
72 long readline (struct linebuffer *linebuffer, register FILE *stream);
73 void fatal (char *message);
74
75 /*
76 * xnew -- allocate storage. SYNOPSIS: Type *xnew (int n, Type);
77 */
78 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type)))
79
80
81
82 char *progname;
83
84 struct option longopts[] =
85 {
86 { "help", no_argument, NULL, 'h' },
87 { "version", no_argument, NULL, 'V' },
88 { 0 }
89 };
90
91 extern int optind;
92
93 int
94 main (int argc, char **argv)
95 {
96 logical labels_saved, printing, header, first, last_was_blank_line;
97 time_t ltoday;
98 struct tm *tm;
99 char *labels, *p, *today;
100 struct linebuffer data;
101
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];
115
116 while (1)
117 {
118 int opt = getopt_long (argc, argv, "hV", longopts, 0);
119 if (opt == EOF)
120 break;
121
122 switch (opt)
123 {
124 case 'V':
125 printf ("%s (GNU Emacs %s)\n", "b2m", VERSION);
126 puts ("b2m is in the public domain.");
127 exit (EXIT_SUCCESS);
128
129 case 'h':
130 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
131 exit (EXIT_SUCCESS);
132 }
133 }
134
135 if (optind != argc)
136 {
137 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
138 exit (EXIT_SUCCESS);
139 }
140
141 labels_saved = printing = header = last_was_blank_line = FALSE;
142 first = TRUE;
143 ltoday = time (0);
144 /* Convert to a string, checking for out-of-range time stamps.
145 Don't use 'ctime', as that might dump core if the hardware clock
146 is set to a bizarre value. */
147 tm = localtime (&ltoday);
148 if (! (tm && TM_YEAR_IN_ASCTIME_RANGE (tm->tm_year)
149 && (today = asctime (tm))))
150 fatal ("current time is out of range");
151 data.size = 200;
152 data.buffer = xnew (200, char);
153
154 if (readline (&data, stdin) == 0
155 || !strneq (data.buffer, "BABYL OPTIONS:", 14))
156 fatal ("standard input is not a Babyl mailfile.");
157
158 while (readline (&data, stdin) > 0)
159 {
160 if (streq (data.buffer, "*** EOOH ***") && !printing)
161 {
162 printing = header = TRUE;
163 printf ("From \"Babyl to mail by %s\" %s", progname, today);
164 continue;
165 }
166
167 if (data.buffer[0] == '\037')
168 {
169 if (data.buffer[1] == '\0')
170 continue;
171 else if (data.buffer[1] == '\f')
172 {
173 if (first)
174 first = FALSE;
175 else if (! last_was_blank_line)
176 puts("");
177 /* Save labels. */
178 readline (&data, stdin);
179 p = strtok (data.buffer, " ,\r\n\t");
180 labels = "X-Babyl-Labels: ";
181
182 while ((p = strtok (NULL, " ,\r\n\t")))
183 labels = concat (labels, p, ", ");
184
185 p = &labels[strlen (labels) - 2];
186 if (*p == ',')
187 *p = '\0';
188 printing = header = FALSE;
189 labels_saved = TRUE;
190 continue;
191 }
192 }
193
194 if ((data.buffer[0] == '\0') && header)
195 {
196 header = FALSE;
197 if (labels_saved)
198 puts (labels);
199 }
200
201 if (printing)
202 {
203 puts (data.buffer);
204 if (data.buffer[0] == '\0')
205 last_was_blank_line = TRUE;
206 else
207 last_was_blank_line = FALSE;
208 }
209 }
210
211 return EXIT_SUCCESS;
212 }
213
214
215
216 /*
217 * Return a newly-allocated string whose contents
218 * concatenate those of s1, s2, s3.
219 */
220 char *
221 concat (char *s1, char *s2, char *s3)
222 {
223 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
224 char *result = xnew (len1 + len2 + len3 + 1, char);
225
226 strcpy (result, s1);
227 strcpy (result + len1, s2);
228 strcpy (result + len1 + len2, s3);
229 result[len1 + len2 + len3] = '\0';
230
231 return result;
232 }
233
234 /*
235 * Read a line of text from `stream' into `linebuffer'.
236 * Return the number of characters read from `stream',
237 * which is the length of the line including the newline, if any.
238 */
239 long
240 readline (struct linebuffer *linebuffer, register FILE *stream)
241 {
242 char *buffer = linebuffer->buffer;
243 register char *p = linebuffer->buffer;
244 register char *pend;
245 int chars_deleted;
246
247 pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */
248
249 while (1)
250 {
251 register int c = getc (stream);
252 if (p == pend)
253 {
254 linebuffer->size *= 2;
255 buffer = (char *) xrealloc (buffer, linebuffer->size);
256 p += buffer - linebuffer->buffer;
257 pend = buffer + linebuffer->size;
258 linebuffer->buffer = buffer;
259 }
260 if (c == EOF)
261 {
262 *p = '\0';
263 chars_deleted = 0;
264 break;
265 }
266 if (c == '\n')
267 {
268 if (p > buffer && p[-1] == '\r')
269 {
270 *--p = '\0';
271 chars_deleted = 2;
272 }
273 else
274 {
275 *p = '\0';
276 chars_deleted = 1;
277 }
278 break;
279 }
280 *p++ = c;
281 }
282
283 return (p - buffer + chars_deleted);
284 }
285
286 /*
287 * Like malloc but get fatal error if memory is exhausted.
288 */
289 long *
290 xmalloc (unsigned int size)
291 {
292 long *result = (long *) malloc (size);
293 if (result == NULL)
294 fatal ("virtual memory exhausted");
295 return result;
296 }
297
298 long *
299 xrealloc (char *ptr, unsigned int size)
300 {
301 long *result = (long *) realloc (ptr, size);
302 if (result == NULL)
303 fatal ("virtual memory exhausted");
304 return result;
305 }
306
307 void
308 fatal (char *message)
309 {
310 fprintf (stderr, "%s: %s\n", progname, message);
311 exit (EXIT_FAILURE);
312 }
313
314 /* arch-tag: 5a3ad2af-a802-408f-83cc-e7cf5e98653e
315 (do not change this comment) */
316
317 /* b2m.c ends here */