💬 Package telnet implements the telnet protocol. https://maze.io/telnet.v1
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

console_windows.go 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package telnet
  2. import (
  3. "os"
  4. "syscall"
  5. "unsafe"
  6. )
  7. type (
  8. short int16
  9. word uint16
  10. iodevice syscall.Handle
  11. coord struct {
  12. x short
  13. y short
  14. }
  15. smallRect struct {
  16. left short
  17. top short
  18. right short
  19. bottom short
  20. }
  21. consoleScreenBufferInfo struct {
  22. size coord
  23. cursorPosition coord
  24. attributes word
  25. window smallRect
  26. maximumWindowSize coord
  27. }
  28. )
  29. var (
  30. kernel32 = syscall.NewLazyDLL("kernel32.dll")
  31. procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo")
  32. in, out iodevice
  33. )
  34. func getConsoleScreenBufferInfo(h syscall.Handle, info *consoleScreenBufferInfo) (err error) {
  35. r0, _, e1 := syscall.Syscall(procGetConsoleScreenBufferInfo.Addr(), 2, uintptr(h), uintptr(unsafe.Pointer(info)), 0)
  36. if int(r0) == 0 {
  37. if e1 != 0 {
  38. err = error(e1)
  39. } else {
  40. err = syscall.EINVAL
  41. }
  42. }
  43. return
  44. }
  45. func getWinSize(out syscall.Handle) coord {
  46. err := getConsoleScreenBufferInfo(out, &tmp_info)
  47. if err != nil {
  48. panic(err)
  49. }
  50. return coord{
  51. x: tmp_info.window.right - tmp_info.window.left + 1,
  52. y: tmp_info.window.bottom - tmp_info.window.top + 1,
  53. }
  54. }
  55. func writeTo(fd iodevice, p []byte) (int, error) {
  56. f := os.NewFile(uintptr(fd), "COMOUT$")
  57. return f.Write(p)
  58. }
  59. func init() {
  60. var err error
  61. var i, o syscall.Handle
  62. i, err = syscall.Open("CONIN$", syscall.O_RDWR, 0)
  63. if err != nil {
  64. panic(err)
  65. }
  66. o, err = syscall.Open("CONOUT$", syscall.O_RDWR, 0)
  67. if err != nil {
  68. panic(err)
  69. }
  70. in, out = iodevice(i), iodevice(o)
  71. }