Debian Codegraph @

texk/web2c/omegafonts/omfonts.c

raw
1 /* omfonts.c: Main routine for ofm2opl, opl2ofm, ovf2ovp, ovp2ovf.
2
3 This file is part of Omega,
4 which is based on the web2c distribution of TeX,
5
6 Copyright (c) 1994--2001 John Plaice and Yannis Haralambous
7
8 Omega is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 Omega is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Omega; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
21
22 */
23
24 #define OMFONTS
25 #define OFM2OPL
26 #define OPL2OFM
27 #define OVP2OVF
28 #define OVF2OVP
29 #include "cpascal.h"
30 #include "parser.h"
31 #include "manifests.h"
32 #include "omfonts.h"
33 #include "list_routines.h"
34 #include "error_routines.h"
35 #include "header_routines.h"
36 #include "font_routines.h"
37 #include "param_routines.h"
38 #include "char_routines.h"
39 #include "dimen_routines.h"
40 #include "parse_ofm.h"
41 #include "out_ofm.h"
42
43 #define PROG_MIN 0
44 #define PROG_OFM2OPL 0
45 #define PROG_OPL2OFM 1
46 #define PROG_OVF2OVP 2
47 #define PROG_OVP2OVF 3
48 #define PROG_OMFONTS 4
49 #define PROG_NUM 5
50
51 #define cmdline(i) (argv[i])
52
53 int verbose_option;
54 int char_format = CHAR_CODE_NUM;
55 int num_format = NUM_CODE_HEX;
56 int text_format = TEXT_CODE_MIXED;
57 int omit_ofm = 0;
58
59 int program;
60
61 const_string name_ofm = NULL;
62 const_string name_opl = NULL;
63 const_string name_ovp = NULL;
64 const_string name_ovf = NULL;
65
66 FILE *file_ofm = NULL;
67 FILE *file_opl = NULL;
68 FILE *file_ovp = NULL;
69 FILE *file_ovf = NULL;
70 FILE *file_output = NULL;
71
72 unsigned char *ofm = NULL;
73 unsigned char *ovf = NULL;
74
75 unsigned length_ofm = 0;
76 unsigned length_ovf = 0;
77
78 const_string name_program;
79
80 const_string OMFONTSHELP[] = {
81 "Usage: omfonts -ofm2opl [OPTION]... FILE...",
82 " omfonts -opl2ofm [OPTION]... FILE...",
83 " omfonts -ovf2ovp [OPTION]... FILE...",
84 " omfonts -ovp2ovf [OPTION]... FILE...",
85 "",
86 "be ofm2opl, opl2ofm, ovf2ovp, or ovp2ovf",
87 NULL
88 };
89
90 static const_string names_program[PROG_NUM] =
91 { "ofm2opl", "opl2ofm", "ovf2ovp", "ovp2ovf", "omfonts" };
92 static const_string *names_help[PROG_NUM] =
93 { OFM2OPLHELP, OPL2OFMHELP, OVF2OVPHELP, OVP2OVFHELP, OMFONTSHELP };
94 static const_string names_msg[PROG_NUM] = {
95 "This is ofm2opl, Version 2.2",
96 "This is opl2ofm, Version 2.2",
97 "This is ovf2ovp, Version 2.2",
98 "This is ovp2ovf, Version 2.2",
99 "This is omfonts, Version 2.2"
100 };
101
102 int no_files=0;
103 const_string *files[3] = {NULL, NULL, NULL};
104 const_string suffixes[3] = {NULL, NULL, NULL};
105 const_string full_suffixes[3] = {NULL, NULL, NULL};
106
107 static struct option long_options[] = {
108 {"verbose", 0, &verbose_option, 1},
109 {"char-format", 1, 0, 0},
110 {"num-format", 1, 0, 0},
111 {"charcode-format", 1, 0, 0},
112 {"text-format", 1, 0, 0},
113 {"omit-ofm", 0, 0, 0},
114 {"ofm2opl", 0, 0, 0},
115 {"opl2ofm", 0, 0, 0},
116 {"ovf2ovp", 0, 0, 0},
117 {"ovp2ovf", 0, 0, 0},
118 {"help", 0, 0, 0},
119 {"version", 0, 0, 0},
120 {0, 0, 0, 0}
121 };
122
123 static void read_in_whole(unsigned char **, unsigned *, FILE *, const_string );
124 static void init_tables(void);
125
126 int
127 main (int argc, string *argv)
128 {
129 int getopt_return_val;
130 int option_index = 0;
131
132 name_program = kpse_program_basename(argv[0]);
133 if (FILESTRCASEEQ(name_program, "ofm2opl"))
134 program = PROG_OFM2OPL;
135 else if (FILESTRCASEEQ(name_program, "opl2ofm"))
136 program = PROG_OPL2OFM;
137 else if (FILESTRCASEEQ(name_program, "ovf2ovp"))
138 program = PROG_OVF2OVP;
139 else if (FILESTRCASEEQ(name_program, "ovp2ovf"))
140 program = PROG_OVP2OVF;
141 else
142 program = PROG_OMFONTS;
143
144 do {
145 getopt_return_val =
146 getopt_long_only(argc, argv, "", long_options, &option_index) ;
147 if (getopt_return_val == -1) { ; }
148 else if ( getopt_return_val == '?' ) {
149 usage (names_program[program]);
150 } else if (!strcmp(long_options[option_index].name, "help")) {
151 usagehelp (names_help[program], NULL);
152 } else if (!strcmp(long_options[option_index ].name, "version")) {
153 printversionandexit(names_msg[program], nil,
154 "J. Plaice, Y. Haralambous, D.E. Knuth",
155 nil);
156 } else if (!strcmp(long_options[option_index ].name, "ofm2opl")) {
157 if (program == PROG_OMFONTS) program = PROG_OFM2OPL;
158 else usage (names_program[program]);
159 } else if (!strcmp(long_options[option_index ].name, "opl2ofm")) {
160 if (program == PROG_OMFONTS) program = PROG_OPL2OFM;
161 else usage (names_program[program]);
162 } else if (!strcmp(long_options[option_index ].name, "ovf2ovp")) {
163 if (program == PROG_OMFONTS) program = PROG_OVF2OVP;
164 else usage (names_program[program]);
165 } else if (!strcmp(long_options[option_index ].name, "ovp2ovf")) {
166 if (program == PROG_OMFONTS) program = PROG_OVP2OVF;
167 else usage (names_program[program]);
168 } else if (!strcmp(long_options[option_index ].name, "omit-ofm")) {
169 omit_ofm = 1;
170 } else if (!strcmp(long_options[option_index ].name, "char-format")) {
171 if (!strcmp(optarg, "ascii")) char_format = CHAR_CODE_ASCII;
172 else if (!strcmp(optarg, "num")) char_format = CHAR_CODE_NUM;
173 else warning_s("Bad character code format (%s)", optarg);
174 } else if (!strcmp(long_options[option_index ].name, "num-format")) {
175 if (!strcmp(optarg, "hex")) num_format = NUM_CODE_HEX;
176 else if (!strcmp(optarg, "octal")) num_format = NUM_CODE_OCTAL;
177 else warning_s("Bad number code format (%s)", optarg);
178 } else if (!strcmp(long_options[option_index ].name, "charcode-format")) {
179 if (!strcmp(optarg, "ascii")) {
180 char_format = CHAR_CODE_ASCII;
181 num_format = NUM_CODE_HEX;
182 } else if (!strcmp(optarg, "hex")) {
183 char_format = CHAR_CODE_NUM;
184 num_format = NUM_CODE_HEX;
185 } else if (!strcmp(optarg, "octal")) {
186 char_format = CHAR_CODE_NUM;
187 num_format = NUM_CODE_OCTAL;
188 } else {
189 warning_s("Bad character code format (%s)", optarg);
190 }
191 } else if (!strcmp(long_options[option_index ].name, "text-format")) {
192 if (!strcmp(optarg, "upper")) text_format = TEXT_CODE_UPPER;
193 else if (!strcmp(optarg, "mixed")) text_format = TEXT_CODE_MIXED;
194 else warning_s("Bad text code format (%s)", optarg);
195 }
196 } while (getopt_return_val != -1);
197
198 switch(program) {
199 case PROG_OFM2OPL: {
200 no_files = 2;
201 files[0] = &name_ofm;
202 files[1] = &name_opl;
203 suffixes[0] = "ofm";
204 suffixes[1] = "opl";
205 full_suffixes[0] = ".ofm";
206 full_suffixes[1] = ".opl";
207 break;
208 }
209 case PROG_OPL2OFM: {
210 no_files = 2;
211 files[0] = &name_opl;
212 files[1] = &name_ofm;
213 suffixes[0] = "opl";
214 suffixes[1] = "ofm";
215 full_suffixes[0] = ".opl";
216 full_suffixes[1] = ".ofm";
217 break;
218 }
219 case PROG_OVF2OVP: {
220 no_files = 3;
221 files[0] = &name_ovf;
222 files[1] = &name_ofm;
223 files[2] = &name_ovp;
224 suffixes[0] = "ovf";
225 suffixes[1] = "ofm";
226 suffixes[2] = "ovp";
227 full_suffixes[0] = ".ovf";
228 full_suffixes[1] = ".ofm";
229 full_suffixes[2] = ".ovp";
230 break;
231 }
232 case PROG_OVP2OVF: {
233 no_files = 3;
234 files[0] = &name_ovp;
235 files[1] = &name_ovf;
236 files[2] = &name_ofm;
237 suffixes[0] = "ovp";
238 suffixes[1] = "ovf";
239 suffixes[2] = "ofm";
240 full_suffixes[0] = ".ovp";
241 full_suffixes[1] = ".ovf";
242 full_suffixes[2] = ".ofm";
243 break;
244 }
245 default: {
246 fprintf(stderr , "Unrecognized program: %s\n", name_program);
247 fprintf(stderr ,
248 "This binary supports ofm2opl, opl2ofm, ovf2ovp, and ovp2ovf\n");
249 usage ("omfonts");
250 }
251 }
252
253 name_program = names_program[program];
254 kpse_set_program_name(argv[0], name_program);
255 kpse_init_prog(uppercasify(name_program), 0, nil, nil);
256
257 if (((argc-optind) > no_files) || ((argc-optind) < 1)) {
258 fprintf(stderr , "%s: %s\n", name_program,
259 no_files == 2 ? "Need one or two file arguments."
260 : "Need one to three file arguments.");
261 usage (name_program);
262 }
263 *(files[0]) = extend_filename(cmdline(optind) , suffixes[0]);
264 if (optind+2 <= argc) {
265 *(files[1]) = extend_filename(cmdline(optind+1) , suffixes[1]);
266 if (no_files == 3) {
267 if (optind+3 <= argc) {
268 *(files[2]) = extend_filename(cmdline(optind+2) , suffixes[2]);
269 } else if (program == PROG_OVP2OVF) {
270 *(files[2]) = make_suffix(cmdline(optind+1), suffixes[2]);
271 }
272 }
273 } else if (program != PROG_OFM2OPL) {
274 *(files[1]) = basenamechangesuffix(*(files[0]),
275 full_suffixes[0], full_suffixes[1]);
276 if ((no_files == 3) && (program == PROG_OVP2OVF)) {
277 *(files[2]) = basenamechangesuffix(*(files[0]),
278 full_suffixes[0], full_suffixes[2]);
279 }
280 }
281
282 switch(program) {
283 case PROG_OFM2OPL: {
284 file_ofm = kpse_open_file(name_ofm, kpse_ofm_format);
285 read_in_whole(&ofm, &length_ofm, file_ofm, name_ofm);
286 (void)fclose(file_ofm);
287 if (name_opl==NULL) file_opl = stdout;
288 else rewrite(file_opl, name_opl);
289 file_output = file_opl;
290 parse_ofm(FALSE);
291 break;
292 }
293 case PROG_OVF2OVP: {
294 file_ovf = kpse_open_file(name_ovf, kpse_ovf_format);
295 read_in_whole(&ovf, &length_ovf, file_ovf, name_ovf);
296 (void)fclose(file_ovf);
297 file_ofm = kpse_open_file(name_ofm, kpse_ofm_format);
298 read_in_whole(&ofm, &length_ofm, file_ofm, name_ofm);
299 (void)fclose(file_ofm);
300 if (name_ovp==NULL) file_ovp = stdout;
301 else rewrite(file_ovp, name_ovp);
302 file_output = file_ovp;
303 parse_ofm(TRUE);
304 break;
305 }
306 case PROG_OPL2OFM: {
307 file_opl = kpse_open_file(name_opl, kpse_opl_format);
308 rewritebin(file_ofm, name_ofm);
309 init_tables();
310 yyin = file_opl;
311 (void)yyparse();
312 output_ofm_file();
313 (void)fclose(file_ofm);
314 if (num_errors > 0) exit(1);
315 break;
316 }
317 case PROG_OVP2OVF: {
318 file_ovp = kpse_open_file(name_ovp, kpse_ovp_format);
319 rewritebin(file_ovf, name_ovf);
320 if (!omit_ofm) rewritebin(file_ofm, name_ofm);
321 init_tables();
322 yyin = file_ovp;
323 (void)yyparse();
324 output_ofm_file();
325 if (!omit_ofm) (void)fclose(file_ofm);
326 output_ovf_file();
327 (void)fclose(file_ovf);
328 if (num_errors > 0) exit(1);
329 break;
330 }
331 default: {exit(1);}
332 }
333 exit(0);
334 }
335
336 #define BIG_BLOCK 0x20000
337 #define LITTLE_BLOCK 0x1000
338
339 static void
340 read_in_whole(unsigned char **contents_loc,
341 unsigned *length_loc,
342 FILE *file,
343 const_string name)
344 {
345 unsigned no_read;
346 unsigned no_total_read = 0;
347 unsigned size = BIG_BLOCK;
348 string where;
349 string current_block;
350
351 current_block = (char *) xmalloc(size);
352 where = current_block;
353
354 while (1) {
355 no_read = fread(where, 1, LITTLE_BLOCK, file);
356 no_total_read += no_read;
357 if ((no_total_read+LITTLE_BLOCK) > size) {
358 size *= 2;
359 current_block = (char *) xrealloc(current_block, size);
360 }
361 where = current_block + no_total_read;
362 if (ferror(file)) {
363 fatal_error_s("Error while reading file %s", name);
364 } else if (feof(file)) {
365 break;
366 }
367 }
368
369 *contents_loc = (unsigned char *) current_block;
370 *length_loc = no_total_read;
371 }
372
373 static void
374 init_tables(void)
375 {
376 font_table_init(); /* subsidiary fonts in virtual fonts */
377 init_header();
378 init_planes();
379 init_measures();
380 }
381