b2m.c 2.42 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * b2m - a filter for Babyl -> Unix mail files
 *
 * usage:	b2m < babyl > mailbox
 *
 * I find this useful whenever I have to use a
 * system which - shock horror! - doesn't run
 * Gnu emacs. At least now I can read all my
 * Gnumacs Babyl format mail files!
 *
 * it's not much but it's free!
 *
 *   Ed Wilkinson
 *   E.Wilkinson@massey.ac.nz
 *   Mon Nov 7 15:54:06 PDT 1988
 */

18 19 20
/* Serious bug: This program uses `gets', which is intrinsically
   unreliable--long lines will cause crashes.
   Someone should fix this program not to use `gets'.  */
Jim Blandy's avatar
Jim Blandy committed
21 22
#include <stdio.h>
#include <time.h>
Jim Blandy's avatar
Jim Blandy committed
23
#include <sys/types.h>
24 25 26
#ifdef MSDOS
#include <fcntl.h>
#endif
Jim Blandy's avatar
Jim Blandy committed
27

28
#include <../src/config.h>
Jim Blandy's avatar
Jim Blandy committed
29

Jim Blandy's avatar
Jim Blandy committed
30 31 32
/* BSD's strings.h does not declare the type of strtok.  */
extern char *strtok ();

33
#ifndef TRUE
Jim Blandy's avatar
Jim Blandy committed
34
#define TRUE  (1)
35 36
#endif
#ifndef FALSE
Jim Blandy's avatar
Jim Blandy committed
37
#define FALSE (0)
38
#endif
Jim Blandy's avatar
Jim Blandy committed
39 40

int header = FALSE, printing;
Jim Blandy's avatar
Jim Blandy committed
41
time_t ltoday;
Jim Blandy's avatar
Jim Blandy committed
42 43
char from[256], labels[256], data[256], *p, *today;

44 45 46
main (argc, argv)
     int argc;
     char **argv;
Jim Blandy's avatar
Jim Blandy committed
47
{
48
#ifdef MSDOS
49
  _fmode = O_BINARY;		/* all of files are treated as binary files */
50 51 52
  (stdout)->_flag &= ~_IOTEXT;
  (stdin)->_flag &= ~_IOTEXT;
#endif
53
  if (strcmp (argv[1], "--help") == 0)
54
    {
55
      fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", argv[0]);
56 57
      exit (0);
    }
58 59
  ltoday = time (0);
  today = ctime (&ltoday);
Jim Blandy's avatar
Jim Blandy committed
60

61 62 63 64 65 66 67 68 69
  /* BUG!  Must not use gets in a reliable program!  */
  if (gets (data))
    {
      if (strncmp (data, "BABYL OPTIONS:", 14))
	{
	  fprintf (stderr, "%s: not a Babyl mailfile!\n", argv[0]);
	  exit (-1);
	}
      else
70
	printing = FALSE;
71
    }
Jim Blandy's avatar
Jim Blandy committed
72
  else
73
    exit (-1);
Jim Blandy's avatar
Jim Blandy committed
74
  if (printing)
75
    puts (data);
Jim Blandy's avatar
Jim Blandy committed
76

77 78
  while (gets (data))
    {
79 80

#if 0
81 82 83 84
      /* What was this for?  Does somebody have something against blank
	 lines?  */
      if (!strcmp (data, ""))
	exit (0);
85
#endif
Jim Blandy's avatar
Jim Blandy committed
86

87 88 89 90 91 92
      if (!strcmp (data, "*** EOOH ***") && !printing)
	{
	  printing = header = TRUE;
	  printf ("From %s %s", argv[0], today);
	  continue;
	}
93

94 95 96 97 98 99
      if (!strcmp (data, "\037\f"))
	{
	  /* save labels */
	  gets (data);
	  p = strtok (data, " ,\r\n\t");
	  strcpy (labels, "X-Babyl-Labels: ");
Jim Blandy's avatar
Jim Blandy committed
100

101 102 103 104 105
	  while (p = strtok (NULL, " ,\r\n\t"))
	    {
	      strcat (labels, p);
	      strcat (labels, ", ");
	    }
Jim Blandy's avatar
Jim Blandy committed
106

107 108 109 110
	  labels[strlen (labels) - 2] = '\0';
	  printing = header = FALSE;
	  continue;
	}
Jim Blandy's avatar
Jim Blandy committed
111

112 113 114 115 116 117
      if (!strlen (data) && header)
	{
	  header = FALSE;
	  if (strcmp (labels, "X-Babyl-Labels"))
	    puts (labels);
	}
Jim Blandy's avatar
Jim Blandy committed
118
    
119 120 121
      if (printing)
	puts (data);
    }
Jim Blandy's avatar
Jim Blandy committed
122
}