OK, turing.

<- leave blank

Mon Jun 29 14:47:56 EDT 2020

static pid_t
forkwin(int hide){
	char spec[256];
	char *wsys;
	int wfd;
	pid_t pid;

	/* Fork child.  */
	pid = fork();
	if(pid != 0)
	    return pid;

	/* Mounting the window system creates a new window.  */
	wsys = getenv("wsys");
	if(!wsys){
		fprintf(stderr, "wsys not set\n");
		exit(1);
	}

	wfd = open(wsys, O_RDWR);
	if(wfd < 0){
		fprintf(stderr, "unable to open \"%s\"\n", wsys);
		exit(1);
	}


	/* We need a separate namespace from parent process.  */
	if(rfork(RFPROC|RFCNAMEG)){
	    printf("test");
	}

	snprintf(spec, sizeof spec, "new -pid %d -scroll %s", getpid(), hide ?
	"-hide" : "");
	if(mount(wfd, -1, "/mnt/wsys", MREPL, spec) < 0){
		fprintf(stderr, "unable to mount\n");
		exit(1);
	}
	if(bind("/mnt/wsys", "/dev", MBEFORE) < 0){
		fprintf(stderr, "unable to bind\n");
		exit(1);
	}

	/* Now reopen standard input, output, and error.  */
	freopen("/dev/cons", "r", stdin);
	setbuf(stdin, NULL);
	freopen("/dev/cons", "w", stdout);
	setbuf(stdout, NULL);
	freopen("/dev/cons", "w", stderr);
	setbuf(stderr, NULL);

	return pid;
}

/* new function - no idea wtf is going on here, but it works */
int
mch_call_shell(char_u *cmd, int options)
{
    char ch;
    pid_t pid;
    int status;
    int hide;
    int fd[2];

    pid = fork();
    if(pid < 0)
	return -1;
    if(pid == 0){
	if (cmd)
	    execl("/bin/rc", "rc", "-c", cmd, NULL);
	 else
	    execl("/bin/rc", "rc", NULL);

	exit(122);
    }
    waitpid(pid, &status, 0);
    return status;
}

/* old function - dunno wtf is going here */
int mch2_call_shell(char_u *cmd, int options) {
    char ch;
    pid_t pid;
    int status;
    int hide;

    USED(ch);
    USED(pid);
    USED(status);
    USED(hide);

    /* Non-interactive commands run in a hidden window.  */
    hide = options & SHELL_FILTER || options & SHELL_DOOUT;
    pid = forkwin(hide);
    if (pid == -1) {
	MSG_PUTS(_("\nCannot fork\n"));
	return -1;
    }
    if (pid == 0) {
	/* Fork again so that we can prompt the user to
	 * press a key before the window closes.  */
	pid = fork();
	if (pid == -1) {
	    exit(255);
	}
	if (pid == 0) {
	    if (cmd) {
		execl("/bin/rc", "rc", "-c", cmd, NULL);
	    } else {
		execl("/bin/rc", "rc", NULL);
	    }
	    exit(122); /* same as on UNIX Vim */
	} else {
	    waitpid(pid, &status, 0);
	    unmount(0, "/dev/");
	    if (!hide) {
		printf("\nPress RETURN to close this window...");
		read(0, &ch, 1);
	    }
	    exit(status);
	}
    }
    waitpid(pid, &status, 0);
    return status;
}