Process.h.in 18 KB

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