github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+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  type (
    17  	// TTY is an os-independent version of the combined info in termios and window size structs.
    18  	// It is used to get/set info to the termios functions as well as marshal/unmarshal data
    19  	// in JSON format for dump and loading.
    20  	TTY struct {
    21  		Ispeed int
    22  		Ospeed int
    23  		Row    int
    24  		Col    int
    25  
    26  		CC map[string]uint8
    27  
    28  		Opts map[string]bool
    29  	}
    30  )
    31  
    32  // Raw sets the tty into raw mode.
    33  func (t *TTYIO) Raw() (*Termios, error) {
    34  	restorer, err := t.Get()
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	raw := MakeRaw(restorer)
    40  
    41  	if err := t.Set(raw); err != nil {
    42  		return nil, err
    43  	}
    44  	return restorer, nil
    45  }
    46  
    47  // Serial configure the serial TTY at given baudrate with ECHO and character conversion (CRNL, ERASE, KILL)
    48  func (t *TTYIO) Serial(baud int) (*Termios, error) {
    49  	restorer, err := t.Get()
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  
    54  	serial := MakeSerialDefault(restorer)
    55  
    56  	if baud != 0 {
    57  		if err := t.Set(serial); err != nil {
    58  			return nil, err
    59  		}
    60  
    61  		serial, err = MakeSerialBaud(serial, baud)
    62  		if err != nil {
    63  			return restorer, err
    64  		}
    65  	}
    66  
    67  	err = t.Set(serial)
    68  	return restorer, err
    69  }
    70  
    71  func (t *TTYIO) Read(b []byte) (int, error) {
    72  	return t.f.Read(b)
    73  }
    74  
    75  func (t *TTYIO) Write(b []byte) (int, error) {
    76  	return t.f.Write(b)
    77  }