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