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