drain.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #pragma once
  2. #include <reproc/reproc.h>
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. /*! Used by `reproc_drain` to provide data to the caller. Each time data is
  7. read, `function` is called with `context`. If a sink returns a non-zero value,
  8. `reproc_drain` will return immediately with the same value. */
  9. typedef struct reproc_sink {
  10. int (*function)(REPROC_STREAM stream,
  11. const uint8_t *buffer,
  12. size_t size,
  13. void *context);
  14. void *context;
  15. } reproc_sink;
  16. /*! Pass `REPROC_SINK_NULL` as the sink for output streams that have not been
  17. redirected to a pipe. */
  18. REPROC_EXPORT extern const reproc_sink REPROC_SINK_NULL;
  19. /*!
  20. Reads from the child process stdout and stderr until an error occurs or both
  21. streams are closed. The `out` and `err` sinks receive the output from stdout and
  22. stderr respectively. The same sink may be passed to both `out` and `err`.
  23. `reproc_drain` always starts by calling both sinks once with an empty buffer and
  24. `stream` set to `REPROC_STREAM_IN` to give each sink the chance to process all
  25. output from the previous call to `reproc_drain` one by one.
  26. When a stream is closed, its corresponding `sink` is called once with `size` set
  27. to zero.
  28. Note that his function returns 0 instead of `REPROC_EPIPE` when both output
  29. streams of the child process are closed.
  30. Actionable errors:
  31. - `REPROC_ETIMEDOUT`
  32. */
  33. REPROC_EXPORT int
  34. reproc_drain(reproc_t *process, reproc_sink out, reproc_sink err);
  35. /*!
  36. Appends the output of a process (stdout and stderr) to the value of `output`.
  37. `output` must point to either `NULL` or a NUL-terminated string.
  38. Calls `realloc` as necessary to make space in `output` to store the output of
  39. the child process. Make sure to always call `reproc_free` on the value of
  40. `output` after calling `reproc_drain` (even if it fails).
  41. Because the resulting sink does not store the output size, `strlen` is called
  42. each time data is read to calculate the current size of the output. This might
  43. cause performance problems when draining processes that produce a lot of output.
  44. Similarly, this sink will not work on processes that have NUL terminators in
  45. their output because `strlen` is used to calculate the current output size.
  46. Returns `REPROC_ENOMEM` if a call to `realloc` fails. `output` will contain any
  47. output read from the child process, preceeded by whatever was stored in it at
  48. the moment its corresponding sink was passed to `reproc_drain`.
  49. The `drain` example shows how to use `reproc_sink_string`.
  50. ```
  51. */
  52. REPROC_EXPORT reproc_sink reproc_sink_string(char **output);
  53. /*! Discards the output of a process. */
  54. REPROC_EXPORT reproc_sink reproc_sink_discard(void);
  55. /*! Calls `free` on `ptr` and returns `NULL`. Use this function to free memory
  56. allocated by `reproc_sink_string`. This avoids issues with allocating across
  57. module (DLL) boundaries on Windows. */
  58. REPROC_EXPORT void *reproc_free(void *ptr);
  59. #ifdef __cplusplus
  60. }
  61. #endif