Diff
Not logged in

Differences From Artifact [d896413a00]:

To Artifact [a9c7167748]:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* 
 * tclWinConsole.c --
 *
 *	This file implements the Windows-specific console functions, and the
 *	"console" channel driver.
 *
 * Copyright (c) 1999 by Scriptics Corp.
 *
 * See the file "license.terms" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * RCS: @(#) $Id: tclWinConsole.c,v 1.15 2005/07/24 22:56:47 dkf Exp $
 */

#include "tclWinInt.h"

#include <fcntl.h>
#include <io.h>
#include <sys/stat.h>











|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* 
 * tclWinConsole.c --
 *
 *	This file implements the Windows-specific console functions, and the
 *	"console" channel driver.
 *
 * Copyright (c) 1999 by Scriptics Corp.
 *
 * See the file "license.terms" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * RCS: @(#) $Id: tclWinConsole.c,v 1.16 2005/11/03 01:16:07 patthoyts Exp $
 */

#include "tclWinInt.h"

#include <fcntl.h>
#include <io.h>
#include <sys/stat.h>
49
50
51
52
53
54
55

56
57
58
59
60
61
62
				   * thread. */

#define CONSOLE_BUFFER_SIZE (8*1024)

/*
 * This structure describes per-instance data for a console based channel.
 */


typedef struct ConsoleInfo {
    HANDLE handle;
    int type;
    struct ConsoleInfo *nextPtr;/* Pointer to next registered console. */
    Tcl_Channel channel;	/* Pointer to channel structure. */
    int validMask;		/* OR'ed combination of TCL_READABLE,







>







49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
				   * thread. */

#define CONSOLE_BUFFER_SIZE (8*1024)

/*
 * This structure describes per-instance data for a console based channel.
 */


typedef struct ConsoleInfo {
    HANDLE handle;
    int type;
    struct ConsoleInfo *nextPtr;/* Pointer to next registered console. */
    Tcl_Channel channel;	/* Pointer to channel structure. */
    int validMask;		/* OR'ed combination of TCL_READABLE,
180
181
182
183
184
185
186







































187
188
189
190
191
192
193
    NULL,			/* close2proc. */
    ConsoleBlockModeProc,	/* Set blocking or non-blocking mode.*/
    NULL,			/* flush proc. */
    NULL,			/* handler proc. */
    NULL,			/* wide seek proc */
    ConsoleThreadActionProc,    /* thread action proc */
};








































/*
 *----------------------------------------------------------------------
 *
 * ConsoleInit --
 *
 *	This function initializes the static variables for this file.







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
    NULL,			/* close2proc. */
    ConsoleBlockModeProc,	/* Set blocking or non-blocking mode.*/
    NULL,			/* flush proc. */
    NULL,			/* handler proc. */
    NULL,			/* wide seek proc */
    ConsoleThreadActionProc,    /* thread action proc */
};

/*
 *----------------------------------------------------------------------
 * 
 * readConsoleBytes, writeConsoleBytes --
 * Wrapper for ReadConsole{A,W}, that takes and returns number of bytes
 * instead of number of TCHARS
 */
static BOOL readConsoleBytes(HANDLE hConsole,
  LPVOID lpBuffer,
  DWORD nbytes,
  LPDWORD nbytesread)
{
    DWORD ntchars;
    BOOL result;
    int tcharsize;
    tcharsize = tclWinProcs->useWide? 2 : 1;
    result = tclWinProcs->readConsoleProc(
	    hConsole, lpBuffer, nbytes / tcharsize, &ntchars, NULL);
    if (nbytesread) 
	*nbytesread = (ntchars*tcharsize);
    return result;
}

static BOOL writeConsoleBytes(HANDLE hConsole,
  const VOID *lpBuffer,
  DWORD nbytes,
  LPDWORD nbyteswritten)
{
    DWORD ntchars;
    BOOL result;
    int tcharsize;
    tcharsize = tclWinProcs->useWide? 2 : 1;
    result = tclWinProcs->writeConsoleProc(
	    hConsole, lpBuffer, nbytes / tcharsize, &ntchars, NULL);
    if (nbyteswritten) 
	*nbyteswritten = (ntchars*tcharsize);
    return result;
}

/*
 *----------------------------------------------------------------------
 *
 * ConsoleInit --
 *
 *	This function initializes the static variables for this file.
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
    
    /*
     * Attempt to read bufSize bytes. The read will return immediately if
     * there is any data available. Otherwise it will block until at least one
     * byte is available or an EOF occurs.
     */

    if (ReadConsole(infoPtr->handle, (LPVOID) buf, (DWORD) bufSize, &count,
	    (LPOVERLAPPED) NULL) == TRUE) {
	buf[count] = '\0';
	return count;
    }

    return -1;
}








|
|







737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
    
    /*
     * Attempt to read bufSize bytes. The read will return immediately if
     * there is any data available. Otherwise it will block until at least one
     * byte is available or an EOF occurs.
     */

    if (readConsoleBytes(infoPtr->handle, (LPVOID) buf, (DWORD) bufSize, &count) 
	    == TRUE) {
	buf[count] = '\0';
	return count;
    }

    return -1;
}

