%  TOPS-20 change file for PKtoPX by Tomas Rokicki.  Send bug reports to
%  ROKICKI@SU-SCORE.  The majority of these changes eliminate the sign-bit
%  manipulations necessary on 32-bit machines, but unnecessary on the 36-bit
%  TOPS-20.

@x [1]
\pageno=\contentspagenumber \advance\pageno by 1
@y
\pageno=\contentspagenumber \advance\pageno by 1
\let\maybe=\iffalse
\def\ttw{{\mc TOPS-20}}
\def\title{PKtoPX changes for \ttw}
@z

@x [4]
@d banner=='This is PKtoPX, Version 2.3'
@y
@d banner=='This is PKtoPX, Version 2.3'
@z

@x [6]
@d print_ln(#)==write_ln(output,#)
@d print(#)==write(output,#)
@y
@d print_ln(#)==write_ln(tty,#)
@d print(#)==write(tty,#)
@z

@x [6]
@p program PKtoPX(input, output);
@y
@p program PKtoPX;
@z

@x [8]
@<Constants...@>=
@!name_length=80; {maximum length of a file name}
@!terminal_line_length=132; {maximum length of an input line}
@!max_div_32=100; {maximum character width divided by 32}
@y
@<Constants...@>=
@!name_length=80; {maximum length of a file name}
@!terminal_line_length=132; {maximum length of an input line}
@!max_div_32=100; {maximum character width divided by 32}
@!max_rescan=200; {maximum length of a command line}
@z

@x [55]
@!pxl_file:word_file; {where the input comes from}
@y
@!pxl_file:byte_file; {where the input comes from}
@z

@x [56]
@p procedure open_pxl_file; {prepares to write packed bytes in a |pxl_file|}
begin rewrite(pxl_file,pxl_name);
pxl_loc := 0 ;
end;
@#
procedure open_pk_file; {prepares the input for reading}
begin reset(pk_file,pk_name);
pk_loc := 0 ;
end;
@y
@p procedure open_pxl_file; {prepares to write packed bytes in a |pxl_file|}
begin rewrite(pxl_file,pxl_name,'/O/B:8');
pxl_loc := 0 ;
end;
@#
procedure open_pk_file; {prepares the input for reading}
begin reset(pk_file,pk_name,'/O/B:8');
pk_loc := 0 ;
end;
@z

@x [60]
@p procedure pixel_integer (i : integer) ;
begin pxl_file^ := i ;
put(pxl_file) ;
incr(pxl_loc) ;
end;
@y
@p procedure pixel_integer (i : integer) ;
begin if i < 0 then i := i + 65536 * 65536 ;
pxl_file^ := i div 16777216 ; put(pxl_file) ;
pxl_file^ := i div 65536 mod 256 ; put(pxl_file) ;
pxl_file^ := i div 256 mod 256 ; put(pxl_file) ;
pxl_file^ := i mod 256 ; put(pxl_file) ;
incr(pxl_loc) ;
end;
@z

@x [79]
@!power : array [0..31] of integer ; {contains the powers of two}
@y
@!power : array [0..32] of integer ; {contains the bit counts}
@z

@x [79]
power[0] := 1 ;
for i := 1 to 30 do power[i] := power[i-1] * 2 ;
power[31] := - one_fourth - one_fourth ;
@y
power[0] := 1 ;
for i := 1 to 32 do power[i] := power[i-1] * 2 ;
@z

@x [92]
@d get_line(#) == if eoln(input) then read_ln(input) ;
   i := 1 ;
   while not (eoln(input) or eof(input)) do begin
      #[i] := input^ ;
      incr(i) ;
      get(input) ;
   end ;
   #[i] := ' '
@y
@d get_line(#) == if eoln(tty) then read_ln(tty) ;
   i := 1 ;
   while not (eoln(tty) or eof(tty)) do begin
      #[i] := tty^ ;
      incr(i) ;
      get(tty) ;
   end ;
   #[i] := ' '
@z

@x [96]
initialize ;
dialog ;
@y
initialize ;
startup := true ;
@<Get command line and check for file names@> ;
if not startup then dialog ;
@z

@x [99]
@* System-dependent changes.
This section should be replaced, if necessary, by changes to the program
that are necessary to make \.{PKtoPX} work at a particular installation.
Any additional routines should be inserted here.
@^system dependencies@>
@y
@* System-dependent changes.
This section should be replaced, if necessary, by changes to the program
that are necessary to make \.{PKtoPX} work at a particular installation.
Any additional routines should be inserted here.
@^system dependencies@>

We call a system procedure to put the command line into the input buffer.
We then read it in, skipping over the program name to the first blank.

@d RSCAN=@'500 {ReSCAN buffer JSYS}

@<Get command line and check for file names@>=
jsys(RSCAN,1,i;0;j); {get the command line}
if (i<>2) or (j<=0) then startup := false
else begin
  if eoln(tty) then read_ln(tty); {for some TOPS-20's}
  read(tty,rescan_buffer:rescan_len); {read in rescan buffer}
  if rescan_len>max_rescan then abort('command line too long!');
  read_ln(tty);
  if rescan_len=j-2 then startup := false
  else begin
    i:=1; while rescan_buffer[i]>' 'do incr(i); {skip invocation}
    while(i<=rescan_len) and (rescan_buffer[i]=' ')do incr(i); {skip spaces}
    if i>rescan_len then startup := false
    else begin
      for j := 1 to name_length do begin
        pk_name[j] := ' ' ; pxl_name[j] := ' ' ;
      end ;
      j := 1 ;
      last_ext := -1 ;
      while ( i <= rescan_len ) and ( rescan_buffer[i] <> ' ' ) and
        ( rescan_buffer[i] <> '/' ) do begin
        pk_name[j] := rescan_buffer[i] ;
        pxl_name[j] := rescan_buffer[i] ;
        if (rescan_buffer[i] = '.') and (last_ext = -1) then last_ext := j ;
        if rescan_buffer[i] in [':',']','>'] then last_ext := -1 ;
        incr(j) ; incr(i) ;
      end ;
      if last_ext = -1 then begin
        last_ext := j ;
        pk_name[j] := '.' ; pk_name[j+1] := 'p' ; pk_name[j+2] := 'k' ;
      end ;
      pxl_name[last_ext] := '.' ; pxl_name[last_ext+1] := 'p' ;
      pxl_name[last_ext+2] := 'x' ; pxl_name[last_ext+3] := 'l' ;
      for i := last_ext+4 to name_length do pxl_name[i] := ' ' ;
    end ;
  end ;
end

@ @<Glob...@>=
@!rescan_buffer:packed array[1..max_rescan] of char;
@!rescan_len:integer;
@!startup : boolean ; {did we have a file name?}
@!last_ext : integer ; {where was the dot?}
@z