The repository formerly known as dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pbd.c 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* Copyright (c) 2017, Curtis McEnroe <programble@gmail.com>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Affero General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <arpa/inet.h>
  17. #include <err.h>
  18. #include <fcntl.h>
  19. #include <netinet/in.h>
  20. #include <stdbool.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <sys/socket.h>
  25. #include <sys/wait.h>
  26. #include <sysexits.h>
  27. #include <unistd.h>
  28. typedef unsigned char byte;
  29. static void spawn(const char *cmd, const char *arg, int dest, int src) {
  30. pid_t pid = fork();
  31. if (pid < 0) err(EX_OSERR, "fork");
  32. if (pid) {
  33. int status;
  34. pid_t dead = waitpid(pid, &status, 0);
  35. if (dead < 0) err(EX_OSERR, "waitpid(%d)", pid);
  36. if (status) warnx("%s: status %d", cmd, status);
  37. } else {
  38. int fd = dup2(src, dest);
  39. if (fd < 0) err(EX_OSERR, "dup2");
  40. execlp(cmd, cmd, arg, NULL);
  41. err(EX_UNAVAILABLE, "%s", cmd);
  42. }
  43. }
  44. static int pbd(void) {
  45. int error;
  46. int server = socket(PF_INET, SOCK_STREAM, 0);
  47. if (server < 0) err(EX_OSERR, "socket");
  48. error = fcntl(server, F_SETFD, FD_CLOEXEC);
  49. if (error) err(EX_IOERR, "fcntl");
  50. struct sockaddr_in addr = {
  51. .sin_family = AF_INET,
  52. .sin_port = htons(7062),
  53. .sin_addr = { .s_addr = htonl(0x7F000001) },
  54. };
  55. error = bind(server, (struct sockaddr *)&addr, sizeof(addr));
  56. if (error) err(EX_UNAVAILABLE, "bind");
  57. error = listen(server, 0);
  58. if (error) err(EX_UNAVAILABLE, "listen");
  59. for (;;) {
  60. int client = accept(server, NULL, NULL);
  61. if (client < 0) err(EX_IOERR, "accept");
  62. error = fcntl(client, F_SETFD, FD_CLOEXEC);
  63. if (error) err(EX_IOERR, "fcntl");
  64. char c = 0;
  65. ssize_t size = read(client, &c, 1);
  66. if (size < 0) warn("read");
  67. switch (c) {
  68. break; case 'p': spawn("pbpaste", NULL, STDOUT_FILENO, client);
  69. break; case 'c': spawn("pbcopy", NULL, STDIN_FILENO, client);
  70. break; case 'o': spawn("xargs", "open", STDIN_FILENO, client);
  71. }
  72. close(client);
  73. }
  74. }
  75. static int pbdClient(char c) {
  76. int client = socket(PF_INET, SOCK_STREAM, 0);
  77. if (client < 0) err(EX_OSERR, "socket");
  78. struct sockaddr_in addr = {
  79. .sin_family = AF_INET,
  80. .sin_port = htons(7062),
  81. .sin_addr = { .s_addr = htonl(0x7F000001) },
  82. };
  83. int error = connect(client, (struct sockaddr *)&addr, sizeof(addr));
  84. if (error) err(EX_UNAVAILABLE, "connect");
  85. ssize_t size = write(client, &c, 1);
  86. if (size < 0) err(EX_IOERR, "write");
  87. return client;
  88. }
  89. static void copy(int out, int in) {
  90. byte buf[4096];
  91. ssize_t readSize;
  92. while (0 < (readSize = read(in, buf, sizeof(buf)))) {
  93. ssize_t writeSize = write(out, buf, readSize);
  94. if (writeSize < 0) err(EX_IOERR, "write(%d)", out);
  95. }
  96. if (readSize < 0) err(EX_IOERR, "read(%d)", in);
  97. }
  98. static int pbcopy(void) {
  99. int client = pbdClient('c');
  100. copy(client, STDIN_FILENO);
  101. return EX_OK;
  102. }
  103. static int pbpaste(void) {
  104. int client = pbdClient('p');
  105. copy(STDOUT_FILENO, client);
  106. return EX_OK;
  107. }
  108. static int open1(const char *url) {
  109. if (!url) return EX_USAGE;
  110. int client = pbdClient('o');
  111. ssize_t size = write(client, url, strlen(url));
  112. if (size < 0) err(EX_IOERR, "write");
  113. return EX_OK;
  114. }
  115. int main(int argc, char *argv[]) {
  116. (void)argc;
  117. if (!argv[0][0] || !argv[0][1]) return EX_USAGE;
  118. switch (argv[0][2]) {
  119. case 'd': return pbd();
  120. case 'c': return pbcopy();
  121. case 'p': return pbpaste();
  122. case 'e': return open1(argv[1]);
  123. default: return EX_USAGE;
  124. }
  125. }