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