author | Claus Gittinger <cg@exept.de> |
Thu, 09 Jun 2016 18:09:48 +0200 | |
changeset 3930 | dc306d478e63 |
parent 3906 | 33485a09adff |
child 3990 | a54abbfabec0 |
permissions | -rw-r--r-- |
667 | 1 |
" |
2 |
COPYRIGHT (c) 1998 by eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
3906 | 12 |
"{ Package: 'stx:libbasic2' }" |
667 | 13 |
|
3906 | 14 |
"{ NameSpace: Smalltalk }" |
667 | 15 |
|
16 |
PipeStream subclass:#UnixPTYStream |
|
17 |
instanceVariableNames:'' |
|
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'OS-Unix' |
|
21 |
! |
|
22 |
||
23 |
!UnixPTYStream class methodsFor:'documentation'! |
|
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1998 by eXept Software AG |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
||
38 |
! |
|
39 |
||
40 |
documentation |
|
41 |
" |
|
42 |
These are much like PipeStreams, but allow bi-directional communication |
|
43 |
with a Unix command. (i.e. everything written to the PTYStream is seen |
|
44 |
by the commands standard-input, everything written by the command to its |
|
45 |
stdErr or stdOut can be read from me. |
|
46 |
||
47 |
In addition, sending control characters (such as INTR or QUIT), |
|
48 |
will be handled by the command as a signal (unless the command changed |
|
49 |
its standard input to raw mode). |
|
50 |
||
51 |
[author:] |
|
52 |
Claus Gittinger |
|
53 |
||
54 |
[see also:] |
|
55 |
TerminalView |
|
56 |
PipeStream ExternalStream FileStream Socket |
|
57 |
OperatingSystem |
|
58 |
" |
|
59 |
||
60 |
! |
|
61 |
||
62 |
examples |
|
63 |
" |
|
64 |
that one is not special (could be done with a PipeStream): |
|
65 |
[exBegin] |
|
66 |
|pty| |
|
67 |
||
68 |
pty := UnixPTYStream to:'ls -l'. |
|
69 |
[pty atEnd] whileFalse:[ |
|
70 |
Transcript showCR:(pty nextLine). |
|
71 |
]. |
|
72 |
pty close. |
|
73 |
[exEnd] |
|
668 | 74 |
|
75 |
||
76 |
prove (done with a PipeStream): |
|
667 | 77 |
[exBegin] |
78 |
|pty| |
|
79 |
||
80 |
pty := PipeStream readingFrom:'ls -l'. |
|
81 |
[pty atEnd] whileFalse:[ |
|
82 |
Transcript showCR:(pty nextLine). |
|
83 |
]. |
|
84 |
pty close. |
|
85 |
[exEnd] |
|
668 | 86 |
|
667 | 87 |
|
668 | 88 |
but that one is not possible with a PipeStream |
89 |
(simulating an editor session): |
|
667 | 90 |
[exBegin] |
91 |
|pty| |
|
92 |
||
93 |
pty := UnixPTYStream to:'ed'. |
|
94 |
[ |
|
95 |
pty readWait. |
|
96 |
[pty atEnd] whileFalse:[ |
|
97 |
Transcript showCR:(pty nextLine). |
|
98 |
pty readWait. |
|
99 |
]. |
|
100 |
pty close. |
|
101 |
] forkAt:9. |
|
102 |
||
103 |
pty nextPutLine:'r Makefile'. |
|
104 |
pty nextPutLine:'1,2d'. |
|
105 |
pty nextPutLine:'$d'. |
|
106 |
pty nextPutLine:'w xxx'. |
|
107 |
pty nextPutLine:'q'. |
|
108 |
[exEnd] |
|
109 |
||
668 | 110 |
|
111 |
and that one is even better ... |
|
112 |
(simulating a login session): |
|
113 |
[exBegin] |
|
114 |
|pty password command| |
|
115 |
||
116 |
pty := UnixPTYStream to:'rlogin ' , OperatingSystem getHostName. |
|
117 |
[ |
|
118 |
pty readWait. |
|
119 |
[pty atEnd] whileFalse:[ |
|
120 |
Transcript show:(pty next). |
|
121 |
pty readWait. |
|
122 |
]. |
|
123 |
pty close. |
|
124 |
] forkAt:9. |
|
125 |
||
669 | 126 |
password := Dialog requestPassword:'password'. |
668 | 127 |
pty nextPutLine:password. |
128 |
command := Dialog request:'command'. |
|
129 |
pty nextPutLine:command. |
|
670 | 130 |
pty nextPutLine:'exit'. |
668 | 131 |
[exEnd] |
132 |
||
667 | 133 |
" |
134 |
! ! |
|
135 |
||
136 |
!UnixPTYStream class methodsFor:'instance creation'! |
|
137 |
||
138 |
to:commandString |
|
139 |
"create and return a new ptyStream which can read/write to the unix command |
|
140 |
given by commandString." |
|
141 |
||
142 |
^ (self basicNew) to:commandString |
|
143 |
||
144 |
"unix: |
|
145 |
UnixPTYStream to:'sh' |
|
146 |
" |
|
147 |
||
148 |
"Modified: / 9.7.1998 / 18:26:31 / cg" |
|
149 |
! ! |
|
150 |
||
151 |
!UnixPTYStream class methodsFor:'blocked instance creation'! |
|
152 |
||
153 |
readingFrom:commandString |
|
154 |
^ self shouldNotImplement |
|
155 |
||
156 |
"Created: / 9.7.1998 / 18:25:09 / cg" |
|
157 |
"Modified: / 9.7.1998 / 18:25:34 / cg" |
|
158 |
! |
|
159 |
||
160 |
readingFrom:commandString errorDisposition:handleError inDirectory:aDirectory |
|
161 |
^ self shouldNotImplement |
|
162 |
||
163 |
"Modified: / 9.7.1998 / 18:25:31 / cg" |
|
164 |
! |
|
165 |
||
166 |
readingFrom:commandString inDirectory:aDirectory |
|
167 |
^ self shouldNotImplement |
|
168 |
||
169 |
"Created: / 9.7.1998 / 18:25:38 / cg" |
|
170 |
! |
|
171 |
||
172 |
writingTo:commandString |
|
173 |
^ self shouldNotImplement |
|
174 |
||
175 |
"Created: / 9.7.1998 / 18:25:42 / cg" |
|
176 |
! |
|
177 |
||
178 |
writingTo:commandString inDirectory:aDirectory |
|
179 |
^ self shouldNotImplement |
|
180 |
||
181 |
"Created: / 9.7.1998 / 18:25:46 / cg" |
|
182 |
! ! |
|
183 |
||
184 |
!UnixPTYStream methodsFor:'private'! |
|
185 |
||
186 |
openPTYFor:aCommandString withMode:mode inDirectory:aDirectrory |
|
187 |
"open a pty to the unix command in commandString" |
|
188 |
||
189 |
|blocked ptyFdArray execFdArray slaveFd masterFd shellAndArgs |
|
1152
10db934b0b71
Cleanup closeDescriptor handling in exec:withArguments:...
Stefan Vogel <sv@exept.de>
parents:
687
diff
changeset
|
190 |
shellPath shellArgs mbx mbxName |
667 | 191 |
env shell args| |
192 |
||
3906 | 193 |
handle notNil ifTrue:[ |
667 | 194 |
"the pipe was already open ... |
195 |
this should (can) not happen." |
|
196 |
^ self errorOpen |
|
197 |
]. |
|
3906 | 198 |
|
667 | 199 |
lastErrorNumber := nil. |
200 |
exitStatus := nil. |
|
201 |
exitSema := Semaphore new name:'pty exitSema'. |
|
3906 | 202 |
|
687 | 203 |
OperatingSystem isVMSlike ifTrue:[ |
667 | 204 |
mbx := OperatingSystem createMailBox. |
205 |
mbx isNil ifTrue:[ |
|
206 |
lastErrorNumber := OperatingSystem currentErrorNumber. |
|
207 |
^ self openError |
|
208 |
]. |
|
209 |
mbxName := OperatingSystem mailBoxNameOf:mbx. |
|
210 |
"/ 'mailBox is ' print. mbx print. ' name is ' print. mbxName printCR. |
|
211 |
||
212 |
shellPath := ''. |
|
213 |
shellArgs := aCommandString. |
|
214 |
||
215 |
execFdArray := Array with:mbx with:mbx with:mbx. |
|
216 |
] ifFalse:[ |
|
217 |
ptyFdArray := OperatingSystem makePTYPair. |
|
218 |
ptyFdArray isNil ifTrue:[ |
|
219 |
lastErrorNumber := OperatingSystem currentErrorNumber. |
|
220 |
^ self openError |
|
221 |
]. |
|
222 |
||
223 |
shellAndArgs := OperatingSystem commandAndArgsForOSCommand:aCommandString. |
|
224 |
shellPath := shellAndArgs at:1. |
|
225 |
shellArgs := shellAndArgs at:2. |
|
226 |
||
227 |
masterFd := ptyFdArray at:1. |
|
228 |
slaveFd := ptyFdArray at:2. |
|
229 |
execFdArray := Array with:slaveFd with:slaveFd with:slaveFd. |
|
230 |
]. |
|
231 |
||
232 |
env := Dictionary new. |
|
233 |
env at:'TERM' put:'dumb'. |
|
234 |
env at:'SHELL' put:shellPath. |
|
235 |
||
236 |
"/ must block here, to avoid races due to early finishing |
|
237 |
"/ subprocesses ... |
|
238 |
||
239 |
blocked := OperatingSystem blockInterrupts. |
|
240 |
||
241 |
pid := Processor |
|
242 |
monitor:[ |
|
243 |
OperatingSystem |
|
244 |
exec:shellPath |
|
245 |
withArguments:shellArgs |
|
246 |
environment:env |
|
247 |
fileDescriptors:execFdArray |
|
248 |
fork:true |
|
249 |
newPgrp:true |
|
250 |
"/ inDirectory:aDirectrory. |
|
251 |
] |
|
252 |
action:[:status | |
|
253 |
status stillAlive ifFalse:[ |
|
254 |
exitStatus := status. |
|
255 |
OperatingSystem closePid:pid. |
|
256 |
pid := nil. |
|
257 |
exitSema signal. |
|
258 |
]. |
|
259 |
]. |
|
260 |
||
3906 | 261 |
(OperatingSystem isVMSlike) ifFalse:[ |
667 | 262 |
OperatingSystem closeFd:slaveFd. |
263 |
]. |
|
264 |
||
265 |
pid notNil ifTrue:[ |
|
3906 | 266 |
(OperatingSystem isMSWINDOWSlike) ifTrue:[ |
667 | 267 |
self setFileDescriptor:masterFd mode:mode. |
268 |
"/ self setFileHandle:masterFd mode:mode |
|
269 |
] ifFalse:[ |
|
3906 | 270 |
(OperatingSystem isVMSlike) ifTrue:[ |
667 | 271 |
"/ |
272 |
"/ reopen the mailbox as a file ... |
|
273 |
"/ |
|
274 |
mbxName := OperatingSystem mailBoxNameOf:mbx. |
|
275 |
mbxName notNil ifTrue:[ |
|
276 |
super open:mbxName withMode:mode |
|
277 |
]. |
|
278 |
] ifFalse:[ |
|
279 |
self setFileDescriptor:masterFd mode:mode. |
|
280 |
] |
|
281 |
] |
|
282 |
] ifFalse:[ |
|
283 |
lastErrorNumber := OperatingSystem currentErrorNumber. |
|
3906 | 284 |
OperatingSystem isVMSlike ifTrue:[ |
667 | 285 |
OperatingSystem destroyMailBox:mbx |
286 |
] ifFalse:[ |
|
287 |
OperatingSystem closeFd:masterFd. |
|
288 |
]. |
|
289 |
]. |
|
290 |
||
291 |
blocked ifFalse:[ |
|
292 |
OperatingSystem unblockInterrupts |
|
293 |
]. |
|
294 |
||
295 |
lastErrorNumber notNil ifTrue:[ |
|
296 |
" |
|
297 |
the pipe open failed for some reason ... |
|
298 |
... this may be either due to an invalid command string, |
|
299 |
or due to the system running out of memory (when forking |
|
300 |
the unix process) |
|
301 |
" |
|
302 |
^ self openError |
|
303 |
]. |
|
304 |
||
305 |
commandString := aCommandString. |
|
306 |
buffered := false. |
|
307 |
||
308 |
position := 1. |
|
309 |
hitEOF := false. |
|
310 |
binary := false. |
|
311 |
Lobby register:self. |
|
312 |
||
313 |
"Created: / 9.7.1998 / 20:21:42 / cg" |
|
314 |
"Modified: / 9.7.1998 / 20:28:31 / cg" |
|
315 |
! |
|
316 |
||
317 |
to:command |
|
318 |
"setup the receiver to read/write to command" |
|
319 |
||
320 |
mode := #readwrite. didWrite := true. |
|
321 |
^ self openPTYFor:command withMode:ReadWriteMode inDirectory:nil |
|
322 |
||
323 |
"Created: / 9.7.1998 / 18:27:40 / cg" |
|
324 |
"Modified: / 9.7.1998 / 20:22:39 / cg" |
|
325 |
! ! |
|
326 |
||
327 |
!UnixPTYStream methodsFor:'testing'! |
|
328 |
||
329 |
atEnd |
|
330 |
ReadErrorSignal handle:[:ex | |
|
331 |
ex return |
|
332 |
] do:[ |
|
333 |
^ super atEnd. |
|
334 |
]. |
|
335 |
^ true |
|
336 |
||
337 |
"Created: / 9.7.1998 / 20:29:03 / cg" |
|
338 |
"Modified: / 9.7.1998 / 20:29:48 / cg" |
|
339 |
! ! |
|
340 |
||
341 |
!UnixPTYStream class methodsFor:'documentation'! |
|
342 |
||
343 |
version |
|
3906 | 344 |
^ '$Header$' |
667 | 345 |
! ! |
3906 | 346 |