%%%  WEB Change File for DVIcopy (Win32 version)
%%%  Time-stamp: "97/02/10 13:35:13 cschenk"
%%%
%%%  Copyright (C) 1997
%%%	Christian Schenk  <cschenk@berlin.snafu.de>
%%%
%%%  This file is part of MiKTeX.
%%%
%%%  MiKTeX is free software; you can redistribute it and/or
%%%  modify it under the terms of the GNU General Public License as
%%%  published by the Free Software Foundation; either version 2, or (at
%%%  your option) any later version.
%%%  
%%%  MiKTeX is distributed in the hope that it will be useful, but
%%%  WITHOUT ANY WARRANTY; without even the implied warranty of
%%%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%%%  General Public License for more details.
%%%  
%%%  You should have received a copy of the GNU General Public License
%%%  along with MiKTeX; if not, write to the Free Software
%%%  Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
%%%  USA.

%% **     1  ******************************************************************

@x
@d preamble_comment=='DVIcopy 1.5 output from '
@d comm_length=24 {length of |preamble_comment|}
@d from_length=6 {length of its |' from '| part}
@y
@d preamble_comment=='DVIcopy 1.5 output from '
@d comm_length=24 {length of |preamble_comment|}
@d commz_length=25 {length of |preamble_comment|}
@d from_length=6 {length of its |' from '| part}
@z

%% **     3  ******************************************************************

@x
procedure initialize; {this procedure gets things started properly}
@y
@<External C routines@>@/
procedure initialize; {this procedure gets things started properly}
@z

@x
  begin print_ln(banner);@/
@y
  begin print(banner); miktex_banner(output); write_ln;@/
@z

%% **     5  ******************************************************************

@x
@<Constants...@>=
@!max_fonts=100; {maximum number of distinct fonts}
@!max_chars=10000; {maximum number of different characters among all fonts}
@!max_widths=3000; {maximum number of different characters widths}
@!max_packets=5000; {maximum number of different characters packets;
  must be less than 65536}
