paste.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #include <AK/String.h>
  27. #include <LibGUI/GApplication.h>
  28. #include <LibGUI/GClipboard.h>
  29. #include <getopt.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. struct Options {
  33. bool print_type { false };
  34. bool no_newline { false };
  35. };
  36. void print_usage(FILE* stream, const char* argv0)
  37. {
  38. fprintf(
  39. stream,
  40. "Usage:\n"
  41. "\t%s [--print-type] [--no-newline]\n"
  42. "\n"
  43. "\t--print-type\t\tDisplay the copied type.\n"
  44. "\t-n, --no-newline\tDo not append a newline.\n"
  45. "\t-h, --help\t\tPrint this help message.\n",
  46. argv0);
  47. }
  48. Options parse_options(int argc, char* argv[])
  49. {
  50. Options options;
  51. static struct option long_options[] = {
  52. { "print-type", no_argument, 0, 'p' },
  53. { "no-newline", no_argument, 0, 'n' },
  54. { "help", no_argument, 0, 'h' },
  55. { 0, 0, 0, 0 }
  56. };
  57. while (true) {
  58. int option_index;
  59. int c = getopt_long(argc, argv, "hn", long_options, &option_index);
  60. if (c == -1)
  61. break;
  62. if (c == 0)
  63. c = long_options[option_index].val;
  64. switch (c) {
  65. case 'p':
  66. options.print_type = true;
  67. break;
  68. case 'n':
  69. options.no_newline = true;
  70. break;
  71. case 'h':
  72. print_usage(stdout, argv[0]);
  73. exit(0);
  74. default:
  75. print_usage(stderr, argv[0]);
  76. exit(1);
  77. }
  78. }
  79. return options;
  80. }
  81. int main(int argc, char* argv[])
  82. {
  83. GApplication app(argc, argv);
  84. Options options = parse_options(argc, argv);
  85. GClipboard& clipboard = GClipboard::the();
  86. auto data_and_type = clipboard.data_and_type();
  87. if (!options.print_type) {
  88. printf("%s", data_and_type.data.characters());
  89. // Append a newline to text contents, but
  90. // only if we're not asked not to do this.
  91. if (data_and_type.type == "text" && !options.no_newline)
  92. putchar('\n');
  93. } else {
  94. printf("%s\n", data_and_type.type.characters());
  95. }
  96. return 0;
  97. }