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.

hnel.c 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 <err.h>
  17. #include <poll.h>
  18. #include <stdbool.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <sys/ioctl.h>
  22. #include <sys/wait.h>
  23. #include <sysexits.h>
  24. #include <termios.h>
  25. #include <unistd.h>
  26. #if defined __FreeBSD__
  27. #include <libutil.h>
  28. #elif defined __linux__
  29. #include <pty.h>
  30. #else
  31. #include <util.h>
  32. #endif
  33. typedef unsigned char byte;
  34. static struct termios saveTerm;
  35. static void restoreTerm(void) {
  36. tcsetattr(STDIN_FILENO, TCSADRAIN, &saveTerm);
  37. }
  38. int main(int argc, char *argv[]) {
  39. int error;
  40. if (argc < 4) return EX_USAGE;
  41. byte table[256] = {0};
  42. if (strlen(argv[1]) != strlen(argv[2])) return EX_USAGE;
  43. for (const char *from = argv[1], *to = argv[2]; *from; ++from, ++to) {
  44. table[(byte)*from] = *to;
  45. }
  46. error = tcgetattr(STDERR_FILENO, &saveTerm);
  47. if (error) err(EX_IOERR, "tcgetattr");
  48. atexit(restoreTerm);
  49. struct termios raw = saveTerm;
  50. cfmakeraw(&raw);
  51. error = tcsetattr(STDERR_FILENO, TCSADRAIN, &raw);
  52. if (error) err(EX_IOERR, "tcsetattr");
  53. struct winsize window;
  54. error = ioctl(STDERR_FILENO, TIOCGWINSZ, &window);
  55. if (error) err(EX_IOERR, "TIOCGWINSZ");
  56. int pty;
  57. pid_t pid = forkpty(&pty, NULL, NULL, &window);
  58. if (pid < 0) err(EX_OSERR, "forkpty");
  59. if (!pid) {
  60. execvp(argv[3], &argv[3]);
  61. err(EX_NOINPUT, "%s", argv[3]);
  62. }
  63. bool enable = true;
  64. byte buf[4096];
  65. struct pollfd fds[2] = {
  66. { .fd = STDIN_FILENO, .events = POLLIN },
  67. { .fd = pty, .events = POLLIN },
  68. };
  69. while (0 < poll(fds, 2, -1)) {
  70. if (fds[0].revents & POLLIN) {
  71. ssize_t readSize = read(STDIN_FILENO, buf, sizeof(buf));
  72. if (readSize < 0) err(EX_IOERR, "read(%d)", STDIN_FILENO);
  73. if (readSize == 1) {
  74. if (buf[0] == CTRL('S')) {
  75. enable ^= true;
  76. continue;
  77. }
  78. byte c = buf[0];
  79. if (enable && table[c]) buf[0] = table[c];
  80. }
  81. ssize_t writeSize = write(pty, buf, readSize);
  82. if (writeSize < 0) err(EX_IOERR, "write(%d)", pty);
  83. if (writeSize < readSize) errx(EX_IOERR, "short write(%d)", pty);
  84. }
  85. if (fds[1].revents & POLLIN) {
  86. ssize_t readSize = read(pty, buf, sizeof(buf));
  87. if (readSize < 0) err(EX_IOERR, "read(%d)", pty);
  88. ssize_t writeSize = write(STDOUT_FILENO, buf, readSize);
  89. if (writeSize < 0) err(EX_IOERR, "write(%d)", STDOUT_FILENO);
  90. if (writeSize < readSize) {
  91. errx(EX_IOERR, "short write(%d)", STDOUT_FILENO);
  92. }
  93. }
  94. int status;
  95. pid_t dead = waitpid(pid, &status, WNOHANG);
  96. if (dead < 0) err(EX_OSERR, "waitpid(%d)", pid);
  97. if (dead) return WIFEXITED(status) ? WEXITSTATUS(status) : EX_SOFTWARE;
  98. }
  99. err(EX_IOERR, "poll");
  100. }