Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Testing latest pari + WASM + node.js... and it works?! Wow.

28495 views
License: GPL3
ubuntu2004
1
#include "dlfcn.h"
2
3
#define INCL_BASE
4
#include <os2.h>
5
#include <float.h>
6
#include <stdlib.h>
7
#include <string.h>
8
#include <stdio.h>
9
10
static ULONG retcode;
11
static char fail[300];
12
13
static ULONG dllHandle;
14
static char dllname[80];
15
static int handle_found;
16
static int handle_loaded;
17
18
#ifdef DLOPEN_INITTERM
19
unsigned long _DLL_InitTerm(unsigned long modHandle, unsigned long flag)
20
{
21
switch (flag) {
22
case 0: /* INIT */
23
/* Save handle */
24
dllHandle = modHandle;
25
handle_found = 1;
26
return TRUE;
27
28
case 1: /* TERM */
29
handle_found = 0;
30
dllHandle = (unsigned long)NULLHANDLE;
31
return TRUE;
32
}
33
return FALSE;
34
}
35
#endif
36
37
HMODULE
38
find_myself(void)
39
{
40
static APIRET APIENTRY (*pDosQueryModFromEIP)(HMODULE * hmod, ULONG * obj, ULONG BufLen, PCHAR Buf, ULONG * Offset, ULONG Address);
41
HMODULE doscalls_h, mod;
42
static int failed = 0;
43
ULONG obj, offset, rc;
44
char buf[260];
45
46
if (failed) return 0;
47
failed = 1;
48
doscalls_h = (HMODULE)dlopen("DOSCALLS",0);
49
if (!doscalls_h) return 0;
50
/* {&doscalls_handle, NULL, 360}, */ /* DosQueryModFromEIP */
51
rc = DosQueryProcAddr(doscalls_h, 360, 0, (PFN*)&pDosQueryModFromEIP);
52
if (rc) return 0;
53
rc = pDosQueryModFromEIP(&mod, &obj, sizeof(buf), buf, &offset, (ULONG)dlopen);
54
if (rc) return 0;
55
failed = 0;
56
handle_found = 1;
57
dllHandle = mod;
58
return mod;
59
}
60
61
void *
62
dlopen(char *path, int mode)
63
{
64
HMODULE handle;
65
char tmp[260], *beg, *dot;
66
ULONG rc;
67
unsigned fpflag = _control87(0,0);
68
69
fail[0] = 0;
70
if (!path) { /* Our own handle. */
71
if (handle_found || find_myself()) {
72
if (handle_loaded) return (void*)dllHandle;
73
rc = DosQueryModuleName(dllHandle, sizeof(dllname), dllname);
74
if (rc) {
75
strcpy(fail, "can't find my DLL name by the handle");
76
retcode = rc; return 0;
77
}
78
rc = DosLoadModule((PSZ)fail, sizeof fail, (PSZ)dllname, &handle);
79
if (rc) {
80
strcpy(fail, "can't load my own DLL");
81
retcode = rc; return 0;
82
}
83
handle_loaded = 1;
84
goto ret;
85
}
86
retcode = ERROR_MOD_NOT_FOUND;
87
strcpy(fail, "can't load from myself: compiled without -DDLOPEN_INITTERM");
88
return 0;
89
}
90
if ((rc = DosLoadModule((PSZ)fail, sizeof fail, (PSZ)path, &handle)) == 0)
91
goto ret;
92
93
retcode = rc;
94
/* Not found. Check for non-FAT name and try truncated name. */
95
/* Don't know if this helps though... */
96
for (beg = dot = path + strlen(path);
97
beg > path && !strchr(":/\\", *(beg-1));
98
beg--)
99
if (*beg == '.') dot = beg;
100
if (dot - beg > 8) {
101
int n = beg+8-path;
102
memmove(tmp, path, n);
103
memmove(tmp+n, dot, strlen(dot)+1);
104
rc = DosLoadModule((PSZ)fail, sizeof fail, (PSZ)tmp, &handle);
105
if (rc == 0) goto ret;
106
retcode = rc;
107
}
108
handle = 0;
109
110
ret:
111
_control87(fpflag, MCW_EM); /* Some modules reset FP flags on load */
112
return (void *)handle;
113
}
114
115
#define ERROR_WRONG_PROCTYPE 0xffffffff
116
117
void *
118
dlsym(void *handle, char *symbol)
119
{
120
ULONG rc, type;
121
PFN addr;
122
123
fail[0] = 0;
124
rc = DosQueryProcAddr((HMODULE)handle, 0, (PSZ)symbol, &addr);
125
if (rc == 0) {
126
rc = DosQueryProcType((HMODULE)handle, 0, (PSZ)symbol, &type);
127
if (rc == 0 && type == PT_32BIT) return (void *)addr;
128
rc = ERROR_WRONG_PROCTYPE;
129
}
130
retcode = rc; return NULL;
131
}
132
133
char *
134
dlerror(void)
135
{
136
static char buf[700];
137
ULONG len;
138
139
if (retcode == 0) return NULL;
140
if (retcode == ERROR_WRONG_PROCTYPE) {
141
strcpy(buf, "Wrong procedure type");
142
len = strlen(buf);
143
}
144
if ((retcode != ERROR_WRONG_PROCTYPE)
145
&& DosGetMessage(NULL, 0, buf, sizeof buf - 1, retcode,
146
(PSZ)"OSO001.MSG", &len)) {
147
if (fail[0])
148
sprintf(buf, "OS/2 system error code %d, problematic module: '%s'",
149
(int)retcode, fail);
150
else
151
sprintf(buf, "OS/2 system error code %d", (int)retcode);
152
} else {
153
buf[len] = '\0';
154
if (len && buf[len - 1] == '\n') buf[--len] = 0;
155
if (len && buf[len - 1] == '\r') buf[--len] = 0;
156
if (len && buf[len - 1] == '.') buf[--len] = 0;
157
if (fail[0] && len < 300)
158
sprintf(buf + len, ", problematic module: '%s'", fail);
159
}
160
retcode = 0; return buf;
161
}
162
163
int
164
dlclose(void *handle)
165
{
166
ULONG rc;
167
168
if ((rc = DosFreeModule((HMODULE)handle)) == 0) return 0;
169
retcode = rc; return 2;
170
}
171
172
void*
173
get_stack(double fraction, long min)
174
{
175
int rc;
176
TIB *tib;
177
PIB *pib;
178
char *s, *e;
179
unsigned long d;
180
181
if (!(_emx_env & 0x200)) return 0; /* not OS/2. */
182
rc = DosGetInfoBlocks(&tib, &pib);
183
if (rc) return 0; /* ignore error */
184
s = (char*)tib->tib_pstack;
185
e = (char*)tib->tib_pstacklimit;
186
d = fraction * (e-s);
187
if (min >= 3*(e-s)/4) min = 3*(e-s)/4;
188
if (d < min) d = min;
189
return (void*)(s + d);
190
}
191
192