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)  2008-2020, 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 #ifndef _FLI_H_INCLUDED
37 #define _FLI_H_INCLUDED
38 
39 #ifndef __SWI_PROLOG__	/* use this to switch on Prolog dialect */
40 #define __SWI_PROLOG__	/* normally defined by the swipl-ld compiler driver */
41 #endif
42 
43 #ifndef __WINDOWS__
44 #if defined(_MSC_VER) || defined(__MINGW32__)
45 #define __WINDOWS__ 1
46 #endif
47 #endif
48 
49 #include <stdarg.h>
50 #include <stdlib.h>			/* get size_t */
51 #include <stddef.h>
52 #ifdef _MSC_VER
53 typedef __int64 int64_t;
54 typedef unsigned __int64 uint64_t;
55 #if (_MSC_VER < 1300)
56 typedef long intptr_t;
57 typedef unsigned long uintptr_t;
58 #endif
59 #else
60 #include <inttypes.h>			/* more portable than stdint.h */
61 #endif
62 
63 #ifdef __cplusplus
64 extern "C" {
65 #endif
66 
67 /* PLVERSION: 10000 * <Major> + 100 * <Minor> + <Patch> */
68 /* PLVERSION_TAG: a string, normally "", but for example "rc1" */
69 
70 #ifndef PLVERSION
71 #define PLVERSION 80203
72 #endif
73 #ifndef PLVERSION_TAG
74 #define PLVERSION_TAG ""
75 #endif
76 
77 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
78 This number is incremented when the   SWI-Prolog PL_*() functions or one
79 of the data types is modified such that old binary extensions cannot run
80 reliably with the  current  version.  This   version  is  introduced  in
81 SWI-Prolog 8.1.30. The  most  recent   violation  of  compatibility  was
82 between versions 8.1.21 and 8.1.22  with   the  introduction of rational
83 numbers are atomic type.
84 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
85 
86 #define PL_FLI_VERSION      2		/* PL_*() functions */
87 #define	PL_REC_VERSION      3		/* PL_record_external(), fastrw */
88 #define PL_QLF_LOADVERSION 67		/* load all versions later >= X */
89 #define PL_QLF_VERSION     67		/* save version number */
90 
91 
92 		 /*******************************
93 		 *	       EXPORT		*
94 		 *******************************/
95 
96 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
97 Traditional and ELF-based Unix systems  don't   need  all this, but COFF
98 based systems need  to  import  and   export  symbols  explicitely  from
99 executables and shared objects (DLL). On some systems (e.g. AIX) this is
100 achieved using import/export files, on Windows   this  is achieved using
101 special  declarations  on  exported  symbols.  So,  a  symbol  is  local
102 (static), shared between the objects building   an executable or DLL (no
103 special declaration) or exported from the executable or DLL.
104 
105 Both using native Microsoft MSVC as well   as recent Cygwin (tested 1.1)
106 compilers support __declspec(...) for exporting symbols.
107 
108 As SWI-Prolog.h can be included separately or together with this file we
109 duplicated this stuff.
110 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
111 
112 #ifndef _PL_EXPORT_DONE
113 #define _PL_EXPORT_DONE
114 
115 #if defined(_MSC_VER) || defined(__MINGW32__)
116 #define HAVE_DECLSPEC
117 #endif
118 
119 #ifdef HAVE_DECLSPEC
120 # ifdef PL_KERNEL
121 #define PL_EXPORT(type)		__declspec(dllexport) type
122 #define PL_EXPORT_DATA(type)	__declspec(dllexport) type
123 #define install_t		void
124 # else
125 #  ifdef __BORLANDC__
126 #define PL_EXPORT(type)		type _stdcall
127 #define PL_EXPORT_DATA(type)	extern type
128 #  else
129 #   ifdef __MINGW32__
130 #define PL_EXPORT(type)		extern type
131 #define PL_EXPORT_DATA(type)	extern type
132 #   else
133 #define PL_EXPORT(type)		extern type
134 #define PL_EXPORT_DATA(type)	__declspec(dllimport) type
135 #   endif
136 #  endif
137 #define install_t		__declspec(dllexport) void
138 # endif
139 #else /*HAVE_DECLSPEC*/
140 #define PL_EXPORT(type)		extern type
141 #define PL_EXPORT_DATA(type)	extern type
142 #define install_t		void
143 #endif /*HAVE_DECLSPEC*/
144 #endif /*_PL_EXPORT_DONE*/
145 
146 
147 		 /*******************************
148 		 *	  GCC ATTRIBUTES	*
149 		 *******************************/
150 
151 #if __GNUC__ >= 4
152 #define WUNUSED __attribute__((warn_unused_result))
153 #else
154 #define WUNUSED
155 #endif
156 
157 
158 		 /*******************************
159 		 *	       TYPES		*
160 		 *******************************/
161 
162 #ifdef _PL_INCLUDE_H
163 typedef Module		module_t;	/* a module */
164 typedef Procedure	predicate_t;	/* a predicate handle */
165 typedef Record		record_t;	/* handle to a recorded term */
166 typedef struct PL_local_data *PL_engine_t; /* handle to a engine */
167 #else
168 typedef	uintptr_t	atom_t;		/* Prolog atom */
169 typedef uintptr_t	functor_t;	/* Name/arity pair */
170 typedef void *		module_t;	/* Prolog module */
171 typedef void *		predicate_t;	/* Prolog procedure */
172 typedef void *		record_t;	/* Prolog recorded term */
173 #ifndef PL_HAVE_TERM_T
174 #define PL_HAVE_TERM_T
175 typedef uintptr_t	term_t;		/* opaque term handle */
176 #endif
177 typedef uintptr_t	qid_t;		/* opaque query handle */
178 typedef uintptr_t	PL_fid_t;	/* opaque foreign context handle */
179 typedef void *		control_t;	/* non-deterministic control arg */
180 typedef void *		PL_engine_t;	/* opaque engine handle */
181 #endif
182 typedef uintptr_t	PL_atomic_t;	/* same a word */
183 typedef uintptr_t	foreign_t;	/* return type of foreign functions */
184 typedef wchar_t	        pl_wchar_t;	/* Prolog wide character */
185 #ifdef __cplusplus
186 typedef void *		pl_function_t;      /* pass function as void* */
187 #else
188 typedef foreign_t	(*pl_function_t)(); /* foreign language functions */
189 #endif
190 typedef uintptr_t	buf_mark_t;	/* buffer mark handle */
191 
192 #ifndef NORETURN
193 #define NORETURN
194 #endif
195 
196 #define fid_t PL_fid_t			/* avoid AIX name-clash */
197 
198 					/* values for PL_get_term_value() */
199 typedef union
200 { int64_t i;				/* PL_INTEGER */
201   double f;				/* PL_FLOAT */
202   char * s;				/* PL_STRING */
203   atom_t a;				/* PL_ATOM */
204   struct				/* PL_TERM */
205   { atom_t name;
206     size_t arity;
207   } t;
208 } term_value_t;
209 
210 
211 #ifndef TRUE
212 #define TRUE	(1)
213 #define FALSE	(0)
214 #endif
215 
216 		 /*******************************
217 		 *      TERM-TYPE CONSTANTS	*
218 		 *******************************/
219 					/* PL_unify_term() arguments */
220 #define	PL_VARIABLE	 (1)		/* nothing */
221 #define PL_ATOM		 (2)		/* const char * */
222 #define PL_INTEGER	 (3)		/* int */
223 #define PL_RATIONAL	 (4)		/* rational number */
224 #define PL_FLOAT	 (5)		/* double */
225 #define PL_STRING	 (6)		/* const char * */
226 #define PL_TERM		 (7)
227 
228 #define PL_NIL		 (8)		/* The constant [] */
229 #define PL_BLOB		 (9)		/* non-atom blob */
230 #define PL_LIST_PAIR	 (10)		/* [_|_] term */
231 
232 					/* PL_unify_term() */
233 #define PL_FUNCTOR	 (11)		/* functor_t, arg ... */
234 #define PL_LIST		 (12)		/* length, arg ... */
235 #define PL_CHARS	 (13)		/* const char * */
236 #define PL_POINTER	 (14)		/* void * */
237 					/* PlArg::PlArg(text, type) */
238 #define PL_CODE_LIST	 (15)		/* [ascii...] */
239 #define PL_CHAR_LIST	 (16)		/* [h,e,l,l,o] */
240 #define PL_BOOL		 (17)		/* PL_set_prolog_flag() */
241 #define PL_FUNCTOR_CHARS (18)		/* PL_unify_term() */
242 #define _PL_PREDICATE_INDICATOR (19)	/* predicate_t (Procedure) */
243 #define PL_SHORT	 (20)		/* short */
244 #define PL_INT		 (21)		/* int */
245 #define PL_LONG		 (22)		/* long */
246 #define PL_DOUBLE	 (23)		/* double */
247 #define PL_NCHARS	 (24)		/* size_t, const char * */
248 #define PL_UTF8_CHARS	 (25)		/* const char * */
249 #define PL_UTF8_STRING	 (26)		/* const char * */
250 #define PL_INT64	 (27)		/* int64_t */
251 #define PL_NUTF8_CHARS	 (28)		/* size_t, const char * */
252 #define PL_NUTF8_CODES	 (29)		/* size_t, const char * */
253 #define PL_NUTF8_STRING	 (30)		/* size_t, const char * */
254 #define PL_NWCHARS	 (31)		/* size_t, const wchar_t * */
255 #define PL_NWCODES	 (32)		/* size_t, const wchar_t * */
256 #define PL_NWSTRING	 (33)		/* size_t, const wchar_t * */
257 #define PL_MBCHARS	 (34)		/* const char * */
258 #define PL_MBCODES	 (35)		/* const char * */
259 #define PL_MBSTRING	 (36)		/* const char * */
260 #define PL_INTPTR	 (37)		/* intptr_t */
261 #define PL_CHAR		 (38)		/* int */
262 #define PL_CODE		 (39)		/* int */
263 #define PL_BYTE		 (40)		/* int */
264 					/* PL_skip_list() */
265 #define PL_PARTIAL_LIST	 (41)		/* a partial list */
266 #define PL_CYCLIC_TERM	 (42)		/* a cyclic list/term */
267 #define PL_NOT_A_LIST	 (43)		/* Object is not a list */
268 					/* dicts */
269 #define PL_DICT		 (44)
270 
271 /* Or'ed flags for PL_set_prolog_flag() */
272 /* MUST fit in a short int! */
273 #define FF_READONLY	 0x1000		/* Read-only prolog flag */
274 #define FF_KEEP		 0x2000		/* keep prolog flag if already set */
275 #define FF_NOCREATE	 0x4000		/* Fail if flag is non-existent */
276 #define FF_FORCE	 0x8000		/* Force setting, overwrite READONLY */
277 #define FF_MASK		 0xf000
278 
279 
280 		/********************************
281 		*    DETERMINISTIC CALL/RETURN  *
282 		*********************************/
283 
284 #define	PL_succeed	return TRUE	/* succeed deterministically */
285 #define PL_fail		return FALSE	/* fail */
286 
287 
288 		/********************************
289 		* NON-DETERMINISTIC CALL/RETURN *
290 		*********************************/
291 
292 /*  Note 1: Non-deterministic foreign functions may also use the deterministic
293     return methods PL_succeed and PL_fail.
294 
295     Note 2: The argument to PL_retry is a sizeof(ptr)-2 bits signed
296     integer (use type intptr_t).
297 */
298 
299 #define PL_FIRST_CALL		(0)
300 #define PL_CUTTED		(1)	/* deprecated */
301 #define PL_PRUNED		(1)
302 #define PL_REDO			(2)
303 
304 #define PL_retry(n)		return _PL_retry(n)
305 #define PL_retry_address(a)	return _PL_retry_address(a)
306 
307 PL_EXPORT(foreign_t)	_PL_retry(intptr_t);
308 PL_EXPORT(foreign_t)	_PL_retry_address(void *);
309 PL_EXPORT(int)		PL_foreign_control(control_t);
310 PL_EXPORT(intptr_t)	PL_foreign_context(control_t);
311 PL_EXPORT(void *)	PL_foreign_context_address(control_t);
312 PL_EXPORT(predicate_t)	PL_foreign_context_predicate(control_t);
313 
314 
315 		/********************************
316 		*      REGISTERING FOREIGNS     *
317 		*********************************/
318 
319 typedef struct PL_extension
320 { const char   *predicate_name;		/* Name of the predicate */
321   short		arity;			/* Arity of the predicate */
322   pl_function_t	function;		/* Implementing functions */
323   short		flags;			/* Or of PL_FA_... */
324 } PL_extension;
325 
326 #define PL_FA_NOTRACE		(0x01)	/* foreign cannot be traced */
327 #define PL_FA_TRANSPARENT	(0x02)	/* foreign is module transparent */
328 #define PL_FA_NONDETERMINISTIC	(0x04)	/* foreign is non-deterministic */
329 #define PL_FA_VARARGS		(0x08)	/* call using t0, ac, ctx */
330 #define PL_FA_CREF		(0x10)	/* Internal: has clause-reference */
331 #define PL_FA_ISO		(0x20)	/* Internal: ISO core predicate */
332 #define PL_FA_META		(0x40)	/* Additional meta-argument spec */
333 
334 extern			PL_extension PL_extensions[]; /* not Win32! */
335 PL_EXPORT(void)		PL_register_extensions(const PL_extension *e);
336 PL_EXPORT(void)		PL_register_extensions_in_module(const char *module, const PL_extension *e);
337 PL_EXPORT(int)		PL_register_foreign(const char *name, int arity,
338 					    pl_function_t func,
339 					    int flags, ...);
340 PL_EXPORT(int)		PL_register_foreign_in_module(const char *module,
341 						      const char *name, int arity,
342 						      pl_function_t func,
343 						      int flags, ...);
344 PL_EXPORT(void)		PL_load_extensions(const PL_extension *e);
345 
346 		 /*******************************
347 		 *	      LICENSE		*
348 		 *******************************/
349 
350 PL_EXPORT(void)		PL_license(const char *license, const char *module);
351 
352 		/********************************
353 		*            MODULES            *
354 		*********************************/
355 
356 PL_EXPORT(module_t)	PL_context(void);
357 PL_EXPORT(atom_t)	PL_module_name(module_t module);
358 PL_EXPORT(module_t)	PL_new_module(atom_t name);
359 PL_EXPORT(int)		PL_strip_module(term_t in, module_t *m, term_t out) WUNUSED;
360 
361 		 /*******************************
362 		 *	     CONSTANTS		*
363 		 *******************************/
364 
365 PL_EXPORT(const atom_t) *_PL_atoms(void); /* base of reserved (meta-)atoms */
366 #ifndef PL_KERNEL
367 #define ATOM_nil	(_PL_atoms()[0]) /* `[]` */
368 #define ATOM_dot	(_PL_atoms()[1]) /* `.` */
369 #endif /*PL_KERNEL*/
370 
371 
372 		 /*******************************
373 		 *	     CALL-BACK		*
374 		 *******************************/
375 
376 #ifdef PL_KERNEL
377 #define PL_Q_DEBUG		0x0001	/* = TRUE for backward compatibility */
378 #endif
379 #define PL_Q_NORMAL		0x0002	/* normal usage */
380 #define PL_Q_NODEBUG		0x0004	/* use this one */
381 #define PL_Q_CATCH_EXCEPTION	0x0008	/* handle exceptions in C */
382 #define PL_Q_PASS_EXCEPTION	0x0010	/* pass to parent environment */
383 #define PL_Q_ALLOW_YIELD	0x0020	/* Support I_YIELD */
384 #define PL_Q_EXT_STATUS		0x0040	/* Return extended status */
385 #ifdef PL_KERNEL
386 #define PL_Q_DETERMINISTIC	0x0100	/* call was deterministic */
387 #endif
388 
389 					/* PL_Q_EXT_STATUS return codes */
390 #define PL_S_EXCEPTION	       -1	/* Query raised exception */
391 #define PL_S_FALSE		0	/* Query failed */
392 #define PL_S_TRUE		1	/* Query succeeded with choicepoint */
393 #define PL_S_LAST		2	/* Query succeeded without CP */
394 
395 
396 			/* Foreign context frames */
397 PL_EXPORT(fid_t)	PL_open_foreign_frame(void);
398 PL_EXPORT(void)		PL_rewind_foreign_frame(fid_t cid);
399 PL_EXPORT(void)		PL_close_foreign_frame(fid_t cid);
400 PL_EXPORT(void)		PL_discard_foreign_frame(fid_t cid);
401 
402 			/* Finding predicates */
403 PL_EXPORT(predicate_t)	PL_pred(functor_t f, module_t m);
404 PL_EXPORT(predicate_t)	PL_predicate(const char *name, int arity,
405 				     const char* module);
406 PL_EXPORT(int)		PL_predicate_info(predicate_t pred,
407 					  atom_t *name, size_t *arity,
408 					  module_t *module);
409 
410 			/* Call-back */
411 PL_EXPORT(qid_t)	PL_open_query(module_t m, int flags,
412 				      predicate_t pred, term_t t0);
413 PL_EXPORT(int)		PL_next_solution(qid_t qid) WUNUSED;
414 PL_EXPORT(int)		PL_close_query(qid_t qid);
415 PL_EXPORT(int)		PL_cut_query(qid_t qid);
416 PL_EXPORT(qid_t)	PL_current_query(void);
417 
418 			/* Simplified (but less flexible) call-back */
419 PL_EXPORT(int)		PL_call(term_t t, module_t m);
420 PL_EXPORT(int)		PL_call_predicate(module_t m, int debug,
421 					  predicate_t pred, term_t t0);
422 			/* Handling exceptions */
423 PL_EXPORT(term_t)	PL_exception(qid_t qid);
424 PL_EXPORT(int)		PL_raise_exception(term_t exception);
425 PL_EXPORT(int)		PL_throw(term_t exception);
426 PL_EXPORT(void)		PL_clear_exception(void);
427 			/* Engine-based coroutining */
428 PL_EXPORT(term_t)	PL_yielded(qid_t qid);
429 
430 
431 		 /*******************************
432 		 *	      ASSERT		*
433 		 *******************************/
434 
435 #define PL_ASSERTZ		0x0000
436 #define PL_ASSERTA		0x0001
437 #define PL_CREATE_THREAD_LOCAL	0x0010
438 #define PL_CREATE_INCREMENTAL	0x0020
439 
440 PL_EXPORT(int)		PL_assert(term_t term, module_t m, int flags);
441 
442 
443 
444 
445 		 /*******************************
446 		 *        TERM-REFERENCES	*
447 		 *******************************/
448 
449 			/* Creating and destroying term-refs */
450 PL_EXPORT(term_t)	PL_new_term_refs(int n);
451 PL_EXPORT(term_t)	PL_new_term_ref(void);
452 PL_EXPORT(term_t)	PL_copy_term_ref(term_t from);
453 PL_EXPORT(void)		PL_reset_term_refs(term_t r);
454 
455 			/* Constants */
456 PL_EXPORT(atom_t)	PL_new_atom(const char *s);
457 PL_EXPORT(atom_t)	PL_new_atom_nchars(size_t len, const char *s);
458 PL_EXPORT(atom_t)	PL_new_atom_wchars(size_t len, const pl_wchar_t *s);
459 PL_EXPORT(atom_t)	PL_new_atom_mbchars(int rep, size_t len, const char *s);
460 PL_EXPORT(const char *)	PL_atom_chars(atom_t a);
461 PL_EXPORT(const char *)	PL_atom_nchars(atom_t a, size_t *len);
462 PL_EXPORT(const wchar_t *)	PL_atom_wchars(atom_t a, size_t *len);
463 #ifdef O_DEBUG_ATOMGC
464 #define PL_register_atom(a) \
465 	_PL_debug_register_atom(a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
466 #define PL_unregister_atom(a) \
467 	_PL_debug_unregister_atom(a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
468 PL_EXPORT(void)		_PL_debug_register_atom(atom_t a,
469 						const char *file, int line,
470 						const char *func);
471 PL_EXPORT(void)		_PL_debug_unregister_atom(atom_t a,
472 						  const char *file, int line,
473 						  const char *func);
474 #else
475 PL_EXPORT(void)		PL_register_atom(atom_t a);
476 PL_EXPORT(void)		PL_unregister_atom(atom_t a);
477 #endif
478 PL_EXPORT(functor_t)	PL_new_functor_sz(atom_t f, size_t a);
479 PL_EXPORT(functor_t)	PL_new_functor(atom_t f, int a);
480 PL_EXPORT(atom_t)	PL_functor_name(functor_t f);
481 PL_EXPORT(int)		PL_functor_arity(functor_t f);
482 PL_EXPORT(size_t)	PL_functor_arity_sz(functor_t f);
483 
484 			/* Get C-values from Prolog terms */
485 PL_EXPORT(int)		PL_get_atom(term_t t, atom_t *a) WUNUSED;
486 PL_EXPORT(int)		PL_get_bool(term_t t, int *value) WUNUSED;
487 PL_EXPORT(int)		PL_get_atom_chars(term_t t, char **a) WUNUSED;
488 #define PL_get_string_chars(t, s, l) PL_get_string(t,s,l)
489 					/* PL_get_string() is deprecated */
490 PL_EXPORT(int)		PL_get_string(term_t t, char **s, size_t *len) WUNUSED;
491 PL_EXPORT(int)		PL_get_chars(term_t t, char **s, unsigned int flags) WUNUSED;
492 PL_EXPORT(int)		PL_get_list_chars(term_t l, char **s,
493 					  unsigned int flags) WUNUSED;
494 PL_EXPORT(int)		PL_get_atom_nchars(term_t t, size_t *len, char **a) WUNUSED;
495 PL_EXPORT(int)		PL_get_list_nchars(term_t l,
496 					   size_t *len, char **s,
497 					   unsigned int flags) WUNUSED;
498 PL_EXPORT(int)		PL_get_nchars(term_t t,
499 				      size_t *len, char **s,
500 				      unsigned int flags) WUNUSED;
501 PL_EXPORT(int)		PL_get_integer(term_t t, int *i) WUNUSED;
502 PL_EXPORT(int)		PL_get_long(term_t t, long *i) WUNUSED;
503 PL_EXPORT(int)		PL_get_intptr(term_t t, intptr_t *i) WUNUSED;
504 PL_EXPORT(int)		PL_get_pointer(term_t t, void **ptr) WUNUSED;
505 PL_EXPORT(int)		PL_get_float(term_t t, double *f) WUNUSED;
506 PL_EXPORT(int)		PL_get_functor(term_t t, functor_t *f) WUNUSED;
507 PL_EXPORT(int)		PL_get_name_arity_sz(term_t t, atom_t *name,
508 					     size_t *arity) WUNUSED;
509 PL_EXPORT(int)		PL_get_compound_name_arity_sz(term_t t, atom_t *name,
510 						      size_t *arity) WUNUSED;
511 PL_EXPORT(int)		PL_get_name_arity(term_t t, atom_t *name,
512 					  int *arity) WUNUSED;
513 PL_EXPORT(int)		PL_get_compound_name_arity(term_t t, atom_t *name,
514 						   int *arity) WUNUSED;
515 PL_EXPORT(int)		PL_get_module(term_t t, module_t *module) WUNUSED;
516 PL_EXPORT(int)		PL_get_arg_sz(size_t index, term_t t, term_t a) WUNUSED;
517 PL_EXPORT(int)		PL_get_arg(int index, term_t t, term_t a) WUNUSED;
518 PL_EXPORT(int)		PL_get_dict_key(atom_t key, term_t dict, term_t value);
519 PL_EXPORT(int)		PL_get_list(term_t l, term_t h, term_t t) WUNUSED;
520 PL_EXPORT(int)		PL_get_head(term_t l, term_t h) WUNUSED;
521 PL_EXPORT(int)		PL_get_tail(term_t l, term_t t) WUNUSED;
522 PL_EXPORT(int)		PL_get_nil(term_t l) WUNUSED;
523 PL_EXPORT(int)		PL_get_term_value(term_t t, term_value_t *v) WUNUSED;
524 PL_EXPORT(char *)	PL_quote(int chr, const char *data);
525 
526 			/* Verify types */
527 PL_EXPORT(int)		PL_term_type(term_t t);
528 PL_EXPORT(int)		PL_is_variable(term_t t);
529 PL_EXPORT(int)		PL_is_ground(term_t t);
530 PL_EXPORT(int)		PL_is_atom(term_t t);
531 PL_EXPORT(int)		PL_is_integer(term_t t);
532 PL_EXPORT(int)		PL_is_string(term_t t);
533 PL_EXPORT(int)		PL_is_float(term_t t);
534 PL_EXPORT(int)		PL_is_rational(term_t t);
535 PL_EXPORT(int)		PL_is_compound(term_t t);
536 PL_EXPORT(int)		PL_is_callable(term_t t);
537 PL_EXPORT(int)		PL_is_functor(term_t t, functor_t f);
538 PL_EXPORT(int)		PL_is_list(term_t t);
539 PL_EXPORT(int)		PL_is_dict(term_t t);
540 PL_EXPORT(int)		PL_is_pair(term_t t);
541 PL_EXPORT(int)		PL_is_atomic(term_t t);
542 PL_EXPORT(int)		PL_is_number(term_t t);
543 PL_EXPORT(int)		PL_is_acyclic(term_t t);
544 
545 			/* Assign to term-references */
546 PL_EXPORT(int)		PL_put_variable(term_t t);
547 PL_EXPORT(int)		PL_put_atom(term_t t, atom_t a);
548 PL_EXPORT(int)		PL_put_bool(term_t t, int val);
549 PL_EXPORT(int)		PL_put_atom_chars(term_t t, const char *chars);
550 PL_EXPORT(int)		PL_put_string_chars(term_t t, const char *chars) WUNUSED;
551 PL_EXPORT(int)		PL_put_chars(term_t t, int flags,
552 				     size_t len, const char *chars) WUNUSED;
553 PL_EXPORT(int)		PL_put_list_chars(term_t t, const char *chars) WUNUSED;
554 PL_EXPORT(int)		PL_put_list_codes(term_t t, const char *chars) WUNUSED;
555 PL_EXPORT(int)		PL_put_atom_nchars(term_t t, size_t l, const char *chars);
556 PL_EXPORT(int)		PL_put_string_nchars(term_t t, size_t len, const char *chars) WUNUSED;
557 PL_EXPORT(int)		PL_put_list_nchars(term_t t, size_t l, const char *chars) WUNUSED;
558 PL_EXPORT(int)		PL_put_list_ncodes(term_t t, size_t l, const char *chars) WUNUSED;
559 PL_EXPORT(int)		PL_put_integer(term_t t, long i) WUNUSED;
560 PL_EXPORT(int)		PL_put_pointer(term_t t, void *ptr) WUNUSED;
561 PL_EXPORT(int)		PL_put_float(term_t t, double f) WUNUSED;
562 PL_EXPORT(int)		PL_put_functor(term_t t, functor_t functor) WUNUSED;
563 PL_EXPORT(int)		PL_put_list(term_t l) WUNUSED;
564 PL_EXPORT(int)		PL_put_nil(term_t l);
565 PL_EXPORT(int)		PL_put_term(term_t t1, term_t t2);
566 PL_EXPORT(int)		PL_put_dict(term_t t, atom_t tag, size_t len,
567 				    const atom_t *keys, term_t values);
568 
569 			/* construct a functor or list-cell */
570 PL_EXPORT(int)		PL_cons_functor(term_t h, functor_t f, ...) WUNUSED;
571 PL_EXPORT(int)		PL_cons_functor_v(term_t h, functor_t fd, term_t a0) WUNUSED;
572 PL_EXPORT(int)		PL_cons_list(term_t l, term_t h, term_t t) WUNUSED;
573 
574 			/* Unify term-references */
575 PL_EXPORT(int)		PL_unify(term_t t1, term_t t2) WUNUSED;
576 PL_EXPORT(int)		PL_unify_atom(term_t t, atom_t a) WUNUSED;
577 PL_EXPORT(int)		PL_unify_atom_chars(term_t t, const char *chars) WUNUSED;
578 PL_EXPORT(int)		PL_unify_list_chars(term_t t, const char *chars) WUNUSED;
579 PL_EXPORT(int)		PL_unify_list_codes(term_t t, const char *chars) WUNUSED;
580 PL_EXPORT(int)		PL_unify_string_chars(term_t t, const char *chars) WUNUSED;
581 PL_EXPORT(int)		PL_unify_atom_nchars(term_t t, size_t l, const char *s) WUNUSED;
582 PL_EXPORT(int)		PL_unify_list_ncodes(term_t t, size_t l, const char *s) WUNUSED;
583 PL_EXPORT(int)		PL_unify_list_nchars(term_t t, size_t l, const char *s) WUNUSED;
584 PL_EXPORT(int)		PL_unify_string_nchars(term_t t,
585 					       size_t len,
586 					       const char *chars) WUNUSED;
587 PL_EXPORT(int)		PL_unify_bool(term_t t, int n) WUNUSED;
588 PL_EXPORT(int)		PL_unify_integer(term_t t, intptr_t n) WUNUSED;
589 PL_EXPORT(int)		PL_unify_float(term_t t, double f) WUNUSED;
590 PL_EXPORT(int)		PL_unify_pointer(term_t t, void *ptr) WUNUSED;
591 PL_EXPORT(int)		PL_unify_functor(term_t t, functor_t f) WUNUSED;
592 PL_EXPORT(int)		PL_unify_compound(term_t t, functor_t f) WUNUSED;
593 PL_EXPORT(int)		PL_unify_list(term_t l, term_t h, term_t t) WUNUSED;
594 PL_EXPORT(int)		PL_unify_nil(term_t l) WUNUSED;
595 PL_EXPORT(int)		PL_unify_arg_sz(size_t index, term_t t, term_t a) WUNUSED;
596 PL_EXPORT(int)		PL_unify_arg(int index, term_t t, term_t a) WUNUSED;
597 PL_EXPORT(int)		PL_unify_term(term_t t, ...) WUNUSED;
598 PL_EXPORT(int)		PL_unify_chars(term_t t, int flags,
599 				       size_t len, const char *s) WUNUSED;
600 
601 		 /*******************************
602 		 *	       LISTS		*
603 		 *******************************/
604 
605 PL_EXPORT(int)		PL_skip_list(term_t list, term_t tail, size_t *len);
606 
607 
608 		 /*******************************
609 		 *    WIDE CHARACTER VERSIONS	*
610 		 *******************************/
611 
612 PL_EXPORT(int)		PL_unify_wchars(term_t t, int type,
613 					size_t len, const pl_wchar_t *s) WUNUSED;
614 PL_EXPORT(int)		PL_unify_wchars_diff(term_t t, term_t tail, int type,
615 					size_t len, const pl_wchar_t *s) WUNUSED;
616 PL_EXPORT(int)		PL_get_wchars(term_t l,
617 				      size_t *length, pl_wchar_t **s,
618 				      unsigned flags) WUNUSED;
619 PL_EXPORT(size_t)	PL_utf8_strlen(const char *s, size_t len) WUNUSED;
620 
621 
622 		 /*******************************
623 		 *	   WIDE INTEGERS	*
624 		 *******************************/
625 
626 
627 PL_EXPORT(int)		PL_get_int64(term_t t, int64_t *i) WUNUSED;
628 PL_EXPORT(int)		PL_unify_int64(term_t t, int64_t value) WUNUSED;
629 PL_EXPORT(int)		PL_unify_uint64(term_t t, uint64_t value) WUNUSED;
630 PL_EXPORT(int)		PL_put_int64(term_t t, int64_t i) WUNUSED;
631 PL_EXPORT(int)		PL_put_uint64(term_t t, uint64_t i) WUNUSED;
632 
633 
634 		 /*******************************
635 		 *     ATTRIBUTED VARIABLES	*
636 		 *******************************/
637 
638 PL_EXPORT(int)		PL_is_attvar(term_t t);
639 PL_EXPORT(int)		PL_get_attr(term_t v, term_t a);
640 
641 
642 		 /*******************************
643 		 *	      ERRORS		*
644 		 *******************************/
645 
646 PL_EXPORT(int)		PL_get_atom_ex(term_t t, atom_t *a);
647 PL_EXPORT(int)		PL_get_integer_ex(term_t t, int *i);
648 PL_EXPORT(int)		PL_get_long_ex(term_t t, long *i);
649 PL_EXPORT(int)		PL_get_int64_ex(term_t t, int64_t *i);
650 PL_EXPORT(int)		PL_get_intptr_ex(term_t t, intptr_t *i);
651 PL_EXPORT(int)		PL_get_size_ex(term_t t, size_t *i);
652 PL_EXPORT(int)		PL_get_bool_ex(term_t t, int *i);
653 PL_EXPORT(int)		PL_get_float_ex(term_t t, double *f);
654 PL_EXPORT(int)		PL_get_char_ex(term_t t, int *p, int eof);
655 PL_EXPORT(int)		PL_unify_bool_ex(term_t t, int val);
656 PL_EXPORT(int)		PL_get_pointer_ex(term_t t, void **addrp);
657 PL_EXPORT(int)		PL_unify_list_ex(term_t l, term_t h, term_t t);
658 PL_EXPORT(int)		PL_unify_nil_ex(term_t l);
659 PL_EXPORT(int)		PL_get_list_ex(term_t l, term_t h, term_t t);
660 PL_EXPORT(int)		PL_get_nil_ex(term_t l);
661 
662 PL_EXPORT(int)		PL_instantiation_error(term_t culprit);
663 PL_EXPORT(int)		PL_uninstantiation_error(term_t culprit);
664 PL_EXPORT(int)		PL_representation_error(const char *resource);
665 PL_EXPORT(int)		PL_type_error(const char *expected, term_t culprit);
666 PL_EXPORT(int)		PL_domain_error(const char *expected, term_t culprit);
667 PL_EXPORT(int)		PL_existence_error(const char *type, term_t culprit);
668 PL_EXPORT(int)		PL_permission_error(const char *operation,
669 					    const char *type, term_t culprit);
670 PL_EXPORT(int)		PL_resource_error(const char *resource);
671 #ifdef SIO_MAGIC
672 PL_EXPORT(int)		PL_syntax_error(const char *msg, IOSTREAM *in);
673 #else
674 PL_EXPORT(int)		PL_syntax_error(const char *msg, void *in);
675 #endif
676 
677 		 /*******************************
678 		 *	       BLOBS		*
679 		 *******************************/
680 
681 #define PL_BLOB_MAGIC_B	0x75293a00	/* Magic to validate a blob-type */
682 #define PL_BLOB_VERSION 1		/* Current version */
683 #define PL_BLOB_MAGIC	(PL_BLOB_MAGIC_B|PL_BLOB_VERSION)
684 
685 #define PL_BLOB_UNIQUE	0x01		/* Blob content is unique */
686 #define PL_BLOB_TEXT	0x02		/* blob contains text */
687 #define PL_BLOB_NOCOPY	0x04		/* do not copy the data */
688 #define PL_BLOB_WCHAR	0x08		/* wide character string */
689 
690 typedef struct PL_blob_t
691 { uintptr_t		magic;		/* PL_BLOB_MAGIC */
692   uintptr_t		flags;		/* PL_BLOB_* */
693   char *		name;		/* name of the type */
694   int			(*release)(atom_t a);
695   int			(*compare)(atom_t a, atom_t b);
696 #ifdef SIO_MAGIC
697   int			(*write)(IOSTREAM *s, atom_t a, int flags);
698 #else
699   int			(*write)(void *s, atom_t a, int flags);
700 #endif
701   void			(*acquire)(atom_t a);
702 #ifdef SIO_MAGIC
703   int			(*save)(atom_t a, IOSTREAM *s);
704   atom_t		(*load)(IOSTREAM *s);
705 #else
706   int			(*save)(atom_t a, void*);
707   atom_t		(*load)(void *s);
708 #endif
709   size_t		padding;	/* Required 0-padding */
710 					/* private */
711   void *		reserved[9];	/* for future extension */
712   int			registered;	/* Already registered? */
713   int			rank;		/* Rank for ordering atoms */
714   struct PL_blob_t *    next;		/* next in registered type-chain */
715   atom_t		atom_name;	/* Name as atom */
716 } PL_blob_t;
717 
718 PL_EXPORT(int)		PL_is_blob(term_t t, PL_blob_t **type);
719 PL_EXPORT(int)		PL_unify_blob(term_t t, void *blob, size_t len,
720 				      PL_blob_t *type);
721 PL_EXPORT(int)		PL_put_blob(term_t t, void *blob, size_t len,
722 				    PL_blob_t *type);
723 PL_EXPORT(int)		PL_get_blob(term_t t, void **blob, size_t *len,
724 				    PL_blob_t **type);
725 
726 PL_EXPORT(void*)	PL_blob_data(atom_t a,
727 				     size_t *len,
728 				     struct PL_blob_t **type);
729 
730 PL_EXPORT(void)		PL_register_blob_type(PL_blob_t *type);
731 PL_EXPORT(PL_blob_t*)	PL_find_blob_type(const char* name);
732 PL_EXPORT(int)		PL_unregister_blob_type(PL_blob_t *type);
733 
734 
735 #ifdef __GNU_MP__
736 
737 		 /*******************************
738 		 *	       GMP		*
739 		 *******************************/
740 
741 PL_EXPORT(int)	PL_get_mpz(term_t t, mpz_t mpz) WUNUSED;
742 PL_EXPORT(int)	PL_get_mpq(term_t t,  mpq_t mpq) WUNUSED;
743 PL_EXPORT(int)	PL_unify_mpz(term_t t, mpz_t mpz) WUNUSED;
744 PL_EXPORT(int)	PL_unify_mpq(term_t t, mpq_t mpq) WUNUSED;
745 
746 #endif /*__GNU_MP__*/
747 
748 		 /*******************************
749 		 *	  FILENAME SUPPORT	*
750 		 *******************************/
751 
752 #define PL_FILE_ABSOLUTE	0x01	/* return absolute path */
753 #define PL_FILE_OSPATH		0x02	/* return path in OS notation */
754 #define PL_FILE_SEARCH		0x04	/* use file_search_path */
755 #define PL_FILE_EXIST		0x08	/* demand file to exist */
756 #define PL_FILE_READ		0x10	/* demand read-access */
757 #define PL_FILE_WRITE		0x20	/* demand write-access */
758 #define PL_FILE_EXECUTE		0x40	/* demand execute-access */
759 #define PL_FILE_NOERRORS	0x80	/* do not raise exceptions */
760 
761 PL_EXPORT(int)		PL_get_file_name(term_t n, char **name, int flags);
762 PL_EXPORT(int)		PL_get_file_nameW(term_t n, wchar_t **name, int flags);
763 PL_EXPORT(void)		PL_changed_cwd(void); /* foreign code changed CWD */
764 PL_EXPORT(char *)	PL_cwd(char *buf, size_t buflen);
765 
766 
767 		 /*******************************
768 		 *    QUINTUS/SICSTUS WRAPPER	*
769 		 *******************************/
770 
771 PL_EXPORT(int)		PL_cvt_i_char(term_t p, char *c);
772 PL_EXPORT(int)		PL_cvt_i_uchar(term_t p, unsigned char *c);
773 PL_EXPORT(int)		PL_cvt_i_short(term_t p, short *s);
774 PL_EXPORT(int)		PL_cvt_i_ushort(term_t p, unsigned short *s);
775 PL_EXPORT(int)		PL_cvt_i_int(term_t p, int *c);
776 PL_EXPORT(int)		PL_cvt_i_uint(term_t p, unsigned int *c);
777 PL_EXPORT(int)		PL_cvt_i_long(term_t p, long *c);
778 PL_EXPORT(int)		PL_cvt_i_ulong(term_t p, unsigned long *c);
779 PL_EXPORT(int)		PL_cvt_i_int64(term_t p, int64_t *c);
780 PL_EXPORT(int)		PL_cvt_i_uint64(term_t p, uint64_t *c);
781 PL_EXPORT(int)		PL_cvt_i_size_t(term_t p, size_t *c);
782 PL_EXPORT(int)		PL_cvt_i_float(term_t p, double *c);
783 PL_EXPORT(int)		PL_cvt_i_single(term_t p, float *c);
784 PL_EXPORT(int)		PL_cvt_i_string(term_t p, char **c);
785 PL_EXPORT(int)		PL_cvt_i_codes(term_t p, char **c);
786 PL_EXPORT(int)		PL_cvt_i_atom(term_t p, atom_t *c);
787 PL_EXPORT(int)		PL_cvt_i_address(term_t p, void *c);
788 PL_EXPORT(int)		PL_cvt_o_int64(int64_t c, term_t p);
789 PL_EXPORT(int)		PL_cvt_o_float(double c, term_t p);
790 PL_EXPORT(int)		PL_cvt_o_single(float c, term_t p);
791 PL_EXPORT(int)		PL_cvt_o_string(const char *c, term_t p);
792 PL_EXPORT(int)		PL_cvt_o_codes(const char *c, term_t p);
793 PL_EXPORT(int)		PL_cvt_o_atom(atom_t c, term_t p);
794 PL_EXPORT(int)		PL_cvt_o_address(void *address, term_t p);
795 PL_EXPORT(term_t)	PL_new_nil_ref(void);
796 
797 /* set/get encoding for PL_cvt_*_string() functions.  The default
798    is UTF-8 (REP_UTF8)
799 */
800 
801 PL_EXPORT(int)		PL_cvt_encoding(void);
802 PL_EXPORT(int)		PL_cvt_set_encoding(int enc);
803 PL_EXPORT(void)		SP_set_state(int state);
804 PL_EXPORT(int)		SP_get_state(void);
805 
806 
807 		 /*******************************
808 		 *	     COMPARE		*
809 		 *******************************/
810 
811 PL_EXPORT(int)		PL_compare(term_t t1, term_t t2);
812 PL_EXPORT(int)		PL_same_compound(term_t t1, term_t t2);
813 
814 		 /*******************************
815 		 *	     MESSAGES		*
816 		 *******************************/
817 
818 PL_EXPORT(int)		PL_warning(const char *fmt, ...);
819 PL_EXPORT(void)		PL_fatal_error(const char *fmt, ...);
820 
821 		 /*******************************
822 		 *      RECORDED DATABASE	*
823 		 *******************************/
824 
825 PL_EXPORT(record_t)	PL_record(term_t term);
826 PL_EXPORT(int)		PL_recorded(record_t record, term_t term);
827 PL_EXPORT(void)		PL_erase(record_t record);
828 PL_EXPORT(record_t)	PL_duplicate_record(record_t r);
829 
830 PL_EXPORT(char *)	PL_record_external(term_t t, size_t *size);
831 PL_EXPORT(int)		PL_recorded_external(const char *rec, term_t term);
832 PL_EXPORT(int)		PL_erase_external(char *rec);
833 
834 		 /*******************************
835 		 *	   PROLOG FLAGS		*
836 		 *******************************/
837 
838 #define PL_set_feature  PL_set_prolog_flag /* compatibility */
839 PL_EXPORT(int)		PL_set_prolog_flag(const char *name, int type, ...);
840 
841 
842 		 /*******************************
843 		 *	INTERNAL FUNCTIONS	*
844 		 *******************************/
845 
846 PL_EXPORT(PL_atomic_t)	_PL_get_atomic(term_t t);
847 PL_EXPORT(void)		_PL_put_atomic(term_t t, PL_atomic_t a);
848 PL_EXPORT(int)		_PL_unify_atomic(term_t t, PL_atomic_t a);
849 PL_EXPORT(int)		_PL_get_arg_sz(size_t index, term_t t, term_t a);
850 PL_EXPORT(int)		_PL_get_arg(int index, term_t t, term_t a);
851 
852 
853 		 /*******************************
854 		 *	    CHAR BUFFERS	*
855 		 *******************************/
856 
857 #define CVT_ATOM	    0x00000001
858 #define CVT_STRING	    0x00000002
859 #define CVT_LIST	    0x00000004
860 #define CVT_INTEGER	    0x00000008
861 #define CVT_RATIONAL	    0x00000010
862 #define CVT_FLOAT	    0x00000020
863 #define CVT_VARIABLE	    0x00000040
864 #define CVT_NUMBER	    (CVT_RATIONAL|CVT_FLOAT)
865 #define CVT_ATOMIC	    (CVT_NUMBER|CVT_ATOM|CVT_STRING)
866 #define CVT_WRITE	    0x00000080
867 #define CVT_WRITE_CANONICAL 0x00000080
868 #define CVT_WRITEQ	    0x000000C0
869 #define CVT_ALL		    (CVT_ATOMIC|CVT_LIST)
870 #define CVT_MASK	    0x00000fff
871 
872 #define CVT_EXCEPTION	    0x00001000	/* throw exception on error */
873 #define CVT_VARNOFAIL	    0x00002000	/* return 2 if argument is unbound */
874 
875 #define BUF_DISCARDABLE	    0x00000000	/* Store in single thread-local buffer */
876 #define BUF_STACK	    0x00010000	/* Store in stack of buffers */
877 #define BUF_MALLOC	    0x00020000	/* Store using PL_malloc() */
878 #define BUF_ALLOW_STACK	    0x00040000	/* Allow pointer into (global) stack */
879 
880 #define BUF_RING	    BUF_STACK   /* legacy ring buffer */
881 
882 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
883 Output   representation   for   PL_get_chars()     and    friends.   The
884 prepresentation type REP_FN is for   PL_get_file_name()  and friends. On
885 Windows we use UTF-8 which is translated   by the `XOS' layer to Windows
886 UNICODE file functions.
887 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
888 
889 #define REP_ISO_LATIN_1	    0x00000000	/* output representation */
890 #define REP_UTF8	    0x00100000
891 #define REP_MB		    0x00200000
892 #ifdef __WINDOWS__
893 #define REP_FN		    REP_UTF8
894 #else
895 #define REP_FN		    REP_MB
896 #endif
897 
898 #define PL_DIFF_LIST	    0x01000000	/* PL_unify_chars() */
899 
900 
901                 /*******************************
902                 *         STRING BUFFERS       *
903                 *******************************/
904 
905 #define PL_STRINGS_MARK() \
906 	{ buf_mark_t __PL_mark; \
907 	  PL_mark_string_buffers(&__PL_mark);
908 #define PL_STRINGS_RELEASE() \
909 	  PL_release_string_buffers_from_mark(__PL_mark); \
910 	}
911 
912 PL_EXPORT(void)		PL_mark_string_buffers(buf_mark_t *mark);
913 PL_EXPORT(void)         PL_release_string_buffers_from_mark(buf_mark_t mark);
914 
915 
916 #ifdef SIO_MAGIC			/* defined from <SWI-Stream.h> */
917 		 /*******************************
918 		 *	  STREAM SUPPORT	*
919 		 *******************************/
920 
921 					/* Make IOSTREAM known to Prolog */
922 #define PL_open_stream  PL_unify_stream	/* compatibility */
923 PL_EXPORT(int)		PL_unify_stream(term_t t, IOSTREAM *s);
924 PL_EXPORT(int)		PL_get_stream_handle(term_t t, IOSTREAM **s);
925 PL_EXPORT(int)		PL_get_stream(term_t t, IOSTREAM **s, int flags);
926 PL_EXPORT(IOSTREAM*)	PL_acquire_stream(IOSTREAM *s);
927 PL_EXPORT(int)		PL_release_stream(IOSTREAM *s);
928 PL_EXPORT(int)		PL_release_stream_noerror(IOSTREAM *s);
929 PL_EXPORT(IOSTREAM *)	PL_open_resource(module_t m,
930 					 const char *name,
931 					 const char *rc_class,
932 					 const char *mode);
933 
934 PL_EXPORT(IOSTREAM *)*_PL_streams(void);	/* base of streams */
935 #ifndef PL_KERNEL
936 #define Suser_input     (_PL_streams()[0])
937 #define Suser_output    (_PL_streams()[1])
938 #define Suser_error     (_PL_streams()[2])
939 #define Scurrent_input  (_PL_streams()[3])
940 #define Scurrent_output (_PL_streams()[4])
941 #endif
942 
943 #define PL_WRT_QUOTED		0x01	/* quote atoms */
944 #define PL_WRT_IGNOREOPS	0x02	/* ignore list/operators */
945 #define PL_WRT_NUMBERVARS	0x04	/* print $VAR(N) as a variable */
946 #define PL_WRT_PORTRAY		0x08	/* call portray */
947 #define PL_WRT_CHARESCAPES	0x10	/* Output ISO escape sequences */
948 #define PL_WRT_BACKQUOTED_STRING 0x20	/* Write strings as `...` */
949 					/* Write attributed variables */
950 #define PL_WRT_ATTVAR_IGNORE	0x040	/* Default: just write the var */
951 #define PL_WRT_ATTVAR_DOTS	0x080	/* Write as Var{...} */
952 #define PL_WRT_ATTVAR_WRITE	0x100	/* Write as Var{Attributes} */
953 #define PL_WRT_ATTVAR_PORTRAY	0x200	/* Use Module:portray_attrs/2 */
954 #define PL_WRT_ATTVAR_MASK \
955 	(PL_WRT_ATTVAR_IGNORE | \
956 	 PL_WRT_ATTVAR_DOTS | \
957 	 PL_WRT_ATTVAR_WRITE | \
958 	 PL_WRT_ATTVAR_PORTRAY)
959 #define PL_WRT_BLOB_PORTRAY	0x400	/* Use portray to emit non-text blobs */
960 #define PL_WRT_NO_CYCLES	0x800	/* Never emit @(Template,Subst) */
961 #define PL_WRT_NEWLINE	       0x2000	/* Add a newline */
962 #define PL_WRT_VARNAMES	       0x4000	/* Internal: variable_names(List)  */
963 #define PL_WRT_BACKQUOTE_IS_SYMBOL 0x8000 /* ` is a symbol char */
964 #define PL_WRT_DOTLISTS	       0x10000	/* Write lists as .(A,B) */
965 #define PL_WRT_BRACETERMS      0x20000	/* Write {A} as {}(A) */
966 #define PL_WRT_NODICT	       0x40000	/* Do not write dicts in pretty syntax */
967 #define PL_WRT_NODOTINATOM     0x80000	/* never write a.b unquoted */
968 #define PL_WRT_NO_LISTS	       0x100000	/* Do not write lists as [...] */
969 #define PL_WRT_RAT_NATURAL     0x200000	/* Write rationals as 1/3 */
970 
971 PL_EXPORT(int)	PL_write_term(IOSTREAM *s,
972 			     term_t term,
973 			     int precedence,
974 			     int flags);
975 
976 					/* PL_ttymode() results */
977 #define PL_NOTTY	0		/* -tty in effect */
978 #define PL_RAWTTY	1		/* get_single_char/1 */
979 #define PL_COOKEDTTY	2		/* normal input */
980 
981 PL_EXPORT(int)		PL_ttymode(IOSTREAM *s);
982 
983 #endif /*SIO_MAGIC*/
984 
985 PL_EXPORT(int)  PL_put_term_from_chars(term_t t, int flags,
986 				       size_t len, const char *s);
987 PL_EXPORT(int)	PL_chars_to_term(const char *chars,
988 				term_t term);
989 PL_EXPORT(int)	PL_wchars_to_term(const pl_wchar_t *chars,
990 				 term_t term);
991 
992 
993 		 /*******************************
994 		 *	    EMBEDDING		*
995 		 *******************************/
996 
997 PL_EXPORT(int)		PL_initialise(int argc, char **argv);
998 PL_EXPORT(int)		PL_winitialise(int argc, wchar_t **argv);
999 PL_EXPORT(int)		PL_is_initialised(int *argc, char ***argv);
1000 PL_EXPORT(int)		PL_set_resource_db_mem(const unsigned char *data,
1001 					       size_t size);
1002 PL_EXPORT(int)		PL_toplevel(void);
1003 PL_EXPORT(int)		PL_cleanup(int status);
1004 PL_EXPORT(void)		PL_cleanup_fork();
1005 PL_EXPORT(int)		PL_halt(int status);
1006 
1007 		 /*******************************
1008 		 *	  DYNAMIC LINKING	*
1009 		 *******************************/
1010 
1011 PL_EXPORT(void *)	PL_dlopen(const char *file, int flags);
1012 PL_EXPORT(const char *) PL_dlerror(void);
1013 PL_EXPORT(void *)	PL_dlsym(void *handle, char *symbol);
1014 PL_EXPORT(int)		PL_dlclose(void *handle);
1015 
1016 
1017 		 /*******************************
1018 		 *      INPUT/PROMPT/ETC	*
1019 		 *******************************/
1020 
1021 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1022 NOTE: the functions in this section are   not  documented, as as yet not
1023 adviced for public usage.  They  are   intended  to  provide an abstract
1024 interface for the GNU readline  interface   as  defined  in the readline
1025 package.
1026 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1027 					/* PL_dispatch() modes */
1028 #define PL_DISPATCH_NOWAIT    0		/* Dispatch only once */
1029 #define PL_DISPATCH_WAIT      1		/* Dispatch till input available */
1030 #define PL_DISPATCH_INSTALLED 2		/* dispatch function installed? */
1031 
1032 PL_EXPORT(int)		PL_dispatch(int fd, int wait);
1033 PL_EXPORT(void)		PL_add_to_protocol(const char *buf, size_t count);
1034 PL_EXPORT(char *)	PL_prompt_string(int fd);
1035 PL_EXPORT(void)		PL_write_prompt(int dowrite);
1036 PL_EXPORT(void)		PL_prompt_next(int fd);
1037 PL_EXPORT(char *)	PL_atom_generator(const char *prefix, int state);
1038 PL_EXPORT(pl_wchar_t*)	PL_atom_generator_w(const pl_wchar_t *pref,
1039 					    pl_wchar_t *buffer,
1040 					    size_t buflen,
1041 					    int state);
1042 
1043 
1044 		 /*******************************
1045 		 *	MEMORY ALLOCATION	*
1046 		 *******************************/
1047 
1048 PL_EXPORT(void *)	PL_malloc(size_t size);
1049 PL_EXPORT(void *)	PL_malloc_atomic(size_t size);
1050 PL_EXPORT(void *)	PL_malloc_uncollectable(size_t size);
1051 PL_EXPORT(void *)	PL_malloc_atomic_uncollectable(size_t size);
1052 PL_EXPORT(void *)	PL_realloc(void *mem, size_t size);
1053 PL_EXPORT(void *)	PL_malloc_unmanaged(size_t size);
1054 PL_EXPORT(void *)	PL_malloc_atomic_unmanaged(size_t size);
1055 PL_EXPORT(void)		PL_free(void *mem);
1056 PL_EXPORT(int)		PL_linger(void *mem);
1057 
1058 
1059 		/********************************
1060 		*             HOOKS		*
1061 		********************************/
1062 
1063 #define PL_DISPATCH_INPUT   0		/* There is input available */
1064 #define PL_DISPATCH_TIMEOUT 1		/* Dispatch timeout */
1065 
1066 typedef int  (*PL_dispatch_hook_t)(int fd);
1067 typedef void (*PL_abort_hook_t)(void);
1068 typedef void (*PL_initialise_hook_t)(int argc, char **argv);
1069 typedef int  (*PL_agc_hook_t)(atom_t a);
1070 
1071 PL_EXPORT(PL_dispatch_hook_t)	PL_dispatch_hook(PL_dispatch_hook_t);
1072 PL_EXPORT(void)			PL_abort_hook(PL_abort_hook_t);
1073 PL_EXPORT(void)			PL_initialise_hook(PL_initialise_hook_t);
1074 PL_EXPORT(int)			PL_abort_unhook(PL_abort_hook_t);
1075 PL_EXPORT(PL_agc_hook_t)	PL_agc_hook(PL_agc_hook_t);
1076 
1077 
1078 		/********************************
1079 		*            SIGNALS            *
1080 		*********************************/
1081 
1082 /* PL_signal() masks (deprecated) */
1083 #define PL_SIGSYNC	0x00010000	/* call handler synchronously */
1084 #define PL_SIGNOFRAME	0x00020000	/* Do not create a Prolog frame */
1085 
1086 #define PLSIG_THROW     0x0002		/* throw signal(num, name) */
1087 #define PLSIG_SYNC      0x0004		/* call synchronously */
1088 #define PLSIG_NOFRAME   0x0008		/* Do not create a Prolog frame */
1089 
1090 
1091 
1092 
1093 typedef struct pl_sigaction
1094 { void        (*sa_cfunction)(int);	/* traditional C function */
1095   predicate_t sa_predicate;		/* call a predicate */
1096   int	      sa_flags;			/* additional flags */
1097   void       *reserved[2];		/* future extentions */
1098 } pl_sigaction_t;
1099 
1100 
1101 PL_EXPORT(void) (*PL_signal(int sig, void (*func)(int)))(int);
1102 PL_EXPORT(int)  PL_sigaction(int sig, pl_sigaction_t *act, pl_sigaction_t *old);
1103 PL_EXPORT(void)	PL_interrupt(int sig);
1104 PL_EXPORT(int)	PL_raise(int sig);
1105 PL_EXPORT(int)	PL_handle_signals(void);
1106 PL_EXPORT(int)	PL_get_signum_ex(term_t sig, int *n);
1107 
1108 
1109 		/********************************
1110 		*      PROLOG ACTION/QUERY      *
1111 		*********************************/
1112 
1113 #define	PL_ACTION_TRACE		1	/* switch to trace mode */
1114 #define PL_ACTION_DEBUG		2	/* switch to debug mode */
1115 #define PL_ACTION_BACKTRACE	3	/* show a backtrace (stack dump) */
1116 #define PL_ACTION_BREAK		4	/* create a break environment */
1117 #define PL_ACTION_HALT		5	/* halt Prolog execution */
1118 #define PL_ACTION_ABORT		6	/* generate a Prolog abort */
1119 					/* 7: Obsolete PL_ACTION_SYMBOLFILE */
1120 #define PL_ACTION_WRITE		8	/* write via Prolog i/o buffer */
1121 #define PL_ACTION_FLUSH		9	/* Flush Prolog i/o buffer */
1122 #define PL_ACTION_GUIAPP	10	/* Win32: set when this is a gui */
1123 #define PL_ACTION_ATTACH_CONSOLE 11	/* MT: Attach a console */
1124 #define PL_GMP_SET_ALLOC_FUNCTIONS 12	/* GMP: do not change allocation functions */
1125 #define PL_ACTION_TRADITIONAL	13	/* Set --traditional */
1126 
1127 #define PL_BT_SAFE		0x1	/* Do not try to print goals */
1128 #define PL_BT_USER		0x2	/* Only show user-goals */
1129 
1130 PL_EXPORT(int)	PL_action(int, ...);	/* perform some action */
1131 PL_EXPORT(void)	PL_on_halt(int (*)(int, void *), void *);
1132 PL_EXPORT(void)	PL_exit_hook(int (*)(int, void *), void *);
1133 PL_EXPORT(void)	PL_backtrace(int depth, int flags);
1134 PL_EXPORT(char *) PL_backtrace_string(int depth, int flags);
1135 PL_EXPORT(int)	PL_check_data(term_t data);
1136 PL_EXPORT(int)	PL_check_stacks(void);
1137 PL_EXPORT(int)	PL_current_prolog_flag(atom_t name, int type, void *ptr);
1138 
1139 
1140 		 /*******************************
1141 		 *	      VERSIONS		*
1142 		 *******************************/
1143 
1144 #define PL_VERSION_SYSTEM	1	/* Prolog version */
1145 #define PL_VERSION_FLI		2	/* PL_* compatibility */
1146 #define PL_VERSION_REC		3	/* PL_record_external() compatibility */
1147 #define PL_VERSION_QLF		4	/* Saved QLF format version */
1148 #define PL_VERSION_QLF_LOAD	5	/* Min loadable QLF format version */
1149 #define PL_VERSION_VM		6	/* VM signature */
1150 #define PL_VERSION_BUILT_IN	7	/* Built-in predicate signature */
1151 
1152 PL_EXPORT(unsigned int) PL_version(int which);
1153 
1154 
1155 		/********************************
1156 		*         QUERY PROLOG          *
1157 		*********************************/
1158 
1159 #define PL_QUERY_ARGC		1	/* return main() argc */
1160 #define PL_QUERY_ARGV		2	/* return main() argv */
1161 					/* 3: Obsolete PL_QUERY_SYMBOLFILE */
1162 					/* 4: Obsolete PL_QUERY_ORGSYMBOLFILE*/
1163 #define PL_QUERY_GETC		5	/* Read character from terminal */
1164 #define PL_QUERY_MAX_INTEGER	6	/* largest integer */
1165 #define PL_QUERY_MIN_INTEGER	7	/* smallest integer */
1166 #define PL_QUERY_MAX_TAGGED_INT	8	/* largest tagged integer */
1167 #define PL_QUERY_MIN_TAGGED_INT	9	/* smallest tagged integer */
1168 #define PL_QUERY_VERSION        10	/* 207006 = 2.7.6 */
1169 #define PL_QUERY_MAX_THREADS	11	/* maximum thread count */
1170 #define PL_QUERY_ENCODING	12	/* I/O encoding */
1171 #define PL_QUERY_USER_CPU	13	/* User CPU in milliseconds */
1172 #define PL_QUERY_HALTING	14	/* If TRUE, we are in PL_cleanup() */
1173 
1174 PL_EXPORT(intptr_t)	PL_query(int);	/* get information from Prolog */
1175 
1176 
1177 		 /*******************************
1178 		 *	  PROLOG THREADS	*
1179 		 *******************************/
1180 
1181 #define PL_THREAD_NO_DEBUG	0x01	/* Start thread in nodebug mode */
1182 #define PL_THREAD_NOT_DETACHED	0x02	/* Allow Prolog to join */
1183 
1184 typedef enum
1185 { PL_THREAD_CANCEL_FAILED = FALSE,	/* failed to cancel; try abort */
1186   PL_THREAD_CANCEL_JOINED = TRUE,	/* cancelled and joined */
1187   PL_THREAD_CANCEL_MUST_JOIN		/* cancelled, must join */
1188 } rc_cancel;
1189 
1190 typedef struct
1191 { size_t    stack_limit;		/* Total stack limit (bytes) */
1192   size_t    table_space;		/* Total tabling space limit (bytes) */
1193   char *    alias;			/* alias name */
1194   rc_cancel (*cancel)(int id);		/* cancel function */
1195   intptr_t  flags;			/* PL_THREAD_* flags */
1196   size_t    max_queue_size;		/* Max size of associated queue */
1197   void *    reserved[3];		/* reserved for extensions */
1198 } PL_thread_attr_t;
1199 
1200 
1201 PL_EXPORT(int)	PL_thread_self(void);	/* Prolog thread id (-1 if none) */
1202 PL_EXPORT(int)	PL_unify_thread_id(term_t t, int i);
1203 PL_EXPORT(int)	PL_get_thread_id_ex(term_t t, int *idp);
1204 PL_EXPORT(int)	PL_get_thread_alias(int tid, atom_t *alias);	/* Locks alias */
1205 PL_EXPORT(int)	PL_thread_attach_engine(PL_thread_attr_t *attr);
1206 PL_EXPORT(int)	PL_thread_destroy_engine(void);
1207 PL_EXPORT(int)	PL_thread_at_exit(void (*function)(void *),
1208 				  void *closure,
1209 				  int global);
1210 PL_EXPORT(int)	PL_thread_raise(int tid, int sig);
1211 #if defined(_WINDOWS_) || defined(_WINDOWS_H)	/* <windows.h> is included */
1212 PL_EXPORT(int)	PL_w32thread_raise(DWORD dwTid, int sig);
1213 PL_EXPORT(int)	PL_wait_for_console_input(void *handle);
1214 PL_EXPORT(int)	PL_w32_wrap_ansi_console(void);
1215 PL_EXPORT(const char*) PL_w32_running_under_wine(void);
1216 #endif
1217 
1218 		 /*******************************
1219 		 *	 ENGINES (MT-ONLY)	*
1220 		 *******************************/
1221 
1222 #define PL_ENGINE_MAIN	  ((PL_engine_t)0x1)
1223 #define PL_ENGINE_CURRENT ((PL_engine_t)0x2)
1224 
1225 #define PL_ENGINE_SET   0		/* engine set successfully */
1226 #define PL_ENGINE_INVAL	2		/* engine doesn't exist */
1227 #define PL_ENGINE_INUSE	3		/* engine is in use */
1228 
1229 PL_EXPORT(PL_engine_t)	PL_create_engine(PL_thread_attr_t *attributes);
1230 PL_EXPORT(int)		PL_set_engine(PL_engine_t engine, PL_engine_t *old);
1231 PL_EXPORT(int)		PL_destroy_engine(PL_engine_t engine);
1232 
1233 
1234 		 /*******************************
1235 		 *	    HASH TABLES		*
1236 		 *******************************/
1237 
1238 #ifdef TABLE_H_INCLUDED
1239 typedef Table		hash_table_t;
1240 typedef TableEnum	hash_table_enum_t;
1241 #else
1242 typedef struct pl_hash_table	  *hash_table_t;
1243 typedef struct pl_hash_table_enum *hash_table_enum_t;
1244 #endif
1245 
1246 #define PL_HT_NEW	0x0001
1247 #define PL_HT_UPDATE	0x0002
1248 
1249 PL_EXPORT(hash_table_t)	PL_new_hash_table(int size,
1250 					  void (*free_symbol)(void *n, void *v));
1251 PL_EXPORT(int)		PL_free_hash_table(hash_table_t table);
1252 PL_EXPORT(void*)	PL_lookup_hash_table(hash_table_t table, void *key);
1253 PL_EXPORT(void*)	PL_add_hash_table(hash_table_t table,
1254 					  void *key, void *value, int flags);
1255 PL_EXPORT(void*)	PL_del_hash_table(hash_table_t table, void *key);
1256 PL_EXPORT(int)		PL_clear_hash_table(hash_table_t table);
1257 PL_EXPORT(hash_table_enum_t) PL_new_hash_table_enum(hash_table_t table);
1258 PL_EXPORT(void)		PL_free_hash_table_enum(hash_table_enum_t e);
1259 PL_EXPORT(int)		PL_advance_hash_table_enum(hash_table_enum_t e,
1260 						   void **key, void **value);
1261 
1262 
1263 		 /*******************************
1264 		 *	     PROFILER		*
1265 		 *******************************/
1266 
1267 typedef struct
1268 { int	(*unify)(term_t t, void *handle);	/* implementation --> Prolog */
1269   int   (*get)(term_t t, void **handle);	/* Prolog --> implementation */
1270   void	(*activate)(int active);		/* (de)activate */
1271   intptr_t	magic;					/* PROFTYPE_MAGIC */
1272 } PL_prof_type_t;
1273 
1274 PL_EXPORT(int)		PL_register_profile_type(PL_prof_type_t *type);
1275 PL_EXPORT(void*)	PL_prof_call(void *handle, PL_prof_type_t *type);
1276 PL_EXPORT(void)		PL_prof_exit(void *node);
1277 
1278 
1279 		 /*******************************
1280 		 *	      DEBUG		*
1281 		 *******************************/
1282 
1283 PL_EXPORT(int)		PL_prolog_debug(const char *topic);
1284 PL_EXPORT(int)		PL_prolog_nodebug(const char *topic);
1285 
1286 
1287 		 /*******************************
1288 		 *	 WINDOWS MESSAGES	*
1289 		 *******************************/
1290 
1291 #if defined(_WINDOWS_) || defined(_WINDOWS_H)	/* <windows.h> is included */
1292 #define PL_MSG_EXCEPTION_RAISED -1
1293 #define PL_MSG_IGNORED 0
1294 #define PL_MSG_HANDLED 1
1295 
1296 PL_EXPORT(LRESULT)	PL_win_message_proc(HWND hwnd,
1297 					    UINT message,
1298 					    WPARAM wParam,
1299 					    LPARAM lParam);
1300 #endif /* _WINDOWS_/_WINDOWS_H */
1301 
1302 
1303 		 /*******************************
1304 		 *       FAST XPCE SUPPORT	*
1305 		 *******************************/
1306 
1307 typedef struct
1308 { int type;				/* PL_INTEGER or PL_ATOM */
1309   union
1310   { uintptr_t i;			/* integer reference value */
1311     atom_t	  a;			/* atom reference value */
1312   } value;
1313 } xpceref_t;
1314 
1315 PL_EXPORT(int)	_PL_get_xpce_reference(term_t t, xpceref_t *ref);
1316 PL_EXPORT(int)	_PL_unify_xpce_reference(term_t t, xpceref_t *ref);
1317 PL_EXPORT(int)	_PL_put_xpce_reference_i(term_t t, uintptr_t r);
1318 PL_EXPORT(int)	_PL_put_xpce_reference_a(term_t t, atom_t name);
1319 
1320 
1321 
1322 		 /*******************************
1323 		 *         TRACE SUPPORT	*
1324 		 *******************************/
1325 
1326 #ifndef _PL_INCLUDE_H
1327 typedef void *QueryFrame;
1328 typedef void *LocalFrame;
1329 typedef void *Code;
1330 #endif
1331 
1332 typedef struct pl_context_t
1333 { PL_engine_t   ld;			/* Engine */
1334   QueryFrame	qf;			/* Current query */
1335   LocalFrame	fr;			/* Current localframe */
1336   Code		pc;			/* Code pointer */
1337   void *	reserved[10];		/* Reserved for extensions */
1338 } pl_context_t;
1339 
1340 PL_EXPORT(int)	PL_get_context(struct pl_context_t *c, int thead_id);
1341 PL_EXPORT(int)	PL_step_context(struct pl_context_t *c);
1342 PL_EXPORT(int)	PL_describe_context(struct pl_context_t *c,
1343 				    char *buf, size_t len);
1344 
1345 /* Define as 1 if undefined or defined as empty */
1346 #if !defined(PL_ARITY_AS_SIZE) || (0-PL_ARITY_AS_SIZE-1)==1
1347 #undef PL_ARITY_AS_SIZE
1348 #define PL_ARITY_AS_SIZE 1
1349 #endif
1350 
1351 #if PL_ARITY_AS_SIZE
1352 #define PL_new_functor(f,a) PL_new_functor_sz(f,a)
1353 #define PL_functor_arity(f) PL_functor_arity_sz(f)
1354 #define PL_get_name_arity(t,n,a) PL_get_name_arity_sz(t,n,a)
1355 #define PL_get_compound_name_arity(t,n,a) PL_get_compound_name_arity_sz(t,n,a)
1356 #define PL_get_arg(i,t,a) PL_get_arg_sz(i,t,a)
1357 #define PL_unify_arg(i,t,a) PL_unify_arg_sz(i,t,a)
1358 #ifndef _PL_INCLUDE_H
1359 #define _PL_get_arg(i,t,a) _PL_get_arg_sz(i,t,a)
1360 #endif
1361 #else
1362 #warning "Term arity has changed from int to size_t."
1363 #warning "Please update your code or use #define PL_ARITY_AS_SIZE 0."
1364 #endif
1365 
1366 #ifdef __cplusplus
1367 }
1368 #endif
1369 
1370 #endif /*_FLI_H_INCLUDED*/
1371