| 1 |
;;; |
| 2 |
;;; termios - termios interface |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2000-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
(define-module gauche.termios |
| 35 |
(use gauche.uvector) |
| 36 |
(export has-windows-console? without-echoing with-terminal-mode) |
| 37 |
) |
| 38 |
(select-module gauche.termios) |
| 39 |
|
| 40 |
(without-precompiling |
| 41 |
(cond-expand |
| 42 |
[gauche.os.windows |
| 43 |
(export msys-get-stty)] |
| 44 |
[else |
| 45 |
(export-if-defined |
| 46 |
<sys-termios-meta> <sys-termios> B0 B110 B115200 B1200 B134 B150 |
| 47 |
B1800 B19200 B200 B230400 B2400 B300 B38400 B4800 B50 B57600 |
| 48 |
B600 B75 B9600 BRKINT BS0 BS1 BSDLY CIBAUD CLOCAL CR0 CR1 CR2 |
| 49 |
CR3 CRDLY CREAD CRTSCTS CS5 CS6 CS7 CS8 CSIZE CSTOPB ECHO |
| 50 |
ECHOCTL ECHOE ECHOK ECHOKE ECHONL ECHOPRT FF0 FF1 FFDLY HUPCL |
| 51 |
ICANON ICRNL IEXTEN IGNBRK IGNCR IGNPAR IMAXBEL INLCR INPCK |
| 52 |
ISIG ISTRIP IUCLC IXANY IXOFF IXON NCCS NL0 NL1 NLDLY NOFLSH |
| 53 |
OCRNL OFDEL OFILL OLCUC ONLCR ONLRET ONOCR OPOST PARENB PARODD |
| 54 |
PENDIN TCIFLUSH TCIOFF TCIOFLUSH TCION TCOFLUSH TCOOFF TCOON |
| 55 |
TCSADRAIN TCSAFLUSH TCSANOW TOSTOP VDISCARD VEOF VEOL VEOL2 |
| 56 |
VERASE VINTR VKILL VLNEXT VMIN VQUIT VREPRINT VSTART VSTOP |
| 57 |
VSUSP VSWTC VT0 VT1 VTDLY VTIME VWERASE XCASE _POSIX_VDISABLE |
| 58 |
sys-cfgetispeed sys-cfgetospeed |
| 59 |
sys-cfsetispeed sys-cfsetospeed sys-forkpty |
| 60 |
sys-forkpty-and-exec sys-openpty sys-tcdrain sys-tcflow |
| 61 |
sys-tcflush sys-tcgetattr sys-tcgetpgrp sys-tcsendbreak |
| 62 |
sys-tcsetattr sys-tcsetpgrp sys-termios-copy)]) |
| 63 |
) |
| 64 |
|
| 65 |
;;--------------------------------------------------------------------- |
| 66 |
;; termios.h |
| 67 |
|
| 68 |
(inline-stub |
| 69 |
(.unless (defined GAUCHE_WINDOWS) |
| 70 |
|
| 71 |
(declcode |
| 72 |
(.include <gauche/priv/portP.h>) |
| 73 |
(.include <termios.h>) |
| 74 |
(.when (defined HAVE_PTY_H) |
| 75 |
(.include <pty.h>)) |
| 76 |
(.when (defined HAVE_UTIL_H) |
| 77 |
(.include <util.h>)) |
| 78 |
;; libutil.h is superseded by bsd/libutil.h |
| 79 |
(.if (defined HAVE_BSD_LIBUTIL_H) |
| 80 |
(.include <bsd/libutil.h>) |
| 81 |
(.if (defined HAVE_LIBUTIL_H) |
| 82 |
(.include <libutil.h>))) |
| 83 |
(.when (defined HAVE_UNISTD_H) |
| 84 |
(.include <unistd.h>))) |
| 85 |
|
| 86 |
;; Constants for termios. Non-POSIX symbols are defined conditionally. |
| 87 |
|
| 88 |
(define-enum TCSANOW) |
| 89 |
(define-enum TCSADRAIN) |
| 90 |
(define-enum TCSAFLUSH) |
| 91 |
(define-enum TCIFLUSH) |
| 92 |
(define-enum TCOFLUSH) |
| 93 |
(define-enum TCIOFLUSH) |
| 94 |
(define-enum TCOOFF) |
| 95 |
(define-enum TCOON) |
| 96 |
(define-enum TCIOFF) |
| 97 |
(define-enum TCION) |
| 98 |
|
| 99 |
;; c_iflag masks |
| 100 |
(define-enum IGNBRK) |
| 101 |
(define-enum BRKINT) |
| 102 |
(define-enum IGNPAR) |
| 103 |
(define-enum INPCK) |
| 104 |
(define-enum ISTRIP) |
| 105 |
(define-enum INLCR) |
| 106 |
(define-enum IGNCR) |
| 107 |
(define-enum ICRNL) |
| 108 |
(define-enum IXON) |
| 109 |
(define-enum IXOFF) |
| 110 |
(define-enum-conditionally IXANY) |
| 111 |
(define-enum-conditionally IUCLC) |
| 112 |
(define-enum-conditionally IMAXBEL) |
| 113 |
|
| 114 |
;; c_oflag masks |
| 115 |
(define-enum OPOST) |
| 116 |
(define-enum-conditionally OLCUC) |
| 117 |
(define-enum-conditionally ONLCR) |
| 118 |
(define-enum-conditionally OCRNL) |
| 119 |
(define-enum-conditionally ONOCR) |
| 120 |
(define-enum-conditionally ONLRET) |
| 121 |
(define-enum-conditionally OFILL) |
| 122 |
(define-enum-conditionally OFDEL) |
| 123 |
(define-enum-conditionally NLDLY) |
| 124 |
(define-enum-conditionally NL0) |
| 125 |
(define-enum-conditionally NL1) |
| 126 |
(define-enum-conditionally CRDLY) |
| 127 |
(define-enum-conditionally CR0) |
| 128 |
(define-enum-conditionally CR1) |
| 129 |
(define-enum-conditionally CR2) |
| 130 |
(define-enum-conditionally CR3) |
| 131 |
(define-enum-conditionally BSDLY) |
| 132 |
(define-enum-conditionally BS0) |
| 133 |
(define-enum-conditionally BS1) |
| 134 |
(define-enum-conditionally VTDLY) |
| 135 |
(define-enum-conditionally VT0) |
| 136 |
(define-enum-conditionally VT1) |
| 137 |
(define-enum-conditionally FFDLY) |
| 138 |
(define-enum-conditionally FF0) |
| 139 |
(define-enum-conditionally FF1) |
| 140 |
|
| 141 |
;; c_cflag masks |
| 142 |
(define-enum CLOCAL) |
| 143 |
(define-enum CREAD) |
| 144 |
(define-enum CSIZE) |
| 145 |
(define-enum CS5) |
| 146 |
(define-enum CS6) |
| 147 |
(define-enum CS7) |
| 148 |
(define-enum CS8) |
| 149 |
(define-enum CSTOPB) |
| 150 |
(define-enum HUPCL) |
| 151 |
(define-enum PARENB) |
| 152 |
(define-enum PARODD) |
| 153 |
(define-enum-conditionally CIBAUD) |
| 154 |
(define-enum-conditionally CRTSCTS) |
| 155 |
|
| 156 |
;; c_lflag masks |
| 157 |
(define-enum ECHO) |
| 158 |
(define-enum ECHOE) |
| 159 |
(define-enum ECHOK) |
| 160 |
(define-enum ECHONL) |
| 161 |
(define-enum ICANON) |
| 162 |
(define-enum ISIG) |
| 163 |
(define-enum NOFLSH) |
| 164 |
(define-enum TOSTOP) |
| 165 |
(define-enum IEXTEN) |
| 166 |
(define-enum-conditionally XCASE) |
| 167 |
(define-enum-conditionally ECHOCTL) |
| 168 |
(define-enum-conditionally ECHOPRT) |
| 169 |
(define-enum-conditionally ECHOKE) |
| 170 |
(define-enum-conditionally FLUSH0) |
| 171 |
(define-enum-conditionally PENDIN) |
| 172 |
|
| 173 |
;; c_cc size |
| 174 |
(define-enum NCCS) |
| 175 |
|
| 176 |
;; disable character |
| 177 |
(define-enum _POSIX_VDISABLE) |
| 178 |
|
| 179 |
;; c_cc subscripts |
| 180 |
(define-enum VEOF) |
| 181 |
(define-enum VEOL) |
| 182 |
(define-enum VERASE) |
| 183 |
(define-enum VINTR) |
| 184 |
(define-enum VKILL) |
| 185 |
(define-enum VMIN) |
| 186 |
(define-enum VQUIT) |
| 187 |
(define-enum VSTART) |
| 188 |
(define-enum VSTOP) |
| 189 |
(define-enum VSUSP) |
| 190 |
(define-enum VTIME) |
| 191 |
(define-enum-conditionally VDISCARD) |
| 192 |
(define-enum-conditionally VDSUSP) |
| 193 |
(define-enum-conditionally VEOL2) |
| 194 |
(define-enum-conditionally VLNEXT) |
| 195 |
(define-enum-conditionally VREPRINT) |
| 196 |
(define-enum-conditionally VSTATUS) |
| 197 |
(define-enum-conditionally VWERASE) |
| 198 |
(define-enum-conditionally VSWTCH) |
| 199 |
(define-enum-conditionally VSWTC) |
| 200 |
|
| 201 |
;; baudrates |
| 202 |
(define-enum B0) |
| 203 |
(define-enum B50) |
| 204 |
(define-enum B75) |
| 205 |
(define-enum B110) |
| 206 |
(define-enum B134) |
| 207 |
(define-enum B150) |
| 208 |
(define-enum B200) |
| 209 |
(define-enum B300) |
| 210 |
(define-enum B600) |
| 211 |
(define-enum B1200) |
| 212 |
(define-enum B1800) |
| 213 |
(define-enum B2400) |
| 214 |
(define-enum B4800) |
| 215 |
(define-enum B9600) |
| 216 |
(define-enum B19200) |
| 217 |
(define-enum B38400) |
| 218 |
(define-enum-conditionally B57600) |
| 219 |
(define-enum-conditionally B115200) |
| 220 |
(define-enum-conditionally B230400) |
| 221 |
|
| 222 |
(define-cstruct <sys-termios> "struct termios" |
| 223 |
(iflag::<ulong> "c_iflag" |
| 224 |
oflag::<ulong> "c_oflag" |
| 225 |
cflag::<ulong> "c_cflag" |
| 226 |
lflag::<ulong> "c_lflag" |
| 227 |
cc::(.array <uint8> :as <u8vector>) "c_cc[NCCS]")) |
| 228 |
|
| 229 |
(define-cproc sys-tcgetattr (port-or-fd) ::<sys-termios> |
| 230 |
(let* ([fd::int (Scm_GetPortFd port-or-fd TRUE)] |
| 231 |
[term::(struct termios)]) |
| 232 |
(when (< (tcgetattr fd (& term)) 0) |
| 233 |
(Scm_SysError "tcgetattr failed")) |
| 234 |
(return (& term)))) |
| 235 |
|
| 236 |
(define-cproc sys-tcsetattr (port-or-fd option::<fixnum> term::<sys-termios>) |
| 237 |
::<void> |
| 238 |
(let* ([fd::int (Scm_GetPortFd port-or-fd TRUE)]) |
| 239 |
(when (< (tcsetattr fd option term) 0) |
| 240 |
(Scm_SysError "tcsetattr failed")))) |
| 241 |
|
| 242 |
(define-cproc sys-tcsendbreak (port-or-fd duration::<fixnum>) ::<boolean> |
| 243 |
(let* ([fd::int (Scm_GetPortFd port-or-fd TRUE)]) |
| 244 |
(return (>= (tcsendbreak fd duration) 0)))) |
| 245 |
|
| 246 |
(define-cproc sys-tcdrain (port-or-fd) ::<void> |
| 247 |
(let* ([fd::int (Scm_GetPortFd port-or-fd TRUE)]) |
| 248 |
(when (< (tcdrain fd) 0) (Scm_SysError "tcdrain failed")))) |
| 249 |
|
| 250 |
(define-cproc sys-tcflush (port-or-fd queue::<int>) ::<void> |
| 251 |
(let* ([fd::int (Scm_GetPortFd port-or-fd TRUE)]) |
| 252 |
(when (< (tcflush fd queue) 0) (Scm_SysError "tcflush failed")))) |
| 253 |
|
| 254 |
(define-cproc sys-tcflow (port-or-fd action::<int>) ::<void> |
| 255 |
(let* ([fd::int (Scm_GetPortFd port-or-fd TRUE)]) |
| 256 |
(when (< (tcflow fd action) 0) (Scm_SysError "tcflow failed")))) |
| 257 |
|
| 258 |
(define-cproc sys-tcgetpgrp (port-or-fd) ::<int> |
| 259 |
(let* ([fd::int (Scm_GetPortFd port-or-fd TRUE)] |
| 260 |
[r::pid_t (tcgetpgrp fd)]) |
| 261 |
(when (< r 0) (Scm_SysError "tcgetpgrp failed")) |
| 262 |
(return r))) |
| 263 |
|
| 264 |
(define-cproc sys-tcsetpgrp (port-or-fd pgrp::<int>) ::<void> |
| 265 |
(let* ([fd::int (Scm_GetPortFd port-or-fd TRUE)]) |
| 266 |
(when (< (tcsetpgrp fd pgrp) 0) (Scm_SysError "tcsetpgrp failed")))) |
| 267 |
|
| 268 |
(define-cproc sys-cfgetispeed (term::<sys-termios>) ::<int> |
| 269 |
(let* ([s::speed_t (cfgetispeed term)]) |
| 270 |
(return s))) |
| 271 |
|
| 272 |
(define-cproc sys-cfsetispeed (term::<sys-termios> speed::<int>) ::<void> |
| 273 |
(when (< (cfsetispeed term speed) 0) |
| 274 |
(Scm_SysError "cfsetispeed failed"))) |
| 275 |
|
| 276 |
(define-cproc sys-cfgetospeed (term::<sys-termios>) ::<int> |
| 277 |
(let* ([s::speed_t (cfgetospeed term)]) |
| 278 |
(return s))) |
| 279 |
|
| 280 |
(define-cproc sys-cfsetospeed (term::<sys-termios> speed::<int>) ::<void> |
| 281 |
(when (< (cfsetospeed term speed) 0) |
| 282 |
(Scm_SysError "cfsetospeed failed"))) |
| 283 |
|
| 284 |
;; Returns fresh copy of <sys-termios> |
| 285 |
;; The fields of struct termios are system-dependent, and it may contain |
| 286 |
;; more fields than specified in POSIX. However, we assume they're safe |
| 287 |
;; for bitwise-copy. The cstruct boxer copies *src into the fresh object. |
| 288 |
(define-cproc sys-termios-copy (src::<sys-termios>) ::<sys-termios> |
| 289 |
(return src)) |
| 290 |
|
| 291 |
;; pty interface |
| 292 |
(.when (defined HAVE_OPENPTY) |
| 293 |
(define-cproc sys-openpty (:optional (term::<sys-termios>? #f)) |
| 294 |
::(<int> <int>) |
| 295 |
(let* ([master::int 0] [slave::int 0]) |
| 296 |
(when (< (openpty (& master) (& slave) NULL term NULL) 0) |
| 297 |
(Scm_SysError "openpty failed")) |
| 298 |
(return master slave))) |
| 299 |
) |
| 300 |
(.when (defined HAVE_FORKPTY) |
| 301 |
(define-cproc sys-forkpty (:optional (term::<sys-termios>? #f)) |
| 302 |
::(<int> <int>) |
| 303 |
(let* ([master::int 0] |
| 304 |
[pid::pid_t (forkpty (& master) NULL term NULL)]) |
| 305 |
(when (< pid 0) |
| 306 |
(Scm_SysError "forkpty failed")) |
| 307 |
(return pid master))) |
| 308 |
(define-cproc sys-forkpty-and-exec (program::<string> args::<list> |
| 309 |
:key (iomap ()) |
| 310 |
(term::<sys-termios>? #f) |
| 311 |
(sigmask::<sys-sigset>? #f)) |
| 312 |
::(<int> <int>) |
| 313 |
(let* ([argc::ScmSmallInt (Scm_Length args)]) |
| 314 |
(when (< argc 1) |
| 315 |
(Scm_Error "argument list must have at least one element: %S" args)) |
| 316 |
(let* ([argv::char** (Scm_ListToCStringArray args TRUE NULL)] |
| 317 |
[cprogram::(const char *) (Scm_GetStringConst program)] |
| 318 |
[fds::int* (Scm_SysPrepareFdMap iomap)] |
| 319 |
[master::int 0] |
| 320 |
[pid::pid_t (forkpty (& master) NULL term NULL)]) |
| 321 |
(when (< pid 0) |
| 322 |
(Scm_SysError "forkpty failed")) |
| 323 |
(when (== pid 0) |
| 324 |
(Scm_SysSwapFds fds) |
| 325 |
(when sigmask |
| 326 |
(Scm_ResetSignalHandlers (& (-> sigmask set))) |
| 327 |
(Scm_SysSigmask SIG_SETMASK sigmask)) |
| 328 |
(execvp cprogram (cast (char * const *) argv)) |
| 329 |
;; here, we failed |
| 330 |
(Scm_Panic "exec failed: %s: %s" cprogram (strerror errno))) |
| 331 |
(return pid master)))) |
| 332 |
) |
| 333 |
|
| 334 |
) ;; !defined(GAUCHE_WINDOWS) |
| 335 |
) ;; inline-stub |
| 336 |
|
| 337 |
;; |
| 338 |
;; High-level utilities |
| 339 |
;; |
| 340 |
|
| 341 |
(without-precompiling |
| 342 |
|
| 343 |
(cond-expand |
| 344 |
[gauche.os.windows (use os.windows)] |
| 345 |
[else]) |
| 346 |
|
| 347 |
;; Heuristics - check if we have a windows console. |
| 348 |
(define (has-windows-console?) |
| 349 |
(cond-expand |
| 350 |
[gauche.os.windows |
| 351 |
(sys-has-windows-console?)] |
| 352 |
[else #f])) |
| 353 |
|
| 354 |
(cond-expand |
| 355 |
[gauche.os.windows |
| 356 |
;; For mintty on MSYS - we don't want to depend on cygwin's dll, |
| 357 |
;; but if we're running on MSYS, we're likely to have stty. |
| 358 |
;; NB: It's better if we could use gauche.process and directly read |
| 359 |
;; from pipe, but at the time it isn't working yet. |
| 360 |
(define (msys-get-stty) |
| 361 |
;; NB: We don't use build-path to avoid depending file.util |
| 362 |
(receive (out tempfile) (sys-mkstemp #"~(sys-tmpdir)/gauche-stty") |
| 363 |
(unwind-protect |
| 364 |
(begin |
| 365 |
(close-output-port out) |
| 366 |
(sys-system #"stty -g > ~tempfile") |
| 367 |
(with-input-from-file tempfile read-line)) |
| 368 |
(sys-unlink tempfile))))] |
| 369 |
[else]) |
| 370 |
|
| 371 |
(define (without-echoing iport proc) |
| 372 |
(cond-expand |
| 373 |
[gauche.os.windows |
| 374 |
(cond [(not iport) |
| 375 |
(if (has-windows-console?) |
| 376 |
(call-with-input-file "CON" |
| 377 |
(cut without-echoing <> proc)) |
| 378 |
(without-echoing (standard-input-port) proc))] ;CON not avail. |
| 379 |
[(sys-isatty iport) ;NB: mintty yields #f here |
| 380 |
(let () |
| 381 |
(define ihandle (sys-get-std-handle STD_INPUT_HANDLE)) |
| 382 |
(define orig-mode (sys-get-console-mode ihandle)) |
| 383 |
(define (echo-off) |
| 384 |
(sys-set-console-mode ihandle |
| 385 |
(logand orig-mode |
| 386 |
(lognot ENABLE_ECHO_INPUT)))) |
| 387 |
(define (echo-on) |
| 388 |
(sys-set-console-mode ihandle orig-mode)) |
| 389 |
(unwind-protect (begin (echo-off) (proc iport)) (echo-on)))] |
| 390 |
[((with-module gauche.internal %sys-mintty?) iport) |
| 391 |
;; We're dealing with mintty |
| 392 |
(let () |
| 393 |
(define saved-attr (msys-get-stty)) |
| 394 |
(define (echo-off) (sys-system "stty -echo icanon iexten isig")) |
| 395 |
(define (echo-on) (sys-system #"stty ~saved-attr")) |
| 396 |
(unwind-protect (begin (echo-off) (proc iport)) (echo-on)))] |
| 397 |
[else (proc iport)])] |
| 398 |
[else ; not gauche.os.windows |
| 399 |
(cond [(not iport) ;; open tty |
| 400 |
(call-with-input-file "/dev/tty" |
| 401 |
(cut without-echoing <> proc))] |
| 402 |
[(sys-isatty iport) |
| 403 |
(let () |
| 404 |
(define attr (sys-tcgetattr iport)) |
| 405 |
(define lflag-save (ref attr'lflag)) |
| 406 |
(define (echo-off) |
| 407 |
(set! (ref attr'lflag) |
| 408 |
(logior ICANON IEXTEN ISIG |
| 409 |
(logand (ref attr'lflag) |
| 410 |
(lognot ECHO)))) |
| 411 |
(sys-tcsetattr iport TCSANOW attr)) |
| 412 |
(define (echo-on) |
| 413 |
(set! (ref attr'lflag) lflag-save) |
| 414 |
(sys-tcsetattr iport TCSANOW attr)) |
| 415 |
(unwind-protect (begin (echo-off) (proc iport)) (echo-on)))] |
| 416 |
[else (proc iport)])])) |
| 417 |
|
| 418 |
#| |
| 419 |
;; sample |
| 420 |
(define (get-password) |
| 421 |
(with-output-to-file |
| 422 |
(cond-expand [gauche.os.windows "CON"] [else "/dev/tty"]) |
| 423 |
(lambda () (display "Password: ") (flush))) |
| 424 |
(without-echoing #f read-line)) |
| 425 |
|# |
| 426 |
|
| 427 |
;; mode should be either one of 'cooked, 'rare or 'raw |
| 428 |
;; NB: Although we work on the given port and also calls PROC with port, |
| 429 |
;; what's changed is actually a device connected to the port. There can |
| 430 |
;; be more than one port connected to the same device, and I/O thru those |
| 431 |
;; ports would also be affected. |
| 432 |
;; NB: Windows console doesn't go well with this API, so we do nothing; |
| 433 |
;; see text.console for abstraction of Windows console. |
| 434 |
(define (with-terminal-mode port mode proc :optional (cleanup #f)) |
| 435 |
(cond-expand |
| 436 |
[gauche.os.windows |
| 437 |
(if (not ((with-module gauche.internal %sys-mintty?) port)) |
| 438 |
(proc port) ; for windows console |
| 439 |
(let () ; for mintty on MSYS |
| 440 |
(define saved-attr (msys-get-stty)) |
| 441 |
(define saved-buffering (port-buffering port)) |
| 442 |
(define new-attr |
| 443 |
(case mode |
| 444 |
[(raw) "-echo -icanon -iexten -isig -icrnl"] |
| 445 |
[(rare) "-echo -icanon -iexten isig -icrnl"] |
| 446 |
[(cooked) " echo icanon iexten isig icrnl"] |
| 447 |
[else |
| 448 |
(error "terminal mode needs to be one of cooked, rare or raw, \ |
| 449 |
but got:" mode)])) |
| 450 |
(define (set) |
| 451 |
(sys-system #"stty ~new-attr") |
| 452 |
(when (memq mode '(raw rare)) |
| 453 |
(set! (port-buffering port) :none))) |
| 454 |
(define (reset) |
| 455 |
(sys-system #"stty ~saved-attr") |
| 456 |
(set! (port-buffering port) saved-buffering) |
| 457 |
(when cleanup (cleanup))) |
| 458 |
(unwind-protect (begin (set) (proc port)) (reset))))] |
| 459 |
[else ; not gauche.os.windows |
| 460 |
(if (not (sys-isatty port)) |
| 461 |
(proc port) |
| 462 |
(let () |
| 463 |
(define saved-attr (sys-tcgetattr port)) |
| 464 |
(define saved-buffering (port-buffering port)) |
| 465 |
(define new-attr |
| 466 |
(rlet1 attr (sys-termios-copy saved-attr) |
| 467 |
;; NB: See cfmakeraw(3) for raw mode setting. |
| 468 |
(case mode |
| 469 |
[(raw) |
| 470 |
(update! (~ attr'iflag) |
| 471 |
(cut logset+clear <> |
| 472 |
0 |
| 473 |
(logior BRKINT ICRNL INPCK ISTRIP IXON))) |
| 474 |
(update! (~ attr'oflag) |
| 475 |
(cut logset+clear <> |
| 476 |
0 |
| 477 |
OPOST)) |
| 478 |
(update! (~ attr'cflag) |
| 479 |
(cut logset+clear <> |
| 480 |
CS8 |
| 481 |
(logior CSIZE PARENB))) |
| 482 |
(update! (~ attr'lflag) |
| 483 |
(cut logset+clear <> |
| 484 |
0 |
| 485 |
(logior ECHO ICANON IEXTEN ISIG)))] |
| 486 |
[(rare) |
| 487 |
(update! (~ attr'iflag) |
| 488 |
(cut logset+clear <> |
| 489 |
BRKINT |
| 490 |
(logior ICRNL INPCK ISTRIP IXON))) |
| 491 |
(update! (~ attr'oflag) |
| 492 |
(cut logset+clear <> |
| 493 |
OPOST |
| 494 |
0)) |
| 495 |
(update! (~ attr'cflag) |
| 496 |
(cut logset+clear <> |
| 497 |
CS8 |
| 498 |
(logior CSIZE PARENB))) |
| 499 |
(update! (~ attr'lflag) |
| 500 |
(cut logset+clear <> |
| 501 |
ISIG |
| 502 |
(logior ECHO ICANON IEXTEN)))] |
| 503 |
[(cooked) |
| 504 |
(update! (~ attr'iflag) |
| 505 |
(cut logset+clear <> |
| 506 |
(logior BRKINT ICRNL INPCK ISTRIP IXON) |
| 507 |
0)) |
| 508 |
(update! (~ attr'oflag) |
| 509 |
(cut logset+clear <> |
| 510 |
OPOST |
| 511 |
0)) |
| 512 |
(update! (~ attr'lflag) |
| 513 |
(cut logset+clear <> |
| 514 |
(logior ECHO ICANON IEXTEN ISIG) |
| 515 |
0))] |
| 516 |
[else |
| 517 |
(error "terminal mode needs to be one of cooked, rare or raw, \ |
| 518 |
but got:" mode)]))) |
| 519 |
(define (set) |
| 520 |
(sys-tcsetattr port TCSANOW new-attr) |
| 521 |
(when (memq mode '(raw rare)) |
| 522 |
(set! (port-buffering port) :none))) |
| 523 |
(define (reset) |
| 524 |
(sys-tcsetattr port TCSANOW saved-attr) |
| 525 |
(set! (port-buffering port) saved-buffering) |
| 526 |
(when cleanup (cleanup))) |
| 527 |
(unwind-protect (begin (set) (proc port)) (reset))))])) |
| 528 |
|
| 529 |
) ; without-precompiling |