2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2021-05-10 16:56:32 +00:00
|
|
|
* Copyright (c) 2019-2021, Sergey Bugaev <bugaevc@serenityos.org>
|
2022-02-19 05:26:39 +00:00
|
|
|
* Copyright (c) 2022, Zachary Penn <zack@sysdevs.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
#include <AK/ByteString.h>
|
2021-05-10 16:56:32 +00:00
|
|
|
#include <AK/Format.h>
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2022-02-19 05:26:39 +00:00
|
|
|
#include <LibCore/System.h>
|
2020-02-06 19:33:02 +00:00
|
|
|
#include <LibGUI/Application.h>
|
|
|
|
#include <LibGUI/Clipboard.h>
|
2022-02-19 05:26:39 +00:00
|
|
|
#include <LibMain/Main.h>
|
2021-05-10 16:56:32 +00:00
|
|
|
#include <stdio.h>
|
2019-09-22 18:50:39 +00:00
|
|
|
#include <stdlib.h>
|
2021-05-10 16:56:32 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2022-03-12 20:24:56 +00:00
|
|
|
static void spawn_command(Span<StringView> command, ByteBuffer const& data, char const* state)
|
2021-05-10 16:56:32 +00:00
|
|
|
{
|
2022-02-19 05:26:39 +00:00
|
|
|
auto pipefd = MUST(Core::System::pipe2(0));
|
|
|
|
pid_t pid = MUST(Core::System::fork());
|
2021-05-10 16:56:32 +00:00
|
|
|
|
2022-02-19 05:26:39 +00:00
|
|
|
if (pid == 0) {
|
2021-05-10 16:56:32 +00:00
|
|
|
// We're the child.
|
2022-02-19 05:26:39 +00:00
|
|
|
MUST(Core::System::dup2(pipefd[0], 0));
|
|
|
|
MUST(Core::System::close(pipefd[0]));
|
|
|
|
MUST(Core::System::close(pipefd[1]));
|
2022-07-11 19:53:29 +00:00
|
|
|
MUST(Core::System::setenv("CLIPBOARD_STATE"sv, { state, strlen(state) }, true));
|
2022-03-12 20:24:56 +00:00
|
|
|
MUST(Core::System::exec(command[0], command, Core::System::SearchInPath::Yes));
|
2021-05-10 16:56:32 +00:00
|
|
|
perror("exec");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're the parent.
|
2022-02-19 05:26:39 +00:00
|
|
|
MUST(Core::System::close(pipefd[0]));
|
2021-05-10 16:56:32 +00:00
|
|
|
FILE* f = fdopen(pipefd[1], "w");
|
|
|
|
fwrite(data.data(), data.size(), 1, f);
|
2022-02-19 05:26:39 +00:00
|
|
|
|
2021-05-10 16:56:32 +00:00
|
|
|
if (ferror(f))
|
|
|
|
warnln("failed to write data to the pipe: {}", strerror(ferror(f)));
|
2022-02-19 05:26:39 +00:00
|
|
|
|
2021-05-10 16:56:32 +00:00
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
if (wait(nullptr) < 0)
|
|
|
|
perror("wait");
|
|
|
|
}
|
2019-09-17 18:45:38 +00:00
|
|
|
|
2022-02-19 05:26:39 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-09-17 18:45:38 +00:00
|
|
|
{
|
2020-01-27 17:25:36 +00:00
|
|
|
bool print_type = false;
|
|
|
|
bool no_newline = false;
|
2021-05-10 16:56:32 +00:00
|
|
|
bool watch = false;
|
2022-03-12 20:24:56 +00:00
|
|
|
Vector<StringView> watch_command;
|
2019-09-17 18:45:38 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
Core::ArgsParser args_parser;
|
2020-12-05 15:22:58 +00:00
|
|
|
args_parser.set_general_help("Paste from the clipboard to stdout.");
|
2020-01-27 17:25:36 +00:00
|
|
|
args_parser.add_option(print_type, "Display the copied type", "print-type", 0);
|
|
|
|
args_parser.add_option(no_newline, "Do not append a newline", "no-newline", 'n');
|
2021-05-10 16:56:32 +00:00
|
|
|
args_parser.add_option(watch, "Run a command when clipboard data changes", "watch", 'w');
|
|
|
|
args_parser.add_positional_argument(watch_command, "Command to run in watch mode", "command", Core::ArgsParser::Required::No);
|
2022-02-19 05:26:39 +00:00
|
|
|
args_parser.parse(arguments);
|
2019-09-17 18:45:38 +00:00
|
|
|
|
2023-05-05 04:24:53 +00:00
|
|
|
auto app = TRY(GUI::Application::create(arguments));
|
2019-09-17 18:45:38 +00:00
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
auto& clipboard = GUI::Clipboard::the();
|
2021-05-10 16:56:32 +00:00
|
|
|
|
|
|
|
if (watch) {
|
2022-07-11 20:18:40 +00:00
|
|
|
watch_command.append({});
|
2021-05-10 16:56:32 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
clipboard.on_change = [&](ByteString const&) {
|
2021-05-10 16:56:32 +00:00
|
|
|
// Technically there's a race here...
|
2021-11-20 14:22:01 +00:00
|
|
|
auto data_and_type = clipboard.fetch_data_and_type();
|
2023-10-10 11:30:58 +00:00
|
|
|
if (data_and_type.mime_type.is_empty()) {
|
2021-05-10 16:56:32 +00:00
|
|
|
spawn_command(watch_command, {}, "clear");
|
|
|
|
} else {
|
|
|
|
spawn_command(watch_command, data_and_type.data, "data");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Trigger it the first time immediately.
|
|
|
|
clipboard.on_change({});
|
|
|
|
|
|
|
|
return app->exec();
|
|
|
|
}
|
|
|
|
|
2021-11-20 14:22:01 +00:00
|
|
|
auto data_and_type = clipboard.fetch_data_and_type();
|
2019-09-17 18:45:38 +00:00
|
|
|
|
2023-10-10 11:30:58 +00:00
|
|
|
if (data_and_type.mime_type.is_empty()) {
|
2020-12-26 23:59:18 +00:00
|
|
|
warnln("Nothing copied");
|
2020-05-15 19:35:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-01-27 17:25:36 +00:00
|
|
|
if (!print_type) {
|
2020-12-26 23:59:18 +00:00
|
|
|
out("{}", StringView(data_and_type.data));
|
2020-12-05 15:22:58 +00:00
|
|
|
// Append a newline to text contents, unless the caller says otherwise.
|
2022-07-11 17:32:29 +00:00
|
|
|
if (data_and_type.mime_type.starts_with("text/"sv) && !no_newline)
|
2020-12-26 23:59:18 +00:00
|
|
|
outln();
|
2019-09-17 18:45:38 +00:00
|
|
|
} else {
|
2020-12-26 23:59:18 +00:00
|
|
|
outln("{}", data_and_type.mime_type);
|
2019-09-17 18:45:38 +00:00
|
|
|
}
|
2019-09-23 07:36:25 +00:00
|
|
|
|
|
|
|
return 0;
|
2019-09-17 18:45:38 +00:00
|
|
|
}
|