PageRenderTime 32ms CodeModel.GetById 20ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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