program runtimek (output);

  {
  this program reads a merged runtime library file
  and sorts the file by subroutine name.
  then it prints the contest with the corresponding
  module/file name
  }
  
  type
    struc = record
      sub_name : string[8];
      mod_name : string[8]
    end;

    sptr = ^struc;

  var
    count : byte;
    loop : byte;
    first, current, last : ^struc;
    table_start, table_end, ptr : ^sptr;
    name : string[8];
    line_buf : string[252];
    f : file of byte;
    
  procedure build_list;
    begin
      line_buf := substr (line_buf, 2, 251);
      while (line_buf[0] > #0) do
        begin
          if line_buf[1] = #1
            then
              begin
                new (last);
                with last^ do
                  begin
                    sub_name := substr (line_buf, 5, ord(line_buf[4]));
                    mod_name := name;
                  end
              end ;
          line_buf := substr (line_buf, 5 + ord(line_buf[4]), 236)
        end
    end;
{$p}
  procedure sort;

    procedure exchange (ptr1, ptr2 : ^sptr);
      var
        temp : ^sptr;
      begin
        temp := ptr1^;
        ptr1^ := ptr2^;
        ptr2^ := temp
      end;

    procedure quick (lo, hi : ^sptr);
      var
        i, j, pivit : ^sptr;
      begin
        if lo < hi
          then
            begin
              i := lo;
              j := hi;
              pivit := j;
              repeat
                while (i < j) and (i^^.sub_name <= pivit^^.sub_name) do
                  i := i + $4;
                while (j > i) and (j^^.sub_name >= pivit^^.sub_name) do
                  j := j - $4;
                if i < j
                  then
                    exchange (i, j);
              until i >= j;
              exchange (i, hi);
              if i - lo < hi - i
                then
                  begin
                    quick (lo, i - $4);
                    quick (i + $4, hi)
                  end
                else
                  begin
                    quick (i + $4, hi);
                    quick (lo, i - $4)
                  end
            end
      end;
      
      begin
        mark (table_start);
        for ptr := $0L to (last - first) div longhex(sizeof(struc)) do
          begin
            new (table_end);
            table_end^ := ptr * longhex(sizeof(struc)) + first
          end;
        quick (table_start, table_end)
    end;
{$p}
  begin
    mark (first);
    last := nil;
    reset (f, cline(1));
    while not eof(f) do
      begin
        read (f, line_buf[0]);
        for loop := #1 to line_buf[0] do
          read (f, line_buf[loop]);
        case line_buf[1] of
          #0 : name := substr (line_buf, 5, ord(line_buf[4]));
          #3 : build_list
        end
      end;
    close (f);
    if last <> nil
      then
        begin
          sort;
          writeln; 
          writeln;
          ptr := table_start;
          loop := #0;
          repeat
            current := ptr^;
            with current^ do
              begin
                write (sub_name:-8, ' : ', mod_name:-8, '   ');
                loop := succ (loop);
                if loop = #3
                  then
                    begin
                      writeln;
                      loop := #0
                    end
              end;
            ptr := ptr + $4
          until ptr > table_end;
          if loop <> #0
            then
              writeln 
        end
  end.
