author | Stefan Vogel <sv@exept.de> |
Fri, 11 Aug 2017 12:14:02 +0200 | |
changeset 22194 | 41775bf87e61 |
parent 22193 | 023c7585d304 |
child 22541 | b8acf94e540b |
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:'' |
21702 | 18 |
classVariableNames:'StdErrorStream StdInStream StdOutStream' |
1295 | 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 |
||
22192 | 275 |
errorNotOpen |
276 |
"report an error, that the stream has not been opened or has been closed. |
|
277 |
Redefined to care for closed stdout and stderr, |
|
278 |
which happens, if the console is closed while I am running. |
|
279 |
In this case, we ignore the error." |
|
280 |
||
281 |
((self == Stderr) or:[self == Stdout]) ifTrue:[ |
|
282 |
^ self |
|
283 |
]. |
|
284 |
^ super errorNotOpen |
|
285 |
! |
|
286 |
||
613 | 287 |
positionError |
2965 | 288 |
"{ Pragma: +optSpace }" |
289 |
||
613 | 290 |
"notify that this stream has no concept of a position" |
291 |
||
8320
229adaeaf183
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
7861
diff
changeset
|
292 |
^ PositionError raiseRequestWith:self |
4559 | 293 |
|
294 |
" |
|
295 |
Stderr positionError |
|
296 |
" |
|
19073 | 297 |
! |
298 |
||
299 |
writeError:errorNumber |
|
20256 | 300 |
"report an error, that some write error occurred. |
19073 | 301 |
Redefined to care for writeErrors on stdout and stderr, |
302 |
which happens under linux, if the console is closed while I am running. |
|
303 |
In this case, we ignore the error." |
|
304 |
||
305 |
((self == Stderr) or:[self == Stdout]) ifTrue:[ |
|
20256 | 306 |
^ self |
19073 | 307 |
]. |
308 |
super writeError:errorNumber |
|
613 | 309 |
! ! |
310 |
||
15919
96f6c9fb9f7a
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15796
diff
changeset
|
311 |
!NonPositionableExternalStream methodsFor:'initialization'! |
96f6c9fb9f7a
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15796
diff
changeset
|
312 |
|
96f6c9fb9f7a
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15796
diff
changeset
|
313 |
initialize |
16340
6203956d63ee
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16207
diff
changeset
|
314 |
"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
|
315 |
|
96f6c9fb9f7a
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15796
diff
changeset
|
316 |
super initialize. |
20105 | 317 |
position := nil. "I am not positionable (reading and writing simultaneously)" |
15922 | 318 |
buffered := false. |
15919
96f6c9fb9f7a
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15796
diff
changeset
|
319 |
! ! |
96f6c9fb9f7a
Initialize in instance initialize method insted in #new
Stefan Vogel <sv@exept.de>
parents:
15796
diff
changeset
|
320 |
|
20368 | 321 |
|
7861
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
322 |
!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
|
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 |
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
|
325 |
"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
|
326 |
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
|
327 |
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
|
328 |
|
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
329 |
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
|
330 |
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
|
331 |
|
8815 | 332 |
|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
|
333 |
|
21750 | 334 |
count == 0 ifTrue:[ ^ 0 ]. |
7861
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
335 |
|
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
336 |
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
|
337 |
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
|
338 |
|
10327 | 339 |
[ |
21750 | 340 |
nRead := self nextAvailableBytes:remaining into:anObject startingAt:offset. |
341 |
nRead == 0 ifTrue:[ |
|
342 |
"atEnd does a readWait" |
|
343 |
(self atEnd) ifTrue:[^ count - remaining] |
|
344 |
] ifFalse:[ |
|
345 |
remaining := remaining - nRead. |
|
346 |
(remaining == 0) ifTrue:[^ count]. |
|
347 |
offset := offset + nRead. |
|
348 |
] |
|
349 |
] loop. |
|
7861
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
350 |
|
21750 | 351 |
"Modified (format): / 16-05-2017 / 12:10:25 / cg" |
20105 | 352 |
! |
353 |
||
354 |
nextInt16MSB:msbFlag |
|
355 |
"redefined to wait for data on pipes and sockets" |
|
356 |
||
357 |
|bytes nRead| |
|
358 |
||
359 |
bytes := ByteArray uninitializedNew:2. |
|
360 |
nRead := self nextBytes:2 into:bytes startingAt:1. |
|
361 |
nRead ~~ 2 ifTrue:[ |
|
362 |
^ self pastEndRead. |
|
363 |
]. |
|
364 |
^ bytes signedInt16At:1 MSB:msbFlag. |
|
365 |
! |
|
366 |
||
367 |
nextInt32MSB:msbFlag |
|
368 |
"redefined to wait for data on pipes and sockets" |
|
369 |
||
370 |
|bytes nRead| |
|
371 |
||
372 |
bytes := ByteArray uninitializedNew:4. |
|
373 |
nRead := self nextBytes:4 into:bytes startingAt:1. |
|
374 |
nRead ~~ 4 ifTrue:[ |
|
375 |
^ self pastEndRead. |
|
376 |
]. |
|
377 |
^ bytes signedInt32At:1 MSB:msbFlag. |
|
378 |
! |
|
379 |
||
21096 | 380 |
nextInt64MSB:msbFlag |
381 |
"redefined to wait for data on pipes and sockets" |
|
382 |
||
383 |
|bytes nRead| |
|
384 |
||
385 |
bytes := ByteArray uninitializedNew:8. |
|
386 |
nRead := self nextBytes:8 into:bytes startingAt:1. |
|
387 |
nRead ~~ 8 ifTrue:[ |
|
388 |
^ self pastEndRead. |
|
389 |
]. |
|
390 |
^ bytes signedInt64At:1 MSB:msbFlag. |
|
391 |
! |
|
392 |
||
20105 | 393 |
nextUnsignedInt16MSB:msbFlag |
394 |
"redefined to wait for data on pipes and sockets" |
|
395 |
||
396 |
|bytes nRead| |
|
397 |
||
21096 | 398 |
bytes := ByteArray uninitializedNew:2. |
20105 | 399 |
nRead := self nextBytes:2 into:bytes startingAt:1. |
400 |
nRead ~~ 2 ifTrue:[ |
|
401 |
^ self pastEndRead. |
|
402 |
]. |
|
403 |
^ bytes unsignedInt16At:1 MSB:msbFlag. |
|
404 |
! |
|
405 |
||
406 |
nextUnsignedInt32MSB:msbFlag |
|
407 |
"redefined to wait for data on pipes and sockets" |
|
408 |
||
409 |
|bytes nRead| |
|
410 |
||
411 |
bytes := ByteArray uninitializedNew:4. |
|
412 |
nRead := self nextBytes:4 into:bytes startingAt:1. |
|
413 |
nRead ~~ 4 ifTrue:[ |
|
414 |
^ self pastEndRead. |
|
415 |
]. |
|
416 |
^ bytes unsignedInt32At:1 MSB:msbFlag. |
|
21096 | 417 |
! |
418 |
||
419 |
nextUnsignedInt64MSB:msbFlag |
|
420 |
"redefined to wait for data on pipes and sockets" |
|
421 |
||
422 |
|bytes nRead| |
|
423 |
||
424 |
bytes := ByteArray uninitializedNew:8. |
|
425 |
nRead := self nextBytes:8 into:bytes startingAt:1. |
|
426 |
nRead ~~ 8 ifTrue:[ |
|
427 |
^ self pastEndRead. |
|
428 |
]. |
|
429 |
^ 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
|
430 |
! ! |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
431 |
|
613 | 432 |
!NonPositionableExternalStream methodsFor:'positioning'! |
433 |
||
434 |
position |
|
20105 | 435 |
position isNil ifTrue:[ |
436 |
^ self positionError. |
|
437 |
]. |
|
438 |
^ position |
|
1 | 439 |
! |
440 |
||
20105 | 441 |
position:newPosition |
442 |
(position isNil or:[newPosition < position]) ifTrue:[ |
|
443 |
"there is no turning back" |
|
444 |
^ self positionError |
|
445 |
]. |
|
446 |
self skip:newPosition - position. |
|
613 | 447 |
! |
44 | 448 |
|
613 | 449 |
skip:numberToSkip |
7241 | 450 |
"skip count bytes/characters, return the receiver. |
451 |
Re-redefined since PositionableStream redefined it." |
|
613 | 452 |
|
16207
923f92b4f0e8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16187
diff
changeset
|
453 |
"don't know how to unread ..." |
613 | 454 |
numberToSkip < 0 ifTrue:[ |
20105 | 455 |
^ self positionError. |
88 | 456 |
]. |
16207
923f92b4f0e8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16187
diff
changeset
|
457 |
numberToSkip timesRepeat:[self next]. |
4467 | 458 |
|
459 |
"Modified: / 30.7.1999 / 12:42:12 / cg" |
|
1 | 460 |
! ! |
461 |
||
177 | 462 |
!NonPositionableExternalStream methodsFor:'printing & storing'! |
463 |
||
464 |
printOn:aStream |
|
2965 | 465 |
"{ Pragma: +optSpace }" |
466 |
||
4392 | 467 |
"append a user printed representation of the receiver to aStream. |
468 |
The format is suitable for a human - not meant to be read back." |
|
177 | 469 |
|
2965 | 470 |
|myName| |
471 |
||
177 | 472 |
self == Stdin ifTrue:[ |
10327 | 473 |
myName := 'Stdin'. |
2965 | 474 |
] ifFalse:[ |
10327 | 475 |
self == Stdout ifTrue:[ |
476 |
myName := 'Stdout'. |
|
477 |
] ifFalse:[ |
|
478 |
self == Stderr ifTrue:[ |
|
479 |
myName := 'Stderr'. |
|
480 |
] |
|
481 |
] |
|
177 | 482 |
]. |
2965 | 483 |
|
484 |
myName notNil ifTrue:[ |
|
10327 | 485 |
aStream nextPutAll:myName. |
486 |
^ self |
|
177 | 487 |
]. |
488 |
super printOn:aStream |
|
489 |
! |
|
490 |
||
491 |
storeOn:aStream |
|
2965 | 492 |
"{ Pragma: +optSpace }" |
493 |
||
213 | 494 |
"append a printed representation of the receiver on aStream, from |
495 |
which the receiver can be reconstructed." |
|
177 | 496 |
|
497 |
((self == Stdin) |
|
498 |
or:[self == Stdout |
|
499 |
or:[self == Stderr]]) ifTrue:[ |
|
500 |
^ self printOn:aStream |
|
501 |
]. |
|
502 |
super storeOn:aStream |
|
503 |
! ! |
|
504 |
||
20680 | 505 |
!NonPositionableExternalStream protectedMethodsFor:'private'! |
506 |
||
507 |
closeFile |
|
21687 | 508 |
<resource: #PRIVATE> |
509 |
||
20680 | 510 |
|semasToSignal| |
511 |
||
512 |
handle notNil ifTrue:[ |
|
513 |
"make sure, that no select is performed on closed file descriptors" |
|
21239 | 514 |
semasToSignal := Processor disableFd:self fileHandle doSignal:false. |
20680 | 515 |
super closeFile. |
516 |
||
517 |
"tell the waiters that they must not wait any longer" |
|
518 |
semasToSignal do:[:eachSema| |
|
519 |
eachSema signalForAll. |
|
520 |
]. |
|
521 |
]. |
|
21687 | 522 |
|
523 |
"Modified: / 07-04-2017 / 14:32:00 / cg" |
|
20680 | 524 |
! ! |
525 |
||
613 | 526 |
!NonPositionableExternalStream methodsFor:'private'! |
360 | 527 |
|
5407 | 528 |
handleForStderr |
529 |
"{ Pragma: +optSpace }" |
|
530 |
||
531 |
"return a stderr handle" |
|
532 |
||
533 |
%{ |
|
18313 | 534 |
#ifdef __SCHTEAM__ |
535 |
return context._RETURN( StandardErrorStream ); |
|
536 |
#else |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19073
diff
changeset
|
537 |
# ifdef __win32__ |
12677
aeab74b04f0b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
12395
diff
changeset
|
538 |
RETURN ( __MKEXTERNALADDRESS( __win32_stderr() )); |
18313 | 539 |
# else |
12677
aeab74b04f0b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
12395
diff
changeset
|
540 |
RETURN ( __MKEXTERNALADDRESS(stderr) ); |
18313 | 541 |
# endif |
10327 | 542 |
#endif |
5407 | 543 |
%} |
544 |
! |
|
545 |
||
546 |
handleForStdin |
|
547 |
"{ Pragma: +optSpace }" |
|
548 |
||
549 |
"return a stdin handle" |
|
550 |
||
551 |
%{ |
|
18313 | 552 |
#ifdef __SCHTEAM__ |
553 |
return context._RETURN( StandardInputStream ); |
|
554 |
#else |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19073
diff
changeset
|
555 |
# ifdef __win32__ |
12677
aeab74b04f0b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
12395
diff
changeset
|
556 |
RETURN ( __MKEXTERNALADDRESS( __win32_stdin() )); |
18313 | 557 |
# else |
12677
aeab74b04f0b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
12395
diff
changeset
|
558 |
RETURN ( __MKEXTERNALADDRESS(stdin) ); |
18313 | 559 |
# endif |
10327 | 560 |
#endif |
5407 | 561 |
%} |
562 |
! |
|
563 |
||
564 |
handleForStdout |
|
565 |
"{ Pragma: +optSpace }" |
|
566 |
||
567 |
"return a stdout handle" |
|
568 |
||
569 |
%{ |
|
18313 | 570 |
#ifdef __SCHTEAM__ |
571 |
return context._RETURN( StandardOutputStream ); |
|
572 |
#else |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19073
diff
changeset
|
573 |
# ifdef __win32__ |
12677
aeab74b04f0b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
12395
diff
changeset
|
574 |
RETURN ( __MKEXTERNALADDRESS( __win32_stdout() )); |
18313 | 575 |
# else |
12677
aeab74b04f0b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
12395
diff
changeset
|
576 |
RETURN ( __MKEXTERNALADDRESS(stdout) ); |
18313 | 577 |
# endif |
10327 | 578 |
#endif |
5407 | 579 |
%} |
580 |
! |
|
581 |
||
613 | 582 |
initializeForStderr |
2965 | 583 |
"{ Pragma: +optSpace }" |
584 |
||
613 | 585 |
"setup for writing to stderr" |
269 | 586 |
|
20256 | 587 |
mode := #writeonly. "since stderr may be redirected to a pipe, reading is forbidden" |
21701 | 588 |
binary := false. |
613 | 589 |
buffered := false. |
20105 | 590 |
position := 0. "only writing - can keep track of position" |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11907
diff
changeset
|
591 |
handle := self handleForStderr. |
12754 | 592 |
handleType := #filePointer. |
21702 | 593 |
hitEOF := false. |
594 |
self initializeEOLMode. |
|
5407 | 595 |
OperatingSystem isMSWINDOWSlike ifTrue:[ |
20105 | 596 |
eolMode := #crlf |
5407 | 597 |
] |
21701 | 598 |
|
21702 | 599 |
"Modified: / 25-04-2017 / 02:13:56 / cg" |
613 | 600 |
! |
1 | 601 |
|
602 |
initializeForStdin |
|
2965 | 603 |
"{ Pragma: +optSpace }" |
604 |
||
44 | 605 |
"setup for reading stdin" |
606 |
||
1 | 607 |
mode := #readonly. |
21701 | 608 |
binary := false. |
6843
ef53f65b7364
stdout/err must not be buffered (sending prompts without NL)
Claus Gittinger <cg@exept.de>
parents:
5918
diff
changeset
|
609 |
"/ buffered := true. |
ef53f65b7364
stdout/err must not be buffered (sending prompts without NL)
Claus Gittinger <cg@exept.de>
parents:
5918
diff
changeset
|
610 |
buffered := false. |
20105 | 611 |
position := 0. "only reading - can keep track of position" |
12754 | 612 |
handleType := #filePointer. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11907
diff
changeset
|
613 |
handle := self handleForStdin. |
21702 | 614 |
hitEOF := false. |
615 |
self initializeEOLMode. |
|
21701 | 616 |
|
21702 | 617 |
"Modified: / 25-04-2017 / 02:14:22 / cg" |
1 | 618 |
! |
619 |
||
620 |
initializeForStdout |
|
2965 | 621 |
"{ Pragma: +optSpace }" |
622 |
||
44 | 623 |
"setup for writing to stdout" |
624 |
||
20105 | 625 |
mode := #writeonly. "since stdout may be redirected to a pipe, reading is forbidden" |
21701 | 626 |
binary := false. |
10 | 627 |
buffered := false. |
20105 | 628 |
position := 0. "only writing - can keep track of position" |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11907
diff
changeset
|
629 |
handle := self handleForStdout. |
12754 | 630 |
handleType := #filePointer. |
21702 | 631 |
self initializeEOLMode. |
632 |
hitEOF := false. |
|
5407 | 633 |
OperatingSystem isMSWINDOWSlike ifTrue:[ |
20105 | 634 |
eolMode := #crlf |
5407 | 635 |
] |
21701 | 636 |
|
21702 | 637 |
"Modified: / 25-04-2017 / 02:14:27 / cg" |
1 | 638 |
! |
639 |
||
640 |
reOpen |
|
2965 | 641 |
"{ Pragma: +optSpace }" |
642 |
||
44 | 643 |
"reopen the stream after an image restart. |
644 |
If I am one of the standard streams, reopen is easy" |
|
1 | 645 |
|
477
8710aba7876b
oops - making id's real objects requires a store macro
Claus Gittinger <cg@exept.de>
parents:
475
diff
changeset
|
646 |
(self == StdInStream) ifTrue:[ |
177 | 647 |
^ self initializeForStdin |
1 | 648 |
]. |
477
8710aba7876b
oops - making id's real objects requires a store macro
Claus Gittinger <cg@exept.de>
parents:
475
diff
changeset
|
649 |
(self == StdOutStream) ifTrue:[ |
177 | 650 |
^ self initializeForStdout |
1 | 651 |
]. |
477
8710aba7876b
oops - making id's real objects requires a store macro
Claus Gittinger <cg@exept.de>
parents:
475
diff
changeset
|
652 |
(self == StdErrorStream) ifTrue:[ |
177 | 653 |
^ self initializeForStderr |
1 | 654 |
]. |
655 |
^ super reOpen |
|
656 |
! ! |
|
613 | 657 |
|
658 |
!NonPositionableExternalStream methodsFor:'queries'! |
|
659 |
||
5851
6823cc642d60
only do readWait for pipes and sockets.
Claus Gittinger <cg@exept.de>
parents:
5407
diff
changeset
|
660 |
atEnd |
21028 | 661 |
"return true, if position is at end. |
662 |
Notice: this is a blocking operation, as we do not know in advance, |
|
663 |
if there will be anything to read |
|
664 |
(i.e. if the partner will send more or close the stream). |
|
665 |
If you want to check for available data, |
|
666 |
use nextAvailable:, or canReadWithoutBlocking" |
|
5851
6823cc642d60
only do readWait for pipes and sockets.
Claus Gittinger <cg@exept.de>
parents:
5407
diff
changeset
|
667 |
|
5918 | 668 |
(self == StdInStream) ifTrue:[ |
20225 | 669 |
OperatingSystem hasConsole ifFalse:[ |
670 |
^ true |
|
671 |
] |
|
5918 | 672 |
]. |
673 |
||
20685 | 674 |
handle notNil ifTrue:[ |
675 |
"first, wait to avoid blocking on the read. |
|
676 |
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
|
677 |
|
20685 | 678 |
self readWaitWithTimeoutMs:nil. |
679 |
]. |
|
20225 | 680 |
handle isNil ifTrue:[ |
20685 | 681 |
"we are closed or were closed while waiting - so we are at the end" |
20225 | 682 |
^ true. |
683 |
]. |
|
5851
6823cc642d60
only do readWait for pipes and sockets.
Claus Gittinger <cg@exept.de>
parents:
5407
diff
changeset
|
684 |
^ super atEnd. |
6823cc642d60
only do readWait for pipes and sockets.
Claus Gittinger <cg@exept.de>
parents:
5407
diff
changeset
|
685 |
! |
6823cc642d60
only do readWait for pipes and sockets.
Claus Gittinger <cg@exept.de>
parents:
5407
diff
changeset
|
686 |
|
17607
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
687 |
collectionSize |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
688 |
"we do not know our size" |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
689 |
|
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
690 |
^ self positionError |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
691 |
! |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
692 |
|
5407 | 693 |
isPositionable |
694 |
"return true, if the stream supports positioning (this one is not)" |
|
695 |
||
696 |
^ false |
|
11455 | 697 |
! |
698 |
||
17607
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
699 |
remainingSize |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
700 |
"we do not know our size" |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
701 |
|
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
702 |
^ self positionError |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
703 |
! |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
704 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11907
diff
changeset
|
705 |
size |
11455 | 706 |
"we do not know our size" |
707 |
||
708 |
^ self positionError |
|
613 | 709 |
! ! |
710 |
||
10349
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
711 |
!NonPositionableExternalStream methodsFor:'reading'! |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
712 |
|
14758
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
713 |
next |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
714 |
"return the next element, if available. |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
715 |
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
|
716 |
Instead, nil is returned immediately. |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
717 |
|
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
718 |
Redefined, to wait on pipes and sockets" |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
719 |
|
18798 | 720 |
self readWaitWithTimeoutMs:nil. |
14758
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
721 |
^ super next |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
722 |
! |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
723 |
|
20105 | 724 |
nextByte |
725 |
"redefined, to wait on pipes and sockets" |
|
15796
b3fdd3c2aec2
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
15720
diff
changeset
|
726 |
|
18798 | 727 |
self readWaitWithTimeoutMs:nil. |
20105 | 728 |
^ super nextByte |
729 |
! |
|
730 |
||
731 |
nextLine |
|
732 |
"return the characters upTo (but excluding) the next cr (carriage return) |
|
733 |
character (i.e. read a single line of text). |
|
734 |
If the previous-to-last character is a cr, this is also removed, |
|
735 |
so it's possible to read alien (i.e. ms-dos) text as well. |
|
736 |
Redefined to not block forever if no cr is read." |
|
737 |
||
20175 | 738 |
|answerStream char wasBinary available "{ Class:SmallInteger }"| |
20105 | 739 |
|
20112 | 740 |
answerStream := WriteStream on:(String uninitializedNew:80). |
20105 | 741 |
[ |
21131 | 742 |
"#atEnd waits for new data or EOF" |
743 |
self atEnd ifTrue:[ |
|
744 |
answerStream size == 0 ifTrue:[ |
|
745 |
^ self pastEndRead. |
|
746 |
]. |
|
747 |
^ answerStream contents. |
|
748 |
]. |
|
20105 | 749 |
available := self numAvailableForRead. |
21131 | 750 |
available == 0 ifTrue:[ |
751 |
"so #atEnd didn't wait above, but no data is present - check for error" |
|
752 |
self peek. |
|
753 |
available := self numAvailableForRead. |
|
754 |
]. |
|
755 |
||
20113 | 756 |
wasBinary := binary. "temporarily set to text mode" |
757 |
binary := false. |
|
20105 | 758 |
available timesRepeat:[ |
20148 | 759 |
char := super nextOrNil. "don't wait - we know that there is data" |
20105 | 760 |
char == Character cr ifTrue:[ |
761 |
(answerStream size ~~ 0 and:[answerStream last == Character return]) ifTrue:[ |
|
762 |
answerStream backStep. |
|
763 |
]. |
|
20113 | 764 |
binary := wasBinary. |
20105 | 765 |
^ answerStream contents. |
766 |
]. |
|
20256 | 767 |
char notNil ifTrue:[ |
20148 | 768 |
answerStream nextPut:char. |
769 |
]. |
|
20105 | 770 |
]. |
20113 | 771 |
binary := wasBinary. "restore saved mode" |
20105 | 772 |
] loop. |
773 |
||
774 |
"not reached" |
|
15796
b3fdd3c2aec2
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
15720
diff
changeset
|
775 |
! |
b3fdd3c2aec2
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
15720
diff
changeset
|
776 |
|
10349
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
777 |
nextOrNil |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
778 |
"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
|
779 |
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
|
780 |
Instead, nil is returned immediately. |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
781 |
|
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
782 |
Redefined, to wait on pipes and sockets" |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
783 |
|
20680 | 784 |
self readWaitWithTimeoutMs:nil. |
785 |
handle isNil ifTrue:[ |
|
786 |
"we were closed while waiting - so we are at the end" |
|
787 |
^ nil. |
|
788 |
]. |
|
14758
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
789 |
^ super nextOrNil |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
790 |
! |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
791 |
|
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
792 |
peek |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
793 |
"Redefined, to wait on pipes and sockets" |
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
794 |
|
18798 | 795 |
self readWaitWithTimeoutMs:nil. |
14758
6a70a7006093
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
12754
diff
changeset
|
796 |
^ super peek |
10349
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
797 |
! |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
798 |
|
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
799 |
peekOrNil |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
800 |
"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
|
801 |
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
|
802 |
Instead, nil is returned immediately. |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
803 |
|
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
804 |
Redefined, to wait on pipes and sockets" |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
805 |
|
20680 | 806 |
self readWaitWithTimeoutMs:nil. |
807 |
handle isNil ifTrue:[ |
|
808 |
"we were closed while waiting - so we are at the end" |
|
809 |
^ nil. |
|
810 |
]. |
|
10349
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
811 |
^ self peek |
18798 | 812 |
! |
813 |
||
20105 | 814 |
upTo:anObject into:aStream |
815 |
"read a collection of all objects up-to anObject and append these |
|
20256 | 816 |
elements to aStream, but excluding anObject. |
20105 | 817 |
The next read operation will return the element after anObject. |
818 |
(i.e. anObject is considered a separator, which is skipped) |
|
819 |
Similar to #through:, but the matching object is not included in the returned collection. |
|
820 |
If anObject is not encountered, all elements up to the end are read and returned. |
|
821 |
Compare this with #through: which also reads up to some object |
|
822 |
and also positions behind it, but DOES include it in the returned value." |
|
823 |
||
824 |
|element available "{ Class:SmallInteger }"| |
|
18798 | 825 |
|
20105 | 826 |
[ |
21131 | 827 |
"#atEnd waits for new data or EOF" |
828 |
self atEnd ifTrue:[ |
|
829 |
^ self. |
|
830 |
]. |
|
20105 | 831 |
available := self numAvailableForRead. |
21131 | 832 |
available == 0 ifTrue:[ |
833 |
"so #atEnd didn't wait above, but no data is present - check for error" |
|
834 |
self peek. |
|
835 |
available := self numAvailableForRead. |
|
836 |
]. |
|
837 |
||
20105 | 838 |
available timesRepeat:[ |
839 |
element := super next. "we know that there is data" |
|
840 |
element = anObject ifTrue:[ |
|
841 |
^ self. |
|
842 |
]. |
|
843 |
aStream nextPut:element. |
|
844 |
]. |
|
845 |
] loop. |
|
846 |
||
847 |
"not reached" |
|
10349
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
848 |
! ! |
cbfa00bba11d
Speedup ExternalStream>>#nextOrNil and ExternalStream>>#peekOrNil.
Stefan Vogel <sv@exept.de>
parents:
10327
diff
changeset
|
849 |
|
21361 | 850 |
!NonPositionableExternalStream methodsFor:'testing'! |
851 |
||
852 |
canReadWithoutBlocking |
|
853 |
"return true, if any data is available for reading (i.e. |
|
854 |
a read operation will not block the smalltalk process), false otherwise. |
|
855 |
We know, that error conditions do not block, so return true for errors." |
|
856 |
||
857 |
^ readAhead notNil |
|
858 |
or:[handle isNil |
|
859 |
or:[mode == #writeonly |
|
860 |
or:[OperatingSystem readCheck:self fileHandle]]] |
|
861 |
||
862 |
" |
|
863 |
|pipe| |
|
864 |
||
865 |
pipe := PipeStream readingFrom:'(sleep 10; echo hello)'. |
|
866 |
pipe canReadWithoutBlocking ifTrue:[ |
|
867 |
Transcript showCR:'data available' |
|
868 |
] ifFalse:[ |
|
869 |
Transcript showCR:'no data available' |
|
870 |
]. |
|
871 |
pipe close |
|
872 |
" |
|
873 |
||
874 |
"Modified: 25.9.1997 / 13:08:45 / stefan" |
|
875 |
! |
|
876 |
||
877 |
canWriteWithoutBlocking |
|
878 |
"return true, if data can be written into the stream |
|
879 |
(i.e. a write operation will not block the smalltalk process). |
|
880 |
We know, that error conditions do not block, so return true for errors." |
|
881 |
||
882 |
^ handle isNil |
|
883 |
or:[mode == #readonly |
|
884 |
or:[OperatingSystem writeCheck:self fileHandle]] |
|
885 |
! ! |
|
886 |
||
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
887 |
!NonPositionableExternalStream methodsFor:'writing'! |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
888 |
|
17642
5988beff84f7
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
17607
diff
changeset
|
889 |
nextPutAll:aCollection |
5988beff84f7
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
17607
diff
changeset
|
890 |
"nextPutBytes handles non-blocking io in receiver" |
5988beff84f7
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
17607
diff
changeset
|
891 |
|
5988beff84f7
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
17607
diff
changeset
|
892 |
self nextPutBytes:aCollection size from:aCollection startingAt:1. |
5988beff84f7
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
17607
diff
changeset
|
893 |
^ self. |
5988beff84f7
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
17607
diff
changeset
|
894 |
! |
5988beff84f7
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
17607
diff
changeset
|
895 |
|
17607
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
896 |
nextPutAll:aCollection startingAt:start to:stop |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
897 |
"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
|
898 |
|
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
899 |
|count| |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
900 |
|
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
901 |
count := stop-start+1. |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
902 |
count ~= (self nextPutBytes:count from:aCollection startingAt:start) ifTrue:[ |
18313 | 903 |
"incomplete write" |
904 |
self writeError. |
|
17607
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
905 |
]. |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
906 |
! |
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
907 |
|
7679 | 908 |
nextPutBytes:initialWriteCount from:buffer startingAt:initialOffset |
909 |
"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
|
910 |
|
17607
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
911 |
|offset remaining wasBlocking| |
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
912 |
|
22194 | 913 |
wasBlocking := self blocking:false. |
22193 | 914 |
handle isNil ifTrue:[ |
22194 | 915 |
"self blocking: above may set handle to nil!!" |
22193 | 916 |
self errorNotOpen. |
917 |
(self == Stderr or:[self == Stdout]) ifTrue:[ |
|
918 |
"Care for closed stdout and stderr, |
|
919 |
which happens, if the console is closed while I am running. |
|
920 |
In this case, we ignore the error." |
|
921 |
^ initialWriteCount. |
|
922 |
]. |
|
923 |
^ 0. |
|
924 |
]. |
|
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
925 |
|
22194 | 926 |
offset := initialOffset. |
927 |
remaining := initialWriteCount. |
|
928 |
||
10327 | 929 |
[remaining ~~ 0] whileTrue:[ |
22193 | 930 |
|count| |
17607
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
931 |
|
22193 | 932 |
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
|
933 |
|
22193 | 934 |
remaining := remaining - count. |
935 |
offset := offset + count. |
|
936 |
remaining ~~ 0 ifTrue:[ |
|
937 |
"Transcript showCR:'writeWait'." |
|
938 |
self writeWait. |
|
939 |
]. |
|
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
940 |
]. |
7679 | 941 |
wasBlocking ifTrue:[self blocking:true]. |
942 |
||
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
943 |
^ offset - initialOffset. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
944 |
! ! |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
945 |
|
1295 | 946 |
!NonPositionableExternalStream class methodsFor:'documentation'! |
947 |
||
948 |
version |
|
18798 | 949 |
^ '$Header$' |
12677
aeab74b04f0b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
12395
diff
changeset
|
950 |
! |
aeab74b04f0b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
12395
diff
changeset
|
951 |
|
aeab74b04f0b
Use __MKEXTERNALADDRESS() instead of __MKOBJ()
Stefan Vogel <sv@exept.de>
parents:
12395
diff
changeset
|
952 |
version_CVS |
18798 | 953 |
^ '$Header$' |
1295 | 954 |
! ! |
17607
224f253b60c8
class: NonPositionableExternalStream
Stefan Vogel <sv@exept.de>
parents:
16348
diff
changeset
|
955 |