- Documentation
- Reference manual
- Foreign Language Interface
- The Foreign Include File
- Argument Passing and Control
- Atoms and functors
- Analysing Terms via the Foreign Interface
- Constructing Terms
- Unifying data
- Convenient functions to generate Prolog exceptions
- Foreign language wrapper support functions
- Serializing and deserializing Prolog terms
- BLOBS: Using atoms to store arbitrary binary data
- Exchanging GMP numbers
- Calling Prolog from C
- Discarding Data
- String buffering
- Foreign Code and Modules
- Prolog exceptions in foreign code
- Catching Signals (Software Interrupts)
- Miscellaneous
- Errors and warnings
- Environment Control from Foreign Code
- Querying Prolog
- Registering Foreign Predicates
- Foreign Code Hooks
- Storing foreign data
- Embedding SWI-Prolog in other applications
- The Foreign Include File
- Foreign Language Interface
- Packages
- Reference manual
foreign_t pl_hello(term_t to) { char *s; if ( PL_get_atom_chars(to, &s) ) { return Sfprintf(Scurrent_output, "Hello \"%s\"\n", s); } else { term_t except; return ( (except=PL_new_term_ref()) && PL_unify_term(except, PL_FUNCTOR_CHARS, "type_error", 2, PL_CHARS, "atom", PL_TERM, to) && PL_raise_exception(except) ); } }
For reference, the preferred implementation of the above is below.
The
CVT_EXCEPTION
tells the system to generate an exception if
the conversion fails. The other CVT_
flags define the
admissible types and REP_MB
requests the string to be
provided in the current locale representation. This implies
that Unicode text is printed correctly if the current environment can
represent it. If not, a representation_error
is raised.
foreign_t pl_hello(term_t to) { char *s; if ( PL_get_chars(to, &s, CVT_ATOM|CVT_STRING|CVT_EXCEPTION|REP_MB) ) { return Sfprintf(Scurrent_output, "Hello \"%s\"\n", s); } return FALSE; }
resource_error
exceptions. Some return type_error
or domain_error
exceptions. A call to Prolog using
PL_next_solution()
may return any exception, including those thrown by explicit calls to throw/1.
If no exception is pending this function returns (term_t)0
.
Normally qid should be 0
. An explicit qid
must be used after a call to PL_next_solution()
that returns FALSE
when the query was created using the PL_Q_PASS_EXCEPTION
flag (see PL_open_query()).
Note that an API may only raise an exception when it fails; if the
API call succeeds, the result of PL_exception(0)
will be 0.228Provided no exception
was pending before calling the API function. As clients must deal with
exceptions immediately after an API call raises one, this can not happen
in a well behaved client. The implementation of a foreign
predicate should normally cleanup and return
FALSE
after an exception is raised (and typically also
after an API call failed for logical reasons; see PL_unify()
for an elaboration on this topic). If the call to Prolog is not the
implementation of a foreign predicate, e.g., when the overall process
control is in some other language, exceptions may be printed by calling print_message/2
and should be discarded by calling PL_clear_exception().
12.4.17 Catching Signals (Software Interrupts)
SWI-Prolog offers both a C and Prolog interface to deal with software interrupts (signals). The Prolog mapping is defined in section 4.12. This subsection deals with handling signals from C.
If a signal is not used by Prolog and the handler does not call Prolog in any way, the native signal interface routines may be used.
Any handler that wishes to call one of the Prolog interface functions should call PL_sigaction() to install the handler. PL_signal() provides a deprecated interface that is notably not capable of properly restoring the old signal status if the signal was previously handled by Prolog.