{-------------------------------------------------------------}
{             routines for phase 3 -- termination             }
{-------------------------------------------------------------}

    procedure unload(t:pnode);
        { dump the contents of a (sub-) tree to disk in
            "preorder," a sequence such that the exact layout
            of the tree will be reconstructed if the tree is
            reloaded from the file. }
        label 99;
        var j   : strindex;
            p   : ppage;
            k   : integer;
            k1, k2 : nchar;
            x   : str;
        begin  {$r-  range checks off during byte i/o }
            if t^.skip then goto 99; { dump not the phony node }
            stget(t^.iref,x);
            k1 := x.len; { turbo won't "write(..,nchar(x.len))"}
            write(savetree,k1);
            for j:=x.len downto 1 do write(savetree,x.val[j]);
            p := t^.phead;
            while p<>nil do begin
                k := p^.num;
                k1 := k div 256; k2 := k mod 256;
                write(savetree,k1,k2);
                p := p^.next
            end;
            k1:= nullch; {turbo can't find nullch in write()}
            write(savetree,k1,k1); { flag end of pages }
            if t^.subt<>nil then begin
                k1 := strmax;
                write(savetree,k1);{ flag start of subtree }
                unload(t^.subt);
                k1 := nullch;
                write(savetree,k1) { flag end of subtree }
            end;
        99: if t^.lson<>nil then unload(t^.lson);
            if t^.rson<>nil then unload(t^.rson);
        end; {$r+ }

procedure saveall;
    { save the term-tree in the file INDEXER.TRE so it can
        be reloaded for additions later, if need be. }
    var k1 : nchar;
    begin
        assign(savetree,'INDEXER.TRE');
        rewrite(savetree);
        if 0=IoResult then begin
            unload(maintree);
            k1 := nullch; {stupid turbo}
            write(savetree,k1); { flag end of main tree }
            close(savetree); {REAL Pascal doesn't require closes}
            if 0<>IoResult then writeln('close error');
        end
    end;
vetree); {REAL Pascal doesn't require closes}
            if 0<>IoResult the