File: slimport.c

package info (click to toggle)
slang2 2.3.1-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,580 kB
  • ctags: 10,851
  • sloc: ansic: 96,830; sh: 3,373; makefile: 1,033; pascal: 143
file content (420 lines) | stat: -rw-r--r-- 10,086 bytes parent folder | download
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
/*
Copyright (C) 2004-2016 John E. Davis

This file is part of the S-Lang Library.

The S-Lang Library 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.

The S-Lang Library 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 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.
*/

#include "slinclud.h"

#include "slang.h"
#include "_slang.h"

#if defined (HAVE_DLFCN_H) || defined(__WIN32__)
# define SLANG_HAS_DYNAMIC_LINKING	1
#else
# define SLANG_HAS_DYNAMIC_LINKING	0
#endif

/* The rest of this file is in the if block */
#if SLANG_HAS_DYNAMIC_LINKING

#ifdef HAVE_DLFCN_H
# include <dlfcn.h>
#endif

#ifdef __WIN32__
# include <windows.h>
# define dlopen(a, b) LoadLibrary(a)
# define dlsym (void *)GetProcAddress
# define dlclose FreeLibrary
# define dlerror() NULL
# define SO_SUFFIX "dll"
#else
# define SO_SUFFIX "so"
#endif

static SLFUTURE_CONST char *Module_Path;
#ifndef MODULE_PATH_ENV_NAME
# define MODULE_PATH_ENV_NAME "SLANG_MODULE_PATH"
#endif
#ifndef MODULE_INSTALL_DIR
# define MODULE_INSTALL_DIR "/usr/local/lib/slang/v2/modules"
#endif

#define MAX_MODULE_NAME_SIZE 256

typedef struct Namespace_List_Type
{
   char *ns;
   struct Namespace_List_Type *next;
}
Namespace_List_Type;

typedef struct _Handle_Type
{
   struct _Handle_Type *next;
   char *module_name;
   VOID_STAR handle;
   int (*ns_init_fun) (SLCONST char *);
   void (*deinit_fun) (void);
   Namespace_List_Type *ns_list;
}
Handle_Type;

static Handle_Type *Handle_List;

static void free_namespace_list (Namespace_List_Type *ns_list)
{
   while (ns_list != NULL)
     {
	Namespace_List_Type *next = ns_list->next;
	SLang_free_slstring (ns_list->ns);
	SLfree ((char *)ns_list);
	ns_list = next;
     }
}

static void free_handle_type (Handle_Type *h)
{
   if (h == NULL)
     return;

   SLang_free_slstring (h->module_name);
   free_namespace_list (h->ns_list);
   SLfree ((char *)h);
}

static void delete_handles (void)
{
   while (Handle_List != NULL)
     {
	Handle_Type *next = Handle_List->next;

	if (Handle_List->deinit_fun != NULL)
	  Handle_List->deinit_fun ();
	/* (void) dlclose (Handle_List->handle); */
	free_handle_type (Handle_List);
	Handle_List = next;
     }
}

static Handle_Type *allocate_handle_type (SLFUTURE_CONST char *module_name, VOID_STAR handle)
{
   Handle_Type *h;

   h = (Handle_Type *) SLcalloc (1, sizeof (Handle_Type));
   if (h == NULL)
     return NULL;
   if (NULL == (h->module_name = SLang_create_slstring (module_name)))
     {
	SLfree ((char *) h);
	return NULL;
     }
   h->handle = handle;
   return h;
}

static Handle_Type *find_handle (SLCONST char *module_name)
{
   Handle_Type *l;

   l = Handle_List;
   while (l != NULL)
     {
	if (0 == strcmp (l->module_name, module_name))
	  break;
	l = l->next;
     }
   return l;
}

static int check_api_version (char *file, int api_version)
{
   if (api_version/10000 == SLANG_VERSION/10000)
     return 0;

   _pSLang_verror (SL_Import_Error, "Module %s is incompatible with this version of S-Lang",
		 file);
   return -1;
}

