summaryrefslogtreecommitdiff
path: root/src/utils/pfbtops/pfbtops.c
blob: bb2a85ab8060e0c9f06f7d37f8270552dc87743a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* This translates ps fonts in .pfb format to ASCII ps files. */

#include <stdio.h>
#include <getopt.h>

#include "nonposix.h"

/* Binary bytes per output line. */
#define BYTES_PER_LINE (64/2)
#define HEX_DIGITS "0123456789abcdef"

static char *program_name;

static void error(s)
     char *s;
{
  fprintf(stderr, "%s: %s\n", program_name, s);
  exit(2);
}

static void usage()
{
  fprintf(stderr, "usage: %s [-v] [pfb_file]\n", program_name);
  exit(1);
}

int main(argc, argv)
     int argc;
     char **argv;
{
  int opt;
  extern int optind;

  program_name = argv[0];

  while ((opt = getopt(argc, argv, "v")) != EOF) {
    switch (opt) {
    case 'v':
      {
	extern char *Version_string;
	printf("GNU pfbtops (groff) version %s\n", Version_string);
	exit(0);
	break;
      }
    case '?':
      usage();
    }
  }

  if (argc - optind > 1)
    usage();
  if (argc > optind && !freopen(argv[optind], "r", stdin))
    {
      perror(argv[optind]);
      exit(1);
    }
#ifdef SET_BINARY
  SET_BINARY(fileno(stdin));
#endif
  for (;;)
    {
      int type, c, i;
      long n;

      c = getchar();
      if (c != 0x80)
	error("first byte of packet not 0x80");
      type = getchar();
      if (type == 3)
	break;
      if (type != 1 && type != 2)
	error("bad packet type");
      n = 0;
      for (i = 0; i < 4; i++)
	{
	  c = getchar();
	  if (c == EOF)
	    error("end of file in packet header");
	  n |= (long)c << (i << 3);
	}
      if (n < 0)
	error("negative packet length");
      if (type == 1)
	{
	  while (--n >= 0)
	    {
	      c = getchar();
	      if (c == EOF)
		error("end of file in text packet");
	      if (c == '\r')
		c = '\n';
	      putchar(c);
	    }
	  if (c != '\n')
	    putchar('\n');
	}
      else
	{
	  int count = 0;
	  while (--n >= 0)
	    {
	      c = getchar();
	      if (c == EOF)
		error("end of file in binary packet");
	      if (count >= BYTES_PER_LINE)
		{
		  putchar('\n');
		  count = 0;
		}
	      count++;
	      putchar(HEX_DIGITS[(c >> 4) & 0xf]);
	      putchar(HEX_DIGITS[c & 0xf]);
	    }
	  putchar('\n');
	}
    }
  exit(0);
}