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