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