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