%include "inc:atdecs.imp"
%include "inc:util.imp"
%include "inc:dict.imp"
%include "inc:fs.imp"
%include "inc:fsutil.imp"

%begin; ! Show System dictionaries etc

%integer bool=16_40000000
%string(255)p, outfile="",object="**"

%record(dictf)%map ofildict
  %result == fildict
%end

%record(dictf)%map oextdict
  %result == extdict
%end

%record(dictf)%map xxx(%integer parm)
%integer p
  p = integer(16_3f9c)+parm
  %result == record(integer(p))
%end

%record(dictf)%map fildict
  %result == xxx(740)
%end

%record(dictf)%map extdict
  %result == xxx(736)
%end

%predicate in(%integer x)
  %predicate in(%record(dictf)%name d)
    %cycle
      %trueif d_beg<x<d_pos
      %falseif d_alt=0
      d == record(d_alt)
    %repeat
  %end
  %trueif in(sysdict)
  %trueif in(extdict)
  %trueif in(fildict)
  %trueif in(comdict)
  %trueif in(oextdict)
  %trueif in(ofildict)
  %false
%end

%routine show(%string(31)n,%record(dictf)%name d,%integer magic)
%string(255)s
%integer tag,w1,w2,phase
  %cycle
    spaces(31-length(n)); printstring(n)
    w1 = (d_pos-d_beg)>>2
    w2 = (d_lim-d_beg)>>2
    write(w1,10); write(w2,8)
    %if w2=0 %then printstring("      100") %c
             %else write((w1*100)//w2,8)
    printsymbol('%'); newline
    tag = d_beg+8
    %while tag<d_pos %cycle
      tag = tag+4 %until byteinteger(tag)=0
      tag = tag+4; transname(tag,s)
      w1 = integer(tag); w2 = integer(tag+4)
      %if matches(s, object) %start
         spaces(31-length(s))
         printstring(s." = ")
         phex(w1); space; phex(w2)
         %if in(w1) %start
           space; transname(w1,s); printstring(s)
         %finishelsestart
           w1 = w2-w1; space %and phex(w1) %if w1>0
         %finish
         newline
      %finish
      tag = tag+magic
    %repeat
    n = "...and..."
    %returnif d_alt=0
    d == record(d_alt)
  %repeat
%end

%predicate selected
%integer old
  old = bool
  bool = old<<1
  %trueif old<0
  %false
%end

p = cliparam
define param("Object",object,0)
define boolean params("System,External,File,Command,OExternal,OFile,Memory",bool,0)
define param("Output",outfile,pam newgroup)
process parameters(p)

open output(1, outfile) %and selectoutput(1) %if outfile # ""

show("System dictionary",sysdict,8) %if selected
show("External dictionary",extdict,4) %if selected
show("File dictionary",fildict,4) %if selected
show("Command symbol dictionary",comdict,8) %if selected
show("Old External dictionary",oextdict,8) %if selected
show("Old File dictionary",ofildict,8) %if selected
%if selected %start
  printstring("Main store limits "); phex(membot); space; phex(memtop)
  write((memtop-membot)>>10,1); printsymbol('k'); newline
  printstring("Free store limits "); phex(freebot); space; phex(freetop)
  write((freetop-freebot)>>10,1); printsymbol('k'); newline
%finish

%endofprogram
