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