1
2
3
4
5
6
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
30
31
32
33
37
38
42
43
44
45
46
47
48
49
50
53
56
57
60
63
64
65
69
70
71
72
77
78
79
80
81
82
83
84
87
88
89
90
94
95
105
106
107
108
109
110
111
112
113
114
115
116
119
120
121
122
130
131
#include <string.h>
#include "jimautoconf.h"
#include <jim.h>
/* ... */
#if defined(HAVE_DLOPEN) || defined(HAVE_DLOPEN_COMPAT)
#ifdef HAVE_DLFCN_H
#include <dlfcn.h>
#endif
#ifndef RTLD_NOW
#define RTLD_NOW 0
#endif
#ifndef RTLD_LOCAL
#define RTLD_LOCAL 0
#endif
static void JimFreeLoadHandles(Jim_Interp *interp, void *data);
/* ... */
int Jim_LoadLibrary(Jim_Interp *interp, const char *pathName)
{
void *handle = dlopen(pathName, RTLD_NOW | RTLD_LOCAL);
if (handle == NULL) {
Jim_SetResultFormatted(interp, "error loading extension \"%s\": %s", pathName,
dlerror());
}if (handle == NULL) { ... }
else {
/* ... */
const char *pt;
const char *pkgname;
int pkgnamelen;
char initsym[40];
typedef int jim_module_init_func_type(Jim_Interp *);
jim_module_init_func_type *onload;
pt = strrchr(pathName, '/');
if (pt) {
pkgname = pt + 1;
}if (pt) { ... }
else {
pkgname = pathName;
}else { ... }
pt = strchr(pkgname, '.');
if (pt) {
pkgnamelen = pt - pkgname;
}if (pt) { ... }
else {
pkgnamelen = strlen(pkgname);
}else { ... }
snprintf(initsym, sizeof(initsym), "Jim_%.*sInit", pkgnamelen, pkgname);
if ((onload = (jim_module_init_func_type *)dlsym(handle, initsym)) == NULL) {
Jim_SetResultFormatted(interp,
"No %s symbol found in extension %s", initsym, pathName);
}if ((onload = (jim_module_init_func_type *)dlsym(handle, initsym)) == NULL) { ... }
else if (onload(interp) != JIM_ERR) {
Jim_Stack *loadHandles = Jim_GetAssocData(interp, "load::handles");
if (loadHandles == NULL) {
loadHandles = Jim_Alloc(sizeof(*loadHandles));
Jim_InitStack(loadHandles);
Jim_SetAssocData(interp, "load::handles", JimFreeLoadHandles, loadHandles);
}if (loadHandles == NULL) { ... }
Jim_StackPush(loadHandles, handle);
Jim_SetEmptyResult(interp);
return JIM_OK;
}else if (onload(interp) != JIM_ERR) { ... }
}else { ... }
if (handle) {
dlclose(handle);
}if (handle) { ... }
return JIM_ERR;
}{ ... }
static void JimFreeOneLoadHandle(void *handle)
{
dlclose(handle);
}{ ... }
static void JimFreeLoadHandles(Jim_Interp *interp, void *data)
{
Jim_Stack *handles = data;
if (handles) {
Jim_FreeStackElements(handles, JimFreeOneLoadHandle);
Jim_FreeStack(handles);
Jim_Free(handles);
}if (handles) { ... }
}{ ... }
/* ... */#else
int Jim_LoadLibrary(Jim_Interp *interp, const char *pathName)
{
JIM_NOTUSED(interp);
JIM_NOTUSED(pathName);
Jim_SetResultString(interp, "the Jim binary has no support for [load]", -1);
return JIM_ERR;
}Jim_LoadLibrary (Jim_Interp *interp, const char *pathName) { ... }
void Jim_FreeLoadHandles(Jim_Interp *interp)
{
}Jim_FreeLoadHandles (Jim_Interp *interp) { ... }
/* ... */#endif
static int Jim_LoadCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
if (argc < 2) {
Jim_WrongNumArgs(interp, 1, argv, "libraryFile");
return JIM_ERR;
}if (argc < 2) { ... }
return Jim_LoadLibrary(interp, Jim_String(argv[1]));
}{ ... }
int Jim_loadInit(Jim_Interp *interp)
{
Jim_CreateCommand(interp, "load", Jim_LoadCoreCommand, NULL, NULL);
return JIM_OK;
}{ ... }