%include "inc:util.imp"
%include "dict.inc"
%begin {show}

%string(255)c,d,e,dd,ee
%integer me,de,t

!Wildcard stuff

%constinteger wildmax=10
%string(31)%array wc(1:wildmax)
%integer wp=0

%predicate matches(%string(*)%name s,p,%integer w)
! S=Subject, P=Pattern. Pattern is the one with the stars in it.
! The bits in S corresponding to wildcards in P are
! stored in global array WC at index position W upwards.
! The global WC index pointer WP is updated appropriately.
%integer slen,plen
  %predicate m(%integer spos,ppos,wpos)
  %integer psym,ssym
    %cycle
      %if ppos=plen %start
        %trueif spos=slen; %false
      %finish
      ppos = ppos+1; psym = charno(p,ppos)
      %exitif psym='*'
      %falseif spos=slen
      spos = spos+1; ssym = charno(s,spos)
      psym = ssym %if ssym!32=psym!32 %and 'a'<=psym!32<='z'
      %unless ssym=psym %start
        %falseunless psym='%'
        wpos = wpos+1; wc(wpos) = tostring(ssym)
        wp = wpos
      %finish
    %repeat
    wpos = wpos+1; wc(wpos) = ""; wp = wpos
    %cycle
      %trueif m(spos,ppos,wpos)
      %exitif spos=slen
      spos = spos+1; ssym = charno(s,spos)
      wc(wpos) = wc(wpos).tostring(ssym)
    %repeat
    wp = wpos-1; %false
  %end
  slen = length(s); plen = length(p)
  wp = w
  %trueif m(0,0,w); %false
%end

  c = cliparam; toupper(c)
  c = d." ".e %while c -> d.("  ").e
  d = c %and e = "*" %unless c -> d.(" ").e
  d = "*" %if d=""
  d = "COM" %if d="SYM"
  me = firstentry(finddict(""))
  %while me#0 %cycle
    dd = translateentry(me)
    %if matches(dd,d,1) %start
      de = firstentry(record(integer(me)))
      %while de#0 %cycle
        ee = translateentry(de)
        %if matches(ee,e,1) %start
          printstring(dd) %and space %unless d=dd
          printstring(ee)
          t = integer(de)
          %if t#0 %start
            spaces(24-length(ee)); space
            %if dd="EXT" %start
              printstring(translateentry(t))
            %elseif dd="FIL"
              phex(t); space; phex(integer(t))
            %else
              printstring(string(t))
            %finish
          %finish
          newline
        %finish
        de = nextentry(de)
      %repeat
    %finish
    me = nextentry(me)
  %repeat
%end
