ladybird/Userland/Utilities/paste.cpp

106 lines
3.2 KiB
C++
Raw Normal View History

/*
* 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>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/ByteString.h>
#include <AK/Format.h>
#include <LibCore/ArgsParser.h>
2022-02-19 05:26:39 +00:00
#include <LibCore/System.h>
#include <LibGUI/Application.h>
#include <LibGUI/Clipboard.h>
2022-02-19 05:26:39 +00:00
#include <LibMain/Main.h>
#include <stdio.h>
#include <stdlib.h>
#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)
{
2022-02-19 05:26:39 +00:00
auto pipefd = MUST(Core::System::pipe2(0));
pid_t pid = MUST(Core::System::fork());
2022-02-19 05:26:39 +00:00
if (pid == 0) {
// 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]));
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));
perror("exec");
exit(1);
}
// We're the parent.
2022-02-19 05:26:39 +00:00
MUST(Core::System::close(pipefd[0]));
FILE* f = fdopen(pipefd[1], "w");
fwrite(data.data(), data.size(), 1, f);
2022-02-19 05:26:39 +00:00
if (ferror(f))
warnln("failed to write data to the pipe: {}", strerror(ferror(f)));
2022-02-19 05:26:39 +00:00
fclose(f);
if (wait(nullptr) < 0)
perror("wait");
}
2022-02-19 05:26:39 +00:00
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
bool print_type = false;
bool no_newline = false;
bool watch = false;
2022-03-12 20:24:56 +00:00
Vector<StringView> watch_command;
Core::ArgsParser args_parser;
args_parser.set_general_help("Paste from the clipboard to stdout.");
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');
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);
auto app = TRY(GUI::Application::create(arguments));
auto& clipboard = GUI::Clipboard::the();
if (watch) {
watch_command.append({});
clipboard.on_change = [&](ByteString const&) {
// Technically there's a race here...
auto data_and_type = clipboard.fetch_data_and_type();
if (data_and_type.mime_type.is_empty()) {
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();
}
auto data_and_type = clipboard.fetch_data_and_type();
if (data_and_type.mime_type.is_empty()) {
warnln("Nothing copied");
return 1;
}
if (!print_type) {
out("{}", StringView(data_and_type.data));
// Append a newline to text contents, unless the caller says otherwise.
if (data_and_type.mime_type.starts_with("text/"sv) && !no_newline)
outln();
} else {
outln("{}", data_and_type.mime_type);
}
return 0;
}