This file is indexed.

/usr/lib/swi-prolog/library/qpforeign.pl is in swi-prolog-nox 5.10.4-3ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
/*  $Id$

    Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        J.Wielemaker@cs.vu.nl
    WWW:           http://www.swi-prolog.org
    Copyright (C): 1985-2010, University of Amsterdam, VU University Amsterdam

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    As a special exception, if you link this library with other files,
    compiled with a Free Software compiler, to produce an executable, this
    library does not by itself cause the resulting executable to be covered
    by the GNU General Public License. This exception does not however
    invalidate any other reasons why the executable file might be covered by
    the GNU General Public License.
*/

:- module(qp_foreign,
	  [ load_foreign_files/0,		%
	    load_foreign_files/2,		% +Files, +Libs
	    load_foreign_files/3,		% +Object, +Files, +Libs
	    make_shared_object/3,		% +Object, +Files, +Libs
	    make_foreign_wrapper_file/1,	% +OutBase
	    make_foreign_wrapper_file/2,	% +OFiles, +OutBase
						% SICStus stuff
	    make_foreign_resource_wrapper/3,    % +Resource, +ResBase, +FileBase
	    load_foreign_resource/2		% +Resource, +Dir
	  ]).

:- use_module(library(shlib)).
:- use_module(library(gensym)).
:- use_module(library(lists)).
:- use_module(library(apply)).
:- use_module(library(error)).

/** <module> Quintus compatible foreign loader

This module defines a  Quintus   compatible  foreign  language interface
based on the foreign_file/2 and foreign/3 declarations.

Predicates:

	* load_foreign_files
	Load all foreign files defined with foreign_file/2 statement
	in the current module.

	* load_foreign_files(+Files, +Libs)
	Load specified foreign files, linking them with the given
	libraries

	* load_foreign_files(+SharedObject, +Files, +Libs)
	As load_foreign_files/2, but first tries to load `SharedObject'.
	If the SharedObject exists it is loaded using load_foreign_library/1.
	Otherwise it is first created with make_shared_object/3.

	* make_shared_object(+SharedObject, +Files, +Libs)
	Generate a wrapper and link it using plld to the given SharedObject.

	* make_foreign_wrapper_file(+Files, +OutBase)
	Generate wrapper for the named object files in OutBase.c.

	* make_foreign_wrapper_file(+Files)
	Generate wrapper for all declared object files in OutBase.c.

Example:

	==
	foreign_file('-lm', [sin/2]).
	foreign(sin, c, sin(+float, [-float])).
	:- load_foreign_files,
	   abolish(foreign_file, 2),
	   abolish(foreign, 3).
	==

Supported types:

	| *Spec*	| *Prolog*	| *C*                           |
	| integer	| integer	| long                          |
	| float		| float,integer	| double                        |
	| single	| float,integer	| single                        |
	| string	| atom,string	| char *			|
	| atom		| atom		| atom identifier (type atomic) |

*NOTE*	This modules requires a correctly functioning plld and
	load_foreign_library/1 on your system.  If this isn't the
	case use make_foreign_wrapper_file/[1,2] to generate a
	wrapper and use static embedding.

*NOTE*	The generated linkN.c file may also be used for creating a
	statically linked executable as described in pl-extend.c

@bug	Only supports C-interface
@bug	Insufficient checking for misusage.
@bug	Documentation is too short and very outdated
*/

:- module_transparent
	load_foreign_files/0.

:- meta_predicate
	load_foreign_files(:, +),
	load_foreign_files(+, :, +),
	make_shared_object(+, :, +),
	make_foreign_wrapper_file(:),
	make_foreign_wrapper_file(:, +),
					% SICStus
	make_foreign_resource_wrapper(:, +),
	load_foreign_resource(:, +).

setting(linker, 'swipl-ld').

hook(M:Goal) :-
	M:Goal.

%%	make_wrappers(+PrologHeads, +Module, +OutStream)

