summaryrefslogtreecommitdiff
path: root/Userland/Utilities/paste.cpp
blob: 061d8c8fc700761afdd30f0b4e43a16975ae961d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * Copyright (c) 2019-2021, Sergey Bugaev <bugaevc@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/Format.h>
#include <AK/String.h>
#include <LibCore/ArgsParser.h>
#include <LibGUI/Application.h>
#include <LibGUI/Clipboard.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>

static void spawn_command(const Vector<const char*>& command, const ByteBuffer& data, const char* state)
{
    int pipefd[2];
    if (pipe(pipefd) < 0) {
        perror("pipe");
        return;
    }

    pid_t pid = fork();
    if (pid < 0) {
        perror("fork");
        return;
    } else if (pid == 0) {
        // We're the child.
        dup2(pipefd[0], 0);
        close(pipefd[0]);
        close(pipefd[1]);
        setenv("CLIPBOARD_STATE", state, true);
        execvp(command[0], const_cast<char**>(command.data()));
        perror("exec");
        exit(1);
    }

    // We're the parent.
    close(pipefd[0]);
    FILE* f = fdopen(pipefd[1], "w");
    fwrite(data.data(), data.size(), 1, f);
    if (ferror(f))
        warnln("failed to write data to the pipe: {}", strerror(ferror(f)));
    fclose(f);

    if (wait(nullptr) < 0)
        perror("wait");
}

int main(int argc, char* argv[])
{
    bool print_type = false;
    bool no_newline = false;
    bool watch = false;
    Vector<const char*> 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);
    args_parser.parse(argc, argv);

    auto app = GUI::Application::construct(argc, argv);

    auto& clipboard = GUI::Clipboard::the();

    if (watch) {
        watch_command.append(nullptr);

        clipboard.on_change = [&](const String&) {
            // Technically there's a race here...
            auto data_and_type = clipboard.data_and_type();
            if (data_and_type.mime_type.is_null()) {
                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.data_and_type();

    if (data_and_type.mime_type.is_null()) {
        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/") && !no_newline)
            outln();
    } else {
        outln("{}", data_and_type.mime_type);
    }

    return 0;
}