2013-11-13 22:52:36 +00:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
2015-03-16 17:25:09 +00:00
|
|
|
* YAP Prolog *
|
2013-11-13 22:52:36 +00:00
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
2014-04-21 11:14:18 +01:00
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-2014 *
|
2013-11-13 22:52:36 +00:00
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
|
2015-01-04 23:58:23 +00:00
|
|
|
%% @{
|
|
|
|
|
2014-07-27 01:14:15 +01:00
|
|
|
/** @defgroup YAPAbsoluteFileName File Name Resolution
|
|
|
|
|
2015-01-04 23:58:23 +00:00
|
|
|
@ingroup builtins
|
2014-04-21 11:14:18 +01:00
|
|
|
|
|
|
|
Support for file name resolution through absolute_file_name/3 and
|
|
|
|
friends. These utility built-ins describe a list of directories that
|
|
|
|
are used by load_files/2 to search. They include pre-compiled paths
|
|
|
|
plus user-defined directories, directories based on environment
|
|
|
|
variables and registry information to search for files.
|
|
|
|
|
2015-01-04 23:58:23 +00:00
|
|
|
**/
|
2014-04-21 11:14:18 +01:00
|
|
|
:- system_module( absolute_file_name, [absolute_file_name/2,
|
2014-04-09 12:39:29 +01:00
|
|
|
absolute_file_name/3,
|
|
|
|
add_to_path/1,
|
|
|
|
add_to_path/2,
|
|
|
|
path/1,
|
|
|
|
remove_from_path/1], ['$full_filename'/3,
|
|
|
|
'$system_library_directories'/2]).
|
|
|
|
|
|
|
|
:- use_system_module( '$_boot', ['$system_catch'/4]).
|
|
|
|
|
|
|
|
:- use_system_module( '$_errors', ['$do_error'/2]).
|
|
|
|
|
|
|
|
:- use_system_module( '$_lists', [member/2]).
|
2014-04-06 17:05:17 +01:00
|
|
|
|
2015-09-21 23:05:36 +01:00
|
|
|
/**
|
|
|
|
@pred user:library_directory(?Directory:atom) is nondet, dynamic
|
|
|
|
|
|
|
|
Dynamic, multi-file predicate that succeeds when _Directory_ is a
|
|
|
|
current library directory name. Asserted in the user module.
|
|
|
|
|
|
|
|
Library directories are the places where files specified in the form
|
|
|
|
`library( _File_ )` are searched by the predicates consult/1,
|
|
|
|
reconsult/1, use_module/1, ensure_loaded/1, and load_files/2.
|
|
|
|
|
|
|
|
This directory is initialized by a rule that calls the system predicate
|
|
|
|
system_library/1.
|
|
|
|
*/
|
|
|
|
:- multifile user:library_directory/1.
|
|
|
|
:- dynamic user:library_directory/1.
|
|
|
|
|
|
|
|
%% user:library_directory( ?Dir )
|
|
|
|
% Specifies the set of directories where
|
|
|
|
% one can find Prolog libraries.
|
|
|
|
%
|
|
|
|
% 1. honor YAPSHAREDIR
|
|
|
|
user:library_directory( Dir ) :-
|
|
|
|
getenv( 'YAPSHAREDIR', Dir0),
|
|
|
|
absolute_file_name( Dir0, [file_type(directory), expand(true),file_errors(fail)], Dir ).
|
|
|
|
%% 2. honor user-library
|
|
|
|
user:library_directory( Dir ) :-
|
|
|
|
absolute_file_name( '~/share/Yap', [file_type(directory), expand(true),file_errors(fail)], Dir ).
|
|
|
|
%% 3. honor current directory
|
|
|
|
user:library_directory( Dir ) :-
|
|
|
|
absolute_file_name( '.', [file_type(directory), expand(true),file_errors(fail)], Dir ).
|
|
|
|
%% 4. honor default location.
|
|
|
|
user:library_directory( Dir ) :-
|
|
|
|
system_library( Dir ).
|
|
|
|
|
|
|
|
/**
|
|
|
|
@pred user:commons_directory(? _Directory_:atom) is nondet, dynamic
|
|
|
|
|
|
|
|
State the location of the Commons Prolog Initiative.
|
|
|
|
|
|
|
|
This directory is initialized as a rule that calls the system predicate
|
|
|
|
library_directories/2.
|
|
|
|
*/
|
|
|
|
|
|
|
|
:- multifile user:commons_directory/1.
|
|
|
|
|
|
|
|
:- dynamic user:commons_directory/1.
|
|
|
|
|
|
|
|
|
|
|
|
user:commons_directory( Path ):-
|
|
|
|
system_commons( Path ).
|
|
|
|
|
|
|
|
/**
|
|
|
|
@pred user:foreign_directory(? _Directory_:atom) is nondet, dynamic
|
|
|
|
|
|
|
|
State the location of the Foreign Prolog Initiative.
|
|
|
|
|
|
|
|
This directory is initialized as a rule that calls the system predicate
|
|
|
|
library_directories/2.
|
|
|
|
*/
|
|
|
|
|
|
|
|
:- multifile user:foreign_directory/1.
|
|
|
|
|
|
|
|
:- dynamic user:foreign_directory/1.
|
|
|
|
|
|
|
|
user:foreign_directory( Path ):-
|
|
|
|
system_foreign( Path ).
|
|
|
|
|
|
|
|
/**
|
|
|
|
@pred user:prolog_file_type(?Suffix:atom, ?Handler:atom) is nondet, dynamic
|
|
|
|
|
|
|
|
This multifile/dynamic predicate relates a file extension _Suffix_
|
|
|
|
to a language or file type _Handler_. By
|
|
|
|
default, it supports the extensions yap, pl, and prolog for prolog files and
|
|
|
|
uses one of dll, so, or dylib for shared objects. Initial definition is:
|
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~prolog
|
|
|
|
prolog_file_type(yap, prolog).
|
|
|
|
prolog_file_type(pl, prolog).
|
|
|
|
prolog_file_type(prolog, prolog).
|
|
|
|
prolog_file_type(qly, prolog).
|
|
|
|
prolog_file_type(qly, qly).
|
|
|
|
prolog_file_type(A, prolog) :-
|
|
|
|
current_prolog_flag(associate, A),
|
|
|
|
A \== prolog,
|
|
|
|
A \==pl,
|
|
|
|
A \== yap.
|
|
|
|
prolog_file_type(A, executable) :-
|
|
|
|
current_prolog_flag(shared_object_extension, A).
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
:- multifile user:prolog_file_type/2.
|
|
|
|
|
|
|
|
:- dynamic user:prolog_file_type/2.
|
|
|
|
|
|
|
|
user:prolog_file_type(yap, prolog).
|
|
|
|
user:prolog_file_type(pl, prolog).
|
|
|
|
user:prolog_file_type(prolog, prolog).
|
|
|
|
user:prolog_file_type(A, prolog) :-
|
|
|
|
current_prolog_flag(associate, A),
|
|
|
|
A \== prolog,
|
|
|
|
A \== pl,
|
|
|
|
A \== yap.
|
|
|
|
user:prolog_file_type(qly, qly).
|
|
|
|
user:prolog_file_type(A, executable) :-
|
|
|
|
current_prolog_flag(shared_object_extension, A).
|
|
|
|
|
|
|
|
/**
|
|
|
|
@pred user:file_search_path(+Name:atom, -Directory:atom) is nondet
|
|
|
|
|
|
|
|
Allows writing file names as compound terms. The _Name_ and
|
|
|
|
_DIRECTORY_ must be atoms. The predicate may generate multiple
|
|
|
|
solutions. The predicate is originally defined as follows:
|
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~prolog
|
|
|
|
file_search_path(library, Dir) :-
|
|
|
|
library_directory(Dir).
|
|
|
|
file_search_path(commons, Dir) :-
|
|
|
|
commons_directory(Dir).
|
|
|
|
file_search_path(swi, Home) :-
|
|
|
|
current_prolog_flag(home, Home).
|
|
|
|
file_search_path(yap, Home) :-
|
|
|
|
current_prolog_flag(home, Home).
|
|
|
|
file_search_path,(system, Dir) :-
|
|
|
|
prolog_flag(host_type, Dir).
|
|
|
|
file_search_path(foreign, Dir) :-
|
|
|
|
foreign_directory(Dir).
|
|
|
|
file_search_path(path, C) :-
|
|
|
|
( getenv('PATH', A),
|
|
|
|
( current_prolog_flag(windows, true)
|
|
|
|
-> atomic_list_concat(B, ;, A)
|
|
|
|
; atomic_list_concat(B, :, A)
|
|
|
|
),
|
|
|
|
lists:member(C, B)
|
|
|
|
).
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Thus, `compile(library(A))` will search for a file using
|
|
|
|
library_directory/1 to obtain the prefix,
|
|
|
|
whereas 'compile(system(A))` would look at the `host_type` flag.
|
|
|
|
|
|
|
|
@}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
:- multifile user:file_search_path/2.
|
|
|
|
|
|
|
|
:- dynamic user:file_search_path/2.
|
|
|
|
user:file_search_path(library, Dir) :-
|
|
|
|
user:library_directory(Dir).
|
|
|
|
user:file_search_path(commons, Dir) :-
|
|
|
|
user:commons_directory(Dir).
|
|
|
|
user:file_search_path(swi, Home) :-
|
|
|
|
current_prolog_flag(home, Home).
|
|
|
|
user:file_search_path(yap, Home) :-
|
|
|
|
current_prolog_flag(home, Home).
|
|
|
|
user:file_search_path(system, Dir) :-
|
|
|
|
prolog_flag(host_type, Dir).
|
|
|
|
user:file_search_path(foreign, Dir) :-
|
|
|
|
working_directory(Dir,Dir).
|
|
|
|
user:file_search_path(foreign, yap('lib/Yap')).
|
|
|
|
user:file_search_path(path, C) :-
|
|
|
|
( getenv('PATH', A),
|
|
|
|
( current_prolog_flag(windows, true)
|
|
|
|
-> atomic_list_concat(B, ;, A)
|
|
|
|
; atomic_list_concat(B, :, A)
|
|
|
|
),
|
|
|
|
lists:member(C, B)
|
|
|
|
).
|
|
|
|
|
|
|
|
%%@}
|
|
|
|
|
|
|
|
%%@ def group Searching for a file ro consult.
|
|
|
|
|
|
|
|
|
2014-04-06 17:05:17 +01:00
|
|
|
/**
|
2014-07-17 18:19:38 +01:00
|
|
|
@pred absolute_file_name(+File:atom, +Options:list, +Path:atom) is nondet
|
|
|
|
@pred absolute_file_name(-File:atom, +Path:atom, +Options:list) is nondet
|
2014-04-06 17:05:17 +01:00
|
|
|
|
2014-04-21 11:14:18 +01:00
|
|
|
_Options_ is a list of options to guide the conversion:
|
2014-04-06 17:05:17 +01:00
|
|
|
|
2014-04-21 11:14:18 +01:00
|
|
|
- extensions(+ _ListOfExtensions_)
|
2014-04-06 17:05:17 +01:00
|
|
|
|
|
|
|
List of file-extensions to try. Default is `''`. For each
|
|
|
|
extension, absolute_file_name/3 will first add the extension and then
|
|
|
|
verify the conditions imposed by the other options. If the condition
|
|
|
|
fails, the next extension of the list is tried. Extensions may be
|
2014-04-21 11:14:18 +01:00
|
|
|
specified both with dot, as `.ext`, or without, as plain `ext`.
|
2014-04-06 17:05:17 +01:00
|
|
|
|
2014-04-21 11:14:18 +01:00
|
|
|
- relative_to(+ _FileOrDir_ )
|
2014-05-25 20:52:45 +01:00
|
|
|
|
2014-04-06 17:05:17 +01:00
|
|
|
Resolve the path relative to the given directory or directory the
|
|
|
|
holding the given file. Without this option, paths are resolved
|
2014-04-21 11:14:18 +01:00
|
|
|
relative to the working directory (see working_directory/2) or,
|
|
|
|
if _Spec_ is atomic and absolute_file_name/3 is executed
|
2014-04-06 17:05:17 +01:00
|
|
|
in a directive, it uses the current source-file as reference.
|
|
|
|
|
2014-04-21 11:14:18 +01:00
|
|
|
- access(+ _Mode_ )
|
2014-04-06 17:05:17 +01:00
|
|
|
|
2014-04-21 11:14:18 +01:00
|
|
|
Imposes the condition access_file( _File_ , _Mode_ ). _Mode_ is one of `read`, `write`, `append`, `exist` or
|
2014-04-06 17:05:17 +01:00
|
|
|
`none` (default).
|
2014-05-25 20:52:45 +01:00
|
|
|
|
2014-04-21 11:14:18 +01:00
|
|
|
See also access_file/2.
|
2014-04-06 17:05:17 +01:00
|
|
|
|
2014-04-21 11:14:18 +01:00
|
|
|
- file_type(+ _Type_ )
|
2014-04-06 17:05:17 +01:00
|
|
|
|
2014-04-21 11:14:18 +01:00
|
|
|
Defines suffixes matching one of several pre-specified type of files. Default mapping is as follows:
|
|
|
|
|
|
|
|
1. `txt` implies `[ '' ]`,
|
2014-05-25 20:52:45 +01:00
|
|
|
|
2014-04-21 11:14:18 +01:00
|
|
|
2. `prolog` implies `['.yap', '.pl', '.prolog', '']`,
|
2014-05-25 20:52:45 +01:00
|
|
|
|
2014-04-21 11:14:18 +01:00
|
|
|
3. `executable` implies `['.so', ',dylib', '.dll']` depending on the Operating system,
|
2014-05-25 20:52:45 +01:00
|
|
|
|
2014-09-22 18:13:35 +01:00
|
|
|
4. `qly` implies `['.qly', '']`,
|
2014-05-25 20:52:45 +01:00
|
|
|
|
2014-04-21 11:14:18 +01:00
|
|
|
5. `directory` implies `['']`,
|
2014-05-25 20:52:45 +01:00
|
|
|
|
2014-04-21 11:14:18 +01:00
|
|
|
6. The file-type `source` is an alias for `prolog` designed to support compatibility with SICStus Prolog. See also prolog_file_type/2.
|
2014-04-06 17:05:17 +01:00
|
|
|
|
2014-04-21 11:14:18 +01:00
|
|
|
Notice that this predicate only
|
2014-04-06 17:05:17 +01:00
|
|
|
returns non-directories, unless the option `file_type(directory)` is
|
|
|
|
specified, or unless `access(none)`.
|
|
|
|
|
|
|
|
- file_errors(`fail`/`error`)
|
|
|
|
|
|
|
|
If `error` (default), throw and `existence_error` exception
|
|
|
|
if the file cannot be found. If `fail`, stay silent.
|
|
|
|
|
|
|
|
- solutions(`first`/`all`)
|
|
|
|
|
2014-04-21 11:14:18 +01:00
|
|
|
If `first` (default), the search cannot backtrack. leaves no choice-point.
|
2014-04-06 17:05:17 +01:00
|
|
|
Otherwise a choice-point will be left and backtracking may yield
|
|
|
|
more solutions.
|
|
|
|
|
|
|
|
- expand(`true`/`false`)
|
|
|
|
|
2014-04-21 11:14:18 +01:00
|
|
|
If `true` (default is `false`) and _Spec_ is atomic,
|
|
|
|
call expand_file_name/2 followed by member/2 on _Spec_ before
|
2014-04-06 17:05:17 +01:00
|
|
|
proceeding. This is originally a SWI-Prolog extension.
|
|
|
|
|
2015-09-21 23:05:36 +01:00
|
|
|
- verbose_file_search(`true`/`false`)
|
|
|
|
|
|
|
|
If `true` (default is `false`) output messages during
|
|
|
|
search. This is often helpful when debugging. Corresponds to the
|
|
|
|
SWI-Prolog flag `verbose_file_search`.
|
|
|
|
|
|
|
|
|
2014-04-06 17:05:17 +01:00
|
|
|
Compatibility considerations to common argument-order in ISO as well
|
|
|
|
as SICStus absolute_file_name/3 forced us to be flexible here.
|
2014-04-21 11:14:18 +01:00
|
|
|
If the last argument is a list and the second not, the arguments are
|
|
|
|
swapped, making the call
|
|
|
|
~~~~~~~~~~~prolog
|
|
|
|
absolute_file_name(+ _Spec_ , - _Path_ ,+ _Options_ )
|
|
|
|
~~~~~~~~~~~
|
|
|
|
valid as well.
|
2014-04-06 17:05:17 +01:00
|
|
|
*/
|
|
|
|
|
2015-02-16 11:43:42 +00:00
|
|
|
absolute_file_name(File,TrueFileName,Opts) :-
|
2014-09-25 09:43:50 +01:00
|
|
|
( var(TrueFileName) ->
|
|
|
|
true ;
|
2015-02-16 11:43:42 +00:00
|
|
|
atom(TrueFileName), TrueFileName \= []
|
2014-09-25 09:43:50 +01:00
|
|
|
), !,
|
|
|
|
absolute_file_name(File,Opts,TrueFileName).
|
2014-06-20 11:13:39 +01:00
|
|
|
absolute_file_name(File,Opts,TrueFileName) :-
|
2014-09-25 09:43:50 +01:00
|
|
|
'$absolute_file_name'(File,Opts,TrueFileName,absolute_file_name(File,Opts,TrueFileName)).
|
2014-04-21 11:14:18 +01:00
|
|
|
|
|
|
|
/**
|
2014-07-17 18:19:38 +01:00
|
|
|
@pred absolute_file_name(+Name:atom,+Path:atom) is nondet
|
2014-04-21 11:14:18 +01:00
|
|
|
|
2015-03-16 17:25:09 +00:00
|
|
|
Converts the given file specification into an absolute path, using default options. See absolute_file_name/3 for details on the options.
|
2014-04-21 11:14:18 +01:00
|
|
|
*/
|
|
|
|
absolute_file_name(V,Out) :- var(V), !, % absolute_file_name needs commenting.
|
|
|
|
'$do_error'(instantiation_error, absolute_file_name(V, Out)).
|
|
|
|
absolute_file_name(user,user) :- !.
|
2015-09-21 23:05:36 +01:00
|
|
|
absolute_file_name(File0,File) :-
|
2014-04-21 11:14:18 +01:00
|
|
|
'$absolute_file_name'(File0,[access(none),file_type(txt),file_errors(fail),solutions(first)],File,absolute_file_name(File0,File)).
|
|
|
|
|
2014-10-02 14:24:12 +01:00
|
|
|
'$full_filename'(F0, F, G) :-
|
2015-04-22 21:21:20 +01:00
|
|
|
'$absolute_file_name'(F0,[access(read),file_type(prolog),file_errors(fail),solutions(first),expand(true)],F,G).
|
2014-04-21 11:14:18 +01:00
|
|
|
|
2013-11-13 22:52:36 +00:00
|
|
|
'$absolute_file_name'(File, _Opts, _TrueFileName, G) :- var(File), !,
|
|
|
|
'$do_error'(instantiation_error, G).
|
2015-09-21 23:05:36 +01:00
|
|
|
'$absolute_file_name'(File,LOpts,TrueFileName, G) :-
|
|
|
|
current_prolog_flag(file_name_variables, OldF),
|
2015-06-19 01:30:13 +01:00
|
|
|
current_prolog_flag( fileerrors, PreviousFileErrors ),
|
2015-09-21 23:05:36 +01:00
|
|
|
current_prolog_flag( verbose_file_search, PreviousVerbose ),
|
|
|
|
abs_file_parameters(LOpts,Opts),
|
|
|
|
get_abs_file_parameter( verbose_file_search, Opts, Verbose ),
|
|
|
|
get_abs_file_parameter( expand, Opts, Expand ),
|
|
|
|
set_prolog_flag( verbose_file_search, Verbose ),
|
|
|
|
get_abs_file_parameter( file_errors, Opts, FErrors ),
|
2015-06-19 01:30:13 +01:00
|
|
|
( FErrors = fail ->
|
|
|
|
set_prolog_flag( fileerrors, false )
|
|
|
|
;
|
|
|
|
set_prolog_flag( fileerrors, true )
|
|
|
|
),
|
2015-09-21 23:05:36 +01:00
|
|
|
set_prolog_flag(file_name_variables, Expand),
|
|
|
|
'$absf_trace'('search for ~w with options ~w', [File, LOpts] ),
|
|
|
|
'$find_in_path'(File, Opts,TrueFileName,G),
|
2013-11-13 22:52:36 +00:00
|
|
|
(
|
2015-09-21 23:05:36 +01:00
|
|
|
get_abs_file_parameter( solutions, Opts, first )
|
|
|
|
->
|
|
|
|
'$absf_trace'('found solution ~a', [TrueFileName] ),
|
|
|
|
% stop_lowxb( _level_trace,
|
|
|
|
set_prolog_flag( fileerrors, PreviousFileErrors ),
|
|
|
|
set_prolog_flag( file_name_variables, OldF),
|
|
|
|
set_prolog_flag( verbose_file_search, PreviousVerbose ),
|
|
|
|
'$absf_trace'('first solution only', [] ),
|
|
|
|
!
|
|
|
|
;
|
|
|
|
(
|
|
|
|
'$absf_trace'('found solution ~a', [TrueFileName] ),
|
|
|
|
% stop_low_level_trace,
|
|
|
|
set_prolog_flag( fileerrors, PreviousFileErrors ),
|
|
|
|
set_prolog_flag( file_name_variables, OldF),
|
|
|
|
set_prolog_flag( verbose_file_search, PreviousVerbose )
|
2014-05-25 20:52:45 +01:00
|
|
|
;
|
2015-09-21 23:05:36 +01:00
|
|
|
set_prolog_flag( verbose_file_search, Verbose ),
|
|
|
|
get_abs_file_parameter( file_errors, Opts, FErrors ),
|
|
|
|
set_prolog_flag(file_name_variables, Expand),
|
|
|
|
fail
|
|
|
|
)
|
|
|
|
;
|
|
|
|
% no solution
|
|
|
|
% stop_low_level_trace,
|
|
|
|
set_prolog_flag( fileerrors, PreviousFileErrors ),
|
|
|
|
set_prolog_flag( verbose_file_search, PreviousVerbose ),
|
|
|
|
set_prolog_flag(file_name_variables, OldF),
|
|
|
|
get_abs_file_parameter( file_errors, Opts, error ),
|
|
|
|
'$do_error'(existence_error(file,File),G)
|
|
|
|
).
|
2014-05-25 20:52:45 +01:00
|
|
|
|
2013-11-13 22:52:36 +00:00
|
|
|
% This sequence must be followed:
|
|
|
|
% user and user_input are special;
|
|
|
|
% library(F) must check library_directories
|
|
|
|
% T(F) must check file_search_path
|
|
|
|
% all must try search in path
|
|
|
|
'$find_in_path'(user,_,user_input, _) :- !.
|
|
|
|
'$find_in_path'(user_input,_,user_input, _) :- !.
|
|
|
|
'$find_in_path'(S, Opts, NewFile, Call) :-
|
|
|
|
S =.. [Name,File0],
|
2014-03-09 12:26:07 +00:00
|
|
|
'$cat_file_name'(File0,File), !,
|
2015-09-21 23:05:36 +01:00
|
|
|
'$absf_trace'('~w(~w) to ~w', [Name, File0, File] ),
|
2013-11-13 22:52:36 +00:00
|
|
|
'$dir_separator'(D),
|
|
|
|
atom_codes(A,[D]),
|
|
|
|
'$extend_path_directory'(Name, A, File, Opts, NewFile, Call).
|
|
|
|
'$find_in_path'(File0,Opts,NewFile,_) :-
|
2014-03-09 12:26:07 +00:00
|
|
|
'$cat_file_name'(File0,File), !,
|
2015-09-21 23:05:36 +01:00
|
|
|
'$add_path'(File, PFile),
|
2013-11-13 22:52:36 +00:00
|
|
|
'$get_abs_file'(PFile,Opts,AbsFile),
|
|
|
|
'$search_in_path'(AbsFile,Opts,NewFile).
|
|
|
|
'$find_in_path'(File,_,_,Call) :-
|
|
|
|
'$do_error'(domain_error(source_sink,File),Call).
|
|
|
|
|
|
|
|
% allow paths in File Name
|
|
|
|
'$cat_file_name'(File0,File) :-
|
|
|
|
atom(File0), !,
|
|
|
|
File = File0.
|
|
|
|
'$cat_file_name'(Atoms, File) :-
|
|
|
|
'$to_list_of_atoms'(Atoms, List, []),
|
|
|
|
atom_concat(List, File).
|
|
|
|
|
|
|
|
'$to_list_of_atoms'(V, _, _) :- var(V), !, fail.
|
|
|
|
'$to_list_of_atoms'(Atom, [Atom|L], L) :- atom(Atom), !.
|
|
|
|
'$to_list_of_atoms'(Atoms, L1, LF) :-
|
|
|
|
Atoms =.. [A,As,Bs],
|
|
|
|
atom_codes(A,[D]),
|
|
|
|
'$dir_separator'(D),
|
|
|
|
'$to_list_of_atoms'(As, L1, [A|L2]),
|
|
|
|
'$to_list_of_atoms'(Bs, L2, LF).
|
|
|
|
|
2015-09-21 23:05:36 +01:00
|
|
|
'$get_abs_file'(File,Opts,AbsFile) :-
|
|
|
|
get_abs_file_parameter( expand, Opts, Expand ),
|
|
|
|
'$absf_trace'('variable expansion allowed? ~w', [Expand] ),
|
|
|
|
'$absolute_file_name'(File,ExpFile),
|
|
|
|
'$absf_trace'(' variable expansion ~w', [ExpFile] ),
|
|
|
|
get_abs_file_parameter( relative_to, Opts, RelTo ),
|
2013-11-13 22:52:36 +00:00
|
|
|
(
|
2015-09-21 23:05:36 +01:00
|
|
|
RelTo \= '.'
|
2014-05-25 20:52:45 +01:00
|
|
|
->
|
2015-09-21 23:05:36 +01:00
|
|
|
( is_absolute_file_name(ExpFile) ->
|
2014-03-09 12:26:07 +00:00
|
|
|
AbsFile = ExpFile
|
|
|
|
;
|
|
|
|
'$dir_separator'(D),
|
|
|
|
atom_codes(DA,[D]),
|
2015-09-21 23:05:36 +01:00
|
|
|
atom_concat([RelTo, DA, ExpFile], AbsFile),
|
|
|
|
'$absf_trace'('add relative path ~a', [RelTo] )
|
2014-03-09 12:26:07 +00:00
|
|
|
)
|
|
|
|
;
|
|
|
|
AbsFile = ExpFile
|
2015-09-21 23:05:36 +01:00
|
|
|
),
|
|
|
|
'$absf_trace'('after relative to absolute path, ~a ', [AbsFile] ).
|
2014-05-25 20:52:45 +01:00
|
|
|
|
2013-11-13 22:52:36 +00:00
|
|
|
|
2015-09-21 23:05:36 +01:00
|
|
|
'$search_in_path'(File,Opts,F) :-
|
|
|
|
get_abs_file_parameter( extensions, Opts, Extensions ),
|
|
|
|
'$absf_trace'('check extensions ~w?', [Extensions] ),
|
2013-11-13 22:52:36 +00:00
|
|
|
'$add_extensions'(Extensions, File, F0),
|
2015-09-21 23:05:36 +01:00
|
|
|
get_abs_file_parameter( file_type, Opts, Type ),
|
|
|
|
get_abs_file_parameter( access, Opts, Access ),
|
|
|
|
'$absf_trace'('check access permission ~a...', [Access] ),
|
|
|
|
'$check_file'(F0,Type, Access, F).
|
|
|
|
'$search_in_path'(File,Opts,F) :-
|
|
|
|
get_abs_file_parameter( file_type, Opts, Type ),
|
|
|
|
'$absf_trace'('check type ~w', [Type] ),
|
|
|
|
'$add_type_extensions'(Type,File, F0),
|
|
|
|
get_abs_file_parameter( access, Opts, Access ),
|
|
|
|
'$absf_trace'('check access permission ~w?', [Access] ),
|
2013-11-13 22:52:36 +00:00
|
|
|
'$check_file'(F0, Type, Access, F).
|
|
|
|
|
2015-06-19 01:30:13 +01:00
|
|
|
% always verify if a directory
|
|
|
|
'$check_file'(F, directory, _, F) :-
|
|
|
|
!,
|
|
|
|
exists_directory(F).
|
2013-11-13 22:52:36 +00:00
|
|
|
'$check_file'(F, _Type, none, F) :- !.
|
2015-06-19 01:30:13 +01:00
|
|
|
'$check_file'(F0, _Type, Access, F0) :-
|
2013-11-13 22:52:36 +00:00
|
|
|
access_file(F0, Access),
|
2015-06-19 01:30:13 +01:00
|
|
|
\+ exists_directory(F0). % if it has a type cannot be a directory..
|
2013-11-13 22:52:36 +00:00
|
|
|
|
2015-09-21 23:05:36 +01:00
|
|
|
'$add_extensions'([Ext|_], File,F) :-
|
|
|
|
'$absf_trace'(' extension ~w', [Ext] ),
|
2013-11-13 22:52:36 +00:00
|
|
|
'$mk_sure_true_ext'(Ext,NExt),
|
|
|
|
atom_concat([File,NExt],F).
|
|
|
|
'$add_extensions'([_|Extensions],File,F) :-
|
|
|
|
'$add_extensions'(Extensions,File,F).
|
2015-09-21 23:05:36 +01:00
|
|
|
|
2013-11-13 22:52:36 +00:00
|
|
|
'$mk_sure_true_ext'(Ext,NExt) :-
|
|
|
|
atom_codes(Ext,[C|L]),
|
|
|
|
C \= 0'.,
|
|
|
|
!,
|
|
|
|
atom_codes(NExt,[0'.,C|L]).
|
|
|
|
'$mk_sure_true_ext'(Ext,Ext).
|
|
|
|
|
|
|
|
'$add_type_extensions'(Type,File,F) :-
|
|
|
|
( Type == source -> NType = prolog ; NType = Type ),
|
|
|
|
user:prolog_file_type(Ext, NType),
|
2015-09-21 23:05:36 +01:00
|
|
|
atom_concat([File,'.',Ext],F),
|
|
|
|
'$absf_trace'(' extension ~w?', [F] ).
|
|
|
|
'$add_type_extensions'(_,File,File) :-
|
|
|
|
'$absf_trace'(' wo extension ~w?', [File] ).
|
2013-11-13 22:52:36 +00:00
|
|
|
|
2015-09-21 23:05:36 +01:00
|
|
|
'$add_path'(File, File) :-
|
|
|
|
is_absolute_file_name(File), !.
|
|
|
|
'$add_path'(File, File) :-
|
|
|
|
working_directory(Dir, Dir),
|
|
|
|
'$dir_separator'( D ),
|
|
|
|
atom_codes( DSep, [D] ),
|
|
|
|
atomic_concat([Dir, DSep,File],PFile),
|
|
|
|
'$absf_trace'(' try . or ~a: ~a', [Dir,PFile] ).
|
|
|
|
'$add_path'(File, PFile) :-
|
2013-11-13 22:52:36 +00:00
|
|
|
recorded('$path',Path,_),
|
2015-09-21 23:05:36 +01:00
|
|
|
atom_concat([Path,File],PFile),
|
|
|
|
'$absf_trace'(' try ~a from path-data base: ~a', [Path, PFile] ).
|
2013-11-13 22:52:36 +00:00
|
|
|
|
2014-03-04 11:58:48 +00:00
|
|
|
'$system_library_directories'(library, Dir) :-
|
2015-02-16 11:43:42 +00:00
|
|
|
user:library_directory( Dir ).
|
2014-11-10 01:54:14 +00:00
|
|
|
% '$split_by_sep'(0, 0, Dirs, Dir).
|
2014-03-06 02:09:48 +00:00
|
|
|
'$system_library_directories'(foreign, Dir) :-
|
2014-11-10 01:54:14 +00:00
|
|
|
foreign_directory( Dir ).
|
2014-10-09 10:43:44 +01:00
|
|
|
% compatibility with old versions
|
|
|
|
% search the current directory first.
|
2014-04-06 17:05:17 +01:00
|
|
|
'$system_library_directories'(commons, Dir) :-
|
2014-11-10 01:54:14 +00:00
|
|
|
commons_directory( Dir ).
|
2013-11-13 22:52:36 +00:00
|
|
|
|
|
|
|
'$split_by_sep'(Start, Next, Dirs, Dir) :-
|
2015-06-19 01:30:13 +01:00
|
|
|
current_prolog_flag(windows, true),
|
2014-03-06 02:09:48 +00:00
|
|
|
'$split_by_sep'(Start, Next, Dirs, ';', Dir), !.
|
2013-11-13 22:52:36 +00:00
|
|
|
'$split_by_sep'(Start, Next, Dirs, Dir) :-
|
|
|
|
'$split_by_sep'(Start, Next, Dirs, ':', Dir).
|
|
|
|
|
|
|
|
'$split_by_sep'(Start, Next, Dirs, Sep, Dir) :-
|
|
|
|
sub_atom(Dirs, Next, 1, _, Let), !,
|
|
|
|
'$continue_split_by_sep'(Let, Start, Next, Dirs, Sep, Dir).
|
|
|
|
'$split_by_sep'(Start, Next, Dirs, _Sep, Dir) :-
|
|
|
|
Next > Start,
|
|
|
|
Len is Next-Start,
|
|
|
|
sub_atom(Dirs, Start, Len, _, Dir).
|
|
|
|
|
|
|
|
|
|
|
|
% closed a directory
|
|
|
|
'$continue_split_by_sep'(Sep, Start, Next, Dirs, Sep, Dir) :-
|
|
|
|
Sz is Next-Start,
|
|
|
|
Sz > 0,
|
|
|
|
sub_atom(Dirs, Start, Sz, _, Dir).
|
|
|
|
% next dir
|
|
|
|
'$continue_split_by_sep'(Sep , _Start, Next, Dirs, Sep, Dir) :- !,
|
|
|
|
N1 is Next+1,
|
|
|
|
'$split_by_sep'(N1, N1, Dirs, Dir).
|
|
|
|
% same dir
|
|
|
|
'$continue_split_by_sep'(_Let, Start, Next, Dirs, Sep, Dir) :-
|
|
|
|
N1 is Next+1,
|
|
|
|
'$split_by_sep'(Start, N1, Dirs, Sep, Dir).
|
|
|
|
|
|
|
|
|
2014-10-07 01:35:41 +01:00
|
|
|
'$extend_path_directory'(_Name, _D, File, _Opts, File, _Call) :-
|
2013-11-13 22:52:36 +00:00
|
|
|
is_absolute_file_name(File), !.
|
|
|
|
'$extend_path_directory'(Name, D, File, Opts, NewFile, Call) :-
|
|
|
|
user:file_search_path(Name, IDirs),
|
2015-09-21 23:05:36 +01:00
|
|
|
'$absf_trace'('file_search_path ~a is ~w', [Name, IDirs] ),
|
2015-01-27 03:25:31 +00:00
|
|
|
ground(IDirs),
|
2015-01-26 03:33:47 +00:00
|
|
|
(
|
|
|
|
'$extend_path_directory'(IDirs, D, File, Opts, NewFile, Call)
|
|
|
|
;
|
|
|
|
atom(IDirs) ->
|
|
|
|
'$split_by_sep'(0, 0, IDirs, Dir)
|
2013-11-13 22:52:36 +00:00
|
|
|
;
|
|
|
|
Dir = IDirs
|
|
|
|
),
|
|
|
|
'$extend_pathd'(Dir, D, File, Opts, NewFile, Call).
|
|
|
|
|
|
|
|
'$extend_pathd'(Dir, A, File, Opts, NewFile, Goal) :-
|
|
|
|
atom(Dir), !,
|
|
|
|
'$add_file_to_dir'(Dir,A,File,NFile),
|
2015-09-21 23:05:36 +01:00
|
|
|
'$absf_trace'(' try ~a', [NFile] ),
|
2013-11-13 22:52:36 +00:00
|
|
|
'$find_in_path'(NFile, Opts, NewFile, Goal), !.
|
|
|
|
'$extend_pathd'(Name, A, File, Opts, OFile, Goal) :-
|
|
|
|
nonvar(Name),
|
|
|
|
Name =.. [N,P0],
|
|
|
|
'$add_file_to_dir'(P0,A,File,NFile),
|
|
|
|
NewName =.. [N,NFile],
|
2015-09-21 23:05:36 +01:00
|
|
|
'$absf_trace'(' try ~a', [NewName] ),
|
2013-11-13 22:52:36 +00:00
|
|
|
'$find_in_path'(NewName, Opts, OFile, Goal).
|
|
|
|
|
|
|
|
'$add_file_to_dir'(P0,A,Atoms,NFile) :-
|
|
|
|
atom_concat([P0,A,Atoms],NFile).
|
|
|
|
|
2015-09-21 23:05:36 +01:00
|
|
|
'$absf_trace'(Msg, Args ) :-
|
|
|
|
current_prolog_flag( verbose_file_search, true ),
|
|
|
|
!,
|
|
|
|
print_message( informational, absolute_file_path( Msg, Args ) ).
|
|
|
|
'$absf_trace'(_Msg, _Args ).
|
|
|
|
|
2014-09-15 19:10:49 +01:00
|
|
|
/** @pred prolog_file_name( +File, -PrologFileName)
|
|
|
|
|
|
|
|
Unify _PrologFileName_ with the Prolog file associated to _File_.
|
|
|
|
|
|
|
|
*/
|
|
|
|
prolog_file_name(File, PrologFileName) :-
|
|
|
|
var(File), !,
|
|
|
|
'$do_error'(instantiation_error, prolog_file_name(File, PrologFileName)).
|
|
|
|
prolog_file_name(user, Out) :- !, Out = user.
|
|
|
|
prolog_file_name(File, PrologFileName) :-
|
|
|
|
atom(File), !,
|
|
|
|
operating_system_support:true_file_name(File, PrologFileName).
|
|
|
|
prolog_file_name(File, PrologFileName) :-
|
2014-10-07 01:35:41 +01:00
|
|
|
'$do_error'(type_error(atom,File), prolog_file_name(File, PrologFileName)).
|
2014-09-15 19:10:49 +01:00
|
|
|
|
2014-04-21 11:14:18 +01:00
|
|
|
/**
|
2014-07-17 18:19:38 +01:00
|
|
|
@pred path(-Directories:list) is det,deprecated
|
2014-04-06 17:05:17 +01:00
|
|
|
|
2014-05-25 20:52:45 +01:00
|
|
|
YAP specific procedure that returns a list of user-defined directories
|
|
|
|
in the library search-path.We suggest using user:file_search_path/2 for
|
|
|
|
compatibility with other Prologs.
|
2014-04-06 17:05:17 +01:00
|
|
|
*/
|
2015-09-21 23:05:36 +01:00
|
|
|
path(Path) :-
|
|
|
|
findall(X,'$in_path'(X),Path).
|
2014-04-06 17:05:17 +01:00
|
|
|
|
2015-09-21 23:05:36 +01:00
|
|
|
'$in_path'(X) :-
|
|
|
|
recorded('$path',Path,_),
|
|
|
|
atom_codes(Path,S),
|
|
|
|
( S = "" -> X = '.' ;
|
|
|
|
atom_codes(X,S) ).
|
2014-04-06 17:05:17 +01:00
|
|
|
|
2014-04-21 11:14:18 +01:00
|
|
|
/**
|
2014-07-17 18:19:38 +01:00
|
|
|
@pred add_to_path(+Directory:atom) is det,deprecated
|
2014-04-06 17:05:17 +01:00
|
|
|
|
2014-04-21 11:14:18 +01:00
|
|
|
YAP-specific predicate to include directory in library search path.
|
2014-05-25 20:52:45 +01:00
|
|
|
We suggest using user:file_search_path/2 for
|
|
|
|
compatibility with other Prologs.
|
2014-04-06 17:05:17 +01:00
|
|
|
*/
|
2015-09-21 23:05:36 +01:00
|
|
|
add_to_path(New) :-
|
|
|
|
add_to_path(New,last).
|
2014-04-06 17:05:17 +01:00
|
|
|
|
2014-04-21 11:14:18 +01:00
|
|
|
/**
|
2014-07-17 18:19:38 +01:00
|
|
|
@pred add_to_path(+Directory:atom, +Position:atom) is det,deprecated
|
2014-04-21 11:14:18 +01:00
|
|
|
|
2014-05-25 20:52:45 +01:00
|
|
|
YAP-specific predicate to include directory in front or back of
|
|
|
|
library search path. We suggest using user:file_search_path/2 for
|
|
|
|
compatibility with other Prologs and more extensive functionality.
|
2014-04-21 11:14:18 +01:00
|
|
|
*/
|
2014-04-06 17:05:17 +01:00
|
|
|
add_to_path(New,Pos) :-
|
|
|
|
atom(New), !,
|
|
|
|
'$check_path'(New,Str),
|
|
|
|
atom_codes(Path,Str),
|
|
|
|
'$add_to_path'(Path,Pos).
|
|
|
|
|
2015-09-21 23:05:36 +01:00
|
|
|
'$add_to_path'(New,_) :-
|
|
|
|
recorded('$path',New,R),
|
|
|
|
erase(R),
|
|
|
|
fail.
|
|
|
|
'$add_to_path'(New,last) :-
|
|
|
|
!,
|
|
|
|
recordz('$path',New,_).
|
|
|
|
'$add_to_path'(New,first) :-
|
|
|
|
recorda('$path',New,_).
|
2014-04-06 17:05:17 +01:00
|
|
|
|
2014-07-17 18:19:38 +01:00
|
|
|
/** @pred remove_from_path(+Directory:atom) is det,deprecated
|
2014-04-06 17:05:17 +01:00
|
|
|
|
|
|
|
*/
|
|
|
|
remove_from_path(New) :- '$check_path'(New,Path),
|
|
|
|
recorded('$path',Path,R), erase(R).
|
|
|
|
|
|
|
|
'$check_path'(At,SAt) :- atom(At), !, atom_codes(At,S), '$check_path'(S,SAt).
|
|
|
|
'$check_path'([],[]).
|
|
|
|
'$check_path'([Ch],[Ch]) :- '$dir_separator'(Ch), !.
|
|
|
|
'$check_path'([Ch],[Ch,A]) :- !, integer(Ch), '$dir_separator'(A).
|
|
|
|
'$check_path'([N|S],[N|SN]) :- integer(N), '$check_path'(S,SN).
|
|
|
|
|