2015-02-23 08:34:20 -07:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <uv.h>
|
2022-09-17 18:17:15 -07:00
|
|
|
#ifdef MSWIN
|
2017-08-06 05:26:17 -07:00
|
|
|
# include <windows.h>
|
|
|
|
#else
|
|
|
|
# include <unistd.h>
|
2017-04-02 02:32:23 -07:00
|
|
|
#endif
|
2015-02-23 08:34:20 -07:00
|
|
|
|
2017-04-02 02:32:23 -07:00
|
|
|
#define is_terminal(stream) (uv_guess_handle(fileno(stream)) == UV_TTY)
|
|
|
|
#define BUF_SIZE 0xfff
|
|
|
|
#define CTRL_C 0x03
|
2017-08-06 05:26:17 -07:00
|
|
|
|
|
|
|
uv_tty_t tty;
|
2017-08-13 06:24:25 -07:00
|
|
|
uv_tty_t tty_out;
|
2015-03-25 05:14:47 -07:00
|
|
|
|
2019-01-29 18:26:12 -07:00
|
|
|
bool owns_tty(void); // silence -Wmissing-prototypes
|
2015-02-23 08:34:20 -07:00
|
|
|
bool owns_tty(void)
|
|
|
|
{
|
2022-09-17 18:17:15 -07:00
|
|
|
#ifdef MSWIN
|
2017-03-30 13:40:37 -07:00
|
|
|
// XXX: We need to make proper detect owns tty
|
|
|
|
// HWND consoleWnd = GetConsoleWindow();
|
|
|
|
// DWORD dwProcessId;
|
|
|
|
// GetWindowThreadProcessId(consoleWnd, &dwProcessId);
|
|
|
|
// return GetCurrentProcessId() == dwProcessId;
|
2017-03-28 02:07:58 -07:00
|
|
|
return true;
|
2015-02-23 08:34:20 -07:00
|
|
|
#else
|
2015-03-25 05:14:47 -07:00
|
|
|
return getsid(0) == getpid();
|
2015-02-23 08:34:20 -07:00
|
|
|
#endif
|
2017-08-06 05:26:17 -07:00
|
|
|
}
|
2015-02-23 08:34:20 -07:00
|
|
|
|
2017-04-02 02:32:23 -07:00
|
|
|
static void walk_cb(uv_handle_t *handle, void *arg)
|
|
|
|
{
|
2015-02-23 08:34:20 -07:00
|
|
|
if (!uv_is_closing(handle)) {
|
2022-09-17 18:17:15 -07:00
|
|
|
#ifdef MSWIN
|
2019-08-17 20:58:35 -07:00
|
|
|
uv_tty_set_mode(&tty, UV_TTY_MODE_NORMAL);
|
|
|
|
#endif
|
2015-02-23 08:34:20 -07:00
|
|
|
uv_close(handle, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-17 18:17:15 -07:00
|
|
|
#ifndef MSWIN
|
2016-12-07 06:01:51 -07:00
|
|
|
static void sig_handler(int signum)
|
2015-02-23 08:34:20 -07:00
|
|
|
{
|
2017-04-02 02:32:23 -07:00
|
|
|
switch (signum) {
|
2016-12-07 06:01:51 -07:00
|
|
|
case SIGWINCH: {
|
|
|
|
int width, height;
|
|
|
|
uv_tty_get_winsize(&tty, &width, &height);
|
|
|
|
fprintf(stderr, "rows: %d, cols: %d\n", height, width);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case SIGHUP:
|
|
|
|
exit(42); // arbitrary exit code to test against
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2015-02-23 08:34:20 -07:00
|
|
|
}
|
2018-01-17 04:41:16 -07:00
|
|
|
#endif
|
2017-08-13 06:24:25 -07:00
|
|
|
|
2022-09-17 18:17:15 -07:00
|
|
|
#ifdef MSWIN
|
2017-08-06 15:26:43 -07:00
|
|
|
static void sigwinch_cb(uv_signal_t *handle, int signum)
|
|
|
|
{
|
|
|
|
int width, height;
|
2017-08-13 06:24:25 -07:00
|
|
|
uv_tty_get_winsize(&tty_out, &width, &height);
|
2017-08-06 15:26:43 -07:00
|
|
|
fprintf(stderr, "rows: %d, cols: %d\n", height, width);
|
|
|
|
}
|
|
|
|
#endif
|
2015-02-23 08:34:20 -07:00
|
|
|
|
|
|
|
static void alloc_cb(uv_handle_t *handle, size_t suggested, uv_buf_t *buf)
|
|
|
|
{
|
|
|
|
buf->len = BUF_SIZE;
|
|
|
|
buf->base = malloc(BUF_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void read_cb(uv_stream_t *stream, ssize_t cnt, const uv_buf_t *buf)
|
|
|
|
{
|
|
|
|
if (cnt <= 0) {
|
|
|
|
uv_read_stop(stream);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-25 05:14:47 -07:00
|
|
|
int *interrupted = stream->data;
|
|
|
|
|
|
|
|
for (int i = 0; i < cnt; i++) {
|
2017-04-02 02:32:23 -07:00
|
|
|
if (buf->base[i] == CTRL_C) {
|
2015-03-25 05:14:47 -07:00
|
|
|
(*interrupted)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-23 08:34:20 -07:00
|
|
|
uv_loop_t write_loop;
|
|
|
|
uv_loop_init(&write_loop);
|
|
|
|
uv_tty_t out;
|
2017-03-28 02:07:58 -07:00
|
|
|
uv_tty_init(&write_loop, &out, fileno(stdout), 0);
|
2017-03-30 13:40:37 -07:00
|
|
|
|
2017-08-13 06:24:25 -07:00
|
|
|
uv_write_t req;
|
2018-01-17 04:40:31 -07:00
|
|
|
uv_buf_t b = {
|
|
|
|
.base = buf->base,
|
2022-09-17 18:17:15 -07:00
|
|
|
#ifdef MSWIN
|
2018-01-17 04:40:31 -07:00
|
|
|
.len = (ULONG)cnt
|
|
|
|
#else
|
|
|
|
.len = (size_t)cnt
|
|
|
|
#endif
|
|
|
|
};
|
2023-11-12 05:13:58 -07:00
|
|
|
uv_write(&req, (uv_stream_t *)&out, &b, 1, NULL);
|
2017-08-13 06:24:25 -07:00
|
|
|
uv_run(&write_loop, UV_RUN_DEFAULT);
|
2017-03-30 13:40:37 -07:00
|
|
|
|
2023-11-12 05:13:58 -07:00
|
|
|
uv_close((uv_handle_t *)&out, NULL);
|
2015-02-23 08:34:20 -07:00
|
|
|
uv_run(&write_loop, UV_RUN_DEFAULT);
|
|
|
|
if (uv_loop_close(&write_loop)) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
free(buf->base);
|
2015-03-25 05:14:47 -07:00
|
|
|
|
|
|
|
if (*interrupted >= 2) {
|
|
|
|
uv_walk(uv_default_loop(), walk_cb, NULL);
|
|
|
|
} else if (*interrupted == 1) {
|
|
|
|
fprintf(stderr, "interrupt received, press again to exit\n");
|
|
|
|
}
|
2015-02-23 08:34:20 -07:00
|
|
|
}
|
|
|
|
|
2015-03-24 03:45:36 -07:00
|
|
|
static void prepare_cb(uv_prepare_t *handle)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "tty ready\n");
|
|
|
|
uv_prepare_stop(handle);
|
|
|
|
}
|
|
|
|
|
2015-02-23 08:34:20 -07:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2015-03-25 05:14:47 -07:00
|
|
|
if (!owns_tty()) {
|
|
|
|
fprintf(stderr, "process does not own the terminal\n");
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
2015-02-23 08:34:20 -07:00
|
|
|
if (!is_terminal(stdin)) {
|
|
|
|
fprintf(stderr, "stdin is not a terminal\n");
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_terminal(stdout)) {
|
|
|
|
fprintf(stderr, "stdout is not a terminal\n");
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_terminal(stderr)) {
|
|
|
|
fprintf(stderr, "stderr is not a terminal\n");
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
2015-03-25 05:14:47 -07:00
|
|
|
if (argc > 1) {
|
2018-07-01 05:48:30 -07:00
|
|
|
errno = 0;
|
|
|
|
int count = (int)strtol(argv[1], NULL, 10);
|
|
|
|
if (errno != 0) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
count = (count < 0 || count > 99999) ? 0 : count;
|
2017-04-02 02:32:23 -07:00
|
|
|
for (int i = 0; i < count; i++) {
|
2015-03-25 05:14:47 -07:00
|
|
|
printf("line%d\n", i);
|
|
|
|
}
|
|
|
|
fflush(stdout);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int interrupted = 0;
|
2015-03-24 03:45:36 -07:00
|
|
|
uv_prepare_t prepare;
|
|
|
|
uv_prepare_init(uv_default_loop(), &prepare);
|
|
|
|
uv_prepare_start(&prepare, prepare_cb);
|
2022-09-17 18:17:15 -07:00
|
|
|
#ifndef MSWIN
|
2015-02-23 08:34:20 -07:00
|
|
|
uv_tty_init(uv_default_loop(), &tty, fileno(stderr), 1);
|
2017-03-28 02:07:58 -07:00
|
|
|
#else
|
|
|
|
uv_tty_init(uv_default_loop(), &tty, fileno(stdin), 1);
|
2017-08-13 06:24:25 -07:00
|
|
|
uv_tty_init(uv_default_loop(), &tty_out, fileno(stdout), 0);
|
|
|
|
int width, height;
|
|
|
|
uv_tty_get_winsize(&tty_out, &width, &height);
|
2017-03-28 02:07:58 -07:00
|
|
|
#endif
|
2015-03-25 05:14:47 -07:00
|
|
|
uv_tty_set_mode(&tty, UV_TTY_MODE_RAW);
|
|
|
|
tty.data = &interrupted;
|
2023-11-12 05:13:58 -07:00
|
|
|
uv_read_start((uv_stream_t *)&tty, alloc_cb, read_cb);
|
2022-09-17 18:17:15 -07:00
|
|
|
#ifndef MSWIN
|
2015-03-25 05:14:47 -07:00
|
|
|
struct sigaction sa;
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sa.sa_flags = 0;
|
2016-12-07 06:01:51 -07:00
|
|
|
sa.sa_handler = sig_handler;
|
|
|
|
sigaction(SIGHUP, &sa, NULL);
|
2015-03-25 05:14:47 -07:00
|
|
|
sigaction(SIGWINCH, &sa, NULL);
|
2017-03-28 02:07:58 -07:00
|
|
|
#else
|
2017-08-06 15:26:43 -07:00
|
|
|
uv_signal_t sigwinch_watcher;
|
|
|
|
uv_signal_init(uv_default_loop(), &sigwinch_watcher);
|
|
|
|
uv_signal_start(&sigwinch_watcher, sigwinch_cb, SIGWINCH);
|
2015-12-20 16:11:23 -07:00
|
|
|
#endif
|
2015-02-23 08:34:20 -07:00
|
|
|
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
|
2015-03-25 05:14:47 -07:00
|
|
|
|
2022-09-17 18:17:15 -07:00
|
|
|
#ifndef MSWIN
|
2016-12-07 06:01:51 -07:00
|
|
|
// XXX: Without this the SIGHUP handler is skipped on some systems.
|
|
|
|
sleep(100);
|
2017-03-28 02:07:58 -07:00
|
|
|
#endif
|
2016-12-07 06:01:51 -07:00
|
|
|
|
2015-03-25 05:14:47 -07:00
|
|
|
return 0;
|
2015-02-23 08:34:20 -07:00
|
|
|
}
|