Sync-to-go: update copyright for 2015
[s-roff.git] / src / ute-pfbtops / pfbtops.c
blob98fb9dc7eca3adb52c6d7c78efa4f5bc21b6840a
1 /*@ This translates ps fonts in .pfb format to ASCII ps files.
3 * Copyright (c) 2014 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
5 * Copyright (C) 1992, 2001, 2003 - 2005 Free Software Foundation, Inc.
6 * Written by James Clark (jjc@jclark.com)
8 * This is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2, or (at your option) any later
11 * version.
13 * This is distributed in the hope that it will be useful, but WITHOUT ANY
14 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with groff; see the file COPYING. If not, write to the Free Software
20 * Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "config.h"
24 #include "pfbtops-config.h"
26 #define __GETOPT_PREFIX groff_ /* FIXME */
28 #include <limits.h>
29 #include <stdio.h>
30 #include <stdlib.h>
32 #include <getopt.h> /* FIXME */
33 #include "nonposix.h"
35 #define HEX_DIGITS "0123456789abcdef"
37 static char *program_name;
39 static void error(const char *s)
41 fprintf(stderr, "%s: %s\n", program_name, s);
42 exit(2);
45 static void usage(FILE *stream)
47 fprintf(stream, "Synopsis: %s [-v] [pfb_file]\n", program_name);
50 static void get_text(int n)
52 int c = 0, c1;
53 int in_string = 0;
54 int is_comment = 0;
55 int count = 0;
57 while (--n >= 0) {
58 c = getchar();
59 if (c == '(' && !is_comment)
60 in_string++;
61 else if (c == ')' && !is_comment)
62 in_string--;
63 else if (c == '%' && !in_string)
64 is_comment = 1;
65 else if (c == '\\' && in_string) {
66 count++;
67 putchar(c);
68 if (n-- == 0)
69 break;
70 c = getchar();
71 /* don't split octal character representations */
72 if (c >= '0' && c <= '7') {
73 count++;
74 putchar(c);
75 if (n-- == 0)
76 break;
77 c = getchar();
78 if (c >= '0' && c <= '7') {
79 count++;
80 putchar(c);
81 if (n-- == 0)
82 break;
83 c = getchar();
84 if (c >= '0' && c <= '7') {
85 count++;
86 putchar(c);
87 if (n-- == 0)
88 break;
89 c = getchar();
94 if (c == EOF)
95 error("end of file in text packet");
96 else if (c == '\r') {
97 if (n-- == 0)
98 break;
99 c1 = getchar();
100 if (c1 != '\n') {
101 ungetc(c1, stdin);
102 n++;
104 c = '\n';
106 if (c == '\n') {
107 count = 0;
108 is_comment = 0;
110 else if (count >= MAX_LINE_LENGTH) {
111 if (in_string > 0) {
112 count = 1;
113 putchar('\\');
114 putchar('\n');
116 else if (is_comment) {
117 count = 2;
118 putchar('\n');
119 putchar('%');
121 else {
122 /* split at the next whitespace character */
123 while (c != ' ' && c != '\t' && c != '\f') {
124 putchar(c);
125 if (n-- == 0)
126 break;
127 c = getchar();
129 count = 0;
130 putchar('\n');
131 continue;
134 count++;
135 putchar(c);
137 if (c != '\n')
138 putchar('\n');
141 static void get_binary(int n)
143 int c;
144 int count = 0;
146 while (--n >= 0) {
147 c = getchar();
148 if (c == EOF)
149 error("end of file in binary packet");
150 if (count >= BYTES_PER_LINE) {
151 putchar('\n');
152 count = 0;
154 count++;
155 putchar(HEX_DIGITS[(c >> 4) & 0xf]);
156 putchar(HEX_DIGITS[c & 0xf]);
158 putchar('\n');
161 int main(int argc, char **argv)
163 int opt;
164 static const struct option long_options[] = {
165 { "help", no_argument, 0, CHAR_MAX + 1 },
166 { "version", no_argument, 0, 'v' },
167 { NULL, 0, 0, 0 }
170 program_name = argv[0];
172 while ((opt = getopt_long(argc, argv, "v", long_options, NULL)) != EOF) {
173 switch (opt) {
174 case 'v':
175 printf(L_PFBTOPS " (" T_ROFF ") v" VERSION);
176 exit(0);
177 break;
178 case CHAR_MAX + 1: /* --help */
179 usage(stdout);
180 exit(0);
181 break;
182 case '?':
183 usage(stderr);
184 exit(1);
185 break;
189 if (argc - optind > 1) {
190 usage(stderr);
191 exit(1);
193 if (argc > optind && !freopen(argv[optind], "r", stdin)) {
194 perror(argv[optind]);
195 exit(1);
197 SET_BINARY(fileno(stdin));
198 for (;;) {
199 int type, c, i;
200 long n;
202 c = getchar();
203 if (c != 0x80)
204 error("first byte of packet not 0x80");
205 type = getchar();
206 if (type == 3)
207 break;
208 if (type != 1 && type != 2)
209 error("bad packet type");
210 n = 0;
211 for (i = 0; i < 4; i++) {
212 c = getchar();
213 if (c == EOF)
214 error("end of file in packet header");
215 n |= (long)c << (i << 3);
217 if (n < 0)
218 error("negative packet length");
219 if (type == 1)
220 get_text(n);
221 else
222 get_binary(n);
224 exit(0);
227 // s-it2-mode