make_wrappers([], _, _).
make_wrappers([H|T], M, Out) :-
	make_wrapper(Out, M:H),
	make_wrappers(T, M, Out).

%%	make_wrapper(+Stream, :PrologHead)
%
%	Generates a C-wrapper function for the given foreign defined
%	Prolog predicate.  The wrapper is called _plw_<predname><arity>.

make_wrapper(Out, Spec) :-
	get_foreign_head(Spec, Func, Head), !,
	(   check_head(Head)
	->  wrapper_name(Head, WrapName, ArgN),
	    make_C_header(Out, WrapName, ArgN),
	    make_C_decls(Out, Head),
	    make_C_prototype(Out, Head),
	    make_C_input_conversions(Out, Head),
	    make_C_wrapper_setup(Out),
	    make_C_call(Out, Head, Func),
	    make_C_wrapper_check(Out),
	    make_C_output_conversions(Out, Head),
	    make_C_footer(Out)
	;   fail
	).
make_wrapper(_, Spec) :-
	existence_error(foreign_declaration, Spec).

%%	get_foreign_head(:Spec, -Func, -Head)
%
%	Get 3rd argument of relevant foreign/3   clause. Seems there are
%	two versions. In Quintus Spec was  a predicate specification and
%	in SICStus it seems to be a (C) function name.

get_foreign_head(M:Function, Function, M:Head) :-
	prolog_load_context(dialect, sicstus), !,
	hook(M:foreign(Function, c, Head)).
get_foreign_head(M:Spec, Func, M:Head) :-
	(   atom(Spec),
	    hook(M:foreign(Spec, c, Head)),
	    functor(Head, Spec, _)
	->  true
	;   Spec = Name/Arity
	->  functor(Head, Name, Arity),
	    hook(M:foreign(Func, c, Head))
	;   Head = Spec,
	    hook(M:foreign(Func, c, Head))
	).


check_head(_:Head) :-
	functor(Head, _, Arity),
	(   arg(N, Head, [-T]),
	    \+ valid_type(T)
	->  warning('Bad return type ~w in ~w', [T, Head]),
	    fail
	;   arg(N, Head, [-T]),
	    N \== Arity
	->  warning('Return type must be last in ~w', Head),
	    fail
	;   (arg(_, Head, -T) ; arg(_, Head, +T)),
	    \+ valid_type(T)
	->  warning('Bad type ~w in ~w', [T, Head]),
	    fail
	;   true
	).

valid_type(int).
valid_type(integer).
valid_type(size_t).
valid_type(float).
valid_type(single).
valid_type(string).
valid_type(chars).			% actually, `codes'!
valid_type(atom).
valid_type(term).
valid_type(address).
valid_type(address(_)).

%%	cvt_name(+Type, +IO, -Suffix) is det.

cvt_name(chars,	     _,	codes) :- !.
cvt_name(address(_), _,	address) :- !.
cvt_name(int,	     o,	int64) :- !.
cvt_name(integer,    o,	int64) :- !.
cvt_name(size_t,     o,	int64) :- !.
cvt_name(integer,    i,	long) :- !.
cvt_name(Type,	     _,	Type).


%%	make_C_header(+Stream, +WrapperName, +Arity)
%
%	Write function-header for the wrapper.  This is easy as the
%	the return-type is always foreign_t and the arguments are
%	always of type `term_t'.  The arguments are simply named `a',
%	`b', ...

make_C_header(Out, WrapName, ArgN) :-
	format(Out, '~n~nstatic foreign_t~n~w(', [WrapName]),
	forall(between(1, ArgN, A),
	       (   (A \== 1 -> format(Out, ', ', []) ; true)
	       ,   arg_name(A, AName),
		   format(Out, 'term_t ~w', [AName])
	       )),
	format(Out, ')~n{ ', []).

%%	make_C_decls(+Stream, :PrologHead)
%
%	Writes the C variable declarations. If  the return value is used
%	a variable named `rval' is created. For each input parameter a C
%	variable named i<argname> is created;   for each output variable
%	o<argname>.

