{-------------------------------------------------------------}
{              routines for phase 2 -- output                 }
{-------------------------------------------------------------}

procedure filenode(np: pnode; var oc: nchar);
    { write one node's contents, term + pages, to the output.
      It is at this level that we insert a blank line on a break
      in the sequence of main-term initial letters.  Once more,
      a loop over an ordered chain is cleaner with Goto. }
    label 99;
    var a : str;
        p : ppage;
        i : 0..9;
        j : strindex;
        k1, k2 : integer;
        ic : nchar;
    begin
        if not np^.skip then begin { ignore phony nodes }
            stget(np^.iref,a);
            ic := upcase(a.val[1]);
            if (indlevel=0) and  { main-term initial change? }
                 (oc<>ic) then writeln(outfile);
            oc := ic;
            for i := 1 to indlevel do write(outfile,'    ');
            for j := 1 to a.len do write(outfile,chr(a.val[j]));
            p := np^.phead;
            while p<>nil do begin
                write(outfile,' ');
                k1 := p^.num;
                k2 := k1+1;
             99:p := p^.next;   { elide sequential numbers     }
                if p<>nil then
                    if p^.num=k2 then begin
                        k2 := k2+1;
                        goto 99
                    end;
                write(outfile,k1:1); { write "17" or "17-19"   }
                if (k1+1)<k2 then write(outfile,'-',k2-1:1);
                if p<>nil then write(outfile,',');
            end;
        writeln(outfile);
        end
    end;

procedure filetree(intree: pnode);
    { walk through a (sub-) tree and write each node }
    var tree    : pnode;
        tw      : treewalk;
        oc      : nchar;
    begin
        oc := nullch;
        initwalk(intree,tw);
        tree := treestep(tw);
        while tree<>nil do begin
            filenode(tree,oc);
            if tree^.subt<>nil then begin
                indlevel := indlevel+1;
                filetree(tree^.subt);
                indlevel := indlevel-1
            end;
            tree := treestep(tw)
        end
    end;

procedure dump;
    begin
        assign(outfile,'INDEXER.OUT');
        rewrite(outfile);
        if 0=IoResult then filetree(maintree);
        close(outfile); { REAL Pascals don't require closes }
        if 0<>IoResult then writeln('close error');
     end;
        close(outfil