Process.h.in 14 KB

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