* b2m.c: Include <limits.h>.
[bpt/emacs.git] / 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 <limits.h>
30 #include <stdio.h>
31 #include <time.h>
32 #include <sys/types.h>
33 #include <getopt.h>
34 #ifdef MSDOS
35 #include <fcntl.h>
36 #endif
37
38 #undef TRUE
39 #define TRUE 1
40 #undef FALSE
41 #define FALSE 0
42
43 #define streq(s,t) (strcmp (s, t) == 0)
44 #define strneq(s,t,n) (strncmp (s, t, n) == 0)
45
46 typedef int logical;
47
48 /* True if TM_YEAR is a struct tm's tm_year value that is acceptable
49 to asctime. Glibc asctime returns a useful string unless TM_YEAR
50 is nearly INT_MAX, but the C Standard lets C libraries overrun a
51 buffer if TM_YEAR needs more than 4 bytes. */
52 #ifdef __GLIBC__
53 # define TM_YEAR_IN_ASCTIME_RANGE(tm_year) ((tm_year) <= INT_MAX - 1900)
54 #else
55 # define TM_YEAR_IN_ASCTIME_RANGE(tm_year) \
56 (-999 - 1900 <= (tm_year) && (tm_year) <= 9999 - 1900)
57 #endif
58
59 /*
60 * A `struct linebuffer' is a structure which holds a line of text.
61 * `readline' reads a line from a stream into a linebuffer and works
62 * regardless of the length of the line.
63 */
64 struct linebuffer
65 {
66 long size;
67 char *buffer;
68 };
69
70 extern char *strtok();
71
72 long *xmalloc (), *xrealloc ();
73 char *concat ();
74 long readline ();
75 void fatal ();
76
77 /*
78 * xnew -- allocate storage. SYNOPSIS: Type *xnew (int n, Type);
79 */
80 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type)))
81
82
83
84 char *progname;
85
86 struct option longopts[] =
87 {
88 { "help", no_argument, NULL, 'h' },
89 { "version", no_argument, NULL, 'V' },
90 { 0 }
91 };
92
93 extern int optind;
94
95 int
96 main (argc, argv)
97 int argc;
98 char **argv;
99 {
100 logical labels_saved, printing, header;
101 time_t ltoday;
102 struct tm *tm;
103 char *labels, *p, *today;
104 struct linebuffer data;
105
106 #ifdef MSDOS
107 _fmode = O_BINARY; /* all of files are treated as binary files */
108 #if __DJGPP__ > 1
109 if (!isatty (fileno (stdout)))
110 setmode (fileno (stdout), O_BINARY);
111 if (!isatty (fileno (stdin)))
112 setmode (fileno (stdin), O_BINARY);
113 #else /* not __DJGPP__ > 1 */
114 (stdout)->_flag &= ~_IOTEXT;
115 (stdin)->_flag &= ~_IOTEXT;
116 #endif /* not __DJGPP__ > 1 */
117 #endif
118 progname = argv[0];
119
120 while (1)
121 {
122 int opt = getopt_long (argc, argv, "hV", longopts, 0);
123 if (opt == EOF)
124 break;
125
126 switch (opt)
127 {
128 case 'V':
129 printf ("%s (GNU Emacs %s)\n", "b2m", VERSION);
130 puts ("b2m is in the public domain.");
131 exit (EXIT_SUCCESS);
132
133 case 'h':
134 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
135 exit (EXIT_SUCCESS);
136 }
137 }
138
139 if (optind != argc)
140 {
141 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
142 exit (EXIT_SUCCESS);
143 }
144
145 labels_saved = printing = header = FALSE;
146 ltoday = time (0);
147 /* Convert to a string, checking for out-of-range time stamps.
148 Don't use 'ctime', as that might dump core if the hardware clock
149 is set to a bizarre value. */
150 tm = localtime (&ltoday);
151 if (! (tm && TM_YEAR_IN_ASCTIME_RANGE (tm->tm_year)))
152 fatal ("current time is out of range");
153 today = asctime (tm);
154 data.size = 200;
155 data.buffer = xnew (200, char);
156
157 if (readline (&data, stdin) == 0
158 || !strneq (data.buffer, "BABYL OPTIONS:", 14))
159 fatal ("standard input is not a Babyl mailfile.");
160
161 while (readline (&data, stdin) > 0)
162 {
163 if (streq (data.buffer, "*** EOOH ***") && !printing)
164 {
165 printing = header = TRUE;
166 printf ("From \"Babyl to mail by %s\" %s", progname, today);
167 continue;
168 }
169
170 if (data.buffer[0] == '\037')
171 {
172 if (data.buffer[1] == '\0')
173 continue;
174 else if (data.buffer[1] == '\f')
175 {
176 /* Save labels. */
177 readline (&data, stdin);
178 p = strtok (data.buffer, " ,\r\n\t");
179 labels = "X-Babyl-Labels: ";
180
181 while ((p = strtok (NULL, " ,\r\n\t")))
182 labels = concat (labels, p, ", ");
183
184 p = &labels[strlen (labels) - 2];
185 if (*p == ',')
186 *p = '\0';
187 printing = header = FALSE;
188 labels_saved = TRUE;
189 continue;
190 }
191 }
192
193 if ((data.buffer[0] == '\0') && header)
194 {
195 header = FALSE;
196 if (labels_saved)
197 puts (labels);
198 }
199
200 if (printing)
201 puts (data.buffer);
202 }
203
204 return EXIT_SUCCESS;
205 }
206
207
208
209 /*
210 * Return a newly-allocated string whose contents
211 * concatenate those of s1, s2, s3.
212 */
213 char *
214 concat (s1, s2, s3)
215 char *s1, *s2, *s3;
216 {
217 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
218 char *result = xnew (len1 + len2 + len3 + 1, char);
219
220 strcpy (result, s1);
221 strcpy (result + len1, s2);
222 strcpy (result + len1 + len2, s3);
223 result[len1 + len2 + len3] = '\0';
224
225 return result;
226 }
227
228 /*
229 * Read a line of text from `stream' into `linebuffer'.
230 * Return the number of characters read from `stream',
231 * which is the length of the line including the newline, if any.
232 */
233 long
234 readline (linebuffer, stream)
235 struct linebuffer *linebuffer;
236 register FILE *stream;
237 {
238 char *buffer = linebuffer->buffer;
239 register char *p = linebuffer->buffer;
240 register char *pend;
241 int chars_deleted;
242
243 pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */
244
245 while (1)
246 {
247 register int c = getc (stream);
248 if (p == pend)
249 {
250 linebuffer->size *= 2;
251 buffer = (char *) xrealloc (buffer, linebuffer->size);
252 p += buffer - linebuffer->buffer;
253 pend = buffer + linebuffer->size;
254 linebuffer->buffer = buffer;
255 }
256 if (c == EOF)
257 {
258 *p = '\0';
259 chars_deleted = 0;
260 break;
261 }
262 if (c == '\n')
263 {
264 if (p > buffer && p[-1] == '\r')
265 {
266 *--p = '\0';
267 chars_deleted = 2;
268 }
269 else
270 {
271 *p = '\0';
272 chars_deleted = 1;
273 }
274 break;
275 }
276 *p++ = c;
277 }
278
279 return (p - buffer + chars_deleted);
280 }
281
282 /*
283 * Like malloc but get fatal error if memory is exhausted.
284 */
285 long *
286 xmalloc (size)
287 unsigned int size;
288 {
289 long *result = (long *) malloc (size);
290 if (result == NULL)
291 fatal ("virtual memory exhausted");
292 return result;
293 }
294
295 long *
296 xrealloc (ptr, size)
297 char *ptr;
298 unsigned int size;
299 {
300 long *result = (long *) realloc (ptr, size);
301 if (result == NULL)
302 fatal ("virtual memory exhausted");
303 return result;
304 }
305
306 void
307 fatal (message)
308 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 */