stdin_unix.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright 2021 The TCell Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use file except in compliance with the License.
  5. // You may obtain a copy of the license at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || zos
  15. // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris zos
  16. package tcell
  17. import (
  18. "errors"
  19. "fmt"
  20. "os"
  21. "os/signal"
  22. "strconv"
  23. "sync"
  24. "syscall"
  25. "time"
  26. "golang.org/x/term"
  27. )
  28. // stdIoTty is an implementation of the Tty API based upon stdin/stdout.
  29. type stdIoTty struct {
  30. fd int
  31. in *os.File
  32. out *os.File
  33. saved *term.State
  34. sig chan os.Signal
  35. cb func()
  36. stopQ chan struct{}
  37. dev string
  38. wg sync.WaitGroup
  39. l sync.Mutex
  40. }
  41. func (tty *stdIoTty) Read(b []byte) (int, error) {
  42. return tty.in.Read(b)
  43. }
  44. func (tty *stdIoTty) Write(b []byte) (int, error) {
  45. return tty.out.Write(b)
  46. }
  47. func (tty *stdIoTty) Close() error {
  48. return nil
  49. }
  50. func (tty *stdIoTty) Start() error {
  51. tty.l.Lock()
  52. defer tty.l.Unlock()
  53. // We open another copy of /dev/tty. This is a workaround for unusual behavior
  54. // observed in macOS, apparently caused when a sub-shell (for example) closes our
  55. // own tty device (when it exits for example). Getting a fresh new one seems to
  56. // resolve the problem. (We believe this is a bug in the macOS tty driver that
  57. // fails to account for dup() references to the same file before applying close()
  58. // related behaviors to the tty.) We're also holding the original copy we opened
  59. // since closing that might have deleterious effects as well. The upshot is that
  60. // we will have up to two separate file handles open on /dev/tty. (Note that when
  61. // using stdin/stdout instead of /dev/tty this problem is not observed.)
  62. var err error
  63. tty.in = os.Stdin
  64. tty.out = os.Stdout
  65. tty.fd = int(tty.in.Fd())
  66. if !term.IsTerminal(tty.fd) {
  67. return errors.New("device is not a terminal")
  68. }
  69. _ = tty.in.SetReadDeadline(time.Time{})
  70. saved, err := term.MakeRaw(tty.fd) // also sets vMin and vTime
  71. if err != nil {
  72. return err
  73. }
  74. tty.saved = saved
  75. tty.stopQ = make(chan struct{})
  76. tty.wg.Add(1)
  77. go func(stopQ chan struct{}) {
  78. defer tty.wg.Done()
  79. for {
  80. select {
  81. case <-tty.sig:
  82. tty.l.Lock()
  83. cb := tty.cb
  84. tty.l.Unlock()
  85. if cb != nil {
  86. cb()
  87. }
  88. case <-stopQ:
  89. return
  90. }
  91. }
  92. }(tty.stopQ)
  93. signal.Notify(tty.sig, syscall.SIGWINCH)
  94. return nil
  95. }
  96. func (tty *stdIoTty) Drain() error {
  97. _ = tty.in.SetReadDeadline(time.Now())
  98. if err := tcSetBufParams(tty.fd, 0, 0); err != nil {
  99. return err
  100. }
  101. return nil
  102. }
  103. func (tty *stdIoTty) Stop() error {
  104. tty.l.Lock()
  105. if err := term.Restore(tty.fd, tty.saved); err != nil {
  106. tty.l.Unlock()
  107. return err
  108. }
  109. _ = tty.in.SetReadDeadline(time.Now())
  110. signal.Stop(tty.sig)
  111. close(tty.stopQ)
  112. tty.l.Unlock()
  113. tty.wg.Wait()
  114. return nil
  115. }
  116. func (tty *stdIoTty) WindowSize() (int, int, error) {
  117. w, h, err := term.GetSize(tty.fd)
  118. if err != nil {
  119. return 0, 0, err
  120. }
  121. if w == 0 {
  122. w, _ = strconv.Atoi(os.Getenv("COLUMNS"))
  123. }
  124. if w == 0 {
  125. w = 80 // default
  126. }
  127. if h == 0 {
  128. h, _ = strconv.Atoi(os.Getenv("LINES"))
  129. }
  130. if h == 0 {
  131. h = 25 // default
  132. }
  133. return w, h, nil
  134. }
  135. func (tty *stdIoTty) NotifyResize(cb func()) {
  136. tty.l.Lock()
  137. tty.cb = cb
  138. tty.l.Unlock()
  139. }
  140. // NewStdioTty opens a tty using standard input/output.
  141. func NewStdIoTty() (Tty, error) {
  142. tty := &stdIoTty{
  143. sig: make(chan os.Signal),
  144. in: os.Stdin,
  145. out: os.Stdout,
  146. }
  147. var err error
  148. tty.fd = int(tty.in.Fd())
  149. if !term.IsTerminal(tty.fd) {
  150. return nil, errors.New("not a terminal")
  151. }
  152. if tty.saved, err = term.GetState(tty.fd); err != nil {
  153. return nil, fmt.Errorf("failed to get state: %w", err)
  154. }
  155. return tty, nil
  156. }