console_windows.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. "errors"
  16. "fmt"
  17. "os"
  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. // Note: We discard console mode warnings, because in/out can be redirected.
  26. //
  27. // TODO: Investigate opening CONOUT$/CONIN$ to handle this correctly
  28. m.in = windows.Handle(os.Stdin.Fd())
  29. if err := windows.GetConsoleMode(m.in, &m.inMode); err == nil {
  30. // Validate that windows.ENABLE_VIRTUAL_TERMINAL_INPUT is supported, but do not set it.
  31. if err = windows.SetConsoleMode(m.in, m.inMode|windows.ENABLE_VIRTUAL_TERMINAL_INPUT); err == nil {
  32. vtInputSupported = true
  33. }
  34. // Unconditionally set the console mode back even on failure because SetConsoleMode
  35. // remembers invalid bits on input handles.
  36. windows.SetConsoleMode(m.in, m.inMode)
  37. }
  38. m.out = windows.Handle(os.Stdout.Fd())
  39. if err := windows.GetConsoleMode(m.out, &m.outMode); err == nil {
  40. if err := windows.SetConsoleMode(m.out, m.outMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING); err == nil {
  41. m.outMode |= windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING
  42. } else {
  43. windows.SetConsoleMode(m.out, m.outMode)
  44. }
  45. }
  46. m.err = windows.Handle(os.Stderr.Fd())
  47. if err := windows.GetConsoleMode(m.err, &m.errMode); err == nil {
  48. if err := windows.SetConsoleMode(m.err, m.errMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING); err == nil {
  49. m.errMode |= windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING
  50. } else {
  51. windows.SetConsoleMode(m.err, m.errMode)
  52. }
  53. }
  54. }
  55. type master struct {
  56. in windows.Handle
  57. inMode uint32
  58. out windows.Handle
  59. outMode uint32
  60. err windows.Handle
  61. errMode uint32
  62. }
  63. func (m *master) SetRaw() error {
  64. if err := makeInputRaw(m.in, m.inMode); err != nil {
  65. return err
  66. }
  67. // Set StdOut and StdErr to raw mode, we ignore failures since
  68. // windows.DISABLE_NEWLINE_AUTO_RETURN might not be supported on this version of
  69. // Windows.
  70. windows.SetConsoleMode(m.out, m.outMode|windows.DISABLE_NEWLINE_AUTO_RETURN)
  71. windows.SetConsoleMode(m.err, m.errMode|windows.DISABLE_NEWLINE_AUTO_RETURN)
  72. return nil
  73. }
  74. func (m *master) Reset() error {
  75. var errs []error
  76. for _, s := range []struct {
  77. fd windows.Handle
  78. mode uint32
  79. }{
  80. {m.in, m.inMode},
  81. {m.out, m.outMode},
  82. {m.err, m.errMode},
  83. } {
  84. if err := windows.SetConsoleMode(s.fd, s.mode); err != nil {
  85. // we can't just abort on the first error, otherwise we might leave
  86. // the console in an unexpected state.
  87. errs = append(errs, fmt.Errorf("unable to restore console mode: %w", err))
  88. }
  89. }
  90. if len(errs) > 0 {
  91. return errs[0]
  92. }
  93. return nil
  94. }
  95. func (m *master) Size() (WinSize, error) {
  96. var info windows.ConsoleScreenBufferInfo
  97. err := windows.GetConsoleScreenBufferInfo(m.out, &info)
  98. if err != nil {
  99. return WinSize{}, fmt.Errorf("unable to get console info: %w", err)
  100. }
  101. winsize := WinSize{
  102. Width: uint16(info.Window.Right - info.Window.Left + 1),
  103. Height: uint16(info.Window.Bottom - info.Window.Top + 1),
  104. }
  105. return winsize, nil
  106. }
  107. func (m *master) Resize(ws WinSize) error {
  108. return ErrNotImplemented
  109. }
  110. func (m *master) ResizeFrom(c Console) error {
  111. return ErrNotImplemented
  112. }
  113. func (m *master) DisableEcho() error {
  114. mode := m.inMode &^ windows.ENABLE_ECHO_INPUT
  115. mode |= windows.ENABLE_PROCESSED_INPUT
  116. mode |= windows.ENABLE_LINE_INPUT
  117. if err := windows.SetConsoleMode(m.in, mode); err != nil {
  118. return fmt.Errorf("unable to set console to disable echo: %w", err)
  119. }
  120. return nil
  121. }
  122. func (m *master) Close() error {
  123. return nil
  124. }
  125. func (m *master) Read(b []byte) (int, error) {
  126. return os.Stdin.Read(b)
  127. }
  128. func (m *master) Write(b []byte) (int, error) {
  129. return os.Stdout.Write(b)
  130. }
  131. func (m *master) Fd() uintptr {
  132. return uintptr(m.in)
  133. }
  134. // on windows, console can only be made from os.Std{in,out,err}, hence there
  135. // isnt a single name here we can use. Return a dummy "console" value in this
  136. // case should be sufficient.
  137. func (m *master) Name() string {
  138. return "console"
  139. }
  140. // makeInputRaw puts the terminal (Windows Console) connected to the given
  141. // file descriptor into raw mode
  142. func makeInputRaw(fd windows.Handle, mode uint32) error {
  143. // See
  144. // -- https://msdn.microsoft.com/en-us/library/windows/desktop/ms686033(v=vs.85).aspx
  145. // -- https://msdn.microsoft.com/en-us/library/windows/desktop/ms683462(v=vs.85).aspx
  146. // Disable these modes
  147. mode &^= windows.ENABLE_ECHO_INPUT
  148. mode &^= windows.ENABLE_LINE_INPUT
  149. mode &^= windows.ENABLE_MOUSE_INPUT
  150. mode &^= windows.ENABLE_WINDOW_INPUT
  151. mode &^= windows.ENABLE_PROCESSED_INPUT
  152. // Enable these modes
  153. mode |= windows.ENABLE_EXTENDED_FLAGS
  154. mode |= windows.ENABLE_INSERT_MODE
  155. mode |= windows.ENABLE_QUICK_EDIT_MODE
  156. if vtInputSupported {
  157. mode |= windows.ENABLE_VIRTUAL_TERMINAL_INPUT
  158. }
  159. if err := windows.SetConsoleMode(fd, mode); err != nil {
  160. return fmt.Errorf("unable to set console to raw mode: %w", err)
  161. }
  162. return nil
  163. }
  164. func checkConsole(f File) error {
  165. var mode uint32
  166. if err := windows.GetConsoleMode(windows.Handle(f.Fd()), &mode); err != nil {
  167. return err
  168. }
  169. return nil
  170. }
  171. func newMaster(f File) (Console, error) {
  172. if f != os.Stdin && f != os.Stdout && f != os.Stderr {
  173. return nil, errors.New("creating a console from a file is not supported on windows")
  174. }
  175. m := &master{}
  176. m.initStdios()
  177. return m, nil
  178. }