Fix warnings produced by compiling with -Wwrite_strings (i.e. use const char *).
[bpt/emacs.git] / lib-src / b2m.c
CommitLineData
cc9940c7
JB
1/*
2 * b2m - a filter for Babyl -> Unix mail files
a43dbef8 3 * The copyright on this file has been disclaimed.
cc9940c7
JB
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
37a9305e
PJ
9 * GNU Emacs. At least now I can read all my
10 * GNU Emacs Babyl format mail files!
cc9940c7
JB
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
e19bdc14
RS
19/* Made conformant to the GNU coding standards January, 1995
20 by Francesco Potorti` <pot@cnuce.cnr.it>. */
21
e19bdc14
RS
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
fbfed6f0 28
4ee9629e
PE
29#include <stdio.h>
30#include <time.h>
31#include <sys/types.h>
32#include <getopt.h>
33#ifdef MSDOS
34#include <fcntl.h>
1f8c2f55
AS
35#endif
36
e19bdc14
RS
37#undef TRUE
38#define TRUE 1
39#undef FALSE
40#define FALSE 0
cc9940c7 41
e19bdc14
RS
42#define streq(s,t) (strcmp (s, t) == 0)
43#define strneq(s,t,n) (strncmp (s, t, n) == 0)
44
45typedef int logical;
46
d65b4235
PE
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
f5565804 52# define TM_YEAR_IN_ASCTIME_RANGE(tm_year) \
d65b4235 53 (1000 - TM_YEAR_BASE <= (tm_year) && (tm_year) <= 9999 - TM_YEAR_BASE)
f5565804
PE
54#endif
55
e19bdc14
RS
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 */
61struct linebuffer
62{
63 long size;
64 char *buffer;
65};
66
873fbd0b 67extern char *strtok(char *, const char *);
128ba46c 68
c532d349
DN
69long *xmalloc (unsigned int size);
70long *xrealloc (char *ptr, unsigned int size);
988e88ab 71char *concat (const char *s1, const char *s2, const char *s3);
c532d349 72long readline (struct linebuffer *linebuffer, register FILE *stream);
988e88ab 73void fatal (const char *message) NO_RETURN;
e19bdc14
RS
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
82char *progname;
cc9940c7 83
e4b34a85
KH
84struct option longopts[] =
85{
86 { "help", no_argument, NULL, 'h' },
87 { "version", no_argument, NULL, 'V' },
88 { 0 }
89};
90
91extern int optind;
92
1f8c2f55 93int
873fbd0b 94main (int argc, char **argv)
cc9940c7 95{
678bc1f5 96 logical labels_saved, printing, header, first, last_was_blank_line;
e19bdc14 97 time_t ltoday;
f5565804 98 struct tm *tm;
e19bdc14
RS
99 char *labels, *p, *today;
100 struct linebuffer data;
101
c6880c90 102#ifdef MSDOS
e3515d10 103 _fmode = O_BINARY; /* all of files are treated as binary files */
18198bb2
RS
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 */
c6880c90
RS
110 (stdout)->_flag &= ~_IOTEXT;
111 (stdin)->_flag &= ~_IOTEXT;
18198bb2 112#endif /* not __DJGPP__ > 1 */
c6880c90 113#endif
d4843060
RS
114 progname = argv[0];
115
e4b34a85
KH
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.");
3f0656ff 127 exit (EXIT_SUCCESS);
e4b34a85
KH
128
129 case 'h':
130 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
3f0656ff 131 exit (EXIT_SUCCESS);
e4b34a85
KH
132 }
133 }
134
135 if (optind != argc)
88d00c7e 136 {
e19bdc14 137 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
3f0656ff 138 exit (EXIT_SUCCESS);
88d00c7e 139 }
e4b34a85 140
678bc1f5
CY
141 labels_saved = printing = header = last_was_blank_line = FALSE;
142 first = TRUE;
e3515d10 143 ltoday = time (0);
f5565804
PE
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);
d65b4235
PE
148 if (! (tm && TM_YEAR_IN_ASCTIME_RANGE (tm->tm_year)
149 && (today = asctime (tm))))
f5565804 150 fatal ("current time is out of range");
e19bdc14
RS
151 data.size = 200;
152 data.buffer = xnew (200, char);
cc9940c7 153
e19bdc14
RS
154 if (readline (&data, stdin) == 0
155 || !strneq (data.buffer, "BABYL OPTIONS:", 14))
156 fatal ("standard input is not a Babyl mailfile.");
cc9940c7 157
e19bdc14 158 while (readline (&data, stdin) > 0)
e3515d10 159 {
e19bdc14 160 if (streq (data.buffer, "*** EOOH ***") && !printing)
e3515d10
RS
161 {
162 printing = header = TRUE;
d4843060 163 printf ("From \"Babyl to mail by %s\" %s", progname, today);
e3515d10
RS
164 continue;
165 }
88d00c7e 166
e19bdc14 167 if (data.buffer[0] == '\037')
e3515d10 168 {
e19bdc14
RS
169 if (data.buffer[1] == '\0')
170 continue;
171 else if (data.buffer[1] == '\f')
e3515d10 172 {
988e88ab 173 static char babyl[] = "X-Babyl-Labels: ";
678bc1f5
CY
174 if (first)
175 first = FALSE;
176 else if (! last_was_blank_line)
177 puts("");
e19bdc14
RS
178 /* Save labels. */
179 readline (&data, stdin);
180 p = strtok (data.buffer, " ,\r\n\t");
988e88ab 181 labels = babyl;
cc9940c7 182
25b18337 183 while ((p = strtok (NULL, " ,\r\n\t")))
e19bdc14
RS
184 labels = concat (labels, p, ", ");
185
7a804c76
KH
186 p = &labels[strlen (labels) - 2];
187 if (*p == ',')
188 *p = '\0';
e19bdc14
RS
189 printing = header = FALSE;
190 labels_saved = TRUE;
191 continue;
192 }
e3515d10 193 }
cc9940c7 194
e19bdc14 195 if ((data.buffer[0] == '\0') && header)
e3515d10
RS
196 {
197 header = FALSE;
e19bdc14 198 if (labels_saved)
e3515d10
RS
199 puts (labels);
200 }
e19bdc14 201
e3515d10 202 if (printing)
678bc1f5
CY
203 {
204 puts (data.buffer);
205 if (data.buffer[0] == '\0')
206 last_was_blank_line = TRUE;
207 else
208 last_was_blank_line = FALSE;
209 }
e19bdc14 210 }
3a213356 211
3f0656ff 212 return EXIT_SUCCESS;
e19bdc14
RS
213}
214
215
216
217/*
218 * Return a newly-allocated string whose contents
219 * concatenate those of s1, s2, s3.
220 */
221char *
988e88ab 222concat (const char *s1, const char *s2, const char *s3)
e19bdc14
RS
223{
224 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
225 char *result = xnew (len1 + len2 + len3 + 1, char);
226
227 strcpy (result, s1);
228 strcpy (result + len1, s2);
229 strcpy (result + len1 + len2, s3);
230 result[len1 + len2 + len3] = '\0';
231
232 return result;
233}
234
235/*
236 * Read a line of text from `stream' into `linebuffer'.
237 * Return the number of characters read from `stream',
238 * which is the length of the line including the newline, if any.
239 */
240long
873fbd0b 241readline (struct linebuffer *linebuffer, register FILE *stream)
e19bdc14
RS
242{
243 char *buffer = linebuffer->buffer;
244 register char *p = linebuffer->buffer;
245 register char *pend;
246 int chars_deleted;
247
248 pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */
249
250 while (1)
251 {
252 register int c = getc (stream);
253 if (p == pend)
254 {
255 linebuffer->size *= 2;
256 buffer = (char *) xrealloc (buffer, linebuffer->size);
257 p += buffer - linebuffer->buffer;
258 pend = buffer + linebuffer->size;
259 linebuffer->buffer = buffer;
260 }
261 if (c == EOF)
262 {
5326c1d6 263 *p = '\0';
e19bdc14
RS
264 chars_deleted = 0;
265 break;
266 }
267 if (c == '\n')
268 {
5326c1d6 269 if (p > buffer && p[-1] == '\r')
e19bdc14
RS
270 {
271 *--p = '\0';
272 chars_deleted = 2;
273 }
274 else
275 {
276 *p = '\0';
277 chars_deleted = 1;
278 }
279 break;
280 }
281 *p++ = c;
e3515d10 282 }
e19bdc14
RS
283
284 return (p - buffer + chars_deleted);
cc9940c7 285}
e19bdc14
RS
286
287/*
288 * Like malloc but get fatal error if memory is exhausted.
289 */
0404b62c 290long *
873fbd0b 291xmalloc (unsigned int size)
e19bdc14 292{
0404b62c 293 long *result = (long *) malloc (size);
e19bdc14
RS
294 if (result == NULL)
295 fatal ("virtual memory exhausted");
296 return result;
297}
298
0404b62c 299long *
873fbd0b 300xrealloc (char *ptr, unsigned int size)
e19bdc14 301{
0404b62c 302 long *result = (long *) realloc (ptr, size);
e19bdc14
RS
303 if (result == NULL)
304 fatal ("virtual memory exhausted");
305 return result;
306}
307
308void
988e88ab 309fatal (const char *message)
e19bdc14
RS
310{
311 fprintf (stderr, "%s: %s\n", progname, message);
3f0656ff 312 exit (EXIT_FAILURE);
e19bdc14
RS
313}
314
ab5796a9
MB
315/* arch-tag: 5a3ad2af-a802-408f-83cc-e7cf5e98653e
316 (do not change this comment) */
3f0656ff
TTN
317
318/* b2m.c ends here */