make_C_decls(Out, _:Head) :-
	arg(_, Head, [-PlType]),
	map_C_type(PlType, CType),
	format(Out, '~wrval;~n  ', [CType]),
	fail.
make_C_decls(Out, _:Head) :-
	arg(N, Head, -PlType),
	arg_name(N, AName),
	(   PlType == term
	->  format(Out, 'term_t o_~w = PL_new_term_ref();~n  ', [AName])
	;   map_C_type(PlType, CType),
	    format(Out, '~wo_~w;~n  ', [CType, AName])
	),
	fail.
make_C_decls(Out, _:Head) :-
	arg(N, Head, +PlType),
	PlType \== term,
	map_C_type(PlType, CType),
	CType \== term,
	arg_name(N, AName),
	format(Out, '~wi_~w;~n  ', [CType, AName]),
	fail.
make_C_decls(Out, _) :-
	format(Out, '~n', []).

%%	make_C_prototype(+Stream, :PrologHead)
%
%	If the function handles floats or doubles, make	a prototype
%	declaration for it to avoid unwanted conversions.

make_C_prototype(Out, M:Head) :-
	(   arg(_, Head, [-Type])
	->  map_C_type(Type, CType)
	;   CType = 'void '
	),
	copy_term(Head, H2),		% don't bind Head
	hook(M:foreign(CFunc, c, H2)), !,
	format(Out, '  extern ~w~w(', [CType, CFunc]),
	(   arg(N, Head, AType),
	    AType \= [_],		% return-type
	    (N > 1 -> format(Out, ', ', []) ; true),
	    (   AType = +T2
	    ->  map_C_type(T2, CT2),
		format(Out, '~w', [CT2])
	    ;	AType == -term
	    ->	format(Out, term_t, [])
	    ;   AType = -T2
	    ->  map_C_type(T2, CT2),
		format(Out, '~w *', [CT2])
	    ),
	    fail
        ;   format(Out, ');~n~n', [])
	).
make_C_prototype(_, _).


%%	make_C_input_conversions(+Stream, :PrologHead)
%
%	Generate the input checking and conversion code.  Assumes
%	boolean functions that take a Prolog term_t as first argument
%	and a pointer to the requested C-type as a second argument.
%	Function returns 0 if the conversion fails.

make_C_input_conversions(Out, _:Head) :-
	findall(N-T, arg(N, Head, +T), IArgs0),
	exclude(term_arg, IArgs0, IArgs),
	(   IArgs == []
	->  true
	;   format(Out, '  if ( ', []),
	    (	member(N-T, IArgs),
		T \== term,
		(IArgs \= [N-T|_] -> format(Out, ' ||~n       ', []) ; true),
		arg_name(N, AName),
		atom_concat(i_, AName, IName),
		cvt_name(T, i, CVT),
		format(Out, '!PL_cvt_i_~w(~w, &~w)', [CVT, AName, IName]),
		fail
	    ;	true
	    ),
	    format(Out, ' )~n    return FALSE;~n~n', [])
	).

term_arg(_-term).


%%	make_C_call(+Stream, :PrologHead, +CFunction)
%
%	Generate  the  actual  call  to   the  foreign  function.  Input
%	variables may be handed directly; output  variables as a pointer
%	to the o<var>, except for output-variables of type term.

make_C_call(Out, _:Head, CFunc) :-
	(   arg(_, Head, [-_])
	->  format(Out, '  rval = ~w(', [CFunc])
	;   format(Out, '  (void) ~w(', [CFunc])
	),
	arg(N, Head, Arg),
	Arg \= [_],
	(N \== 1 -> format(Out, ', ', []) ; true),
	arg_name(N, AName),
	(   Arg = -term
	->  format(Out, 'o_~w', [AName])
	;   Arg = -_
	->  format(Out, '&o_~w', [AName])
	;   Arg = +term
	->  format(Out, '~w', [AName])
	;   format(Out, 'i_~w', [AName])
	),
	fail.
