Command.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. * Copyright (c) 2022, David Tuin <davidot@serenityos.org>
  4. * Copyright (c) 2023, Shannon Booth <shannon@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/ByteBuffer.h>
  10. #include <AK/LexicalPath.h>
  11. #include <AK/Optional.h>
  12. #include <AK/String.h>
  13. #include <LibCore/File.h>
  14. #include <LibCore/Forward.h>
  15. #include <spawn.h>
  16. namespace Core {
  17. // FIXME: Unify this and the below 'command' functions with Command class below
  18. struct CommandResult {
  19. int exit_code { 0 };
  20. ByteBuffer output;
  21. ByteBuffer error;
  22. };
  23. ErrorOr<CommandResult> command(ByteString const& program, Vector<ByteString> const& arguments, Optional<LexicalPath> chdir);
  24. ErrorOr<CommandResult> command(ByteString const& command_string, Optional<LexicalPath> chdir);
  25. class Command {
  26. public:
  27. struct ProcessOutputs {
  28. ByteBuffer standard_output;
  29. ByteBuffer standard_error;
  30. };
  31. static ErrorOr<OwnPtr<Command>> create(StringView command, char const* const arguments[]);
  32. Command(pid_t pid, NonnullOwnPtr<Core::File> stdin_file, NonnullOwnPtr<Core::File> stdout_file, NonnullOwnPtr<Core::File> stderr_file);
  33. ErrorOr<void> write(StringView input);
  34. ErrorOr<void> write_lines(Span<ByteString> lines);
  35. ErrorOr<ProcessOutputs> read_all();
  36. enum class ProcessResult {
  37. Running,
  38. DoneWithZeroExitCode,
  39. Failed,
  40. FailedFromTimeout,
  41. Unknown,
  42. };
  43. ErrorOr<ProcessResult> status(int options = 0);
  44. private:
  45. pid_t m_pid { -1 };
  46. NonnullOwnPtr<Core::File> m_stdin;
  47. NonnullOwnPtr<Core::File> m_stdout;
  48. NonnullOwnPtr<Core::File> m_stderr;
  49. };
  50. }