aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/scicore/modules.cpp
blob: f63d4e4aa0843fe36c0ced9b77eceed4e9f3ce99 (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
/***************************************************************************
 modules.c Copyright (C) 2001 Christoph Reichenbach


 This program may be modified and copied freely according to the terms of
 the GNU general public license (GPL), as long as the above copyright
 notice and the licensing information contained herein are preserved.

 Please refer to www.gnu.org for licensing details.

 This work is provided AS IS, without warranty of any kind, expressed or
 implied, including but not limited to the warranties of merchantibility,
 noninfringement, and fitness for a specific purpose. The author will not
 be held liable for any damage caused by this work or derivatives of it.

 By using this source code, you agree to the licensing terms as stated
 above.


 Please contact the maintainer for bug reports or inquiries.

 Current Maintainer:

    Christoph Reichenbach (CR) <jameson@linuxgames.com>

***************************************************************************/

#if 0
#ifndef __BEOS__

#include <sci_memory.h>
#include <modules.h>
#include <dlfcn.h>


static sci_module_t *
_sci_try_open_module(char *filename, char *path, char *struct_name, void **handle)
{
	char *fullname = sci_malloc(strlen(path) + strlen(DIR_SEPARATOR_STR)
				+ strlen(filename));
	sci_module_t *module;
fprintf(stderr,"Trying module %s at %s\n", filename, path);
	strcpy(fullname, path);
	strcat(fullname, DIR_SEPARATOR_STR);
	strcat(fullname, filename);

fprintf(stderr,"Total name is %s\n", fullname);
	*handle = dlopen(fullname, RTLD_NOW);
fprintf(stderr,"Could not open because: %s\n", dlerror());
	free(fullname);

	if (!*handle)
		return NULL;

	module = (sci_module_t *) dlsym(*handle, struct_name);
	if (!module)
		fprintf(stderr,"%s: Failed to find symbol '%s'.\n",
			fullname, struct_name);

	return module;
}

void *
sci_find_module(char *path, char *name, char *type, char *struct_prefix,
		char *file_suffix, int magic, int version, void **handle)
{
	char *module_name = sci_malloc(strlen(type) + strlen(DIR_SEPARATOR_STR)
				 + strlen(name) + strlen(file_suffix)
				 + strlen(MODULE_NAME_SUFFIX) + 1);
	char *struct_name = sci_malloc(strlen(struct_prefix) + strlen(name) + 1);
	char *dir_end;
	char *path_pos = path;
	char path_separator = PATH_SEPARATOR_STR[0];
	sci_module_t *module = NULL;

	strcpy(module_name, type);
	strcat(module_name, DIR_SEPARATOR_STR);
	strcat(module_name, name);
	strcat(module_name, file_suffix);
	strcat(module_name, MODULE_NAME_SUFFIX);

	strcpy(struct_name, struct_prefix);
	strcat(struct_name, name);

	do {
		dir_end = strchr(path_pos, path_separator);
		if (dir_end)
			*dir_end = 0;

		module = _sci_try_open_module(module_name, path_pos,
					      struct_name, handle);

		if (module) {
			if (module->class_magic != magic) {
				fprintf(stderr, "%s at %s is not a %s module, skipping...\n",
					module_name, path_pos, type);
				dlclose(*handle);
				module = NULL;
			} else if (module->class_version != version) {
				fprintf(stderr, "%s at %s has %s module version %d,"
					" expected %d- skipping...\n",
					module_name, path_pos, type, module->class_version,
					version);
				dlclose(*handle);
				module = NULL;
			}
		}

		if (dir_end) {
			*dir_end = path_separator;
			path_pos = dir_end + 1;
		}

	} while (!module && dir_end);

	if (!module) {
		*handle = NULL;
		fprintf(stderr,"%s module '%s' not found in path '%s'.\n",
			type, name, path);
	} else {
		if (dir_end)
			*dir_end = 0;

		printf("Using %s driver '%s', version %s, from '%s'.\n",
		       type, module->module_name, module->module_version,
		       path_pos);

		if (dir_end)
			*dir_end = path_separator;
	}

	free(module_name);
	free(struct_name);

	return (void *) module;
}


void
sci_close_module(void *module, char *type, char *name)
{
	if (!module)
		return;

	if (dlclose(module)) {
		fprintf(stderr,"Error while closing %s module '%s': %s\n",
			type, name, dlerror());
	}
}

#endif /* !__BEOS__ */

#endif /* 0 */