make_C_call(Out, _, _) :-
	format(Out, ');~n', []).

%%	make_C_wrapper_setup(+Stream)
%
%	Call SP_WRAP_INIT() when  running  on   SICStus.  This  supports
%	SP_fail() and SP_raise_exception().

make_C_wrapper_setup(Stream) :-
	prolog_load_context(dialect, sicstus), !,
	format(Stream, '  SP_WRAP_INIT();~n', []).
make_C_wrapper_setup(_).


%%	make_C_wrapper_check(+Stream)
%
%	Call  SP_WRAP_CHECK_STATE()  when  running    on  SICStus.  This
%	supports SP_fail() and SP_raise_exception().

make_C_wrapper_check(Stream) :-
	prolog_load_context(dialect, sicstus), !,
	format(Stream, '  SP_WRAP_CHECK_STATE();~n', []).
make_C_wrapper_check(_).


%%	make_C_output_conversions(+Stream, :PrologHead)
%
%	Generate conversions for the output arguments and unify them
%	with the Prolog term_t arguments.

make_C_output_conversions(Out, _:Head) :-
	findall(N-T, arg(N, Head, -T), OArgs0),
	(   arg(_, Head, [-RT])
	->  OArgs = [rval-RT|OArgs0]
	;   OArgs = OArgs0
	),
	(   OArgs == []
	->  true
	;   format(Out, '~n  if ( ', []),
	    (	member(N-T, OArgs),
		(   N == rval
		->  OName = rval,
		    arg(RN, Head, [-_]),
		    arg_name(RN, AName)
		;   arg_name(N, AName),
		    atom_concat(o_, AName, OName)
		),
		(OArgs = [N-T|_] -> true ; format(Out, ' ||~n       ', [])),
		(   T == term
		->  format(Out, '!PL_unify(~w, ~w)', [OName, AName])
		;   cvt_name(T, o, CVT),
		    format(Out, '!PL_cvt_o_~w(~w, ~w)', [CVT, OName, AName])
		),
		fail
	    ;	true
	    ),
	    format(Out, ' )~n    return FALSE;~n', [])
	).


make_C_footer(Out) :-
	format(Out, '~n  return TRUE;~n}~n', []).

		 /*******************************
		 *	  INIT STATEMENT	*
		 *******************************/

%%	make_C_init(+Stream, +InstallFunc, +InitFunc, +Module, +PredList)
%
%	Generate an array of PL_extension structures,   that may be used
%	to create a statically  linked  image   as  well  as through the
%	PL_load_extensions() call.
%
%	Of the supported PL_FA_<FLAGS>, TRANSPARENT   may be declared by
%	looking at the transparent  (meta_predivate)   attribute  of the
%	predicate.

make_C_init(Out, InstallFunc, Init, M, Preds) :-
	format(Out, '~n~nstatic PL_extension predicates [] =~n{~n', []),
	format(Out, '/*{ "name", arity, function, PL_FA_<flags> },*/~n', []),
	(   member(Pred, Preds),
	    get_foreign_head(M:Pred, _Func, Head),
	    Head = M:H,
	    functor(H, Name, Arity),
	    wrapper_name(Head, Wrapper, Arity),
	    foreign_attributes(M:H, Atts),
	    format(Out, '  { "~w", ~d, ~w, ~w },~n',
		   [Name, Arity, Wrapper, Atts]),
	    fail
	;   true
	),
	format(Out, '  { NULL, 0, NULL, 0 } /* terminator */~n};~n~n', []),
	format(Out, 'install_t~n~w()~n{ PL_load_extensions(predicates);~n',
	       [InstallFunc]),
	sicstus_init_function(Out, Init),
	format(Out, '}~n', []).

sicstus_init_function(_, -) :- !.
sicstus_init_function(Out, Init) :-
	format(Out, '  extern void ~w(int);~n', [Init]),
	format(Out, '  ~w(0);~n', [Init]).

