Debian Codegraph @

texk/dviout-util/dvispc.c

raw
1 /*
2 * <<< dvispc >>>
3 *
4 * A program to modify DVI file to be page-independent
5 * Support the following specials
6 * color specials: push, pop, background
7 * pdf specials: pdf:bcolor, pdf:ecolor, pdf:bgcolor
8 * tpic specials: pn
9 *
10 * Originally written by SHIMA
11 * January 2003
12 */
13
14 #ifndef WIN32
15 #define UNIX 1
16 #endif
17
18 /* %%% Contents of an extended DVI file %%%
19 * PRE(247)
20 * ... % PREAMBRE %
21 * ...
22 *
23 * BOP(139)
24 * c[10][4]
25 * p[4] -1
26 * ... % Contents of the first pages
27 * EOP(140)
28 *
29 * BOP(139)
30 * ... % Contents of pages
31 * ...
32 *
33 * BOP(139) % top of the last page
34 * c[10][4]
35 * p[4] address of BOP at the top of the former page
36 * ... % Contents of the last page
37 * EOP(140) % end of the last page
38 */
39
40 /* % Added part %
41 * POST(248)
42 * Data[num_add][?] series of data
43 * FileNames[num_add][?] series of strings NULL terminated
44 * pt_Data[num_add][BOD[4], LOD[4]] pointer and length of data
45 *
46 * % 20 byte
47 * BOF[4] top of series of strings
48 * EOF[4] end of series of strings
49 * num_add[4] number of data
50 * top_add[4] address of POST at the top of added part
51 * AdID[3] 'A', 'd', 'I'
52 * EOP(140) % End of Added part %
53 */
54
55 /*
56 * POST(248) % Top of POSTAMBRE %
57 * p[4] address of BOP at the top of the last page
58 * ....
59 * POST_POST(249)
60 * top_post[4] address of POST at the top of POSTAMBRE
61 * 2 (or 3), 223,...,223 4 - 7 times 223
62 */
63
64 /*
65 * The extended DVI file should be the following
66 *
67 * 1. The 8 byte preceeding to the top of the POSTAMBRE:
68 * top_add[4], 'A', 'd', 'O', 140(=EOP)
69 *
70 * 2. DVI[top_add[4]] 140(=EOP)
71 * DVI[top_add[4]-1] 248(=POS)
72 */
73
74 #include <stdio.h>
75 #include <stdlib.h>
76 #include <string.h>
77 #include <ctype.h>
78
79 #ifdef UNIX /* for mkstemp(), umask() etc. */
80 #include <unistd.h> /* for mkstemp(), umask() etc. */
81 #include <sys/types.h>
82 #include <sys/stat.h>
83 #else
84 #include <io.h>
85 #include <fcntl.h>
86 #endif
87
88 #include <config.h>
89 #ifdef KPATHSEA
90 #include <kpathsea/config.h>
91 #if defined(WIN32)
92 #include <kpathsea/variable.h>
93 #endif
94 #endif
95 #ifdef PTEXENC
96 #include <ptexenc/ptexenc.h>
97 #include <ptexenc/unicode.h>
98 #endif
99
100 #include "common.h"
101
102 #define uchar unsigned char
103 #define uint unsigned int
104 #define Long int
105
106 #ifdef UNIX
107 #define READ_TEXT "r"
108 #define READ_BINARY "r"
109 #define WRITE_BINARY "w"
110 #define WRITE_TEXT "w"
111 #define StrCmp strcmp
112 #else
113 #define READ_TEXT "rt"
114 #define READ_BINARY "rb"
115 #define WRITE_BINARY "wb"
116 #define WRITE_TEXT "wt"
117 #define StrCmp stricmp
118 #endif
119 #if defined(WIN32) && defined(KPATHSEA)
120 #undef fopen
121 #undef fprintf
122 #undef fputs
123 #undef putc
124 #define fopen fsyscp_fopen
125 #define fprintf win32_fprintf
126 #define fputs win32_fputs
127 #define putc win32_putc
128 #endif
129
130 #define PIXEL int
131 #define SIZE_PARA int
132 #define PAGE_INDEX int
133 #define SCALED_PT int
134
135 #define DIMENSION struct DIMENSION_REC
136 /* Information about device-output
137 */
138
139 struct DIMENSION_REC {
140 PIXEL text_width, text_height;
141 /* These are the maxmum size directed in DVI-file.
142 */
143 int mag;
144 /* is the magnification for output. This is the same value as 'mag' in
145 * DVI-file ( so equal the one in DVI_INFO ), when no '-mag' & '-half'
146 * options directed.
147 */
148 SIZE_PARA size_para;
149 /* is size-parameter for conversion pixel<-->scaled pt.
150 * ( equal "num * den / mag" )
151 */
152 PAGE_INDEX *page_index;
153 /* the index of file-offset of pages in dvifile
154 */
155 int total_page;
156 /* same as 'total_page' in DVI_INFO.
157 */
158 #if 0
159 int start_page, end_page;
160 /* the starting and the ending page to print.
161 */
162 #endif
163 int max_nombre_page;
164 /* the maximal nombre page
165 */
166 } dvi_dim;
167
168
169 #define MAX_INCL 2048 /* maximal number of embedded files */
170 #ifndef MAX_PATH
171 #define MAX_PATH 0x200 /* maximal length of path name */
172 #endif
173
174 #define ID 2
175 #define IDP 3
176 #define END_DVI 223
177
178 #define SET_CHAR_0 0
179 #define SET1 128
180 #define SET_RULE 132
181 #define PUT1 133
182 #define PUT_RULE 137
183 #define NOP 138
184 #define BOP 139
185 #define EOP 140
186 #define PUSH 141
187 #define POP 142
188 #define RIGHT1 143
189 #define W0 147
190 #define W1 148
191 #define X0 152
192 #define X1 153
193 #define DOWN1 157
194 #define Y0 161
195 #define Y1 162
196 #define Z0 166
197 #define Z1 167
198 #define FNT_NUM_0 171
199 #define FNT1 235
200 #define XXX1 239
201 #define FNT_DEF_1 243
202 #define FNT_DEF_4 246
203 #define PRE 247
204 #define POST 248
205 #define POST_POST 249
206 #define OPCODE 250
207 #define EOFNC 255 /* end of func */
208
209 #define DVIFILE_INFO struct DVIFILE_INFO_REC
210
211 #define MAX_LEN 128
212 #define MAX_COLOR 512
213 #define COLOR_BUF_SIZE MAX_COLOR*32
214 #define MAX_ANNOT 8
215 #define ANNOT_BUF_SIZE MAX_ANNOT*512
216 #define COMMON_SIZE 0x4000
217 #define MAX_FONT 256
218
219 #define DTL_STRING 1 /* 0 \' \\ */
220 #define DTL_CHAR 2 /* 1 (...) */
221 #define DTL_CHAR2 4 /* 2 character code by dig/hex */
222 #define DTL_CMD 8 /* 3 compact command name */
223 #define DTL_FNTDEF 0x10 /* 4 checksum is in octal */
224 #define DTL_FNTNAME 0x20 /* 5 dir/name */
225 #define DTL_ILL 0x40 /* 6 opcode/illegal */
226 #define DTL_PAGE 0x80 /* 7 */
227 #define DTL_OCT 0x100 /* 8 */
228 #define DTL_EXACT 0x200 /* 9 -> DVI according to string */
229 #define DTL_FNTDFN 0x400 /* 1 check used font definition */
230 #define DTL_VARIETY 0x800 /* 2 ignore variety */
231 #define DTL_FORM 0x8000 /* the first line: variety */
232 int f_dtl = 0;
233
234 enum {
235 EXE2MODIFY, EXE2CHECK, EXE2SPECIAL, EXE2TEXT, EXE2DVI
236 };
237
238 int f_mode = EXE2MODIFY; /* 0: -c modify
239 1: -d report only
240 2: -s specials
241 3: -a to_Text
242 4: -x to_DVI */
243
244 int f_debug = 0; /* -v */
245 int f_overwrite = 0;
246 int f_backup = 0; /* -b */
247
248 #ifdef PTEXENC
249 int f_jstr = 0; /* -J */
250 #else
251 #ifdef UNIX
252 int f_sjis = 0; /* -j */
253 #else
254 int f_sjis = 1;
255 #endif
256 #endif
257 int f_pos = 0; /* position */
258 int f_book = 0; /* multiple of four pages */
259
260 int f_ptex = 0;
261 int f_prescan = 0;
262 int f_last = 0;
263 int max_stack;
264 char *out_pages ="T-L";
265 int total_book_page;
266
267 /* non-stack specials */
268 char background[MAX_LEN];
269 char background_prev[MAX_LEN];
270 int f_background = 0; /* in each page, 0: not found; 1: found */
271 char pdf_bgcolor[MAX_LEN];
272 char pdf_bgcolor_prev[MAX_LEN];
273 int f_pdf_bgcolor = 0; /* in each page, 0: not found; 1: found */
274 char tpic_pn[MAX_LEN];
275 char tpic_pn_prev[MAX_LEN];
276 int f_pn = 0; /* in each page, 0: not found and not actually used;
277 1: found before actual use
278 -1: not found before actual use */
279
280 /* stack specials */
281 int color_depth;
282 int color_depth_max;
283 int color_under;
284 char *color_pt[MAX_COLOR];
285 int pdf_color_depth;
286 int pdf_color_depth_max;
287 int pdf_color_under;
288 char *pdf_color_pt[MAX_COLOR];
289 int pdf_annot_depth;
290 int pdf_annot_depth_max;
291 int pdf_annot_under;
292 char *pdf_annot_pt[MAX_ANNOT];
293
294 int f_needs_corr; /* flag to determine if correction is needed */
295 char color_buf[COLOR_BUF_SIZE]; /* common buffer for color/pdf_color */
296 char annot_buf[ANNOT_BUF_SIZE];
297
298 char tmp_buf[COMMON_SIZE];
299 FILE *fp_in;
300 FILE *fp_out;
301
302 #define DVIFILE_INFO struct DVIFILE_INFO_REC
303 /* Information about Preamble of DVI-file. ( to say the truth, this is
304 * the same thing as Preamble itself. So, you'd better see
305 * "TeX : The Program." )
306 */
307 struct DVIFILE_INFO_REC {
308 FILE *file_ptr;
309 char *file_name;
310 Long post, pt_post, last_bop;
311 Long num, den, mag;
312 SCALED_PT l, u;
313 int stack_depth;
314 int total_page;
315 } dvi_info;
316
317 char infile[MAX_PATH];
318 char outfile[MAX_PATH];
319 char font_use[MAX_FONT];
320
321 const int AdID = (('A'<<24)+('d'<<16)+('O'<<8)+EOP);
322
323 #ifndef PTEXENC
324 // #define issjis1(c) ((c)>=0x81&&(c)<=0xfc&&((c)<=0x9f||(c)>=0xe0))
325 // #define issjis2(c) ((c)>=0x40 && (c)<=0xfc && (c)!=0x7f)
326 #endif
327 #define isjis(c) (((c)>=0x21 && (c)<=0x7e))
328 #define is_hex(c) ((c>='0'&&c<='9')||(c>='a'&&c<='f')||(c>='A'&&c<='F'))
329 #define is_oct(c) (c>='0'&&c<='7')
330 // #define is_dig(c) (c>='0'&&c<='9')
331 #define hex_to_dig(c) ((c>='0'&&c<='9')?(c-'0'):(toupper(c)-('A'-10)))
332 #define MSG(x) (f_debug?(x):"")
333 #define read_byte(x) getc(x)
334 #define write_byte(x,y) putc(x,y)
335 #define read_short(x) signed_read_n(x,2)
336
337 uint work(FILE *);
338 uint s_work(FILE *);
339 int strsubcmp(char *s, char *t);
340 int strsubcmp_n(char *s, char *t);
341 void sp_color(char *sp);
342 void sp_pdf_bcolor(char *sp);
343 void sp_pdf_ecolor(char *sp);
344 void sp_pdf_bann(char *sp);
345 void sp_pdf_eann(char *sp);
346 void read_post(DVIFILE_INFO *dvi);
347 uint interpret(FILE *);
348 void make_page_index(DVIFILE_INFO *dvi, DIMENSION *dim);
349 void translate(DVIFILE_INFO *dvi, DIMENSION *dim);
350 void transpre(FILE *);
351 void transpost(FILE *);
352 void trans2dvi(void);
353 void replace(char *, char *);
354
355 Long read_n(FILE * fp, int n)
356 {
357 long d;
358
359 if(n--)
360 d = (unsigned char)getc(fp);
361 else
362 return 0;
363 while (n-- > 0)
364 d = (d<<8) + (unsigned char)getc(fp);
365 return d;
366 }
367
368
369 Long signed_read_n(FILE * fp, int n)
370 {
371 long d;
372
373 if(n--)
374 d = (signed char)getc(fp);
375 else
376 return 0;
377 while (n-- > 0)
378 d = (d<<8) + (unsigned char)getc(fp);
379 return d;
380 }
381
382
383 void Exit(int code)
384 {
385 if(fp_out != stdout && fp_out != stderr && *outfile){
386 if(fp_out){
387 fclose(fp_out);
388 unlink(outfile);
389 }
390 }
391 exit(code);
392 }
393
394
395 void error(char *msg)
396 {
397 fprintf(stderr, "%s\n", msg);
398 Exit(1);
399 }
400
401
402 void write_long(int x, FILE *fp)
403 {
404 write_byte(x >> 24, fp);
405 write_byte((x >> 16) & 0xff, fp);
406 write_byte((x >> 8) & 0xff, fp);
407 write_byte(x & 0xff, fp);
408 }
409
410
411 Long read_long(FILE *fp)
412 {
413 int i;
414
415 i = read_byte(fp) << 24;
416 i += read_byte(fp) << 16;
417 i += read_byte(fp) << 8;
418 return i + read_byte(fp);
419 }
420
421
422 void write_word(int x, FILE *fp)
423 {
424 write_byte((x >> 8) & 0xff, fp);
425 write_byte(x & 0xff, fp);
426 }
427
428
429 Long read_word(FILE *fp)
430 {
431 int i;
432
433 i = read_byte(fp) << 8;
434 return i + read_byte(fp);
435 }
436
437
438 void usage(int ex)
439 {
440 fprintf(stderr,
441 "\t Modify a DVI file to be page-independent in specials\n"
442 "\t Translation between DVI file <-> Text file\n"
443 "\t Originally written by SHIMA, Jan. 2003\n"
444 "\t Ver.%s (%s)\n\n", VERSION, TL_VERSION);
445 fprintf(stderr,
446 "Usage: dvispc [-c] [-bvz] input_dvi_file [output_dvi_file]\n"
447 " dvispc -d input_dvi_file\n"
448 " dvispc -s [-p..] input_dvi_file [output_text_file]\n"
449 #ifdef PTEXENC
450 " dvispc -a [-ltv][-J..][-p..][-r..] input_dvi_file [output_text_file]\n"
451 #else
452 " dvispc -a [-jltv][-p..][-r..] input_dvi_file [output_text_file]\n"
453 #endif
454 " dvispc -x[..] [-ltv][-r..] [input_text_file] output_dvi_file\n\n"
455 "Mode options:\n"
456 " -c: correct input DVI to be page-indepent in specials (default)\n"
457 " -d: dry run to check page-independence\n"
458 " -s: show specials\n"
459 " -a: translate DVI to Text\n"
460 " -x: translate Text to DVI (-x0:str0 1:chkfnt 2:variety)\n\n"
461 "Other options:\n"
462 #ifdef PTEXENC
463 " -v: verbose -l: location\n"
464 #else
465 " -v: verbose -j: Japanese characters -l: location\n"
466 #endif
467 " -b: backup original even if output_dvi_file is not given\n"
468 " -z: append empty pages if necessary to have multiple of 4 pages for book\n"
469 " -p: T:preamble L:postamble pages with - (eg: -pT-L -pT2/4-8L -p-4 etc.)\n"
470 " -r: replace (-rorg_1=new_1/org_2=new_2... eg. -rxxx=special/fnt=font)\n"
471 " -t: compatible to DTL (the followings are suboptions if necessary eg. -t02)\n"
472 " 0:str 1:ch 2:ch2 3:cmd 4:c-sum 5:dir/name 6:err 7:page 8:oct 9:str0\n"
473 #ifdef PTEXENC
474 " -J: set Japanese characters output with a suboption:\n"
475 " e:EUC-JP s:Shift_JIS u:UTF-8 for pTeX or U:UTF-8 for upTeX.\n"
476 #endif
477 " output_dvi_file : overwrite if it is not specified.\n"
478 " output_text_file : stdout if it is not specified.\n"
479 " input_text_file : stdin if it is not specified.\n\n"
480 "Supported specials:\n"
481 " color specials: push, pop, background\n"
482 " pdf specials: pdf:bcolor, pdf:ecolor, pdf:bgcolor\n"
483 " tpic specials: pn\n"
484 );
485 fprintf(stderr, "\nEmail bug reports to %s.\n", BUG_ADDRESS);
486 exit(ex);
487 }
488
489
490 int main(int argc, char **argv)
491 {
492 int i, len, ch, fnum;
493 char *o_name, *n_name, *s;
494
495 if(argc <= 1)
496 usage(0);
497
498 #if defined(WIN32) && defined(KPATHSEA)
499 {
500 int ac;
501 char **av, *enc;
502
503 kpse_set_program_name(argv[0], "dvispc");
504 enc = kpse_var_value("command_line_encoding");
505 if (get_command_line_args_utf8(enc, &ac, &av)) {
506 argc = ac;
507 argv = av;
508 }
509 }
510 #endif
511
512 for(i = 1; i < argc && argv[i][0] == '-'; i++){
513 for(len = 1; argv[i][len]; len++){
514 switch(argv[i][len]){
515 case 'c':
516 f_mode = EXE2MODIFY;
517 break;
518
519 case 's':
520 f_mode = EXE2SPECIAL;
521 break;
522
523 case 'a':
524 f_mode = EXE2TEXT;
525 break;
526
527 case 'd':
528 f_mode = EXE2CHECK;
529 case 'v':
530 f_debug = 1;
531 break;
532
533 case 'x':
534 f_mode = EXE2DVI;
535 if(isdigit(argv[i][len+1])){
536 while(isdigit(ch = argv[i][++len]))
537 f_dtl ^= (1 << (ch - ('0'-9)));
538 len--;
539 }
540 break;
541
542 case 'b':
543 f_backup = 1;
544 break;
545
546 #ifdef PTEXENC
547 case 'J':
548 f_jstr = 1;
549 switch(argv[i][len+1]){
550 case 'e':
551 set_enc_string ("euc", "default"); break;
552 case 's':
553 set_enc_string ("sjis", "default"); break;
554 case 'u':
555 set_enc_string ("utf8", "default"); break;
556 case 'U':
557 enable_UPTEX(true);
558 set_enc_string ("utf8", "uptex"); break;
559 default:
560 fprintf(stderr, "Unknown option:%s\n", argv[i]);
561 exit(1);
562 }
563 len++;
564 break;
565 #else
566 case 'j':
567 f_sjis = 1 - f_sjis;
568 break;
569 #endif
570
571 case 'p':
572 s = out_pages = argv[i]+len+1;
573 if(!*out_pages)
574 out_pages = "T-L";
575 else{
576 while(*s){
577 if((*s >= '0' && *s <= '9')
578 || *s == 'T' || *s == 'L' || *s == '-'){
579 s++;
580 continue;
581 }
582 error: fprintf(stderr, "Error in parameter %s\n", argv[i]);
583 exit(1);
584 }
585 }
586 goto skip;
587
588 case 'r':
589 if(strlen(argv[i]+len+1) > COMMON_SIZE-2){
590 fprintf(stderr, "Too long argument %s\n", argv[i]);
591 exit(1);
592 }
593 strcpy(tmp_buf, argv[i]+len+1);
594 for(o_name = n_name = tmp_buf; *o_name; ){
595 while(*++n_name != 0 && *n_name != '=');
596 if(!*n_name){
597 fprintf(stderr, "Wrong argument %s\n", argv[i]);
598 exit(1);
599 }
600 *n_name++ = 0;
601 for(s = n_name; *++s && *s != '/';);
602 if(!*s)
603 s[1] = 0;
604 *s = 0;
605 if(*o_name < 'a' || *o_name > 'z' || !*n_name)
606 goto error;
607 replace(o_name, n_name);
608 o_name = n_name = s+1;
609 }
610 goto skip;
611
612 case 't':
613 if(isdigit(argv[i][len+1])){
614 while(isdigit(ch = argv[i][++len]))
615 f_dtl ^= (1 << (ch - '0'));
616 len--;
617 }else{
618 #ifndef PTEXENC
619 f_sjis = 1-f_sjis;
620 #endif
621 f_dtl = 0x00ffffff;
622 }
623 break;
624
625 case 'l':
626 f_pos = 1;
627 break;
628
629 case 'z':
630 f_book = 1;
631 break;
632
633 default:
634 fprintf(stderr, "Unknown option:%s\n", argv[i]);
635 exit(1);
636 }
637 }
638 skip: ;
639 }
640 /* now, i = (number of optional arguments) + 1
641 cf. argc = (number of all arguments) + 1
642 {argv[0] is the program name itself} ^^^ */
643
644 fnum = 0;
645 if(!isatty(fileno(stdin))){ /* if stdin is redirected from a file */
646 fp_in = stdin;
647 fnum++;
648 }
649 if(!isatty(fileno(stdout))){ /* if stdout is redirected to a file */
650 fp_out = stdout;
651 fnum++;
652 }
653 if(fnum != 2){
654 len = strlen(argv[argc-1]);
655 if(len >= MAX_PATH-5 || strlen(argv[i-1]) >= MAX_PATH-5)
656 error("Too long filename");
657 }
658
659 switch(argc - i){ /* number of non-optional arguments */
660 case 0:
661 if(fp_in == NULL)
662 /* infile not given, empty stdin; nothing I can do */
663 usage(1);
664 if(fp_out == NULL){
665 /* outfile not given, free stdout;
666 binary cannot be written, text is fine */
667 if(f_mode == EXE2MODIFY || f_mode == EXE2DVI)
668 usage(1);
669 fp_out = stdout;
670 }
671 break;
672
673 case 1:
674 /* if(fnum == 2)
675 usage(1); */
676 if(!fnum){ /* empty stdin, free stdout */
677 /* if EXE2DVI, the only argument might be outfile,
678 but no input available; nothing I can do */
679 if(f_mode == EXE2DVI)
680 usage(1);
681 /* otherwise, the only argument should be infile */
682 strcpy(infile, argv[argc-1]);
683 /* outfile not given;
684 nonetheless binary should be written to a file,
685 text is fine with free stdout */
686 if(f_mode == EXE2MODIFY)
687 strcpy(outfile, argv[argc-1]);
688 else
689 fp_out = stdout;
690 }else
691 /* [TODO] this may not work well? (especially for EXE2DVI) */
692 /* if fp_out == NULL, non-empty stdin and free stdout
693 -> the only argument = outfile (input from stdin)
694 otherwise, redirected stdout
695 -> the only argument = infile
696 if fnum == 2, non-empty stdin will be discarded but don't care!
697 (output to overwrite for EXE2MODIFY, stdout for others) */
698 strcpy((fp_out == NULL)?outfile:infile, argv[argc-1]);
699 break;
700
701 case 2:
702 /* prioritize filename arguments;
703 if fp_in != NULL, non-empty stdin will be discarded but don't care! */
704 strcpy(infile, argv[argc-2]);
705 strcpy(outfile, argv[argc-1]);
706 break;
707 default:
708 usage(1);
709 }
710 #ifndef UNIX
711 if(fp_out && !*outfile && (f_mode == EXE2DVI || f_mode == EXE2MODIFY))
712 setmode( fileno( stdout ), O_BINARY);
713 #endif
714 if(fp_in && !*infile && f_mode != EXE2DVI){
715 fprintf(stderr, "*** stdin is a DVI file. ***\n"
716 "*** Random Access may not be supported! ***\n");
717 #ifndef UNIX
718 setmode( fileno( stdin ), O_BINARY);
719 #endif
720 }
721
722 /* append .dvi suffix if input/output is DVI */
723 if(f_mode == EXE2DVI || f_mode == EXE2MODIFY){
724 len = strlen(outfile);
725 if(len){
726 if(len < 4 || StrCmp(outfile + len - 4, ".dvi"))
727 strcat(outfile, ".dvi");
728 }
729 }
730 if(f_mode != EXE2DVI){
731 len = strlen(infile);
732 if(len){
733 if(len < 4 || StrCmp(infile + len - 4, ".dvi")){
734 strcat(infile, ".dvi");
735 len += 4; /* will be reused later while preparing overwrite */
736 }
737 dvi_info.file_name = infile;
738 }
739 }
740
741 /* -x : text -> DVI */
742 if(f_mode == EXE2DVI){
743 /* use infile if given, otherwise use existing fp_in (= non-empty stdin)
744 note that fp_in and infile are exclusive (already checked above) */
745 if(fp_in == NULL || *infile){
746 fp_in = fopen(infile, READ_TEXT);
747 if(fp_in == NULL){
748 fprintf(stderr, "Cannot open %s for input\n", infile);
749 exit(1);
750 }
751 }
752 /* use outfile if given */
753 if(fp_out == NULL || *outfile){
754 fp_out = fopen(outfile, WRITE_BINARY);
755 if(fp_out == NULL){
756 fprintf(stderr, "Cannot open %s for output\n", outfile);
757 exit(1);
758 }
759 }
760 trans2dvi(); /* files will be closed */
761 return 0;
762 }
763
764 /* dvi->dvi or -d or -s or -a */
765 /* [TODO] comments not added yet */
766 if(argc - i == 1){
767 if(f_mode == EXE2MODIFY && !fnum){
768 #ifdef UNIX
769 static char tmpfile[] = "/tmp/dvispcXXXXXX";
770 int fd;
771 mode_t mask;
772
773 same: mask = umask(077);
774 fd = mkstemp(tmpfile);
775 /* better to use tmpfile() */
776 umask(mask);
777 if(fd == -1){
778 fprintf(stderr, "Cannot open temporary file\n");
779 exit(1);
780 }
781 strcpy(outfile, tmpfile);
782 close(fd); /* will be fopen-ed again afterward */
783 #else
784 same: strcpy(outfile, infile);
785 strcpy(outfile + len - 3, "$$$");
786 #endif
787 f_overwrite = 1;
788 }
789 }else if(argc - i == 2){
790 #ifdef UNIX
791 struct stat infstat, outfstat;
792 if(stat(infile, &infstat) == 0 && stat(outfile, &outfstat) == 0 &&
793 infstat.st_dev == outfstat.st_dev && infstat.st_ino == outfstat.st_ino)
794 #else
795 if(!StrCmp(infile,outfile))
796 #endif
797 goto same;
798 }
799 if(fp_in && !*infile){
800 dvi_info.file_ptr = fp_in;
801 dvi_info.file_name = "stdin";
802 }else if ((dvi_info.file_ptr = fopen(dvi_info.file_name, READ_BINARY)) == NULL){
803 fprintf(stderr, "Cannot open %s for input\n", infile);
804 exit(1);
805 }
806 /* [TODO] I'd like to use outfile if given */
807 if(fp_out == NULL || *outfile){
808 if(!*outfile)
809 fp_out = (f_mode == EXE2TEXT || f_mode == EXE2SPECIAL)?stdout:stderr;
810 else if(f_mode == EXE2MODIFY)
811 fp_out = stderr;
812 else{
813 #ifndef UNIX
814 fp_out = fopen(outfile, WRITE_BINARY);
815 #else
816 fp_out = fopen(outfile, WRITE_TEXT);
817 #endif
818 if(fp_out == NULL){
819 fprintf(stderr, "Cannot open %s for output\n", outfile);
820 exit(1);
821 }
822 }
823 }
824 read_post(&dvi_info);
825 make_page_index(&dvi_info, &dvi_dim);
826 translate(&dvi_info, &dvi_dim);
827 return 0;
828 }
829
830
831 void write_sp(FILE *fp, char *sp)
832 {
833 int len;
834 len = strlen(sp);
835
836 if(f_debug)
837 fprintf(fp_out, "%s", sp);
838 if(f_mode != EXE2MODIFY || f_last)
839 return; /* dry-run for EXE2CHECK */
840 if(len <= 0xff)
841 fprintf(fp, "%c%c%s", XXX1, len, sp);
842 else if(len <= 0xffffffff){
843 fprintf(fp, "%c", XXX1+3);
844 write_long(len, fp);
845 fprintf(fp, "%s", sp);
846 }else{
847 fprintf(stderr, "Too long special:\n%s\n", sp);
848 Exit(1);
849 }
850 }
851 void write_sp_nodebug(FILE *fp, char *sp)
852 { /* omit debug message (for backward compatibility) */
853 if(f_debug){
854 f_debug = 0; /* disable debug printing temporarily */
855 write_sp(fp, sp);
856 f_debug = 1;
857 }else
858 write_sp(fp, sp);
859 }
860
861
862 void translate(DVIFILE_INFO *dvi, DIMENSION *dim)
863 {
864 int page, page2, pos, count, size, former, current, flag;
865 FILE *fp;
866
867 if(f_mode == EXE2MODIFY){
868 fp = (*outfile)?fopen(outfile, WRITE_BINARY):fp_out;
869 if(fp == NULL){
870 fprintf(stderr, "Cannot open %s for output\n", outfile);
871 exit(4);
872 }else if(fp==stdout)
873 f_debug = 0; /* ignore -v option, as debug output goes there, sigh */
874 }else
875 fp = NULL;
876
877 f_needs_corr = flag = 0;
878 f_last = 0;
879
880 if(f_mode == EXE2TEXT || f_mode == EXE2SPECIAL){
881 while(out_pages && *out_pages){
882 if(*out_pages == 'T'){
883 if(f_mode == EXE2TEXT){
884 fseek(dvi->file_ptr, dvi->post, SEEK_SET);
885 transpre(dvi->file_ptr);
886 }
887 out_pages++;
888 }else if(*out_pages == 'L'){
889 if(f_mode == EXE2TEXT){
890 fseek(dvi->file_ptr, dvi->post, SEEK_SET);
891 transpost(dvi->file_ptr);
892 }
893 out_pages++;
894 }else if(*out_pages == '-'){
895 page = 1;
896 goto lastpage;
897 }else if(isdigit(*out_pages)){
898 page = atoi(out_pages);
899 while(isdigit(*++out_pages));
900 if(*out_pages == '-'){
901 lastpage: if(isdigit(*++out_pages)){
902 page2 = atoi(out_pages);
903 while(isdigit(*++out_pages));
904 }else
905 page2 = dim->total_page;
906 }else
907 page2 = page;
908 if(page < 1 || page > page2 || page2 > dim->total_page){
909 fprintf(stderr, "Page instruction %d-%d by -p is out of range (1-%d)\n",
910 page, page2, dim->total_page);
911 exit(1);
912 }
913 for( ; page <= page2; page++){
914 if(!(f_dtl&DTL_PAGE)){
915 if(f_mode == EXE2TEXT)
916 putc(' ', fp_out);
917 fprintf(fp_out, "[%d]\n", page);
918 }
919 fseek(dvi->file_ptr, dim->page_index[page], SEEK_SET);
920 interpret(dvi->file_ptr);
921 }
922 }else
923 out_pages++;
924 }
925 fclose(dvi->file_ptr);
926 fclose(fp_out);
927 dvi->file_ptr = fp_out = NULL;
928 return;
929 } /* done for if(f_mode == EXE2TEXT || f_mode == EXE2SPECIAL),
930 the rest of translate() is meant for
931 if((f_mode == EXE2MODIFY) and (f_mode == EXE2CHECK)) */
932
933 /* Prior scanning. This ensures page independence in reverse order too,
934 (e.g. Page 1 & 2 without background (= white) and Page 3 with background)
935 by checking whether non-stack specials (except tpic_pn) appears somewhere in DVI.
936 Specials with paired syntax (push/pop, bcolor/ecolor) are already safe
937 without pre-scanning, so these are skipped due to f_prescan = 1.
938 Other specials (background, pdf_bgcolor) are handled in this scanning. */
939 f_prescan = 1; /* change behavior of interpret(dvi) */
940 for(page = 1; page <= dim->total_page; page++){
941 fseek(dvi->file_ptr, dim->page_index[page], SEEK_SET);
942 interpret(dvi->file_ptr);
943 }
944 f_prescan = 0; /* restore interpret(dvi) */
945
946 former = current = -1;
947 if(fp){ /* f_mode == EXE2MODIFY and can be opened */
948 fseek(dvi->file_ptr, 0, SEEK_SET);
949 for(size = dim->page_index[1]; size > 0; size--)
950 write_byte(read_byte(dvi->file_ptr), fp); /* Write preamble */
951 /* [Process 1] Start writing the first page. */
952 current = ftell(fp); /* position of 1-st BOP */
953 for(size = 41; size > 0; size--) /* Write BOP and c[] */
954 write_byte(read_byte(dvi->file_ptr), fp);
955 write_long(former, fp); /* ptr to the former page = -1 */
956 }
957
958 for(page = 1; page <= dim->total_page; page++){ /* page loop start */
959 fseek(dvi->file_ptr, dim->page_index[page], SEEK_SET);
960 pos = interpret(dvi->file_ptr); /* scanned the whole page content; now
961 pos = position of EOP + 1 */
962 if(f_debug){ /* EXE2CHECK always falls into this */
963 fprintf(fp_out, "[%d]", page);
964 flag = f_needs_corr; /* reserved for later check */
965 }
966
967 /* [Process 2] Before writing the current page, handle non-stack specials.
968 * If not found but necessary, put one.
969 * If found, save current status before going back to the loop head
970 for the next page (= scanning the whole next page might overwrite it!).
971 Also, if the page is suffering from stack underflow,
972 open lacking stacks beforehand. */
973 while(color_under > 0){ /* recover underflow of color stack */
974 write_sp_nodebug(fp, "color push Black");
975 f_needs_corr++;
976 color_under--;
977 }
978 while(pdf_color_under > 0){ /* recover underflow of pdf:bcolor ... pdf:ecolor stack */
979 write_sp_nodebug(fp, "pdf:bcolor [0]");
980 f_needs_corr++;
981 pdf_color_under--;
982 }
983 if(background[0]){ /* background used somewhere */
984 if(!f_background){ /* no background in this page */
985 if(f_debug) fprintf(fp_out, "\n");
986 if(!background_prev[0]) /* assume white */
987 write_sp(fp, "background gray 1");
988 else
989 write_sp(fp, background_prev);
990 f_needs_corr++;
991 }else /* this page had one! */
992 strncpy(background_prev, background, MAX_LEN); /* save current */
993 }
994 if(pdf_bgcolor[0]){ /* pdf:bgcolor used somewhere */
995 if(!f_pdf_bgcolor){ /* no pdf:bgcolor in this page */
996 if(f_debug) fprintf(fp_out, "\n");
997 if(!pdf_bgcolor_prev[0]) /* assume white */
998 write_sp(fp, "pdf:bgcolor [1]");
999 else
1000 write_sp(fp, pdf_bgcolor_prev);
1001 f_needs_corr++;
1002 }else /* this page had one! */
1003 strncpy(pdf_bgcolor_prev, pdf_bgcolor, MAX_LEN); /* save current */
1004 }
1005 // while(pdf_annot_under > 0){ /* recover underflow of pdf:bann ... pdf:eann stack */
1006 // /* [TODO] what should we do here? */
1007 // f_needs_corr++;
1008 // pdf_annot_under--;
1009 // }
1010 if(f_pn < 0) { /* tpic_pn from the former page should be effective ... */
1011 if(!tpic_pn_prev[0]) /* ... but nothing found before */
1012 fprintf(stderr, "\nCannot find valid tpic pn."
1013 "\nPlease check your LaTeX source.");
1014 else{ /* ... OK */
1015 if(f_debug) fprintf(fp_out, "\n");
1016 write_sp(fp, tpic_pn_prev);
1017 f_needs_corr++;
1018 }
1019 }
1020 if(tpic_pn[0] && f_pn) /* tpic_pn used in this page */
1021 strncpy(tpic_pn_prev, tpic_pn, MAX_LEN); /* save current */
1022
1023 /* [Process 3] Write contents of the current page. */
1024 if(f_mode == EXE2MODIFY){
1025 fseek(dvi->file_ptr, dim->page_index[page]+45, SEEK_SET);
1026 for(size = pos - dim->page_index[page] - 46; size > 0; size--)
1027 write_byte(read_byte(dvi->file_ptr), fp);
1028 }
1029
1030 /* [Process 4] After writing the current page,
1031 close not-yet-closed stacks. */
1032 for(count = 0; count < color_depth; count++){
1033 write_sp_nodebug(fp, "color pop");
1034 f_needs_corr++;
1035 }
1036 for(count = 0; count < pdf_color_depth; count++){
1037 write_sp_nodebug(fp, "pdf:ecolor");
1038 f_needs_corr++;
1039 }
1040 // for(count = 0; count < pdf_annot_depth; count++){
1041 // write_sp_nodebug(fp, "pdf:eann");
1042 // f_needs_corr++;
1043 // }
1044 if(f_mode == EXE2MODIFY){
1045 write_byte((uchar)EOP, fp); /* write EOP */
1046 former = current;
1047 current = ftell(fp); /* get position of BOP/POST */
1048 }
1049
1050 if(page == dim->total_page)
1051 f_last = 1; /* reached the last page, change behavior of write_sp(fp,sp) */
1052
1053 /* [Process 5] Except for the last page,
1054 start the next page with passing not-yet-closed stacks. */
1055 if(f_mode == EXE2MODIFY && !f_last){
1056 fseek(dvi->file_ptr, dim->page_index[page+1], SEEK_SET);
1057 for(size = 41; size > 0; size--) /* write BOP and c[] */
1058 write_byte(read_byte(dvi->file_ptr), fp);
1059 write_long(former, fp); /* position of BOP of the former page */
1060 }
1061 for(count = 0; count < color_depth; count++){
1062 if(f_debug) fprintf(fp_out, "\n%d:", count+1);
1063 write_sp(fp, color_pt[count]);
1064 }
1065 for(count = 0; count < pdf_color_depth; count++){
1066 if(f_debug) fprintf(fp_out, "\n%d:", count+1);
1067 write_sp(fp, pdf_color_pt[count]);
1068 }
1069 // for(count = 0; count < pdf_annot_depth; count++){
1070 // if(f_debug) fprintf(fp_out, "\n%d:", count+1);
1071 // write_sp(fp, pdf_annot_pt[count]);
1072 // }
1073 /* f_needs_corr already set properly in [Process 4] */
1074
1075 if(f_debug){ /* EXE2CHECK always falls into this */
1076 if(flag != f_needs_corr) /* at least one special printed for debug */
1077 fprintf(fp_out, "\n");
1078 }
1079 f_last = 0; /* restore write_sp(fp,sp) */
1080 } /* page loop end */
1081
1082 if(f_debug) { /* EXE2CHECK always falls into this */
1083 if(color_depth_max)
1084 fprintf(fp_out, "\nMaximal depth of color stack:%d", color_depth_max);
1085 if(pdf_color_depth_max)
1086 fprintf(fp_out, "\nMaximal depth of pdf:bcolor ... pdf:ecolor stack:%d", pdf_color_depth_max);
1087 if(pdf_annot_depth_max)
1088 fprintf(fp_out, "\nMaximal depth of pdf:bann ... pdf:eann stack:%d", pdf_annot_depth_max);
1089 }
1090 if(f_mode == EXE2CHECK){
1091 fclose(dvi->file_ptr);
1092 fprintf(fp_out, f_needs_corr?
1093 "\nSome corrections are necessary!\n":
1094 "\nNo modification is necessary\n");
1095 fclose(fp_out);
1096 dvi->file_ptr = fp_out = NULL;
1097 return;
1098 } /* done for EXE2CHECK; remainings are for EXE2MODIFY */
1099
1100 /* if -z option is given, add empty pages to make multiple of 4 pages */
1101 if(f_book && dim->total_page%4 == 0)
1102 f_book = 0; /* modification unnecessary */
1103 if(f_book){
1104 total_book_page = dim->total_page + (4 - dim->total_page%4)%4;
1105 for(page = dim->total_page; page < total_book_page; page++){
1106 write_byte((uchar)BOP,fp);
1107 write_long(-1, fp);
1108 for (count = 1; count < 10; count++) /* set all sub counts to 0 */
1109 write_long(0, fp);
1110 write_long(former, fp);
1111 /* always white page */
1112 if(background[0]) /* background is used somewhere */
1113 write_sp(fp, "background gray 1");
1114 if(pdf_bgcolor[0]) /* pdf:bgcolor is used somewhere */
1115 write_sp(fp, "pdf:bgcolor [1]");
1116 write_byte((uchar)EOP,fp);
1117 former = current;
1118 current = ftell(fp); /* get position of BOP/POST */
1119 }
1120 }
1121
1122 write_byte((uchar)POST,fp); /* write POST */
1123 write_long(former, fp); /* position of the last BOP */
1124
1125 fseek(dvi->file_ptr, dvi->post + 5, SEEK_SET);
1126 if(f_book){
1127 write_long(read_long(dvi->file_ptr), fp); /* numerator */
1128 write_long(read_long(dvi->file_ptr), fp); /* denominator */
1129 write_long(read_long(dvi->file_ptr), fp); /* magnification */
1130 write_long(read_long(dvi->file_ptr), fp); /* tallest page height */
1131 write_long(read_long(dvi->file_ptr), fp); /* widest page width */
1132 write_word(read_word(dvi->file_ptr), fp); /* DVI stack size */
1133 read_word(dvi->file_ptr); /* skip original number of pages */
1134 write_word(total_book_page, fp); /* new number of pages */
1135 for(size = dvi->pt_post - dvi->post - 29; size-- > 0; )
1136 write_byte(read_byte(dvi->file_ptr), fp); /* write postamble upto post_post */
1137 }
1138 else{
1139 for(size = dvi->pt_post - dvi->post - 5; size-- > 0; )
1140 write_byte(read_byte(dvi->file_ptr), fp); /* write postamble upto post_post */
1141 }
1142 write_long(current, fp); /* position of POST */
1143 read_long(dvi->file_ptr); /* skip old position of POST */
1144 write_byte(read_byte(dvi->file_ptr), fp); /* write id = 2/3 */
1145
1146 for(size = 4; size > 0; size--)
1147 write_byte((uchar)END_DVI, fp); /* write END_DVI */
1148 size = ftell(fp);
1149 while(size++ & 3) /* fill END_DVI */
1150 write_byte((uchar)END_DVI, fp);
1151 fclose(fp);
1152 fclose(dvi->file_ptr);
1153 fp = dvi->file_ptr = NULL;
1154 if(!f_needs_corr && !f_book){
1155 unlink(outfile);
1156 fprintf(stderr, "\nNo correction is done.\n");
1157 return;
1158 }
1159 if(f_overwrite){
1160 if(f_backup){
1161 sprintf(tmp_buf, "%s.bak", infile);
1162 if(rename(infile, tmp_buf) == -1){
1163 fprintf(stderr, "Cannot backup %s to %s\n", infile, tmp_buf);
1164 Exit(1);
1165 }
1166 }else
1167 unlink(infile);
1168 if(rename(outfile, infile) == -1){
1169 fprintf(stderr, "Cannot overwrite %s\n", infile);
1170 Exit(1);
1171 }
1172 fprintf(stderr, "\nDVI file %s is corrected\n", infile);
1173 }else
1174 fprintf(stderr, "\nCreate a new DVI file %s\n", (fp_out==stdout)?"(stdout)":outfile);
1175 }
1176
1177
1178 void make_page_index(DVIFILE_INFO *dvi, DIMENSION *dim)
1179 {
1180 int i, tmp;
1181 Long offset;
1182
1183 dim->page_index =
1184 (PAGE_INDEX *)malloc(sizeof(PAGE_INDEX) * (
1185 (dim->total_page = dvi->total_page) + 2));
1186
1187 dim->max_nombre_page = 0;
1188 for (offset = dvi->last_bop, i = dvi->total_page; i > 0; i--) {
1189 fseek(dvi->file_ptr, offset, SEEK_SET);
1190 if ((uchar)read_byte(dvi->file_ptr) != BOP){
1191 fprintf(stderr, "No BOP command in page %d\n", i);
1192 Exit(1);
1193 }
1194 dim->page_index[i] = offset;
1195 if((tmp = read_long(dvi->file_ptr))
1196 > dim->max_nombre_page)
1197 dim->max_nombre_page = tmp;
1198
1199 /* Read count[0] */
1200 fseek(dvi->file_ptr, 36L, SEEK_CUR);
1201 /* Skip other 'count' values */
1202 offset = read_long(dvi->file_ptr);
1203 }
1204 }
1205
1206 uint interpret(FILE *dvi)
1207 {
1208 int i;
1209
1210 /*
1211 font = NULL;
1212
1213 H0 = output->h_0;
1214 V0 = output->v_0;
1215 */
1216 if ((uchar)read_byte(dvi) != BOP)
1217 error("No BOP");
1218 if(f_mode == EXE2TEXT){
1219 if(f_pos)
1220 fprintf(fp_out, "%ld: ", ftell(dvi)-1);
1221 fputs("bop", fp_out);
1222 fprintf(fp_out, " %d%s", read_long(dvi), MSG("/page"));
1223 for(i = 1; i < 10; i++)
1224 fprintf(fp_out, " %d", read_long(dvi));
1225 fprintf(fp_out, " %d%s\n", read_long(dvi), MSG("/former_bop"));
1226 }else
1227 fseek(dvi, 44L, SEEK_CUR);
1228 return (f_mode == EXE2TEXT)?work(dvi):s_work(dvi);
1229 }
1230
1231 /* set_char_0, 0 SET_CHAR_0 */
1232 static uchar cmd128_mode[] =
1233 {
1234 0x31, /* 128 SET1 */
1235 0x32, /* 129 */
1236 0x33, /* 130 */
1237 0x34, /* 131 */
1238 8, /* 132 SET_RULE */
1239 0x31, /* 133 PUT1 */
1240 0x32, /* 134 */
1241 0x33, /* 135 */
1242 0x34, /* 136 */
1243 8, /* 137 PUT_RULE */
1244 0, /* 138 NOP */
1245 0x10, /* 139 BOP */
1246 0x10, /* 140 */
1247 0, /* 141 PUSH */
1248 0, /* 142 POP */
1249 1, /* 143 RIGHT1 */
1250 2, /* 144 */
1251 3, /* 145 */
1252 4, /* 146 */
1253 0, /* 147 W0 */
1254 1, /* 148 W1 */
1255 2, /* 149 */
1256 3, /* 150 */
1257 4, /* 151 */
1258 0, /* 152 X0 */
1259 1, /* 153 X1 */
1260 2, /* 154 */
1261 3, /* 155 */
1262 4, /* 156 */
1263 1, /* 157 DOWN1 */
1264 2, /* 158 */
1265 3, /* 159 */
1266 4, /* 160 */
1267 0, /* 161 Y0 */
1268 1, /* 162 Y1 */
1269 2, /* 163 */
1270 3, /* 164 */
1271 4, /* 165 */
1272 0, /* 166 Z0 */
1273 1, /* 167 Z1 */
1274 2, /* 168 */
1275 3, /* 169 */
1276 4 /* 170 */
1277 };
1278
1279 /* fnt_num_0, 171 FNT_NUM_0 */
1280 static uchar cmd235_mode[] =
1281 {
1282 0x41, /* 235 FNT1 */
1283 0x42, /* 236 */
1284 0x43, /* 237 */
1285 0x44, /* 238 */
1286
1287 0x51, /* 239 XXX1 */
1288 0x52, /* 240 */
1289 0x53, /* 241 */
1290 0x54, /* 242 */
1291
1292 0x21, /* 243 FNT_DEF_1 */
1293 0x22, /* 244 */
1294 0x23, /* 245 */
1295 0x24, /* 246 */
1296
1297 0x10, /* 247 PRE */
1298 0x10, /* 248 */
1299 0x10, /* 249 */
1300 0x10, /* 250 */
1301 0x10, /* 251 */
1302 0x10, /* 252 */
1303 0x10, /* 253 */
1304 0x10, /* 254 */
1305 0x10 /* 255 EOFNC */
1306 };
1307
1308
1309 int strsubcmp(char *s, char *t)
1310 {
1311 while(*s == *t){
1312 if(!*t)
1313 return 0;
1314 s++;
1315 t++;
1316 }
1317 return(!*t && (uchar)(*s) <= ' ')?0:1;
1318 }
1319
1320 /* without space separator (e.g. pdf:bann<<...>>, instead of pdf:bann <<...>>) */
1321 int strsubcmp_n(char *s, char *t)
1322 {
1323 while(*s == *t){
1324 if(!*t)
1325 return 0;
1326 s++;
1327 t++;
1328 }
1329 return(!*t)?0:1;
1330 }
1331
1332 uint s_work(FILE *dvi)
1333 {
1334 int code, mode, tmp = 0;
1335 f_background = 0;
1336 f_pdf_bgcolor = 0;
1337 f_pn = 0;
1338
1339 while ((code = (uchar)read_byte(dvi)) != EOP){
1340 if(code >= 128){
1341 if (code < FNT_NUM_0){
1342 mode = cmd128_mode[code - 128];
1343 goto work;
1344 }else if (code >= FNT1){
1345 mode = cmd235_mode[code - FNT1];
1346 work: if (mode >= 0x30)
1347 tmp = read_n(dvi, mode & 0xf);
1348 switch (mode >> 4) {
1349 case (0):
1350 while(mode-- > 0)
1351 (void)read_byte(dvi);
1352 break;
1353
1354 case (1):
1355 if(!f_ptex || code != EOFNC){
1356 fprintf(stderr, "illegal code(%d)\n", code);
1357 Exit(1);
1358 }
1359 (void)read_byte(dvi);
1360 break;
1361
1362 case (2):
1363 fseek(dvi, (Long)((mode & 0xf) + 12), SEEK_CUR);
1364 tmp = (uchar)read_byte(dvi) + (uchar)read_byte(dvi);
1365 skip: while (tmp--)
1366 (void)read_byte(dvi);
1367 break;
1368 /*
1369 case (3):
1370 break;
1371
1372 case (4):
1373 break;
1374 */
1375 case (5):
1376 if(tmp < COMMON_SIZE-1){
1377 char *special;
1378
1379 special = tmp_buf;
1380 while (tmp-- > 0)
1381 *special++ = (uchar)getc(dvi);
1382 *special = '\0';
1383 if(f_mode == EXE2SPECIAL){
1384 fprintf(fp_out, "{%s}\n", tmp_buf);
1385 break;
1386 }
1387 special = tmp_buf;
1388 while(*special && ((uchar)*special) <= ' ')
1389 special++;
1390 if(!strsubcmp(special, "pn")){ /* pn: tpic pen size */
1391 if(!f_prescan) strncpy(tpic_pn, special, MAX_LEN);
1392 if(!f_pn)
1393 f_pn = 1;
1394 }else if(!f_pn &&
1395 (!strsubcmp(special, "pa") || /* pa: tpic pen at */
1396 !strsubcmp(special, "ar")) ) /* ar: draw circle */
1397 f_pn = -1;
1398 else if(!strsubcmp(special, "color")) /* color push/pop */
1399 sp_color(special);
1400 else if(!strsubcmp(special, "pdf:bcolor")) /* pdf:bcolor */
1401 sp_pdf_bcolor(special);
1402 else if(!strsubcmp(special, "pdf:ecolor")) /* pdf:ecolor */
1403 sp_pdf_ecolor(special);
1404 else if(!strsubcmp(special, "background")){ /* background */
1405 strncpy(background, special, MAX_LEN);
1406 f_background = 1;
1407 }
1408 else if(!strsubcmp(special, "pdf:bgcolor")){ /* pdf:bgcolor */
1409 strncpy(pdf_bgcolor, special, MAX_LEN);
1410 f_pdf_bgcolor = 1;
1411 }
1412 // else if(!strsubcmp_n(special, "pdf:bann")) /* pdf:bann */
1413 // sp_pdf_bann(special);
1414 // else if(!strsubcmp(special, "pdf:eann")) /* pdf:eann */
1415 // sp_pdf_eann(special);
1416 break;
1417 }
1418 goto skip;
1419 }
1420 }
1421 }
1422 }
1423 return ftell(dvi);
1424 }
1425
1426 /* color specials */
1427 void sp_color(char *sp)
1428 {
1429 char *s;
1430 if(f_prescan) return;
1431
1432 if(strstr(sp, "pop")){
1433 if(--color_depth < 0){
1434 fprintf(stderr, "color stack underflow\n");
1435 color_under++;
1436 f_needs_corr++;
1437 color_depth = 0;
1438 }
1439 return;
1440 }
1441 if(strstr(sp, "push")){
1442 if(color_depth >= MAX_COLOR){
1443 fprintf(stderr, "Too many color push > %d\n", MAX_COLOR);
1444 Exit(1);
1445 }
1446 if(color_depth){
1447 s = color_pt[color_depth-1];
1448 s += strlen(s) + 1;
1449 }
1450 else
1451 s = color_buf;
1452 if(s - color_buf + strlen(sp) >= COLOR_BUF_SIZE - 2)
1453 error("Too much color definitions");
1454 else{
1455 strcpy(s, sp);
1456 color_pt[color_depth++] = s;
1457 }
1458 if(color_depth > color_depth_max)
1459 color_depth_max = color_depth;
1460 }
1461 }
1462
1463 /* pdf:bcolor special */
1464 void sp_pdf_bcolor(char *sp)
1465 {
1466 char *s;
1467 if(f_prescan) return;
1468
1469 /* copied from "color push" routine of sp_color */
1470 if(pdf_color_depth >= MAX_COLOR){
1471 fprintf(stderr, "Too many pdf:bcolor > %d\n", MAX_COLOR);
1472 Exit(1);
1473 }
1474 if(pdf_color_depth){
1475 s = pdf_color_pt[pdf_color_depth-1];
1476 s += strlen(s) + 1;
1477 }
1478 else
1479 s = color_buf;
1480 if(s - color_buf + strlen(sp) >= COLOR_BUF_SIZE - 2)
1481 error("Too much color definitions");
1482 else{
1483 strcpy(s, sp);
1484 pdf_color_pt[pdf_color_depth++] = s;
1485 }
1486 if(pdf_color_depth > pdf_color_depth_max)
1487 pdf_color_depth_max = pdf_color_depth;
1488 }
1489
1490 /* pdf:ecolor special */
1491 void sp_pdf_ecolor(char *sp)
1492 {
1493 char *s;
1494 if(f_prescan) return;
1495
1496 /* copied from "color pop" routine of sp_color */
1497 if(--pdf_color_depth < 0){
1498 fprintf(stderr, "pdf:bcolor ... pdf:ecolor stack underflow\n");
1499 pdf_color_under++;
1500 f_needs_corr++;
1501 pdf_color_depth = 0;
1502 }
1503 return;
1504 }
1505
1506 /* pdf:bann special */
1507 void sp_pdf_bann(char *sp)
1508 {
1509 char *s;
1510 if(f_prescan) return;
1511 if(pdf_annot_depth >= MAX_ANNOT){
1512 fprintf(stderr, "Too many pdf:bann > %d\n", MAX_ANNOT);
1513 Exit(1);
1514 }
1515 if(pdf_annot_depth){
1516 s = pdf_annot_pt[pdf_annot_depth-1];
1517 s += strlen(s) + 1;
1518 }
1519 else
1520 s = annot_buf;
1521 if(s - annot_buf + strlen(sp) >= ANNOT_BUF_SIZE - 2)
1522 error("Too much annot definitions");
1523 else{
1524 strcpy(s, sp);
1525 pdf_annot_pt[pdf_annot_depth++] = s;
1526 }
1527 if(pdf_annot_depth > pdf_annot_depth_max)
1528 pdf_annot_depth_max = pdf_annot_depth;
1529 }
1530
1531 /* pdf:eann special */
1532 void sp_pdf_eann(char *sp)
1533 {
1534 char *s;
1535 if(f_prescan) return;
1536 if(--pdf_annot_depth < 0){
1537 fprintf(stderr, "pdf:bann ... pdf:eann stack underflow\n");
1538 pdf_annot_under++;
1539 // f_needs_corr++;
1540 pdf_annot_depth = 0;
1541 }
1542 return;
1543 }
1544
1545 int num_add;
1546 int top_add;
1547
1548 void read_post(DVIFILE_INFO *dvi)
1549 /* read POSTAMBLE */
1550 {
1551 int code, endofs, top_add;
1552
1553 if ( read_byte(dvi->file_ptr) != PRE ||
1554 ((code = read_byte(dvi->file_ptr)) != ID && code != IDP) ){
1555 err: fprintf(stderr, "%s is not correct DVI file\n", dvi->file_name);
1556 Exit(254);
1557 }
1558 for (endofs = -3L; fseek(dvi->file_ptr, endofs, SEEK_END),
1559 (code = read_byte(dvi->file_ptr)) != ID && code != IDP; endofs--){
1560 /* Search id number */
1561 if (code == EOF || code != END_DVI)
1562 goto err;
1563 }
1564 f_ptex = (code==IDP)?1:0;
1565 fseek(dvi->file_ptr, endofs - 4L, SEEK_END);
1566 dvi->pt_post = ftell(dvi->file_ptr); /* get POST_POST */
1567 if ((top_add = dvi->post = read_long(dvi->file_ptr)) <= 0) /* get POST */
1568 goto err;
1569 /* Read the position of POSTAMBLE */
1570
1571 fseek(dvi->file_ptr, dvi->post - 4, SEEK_SET);
1572 /* Set file-ptr at POSTAMBLE */
1573
1574 if(AdID == read_long(dvi->file_ptr))
1575 num_add = 1;
1576 if((code = read_byte(dvi->file_ptr)) != POST) /* at POST */
1577 goto err;
1578 if((dvi->last_bop = read_long(dvi->file_ptr)) <= 0) /* get LAST_BOP */
1579 goto err;
1580 #if 0
1581 /* for extended DVI file of dviout */
1582 fseek(dvi->file_ptr, dvi->post - 20, SEEK_SET);
1583 if(num_add){
1584 int i, j, size;
1585 char *s;
1586
1587 bofn = read_long(dvi->file_ptr);
1588 eofn = read_long(dvi->file_ptr);
1589 num_add = last_add = read_long(dvi->file_ptr);
1590 top_add = read_long(dvi->file_ptr);
1591 fseek(dvi->file_ptr, dvi->post - 20 - 8*num_add, SEEK_SET);
1592 for(i = 0; i < num_add; i++){
1593 pt_bod[i] = read_long(dvi->file_ptr);
1594 lod[i] = read_long(dvi->file_ptr);
1595 }
1596 fseek(dvi->file_ptr, bofn, SEEK_SET);
1597 size = eofn - bofn;
1598 s = malloc(size);
1599 for(i = 0; i < size; i++)
1600 s[i] = read_byte(dvi->file_ptr);
1601 pt_name[0] = s;
1602 for(i = j = 0; i < size; ){
1603 if(!s[i++])
1604 pt_name[++j] = s + i;
1605 }
1606 }
1607 #endif
1608 fseek(dvi->file_ptr, dvi->post, SEEK_SET);
1609
1610 /* Set file-ptr at POSTAMBLE */
1611 if ((uchar)read_byte(dvi->file_ptr) != POST)
1612 error("No Postamble");
1613
1614 if ((dvi->last_bop = read_long(dvi->file_ptr)) <= 0)
1615 error("Negative Pointer(Last BOP)");
1616
1617 dvi->num = read_long(dvi->file_ptr);
1618 dvi->den = read_long(dvi->file_ptr);
1619 dvi->mag = read_long(dvi->file_ptr);
1620 dvi->l = read_long(dvi->file_ptr);
1621 dvi->u = read_long(dvi->file_ptr);
1622 dvi->stack_depth = read_short(dvi->file_ptr);
1623 dvi->total_page = read_short(dvi->file_ptr);
1624
1625 if (dvi->num <= 0 || dvi->den <= 0 || dvi->mag <= 0)
1626 error("Reading Illegal Long");
1627 if (dvi->stack_depth < 0 || dvi->total_page <= 0)
1628 error("Reading Illegal Integer");
1629 }
1630
1631 /************** dvi -> text ******************/
1632
1633 static char *SETCHAR = "setchar";
1634
1635 static char *cmd128_name[] =
1636 {
1637 "set1", /* 128 SET1 */
1638 "set2", /* 129 */
1639 "set3", /* 130 */
1640 "set4", /* 131 */
1641 "setrule", /* 132 SET_RULE */
1642 "put1", /* 133 PUT1 */
1643 "put2", /* 134 */
1644 "put3", /* 135 */
1645 "put4", /* 136 */
1646 "putrule", /* 137 PUT_RULE */
1647 "nop", /* 138 NOP */
1648 "bop", /* 139 BOP */
1649 "eop", /* 140 EOP */
1650 "push", /* 141 PUSH */
1651 "pop", /* 142 POP */
1652 "right1", /* 143 RIGHT1 */
1653 "right2", /* 144 */
1654 "right3", /* 145 */
1655 "right4", /* 146 */
1656 "w0", /* 147 W0 */
1657 "w1", /* 148 W1 */
1658 "w2", /* 149 */
1659 "w3", /* 150 */
1660 "w4", /* 151 */
1661 "x0", /* 152 X0 */
1662 "x1", /* 153 X1 */
1663 "x2", /* 154 */
1664 "x3", /* 155 */
1665 "x4", /* 156 */
1666 "down1", /* 157 DOWN1 */
1667 "down2", /* 158 */
1668 "down3", /* 159 */
1669 "down4", /* 160 */
1670 "y0", /* 161 Y0 */
1671 "y1", /* 162 Y1 */
1672 "y2", /* 163 */
1673 "y3", /* 164 */
1674 "y4", /* 165 */
1675 "z0", /* 166 Z0 */
1676 "z1", /* 167 Z1 */
1677 "z2", /* 168 */
1678 "z3", /* 169 */
1679 "z4" /* 170 */
1680 };
1681
1682 static char *cmd235_name[] =
1683 {
1684 "fnt1", /* 235 FNT1 */
1685 "fnt2", /* 236 */
1686 "fnt3", /* 237 */
1687 "fnt4", /* 238 */
1688
1689 "xxx1", /* 239 XXX1 */
1690 "xxx2", /* 240 */
1691 "xxx3", /* 241 */
1692 "xxx4", /* 242 */
1693
1694 "fntdef1", /* 243 FNT_DEF_1 */
1695 "fntdef2", /* 244 */
1696 "fntdef3", /* 245 */
1697 "fntdef4", /* 246 */
1698
1699 "pre", /* 247 PRE */
1700 "illegal", /* 248 */
1701 "illegal", /* 249 */
1702 "illegal", /* 250 */
1703 "illegal", /* 251 */
1704 "illegal", /* 252 */
1705 "illegal", /* 253 */
1706 "illegal", /* 254 */
1707 "dir" /* 255 EOFNC */
1708 };
1709
1710 static char *c128_name[] =
1711 {
1712 "s1", /* 128 SET1 */
1713 "s2", /* 129 */
1714 "s3", /* 130 */
1715 "s4", /* 131 */
1716 "sr", /* 132 SET_RULE */
1717 "p1", /* 133 PUT1 */
1718 "p2", /* 134 */
1719 "p3", /* 135 */
1720 "p4", /* 136 */
1721 "pr", /* 137 PUT_RULE */
1722 "nop", /* 138 NOP */
1723 "bop", /* 139 BOP */
1724 "eop", /* 140 EOP */
1725 "[", /* 141 PUSH */
1726 "]", /* 142 POP */
1727 "r1", /* 143 RIGHT1 */
1728 "r2", /* 144 */
1729 "r3", /* 145 */
1730 "r4", /* 146 */
1731 "w0", /* 147 W0 */
1732 "w1", /* 148 W1 */
1733 "w2", /* 149 */
1734 "w3", /* 150 */
1735 "w4", /* 151 */
1736 "x0", /* 152 X0 */
1737 "x1", /* 153 X1 */
1738 "x2", /* 154 */
1739 "x3", /* 155 */
1740 "x4", /* 156 */
1741 "d1", /* 157 DOWN1 */
1742 "d2", /* 158 */
1743 "d3", /* 159 */
1744 "d4", /* 160 */
1745 "y0", /* 161 Y0 */
1746 "y1", /* 162 Y1 */
1747 "y2", /* 163 */
1748 "y3", /* 164 */
1749 "y4", /* 165 */
1750 "z0", /* 166 Z0 */
1751 "z1", /* 167 Z1 */
1752 "z2", /* 168 */
1753 "z3", /* 169 */
1754 "z4" /* 170 */
1755 };
1756
1757 static char *c235_name[] =
1758 {
1759 "f1", /* 235 FNT1 */
1760 "f2", /* 236 */
1761 "f3", /* 237 */
1762 "f4", /* 238 */
1763
1764 "special1", /* 239 XXX1 */
1765 "special2", /* 240 */
1766 "special3", /* 241 */
1767 "special4", /* 242 */
1768
1769 "fd1", /* 243 FNT_DEF_1 */
1770 "fd2", /* 244 */
1771 "fd3", /* 245 */
1772 "fd4", /* 246 */
1773
1774 "pre", /* 247 PRE */
1775 "opcode", /* 248 */
1776 "opcode", /* 249 */
1777 "opcode", /* 250 */
1778 "opcode", /* 251 */
1779 "opcode", /* 252 */
1780 "opcode", /* 253 */
1781 "opcode", /* 254 */
1782 "dir" /* 255 EOFNC */
1783 };
1784
1785
1786 #define MAX_STR 0x60
1787
1788 char str_buf[MAX_STR];
1789 int len = 0;
1790
1791 void flush_str(void)
1792 {
1793 if(!len)
1794 return;
1795 str_buf[len] = 0;
1796 if(f_dtl&DTL_CHAR)
1797 fprintf(fp_out, "(%s)\n", str_buf);
1798 else
1799 fprintf(fp_out, " \"%s\"\n", str_buf);
1800 len = 0;
1801 }
1802
1803 #ifndef PTEXENC
1804 #ifndef UNIX
1805 void jis2sjis(int *h, int *l)
1806 {
1807 if (*h & 1) {
1808 if (*l < 0x60) *l += 0x1f;
1809 else *l += 0x20;
1810 }else *l += 0x7e;
1811 if (*h < 0x5f) *h = (*h + 0xe1) >> 1;
1812 else *h = (*h + 0x161) >> 1;
1813 }
1814 #endif
1815 #endif
1816
1817 void out_string(FILE *in, FILE *out, int len)
1818 {
1819 int ch;
1820
1821 while(len-- > 0){
1822 ch = getc(in);
1823 if(f_dtl & DTL_STRING){
1824 if(ch < ' ' || ch > 0x7e){
1825 fprintf(out, "\\%02X", ch & 0xff);
1826 continue;
1827 }
1828 if(ch == '\'' || ch == '\\')
1829 putc('\\', out);
1830 }
1831 putc(ch, out);
1832 }
1833 }
1834
1835
1836 void fontdef(FILE *dvi, int nn)
1837 {
1838 int code, tmp = 0;
1839 uint csum;
1840
1841 fprintf(fp_out, " %d", read_n(dvi, nn)); /* code */
1842 csum = read_n(dvi, 4);
1843 if(csum)
1844 fprintf(fp_out,
1845 (f_dtl&DTL_FNTDEF)?((f_dtl&DTL_OCT)?" %o%s":" 0%o%s"):
1846 " 0x%X%s", csum, MSG("/c-sum")); /* chksum */
1847 else
1848 fprintf(fp_out, " 0%s", MSG("/c-sum"));
1849 fprintf(fp_out, " %u%s", read_long(dvi), MSG("/s-size")); /* scaled size */
1850 fprintf(fp_out, " %u%s", read_long(dvi), MSG("/d-size")); /* design size */
1851 tmp = (uchar)read_byte(dvi);
1852 fprintf(fp_out, " %d%s", tmp, MSG("/dir")); /* len:directry */
1853 code = (uchar)read_byte(dvi);
1854 fprintf(fp_out, " %d%s '", code, MSG("/name")); /* len:name */
1855 while (tmp-- > 0)
1856 putc(read_byte(dvi), fp_out);
1857 if((f_dtl&DTL_FNTNAME))
1858 fputs("' '", fp_out);
1859 while(code-- > 0)
1860 putc(read_byte(dvi), fp_out);
1861 fputs("'\n", fp_out);
1862 }
1863
1864
1865 uchar skipnop(FILE *dvi)
1866 {
1867 uchar code;
1868
1869 while (code = (uchar)getc(dvi)) {
1870 if (code >= FNT_DEF_1 && code <= FNT_DEF_4) {
1871 fprintf(fp_out, "%s", (f_dtl&DTL_CMD)?c235_name[code-FNT1]:cmd235_name[code-FNT1]);
1872 fontdef(dvi, code-FNT_DEF_1+1);
1873 continue;
1874 }
1875 else if (code == NOP) {
1876 fprintf(fp_out, "%s\n", (f_dtl&DTL_CMD)?c128_name[code-128]:cmd128_name[code-128]);
1877 continue;
1878 }
1879 break;
1880 }
1881 return code;
1882 }
1883
1884
1885 /* preamble */
1886 void transpre(FILE *dvi)
1887 {
1888 int len;
1889 fseek(dvi, 1, SEEK_SET);
1890 if(f_dtl & DTL_FORM)
1891 fprintf(fp_out, "variety sequences-6\n");
1892 if(f_pos)
1893 fputs("0: ", fp_out);
1894 fprintf(fp_out, "pre %u%s ", getc(dvi), MSG("/id")); /* id */
1895 fprintf(fp_out, "%u%s ", read_long(dvi), MSG("/num")); /* num */
1896 fprintf(fp_out, "%u%s ", read_long(dvi), MSG("/den")); /* den */
1897 fprintf(fp_out, "%u%s ", read_long(dvi), MSG("/mag")); /* mag */
1898 fprintf(fp_out, "%u%s ", len = getc(dvi),MSG("/len")); /* len */
1899 putc('\'', fp_out);
1900 out_string(dvi, fp_out, len);
1901 fputs("\'\n", fp_out);
1902 skipnop(dvi);
1903 }
1904
1905
1906 /* postamble */
1907 void transpost(FILE *dvi)
1908 {
1909 int code;
1910
1911 if(f_pos)
1912 fprintf(fp_out, "%ld: ", ftell(dvi));
1913 getc(dvi);
1914 fprintf(fp_out, "post %u%s ", read_long(dvi), MSG("/final_bop")); /* pointer to the last bop*/
1915 fprintf(fp_out, "%u%s ", read_long(dvi), MSG("/num")); /* num */
1916 fprintf(fp_out, "%u%s ", read_long(dvi), MSG("/den")); /* den */
1917 fprintf(fp_out, "%u%s ", read_long(dvi), MSG("/mag")); /* mag */
1918 fprintf(fp_out, "%u%s ", read_long(dvi), MSG("/h+d")); /* h+d */
1919 fprintf(fp_out, "%u%s ", read_long(dvi), MSG("/w")); /* w */
1920 fprintf(fp_out, "%u%s ", read_n(dvi, 2), MSG("/stack")); /* stack */
1921 fprintf(fp_out, "%u%s\n", read_n(dvi, 2), MSG("/pages")); /* pages */
1922 work(dvi); /* definiton of fonts */
1923 fprintf(fp_out, " %u%s", read_long(dvi), MSG("/post")); /* pointer to post */
1924 fprintf(fp_out, " %u%s", getc(dvi), MSG("/id")); /* id */
1925 while( (code = getc(dvi)) != EOF)
1926 fprintf(fp_out, " %u", code);
1927 putc('\n', fp_out);
1928 }
1929
1930
1931 /* content of a page */
1932 uint work(FILE *dvi)
1933 {
1934 int code, mode, tmp = 0;
1935 long pos = 0;
1936 uint csum;
1937 #ifdef PTEXENC
1938 int imb;
1939 long wch, wcp;
1940 char mbstr[9];
1941 #else
1942 int h_code, l_code;
1943 #endif
1944
1945 while( (code = (uchar)read_byte(dvi)) != EOP && code != POST_POST){
1946 if(f_pos)
1947 pos = ftell(dvi)-1;
1948 if(code < 128){
1949 if(f_pos && (!len || !(f_dtl&DTL_CHAR)))
1950 fprintf(fp_out, "%ld: ", pos);
1951 if(code >= 0x20 && code < 0x7f){
1952 if(len > MAX_STR-2){
1953 flush_str();
1954 len = 0;
1955 if(f_pos)
1956 fprintf(fp_out, "%ld: ", pos);
1957 }
1958 if((f_dtl & DTL_CHAR) &&
1959 (code == '(' || code == ')' || code == '\\' || code == '"'))
1960 str_buf[len++] = '\\';
1961 str_buf[len++] = (uchar)code;
1962 }else{
1963 if(len && f_pos)
1964 fprintf(fp_out, "%ld: ", pos);
1965 len = 0;
1966 flush_str();
1967 if(f_dtl & DTL_CHAR)
1968 fprintf(fp_out, "\\%02X\n", code);
1969 }
1970 if(!(f_dtl & DTL_CHAR))
1971 fprintf(fp_out, "%s%d\n", SETCHAR, code);
1972 }else{
1973 if(len)
1974 flush_str();
1975 if(f_pos)
1976 fprintf(fp_out, "%ld: ", pos);
1977 if (code < FNT_NUM_0){
1978 mode = cmd128_mode[code - 128];
1979 fprintf(fp_out, "%s", (f_dtl&DTL_CMD)?
1980 c128_name[code-128]:cmd128_name[code-128]);
1981 if(!(mode & 0xf)){
1982 putc('\n', fp_out);
1983 continue;
1984 }
1985 if((mode & 0xf) > 4){
1986 fprintf(fp_out, " %d", read_long(dvi));
1987 mode -= 4;
1988 }
1989 if(mode > 0x30){
1990 code = read_n(dvi, mode & 0xf);
1991 #ifdef PTEXENC
1992 if(f_jstr){
1993 // internal-euc/sjis: fromDVI cannot convert ASCII range
1994 if (is_internalUPTEX() || (isjis(code>>8) && isjis(code&0xff))) {
1995 wch = fromDVI(code);
1996 imb = 0; memset(mbstr, '\0', 9);
1997 if (is_internalUPTEX()) {
1998 int j, len;
1999 len = UVS_get_codepoint_length(wch);
2000 for (j=1; j<=len; j++) {
2001 wcp = UVS_get_codepoint_in_sequence(wch,j);
2002 if (wcp>0) {
2003 wcp = UCStoUTF8(wcp);
2004 if (BYTE1(wcp) != 0) mbstr[imb++]=BYTE1(wcp);
2005 if (BYTE2(wcp) != 0) mbstr[imb++]=BYTE2(wcp);
2006 if (BYTE3(wcp) != 0) mbstr[imb++]=BYTE3(wcp);
2007 /* always */ mbstr[imb++]=BYTE4(wcp);
2008 }
2009 }
2010 } else {
2011 if (BYTE1(wch) != 0) mbstr[imb++]=BYTE1(wch);
2012 if (BYTE2(wch) != 0) mbstr[imb++]=BYTE2(wch);
2013 if (BYTE3(wch) != 0) mbstr[imb++]=BYTE3(wch);
2014 /* always */ mbstr[imb++]=BYTE4(wch);
2015 }
2016 fprintf(fp_out,
2017 (f_dtl&DTL_CHAR2)?" %u \"":" 0x%x \"", code);
2018 fputs2(mbstr, fp_out);
2019 fprintf(fp_out, "\"\n");
2020 continue;
2021 }
2022 }
2023 #else
2024 if(f_sjis){
2025 l_code = code & 0xff;
2026 h_code = code >> 8;
2027 if(isjis(h_code) && isjis(l_code)){
2028 #ifdef UNIX
2029 h_code |= 0x80;
2030 l_code |= 0x80;
2031 #else
2032 jis2sjis(&h_code, &l_code);
2033 #endif
2034 fprintf(fp_out,
2035 (f_dtl&DTL_CHAR2)?" %u \"%c%c\"\n":" 0x%x \"%c%c\"\n",
2036 code, h_code, l_code);
2037 continue;
2038 }
2039 }
2040 #endif
2041 fprintf(fp_out, (f_dtl&DTL_CHAR2)?" %u\n":" 0x%x\n", code);
2042 }else
2043 fprintf(fp_out, " %d\n", signed_read_n(dvi, mode & 0xf));
2044 continue;
2045 }else if (code< FNT1){
2046 fprintf(fp_out, (f_dtl&DTL_CMD)?"fn%d\n":"fntnum%d\n", code - FNT_NUM_0);
2047 continue;
2048 }else{
2049 mode = cmd235_mode[code - FNT1];
2050 fprintf(fp_out, "%s", (f_dtl&DTL_CMD)?c235_name[code-FNT1]:cmd235_name[code-FNT1]);
2051 if (mode >= 0x30)
2052 tmp = (long)read_n(dvi, mode & 0xf);
2053 switch (mode >> 4) {
2054 /*
2055 case (0):
2056 skip_m: while(mode-- > 0)
2057 (void)read_byte(dvi);
2058 break;
2059 */
2060 case (1):
2061 if(!f_ptex || code != EOFNC){
2062 fprintf(stderr, "illegal code(%d)", code);
2063 if((f_dtl&DTL_ILL))
2064 fprintf(stderr, " %d\n", code);
2065 else
2066 Exit(1);
2067 }else
2068 fprintf(fp_out, " %d\n", read_byte(dvi));
2069 continue;
2070
2071 case (2): /* fntdef */
2072 fontdef(dvi, mode & 0xf);
2073 continue;
2074 /*
2075 case (3):
2076 break;
2077 */
2078
2079 case (4): /* fntnum */
2080 fprintf(fp_out, " %u\n", tmp);
2081 continue;
2082
2083 case (5): /* specials */
2084 fprintf(fp_out, " %d%s '", tmp, MSG("/len"));
2085 out_string(dvi, fp_out, tmp);
2086 putc('\'', fp_out);
2087 putc('\n', fp_out);
2088 continue;
2089 }
2090 while(tmp-- > 0)
2091 getc(dvi);
2092 }
2093 }
2094 }
2095 if(f_pos)
2096 fprintf(fp_out, "%ld: ", ftell(dvi)-1);
2097 fputs((code==EOP)?"eop\n":"post_post", fp_out);
2098 if (code==EOP) skipnop(dvi);
2099 return ftell(dvi);
2100 }
2101
2102
2103 /************************* text -> dvi *************************************/
2104 struct KEY_LIST {
2105 char *name;
2106 short int code;
2107 short int len;
2108 };
2109
2110 struct KEY_LIST key[] = {
2111 {"bop", BOP, 1},
2112 {"d", DOWN1, 4},
2113 {"dir", EOFNC, 1},
2114 {"down", DOWN1, 4},
2115 {"eop", EOP, 1},
2116 {"f", FNT1, 4},
2117 {"fd", FNT_DEF_1, 4},
2118 {"fn", FNT_NUM_0, 64},
2119 {"fnt", FNT1, 4},
2120 {"fntdef", FNT_DEF_1, 4},
2121 {"fntnum", FNT_NUM_0, 64},
2122 {"nop", NOP, 1},
2123 {"opcode", OPCODE, 1},
2124 {"p", PUT1, 4},
2125 {"pop", POP, 1},
2126 {"post", POST, 1},
2127 {"post_post",POST_POST, 1},
2128 {"pr", PUT_RULE, 1},
2129 {"pre", PRE, 1},
2130 {"push", PUSH, 1},
2131 {"put", PUT1, 4},
2132 {"putrule", PUT_RULE, 1},
2133 {"r", RIGHT1, 4},
2134 {"right", RIGHT1, 4},
2135 {"s", SET1, 4},
2136 {"set", SET1, 4},
2137 {"setchar", SET_CHAR_0,128},
2138 {"setrule", SET_RULE, 1},
2139 {"special", XXX1, 4},
2140 {"sr", SET_RULE, 1},
2141 {"w", W0, 5},
2142 {"x", X0, 5},
2143 {"xxx", XXX1, 4},
2144 {"y", Y0, 5},
2145 {"z", Z0, 5}
2146 };
2147
2148 void key_swap(int i, int j)
2149 {
2150 char tmp[sizeof(struct KEY_LIST)];
2151
2152 memcpy(tmp, &key[i], sizeof(struct KEY_LIST));
2153 memcpy(&key[i], &key[j], sizeof(struct KEY_LIST));
2154 memcpy(&key[j], tmp, sizeof(struct KEY_LIST));
2155 }
2156
2157 void sort_key(void)
2158 {
2159 int i, j;
2160
2161 for(i = 1; i < sizeof(key)/sizeof(struct KEY_LIST); i++){
2162 if(strcmp(key[i-1].name, key[i].name) > 0){
2163 j = i;
2164 do{
2165 key_swap(j-1, j);
2166 j--;
2167 }while(j >= 1 && strcmp(key[j-1].name, key[j].name) > 0);
2168 }
2169 }
2170 }
2171
2172 void replace(char *o_name, char *n_name)
2173 {
2174 int i, j, count;
2175 char *s, **cmd;
2176
2177 if(!strcmp(o_name, SETCHAR)){
2178 SETCHAR = n_name;
2179 count = 1;
2180 }else{
2181 count = 0;
2182 cmd = cmd128_name;
2183 i = sizeof(cmd128_name)/sizeof(char *);
2184 again: while(--i >= 0){
2185 for(j = 0; cmd[i][j] == o_name[j] && o_name[j]; j++);
2186 if(!o_name[j]){
2187 if(!cmd[i][j] || (cmd[i][j] >= '0' && cmd[i][j] <= '9')){
2188 s = malloc(strlen(cmd[i]) + j + 1);
2189 strcpy(s, n_name);
2190 strcat(s, cmd[i]+j);
2191 cmd[i] = s;
2192 count++;
2193 }
2194 }
2195 }
2196 if(cmd == cmd128_name){
2197 cmd = cmd235_name;
2198 i = sizeof(cmd235_name)/sizeof(char *) - 1;
2199 goto again;
2200 }
2201 }
2202 if(!count){
2203 fprintf(stderr, "%s is not a keyword\n", o_name);
2204 exit(1);
2205 }
2206 for(i = sizeof(key)/sizeof(struct KEY_LIST)-1; i >= 0 && strcmp(o_name, key[i].name); i--);
2207 if(i >= 0){
2208 key[i].name = malloc(strlen(n_name)+1);
2209 strcpy(key[i].name, n_name);
2210 sort_key();
2211 }
2212 }
2213
2214 int sub_number;
2215
2216 int find_key(char *s0)
2217 {
2218 int top, end, pt, num;
2219 char *s, *t;
2220
2221 top = 0;
2222 end = sizeof(key)/sizeof(struct KEY_LIST)-1;
2223
2224 while(top <= end){
2225 s = s0;
2226 t = key[pt = (top+end)/2].name;
2227 while(*s == *t && *t){
2228 s++;
2229 t++;
2230 }
2231 if(!*t){
2232 if((num = key[pt].len) == 1){
2233 if(*s <= ' '){
2234 sub_number = 0;
2235 return pt;
2236 }
2237 }else if(*s >= '0' && *s <= '9'){
2238 sub_number = atoi(s);
2239 if(num == 4){
2240 if(sub_number > 0 && sub_number <= 4)
2241 return pt;
2242 }else{
2243 if(sub_number < num)
2244 return pt;
2245 }
2246 }
2247 }
2248 if(*s > *t)
2249 top = pt+1;
2250 else
2251 end = pt-1;
2252 }
2253 return -1;
2254 }
2255
2256 void write_n(int num, int byte)
2257 {
2258 switch(byte){
2259 case 4:
2260 putc((num>>24) & 0xff, fp_out);
2261 case 3:
2262 putc((num>>16)& 0xff, fp_out);
2263 case 2:
2264 putc((num>>8) & 0xff, fp_out);
2265 case 1:
2266 putc(num & 0xff, fp_out);
2267 }
2268 }
2269
2270 uchar *get_next(unsigned char *pt)
2271 {
2272 while(*pt > ' ')
2273 pt++;
2274 while(*pt <= ' ' && *pt)
2275 pt++;
2276 return pt;
2277 }
2278
2279
2280 uint a2i(unsigned char *s)
2281 {
2282 uint num;
2283 /* strtol(s, &pt, 0) will overflow for unsigned */
2284
2285 if(!*s)
2286 return 0;
2287 if(*s != '0'){
2288 if(*s == '-' && isdigit(s[1]))
2289 return((uint)0xffffffff - ((uint)atoi((char *)s+1) - 1));
2290 return((uint)atoi((char *)s));
2291 }
2292 if(s[1] == 'x' || s[1] == 'X'){
2293 num = 0;
2294 for(s += 2; is_hex(*s); s++)
2295 num = (num << 4) + hex_to_dig(*s);
2296 return num;
2297 }else{
2298 num = 0;
2299 for(s += 1; is_oct(*s); s++)
2300 num = (num << 3) + *s - '0';
2301 return num;
2302 }
2303 }
2304
2305
2306 int StrLen(uchar *pt)
2307 {
2308 uchar *s;
2309
2310 for(s = pt; *s && *s != '\''; s++){
2311 if(*s == '\\' && (f_dtl&DTL_STRING)){
2312 if(is_hex(s[1]))
2313 s++;
2314 if(*s)
2315 s++;
2316 }
2317 }
2318 return s - pt;
2319 }
2320
2321
2322 void trans2dvi(void)
2323 {
2324 int num, pt, num0, pos0, len;
2325 int code = -1, pos = -1, page = 0, line = 0, f_in = 0, f_proc = 0, stack = 0;
2326 uchar *s, *base;
2327 static char hex[6] = "0x00";
2328
2329 s = (uchar *)tmp_buf; /* for Warning of compiler */
2330 while(fgets(tmp_buf, COMMON_SIZE, fp_in) != NULL){
2331 if(!line++ && !strncmp(tmp_buf, "variety ", 8)){
2332 if(!(f_dtl&DTL_VARIETY))
2333 f_dtl = 0x0ffff;
2334 continue;
2335 }
2336 base = (uchar *)tmp_buf;
2337 if(f_pos){ /* position: */
2338 if(*base <= ' ')
2339 continue;
2340 skp: while(*base++ > ' ');
2341 }
2342 if(*base < 'a' || *base > 'z'){
2343 if(!f_in){
2344 if(!f_proc && !f_pos && !strncmp(tmp_buf, "0: pre 2 ", 9)){
2345 f_pos = 1;
2346 goto skp;
2347 }
2348 continue;
2349 }else if(*base == '['){ /* PUSH by DTL */
2350 code = PUSH;
2351 goto cmd;
2352 }else if(*base == ']'){ /* POP by DTL */
2353 code = POP;
2354 goto cmd;
2355 }else if(*base == '\\' && (f_dtl&DTL_CHAR)){
2356 if(is_hex(base[1]) && is_hex(base[2])){ /* char \XY by DTL */
2357 if(f_proc != 1){
2358 if(!f_proc)
2359 goto er0;
2360 if(f_proc == 2)
2361 goto er3;
2362 goto er1;
2363 }
2364 if(!f_in)
2365 goto er2;
2366 hex[2] = base[1];
2367 hex[3] = base[2];
2368 putc(a2i((uchar *)hex), fp_out);
2369 }
2370 }else if(*base == '(' && (f_dtl&DTL_CHAR)){ /* char (...) by DTL */
2371 if(f_proc != 1){
2372 if(!f_proc)
2373 goto er0;
2374 if(f_proc == 2)
2375 goto er3;
2376 goto er1;
2377 }
2378 if(!f_in)
2379 goto er2;
2380 for(s = base; *++s != ')' && *s; ){ /* for DTL */
2381 if(*s != '\\')
2382 putc(*s, fp_out);
2383 else{
2384 s++;
2385 if(is_hex(*s) && is_hex(s[1])){
2386 hex[2] = *s;
2387 hex[3] = *s++;
2388 putc(a2i((uchar *)hex), fp_out);
2389 }else
2390 putc(*s, fp_out);
2391 }
2392 }
2393 }
2394 continue;
2395 }
2396 pt = find_key((char *)base);
2397 if(pt < 0){
2398 fprintf(stderr, "No key!\n");
2399 goto err;
2400 }
2401 code = key[pt].code;
2402 cmd: if(!page && code != PRE){
2403 er0: fprintf(stderr, "Need PRE comamnd at the top\n");
2404 goto err;
2405 }
2406 if(f_proc > 2){
2407 er1: fprintf(stderr, "Command after POST_POST\n");
2408 goto err;
2409 }
2410 if(!f_in){
2411 er2: if(code < FNT_DEF_1 && code != NOP && code != BOP){
2412 fprintf(stderr, "This command shoud be after BOP\n");
2413 goto err;
2414 }
2415 }
2416 if(f_proc == 2 && (code < FNT_DEF_1 || code > POST_POST)){
2417 er3: /* POST and PRE will be checked later */
2418 fprintf(stderr, "This command shoud be not be after POST\n");
2419 goto err;
2420 }
2421 switch(code){
2422 case PRE:
2423 if(f_proc){
2424 fprintf(stderr, "Multiple PRE command\n");
2425 goto err;
2426 }
2427 f_proc = 1;
2428 putc(code, fp_out); /* pre */
2429 page++;
2430 s = get_next(base);
2431 code = a2i(s);
2432 putc(code, fp_out); /* id */
2433 for(num=0; num <3; num++){
2434 s = get_next(s);
2435 code = a2i(s);
2436 write_n(code, 4);
2437 }
2438 s = get_next(s);
2439 code = a2i(s);
2440 s = get_next(s) + 1;
2441 if(f_dtl&DTL_EXACT){
2442 len = StrLen(s);
2443 if(len > 255)
2444 fprintf(stderr, "string length is too wrong (%d) in line %d\n%s\n",
2445 len, line, tmp_buf);
2446 else if(code != len)
2447 fprintf(stderr, "string length in [%d:%s]: %d -> %d:\n",
2448 line, tmp_buf, code, len);
2449 }else
2450 len = code;
2451 putc(len, fp_out); /* length of string */
2452 strout: if(len >= COMMON_SIZE/2){
2453 fprintf(stderr, "Too long data: %d\n", len);
2454 Exit(1);
2455 }
2456 while(len-- > 0){
2457 if((f_dtl&DTL_STRING) && *s == '\\'){
2458 s++;
2459 if(is_hex(*s)){
2460 code = hex_to_dig(*s) << 4;
2461 s++;
2462 code += hex_to_dig(*s);
2463 putc(code, fp_out);
2464 continue;
2465 }
2466 }
2467 putc(*s++, fp_out);
2468 }
2469 if(f_debug){
2470 if(*s != '\'' || s[1] > ' '){
2471 fprintf(stderr, "String length is not correct!\n");
2472 goto err;
2473 }
2474 }
2475 break;
2476
2477 case BOP:
2478 f_in = 1;
2479 pos0 = ftell(fp_out);
2480 putc(code, fp_out);
2481 s = base;
2482 for(num = 0; num < 10; num++){ /* c[] */
2483 s = get_next(s);
2484 write_n(a2i(s), 4);
2485 }
2486 if(f_debug){
2487 num0 = a2i(get_next(s));
2488 if(pos != num0)
2489 fprintf(stderr,
2490 "Address of BOP of page %d: %d -> %d\n",
2491 page-1, num0, pos);
2492 }
2493 write_n(pos, 4); /* position of the former bop */
2494 pos = pos0;
2495 break;
2496
2497 case FNT_NUM_0:
2498 font_use[sub_number] = 1;
2499 case SET_CHAR_0:
2500 putc(code+sub_number, fp_out);
2501 break;
2502
2503 case EOP:
2504 if(!f_in){
2505 fprintf(stderr, "EOP without BOP\n");
2506 goto err;
2507 }
2508 f_in = 0;
2509 if(stack > 0){
2510 fprintf(stderr, "Stack remains at page %d. It is corrected\n", stack);
2511 while(stack--)
2512 putc(code, fp_out);
2513 }
2514 page++;
2515 putc(code, fp_out);
2516 break;
2517
2518 case POP:
2519 if(--stack < 0){
2520 fprintf(stderr, "Stack underflow\n");
2521 goto err;
2522 }
2523 putc(code, fp_out);
2524 break;
2525
2526 case PUSH:
2527 if(++stack > max_stack)
2528 max_stack = stack;
2529 putc(code, fp_out);
2530 break;
2531
2532 case FNT1:
2533 putc(code+sub_number-1, fp_out);
2534 num = a2i(get_next(base));
2535 if(num >= 0 && num < MAX_FONT)
2536 font_use[num] = 1;
2537 write_n(num, sub_number);
2538 break;
2539
2540 case PUT1:
2541 case SET1:
2542 case DOWN1:
2543 case RIGHT1:
2544 putc(code+sub_number-1, fp_out);
2545 put_num: write_n(a2i(get_next(base)), sub_number);
2546 break;
2547
2548 case XXX1:
2549 s = get_next(base);
2550 num = a2i(s);
2551 s = get_next(s) + 1;
2552 if(f_dtl&DTL_EXACT){
2553 len = StrLen(s);
2554 if(num != len)
2555 fprintf(stderr, "string length in %d:[%s]: %d -> %d\n",
2556 line, tmp_buf, num, len);
2557 num0 = (len < 0x100)?1:2;
2558 if(sub_number != num0)
2559 fprintf(stderr, "Code is changed at line %d: xxx%d -> xxx%d\n",
2560 line, sub_number, num0);
2561 sub_number = num0;
2562 }else
2563 len = num;
2564 putc(code+sub_number-1, fp_out);
2565 write_n(len, sub_number);
2566 goto strout;
2567
2568 case EOFNC:
2569 putc(code, fp_out);
2570 sub_number = 1;
2571 goto put_num;
2572
2573 case W0:
2574 case X0:
2575 case Y0:
2576 case Z0:
2577 putc(code+sub_number, fp_out);
2578 if(sub_number)
2579 goto put_num;
2580 break;
2581
2582 case SET_RULE:
2583 case PUT_RULE:
2584 putc(code, fp_out);
2585 s = get_next(base);
2586 write_n(a2i(s), 4);
2587 s = get_next(s);
2588 write_n(a2i(s), 4);
2589 break;
2590
2591 case POST:
2592 if(f_in){
2593 fprintf(stderr, "Need EOP before POST\n");
2594 goto err;
2595 }
2596 if(f_proc > 1){
2597 fprintf(stderr, "Multiple POST\n");
2598 goto err;
2599 }
2600 f_proc = 2;
2601 pos0 = ftell(fp_out);
2602 putc(code, fp_out);
2603 s = get_next(base);
2604 if(f_debug){
2605 num0 = a2i(s);
2606 if(pos != num0)
2607 fprintf(stderr,
2608 "Address of BOP of the last page: %d -> %d\n",
2609 num0, pos);
2610 }
2611 write_n(pos, 4); /* position of the last bop */
2612 pos = pos0;
2613 s = get_next(s);
2614 for(num = 5; num-->0; ){
2615 write_n(a2i(s), 4);
2616 s = get_next(s);
2617 }
2618 num0 = a2i(s);
2619 if(max_stack != num0)
2620 fprintf(stderr, "Max stack depth: %d -> %d\n",
2621 num0, max_stack);
2622 write_n(max_stack, 2);
2623 s = get_next(s);
2624 num0 = a2i(s);
2625 if(num0 != --page)
2626 fprintf(stderr, "The number of total pages: %d -> %d\n",
2627 num0, page);
2628 write_n(page, 2);
2629 break;
2630
2631 case FNT_DEF_1:
2632 s = get_next(base);
2633 num0 = a2i(s);
2634 if(f_dtl & DTL_FNTDFN){
2635 if(num0 >= 0 && num0 < MAX_FONT){
2636 if(f_proc == 2){
2637 if(!font_use[num0]){
2638 fprintf(stderr, "The definition of font %d is deleted\n",
2639 num0);
2640 break;
2641 }else
2642 font_use[num0] = 1;
2643 }
2644 }
2645 }
2646 putc(code+sub_number-1, fp_out);
2647 write_n(num0, sub_number);
2648 s = get_next(s);
2649 for(num = 0; num < 3; num++){ /* checksum/s-size/d-size */
2650 if((f_dtl&DTL_FNTDEF) && !num && (s[1] != 'x' && s[1] != 'X')){
2651 num0 = *(s-1);
2652 *(s-1) = '0';
2653 write_n(a2i(s-1), 4);
2654 *(s-1) = (uchar)num0;
2655 }else
2656 write_n(a2i(s), 4);
2657 s = get_next(s);
2658 }
2659 num = a2i(s);
2660 s = get_next(s); /* directory */
2661 putc(num, fp_out);
2662 code = a2i(s);
2663 s = get_next(s) + 1; /* directory/name */
2664 putc(code, fp_out);
2665 while(num-- > 0)
2666 putc(*s++, fp_out); /* directory */
2667 if((*s == '\'' && s[1] <= ' ') || (f_dtl&DTL_FNTNAME))
2668 s = get_next(s) + 1;
2669 while(code-- > 0)
2670 putc(*s++, fp_out); /* name */
2671 break;
2672
2673 case POST_POST:
2674 if(f_proc++ != 2){
2675 fprintf(stderr, "Need POST before POST_POST!\n");
2676 goto err;
2677 }
2678 putc(code, fp_out);
2679 s = get_next(base);
2680 write_n(pos, 4); /* position of post */
2681 if(f_debug){
2682 num0 = a2i(s);
2683 if(pos != num0)
2684 fprintf(stderr, "Address of POST: %d -> %d\n",
2685 num0, pos);
2686 }
2687 for(num = 5; num-- > 0; ){
2688 s = get_next(s);
2689 code = a2i(s);
2690 if(num == 4){
2691 if(code != 2 && code != 3){
2692 fprintf(stderr, "Last id (%d) should be 2 or 3\n", code);
2693 goto err;
2694 }
2695 }else{
2696 if(code != END_DVI)
2697 fprintf(stderr, "Last code: %d -> %d\n", code, END_DVI);
2698 code = END_DVI;
2699 }
2700 putc(code, fp_out);
2701 }
2702 num = ftell(fp_out);
2703 while(num++ & 3)
2704 putc((uchar)END_DVI, fp_out);
2705 break;
2706
2707 case OPCODE:
2708 putc(a2i(get_next(s)), fp_out);
2709 break;
2710
2711 case NOP:
2712 putc(code, fp_out);
2713 break;
2714
2715 default:
2716 err: fprintf(stderr, "Error(line %d, code %d): %s\n", line, code, tmp_buf);
2717 Exit(1);
2718 }
2719 }
2720 num = ftell(fp_out);
2721 fprintf(stderr, "Write %d byte (%d page): %s\n", num, page,
2722 (fp_out==stdout)?"(stdout)":outfile);
2723 fclose(fp_in);
2724 fclose(fp_out);
2725 fp_in = fp_out = NULL;
2726 }