%externalroutine Help(%string(28) Wanted)
   %recordformat hfm(%string(31-9) Key, %byte Flag, %integer start, finish)
   %externalroutinespec To Upper(%string(*)%name S)
   %externalroutinespec open da(%integer chan, rw, %string(25) file)
   %externalroutinespec read da(%integer chan, block, %name to)
   %externalroutinespec close da(%integer chan)
   %routinespec Copy(%integer from, to)
   %routinespec find topic(%integername start, finish)
   %routinespec show all

   %integer start, finish, l, control
   %byteintegerarray b(0:511)
   %string(28) Topic, Topics

   Open Da(1, 0, "Help:Help.dat")

   control = 0
   %if wanted # "" %start
      control = Charno(wanted, length(wanted))
      %if control = '!' %start
         length(wanted) = length(wanted)-1
      %else
         control = 0
      %finish
   %finish

   %if wanted = "?"  %start
      show all
      %return
   %finish

   Topic = wanted
   To Upper(Topic)

   find topic(start, finish)
   %if start # 0 %start
      copy(start, finish)
   %else
      printstring("No information about ".wanted);  newline
      printstring("Help ?     will give a list of the topics available")
      New Line
   %finish
   %return

   %record(hfm)%map R(%integer n)
      %owninteger previous = -1
      %integer block, byte
      n = n*32
      block = n>>9
      read da(1, block, b(0)) %unless previous = block
      previous = block
      byte = n&511
      %result == record(addr(b(byte)))
   %end

   %routine show all
      %integer j,n
      %record(hfm)%name H

      %routine Add(%string(31) s)
         %owninteger n = 3
         %owninteger pending = 0
         %return %if s = ""
         spaces(pending);  pending = 0
         printstring(S)
         n = n-1
         n = 0 %if length(s) >= 22
         %if n <= 0 %start
            newline
            n = 3
         %else
            pending = 24-length(s)
         %finish
      %end

      printstring("Help is available on the following topics:");  newline
      n = R(0)_Finish
      %for j = 1,1,n %cycle
         h == r(J)
         add(H_key) %if H_Flag = '+'
      %repeat
      newline
   %end

   %routine find topic(%integername start, finish)
      %integer High, Low, P
      %record(hfm)%name H
      low = 1;  high = r(0)_finish
      %while low <= high %cycle
         p = (low+high)>>1
         H == R(P)
         %if H_Key = Topic %start
            start = H_Start;  Finish = H_finish
            %return
         %finish
         %if H_Key > Topic %then High = p-1 %else Low = P+1
      %repeat
      start = 0;  finish = 0
   %end

   %routine copy(%integer from, to)
      %integer last = nl, sym
      %integer quiet = 0
      %integer n, byte, block, next block, next byte
      Block = from>>9
      Byte  = from&511
      Next Block = to>>9
      Next Byte  = to&511
      n = (next block-block)*512-byte+next byte
      quiet = 1 %if control = '!'
      %cycle
         read da(1, block, b(0))
         %cycle
            sym = b(byte)
            %if last = nl %and sym = '?' %start   {more detailed info}
               %return %if control # '!'
               quiet = 0
            %else
               printsymbol(sym) %if quiet = 0
            %finish
            last = sym
            byte = byte+1
            n = n-1
         %repeat %until byte = 512 %or n = 0
         %exit %if n = 0
         block = block+1
         byte = 0
      %repeat
   %end
%end

%begin
   %include "Sysinc:command.inc"
   Help(Command_Parameter)
%endofprogram
