/test/test-tty.c

http://github.com/joyent/libuv · C · 111 lines · 62 code · 21 blank · 28 comment · 10 complexity · e8474b8f3265a92e7619200676bf5284 MD5 · raw file

  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. TEST_IMPL(tty) {
  31. int r, width, height;
  32. int ttyin_fd, ttyout_fd;
  33. uv_tty_t tty_in, tty_out;
  34. uv_loop_t* loop = uv_default_loop();
  35. /* Make sure we have an FD that refers to a tty */
  36. #ifdef _WIN32
  37. HANDLE handle;
  38. handle = CreateFileA("conin$",
  39. GENERIC_READ | GENERIC_WRITE,
  40. FILE_SHARE_READ | FILE_SHARE_WRITE,
  41. NULL,
  42. OPEN_EXISTING,
  43. FILE_ATTRIBUTE_NORMAL,
  44. NULL);
  45. ASSERT(handle != INVALID_HANDLE_VALUE);
  46. ttyin_fd = _open_osfhandle((intptr_t) handle, 0);
  47. handle = CreateFileA("conout$",
  48. GENERIC_READ | GENERIC_WRITE,
  49. FILE_SHARE_READ | FILE_SHARE_WRITE,
  50. NULL,
  51. OPEN_EXISTING,
  52. FILE_ATTRIBUTE_NORMAL,
  53. NULL);
  54. ASSERT(handle != INVALID_HANDLE_VALUE);
  55. ttyout_fd = _open_osfhandle((intptr_t) handle, 0);
  56. #else /* unix */
  57. ttyin_fd = open("/dev/tty", O_RDONLY, 0);
  58. ttyout_fd = open("/dev/tty", O_WRONLY, 0);
  59. #endif
  60. ASSERT(ttyin_fd >= 0);
  61. ASSERT(ttyout_fd >= 0);
  62. ASSERT(UV_UNKNOWN_HANDLE == uv_guess_handle(-1));
  63. ASSERT(UV_TTY == uv_guess_handle(ttyin_fd));
  64. ASSERT(UV_TTY == uv_guess_handle(ttyout_fd));
  65. r = uv_tty_init(uv_default_loop(), &tty_in, ttyin_fd, 1);
  66. ASSERT(r == 0);
  67. r = uv_tty_init(uv_default_loop(), &tty_out, ttyout_fd, 2);
  68. ASSERT(r == 0);
  69. r = uv_tty_get_winsize(&tty_out, &width, &height);
  70. ASSERT(r == 0);
  71. printf("width=%d height=%d\n", width, height);
  72. /*
  73. * Is it a safe assumption that most people have terminals larger than
  74. * 10x10?
  75. */
  76. ASSERT(width > 10);
  77. ASSERT(height > 10);
  78. /* Turn on raw mode. */
  79. r = uv_tty_set_mode(&tty_in, 1);
  80. ASSERT(r == 0);
  81. /* Turn off raw mode. */
  82. r = uv_tty_set_mode(&tty_in, 0);
  83. ASSERT(r == 0);
  84. /* TODO check the actual mode! */
  85. uv_close((uv_handle_t*) &tty_in, NULL);
  86. uv_close((uv_handle_t*) &tty_out, NULL);
  87. uv_run(loop);
  88. MAKE_VALGRIND_HAPPY();
  89. return 0;
  90. }