<commit_before>/* ----------------------------------------------------------------------------
   (c) The University of Glasgow 2004
   
   Support for System.Process
   ------------------------------------------------------------------------- */

#include "HsBase.h"

#if defined(mingw32_HOST_OS)
#include <windows.h>
#include <stdlib.h>
#endif

#ifdef HAVE_VFORK_H
#include <vfork.h>
#endif

#ifdef HAVE_VFORK
#define fork vfork
#endif

#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif

#if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
/* ----------------------------------------------------------------------------
   UNIX versions
   ------------------------------------------------------------------------- */

ProcHandle
runProcess (char *const args[], char *workingDirectory, char **environment, 
	    int fdStdInput, int fdStdOutput, int fdStdError,
	    int set_inthandler, long inthandler, 
	    int set_quithandler, long quithandler)
{
    int pid;
    struct sigaction dfl;

    switch(pid = fork())
    {
    case -1:
	return -1;
	
    case 0:
    {
	pPrPr_disableITimers();
	
	if (workingDirectory) {
	    chdir (workingDirectory);
	}
	
	/* Set the SIGINT/SIGQUIT signal handlers in the child, if requested 
	 */
        (void)sigemptyset(&dfl.sa_mask);
        dfl.sa_flags = 0;
	if (set_inthandler) {
	    dfl.sa_handler = (void *)inthandler;
	    (void)sigaction(SIGINT, &dfl, NULL);
	}
	if (set_quithandler) {
	    dfl.sa_handler = (void *)quithandler;
	    (void)sigaction(SIGQUIT,  &dfl, NULL);
	}

	dup2 (fdStdInput,  STDIN_FILENO);
	dup2 (fdStdOutput, STDOUT_FILENO);
	dup2 (fdStdError,  STDERR_FILENO);
	
	if (environment) {
	    execvpe(args[0], args, environment);
	} else {
	    execvp(args[0], args);
	}
    }
    _exit(127);
    }
    
    return pid;
}

ProcHandle
runInteractiveProcess (char *const args[], 
		       char *workingDirectory, char **environment,
		       int *pfdStdInput, int *pfdStdOutput, int *pfdStdError)
{
    int pid;
    int fdStdInput[2], fdStdOutput[2], fdStdError[2];

    pipe(fdStdInput);
    pipe(fdStdOutput);
    pipe(fdStdError);

    switch(pid = fork())
    {
    case -1:
	close(fdStdInput[0]);
	close(fdStdInput[1]);
	close(fdStdOutput[0]);
	close(fdStdOutput[1]);
	close(fdStdError[0]);
	close(fdStdError[1]);
	return -1;
	
    case 0:
    {
	pPrPr_disableITimers();
	
	if (workingDirectory) {
	    chdir (workingDirectory);
	}
	
	dup2 (fdStdInput[0], STDIN_FILENO);
	dup2 (fdStdOutput[1], STDOUT_FILENO);
	dup2 (fdStdError[1], STDERR_FILENO);
	
	close(fdStdInput[0]);
	close(fdStdInput[1]);
	close(fdStdOutput[0]);
	close(fdStdOutput[1]);
	close(fdStdError[0]);
	close(fdStdError[1]);
	
	/* the child */
	if (environment) {
	    execvpe(args[0], args, environment);
	} else {
	    execvp(args[0], args);
	}
    }
    _exit(127);
    
    default:
	close(fdStdInput[0]);
	close(fdStdOutput[1]);
	close(fdStdError[1]);
	
	*pfdStdInput  = fdStdInput[1];
	*pfdStdOutput = fdStdOutput[0];
	*pfdStdError  = fdStdError[0];
	break;
    }
    
    return pid;
}

int
terminateProcess (ProcHandle handle)
{
    return (kill(handle, SIGTERM) == 0);
}

int
getProcessExitCode (ProcHandle handle, int *pExitCode)
{
    int wstat;
    
    *pExitCode = 0;
    
    if (waitpid(handle, &wstat, WNOHANG) > 0)
    {
	if (WIFEXITED(wstat))
	{
	    *pExitCode = WEXITSTATUS(wstat);
	    return 1;
	}
	else
	    if (WIFSIGNALED(wstat))
	    {
		errno = EINTR;
		return -1;
	    }
	    else
	    {
		/* This should never happen */
	    }
    }
    
    return 0;
}

