gtkutil.c (xg_update_frame_menubar): Do nothing if menubar already has a parent.
[emacs.git] / lib-src / b2m.c
blob7de48e6a522ec3bb4f7fe8a213ee9b0d70568f11
1 /*
2 * b2m - a filter for Babyl -> Unix mail files
3 * The copyright on this file has been disclaimed.
5 * usage: b2m < babyl > mailbox
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!
12 * it's not much but it's free!
14 * Ed Wilkinson
15 * E.Wilkinson@massey.ac.nz
16 * Mon Nov 7 15:54:06 PDT 1988
19 /* Made conformant to the GNU coding standards January, 1995
20 by Francesco Potorti` <pot@cnuce.cnr.it>. */
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
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
37 #undef TRUE
38 #define TRUE 1
39 #undef FALSE
40 #define FALSE 0
42 #define streq(s,t) (strcmp (s, t) == 0)
43 #define strneq(s,t,n) (strncmp (s, t, n) == 0)
45 typedef int logical;
47 #define TM_YEAR_BASE 1900
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
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.
61 struct linebuffer
63 long size;
64 char *buffer;
67 extern char *strtok();
69 long *xmalloc (), *xrealloc ();
70 char *concat ();
71 long readline ();
72 void fatal ();
75 * xnew -- allocate storage. SYNOPSIS: Type *xnew (int n, Type);
77 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type)))
81 char *progname;
83 struct option longopts[] =
85 { "help", no_argument, NULL, 'h' },
86 { "version", no_argument, NULL, 'V' },
87 { 0 }
90 extern int optind;
92 int
93 main (argc, argv)
94 int argc;
95 char **argv;
97 logical labels_saved, printing, header, first, last_was_blank_line;
98 time_t ltoday;
99 struct tm *tm;
100 char *labels, *p, *today;
101 struct linebuffer data;
103 #ifdef MSDOS
104 _fmode = O_BINARY; /* all of files are treated as binary files */
105 #if __DJGPP__ > 1
106 if (!isatty (fileno (stdout)))
107 setmode (fileno (stdout), O_BINARY);
108 if (!isatty (fileno (stdin)))
109 setmode (fileno (stdin), O_BINARY);
110 #else /* not __DJGPP__ > 1 */
111 (stdout)->_flag &= ~_IOTEXT;
112 (stdin)->_flag &= ~_IOTEXT;
113 #endif /* not __DJGPP__ > 1 */
114 #endif
115 progname = argv[0];
117 while (1)
119 int opt = getopt_long (argc, argv, "hV", longopts, 0);
120 if (opt == EOF)
121 break;
123 switch (opt)
125 case 'V':
126 printf ("%s (GNU Emacs %s)\n", "b2m", VERSION);
127 puts ("b2m is in the public domain.");
128 exit (EXIT_SUCCESS);
130 case 'h':
131 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
132 exit (EXIT_SUCCESS);
136 if (optind != argc)
138 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
139 exit (EXIT_SUCCESS);
142 labels_saved = printing = header = last_was_blank_line = FALSE;
143 first = TRUE;
144 ltoday = time (0);
145 /* Convert to a string, checking for out-of-range time stamps.
146 Don't use 'ctime', as that might dump core if the hardware clock
147 is set to a bizarre value. */
148 tm = localtime (&ltoday);
149 if (! (tm && TM_YEAR_IN_ASCTIME_RANGE (tm->tm_year)
150 && (today = asctime (tm))))
151 fatal ("current time is out of range");
152 data.size = 200;
153 data.buffer = xnew (200, char);
155 if (readline (&data, stdin) == 0
156 || !strneq (data.buffer, "BABYL OPTIONS:", 14))
157 fatal ("standard input is not a Babyl mailfile.");
159 while (readline (&data, stdin) > 0)
161 if (streq (data.buffer, "*** EOOH ***") && !printing)
163 printing = header = TRUE;
164 printf ("From \"Babyl to mail by %s\" %s", progname, today);
165 continue;
168 if (data.buffer[0] == '\037')
170 if (data.buffer[1] == '\0')
171 continue;
172 else if (data.buffer[1] == '\f')
174 if (first)
175 first = FALSE;
176 else if (! last_was_blank_line)
177 puts("");
178 /* Save labels. */
179 readline (&data, stdin);
180 p = strtok (data.buffer, " ,\r\n\t");
181 labels = "X-Babyl-Labels: ";
183 while ((p = strtok (NULL, " ,\r\n\t")))
184 labels = concat (labels, p, ", ");
186 p = &labels[strlen (labels) - 2];
187 if (*p == ',')
188 *p = '\0';
189 printing = header = FALSE;
190 labels_saved = TRUE;
191 continue;
195 if ((data.buffer[0] == '\0') && header)
197 header = FALSE;
198 if (labels_saved)
199 puts (labels);
202 if (printing)
204 puts (data.buffer);
205 if (data.buffer[0] == '\0')
206 last_was_blank_line = TRUE;
207 else
208 last_was_blank_line = FALSE;
212 return EXIT_SUCCESS;
218 * Return a newly-allocated string whose contents
219 * concatenate those of s1, s2, s3.
221 char *
222 concat (s1, s2, s3)
223 char *s1, *s2, *s3;
225 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
226 char *result = xnew (len1 + len2 + len3 + 1, char);
228 strcpy (result, s1);
229 strcpy (result + len1, s2);
230 strcpy (result + len1 + len2, s3);
231 result[len1 + len2 + len3] = '\0';
233 return result;
237 * Read a line of text from `stream' into `linebuffer'.
238 * Return the number of characters read from `stream',
239 * which is the length of the line including the newline, if any.
241 long
242 readline (linebuffer, stream)
243 struct linebuffer *linebuffer;
244 register FILE *stream;
246 char *buffer = linebuffer->buffer;
247 register char *p = linebuffer->buffer;
248 register char *pend;
249 int chars_deleted;
251 pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */
253 while (1)
255 register int c = getc (stream);
256 if (p == pend)
258 linebuffer->size *= 2;
259 buffer = (char *) xrealloc (buffer, linebuffer->size);
260 p += buffer - linebuffer->buffer;
261 pend = buffer + linebuffer->size;
262 linebuffer->buffer = buffer;
264 if (c == EOF)
266 *p = '\0';
267 chars_deleted = 0;
268 break;
270 if (c == '\n')
272 if (p > buffer && p[-1] == '\r')
274 *--p = '\0';
275 chars_deleted = 2;
277 else
279 *p = '\0';
280 chars_deleted = 1;
282 break;
284 *p++ = c;
287 return (p - buffer + chars_deleted);
291 * Like malloc but get fatal error if memory is exhausted.
293 long *
294 xmalloc (size)
295 unsigned int size;
297 long *result = (long *) malloc (size);
298 if (result == NULL)
299 fatal ("virtual memory exhausted");
300 return result;
303 long *
304 xrealloc (ptr, size)
305 char *ptr;
306 unsigned int size;
308 long *result = (long *) realloc (ptr, size);
309 if (result == NULL)
310 fatal ("virtual memory exhausted");
311 return result;
314 void
315 fatal (message)
316 char *message;
318 fprintf (stderr, "%s: %s\n", progname, message);
319 exit (EXIT_FAILURE);
322 /* arch-tag: 5a3ad2af-a802-408f-83cc-e7cf5e98653e
323 (do not change this comment) */
325 /* b2m.c ends here */