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
/* Copyright (C) 2009 The PARI group.
2
3
This file is part of the PARI/GP package.
4
5
PARI/GP is free software; you can redistribute it and/or modify it under the
6
terms of the GNU General Public License as published by the Free Software
7
Foundation; either version 2 of the License, or (at your option) any later
8
version. It is distributed in the hope that it will be useful, but WITHOUT
9
ANY WARRANTY WHATSOEVER.
10
11
Check the License for details. You should have received a copy of it, along
12
with the package; see the file 'COPYING'. If not, write to the Free Software
13
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
14
15
/* Originally written by Vasili Burdo */
16
17
#include "pwinver.h"
18
#include <windows.h>
19
#include <stdio.h>
20
#include "mingw.h"
21
22
static const char * pariwin32_basedir = NULL;
23
24
const char*
25
win32_basedir(void)
26
{
27
if (!pariwin32_basedir)
28
{
29
char basedir[1024];
30
char* slash;
31
GetModuleFileNameA(0, basedir, sizeof(basedir) );
32
slash = strrchr(basedir, '\\');
33
if (slash) slash[1] = 0;
34
pariwin32_basedir = strdup(basedir);
35
}
36
return pariwin32_basedir;
37
}
38
39
char*
40
win32_datadir(void)
41
{
42
char datadir[1029];
43
const char * basedir = win32_basedir();
44
sprintf(datadir, "%sdata", basedir);
45
return strdup(datadir);
46
}
47
48
static WORD
49
win32_console_color(unsigned long c)
50
{
51
int shift, intense = 0;
52
if( c >= 30 && c <= 37 ) { shift = 0; c -= 30; } else
53
if( c >= 40 && c <= 47 ) { shift = 4; c -= 40; } else
54
if( c >= 90 && c <= 97 ) { shift = 0; intense = 8; c -= 90; } else
55
if(c >= 100 && c <= 107) { shift = 4; intense = 8; c -= 100; } else
56
return 0;
57
58
WORD w = 0;
59
switch(c) {
60
case 0: w = 0; break; /* black */
61
case 1: w = 4; break; /* red */
62
case 2: w = 2; break; /* green */
63
case 3: w = 6; break; /* yellow RG */
64
case 4: w = 1; break; /* blue */
65
case 5: w = 5; break; /* magenta RB */
66
case 6: w = 3; break; /* cyan GB */
67
case 7: w = 7; break; /* white RGB */
68
}
69
return (w|intense) << shift;
70
}
71
72
void
73
win32_ansi_fputs(const char* s, void* f)
74
{
75
WORD color;
76
unsigned long c[3];
77
long nbarg;
78
if( !(f == stdout || f == stderr) ) {
79
fputs(s,f);
80
return;
81
}
82
83
while(1) {
84
char *p;
85
p = strstr(s, "\x1b[");
86
if( p > s )
87
fwrite(s,p-s,1,f);
88
89
if( p )
90
p += 2;
91
else {
92
fputs(s,f);
93
return;
94
}
95
nbarg = 0;
96
c[nbarg++] = strtoul(p,&p,10);
97
if( *p == ';' ) c[nbarg++] = strtoul(p+1,&p,10);
98
if( *p == ';' ) c[nbarg++] = strtoul(p+1,&p,10);
99
if( *p++ == 'm' ) {
100
switch(nbarg)
101
{
102
case 1:
103
color = 7;
104
break;
105
case 2:
106
color = win32_console_color(c[1]);
107
if (c[0]&4) color |= 0x8000;
108
break;
109
case 3:
110
color = win32_console_color(c[1]) | win32_console_color(c[2]);
111
if (c[0]&4) color |= 0x8000;
112
}
113
fflush(f);
114
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
115
}
116
s = p;
117
}
118
}
119
120
int
121
win32_terminal_width(void)
122
{
123
CONSOLE_SCREEN_BUFFER_INFO sbi;
124
if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &sbi))
125
return 0;
126
return sbi.srWindow.Right - sbi.srWindow.Left + 1;
127
}
128
129
int
130
win32_terminal_height(void)
131
{
132
CONSOLE_SCREEN_BUFFER_INFO sbi;
133
if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &sbi))
134
return 0;
135
return sbi.srWindow.Bottom - sbi.srWindow.Top + 1;
136
}
137
138
void
139
win32_set_codepage(void)
140
{
141
SetConsoleCP( GetACP() );
142
SetConsoleOutputCP( GetACP() );
143
}
144
145
void
146
win32_set_pdf_viewer(void)
147
{
148
char *s = getenv("GP_PDF_VIEWER");
149
if (!s)
150
{
151
HKEY handle;
152
const char *key = "AcroExch.Document\\shell\\open\\command";
153
const long SZ = 512;
154
char str[SZ], *buf;
155
int status;
156
DWORD L = SZ;
157
158
(void)RegOpenKeyEx(HKEY_CLASSES_ROOT, key, 0, KEY_READ, &handle);
159
status = RegQueryValueEx(handle, NULL, 0, NULL, (LPBYTE)str, &L);
160
RegCloseKey(handle);
161
if (status) return;
162
buf = malloc(strlen(str)+16); /*must not be freed*/
163
sprintf(buf,"GP_PDF_VIEWER=%s",str);
164
putenv(buf);
165
}
166
}
167
168
extern int win32ctrlc, win32alrm;
169
static HANDLE hTimerQueue = NULL;
170
171
static void CALLBACK
172
win32_cb_alarm(void *lpParam, BOOLEAN TimerOrWaitFired)
173
{
174
(void) lpParam; (void) TimerOrWaitFired;
175
win32ctrlc++;
176
win32alrm = 1;
177
}
178
179
void
180
win32_alarm(unsigned int s)
181
{
182
if (hTimerQueue)
183
{
184
HANDLE oldhTimerQueue = hTimerQueue;
185
hTimerQueue = NULL;
186
DeleteTimerQueue(oldhTimerQueue);
187
}
188
if (s)
189
{
190
void *arg = NULL;
191
HANDLE hTimer = NULL;
192
hTimerQueue = CreateTimerQueue();
193
CreateTimerQueueTimer( &hTimer, hTimerQueue,
194
(WAITORTIMERCALLBACK)win32_cb_alarm, &arg , s*1000, 0, 0);
195
}
196
}
197
198
#define WIN32_FILETIME_PER_MILLISECOND 10000
199
200
long
201
win32_timer(void)
202
{
203
FILETIME lpCreation, lpExit, lpKernel, lpUser;
204
LARGE_INTEGER time;
205
GetProcessTimes(
206
GetCurrentProcess(),
207
&lpCreation, &lpExit, &lpKernel, &lpUser
208
);
209
time.HighPart = lpUser.dwHighDateTime;
210
time.LowPart = lpUser.dwLowDateTime;
211
time.QuadPart /= WIN32_FILETIME_PER_MILLISECOND;
212
return time.LowPart;
213
}
214
215
long
216
win32_nbthreads(void)
217
{
218
SYSTEM_INFO system_info;
219
GetSystemInfo(&system_info);
220
return system_info.dwNumberOfProcessors;
221
}
222
223