View source with raw comments or as raw
    1/*  Part of SWISH
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2015-2017, VU University Amsterdam
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(swish_include,
   36          [
   37          ]).   38:- use_module(storage).   39:- use_module(config).   40:- use_module(library(sandbox), []).   41:- use_module(library(debug)).   42:- use_module(library(settings)).   43:- use_module(library(filesex)).   44:- use_module(library(error)).   45:- use_module(library(readutil)).

Support :- include(File) from SWISH

This module allows SWISH programs to include other programs from the shared gitty store. It realises this using the following steps:

We allow for hierarchical and circular includes. */

   62swish:term_expansion(:- include(FileIn), Expansion) :-
   63    include_file_id(FileIn, File),
   64    (   prolog_load_context(module, Module),
   65        clause(Module:'swish included'(File), true)
   66    ->  Expansion = []
   67    ;   Expansion = [ (:- discontiguous('swish included'/1)),
   68                      'swish included'(File),
   69                      (:- include(stream(URI, Stream, [close(true)])))
   70                    ],
   71        '$push_input_context'(swish_include),
   72        include_data(File, URI, Data),
   73        open_string(Data, Stream),
   74        '$pop_input_context'
   75    ).
 include_data(+FileSpec, -URI, -Data)
Fetch the data to be included and obtain the URI for it.
   81include_data(Name, URI, Data) :-        % Deal with gitty files
   82    atom(Name),
   83    !,
   84    add_extension(Name, FileExt),
   85    catch(storage_file(FileExt, Data, _Meta),
   86          error(existence_error(_,_),_),
   87          fail),
   88    atom_concat('swish://', FileExt, URI).
   89include_data(Spec, URI, Data) :-
   90    absolute_file_name(Spec, Path, [ file_type(prolog), access(read) ]),
   91    read_file_to_string(Path, Data, []),
   92    Spec =.. [Alias,_],
   93    file_base_name(Path, NameExt),
   94    format(atom(URI), 'swish://~w/~w', [Alias, NameExt]).
 include_file_id(+FileIn, -File) is det
Normalise an include file identifier and verify its safeness.
  101include_file_id(FileIn, File) :-
  102    atomic(FileIn),
  103    !,
  104    atom_string(File, FileIn).
  105include_file_id(FileIn, File) :-
  106    compound(FileIn),
  107    FileIn =.. [Alias,NameIn],
  108    atom_string(Name, NameIn),
  109    (   safe_name(Name),
  110        swish_config(include_alias, Alias)
  111    ->  true
  112    ;   permission_error(include, file, Name)
  113    ),
  114    File =.. [Alias,Name].
  115
  116safe_name(Name) :-
  117    \+ (   sub_atom(Name, 0, _, _, '../')
  118       ;   sub_atom(Name, _, _, _, '/../')
  119       ;   sub_atom(Name, _, _, 0, '/..')
  120       ;   Name == '..'
  121       ).
 file_alias(+File, -Spec) is semidet
Translate Alias/Name into Alias(Name) if Alias is known and Name is safe.
  128file_alias(File, Spec) :-
  129    atomic_list_concat([Alias,Name], /, File),
  130    swish_config(include_alias, Alias),
  131    safe_name(Name),
  132    !,
  133    Spec =.. [Alias,Name].
 add_extension(+File, -FileExt) is det
