| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
package term |
| 22 |
|
| 23 |
import ( |
| 24 |
"errors" |
| 25 |
"os" |
| 26 |
"strconv" |
| 27 |
"strings" |
| 28 |
"syscall" |
| 29 |
"unsafe" |
| 30 |
) |
| 31 |
|
| 32 |
|
| 33 |
const ( |
| 34 |
TCGETS = 0x5401 |
| 35 |
TCSETS = 0x5402 |
| 36 |
TIOCGWINSZ = 0x5413 |
| 37 |
TIOCSWINSZ = 0x5414 |
| 38 |
TIOCGPTN = 0x80045430 |
| 39 |
TIOCSPTLCK = 0x40045431 |
| 40 |
CBAUD = 0010017 |
| 41 |
CBAUDEX = 0010000 |
| 42 |
) |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
const ( |
| 47 |
IGNBRK = 0000001 |
| 48 |
BRKINT = 0000002 |
| 49 |
IGNPAR = 0000004 |
| 50 |
PARMRK = 0000010 |
| 51 |
INPCK = 0000020 |
| 52 |
ISTRIP = 0000040 |
| 53 |
INLCR = 0000100 |
| 54 |
IGNCR = 0000200 |
| 55 |
ICRNL = 0000400 |
| 56 |
IUCLC = 0001000 |
| 57 |
IXON = 0002000 |
| 58 |
IXANY = 0004000 |
| 59 |
IXOFF = 0010000 |
| 60 |
IMAXBEL = 0020000 |
| 61 |
IUTF8 = 0040000 |
| 62 |
) |
| 63 |
|
| 64 |
|
| 65 |
const ( |
| 66 |
OPOST = 0000001 |
| 67 |
OLCUC = 0000002 |
| 68 |
ONLCR = 0000004 |
| 69 |
OCRNL = 0000010 |
| 70 |
ONOCR = 0000020 |
| 71 |
ONLRET = 0000040 |
| 72 |
OFILL = 0000100 |
| 73 |
OFDEL = 0000200 |
| 74 |
) |
| 75 |
|
| 76 |
|
| 77 |
const ( |
| 78 |
CSIZE = 0000060 |
| 79 |
CS5 = 0000000 |
| 80 |
CS6 = 0000020 |
| 81 |
CS7 = 0000040 |
| 82 |
CS8 = 0000060 |
| 83 |
CSTOPB = 0000100 |
| 84 |
CREAD = 0000200 |
| 85 |
PARENB = 0000400 |
| 86 |
PARODD = 0001000 |
| 87 |
HUPCL = 0002000 |
| 88 |
CLOCAL = 0004000 |
| 89 |
) |
| 90 |
|
| 91 |
|
| 92 |
const ( |
| 93 |
ISIG = 0000001 |
| 94 |
ICANON = 0000002 |
| 95 |
XCASE = 0000004 |
| 96 |
ECHO = 0000010 |
| 97 |
ECHOE = 0000020 |
| 98 |
ECHOK = 0000040 |
| 99 |
ECHONL = 0000100 |
| 100 |
NOFLSH = 0000200 |
| 101 |
TOSTOP = 0000400 |
| 102 |
ECHOCTL = 0001000 |
| 103 |
ECHOPRT = 0002000 |
| 104 |
ECHOKE = 0004000 |
| 105 |
IEXTEN = 0100000 |
| 106 |
) |
| 107 |
|
| 108 |
|
| 109 |
const ( |
| 110 |
VINTR = 0 |
| 111 |
VQUIT = 1 |
| 112 |
VERASE = 2 |
| 113 |
VKILL = 3 |
| 114 |
VEOF = 4 |
| 115 |
VTIME = 5 |
| 116 |
VMIN = 6 |
| 117 |
VSWTC = 7 |
| 118 |
VSTART = 8 |
| 119 |
VSTOP = 9 |
| 120 |
VSUSP = 10 |
| 121 |
VEOL = 11 |
| 122 |
VREPRINT = 12 |
| 123 |
VDISCARD = 13 |
| 124 |
VWERASE = 14 |
| 125 |
VLNEXT = 15 |
| 126 |
VEOL2 = 16 |
| 127 |
tNCCS = 32 |
| 128 |
) |
| 129 |
|
| 130 |
|
| 131 |
type Termios struct { |
| 132 |
Iflag uint32 |
| 133 |
Oflag uint32 |
| 134 |
Cflag uint32 |
| 135 |
Lflag uint32 |
| 136 |
Line byte |
| 137 |
Cc [tNCCS]byte |
| 138 |
Ispeed uint32 |
| 139 |
Ospeed uint32 |
| 140 |
Wz Winsize |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
type Winsize struct { |
| 145 |
WsRow uint16 |
| 146 |
WsCol uint16 |
| 147 |
WsXpixel uint16 |
| 148 |
WsYpixel uint16 |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
type PTY struct { |
| 163 |
Master *os.File |
| 164 |
Slave *os.File |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
func (t *Termios) Raw() { |
| 171 |
t.Iflag &^= IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON |
| 172 |
|
| 173 |
t.Oflag &^= OPOST |
| 174 |
t.Lflag &^= ECHO | ECHONL | ICANON | ISIG | IEXTEN |
| 175 |
t.Cflag &^= CSIZE | PARENB |
| 176 |
t.Cflag |= CS8 |
| 177 |
t.Cc[VMIN] = 1 |
| 178 |
t.Cc[VTIME] = 0 |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
func (t *Termios) Cook() { |
| 184 |
t.Iflag |= BRKINT | IGNPAR | ISTRIP | ICRNL | IXON |
| 185 |
t.Oflag |= OPOST |
| 186 |
t.Lflag |= ISIG | ICANON |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
func (t *Termios) Sane() { |
| 192 |
t.Iflag &^= IGNBRK | INLCR | IGNCR | IUTF8 | IXOFF | IUCLC | IXANY |
| 193 |
t.Iflag |= BRKINT | ICRNL | IMAXBEL |
| 194 |
t.Oflag |= OPOST | ONLCR |
| 195 |
t.Oflag &^= OLCUC | OCRNL | ONOCR | ONLRET |
| 196 |
t.Cflag |= CREAD |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
func (t *Termios) Set(file *os.File) error { |
| 201 |
fd := file.Fd() |
| 202 |
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), uintptr(TCSETS), uintptr(unsafe.Pointer(t))) |
| 203 |
if errno != 0 { |
| 204 |
return errno |
| 205 |
} |
| 206 |
return nil |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
func Attr(file *os.File) (Termios, error) { |
| 211 |
var t Termios |
| 212 |
fd := file.Fd() |
| 213 |
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), uintptr(TCGETS), uintptr(unsafe.Pointer(&t))) |
| 214 |
if errno != 0 { |
| 215 |
return t, errno |
| 216 |
} |
| 217 |
t.Ispeed &= CBAUD | CBAUDEX |
| 218 |
t.Ospeed &= CBAUD | CBAUDEX |
| 219 |
return t, nil |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
func Isatty(file *os.File) bool { |
| 224 |
_, err := Attr(file) |
| 225 |
return err == nil |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
func GetPass(prompt string, f *os.File, pbuf []byte) ([]byte, error) { |
| 230 |
t, err := Attr(f) |
| 231 |
if err != nil { |
| 232 |
return nil, err |
| 233 |
} |
| 234 |
defer t.Set(f) |
| 235 |
noecho := t |
| 236 |
noecho.Lflag = noecho.Lflag &^ ECHO |
| 237 |
if err := noecho.Set(f); err != nil { |
| 238 |
return nil, err |
| 239 |
} |
| 240 |
b := make([]byte, 1, 1) |
| 241 |
i := 0 |
| 242 |
if _, err := f.Write([]byte(prompt)); err != nil { |
| 243 |
return nil, err |
| 244 |
} |
| 245 |
for ; i < len(pbuf); i++ { |
| 246 |
if _, err := f.Read(b); err != nil { |
| 247 |
b[0] = 0 |
| 248 |
clearbuf(pbuf[:i+1]) |
| 249 |
} |
| 250 |
if b[0] == '\n' || b[0] == '\r' { |
| 251 |
return pbuf[:i], nil |
| 252 |
} |
| 253 |
pbuf[i] = b[0] |
| 254 |
b[0] = 0 |
| 255 |
} |
| 256 |
clearbuf(pbuf[:i+1]) |
| 257 |
return nil, errors.New("ran out of bufferspace") |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
func clearbuf(b []byte) { |
| 262 |
for i := range b { |
| 263 |
b[i] = 0 |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
func GetChar(f *os.File) (b byte, err error) { |
| 269 |
bs := make([]byte, 1, 1) |
| 270 |
if _, err = f.Read(bs); err != nil { |
| 271 |
return 0, err |
| 272 |
} |
| 273 |
return bs[0], err |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
func (p *PTY) PTSName() (string, error) { |
| 278 |
n, err := p.PTSNumber() |
| 279 |
if err != nil { |
| 280 |
return "", err |
| 281 |
} |
| 282 |
return "/dev/pts/" + strconv.Itoa(int(n)), nil |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
func (p *PTY) PTSNumber() (uint, error) { |
| •287 |
var ptyno uint32 |
| 288 |
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(p.Master.Fd()), uintptr(TIOCGPTN), uintptr(unsafe.Pointer(&ptyno))) |
| 289 |
if errno != 0 { |
| 290 |
return 0, errno |
| 291 |
} |
| •292 |
return uint(ptyno), nil |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
func (t *Termios) Winsz(file *os.File) error { |
| 310 |
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(file.Fd()), uintptr(TIOCGWINSZ), uintptr(unsafe.Pointer(&t.Wz))) |
| 311 |
if errno != 0 { |
| 312 |
return errno |
| 313 |
} |
| 314 |
return nil |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
func (t *Termios) Setwinsz(file *os.File) error { |
| 319 |
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(file.Fd()), uintptr(TIOCSWINSZ), uintptr(unsafe.Pointer(&t.Wz))) |
| 320 |
if errno != 0 { |
| 321 |
return errno |
| 322 |
} |
| 323 |
return nil |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
func OpenPTY() (*PTY, error) { |
| 328 |
|
| 329 |
master, err := os.OpenFile("/dev/ptmx", os.O_RDWR, 0) |
| 330 |
if err != nil { |
| 331 |
return nil, err |
| 332 |
} |
| 333 |
|
| 334 |
|
| •335 |
var unlock int32 |
| 336 |
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(master.Fd()), uintptr(TIOCSPTLCK), uintptr(unsafe.Pointer(&unlock))); errno != 0 { |
| 337 |
master.Close() |
| 338 |
return nil, errno |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
pty := &PTY{Master: master} |
| 343 |
slaveStr, err := pty.PTSName() |
| 344 |
if err != nil { |
| 345 |
master.Close() |
| 346 |
return nil, err |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
pty.Slave, err = os.OpenFile(slaveStr, os.O_RDWR|syscall.O_NOCTTY, 0) |
| 351 |
if err != nil { |
| 352 |
master.Close() |
| 353 |
return nil, err |
| 354 |
} |
| 355 |
|
| 356 |
return pty, nil |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
func (p *PTY) Close() error { |
| 361 |
slaveErr := errors.New("Slave FD nil") |
| 362 |
if p.Slave != nil { |
| 363 |
slaveErr = p.Slave.Close() |
| 364 |
} |
| 365 |
masterErr := errors.New("Master FD nil") |
| 366 |
if p.Master != nil { |
| 367 |
masterErr = p.Master.Close() |
| 368 |
} |
| 369 |
if slaveErr != nil || masterErr != nil { |
| 370 |
var errs []string |
| 371 |
if slaveErr != nil { |
| 372 |
errs = append(errs, "Slave: "+slaveErr.Error()) |
| 373 |
} |
| 374 |
if masterErr != nil { |
| 375 |
errs = append(errs, "Master: "+masterErr.Error()) |
| 376 |
} |
| 377 |
return errors.New(strings.Join(errs, " ")) |
| 378 |
} |
| 379 |
return nil |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
func (p *PTY) ReadByte() (byte, error) { |
| 384 |
bs := make([]byte, 1, 1) |
| 385 |
_, err := p.Master.Read(bs) |
| 386 |
return bs[0], err |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
func (p *PTY) GetChar() (byte, error) { |
| 391 |
return p.ReadByte() |
| 392 |
} |