File: ssh.go

package info (click to toggle)
golang-github-google-goterm 0.0~git20200907.555d40f-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 184 kB
  • sloc: makefile: 4
file content (203 lines) | stat: -rw-r--r-- 5,754 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.


package term

const (
	// Terminal attribute types.
	sshIflag = iota
	sshOflag
	sshCflag
	sshLflag
	sshCchar
	sshTspeed
	sshNOP

	// SSH terminal attributes.
	sshTTYOPEND    = 0
	sshVINTR       = 1
	sshVQUIT       = 2
	sshVERASE      = 3
	sshVKILL       = 4
	sshVEOF        = 5
	sshVEOL        = 6
	sshVEOL2       = 7
	sshVSTART      = 8
	sshVSTOP       = 9
	sshVSUSP       = 10
	sshVDSUSP      = 11
	sshVREPRINT    = 12
	sshVWERASE     = 13
	sshVLNEXT      = 14
	sshVFLUSH      = 15
	sshVSWTCH      = 16
	sshVSTATUS     = 17
	sshVDISCARD    = 18
	sshIGNPAR      = 30
	sshPARMRK      = 31
	sshINPCK       = 32
	sshISTRIP      = 33
	sshINLCR       = 34
	sshIGNCR       = 35
	sshICRNL       = 36
	sshIUCLC       = 37
	sshIXON        = 38
	sshIXANY       = 39
	sshIXOFF       = 40
	sshIMAXBEL     = 41
	sshISIG        = 50
	sshICANON      = 51
	sshXCASE       = 52
	sshECHO        = 53
	sshECHOE       = 54
	sshECHOK       = 55
	sshECHONL      = 56
	sshNOFLSH      = 57
	sshTOSTOP      = 58
	sshIEXTEN      = 59
	sshECHOCTL     = 60
	sshECHOKE      = 61
	sshPENDIN      = 62
	sshOPOST       = 70
	sshOLCUC       = 71
	sshONLCR       = 72
	sshOCRNL       = 73
	sshONOCR       = 74
	sshONLRET      = 75
	sshCS7         = 90
	sshCS8         = 91
	sshPARENB      = 92
	sshPARODD      = 93
	sshTTYOPISPEED = 128
	sshTTYOPOSPEED = 129
)

