Process.h.in 15 KB

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