| ︙ | | | ︙ | |
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
#endif
/*
* Ring buffer for storing data. Actual data is from bufPtr[start]:bufPtr[size-1]
* and bufPtr[0]:bufPtr[length - (size-start)].
*/
typedef struct RingBuffer {
char *bufPtr; /* Pointer to buffer storage */
Tcl_Size capacity; /* Size of the buffer in RingBufferChar */
Tcl_Size start; /* Start of the data within the buffer. */
Tcl_Size length; /* Number of RingBufferChar*/
} RingBuffer;
#define RingBufferLength(ringPtr_) ((ringPtr_)->length)
#define RingBufferHasFreeSpace(ringPtr_) ((ringPtr_)->length < (ringPtr_)->capacity)
#define RINGBUFFER_ASSERT(ringPtr_) assert(RingBufferCheck(ringPtr_))
/*
* The Win32 console API does not support non-blocking I/O in any form. Thus
|
|
|
|
|
|
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
#endif
/*
* Ring buffer for storing data. Actual data is from bufPtr[start]:bufPtr[size-1]
* and bufPtr[0]:bufPtr[length - (size-start)].
*/
typedef struct RingBuffer {
char *bufPtr; /* Pointer to buffer storage */
Tcl_Size capacity; /* Size of the buffer in RingBufferChar */
Tcl_Size start; /* Start of the data within the buffer. */
Tcl_Size length; /* Number of RingBufferChar*/
} RingBuffer;
#define RingBufferLength(ringPtr_) ((ringPtr_)->length)
#define RingBufferHasFreeSpace(ringPtr_) ((ringPtr_)->length < (ringPtr_)->capacity)
#define RINGBUFFER_ASSERT(ringPtr_) assert(RingBufferCheck(ringPtr_))
/*
* The Win32 console API does not support non-blocking I/O in any form. Thus
|
| ︙ | | | ︙ | |
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
* Note on reference counting - a ConsoleHandleInfo instance has multiple
* references to it - one each from every channel that is attached to it
* plus one from the console thread itself which also serves as the reference
* from gConsoleHandleInfoList.
*/
typedef struct ConsoleHandleInfo {
struct ConsoleHandleInfo *nextPtr; /* Process-global list of consoles */
HANDLE console; /* Console handle */
HANDLE consoleThread; /* Handle to thread doing actual i/o on the console */
SRWLOCK lock; /* Controls access to this structure.
* Cheaper than CRITICAL_SECTION but note does not
* support recursive locks or Try* style attempts.*/
CONDITION_VARIABLE consoleThreadCV;/* For awakening console thread */
CONDITION_VARIABLE interpThreadCV; /* For awakening interpthread(s) */
RingBuffer buffer; /* Buffer for data transferred between console
* threads and Tcl threads. For input consoles,
* written by the console thread and read by Tcl
* threads. The converse for output threads */
DWORD initMode; /* Initial console mode. */
DWORD lastError; /* An error caused by the last background
* operation. Set to 0 if no error has been
* detected. */
int numRefs; /* See comments above */
int permissions; /* TCL_READABLE for input consoles, TCL_WRITABLE
* for output. Only one or the other can be set. */
int flags;
#define CONSOLE_DATA_AWAITED 0x0001 /* An interpreter is awaiting data */
} ConsoleHandleInfo;
/*
* This structure describes per-instance data for a console based channel.
*
* Note on locking - this structure has no locks because it is accessed
* only from the thread owning channel EXCEPT when a console traverses it
* looking for a channel that is watching for events on the console. Even
|
|
|
>
|
|
|
>
|
|
|
|
|
|
|
|
|
|
|
>
|
>
|
|
>
>
|
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
* Note on reference counting - a ConsoleHandleInfo instance has multiple
* references to it - one each from every channel that is attached to it
* plus one from the console thread itself which also serves as the reference
* from gConsoleHandleInfoList.
*/
typedef struct ConsoleHandleInfo {
struct ConsoleHandleInfo *nextPtr; /* Process-global list of consoles */
HANDLE console; /* Console handle */
HANDLE consoleThread; /* Handle to thread doing actual I/O on the
* console */
SRWLOCK lock; /* Controls access to this structure. Cheaper
* than CRITICAL_SECTION but note does not
* support recursive locks or Try* style
* attempts. */
CONDITION_VARIABLE consoleThreadCV;/* For awakening console thread */
CONDITION_VARIABLE interpThreadCV; /* For awakening interpthread(s) */
RingBuffer buffer; /* Buffer for data transferred between console
* threads and Tcl threads. For input consoles,
* written by the console thread and read by Tcl
* threads. The converse for output threads */
DWORD initMode; /* Initial console mode. */
DWORD lastError; /* An error caused by the last background
* operation. Set to 0 if no error has been
* detected. */
int numRefs; /* See comments above */
int permissions; /* TCL_READABLE for input consoles, TCL_WRITABLE
* for output. Only one or the other can be
* set. */
int flags; /* State flags */
} ConsoleHandleInfo;
enum ConsoleHandleInfoFlags {
CONSOLE_DATA_AWAITED = 1 /* An interpreter is awaiting data */
};
/*
* This structure describes per-instance data for a console based channel.
*
* Note on locking - this structure has no locks because it is accessed
* only from the thread owning channel EXCEPT when a console traverses it
* looking for a channel that is watching for events on the console. Even
|
| ︙ | | | ︙ | |
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
int permissions; /* OR'ed combination of TCL_READABLE,
* TCL_WRITABLE, or TCL_EXCEPTION: indicates
* which operations are valid on the file. */
int watchMask; /* OR'ed combination of TCL_READABLE,
* TCL_WRITABLE, or TCL_EXCEPTION: indicates
* which events should be reported. */
int flags; /* State flags */
#define CONSOLE_EVENT_QUEUED 0x0001 /* Notification event already queued */
#define CONSOLE_ASYNC 0x0002 /* Channel is non-blocking. */
#define CONSOLE_READ_OPS 0x0004 /* Channel supports read-related ops. */
} ConsoleChannelInfo;
/*
* The following structure is what is added to the Tcl event queue when
* console events are generated.
*/
typedef struct {
|
>
>
>
|
|
|
<
>
|
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
int permissions; /* OR'ed combination of TCL_READABLE,
* TCL_WRITABLE, or TCL_EXCEPTION: indicates
* which operations are valid on the file. */
int watchMask; /* OR'ed combination of TCL_READABLE,
* TCL_WRITABLE, or TCL_EXCEPTION: indicates
* which events should be reported. */
int flags; /* State flags */
} ConsoleChannelInfo;
enum ConsoleChannelInfoFlags {
CONSOLE_EVENT_QUEUED = 1, /* Notification event already queued */
CONSOLE_ASYNC = 2, /* Channel is non-blocking. */
CONSOLE_READ_OPS = 4 /* Channel supports read-related ops. */
};
/*
* The following structure is what is added to the Tcl event queue when
* console events are generated.
*/
typedef struct {
|
| ︙ | | | ︙ | |
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
};
/*
*------------------------------------------------------------------------
*
* RingBufferInit --
*
* Initializes the ring buffer to a given size.
*
* Results:
* None.
*
* Side effects:
* Panics on allocation failure.
*
*------------------------------------------------------------------------
*/
static void
RingBufferInit(
RingBuffer *ringPtr,
Tcl_Size capacity)
|
|
|
|
|
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
};
/*
*------------------------------------------------------------------------
*
* RingBufferInit --
*
* Initializes the ring buffer to a given size.
*
* Results:
* None.
*
* Side effects:
* Panics on allocation failure.
*
*------------------------------------------------------------------------
*/
static void
RingBufferInit(
RingBuffer *ringPtr,
Tcl_Size capacity)
|
| ︙ | | | ︙ | |
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
}
/*
*------------------------------------------------------------------------
*
* RingBufferClear
*
* Clears the contents of a ring buffer.
*
* Results:
* None.
*
* Side effects:
* The allocated internal buffer is freed.
*
*------------------------------------------------------------------------
*/
static void
RingBufferClear(
RingBuffer *ringPtr)
{
|
|
|
|
|
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
}
/*
*------------------------------------------------------------------------
*
* RingBufferClear
*
* Clears the contents of a ring buffer.
*
* Results:
* None.
*
* Side effects:
* The allocated internal buffer is freed.
*
*------------------------------------------------------------------------
*/
static void
RingBufferClear(
RingBuffer *ringPtr)
{
|
| ︙ | | | ︙ | |
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
}
/*
*------------------------------------------------------------------------
*
* RingBufferIn --
*
* Appends data to the ring buffer.
*
* Results:
* Returns number of bytes copied.
*
* Side effects:
* Internal buffer is updated.
*
*------------------------------------------------------------------------
*/
static Tcl_Size
RingBufferIn(
RingBuffer *ringPtr,
const char *srcPtr, /* Source to be copied */
|
|
|
|
|
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
}
/*
*------------------------------------------------------------------------
*
* RingBufferIn --
*
* Appends data to the ring buffer.
*
* Results:
* Returns number of bytes copied.
*
* Side effects:
* Internal buffer is updated.
*
*------------------------------------------------------------------------
*/
static Tcl_Size
RingBufferIn(
RingBuffer *ringPtr,
const char *srcPtr, /* Source to be copied */
|
| ︙ | | | ︙ | |
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
|
/* srcLen > endSpace */
memmove(endSpaceStart + ringPtr->bufPtr, srcPtr, endSpace);
memmove(ringPtr->bufPtr, endSpace + srcPtr, srcLen - endSpace);
}
} else {
/* No room at the back. Existing data wrap to front. */
Tcl_Size wrapLen =
ringPtr->start + ringPtr->length - ringPtr->capacity;
memmove(wrapLen + ringPtr->bufPtr, srcPtr, srcLen);
}
ringPtr->length += srcLen;
RINGBUFFER_ASSERT(ringPtr);
return srcLen;
}
/*
*------------------------------------------------------------------------
*
* RingBufferOut --
*
* Moves data out of the ring buffer. If dstPtr is NULL, the data
* is simply removed.
*
* Results:
* Returns number of bytes copied or removed.
*
* Side effects:
* Internal buffer is updated.
*
*------------------------------------------------------------------------
*/
static Tcl_Size
RingBufferOut(
RingBuffer *ringPtr,
char *dstPtr, /* Buffer for output data. May be NULL */
|
|
|
|
|
|
|
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
/* srcLen > endSpace */
memmove(endSpaceStart + ringPtr->bufPtr, srcPtr, endSpace);
memmove(ringPtr->bufPtr, endSpace + srcPtr, srcLen - endSpace);
}
} else {
/* No room at the back. Existing data wrap to front. */
Tcl_Size wrapLen =
ringPtr->start + ringPtr->length - ringPtr->capacity;
memmove(wrapLen + ringPtr->bufPtr, srcPtr, srcLen);
}
ringPtr->length += srcLen;
RINGBUFFER_ASSERT(ringPtr);
return srcLen;
}
/*
*------------------------------------------------------------------------
*
* RingBufferOut --
*
* Moves data out of the ring buffer. If dstPtr is NULL, the data
* is simply removed.
*
* Results:
* Returns number of bytes copied or removed.
*
* Side effects:
* Internal buffer is updated.
*
*------------------------------------------------------------------------
*/
static Tcl_Size
RingBufferOut(
RingBuffer *ringPtr,
char *dstPtr, /* Buffer for output data. May be NULL */
|
| ︙ | | | ︙ | |
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
|
ringPtr->start += dstCapacity;
} else {
Tcl_Size wrapLen = dstCapacity - leadLen;
if (dstPtr) {
memmove(dstPtr,
ringPtr->start + ringPtr->bufPtr,
leadLen);
memmove(
leadLen + dstPtr, ringPtr->bufPtr, wrapLen);
}
ringPtr->start = wrapLen;
}
ringPtr->length -= dstCapacity;
if (ringPtr->start == ringPtr->capacity || ringPtr->length == 0) {
ringPtr->start = 0;
|
<
|
|
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
|
ringPtr->start += dstCapacity;
} else {
Tcl_Size wrapLen = dstCapacity - leadLen;
if (dstPtr) {
memmove(dstPtr,
ringPtr->start + ringPtr->bufPtr,
leadLen);
memmove(leadLen + dstPtr, ringPtr->bufPtr, wrapLen);
}
ringPtr->start = wrapLen;
}
ringPtr->length -= dstCapacity;
if (ringPtr->start == ringPtr->capacity || ringPtr->length == 0) {
ringPtr->start = 0;
|
| ︙ | | | ︙ | |
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
|
#endif
/*
*------------------------------------------------------------------------
*
* ReadConsoleChars --
*
* Wrapper for ReadConsoleW.
*
* Results:
* Returns 0 on success, else Windows error code.
*
* Side effects:
* On success the number of characters (not bytes) read is stored in
* *nCharsReadPtr. This will be 0 if the operation was interrupted by
* a Ctrl-C or a CancelIo call.
*
*------------------------------------------------------------------------
*/
static DWORD
ReadConsoleChars(
HANDLE hConsole,
WCHAR *lpBuffer,
Tcl_Size nChars,
Tcl_Size *nCharsReadPtr)
{
DWORD nRead;
BOOL result;
/*
* If user types a Ctrl-Break or Ctrl-C, ReadConsole will return success
* with ntchars == 0 and GetLastError() will be ERROR_OPERATION_ABORTED.
* If no Ctrl signal handlers have been established, the default signal
* OS handler in a separate thread will terminate the program. If a Ctrl
* signal handler has been established (through an extension for
* example), it will run and take whatever action it deems appropriate.
*
* If one thread closes its channel, it calls CancelSynchronousIo on the
* console handle which results again in success being returned and
* GetLastError() being ERROR_OPERATION_ABORTED but ntchars in
* unmodified.
*
* In both cases above we will return success but with nbytesread as 0.
* This allows caller to check for thread termination etc.
*
* See https://bugs.python.org/issue30237
* or https://github.com/microsoft/terminal/issues/12143
*/
nRead = (DWORD)-1;
result = ReadConsoleW(hConsole, lpBuffer, nChars, &nRead, NULL);
if (result) {
if ((nRead == 0 || nRead == (DWORD)-1)
&& GetLastError() == ERROR_OPERATION_ABORTED) {
nRead = 0;
}
*nCharsReadPtr = nRead;
return 0;
} else {
return GetLastError();
}
}
/*
*------------------------------------------------------------------------
*
* WriteConsoleChars --
*
* Wrapper for WriteConsoleW.
*
* Results:
* Returns 0 on success, Windows error code on failure.
*
* Side effects:
* On success the number of characters (not bytes) written is stored in
* *nCharsWrittenPtr. This will be 0 if the operation was interrupted by
* a Ctrl-C or a CancelIo call.
*
*------------------------------------------------------------------------
*/
static DWORD
WriteConsoleChars(
HANDLE hConsole,
const WCHAR *lpBuffer,
Tcl_Size nChars,
Tcl_Size *nCharsWrittenPtr)
{
DWORD nCharsWritten;
BOOL result;
/* See comments in ReadConsoleChars, not sure that applies here */
nCharsWritten = (DWORD)-1;
result = WriteConsoleW(hConsole, lpBuffer, nChars, &nCharsWritten, NULL);
if (result) {
if (nCharsWritten == (DWORD) -1) {
nCharsWritten = 0;
}
*nCharsWrittenPtr = nCharsWritten;
return 0;
} else {
return GetLastError();
}
}
/*
*----------------------------------------------------------------------
*
* ConsoleInit --
*
|
|
|
|
|
|
<
|
|
|
>
|
|
|
|
|
|
<
<
<
|
|
|
|
|
<
|
|
|
>
|
|
|
|
|
<
<
<
|
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
|
#endif
/*
*------------------------------------------------------------------------
*
* ReadConsoleChars --
*
* Wrapper for ReadConsoleW.
*
* Results:
* Returns 0 on success, else Windows error code.
*
* Side effects:
* On success the number of characters (not bytes) read is stored in
* *nCharsReadPtr. This will be 0 if the operation was interrupted by
* a Ctrl-C or a CancelIo call.
*
*------------------------------------------------------------------------
*/
static DWORD
ReadConsoleChars(
HANDLE hConsole,
WCHAR *lpBuffer,
Tcl_Size nChars,
Tcl_Size *nCharsReadPtr)
{
DWORD nRead;
/*
* If user types a Ctrl-Break or Ctrl-C, ReadConsole will return success
* with ntchars == 0 and GetLastError() will be ERROR_OPERATION_ABORTED.
* If no Ctrl signal handlers have been established, the default signal
* OS handler in a separate thread will terminate the program. If a Ctrl
* signal handler has been established (through an extension for
* example), it will run and take whatever action it deems appropriate.
*
* If one thread closes its channel, it calls CancelSynchronousIo on the
* console handle which results again in success being returned and
* GetLastError() being ERROR_OPERATION_ABORTED but ntchars in
* unmodified.
*
* In both cases above we will return success but with nbytesread as 0.
* This allows caller to check for thread termination etc.
*
* See https://bugs.python.org/issue30237
* or https://github.com/microsoft/terminal/issues/12143
*/
nRead = (DWORD) -1;
if (!ReadConsoleW(hConsole, lpBuffer, nChars, &nRead, NULL)) {
return GetLastError();
}
if ((nRead == 0 || nRead == (DWORD) -1)
&& GetLastError() == ERROR_OPERATION_ABORTED) {
nRead = 0;
}
*nCharsReadPtr = nRead;
return 0;
}
/*
*------------------------------------------------------------------------
*
* WriteConsoleChars --
*
* Wrapper for WriteConsoleW.
*
* Results:
* Returns 0 on success, Windows error code on failure.
*
* Side effects:
* On success the number of characters (not bytes) written is stored in
* *nCharsWrittenPtr. This will be 0 if the operation was interrupted by
* a Ctrl-C or a CancelIo call.
*
*------------------------------------------------------------------------
*/
static DWORD
WriteConsoleChars(
HANDLE hConsole,
const WCHAR *lpBuffer,
Tcl_Size nChars,
Tcl_Size *nCharsWrittenPtr)
{
DWORD nCharsWritten;
/* See comments in ReadConsoleChars, not sure that applies here */
nCharsWritten = (DWORD) -1;
if (!WriteConsoleW(hConsole, lpBuffer, nChars, &nCharsWritten, NULL)) {
return GetLastError();
}
if (nCharsWritten == (DWORD) -1) {
nCharsWritten = 0;
}
*nCharsWrittenPtr = nCharsWritten;
return 0;
}
/*
*----------------------------------------------------------------------
*
* ConsoleInit --
*
|
| ︙ | | | ︙ | |
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
|
}
/*
*------------------------------------------------------------------------
*
* NudgeWatchers --
*
* Wakes up all threads which have file event watchers on the passed
* console handle.
*
* The function locks and releases gConsoleLock.
* Caller must not be holding locks that will violate lock hierarchy.
*
* Results:
* None.
*
* Side effects:
* As above.
*------------------------------------------------------------------------
*/
static void
NudgeWatchers(
HANDLE consoleHandle)
{
ConsoleChannelInfo *chanInfoPtr;
|
|
|
|
|
|
|
>
|
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
|
}
/*
*------------------------------------------------------------------------
*
* NudgeWatchers --
*
* Wakes up all threads which have file event watchers on the passed
* console handle.
*
* The function locks and releases gConsoleLock.
* Caller must not be holding locks that will violate lock hierarchy.
*
* Results:
* None.
*
* Side effects:
* As above.
*
*------------------------------------------------------------------------
*/
static void
NudgeWatchers(
HANDLE consoleHandle)
{
ConsoleChannelInfo *chanInfoPtr;
|
| ︙ | | | ︙ | |
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
|
* Walk the list of channels. See general comments for struct
* ConsoleChannelInfo with regard to locking and field access.
*/
AcquireSRWLockShared(&gConsoleLock); /* READ lock - no data modification */
for (chanInfoPtr = gWatchingChannelList; block && chanInfoPtr != NULL;
chanInfoPtr = chanInfoPtr->nextWatchingChannelPtr) {
ConsoleHandleInfo *handleInfoPtr;
handleInfoPtr = FindConsoleInfo(chanInfoPtr);
if (handleInfoPtr != NULL) {
AcquireSRWLockShared(&handleInfoPtr->lock);
/* Remember at most one of READABLE, WRITABLE set */
if (chanInfoPtr->watchMask & TCL_READABLE) {
if (RingBufferLength(&handleInfoPtr->buffer) > 0
|| handleInfoPtr->lastError != ERROR_SUCCESS) {
block = 0; /* Input data available */
|
|
<
|
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
|
* Walk the list of channels. See general comments for struct
* ConsoleChannelInfo with regard to locking and field access.
*/
AcquireSRWLockShared(&gConsoleLock); /* READ lock - no data modification */
for (chanInfoPtr = gWatchingChannelList; block && chanInfoPtr != NULL;
chanInfoPtr = chanInfoPtr->nextWatchingChannelPtr) {
ConsoleHandleInfo *handleInfoPtr = FindConsoleInfo(chanInfoPtr);
if (handleInfoPtr != NULL) {
AcquireSRWLockShared(&handleInfoPtr->lock);
/* Remember at most one of READABLE, WRITABLE set */
if (chanInfoPtr->watchMask & TCL_READABLE) {
if (RingBufferLength(&handleInfoPtr->buffer) > 0
|| handleInfoPtr->lastError != ERROR_SUCCESS) {
block = 0; /* Input data available */
|
| ︙ | | | ︙ | |
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
|
TCL_UNUSED(int) /*direction*/,
void **handlePtr) /* Where to store the handle. */
{
ConsoleChannelInfo *chanInfoPtr = (ConsoleChannelInfo *)instanceData;
if (chanInfoPtr->handle == INVALID_HANDLE_VALUE) {
return TCL_ERROR;
} else {
*handlePtr = chanInfoPtr->handle;
return TCL_OK;
}
}
/*
*------------------------------------------------------------------------
*
* ConsoleDataAvailable --
*
* Checks if there is data in the console input queue.
*
* Results:
* Returns 1 if the input queue has data, -1 on error else 0 if empty.
*
* Side effects:
* None.
*
*------------------------------------------------------------------------
*/
static int
ConsoleDataAvailable(
HANDLE consoleHandle)
{
INPUT_RECORD input[10];
DWORD count;
DWORD i;
/*
* Need at least one keyboard event.
|
<
>
|
|
<
|
|
|
|
|
|
|
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
|
TCL_UNUSED(int) /*direction*/,
void **handlePtr) /* Where to store the handle. */
{
ConsoleChannelInfo *chanInfoPtr = (ConsoleChannelInfo *)instanceData;
if (chanInfoPtr->handle == INVALID_HANDLE_VALUE) {
return TCL_ERROR;
}
*handlePtr = chanInfoPtr->handle;
return TCL_OK;
}
/*
*------------------------------------------------------------------------
*
* ConsoleDataAvailable --
*
* Checks if there is data in the console input queue.
*
* Results:
* Returns 1 if the input queue has data, -1 on error else 0 if empty.
*
* Side effects:
* None.
*
*------------------------------------------------------------------------
*/
static int
ConsoleDataAvailable(
HANDLE consoleHandle)
{
INPUT_RECORD input[10];
DWORD count;
DWORD i;
/*
* Need at least one keyboard event.
|
| ︙ | | | ︙ | |
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
|
* Always 0.
*
* Side effects:
* Signals the main thread when input become available.
*
*----------------------------------------------------------------------
*/
static DWORD WINAPI
ConsoleReaderThread(
LPVOID arg)
{
ConsoleHandleInfo *handleInfoPtr = (ConsoleHandleInfo *) arg;
ConsoleHandleInfo **iterator;
Tcl_Size inputLen = 0;
|
<
|
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
|
* Always 0.
*
* Side effects:
* Signals the main thread when input become available.
*
*----------------------------------------------------------------------
*/
static DWORD WINAPI
ConsoleReaderThread(
LPVOID arg)
{
ConsoleHandleInfo *handleInfoPtr = (ConsoleHandleInfo *) arg;
ConsoleHandleInfo **iterator;
Tcl_Size inputLen = 0;
|
| ︙ | | | ︙ | |
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
|
consoleHandle = handleInfoPtr->console;
WakeConditionVariable(&handleInfoPtr->interpThreadCV);
ReleaseSRWLockExclusive(&handleInfoPtr->lock);
offset = 0;
while (numBytes > 0) {
Tcl_Size numWChars = numBytes / sizeof(WCHAR);
DWORD status;
status = WriteConsoleChars(handleInfoPtr->console,
(WCHAR *)(offset + buffer), numWChars, &numWChars);
if (status != 0) {
/* Only overwrite if no previous error */
if (handleInfoPtr->lastError == 0) {
handleInfoPtr->lastError = status;
}
if (status == ERROR_INVALID_HANDLE) {
|
<
|
|
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
|
consoleHandle = handleInfoPtr->console;
WakeConditionVariable(&handleInfoPtr->interpThreadCV);
ReleaseSRWLockExclusive(&handleInfoPtr->lock);
offset = 0;
while (numBytes > 0) {
Tcl_Size numWChars = numBytes / sizeof(WCHAR);
DWORD status = WriteConsoleChars(handleInfoPtr->console,
(WCHAR *)(offset + buffer), numWChars, &numWChars);
if (status != 0) {
/* Only overwrite if no previous error */
if (handleInfoPtr->lastError == 0) {
handleInfoPtr->lastError = status;
}
if (status == ERROR_INVALID_HANDLE) {
|
| ︙ | | | ︙ | |
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
|
}
/*
*------------------------------------------------------------------------
*
* AllocateConsoleHandleInfo --
*
* Allocates a ConsoleHandleInfo for the passed console handle. As
* a side effect starts a console thread to handle i/o on the handle.
*
* Important: Caller must be holding an EXCLUSIVE lock on gConsoleLock
* when calling this function. The lock continues to be held on return.
*
* Results:
* Pointer to an unlocked ConsoleHandleInfo structure. The reference
* count on the structure is 1. This corresponds to the common reference
* from the console thread and the gConsoleHandleInfoList. Returns NULL
* on error.
*
* Side effects:
* A console reader or writer thread is started. The returned structure
* is placed on the active console handler list gConsoleHandleInfoList.
*
*------------------------------------------------------------------------
*/
static ConsoleHandleInfo *
AllocateConsoleHandleInfo(
HANDLE consoleHandle,
int permissions) /* TCL_READABLE or TCL_WRITABLE */
{
ConsoleHandleInfo *handleInfoPtr;
DWORD consoleMode;
handleInfoPtr = (ConsoleHandleInfo *)Tcl_Alloc(sizeof(*handleInfoPtr));
memset(handleInfoPtr, 0, sizeof(*handleInfoPtr));
handleInfoPtr->console = consoleHandle;
InitializeSRWLock(&handleInfoPtr->lock);
InitializeConditionVariable(&handleInfoPtr->consoleThreadCV);
InitializeConditionVariable(&handleInfoPtr->interpThreadCV);
RingBufferInit(&handleInfoPtr->buffer, CONSOLE_BUFFER_SIZE);
handleInfoPtr->lastError = 0;
handleInfoPtr->permissions = permissions;
handleInfoPtr->numRefs = 1; /* See function header */
if (permissions == TCL_READABLE) {
GetConsoleMode(consoleHandle, &handleInfoPtr->initMode);
consoleMode = handleInfoPtr->initMode;
consoleMode &= ~(ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT);
consoleMode |= ENABLE_LINE_INPUT;
SetConsoleMode(consoleHandle, consoleMode);
}
handleInfoPtr->consoleThread = CreateThread(
|
|
|
|
|
|
|
|
|
|
|
|
|
<
>
|
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
|
}
/*
*------------------------------------------------------------------------
*
* AllocateConsoleHandleInfo --
*
* Allocates a ConsoleHandleInfo for the passed console handle. As
* a side effect starts a console thread to handle i/o on the handle.
*
* Important: Caller must be holding an EXCLUSIVE lock on gConsoleLock
* when calling this function. The lock continues to be held on return.
*
* Results:
* Pointer to an unlocked ConsoleHandleInfo structure. The reference
* count on the structure is 1. This corresponds to the common reference
* from the console thread and the gConsoleHandleInfoList. Returns NULL
* on error.
*
* Side effects:
* A console reader or writer thread is started. The returned structure
* is placed on the active console handler list gConsoleHandleInfoList.
*
*------------------------------------------------------------------------
*/
static ConsoleHandleInfo *
AllocateConsoleHandleInfo(
HANDLE consoleHandle,
int permissions) /* TCL_READABLE or TCL_WRITABLE */
{
ConsoleHandleInfo *handleInfoPtr = (ConsoleHandleInfo *)
Tcl_Alloc(sizeof(*handleInfoPtr));
memset(handleInfoPtr, 0, sizeof(*handleInfoPtr));
handleInfoPtr->console = consoleHandle;
InitializeSRWLock(&handleInfoPtr->lock);
InitializeConditionVariable(&handleInfoPtr->consoleThreadCV);
InitializeConditionVariable(&handleInfoPtr->interpThreadCV);
RingBufferInit(&handleInfoPtr->buffer, CONSOLE_BUFFER_SIZE);
handleInfoPtr->lastError = 0;
handleInfoPtr->permissions = permissions;
handleInfoPtr->numRefs = 1; /* See function header */
if (permissions == TCL_READABLE) {
DWORD consoleMode;
GetConsoleMode(consoleHandle, &handleInfoPtr->initMode);
consoleMode = handleInfoPtr->initMode;
consoleMode &= ~(ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT);
consoleMode |= ENABLE_LINE_INPUT;
SetConsoleMode(consoleHandle, consoleMode);
}
handleInfoPtr->consoleThread = CreateThread(
|
| ︙ | | | ︙ | |
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
|
}
/*
*------------------------------------------------------------------------
*
* FindConsoleInfo --
*
* Finds the ConsoleHandleInfo record for a given ConsoleChannelInfo.
* The found record must match the console handle. It is the caller's
* responsibility to check the permissions (read/write) in the returned
* ConsoleHandleInfo match permissions in chanInfoPtr. This function does
* not check that.
*
* Important: Caller must be holding an shared or exclusive lock on
* gConsoleMutex. That ensures the returned pointer stays valid on
* return without risk of deallocation by other threads.
*
* Results:
* Pointer to the found ConsoleHandleInfo or NULL if not found
*
* Side effects:
* None.
*
*------------------------------------------------------------------------
*/
static ConsoleHandleInfo *
FindConsoleInfo(
const ConsoleChannelInfo *chanInfoPtr)
{
ConsoleHandleInfo *handleInfoPtr;
for (handleInfoPtr = gConsoleHandleInfoList; handleInfoPtr; handleInfoPtr = handleInfoPtr->nextPtr) {
if (handleInfoPtr->console == chanInfoPtr->handle) {
return handleInfoPtr;
}
}
return NULL;
}
|
|
|
|
|
|
|
|
|
|
|
|
>
|
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
|
}
/*
*------------------------------------------------------------------------
*
* FindConsoleInfo --
*
* Finds the ConsoleHandleInfo record for a given ConsoleChannelInfo.
* The found record must match the console handle. It is the caller's
* responsibility to check the permissions (read/write) in the returned
* ConsoleHandleInfo match permissions in chanInfoPtr. This function does
* not check that.
*
* Important: Caller must be holding an shared or exclusive lock on
* gConsoleMutex. That ensures the returned pointer stays valid on
* return without risk of deallocation by other threads.
*
* Results:
* Pointer to the found ConsoleHandleInfo or NULL if not found
*
* Side effects:
* None.
*
*------------------------------------------------------------------------
*/
static ConsoleHandleInfo *
FindConsoleInfo(
const ConsoleChannelInfo *chanInfoPtr)
{
ConsoleHandleInfo *handleInfoPtr;
for (handleInfoPtr = gConsoleHandleInfoList; handleInfoPtr;
handleInfoPtr = handleInfoPtr->nextPtr) {
if (handleInfoPtr->console == chanInfoPtr->handle) {
return handleInfoPtr;
}
}
return NULL;
}
|
| ︙ | | | ︙ | |
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
|
* May modify an option on a console. Sets Error message if needed (by
* calling Tcl_BadChannelOption).
*
*----------------------------------------------------------------------
*/
static int
ConsoleSetOptionProc(
void *instanceData, /* File state. */
Tcl_Interp *interp, /* For error reporting - can be NULL. */
const char *optionName, /* Which option to set? */
const char *value) /* New value for option. */
{
ConsoleChannelInfo *chanInfoPtr = (ConsoleChannelInfo *)instanceData;
int len = strlen(optionName);
int vlen = strlen(value);
|
|
|
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
|
* May modify an option on a console. Sets Error message if needed (by
* calling Tcl_BadChannelOption).
*
*----------------------------------------------------------------------
*/
static int
ConsoleSetOptionProc(
void *instanceData, /* File state. */
Tcl_Interp *interp, /* For error reporting - can be NULL. */
const char *optionName, /* Which option to set? */
const char *value) /* New value for option. */
{
ConsoleChannelInfo *chanInfoPtr = (ConsoleChannelInfo *)instanceData;
int len = strlen(optionName);
int vlen = strlen(value);
|
| ︙ | | | ︙ | |
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
|
* (by calling Tcl_BadChannelOption).
*
*----------------------------------------------------------------------
*/
static int
ConsoleGetOptionProc(
void *instanceData, /* File state. */
Tcl_Interp *interp, /* For error reporting - can be NULL. */
const char *optionName, /* Option to get. */
Tcl_DString *dsPtr) /* Where to store value(s). */
{
ConsoleChannelInfo *chanInfoPtr = (ConsoleChannelInfo *)instanceData;
int valid = 0; /* Flag if valid option parsed. */
unsigned int len;
|
|
|
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
|
* (by calling Tcl_BadChannelOption).
*
*----------------------------------------------------------------------
*/
static int
ConsoleGetOptionProc(
void *instanceData, /* File state. */
Tcl_Interp *interp, /* For error reporting - can be NULL. */
const char *optionName, /* Option to get. */
Tcl_DString *dsPtr) /* Where to store value(s). */
{
ConsoleChannelInfo *chanInfoPtr = (ConsoleChannelInfo *)instanceData;
int valid = 0; /* Flag if valid option parsed. */
unsigned int len;
|
| ︙ | | | ︙ | |