Process.h.in 20 KB

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