Serenity Operating System
at portability 92 lines 3.2 kB view raw
1/* 2 * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/ByteBuffer.h> 28#include <AK/String.h> 29#include <AK/StringBuilder.h> 30#include <LibCore/ArgsParser.h> 31#include <LibCore/File.h> 32#include <LibGUI/Application.h> 33#include <LibGUI/Clipboard.h> 34#include <stdio.h> 35#include <stdlib.h> 36 37struct Options { 38 String data; 39 StringView type { "text" }; 40}; 41 42Options parse_options(int argc, char* argv[]) 43{ 44 const char* type = nullptr; 45 Vector<const char*> text; 46 47 Core::ArgsParser args_parser; 48 args_parser.add_option(type, "Pick a type", "type", 't', "type"); 49 args_parser.add_positional_argument(text, "Text to copy", "text", Core::ArgsParser::Required::No); 50 args_parser.parse(argc, argv); 51 52 Options options; 53 options.type = type; 54 55 if (text.is_empty()) { 56 // Copy our stdin. 57 auto c_stdin = Core::File::construct(); 58 bool success = c_stdin->open( 59 STDIN_FILENO, 60 Core::IODevice::OpenMode::ReadOnly, 61 Core::File::ShouldCloseFileDescription::No); 62 ASSERT(success); 63 auto buffer = c_stdin->read_all(); 64 dbg() << "Read size " << buffer.size(); 65 options.data = String((char*)buffer.data(), buffer.size()); 66 } else { 67 // Copy the rest of our command-line args. 68 StringBuilder builder; 69 bool first = true; 70 for (auto& word : text) { 71 if (!first) 72 builder.append(' '); 73 first = false; 74 builder.append(word); 75 } 76 options.data = builder.to_string(); 77 } 78 79 return options; 80} 81 82int main(int argc, char* argv[]) 83{ 84 GUI::Application app(argc, argv); 85 86 Options options = parse_options(argc, argv); 87 88 auto& clipboard = GUI::Clipboard::the(); 89 clipboard.set_data(options.data, options.type); 90 91 return 0; 92}