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