This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathstatic.c
105 lines (84 loc) · 2.2 KB
/
static.c
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
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#ifdef HAVE_KDBCONFIG_H
#include "kdbconfig.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <exported_symbols.h>
#include <kdberrors.h>
#include <kdbmodule.h>
int elektraModulesInit (KeySet * modules, Key * error ELEKTRA_UNUSED)
{
ksAppendKey (modules, keyNew ("system:/elektra/modules", KEY_END));
return 0;
}
static kdblib_symbol * elektraStaticLoad (const char * module)
{
kdblib_symbol * current;
current = kdb_exported_syms;
while (current->name != NULL)
{
/* Skip symbols, we're searching for
* the module name */
if (current->function == NULL && strcmp (current->name, module) == 0)
{
/* Go to the first symbol for this file */
current++;
return current;
}
current++;
}
return NULL;
}
static kdblib_symbol * elektraStaticSym (kdblib_symbol * handle, const char * symbol)
{
kdblib_symbol * current;
current = handle;
/* For each symbol about this module */
while (current->function != NULL)
{
if (strcmp (current->name, symbol) == 0) return current;
current++;
}
return NULL;
}
elektraPluginFactory elektraModulesLoad (KeySet * modules, const char * name, Key * error)
{
Key * moduleKey = keyNew ("system:/elektra/modules", KEY_END);
keyAddBaseName (moduleKey, name);
Key * lookup = ksLookup (modules, moduleKey, 0);
if (lookup)
{
kdblib_symbol * module = (kdblib_symbol *) keyValue (lookup);
keyDel (moduleKey);
return (elektraPluginFactory) module->function;
}
kdblib_symbol * handle = elektraStaticLoad (name);
if (handle == NULL)
{
ELEKTRA_ADD_INSTALLATION_WARNINGF (error, "Did not find module: %s", name);
keyDel (moduleKey);
return 0;
}
kdblib_symbol * module = elektraStaticSym (handle, "elektraPluginSymbol");
if (module == NULL)
{
ELEKTRA_ADD_INSTALLATION_WARNING (
error, "Could not get pointer to factory, static sym failed: no such symbol elektraPluginSymbol");
return 0;
}
keySetBinary (moduleKey, module, sizeof (kdblib_symbol));
ksAppendKey (modules, moduleKey);
return (elektraPluginFactory) module->function;
}
int elektraModulesClose (KeySet * modules, Key * error ELEKTRA_UNUSED)
{
ksClear (modules);
return 0;
}