foreign_attributes(Head, Atts) :-
	findall(A, foreign_attribute(Head, A), A0),
	(   A0 == []
	->  Atts = 0
	;   atomic_list_concat(A0, '|', Atts)
	).

foreign_attribute(Head, 'PL_FA_TRANSPARENT') :-
	predicate_property(Head, transparent).

%%	make_C_deinit(+Stream, +UninstallFunc, +DeInitFunc) is det.
%
%	Write the uninstall function

make_C_deinit(_, _, -) :- !.
make_C_deinit(Out, Func, DeInit) :-
	format(Out, '~ninstall_t~n', []),
	format(Out, '~w()~n', [Func]),
	format(Out, '{ extern void ~w(int);~n', [DeInit]),
	format(Out, '  ~w(0);~n', [DeInit]),
	format(Out, '}~n', []).


%%	make_C_file_header(+Stream)
%
%	Output the generic header declarations needed and some comments

make_C_file_header(Out) :-
	current_prolog_flag(version_data, swi(Major, Minor, Patch, _)),
	get_time(Time),
	format_time(string(When), '%F %H:%M', Time),
	format(Out, '/*  SWI-Prolog link wrapper~n', []),
	format(Out, '    Generated by SWI-Prolog version ~w.~w.~w~n',
	       [Major, Minor, Patch]),
	format(Out, '    At ~s~n', [When]),
	(   source_location(File, Line)
	->  format(Out, '    Source context ~w:~d~n', [File, Line])
	;   true
	),
	format(Out, '*/~n~n', []),
	format(Out, '#include <SWI-Prolog.h>~n', []),
	make_C_compat_file_header(Out),
	format(Out, '#ifndef NULL~n', []),
	format(Out, '#define NULL ((void *)0)~n', []),
	format(Out, '#endif~n~n', []).


make_C_compat_file_header(Out) :-
	prolog_load_context(dialect, sicstus), !,
	format(Out, '#define SP_WRAPPER 1~n', []),
	format(Out, '#include <sicstus/sicstus.h>~n', []).
make_C_compat_file_header(_).


		 /*******************************
		 *	     TOPLEVEL		*
		 *******************************/

%%	load_foreign_files is det.
%%	load_foreign_files(:Files, +Libs) is det.
%%	load_foreign_files(+SharedObject, :Files, +Libs) is det.
%
%	Calls make_foreign_wrapper_file(+File), compiles the wrapper
%	and loads the predicates.

load_foreign_files :-
	context_module(M),
	findall(File, hook(M:foreign_file(File, _)), OFiles),
	load_foreign_files(M:OFiles, []).
load_foreign_files(OFiles, Libs) :-
	gensym(link, LinkBase),
	load_foreign_files(LinkBase, OFiles, Libs).

load_foreign_files(LinkBase, M:_, _) :-
	catch(load_foreign_library(M:LinkBase), _, fail), !.
load_foreign_files(LinkBase, OFiles, Libs) :-
	make_shared_object(LinkBase, OFiles, Libs),
	OFiles = M:_List,
	load_foreign_library(M:LinkBase).

%%	make_shared_object(+Object, :Files, +Libs) is det.
%
%	Generate  a  wrapper  and  link  it  using  plld  to  the  given
%	SharedObject.

make_shared_object(LinkBase, M:OFiles, Libs) :-
	make_foreign_wrapper_file(M:OFiles, LinkBase),
	file_name_extension(LinkBase, c, CFile),
	build_shared_object(LinkBase, [CFile|OFiles], Libs).

%%	make_foreign_wrapper_file(:OutFile) is det.
%%	make_foreign_wrapper_file(:Files, +OutFile) is det.
%
%	Just output the wrapper file to the named .c file.  May be used
%	to prepare for static linking or the preparation of the native
%	SWI-Prolog foreign-file.

make_foreign_wrapper_file(M:CFile) :-
	findall(File, hook(M:foreign_file(File, _)), OFiles),
	make_foreign_wrapper_file(M:OFiles, CFile).
