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