784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
	bytesWritten = toWrite;
    } else {
	/*
	 * In the blocking case, just try to write the buffer directly. This
	 * avoids an unnecessary copy.
	 */

	if (WriteConsole(infoPtr->handle, buf, toWrite, &bytesWritten,
		NULL) == FALSE) {
	    TclWinConvertError(GetLastError());
	    goto error;
	}
    }
    return bytesWritten;

  error:







|
|







824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
	bytesWritten = toWrite;
    } else {
	/*
	 * In the blocking case, just try to write the buffer directly. This
	 * avoids an unnecessary copy.
	 */

	if (writeConsoleBytes(infoPtr->handle, buf, toWrite, &bytesWritten)
		== FALSE) {
	    TclWinConvertError(GetLastError());
	    goto error;
	}
    }
    return bytesWritten;

  error:
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
	count = 0;

	/* 
	 * Look for data on the console, but first ignore any events that are
	 * not KEY_EVENTs.
	 */

	if (ReadConsoleA(handle, infoPtr->buffer, CONSOLE_BUFFER_SIZE,
		(LPDWORD) &infoPtr->bytesRead, NULL) != FALSE) {
	    /*
	     * Data was stored in the buffer.
	     */
	    
	    infoPtr->readFlags |= CONSOLE_BUFFERED;
	} else {
	    DWORD err;







|
|







1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
	count = 0;

	/* 
	 * Look for data on the console, but first ignore any events that are
	 * not KEY_EVENTs.
	 */

	if (readConsoleBytes(handle, infoPtr->buffer, CONSOLE_BUFFER_SIZE,
		(LPDWORD) &infoPtr->bytesRead) != FALSE) {
	    /*
	     * Data was stored in the buffer.
	     */
	    
	    infoPtr->readFlags |= CONSOLE_BUFFERED;
	} else {
	    DWORD err;
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
	toWrite = infoPtr->toWrite;

	/*
	 * Loop until all of the bytes are written or an error occurs.
	 */

	while (toWrite > 0) {
	    if (WriteConsole(handle, buf, toWrite, &count, NULL) == FALSE) {
		infoPtr->writeError = GetLastError();
		break;
	    } else {
		toWrite -= count;
		buf += count;
	    }
	}







|







1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
	toWrite = infoPtr->toWrite;

	/*
	 * Loop until all of the bytes are written or an error occurs.
	 */

	while (toWrite > 0) {
	    if (writeConsoleBytes(handle, buf, toWrite, &count) == FALSE) {
		infoPtr->writeError = GetLastError();
		break;
	    } else {
		toWrite -= count;
		buf += count;
	    }
	}
1359
1360
1361
1362
1363
1364
1365



1366
1367
1368
1369
1370
1371
1372
1373
    /*
     * Files have default translation of AUTO and ^Z eof char, which means
     * that a ^Z will be accepted as EOF when reading.
     */
    
    Tcl_SetChannelOption(NULL, infoPtr->channel, "-translation", "auto");
    Tcl_SetChannelOption(NULL, infoPtr->channel, "-eofchar", "\032 {}");



    Tcl_SetChannelOption(NULL, infoPtr->channel, "-encoding", encoding);

    return infoPtr->channel;
}

/*
 *----------------------------------------------------------------------
 *







>
>
>
|







1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
    /*
     * Files have default translation of AUTO and ^Z eof char, which means
     * that a ^Z will be accepted as EOF when reading.
     */
    
    Tcl_SetChannelOption(NULL, infoPtr->channel, "-translation", "auto");
    Tcl_SetChannelOption(NULL, infoPtr->channel, "-eofchar", "\032 {}");
    if (tclWinProcs->useWide)
	Tcl_SetChannelOption(NULL, infoPtr->channel, "-encoding", "unicode");
    else
	Tcl_SetChannelOption(NULL, infoPtr->channel, "-encoding", encoding);

    return infoPtr->channel;
}

/*
 *----------------------------------------------------------------------
 *