github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+incompatible/pkg/termios/termios.go (about)

     1  // Copyright 2015-2017 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package termios implements basic termios operations including getting
     6  // a tty struct, termio struct, a winsize struct, and setting raw mode.
     7  // To get a TTY, call termios.New.
     8  // To get a Termios, call tty.Get(); to set it, call tty.Set(*Termios)
     9  // To set raw mode and then restore, one can do:
    10  // tty := termios.NewTTY()
    11  // restorer, err := tty.Raw()
    12  // do things
    13  // tty.Set(restorer)
    14  package termios
    15  
    16  import (
    17  	"golang.org/x/sys/unix"
    18  )
    19  
    20  func (t *TTY) Raw() (*unix.Termios, error) {
    21  	restorer, err := t.Get()
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  
    26  	raw := MakeRaw(restorer)
    27  
    28  	if err := t.Set(raw); err != nil {
    29  		return nil, err
    30  	}
    31  	return restorer, nil
    32  }
    33  
    34  // Serial configure the serial TTY at given baudrate with ECHO and character conversion (CRNL, ERASE, KILL)
    35  func (t *TTY) Serial(baud int) (*unix.Termios, error) {
    36  	restorer, err := t.Get()
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	serial := MakeSerialDefault(restorer)
    42  
    43  	if baud != 0 {
    44  		if err := t.Set(serial); err != nil {
    45  			return nil, err
    46  		}
    47  
    48  		serial, err = MakeSerialBaud(serial, baud)
    49  		if err != nil {
    50  			return restorer, err
    51  		}
    52  	}
    53  
    54  	err = t.Set(serial)
    55  	return restorer, err
    56  }