YAP 7.1.0
load_shl.c
1
2#include "Yap.h"
3#include "Yatom.h"
4#include "YapHeap.h"
5#include "yapio.h"
6#include "Foreign.h"
7
8#if LOAD_SHL
9
10#include <dl.h>
11#include <malloc.h>
12#include <stdio.h>
13
14/*
15 * YAP_FindExecutable(argv[0]) should be called on yap initialization to
16 * locate the executable of Yap
17*/
18
19char * Yap_FindExecutable(void)
20{
21}
22
23
24void *
25Yap_LoadForeignFile(char *file, int flags)
26{
27 /* not implemented */
28 return NULL;
29}
30
31int
32Yap_CallForeignFile(void *handle, char *f)
33{
34 return FALSE;
35}
36
37int
38Yap_CloseForeignFile(void *handle)
39{
40 return -1;
41}
42
43
44/*
45 * LoadForeign(ofiles,libs,proc_name,init_proc) dynamically loads foreign
46 * code files and libraries and locates an initialization routine
47*/
48
49static Int
50LoadForeign( StringList ofiles, StringList libs,
51 char *proc_name, YapInitProc *init_proc )
52{
53
54 /* *init_proc is initialized to NULL in load_foreign.c */
55 int init_missing = -1;
56
57 int n, i;
58 struct shl_symbol *p;
59
60 while( ofiles ) {
61 int valid_fname;
62
63 /* shl_load wants to follow the LD_CONFIG_PATH */
64 const char *file = AtomName(ofiles->name);
65 valid_fname = Yap_findFile(file, NULL, NULL, LOCAL_FileNameBuf, true, YAP_OBJ, true, true);
66
67 if( !valid_fname ) {
68 strcpy( LOCAL_ErrorSay, "%% Trying to open non-existing file in LoadForeign" );
69 return LOAD_FAILLED;
70 }
71
72 ofiles->handle = Yap_AllocCodeSpace( sizeof(shl_t) );
73 *(shl_t *)ofiles->handle = shl_load( LOCAL_FileNameBuf, BIND_DEFERRED, 0 );
74 if( *(shl_t *)ofiles->handle == NULL ) {
75 strncpy( LOCAL_ErrorSay, strerror(errno), MAX_ERROR_MSG_SIZE );
76 return LOAD_FAILLED;
77 }
78
79 if( init_missing ) {
80 init_missing = shl_findsym( ofiles->handle, proc_name,
81 TYPE_PROCEDURE, init_proc );
82 }
83
84 ofiles = ofiles->next;
85 }
86
87 if( init_missing ) {
88 strcpy( LOCAL_ErrorSay, "Could not locate initialization routine" );
89 return LOAD_FAILLED;
90 }
91
92 while( libs ) {
93 char *s = AtomName(lib->s);
94
95 if( s[0] == '-' ) {
96 strcpy( LOCAL_FileNameBuf, "lib" );
97 strcat( LOCAL_FileNameBuf, s+2 );
98 strcat( LOCAL_FileNameBuf, ".sl" );
99 }
100 else {
101 strcpy( LOCAL_FileNameBuf, s );
102 }
103
104 *(shl_t *)libs->handle = shl_load( LOCAL_FileNameBuf, BIND_DEFERRED, 0 );
105 if( *(shl_t *)libs->handle == NULL ) {
106 strncpy( LOCAL_ErrorSay, strerror(errno), MAX_ERROR_MSG_SIZE );
107 return LOAD_FAILLED;
108 }
109
110 libs = libs->next;
111 }
112
113 return LOAD_SUCCEEDED;
114}
115
116
117Int
118Yap_LoadForeign(StringList ofiles, StringList libs,
119 char *proc_name, YapInitProc *init_proc)
120{
121 return LoadForeign(ofiles, libs, proc_name, init_proc);
122}
123
124void
125Yap_ShutdownLoadForeign( void )
126{
127 ForeignObj *f_code;
128 int err;
129
130 f_code = ForeignCodeLoaded;
131 while( f_code != NULL ) {
132 StringList objs, libs;
133
134 objs = f_code->objs;
135 while( objs ) {
136 err = shl_unload( *(shl_t *)objs->handle );
137 if( err ) {
138 /* dunno how to properly report an error here */
139 perror( NULL );
140 return;
141 }
142 Yap_FreeCodeSpace( objs->handle );
143 objs = objs->next;
144 }
145
146 libs = f_code->libs;
147 while( libs ) {
148 err = shl_unload( *(shl_t *)libs->handle );
149 if( err ) {
150 /* dunno how to properly report an error here */
151 perror( NULL );
152 return;
153 }
154 Yap_FreeCodeSpace( libs->handle );
155 libs = libs->next;
156 }
157 f_code = f_code->next;
158 }
159}
160
161Int
162Yap_ReLoadForeign(StringList ofiles, StringList libs,
163 char *proc_name, YapInitProc *init_proc)
164{
165 ShutdownLoadForeign();
166 return( LoadForeign( ofiles, libs, proc_name, init_proc ) );
167}
168
169/*
170dunno what this one is supposed to do, no load_* defines it
171void ReOpenLoadForeign(void);
172*/
173
174#endif
175
load_foreign_files/3 has works for the following configurations:
Main definitions.