groff before CVS: release 1.05
[s-roff.git] / ps / pfbtops.c
blobbc79bce59db3eb15b4214e765a6bd04dd5c57b69
1 /* This translates ps fonts in .pfb format to ASCII ps files. */
3 #include <stdio.h>
5 /* Binary bytes per output line. */
6 #define BYTES_PER_LINE (79/2)
7 #define HEX_DIGITS "0123456789ABCDEF"
9 static char *program_name;
11 static void error(s)
12 char *s;
14 fprintf(stderr, "%s: %s\n", program_name, s);
15 exit(2);
18 static void usage()
20 fprintf(stderr, "usage: %s [-v] [pfb_file]\n", program_name);
21 exit(1);
24 int main(argc, argv)
25 int argc;
26 char **argv;
28 int opt;
29 extern int optind;
31 program_name = argv[0];
33 while ((opt = getopt(argc, argv, "v")) != EOF) {
34 switch (opt) {
35 case 'v':
37 extern char *version_string;
38 fprintf(stderr, "pfbtops groff version %s\n", version_string);
39 fflush(stderr);
40 break;
42 case '?':
43 usage();
47 if (argc - optind > 1)
48 usage();
49 if (argc > optind && !freopen(argv[optind], "r", stdin))
51 perror(argv[optind]);
52 exit(1);
54 for (;;)
56 int type, c, i;
57 long n;
59 c = getchar();
60 if (c != 0x80)
61 error("first byte of packet not 0x80");
62 type = getchar();
63 if (type == 3)
64 break;
65 if (type != 1 && type != 2)
66 error("bad packet type");
67 n = 0;
68 for (i = 0; i < 4; i++)
70 c = getchar();
71 if (c == EOF)
72 error("end of file in packet header");
73 n |= (long)c << (i << 3);
75 if (n < 0)
76 error("negative packet length");
77 if (type == 1)
79 while (--n >= 0)
81 c = getchar();
82 if (c == EOF)
83 error("end of file in text packet");
84 if (c == '\r')
85 c = '\n';
86 putchar(c);
88 if (c != '\n')
89 putchar('\n');
91 else
93 int count = 0;
94 while (--n >= 0)
96 c = getchar();
97 if (c == EOF)
98 error("end of file in binary packet");
99 if (count >= BYTES_PER_LINE)
101 putchar('\n');
102 count = 0;
104 count++;
105 putchar(HEX_DIGITS[(c >> 4) & 0xf]);
106 putchar(HEX_DIGITS[c & 0xf]);
108 putchar('\n');
111 exit(0);