Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmWin32ProcessExecution.h
blob00357396dfa0ccce5b1d15353a4f2a7f7bf2afc9
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmWin32ProcessExecution.h,v $
5 Language: C++
6 Date: $Date: 2006/05/12 18:12:13 $
7 Version: $Revision: 1.13 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #ifndef cmWin32ProcessExecution_h
18 #define cmWin32ProcessExecution_h
20 #include "cmStandardIncludes.h"
21 #include "windows.h"
23 class cmMakefile;
25 /** \class cmWin32ProcessExecution
26 * \brief A process executor for windows
28 * cmWin32ProcessExecution is a class that provides a "clean" way of
29 * executing processes on Windows. It is modified code from Python 2.1
30 * distribution.
32 * Portable 'popen' replacement for Win32.
34 * Written by Bill Tutt <billtut@microsoft.com>. Minor tweaks and 2.0
35 * integration by Fredrik Lundh <fredrik@pythonware.com> Return code
36 * handling by David Bolen <db3l@fitlinxx.com>.
38 * Modified for CMake.
40 * For more information, please check Microsoft Knowledge Base
41 * Articles Q190351 and Q150956.
43 class cmWin32ProcessExecution
45 public:
46 cmWin32ProcessExecution()
48 this->HideWindows = false;
49 this->SetConsoleSpawn("w9xpopen.exe");
50 this->Initialize();
52 ~cmWin32ProcessExecution();
53 ///! If true windows will be created hidden.
54 void SetHideWindows(bool v) { this->HideWindows = v; }
56 /**
57 * Initialize the process execution datastructure. Do not call while
58 * running the process.
60 void Initialize()
62 this->ProcessHandle = 0;
63 this->ExitValue = -1;
64 // Comment this out. Maybe we will need it in the future.
65 // file IO access to the process might be cool.
66 //this->StdIn = 0;
67 //this->StdOut = 0;
68 //this->StdErr = 0;
69 this->pStdIn = -1;
70 this->pStdOut = -1;
71 this->pStdErr = -1;
74 /**
75 * Start the process in the directory path. Make sure that the
76 * executable is either in the path or specify the full path. The
77 * argument verbose specifies wether or not to display output while
78 * it is being generated.
80 bool StartProcess(const char*, const char* path, bool verbose);
82 /**
83 * Wait for the process to finish. If timeout is specified, it will
84 * break the process after timeout expires. (Timeout code is not yet
85 * implemented.
87 bool Wait(int timeout);
89 /**
90 * Get the output of the process (mixed stdout and stderr) as
91 * std::string.
93 const std::string GetOutput() const { return this->Output; }
95 /**
96 * Get the return value of the process. If the process is still
97 * running, the return value is -1.
99 int GetExitValue() const { return this->ExitValue; }
102 * On Windows 9x there is a bug in the process execution code which
103 * may result in blocking. That is why this workaround is
104 * used. Specify the console spawn, which should run the
105 * Windows9xHack code.
107 void SetConsoleSpawn(const char* prog) { this->ConsoleSpawn = prog; }
108 static int Windows9xHack(const char* command);
110 /** Code from a Borland web site with the following explaination :
111 * In this article, I will explain how to spawn a console
112 * application and redirect its standard input/output using
113 * anonymous pipes. An anonymous pipe is a pipe that goes only in
114 * one direction (read pipe, write pipe, etc.). Maybe you are
115 * asking, "why would I ever need to do this sort of thing?" One
116 * example would be a Windows telnet server, where you spawn a shell
117 * and listen on a port and send and receive data between the shell
118 * and the socket client. (Windows does not really have a built-in
119 * remote shell). First, we should talk about pipes. A pipe in
120 * Windows is simply a method of communication, often between
121 * process. The SDK defines a pipe as "a communication conduit with
122 * two ends; a process with a handle to one end can communicate with
123 * a process having a handle to the other end." In our case, we are
124 * using "anonymous" pipes, one-way pipes that "transfer data
125 * between a parent process and a child process or between two child
126 * processes of the same parent process." It's easiest to imagine a
127 * pipe as its namesake. An actual pipe running between processes
128 * that can carry data. We are using anonymous pipes because the
129 * console app we are spawning is a child process. We use the
130 * CreatePipe function which will create an anonymous pipe and
131 * return a read handle and a write handle. We will create two
132 * pipes, on for stdin and one for stdout. We will then monitor the
133 * read end of the stdout pipe to check for display on our child
134 * process. Every time there is something availabe for reading, we
135 * will display it in our app. Consequently, we check for input in
136 * our app and send it off to the write end of the stdin pipe.
138 static bool BorlandRunCommand(const char* command,
139 const char* dir,
140 std::string& output, int& retVal,
141 bool verbose,
142 int timeout, bool hideWindows);
144 private:
145 bool CloseHandles();
146 bool PrivateOpen(const char*, const char*, int, int);
147 bool PrivateClose(int timeout);
149 HANDLE ProcessHandle;
150 HANDLE hChildStdinRd;
151 HANDLE hChildStdinWr;
152 HANDLE hChildStdoutRd;
153 HANDLE hChildStdoutWr;
154 HANDLE hChildStderrRd;
155 HANDLE hChildStderrWr;
156 HANDLE hChildStdinWrDup;
157 HANDLE hChildStdoutRdDup;
158 HANDLE hChildStderrRdDup;
161 int pStdIn;
162 int pStdOut;
163 int pStdErr;
165 int ExitValue;
167 std::string Output;
168 std::string ConsoleSpawn;
169 bool Verbose;
170 bool HideWindows;
174 #endif