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
/* From Loic Grenie */
2
/* to build
3
gcc -o cook -O2 cook.c
4
cp cook uncook
5
to use (see mpigp)
6
./uncook mpirun -np 1 ./cook ./gp : -np 4 ./gp
7
*/
8
9
#define _XOPEN_SOURCE 600
10
#include <sys/types.h>
11
#include <sys/wait.h>
12
#include <signal.h>
13
#include <unistd.h>
14
#include <stdlib.h>
15
#include <fcntl.h>
16
#include <sys/select.h>
17
#include <stdio.h>
18
#include <string.h>
19
#include <strings.h>
20
#include <termios.h>
21
#include <sys/ioctl.h>
22
23
int dead = 0, status;
24
25
void chld_handler(int sig)
26
{
27
waitpid(-1, &status, WUNTRACED);
28
if (WIFEXITED(status))
29
dead = 1;
30
else if (WIFSIGNALED(status))
31
dead = 1;
32
else if (WIFSTOPPED(status))
33
kill(getpid(), WSTOPSIG(status));
34
}
35
36
void usage(char *argv0, int exitok)
37
{
38
fprintf(exitok ? stdout : stderr,
39
"Usage: %s [-u] [-c] [--] program [args]\n", argv0);
40
exit(!exitok);
41
}
42
43
int main(int argc, char **argv)
44
{
45
int cook, tiook = 0;
46
struct termios tio;
47
int fd;
48
char *name, *argv0 = argv[0];
49
char bufin[1024], bufout[1024];
50
int lgin = 0, lgout = 0;
51
52
if (argv[0]) {
53
char *p = strrchr(argv[0], '/');
54
55
if (!p)
56
p = argv[0];
57
else
58
p++;
59
cook = strcmp(p, "uncook");
60
}
61
argc--;
62
argv++;
63
while (argc > 0 && *argv && **argv == '-') {
64
char *p = *argv;
65
if (!strcmp(*argv, "--")) {
66
argc--;
67
argv++;
68
break;
69
}
70
else if (!strcmp(*argv, "--help"))
71
usage(argv0, 1);
72
while (*++p) {
73
switch (*p) {
74
case 'u':
75
cook = 0;
76
break;
77
case 'c':
78
cook = 1;
79
break;
80
default:
81
usage(argv0, *p == 'h');
82
break;
83
}
84
}
85
}
86
if (argc <= 0 || !argv)
87
usage(argv0, 0);
88
if (cook) {
89
fd = posix_openpt(O_RDWR);
90
if (fd == 0) {
91
fd = dup(fd);
92
if (fd == 1) {
93
fd = dup(fd);
94
close(1);
95
}
96
close(0);
97
}
98
if (grantpt(fd) < 0) {
99
perror("grantpt");
100
exit(3);
101
}
102
if (unlockpt(fd)) {
103
perror("unlockpt");
104
exit(4);
105
}
106
name = ptsname(fd);
107
}
108
else {
109
struct winsize wsz;
110
111
if (!ioctl(0, TIOCGWINSZ, &wsz)) {
112
char buf[1024];
113
sprintf(buf, "%d", wsz.ws_row);
114
setenv("ROWS", buf, 0);
115
sprintf(buf, "%d", wsz.ws_col);
116
setenv("COLUMNS", buf, 0);
117
}
118
if (!tcgetattr(0, &tio))
119
{
120
struct termios tio2;
121
122
tiook = 1;
123
bcopy(&tio, &tio2, sizeof tio);
124
tio2.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
125
| INLCR | IGNCR | ICRNL | IXON);
126
tio2.c_oflag &= ~OPOST;
127
tio2.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
128
tio2.c_cflag &= ~(CSIZE | PARENB);
129
tio2.c_cflag |= CS8;
130
tcsetattr(0, TCSADRAIN, &tio2);
131
}
132
else
133
(void)system("stty -isig -icanon -echo");
134
}
135
switch(fork()) {
136
case -1:
137
perror("fork");
138
exit(1);
139
case 0:
140
/* Child */
141
if (cook) {
142
char *prows, *pcols;
143
struct winsize wsz;
144
145
close(0); close(1); close(2);
146
setsid();
147
close(fd);
148
open(name, O_RDWR); /* stdin */
149
open(name, O_RDWR); /* stdout */
150
(void)system("stty sane pass8"); /* Before opening stderr */
151
open(name, O_RDWR); /* stderr */
152
if ((prows = getenv("ROWS")) && (pcols = getenv("COLUMNS"))) {
153
wsz.ws_row = atoi(prows);
154
wsz.ws_col = atoi(pcols);
155
ioctl(0, TIOCSWINSZ, &wsz);
156
}
157
}
158
execvp(argv[0], argv);
159
fprintf(stderr, "Command not found\n");
160
exit(2);
161
}
162
signal(SIGCHLD, chld_handler);
163
164
if (cook) {
165
fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) & ~O_NONBLOCK);
166
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) & ~O_NONBLOCK);
167
while (!dead || lgout) {
168
int maxfd = 2, r;
169
fd_set inset, outset;
170
171
FD_ZERO(&inset);
172
FD_ZERO(&outset);
173
if (lgin < sizeof(bufin)) {
174
FD_SET(0, &inset);
175
if (maxfd < 1) maxfd = 1;
176
}
177
if (lgout) {
178
FD_SET(1, &outset);
179
if (maxfd < 2) maxfd = 2;
180
}
181
if (!dead && lgin) {
182
FD_SET(fd, &outset);
183
if (maxfd <= fd) maxfd = fd + 1;
184
}
185
if (!dead && lgout < sizeof(bufout)) {
186
FD_SET(fd, &inset);
187
if (maxfd <= fd) maxfd = fd + 1;
188
}
189
select(maxfd, &inset, &outset, NULL, NULL);
190
if (FD_ISSET(0, &inset)) {
191
r = read(0, bufin+lgin, sizeof(bufin) - lgin);
192
if (r > 0)
193
lgin += r;
194
else
195
close(0);
196
}
197
if (FD_ISSET(1, &outset)) {
198
r = write(1, bufout, lgout);
199
if (r <= 0)
200
lgout = 0;
201
else
202
lgout -= r;
203
}
204
if (FD_ISSET(fd, &inset)) {
205
r = read(fd, bufout+lgout, sizeof(bufout) - lgout);
206
if (r <= 0) exit(0);
207
lgout += r;
208
}
209
if (FD_ISSET(fd, &outset)) {
210
r = write(fd, bufin, lgin);
211
if (r <= 0) exit(0);
212
lgin -= r;
213
}
214
}
215
}
216
else {
217
while (!dead)
218
chld_handler(0);
219
}
220
if (tiook)
221
tcsetattr(0, TCSADRAIN, &tio);
222
if (dead) {
223
if (WIFEXITED(status))
224
exit(WEXITSTATUS(status));
225
else if (WIFSIGNALED(status))
226
kill(getpid(), WTERMSIG(status));
227
fprintf(stderr, "Unknown exit status %08x\n", status);
228
exit(1);
229
}
230
/* NOT REACHED */
231
exit(0);
232
}
233
234