summaryrefslogtreecommitdiff
path: root/src/lib/libdll/dll_lib.c
blob: 29e4cd8b99625c2b3ab7436e08c2d0f1d70b537f (plain)
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
/***********************************************************************
*                                                                      *
*               This software is part of the ast package               *
*          Copyright (c) 1997-2011 AT&T Intellectual Property          *
*                      and is licensed under the                       *
*                 Eclipse Public License, Version 1.0                  *
*                    by AT&T Intellectual Property                     *
*                                                                      *
*                A copy of the License is available at                 *
*          http://www.eclipse.org/org/documents/epl-v10.html           *
*         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
*                                                                      *
*              Information and Software Systems Research               *
*                            AT&T Research                             *
*                           Florham Park NJ                            *
*                                                                      *
*                 Glenn Fowler <gsf@research.att.com>                  *
*                                                                      *
***********************************************************************/
#pragma prototyped
/*
 * Glenn Fowler
 * AT&T Research
 */

#include "dlllib.h"

typedef void* (*Dll_lib_f)(const char*, void*, const char*);

typedef struct Dll_lib_s
{
	struct Dll_lib_s*	next;
	Dll_lib_f		libf;
	char*			path;
	char			base[1];
} Dll_lib_t;

/*
 * split <name,base,type,opts> from name into names
 */

Dllnames_t*
dllnames(const char* id, const char* name, Dllnames_t* names)
{
	char*	s;
	char*	t;
	char*	b;
	char*	e;
	size_t	n;

	n = strlen(id);
	if (strneq(name, id, n) && (streq(name + n, "_s") || streq(name + n, "_t")))
		return 0;
	if (!names)
	{
		s = fmtbuf(sizeof(Dllnames_t*) + sizeof(names) - 1);
		if (n = (s - (char*)0) % sizeof(names))
			s += sizeof(names) - n;
		names = (Dllnames_t*)s;
	}

	/*
	 * determine the base name
	 */

	if ((s = strrchr(name, '/')) || (s = strrchr(name, '\\')))
		s++;
	else
		s = (char*)name;
	if (strneq(s, "lib", 3))
		s += 3;
	b = names->base = names->data;
	e = b + sizeof(names->data) - 1;
	t = s;
	while (b < e && *t && *t != '.' && *t != '-' && *t != ':')
		*b++ = *t++;
	*b++ = 0;

	/*
	 * determine the optional type
	 */

	if (t = strrchr(s, ':'))
	{
		names->name = b;
		while (b < e && s < t)
			*b++ = *s++;
		*b++ = 0;
		names->type = b;
		while (b < e && *++t)
			*b++ = *t;
		*b++ = 0;
	}
	else
	{
		names->name = (char*)name;
		names->type = 0;
	}
	*(names->path = b) = 0;
	names->opts = 0;
	names->id = (char*)id;
	return names;
}

/*
 * return method pointer for <id,version> in names
 */

void*
dll_lib(Dllnames_t* names, unsigned long version, Dllerror_f errorf, void* disc)
{
	void*			dll;
	Dll_lib_t*		lib;
	Dll_lib_f		libf;
	int			n;
	char			sym[64];

	static Dll_lib_t*	loaded;

	if (!names)
		return 0;

	/*
	 * check if plugin already loaded
	 */

	for (lib = loaded; lib; lib = lib->next)
		if (streq(names->base, lib->base))
		{
			libf = lib->libf;
			goto init;
		}

	/*
	 * load
	 */

	if (!(dll = dllplugin(names->id, names->name, NiL, version, NiL, RTLD_LAZY, names->path, names->data + sizeof(names->data) - names->path)) && (streq(names->name, names->base) || !(dll = dllplugin(names->id, names->base, NiL, version, NiL, RTLD_LAZY, names->path, names->data + sizeof(names->data) - names->path))))
	{
		if (errorf)
			(*errorf)(NiL, disc, 2, "%s: library not found", names->name);
		return 0;
	}

	/*
	 * init
	 */

	sfsprintf(sym, sizeof(sym), "%s_lib", names->id);
	if (!(libf = (Dll_lib_f)dlllook(dll, sym)))
	{
		if (errorf)
			(*errorf)(NiL, disc, 2, "%s: %s: initialization function not found in library", names->path, sym);
		return 0;
	}

	/*
	 * add to the loaded list
	 */

	if (lib = newof(0, Dll_lib_t, 1, (n = strlen(names->base)) + strlen(names->path) + 1))
	{
		lib->libf = libf;
		strcpy(lib->base, names->base);
		strcpy(lib->path = lib->base + n + 1, names->path);
		lib->next = loaded;
		loaded = lib;
	}
 init:
	return (*libf)(names->path, disc, names->type);
}

/*
 * return method pointer for <id,name,version>
 */

void*
dllmeth(const char* id, const char* name, unsigned long version)
{
	Dllnames_t	names;

	return dll_lib(dllnames(id, name, &names), version, 0, 0);
}