int waitForProcess (ProcHandle handle)
{
    int wstat;
    
    while (waitpid(handle, &wstat, 0) < 0)
    {
	if (errno != EINTR)
	{
	    return -1;<commit_msg>[project @ 2005-03-07 10:40:44 by simonmar] merge rev. 1.4.2.1 to HEAD<commit_after>/* ----------------------------------------------------------------------------
   (c) The University of Glasgow 2004
   
   Support for System.Process
   ------------------------------------------------------------------------- */

#include "HsBase.h"

#if defined(mingw32_HOST_OS)
#include <windows.h>
#include <stdlib.h>
#endif

#ifdef HAVE_VFORK_H
#include <vfork.h>
#endif

#ifdef HAVE_VFORK
#define fork vfork
#endif

#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif

#if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
/* ----------------------------------------------------------------------------
   UNIX versions
   ------------------------------------------------------------------------- */

ProcHandle
runProcess (char *const args[], char *workingDirectory, char **environment, 
	    int fdStdInput, int fdStdOutput, int fdStdError,
	    int set_inthandler, long inthandler, 
	    int set_quithandler, long quithandler)
{
    int pid;
    struct sigaction dfl;

    switch(pid = fork())
    {
    case -1:
	return -1;
	
    case 0:
    {
	pPrPr_disableITimers();
	
	if (workingDirectory) {
	    chdir (workingDirectory);
	}
	
	/* Set the SIGINT/SIGQUIT signal handlers in the child, if requested 
	 */
        (void)sigemptyset(&dfl.sa_mask);
        dfl.sa_flags = 0;
	if (set_inthandler) {
	    dfl.sa_handler = (void *)inthandler;
	    (void)sigaction(SIGINT, &dfl, NULL);
	}
	if (set_quithandler) {
	    dfl.sa_handler = (void *)quithandler;
	    (void)sigaction(SIGQUIT,  &dfl, NULL);
	}

	dup2 (fdStdInput,  STDIN_FILENO);
	dup2 (fdStdOutput, STDOUT_FILENO);
	dup2 (fdStdError,  STDERR_FILENO);
	
	if (environment) {
	    execvpe(args[0], args, environment);
	} else {
	    execvp(args[0], args);
	}
    }
    _exit(127);
    }
    
    return pid;
}

ProcHandle
runInteractiveProcess (char *const args[], 
		       char *workingDirectory, char **environment,
		       int *pfdStdInput, int *pfdStdOutput, int *pfdStdError)
{
    int pid;
    int fdStdInput[2], fdStdOutput[2], fdStdError[2];

    pipe(fdStdInput);
    pipe(fdStdOutput);
    pipe(fdStdError);

    switch(pid = fork())
    {
    case -1:
	close(fdStdInput[0]);
	close(fdStdInput[1]);
	close(fdStdOutput[0]);
	close(fdStdOutput[1]);
	close(fdStdError[0]);
	close(fdStdError[1]);
	return -1;
	
    case 0:
    {
	pPrPr_disableITimers();
	
	if (workingDirectory) {
	    chdir (workingDirectory);
	}
	
	dup2 (fdStdInput[0], STDIN_FILENO);
	dup2 (fdStdOutput[1], STDOUT_FILENO);
	dup2 (fdStdError[1], STDERR_FILENO);
	
	close(fdStdInput[0]);
	close(fdStdInput[1]);
	close(fdStdOutput[0]);
	close(fdStdOutput[1]);
	close(fdStdError[0]);
	close(fdStdError[1]);
	
	/* the child */
	if (environment) {
	    execvpe(args[0], args, environment);
	} else {
	    execvp(args[0], args);
	}
    }
    _exit(127);
    
    default:
	close(fdStdInput[0]);
	close(fdStdOutput[1]);
	close(fdStdError[1]);
	
	*pfdStdInput  = fdStdInput[1];
	*pfdStdOutput = fdStdOutput[0];
	*pfdStdError  = fdStdError[0];
	break;
    }
    
    return pid;
}

int
terminateProcess (ProcHandle handle)
{
    return (kill(handle, SIGTERM) == 0);
}

int
getProcessExitCode (ProcHandle handle, int *pExitCode)
{
    int wstat, res;
    
    *pExitCode = 0;
    
    if ((res = waitpid(handle, &wstat, WNOHANG)) > 0)
    {
	if (WIFEXITED(wstat))
	{
	    *pExitCode = WEXITSTATUS(wstat);
	    return 1;
	}
	else
	    if (WIFSIGNALED(wstat))
	    {
		errno = EINTR;
		return -1;
	    }
	    else
	    {
		/* This should never happen */
	    }
    }
    
    if (res == 0) return 0;

    if (errno == ECHILD) 
    {
	    *pExitCode = 0;
	    return 1;
    }

    return -1;
}

int waitForProcess (ProcHandle handle)
{
    int wstat;
    
    while (waitpid(handle, &wstat, 0) < 0)
    {
	if (errno != EINTR)
	{
	    return -1;
{'fraction_non_alphanumeric': 0.2066905615292712, 'fraction_numerical': 0.02044338245055091, 'mean_word_length': 2.2885202968136187, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3664135', 'n_tokens_mistral': 2910, 'n_tokens_neox': 2719, 'n_words': 564}