;; generated automatically.  DO NOT EDIT
#!no-fold-case
(define-module gauche.termios (use gauche.uvector) (export-all))
(select-module gauche.termios)
(dynamic-load "gauche--termios")
(cond-expand (gauche.os.windows (use os.windows)) (else))
(cond-expand (gauche.os.windows (define (has-windows-console?) (or (not (sys-getenv "MSYSCON")) (sys-isatty (standard-input-port)) (sys-isatty (standard-output-port)) (sys-isatty (standard-error-port)))) (define (msys-get-stty) (receive (out tempfile) (sys-mkstemp (string-append (x->string (sys-tmpdir)) "/gauche-stty")) (unwind-protect (begin (close-output-port out) (sys-system (string-append "stty -g > " (x->string tempfile))) (with-input-from-file tempfile read-line)) (sys-unlink tempfile))))) (else (define (has-windows-console?) #f)))
(define (without-echoing iport proc) (cond-expand (gauche.os.windows (cond ((not iport) (if (has-windows-console?) (call-with-input-file "CON" (cut without-echoing <> proc)) (without-echoing (standard-input-port) proc))) ((sys-isatty iport) (let () (define ihandle (sys-get-std-handle STD_INPUT_HANDLE)) (define orig-mode (sys-get-console-mode ihandle)) (define (echo-off) (sys-set-console-mode ihandle (logand orig-mode (lognot ENABLE_ECHO_INPUT)))) (define (echo-on) (sys-set-console-mode ihandle orig-mode)) (unwind-protect (begin (echo-off) (proc iport)) (echo-on)))) ((not (has-windows-console?)) (let () (define saved-attr (msys-get-stty)) (define (echo-off) (sys-system "stty -echo  icanon  iexten  isig")) (define (echo-on) (sys-system (string-append "stty " (x->string saved-attr)))) (unwind-protect (begin (echo-off) (proc iport)) (echo-on)))) (else (proc iport)))) (else (cond ((not iport) (call-with-input-file "/dev/tty" (cut without-echoing <> proc))) ((sys-isatty iport) (let () (define attr (sys-tcgetattr iport)) (define lflag-save (ref attr 'lflag)) (define (echo-off) (set! (ref attr 'lflag) (logior ICANON IEXTEN ISIG (logand (ref attr 'lflag) (lognot ECHO)))) (sys-tcsetattr iport TCSANOW attr)) (define (echo-on) (set! (ref attr 'lflag) lflag-save) (sys-tcsetattr iport TCSANOW attr)) (unwind-protect (begin (echo-off) (proc iport)) (echo-on)))) (else (proc iport))))))
(define (with-terminal-mode port mode proc :optional (cleanup #f)) (cond-expand (gauche.os.windows (if (has-windows-console?) (proc port) (let () (define saved-attr (msys-get-stty)) (define saved-buffering (port-buffering port)) (define new-attr (case mode ((raw) "-echo -icanon -iexten -isig") ((rare) "-echo -icanon -iexten  isig") ((cooked) " echo  icanon  iexten  isig") (else (error "terminal mode needs to be one of cooked, rare or raw, but got:" mode)))) (define (set) (sys-system (string-append "stty " (x->string new-attr))) (when (memq mode '(raw rare)) (set! (port-buffering port) :none))) (define (reset) (sys-system (string-append "stty " (x->string saved-attr))) (set! (port-buffering port) saved-buffering) (when cleanup (cleanup))) (unwind-protect (begin (set) (proc port)) (reset))))) (else (if (not (sys-isatty port)) (proc port) (let () (define saved-attr (sys-tcgetattr port)) (define saved-buffering (port-buffering port)) (define new-attr (rlet1 attr (sys-termios-copy saved-attr) (case mode ((raw) (set! (~ attr 'iflag) (logand (~ attr 'iflag) (lognot (logior BRKINT ICRNL INPCK ISTRIP IXON)))) (set! (~ attr 'oflag) (logand (~ attr 'oflag) (lognot OPOST))) (set! (~ attr 'cflag) (logior (logand (~ attr 'cflag) (lognot (logior CSIZE PARENB))) CS8)) (set! (~ attr 'lflag) (logand (~ attr 'lflag) (lognot (logior ECHO ICANON IEXTEN ISIG))))) ((rare) (set! (~ attr 'iflag) (logior BRKINT (logand (~ attr 'iflag) (lognot (logior ICRNL INPCK ISTRIP IXON))))) (set! (~ attr 'oflag) (logand (~ attr 'oflag) (lognot OPOST))) (set! (~ attr 'cflag) (logior (logand (~ attr 'cflag) (lognot (logior CSIZE PARENB))) CS8)) (set! (~ attr 'lflag) (logior ISIG (logand (~ attr 'lflag) (lognot (logior ECHO ICANON IEXTEN)))))) ((cooked) (set! (~ attr 'iflag) (logior (~ attr 'iflag) BRKINT ICRNL INPCK ISTRIP IXON)) (set! (~ attr 'oflag) (logior (~ attr 'oflag) OPOST)) (set! (~ attr 'lflag) (logior (~ attr 'lflag) ECHO ICANON IEXTEN ISIG))) (else (error "terminal mode needs to be one of cooked, rare or raw, but got:" mode))))) (define (set) (sys-tcsetattr port TCSANOW new-attr) (when (memq mode '(raw rare)) (set! (port-buffering port) :none))) (define (reset) (sys-tcsetattr port TCSANOW saved-attr) (set! (port-buffering port) saved-buffering) (when cleanup (cleanup))) (unwind-protect (begin (set) (proc port)) (reset)))))))
