Process.h.in 12 KB

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