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)  2019, VU University Amsterdam
    7			 CWI, 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(gpp, []).   37:- use_module(library(error)).

Use XSB gpp preprocessor

This library enables using the XSB gpp preprocessor on .P files. Currently the gpp program is not provided with SWI-Prolog. If you need this compatibility get it from XSB. The XSB sources provide gpp.c, a simple stand alone C file that can be compiled with any C compiler. The gpp program must be installed in a directory accessible through the PATH environment variable. The preprocessor is enabled using

:- use_module(library(dialect/xsb/gpp)).

Running code through gpp can be useful for several reasons:

There are also good reasons not to use a preprocessor:

   74:- multifile
   75    prolog:open_source_hook/3,
   76    prolog:gpp_options/1.   77:- dynamic
   78    prolog:gpp_options/1.
 prolog:open_source_hook(+Path, -Stream, +Options)
Implementation of the open source hook to use the XSB gpp preprocessor on .P files. This requires gpp on the PATH environment variable.
   86prolog:open_source_hook(Path, Stream, _Options) :-
   87    file_name_extension(_, 'P', Path),
   88    gpp_command(Path, Command),
   89    open(pipe(Command), read, Stream),
   90    set_stream(Stream, file_name(Path)).
   91
   92gpp_command(Path, Command) :-
   93    gpp_executable(Gpp),
   94    quoted_os_path(Gpp, OSGpp),
   95    quoted_os_path(Path, OSPath),
   96    gpp_option_string(Options),
   97    format(string(Command), '~w ~w ~w', [OSGpp, Options, OSPath]).
   98
   99quoted_os_path(PrologPath, QOSPath) :-
  100    prolog_to_os_filename(PrologPath, OSPath),
  101    (   \+ sub_atom(OSPath, _, _, _, '"'),
  102        \+ sub_atom(OSPath, _, _, _, '$')
  103    ->  format(string(QOSPath), '"~w"', [OSPath])
  104    ;   \+ sub_atom(OSPath, _, _, _, '\'')
  105    ->  format(string(QOSPath), '\'~w\'', [OSPath])
  106    ;   representation_error(path)
  107    ).
  108
  109gpp_executable(Gpp) :-
  110    exe_options(ExeOptions),
  111    absolute_file_name(path(gpp), Gpp, ExeOptions).
  112
  113exe_options(Options) :-
  114    current_prolog_flag(windows, true),
  115    !,
  116    Options = [ extensions(['',exe,com]), access(read) ].
  117exe_options(Options) :-
  118    Options = [ access(execute) ].
  119
  120gpp_option_string(String) :-
  121    prolog:gpp_options(String),
  122    !.
  123gpp_option_string(String) :-
  124    findall(Opt, gpp_option(Opt), Opts),
  125    atomics_to_string(Opts, " ", String).
  126
  127gpp_option('-P').
  128gpp_option('-m').
  129gpp_option('-nostdinc').
  130%gpp_option('-nocurinc').
  131gpp_option('-DSWI_PROLOG').
 prolog:gpp_options(-Options)
This multifile dynamic hook can be used to specify options for the gpp preprocessor. The default options are
-P -m -nostdinc -DSWI_PROLOG