home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / p / plbin.zip / pl / boot / autoload.pl < prev    next >
Text File  |  1992-09-10  |  4KB  |  145 lines

  1. /*  autoload.pl,v 1.2 1992/09/10 19:42:52 jan Exp
  2.  
  3.     Copyright (c) 1990 Jan Wielemaker. All rights reserved.
  4.     jan@swi.psy.uva.nl
  5.  
  6.     Purpose: Automatic library loading
  7. */
  8.  
  9. :- module($autoload,
  10.     [ $find_library/5
  11.     , $in_library/2
  12.     , $update_library_index/0
  13.     , make_library_index/1
  14.     ]).
  15.  
  16. :- dynamic
  17.     library_index/3.            % Head x Module x Path
  18.  
  19. %    $find_library(+Module, +Name, +Arity, -LoadModule, -Library)
  20. %
  21. %    Locate a predicate in the library.  Name and arity are the name
  22. %    and arity of the predicate searched for.  `Module' is the
  23. %    preferred target module.  The return values are the full path names
  24. %    of the library and module declared in that file.
  25.  
  26. $find_library(Module, Name, Arity, LoadModule, Library) :-
  27.     load_library_index,
  28.     functor(Head, Name, Arity),
  29.     (   library_index(Head, Module, Library),
  30.         LoadModule = Module
  31.     ;   library_index(Head, LoadModule, Library)
  32.     ), !.
  33.  
  34. %    $in_library(?Name, ?Arity)
  35. %    Is true if Name/Arity is in the autoload libraries.
  36.  
  37. $in_library(Name, Arity) :-
  38.     load_library_index,
  39.     library_index(Head, _, _),
  40.     functor(Head, Name, Arity).
  41.  
  42.         /********************************
  43.         *          UPDATE INDEX        *
  44.         ********************************/
  45.  
  46. $update_library_index :-
  47.     absolute_file_name('', CWD),
  48.     user:library_directory(Dir),
  49.         update_library_index(CWD, 'INDEX.pl', Dir),
  50.     fail.
  51. $update_library_index.
  52.  
  53. update_library_index(CWD, Index, Dir) :-
  54.     exists_directory(Dir),
  55.     concat_atom([Dir, /, Index], IndexFile),
  56.     access_file(IndexFile, write),
  57.     chdir(Dir),
  58.     time_file(Index, IndexTime),
  59.     expand_file_name('*.pl', Files),
  60.     (   member(X, ['.'|Files]),
  61.         X \== Index,
  62.         time_file(X, TX),
  63.         TX @> IndexTime
  64.     ->  chdir(CWD),
  65.         format(user, 'Rebuilding index for library ~w ... ', Dir),
  66.         ttyflush,
  67.         library_index(Dir, Index),
  68.         format(user, 'ok.~n', []),
  69.         clear_library_index
  70.     ;   chdir(CWD)
  71.     ).
  72.  
  73. clear_library_index :-
  74.     retractall(library_index(_, _, _)).
  75.  
  76.         /********************************
  77.         *           LOAD INDEX        *
  78.         ********************************/
  79.  
  80. load_library_index :-
  81.     library_index(_, _, _), !.        % loaded
  82. load_library_index :-
  83.     user:library_directory(Dir),
  84.         concat_atom([Dir, '/', 'INDEX.pl'], Index),
  85.         exists_file(Index),
  86.         read_index(Index, Dir),
  87.     fail.
  88. load_library_index.
  89.     
  90. read_index(Index, Dir) :-
  91.     seeing(Old), see(Index),
  92.     repeat,
  93.         read(Term),
  94.         (   Term == end_of_file
  95.         ->  !
  96.         ;   assert_index(Term, Dir),
  97.             fail
  98.         ),
  99.     seen, see(Old).
  100.  
  101. assert_index(index(Name, Arity, Module, File), Dir) :- !,
  102.     functor(Head, Name, Arity),
  103.     concat_atom([Dir, '/', File], Path),
  104.     assertz(library_index(Head, Module, Path)).
  105. assert_index(Term, Dir) :-
  106.     $warning('Illegal term in INDEX.pl of directory ~w: ~w', [Dir, Term]).
  107.     
  108.  
  109.         /********************************
  110.         *       CREATE INDEX.pl        *
  111.         ********************************/
  112.  
  113. make_library_index(Dir) :-
  114.     access_file(Dir, write), !,
  115.     library_index(Dir, 'INDEX.pl').
  116. make_library_index(Dir) :-
  117.     $warning('make_library_index/1: Cannot write ~w', [Dir]).
  118.  
  119. library_index(Dir, Index) :-
  120.     absolute_file_name('', OldDir),
  121.     chdir(Dir),
  122.     expand_file_name('*.pl', Files),
  123.     delete(Files, Index, PrologFiles),
  124.     open(Index, write, Fd),
  125.     index_header(Fd),
  126.     checklist(index_file(Fd), PrologFiles),
  127.     close(Fd),
  128.     chdir(OldDir).
  129.  
  130. index_file(Fd, File) :-
  131.     open(File, read, In),
  132.     read(In, Term),
  133.     close(In),
  134.     Term = (:- module(Module, Public)), !,
  135.     forall( member(Name/Arity, Public),
  136.         format(Fd, 'index((~k), ~k, ~k, ~k).~n',
  137.                [Name, Arity, Module, File])).
  138. index_file(_, _).
  139.  
  140. index_header(Fd):-
  141.     format(Fd, '/*  autoload.pl,v 1.2 1992/09/10 19:42:52 jan Exp~n~n', []),
  142.     format(Fd, '    Creator: make/0~n~n', []),
  143.     format(Fd, '    Purpose: Provide index for autoload~n', []),
  144.     format(Fd, '*/~n~n', []).
  145.