22 #ifdef HAVE_SYS_IOCTL_H
23 #include <sys/ioctl.h>
26 #if defined HAVE_TERMIOS_H
28 typedef struct termios conmode;
31 setattr(
int fd, conmode *t)
33 while (tcsetattr(fd, TCSAFLUSH, t)) {
34 if (errno != EINTR)
return 0;
38 # define getattr(fd, t) (tcgetattr(fd, t) == 0)
39 #elif defined HAVE_TERMIO_H
41 typedef struct termio conmode;
42 # define setattr(fd, t) (ioctl(fd, TCSETAF, t) == 0)
43 # define getattr(fd, t) (ioctl(fd, TCGETA, t) == 0)
44 #elif defined HAVE_SGTTY_H
46 typedef struct sgttyb conmode;
48 # define setattr(fd, t) (stty(fd, t) == 0)
50 # define setattr(fd, t) (ioctl((fd), TIOCSETP, (t)) == 0)
53 # define getattr(fd, t) (gtty(fd, t) == 0)
55 # define getattr(fd, t) (ioctl((fd), TIOCGETP, (t)) == 0)
59 typedef DWORD conmode;
61 #ifdef HAVE_RB_W32_MAP_ERRNO
62 #define LAST_ERROR rb_w32_map_errno(GetLastError())
64 #define LAST_ERROR EBADF
66 #define SET_LAST_ERROR (errno = LAST_ERROR, 0)
69 setattr(
int fd, conmode *t)
72 if (!x)
errno = LAST_ERROR;
77 getattr(
int fd, conmode *t)
80 if (!x)
errno = LAST_ERROR;
84 #ifndef SET_LAST_ERROR
85 #define SET_LAST_ERROR (0)
89 #define InitVM(ext) {void InitVM_##ext(void);InitVM_##ext();}
126 #ifdef HAVE_CFMAKERAW
128 t->c_lflag &= ~(ECHOE|ECHOK);
129 #elif defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
130 t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
131 t->c_oflag &= ~OPOST;
132 t->c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN);
133 t->c_cflag &= ~(CSIZE|PARENB);
135 #elif defined HAVE_SGTTY_H
136 t->sg_flags &= ~ECHO;
141 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
144 if (r->
vmin >= 0) t->c_cc[VMIN] = r->
vmin;
153 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
154 t->c_iflag |= (BRKINT|ISTRIP|ICRNL|IXON);
156 t->c_lflag |= (ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN);
157 #elif defined HAVE_SGTTY_H
161 *t |= ENABLE_ECHO_INPUT|ENABLE_LINE_INPUT|ENABLE_PROCESSED_INPUT;
168 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
169 t->c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
170 #elif defined HAVE_SGTTY_H
171 t->sg_flags &= ~ECHO;
173 *t &= ~ENABLE_ECHO_INPUT;
180 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
181 t->c_lflag |= (ECHO | ECHOE | ECHOK | ECHONL);
182 #elif defined HAVE_SGTTY_H
185 *t |= ENABLE_ECHO_INPUT;
192 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
193 return (t->c_lflag & (ECHO | ECHONL)) != 0;
194 #elif defined HAVE_SGTTY_H
195 return (t->sg_flags & ECHO) != 0;
197 return (*t & ENABLE_ECHO_INPUT) != 0;
205 if (!getattr(fd, t))
return 0;
208 return setattr(fd, &r);
212 #define GetReadFD(fptr) fileno(GetReadFile(fptr))
214 #define GetReadFD(fptr) ((fptr)->fd)
218 #define GetWriteFD(fptr) fileno(GetWriteFile(fptr))
225 if (!wio)
return fptr->
fd;
229 #define GetWriteFD(fptr) get_write_fd(fptr)
256 if (fd[1] != -1 && fd[1] != fd[0]) {
269 if (fd[0] != -1 && fd[0] ==
GetReadFD(fptr)) {
270 if (!setattr(fd[0], t+0)) {
275 if (fd[1] != -1 && fd[1] != fd[0] && fd[1] ==
GetWriteFD(fptr)) {
276 if (!setattr(fd[1], t+1)) {
467 #if defined TIOCGWINSZ
468 typedef struct winsize rb_console_size_t;
469 #define getwinsize(fd, buf) (ioctl((fd), TIOCGWINSZ, (buf)) == 0)
470 #define setwinsize(fd, buf) (ioctl((fd), TIOCSWINSZ, (buf)) == 0)
471 #define winsize_row(buf) (buf)->ws_row
472 #define winsize_col(buf) (buf)->ws_col
474 typedef CONSOLE_SCREEN_BUFFER_INFO rb_console_size_t;
475 #define getwinsize(fd, buf) ( \
476 GetConsoleScreenBufferInfo((HANDLE)rb_w32_get_osfhandle(fd), (buf)) || \
478 #define winsize_row(buf) ((buf)->srWindow.Bottom - (buf)->srWindow.Top + 1)
479 #define winsize_col(buf) (buf)->dwSize.X
482 #if defined TIOCGWINSZ || defined _WIN32
483 #define USE_CONSOLE_GETSIZE 1
486 #ifdef USE_CONSOLE_GETSIZE
496 console_winsize(
VALUE io)
500 rb_console_size_t ws;
521 rb_console_size_t ws;
526 VALUE row, col, xpixel, ypixel;
527 #if defined TIOCSWINSZ
534 &row, &col, &xpixel, &ypixel);
535 #if defined TIOCSWINSZ
537 ws.ws_row = ws.ws_col = ws.ws_xpixel = ws.ws_ypixel = 0;
538 #define SET(m) ws.ws_##m = NIL_P(m) ? 0 : (unsigned short)NUM2UINT(m)
552 if ((ws.dwSize.X < newcol && (ws.dwSize.X = newcol, 1)) ||
553 (ws.dwSize.Y < newrow && (ws.dwSize.Y = newrow, 1))) {
554 if (!(SetConsoleScreenBufferSize(wh, ws.dwSize) ||
SET_LAST_ERROR)) {
558 ws.srWindow.Left = 0;
560 ws.srWindow.Right = newcol;
561 ws.srWindow.Bottom = newrow;
586 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
608 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
626 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
631 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
634 if (fd2 != -1 && fd1 != fd2) {
670 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H || defined HAVE_SGTTY_H
671 # define CONSOLE_DEVICE "/dev/tty"
673 # define CONSOLE_DEVICE "con$"
674 # define CONSOLE_DEVICE_FOR_READING "conin$"
675 # define CONSOLE_DEVICE_FOR_WRITING "conout$"
677 #ifndef CONSOLE_DEVICE_FOR_READING
678 # define CONSOLE_DEVICE_FOR_READING CONSOLE_DEVICE
680 #ifdef CONSOLE_DEVICE_FOR_WRITING
686 #ifdef CONSOLE_DEVICE_FOR_WRITING
687 fd = open(CONSOLE_DEVICE_FOR_WRITING, O_WRONLY);
688 if (fd < 0)
return Qnil;
696 #ifdef CONSOLE_DEVICE_FOR_WRITING
707 #ifdef CONSOLE_DEVICE_FOR_WRITING
709 # ifdef HAVE_RB_IO_GET_WRITE_IO