amuz.es/src/infra/goutils@v0.1.3/tty/code/termcodes.go (about)

     1  // +build !windows
     2  
     3  package code
     4  
     5  import (
     6  	"os"
     7  	"syscall"
     8  )
     9  
    10  // POSIX terminal mode flags as listed in RFC 4254 Section 8.
    11  const (
    12  	tty_OP_END    = 0
    13  	VINTR         = 1
    14  	VQUIT         = 2
    15  	VERASE        = 3
    16  	VKILL         = 4
    17  	VEOF          = 5
    18  	VEOL          = 6
    19  	VEOL2         = 7
    20  	VSTART        = 8
    21  	VSTOP         = 9
    22  	VSUSP         = 10
    23  	VDSUSP        = 11
    24  	VREPRINT      = 12
    25  	VWERASE       = 13
    26  	VLNEXT        = 14
    27  	VFLUSH        = 15
    28  	VSWTCH        = 16
    29  	VSTATUS       = 17
    30  	VDISCARD      = 18
    31  	IGNPAR        = 30
    32  	PARMRK        = 31
    33  	INPCK         = 32
    34  	ISTRIP        = 33
    35  	INLCR         = 34
    36  	IGNCR         = 35
    37  	ICRNL         = 36
    38  	IUCLC         = 37
    39  	IXON          = 38
    40  	IXANY         = 39
    41  	IXOFF         = 40
    42  	IMAXBEL       = 41
    43  	IUTF8         = 42
    44  	ISIG          = 50
    45  	ICANON        = 51
    46  	XCASE         = 52
    47  	ECHO          = 53
    48  	ECHOE         = 54
    49  	ECHOK         = 55
    50  	ECHONL        = 56
    51  	NOFLSH        = 57
    52  	TOSTOP        = 58
    53  	IEXTEN        = 59
    54  	ECHOCTL       = 60
    55  	ECHOKE        = 61
    56  	PENDIN        = 62
    57  	OPOST         = 70
    58  	OLCUC         = 71
    59  	ONLCR         = 72
    60  	OCRNL         = 73
    61  	ONOCR         = 74
    62  	ONLRET        = 75
    63  	CS7           = 90
    64  	CS8           = 91
    65  	PARENB        = 92
    66  	PARODD        = 93
    67  	TTY_OP_ISPEED = 128
    68  	TTY_OP_OSPEED = 129
    69  )
    70  
    71  // struct termios {
    72  // 	tcflag_t c_iflag;   /* input modes */
    73  // 	tcflag_t c_oflag;   /* output modes */
    74  // 	tcflag_t c_cflag;   /* control modes */
    75  // 	tcflag_t c_lflag;   /* local modes */
    76  // 	cc_t c_cc[NCCS];    /* special characters */
    77  // 	speed_t c_ispeed;
    78  // 	speed_t c_ospeed;
    79  // };
    80  
    81  type Setter interface {
    82  	Set(pty *os.File, termios *syscall.Termios, value uint32) error
    83  }
    84  
    85  var TermAttrSetters = map[uint8]Setter{
    86  	VINTR:    &ccSetter{Character: syscall.VINTR},
    87  	VQUIT:    &ccSetter{Character: syscall.VQUIT},
    88  	VERASE:   &ccSetter{Character: syscall.VERASE},
    89  	VKILL:    &ccSetter{Character: syscall.VKILL},
    90  	VEOF:     &ccSetter{Character: syscall.VEOF},
    91  	VEOL:     &ccSetter{Character: syscall.VEOL},
    92  	VEOL2:    &ccSetter{Character: syscall.VEOL2},
    93  	VSTART:   &ccSetter{Character: syscall.VSTART},
    94  	VSTOP:    &ccSetter{Character: syscall.VSTOP},
    95  	VSUSP:    &ccSetter{Character: syscall.VSUSP},
    96  	VDSUSP:   &nopSetter{},
    97  	VREPRINT: &ccSetter{Character: syscall.VREPRINT},
    98  	VWERASE:  &ccSetter{Character: syscall.VWERASE},
    99  	VLNEXT:   &ccSetter{Character: syscall.VLNEXT},
   100  	VFLUSH:   &nopSetter{},
   101  	VSWTCH:   &nopSetter{},
   102  	VSTATUS:  &nopSetter{},
   103  	VDISCARD: &ccSetter{Character: syscall.VDISCARD},
   104  
   105  	// Input modes
   106  	IGNPAR:  &iflagSetter{Flag: syscall.IGNPAR},
   107  	PARMRK:  &iflagSetter{Flag: syscall.PARMRK},
   108  	INPCK:   &iflagSetter{Flag: syscall.INPCK},
   109  	ISTRIP:  &iflagSetter{Flag: syscall.ISTRIP},
   110  	INLCR:   &iflagSetter{Flag: syscall.INLCR},
   111  	IGNCR:   &iflagSetter{Flag: syscall.IGNCR},
   112  	ICRNL:   &iflagSetter{Flag: syscall.ICRNL},
   113  	IUCLC:   &nopSetter{},
   114  	IXON:    &iflagSetter{Flag: syscall.IXON},
   115  	IXANY:   &iflagSetter{Flag: syscall.IXANY},
   116  	IXOFF:   &iflagSetter{Flag: syscall.IXOFF},
   117  	IMAXBEL: &iflagSetter{Flag: syscall.IMAXBEL},
   118  	IUTF8:   &iflagSetter{Flag: syscall.IUTF8}, //IUTF8
   119  
   120  	// Local modes
   121  	ISIG:    &lflagSetter{Flag: syscall.ISIG},
   122  	ICANON:  &lflagSetter{Flag: syscall.ICANON},
   123  	XCASE:   &nopSetter{},
   124  	ECHO:    &lflagSetter{Flag: syscall.ECHO},
   125  	ECHOE:   &lflagSetter{Flag: syscall.ECHOE},
   126  	ECHOK:   &lflagSetter{Flag: syscall.ECHOK},
   127  	ECHONL:  &lflagSetter{Flag: syscall.ECHONL},
   128  	NOFLSH:  &lflagSetter{Flag: syscall.NOFLSH},
   129  	TOSTOP:  &lflagSetter{Flag: syscall.TOSTOP},
   130  	IEXTEN:  &lflagSetter{Flag: syscall.IEXTEN},
   131  	ECHOCTL: &lflagSetter{Flag: syscall.ECHOCTL},
   132  	ECHOKE:  &lflagSetter{Flag: syscall.ECHOKE},
   133  	PENDIN:  &lflagSetter{Flag: syscall.PENDIN},
   134  
   135  	// Output modes
   136  	OPOST:  &oflagSetter{Flag: syscall.OPOST},
   137  	OLCUC:  &nopSetter{},
   138  	ONLCR:  &oflagSetter{Flag: syscall.ONLCR},
   139  	OCRNL:  &oflagSetter{Flag: syscall.OCRNL},
   140  	ONOCR:  &oflagSetter{Flag: syscall.ONOCR},
   141  	ONLRET: &oflagSetter{Flag: syscall.ONLRET},
   142  
   143  	// Control modes
   144  	CS7:    &cflagSetter{Flag: syscall.CS7},
   145  	CS8:    &cflagSetter{Flag: syscall.CS8},
   146  	PARENB: &cflagSetter{Flag: syscall.PARENB},
   147  	PARODD: &cflagSetter{Flag: syscall.PARODD},
   148  
   149  	// Baud rates (ignore)
   150  	TTY_OP_ISPEED: &nopSetter{},
   151  	TTY_OP_OSPEED: &nopSetter{},
   152  }
   153  
   154  type nopSetter struct{}
   155  
   156  type ccSetter struct {
   157  	Character uint8
   158  }
   159  
   160  func (cc *ccSetter) Set(pty *os.File, termios *syscall.Termios, value uint32) error {
   161  	termios.Cc[cc.Character] = byte(value)
   162  	return SetAttr(pty, termios)
   163  }
   164  
   165  func (i *iflagSetter) Set(pty *os.File, termios *syscall.Termios, value uint32) error {
   166  	if value == 0 {
   167  		termios.Iflag &^= i.Flag
   168  	} else {
   169  		termios.Iflag |= i.Flag
   170  	}
   171  	return SetAttr(pty, termios)
   172  }
   173  
   174  func (l *lflagSetter) Set(pty *os.File, termios *syscall.Termios, value uint32) error {
   175  	if value == 0 {
   176  		termios.Lflag &^= l.Flag
   177  	} else {
   178  		termios.Lflag |= l.Flag
   179  	}
   180  	return SetAttr(pty, termios)
   181  }
   182  
   183  func (o *oflagSetter) Set(pty *os.File, termios *syscall.Termios, value uint32) error {
   184  	if value == 0 {
   185  		termios.Oflag &^= o.Flag
   186  	} else {
   187  		termios.Oflag |= o.Flag
   188  	}
   189  
   190  	return SetAttr(pty, termios)
   191  }
   192  
   193  func (c *cflagSetter) Set(pty *os.File, termios *syscall.Termios, value uint32) error {
   194  	switch c.Flag {
   195  	// CSIZE is a field
   196  	case syscall.CS7, syscall.CS8:
   197  		termios.Cflag &^= syscall.CSIZE
   198  		termios.Cflag |= c.Flag
   199  	default:
   200  		if value == 0 {
   201  			termios.Cflag &^= c.Flag
   202  		} else {
   203  			termios.Cflag |= c.Flag
   204  		}
   205  	}
   206  
   207  	return SetAttr(pty, termios)
   208  }
   209  
   210  func (n *nopSetter) Set(pty *os.File, termios *syscall.Termios, value uint32) error {
   211  	return nil
   212  }