View source with formatted comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2006-2015, University of Amsterdam
    7                              VU University Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(www_browser,
   37          [ www_open_url/1,             % +UrlOrSpec
   38            expand_url_path/2           % +Spec, -URL
   39          ]).   40:- use_module(library(lists)).   41:- if(exists_source(library(process))).   42:- use_module(library(process)).   43:- endif.   44
   45:- multifile
   46    known_browser/2.   47
   48/** <module> Open a URL in the users browser
   49
   50This library deals with the highly platform   specific task of opening a
   51web  page.  In  addition,   is   provides    a   mechanism   similar  to
   52absolute_file_name/3 that expands compound terms   to concrete URLs. For
   53example, the SWI-Prolog home page can be opened using:
   54
   55  ==
   56  ?- www_open_url(swipl(.)).
   57  ==
   58*/
   59
   60%!  www_open_url(+Url)
   61%
   62%   Open URL in running version of the users' browser or start a new
   63%   browser.  This predicate tries the following steps:
   64%
   65%     1. If a prolog flag (see set_prolog_flag/2) =browser= is set
   66%     and this is the name of a known executable, use this.  The
   67%     flag may be set to `Command-Mode`, where mode is one of `fg`
   68%     or `bg`, requesting Command to run in foreground or background
   69%     mode.  Default is `bg`.
   70%
   71%     2. On Windows, use win_shell(open, URL)
   72%
   73%     3. Find a generic `open' comment.  Candidates are =xdg-open=,
   74%     =open= or =|gnome-open|=.
   75%
   76%     4. If a environment variable =BROWSER= is set
   77%     and this is the name of a known executable, use this.
   78%
   79%     5. Try to find a known browser.
   80%
   81%     @tbd  Figure out the right tool in step 3 as it is not
   82%           uncommon that multiple are installed.
   83
   84www_open_url(Spec) :-                   % user configured
   85    expand_url_path(Spec, URL),
   86    open_url(URL).
   87
   88open_url(URL) :-
   89    current_prolog_flag(browser, Browser),
   90    expand_browser_flag(Browser, Command, Mode),
   91    has_command(Command),
   92    !,
   93    run_command(Command, [URL], Mode).
   94:- if(current_predicate(win_shell/2)).   95open_url(URL) :-                        % Windows shell
   96    win_shell(open, URL).
   97:- endif.   98open_url(URL) :-                        % Unix `open document'
   99    open_command(Open),
  100    has_command(Open),
  101    !,
  102    run_command(Open, [URL], fg).
  103open_url(URL) :-                        % user configured
  104    getenv('BROWSER', Browser),
  105    has_command(Browser),
  106    !,
  107    run_browser(Browser, URL).
  108open_url(URL) :-                        % something we know
  109    known_browser(Browser, _),
  110    has_command(Browser),
  111    !,
  112    run_browser(Browser, URL).
  113
  114expand_browser_flag(Command-Mode, Command, Mode) :- !.
  115expand_browser_flag(Command, Command, bg) :- atomic(Command).
  116
  117open_command(open) :-                   % Apples open command
  118    current_prolog_flag(apple, true).
  119open_command('xdg-open').               % Free desktop
  120open_command('gnome-open').             % Gnome (deprecated in favour of xdg-open
  121open_command(open).                     % Who knows
  122
  123%!  run_browser(+Browser, +URL) is det.
  124%
  125%   Open a page using a browser.
  126
  127run_browser(Browser, URL) :-
  128    run_command(Browser, [URL], bg).
  129
  130%!  run_command(+Command, +Args, +Background)
  131%
  132%   Run OS command Command using Args,   silencing  the error output
  133%   because many browsers are rather verbose.
  134
  135:- if(current_predicate(process_create/3)).  136run_command(Command, Args, fg) :-
  137    !,
  138    process_create(path(Command), Args, [stderr(null)]).
  139:- endif.  140:- if(current_prolog_flag(unix, true)).  141run_command(Command, [Arg], fg) :-
  142    format(string(Cmd), "\"~w\" \"~w\" &> /dev/null", [Command, Arg]),
  143    shell(Cmd).
  144run_command(Command, [Arg], bg) :-
  145    format(string(Cmd), "\"~w\" \"~w\" &> /dev/null &", [Command, Arg]),
  146    shell(Cmd).
  147:- else.  148run_command(Command, [Arg], fg) :-
  149    format(string(Cmd), "\"~w\" \"~w\"", [Command, Arg]),
  150    shell(Cmd).
  151run_command(Command, [Arg], bg) :-
  152    format(string(Cmd), "\"~w\" \"~w\" &", [Command, Arg]),
  153    shell(Cmd).
  154:- endif.  155
  156%!  known_browser(+FileBaseName, -Compatible)
  157%
  158%   True if browser FileBaseName has a remote protocol compatible to
  159%   Compatible.
  160
  161known_browser(firefox,   netscape).
  162known_browser(mozilla,   netscape).
  163known_browser(netscape,  netscape).
  164known_browser(konqueror, -).
  165known_browser(opera,     -).
  166
  167
  168%!  has_command(+Command)
  169%
  170%   Succeeds if Command is in  $PATH.   Works  for Unix systems. For
  171%   Windows we have to test for executable extensions.
  172
  173:- dynamic
  174    command_cache/2.  175:- volatile
  176    command_cache/2.  177
  178has_command(Command) :-
  179    command_cache(Command, Path),
  180    !,
  181    Path \== (-).
  182has_command(Command) :-
  183    (   getenv('PATH', Path),
  184        (   current_prolog_flag(windows, true)
  185        ->  Sep = (;)
  186        ;   Sep = (:)
  187        ),
  188        atomic_list_concat(Parts, Sep, Path),
  189        member(Part, Parts),
  190        prolog_to_os_filename(PlPart, Part),
  191        atomic_list_concat([PlPart, Command], /, Exe),
  192        access_file(Exe, execute)
  193    ->  assert(command_cache(Command, Exe))
  194    ;   assert(command_cache(Command, -)),
  195        fail
  196    ).
  197
  198
  199                 /*******************************
  200                 *            NET PATHS         *
  201                 *******************************/
  202
  203%!  url_path(+Alias, -Expansion) is nondet.
  204%
  205%   Define URL path aliases. This multifile  predicate is defined in
  206%   module =user=. Expansion is either a URL, or a term Alias(Sub).
  207
  208:- multifile
  209    user:url_path/2.  210
  211user:url_path(swipl,          'http://www.swi-prolog.org').
  212user:url_path(swipl_book,     'http://books.google.nl/books/about/\c
  213                               SWI_Prolog_Reference_Manual_6_2_2.html?\c
  214                               id=q6R3Q3B-VC4C&redir_esc=y').
  215
  216user:url_path(swipl_faq,      swipl('FAQ')).
  217user:url_path(swipl_man,      swipl('pldoc/doc_for?object=manual')).
  218user:url_path(swipl_mail,     swipl('Mailinglist.html')).
  219user:url_path(swipl_download, swipl('Download.html')).
  220user:url_path(swipl_pack,     swipl('pack/list')).
  221user:url_path(swipl_bugs,     swipl('bugzilla/')).
  222user:url_path(swipl_quick,    swipl('man/quickstart.html')).
  223
  224%!  expand_url_path(+Spec, -URL)
  225%
  226%   Expand URL specifications similar   to absolute_file_name/3. The
  227%   predicate url_path/2 plays the role of file_search_path/2.
  228%
  229%   @error  existence_error(url_path, Spec) if the location is not
  230%           defined.
  231
  232expand_url_path(URL, URL) :-
  233    atomic(URL),
  234    !.                 % Allow atom and string
  235expand_url_path(Spec, URL) :-
  236    Spec =.. [Path, Local],
  237    (   user:url_path(Path, Spec2)
  238    ->  expand_url_path(Spec2, URL0),
  239        (   Local == '.'
  240        ->  URL = URL0
  241        ;   sub_atom(Local, 0, _, _, #)
  242        ->  atom_concat(URL0, Local, URL)
  243        ;   atomic_list_concat([URL0, Local], /, URL)
  244        )
  245    ;   throw(error(existence_error(url_path, Path), expand_url_path/2))
  246    )