%Begin
%include "inc:util.imp"
%constinteger boardaddr2 = 16_7FF80    {was C8} ;!Channel 0
%recordformat DEVICE FM(%byte bstatus,data,intvec,dstatus,x,mode,y,command)
%string (255) s,param,junk
%integer verbatim
@boardaddr2 %record(device fm) device2

%routine send(%string (255) infile,font)
%string (255) qual,root,extn
%integer i,n

%routine putc(%Integer sym)
   %integer c
!t!   printsymbol(sym); %return
   %if device2_dstatus&2#0 %start ;!Something  back.  Check for X-off
     c = device2_data
     %if c=19 %start
       printstring("X-Offed by printer - waiting for X-On...  ")
       %cycle
         %while device2_dstatus&2=0 %cycle; %Repeat
       %repeatuntil device2_data=17
       printstring("  X-On"); newline
     %finish
   %finish

   %While device2_dstatus&1=0 %cycle; %repeat
   device2_data =sym
%End

%routine puts(%string (255) s)
   %integer i
   %return %if s=""
   %for i=1,1,length(s) %cycle
      %if charno(s,i) = '$' %and verbatim#0 %then putc('$')
      putc(charno(s,i))
   %repeat
%end

%routine dump(%string (255) file, %integer stream)
   %string (255) s, incfile, a
   %integer i
   %on 3,9 %start; ->eof; %finish

   openinput(stream, file); selectinput(stream)
   readsymbol(i)
   %if i=27 %then printline(file." is not a Layout 2.0 image") %and %return
   putc(i)
   %cycle
      readline(s)
      %if s -> a.("$z.include ").incfile %start
         dump(incfile, stream+1)
      %else
         puts(s); putc(10)
      %finish
   %repeat
   
   eof:closeinput
   puts(s) %and putc(10) %if s#""
%end


qual="" %unless infile -> infile.("/").qual
root=infile %unless infile -> root.(".").extn
infile = infile.".lay" %if exists(infile.".lay")
%unless exists(infile) %start
   printline("Can't access ".infile)
   %return
%finish

printstring("Sending ".infile." to laser printer"); newline

putc('$'); puts("m0.6"""); putc(10)
%if verbatim#0 %start
   putc('$'); puts("l0"); putc(10)
   putc('$')
   %if junk="" %or junk="8" %start
      puts("list8")
   %elseif junk="10" %or junk="12"
      puts("list"); puts(junk)
   %elseif length(junk)>4
      puts(junk)
   %finish
   putc(10)
%finish
dump(infile, 1)
%if verbatim#0 %start
   putc('$'); puts("e*"); putc(10)
%finish

%end

%routine set up chan 2
  %constinteger mode1=16_4e,mode2=16_30,comm=16_37,reset=8
  %on 0 %start
     printstring("No QSART at "); phex(boardaddr2); newline
     %stop
  %finish
  device2_bstatus = reset; device2_bstatus = 0
  device2_mode = mode1;    device2_mode = mode2+13 ;!9600
  device2_dstatus = 0;     device2_dstatus = 0
  device2_dstatus = 0;     device2_command = comm
%end

   setup chan 2
   param = cli param
   %if param -> param.("-v").junk %then verbatim=1 %else verbatim=0
   length(param) = length(param)-1 %while charno(param, length(param)) = ' '
   send(param,junk)
%endofprogram

%string(255) %fn top file(%string (255) dir)
   %string (255) s
   %on 3,9 %start; %result=""; %finish
   openinput(1,dir); selectinput(1)
   readline(s)
   %result=s
%end

%routine wait(%integer ticks)
   ticks=cputime+ticks
   %cycle; %repeatuntil cputime>=ticks
%end

