test-tty.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. return TEST_SKIP;
  63. }
  64. ttyout_fd = open("/dev/tty", O_WRONLY, 0);
  65. if (ttyout_fd < 0) {
  66. LOGF("Cannot open /dev/tty as write-only: %s\n", strerror(errno));
  67. return TEST_SKIP;
  68. }
  69. #endif
  70. ASSERT(ttyin_fd >= 0);
  71. ASSERT(ttyout_fd >= 0);
  72. ASSERT(UV_UNKNOWN_HANDLE == uv_guess_handle(-1));
  73. ASSERT(UV_TTY == uv_guess_handle(ttyin_fd));
  74. ASSERT(UV_TTY == uv_guess_handle(ttyout_fd));
  75. r = uv_tty_init(uv_default_loop(), &tty_in, ttyin_fd, 1); /* Readable. */
  76. ASSERT(r == 0);
  77. r = uv_tty_init(uv_default_loop(), &tty_out, ttyout_fd, 0); /* Writable. */
  78. ASSERT(r == 0);
  79. r = uv_tty_get_winsize(&tty_out, &width, &height);
  80. ASSERT(r == 0);
  81. printf("width=%d height=%d\n", width, height);
  82. /*
  83. * Is it a safe assumption that most people have terminals larger than
  84. * 10x10?
  85. */
  86. ASSERT(width > 10);
  87. ASSERT(height > 10);
  88. /* Turn on raw mode. */
  89. r = uv_tty_set_mode(&tty_in, 1);
  90. ASSERT(r == 0);
  91. /* Turn off raw mode. */
  92. r = uv_tty_set_mode(&tty_in, 0);
  93. ASSERT(r == 0);
  94. /* TODO check the actual mode! */
  95. uv_close((uv_handle_t*) &tty_in, NULL);
  96. uv_close((uv_handle_t*) &tty_out, NULL);
  97. uv_run(loop, UV_RUN_DEFAULT);
  98. MAKE_VALGRIND_HAPPY();
  99. return 0;
  100. }