2009-09-28 19:43:28 +04:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2002-09-28 01:26:37 +04:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
2002-09-28 01:26:37 +04:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
2002-09-28 01:26:37 +04:00
|
|
|
#include "cmWin32ProcessExecution.h"
|
|
|
|
|
2012-08-13 21:42:58 +04:00
|
|
|
#include "cmSystemTools.h"
|
2002-09-28 01:26:37 +04:00
|
|
|
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <io.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <windows.h>
|
|
|
|
|
2002-09-30 05:55:10 +04:00
|
|
|
#if defined(__BORLANDC__)
|
|
|
|
# define STRICMP stricmp
|
|
|
|
# define TO_INTPTR(x) ((long)(x))
|
2010-04-13 00:07:05 +04:00
|
|
|
#endif // Borland
|
|
|
|
#if defined(_MSC_VER) // Visual studio
|
2002-09-30 05:55:10 +04:00
|
|
|
# if ( _MSC_VER >= 1300 )
|
|
|
|
# include <stddef.h>
|
|
|
|
# define TO_INTPTR(x) ((intptr_t)(x))
|
|
|
|
# else // Visual Studio 6
|
|
|
|
# define TO_INTPTR(x) ((long)(x))
|
|
|
|
# endif // Visual studio .NET
|
|
|
|
# define STRICMP _stricmp
|
2010-04-13 00:07:05 +04:00
|
|
|
#endif // Visual Studio
|
|
|
|
#if defined(__MINGW32__)
|
|
|
|
# include <stdint.h>
|
|
|
|
# define TO_INTPTR(x) ((intptr_t)(x))
|
|
|
|
# define STRICMP _stricmp
|
|
|
|
#endif // MinGW
|
2002-09-30 05:55:10 +04:00
|
|
|
|
2002-09-28 01:26:37 +04:00
|
|
|
#define POPEN_1 1
|
|
|
|
#define POPEN_2 2
|
|
|
|
#define POPEN_3 3
|
|
|
|
#define POPEN_4 4
|
|
|
|
|
|
|
|
#define cmMAX(x,y) (((x)<(y))?(y):(x))
|
|
|
|
|
2002-10-04 18:38:14 +04:00
|
|
|
void DisplayErrorMessage()
|
|
|
|
{
|
|
|
|
LPVOID lpMsgBuf;
|
2012-08-13 21:42:58 +04:00
|
|
|
FormatMessage(
|
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
2002-10-04 18:38:14 +04:00
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL,
|
|
|
|
GetLastError(),
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
|
|
|
|
(LPTSTR) &lpMsgBuf,
|
|
|
|
0,
|
2012-08-13 21:42:58 +04:00
|
|
|
NULL
|
2002-10-04 18:38:14 +04:00
|
|
|
);
|
|
|
|
// Process any inserts in lpMsgBuf.
|
2012-08-13 21:42:58 +04:00
|
|
|
// ...
|
2002-10-04 18:38:14 +04:00
|
|
|
// Display the string.
|
|
|
|
MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
|
|
|
|
// Free the buffer.
|
|
|
|
LocalFree( lpMsgBuf );
|
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
|
|
|
// Code from a Borland web site with the following explaination :
|
2002-10-04 18:38:14 +04:00
|
|
|
/* In this article, I will explain how to spawn a console application
|
|
|
|
* and redirect its standard input/output using anonymous pipes. An
|
|
|
|
* anonymous pipe is a pipe that goes only in one direction (read
|
|
|
|
* pipe, write pipe, etc.). Maybe you are asking, "why would I ever
|
|
|
|
* need to do this sort of thing?" One example would be a Windows
|
|
|
|
* telnet server, where you spawn a shell and listen on a port and
|
|
|
|
* send and receive data between the shell and the socket
|
|
|
|
* client. (Windows does not really have a built-in remote
|
|
|
|
* shell). First, we should talk about pipes. A pipe in Windows is
|
|
|
|
* simply a method of communication, often between process. The SDK
|
|
|
|
* defines a pipe as "a communication conduit with two ends;
|
|
|
|
a process
|
|
|
|
* with a handle to one end can communicate with a process having a
|
|
|
|
* handle to the other end." In our case, we are using "anonymous"
|
|
|
|
* pipes, one-way pipes that "transfer data between a parent process
|
|
|
|
* and a child process or between two child processes of the same
|
|
|
|
* parent process." It's easiest to imagine a pipe as its namesake. An
|
|
|
|
* actual pipe running between processes that can carry data. We are
|
|
|
|
* using anonymous pipes because the console app we are spawning is a
|
|
|
|
* child process. We use the CreatePipe function which will create an
|
|
|
|
* anonymous pipe and return a read handle and a write handle. We will
|
|
|
|
* create two pipes, on for stdin and one for stdout. We will then
|
|
|
|
* monitor the read end of the stdout pipe to check for display on our
|
|
|
|
* child process. Every time there is something availabe for reading,
|
|
|
|
* we will display it in our app. Consequently, we check for input in
|
2012-08-13 21:42:58 +04:00
|
|
|
* our app and send it off to the write end of the stdin pipe. */
|
2002-10-04 18:38:14 +04:00
|
|
|
|
2012-08-13 21:42:58 +04:00
|
|
|
inline bool IsWinNT()
|
|
|
|
//check if we're running NT
|
2002-10-04 18:38:14 +04:00
|
|
|
{
|
|
|
|
OSVERSIONINFO osv;
|
|
|
|
osv.dwOSVersionInfoSize = sizeof(osv);
|
|
|
|
GetVersionEx(&osv);
|
2012-08-13 21:42:58 +04:00
|
|
|
return (osv.dwPlatformId == VER_PLATFORM_WIN32_NT);
|
2002-10-04 18:38:14 +04:00
|
|
|
}
|
|
|
|
|
2012-08-13 21:42:58 +04:00
|
|
|
//---------------------------------------------------------------------------
|
2002-10-04 18:38:14 +04:00
|
|
|
bool cmWin32ProcessExecution::BorlandRunCommand(
|
2012-08-13 21:42:58 +04:00
|
|
|
const char* command, const char* dir,
|
|
|
|
std::string& output, int& retVal, bool verbose, int /* timeout */,
|
|
|
|
bool hideWindows)
|
2002-10-04 18:38:14 +04:00
|
|
|
{
|
|
|
|
//verbose = true;
|
2012-08-13 21:42:58 +04:00
|
|
|
//std::cerr << std::endl
|
|
|
|
// << "WindowsRunCommand(" << command << ")" << std::endl
|
2002-10-04 18:38:14 +04:00
|
|
|
// << std::flush;
|
|
|
|
const int BUFFER_SIZE = 4096;
|
|
|
|
char buf[BUFFER_SIZE];
|
2012-08-13 21:42:58 +04:00
|
|
|
|
|
|
|
//i/o buffer
|
2002-10-04 18:38:14 +04:00
|
|
|
STARTUPINFO si;
|
|
|
|
SECURITY_ATTRIBUTES sa;
|
|
|
|
SECURITY_DESCRIPTOR sd;
|
2012-08-13 21:42:58 +04:00
|
|
|
|
|
|
|
//security information for pipes
|
2002-10-04 18:38:14 +04:00
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
HANDLE newstdin,newstdout,read_stdout,write_stdin;
|
2012-08-13 21:42:58 +04:00
|
|
|
|
|
|
|
//pipe handles
|
|
|
|
if (IsWinNT())
|
|
|
|
//initialize security descriptor (Windows NT)
|
2002-10-04 18:38:14 +04:00
|
|
|
{
|
|
|
|
InitializeSecurityDescriptor(&sd,SECURITY_DESCRIPTOR_REVISION);
|
|
|
|
SetSecurityDescriptorDacl(&sd, true, NULL, false);
|
|
|
|
sa.lpSecurityDescriptor = &sd;
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-10-04 18:38:14 +04:00
|
|
|
}
|
|
|
|
else sa.lpSecurityDescriptor = NULL;
|
|
|
|
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
|
|
|
|
sa.bInheritHandle = true;
|
2012-08-13 21:42:58 +04:00
|
|
|
|
|
|
|
//allow inheritable handles
|
|
|
|
if (!CreatePipe(&newstdin,&write_stdin,&sa,0))
|
|
|
|
//create stdin pipe
|
2002-10-04 18:38:14 +04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
if (!CreatePipe(&read_stdout,&newstdout,&sa,0))
|
|
|
|
//create stdout pipe
|
2002-10-04 18:38:14 +04:00
|
|
|
{
|
|
|
|
CloseHandle(newstdin);
|
|
|
|
CloseHandle(write_stdin);
|
|
|
|
return false;
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-10-04 18:38:14 +04:00
|
|
|
}
|
|
|
|
GetStartupInfo(&si);
|
2012-08-13 21:42:58 +04:00
|
|
|
|
|
|
|
//set startupinfo for the spawned process
|
2002-10-04 18:38:14 +04:00
|
|
|
/* The dwFlags member tells CreateProcess how to make the
|
|
|
|
* process. STARTF_USESTDHANDLES validates the hStd*
|
|
|
|
* members. STARTF_USESHOWWINDOW validates the wShowWindow
|
2012-08-13 21:42:58 +04:00
|
|
|
* member. */
|
|
|
|
|
2002-10-04 18:38:14 +04:00
|
|
|
si.cb = sizeof(STARTUPINFO);
|
|
|
|
si.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
|
|
|
|
si.hStdOutput = newstdout;
|
|
|
|
si.hStdError = newstdout;
|
2002-11-08 23:46:08 +03:00
|
|
|
si.wShowWindow = SW_SHOWDEFAULT;
|
|
|
|
if(hideWindows)
|
|
|
|
{
|
|
|
|
si.wShowWindow = SW_HIDE;
|
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-10-04 18:38:14 +04:00
|
|
|
//set the new handles for the child process si.hStdInput = newstdin;
|
|
|
|
char* commandAndArgs = strcpy(new char[strlen(command)+1], command);
|
2002-11-05 00:59:18 +03:00
|
|
|
if (!CreateProcess(NULL,commandAndArgs,NULL,NULL,TRUE,
|
2012-08-13 21:42:58 +04:00
|
|
|
0, // CREATE_NEW_CONSOLE,
|
|
|
|
NULL,dir,&si,&pi))
|
2002-10-04 18:38:14 +04:00
|
|
|
{
|
|
|
|
std::cerr << "CreateProcess failed " << commandAndArgs << std::endl;
|
|
|
|
CloseHandle(newstdin);
|
|
|
|
CloseHandle(newstdout);
|
|
|
|
CloseHandle(read_stdout);
|
|
|
|
CloseHandle(write_stdin);
|
|
|
|
delete [] commandAndArgs;
|
|
|
|
return false;
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-10-04 18:38:14 +04:00
|
|
|
}
|
|
|
|
delete [] commandAndArgs;
|
|
|
|
unsigned long exit=0;
|
2012-08-13 21:42:58 +04:00
|
|
|
|
|
|
|
//process exit code unsigned
|
2002-10-04 18:38:14 +04:00
|
|
|
unsigned long bread;
|
2012-08-13 21:42:58 +04:00
|
|
|
|
|
|
|
//bytes read unsigned
|
2002-10-04 18:38:14 +04:00
|
|
|
unsigned long avail;
|
2012-08-13 21:42:58 +04:00
|
|
|
|
|
|
|
//bytes available
|
2002-10-04 18:38:14 +04:00
|
|
|
memset(buf, 0, sizeof(buf));
|
2012-08-13 21:42:58 +04:00
|
|
|
for(;;)
|
|
|
|
//main program loop
|
2002-10-04 18:38:14 +04:00
|
|
|
{
|
|
|
|
Sleep(10);
|
2012-08-13 21:42:58 +04:00
|
|
|
//check to see if there is any data to read from stdout
|
2002-10-04 18:38:14 +04:00
|
|
|
//std::cout << "Peek for data..." << std::endl;
|
|
|
|
PeekNamedPipe(read_stdout,buf,1023,&bread,&avail,NULL);
|
2012-08-13 21:42:58 +04:00
|
|
|
if (bread != 0)
|
2002-10-04 18:38:14 +04:00
|
|
|
{
|
|
|
|
memset(buf, 0, sizeof(buf));
|
2012-08-13 21:42:58 +04:00
|
|
|
if (avail > 1023)
|
2002-10-04 18:38:14 +04:00
|
|
|
{
|
2012-08-13 21:42:58 +04:00
|
|
|
while (bread >= 1023)
|
2002-10-04 18:38:14 +04:00
|
|
|
{
|
|
|
|
//std::cout << "Read data..." << std::endl;
|
|
|
|
ReadFile(read_stdout,buf,1023,&bread,NULL);
|
2012-08-13 21:42:58 +04:00
|
|
|
|
|
|
|
//read the stdout pipe
|
2002-10-04 18:38:14 +04:00
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
output += buf;
|
|
|
|
if (verbose)
|
|
|
|
{
|
2004-01-07 19:24:22 +03:00
|
|
|
cmSystemTools::Stdout(buf);
|
2002-10-04 18:38:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
else
|
2002-10-04 18:38:14 +04:00
|
|
|
{
|
|
|
|
ReadFile(read_stdout,buf,1023,&bread,NULL);
|
|
|
|
output += buf;
|
2012-08-13 21:42:58 +04:00
|
|
|
if(verbose)
|
2002-10-04 18:38:14 +04:00
|
|
|
{
|
2004-01-07 19:24:22 +03:00
|
|
|
cmSystemTools::Stdout(buf);
|
2002-10-04 18:38:14 +04:00
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-10-04 18:38:14 +04:00
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-10-04 18:38:14 +04:00
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-10-04 18:38:14 +04:00
|
|
|
//std::cout << "Check for process..." << std::endl;
|
|
|
|
GetExitCodeProcess(pi.hProcess,&exit);
|
2012-08-13 21:42:58 +04:00
|
|
|
|
|
|
|
//while the process is running
|
2002-10-04 18:38:14 +04:00
|
|
|
if (exit != STILL_ACTIVE) break;
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-10-04 18:38:14 +04:00
|
|
|
}
|
|
|
|
WaitForSingleObject(pi.hProcess, INFINITE);
|
|
|
|
GetExitCodeProcess(pi.hProcess,&exit);
|
|
|
|
CloseHandle(pi.hThread);
|
|
|
|
CloseHandle(pi.hProcess);
|
|
|
|
CloseHandle(newstdin);
|
2012-08-13 21:42:58 +04:00
|
|
|
|
|
|
|
//clean stuff up
|
2002-10-04 18:38:14 +04:00
|
|
|
CloseHandle(newstdout);
|
|
|
|
CloseHandle(read_stdout);
|
|
|
|
CloseHandle(write_stdin);
|
|
|
|
retVal = exit;
|
|
|
|
return true;
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-10-04 18:38:14 +04:00
|
|
|
}
|
|
|
|
|
2002-09-28 01:26:37 +04:00
|
|
|
bool cmWin32ProcessExecution::StartProcess(
|
|
|
|
const char* cmd, const char* path, bool verbose)
|
|
|
|
{
|
|
|
|
this->Initialize();
|
2006-03-15 19:02:08 +03:00
|
|
|
this->Verbose = verbose;
|
2002-09-28 01:26:37 +04:00
|
|
|
return this->PrivateOpen(cmd, path, _O_RDONLY | _O_TEXT, POPEN_3);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmWin32ProcessExecution::Wait(int timeout)
|
|
|
|
{
|
|
|
|
return this->PrivateClose(timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL RealPopenCreateProcess(const char *cmdstring,
|
2012-08-13 21:42:58 +04:00
|
|
|
const char *path,
|
2002-10-10 19:08:52 +04:00
|
|
|
const char *szConsoleSpawn,
|
|
|
|
HANDLE hStdin,
|
|
|
|
HANDLE hStdout,
|
|
|
|
HANDLE hStderr,
|
2002-11-08 23:46:08 +03:00
|
|
|
HANDLE *hProcess,
|
2003-01-27 20:25:34 +03:00
|
|
|
bool hideWindows,
|
|
|
|
std::string& output)
|
2002-09-28 01:26:37 +04:00
|
|
|
{
|
|
|
|
PROCESS_INFORMATION piProcInfo;
|
|
|
|
STARTUPINFO siStartInfo;
|
2010-12-28 17:53:09 +03:00
|
|
|
char *s1=0,*s2=0;
|
|
|
|
const char *s3 = " /c ";
|
2006-08-01 19:38:42 +04:00
|
|
|
int i = GetEnvironmentVariable("COMSPEC",NULL,0);
|
|
|
|
if (i)
|
2002-09-28 01:26:37 +04:00
|
|
|
{
|
|
|
|
char *comshell;
|
|
|
|
|
2003-08-22 00:22:23 +04:00
|
|
|
s1 = (char *)malloc(i);
|
2006-08-01 19:38:42 +04:00
|
|
|
int x = GetEnvironmentVariable("COMSPEC", s1, i);
|
|
|
|
if (!x)
|
2002-09-30 05:55:10 +04:00
|
|
|
{
|
2003-08-22 00:22:23 +04:00
|
|
|
free(s1);
|
2002-09-28 01:26:37 +04:00
|
|
|
return x;
|
2002-09-30 05:55:10 +04:00
|
|
|
}
|
2002-09-28 01:26:37 +04:00
|
|
|
|
|
|
|
/* Explicitly check if we are using COMMAND.COM. If we are
|
|
|
|
* then use the w9xpopen hack.
|
|
|
|
*/
|
|
|
|
comshell = s1 + x;
|
|
|
|
while (comshell >= s1 && *comshell != '\\')
|
|
|
|
--comshell;
|
|
|
|
++comshell;
|
|
|
|
|
|
|
|
if (GetVersion() < 0x80000000 &&
|
2012-08-13 21:42:58 +04:00
|
|
|
STRICMP(comshell, "command.com") != 0)
|
2002-09-28 01:26:37 +04:00
|
|
|
{
|
|
|
|
/* NT/2000 and not using command.com. */
|
2002-09-29 22:09:38 +04:00
|
|
|
x = i + (int)strlen(s3) + (int)strlen(cmdstring) + 1;
|
2003-08-22 00:22:23 +04:00
|
|
|
s2 = (char *)malloc(x);
|
2002-09-28 01:26:37 +04:00
|
|
|
ZeroMemory(s2, x);
|
2002-10-10 19:08:52 +04:00
|
|
|
//sprintf(s2, "%s%s%s", s1, s3, cmdstring);
|
|
|
|
sprintf(s2, "%s", cmdstring);
|
2002-09-28 01:26:37 +04:00
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
else
|
2002-09-28 01:26:37 +04:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Oh gag, we're on Win9x or using COMMAND.COM. Use
|
|
|
|
* the workaround listed in KB: Q150956
|
|
|
|
*/
|
|
|
|
char modulepath[_MAX_PATH];
|
|
|
|
struct stat statinfo;
|
|
|
|
GetModuleFileName(NULL, modulepath, sizeof(modulepath));
|
|
|
|
for (i = x = 0; modulepath[i]; i++)
|
2002-10-10 19:08:52 +04:00
|
|
|
if (modulepath[i] == '\\')
|
|
|
|
x = i+1;
|
2002-09-28 01:26:37 +04:00
|
|
|
modulepath[x] = '\0';
|
|
|
|
/* Create the full-name to w9xpopen, so we can test it exists */
|
2012-08-13 21:42:58 +04:00
|
|
|
strncat(modulepath,
|
|
|
|
szConsoleSpawn,
|
2002-10-10 19:08:52 +04:00
|
|
|
(sizeof(modulepath)/sizeof(modulepath[0]))
|
|
|
|
-strlen(modulepath));
|
2012-08-13 21:42:58 +04:00
|
|
|
if (stat(modulepath, &statinfo) != 0)
|
2002-10-10 19:08:52 +04:00
|
|
|
{
|
2012-08-13 21:42:58 +04:00
|
|
|
/* Eeek - file-not-found - possibly an embedding
|
|
|
|
situation - see if we can locate it in sys.prefix
|
2002-10-10 19:08:52 +04:00
|
|
|
*/
|
2012-08-13 21:42:58 +04:00
|
|
|
strncpy(modulepath,
|
|
|
|
".",
|
2002-10-10 19:08:52 +04:00
|
|
|
sizeof(modulepath)/sizeof(modulepath[0]));
|
|
|
|
if (modulepath[strlen(modulepath)-1] != '\\')
|
|
|
|
strcat(modulepath, "\\");
|
2012-08-13 21:42:58 +04:00
|
|
|
strncat(modulepath,
|
|
|
|
szConsoleSpawn,
|
2002-10-10 19:08:52 +04:00
|
|
|
(sizeof(modulepath)/sizeof(modulepath[0]))
|
|
|
|
-strlen(modulepath));
|
|
|
|
/* No where else to look - raise an easily identifiable
|
|
|
|
error, rather than leaving Windows to report
|
|
|
|
"file not found" - as the user is probably blissfully
|
|
|
|
unaware this shim EXE is used, and it will confuse them.
|
|
|
|
(well, it confused me for a while ;-)
|
|
|
|
*/
|
2012-08-13 21:42:58 +04:00
|
|
|
if (stat(modulepath, &statinfo) != 0)
|
2002-10-10 19:08:52 +04:00
|
|
|
{
|
2012-08-13 21:42:58 +04:00
|
|
|
std::cout
|
2002-10-10 19:08:52 +04:00
|
|
|
<< "Can not locate '" << modulepath
|
|
|
|
<< "' which is needed "
|
|
|
|
"for popen to work with your shell "
|
2012-08-13 21:42:58 +04:00
|
|
|
"or platform." << std::endl;
|
2003-08-22 00:22:23 +04:00
|
|
|
free(s1);
|
|
|
|
free(s2);
|
2002-10-10 19:08:52 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2002-09-29 22:09:38 +04:00
|
|
|
x = i + (int)strlen(s3) + (int)strlen(cmdstring) + 1 +
|
2012-08-13 21:42:58 +04:00
|
|
|
(int)strlen(modulepath) +
|
2002-10-10 19:08:52 +04:00
|
|
|
(int)strlen(szConsoleSpawn) + 1;
|
2003-08-22 00:22:23 +04:00
|
|
|
if(s2)
|
|
|
|
{
|
|
|
|
free(s2);
|
|
|
|
}
|
|
|
|
s2 = (char *)malloc(x);
|
2002-09-28 01:26:37 +04:00
|
|
|
ZeroMemory(s2, x);
|
|
|
|
sprintf(
|
2002-10-10 19:08:52 +04:00
|
|
|
s2,
|
|
|
|
"%s %s%s%s",
|
|
|
|
modulepath,
|
|
|
|
s1,
|
|
|
|
s3,
|
|
|
|
cmdstring);
|
2002-09-30 23:00:42 +04:00
|
|
|
sprintf(
|
2002-10-10 19:08:52 +04:00
|
|
|
s2,
|
|
|
|
"%s %s",
|
|
|
|
modulepath,
|
|
|
|
cmdstring);
|
2002-09-28 01:26:37 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Could be an else here to try cmd.exe / command.com in the path
|
|
|
|
Now we'll just error out.. */
|
2012-08-13 21:42:58 +04:00
|
|
|
else
|
2002-09-28 01:26:37 +04:00
|
|
|
{
|
|
|
|
std::cout << "Cannot locate a COMSPEC environment variable to "
|
2012-08-13 21:42:58 +04:00
|
|
|
<< "use as the shell" << std::endl;
|
2003-08-22 00:22:23 +04:00
|
|
|
free(s2);
|
|
|
|
free(s1);
|
2002-09-28 01:26:37 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-09-28 01:26:37 +04:00
|
|
|
ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
|
|
|
|
siStartInfo.cb = sizeof(STARTUPINFO);
|
|
|
|
siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
|
|
|
|
siStartInfo.hStdInput = hStdin;
|
|
|
|
siStartInfo.hStdOutput = hStdout;
|
|
|
|
siStartInfo.hStdError = hStderr;
|
2012-08-13 21:42:58 +04:00
|
|
|
siStartInfo.wShowWindow = SW_SHOWDEFAULT;
|
2002-11-08 23:46:08 +03:00
|
|
|
if(hideWindows)
|
|
|
|
{
|
|
|
|
siStartInfo.wShowWindow = SW_HIDE;
|
|
|
|
}
|
2002-09-28 01:26:37 +04:00
|
|
|
|
2002-10-10 19:08:52 +04:00
|
|
|
//std::cout << "Create process: " << s2 << std::endl;
|
2002-09-28 01:26:37 +04:00
|
|
|
if (CreateProcess(NULL,
|
2002-10-10 19:08:52 +04:00
|
|
|
s2,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
TRUE,
|
2002-11-05 00:59:18 +03:00
|
|
|
0, //CREATE_NEW_CONSOLE,
|
2002-10-10 19:08:52 +04:00
|
|
|
NULL,
|
|
|
|
path,
|
|
|
|
&siStartInfo,
|
2012-08-13 21:42:58 +04:00
|
|
|
&piProcInfo) )
|
2002-09-28 01:26:37 +04:00
|
|
|
{
|
|
|
|
/* Close the handles now so anyone waiting is woken. */
|
|
|
|
CloseHandle(piProcInfo.hThread);
|
|
|
|
/* Return process handle */
|
|
|
|
*hProcess = piProcInfo.hProcess;
|
2002-10-03 01:46:21 +04:00
|
|
|
//std::cout << "Process created..." << std::endl;
|
2003-08-22 00:22:23 +04:00
|
|
|
free(s2);
|
|
|
|
free(s1);
|
2002-09-28 01:26:37 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
2004-06-24 00:34:38 +04:00
|
|
|
|
|
|
|
output += "CreateProcessError: ";
|
2006-10-05 21:43:02 +04:00
|
|
|
{
|
|
|
|
/* Format the error message. */
|
|
|
|
char message[1024];
|
|
|
|
DWORD original = GetLastError();
|
|
|
|
DWORD length = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS, 0, original,
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
message, 1023, 0);
|
|
|
|
if(length < 1)
|
|
|
|
{
|
|
|
|
/* FormatMessage failed. Use a default message. */
|
|
|
|
_snprintf(message, 1023,
|
|
|
|
"Process execution failed with error 0x%X. "
|
|
|
|
"FormatMessage failed with error 0x%X",
|
|
|
|
original, GetLastError());
|
|
|
|
}
|
|
|
|
output += message;
|
|
|
|
}
|
2004-06-24 00:34:38 +04:00
|
|
|
output += "\n";
|
|
|
|
output += "for command: ";
|
2003-01-27 20:25:34 +03:00
|
|
|
output += s2;
|
2004-06-24 00:34:38 +04:00
|
|
|
if(path)
|
|
|
|
{
|
|
|
|
output += "\nin dir: ";
|
|
|
|
output += path;
|
|
|
|
}
|
2003-01-27 20:25:34 +03:00
|
|
|
output += "\n";
|
2003-08-22 00:22:23 +04:00
|
|
|
free(s2);
|
|
|
|
free(s1);
|
2002-09-28 01:26:37 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The following code is based off of KB: Q190351 */
|
|
|
|
|
2012-08-13 21:42:58 +04:00
|
|
|
bool cmWin32ProcessExecution::PrivateOpen(const char *cmdstring,
|
2002-10-10 19:08:52 +04:00
|
|
|
const char* path,
|
2012-08-13 21:42:58 +04:00
|
|
|
int mode,
|
2002-10-10 19:08:52 +04:00
|
|
|
int n)
|
2002-09-28 01:26:37 +04:00
|
|
|
{
|
2004-09-08 18:41:54 +04:00
|
|
|
HANDLE hProcess;
|
2002-09-28 01:26:37 +04:00
|
|
|
|
|
|
|
SECURITY_ATTRIBUTES saAttr;
|
|
|
|
BOOL fSuccess;
|
|
|
|
int fd1, fd2, fd3;
|
2004-09-08 18:41:54 +04:00
|
|
|
this->hChildStdinRd = 0;
|
|
|
|
this->hChildStdinWr = 0;
|
|
|
|
this->hChildStdoutRd = 0;
|
|
|
|
this->hChildStdoutWr = 0;
|
|
|
|
this->hChildStderrRd = 0;
|
|
|
|
this->hChildStderrWr = 0;
|
|
|
|
this->hChildStdinWrDup = 0;
|
|
|
|
this->hChildStdoutRdDup = 0;
|
|
|
|
this->hChildStderrRdDup = 0;
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-09-28 01:26:37 +04:00
|
|
|
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
|
2002-10-03 01:46:21 +04:00
|
|
|
saAttr.bInheritHandle = TRUE;
|
|
|
|
saAttr.lpSecurityDescriptor = NULL;
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2007-03-02 18:50:45 +03:00
|
|
|
fd1 = 0;
|
|
|
|
fd2 = 0;
|
|
|
|
fd3 = 0;
|
|
|
|
|
2004-09-08 18:41:54 +04:00
|
|
|
if (!CreatePipe(&this->hChildStdinRd, &this->hChildStdinWr, &saAttr, 0))
|
2002-09-30 05:55:10 +04:00
|
|
|
{
|
2006-03-15 19:02:08 +03:00
|
|
|
this->Output += "CreatePipeError\n";
|
2003-01-27 20:18:43 +03:00
|
|
|
return false;
|
2002-09-30 05:55:10 +04:00
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-09-28 01:26:37 +04:00
|
|
|
/* Create new output read handle and the input write handle. Set
|
|
|
|
* the inheritance properties to FALSE. Otherwise, the child inherits
|
2012-11-07 20:13:09 +04:00
|
|
|
* these handles; resulting in non-closeable handles to the pipes
|
2002-09-28 01:26:37 +04:00
|
|
|
* being created. */
|
2004-09-08 18:41:54 +04:00
|
|
|
fSuccess = DuplicateHandle(GetCurrentProcess(), this->hChildStdinWr,
|
|
|
|
GetCurrentProcess(), &this->hChildStdinWrDup, 0,
|
2002-10-10 19:08:52 +04:00
|
|
|
FALSE,
|
|
|
|
DUPLICATE_SAME_ACCESS);
|
2002-09-28 01:26:37 +04:00
|
|
|
if (!fSuccess)
|
2003-01-27 20:18:43 +03:00
|
|
|
{
|
2006-03-15 19:02:08 +03:00
|
|
|
this->Output += "DuplicateHandleError\n";
|
2003-01-27 20:18:43 +03:00
|
|
|
return false;
|
|
|
|
}
|
2002-09-30 05:55:10 +04:00
|
|
|
|
2002-09-28 01:26:37 +04:00
|
|
|
|
|
|
|
/* Close the inheritable version of ChildStdin
|
|
|
|
that we're using. */
|
|
|
|
CloseHandle(hChildStdinWr);
|
|
|
|
|
2004-09-08 18:41:54 +04:00
|
|
|
if (!CreatePipe(&this->hChildStdoutRd, &this->hChildStdoutWr, &saAttr, 0))
|
2003-01-27 20:18:43 +03:00
|
|
|
{
|
2006-03-15 19:02:08 +03:00
|
|
|
this->Output += "CreatePipeError\n";
|
2003-01-27 20:18:43 +03:00
|
|
|
return false;
|
|
|
|
}
|
2002-09-28 01:26:37 +04:00
|
|
|
|
2004-09-08 18:41:54 +04:00
|
|
|
fSuccess = DuplicateHandle(GetCurrentProcess(), this->hChildStdoutRd,
|
|
|
|
GetCurrentProcess(), &this->hChildStdoutRdDup, 0,
|
2002-10-10 19:08:52 +04:00
|
|
|
FALSE, DUPLICATE_SAME_ACCESS);
|
2002-09-28 01:26:37 +04:00
|
|
|
if (!fSuccess)
|
2003-01-27 20:18:43 +03:00
|
|
|
{
|
2006-03-15 19:02:08 +03:00
|
|
|
this->Output += "DuplicateHandleError\n";
|
2003-01-27 20:18:43 +03:00
|
|
|
return false;
|
|
|
|
}
|
2002-09-28 01:26:37 +04:00
|
|
|
|
|
|
|
/* Close the inheritable version of ChildStdout
|
|
|
|
that we're using. */
|
|
|
|
CloseHandle(hChildStdoutRd);
|
|
|
|
|
2012-08-13 21:42:58 +04:00
|
|
|
if (n != POPEN_4)
|
2002-09-28 01:26:37 +04:00
|
|
|
{
|
2004-09-08 18:41:54 +04:00
|
|
|
if (!CreatePipe(&this->hChildStderrRd, &this->hChildStderrWr, &saAttr, 0))
|
2003-01-27 20:18:43 +03:00
|
|
|
{
|
2006-03-15 19:02:08 +03:00
|
|
|
this->Output += "CreatePipeError\n";
|
2003-01-27 20:18:43 +03:00
|
|
|
return false;
|
|
|
|
}
|
2002-09-30 05:55:10 +04:00
|
|
|
fSuccess = DuplicateHandle(GetCurrentProcess(),
|
2004-09-08 18:41:54 +04:00
|
|
|
this->hChildStderrRd,
|
|
|
|
GetCurrentProcess(),
|
|
|
|
&this->hChildStderrRdDup, 0,
|
|
|
|
FALSE, DUPLICATE_SAME_ACCESS);
|
2002-09-28 01:26:37 +04:00
|
|
|
if (!fSuccess)
|
2003-01-27 20:18:43 +03:00
|
|
|
{
|
2006-03-15 19:02:08 +03:00
|
|
|
this->Output += "DuplicateHandleError\n";
|
2003-01-27 20:18:43 +03:00
|
|
|
return false;
|
|
|
|
}
|
2002-09-28 01:26:37 +04:00
|
|
|
/* Close the inheritable version of ChildStdErr that we're using. */
|
|
|
|
CloseHandle(hChildStderrRd);
|
2002-09-30 23:00:42 +04:00
|
|
|
|
2002-09-28 01:26:37 +04:00
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
|
|
|
switch (n)
|
2002-09-28 01:26:37 +04:00
|
|
|
{
|
|
|
|
case POPEN_1:
|
2012-08-13 21:42:58 +04:00
|
|
|
switch (mode & (_O_RDONLY | _O_TEXT | _O_BINARY | _O_WRONLY))
|
2002-10-10 19:08:52 +04:00
|
|
|
{
|
|
|
|
case _O_WRONLY | _O_TEXT:
|
|
|
|
/* Case for writing to child Stdin in text mode. */
|
2004-09-08 18:41:54 +04:00
|
|
|
fd1 = _open_osfhandle(TO_INTPTR(this->hChildStdinWrDup), mode);
|
2002-10-10 19:08:52 +04:00
|
|
|
/* We don't care about these pipes anymore,
|
|
|
|
so close them. */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case _O_RDONLY | _O_TEXT:
|
|
|
|
/* Case for reading from child Stdout in text mode. */
|
2004-09-08 18:41:54 +04:00
|
|
|
fd1 = _open_osfhandle(TO_INTPTR(this->hChildStdoutRdDup), mode);
|
2002-10-10 19:08:52 +04:00
|
|
|
/* We don't care about these pipes anymore,
|
|
|
|
so close them. */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case _O_RDONLY | _O_BINARY:
|
|
|
|
/* Case for readinig from child Stdout in
|
|
|
|
binary mode. */
|
2004-09-08 18:41:54 +04:00
|
|
|
fd1 = _open_osfhandle(TO_INTPTR(this->hChildStdoutRdDup), mode);
|
2002-10-10 19:08:52 +04:00
|
|
|
/* We don't care about these pipes anymore,
|
|
|
|
so close them. */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case _O_WRONLY | _O_BINARY:
|
|
|
|
/* Case for writing to child Stdin in binary mode. */
|
2004-09-08 18:41:54 +04:00
|
|
|
fd1 = _open_osfhandle(TO_INTPTR(this->hChildStdinWrDup), mode);
|
2002-10-10 19:08:52 +04:00
|
|
|
/* We don't care about these pipes anymore,
|
|
|
|
so close them. */
|
|
|
|
break;
|
|
|
|
}
|
2002-09-28 01:26:37 +04:00
|
|
|
break;
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-09-28 01:26:37 +04:00
|
|
|
case POPEN_2:
|
2012-08-13 21:42:58 +04:00
|
|
|
case POPEN_4:
|
|
|
|
//if ( 1 )
|
2002-10-10 19:08:52 +04:00
|
|
|
{
|
2004-09-08 18:41:54 +04:00
|
|
|
fd1 = _open_osfhandle(TO_INTPTR(this->hChildStdinWrDup), mode);
|
|
|
|
fd2 = _open_osfhandle(TO_INTPTR(this->hChildStdoutRdDup), mode);
|
2002-10-10 19:08:52 +04:00
|
|
|
break;
|
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-09-28 01:26:37 +04:00
|
|
|
case POPEN_3:
|
2012-08-13 21:42:58 +04:00
|
|
|
//if ( 1)
|
2002-10-10 19:08:52 +04:00
|
|
|
{
|
2004-09-08 18:41:54 +04:00
|
|
|
fd1 = _open_osfhandle(TO_INTPTR(this->hChildStdinWrDup), mode);
|
|
|
|
fd2 = _open_osfhandle(TO_INTPTR(this->hChildStdoutRdDup), mode);
|
|
|
|
fd3 = _open_osfhandle(TO_INTPTR(this->hChildStderrRdDup), mode);
|
2002-10-10 19:08:52 +04:00
|
|
|
break;
|
|
|
|
}
|
2002-09-28 01:26:37 +04:00
|
|
|
}
|
|
|
|
|
2012-08-13 21:42:58 +04:00
|
|
|
if (n == POPEN_4)
|
2002-09-28 01:26:37 +04:00
|
|
|
{
|
|
|
|
if (!RealPopenCreateProcess(cmdstring,
|
2002-10-10 19:08:52 +04:00
|
|
|
path,
|
2006-03-15 19:02:08 +03:00
|
|
|
this->ConsoleSpawn.c_str(),
|
2004-09-08 18:41:54 +04:00
|
|
|
this->hChildStdinRd,
|
|
|
|
this->hChildStdoutWr,
|
|
|
|
this->hChildStdoutWr,
|
2006-03-15 19:02:08 +03:00
|
|
|
&hProcess, this->HideWindows,
|
|
|
|
this->Output))
|
2007-07-24 23:55:03 +04:00
|
|
|
{
|
|
|
|
if(fd1 >= 0)
|
|
|
|
{
|
|
|
|
close(fd1);
|
|
|
|
}
|
|
|
|
if(fd2 >= 0)
|
|
|
|
{
|
|
|
|
close(fd2);
|
|
|
|
}
|
|
|
|
if(fd3 >= 0)
|
|
|
|
{
|
|
|
|
close(fd3);
|
|
|
|
}
|
2003-08-28 01:45:26 +04:00
|
|
|
return 0;
|
2007-07-24 23:55:03 +04:00
|
|
|
}
|
2002-09-28 01:26:37 +04:00
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
else
|
2002-09-28 01:26:37 +04:00
|
|
|
{
|
|
|
|
if (!RealPopenCreateProcess(cmdstring,
|
2002-10-10 19:08:52 +04:00
|
|
|
path,
|
2006-03-15 19:02:08 +03:00
|
|
|
this->ConsoleSpawn.c_str(),
|
2004-09-08 18:41:54 +04:00
|
|
|
this->hChildStdinRd,
|
|
|
|
this->hChildStdoutWr,
|
|
|
|
this->hChildStderrWr,
|
2006-03-15 19:02:08 +03:00
|
|
|
&hProcess, this->HideWindows,
|
|
|
|
this->Output))
|
2007-07-24 23:55:03 +04:00
|
|
|
{
|
|
|
|
if(fd1 >= 0)
|
|
|
|
{
|
|
|
|
close(fd1);
|
|
|
|
}
|
|
|
|
if(fd2 >= 0)
|
|
|
|
{
|
|
|
|
close(fd2);
|
|
|
|
}
|
|
|
|
if(fd3 >= 0)
|
|
|
|
{
|
|
|
|
close(fd3);
|
|
|
|
}
|
2003-08-28 01:45:26 +04:00
|
|
|
return 0;
|
2007-07-24 23:55:03 +04:00
|
|
|
}
|
2002-09-28 01:26:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Child is launched. Close the parents copy of those pipe
|
|
|
|
* handles that only the child should have open. You need to
|
|
|
|
* make sure that no handles to the write end of the output pipe
|
|
|
|
* are maintained in this process or else the pipe will not close
|
|
|
|
* when the child process exits and the ReadFile will hang. */
|
2006-03-15 19:02:08 +03:00
|
|
|
this->ProcessHandle = hProcess;
|
2002-09-30 05:55:10 +04:00
|
|
|
if ( fd1 >= 0 )
|
2002-09-28 01:26:37 +04:00
|
|
|
{
|
2006-03-15 19:02:08 +03:00
|
|
|
this->pStdIn = fd1;
|
2002-09-28 01:26:37 +04:00
|
|
|
}
|
2002-09-30 05:55:10 +04:00
|
|
|
if ( fd2 >= 0 )
|
2002-09-28 01:26:37 +04:00
|
|
|
{
|
2006-03-15 19:02:08 +03:00
|
|
|
this->pStdOut = fd2;
|
2002-09-28 01:26:37 +04:00
|
|
|
}
|
2002-09-30 05:55:10 +04:00
|
|
|
if ( fd3 >= 0 )
|
2002-09-28 01:26:37 +04:00
|
|
|
{
|
2006-03-15 19:02:08 +03:00
|
|
|
this->pStdErr = fd3;
|
2002-09-28 01:26:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-01-17 22:29:11 +03:00
|
|
|
bool cmWin32ProcessExecution::CloseHandles()
|
|
|
|
{
|
2007-07-24 23:55:03 +04:00
|
|
|
if(this->pStdErr != -1 )
|
|
|
|
{
|
2008-09-05 01:02:25 +04:00
|
|
|
// this will close this as well: this->hChildStderrRdDup
|
2007-07-24 23:55:03 +04:00
|
|
|
_close(this->pStdErr);
|
|
|
|
this->pStdErr = -1;
|
2008-09-05 01:02:25 +04:00
|
|
|
this->hChildStderrRdDup = 0;
|
2007-07-24 23:55:03 +04:00
|
|
|
}
|
|
|
|
if(this->pStdIn != -1 )
|
|
|
|
{
|
2008-09-05 01:02:25 +04:00
|
|
|
// this will close this as well: this->hChildStdinWrDup
|
2007-07-24 23:55:03 +04:00
|
|
|
_close(this->pStdIn);
|
|
|
|
this->pStdIn = -1;
|
2008-09-05 01:02:25 +04:00
|
|
|
this->hChildStdinWrDup = 0;
|
2007-07-24 23:55:03 +04:00
|
|
|
}
|
|
|
|
if(this->pStdOut != -1 )
|
|
|
|
{
|
2008-09-05 01:02:25 +04:00
|
|
|
// this will close this as well: this->hChildStdoutRdDup
|
2007-07-24 23:55:03 +04:00
|
|
|
_close(this->pStdOut);
|
|
|
|
this->pStdOut = -1;
|
2008-09-05 01:02:25 +04:00
|
|
|
this->hChildStdoutRdDup = 0;
|
2007-07-24 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
2005-01-17 22:29:11 +03:00
|
|
|
bool ret = true;
|
|
|
|
if (this->hChildStdinRd && !CloseHandle(this->hChildStdinRd))
|
|
|
|
{
|
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
this->hChildStdinRd = 0;
|
2008-09-05 01:02:25 +04:00
|
|
|
// now close these two
|
2005-01-17 22:29:11 +03:00
|
|
|
if (this->hChildStdoutWr && !CloseHandle(this->hChildStdoutWr))
|
|
|
|
{
|
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
this->hChildStdoutWr = 0;
|
|
|
|
if (this->hChildStderrWr && !CloseHandle(this->hChildStderrWr))
|
|
|
|
{
|
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
this->hChildStderrWr = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
cmWin32ProcessExecution::~cmWin32ProcessExecution()
|
|
|
|
{
|
|
|
|
this->CloseHandles();
|
|
|
|
}
|
|
|
|
|
2002-10-04 18:38:14 +04:00
|
|
|
bool cmWin32ProcessExecution::PrivateClose(int /* timeout */)
|
2002-09-28 01:26:37 +04:00
|
|
|
{
|
2006-03-15 19:02:08 +03:00
|
|
|
HANDLE hProcess = this->ProcessHandle;
|
2002-09-28 01:26:37 +04:00
|
|
|
|
|
|
|
int result = -1;
|
|
|
|
DWORD exit_code;
|
|
|
|
|
|
|
|
std::string output = "";
|
|
|
|
bool done = false;
|
|
|
|
while(!done)
|
|
|
|
{
|
2002-10-03 01:46:21 +04:00
|
|
|
Sleep(10);
|
2002-09-30 15:55:31 +04:00
|
|
|
bool have_some = false;
|
2002-10-03 01:46:21 +04:00
|
|
|
struct _stat fsout;
|
|
|
|
struct _stat fserr;
|
2006-03-15 19:02:08 +03:00
|
|
|
int rout = _fstat(this->pStdOut, &fsout);
|
|
|
|
int rerr = _fstat(this->pStdErr, &fserr);
|
2002-10-03 01:46:21 +04:00
|
|
|
if ( rout && rerr )
|
2002-09-28 01:26:37 +04:00
|
|
|
{
|
2002-10-03 01:46:21 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (fserr.st_size > 0)
|
|
|
|
{
|
2005-10-28 19:52:23 +04:00
|
|
|
char buffer[1024];
|
2006-03-15 19:02:08 +03:00
|
|
|
int len = read(this->pStdErr, buffer, 1023);
|
2002-09-28 01:26:37 +04:00
|
|
|
buffer[len] = 0;
|
2006-03-15 19:02:08 +03:00
|
|
|
if ( this->Verbose )
|
2002-10-10 19:08:52 +04:00
|
|
|
{
|
2004-01-07 19:24:22 +03:00
|
|
|
cmSystemTools::Stdout(buffer);
|
2002-10-10 19:08:52 +04:00
|
|
|
}
|
2002-09-28 01:26:37 +04:00
|
|
|
output += buffer;
|
2002-09-30 15:55:31 +04:00
|
|
|
have_some = true;
|
2002-09-28 01:26:37 +04:00
|
|
|
}
|
2002-10-03 01:46:21 +04:00
|
|
|
if (fsout.st_size > 0)
|
2002-09-28 01:26:37 +04:00
|
|
|
{
|
2005-10-28 19:52:23 +04:00
|
|
|
char buffer[1024];
|
2006-03-15 19:02:08 +03:00
|
|
|
int len = read(this->pStdOut, buffer, 1023);
|
2002-09-28 01:26:37 +04:00
|
|
|
buffer[len] = 0;
|
2006-03-15 19:02:08 +03:00
|
|
|
if ( this->Verbose )
|
2002-10-10 19:08:52 +04:00
|
|
|
{
|
2004-01-07 19:24:22 +03:00
|
|
|
cmSystemTools::Stdout(buffer);
|
2002-10-10 19:08:52 +04:00
|
|
|
}
|
2002-09-28 01:26:37 +04:00
|
|
|
output += buffer;
|
2002-09-30 15:55:31 +04:00
|
|
|
have_some = true;
|
2002-09-28 01:26:37 +04:00
|
|
|
}
|
|
|
|
unsigned long exitCode;
|
2002-09-30 15:55:31 +04:00
|
|
|
if ( ! have_some )
|
2002-09-28 01:26:37 +04:00
|
|
|
{
|
2002-09-30 15:55:31 +04:00
|
|
|
GetExitCodeProcess(hProcess,&exitCode);
|
|
|
|
if (exitCode != STILL_ACTIVE)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2002-09-28 01:26:37 +04:00
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
}
|
|
|
|
|
2002-09-30 05:55:10 +04:00
|
|
|
|
2002-09-28 01:26:37 +04:00
|
|
|
if (WaitForSingleObject(hProcess, INFINITE) != WAIT_FAILED &&
|
2012-08-13 21:42:58 +04:00
|
|
|
GetExitCodeProcess(hProcess, &exit_code))
|
2002-09-28 01:26:37 +04:00
|
|
|
{
|
|
|
|
result = exit_code;
|
2012-08-13 21:42:58 +04:00
|
|
|
}
|
|
|
|
else
|
2002-09-28 01:26:37 +04:00
|
|
|
{
|
|
|
|
/* Indicate failure - this will cause the file object
|
|
|
|
* to raise an I/O error and translate the last Win32
|
|
|
|
* error code from errno. We do have a problem with
|
|
|
|
* last errors that overlap the normal errno table,
|
|
|
|
* but that's a consistent problem with the file object.
|
|
|
|
*/
|
2012-08-13 21:42:58 +04:00
|
|
|
if (result != EOF)
|
2002-09-28 01:26:37 +04:00
|
|
|
{
|
|
|
|
/* If the error wasn't from the fclose(), then
|
|
|
|
* set errno for the file object error handling.
|
|
|
|
*/
|
|
|
|
errno = GetLastError();
|
|
|
|
}
|
|
|
|
result = -1;
|
|
|
|
}
|
2002-09-30 05:55:10 +04:00
|
|
|
|
2002-09-28 01:26:37 +04:00
|
|
|
/* Free up the native handle at this point */
|
|
|
|
CloseHandle(hProcess);
|
2006-03-15 19:02:08 +03:00
|
|
|
this->ExitValue = result;
|
|
|
|
this->Output += output;
|
2005-01-17 22:29:11 +03:00
|
|
|
bool ret = this->CloseHandles();
|
|
|
|
if ( result < 0 || !ret)
|
2002-09-28 01:26:37 +04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cmWin32ProcessExecution::Windows9xHack(const char* command)
|
|
|
|
{
|
|
|
|
BOOL bRet;
|
|
|
|
STARTUPINFO si;
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
DWORD exit_code=0;
|
|
|
|
|
2012-08-13 21:42:58 +04:00
|
|
|
if (!command)
|
2002-09-28 01:26:37 +04:00
|
|
|
{
|
|
|
|
cmSystemTools::Error("Windows9xHack: Command not specified");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make child process use this app's standard files. */
|
|
|
|
ZeroMemory(&si, sizeof si);
|
|
|
|
si.cb = sizeof si;
|
|
|
|
si.dwFlags = STARTF_USESTDHANDLES;
|
|
|
|
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
|
|
|
|
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-09-30 23:00:42 +04:00
|
|
|
char * app = 0;
|
2002-09-28 01:26:37 +04:00
|
|
|
char* cmd = new char[ strlen(command) + 1 ];
|
|
|
|
strcpy(cmd, command);
|
|
|
|
|
|
|
|
bRet = CreateProcess(
|
2002-09-30 23:00:42 +04:00
|
|
|
app, cmd,
|
2003-08-28 01:45:26 +04:00
|
|
|
0, 0,
|
2002-09-28 01:26:37 +04:00
|
|
|
TRUE, 0,
|
2003-08-28 01:45:26 +04:00
|
|
|
0, 0,
|
2002-09-28 01:26:37 +04:00
|
|
|
&si, &pi
|
|
|
|
);
|
|
|
|
delete [] cmd;
|
|
|
|
|
2012-08-13 21:42:58 +04:00
|
|
|
if (bRet)
|
2002-09-28 01:26:37 +04:00
|
|
|
{
|
2012-08-13 21:42:58 +04:00
|
|
|
if (WaitForSingleObject(pi.hProcess, INFINITE) != WAIT_FAILED)
|
2002-09-28 01:26:37 +04:00
|
|
|
{
|
|
|
|
GetExitCodeProcess(pi.hProcess, &exit_code);
|
|
|
|
}
|
|
|
|
CloseHandle(pi.hProcess);
|
|
|
|
CloseHandle(pi.hThread);
|
|
|
|
return exit_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|