1 // This is an open source non-commercial project. Dear PVS-Studio, please check
2 // it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
3 
4 #include <stdbool.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <uv.h>
8 #ifdef _WIN32
9 # include <windows.h>
10 #else
11 # include <unistd.h>
12 #endif
13 
14 // -V:STRUCT_CAST:641
15 #define STRUCT_CAST(Type, obj) ((Type *)(obj))
16 #define is_terminal(stream) (uv_guess_handle(fileno(stream)) == UV_TTY)
17 #define BUF_SIZE 0xfff
18 #define CTRL_C 0x03
19 
20 uv_tty_t tty;
21 uv_tty_t tty_out;
22 
23 bool owns_tty(void);  // silence -Wmissing-prototypes
owns_tty(void)24 bool owns_tty(void)
25 {
26 #ifdef _WIN32
27   // XXX: We need to make proper detect owns tty
28   // HWND consoleWnd = GetConsoleWindow();
29   // DWORD dwProcessId;
30   // GetWindowThreadProcessId(consoleWnd, &dwProcessId);
31   // return GetCurrentProcessId() == dwProcessId;
32   return true;
33 #else
34   return getsid(0) == getpid();
35 #endif
36 }
37 
walk_cb(uv_handle_t * handle,void * arg)38 static void walk_cb(uv_handle_t *handle, void *arg)
39 {
40   if (!uv_is_closing(handle)) {
41 #ifdef WIN32
42     uv_tty_set_mode(&tty, UV_TTY_MODE_NORMAL);
43 #endif
44     uv_close(handle, NULL);
45   }
46 }
47 
48 #ifndef WIN32
sig_handler(int signum)49 static void sig_handler(int signum)
50 {
51   switch (signum) {
52   case SIGWINCH: {
53     int width, height;
54     uv_tty_get_winsize(&tty, &width, &height);
55     fprintf(stderr, "rows: %d, cols: %d\n", height, width);
56     return;
57   }
58   case SIGHUP:
59     exit(42);  // arbitrary exit code to test against
60     return;
61   default:
62     return;
63   }
64 }
65 #endif
66 
67 #ifdef WIN32
sigwinch_cb(uv_signal_t * handle,int signum)68 static void sigwinch_cb(uv_signal_t *handle, int signum)
69 {
70   int width, height;
71   uv_tty_get_winsize(&tty_out, &width, &height);
72   fprintf(stderr, "rows: %d, cols: %d\n", height, width);
73 }
74 #endif
75 
alloc_cb(uv_handle_t * handle,size_t suggested,uv_buf_t * buf)76 static void alloc_cb(uv_handle_t *handle, size_t suggested, uv_buf_t *buf)
77 {
78   buf->len = BUF_SIZE;
79   buf->base = malloc(BUF_SIZE);
80 }
81 
read_cb(uv_stream_t * stream,ssize_t cnt,const uv_buf_t * buf)82 static void read_cb(uv_stream_t *stream, ssize_t cnt, const uv_buf_t *buf)
83 {
84   if (cnt <= 0) {
85     uv_read_stop(stream);
86     return;
87   }
88 
89   int *interrupted = stream->data;
90 
91   for (int i = 0; i < cnt; i++) {
92     if (buf->base[i] == CTRL_C) {
93       (*interrupted)++;
94     }
95   }
96 
97   uv_loop_t write_loop;
98   uv_loop_init(&write_loop);
99   uv_tty_t out;
100   uv_tty_init(&write_loop, &out, fileno(stdout), 0);
101 
102   uv_write_t req;
103   uv_buf_t b = {
104     .base = buf->base,
105 #ifdef WIN32
106     .len = (ULONG)cnt
107 #else
108     .len = (size_t)cnt
109 #endif
110   };
111   uv_write(&req, STRUCT_CAST(uv_stream_t, &out), &b, 1, NULL);
112   uv_run(&write_loop, UV_RUN_DEFAULT);
113 
114   uv_close(STRUCT_CAST(uv_handle_t, &out), NULL);
115   uv_run(&write_loop, UV_RUN_DEFAULT);
116   if (uv_loop_close(&write_loop)) {
117     abort();
118   }
119   free(buf->base);
120 
121   if (*interrupted >= 2) {
122     uv_walk(uv_default_loop(), walk_cb, NULL);
123   } else if (*interrupted == 1) {
124     fprintf(stderr, "interrupt received, press again to exit\n");
125   }
126 }
127 
prepare_cb(uv_prepare_t * handle)128 static void prepare_cb(uv_prepare_t *handle)
129 {
130   fprintf(stderr, "tty ready\n");
131   uv_prepare_stop(handle);
132 }
133 
main(int argc,char ** argv)134 int main(int argc, char **argv)
135 {
136   if (!owns_tty()) {
137     fprintf(stderr, "process does not own the terminal\n");
138     exit(2);
139   }
140 
141   if (!is_terminal(stdin)) {
142     fprintf(stderr, "stdin is not a terminal\n");
143     exit(2);
144   }
145 
146   if (!is_terminal(stdout)) {
147     fprintf(stderr, "stdout is not a terminal\n");
148     exit(2);
149   }
150 
151   if (!is_terminal(stderr)) {
152     fprintf(stderr, "stderr is not a terminal\n");
153     exit(2);
154   }
155 
156   if (argc > 1) {
157     errno = 0;
158     int count = (int)strtol(argv[1], NULL, 10);
159     if (errno != 0) {
160       abort();
161     }
162     count = (count < 0 || count > 99999) ? 0 : count;
163     for (int i = 0; i < count; i++) {
164       printf("line%d\n", i);
165     }
166     fflush(stdout);
167     return 0;
168   }
169 
170   int interrupted = 0;
171   uv_prepare_t prepare;
172   uv_prepare_init(uv_default_loop(), &prepare);
173   uv_prepare_start(&prepare, prepare_cb);
174 #ifndef WIN32
175   uv_tty_init(uv_default_loop(), &tty, fileno(stderr), 1);
176 #else
177   uv_tty_init(uv_default_loop(), &tty, fileno(stdin), 1);
178   uv_tty_init(uv_default_loop(), &tty_out, fileno(stdout), 0);
179   int width, height;
180   uv_tty_get_winsize(&tty_out, &width, &height);
181 #endif
182   uv_tty_set_mode(&tty, UV_TTY_MODE_RAW);
183   tty.data = &interrupted;
184   uv_read_start(STRUCT_CAST(uv_stream_t, &tty), alloc_cb, read_cb);
185 #ifndef WIN32
186   struct sigaction sa;
187   sigemptyset(&sa.sa_mask);
188   sa.sa_flags = 0;
189   sa.sa_handler = sig_handler;
190   sigaction(SIGHUP, &sa, NULL);
191   sigaction(SIGWINCH, &sa, NULL);
192 #else
193   uv_signal_t sigwinch_watcher;
194   uv_signal_init(uv_default_loop(), &sigwinch_watcher);
195   uv_signal_start(&sigwinch_watcher, sigwinch_cb, SIGWINCH);
196 #endif
197   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
198 
199 #ifndef WIN32
200   // XXX: Without this the SIGHUP handler is skipped on some systems.
201   sleep(100);
202 #endif
203 
204   return 0;
205 }
206