| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
#ifdef HAVE_CONFIG_H |
|---|
| 23 |
#include <config.h> |
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
#undef static |
|---|
| 27 |
#endif |
|---|
| 28 |
|
|---|
| 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 |
|---|
| 36 |
|
|---|
| 37 |
#undef TRUE |
|---|
| 38 |
#define TRUE 1 |
|---|
| 39 |
#undef FALSE |
|---|
| 40 |
#define FALSE 0 |
|---|
| 41 |
|
|---|
| 42 |
#define streq(s,t) (strcmp (s, t) == 0) |
|---|
| 43 |
#define strneq(s,t,n) (strncmp (s, t, n) == 0) |
|---|
| 44 |
|
|---|
| 45 |
typedef int logical; |
|---|
| 46 |
|
|---|
| 47 |
#define TM_YEAR_BASE 1900 |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 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 |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
struct linebuffer |
|---|
| 62 |
{ |
|---|
| 63 |
long size; |
|---|
| 64 |
char *buffer; |
|---|
| 65 |
}; |
|---|
| 66 |
|
|---|
| 67 |
extern char *strtok(); |
|---|
| 68 |
|
|---|
| 69 |
long *xmalloc (), *xrealloc (); |
|---|
| 70 |
char *concat (); |
|---|
| 71 |
long readline (); |
|---|
| 72 |
void fatal (); |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
#define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type))) |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
char *progname; |
|---|
| 82 |
|
|---|
| 83 |
struct option longopts[] = |
|---|
| 84 |
{ |
|---|
| 85 |
{ "help", no_argument, NULL, 'h' }, |
|---|
| 86 |
{ "version", no_argument, NULL, 'V' }, |
|---|
| 87 |
{ 0 } |
|---|
| 88 |
}; |
|---|
| 89 |
|
|---|
| 90 |
extern int optind; |
|---|
| 91 |
|
|---|
| 92 |
int |
|---|
| 93 |
main (argc, argv) |
|---|
| 94 |
int argc; |
|---|
| 95 |
char **argv; |
|---|
| 96 |
{ |
|---|
| 97 |
logical labels_saved, printing, header; |
|---|
| 98 |
time_t ltoday; |
|---|
| 99 |
struct tm *tm; |
|---|
| 100 |
char *labels, *p, *today; |
|---|
| 101 |
struct linebuffer data; |
|---|
| 102 |
|
|---|
| 103 |
#ifdef MSDOS |
|---|
| 104 |
_fmode = O_BINARY; |
|---|
| 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 |
|---|
| 111 |
(stdout)->_flag &= ~_IOTEXT; |
|---|
| 112 |
(stdin)->_flag &= ~_IOTEXT; |
|---|
| 113 |
#endif |
|---|
| 114 |
#endif |
|---|
| 115 |
progname = argv[0]; |
|---|
| 116 |
|
|---|
| 117 |
while (1) |
|---|
| 118 |
{ |
|---|
| 119 |
int opt = getopt_long (argc, argv, "hV", longopts, 0); |
|---|
| 120 |
if (opt == EOF) |
|---|
| 121 |
break; |
|---|
| 122 |
|
|---|
| 123 |
switch (opt) |
|---|
| 124 |
{ |
|---|
| 125 |
case 'V': |
|---|
| 126 |
printf ("%s (GNU Emacs %s)\n", "b2m", VERSION); |
|---|
| 127 |
puts ("b2m is in the public domain."); |
|---|
| 128 |
exit (EXIT_SUCCESS); |
|---|
| 129 |
|
|---|
| 130 |
case 'h': |
|---|
| 131 |
fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname); |
|---|
| 132 |
exit (EXIT_SUCCESS); |
|---|
| 133 |
} |
|---|
| 134 |
} |
|---|
| 135 |
|
|---|
| 136 |
if (optind != argc) |
|---|
| 137 |
{ |
|---|
| 138 |
fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname); |
|---|
| 139 |
exit (EXIT_SUCCESS); |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
labels_saved = printing = header = FALSE; |
|---|
| 143 |
ltoday = time (0); |
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
tm = localtime (<oday); |
|---|
| 148 |
if (! (tm && TM_YEAR_IN_ASCTIME_RANGE (tm->tm_year) |
|---|
| 149 |
&& (today = asctime (tm)))) |
|---|
| 150 |
fatal ("current time is out of range"); |
|---|
| 151 |
data.size = 200; |
|---|
| 152 |
data.buffer = xnew (200, char); |
|---|
| 153 |
|
|---|
| 154 |
if (readline (&data, stdin) == 0 |
|---|
| 155 |
|| !strneq (data.buffer, "BABYL OPTIONS:", 14)) |
|---|
| 156 |
fatal ("standard input is not a Babyl mailfile."); |
|---|
| 157 |
|
|---|
| 158 |
while (readline (&data, stdin) > 0) |
|---|
| 159 |
{ |
|---|
| 160 |
if (streq (data.buffer, "*** EOOH ***") && !printing) |
|---|
| 161 |
{ |
|---|
| 162 |
printing = header = TRUE; |
|---|
| 163 |
printf ("From \"Babyl to mail by %s\" %s", progname, today); |
|---|
| 164 |
continue; |
|---|
| 165 |
} |
|---|
| 166 |
|
|---|
| 167 |
if (data.buffer[0] == '\037') |
|---|
| 168 |
{ |
|---|
| 169 |
if (data.buffer[1] == '\0') |
|---|
| 170 |
continue; |
|---|
| 171 |
else if (data.buffer[1] == '\f') |
|---|
| 172 |
{ |
|---|
| 173 |
|
|---|
| 174 |
readline (&data, stdin); |
|---|
| 175 |
p = strtok (data.buffer, " ,\r\n\t"); |
|---|
| 176 |
labels = "X-Babyl-Labels: "; |
|---|
| 177 |
|
|---|
| 178 |
while ((p = strtok (NULL, " ,\r\n\t"))) |
|---|
| 179 |
labels = concat (labels, p, ", "); |
|---|
| 180 |
|
|---|
| 181 |
p = &labels[strlen (labels) - 2]; |
|---|
| 182 |
if (*p == ',') |
|---|
| 183 |
*p = '\0'; |
|---|
| 184 |
printing = header = FALSE; |
|---|
| 185 |
labels_saved = TRUE; |
|---|
| 186 |
continue; |
|---|
| 187 |
} |
|---|
| 188 |
} |
|---|
| 189 |
|
|---|
| 190 |
if ((data.buffer[0] == '\0') && header) |
|---|
| 191 |
{ |
|---|
| 192 |
header = FALSE; |
|---|
| 193 |
if (labels_saved) |
|---|
| 194 |
puts (labels); |
|---|
| 195 |
} |
|---|
| 196 |
|
|---|
| 197 |
if (printing) |
|---|
| 198 |
puts (data.buffer); |
|---|
| 199 |
} |
|---|
| 200 |
|
|---|
| 201 |
return EXIT_SUCCESS; |
|---|
| 202 |
} |
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
char * |
|---|
| 211 |
concat (s1, s2, s3) |
|---|
| 212 |
char *s1, *s2, *s3; |
|---|
| 213 |
{ |
|---|
| 214 |
int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3); |
|---|
| 215 |
char *result = xnew (len1 + len2 + len3 + 1, char); |
|---|
| 216 |
|
|---|
| 217 |
strcpy (result, s1); |
|---|
| 218 |
strcpy (result + len1, s2); |
|---|
| 219 |
strcpy (result + len1 + len2, s3); |
|---|
| 220 |
result[len1 + len2 + len3] = '\0'; |
|---|
| 221 |
|
|---|
| 222 |
return result; |
|---|
| 223 |
} |
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
long |
|---|
| 231 |
readline (linebuffer, stream) |
|---|
| 232 |
struct linebuffer *linebuffer; |
|---|
| 233 |
register FILE *stream; |
|---|
| 234 |
{ |
|---|
| 235 |
char *buffer = linebuffer->buffer; |
|---|
| 236 |
register char *p = linebuffer->buffer; |
|---|
| 237 |
register char *pend; |
|---|
| 238 |
int chars_deleted; |
|---|
| 239 |
|
|---|
| 240 |
pend = p + linebuffer->size; |
|---|
| 241 |
|
|---|
| 242 |
while (1) |
|---|
| 243 |
{ |
|---|
| 244 |
register int c = getc (stream); |
|---|
| 245 |
if (p == pend) |
|---|
| 246 |
{ |
|---|
| 247 |
linebuffer->size *= 2; |
|---|
| 248 |
buffer = (char *) xrealloc (buffer, linebuffer->size); |
|---|
| 249 |
p += buffer - linebuffer->buffer; |
|---|
| 250 |
pend = buffer + linebuffer->size; |
|---|
| 251 |
linebuffer->buffer = buffer; |
|---|
| 252 |
} |
|---|
| 253 |
if (c == EOF) |
|---|
| 254 |
{ |
|---|
| 255 |
*p = '\0'; |
|---|
| 256 |
chars_deleted = 0; |
|---|
| 257 |
break; |
|---|
| 258 |
} |
|---|
| 259 |
if (c == '\n') |
|---|
| 260 |
{ |
|---|
| 261 |
if (p > buffer && p[-1] == '\r') |
|---|
| 262 |
{ |
|---|
| 263 |
*--p = '\0'; |
|---|
| 264 |
chars_deleted = 2; |
|---|
| 265 |
} |
|---|
| 266 |
else |
|---|
| 267 |
{ |
|---|
| 268 |
*p = '\0'; |
|---|
| 269 |
chars_deleted = 1; |
|---|
| 270 |
} |
|---|
| 271 |
break; |
|---|
| 272 |
} |
|---|
| 273 |
*p++ = c; |
|---|
| 274 |
} |
|---|
| 275 |
|
|---|
| 276 |
return (p - buffer + chars_deleted); |
|---|
| 277 |
} |
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
long * |
|---|
| 283 |
xmalloc (size) |
|---|
| 284 |
unsigned int size; |
|---|
| 285 |
{ |
|---|
| 286 |
long *result = (long *) malloc (size); |
|---|
| 287 |
if (result == NULL) |
|---|
| 288 |
fatal ("virtual memory exhausted"); |
|---|
| 289 |
return result; |
|---|
| 290 |
} |
|---|
| 291 |
|
|---|
| 292 |
long * |
|---|
| 293 |
xrealloc (ptr, size) |
|---|
| 294 |
char *ptr; |
|---|
| 295 |
unsigned int size; |
|---|
| 296 |
{ |
|---|
| 297 |
long *result = (long *) realloc (ptr, size); |
|---|
| 298 |
if (result == NULL) |
|---|
| 299 |
fatal ("virtual memory exhausted"); |
|---|
| 300 |
return result; |
|---|
| 301 |
} |
|---|
| 302 |
|
|---|
| 303 |
void |
|---|
| 304 |
fatal (message) |
|---|
| 305 |
char *message; |
|---|
| 306 |
{ |
|---|
| 307 |
fprintf (stderr, "%s: %s\n", progname, message); |
|---|
| 308 |
exit (EXIT_FAILURE); |
|---|
| 309 |
} |
|---|
| 310 |
|
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 |
|
|---|
| 314 |
|
|---|