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:- autoload(library(lists),[member/2]).   41
   42:- if(exists_source(library(process))).   43:- autoload(library(process), [process_create/3]).   44:- endif.   45
   46:- multifile
   47    known_browser/2.   48
   49/** <module> Open a URL in the users browser
   50
   51This library deals with the highly platform   specific task of opening a
   52web  page.  In  addition,   is   provides    a   mechanism   similar  to
   53absolute_file_name/3 that expands compound terms   to concrete URLs. For
   54example, the SWI-Prolog home page can be opened using:
   55
   56  ==
   57  ?- www_open_url(swipl(.)).
   58  ==
   59*/
   60
   61%!  www_open_url(+Url)
   62%
   63%   Open URL in running version of the users' browser or start a new
   64%   browser.  This predicate tries the following steps:
   65%
   66%     1. If a prolog flag (see set_prolog_flag/2) =browser= is set
   67%     and this is the name of a known executable, use this.  The
   68%     flag may be set to `Command-Mode`, where mode is one of `fg`
   69%     or `bg`, requesting Command to run in foreground or background
   70%     mode.  Default is `bg`.
   71%
   72%     2. On Windows, use win_shell(open, URL)
   73%
   74%     3. Find a generic `open' comment.  Candidates are =xdg-open=,
   75%     =open= or =|gnome-open|=.
   76%
   77%     4. If a environment variable =BROWSER= is set
   78%     and this is the name of a known executable, use this.
   79%
   80%     5. Try to find a known browser.
   81%
   82%     @tbd  Figure out the right tool in step 3 as it is not
   83%           uncommon that multiple are installed.
   84
   85www_open_url(Spec) :-                   % user configured
   86    expand_url_path(Spec, URL),
   87    open_url(URL).
   88
   89open_url(URL) :-
   90    current_prolog_flag(browser, Browser),
   91    expand_browser_flag(Browser, Command, Mode),
   92    has_command(Command),
   93    !,
   94    run_command(Command, [URL], Mode).
   95:- if(current_predicate(win_shell/2)).   96open_url(URL) :-                        % Windows shell
   97    win_shell(open, URL),
   98    !.
   99:- endif.  100open_url(URL) :-                        % Unix `open document'
  101    open_command(Open),
  102    has_command(Open),
  103    !,
  104    run_command(Open, [URL], fg).
  105open_url(URL) :-                        % user configured
  106    getenv('BROWSER', Browser),
  107    has_command(Browser),
  108    !,
  109    run_browser(Browser, URL).
  110open_url(URL) :-                        % something we know
  111    known_browser(Browser, _),
  112    has_command(Browser),
  113    !,
  114    run_browser(Browser, URL).
  115
  116expand_browser_flag(Command-Mode, Command, Mode) :- !.
  117expand_browser_flag(Command, Command, bg) :- atomic(Command).
  118
  119open_command(open) :-                   % Apples open command
  120    current_prolog_flag(apple, true).
  121open_command('xdg-open').               % Free desktop
  122open_command('gnome-open').             % Gnome (deprecated in favour of xdg-open
  123open_command(open).                     % Who knows
  124
  125%!  run_browser(+Browser, +URL) is det.
  126%
  127%   Open a page using a browser.
  128
  129run_browser(Browser, URL) :-
  130    run_command(Browser, [URL], bg).
  131
  132%!  run_command(+Command, +Args, +Background)
  133%
  134%   Run OS command Command using Args,   silencing  the error output
  135%   because many browsers are rather verbose.
  136
  137:- if(current_predicate(process_create/3)).  138run_command(Command, Args, fg) :-
  139    !,
  140    process_create(path(Command), Args, [stderr(null)]).
  141:- endif.  142:- if(current_prolog_flag(unix, true)).  143run_command(Command, [Arg], fg) :-
  144    format(string(Cmd), "\"~w\" \"~w\" &> /dev/null", [Command, Arg]),
  145    shell(Cmd).
  146run_command(Command, [Arg], bg) :-
  147    format(string(Cmd), "\"~w\" \"~w\" &> /dev/null &", [Command, Arg]),
  148    shell(Cmd).
  149:- else.  150run_command(Command, [Arg], fg) :-
  151    format(string(Cmd), "\"~w\" \"~w\"", [Command, Arg]),
  152    shell(Cmd).
  153run_command(Command, [Arg], bg) :-
  154    format(string(Cmd), "\"~w\" \"~w\" &", [Command, Arg]),
  155    shell(Cmd).
  156:- endif.  157
  158%!  known_browser(+FileBaseName, -Compatible)
  159%
  160%   True if browser FileBaseName has a remote protocol compatible to
  161%   Compatible.
  162
  163known_browser(firefox,   netscape).
  164known_browser(mozilla,   netscape).
  165known_browser(netscape,  netscape).
  166known_browser(konqueror, -).
  167known_browser(opera,     -).
  168
  169
  170%!  has_command(+Command)
  171%
  172%   Succeeds if Command is in  $PATH.   Works  for Unix systems. For
  173%   Windows we have to test for executable extensions.
  174
  175:- dynamic
  176    command_cache/2.  177:- volatile
  178    command_cache/2.  179
  180has_command(Command) :-
  181    command_cache(Command, Path),
  182    !,
  183    Path \== (-).
  184has_command(Command) :-
  185    (   getenv('PATH', Path),
  186        (   current_prolog_flag(windows, true)
  187        ->  Sep = (;)
  188        ;   Sep = (:)
  189        ),
  190        atomic_list_concat(Parts, Sep, Path),
  191        member(Part, Parts),
  192        prolog_to_os_filename(PlPart, Part),
  193        atomic_list_concat([PlPart, Command], /, Exe),
  194        access_file(Exe, execute)
  195    ->  assert(command_cache(Command, Exe))
  196    ;   assert(command_cache(Command, -)),
  197        fail
  198    ).
  199
  200
  201                 /*******************************
  202                 *            NET PATHS         *
  203                 *******************************/
  204
  205%!  url_path(+Alias, -Expansion) is nondet.
  206%
  207%   Define URL path aliases. This multifile  predicate is defined in
  208%   module =user=. Expansion is either a URL, or a term Alias(Sub).
  209
  210:- multifile
  211    user:url_path/2.  212
  213user:url_path(swipl,          'http://www.swi-prolog.org').
  214user:url_path(swipl_book,     'http://books.google.nl/books/about/\c
  215                               SWI_Prolog_Reference_Manual_6_2_2.html?\c
  216                               id=q6R3Q3B-VC4C&redir_esc=y').
  217
  218user:url_path(swipl_faq,      swipl('FAQ')).
  219user:url_path(swipl_man,      swipl('pldoc/doc_for?object=manual')).
  220user:url_path(swipl_mail,     swipl('Mailinglist.html')).
  221user:url_path(swipl_download, swipl('Download.html')).
  222user:url_path(swipl_pack,     swipl('pack/list')).
  223user:url_path(swipl_bugs,     swipl('bug.html')).
  224user:url_path(swipl_quick,    swipl('man/quickstart.html')).
  225
  226%!  expand_url_path(+Spec, -URL)
  227%
  228%   Expand URL specifications similar   to absolute_file_name/3. The
  229%   predicate url_path/2 plays the role of file_search_path/2.
  230%
  231%   @error  existence_error(url_path, Spec) if the location is not
  232%           defined.
  233
  234expand_url_path(URL, URL) :-
  235    atomic(URL),
  236    !.                 % Allow atom and string
  237expand_url_path(Spec, URL) :-
  238    Spec =.. [Path, Local],
  239    (   user:url_path(Path, Spec2)
  240    ->  expand_url_path(Spec2, URL0),
  241        (   Local == '.'
  242        ->  URL = URL0
  243        ;   sub_atom(Local, 0, _, _, #)
  244        ->  atom_concat(URL0, Local, URL)
  245        ;   sub_atom(URL0, 0, _, _, /)
  246        ->  absolute_file_name(Local, URL, [relative_to(URL0)])
  247        ;   atomic_list_concat([URL0, Local], /, URL)
  248        )
  249    ;   throw(error(existence_error(url_path, Path), expand_url_path/2))
  250    )