2020-09-15 18:45:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
2023-07-17 04:39:07 +00:00
|
|
|
* Copyright (c) 2022, David Tuin <davidot@serenityos.org>
|
|
|
|
* Copyright (c) 2023, Shannon Booth <shannon@serenityos.org>
|
2020-09-15 18:45:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-09-15 18:45:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-05-13 16:55:15 +00:00
|
|
|
#include <AK/ByteBuffer.h>
|
2020-09-15 18:45:21 +00:00
|
|
|
#include <AK/LexicalPath.h>
|
|
|
|
#include <AK/Optional.h>
|
2023-05-13 16:55:15 +00:00
|
|
|
#include <AK/String.h>
|
2023-07-17 04:39:07 +00:00
|
|
|
#include <LibCore/File.h>
|
|
|
|
#include <LibCore/Forward.h>
|
2020-09-15 18:45:21 +00:00
|
|
|
#include <spawn.h>
|
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
|
2023-07-17 04:39:07 +00:00
|
|
|
// FIXME: Unify this and the below 'command' functions with Command class below
|
2022-01-07 14:17:19 +00:00
|
|
|
struct CommandResult {
|
|
|
|
int exit_code { 0 };
|
2023-05-13 16:55:15 +00:00
|
|
|
ByteBuffer output;
|
|
|
|
ByteBuffer error;
|
2022-01-07 14:17:19 +00:00
|
|
|
};
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ErrorOr<CommandResult> command(ByteString const& program, Vector<ByteString> const& arguments, Optional<LexicalPath> chdir);
|
|
|
|
ErrorOr<CommandResult> command(ByteString const& command_string, Optional<LexicalPath> chdir);
|
2020-09-15 18:45:21 +00:00
|
|
|
|
2023-07-17 04:39:07 +00:00
|
|
|
class Command {
|
|
|
|
public:
|
|
|
|
struct ProcessOutputs {
|
|
|
|
ByteBuffer standard_output;
|
|
|
|
ByteBuffer standard_error;
|
|
|
|
};
|
|
|
|
|
|
|
|
static ErrorOr<OwnPtr<Command>> create(StringView command, char const* const arguments[]);
|
|
|
|
|
|
|
|
Command(pid_t pid, NonnullOwnPtr<Core::File> stdin_file, NonnullOwnPtr<Core::File> stdout_file, NonnullOwnPtr<Core::File> stderr_file);
|
|
|
|
|
|
|
|
ErrorOr<void> write(StringView input);
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ErrorOr<void> write_lines(Span<ByteString> lines);
|
2023-07-17 04:39:07 +00:00
|
|
|
|
|
|
|
ErrorOr<ProcessOutputs> read_all();
|
|
|
|
|
|
|
|
enum class ProcessResult {
|
|
|
|
Running,
|
|
|
|
DoneWithZeroExitCode,
|
|
|
|
Failed,
|
|
|
|
FailedFromTimeout,
|
|
|
|
Unknown,
|
|
|
|
};
|
|
|
|
|
|
|
|
ErrorOr<ProcessResult> status(int options = 0);
|
|
|
|
|
|
|
|
private:
|
|
|
|
pid_t m_pid { -1 };
|
|
|
|
NonnullOwnPtr<Core::File> m_stdin;
|
|
|
|
NonnullOwnPtr<Core::File> m_stdout;
|
|
|
|
NonnullOwnPtr<Core::File> m_stderr;
|
|
|
|
};
|
|
|
|
|
2020-09-15 18:45:21 +00:00
|
|
|
}
|