# include "jam.h"
# include "execcmd.h"
# include "lists.h"
# include "variable.h"
# include <errno.h>
# if defined( unix ) || defined( NT ) || defined( __OS2__ )
# if defined( _AIX) || \
(defined (COHERENT) && defined (_I386)) || \
defined(__sgi) || \
defined(__Lynx__) || \
defined(M_XENIX) || \
defined(__QNX__) || \
defined(__BEOS__) || \
defined(__ISC)
# define vfork() fork()
# endif
# if defined( NT ) || defined( __OS2__ )
# include <process.h>
# if !defined( __BORLANDC__ ) && !defined( __OS2__ )
# define wait my_wait
static int my_wait(int *status);
# endif
# endif
# include <unistd.h>
# include <sys/wait.h>
static int intr = 0;
static int cmdsrunning = 0;
# ifdef NT
static void (*istat)( int );
void onintr( int );
# else
static void (*istat)();
#endif
static struct
{
int pid;
void (*func)();
void *closure;
# if defined( NT ) || defined( __OS2__ )
char *tempfile;
# endif
} cmdtab[ MAXJOBS ] = {{0}};
void
onintr( disp )
int disp;
{
intr++;
#ifdef APPLE_EXTENSIONS
pbx_printf( "JAM ", "...interrupted\n" );
#else
printf( "...interrupted\n" );
#endif
}
void
execcmd( string, func, closure, shell, exportvars )
char *string;
void (*func)();
void *closure;
LIST *shell;
int exportvars;
{
int pid;
int slot;
char *argv[ MAXARGC + 1 ];
#ifdef APPLE_EXTENSIONS
int pipeForStdInToShell[2];
static char usePipeForShellCommands = -1;
#endif
# if defined( NT ) || defined( __OS2__ )
static char *comspec;
char *p;
if( !comspec && !( comspec = getenv( "COMSPEC" ) ) )
comspec = "cmd.exe";
# endif
#ifdef APPLE_EXTENSIONS
if( usePipeForShellCommands == -1 ) {
if( getenv( "USE_CMDLINE_FOR_SHELL_COMMANDS" ) != NULL ) {
usePipeForShellCommands = 0;
} else {
usePipeForShellCommands = 1;
}
}
#endif
for( slot = 0; slot < MAXJOBS; slot++ )
if( !cmdtab[ slot ].pid )
break;
if( slot == MAXJOBS )
{
printf( "no slots for child!\n" );
exit( EXITBAD );
}
# if defined( NT ) || defined( __OS2__ )
if( !cmdtab[ slot ].tempfile )
{
char *tempdir;
if( !( tempdir = getenv( "TEMP" ) ) &&
!( tempdir = getenv( "TMP" ) ) )
tempdir = "\\temp";
cmdtab[ slot ].tempfile = malloc( strlen( tempdir ) + 14 );
sprintf( cmdtab[ slot ].tempfile, "%s\\jamtmp%02d.bat",
tempdir, slot );
}
while( isspace( *string ) )
++string;
p = strchr( string, '\n' );
while( p && isspace( *p ) )
++p;
if( p && *p || strlen( string ) > MAXLINE )
{
FILE *f;
f = fopen( cmdtab[ slot ].tempfile, "w" );
fputs( string, f );
fclose( f );
string = cmdtab[ slot ].tempfile;
}
# endif
if( shell )
{
int i;
char jobno[4];
int gotpercent = 0;
sprintf( jobno, "%d", slot + 1 );
for( i = 0; shell && i < MAXARGC; i++, shell = list_next( shell ) )
{
switch( shell->string[0] )
{
case '%': argv[i] = string; gotpercent++; break;
case '!': argv[i] = jobno; break;
default: argv[i] = shell->string;
}
if( DEBUG_EXECCMD )
printf( "argv[%d] = '%s'\n", i, argv[i] );
}
if( !gotpercent )
argv[i++] = string;
argv[i] = 0;
}
else
{
# if defined( NT ) || defined( __OS2__ )
argv[0] = comspec;
argv[1] = "/Q/C";
argv[2] = string;
argv[3] = 0;
# else
argv[0] = "/bin/sh";
#ifdef APPLE_EXTENSIONS
if (usePipeForShellCommands) {
argv[1] = "-s";
argv[2] = NULL;
} else {
argv[1] = "-c";
argv[2] = string;
argv[3] = 0;
}
#else
argv[1] = "-c";
argv[2] = string;
argv[3] = 0;
#endif
# endif
}
if( !cmdsrunning++ )
istat = signal( SIGINT, onintr );
#ifdef APPLE_EXTENSIONS
#if 0 {
int stdout_pipe[2], stderr_pipe[2];
pipe(stdout_pipe);
pipe(stderr_pipe);
printf("stdout_pipe[0]=%i, stdout_pipe[1]=%i\n", stdout_pipe[0], stdout_pipe[1]);
printf("stderr_pipe[0]=%i, stderr_pipe[1]=%i\n", stderr_pipe[0], stderr_pipe[1]);
}
#endif
#endif
# if defined( NT ) || defined( __OS2__ )
if( ( pid = spawnvp( P_NOWAIT, argv[0], argv ) ) < 0 )
{
perror( "spawn" );
exit( EXITBAD );
}
# else
#ifdef APPLE_EXTENSIONS
if (usePipeForShellCommands) {
pipe(pipeForStdInToShell);
}
#endif
if ((pid = vfork()) == 0)
{
#ifdef APPLE_EXTENSIONS
if (usePipeForShellCommands) {
close(pipeForStdInToShell[1]);
dup2(pipeForStdInToShell[0], 0);
}
#endif
if (exportvars)
var_setenv_all_exported_variables();
execvp( argv[0], argv );
_exit(127);
}
if( pid == -1 )
{
perror( "vfork" );
exit( EXITBAD );
}
# endif
cmdtab[ slot ].pid = pid;
cmdtab[ slot ].func = func;
cmdtab[ slot ].closure = closure;
#ifdef APPLE_EXTENSIONS
if (usePipeForShellCommands) {
close(pipeForStdInToShell[0]);
{
int toWrite = strlen(string);
int written = write(pipeForStdInToShell[1], string, toWrite);
if (toWrite != written) {
printf("Error: only wrote %d bytes of %d bytes of command to sub-shell.", written, toWrite);
}
}
close(pipeForStdInToShell[1]);
}
#endif
while( cmdsrunning >= MAXJOBS || cmdsrunning >= globs.jobs )
if( !execwait() )
break;
}
int
execwait()
{
int i;
int status, w;
int rstat;
if( !cmdsrunning )
return 0;
while( ( w = wait( &status ) ) == -1 && errno == EINTR )
;
if( w == -1 )
{
printf( "child process(es) lost!\n" );
perror("wait");
exit( EXITBAD );
}
for( i = 0; i < MAXJOBS; i++ )
if( w == cmdtab[ i ].pid )
break;
if( i == MAXJOBS )
{
printf( "waif child found!\n" );
exit( EXITBAD );
}
if( !--cmdsrunning )
signal( SIGINT, istat );
if( intr )
rstat = EXEC_CMD_INTR;
else if( w == -1 || status != 0 )
rstat = EXEC_CMD_FAIL;
else
rstat = EXEC_CMD_OK;
cmdtab[ i ].pid = 0;
(*cmdtab[ i ].func)( cmdtab[ i ].closure, rstat );
return 1;
}
# if defined( NT ) && !defined( __BORLANDC__ )
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
static int
my_wait( status )
int *status;
{
int i, num_active = 0;
DWORD exitcode, waitcode;
static HANDLE *active_handles = 0;
if (!active_handles)
active_handles = (HANDLE *)malloc(globs.jobs * sizeof(HANDLE) );
for ( i = 0; i < globs.jobs; i++ ) {
if ( cmdtab[i].pid ) {
if ( GetExitCodeProcess((HANDLE)cmdtab[i].pid, &exitcode) ) {
if ( exitcode == STILL_ACTIVE )
active_handles[num_active++] = (HANDLE)cmdtab[i].pid;
else {
CloseHandle((HANDLE)cmdtab[i].pid);
*status = (int)((exitcode & 0xff) << 8);
return cmdtab[i].pid;
}
}
else
goto FAILED;
}
}
if ( !num_active ) {
errno = ECHILD;
return -1;
}
waitcode = WaitForMultipleObjects( num_active,
active_handles,
FALSE,
INFINITE );
if ( waitcode != WAIT_FAILED ) {
if ( waitcode >= WAIT_ABANDONED_0
&& waitcode < WAIT_ABANDONED_0 + num_active )
i = waitcode - WAIT_ABANDONED_0;
else
i = waitcode - WAIT_OBJECT_0;
if ( GetExitCodeProcess(active_handles[i], &exitcode) ) {
CloseHandle(active_handles[i]);
*status = (int)((exitcode & 0xff) << 8);
return (int)active_handles[i];
}
}
FAILED:
errno = GetLastError();
return -1;
}
# endif
# endif