#if defined(__GNUC__)
# pragma GCC diagnostic ignored "-Wformat-nonliteral"
#endif
static VOID_STAR do_dlsym (VOID_STAR handle, SLFUTURE_CONST char *file, int check_error, SLFUTURE_CONST char *fmt, char *module)
{
   char symbol[MAX_MODULE_NAME_SIZE + 32];
   VOID_STAR s;

   SLsnprintf (symbol, sizeof(symbol), fmt, module);
   if (NULL != (s = (VOID_STAR) dlsym (handle, symbol)))
     return s;

   if (check_error)
     {
	SLCONST char *err;

	if (NULL == (err = (char *) dlerror ()))
	  err = "UNKNOWN";

	_pSLang_verror (SL_Import_Error,
		      "Unable to get symbol %s from %s: %s",
		      symbol, file, err);
     }
   return NULL;
}
#if defined(__GNUC__)
# pragma GCC diagnostic warning "-Wformat-nonliteral"
#endif

static Handle_Type *dynamic_link_module (SLFUTURE_CONST char *module)
{
   Handle_Type *h;
   VOID_STAR handle;
   SLFUTURE_CONST char *err;
   char filebuf[1024];
   char *save_file;
   char *save_err;
   int api_version;
   int *api_version_ptr;
#define MAX_MODULE_NAME_SIZE 256
   char module_so[MAX_MODULE_NAME_SIZE + 32];
   char *module_name;
   char *file, *pathfile;

   if (strlen (module) >= MAX_MODULE_NAME_SIZE)
     {
	_pSLang_verror (SL_LimitExceeded_Error, "module name too long");
	return NULL;
     }
   SLsnprintf (module_so, sizeof(module_so), "%s-module.%s", module, SO_SUFFIX);

   if (Module_Path != NULL)
     pathfile = SLpath_find_file_in_path (Module_Path, module_so);
   else pathfile = NULL;

   if ((pathfile == NULL)
       && (NULL != (pathfile = _pSLsecure_getenv (MODULE_PATH_ENV_NAME))))
     pathfile = SLpath_find_file_in_path (pathfile, module_so);

   if (pathfile == NULL)
     pathfile = SLpath_find_file_in_path (MODULE_INSTALL_DIR, module_so);

   if (pathfile != NULL)
     file = pathfile;
   else
     file = module_so;

   save_err = NULL;
   save_file = file;
   while (1)
     {
#ifndef RTLD_GLOBAL
# define RTLD_GLOBAL 0
#endif
#ifdef RTLD_NOW
	handle = (VOID_STAR) dlopen (file, RTLD_NOW | RTLD_GLOBAL);
#else
	handle = (VOID_STAR) dlopen (file, RTLD_LAZY | RTLD_GLOBAL);
#endif

	if (handle != NULL)
	  {
	     if (_pSLang_Load_File_Verbose & SLANG_LOAD_MODULE_VERBOSE)
	       SLang_vmessage ("Importing %s", file);
	     if (save_err != NULL)
	       SLfree (save_err);
	     break;
	  }

	/* Purify reports that dlerror returns a pointer that generates UMR
	 * errors.  There is nothing that I can do about that....
	 */
	if ((NULL == strchr (file, '/'))
	    && (strlen(file) < sizeof(filebuf)))
	  {
	     err = (char *) dlerror ();
	     if (err != NULL)
	       save_err = SLmake_string (err);

	     SLsnprintf (filebuf, sizeof (filebuf), "./%s", file);
	     file = filebuf;
	     continue;
	  }

	if ((NULL == (err = save_err))
	    && (NULL == (err = (char *) dlerror ())))
	  err = "UNKNOWN";

	_pSLang_verror (SL_Import_Error,
		      "Error linking to %s: %s", save_file, err);

	if (save_err != NULL)
	  SLfree (save_err);
	if (pathfile != NULL)
	  SLfree (pathfile);

	return NULL;
     }

   /* Using SLpath_basename allows, e.g., import ("/path/to/module"); */
   module_name = SLpath_basename (module);

   api_version_ptr = (int *) do_dlsym (handle, file, 0, "SLmodule_%s_api_version", module_name);
   if (api_version_ptr == NULL)
     api_version_ptr = (int *) do_dlsym (handle, file, 0, "_SLmodule_%s_api_version", module_name);

   if (api_version_ptr == NULL)
     api_version = 0;
   else
     api_version = *api_version_ptr;

   if ((-1 == check_api_version (file, api_version))
       || (NULL == (h = allocate_handle_type (module, handle))))
     {
	SLfree (pathfile);	       /* NULL ok */
	dlclose (handle);
	return NULL;
     }

   if (NULL == (h->ns_init_fun = (int (*)(SLCONST char *)) do_dlsym (handle, file, 1, "init_%s_module_ns", module_name)))
     {
	SLfree (pathfile);
	free_handle_type (h);
	dlclose (handle);
	return NULL;
     }
   h->deinit_fun = (void (*)(void)) do_dlsym (handle, file, 0, "deinit_%s_module", module_name);

   SLfree (pathfile);		       /* NULL ok */
   h->next = Handle_List;
   Handle_List = h;

   return h;
}

