/*  --- University of Sussex POPLOG file -----------------------------------
 *  File:           $usepop/pop/local/plog/lib/xref/xrfdef.pl
 *  Purpose:        Prolog Cross-Reference Program - Handles .DEFinition files
 *  Author:         Dave Bowen (Edinburgh), Nov 20 1983
 *  Machines:       cvaxa unx1
 *  Documentation:  HELP * XREF
 *  Related Files:  Loaded by $usepop/pop/local/plog/lib/xref.pl
 */

/*
:- public load/1.       % User entry
:- public load_file/1.      % This is Called by XRF.PL 

:- mode load(+).
:- mode load_file(+).
:- mode note(+).
:- mode check_width(+).
:- mode check_filename(+).
*/

/******************************************************************************
Data for user definitions.

The following terms may be recorded using a predicate as the key:

    $system     for built-in predicates.
    $known(Where)   for predicates known to be defined in "Where".
    $applies(P,T)   for predicates P which apply one of their arguments T
    $called     for predicates which are called from other places

The following types of term may be recorded under the key $define:

    width(N)    where N is the required page width for the 
            cross-reference listing.
    title(X)    where X is the title for the listing.
    cross_ref_file(F) where F is the filename for the listing.
    globals_file(G) where G is the filename for the listing of the imports
            and exports of each file. If G='no' then no such 
            listing will be produced.

******************************************************************************/


                /* Load in definition file containing system or
                   known predicates, or operators */
load([F|L]) :- !,
    (load(F) ; true), !,
    load(L).
load([]) :- !.
load(File) :- !,
    see_chek(File),
    load_file(File),
    seen.

                /* Load given DEFinition file */
load_file(File) :-
    repeat,
      read(T),
    (T=end_of_file, !; note(T), fail),
    nl, write('Definition file '), 
    write(File), write(' loaded'),
    nl, nl.

                /* Process terms in definition file. */
note(system(P)) :- !,
    crecord(P,'$system').       % P is a system (built-in) predicate
note(known(P,Where)) :- !,
    crecord(P, '$known'(Where)).    % P is known to be defined in Where
note(op(Prec,Assoc,Name)) :- !,
    call(op(Prec,Assoc,Name)).  % operator defns handled as normally
note(applies(P,T)) :-  !,
    recorda(P, '$applies'(P,T),_).  % P must contain T which it Calls
note(called(P)) :- !,
    mark_interpreted(P).        % P is called from somewhere

                /* The following clauses deal with answers
                   to questions about layout of output etc */
note(width(N)) :- !,
    check_width(N),         % Paper width for cross-ref listing
    recorda('$define',width(N),_).
note(title(T)) :- !,            % Title of cross-ref listing
    recorda('$define',title(T),_).
note(cross_ref_file(F)) :- !,       % File name for cross-ref listing
    check_filename(F),
    recorda('$define',cross_ref_file(F),_).
note(globals_file(G)) :- !,     % File name for imports/exports listing
    check_filename(G),      % - may be 'no' meaning no listing reqd
    recorda('$define',globals_file(G),_).


% Routines for checking validity of arguments 

                /* Check Width is in range */
check_width(Width) :-  
    integer(Width), Width>=50, Width=<150, !;
    complain(['Width (',Width,') must be between 50 and 150']).

                /* Check legal file name */
check_filename(F) :-
    atom(F), ! ; complain(['Illegal file name ',F]). 
