Process.h.in 11 KB

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