View source with raw 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:- if(exists_source(library(desktop))).   46:- autoload(library(desktop), [desktop_open/2]).   47:- endif.   48
   49:- multifile
   50    known_browser/2.

Open a URL in the users browser

This library deals with the highly platform specific task of opening a web page. In addition, is provides a mechanism similar to absolute_file_name/3 that expands compound terms to concrete URLs. For example, the SWI-Prolog home page can be opened using:

?- www_open_url(swipl(.)).
 www_open_url(+Url)
Open URL in running version of the users' browser or start a new browser. This predicate tries the following steps:
  1. If a prolog flag (see set_prolog_flag/2) browser is set and this is the name of a known executable, use this. The flag may be set to Command-Mode, where mode is one of fg or bg, requesting Command to run in foreground or background mode. Default is bg.
  2. Hand the URL to the desktop using desktop_open/2 from library(desktop) if this library is available. On Windows we use win_shell/2 if it is not.
  3. If a environment variable BROWSER is set and this is the name of a known executable, use this.
  4. Try to find a known browser.
   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(desktop_open/2)).   95open_url(URL) :-                        % let the desktop open the URL
   96    catch(desktop_open(URL, []), error(_,_), fail),
   97    !.
   98:- endif.   99:- if(current_predicate(win_shell/2)).  100open_url(URL) :-                        % Windows shell.  Only used if
  101    win_shell(open, URL),               % library(desktop) is not available
  102    !.
  103:- endif.  104open_url(URL) :-                        % user configured
  105    getenv('BROWSER', Browser),
  106    has_command(Browser),
  107    !,
  108    run_browser(Browser, URL).
  109open_url(URL) :-                        % something we know
  110    known_browser(Browser, _),
  111    has_command(Browser),
  112    !,
  113    run_browser(Browser, URL).
  114
  115expand_browser_flag(Command-Mode, Command, Mode) :- !.
  116expand_browser_flag(Command, Command, bg) :- atomic(Command).
 run_browser(+Browser, +URL) is det
Open a page using a browser.
  122run_browser(Browser, URL) :-
  123    run_command(Browser, [URL], bg).
 run_command(+Command, +Args, +Background)
Run OS command Command using Args, silencing the error output because many browsers are rather verbose.
  130:- if(current_predicate(process_create/3)).  131run_command(Command, Args, fg) :-
  132    !,
  133    process_create(path(Command), Args, [stderr(null)]).
  134:- endif.  135:- if(current_prolog_flag(unix, true)).  136run_command(Command, [Arg], fg) :-
  137    format(string(Cmd), "\"~w\" \"~w\" &> /dev/null", [Command, Arg]),
  138    shell(Cmd).
  139run_command(Command, [Arg], bg) :-
  140    format(string(Cmd), "\"~w\" \"~w\" &> /dev/null &", [Command, Arg]),
  141    shell(Cmd).
  142:- else.  143run_command(Command, [Arg], fg) :-
  144    format(string(Cmd), "\"~w\" \"~w\"", [Command, Arg]),
  145    shell(Cmd).
  146run_command(Command, [Arg], bg) :-
  147    format(string(Cmd), "\"~w\" \"~w\" &", [Command, Arg]),
  148    shell(Cmd).
  149:- endif.
 known_browser(+FileBaseName, -Compatible)
True if browser FileBaseName has a remote protocol compatible to Compatible.
  156known_browser(firefox,   netscape).
  157known_browser(mozilla,   netscape).
  158known_browser(netscape,  netscape).
  159known_browser(konqueror, -).
  160known_browser(opera,     -).
 has_command(+Command)
Succeeds if Command is in $PATH. Works for Unix systems. For Windows we have to test for executable extensions.
  168:- dynamic
  169    command_cache/2.  170:- volatile
  171    command_cache/2.  172
  173has_command(Command) :-
  174    command_cache(Command, Path),
  175    !,
  176    Path \== (-).
  177has_command(Command) :-
  178    (   getenv('PATH', Path),
  179        (   current_prolog_flag(windows, true)
  180        ->  Sep = (;)
  181        ;   Sep = (:)
  182        ),
  183        atomic_list_concat(Parts, Sep, Path),
  184        member(Part, Parts),
  185        prolog_to_os_filename(PlPart, Part),
  186        atomic_list_concat([PlPart, Command], /, Exe),
  187        access_file(Exe, execute)
  188    ->  assert(command_cache(Command, Exe))
  189    ;   assert(command_cache(Command, -)),
  190        fail
  191    ).
  192
  193
  194                 /*******************************
  195                 *            NET PATHS         *
  196                 *******************************/
 url_path(+Alias, -Expansion) is nondet
Define URL path aliases. This multifile predicate is defined in module user. Expansion is either a URL, or a term Alias(Sub).
  203:- multifile
  204    user:url_path/2.  205
  206user:url_path(swipl,          'http://www.swi-prolog.org').
  207user:url_path(swipl_book,     'http://books.google.nl/books/about/\c
  208                               SWI_Prolog_Reference_Manual_6_2_2.html?\c
  209                               id=q6R3Q3B-VC4C&redir_esc=y').
  210
  211user:url_path(swipl_faq,      swipl('FAQ')).
  212user:url_path(swipl_man,      swipl('pldoc/doc_for?object=manual')).
  213user:url_path(swipl_mail,     swipl('Mailinglist.html')).
  214user:url_path(swipl_download, swipl('Download.html')).
  215user:url_path(swipl_pack,     swipl('pack/list')).
  216user:url_path(swipl_bugs,     swipl('bug.html')).
  217user:url_path(swipl_quick,    swipl('man/quickstart.html')).
 expand_url_path(+Spec, -URL)
Expand URL specifications similar to absolute_file_name/3. The predicate url_path/2 plays the role of file_search_path/2.
Errors
- existence_error(url_path, Spec) if the location is not defined.
  227expand_url_path(URL, URL) :-
  228    atomic(URL),
  229    !.                 % Allow atom and string
  230expand_url_path(Spec, URL) :-
  231    Spec =.. [Path, Local],
  232    (   user:url_path(Path, Spec2)
  233    ->  expand_url_path(Spec2, URL0),
  234        (   Local == '.'
  235        ->  URL = URL0
  236        ;   sub_atom(Local, 0, _, _, #)
  237        ->  atom_concat(URL0, Local, URL)
  238        ;   sub_atom(URL0, 0, _, _, /)
  239        ->  absolute_file_name(Local, URL, [relative_to(URL0)])
  240        ;   atomic_list_concat([URL0, Local], /, URL)
  241        )
  242    ;   throw(error(existence_error(url_path, Path), expand_url_path/2))
  243    )