aboutsummaryrefslogtreecommitdiff
path: root/test/functional/fixtures/tty-test.c
diff options
context:
space:
mode:
authorZyX <kp-pav@yandex.ru>2017-12-03 16:49:30 +0300
committerZyX <kp-pav@yandex.ru>2017-12-03 16:49:30 +0300
commitc49e22d3964d6c7ae1c24e8ad01b5fec4ca40b57 (patch)
treeb7e59c416d1435725c65f8952b6e55c70544d97e /test/functional/fixtures/tty-test.c
parent62108c3b0be46936c83f6d4c98b44ceb5e6f77fd (diff)
parent27a577586eace687c47e7398845178208cae524a (diff)
downloadrneovim-c49e22d3964d6c7ae1c24e8ad01b5fec4ca40b57.tar.gz
rneovim-c49e22d3964d6c7ae1c24e8ad01b5fec4ca40b57.tar.bz2
rneovim-c49e22d3964d6c7ae1c24e8ad01b5fec4ca40b57.zip
Merge branch 'master' into s-dash-stdin
Diffstat (limited to 'test/functional/fixtures/tty-test.c')
-rw-r--r--test/functional/fixtures/tty-test.c91
1 files changed, 55 insertions, 36 deletions
diff --git a/test/functional/fixtures/tty-test.c b/test/functional/fixtures/tty-test.c
index 778e7f3cd3..edcbe23f86 100644
--- a/test/functional/fixtures/tty-test.c
+++ b/test/functional/fixtures/tty-test.c
@@ -1,40 +1,49 @@
+// This is an open source non-commercial project. Dear PVS-Studio, please check
+// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
+
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <uv.h>
+#ifdef _WIN32
+# include <windows.h>
+#else
+# include <unistd.h>
+#endif
+
+// -V:STRUCT_CAST:641
+#define STRUCT_CAST(Type, obj) ((Type *)(obj))
+#define is_terminal(stream) (uv_guess_handle(fileno(stream)) == UV_TTY)
+#define BUF_SIZE 0xfff
+#define CTRL_C 0x03
uv_tty_t tty;
+uv_tty_t tty_out;
-#ifdef _WIN32
-#include <windows.h>
bool owns_tty(void)
{
- HWND consoleWnd = GetConsoleWindow();
- DWORD dwProcessId;
- GetWindowThreadProcessId(consoleWnd, &dwProcessId);
- return GetCurrentProcessId() == dwProcessId;
-}
+#ifdef _WIN32
+ // XXX: We need to make proper detect owns tty
+ // HWND consoleWnd = GetConsoleWindow();
+ // DWORD dwProcessId;
+ // GetWindowThreadProcessId(consoleWnd, &dwProcessId);
+ // return GetCurrentProcessId() == dwProcessId;
+ return true;
#else
-#include <unistd.h>
-bool owns_tty(void)
-{
return getsid(0) == getpid();
-}
#endif
+}
-#define is_terminal(stream) (uv_guess_handle(fileno(stream)) == UV_TTY)
-#define BUF_SIZE 0xfff
-
-static void walk_cb(uv_handle_t *handle, void *arg) {
+static void walk_cb(uv_handle_t *handle, void *arg)
+{
if (!uv_is_closing(handle)) {
uv_close(handle, NULL);
}
}
-#ifndef WIN32
static void sig_handler(int signum)
{
- switch(signum) {
+ switch (signum) {
case SIGWINCH: {
int width, height;
uv_tty_get_winsize(&tty, &width, &height);
@@ -48,15 +57,15 @@ static void sig_handler(int signum)
return;
}
}
-#endif
-// static void sigwinch_cb(uv_signal_t *handle, int signum)
-// {
-// int width, height;
-// uv_tty_t *tty = handle->data;
-// uv_tty_get_winsize(tty, &width, &height);
-// fprintf(stderr, "rows: %d, cols: %d\n", height, width);
-// }
+#ifdef WIN32
+static void sigwinch_cb(uv_signal_t *handle, int signum)
+{
+ int width, height;
+ uv_tty_get_winsize(&tty_out, &width, &height);
+ fprintf(stderr, "rows: %d, cols: %d\n", height, width);
+}
+#endif
static void alloc_cb(uv_handle_t *handle, size_t suggested, uv_buf_t *buf)
{
@@ -74,7 +83,7 @@ static void read_cb(uv_stream_t *stream, ssize_t cnt, const uv_buf_t *buf)
int *interrupted = stream->data;
for (int i = 0; i < cnt; i++) {
- if (buf->base[i] == 3) {
+ if (buf->base[i] == CTRL_C) {
(*interrupted)++;
}
}
@@ -82,12 +91,14 @@ static void read_cb(uv_stream_t *stream, ssize_t cnt, const uv_buf_t *buf)
uv_loop_t write_loop;
uv_loop_init(&write_loop);
uv_tty_t out;
- uv_tty_init(&write_loop, &out, 1, 0);
+ uv_tty_init(&write_loop, &out, fileno(stdout), 0);
+
uv_write_t req;
uv_buf_t b = {.base = buf->base, .len = (size_t)cnt};
- uv_write(&req, (uv_stream_t *)&out, &b, 1, NULL);
+ uv_write(&req, STRUCT_CAST(uv_stream_t, &out), &b, 1, NULL);
uv_run(&write_loop, UV_RUN_DEFAULT);
- uv_close((uv_handle_t *)&out, NULL);
+
+ uv_close(STRUCT_CAST(uv_handle_t, &out), NULL);
uv_run(&write_loop, UV_RUN_DEFAULT);
if (uv_loop_close(&write_loop)) {
abort();
@@ -131,7 +142,7 @@ int main(int argc, char **argv)
if (argc > 1) {
int count = atoi(argv[1]);
- for (int i = 0; i < count; ++i) {
+ for (int i = 0; i < count; i++) {
printf("line%d\n", i);
}
fflush(stdout);
@@ -142,11 +153,17 @@ int main(int argc, char **argv)
uv_prepare_t prepare;
uv_prepare_init(uv_default_loop(), &prepare);
uv_prepare_start(&prepare, prepare_cb);
- // uv_tty_t tty;
+#ifndef WIN32
uv_tty_init(uv_default_loop(), &tty, fileno(stderr), 1);
+#else
+ uv_tty_init(uv_default_loop(), &tty, fileno(stdin), 1);
+ uv_tty_init(uv_default_loop(), &tty_out, fileno(stdout), 0);
+ int width, height;
+ uv_tty_get_winsize(&tty_out, &width, &height);
+#endif
uv_tty_set_mode(&tty, UV_TTY_MODE_RAW);
tty.data = &interrupted;
- uv_read_start((uv_stream_t *)&tty, alloc_cb, read_cb);
+ uv_read_start(STRUCT_CAST(uv_stream_t, &tty), alloc_cb, read_cb);
#ifndef WIN32
struct sigaction sa;
sigemptyset(&sa.sa_mask);
@@ -154,15 +171,17 @@ int main(int argc, char **argv)
sa.sa_handler = sig_handler;
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGWINCH, &sa, NULL);
- // uv_signal_t sigwinch_watcher;
- // uv_signal_init(uv_default_loop(), &sigwinch_watcher);
- // sigwinch_watcher.data = &tty;
- // uv_signal_start(&sigwinch_watcher, sigwinch_cb, SIGWINCH);
+#else
+ uv_signal_t sigwinch_watcher;
+ uv_signal_init(uv_default_loop(), &sigwinch_watcher);
+ uv_signal_start(&sigwinch_watcher, sigwinch_cb, SIGWINCH);
#endif
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
+#ifndef WIN32
// XXX: Without this the SIGHUP handler is skipped on some systems.
sleep(100);
+#endif
return 0;
}