author | Claus Gittinger <cg@exept.de> |
Fri, 28 Jun 2019 08:49:56 +0200 | |
changeset 24385 | dd84e4dfb5cc |
parent 23901 | f6e98ee5be18 |
child 24602 | cf345afadfb0 |
permissions | -rw-r--r-- |
23901 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1 | 3 |
" |
5 | 4 |
COPYRIGHT (c) 1989 by Claus Gittinger |
177 | 5 |
All Rights Reserved |
1 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
5407 | 14 |
"{ Package: 'stx:libbasic' }" |
15 |
||
17607
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
16 |
"{ NameSpace: Smalltalk }" |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
17 |
|
1344
32a51164b237
No longer a subclass of UnboundedExternalStream, now subclass of ExternalStream.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
18 |
ExternalStream subclass:#NonPositionableExternalStream |
1295 | 19 |
instanceVariableNames:'' |
21702 | 20 |
classVariableNames:'StdErrorStream StdInStream StdOutStream' |
1295 | 21 |
poolDictionaries:'' |
22 |
category:'Streams-External' |
|
1 | 23 |
! |
24 |
||
613 | 25 |
!NonPositionableExternalStream primitiveDefinitions! |
26 |
%{ |
|
2896 | 27 |
|
5918 | 28 |
#ifndef _STDIO_H_INCLUDED_ |
29 |
# include <stdio.h> |
|
30 |
# define _STDIO_H_INCLUDED_ |
|
31 |
#endif |
|
3956
51f1a9a4d63f
changes for egcs (stdio uses __new / utsname)
Claus Gittinger <cg@exept.de>
parents:
3808
diff
changeset
|
32 |
|
10410 | 33 |
#ifndef REMOVE_LATER |
34 |
# define __win32_stdout() stdout |
|
35 |
# define __win32_stderr() stderr |
|
36 |
# define __win32_stdin() stdin |
|
37 |
#else |
|
38 |
extern FILE *__win32_stdin(); |
|
39 |
extern FILE *__win32_stderr(); |
|
40 |
extern FILE *__win32_stdout(); |
|
41 |
#endif |
|
42 |
||
613 | 43 |
%} |
44 |
||
45 |
! ! |
|
46 |
||
88 | 47 |
!NonPositionableExternalStream class methodsFor:'documentation'! |
48 |
||
49 |
copyright |
|
50 |
" |
|
51 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
177 | 52 |
All Rights Reserved |
88 | 53 |
|
54 |
This software is furnished under a license and may be used |
|
55 |
only in accordance with the terms of that license and with the |
|
56 |
inclusion of the above copyright notice. This software may not |
|
57 |
be provided or otherwise made available to, or used by, any |
|
58 |
other person. No title to or ownership of the software is |
|
59 |
hereby transferred. |
|
60 |
" |
|
61 |
! |
|
62 |
||
63 |
documentation |
|
64 |
" |
|
65 |
This class provides common protocol for all non-positionable, |
|
66 |
external streams. Concrete subclasses are terminal streams, pipe streams, |
|
67 |
PrinterStreams, Sockets etc. |
|
68 |
||
69 |
There are three special instances of this class, representing stdin, |
|
70 |
stdout and stderr of the smalltalk/X process (see Unix manuals, if you |
|
20806 | 71 |
don't know what those are used for). These special streams are bound to |
177 | 72 |
to globals Stdin, Stdout and Stderr at early initialization time |
73 |
(see Smalltalk>>initializeStandardStreams). |
|
159 | 74 |
|
75 |
The name of this class is a historical leftover - it should be called |
|
76 |
'TTYStream' or similar. |
|
1295 | 77 |
|
78 |
[author:] |
|
20806 | 79 |
Claus Gittinger |
88 | 80 |
" |
216 | 81 |
! ! |
82 |
||
1 | 83 |
!NonPositionableExternalStream class methodsFor:'instance creation'! |
84 |
||
613 | 85 |
forStderr |
2965 | 86 |
"{ Pragma: +optSpace }" |
87 |
||
613 | 88 |
"return a NonPositionableExternalStream object for writing to |
89 |
Unixes standard error output file descriptor" |
|
90 |
||
91 |
StdErrorStream isNil ifTrue:[ |
|
92 |
StdErrorStream := self basicNew initializeForStderr |
|
93 |
]. |
|
94 |
^ StdErrorStream |
|
95 |
! |
|
96 |
||
1 | 97 |
forStdin |
2965 | 98 |
"{ Pragma: +optSpace }" |
99 |
||
44 | 100 |
"return a NonPositionableExternalStream object for reading from |
101 |
Unixes standard input file descriptor" |
|
102 |
||
88 | 103 |
StdInStream isNil ifTrue:[ |
177 | 104 |
StdInStream := self basicNew initializeForStdin |
88 | 105 |
]. |
106 |
^ StdInStream |
|
1 | 107 |
! |
108 |
||
109 |
forStdout |
|
2965 | 110 |
"{ Pragma: +optSpace }" |
111 |
||
44 | 112 |
"return a NonPositionableExternalStream object for writing to |
113 |
Unixes standard output file descriptor" |
|
114 |
||
88 | 115 |
StdOutStream isNil ifTrue:[ |
177 | 116 |
StdOutStream := self basicNew initializeForStdout |
88 | 117 |
]. |
118 |
^ StdOutStream |
|
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
119 |
! |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
120 |
|
21207 | 121 |
makeBidirectionalPipe |
122 |
"return an array with two streams - the first one for reading, |
|
123 |
the second for writing. |
|
124 |
This is the higher level equivalent of OperatingSystem>>makeBidirectionalPipe |
|
125 |
(which returns an array of file-descriptors)." |
|
126 |
||
127 |
|pipe rs ws| |
|
128 |
||
23503 | 129 |
"/ must protect from being unwound, otherwise the pipe-FD might not be registered for finalization |
130 |
[ |
|
21207 | 131 |
pipe := OperatingSystem makeBidirectionalPipe. |
23503 | 132 |
pipe isNil ifTrue:[ |
133 |
"/ ok, maybe someone has forgotten to close a stream; enforce finalization and try again |
|
134 |
'makePipe: enforcing finalization to close any open streams' infoPrintCR. |
|
135 |
ObjectMemory garbageCollect; finalize. |
|
136 |
pipe := OperatingSystem makeBidirectionalPipe. |
|
137 |
pipe isNil ifTrue:[ |
|
138 |
|errorNumber errorHolder| |
|
21207 | 139 |
|
23503 | 140 |
errorNumber := OperatingSystem lastErrorNumber. |
141 |
errorHolder := errorNumber. |
|
142 |
OpenError newException |
|
143 |
errorCode:errorNumber; |
|
144 |
osErrorHolder:errorHolder; |
|
145 |
raise. |
|
146 |
]. |
|
147 |
]. |
|
21207 | 148 |
|
23503 | 149 |
rs := PipeStream forFileDescriptor:(pipe at:1) mode:#readWrite buffered:false handleType:#pipeFilePointer. |
150 |
ws := PipeStream forFileDescriptor:(pipe at:2) mode:#readWrite buffered:false handleType:#pipeFilePointer. |
|
151 |
] valueUninterruptably. |
|
21207 | 152 |
^ Array with:rs with:ws |
153 |
||
154 |
" |
|
155 |
|pipe rs ws| |
|
156 |
||
157 |
pipe := NonPositionableExternalStream makeBidirectionalPipe. |
|
158 |
rs := pipe at:1. |
|
159 |
ws := pipe at:2. |
|
160 |
||
161 |
'read ...'. |
|
162 |
[ |
|
163 |
1 to:10 do:[:i | |
|
164 |
Transcript showCR:rs nextLine |
|
165 |
]. |
|
166 |
rs close. |
|
167 |
] forkAt:7. |
|
168 |
||
169 |
'write ...'. |
|
170 |
[ |
|
171 |
1 to:10 do:[:i | |
|
172 |
ws nextPutAll:'hello world '; nextPutAll:i printString; cr |
|
173 |
]. |
|
174 |
ws close. |
|
175 |
] fork. |
|
176 |
" |
|
23485 | 177 |
|
23503 | 178 |
"Modified: / 04-11-2018 / 11:19:35 / Claus Gittinger" |
21207 | 179 |
! |
180 |
||
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
181 |
makePTYPair |
10327 | 182 |
"return an array with two streams - the first one is the master, |
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
183 |
the second the slave of a ptym/ptys pair. |
23779 | 184 |
(actually, there is a third element, which is the slave-pty's name) |
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
185 |
This is much like a bidirectional pipe, but allows signals & |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
186 |
control chars to be passed through the connection. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
187 |
This is needed to execute a shell in a view. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
188 |
This is the higher level equivalent of OperatingSystem>>makePTYPair |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
189 |
(which returns an array of file-descriptors)." |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
190 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
191 |
|ptyPair master slave| |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
192 |
|
23503 | 193 |
"/ must protect from being unwound, otherwise the pipe-FD might not be registered for finalization |
194 |
[ |
|
23779 | 195 |
ptyPair := OperatingSystem makePTY. |
23503 | 196 |
ptyPair notNil ifTrue:[ |
23779 | 197 |
master := PipeStream |
198 |
forFileDescriptor:(ptyPair at:1) mode:#readWrite |
|
199 |
buffered:false handleType:nil. |
|
200 |
slave := PipeStream |
|
201 |
forFileDescriptor:(ptyPair at:2) mode:#readWrite |
|
202 |
buffered:false handleType:nil. |
|
23503 | 203 |
slave buffered:false. |
204 |
ptyPair at:1 put:master. |
|
205 |
ptyPair at:2 put:slave. |
|
206 |
] |
|
207 |
] valueUninterruptably. |
|
208 |
^ ptyPair |
|
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
209 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
210 |
" |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
211 |
ExternalStream makePTYPair. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
212 |
" |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
213 |
|
23487 | 214 |
"Modified: / 29-02-1996 / 18:28:36 / cg" |
23779 | 215 |
"Modified (comment): / 27-02-2019 / 18:49:46 / Claus Gittinger" |
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
216 |
! |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
217 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
218 |
makePipe |
10327 | 219 |
"return an array with two streams - the first one for reading, |
220 |
the second for writing. |
|
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
221 |
This is the higher level equivalent of OperatingSystem>>makePipe |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
222 |
(which returns an array of file-descriptors)." |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
223 |
|
21230 | 224 |
|pipeArray rs ws| |
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
225 |
|
23503 | 226 |
"/ must protect from being unwound, otherwise the pipe-FD might not be registered for finalization |
227 |
[ |
|
21230 | 228 |
pipeArray := OperatingSystem makePipe. |
23503 | 229 |
pipeArray isNil ifTrue:[ |
230 |
"/ ok, maybe someone has forgotten to close a stream; enforce finalization and try again |
|
231 |
'makePipe: enforcing finalization to close any open streams' infoPrintCR. |
|
232 |
ObjectMemory garbageCollect; finalize. |
|
233 |
pipeArray := OperatingSystem makePipe. |
|
234 |
pipeArray isNil ifTrue:[ |
|
235 |
|errorNumber errorHolder| |
|
21207 | 236 |
|
23503 | 237 |
errorNumber := OperatingSystem lastErrorNumber. |
238 |
errorHolder := errorNumber. |
|
239 |
OpenError newException |
|
240 |
errorCode:errorNumber; |
|
241 |
osErrorHolder:errorHolder; |
|
242 |
raise. |
|
243 |
]. |
|
244 |
]. |
|
245 |
rs := PipeStream forFileDescriptor:(pipeArray at:1) mode:#readonly buffered:false handleType:#pipeFilePointer. |
|
246 |
ws := PipeStream forFileDescriptor:(pipeArray at:2) mode:#writeonly buffered:false handleType:#pipeFilePointer. |
|
247 |
] valueUninterruptably. |
|
21207 | 248 |
|
249 |
^ Array with:rs with:ws |
|
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
250 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
251 |
" |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
252 |
|pipe rs ws| |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
253 |
|
7541 | 254 |
pipe := NonPositionableExternalStream makePipe. |
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
255 |
rs := pipe at:1. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
256 |
ws := pipe at:2. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
257 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
258 |
'read ...'. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
259 |
[ |
21207 | 260 |
1 to:10 do:[:i | |
261 |
Transcript showCR:rs nextLine |
|
262 |
]. |
|
263 |
rs close. |
|
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
264 |
] forkAt:7. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
265 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
266 |
'write ...'. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
267 |
[ |
21207 | 268 |
1 to:10 do:[:i | |
269 |
ws nextPutAll:'hello world '; nextPutAll:i printString; cr |
|
270 |
]. |
|
271 |
ws close. |
|
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
272 |
] fork. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
273 |
" |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
274 |
|
23485 | 275 |
"Modified: / 29-02-1996 / 18:28:36 / cg" |
23503 | 276 |
"Modified (format): / 04-11-2018 / 11:17:48 / Claus Gittinger" |
613 | 277 |
! ! |
278 |
||
16340
6203956d63ee
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16207
diff
changeset
|
279 |
!NonPositionableExternalStream methodsFor:'accessing'! |
6203956d63ee
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16207
diff
changeset
|
280 |
|
6203956d63ee
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16207
diff
changeset
|
281 |
buffered:aBoolean |
16348 | 282 |
"do not allow to change to buffered mode - ignore true here" |
16340
6203956d63ee
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16207
diff
changeset
|
283 |
|
16348 | 284 |
aBoolean ifFalse:[ |
23901 | 285 |
super buffered:false. |
16348 | 286 |
]. |
23901 | 287 |
^ false |
288 |
||
289 |
"Modified: / 13-03-2019 / 19:33:55 / Stefan Vogel" |
|
23489 | 290 |
! |
291 |
||
292 |
setCommandString:dummyString |
|
293 |
"dummy for PipeStream compatibility" |
|
294 |
||
295 |
"Created: / 29-10-2018 / 18:06:44 / Claus Gittinger" |
|
16340
6203956d63ee
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16207
diff
changeset
|
296 |
! ! |
6203956d63ee
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16207
diff
changeset
|
297 |
|
613 | 298 |
!NonPositionableExternalStream methodsFor:'error handling'! |
299 |
||
22192 | 300 |
errorNotOpen |
301 |
"report an error, that the stream has not been opened or has been closed. |
|
302 |
Redefined to care for closed stdout and stderr, |
|
303 |
which happens, if the console is closed while I am running. |
|
304 |
In this case, we ignore the error." |
|
305 |
||
306 |
((self == Stderr) or:[self == Stdout]) ifTrue:[ |
|
307 |
^ self |
|
308 |
]. |
|
309 |
^ super errorNotOpen |
|
310 |
! |
|
311 |
||
613 | 312 |
positionError |
2965 | 313 |
"{ Pragma: +optSpace }" |
314 |
||
613 | 315 |
"notify that this stream has no concept of a position" |
316 |
||
8320
229adaeaf183
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
7861
diff
changeset
|
317 |
^ PositionError raiseRequestWith:self |
4559 | 318 |
|
319 |
" |
|
320 |
Stderr positionError |
|
321 |
" |
|
19073 | 322 |
! |
323 |
||
324 |
writeError:errorNumber |
|
20256 | 325 |
"report an error, that some write error occurred. |
19073 | 326 |
Redefined to care for writeErrors on stdout and stderr, |
327 |
which happens under linux, if the console is closed while I am running. |
|
328 |
In this case, we ignore the error." |
|
329 |
||
22541 | 330 |
"/ No!! this causes nextPutAll: to loop forever, since there are always bytes remaining |
331 |
"/ |
|
332 |
"/ ((self == Stderr) or:[self == Stdout]) ifTrue:[ |
|
333 |
"/ ^ self |
|
334 |
"/ ]. |
|
19073 | 335 |
super writeError:errorNumber |
22541 | 336 |
|
337 |
"Modified: / 09-02-2018 / 20:33:30 / stefan" |
|
613 | 338 |
! ! |
339 |
||
15919
96f6c9fb9f7a
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15796
diff
changeset
|
340 |
!NonPositionableExternalStream methodsFor:'initialization'! |
96f6c9fb9f7a
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15796
diff
changeset
|
341 |
|
96f6c9fb9f7a
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15796
diff
changeset
|
342 |
initialize |
16340
6203956d63ee
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16207
diff
changeset
|
343 |
"non-positionable streams do not work well when buffered" |
15919
96f6c9fb9f7a
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15796
diff
changeset
|
344 |
|
96f6c9fb9f7a
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15796
diff
changeset
|
345 |
super initialize. |
20105 | 346 |
position := nil. "I am not positionable (reading and writing simultaneously)" |
15922 | 347 |
buffered := false. |
15919
96f6c9fb9f7a
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15796
diff
changeset
|
348 |
! ! |
96f6c9fb9f7a
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15796
diff
changeset
|
349 |
|
20368 | 350 |
|
7861
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
351 |
!NonPositionableExternalStream methodsFor:'non homogenous reading'! |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
352 |
|
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
353 |
nextBytes:count into:anObject startingAt:start |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
354 |
"read the next count bytes into an object and return the number of |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
355 |
bytes read or the number of bytes read, if EOF is encountered before. |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
356 |
An exception is raised if the connection is broken. |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
357 |
|
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
358 |
Redefined here to avoid blocking of ST/X when waiting for io. |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
359 |
Instead only the calling thread will block" |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
360 |
|
8815 | 361 |
|remaining offset nRead| |
7861
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
362 |
|
21750 | 363 |
count == 0 ifTrue:[ ^ 0 ]. |
7861
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
364 |
|
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
365 |
remaining := count. |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
366 |
offset := start. |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
367 |
|
10327 | 368 |
[ |
21750 | 369 |
nRead := self nextAvailableBytes:remaining into:anObject startingAt:offset. |
370 |
nRead == 0 ifTrue:[ |
|
371 |
"atEnd does a readWait" |
|
372 |
(self atEnd) ifTrue:[^ count - remaining] |
|
373 |
] ifFalse:[ |
|
374 |
remaining := remaining - nRead. |
|
375 |
(remaining == 0) ifTrue:[^ count]. |
|
376 |
offset := offset + nRead. |
|
377 |
] |
|
378 |
] loop. |
|
7861
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
379 |
|
21750 | 380 |
"Modified (format): / 16-05-2017 / 12:10:25 / cg" |
20105 | 381 |
! |
382 |
||
383 |
nextInt16MSB:msbFlag |
|
384 |
"redefined to wait for data on pipes and sockets" |
|
385 |
||
386 |
|bytes nRead| |
|
387 |
||
388 |
bytes := ByteArray uninitializedNew:2. |
|
389 |
nRead := self nextBytes:2 into:bytes startingAt:1. |
|
390 |
nRead ~~ 2 ifTrue:[ |
|
391 |
^ self pastEndRead. |
|
392 |
]. |
|
393 |
^ bytes signedInt16At:1 MSB:msbFlag. |
|
394 |
! |
|
395 |
||
396 |
nextInt32MSB:msbFlag |
|
397 |
"redefined to wait for data on pipes and sockets" |
|
398 |
||
399 |
|bytes nRead| |
|
400 |
||
401 |
bytes := ByteArray uninitializedNew:4. |
|
402 |
nRead := self nextBytes:4 into:bytes startingAt:1. |
|
403 |
nRead ~~ 4 ifTrue:[ |
|
404 |
^ self pastEndRead. |
|
405 |
]. |
|
406 |
^ bytes signedInt32At:1 MSB:msbFlag. |
|
407 |
! |
|
408 |
||
21096 | 409 |
nextInt64MSB:msbFlag |
410 |
"redefined to wait for data on pipes and sockets" |
|
411 |
||
412 |
|bytes nRead| |
|
413 |
||
414 |
bytes := ByteArray uninitializedNew:8. |
|
415 |
nRead := self nextBytes:8 into:bytes startingAt:1. |
|
416 |
nRead ~~ 8 ifTrue:[ |
|
417 |
^ self pastEndRead. |
|
418 |
]. |
|
419 |
^ bytes signedInt64At:1 MSB:msbFlag. |
|
420 |
! |
|
421 |
||
20105 | 422 |
nextUnsignedInt16MSB:msbFlag |
423 |
"redefined to wait for data on pipes and sockets" |
|
424 |
||
425 |
|bytes nRead| |
|
426 |
||
21096 | 427 |
bytes := ByteArray uninitializedNew:2. |
20105 | 428 |
nRead := self nextBytes:2 into:bytes startingAt:1. |
429 |
nRead ~~ 2 ifTrue:[ |
|
430 |
^ self pastEndRead. |
|
431 |
]. |
|
432 |
^ bytes unsignedInt16At:1 MSB:msbFlag. |
|
433 |
! |
|
434 |
||
435 |
nextUnsignedInt32MSB:msbFlag |
|
436 |
"redefined to wait for data on pipes and sockets" |
|
437 |
||
438 |
|bytes nRead| |
|
439 |
||
440 |
bytes := ByteArray uninitializedNew:4. |
|
441 |
nRead := self nextBytes:4 into:bytes startingAt:1. |
|
442 |
nRead ~~ 4 ifTrue:[ |
|
443 |
^ self pastEndRead. |
|
444 |
]. |
|
445 |
^ bytes unsignedInt32At:1 MSB:msbFlag. |
|
21096 | 446 |
! |
447 |
||
448 |
nextUnsignedInt64MSB:msbFlag |
|
449 |
"redefined to wait for data on pipes and sockets" |
|
450 |
||
451 |
|bytes nRead| |
|
452 |
||
453 |
bytes := ByteArray uninitializedNew:8. |
|
454 |
nRead := self nextBytes:8 into:bytes startingAt:1. |
|
455 |
nRead ~~ 8 ifTrue:[ |
|
456 |
^ self pastEndRead. |
|
457 |
]. |
|
458 |
^ bytes unsignedInt64At:1 MSB:msbFlag. |
|
7861
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
459 |
! ! |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
460 |
|
613 | 461 |
!NonPositionableExternalStream methodsFor:'positioning'! |
462 |
||
463 |
position |
|
20105 | 464 |
position isNil ifTrue:[ |
465 |
^ self positionError. |
|
466 |
]. |
|
467 |
^ position |
|
1 | 468 |
! |
469 |
||
20105 | 470 |
position:newPosition |
471 |
(position isNil or:[newPosition < position]) ifTrue:[ |
|
472 |
"there is no turning back" |
|
473 |
^ self positionError |
|
474 |
]. |
|
475 |
self skip:newPosition - position. |
|
613 | 476 |
! |
44 | 477 |
|
22954 | 478 |
setToEnd |
479 |
"skip to the end of stream." |
|
480 |
||
481 |
[self nextOrNil notNil] whileTrue. |
|
482 |
||
483 |
"Created: / 18-05-2018 / 14:07:36 / Stefan Vogel" |
|
484 |
! |
|
485 |
||
613 | 486 |
skip:numberToSkip |
7241 | 487 |
"skip count bytes/characters, return the receiver. |
488 |
Re-redefined since PositionableStream redefined it." |
|
613 | 489 |
|
16207
923f92b4f0e8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16187
diff
changeset
|
490 |
"don't know how to unread ..." |
613 | 491 |
numberToSkip < 0 ifTrue:[ |
20105 | 492 |
^ self positionError. |
88 | 493 |
]. |
16207
923f92b4f0e8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16187
diff
changeset
|
494 |
numberToSkip timesRepeat:[self next]. |
4467 | 495 |
|
496 |
"Modified: / 30.7.1999 / 12:42:12 / cg" |
|
1 | 497 |
! ! |
498 |
||
177 | 499 |
!NonPositionableExternalStream methodsFor:'printing & storing'! |
500 |
||
501 |
printOn:aStream |
|
2965 | 502 |
"{ Pragma: +optSpace }" |
503 |
||
4392 | 504 |
"append a user printed representation of the receiver to aStream. |
505 |
The format is suitable for a human - not meant to be read back." |
|
177 | 506 |
|
2965 | 507 |
|myName| |
508 |
||
177 | 509 |
self == Stdin ifTrue:[ |
10327 | 510 |
myName := 'Stdin'. |
2965 | 511 |
] ifFalse:[ |
10327 | 512 |
self == Stdout ifTrue:[ |
513 |
myName := 'Stdout'. |
|
514 |
] ifFalse:[ |
|
515 |
self == Stderr ifTrue:[ |
|
516 |
myName := 'Stderr'. |
|
517 |
] |
|
518 |
] |
|
177 | 519 |
]. |
2965 | 520 |
|
521 |
myName notNil ifTrue:[ |
|
10327 | 522 |
aStream nextPutAll:myName. |
523 |
^ self |
|
177 | 524 |
]. |
525 |
super printOn:aStream |
|
526 |
! |
|
527 |
||
528 |
storeOn:aStream |
|
2965 | 529 |
"{ Pragma: +optSpace }" |
530 |
||
213 | 531 |
"append a printed representation of the receiver on aStream, from |
532 |
which the receiver can be reconstructed." |
|
177 | 533 |
|
534 |
((self == Stdin) |
|
535 |
or:[self == Stdout |
|
536 |
or:[self == Stderr]]) ifTrue:[ |
|
537 |
^ self printOn:aStream |
|
538 |
]. |
|
539 |
super storeOn:aStream |
|
540 |
! ! |
|
541 |
||
20680 | 542 |
!NonPositionableExternalStream protectedMethodsFor:'private'! |
543 |
||
544 |
closeFile |
|
21687 | 545 |
<resource: #PRIVATE> |
546 |
||
20680 | 547 |
|semasToSignal| |
548 |
||
549 |
handle notNil ifTrue:[ |
|
550 |
"make sure, that no select is performed on closed file descriptors" |
|
21239 | 551 |
semasToSignal := Processor disableFd:self fileHandle doSignal:false. |
20680 | 552 |
super closeFile. |
553 |
||
554 |
"tell the waiters that they must not wait any longer" |
|
555 |
semasToSignal do:[:eachSema| |
|
556 |
eachSema signalForAll. |
|
557 |
]. |
|
558 |
]. |
|
21687 | 559 |
|
560 |
"Modified: / 07-04-2017 / 14:32:00 / cg" |
|
20680 | 561 |
! ! |
562 |
||
613 | 563 |
!NonPositionableExternalStream methodsFor:'private'! |
360 | 564 |
|
5407 | 565 |
handleForStderr |
566 |
"{ Pragma: +optSpace }" |
|
567 |
||
568 |
"return a stderr handle" |
|
569 |
||
570 |
%{ |
|
18313 | 571 |
#ifdef __SCHTEAM__ |
572 |
return context._RETURN( StandardErrorStream ); |
|
573 |
#else |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19073
diff
changeset
|
574 |
# ifdef __win32__ |
12677
aeab74b04f0b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
12395
diff
changeset
|
575 |
RETURN ( __MKEXTERNALADDRESS( __win32_stderr() )); |
18313 | 576 |
# else |
12677
aeab74b04f0b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
12395
diff
changeset
|
577 |
RETURN ( __MKEXTERNALADDRESS(stderr) ); |
18313 | 578 |
# endif |
10327 | 579 |
#endif |
5407 | 580 |
%} |
581 |
! |
|
582 |
||
583 |
handleForStdin |
|
584 |
"{ Pragma: +optSpace }" |
|
585 |
||
586 |
"return a stdin handle" |
|
587 |
||
588 |
%{ |
|
18313 | 589 |
#ifdef __SCHTEAM__ |
590 |
return context._RETURN( StandardInputStream ); |
|
591 |
#else |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19073
diff
changeset
|
592 |
# ifdef __win32__ |
12677
aeab74b04f0b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
12395
diff
changeset
|
593 |
RETURN ( __MKEXTERNALADDRESS( __win32_stdin() )); |
18313 | 594 |
# else |
12677
aeab74b04f0b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
12395
diff
changeset
|
595 |
RETURN ( __MKEXTERNALADDRESS(stdin) ); |
18313 | 596 |
# endif |
10327 | 597 |
#endif |
5407 | 598 |
%} |
599 |
! |
|
600 |
||
601 |
handleForStdout |
|
602 |
"{ Pragma: +optSpace }" |
|
603 |
||
604 |
"return a stdout handle" |
|
605 |
||
606 |
%{ |
|
18313 | 607 |
#ifdef __SCHTEAM__ |
608 |
return context._RETURN( StandardOutputStream ); |
|
609 |
#else |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19073
diff
changeset
|
610 |
# ifdef __win32__ |
12677
aeab74b04f0b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
12395
diff
changeset
|
611 |
RETURN ( __MKEXTERNALADDRESS( __win32_stdout() )); |
18313 | 612 |
# else |
12677
aeab74b04f0b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
12395
diff
changeset
|
613 |
RETURN ( __MKEXTERNALADDRESS(stdout) ); |
18313 | 614 |
# endif |
10327 | 615 |
#endif |
5407 | 616 |
%} |
617 |
! |
|
618 |
||
613 | 619 |
initializeForStderr |
22702 | 620 |
"setup for writing to stderr" |
2965 | 621 |
|
22702 | 622 |
"{ Pragma: +optSpace }" |
269 | 623 |
|
20256 | 624 |
mode := #writeonly. "since stderr may be redirected to a pipe, reading is forbidden" |
21701 | 625 |
binary := false. |
613 | 626 |
buffered := false. |
20105 | 627 |
position := 0. "only writing - can keep track of position" |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11907
diff
changeset
|
628 |
handle := self handleForStderr. |
12754 | 629 |
handleType := #filePointer. |
21702 | 630 |
hitEOF := false. |
631 |
self initializeEOLMode. |
|
5407 | 632 |
OperatingSystem isMSWINDOWSlike ifTrue:[ |
20105 | 633 |
eolMode := #crlf |
22702 | 634 |
]. |
635 |
"This will possibly never be finalized, but keep in in Lobby |
|
636 |
to make #openStreams work" |
|
637 |
self registerForFinalization. |
|
638 |
"#registerForFinalization sets #preventTenueOf:, but I live forever..." |
|
639 |
ObjectMemory allowTenureOf:self. |
|
21701 | 640 |
|
21702 | 641 |
"Modified: / 25-04-2017 / 02:13:56 / cg" |
22702 | 642 |
"Modified (comment): / 24-04-2018 / 10:28:50 / stefan" |
613 | 643 |
! |
1 | 644 |
|
645 |
initializeForStdin |
|
2965 | 646 |
"{ Pragma: +optSpace }" |
647 |
||
44 | 648 |
"setup for reading stdin" |
649 |
||
1 | 650 |
mode := #readonly. |
21701 | 651 |
binary := false. |
6843
ef53f65b7364
stdout/err must not be buffered (sending prompts without NL)
Claus Gittinger <cg@exept.de>
parents:
5918
diff
changeset
|
652 |
"/ buffered := true. |
ef53f65b7364
stdout/err must not be buffered (sending prompts without NL)
Claus Gittinger <cg@exept.de>
parents:
5918
diff
changeset
|
653 |
buffered := false. |
20105 | 654 |
position := 0. "only reading - can keep track of position" |
12754 | 655 |
handleType := #filePointer. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11907
diff
changeset
|
656 |
handle := self handleForStdin. |
21702 | 657 |
hitEOF := false. |
658 |
self initializeEOLMode. |
|
22702 | 659 |
"This will possibly never be finalized, but keep in in Lobby |
660 |
to make #openStreams work" |
|
661 |
self registerForFinalization. |
|
662 |
"#registerForFinalization sets #preventTenueOf:, but I live forever..." |
|
663 |
ObjectMemory allowTenureOf:self. |
|
21701 | 664 |
|
21702 | 665 |
"Modified: / 25-04-2017 / 02:14:22 / cg" |
22702 | 666 |
"Modified (comment): / 24-04-2018 / 10:28:43 / stefan" |
1 | 667 |
! |
668 |
||
669 |
initializeForStdout |
|
2965 | 670 |
"{ Pragma: +optSpace }" |
671 |
||
44 | 672 |
"setup for writing to stdout" |
673 |
||
20105 | 674 |
mode := #writeonly. "since stdout may be redirected to a pipe, reading is forbidden" |
21701 | 675 |
binary := false. |
10 | 676 |
buffered := false. |
20105 | 677 |
position := 0. "only writing - can keep track of position" |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11907
diff
changeset
|
678 |
handle := self handleForStdout. |
12754 | 679 |
handleType := #filePointer. |
21702 | 680 |
self initializeEOLMode. |
681 |
hitEOF := false. |
|
5407 | 682 |
OperatingSystem isMSWINDOWSlike ifTrue:[ |
20105 | 683 |
eolMode := #crlf |
22702 | 684 |
]. |
685 |
||
686 |
"This will possibly never be finalized, but keep in in Lobby |
|
687 |
to make #openStreams work" |
|
688 |
self registerForFinalization. |
|
689 |
"#registerForFinalization sets #preventTenueOf:, but I live forever..." |
|
690 |
ObjectMemory allowTenureOf:self. |
|
21701 | 691 |
|
21702 | 692 |
"Modified: / 25-04-2017 / 02:14:27 / cg" |
22702 | 693 |
"Modified (comment): / 24-04-2018 / 10:28:38 / stefan" |
1 | 694 |
! |
695 |
||
696 |
reOpen |
|
2965 | 697 |
"{ Pragma: +optSpace }" |
698 |
||
44 | 699 |
"reopen the stream after an image restart. |
700 |
If I am one of the standard streams, reopen is easy" |
|
1 | 701 |
|
477
8710aba7876b
oops - making id's real objects requires a store macro
Claus Gittinger <cg@exept.de>
parents:
475
diff
changeset
|
702 |
(self == StdInStream) ifTrue:[ |
177 | 703 |
^ self initializeForStdin |
1 | 704 |
]. |
477
8710aba7876b
oops - making id's real objects requires a store macro
Claus Gittinger <cg@exept.de>
parents:
475
diff
changeset
|
705 |
(self == StdOutStream) ifTrue:[ |
177 | 706 |
^ self initializeForStdout |
1 | 707 |
]. |
477
8710aba7876b
oops - making id's real objects requires a store macro
Claus Gittinger <cg@exept.de>
parents:
475
diff
changeset
|
708 |
(self == StdErrorStream) ifTrue:[ |
177 | 709 |
^ self initializeForStderr |
1 | 710 |
]. |
711 |
^ super reOpen |
|
712 |
! ! |
|
613 | 713 |
|
714 |
!NonPositionableExternalStream methodsFor:'queries'! |
|
715 |
||
5851
6823cc642d60
only do readWait for pipes and sockets.
Claus Gittinger <cg@exept.de>
parents:
5407
diff
changeset
|
716 |
atEnd |
21028 | 717 |
"return true, if position is at end. |
718 |
Notice: this is a blocking operation, as we do not know in advance, |
|
719 |
if there will be anything to read |
|
720 |
(i.e. if the partner will send more or close the stream). |
|
721 |
If you want to check for available data, |
|
722 |
use nextAvailable:, or canReadWithoutBlocking" |
|
5851
6823cc642d60
only do readWait for pipes and sockets.
Claus Gittinger <cg@exept.de>
parents:
5407
diff
changeset
|
723 |
|
5918 | 724 |
(self == StdInStream) ifTrue:[ |
20225 | 725 |
OperatingSystem hasConsole ifFalse:[ |
726 |
^ true |
|
22777 | 727 |
]. |
728 |
"/ ^ self isOpen not. |
|
5918 | 729 |
]. |
730 |
||
20685 | 731 |
handle notNil ifTrue:[ |
732 |
"first, wait to avoid blocking on the read. |
|
733 |
On end of stream or error, readWait will return" |
|
5851
6823cc642d60
only do readWait for pipes and sockets.
Claus Gittinger <cg@exept.de>
parents:
5407
diff
changeset
|
734 |
|
22777 | 735 |
(self == StdInStream) ifTrue:[ |
736 |
self readWaitWithTimeoutMs:0.05. |
|
737 |
] ifFalse:[ |
|
738 |
self readWaitWithTimeoutMs:nil. |
|
739 |
]. |
|
20685 | 740 |
]. |
20225 | 741 |
handle isNil ifTrue:[ |
20685 | 742 |
"we are closed or were closed while waiting - so we are at the end" |
20225 | 743 |
^ true. |
744 |
]. |
|
5851
6823cc642d60
only do readWait for pipes and sockets.
Claus Gittinger <cg@exept.de>
parents:
5407
diff
changeset
|
745 |
^ super atEnd. |
6823cc642d60
only do readWait for pipes and sockets.
Claus Gittinger <cg@exept.de>
parents:
5407
diff
changeset
|
746 |
! |
6823cc642d60
only do readWait for pipes and sockets.
Claus Gittinger <cg@exept.de>
parents:
5407
diff
changeset
|
747 |
|
17607
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
748 |
collectionSize |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
749 |
"we do not know our size" |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
750 |
|
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
751 |
^ self positionError |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
752 |
! |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
753 |
|
5407 | 754 |
isPositionable |
755 |
"return true, if the stream supports positioning (this one is not)" |
|
756 |
||
757 |
^ false |
|
11455 | 758 |
! |
759 |
||
17607
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
760 |
remainingSize |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
761 |
"we do not know our size" |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
762 |
|
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
763 |
^ self positionError |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
764 |
! |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
765 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11907
diff
changeset
|
766 |
size |
11455 | 767 |
"we do not know our size" |
768 |
||
769 |
^ self positionError |
|
613 | 770 |
! ! |
771 |
||
10349
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
772 |
!NonPositionableExternalStream methodsFor:'reading'! |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
773 |
|
14758
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
774 |
next |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
775 |
"return the next element, if available. |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
776 |
If nothing is available, this does never raise a read-beyond end signal. |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
777 |
Instead, nil is returned immediately. |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
778 |
|
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
779 |
Redefined, to wait on pipes and sockets" |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
780 |
|
18798 | 781 |
self readWaitWithTimeoutMs:nil. |
14758
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
782 |
^ super next |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
783 |
! |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
784 |
|
20105 | 785 |
nextByte |
786 |
"redefined, to wait on pipes and sockets" |
|
15796
b3fdd3c2aec2
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
15720
diff
changeset
|
787 |
|
18798 | 788 |
self readWaitWithTimeoutMs:nil. |
20105 | 789 |
^ super nextByte |
790 |
! |
|
791 |
||
792 |
nextLine |
|
793 |
"return the characters upTo (but excluding) the next cr (carriage return) |
|
794 |
character (i.e. read a single line of text). |
|
795 |
If the previous-to-last character is a cr, this is also removed, |
|
796 |
so it's possible to read alien (i.e. ms-dos) text as well. |
|
797 |
Redefined to not block forever if no cr is read." |
|
798 |
||
20175 | 799 |
|answerStream char wasBinary available "{ Class:SmallInteger }"| |
20105 | 800 |
|
20112 | 801 |
answerStream := WriteStream on:(String uninitializedNew:80). |
20105 | 802 |
[ |
21131 | 803 |
"#atEnd waits for new data or EOF" |
804 |
self atEnd ifTrue:[ |
|
22954 | 805 |
answerStream isEmpty ifTrue:[ |
21131 | 806 |
^ self pastEndRead. |
807 |
]. |
|
808 |
^ answerStream contents. |
|
809 |
]. |
|
20105 | 810 |
available := self numAvailableForRead. |
21131 | 811 |
available == 0 ifTrue:[ |
812 |
"so #atEnd didn't wait above, but no data is present - check for error" |
|
813 |
self peek. |
|
814 |
available := self numAvailableForRead. |
|
815 |
]. |
|
816 |
||
20113 | 817 |
wasBinary := binary. "temporarily set to text mode" |
818 |
binary := false. |
|
20105 | 819 |
available timesRepeat:[ |
20148 | 820 |
char := super nextOrNil. "don't wait - we know that there is data" |
20105 | 821 |
char == Character cr ifTrue:[ |
22954 | 822 |
(answerStream isEmpty not and:[answerStream last == Character return]) ifTrue:[ |
20105 | 823 |
answerStream backStep. |
824 |
]. |
|
20113 | 825 |
binary := wasBinary. |
20105 | 826 |
^ answerStream contents. |
827 |
]. |
|
20256 | 828 |
char notNil ifTrue:[ |
20148 | 829 |
answerStream nextPut:char. |
830 |
]. |
|
20105 | 831 |
]. |
20113 | 832 |
binary := wasBinary. "restore saved mode" |
20105 | 833 |
] loop. |
834 |
||
835 |
"not reached" |
|
22954 | 836 |
|
837 |
"Modified: / 18-05-2018 / 14:01:19 / Stefan Vogel" |
|
15796
b3fdd3c2aec2
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
15720
diff
changeset
|
838 |
! |
b3fdd3c2aec2
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
15720
diff
changeset
|
839 |
|
10349
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
840 |
nextOrNil |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
841 |
"like #next, this returns the next element, if available. |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
842 |
If nothing is available, this does never raise a read-beyond end signal. |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
843 |
Instead, nil is returned immediately. |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
844 |
|
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
845 |
Redefined, to wait on pipes and sockets" |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
846 |
|
20680 | 847 |
self readWaitWithTimeoutMs:nil. |
848 |
handle isNil ifTrue:[ |
|
849 |
"we were closed while waiting - so we are at the end" |
|
850 |
^ nil. |
|
851 |
]. |
|
14758
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
852 |
^ super nextOrNil |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
853 |
! |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
854 |
|
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
855 |
peek |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
856 |
"Redefined, to wait on pipes and sockets" |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
857 |
|
18798 | 858 |
self readWaitWithTimeoutMs:nil. |
14758
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
859 |
^ super peek |
10349
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
860 |
! |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
861 |
|
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
862 |
peekOrNil |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
863 |
"like #peek, this returns the next element, if available. |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
864 |
If nothing is available, this does never raise a read-beyond end signal. |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
865 |
Instead, nil is returned immediately. |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
866 |
|
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
867 |
Redefined, to wait on pipes and sockets" |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
868 |
|
20680 | 869 |
self readWaitWithTimeoutMs:nil. |
870 |
handle isNil ifTrue:[ |
|
871 |
"we were closed while waiting - so we are at the end" |
|
872 |
^ nil. |
|
873 |
]. |
|
10349
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
874 |
^ self peek |
18798 | 875 |
! |
876 |
||
20105 | 877 |
upTo:anObject into:aStream |
878 |
"read a collection of all objects up-to anObject and append these |
|
20256 | 879 |
elements to aStream, but excluding anObject. |
20105 | 880 |
The next read operation will return the element after anObject. |
881 |
(i.e. anObject is considered a separator, which is skipped) |
|
882 |
Similar to #through:, but the matching object is not included in the returned collection. |
|
883 |
If anObject is not encountered, all elements up to the end are read and returned. |
|
884 |
Compare this with #through: which also reads up to some object |
|
885 |
and also positions behind it, but DOES include it in the returned value." |
|
886 |
||
887 |
|element available "{ Class:SmallInteger }"| |
|
18798 | 888 |
|
20105 | 889 |
[ |
21131 | 890 |
"#atEnd waits for new data or EOF" |
891 |
self atEnd ifTrue:[ |
|
892 |
^ self. |
|
893 |
]. |
|
20105 | 894 |
available := self numAvailableForRead. |
21131 | 895 |
available == 0 ifTrue:[ |
896 |
"so #atEnd didn't wait above, but no data is present - check for error" |
|
897 |
self peek. |
|
898 |
available := self numAvailableForRead. |
|
899 |
]. |
|
900 |
||
20105 | 901 |
available timesRepeat:[ |
902 |
element := super next. "we know that there is data" |
|
903 |
element = anObject ifTrue:[ |
|
904 |
^ self. |
|
905 |
]. |
|
906 |
aStream nextPut:element. |
|
907 |
]. |
|
908 |
] loop. |
|
909 |
||
910 |
"not reached" |
|
10349
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
911 |
! ! |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
912 |
|
21361 | 913 |
!NonPositionableExternalStream methodsFor:'testing'! |
914 |
||
915 |
canReadWithoutBlocking |
|
916 |
"return true, if any data is available for reading (i.e. |
|
917 |
a read operation will not block the smalltalk process), false otherwise. |
|
918 |
We know, that error conditions do not block, so return true for errors." |
|
919 |
||
920 |
^ readAhead notNil |
|
921 |
or:[handle isNil |
|
922 |
or:[mode == #writeonly |
|
923 |
or:[OperatingSystem readCheck:self fileHandle]]] |
|
924 |
||
925 |
" |
|
926 |
|pipe| |
|
927 |
||
928 |
pipe := PipeStream readingFrom:'(sleep 10; echo hello)'. |
|
929 |
pipe canReadWithoutBlocking ifTrue:[ |
|
930 |
Transcript showCR:'data available' |
|
931 |
] ifFalse:[ |
|
932 |
Transcript showCR:'no data available' |
|
933 |
]. |
|
934 |
pipe close |
|
935 |
" |
|
936 |
||
937 |
"Modified: 25.9.1997 / 13:08:45 / stefan" |
|
938 |
! |
|
939 |
||
940 |
canWriteWithoutBlocking |
|
941 |
"return true, if data can be written into the stream |
|
942 |
(i.e. a write operation will not block the smalltalk process). |
|
943 |
We know, that error conditions do not block, so return true for errors." |
|
944 |
||
945 |
^ handle isNil |
|
946 |
or:[mode == #readonly |
|
947 |
or:[OperatingSystem writeCheck:self fileHandle]] |
|
948 |
! ! |
|
949 |
||
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
950 |
!NonPositionableExternalStream methodsFor:'writing'! |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
951 |
|
17642
5988beff84f7
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
17607
diff
changeset
|
952 |
nextPutAll:aCollection |
5988beff84f7
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
17607
diff
changeset
|
953 |
"nextPutBytes handles non-blocking io in receiver" |
5988beff84f7
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
17607
diff
changeset
|
954 |
|
5988beff84f7
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
17607
diff
changeset
|
955 |
self nextPutBytes:aCollection size from:aCollection startingAt:1. |
22541 | 956 |
|
957 |
"Modified: / 09-02-2018 / 19:59:15 / stefan" |
|
17642
5988beff84f7
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
17607
diff
changeset
|
958 |
! |
5988beff84f7
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
17607
diff
changeset
|
959 |
|
17607
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
960 |
nextPutAll:aCollection startingAt:start to:stop |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
961 |
"redefined, to wait until stream is writable, to avoid blocking in a write" |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
962 |
|
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
963 |
|count| |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
964 |
|
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
965 |
count := stop-start+1. |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
966 |
count ~= (self nextPutBytes:count from:aCollection startingAt:start) ifTrue:[ |
18313 | 967 |
"incomplete write" |
968 |
self writeError. |
|
17607
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
969 |
]. |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
970 |
! |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
971 |
|
7679 | 972 |
nextPutBytes:initialWriteCount from:buffer startingAt:initialOffset |
973 |
"redefined, to wait until stream is writable, to avoid blocking in a write" |
|
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
974 |
|
17607
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
975 |
|offset remaining wasBlocking| |
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
976 |
|
22194 | 977 |
wasBlocking := self blocking:false. |
22193 | 978 |
handle isNil ifTrue:[ |
22194 | 979 |
"self blocking: above may set handle to nil!!" |
22193 | 980 |
self errorNotOpen. |
981 |
(self == Stderr or:[self == Stdout]) ifTrue:[ |
|
982 |
"Care for closed stdout and stderr, |
|
983 |
which happens, if the console is closed while I am running. |
|
984 |
In this case, we ignore the error." |
|
985 |
^ initialWriteCount. |
|
986 |
]. |
|
987 |
^ 0. |
|
988 |
]. |
|
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
989 |
|
22194 | 990 |
offset := initialOffset. |
991 |
remaining := initialWriteCount. |
|
992 |
||
10327 | 993 |
[remaining ~~ 0] whileTrue:[ |
22193 | 994 |
|count| |
17607
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
995 |
|
22193 | 996 |
count := super nextPutBytes:remaining from:buffer startingAt:offset. |
7682
5d43745f59ac
only do a writeWait if previous non-blocking write failed to
Claus Gittinger <cg@exept.de>
parents:
7679
diff
changeset
|
997 |
|
22193 | 998 |
remaining := remaining - count. |
999 |
offset := offset + count. |
|
1000 |
remaining ~~ 0 ifTrue:[ |
|
1001 |
"Transcript showCR:'writeWait'." |
|
1002 |
self writeWait. |
|
1003 |
]. |
|
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
1004 |
]. |
7679 | 1005 |
wasBlocking ifTrue:[self blocking:true]. |
1006 |
||
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
1007 |
^ offset - initialOffset. |
22541 | 1008 |
! |
1009 |
||
1010 |
nextPutLine:aString |
|
1011 |
"write the characters in aString and append an end-of-Line marker |
|
1012 |
(LF, CR or CRLF - depending in the setting of eolMode)" |
|
1013 |
||
1014 |
self nextPutAll:aString. |
|
1015 |
self cr. |
|
1016 |
||
1017 |
"Created: / 09-02-2018 / 21:13:47 / stefan" |
|
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
1018 |
! ! |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
1019 |
|
1295 | 1020 |
!NonPositionableExternalStream class methodsFor:'documentation'! |
1021 |
||
1022 |
version |
|
18798 | 1023 |
^ '$Header$' |
12677
aeab74b04f0b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
12395
diff
changeset
|
1024 |
! |
aeab74b04f0b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
12395
diff
changeset
|
1025 |
|
aeab74b04f0b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
12395
diff
changeset
|
1026 |
version_CVS |
18798 | 1027 |
^ '$Header$' |
1295 | 1028 |
! ! |
17607
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
1029 |