github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/abi/linux/tty.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package linux
    16  
    17  const (
    18  	// NumControlCharacters is the number of control characters in Termios.
    19  	NumControlCharacters = 19
    20  	// disabledChar is used to indicate that a control character is
    21  	// disabled.
    22  	disabledChar = 0
    23  )
    24  
    25  // Winsize is struct winsize, defined in uapi/asm-generic/termios.h.
    26  //
    27  // +marshal
    28  type Winsize struct {
    29  	Row    uint16
    30  	Col    uint16
    31  	Xpixel uint16
    32  	Ypixel uint16
    33  }
    34  
    35  // Termios is struct termios, defined in uapi/asm-generic/termbits.h.
    36  //
    37  // +marshal
    38  type Termios struct {
    39  	InputFlags        uint32
    40  	OutputFlags       uint32
    41  	ControlFlags      uint32
    42  	LocalFlags        uint32
    43  	LineDiscipline    uint8
    44  	ControlCharacters [NumControlCharacters]uint8
    45  }
    46  
    47  // KernelTermios is struct ktermios/struct termios2, defined in
    48  // uapi/asm-generic/termbits.h.
    49  //
    50  // +stateify savable
    51  type KernelTermios struct {
    52  	InputFlags        uint32
    53  	OutputFlags       uint32
    54  	ControlFlags      uint32
    55  	LocalFlags        uint32
    56  	LineDiscipline    uint8
    57  	ControlCharacters [NumControlCharacters]uint8
    58  	InputSpeed        uint32
    59  	OutputSpeed       uint32
    60  }
    61  
    62  // IEnabled returns whether flag is enabled in termios input flags.
    63  func (t *KernelTermios) IEnabled(flag uint32) bool {
    64  	return t.InputFlags&flag == flag
    65  }
    66  
    67  // OEnabled returns whether flag is enabled in termios output flags.
    68  func (t *KernelTermios) OEnabled(flag uint32) bool {
    69  	return t.OutputFlags&flag == flag
    70  }
    71  
    72  // CEnabled returns whether flag is enabled in termios control flags.
    73  func (t *KernelTermios) CEnabled(flag uint32) bool {
    74  	return t.ControlFlags&flag == flag
    75  }
    76  
    77  // LEnabled returns whether flag is enabled in termios local flags.
    78  func (t *KernelTermios) LEnabled(flag uint32) bool {
    79  	return t.LocalFlags&flag == flag
    80  }
    81  
    82  // ToTermios copies fields that are shared with Termios into a new Termios
    83  // struct.
    84  func (t *KernelTermios) ToTermios() Termios {
    85  	return Termios{
    86  		InputFlags:        t.InputFlags,
    87  		OutputFlags:       t.OutputFlags,
    88  		ControlFlags:      t.ControlFlags,
    89  		LocalFlags:        t.LocalFlags,
    90  		LineDiscipline:    t.LineDiscipline,
    91  		ControlCharacters: t.ControlCharacters,
    92  	}
    93  }
    94  
    95  // FromTermios copies fields that are shared with Termios into this
    96  // KernelTermios struct.
    97  func (t *KernelTermios) FromTermios(term Termios) {
    98  	t.InputFlags = term.InputFlags
    99  	t.OutputFlags = term.OutputFlags
   100  	t.ControlFlags = term.ControlFlags
   101  	t.LocalFlags = term.LocalFlags
   102  	t.LineDiscipline = term.LineDiscipline
   103  	t.ControlCharacters = term.ControlCharacters
   104  }
   105  
   106  // IsTerminating returns whether c is a line terminating character.
   107  func (t *KernelTermios) IsTerminating(cBytes []byte) bool {
   108  	// All terminating characters are 1 byte.
   109  	if len(cBytes) != 1 {
   110  		return false
   111  	}
   112  	c := cBytes[0]
   113  
   114  	// Is this the user-set EOF character?
   115  	if t.IsEOF(c) {
   116  		return true
   117  	}
   118  
   119  	switch c {
   120  	case disabledChar:
   121  		return false
   122  	case '\n', t.ControlCharacters[VEOL]:
   123  		return true
   124  	case t.ControlCharacters[VEOL2]:
   125  		return t.LEnabled(IEXTEN)
   126  	}
   127  	return false
   128  }
   129  
   130  // IsEOF returns whether c is the EOF character.
   131  func (t *KernelTermios) IsEOF(c byte) bool {
   132  	return c == t.ControlCharacters[VEOF] && t.ControlCharacters[VEOF] != disabledChar
   133  }
   134  
   135  // Input flags.
   136  const (
   137  	IGNBRK  = 0000001
   138  	BRKINT  = 0000002
   139  	IGNPAR  = 0000004
   140  	PARMRK  = 0000010
   141  	INPCK   = 0000020
   142  	ISTRIP  = 0000040
   143  	INLCR   = 0000100
   144  	IGNCR   = 0000200
   145  	ICRNL   = 0000400
   146  	IUCLC   = 0001000
   147  	IXON    = 0002000
   148  	IXANY   = 0004000
   149  	IXOFF   = 0010000
   150  	IMAXBEL = 0020000
   151  	IUTF8   = 0040000
   152  )
   153  
   154  // Output flags.
   155  const (
   156  	OPOST  = 0000001
   157  	OLCUC  = 0000002
   158  	ONLCR  = 0000004
   159  	OCRNL  = 0000010
   160  	ONOCR  = 0000020
   161  	ONLRET = 0000040
   162  	OFILL  = 0000100
   163  	OFDEL  = 0000200
   164  	NLDLY  = 0000400
   165  	NL0    = 0000000
   166  	NL1    = 0000400
   167  	CRDLY  = 0003000
   168  	CR0    = 0000000
   169  	CR1    = 0001000
   170  	CR2    = 0002000
   171  	CR3    = 0003000
   172  	TABDLY = 0014000
   173  	TAB0   = 0000000
   174  	TAB1   = 0004000
   175  	TAB2   = 0010000
   176  	TAB3   = 0014000
   177  	XTABS  = 0014000
   178  	BSDLY  = 0020000
   179  	BS0    = 0000000
   180  	BS1    = 0020000
   181  	VTDLY  = 0040000
   182  	VT0    = 0000000
   183  	VT1    = 0040000
   184  	FFDLY  = 0100000
   185  	FF0    = 0000000
   186  	FF1    = 0100000
   187  )
   188  
   189  // Control flags.
   190  const (
   191  	CBAUD    = 0010017
   192  	B0       = 0000000
   193  	B50      = 0000001
   194  	B75      = 0000002
   195  	B110     = 0000003
   196  	B134     = 0000004
   197  	B150     = 0000005
   198  	B200     = 0000006
   199  	B300     = 0000007
   200  	B600     = 0000010
   201  	B1200    = 0000011
   202  	B1800    = 0000012
   203  	B2400    = 0000013
   204  	B4800    = 0000014
   205  	B9600    = 0000015
   206  	B19200   = 0000016
   207  	B38400   = 0000017
   208  	EXTA     = B19200
   209  	EXTB     = B38400
   210  	CSIZE    = 0000060
   211  	CS5      = 0000000
   212  	CS6      = 0000020
   213  	CS7      = 0000040
   214  	CS8      = 0000060
   215  	CSTOPB   = 0000100
   216  	CREAD    = 0000200
   217  	PARENB   = 0000400
   218  	PARODD   = 0001000
   219  	HUPCL    = 0002000
   220  	CLOCAL   = 0004000
   221  	CBAUDEX  = 0010000
   222  	BOTHER   = 0010000
   223  	B57600   = 0010001
   224  	B115200  = 0010002
   225  	B230400  = 0010003
   226  	B460800  = 0010004
   227  	B500000  = 0010005
   228  	B576000  = 0010006
   229  	B921600  = 0010007
   230  	B1000000 = 0010010
   231  	B1152000 = 0010011
   232  	B1500000 = 0010012
   233  	B2000000 = 0010013
   234  	B2500000 = 0010014
   235  	B3000000 = 0010015
   236  	B3500000 = 0010016
   237  	B4000000 = 0010017
   238  	CIBAUD   = 002003600000
   239  	CMSPAR   = 010000000000
   240  	CRTSCTS  = 020000000000
   241  
   242  	// IBSHIFT is the shift from CBAUD to CIBAUD.
   243  	IBSHIFT = 16
   244  )
   245  
   246  // Local flags.
   247  const (
   248  	ISIG    = 0000001
   249  	ICANON  = 0000002
   250  	XCASE   = 0000004
   251  	ECHO    = 0000010
   252  	ECHOE   = 0000020
   253  	ECHOK   = 0000040
   254  	ECHONL  = 0000100
   255  	NOFLSH  = 0000200
   256  	TOSTOP  = 0000400
   257  	ECHOCTL = 0001000
   258  	ECHOPRT = 0002000
   259  	ECHOKE  = 0004000
   260  	FLUSHO  = 0010000
   261  	PENDIN  = 0040000
   262  	IEXTEN  = 0100000
   263  	EXTPROC = 0200000
   264  )
   265  
   266  // Control Character indices.
   267  const (
   268  	VINTR    = 0
   269  	VQUIT    = 1
   270  	VERASE   = 2
   271  	VKILL    = 3
   272  	VEOF     = 4
   273  	VTIME    = 5
   274  	VMIN     = 6
   275  	VSWTC    = 7
   276  	VSTART   = 8
   277  	VSTOP    = 9
   278  	VSUSP    = 10
   279  	VEOL     = 11
   280  	VREPRINT = 12
   281  	VDISCARD = 13
   282  	VWERASE  = 14
   283  	VLNEXT   = 15
   284  	VEOL2    = 16
   285  )
   286  
   287  // ControlCharacter returns the termios-style control character for the passed
   288  // character.
   289  //
   290  // e.g., for Ctrl-C, i.e., ^C, call ControlCharacter('C').
   291  //
   292  // Standard control characters are ASCII bytes 0 through 31.
   293  func ControlCharacter(c byte) uint8 {
   294  	// A is 1, B is 2, etc.
   295  	return uint8(c - 'A' + 1)
   296  }
   297  
   298  // DefaultControlCharacters is the default set of Termios control characters.
   299  var DefaultControlCharacters = [NumControlCharacters]uint8{
   300  	ControlCharacter('C'),  // VINTR = ^C
   301  	ControlCharacter('\\'), // VQUIT = ^\
   302  	'\x7f',                 // VERASE = DEL
   303  	ControlCharacter('U'),  // VKILL = ^U
   304  	ControlCharacter('D'),  // VEOF = ^D
   305  	0,                      // VTIME
   306  	1,                      // VMIN
   307  	0,                      // VSWTC
   308  	ControlCharacter('Q'),  // VSTART = ^Q
   309  	ControlCharacter('S'),  // VSTOP = ^S
   310  	ControlCharacter('Z'),  // VSUSP = ^Z
   311  	0,                      // VEOL
   312  	ControlCharacter('R'),  // VREPRINT = ^R
   313  	ControlCharacter('O'),  // VDISCARD = ^O
   314  	ControlCharacter('W'),  // VWERASE = ^W
   315  	ControlCharacter('V'),  // VLNEXT = ^V
   316  	0,                      // VEOL2
   317  }
   318  
   319  // MasterTermios is the terminal configuration of the master end of a Unix98
   320  // pseudoterminal.
   321  var MasterTermios = KernelTermios{
   322  	ControlFlags:      B38400 | CS8 | CREAD,
   323  	ControlCharacters: DefaultControlCharacters,
   324  	InputSpeed:        38400,
   325  	OutputSpeed:       38400,
   326  }
   327  
   328  // DefaultReplicaTermios is the default terminal configuration of the replica
   329  // end of a Unix98 pseudoterminal.
   330  var DefaultReplicaTermios = KernelTermios{
   331  	InputFlags:        ICRNL | IXON,
   332  	OutputFlags:       OPOST | ONLCR,
   333  	ControlFlags:      B38400 | CS8 | CREAD,
   334  	LocalFlags:        ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN,
   335  	ControlCharacters: DefaultControlCharacters,
   336  	InputSpeed:        38400,
   337  	OutputSpeed:       38400,
   338  }
   339  
   340  // WindowSize corresponds to struct winsize defined in
   341  // include/uapi/asm-generic/termios.h.
   342  //
   343  // +stateify savable
   344  // +marshal
   345  type WindowSize struct {
   346  	Rows uint16
   347  	Cols uint16
   348  	_    [4]byte // Padding for 2 unused shorts.
   349  }