Add a file name extension to indicate this is a Prolog file.
  139add_extension(File, FileExt) :-
  140    file_name_extension(_, Ext, File),
  141    Ext \== '',
  142    !,
  143    FileExt = File.
  144add_extension(Hash, Hash) :-
  145    is_hash(Hash),
  146    !.
  147add_extension(File, FileExt) :-
  148    file_name_extension(File, pl, FileExt).
  149
  150is_hash(Name) :-
  151    atom_length(Name, 40),
  152    split_string(Name, ":", "0123456789abcdef", [""]).
  153
  154
  155                 /*******************************
  156                 *            SANDBOX           *
  157                 *******************************/
  158
  159:- multifile
  160    sandbox:safe_directive/1.  161
  162sandbox:safe_directive(M:include(stream(Id, Stream, [close(true)]))) :-
  163    is_stream(Stream),
  164    sub_atom(Id, 0, _, _, 'swish://'),
  165    prolog_load_context(module, M).
  166
  167
  168                 /*******************************
  169                 *            COLOUR            *
  170                 *******************************/
  171
  172:- multifile
  173    prolog_colour:term_colours/2.  174
  175prolog_colour:term_colours((:- include(FileIn)),
  176                           neck(directive) -
  177                           [ goal(built_in,include(FileIn)) -
  178                             [ FileClass
  179                             ]
  180                           ]) :-
  181    debug(include, 'Classifying ~p', [FileIn]),
  182    (   catch(include_file_id(FileIn, File), _, fail)
  183    ->  classify_include(File, FileClass)
  184    ;   FileClass = nofile
  185    ),
  186    debug(include, 'Class ~p', [FileClass]).
  187
  188classify_include(File, FileClass) :-
  189    atom(File),
  190    !,
  191    add_extension(File, FileExt),
  192    catch(storage_meta_data(FileExt, _Meta), _, fail),
  193    atom_concat('swish://', FileExt, Id),
  194    FileClass = file(Id).
  195classify_include(Spec, FileClass) :-
  196    absolute_file_name(Spec, Path, [ file_type(prolog), access(read) ]),
  197    Spec =.. [Alias,_],
  198    file_base_name(Path, NameExt),
  199    format(atom(URI), 'swish://~w/~w', [Alias, NameExt]),
  200    FileClass = file(URI).
  201
  202
  203                 /*******************************
  204                 *            XREF              *
  205                 *******************************/
  206
  207:- multifile
  208    prolog:xref_open_source/2,
  209    prolog:xref_source_file/3,
  210    prolog:xref_source_identifier/2,
  211    prolog:xref_source_time/2.
 prolog:xref_source_identifier(+Src, -Id) is semidet
prolog:xref_open_source(+File, -Stream) is det
prolog:xref_source_time(+File, -Modified) is det
Map swish://file to a file from the gitty store.
  219prolog:xref_source_identifier(Src, Id) :-
  220    atom(Src),
  221    sub_atom(Src, 0, _, _, 'swish://'),
  222    !,
  223    Id = Src.
  224
  225prolog:xref_open_source(File, Stream) :-
  226    atom(File),
  227    atom_concat('swish://', Name, File),
  228    (   file_alias(File, Spec)
  229    ->  absolute_file_name(Spec, Path, [ file_type(prolog), access(read) ]),
  230        open(Path, read, Stream)
  231    ;   catch(storage_file(Name, Data, _Meta), _, fail),
  232        open_string(Data, Stream)
  233    ).
  234
  235prolog:xref_source_time(File, Modified) :-
  236    atom(File),
  237    atom_concat('swish://', Name, File),
  238    (   file_alias(File, Spec)
  239    ->  absolute_file_name(Spec, Path, [ file_type(prolog), access(read) ]),
  240        time_file(Path, Modified)
  241    ;   catch(storage_meta_data(Name, Meta), _, fail),
  242        Modified = Meta.get(time)
  243    ).
 prolog:xref_source_file(+Term, -Path, +Options)
Deal with the above expansion for :- include(program) to support the cross-referencer.
  250prolog:xref_source_file(stream(Id, _Stream, [close(true)]), Id, _).
  251prolog:xref_source_file(File, Id, Options) :-
  252    atom(File),
  253    option(relative_to(Src), Options),
  254    atom(Src),
  255    atom_concat('swish://', SrcFile, Src),
  256    add_extension(File, FileExt),
  257    file_directory_name(SrcFile, SrcDir),
  258    directory_file_path(SrcDir, FileExt, TargetFile),
  259    atom_concat('swish://', TargetFile, Id)