Process.h.in 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*=========================================================================
  2. Program: KWSys - Kitware System Library
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #ifndef @KWSYS_NAMESPACE@_Process_h
  14. #define @KWSYS_NAMESPACE@_Process_h
  15. #include <@KWSYS_NAMESPACE@/Configure.h>
  16. /* Redefine all public interface symbol names to be in the proper
  17. namespace. These macros are used internally to kwsys only, and are
  18. not visible to user code. Use kwsysHeaderDump.pl to reproduce
  19. these macros after making changes to the interface. */
  20. #define kwsys(x) @KWSYS_NAMESPACE@##x
  21. #define kwsysEXPORT @KWSYS_NAMESPACE@_EXPORT
  22. #define kwsysProcess kwsys(Process)
  23. #define kwsysProcess_s kwsys(Process_s)
  24. #define kwsysProcess_New kwsys(Process_New)
  25. #define kwsysProcess_Delete kwsys(Process_Delete)
  26. #define kwsysProcess_SetCommand kwsys(Process_SetCommand)
  27. #define kwsysProcess_AddCommand kwsys(Process_AddCommand)
  28. #define kwsysProcess_SetTimeout kwsys(Process_SetTimeout)
  29. #define kwsysProcess_SetWorkingDirectory kwsys(Process_SetWorkingDirectory)
  30. #define kwsysProcess_Option_HideWindow kwsys(Process_Option_HideWindow)
  31. #define kwsysProcess_GetOption kwsys(Process_GetOption)
  32. #define kwsysProcess_SetOption kwsys(Process_SetOption)
  33. #define kwsysProcess_Option_e kwsys(Process_Option_e)
  34. #define kwsysProcess_State_Starting kwsys(Process_State_Starting)
  35. #define kwsysProcess_State_Error kwsys(Process_State_Error)
  36. #define kwsysProcess_State_Exception kwsys(Process_State_Exception)
  37. #define kwsysProcess_State_Executing kwsys(Process_State_Executing)
  38. #define kwsysProcess_State_Exited kwsys(Process_State_Exited)
  39. #define kwsysProcess_State_Expired kwsys(Process_State_Expired)
  40. #define kwsysProcess_State_Killed kwsys(Process_State_Killed)
  41. #define kwsysProcess_GetState kwsys(Process_GetState)
  42. #define kwsysProcess_State_e kwsys(Process_State_e)
  43. #define kwsysProcess_Exception_None kwsys(Process_Exception_None)
  44. #define kwsysProcess_Exception_Fault kwsys(Process_Exception_Fault)
  45. #define kwsysProcess_Exception_Illegal kwsys(Process_Exception_Illegal)
  46. #define kwsysProcess_Exception_Interrupt kwsys(Process_Exception_Interrupt)
  47. #define kwsysProcess_Exception_Numerical kwsys(Process_Exception_Numerical)
  48. #define kwsysProcess_Exception_Other kwsys(Process_Exception_Other)
  49. #define kwsysProcess_GetExitException kwsys(Process_GetExitException)
  50. #define kwsysProcess_Exception_e kwsys(Process_Exception_e)
  51. #define kwsysProcess_GetExitCode kwsys(Process_GetExitCode)
  52. #define kwsysProcess_GetExitValue kwsys(Process_GetExitValue)
  53. #define kwsysProcess_GetErrorString kwsys(Process_GetErrorString)
  54. #define kwsysProcess_Execute kwsys(Process_Execute)
  55. #define kwsysProcess_WaitForData kwsys(Process_WaitForData)
  56. #define kwsysProcess_Pipes_e kwsys(Process_Pipes_e)
  57. #define kwsysProcess_Pipe_STDOUT kwsys(Process_Pipe_STDOUT)
  58. #define kwsysProcess_Pipe_STDERR kwsys(Process_Pipe_STDERR)
  59. #define kwsysProcess_Pipe_Timeout kwsys(Process_Pipe_Timeout)
  60. #define kwsysProcess_WaitForExit kwsys(Process_WaitForExit)
  61. #define kwsysProcess_Kill kwsys(Process_Kill)
  62. #if defined(__cplusplus)
  63. extern "C"
  64. {
  65. #endif
  66. /**
  67. * Process control data structure.
  68. */
  69. typedef struct kwsysProcess_s kwsysProcess;
  70. /**
  71. * Create a new Process instance.
  72. */
  73. kwsysEXPORT kwsysProcess* kwsysProcess_New();
  74. /**
  75. * Delete an existing Process instance. If the instance is currently
  76. * executing a process, this blocks until the process terminates.
  77. */
  78. kwsysEXPORT void kwsysProcess_Delete(kwsysProcess* cp);
  79. /**
  80. * Set the command line to be executed. Argument is an array of
  81. * pointers to the command and each argument. Ths array must end with
  82. * a NULL pointer. Any previous command lines are removed. Returns
  83. * 1 for success and 0 otherwise.
  84. */
  85. kwsysEXPORT int kwsysProcess_SetCommand(kwsysProcess* cp,
  86. char const* const* command);
  87. /**
  88. * Add a command line to be executed. Argument is an array of
  89. * pointers to the command and each argument. Ths array must end with
  90. * a NULL pointer. If this is not the first command added, its
  91. * standard input will be connected to the standard output of the
  92. * previous command. Returns 1 for success and 0 otherwise.
  93. */
  94. kwsysEXPORT int kwsysProcess_AddCommand(kwsysProcess* cp,
  95. char const* const* command);
  96. /**
  97. * Set the timeout for the child process. The timeout period begins
  98. * when the child is executed. If the child has not terminated when
  99. * the timeout expires, it will be killed. A non-positive (<= 0)
  100. * value will disable the timeout.
  101. */
  102. kwsysEXPORT void kwsysProcess_SetTimeout(kwsysProcess* cp, double timeout);
  103. /**
  104. * Set the working directory for the child process. The working
  105. * directory can be absolute or relative to the current directory.
  106. */
  107. kwsysEXPORT void kwsysProcess_SetWorkingDirectory(kwsysProcess* cp,
  108. const char* dir);
  109. /**
  110. * Get/Set a platform-specific option. Possible options are:
  111. *
  112. * kwsysProcess_Option_HideWindow = Whether to hide window on Windows.
  113. * 0 = No (default)
  114. * 1 = Yes
  115. */
  116. kwsysEXPORT int kwsysProcess_GetOption(kwsysProcess* cp, int optionId);
  117. kwsysEXPORT void kwsysProcess_SetOption(kwsysProcess* cp, int optionId,
  118. int value);
  119. enum kwsysProcess_Option_e
  120. {
  121. kwsysProcess_Option_HideWindow
  122. };
  123. /**
  124. * Get the current state of the Process instance. Possible states are:
  125. *
  126. * kwsysProcess_State_Starting = Execute has not yet been called.
  127. * kwsysProcess_State_Error = Error administrating the child process.
  128. * kwsysProcess_State_Exception = Child process exited abnormally.
  129. * kwsysProcess_State_Executing = Child process is currently running.
  130. * kwsysProcess_State_Exited = Child process exited normally.
  131. * kwsysProcess_State_Expired = Child process's timeout expired.
  132. * kwsysProcess_State_Killed = Child process terminated by Kill method.
  133. */
  134. kwsysEXPORT int kwsysProcess_GetState(kwsysProcess* cp);
  135. enum kwsysProcess_State_e
  136. {
  137. kwsysProcess_State_Starting,
  138. kwsysProcess_State_Error,
  139. kwsysProcess_State_Exception,
  140. kwsysProcess_State_Executing,
  141. kwsysProcess_State_Exited,
  142. kwsysProcess_State_Expired,
  143. kwsysProcess_State_Killed
  144. };
  145. /**
  146. * When GetState returns "Exception", this method returns a
  147. * platform-independent description of the exceptional behavior that
  148. * caused the child to terminate abnormally. Possible exceptions are:
  149. *
  150. * kwsysProcess_Exception_None = No exceptional behavior occurred.
  151. * kwsysProcess_Exception_Fault = Child crashed with a memory fault.
  152. * kwsysProcess_Exception_Illegal = Child crashed with an illegal instruction.
  153. * kwsysProcess_Exception_Interrupt = Child was interrupted by user (Cntl-C/Break).
  154. * kwsysProcess_Exception_Numerical = Child crashed with a numerical exception.
  155. * kwsysProcess_Exception_Other = Child terminated for another reason.
  156. */
  157. kwsysEXPORT int kwsysProcess_GetExitException(kwsysProcess* cp);
  158. enum kwsysProcess_Exception_e
  159. {
  160. kwsysProcess_Exception_None,
  161. kwsysProcess_Exception_Fault,
  162. kwsysProcess_Exception_Illegal,
  163. kwsysProcess_Exception_Interrupt,
  164. kwsysProcess_Exception_Numerical,
  165. kwsysProcess_Exception_Other
  166. };
  167. /**
  168. * When GetState returns "Exited" or "Exception", this method returns
  169. * the platform-specific raw exit code of the process. UNIX platforms
  170. * should use WIFEXITED/WEXITSTATUS and WIFSIGNALED/WTERMSIG to access
  171. * this value. Windows users should compare the value to the various
  172. * EXCEPTION_* values.
  173. *
  174. * If GetState returns "Exited", use GetExitValue to get the
  175. * platform-independent child return value.
  176. */
  177. kwsysEXPORT int kwsysProcess_GetExitCode(kwsysProcess* cp);
  178. /**
  179. * When GetState returns "Exited", this method returns the child's
  180. * platform-independent exit code (such as the value returned by the
  181. * child's main).
  182. */
  183. kwsysEXPORT int kwsysProcess_GetExitValue(kwsysProcess* cp);
  184. /**
  185. * When GetState returns "Error", this method returns a string
  186. * describing the problem. Otherwise, it returns NULL.
  187. */
  188. kwsysEXPORT const char* kwsysProcess_GetErrorString(kwsysProcess* cp);
  189. /**
  190. * Start executing the child process.
  191. */
  192. kwsysEXPORT void kwsysProcess_Execute(kwsysProcess* cp);
  193. /**
  194. * Block until data are available on a requested pipe, a timeout
  195. * expires, or the child process terminates. Arguments are as
  196. * follows:
  197. *
  198. * pipes = Flags for the child output pipes of interest to the caller.
  199. * Possible values are Pipe_STDOUT and Pipe_STDERR. Multiple
  200. * pipes may be specified by using the bitwise OR operator '|'.
  201. * data = If data are read, the pointer to which this points is
  202. * set to point to the data.
  203. * length = If data are read, the integer to which this points is
  204. * set to the length of the data read.
  205. * timeout = Specifies the maximum time this call may block. Upon
  206. * return after reading data, the time elapsed is subtracted
  207. * from the timeout value. If this timeout expires, the
  208. * value is set to 0. A NULL pointer passed for this argument
  209. * indicates no timeout for the call.
  210. *
  211. * Return value will be one of:
  212. *
  213. * 0 = No more data will be available from the child process,
  214. * or no process has been executed. WaitForExit should
  215. * be called to wait for the process to terminate.
  216. * Pipe_STDOUT = Data have been read from the child's stdout pipe.
  217. * Pipe_STDERR = Data have been read from the child's stderr pipe.
  218. * Pipe_Timeout = No data available within timeout specified for the
  219. * call. Time elapsed has been subtracted from timeout
  220. * argument.
  221. */
  222. kwsysEXPORT int kwsysProcess_WaitForData(kwsysProcess* cp, int pipes,
  223. char** data, int* length,
  224. double* timeout);
  225. enum kwsysProcess_Pipes_e
  226. {
  227. kwsysProcess_Pipe_STDOUT=1,
  228. kwsysProcess_Pipe_STDERR=2,
  229. kwsysProcess_Pipe_Timeout=255
  230. };
  231. /**
  232. * Block until the child process terminates or the given timeout
  233. * expires. If no process is running, returns immediatly. The
  234. * argument is:
  235. *
  236. * timeout = Specifies the maximum time this call may block. Upon
  237. * returning due to child termination, the elapsed time
  238. * is subtracted from the given value. A NULL pointer
  239. * passed for this argument indicates no timeout for the
  240. * call.
  241. *
  242. * Return value will be one of:
  243. *
  244. * 0 = Child did not terminate within timeout specified for
  245. * the call. Time elapsed has been subtracted from timeout
  246. * argument.
  247. * 1 = Child has terminated or was not running.
  248. */
  249. kwsysEXPORT int kwsysProcess_WaitForExit(kwsysProcess* cp, double* timeout);
  250. /**
  251. * Forcefully terminate the child process that is currently running.
  252. * The caller should call WaitForExit after this returns to wait for
  253. * the child to terminate.
  254. */
  255. kwsysEXPORT void kwsysProcess_Kill(kwsysProcess* cp);
  256. #if defined(__cplusplus)
  257. } /* extern "C" */
  258. #endif
  259. /* If we are building a kwsysProcess .c file, let it use these macros.
  260. Otherwise, undefine them to keep the namespace clean. */
  261. #if !defined(KWSYS_IN_PROCESS_C)
  262. # undef kwsys
  263. # undef kwsysEXPORT
  264. # undef kwsysProcess
  265. # undef kwsysProcess_s
  266. # undef kwsysProcess_New
  267. # undef kwsysProcess_Delete
  268. # undef kwsysProcess_SetCommand
  269. # undef kwsysProcess_AddCommand
  270. # undef kwsysProcess_SetTimeout
  271. # undef kwsysProcess_SetWorkingDirectory
  272. # undef kwsysProcess_Option_HideWindow
  273. # undef kwsysProcess_GetOption
  274. # undef kwsysProcess_SetOption
  275. # undef kwsysProcess_Option_e
  276. # undef kwsysProcess_State_Starting
  277. # undef kwsysProcess_State_Error
  278. # undef kwsysProcess_State_Exception
  279. # undef kwsysProcess_State_Executing
  280. # undef kwsysProcess_State_Exited
  281. # undef kwsysProcess_State_Expired
  282. # undef kwsysProcess_State_Killed
  283. # undef kwsysProcess_GetState
  284. # undef kwsysProcess_State_e
  285. # undef kwsysProcess_Exception_None
  286. # undef kwsysProcess_Exception_Fault
  287. # undef kwsysProcess_Exception_Illegal
  288. # undef kwsysProcess_Exception_Interrupt
  289. # undef kwsysProcess_Exception_Numerical
  290. # undef kwsysProcess_Exception_Other
  291. # undef kwsysProcess_GetExitException
  292. # undef kwsysProcess_Exception_e
  293. # undef kwsysProcess_GetExitCode
  294. # undef kwsysProcess_GetExitValue
  295. # undef kwsysProcess_GetErrorString
  296. # undef kwsysProcess_Execute
  297. # undef kwsysProcess_WaitForData
  298. # undef kwsysProcess_Pipes_e
  299. # undef kwsysProcess_Pipe_STDOUT
  300. # undef kwsysProcess_Pipe_STDERR
  301. # undef kwsysProcess_Pipe_Timeout
  302. # undef kwsysProcess_WaitForExit
  303. # undef kwsysProcess_Kill
  304. #endif
  305. #endif