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