@!max_bytes=30000; {maximum number of bytes for characters packets}
@!max_recursion=10; {\.{VF} files shouldn't recurse beyond this level}
@!stack_size=100; {\.{DVI} files shouldn't |push| beyond this depth}
@!terminal_line_length=150; {maximum number of characters input in a single
  line of input from the terminal}
@!name_length=50; {a file name shouldn't be longer than this}
@y
@<Constants...@>=
@!max_fonts=300; {maximum number of distinct fonts}
@!max_chars=750000; {maximum number of different characters among all fonts}
@!max_widths=10000; {maximum number of different characters widths}
@!max_packets=20000; {maximum number of different characters packets;
  must be less than 65536}
@!max_bytes=100000; {maximum number of bytes for characters packets}
@!max_recursion=10; {\.{VF} files shouldn't recurse beyond this level}
@!stack_size=100; {\.{DVI} files shouldn't |push| beyond this depth}
@!terminal_line_length=150; {maximum number of characters input in a single
  line of input from the terminal}
@!name_length=260; {a file name shouldn't be longer than this}
@!namez_length=261; {a file name shouldn't be longer than this}
@z

%% **    23  ******************************************************************

@x
@d abort(#)==begin print_ln(' ',#,'.'); jump_out;
    end
@y
@d abort(#)==begin print_ln(' ',#,'.'); jump_out;
    end
@d goto_final_end==c4p_longjmp(env_buf,1)
@z

@x
procedure jump_out;
begin mark_fatal; close_files_and_terminate;
goto final_end;
@y
procedure jump_out;
begin mark_fatal; close_files_and_terminate;
goto_final_end;
@z

%% **    62  ******************************************************************

@x
@!cur_name:packed array[1..name_length] of char; {external name,
  with no lower case letters}
@y
@!cur_name:packed array[1..namez_length] of char; {external name,
  with no lower case letters}
@z

%% **    67  ******************************************************************

@x
  if (b>="a")and(b<="z") then Decr(b)("a"-"A"); {convert to upper case}
@y
  {|if (b>="a")and(b<="z") then Decr(b)("a"-"A"); {convert to upper case}|}
@z

@x
  begin incr(l_cur_name); cur_name[l_cur_name]:=' ';
  end;
@y
  begin incr(l_cur_name); cur_name[l_cur_name]:=chr(0);
  end;
cur_name[name_length]:=chr(0);
@z

%% **    91  ******************************************************************

@x
id4(".")("T")("F")("M")(tfm_ext); {file name extension for \.{TFM} files}
@y
id4(".")("t")("f")("m")(tfm_ext); {file name extension for \.{TFM} files}
@z

%% **    92  ******************************************************************

@x
@d TFM_default_area_name=='TeXfonts:' {change this to the correct name}
@d TFM_default_area_name_length=9 {change this to the correct length}

@<Glob...@>=
@!TFM_default_area:packed array[1..TFM_default_area_name_length] of char;
@y
@d TFM_default_area_name=='[TeX]Font Metric Dirs:' {change this to the correct name}
@d TFM_default_area_name_length=22 {change this to the correct length}
@d TFM_def_area_namz_length=23 {change this to the correct length}

@<Glob...@>=
@!TFM_default_area:packed array[1..TFM_def_area_namz_length] of char;
@z

%% **    93  ******************************************************************

@x
@ @<Set init...@>=
TFM_default_area:=TFM_default_area_name;
@y
@ @<Set init...@>=
c4p_strcpy (TFM_default_area, TFM_default_area_name);
@z

%% **    95  ******************************************************************

@x
reset(tfm_file,cur_name);
if eof(tfm_file) then
@^system dependencies@>
  abort('---not loaded, TFM file can''t be opened!')
@y
if not mt_open_tfm_file (tfm_file, cur_name) then
@^system dependencies@>
  abort('---not loaded, TFM file can''t be opened!')
@z

%% **   110  ******************************************************************

@x
@<Open input file(s)@>=
reset(dvi_file); {prepares to read packed bytes from |dvi_file|}
dvi_loc:=0;
@y
@<Open input file(s)@>=
if (c4pargc <> 3) then
 abort('Usage: dvicopy inputfile outfile');
c4p_fopen(dvi_file,c4p_argv[1],c4p_rb_mode,true);
reset(dvi_file);
cur_loc:=0;
@z

%% **   112  ******************************************************************

@x
@ Next we come to the routines that are used only if |random_reading|    is
|true|. The driver program below needs two such routines: |dvi_length| should
compute the total number of bytes in |dvi_file|, possibly also
causing |eof(dvi_file)| to be true; and |dvi_move(n)| should position
|dvi_file| so that the next |dvi_byte| will read byte |n|, starting with
|n=0| for the first byte in the file.
@^system dependencies@>

Such routines are, of course, highly system dependent. They are implemented
here in terms of two assumed system routines called |set_pos| and |cur_pos|.
The call |set_pos(f,n)| moves to item |n| in file |f|, unless |n| is
negative or larger than the total number of items in |f|; in the latter
case, |set_pos(f,n)| moves to the end of file |f|.
The call |cur_pos(f)| gives the total number of items in |f|, if
|eof(f)| is true; we use |cur_pos| only in such a situation.

@p function dvi_length:int_32;
begin set_pos(dvi_file,-1); dvi_length:=cur_pos(dvi_file);
end;
@#
procedure dvi_move(@!n:int_32);
begin set_pos(dvi_file,n); dvi_loc:=n;
end;
@y
@ Next we come to the routines that are used only if |random_reading|    is
|true|. The driver program below needs two such routines: |dvi_length| should
compute the total number of bytes in |dvi_file|, possibly also
causing |eof(dvi_file)| to be true; and |dvi_move(n)| should position
|dvi_file| so that the next |dvi_byte| will read byte |n|, starting with
|n=0| for the first byte in the file.
@^system dependencies@>

Such routines are, of course, highly system dependent. They are implemented
here in terms of two system routines called |@!c4p_fseek()| and
|@!c4p_ftell()|.
The call |c4p_fseek(f,n,@!c4p_set)| moves to byte |n| in file |f|.
The call |c4p_fseek(f,n,@!c4p_end)| moves to the end of file |f|.
The call |c4p_ftell(f)| gives the total number of bytes in |f|, if
|eof(f)| is true; we use |c4p_ftell()| only in such a situation.

@p function dvi_length:int_32;
begin c4p_fseek(dvi_file,0,c4p_end); dvi_length:=c4p_ftell(dvi_file);
end;
@#
procedure dvi_move(n:int_32);
begin c4p_fseek(dvi_file,n,c4p_set); dvi_loc:=n;
end;
@z

%% **   135  ******************************************************************

@x
id3(".")("V")("F")(vf_ext); {file name extension for \.{VF} files}
@y
id3(".")("v")("f")(vf_ext); {file name extension for \.{VF} files}
@z

%% **   137  ******************************************************************

@x
@d VF_default_area_name=='TeXvfonts:' {change this to the correct name}
@d VF_default_area_name_length=10 {change this to the correct length}

@<Glob...@>=
@!VF_default_area:packed array[1..VF_default_area_name_length] of char;
@y
@d VF_default_area_name=='[dvips]VFFONTS:' {change this to the correct name}
@d VF_default_area_name_length=15 {change this to the correct length}
@d VF_def_area_namz_length=16 {change this to the correct length}

@<Glob...@>=
@!VF_default_area:packed array[1..VF_def_area_namz_length] of char;
@z

%% **   138  ******************************************************************

@x
@ @<Set init...@>=
VF_default_area:=VF_default_area_name;
@y
@ @<Set init...@>=
c4p_strcpy (VF_default_area, VF_default_area_name);
@z

%% **   139  ******************************************************************

@x
make_font_name(VF_default_area_name_length)(VF_default_area)(vf_ext);
reset(vf_file,cur_name);
if eof(vf_file) then
@^system dependencies@>
  goto not_found;
vf_loc:=0
@y
make_font_name(VF_default_area_name_length)(VF_default_area)(vf_ext);
if not mt_open_vf_file (vf_file,cur_name) then
@^system dependencies@>
  goto not_found;
vf_loc:=0
@z

%% **   163  ******************************************************************

@x
@ @<VF: Start a new level@>=
append_one(push);
vf_move[vf_ptr]:=vf_move[vf_ptr-1];
vf_push_loc[vf_ptr]:=byte_ptr;
vf_last_end[vf_ptr]:=byte_ptr;
vf_last[vf_ptr]:=vf_other
@y
@ @<VF: Start a new level@>=
append_one(push);
c4p_arrcpy (vf_move[vf_ptr], vf_move[vf_ptr-1]);
vf_last[vf_ptr]:=vf_other
@z

%% **   170  ******************************************************************

@x
  vf_move[vf_ptr]:=vf_move[vf_ptr-1];
@y
  c4p_arrcpy (vf_move[vf_ptr], vf_move[vf_ptr-1]);
@z

%% **   176  ******************************************************************

@x
@<Action procedures for |dialog|@>=
procedure input_ln; {inputs a line from the terminal}
var k:0..terminal_line_length;
begin if n_opt=0 then
  begin print('Enter option: '); update_terminal; reset(input);
  if eoln(input) then read_ln(input);
  k:=0; pckt_room(terminal_line_length);
  while (k<terminal_line_length)and not eoln(input) do
    begin append_byte(xord[input^]); incr(k); get(input);
    end;
  end
else if k_opt<n_opt then
  begin incr(k_opt);
  {Copy command line option number |k_opt| into |byte_mem| array!}
  end;
end;
@y
@<Action procedures for |dialog|@>=
procedure input_ln; {inputs a line from the terminal}
var k:0..terminal_line_length;
begin if n_opt=0 then
  begin print('Enter option: '); update_terminal; {reset(input)};
  if eoln(input) then read_ln(input);
  k:=0; pckt_room(terminal_line_length);
  while (k<terminal_line_length)and not eoln(input) do
    begin append_byte(xord[input^]); incr(k); get(input);
    end;
  end
else if k_opt<n_opt then
  begin incr(k_opt);
  {Copy command line option number |k_opt| into |byte_mem| array!}
  end;
end;
@z

%% **   241  ******************************************************************

@x
@p begin initialize; {get all variables initialized}
@y
@p begin
if c4p_setjmp (env_buf) <> 0 then goto final_end;
initialize; {get all variables initialized}
@z

%% **   246  ******************************************************************

@x
@<Open output file(s)@>=
rewrite(out_file); {prepares to write packed bytes to |out_file|}
@y
@<Open output file(s)@>=
if (c4pargc <> 3) then
 abort('Usage: dvicopy inputfile outputfile');
c4p_fopen(out_file,c4p_argv[2],c4p_wb_mode,true);
 {prepares to write packed bytes to |out_file|}
@z

%% **   260  ******************************************************************

@x
@!comment:packed array[1..comm_length] of char; {preamble comment prefix}
@y
@!comment:packed array[1..commz_length] of char; {preamble comment prefix}
@z

%% **   261  ******************************************************************

@x
comment:=preamble_comment; pckt_room(comm_length);
for k:=1 to comm_length do append_byte(xord[comment[k]]);
while byte_mem[p]=bi(" ") do incr(p); {remove leading blanks}
@y
c4p_strcpy(comment, preamble_comment); pckt_room(comm_length);
for k:=1 to comm_length do append_byte(xord[comment[k]]);
while byte_mem[p]=bi(" ") do incr(p); {remove leading blanks}
@z

%% **   293  ******************************************************************

@x
will be consistent with the printed program. More extensive changes,
which introduce new sections, can be inserted here; then only the index
itself will get a new section number.
@^system dependencies@>
@y
will be consistent with the printed program. More extensive changes,
which introduce new sections, can be inserted here; then only the index
itself will get a new section number.
@^system dependencies@>

@ @<External C routines@>=
function mt_open_tfm_file (var f:byte_file; n:integer):boolean; forward;
function mt_open_vf_file (var f:byte_file; n:integer):boolean; forward;
procedure miktex_banner (var f:text_file); forward;

@ @<Globals...@>=
@!env_buf : c4p_jmp_buf;
@z
