Process.h.in 12 KB

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