%begin; !D (Directory operations)
%include "inc:util.imp"
%externalstring(255)%fnspec FINFO(%string(255)directory,%integer number)

%integer num=1,full=0,ruthless=0
%string(255)whoever,file,new
%integer sym

%routine file info(%integer num,full)
%string(255)s
%integer p
  s = finfo(whoever, num)
  %stopif length(s)=0
  p=1; p = p+1 %while p<=length(s) %and charno(s,p)#' '
  file = substring(s,1,p-1)
  printsymbol(13); printsymbol(27); printsymbol('K')
  %if full=0 %then printstring(file) %else printstring(s)
  length(file) = length(file)-1 %if charno(file,length(file)) = '?'
  %returnif whoever=""
  file = whoever.":".file
%end

%routine readname(%string(255)%name s)
%integer k
  s = ""
  readsymbol(k) %until k#' '
  %while k#nl %cycle
    s = s.tostring(k); readsymbol(k)
  %repeat
%end

%routine show(%string(255)%name file)
%integer line=0,pos=0
  %routine dhex(%integer x)
    x=x&15; x=x+7 %if x>9; printsymbol(x+'0')
  %end
  %routine bhex(%integer x)
    dhex(x>>4); dhex(x)
  %end
  %routine sym
  %integer k
    readsymbol(k)
    %if k=nl %start
      pos=0; line = line+1; newline
      %signal 9 %if line>22
    %finishelseif ' '<=k<=126 %start
      pos = pos+1
      %if pos>=80 %start
        pos = 1; line = line+1; newline
        %signal 9 %if line>22
      %finish
      printsymbol(k)
    %finishelsestart
      pos = pos+3
      %if pos>=78 %start
        pos = 3; line = line+1; newline
        %signal 9 %if line>22
      %finish
      space; bhex(k)
    %finish
  %end
  %onevent 3,9 %start
    closeinput; %return
  %finish
  selectinput(1); openinput(1,file); selectinput(1)
  %cycle
    sym
  %repeat
%end

%routine get symbol (%integer %name sym)
   %on 3,9 %start
       newline
       %stop
   %finish
   read symbol (sym)
%end

%predicate sensitive(%string(255)file)
%integer i,sym,x=0
  %for i=1,1,length(file) %cycle
    sym = charno(file,i); sym = sym-32 %if sym>='a'-1
    x = x<<8+sym %if x#0 %or sym='.'
  %repeat
  i = ruthless
  %if nextsymbol = '!' %start
    skip symbol;  i = 1
    skip symbol %and ruthless = 1 %if nextsymbol = '!'
  %finish
  %if x=0 %or x='.ASM' %or x='.C' %start
    %false %if i # 0
    printstring("Not unless you say 'd!'"); newline
    %true
  %finish
  %false
%end

%routine explain
   printstring("
<return>, <cursor DOWN>, <cursor RIGHT>: Next file
<linefeed>, <cursor UP>, <cursor Left> : Previous file
F:  Full Info   S:      Show File     P: Permit file
C:  Copy file   R or N: Rename file   D: Delete file
?:  Help        Q, E,X: Exit

")
%end

%onevent 3,9 %start
  newline
  %cycle
    num = num+1; file info(num,0); newline
  %repeat
%finish

whoever = cliparam
%if whoever -> whoever.(":").file %start
  %if file#"" %start
    Print string ("D only takes a directory as a parameter")
    Newline
    %stop
  %finish
%finish
prompt(" ")
file info(0,1); newline
printstring("Type '?' for Help"); newline

set terminalmode(nopage)
%cycle
  %stopif num=0
  file info(num,full); full=0
  getsymbol(sym)
  %if sym=27 %start;           !Escape
    readsymbol(sym)
    num = num-1 %if sym='A' %or sym='D'
    num = num+1 %if sym='B' %or sym='C'
  %finishelseif sym=nl %start; !Return
    num = num+1
  %finishelseif sym=13 %start; !LF
    num = num-1
  %finishelsestart;             !Special function
    sym = sym!32
    %signal 9 %if sym='l'
    %if sym='f' %start;         !Full information
      full=1
    %elseif sym='d';            !Delete
      delete(file) %unless sensitive(file)
    %elseif sym='r' %or sym='n';!Rename
      readname(new); rename(file, new)
    %elseif sym='s';            !Show
      show(file)
    %elseif sym='p';            !Permit
      readname(new);permit(file, new)
    %elseif sym='c';            !Copy
      readname(new);copy(file, new)
    %elseif sym = '?';          !Help
      explain
    %else %if sym='q' %or sym='e' %or sym='x';  ! Quit, Exit
      %exit
    %finish
    selectinput(0)
    readsymbol(sym) %until sym=nl
  %finish
%repeat
select input (0)
read symbol (sym) %until sym=nl
%endofprogram
