author | Stefan Vogel <sv@exept.de> |
Tue, 08 Nov 2005 12:08:47 +0100 | |
changeset 8973 | c715c59b633d |
parent 8815 | 88c21d3f2c9c |
child 10327 | c7bb805fa32c |
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 |
" |
|
12 |
||
5407 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
1344
32a51164b237
No longer a subclass of UnboundedExternalStream, now subclass of ExternalStream.
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
15 |
ExternalStream subclass:#NonPositionableExternalStream |
1295 | 16 |
instanceVariableNames:'' |
17 |
classVariableNames:'StdInStream StdOutStream StdErrorStream' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Streams-External' |
|
1 | 20 |
! |
21 |
||
613 | 22 |
!NonPositionableExternalStream primitiveDefinitions! |
23 |
%{ |
|
2896 | 24 |
|
5918 | 25 |
#ifndef _STDIO_H_INCLUDED_ |
26 |
# include <stdio.h> |
|
27 |
# define _STDIO_H_INCLUDED_ |
|
28 |
#endif |
|
3956
51f1a9a4d63f
changes for egcs (stdio uses __new / utsname)
Claus Gittinger <cg@exept.de>
parents:
3808
diff
changeset
|
29 |
|
613 | 30 |
%} |
31 |
||
32 |
! ! |
|
33 |
||
88 | 34 |
!NonPositionableExternalStream class methodsFor:'documentation'! |
35 |
||
36 |
copyright |
|
37 |
" |
|
38 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
177 | 39 |
All Rights Reserved |
88 | 40 |
|
41 |
This software is furnished under a license and may be used |
|
42 |
only in accordance with the terms of that license and with the |
|
43 |
inclusion of the above copyright notice. This software may not |
|
44 |
be provided or otherwise made available to, or used by, any |
|
45 |
other person. No title to or ownership of the software is |
|
46 |
hereby transferred. |
|
47 |
" |
|
48 |
! |
|
49 |
||
50 |
documentation |
|
51 |
" |
|
52 |
This class provides common protocol for all non-positionable, |
|
53 |
external streams. Concrete subclasses are terminal streams, pipe streams, |
|
54 |
PrinterStreams, Sockets etc. |
|
55 |
||
56 |
There are three special instances of this class, representing stdin, |
|
57 |
stdout and stderr of the smalltalk/X process (see Unix manuals, if you |
|
177 | 58 |
dont know what those are used for). These special streams are bound to |
59 |
to globals Stdin, Stdout and Stderr at early initialization time |
|
60 |
(see Smalltalk>>initializeStandardStreams). |
|
159 | 61 |
|
62 |
The name of this class is a historical leftover - it should be called |
|
63 |
'TTYStream' or similar. |
|
1295 | 64 |
|
65 |
[author:] |
|
3808
5a7866138026
set eolMode of stderr & stdout to #crlf under win32
Claus Gittinger <cg@exept.de>
parents:
3173
diff
changeset
|
66 |
Claus Gittinger |
88 | 67 |
" |
216 | 68 |
! ! |
69 |
||
1 | 70 |
!NonPositionableExternalStream class methodsFor:'instance creation'! |
71 |
||
613 | 72 |
forStderr |
2965 | 73 |
"{ Pragma: +optSpace }" |
74 |
||
613 | 75 |
"return a NonPositionableExternalStream object for writing to |
76 |
Unixes standard error output file descriptor" |
|
77 |
||
78 |
StdErrorStream isNil ifTrue:[ |
|
79 |
StdErrorStream := self basicNew initializeForStderr |
|
80 |
]. |
|
81 |
^ StdErrorStream |
|
82 |
! |
|
83 |
||
1 | 84 |
forStdin |
2965 | 85 |
"{ Pragma: +optSpace }" |
86 |
||
44 | 87 |
"return a NonPositionableExternalStream object for reading from |
88 |
Unixes standard input file descriptor" |
|
89 |
||
88 | 90 |
StdInStream isNil ifTrue:[ |
177 | 91 |
StdInStream := self basicNew initializeForStdin |
88 | 92 |
]. |
93 |
^ StdInStream |
|
1 | 94 |
! |
95 |
||
96 |
forStdout |
|
2965 | 97 |
"{ Pragma: +optSpace }" |
98 |
||
44 | 99 |
"return a NonPositionableExternalStream object for writing to |
100 |
Unixes standard output file descriptor" |
|
101 |
||
88 | 102 |
StdOutStream isNil ifTrue:[ |
177 | 103 |
StdOutStream := self basicNew initializeForStdout |
88 | 104 |
]. |
105 |
^ StdOutStream |
|
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
106 |
! |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
107 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
108 |
makePTYPair |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
109 |
"return an array with two streams - the first one is the master, |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
110 |
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
|
111 |
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
|
112 |
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
|
113 |
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
|
114 |
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
|
115 |
(which returns an array of file-descriptors)." |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
116 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
117 |
|ptyPair master slave| |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
118 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
119 |
ptyPair := OperatingSystem makePTYPair. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
120 |
ptyPair notNil ifTrue:[ |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
121 |
master := self forReadWriteToFileDescriptor:(ptyPair at:1). |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
122 |
master buffered:false. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
123 |
slave := self forReadWriteToFileDescriptor:(ptyPair at:2). |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
124 |
slave buffered:false. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
125 |
ptyPair at:1 put:master. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
126 |
ptyPair at:2 put:slave. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
127 |
^ ptyPair |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
128 |
]. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
129 |
^ nil |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
130 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
131 |
" |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
132 |
ExternalStream makePTYPair. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
133 |
" |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
134 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
135 |
"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
|
136 |
! |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
137 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
138 |
makePipe |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
139 |
"return an array with two streams - the first one for reading, |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
140 |
the second for writing. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
141 |
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
|
142 |
(which returns an array of file-descriptors)." |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
143 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
144 |
|pipe rs ws| |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
145 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
146 |
pipe := OperatingSystem makePipe. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
147 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
148 |
pipe notNil ifTrue:[ |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
149 |
rs := self forReadingFromFileDescriptor:(pipe at:1). |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
150 |
rs buffered:false. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
151 |
ws := self forWritingToFileDescriptor:(pipe at:2). |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
152 |
ws buffered:false. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
153 |
^ Array with:rs with:ws |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
154 |
]. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
155 |
^ nil |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
156 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
157 |
" |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
158 |
|pipe rs ws| |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
159 |
|
7541 | 160 |
pipe := NonPositionableExternalStream makePipe. |
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
161 |
rs := pipe at:1. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
162 |
ws := pipe at:2. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
163 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
164 |
'read ...'. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
165 |
[ |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
166 |
1 to:10 do:[:i | |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
167 |
Transcript showCR:rs nextLine |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
168 |
]. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
169 |
rs close. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
170 |
] forkAt:7. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
171 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
172 |
'write ...'. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
173 |
[ |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
174 |
1 to:10 do:[:i | |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
175 |
ws nextPutAll:'hello world '; nextPutAll:i printString; cr |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
176 |
]. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
177 |
ws close. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
178 |
] fork. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
179 |
" |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
180 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
181 |
"Modified: 29.2.1996 / 18:28:36 / cg" |
613 | 182 |
! ! |
183 |
||
184 |
!NonPositionableExternalStream methodsFor:'error handling'! |
|
185 |
||
186 |
positionError |
|
2965 | 187 |
"{ Pragma: +optSpace }" |
188 |
||
613 | 189 |
"notify that this stream has no concept of a position" |
190 |
||
8320
229adaeaf183
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
7861
diff
changeset
|
191 |
^ PositionError raiseRequestWith:self |
4559 | 192 |
|
193 |
" |
|
194 |
Stderr positionError |
|
195 |
" |
|
613 | 196 |
! ! |
197 |
||
7861
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
198 |
!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
|
199 |
|
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
200 |
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
|
201 |
"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
|
202 |
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
|
203 |
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
|
204 |
|
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
205 |
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
|
206 |
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
|
207 |
|
8815 | 208 |
|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
|
209 |
|
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
210 |
count == 0 ifTrue:[ |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
211 |
^ 0. |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
212 |
]. |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
213 |
|
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
214 |
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
|
215 |
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
|
216 |
|
8815 | 217 |
[ |
7861
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
218 |
nRead := self nextAvailableBytes:remaining into:anObject startingAt:offset. |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
219 |
nRead == 0 ifTrue:[ |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
220 |
"atEnd does a readWait" |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
221 |
self atEnd not. |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
222 |
] ifFalse:[ |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
223 |
remaining := remaining - nRead. |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
224 |
offset := offset + nRead. |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
225 |
remaining ~~ 0. |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
226 |
] |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
227 |
] whileTrue. |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
228 |
|
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
229 |
^ count - remaining |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
230 |
! ! |
9c2cd42e4667
Provide a #nextBytes:into:startingAt: that does not block the whole ST/X
Stefan Vogel <sv@exept.de>
parents:
7682
diff
changeset
|
231 |
|
613 | 232 |
!NonPositionableExternalStream methodsFor:'positioning'! |
233 |
||
234 |
position |
|
2965 | 235 |
"{ Pragma: +optSpace }" |
236 |
||
613 | 237 |
"catch position - there is none here" |
238 |
||
239 |
^ self positionError |
|
1 | 240 |
! |
241 |
||
613 | 242 |
position:aPosition |
2965 | 243 |
"{ Pragma: +optSpace }" |
244 |
||
613 | 245 |
"catch position - there is none here" |
246 |
||
247 |
^ self positionError |
|
248 |
! |
|
44 | 249 |
|
613 | 250 |
skip:numberToSkip |
7241 | 251 |
"skip count bytes/characters, return the receiver. |
252 |
Re-redefined since PositionableStream redefined it." |
|
613 | 253 |
|
254 |
"dont know how to unread ..." |
|
255 |
numberToSkip < 0 ifTrue:[ |
|
8320
229adaeaf183
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
7861
diff
changeset
|
256 |
PositionError raiseRequest. |
4467 | 257 |
^ self |
88 | 258 |
]. |
613 | 259 |
numberToSkip timesRepeat:self next |
4467 | 260 |
|
261 |
"Modified: / 30.7.1999 / 12:42:12 / cg" |
|
3173
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
262 |
! |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
263 |
|
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
264 |
skipThroughAll:aCollection |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
265 |
"skip for and through the sequence given by the argument, aCollection; |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
266 |
return nil if not found, the receiver otherwise. |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
267 |
On a successful match, the next read will return elements after aCollection; |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
268 |
if no match was found, the receiver will be positioned at the end. |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
269 |
Redefined to be the same as Stream>>#skipThroughAll, to undo |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
270 |
the redefinition from PositionableStream" |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
271 |
|
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
272 |
|buffer l first idx| |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
273 |
|
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
274 |
l := aCollection size. |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
275 |
first := aCollection at:1. |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
276 |
[self atEnd] whileFalse:[ |
3808
5a7866138026
set eolMode of stderr & stdout to #crlf under win32
Claus Gittinger <cg@exept.de>
parents:
3173
diff
changeset
|
277 |
buffer isNil ifTrue:[ |
5a7866138026
set eolMode of stderr & stdout to #crlf under win32
Claus Gittinger <cg@exept.de>
parents:
3173
diff
changeset
|
278 |
buffer := self nextAvailable:l. |
5a7866138026
set eolMode of stderr & stdout to #crlf under win32
Claus Gittinger <cg@exept.de>
parents:
3173
diff
changeset
|
279 |
]. |
5a7866138026
set eolMode of stderr & stdout to #crlf under win32
Claus Gittinger <cg@exept.de>
parents:
3173
diff
changeset
|
280 |
buffer = aCollection ifTrue:[ |
5a7866138026
set eolMode of stderr & stdout to #crlf under win32
Claus Gittinger <cg@exept.de>
parents:
3173
diff
changeset
|
281 |
^ self |
5a7866138026
set eolMode of stderr & stdout to #crlf under win32
Claus Gittinger <cg@exept.de>
parents:
3173
diff
changeset
|
282 |
]. |
5a7866138026
set eolMode of stderr & stdout to #crlf under win32
Claus Gittinger <cg@exept.de>
parents:
3173
diff
changeset
|
283 |
idx := buffer indexOf:first startingAt:2. |
5a7866138026
set eolMode of stderr & stdout to #crlf under win32
Claus Gittinger <cg@exept.de>
parents:
3173
diff
changeset
|
284 |
idx == 0 ifTrue:[ |
5a7866138026
set eolMode of stderr & stdout to #crlf under win32
Claus Gittinger <cg@exept.de>
parents:
3173
diff
changeset
|
285 |
buffer := nil |
5a7866138026
set eolMode of stderr & stdout to #crlf under win32
Claus Gittinger <cg@exept.de>
parents:
3173
diff
changeset
|
286 |
] ifFalse:[ |
5a7866138026
set eolMode of stderr & stdout to #crlf under win32
Claus Gittinger <cg@exept.de>
parents:
3173
diff
changeset
|
287 |
buffer := (buffer copyFrom:idx) , (self nextAvailable:(idx - 1)) |
5a7866138026
set eolMode of stderr & stdout to #crlf under win32
Claus Gittinger <cg@exept.de>
parents:
3173
diff
changeset
|
288 |
] |
3173
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
289 |
]. |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
290 |
^ nil |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
291 |
|
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
292 |
" |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
293 |
|s| |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
294 |
s := ReadStream on:'12345678901234567890'. |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
295 |
s skipThroughAll:'901'. |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
296 |
s upToEnd |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
297 |
" |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
298 |
" |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
299 |
|s| |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
300 |
s := ReadStream on:'12345678901234567890'. |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
301 |
s skipThroughAll:'1234'. |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
302 |
s upToEnd |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
303 |
" |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
304 |
" |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
305 |
|s| |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
306 |
s := ReadStream on:'12345678901234567890'. |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
307 |
s skipThroughAll:'999'. |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
308 |
s atEnd |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
309 |
" |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
310 |
|
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
311 |
"Modified: / 11.1.1997 / 19:09:06 / cg" |
664f2ffa7493
Redefine #skipThroughAll: to not use #position.
Stefan Vogel <sv@exept.de>
parents:
2965
diff
changeset
|
312 |
"Created: / 15.1.1998 / 23:33:37 / stefan" |
1 | 313 |
! ! |
314 |
||
177 | 315 |
!NonPositionableExternalStream methodsFor:'printing & storing'! |
316 |
||
317 |
printOn:aStream |
|
2965 | 318 |
"{ Pragma: +optSpace }" |
319 |
||
4392 | 320 |
"append a user printed representation of the receiver to aStream. |
321 |
The format is suitable for a human - not meant to be read back." |
|
177 | 322 |
|
2965 | 323 |
|myName| |
324 |
||
177 | 325 |
self == Stdin ifTrue:[ |
4392 | 326 |
myName := 'Stdin'. |
2965 | 327 |
] ifFalse:[ |
4392 | 328 |
self == Stdout ifTrue:[ |
329 |
myName := 'Stdout'. |
|
330 |
] ifFalse:[ |
|
331 |
self == Stderr ifTrue:[ |
|
332 |
myName := 'Stderr'. |
|
333 |
] |
|
334 |
] |
|
177 | 335 |
]. |
2965 | 336 |
|
337 |
myName notNil ifTrue:[ |
|
4392 | 338 |
aStream nextPutAll:myName. |
339 |
^ self |
|
177 | 340 |
]. |
341 |
super printOn:aStream |
|
342 |
! |
|
343 |
||
344 |
storeOn:aStream |
|
2965 | 345 |
"{ Pragma: +optSpace }" |
346 |
||
213 | 347 |
"append a printed representation of the receiver on aStream, from |
348 |
which the receiver can be reconstructed." |
|
177 | 349 |
|
350 |
((self == Stdin) |
|
351 |
or:[self == Stdout |
|
352 |
or:[self == Stderr]]) ifTrue:[ |
|
353 |
^ self printOn:aStream |
|
354 |
]. |
|
355 |
super storeOn:aStream |
|
356 |
! ! |
|
357 |
||
613 | 358 |
!NonPositionableExternalStream methodsFor:'private'! |
360 | 359 |
|
5407 | 360 |
handleForStderr |
361 |
"{ Pragma: +optSpace }" |
|
362 |
||
363 |
"return a stderr handle" |
|
364 |
||
365 |
%{ |
|
366 |
RETURN ( __MKOBJ(stderr) ); |
|
367 |
%} |
|
368 |
! |
|
369 |
||
370 |
handleForStdin |
|
371 |
"{ Pragma: +optSpace }" |
|
372 |
||
373 |
"return a stdin handle" |
|
374 |
||
375 |
%{ |
|
376 |
RETURN ( __MKOBJ(stdin) ); |
|
377 |
%} |
|
378 |
! |
|
379 |
||
380 |
handleForStdout |
|
381 |
"{ Pragma: +optSpace }" |
|
382 |
||
383 |
"return a stdout handle" |
|
384 |
||
385 |
%{ |
|
386 |
RETURN ( __MKOBJ(stdout) ); |
|
387 |
%} |
|
388 |
! |
|
389 |
||
613 | 390 |
initializeForStderr |
2965 | 391 |
"{ Pragma: +optSpace }" |
392 |
||
613 | 393 |
"setup for writing to stderr" |
269 | 394 |
|
613 | 395 |
mode := #readwrite. |
396 |
buffered := false. |
|
5407 | 397 |
filePointer := self handleForStderr. |
398 |
OperatingSystem isMSWINDOWSlike ifTrue:[ |
|
399 |
eolMode := #crlf |
|
400 |
] |
|
613 | 401 |
! |
1 | 402 |
|
403 |
initializeForStdin |
|
2965 | 404 |
"{ Pragma: +optSpace }" |
405 |
||
44 | 406 |
"setup for reading stdin" |
407 |
||
1 | 408 |
mode := #readonly. |
6843
ef53f65b7364
stdout/err must not be buffered (sending prompts without NL)
Claus Gittinger <cg@exept.de>
parents:
5918
diff
changeset
|
409 |
"/ buffered := true. |
ef53f65b7364
stdout/err must not be buffered (sending prompts without NL)
Claus Gittinger <cg@exept.de>
parents:
5918
diff
changeset
|
410 |
buffered := false. |
5407 | 411 |
filePointer := self handleForStdin. |
1 | 412 |
! |
413 |
||
414 |
initializeForStdout |
|
2965 | 415 |
"{ Pragma: +optSpace }" |
416 |
||
44 | 417 |
"setup for writing to stdout" |
418 |
||
1 | 419 |
mode := #readwrite. |
10 | 420 |
buffered := false. |
5407 | 421 |
filePointer := self handleForStdout. |
422 |
OperatingSystem isMSWINDOWSlike ifTrue:[ |
|
423 |
eolMode := #crlf |
|
424 |
] |
|
1 | 425 |
! |
426 |
||
427 |
reOpen |
|
2965 | 428 |
"{ Pragma: +optSpace }" |
429 |
||
44 | 430 |
"reopen the stream after an image restart. |
431 |
If I am one of the standard streams, reopen is easy" |
|
1 | 432 |
|
477
8710aba7876b
oops - making id's real objects requires a store macro
Claus Gittinger <cg@exept.de>
parents:
475
diff
changeset
|
433 |
(self == StdInStream) ifTrue:[ |
177 | 434 |
^ self initializeForStdin |
1 | 435 |
]. |
477
8710aba7876b
oops - making id's real objects requires a store macro
Claus Gittinger <cg@exept.de>
parents:
475
diff
changeset
|
436 |
(self == StdOutStream) ifTrue:[ |
177 | 437 |
^ self initializeForStdout |
1 | 438 |
]. |
477
8710aba7876b
oops - making id's real objects requires a store macro
Claus Gittinger <cg@exept.de>
parents:
475
diff
changeset
|
439 |
(self == StdErrorStream) ifTrue:[ |
177 | 440 |
^ self initializeForStderr |
1 | 441 |
]. |
442 |
^ super reOpen |
|
443 |
! ! |
|
613 | 444 |
|
445 |
!NonPositionableExternalStream methodsFor:'queries'! |
|
446 |
||
5851
6823cc642d60
only do readWait for pipes and sockets.
Claus Gittinger <cg@exept.de>
parents:
5407
diff
changeset
|
447 |
atEnd |
6823cc642d60
only do readWait for pipes and sockets.
Claus Gittinger <cg@exept.de>
parents:
5407
diff
changeset
|
448 |
"return true, if position is at end" |
6823cc642d60
only do readWait for pipes and sockets.
Claus Gittinger <cg@exept.de>
parents:
5407
diff
changeset
|
449 |
|
5918 | 450 |
(self == StdInStream) ifTrue:[ |
451 |
OperatingSystem hasConsole ifFalse:[ |
|
452 |
^ true |
|
453 |
] |
|
454 |
]. |
|
455 |
||
5851
6823cc642d60
only do readWait for pipes and sockets.
Claus Gittinger <cg@exept.de>
parents:
5407
diff
changeset
|
456 |
"first, wait to avoid blocking on the read. |
6823cc642d60
only do readWait for pipes and sockets.
Claus Gittinger <cg@exept.de>
parents:
5407
diff
changeset
|
457 |
On end of stream or error, readWait will return" |
6823cc642d60
only do readWait for pipes and sockets.
Claus Gittinger <cg@exept.de>
parents:
5407
diff
changeset
|
458 |
|
6823cc642d60
only do readWait for pipes and sockets.
Claus Gittinger <cg@exept.de>
parents:
5407
diff
changeset
|
459 |
self readWait. |
6823cc642d60
only do readWait for pipes and sockets.
Claus Gittinger <cg@exept.de>
parents:
5407
diff
changeset
|
460 |
^ super atEnd. |
6823cc642d60
only do readWait for pipes and sockets.
Claus Gittinger <cg@exept.de>
parents:
5407
diff
changeset
|
461 |
! |
6823cc642d60
only do readWait for pipes and sockets.
Claus Gittinger <cg@exept.de>
parents:
5407
diff
changeset
|
462 |
|
4962
0023029d2522
allow Transcript current if Transcript is stdErr
Stefan Vogel <sv@exept.de>
parents:
4559
diff
changeset
|
463 |
current |
0023029d2522
allow Transcript current if Transcript is stdErr
Stefan Vogel <sv@exept.de>
parents:
4559
diff
changeset
|
464 |
"for compatibility with Transcript - allow Transcript current, |
0023029d2522
allow Transcript current if Transcript is stdErr
Stefan Vogel <sv@exept.de>
parents:
4559
diff
changeset
|
465 |
even if redirected to the standardError" |
0023029d2522
allow Transcript current if Transcript is stdErr
Stefan Vogel <sv@exept.de>
parents:
4559
diff
changeset
|
466 |
|
0023029d2522
allow Transcript current if Transcript is stdErr
Stefan Vogel <sv@exept.de>
parents:
4559
diff
changeset
|
467 |
self == Transcript ifTrue:[ |
0023029d2522
allow Transcript current if Transcript is stdErr
Stefan Vogel <sv@exept.de>
parents:
4559
diff
changeset
|
468 |
^ self |
0023029d2522
allow Transcript current if Transcript is stdErr
Stefan Vogel <sv@exept.de>
parents:
4559
diff
changeset
|
469 |
]. |
0023029d2522
allow Transcript current if Transcript is stdErr
Stefan Vogel <sv@exept.de>
parents:
4559
diff
changeset
|
470 |
^ super current |
5407 | 471 |
! |
472 |
||
473 |
isPositionable |
|
474 |
"return true, if the stream supports positioning (this one is not)" |
|
475 |
||
476 |
^ false |
|
613 | 477 |
! ! |
478 |
||
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
479 |
!NonPositionableExternalStream methodsFor:'writing'! |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
480 |
|
7679 | 481 |
nextPutBytes:initialWriteCount from:buffer startingAt:initialOffset |
482 |
"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
|
483 |
|
7682
5d43745f59ac
only do a writeWait if previous non-blocking write failed to
Claus Gittinger <cg@exept.de>
parents:
7679
diff
changeset
|
484 |
|count offset remaining wasBlocking| |
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
485 |
|
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
486 |
offset := initialOffset. |
7679 | 487 |
remaining := initialWriteCount. |
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
488 |
|
7679 | 489 |
wasBlocking := self blocking:false. |
7682
5d43745f59ac
only do a writeWait if previous non-blocking write failed to
Claus Gittinger <cg@exept.de>
parents:
7679
diff
changeset
|
490 |
[remaining ~~ 0] whileTrue:[ |
7679 | 491 |
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
|
492 |
count ~~ remaining ifTrue:[ self writeWait. ]. |
5d43745f59ac
only do a writeWait if previous non-blocking write failed to
Claus Gittinger <cg@exept.de>
parents:
7679
diff
changeset
|
493 |
|
7679 | 494 |
remaining := remaining - count. |
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
495 |
offset := offset + count. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
496 |
]. |
7679 | 497 |
wasBlocking ifTrue:[self blocking:true]. |
498 |
||
7497
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
499 |
^ offset - initialOffset. |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
500 |
! ! |
cc29afd51151
Do not block when writing to pipes
Stefan Vogel <sv@exept.de>
parents:
7241
diff
changeset
|
501 |
|
1295 | 502 |
!NonPositionableExternalStream class methodsFor:'documentation'! |
503 |
||
504 |
version |
|
8815 | 505 |
^ '$Header: /cvs/stx/stx/libbasic/NonPositionableExternalStream.st,v 1.51 2005-04-04 10:15:54 cg Exp $' |
1295 | 506 |
! ! |