console_windows.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. Copyright The containerd Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package console
  14. import (
  15. "fmt"
  16. "os"
  17. "github.com/pkg/errors"
  18. "golang.org/x/sys/windows"
  19. )
  20. var (
  21. vtInputSupported bool
  22. ErrNotImplemented = errors.New("not implemented")
  23. )
  24. func (m *master) initStdios() {
  25. m.in = windows.Handle(os.Stdin.Fd())
  26. if err := windows.GetConsoleMode(m.in, &m.inMode); err == nil {
  27. // Validate that windows.ENABLE_VIRTUAL_TERMINAL_INPUT is supported, but do not set it.
  28. if err = windows.SetConsoleMode(m.in, m.inMode|windows.ENABLE_VIRTUAL_TERMINAL_INPUT); err == nil {
  29. vtInputSupported = true
  30. }
  31. // Unconditionally set the console mode back even on failure because SetConsoleMode
  32. // remembers invalid bits on input handles.
  33. windows.SetConsoleMode(m.in, m.inMode)
  34. } else {
  35. fmt.Printf("failed to get console mode for stdin: %v\n", err)
  36. }
  37. m.out = windows.Handle(os.Stdout.Fd())
  38. if err := windows.GetConsoleMode(m.out, &m.outMode); err == nil {
  39. if err := windows.SetConsoleMode(m.out, m.outMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING); err == nil {
  40. m.outMode |= windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING
  41. } else {
  42. windows.SetConsoleMode(m.out, m.outMode)
  43. }
  44. } else {
  45. fmt.Printf("failed to get console mode for stdout: %v\n", err)
  46. }
  47. m.err = windows.Handle(os.Stderr.Fd())
  48. if err := windows.GetConsoleMode(m.err, &m.errMode); err == nil {
  49. if err := windows.SetConsoleMode(m.err, m.errMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING); err == nil {
  50. m.errMode |= windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING
  51. } else {
  52. windows.SetConsoleMode(m.err, m.errMode)
  53. }
  54. } else {
  55. fmt.Printf("failed to get console mode for stderr: %v\n", err)
  56. }
  57. }
  58. type master struct {
  59. in windows.Handle
  60. inMode uint32
  61. out windows.Handle
  62. outMode uint32
  63. err windows.Handle
  64. errMode uint32
  65. }
  66. func (m *master) SetRaw() error {
  67. if err := makeInputRaw(m.in, m.inMode); err != nil {
  68. return err
  69. }
  70. // Set StdOut and StdErr to raw mode, we ignore failures since
  71. // windows.DISABLE_NEWLINE_AUTO_RETURN might not be supported on this version of
  72. // Windows.
  73. windows.SetConsoleMode(m.out, m.outMode|windows.DISABLE_NEWLINE_AUTO_RETURN)
  74. windows.SetConsoleMode(m.err, m.errMode|windows.DISABLE_NEWLINE_AUTO_RETURN)
  75. return nil
  76. }
  77. func (m *master) Reset() error {
  78. for _, s := range []struct {
  79. fd windows.Handle
  80. mode uint32
  81. }{
  82. {m.in, m.inMode},
  83. {m.out, m.outMode},
  84. {m.err, m.errMode},
  85. } {
  86. if err := windows.SetConsoleMode(s.fd, s.mode); err != nil {
  87. return errors.Wrap(err, "unable to restore console mode")
  88. }
  89. }
  90. return nil
  91. }
  92. func (m *master) Size() (WinSize, error) {
  93. var info windows.ConsoleScreenBufferInfo
  94. err := windows.GetConsoleScreenBufferInfo(m.out, &info)
  95. if err != nil {
  96. return WinSize{}, errors.Wrap(err, "unable to get console info")
  97. }
  98. winsize := WinSize{
  99. Width: uint16(info.Window.Right - info.Window.Left + 1),
  100. Height: uint16(info.Window.Bottom - info.Window.Top + 1),
  101. }
  102. return winsize, nil
  103. }
  104. func (m *master) Resize(ws WinSize) error {
  105. return ErrNotImplemented
  106. }
  107. func (m *master) ResizeFrom(c Console) error {
  108. return ErrNotImplemented
  109. }
  110. func (m *master) DisableEcho() error {
  111. mode := m.inMode &^ windows.ENABLE_ECHO_INPUT
  112. mode |= windows.ENABLE_PROCESSED_INPUT
  113. mode |= windows.ENABLE_LINE_INPUT
  114. if err := windows.SetConsoleMode(m.in, mode); err != nil {
  115. return errors.Wrap(err, "unable to set console to disable echo")
  116. }
  117. return nil
  118. }
  119. func (m *master) Close() error {
  120. return nil
  121. }
  122. func (m *master) Read(b []byte) (int, error) {
  123. return os.Stdin.Read(b)
  124. }
  125. func (m *master) Write(b []byte) (int, error) {
  126. return os.Stdout.Write(b)
  127. }
  128. func (m *master) Fd() uintptr {
  129. return uintptr(m.in)
  130. }
  131. // on windows, console can only be made from os.Std{in,out,err}, hence there
  132. // isnt a single name here we can use. Return a dummy "console" value in this
  133. // case should be sufficient.
  134. func (m *master) Name() string {
  135. return "console"
  136. }
  137. // makeInputRaw puts the terminal (Windows Console) connected to the given
  138. // file descriptor into raw mode
  139. func makeInputRaw(fd windows.Handle, mode uint32) error {
  140. // See
  141. // -- https://msdn.microsoft.com/en-us/library/windows/desktop/ms686033(v=vs.85).aspx
  142. // -- https://msdn.microsoft.com/en-us/library/windows/desktop/ms683462(v=vs.85).aspx
  143. // Disable these modes
  144. mode &^= windows.ENABLE_ECHO_INPUT
  145. mode &^= windows.ENABLE_LINE_INPUT
  146. mode &^= windows.ENABLE_MOUSE_INPUT
  147. mode &^= windows.ENABLE_WINDOW_INPUT
  148. mode &^= windows.ENABLE_PROCESSED_INPUT
  149. // Enable these modes
  150. mode |= windows.ENABLE_EXTENDED_FLAGS
  151. mode |= windows.ENABLE_INSERT_MODE
  152. mode |= windows.ENABLE_QUICK_EDIT_MODE
  153. if vtInputSupported {
  154. mode |= windows.ENABLE_VIRTUAL_TERMINAL_INPUT
  155. }
  156. if err := windows.SetConsoleMode(fd, mode); err != nil {
  157. return errors.Wrap(err, "unable to set console to raw mode")
  158. }
  159. return nil
  160. }
  161. func checkConsole(f File) error {
  162. var mode uint32
  163. if err := windows.GetConsoleMode(windows.Handle(f.Fd()), &mode); err != nil {
  164. return err
  165. }
  166. return nil
  167. }
  168. func newMaster(f File) (Console, error) {
  169. if f != os.Stdin && f != os.Stdout && f != os.Stderr {
  170. return nil, errors.New("creating a console from a file is not supported on windows")
  171. }
  172. m := &master{}
  173. m.initStdios()
  174. return m, nil
  175. }