Process.h.in 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*============================================================================
  2. KWSys - Kitware System Library
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License 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. #if !@KWSYS_NAMESPACE@_NAME_IS_KWSYS
  22. # define kwsysProcess kwsys_ns(Process)
  23. # define kwsysProcess_s kwsys_ns(Process_s)
  24. # define kwsysProcess_New kwsys_ns(Process_New)
  25. # define kwsysProcess_Delete kwsys_ns(Process_Delete)
  26. # define kwsysProcess_SetCommand kwsys_ns(Process_SetCommand)
  27. # define kwsysProcess_AddCommand kwsys_ns(Process_AddCommand)
  28. # define kwsysProcess_SetTimeout kwsys_ns(Process_SetTimeout)
  29. # define kwsysProcess_SetWorkingDirectory kwsys_ns(Process_SetWorkingDirectory)
  30. # define kwsysProcess_SetPipeFile kwsys_ns(Process_SetPipeFile)
  31. # define kwsysProcess_SetPipeNative kwsys_ns(Process_SetPipeNative)
  32. # define kwsysProcess_SetPipeShared kwsys_ns(Process_SetPipeShared)
  33. # define kwsysProcess_Option_Detach kwsys_ns(Process_Option_Detach)
  34. # define kwsysProcess_Option_HideWindow kwsys_ns(Process_Option_HideWindow)
  35. # define kwsysProcess_Option_MergeOutput kwsys_ns(Process_Option_MergeOutput)
  36. # define kwsysProcess_Option_Verbatim kwsys_ns(Process_Option_Verbatim)
  37. # define kwsysProcess_Option_CreateProcessGroup kwsys_ns(Process_Option_CreateProcessGroup)
  38. # define kwsysProcess_GetOption kwsys_ns(Process_GetOption)
  39. # define kwsysProcess_SetOption kwsys_ns(Process_SetOption)
  40. # define kwsysProcess_Option_e kwsys_ns(Process_Option_e)
  41. # define kwsysProcess_State_Starting kwsys_ns(Process_State_Starting)
  42. # define kwsysProcess_State_Error kwsys_ns(Process_State_Error)
  43. # define kwsysProcess_State_Exception kwsys_ns(Process_State_Exception)
  44. # define kwsysProcess_State_Executing kwsys_ns(Process_State_Executing)
  45. # define kwsysProcess_State_Exited kwsys_ns(Process_State_Exited)
  46. # define kwsysProcess_State_Expired kwsys_ns(Process_State_Expired)
  47. # define kwsysProcess_State_Killed kwsys_ns(Process_State_Killed)
  48. # define kwsysProcess_State_Disowned kwsys_ns(Process_State_Disowned)
  49. # define kwsysProcess_GetState kwsys_ns(Process_GetState)
  50. # define kwsysProcess_State_e kwsys_ns(Process_State_e)
  51. # define kwsysProcess_Exception_None kwsys_ns(Process_Exception_None)
  52. # define kwsysProcess_Exception_Fault kwsys_ns(Process_Exception_Fault)
  53. # define kwsysProcess_Exception_Illegal kwsys_ns(Process_Exception_Illegal)
  54. # define kwsysProcess_Exception_Interrupt kwsys_ns(Process_Exception_Interrupt)
  55. # define kwsysProcess_Exception_Numerical kwsys_ns(Process_Exception_Numerical)
  56. # define kwsysProcess_Exception_Other kwsys_ns(Process_Exception_Other)
  57. # define kwsysProcess_GetExitException kwsys_ns(Process_GetExitException)
  58. # define kwsysProcess_Exception_e kwsys_ns(Process_Exception_e)
  59. # define kwsysProcess_GetExitCode kwsys_ns(Process_GetExitCode)
  60. # define kwsysProcess_GetExitValue kwsys_ns(Process_GetExitValue)
  61. # define kwsysProcess_GetErrorString kwsys_ns(Process_GetErrorString)
  62. # define kwsysProcess_GetExceptionString kwsys_ns(Process_GetExceptionString)
  63. # define kwsysProcess_Execute kwsys_ns(Process_Execute)
  64. # define kwsysProcess_Disown kwsys_ns(Process_Disown)
  65. # define kwsysProcess_WaitForData kwsys_ns(Process_WaitForData)
  66. # define kwsysProcess_Pipes_e kwsys_ns(Process_Pipes_e)
  67. # define kwsysProcess_Pipe_None kwsys_ns(Process_Pipe_None)
  68. # define kwsysProcess_Pipe_STDIN kwsys_ns(Process_Pipe_STDIN)
  69. # define kwsysProcess_Pipe_STDOUT kwsys_ns(Process_Pipe_STDOUT)
  70. # define kwsysProcess_Pipe_STDERR kwsys_ns(Process_Pipe_STDERR)
  71. # define kwsysProcess_Pipe_Timeout kwsys_ns(Process_Pipe_Timeout)
  72. # define kwsysProcess_Pipe_Handle kwsys_ns(Process_Pipe_Handle)
  73. # define kwsysProcess_WaitForExit kwsys_ns(Process_WaitForExit)
  74. # define kwsysProcess_Interrupt kwsys_ns(Process_Interrupt)
  75. # define kwsysProcess_Kill kwsys_ns(Process_Kill)
  76. #endif
  77. #if defined(__cplusplus)
  78. extern "C"
  79. {
  80. #endif
  81. /**
  82. * Process control data structure.
  83. */
  84. typedef struct kwsysProcess_s kwsysProcess;
  85. /* Platform-specific pipe handle type. */
  86. #if defined(_WIN32) && !defined(__CYGWIN__)
  87. typedef void* kwsysProcess_Pipe_Handle;
  88. #else
  89. typedef int kwsysProcess_Pipe_Handle;
  90. #endif
  91. /**
  92. * Create a new Process instance.
  93. */
  94. kwsysEXPORT kwsysProcess* kwsysProcess_New(void);
  95. /**
  96. * Delete an existing Process instance. If the instance is currently
  97. * executing a process, this blocks until the process terminates.
  98. */
  99. kwsysEXPORT void kwsysProcess_Delete(kwsysProcess* cp);
  100. /**
  101. * Set the command line to be executed. Argument is an array of
  102. * pointers to the command and each argument. The array must end with
  103. * a NULL pointer. Any previous command lines are removed. Returns
  104. * 1 for success and 0 otherwise.
  105. */
  106. kwsysEXPORT int kwsysProcess_SetCommand(kwsysProcess* cp,
  107. char const* const* command);
  108. /**
  109. * Add a command line to be executed. Argument is an array of
  110. * pointers to the command and each argument. The array must end with
  111. * a NULL pointer. If this is not the first command added, its
  112. * standard input will be connected to the standard output of the
  113. * previous command. Returns 1 for success and 0 otherwise.
  114. */
  115. kwsysEXPORT int kwsysProcess_AddCommand(kwsysProcess* cp,
  116. char const* const* command);
  117. /**
  118. * Set the timeout in seconds for the child process. The timeout
  119. * period begins when the child is executed. If the child has not
  120. * terminated when the timeout expires, it will be killed. A
  121. * non-positive (<= 0) value will disable the timeout.
  122. */
  123. kwsysEXPORT void kwsysProcess_SetTimeout(kwsysProcess* cp, double timeout);
  124. /**
  125. * Set the working directory for the child process. The working
  126. * directory can be absolute or relative to the current directory.
  127. * Returns 1 for success and 0 for failure.
  128. */
  129. kwsysEXPORT int kwsysProcess_SetWorkingDirectory(kwsysProcess* cp,
  130. const char* dir);
  131. /**
  132. * Set the name of a file to be attached to the given pipe. Returns 1
  133. * for success and 0 for failure.
  134. */
  135. kwsysEXPORT int kwsysProcess_SetPipeFile(kwsysProcess* cp, int pipe,
  136. const char* file);
  137. /**
  138. * Set whether the given pipe in the child is shared with the parent
  139. * process. The default is no for Pipe_STDOUT and Pipe_STDERR and yes
  140. * for Pipe_STDIN.
  141. */
  142. kwsysEXPORT void kwsysProcess_SetPipeShared(kwsysProcess* cp, int pipe,
  143. int shared);
  144. /**
  145. * Specify a platform-specific native pipe for use as one of the child
  146. * interface pipes. The native pipe is specified by an array of two
  147. * descriptors or handles. The first entry in the array (index 0)
  148. * should be the read end of the pipe. The second entry in the array
  149. * (index 1) should be the write end of the pipe. If a null pointer
  150. * is given the option will be disabled.
  151. *
  152. * For Pipe_STDIN the native pipe is connected to the first child in
  153. * the pipeline as its stdin. After the children are created the
  154. * write end of the pipe will be closed in the child process and the
  155. * read end will be closed in the parent process.
  156. *
  157. * For Pipe_STDOUT and Pipe_STDERR the pipe is connected to the last
  158. * child as its stdout or stderr. After the children are created the
  159. * write end of the pipe will be closed in the parent process and the
  160. * read end will be closed in the child process.
  161. */
  162. kwsysEXPORT void kwsysProcess_SetPipeNative(kwsysProcess* cp, int pipe,
  163. kwsysProcess_Pipe_Handle p[2]);
  164. /**
  165. * Get/Set a possibly platform-specific option. Possible options are:
  166. *
  167. * kwsysProcess_Option_Detach = Whether to detach the process.
  168. * 0 = No (default)
  169. * 1 = Yes
  170. *
  171. * kwsysProcess_Option_HideWindow = Whether to hide window on Windows.
  172. * 0 = No (default)
  173. * 1 = Yes
  174. *
  175. * kwsysProcess_Option_MergeOutput = Whether to merge stdout/stderr.
  176. * No content will be returned as stderr.
  177. * Any actual stderr will be on stdout.
  178. * 0 = No (default)
  179. * 1 = Yes
  180. *
  181. * kwsysProcess_Option_Verbatim = Whether SetCommand and AddCommand
  182. * should treat the first argument
  183. * as a verbatim command line
  184. * and ignore the rest of the arguments.
  185. * 0 = No (default)
  186. * 1 = Yes
  187. *
  188. * kwsysProcess_Option_CreateProcessGroup = Whether to place the process in a
  189. * new process group. This is
  190. * useful if you want to send Ctrl+C
  191. * to the process. On UNIX, also
  192. * places the process in a new
  193. * session.
  194. * 0 = No (default)
  195. * 1 = Yes
  196. */
  197. kwsysEXPORT int kwsysProcess_GetOption(kwsysProcess* cp, int optionId);
  198. kwsysEXPORT void kwsysProcess_SetOption(kwsysProcess* cp, int optionId,
  199. int value);
  200. enum kwsysProcess_Option_e
  201. {
  202. kwsysProcess_Option_HideWindow,
  203. kwsysProcess_Option_Detach,
  204. kwsysProcess_Option_MergeOutput,
  205. kwsysProcess_Option_Verbatim,
  206. kwsysProcess_Option_CreateProcessGroup
  207. };
  208. /**
  209. * Get the current state of the Process instance. Possible states are:
  210. *
  211. * kwsysProcess_State_Starting = Execute has not yet been called.
  212. * kwsysProcess_State_Error = Error administrating the child process.
  213. * kwsysProcess_State_Exception = Child process exited abnormally.
  214. * kwsysProcess_State_Executing = Child process is currently running.
  215. * kwsysProcess_State_Exited = Child process exited normally.
  216. * kwsysProcess_State_Expired = Child process's timeout expired.
  217. * kwsysProcess_State_Killed = Child process terminated by Kill method.
  218. * kwsysProcess_State_Disowned = Child is no longer managed by this object.
  219. */
  220. kwsysEXPORT int kwsysProcess_GetState(kwsysProcess* cp);
  221. enum kwsysProcess_State_e
  222. {
  223. kwsysProcess_State_Starting,
  224. kwsysProcess_State_Error,
  225. kwsysProcess_State_Exception,
  226. kwsysProcess_State_Executing,
  227. kwsysProcess_State_Exited,
  228. kwsysProcess_State_Expired,
  229. kwsysProcess_State_Killed,
  230. kwsysProcess_State_Disowned
  231. };
  232. /**
  233. * When GetState returns "Exception", this method returns a
  234. * platform-independent description of the exceptional behavior that
  235. * caused the child to terminate abnormally. Possible exceptions are:
  236. *
  237. * kwsysProcess_Exception_None = No exceptional behavior occurred.
  238. * kwsysProcess_Exception_Fault = Child crashed with a memory fault.
  239. * kwsysProcess_Exception_Illegal = Child crashed with an illegal instruction.
  240. * kwsysProcess_Exception_Interrupt = Child was interrupted by user (Cntl-C/Break).
  241. * kwsysProcess_Exception_Numerical = Child crashed with a numerical exception.
  242. * kwsysProcess_Exception_Other = Child terminated for another reason.
  243. */
  244. kwsysEXPORT int kwsysProcess_GetExitException(kwsysProcess* cp);
  245. enum kwsysProcess_Exception_e
  246. {
  247. kwsysProcess_Exception_None,
  248. kwsysProcess_Exception_Fault,
  249. kwsysProcess_Exception_Illegal,
  250. kwsysProcess_Exception_Interrupt,
  251. kwsysProcess_Exception_Numerical,
  252. kwsysProcess_Exception_Other
  253. };
  254. /**
  255. * When GetState returns "Exited" or "Exception", this method returns
  256. * the platform-specific raw exit code of the process. UNIX platforms
  257. * should use WIFEXITED/WEXITSTATUS and WIFSIGNALED/WTERMSIG to access
  258. * this value. Windows users should compare the value to the various
  259. * EXCEPTION_* values.
  260. *
  261. * If GetState returns "Exited", use GetExitValue to get the
  262. * platform-independent child return value.
  263. */
  264. kwsysEXPORT int kwsysProcess_GetExitCode(kwsysProcess* cp);
  265. /**
  266. * When GetState returns "Exited", this method returns the child's
  267. * platform-independent exit code (such as the value returned by the
  268. * child's main).
  269. */
  270. kwsysEXPORT int kwsysProcess_GetExitValue(kwsysProcess* cp);
  271. /**
  272. * When GetState returns "Error", this method returns a string
  273. * describing the problem. Otherwise, it returns NULL.
  274. */
  275. kwsysEXPORT const char* kwsysProcess_GetErrorString(kwsysProcess* cp);
  276. /**
  277. * When GetState returns "Exception", this method returns a string
  278. * describing the problem. Otherwise, it returns NULL.
  279. */
  280. kwsysEXPORT const char* kwsysProcess_GetExceptionString(kwsysProcess* cp);
  281. /**
  282. * Start executing the child process.
  283. */
  284. kwsysEXPORT void kwsysProcess_Execute(kwsysProcess* cp);
  285. /**
  286. * Stop management of a detached child process. This closes any pipes
  287. * being read. If the child was not created with the
  288. * kwsysProcess_Option_Detach option, this method does nothing. This
  289. * is because disowning a non-detached process will cause the child
  290. * exit signal to be left unhandled until this process exits.
  291. */
  292. kwsysEXPORT void kwsysProcess_Disown(kwsysProcess* cp);
  293. /**
  294. * Block until data are available on a pipe, a timeout expires, or the
  295. * child process terminates. Arguments are as follows:
  296. *
  297. * data = If data are read, the pointer to which this points is
  298. * set to point to the data.
  299. * length = If data are read, the integer to which this points is
  300. * set to the length of the data read.
  301. * timeout = Specifies the maximum time this call may block. Upon
  302. * return after reading data, the time elapsed is subtracted
  303. * from the timeout value. If this timeout expires, the
  304. * value is set to 0. A NULL pointer passed for this argument
  305. * indicates no timeout for the call. A negative or zero
  306. * value passed for this argument may be used for polling
  307. * and will always return immediately.
  308. *
  309. * Return value will be one of:
  310. *
  311. * Pipe_None = No more data will be available from the child process,
  312. * ( == 0) or no process has been executed. WaitForExit should
  313. * be called to wait for the process to terminate.
  314. * Pipe_STDOUT = Data have been read from the child's stdout pipe.
  315. * Pipe_STDERR = Data have been read from the child's stderr pipe.
  316. * Pipe_Timeout = No data available within timeout specified for the
  317. * call. Time elapsed has been subtracted from timeout
  318. * argument.
  319. */
  320. kwsysEXPORT int kwsysProcess_WaitForData(kwsysProcess* cp, char** data,
  321. int* length, double* timeout);
  322. enum kwsysProcess_Pipes_e
  323. {
  324. kwsysProcess_Pipe_None,
  325. kwsysProcess_Pipe_STDIN,
  326. kwsysProcess_Pipe_STDOUT,
  327. kwsysProcess_Pipe_STDERR,
  328. kwsysProcess_Pipe_Timeout=255
  329. };
  330. /**
  331. * Block until the child process terminates or the given timeout
  332. * expires. If no process is running, returns immediatly. The
  333. * argument is:
  334. *
  335. * timeout = Specifies the maximum time this call may block. Upon
  336. * returning due to child termination, the elapsed time
  337. * is subtracted from the given value. A NULL pointer
  338. * passed for this argument indicates no timeout for the
  339. * call.
  340. *
  341. * Return value will be one of:
  342. *
  343. * 0 = Child did not terminate within timeout specified for
  344. * the call. Time elapsed has been subtracted from timeout
  345. * argument.
  346. * 1 = Child has terminated or was not running.
  347. */
  348. kwsysEXPORT int kwsysProcess_WaitForExit(kwsysProcess* cp, double* timeout);
  349. /**
  350. * Interrupt the process group for the child process that is currently
  351. * running by sending it the appropriate operating-system specific signal.
  352. * The caller should call WaitForExit after this returns to wait for the
  353. * child to terminate.
  354. *
  355. * WARNING: If you didn't specify kwsysProcess_Option_CreateProcessGroup,
  356. * you will interrupt your own process group.
  357. */
  358. kwsysEXPORT void kwsysProcess_Interrupt(kwsysProcess* cp);
  359. /**
  360. * Forcefully terminate the child process that is currently running.
  361. * The caller should call WaitForExit after this returns to wait for
  362. * the child to terminate.
  363. */
  364. kwsysEXPORT void kwsysProcess_Kill(kwsysProcess* cp);
  365. #if defined(__cplusplus)
  366. } /* extern "C" */
  367. #endif
  368. /* If we are building a kwsys .c or .cxx file, let it use these macros.
  369. Otherwise, undefine them to keep the namespace clean. */
  370. #if !defined(KWSYS_NAMESPACE)
  371. # undef kwsys_ns
  372. # undef kwsysEXPORT
  373. # if !@KWSYS_NAMESPACE@_NAME_IS_KWSYS
  374. # undef kwsysProcess
  375. # undef kwsysProcess_s
  376. # undef kwsysProcess_New
  377. # undef kwsysProcess_Delete
  378. # undef kwsysProcess_SetCommand
  379. # undef kwsysProcess_AddCommand
  380. # undef kwsysProcess_SetTimeout
  381. # undef kwsysProcess_SetWorkingDirectory
  382. # undef kwsysProcess_SetPipeFile
  383. # undef kwsysProcess_SetPipeNative
  384. # undef kwsysProcess_SetPipeShared
  385. # undef kwsysProcess_Option_Detach
  386. # undef kwsysProcess_Option_HideWindow
  387. # undef kwsysProcess_Option_MergeOutput
  388. # undef kwsysProcess_Option_Verbatim
  389. # undef kwsysProcess_Option_CreateProcessGroup
  390. # undef kwsysProcess_GetOption
  391. # undef kwsysProcess_SetOption
  392. # undef kwsysProcess_Option_e
  393. # undef kwsysProcess_State_Starting
  394. # undef kwsysProcess_State_Error
  395. # undef kwsysProcess_State_Exception
  396. # undef kwsysProcess_State_Executing
  397. # undef kwsysProcess_State_Exited
  398. # undef kwsysProcess_State_Expired
  399. # undef kwsysProcess_State_Killed
  400. # undef kwsysProcess_State_Disowned
  401. # undef kwsysProcess_GetState
  402. # undef kwsysProcess_State_e
  403. # undef kwsysProcess_Exception_None
  404. # undef kwsysProcess_Exception_Fault
  405. # undef kwsysProcess_Exception_Illegal
  406. # undef kwsysProcess_Exception_Interrupt
  407. # undef kwsysProcess_Exception_Numerical
  408. # undef kwsysProcess_Exception_Other
  409. # undef kwsysProcess_GetExitException
  410. # undef kwsysProcess_Exception_e
  411. # undef kwsysProcess_GetExitCode
  412. # undef kwsysProcess_GetExitValue
  413. # undef kwsysProcess_GetErrorString
  414. # undef kwsysProcess_GetExceptionString
  415. # undef kwsysProcess_Execute
  416. # undef kwsysProcess_Disown
  417. # undef kwsysProcess_WaitForData
  418. # undef kwsysProcess_Pipes_e
  419. # undef kwsysProcess_Pipe_None
  420. # undef kwsysProcess_Pipe_STDIN
  421. # undef kwsysProcess_Pipe_STDOUT
  422. # undef kwsysProcess_Pipe_STDERR
  423. # undef kwsysProcess_Pipe_Timeout
  424. # undef kwsysProcess_Pipe_Handle
  425. # undef kwsysProcess_WaitForExit
  426. # undef kwsysProcess_Interrupt
  427. # undef kwsysProcess_Kill
  428. # endif
  429. #endif
  430. #endif