static int import_module (SLFUTURE_CONST char *module, SLFUTURE_CONST char *ns)
{
   Handle_Type *h;
   Namespace_List_Type *ns_list;

   if (ns == NULL)
     ns = _pSLang_cur_namespace_intrinsic ();

   if (*ns == 0)
     ns = "Global";

   if ((NULL == (h = find_handle (module)))
       && (NULL == (h = dynamic_link_module (module))))
     return -1;

   ns_list = h->ns_list;
   while (ns_list != NULL)
     {
	if (0 == strcmp (ns, ns_list->ns))
	  return 0;		       /* already linked to this ns */
	ns_list = ns_list->next;
     }

   if (NULL == (ns_list = (Namespace_List_Type *)SLmalloc (sizeof (Namespace_List_Type))))
     return -1;

   if (NULL == (ns_list->ns = SLang_create_slstring (ns)))
     {
	SLfree ((char *)ns_list);
	return -1;
     }
   ns_list->next = h->ns_list;
   h->ns_list = ns_list;

   if (-1 == (h->ns_init_fun (ns)))
     return -1;

   return 0;
}

static void import_module_intrin (void)
{
   char *module;
   char *ns = NULL;

   if (SLang_Num_Function_Args == 2)
     {
	if (-1 == SLang_pop_slstring (&ns))
	  return;
     }

   if (-1 == SLang_pop_slstring (&module))
     {
	SLang_free_slstring (ns);      /* NULL ok */
	return;
     }

   (void) import_module (module, ns);
   SLang_free_slstring (module);
   SLang_free_slstring (ns);	       /* NULL ok */
}

static void set_import_module_path (char *path)
{
   (void) SLang_set_module_load_path (path);
}

static SLCONST char *get_import_module_path (void)
{
   char *path;
   if (Module_Path != NULL)
     return Module_Path;
   if (NULL != (path = _pSLsecure_getenv (MODULE_PATH_ENV_NAME)))
     return path;
   return MODULE_INSTALL_DIR;
}

static SLang_Intrin_Fun_Type Module_Intrins [] =
{
   MAKE_INTRINSIC_0("import", import_module_intrin, SLANG_VOID_TYPE),
   MAKE_INTRINSIC_S("set_import_module_path", set_import_module_path, SLANG_VOID_TYPE),
   MAKE_INTRINSIC_0("get_import_module_path", get_import_module_path, SLANG_STRING_TYPE),
   SLANG_END_INTRIN_FUN_TABLE
};

#endif				       /* SLANG_HAS_DYNAMIC_LINKING */

int SLang_set_module_load_path (SLFUTURE_CONST char *path)
{
#if SLANG_HAS_DYNAMIC_LINKING
   if (NULL == (path = SLang_create_slstring (path)))
     return -1;
   SLang_free_slstring ((char *) Module_Path);
   Module_Path = path;
   return 0;
#else
   (void) path;
   return -1;
#endif
}

int SLang_init_import (void)
{
#if SLANG_HAS_DYNAMIC_LINKING
   (void) SLang_add_cleanup_function (delete_handles);
   return SLadd_intrin_fun_table (Module_Intrins, "__IMPORT__");
#else
   return 0;
#endif
}