Process.h.in 15 KB

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