author | HG Automerge |
Thu, 12 Jan 2017 09:42:50 +0000 | |
branch | jv |
changeset 21252 | 1b7c2d5523d5 |
parent 21249 | 86c01ee5a76e |
parent 21230 | 9b7a6559527b |
child 21285 | 7770135c2b54 |
permissions | -rw-r--r-- |
21207 | 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:'' |
20 |
classVariableNames:'StdInStream StdOutStream StdErrorStream' |
|
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 |
||
129 |
pipe := OperatingSystem makeBidirectionalPipe. |
|
130 |
pipe isNil ifTrue:[ |
|
131 |
"/ ok, maybe someone has forgotten to close a stream; enforce finalization and try again |
|
132 |
'makePipe: enforcing finalization to close any open streams' infoPrintCR. |
|
133 |
ObjectMemory garbageCollect; finalize. |
|
134 |
pipe := OperatingSystem makeBidirectionalPipe. |
|
135 |
]. |
|
136 |
||
137 |
pipe isNil ifTrue:[ |
|
138 |
|errorNumber errorHolder| |
|
139 |
||
140 |
errorNumber := OperatingSystem lastErrorNumber. |
|
141 |
errorHolder := errorNumber. |
|
142 |
OpenError newException |
|
143 |
errorCode:errorNumber; |
|
144 |
osErrorHolder:errorHolder; |
|
145 |
raise. |
|
146 |
]. |
|
147 |
||
21227 | 148 |
rs := NonPositionableExternalStream forFileDescriptor:(pipe at:1) mode:#readWrite buffered:false handleType:#pipeFilePointer. |
149 |
ws := NonPositionableExternalStream forFileDescriptor:(pipe at:2) mode:#readWrite buffered:false handleType:#pipeFilePointer. |
|
21207 | 150 |
^ Array with:rs with:ws |
151 |
||
152 |
" |
|
153 |
|pipe rs ws| |
|
154 |
||
155 |
pipe := NonPositionableExternalStream makeBidirectionalPipe. |
|
156 |
rs := pipe at:1. |
|
157 |
ws := pipe at:2. |
|
158 |
||
159 |
'read ...'. |
|
160 |
[ |
|
161 |
1 to:10 do:[:i | |
|
162 |
Transcript showCR:rs nextLine |
|
163 |
]. |
|
164 |
rs close. |
|
165 |
] forkAt:7. |
|
166 |
||
167 |
'write ...'. |
|
168 |
[ |
|
169 |
1 to:10 do:[:i | |
|
170 |
ws nextPutAll:'hello world '; nextPutAll:i printString; cr |
|
171 |
]. |
|
172 |
ws close. |
|
173 |
] fork. |
|
174 |
" |
|
175 |
! |
|
176 |
||
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
177 |
makePTYPair |
10327 | 178 |
"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
|
179 |
the second the slave of a ptym/ptys pair. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
180 |
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
|
181 |
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
|
182 |
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
|
183 |
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
|
184 |
(which returns an array of file-descriptors)." |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
185 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
186 |
|ptyPair master slave| |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
187 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
188 |
ptyPair := OperatingSystem makePTYPair. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
189 |
ptyPair notNil ifTrue:[ |
21227 | 190 |
master := NonPositionableExternalStream forReadWriteToFileDescriptor:(ptyPair at:1). |
191 |
master buffered:false. |
|
192 |
slave := NonPositionableExternalStream forReadWriteToFileDescriptor:(ptyPair at:2). |
|
193 |
slave buffered:false. |
|
194 |
ptyPair at:1 put:master. |
|
195 |
ptyPair at:2 put:slave. |
|
196 |
^ ptyPair |
|
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
197 |
]. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
198 |
^ nil |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
199 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
200 |
" |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
201 |
ExternalStream makePTYPair. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
202 |
" |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
203 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
204 |
"Modified: 29.2.1996 / 18:28:36 / cg" |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
205 |
! |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
206 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
207 |
makePipe |
10327 | 208 |
"return an array with two streams - the first one for reading, |
209 |
the second for writing. |
|
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
210 |
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
|
211 |
(which returns an array of file-descriptors)." |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
212 |
|
21230 | 213 |
|pipeArray rs ws| |
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
214 |
|
21230 | 215 |
pipeArray := OperatingSystem makePipe. |
216 |
pipeArray isNil ifTrue:[ |
|
21207 | 217 |
"/ ok, maybe someone has forgotten to close a stream; enforce finalization and try again |
218 |
'makePipe: enforcing finalization to close any open streams' infoPrintCR. |
|
219 |
ObjectMemory garbageCollect; finalize. |
|
21230 | 220 |
pipeArray := OperatingSystem makePipe. |
11907
98248eb75a8d
#retry makePipe after a failed try and garbageCollection
sr
parents:
11455
diff
changeset
|
221 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11907
diff
changeset
|
222 |
|
21230 | 223 |
pipeArray isNil ifTrue:[ |
21207 | 224 |
|errorNumber errorHolder| |
225 |
||
226 |
errorNumber := OperatingSystem lastErrorNumber. |
|
227 |
errorHolder := errorNumber. |
|
228 |
OpenError newException |
|
229 |
errorCode:errorNumber; |
|
230 |
osErrorHolder:errorHolder; |
|
231 |
raise. |
|
11907
98248eb75a8d
#retry makePipe after a failed try and garbageCollection
sr
parents:
11455
diff
changeset
|
232 |
]. |
21207 | 233 |
|
21230 | 234 |
rs := NonPositionableExternalStream forFileDescriptor:(pipeArray at:1) mode:#readonly buffered:false handleType:#pipeFilePointer. |
235 |
ws := NonPositionableExternalStream forFileDescriptor:(pipeArray at:2) mode:#writeonly buffered:false handleType:#pipeFilePointer. |
|
21207 | 236 |
^ Array with:rs with:ws |
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
237 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
238 |
" |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
239 |
|pipe rs ws| |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
240 |
|
7541 | 241 |
pipe := NonPositionableExternalStream makePipe. |
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
242 |
rs := pipe at:1. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
243 |
ws := pipe at:2. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
244 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
245 |
'read ...'. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
246 |
[ |
21207 | 247 |
1 to:10 do:[:i | |
248 |
Transcript showCR:rs nextLine |
|
249 |
]. |
|
250 |
rs close. |
|
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
251 |
] forkAt:7. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
252 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
253 |
'write ...'. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
254 |
[ |
21207 | 255 |
1 to:10 do:[:i | |
256 |
ws nextPutAll:'hello world '; nextPutAll:i printString; cr |
|
257 |
]. |
|
258 |
ws close. |
|
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
259 |
] fork. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
260 |
" |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
261 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
262 |
"Modified: 29.2.1996 / 18:28:36 / cg" |
613 | 263 |
! ! |
264 |
||
16340
6203956d63ee
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16207
diff
changeset
|
265 |
!NonPositionableExternalStream methodsFor:'accessing'! |
6203956d63ee
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16207
diff
changeset
|
266 |
|
6203956d63ee
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16207
diff
changeset
|
267 |
buffered:aBoolean |
16348 | 268 |
"do not allow to change to buffered mode - ignore true here" |
16340
6203956d63ee
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16207
diff
changeset
|
269 |
|
16348 | 270 |
aBoolean ifFalse:[ |
271 |
super buffered:false. |
|
272 |
]. |
|
16340
6203956d63ee
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16207
diff
changeset
|
273 |
! ! |
6203956d63ee
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16207
diff
changeset
|
274 |
|
613 | 275 |
!NonPositionableExternalStream methodsFor:'error handling'! |
276 |
||
277 |
positionError |
|
2965 | 278 |
"{ Pragma: +optSpace }" |
279 |
||
613 | 280 |
"notify that this stream has no concept of a position" |
281 |
||
8320
229adaeaf183
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
7861
diff
changeset
|
282 |
^ PositionError raiseRequestWith:self |
4559 | 283 |
|
284 |
" |
|
285 |
Stderr positionError |
|
286 |
" |
|
19073 | 287 |
! |
288 |
||
289 |
writeError:errorNumber |
|
20256 | 290 |
"report an error, that some write error occurred. |
19073 | 291 |
Redefined to care for writeErrors on stdout and stderr, |
292 |
which happens under linux, if the console is closed while I am running. |
|
293 |
In this case, we ignore the error." |
|
294 |
||
295 |
((self == Stderr) or:[self == Stdout]) ifTrue:[ |
|
20256 | 296 |
^ self |
19073 | 297 |
]. |
298 |
super writeError:errorNumber |
|
613 | 299 |
! ! |
300 |
||
15919
96f6c9fb9f7a
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15796
diff
changeset
|
301 |
!NonPositionableExternalStream methodsFor:'initialization'! |
96f6c9fb9f7a
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15796
diff
changeset
|
302 |
|
96f6c9fb9f7a
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15796
diff
changeset
|
303 |
initialize |
16340
6203956d63ee
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16207
diff
changeset
|
304 |
"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
|
305 |
|
96f6c9fb9f7a
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15796
diff
changeset
|
306 |
super initialize. |
20105 | 307 |
position := nil. "I am not positionable (reading and writing simultaneously)" |
15922 | 308 |
buffered := false. |
15919
96f6c9fb9f7a
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15796
diff
changeset
|
309 |
! ! |
96f6c9fb9f7a
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15796
diff
changeset
|
310 |
|
20368 | 311 |
|
7861
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
312 |
!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
|
313 |
|
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
314 |
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
|
315 |
"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
|
316 |
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
|
317 |
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
|
318 |
|
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
319 |
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
|
320 |
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
|
321 |
|
8815 | 322 |
|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
|
323 |
|
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
324 |
count == 0 ifTrue:[ |
10327 | 325 |
^ 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
|
326 |
]. |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
327 |
|
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
328 |
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
|
329 |
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
|
330 |
|
10327 | 331 |
[ |
332 |
nRead := self nextAvailableBytes:remaining into:anObject startingAt:offset. |
|
333 |
nRead == 0 ifTrue:[ |
|
334 |
"atEnd does a readWait" |
|
335 |
self atEnd not. |
|
336 |
] ifFalse:[ |
|
337 |
remaining := remaining - nRead. |
|
338 |
offset := offset + nRead. |
|
339 |
remaining ~~ 0. |
|
340 |
] |
|
7861
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
341 |
] whileTrue. |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
342 |
|
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
343 |
^ count - remaining |
20105 | 344 |
! |
345 |
||
346 |
nextInt16MSB:msbFlag |
|
347 |
"redefined to wait for data on pipes and sockets" |
|
348 |
||
349 |
|bytes nRead| |
|
350 |
||
351 |
bytes := ByteArray uninitializedNew:2. |
|
352 |
nRead := self nextBytes:2 into:bytes startingAt:1. |
|
353 |
nRead ~~ 2 ifTrue:[ |
|
354 |
^ self pastEndRead. |
|
355 |
]. |
|
356 |
^ bytes signedInt16At:1 MSB:msbFlag. |
|
357 |
! |
|
358 |
||
359 |
nextInt32MSB:msbFlag |
|
360 |
"redefined to wait for data on pipes and sockets" |
|
361 |
||
362 |
|bytes nRead| |
|
363 |
||
364 |
bytes := ByteArray uninitializedNew:4. |
|
365 |
nRead := self nextBytes:4 into:bytes startingAt:1. |
|
366 |
nRead ~~ 4 ifTrue:[ |
|
367 |
^ self pastEndRead. |
|
368 |
]. |
|
369 |
^ bytes signedInt32At:1 MSB:msbFlag. |
|
370 |
! |
|
371 |
||
21096 | 372 |
nextInt64MSB:msbFlag |
373 |
"redefined to wait for data on pipes and sockets" |
|
374 |
||
375 |
|bytes nRead| |
|
376 |
||
377 |
bytes := ByteArray uninitializedNew:8. |
|
378 |
nRead := self nextBytes:8 into:bytes startingAt:1. |
|
379 |
nRead ~~ 8 ifTrue:[ |
|
380 |
^ self pastEndRead. |
|
381 |
]. |
|
382 |
^ bytes signedInt64At:1 MSB:msbFlag. |
|
383 |
! |
|
384 |
||
20105 | 385 |
nextUnsignedInt16MSB:msbFlag |
386 |
"redefined to wait for data on pipes and sockets" |
|
387 |
||
388 |
|bytes nRead| |
|
389 |
||
21096 | 390 |
bytes := ByteArray uninitializedNew:2. |
20105 | 391 |
nRead := self nextBytes:2 into:bytes startingAt:1. |
392 |
nRead ~~ 2 ifTrue:[ |
|
393 |
^ self pastEndRead. |
|
394 |
]. |
|
395 |
^ bytes unsignedInt16At:1 MSB:msbFlag. |
|
396 |
! |
|
397 |
||
398 |
nextUnsignedInt32MSB:msbFlag |
|
399 |
"redefined to wait for data on pipes and sockets" |
|
400 |
||
401 |
|bytes nRead| |
|
402 |
||
403 |
bytes := ByteArray uninitializedNew:4. |
|
404 |
nRead := self nextBytes:4 into:bytes startingAt:1. |
|
405 |
nRead ~~ 4 ifTrue:[ |
|
406 |
^ self pastEndRead. |
|
407 |
]. |
|
408 |
^ bytes unsignedInt32At:1 MSB:msbFlag. |
|
21096 | 409 |
! |
410 |
||
411 |
nextUnsignedInt64MSB:msbFlag |
|
412 |
"redefined to wait for data on pipes and sockets" |
|
413 |
||
414 |
|bytes nRead| |
|
415 |
||
416 |
bytes := ByteArray uninitializedNew:8. |
|
417 |
nRead := self nextBytes:8 into:bytes startingAt:1. |
|
418 |
nRead ~~ 8 ifTrue:[ |
|
419 |
^ self pastEndRead. |
|
420 |
]. |
|
421 |
^ 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
|
422 |
! ! |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
423 |
|
613 | 424 |
!NonPositionableExternalStream methodsFor:'positioning'! |
425 |
||
426 |
position |
|
20105 | 427 |
position isNil ifTrue:[ |
428 |
^ self positionError. |
|
429 |
]. |
|
430 |
^ position |
|
1 | 431 |
! |
432 |
||
20105 | 433 |
position:newPosition |
434 |
(position isNil or:[newPosition < position]) ifTrue:[ |
|
435 |
"there is no turning back" |
|
436 |
^ self positionError |
|
437 |
]. |
|
438 |
self skip:newPosition - position. |
|
613 | 439 |
! |
44 | 440 |
|
613 | 441 |
skip:numberToSkip |
7241 | 442 |
"skip count bytes/characters, return the receiver. |
443 |
Re-redefined since PositionableStream redefined it." |
|
613 | 444 |
|
16207
923f92b4f0e8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16187
diff
changeset
|
445 |
"don't know how to unread ..." |
613 | 446 |
numberToSkip < 0 ifTrue:[ |
20105 | 447 |
^ self positionError. |
88 | 448 |
]. |
16207
923f92b4f0e8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16187
diff
changeset
|
449 |
numberToSkip timesRepeat:[self next]. |
4467 | 450 |
|
451 |
"Modified: / 30.7.1999 / 12:42:12 / cg" |
|
1 | 452 |
! ! |
453 |
||
177 | 454 |
!NonPositionableExternalStream methodsFor:'printing & storing'! |
455 |
||
456 |
printOn:aStream |
|
2965 | 457 |
"{ Pragma: +optSpace }" |
458 |
||
4392 | 459 |
"append a user printed representation of the receiver to aStream. |
460 |
The format is suitable for a human - not meant to be read back." |
|
177 | 461 |
|
2965 | 462 |
|myName| |
463 |
||
177 | 464 |
self == Stdin ifTrue:[ |
10327 | 465 |
myName := 'Stdin'. |
2965 | 466 |
] ifFalse:[ |
10327 | 467 |
self == Stdout ifTrue:[ |
468 |
myName := 'Stdout'. |
|
469 |
] ifFalse:[ |
|
470 |
self == Stderr ifTrue:[ |
|
471 |
myName := 'Stderr'. |
|
472 |
] |
|
473 |
] |
|
177 | 474 |
]. |
2965 | 475 |
|
476 |
myName notNil ifTrue:[ |
|
10327 | 477 |
aStream nextPutAll:myName. |
478 |
^ self |
|
177 | 479 |
]. |
480 |
super printOn:aStream |
|
481 |
! |
|
482 |
||
483 |
storeOn:aStream |
|
2965 | 484 |
"{ Pragma: +optSpace }" |
485 |
||
213 | 486 |
"append a printed representation of the receiver on aStream, from |
487 |
which the receiver can be reconstructed." |
|
177 | 488 |
|
489 |
((self == Stdin) |
|
490 |
or:[self == Stdout |
|
491 |
or:[self == Stderr]]) ifTrue:[ |
|
492 |
^ self printOn:aStream |
|
493 |
]. |
|
494 |
super storeOn:aStream |
|
495 |
! ! |
|
496 |
||
20680 | 497 |
!NonPositionableExternalStream protectedMethodsFor:'private'! |
498 |
||
499 |
closeFile |
|
500 |
|semasToSignal| |
|
501 |
||
502 |
handle notNil ifTrue:[ |
|
503 |
"make sure, that no select is performed on closed file descriptors" |
|
21056 | 504 |
semasToSignal := Processor disableFd:self fileDescriptor doSignal:false. |
20680 | 505 |
super closeFile. |
506 |
||
507 |
"tell the waiters that they must not wait any longer" |
|
508 |
semasToSignal do:[:eachSema| |
|
509 |
eachSema signalForAll. |
|
510 |
]. |
|
511 |
]. |
|
512 |
! ! |
|
513 |
||
613 | 514 |
!NonPositionableExternalStream methodsFor:'private'! |
360 | 515 |
|
5407 | 516 |
handleForStderr |
517 |
"{ Pragma: +optSpace }" |
|
518 |
||
519 |
"return a stderr handle" |
|
520 |
||
521 |
%{ |
|
18313 | 522 |
#ifdef __SCHTEAM__ |
523 |
return context._RETURN( StandardErrorStream ); |
|
524 |
#else |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19073
diff
changeset
|
525 |
# ifdef __win32__ |
12677
aeab74b04f0b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
12395
diff
changeset
|
526 |
RETURN ( __MKEXTERNALADDRESS( __win32_stderr() )); |
18313 | 527 |
# else |
12677
aeab74b04f0b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
12395
diff
changeset
|
528 |
RETURN ( __MKEXTERNALADDRESS(stderr) ); |
18313 | 529 |
# endif |
10327 | 530 |
#endif |
5407 | 531 |
%} |
532 |
! |
|
533 |
||
534 |
handleForStdin |
|
535 |
"{ Pragma: +optSpace }" |
|
536 |
||
537 |
"return a stdin handle" |
|
538 |
||
539 |
%{ |
|
18313 | 540 |
#ifdef __SCHTEAM__ |
541 |
return context._RETURN( StandardInputStream ); |
|
542 |
#else |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19073
diff
changeset
|
543 |
# ifdef __win32__ |
12677
aeab74b04f0b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
12395
diff
changeset
|
544 |
RETURN ( __MKEXTERNALADDRESS( __win32_stdin() )); |
18313 | 545 |
# else |
12677
aeab74b04f0b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
12395
diff
changeset
|
546 |
RETURN ( __MKEXTERNALADDRESS(stdin) ); |
18313 | 547 |
# endif |
10327 | 548 |
#endif |
5407 | 549 |
%} |
550 |
! |
|
551 |
||
552 |
handleForStdout |
|
553 |
"{ Pragma: +optSpace }" |
|
554 |
||
555 |
"return a stdout handle" |
|
556 |
||
557 |
%{ |
|
18313 | 558 |
#ifdef __SCHTEAM__ |
559 |
return context._RETURN( StandardOutputStream ); |
|
560 |
#else |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19073
diff
changeset
|
561 |
# ifdef __win32__ |
12677
aeab74b04f0b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
12395
diff
changeset
|
562 |
RETURN ( __MKEXTERNALADDRESS( __win32_stdout() )); |
18313 | 563 |
# else |
12677
aeab74b04f0b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
12395
diff
changeset
|
564 |
RETURN ( __MKEXTERNALADDRESS(stdout) ); |
18313 | 565 |
# endif |
10327 | 566 |
#endif |
5407 | 567 |
%} |
568 |
! |
|
569 |
||
613 | 570 |
initializeForStderr |
2965 | 571 |
"{ Pragma: +optSpace }" |
572 |
||
613 | 573 |
"setup for writing to stderr" |
269 | 574 |
|
20256 | 575 |
mode := #writeonly. "since stderr may be redirected to a pipe, reading is forbidden" |
613 | 576 |
buffered := false. |
20105 | 577 |
position := 0. "only writing - can keep track of position" |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11907
diff
changeset
|
578 |
handle := self handleForStderr. |
12754 | 579 |
handleType := #filePointer. |
5407 | 580 |
OperatingSystem isMSWINDOWSlike ifTrue:[ |
20105 | 581 |
eolMode := #crlf |
5407 | 582 |
] |
613 | 583 |
! |
1 | 584 |
|
585 |
initializeForStdin |
|
2965 | 586 |
"{ Pragma: +optSpace }" |
587 |
||
44 | 588 |
"setup for reading stdin" |
589 |
||
1 | 590 |
mode := #readonly. |
6843
ef53f65b7364
stdout/err must not be buffered (sending prompts without NL)
Claus Gittinger <cg@exept.de>
parents:
5918
diff
changeset
|
591 |
"/ buffered := true. |
ef53f65b7364
stdout/err must not be buffered (sending prompts without NL)
Claus Gittinger <cg@exept.de>
parents:
5918
diff
changeset
|
592 |
buffered := false. |
20105 | 593 |
position := 0. "only reading - can keep track of position" |
12754 | 594 |
handleType := #filePointer. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11907
diff
changeset
|
595 |
handle := self handleForStdin. |
1 | 596 |
! |
597 |
||
598 |
initializeForStdout |
|
2965 | 599 |
"{ Pragma: +optSpace }" |
600 |
||
44 | 601 |
"setup for writing to stdout" |
602 |
||
20105 | 603 |
mode := #writeonly. "since stdout may be redirected to a pipe, reading is forbidden" |
10 | 604 |
buffered := false. |
20105 | 605 |
position := 0. "only writing - can keep track of position" |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11907
diff
changeset
|
606 |
handle := self handleForStdout. |
12754 | 607 |
handleType := #filePointer. |
5407 | 608 |
OperatingSystem isMSWINDOWSlike ifTrue:[ |
20105 | 609 |
eolMode := #crlf |
5407 | 610 |
] |
1 | 611 |
! |
612 |
||
613 |
reOpen |
|
2965 | 614 |
"{ Pragma: +optSpace }" |
615 |
||
44 | 616 |
"reopen the stream after an image restart. |
617 |
If I am one of the standard streams, reopen is easy" |
|
1 | 618 |
|
477
8710aba7876b
oops - making id's real objects requires a store macro
Claus Gittinger <cg@exept.de>
parents:
475
diff
changeset
|
619 |
(self == StdInStream) ifTrue:[ |
177 | 620 |
^ self initializeForStdin |
1 | 621 |
]. |
477
8710aba7876b
oops - making id's real objects requires a store macro
Claus Gittinger <cg@exept.de>
parents:
475
diff
changeset
|
622 |
(self == StdOutStream) ifTrue:[ |
177 | 623 |
^ self initializeForStdout |
1 | 624 |
]. |
477
8710aba7876b
oops - making id's real objects requires a store macro
Claus Gittinger <cg@exept.de>
parents:
475
diff
changeset
|
625 |
(self == StdErrorStream) ifTrue:[ |
177 | 626 |
^ self initializeForStderr |
1 | 627 |
]. |
628 |
^ super reOpen |
|
629 |
! ! |
|
613 | 630 |
|
631 |
!NonPositionableExternalStream methodsFor:'queries'! |
|
632 |
||
5851
6823cc642d60
only do readWait for pipes and sockets.
Claus Gittinger <cg@exept.de>
parents:
5407
diff
changeset
|
633 |
atEnd |
21028 | 634 |
"return true, if position is at end. |
635 |
Notice: this is a blocking operation, as we do not know in advance, |
|
636 |
if there will be anything to read |
|
637 |
(i.e. if the partner will send more or close the stream). |
|
638 |
If you want to check for available data, |
|
639 |
use nextAvailable:, or canReadWithoutBlocking" |
|
5851
6823cc642d60
only do readWait for pipes and sockets.
Claus Gittinger <cg@exept.de>
parents:
5407
diff
changeset
|
640 |
|
5918 | 641 |
(self == StdInStream) ifTrue:[ |
20225 | 642 |
OperatingSystem hasConsole ifFalse:[ |
643 |
^ true |
|
644 |
] |
|
5918 | 645 |
]. |
646 |
||
20685 | 647 |
handle notNil ifTrue:[ |
648 |
"first, wait to avoid blocking on the read. |
|
649 |
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
|
650 |
|
20685 | 651 |
self readWaitWithTimeoutMs:nil. |
652 |
]. |
|
20225 | 653 |
handle isNil ifTrue:[ |
20685 | 654 |
"we are closed or were closed while waiting - so we are at the end" |
20225 | 655 |
^ true. |
656 |
]. |
|
5851
6823cc642d60
only do readWait for pipes and sockets.
Claus Gittinger <cg@exept.de>
parents:
5407
diff
changeset
|
657 |
^ super atEnd. |
6823cc642d60
only do readWait for pipes and sockets.
Claus Gittinger <cg@exept.de>
parents:
5407
diff
changeset
|
658 |
! |
6823cc642d60
only do readWait for pipes and sockets.
Claus Gittinger <cg@exept.de>
parents:
5407
diff
changeset
|
659 |
|
17607
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
660 |
collectionSize |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
661 |
"we do not know our size" |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
662 |
|
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
663 |
^ self positionError |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
664 |
! |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
665 |
|
5407 | 666 |
isPositionable |
667 |
"return true, if the stream supports positioning (this one is not)" |
|
668 |
||
669 |
^ false |
|
11455 | 670 |
! |
671 |
||
17607
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
672 |
remainingSize |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
673 |
"we do not know our size" |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
674 |
|
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
675 |
^ self positionError |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
676 |
! |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
677 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11907
diff
changeset
|
678 |
size |
11455 | 679 |
"we do not know our size" |
680 |
||
681 |
^ self positionError |
|
613 | 682 |
! ! |
683 |
||
10349
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
684 |
!NonPositionableExternalStream methodsFor:'reading'! |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
685 |
|
14758
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
686 |
next |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
687 |
"return the next element, if available. |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
688 |
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
|
689 |
Instead, nil is returned immediately. |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
690 |
|
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
691 |
Redefined, to wait on pipes and sockets" |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
692 |
|
18798 | 693 |
self readWaitWithTimeoutMs:nil. |
14758
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
694 |
^ super next |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
695 |
! |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
696 |
|
20105 | 697 |
nextByte |
698 |
"redefined, to wait on pipes and sockets" |
|
15796
b3fdd3c2aec2
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
15720
diff
changeset
|
699 |
|
18798 | 700 |
self readWaitWithTimeoutMs:nil. |
20105 | 701 |
^ super nextByte |
702 |
! |
|
703 |
||
704 |
nextLine |
|
705 |
"return the characters upTo (but excluding) the next cr (carriage return) |
|
706 |
character (i.e. read a single line of text). |
|
707 |
If the previous-to-last character is a cr, this is also removed, |
|
708 |
so it's possible to read alien (i.e. ms-dos) text as well. |
|
709 |
Redefined to not block forever if no cr is read." |
|
710 |
||
20175 | 711 |
|answerStream char wasBinary available "{ Class:SmallInteger }"| |
20105 | 712 |
|
20112 | 713 |
answerStream := WriteStream on:(String uninitializedNew:80). |
20105 | 714 |
[ |
21131 | 715 |
"#atEnd waits for new data or EOF" |
716 |
self atEnd ifTrue:[ |
|
717 |
answerStream size == 0 ifTrue:[ |
|
718 |
^ self pastEndRead. |
|
719 |
]. |
|
720 |
^ answerStream contents. |
|
721 |
]. |
|
20105 | 722 |
available := self numAvailableForRead. |
21131 | 723 |
available == 0 ifTrue:[ |
724 |
"so #atEnd didn't wait above, but no data is present - check for error" |
|
725 |
self peek. |
|
726 |
available := self numAvailableForRead. |
|
727 |
]. |
|
728 |
||
20113 | 729 |
wasBinary := binary. "temporarily set to text mode" |
730 |
binary := false. |
|
20105 | 731 |
available timesRepeat:[ |
20148 | 732 |
char := super nextOrNil. "don't wait - we know that there is data" |
20105 | 733 |
char == Character cr ifTrue:[ |
734 |
(answerStream size ~~ 0 and:[answerStream last == Character return]) ifTrue:[ |
|
735 |
answerStream backStep. |
|
736 |
]. |
|
20113 | 737 |
binary := wasBinary. |
20105 | 738 |
^ answerStream contents. |
739 |
]. |
|
20256 | 740 |
char notNil ifTrue:[ |
20148 | 741 |
answerStream nextPut:char. |
742 |
]. |
|
20105 | 743 |
]. |
20113 | 744 |
binary := wasBinary. "restore saved mode" |
20105 | 745 |
] loop. |
746 |
||
747 |
"not reached" |
|
15796
b3fdd3c2aec2
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
15720
diff
changeset
|
748 |
! |
b3fdd3c2aec2
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
15720
diff
changeset
|
749 |
|
10349
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
750 |
nextOrNil |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
751 |
"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
|
752 |
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
|
753 |
Instead, nil is returned immediately. |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
754 |
|
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
755 |
Redefined, to wait on pipes and sockets" |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
756 |
|
20680 | 757 |
self readWaitWithTimeoutMs:nil. |
758 |
handle isNil ifTrue:[ |
|
759 |
"we were closed while waiting - so we are at the end" |
|
760 |
^ nil. |
|
761 |
]. |
|
14758
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
762 |
^ super nextOrNil |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
763 |
! |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
764 |
|
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
765 |
peek |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
766 |
"Redefined, to wait on pipes and sockets" |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
767 |
|
18798 | 768 |
self readWaitWithTimeoutMs:nil. |
14758
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
769 |
^ super peek |
10349
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
770 |
! |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
771 |
|
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
772 |
peekOrNil |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
773 |
"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
|
774 |
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
|
775 |
Instead, nil is returned immediately. |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
776 |
|
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
777 |
Redefined, to wait on pipes and sockets" |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
778 |
|
20680 | 779 |
self readWaitWithTimeoutMs:nil. |
780 |
handle isNil ifTrue:[ |
|
781 |
"we were closed while waiting - so we are at the end" |
|
782 |
^ nil. |
|
783 |
]. |
|
10349
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
784 |
^ self peek |
18798 | 785 |
! |
786 |
||
20105 | 787 |
upTo:anObject into:aStream |
788 |
"read a collection of all objects up-to anObject and append these |
|
20256 | 789 |
elements to aStream, but excluding anObject. |
20105 | 790 |
The next read operation will return the element after anObject. |
791 |
(i.e. anObject is considered a separator, which is skipped) |
|
792 |
Similar to #through:, but the matching object is not included in the returned collection. |
|
793 |
If anObject is not encountered, all elements up to the end are read and returned. |
|
794 |
Compare this with #through: which also reads up to some object |
|
795 |
and also positions behind it, but DOES include it in the returned value." |
|
796 |
||
797 |
|element available "{ Class:SmallInteger }"| |
|
18798 | 798 |
|
20105 | 799 |
[ |
21131 | 800 |
"#atEnd waits for new data or EOF" |
801 |
self atEnd ifTrue:[ |
|
802 |
^ self. |
|
803 |
]. |
|
20105 | 804 |
available := self numAvailableForRead. |
21131 | 805 |
available == 0 ifTrue:[ |
806 |
"so #atEnd didn't wait above, but no data is present - check for error" |
|
807 |
self peek. |
|
808 |
available := self numAvailableForRead. |
|
809 |
]. |
|
810 |
||
20105 | 811 |
available timesRepeat:[ |
812 |
element := super next. "we know that there is data" |
|
813 |
element = anObject ifTrue:[ |
|
814 |
^ self. |
|
815 |
]. |
|
816 |
aStream nextPut:element. |
|
817 |
]. |
|
818 |
] loop. |
|
819 |
||
820 |
"not reached" |
|
10349
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
821 |
! ! |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
822 |
|
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
823 |
!NonPositionableExternalStream methodsFor:'writing'! |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
824 |
|
17642
5988beff84f7
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
17607
diff
changeset
|
825 |
nextPutAll:aCollection |
5988beff84f7
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
17607
diff
changeset
|
826 |
"nextPutBytes handles non-blocking io in receiver" |
5988beff84f7
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
17607
diff
changeset
|
827 |
|
5988beff84f7
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
17607
diff
changeset
|
828 |
self nextPutBytes:aCollection size from:aCollection startingAt:1. |
5988beff84f7
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
17607
diff
changeset
|
829 |
^ self. |
5988beff84f7
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
17607
diff
changeset
|
830 |
! |
5988beff84f7
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
17607
diff
changeset
|
831 |
|
17607
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
832 |
nextPutAll:aCollection startingAt:start to:stop |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
833 |
"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
|
834 |
|
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
835 |
|count| |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
836 |
|
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
837 |
count := stop-start+1. |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
838 |
count ~= (self nextPutBytes:count from:aCollection startingAt:start) ifTrue:[ |
18313 | 839 |
"incomplete write" |
840 |
self writeError. |
|
17607
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
841 |
]. |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
842 |
! |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
843 |
|
7679 | 844 |
nextPutBytes:initialWriteCount from:buffer startingAt:initialOffset |
845 |
"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
|
846 |
|
17607
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
847 |
|offset remaining wasBlocking| |
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
848 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
849 |
offset := initialOffset. |
7679 | 850 |
remaining := initialWriteCount. |
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
851 |
|
7679 | 852 |
wasBlocking := self blocking:false. |
10327 | 853 |
[remaining ~~ 0] whileTrue:[ |
18313 | 854 |
|count| |
17607
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
855 |
|
18313 | 856 |
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
|
857 |
|
18313 | 858 |
remaining := remaining - count. |
859 |
offset := offset + count. |
|
860 |
remaining ~~ 0 ifTrue:[ |
|
861 |
"Transcript showCR:'writeWait'." |
|
862 |
self writeWait. |
|
863 |
]. |
|
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
864 |
]. |
7679 | 865 |
wasBlocking ifTrue:[self blocking:true]. |
866 |
||
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
867 |
^ offset - initialOffset. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
868 |
! ! |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
869 |
|
1295 | 870 |
!NonPositionableExternalStream class methodsFor:'documentation'! |
871 |
||
872 |
version |
|
18798 | 873 |
^ '$Header$' |
12677
aeab74b04f0b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
12395
diff
changeset
|
874 |
! |
aeab74b04f0b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
12395
diff
changeset
|
875 |
|
aeab74b04f0b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
12395
diff
changeset
|
876 |
version_CVS |
18798 | 877 |
^ '$Header$' |
1295 | 878 |
! ! |
17607
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
879 |