-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsys.cpp
112 lines (109 loc) · 2.85 KB
/
sys.cpp
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
#include "ael.h"
//[doc] loads and dynamic library (c++) from a file path
void _ael_load(aelinterpreter& ael, phrase& _phrase);
#ifdef _WIN32
void _ael_load(aelinterpreter& ael, phrase& _phrase)
{
HINSTANCE dllHandle = NULL;
dllHandle = LoadLibrary(ael.get_value(_phrase[1]).c_str());
if(!dllHandle)
{
// finding the library in Windows's AEL_PATH
char* _ael_path = NULL;
_ael_path = getenv((char*)"AEL_PATH");
if(_ael_path == NULL)
{
std::cerr << "Environment variable not found...please reinstall Ael" << std::endl;
abort();
}
phrase _paths = split(_ael_path, ';');
for(uint i=0;i<_paths.size();i++)
{
tok _possible_path = join(_paths[i], ael.get_value(_phrase[1]));
dllHandle = LoadLibrary(_possible_path.c_str());
if(dllHandle)
break;
}
// not found in absolute path and not found in AEL_PATH
if(!dllHandle)
{
std::cout << "Library '" << ael.get_value(_phrase[1]) << "' not found!" << std::endl;
return;
}
}
aelfunction lib_init;
lib_init = (aelfunction)GetProcAddress(dllHandle, "ael_lib_init");
if(!lib_init)
{
std::cerr << "Library not formated!" << std::endl;
return;
}
lib_init(ael, _phrase);
}
#endif
#ifdef __unix
//[doc] unix implementation
void _ael_load(aelinterpreter& ael, phrase& ph)
{
void* dllHandle = NULL;
dllHandle = dlopen(ael.get_value(ph[1]).c_str(), RTLD_LAZY);
if(!dllHandle)
{
// finding the library in Unix's AEL_PATH
char* _ael_path = NULL;
_ael_path = getenv((char*)"AEL_PATH");
if(_ael_path == NULL)
{
std::cerr << "Environment variable not found...please reinstall Ael" << std::endl;
abort();
}
phrase _paths = split(_ael_path, ';');
for(uint i=0;i<_paths.size();i++)
{
tok _possible_path = join(_paths[i], ael.get_value(ph[1]));
dllHandle = dlopen(_possible_path.c_str(), RTLD_LAZY);
if(dllHandle)
break;
}
// not found in absolute path and not found in AEL_PATH
if(!dllHandle)
{
std::cerr << "Library '" << ael.get_value(ph[1]) << "' not found!" << std::endl;
return;
}
}
aelfunction lib_init;
lib_init = (aelfunction)dlsym(dllHandle, "ael_lib_init");
if(!lib_init)
{
std::cerr << "Library not formated!" << std::endl;
return;
}
lib_init(ael, ph);
// dlclose(dllhandle);
}
#endif
#ifdef __unix
//[doc] returns to a var the process id of program
void _ael_getpid(aelinterpreter& ael, phrase& ph)
{
if(ph.size()!=2)
{
std::cerr << "Error: " << ph[0] << " takes exactly 1 argument (" << ph.size()-1 << " given)" << std::endl;
return;
}
int pid = (int)getpid();
ael.dictionary[ph[1]] = to_string(pid);
}
//[doc] returns to a var the process id of the parent program
void _ael_getppid(aelinterpreter& ael, phrase& ph)
{
if(ph.size()!=2)
{
std::cerr << "Error: " << ph[0] << " takes exactly 1 argument (" << ph.size()-1 << " given)" << std::endl;
return;
}
int pid = (int)getppid();
ael.dictionary[ph[1]] = to_string(pid);
}
#endif