make_foreign_wrapper_file(M:OFiles, Base) :-
	file_name_extension(Base, c, CFile),
	file_base_name(Base, FuncBase),
	atom_concat(install_, FuncBase, InstallFunc),
	collect_foreign_predicates(OFiles, M, Preds),
	open(CFile, write, Out),
	make_C_file_header(Out),
	make_wrappers(Preds, M, Out),
	make_C_init(Out, InstallFunc, -, M, Preds),
	close(Out).


collect_foreign_predicates([], _, []).
collect_foreign_predicates([File|Files], M, Preds) :-
	hook(M:foreign_file(File, P0)),
	collect_foreign_predicates(Files, M, P1),
	append(P0, P1, Preds).

build_shared_object(Object, Files, Libs) :-
	current_prolog_flag(shared_object_extension, Ext),
	file_name_extension(Object, Ext, SharedObject),
	append(Files, Libs, Input),
	atomic_list_concat(Input, ' ', InputAtom),
	setting(linker, Linker),
	format(string(Command),
	       '~w -shared -o ~w ~w', [Linker, SharedObject, InputAtom]),
	shell(Command).


		 /*******************************
		 *	      SICSTUS		*
		 *******************************/

%%	make_foreign_resource_wrapper(:Resource, +ResBase, +FileBase)
%
%	Create a wrapper-file for the given foreign resource

make_foreign_resource_wrapper(M:Resource, ResBase, FileBase) :-
	hook(M:foreign_resource(Resource, Functions)),
	take(init(Init), Functions, Functions1, -),
	take(deinit(DeInit), Functions1, Preds, -),
	file_name_extension(FileBase, c, CFile),
	file_base_name(ResBase, FuncBase),
	atom_concat(install_, FuncBase, InstallFunc),
	atom_concat(uninstall_, FuncBase, UninstallFunc),
	open(CFile, write, Out),
	make_C_file_header(Out),
	make_wrappers(Preds, M, Out),
	make_C_init(Out, InstallFunc, Init, M, Preds),
	make_C_deinit(Out, UninstallFunc, DeInit),
	close(Out).

take(Term, List, Rest, Default) :-
	(   select(Term, List, Rest)
	->  true
	;   arg(1, Term, Default),
	    Rest = List
	).


%%	load_foreign_resource(:Resource, +Dir)
%
%	Load a foreign module. First try to  load from the same direcory
%	as the Prolog file. Otherwise   load  using SWI-Prolog's default
%	search path.

load_foreign_resource(M:Resource, Source) :-
	absolute_file_name(Resource, Object,
			   [ file_type(executable),
			     relative_to(Source),
			     file_errors(fail)
			   ]), !,
	load_foreign_library(M:Object).
load_foreign_resource(M:Resource, _) :-
	load_foreign_library(M:foreign(Resource)).


		 /*******************************
		 *	       UTIL		*
		 *******************************/

arg_name(N, Name) :-
	C is N + 0'a - 1,
	atom_codes(Name, [C]).

wrapper_name(_:Head, Wrapper, Arity) :-
	functor(Head, Name, Arity),
	atomic_list_concat(['_plw_', Name, Arity], Wrapper).

%%	map_C_type(+Prolog, -C)
%
%	Map Prolog interface type declarations into C types.

map_C_type(X, Y) :-
	map_C_type_(X, Y), !.
map_C_type(X, X).

map_C_type_(int, 'int ').
map_C_type_(integer, 'long ').
map_C_type_(size_t, 'size_t ').
map_C_type_(float,   'double ').
map_C_type_(string,  'char *').
map_C_type_(chars,   'char *').
map_C_type_(address, 'void *').
map_C_type_(address(Of), Type) :-
	atom_concat(Of, ' *', Type).
map_C_type_(term,    'term_t ').

warning(Fmt, Args) :-
	print_message(warning, format(Fmt, Args)).