var convertSSH = map[uint8]struct {
	tType  uint
	native uint32
}{
	sshTTYOPEND:    {tType: sshNOP},
	sshVINTR:       {tType: sshCchar, native: VINTR},
	sshVQUIT:       {tType: sshCchar, native: VQUIT},
	sshVERASE:      {tType: sshCchar, native: VERASE},
	sshVKILL:       {tType: sshCchar, native: VKILL},
	sshVEOF:        {tType: sshCchar, native: VEOF},
	sshVEOL:        {tType: sshCchar, native: VEOL},
	sshVEOL2:       {tType: sshCchar, native: VEOL2},
	sshVSTART:      {tType: sshCchar, native: VSTART},
	sshVSTOP:       {tType: sshCchar, native: VSTOP},
	sshVSUSP:       {tType: sshCchar, native: VSUSP},
	sshVDSUSP:      {tType: sshCchar, native: sshNOP},
	sshVREPRINT:    {tType: sshCchar, native: VREPRINT},
	sshVWERASE:     {tType: sshCchar, native: VWERASE},
	sshVLNEXT:      {tType: sshCchar, native: VLNEXT},
	sshVFLUSH:      {tType: sshNOP},
	sshVSWTCH:      {tType: sshCchar, native: VSWTC},
	sshVSTATUS:     {tType: sshNOP},
	sshVDISCARD:    {tType: sshCchar, native: VDISCARD},
	sshIGNPAR:      {tType: sshIflag, native: IGNPAR},
	sshPARMRK:      {tType: sshIflag, native: PARMRK},
	sshINPCK:       {tType: sshIflag, native: INPCK},
	sshISTRIP:      {tType: sshIflag, native: ISTRIP},
	sshINLCR:       {tType: sshIflag, native: INLCR},
	sshIGNCR:       {tType: sshIflag, native: IGNCR},
	sshICRNL:       {tType: sshIflag, native: ICRNL},
	sshIUCLC:       {tType: sshIflag, native: IUCLC},
	sshIXON:        {tType: sshIflag, native: IXON},
	sshIXANY:       {tType: sshIflag, native: IXANY},
	sshIXOFF:       {tType: sshIflag, native: IXOFF},
	sshIMAXBEL:     {tType: sshIflag, native: IMAXBEL},
	sshISIG:        {tType: sshLflag, native: ISIG},
	sshICANON:      {tType: sshLflag, native: ICANON},
	sshXCASE:       {tType: sshLflag, native: XCASE},
	sshECHO:        {tType: sshLflag, native: ECHO},
	sshECHOE:       {tType: sshLflag, native: ECHOE},
	sshECHOK:       {tType: sshLflag, native: ECHOK},
	sshECHONL:      {tType: sshLflag, native: ECHONL},
	sshNOFLSH:      {tType: sshLflag, native: NOFLSH},
	sshTOSTOP:      {tType: sshLflag, native: TOSTOP},
	sshIEXTEN:      {tType: sshLflag, native: IEXTEN},
	sshECHOCTL:     {tType: sshLflag, native: ECHOCTL},
	sshECHOKE:      {tType: sshLflag, native: ECHOKE},
	sshPENDIN:      {tType: sshNOP},
	sshOPOST:       {tType: sshOflag, native: OPOST},
	sshOLCUC:       {tType: sshOflag, native: OLCUC},
	sshONLCR:       {tType: sshOflag, native: ONLCR},
	sshOCRNL:       {tType: sshOflag, native: OCRNL},
	sshONOCR:       {tType: sshOflag, native: ONOCR},
	sshONLRET:      {tType: sshOflag, native: ONLRET},
	sshCS7:         {tType: sshCflag, native: CS7},
	sshCS8:         {tType: sshCflag, native: CS8},
	sshPARENB:      {tType: sshCflag, native: PARENB},
	sshPARODD:      {tType: sshCflag, native: PARODD},
	sshTTYOPISPEED: {tType: sshTspeed},
	sshTTYOPOSPEED: {tType: sshTspeed},
}

// ToSSH converts the Termios attributes to SSH attributes usable as ssh.TerminalModes.
func (t *Termios) ToSSH() map[uint8]uint32 {
	sshModes := make(map[uint8]uint32, len(convertSSH))
	var flags uint32
	for sshID, tios := range convertSSH {
		switch tios.tType {
		case sshIflag:
			flags = t.Iflag
		case sshOflag:
			flags = t.Oflag
		case sshLflag:
			flags = t.Lflag
		case sshCflag:
			flags = t.Cflag
		case sshCchar:
			sshModes[sshID] = uint32(t.Cc[tios.native])
			continue
		case sshTspeed:
			sshModes[sshTTYOPISPEED], sshModes[sshTTYOPOSPEED] = t.Ispeed, t.Ospeed
			continue
		default:
			continue
		}
		var onOff uint32
		if tios.native&flags > 0 {
			onOff = 1
		}
		sshModes[sshID] = onOff
	}
	return sshModes
}

// FromSSH converts SSH attributes to Termios attributes.
func (t *Termios) FromSSH(termModes map[uint8]uint32) {
	var flags *uint32
	for sshID, val := range termModes {
		switch convertSSH[sshID].tType {
		case sshIflag:
			flags = &t.Iflag
		case sshOflag:
			flags = &t.Oflag
		case sshLflag:
			flags = &t.Lflag
		case sshCflag:
			flags = &t.Cflag
		case sshCchar:
			t.Cc[convertSSH[sshID].native] = byte(val)
			continue
		case sshTspeed:
			if sshID == sshTTYOPISPEED {
				t.Ispeed = val
			} else {
				t.Ospeed = val
			}
			continue
		default:
			continue
		}
		if val > 0 {
			*flags |= convertSSH[sshID].native
		} else {
			*flags &^= convertSSH[sshID].native
		}
	}
}