test-tty.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "uv.h"
  22. #include "task.h"
  23. #ifdef _WIN32
  24. # include <io.h>
  25. # include <windows.h>
  26. #else /* Unix */
  27. # include <fcntl.h>
  28. # include <unistd.h>
  29. #endif
  30. #include <string.h>
  31. #include <errno.h>
  32. TEST_IMPL(tty) {
  33. int r, width, height;
  34. int ttyin_fd, ttyout_fd;
  35. uv_tty_t tty_in, tty_out;
  36. uv_loop_t* loop = uv_default_loop();
  37. /* Make sure we have an FD that refers to a tty */
  38. #ifdef _WIN32
  39. HANDLE handle;
  40. handle = CreateFileA("conin$",
  41. GENERIC_READ | GENERIC_WRITE,
  42. FILE_SHARE_READ | FILE_SHARE_WRITE,
  43. NULL,
  44. OPEN_EXISTING,
  45. FILE_ATTRIBUTE_NORMAL,
  46. NULL);
  47. ASSERT(handle != INVALID_HANDLE_VALUE);
  48. ttyin_fd = _open_osfhandle((intptr_t) handle, 0);
  49. handle = CreateFileA("conout$",
  50. GENERIC_READ | GENERIC_WRITE,
  51. FILE_SHARE_READ | FILE_SHARE_WRITE,
  52. NULL,
  53. OPEN_EXISTING,
  54. FILE_ATTRIBUTE_NORMAL,
  55. NULL);
  56. ASSERT(handle != INVALID_HANDLE_VALUE);
  57. ttyout_fd = _open_osfhandle((intptr_t) handle, 0);
  58. #else /* unix */
  59. ttyin_fd = open("/dev/tty", O_RDONLY, 0);
  60. if (ttyin_fd < 0)
  61. LOGF("Cannot open /dev/tty as read-only: %s\n", strerror(errno));
  62. ttyout_fd = open("/dev/tty", O_WRONLY, 0);
  63. if (ttyout_fd < 0)
  64. LOGF("Cannot open /dev/tty as write-only: %s\n", strerror(errno));
  65. #endif
  66. ASSERT(ttyin_fd >= 0);
  67. ASSERT(ttyout_fd >= 0);
  68. ASSERT(UV_UNKNOWN_HANDLE == uv_guess_handle(-1));
  69. ASSERT(UV_TTY == uv_guess_handle(ttyin_fd));
  70. ASSERT(UV_TTY == uv_guess_handle(ttyout_fd));
  71. r = uv_tty_init(uv_default_loop(), &tty_in, ttyin_fd, 1);
  72. ASSERT(r == 0);
  73. r = uv_tty_init(uv_default_loop(), &tty_out, ttyout_fd, 2);
  74. ASSERT(r == 0);
  75. r = uv_tty_get_winsize(&tty_out, &width, &height);
  76. ASSERT(r == 0);
  77. printf("width=%d height=%d\n", width, height);
  78. /*
  79. * Is it a safe assumption that most people have terminals larger than
  80. * 10x10?
  81. */
  82. ASSERT(width > 10);
  83. ASSERT(height > 10);
  84. /* Turn on raw mode. */
  85. r = uv_tty_set_mode(&tty_in, 1);
  86. ASSERT(r == 0);
  87. /* Turn off raw mode. */
  88. r = uv_tty_set_mode(&tty_in, 0);
  89. ASSERT(r == 0);
  90. /* TODO check the actual mode! */
  91. uv_close((uv_handle_t*) &tty_in, NULL);
  92. uv_close((uv_handle_t*) &tty_out, NULL);
  93. uv_run(loop, UV_RUN_DEFAULT);
  94. MAKE_VALGRIND_HAPPY();
  95. return 0;
  96. }