| 1 |
/* |
| 2 |
* tclIOGT.c -- |
| 3 |
* |
| 4 |
* Implements a generic transformation exposing the underlying API at the |
| 5 |
* script level. Contributed by Andreas Kupries. |
| 6 |
* |
| 7 |
* Copyright (c) 2000 Ajuba Solutions |
| 8 |
* Copyright (c) 1999-2000 Andreas Kupries ([email protected]) |
| 9 |
* |
| 10 |
* See the file "license.terms" for information on usage and redistribution of |
| 11 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include "tclInt.h" |
| 15 |
#include "tclIO.h" |
| 16 |
|
| 17 |
/* |
| 18 |
* Forward declarations of internal procedures. First the driver procedures of |
| 19 |
* the transformation. |
| 20 |
*/ |
| 21 |
|
| 22 |
static int TransformBlockModeProc(ClientData instanceData, |
| 23 |
int mode); |
| 24 |
static int TransformCloseProc(ClientData instanceData, |
| 25 |
Tcl_Interp *interp); |
| 26 |
static int TransformClose2Proc(ClientData instanceData, |
| 27 |
Tcl_Interp *interp, int flags); |
| 28 |
static int TransformInputProc(ClientData instanceData, char *buf, |
| 29 |
int toRead, int *errorCodePtr); |
| 30 |
static int TransformOutputProc(ClientData instanceData, |
| 31 |
const char *buf, int toWrite, int *errorCodePtr); |
| 32 |
static int TransformSeekProc(ClientData instanceData, long offset, |
| 33 |
int mode, int *errorCodePtr); |
| 34 |
static int TransformSetOptionProc(ClientData instanceData, |
| 35 |
Tcl_Interp *interp, const char *optionName, |
| 36 |
const char *value); |
| 37 |
static int TransformGetOptionProc(ClientData instanceData, |
| 38 |
Tcl_Interp *interp, const char *optionName, |
| 39 |
Tcl_DString *dsPtr); |
| 40 |
static void TransformWatchProc(ClientData instanceData, int mask); |
| 41 |
static int TransformGetFileHandleProc(ClientData instanceData, |
| 42 |
int direction, ClientData *handlePtr); |
| 43 |
static int TransformNotifyProc(ClientData instanceData, int mask); |
| 44 |
static Tcl_WideInt TransformWideSeekProc(ClientData instanceData, |
| 45 |
Tcl_WideInt offset, int mode, int *errorCodePtr); |
| 46 |
|
| 47 |
/* |
| 48 |
* Forward declarations of internal procedures. Secondly the procedures for |
| 49 |
* handling and generating fileeevents. |
| 50 |
*/ |
| 51 |
|
| 52 |
static void TransformChannelHandlerTimer(ClientData clientData); |
| 53 |
|
| 54 |
/* |
| 55 |
* Forward declarations of internal procedures. Third, helper procedures |
| 56 |
* encapsulating essential tasks. |
| 57 |
*/ |
| 58 |
|
| 59 |
typedef struct TransformChannelData TransformChannelData; |
| 60 |
|
| 61 |
static int ExecuteCallback(TransformChannelData *ctrl, |
| 62 |
Tcl_Interp *interp, unsigned char *op, |
| 63 |
unsigned char *buf, int bufLen, int transmit, |
| 64 |
int preserve); |
| 65 |
|
| 66 |
/* |
| 67 |
* Action codes to give to 'ExecuteCallback' (argument 'transmit'), telling |
| 68 |
* the procedure what to do with the result of the script it calls. |
| 69 |
*/ |
| 70 |
|
| 71 |
#define TRANSMIT_DONT 0 /* No transfer to do. */ |
| 72 |
#define TRANSMIT_DOWN 1 /* Transfer to the underlying channel. */ |
| 73 |
#define TRANSMIT_SELF 2 /* Transfer into our channel. */ |
| 74 |
#define TRANSMIT_IBUF 3 /* Transfer to internal input buffer. */ |
| 75 |
#define TRANSMIT_NUM 4 /* Transfer number to 'maxRead'. */ |
| 76 |
|
| 77 |
/* |
| 78 |
* Codes for 'preserve' of 'ExecuteCallback'. |
| 79 |
*/ |
| 80 |
|
| 81 |
#define P_PRESERVE 1 |
| 82 |
#define P_NO_PRESERVE 0 |
| 83 |
|
| 84 |
/* |
| 85 |
* Strings for the action codes delivered to the script implementing a |
| 86 |
* transformation. Argument 'op' of 'ExecuteCallback'. |
| 87 |
*/ |
| 88 |
|
| 89 |
#define A_CREATE_WRITE (UCHARP("create/write")) |
| 90 |
#define A_DELETE_WRITE (UCHARP("delete/write")) |
| 91 |
#define A_FLUSH_WRITE (UCHARP("flush/write")) |
| 92 |
#define A_WRITE (UCHARP("write")) |
| 93 |
|
| 94 |
#define A_CREATE_READ (UCHARP("create/read")) |
| 95 |
#define A_DELETE_READ (UCHARP("delete/read")) |
| 96 |
#define A_FLUSH_READ (UCHARP("flush/read")) |
| 97 |
#define A_READ (UCHARP("read")) |
| 98 |
|
| 99 |
#define A_QUERY_MAXREAD (UCHARP("query/maxRead")) |
| 100 |
#define A_CLEAR_READ (UCHARP("clear/read")) |
| 101 |
|
| 102 |
/* |
| 103 |
* Management of a simple buffer. |
| 104 |
*/ |
| 105 |
|
| 106 |
typedef struct ResultBuffer ResultBuffer; |
| 107 |
|
| 108 |
static inline void ResultClear(ResultBuffer *r); |
| 109 |
static inline void ResultInit(ResultBuffer *r); |
| 110 |
static inline int ResultEmpty(ResultBuffer *r); |
| 111 |
static inline size_t ResultCopy(ResultBuffer *r, unsigned char *buf, |
| 112 |
size_t toRead); |
| 113 |
static inline void ResultAdd(ResultBuffer *r, unsigned char *buf, |
| 114 |
size_t toWrite); |
| 115 |
|
| 116 |
/* |
| 117 |
* This structure describes the channel type structure for Tcl-based |
| 118 |
* transformations. |
| 119 |
*/ |
| 120 |
|
| 121 |
static const Tcl_ChannelType transformChannelType = { |
| 122 |
"transform", /* Type name. */ |
| 123 |
TCL_CHANNEL_VERSION_5, /* v5 channel */ |
| 124 |
TransformCloseProc, /* Close proc. */ |
| 125 |
TransformInputProc, /* Input proc. */ |
| 126 |
TransformOutputProc, /* Output proc. */ |
| 127 |
TransformSeekProc, /* Seek proc. */ |
| 128 |
TransformSetOptionProc, /* Set option proc. */ |
| 129 |
TransformGetOptionProc, /* Get option proc. */ |
| 130 |
TransformWatchProc, /* Initialize notifier. */ |
| 131 |
TransformGetFileHandleProc, /* Get OS handles out of channel. */ |
| 132 |
TransformClose2Proc, /* close2proc */ |
| 133 |
TransformBlockModeProc, /* Set blocking/nonblocking mode.*/ |
| 134 |
NULL, /* Flush proc. */ |
| 135 |
TransformNotifyProc, /* Handling of events bubbling up. */ |
| 136 |
TransformWideSeekProc, /* Wide seek proc. */ |
| 137 |
NULL, /* Thread action. */ |
| 138 |
NULL /* Truncate. */ |
| 139 |
}; |
| 140 |
|
| 141 |
/* |
| 142 |
* Possible values for 'flags' field in control structure, see below. |
| 143 |
*/ |
| 144 |
|
| 145 |
#define CHANNEL_ASYNC (1<<0) /* Non-blocking mode. */ |
| 146 |
|
| 147 |
/* |
| 148 |
* Definition of the structure containing the information about the internal |
| 149 |
* input buffer. |
| 150 |
*/ |
| 151 |
|
| 152 |
struct ResultBuffer { |
| 153 |
unsigned char *buf; /* Reference to the buffer area. */ |
| 154 |
size_t allocated; /* Allocated size of the buffer area. */ |
| 155 |
size_t used; /* Number of bytes in the buffer, no more than |
| 156 |
* number allocated. */ |
| 157 |
}; |
| 158 |
|
| 159 |
/* |
| 160 |
* Additional bytes to allocate during buffer expansion. |
| 161 |
*/ |
| 162 |
|
| 163 |
#define INCREMENT 512 |
| 164 |
|
| 165 |
/* |
| 166 |
* Number of milliseconds to wait before firing an event to flush out |
| 167 |
* information waiting in buffers (fileevent support). |
| 168 |
*/ |
| 169 |
|
| 170 |
#define FLUSH_DELAY 5 |
| 171 |
|
| 172 |
/* |
| 173 |
* Convenience macro to make some casts easier to use. |
| 174 |
*/ |
| 175 |
|
| 176 |
#define UCHARP(x) ((unsigned char *) (x)) |
| 177 |
|
| 178 |
/* |
| 179 |
* Definition of a structure used by all transformations generated here to |
| 180 |
* maintain their local state. |
| 181 |
*/ |
| 182 |
|
| 183 |
struct TransformChannelData { |
| 184 |
/* |
| 185 |
* General section. Data to integrate the transformation into the channel |
| 186 |
* system. |
| 187 |
*/ |
| 188 |
|
| 189 |
Tcl_Channel self; /* Our own Channel handle. */ |
| 190 |
int readIsFlushed; /* Flag to note whether in.flushProc was |
| 191 |
* called or not. */ |
| 192 |
int eofPending; /* Flag: EOF seen down, not raised up */ |
| 193 |
int flags; /* Currently CHANNEL_ASYNC or zero. */ |
| 194 |
int watchMask; /* Current watch/event/interest mask. */ |
| 195 |
int mode; /* Mode of parent channel, OR'ed combination |
| 196 |
* of TCL_READABLE, TCL_WRITABLE. */ |
| 197 |
Tcl_TimerToken timer; /* Timer for automatic flushing of information |
| 198 |
* sitting in an internal buffer. Required for |
| 199 |
* full fileevent support. */ |
| 200 |
|
| 201 |
/* |
| 202 |
* Transformation specific data. |
| 203 |
*/ |
| 204 |
|
| 205 |
int maxRead; /* Maximum allowed number of bytes to read, as |
| 206 |
* given to us by the Tcl script implementing |
| 207 |
* the transformation. */ |
| 208 |
Tcl_Interp *interp; /* Reference to the interpreter which created |
| 209 |
* the transformation. Used to execute the |
| 210 |
* code below. */ |
| 211 |
Tcl_Obj *command; /* Tcl code to execute for a buffer */ |
| 212 |
ResultBuffer result; /* Internal buffer used to store the result of |
| 213 |
* a transformation of incoming data. Also |
| 214 |
* serves as buffer of all data not yet |
| 215 |
* consumed by the reader. */ |
| 216 |
int refCount; |
| 217 |
}; |
| 218 |
|
| 219 |
static void |
| 220 |
PreserveData( |
| 221 |
TransformChannelData *dataPtr) |
| 222 |
{ |
| 223 |
dataPtr->refCount++; |
| 224 |
} |
| 225 |
|
| 226 |
static void |
| 227 |
ReleaseData( |
| 228 |
TransformChannelData *dataPtr) |
| 229 |
{ |
| 230 |
if (--dataPtr->refCount) { |
| 231 |
return; |
| 232 |
} |
| 233 |
ResultClear(&dataPtr->result); |
| 234 |
Tcl_DecrRefCount(dataPtr->command); |
| 235 |
ckfree(dataPtr); |
| 236 |
} |
| 237 |
|
| 238 |
/* |
| 239 |
*---------------------------------------------------------------------- |
| 240 |
* |
| 241 |
* TclChannelTransform -- |
| 242 |
* |
| 243 |
* Implements the Tcl "testchannel transform" debugging command. This is |
| 244 |
* part of the testing environment. This sets up a tcl script (cmdObjPtr) |
| 245 |
* to be used as a transform on the channel. |
| 246 |
* |
| 247 |
* Results: |
| 248 |
* A standard Tcl result. |
| 249 |
* |
| 250 |
* Side effects: |
| 251 |
* None. |
| 252 |
* |
| 253 |
*---------------------------------------------------------------------- |
| 254 |
*/ |
| 255 |
|
| 256 |
/* ARGSUSED */ |
| 257 |
int |
| 258 |
TclChannelTransform( |
| 259 |
Tcl_Interp *interp, /* Interpreter for result. */ |
| 260 |
Tcl_Channel chan, /* Channel to transform. */ |
| 261 |
Tcl_Obj *cmdObjPtr) /* Script to use for transform. */ |
| 262 |
{ |
| 263 |
Channel *chanPtr; /* The actual channel. */ |
| 264 |
ChannelState *statePtr; /* State info for channel. */ |
| 265 |
int mode; /* Read/write mode of the channel. */ |
| 266 |
int objc; |
| 267 |
TransformChannelData *dataPtr; |
| 268 |
Tcl_DString ds; |
| 269 |
|
| 270 |
if (chan == NULL) { |
| 271 |
return TCL_ERROR; |
| 272 |
} |
| 273 |
|
| 274 |
if (TCL_OK != TclListObjLength(interp, cmdObjPtr, &objc)) { |
| 275 |
Tcl_SetObjResult(interp, |
| 276 |
Tcl_NewStringObj("-command value is not a list", -1)); |
| 277 |
return TCL_ERROR; |
| 278 |
} |
| 279 |
|
| 280 |
chanPtr = (Channel *) chan; |
| 281 |
statePtr = chanPtr->state; |
| 282 |
chanPtr = statePtr->topChanPtr; |
| 283 |
chan = (Tcl_Channel) chanPtr; |
| 284 |
mode = (statePtr->flags & (TCL_READABLE|TCL_WRITABLE)); |
| 285 |
|
| 286 |
/* |
| 287 |
* Now initialize the transformation state and stack it upon the specified |
| 288 |
* channel. One of the necessary things to do is to retrieve the blocking |
| 289 |
* regime of the underlying channel and to use the same for us too. |
| 290 |
*/ |
| 291 |
|
| 292 |
dataPtr = (TransformChannelData *)ckalloc(sizeof(TransformChannelData)); |
| 293 |
|
| 294 |
dataPtr->refCount = 1; |
| 295 |
Tcl_DStringInit(&ds); |
| 296 |
Tcl_GetChannelOption(interp, chan, "-blocking", &ds); |
| 297 |
dataPtr->readIsFlushed = 0; |
| 298 |
dataPtr->eofPending = 0; |
| 299 |
dataPtr->flags = 0; |
| 300 |
if (ds.string[0] == '0') { |
| 301 |
dataPtr->flags |= CHANNEL_ASYNC; |
| 302 |
} |
| 303 |
Tcl_DStringFree(&ds); |
| 304 |
|
| 305 |
dataPtr->watchMask = 0; |
| 306 |
dataPtr->mode = mode; |
| 307 |
dataPtr->timer = NULL; |
| 308 |
dataPtr->maxRead = 4096; /* Initial value not relevant. */ |
| 309 |
dataPtr->interp = interp; |
| 310 |
dataPtr->command = cmdObjPtr; |
| 311 |
Tcl_IncrRefCount(dataPtr->command); |
| 312 |
|
| 313 |
ResultInit(&dataPtr->result); |
| 314 |
|
| 315 |
dataPtr->self = Tcl_StackChannel(interp, &transformChannelType, dataPtr, |
| 316 |
mode, chan); |
| 317 |
if (dataPtr->self == NULL) { |
| 318 |
Tcl_AppendPrintfToObj(Tcl_GetObjResult(interp), |
| 319 |
"\nfailed to stack channel \"%s\"", Tcl_GetChannelName(chan)); |
| 320 |
ReleaseData(dataPtr); |
| 321 |
return TCL_ERROR; |
| 322 |
} |
| 323 |
Tcl_Preserve(dataPtr->self); |
| 324 |
|
| 325 |
/* |
| 326 |
* At last initialize the transformation at the script level. |
| 327 |
*/ |
| 328 |
|
| 329 |
PreserveData(dataPtr); |
| 330 |
if ((dataPtr->mode & TCL_WRITABLE) && ExecuteCallback(dataPtr, NULL, |
| 331 |
A_CREATE_WRITE, NULL, 0, TRANSMIT_DONT, P_NO_PRESERVE) != TCL_OK){ |
| 332 |
Tcl_UnstackChannel(interp, chan); |
| 333 |
ReleaseData(dataPtr); |
| 334 |
return TCL_ERROR; |
| 335 |
} |
| 336 |
|
| 337 |
if ((dataPtr->mode & TCL_READABLE) && ExecuteCallback(dataPtr, NULL, |
| 338 |
A_CREATE_READ, NULL, 0, TRANSMIT_DONT, P_NO_PRESERVE) != TCL_OK) { |
| 339 |
ExecuteCallback(dataPtr, NULL, A_DELETE_WRITE, NULL, 0, TRANSMIT_DONT, |
| 340 |
P_NO_PRESERVE); |
| 341 |
Tcl_UnstackChannel(interp, chan); |
| 342 |
ReleaseData(dataPtr); |
| 343 |
return TCL_ERROR; |
| 344 |
} |
| 345 |
|
| 346 |
ReleaseData(dataPtr); |
| 347 |
return TCL_OK; |
| 348 |
} |
| 349 |
|
| 350 |
/* |
| 351 |
*---------------------------------------------------------------------- |
| 352 |
* |
| 353 |
* ExecuteCallback -- |
| 354 |
* |
| 355 |
* Executes the defined callback for buffer and operation. |
| 356 |
* |
| 357 |
* Side effects: |
| 358 |
* As of the executed tcl script. |
| 359 |
* |
| 360 |
* Result: |
| 361 |
* A standard TCL error code. In case of an error a message is left in |
| 362 |
* the result area of the specified interpreter. |
| 363 |
* |
| 364 |
*---------------------------------------------------------------------- |
| 365 |
*/ |
| 366 |
|
| 367 |
static int |
| 368 |
ExecuteCallback( |
| 369 |
TransformChannelData *dataPtr, |
| 370 |
/* Transformation with the callback. */ |
| 371 |
Tcl_Interp *interp, /* Current interpreter, possibly NULL. */ |
| 372 |
unsigned char *op, /* Operation invoking the callback. */ |
| 373 |
unsigned char *buf, /* Buffer to give to the script. */ |
| 374 |
int bufLen, /* And its length. */ |
| 375 |
int transmit, /* Flag, determines whether the result of the |
| 376 |
* callback is sent to the underlying channel |
| 377 |
* or not. */ |
| 378 |
int preserve) /* Flag. If true the procedure will preserve |
| 379 |
* the result state of all accessed |
| 380 |
* interpreters. */ |
| 381 |
{ |
| 382 |
Tcl_Obj *resObj; /* See below, switch (transmit). */ |
| 383 |
int resLen; |
| 384 |
unsigned char *resBuf; |
| 385 |
Tcl_InterpState state = NULL; |
| 386 |
int res = TCL_OK; |
| 387 |
Tcl_Obj *command = TclListObjCopy(NULL, dataPtr->command); |
| 388 |
Tcl_Interp *eval = dataPtr->interp; |
| 389 |
|
| 390 |
Tcl_Preserve(eval); |
| 391 |
|
| 392 |
/* |
| 393 |
* Step 1, create the complete command to execute. Do this by appending |
| 394 |
* operation and buffer to operate upon to a copy of the callback |
| 395 |
* definition. We *cannot* create a list containing 3 objects and then use |
| 396 |
* 'Tcl_EvalObjv', because the command may contain additional prefixed |
| 397 |
* arguments. Feather's curried commands would come in handy here. |
| 398 |
*/ |
| 399 |
|
| 400 |
if (preserve == P_PRESERVE) { |
| 401 |
state = Tcl_SaveInterpState(eval, res); |
| 402 |
} |
| 403 |
|
| 404 |
Tcl_IncrRefCount(command); |
| 405 |
Tcl_ListObjAppendElement(NULL, command, Tcl_NewStringObj((char *) op, -1)); |
| 406 |
|
| 407 |
/* |
| 408 |
* Use a byte-array to prevent the misinterpretation of binary data coming |
| 409 |
* through as Utf while at the tcl level. |
| 410 |
*/ |
| 411 |
|
| 412 |
Tcl_ListObjAppendElement(NULL, command, Tcl_NewByteArrayObj(buf, bufLen)); |
| 413 |
|
| 414 |
/* |
| 415 |
* Step 2, execute the command at the global level of the interpreter used |
| 416 |
* to create the transformation. Destroy the command afterward. If an |
| 417 |
* error occurred and the current interpreter is defined and not equal to |
| 418 |
* the interpreter for the callback, then copy the error message into |
| 419 |
* current interpreter. Don't copy if in preservation mode. |
| 420 |
*/ |
| 421 |
|
| 422 |
res = Tcl_EvalObjEx(eval, command, TCL_EVAL_GLOBAL); |
| 423 |
Tcl_DecrRefCount(command); |
| 424 |
command = NULL; |
| 425 |
|
| 426 |
if ((res != TCL_OK) && (interp != NULL) && (eval != interp) |
| 427 |
&& (preserve == P_NO_PRESERVE)) { |
| 428 |
Tcl_SetObjResult(interp, Tcl_GetObjResult(eval)); |
| 429 |
Tcl_Release(eval); |
| 430 |
return res; |
| 431 |
} |
| 432 |
|
| 433 |
/* |
| 434 |
* Step 3, transmit a possible conversion result to the underlying |
| 435 |
* channel, or ourselves. |
| 436 |
*/ |
| 437 |
|
| 438 |
switch (transmit) { |
| 439 |
case TRANSMIT_DONT: |
| 440 |
/* nothing to do */ |
| 441 |
break; |
| 442 |
|
| 443 |
case TRANSMIT_DOWN: |
| 444 |
if (dataPtr->self == NULL) { |
| 445 |
break; |
| 446 |
} |
| 447 |
resObj = Tcl_GetObjResult(eval); |
| 448 |
resBuf = Tcl_GetByteArrayFromObj(resObj, &resLen); |
| 449 |
Tcl_WriteRaw(Tcl_GetStackedChannel(dataPtr->self), (char *) resBuf, |
| 450 |
resLen); |
| 451 |
break; |
| 452 |
|
| 453 |
case TRANSMIT_SELF: |
| 454 |
if (dataPtr->self == NULL) { |
| 455 |
break; |
| 456 |
} |
| 457 |
resObj = Tcl_GetObjResult(eval); |
| 458 |
resBuf = Tcl_GetByteArrayFromObj(resObj, &resLen); |
| 459 |
Tcl_WriteRaw(dataPtr->self, (char *) resBuf, resLen); |
| 460 |
break; |
| 461 |
|
| 462 |
case TRANSMIT_IBUF: |
| 463 |
resObj = Tcl_GetObjResult(eval); |
| 464 |
resBuf = Tcl_GetByteArrayFromObj(resObj, &resLen); |
| 465 |
ResultAdd(&dataPtr->result, resBuf, resLen); |
| 466 |
break; |
| 467 |
|
| 468 |
case TRANSMIT_NUM: |
| 469 |
/* |
| 470 |
* Interpret result as integer number. |
| 471 |
*/ |
| 472 |
|
| 473 |
resObj = Tcl_GetObjResult(eval); |
| 474 |
TclGetIntFromObj(eval, resObj, &dataPtr->maxRead); |
| 475 |
break; |
| 476 |
} |
| 477 |
|
| 478 |
Tcl_ResetResult(eval); |
| 479 |
if (preserve == P_PRESERVE) { |
| 480 |
(void) Tcl_RestoreInterpState(eval, state); |
| 481 |
} |
| 482 |
Tcl_Release(eval); |
| 483 |
return res; |
| 484 |
} |
| 485 |
|
| 486 |
/* |
| 487 |
*---------------------------------------------------------------------- |
| 488 |
* |
| 489 |
* TransformBlockModeProc -- |
| 490 |
* |
| 491 |
* Trap handler. Called by the generic IO system during option processing |
| 492 |
* to change the blocking mode of the channel. |
| 493 |
* |
| 494 |
* Side effects: |
| 495 |
* Forwards the request to the underlying channel. |
| 496 |
* |
| 497 |
* Result: |
| 498 |
* 0 if successful, errno when failed. |
| 499 |
* |
| 500 |
*---------------------------------------------------------------------- |
| 501 |
*/ |
| 502 |
|
| 503 |
static int |
| 504 |
TransformBlockModeProc( |
| 505 |
ClientData instanceData, /* State of transformation. */ |
| 506 |
int mode) /* New blocking mode. */ |
| 507 |
{ |
| 508 |
TransformChannelData *dataPtr = (TransformChannelData *)instanceData; |
| 509 |
|
| 510 |
if (mode == TCL_MODE_NONBLOCKING) { |
| 511 |
dataPtr->flags |= CHANNEL_ASYNC; |
| 512 |
} else { |
| 513 |
dataPtr->flags &= ~CHANNEL_ASYNC; |
| 514 |
} |
| 515 |
return 0; |
| 516 |
} |
| 517 |
|
| 518 |
/* |
| 519 |
*---------------------------------------------------------------------- |
| 520 |
* |
| 521 |
* TransformCloseProc/TransformClose2Proc -- |
| 522 |
* |
| 523 |
* Trap handler. Called by the generic IO system during destruction of |
| 524 |
* the transformation channel. |
| 525 |
* |
| 526 |
* Side effects: |
| 527 |
* Releases the memory allocated in 'Tcl_TransformObjCmd'. |
| 528 |
* |
| 529 |
* Result: |
| 530 |
* None. |
| 531 |
* |
| 532 |
*---------------------------------------------------------------------- |
| 533 |
*/ |
| 534 |
|
| 535 |
static int |
| 536 |
TransformCloseProc( |
| 537 |
ClientData instanceData, |
| 538 |
Tcl_Interp *interp) |
| 539 |
{ |
| 540 |
TransformChannelData *dataPtr = (TransformChannelData *)instanceData; |
| 541 |
|
| 542 |
/* |
| 543 |
* Important: In this procedure 'dataPtr->self' already points to the |
| 544 |
* underlying channel. |
| 545 |
* |
| 546 |
* There is no need to cancel an existing channel handler, this is already |
| 547 |
* done. Either by 'Tcl_UnstackChannel' or by the general cleanup in |
| 548 |
* 'Tcl_Close'. |
| 549 |
* |
| 550 |
* But we have to cancel an active timer to prevent it from firing on the |
| 551 |
* removed channel. |
| 552 |
*/ |
| 553 |
|
| 554 |
if (dataPtr->timer != NULL) { |
| 555 |
Tcl_DeleteTimerHandler(dataPtr->timer); |
| 556 |
dataPtr->timer = NULL; |
| 557 |
} |
| 558 |
|
| 559 |
/* |
| 560 |
* Now flush data waiting in internal buffers to output and input. The |
| 561 |
* input must be done despite the fact that there is no real receiver for |
| 562 |
* it anymore. But the scripts might have sideeffects other parts of the |
| 563 |
* system rely on (f.e. signalling the close to interested parties). |
| 564 |
*/ |
| 565 |
|
| 566 |
PreserveData(dataPtr); |
| 567 |
if (dataPtr->mode & TCL_WRITABLE) { |
| 568 |
ExecuteCallback(dataPtr, interp, A_FLUSH_WRITE, NULL, 0, |
| 569 |
TRANSMIT_DOWN, P_PRESERVE); |
| 570 |
} |
| 571 |
|
| 572 |
if ((dataPtr->mode & TCL_READABLE) && !dataPtr->readIsFlushed) { |
| 573 |
dataPtr->readIsFlushed = 1; |
| 574 |
ExecuteCallback(dataPtr, interp, A_FLUSH_READ, NULL, 0, TRANSMIT_IBUF, |
| 575 |
P_PRESERVE); |
| 576 |
} |
| 577 |
|
| 578 |
if (dataPtr->mode & TCL_WRITABLE) { |
| 579 |
ExecuteCallback(dataPtr, interp, A_DELETE_WRITE, NULL, 0, |
| 580 |
TRANSMIT_DONT, P_PRESERVE); |
| 581 |
} |
| 582 |
if (dataPtr->mode & TCL_READABLE) { |
| 583 |
ExecuteCallback(dataPtr, interp, A_DELETE_READ, NULL, 0, |
| 584 |
TRANSMIT_DONT, P_PRESERVE); |
| 585 |
} |
| 586 |
ReleaseData(dataPtr); |
| 587 |
|
| 588 |
/* |
| 589 |
* General cleanup. |
| 590 |
*/ |
| 591 |
|
| 592 |
Tcl_Release(dataPtr->self); |
| 593 |
dataPtr->self = NULL; |
| 594 |
ReleaseData(dataPtr); |
| 595 |
return TCL_OK; |
| 596 |
} |
| 597 |
|
| 598 |
static int |
| 599 |
TransformClose2Proc( |
| 600 |
ClientData instanceData, |
| 601 |
Tcl_Interp *interp, |
| 602 |
int flags) |
| 603 |
{ |
| 604 |
if ((flags & (TCL_CLOSE_READ | TCL_CLOSE_WRITE)) == 0) { |
| 605 |
return TransformCloseProc(instanceData, interp); |
| 606 |
} |
| 607 |
return EINVAL; |
| 608 |
} |
| 609 |
|
| 610 |
/* |
| 611 |
*---------------------------------------------------------------------- |
| 612 |
* |
| 613 |
* TransformInputProc -- |
| 614 |
* |
| 615 |
* Called by the generic IO system to convert read data. |
| 616 |
* |
| 617 |
* Side effects: |
| 618 |
* As defined by the conversion. |
| 619 |
* |
| 620 |
* Result: |
| 621 |
* A transformed buffer. |
| 622 |
* |
| 623 |
*---------------------------------------------------------------------- |
| 624 |
*/ |
| 625 |
|
| 626 |
static int |
| 627 |
TransformInputProc( |
| 628 |
ClientData instanceData, |
| 629 |
char *buf, |
| 630 |
int toRead, |
| 631 |
int *errorCodePtr) |
| 632 |
{ |
| 633 |
TransformChannelData *dataPtr = (TransformChannelData *)instanceData; |
| 634 |
int gotBytes, read, copied; |
| 635 |
Tcl_Channel downChan; |
| 636 |
|
| 637 |
/* |
| 638 |
* Should assert(dataPtr->mode & TCL_READABLE); |
| 639 |
*/ |
| 640 |
|
| 641 |
if (toRead == 0 || dataPtr->self == NULL) { |
| 642 |
/* |
| 643 |
* Catch a no-op. TODO: Is this a panic()? |
| 644 |
*/ |
| 645 |
return 0; |
| 646 |
} |
| 647 |
|
| 648 |
gotBytes = 0; |
| 649 |
downChan = Tcl_GetStackedChannel(dataPtr->self); |
| 650 |
|
| 651 |
PreserveData(dataPtr); |
| 652 |
while (toRead > 0) { |
| 653 |
/* |
| 654 |
* Loop until the request is satisfied (or no data is available from |
| 655 |
* below, possibly EOF). |
| 656 |
*/ |
| 657 |
|
| 658 |
copied = ResultCopy(&dataPtr->result, UCHARP(buf), toRead); |
| 659 |
toRead -= copied; |
| 660 |
buf += copied; |
| 661 |
gotBytes += copied; |
| 662 |
|
| 663 |
if (toRead == 0) { |
| 664 |
/* |
| 665 |
* The request was completely satisfied from our buffers. We can |
| 666 |
* break out of the loop and return to the caller. |
| 667 |
*/ |
| 668 |
|
| 669 |
break; |
| 670 |
} |
| 671 |
|
| 672 |
/* |
| 673 |
* Length (dataPtr->result) == 0, toRead > 0 here. Use the incoming |
| 674 |
* 'buf'! as target to store the intermediary information read from |
| 675 |
* the underlying channel. |
| 676 |
* |
| 677 |
* Ask the tcl level how much data it allows us to read from the |
| 678 |
* underlying channel. This feature allows the transform to signal EOF |
| 679 |
* upstream although there is none downstream. Useful to control an |
| 680 |
* unbounded 'fcopy', either through counting bytes, or by pattern |
| 681 |
* matching. |
| 682 |
*/ |
| 683 |
|
| 684 |
ExecuteCallback(dataPtr, NULL, A_QUERY_MAXREAD, NULL, 0, |
| 685 |
TRANSMIT_NUM /* -> maxRead */, P_PRESERVE); |
| 686 |
|
| 687 |
if (dataPtr->maxRead >= 0) { |
| 688 |
if (dataPtr->maxRead < toRead) { |
| 689 |
toRead = dataPtr->maxRead; |
| 690 |
} |
| 691 |
} /* else: 'maxRead < 0' == Accept the current value of toRead. */ |
| 692 |
if (toRead <= 0) { |
| 693 |
break; |
| 694 |
} |
| 695 |
if (dataPtr->eofPending) { |
| 696 |
/* |
| 697 |
* Already saw EOF from downChan; don't ask again. |
| 698 |
* NOTE: Could move this up to avoid the last maxRead |
| 699 |
* execution. Believe this would still be correct behavior, |
| 700 |
* but the test suite tests the whole command callback |
| 701 |
* sequence, so leave it unchanged for now. |
| 702 |
*/ |
| 703 |
|
| 704 |
break; |
| 705 |
} |
| 706 |
|
| 707 |
/* |
| 708 |
* Get bytes from the underlying channel. |
| 709 |
*/ |
| 710 |
|
| 711 |
read = Tcl_ReadRaw(downChan, buf, toRead); |
| 712 |
if (read < 0) { |
| 713 |
if (Tcl_InputBlocked(downChan) && (gotBytes > 0)) { |
| 714 |
/* |
| 715 |
* Zero bytes available from downChan because blocked. |
| 716 |
* But nonzero bytes already copied, so total is a |
| 717 |
* valid blocked short read. Return to caller. |
| 718 |
*/ |
| 719 |
|
| 720 |
break; |
| 721 |
} |
| 722 |
|
| 723 |
/* |
| 724 |
* Either downChan is not blocked (there's a real error). |
| 725 |
* or it is and there are no bytes copied yet. In either |
| 726 |
* case we want to pass the "error" along to the caller, |
| 727 |
* either to report an error, or to signal to the caller |
| 728 |
* that zero bytes are available because blocked. |
| 729 |
*/ |
| 730 |
|
| 731 |
*errorCodePtr = Tcl_GetErrno(); |
| 732 |
gotBytes = -1; |
| 733 |
break; |
| 734 |
} else if (read == 0) { |
| 735 |
|
| 736 |
/* |
| 737 |
* Zero returned from Tcl_ReadRaw() always indicates EOF |
| 738 |
* on the down channel. |
| 739 |
*/ |
| 740 |
|
| 741 |
dataPtr->eofPending = 1; |
| 742 |
dataPtr->readIsFlushed = 1; |
| 743 |
ExecuteCallback(dataPtr, NULL, A_FLUSH_READ, NULL, 0, |
| 744 |
TRANSMIT_IBUF, P_PRESERVE); |
| 745 |
|
| 746 |
if (ResultEmpty(&dataPtr->result)) { |
| 747 |
/* |
| 748 |
* We had nothing to flush. |
| 749 |
*/ |
| 750 |
|
| 751 |
break; |
| 752 |
} |
| 753 |
|
| 754 |
continue; /* at: while (toRead > 0) */ |
| 755 |
} /* read == 0 */ |
| 756 |
|
| 757 |
/* |
| 758 |
* Transform the read chunk and add the result to our read buffer |
| 759 |
* (dataPtr->result). |
| 760 |
*/ |
| 761 |
|
| 762 |
if (ExecuteCallback(dataPtr, NULL, A_READ, UCHARP(buf), read, |
| 763 |
TRANSMIT_IBUF, P_PRESERVE) != TCL_OK) { |
| 764 |
*errorCodePtr = EINVAL; |
| 765 |
gotBytes = -1; |
| 766 |
break; |
| 767 |
} |
| 768 |
} /* while toRead > 0 */ |
| 769 |
|
| 770 |
if (gotBytes == 0) { |
| 771 |
dataPtr->eofPending = 0; |
| 772 |
} |
| 773 |
ReleaseData(dataPtr); |
| 774 |
return gotBytes; |
| 775 |
} |
| 776 |
|
| 777 |
/* |
| 778 |
*---------------------------------------------------------------------- |
| 779 |
* |
| 780 |
* TransformOutputProc -- |
| 781 |
* |
| 782 |
* Called by the generic IO system to convert data waiting to be written. |
| 783 |
* |
| 784 |
* Side effects: |
| 785 |
* As defined by the transformation. |
| 786 |
* |
| 787 |
* Result: |
| 788 |
* A transformed buffer. |
| 789 |
* |
| 790 |
*---------------------------------------------------------------------- |
| 791 |
*/ |
| 792 |
|
| 793 |
static int |
| 794 |
TransformOutputProc( |
| 795 |
ClientData instanceData, |
| 796 |
const char *buf, |
| 797 |
int toWrite, |
| 798 |
int *errorCodePtr) |
| 799 |
{ |
| 800 |
TransformChannelData *dataPtr = (TransformChannelData *)instanceData; |
| 801 |
|
| 802 |
/* |
| 803 |
* Should assert(dataPtr->mode & TCL_WRITABLE); |
| 804 |
*/ |
| 805 |
|
| 806 |
if (toWrite == 0) { |
| 807 |
/* |
| 808 |
* Catch a no-op. |
| 809 |
*/ |
| 810 |
|
| 811 |
return 0; |
| 812 |
} |
| 813 |
|
| 814 |
PreserveData(dataPtr); |
| 815 |
if (ExecuteCallback(dataPtr, NULL, A_WRITE, UCHARP(buf), toWrite, |
| 816 |
TRANSMIT_DOWN, P_NO_PRESERVE) != TCL_OK) { |
| 817 |
*errorCodePtr = EINVAL; |
| 818 |
toWrite = -1; |
| 819 |
} |
| 820 |
ReleaseData(dataPtr); |
| 821 |
|
| 822 |
return toWrite; |
| 823 |
} |
| 824 |
|
| 825 |
/* |
| 826 |
*---------------------------------------------------------------------- |
| 827 |
* |
| 828 |
* TransformSeekProc -- |
| 829 |
* |
| 830 |
* This procedure is called by the generic IO level to move the access |
| 831 |
* point in a channel. |
| 832 |
* |
| 833 |
* Side effects: |
| 834 |
* Moves the location at which the channel will be accessed in future |
| 835 |
* operations. Flushes all transformation buffers, then forwards it to |
| 836 |
* the underlying channel. |
| 837 |
* |
| 838 |
* Result: |
| 839 |
* -1 if failed, the new position if successful. An output argument |
| 840 |
* contains the POSIX error code if an error occurred, or zero. |
| 841 |
* |
| 842 |
*---------------------------------------------------------------------- |
| 843 |
*/ |
| 844 |
|
| 845 |
static int |
| 846 |
TransformSeekProc( |
| 847 |
ClientData instanceData, /* The channel to manipulate. */ |
| 848 |
long offset, /* Size of movement. */ |
| 849 |
int mode, /* How to move. */ |
| 850 |
int *errorCodePtr) /* Location of error flag. */ |
| 851 |
{ |
| 852 |
TransformChannelData *dataPtr = (TransformChannelData *)instanceData; |
| 853 |
Tcl_Channel parent = Tcl_GetStackedChannel(dataPtr->self); |
| 854 |
const Tcl_ChannelType *parentType = Tcl_GetChannelType(parent); |
| 855 |
Tcl_DriverSeekProc *parentSeekProc = Tcl_ChannelSeekProc(parentType); |
| 856 |
|
| 857 |
if ((offset == 0) && (mode == SEEK_CUR)) { |
| 858 |
/* |
| 859 |
* This is no seek but a request to tell the caller the current |
| 860 |
* location. Simply pass the request down. |
| 861 |
*/ |
| 862 |
|
| 863 |
return parentSeekProc(Tcl_GetChannelInstanceData(parent), offset, |
| 864 |
mode, errorCodePtr); |
| 865 |
} |
| 866 |
|
| 867 |
/* |
| 868 |
* It is a real request to change the position. Flush all data waiting for |
| 869 |
* output and discard everything in the input buffers. Then pass the |
| 870 |
* request down, unchanged. |
| 871 |
*/ |
| 872 |
|
| 873 |
PreserveData(dataPtr); |
| 874 |
if (dataPtr->mode & TCL_WRITABLE) { |
| 875 |
ExecuteCallback(dataPtr, NULL, A_FLUSH_WRITE, NULL, 0, TRANSMIT_DOWN, |
| 876 |
P_NO_PRESERVE); |
| 877 |
} |
| 878 |
|
| 879 |
if (dataPtr->mode & TCL_READABLE) { |
| 880 |
ExecuteCallback(dataPtr, NULL, A_CLEAR_READ, NULL, 0, TRANSMIT_DONT, |
| 881 |
P_NO_PRESERVE); |
| 882 |
ResultClear(&dataPtr->result); |
| 883 |
dataPtr->readIsFlushed = 0; |
| 884 |
dataPtr->eofPending = 0; |
| 885 |
} |
| 886 |
ReleaseData(dataPtr); |
| 887 |
|
| 888 |
return parentSeekProc(Tcl_GetChannelInstanceData(parent), offset, mode, |
| 889 |
errorCodePtr); |
| 890 |
} |
| 891 |
|
| 892 |
/* |
| 893 |
*---------------------------------------------------------------------- |
| 894 |
* |
| 895 |
* TransformWideSeekProc -- |
| 896 |
* |
| 897 |
* This procedure is called by the generic IO level to move the access |
| 898 |
* point in a channel, with a (potentially) 64-bit offset. |
| 899 |
* |
| 900 |
* Side effects: |
| 901 |
* Moves the location at which the channel will be accessed in future |
| 902 |
* operations. Flushes all transformation buffers, then forwards it to |
| 903 |
* the underlying channel. |
| 904 |
* |
| 905 |
* Result: |
| 906 |
* -1 if failed, the new position if successful. An output argument |
| 907 |
* contains the POSIX error code if an error occurred, or zero. |
| 908 |
* |
| 909 |
*---------------------------------------------------------------------- |
| 910 |
*/ |
| 911 |
|
| 912 |
static Tcl_WideInt |
| 913 |
TransformWideSeekProc( |
| 914 |
ClientData instanceData, /* The channel to manipulate. */ |
| 915 |
Tcl_WideInt offset, /* Size of movement. */ |
| 916 |
int mode, /* How to move. */ |
| 917 |
int *errorCodePtr) /* Location of error flag. */ |
| 918 |
{ |
| 919 |
TransformChannelData *dataPtr = (TransformChannelData *)instanceData; |
| 920 |
Tcl_Channel parent = Tcl_GetStackedChannel(dataPtr->self); |
| 921 |
const Tcl_ChannelType *parentType = Tcl_GetChannelType(parent); |
| 922 |
Tcl_DriverSeekProc *parentSeekProc = Tcl_ChannelSeekProc(parentType); |
| 923 |
Tcl_DriverWideSeekProc *parentWideSeekProc = |
| 924 |
Tcl_ChannelWideSeekProc(parentType); |
| 925 |
ClientData parentData = Tcl_GetChannelInstanceData(parent); |
| 926 |
|
| 927 |
if ((offset == 0) && (mode == SEEK_CUR)) { |
| 928 |
/* |
| 929 |
* This is no seek but a request to tell the caller the current |
| 930 |
* location. Simply pass the request down. |
| 931 |
*/ |
| 932 |
|
| 933 |
if (parentWideSeekProc != NULL) { |
| 934 |
return parentWideSeekProc(parentData, offset, mode, errorCodePtr); |
| 935 |
} |
| 936 |
|
| 937 |
return Tcl_LongAsWide(parentSeekProc(parentData, 0, mode, |
| 938 |
errorCodePtr)); |
| 939 |
} |
| 940 |
|
| 941 |
/* |
| 942 |
* It is a real request to change the position. Flush all data waiting for |
| 943 |
* output and discard everything in the input buffers. Then pass the |
| 944 |
* request down, unchanged. |
| 945 |
*/ |
| 946 |
|
| 947 |
PreserveData(dataPtr); |
| 948 |
if (dataPtr->mode & TCL_WRITABLE) { |
| 949 |
ExecuteCallback(dataPtr, NULL, A_FLUSH_WRITE, NULL, 0, TRANSMIT_DOWN, |
| 950 |
P_NO_PRESERVE); |
| 951 |
} |
| 952 |
|
| 953 |
if (dataPtr->mode & TCL_READABLE) { |
| 954 |
ExecuteCallback(dataPtr, NULL, A_CLEAR_READ, NULL, 0, TRANSMIT_DONT, |
| 955 |
P_NO_PRESERVE); |
| 956 |
ResultClear(&dataPtr->result); |
| 957 |
dataPtr->readIsFlushed = 0; |
| 958 |
dataPtr->eofPending = 0; |
| 959 |
} |
| 960 |
ReleaseData(dataPtr); |
| 961 |
|
| 962 |
/* |
| 963 |
* If we have a wide seek capability, we should stick with that. |
| 964 |
*/ |
| 965 |
|
| 966 |
if (parentWideSeekProc != NULL) { |
| 967 |
return parentWideSeekProc(parentData, offset, mode, errorCodePtr); |
| 968 |
} |
| 969 |
|
| 970 |
/* |
| 971 |
* We're transferring to narrow seeks at this point; this is a bit complex |
| 972 |
* because we have to check whether the seek is possible first (i.e. |
| 973 |
* whether we are losing information in truncating the bits of the |
| 974 |
* offset). Luckily, there's a defined error for what happens when trying |
| 975 |
* to go out of the representable range. |
| 976 |
*/ |
| 977 |
|
| 978 |
if (offset<Tcl_LongAsWide(LONG_MIN) || offset>Tcl_LongAsWide(LONG_MAX)) { |
| 979 |
*errorCodePtr = EOVERFLOW; |
| 980 |
return Tcl_LongAsWide(-1); |
| 981 |
} |
| 982 |
|
| 983 |
return Tcl_LongAsWide(parentSeekProc(parentData, Tcl_WideAsLong(offset), |
| 984 |
mode, errorCodePtr)); |
| 985 |
} |
| 986 |
|
| 987 |
/* |
| 988 |
*---------------------------------------------------------------------- |
| 989 |
* |
| 990 |
* TransformSetOptionProc -- |
| 991 |
* |
| 992 |
* Called by generic layer to handle the reconfiguration of channel |
| 993 |
* specific options. As this channel type does not have such, it simply |
| 994 |
* passes all requests downstream. |
| 995 |
* |
| 996 |
* Side effects: |
| 997 |
* As defined by the channel downstream. |
| 998 |
* |
| 999 |
* Result: |
| 1000 |
* A standard TCL error code. |
| 1001 |
* |
| 1002 |
*---------------------------------------------------------------------- |
| 1003 |
*/ |
| 1004 |
|
| 1005 |
static int |
| 1006 |
TransformSetOptionProc( |
| 1007 |
ClientData instanceData, |
| 1008 |
Tcl_Interp *interp, |
| 1009 |
const char *optionName, |
| 1010 |
const char *value) |
| 1011 |
{ |
| 1012 |
TransformChannelData *dataPtr = (TransformChannelData *)instanceData; |
| 1013 |
Tcl_Channel downChan = Tcl_GetStackedChannel(dataPtr->self); |
| 1014 |
Tcl_DriverSetOptionProc *setOptionProc; |
| 1015 |
|
| 1016 |
setOptionProc = Tcl_ChannelSetOptionProc(Tcl_GetChannelType(downChan)); |
| 1017 |
if (setOptionProc == NULL) { |
| 1018 |
return TCL_ERROR; |
| 1019 |
} |
| 1020 |
|
| 1021 |
return setOptionProc(Tcl_GetChannelInstanceData(downChan), interp, |
| 1022 |
optionName, value); |
| 1023 |
} |
| 1024 |
|
| 1025 |
/* |
| 1026 |
*---------------------------------------------------------------------- |
| 1027 |
* |
| 1028 |
* TransformGetOptionProc -- |
| 1029 |
* |
| 1030 |
* Called by generic layer to handle requests for the values of channel |
| 1031 |
* specific options. As this channel type does not have such, it simply |
| 1032 |
* passes all requests downstream. |
| 1033 |
* |
| 1034 |
* Side effects: |
| 1035 |
* As defined by the channel downstream. |
| 1036 |
* |
| 1037 |
* Result: |
| 1038 |
* A standard TCL error code. |
| 1039 |
* |
| 1040 |
*---------------------------------------------------------------------- |
| 1041 |
*/ |
| 1042 |
|
| 1043 |
static int |
| 1044 |
TransformGetOptionProc( |
| 1045 |
ClientData instanceData, |
| 1046 |
Tcl_Interp *interp, |
| 1047 |
const char *optionName, |
| 1048 |
Tcl_DString *dsPtr) |
| 1049 |
{ |
| 1050 |
TransformChannelData *dataPtr = (TransformChannelData *)instanceData; |
| 1051 |
Tcl_Channel downChan = Tcl_GetStackedChannel(dataPtr->self); |
| 1052 |
Tcl_DriverGetOptionProc *getOptionProc; |
| 1053 |
|
| 1054 |
getOptionProc = Tcl_ChannelGetOptionProc(Tcl_GetChannelType(downChan)); |
| 1055 |
if (getOptionProc != NULL) { |
| 1056 |
return getOptionProc(Tcl_GetChannelInstanceData(downChan), interp, |
| 1057 |
optionName, dsPtr); |
| 1058 |
} else if (optionName == NULL) { |
| 1059 |
/* |
| 1060 |
* Request is query for all options, this is ok. |
| 1061 |
*/ |
| 1062 |
|
| 1063 |
return TCL_OK; |
| 1064 |
} |
| 1065 |
|
| 1066 |
/* |
| 1067 |
* Request for a specific option has to fail, since we don't have any. |
| 1068 |
*/ |
| 1069 |
|
| 1070 |
return TCL_ERROR; |
| 1071 |
} |
| 1072 |
|
| 1073 |
/* |
| 1074 |
*---------------------------------------------------------------------- |
| 1075 |
* |
| 1076 |
* TransformWatchProc -- |
| 1077 |
* |
| 1078 |
* Initialize the notifier to watch for events from this channel. |
| 1079 |
* |
| 1080 |
* Side effects: |
| 1081 |
* Sets up the notifier so that a future event on the channel will be |
| 1082 |
* seen by Tcl. |
| 1083 |
* |
| 1084 |
* Result: |
| 1085 |
* None. |
| 1086 |
* |
| 1087 |
*---------------------------------------------------------------------- |
| 1088 |
*/ |
| 1089 |
|
| 1090 |
/* ARGSUSED */ |
| 1091 |
static void |
| 1092 |
TransformWatchProc( |
| 1093 |
ClientData instanceData, /* Channel to watch. */ |
| 1094 |
int mask) /* Events of interest. */ |
| 1095 |
{ |
| 1096 |
TransformChannelData *dataPtr = (TransformChannelData *)instanceData; |
| 1097 |
Tcl_Channel downChan; |
| 1098 |
|
| 1099 |
/* |
| 1100 |
* The caller expressed interest in events occurring for this channel. We |
| 1101 |
* are forwarding the call to the underlying channel now. |
| 1102 |
*/ |
| 1103 |
|
| 1104 |
dataPtr->watchMask = mask; |
| 1105 |
|
| 1106 |
/* |
| 1107 |
* No channel handlers any more. We will be notified automatically about |
| 1108 |
* events on the channel below via a call to our 'TransformNotifyProc'. |
| 1109 |
* But we have to pass the interest down now. We are allowed to add |
| 1110 |
* additional 'interest' to the mask if we want to. But this |
| 1111 |
* transformation has no such interest. It just passes the request down, |
| 1112 |
* unchanged. |
| 1113 |
*/ |
| 1114 |
|
| 1115 |
if (dataPtr->self == NULL) { |
| 1116 |
return; |
| 1117 |
} |
| 1118 |
downChan = Tcl_GetStackedChannel(dataPtr->self); |
| 1119 |
|
| 1120 |
Tcl_GetChannelType(downChan)->watchProc( |
| 1121 |
Tcl_GetChannelInstanceData(downChan), mask); |
| 1122 |
|
| 1123 |
/* |
| 1124 |
* Management of the internal timer. |
| 1125 |
*/ |
| 1126 |
|
| 1127 |
if ((dataPtr->timer != NULL) && |
| 1128 |
(!(mask & TCL_READABLE) || ResultEmpty(&dataPtr->result))) { |
| 1129 |
/* |
| 1130 |
* A pending timer exists, but either is there no (more) interest in |
| 1131 |
* the events it generates or nothing is available for reading, so |
| 1132 |
* remove it. |
| 1133 |
*/ |
| 1134 |
|
| 1135 |
Tcl_DeleteTimerHandler(dataPtr->timer); |
| 1136 |
dataPtr->timer = NULL; |
| 1137 |
} |
| 1138 |
|
| 1139 |
if ((dataPtr->timer == NULL) && (mask & TCL_READABLE) |
| 1140 |
&& !ResultEmpty(&dataPtr->result)) { |
| 1141 |
/* |
| 1142 |
* There is no pending timer, but there is interest in readable events |
| 1143 |
* and we actually have data waiting, so generate a timer to flush |
| 1144 |
* that. |
| 1145 |
*/ |
| 1146 |
|
| 1147 |
dataPtr->timer = Tcl_CreateTimerHandler(FLUSH_DELAY, |
| 1148 |
TransformChannelHandlerTimer, dataPtr); |
| 1149 |
} |
| 1150 |
} |
| 1151 |
|
| 1152 |
/* |
| 1153 |
*---------------------------------------------------------------------- |
| 1154 |
* |
| 1155 |
* TransformGetFileHandleProc -- |
| 1156 |
* |
| 1157 |
* Called from Tcl_GetChannelHandle to retrieve OS specific file handle |
| 1158 |
* from inside this channel. |
| 1159 |
* |
| 1160 |
* Side effects: |
| 1161 |
* None. |
| 1162 |
* |
| 1163 |
* Result: |
| 1164 |
* The appropriate Tcl_File or NULL if not present. |
| 1165 |
* |
| 1166 |
*---------------------------------------------------------------------- |
| 1167 |
*/ |
| 1168 |
|
| 1169 |
static int |
| 1170 |
TransformGetFileHandleProc( |
| 1171 |
ClientData instanceData, /* Channel to query. */ |
| 1172 |
int direction, /* Direction of interest. */ |
| 1173 |
ClientData *handlePtr) /* Place to store the handle into. */ |
| 1174 |
{ |
| 1175 |
TransformChannelData *dataPtr = (TransformChannelData *)instanceData; |
| 1176 |
|
| 1177 |
/* |
| 1178 |
* Return the handle belonging to parent channel. IOW, pass the request |
| 1179 |
* down and the result up. |
| 1180 |
*/ |
| 1181 |
|
| 1182 |
return Tcl_GetChannelHandle(Tcl_GetStackedChannel(dataPtr->self), |
| 1183 |
direction, handlePtr); |
| 1184 |
} |
| 1185 |
|
| 1186 |
/* |
| 1187 |
*---------------------------------------------------------------------- |
| 1188 |
* |
| 1189 |
* TransformNotifyProc -- |
| 1190 |
* |
| 1191 |
* Handler called by Tcl to inform us of activity on the underlying |
| 1192 |
* channel. |
| 1193 |
* |
| 1194 |
* Side effects: |
| 1195 |
* May process the incoming event by itself. |
| 1196 |
* |
| 1197 |
* Result: |
| 1198 |
* None. |
| 1199 |
* |
| 1200 |
*---------------------------------------------------------------------- |
| 1201 |
*/ |
| 1202 |
|
| 1203 |
static int |
| 1204 |
TransformNotifyProc( |
| 1205 |
ClientData clientData, /* The state of the notified |
| 1206 |
* transformation. */ |
| 1207 |
int mask) /* The mask of occurring events. */ |
| 1208 |
{ |
| 1209 |
TransformChannelData *dataPtr = (TransformChannelData *)clientData; |
| 1210 |
|
| 1211 |
/* |
| 1212 |
* An event occurred in the underlying channel. This transformation doesn't |
| 1213 |
* process such events thus returns the incoming mask unchanged. |
| 1214 |
*/ |
| 1215 |
|
| 1216 |
if (dataPtr->timer != NULL) { |
| 1217 |
/* |
| 1218 |
* Delete an existing timer. It was not fired, yet we are here, so the |
| 1219 |
* channel below generated such an event and we don't have to. The |
| 1220 |
* renewal of the interest after the execution of channel handlers |
| 1221 |
* will eventually cause us to recreate the timer (in |
| 1222 |
* TransformWatchProc). |
| 1223 |
*/ |
| 1224 |
|
| 1225 |
Tcl_DeleteTimerHandler(dataPtr->timer); |
| 1226 |
dataPtr->timer = NULL; |
| 1227 |
} |
| 1228 |
return mask; |
| 1229 |
} |
| 1230 |
|
| 1231 |
/* |
| 1232 |
*---------------------------------------------------------------------- |
| 1233 |
* |
| 1234 |
* TransformChannelHandlerTimer -- |
| 1235 |
* |
| 1236 |
* Called by the notifier (-> timer) to flush out information waiting in |
| 1237 |
* the input buffer. |
| 1238 |
* |
| 1239 |
* Side effects: |
| 1240 |
* As of 'Tcl_NotifyChannel'. |
| 1241 |
* |
| 1242 |
* Result: |
| 1243 |
* None. |
| 1244 |
* |
| 1245 |
*---------------------------------------------------------------------- |
| 1246 |
*/ |
| 1247 |
|
| 1248 |
static void |
| 1249 |
TransformChannelHandlerTimer( |
| 1250 |
ClientData clientData) /* Transformation to query. */ |
| 1251 |
{ |
| 1252 |
TransformChannelData *dataPtr = (TransformChannelData *)clientData; |
| 1253 |
|
| 1254 |
dataPtr->timer = NULL; |
| 1255 |
if (!(dataPtr->watchMask&TCL_READABLE) || ResultEmpty(&dataPtr->result)) { |
| 1256 |
/* |
| 1257 |
* The timer fired, but either is there no (more) interest in the |
| 1258 |
* events it generates or nothing is available for reading, so ignore |
| 1259 |
* it and don't recreate it. |
| 1260 |
*/ |
| 1261 |
|
| 1262 |
return; |
| 1263 |
} |
| 1264 |
Tcl_NotifyChannel(dataPtr->self, TCL_READABLE); |
| 1265 |
} |
| 1266 |
|
| 1267 |
/* |
| 1268 |
*---------------------------------------------------------------------- |
| 1269 |
* |
| 1270 |
* ResultClear -- |
| 1271 |
* |
| 1272 |
* Deallocates any memory allocated by 'ResultAdd'. |
| 1273 |
* |
| 1274 |
* Side effects: |
| 1275 |
* See above. |
| 1276 |
* |
| 1277 |
* Result: |
| 1278 |
* None. |
| 1279 |
* |
| 1280 |
*---------------------------------------------------------------------- |
| 1281 |
*/ |
| 1282 |
|
| 1283 |
static inline void |
| 1284 |
ResultClear( |
| 1285 |
ResultBuffer *r) /* Reference to the buffer to clear out. */ |
| 1286 |
{ |
| 1287 |
r->used = 0; |
| 1288 |
|
| 1289 |
if (r->allocated) { |
| 1290 |
ckfree(r->buf); |
| 1291 |
r->buf = NULL; |
| 1292 |
r->allocated = 0; |
| 1293 |
} |
| 1294 |
} |
| 1295 |
|
| 1296 |
/* |
| 1297 |
*---------------------------------------------------------------------- |
| 1298 |
* |
| 1299 |
* ResultInit -- |
| 1300 |
* |
| 1301 |
* Initializes the specified buffer structure. The structure will contain |
| 1302 |
* valid information for an empty buffer. |
| 1303 |
* |
| 1304 |
* Side effects: |
| 1305 |
* See above. |
| 1306 |
* |
| 1307 |
* Result: |
| 1308 |
* None. |
| 1309 |
* |
| 1310 |
*---------------------------------------------------------------------- |
| 1311 |
*/ |
| 1312 |
|
| 1313 |
static inline void |
| 1314 |
ResultInit( |
| 1315 |
ResultBuffer *r) /* Reference to the structure to |
| 1316 |
* initialize. */ |
| 1317 |
{ |
| 1318 |
r->used = 0; |
| 1319 |
r->allocated = 0; |
| 1320 |
r->buf = NULL; |
| 1321 |
} |
| 1322 |
|
| 1323 |
/* |
| 1324 |
*---------------------------------------------------------------------- |
| 1325 |
* |
| 1326 |
* ResultEmpty -- |
| 1327 |
* |
| 1328 |
* Returns whether the number of bytes stored in the buffer is zero. |
| 1329 |
* |
| 1330 |
* Side effects: |
| 1331 |
* None. |
| 1332 |
* |
| 1333 |
* Result: |
| 1334 |
* A boolean. |
| 1335 |
* |
| 1336 |
*---------------------------------------------------------------------- |
| 1337 |
*/ |
| 1338 |
|
| 1339 |
static inline int |
| 1340 |
ResultEmpty( |
| 1341 |
ResultBuffer *r) /* The structure to query. */ |
| 1342 |
{ |
| 1343 |
return r->used == 0; |
| 1344 |
} |
| 1345 |
|
| 1346 |
/* |
| 1347 |
*---------------------------------------------------------------------- |
| 1348 |
* |
| 1349 |
* ResultCopy -- |
| 1350 |
* |
| 1351 |
* Copies the requested number of bytes from the buffer into the |
| 1352 |
* specified array and removes them from the buffer afterward. Copies |
| 1353 |
* less if there is not enough data in the buffer. |
| 1354 |
* |
| 1355 |
* Side effects: |
| 1356 |
* See above. |
| 1357 |
* |
| 1358 |
* Result: |
| 1359 |
* The number of actually copied bytes, possibly less than 'toRead'. |
| 1360 |
* |
| 1361 |
*---------------------------------------------------------------------- |
| 1362 |
*/ |
| 1363 |
|
| 1364 |
static inline size_t |
| 1365 |
ResultCopy( |
| 1366 |
ResultBuffer *r, /* The buffer to read from. */ |
| 1367 |
unsigned char *buf, /* The buffer to copy into. */ |
| 1368 |
size_t toRead) /* Number of requested bytes. */ |
| 1369 |
{ |
| 1370 |
if (ResultEmpty(r)) { |
| 1371 |
/* |
| 1372 |
* Nothing to copy in the case of an empty buffer. |
| 1373 |
*/ |
| 1374 |
|
| 1375 |
return 0; |
| 1376 |
} else if (r->used == toRead) { |
| 1377 |
/* |
| 1378 |
* We have just enough. Copy everything to the caller. |
| 1379 |
*/ |
| 1380 |
|
| 1381 |
memcpy(buf, r->buf, toRead); |
| 1382 |
r->used = 0; |
| 1383 |
} else if (r->used > toRead) { |
| 1384 |
/* |
| 1385 |
* The internal buffer contains more than requested. Copy the |
| 1386 |
* requested subset to the caller, and shift the remaining bytes down. |
| 1387 |
*/ |
| 1388 |
|
| 1389 |
memcpy(buf, r->buf, toRead); |
| 1390 |
memmove(r->buf, r->buf + toRead, r->used - toRead); |
| 1391 |
r->used -= toRead; |
| 1392 |
} else { |
| 1393 |
/* |
| 1394 |
* There is not enough in the buffer to satisfy the caller, so take |
| 1395 |
* everything. |
| 1396 |
*/ |
| 1397 |
|
| 1398 |
memcpy(buf, r->buf, r->used); |
| 1399 |
toRead = r->used; |
| 1400 |
r->used = 0; |
| 1401 |
} |
| 1402 |
return toRead; |
| 1403 |
} |
| 1404 |
|
| 1405 |
/* |
| 1406 |
*---------------------------------------------------------------------- |
| 1407 |
* |
| 1408 |
* ResultAdd -- |
| 1409 |
* |
| 1410 |
* Adds the bytes in the specified array to the buffer, by appending it. |
| 1411 |
* |
| 1412 |
* Side effects: |
| 1413 |
* See above. |
| 1414 |
* |
| 1415 |
* Result: |
| 1416 |
* None. |
| 1417 |
* |
| 1418 |
*---------------------------------------------------------------------- |
| 1419 |
*/ |
| 1420 |
|
| 1421 |
static inline void |
| 1422 |
ResultAdd( |
| 1423 |
ResultBuffer *r, /* The buffer to extend. */ |
| 1424 |
unsigned char *buf, /* The buffer to read from. */ |
| 1425 |
size_t toWrite) /* The number of bytes in 'buf'. */ |
| 1426 |
{ |
| 1427 |
if ((r->used + toWrite + 1) > r->allocated) { |
| 1428 |
/* |
| 1429 |
* Extension of the internal buffer is required. |
| 1430 |
*/ |
| 1431 |
|
| 1432 |
if (r->allocated == 0) { |
| 1433 |
r->allocated = toWrite + INCREMENT; |
| 1434 |
r->buf = (unsigned char *)ckalloc(r->allocated); |
| 1435 |
} else { |
| 1436 |
r->allocated += toWrite + INCREMENT; |
| 1437 |
r->buf = (unsigned char *)ckrealloc(r->buf, r->allocated); |
| 1438 |
} |
| 1439 |
} |
| 1440 |
|
| 1441 |
/* |
| 1442 |
* Now we may copy the data. |
| 1443 |
*/ |
| 1444 |
|
| 1445 |
memcpy(r->buf + r->used, buf, toWrite); |
| 1446 |
r->used += toWrite; |
| 1447 |
} |
| 1448 |
|
| 1449 |
/* |
| 1450 |
* Local Variables: |
| 1451 |
* mode: c |
| 1452 |
* c-basic-offset: 4 |
| 1453 |
* fill-column: 78 |
| 1454 |
* End: |
| 1455 |
*/ |