| 1 |
/* |
| 2 |
* tclIO.c -- |
| 3 |
* |
| 4 |
* This file provides the generic portions (those that are the same on |
| 5 |
* all platforms and for all channel types) of Tcl's IO facilities. |
| 6 |
* |
| 7 |
* Copyright (c) 1998-2000 Ajuba Solutions |
| 8 |
* Copyright (c) 1995-1997 Sun Microsystems, Inc. |
| 9 |
* Contributions from Don Porter, NIST, 2014. (not subject to US copyright) |
| 10 |
* |
| 11 |
* See the file "license.terms" for information on usage and redistribution of |
| 12 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 13 |
*/ |
| 14 |
|
| 15 |
#include "tclInt.h" |
| 16 |
#include "tclIO.h" |
| 17 |
#include <assert.h> |
| 18 |
|
| 19 |
/* |
| 20 |
* For each channel handler registered in a call to Tcl_CreateChannelHandler, |
| 21 |
* there is one record of the following type. All of records for a specific |
| 22 |
* channel are chained together in a singly linked list which is stored in |
| 23 |
* the channel structure. |
| 24 |
*/ |
| 25 |
|
| 26 |
typedef struct ChannelHandler { |
| 27 |
Channel *chanPtr; /* The channel structure for this channel. */ |
| 28 |
int mask; /* Mask of desired events. */ |
| 29 |
Tcl_ChannelProc *proc; /* Procedure to call in the type of |
| 30 |
* Tcl_CreateChannelHandler. */ |
| 31 |
void *clientData; /* Argument to pass to procedure. */ |
| 32 |
struct ChannelHandler *nextPtr; |
| 33 |
/* Next one in list of registered handlers. */ |
| 34 |
} ChannelHandler; |
| 35 |
|
| 36 |
/* |
| 37 |
* This structure keeps track of the current ChannelHandler being invoked in |
| 38 |
* the current invocation of Tcl_NotifyChannel. There is a potential |
| 39 |
* problem if a ChannelHandler is deleted while it is the current one, since |
| 40 |
* Tcl_NotifyChannel needs to look at the nextPtr field. To handle this |
| 41 |
* problem, structures of the type below indicate the next handler to be |
| 42 |
* processed for any (recursively nested) dispatches in progress. The |
| 43 |
* nextHandlerPtr field is updated if the handler being pointed to is deleted. |
| 44 |
* The nestedHandlerPtr field is used to chain together all recursive |
| 45 |
* invocations, so that Tcl_DeleteChannelHandler can find all the recursively |
| 46 |
* nested invocations of Tcl_NotifyChannel and compare the handler being |
| 47 |
* deleted against the NEXT handler to be invoked in that invocation; when it |
| 48 |
* finds such a situation, Tcl_DeleteChannelHandler updates the nextHandlerPtr |
| 49 |
* field of the structure to the next handler. |
| 50 |
*/ |
| 51 |
|
| 52 |
typedef struct NextChannelHandler { |
| 53 |
ChannelHandler *nextHandlerPtr; |
| 54 |
/* The next handler to be invoked in |
| 55 |
* this invocation. */ |
| 56 |
struct NextChannelHandler *nestedHandlerPtr; |
| 57 |
/* Next nested invocation of |
| 58 |
* Tcl_NotifyChannel. */ |
| 59 |
} NextChannelHandler; |
| 60 |
|
| 61 |
/* |
| 62 |
* The following structure is used by Tcl_GetsObj() to encapsulates the |
| 63 |
* state for a "gets" operation. |
| 64 |
*/ |
| 65 |
|
| 66 |
typedef struct GetsState { |
| 67 |
Tcl_Obj *objPtr; /* The object to which UTF-8 characters |
| 68 |
* will be appended. */ |
| 69 |
char **dstPtr; /* Pointer into objPtr's string rep where |
| 70 |
* next character should be stored. */ |
| 71 |
Tcl_Encoding encoding; /* The encoding to use to convert raw bytes |
| 72 |
* to UTF-8. */ |
| 73 |
ChannelBuffer *bufPtr; /* The current buffer of raw bytes being |
| 74 |
* emptied. */ |
| 75 |
Tcl_EncodingState state; /* The encoding state just before the last |
| 76 |
* external to UTF-8 conversion in |
| 77 |
* FilterInputBytes(). */ |
| 78 |
int rawRead; /* The number of bytes removed from bufPtr |
| 79 |
* in the last call to FilterInputBytes(). */ |
| 80 |
int bytesWrote; /* The number of bytes of UTF-8 data |
| 81 |
* appended to objPtr during the last call to |
| 82 |
* FilterInputBytes(). */ |
| 83 |
int charsWrote; /* The corresponding number of UTF-8 |
| 84 |
* characters appended to objPtr during the |
| 85 |
* last call to FilterInputBytes(). */ |
| 86 |
int totalChars; /* The total number of UTF-8 characters |
| 87 |
* appended to objPtr so far, just before the |
| 88 |
* last call to FilterInputBytes(). */ |
| 89 |
} GetsState; |
| 90 |
|
| 91 |
/* |
| 92 |
* The following structure encapsulates the state for a background channel |
| 93 |
* copy. Note that the data buffer for the copy will be appended to this |
| 94 |
* structure. |
| 95 |
*/ |
| 96 |
|
| 97 |
typedef struct CopyState { |
| 98 |
struct Channel *readPtr; /* Pointer to input channel. */ |
| 99 |
struct Channel *writePtr; /* Pointer to output channel. */ |
| 100 |
int refCount; /* Reference counter. */ |
| 101 |
int readFlags; /* Original read channel flags. */ |
| 102 |
int writeFlags; /* Original write channel flags. */ |
| 103 |
Tcl_WideInt toRead; /* Number of bytes to copy, or -1. */ |
| 104 |
Tcl_WideInt total; /* Total bytes transferred (written). */ |
| 105 |
Tcl_Interp *interp; /* Interp that started the copy. */ |
| 106 |
Tcl_Obj *cmdPtr; /* Command to be invoked at completion. */ |
| 107 |
int bufSize; /* Size of appended buffer. */ |
| 108 |
char buffer[TCLFLEXARRAY]; /* Copy buffer, this must be the last |
| 109 |
* field. */ |
| 110 |
} CopyState; |
| 111 |
|
| 112 |
/* |
| 113 |
* All static variables used in this file are collected into a single instance |
| 114 |
* of the following structure. For multi-threaded implementations, there is |
| 115 |
* one instance of this structure for each thread. |
| 116 |
* |
| 117 |
* Notice that different structures with the same name appear in other files. |
| 118 |
* The structure defined below is used in this file only. |
| 119 |
*/ |
| 120 |
|
| 121 |
typedef struct ThreadSpecificData { |
| 122 |
NextChannelHandler *nestedHandlerPtr; |
| 123 |
/* This variable holds the list of nested |
| 124 |
* Tcl_NotifyChannel invocations. */ |
| 125 |
ChannelState *firstCSPtr; /* List of all channels currently open, |
| 126 |
* indexed by ChannelState, as only one |
| 127 |
* ChannelState exists per set of stacked |
| 128 |
* channels. */ |
| 129 |
Tcl_Channel stdinChannel; /* Static variable for the stdin channel. */ |
| 130 |
int stdinInitialized; |
| 131 |
Tcl_Channel stdoutChannel; /* Static variable for the stdout channel. */ |
| 132 |
int stdoutInitialized; |
| 133 |
Tcl_Channel stderrChannel; /* Static variable for the stderr channel. */ |
| 134 |
int stderrInitialized; |
| 135 |
Tcl_Encoding binaryEncoding; |
| 136 |
} ThreadSpecificData; |
| 137 |
|
| 138 |
static Tcl_ThreadDataKey dataKey; |
| 139 |
|
| 140 |
/* |
| 141 |
* Structure to record a close callback. One such record exists for |
| 142 |
* each close callback registered for a channel. |
| 143 |
*/ |
| 144 |
|
| 145 |
typedef struct CloseCallback { |
| 146 |
Tcl_CloseProc *proc; /* The procedure to call. */ |
| 147 |
void *clientData; /* Arbitrary one-word data to pass |
| 148 |
* to the callback. */ |
| 149 |
struct CloseCallback *nextPtr; |
| 150 |
/* For chaining close callbacks. */ |
| 151 |
} CloseCallback; |
| 152 |
|
| 153 |
/* |
| 154 |
* Static functions in this file: |
| 155 |
*/ |
| 156 |
|
| 157 |
static ChannelBuffer * AllocChannelBuffer(int length); |
| 158 |
static void PreserveChannelBuffer(ChannelBuffer *bufPtr); |
| 159 |
static void ReleaseChannelBuffer(ChannelBuffer *bufPtr); |
| 160 |
static int IsShared(ChannelBuffer *bufPtr); |
| 161 |
static void ChannelFree(Channel *chanPtr); |
| 162 |
static void ChannelTimerProc(void *clientData); |
| 163 |
static int ChanRead(Channel *chanPtr, char *dst, int dstSize); |
| 164 |
static int CheckChannelErrors(ChannelState *statePtr, |
| 165 |
int direction); |
| 166 |
static int CheckForDeadChannel(Tcl_Interp *interp, |
| 167 |
ChannelState *statePtr); |
| 168 |
static void CheckForStdChannelsBeingClosed(Tcl_Channel chan); |
| 169 |
static void CleanupChannelHandlers(Tcl_Interp *interp, |
| 170 |
Channel *chanPtr); |
| 171 |
static int CloseChannel(Tcl_Interp *interp, Channel *chanPtr, |
| 172 |
int errorCode); |
| 173 |
static int CloseChannelPart(Tcl_Interp *interp, Channel *chanPtr, |
| 174 |
int errorCode, int flags); |
| 175 |
static int CloseWrite(Tcl_Interp *interp, Channel *chanPtr); |
| 176 |
static void CommonGetsCleanup(Channel *chanPtr); |
| 177 |
static int CopyData(CopyState *csPtr, int mask); |
| 178 |
static void DeleteTimerHandler(ChannelState *statePtr); |
| 179 |
static int MoveBytes(CopyState *csPtr); |
| 180 |
|
| 181 |
static void MBCallback(CopyState *csPtr, Tcl_Obj *errObj); |
| 182 |
static void MBError(CopyState *csPtr, int mask, int errorCode); |
| 183 |
static int MBRead(CopyState *csPtr); |
| 184 |
static int MBWrite(CopyState *csPtr); |
| 185 |
static void MBEvent(void *clientData, int mask); |
| 186 |
|
| 187 |
static void CopyEventProc(void *clientData, int mask); |
| 188 |
static void CreateScriptRecord(Tcl_Interp *interp, |
| 189 |
Channel *chanPtr, int mask, Tcl_Obj *scriptPtr); |
| 190 |
static void DeleteChannelTable(void *clientData, |
| 191 |
Tcl_Interp *interp); |
| 192 |
static void DeleteScriptRecord(Tcl_Interp *interp, |
| 193 |
Channel *chanPtr, int mask); |
| 194 |
static int DetachChannel(Tcl_Interp *interp, Tcl_Channel chan); |
| 195 |
static void DiscardInputQueued(ChannelState *statePtr, |
| 196 |
int discardSavedBuffers); |
| 197 |
static void DiscardOutputQueued(ChannelState *chanPtr); |
| 198 |
static int DoRead(Channel *chanPtr, char *dst, int bytesToRead, |
| 199 |
int allowShortReads); |
| 200 |
static int DoReadChars(Channel *chan, Tcl_Obj *objPtr, int toRead, |
| 201 |
int appendFlag); |
| 202 |
static int FilterInputBytes(Channel *chanPtr, |
| 203 |
GetsState *statePtr); |
| 204 |
static int FlushChannel(Tcl_Interp *interp, Channel *chanPtr, |
| 205 |
int calledFromAsyncFlush); |
| 206 |
static int TclGetsObjBinary(Tcl_Channel chan, Tcl_Obj *objPtr); |
| 207 |
static Tcl_Encoding GetBinaryEncoding(void); |
| 208 |
static void FreeBinaryEncoding(void *clientData); |
| 209 |
static Tcl_HashTable * GetChannelTable(Tcl_Interp *interp); |
| 210 |
static int GetInput(Channel *chanPtr); |
| 211 |
static void PeekAhead(Channel *chanPtr, char **dstEndPtr, |
| 212 |
GetsState *gsPtr); |
| 213 |
static int ReadBytes(ChannelState *statePtr, Tcl_Obj *objPtr, |
| 214 |
int charsLeft); |
| 215 |
static int ReadChars(ChannelState *statePtr, Tcl_Obj *objPtr, |
| 216 |
int charsLeft, int *factorPtr); |
| 217 |
static void RecycleBuffer(ChannelState *statePtr, |
| 218 |
ChannelBuffer *bufPtr, int mustDiscard); |
| 219 |
static int StackSetBlockMode(Channel *chanPtr, int mode); |
| 220 |
static int SetBlockMode(Tcl_Interp *interp, Channel *chanPtr, |
| 221 |
int mode); |
| 222 |
static void StopCopy(CopyState *csPtr); |
| 223 |
static void CopyDecrRefCount(CopyState *csPtr); |
| 224 |
static void TranslateInputEOL(ChannelState *statePtr, char *dst, |
| 225 |
const char *src, int *dstLenPtr, int *srcLenPtr); |
| 226 |
static void UpdateInterest(Channel *chanPtr); |
| 227 |
static int Write(Channel *chanPtr, const char *src, |
| 228 |
int srcLen, Tcl_Encoding encoding); |
| 229 |
static Tcl_Obj * FixLevelCode(Tcl_Obj *msg); |
| 230 |
static void SpliceChannel(Tcl_Channel chan); |
| 231 |
static void CutChannel(Tcl_Channel chan); |
| 232 |
static int WillRead(Channel *chanPtr); |
| 233 |
|
| 234 |
#define WriteChars(chanPtr, src, srcLen) \ |
| 235 |
Write(chanPtr, src, srcLen, chanPtr->state->encoding) |
| 236 |
#define WriteBytes(chanPtr, src, srcLen) \ |
| 237 |
Write(chanPtr, src, srcLen, tclIdentityEncoding) |
| 238 |
|
| 239 |
/* |
| 240 |
* Simplifying helper macros. All may use their argument(s) multiple times. |
| 241 |
* The ANSI C "prototypes" for the macros are listed below, together with a |
| 242 |
* short description of what the macro does. |
| 243 |
* |
| 244 |
* -------------------------------------------------------------------------- |
| 245 |
* int BytesLeft(ChannelBuffer *bufPtr) |
| 246 |
* |
| 247 |
* Returns the number of bytes of data remaining in the buffer. |
| 248 |
* |
| 249 |
* int SpaceLeft(ChannelBuffer *bufPtr) |
| 250 |
* |
| 251 |
* Returns the number of bytes of space remaining at the end of the |
| 252 |
* buffer. |
| 253 |
* |
| 254 |
* int IsBufferReady(ChannelBuffer *bufPtr) |
| 255 |
* |
| 256 |
* Returns whether a buffer has bytes available within it. |
| 257 |
* |
| 258 |
* int IsBufferEmpty(ChannelBuffer *bufPtr) |
| 259 |
* |
| 260 |
* Returns whether a buffer is entirely empty. Note that this is not the |
| 261 |
* inverse of the above operation; trying to merge the two seems to lead |
| 262 |
* to occasional crashes... |
| 263 |
* |
| 264 |
* int IsBufferFull(ChannelBuffer *bufPtr) |
| 265 |
* |
| 266 |
* Returns whether more data can be added to a buffer. |
| 267 |
* |
| 268 |
* int IsBufferOverflowing(ChannelBuffer *bufPtr) |
| 269 |
* |
| 270 |
* Returns whether a buffer has more data in it than it should. |
| 271 |
* |
| 272 |
* char *InsertPoint(ChannelBuffer *bufPtr) |
| 273 |
* |
| 274 |
* Returns a pointer to where characters should be added to the buffer. |
| 275 |
* |
| 276 |
* char *RemovePoint(ChannelBuffer *bufPtr) |
| 277 |
* |
| 278 |
* Returns a pointer to where characters should be removed from the |
| 279 |
* buffer. |
| 280 |
* -------------------------------------------------------------------------- |
| 281 |
*/ |
| 282 |
|
| 283 |
#define BytesLeft(bufPtr) ((bufPtr)->nextAdded - (bufPtr)->nextRemoved) |
| 284 |
|
| 285 |
#define SpaceLeft(bufPtr) ((bufPtr)->bufLength - (bufPtr)->nextAdded) |
| 286 |
|
| 287 |
#define IsBufferReady(bufPtr) ((bufPtr)->nextAdded > (bufPtr)->nextRemoved) |
| 288 |
|
| 289 |
#define IsBufferEmpty(bufPtr) ((bufPtr)->nextAdded == (bufPtr)->nextRemoved) |
| 290 |
|
| 291 |
#define IsBufferFull(bufPtr) ((bufPtr) && (bufPtr)->nextAdded >= (bufPtr)->bufLength) |
| 292 |
|
| 293 |
#define IsBufferOverflowing(bufPtr) ((bufPtr)->nextAdded>(bufPtr)->bufLength) |
| 294 |
|
| 295 |
#define InsertPoint(bufPtr) (&(bufPtr)->buf[(bufPtr)->nextAdded]) |
| 296 |
|
| 297 |
#define RemovePoint(bufPtr) (&(bufPtr)->buf[(bufPtr)->nextRemoved]) |
| 298 |
|
| 299 |
/* |
| 300 |
* For working with channel state flag bits. |
| 301 |
*/ |
| 302 |
|
| 303 |
#define SetFlag(statePtr, flag) ((statePtr)->flags |= (flag)) |
| 304 |
#define ResetFlag(statePtr, flag) ((statePtr)->flags &= ~(flag)) |
| 305 |
#define GotFlag(statePtr, flag) ((statePtr)->flags & (flag)) |
| 306 |
|
| 307 |
/* |
| 308 |
* Macro for testing whether a string (in optionName, length len) matches a |
| 309 |
* value (prefix matching rules). Arguments are the minimum length to match |
| 310 |
* and the value to match against. (Can't use Tcl_GetIndexFromObj as this is |
| 311 |
* used in a situation where no objects are available.) |
| 312 |
*/ |
| 313 |
|
| 314 |
#define HaveOpt(minLength, nameString) \ |
| 315 |
((len > (minLength)) && (optionName[1] == (nameString)[1]) \ |
| 316 |
&& (strncmp(optionName, (nameString), len) == 0)) |
| 317 |
|
| 318 |
/* |
| 319 |
* The ChannelObjType type. Used to store the result of looking up |
| 320 |
* a channel name in the context of an interp. Saves the lookup |
| 321 |
* result and values needed to check its continued validity. |
| 322 |
*/ |
| 323 |
|
| 324 |
typedef struct ResolvedChanName { |
| 325 |
ChannelState *statePtr; /* The saved lookup result */ |
| 326 |
Tcl_Interp *interp; /* The interp in which the lookup was done. */ |
| 327 |
int epoch; /* The epoch of the channel when the lookup |
| 328 |
* was done. Use to verify validity. */ |
| 329 |
int refCount; /* Share this struct among many Tcl_Obj. */ |
| 330 |
} ResolvedChanName; |
| 331 |
|
| 332 |
static void DupChannelInternalRep(Tcl_Obj *objPtr, Tcl_Obj *copyPtr); |
| 333 |
static void FreeChannelInternalRep(Tcl_Obj *objPtr); |
| 334 |
|
| 335 |
static const Tcl_ObjType chanObjType = { |
| 336 |
"channel", /* name for this type */ |
| 337 |
FreeChannelInternalRep, /* freeIntRepProc */ |
| 338 |
DupChannelInternalRep, /* dupIntRepProc */ |
| 339 |
NULL, /* updateStringProc */ |
| 340 |
NULL /* setFromAnyProc */ |
| 341 |
}; |
| 342 |
|
| 343 |
#define BUSY_STATE(st, fl) \ |
| 344 |
((((st)->csPtrR) && ((fl) & TCL_READABLE)) || \ |
| 345 |
(((st)->csPtrW) && ((fl) & TCL_WRITABLE))) |
| 346 |
|
| 347 |
#define MAX_CHANNEL_BUFFER_SIZE (1024*1024) |
| 348 |
|
| 349 |
/* |
| 350 |
*--------------------------------------------------------------------------- |
| 351 |
* |
| 352 |
* ChanClose, ChanRead, ChanSeek, ChanThreadAction, ChanWatch, ChanWrite -- |
| 353 |
* |
| 354 |
* Simplify the access to selected channel driver "methods" that are used |
| 355 |
* in multiple places in a stereotypical fashion. These are just thin |
| 356 |
* wrappers around the driver functions. |
| 357 |
* |
| 358 |
*--------------------------------------------------------------------------- |
| 359 |
*/ |
| 360 |
|
| 361 |
static inline int |
| 362 |
ChanClose( |
| 363 |
Channel *chanPtr, |
| 364 |
Tcl_Interp *interp) |
| 365 |
{ |
| 366 |
if ((chanPtr->typePtr->closeProc != TCL_CLOSE2PROC) && (chanPtr->typePtr->closeProc != NULL)) { |
| 367 |
return chanPtr->typePtr->closeProc(chanPtr->instanceData, interp); |
| 368 |
} else { |
| 369 |
return chanPtr->typePtr->close2Proc(chanPtr->instanceData, interp, 0); |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
/* |
| 374 |
*--------------------------------------------------------------------------- |
| 375 |
* |
| 376 |
* ChanRead -- |
| 377 |
* |
| 378 |
* Read up to dstSize bytes using the inputProc of chanPtr, store them at |
| 379 |
* dst, and return the number of bytes stored. |
| 380 |
* |
| 381 |
* Results: |
| 382 |
* The return value of the driver inputProc, |
| 383 |
* - number of bytes stored at dst, ot |
| 384 |
* - -1 on error, with a Posix error code available to the caller by |
| 385 |
* calling Tcl_GetErrno(). |
| 386 |
* |
| 387 |
* Side effects: |
| 388 |
* The CHANNEL_BLOCKED and CHANNEL_EOF flags of the channel state are set |
| 389 |
* as appropriate. On EOF, the inputEncodingFlags are set to perform |
| 390 |
* ending operations on decoding. |
| 391 |
* |
| 392 |
* TODO - Is this really the right place for that? |
| 393 |
* |
| 394 |
*--------------------------------------------------------------------------- |
| 395 |
*/ |
| 396 |
static int |
| 397 |
ChanRead( |
| 398 |
Channel *chanPtr, |
| 399 |
char *dst, |
| 400 |
int dstSize) |
| 401 |
{ |
| 402 |
int bytesRead, result; |
| 403 |
|
| 404 |
/* |
| 405 |
* If the caller asked for zero bytes, we'd force the inputProc to return |
| 406 |
* zero bytes, and then misinterpret that as EOF. |
| 407 |
*/ |
| 408 |
|
| 409 |
assert(dstSize > 0); |
| 410 |
|
| 411 |
/* |
| 412 |
* Each read op must set the blocked and eof states anew, not let |
| 413 |
* the effect of prior reads leak through. |
| 414 |
*/ |
| 415 |
|
| 416 |
if (GotFlag(chanPtr->state, CHANNEL_EOF)) { |
| 417 |
chanPtr->state->inputEncodingFlags |= TCL_ENCODING_START; |
| 418 |
} |
| 419 |
ResetFlag(chanPtr->state, CHANNEL_BLOCKED | CHANNEL_EOF); |
| 420 |
chanPtr->state->inputEncodingFlags &= ~TCL_ENCODING_END; |
| 421 |
if (WillRead(chanPtr) < 0) { |
| 422 |
return -1; |
| 423 |
} |
| 424 |
|
| 425 |
bytesRead = chanPtr->typePtr->inputProc(chanPtr->instanceData, |
| 426 |
dst, dstSize, &result); |
| 427 |
|
| 428 |
/* |
| 429 |
* Stop any flag leakage through stacked channel levels. |
| 430 |
*/ |
| 431 |
|
| 432 |
if (GotFlag(chanPtr->state, CHANNEL_EOF)) { |
| 433 |
chanPtr->state->inputEncodingFlags |= TCL_ENCODING_START; |
| 434 |
} |
| 435 |
ResetFlag(chanPtr->state, CHANNEL_BLOCKED | CHANNEL_EOF); |
| 436 |
chanPtr->state->inputEncodingFlags &= ~TCL_ENCODING_END; |
| 437 |
if (bytesRead < 0) { |
| 438 |
if ((result == EWOULDBLOCK) || (result == EAGAIN)) { |
| 439 |
SetFlag(chanPtr->state, CHANNEL_BLOCKED); |
| 440 |
result = EAGAIN; |
| 441 |
} |
| 442 |
Tcl_SetErrno(result); |
| 443 |
} else if (bytesRead == 0) { |
| 444 |
SetFlag(chanPtr->state, CHANNEL_EOF); |
| 445 |
chanPtr->state->inputEncodingFlags |= TCL_ENCODING_END; |
| 446 |
} else { |
| 447 |
/* |
| 448 |
* If we get a short read, signal up that we may be BLOCKED. We should |
| 449 |
* avoid calling the driver because on some platforms we will block in |
| 450 |
* the low level reading code even though the channel is set into |
| 451 |
* nonblocking mode. |
| 452 |
*/ |
| 453 |
|
| 454 |
if (bytesRead < dstSize) { |
| 455 |
SetFlag(chanPtr->state, CHANNEL_BLOCKED); |
| 456 |
} |
| 457 |
} |
| 458 |
return bytesRead; |
| 459 |
} |
| 460 |
|
| 461 |
static inline Tcl_WideInt |
| 462 |
ChanSeek( |
| 463 |
Channel *chanPtr, |
| 464 |
Tcl_WideInt offset, |
| 465 |
int mode, |
| 466 |
int *errnoPtr) |
| 467 |
{ |
| 468 |
/* |
| 469 |
* Note that we prefer the wideSeekProc if that field is available in the |
| 470 |
* type and non-NULL. |
| 471 |
*/ |
| 472 |
|
| 473 |
if (Tcl_ChannelWideSeekProc(chanPtr->typePtr) != NULL) { |
| 474 |
return Tcl_ChannelWideSeekProc(chanPtr->typePtr)(chanPtr->instanceData, |
| 475 |
offset, mode, errnoPtr); |
| 476 |
} |
| 477 |
|
| 478 |
if ((offset >= LONG_MIN) && (offset <= LONG_MAX)) { |
| 479 |
return Tcl_ChannelSeekProc(chanPtr->typePtr)(chanPtr->instanceData, |
| 480 |
(long)offset, mode, errnoPtr); |
| 481 |
} |
| 482 |
*errnoPtr = EOVERFLOW; |
| 483 |
return -1; |
| 484 |
} |
| 485 |
|
| 486 |
static inline void |
| 487 |
ChanThreadAction( |
| 488 |
Channel *chanPtr, |
| 489 |
int action) |
| 490 |
{ |
| 491 |
Tcl_DriverThreadActionProc *threadActionProc = |
| 492 |
Tcl_ChannelThreadActionProc(chanPtr->typePtr); |
| 493 |
|
| 494 |
if (threadActionProc != NULL) { |
| 495 |
threadActionProc(chanPtr->instanceData, action); |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
static inline void |
| 500 |
ChanWatch( |
| 501 |
Channel *chanPtr, |
| 502 |
int mask) |
| 503 |
{ |
| 504 |
chanPtr->typePtr->watchProc(chanPtr->instanceData, mask); |
| 505 |
} |
| 506 |
|
| 507 |
static inline int |
| 508 |
ChanWrite( |
| 509 |
Channel *chanPtr, |
| 510 |
const char *src, |
| 511 |
int srcLen, |
| 512 |
int *errnoPtr) |
| 513 |
{ |
| 514 |
return chanPtr->typePtr->outputProc(chanPtr->instanceData, src, srcLen, |
| 515 |
errnoPtr); |
| 516 |
} |
| 517 |
|
| 518 |
/* |
| 519 |
*--------------------------------------------------------------------------- |
| 520 |
* |
| 521 |
* TclInitIOSubsystem -- |
| 522 |
* |
| 523 |
* Initialize all resources used by this subsystem on a per-process |
| 524 |
* basis. |
| 525 |
* |
| 526 |
* Results: |
| 527 |
* None. |
| 528 |
* |
| 529 |
* Side effects: |
| 530 |
* Depends on the memory subsystems. |
| 531 |
* |
| 532 |
*--------------------------------------------------------------------------- |
| 533 |
*/ |
| 534 |
|
| 535 |
void |
| 536 |
TclInitIOSubsystem(void) |
| 537 |
{ |
| 538 |
/* |
| 539 |
* By fetching thread local storage we take care of allocating it for each |
| 540 |
* thread. |
| 541 |
*/ |
| 542 |
|
| 543 |
(void) TCL_TSD_INIT(&dataKey); |
| 544 |
} |
| 545 |
|
| 546 |
/* |
| 547 |
*------------------------------------------------------------------------- |
| 548 |
* |
| 549 |
* TclFinalizeIOSubsystem -- |
| 550 |
* |
| 551 |
* Releases all resources used by this subsystem on a per-process basis. |
| 552 |
* Closes all extant channels that have not already been closed because |
| 553 |
* they were not owned by any interp. |
| 554 |
* |
| 555 |
* Results: |
| 556 |
* None. |
| 557 |
* |
| 558 |
* Side effects: |
| 559 |
* Depends on encoding and memory subsystems. |
| 560 |
* |
| 561 |
*------------------------------------------------------------------------- |
| 562 |
*/ |
| 563 |
|
| 564 |
void |
| 565 |
TclFinalizeIOSubsystem(void) |
| 566 |
{ |
| 567 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 568 |
Channel *chanPtr = NULL; /* Iterates over open channels. */ |
| 569 |
ChannelState *statePtr; /* State of channel stack */ |
| 570 |
int active = 1; /* Flag == 1 while there's still work to do */ |
| 571 |
int doflushnb; |
| 572 |
|
| 573 |
/* |
| 574 |
* Fetch the pre-TIP#398 compatibility flag. |
| 575 |
*/ |
| 576 |
|
| 577 |
{ |
| 578 |
const char *s; |
| 579 |
Tcl_DString ds; |
| 580 |
|
| 581 |
s = TclGetEnv("TCL_FLUSH_NONBLOCKING_ON_EXIT", &ds); |
| 582 |
doflushnb = ((s != NULL) && strcmp(s, "0")); |
| 583 |
if (s != NULL) { |
| 584 |
Tcl_DStringFree(&ds); |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
/* |
| 589 |
* Walk all channel state structures known to this thread and close |
| 590 |
* corresponding channels. |
| 591 |
*/ |
| 592 |
|
| 593 |
while (active) { |
| 594 |
/* |
| 595 |
* Iterate through the open channel list, and find the first channel |
| 596 |
* that isn't dead. We start from the head of the list each time, |
| 597 |
* because the close action on one channel can close others. |
| 598 |
*/ |
| 599 |
|
| 600 |
active = 0; |
| 601 |
for (statePtr = tsdPtr->firstCSPtr; |
| 602 |
statePtr != NULL; |
| 603 |
statePtr = statePtr->nextCSPtr) { |
| 604 |
chanPtr = statePtr->topChanPtr; |
| 605 |
if (GotFlag(statePtr, CHANNEL_DEAD)) { |
| 606 |
continue; |
| 607 |
} |
| 608 |
if (!GotFlag(statePtr, CHANNEL_INCLOSE | CHANNEL_CLOSED ) |
| 609 |
|| GotFlag(statePtr, BG_FLUSH_SCHEDULED)) { |
| 610 |
ResetFlag(statePtr, BG_FLUSH_SCHEDULED); |
| 611 |
active = 1; |
| 612 |
break; |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
/* |
| 617 |
* We've found a live (or bg-closing) channel. Close it. |
| 618 |
*/ |
| 619 |
|
| 620 |
if (active) { |
| 621 |
TclChannelPreserve((Tcl_Channel)chanPtr); |
| 622 |
|
| 623 |
/* |
| 624 |
* TIP #398: by default, we no longer set the channel back into |
| 625 |
* blocking mode. To restore the old blocking behavior, the |
| 626 |
* environment variable TCL_FLUSH_NONBLOCKING_ON_EXIT must be set |
| 627 |
* and not be "0". |
| 628 |
*/ |
| 629 |
|
| 630 |
if (doflushnb) { |
| 631 |
/* |
| 632 |
* Set the channel back into blocking mode to ensure that we |
| 633 |
* wait for all data to flush out. |
| 634 |
*/ |
| 635 |
|
| 636 |
(void) Tcl_SetChannelOption(NULL, (Tcl_Channel) chanPtr, |
| 637 |
"-blocking", "on"); |
| 638 |
} |
| 639 |
|
| 640 |
if ((chanPtr == (Channel *) tsdPtr->stdinChannel) || |
| 641 |
(chanPtr == (Channel *) tsdPtr->stdoutChannel) || |
| 642 |
(chanPtr == (Channel *) tsdPtr->stderrChannel)) { |
| 643 |
/* |
| 644 |
* Decrement the refcount which was earlier artificially |
| 645 |
* bumped up to keep the channel from being closed. |
| 646 |
*/ |
| 647 |
|
| 648 |
statePtr->refCount--; |
| 649 |
} |
| 650 |
|
| 651 |
if (statePtr->refCount <= 0) { |
| 652 |
/* |
| 653 |
* Close it only if the refcount indicates that the channel is |
| 654 |
* not referenced from any interpreter. If it is, that |
| 655 |
* interpreter will close the channel when it gets destroyed. |
| 656 |
*/ |
| 657 |
|
| 658 |
(void) Tcl_Close(NULL, (Tcl_Channel) chanPtr); |
| 659 |
} else { |
| 660 |
/* |
| 661 |
* The refcount is greater than zero, so flush the channel. |
| 662 |
*/ |
| 663 |
|
| 664 |
Tcl_Flush((Tcl_Channel) chanPtr); |
| 665 |
|
| 666 |
/* |
| 667 |
* Call the device driver to actually close the underlying |
| 668 |
* device for this channel. |
| 669 |
*/ |
| 670 |
|
| 671 |
(void) ChanClose(chanPtr, NULL); |
| 672 |
|
| 673 |
/* |
| 674 |
* Finally, we clean up the fields in the channel data |
| 675 |
* structure since all of them have been deleted already. We |
| 676 |
* mark the channel with CHANNEL_DEAD to prevent any further |
| 677 |
* IO operations on it. |
| 678 |
*/ |
| 679 |
|
| 680 |
chanPtr->instanceData = NULL; |
| 681 |
SetFlag(statePtr, CHANNEL_DEAD); |
| 682 |
} |
| 683 |
TclChannelRelease((Tcl_Channel)chanPtr); |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
TclpFinalizeSockets(); |
| 688 |
TclpFinalizePipes(); |
| 689 |
} |
| 690 |
|
| 691 |
/* |
| 692 |
*---------------------------------------------------------------------- |
| 693 |
* |
| 694 |
* Tcl_SetStdChannel -- |
| 695 |
* |
| 696 |
* This function is used to change the channels that are used for |
| 697 |
* stdin/stdout/stderr in new interpreters. |
| 698 |
* |
| 699 |
* Results: |
| 700 |
* None |
| 701 |
* |
| 702 |
* Side effects: |
| 703 |
* None. |
| 704 |
* |
| 705 |
*---------------------------------------------------------------------- |
| 706 |
*/ |
| 707 |
|
| 708 |
void |
| 709 |
Tcl_SetStdChannel( |
| 710 |
Tcl_Channel channel, |
| 711 |
int type) /* One of TCL_STDIN, TCL_STDOUT, TCL_STDERR. */ |
| 712 |
{ |
| 713 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 714 |
|
| 715 |
int init = channel ? 1 : -1; |
| 716 |
switch (type) { |
| 717 |
case TCL_STDIN: |
| 718 |
tsdPtr->stdinInitialized = init; |
| 719 |
tsdPtr->stdinChannel = channel; |
| 720 |
break; |
| 721 |
case TCL_STDOUT: |
| 722 |
tsdPtr->stdoutInitialized = init; |
| 723 |
tsdPtr->stdoutChannel = channel; |
| 724 |
break; |
| 725 |
case TCL_STDERR: |
| 726 |
tsdPtr->stderrInitialized = init; |
| 727 |
tsdPtr->stderrChannel = channel; |
| 728 |
break; |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
/* |
| 733 |
*---------------------------------------------------------------------- |
| 734 |
* |
| 735 |
* Tcl_GetStdChannel -- |
| 736 |
* |
| 737 |
* Returns the specified standard channel. |
| 738 |
* |
| 739 |
* Results: |
| 740 |
* Returns the specified standard channel, or NULL. |
| 741 |
* |
| 742 |
* Side effects: |
| 743 |
* May cause the creation of a standard channel and the underlying file. |
| 744 |
* |
| 745 |
*---------------------------------------------------------------------- |
| 746 |
*/ |
| 747 |
|
| 748 |
Tcl_Channel |
| 749 |
Tcl_GetStdChannel( |
| 750 |
int type) /* One of TCL_STDIN, TCL_STDOUT, TCL_STDERR. */ |
| 751 |
{ |
| 752 |
Tcl_Channel channel = NULL; |
| 753 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 754 |
|
| 755 |
/* |
| 756 |
* If the channels were not created yet, create them now and store them in |
| 757 |
* the static variables. |
| 758 |
*/ |
| 759 |
|
| 760 |
switch (type) { |
| 761 |
case TCL_STDIN: |
| 762 |
if (!tsdPtr->stdinInitialized) { |
| 763 |
tsdPtr->stdinInitialized = -1; |
| 764 |
tsdPtr->stdinChannel = TclpGetDefaultStdChannel(TCL_STDIN); |
| 765 |
|
| 766 |
/* |
| 767 |
* Artificially bump the refcount to ensure that the channel is |
| 768 |
* only closed on exit. |
| 769 |
* |
| 770 |
* NOTE: Must only do this if stdinChannel is not NULL. It can be |
| 771 |
* NULL in situations where Tcl is unable to connect to the |
| 772 |
* standard input. |
| 773 |
*/ |
| 774 |
|
| 775 |
if (tsdPtr->stdinChannel != NULL) { |
| 776 |
tsdPtr->stdinInitialized = 1; |
| 777 |
Tcl_RegisterChannel(NULL, tsdPtr->stdinChannel); |
| 778 |
} |
| 779 |
} |
| 780 |
channel = tsdPtr->stdinChannel; |
| 781 |
break; |
| 782 |
case TCL_STDOUT: |
| 783 |
if (!tsdPtr->stdoutInitialized) { |
| 784 |
tsdPtr->stdoutInitialized = -1; |
| 785 |
tsdPtr->stdoutChannel = TclpGetDefaultStdChannel(TCL_STDOUT); |
| 786 |
if (tsdPtr->stdoutChannel != NULL) { |
| 787 |
tsdPtr->stdoutInitialized = 1; |
| 788 |
Tcl_RegisterChannel(NULL, tsdPtr->stdoutChannel); |
| 789 |
} |
| 790 |
} |
| 791 |
channel = tsdPtr->stdoutChannel; |
| 792 |
break; |
| 793 |
case TCL_STDERR: |
| 794 |
if (!tsdPtr->stderrInitialized) { |
| 795 |
tsdPtr->stderrInitialized = -1; |
| 796 |
tsdPtr->stderrChannel = TclpGetDefaultStdChannel(TCL_STDERR); |
| 797 |
if (tsdPtr->stderrChannel != NULL) { |
| 798 |
tsdPtr->stderrInitialized = 1; |
| 799 |
Tcl_RegisterChannel(NULL, tsdPtr->stderrChannel); |
| 800 |
} |
| 801 |
} |
| 802 |
channel = tsdPtr->stderrChannel; |
| 803 |
break; |
| 804 |
} |
| 805 |
return channel; |
| 806 |
} |
| 807 |
|
| 808 |
/* |
| 809 |
*---------------------------------------------------------------------- |
| 810 |
* |
| 811 |
* Tcl_CreateCloseHandler |
| 812 |
* |
| 813 |
* Creates a close callback which will be called when the channel is |
| 814 |
* closed. |
| 815 |
* |
| 816 |
* Results: |
| 817 |
* None. |
| 818 |
* |
| 819 |
* Side effects: |
| 820 |
* Causes the callback to be called in the future when the channel will |
| 821 |
* be closed. |
| 822 |
* |
| 823 |
*---------------------------------------------------------------------- |
| 824 |
*/ |
| 825 |
|
| 826 |
void |
| 827 |
Tcl_CreateCloseHandler( |
| 828 |
Tcl_Channel chan, /* The channel for which to create the close |
| 829 |
* callback. */ |
| 830 |
Tcl_CloseProc *proc, /* The callback routine to call when the |
| 831 |
* channel will be closed. */ |
| 832 |
void *clientData) /* Arbitrary data to pass to the close |
| 833 |
* callback. */ |
| 834 |
{ |
| 835 |
ChannelState *statePtr = ((Channel *) chan)->state; |
| 836 |
CloseCallback *cbPtr; |
| 837 |
|
| 838 |
cbPtr = (CloseCallback *)ckalloc(sizeof(CloseCallback)); |
| 839 |
cbPtr->proc = proc; |
| 840 |
cbPtr->clientData = clientData; |
| 841 |
|
| 842 |
cbPtr->nextPtr = statePtr->closeCbPtr; |
| 843 |
statePtr->closeCbPtr = cbPtr; |
| 844 |
} |
| 845 |
|
| 846 |
/* |
| 847 |
*---------------------------------------------------------------------- |
| 848 |
* |
| 849 |
* Tcl_DeleteCloseHandler -- |
| 850 |
* |
| 851 |
* Removes a callback that would have been called on closing the channel. |
| 852 |
* If there is no matching callback then this function has no effect. |
| 853 |
* |
| 854 |
* Results: |
| 855 |
* None. |
| 856 |
* |
| 857 |
* Side effects: |
| 858 |
* The callback will not be called in the future when the channel is |
| 859 |
* eventually closed. |
| 860 |
* |
| 861 |
*---------------------------------------------------------------------- |
| 862 |
*/ |
| 863 |
|
| 864 |
void |
| 865 |
Tcl_DeleteCloseHandler( |
| 866 |
Tcl_Channel chan, /* The channel for which to cancel the close |
| 867 |
* callback. */ |
| 868 |
Tcl_CloseProc *proc, /* The procedure for the callback to |
| 869 |
* remove. */ |
| 870 |
void *clientData) /* The callback data for the callback to |
| 871 |
* remove. */ |
| 872 |
{ |
| 873 |
ChannelState *statePtr = ((Channel *) chan)->state; |
| 874 |
CloseCallback *cbPtr, *cbPrevPtr; |
| 875 |
|
| 876 |
for (cbPtr = statePtr->closeCbPtr, cbPrevPtr = NULL; |
| 877 |
cbPtr != NULL; cbPtr = cbPtr->nextPtr) { |
| 878 |
if ((cbPtr->proc == proc) && (cbPtr->clientData == clientData)) { |
| 879 |
if (cbPrevPtr == NULL) { |
| 880 |
statePtr->closeCbPtr = cbPtr->nextPtr; |
| 881 |
} else { |
| 882 |
cbPrevPtr->nextPtr = cbPtr->nextPtr; |
| 883 |
} |
| 884 |
ckfree(cbPtr); |
| 885 |
break; |
| 886 |
} |
| 887 |
cbPrevPtr = cbPtr; |
| 888 |
} |
| 889 |
} |
| 890 |
|
| 891 |
/* |
| 892 |
*---------------------------------------------------------------------- |
| 893 |
* |
| 894 |
* GetChannelTable -- |
| 895 |
* |
| 896 |
* Gets and potentially initializes the channel table for an interpreter. |
| 897 |
* If it is initializing the table it also inserts channels for stdin, |
| 898 |
* stdout and stderr if the interpreter is trusted. |
| 899 |
* |
| 900 |
* Results: |
| 901 |
* A pointer to the hash table created, for use by the caller. |
| 902 |
* |
| 903 |
* Side effects: |
| 904 |
* Initializes the channel table for an interpreter. May create channels |
| 905 |
* for stdin, stdout and stderr. |
| 906 |
* |
| 907 |
*---------------------------------------------------------------------- |
| 908 |
*/ |
| 909 |
|
| 910 |
static Tcl_HashTable * |
| 911 |
GetChannelTable( |
| 912 |
Tcl_Interp *interp) |
| 913 |
{ |
| 914 |
Tcl_HashTable *hTblPtr; /* Hash table of channels. */ |
| 915 |
Tcl_Channel stdinChan, stdoutChan, stderrChan; |
| 916 |
|
| 917 |
hTblPtr = (Tcl_HashTable *)Tcl_GetAssocData(interp, "tclIO", NULL); |
| 918 |
if (hTblPtr == NULL) { |
| 919 |
hTblPtr = (Tcl_HashTable *)ckalloc(sizeof(Tcl_HashTable)); |
| 920 |
Tcl_InitHashTable(hTblPtr, TCL_STRING_KEYS); |
| 921 |
Tcl_SetAssocData(interp, "tclIO", |
| 922 |
(Tcl_InterpDeleteProc *) DeleteChannelTable, hTblPtr); |
| 923 |
|
| 924 |
/* |
| 925 |
* If the interpreter is trusted (not "safe"), insert channels for |
| 926 |
* stdin, stdout and stderr (possibly creating them in the process). |
| 927 |
*/ |
| 928 |
|
| 929 |
if (Tcl_IsSafe(interp) == 0) { |
| 930 |
stdinChan = Tcl_GetStdChannel(TCL_STDIN); |
| 931 |
if (stdinChan != NULL) { |
| 932 |
Tcl_RegisterChannel(interp, stdinChan); |
| 933 |
} |
| 934 |
stdoutChan = Tcl_GetStdChannel(TCL_STDOUT); |
| 935 |
if (stdoutChan != NULL) { |
| 936 |
Tcl_RegisterChannel(interp, stdoutChan); |
| 937 |
} |
| 938 |
stderrChan = Tcl_GetStdChannel(TCL_STDERR); |
| 939 |
if (stderrChan != NULL) { |
| 940 |
Tcl_RegisterChannel(interp, stderrChan); |
| 941 |
} |
| 942 |
} |
| 943 |
} |
| 944 |
return hTblPtr; |
| 945 |
} |
| 946 |
|
| 947 |
/* |
| 948 |
*---------------------------------------------------------------------- |
| 949 |
* |
| 950 |
* DeleteChannelTable -- |
| 951 |
* |
| 952 |
* Deletes the channel table for an interpreter, closing any open |
| 953 |
* channels whose refcount reaches zero. This procedure is invoked when |
| 954 |
* an interpreter is deleted, via the AssocData cleanup mechanism. |
| 955 |
* |
| 956 |
* Results: |
| 957 |
* None. |
| 958 |
* |
| 959 |
* Side effects: |
| 960 |
* Deletes the hash table of channels. May close channels. May flush |
| 961 |
* output on closed channels. Removes any channelEvent handlers that were |
| 962 |
* registered in this interpreter. |
| 963 |
* |
| 964 |
*---------------------------------------------------------------------- |
| 965 |
*/ |
| 966 |
|
| 967 |
static void |
| 968 |
DeleteChannelTable( |
| 969 |
void *clientData, /* The per-interpreter data structure. */ |
| 970 |
Tcl_Interp *interp) /* The interpreter being deleted. */ |
| 971 |
{ |
| 972 |
Tcl_HashTable *hTblPtr; /* The hash table. */ |
| 973 |
Tcl_HashSearch hSearch; /* Search variable. */ |
| 974 |
Tcl_HashEntry *hPtr; /* Search variable. */ |
| 975 |
Channel *chanPtr; /* Channel being deleted. */ |
| 976 |
ChannelState *statePtr; /* State of Channel being deleted. */ |
| 977 |
EventScriptRecord *sPtr, *prevPtr, *nextPtr; |
| 978 |
/* Variables to loop over all channel events |
| 979 |
* registered, to delete the ones that refer |
| 980 |
* to the interpreter being deleted. */ |
| 981 |
|
| 982 |
/* |
| 983 |
* Delete all the registered channels - this will close channels whose |
| 984 |
* refcount reaches zero. |
| 985 |
*/ |
| 986 |
|
| 987 |
hTblPtr = (Tcl_HashTable *)clientData; |
| 988 |
for (hPtr = Tcl_FirstHashEntry(hTblPtr, &hSearch); hPtr != NULL; |
| 989 |
hPtr = Tcl_FirstHashEntry(hTblPtr, &hSearch)) { |
| 990 |
chanPtr = (Channel *)Tcl_GetHashValue(hPtr); |
| 991 |
statePtr = chanPtr->state; |
| 992 |
|
| 993 |
/* |
| 994 |
* Remove any file events registered in this interpreter. |
| 995 |
*/ |
| 996 |
|
| 997 |
for (sPtr = statePtr->scriptRecordPtr, prevPtr = NULL; |
| 998 |
sPtr != NULL; sPtr = nextPtr) { |
| 999 |
nextPtr = sPtr->nextPtr; |
| 1000 |
if (sPtr->interp == interp) { |
| 1001 |
if (prevPtr == NULL) { |
| 1002 |
statePtr->scriptRecordPtr = nextPtr; |
| 1003 |
} else { |
| 1004 |
prevPtr->nextPtr = nextPtr; |
| 1005 |
} |
| 1006 |
|
| 1007 |
Tcl_DeleteChannelHandler((Tcl_Channel) chanPtr, |
| 1008 |
TclChannelEventScriptInvoker, sPtr); |
| 1009 |
|
| 1010 |
TclDecrRefCount(sPtr->scriptPtr); |
| 1011 |
ckfree(sPtr); |
| 1012 |
} else { |
| 1013 |
prevPtr = sPtr; |
| 1014 |
} |
| 1015 |
} |
| 1016 |
|
| 1017 |
/* |
| 1018 |
* Cannot call Tcl_UnregisterChannel because that procedure calls |
| 1019 |
* Tcl_GetAssocData to get the channel table, which might already be |
| 1020 |
* inaccessible from the interpreter structure. Instead, we emulate |
| 1021 |
* the behavior of Tcl_UnregisterChannel directly here. |
| 1022 |
*/ |
| 1023 |
|
| 1024 |
Tcl_DeleteHashEntry(hPtr); |
| 1025 |
statePtr->epoch++; |
| 1026 |
if (statePtr->refCount-- <= 1) { |
| 1027 |
if (!GotFlag(statePtr, BG_FLUSH_SCHEDULED)) { |
| 1028 |
(void) Tcl_Close(interp, (Tcl_Channel) chanPtr); |
| 1029 |
} |
| 1030 |
} |
| 1031 |
|
| 1032 |
} |
| 1033 |
Tcl_DeleteHashTable(hTblPtr); |
| 1034 |
ckfree(hTblPtr); |
| 1035 |
} |
| 1036 |
|
| 1037 |
/* |
| 1038 |
*---------------------------------------------------------------------- |
| 1039 |
* |
| 1040 |
* CheckForStdChannelsBeingClosed -- |
| 1041 |
* |
| 1042 |
* Perform special handling for standard channels being closed. When |
| 1043 |
* given a standard channel, if the refcount is now 1, it means that the |
| 1044 |
* last reference to the standard channel is being explicitly closed. Now |
| 1045 |
* bump the refcount artificially down to 0, to ensure the normal |
| 1046 |
* handling of channels being closed will occur. Also reset the static |
| 1047 |
* pointer to the channel to NULL, to avoid dangling references. |
| 1048 |
* |
| 1049 |
* Results: |
| 1050 |
* None. |
| 1051 |
* |
| 1052 |
* Side effects: |
| 1053 |
* Manipulates the refcount on standard channels. May smash the global |
| 1054 |
* static pointer to a standard channel. |
| 1055 |
* |
| 1056 |
*---------------------------------------------------------------------- |
| 1057 |
*/ |
| 1058 |
|
| 1059 |
static void |
| 1060 |
CheckForStdChannelsBeingClosed( |
| 1061 |
Tcl_Channel chan) |
| 1062 |
{ |
| 1063 |
ChannelState *statePtr = ((Channel *) chan)->state; |
| 1064 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 1065 |
|
| 1066 |
if (tsdPtr->stdinInitialized == 1 |
| 1067 |
&& tsdPtr->stdinChannel != NULL |
| 1068 |
&& statePtr == ((Channel *)tsdPtr->stdinChannel)->state) { |
| 1069 |
if (statePtr->refCount < 2) { |
| 1070 |
statePtr->refCount = 0; |
| 1071 |
tsdPtr->stdinChannel = NULL; |
| 1072 |
return; |
| 1073 |
} |
| 1074 |
} else if (tsdPtr->stdoutInitialized == 1 |
| 1075 |
&& tsdPtr->stdoutChannel != NULL |
| 1076 |
&& statePtr == ((Channel *)tsdPtr->stdoutChannel)->state) { |
| 1077 |
if (statePtr->refCount < 2) { |
| 1078 |
statePtr->refCount = 0; |
| 1079 |
tsdPtr->stdoutChannel = NULL; |
| 1080 |
return; |
| 1081 |
} |
| 1082 |
} else if (tsdPtr->stderrInitialized == 1 |
| 1083 |
&& tsdPtr->stderrChannel != NULL |
| 1084 |
&& statePtr == ((Channel *)tsdPtr->stderrChannel)->state) { |
| 1085 |
if (statePtr->refCount < 2) { |
| 1086 |
statePtr->refCount = 0; |
| 1087 |
tsdPtr->stderrChannel = NULL; |
| 1088 |
return; |
| 1089 |
} |
| 1090 |
} |
| 1091 |
} |
| 1092 |
|
| 1093 |
/* |
| 1094 |
*---------------------------------------------------------------------- |
| 1095 |
* |
| 1096 |
* Tcl_IsStandardChannel -- |
| 1097 |
* |
| 1098 |
* Test if the given channel is a standard channel. No attempt is made to |
| 1099 |
* check if the channel or the standard channels are initialized or |
| 1100 |
* otherwise valid. |
| 1101 |
* |
| 1102 |
* Results: |
| 1103 |
* Returns 1 if true, 0 if false. |
| 1104 |
* |
| 1105 |
* Side effects: |
| 1106 |
* None. |
| 1107 |
* |
| 1108 |
*---------------------------------------------------------------------- |
| 1109 |
*/ |
| 1110 |
|
| 1111 |
int |
| 1112 |
Tcl_IsStandardChannel( |
| 1113 |
Tcl_Channel chan) /* Channel to check. */ |
| 1114 |
{ |
| 1115 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 1116 |
|
| 1117 |
if ((chan == tsdPtr->stdinChannel) |
| 1118 |
|| (chan == tsdPtr->stdoutChannel) |
| 1119 |
|| (chan == tsdPtr->stderrChannel)) { |
| 1120 |
return 1; |
| 1121 |
} else { |
| 1122 |
return 0; |
| 1123 |
} |
| 1124 |
} |
| 1125 |
|
| 1126 |
/* |
| 1127 |
*---------------------------------------------------------------------- |
| 1128 |
* |
| 1129 |
* Tcl_RegisterChannel -- |
| 1130 |
* |
| 1131 |
* Adds an already-open channel to the channel table of an interpreter. |
| 1132 |
* If the interpreter passed as argument is NULL, it only increments the |
| 1133 |
* channel refCount. |
| 1134 |
* |
| 1135 |
* Results: |
| 1136 |
* None. |
| 1137 |
* |
| 1138 |
* Side effects: |
| 1139 |
* May increment the reference count of a channel. |
| 1140 |
* |
| 1141 |
*---------------------------------------------------------------------- |
| 1142 |
*/ |
| 1143 |
|
| 1144 |
void |
| 1145 |
Tcl_RegisterChannel( |
| 1146 |
Tcl_Interp *interp, /* Interpreter in which to add the channel. */ |
| 1147 |
Tcl_Channel chan) /* The channel to add to this interpreter |
| 1148 |
* channel table. */ |
| 1149 |
{ |
| 1150 |
Tcl_HashTable *hTblPtr; /* Hash table of channels. */ |
| 1151 |
Tcl_HashEntry *hPtr; /* Search variable. */ |
| 1152 |
int isNew; /* Is the hash entry new or does it exist? */ |
| 1153 |
Channel *chanPtr; /* The actual channel. */ |
| 1154 |
ChannelState *statePtr; /* State of the actual channel. */ |
| 1155 |
|
| 1156 |
/* |
| 1157 |
* Always (un)register bottom-most channel in the stack. This makes |
| 1158 |
* management of the channel list easier because no manipulation is |
| 1159 |
* necessary during (un)stack operation. |
| 1160 |
*/ |
| 1161 |
|
| 1162 |
chanPtr = ((Channel *) chan)->state->bottomChanPtr; |
| 1163 |
statePtr = chanPtr->state; |
| 1164 |
|
| 1165 |
if (statePtr->channelName == NULL) { |
| 1166 |
Tcl_Panic("Tcl_RegisterChannel: channel without name"); |
| 1167 |
} |
| 1168 |
if (interp != NULL) { |
| 1169 |
hTblPtr = GetChannelTable(interp); |
| 1170 |
hPtr = Tcl_CreateHashEntry(hTblPtr, statePtr->channelName, &isNew); |
| 1171 |
if (!isNew) { |
| 1172 |
if (chan == Tcl_GetHashValue(hPtr)) { |
| 1173 |
return; |
| 1174 |
} |
| 1175 |
|
| 1176 |
Tcl_Panic("Tcl_RegisterChannel: duplicate channel names"); |
| 1177 |
} |
| 1178 |
Tcl_SetHashValue(hPtr, chanPtr); |
| 1179 |
} |
| 1180 |
statePtr->refCount++; |
| 1181 |
} |
| 1182 |
|
| 1183 |
/* |
| 1184 |
*---------------------------------------------------------------------- |
| 1185 |
* |
| 1186 |
* Tcl_UnregisterChannel -- |
| 1187 |
* |
| 1188 |
* Deletes the hash entry for a channel associated with an interpreter. |
| 1189 |
* If the interpreter given as argument is NULL, it only decrements the |
| 1190 |
* reference count. (This all happens in the Tcl_DetachChannel helper |
| 1191 |
* function). |
| 1192 |
* |
| 1193 |
* Finally, if the reference count of the channel drops to zero, it is |
| 1194 |
* deleted. |
| 1195 |
* |
| 1196 |
* Results: |
| 1197 |
* A standard Tcl result. |
| 1198 |
* |
| 1199 |
* Side effects: |
| 1200 |
* Calls Tcl_DetachChannel which deletes the hash entry for a channel |
| 1201 |
* associated with an interpreter. |
| 1202 |
* |
| 1203 |
* May delete the channel, which can have a variety of consequences, |
| 1204 |
* especially if we are forced to close the channel. |
| 1205 |
* |
| 1206 |
*---------------------------------------------------------------------- |
| 1207 |
*/ |
| 1208 |
|
| 1209 |
int |
| 1210 |
Tcl_UnregisterChannel( |
| 1211 |
Tcl_Interp *interp, /* Interpreter in which channel is defined. */ |
| 1212 |
Tcl_Channel chan) /* Channel to delete. */ |
| 1213 |
{ |
| 1214 |
ChannelState *statePtr; /* State of the real channel. */ |
| 1215 |
|
| 1216 |
statePtr = ((Channel *) chan)->state->bottomChanPtr->state; |
| 1217 |
|
| 1218 |
if (GotFlag(statePtr, CHANNEL_INCLOSE)) { |
| 1219 |
if (interp != NULL) { |
| 1220 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 1221 |
"illegal recursive call to close through close-handler" |
| 1222 |
" of channel", -1)); |
| 1223 |
} |
| 1224 |
return TCL_ERROR; |
| 1225 |
} |
| 1226 |
|
| 1227 |
if (DetachChannel(interp, chan) != TCL_OK) { |
| 1228 |
return TCL_OK; |
| 1229 |
} |
| 1230 |
|
| 1231 |
statePtr = ((Channel *) chan)->state->bottomChanPtr->state; |
| 1232 |
|
| 1233 |
/* |
| 1234 |
* Perform special handling for standard channels being closed. If the |
| 1235 |
* refCount is now 1 it means that the last reference to the standard |
| 1236 |
* channel is being explicitly closed, so bump the refCount down |
| 1237 |
* artificially to 0. This will ensure that the channel is actually |
| 1238 |
* closed, below. Also set the static pointer to NULL for the channel. |
| 1239 |
*/ |
| 1240 |
|
| 1241 |
CheckForStdChannelsBeingClosed(chan); |
| 1242 |
|
| 1243 |
/* |
| 1244 |
* If the refCount reached zero, close the actual channel. |
| 1245 |
*/ |
| 1246 |
|
| 1247 |
if (statePtr->refCount <= 0) { |
| 1248 |
Tcl_Preserve(statePtr); |
| 1249 |
if (!GotFlag(statePtr, BG_FLUSH_SCHEDULED)) { |
| 1250 |
/* |
| 1251 |
* We don't want to re-enter Tcl_Close(). |
| 1252 |
*/ |
| 1253 |
|
| 1254 |
if (!GotFlag(statePtr, CHANNEL_CLOSED)) { |
| 1255 |
if (Tcl_Close(interp, chan) != TCL_OK) { |
| 1256 |
SetFlag(statePtr, CHANNEL_CLOSED); |
| 1257 |
Tcl_Release(statePtr); |
| 1258 |
return TCL_ERROR; |
| 1259 |
} |
| 1260 |
} |
| 1261 |
} |
| 1262 |
SetFlag(statePtr, CHANNEL_CLOSED); |
| 1263 |
Tcl_Release(statePtr); |
| 1264 |
} |
| 1265 |
return TCL_OK; |
| 1266 |
} |
| 1267 |
|
| 1268 |
/* |
| 1269 |
*---------------------------------------------------------------------- |
| 1270 |
* |
| 1271 |
* Tcl_DetachChannel -- |
| 1272 |
* |
| 1273 |
* Deletes the hash entry for a channel associated with an interpreter. |
| 1274 |
* If the interpreter given as argument is NULL, it only decrements the |
| 1275 |
* reference count. Even if the ref count drops to zero, the channel is |
| 1276 |
* NOT closed or cleaned up. This allows a channel to be detached from an |
| 1277 |
* interpreter and left in the same state it was in when it was |
| 1278 |
* originally returned by 'Tcl_OpenFileChannel', for example. |
| 1279 |
* |
| 1280 |
* This function cannot be used on the standard channels, and will return |
| 1281 |
* TCL_ERROR if that is attempted. |
| 1282 |
* |
| 1283 |
* This function should only be necessary for special purposes in which |
| 1284 |
* you need to generate a pristine channel from one that has already been |
| 1285 |
* used. All ordinary purposes will almost always want to use |
| 1286 |
* Tcl_UnregisterChannel instead. |
| 1287 |
* |
| 1288 |
* Provided the channel is not attached to any other interpreter, it can |
| 1289 |
* then be closed with Tcl_Close, rather than with Tcl_UnregisterChannel. |
| 1290 |
* |
| 1291 |
* Results: |
| 1292 |
* A standard Tcl result. If the channel is not currently registered with |
| 1293 |
* the given interpreter, TCL_ERROR is returned, otherwise TCL_OK. |
| 1294 |
* However no error messages are left in the interp's result. |
| 1295 |
* |
| 1296 |
* Side effects: |
| 1297 |
* Deletes the hash entry for a channel associated with an interpreter. |
| 1298 |
* |
| 1299 |
*---------------------------------------------------------------------- |
| 1300 |
*/ |
| 1301 |
|
| 1302 |
int |
| 1303 |
Tcl_DetachChannel( |
| 1304 |
Tcl_Interp *interp, /* Interpreter in which channel is defined. */ |
| 1305 |
Tcl_Channel chan) /* Channel to delete. */ |
| 1306 |
{ |
| 1307 |
if (Tcl_IsStandardChannel(chan)) { |
| 1308 |
return TCL_ERROR; |
| 1309 |
} |
| 1310 |
|
| 1311 |
return DetachChannel(interp, chan); |
| 1312 |
} |
| 1313 |
|
| 1314 |
/* |
| 1315 |
*---------------------------------------------------------------------- |
| 1316 |
* |
| 1317 |
* DetachChannel -- |
| 1318 |
* |
| 1319 |
* Deletes the hash entry for a channel associated with an interpreter. |
| 1320 |
* If the interpreter given as argument is NULL, it only decrements the |
| 1321 |
* reference count. Even if the ref count drops to zero, the channel is |
| 1322 |
* NOT closed or cleaned up. This allows a channel to be detached from an |
| 1323 |
* interpreter and left in the same state it was in when it was |
| 1324 |
* originally returned by 'Tcl_OpenFileChannel', for example. |
| 1325 |
* |
| 1326 |
* Results: |
| 1327 |
* A standard Tcl result. If the channel is not currently registered with |
| 1328 |
* the given interpreter, TCL_ERROR is returned, otherwise TCL_OK. |
| 1329 |
* However no error messages are left in the interp's result. |
| 1330 |
* |
| 1331 |
* Side effects: |
| 1332 |
* Deletes the hash entry for a channel associated with an interpreter. |
| 1333 |
* |
| 1334 |
*---------------------------------------------------------------------- |
| 1335 |
*/ |
| 1336 |
|
| 1337 |
static int |
| 1338 |
DetachChannel( |
| 1339 |
Tcl_Interp *interp, /* Interpreter in which channel is defined. */ |
| 1340 |
Tcl_Channel chan) /* Channel to delete. */ |
| 1341 |
{ |
| 1342 |
Tcl_HashTable *hTblPtr; /* Hash table of channels. */ |
| 1343 |
Tcl_HashEntry *hPtr; /* Search variable. */ |
| 1344 |
Channel *chanPtr; /* The real IO channel. */ |
| 1345 |
ChannelState *statePtr; /* State of the real channel. */ |
| 1346 |
|
| 1347 |
/* |
| 1348 |
* Always (un)register bottom-most channel in the stack. This makes |
| 1349 |
* management of the channel list easier because no manipulation is |
| 1350 |
* necessary during (un)stack operation. |
| 1351 |
*/ |
| 1352 |
|
| 1353 |
chanPtr = ((Channel *) chan)->state->bottomChanPtr; |
| 1354 |
statePtr = chanPtr->state; |
| 1355 |
|
| 1356 |
if (interp != NULL) { |
| 1357 |
hTblPtr = (Tcl_HashTable *)Tcl_GetAssocData(interp, "tclIO", NULL); |
| 1358 |
if (hTblPtr == NULL) { |
| 1359 |
return TCL_ERROR; |
| 1360 |
} |
| 1361 |
hPtr = Tcl_FindHashEntry(hTblPtr, statePtr->channelName); |
| 1362 |
if (hPtr == NULL) { |
| 1363 |
return TCL_ERROR; |
| 1364 |
} |
| 1365 |
if ((Channel *) Tcl_GetHashValue(hPtr) != chanPtr) { |
| 1366 |
return TCL_ERROR; |
| 1367 |
} |
| 1368 |
Tcl_DeleteHashEntry(hPtr); |
| 1369 |
statePtr->epoch++; |
| 1370 |
|
| 1371 |
/* |
| 1372 |
* Remove channel handlers that refer to this interpreter, so that |
| 1373 |
* they will not be present if the actual close is delayed and more |
| 1374 |
* events happen on the channel. This may occur if the channel is |
| 1375 |
* shared between several interpreters, or if the channel has async |
| 1376 |
* flushing active. |
| 1377 |
*/ |
| 1378 |
|
| 1379 |
CleanupChannelHandlers(interp, chanPtr); |
| 1380 |
} |
| 1381 |
|
| 1382 |
statePtr->refCount--; |
| 1383 |
|
| 1384 |
return TCL_OK; |
| 1385 |
} |
| 1386 |
|
| 1387 |
/* |
| 1388 |
*--------------------------------------------------------------------------- |
| 1389 |
* |
| 1390 |
* Tcl_GetChannel -- |
| 1391 |
* |
| 1392 |
* Finds an existing Tcl_Channel structure by name in a given |
| 1393 |
* interpreter. This function is public because it is used by |
| 1394 |
* channel-type-specific functions. |
| 1395 |
* |
| 1396 |
* Results: |
| 1397 |
* A Tcl_Channel or NULL on failure. If failed, interp's result object |
| 1398 |
* contains an error message. *modePtr is filled with the modes in which |
| 1399 |
* the channel was opened. |
| 1400 |
* |
| 1401 |
* Side effects: |
| 1402 |
* None. |
| 1403 |
* |
| 1404 |
*--------------------------------------------------------------------------- |
| 1405 |
*/ |
| 1406 |
|
| 1407 |
Tcl_Channel |
| 1408 |
Tcl_GetChannel( |
| 1409 |
Tcl_Interp *interp, /* Interpreter in which to find or create the |
| 1410 |
* channel. */ |
| 1411 |
const char *chanName, /* The name of the channel. */ |
| 1412 |
int *modePtr) /* Where to store the mode in which the |
| 1413 |
* channel was opened? Will contain an OR'ed |
| 1414 |
* combination of TCL_READABLE and |
| 1415 |
* TCL_WRITABLE, if non-NULL. */ |
| 1416 |
{ |
| 1417 |
Channel *chanPtr; /* The actual channel. */ |
| 1418 |
Tcl_HashTable *hTblPtr; /* Hash table of channels. */ |
| 1419 |
Tcl_HashEntry *hPtr; /* Search variable. */ |
| 1420 |
const char *name; /* Translated name. */ |
| 1421 |
|
| 1422 |
/* |
| 1423 |
* Substitute "stdin", etc. Note that even though we immediately find the |
| 1424 |
* channel using Tcl_GetStdChannel, we still need to look it up in the |
| 1425 |
* specified interpreter to ensure that it is present in the channel |
| 1426 |
* table. Otherwise, safe interpreters would always have access to the |
| 1427 |
* standard channels. |
| 1428 |
*/ |
| 1429 |
|
| 1430 |
name = chanName; |
| 1431 |
if ((chanName[0] == 's') && (chanName[1] == 't')) { |
| 1432 |
chanPtr = NULL; |
| 1433 |
if (strcmp(chanName, "stdin") == 0) { |
| 1434 |
chanPtr = (Channel *) Tcl_GetStdChannel(TCL_STDIN); |
| 1435 |
} else if (strcmp(chanName, "stdout") == 0) { |
| 1436 |
chanPtr = (Channel *) Tcl_GetStdChannel(TCL_STDOUT); |
| 1437 |
} else if (strcmp(chanName, "stderr") == 0) { |
| 1438 |
chanPtr = (Channel *) Tcl_GetStdChannel(TCL_STDERR); |
| 1439 |
} |
| 1440 |
if (chanPtr != NULL) { |
| 1441 |
name = chanPtr->state->channelName; |
| 1442 |
} |
| 1443 |
} |
| 1444 |
|
| 1445 |
hTblPtr = GetChannelTable(interp); |
| 1446 |
hPtr = Tcl_FindHashEntry(hTblPtr, name); |
| 1447 |
if (hPtr == NULL) { |
| 1448 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1449 |
"can not find channel named \"%s\"", chanName)); |
| 1450 |
Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "CHANNEL", chanName, (char *)NULL); |
| 1451 |
return NULL; |
| 1452 |
} |
| 1453 |
|
| 1454 |
/* |
| 1455 |
* Always return bottom-most channel in the stack. This one lives the |
| 1456 |
* longest - other channels may go away unnoticed. The other APIs |
| 1457 |
* compensate where necessary to retrieve the topmost channel again. |
| 1458 |
*/ |
| 1459 |
|
| 1460 |
chanPtr = (Channel *)Tcl_GetHashValue(hPtr); |
| 1461 |
chanPtr = chanPtr->state->bottomChanPtr; |
| 1462 |
if (modePtr != NULL) { |
| 1463 |
*modePtr = GotFlag(chanPtr->state, TCL_READABLE|TCL_WRITABLE); |
| 1464 |
} |
| 1465 |
|
| 1466 |
return (Tcl_Channel) chanPtr; |
| 1467 |
} |
| 1468 |
|
| 1469 |
/* |
| 1470 |
*--------------------------------------------------------------------------- |
| 1471 |
* |
| 1472 |
* TclGetChannelFromObj -- |
| 1473 |
* |
| 1474 |
* Finds an existing Tcl_Channel structure by name in a given |
| 1475 |
* interpreter. This function is public because it is used by |
| 1476 |
* channel-type-specific functions. |
| 1477 |
* |
| 1478 |
* Results: |
| 1479 |
* A Tcl_Channel or NULL on failure. If failed, interp's result object |
| 1480 |
* contains an error message. *modePtr is filled with the modes in which |
| 1481 |
* the channel was opened. |
| 1482 |
* |
| 1483 |
* Side effects: |
| 1484 |
* None. |
| 1485 |
* |
| 1486 |
*--------------------------------------------------------------------------- |
| 1487 |
*/ |
| 1488 |
|
| 1489 |
int |
| 1490 |
TclGetChannelFromObj( |
| 1491 |
Tcl_Interp *interp, /* Interpreter in which to find or create the |
| 1492 |
* channel. */ |
| 1493 |
Tcl_Obj *objPtr, |
| 1494 |
Tcl_Channel *channelPtr, |
| 1495 |
int *modePtr, /* Where to store the mode in which the |
| 1496 |
* channel was opened? Will contain an OR'ed |
| 1497 |
* combination of TCL_READABLE and |
| 1498 |
* TCL_WRITABLE, if non-NULL. */ |
| 1499 |
int flags) |
| 1500 |
{ |
| 1501 |
ChannelState *statePtr; |
| 1502 |
ResolvedChanName *resPtr = NULL; |
| 1503 |
Tcl_Channel chan; |
| 1504 |
(void)flags; |
| 1505 |
|
| 1506 |
if (interp == NULL) { |
| 1507 |
return TCL_ERROR; |
| 1508 |
} |
| 1509 |
|
| 1510 |
if (objPtr->typePtr == &chanObjType) { |
| 1511 |
/* |
| 1512 |
* Confirm validity of saved lookup results. |
| 1513 |
*/ |
| 1514 |
|
| 1515 |
resPtr = (ResolvedChanName *)objPtr->internalRep.twoPtrValue.ptr1; |
| 1516 |
statePtr = resPtr->statePtr; |
| 1517 |
if ((resPtr->interp == interp) /* Same interp context */ |
| 1518 |
/* No epoch change in channel since lookup */ |
| 1519 |
&& (resPtr->epoch == statePtr->epoch)) { |
| 1520 |
/* |
| 1521 |
* Have a valid saved lookup. Jump to end to return it. |
| 1522 |
*/ |
| 1523 |
|
| 1524 |
goto valid; |
| 1525 |
} |
| 1526 |
} |
| 1527 |
|
| 1528 |
chan = Tcl_GetChannel(interp, TclGetString(objPtr), NULL); |
| 1529 |
|
| 1530 |
if (chan == NULL) { |
| 1531 |
if (resPtr) { |
| 1532 |
FreeChannelInternalRep(objPtr); |
| 1533 |
} |
| 1534 |
return TCL_ERROR; |
| 1535 |
} |
| 1536 |
|
| 1537 |
if (resPtr && resPtr->refCount == 1) { |
| 1538 |
/* Re-use the ResolvedCmdName struct */ |
| 1539 |
Tcl_Release((void *)resPtr->statePtr); |
| 1540 |
|
| 1541 |
} else { |
| 1542 |
TclFreeIntRep(objPtr); |
| 1543 |
|
| 1544 |
resPtr = (ResolvedChanName *) ckalloc(sizeof(ResolvedChanName)); |
| 1545 |
resPtr->refCount = 1; |
| 1546 |
objPtr->internalRep.twoPtrValue.ptr1 = (void *)resPtr; |
| 1547 |
objPtr->typePtr = &chanObjType; |
| 1548 |
} |
| 1549 |
statePtr = ((Channel *)chan)->state; |
| 1550 |
resPtr->statePtr = statePtr; |
| 1551 |
Tcl_Preserve((void *) statePtr); |
| 1552 |
resPtr->interp = interp; |
| 1553 |
resPtr->epoch = statePtr->epoch; |
| 1554 |
|
| 1555 |
valid: |
| 1556 |
*channelPtr = (Tcl_Channel) statePtr->bottomChanPtr; |
| 1557 |
|
| 1558 |
if (modePtr != NULL) { |
| 1559 |
*modePtr = GotFlag(statePtr, TCL_READABLE|TCL_WRITABLE); |
| 1560 |
} |
| 1561 |
|
| 1562 |
return TCL_OK; |
| 1563 |
} |
| 1564 |
|
| 1565 |
/* |
| 1566 |
*---------------------------------------------------------------------- |
| 1567 |
* |
| 1568 |
* Tcl_CreateChannel -- |
| 1569 |
* |
| 1570 |
* Creates a new entry in the hash table for a Tcl_Channel record. |
| 1571 |
* |
| 1572 |
* Results: |
| 1573 |
* Returns the new Tcl_Channel. |
| 1574 |
* |
| 1575 |
* Side effects: |
| 1576 |
* Creates a new Tcl_Channel instance and inserts it into the hash table. |
| 1577 |
* |
| 1578 |
*---------------------------------------------------------------------- |
| 1579 |
*/ |
| 1580 |
|
| 1581 |
Tcl_Channel |
| 1582 |
Tcl_CreateChannel( |
| 1583 |
const Tcl_ChannelType *typePtr, /* The channel type record. */ |
| 1584 |
const char *chanName, /* Name of channel to record. */ |
| 1585 |
void *instanceData, /* Instance specific data. */ |
| 1586 |
int mask) /* TCL_READABLE & TCL_WRITABLE to indicate if |
| 1587 |
* the channel is readable, writable. */ |
| 1588 |
{ |
| 1589 |
Channel *chanPtr; /* The channel structure newly created. */ |
| 1590 |
ChannelState *statePtr; /* The stack-level independent state info for |
| 1591 |
* the channel. */ |
| 1592 |
const char *name; |
| 1593 |
char *tmp; |
| 1594 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 1595 |
|
| 1596 |
/* |
| 1597 |
* With the change of the Tcl_ChannelType structure to use a version in |
| 1598 |
* 8.3.2+, we have to make sure that our assumption that the structure |
| 1599 |
* remains a binary compatible size is true. |
| 1600 |
* |
| 1601 |
* If this assertion fails on some system, then it can be removed only if |
| 1602 |
* the user recompiles code with older channel drivers in the new system |
| 1603 |
* as well. |
| 1604 |
*/ |
| 1605 |
|
| 1606 |
assert(sizeof(Tcl_ChannelTypeVersion) == sizeof(Tcl_DriverBlockModeProc *)); |
| 1607 |
assert(typePtr->typeName != NULL); |
| 1608 |
if (((NULL == typePtr->closeProc) || (TCL_CLOSE2PROC == typePtr->closeProc)) && (typePtr->close2Proc == NULL)) { |
| 1609 |
Tcl_Panic("channel type %s must define closeProc or close2Proc", typePtr->typeName); |
| 1610 |
} |
| 1611 |
if ((TCL_READABLE & mask) && (NULL == typePtr->inputProc)) { |
| 1612 |
Tcl_Panic("channel type %s must define inputProc when used for reader channel", typePtr->typeName); |
| 1613 |
} |
| 1614 |
if ((TCL_WRITABLE & mask) && (NULL == typePtr->outputProc)) { |
| 1615 |
Tcl_Panic("channel type %s must define outputProc when used for writer channel", typePtr->typeName); |
| 1616 |
} |
| 1617 |
if (NULL == typePtr->watchProc) { |
| 1618 |
Tcl_Panic("channel type %s must define watchProc", typePtr->typeName); |
| 1619 |
} |
| 1620 |
if ((NULL != typePtr->wideSeekProc) && (NULL == typePtr->seekProc)) { |
| 1621 |
Tcl_Panic("channel type %s must define seekProc if defining wideSeekProc", typePtr->typeName); |
| 1622 |
} |
| 1623 |
|
| 1624 |
/* |
| 1625 |
* JH: We could subsequently memset these to 0 to avoid the numerous |
| 1626 |
* assignments to 0/NULL below. |
| 1627 |
*/ |
| 1628 |
|
| 1629 |
chanPtr = (Channel *)ckalloc(sizeof(Channel)); |
| 1630 |
statePtr = (ChannelState *)ckalloc(sizeof(ChannelState)); |
| 1631 |
chanPtr->state = statePtr; |
| 1632 |
|
| 1633 |
chanPtr->instanceData = instanceData; |
| 1634 |
chanPtr->typePtr = typePtr; |
| 1635 |
|
| 1636 |
/* |
| 1637 |
* Set all the bits that are part of the stack-independent state |
| 1638 |
* information for the channel. |
| 1639 |
*/ |
| 1640 |
|
| 1641 |
if (chanName != NULL) { |
| 1642 |
unsigned len = strlen(chanName) + 1; |
| 1643 |
|
| 1644 |
/* |
| 1645 |
* Make sure we allocate at least 7 bytes, so it fits for "stdout" |
| 1646 |
* later. |
| 1647 |
*/ |
| 1648 |
|
| 1649 |
tmp = (char *)ckalloc((len < 7) ? 7 : len); |
| 1650 |
strcpy(tmp, chanName); |
| 1651 |
} else { |
| 1652 |
tmp = (char *)ckalloc(7); |
| 1653 |
tmp[0] = '\0'; |
| 1654 |
} |
| 1655 |
statePtr->channelName = tmp; |
| 1656 |
statePtr->flags = mask; |
| 1657 |
|
| 1658 |
/* |
| 1659 |
* Set the channel to system default encoding. |
| 1660 |
* |
| 1661 |
* Note the strange bit of protection taking place here. If the system |
| 1662 |
* encoding name is reported back as "binary", something weird is |
| 1663 |
* happening. Tcl provides no "binary" encoding, so someone else has |
| 1664 |
* provided one. We ignore it so as not to interfere with the "magic" |
| 1665 |
* interpretation that Tcl_Channels give to the "-encoding binary" option. |
| 1666 |
*/ |
| 1667 |
|
| 1668 |
statePtr->encoding = NULL; |
| 1669 |
name = Tcl_GetEncodingName(NULL); |
| 1670 |
if (strcmp(name, "binary") != 0) { |
| 1671 |
statePtr->encoding = Tcl_GetEncoding(NULL, name); |
| 1672 |
} |
| 1673 |
statePtr->inputEncodingState = NULL; |
| 1674 |
statePtr->inputEncodingFlags = TCL_ENCODING_START; |
| 1675 |
statePtr->outputEncodingState = NULL; |
| 1676 |
statePtr->outputEncodingFlags = TCL_ENCODING_START; |
| 1677 |
|
| 1678 |
/* |
| 1679 |
* Set the channel up initially in AUTO input translation mode to accept |
| 1680 |
* "\n", "\r" and "\r\n". Output translation mode is set to a platform |
| 1681 |
* specific default value. The eofChar is set to 0 for both input and |
| 1682 |
* output, so that Tcl does not look for an in-file EOF indicator (e.g., |
| 1683 |
* ^Z) and does not append an EOF indicator to files. |
| 1684 |
*/ |
| 1685 |
|
| 1686 |
statePtr->inputTranslation = TCL_TRANSLATE_AUTO; |
| 1687 |
statePtr->outputTranslation = TCL_PLATFORM_TRANSLATION; |
| 1688 |
statePtr->inEofChar = 0; |
| 1689 |
statePtr->outEofChar = 0; |
| 1690 |
|
| 1691 |
statePtr->unreportedError = 0; |
| 1692 |
statePtr->refCount = 0; |
| 1693 |
statePtr->closeCbPtr = NULL; |
| 1694 |
statePtr->curOutPtr = NULL; |
| 1695 |
statePtr->outQueueHead = NULL; |
| 1696 |
statePtr->outQueueTail = NULL; |
| 1697 |
statePtr->saveInBufPtr = NULL; |
| 1698 |
statePtr->inQueueHead = NULL; |
| 1699 |
statePtr->inQueueTail = NULL; |
| 1700 |
statePtr->chPtr = NULL; |
| 1701 |
statePtr->interestMask = 0; |
| 1702 |
statePtr->scriptRecordPtr = NULL; |
| 1703 |
statePtr->bufSize = CHANNELBUFFER_DEFAULT_SIZE; |
| 1704 |
statePtr->timer = NULL; |
| 1705 |
statePtr->timerChanPtr = NULL; |
| 1706 |
statePtr->csPtrR = NULL; |
| 1707 |
statePtr->csPtrW = NULL; |
| 1708 |
statePtr->outputStage = NULL; |
| 1709 |
|
| 1710 |
/* |
| 1711 |
* As we are creating the channel, it is obviously the top for now. |
| 1712 |
*/ |
| 1713 |
|
| 1714 |
statePtr->topChanPtr = chanPtr; |
| 1715 |
statePtr->bottomChanPtr = chanPtr; |
| 1716 |
chanPtr->downChanPtr = NULL; |
| 1717 |
chanPtr->upChanPtr = NULL; |
| 1718 |
chanPtr->inQueueHead = NULL; |
| 1719 |
chanPtr->inQueueTail = NULL; |
| 1720 |
chanPtr->refCount = 0; |
| 1721 |
|
| 1722 |
/* |
| 1723 |
* TIP #219, Tcl Channel Reflection API |
| 1724 |
*/ |
| 1725 |
|
| 1726 |
statePtr->chanMsg = NULL; |
| 1727 |
statePtr->unreportedMsg = NULL; |
| 1728 |
|
| 1729 |
statePtr->epoch = 0; |
| 1730 |
|
| 1731 |
/* |
| 1732 |
* Link the channel into the list of all channels; create an on-exit |
| 1733 |
* handler if there is not one already, to close off all the channels in |
| 1734 |
* the list on exit. |
| 1735 |
* |
| 1736 |
* JH: Could call Tcl_SpliceChannel, but need to avoid NULL check. |
| 1737 |
* |
| 1738 |
* TIP #218. |
| 1739 |
* AK: Just initialize the field to NULL before invoking Tcl_SpliceChannel |
| 1740 |
* We need Tcl_SpliceChannel, for the threadAction calls. There is no |
| 1741 |
* real reason to duplicate all of this. |
| 1742 |
* NOTE: All drivers using thread actions now have to perform their TSD |
| 1743 |
* manipulation only in their thread action proc. Doing it when |
| 1744 |
* creating their instance structures will collide with the thread |
| 1745 |
* action activity and lead to damaged lists. |
| 1746 |
*/ |
| 1747 |
|
| 1748 |
statePtr->nextCSPtr = NULL; |
| 1749 |
SpliceChannel((Tcl_Channel) chanPtr); |
| 1750 |
|
| 1751 |
/* |
| 1752 |
* Install this channel in the first empty standard channel slot, if the |
| 1753 |
* channel was previously closed explicitly. |
| 1754 |
*/ |
| 1755 |
|
| 1756 |
if ((tsdPtr->stdinChannel == NULL) && (tsdPtr->stdinInitialized == 1)) { |
| 1757 |
strcpy(tmp, "stdin"); |
| 1758 |
Tcl_SetStdChannel((Tcl_Channel) chanPtr, TCL_STDIN); |
| 1759 |
Tcl_RegisterChannel(NULL, (Tcl_Channel) chanPtr); |
| 1760 |
} else if ((tsdPtr->stdoutChannel == NULL) && |
| 1761 |
(tsdPtr->stdoutInitialized == 1)) { |
| 1762 |
strcpy(tmp, "stdout"); |
| 1763 |
Tcl_SetStdChannel((Tcl_Channel) chanPtr, TCL_STDOUT); |
| 1764 |
Tcl_RegisterChannel(NULL, (Tcl_Channel) chanPtr); |
| 1765 |
} else if ((tsdPtr->stderrChannel == NULL) && |
| 1766 |
(tsdPtr->stderrInitialized == 1)) { |
| 1767 |
strcpy(tmp, "stderr"); |
| 1768 |
Tcl_SetStdChannel((Tcl_Channel) chanPtr, TCL_STDERR); |
| 1769 |
Tcl_RegisterChannel(NULL, (Tcl_Channel) chanPtr); |
| 1770 |
} |
| 1771 |
return (Tcl_Channel) chanPtr; |
| 1772 |
} |
| 1773 |
|
| 1774 |
/* |
| 1775 |
*---------------------------------------------------------------------- |
| 1776 |
* |
| 1777 |
* Tcl_StackChannel -- |
| 1778 |
* |
| 1779 |
* Replaces an entry in the hash table for a Tcl_Channel record. The |
| 1780 |
* replacement is a new channel with same name, it supercedes the |
| 1781 |
* replaced channel. Input and output of the superceded channel is now |
| 1782 |
* going through the newly created channel and allows the arbitrary |
| 1783 |
* filtering/manipulation of the dataflow. |
| 1784 |
* |
| 1785 |
* Andreas Kupries <[email protected]>, 12/13/1998 "Trf-Patch for |
| 1786 |
* filtering channels" |
| 1787 |
* |
| 1788 |
* Results: |
| 1789 |
* Returns the new Tcl_Channel, which actually contains the saved |
| 1790 |
* information about prevChan. |
| 1791 |
* |
| 1792 |
* Side effects: |
| 1793 |
* A new channel structure is allocated and linked below the existing |
| 1794 |
* channel. The channel operations and client data of the existing |
| 1795 |
* channel are copied down to the newly created channel, and the current |
| 1796 |
* channel has its operations replaced by the new typePtr. |
| 1797 |
* |
| 1798 |
*---------------------------------------------------------------------- |
| 1799 |
*/ |
| 1800 |
|
| 1801 |
Tcl_Channel |
| 1802 |
Tcl_StackChannel( |
| 1803 |
Tcl_Interp *interp, /* The interpreter we are working in */ |
| 1804 |
const Tcl_ChannelType *typePtr, |
| 1805 |
/* The channel type record for the new |
| 1806 |
* channel. */ |
| 1807 |
void *instanceData, /* Instance specific data for the new |
| 1808 |
* channel. */ |
| 1809 |
int mask, /* TCL_READABLE & TCL_WRITABLE to indicate if |
| 1810 |
* the channel is readable, writable. */ |
| 1811 |
Tcl_Channel prevChan) /* The channel structure to replace */ |
| 1812 |
{ |
| 1813 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 1814 |
Channel *chanPtr, *prevChanPtr; |
| 1815 |
ChannelState *statePtr; |
| 1816 |
|
| 1817 |
/* |
| 1818 |
* Find the given channel (prevChan) in the list of all channels. If we do |
| 1819 |
* not find it, then it was never registered correctly. |
| 1820 |
* |
| 1821 |
* This operation should occur at the top of a channel stack. |
| 1822 |
*/ |
| 1823 |
|
| 1824 |
statePtr = (ChannelState *) tsdPtr->firstCSPtr; |
| 1825 |
prevChanPtr = ((Channel *) prevChan)->state->topChanPtr; |
| 1826 |
|
| 1827 |
while ((statePtr != NULL) && (statePtr->topChanPtr != prevChanPtr)) { |
| 1828 |
statePtr = statePtr->nextCSPtr; |
| 1829 |
} |
| 1830 |
|
| 1831 |
if (statePtr == NULL) { |
| 1832 |
if (interp) { |
| 1833 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1834 |
"couldn't find state for channel \"%s\"", |
| 1835 |
Tcl_GetChannelName(prevChan))); |
| 1836 |
} |
| 1837 |
return NULL; |
| 1838 |
} |
| 1839 |
|
| 1840 |
/* |
| 1841 |
* Here we check if the given "mask" matches the "flags" of the already |
| 1842 |
* existing channel. |
| 1843 |
* |
| 1844 |
* | - | R | W | RW | |
| 1845 |
* --+---+---+---+----+ <=> 0 != (chan->mask & prevChan->mask) |
| 1846 |
* - | | | | | |
| 1847 |
* R | | + | | + | The superceding channel is allowed to restrict |
| 1848 |
* W | | | + | + | the capabilities of the superceded one! |
| 1849 |
* RW| | + | + | + | |
| 1850 |
* --+---+---+---+----+ |
| 1851 |
*/ |
| 1852 |
|
| 1853 |
if ((mask & GotFlag(statePtr, TCL_READABLE|TCL_WRITABLE)) == 0) { |
| 1854 |
if (interp) { |
| 1855 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1856 |
"reading and writing both disallowed for channel \"%s\"", |
| 1857 |
Tcl_GetChannelName(prevChan))); |
| 1858 |
} |
| 1859 |
return NULL; |
| 1860 |
} |
| 1861 |
|
| 1862 |
/* |
| 1863 |
* Flush the buffers. This ensures that any data still in them at this |
| 1864 |
* time is not handled by the new transformation. Restrict this to |
| 1865 |
* writable channels. Take care to hide a possible bg-copy in progress |
| 1866 |
* from Tcl_Flush and the CheckForChannelErrors inside. |
| 1867 |
*/ |
| 1868 |
|
| 1869 |
if ((mask & TCL_WRITABLE) != 0) { |
| 1870 |
CopyState *csPtrR = statePtr->csPtrR; |
| 1871 |
CopyState *csPtrW = statePtr->csPtrW; |
| 1872 |
|
| 1873 |
statePtr->csPtrR = NULL; |
| 1874 |
statePtr->csPtrW = NULL; |
| 1875 |
|
| 1876 |
/* |
| 1877 |
* TODO: Examine what can go wrong if Tcl_Flush() call disturbs |
| 1878 |
* the stacking state of this channel during its operations. |
| 1879 |
*/ |
| 1880 |
if (Tcl_Flush((Tcl_Channel) prevChanPtr) != TCL_OK) { |
| 1881 |
statePtr->csPtrR = csPtrR; |
| 1882 |
statePtr->csPtrW = csPtrW; |
| 1883 |
if (interp) { |
| 1884 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1885 |
"could not flush channel \"%s\"", |
| 1886 |
Tcl_GetChannelName(prevChan))); |
| 1887 |
} |
| 1888 |
return NULL; |
| 1889 |
} |
| 1890 |
|
| 1891 |
statePtr->csPtrR = csPtrR; |
| 1892 |
statePtr->csPtrW = csPtrW; |
| 1893 |
} |
| 1894 |
|
| 1895 |
/* |
| 1896 |
* Discard any input in the buffers. They are not yet read by the user of |
| 1897 |
* the channel, so they have to go through the new transformation before |
| 1898 |
* reading. As the buffers contain the untransformed form their contents |
| 1899 |
* are not only useless but actually distorts our view of the system. |
| 1900 |
* |
| 1901 |
* To preserve the information without having to read them again and to |
| 1902 |
* avoid problems with the location in the channel (seeking might be |
| 1903 |
* impossible) we move the buffers from the common state structure into |
| 1904 |
* the channel itself. We use the buffers in the channel below the new |
| 1905 |
* transformation to hold the data. In the future this allows us to write |
| 1906 |
* transformations which preread data and push the unused part back when |
| 1907 |
* they are going away. |
| 1908 |
*/ |
| 1909 |
|
| 1910 |
if (((mask & TCL_READABLE) != 0) && (statePtr->inQueueHead != NULL)) { |
| 1911 |
/* |
| 1912 |
* When statePtr->inQueueHead is not NULL, we know |
| 1913 |
* prevChanPtr->inQueueHead must be NULL. |
| 1914 |
*/ |
| 1915 |
|
| 1916 |
assert(prevChanPtr->inQueueHead == NULL); |
| 1917 |
assert(prevChanPtr->inQueueTail == NULL); |
| 1918 |
|
| 1919 |
prevChanPtr->inQueueHead = statePtr->inQueueHead; |
| 1920 |
prevChanPtr->inQueueTail = statePtr->inQueueTail; |
| 1921 |
|
| 1922 |
statePtr->inQueueHead = NULL; |
| 1923 |
statePtr->inQueueTail = NULL; |
| 1924 |
} |
| 1925 |
|
| 1926 |
chanPtr = (Channel *)ckalloc(sizeof(Channel)); |
| 1927 |
|
| 1928 |
/* |
| 1929 |
* Save some of the current state into the new structure, reinitialize the |
| 1930 |
* parts which will stay with the transformation. |
| 1931 |
* |
| 1932 |
* Remarks: |
| 1933 |
*/ |
| 1934 |
|
| 1935 |
chanPtr->state = statePtr; |
| 1936 |
chanPtr->instanceData = instanceData; |
| 1937 |
chanPtr->typePtr = typePtr; |
| 1938 |
chanPtr->downChanPtr = prevChanPtr; |
| 1939 |
chanPtr->upChanPtr = NULL; |
| 1940 |
chanPtr->inQueueHead = NULL; |
| 1941 |
chanPtr->inQueueTail = NULL; |
| 1942 |
chanPtr->refCount = 0; |
| 1943 |
|
| 1944 |
/* |
| 1945 |
* Place new block at the head of a possibly existing list of previously |
| 1946 |
* stacked channels. |
| 1947 |
*/ |
| 1948 |
|
| 1949 |
prevChanPtr->upChanPtr = chanPtr; |
| 1950 |
statePtr->topChanPtr = chanPtr; |
| 1951 |
|
| 1952 |
/* |
| 1953 |
* TIP #218, Channel Thread Actions. |
| 1954 |
* |
| 1955 |
* We call the thread actions for the new channel directly. We _cannot_ |
| 1956 |
* use SpliceChannel, because the (thread-)global list of all channels |
| 1957 |
* always contains the _ChannelState_ for a stack of channels, not the |
| 1958 |
* individual channels. And SpliceChannel would not only call the thread |
| 1959 |
* actions, but also add the shared ChannelState to this list a second |
| 1960 |
* time, mangling it. |
| 1961 |
*/ |
| 1962 |
|
| 1963 |
ChanThreadAction(chanPtr, TCL_CHANNEL_THREAD_INSERT); |
| 1964 |
|
| 1965 |
return (Tcl_Channel) chanPtr; |
| 1966 |
} |
| 1967 |
|
| 1968 |
void |
| 1969 |
TclChannelPreserve( |
| 1970 |
Tcl_Channel chan) |
| 1971 |
{ |
| 1972 |
((Channel *)chan)->refCount++; |
| 1973 |
} |
| 1974 |
|
| 1975 |
void |
| 1976 |
TclChannelRelease( |
| 1977 |
Tcl_Channel chan) |
| 1978 |
{ |
| 1979 |
Channel *chanPtr = (Channel *) chan; |
| 1980 |
|
| 1981 |
if (chanPtr->refCount == 0) { |
| 1982 |
Tcl_Panic("Channel released more than preserved"); |
| 1983 |
} |
| 1984 |
if (--chanPtr->refCount) { |
| 1985 |
return; |
| 1986 |
} |
| 1987 |
if (chanPtr->typePtr == NULL) { |
| 1988 |
ckfree(chanPtr); |
| 1989 |
} |
| 1990 |
} |
| 1991 |
|
| 1992 |
static void |
| 1993 |
ChannelFree( |
| 1994 |
Channel *chanPtr) |
| 1995 |
{ |
| 1996 |
if (chanPtr->refCount == 0) { |
| 1997 |
ckfree(chanPtr); |
| 1998 |
return; |
| 1999 |
} |
| 2000 |
chanPtr->typePtr = NULL; |
| 2001 |
} |
| 2002 |
|
| 2003 |
/* |
| 2004 |
*---------------------------------------------------------------------- |
| 2005 |
* |
| 2006 |
* Tcl_UnstackChannel -- |
| 2007 |
* |
| 2008 |
* Unstacks an entry in the hash table for a Tcl_Channel record. This is |
| 2009 |
* the reverse to 'Tcl_StackChannel'. |
| 2010 |
* |
| 2011 |
* Results: |
| 2012 |
* A standard Tcl result. |
| 2013 |
* |
| 2014 |
* Side effects: |
| 2015 |
* If TCL_ERROR is returned, the Posix error code will be set with |
| 2016 |
* Tcl_SetErrno. May leave a message in interp result as well. |
| 2017 |
* |
| 2018 |
*---------------------------------------------------------------------- |
| 2019 |
*/ |
| 2020 |
|
| 2021 |
int |
| 2022 |
Tcl_UnstackChannel( |
| 2023 |
Tcl_Interp *interp, /* The interpreter we are working in */ |
| 2024 |
Tcl_Channel chan) /* The channel to unstack */ |
| 2025 |
{ |
| 2026 |
Channel *chanPtr = (Channel *) chan; |
| 2027 |
ChannelState *statePtr = chanPtr->state; |
| 2028 |
int result = 0; |
| 2029 |
|
| 2030 |
/* |
| 2031 |
* This operation should occur at the top of a channel stack. |
| 2032 |
*/ |
| 2033 |
|
| 2034 |
chanPtr = statePtr->topChanPtr; |
| 2035 |
|
| 2036 |
if (chanPtr->downChanPtr != NULL) { |
| 2037 |
/* |
| 2038 |
* Instead of manipulating the per-thread / per-interp list/hash table |
| 2039 |
* of registered channels we wind down the state of the |
| 2040 |
* transformation, and then restore the state of underlying channel |
| 2041 |
* into the old structure. |
| 2042 |
* |
| 2043 |
* TODO: Figure out how to handle the situation where the chan |
| 2044 |
* operations called below by this unstacking operation cause |
| 2045 |
* another unstacking recursively. In that case the downChanPtr |
| 2046 |
* value we're holding on to will not be the right thing. |
| 2047 |
*/ |
| 2048 |
|
| 2049 |
Channel *downChanPtr = chanPtr->downChanPtr; |
| 2050 |
|
| 2051 |
/* |
| 2052 |
* Flush the buffers. This ensures that any data still in them at this |
| 2053 |
* time _is_ handled by the transformation we are unstacking right |
| 2054 |
* now. Restrict this to writable channels. Take care to hide a |
| 2055 |
* possible bg-copy in progress from Tcl_Flush and the |
| 2056 |
* CheckForChannelErrors inside. |
| 2057 |
*/ |
| 2058 |
|
| 2059 |
if (GotFlag(statePtr, TCL_WRITABLE)) { |
| 2060 |
CopyState *csPtrR = statePtr->csPtrR; |
| 2061 |
CopyState *csPtrW = statePtr->csPtrW; |
| 2062 |
|
| 2063 |
statePtr->csPtrR = NULL; |
| 2064 |
statePtr->csPtrW = NULL; |
| 2065 |
|
| 2066 |
if (Tcl_Flush((Tcl_Channel) chanPtr) != TCL_OK) { |
| 2067 |
statePtr->csPtrR = csPtrR; |
| 2068 |
statePtr->csPtrW = csPtrW; |
| 2069 |
|
| 2070 |
/* |
| 2071 |
* TIP #219, Tcl Channel Reflection API. |
| 2072 |
* Move error messages put by the driver into the chan/ip |
| 2073 |
* bypass area into the regular interpreter result. Fall back |
| 2074 |
* to the regular message if nothing was found in the |
| 2075 |
* bypasses. |
| 2076 |
*/ |
| 2077 |
|
| 2078 |
if (!TclChanCaughtErrorBypass(interp, chan) && interp) { |
| 2079 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 2080 |
"could not flush channel \"%s\"", |
| 2081 |
Tcl_GetChannelName((Tcl_Channel) chanPtr))); |
| 2082 |
} |
| 2083 |
return TCL_ERROR; |
| 2084 |
} |
| 2085 |
|
| 2086 |
statePtr->csPtrR = csPtrR; |
| 2087 |
statePtr->csPtrW = csPtrW; |
| 2088 |
} |
| 2089 |
|
| 2090 |
/* |
| 2091 |
* Anything in the input queue and the push-back buffers of the |
| 2092 |
* transformation going away is transformed data, but not yet read. As |
| 2093 |
* unstacking means that the caller does not want to see transformed |
| 2094 |
* data any more we have to discard these bytes. To avoid writing an |
| 2095 |
* analogue to 'DiscardInputQueued' we move the information in the |
| 2096 |
* push back buffers to the input queue and then call |
| 2097 |
* 'DiscardInputQueued' on that. |
| 2098 |
*/ |
| 2099 |
|
| 2100 |
if (GotFlag(statePtr, TCL_READABLE) && |
| 2101 |
((statePtr->inQueueHead != NULL) || |
| 2102 |
(chanPtr->inQueueHead != NULL))) { |
| 2103 |
if ((statePtr->inQueueHead != NULL) && |
| 2104 |
(chanPtr->inQueueHead != NULL)) { |
| 2105 |
statePtr->inQueueTail->nextPtr = chanPtr->inQueueHead; |
| 2106 |
statePtr->inQueueTail = chanPtr->inQueueTail; |
| 2107 |
statePtr->inQueueHead = statePtr->inQueueTail; |
| 2108 |
} else if (chanPtr->inQueueHead != NULL) { |
| 2109 |
statePtr->inQueueHead = chanPtr->inQueueHead; |
| 2110 |
statePtr->inQueueTail = chanPtr->inQueueTail; |
| 2111 |
} |
| 2112 |
|
| 2113 |
chanPtr->inQueueHead = NULL; |
| 2114 |
chanPtr->inQueueTail = NULL; |
| 2115 |
|
| 2116 |
DiscardInputQueued(statePtr, 0); |
| 2117 |
} |
| 2118 |
|
| 2119 |
/* |
| 2120 |
* TIP #218, Channel Thread Actions. |
| 2121 |
* |
| 2122 |
* We call the thread actions for the new channel directly. We |
| 2123 |
* _cannot_ use CutChannel, because the (thread-)global list of all |
| 2124 |
* channels always contains the _ChannelState_ for a stack of |
| 2125 |
* channels, not the individual channels. And SpliceChannel would not |
| 2126 |
* only call the thread actions, but also remove the shared |
| 2127 |
* ChannelState from this list despite there being more channels for |
| 2128 |
* the state which are still active. |
| 2129 |
*/ |
| 2130 |
|
| 2131 |
ChanThreadAction(chanPtr, TCL_CHANNEL_THREAD_REMOVE); |
| 2132 |
|
| 2133 |
statePtr->topChanPtr = downChanPtr; |
| 2134 |
downChanPtr->upChanPtr = NULL; |
| 2135 |
|
| 2136 |
/* |
| 2137 |
* Leave this link intact for closeproc |
| 2138 |
* chanPtr->downChanPtr = NULL; |
| 2139 |
*/ |
| 2140 |
|
| 2141 |
/* |
| 2142 |
* Close and free the channel driver state. |
| 2143 |
*/ |
| 2144 |
|
| 2145 |
result = ChanClose(chanPtr, interp); |
| 2146 |
ChannelFree(chanPtr); |
| 2147 |
|
| 2148 |
UpdateInterest(statePtr->topChanPtr); |
| 2149 |
|
| 2150 |
if (result != 0) { |
| 2151 |
Tcl_SetErrno(result); |
| 2152 |
|
| 2153 |
/* |
| 2154 |
* TIP #219, Tcl Channel Reflection API. |
| 2155 |
* Move error messages put by the driver into the chan/ip bypass |
| 2156 |
* area into the regular interpreter result. |
| 2157 |
*/ |
| 2158 |
|
| 2159 |
TclChanCaughtErrorBypass(interp, chan); |
| 2160 |
return TCL_ERROR; |
| 2161 |
} |
| 2162 |
} else { |
| 2163 |
/* |
| 2164 |
* This channel does not cover another one. Simply do a close, if |
| 2165 |
* necessary. |
| 2166 |
*/ |
| 2167 |
|
| 2168 |
if (statePtr->refCount <= 0) { |
| 2169 |
if (Tcl_Close(interp, chan) != TCL_OK) { |
| 2170 |
/* |
| 2171 |
* TIP #219, Tcl Channel Reflection API. |
| 2172 |
* "TclChanCaughtErrorBypass" is not required here, it was |
| 2173 |
* done already by "Tcl_Close". |
| 2174 |
*/ |
| 2175 |
|
| 2176 |
return TCL_ERROR; |
| 2177 |
} |
| 2178 |
} |
| 2179 |
|
| 2180 |
/* |
| 2181 |
* TIP #218, Channel Thread Actions. |
| 2182 |
* Not required in this branch, this is done by Tcl_Close. If |
| 2183 |
* Tcl_Close is not called then the ChannelState is still active in |
| 2184 |
* the thread and no action has to be taken either. |
| 2185 |
*/ |
| 2186 |
} |
| 2187 |
|
| 2188 |
return TCL_OK; |
| 2189 |
} |
| 2190 |
|
| 2191 |
/* |
| 2192 |
*---------------------------------------------------------------------- |
| 2193 |
* |
| 2194 |
* Tcl_GetStackedChannel -- |
| 2195 |
* |
| 2196 |
* Determines whether the specified channel is stacked upon another. |
| 2197 |
* |
| 2198 |
* Results: |
| 2199 |
* NULL if the channel is not stacked upon another one, or a reference to |
| 2200 |
* the channel it is stacked upon. This reference can be used in queries, |
| 2201 |
* but modification is not allowed. |
| 2202 |
* |
| 2203 |
* Side effects: |
| 2204 |
* None. |
| 2205 |
* |
| 2206 |
*---------------------------------------------------------------------- |
| 2207 |
*/ |
| 2208 |
|
| 2209 |
Tcl_Channel |
| 2210 |
Tcl_GetStackedChannel( |
| 2211 |
Tcl_Channel chan) |
| 2212 |
{ |
| 2213 |
Channel *chanPtr = (Channel *) chan; |
| 2214 |
/* The actual channel. */ |
| 2215 |
|
| 2216 |
return (Tcl_Channel) chanPtr->downChanPtr; |
| 2217 |
} |
| 2218 |
|
| 2219 |
/* |
| 2220 |
*---------------------------------------------------------------------- |
| 2221 |
* |
| 2222 |
* Tcl_GetTopChannel -- |
| 2223 |
* |
| 2224 |
* Returns the top channel of a channel stack. |
| 2225 |
* |
| 2226 |
* Results: |
| 2227 |
* NULL if the channel is not stacked upon another one, or a reference to |
| 2228 |
* the channel it is stacked upon. This reference can be used in queries, |
| 2229 |
* but modification is not allowed. |
| 2230 |
* |
| 2231 |
* Side effects: |
| 2232 |
* None. |
| 2233 |
* |
| 2234 |
*---------------------------------------------------------------------- |
| 2235 |
*/ |
| 2236 |
|
| 2237 |
Tcl_Channel |
| 2238 |
Tcl_GetTopChannel( |
| 2239 |
Tcl_Channel chan) |
| 2240 |
{ |
| 2241 |
Channel *chanPtr = (Channel *) chan; |
| 2242 |
/* The actual channel. */ |
| 2243 |
|
| 2244 |
return (Tcl_Channel) chanPtr->state->topChanPtr; |
| 2245 |
} |
| 2246 |
|
| 2247 |
/* |
| 2248 |
*---------------------------------------------------------------------- |
| 2249 |
* |
| 2250 |
* Tcl_GetChannelInstanceData -- |
| 2251 |
* |
| 2252 |
* Returns the client data associated with a channel. |
| 2253 |
* |
| 2254 |
* Results: |
| 2255 |
* The client data. |
| 2256 |
* |
| 2257 |
* Side effects: |
| 2258 |
* None. |
| 2259 |
* |
| 2260 |
*---------------------------------------------------------------------- |
| 2261 |
*/ |
| 2262 |
|
| 2263 |
void * |
| 2264 |
Tcl_GetChannelInstanceData( |
| 2265 |
Tcl_Channel chan) /* Channel for which to return client data. */ |
| 2266 |
{ |
| 2267 |
Channel *chanPtr = (Channel *) chan; |
| 2268 |
/* The actual channel. */ |
| 2269 |
|
| 2270 |
return chanPtr->instanceData; |
| 2271 |
} |
| 2272 |
|
| 2273 |
/* |
| 2274 |
*---------------------------------------------------------------------- |
| 2275 |
* |
| 2276 |
* Tcl_GetChannelThread -- |
| 2277 |
* |
| 2278 |
* Given a channel structure, returns the thread managing it. TIP #10 |
| 2279 |
* |
| 2280 |
* Results: |
| 2281 |
* Returns the id of the thread managing the channel. |
| 2282 |
* |
| 2283 |
* Side effects: |
| 2284 |
* None. |
| 2285 |
* |
| 2286 |
*---------------------------------------------------------------------- |
| 2287 |
*/ |
| 2288 |
|
| 2289 |
Tcl_ThreadId |
| 2290 |
Tcl_GetChannelThread( |
| 2291 |
Tcl_Channel chan) /* The channel to return the managing thread |
| 2292 |
* for. */ |
| 2293 |
{ |
| 2294 |
Channel *chanPtr = (Channel *) chan; |
| 2295 |
/* The actual channel. */ |
| 2296 |
|
| 2297 |
return chanPtr->state->managingThread; |
| 2298 |
} |
| 2299 |
|
| 2300 |
/* |
| 2301 |
*---------------------------------------------------------------------- |
| 2302 |
* |
| 2303 |
* Tcl_GetChannelType -- |
| 2304 |
* |
| 2305 |
* Given a channel structure, returns the channel type structure. |
| 2306 |
* |
| 2307 |
* Results: |
| 2308 |
* Returns a pointer to the channel type structure. |
| 2309 |
* |
| 2310 |
* Side effects: |
| 2311 |
* None. |
| 2312 |
* |
| 2313 |
*---------------------------------------------------------------------- |
| 2314 |
*/ |
| 2315 |
|
| 2316 |
const Tcl_ChannelType * |
| 2317 |
Tcl_GetChannelType( |
| 2318 |
Tcl_Channel chan) /* The channel to return type for. */ |
| 2319 |
{ |
| 2320 |
Channel *chanPtr = (Channel *) chan; |
| 2321 |
/* The actual channel. */ |
| 2322 |
|
| 2323 |
return chanPtr->typePtr; |
| 2324 |
} |
| 2325 |
|
| 2326 |
/* |
| 2327 |
*---------------------------------------------------------------------- |
| 2328 |
* |
| 2329 |
* Tcl_GetChannelMode -- |
| 2330 |
* |
| 2331 |
* Computes a mask indicating whether the channel is open for reading and |
| 2332 |
* writing. |
| 2333 |
* |
| 2334 |
* Results: |
| 2335 |
* An OR-ed combination of TCL_READABLE and TCL_WRITABLE. |
| 2336 |
* |
| 2337 |
* Side effects: |
| 2338 |
* None. |
| 2339 |
* |
| 2340 |
*---------------------------------------------------------------------- |
| 2341 |
*/ |
| 2342 |
|
| 2343 |
int |
| 2344 |
Tcl_GetChannelMode( |
| 2345 |
Tcl_Channel chan) /* The channel for which the mode is being |
| 2346 |
* computed. */ |
| 2347 |
{ |
| 2348 |
ChannelState *statePtr = ((Channel *) chan)->state; |
| 2349 |
/* State of actual channel. */ |
| 2350 |
|
| 2351 |
return GotFlag(statePtr, TCL_READABLE|TCL_WRITABLE); |
| 2352 |
} |
| 2353 |
|
| 2354 |
/* |
| 2355 |
*---------------------------------------------------------------------- |
| 2356 |
* |
| 2357 |
* Tcl_GetChannelName -- |
| 2358 |
* |
| 2359 |
* Returns the string identifying the channel name. |
| 2360 |
* |
| 2361 |
* Results: |
| 2362 |
* The string containing the channel name. This memory is owned by the |
| 2363 |
* generic layer and should not be modified by the caller. |
| 2364 |
* |
| 2365 |
* Side effects: |
| 2366 |
* None. |
| 2367 |
* |
| 2368 |
*---------------------------------------------------------------------- |
| 2369 |
*/ |
| 2370 |
|
| 2371 |
const char * |
| 2372 |
Tcl_GetChannelName( |
| 2373 |
Tcl_Channel chan) /* The channel for which to return the name. */ |
| 2374 |
{ |
| 2375 |
ChannelState *statePtr = ((Channel *) chan)->state; |
| 2376 |
/* State of actual channel. */ |
| 2377 |
|
| 2378 |
return statePtr->channelName; |
| 2379 |
} |
| 2380 |
|
| 2381 |
/* |
| 2382 |
*---------------------------------------------------------------------- |
| 2383 |
* |
| 2384 |
* Tcl_GetChannelHandle -- |
| 2385 |
* |
| 2386 |
* Returns an OS handle associated with a channel. |
| 2387 |
* |
| 2388 |
* Results: |
| 2389 |
* Returns TCL_OK and places the handle in handlePtr, or returns |
| 2390 |
* TCL_ERROR on failure. |
| 2391 |
* |
| 2392 |
* Side effects: |
| 2393 |
* None. |
| 2394 |
* |
| 2395 |
*---------------------------------------------------------------------- |
| 2396 |
*/ |
| 2397 |
|
| 2398 |
int |
| 2399 |
Tcl_GetChannelHandle( |
| 2400 |
Tcl_Channel chan, /* The channel to get file from. */ |
| 2401 |
int direction, /* TCL_WRITABLE or TCL_READABLE. */ |
| 2402 |
void **handlePtr) /* Where to store handle */ |
| 2403 |
{ |
| 2404 |
Channel *chanPtr; /* The actual channel. */ |
| 2405 |
void *handle; |
| 2406 |
int result; |
| 2407 |
|
| 2408 |
chanPtr = ((Channel *) chan)->state->bottomChanPtr; |
| 2409 |
if (!chanPtr->typePtr->getHandleProc) { |
| 2410 |
Tcl_SetChannelError(chan, Tcl_ObjPrintf( |
| 2411 |
"channel \"%s\" does not support OS handles", |
| 2412 |
Tcl_GetChannelName(chan))); |
| 2413 |
return TCL_ERROR; |
| 2414 |
} |
| 2415 |
result = chanPtr->typePtr->getHandleProc(chanPtr->instanceData, direction, |
| 2416 |
&handle); |
| 2417 |
if (handlePtr) { |
| 2418 |
*handlePtr = handle; |
| 2419 |
} |
| 2420 |
return result; |
| 2421 |
} |
| 2422 |
|
| 2423 |
/* |
| 2424 |
*--------------------------------------------------------------------------- |
| 2425 |
* |
| 2426 |
* AllocChannelBuffer -- |
| 2427 |
* |
| 2428 |
* A channel buffer has BUFFER_PADDING bytes extra at beginning to hold |
| 2429 |
* any bytes of a native-encoding character that got split by the end of |
| 2430 |
* the previous buffer and need to be moved to the beginning of the next |
| 2431 |
* buffer to make a contiguous string so it can be converted to UTF-8. |
| 2432 |
* |
| 2433 |
* A channel buffer has BUFFER_PADDING bytes extra at the end to hold any |
| 2434 |
* bytes of a native-encoding character (generated from a UTF-8 |
| 2435 |
* character) that overflow past the end of the buffer and need to be |
| 2436 |
* moved to the next buffer. |
| 2437 |
* |
| 2438 |
* Results: |
| 2439 |
* A newly allocated channel buffer. |
| 2440 |
* |
| 2441 |
* Side effects: |
| 2442 |
* None. |
| 2443 |
* |
| 2444 |
*--------------------------------------------------------------------------- |
| 2445 |
*/ |
| 2446 |
|
| 2447 |
static ChannelBuffer * |
| 2448 |
AllocChannelBuffer( |
| 2449 |
int length) /* Desired length of channel buffer. */ |
| 2450 |
{ |
| 2451 |
ChannelBuffer *bufPtr; |
| 2452 |
int n; |
| 2453 |
|
| 2454 |
n = length + CHANNELBUFFER_HEADER_SIZE + BUFFER_PADDING + BUFFER_PADDING; |
| 2455 |
bufPtr = (ChannelBuffer *)ckalloc(n); |
| 2456 |
bufPtr->nextAdded = BUFFER_PADDING; |
| 2457 |
bufPtr->nextRemoved = BUFFER_PADDING; |
| 2458 |
bufPtr->bufLength = length + BUFFER_PADDING; |
| 2459 |
bufPtr->nextPtr = NULL; |
| 2460 |
bufPtr->refCount = 1; |
| 2461 |
return bufPtr; |
| 2462 |
} |
| 2463 |
|
| 2464 |
static void |
| 2465 |
PreserveChannelBuffer( |
| 2466 |
ChannelBuffer *bufPtr) |
| 2467 |
{ |
| 2468 |
if (!bufPtr->refCount) { |
| 2469 |
Tcl_Panic("Reuse of ChannelBuffer! %p", bufPtr); |
| 2470 |
} |
| 2471 |
bufPtr->refCount++; |
| 2472 |
} |
| 2473 |
|
| 2474 |
static void |
| 2475 |
ReleaseChannelBuffer( |
| 2476 |
ChannelBuffer *bufPtr) |
| 2477 |
{ |
| 2478 |
if (--bufPtr->refCount) { |
| 2479 |
return; |
| 2480 |
} |
| 2481 |
ckfree(bufPtr); |
| 2482 |
} |
| 2483 |
|
| 2484 |
static int |
| 2485 |
IsShared( |
| 2486 |
ChannelBuffer *bufPtr) |
| 2487 |
{ |
| 2488 |
return bufPtr->refCount > 1; |
| 2489 |
} |
| 2490 |
|
| 2491 |
/* |
| 2492 |
*---------------------------------------------------------------------- |
| 2493 |
* |
| 2494 |
* RecycleBuffer -- |
| 2495 |
* |
| 2496 |
* Helper function to recycle input and output buffers. Ensures that two |
| 2497 |
* input buffers are saved (one in the input queue and another in the |
| 2498 |
* saveInBufPtr field) and that curOutPtr is set to a buffer. Only if |
| 2499 |
* these conditions are met is the buffer freed to the OS. |
| 2500 |
* |
| 2501 |
* Results: |
| 2502 |
* None. |
| 2503 |
* |
| 2504 |
* Side effects: |
| 2505 |
* May free a buffer to the OS. |
| 2506 |
* |
| 2507 |
*---------------------------------------------------------------------- |
| 2508 |
*/ |
| 2509 |
|
| 2510 |
static void |
| 2511 |
RecycleBuffer( |
| 2512 |
ChannelState *statePtr, /* ChannelState in which to recycle buffers. */ |
| 2513 |
ChannelBuffer *bufPtr, /* The buffer to recycle. */ |
| 2514 |
int mustDiscard) /* If nonzero, free the buffer to the OS, |
| 2515 |
* always. */ |
| 2516 |
{ |
| 2517 |
/* |
| 2518 |
* Do we have to free the buffer to the OS? |
| 2519 |
*/ |
| 2520 |
|
| 2521 |
if (IsShared(bufPtr)) { |
| 2522 |
mustDiscard = 1; |
| 2523 |
} |
| 2524 |
|
| 2525 |
if (mustDiscard) { |
| 2526 |
ReleaseChannelBuffer(bufPtr); |
| 2527 |
return; |
| 2528 |
} |
| 2529 |
|
| 2530 |
/* |
| 2531 |
* Only save buffers which have the requested buffer size for the channel. |
| 2532 |
* This is to honor dynamic changes of the buffe rsize made by the user. |
| 2533 |
*/ |
| 2534 |
|
| 2535 |
if ((bufPtr->bufLength) != statePtr->bufSize + BUFFER_PADDING) { |
| 2536 |
ReleaseChannelBuffer(bufPtr); |
| 2537 |
return; |
| 2538 |
} |
| 2539 |
|
| 2540 |
/* |
| 2541 |
* Only save buffers for the input queue if the channel is readable. |
| 2542 |
*/ |
| 2543 |
|
| 2544 |
if (GotFlag(statePtr, TCL_READABLE)) { |
| 2545 |
if (statePtr->inQueueHead == NULL) { |
| 2546 |
statePtr->inQueueHead = bufPtr; |
| 2547 |
statePtr->inQueueTail = bufPtr; |
| 2548 |
goto keepBuffer; |
| 2549 |
} |
| 2550 |
if (statePtr->saveInBufPtr == NULL) { |
| 2551 |
statePtr->saveInBufPtr = bufPtr; |
| 2552 |
goto keepBuffer; |
| 2553 |
} |
| 2554 |
} |
| 2555 |
|
| 2556 |
/* |
| 2557 |
* Only save buffers for the output queue if the channel is writable. |
| 2558 |
*/ |
| 2559 |
|
| 2560 |
if (GotFlag(statePtr, TCL_WRITABLE)) { |
| 2561 |
if (statePtr->curOutPtr == NULL) { |
| 2562 |
statePtr->curOutPtr = bufPtr; |
| 2563 |
goto keepBuffer; |
| 2564 |
} |
| 2565 |
} |
| 2566 |
|
| 2567 |
/* |
| 2568 |
* If we reached this code we return the buffer to the OS. |
| 2569 |
*/ |
| 2570 |
|
| 2571 |
ReleaseChannelBuffer(bufPtr); |
| 2572 |
return; |
| 2573 |
|
| 2574 |
keepBuffer: |
| 2575 |
bufPtr->nextRemoved = BUFFER_PADDING; |
| 2576 |
bufPtr->nextAdded = BUFFER_PADDING; |
| 2577 |
bufPtr->nextPtr = NULL; |
| 2578 |
} |
| 2579 |
|
| 2580 |
/* |
| 2581 |
*---------------------------------------------------------------------- |
| 2582 |
* |
| 2583 |
* DiscardOutputQueued -- |
| 2584 |
* |
| 2585 |
* Discards all output queued in the output queue of a channel. |
| 2586 |
* |
| 2587 |
* Results: |
| 2588 |
* None. |
| 2589 |
* |
| 2590 |
* Side effects: |
| 2591 |
* Recycles buffers. |
| 2592 |
* |
| 2593 |
*---------------------------------------------------------------------- |
| 2594 |
*/ |
| 2595 |
|
| 2596 |
static void |
| 2597 |
DiscardOutputQueued( |
| 2598 |
ChannelState *statePtr) /* ChannelState for which to discard output. */ |
| 2599 |
{ |
| 2600 |
ChannelBuffer *bufPtr; |
| 2601 |
|
| 2602 |
while (statePtr->outQueueHead != NULL) { |
| 2603 |
bufPtr = statePtr->outQueueHead; |
| 2604 |
statePtr->outQueueHead = bufPtr->nextPtr; |
| 2605 |
RecycleBuffer(statePtr, bufPtr, 0); |
| 2606 |
} |
| 2607 |
statePtr->outQueueHead = NULL; |
| 2608 |
statePtr->outQueueTail = NULL; |
| 2609 |
bufPtr = statePtr->curOutPtr; |
| 2610 |
if (bufPtr && BytesLeft(bufPtr)) { |
| 2611 |
statePtr->curOutPtr = NULL; |
| 2612 |
RecycleBuffer(statePtr, bufPtr, 0); |
| 2613 |
} |
| 2614 |
} |
| 2615 |
|
| 2616 |
/* |
| 2617 |
*---------------------------------------------------------------------- |
| 2618 |
* |
| 2619 |
* CheckForDeadChannel -- |
| 2620 |
* |
| 2621 |
* This function checks is a given channel is Dead (a channel that has |
| 2622 |
* been closed but not yet deallocated.) |
| 2623 |
* |
| 2624 |
* Results: |
| 2625 |
* True (1) if channel is Dead, False (0) if channel is Ok |
| 2626 |
* |
| 2627 |
* Side effects: |
| 2628 |
* None |
| 2629 |
* |
| 2630 |
*---------------------------------------------------------------------- |
| 2631 |
*/ |
| 2632 |
|
| 2633 |
static int |
| 2634 |
CheckForDeadChannel( |
| 2635 |
Tcl_Interp *interp, /* For error reporting (can be NULL) */ |
| 2636 |
ChannelState *statePtr) /* The channel state to check. */ |
| 2637 |
{ |
| 2638 |
if (!GotFlag(statePtr, CHANNEL_DEAD)) { |
| 2639 |
return 0; |
| 2640 |
} |
| 2641 |
|
| 2642 |
Tcl_SetErrno(EINVAL); |
| 2643 |
if (interp) { |
| 2644 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 2645 |
"unable to access channel: invalid channel", -1)); |
| 2646 |
} |
| 2647 |
return 1; |
| 2648 |
} |
| 2649 |
|
| 2650 |
/* |
| 2651 |
*---------------------------------------------------------------------- |
| 2652 |
* |
| 2653 |
* FlushChannel -- |
| 2654 |
* |
| 2655 |
* This function flushes as much of the queued output as is possible now. |
| 2656 |
* If calledFromAsyncFlush is nonzero, it is being called in an event |
| 2657 |
* handler to flush channel output asynchronously. |
| 2658 |
* |
| 2659 |
* Results: |
| 2660 |
* 0 if successful, else the error code that was returned by the channel |
| 2661 |
* type operation. May leave a message in the interp result. |
| 2662 |
* |
| 2663 |
* Side effects: |
| 2664 |
* May produce output on a channel. May block indefinitely if the channel |
| 2665 |
* is synchronous. May schedule an async flush on the channel. May |
| 2666 |
* recycle memory for buffers in the output queue. |
| 2667 |
* |
| 2668 |
*---------------------------------------------------------------------- |
| 2669 |
*/ |
| 2670 |
|
| 2671 |
static int |
| 2672 |
FlushChannel( |
| 2673 |
Tcl_Interp *interp, /* For error reporting during close. */ |
| 2674 |
Channel *chanPtr, /* The channel to flush on. */ |
| 2675 |
int calledFromAsyncFlush) /* If nonzero then we are being called from an |
| 2676 |
* asynchronous flush callback. */ |
| 2677 |
{ |
| 2678 |
ChannelState *statePtr = chanPtr->state; |
| 2679 |
/* State of the channel stack. */ |
| 2680 |
ChannelBuffer *bufPtr; /* Iterates over buffered output queue. */ |
| 2681 |
int written; /* Amount of output data actually written in |
| 2682 |
* current round. */ |
| 2683 |
int errorCode = 0; /* Stores POSIX error codes from channel |
| 2684 |
* driver operations. */ |
| 2685 |
int wroteSome = 0; /* Set to one if any data was written to the |
| 2686 |
* driver. */ |
| 2687 |
|
| 2688 |
/* |
| 2689 |
* Prevent writing on a dead channel -- a channel that has been closed but |
| 2690 |
* not yet deallocated. This can occur if the exit handler for the channel |
| 2691 |
* deallocation runs before all channels are unregistered in all |
| 2692 |
* interpreters. |
| 2693 |
*/ |
| 2694 |
|
| 2695 |
if (CheckForDeadChannel(interp, statePtr)) { |
| 2696 |
return -1; |
| 2697 |
} |
| 2698 |
|
| 2699 |
/* |
| 2700 |
* Should we shift the current output buffer over to the output queue? |
| 2701 |
* First check that there are bytes in it. If so then... |
| 2702 |
* |
| 2703 |
* If the output queue is empty, then yes, trusting the caller called us |
| 2704 |
* only when written bytes ought to be flushed. |
| 2705 |
* |
| 2706 |
* If the current output buffer is full, then yes, so we can meet the |
| 2707 |
* post-condition that on a successful return to caller we've left space |
| 2708 |
* in the current output buffer for more writing (the flush call was to |
| 2709 |
* make new room). |
| 2710 |
* |
| 2711 |
* If the channel is blocking, then yes, so we guarantee that blocking |
| 2712 |
* flushes actually flush all pending data. |
| 2713 |
* |
| 2714 |
* Otherwise, no. Keep the current output buffer where it is so more |
| 2715 |
* can be written to it, possibly filling it, to promote more efficient |
| 2716 |
* buffer usage. |
| 2717 |
*/ |
| 2718 |
|
| 2719 |
bufPtr = statePtr->curOutPtr; |
| 2720 |
if (bufPtr && BytesLeft(bufPtr) && /* Keep empties off queue */ |
| 2721 |
(statePtr->outQueueHead == NULL || IsBufferFull(bufPtr) |
| 2722 |
|| !GotFlag(statePtr, CHANNEL_NONBLOCKING))) { |
| 2723 |
if (statePtr->outQueueHead == NULL) { |
| 2724 |
statePtr->outQueueHead = bufPtr; |
| 2725 |
} else { |
| 2726 |
statePtr->outQueueTail->nextPtr = bufPtr; |
| 2727 |
} |
| 2728 |
statePtr->outQueueTail = bufPtr; |
| 2729 |
statePtr->curOutPtr = NULL; |
| 2730 |
} |
| 2731 |
|
| 2732 |
assert(!IsBufferFull(statePtr->curOutPtr)); |
| 2733 |
|
| 2734 |
/* |
| 2735 |
* If we are not being called from an async flush and an async flush |
| 2736 |
* is active, we just return without producing any output. |
| 2737 |
*/ |
| 2738 |
|
| 2739 |
if (!calledFromAsyncFlush && GotFlag(statePtr, BG_FLUSH_SCHEDULED)) { |
| 2740 |
return 0; |
| 2741 |
} |
| 2742 |
|
| 2743 |
/* |
| 2744 |
* Loop over the queued buffers and attempt to flush as much as possible |
| 2745 |
* of the queued output to the channel. |
| 2746 |
*/ |
| 2747 |
|
| 2748 |
TclChannelPreserve((Tcl_Channel)chanPtr); |
| 2749 |
while (statePtr->outQueueHead) { |
| 2750 |
bufPtr = statePtr->outQueueHead; |
| 2751 |
|
| 2752 |
/* |
| 2753 |
* Produce the output on the channel. |
| 2754 |
*/ |
| 2755 |
|
| 2756 |
PreserveChannelBuffer(bufPtr); |
| 2757 |
written = ChanWrite(chanPtr, RemovePoint(bufPtr), BytesLeft(bufPtr), |
| 2758 |
&errorCode); |
| 2759 |
|
| 2760 |
/* |
| 2761 |
* If the write failed completely attempt to start the asynchronous |
| 2762 |
* flush mechanism and break out of this loop - do not attempt to |
| 2763 |
* write any more output at this time. |
| 2764 |
*/ |
| 2765 |
|
| 2766 |
if (written < 0) { |
| 2767 |
/* |
| 2768 |
* If the last attempt to write was interrupted, simply retry. |
| 2769 |
*/ |
| 2770 |
|
| 2771 |
if (errorCode == EINTR) { |
| 2772 |
errorCode = 0; |
| 2773 |
ReleaseChannelBuffer(bufPtr); |
| 2774 |
continue; |
| 2775 |
} |
| 2776 |
|
| 2777 |
/* |
| 2778 |
* If the channel is non-blocking and we would have blocked, start |
| 2779 |
* a background flushing handler and break out of the loop. |
| 2780 |
*/ |
| 2781 |
|
| 2782 |
if ((errorCode == EWOULDBLOCK) || (errorCode == EAGAIN)) { |
| 2783 |
/* |
| 2784 |
* This used to check for CHANNEL_NONBLOCKING, and panic if |
| 2785 |
* the channel was blocking. However, it appears that setting |
| 2786 |
* stdin to -blocking 0 has some effect on the stdout when |
| 2787 |
* it's a tty channel (dup'ed underneath) |
| 2788 |
*/ |
| 2789 |
|
| 2790 |
if (!GotFlag(statePtr, BG_FLUSH_SCHEDULED) && !TclInExit()) { |
| 2791 |
SetFlag(statePtr, BG_FLUSH_SCHEDULED); |
| 2792 |
UpdateInterest(chanPtr); |
| 2793 |
} |
| 2794 |
errorCode = 0; |
| 2795 |
ReleaseChannelBuffer(bufPtr); |
| 2796 |
break; |
| 2797 |
} |
| 2798 |
|
| 2799 |
/* |
| 2800 |
* Decide whether to report the error upwards or defer it. |
| 2801 |
*/ |
| 2802 |
|
| 2803 |
if (calledFromAsyncFlush) { |
| 2804 |
/* |
| 2805 |
* TIP #219, Tcl Channel Reflection API. |
| 2806 |
* When deferring the error copy a message from the bypass into |
| 2807 |
* the unreported area. Or discard it if the new error is to |
| 2808 |
* be ignored in favor of an earlier deferred error. |
| 2809 |
*/ |
| 2810 |
|
| 2811 |
Tcl_Obj *msg = statePtr->chanMsg; |
| 2812 |
|
| 2813 |
if (statePtr->unreportedError == 0) { |
| 2814 |
statePtr->unreportedError = errorCode; |
| 2815 |
statePtr->unreportedMsg = msg; |
| 2816 |
if (msg != NULL) { |
| 2817 |
Tcl_IncrRefCount(msg); |
| 2818 |
} |
| 2819 |
} else { |
| 2820 |
/* |
| 2821 |
* An old unreported error is kept, and this error thrown |
| 2822 |
* away. |
| 2823 |
*/ |
| 2824 |
|
| 2825 |
statePtr->chanMsg = NULL; |
| 2826 |
if (msg != NULL) { |
| 2827 |
TclDecrRefCount(msg); |
| 2828 |
} |
| 2829 |
} |
| 2830 |
} else { |
| 2831 |
/* |
| 2832 |
* TIP #219, Tcl Channel Reflection API. |
| 2833 |
* Move error messages put by the driver into the chan bypass |
| 2834 |
* area into the regular interpreter result. Fall back to the |
| 2835 |
* regular message if nothing was found in the bypasses. |
| 2836 |
*/ |
| 2837 |
|
| 2838 |
Tcl_SetErrno(errorCode); |
| 2839 |
if (interp != NULL && !TclChanCaughtErrorBypass(interp, |
| 2840 |
(Tcl_Channel) chanPtr)) { |
| 2841 |
Tcl_SetObjResult(interp, |
| 2842 |
Tcl_NewStringObj(Tcl_PosixError(interp), -1)); |
| 2843 |
} |
| 2844 |
|
| 2845 |
/* |
| 2846 |
* An unreportable bypassed message is kept, for the caller of |
| 2847 |
* Tcl_Seek, Tcl_Write, etc. |
| 2848 |
*/ |
| 2849 |
} |
| 2850 |
|
| 2851 |
/* |
| 2852 |
* When we get an error we throw away all the output currently |
| 2853 |
* queued. |
| 2854 |
*/ |
| 2855 |
|
| 2856 |
ReleaseChannelBuffer(bufPtr); |
| 2857 |
DiscardOutputQueued(statePtr); |
| 2858 |
break; |
| 2859 |
} else { |
| 2860 |
/* |
| 2861 |
* TODO: Consider detecting and reacting to short writes on |
| 2862 |
* blocking channels. Ought not happen. See iocmd-24.2. |
| 2863 |
*/ |
| 2864 |
|
| 2865 |
wroteSome = 1; |
| 2866 |
} |
| 2867 |
|
| 2868 |
bufPtr->nextRemoved += written; |
| 2869 |
|
| 2870 |
/* |
| 2871 |
* If this buffer is now empty, recycle it. |
| 2872 |
*/ |
| 2873 |
|
| 2874 |
if (IsBufferEmpty(bufPtr)) { |
| 2875 |
statePtr->outQueueHead = bufPtr->nextPtr; |
| 2876 |
if (statePtr->outQueueHead == NULL) { |
| 2877 |
statePtr->outQueueTail = NULL; |
| 2878 |
} |
| 2879 |
RecycleBuffer(statePtr, bufPtr, 0); |
| 2880 |
} |
| 2881 |
ReleaseChannelBuffer(bufPtr); |
| 2882 |
} /* Closes "while". */ |
| 2883 |
|
| 2884 |
/* |
| 2885 |
* If we wrote some data while flushing in the background, we are done. |
| 2886 |
* We can't finish the background flush until we run out of data and the |
| 2887 |
* channel becomes writable again. This ensures that all of the pending |
| 2888 |
* data has been flushed at the system level. |
| 2889 |
*/ |
| 2890 |
|
| 2891 |
if (GotFlag(statePtr, BG_FLUSH_SCHEDULED)) { |
| 2892 |
if (wroteSome) { |
| 2893 |
goto done; |
| 2894 |
} else if (statePtr->outQueueHead == NULL) { |
| 2895 |
ResetFlag(statePtr, BG_FLUSH_SCHEDULED); |
| 2896 |
ChanWatch(chanPtr, statePtr->interestMask); |
| 2897 |
} else { |
| 2898 |
/* |
| 2899 |
* When we are calledFromAsyncFlush, that means a writable |
| 2900 |
* state on the channel triggered the call, so we should be |
| 2901 |
* able to write something. Either we did write something |
| 2902 |
* and wroteSome should be set, or there was nothing left to |
| 2903 |
* write in this call, and we've completed the BG flush. |
| 2904 |
* These are the two cases above. If we get here, that means |
| 2905 |
* there is some kind failure in the writable event machinery. |
| 2906 |
* |
| 2907 |
* The tls extension indeed suffers from flaws in its channel |
| 2908 |
* event mgmt. See https://core.tcl-lang.org/tcl/info/c31ca233ca. |
| 2909 |
* Until that patch is broadly distributed, disable the |
| 2910 |
* assertion checking here, so that programs using Tcl and |
| 2911 |
* tls can be debugged. |
| 2912 |
|
| 2913 |
assert(!calledFromAsyncFlush); |
| 2914 |
*/ |
| 2915 |
} |
| 2916 |
} |
| 2917 |
|
| 2918 |
/* |
| 2919 |
* If the channel is flagged as closed, delete it when the refCount drops |
| 2920 |
* to zero, the output queue is empty and there is no output in the |
| 2921 |
* current output buffer. |
| 2922 |
*/ |
| 2923 |
|
| 2924 |
if (GotFlag(statePtr, CHANNEL_CLOSED) && (statePtr->refCount <= 0) && |
| 2925 |
(statePtr->outQueueHead == NULL) && |
| 2926 |
((statePtr->curOutPtr == NULL) || |
| 2927 |
IsBufferEmpty(statePtr->curOutPtr))) { |
| 2928 |
errorCode = CloseChannel(interp, chanPtr, errorCode); |
| 2929 |
goto done; |
| 2930 |
} |
| 2931 |
|
| 2932 |
/* |
| 2933 |
* If the write-side of the channel is flagged as closed, delete it when |
| 2934 |
* the output queue is empty and there is no output in the current output |
| 2935 |
* buffer. |
| 2936 |
*/ |
| 2937 |
|
| 2938 |
if (GotFlag(statePtr, CHANNEL_CLOSEDWRITE) && |
| 2939 |
(statePtr->outQueueHead == NULL) && |
| 2940 |
((statePtr->curOutPtr == NULL) || |
| 2941 |
IsBufferEmpty(statePtr->curOutPtr))) { |
| 2942 |
errorCode = CloseChannelPart(interp, chanPtr, errorCode, |
| 2943 |
TCL_CLOSE_WRITE); |
| 2944 |
goto done; |
| 2945 |
} |
| 2946 |
|
| 2947 |
done: |
| 2948 |
TclChannelRelease((Tcl_Channel)chanPtr); |
| 2949 |
return errorCode; |
| 2950 |
} |
| 2951 |
|
| 2952 |
static void |
| 2953 |
FreeChannelState( |
| 2954 |
char *blockPtr) /* Channel state to free. */ |
| 2955 |
{ |
| 2956 |
ChannelState *statePtr = (ChannelState *)blockPtr; |
| 2957 |
/* |
| 2958 |
* Even after close some members can be filled again (in events etc). |
| 2959 |
* Test in bug [79474c588] illustrates one leak (on remaining chanMsg). |
| 2960 |
* Possible other fields need freeing on some constellations. |
| 2961 |
*/ |
| 2962 |
|
| 2963 |
DiscardInputQueued(statePtr, 1); |
| 2964 |
if (statePtr->curOutPtr != NULL) { |
| 2965 |
ReleaseChannelBuffer(statePtr->curOutPtr); |
| 2966 |
} |
| 2967 |
DiscardOutputQueued(statePtr); |
| 2968 |
|
| 2969 |
DeleteTimerHandler(statePtr); |
| 2970 |
|
| 2971 |
if (statePtr->chanMsg) { |
| 2972 |
Tcl_DecrRefCount(statePtr->chanMsg); |
| 2973 |
} |
| 2974 |
if (statePtr->unreportedMsg) { |
| 2975 |
Tcl_DecrRefCount(statePtr->unreportedMsg); |
| 2976 |
} |
| 2977 |
ckfree(statePtr); |
| 2978 |
} |
| 2979 |
|
| 2980 |
/* |
| 2981 |
*---------------------------------------------------------------------- |
| 2982 |
* |
| 2983 |
* CloseChannel -- |
| 2984 |
* |
| 2985 |
* Utility procedure to close a channel and free associated resources. |
| 2986 |
* |
| 2987 |
* If the channel was stacked, then the it will copy the necessary |
| 2988 |
* elements of the NEXT channel into the TOP channel, in essence |
| 2989 |
* unstacking the channel. The NEXT channel will then be freed. |
| 2990 |
* |
| 2991 |
* If the channel was not stacked, then we will free all the bits for the |
| 2992 |
* TOP channel, including the data structure itself. |
| 2993 |
* |
| 2994 |
* Results: |
| 2995 |
* Error code from an unreported error or the driver close operation. |
| 2996 |
* |
| 2997 |
* Side effects: |
| 2998 |
* May close the actual channel, may free memory, may change the value of |
| 2999 |
* errno. |
| 3000 |
* |
| 3001 |
*---------------------------------------------------------------------- |
| 3002 |
*/ |
| 3003 |
|
| 3004 |
static int |
| 3005 |
CloseChannel( |
| 3006 |
Tcl_Interp *interp, /* For error reporting. */ |
| 3007 |
Channel *chanPtr, /* The channel to close. */ |
| 3008 |
int errorCode) /* Status of operation so far. */ |
| 3009 |
{ |
| 3010 |
int result = 0; /* Of calling driver close operation. */ |
| 3011 |
ChannelState *statePtr; /* State of the channel stack. */ |
| 3012 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 3013 |
|
| 3014 |
if (chanPtr == NULL) { |
| 3015 |
return result; |
| 3016 |
} |
| 3017 |
statePtr = chanPtr->state; |
| 3018 |
|
| 3019 |
/* |
| 3020 |
* No more input can be consumed so discard any leftover input. |
| 3021 |
*/ |
| 3022 |
|
| 3023 |
DiscardInputQueued(statePtr, 1); |
| 3024 |
|
| 3025 |
/* |
| 3026 |
* Discard a leftover buffer in the current output buffer field. |
| 3027 |
*/ |
| 3028 |
|
| 3029 |
if (statePtr->curOutPtr != NULL) { |
| 3030 |
ReleaseChannelBuffer(statePtr->curOutPtr); |
| 3031 |
statePtr->curOutPtr = NULL; |
| 3032 |
} |
| 3033 |
|
| 3034 |
/* |
| 3035 |
* The caller guarantees that there are no more buffers queued for output. |
| 3036 |
*/ |
| 3037 |
|
| 3038 |
if (statePtr->outQueueHead != NULL) { |
| 3039 |
Tcl_Panic("TclFlush, closed channel: queued output left"); |
| 3040 |
} |
| 3041 |
|
| 3042 |
/* |
| 3043 |
* If the EOF character is set in the channel, append that to the output |
| 3044 |
* device. |
| 3045 |
*/ |
| 3046 |
|
| 3047 |
if ((statePtr->outEofChar != 0) && GotFlag(statePtr, TCL_WRITABLE)) { |
| 3048 |
int dummy; |
| 3049 |
char c = (char) statePtr->outEofChar; |
| 3050 |
|
| 3051 |
(void) ChanWrite(chanPtr, &c, 1, &dummy); |
| 3052 |
} |
| 3053 |
|
| 3054 |
/* |
| 3055 |
* TIP #219, Tcl Channel Reflection API. |
| 3056 |
* Move a leftover error message in the channel bypass into the |
| 3057 |
* interpreter bypass. Just clear it if there is no interpreter. |
| 3058 |
*/ |
| 3059 |
|
| 3060 |
if (statePtr->chanMsg != NULL) { |
| 3061 |
if (interp != NULL) { |
| 3062 |
Tcl_SetChannelErrorInterp(interp, statePtr->chanMsg); |
| 3063 |
} |
| 3064 |
TclDecrRefCount(statePtr->chanMsg); |
| 3065 |
statePtr->chanMsg = NULL; |
| 3066 |
} |
| 3067 |
|
| 3068 |
/* |
| 3069 |
* Remove this channel from of the list of all channels. |
| 3070 |
*/ |
| 3071 |
|
| 3072 |
CutChannel((Tcl_Channel) chanPtr); |
| 3073 |
|
| 3074 |
/* |
| 3075 |
* Close and free the channel driver state. |
| 3076 |
* This may leave a TIP #219 error message in the interp. |
| 3077 |
*/ |
| 3078 |
|
| 3079 |
result = ChanClose(chanPtr, interp); |
| 3080 |
|
| 3081 |
/* |
| 3082 |
* Some resources can be cleared only if the bottom channel in a stack is |
| 3083 |
* closed. All the other channels in the stack are not allowed to remove. |
| 3084 |
*/ |
| 3085 |
|
| 3086 |
if (chanPtr == statePtr->bottomChanPtr) { |
| 3087 |
if (statePtr->channelName != NULL) { |
| 3088 |
ckfree(statePtr->channelName); |
| 3089 |
statePtr->channelName = NULL; |
| 3090 |
} |
| 3091 |
|
| 3092 |
Tcl_FreeEncoding(statePtr->encoding); |
| 3093 |
} |
| 3094 |
|
| 3095 |
/* |
| 3096 |
* If we are being called synchronously, report either any latent error on |
| 3097 |
* the channel or the current error. |
| 3098 |
*/ |
| 3099 |
|
| 3100 |
if (statePtr->unreportedError != 0) { |
| 3101 |
errorCode = statePtr->unreportedError; |
| 3102 |
|
| 3103 |
/* |
| 3104 |
* TIP #219, Tcl Channel Reflection API. |
| 3105 |
* Move an error message found in the unreported area into the regular |
| 3106 |
* bypass (interp). This kills any message in the channel bypass area. |
| 3107 |
*/ |
| 3108 |
|
| 3109 |
if (statePtr->chanMsg != NULL) { |
| 3110 |
TclDecrRefCount(statePtr->chanMsg); |
| 3111 |
statePtr->chanMsg = NULL; |
| 3112 |
} |
| 3113 |
if (interp) { |
| 3114 |
Tcl_SetChannelErrorInterp(interp, statePtr->unreportedMsg); |
| 3115 |
} |
| 3116 |
} |
| 3117 |
if (errorCode == 0) { |
| 3118 |
errorCode = result; |
| 3119 |
if (errorCode != 0) { |
| 3120 |
Tcl_SetErrno(errorCode); |
| 3121 |
} |
| 3122 |
} |
| 3123 |
|
| 3124 |
/* |
| 3125 |
* Cancel any outstanding timer. |
| 3126 |
*/ |
| 3127 |
|
| 3128 |
DeleteTimerHandler(statePtr); |
| 3129 |
|
| 3130 |
/* |
| 3131 |
* Mark the channel as deleted by clearing the type structure. |
| 3132 |
*/ |
| 3133 |
|
| 3134 |
if (chanPtr->downChanPtr != NULL) { |
| 3135 |
Channel *downChanPtr = chanPtr->downChanPtr; |
| 3136 |
|
| 3137 |
statePtr->nextCSPtr = tsdPtr->firstCSPtr; |
| 3138 |
tsdPtr->firstCSPtr = statePtr; |
| 3139 |
|
| 3140 |
statePtr->topChanPtr = downChanPtr; |
| 3141 |
downChanPtr->upChanPtr = NULL; |
| 3142 |
|
| 3143 |
ChannelFree(chanPtr); |
| 3144 |
|
| 3145 |
return Tcl_Close(interp, (Tcl_Channel) downChanPtr); |
| 3146 |
} |
| 3147 |
|
| 3148 |
/* |
| 3149 |
* There is only the TOP Channel, so we free the remaining pointers we |
| 3150 |
* have and then ourselves. Since this is the last of the channels in the |
| 3151 |
* stack, make sure to free the ChannelState structure associated with it. |
| 3152 |
*/ |
| 3153 |
|
| 3154 |
ChannelFree(chanPtr); |
| 3155 |
|
| 3156 |
Tcl_EventuallyFree(statePtr, FreeChannelState); |
| 3157 |
|
| 3158 |
return errorCode; |
| 3159 |
} |
| 3160 |
|
| 3161 |
/* |
| 3162 |
*---------------------------------------------------------------------- |
| 3163 |
* |
| 3164 |
* Tcl_CutChannel -- |
| 3165 |
* CutChannel -- |
| 3166 |
* |
| 3167 |
* Removes a channel from the (thread-)global list of all channels (in |
| 3168 |
* that thread). This is actually the statePtr for the stack of channel. |
| 3169 |
* |
| 3170 |
* Results: |
| 3171 |
* Nothing. |
| 3172 |
* |
| 3173 |
* Side effects: |
| 3174 |
* Resets the field 'nextCSPtr' of the specified channel state to NULL. |
| 3175 |
* |
| 3176 |
* NOTE: |
| 3177 |
* The channel to cut out of the list must not be referenced in any |
| 3178 |
* interpreter. This is something this procedure cannot check (despite |
| 3179 |
* the refcount) because the caller usually wants fiddle with the channel |
| 3180 |
* (like transferring it to a different thread) and thus keeps the |
| 3181 |
* refcount artificially high to prevent its destruction. |
| 3182 |
* |
| 3183 |
*---------------------------------------------------------------------- |
| 3184 |
*/ |
| 3185 |
|
| 3186 |
static void |
| 3187 |
CutChannel( |
| 3188 |
Tcl_Channel chan) /* The channel being removed. Must not be |
| 3189 |
* referenced in any interpreter. */ |
| 3190 |
{ |
| 3191 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 3192 |
ChannelState *prevCSPtr; /* Preceding channel state in list of all |
| 3193 |
* states - used to splice a channel out of |
| 3194 |
* the list on close. */ |
| 3195 |
ChannelState *statePtr = ((Channel *) chan)->state; |
| 3196 |
/* State of the channel stack. */ |
| 3197 |
|
| 3198 |
/* |
| 3199 |
* Remove this channel from of the list of all channels (in the current |
| 3200 |
* thread). |
| 3201 |
*/ |
| 3202 |
|
| 3203 |
if (tsdPtr->firstCSPtr && (statePtr == tsdPtr->firstCSPtr)) { |
| 3204 |
tsdPtr->firstCSPtr = statePtr->nextCSPtr; |
| 3205 |
} else { |
| 3206 |
for (prevCSPtr = tsdPtr->firstCSPtr; |
| 3207 |
prevCSPtr && (prevCSPtr->nextCSPtr != statePtr); |
| 3208 |
prevCSPtr = prevCSPtr->nextCSPtr) { |
| 3209 |
/* Empty loop body. */ |
| 3210 |
} |
| 3211 |
if (prevCSPtr == NULL) { |
| 3212 |
Tcl_Panic("FlushChannel: damaged channel list"); |
| 3213 |
} |
| 3214 |
prevCSPtr->nextCSPtr = statePtr->nextCSPtr; |
| 3215 |
} |
| 3216 |
|
| 3217 |
statePtr->nextCSPtr = NULL; |
| 3218 |
|
| 3219 |
/* |
| 3220 |
* TIP #218, Channel Thread Actions |
| 3221 |
*/ |
| 3222 |
|
| 3223 |
ChanThreadAction((Channel *) chan, TCL_CHANNEL_THREAD_REMOVE); |
| 3224 |
|
| 3225 |
/* Channel is not managed by any thread */ |
| 3226 |
statePtr->managingThread = NULL; |
| 3227 |
} |
| 3228 |
|
| 3229 |
void |
| 3230 |
Tcl_CutChannel( |
| 3231 |
Tcl_Channel chan) /* The channel being added. Must not be |
| 3232 |
* referenced in any interpreter. */ |
| 3233 |
{ |
| 3234 |
Channel *chanPtr = ((Channel *) chan)->state->bottomChanPtr; |
| 3235 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 3236 |
ChannelState *prevCSPtr; /* Preceding channel state in list of all |
| 3237 |
* states - used to splice a channel out of |
| 3238 |
* the list on close. */ |
| 3239 |
ChannelState *statePtr = chanPtr->state; |
| 3240 |
/* State of the channel stack. */ |
| 3241 |
|
| 3242 |
/* |
| 3243 |
* Remove this channel from of the list of all channels (in the current |
| 3244 |
* thread). |
| 3245 |
*/ |
| 3246 |
|
| 3247 |
if (tsdPtr->firstCSPtr && (statePtr == tsdPtr->firstCSPtr)) { |
| 3248 |
tsdPtr->firstCSPtr = statePtr->nextCSPtr; |
| 3249 |
} else { |
| 3250 |
for (prevCSPtr = tsdPtr->firstCSPtr; |
| 3251 |
prevCSPtr && (prevCSPtr->nextCSPtr != statePtr); |
| 3252 |
prevCSPtr = prevCSPtr->nextCSPtr) { |
| 3253 |
/* Empty loop body. */ |
| 3254 |
} |
| 3255 |
if (prevCSPtr == NULL) { |
| 3256 |
Tcl_Panic("FlushChannel: damaged channel list"); |
| 3257 |
} |
| 3258 |
prevCSPtr->nextCSPtr = statePtr->nextCSPtr; |
| 3259 |
} |
| 3260 |
|
| 3261 |
statePtr->nextCSPtr = NULL; |
| 3262 |
|
| 3263 |
/* |
| 3264 |
* TIP #218, Channel Thread Actions |
| 3265 |
* For all transformations and the base channel. |
| 3266 |
*/ |
| 3267 |
|
| 3268 |
for (; chanPtr != NULL ; chanPtr = chanPtr->upChanPtr) { |
| 3269 |
ChanThreadAction(chanPtr, TCL_CHANNEL_THREAD_REMOVE); |
| 3270 |
} |
| 3271 |
|
| 3272 |
/* Channel is not managed by any thread */ |
| 3273 |
statePtr->managingThread = NULL; |
| 3274 |
} |
| 3275 |
|
| 3276 |
/* |
| 3277 |
*---------------------------------------------------------------------- |
| 3278 |
* |
| 3279 |
* Tcl_SpliceChannel -- |
| 3280 |
* SpliceChannel -- |
| 3281 |
* |
| 3282 |
* Adds a channel to the (thread-)global list of all channels (in that |
| 3283 |
* thread). Expects that the field 'nextChanPtr' in the channel is set to |
| 3284 |
* NULL. |
| 3285 |
* |
| 3286 |
* Results: |
| 3287 |
* Nothing. |
| 3288 |
* |
| 3289 |
* Side effects: |
| 3290 |
* Nothing. |
| 3291 |
* |
| 3292 |
* NOTE: |
| 3293 |
* The channel to splice into the list must not be referenced in any |
| 3294 |
* interpreter. This is something this procedure cannot check (despite |
| 3295 |
* the refcount) because the caller usually wants fiddle with the channel |
| 3296 |
* (like transferring it to a different thread) and thus keeps the |
| 3297 |
* refcount artificially high to prevent its destruction. |
| 3298 |
* |
| 3299 |
*---------------------------------------------------------------------- |
| 3300 |
*/ |
| 3301 |
|
| 3302 |
static void |
| 3303 |
SpliceChannel( |
| 3304 |
Tcl_Channel chan) /* The channel being added. Must not be |
| 3305 |
* referenced in any interpreter. */ |
| 3306 |
{ |
| 3307 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 3308 |
ChannelState *statePtr = ((Channel *) chan)->state; |
| 3309 |
|
| 3310 |
if (statePtr->nextCSPtr != NULL) { |
| 3311 |
Tcl_Panic("SpliceChannel: trying to add channel used in different list"); |
| 3312 |
} |
| 3313 |
|
| 3314 |
statePtr->nextCSPtr = tsdPtr->firstCSPtr; |
| 3315 |
tsdPtr->firstCSPtr = statePtr; |
| 3316 |
|
| 3317 |
/* |
| 3318 |
* TIP #10. Mark the current thread as the new one managing this channel. |
| 3319 |
* Note: 'Tcl_GetCurrentThread' returns sensible values even for |
| 3320 |
* a non-threaded core. |
| 3321 |
*/ |
| 3322 |
|
| 3323 |
statePtr->managingThread = Tcl_GetCurrentThread(); |
| 3324 |
|
| 3325 |
/* |
| 3326 |
* TIP #218, Channel Thread Actions |
| 3327 |
*/ |
| 3328 |
|
| 3329 |
ChanThreadAction((Channel *) chan, TCL_CHANNEL_THREAD_INSERT); |
| 3330 |
} |
| 3331 |
|
| 3332 |
void |
| 3333 |
Tcl_SpliceChannel( |
| 3334 |
Tcl_Channel chan) /* The channel being added. Must not be |
| 3335 |
* referenced in any interpreter. */ |
| 3336 |
{ |
| 3337 |
Channel *chanPtr = ((Channel *) chan)->state->bottomChanPtr; |
| 3338 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 3339 |
ChannelState *statePtr = chanPtr->state; |
| 3340 |
|
| 3341 |
if (statePtr->nextCSPtr != NULL) { |
| 3342 |
Tcl_Panic("SpliceChannel: trying to add channel used in different list"); |
| 3343 |
} |
| 3344 |
|
| 3345 |
statePtr->nextCSPtr = tsdPtr->firstCSPtr; |
| 3346 |
tsdPtr->firstCSPtr = statePtr; |
| 3347 |
|
| 3348 |
/* |
| 3349 |
* TIP #10. Mark the current thread as the new one managing this channel. |
| 3350 |
* Note: 'Tcl_GetCurrentThread' returns sensible values even for |
| 3351 |
* a non-threaded core. |
| 3352 |
*/ |
| 3353 |
|
| 3354 |
statePtr->managingThread = Tcl_GetCurrentThread(); |
| 3355 |
|
| 3356 |
/* |
| 3357 |
* TIP #218, Channel Thread Actions |
| 3358 |
* For all transformations and the base channel. |
| 3359 |
*/ |
| 3360 |
|
| 3361 |
for (; chanPtr != NULL ; chanPtr = chanPtr->upChanPtr) { |
| 3362 |
ChanThreadAction(chanPtr, TCL_CHANNEL_THREAD_INSERT); |
| 3363 |
} |
| 3364 |
} |
| 3365 |
|
| 3366 |
/* |
| 3367 |
*---------------------------------------------------------------------- |
| 3368 |
* |
| 3369 |
* Tcl_Close -- |
| 3370 |
* |
| 3371 |
* Closes a channel. |
| 3372 |
* |
| 3373 |
* Results: |
| 3374 |
* A standard Tcl result. |
| 3375 |
* |
| 3376 |
* Side effects: |
| 3377 |
* Closes the channel if this is the last reference. |
| 3378 |
* |
| 3379 |
* NOTE: |
| 3380 |
* Tcl_Close removes the channel as far as the user is concerned. |
| 3381 |
* However, it may continue to exist for a while longer if it has a |
| 3382 |
* background flush scheduled. The device itself is eventually closed and |
| 3383 |
* the channel record removed, in CloseChannel, above. |
| 3384 |
* |
| 3385 |
*---------------------------------------------------------------------- |
| 3386 |
*/ |
| 3387 |
|
| 3388 |
int |
| 3389 |
Tcl_Close( |
| 3390 |
Tcl_Interp *interp, /* Interpreter for errors. */ |
| 3391 |
Tcl_Channel chan) /* The channel being closed. Must not be |
| 3392 |
* referenced in any interpreter. */ |
| 3393 |
{ |
| 3394 |
CloseCallback *cbPtr; /* Iterate over close callbacks for this |
| 3395 |
* channel. */ |
| 3396 |
Channel *chanPtr; /* The real IO channel. */ |
| 3397 |
ChannelState *statePtr; /* State of real IO channel. */ |
| 3398 |
int result = 0; /* Of calling FlushChannel. */ |
| 3399 |
int flushcode; |
| 3400 |
int stickyError; |
| 3401 |
|
| 3402 |
if (chan == NULL) { |
| 3403 |
return TCL_OK; |
| 3404 |
} |
| 3405 |
|
| 3406 |
/* |
| 3407 |
* Perform special handling for standard channels being closed. If the |
| 3408 |
* refCount is now 1 it means that the last reference to the standard |
| 3409 |
* channel is being explicitly closed, so bump the refCount down |
| 3410 |
* artificially to 0. This will ensure that the channel is actually |
| 3411 |
* closed, below. Also set the static pointer to NULL for the channel. |
| 3412 |
*/ |
| 3413 |
|
| 3414 |
CheckForStdChannelsBeingClosed(chan); |
| 3415 |
|
| 3416 |
/* |
| 3417 |
* This operation should occur at the top of a channel stack. |
| 3418 |
*/ |
| 3419 |
|
| 3420 |
chanPtr = (Channel *) chan; |
| 3421 |
statePtr = chanPtr->state; |
| 3422 |
chanPtr = statePtr->topChanPtr; |
| 3423 |
|
| 3424 |
if (statePtr->refCount > 0) { |
| 3425 |
Tcl_Panic("called Tcl_Close on channel with refCount > 0"); |
| 3426 |
} |
| 3427 |
|
| 3428 |
if (GotFlag(statePtr, CHANNEL_INCLOSE)) { |
| 3429 |
if (interp) { |
| 3430 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 3431 |
"illegal recursive call to close through close-handler" |
| 3432 |
" of channel", -1)); |
| 3433 |
} |
| 3434 |
return TCL_ERROR; |
| 3435 |
} |
| 3436 |
SetFlag(statePtr, CHANNEL_INCLOSE); |
| 3437 |
|
| 3438 |
/* |
| 3439 |
* When the channel has an escape sequence driven encoding such as |
| 3440 |
* iso2022, the terminated escape sequence must write to the buffer. |
| 3441 |
*/ |
| 3442 |
|
| 3443 |
stickyError = 0; |
| 3444 |
|
| 3445 |
if (GotFlag(statePtr, TCL_WRITABLE) && (statePtr->encoding != NULL) |
| 3446 |
&& !(statePtr->outputEncodingFlags & TCL_ENCODING_START)) { |
| 3447 |
int code = CheckChannelErrors(statePtr, TCL_WRITABLE); |
| 3448 |
|
| 3449 |
if (code == 0) { |
| 3450 |
statePtr->outputEncodingFlags |= TCL_ENCODING_END; |
| 3451 |
code = WriteChars(chanPtr, "", 0); |
| 3452 |
statePtr->outputEncodingFlags &= ~TCL_ENCODING_END; |
| 3453 |
statePtr->outputEncodingFlags |= TCL_ENCODING_START; |
| 3454 |
} |
| 3455 |
if (code < 0) { |
| 3456 |
stickyError = Tcl_GetErrno(); |
| 3457 |
} |
| 3458 |
|
| 3459 |
/* |
| 3460 |
* TIP #219, Tcl Channel Reflection API. |
| 3461 |
* Move an error message found in the channel bypass into the |
| 3462 |
* interpreter bypass. Just clear it if there is no interpreter. |
| 3463 |
*/ |
| 3464 |
|
| 3465 |
if (statePtr->chanMsg != NULL) { |
| 3466 |
if (interp != NULL) { |
| 3467 |
Tcl_SetChannelErrorInterp(interp, statePtr->chanMsg); |
| 3468 |
} |
| 3469 |
TclDecrRefCount(statePtr->chanMsg); |
| 3470 |
statePtr->chanMsg = NULL; |
| 3471 |
} |
| 3472 |
} |
| 3473 |
|
| 3474 |
Tcl_ClearChannelHandlers(chan); |
| 3475 |
|
| 3476 |
/* |
| 3477 |
* Invoke the registered close callbacks and delete their records. |
| 3478 |
*/ |
| 3479 |
|
| 3480 |
while (statePtr->closeCbPtr != NULL) { |
| 3481 |
cbPtr = statePtr->closeCbPtr; |
| 3482 |
statePtr->closeCbPtr = cbPtr->nextPtr; |
| 3483 |
cbPtr->proc(cbPtr->clientData); |
| 3484 |
ckfree(cbPtr); |
| 3485 |
} |
| 3486 |
|
| 3487 |
ResetFlag(statePtr, CHANNEL_INCLOSE); |
| 3488 |
|
| 3489 |
/* |
| 3490 |
* If this channel supports it, close the read side, since we don't need |
| 3491 |
* it anymore and this will help avoid deadlocks on some channel types. |
| 3492 |
*/ |
| 3493 |
|
| 3494 |
if ((chanPtr->typePtr->closeProc == TCL_CLOSE2PROC) || (chanPtr->typePtr->closeProc == NULL)) { |
| 3495 |
/* If this half-close gives a EINVAL or ENOTCONN, just continue the full close */ |
| 3496 |
result = chanPtr->typePtr->close2Proc(chanPtr->instanceData, interp, TCL_CLOSE_READ); |
| 3497 |
if ((result == EINVAL) || result == ENOTCONN) { |
| 3498 |
result = 0; |
| 3499 |
} |
| 3500 |
} |
| 3501 |
|
| 3502 |
/* |
| 3503 |
* The call to FlushChannel will flush any queued output and invoke the |
| 3504 |
* close function of the channel driver, or it will set up the channel to |
| 3505 |
* be flushed and closed asynchronously. |
| 3506 |
*/ |
| 3507 |
|
| 3508 |
SetFlag(statePtr, CHANNEL_CLOSED); |
| 3509 |
|
| 3510 |
flushcode = FlushChannel(interp, chanPtr, 0); |
| 3511 |
|
| 3512 |
/* |
| 3513 |
* TIP #219. |
| 3514 |
* Capture error messages put by the driver into the bypass area and put |
| 3515 |
* them into the regular interpreter result. |
| 3516 |
* |
| 3517 |
* Notes: Due to the assertion of CHANNEL_CLOSED in the flags |
| 3518 |
* FlushChannel() has called CloseChannel() and thus freed all the channel |
| 3519 |
* structures. We must not try to access "chan" anymore, hence the NULL |
| 3520 |
* argument in the call below. The only place which may still contain a |
| 3521 |
* message is the interpreter itself, and "CloseChannel" made sure to lift |
| 3522 |
* any channel message it generated into it. |
| 3523 |
*/ |
| 3524 |
|
| 3525 |
if (TclChanCaughtErrorBypass(interp, NULL)) { |
| 3526 |
result = EINVAL; |
| 3527 |
} |
| 3528 |
|
| 3529 |
if (stickyError != 0) { |
| 3530 |
Tcl_SetErrno(stickyError); |
| 3531 |
if (interp != NULL) { |
| 3532 |
Tcl_SetObjResult(interp, |
| 3533 |
Tcl_NewStringObj(Tcl_PosixError(interp), -1)); |
| 3534 |
} |
| 3535 |
return TCL_ERROR; |
| 3536 |
} |
| 3537 |
|
| 3538 |
/* |
| 3539 |
* Bug 97069ea11a: set error message if a flush code is set and no error |
| 3540 |
* message set up to now. |
| 3541 |
*/ |
| 3542 |
|
| 3543 |
if (flushcode != 0) { |
| 3544 |
/* flushcode has precedence, if available */ |
| 3545 |
result = flushcode; |
| 3546 |
} |
| 3547 |
if ((result != 0) && (result != TCL_ERROR) && (interp != NULL) |
| 3548 |
&& 0 == Tcl_GetCharLength(Tcl_GetObjResult(interp))) { |
| 3549 |
Tcl_SetErrno(result); |
| 3550 |
Tcl_SetObjResult(interp, |
| 3551 |
Tcl_NewStringObj(Tcl_PosixError(interp), -1)); |
| 3552 |
} |
| 3553 |
if (result != 0) { |
| 3554 |
return TCL_ERROR; |
| 3555 |
} |
| 3556 |
return TCL_OK; |
| 3557 |
} |
| 3558 |
|
| 3559 |
/* |
| 3560 |
*---------------------------------------------------------------------- |
| 3561 |
* |
| 3562 |
* Tcl_CloseEx -- |
| 3563 |
* |
| 3564 |
* Closes one side of a channel, read or write. |
| 3565 |
* |
| 3566 |
* Results: |
| 3567 |
* A standard Tcl result. |
| 3568 |
* |
| 3569 |
* Side effects: |
| 3570 |
* Closes one direction of the channel. |
| 3571 |
* |
| 3572 |
* NOTE: |
| 3573 |
* Tcl_CloseEx closes the specified direction of the channel as far as |
| 3574 |
* the user is concerned. The channel keeps existing however. You cannot |
| 3575 |
* call this function to close the last possible direction of the |
| 3576 |
* channel. Use Tcl_Close for that. |
| 3577 |
* |
| 3578 |
*---------------------------------------------------------------------- |
| 3579 |
*/ |
| 3580 |
|
| 3581 |
int |
| 3582 |
Tcl_CloseEx( |
| 3583 |
Tcl_Interp *interp, /* Interpreter for errors. */ |
| 3584 |
Tcl_Channel chan, /* The channel being closed. May still be used |
| 3585 |
* by some interpreter. */ |
| 3586 |
int flags) /* Flags telling us which side to close. */ |
| 3587 |
{ |
| 3588 |
Channel *chanPtr; /* The real IO channel. */ |
| 3589 |
ChannelState *statePtr; /* State of real IO channel. */ |
| 3590 |
|
| 3591 |
if (chan == NULL) { |
| 3592 |
return TCL_OK; |
| 3593 |
} |
| 3594 |
|
| 3595 |
chanPtr = (Channel *) chan; |
| 3596 |
statePtr = chanPtr->state; |
| 3597 |
|
| 3598 |
if ((flags & (TCL_READABLE | TCL_WRITABLE)) == 0) { |
| 3599 |
return Tcl_Close(interp, chan); |
| 3600 |
} |
| 3601 |
if ((flags & (TCL_READABLE | TCL_WRITABLE)) == (TCL_READABLE | TCL_WRITABLE)) { |
| 3602 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 3603 |
"double-close of channels not supported by %ss", |
| 3604 |
chanPtr->typePtr->typeName)); |
| 3605 |
return TCL_ERROR; |
| 3606 |
} |
| 3607 |
|
| 3608 |
/* |
| 3609 |
* Does the channel support half-close anyway? Error if not. |
| 3610 |
*/ |
| 3611 |
|
| 3612 |
if (!chanPtr->typePtr->close2Proc) { |
| 3613 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 3614 |
"half-close of channels not supported by %ss", |
| 3615 |
chanPtr->typePtr->typeName)); |
| 3616 |
return TCL_ERROR; |
| 3617 |
} |
| 3618 |
|
| 3619 |
/* |
| 3620 |
* Is the channel unstacked ? If not we fail. |
| 3621 |
*/ |
| 3622 |
|
| 3623 |
if (chanPtr != statePtr->topChanPtr) { |
| 3624 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 3625 |
"half-close not applicable to stack of transformations", -1)); |
| 3626 |
return TCL_ERROR; |
| 3627 |
} |
| 3628 |
|
| 3629 |
/* |
| 3630 |
* Check direction against channel mode. It is an error if we try to close |
| 3631 |
* a direction not supported by the channel (already closed, or never |
| 3632 |
* opened for that direction). |
| 3633 |
*/ |
| 3634 |
|
| 3635 |
if (!(GotFlag(statePtr, TCL_READABLE|TCL_WRITABLE) & flags)) { |
| 3636 |
const char *msg; |
| 3637 |
|
| 3638 |
if (flags & TCL_CLOSE_READ) { |
| 3639 |
msg = "read"; |
| 3640 |
} else { |
| 3641 |
msg = "write"; |
| 3642 |
} |
| 3643 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 3644 |
"Half-close of %s-side not possible, side not opened or" |
| 3645 |
" already closed", msg)); |
| 3646 |
return TCL_ERROR; |
| 3647 |
} |
| 3648 |
|
| 3649 |
/* |
| 3650 |
* A user may try to call half-close from within a channel close handler. |
| 3651 |
* That won't do. |
| 3652 |
*/ |
| 3653 |
|
| 3654 |
if (GotFlag(statePtr, CHANNEL_INCLOSE)) { |
| 3655 |
if (interp) { |
| 3656 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 3657 |
"illegal recursive call to close through close-handler" |
| 3658 |
" of channel", -1)); |
| 3659 |
} |
| 3660 |
return TCL_ERROR; |
| 3661 |
} |
| 3662 |
|
| 3663 |
if (flags & TCL_CLOSE_READ) { |
| 3664 |
/* |
| 3665 |
* Call the finalization code directly. There are no events to handle, |
| 3666 |
* there cannot be for the read-side. |
| 3667 |
*/ |
| 3668 |
|
| 3669 |
return CloseChannelPart(interp, chanPtr, 0, flags); |
| 3670 |
} else if (flags & TCL_CLOSE_WRITE) { |
| 3671 |
Tcl_Preserve(statePtr); |
| 3672 |
if (!GotFlag(statePtr, BG_FLUSH_SCHEDULED)) { |
| 3673 |
/* |
| 3674 |
* We don't want to re-enter CloseWrite(). |
| 3675 |
*/ |
| 3676 |
|
| 3677 |
if (!GotFlag(statePtr, CHANNEL_CLOSEDWRITE)) { |
| 3678 |
if (CloseWrite(interp, chanPtr) != TCL_OK) { |
| 3679 |
SetFlag(statePtr, CHANNEL_CLOSEDWRITE); |
| 3680 |
Tcl_Release(statePtr); |
| 3681 |
return TCL_ERROR; |
| 3682 |
} |
| 3683 |
} |
| 3684 |
} |
| 3685 |
SetFlag(statePtr, CHANNEL_CLOSEDWRITE); |
| 3686 |
Tcl_Release(statePtr); |
| 3687 |
} |
| 3688 |
|
| 3689 |
return TCL_OK; |
| 3690 |
} |
| 3691 |
|
| 3692 |
/* |
| 3693 |
*---------------------------------------------------------------------- |
| 3694 |
* |
| 3695 |
* CloseWrite -- |
| 3696 |
* |
| 3697 |
* Closes the write side a channel. |
| 3698 |
* |
| 3699 |
* Results: |
| 3700 |
* A standard Tcl result. |
| 3701 |
* |
| 3702 |
* Side effects: |
| 3703 |
* Closes the write side of the channel. |
| 3704 |
* |
| 3705 |
* NOTE: |
| 3706 |
* CloseWrite removes the channel as far as the user is concerned. |
| 3707 |
* However, the output data structures may continue to exist for a while |
| 3708 |
* longer if it has a background flush scheduled. The device itself is |
| 3709 |
* eventually closed and the channel structures modified, in |
| 3710 |
* CloseChannelPart, below. |
| 3711 |
* |
| 3712 |
*---------------------------------------------------------------------- |
| 3713 |
*/ |
| 3714 |
|
| 3715 |
static int |
| 3716 |
CloseWrite( |
| 3717 |
Tcl_Interp *interp, /* Interpreter for errors. */ |
| 3718 |
Channel *chanPtr) /* The channel whose write side is being |
| 3719 |
* closed. May still be used by some |
| 3720 |
* interpreter */ |
| 3721 |
{ |
| 3722 |
/* |
| 3723 |
* Notes: clear-channel-handlers - write side only ? or keep around, just |
| 3724 |
* not called. |
| 3725 |
* |
| 3726 |
* No close callbacks are run - channel is still open (read side) |
| 3727 |
*/ |
| 3728 |
|
| 3729 |
ChannelState *statePtr = chanPtr->state; |
| 3730 |
/* State of real IO channel. */ |
| 3731 |
int flushcode; |
| 3732 |
int result = 0; |
| 3733 |
|
| 3734 |
/* |
| 3735 |
* The call to FlushChannel will flush any queued output and invoke the |
| 3736 |
* close function of the channel driver, or it will set up the channel to |
| 3737 |
* be flushed and closed asynchronously. |
| 3738 |
*/ |
| 3739 |
|
| 3740 |
SetFlag(statePtr, CHANNEL_CLOSEDWRITE); |
| 3741 |
|
| 3742 |
flushcode = FlushChannel(interp, chanPtr, 0); |
| 3743 |
|
| 3744 |
/* |
| 3745 |
* TIP #219. |
| 3746 |
* Capture error messages put by the driver into the bypass area and put |
| 3747 |
* them into the regular interpreter result. |
| 3748 |
* |
| 3749 |
* Notes: Due to the assertion of CHANNEL_CLOSEDWRITE in the flags |
| 3750 |
* FlushChannel() has called CloseChannelPart(). While we can still access |
| 3751 |
* "chan" (no structures were freed), the only place which may still |
| 3752 |
* contain a message is the interpreter itself, and "CloseChannelPart" |
| 3753 |
* made sure to lift any channel message it generated into it. Hence the |
| 3754 |
* NULL argument in the call below. |
| 3755 |
*/ |
| 3756 |
|
| 3757 |
if (TclChanCaughtErrorBypass(interp, NULL)) { |
| 3758 |
result = EINVAL; |
| 3759 |
} |
| 3760 |
|
| 3761 |
if ((flushcode != 0) || (result != 0)) { |
| 3762 |
return TCL_ERROR; |
| 3763 |
} |
| 3764 |
|
| 3765 |
return TCL_OK; |
| 3766 |
} |
| 3767 |
|
| 3768 |
/* |
| 3769 |
*---------------------------------------------------------------------- |
| 3770 |
* |
| 3771 |
* CloseChannelPart -- |
| 3772 |
* |
| 3773 |
* Utility procedure to close a channel partially and free associated |
| 3774 |
* resources. If the channel was stacked it will never be run (The higher |
| 3775 |
* level forbid this). If the channel was not stacked, then we will free |
| 3776 |
* all the bits of the chosen side (read, or write) for the TOP channel. |
| 3777 |
* |
| 3778 |
* Results: |
| 3779 |
* Error code from an unreported error or the driver close2 operation. |
| 3780 |
* |
| 3781 |
* Side effects: |
| 3782 |
* May free memory, may change the value of errno. |
| 3783 |
* |
| 3784 |
*---------------------------------------------------------------------- |
| 3785 |
*/ |
| 3786 |
|
| 3787 |
static int |
| 3788 |
CloseChannelPart( |
| 3789 |
Tcl_Interp *interp, /* Interpreter for errors. */ |
| 3790 |
Channel *chanPtr, /* The channel being closed. May still be used |
| 3791 |
* by some interpreter. */ |
| 3792 |
int errorCode, /* Status of operation so far. */ |
| 3793 |
int flags) /* Flags telling us which side to close. */ |
| 3794 |
{ |
| 3795 |
ChannelState *statePtr; /* State of real IO channel. */ |
| 3796 |
int result; /* Of calling the close2proc. */ |
| 3797 |
|
| 3798 |
statePtr = chanPtr->state; |
| 3799 |
|
| 3800 |
if (flags & TCL_CLOSE_READ) { |
| 3801 |
/* |
| 3802 |
* No more input can be consumed so discard any leftover input. |
| 3803 |
*/ |
| 3804 |
|
| 3805 |
DiscardInputQueued(statePtr, 1); |
| 3806 |
} else if (flags & TCL_CLOSE_WRITE) { |
| 3807 |
/* |
| 3808 |
* The caller guarantees that there are no more buffers queued for |
| 3809 |
* output. |
| 3810 |
*/ |
| 3811 |
|
| 3812 |
if (statePtr->outQueueHead != NULL) { |
| 3813 |
Tcl_Panic("ClosechanHalf, closed write-side of channel: " |
| 3814 |
"queued output left"); |
| 3815 |
} |
| 3816 |
|
| 3817 |
/* |
| 3818 |
* If the EOF character is set in the channel, append that to the |
| 3819 |
* output device. |
| 3820 |
*/ |
| 3821 |
|
| 3822 |
if ((statePtr->outEofChar != 0) && GotFlag(statePtr, TCL_WRITABLE)) { |
| 3823 |
int dummy; |
| 3824 |
char c = (char) statePtr->outEofChar; |
| 3825 |
|
| 3826 |
(void) ChanWrite(chanPtr, &c, 1, &dummy); |
| 3827 |
} |
| 3828 |
|
| 3829 |
/* |
| 3830 |
* TIP #219, Tcl Channel Reflection API. |
| 3831 |
* Move a leftover error message in the channel bypass into the |
| 3832 |
* interpreter bypass. Just clear it if there is no interpreter. |
| 3833 |
*/ |
| 3834 |
|
| 3835 |
if (statePtr->chanMsg != NULL) { |
| 3836 |
if (interp != NULL) { |
| 3837 |
Tcl_SetChannelErrorInterp(interp, statePtr->chanMsg); |
| 3838 |
} |
| 3839 |
TclDecrRefCount(statePtr->chanMsg); |
| 3840 |
statePtr->chanMsg = NULL; |
| 3841 |
} |
| 3842 |
} |
| 3843 |
|
| 3844 |
/* |
| 3845 |
* Finally do what is asked of us. Close and free the channel driver state |
| 3846 |
* for the chosen side of the channel. This may leave a TIP #219 error |
| 3847 |
* message in the interp. |
| 3848 |
*/ |
| 3849 |
|
| 3850 |
result = chanPtr->typePtr->close2Proc(chanPtr->instanceData, NULL, flags); |
| 3851 |
|
| 3852 |
/* |
| 3853 |
* If we are being called synchronously, report either any latent error on |
| 3854 |
* the channel or the current error. |
| 3855 |
*/ |
| 3856 |
|
| 3857 |
if (statePtr->unreportedError != 0) { |
| 3858 |
errorCode = statePtr->unreportedError; |
| 3859 |
|
| 3860 |
/* |
| 3861 |
* TIP #219, Tcl Channel Reflection API. |
| 3862 |
* Move an error message found in the unreported area into the regular |
| 3863 |
* bypass (interp). This kills any message in the channel bypass area. |
| 3864 |
*/ |
| 3865 |
|
| 3866 |
if (statePtr->chanMsg != NULL) { |
| 3867 |
TclDecrRefCount(statePtr->chanMsg); |
| 3868 |
statePtr->chanMsg = NULL; |
| 3869 |
} |
| 3870 |
if (interp) { |
| 3871 |
Tcl_SetChannelErrorInterp(interp, statePtr->unreportedMsg); |
| 3872 |
} |
| 3873 |
} |
| 3874 |
if (errorCode == 0) { |
| 3875 |
errorCode = result; |
| 3876 |
if (errorCode != 0) { |
| 3877 |
Tcl_SetErrno(errorCode); |
| 3878 |
} |
| 3879 |
} |
| 3880 |
|
| 3881 |
/* |
| 3882 |
* TIP #219. |
| 3883 |
* Capture error messages put by the driver into the bypass area and put |
| 3884 |
* them into the regular interpreter result. See also the bottom of |
| 3885 |
* CloseWrite(). |
| 3886 |
*/ |
| 3887 |
|
| 3888 |
if (TclChanCaughtErrorBypass(interp, (Tcl_Channel) chanPtr)) { |
| 3889 |
result = EINVAL; |
| 3890 |
} |
| 3891 |
|
| 3892 |
if (result != 0) { |
| 3893 |
return TCL_ERROR; |
| 3894 |
} |
| 3895 |
|
| 3896 |
/* |
| 3897 |
* Remove the closed side from the channel mode/flags. |
| 3898 |
*/ |
| 3899 |
|
| 3900 |
ResetFlag(statePtr, flags & (TCL_READABLE | TCL_WRITABLE)); |
| 3901 |
return TCL_OK; |
| 3902 |
} |
| 3903 |
|
| 3904 |
/* |
| 3905 |
*---------------------------------------------------------------------- |
| 3906 |
* |
| 3907 |
* Tcl_ClearChannelHandlers -- |
| 3908 |
* |
| 3909 |
* Removes all channel handlers and event scripts from the channel, |
| 3910 |
* cancels all background copies involving the channel and any interest |
| 3911 |
* in events. |
| 3912 |
* |
| 3913 |
* Results: |
| 3914 |
* None. |
| 3915 |
* |
| 3916 |
* Side effects: |
| 3917 |
* See above. Deallocates memory. |
| 3918 |
* |
| 3919 |
*---------------------------------------------------------------------- |
| 3920 |
*/ |
| 3921 |
|
| 3922 |
void |
| 3923 |
Tcl_ClearChannelHandlers( |
| 3924 |
Tcl_Channel channel) |
| 3925 |
{ |
| 3926 |
ChannelHandler *chPtr, *chNext; /* Iterate over channel handlers. */ |
| 3927 |
EventScriptRecord *ePtr, *eNextPtr; /* Iterate over eventscript records. */ |
| 3928 |
Channel *chanPtr; /* The real IO channel. */ |
| 3929 |
ChannelState *statePtr; /* State of real IO channel. */ |
| 3930 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 3931 |
NextChannelHandler *nhPtr; |
| 3932 |
|
| 3933 |
/* |
| 3934 |
* This operation should occur at the top of a channel stack. |
| 3935 |
*/ |
| 3936 |
|
| 3937 |
chanPtr = (Channel *) channel; |
| 3938 |
statePtr = chanPtr->state; |
| 3939 |
chanPtr = statePtr->topChanPtr; |
| 3940 |
|
| 3941 |
/* |
| 3942 |
* Cancel any outstanding timer. |
| 3943 |
*/ |
| 3944 |
|
| 3945 |
DeleteTimerHandler(statePtr); |
| 3946 |
|
| 3947 |
/* |
| 3948 |
* Remove any references to channel handlers for this channel that may be |
| 3949 |
* about to be invoked. |
| 3950 |
*/ |
| 3951 |
|
| 3952 |
for (nhPtr = tsdPtr->nestedHandlerPtr; nhPtr != NULL; |
| 3953 |
nhPtr = nhPtr->nestedHandlerPtr) { |
| 3954 |
if (nhPtr->nextHandlerPtr && |
| 3955 |
(nhPtr->nextHandlerPtr->chanPtr == chanPtr)) { |
| 3956 |
nhPtr->nextHandlerPtr = NULL; |
| 3957 |
} |
| 3958 |
} |
| 3959 |
|
| 3960 |
/* |
| 3961 |
* Remove all the channel handler records attached to the channel itself. |
| 3962 |
*/ |
| 3963 |
|
| 3964 |
for (chPtr = statePtr->chPtr; chPtr != NULL; chPtr = chNext) { |
| 3965 |
chNext = chPtr->nextPtr; |
| 3966 |
ckfree(chPtr); |
| 3967 |
} |
| 3968 |
statePtr->chPtr = NULL; |
| 3969 |
|
| 3970 |
/* |
| 3971 |
* Cancel any pending copy operation. |
| 3972 |
*/ |
| 3973 |
|
| 3974 |
if (statePtr->csPtrR) { |
| 3975 |
StopCopy(statePtr->csPtrR); |
| 3976 |
statePtr->csPtrR = NULL; |
| 3977 |
} |
| 3978 |
if (statePtr->csPtrW) { |
| 3979 |
StopCopy(statePtr->csPtrW); |
| 3980 |
statePtr->csPtrW = NULL; |
| 3981 |
} |
| 3982 |
|
| 3983 |
/* |
| 3984 |
* Must set the interest mask now to 0, otherwise infinite loops will |
| 3985 |
* occur if Tcl_DoOneEvent is called before the channel is finally deleted |
| 3986 |
* in FlushChannel. This can happen if the channel has a background flush |
| 3987 |
* active. |
| 3988 |
*/ |
| 3989 |
|
| 3990 |
statePtr->interestMask = 0; |
| 3991 |
|
| 3992 |
/* |
| 3993 |
* Remove any EventScript records for this channel. |
| 3994 |
*/ |
| 3995 |
|
| 3996 |
for (ePtr = statePtr->scriptRecordPtr; ePtr != NULL; ePtr = eNextPtr) { |
| 3997 |
eNextPtr = ePtr->nextPtr; |
| 3998 |
TclDecrRefCount(ePtr->scriptPtr); |
| 3999 |
ckfree(ePtr); |
| 4000 |
} |
| 4001 |
statePtr->scriptRecordPtr = NULL; |
| 4002 |
} |
| 4003 |
|
| 4004 |
/* |
| 4005 |
*---------------------------------------------------------------------- |
| 4006 |
* |
| 4007 |
* Tcl_Write -- |
| 4008 |
* |
| 4009 |
* Puts a sequence of bytes into an output buffer, may queue the buffer |
| 4010 |
* for output if it gets full, and also remembers whether the current |
| 4011 |
* buffer is ready e.g. if it contains a newline and we are in line |
| 4012 |
* buffering mode. Compensates stacking, i.e. will redirect the data from |
| 4013 |
* the specified channel to the topmost channel in a stack. |
| 4014 |
* |
| 4015 |
* No encoding conversions are applied to the bytes being read. |
| 4016 |
* |
| 4017 |
* Results: |
| 4018 |
* The number of bytes written or -1 in case of error. If |
| 4019 |
* -1, Tcl_GetErrno will return the error code. |
| 4020 |
* |
| 4021 |
* Side effects: |
| 4022 |
* May buffer up output and may cause output to be produced on the |
| 4023 |
* channel. |
| 4024 |
* |
| 4025 |
*---------------------------------------------------------------------- |
| 4026 |
*/ |
| 4027 |
|
| 4028 |
int |
| 4029 |
Tcl_Write( |
| 4030 |
Tcl_Channel chan, /* The channel to buffer output for. */ |
| 4031 |
const char *src, /* Data to queue in output buffer. */ |
| 4032 |
int srcLen) /* Length of data in bytes, or < 0 for |
| 4033 |
* strlen(). */ |
| 4034 |
{ |
| 4035 |
/* |
| 4036 |
* Always use the topmost channel of the stack |
| 4037 |
*/ |
| 4038 |
|
| 4039 |
Channel *chanPtr; |
| 4040 |
ChannelState *statePtr; /* State info for channel */ |
| 4041 |
|
| 4042 |
statePtr = ((Channel *) chan)->state; |
| 4043 |
chanPtr = statePtr->topChanPtr; |
| 4044 |
|
| 4045 |
if (CheckChannelErrors(statePtr, TCL_WRITABLE) != 0) { |
| 4046 |
return -1; |
| 4047 |
} |
| 4048 |
|
| 4049 |
if (srcLen < 0) { |
| 4050 |
srcLen = strlen(src); |
| 4051 |
} |
| 4052 |
if (WriteBytes(chanPtr, src, srcLen) < 0) { |
| 4053 |
return -1; |
| 4054 |
} |
| 4055 |
return srcLen; |
| 4056 |
} |
| 4057 |
|
| 4058 |
/* |
| 4059 |
*---------------------------------------------------------------------- |
| 4060 |
* |
| 4061 |
* Tcl_WriteRaw -- |
| 4062 |
* |
| 4063 |
* Puts a sequence of bytes into an output buffer, may queue the buffer |
| 4064 |
* for output if it gets full, and also remembers whether the current |
| 4065 |
* buffer is ready e.g. if it contains a newline and we are in line |
| 4066 |
* buffering mode. Writes directly to the driver of the channel, does not |
| 4067 |
* compensate for stacking. |
| 4068 |
* |
| 4069 |
* No encoding conversions are applied to the bytes being read. |
| 4070 |
* |
| 4071 |
* Results: |
| 4072 |
* The number of bytes written or -1 in case of error. If |
| 4073 |
* -1, Tcl_GetErrno will return the error code. |
| 4074 |
* |
| 4075 |
* Side effects: |
| 4076 |
* May buffer up output and may cause output to be produced on the |
| 4077 |
* channel. |
| 4078 |
* |
| 4079 |
*---------------------------------------------------------------------- |
| 4080 |
*/ |
| 4081 |
|
| 4082 |
int |
| 4083 |
Tcl_WriteRaw( |
| 4084 |
Tcl_Channel chan, /* The channel to buffer output for. */ |
| 4085 |
const char *src, /* Data to queue in output buffer. */ |
| 4086 |
int srcLen) /* Length of data in bytes, or -1 for |
| 4087 |
* strlen(). */ |
| 4088 |
{ |
| 4089 |
Channel *chanPtr = ((Channel *) chan); |
| 4090 |
ChannelState *statePtr = chanPtr->state; |
| 4091 |
/* State info for channel */ |
| 4092 |
int errorCode; |
| 4093 |
int written; |
| 4094 |
|
| 4095 |
if (CheckChannelErrors(statePtr, TCL_WRITABLE | CHANNEL_RAW_MODE) != 0) { |
| 4096 |
return -1; |
| 4097 |
} |
| 4098 |
|
| 4099 |
if (srcLen < 0) { |
| 4100 |
srcLen = strlen(src); |
| 4101 |
} |
| 4102 |
|
| 4103 |
/* |
| 4104 |
* Go immediately to the driver, do all the error handling by ourselves. |
| 4105 |
* The code was stolen from 'FlushChannel'. |
| 4106 |
*/ |
| 4107 |
|
| 4108 |
written = ChanWrite(chanPtr, src, srcLen, &errorCode); |
| 4109 |
if (written < 0) { |
| 4110 |
Tcl_SetErrno(errorCode); |
| 4111 |
} |
| 4112 |
|
| 4113 |
return written; |
| 4114 |
} |
| 4115 |
|
| 4116 |
/* |
| 4117 |
*--------------------------------------------------------------------------- |
| 4118 |
* |
| 4119 |
* Tcl_WriteChars -- |
| 4120 |
* |
| 4121 |
* Takes a sequence of UTF-8 characters and converts them for output |
| 4122 |
* using the channel's current encoding, may queue the buffer for output |
| 4123 |
* if it gets full, and also remembers whether the current buffer is |
| 4124 |
* ready e.g. if it contains a newline and we are in line buffering |
| 4125 |
* mode. Compensates stacking, i.e. will redirect the data from the |
| 4126 |
* specified channel to the topmost channel in a stack. |
| 4127 |
* |
| 4128 |
* Results: |
| 4129 |
* The number of bytes written or -1 in case of error. If |
| 4130 |
* -1, Tcl_GetErrno will return the error code. |
| 4131 |
* |
| 4132 |
* Side effects: |
| 4133 |
* May buffer up output and may cause output to be produced on the |
| 4134 |
* channel. |
| 4135 |
* |
| 4136 |
*---------------------------------------------------------------------- |
| 4137 |
*/ |
| 4138 |
|
| 4139 |
int |
| 4140 |
Tcl_WriteChars( |
| 4141 |
Tcl_Channel chan, /* The channel to buffer output for. */ |
| 4142 |
const char *src, /* UTF-8 characters to queue in output |
| 4143 |
* buffer. */ |
| 4144 |
int len) /* Length of string in bytes, or -1 for |
| 4145 |
* strlen(). */ |
| 4146 |
{ |
| 4147 |
Channel *chanPtr = (Channel *) chan; |
| 4148 |
ChannelState *statePtr = chanPtr->state; /* State info for channel */ |
| 4149 |
int result; |
| 4150 |
Tcl_Obj *objPtr; |
| 4151 |
|
| 4152 |
if (CheckChannelErrors(statePtr, TCL_WRITABLE) != 0) { |
| 4153 |
return -1; |
| 4154 |
} |
| 4155 |
|
| 4156 |
chanPtr = statePtr->topChanPtr; |
| 4157 |
|
| 4158 |
if (len < 0) { |
| 4159 |
len = strlen(src); |
| 4160 |
} |
| 4161 |
if (statePtr->encoding) { |
| 4162 |
return WriteChars(chanPtr, src, len); |
| 4163 |
} |
| 4164 |
|
| 4165 |
/* |
| 4166 |
* Inefficient way to convert UTF-8 to byte-array, but the code |
| 4167 |
* parallels the way it is done for objects. Special case for 1-byte |
| 4168 |
* (used by e.g. [puts] for the \n) could be extended to more efficient |
| 4169 |
* translation of the src string. |
| 4170 |
*/ |
| 4171 |
|
| 4172 |
if ((len == 1) && (UCHAR(*src) < 0xC0)) { |
| 4173 |
return WriteBytes(chanPtr, src, len); |
| 4174 |
} |
| 4175 |
|
| 4176 |
objPtr = Tcl_NewStringObj(src, len); |
| 4177 |
src = (char *) Tcl_GetByteArrayFromObj(objPtr, &len); |
| 4178 |
result = WriteBytes(chanPtr, src, len); |
| 4179 |
TclDecrRefCount(objPtr); |
| 4180 |
return result; |
| 4181 |
} |
| 4182 |
|
| 4183 |
/* |
| 4184 |
*--------------------------------------------------------------------------- |
| 4185 |
* |
| 4186 |
* Tcl_WriteObj -- |
| 4187 |
* |
| 4188 |
* Takes the Tcl object and queues its contents for output. If the |
| 4189 |
* encoding of the channel is NULL, takes the byte-array representation |
| 4190 |
* of the object and queues those bytes for output. Otherwise, takes the |
| 4191 |
* characters in the UTF-8 (string) representation of the object and |
| 4192 |
* converts them for output using the channel's current encoding. May |
| 4193 |
* flush internal buffers to output if one becomes full or is ready for |
| 4194 |
* some other reason, e.g. if it contains a newline and the channel is in |
| 4195 |
* line buffering mode. |
| 4196 |
* |
| 4197 |
* Results: |
| 4198 |
* The number of bytes written or -1 in case of error. If |
| 4199 |
* -1, Tcl_GetErrno() will return the error code. |
| 4200 |
* |
| 4201 |
* Side effects: |
| 4202 |
* May buffer up output and may cause output to be produced on the |
| 4203 |
* channel. |
| 4204 |
* |
| 4205 |
*---------------------------------------------------------------------- |
| 4206 |
*/ |
| 4207 |
|
| 4208 |
int |
| 4209 |
Tcl_WriteObj( |
| 4210 |
Tcl_Channel chan, /* The channel to buffer output for. */ |
| 4211 |
Tcl_Obj *objPtr) /* The object to write. */ |
| 4212 |
{ |
| 4213 |
/* |
| 4214 |
* Always use the topmost channel of the stack |
| 4215 |
*/ |
| 4216 |
|
| 4217 |
Channel *chanPtr; |
| 4218 |
ChannelState *statePtr; /* State info for channel */ |
| 4219 |
const char *src; |
| 4220 |
int srcLen; |
| 4221 |
|
| 4222 |
statePtr = ((Channel *) chan)->state; |
| 4223 |
chanPtr = statePtr->topChanPtr; |
| 4224 |
|
| 4225 |
if (CheckChannelErrors(statePtr, TCL_WRITABLE) != 0) { |
| 4226 |
return -1; |
| 4227 |
} |
| 4228 |
if (statePtr->encoding == NULL) { |
| 4229 |
src = (char *) Tcl_GetByteArrayFromObj(objPtr, &srcLen); |
| 4230 |
return WriteBytes(chanPtr, src, srcLen); |
| 4231 |
} else { |
| 4232 |
src = TclGetStringFromObj(objPtr, &srcLen); |
| 4233 |
return WriteChars(chanPtr, src, srcLen); |
| 4234 |
} |
| 4235 |
} |
| 4236 |
|
| 4237 |
static void |
| 4238 |
WillWrite( |
| 4239 |
Channel *chanPtr) |
| 4240 |
{ |
| 4241 |
int inputBuffered; |
| 4242 |
|
| 4243 |
if ((Tcl_ChannelSeekProc(chanPtr->typePtr) != NULL) |
| 4244 |
&& ((inputBuffered = Tcl_InputBuffered((Tcl_Channel) chanPtr)) > 0)){ |
| 4245 |
int ignore; |
| 4246 |
|
| 4247 |
DiscardInputQueued(chanPtr->state, 0); |
| 4248 |
ChanSeek(chanPtr, -inputBuffered, SEEK_CUR, &ignore); |
| 4249 |
} |
| 4250 |
} |
| 4251 |
|
| 4252 |
static int |
| 4253 |
WillRead( |
| 4254 |
Channel *chanPtr) |
| 4255 |
{ |
| 4256 |
if (chanPtr->typePtr == NULL) { |
| 4257 |
/* |
| 4258 |
* Prevent read attempts on a closed channel. |
| 4259 |
*/ |
| 4260 |
|
| 4261 |
DiscardInputQueued(chanPtr->state, 0); |
| 4262 |
Tcl_SetErrno(EINVAL); |
| 4263 |
return -1; |
| 4264 |
} |
| 4265 |
if ((Tcl_ChannelSeekProc(chanPtr->typePtr) != NULL) |
| 4266 |
&& (Tcl_OutputBuffered((Tcl_Channel) chanPtr) > 0)) { |
| 4267 |
/* |
| 4268 |
* CAVEAT - The assumption here is that FlushChannel() will push out |
| 4269 |
* the bytes of any writes that are in progress. Since this is a |
| 4270 |
* seekable channel, we assume it is not one that can block and force |
| 4271 |
* bg flushing. Channels we know that can do that - sockets, pipes - |
| 4272 |
* are not seekable. If the assumption is wrong, more drastic measures |
| 4273 |
* may be required here like temporarily setting the channel into |
| 4274 |
* blocking mode. |
| 4275 |
*/ |
| 4276 |
|
| 4277 |
if (FlushChannel(NULL, chanPtr, 0) != 0) { |
| 4278 |
return -1; |
| 4279 |
} |
| 4280 |
} |
| 4281 |
return 0; |
| 4282 |
} |
| 4283 |
|
| 4284 |
/* |
| 4285 |
*---------------------------------------------------------------------- |
| 4286 |
* |
| 4287 |
* Write -- |
| 4288 |
* |
| 4289 |
* Convert srcLen bytes starting at src according to encoding and write |
| 4290 |
* produced bytes into an output buffer, may queue the buffer for output |
| 4291 |
* if it gets full, and also remembers whether the current buffer is |
| 4292 |
* ready e.g. if it contains a newline and we are in line buffering mode. |
| 4293 |
* |
| 4294 |
* Results: |
| 4295 |
* The number of bytes written or -1 in case of error. If -1, |
| 4296 |
* Tcl_GetErrno will return the error code. |
| 4297 |
* |
| 4298 |
* Side effects: |
| 4299 |
* May buffer up output and may cause output to be produced on the |
| 4300 |
* channel. |
| 4301 |
* |
| 4302 |
*---------------------------------------------------------------------- |
| 4303 |
*/ |
| 4304 |
|
| 4305 |
static int |
| 4306 |
Write( |
| 4307 |
Channel *chanPtr, /* The channel to buffer output for. */ |
| 4308 |
const char *src, /* UTF-8 string to write. */ |
| 4309 |
int srcLen, /* Length of UTF-8 string in bytes. */ |
| 4310 |
Tcl_Encoding encoding) |
| 4311 |
{ |
| 4312 |
ChannelState *statePtr = chanPtr->state; |
| 4313 |
/* State info for channel */ |
| 4314 |
char *nextNewLine = NULL; |
| 4315 |
int endEncoding, needNlFlush = 0; |
| 4316 |
int saved = 0, total = 0, flushed = 0; |
| 4317 |
char safe[BUFFER_PADDING]; |
| 4318 |
int encodingError = 0; |
| 4319 |
|
| 4320 |
if (srcLen) { |
| 4321 |
WillWrite(chanPtr); |
| 4322 |
} |
| 4323 |
|
| 4324 |
/* |
| 4325 |
* Write the terminated escape sequence even if srcLen is 0. |
| 4326 |
*/ |
| 4327 |
|
| 4328 |
endEncoding = ((statePtr->outputEncodingFlags & TCL_ENCODING_END) != 0); |
| 4329 |
if (GotFlag(statePtr, CHANNEL_LINEBUFFERED) |
| 4330 |
|| (statePtr->outputTranslation != TCL_TRANSLATE_LF)) { |
| 4331 |
nextNewLine = (char *)memchr(src, '\n', srcLen); |
| 4332 |
} |
| 4333 |
|
| 4334 |
while (srcLen + saved + endEncoding > 0 && !encodingError) { |
| 4335 |
ChannelBuffer *bufPtr; |
| 4336 |
char *dst; |
| 4337 |
int result, srcRead, dstLen, dstWrote; |
| 4338 |
int srcLimit = srcLen; |
| 4339 |
|
| 4340 |
if (nextNewLine) { |
| 4341 |
srcLimit = nextNewLine - src; |
| 4342 |
} |
| 4343 |
|
| 4344 |
/* Get space to write into */ |
| 4345 |
bufPtr = statePtr->curOutPtr; |
| 4346 |
if (bufPtr == NULL) { |
| 4347 |
bufPtr = AllocChannelBuffer(statePtr->bufSize); |
| 4348 |
statePtr->curOutPtr = bufPtr; |
| 4349 |
} |
| 4350 |
if (saved) { |
| 4351 |
/* |
| 4352 |
* Here's some translated bytes left over from the last buffer |
| 4353 |
* that we need to stick at the beginning of this buffer. |
| 4354 |
*/ |
| 4355 |
|
| 4356 |
memcpy(InsertPoint(bufPtr), safe, saved); |
| 4357 |
bufPtr->nextAdded += saved; |
| 4358 |
saved = 0; |
| 4359 |
} |
| 4360 |
PreserveChannelBuffer(bufPtr); |
| 4361 |
dst = InsertPoint(bufPtr); |
| 4362 |
dstLen = SpaceLeft(bufPtr); |
| 4363 |
|
| 4364 |
result = Tcl_UtfToExternal(NULL, encoding, src, srcLimit, |
| 4365 |
statePtr->outputEncodingFlags, |
| 4366 |
&statePtr->outputEncodingState, dst, |
| 4367 |
dstLen + BUFFER_PADDING, &srcRead, &dstWrote, NULL); |
| 4368 |
|
| 4369 |
/* |
| 4370 |
* See chan-io-1.[89]. Tcl Bug 506297. |
| 4371 |
*/ |
| 4372 |
|
| 4373 |
statePtr->outputEncodingFlags &= ~TCL_ENCODING_START; |
| 4374 |
|
| 4375 |
if ((result != TCL_OK) && (srcRead + dstWrote == 0)) { |
| 4376 |
encodingError = 1; |
| 4377 |
result = TCL_OK; |
| 4378 |
} |
| 4379 |
|
| 4380 |
bufPtr->nextAdded += dstWrote; |
| 4381 |
src += srcRead; |
| 4382 |
srcLen -= srcRead; |
| 4383 |
total += dstWrote; |
| 4384 |
dst += dstWrote; |
| 4385 |
dstLen -= dstWrote; |
| 4386 |
|
| 4387 |
if (src == nextNewLine && dstLen > 0) { |
| 4388 |
static char crln[3] = "\r\n"; |
| 4389 |
char *nl = NULL; |
| 4390 |
int nlLen = 0; |
| 4391 |
|
| 4392 |
switch (statePtr->outputTranslation) { |
| 4393 |
case TCL_TRANSLATE_LF: |
| 4394 |
nl = crln + 1; |
| 4395 |
nlLen = 1; |
| 4396 |
break; |
| 4397 |
case TCL_TRANSLATE_CR: |
| 4398 |
nl = crln; |
| 4399 |
nlLen = 1; |
| 4400 |
break; |
| 4401 |
case TCL_TRANSLATE_CRLF: |
| 4402 |
nl = crln; |
| 4403 |
nlLen = 2; |
| 4404 |
break; |
| 4405 |
default: |
| 4406 |
Tcl_Panic("unknown output translation requested"); |
| 4407 |
break; |
| 4408 |
} |
| 4409 |
|
| 4410 |
result |= Tcl_UtfToExternal(NULL, encoding, nl, nlLen, |
| 4411 |
statePtr->outputEncodingFlags, |
| 4412 |
&statePtr->outputEncodingState, dst, |
| 4413 |
dstLen + BUFFER_PADDING, &srcRead, &dstWrote, NULL); |
| 4414 |
assert(srcRead == nlLen); |
| 4415 |
|
| 4416 |
bufPtr->nextAdded += dstWrote; |
| 4417 |
src++; |
| 4418 |
srcLen--; |
| 4419 |
total += dstWrote; |
| 4420 |
dst += dstWrote; |
| 4421 |
dstLen -= dstWrote; |
| 4422 |
nextNewLine = (char *)memchr(src, '\n', srcLen); |
| 4423 |
needNlFlush = 1; |
| 4424 |
} |
| 4425 |
|
| 4426 |
if (IsBufferOverflowing(bufPtr)) { |
| 4427 |
/* |
| 4428 |
* When translating from UTF-8 to external encoding, we allowed |
| 4429 |
* the translation to produce a character that crossed the end of |
| 4430 |
* the output buffer, so that we would get a completely full |
| 4431 |
* buffer before flushing it. The extra bytes will be moved to the |
| 4432 |
* beginning of the next buffer. |
| 4433 |
*/ |
| 4434 |
|
| 4435 |
saved = -SpaceLeft(bufPtr); |
| 4436 |
memcpy(safe, dst + dstLen, saved); |
| 4437 |
bufPtr->nextAdded = bufPtr->bufLength; |
| 4438 |
} |
| 4439 |
|
| 4440 |
if ((srcLen + saved == 0) && (result == TCL_OK)) { |
| 4441 |
endEncoding = 0; |
| 4442 |
} |
| 4443 |
|
| 4444 |
if (IsBufferFull(bufPtr)) { |
| 4445 |
if (FlushChannel(NULL, chanPtr, 0) != 0) { |
| 4446 |
ReleaseChannelBuffer(bufPtr); |
| 4447 |
return -1; |
| 4448 |
} |
| 4449 |
flushed += statePtr->bufSize; |
| 4450 |
|
| 4451 |
/* |
| 4452 |
* We just flushed. So if we have needNlFlush set to record that |
| 4453 |
* we need to flush because there is a (translated) newline in the |
| 4454 |
* buffer, that's likely not true any more. But there is a tricky |
| 4455 |
* exception. If we have saved bytes that did not really get |
| 4456 |
* flushed and those bytes came from a translation of a newline as |
| 4457 |
* the last thing taken from the src array, then needNlFlush needs |
| 4458 |
* to remain set to flag that the next buffer still needs a |
| 4459 |
* newline flush. |
| 4460 |
*/ |
| 4461 |
|
| 4462 |
if (needNlFlush && (saved == 0 || src[-1] != '\n')) { |
| 4463 |
needNlFlush = 0; |
| 4464 |
} |
| 4465 |
} |
| 4466 |
ReleaseChannelBuffer(bufPtr); |
| 4467 |
} |
| 4468 |
if (((flushed < total) && GotFlag(statePtr, CHANNEL_UNBUFFERED)) || |
| 4469 |
(needNlFlush && GotFlag(statePtr, CHANNEL_LINEBUFFERED))) { |
| 4470 |
if (FlushChannel(NULL, chanPtr, 0) != 0) { |
| 4471 |
return -1; |
| 4472 |
} |
| 4473 |
} |
| 4474 |
|
| 4475 |
if (encodingError) { |
| 4476 |
Tcl_SetErrno(EINVAL); |
| 4477 |
return -1; |
| 4478 |
} |
| 4479 |
return total; |
| 4480 |
} |
| 4481 |
|
| 4482 |
/* |
| 4483 |
*--------------------------------------------------------------------------- |
| 4484 |
* |
| 4485 |
* Tcl_Gets -- |
| 4486 |
* |
| 4487 |
* Reads a complete line of input from the channel into a Tcl_DString. |
| 4488 |
* |
| 4489 |
* Results: |
| 4490 |
* Length of line read (in characters) or -1 if error, EOF, or blocked. |
| 4491 |
* If -1, use Tcl_GetErrno() to retrieve the POSIX error code for the |
| 4492 |
* error or condition that occurred. |
| 4493 |
* |
| 4494 |
* Side effects: |
| 4495 |
* May flush output on the channel. May cause input to be consumed from |
| 4496 |
* the channel. |
| 4497 |
* |
| 4498 |
*--------------------------------------------------------------------------- |
| 4499 |
*/ |
| 4500 |
|
| 4501 |
int |
| 4502 |
Tcl_Gets( |
| 4503 |
Tcl_Channel chan, /* Channel from which to read. */ |
| 4504 |
Tcl_DString *lineRead) /* The line read will be appended to this |
| 4505 |
* DString as UTF-8 characters. The caller |
| 4506 |
* must have initialized it and is responsible |
| 4507 |
* for managing the storage. */ |
| 4508 |
{ |
| 4509 |
Tcl_Obj *objPtr; |
| 4510 |
int charsStored; |
| 4511 |
|
| 4512 |
TclNewObj(objPtr); |
| 4513 |
charsStored = Tcl_GetsObj(chan, objPtr); |
| 4514 |
if (charsStored > 0) { |
| 4515 |
TclDStringAppendObj(lineRead, objPtr); |
| 4516 |
} |
| 4517 |
TclDecrRefCount(objPtr); |
| 4518 |
return charsStored; |
| 4519 |
} |
| 4520 |
|
| 4521 |
/* |
| 4522 |
*--------------------------------------------------------------------------- |
| 4523 |
* |
| 4524 |
* Tcl_GetsObj -- |
| 4525 |
* |
| 4526 |
* Accumulate input from the input channel until end-of-line or |
| 4527 |
* end-of-file has been seen. Bytes read from the input channel are |
| 4528 |
* converted to UTF-8 using the encoding specified by the channel. |
| 4529 |
* |
| 4530 |
* Results: |
| 4531 |
* Number of characters accumulated in the object or -1 if error, |
| 4532 |
* blocked, or EOF. If -1, use Tcl_GetErrno() to retrieve the POSIX error |
| 4533 |
* code for the error or condition that occurred. |
| 4534 |
* |
| 4535 |
* Side effects: |
| 4536 |
* Consumes input from the channel. |
| 4537 |
* |
| 4538 |
* On reading EOF, leave channel pointing at EOF char. On reading EOL, |
| 4539 |
* leave channel pointing after EOL, but don't return EOL in dst buffer. |
| 4540 |
* |
| 4541 |
*--------------------------------------------------------------------------- |
| 4542 |
*/ |
| 4543 |
|
| 4544 |
int |
| 4545 |
Tcl_GetsObj( |
| 4546 |
Tcl_Channel chan, /* Channel from which to read. */ |
| 4547 |
Tcl_Obj *objPtr) /* The line read will be appended to this |
| 4548 |
* object as UTF-8 characters. */ |
| 4549 |
{ |
| 4550 |
GetsState gs; |
| 4551 |
Channel *chanPtr = (Channel *) chan; |
| 4552 |
ChannelState *statePtr = chanPtr->state; |
| 4553 |
/* State info for channel */ |
| 4554 |
ChannelBuffer *bufPtr; |
| 4555 |
int inEofChar, skip, copiedTotal, oldFlags, oldRemoved; |
| 4556 |
int oldLength; |
| 4557 |
Tcl_Encoding encoding; |
| 4558 |
char *dst, *dstEnd, *eol, *eof; |
| 4559 |
Tcl_EncodingState oldState; |
| 4560 |
|
| 4561 |
if (CheckChannelErrors(statePtr, TCL_READABLE) != 0) { |
| 4562 |
return -1; |
| 4563 |
} |
| 4564 |
|
| 4565 |
/* |
| 4566 |
* If we're sitting ready to read the eofchar, there's no need to |
| 4567 |
* do it. |
| 4568 |
*/ |
| 4569 |
|
| 4570 |
if (GotFlag(statePtr, CHANNEL_STICKY_EOF)) { |
| 4571 |
SetFlag(statePtr, CHANNEL_EOF); |
| 4572 |
assert(statePtr->inputEncodingFlags & TCL_ENCODING_END); |
| 4573 |
assert(!GotFlag(statePtr, CHANNEL_BLOCKED|INPUT_SAW_CR)); |
| 4574 |
|
| 4575 |
/* TODO: Do we need this? */ |
| 4576 |
UpdateInterest(chanPtr); |
| 4577 |
return -1; |
| 4578 |
} |
| 4579 |
|
| 4580 |
/* |
| 4581 |
* A binary version of Tcl_GetsObj. This could also handle encodings that |
| 4582 |
* are ascii-7 pure (iso8859, utf-8, ...) with a final encoding conversion |
| 4583 |
* done on objPtr. |
| 4584 |
*/ |
| 4585 |
|
| 4586 |
if ((statePtr->encoding == NULL) |
| 4587 |
&& ((statePtr->inputTranslation == TCL_TRANSLATE_LF) |
| 4588 |
|| (statePtr->inputTranslation == TCL_TRANSLATE_CR))) { |
| 4589 |
return TclGetsObjBinary(chan, objPtr); |
| 4590 |
} |
| 4591 |
|
| 4592 |
/* |
| 4593 |
* This operation should occur at the top of a channel stack. |
| 4594 |
*/ |
| 4595 |
|
| 4596 |
chanPtr = statePtr->topChanPtr; |
| 4597 |
TclChannelPreserve((Tcl_Channel)chanPtr); |
| 4598 |
|
| 4599 |
bufPtr = statePtr->inQueueHead; |
| 4600 |
encoding = statePtr->encoding; |
| 4601 |
|
| 4602 |
/* |
| 4603 |
* Preserved so we can restore the channel's state in case we don't find a |
| 4604 |
* newline in the available input. |
| 4605 |
*/ |
| 4606 |
|
| 4607 |
(void)TclGetStringFromObj(objPtr, &oldLength); |
| 4608 |
oldFlags = statePtr->inputEncodingFlags; |
| 4609 |
oldState = statePtr->inputEncodingState; |
| 4610 |
oldRemoved = BUFFER_PADDING; |
| 4611 |
if (bufPtr != NULL) { |
| 4612 |
oldRemoved = bufPtr->nextRemoved; |
| 4613 |
} |
| 4614 |
|
| 4615 |
/* |
| 4616 |
* If there is no encoding, use "iso8859-1" -- Tcl_GetsObj() doesn't |
| 4617 |
* produce ByteArray objects. |
| 4618 |
*/ |
| 4619 |
|
| 4620 |
if (encoding == NULL) { |
| 4621 |
encoding = GetBinaryEncoding(); |
| 4622 |
} |
| 4623 |
|
| 4624 |
/* |
| 4625 |
* Object used by FilterInputBytes to keep track of how much data has been |
| 4626 |
* consumed from the channel buffers. |
| 4627 |
*/ |
| 4628 |
|
| 4629 |
gs.objPtr = objPtr; |
| 4630 |
gs.dstPtr = &dst; |
| 4631 |
gs.encoding = encoding; |
| 4632 |
gs.bufPtr = bufPtr; |
| 4633 |
gs.state = oldState; |
| 4634 |
gs.rawRead = 0; |
| 4635 |
gs.bytesWrote = 0; |
| 4636 |
gs.charsWrote = 0; |
| 4637 |
gs.totalChars = 0; |
| 4638 |
|
| 4639 |
dst = objPtr->bytes + oldLength; |
| 4640 |
dstEnd = dst; |
| 4641 |
|
| 4642 |
skip = 0; |
| 4643 |
eof = NULL; |
| 4644 |
inEofChar = statePtr->inEofChar; |
| 4645 |
|
| 4646 |
ResetFlag(statePtr, CHANNEL_BLOCKED); |
| 4647 |
while (1) { |
| 4648 |
if (dst >= dstEnd) { |
| 4649 |
if (FilterInputBytes(chanPtr, &gs) != 0) { |
| 4650 |
goto restore; |
| 4651 |
} |
| 4652 |
dstEnd = dst + gs.bytesWrote; |
| 4653 |
} |
| 4654 |
|
| 4655 |
/* |
| 4656 |
* Remember if EOF char is seen, then look for EOL anyhow, because the |
| 4657 |
* EOL might be before the EOF char. |
| 4658 |
*/ |
| 4659 |
|
| 4660 |
if (inEofChar != '\0') { |
| 4661 |
for (eol = dst; eol < dstEnd; eol++) { |
| 4662 |
if (*eol == inEofChar) { |
| 4663 |
dstEnd = eol; |
| 4664 |
eof = eol; |
| 4665 |
break; |
| 4666 |
} |
| 4667 |
} |
| 4668 |
} |
| 4669 |
|
| 4670 |
/* |
| 4671 |
* On EOL, leave current file position pointing after the EOL, but |
| 4672 |
* don't store the EOL in the output string. |
| 4673 |
*/ |
| 4674 |
|
| 4675 |
switch (statePtr->inputTranslation) { |
| 4676 |
case TCL_TRANSLATE_LF: |
| 4677 |
for (eol = dst; eol < dstEnd; eol++) { |
| 4678 |
if (*eol == '\n') { |
| 4679 |
skip = 1; |
| 4680 |
goto gotEOL; |
| 4681 |
} |
| 4682 |
} |
| 4683 |
break; |
| 4684 |
case TCL_TRANSLATE_CR: |
| 4685 |
for (eol = dst; eol < dstEnd; eol++) { |
| 4686 |
if (*eol == '\r') { |
| 4687 |
skip = 1; |
| 4688 |
goto gotEOL; |
| 4689 |
} |
| 4690 |
} |
| 4691 |
break; |
| 4692 |
case TCL_TRANSLATE_CRLF: |
| 4693 |
for (eol = dst; eol < dstEnd; eol++) { |
| 4694 |
if (*eol == '\r') { |
| 4695 |
eol++; |
| 4696 |
|
| 4697 |
/* |
| 4698 |
* If a CR is at the end of the buffer, then check for a |
| 4699 |
* LF at the beginning of the next buffer, unless EOF char |
| 4700 |
* was found already. |
| 4701 |
*/ |
| 4702 |
|
| 4703 |
if (eol >= dstEnd) { |
| 4704 |
int offset; |
| 4705 |
|
| 4706 |
if (eol != eof) { |
| 4707 |
offset = eol - objPtr->bytes; |
| 4708 |
dst = dstEnd; |
| 4709 |
if (FilterInputBytes(chanPtr, &gs) != 0) { |
| 4710 |
goto restore; |
| 4711 |
} |
| 4712 |
dstEnd = dst + gs.bytesWrote; |
| 4713 |
eol = objPtr->bytes + offset; |
| 4714 |
} |
| 4715 |
if (eol >= dstEnd) { |
| 4716 |
skip = 0; |
| 4717 |
goto gotEOL; |
| 4718 |
} |
| 4719 |
} |
| 4720 |
if (*eol == '\n') { |
| 4721 |
eol--; |
| 4722 |
skip = 2; |
| 4723 |
goto gotEOL; |
| 4724 |
} |
| 4725 |
} |
| 4726 |
} |
| 4727 |
break; |
| 4728 |
case TCL_TRANSLATE_AUTO: |
| 4729 |
eol = dst; |
| 4730 |
skip = 1; |
| 4731 |
if (GotFlag(statePtr, INPUT_SAW_CR)) { |
| 4732 |
if ((eol < dstEnd) && (*eol == '\n')) { |
| 4733 |
/* |
| 4734 |
* Skip the raw bytes that make up the '\n'. |
| 4735 |
*/ |
| 4736 |
|
| 4737 |
int rawRead; |
| 4738 |
char tmp[TCL_UTF_MAX]; |
| 4739 |
|
| 4740 |
bufPtr = gs.bufPtr; |
| 4741 |
Tcl_ExternalToUtf(NULL, gs.encoding, RemovePoint(bufPtr), |
| 4742 |
gs.rawRead, statePtr->inputEncodingFlags |
| 4743 |
| TCL_ENCODING_NO_TERMINATE, &gs.state, tmp, |
| 4744 |
sizeof(tmp), &rawRead, NULL, NULL); |
| 4745 |
bufPtr->nextRemoved += rawRead; |
| 4746 |
gs.rawRead -= rawRead; |
| 4747 |
gs.bytesWrote--; |
| 4748 |
gs.charsWrote--; |
| 4749 |
memmove(dst, dst + 1, dstEnd - dst); |
| 4750 |
dstEnd--; |
| 4751 |
} |
| 4752 |
} |
| 4753 |
for (eol = dst; eol < dstEnd; eol++) { |
| 4754 |
if (*eol == '\r') { |
| 4755 |
eol++; |
| 4756 |
if (eol == dstEnd) { |
| 4757 |
/* |
| 4758 |
* If buffer ended on \r, peek ahead to see if a \n is |
| 4759 |
* available, unless EOF char was found already. |
| 4760 |
*/ |
| 4761 |
|
| 4762 |
if (eol != eof) { |
| 4763 |
int offset; |
| 4764 |
|
| 4765 |
offset = eol - objPtr->bytes; |
| 4766 |
dst = dstEnd; |
| 4767 |
PeekAhead(chanPtr, &dstEnd, &gs); |
| 4768 |
eol = objPtr->bytes + offset; |
| 4769 |
} |
| 4770 |
|
| 4771 |
if (eol >= dstEnd) { |
| 4772 |
eol--; |
| 4773 |
SetFlag(statePtr, INPUT_SAW_CR); |
| 4774 |
goto gotEOL; |
| 4775 |
} |
| 4776 |
} |
| 4777 |
if (*eol == '\n') { |
| 4778 |
skip++; |
| 4779 |
} |
| 4780 |
eol--; |
| 4781 |
ResetFlag(statePtr, INPUT_SAW_CR); |
| 4782 |
goto gotEOL; |
| 4783 |
} else if (*eol == '\n') { |
| 4784 |
ResetFlag(statePtr, INPUT_SAW_CR); |
| 4785 |
goto gotEOL; |
| 4786 |
} |
| 4787 |
} |
| 4788 |
} |
| 4789 |
if (eof != NULL) { |
| 4790 |
/* |
| 4791 |
* EOF character was seen. On EOF, leave current file position |
| 4792 |
* pointing at the EOF character, but don't store the EOF |
| 4793 |
* character in the output string. |
| 4794 |
*/ |
| 4795 |
|
| 4796 |
dstEnd = eof; |
| 4797 |
SetFlag(statePtr, CHANNEL_EOF | CHANNEL_STICKY_EOF); |
| 4798 |
statePtr->inputEncodingFlags |= TCL_ENCODING_END; |
| 4799 |
ResetFlag(statePtr, CHANNEL_BLOCKED|INPUT_SAW_CR); |
| 4800 |
} |
| 4801 |
if (GotFlag(statePtr, CHANNEL_EOF)) { |
| 4802 |
skip = 0; |
| 4803 |
eol = dstEnd; |
| 4804 |
if (eol == objPtr->bytes + oldLength) { |
| 4805 |
/* |
| 4806 |
* If we didn't append any bytes before encountering EOF, |
| 4807 |
* caller needs to see -1. |
| 4808 |
*/ |
| 4809 |
|
| 4810 |
Tcl_SetObjLength(objPtr, oldLength); |
| 4811 |
CommonGetsCleanup(chanPtr); |
| 4812 |
copiedTotal = -1; |
| 4813 |
ResetFlag(statePtr, CHANNEL_BLOCKED|INPUT_SAW_CR); |
| 4814 |
goto done; |
| 4815 |
} |
| 4816 |
goto gotEOL; |
| 4817 |
} |
| 4818 |
dst = dstEnd; |
| 4819 |
} |
| 4820 |
|
| 4821 |
/* |
| 4822 |
* Found EOL or EOF, but the output buffer may now contain too many UTF-8 |
| 4823 |
* characters. We need to know how many raw bytes correspond to the number |
| 4824 |
* of UTF-8 characters we want, plus how many raw bytes correspond to the |
| 4825 |
* character(s) making up EOL (if any), so we can remove the correct |
| 4826 |
* number of bytes from the channel buffer. |
| 4827 |
*/ |
| 4828 |
|
| 4829 |
gotEOL: |
| 4830 |
/* |
| 4831 |
* Regenerate the top channel, in case it was changed due to |
| 4832 |
* self-modifying reflected transforms. |
| 4833 |
*/ |
| 4834 |
|
| 4835 |
if (chanPtr != statePtr->topChanPtr) { |
| 4836 |
TclChannelRelease((Tcl_Channel)chanPtr); |
| 4837 |
chanPtr = statePtr->topChanPtr; |
| 4838 |
TclChannelPreserve((Tcl_Channel)chanPtr); |
| 4839 |
} |
| 4840 |
|
| 4841 |
bufPtr = gs.bufPtr; |
| 4842 |
if (bufPtr == NULL) { |
| 4843 |
Tcl_Panic("Tcl_GetsObj: gotEOL reached with bufPtr==NULL"); |
| 4844 |
} |
| 4845 |
statePtr->inputEncodingState = gs.state; |
| 4846 |
Tcl_ExternalToUtf(NULL, gs.encoding, RemovePoint(bufPtr), gs.rawRead, |
| 4847 |
statePtr->inputEncodingFlags | TCL_ENCODING_NO_TERMINATE, |
| 4848 |
&statePtr->inputEncodingState, dst, |
| 4849 |
eol - dst + skip + TCL_UTF_MAX - 1, &gs.rawRead, NULL, |
| 4850 |
&gs.charsWrote); |
| 4851 |
bufPtr->nextRemoved += gs.rawRead; |
| 4852 |
|
| 4853 |
/* |
| 4854 |
* Recycle all the emptied buffers. |
| 4855 |
*/ |
| 4856 |
|
| 4857 |
Tcl_SetObjLength(objPtr, eol - objPtr->bytes); |
| 4858 |
CommonGetsCleanup(chanPtr); |
| 4859 |
ResetFlag(statePtr, CHANNEL_BLOCKED); |
| 4860 |
copiedTotal = gs.totalChars + gs.charsWrote - skip; |
| 4861 |
goto done; |
| 4862 |
|
| 4863 |
/* |
| 4864 |
* Couldn't get a complete line. This only happens if we get a error |
| 4865 |
* reading from the channel or we are non-blocking and there wasn't an EOL |
| 4866 |
* or EOF in the data available. |
| 4867 |
*/ |
| 4868 |
|
| 4869 |
restore: |
| 4870 |
/* |
| 4871 |
* Regenerate the top channel, in case it was changed due to |
| 4872 |
* self-modifying reflected transforms. |
| 4873 |
*/ |
| 4874 |
if (chanPtr != statePtr->topChanPtr) { |
| 4875 |
TclChannelRelease((Tcl_Channel)chanPtr); |
| 4876 |
chanPtr = statePtr->topChanPtr; |
| 4877 |
TclChannelPreserve((Tcl_Channel)chanPtr); |
| 4878 |
} |
| 4879 |
bufPtr = statePtr->inQueueHead; |
| 4880 |
if (bufPtr != NULL) { |
| 4881 |
bufPtr->nextRemoved = oldRemoved; |
| 4882 |
bufPtr = bufPtr->nextPtr; |
| 4883 |
} |
| 4884 |
|
| 4885 |
for ( ; bufPtr != NULL; bufPtr = bufPtr->nextPtr) { |
| 4886 |
bufPtr->nextRemoved = BUFFER_PADDING; |
| 4887 |
} |
| 4888 |
CommonGetsCleanup(chanPtr); |
| 4889 |
|
| 4890 |
statePtr->inputEncodingState = oldState; |
| 4891 |
statePtr->inputEncodingFlags = oldFlags; |
| 4892 |
Tcl_SetObjLength(objPtr, oldLength); |
| 4893 |
|
| 4894 |
/* |
| 4895 |
* We didn't get a complete line so we need to indicate to UpdateInterest |
| 4896 |
* that the gets blocked. It will wait for more data instead of firing a |
| 4897 |
* timer, avoiding a busy wait. This is where we are assuming that the |
| 4898 |
* next operation is a gets. No more file events will be delivered on this |
| 4899 |
* channel until new data arrives or some operation is performed on the |
| 4900 |
* channel (e.g. gets, read, fconfigure) that changes the blocking state. |
| 4901 |
* Note that this means a file event will not be delivered even though a |
| 4902 |
* read would be able to consume the buffered data. |
| 4903 |
*/ |
| 4904 |
|
| 4905 |
SetFlag(statePtr, CHANNEL_NEED_MORE_DATA); |
| 4906 |
copiedTotal = -1; |
| 4907 |
|
| 4908 |
/* |
| 4909 |
* Update the notifier state so we don't block while there is still data |
| 4910 |
* in the buffers. |
| 4911 |
*/ |
| 4912 |
|
| 4913 |
done: |
| 4914 |
assert(!GotFlag(statePtr, CHANNEL_EOF) |
| 4915 |
|| GotFlag(statePtr, CHANNEL_STICKY_EOF) |
| 4916 |
|| Tcl_InputBuffered((Tcl_Channel)chanPtr) == 0); |
| 4917 |
assert(!(GotFlag(statePtr, CHANNEL_EOF|CHANNEL_BLOCKED) |
| 4918 |
== (CHANNEL_EOF|CHANNEL_BLOCKED))); |
| 4919 |
|
| 4920 |
/* |
| 4921 |
* Regenerate the top channel, in case it was changed due to |
| 4922 |
* self-modifying reflected transforms. |
| 4923 |
*/ |
| 4924 |
|
| 4925 |
if (chanPtr != statePtr->topChanPtr) { |
| 4926 |
TclChannelRelease((Tcl_Channel)chanPtr); |
| 4927 |
chanPtr = statePtr->topChanPtr; |
| 4928 |
TclChannelPreserve((Tcl_Channel)chanPtr); |
| 4929 |
} |
| 4930 |
UpdateInterest(chanPtr); |
| 4931 |
TclChannelRelease((Tcl_Channel)chanPtr); |
| 4932 |
return copiedTotal; |
| 4933 |
} |
| 4934 |
|
| 4935 |
/* |
| 4936 |
*--------------------------------------------------------------------------- |
| 4937 |
* |
| 4938 |
* TclGetsObjBinary -- |
| 4939 |
* |
| 4940 |
* A variation of Tcl_GetsObj that works directly on the buffers until |
| 4941 |
* end-of-line or end-of-file has been seen. Bytes read from the input |
| 4942 |
* channel return as a ByteArray obj. |
| 4943 |
* |
| 4944 |
* WARNING! The notion of "binary" used here is different from notions |
| 4945 |
* of "binary" used in other places. In particular, this "binary" routine |
| 4946 |
* may be called when an -eofchar is set on the channel. |
| 4947 |
* |
| 4948 |
* Results: |
| 4949 |
* Number of characters accumulated in the object or -1 if error, |
| 4950 |
* blocked, or EOF. If -1, use Tcl_GetErrno() to retrieve the POSIX error |
| 4951 |
* code for the error or condition that occurred. |
| 4952 |
* |
| 4953 |
* Side effects: |
| 4954 |
* Consumes input from the channel. |
| 4955 |
* |
| 4956 |
* On reading EOF, leave channel pointing at EOF char. On reading EOL, |
| 4957 |
* leave channel pointing after EOL, but don't return EOL in dst buffer. |
| 4958 |
* |
| 4959 |
*--------------------------------------------------------------------------- |
| 4960 |
*/ |
| 4961 |
|
| 4962 |
static int |
| 4963 |
TclGetsObjBinary( |
| 4964 |
Tcl_Channel chan, /* Channel from which to read. */ |
| 4965 |
Tcl_Obj *objPtr) /* The line read will be appended to this |
| 4966 |
* object as UTF-8 characters. */ |
| 4967 |
{ |
| 4968 |
Channel *chanPtr = (Channel *) chan; |
| 4969 |
ChannelState *statePtr = chanPtr->state; |
| 4970 |
/* State info for channel */ |
| 4971 |
ChannelBuffer *bufPtr; |
| 4972 |
int inEofChar, skip, copiedTotal, oldFlags, oldRemoved; |
| 4973 |
int rawLen, byteLen, oldLength; |
| 4974 |
int eolChar; |
| 4975 |
unsigned char *dst, *dstEnd, *eol, *eof, *byteArray; |
| 4976 |
|
| 4977 |
/* |
| 4978 |
* This operation should occur at the top of a channel stack. |
| 4979 |
*/ |
| 4980 |
|
| 4981 |
chanPtr = statePtr->topChanPtr; |
| 4982 |
TclChannelPreserve((Tcl_Channel)chanPtr); |
| 4983 |
|
| 4984 |
bufPtr = statePtr->inQueueHead; |
| 4985 |
|
| 4986 |
/* |
| 4987 |
* Preserved so we can restore the channel's state in case we don't find a |
| 4988 |
* newline in the available input. |
| 4989 |
*/ |
| 4990 |
|
| 4991 |
byteArray = Tcl_GetByteArrayFromObj(objPtr, &byteLen); |
| 4992 |
oldFlags = statePtr->inputEncodingFlags; |
| 4993 |
oldRemoved = BUFFER_PADDING; |
| 4994 |
oldLength = byteLen; |
| 4995 |
if (bufPtr != NULL) { |
| 4996 |
oldRemoved = bufPtr->nextRemoved; |
| 4997 |
} |
| 4998 |
|
| 4999 |
rawLen = 0; |
| 5000 |
skip = 0; |
| 5001 |
eof = NULL; |
| 5002 |
inEofChar = statePtr->inEofChar; |
| 5003 |
|
| 5004 |
/* |
| 5005 |
* Only handle TCL_TRANSLATE_LF and TCL_TRANSLATE_CR. |
| 5006 |
*/ |
| 5007 |
|
| 5008 |
eolChar = (statePtr->inputTranslation == TCL_TRANSLATE_LF) ? '\n' : '\r'; |
| 5009 |
|
| 5010 |
ResetFlag(statePtr, CHANNEL_BLOCKED); |
| 5011 |
while (1) { |
| 5012 |
/* |
| 5013 |
* Subtract the number of bytes that were removed from channel buffer |
| 5014 |
* during last call. |
| 5015 |
*/ |
| 5016 |
|
| 5017 |
if (bufPtr != NULL) { |
| 5018 |
bufPtr->nextRemoved += rawLen; |
| 5019 |
if (!IsBufferReady(bufPtr)) { |
| 5020 |
bufPtr = bufPtr->nextPtr; |
| 5021 |
} |
| 5022 |
} |
| 5023 |
|
| 5024 |
if ((bufPtr == NULL) || (bufPtr->nextAdded == BUFFER_PADDING)) { |
| 5025 |
/* |
| 5026 |
* All channel buffers were exhausted and the caller still hasn't |
| 5027 |
* seen EOL. Need to read more bytes from the channel device. Side |
| 5028 |
* effect is to allocate another channel buffer. |
| 5029 |
*/ |
| 5030 |
|
| 5031 |
if (GetInput(chanPtr) != 0) { |
| 5032 |
goto restore; |
| 5033 |
} |
| 5034 |
bufPtr = statePtr->inQueueTail; |
| 5035 |
if (bufPtr == NULL) { |
| 5036 |
goto restore; |
| 5037 |
} |
| 5038 |
} else { |
| 5039 |
/* |
| 5040 |
* Incoming CHANNEL_STICKY_EOF is filtered out on entry. A new |
| 5041 |
* CHANNEL_STICKY_EOF set in this routine leads to return before |
| 5042 |
* coming back here. When we are not dealing with |
| 5043 |
* CHANNEL_STICKY_EOF, a CHANNEL_EOF implies an empty buffer. |
| 5044 |
* Here the buffer is non-empty so we know we're a non-EOF. |
| 5045 |
*/ |
| 5046 |
|
| 5047 |
assert(!GotFlag(statePtr, CHANNEL_STICKY_EOF)); |
| 5048 |
assert(!GotFlag(statePtr, CHANNEL_EOF)); |
| 5049 |
} |
| 5050 |
|
| 5051 |
dst = (unsigned char *) RemovePoint(bufPtr); |
| 5052 |
dstEnd = dst + BytesLeft(bufPtr); |
| 5053 |
|
| 5054 |
/* |
| 5055 |
* Remember if EOF char is seen, then look for EOL anyhow, because the |
| 5056 |
* EOL might be before the EOF char. |
| 5057 |
* XXX - in the binary case, consider coincident search for eol/eof. |
| 5058 |
*/ |
| 5059 |
|
| 5060 |
if (inEofChar != '\0') { |
| 5061 |
for (eol = dst; eol < dstEnd; eol++) { |
| 5062 |
if (*eol == inEofChar) { |
| 5063 |
dstEnd = eol; |
| 5064 |
eof = eol; |
| 5065 |
break; |
| 5066 |
} |
| 5067 |
} |
| 5068 |
} |
| 5069 |
|
| 5070 |
/* |
| 5071 |
* On EOL, leave current file position pointing after the EOL, but |
| 5072 |
* don't store the EOL in the output string. |
| 5073 |
*/ |
| 5074 |
|
| 5075 |
for (eol = dst; eol < dstEnd; eol++) { |
| 5076 |
if (*eol == eolChar) { |
| 5077 |
skip = 1; |
| 5078 |
goto gotEOL; |
| 5079 |
} |
| 5080 |
} |
| 5081 |
if (eof != NULL) { |
| 5082 |
/* |
| 5083 |
* EOF character was seen. On EOF, leave current file position |
| 5084 |
* pointing at the EOF character, but don't store the EOF |
| 5085 |
* character in the output string. |
| 5086 |
*/ |
| 5087 |
|
| 5088 |
SetFlag(statePtr, CHANNEL_EOF | CHANNEL_STICKY_EOF); |
| 5089 |
statePtr->inputEncodingFlags |= TCL_ENCODING_END; |
| 5090 |
ResetFlag(statePtr, CHANNEL_BLOCKED|INPUT_SAW_CR); |
| 5091 |
} |
| 5092 |
if (GotFlag(statePtr, CHANNEL_EOF)) { |
| 5093 |
skip = 0; |
| 5094 |
eol = dstEnd; |
| 5095 |
if ((dst == dstEnd) && (byteLen == oldLength)) { |
| 5096 |
/* |
| 5097 |
* If we didn't append any bytes before encountering EOF, |
| 5098 |
* caller needs to see -1. |
| 5099 |
*/ |
| 5100 |
|
| 5101 |
byteArray = Tcl_SetByteArrayLength(objPtr, oldLength); |
| 5102 |
CommonGetsCleanup(chanPtr); |
| 5103 |
copiedTotal = -1; |
| 5104 |
ResetFlag(statePtr, CHANNEL_BLOCKED); |
| 5105 |
goto done; |
| 5106 |
} |
| 5107 |
goto gotEOL; |
| 5108 |
} |
| 5109 |
if (GotFlag(statePtr, CHANNEL_BLOCKED|CHANNEL_NONBLOCKING) |
| 5110 |
== (CHANNEL_BLOCKED|CHANNEL_NONBLOCKING)) { |
| 5111 |
goto restore; |
| 5112 |
} |
| 5113 |
|
| 5114 |
/* |
| 5115 |
* Copy bytes from the channel buffer to the ByteArray. This may |
| 5116 |
* realloc space, so keep track of result. |
| 5117 |
*/ |
| 5118 |
|
| 5119 |
rawLen = dstEnd - dst; |
| 5120 |
byteArray = Tcl_SetByteArrayLength(objPtr, byteLen + rawLen); |
| 5121 |
memcpy(byteArray + byteLen, dst, rawLen); |
| 5122 |
byteLen += rawLen; |
| 5123 |
} |
| 5124 |
|
| 5125 |
/* |
| 5126 |
* Found EOL or EOF, but the output buffer may now contain too many bytes. |
| 5127 |
* We need to know how many bytes correspond to the number we want, so we |
| 5128 |
* can remove the correct number of bytes from the channel buffer. |
| 5129 |
*/ |
| 5130 |
|
| 5131 |
gotEOL: |
| 5132 |
if (bufPtr == NULL) { |
| 5133 |
Tcl_Panic("TclGetsObjBinary: gotEOL reached with bufPtr==NULL"); |
| 5134 |
} |
| 5135 |
|
| 5136 |
rawLen = eol - dst; |
| 5137 |
byteArray = Tcl_SetByteArrayLength(objPtr, byteLen + rawLen); |
| 5138 |
memcpy(byteArray + byteLen, dst, rawLen); |
| 5139 |
byteLen += rawLen; |
| 5140 |
bufPtr->nextRemoved += rawLen + skip; |
| 5141 |
|
| 5142 |
/* |
| 5143 |
* Convert the buffer if there was an encoding. |
| 5144 |
* XXX - unimplemented. |
| 5145 |
*/ |
| 5146 |
|
| 5147 |
if (statePtr->encoding != NULL) { |
| 5148 |
} |
| 5149 |
|
| 5150 |
/* |
| 5151 |
* Recycle all the emptied buffers. |
| 5152 |
*/ |
| 5153 |
|
| 5154 |
CommonGetsCleanup(chanPtr); |
| 5155 |
ResetFlag(statePtr, CHANNEL_BLOCKED); |
| 5156 |
copiedTotal = byteLen; |
| 5157 |
goto done; |
| 5158 |
|
| 5159 |
/* |
| 5160 |
* Couldn't get a complete line. This only happens if we get a error |
| 5161 |
* reading from the channel or we are non-blocking and there wasn't an EOL |
| 5162 |
* or EOF in the data available. |
| 5163 |
*/ |
| 5164 |
|
| 5165 |
restore: |
| 5166 |
bufPtr = statePtr->inQueueHead; |
| 5167 |
if (bufPtr) { |
| 5168 |
bufPtr->nextRemoved = oldRemoved; |
| 5169 |
bufPtr = bufPtr->nextPtr; |
| 5170 |
} |
| 5171 |
|
| 5172 |
for ( ; bufPtr != NULL; bufPtr = bufPtr->nextPtr) { |
| 5173 |
bufPtr->nextRemoved = BUFFER_PADDING; |
| 5174 |
} |
| 5175 |
CommonGetsCleanup(chanPtr); |
| 5176 |
|
| 5177 |
statePtr->inputEncodingFlags = oldFlags; |
| 5178 |
byteArray = Tcl_SetByteArrayLength(objPtr, oldLength); |
| 5179 |
|
| 5180 |
/* |
| 5181 |
* We didn't get a complete line so we need to indicate to UpdateInterest |
| 5182 |
* that the gets blocked. It will wait for more data instead of firing a |
| 5183 |
* timer, avoiding a busy wait. This is where we are assuming that the |
| 5184 |
* next operation is a gets. No more file events will be delivered on this |
| 5185 |
* channel until new data arrives or some operation is performed on the |
| 5186 |
* channel (e.g. gets, read, fconfigure) that changes the blocking state. |
| 5187 |
* Note that this means a file event will not be delivered even though a |
| 5188 |
* read would be able to consume the buffered data. |
| 5189 |
*/ |
| 5190 |
|
| 5191 |
SetFlag(statePtr, CHANNEL_NEED_MORE_DATA); |
| 5192 |
copiedTotal = -1; |
| 5193 |
|
| 5194 |
/* |
| 5195 |
* Update the notifier state so we don't block while there is still data |
| 5196 |
* in the buffers. |
| 5197 |
*/ |
| 5198 |
|
| 5199 |
done: |
| 5200 |
assert(!GotFlag(statePtr, CHANNEL_EOF) |
| 5201 |
|| GotFlag(statePtr, CHANNEL_STICKY_EOF) |
| 5202 |
|| Tcl_InputBuffered((Tcl_Channel)chanPtr) == 0); |
| 5203 |
assert(!(GotFlag(statePtr, CHANNEL_EOF|CHANNEL_BLOCKED) |
| 5204 |
== (CHANNEL_EOF|CHANNEL_BLOCKED))); |
| 5205 |
UpdateInterest(chanPtr); |
| 5206 |
TclChannelRelease((Tcl_Channel)chanPtr); |
| 5207 |
return copiedTotal; |
| 5208 |
} |
| 5209 |
|
| 5210 |
/* |
| 5211 |
*--------------------------------------------------------------------------- |
| 5212 |
* |
| 5213 |
* FreeBinaryEncoding -- |
| 5214 |
* |
| 5215 |
* Frees any "iso8859-1" Tcl_Encoding created by [gets] on a binary |
| 5216 |
* channel in a thread as part of that thread's finalization. |
| 5217 |
* |
| 5218 |
* Results: |
| 5219 |
* None. |
| 5220 |
* |
| 5221 |
*--------------------------------------------------------------------------- |
| 5222 |
*/ |
| 5223 |
|
| 5224 |
static void |
| 5225 |
FreeBinaryEncoding( |
| 5226 |
void *dummy) /* Not used */ |
| 5227 |
{ |
| 5228 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 5229 |
(void)dummy; |
| 5230 |
|
| 5231 |
if (tsdPtr->binaryEncoding != NULL) { |
| 5232 |
Tcl_FreeEncoding(tsdPtr->binaryEncoding); |
| 5233 |
tsdPtr->binaryEncoding = NULL; |
| 5234 |
} |
| 5235 |
} |
| 5236 |
|
| 5237 |
static Tcl_Encoding |
| 5238 |
GetBinaryEncoding(void) |
| 5239 |
{ |
| 5240 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 5241 |
|
| 5242 |
if (tsdPtr->binaryEncoding == NULL) { |
| 5243 |
tsdPtr->binaryEncoding = Tcl_GetEncoding(NULL, "iso8859-1"); |
| 5244 |
Tcl_CreateThreadExitHandler(FreeBinaryEncoding, NULL); |
| 5245 |
} |
| 5246 |
if (tsdPtr->binaryEncoding == NULL) { |
| 5247 |
Tcl_Panic("binary encoding is not available"); |
| 5248 |
} |
| 5249 |
return tsdPtr->binaryEncoding; |
| 5250 |
} |
| 5251 |
|
| 5252 |
/* |
| 5253 |
*--------------------------------------------------------------------------- |
| 5254 |
* |
| 5255 |
* FilterInputBytes -- |
| 5256 |
* |
| 5257 |
* Helper function for Tcl_GetsObj. Produces UTF-8 characters from raw |
| 5258 |
* bytes read from the channel. |
| 5259 |
* |
| 5260 |
* Consumes available bytes from channel buffers. When channel buffers |
| 5261 |
* are exhausted, reads more bytes from channel device into a new channel |
| 5262 |
* buffer. It is the caller's responsibility to free the channel buffers |
| 5263 |
* that have been exhausted. |
| 5264 |
* |
| 5265 |
* Results: |
| 5266 |
* The return value is -1 if there was an error reading from the channel, |
| 5267 |
* 0 otherwise. |
| 5268 |
* |
| 5269 |
* Side effects: |
| 5270 |
* Status object keeps track of how much data from channel buffers has |
| 5271 |
* been consumed and where UTF-8 bytes should be stored. |
| 5272 |
* |
| 5273 |
*--------------------------------------------------------------------------- |
| 5274 |
*/ |
| 5275 |
|
| 5276 |
static int |
| 5277 |
FilterInputBytes( |
| 5278 |
Channel *chanPtr, /* Channel to read. */ |
| 5279 |
GetsState *gsPtr) /* Current state of gets operation. */ |
| 5280 |
{ |
| 5281 |
ChannelState *statePtr = chanPtr->state; |
| 5282 |
/* State info for channel */ |
| 5283 |
ChannelBuffer *bufPtr; |
| 5284 |
char *raw, *dst; |
| 5285 |
int offset, toRead, dstNeeded, spaceLeft, result, rawLen; |
| 5286 |
Tcl_Obj *objPtr; |
| 5287 |
#define ENCODING_LINESIZE 20 /* Lower bound on how many bytes to convert at |
| 5288 |
* a time. Since we don't know a priori how |
| 5289 |
* many bytes of storage this many source |
| 5290 |
* bytes will use, we actually need at least |
| 5291 |
* ENCODING_LINESIZE * TCL_MAX_UTF bytes of |
| 5292 |
* room. */ |
| 5293 |
|
| 5294 |
objPtr = gsPtr->objPtr; |
| 5295 |
|
| 5296 |
/* |
| 5297 |
* Subtract the number of bytes that were removed from channel buffer |
| 5298 |
* during last call. |
| 5299 |
*/ |
| 5300 |
|
| 5301 |
bufPtr = gsPtr->bufPtr; |
| 5302 |
if (bufPtr != NULL) { |
| 5303 |
bufPtr->nextRemoved += gsPtr->rawRead; |
| 5304 |
if (!IsBufferReady(bufPtr)) { |
| 5305 |
bufPtr = bufPtr->nextPtr; |
| 5306 |
} |
| 5307 |
} |
| 5308 |
gsPtr->totalChars += gsPtr->charsWrote; |
| 5309 |
|
| 5310 |
if ((bufPtr == NULL) || (bufPtr->nextAdded == BUFFER_PADDING)) { |
| 5311 |
/* |
| 5312 |
* All channel buffers were exhausted and the caller still hasn't seen |
| 5313 |
* EOL. Need to read more bytes from the channel device. Side effect |
| 5314 |
* is to allocate another channel buffer. |
| 5315 |
*/ |
| 5316 |
|
| 5317 |
read: |
| 5318 |
if (GotFlag(statePtr, CHANNEL_NONBLOCKING|CHANNEL_BLOCKED) |
| 5319 |
== (CHANNEL_NONBLOCKING|CHANNEL_BLOCKED)) { |
| 5320 |
gsPtr->charsWrote = 0; |
| 5321 |
gsPtr->rawRead = 0; |
| 5322 |
return -1; |
| 5323 |
} |
| 5324 |
if (GetInput(chanPtr) != 0) { |
| 5325 |
gsPtr->charsWrote = 0; |
| 5326 |
gsPtr->rawRead = 0; |
| 5327 |
return -1; |
| 5328 |
} |
| 5329 |
bufPtr = statePtr->inQueueTail; |
| 5330 |
gsPtr->bufPtr = bufPtr; |
| 5331 |
if (bufPtr == NULL) { |
| 5332 |
gsPtr->charsWrote = 0; |
| 5333 |
gsPtr->rawRead = 0; |
| 5334 |
return -1; |
| 5335 |
} |
| 5336 |
} else { |
| 5337 |
/* |
| 5338 |
* Incoming CHANNEL_STICKY_EOF is filtered out on entry. A new |
| 5339 |
* CHANNEL_STICKY_EOF set in this routine leads to return before |
| 5340 |
* coming back here. When we are not dealing with CHANNEL_STICKY_EOF, |
| 5341 |
* a CHANNEL_EOF implies an empty buffer. Here the buffer is |
| 5342 |
* non-empty so we know we're a non-EOF. |
| 5343 |
*/ |
| 5344 |
|
| 5345 |
assert(!GotFlag(statePtr, CHANNEL_STICKY_EOF)); |
| 5346 |
assert(!GotFlag(statePtr, CHANNEL_EOF)); |
| 5347 |
} |
| 5348 |
|
| 5349 |
/* |
| 5350 |
* Convert some of the bytes from the channel buffer to UTF-8. Space in |
| 5351 |
* objPtr's string rep is used to hold the UTF-8 characters. Grow the |
| 5352 |
* string rep if we need more space. |
| 5353 |
*/ |
| 5354 |
|
| 5355 |
raw = RemovePoint(bufPtr); |
| 5356 |
rawLen = BytesLeft(bufPtr); |
| 5357 |
|
| 5358 |
dst = *gsPtr->dstPtr; |
| 5359 |
offset = dst - objPtr->bytes; |
| 5360 |
toRead = ENCODING_LINESIZE; |
| 5361 |
if (toRead > rawLen) { |
| 5362 |
toRead = rawLen; |
| 5363 |
} |
| 5364 |
dstNeeded = toRead * TCL_UTF_MAX; |
| 5365 |
spaceLeft = objPtr->length - offset; |
| 5366 |
if (dstNeeded > spaceLeft) { |
| 5367 |
int length = offset + ((offset < dstNeeded) ? dstNeeded : offset); |
| 5368 |
|
| 5369 |
if (Tcl_AttemptSetObjLength(objPtr, length) == 0) { |
| 5370 |
length = offset + dstNeeded; |
| 5371 |
if (Tcl_AttemptSetObjLength(objPtr, length) == 0) { |
| 5372 |
dstNeeded = TCL_UTF_MAX - 1 + toRead; |
| 5373 |
length = offset + dstNeeded; |
| 5374 |
Tcl_SetObjLength(objPtr, length); |
| 5375 |
} |
| 5376 |
} |
| 5377 |
spaceLeft = length - offset; |
| 5378 |
dst = objPtr->bytes + offset; |
| 5379 |
*gsPtr->dstPtr = dst; |
| 5380 |
} |
| 5381 |
gsPtr->state = statePtr->inputEncodingState; |
| 5382 |
|
| 5383 |
result = Tcl_ExternalToUtf(NULL, gsPtr->encoding, raw, rawLen, |
| 5384 |
statePtr->inputEncodingFlags | TCL_ENCODING_NO_TERMINATE, |
| 5385 |
&statePtr->inputEncodingState, dst, spaceLeft, &gsPtr->rawRead, |
| 5386 |
&gsPtr->bytesWrote, &gsPtr->charsWrote); |
| 5387 |
|
| 5388 |
/* |
| 5389 |
* Make sure that if we go through 'gets', that we reset the |
| 5390 |
* TCL_ENCODING_START flag still. [Bug #523988] |
| 5391 |
*/ |
| 5392 |
|
| 5393 |
statePtr->inputEncodingFlags &= ~TCL_ENCODING_START; |
| 5394 |
|
| 5395 |
if (result == TCL_CONVERT_MULTIBYTE) { |
| 5396 |
/* |
| 5397 |
* The last few bytes in this channel buffer were the start of a |
| 5398 |
* multibyte sequence. If this buffer was full, then move them to the |
| 5399 |
* next buffer so the bytes will be contiguous. |
| 5400 |
*/ |
| 5401 |
|
| 5402 |
ChannelBuffer *nextPtr; |
| 5403 |
int extra; |
| 5404 |
|
| 5405 |
nextPtr = bufPtr->nextPtr; |
| 5406 |
if (!IsBufferFull(bufPtr)) { |
| 5407 |
if (gsPtr->rawRead > 0) { |
| 5408 |
/* |
| 5409 |
* Some raw bytes were converted to UTF-8. Fall through, |
| 5410 |
* returning those UTF-8 characters because a EOL might be |
| 5411 |
* present in them. |
| 5412 |
*/ |
| 5413 |
} else if (GotFlag(statePtr, CHANNEL_EOF)) { |
| 5414 |
/* |
| 5415 |
* There was a partial character followed by EOF on the |
| 5416 |
* device. Fall through, returning that nothing was found. |
| 5417 |
*/ |
| 5418 |
|
| 5419 |
bufPtr->nextRemoved = bufPtr->nextAdded; |
| 5420 |
} else { |
| 5421 |
/* |
| 5422 |
* There are no more cached raw bytes left. See if we can get |
| 5423 |
* some more, but avoid blocking on a non-blocking channel. |
| 5424 |
*/ |
| 5425 |
|
| 5426 |
goto read; |
| 5427 |
} |
| 5428 |
} else { |
| 5429 |
if (nextPtr == NULL) { |
| 5430 |
nextPtr = AllocChannelBuffer(statePtr->bufSize); |
| 5431 |
bufPtr->nextPtr = nextPtr; |
| 5432 |
statePtr->inQueueTail = nextPtr; |
| 5433 |
} |
| 5434 |
extra = rawLen - gsPtr->rawRead; |
| 5435 |
memcpy(nextPtr->buf + (BUFFER_PADDING - extra), |
| 5436 |
raw + gsPtr->rawRead, (size_t) extra); |
| 5437 |
nextPtr->nextRemoved -= extra; |
| 5438 |
bufPtr->nextAdded -= extra; |
| 5439 |
} |
| 5440 |
} |
| 5441 |
|
| 5442 |
gsPtr->bufPtr = bufPtr; |
| 5443 |
return 0; |
| 5444 |
} |
| 5445 |
|
| 5446 |
/* |
| 5447 |
*--------------------------------------------------------------------------- |
| 5448 |
* |
| 5449 |
* PeekAhead -- |
| 5450 |
* |
| 5451 |
* Helper function used by Tcl_GetsObj(). Called when we've seen a \r at |
| 5452 |
* the end of the UTF-8 string and want to look ahead one character to |
| 5453 |
* see if it is a \n. |
| 5454 |
* |
| 5455 |
* Results: |
| 5456 |
* *gsPtr->dstPtr is filled with a pointer to the start of the range of |
| 5457 |
* UTF-8 characters that were found by peeking and *dstEndPtr is filled |
| 5458 |
* with a pointer to the bytes just after the end of the range. |
| 5459 |
* |
| 5460 |
* Side effects: |
| 5461 |
* If no more raw bytes were available in one of the channel buffers, |
| 5462 |
* tries to perform a non-blocking read to get more bytes from the |
| 5463 |
* channel device. |
| 5464 |
* |
| 5465 |
*--------------------------------------------------------------------------- |
| 5466 |
*/ |
| 5467 |
|
| 5468 |
static void |
| 5469 |
PeekAhead( |
| 5470 |
Channel *chanPtr, /* The channel to read. */ |
| 5471 |
char **dstEndPtr, /* Filled with pointer to end of new range of |
| 5472 |
* UTF-8 characters. */ |
| 5473 |
GetsState *gsPtr) /* Current state of gets operation. */ |
| 5474 |
{ |
| 5475 |
ChannelState *statePtr = chanPtr->state; |
| 5476 |
/* State info for channel */ |
| 5477 |
ChannelBuffer *bufPtr; |
| 5478 |
Tcl_DriverBlockModeProc *blockModeProc; |
| 5479 |
int bytesLeft; |
| 5480 |
|
| 5481 |
bufPtr = gsPtr->bufPtr; |
| 5482 |
|
| 5483 |
/* |
| 5484 |
* If there's any more raw input that's still buffered, we'll peek into |
| 5485 |
* that. Otherwise, only get more data from the channel driver if it looks |
| 5486 |
* like there might actually be more data. The assumption is that if the |
| 5487 |
* channel buffer is filled right up to the end, then there might be more |
| 5488 |
* data to read. |
| 5489 |
*/ |
| 5490 |
|
| 5491 |
blockModeProc = NULL; |
| 5492 |
if (bufPtr->nextPtr == NULL) { |
| 5493 |
bytesLeft = BytesLeft(bufPtr) - gsPtr->rawRead; |
| 5494 |
if (bytesLeft == 0) { |
| 5495 |
if (!IsBufferFull(bufPtr)) { |
| 5496 |
/* |
| 5497 |
* Don't peek ahead if last read was short read. |
| 5498 |
*/ |
| 5499 |
|
| 5500 |
goto cleanup; |
| 5501 |
} |
| 5502 |
if (!GotFlag(statePtr, CHANNEL_NONBLOCKING)) { |
| 5503 |
blockModeProc = Tcl_ChannelBlockModeProc(chanPtr->typePtr); |
| 5504 |
if (blockModeProc == NULL) { |
| 5505 |
/* |
| 5506 |
* Don't peek ahead if cannot set non-blocking mode. |
| 5507 |
*/ |
| 5508 |
|
| 5509 |
goto cleanup; |
| 5510 |
} |
| 5511 |
StackSetBlockMode(chanPtr, TCL_MODE_NONBLOCKING); |
| 5512 |
} |
| 5513 |
} |
| 5514 |
} |
| 5515 |
if (FilterInputBytes(chanPtr, gsPtr) == 0) { |
| 5516 |
*dstEndPtr = *gsPtr->dstPtr + gsPtr->bytesWrote; |
| 5517 |
} |
| 5518 |
if (blockModeProc != NULL) { |
| 5519 |
StackSetBlockMode(chanPtr, TCL_MODE_BLOCKING); |
| 5520 |
} |
| 5521 |
return; |
| 5522 |
|
| 5523 |
cleanup: |
| 5524 |
bufPtr->nextRemoved += gsPtr->rawRead; |
| 5525 |
gsPtr->rawRead = 0; |
| 5526 |
gsPtr->totalChars += gsPtr->charsWrote; |
| 5527 |
gsPtr->bytesWrote = 0; |
| 5528 |
gsPtr->charsWrote = 0; |
| 5529 |
} |
| 5530 |
|
| 5531 |
/* |
| 5532 |
*--------------------------------------------------------------------------- |
| 5533 |
* |
| 5534 |
* CommonGetsCleanup -- |
| 5535 |
* |
| 5536 |
* Helper function for Tcl_GetsObj() to restore the channel after a |
| 5537 |
* "gets" operation. |
| 5538 |
* |
| 5539 |
* Results: |
| 5540 |
* None. |
| 5541 |
* |
| 5542 |
* Side effects: |
| 5543 |
* Encoding may be freed. |
| 5544 |
* |
| 5545 |
*--------------------------------------------------------------------------- |
| 5546 |
*/ |
| 5547 |
|
| 5548 |
static void |
| 5549 |
CommonGetsCleanup( |
| 5550 |
Channel *chanPtr) |
| 5551 |
{ |
| 5552 |
ChannelState *statePtr = chanPtr->state; |
| 5553 |
/* State info for channel */ |
| 5554 |
ChannelBuffer *bufPtr, *nextPtr; |
| 5555 |
|
| 5556 |
bufPtr = statePtr->inQueueHead; |
| 5557 |
for ( ; bufPtr != NULL; bufPtr = nextPtr) { |
| 5558 |
nextPtr = bufPtr->nextPtr; |
| 5559 |
if (IsBufferReady(bufPtr)) { |
| 5560 |
break; |
| 5561 |
} |
| 5562 |
RecycleBuffer(statePtr, bufPtr, 0); |
| 5563 |
} |
| 5564 |
statePtr->inQueueHead = bufPtr; |
| 5565 |
if (bufPtr == NULL) { |
| 5566 |
statePtr->inQueueTail = NULL; |
| 5567 |
} else { |
| 5568 |
/* |
| 5569 |
* If any multi-byte characters were split across channel buffer |
| 5570 |
* boundaries, the split-up bytes were moved to the next channel |
| 5571 |
* buffer by FilterInputBytes(). Move the bytes back to their original |
| 5572 |
* buffer because the caller could change the channel's encoding which |
| 5573 |
* could change the interpretation of whether those bytes really made |
| 5574 |
* up multi-byte characters after all. |
| 5575 |
*/ |
| 5576 |
|
| 5577 |
nextPtr = bufPtr->nextPtr; |
| 5578 |
for ( ; nextPtr != NULL; nextPtr = bufPtr->nextPtr) { |
| 5579 |
int extra; |
| 5580 |
|
| 5581 |
extra = SpaceLeft(bufPtr); |
| 5582 |
if (extra > 0) { |
| 5583 |
memcpy(InsertPoint(bufPtr), |
| 5584 |
nextPtr->buf + (BUFFER_PADDING - extra), |
| 5585 |
(size_t) extra); |
| 5586 |
bufPtr->nextAdded += extra; |
| 5587 |
nextPtr->nextRemoved = BUFFER_PADDING; |
| 5588 |
} |
| 5589 |
bufPtr = nextPtr; |
| 5590 |
} |
| 5591 |
} |
| 5592 |
} |
| 5593 |
|
| 5594 |
/* |
| 5595 |
*---------------------------------------------------------------------- |
| 5596 |
* |
| 5597 |
* Tcl_Read -- |
| 5598 |
* |
| 5599 |
* Reads a given number of bytes from a channel. EOL and EOF translation |
| 5600 |
* is done on the bytes being read, so the number of bytes consumed from |
| 5601 |
* the channel may not be equal to the number of bytes stored in the |
| 5602 |
* destination buffer. |
| 5603 |
* |
| 5604 |
* No encoding conversions are applied to the bytes being read. |
| 5605 |
* |
| 5606 |
* Results: |
| 5607 |
* The number of bytes read, or -1 on error. Use Tcl_GetErrno() to |
| 5608 |
* retrieve the error code for the error that occurred. |
| 5609 |
* |
| 5610 |
* Side effects: |
| 5611 |
* May cause input to be buffered. |
| 5612 |
* |
| 5613 |
*---------------------------------------------------------------------- |
| 5614 |
*/ |
| 5615 |
|
| 5616 |
int |
| 5617 |
Tcl_Read( |
| 5618 |
Tcl_Channel chan, /* The channel from which to read. */ |
| 5619 |
char *dst, /* Where to store input read. */ |
| 5620 |
int bytesToRead) /* Maximum number of bytes to read. */ |
| 5621 |
{ |
| 5622 |
Channel *chanPtr = (Channel *) chan; |
| 5623 |
ChannelState *statePtr = chanPtr->state; |
| 5624 |
/* State info for channel */ |
| 5625 |
|
| 5626 |
/* |
| 5627 |
* This operation should occur at the top of a channel stack. |
| 5628 |
*/ |
| 5629 |
|
| 5630 |
chanPtr = statePtr->topChanPtr; |
| 5631 |
|
| 5632 |
if (CheckChannelErrors(statePtr, TCL_READABLE) != 0) { |
| 5633 |
return -1; |
| 5634 |
} |
| 5635 |
|
| 5636 |
return DoRead(chanPtr, dst, bytesToRead, 0); |
| 5637 |
} |
| 5638 |
|
| 5639 |
/* |
| 5640 |
*---------------------------------------------------------------------- |
| 5641 |
* |
| 5642 |
* Tcl_ReadRaw -- |
| 5643 |
* |
| 5644 |
* Reads a given number of bytes from a channel. EOL and EOF translation |
| 5645 |
* is done on the bytes being read, so the number of bytes consumed from |
| 5646 |
* the channel may not be equal to the number of bytes stored in the |
| 5647 |
* destination buffer. |
| 5648 |
* |
| 5649 |
* No encoding conversions are applied to the bytes being read. |
| 5650 |
* |
| 5651 |
* Results: |
| 5652 |
* The number of bytes read, or -1 on error. Use Tcl_GetErrno() to |
| 5653 |
* retrieve the error code for the error that occurred. |
| 5654 |
* |
| 5655 |
* Side effects: |
| 5656 |
* May cause input to be buffered. |
| 5657 |
* |
| 5658 |
*---------------------------------------------------------------------- |
| 5659 |
*/ |
| 5660 |
|
| 5661 |
int |
| 5662 |
Tcl_ReadRaw( |
| 5663 |
Tcl_Channel chan, /* The channel from which to read. */ |
| 5664 |
char *readBuf, /* Where to store input read. */ |
| 5665 |
int bytesToRead) /* Maximum number of bytes to read. */ |
| 5666 |
{ |
| 5667 |
Channel *chanPtr = (Channel *) chan; |
| 5668 |
ChannelState *statePtr = chanPtr->state; |
| 5669 |
/* State info for channel */ |
| 5670 |
int copied = 0; |
| 5671 |
|
| 5672 |
assert(bytesToRead > 0); |
| 5673 |
if (CheckChannelErrors(statePtr, TCL_READABLE | CHANNEL_RAW_MODE) != 0) { |
| 5674 |
return -1; |
| 5675 |
} |
| 5676 |
|
| 5677 |
/* |
| 5678 |
* First read bytes from the push-back buffers. |
| 5679 |
*/ |
| 5680 |
|
| 5681 |
while (chanPtr->inQueueHead && bytesToRead > 0) { |
| 5682 |
ChannelBuffer *bufPtr = chanPtr->inQueueHead; |
| 5683 |
int bytesInBuffer = BytesLeft(bufPtr); |
| 5684 |
int toCopy = (bytesInBuffer < bytesToRead) ? bytesInBuffer |
| 5685 |
: bytesToRead; |
| 5686 |
|
| 5687 |
/* |
| 5688 |
* Copy the current chunk into the read buffer. |
| 5689 |
*/ |
| 5690 |
|
| 5691 |
memcpy(readBuf, RemovePoint(bufPtr), toCopy); |
| 5692 |
bufPtr->nextRemoved += toCopy; |
| 5693 |
copied += toCopy; |
| 5694 |
readBuf += toCopy; |
| 5695 |
bytesToRead -= toCopy; |
| 5696 |
|
| 5697 |
/* |
| 5698 |
* If the current buffer is empty recycle it. |
| 5699 |
*/ |
| 5700 |
|
| 5701 |
if (IsBufferEmpty(bufPtr)) { |
| 5702 |
chanPtr->inQueueHead = bufPtr->nextPtr; |
| 5703 |
if (chanPtr->inQueueHead == NULL) { |
| 5704 |
chanPtr->inQueueTail = NULL; |
| 5705 |
} |
| 5706 |
RecycleBuffer(chanPtr->state, bufPtr, 0); |
| 5707 |
} |
| 5708 |
} |
| 5709 |
|
| 5710 |
/* |
| 5711 |
* Go to the driver only if we got nothing from pushback. Have to do it |
| 5712 |
* this way to avoid EOF mistimings when we consider the ability that EOF |
| 5713 |
* may not be a permanent condition in the driver, and in that case we |
| 5714 |
* have to synchronize. |
| 5715 |
*/ |
| 5716 |
|
| 5717 |
if (copied) { |
| 5718 |
return copied; |
| 5719 |
} |
| 5720 |
|
| 5721 |
/* |
| 5722 |
* This test not needed. |
| 5723 |
*/ |
| 5724 |
|
| 5725 |
if (bytesToRead > 0) { |
| 5726 |
int nread = ChanRead(chanPtr, readBuf, bytesToRead); |
| 5727 |
|
| 5728 |
if (nread < 0) { |
| 5729 |
/* |
| 5730 |
* An error signaled. If CHANNEL_BLOCKED, then the error is not |
| 5731 |
* real, but an indication of blocked state. In that case, retain |
| 5732 |
* the flag and let caller receive the short read of copied bytes |
| 5733 |
* from the pushback. HOWEVER, if copied==0 bytes from pushback |
| 5734 |
* then repeat signalling the blocked state as an error to caller |
| 5735 |
* so there is no false report of an EOF. When !CHANNEL_BLOCKED, |
| 5736 |
* the error is real and passes on to caller. |
| 5737 |
*/ |
| 5738 |
|
| 5739 |
if (!GotFlag(statePtr, CHANNEL_BLOCKED) || copied == 0) { |
| 5740 |
copied = -1; |
| 5741 |
} |
| 5742 |
} else if (nread > 0) { |
| 5743 |
/* |
| 5744 |
* Successful read (short is OK) - add to bytes copied. |
| 5745 |
*/ |
| 5746 |
|
| 5747 |
copied += nread; |
| 5748 |
} else { |
| 5749 |
/* |
| 5750 |
* nread == 0. Driver is at EOF. Let that state filter up. |
| 5751 |
*/ |
| 5752 |
} |
| 5753 |
} |
| 5754 |
return copied; |
| 5755 |
} |
| 5756 |
|
| 5757 |
/* |
| 5758 |
*--------------------------------------------------------------------------- |
| 5759 |
* |
| 5760 |
* Tcl_ReadChars -- |
| 5761 |
* |
| 5762 |
* Reads from the channel until the requested number of characters have |
| 5763 |
* been seen, EOF is seen, or the channel would block. EOL and EOF |
| 5764 |
* translation is done. If reading binary data, the raw bytes are wrapped |
| 5765 |
* in a Tcl byte array object. Otherwise, the raw bytes are converted to |
| 5766 |
* UTF-8 using the channel's current encoding and stored in a Tcl string |
| 5767 |
* object. |
| 5768 |
* |
| 5769 |
* Results: |
| 5770 |
* The number of characters read, or -1 on error. Use Tcl_GetErrno() to |
| 5771 |
* retrieve the error code for the error that occurred. |
| 5772 |
* |
| 5773 |
* Side effects: |
| 5774 |
* May cause input to be buffered. |
| 5775 |
* |
| 5776 |
*--------------------------------------------------------------------------- |
| 5777 |
*/ |
| 5778 |
|
| 5779 |
int |
| 5780 |
Tcl_ReadChars( |
| 5781 |
Tcl_Channel chan, /* The channel to read. */ |
| 5782 |
Tcl_Obj *objPtr, /* Input data is stored in this object. */ |
| 5783 |
int toRead, /* Maximum number of characters to store, or |
| 5784 |
* -1 to read all available data (up to EOF or |
| 5785 |
* when channel blocks). */ |
| 5786 |
int appendFlag) /* If non-zero, data read from the channel |
| 5787 |
* will be appended to the object. Otherwise, |
| 5788 |
* the data will replace the existing contents |
| 5789 |
* of the object. */ |
| 5790 |
{ |
| 5791 |
Channel *chanPtr = (Channel *) chan; |
| 5792 |
ChannelState *statePtr = chanPtr->state; |
| 5793 |
/* State info for channel */ |
| 5794 |
|
| 5795 |
/* |
| 5796 |
* This operation should occur at the top of a channel stack. |
| 5797 |
*/ |
| 5798 |
|
| 5799 |
chanPtr = statePtr->topChanPtr; |
| 5800 |
|
| 5801 |
if (CheckChannelErrors(statePtr, TCL_READABLE) != 0) { |
| 5802 |
/* |
| 5803 |
* Update the notifier state so we don't block while there is still |
| 5804 |
* data in the buffers. |
| 5805 |
*/ |
| 5806 |
|
| 5807 |
UpdateInterest(chanPtr); |
| 5808 |
return -1; |
| 5809 |
} |
| 5810 |
|
| 5811 |
return DoReadChars(chanPtr, objPtr, toRead, appendFlag); |
| 5812 |
} |
| 5813 |
/* |
| 5814 |
*--------------------------------------------------------------------------- |
| 5815 |
* |
| 5816 |
* DoReadChars -- |
| 5817 |
* |
| 5818 |
* Reads from the channel until the requested number of characters have |
| 5819 |
* been seen, EOF is seen, or the channel would block. EOL and EOF |
| 5820 |
* translation is done. If reading binary data, the raw bytes are wrapped |
| 5821 |
* in a Tcl byte array object. Otherwise, the raw bytes are converted to |
| 5822 |
* UTF-8 using the channel's current encoding and stored in a Tcl string |
| 5823 |
* object. |
| 5824 |
* |
| 5825 |
* Results: |
| 5826 |
* The number of characters read, or -1 on error. Use Tcl_GetErrno() to |
| 5827 |
* retrieve the error code for the error that occurred. |
| 5828 |
* |
| 5829 |
* Side effects: |
| 5830 |
* May cause input to be buffered. |
| 5831 |
* |
| 5832 |
*--------------------------------------------------------------------------- |
| 5833 |
*/ |
| 5834 |
|
| 5835 |
static int |
| 5836 |
DoReadChars( |
| 5837 |
Channel *chanPtr, /* The channel to read. */ |
| 5838 |
Tcl_Obj *objPtr, /* Input data is stored in this object. */ |
| 5839 |
int toRead, /* Maximum number of characters to store, or |
| 5840 |
* -1 to read all available data (up to EOF or |
| 5841 |
* when channel blocks). */ |
| 5842 |
int appendFlag) /* If non-zero, data read from the channel |
| 5843 |
* will be appended to the object. Otherwise, |
| 5844 |
* the data will replace the existing contents |
| 5845 |
* of the object. */ |
| 5846 |
{ |
| 5847 |
ChannelState *statePtr = chanPtr->state; |
| 5848 |
/* State info for channel */ |
| 5849 |
ChannelBuffer *bufPtr; |
| 5850 |
int copied; |
| 5851 |
int result; |
| 5852 |
Tcl_Encoding encoding = statePtr->encoding; |
| 5853 |
int binaryMode; |
| 5854 |
#define UTF_EXPANSION_FACTOR 1024 |
| 5855 |
int factor = UTF_EXPANSION_FACTOR; |
| 5856 |
|
| 5857 |
binaryMode = (encoding == NULL) |
| 5858 |
&& (statePtr->inputTranslation == TCL_TRANSLATE_LF) |
| 5859 |
&& (statePtr->inEofChar == '\0'); |
| 5860 |
|
| 5861 |
if (appendFlag == 0) { |
| 5862 |
if (binaryMode) { |
| 5863 |
Tcl_SetByteArrayLength(objPtr, 0); |
| 5864 |
} else { |
| 5865 |
Tcl_SetObjLength(objPtr, 0); |
| 5866 |
|
| 5867 |
/* |
| 5868 |
* We're going to access objPtr->bytes directly, so we must ensure |
| 5869 |
* that this is actually a string object (otherwise it might have |
| 5870 |
* been pure Unicode). |
| 5871 |
* |
| 5872 |
* Probably not needed anymore. |
| 5873 |
*/ |
| 5874 |
|
| 5875 |
TclGetString(objPtr); |
| 5876 |
} |
| 5877 |
} |
| 5878 |
|
| 5879 |
/* |
| 5880 |
* Early out when next read will see eofchar. |
| 5881 |
* |
| 5882 |
* NOTE: See DoRead for argument that it's a bug (one we're keeping) to |
| 5883 |
* have this escape before the one for zero-char read request. |
| 5884 |
*/ |
| 5885 |
|
| 5886 |
if (GotFlag(statePtr, CHANNEL_STICKY_EOF)) { |
| 5887 |
SetFlag(statePtr, CHANNEL_EOF); |
| 5888 |
assert(statePtr->inputEncodingFlags & TCL_ENCODING_END); |
| 5889 |
assert(!GotFlag(statePtr, CHANNEL_BLOCKED|INPUT_SAW_CR)); |
| 5890 |
|
| 5891 |
/* TODO: UpdateInterest not needed here? */ |
| 5892 |
UpdateInterest(chanPtr); |
| 5893 |
return 0; |
| 5894 |
} |
| 5895 |
|
| 5896 |
/* |
| 5897 |
* Special handling for zero-char read request. |
| 5898 |
*/ |
| 5899 |
if (toRead == 0) { |
| 5900 |
if (GotFlag(statePtr, CHANNEL_EOF)) { |
| 5901 |
statePtr->inputEncodingFlags |= TCL_ENCODING_START; |
| 5902 |
} |
| 5903 |
ResetFlag(statePtr, CHANNEL_BLOCKED|CHANNEL_EOF); |
| 5904 |
statePtr->inputEncodingFlags &= ~TCL_ENCODING_END; |
| 5905 |
/* TODO: UpdateInterest not needed here? */ |
| 5906 |
UpdateInterest(chanPtr); |
| 5907 |
return 0; |
| 5908 |
} |
| 5909 |
|
| 5910 |
/* |
| 5911 |
* This operation should occur at the top of a channel stack. |
| 5912 |
*/ |
| 5913 |
|
| 5914 |
chanPtr = statePtr->topChanPtr; |
| 5915 |
TclChannelPreserve((Tcl_Channel)chanPtr); |
| 5916 |
|
| 5917 |
/* |
| 5918 |
* Must clear the BLOCKED|EOF flags here since we check before reading. |
| 5919 |
*/ |
| 5920 |
|
| 5921 |
if (GotFlag(statePtr, CHANNEL_EOF)) { |
| 5922 |
statePtr->inputEncodingFlags |= TCL_ENCODING_START; |
| 5923 |
} |
| 5924 |
ResetFlag(statePtr, CHANNEL_BLOCKED|CHANNEL_EOF); |
| 5925 |
statePtr->inputEncodingFlags &= ~TCL_ENCODING_END; |
| 5926 |
for (copied = 0; toRead != 0 ; ) { |
| 5927 |
int copiedNow = -1; |
| 5928 |
if (statePtr->inQueueHead != NULL) { |
| 5929 |
if (binaryMode) { |
| 5930 |
copiedNow = ReadBytes(statePtr, objPtr, toRead); |
| 5931 |
} else { |
| 5932 |
copiedNow = ReadChars(statePtr, objPtr, toRead, &factor); |
| 5933 |
} |
| 5934 |
|
| 5935 |
/* |
| 5936 |
* Recycle current buffer if empty. |
| 5937 |
*/ |
| 5938 |
|
| 5939 |
bufPtr = statePtr->inQueueHead; |
| 5940 |
if (IsBufferEmpty(bufPtr)) { |
| 5941 |
ChannelBuffer *nextPtr = bufPtr->nextPtr; |
| 5942 |
|
| 5943 |
RecycleBuffer(statePtr, bufPtr, 0); |
| 5944 |
statePtr->inQueueHead = nextPtr; |
| 5945 |
if (nextPtr == NULL) { |
| 5946 |
statePtr->inQueueTail = NULL; |
| 5947 |
} |
| 5948 |
} |
| 5949 |
} |
| 5950 |
|
| 5951 |
if (copiedNow < 0) { |
| 5952 |
if (GotFlag(statePtr, CHANNEL_EOF)) { |
| 5953 |
break; |
| 5954 |
} |
| 5955 |
if (GotFlag(statePtr, CHANNEL_NONBLOCKING|CHANNEL_BLOCKED) |
| 5956 |
== (CHANNEL_NONBLOCKING|CHANNEL_BLOCKED)) { |
| 5957 |
break; |
| 5958 |
} |
| 5959 |
result = GetInput(chanPtr); |
| 5960 |
if (chanPtr != statePtr->topChanPtr) { |
| 5961 |
TclChannelRelease((Tcl_Channel)chanPtr); |
| 5962 |
chanPtr = statePtr->topChanPtr; |
| 5963 |
TclChannelPreserve((Tcl_Channel)chanPtr); |
| 5964 |
} |
| 5965 |
if (result != 0) { |
| 5966 |
if (!GotFlag(statePtr, CHANNEL_BLOCKED)) { |
| 5967 |
copied = -1; |
| 5968 |
} |
| 5969 |
break; |
| 5970 |
} |
| 5971 |
} else { |
| 5972 |
copied += copiedNow; |
| 5973 |
toRead -= copiedNow; |
| 5974 |
} |
| 5975 |
} |
| 5976 |
|
| 5977 |
/* |
| 5978 |
* Failure to fill a channel buffer may have left channel reporting a |
| 5979 |
* "blocked" state, but so long as we fulfilled the request here, the |
| 5980 |
* caller does not consider us blocked. |
| 5981 |
*/ |
| 5982 |
|
| 5983 |
if (toRead == 0) { |
| 5984 |
ResetFlag(statePtr, CHANNEL_BLOCKED); |
| 5985 |
} |
| 5986 |
|
| 5987 |
/* |
| 5988 |
* Regenerate chanPtr in case it was changed due to |
| 5989 |
* self-modifying reflected transforms. |
| 5990 |
*/ |
| 5991 |
|
| 5992 |
if (chanPtr != statePtr->topChanPtr) { |
| 5993 |
TclChannelRelease((Tcl_Channel)chanPtr); |
| 5994 |
chanPtr = statePtr->topChanPtr; |
| 5995 |
TclChannelPreserve((Tcl_Channel)chanPtr); |
| 5996 |
} |
| 5997 |
|
| 5998 |
/* |
| 5999 |
* Update the notifier state so we don't block while there is still data |
| 6000 |
* in the buffers. |
| 6001 |
*/ |
| 6002 |
|
| 6003 |
assert(!GotFlag(statePtr, CHANNEL_EOF) |
| 6004 |
|| GotFlag(statePtr, CHANNEL_STICKY_EOF) |
| 6005 |
|| Tcl_InputBuffered((Tcl_Channel)chanPtr) == 0); |
| 6006 |
assert(!(GotFlag(statePtr, CHANNEL_EOF|CHANNEL_BLOCKED) |
| 6007 |
== (CHANNEL_EOF|CHANNEL_BLOCKED))); |
| 6008 |
UpdateInterest(chanPtr); |
| 6009 |
|
| 6010 |
TclChannelRelease((Tcl_Channel)chanPtr); |
| 6011 |
return copied; |
| 6012 |
} |
| 6013 |
|
| 6014 |
/* |
| 6015 |
*--------------------------------------------------------------------------- |
| 6016 |
* |
| 6017 |
* ReadBytes -- |
| 6018 |
* |
| 6019 |
* Reads from the channel until the requested number of bytes have been |
| 6020 |
* seen, EOF is seen, or the channel would block. Bytes from the channel |
| 6021 |
* are stored in objPtr as a ByteArray object. EOL and EOF translation |
| 6022 |
* are done. |
| 6023 |
* |
| 6024 |
* 'bytesToRead' can safely be a very large number because space is only |
| 6025 |
* allocated to hold data read from the channel as needed. |
| 6026 |
* |
| 6027 |
* Results: |
| 6028 |
* The return value is the number of bytes appended to the object, or |
| 6029 |
* -1 to indicate that zero bytes were read due to an EOF. |
| 6030 |
* |
| 6031 |
* Side effects: |
| 6032 |
* The storage of bytes in objPtr can cause (re-)allocation of memory. |
| 6033 |
* |
| 6034 |
*--------------------------------------------------------------------------- |
| 6035 |
*/ |
| 6036 |
|
| 6037 |
static int |
| 6038 |
ReadBytes( |
| 6039 |
ChannelState *statePtr, /* State of the channel to read. */ |
| 6040 |
Tcl_Obj *objPtr, /* Input data is appended to this ByteArray |
| 6041 |
* object. Its length is how much space has |
| 6042 |
* been allocated to hold data, not how many |
| 6043 |
* bytes of data have been stored in the |
| 6044 |
* object. */ |
| 6045 |
int bytesToRead) /* Maximum number of bytes to store, or -1 to |
| 6046 |
* get all available bytes. Bytes are obtained |
| 6047 |
* from the first buffer in the queue - even |
| 6048 |
* if this number is larger than the number of |
| 6049 |
* bytes available in the first buffer, only |
| 6050 |
* the bytes from the first buffer are |
| 6051 |
* returned. */ |
| 6052 |
{ |
| 6053 |
ChannelBuffer *bufPtr = statePtr->inQueueHead; |
| 6054 |
int srcLen = BytesLeft(bufPtr); |
| 6055 |
int toRead = bytesToRead>srcLen || bytesToRead<0 ? srcLen : bytesToRead; |
| 6056 |
|
| 6057 |
TclAppendBytesToByteArray(objPtr, (unsigned char *) RemovePoint(bufPtr), |
| 6058 |
toRead); |
| 6059 |
bufPtr->nextRemoved += toRead; |
| 6060 |
return toRead; |
| 6061 |
} |
| 6062 |
|
| 6063 |
/* |
| 6064 |
*--------------------------------------------------------------------------- |
| 6065 |
* |
| 6066 |
* ReadChars -- |
| 6067 |
* |
| 6068 |
* Reads from the channel until the requested number of UTF-8 characters |
| 6069 |
* have been seen, EOF is seen, or the channel would block. Raw bytes |
| 6070 |
* from the channel are converted to UTF-8 and stored in objPtr. EOL and |
| 6071 |
* EOF translation is done. |
| 6072 |
* |
| 6073 |
* 'charsToRead' can safely be a very large number because space is only |
| 6074 |
* allocated to hold data read from the channel as needed. |
| 6075 |
* |
| 6076 |
* 'charsToRead' may *not* be 0. |
| 6077 |
* |
| 6078 |
* Results: |
| 6079 |
* The return value is the number of characters appended to the object, |
| 6080 |
* *offsetPtr is filled with the number of bytes that were appended, and |
| 6081 |
* *factorPtr is filled with the expansion factor used to guess how many |
| 6082 |
* bytes of UTF-8 to allocate to hold N source bytes. |
| 6083 |
* |
| 6084 |
* Side effects: |
| 6085 |
* None. |
| 6086 |
* |
| 6087 |
*--------------------------------------------------------------------------- |
| 6088 |
*/ |
| 6089 |
|
| 6090 |
static int |
| 6091 |
ReadChars( |
| 6092 |
ChannelState *statePtr, /* State of channel to read. */ |
| 6093 |
Tcl_Obj *objPtr, /* Input data is appended to this object. |
| 6094 |
* objPtr->length is how much space has been |
| 6095 |
* allocated to hold data, not how many bytes |
| 6096 |
* of data have been stored in the object. */ |
| 6097 |
int charsToRead, /* Maximum number of characters to store, or |
| 6098 |
* -1 to get all available characters. |
| 6099 |
* Characters are obtained from the first |
| 6100 |
* buffer in the queue -- even if this number |
| 6101 |
* is larger than the number of characters |
| 6102 |
* available in the first buffer, only the |
| 6103 |
* characters from the first buffer are |
| 6104 |
* returned. The exception is when there is |
| 6105 |
* not any complete character in the first |
| 6106 |
* buffer. In that case, a recursive call |
| 6107 |
* effectively obtains chars from the |
| 6108 |
* second buffer. */ |
| 6109 |
int *factorPtr) /* On input, contains a guess of how many |
| 6110 |
* bytes need to be allocated to hold the |
| 6111 |
* result of converting N source bytes to |
| 6112 |
* UTF-8. On output, contains another guess |
| 6113 |
* based on the data seen so far. */ |
| 6114 |
{ |
| 6115 |
Tcl_Encoding encoding = statePtr->encoding? statePtr->encoding |
| 6116 |
: GetBinaryEncoding(); |
| 6117 |
Tcl_EncodingState savedState = statePtr->inputEncodingState; |
| 6118 |
ChannelBuffer *bufPtr = statePtr->inQueueHead; |
| 6119 |
int savedIEFlags = statePtr->inputEncodingFlags; |
| 6120 |
int savedFlags = statePtr->flags; |
| 6121 |
char *dst, *src = RemovePoint(bufPtr); |
| 6122 |
int numBytes; |
| 6123 |
int srcLen = BytesLeft(bufPtr); |
| 6124 |
|
| 6125 |
/* |
| 6126 |
* One src byte can yield at most one character. So when the number of |
| 6127 |
* src bytes we plan to read is less than the limit on character count to |
| 6128 |
* be read, clearly we will remain within that limit, and we can use the |
| 6129 |
* value of "srcLen" as a tighter limit for sizing receiving buffers. |
| 6130 |
*/ |
| 6131 |
|
| 6132 |
int toRead = ((charsToRead<0)||(charsToRead > srcLen)) ? srcLen : charsToRead; |
| 6133 |
|
| 6134 |
/* |
| 6135 |
* 'factor' is how much we guess that the bytes in the source buffer will |
| 6136 |
* expand when converted to UTF-8 chars. This guess comes from analyzing |
| 6137 |
* how many characters were produced by the previous pass. |
| 6138 |
*/ |
| 6139 |
|
| 6140 |
int factor = *factorPtr; |
| 6141 |
int dstLimit = TCL_UTF_MAX - 1 + toRead * factor / UTF_EXPANSION_FACTOR; |
| 6142 |
|
| 6143 |
if (dstLimit <= 0) { |
| 6144 |
dstLimit = INT_MAX; /* avoid overflow */ |
| 6145 |
} |
| 6146 |
(void)TclGetStringFromObj(objPtr, &numBytes); |
| 6147 |
TclAppendUtfToUtf(objPtr, NULL, dstLimit); |
| 6148 |
if (toRead == srcLen) { |
| 6149 |
unsigned int size; |
| 6150 |
|
| 6151 |
dst = TclGetStringStorage(objPtr, &size) + numBytes; |
| 6152 |
dstLimit = size - numBytes; |
| 6153 |
} else { |
| 6154 |
dst = TclGetString(objPtr) + numBytes; |
| 6155 |
} |
| 6156 |
|
| 6157 |
/* |
| 6158 |
* This routine is burdened with satisfying several constraints. It cannot |
| 6159 |
* append more than 'charsToRead` chars onto objPtr. This is measured |
| 6160 |
* after encoding and translation transformations are completed. There is |
| 6161 |
* no precise number of src bytes that can be associated with the limit. |
| 6162 |
* Yet, when we are done, we must know precisely the number of src bytes |
| 6163 |
* that were consumed to produce the appended chars, so that all |
| 6164 |
* subsequent bytes are left in the buffers for future read operations. |
| 6165 |
* |
| 6166 |
* The consequence is that we have no choice but to implement a "trial and |
| 6167 |
* error" approach, where in general we may need to perform |
| 6168 |
* transformations and copies multiple times to achieve a consistent set |
| 6169 |
* of results. This takes the shape of a loop. |
| 6170 |
*/ |
| 6171 |
|
| 6172 |
while (1) { |
| 6173 |
int dstDecoded, dstRead, dstWrote, srcRead, numChars, code; |
| 6174 |
int flags = statePtr->inputEncodingFlags | TCL_ENCODING_NO_TERMINATE; |
| 6175 |
|
| 6176 |
if (charsToRead > 0) { |
| 6177 |
flags |= TCL_ENCODING_CHAR_LIMIT; |
| 6178 |
numChars = charsToRead; |
| 6179 |
} |
| 6180 |
|
| 6181 |
/* |
| 6182 |
* Perform the encoding transformation. Read no more than srcLen |
| 6183 |
* bytes, write no more than dstLimit bytes. |
| 6184 |
* |
| 6185 |
* Some trickiness with encoding flags here. We do not want the end |
| 6186 |
* of a buffer to be treated as the end of all input when the presence |
| 6187 |
* of bytes in a next buffer are already known to exist. This is |
| 6188 |
* checked with an assert() because so far no test case causing the |
| 6189 |
* assertion to be false has been created. The normal operations of |
| 6190 |
* channel reading appear to cause EOF and TCL_ENCODING_END setting to |
| 6191 |
* appear only in situations where there are no further bytes in any |
| 6192 |
* buffers. |
| 6193 |
*/ |
| 6194 |
|
| 6195 |
assert(bufPtr->nextPtr == NULL || BytesLeft(bufPtr->nextPtr) == 0 |
| 6196 |
|| (statePtr->inputEncodingFlags & TCL_ENCODING_END) == 0); |
| 6197 |
|
| 6198 |
code = Tcl_ExternalToUtf(NULL, encoding, src, srcLen, |
| 6199 |
flags, &statePtr->inputEncodingState, |
| 6200 |
dst, dstLimit, &srcRead, &dstDecoded, &numChars); |
| 6201 |
|
| 6202 |
/* |
| 6203 |
* Perform the translation transformation in place. Read no more than |
| 6204 |
* the dstDecoded bytes the encoding transformation actually produced. |
| 6205 |
* Capture the number of bytes written in dstWrote. Capture the number |
| 6206 |
* of bytes actually consumed in dstRead. |
| 6207 |
*/ |
| 6208 |
|
| 6209 |
dstWrote = dstLimit; |
| 6210 |
dstRead = dstDecoded; |
| 6211 |
TranslateInputEOL(statePtr, dst, dst, &dstWrote, &dstRead); |
| 6212 |
|
| 6213 |
if (dstRead < dstDecoded) { |
| 6214 |
/* |
| 6215 |
* The encoding transformation produced bytes that the translation |
| 6216 |
* transformation did not consume. Why did this happen? |
| 6217 |
*/ |
| 6218 |
|
| 6219 |
if (statePtr->inEofChar && dst[dstRead] == statePtr->inEofChar) { |
| 6220 |
/* |
| 6221 |
* 1) There's an eof char set on the channel, and |
| 6222 |
* we saw it and stopped translating at that point. |
| 6223 |
* |
| 6224 |
* NOTE the bizarre spec of TranslateInputEOL in this case. |
| 6225 |
* Clearly the eof char had to be read in order to account for |
| 6226 |
* the stopping, but the value of dstRead does not include it. |
| 6227 |
* |
| 6228 |
* Also rather bizarre, our caller can only notice an EOF |
| 6229 |
* condition if we return the value -1 as the number of chars |
| 6230 |
* read. This forces us to perform a 2-call dance where the |
| 6231 |
* first call can read all the chars up to the eof char, and |
| 6232 |
* the second call is solely for consuming the encoded eof |
| 6233 |
* char then pointed at by src so that we can return that |
| 6234 |
* magic -1 value. This seems really wasteful, especially |
| 6235 |
* since the first decoding pass of each call is likely to |
| 6236 |
* decode many bytes beyond that eof char that's all we care |
| 6237 |
* about. |
| 6238 |
*/ |
| 6239 |
|
| 6240 |
if (dstRead == 0) { |
| 6241 |
/* |
| 6242 |
* Curious choice in the eof char handling. We leave the |
| 6243 |
* eof char in the buffer. So, no need to compute a proper |
| 6244 |
* srcRead value. At this point, there are no chars before |
| 6245 |
* the eof char in the buffer. |
| 6246 |
*/ |
| 6247 |
|
| 6248 |
Tcl_SetObjLength(objPtr, numBytes); |
| 6249 |
return -1; |
| 6250 |
} |
| 6251 |
|
| 6252 |
{ |
| 6253 |
/* |
| 6254 |
* There are chars leading the buffer before the eof char. |
| 6255 |
* Adjust the dstLimit so we go back and read only those |
| 6256 |
* and do not encounter the eof char this time. |
| 6257 |
*/ |
| 6258 |
|
| 6259 |
dstLimit = dstRead + (TCL_UTF_MAX - 1); |
| 6260 |
statePtr->flags = savedFlags; |
| 6261 |
statePtr->inputEncodingFlags = savedIEFlags; |
| 6262 |
statePtr->inputEncodingState = savedState; |
| 6263 |
continue; |
| 6264 |
} |
| 6265 |
} |
| 6266 |
|
| 6267 |
/* |
| 6268 |
* 2) The other way to read fewer bytes than are decoded is when |
| 6269 |
* the final byte is \r and we're in a CRLF translation mode so |
| 6270 |
* we cannot decide whether to record \r or \n yet. |
| 6271 |
*/ |
| 6272 |
|
| 6273 |
assert(dst[dstRead] == '\r'); |
| 6274 |
assert(statePtr->inputTranslation == TCL_TRANSLATE_CRLF); |
| 6275 |
|
| 6276 |
if (dstWrote > 0) { |
| 6277 |
/* |
| 6278 |
* There are chars we can read before we hit the bare CR. Go |
| 6279 |
* back with a smaller dstLimit so we get them in the next |
| 6280 |
* pass, compute a matching srcRead, and don't end up back |
| 6281 |
* here in this call. |
| 6282 |
*/ |
| 6283 |
|
| 6284 |
dstLimit = dstRead + (TCL_UTF_MAX - 1); |
| 6285 |
statePtr->flags = savedFlags; |
| 6286 |
statePtr->inputEncodingFlags = savedIEFlags; |
| 6287 |
statePtr->inputEncodingState = savedState; |
| 6288 |
continue; |
| 6289 |
} |
| 6290 |
|
| 6291 |
assert(dstWrote == 0); |
| 6292 |
assert(dstRead == 0); |
| 6293 |
|
| 6294 |
/* |
| 6295 |
* We decoded only the bare CR, and we cannot read a translated |
| 6296 |
* char from that alone. We have to know what's next. So why do |
| 6297 |
* we only have the one decoded char? |
| 6298 |
*/ |
| 6299 |
|
| 6300 |
if (code != TCL_OK) { |
| 6301 |
int read, decoded, count; |
| 6302 |
char buffer[TCL_UTF_MAX + 1]; |
| 6303 |
|
| 6304 |
/* |
| 6305 |
* Didn't get everything the buffer could offer |
| 6306 |
*/ |
| 6307 |
|
| 6308 |
statePtr->flags = savedFlags; |
| 6309 |
statePtr->inputEncodingFlags = savedIEFlags; |
| 6310 |
statePtr->inputEncodingState = savedState; |
| 6311 |
|
| 6312 |
assert(bufPtr->nextPtr == NULL |
| 6313 |
|| BytesLeft(bufPtr->nextPtr) == 0 || 0 == |
| 6314 |
(statePtr->inputEncodingFlags & TCL_ENCODING_END)); |
| 6315 |
|
| 6316 |
Tcl_ExternalToUtf(NULL, encoding, src, srcLen, |
| 6317 |
(statePtr->inputEncodingFlags | TCL_ENCODING_NO_TERMINATE), |
| 6318 |
&statePtr->inputEncodingState, buffer, sizeof(buffer), |
| 6319 |
&read, &decoded, &count); |
| 6320 |
|
| 6321 |
if (count == 2) { |
| 6322 |
if (buffer[1] == '\n') { |
| 6323 |
/* \r\n translate to \n */ |
| 6324 |
dst[0] = '\n'; |
| 6325 |
bufPtr->nextRemoved += read; |
| 6326 |
} else { |
| 6327 |
dst[0] = '\r'; |
| 6328 |
bufPtr->nextRemoved += srcRead; |
| 6329 |
} |
| 6330 |
|
| 6331 |
statePtr->inputEncodingFlags &= ~TCL_ENCODING_START; |
| 6332 |
|
| 6333 |
Tcl_SetObjLength(objPtr, numBytes + 1); |
| 6334 |
return 1; |
| 6335 |
} |
| 6336 |
|
| 6337 |
} else if (GotFlag(statePtr, CHANNEL_EOF)) { |
| 6338 |
/* |
| 6339 |
* The bare \r is the only char and we will never read a |
| 6340 |
* subsequent char to make the determination. |
| 6341 |
*/ |
| 6342 |
|
| 6343 |
dst[0] = '\r'; |
| 6344 |
bufPtr->nextRemoved = bufPtr->nextAdded; |
| 6345 |
Tcl_SetObjLength(objPtr, numBytes + 1); |
| 6346 |
return 1; |
| 6347 |
} |
| 6348 |
|
| 6349 |
/* |
| 6350 |
* Revise the dstRead value so that the numChars calc below |
| 6351 |
* correctly computes zero characters read. |
| 6352 |
*/ |
| 6353 |
|
| 6354 |
dstRead = numChars; |
| 6355 |
|
| 6356 |
/* FALL THROUGH - get more data (dstWrote == 0) */ |
| 6357 |
} |
| 6358 |
|
| 6359 |
/* |
| 6360 |
* The translation transformation can only reduce the number of chars |
| 6361 |
* when it converts \r\n into \n. The reduction in the number of chars |
| 6362 |
* is the difference in bytes read and written. |
| 6363 |
*/ |
| 6364 |
|
| 6365 |
numChars -= (dstRead - dstWrote); |
| 6366 |
|
| 6367 |
if (charsToRead > 0 && numChars > charsToRead) { |
| 6368 |
|
| 6369 |
/* |
| 6370 |
* TODO: This cannot happen anymore. |
| 6371 |
* |
| 6372 |
* We read more chars than allowed. Reset limits to prevent that |
| 6373 |
* and try again. Don't forget the extra padding of TCL_UTF_MAX |
| 6374 |
* bytes demanded by the Tcl_ExternalToUtf() call! |
| 6375 |
*/ |
| 6376 |
|
| 6377 |
dstLimit = Tcl_UtfAtIndex(dst, charsToRead) - dst + (TCL_UTF_MAX - 1); |
| 6378 |
statePtr->flags = savedFlags; |
| 6379 |
statePtr->inputEncodingFlags = savedIEFlags; |
| 6380 |
statePtr->inputEncodingState = savedState; |
| 6381 |
continue; |
| 6382 |
} |
| 6383 |
|
| 6384 |
if (dstWrote == 0) { |
| 6385 |
ChannelBuffer *nextPtr; |
| 6386 |
|
| 6387 |
/* |
| 6388 |
* We were not able to read any chars. |
| 6389 |
*/ |
| 6390 |
|
| 6391 |
assert(numChars == 0); |
| 6392 |
|
| 6393 |
/* |
| 6394 |
* There is one situation where this is the correct final result. |
| 6395 |
* If the src buffer contains only a single \n byte, and we are in |
| 6396 |
* TCL_TRANSLATE_AUTO mode, and when the translation pass was made |
| 6397 |
* the INPUT_SAW_CR flag was set on the channel. In that case, the |
| 6398 |
* correct behavior is to consume that \n and produce the empty |
| 6399 |
* string. |
| 6400 |
*/ |
| 6401 |
|
| 6402 |
if (dstRead == 1 && dst[0] == '\n') { |
| 6403 |
assert(statePtr->inputTranslation == TCL_TRANSLATE_AUTO); |
| 6404 |
|
| 6405 |
goto consume; |
| 6406 |
} |
| 6407 |
|
| 6408 |
/* |
| 6409 |
* Otherwise, reading zero characters indicates there's something |
| 6410 |
* incomplete at the end of the src buffer. Maybe there were not |
| 6411 |
* enough src bytes to decode into a char. Maybe a lone \r could |
| 6412 |
* not be translated (crlf mode). Need to combine any unused src |
| 6413 |
* bytes we have in the first buffer with subsequent bytes to try |
| 6414 |
* again. |
| 6415 |
*/ |
| 6416 |
|
| 6417 |
nextPtr = bufPtr->nextPtr; |
| 6418 |
|
| 6419 |
if (nextPtr == NULL) { |
| 6420 |
if (srcLen > 0) { |
| 6421 |
SetFlag(statePtr, CHANNEL_NEED_MORE_DATA); |
| 6422 |
} |
| 6423 |
Tcl_SetObjLength(objPtr, numBytes); |
| 6424 |
return -1; |
| 6425 |
} |
| 6426 |
|
| 6427 |
/* |
| 6428 |
* Space is made at the beginning of the buffer to copy the |
| 6429 |
* previous unused bytes there. Check first if the buffer we are |
| 6430 |
* using actually has enough space at its beginning for the data |
| 6431 |
* we are copying. Because if not we will write over the buffer |
| 6432 |
* management information, especially the 'nextPtr'. |
| 6433 |
* |
| 6434 |
* Note that the BUFFER_PADDING (See AllocChannelBuffer) is used |
| 6435 |
* to prevent exactly this situation. I.e. it should never happen. |
| 6436 |
* Therefore it is ok to panic should it happen despite the |
| 6437 |
* precautions. |
| 6438 |
*/ |
| 6439 |
|
| 6440 |
if (nextPtr->nextRemoved < srcLen) { |
| 6441 |
Tcl_Panic("Buffer Underflow, BUFFER_PADDING not enough"); |
| 6442 |
} |
| 6443 |
|
| 6444 |
nextPtr->nextRemoved -= srcLen; |
| 6445 |
memcpy(RemovePoint(nextPtr), src, srcLen); |
| 6446 |
RecycleBuffer(statePtr, bufPtr, 0); |
| 6447 |
statePtr->inQueueHead = nextPtr; |
| 6448 |
Tcl_SetObjLength(objPtr, numBytes); |
| 6449 |
return ReadChars(statePtr, objPtr, charsToRead, factorPtr); |
| 6450 |
} |
| 6451 |
|
| 6452 |
statePtr->inputEncodingFlags &= ~TCL_ENCODING_START; |
| 6453 |
|
| 6454 |
consume: |
| 6455 |
bufPtr->nextRemoved += srcRead; |
| 6456 |
|
| 6457 |
/* |
| 6458 |
* If this read contained multibyte characters, revise factorPtr so |
| 6459 |
* the next read will allocate bigger buffers. |
| 6460 |
*/ |
| 6461 |
|
| 6462 |
if (numChars && numChars < srcRead) { |
| 6463 |
*factorPtr = srcRead * UTF_EXPANSION_FACTOR / numChars; |
| 6464 |
} |
| 6465 |
Tcl_SetObjLength(objPtr, numBytes + dstWrote); |
| 6466 |
return numChars; |
| 6467 |
} |
| 6468 |
} |
| 6469 |
|
| 6470 |
/* |
| 6471 |
*--------------------------------------------------------------------------- |
| 6472 |
* |
| 6473 |
* TranslateInputEOL -- |
| 6474 |
* |
| 6475 |
* Perform input EOL and EOF translation on the source buffer, leaving |
| 6476 |
* the translated result in the destination buffer. |
| 6477 |
* |
| 6478 |
* Results: |
| 6479 |
* The return value is 1 if the EOF character was found when copying |
| 6480 |
* bytes to the destination buffer, 0 otherwise. |
| 6481 |
* |
| 6482 |
* Side effects: |
| 6483 |
* None. |
| 6484 |
* |
| 6485 |
*--------------------------------------------------------------------------- |
| 6486 |
*/ |
| 6487 |
|
| 6488 |
static void |
| 6489 |
TranslateInputEOL( |
| 6490 |
ChannelState *statePtr, /* Channel being read, for EOL translation and |
| 6491 |
* EOF character. */ |
| 6492 |
char *dstStart, /* Output buffer filled with chars by applying |
| 6493 |
* appropriate EOL translation to source |
| 6494 |
* characters. */ |
| 6495 |
const char *srcStart, /* Source characters. */ |
| 6496 |
int *dstLenPtr, /* On entry, the maximum length of output |
| 6497 |
* buffer in bytes. On exit, the number of |
| 6498 |
* bytes actually used in output buffer. */ |
| 6499 |
int *srcLenPtr) /* On entry, the length of source buffer. On |
| 6500 |
* exit, the number of bytes read from the |
| 6501 |
* source buffer. */ |
| 6502 |
{ |
| 6503 |
const char *eof = NULL; |
| 6504 |
int dstLen = *dstLenPtr; |
| 6505 |
int srcLen = *srcLenPtr; |
| 6506 |
int inEofChar = statePtr->inEofChar; |
| 6507 |
|
| 6508 |
/* |
| 6509 |
* Depending on the translation mode in use, there's no need to scan more |
| 6510 |
* srcLen bytes at srcStart than can possibly transform to dstLen bytes. |
| 6511 |
* This keeps the scan for eof char below from being pointlessly long. |
| 6512 |
*/ |
| 6513 |
|
| 6514 |
switch (statePtr->inputTranslation) { |
| 6515 |
case TCL_TRANSLATE_LF: |
| 6516 |
case TCL_TRANSLATE_CR: |
| 6517 |
if (srcLen > dstLen) { |
| 6518 |
/* |
| 6519 |
* In these modes, each src byte become a dst byte. |
| 6520 |
*/ |
| 6521 |
|
| 6522 |
srcLen = dstLen; |
| 6523 |
} |
| 6524 |
break; |
| 6525 |
default: |
| 6526 |
/* |
| 6527 |
* In other modes, at most 2 src bytes become a dst byte. |
| 6528 |
*/ |
| 6529 |
|
| 6530 |
if (srcLen/2 > dstLen) { |
| 6531 |
srcLen = 2 * dstLen; |
| 6532 |
} |
| 6533 |
break; |
| 6534 |
} |
| 6535 |
|
| 6536 |
if (inEofChar != '\0') { |
| 6537 |
/* |
| 6538 |
* Make sure we do not read past any logical end of channel input |
| 6539 |
* created by the presence of the input eof char. |
| 6540 |
*/ |
| 6541 |
|
| 6542 |
if ((eof = (const char *)memchr(srcStart, inEofChar, srcLen))) { |
| 6543 |
srcLen = eof - srcStart; |
| 6544 |
} |
| 6545 |
} |
| 6546 |
|
| 6547 |
switch (statePtr->inputTranslation) { |
| 6548 |
case TCL_TRANSLATE_LF: |
| 6549 |
case TCL_TRANSLATE_CR: |
| 6550 |
if (dstStart != srcStart) { |
| 6551 |
memcpy(dstStart, srcStart, srcLen); |
| 6552 |
} |
| 6553 |
if (statePtr->inputTranslation == TCL_TRANSLATE_CR) { |
| 6554 |
char *dst = dstStart; |
| 6555 |
char *dstEnd = dstStart + srcLen; |
| 6556 |
|
| 6557 |
while ((dst = (char *)memchr(dst, '\r', dstEnd - dst))) { |
| 6558 |
*dst++ = '\n'; |
| 6559 |
} |
| 6560 |
} |
| 6561 |
dstLen = srcLen; |
| 6562 |
break; |
| 6563 |
case TCL_TRANSLATE_CRLF: { |
| 6564 |
const char *crFound, *src = srcStart; |
| 6565 |
char *dst = dstStart; |
| 6566 |
int lesser = (dstLen < srcLen) ? dstLen : srcLen; |
| 6567 |
|
| 6568 |
while ((crFound = (const char *)memchr(src, '\r', lesser))) { |
| 6569 |
int numBytes = crFound - src; |
| 6570 |
memmove(dst, src, numBytes); |
| 6571 |
|
| 6572 |
dst += numBytes; |
| 6573 |
dstLen -= numBytes; |
| 6574 |
src += numBytes; |
| 6575 |
srcLen -= numBytes; |
| 6576 |
if (srcLen == 1) { |
| 6577 |
/* valid src bytes end in \r */ |
| 6578 |
if (eof) { |
| 6579 |
*dst++ = '\r'; |
| 6580 |
src++; |
| 6581 |
srcLen--; |
| 6582 |
} else { |
| 6583 |
lesser = 0; |
| 6584 |
break; |
| 6585 |
} |
| 6586 |
} else if (src[1] == '\n') { |
| 6587 |
*dst++ = '\n'; |
| 6588 |
src += 2; |
| 6589 |
srcLen -= 2; |
| 6590 |
} else { |
| 6591 |
*dst++ = '\r'; |
| 6592 |
src++; |
| 6593 |
srcLen--; |
| 6594 |
} |
| 6595 |
dstLen--; |
| 6596 |
lesser = (dstLen < srcLen) ? dstLen : srcLen; |
| 6597 |
} |
| 6598 |
memmove(dst, src, lesser); |
| 6599 |
srcLen = src + lesser - srcStart; |
| 6600 |
dstLen = dst + lesser - dstStart; |
| 6601 |
break; |
| 6602 |
} |
| 6603 |
case TCL_TRANSLATE_AUTO: { |
| 6604 |
const char *crFound, *src = srcStart; |
| 6605 |
char *dst = dstStart; |
| 6606 |
int lesser; |
| 6607 |
|
| 6608 |
if (GotFlag(statePtr, INPUT_SAW_CR) && srcLen) { |
| 6609 |
if (*src == '\n') { |
| 6610 |
src++; |
| 6611 |
srcLen--; |
| 6612 |
} |
| 6613 |
ResetFlag(statePtr, INPUT_SAW_CR); |
| 6614 |
} |
| 6615 |
lesser = (dstLen < srcLen) ? dstLen : srcLen; |
| 6616 |
while ((crFound = (const char *)memchr(src, '\r', lesser))) { |
| 6617 |
int numBytes = crFound - src; |
| 6618 |
memmove(dst, src, numBytes); |
| 6619 |
|
| 6620 |
dst[numBytes] = '\n'; |
| 6621 |
dst += numBytes + 1; |
| 6622 |
dstLen -= numBytes + 1; |
| 6623 |
src += numBytes + 1; |
| 6624 |
srcLen -= numBytes + 1; |
| 6625 |
if (srcLen == 0) { |
| 6626 |
SetFlag(statePtr, INPUT_SAW_CR); |
| 6627 |
} else if (*src == '\n') { |
| 6628 |
src++; |
| 6629 |
srcLen--; |
| 6630 |
} |
| 6631 |
lesser = (dstLen < srcLen) ? dstLen : srcLen; |
| 6632 |
} |
| 6633 |
memmove(dst, src, lesser); |
| 6634 |
srcLen = src + lesser - srcStart; |
| 6635 |
dstLen = dst + lesser - dstStart; |
| 6636 |
break; |
| 6637 |
} |
| 6638 |
default: |
| 6639 |
Tcl_Panic("unknown input translation %d", statePtr->inputTranslation); |
| 6640 |
} |
| 6641 |
*dstLenPtr = dstLen; |
| 6642 |
*srcLenPtr = srcLen; |
| 6643 |
|
| 6644 |
if (srcStart + srcLen == eof) { |
| 6645 |
/* |
| 6646 |
* EOF character was seen in EOL translated range. Leave current file |
| 6647 |
* position pointing at the EOF character, but don't store the EOF |
| 6648 |
* character in the output string. |
| 6649 |
*/ |
| 6650 |
|
| 6651 |
SetFlag(statePtr, CHANNEL_EOF | CHANNEL_STICKY_EOF); |
| 6652 |
statePtr->inputEncodingFlags |= TCL_ENCODING_END; |
| 6653 |
ResetFlag(statePtr, CHANNEL_BLOCKED|INPUT_SAW_CR); |
| 6654 |
} |
| 6655 |
} |
| 6656 |
|
| 6657 |
/* |
| 6658 |
*---------------------------------------------------------------------- |
| 6659 |
* |
| 6660 |
* Tcl_Ungets -- |
| 6661 |
* |
| 6662 |
* Causes the supplied string to be added to the input queue of the |
| 6663 |
* channel, at either the head or tail of the queue. |
| 6664 |
* |
| 6665 |
* Results: |
| 6666 |
* The number of bytes stored in the channel, or -1 on error. |
| 6667 |
* |
| 6668 |
* Side effects: |
| 6669 |
* Adds input to the input queue of a channel. |
| 6670 |
* |
| 6671 |
*---------------------------------------------------------------------- |
| 6672 |
*/ |
| 6673 |
|
| 6674 |
int |
| 6675 |
Tcl_Ungets( |
| 6676 |
Tcl_Channel chan, /* The channel for which to add the input. */ |
| 6677 |
const char *str, /* The input itself. */ |
| 6678 |
int len, /* The length of the input. */ |
| 6679 |
int atEnd) /* If non-zero, add at end of queue; otherwise |
| 6680 |
* add at head of queue. */ |
| 6681 |
{ |
| 6682 |
Channel *chanPtr; /* The real IO channel. */ |
| 6683 |
ChannelState *statePtr; /* State of actual channel. */ |
| 6684 |
ChannelBuffer *bufPtr; /* Buffer to contain the data. */ |
| 6685 |
int flags; |
| 6686 |
|
| 6687 |
chanPtr = (Channel *) chan; |
| 6688 |
statePtr = chanPtr->state; |
| 6689 |
|
| 6690 |
/* |
| 6691 |
* This operation should occur at the top of a channel stack. |
| 6692 |
*/ |
| 6693 |
|
| 6694 |
chanPtr = statePtr->topChanPtr; |
| 6695 |
|
| 6696 |
/* |
| 6697 |
* CheckChannelErrors clears too many flag bits in this one case. |
| 6698 |
*/ |
| 6699 |
|
| 6700 |
flags = statePtr->flags; |
| 6701 |
if (CheckChannelErrors(statePtr, TCL_READABLE) != 0) { |
| 6702 |
len = -1; |
| 6703 |
goto done; |
| 6704 |
} |
| 6705 |
statePtr->flags = flags; |
| 6706 |
|
| 6707 |
/* |
| 6708 |
* Clear the EOF flags, and clear the BLOCKED bit. |
| 6709 |
*/ |
| 6710 |
|
| 6711 |
if (GotFlag(statePtr, CHANNEL_EOF)) { |
| 6712 |
statePtr->inputEncodingFlags |= TCL_ENCODING_START; |
| 6713 |
} |
| 6714 |
ResetFlag(statePtr, |
| 6715 |
CHANNEL_BLOCKED | CHANNEL_STICKY_EOF | CHANNEL_EOF | INPUT_SAW_CR); |
| 6716 |
statePtr->inputEncodingFlags &= ~TCL_ENCODING_END; |
| 6717 |
|
| 6718 |
bufPtr = AllocChannelBuffer(len); |
| 6719 |
memcpy(InsertPoint(bufPtr), str, len); |
| 6720 |
bufPtr->nextAdded += len; |
| 6721 |
|
| 6722 |
if (statePtr->inQueueHead == NULL) { |
| 6723 |
bufPtr->nextPtr = NULL; |
| 6724 |
statePtr->inQueueHead = bufPtr; |
| 6725 |
statePtr->inQueueTail = bufPtr; |
| 6726 |
} else if (atEnd) { |
| 6727 |
bufPtr->nextPtr = NULL; |
| 6728 |
statePtr->inQueueTail->nextPtr = bufPtr; |
| 6729 |
statePtr->inQueueTail = bufPtr; |
| 6730 |
} else { |
| 6731 |
bufPtr->nextPtr = statePtr->inQueueHead; |
| 6732 |
statePtr->inQueueHead = bufPtr; |
| 6733 |
} |
| 6734 |
|
| 6735 |
/* |
| 6736 |
* Update the notifier state so we don't block while there is still data |
| 6737 |
* in the buffers. |
| 6738 |
*/ |
| 6739 |
|
| 6740 |
done: |
| 6741 |
UpdateInterest(chanPtr); |
| 6742 |
return len; |
| 6743 |
} |
| 6744 |
|
| 6745 |
/* |
| 6746 |
*---------------------------------------------------------------------- |
| 6747 |
* |
| 6748 |
* Tcl_Flush -- |
| 6749 |
* |
| 6750 |
* Flushes output data on a channel. |
| 6751 |
* |
| 6752 |
* Results: |
| 6753 |
* A standard Tcl result. |
| 6754 |
* |
| 6755 |
* Side effects: |
| 6756 |
* May flush output queued on this channel. |
| 6757 |
* |
| 6758 |
*---------------------------------------------------------------------- |
| 6759 |
*/ |
| 6760 |
|
| 6761 |
int |
| 6762 |
Tcl_Flush( |
| 6763 |
Tcl_Channel chan) /* The Channel to flush. */ |
| 6764 |
{ |
| 6765 |
int result; /* Of calling FlushChannel. */ |
| 6766 |
Channel *chanPtr = (Channel *) chan; |
| 6767 |
/* The actual channel. */ |
| 6768 |
ChannelState *statePtr = chanPtr->state; |
| 6769 |
/* State of actual channel. */ |
| 6770 |
|
| 6771 |
/* |
| 6772 |
* This operation should occur at the top of a channel stack. |
| 6773 |
*/ |
| 6774 |
|
| 6775 |
chanPtr = statePtr->topChanPtr; |
| 6776 |
|
| 6777 |
if (CheckChannelErrors(statePtr, TCL_WRITABLE) != 0) { |
| 6778 |
return TCL_ERROR; |
| 6779 |
} |
| 6780 |
|
| 6781 |
result = FlushChannel(NULL, chanPtr, 0); |
| 6782 |
if (result != 0) { |
| 6783 |
return TCL_ERROR; |
| 6784 |
} |
| 6785 |
|
| 6786 |
return TCL_OK; |
| 6787 |
} |
| 6788 |
|
| 6789 |
/* |
| 6790 |
*---------------------------------------------------------------------- |
| 6791 |
* |
| 6792 |
* DiscardInputQueued -- |
| 6793 |
* |
| 6794 |
* Discards any input read from the channel but not yet consumed by Tcl |
| 6795 |
* reading commands. |
| 6796 |
* |
| 6797 |
* Results: |
| 6798 |
* None. |
| 6799 |
* |
| 6800 |
* Side effects: |
| 6801 |
* May discard input from the channel. If discardLastBuffer is zero, |
| 6802 |
* leaves one buffer in place for back-filling. |
| 6803 |
* |
| 6804 |
*---------------------------------------------------------------------- |
| 6805 |
*/ |
| 6806 |
|
| 6807 |
static void |
| 6808 |
DiscardInputQueued( |
| 6809 |
ChannelState *statePtr, /* Channel on which to discard the queued |
| 6810 |
* input. */ |
| 6811 |
int discardSavedBuffers) /* If non-zero, discard all buffers including |
| 6812 |
* last one. */ |
| 6813 |
{ |
| 6814 |
ChannelBuffer *bufPtr, *nxtPtr; |
| 6815 |
/* Loop variables. */ |
| 6816 |
|
| 6817 |
bufPtr = statePtr->inQueueHead; |
| 6818 |
statePtr->inQueueHead = NULL; |
| 6819 |
statePtr->inQueueTail = NULL; |
| 6820 |
for (; bufPtr != NULL; bufPtr = nxtPtr) { |
| 6821 |
nxtPtr = bufPtr->nextPtr; |
| 6822 |
RecycleBuffer(statePtr, bufPtr, discardSavedBuffers); |
| 6823 |
} |
| 6824 |
|
| 6825 |
/* |
| 6826 |
* If discardSavedBuffers is nonzero, must also discard any previously |
| 6827 |
* saved buffer in the saveInBufPtr field. |
| 6828 |
*/ |
| 6829 |
|
| 6830 |
if (discardSavedBuffers && statePtr->saveInBufPtr != NULL) { |
| 6831 |
ReleaseChannelBuffer(statePtr->saveInBufPtr); |
| 6832 |
statePtr->saveInBufPtr = NULL; |
| 6833 |
} |
| 6834 |
} |
| 6835 |
|
| 6836 |
/* |
| 6837 |
*--------------------------------------------------------------------------- |
| 6838 |
* |
| 6839 |
* GetInput -- |
| 6840 |
* |
| 6841 |
* Reads input data from a device into a channel buffer. |
| 6842 |
* |
| 6843 |
* IMPORTANT! This routine is only called on a chanPtr argument |
| 6844 |
* that is the top channel of a stack! |
| 6845 |
* |
| 6846 |
* Results: |
| 6847 |
* The return value is the Posix error code if an error occurred while |
| 6848 |
* reading from the file, or 0 otherwise. |
| 6849 |
* |
| 6850 |
* Side effects: |
| 6851 |
* Reads from the underlying device. |
| 6852 |
* |
| 6853 |
*--------------------------------------------------------------------------- |
| 6854 |
*/ |
| 6855 |
|
| 6856 |
static int |
| 6857 |
GetInput( |
| 6858 |
Channel *chanPtr) /* Channel to read input from. */ |
| 6859 |
{ |
| 6860 |
int toRead; /* How much to read? */ |
| 6861 |
int result; /* Of calling driver. */ |
| 6862 |
int nread; /* How much was read from channel? */ |
| 6863 |
ChannelBuffer *bufPtr; /* New buffer to add to input queue. */ |
| 6864 |
ChannelState *statePtr = chanPtr->state; |
| 6865 |
/* State info for channel */ |
| 6866 |
|
| 6867 |
/* |
| 6868 |
* Verify that all callers know better than to call us when |
| 6869 |
* it's recorded that the next char waiting to be read is the |
| 6870 |
* eofchar. |
| 6871 |
*/ |
| 6872 |
|
| 6873 |
assert(!GotFlag(statePtr, CHANNEL_STICKY_EOF)); |
| 6874 |
|
| 6875 |
/* |
| 6876 |
* Prevent reading from a dead channel -- a channel that has been closed |
| 6877 |
* but not yet deallocated, which can happen if the exit handler for |
| 6878 |
* channel cleanup has run but the channel is still registered in some |
| 6879 |
* interpreter. |
| 6880 |
*/ |
| 6881 |
|
| 6882 |
if (CheckForDeadChannel(NULL, statePtr)) { |
| 6883 |
return EINVAL; |
| 6884 |
} |
| 6885 |
|
| 6886 |
/* |
| 6887 |
* WARNING: There was once a comment here claiming that it was a bad idea |
| 6888 |
* to make another call to the inputproc of a channel driver when EOF has |
| 6889 |
* already been detected on the channel. Through much of Tcl's history, |
| 6890 |
* this warning was then completely negated by having all (most?) read |
| 6891 |
* paths clear the EOF setting before reaching here. So we had a guard |
| 6892 |
* that was never triggered. |
| 6893 |
* |
| 6894 |
* Don't be tempted to restore the guard. Even if EOF is set on the |
| 6895 |
* channel, continue through and call the inputproc again. This is the |
| 6896 |
* way to enable the ability to [read] again beyond the EOF, which seems a |
| 6897 |
* strange thing to do, but for which use cases exist [Tcl Bug 5adc350683] |
| 6898 |
* and which may even be essential for channels representing things like |
| 6899 |
* ttys or other devices where the stream might take the logical form of a |
| 6900 |
* series of 'files' separated by an EOF condition. |
| 6901 |
* |
| 6902 |
* First check for more buffers in the pushback area of the topmost |
| 6903 |
* channel in the stack and use them. They can be the result of a |
| 6904 |
* transformation which went away without reading all the information |
| 6905 |
* placed in the area when it was stacked. |
| 6906 |
*/ |
| 6907 |
|
| 6908 |
if (chanPtr->inQueueHead != NULL) { |
| 6909 |
/* TODO: Tests to cover this. */ |
| 6910 |
assert(statePtr->inQueueHead == NULL); |
| 6911 |
|
| 6912 |
statePtr->inQueueHead = chanPtr->inQueueHead; |
| 6913 |
statePtr->inQueueTail = chanPtr->inQueueTail; |
| 6914 |
chanPtr->inQueueHead = NULL; |
| 6915 |
chanPtr->inQueueTail = NULL; |
| 6916 |
return 0; |
| 6917 |
} |
| 6918 |
|
| 6919 |
/* |
| 6920 |
* Nothing in the pushback area, fall back to the usual handling (driver, |
| 6921 |
* etc.) |
| 6922 |
*/ |
| 6923 |
|
| 6924 |
/* |
| 6925 |
* See if we can fill an existing buffer. If we can, read only as much as |
| 6926 |
* will fit in it. Otherwise allocate a new buffer, add it to the input |
| 6927 |
* queue and attempt to fill it to the max. |
| 6928 |
*/ |
| 6929 |
|
| 6930 |
bufPtr = statePtr->inQueueTail; |
| 6931 |
|
| 6932 |
if ((bufPtr == NULL) || IsBufferFull(bufPtr)) { |
| 6933 |
bufPtr = statePtr->saveInBufPtr; |
| 6934 |
statePtr->saveInBufPtr = NULL; |
| 6935 |
|
| 6936 |
/* |
| 6937 |
* Check the actual buffersize against the requested buffersize. |
| 6938 |
* Saved buffers of the wrong size are squashed. This is done to honor |
| 6939 |
* dynamic changes of the buffersize made by the user. |
| 6940 |
* |
| 6941 |
* TODO: Tests to cover this. |
| 6942 |
*/ |
| 6943 |
|
| 6944 |
if ((bufPtr != NULL) |
| 6945 |
&& (bufPtr->bufLength != statePtr->bufSize + BUFFER_PADDING)) { |
| 6946 |
ReleaseChannelBuffer(bufPtr); |
| 6947 |
bufPtr = NULL; |
| 6948 |
} |
| 6949 |
|
| 6950 |
if (bufPtr == NULL) { |
| 6951 |
bufPtr = AllocChannelBuffer(statePtr->bufSize); |
| 6952 |
} |
| 6953 |
bufPtr->nextPtr = NULL; |
| 6954 |
|
| 6955 |
toRead = SpaceLeft(bufPtr); |
| 6956 |
assert(toRead == statePtr->bufSize); |
| 6957 |
|
| 6958 |
if (statePtr->inQueueTail == NULL) { |
| 6959 |
statePtr->inQueueHead = bufPtr; |
| 6960 |
} else { |
| 6961 |
statePtr->inQueueTail->nextPtr = bufPtr; |
| 6962 |
} |
| 6963 |
statePtr->inQueueTail = bufPtr; |
| 6964 |
} else { |
| 6965 |
toRead = SpaceLeft(bufPtr); |
| 6966 |
} |
| 6967 |
|
| 6968 |
PreserveChannelBuffer(bufPtr); |
| 6969 |
nread = ChanRead(chanPtr, InsertPoint(bufPtr), toRead); |
| 6970 |
ReleaseChannelBuffer(bufPtr); |
| 6971 |
|
| 6972 |
if (nread < 0) { |
| 6973 |
result = Tcl_GetErrno(); |
| 6974 |
} else { |
| 6975 |
result = 0; |
| 6976 |
if (statePtr->inQueueTail != NULL) { |
| 6977 |
statePtr->inQueueTail->nextAdded += nread; |
| 6978 |
} |
| 6979 |
} |
| 6980 |
|
| 6981 |
return result; |
| 6982 |
} |
| 6983 |
|
| 6984 |
/* |
| 6985 |
*---------------------------------------------------------------------- |
| 6986 |
* |
| 6987 |
* Tcl_Seek -- |
| 6988 |
* |
| 6989 |
* Implements seeking on Tcl Channels. This is a public function so that |
| 6990 |
* other C facilities may be implemented on top of it. |
| 6991 |
* |
| 6992 |
* Results: |
| 6993 |
* The new access point or -1 on error. If error, use Tcl_GetErrno() to |
| 6994 |
* retrieve the POSIX error code for the error that occurred. |
| 6995 |
* |
| 6996 |
* Side effects: |
| 6997 |
* May flush output on the channel. May discard queued input. |
| 6998 |
* |
| 6999 |
*---------------------------------------------------------------------- |
| 7000 |
*/ |
| 7001 |
|
| 7002 |
Tcl_WideInt |
| 7003 |
Tcl_Seek( |
| 7004 |
Tcl_Channel chan, /* The channel on which to seek. */ |
| 7005 |
Tcl_WideInt offset, /* Offset to seek to. */ |
| 7006 |
int mode) /* Relative to which location to seek? */ |
| 7007 |
{ |
| 7008 |
Channel *chanPtr = (Channel *) chan; |
| 7009 |
/* The real IO channel. */ |
| 7010 |
ChannelState *statePtr = chanPtr->state; |
| 7011 |
/* State info for channel */ |
| 7012 |
int inputBuffered, outputBuffered; |
| 7013 |
/* # bytes held in buffers. */ |
| 7014 |
int result; /* Of device driver operations. */ |
| 7015 |
Tcl_WideInt curPos; /* Position on the device. */ |
| 7016 |
int wasAsync; /* Was the channel nonblocking before the seek |
| 7017 |
* operation? If so, must restore to |
| 7018 |
* non-blocking mode after the seek. */ |
| 7019 |
|
| 7020 |
if (CheckChannelErrors(statePtr, TCL_WRITABLE | TCL_READABLE) != 0) { |
| 7021 |
return -1; |
| 7022 |
} |
| 7023 |
|
| 7024 |
/* |
| 7025 |
* Disallow seek on dead channels - channels that have been closed but not |
| 7026 |
* yet been deallocated. Such channels can be found if the exit handler |
| 7027 |
* for channel cleanup has run but the channel is still registered in an |
| 7028 |
* interpreter. |
| 7029 |
*/ |
| 7030 |
|
| 7031 |
if (CheckForDeadChannel(NULL, statePtr)) { |
| 7032 |
return -1; |
| 7033 |
} |
| 7034 |
|
| 7035 |
/* |
| 7036 |
* This operation should occur at the top of a channel stack. |
| 7037 |
*/ |
| 7038 |
|
| 7039 |
chanPtr = statePtr->topChanPtr; |
| 7040 |
|
| 7041 |
/* |
| 7042 |
* Disallow seek on channels whose type does not have a seek procedure |
| 7043 |
* defined. This means that the channel does not support seeking. |
| 7044 |
*/ |
| 7045 |
|
| 7046 |
if (Tcl_ChannelSeekProc(chanPtr->typePtr) == NULL) { |
| 7047 |
Tcl_SetErrno(EINVAL); |
| 7048 |
return -1; |
| 7049 |
} |
| 7050 |
|
| 7051 |
/* |
| 7052 |
* Compute how much input and output is buffered. If both input and output |
| 7053 |
* is buffered, cannot compute the current position. |
| 7054 |
*/ |
| 7055 |
|
| 7056 |
inputBuffered = Tcl_InputBuffered(chan); |
| 7057 |
outputBuffered = Tcl_OutputBuffered(chan); |
| 7058 |
|
| 7059 |
if ((inputBuffered != 0) && (outputBuffered != 0)) { |
| 7060 |
Tcl_SetErrno(EFAULT); |
| 7061 |
return -1; |
| 7062 |
} |
| 7063 |
|
| 7064 |
/* |
| 7065 |
* If we are seeking relative to the current position, compute the |
| 7066 |
* corrected offset taking into account the amount of unread input. |
| 7067 |
*/ |
| 7068 |
|
| 7069 |
if (mode == SEEK_CUR) { |
| 7070 |
offset -= inputBuffered; |
| 7071 |
} |
| 7072 |
|
| 7073 |
/* |
| 7074 |
* Discard any queued input - this input should not be read after the |
| 7075 |
* seek. |
| 7076 |
*/ |
| 7077 |
|
| 7078 |
DiscardInputQueued(statePtr, 0); |
| 7079 |
|
| 7080 |
/* |
| 7081 |
* Reset EOF and BLOCKED flags. We invalidate them by moving the access |
| 7082 |
* point. Also clear CR related flags. |
| 7083 |
*/ |
| 7084 |
|
| 7085 |
if (GotFlag(statePtr, CHANNEL_EOF)) { |
| 7086 |
statePtr->inputEncodingFlags |= TCL_ENCODING_START; |
| 7087 |
} |
| 7088 |
ResetFlag(statePtr, CHANNEL_EOF | CHANNEL_STICKY_EOF | CHANNEL_BLOCKED | |
| 7089 |
INPUT_SAW_CR); |
| 7090 |
statePtr->inputEncodingFlags &= ~TCL_ENCODING_END; |
| 7091 |
|
| 7092 |
/* |
| 7093 |
* If the channel is in asynchronous output mode, switch it back to |
| 7094 |
* synchronous mode and cancel any async flush that may be scheduled. |
| 7095 |
* After the flush, the channel will be put back into asynchronous output |
| 7096 |
* mode. |
| 7097 |
*/ |
| 7098 |
|
| 7099 |
wasAsync = 0; |
| 7100 |
if (GotFlag(statePtr, CHANNEL_NONBLOCKING)) { |
| 7101 |
wasAsync = 1; |
| 7102 |
result = StackSetBlockMode(chanPtr, TCL_MODE_BLOCKING); |
| 7103 |
if (result != 0) { |
| 7104 |
return -1; |
| 7105 |
} |
| 7106 |
ResetFlag(statePtr, CHANNEL_NONBLOCKING); |
| 7107 |
if (GotFlag(statePtr, BG_FLUSH_SCHEDULED)) { |
| 7108 |
ResetFlag(statePtr, BG_FLUSH_SCHEDULED); |
| 7109 |
} |
| 7110 |
} |
| 7111 |
|
| 7112 |
/* |
| 7113 |
* If the flush fails we cannot recover the original position. In that |
| 7114 |
* case the seek is not attempted because we do not know where the access |
| 7115 |
* position is - instead we return the error. FlushChannel has already |
| 7116 |
* called Tcl_SetErrno() to report the error upwards. If the flush |
| 7117 |
* succeeds we do the seek also. |
| 7118 |
*/ |
| 7119 |
|
| 7120 |
if (FlushChannel(NULL, chanPtr, 0) != 0) { |
| 7121 |
curPos = -1; |
| 7122 |
} else { |
| 7123 |
/* |
| 7124 |
* Now seek to the new position in the channel as requested by the |
| 7125 |
* caller. |
| 7126 |
*/ |
| 7127 |
|
| 7128 |
curPos = ChanSeek(chanPtr, offset, mode, &result); |
| 7129 |
if (curPos == -1) { |
| 7130 |
Tcl_SetErrno(result); |
| 7131 |
} |
| 7132 |
} |
| 7133 |
|
| 7134 |
/* |
| 7135 |
* Restore to nonblocking mode if that was the previous behavior. |
| 7136 |
* |
| 7137 |
* NOTE: Even if there was an async flush active we do not restore it now |
| 7138 |
* because we already flushed all the queued output, above. |
| 7139 |
*/ |
| 7140 |
|
| 7141 |
if (wasAsync) { |
| 7142 |
SetFlag(statePtr, CHANNEL_NONBLOCKING); |
| 7143 |
result = StackSetBlockMode(chanPtr, TCL_MODE_NONBLOCKING); |
| 7144 |
if (result != 0) { |
| 7145 |
return -1; |
| 7146 |
} |
| 7147 |
} |
| 7148 |
|
| 7149 |
return curPos; |
| 7150 |
} |
| 7151 |
|
| 7152 |
/* |
| 7153 |
*---------------------------------------------------------------------- |
| 7154 |
* |
| 7155 |
* Tcl_Tell -- |
| 7156 |
* |
| 7157 |
* Returns the position of the next character to be read/written on this |
| 7158 |
* channel. |
| 7159 |
* |
| 7160 |
* Results: |
| 7161 |
* A nonnegative integer on success, -1 on failure. If failed, use |
| 7162 |
* Tcl_GetErrno() to retrieve the POSIX error code for the error that |
| 7163 |
* occurred. |
| 7164 |
* |
| 7165 |
* Side effects: |
| 7166 |
* None. |
| 7167 |
* |
| 7168 |
*---------------------------------------------------------------------- |
| 7169 |
*/ |
| 7170 |
|
| 7171 |
Tcl_WideInt |
| 7172 |
Tcl_Tell( |
| 7173 |
Tcl_Channel chan) /* The channel to return pos for. */ |
| 7174 |
{ |
| 7175 |
Channel *chanPtr = (Channel *) chan; |
| 7176 |
/* The real IO channel. */ |
| 7177 |
ChannelState *statePtr = chanPtr->state; |
| 7178 |
/* State info for channel */ |
| 7179 |
int inputBuffered, outputBuffered; |
| 7180 |
/* # bytes held in buffers. */ |
| 7181 |
int result; /* Of calling device driver. */ |
| 7182 |
Tcl_WideInt curPos; /* Position on device. */ |
| 7183 |
|
| 7184 |
if (CheckChannelErrors(statePtr, TCL_WRITABLE | TCL_READABLE) != 0) { |
| 7185 |
return -1; |
| 7186 |
} |
| 7187 |
|
| 7188 |
/* |
| 7189 |
* Disallow tell on dead channels -- channels that have been closed but |
| 7190 |
* not yet been deallocated. Such channels can be found if the exit |
| 7191 |
* handler for channel cleanup has run but the channel is still registered |
| 7192 |
* in an interpreter. |
| 7193 |
*/ |
| 7194 |
|
| 7195 |
if (CheckForDeadChannel(NULL, statePtr)) { |
| 7196 |
return -1; |
| 7197 |
} |
| 7198 |
|
| 7199 |
/* |
| 7200 |
* This operation should occur at the top of a channel stack. |
| 7201 |
*/ |
| 7202 |
|
| 7203 |
chanPtr = statePtr->topChanPtr; |
| 7204 |
|
| 7205 |
/* |
| 7206 |
* Disallow tell on channels whose type does not have a seek procedure |
| 7207 |
* defined. This means that the channel does not support seeking. |
| 7208 |
*/ |
| 7209 |
|
| 7210 |
if (Tcl_ChannelSeekProc(chanPtr->typePtr) == NULL) { |
| 7211 |
Tcl_SetErrno(EINVAL); |
| 7212 |
return -1; |
| 7213 |
} |
| 7214 |
|
| 7215 |
/* |
| 7216 |
* Compute how much input and output is buffered. If both input and output |
| 7217 |
* is buffered, cannot compute the current position. |
| 7218 |
*/ |
| 7219 |
|
| 7220 |
inputBuffered = Tcl_InputBuffered(chan); |
| 7221 |
outputBuffered = Tcl_OutputBuffered(chan); |
| 7222 |
|
| 7223 |
/* |
| 7224 |
* Get the current position in the device and compute the position where |
| 7225 |
* the next character will be read or written. Note that we prefer the |
| 7226 |
* wideSeekProc if that is available and non-NULL... |
| 7227 |
*/ |
| 7228 |
|
| 7229 |
curPos = ChanSeek(chanPtr, 0, SEEK_CUR, &result); |
| 7230 |
if (curPos == -1) { |
| 7231 |
Tcl_SetErrno(result); |
| 7232 |
return -1; |
| 7233 |
} |
| 7234 |
|
| 7235 |
if (inputBuffered != 0) { |
| 7236 |
return curPos - inputBuffered; |
| 7237 |
} |
| 7238 |
return curPos + outputBuffered; |
| 7239 |
} |
| 7240 |
|
| 7241 |
/* |
| 7242 |
*--------------------------------------------------------------------------- |
| 7243 |
* |
| 7244 |
* Tcl_SeekOld, Tcl_TellOld -- |
| 7245 |
* |
| 7246 |
* Backward-compatibility versions of the seek/tell interface that do not |
| 7247 |
* support 64-bit offsets. This interface is not documented or expected |
| 7248 |
* to be supported indefinitely. |
| 7249 |
* |
| 7250 |
* Results: |
| 7251 |
* As for Tcl_Seek and Tcl_Tell respectively, except truncated to |
| 7252 |
* whatever value will fit in an 'int'. |
| 7253 |
* |
| 7254 |
* Side effects: |
| 7255 |
* As for Tcl_Seek and Tcl_Tell respectively. |
| 7256 |
* |
| 7257 |
*--------------------------------------------------------------------------- |
| 7258 |
*/ |
| 7259 |
|
| 7260 |
int |
| 7261 |
Tcl_SeekOld( |
| 7262 |
Tcl_Channel chan, /* The channel on which to seek. */ |
| 7263 |
int offset, /* Offset to seek to. */ |
| 7264 |
int mode) /* Relative to which location to seek? */ |
| 7265 |
{ |
| 7266 |
Tcl_WideInt wOffset, wResult; |
| 7267 |
|
| 7268 |
wOffset = Tcl_LongAsWide((long) offset); |
| 7269 |
wResult = Tcl_Seek(chan, wOffset, mode); |
| 7270 |
return (int) Tcl_WideAsLong(wResult); |
| 7271 |
} |
| 7272 |
|
| 7273 |
int |
| 7274 |
Tcl_TellOld( |
| 7275 |
Tcl_Channel chan) /* The channel to return pos for. */ |
| 7276 |
{ |
| 7277 |
Tcl_WideInt wResult = Tcl_Tell(chan); |
| 7278 |
|
| 7279 |
return (int) Tcl_WideAsLong(wResult); |
| 7280 |
} |
| 7281 |
|
| 7282 |
/* |
| 7283 |
*--------------------------------------------------------------------------- |
| 7284 |
* |
| 7285 |
* Tcl_TruncateChannel -- |
| 7286 |
* |
| 7287 |
* Truncate a channel to the given length. |
| 7288 |
* |
| 7289 |
* Results: |
| 7290 |
* TCL_OK on success, TCL_ERROR if the operation failed (e.g., is not |
| 7291 |
* supported by the type of channel, or the underlying OS operation |
| 7292 |
* failed in some way). |
| 7293 |
* |
| 7294 |
* Side effects: |
| 7295 |
* Seeks the channel to the current location. Sets errno on OS error. |
| 7296 |
* |
| 7297 |
*--------------------------------------------------------------------------- |
| 7298 |
*/ |
| 7299 |
|
| 7300 |
int |
| 7301 |
Tcl_TruncateChannel( |
| 7302 |
Tcl_Channel chan, /* Channel to truncate. */ |
| 7303 |
Tcl_WideInt length) /* Length to truncate it to. */ |
| 7304 |
{ |
| 7305 |
Channel *chanPtr = (Channel *) chan; |
| 7306 |
Tcl_DriverTruncateProc *truncateProc = |
| 7307 |
Tcl_ChannelTruncateProc(chanPtr->typePtr); |
| 7308 |
int result; |
| 7309 |
|
| 7310 |
if (truncateProc == NULL) { |
| 7311 |
/* |
| 7312 |
* Feature not supported and it's not emulatable. Pretend it's |
| 7313 |
* returned an EINVAL, a very generic error! |
| 7314 |
*/ |
| 7315 |
|
| 7316 |
Tcl_SetErrno(EINVAL); |
| 7317 |
return TCL_ERROR; |
| 7318 |
} |
| 7319 |
|
| 7320 |
if (!GotFlag(chanPtr->state, TCL_WRITABLE)) { |
| 7321 |
/* |
| 7322 |
* We require that the file was opened of writing. Do that check now |
| 7323 |
* so that we only flush if we think we're going to succeed. |
| 7324 |
*/ |
| 7325 |
|
| 7326 |
Tcl_SetErrno(EINVAL); |
| 7327 |
return TCL_ERROR; |
| 7328 |
} |
| 7329 |
|
| 7330 |
/* |
| 7331 |
* Seek first to force a total flush of all pending buffers and ditch any |
| 7332 |
* preread input data. |
| 7333 |
*/ |
| 7334 |
|
| 7335 |
WillWrite(chanPtr); |
| 7336 |
|
| 7337 |
if (WillRead(chanPtr) < 0) { |
| 7338 |
return TCL_ERROR; |
| 7339 |
} |
| 7340 |
|
| 7341 |
/* |
| 7342 |
* We're all flushed to disk now and we also don't have any unfortunate |
| 7343 |
* input baggage around either; can truncate with impunity. |
| 7344 |
*/ |
| 7345 |
|
| 7346 |
result = truncateProc(chanPtr->instanceData, length); |
| 7347 |
if (result != 0) { |
| 7348 |
Tcl_SetErrno(result); |
| 7349 |
return TCL_ERROR; |
| 7350 |
} |
| 7351 |
return TCL_OK; |
| 7352 |
} |
| 7353 |
|
| 7354 |
/* |
| 7355 |
*--------------------------------------------------------------------------- |
| 7356 |
* |
| 7357 |
* CheckChannelErrors -- |
| 7358 |
* |
| 7359 |
* See if the channel is in an ready state and can perform the desired |
| 7360 |
* operation. |
| 7361 |
* |
| 7362 |
* Results: |
| 7363 |
* The return value is 0 if the channel is OK, otherwise the return value |
| 7364 |
* is -1 and errno is set to indicate the error. |
| 7365 |
* |
| 7366 |
* Side effects: |
| 7367 |
* May clear the EOF and/or BLOCKED bits if reading from channel. |
| 7368 |
* |
| 7369 |
*--------------------------------------------------------------------------- |
| 7370 |
*/ |
| 7371 |
|
| 7372 |
static int |
| 7373 |
CheckChannelErrors( |
| 7374 |
ChannelState *statePtr, /* Channel to check. */ |
| 7375 |
int flags) /* Test if channel supports desired operation: |
| 7376 |
* TCL_READABLE, TCL_WRITABLE. Also indicates |
| 7377 |
* Raw read or write for special close |
| 7378 |
* processing */ |
| 7379 |
{ |
| 7380 |
int direction = flags & (TCL_READABLE|TCL_WRITABLE); |
| 7381 |
|
| 7382 |
/* |
| 7383 |
* Check for unreported error. |
| 7384 |
*/ |
| 7385 |
|
| 7386 |
if (statePtr->unreportedError != 0) { |
| 7387 |
Tcl_SetErrno(statePtr->unreportedError); |
| 7388 |
statePtr->unreportedError = 0; |
| 7389 |
|
| 7390 |
/* |
| 7391 |
* TIP #219, Tcl Channel Reflection API. |
| 7392 |
* Move a deferred error message back into the channel bypass. |
| 7393 |
*/ |
| 7394 |
|
| 7395 |
if (statePtr->chanMsg != NULL) { |
| 7396 |
TclDecrRefCount(statePtr->chanMsg); |
| 7397 |
} |
| 7398 |
statePtr->chanMsg = statePtr->unreportedMsg; |
| 7399 |
statePtr->unreportedMsg = NULL; |
| 7400 |
return -1; |
| 7401 |
} |
| 7402 |
|
| 7403 |
/* |
| 7404 |
* Only the raw read and write operations are allowed during close in |
| 7405 |
* order to drain data from stacked channels. |
| 7406 |
*/ |
| 7407 |
|
| 7408 |
if (GotFlag(statePtr, CHANNEL_CLOSED) && !(flags & CHANNEL_RAW_MODE)) { |
| 7409 |
Tcl_SetErrno(EACCES); |
| 7410 |
return -1; |
| 7411 |
} |
| 7412 |
|
| 7413 |
/* |
| 7414 |
* Fail if the channel is not opened for desired operation. |
| 7415 |
*/ |
| 7416 |
|
| 7417 |
if (GotFlag(statePtr, direction) == 0) { |
| 7418 |
Tcl_SetErrno(EACCES); |
| 7419 |
return -1; |
| 7420 |
} |
| 7421 |
|
| 7422 |
/* |
| 7423 |
* Fail if the channel is in the middle of a background copy. |
| 7424 |
* |
| 7425 |
* Don't do this tests for raw channels here or else the chaining in the |
| 7426 |
* transformation drivers will fail with 'file busy' error instead of |
| 7427 |
* retrieving and transforming the data to copy. |
| 7428 |
*/ |
| 7429 |
|
| 7430 |
if (BUSY_STATE(statePtr, flags) && ((flags & CHANNEL_RAW_MODE) == 0)) { |
| 7431 |
Tcl_SetErrno(EBUSY); |
| 7432 |
return -1; |
| 7433 |
} |
| 7434 |
|
| 7435 |
if (direction == TCL_READABLE) { |
| 7436 |
ResetFlag(statePtr, CHANNEL_NEED_MORE_DATA); |
| 7437 |
} |
| 7438 |
|
| 7439 |
return 0; |
| 7440 |
} |
| 7441 |
|
| 7442 |
/* |
| 7443 |
*---------------------------------------------------------------------- |
| 7444 |
* |
| 7445 |
* Tcl_Eof -- |
| 7446 |
* |
| 7447 |
* Returns 1 if the channel is at EOF, 0 otherwise. |
| 7448 |
* |
| 7449 |
* Results: |
| 7450 |
* 1 or 0, always. |
| 7451 |
* |
| 7452 |
* Side effects: |
| 7453 |
* None. |
| 7454 |
* |
| 7455 |
*---------------------------------------------------------------------- |
| 7456 |
*/ |
| 7457 |
|
| 7458 |
int |
| 7459 |
Tcl_Eof( |
| 7460 |
Tcl_Channel chan) /* Does this channel have EOF? */ |
| 7461 |
{ |
| 7462 |
ChannelState *statePtr = ((Channel *) chan)->state; |
| 7463 |
/* State of real channel structure. */ |
| 7464 |
|
| 7465 |
return GotFlag(statePtr, CHANNEL_EOF) ? 1 : 0; |
| 7466 |
} |
| 7467 |
|
| 7468 |
/* |
| 7469 |
*---------------------------------------------------------------------- |
| 7470 |
* |
| 7471 |
* Tcl_InputBlocked -- |
| 7472 |
* |
| 7473 |
* Returns 1 if input is blocked on this channel, 0 otherwise. |
| 7474 |
* |
| 7475 |
* Results: |
| 7476 |
* 0 or 1, always. |
| 7477 |
* |
| 7478 |
* Side effects: |
| 7479 |
* None. |
| 7480 |
* |
| 7481 |
*---------------------------------------------------------------------- |
| 7482 |
*/ |
| 7483 |
|
| 7484 |
int |
| 7485 |
Tcl_InputBlocked( |
| 7486 |
Tcl_Channel chan) /* Is this channel blocked? */ |
| 7487 |
{ |
| 7488 |
ChannelState *statePtr = ((Channel *) chan)->state; |
| 7489 |
/* State of real channel structure. */ |
| 7490 |
|
| 7491 |
return GotFlag(statePtr, CHANNEL_BLOCKED) ? 1 : 0; |
| 7492 |
} |
| 7493 |
|
| 7494 |
/* |
| 7495 |
*---------------------------------------------------------------------- |
| 7496 |
* |
| 7497 |
* Tcl_InputBuffered -- |
| 7498 |
* |
| 7499 |
* Returns the number of bytes of input currently buffered in the common |
| 7500 |
* internal buffer of a channel. |
| 7501 |
* |
| 7502 |
* Results: |
| 7503 |
* The number of input bytes buffered, or zero if the channel is not open |
| 7504 |
* for reading. |
| 7505 |
* |
| 7506 |
* Side effects: |
| 7507 |
* None. |
| 7508 |
* |
| 7509 |
*---------------------------------------------------------------------- |
| 7510 |
*/ |
| 7511 |
|
| 7512 |
int |
| 7513 |
Tcl_InputBuffered( |
| 7514 |
Tcl_Channel chan) /* The channel to query. */ |
| 7515 |
{ |
| 7516 |
ChannelState *statePtr = ((Channel *) chan)->state; |
| 7517 |
/* State of real channel structure. */ |
| 7518 |
ChannelBuffer *bufPtr; |
| 7519 |
int bytesBuffered; |
| 7520 |
|
| 7521 |
for (bytesBuffered = 0, bufPtr = statePtr->inQueueHead; bufPtr != NULL; |
| 7522 |
bufPtr = bufPtr->nextPtr) { |
| 7523 |
bytesBuffered += BytesLeft(bufPtr); |
| 7524 |
} |
| 7525 |
|
| 7526 |
/* |
| 7527 |
* Remember the bytes in the topmost pushback area. |
| 7528 |
*/ |
| 7529 |
|
| 7530 |
for (bufPtr = statePtr->topChanPtr->inQueueHead; bufPtr != NULL; |
| 7531 |
bufPtr = bufPtr->nextPtr) { |
| 7532 |
bytesBuffered += BytesLeft(bufPtr); |
| 7533 |
} |
| 7534 |
|
| 7535 |
return bytesBuffered; |
| 7536 |
} |
| 7537 |
|
| 7538 |
/* |
| 7539 |
*---------------------------------------------------------------------- |
| 7540 |
* |
| 7541 |
* Tcl_OutputBuffered -- |
| 7542 |
* |
| 7543 |
* Returns the number of bytes of output currently buffered in the common |
| 7544 |
* internal buffer of a channel. |
| 7545 |
* |
| 7546 |
* Results: |
| 7547 |
* The number of output bytes buffered, or zero if the channel is not open |
| 7548 |
* for writing. |
| 7549 |
* |
| 7550 |
* Side effects: |
| 7551 |
* None. |
| 7552 |
* |
| 7553 |
*---------------------------------------------------------------------- |
| 7554 |
*/ |
| 7555 |
|
| 7556 |
int |
| 7557 |
Tcl_OutputBuffered( |
| 7558 |
Tcl_Channel chan) /* The channel to query. */ |
| 7559 |
{ |
| 7560 |
ChannelState *statePtr = ((Channel *) chan)->state; |
| 7561 |
/* State of real channel structure. */ |
| 7562 |
ChannelBuffer *bufPtr; |
| 7563 |
int bytesBuffered; |
| 7564 |
|
| 7565 |
for (bytesBuffered = 0, bufPtr = statePtr->outQueueHead; bufPtr != NULL; |
| 7566 |
bufPtr = bufPtr->nextPtr) { |
| 7567 |
bytesBuffered += BytesLeft(bufPtr); |
| 7568 |
} |
| 7569 |
if (statePtr->curOutPtr != NULL) { |
| 7570 |
ChannelBuffer *curOutPtr = statePtr->curOutPtr; |
| 7571 |
|
| 7572 |
if (IsBufferReady(curOutPtr)) { |
| 7573 |
bytesBuffered += BytesLeft(curOutPtr); |
| 7574 |
} |
| 7575 |
} |
| 7576 |
|
| 7577 |
return bytesBuffered; |
| 7578 |
} |
| 7579 |
|
| 7580 |
/* |
| 7581 |
*---------------------------------------------------------------------- |
| 7582 |
* |
| 7583 |
* Tcl_ChannelBuffered -- |
| 7584 |
* |
| 7585 |
* Returns the number of bytes of input currently buffered in the |
| 7586 |
* internal buffer (push back area) of a channel. |
| 7587 |
* |
| 7588 |
* Results: |
| 7589 |
* The number of input bytes buffered, or zero if the channel is not open |
| 7590 |
* for reading. |
| 7591 |
* |
| 7592 |
* Side effects: |
| 7593 |
* None. |
| 7594 |
* |
| 7595 |
*---------------------------------------------------------------------- |
| 7596 |
*/ |
| 7597 |
|
| 7598 |
int |
| 7599 |
Tcl_ChannelBuffered( |
| 7600 |
Tcl_Channel chan) /* The channel to query. */ |
| 7601 |
{ |
| 7602 |
Channel *chanPtr = (Channel *) chan; |
| 7603 |
/* Real channel structure. */ |
| 7604 |
ChannelBuffer *bufPtr; |
| 7605 |
int bytesBuffered = 0; |
| 7606 |
|
| 7607 |
for (bufPtr = chanPtr->inQueueHead; bufPtr != NULL; |
| 7608 |
bufPtr = bufPtr->nextPtr) { |
| 7609 |
bytesBuffered += BytesLeft(bufPtr); |
| 7610 |
} |
| 7611 |
|
| 7612 |
return bytesBuffered; |
| 7613 |
} |
| 7614 |
|
| 7615 |
/* |
| 7616 |
*---------------------------------------------------------------------- |
| 7617 |
* |
| 7618 |
* Tcl_SetChannelBufferSize -- |
| 7619 |
* |
| 7620 |
* Sets the size of buffers to allocate to store input or output in the |
| 7621 |
* channel. The size must be between 1 byte and 1 MByte. |
| 7622 |
* |
| 7623 |
* Results: |
| 7624 |
* None. |
| 7625 |
* |
| 7626 |
* Side effects: |
| 7627 |
* Sets the size of buffers subsequently allocated for this channel. |
| 7628 |
* |
| 7629 |
*---------------------------------------------------------------------- |
| 7630 |
*/ |
| 7631 |
|
| 7632 |
void |
| 7633 |
Tcl_SetChannelBufferSize( |
| 7634 |
Tcl_Channel chan, /* The channel whose buffer size to set. */ |
| 7635 |
int sz) /* The size to set. */ |
| 7636 |
{ |
| 7637 |
ChannelState *statePtr; /* State of real channel structure. */ |
| 7638 |
|
| 7639 |
/* |
| 7640 |
* Clip the buffer size to force it into the [1,1M] range |
| 7641 |
*/ |
| 7642 |
|
| 7643 |
if (sz < 1) { |
| 7644 |
sz = 1; |
| 7645 |
} else if (sz > MAX_CHANNEL_BUFFER_SIZE) { |
| 7646 |
sz = MAX_CHANNEL_BUFFER_SIZE; |
| 7647 |
} |
| 7648 |
|
| 7649 |
statePtr = ((Channel *) chan)->state; |
| 7650 |
|
| 7651 |
if (statePtr->bufSize == sz) { |
| 7652 |
return; |
| 7653 |
} |
| 7654 |
statePtr->bufSize = sz; |
| 7655 |
|
| 7656 |
/* |
| 7657 |
* If bufsize changes, need to get rid of old utility buffer. |
| 7658 |
*/ |
| 7659 |
|
| 7660 |
if (statePtr->saveInBufPtr != NULL) { |
| 7661 |
RecycleBuffer(statePtr, statePtr->saveInBufPtr, 1); |
| 7662 |
statePtr->saveInBufPtr = NULL; |
| 7663 |
} |
| 7664 |
if ((statePtr->inQueueHead != NULL) |
| 7665 |
&& (statePtr->inQueueHead->nextPtr == NULL) |
| 7666 |
&& IsBufferEmpty(statePtr->inQueueHead)) { |
| 7667 |
RecycleBuffer(statePtr, statePtr->inQueueHead, 1); |
| 7668 |
statePtr->inQueueHead = NULL; |
| 7669 |
statePtr->inQueueTail = NULL; |
| 7670 |
} |
| 7671 |
} |
| 7672 |
|
| 7673 |
/* |
| 7674 |
*---------------------------------------------------------------------- |
| 7675 |
* |
| 7676 |
* Tcl_GetChannelBufferSize -- |
| 7677 |
* |
| 7678 |
* Retrieves the size of buffers to allocate for this channel. |
| 7679 |
* |
| 7680 |
* Results: |
| 7681 |
* The size. |
| 7682 |
* |
| 7683 |
* Side effects: |
| 7684 |
* None. |
| 7685 |
* |
| 7686 |
*---------------------------------------------------------------------- |
| 7687 |
*/ |
| 7688 |
|
| 7689 |
int |
| 7690 |
Tcl_GetChannelBufferSize( |
| 7691 |
Tcl_Channel chan) /* The channel for which to find the buffer |
| 7692 |
* size. */ |
| 7693 |
{ |
| 7694 |
ChannelState *statePtr = ((Channel *) chan)->state; |
| 7695 |
/* State of real channel structure. */ |
| 7696 |
|
| 7697 |
return statePtr->bufSize; |
| 7698 |
} |
| 7699 |
|
| 7700 |
/* |
| 7701 |
*---------------------------------------------------------------------- |
| 7702 |
* |
| 7703 |
* Tcl_BadChannelOption -- |
| 7704 |
* |
| 7705 |
* This procedure generates a "bad option" error message in an (optional) |
| 7706 |
* interpreter. It is used by channel drivers when a invalid Set/Get |
| 7707 |
* option is requested. Its purpose is to concatenate the generic options |
| 7708 |
* list to the specific ones and factorize the generic options error |
| 7709 |
* message string. |
| 7710 |
* |
| 7711 |
* Results: |
| 7712 |
* TCL_ERROR. |
| 7713 |
* |
| 7714 |
* Side effects: |
| 7715 |
|
| 7716 |
* An error message is generated in interp's result object to indicate |
| 7717 |
* that a command was invoked with a bad option. The message has the |
| 7718 |
* form: |
| 7719 |
* bad option "blah": should be one of |
| 7720 |
* <...generic options...>+<...specific options...> |
| 7721 |
* "blah" is the optionName argument and "<specific options>" is a space |
| 7722 |
* separated list of specific option words. The function takes good care |
| 7723 |
* of inserting minus signs before each option, commas after, and an "or" |
| 7724 |
* before the last option. |
| 7725 |
* |
| 7726 |
*---------------------------------------------------------------------- |
| 7727 |
*/ |
| 7728 |
|
| 7729 |
int |
| 7730 |
Tcl_BadChannelOption( |
| 7731 |
Tcl_Interp *interp, /* Current interpreter (can be NULL).*/ |
| 7732 |
const char *optionName, /* 'bad option' name */ |
| 7733 |
const char *optionList) /* Specific options list to append to the |
| 7734 |
* standard generic options. Can be NULL for |
| 7735 |
* generic options only. */ |
| 7736 |
{ |
| 7737 |
if (interp != NULL) { |
| 7738 |
const char *genericopt = |
| 7739 |
"blocking buffering buffersize encoding eofchar translation"; |
| 7740 |
const char **argv; |
| 7741 |
int argc, i; |
| 7742 |
Tcl_DString ds; |
| 7743 |
Tcl_Obj *errObj; |
| 7744 |
|
| 7745 |
Tcl_DStringInit(&ds); |
| 7746 |
Tcl_DStringAppend(&ds, genericopt, -1); |
| 7747 |
if (optionList && (*optionList)) { |
| 7748 |
TclDStringAppendLiteral(&ds, " "); |
| 7749 |
Tcl_DStringAppend(&ds, optionList, -1); |
| 7750 |
} |
| 7751 |
if (Tcl_SplitList(interp, Tcl_DStringValue(&ds), |
| 7752 |
&argc, &argv) != TCL_OK) { |
| 7753 |
Tcl_Panic("malformed option list in channel driver"); |
| 7754 |
} |
| 7755 |
Tcl_ResetResult(interp); |
| 7756 |
errObj = Tcl_ObjPrintf("bad option \"%s\": should be one of ", |
| 7757 |
optionName ? optionName : ""); |
| 7758 |
argc--; |
| 7759 |
for (i = 0; i < argc; i++) { |
| 7760 |
Tcl_AppendPrintfToObj(errObj, "-%s, ", argv[i]); |
| 7761 |
} |
| 7762 |
Tcl_AppendPrintfToObj(errObj, "or -%s", argv[i]); |
| 7763 |
Tcl_SetObjResult(interp, errObj); |
| 7764 |
Tcl_DStringFree(&ds); |
| 7765 |
ckfree(argv); |
| 7766 |
} |
| 7767 |
Tcl_SetErrno(EINVAL); |
| 7768 |
return TCL_ERROR; |
| 7769 |
} |
| 7770 |
|
| 7771 |
/* |
| 7772 |
*---------------------------------------------------------------------- |
| 7773 |
* |
| 7774 |
* Tcl_GetChannelOption -- |
| 7775 |
* |
| 7776 |
* Gets a mode associated with an IO channel. If the optionName arg is |
| 7777 |
* non NULL, retrieves the value of that option. If the optionName arg is |
| 7778 |
* NULL, retrieves a list of alternating option names and values for the |
| 7779 |
* given channel. |
| 7780 |
* |
| 7781 |
* Results: |
| 7782 |
* A standard Tcl result. Also sets the supplied DString to the string |
| 7783 |
* value of the option(s) returned. |
| 7784 |
* |
| 7785 |
* Side effects: |
| 7786 |
* None. |
| 7787 |
* |
| 7788 |
*---------------------------------------------------------------------- |
| 7789 |
*/ |
| 7790 |
|
| 7791 |
int |
| 7792 |
Tcl_GetChannelOption( |
| 7793 |
Tcl_Interp *interp, /* For error reporting - can be NULL. */ |
| 7794 |
Tcl_Channel chan, /* Channel on which to get option. */ |
| 7795 |
const char *optionName, /* Option to get. */ |
| 7796 |
Tcl_DString *dsPtr) /* Where to store value(s). */ |
| 7797 |
{ |
| 7798 |
size_t len; /* Length of optionName string. */ |
| 7799 |
char optionVal[128]; /* Buffer for snprintf. */ |
| 7800 |
Channel *chanPtr = (Channel *) chan; |
| 7801 |
ChannelState *statePtr = chanPtr->state; |
| 7802 |
/* State info for channel */ |
| 7803 |
int flags; |
| 7804 |
|
| 7805 |
/* |
| 7806 |
* Disallow options on dead channels -- channels that have been closed but |
| 7807 |
* not yet been deallocated. Such channels can be found if the exit |
| 7808 |
* handler for channel cleanup has run but the channel is still registered |
| 7809 |
* in an interpreter. |
| 7810 |
*/ |
| 7811 |
|
| 7812 |
if (CheckForDeadChannel(interp, statePtr)) { |
| 7813 |
return TCL_ERROR; |
| 7814 |
} |
| 7815 |
|
| 7816 |
/* |
| 7817 |
* This operation should occur at the top of a channel stack. |
| 7818 |
*/ |
| 7819 |
|
| 7820 |
chanPtr = statePtr->topChanPtr; |
| 7821 |
|
| 7822 |
/* |
| 7823 |
* If we are in the middle of a background copy, use the saved flags. |
| 7824 |
*/ |
| 7825 |
|
| 7826 |
if (statePtr->csPtrR) { |
| 7827 |
flags = statePtr->csPtrR->readFlags; |
| 7828 |
} else if (statePtr->csPtrW) { |
| 7829 |
flags = statePtr->csPtrW->writeFlags; |
| 7830 |
} else { |
| 7831 |
flags = statePtr->flags; |
| 7832 |
} |
| 7833 |
|
| 7834 |
/* |
| 7835 |
* If the optionName is NULL it means that we want a list of all options |
| 7836 |
* and values. |
| 7837 |
*/ |
| 7838 |
|
| 7839 |
if (optionName == NULL) { |
| 7840 |
len = 0; |
| 7841 |
} else { |
| 7842 |
len = strlen(optionName); |
| 7843 |
} |
| 7844 |
|
| 7845 |
if (len == 0 || HaveOpt(2, "-blocking")) { |
| 7846 |
if (len == 0) { |
| 7847 |
Tcl_DStringAppendElement(dsPtr, "-blocking"); |
| 7848 |
} |
| 7849 |
Tcl_DStringAppendElement(dsPtr, |
| 7850 |
(flags & CHANNEL_NONBLOCKING) ? "0" : "1"); |
| 7851 |
if (len > 0) { |
| 7852 |
return TCL_OK; |
| 7853 |
} |
| 7854 |
} |
| 7855 |
if (len == 0 || HaveOpt(7, "-buffering")) { |
| 7856 |
if (len == 0) { |
| 7857 |
Tcl_DStringAppendElement(dsPtr, "-buffering"); |
| 7858 |
} |
| 7859 |
if (flags & CHANNEL_LINEBUFFERED) { |
| 7860 |
Tcl_DStringAppendElement(dsPtr, "line"); |
| 7861 |
} else if (flags & CHANNEL_UNBUFFERED) { |
| 7862 |
Tcl_DStringAppendElement(dsPtr, "none"); |
| 7863 |
} else { |
| 7864 |
Tcl_DStringAppendElement(dsPtr, "full"); |
| 7865 |
} |
| 7866 |
if (len > 0) { |
| 7867 |
return TCL_OK; |
| 7868 |
} |
| 7869 |
} |
| 7870 |
if (len == 0 || HaveOpt(7, "-buffersize")) { |
| 7871 |
if (len == 0) { |
| 7872 |
Tcl_DStringAppendElement(dsPtr, "-buffersize"); |
| 7873 |
} |
| 7874 |
TclFormatInt(optionVal, statePtr->bufSize); |
| 7875 |
Tcl_DStringAppendElement(dsPtr, optionVal); |
| 7876 |
if (len > 0) { |
| 7877 |
return TCL_OK; |
| 7878 |
} |
| 7879 |
} |
| 7880 |
if (len == 0 || HaveOpt(2, "-encoding")) { |
| 7881 |
if (len == 0) { |
| 7882 |
Tcl_DStringAppendElement(dsPtr, "-encoding"); |
| 7883 |
} |
| 7884 |
if (statePtr->encoding == NULL) { |
| 7885 |
Tcl_DStringAppendElement(dsPtr, "binary"); |
| 7886 |
} else { |
| 7887 |
Tcl_DStringAppendElement(dsPtr, |
| 7888 |
Tcl_GetEncodingName(statePtr->encoding)); |
| 7889 |
} |
| 7890 |
if (len > 0) { |
| 7891 |
return TCL_OK; |
| 7892 |
} |
| 7893 |
} |
| 7894 |
if (len == 0 || HaveOpt(2, "-eofchar")) { |
| 7895 |
if (len == 0) { |
| 7896 |
Tcl_DStringAppendElement(dsPtr, "-eofchar"); |
| 7897 |
} |
| 7898 |
if (((flags & (TCL_READABLE|TCL_WRITABLE)) == |
| 7899 |
(TCL_READABLE|TCL_WRITABLE)) && (len == 0)) { |
| 7900 |
Tcl_DStringStartSublist(dsPtr); |
| 7901 |
} |
| 7902 |
if (flags & TCL_READABLE) { |
| 7903 |
if (statePtr->inEofChar == 0) { |
| 7904 |
Tcl_DStringAppendElement(dsPtr, ""); |
| 7905 |
} else { |
| 7906 |
char buf[2]; |
| 7907 |
|
| 7908 |
buf[1] = '\0'; |
| 7909 |
buf[0] = statePtr->inEofChar; |
| 7910 |
Tcl_DStringAppendElement(dsPtr, buf); |
| 7911 |
} |
| 7912 |
} |
| 7913 |
if (flags & TCL_WRITABLE) { |
| 7914 |
if (statePtr->outEofChar == 0) { |
| 7915 |
Tcl_DStringAppendElement(dsPtr, ""); |
| 7916 |
} else { |
| 7917 |
char buf[2]; |
| 7918 |
|
| 7919 |
buf[1] = '\0'; |
| 7920 |
buf[0] = statePtr->outEofChar; |
| 7921 |
Tcl_DStringAppendElement(dsPtr, buf); |
| 7922 |
} |
| 7923 |
} |
| 7924 |
if (!(flags & (TCL_READABLE|TCL_WRITABLE))) { |
| 7925 |
/* |
| 7926 |
* Not readable or writable (e.g. server socket) |
| 7927 |
*/ |
| 7928 |
|
| 7929 |
Tcl_DStringAppendElement(dsPtr, ""); |
| 7930 |
} |
| 7931 |
if (((flags & (TCL_READABLE|TCL_WRITABLE)) == |
| 7932 |
(TCL_READABLE|TCL_WRITABLE)) && (len == 0)) { |
| 7933 |
Tcl_DStringEndSublist(dsPtr); |
| 7934 |
} |
| 7935 |
if (len > 0) { |
| 7936 |
return TCL_OK; |
| 7937 |
} |
| 7938 |
} |
| 7939 |
if (len == 0 || HaveOpt(1, "-translation")) { |
| 7940 |
if (len == 0) { |
| 7941 |
Tcl_DStringAppendElement(dsPtr, "-translation"); |
| 7942 |
} |
| 7943 |
if (((flags & (TCL_READABLE|TCL_WRITABLE)) == |
| 7944 |
(TCL_READABLE|TCL_WRITABLE)) && (len == 0)) { |
| 7945 |
Tcl_DStringStartSublist(dsPtr); |
| 7946 |
} |
| 7947 |
if (flags & TCL_READABLE) { |
| 7948 |
if (statePtr->inputTranslation == TCL_TRANSLATE_AUTO) { |
| 7949 |
Tcl_DStringAppendElement(dsPtr, "auto"); |
| 7950 |
} else if (statePtr->inputTranslation == TCL_TRANSLATE_CR) { |
| 7951 |
Tcl_DStringAppendElement(dsPtr, "cr"); |
| 7952 |
} else if (statePtr->inputTranslation == TCL_TRANSLATE_CRLF) { |
| 7953 |
Tcl_DStringAppendElement(dsPtr, "crlf"); |
| 7954 |
} else { |
| 7955 |
Tcl_DStringAppendElement(dsPtr, "lf"); |
| 7956 |
} |
| 7957 |
} |
| 7958 |
if (flags & TCL_WRITABLE) { |
| 7959 |
if (statePtr->outputTranslation == TCL_TRANSLATE_AUTO) { |
| 7960 |
Tcl_DStringAppendElement(dsPtr, "auto"); |
| 7961 |
} else if (statePtr->outputTranslation == TCL_TRANSLATE_CR) { |
| 7962 |
Tcl_DStringAppendElement(dsPtr, "cr"); |
| 7963 |
} else if (statePtr->outputTranslation == TCL_TRANSLATE_CRLF) { |
| 7964 |
Tcl_DStringAppendElement(dsPtr, "crlf"); |
| 7965 |
} else { |
| 7966 |
Tcl_DStringAppendElement(dsPtr, "lf"); |
| 7967 |
} |
| 7968 |
} |
| 7969 |
if (!(flags & (TCL_READABLE|TCL_WRITABLE))) { |
| 7970 |
/* |
| 7971 |
* Not readable or writable (e.g. server socket) |
| 7972 |
*/ |
| 7973 |
|
| 7974 |
Tcl_DStringAppendElement(dsPtr, "auto"); |
| 7975 |
} |
| 7976 |
if (((flags & (TCL_READABLE|TCL_WRITABLE)) == |
| 7977 |
(TCL_READABLE|TCL_WRITABLE)) && (len == 0)) { |
| 7978 |
Tcl_DStringEndSublist(dsPtr); |
| 7979 |
} |
| 7980 |
if (len > 0) { |
| 7981 |
return TCL_OK; |
| 7982 |
} |
| 7983 |
} |
| 7984 |
|
| 7985 |
if (chanPtr->typePtr->getOptionProc != NULL) { |
| 7986 |
/* |
| 7987 |
* Let the driver specific handle additional options and result code |
| 7988 |
* and message. |
| 7989 |
*/ |
| 7990 |
|
| 7991 |
return chanPtr->typePtr->getOptionProc(chanPtr->instanceData, interp, |
| 7992 |
optionName, dsPtr); |
| 7993 |
} else { |
| 7994 |
/* |
| 7995 |
* No driver specific options case. |
| 7996 |
*/ |
| 7997 |
|
| 7998 |
if (len == 0) { |
| 7999 |
return TCL_OK; |
| 8000 |
} |
| 8001 |
return Tcl_BadChannelOption(interp, optionName, NULL); |
| 8002 |
} |
| 8003 |
} |
| 8004 |
|
| 8005 |
/* |
| 8006 |
*--------------------------------------------------------------------------- |
| 8007 |
* |
| 8008 |
* Tcl_SetChannelOption -- |
| 8009 |
* |
| 8010 |
* Sets an option on a channel. |
| 8011 |
* |
| 8012 |
* Results: |
| 8013 |
* A standard Tcl result. On error, sets interp's result object if |
| 8014 |
* interp is not NULL. |
| 8015 |
* |
| 8016 |
* Side effects: |
| 8017 |
* May modify an option on a device. |
| 8018 |
* |
| 8019 |
*--------------------------------------------------------------------------- |
| 8020 |
*/ |
| 8021 |
|
| 8022 |
int |
| 8023 |
Tcl_SetChannelOption( |
| 8024 |
Tcl_Interp *interp, /* For error reporting - can be NULL. */ |
| 8025 |
Tcl_Channel chan, /* Channel on which to set mode. */ |
| 8026 |
const char *optionName, /* Which option to set? */ |
| 8027 |
const char *newValue) /* New value for option. */ |
| 8028 |
{ |
| 8029 |
Channel *chanPtr = (Channel *) chan; |
| 8030 |
/* The real IO channel. */ |
| 8031 |
ChannelState *statePtr = chanPtr->state; |
| 8032 |
/* State info for channel */ |
| 8033 |
size_t len; /* Length of optionName string. */ |
| 8034 |
int argc; |
| 8035 |
const char **argv; |
| 8036 |
|
| 8037 |
/* |
| 8038 |
* If the channel is in the middle of a background copy, fail. |
| 8039 |
*/ |
| 8040 |
|
| 8041 |
if (statePtr->csPtrR || statePtr->csPtrW) { |
| 8042 |
if (interp) { |
| 8043 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 8044 |
"unable to set channel options: background copy in" |
| 8045 |
" progress", -1)); |
| 8046 |
} |
| 8047 |
return TCL_ERROR; |
| 8048 |
} |
| 8049 |
|
| 8050 |
/* |
| 8051 |
* Disallow options on dead channels -- channels that have been closed but |
| 8052 |
* not yet been deallocated. Such channels can be found if the exit |
| 8053 |
* handler for channel cleanup has run but the channel is still registered |
| 8054 |
* in an interpreter. |
| 8055 |
*/ |
| 8056 |
|
| 8057 |
if (CheckForDeadChannel(NULL, statePtr)) { |
| 8058 |
return TCL_ERROR; |
| 8059 |
} |
| 8060 |
|
| 8061 |
/* |
| 8062 |
* This operation should occur at the top of a channel stack. |
| 8063 |
*/ |
| 8064 |
|
| 8065 |
chanPtr = statePtr->topChanPtr; |
| 8066 |
|
| 8067 |
len = strlen(optionName); |
| 8068 |
|
| 8069 |
if (HaveOpt(2, "-blocking")) { |
| 8070 |
int newMode; |
| 8071 |
|
| 8072 |
if (Tcl_GetBoolean(interp, newValue, &newMode) == TCL_ERROR) { |
| 8073 |
return TCL_ERROR; |
| 8074 |
} |
| 8075 |
if (newMode) { |
| 8076 |
newMode = TCL_MODE_BLOCKING; |
| 8077 |
} else { |
| 8078 |
newMode = TCL_MODE_NONBLOCKING; |
| 8079 |
} |
| 8080 |
return SetBlockMode(interp, chanPtr, newMode); |
| 8081 |
} else if (HaveOpt(7, "-buffering")) { |
| 8082 |
len = strlen(newValue); |
| 8083 |
if ((newValue[0] == 'f') && (strncmp(newValue, "full", len) == 0)) { |
| 8084 |
ResetFlag(statePtr, CHANNEL_UNBUFFERED | CHANNEL_LINEBUFFERED); |
| 8085 |
} else if ((newValue[0] == 'l') && |
| 8086 |
(strncmp(newValue, "line", len) == 0)) { |
| 8087 |
ResetFlag(statePtr, CHANNEL_UNBUFFERED); |
| 8088 |
SetFlag(statePtr, CHANNEL_LINEBUFFERED); |
| 8089 |
} else if ((newValue[0] == 'n') && |
| 8090 |
(strncmp(newValue, "none", len) == 0)) { |
| 8091 |
ResetFlag(statePtr, CHANNEL_LINEBUFFERED); |
| 8092 |
SetFlag(statePtr, CHANNEL_UNBUFFERED); |
| 8093 |
} else if (interp) { |
| 8094 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 8095 |
"bad value for -buffering: must be one of" |
| 8096 |
" full, line, or none", -1)); |
| 8097 |
return TCL_ERROR; |
| 8098 |
} |
| 8099 |
return TCL_OK; |
| 8100 |
} else if (HaveOpt(7, "-buffersize")) { |
| 8101 |
int newBufferSize; |
| 8102 |
|
| 8103 |
if (Tcl_GetInt(interp, newValue, &newBufferSize) == TCL_ERROR) { |
| 8104 |
return TCL_ERROR; |
| 8105 |
} |
| 8106 |
Tcl_SetChannelBufferSize(chan, newBufferSize); |
| 8107 |
return TCL_OK; |
| 8108 |
} else if (HaveOpt(2, "-encoding")) { |
| 8109 |
Tcl_Encoding encoding; |
| 8110 |
|
| 8111 |
if ((newValue[0] == '\0') || (strcmp(newValue, "binary") == 0)) { |
| 8112 |
encoding = NULL; |
| 8113 |
} else { |
| 8114 |
encoding = Tcl_GetEncoding(interp, newValue); |
| 8115 |
if (encoding == NULL) { |
| 8116 |
return TCL_ERROR; |
| 8117 |
} |
| 8118 |
} |
| 8119 |
|
| 8120 |
/* |
| 8121 |
* When the channel has an escape sequence driven encoding such as |
| 8122 |
* iso2022, the terminated escape sequence must write to the buffer. |
| 8123 |
*/ |
| 8124 |
|
| 8125 |
if ((statePtr->encoding != NULL) |
| 8126 |
&& !(statePtr->outputEncodingFlags & TCL_ENCODING_START) |
| 8127 |
&& (CheckChannelErrors(statePtr, TCL_WRITABLE) == 0)) { |
| 8128 |
statePtr->outputEncodingFlags |= TCL_ENCODING_END; |
| 8129 |
WriteChars(chanPtr, "", 0); |
| 8130 |
} |
| 8131 |
Tcl_FreeEncoding(statePtr->encoding); |
| 8132 |
statePtr->encoding = encoding; |
| 8133 |
statePtr->inputEncodingState = NULL; |
| 8134 |
statePtr->inputEncodingFlags = TCL_ENCODING_START; |
| 8135 |
statePtr->outputEncodingState = NULL; |
| 8136 |
statePtr->outputEncodingFlags = TCL_ENCODING_START; |
| 8137 |
ResetFlag(statePtr, CHANNEL_NEED_MORE_DATA); |
| 8138 |
UpdateInterest(chanPtr); |
| 8139 |
return TCL_OK; |
| 8140 |
} else if (HaveOpt(2, "-eofchar")) { |
| 8141 |
if (Tcl_SplitList(interp, newValue, &argc, &argv) == TCL_ERROR) { |
| 8142 |
return TCL_ERROR; |
| 8143 |
} else if (argc == 0) { |
| 8144 |
statePtr->inEofChar = 0; |
| 8145 |
statePtr->outEofChar = 0; |
| 8146 |
} else if (argc == 1 || argc == 2) { |
| 8147 |
int outIndex = (argc - 1); |
| 8148 |
int inValue = (int) argv[0][0]; |
| 8149 |
int outValue = (int) argv[outIndex][0]; |
| 8150 |
|
| 8151 |
if (inValue & 0x80 || outValue & 0x80) { |
| 8152 |
if (interp) { |
| 8153 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 8154 |
"bad value for -eofchar: must be non-NUL ASCII" |
| 8155 |
" character", -1)); |
| 8156 |
} |
| 8157 |
ckfree(argv); |
| 8158 |
return TCL_ERROR; |
| 8159 |
} |
| 8160 |
if (GotFlag(statePtr, TCL_READABLE)) { |
| 8161 |
statePtr->inEofChar = inValue; |
| 8162 |
} |
| 8163 |
if (GotFlag(statePtr, TCL_WRITABLE)) { |
| 8164 |
statePtr->outEofChar = outValue; |
| 8165 |
} |
| 8166 |
} else { |
| 8167 |
if (interp) { |
| 8168 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 8169 |
"bad value for -eofchar: should be a list of zero," |
| 8170 |
" one, or two elements", -1)); |
| 8171 |
} |
| 8172 |
ckfree(argv); |
| 8173 |
return TCL_ERROR; |
| 8174 |
} |
| 8175 |
if (argv != NULL) { |
| 8176 |
ckfree(argv); |
| 8177 |
} |
| 8178 |
|
| 8179 |
/* |
| 8180 |
* [Bug 930851] Reset EOF and BLOCKED flags. Changing the character |
| 8181 |
* which signals eof can transform a current eof condition into a 'go |
| 8182 |
* ahead'. Ditto for blocked. |
| 8183 |
*/ |
| 8184 |
|
| 8185 |
if (GotFlag(statePtr, CHANNEL_EOF)) { |
| 8186 |
statePtr->inputEncodingFlags |= TCL_ENCODING_START; |
| 8187 |
} |
| 8188 |
ResetFlag(statePtr, CHANNEL_EOF|CHANNEL_STICKY_EOF|CHANNEL_BLOCKED); |
| 8189 |
statePtr->inputEncodingFlags &= ~TCL_ENCODING_END; |
| 8190 |
return TCL_OK; |
| 8191 |
} else if (HaveOpt(1, "-translation")) { |
| 8192 |
const char *readMode, *writeMode; |
| 8193 |
|
| 8194 |
if (Tcl_SplitList(interp, newValue, &argc, &argv) == TCL_ERROR) { |
| 8195 |
return TCL_ERROR; |
| 8196 |
} |
| 8197 |
|
| 8198 |
if (argc == 1) { |
| 8199 |
readMode = GotFlag(statePtr, TCL_READABLE) ? argv[0] : NULL; |
| 8200 |
writeMode = GotFlag(statePtr, TCL_WRITABLE) ? argv[0] : NULL; |
| 8201 |
} else if (argc == 2) { |
| 8202 |
readMode = GotFlag(statePtr, TCL_READABLE) ? argv[0] : NULL; |
| 8203 |
writeMode = GotFlag(statePtr, TCL_WRITABLE) ? argv[1] : NULL; |
| 8204 |
} else { |
| 8205 |
if (interp) { |
| 8206 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 8207 |
"bad value for -translation: must be a one or two" |
| 8208 |
" element list", -1)); |
| 8209 |
} |
| 8210 |
ckfree(argv); |
| 8211 |
return TCL_ERROR; |
| 8212 |
} |
| 8213 |
|
| 8214 |
if (readMode) { |
| 8215 |
TclEolTranslation translation; |
| 8216 |
|
| 8217 |
if (*readMode == '\0') { |
| 8218 |
translation = statePtr->inputTranslation; |
| 8219 |
} else if (strcmp(readMode, "auto") == 0) { |
| 8220 |
translation = TCL_TRANSLATE_AUTO; |
| 8221 |
} else if (strcmp(readMode, "binary") == 0) { |
| 8222 |
translation = TCL_TRANSLATE_LF; |
| 8223 |
statePtr->inEofChar = 0; |
| 8224 |
Tcl_FreeEncoding(statePtr->encoding); |
| 8225 |
statePtr->encoding = NULL; |
| 8226 |
} else if (strcmp(readMode, "lf") == 0) { |
| 8227 |
translation = TCL_TRANSLATE_LF; |
| 8228 |
} else if (strcmp(readMode, "cr") == 0) { |
| 8229 |
translation = TCL_TRANSLATE_CR; |
| 8230 |
} else if (strcmp(readMode, "crlf") == 0) { |
| 8231 |
translation = TCL_TRANSLATE_CRLF; |
| 8232 |
} else if (strcmp(readMode, "platform") == 0) { |
| 8233 |
translation = TCL_PLATFORM_TRANSLATION; |
| 8234 |
} else { |
| 8235 |
if (interp) { |
| 8236 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 8237 |
"bad value for -translation: must be one of " |
| 8238 |
"auto, binary, cr, lf, crlf, or platform", -1)); |
| 8239 |
} |
| 8240 |
ckfree(argv); |
| 8241 |
return TCL_ERROR; |
| 8242 |
} |
| 8243 |
|
| 8244 |
/* |
| 8245 |
* Reset the EOL flags since we need to look at any buffered data |
| 8246 |
* to see if the new translation mode allows us to complete the |
| 8247 |
* line. |
| 8248 |
*/ |
| 8249 |
|
| 8250 |
if (translation != statePtr->inputTranslation) { |
| 8251 |
statePtr->inputTranslation = translation; |
| 8252 |
ResetFlag(statePtr, INPUT_SAW_CR | CHANNEL_NEED_MORE_DATA); |
| 8253 |
UpdateInterest(chanPtr); |
| 8254 |
} |
| 8255 |
} |
| 8256 |
if (writeMode) { |
| 8257 |
if (*writeMode == '\0') { |
| 8258 |
/* Do nothing. */ |
| 8259 |
} else if (strcmp(writeMode, "auto") == 0) { |
| 8260 |
/* |
| 8261 |
* This is a hack to get TCP sockets to produce output in CRLF |
| 8262 |
* mode if they are being set into AUTO mode. A better |
| 8263 |
* solution for achieving this effect will be coded later. |
| 8264 |
*/ |
| 8265 |
|
| 8266 |
if (strcmp(Tcl_ChannelName(chanPtr->typePtr), "tcp") == 0) { |
| 8267 |
statePtr->outputTranslation = TCL_TRANSLATE_CRLF; |
| 8268 |
} else { |
| 8269 |
statePtr->outputTranslation = TCL_PLATFORM_TRANSLATION; |
| 8270 |
} |
| 8271 |
} else if (strcmp(writeMode, "binary") == 0) { |
| 8272 |
statePtr->outEofChar = 0; |
| 8273 |
statePtr->outputTranslation = TCL_TRANSLATE_LF; |
| 8274 |
Tcl_FreeEncoding(statePtr->encoding); |
| 8275 |
statePtr->encoding = NULL; |
| 8276 |
} else if (strcmp(writeMode, "lf") == 0) { |
| 8277 |
statePtr->outputTranslation = TCL_TRANSLATE_LF; |
| 8278 |
} else if (strcmp(writeMode, "cr") == 0) { |
| 8279 |
statePtr->outputTranslation = TCL_TRANSLATE_CR; |
| 8280 |
} else if (strcmp(writeMode, "crlf") == 0) { |
| 8281 |
statePtr->outputTranslation = TCL_TRANSLATE_CRLF; |
| 8282 |
} else if (strcmp(writeMode, "platform") == 0) { |
| 8283 |
statePtr->outputTranslation = TCL_PLATFORM_TRANSLATION; |
| 8284 |
} else { |
| 8285 |
if (interp) { |
| 8286 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 8287 |
"bad value for -translation: must be one of " |
| 8288 |
"auto, binary, cr, lf, crlf, or platform", -1)); |
| 8289 |
} |
| 8290 |
ckfree(argv); |
| 8291 |
return TCL_ERROR; |
| 8292 |
} |
| 8293 |
} |
| 8294 |
ckfree(argv); |
| 8295 |
return TCL_OK; |
| 8296 |
} else if (chanPtr->typePtr->setOptionProc != NULL) { |
| 8297 |
return chanPtr->typePtr->setOptionProc(chanPtr->instanceData, interp, |
| 8298 |
optionName, newValue); |
| 8299 |
} else { |
| 8300 |
return Tcl_BadChannelOption(interp, optionName, NULL); |
| 8301 |
} |
| 8302 |
|
| 8303 |
return TCL_OK; |
| 8304 |
} |
| 8305 |
|
| 8306 |
/* |
| 8307 |
*---------------------------------------------------------------------- |
| 8308 |
* |
| 8309 |
* CleanupChannelHandlers -- |
| 8310 |
* |
| 8311 |
* Removes channel handlers that refer to the supplied interpreter, so |
| 8312 |
* that if the actual channel is not closed now, these handlers will not |
| 8313 |
* run on subsequent events on the channel. This would be erroneous, |
| 8314 |
* because the interpreter no longer has a reference to this channel. |
| 8315 |
* |
| 8316 |
* Results: |
| 8317 |
* None. |
| 8318 |
* |
| 8319 |
* Side effects: |
| 8320 |
* Removes channel handlers. |
| 8321 |
* |
| 8322 |
*---------------------------------------------------------------------- |
| 8323 |
*/ |
| 8324 |
|
| 8325 |
static void |
| 8326 |
CleanupChannelHandlers( |
| 8327 |
Tcl_Interp *interp, |
| 8328 |
Channel *chanPtr) |
| 8329 |
{ |
| 8330 |
ChannelState *statePtr = chanPtr->state; |
| 8331 |
/* State info for channel */ |
| 8332 |
EventScriptRecord *sPtr, *prevPtr, *nextPtr; |
| 8333 |
|
| 8334 |
/* |
| 8335 |
* Remove fileevent records on this channel that refer to the given |
| 8336 |
* interpreter. |
| 8337 |
*/ |
| 8338 |
|
| 8339 |
for (sPtr = statePtr->scriptRecordPtr, prevPtr = NULL; |
| 8340 |
sPtr != NULL; sPtr = nextPtr) { |
| 8341 |
nextPtr = sPtr->nextPtr; |
| 8342 |
if (sPtr->interp == interp) { |
| 8343 |
if (prevPtr == NULL) { |
| 8344 |
statePtr->scriptRecordPtr = nextPtr; |
| 8345 |
} else { |
| 8346 |
prevPtr->nextPtr = nextPtr; |
| 8347 |
} |
| 8348 |
|
| 8349 |
Tcl_DeleteChannelHandler((Tcl_Channel) chanPtr, |
| 8350 |
TclChannelEventScriptInvoker, sPtr); |
| 8351 |
|
| 8352 |
TclDecrRefCount(sPtr->scriptPtr); |
| 8353 |
ckfree(sPtr); |
| 8354 |
} else { |
| 8355 |
prevPtr = sPtr; |
| 8356 |
} |
| 8357 |
} |
| 8358 |
} |
| 8359 |
|
| 8360 |
/* |
| 8361 |
*---------------------------------------------------------------------- |
| 8362 |
* |
| 8363 |
* Tcl_NotifyChannel -- |
| 8364 |
* |
| 8365 |
* This procedure is called by a channel driver when a driver detects an |
| 8366 |
* event on a channel. This procedure is responsible for actually |
| 8367 |
* handling the event by invoking any channel handler callbacks. |
| 8368 |
* |
| 8369 |
* Results: |
| 8370 |
* None. |
| 8371 |
* |
| 8372 |
* Side effects: |
| 8373 |
* Whatever the channel handler callback procedure does. |
| 8374 |
* |
| 8375 |
*---------------------------------------------------------------------- |
| 8376 |
*/ |
| 8377 |
|
| 8378 |
void |
| 8379 |
Tcl_NotifyChannel( |
| 8380 |
Tcl_Channel channel, /* Channel that detected an event. */ |
| 8381 |
int mask) /* OR'ed combination of TCL_READABLE, |
| 8382 |
* TCL_WRITABLE, or TCL_EXCEPTION: indicates |
| 8383 |
* which events were detected. */ |
| 8384 |
{ |
| 8385 |
Channel *chanPtr = (Channel *) channel; |
| 8386 |
ChannelState *statePtr = chanPtr->state; |
| 8387 |
/* State info for channel */ |
| 8388 |
ChannelHandler *chPtr; |
| 8389 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 8390 |
NextChannelHandler nh; |
| 8391 |
Channel *upChanPtr; |
| 8392 |
const Tcl_ChannelType *upTypePtr; |
| 8393 |
|
| 8394 |
/* |
| 8395 |
* In contrast to the other API functions this procedure walks towards the |
| 8396 |
* top of a stack and not down from it. |
| 8397 |
* |
| 8398 |
* The channel calling this procedure is the one who generated the event, |
| 8399 |
* and thus does not take part in handling it. IOW, its HandlerProc is not |
| 8400 |
* called, instead we begin with the channel above it. |
| 8401 |
* |
| 8402 |
* This behaviour also allows the transformation channels to generate |
| 8403 |
* their own events and pass them upward. |
| 8404 |
*/ |
| 8405 |
|
| 8406 |
while (mask && (chanPtr->upChanPtr != NULL)) { |
| 8407 |
Tcl_DriverHandlerProc *upHandlerProc; |
| 8408 |
|
| 8409 |
upChanPtr = chanPtr->upChanPtr; |
| 8410 |
upTypePtr = upChanPtr->typePtr; |
| 8411 |
upHandlerProc = Tcl_ChannelHandlerProc(upTypePtr); |
| 8412 |
if (upHandlerProc != NULL) { |
| 8413 |
mask = upHandlerProc(upChanPtr->instanceData, mask); |
| 8414 |
} |
| 8415 |
|
| 8416 |
/* |
| 8417 |
* ELSE: Ignore transformations which are unable to handle the event |
| 8418 |
* coming from below. Assume that they don't change the mask and pass |
| 8419 |
* it on. |
| 8420 |
*/ |
| 8421 |
|
| 8422 |
chanPtr = upChanPtr; |
| 8423 |
} |
| 8424 |
|
| 8425 |
channel = (Tcl_Channel) chanPtr; |
| 8426 |
|
| 8427 |
/* |
| 8428 |
* Here we have either reached the top of the stack or the mask is empty. |
| 8429 |
* We break out of the procedure if it is the latter. |
| 8430 |
*/ |
| 8431 |
|
| 8432 |
if (!mask) { |
| 8433 |
return; |
| 8434 |
} |
| 8435 |
|
| 8436 |
/* |
| 8437 |
* We are now above the topmost channel in a stack and have events left. |
| 8438 |
* Now call the channel handlers as usual. |
| 8439 |
* |
| 8440 |
* Preserve the channel struct in case the script closes it. |
| 8441 |
*/ |
| 8442 |
|
| 8443 |
TclChannelPreserve((Tcl_Channel)channel); |
| 8444 |
Tcl_Preserve(statePtr); |
| 8445 |
|
| 8446 |
/* |
| 8447 |
* Avoid processing if the channel owner has been changed. |
| 8448 |
*/ |
| 8449 |
if (statePtr->managingThread != Tcl_GetCurrentThread()) { |
| 8450 |
goto done; |
| 8451 |
} |
| 8452 |
|
| 8453 |
/* |
| 8454 |
* If we are flushing in the background, be sure to call FlushChannel for |
| 8455 |
* writable events. Note that we have to discard the writable event so we |
| 8456 |
* don't call any write handlers before the flush is complete. |
| 8457 |
*/ |
| 8458 |
|
| 8459 |
if (GotFlag(statePtr, BG_FLUSH_SCHEDULED) && (mask & TCL_WRITABLE)) { |
| 8460 |
if (0 == FlushChannel(NULL, chanPtr, 1)) { |
| 8461 |
mask &= ~TCL_WRITABLE; |
| 8462 |
} |
| 8463 |
} |
| 8464 |
|
| 8465 |
/* |
| 8466 |
* Add this invocation to the list of recursive invocations of |
| 8467 |
* Tcl_NotifyChannel. |
| 8468 |
*/ |
| 8469 |
|
| 8470 |
nh.nextHandlerPtr = NULL; |
| 8471 |
nh.nestedHandlerPtr = tsdPtr->nestedHandlerPtr; |
| 8472 |
tsdPtr->nestedHandlerPtr = &nh; |
| 8473 |
|
| 8474 |
for (chPtr = statePtr->chPtr; chPtr != NULL; ) { |
| 8475 |
/* |
| 8476 |
* If this channel handler is interested in any of the events that |
| 8477 |
* have occurred on the channel, invoke its procedure. |
| 8478 |
*/ |
| 8479 |
|
| 8480 |
if ((chPtr->mask & mask) != 0) { |
| 8481 |
nh.nextHandlerPtr = chPtr->nextPtr; |
| 8482 |
chPtr->proc(chPtr->clientData, chPtr->mask & mask); |
| 8483 |
chPtr = nh.nextHandlerPtr; |
| 8484 |
} else { |
| 8485 |
chPtr = chPtr->nextPtr; |
| 8486 |
} |
| 8487 |
|
| 8488 |
/* |
| 8489 |
* Stop if the channel owner has been changed in-between. |
| 8490 |
*/ |
| 8491 |
if (chanPtr->state->managingThread != Tcl_GetCurrentThread()) { |
| 8492 |
goto done; |
| 8493 |
} |
| 8494 |
} |
| 8495 |
|
| 8496 |
/* |
| 8497 |
* Update the notifier interest, since it may have changed after invoking |
| 8498 |
* event handlers. Skip that if the channel was deleted in the call to the |
| 8499 |
* channel handler. |
| 8500 |
*/ |
| 8501 |
|
| 8502 |
if (chanPtr->typePtr != NULL) { |
| 8503 |
/* |
| 8504 |
* TODO: This call may not be needed. If a handler induced a |
| 8505 |
* change in interest, that handler should have made its own |
| 8506 |
* UpdateInterest() call, one would think. |
| 8507 |
*/ |
| 8508 |
UpdateInterest(chanPtr); |
| 8509 |
} |
| 8510 |
|
| 8511 |
done: |
| 8512 |
Tcl_Release(statePtr); |
| 8513 |
TclChannelRelease(channel); |
| 8514 |
|
| 8515 |
tsdPtr->nestedHandlerPtr = nh.nestedHandlerPtr; |
| 8516 |
} |
| 8517 |
|
| 8518 |
/* |
| 8519 |
*---------------------------------------------------------------------- |
| 8520 |
* |
| 8521 |
* UpdateInterest -- |
| 8522 |
* |
| 8523 |
* Arrange for the notifier to call us back at appropriate times based on |
| 8524 |
* the current state of the channel. |
| 8525 |
* |
| 8526 |
* Results: |
| 8527 |
* None. |
| 8528 |
* |
| 8529 |
* Side effects: |
| 8530 |
* May schedule a timer or driver handler. |
| 8531 |
* |
| 8532 |
*---------------------------------------------------------------------- |
| 8533 |
*/ |
| 8534 |
|
| 8535 |
static void |
| 8536 |
UpdateInterest( |
| 8537 |
Channel *chanPtr) /* Channel to update. */ |
| 8538 |
{ |
| 8539 |
ChannelState *statePtr = chanPtr->state; |
| 8540 |
/* State info for channel */ |
| 8541 |
int mask = statePtr->interestMask; |
| 8542 |
|
| 8543 |
if (chanPtr->typePtr == NULL) { |
| 8544 |
/* Do not update interest on a closed channel */ |
| 8545 |
return; |
| 8546 |
} |
| 8547 |
|
| 8548 |
/* |
| 8549 |
* If there are flushed buffers waiting to be written, then we need to |
| 8550 |
* watch for the channel to become writable. |
| 8551 |
*/ |
| 8552 |
|
| 8553 |
if (GotFlag(statePtr, BG_FLUSH_SCHEDULED)) { |
| 8554 |
mask |= TCL_WRITABLE; |
| 8555 |
} |
| 8556 |
|
| 8557 |
/* |
| 8558 |
* If there is data in the input queue, and we aren't waiting for more |
| 8559 |
* data, then we need to schedule a timer so we don't block in the |
| 8560 |
* notifier. Also, cancel the read interest so we don't get duplicate |
| 8561 |
* events. |
| 8562 |
*/ |
| 8563 |
|
| 8564 |
if (mask & TCL_READABLE) { |
| 8565 |
if (!GotFlag(statePtr, CHANNEL_NEED_MORE_DATA) |
| 8566 |
&& (statePtr->inQueueHead != NULL) |
| 8567 |
&& IsBufferReady(statePtr->inQueueHead)) { |
| 8568 |
mask &= ~TCL_READABLE; |
| 8569 |
|
| 8570 |
/* |
| 8571 |
* Andreas Kupries, April 11, 2003 |
| 8572 |
* |
| 8573 |
* Some operating systems (Solaris 2.6 and higher (but not Solaris |
| 8574 |
* 2.5, go figure)) generate READABLE and EXCEPTION events when |
| 8575 |
* select()'ing [*] on a plain file, even if EOF was not yet |
| 8576 |
* reached. This is a problem in the following situation: |
| 8577 |
* |
| 8578 |
* - An extension asks to get both READABLE and EXCEPTION events. |
| 8579 |
* - It reads data into a buffer smaller than the buffer used by |
| 8580 |
* Tcl itself. |
| 8581 |
* - It does not process all events in the event queue, but only |
| 8582 |
* one, at least in some situations. |
| 8583 |
* |
| 8584 |
* In that case we can get into a situation where |
| 8585 |
* |
| 8586 |
* - Tcl drops READABLE here, because it has data in its own |
| 8587 |
* buffers waiting to be read by the extension. |
| 8588 |
* - A READABLE event is synthesized via timer. |
| 8589 |
* - The OS still reports the EXCEPTION condition on the file. |
| 8590 |
* - And the extension gets the EXCEPTION event first, and handles |
| 8591 |
* this as EOF. |
| 8592 |
* |
| 8593 |
* End result ==> Premature end of reading from a file. |
| 8594 |
* |
| 8595 |
* The concrete example is 'Expect', and its [expect] command |
| 8596 |
* (and at the C-level, deep in the bowels of Expect, |
| 8597 |
* 'exp_get_next_event'. See marker 'SunOS' for commentary in |
| 8598 |
* that function too). |
| 8599 |
* |
| 8600 |
* [*] As the Tcl notifier does. See also for marker 'SunOS' in |
| 8601 |
* file 'exp_event.c' of Expect. |
| 8602 |
* |
| 8603 |
* Our solution here is to drop the interest in the EXCEPTION |
| 8604 |
* events too. This compiles on all platforms, and also passes the |
| 8605 |
* testsuite on all of them. |
| 8606 |
*/ |
| 8607 |
|
| 8608 |
mask &= ~TCL_EXCEPTION; |
| 8609 |
|
| 8610 |
if (!statePtr->timer) { |
| 8611 |
TclChannelPreserve((Tcl_Channel)chanPtr); |
| 8612 |
statePtr->timerChanPtr = chanPtr; |
| 8613 |
statePtr->timer = Tcl_CreateTimerHandler(SYNTHETIC_EVENT_TIME, |
| 8614 |
ChannelTimerProc, chanPtr); |
| 8615 |
} |
| 8616 |
} |
| 8617 |
} |
| 8618 |
ChanWatch(chanPtr, mask); |
| 8619 |
} |
| 8620 |
|
| 8621 |
/* |
| 8622 |
*---------------------------------------------------------------------- |
| 8623 |
* |
| 8624 |
* ChannelTimerProc -- |
| 8625 |
* |
| 8626 |
* Timer handler scheduled by UpdateInterest to monitor the channel |
| 8627 |
* buffers until they are empty. |
| 8628 |
* |
| 8629 |
* Results: |
| 8630 |
* None. |
| 8631 |
* |
| 8632 |
* Side effects: |
| 8633 |
* May invoke channel handlers. |
| 8634 |
* |
| 8635 |
*---------------------------------------------------------------------- |
| 8636 |
*/ |
| 8637 |
|
| 8638 |
static void |
| 8639 |
ChannelTimerProc( |
| 8640 |
void *clientData) |
| 8641 |
{ |
| 8642 |
Channel *chanPtr = (Channel *)clientData; |
| 8643 |
/* State info for channel */ |
| 8644 |
ChannelState *statePtr = chanPtr->state; |
| 8645 |
|
| 8646 |
if (chanPtr->typePtr == NULL) { |
| 8647 |
statePtr->timer = NULL; |
| 8648 |
TclChannelRelease((Tcl_Channel)statePtr->timerChanPtr); |
| 8649 |
statePtr->timerChanPtr = NULL; |
| 8650 |
} else { |
| 8651 |
if (!GotFlag(statePtr, CHANNEL_NEED_MORE_DATA) |
| 8652 |
&& (statePtr->interestMask & TCL_READABLE) |
| 8653 |
&& (statePtr->inQueueHead != NULL) |
| 8654 |
&& IsBufferReady(statePtr->inQueueHead)) { |
| 8655 |
/* |
| 8656 |
* Restart the timer in case a channel handler reenters the event loop |
| 8657 |
* before UpdateInterest gets called by Tcl_NotifyChannel. |
| 8658 |
*/ |
| 8659 |
statePtr->timer = Tcl_CreateTimerHandler(SYNTHETIC_EVENT_TIME, |
| 8660 |
ChannelTimerProc,chanPtr); |
| 8661 |
Tcl_Preserve(statePtr); |
| 8662 |
Tcl_NotifyChannel((Tcl_Channel) chanPtr, TCL_READABLE); |
| 8663 |
Tcl_Release(statePtr); |
| 8664 |
} else { |
| 8665 |
statePtr->timer = NULL; |
| 8666 |
UpdateInterest(chanPtr); |
| 8667 |
TclChannelRelease((Tcl_Channel)statePtr->timerChanPtr); |
| 8668 |
statePtr->timerChanPtr = NULL; |
| 8669 |
} |
| 8670 |
} |
| 8671 |
} |
| 8672 |
|
| 8673 |
static void |
| 8674 |
DeleteTimerHandler( |
| 8675 |
ChannelState *statePtr |
| 8676 |
) |
| 8677 |
{ |
| 8678 |
if (statePtr->timer != NULL) { |
| 8679 |
Tcl_DeleteTimerHandler(statePtr->timer); |
| 8680 |
statePtr->timer = NULL; |
| 8681 |
TclChannelRelease((Tcl_Channel)statePtr->timerChanPtr); |
| 8682 |
statePtr->timerChanPtr = NULL; |
| 8683 |
} |
| 8684 |
} |
| 8685 |
|
| 8686 |
/* |
| 8687 |
*---------------------------------------------------------------------- |
| 8688 |
* |
| 8689 |
* Tcl_CreateChannelHandler -- |
| 8690 |
* |
| 8691 |
* Arrange for a given procedure to be invoked whenever the channel |
| 8692 |
* indicated by the chanPtr arg becomes readable or writable. |
| 8693 |
* |
| 8694 |
* Results: |
| 8695 |
* None. |
| 8696 |
* |
| 8697 |
* Side effects: |
| 8698 |
* From now on, whenever the I/O channel given by chanPtr becomes ready |
| 8699 |
* in the way indicated by mask, proc will be invoked. See the manual |
| 8700 |
* entry for details on the calling sequence to proc. If there is already |
| 8701 |
* an event handler for chan, proc and clientData, then the mask will be |
| 8702 |
* updated. |
| 8703 |
* |
| 8704 |
*---------------------------------------------------------------------- |
| 8705 |
*/ |
| 8706 |
|
| 8707 |
void |
| 8708 |
Tcl_CreateChannelHandler( |
| 8709 |
Tcl_Channel chan, /* The channel to create the handler for. */ |
| 8710 |
int mask, /* OR'ed combination of TCL_READABLE, |
| 8711 |
* TCL_WRITABLE, and TCL_EXCEPTION: indicates |
| 8712 |
* conditions under which proc should be |
| 8713 |
* called. Use 0 to disable a registered |
| 8714 |
* handler. */ |
| 8715 |
Tcl_ChannelProc *proc, /* Procedure to call for each selected |
| 8716 |
* event. */ |
| 8717 |
void *clientData) /* Arbitrary data to pass to proc. */ |
| 8718 |
{ |
| 8719 |
ChannelHandler *chPtr; |
| 8720 |
Channel *chanPtr = (Channel *) chan; |
| 8721 |
ChannelState *statePtr = chanPtr->state; |
| 8722 |
/* State info for channel */ |
| 8723 |
|
| 8724 |
/* |
| 8725 |
* Check whether this channel handler is not already registered. If it is |
| 8726 |
* not, create a new record, else reuse existing record (smash current |
| 8727 |
* values). |
| 8728 |
*/ |
| 8729 |
|
| 8730 |
for (chPtr = statePtr->chPtr; chPtr != NULL; chPtr = chPtr->nextPtr) { |
| 8731 |
if ((chPtr->chanPtr == chanPtr) && (chPtr->proc == proc) && |
| 8732 |
(chPtr->clientData == clientData)) { |
| 8733 |
break; |
| 8734 |
} |
| 8735 |
} |
| 8736 |
if (chPtr == NULL) { |
| 8737 |
chPtr = (ChannelHandler *)ckalloc(sizeof(ChannelHandler)); |
| 8738 |
chPtr->mask = 0; |
| 8739 |
chPtr->proc = proc; |
| 8740 |
chPtr->clientData = clientData; |
| 8741 |
chPtr->chanPtr = chanPtr; |
| 8742 |
chPtr->nextPtr = statePtr->chPtr; |
| 8743 |
statePtr->chPtr = chPtr; |
| 8744 |
} |
| 8745 |
|
| 8746 |
/* |
| 8747 |
* The remainder of the initialization below is done regardless of whether |
| 8748 |
* this is a new record or a modification of an old one. |
| 8749 |
*/ |
| 8750 |
|
| 8751 |
chPtr->mask = mask; |
| 8752 |
|
| 8753 |
/* |
| 8754 |
* Recompute the interest mask for the channel - this call may actually be |
| 8755 |
* disabling an existing handler. |
| 8756 |
*/ |
| 8757 |
|
| 8758 |
statePtr->interestMask = 0; |
| 8759 |
for (chPtr = statePtr->chPtr; chPtr != NULL; chPtr = chPtr->nextPtr) { |
| 8760 |
statePtr->interestMask |= chPtr->mask; |
| 8761 |
} |
| 8762 |
|
| 8763 |
UpdateInterest(statePtr->topChanPtr); |
| 8764 |
} |
| 8765 |
|
| 8766 |
/* |
| 8767 |
*---------------------------------------------------------------------- |
| 8768 |
* |
| 8769 |
* Tcl_DeleteChannelHandler -- |
| 8770 |
* |
| 8771 |
* Cancel a previously arranged callback arrangement for an IO channel. |
| 8772 |
* |
| 8773 |
* Results: |
| 8774 |
* None. |
| 8775 |
* |
| 8776 |
* Side effects: |
| 8777 |
* If a callback was previously registered for this chan, proc and |
| 8778 |
* clientData, it is removed and the callback will no longer be called |
| 8779 |
* when the channel becomes ready for IO. |
| 8780 |
* |
| 8781 |
*---------------------------------------------------------------------- |
| 8782 |
*/ |
| 8783 |
|
| 8784 |
void |
| 8785 |
Tcl_DeleteChannelHandler( |
| 8786 |
Tcl_Channel chan, /* The channel for which to remove the |
| 8787 |
* callback. */ |
| 8788 |
Tcl_ChannelProc *proc, /* The procedure in the callback to delete. */ |
| 8789 |
void *clientData) /* The client data in the callback to |
| 8790 |
* delete. */ |
| 8791 |
{ |
| 8792 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 8793 |
ChannelHandler *chPtr, *prevChPtr; |
| 8794 |
Channel *chanPtr = (Channel *) chan; |
| 8795 |
ChannelState *statePtr = chanPtr->state; |
| 8796 |
/* State info for channel */ |
| 8797 |
NextChannelHandler *nhPtr; |
| 8798 |
|
| 8799 |
/* |
| 8800 |
* Find the entry and the previous one in the list. |
| 8801 |
*/ |
| 8802 |
|
| 8803 |
for (prevChPtr = NULL, chPtr = statePtr->chPtr; chPtr != NULL; |
| 8804 |
chPtr = chPtr->nextPtr) { |
| 8805 |
if ((chPtr->chanPtr == chanPtr) && (chPtr->clientData == clientData) |
| 8806 |
&& (chPtr->proc == proc)) { |
| 8807 |
break; |
| 8808 |
} |
| 8809 |
prevChPtr = chPtr; |
| 8810 |
} |
| 8811 |
|
| 8812 |
/* |
| 8813 |
* If not found, return without doing anything. |
| 8814 |
*/ |
| 8815 |
|
| 8816 |
if (chPtr == NULL) { |
| 8817 |
return; |
| 8818 |
} |
| 8819 |
|
| 8820 |
/* |
| 8821 |
* If Tcl_NotifyChannel is about to process this handler, tell it to |
| 8822 |
* process the next one instead - we are going to delete *this* one. |
| 8823 |
*/ |
| 8824 |
|
| 8825 |
for (nhPtr = tsdPtr->nestedHandlerPtr; nhPtr != NULL; |
| 8826 |
nhPtr = nhPtr->nestedHandlerPtr) { |
| 8827 |
if (nhPtr->nextHandlerPtr == chPtr) { |
| 8828 |
nhPtr->nextHandlerPtr = chPtr->nextPtr; |
| 8829 |
} |
| 8830 |
} |
| 8831 |
|
| 8832 |
/* |
| 8833 |
* Splice it out of the list of channel handlers. |
| 8834 |
*/ |
| 8835 |
|
| 8836 |
if (prevChPtr == NULL) { |
| 8837 |
statePtr->chPtr = chPtr->nextPtr; |
| 8838 |
} else { |
| 8839 |
prevChPtr->nextPtr = chPtr->nextPtr; |
| 8840 |
} |
| 8841 |
ckfree(chPtr); |
| 8842 |
|
| 8843 |
/* |
| 8844 |
* Recompute the interest list for the channel, so that infinite loops |
| 8845 |
* will not result if Tcl_DeleteChannelHandler is called inside an event. |
| 8846 |
*/ |
| 8847 |
|
| 8848 |
statePtr->interestMask = 0; |
| 8849 |
for (chPtr = statePtr->chPtr; chPtr != NULL; chPtr = chPtr->nextPtr) { |
| 8850 |
statePtr->interestMask |= chPtr->mask; |
| 8851 |
} |
| 8852 |
|
| 8853 |
UpdateInterest(statePtr->topChanPtr); |
| 8854 |
} |
| 8855 |
|
| 8856 |
/* |
| 8857 |
*---------------------------------------------------------------------- |
| 8858 |
* |
| 8859 |
* DeleteScriptRecord -- |
| 8860 |
* |
| 8861 |
* Delete a script record for this combination of channel, interp and |
| 8862 |
* mask. |
| 8863 |
* |
| 8864 |
* Results: |
| 8865 |
* None. |
| 8866 |
* |
| 8867 |
* Side effects: |
| 8868 |
* Deletes a script record and cancels a channel event handler. |
| 8869 |
* |
| 8870 |
*---------------------------------------------------------------------- |
| 8871 |
*/ |
| 8872 |
|
| 8873 |
static void |
| 8874 |
DeleteScriptRecord( |
| 8875 |
Tcl_Interp *interp, /* Interpreter in which script was to be |
| 8876 |
* executed. */ |
| 8877 |
Channel *chanPtr, /* The channel for which to delete the script |
| 8878 |
* record (if any). */ |
| 8879 |
int mask) /* Events in mask must exactly match mask of |
| 8880 |
* script to delete. */ |
| 8881 |
{ |
| 8882 |
ChannelState *statePtr = chanPtr->state; |
| 8883 |
/* State info for channel */ |
| 8884 |
EventScriptRecord *esPtr, *prevEsPtr; |
| 8885 |
|
| 8886 |
for (esPtr = statePtr->scriptRecordPtr, prevEsPtr = NULL; esPtr != NULL; |
| 8887 |
prevEsPtr = esPtr, esPtr = esPtr->nextPtr) { |
| 8888 |
if ((esPtr->interp == interp) && (esPtr->mask == mask)) { |
| 8889 |
if (esPtr == statePtr->scriptRecordPtr) { |
| 8890 |
statePtr->scriptRecordPtr = esPtr->nextPtr; |
| 8891 |
} else { |
| 8892 |
CLANG_ASSERT(prevEsPtr); |
| 8893 |
prevEsPtr->nextPtr = esPtr->nextPtr; |
| 8894 |
} |
| 8895 |
|
| 8896 |
Tcl_DeleteChannelHandler((Tcl_Channel) chanPtr, |
| 8897 |
TclChannelEventScriptInvoker, esPtr); |
| 8898 |
|
| 8899 |
TclDecrRefCount(esPtr->scriptPtr); |
| 8900 |
ckfree(esPtr); |
| 8901 |
|
| 8902 |
break; |
| 8903 |
} |
| 8904 |
} |
| 8905 |
} |
| 8906 |
|
| 8907 |
/* |
| 8908 |
*---------------------------------------------------------------------- |
| 8909 |
* |
| 8910 |
* CreateScriptRecord -- |
| 8911 |
* |
| 8912 |
* Creates a record to store a script to be executed when a specific |
| 8913 |
* event fires on a specific channel. |
| 8914 |
* |
| 8915 |
* Results: |
| 8916 |
* None. |
| 8917 |
* |
| 8918 |
* Side effects: |
| 8919 |
* Causes the script to be stored for later execution. |
| 8920 |
* |
| 8921 |
*---------------------------------------------------------------------- |
| 8922 |
*/ |
| 8923 |
|
| 8924 |
static void |
| 8925 |
CreateScriptRecord( |
| 8926 |
Tcl_Interp *interp, /* Interpreter in which to execute the stored |
| 8927 |
* script. */ |
| 8928 |
Channel *chanPtr, /* Channel for which script is to be stored */ |
| 8929 |
int mask, /* Set of events for which script will be |
| 8930 |
* invoked. */ |
| 8931 |
Tcl_Obj *scriptPtr) /* Pointer to script object. */ |
| 8932 |
{ |
| 8933 |
ChannelState *statePtr = chanPtr->state; |
| 8934 |
/* State info for channel */ |
| 8935 |
EventScriptRecord *esPtr; |
| 8936 |
int makeCH; |
| 8937 |
|
| 8938 |
for (esPtr=statePtr->scriptRecordPtr; esPtr!=NULL; esPtr=esPtr->nextPtr) { |
| 8939 |
if ((esPtr->interp == interp) && (esPtr->mask == mask)) { |
| 8940 |
TclDecrRefCount(esPtr->scriptPtr); |
| 8941 |
esPtr->scriptPtr = NULL; |
| 8942 |
break; |
| 8943 |
} |
| 8944 |
} |
| 8945 |
|
| 8946 |
makeCH = (esPtr == NULL); |
| 8947 |
|
| 8948 |
if (makeCH) { |
| 8949 |
esPtr = (EventScriptRecord *)ckalloc(sizeof(EventScriptRecord)); |
| 8950 |
} |
| 8951 |
|
| 8952 |
/* |
| 8953 |
* Initialize the structure before calling Tcl_CreateChannelHandler, |
| 8954 |
* because a reflected channel calling 'chan postevent' aka |
| 8955 |
* 'Tcl_NotifyChannel' in its 'watch'Proc will invoke |
| 8956 |
* 'TclChannelEventScriptInvoker' immediately, and we do not wish it to |
| 8957 |
* see uninitialized memory and crash. See [Bug 2918110]. |
| 8958 |
*/ |
| 8959 |
|
| 8960 |
esPtr->chanPtr = chanPtr; |
| 8961 |
esPtr->interp = interp; |
| 8962 |
esPtr->mask = mask; |
| 8963 |
Tcl_IncrRefCount(scriptPtr); |
| 8964 |
esPtr->scriptPtr = scriptPtr; |
| 8965 |
|
| 8966 |
if (makeCH) { |
| 8967 |
esPtr->nextPtr = statePtr->scriptRecordPtr; |
| 8968 |
statePtr->scriptRecordPtr = esPtr; |
| 8969 |
|
| 8970 |
Tcl_CreateChannelHandler((Tcl_Channel) chanPtr, mask, |
| 8971 |
TclChannelEventScriptInvoker, esPtr); |
| 8972 |
} |
| 8973 |
} |
| 8974 |
|
| 8975 |
/* |
| 8976 |
*---------------------------------------------------------------------- |
| 8977 |
* |
| 8978 |
* TclChannelEventScriptInvoker -- |
| 8979 |
* |
| 8980 |
* Invokes a script scheduled by "fileevent" for when the channel becomes |
| 8981 |
* ready for IO. This function is invoked by the channel handler which |
| 8982 |
* was created by the Tcl "fileevent" command. |
| 8983 |
* |
| 8984 |
* Results: |
| 8985 |
* None. |
| 8986 |
* |
| 8987 |
* Side effects: |
| 8988 |
* Whatever the script does. |
| 8989 |
* |
| 8990 |
*---------------------------------------------------------------------- |
| 8991 |
*/ |
| 8992 |
|
| 8993 |
void |
| 8994 |
TclChannelEventScriptInvoker( |
| 8995 |
void *clientData, /* The script+interp record. */ |
| 8996 |
int mask) /* Not used. */ |
| 8997 |
{ |
| 8998 |
EventScriptRecord *esPtr = (EventScriptRecord *)clientData; |
| 8999 |
/* The event script + interpreter to eval it |
| 9000 |
* in. */ |
| 9001 |
Channel *chanPtr = esPtr->chanPtr; |
| 9002 |
/* The channel for which this handler is |
| 9003 |
* registered. */ |
| 9004 |
Tcl_Interp *interp = esPtr->interp; |
| 9005 |
/* Interpreter in which to eval the script. */ |
| 9006 |
int result; /* Result of call to eval script. */ |
| 9007 |
|
| 9008 |
mask = esPtr->mask; |
| 9009 |
|
| 9010 |
/* |
| 9011 |
* Be sure event executed in managed channel (covering bugs similar [f583715154]). |
| 9012 |
*/ |
| 9013 |
assert(chanPtr->state->managingThread == Tcl_GetCurrentThread()); |
| 9014 |
|
| 9015 |
/* |
| 9016 |
* We must preserve the interpreter so we can report errors on it later. |
| 9017 |
* Note that we do not need to preserve the channel because that is done |
| 9018 |
* by Tcl_NotifyChannel before calling channel handlers. |
| 9019 |
*/ |
| 9020 |
|
| 9021 |
Tcl_Preserve(interp); |
| 9022 |
TclChannelPreserve((Tcl_Channel)chanPtr); |
| 9023 |
result = Tcl_EvalObjEx(interp, esPtr->scriptPtr, TCL_EVAL_GLOBAL); |
| 9024 |
|
| 9025 |
/* |
| 9026 |
* On error, cause a background error and remove the channel handler and |
| 9027 |
* the script record. |
| 9028 |
* |
| 9029 |
* NOTE: Must delete channel handler before causing the background error |
| 9030 |
* because the background error may want to reinstall the handler. |
| 9031 |
*/ |
| 9032 |
|
| 9033 |
if (result != TCL_OK) { |
| 9034 |
if (chanPtr->typePtr != NULL) { |
| 9035 |
DeleteScriptRecord(interp, chanPtr, mask); |
| 9036 |
} |
| 9037 |
Tcl_BackgroundException(interp, result); |
| 9038 |
} |
| 9039 |
TclChannelRelease((Tcl_Channel)chanPtr); |
| 9040 |
Tcl_Release(interp); |
| 9041 |
} |
| 9042 |
|
| 9043 |
/* |
| 9044 |
*---------------------------------------------------------------------- |
| 9045 |
* |
| 9046 |
* Tcl_FileEventObjCmd -- |
| 9047 |
* |
| 9048 |
* This procedure implements the "fileevent" Tcl command. See the user |
| 9049 |
* documentation for details on what it does. This command is based on |
| 9050 |
* the Tk command "fileevent" which in turn is based on work contributed |
| 9051 |
* by Mark Diekhans. |
| 9052 |
* |
| 9053 |
* Results: |
| 9054 |
* A standard Tcl result. |
| 9055 |
* |
| 9056 |
* Side effects: |
| 9057 |
* May create a channel handler for the specified channel. |
| 9058 |
* |
| 9059 |
*---------------------------------------------------------------------- |
| 9060 |
*/ |
| 9061 |
|
| 9062 |
int |
| 9063 |
Tcl_FileEventObjCmd( |
| 9064 |
void *dummy, /* Not used. */ |
| 9065 |
Tcl_Interp *interp, /* Interpreter in which the channel for which |
| 9066 |
* to create the handler is found. */ |
| 9067 |
int objc, /* Number of arguments. */ |
| 9068 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 9069 |
{ |
| 9070 |
Channel *chanPtr; /* The channel to create the handler for. */ |
| 9071 |
ChannelState *statePtr; /* State info for channel */ |
| 9072 |
Tcl_Channel chan; /* The opaque type for the channel. */ |
| 9073 |
const char *chanName; |
| 9074 |
int modeIndex; /* Index of mode argument. */ |
| 9075 |
int mask; |
| 9076 |
static const char *const modeOptions[] = {"readable", "writable", NULL}; |
| 9077 |
static const int maskArray[] = {TCL_READABLE, TCL_WRITABLE}; |
| 9078 |
(void)dummy; |
| 9079 |
|
| 9080 |
if ((objc != 3) && (objc != 4)) { |
| 9081 |
Tcl_WrongNumArgs(interp, 1, objv, "channelId event ?script?"); |
| 9082 |
return TCL_ERROR; |
| 9083 |
} |
| 9084 |
if (Tcl_GetIndexFromObj(interp, objv[2], modeOptions, "event name", 0, |
| 9085 |
&modeIndex) != TCL_OK) { |
| 9086 |
return TCL_ERROR; |
| 9087 |
} |
| 9088 |
mask = maskArray[modeIndex]; |
| 9089 |
|
| 9090 |
chanName = TclGetString(objv[1]); |
| 9091 |
chan = Tcl_GetChannel(interp, chanName, NULL); |
| 9092 |
if (chan == NULL) { |
| 9093 |
return TCL_ERROR; |
| 9094 |
} |
| 9095 |
chanPtr = (Channel *) chan; |
| 9096 |
statePtr = chanPtr->state; |
| 9097 |
if (GotFlag(statePtr, mask) == 0) { |
| 9098 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf("channel is not %s", |
| 9099 |
(mask == TCL_READABLE) ? "readable" : "writable")); |
| 9100 |
return TCL_ERROR; |
| 9101 |
} |
| 9102 |
|
| 9103 |
/* |
| 9104 |
* If we are supposed to return the script, do so. |
| 9105 |
*/ |
| 9106 |
|
| 9107 |
if (objc == 3) { |
| 9108 |
EventScriptRecord *esPtr; |
| 9109 |
|
| 9110 |
for (esPtr = statePtr->scriptRecordPtr; esPtr != NULL; |
| 9111 |
esPtr = esPtr->nextPtr) { |
| 9112 |
if ((esPtr->interp == interp) && (esPtr->mask == mask)) { |
| 9113 |
Tcl_SetObjResult(interp, esPtr->scriptPtr); |
| 9114 |
break; |
| 9115 |
} |
| 9116 |
} |
| 9117 |
return TCL_OK; |
| 9118 |
} |
| 9119 |
|
| 9120 |
/* |
| 9121 |
* If we are supposed to delete a stored script, do so. |
| 9122 |
*/ |
| 9123 |
|
| 9124 |
if (*(TclGetString(objv[3])) == '\0') { |
| 9125 |
DeleteScriptRecord(interp, chanPtr, mask); |
| 9126 |
return TCL_OK; |
| 9127 |
} |
| 9128 |
|
| 9129 |
/* |
| 9130 |
* Make the script record that will link between the event and the script |
| 9131 |
* to invoke. This also creates a channel event handler which will |
| 9132 |
* evaluate the script in the supplied interpreter. |
| 9133 |
*/ |
| 9134 |
|
| 9135 |
CreateScriptRecord(interp, chanPtr, mask, objv[3]); |
| 9136 |
|
| 9137 |
return TCL_OK; |
| 9138 |
} |
| 9139 |
|
| 9140 |
/* |
| 9141 |
*---------------------------------------------------------------------- |
| 9142 |
* |
| 9143 |
* ZeroTransferTimerProc -- |
| 9144 |
* |
| 9145 |
* Timer handler scheduled by TclCopyChannel so that -command is |
| 9146 |
* called asynchronously even when -size is 0. |
| 9147 |
* |
| 9148 |
* Results: |
| 9149 |
* None. |
| 9150 |
* |
| 9151 |
* Side effects: |
| 9152 |
* Calls CopyData for -command invocation. |
| 9153 |
* |
| 9154 |
*---------------------------------------------------------------------- |
| 9155 |
*/ |
| 9156 |
|
| 9157 |
static void |
| 9158 |
ZeroTransferTimerProc( |
| 9159 |
void *clientData) |
| 9160 |
{ |
| 9161 |
/* calling CopyData with mask==0 still implies immediate invocation of the |
| 9162 |
* -command callback, and completion of the fcopy. |
| 9163 |
*/ |
| 9164 |
CopyData((CopyState *)clientData, 0); |
| 9165 |
} |
| 9166 |
|
| 9167 |
/* |
| 9168 |
*---------------------------------------------------------------------- |
| 9169 |
* |
| 9170 |
* TclCopyChannel -- |
| 9171 |
* |
| 9172 |
* This routine copies data from one channel to another, either |
| 9173 |
* synchronously or asynchronously. If a command script is supplied, the |
| 9174 |
* operation runs in the background. The script is invoked when the copy |
| 9175 |
* completes. Otherwise the function waits until the copy is completed |
| 9176 |
* before returning. |
| 9177 |
* |
| 9178 |
* Results: |
| 9179 |
* A standard Tcl result. |
| 9180 |
* |
| 9181 |
* Side effects: |
| 9182 |
* May schedule a background copy operation that causes both channels to |
| 9183 |
* be marked busy. |
| 9184 |
* |
| 9185 |
*---------------------------------------------------------------------- |
| 9186 |
*/ |
| 9187 |
|
| 9188 |
int |
| 9189 |
TclCopyChannelOld( |
| 9190 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 9191 |
Tcl_Channel inChan, /* Channel to read from. */ |
| 9192 |
Tcl_Channel outChan, /* Channel to write to. */ |
| 9193 |
int toRead, /* Amount of data to copy, or -1 for all. */ |
| 9194 |
Tcl_Obj *cmdPtr) /* Pointer to script to execute or NULL. */ |
| 9195 |
{ |
| 9196 |
return TclCopyChannel(interp, inChan, outChan, toRead, |
| 9197 |
cmdPtr); |
| 9198 |
} |
| 9199 |
|
| 9200 |
int |
| 9201 |
TclCopyChannel( |
| 9202 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 9203 |
Tcl_Channel inChan, /* Channel to read from. */ |
| 9204 |
Tcl_Channel outChan, /* Channel to write to. */ |
| 9205 |
Tcl_WideInt toRead, /* Amount of data to copy, or -1 for all. */ |
| 9206 |
Tcl_Obj *cmdPtr) /* Pointer to script to execute or NULL. */ |
| 9207 |
{ |
| 9208 |
Channel *inPtr = (Channel *) inChan; |
| 9209 |
Channel *outPtr = (Channel *) outChan; |
| 9210 |
ChannelState *inStatePtr, *outStatePtr; |
| 9211 |
int readFlags, writeFlags; |
| 9212 |
CopyState *csPtr; |
| 9213 |
int nonBlocking = (cmdPtr) ? CHANNEL_NONBLOCKING : 0; |
| 9214 |
int moveBytes; |
| 9215 |
|
| 9216 |
inStatePtr = inPtr->state; |
| 9217 |
outStatePtr = outPtr->state; |
| 9218 |
|
| 9219 |
if (BUSY_STATE(inStatePtr, TCL_READABLE)) { |
| 9220 |
if (interp) { |
| 9221 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 9222 |
"channel \"%s\" is busy", Tcl_GetChannelName(inChan))); |
| 9223 |
} |
| 9224 |
return TCL_ERROR; |
| 9225 |
} |
| 9226 |
if (BUSY_STATE(outStatePtr, TCL_WRITABLE)) { |
| 9227 |
if (interp) { |
| 9228 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 9229 |
"channel \"%s\" is busy", Tcl_GetChannelName(outChan))); |
| 9230 |
} |
| 9231 |
return TCL_ERROR; |
| 9232 |
} |
| 9233 |
|
| 9234 |
readFlags = inStatePtr->flags; |
| 9235 |
writeFlags = outStatePtr->flags; |
| 9236 |
|
| 9237 |
/* |
| 9238 |
* Set up the blocking mode appropriately. Background copies need |
| 9239 |
* non-blocking channels. Foreground copies need blocking channels. If |
| 9240 |
* there is an error, restore the old blocking mode. |
| 9241 |
*/ |
| 9242 |
|
| 9243 |
if (nonBlocking != (readFlags & CHANNEL_NONBLOCKING)) { |
| 9244 |
if (SetBlockMode(interp, inPtr, nonBlocking ? |
| 9245 |
TCL_MODE_NONBLOCKING : TCL_MODE_BLOCKING) != TCL_OK) { |
| 9246 |
return TCL_ERROR; |
| 9247 |
} |
| 9248 |
} |
| 9249 |
if ((inPtr!=outPtr) && (nonBlocking!=(writeFlags&CHANNEL_NONBLOCKING)) && |
| 9250 |
(SetBlockMode(NULL, outPtr, nonBlocking ? |
| 9251 |
TCL_MODE_NONBLOCKING : TCL_MODE_BLOCKING) != TCL_OK) && |
| 9252 |
(nonBlocking != (readFlags & CHANNEL_NONBLOCKING))) { |
| 9253 |
SetBlockMode(NULL, inPtr, (readFlags & CHANNEL_NONBLOCKING) |
| 9254 |
? TCL_MODE_NONBLOCKING : TCL_MODE_BLOCKING); |
| 9255 |
return TCL_ERROR; |
| 9256 |
} |
| 9257 |
|
| 9258 |
/* |
| 9259 |
* Make sure the output side is unbuffered. |
| 9260 |
*/ |
| 9261 |
|
| 9262 |
ResetFlag(outStatePtr, CHANNEL_LINEBUFFERED); |
| 9263 |
SetFlag(outStatePtr, CHANNEL_UNBUFFERED); |
| 9264 |
|
| 9265 |
/* |
| 9266 |
* Test for conditions where we know we can just move bytes from input |
| 9267 |
* channel to output channel with no transformation or even examination |
| 9268 |
* of the bytes themselves. |
| 9269 |
*/ |
| 9270 |
|
| 9271 |
moveBytes = inStatePtr->inEofChar == '\0' /* No eofChar to stop input */ |
| 9272 |
&& inStatePtr->inputTranslation == TCL_TRANSLATE_LF |
| 9273 |
&& outStatePtr->outputTranslation == TCL_TRANSLATE_LF |
| 9274 |
&& inStatePtr->encoding == outStatePtr->encoding; |
| 9275 |
|
| 9276 |
/* |
| 9277 |
* Allocate a new CopyState to maintain info about the current copy in |
| 9278 |
* progress. This structure will be deallocated when the copy is |
| 9279 |
* completed. |
| 9280 |
*/ |
| 9281 |
|
| 9282 |
csPtr = (CopyState *)ckalloc(TclOffset(CopyState, buffer) + 1U + !moveBytes * inStatePtr->bufSize); |
| 9283 |
csPtr->bufSize = !moveBytes * inStatePtr->bufSize; |
| 9284 |
csPtr->readPtr = inPtr; |
| 9285 |
csPtr->writePtr = outPtr; |
| 9286 |
csPtr->refCount = 2; /* two references below (inStatePtr, outStatePtr) */ |
| 9287 |
csPtr->readFlags = readFlags; |
| 9288 |
csPtr->writeFlags = writeFlags; |
| 9289 |
csPtr->toRead = toRead; |
| 9290 |
csPtr->total = 0; |
| 9291 |
csPtr->interp = interp; |
| 9292 |
if (cmdPtr) { |
| 9293 |
Tcl_IncrRefCount(cmdPtr); |
| 9294 |
} |
| 9295 |
csPtr->cmdPtr = cmdPtr; |
| 9296 |
|
| 9297 |
TclChannelPreserve(inChan); |
| 9298 |
TclChannelPreserve(outChan); |
| 9299 |
|
| 9300 |
inStatePtr->csPtrR = csPtr; |
| 9301 |
outStatePtr->csPtrW = csPtr; |
| 9302 |
|
| 9303 |
if (moveBytes) { |
| 9304 |
return MoveBytes(csPtr); |
| 9305 |
} |
| 9306 |
|
| 9307 |
/* |
| 9308 |
* Special handling of -size 0 async transfers, so that the -command is |
| 9309 |
* still called asynchronously. |
| 9310 |
*/ |
| 9311 |
|
| 9312 |
if ((nonBlocking == CHANNEL_NONBLOCKING) && (toRead == 0)) { |
| 9313 |
Tcl_CreateTimerHandler(0, ZeroTransferTimerProc, csPtr); |
| 9314 |
return TCL_OK; |
| 9315 |
} |
| 9316 |
|
| 9317 |
/* |
| 9318 |
* Start copying data between the channels. |
| 9319 |
*/ |
| 9320 |
|
| 9321 |
return CopyData(csPtr, 0); |
| 9322 |
} |
| 9323 |
|
| 9324 |
/* |
| 9325 |
*---------------------------------------------------------------------- |
| 9326 |
* |
| 9327 |
* CopyData -- |
| 9328 |
* |
| 9329 |
* This function implements the lowest level of the copying mechanism for |
| 9330 |
* TclCopyChannel. |
| 9331 |
* |
| 9332 |
* Results: |
| 9333 |
* Returns TCL_OK on success, else TCL_ERROR. |
| 9334 |
* |
| 9335 |
* Side effects: |
| 9336 |
* Moves data between channels, may create channel handlers. |
| 9337 |
* |
| 9338 |
*---------------------------------------------------------------------- |
| 9339 |
*/ |
| 9340 |
|
| 9341 |
static void |
| 9342 |
MBCallback( |
| 9343 |
CopyState *csPtr, |
| 9344 |
Tcl_Obj *errObj) |
| 9345 |
{ |
| 9346 |
Tcl_Obj *cmdPtr = Tcl_DuplicateObj(csPtr->cmdPtr); |
| 9347 |
Tcl_WideInt total = csPtr->total; |
| 9348 |
Tcl_Interp *interp = csPtr->interp; |
| 9349 |
int code; |
| 9350 |
|
| 9351 |
Tcl_IncrRefCount(cmdPtr); |
| 9352 |
StopCopy(csPtr); |
| 9353 |
|
| 9354 |
/* TODO: What if cmdPtr is not a list?! */ |
| 9355 |
|
| 9356 |
Tcl_ListObjAppendElement(NULL, cmdPtr, Tcl_NewWideIntObj(total)); |
| 9357 |
if (errObj) { |
| 9358 |
Tcl_ListObjAppendElement(NULL, cmdPtr, errObj); |
| 9359 |
} |
| 9360 |
|
| 9361 |
Tcl_Preserve(interp); |
| 9362 |
code = Tcl_EvalObjEx(interp, cmdPtr, TCL_EVAL_GLOBAL); |
| 9363 |
if (code != TCL_OK) { |
| 9364 |
Tcl_BackgroundException(interp, code); |
| 9365 |
} |
| 9366 |
Tcl_Release(interp); |
| 9367 |
TclDecrRefCount(cmdPtr); |
| 9368 |
} |
| 9369 |
|
| 9370 |
static void |
| 9371 |
MBError( |
| 9372 |
CopyState *csPtr, |
| 9373 |
int mask, |
| 9374 |
int errorCode) |
| 9375 |
{ |
| 9376 |
Tcl_Channel inChan = (Tcl_Channel) csPtr->readPtr; |
| 9377 |
Tcl_Channel outChan = (Tcl_Channel) csPtr->writePtr; |
| 9378 |
Tcl_Obj *errObj; |
| 9379 |
|
| 9380 |
Tcl_SetErrno(errorCode); |
| 9381 |
|
| 9382 |
errObj = Tcl_ObjPrintf( "error %sing \"%s\": %s", |
| 9383 |
(mask & TCL_READABLE) ? "read" : "writ", |
| 9384 |
Tcl_GetChannelName((mask & TCL_READABLE) ? inChan : outChan), |
| 9385 |
Tcl_PosixError(csPtr->interp)); |
| 9386 |
|
| 9387 |
if (csPtr->cmdPtr) { |
| 9388 |
MBCallback(csPtr, errObj); |
| 9389 |
} else { |
| 9390 |
Tcl_SetObjResult(csPtr->interp, errObj); |
| 9391 |
StopCopy(csPtr); |
| 9392 |
} |
| 9393 |
} |
| 9394 |
|
| 9395 |
static void |
| 9396 |
MBEvent( |
| 9397 |
void *clientData, |
| 9398 |
int mask) |
| 9399 |
{ |
| 9400 |
CopyState *csPtr = (CopyState *) clientData; |
| 9401 |
Tcl_Channel inChan = (Tcl_Channel) csPtr->readPtr; |
| 9402 |
Tcl_Channel outChan = (Tcl_Channel) csPtr->writePtr; |
| 9403 |
ChannelState *inStatePtr = csPtr->readPtr->state; |
| 9404 |
|
| 9405 |
if (mask & TCL_WRITABLE) { |
| 9406 |
Tcl_DeleteChannelHandler(inChan, MBEvent, csPtr); |
| 9407 |
Tcl_DeleteChannelHandler(outChan, MBEvent, csPtr); |
| 9408 |
switch (MBWrite(csPtr)) { |
| 9409 |
case TCL_OK: |
| 9410 |
MBCallback(csPtr, NULL); |
| 9411 |
break; |
| 9412 |
case TCL_CONTINUE: |
| 9413 |
Tcl_CreateChannelHandler(inChan, TCL_READABLE, MBEvent, csPtr); |
| 9414 |
break; |
| 9415 |
} |
| 9416 |
} else if (mask & TCL_READABLE) { |
| 9417 |
if (TCL_OK == MBRead(csPtr)) { |
| 9418 |
/* When at least one full buffer is present, stop reading. */ |
| 9419 |
if (IsBufferFull(inStatePtr->inQueueHead) |
| 9420 |
|| !Tcl_InputBlocked(inChan)) { |
| 9421 |
Tcl_DeleteChannelHandler(inChan, MBEvent, csPtr); |
| 9422 |
} |
| 9423 |
|
| 9424 |
/* Successful read -- set up to write the bytes we read */ |
| 9425 |
Tcl_CreateChannelHandler(outChan, TCL_WRITABLE, MBEvent, csPtr); |
| 9426 |
} |
| 9427 |
} |
| 9428 |
} |
| 9429 |
|
| 9430 |
static int |
| 9431 |
MBRead( |
| 9432 |
CopyState *csPtr) |
| 9433 |
{ |
| 9434 |
ChannelState *inStatePtr = csPtr->readPtr->state; |
| 9435 |
ChannelBuffer *bufPtr = inStatePtr->inQueueHead; |
| 9436 |
int code; |
| 9437 |
|
| 9438 |
if (bufPtr && BytesLeft(bufPtr) > 0) { |
| 9439 |
return TCL_OK; |
| 9440 |
} |
| 9441 |
|
| 9442 |
code = GetInput(inStatePtr->topChanPtr); |
| 9443 |
if (code == 0 || GotFlag(inStatePtr, CHANNEL_BLOCKED)) { |
| 9444 |
return TCL_OK; |
| 9445 |
} else { |
| 9446 |
MBError(csPtr, TCL_READABLE, code); |
| 9447 |
return TCL_ERROR; |
| 9448 |
} |
| 9449 |
} |
| 9450 |
|
| 9451 |
static int |
| 9452 |
MBWrite( |
| 9453 |
CopyState *csPtr) |
| 9454 |
{ |
| 9455 |
ChannelState *inStatePtr = csPtr->readPtr->state; |
| 9456 |
ChannelState *outStatePtr = csPtr->writePtr->state; |
| 9457 |
ChannelBuffer *bufPtr = inStatePtr->inQueueHead; |
| 9458 |
ChannelBuffer *tail = NULL; |
| 9459 |
int code; |
| 9460 |
Tcl_WideInt inBytes = 0; |
| 9461 |
|
| 9462 |
/* Count up number of bytes waiting in the input queue */ |
| 9463 |
while (bufPtr) { |
| 9464 |
inBytes += BytesLeft(bufPtr); |
| 9465 |
tail = bufPtr; |
| 9466 |
if (csPtr->toRead != -1 && csPtr->toRead < inBytes) { |
| 9467 |
/* Queue has enough bytes to complete the copy */ |
| 9468 |
break; |
| 9469 |
} |
| 9470 |
bufPtr = bufPtr->nextPtr; |
| 9471 |
} |
| 9472 |
|
| 9473 |
if (bufPtr) { |
| 9474 |
/* Split the overflowing buffer in two */ |
| 9475 |
int extra = (int) (inBytes - csPtr->toRead); |
| 9476 |
/* Note that going with int for extra assumes that inBytes is not too |
| 9477 |
* much over toRead to require a wide itself. If that gets violated |
| 9478 |
* then the calculations involving extra must be made wide too. |
| 9479 |
* |
| 9480 |
* Noted with Win32/MSVC debug build treating the warning (possible of |
| 9481 |
* data in __int64 to int conversion) as error. |
| 9482 |
*/ |
| 9483 |
|
| 9484 |
bufPtr = AllocChannelBuffer(extra); |
| 9485 |
|
| 9486 |
tail->nextAdded -= extra; |
| 9487 |
memcpy(InsertPoint(bufPtr), InsertPoint(tail), extra); |
| 9488 |
bufPtr->nextAdded += extra; |
| 9489 |
bufPtr->nextPtr = tail->nextPtr; |
| 9490 |
tail->nextPtr = NULL; |
| 9491 |
inBytes = csPtr->toRead; |
| 9492 |
} |
| 9493 |
|
| 9494 |
/* Update the byte counts */ |
| 9495 |
if (csPtr->toRead != -1) { |
| 9496 |
csPtr->toRead -= inBytes; |
| 9497 |
} |
| 9498 |
csPtr->total += inBytes; |
| 9499 |
|
| 9500 |
/* Move buffers from input to output channels */ |
| 9501 |
if (outStatePtr->outQueueTail) { |
| 9502 |
outStatePtr->outQueueTail->nextPtr = inStatePtr->inQueueHead; |
| 9503 |
} else { |
| 9504 |
outStatePtr->outQueueHead = inStatePtr->inQueueHead; |
| 9505 |
} |
| 9506 |
outStatePtr->outQueueTail = tail; |
| 9507 |
inStatePtr->inQueueHead = bufPtr; |
| 9508 |
if (inStatePtr->inQueueTail == tail) { |
| 9509 |
inStatePtr->inQueueTail = bufPtr; |
| 9510 |
} |
| 9511 |
if (bufPtr == NULL) { |
| 9512 |
inStatePtr->inQueueTail = NULL; |
| 9513 |
} |
| 9514 |
|
| 9515 |
code = FlushChannel(csPtr->interp, outStatePtr->topChanPtr, 0); |
| 9516 |
if (code) { |
| 9517 |
MBError(csPtr, TCL_WRITABLE, code); |
| 9518 |
return TCL_ERROR; |
| 9519 |
} |
| 9520 |
if (csPtr->toRead == 0 || GotFlag(inStatePtr, CHANNEL_EOF)) { |
| 9521 |
return TCL_OK; |
| 9522 |
} |
| 9523 |
return TCL_CONTINUE; |
| 9524 |
} |
| 9525 |
|
| 9526 |
static int |
| 9527 |
MoveBytes( |
| 9528 |
CopyState *csPtr) /* State of copy operation. */ |
| 9529 |
{ |
| 9530 |
ChannelState *outStatePtr = csPtr->writePtr->state; |
| 9531 |
ChannelBuffer *bufPtr = outStatePtr->curOutPtr; |
| 9532 |
int errorCode; |
| 9533 |
|
| 9534 |
if (bufPtr && BytesLeft(bufPtr)) { |
| 9535 |
/* If we start with unflushed bytes in the destination |
| 9536 |
* channel, flush them out of the way first. */ |
| 9537 |
|
| 9538 |
errorCode = FlushChannel(csPtr->interp, outStatePtr->topChanPtr, 0); |
| 9539 |
if (errorCode != 0) { |
| 9540 |
MBError(csPtr, TCL_WRITABLE, errorCode); |
| 9541 |
return TCL_ERROR; |
| 9542 |
} |
| 9543 |
} |
| 9544 |
|
| 9545 |
if (csPtr->cmdPtr) { |
| 9546 |
Tcl_Channel inChan = (Tcl_Channel) csPtr->readPtr; |
| 9547 |
Tcl_CreateChannelHandler(inChan, TCL_READABLE, MBEvent, csPtr); |
| 9548 |
return TCL_OK; |
| 9549 |
} |
| 9550 |
|
| 9551 |
while (1) { |
| 9552 |
int code; |
| 9553 |
|
| 9554 |
if (TCL_ERROR == MBRead(csPtr)) { |
| 9555 |
return TCL_ERROR; |
| 9556 |
} |
| 9557 |
code = MBWrite(csPtr); |
| 9558 |
if (code == TCL_OK) { |
| 9559 |
Tcl_SetObjResult(csPtr->interp, Tcl_NewWideIntObj(csPtr->total)); |
| 9560 |
StopCopy(csPtr); |
| 9561 |
return TCL_OK; |
| 9562 |
} |
| 9563 |
if (code == TCL_ERROR) { |
| 9564 |
return TCL_ERROR; |
| 9565 |
} |
| 9566 |
/* code == TCL_CONTINUE --> continue the loop */ |
| 9567 |
} |
| 9568 |
return TCL_OK; /* Silence compiler warnings */ |
| 9569 |
} |
| 9570 |
|
| 9571 |
static int |
| 9572 |
CopyData( |
| 9573 |
CopyState *csPtr, /* State of copy operation. */ |
| 9574 |
int mask) /* Current channel event flags. */ |
| 9575 |
{ |
| 9576 |
Tcl_Interp *interp; |
| 9577 |
Tcl_Obj *cmdPtr, *errObj = NULL, *bufObj = NULL, *msg = NULL; |
| 9578 |
Tcl_Channel inChan, outChan; |
| 9579 |
ChannelState *inStatePtr, *outStatePtr; |
| 9580 |
int result = TCL_OK; |
| 9581 |
int sizeb; |
| 9582 |
Tcl_WideInt total; |
| 9583 |
int size; |
| 9584 |
const char *buffer; |
| 9585 |
int inBinary, outBinary, sameEncoding; |
| 9586 |
/* Encoding control */ |
| 9587 |
int underflow; /* Input underflow */ |
| 9588 |
|
| 9589 |
csPtr->refCount++; /* avoid freeing during handling */ |
| 9590 |
|
| 9591 |
inChan = (Tcl_Channel) csPtr->readPtr; |
| 9592 |
outChan = (Tcl_Channel) csPtr->writePtr; |
| 9593 |
inStatePtr = csPtr->readPtr->state; |
| 9594 |
outStatePtr = csPtr->writePtr->state; |
| 9595 |
interp = csPtr->interp; |
| 9596 |
cmdPtr = csPtr->cmdPtr; |
| 9597 |
|
| 9598 |
/* |
| 9599 |
* Copy the data the slow way, using the translation mechanism. |
| 9600 |
* |
| 9601 |
* Note: We have make sure that we use the topmost channel in a stack for |
| 9602 |
* the copying. The caller uses Tcl_GetChannel to access it, and thus gets |
| 9603 |
* the bottom of the stack. |
| 9604 |
*/ |
| 9605 |
|
| 9606 |
inBinary = (inStatePtr->encoding == NULL); |
| 9607 |
outBinary = (outStatePtr->encoding == NULL); |
| 9608 |
sameEncoding = (inStatePtr->encoding == outStatePtr->encoding); |
| 9609 |
|
| 9610 |
if (!(inBinary || sameEncoding)) { |
| 9611 |
TclNewObj(bufObj); |
| 9612 |
Tcl_IncrRefCount(bufObj); |
| 9613 |
} |
| 9614 |
|
| 9615 |
while (csPtr->toRead != 0) { |
| 9616 |
/* |
| 9617 |
* Check for unreported background errors. |
| 9618 |
*/ |
| 9619 |
|
| 9620 |
Tcl_GetChannelError(inChan, &msg); |
| 9621 |
if ((inStatePtr->unreportedError != 0) || (msg != NULL)) { |
| 9622 |
Tcl_SetErrno(inStatePtr->unreportedError); |
| 9623 |
inStatePtr->unreportedError = 0; |
| 9624 |
goto readError; |
| 9625 |
} |
| 9626 |
Tcl_GetChannelError(outChan, &msg); |
| 9627 |
if ((outStatePtr->unreportedError != 0) || (msg != NULL)) { |
| 9628 |
Tcl_SetErrno(outStatePtr->unreportedError); |
| 9629 |
outStatePtr->unreportedError = 0; |
| 9630 |
goto writeError; |
| 9631 |
} |
| 9632 |
|
| 9633 |
if (cmdPtr && (mask == 0)) { |
| 9634 |
/* |
| 9635 |
* In async mode, we skip reading synchronously and fake an |
| 9636 |
* underflow instead to prime the readable fileevent. |
| 9637 |
*/ |
| 9638 |
|
| 9639 |
size = 0; |
| 9640 |
underflow = 1; |
| 9641 |
} else { |
| 9642 |
/* |
| 9643 |
* Read up to bufSize bytes. |
| 9644 |
*/ |
| 9645 |
|
| 9646 |
if ((csPtr->toRead == -1) |
| 9647 |
|| (csPtr->toRead > (Tcl_WideInt)csPtr->bufSize)) { |
| 9648 |
sizeb = csPtr->bufSize; |
| 9649 |
} else { |
| 9650 |
sizeb = (int) csPtr->toRead; |
| 9651 |
} |
| 9652 |
|
| 9653 |
if (inBinary || sameEncoding) { |
| 9654 |
size = DoRead(inStatePtr->topChanPtr, csPtr->buffer, sizeb, |
| 9655 |
!GotFlag(inStatePtr, CHANNEL_NONBLOCKING)); |
| 9656 |
} else { |
| 9657 |
size = DoReadChars(inStatePtr->topChanPtr, bufObj, sizeb, |
| 9658 |
0 /* No append */); |
| 9659 |
} |
| 9660 |
underflow = (size >= 0) && (size < sizeb); /* Input underflow */ |
| 9661 |
} |
| 9662 |
|
| 9663 |
if (size < 0) { |
| 9664 |
readError: |
| 9665 |
if (interp) { |
| 9666 |
TclNewObj(errObj); |
| 9667 |
Tcl_AppendStringsToObj(errObj, "error reading \"", |
| 9668 |
Tcl_GetChannelName(inChan), "\": ", (char *)NULL); |
| 9669 |
if (msg != NULL) { |
| 9670 |
Tcl_AppendObjToObj(errObj, msg); |
| 9671 |
} else { |
| 9672 |
Tcl_AppendStringsToObj(errObj, Tcl_PosixError(interp), |
| 9673 |
(char *)NULL); |
| 9674 |
} |
| 9675 |
} |
| 9676 |
if (msg != NULL) { |
| 9677 |
Tcl_DecrRefCount(msg); |
| 9678 |
} |
| 9679 |
break; |
| 9680 |
} else if (underflow) { |
| 9681 |
/* |
| 9682 |
* We had an underflow on the read side. If we are at EOF, and not |
| 9683 |
* in the synchronous part of an asynchronous fcopy, then the |
| 9684 |
* copying is done, otherwise set up a channel handler to detect |
| 9685 |
* when the channel becomes readable again. |
| 9686 |
*/ |
| 9687 |
|
| 9688 |
if ((size == 0) && Tcl_Eof(inChan) && !(cmdPtr && (mask == 0))) { |
| 9689 |
break; |
| 9690 |
} |
| 9691 |
if (cmdPtr && (!Tcl_Eof(inChan) || (mask == 0)) |
| 9692 |
&& !(mask & TCL_READABLE)) { |
| 9693 |
if (mask & TCL_WRITABLE) { |
| 9694 |
Tcl_DeleteChannelHandler(outChan, CopyEventProc, csPtr); |
| 9695 |
} |
| 9696 |
Tcl_CreateChannelHandler(inChan, TCL_READABLE, CopyEventProc, |
| 9697 |
csPtr); |
| 9698 |
} |
| 9699 |
if (size == 0) { |
| 9700 |
if (!GotFlag(inStatePtr, CHANNEL_NONBLOCKING)) { |
| 9701 |
/* |
| 9702 |
* We allowed a short read. Keep trying. |
| 9703 |
*/ |
| 9704 |
|
| 9705 |
continue; |
| 9706 |
} |
| 9707 |
if (bufObj != NULL) { |
| 9708 |
TclDecrRefCount(bufObj); |
| 9709 |
bufObj = NULL; |
| 9710 |
} |
| 9711 |
goto done; |
| 9712 |
} |
| 9713 |
} |
| 9714 |
|
| 9715 |
/* |
| 9716 |
* Now write the buffer out. |
| 9717 |
*/ |
| 9718 |
|
| 9719 |
if (inBinary || sameEncoding) { |
| 9720 |
buffer = csPtr->buffer; |
| 9721 |
sizeb = size; |
| 9722 |
} else { |
| 9723 |
buffer = TclGetStringFromObj(bufObj, &sizeb); |
| 9724 |
} |
| 9725 |
|
| 9726 |
if (outBinary || sameEncoding) { |
| 9727 |
sizeb = WriteBytes(outStatePtr->topChanPtr, buffer, sizeb); |
| 9728 |
} else { |
| 9729 |
sizeb = WriteChars(outStatePtr->topChanPtr, buffer, sizeb); |
| 9730 |
} |
| 9731 |
|
| 9732 |
/* |
| 9733 |
* [Bug 2895565]. At this point 'size' still contains the number of |
| 9734 |
* bytes or characters which have been read. We keep this to later to |
| 9735 |
* update the totals and toRead information, see marker (UP) below. We |
| 9736 |
* must not overwrite it with 'sizeb', which is the number of written |
| 9737 |
* bytes or characters, and both EOL translation and encoding |
| 9738 |
* conversion may have changed this number unpredictably in relation |
| 9739 |
* to 'size' (It can be smaller or larger, in the latter case able to |
| 9740 |
* drive toRead below -1, causing infinite looping). Completely |
| 9741 |
* unsuitable for updating totals and toRead. |
| 9742 |
*/ |
| 9743 |
|
| 9744 |
if (sizeb < 0) { |
| 9745 |
writeError: |
| 9746 |
if (interp) { |
| 9747 |
TclNewObj(errObj); |
| 9748 |
Tcl_AppendStringsToObj(errObj, "error writing \"", |
| 9749 |
Tcl_GetChannelName(outChan), "\": ", (char *)NULL); |
| 9750 |
if (msg != NULL) { |
| 9751 |
Tcl_AppendObjToObj(errObj, msg); |
| 9752 |
} else { |
| 9753 |
Tcl_AppendStringsToObj(errObj, Tcl_PosixError(interp), |
| 9754 |
(char *)NULL); |
| 9755 |
} |
| 9756 |
} |
| 9757 |
if (msg != NULL) { |
| 9758 |
Tcl_DecrRefCount(msg); |
| 9759 |
} |
| 9760 |
break; |
| 9761 |
} |
| 9762 |
|
| 9763 |
/* |
| 9764 |
* Update the current byte count. Do it now so the count is valid |
| 9765 |
* before a return or break takes us out of the loop. The invariant at |
| 9766 |
* the top of the loop should be that csPtr->toRead holds the number |
| 9767 |
* of bytes left to copy. |
| 9768 |
*/ |
| 9769 |
|
| 9770 |
if (csPtr->toRead != -1) { |
| 9771 |
csPtr->toRead -= size; |
| 9772 |
} |
| 9773 |
csPtr->total += size; |
| 9774 |
|
| 9775 |
/* |
| 9776 |
* Break loop if EOF && (size>0) |
| 9777 |
*/ |
| 9778 |
|
| 9779 |
if (Tcl_Eof(inChan)) { |
| 9780 |
break; |
| 9781 |
} |
| 9782 |
|
| 9783 |
/* |
| 9784 |
* Check to see if the write is happening in the background. If so, |
| 9785 |
* stop copying and wait for the channel to become writable again. |
| 9786 |
* After input underflow we already installed a readable handler |
| 9787 |
* therefore we don't need a writable handler. |
| 9788 |
*/ |
| 9789 |
|
| 9790 |
if (!underflow && GotFlag(outStatePtr, BG_FLUSH_SCHEDULED)) { |
| 9791 |
if (!(mask & TCL_WRITABLE)) { |
| 9792 |
if (mask & TCL_READABLE) { |
| 9793 |
Tcl_DeleteChannelHandler(inChan, CopyEventProc, csPtr); |
| 9794 |
} |
| 9795 |
Tcl_CreateChannelHandler(outChan, TCL_WRITABLE, |
| 9796 |
CopyEventProc, csPtr); |
| 9797 |
} |
| 9798 |
if (bufObj != NULL) { |
| 9799 |
TclDecrRefCount(bufObj); |
| 9800 |
bufObj = NULL; |
| 9801 |
} |
| 9802 |
goto done; |
| 9803 |
} |
| 9804 |
|
| 9805 |
/* |
| 9806 |
* For background copies, we only do one buffer per invocation so we |
| 9807 |
* don't starve the rest of the system. |
| 9808 |
*/ |
| 9809 |
|
| 9810 |
if (cmdPtr && (csPtr->toRead != 0)) { |
| 9811 |
/* |
| 9812 |
* The first time we enter this code, there won't be a channel |
| 9813 |
* handler established yet, so do it here. |
| 9814 |
*/ |
| 9815 |
|
| 9816 |
if (mask == 0) { |
| 9817 |
Tcl_CreateChannelHandler(outChan, TCL_WRITABLE, CopyEventProc, |
| 9818 |
csPtr); |
| 9819 |
} |
| 9820 |
if (bufObj != NULL) { |
| 9821 |
TclDecrRefCount(bufObj); |
| 9822 |
bufObj = NULL; |
| 9823 |
} |
| 9824 |
goto done; |
| 9825 |
} |
| 9826 |
} /* while */ |
| 9827 |
|
| 9828 |
if (bufObj != NULL) { |
| 9829 |
TclDecrRefCount(bufObj); |
| 9830 |
bufObj = NULL; |
| 9831 |
} |
| 9832 |
|
| 9833 |
/* |
| 9834 |
* Make the callback or return the number of bytes transferred. The local |
| 9835 |
* total is used because StopCopy frees csPtr. |
| 9836 |
*/ |
| 9837 |
|
| 9838 |
total = csPtr->total; |
| 9839 |
if (cmdPtr && interp) { |
| 9840 |
int code; |
| 9841 |
|
| 9842 |
/* |
| 9843 |
* Get a private copy of the command so we can mutate it by adding |
| 9844 |
* arguments. Note that StopCopy frees our saved reference to the |
| 9845 |
* original command obj. |
| 9846 |
*/ |
| 9847 |
|
| 9848 |
cmdPtr = Tcl_DuplicateObj(cmdPtr); |
| 9849 |
Tcl_IncrRefCount(cmdPtr); |
| 9850 |
StopCopy(csPtr); |
| 9851 |
Tcl_Preserve(interp); |
| 9852 |
|
| 9853 |
Tcl_ListObjAppendElement(interp, cmdPtr, Tcl_NewWideIntObj(total)); |
| 9854 |
if (errObj) { |
| 9855 |
Tcl_ListObjAppendElement(interp, cmdPtr, errObj); |
| 9856 |
} |
| 9857 |
code = Tcl_EvalObjEx(interp, cmdPtr, TCL_EVAL_GLOBAL); |
| 9858 |
if (code != TCL_OK) { |
| 9859 |
Tcl_BackgroundException(interp, code); |
| 9860 |
result = TCL_ERROR; |
| 9861 |
} |
| 9862 |
TclDecrRefCount(cmdPtr); |
| 9863 |
Tcl_Release(interp); |
| 9864 |
} else { |
| 9865 |
StopCopy(csPtr); |
| 9866 |
if (interp) { |
| 9867 |
if (errObj) { |
| 9868 |
Tcl_SetObjResult(interp, errObj); |
| 9869 |
result = TCL_ERROR; |
| 9870 |
} else { |
| 9871 |
Tcl_ResetResult(interp); |
| 9872 |
Tcl_SetObjResult(interp, Tcl_NewWideIntObj(total)); |
| 9873 |
} |
| 9874 |
} |
| 9875 |
} |
| 9876 |
|
| 9877 |
done: |
| 9878 |
CopyDecrRefCount(csPtr); |
| 9879 |
return result; |
| 9880 |
} |
| 9881 |
|
| 9882 |
/* |
| 9883 |
*---------------------------------------------------------------------- |
| 9884 |
* |
| 9885 |
* DoRead -- |
| 9886 |
* |
| 9887 |
* Stores up to "bytesToRead" bytes in memory pointed to by "dst". |
| 9888 |
* These bytes come from reading the channel "chanPtr" and |
| 9889 |
* performing the configured translations. No encoding conversions |
| 9890 |
* are applied to the bytes being read. |
| 9891 |
* |
| 9892 |
* Results: |
| 9893 |
* The number of bytes actually stored (<= bytesToRead), |
| 9894 |
* or -1 if there is an error in reading the channel. Use |
| 9895 |
* Tcl_GetErrno() to retrieve the error code for the error |
| 9896 |
* that occurred. |
| 9897 |
* |
| 9898 |
* The number of bytes stored can be less than the number |
| 9899 |
* requested when |
| 9900 |
* - EOF is reached on the channel; or |
| 9901 |
* - the channel is non-blocking, and we've read all we can |
| 9902 |
* without blocking. |
| 9903 |
* - a channel reading error occurs (and we return -1) |
| 9904 |
* |
| 9905 |
* Side effects: |
| 9906 |
* May cause input to be buffered. |
| 9907 |
* |
| 9908 |
*---------------------------------------------------------------------- |
| 9909 |
*/ |
| 9910 |
|
| 9911 |
static int |
| 9912 |
DoRead( |
| 9913 |
Channel *chanPtr, /* The channel from which to read. */ |
| 9914 |
char *dst, /* Where to store input read. */ |
| 9915 |
int bytesToRead, /* Maximum number of bytes to read. */ |
| 9916 |
int allowShortReads) /* Allow half-blocking (pipes,sockets) */ |
| 9917 |
{ |
| 9918 |
ChannelState *statePtr = chanPtr->state; |
| 9919 |
char *p = dst; |
| 9920 |
|
| 9921 |
assert(bytesToRead >= 0); |
| 9922 |
|
| 9923 |
/* |
| 9924 |
* Early out when we know a read will get the eofchar. |
| 9925 |
* |
| 9926 |
* NOTE: This seems to be a bug. The special handling for |
| 9927 |
* a zero-char read request ought to come first. As coded |
| 9928 |
* the EOF due to eofchar has distinguishing behavior from |
| 9929 |
* the EOF due to reported EOF on the underlying device, and |
| 9930 |
* that seems undesirable. However recent history indicates |
| 9931 |
* that new inconsistent behavior in a patchlevel has problems |
| 9932 |
* too. Keep on keeping on for now. |
| 9933 |
*/ |
| 9934 |
|
| 9935 |
if (GotFlag(statePtr, CHANNEL_STICKY_EOF)) { |
| 9936 |
SetFlag(statePtr, CHANNEL_EOF); |
| 9937 |
assert(statePtr->inputEncodingFlags & TCL_ENCODING_END); |
| 9938 |
assert(!GotFlag(statePtr, CHANNEL_BLOCKED|INPUT_SAW_CR)); |
| 9939 |
|
| 9940 |
/* TODO: Don't need this call */ |
| 9941 |
UpdateInterest(chanPtr); |
| 9942 |
return 0; |
| 9943 |
} |
| 9944 |
|
| 9945 |
/* |
| 9946 |
* Special handling for zero-char read request. |
| 9947 |
*/ |
| 9948 |
|
| 9949 |
if (bytesToRead == 0) { |
| 9950 |
if (GotFlag(statePtr, CHANNEL_EOF)) { |
| 9951 |
statePtr->inputEncodingFlags |= TCL_ENCODING_START; |
| 9952 |
} |
| 9953 |
ResetFlag(statePtr, CHANNEL_BLOCKED|CHANNEL_EOF); |
| 9954 |
statePtr->inputEncodingFlags &= ~TCL_ENCODING_END; |
| 9955 |
/* TODO: Don't need this call */ |
| 9956 |
UpdateInterest(chanPtr); |
| 9957 |
return 0; |
| 9958 |
} |
| 9959 |
|
| 9960 |
TclChannelPreserve((Tcl_Channel)chanPtr); |
| 9961 |
while (bytesToRead) { |
| 9962 |
/* |
| 9963 |
* Each pass through the loop is intended to process up to one channel |
| 9964 |
* buffer. |
| 9965 |
*/ |
| 9966 |
|
| 9967 |
int bytesRead, bytesWritten; |
| 9968 |
ChannelBuffer *bufPtr = statePtr->inQueueHead; |
| 9969 |
|
| 9970 |
/* |
| 9971 |
* Don't read more data if we have what we need. |
| 9972 |
*/ |
| 9973 |
|
| 9974 |
while (!bufPtr || /* We got no buffer! OR */ |
| 9975 |
(!IsBufferFull(bufPtr) && /* Our buffer has room AND */ |
| 9976 |
(BytesLeft(bufPtr) < bytesToRead))) { |
| 9977 |
/* Not enough bytes in it yet |
| 9978 |
* to fill the dst */ |
| 9979 |
int code; |
| 9980 |
|
| 9981 |
moreData: |
| 9982 |
code = GetInput(chanPtr); |
| 9983 |
bufPtr = statePtr->inQueueHead; |
| 9984 |
|
| 9985 |
if (GotFlag(statePtr, CHANNEL_EOF|CHANNEL_BLOCKED)) { |
| 9986 |
/* |
| 9987 |
* Further reads cannot do any more. |
| 9988 |
*/ |
| 9989 |
|
| 9990 |
break; |
| 9991 |
} |
| 9992 |
|
| 9993 |
if (code || !bufPtr) { |
| 9994 |
/* Read error (or channel dead/closed) */ |
| 9995 |
goto readErr; |
| 9996 |
} |
| 9997 |
|
| 9998 |
assert(IsBufferFull(bufPtr)); |
| 9999 |
} |
| 10000 |
|
| 10001 |
if (!bufPtr) { |
| 10002 |
readErr: |
| 10003 |
|
| 10004 |
UpdateInterest(chanPtr); |
| 10005 |
TclChannelRelease((Tcl_Channel)chanPtr); |
| 10006 |
return -1; |
| 10007 |
} |
| 10008 |
|
| 10009 |
bytesRead = BytesLeft(bufPtr); |
| 10010 |
bytesWritten = bytesToRead; |
| 10011 |
|
| 10012 |
TranslateInputEOL(statePtr, p, RemovePoint(bufPtr), |
| 10013 |
&bytesWritten, &bytesRead); |
| 10014 |
bufPtr->nextRemoved += bytesRead; |
| 10015 |
p += bytesWritten; |
| 10016 |
bytesToRead -= bytesWritten; |
| 10017 |
|
| 10018 |
if (!IsBufferEmpty(bufPtr)) { |
| 10019 |
/* |
| 10020 |
* Buffer is not empty. How can that be? |
| 10021 |
* |
| 10022 |
* 0) We stopped early because we got all the bytes we were |
| 10023 |
* seeking. That's fine. |
| 10024 |
*/ |
| 10025 |
|
| 10026 |
if (bytesToRead == 0) { |
| 10027 |
break; |
| 10028 |
} |
| 10029 |
|
| 10030 |
/* |
| 10031 |
* 1) We're @EOF because we saw eof char. |
| 10032 |
*/ |
| 10033 |
|
| 10034 |
if (GotFlag(statePtr, CHANNEL_STICKY_EOF)) { |
| 10035 |
break; |
| 10036 |
} |
| 10037 |
|
| 10038 |
/* |
| 10039 |
* 2) The buffer holds a \r while in CRLF translation, followed by |
| 10040 |
* the end of the buffer. |
| 10041 |
*/ |
| 10042 |
|
| 10043 |
assert(statePtr->inputTranslation == TCL_TRANSLATE_CRLF); |
| 10044 |
assert(RemovePoint(bufPtr)[0] == '\r'); |
| 10045 |
assert(BytesLeft(bufPtr) == 1); |
| 10046 |
|
| 10047 |
if (bufPtr->nextPtr == NULL) { |
| 10048 |
/* |
| 10049 |
* There's no more buffered data... |
| 10050 |
*/ |
| 10051 |
|
| 10052 |
if (GotFlag(statePtr, CHANNEL_EOF)) { |
| 10053 |
/* |
| 10054 |
* ...and there never will be. |
| 10055 |
*/ |
| 10056 |
|
| 10057 |
*p++ = '\r'; |
| 10058 |
bytesToRead--; |
| 10059 |
bufPtr->nextRemoved++; |
| 10060 |
} else if (GotFlag(statePtr, CHANNEL_BLOCKED)) { |
| 10061 |
/* |
| 10062 |
* ...and we cannot get more now. |
| 10063 |
*/ |
| 10064 |
|
| 10065 |
SetFlag(statePtr, CHANNEL_NEED_MORE_DATA); |
| 10066 |
break; |
| 10067 |
} else { |
| 10068 |
/* |
| 10069 |
* ...so we need to get some. |
| 10070 |
*/ |
| 10071 |
|
| 10072 |
goto moreData; |
| 10073 |
} |
| 10074 |
} |
| 10075 |
|
| 10076 |
if (bufPtr->nextPtr) { |
| 10077 |
/* |
| 10078 |
* There's a next buffer. Shift orphan \r to it. |
| 10079 |
*/ |
| 10080 |
|
| 10081 |
ChannelBuffer *nextPtr = bufPtr->nextPtr; |
| 10082 |
|
| 10083 |
nextPtr->nextRemoved -= 1; |
| 10084 |
RemovePoint(nextPtr)[0] = '\r'; |
| 10085 |
bufPtr->nextRemoved++; |
| 10086 |
} |
| 10087 |
} |
| 10088 |
|
| 10089 |
if (IsBufferEmpty(bufPtr)) { |
| 10090 |
statePtr->inQueueHead = bufPtr->nextPtr; |
| 10091 |
if (statePtr->inQueueHead == NULL) { |
| 10092 |
statePtr->inQueueTail = NULL; |
| 10093 |
} |
| 10094 |
RecycleBuffer(statePtr, bufPtr, 0); |
| 10095 |
bufPtr = statePtr->inQueueHead; |
| 10096 |
} |
| 10097 |
|
| 10098 |
if ((GotFlag(statePtr, CHANNEL_NONBLOCKING) || allowShortReads) |
| 10099 |
&& GotFlag(statePtr, CHANNEL_BLOCKED)) { |
| 10100 |
break; |
| 10101 |
} |
| 10102 |
|
| 10103 |
/* |
| 10104 |
* When there's no buffered data to read, and we're at EOF, escape to |
| 10105 |
* the caller. |
| 10106 |
*/ |
| 10107 |
|
| 10108 |
if (GotFlag(statePtr, CHANNEL_EOF) |
| 10109 |
&& (bufPtr == NULL || IsBufferEmpty(bufPtr))) { |
| 10110 |
break; |
| 10111 |
} |
| 10112 |
} |
| 10113 |
if (bytesToRead == 0) { |
| 10114 |
ResetFlag(statePtr, CHANNEL_BLOCKED); |
| 10115 |
} |
| 10116 |
|
| 10117 |
assert(!GotFlag(statePtr, CHANNEL_EOF) |
| 10118 |
|| GotFlag(statePtr, CHANNEL_STICKY_EOF) |
| 10119 |
|| Tcl_InputBuffered((Tcl_Channel)chanPtr) == 0); |
| 10120 |
assert(!(GotFlag(statePtr, CHANNEL_EOF|CHANNEL_BLOCKED) |
| 10121 |
== (CHANNEL_EOF|CHANNEL_BLOCKED))); |
| 10122 |
UpdateInterest(chanPtr); |
| 10123 |
TclChannelRelease((Tcl_Channel)chanPtr); |
| 10124 |
return (int)(p - dst); |
| 10125 |
} |
| 10126 |
|
| 10127 |
/* |
| 10128 |
*---------------------------------------------------------------------- |
| 10129 |
* |
| 10130 |
* CopyEventProc -- |
| 10131 |
* |
| 10132 |
* This routine is invoked as a channel event handler for the background |
| 10133 |
* copy operation. It is just a trivial wrapper around the CopyData |
| 10134 |
* routine. |
| 10135 |
* |
| 10136 |
* Results: |
| 10137 |
* None. |
| 10138 |
* |
| 10139 |
* Side effects: |
| 10140 |
* None. |
| 10141 |
* |
| 10142 |
*---------------------------------------------------------------------- |
| 10143 |
*/ |
| 10144 |
|
| 10145 |
static void |
| 10146 |
CopyEventProc( |
| 10147 |
void *clientData, |
| 10148 |
int mask) |
| 10149 |
{ |
| 10150 |
(void) CopyData((CopyState *)clientData, mask); |
| 10151 |
} |
| 10152 |
|
| 10153 |
/* |
| 10154 |
*---------------------------------------------------------------------- |
| 10155 |
* |
| 10156 |
* StopCopy -- |
| 10157 |
* |
| 10158 |
* This routine halts a copy that is in progress. |
| 10159 |
* |
| 10160 |
* Results: |
| 10161 |
* None. |
| 10162 |
* |
| 10163 |
* Side effects: |
| 10164 |
* Removes any pending channel handlers and restores the blocking and |
| 10165 |
* buffering modes of the channels. The CopyState is freed. |
| 10166 |
* |
| 10167 |
*---------------------------------------------------------------------- |
| 10168 |
*/ |
| 10169 |
|
| 10170 |
static void |
| 10171 |
StopCopy( |
| 10172 |
CopyState *csPtr) /* State for bg copy to stop . */ |
| 10173 |
{ |
| 10174 |
ChannelState *inStatePtr, *outStatePtr; |
| 10175 |
Tcl_Channel inChan, outChan; |
| 10176 |
|
| 10177 |
int nonBlocking; |
| 10178 |
|
| 10179 |
if (!csPtr) { |
| 10180 |
return; |
| 10181 |
} |
| 10182 |
|
| 10183 |
inChan = (Tcl_Channel) csPtr->readPtr; |
| 10184 |
outChan = (Tcl_Channel) csPtr->writePtr; |
| 10185 |
inStatePtr = csPtr->readPtr->state; |
| 10186 |
outStatePtr = csPtr->writePtr->state; |
| 10187 |
|
| 10188 |
/* |
| 10189 |
* Restore the old blocking mode and output buffering mode. |
| 10190 |
*/ |
| 10191 |
|
| 10192 |
nonBlocking = csPtr->readFlags & CHANNEL_NONBLOCKING; |
| 10193 |
if (nonBlocking != GotFlag(inStatePtr, CHANNEL_NONBLOCKING)) { |
| 10194 |
SetBlockMode(NULL, csPtr->readPtr, |
| 10195 |
nonBlocking ? TCL_MODE_NONBLOCKING : TCL_MODE_BLOCKING); |
| 10196 |
} |
| 10197 |
if (csPtr->readPtr != csPtr->writePtr) { |
| 10198 |
nonBlocking = csPtr->writeFlags & CHANNEL_NONBLOCKING; |
| 10199 |
if (nonBlocking != GotFlag(outStatePtr, CHANNEL_NONBLOCKING)) { |
| 10200 |
SetBlockMode(NULL, csPtr->writePtr, |
| 10201 |
nonBlocking ? TCL_MODE_NONBLOCKING : TCL_MODE_BLOCKING); |
| 10202 |
} |
| 10203 |
} |
| 10204 |
ResetFlag(outStatePtr, CHANNEL_LINEBUFFERED | CHANNEL_UNBUFFERED); |
| 10205 |
SetFlag(outStatePtr, |
| 10206 |
csPtr->writeFlags & (CHANNEL_LINEBUFFERED | CHANNEL_UNBUFFERED)); |
| 10207 |
|
| 10208 |
if (csPtr->cmdPtr) { |
| 10209 |
Tcl_DeleteChannelHandler(inChan, CopyEventProc, csPtr); |
| 10210 |
if (inChan != outChan) { |
| 10211 |
Tcl_DeleteChannelHandler(outChan, CopyEventProc, csPtr); |
| 10212 |
} |
| 10213 |
Tcl_DeleteChannelHandler(inChan, MBEvent, csPtr); |
| 10214 |
Tcl_DeleteChannelHandler(outChan, MBEvent, csPtr); |
| 10215 |
TclDecrRefCount(csPtr->cmdPtr); |
| 10216 |
csPtr->cmdPtr = NULL; |
| 10217 |
} |
| 10218 |
|
| 10219 |
if (inStatePtr->csPtrR) { |
| 10220 |
assert(inStatePtr->csPtrR == csPtr); |
| 10221 |
inStatePtr->csPtrR = NULL; |
| 10222 |
CopyDecrRefCount(csPtr); |
| 10223 |
} |
| 10224 |
if (outStatePtr->csPtrW) { |
| 10225 |
assert(outStatePtr->csPtrW == csPtr); |
| 10226 |
outStatePtr->csPtrW = NULL; |
| 10227 |
CopyDecrRefCount(csPtr); |
| 10228 |
} |
| 10229 |
} |
| 10230 |
|
| 10231 |
static void |
| 10232 |
CopyDecrRefCount( |
| 10233 |
CopyState *csPtr) |
| 10234 |
{ |
| 10235 |
if (csPtr->refCount-- > 1) { |
| 10236 |
return; |
| 10237 |
} |
| 10238 |
|
| 10239 |
TclChannelRelease((Tcl_Channel)csPtr->readPtr); |
| 10240 |
TclChannelRelease((Tcl_Channel)csPtr->writePtr); |
| 10241 |
|
| 10242 |
ckfree(csPtr); |
| 10243 |
} |
| 10244 |
|
| 10245 |
/* |
| 10246 |
*---------------------------------------------------------------------- |
| 10247 |
* |
| 10248 |
* StackSetBlockMode -- |
| 10249 |
* |
| 10250 |
* This function sets the blocking mode for a channel, iterating through |
| 10251 |
* each channel in a stack and updates the state flags. |
| 10252 |
* |
| 10253 |
* Results: |
| 10254 |
* 0 if OK, result code from failed blockModeProc otherwise. |
| 10255 |
* |
| 10256 |
* Side effects: |
| 10257 |
* Modifies the blocking mode of the channel and possibly generates an |
| 10258 |
* error. |
| 10259 |
* |
| 10260 |
*---------------------------------------------------------------------- |
| 10261 |
*/ |
| 10262 |
|
| 10263 |
static int |
| 10264 |
StackSetBlockMode( |
| 10265 |
Channel *chanPtr, /* Channel to modify. */ |
| 10266 |
int mode) /* One of TCL_MODE_BLOCKING or |
| 10267 |
* TCL_MODE_NONBLOCKING. */ |
| 10268 |
{ |
| 10269 |
int result = 0; |
| 10270 |
Tcl_DriverBlockModeProc *blockModeProc; |
| 10271 |
ChannelState *statePtr = chanPtr->state; |
| 10272 |
|
| 10273 |
/* |
| 10274 |
* Start at the top of the channel stack |
| 10275 |
* TODO: Examine what can go wrong when blockModeProc calls |
| 10276 |
* disturb the stacking state of the channel. |
| 10277 |
*/ |
| 10278 |
|
| 10279 |
chanPtr = statePtr->topChanPtr; |
| 10280 |
while (chanPtr != NULL) { |
| 10281 |
blockModeProc = Tcl_ChannelBlockModeProc(chanPtr->typePtr); |
| 10282 |
if (blockModeProc != NULL) { |
| 10283 |
result = blockModeProc(chanPtr->instanceData, mode); |
| 10284 |
if (result != 0) { |
| 10285 |
Tcl_SetErrno(result); |
| 10286 |
return result; |
| 10287 |
} |
| 10288 |
} |
| 10289 |
chanPtr = chanPtr->downChanPtr; |
| 10290 |
} |
| 10291 |
return 0; |
| 10292 |
} |
| 10293 |
|
| 10294 |
/* |
| 10295 |
*---------------------------------------------------------------------- |
| 10296 |
* |
| 10297 |
* SetBlockMode -- |
| 10298 |
* |
| 10299 |
* This function sets the blocking mode for a channel and updates the |
| 10300 |
* state flags. |
| 10301 |
* |
| 10302 |
* Results: |
| 10303 |
* A standard Tcl result. |
| 10304 |
* |
| 10305 |
* Side effects: |
| 10306 |
* Modifies the blocking mode of the channel and possibly generates an |
| 10307 |
* error. |
| 10308 |
* |
| 10309 |
*---------------------------------------------------------------------- |
| 10310 |
*/ |
| 10311 |
|
| 10312 |
static int |
| 10313 |
SetBlockMode( |
| 10314 |
Tcl_Interp *interp, /* Interp for error reporting. */ |
| 10315 |
Channel *chanPtr, /* Channel to modify. */ |
| 10316 |
int mode) /* One of TCL_MODE_BLOCKING or |
| 10317 |
* TCL_MODE_NONBLOCKING. */ |
| 10318 |
{ |
| 10319 |
int result = 0; |
| 10320 |
ChannelState *statePtr = chanPtr->state; |
| 10321 |
/* State info for channel */ |
| 10322 |
|
| 10323 |
result = StackSetBlockMode(chanPtr, mode); |
| 10324 |
if (result != 0) { |
| 10325 |
if (interp != NULL) { |
| 10326 |
/* |
| 10327 |
* TIP #219. |
| 10328 |
* Move error messages put by the driver into the bypass area and |
| 10329 |
* put them into the regular interpreter result. Fall back to the |
| 10330 |
* regular message if nothing was found in the bypass. |
| 10331 |
* |
| 10332 |
* Note that we cannot have a message in the interpreter bypass |
| 10333 |
* area, StackSetBlockMode is restricted to the channel bypass. |
| 10334 |
* We still need the interp as the destination of the move. |
| 10335 |
*/ |
| 10336 |
|
| 10337 |
if (!TclChanCaughtErrorBypass(interp, (Tcl_Channel) chanPtr)) { |
| 10338 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 10339 |
"error setting blocking mode: %s", |
| 10340 |
Tcl_PosixError(interp))); |
| 10341 |
} |
| 10342 |
} else { |
| 10343 |
/* |
| 10344 |
* TIP #219. |
| 10345 |
* If we have no interpreter to put a bypass message into we have |
| 10346 |
* to clear it, to prevent its propagation and use in other places |
| 10347 |
* unrelated to the actual occurence of the problem. |
| 10348 |
*/ |
| 10349 |
|
| 10350 |
Tcl_SetChannelError((Tcl_Channel) chanPtr, NULL); |
| 10351 |
} |
| 10352 |
return TCL_ERROR; |
| 10353 |
} |
| 10354 |
if (mode == TCL_MODE_BLOCKING) { |
| 10355 |
ResetFlag(statePtr, CHANNEL_NONBLOCKING | BG_FLUSH_SCHEDULED); |
| 10356 |
} else { |
| 10357 |
SetFlag(statePtr, CHANNEL_NONBLOCKING); |
| 10358 |
} |
| 10359 |
return TCL_OK; |
| 10360 |
} |
| 10361 |
|
| 10362 |
/* |
| 10363 |
*---------------------------------------------------------------------- |
| 10364 |
* |
| 10365 |
* Tcl_GetChannelNames -- |
| 10366 |
* |
| 10367 |
* Return the names of all open channels in the interp. |
| 10368 |
* |
| 10369 |
* Results: |
| 10370 |
* TCL_OK or TCL_ERROR. |
| 10371 |
* |
| 10372 |
* Side effects: |
| 10373 |
* Interp result modified with list of channel names. |
| 10374 |
* |
| 10375 |
*---------------------------------------------------------------------- |
| 10376 |
*/ |
| 10377 |
|
| 10378 |
int |
| 10379 |
Tcl_GetChannelNames( |
| 10380 |
Tcl_Interp *interp) /* Interp for error reporting. */ |
| 10381 |
{ |
| 10382 |
return Tcl_GetChannelNamesEx(interp, NULL); |
| 10383 |
} |
| 10384 |
|
| 10385 |
/* |
| 10386 |
*---------------------------------------------------------------------- |
| 10387 |
* |
| 10388 |
* Tcl_GetChannelNamesEx -- |
| 10389 |
* |
| 10390 |
* Return the names of open channels in the interp filtered filtered |
| 10391 |
* through a pattern. If pattern is NULL, it returns all the open |
| 10392 |
* channels. |
| 10393 |
* |
| 10394 |
* Results: |
| 10395 |
* TCL_OK or TCL_ERROR. |
| 10396 |
* |
| 10397 |
* Side effects: |
| 10398 |
* Interp result modified with list of channel names. |
| 10399 |
* |
| 10400 |
*---------------------------------------------------------------------- |
| 10401 |
*/ |
| 10402 |
|
| 10403 |
int |
| 10404 |
Tcl_GetChannelNamesEx( |
| 10405 |
Tcl_Interp *interp, /* Interp for error reporting. */ |
| 10406 |
const char *pattern) /* Pattern to filter on. */ |
| 10407 |
{ |
| 10408 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 10409 |
ChannelState *statePtr; |
| 10410 |
const char *name; /* Name for channel */ |
| 10411 |
Tcl_Obj *resultPtr; /* Pointer to result object */ |
| 10412 |
Tcl_HashTable *hTblPtr; /* Hash table of channels. */ |
| 10413 |
Tcl_HashEntry *hPtr; /* Search variable. */ |
| 10414 |
Tcl_HashSearch hSearch; /* Search variable. */ |
| 10415 |
|
| 10416 |
if (interp == NULL) { |
| 10417 |
return TCL_OK; |
| 10418 |
} |
| 10419 |
|
| 10420 |
/* |
| 10421 |
* Get the channel table that stores the channels registered for this |
| 10422 |
* interpreter. |
| 10423 |
*/ |
| 10424 |
|
| 10425 |
hTblPtr = GetChannelTable(interp); |
| 10426 |
TclNewObj(resultPtr); |
| 10427 |
if ((pattern != NULL) && TclMatchIsTrivial(pattern) |
| 10428 |
&& !((pattern[0] == 's') && (pattern[1] == 't') |
| 10429 |
&& (pattern[2] == 'd'))) { |
| 10430 |
if ((Tcl_FindHashEntry(hTblPtr, pattern) != NULL) |
| 10431 |
&& (Tcl_ListObjAppendElement(interp, resultPtr, |
| 10432 |
Tcl_NewStringObj(pattern, -1)) != TCL_OK)) { |
| 10433 |
goto error; |
| 10434 |
} |
| 10435 |
goto done; |
| 10436 |
} |
| 10437 |
|
| 10438 |
for (hPtr = Tcl_FirstHashEntry(hTblPtr, &hSearch); hPtr != NULL; |
| 10439 |
hPtr = Tcl_NextHashEntry(&hSearch)) { |
| 10440 |
statePtr = ((Channel *) Tcl_GetHashValue(hPtr))->state; |
| 10441 |
|
| 10442 |
if (statePtr->topChanPtr == (Channel *) tsdPtr->stdinChannel) { |
| 10443 |
name = "stdin"; |
| 10444 |
} else if (statePtr->topChanPtr == (Channel *) tsdPtr->stdoutChannel) { |
| 10445 |
name = "stdout"; |
| 10446 |
} else if (statePtr->topChanPtr == (Channel *) tsdPtr->stderrChannel) { |
| 10447 |
name = "stderr"; |
| 10448 |
} else { |
| 10449 |
/* |
| 10450 |
* This is also stored in Tcl_GetHashKey(hTblPtr, hPtr), but it's |
| 10451 |
* simpler to just grab the name from the statePtr. |
| 10452 |
*/ |
| 10453 |
|
| 10454 |
name = statePtr->channelName; |
| 10455 |
} |
| 10456 |
|
| 10457 |
if (((pattern == NULL) || Tcl_StringMatch(name, pattern)) && |
| 10458 |
(Tcl_ListObjAppendElement(interp, resultPtr, |
| 10459 |
Tcl_NewStringObj(name, -1)) != TCL_OK)) { |
| 10460 |
error: |
| 10461 |
TclDecrRefCount(resultPtr); |
| 10462 |
return TCL_ERROR; |
| 10463 |
} |
| 10464 |
} |
| 10465 |
|
| 10466 |
done: |
| 10467 |
Tcl_SetObjResult(interp, resultPtr); |
| 10468 |
return TCL_OK; |
| 10469 |
} |
| 10470 |
|
| 10471 |
/* |
| 10472 |
*---------------------------------------------------------------------- |
| 10473 |
* |
| 10474 |
* Tcl_IsChannelRegistered -- |
| 10475 |
* |
| 10476 |
* Checks whether the channel is associated with the interp. See also |
| 10477 |
* Tcl_RegisterChannel and Tcl_UnregisterChannel. |
| 10478 |
* |
| 10479 |
* Results: |
| 10480 |
* 0 if the channel is not registered in the interpreter, 1 else. |
| 10481 |
* |
| 10482 |
* Side effects: |
| 10483 |
* None. |
| 10484 |
* |
| 10485 |
*---------------------------------------------------------------------- |
| 10486 |
*/ |
| 10487 |
|
| 10488 |
int |
| 10489 |
Tcl_IsChannelRegistered( |
| 10490 |
Tcl_Interp *interp, /* The interp to query of the channel */ |
| 10491 |
Tcl_Channel chan) /* The channel to check */ |
| 10492 |
{ |
| 10493 |
Tcl_HashTable *hTblPtr; /* Hash table of channels. */ |
| 10494 |
Tcl_HashEntry *hPtr; /* Search variable. */ |
| 10495 |
Channel *chanPtr; /* The real IO channel. */ |
| 10496 |
ChannelState *statePtr; /* State of the real channel. */ |
| 10497 |
|
| 10498 |
/* |
| 10499 |
* Always check bottom-most channel in the stack. This is the one that |
| 10500 |
* gets registered. |
| 10501 |
*/ |
| 10502 |
|
| 10503 |
chanPtr = ((Channel *) chan)->state->bottomChanPtr; |
| 10504 |
statePtr = chanPtr->state; |
| 10505 |
|
| 10506 |
hTblPtr = (Tcl_HashTable *)Tcl_GetAssocData(interp, "tclIO", NULL); |
| 10507 |
if (hTblPtr == NULL) { |
| 10508 |
return 0; |
| 10509 |
} |
| 10510 |
hPtr = Tcl_FindHashEntry(hTblPtr, statePtr->channelName); |
| 10511 |
if (hPtr == NULL) { |
| 10512 |
return 0; |
| 10513 |
} |
| 10514 |
if ((Channel *) Tcl_GetHashValue(hPtr) != chanPtr) { |
| 10515 |
return 0; |
| 10516 |
} |
| 10517 |
|
| 10518 |
return 1; |
| 10519 |
} |
| 10520 |
|
| 10521 |
/* |
| 10522 |
*---------------------------------------------------------------------- |
| 10523 |
* |
| 10524 |
* Tcl_IsChannelShared -- |
| 10525 |
* |
| 10526 |
* Checks whether the channel is shared by multiple interpreters. |
| 10527 |
* |
| 10528 |
* Results: |
| 10529 |
* A boolean value (0 = Not shared, 1 = Shared). |
| 10530 |
* |
| 10531 |
* Side effects: |
| 10532 |
* None. |
| 10533 |
* |
| 10534 |
*---------------------------------------------------------------------- |
| 10535 |
*/ |
| 10536 |
|
| 10537 |
int |
| 10538 |
Tcl_IsChannelShared( |
| 10539 |
Tcl_Channel chan) /* The channel to query */ |
| 10540 |
{ |
| 10541 |
ChannelState *statePtr = ((Channel *) chan)->state; |
| 10542 |
/* State of real channel structure. */ |
| 10543 |
|
| 10544 |
return ((statePtr->refCount > 1) ? 1 : 0); |
| 10545 |
} |
| 10546 |
|
| 10547 |
/* |
| 10548 |
*---------------------------------------------------------------------- |
| 10549 |
* |
| 10550 |
* Tcl_IsChannelExisting -- |
| 10551 |
* |
| 10552 |
* Checks whether a channel of the given name exists in the |
| 10553 |
* (thread)-global list of all channels. See Tcl_GetChannelNamesEx for |
| 10554 |
* function exposed at the Tcl level. |
| 10555 |
* |
| 10556 |
* Results: |
| 10557 |
* A boolean value (0 = Does not exist, 1 = Does exist). |
| 10558 |
* |
| 10559 |
* Side effects: |
| 10560 |
* None. |
| 10561 |
* |
| 10562 |
*---------------------------------------------------------------------- |
| 10563 |
*/ |
| 10564 |
|
| 10565 |
int |
| 10566 |
Tcl_IsChannelExisting( |
| 10567 |
const char *chanName) /* The name of the channel to look for. */ |
| 10568 |
{ |
| 10569 |
ChannelState *statePtr; |
| 10570 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 10571 |
const char *name; |
| 10572 |
int chanNameLen; |
| 10573 |
|
| 10574 |
chanNameLen = strlen(chanName); |
| 10575 |
for (statePtr = tsdPtr->firstCSPtr; statePtr != NULL; |
| 10576 |
statePtr = statePtr->nextCSPtr) { |
| 10577 |
if (statePtr->topChanPtr == (Channel *) tsdPtr->stdinChannel) { |
| 10578 |
name = "stdin"; |
| 10579 |
} else if (statePtr->topChanPtr == (Channel *) tsdPtr->stdoutChannel) { |
| 10580 |
name = "stdout"; |
| 10581 |
} else if (statePtr->topChanPtr == (Channel *) tsdPtr->stderrChannel) { |
| 10582 |
name = "stderr"; |
| 10583 |
} else { |
| 10584 |
name = statePtr->channelName; |
| 10585 |
} |
| 10586 |
|
| 10587 |
if ((*chanName == *name) && |
| 10588 |
(memcmp(name, chanName, chanNameLen + 1) == 0)) { |
| 10589 |
return 1; |
| 10590 |
} |
| 10591 |
} |
| 10592 |
|
| 10593 |
return 0; |
| 10594 |
} |
| 10595 |
|
| 10596 |
/* |
| 10597 |
*---------------------------------------------------------------------- |
| 10598 |
* |
| 10599 |
* Tcl_ChannelName -- |
| 10600 |
* |
| 10601 |
* Return the name of the channel type. |
| 10602 |
* |
| 10603 |
* Results: |
| 10604 |
* A pointer the name of the channel type. |
| 10605 |
* |
| 10606 |
* Side effects: |
| 10607 |
* None. |
| 10608 |
* |
| 10609 |
*---------------------------------------------------------------------- |
| 10610 |
*/ |
| 10611 |
|
| 10612 |
const char * |
| 10613 |
Tcl_ChannelName( |
| 10614 |
const Tcl_ChannelType *chanTypePtr) /* Pointer to channel type. */ |
| 10615 |
{ |
| 10616 |
return chanTypePtr->typeName; |
| 10617 |
} |
| 10618 |
|
| 10619 |
/* |
| 10620 |
*---------------------------------------------------------------------- |
| 10621 |
* |
| 10622 |
* Tcl_ChannelVersion -- |
| 10623 |
* |
| 10624 |
* Return the of version of the channel type. |
| 10625 |
* |
| 10626 |
* Results: |
| 10627 |
* One of the TCL_CHANNEL_VERSION_* constants from tcl.h |
| 10628 |
* |
| 10629 |
* Side effects: |
| 10630 |
* None. |
| 10631 |
* |
| 10632 |
*---------------------------------------------------------------------- |
| 10633 |
*/ |
| 10634 |
|
| 10635 |
Tcl_ChannelTypeVersion |
| 10636 |
Tcl_ChannelVersion( |
| 10637 |
const Tcl_ChannelType *chanTypePtr) |
| 10638 |
/* Pointer to channel type. */ |
| 10639 |
{ |
| 10640 |
if ((chanTypePtr->version < TCL_CHANNEL_VERSION_2) |
| 10641 |
|| (chanTypePtr->version > TCL_CHANNEL_VERSION_5)) { |
| 10642 |
/* |
| 10643 |
* In <v2 channel versions, the version field is occupied by the |
| 10644 |
* Tcl_DriverBlockModeProc |
| 10645 |
*/ |
| 10646 |
return TCL_CHANNEL_VERSION_1; |
| 10647 |
} |
| 10648 |
return chanTypePtr->version; |
| 10649 |
} |
| 10650 |
|
| 10651 |
/* |
| 10652 |
*---------------------------------------------------------------------- |
| 10653 |
* |
| 10654 |
* Tcl_ChannelBlockModeProc -- |
| 10655 |
* |
| 10656 |
* Return the Tcl_DriverBlockModeProc of the channel type. |
| 10657 |
* |
| 10658 |
* Results: |
| 10659 |
* A pointer to the proc. |
| 10660 |
* |
| 10661 |
* Side effects: |
| 10662 |
* None. |
| 10663 |
* |
| 10664 |
*---------------------------------------------------------------------- */ |
| 10665 |
|
| 10666 |
Tcl_DriverBlockModeProc * |
| 10667 |
Tcl_ChannelBlockModeProc( |
| 10668 |
const Tcl_ChannelType *chanTypePtr) |
| 10669 |
/* Pointer to channel type. */ |
| 10670 |
{ |
| 10671 |
if (Tcl_ChannelVersion(chanTypePtr) < TCL_CHANNEL_VERSION_2) { |
| 10672 |
/* |
| 10673 |
* The v1 structure had the blockModeProc in a different place. |
| 10674 |
*/ |
| 10675 |
return (Tcl_DriverBlockModeProc *) chanTypePtr->version; |
| 10676 |
} |
| 10677 |
|
| 10678 |
return chanTypePtr->blockModeProc; |
| 10679 |
} |
| 10680 |
|
| 10681 |
/* |
| 10682 |
*---------------------------------------------------------------------- |
| 10683 |
* |
| 10684 |
* Tcl_ChannelCloseProc -- |
| 10685 |
* |
| 10686 |
* Return the Tcl_DriverCloseProc of the channel type. |
| 10687 |
* |
| 10688 |
* Results: |
| 10689 |
* A pointer to the proc. |
| 10690 |
* |
| 10691 |
* Side effects: |
| 10692 |
* None. |
| 10693 |
* |
| 10694 |
*---------------------------------------------------------------------- |
| 10695 |
*/ |
| 10696 |
|
| 10697 |
Tcl_DriverCloseProc * |
| 10698 |
Tcl_ChannelCloseProc( |
| 10699 |
const Tcl_ChannelType *chanTypePtr) |
| 10700 |
/* Pointer to channel type. */ |
| 10701 |
{ |
| 10702 |
return chanTypePtr->closeProc; |
| 10703 |
} |
| 10704 |
|
| 10705 |
/* |
| 10706 |
*---------------------------------------------------------------------- |
| 10707 |
* |
| 10708 |
* Tcl_ChannelClose2Proc -- |
| 10709 |
* |
| 10710 |
* Return the Tcl_DriverClose2Proc of the channel type. |
| 10711 |
* |
| 10712 |
* Results: |
| 10713 |
* A pointer to the proc. |
| 10714 |
* |
| 10715 |
* Side effects: |
| 10716 |
* None. |
| 10717 |
* |
| 10718 |
*---------------------------------------------------------------------- |
| 10719 |
*/ |
| 10720 |
|
| 10721 |
Tcl_DriverClose2Proc * |
| 10722 |
Tcl_ChannelClose2Proc( |
| 10723 |
const Tcl_ChannelType *chanTypePtr) |
| 10724 |
/* Pointer to channel type. */ |
| 10725 |
{ |
| 10726 |
return chanTypePtr->close2Proc; |
| 10727 |
} |
| 10728 |
|
| 10729 |
/* |
| 10730 |
*---------------------------------------------------------------------- |
| 10731 |
* |
| 10732 |
* Tcl_ChannelInputProc -- |
| 10733 |
* |
| 10734 |
* Return the Tcl_DriverInputProc of the channel type. |
| 10735 |
* |
| 10736 |
* Results: |
| 10737 |
* A pointer to the proc. |
| 10738 |
* |
| 10739 |
* Side effects: |
| 10740 |
* None. |
| 10741 |
* |
| 10742 |
*---------------------------------------------------------------------- |
| 10743 |
*/ |
| 10744 |
|
| 10745 |
Tcl_DriverInputProc * |
| 10746 |
Tcl_ChannelInputProc( |
| 10747 |
const Tcl_ChannelType *chanTypePtr) |
| 10748 |
/* Pointer to channel type. */ |
| 10749 |
{ |
| 10750 |
return chanTypePtr->inputProc; |
| 10751 |
} |
| 10752 |
|
| 10753 |
/* |
| 10754 |
*---------------------------------------------------------------------- |
| 10755 |
* |
| 10756 |
* Tcl_ChannelOutputProc -- |
| 10757 |
* |
| 10758 |
* Return the Tcl_DriverOutputProc of the channel type. |
| 10759 |
* |
| 10760 |
* Results: |
| 10761 |
* A pointer to the proc. |
| 10762 |
* |
| 10763 |
* Side effects: |
| 10764 |
* None. |
| 10765 |
* |
| 10766 |
*---------------------------------------------------------------------- |
| 10767 |
*/ |
| 10768 |
|
| 10769 |
Tcl_DriverOutputProc * |
| 10770 |
Tcl_ChannelOutputProc( |
| 10771 |
const Tcl_ChannelType *chanTypePtr) |
| 10772 |
/* Pointer to channel type. */ |
| 10773 |
{ |
| 10774 |
return chanTypePtr->outputProc; |
| 10775 |
} |
| 10776 |
|
| 10777 |
/* |
| 10778 |
*---------------------------------------------------------------------- |
| 10779 |
* |
| 10780 |
* Tcl_ChannelSeekProc -- |
| 10781 |
* |
| 10782 |
* Return the Tcl_DriverSeekProc of the channel type. |
| 10783 |
* |
| 10784 |
* Results: |
| 10785 |
* A pointer to the proc. |
| 10786 |
* |
| 10787 |
* Side effects: |
| 10788 |
* None. |
| 10789 |
* |
| 10790 |
*---------------------------------------------------------------------- |
| 10791 |
*/ |
| 10792 |
|
| 10793 |
Tcl_DriverSeekProc * |
| 10794 |
Tcl_ChannelSeekProc( |
| 10795 |
const Tcl_ChannelType *chanTypePtr) |
| 10796 |
/* Pointer to channel type. */ |
| 10797 |
{ |
| 10798 |
return chanTypePtr->seekProc; |
| 10799 |
} |
| 10800 |
|
| 10801 |
/* |
| 10802 |
*---------------------------------------------------------------------- |
| 10803 |
* |
| 10804 |
* Tcl_ChannelSetOptionProc -- |
| 10805 |
* |
| 10806 |
* Return the Tcl_DriverSetOptionProc of the channel type. |
| 10807 |
* |
| 10808 |
* Results: |
| 10809 |
* A pointer to the proc. |
| 10810 |
* |
| 10811 |
* Side effects: |
| 10812 |
* None. |
| 10813 |
* |
| 10814 |
*---------------------------------------------------------------------- |
| 10815 |
*/ |
| 10816 |
|
| 10817 |
Tcl_DriverSetOptionProc * |
| 10818 |
Tcl_ChannelSetOptionProc( |
| 10819 |
const Tcl_ChannelType *chanTypePtr) |
| 10820 |
/* Pointer to channel type. */ |
| 10821 |
{ |
| 10822 |
return chanTypePtr->setOptionProc; |
| 10823 |
} |
| 10824 |
|
| 10825 |
/* |
| 10826 |
*---------------------------------------------------------------------- |
| 10827 |
* |
| 10828 |
* Tcl_ChannelGetOptionProc -- |
| 10829 |
* |
| 10830 |
* Return the Tcl_DriverGetOptionProc of the channel type. |
| 10831 |
* |
| 10832 |
* Results: |
| 10833 |
* A pointer to the proc. |
| 10834 |
* |
| 10835 |
* Side effects: |
| 10836 |
* None. |
| 10837 |
* |
| 10838 |
*---------------------------------------------------------------------- |
| 10839 |
*/ |
| 10840 |
|
| 10841 |
Tcl_DriverGetOptionProc * |
| 10842 |
Tcl_ChannelGetOptionProc( |
| 10843 |
const Tcl_ChannelType *chanTypePtr) |
| 10844 |
/* Pointer to channel type. */ |
| 10845 |
{ |
| 10846 |
return chanTypePtr->getOptionProc; |
| 10847 |
} |
| 10848 |
|
| 10849 |
/* |
| 10850 |
*---------------------------------------------------------------------- |
| 10851 |
* |
| 10852 |
* Tcl_ChannelWatchProc -- |
| 10853 |
* |
| 10854 |
* Return the Tcl_DriverWatchProc of the channel type. |
| 10855 |
* |
| 10856 |
* Results: |
| 10857 |
* A pointer to the proc. |
| 10858 |
* |
| 10859 |
* Side effects: |
| 10860 |
* None. |
| 10861 |
* |
| 10862 |
*---------------------------------------------------------------------- |
| 10863 |
*/ |
| 10864 |
|
| 10865 |
Tcl_DriverWatchProc * |
| 10866 |
Tcl_ChannelWatchProc( |
| 10867 |
const Tcl_ChannelType *chanTypePtr) |
| 10868 |
/* Pointer to channel type. */ |
| 10869 |
{ |
| 10870 |
return chanTypePtr->watchProc; |
| 10871 |
} |
| 10872 |
|
| 10873 |
/* |
| 10874 |
*---------------------------------------------------------------------- |
| 10875 |
* |
| 10876 |
* Tcl_ChannelGetHandleProc -- |
| 10877 |
* |
| 10878 |
* Return the Tcl_DriverGetHandleProc of the channel type. |
| 10879 |
* |
| 10880 |
* Results: |
| 10881 |
* A pointer to the proc. |
| 10882 |
* |
| 10883 |
* Side effects: |
| 10884 |
* None. |
| 10885 |
* |
| 10886 |
*---------------------------------------------------------------------- |
| 10887 |
*/ |
| 10888 |
|
| 10889 |
Tcl_DriverGetHandleProc * |
| 10890 |
Tcl_ChannelGetHandleProc( |
| 10891 |
const Tcl_ChannelType *chanTypePtr) |
| 10892 |
/* Pointer to channel type. */ |
| 10893 |
{ |
| 10894 |
return chanTypePtr->getHandleProc; |
| 10895 |
} |
| 10896 |
|
| 10897 |
/* |
| 10898 |
*---------------------------------------------------------------------- |
| 10899 |
* |
| 10900 |
* Tcl_ChannelFlushProc -- |
| 10901 |
* |
| 10902 |
* Return the Tcl_DriverFlushProc of the channel type. |
| 10903 |
* |
| 10904 |
* Results: |
| 10905 |
* A pointer to the proc. |
| 10906 |
* |
| 10907 |
* Side effects: |
| 10908 |
* None. |
| 10909 |
* |
| 10910 |
*---------------------------------------------------------------------- |
| 10911 |
*/ |
| 10912 |
|
| 10913 |
Tcl_DriverFlushProc * |
| 10914 |
Tcl_ChannelFlushProc( |
| 10915 |
const Tcl_ChannelType *chanTypePtr) |
| 10916 |
/* Pointer to channel type. */ |
| 10917 |
{ |
| 10918 |
if (Tcl_ChannelVersion(chanTypePtr) < TCL_CHANNEL_VERSION_2) { |
| 10919 |
return NULL; |
| 10920 |
} |
| 10921 |
return chanTypePtr->flushProc; |
| 10922 |
} |
| 10923 |
|
| 10924 |
/* |
| 10925 |
*---------------------------------------------------------------------- |
| 10926 |
* |
| 10927 |
* Tcl_ChannelHandlerProc -- |
| 10928 |
* |
| 10929 |
* Return the Tcl_DriverHandlerProc of the channel type. |
| 10930 |
* |
| 10931 |
* Results: |
| 10932 |
* A pointer to the proc. |
| 10933 |
* |
| 10934 |
* Side effects: |
| 10935 |
* None. |
| 10936 |
* |
| 10937 |
*---------------------------------------------------------------------- |
| 10938 |
*/ |
| 10939 |
|
| 10940 |
Tcl_DriverHandlerProc * |
| 10941 |
Tcl_ChannelHandlerProc( |
| 10942 |
const Tcl_ChannelType *chanTypePtr) |
| 10943 |
/* Pointer to channel type. */ |
| 10944 |
{ |
| 10945 |
if (Tcl_ChannelVersion(chanTypePtr) < TCL_CHANNEL_VERSION_2) { |
| 10946 |
return NULL; |
| 10947 |
} |
| 10948 |
return chanTypePtr->handlerProc; |
| 10949 |
} |
| 10950 |
|
| 10951 |
/* |
| 10952 |
*---------------------------------------------------------------------- |
| 10953 |
* |
| 10954 |
* Tcl_ChannelWideSeekProc -- |
| 10955 |
* |
| 10956 |
* Return the Tcl_DriverWideSeekProc of the channel type. |
| 10957 |
* |
| 10958 |
* Results: |
| 10959 |
* A pointer to the proc. |
| 10960 |
* |
| 10961 |
* Side effects: |
| 10962 |
* None. |
| 10963 |
* |
| 10964 |
*---------------------------------------------------------------------- |
| 10965 |
*/ |
| 10966 |
|
| 10967 |
Tcl_DriverWideSeekProc * |
| 10968 |
Tcl_ChannelWideSeekProc( |
| 10969 |
const Tcl_ChannelType *chanTypePtr) |
| 10970 |
/* Pointer to channel type. */ |
| 10971 |
{ |
| 10972 |
if (Tcl_ChannelVersion(chanTypePtr) < TCL_CHANNEL_VERSION_3) { |
| 10973 |
return NULL; |
| 10974 |
} |
| 10975 |
return chanTypePtr->wideSeekProc; |
| 10976 |
} |
| 10977 |
|
| 10978 |
/* |
| 10979 |
*---------------------------------------------------------------------- |
| 10980 |
* |
| 10981 |
* Tcl_ChannelThreadActionProc -- |
| 10982 |
* |
| 10983 |
* TIP #218, Channel Thread Actions. Return the |
| 10984 |
* Tcl_DriverThreadActionProc of the channel type. |
| 10985 |
* |
| 10986 |
* Results: |
| 10987 |
* A pointer to the proc. |
| 10988 |
* |
| 10989 |
* Side effects: |
| 10990 |
* None. |
| 10991 |
* |
| 10992 |
*---------------------------------------------------------------------- |
| 10993 |
*/ |
| 10994 |
|
| 10995 |
Tcl_DriverThreadActionProc * |
| 10996 |
Tcl_ChannelThreadActionProc( |
| 10997 |
const Tcl_ChannelType *chanTypePtr) |
| 10998 |
/* Pointer to channel type. */ |
| 10999 |
{ |
| 11000 |
if (Tcl_ChannelVersion(chanTypePtr) < TCL_CHANNEL_VERSION_4) { |
| 11001 |
return NULL; |
| 11002 |
} |
| 11003 |
return chanTypePtr->threadActionProc; |
| 11004 |
} |
| 11005 |
|
| 11006 |
/* |
| 11007 |
*---------------------------------------------------------------------- |
| 11008 |
* |
| 11009 |
* Tcl_SetChannelErrorInterp -- |
| 11010 |
* |
| 11011 |
* TIP #219, Tcl Channel Reflection API. |
| 11012 |
* Store an error message for the I/O system. |
| 11013 |
* |
| 11014 |
* Results: |
| 11015 |
* None. |
| 11016 |
* |
| 11017 |
* Side effects: |
| 11018 |
* Discards a previously stored message. |
| 11019 |
* |
| 11020 |
*---------------------------------------------------------------------- |
| 11021 |
*/ |
| 11022 |
|
| 11023 |
void |
| 11024 |
Tcl_SetChannelErrorInterp( |
| 11025 |
Tcl_Interp *interp, /* Interp to store the data into. */ |
| 11026 |
Tcl_Obj *msg) /* Error message to store. */ |
| 11027 |
{ |
| 11028 |
Interp *iPtr = (Interp *) interp; |
| 11029 |
|
| 11030 |
if (iPtr->chanMsg != NULL) { |
| 11031 |
TclDecrRefCount(iPtr->chanMsg); |
| 11032 |
iPtr->chanMsg = NULL; |
| 11033 |
} |
| 11034 |
|
| 11035 |
if (msg != NULL) { |
| 11036 |
iPtr->chanMsg = FixLevelCode(msg); |
| 11037 |
Tcl_IncrRefCount(iPtr->chanMsg); |
| 11038 |
} |
| 11039 |
return; |
| 11040 |
} |
| 11041 |
|
| 11042 |
/* |
| 11043 |
*---------------------------------------------------------------------- |
| 11044 |
* |
| 11045 |
* Tcl_SetChannelError -- |
| 11046 |
* |
| 11047 |
* TIP #219, Tcl Channel Reflection API. |
| 11048 |
* Store an error message for the I/O system. |
| 11049 |
* |
| 11050 |
* Results: |
| 11051 |
* None. |
| 11052 |
* |
| 11053 |
* Side effects: |
| 11054 |
* Discards a previously stored message. |
| 11055 |
* |
| 11056 |
*---------------------------------------------------------------------- |
| 11057 |
*/ |
| 11058 |
|
| 11059 |
void |
| 11060 |
Tcl_SetChannelError( |
| 11061 |
Tcl_Channel chan, /* Channel to store the data into. */ |
| 11062 |
Tcl_Obj *msg) /* Error message to store. */ |
| 11063 |
{ |
| 11064 |
ChannelState *statePtr = ((Channel *)chan)->state; |
| 11065 |
|
| 11066 |
if (statePtr->chanMsg != NULL) { |
| 11067 |
TclDecrRefCount(statePtr->chanMsg); |
| 11068 |
statePtr->chanMsg = NULL; |
| 11069 |
} |
| 11070 |
|
| 11071 |
if (msg != NULL) { |
| 11072 |
statePtr->chanMsg = FixLevelCode(msg); |
| 11073 |
Tcl_IncrRefCount(statePtr->chanMsg); |
| 11074 |
} |
| 11075 |
return; |
| 11076 |
} |
| 11077 |
|
| 11078 |
/* |
| 11079 |
*---------------------------------------------------------------------- |
| 11080 |
* |
| 11081 |
* FixLevelCode -- |
| 11082 |
* |
| 11083 |
* TIP #219, Tcl Channel Reflection API. |
| 11084 |
* Scans an error message for bad -code / -level directives. Returns a |
| 11085 |
* modified copy with such directives corrected, and the input if it had |
| 11086 |
* no problems. |
| 11087 |
* |
| 11088 |
* Results: |
| 11089 |
* A Tcl_Obj* |
| 11090 |
* |
| 11091 |
* Side effects: |
| 11092 |
* None. |
| 11093 |
* |
| 11094 |
*---------------------------------------------------------------------- |
| 11095 |
*/ |
| 11096 |
|
| 11097 |
static Tcl_Obj * |
| 11098 |
FixLevelCode( |
| 11099 |
Tcl_Obj *msg) |
| 11100 |
{ |
| 11101 |
int explicitResult, numOptions, lcn; |
| 11102 |
int lc; |
| 11103 |
Tcl_Obj **lv, **lvn; |
| 11104 |
int res, i, j, val, lignore, cignore; |
| 11105 |
int newlevel = -1, newcode = -1; |
| 11106 |
|
| 11107 |
/* ASSERT msg != NULL */ |
| 11108 |
|
| 11109 |
/* |
| 11110 |
* Process the caught message. |
| 11111 |
* |
| 11112 |
* Syntax = (option value)... ?message? |
| 11113 |
* |
| 11114 |
* Bad message syntax causes a panic, because the other side uses |
| 11115 |
* Tcl_GetReturnOptions and list construction functions to marshal the |
| 11116 |
* information. Hence an error means that we've got serious breakage. |
| 11117 |
*/ |
| 11118 |
|
| 11119 |
res = TclListObjGetElements(NULL, msg, &lc, &lv); |
| 11120 |
if (res != TCL_OK) { |
| 11121 |
Tcl_Panic("Tcl_SetChannelError: bad syntax of message"); |
| 11122 |
} |
| 11123 |
|
| 11124 |
explicitResult = (1 == (lc % 2)); |
| 11125 |
numOptions = lc - explicitResult; |
| 11126 |
|
| 11127 |
/* |
| 11128 |
* No options, nothing to do. |
| 11129 |
*/ |
| 11130 |
|
| 11131 |
if (numOptions == 0) { |
| 11132 |
return msg; |
| 11133 |
} |
| 11134 |
|
| 11135 |
/* |
| 11136 |
* Check for -code x, x != 1|error, and -level x, x != 0 |
| 11137 |
*/ |
| 11138 |
|
| 11139 |
for (i = 0; i < numOptions; i += 2) { |
| 11140 |
if (0 == strcmp(TclGetString(lv[i]), "-code")) { |
| 11141 |
/* |
| 11142 |
* !"error", !integer, integer != 1 (numeric code for error) |
| 11143 |
*/ |
| 11144 |
|
| 11145 |
res = TclGetIntFromObj(NULL, lv[i+1], &val); |
| 11146 |
if (((res == TCL_OK) && (val != 1)) || ((res != TCL_OK) && |
| 11147 |
(0 != strcmp(TclGetString(lv[i+1]), "error")))) { |
| 11148 |
newcode = 1; |
| 11149 |
} |
| 11150 |
} else if (0 == strcmp(TclGetString(lv[i]), "-level")) { |
| 11151 |
/* |
| 11152 |
* !integer, integer != 0 |
| 11153 |
*/ |
| 11154 |
|
| 11155 |
res = TclGetIntFromObj(NULL, lv [i+1], &val); |
| 11156 |
if ((res != TCL_OK) || (val != 0)) { |
| 11157 |
newlevel = 0; |
| 11158 |
} |
| 11159 |
} |
| 11160 |
} |
| 11161 |
|
| 11162 |
/* |
| 11163 |
* -code, -level are either not present or ok. Nothing to do. |
| 11164 |
*/ |
| 11165 |
|
| 11166 |
if ((newlevel < 0) && (newcode < 0)) { |
| 11167 |
return msg; |
| 11168 |
} |
| 11169 |
|
| 11170 |
lcn = numOptions; |
| 11171 |
if (explicitResult) { |
| 11172 |
lcn ++; |
| 11173 |
} |
| 11174 |
if (newlevel >= 0) { |
| 11175 |
lcn += 2; |
| 11176 |
} |
| 11177 |
if (newcode >= 0) { |
| 11178 |
lcn += 2; |
| 11179 |
} |
| 11180 |
|
| 11181 |
lvn = (Tcl_Obj **)ckalloc(lcn * sizeof(Tcl_Obj *)); |
| 11182 |
|
| 11183 |
/* |
| 11184 |
* New level/code information is spliced into the first occurrence of |
| 11185 |
* -level, -code, further occurrences are ignored. The options cannot be |
| 11186 |
* not present, we would not come here. Options which are ok are simply |
| 11187 |
* copied over. |
| 11188 |
*/ |
| 11189 |
|
| 11190 |
lignore = cignore = 0; |
| 11191 |
for (i=0, j=0; i<numOptions; i+=2) { |
| 11192 |
if (0 == strcmp(TclGetString(lv[i]), "-level")) { |
| 11193 |
if (newlevel >= 0) { |
| 11194 |
lvn[j++] = lv[i]; |
| 11195 |
lvn[j++] = Tcl_NewIntObj(newlevel); |
| 11196 |
newlevel = -1; |
| 11197 |
lignore = 1; |
| 11198 |
continue; |
| 11199 |
} else if (lignore) { |
| 11200 |
continue; |
| 11201 |
} |
| 11202 |
} else if (0 == strcmp(TclGetString(lv[i]), "-code")) { |
| 11203 |
if (newcode >= 0) { |
| 11204 |
lvn[j++] = lv[i]; |
| 11205 |
lvn[j++] = Tcl_NewIntObj(newcode); |
| 11206 |
newcode = -1; |
| 11207 |
cignore = 1; |
| 11208 |
continue; |
| 11209 |
} else if (cignore) { |
| 11210 |
continue; |
| 11211 |
} |
| 11212 |
} |
| 11213 |
|
| 11214 |
/* |
| 11215 |
* Keep everything else, possibly copied down. |
| 11216 |
*/ |
| 11217 |
|
| 11218 |
lvn[j++] = lv[i]; |
| 11219 |
lvn[j++] = lv[i+1]; |
| 11220 |
} |
| 11221 |
if (newlevel >= 0) { |
| 11222 |
Tcl_Panic("Defined newlevel not used in rewrite"); |
| 11223 |
} |
| 11224 |
if (newcode >= 0) { |
| 11225 |
Tcl_Panic("Defined newcode not used in rewrite"); |
| 11226 |
} |
| 11227 |
|
| 11228 |
if (explicitResult) { |
| 11229 |
lvn[j++] = lv[i]; |
| 11230 |
} |
| 11231 |
|
| 11232 |
msg = Tcl_NewListObj(j, lvn); |
| 11233 |
|
| 11234 |
ckfree(lvn); |
| 11235 |
return msg; |
| 11236 |
} |
| 11237 |
|
| 11238 |
/* |
| 11239 |
*---------------------------------------------------------------------- |
| 11240 |
* |
| 11241 |
* Tcl_GetChannelErrorInterp -- |
| 11242 |
* |
| 11243 |
* TIP #219, Tcl Channel Reflection API. |
| 11244 |
* Return the message stored by the channel driver. |
| 11245 |
* |
| 11246 |
* Results: |
| 11247 |
* Tcl error message object. |
| 11248 |
* |
| 11249 |
* Side effects: |
| 11250 |
* Resets the stored data to NULL. |
| 11251 |
* |
| 11252 |
*---------------------------------------------------------------------- |
| 11253 |
*/ |
| 11254 |
|
| 11255 |
void |
| 11256 |
Tcl_GetChannelErrorInterp( |
| 11257 |
Tcl_Interp *interp, /* Interp to query. */ |
| 11258 |
Tcl_Obj **msg) /* Place for error message. */ |
| 11259 |
{ |
| 11260 |
Interp *iPtr = (Interp *) interp; |
| 11261 |
|
| 11262 |
*msg = iPtr->chanMsg; |
| 11263 |
iPtr->chanMsg = NULL; |
| 11264 |
} |
| 11265 |
|
| 11266 |
/* |
| 11267 |
*---------------------------------------------------------------------- |
| 11268 |
* |
| 11269 |
* Tcl_GetChannelError -- |
| 11270 |
* |
| 11271 |
* TIP #219, Tcl Channel Reflection API. |
| 11272 |
* Return the message stored by the channel driver. |
| 11273 |
* |
| 11274 |
* Results: |
| 11275 |
* Tcl error message object. |
| 11276 |
* |
| 11277 |
* Side effects: |
| 11278 |
* Resets the stored data to NULL. |
| 11279 |
* |
| 11280 |
*---------------------------------------------------------------------- |
| 11281 |
*/ |
| 11282 |
|
| 11283 |
void |
| 11284 |
Tcl_GetChannelError( |
| 11285 |
Tcl_Channel chan, /* Channel to query. */ |
| 11286 |
Tcl_Obj **msg) /* Place for error message. */ |
| 11287 |
{ |
| 11288 |
ChannelState *statePtr = ((Channel *) chan)->state; |
| 11289 |
|
| 11290 |
*msg = statePtr->chanMsg; |
| 11291 |
statePtr->chanMsg = NULL; |
| 11292 |
} |
| 11293 |
|
| 11294 |
/* |
| 11295 |
*---------------------------------------------------------------------- |
| 11296 |
* |
| 11297 |
* Tcl_ChannelTruncateProc -- |
| 11298 |
* |
| 11299 |
* TIP #208 (subsection relating to truncation, based on TIP #206). |
| 11300 |
* Return the Tcl_DriverTruncateProc of the channel type. |
| 11301 |
* |
| 11302 |
* Results: |
| 11303 |
* A pointer to the proc. |
| 11304 |
* |
| 11305 |
* Side effects: |
| 11306 |
* None. |
| 11307 |
* |
| 11308 |
*---------------------------------------------------------------------- |
| 11309 |
*/ |
| 11310 |
|
| 11311 |
Tcl_DriverTruncateProc * |
| 11312 |
Tcl_ChannelTruncateProc( |
| 11313 |
const Tcl_ChannelType *chanTypePtr) |
| 11314 |
/* Pointer to channel type. */ |
| 11315 |
{ |
| 11316 |
if (Tcl_ChannelVersion(chanTypePtr) < TCL_CHANNEL_VERSION_5) { |
| 11317 |
return NULL; |
| 11318 |
} |
| 11319 |
return chanTypePtr->truncateProc; |
| 11320 |
} |
| 11321 |
|
| 11322 |
/* |
| 11323 |
*---------------------------------------------------------------------- |
| 11324 |
* |
| 11325 |
* DupChannelInternalRep -- |
| 11326 |
* |
| 11327 |
* Initialize the internal representation of a new Tcl_Obj to a copy of |
| 11328 |
* the internal representation of an existing string object. |
| 11329 |
* |
| 11330 |
* Results: |
| 11331 |
* None. |
| 11332 |
* |
| 11333 |
* Side effects: |
| 11334 |
* copyPtr's internal rep is set to a copy of srcPtr's internal |
| 11335 |
* representation. |
| 11336 |
* |
| 11337 |
*---------------------------------------------------------------------- |
| 11338 |
*/ |
| 11339 |
|
| 11340 |
static void |
| 11341 |
DupChannelInternalRep( |
| 11342 |
Tcl_Obj *srcPtr, /* Object with internal rep to copy. Must have |
| 11343 |
* an internal rep of type "Channel". */ |
| 11344 |
Tcl_Obj *copyPtr) /* Object with internal rep to set. Must not |
| 11345 |
* currently have an internal rep.*/ |
| 11346 |
{ |
| 11347 |
ResolvedChanName *resPtr = (ResolvedChanName *)srcPtr->internalRep.twoPtrValue.ptr1; |
| 11348 |
|
| 11349 |
resPtr->refCount++; |
| 11350 |
copyPtr->internalRep.twoPtrValue.ptr1 = resPtr; |
| 11351 |
copyPtr->typePtr = srcPtr->typePtr; |
| 11352 |
} |
| 11353 |
|
| 11354 |
/* |
| 11355 |
*---------------------------------------------------------------------- |
| 11356 |
* |
| 11357 |
* FreeChannelInternalRep -- |
| 11358 |
* |
| 11359 |
* Release statePtr storage. |
| 11360 |
* |
| 11361 |
* Results: |
| 11362 |
* None. |
| 11363 |
* |
| 11364 |
* Side effects: |
| 11365 |
* May cause state to be freed. |
| 11366 |
* |
| 11367 |
*---------------------------------------------------------------------- |
| 11368 |
*/ |
| 11369 |
|
| 11370 |
static void |
| 11371 |
FreeChannelInternalRep( |
| 11372 |
Tcl_Obj *objPtr) /* Object with internal rep to free. */ |
| 11373 |
{ |
| 11374 |
ResolvedChanName *resPtr = (ResolvedChanName *)objPtr->internalRep.twoPtrValue.ptr1; |
| 11375 |
|
| 11376 |
objPtr->typePtr = NULL; |
| 11377 |
if (resPtr->refCount-- > 1) { |
| 11378 |
return; |
| 11379 |
} |
| 11380 |
Tcl_Release(resPtr->statePtr); |
| 11381 |
ckfree(resPtr); |
| 11382 |
} |
| 11383 |
|
| 11384 |
#if 0 |
| 11385 |
/* |
| 11386 |
* For future debugging work, a simple function to print the flags of a |
| 11387 |
* channel in semi-readable form. |
| 11388 |
*/ |
| 11389 |
|
| 11390 |
static int |
| 11391 |
DumpFlags( |
| 11392 |
char *str, |
| 11393 |
int flags) |
| 11394 |
{ |
| 11395 |
int i = 0; |
| 11396 |
char buf[24]; |
| 11397 |
|
| 11398 |
#define ChanFlag(chr, bit) (buf[i++] = ((flags & (bit)) ? (chr) : '_')) |
| 11399 |
|
| 11400 |
ChanFlag('r', TCL_READABLE); |
| 11401 |
ChanFlag('w', TCL_WRITABLE); |
| 11402 |
ChanFlag('n', CHANNEL_NONBLOCKING); |
| 11403 |
ChanFlag('l', CHANNEL_LINEBUFFERED); |
| 11404 |
ChanFlag('u', CHANNEL_UNBUFFERED); |
| 11405 |
ChanFlag('F', BG_FLUSH_SCHEDULED); |
| 11406 |
ChanFlag('c', CHANNEL_CLOSED); |
| 11407 |
ChanFlag('E', CHANNEL_EOF); |
| 11408 |
ChanFlag('S', CHANNEL_STICKY_EOF); |
| 11409 |
ChanFlag('B', CHANNEL_BLOCKED); |
| 11410 |
ChanFlag('/', INPUT_SAW_CR); |
| 11411 |
ChanFlag('D', CHANNEL_DEAD); |
| 11412 |
ChanFlag('R', CHANNEL_RAW_MODE); |
| 11413 |
ChanFlag('x', CHANNEL_INCLOSE); |
| 11414 |
|
| 11415 |
buf[i] ='\0'; |
| 11416 |
|
| 11417 |
fprintf(stderr, "%s: %s\n", str, buf); |
| 11418 |
return 0; |
| 11419 |
} |
| 11420 |
#endif |
| 11421 |
|
| 11422 |
/* |
| 11423 |
* Local Variables: |
| 11424 |
* mode: c |
| 11425 |
* c-basic-offset: 4 |
| 11426 |
* fill-column: 78 |
| 11427 |
* tab-width: 8 |
| 11428 |
* indent-tabs-mode: nil |
| 11429 |
* End: |
| 11430 |
*/ |