Skip to content

Commit 3c0d563

Browse files
committed
Turn some defines into enums, plus other minor cleanup in the win console code
1 parent 07c59c3 commit 3c0d563

File tree

1 file changed

+78
-77
lines changed

1 file changed

+78
-77
lines changed

win/tclWinConsole.c

Lines changed: 78 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
* multiple Tcl channels corresponding to a single console handle.
3737
*
3838
* - Even with multiple threads, more than one file event handler is
39-
* unlikely. It does not make sense for multiple threads to register
40-
* handlers for stdin because the input would be randomly fragmented amongst
41-
* the threads.
39+
* unlikely. It does not make sense for multiple threads to register
40+
* handlers for stdin because the input would be randomly fragmented amongst
41+
* the threads.
4242
*
4343
* Various design factors are driven by the above, e.g. use of lists instead
4444
* of hash tables (at most 3 console handles) and use of global instead of
@@ -58,7 +58,7 @@
5858
*
5959
* If multiple threads are reading from stdin, the input is sprayed in
6060
* random fashion. This is not good application design and hence no plan to
61-
* address this (not clear what should be done even in theory)
61+
* address this (not clear what should be done even in theory).
6262
*
6363
* For output, we do not restrict all output to the console writer threads.
6464
* See ConsoleOutputProc for the conditions.
@@ -94,14 +94,18 @@ static int gInitialized = 0;
9494
* and bufPtr[0]:bufPtr[length - (size-start)].
9595
*/
9696
typedef struct RingBuffer {
97-
char *bufPtr; /* Pointer to buffer storage */
98-
Tcl_Size capacity; /* Size of the buffer in RingBufferChar */
99-
Tcl_Size start; /* Start of the data within the buffer. */
100-
Tcl_Size length; /* Number of RingBufferChar*/
97+
char *bufPtr; /* Pointer to buffer storage */
98+
Tcl_Size capacity; /* Size of the buffer in RingBufferChar */
99+
Tcl_Size start; /* Start of the data within the buffer. */
100+
Tcl_Size length; /* Number of RingBufferChar*/
101101
} RingBuffer;
102-
#define RingBufferLength(ringPtr_) ((ringPtr_)->length)
103-
#define RingBufferHasFreeSpace(ringPtr_) ((ringPtr_)->length < (ringPtr_)->capacity)
104-
#define RINGBUFFER_ASSERT(ringPtr_) assert(RingBufferCheck(ringPtr_))
102+
103+
#define RingBufferLength(ringPtr_) \
104+
((ringPtr_)->length)
105+
#define RingBufferHasFreeSpace(ringPtr_) \
106+
((ringPtr_)->length < (ringPtr_)->capacity)
107+
#define RINGBUFFER_ASSERT(ringPtr_) \
108+
assert(RingBufferCheck(ringPtr_))
105109

106110
/*
107111
* The Win32 console API does not support non-blocking I/O in any form. Thus
@@ -126,28 +130,34 @@ typedef struct RingBuffer {
126130
*/
127131
typedef struct ConsoleHandleInfo {
128132
struct ConsoleHandleInfo *nextPtr; /* Process-global list of consoles */
129-
HANDLE console; /* Console handle */
130-
HANDLE consoleThread; /* Handle to thread doing actual i/o on the console */
131-
SRWLOCK lock; /* Controls access to this structure.
132-
* Cheaper than CRITICAL_SECTION but note does not
133-
* support recursive locks or Try* style attempts.*/
133+
HANDLE console; /* Console handle */
134+
HANDLE consoleThread; /* Handle to thread doing actual i/o on the
135+
* console */
136+
SRWLOCK lock; /* Controls access to this structure. Cheaper
137+
* than CRITICAL_SECTION but note does not
138+
* support recursive locks or Try* style
139+
* attempts. */
134140
CONDITION_VARIABLE consoleThreadCV;/* For awakening console thread */
135141
CONDITION_VARIABLE interpThreadCV; /* For awakening interpthread(s) */
136-
RingBuffer buffer; /* Buffer for data transferred between console
137-
* threads and Tcl threads. For input consoles,
138-
* written by the console thread and read by Tcl
139-
* threads. The converse for output threads */
140-
DWORD initMode; /* Initial console mode. */
141-
DWORD lastError; /* An error caused by the last background
142-
* operation. Set to 0 if no error has been
143-
* detected. */
144-
int numRefs; /* See comments above */
145-
int permissions; /* TCL_READABLE for input consoles, TCL_WRITABLE
146-
* for output. Only one or the other can be set. */
147-
int flags;
148-
#define CONSOLE_DATA_AWAITED 0x0001 /* An interpreter is awaiting data */
142+
RingBuffer buffer; /* Buffer for data transferred between console
143+
* threads and Tcl threads. For input consoles,
144+
* written by the console thread and read by Tcl
145+
* threads. The converse for output threads */
146+
DWORD initMode; /* Initial console mode. */
147+
DWORD lastError; /* An error caused by the last background
148+
* operation. Set to 0 if no error has been
149+
* detected. */
150+
int numRefs; /* See comments above */
151+
int permissions; /* TCL_READABLE for input consoles,
152+
* TCL_WRITABLE for output. Only one or the
153+
* other can be set. */
154+
int flags; /* State flags */
149155
} ConsoleHandleInfo;
150156

157+
enum ConsoleHandleInfoFlags {
158+
CONSOLE_DATA_AWAITED = 1 /* An interpreter is awaiting data */
159+
};
160+
151161
/*
152162
* This structure describes per-instance data for a console based channel.
153163
*
@@ -190,11 +200,14 @@ typedef struct ConsoleChannelInfo {
190200
* TCL_WRITABLE, or TCL_EXCEPTION: indicates
191201
* which events should be reported. */
192202
int flags; /* State flags */
193-
#define CONSOLE_EVENT_QUEUED 0x0001 /* Notification event already queued */
194-
#define CONSOLE_ASYNC 0x0002 /* Channel is non-blocking. */
195-
#define CONSOLE_READ_OPS 0x0004 /* Channel supports read-related ops. */
196203
} ConsoleChannelInfo;
197204

205+
enum ConsoleChannelInfoFlags {
206+
CONSOLE_EVENT_QUEUED = 1, /* Notification event already queued */
207+
CONSOLE_ASYNC = 2, /* Channel is non-blocking. */
208+
CONSOLE_READ_OPS = 4 /* Channel supports read-related ops. */
209+
};
210+
198211
/*
199212
* The following structure is what is added to the Tcl event queue when
200213
* console events are generated.
@@ -424,7 +437,7 @@ RingBufferIn(
424437
} else {
425438
/* No room at the back. Existing data wrap to front. */
426439
Tcl_Size wrapLen =
427-
ringPtr->start + ringPtr->length - ringPtr->capacity;
440+
ringPtr->start + ringPtr->length - ringPtr->capacity;
428441
memmove(wrapLen + ringPtr->bufPtr, srcPtr, srcLen);
429442
}
430443

@@ -484,11 +497,8 @@ RingBufferOut(
484497
} else {
485498
Tcl_Size wrapLen = dstCapacity - leadLen;
486499
if (dstPtr) {
487-
memmove(dstPtr,
488-
ringPtr->start + ringPtr->bufPtr,
489-
leadLen);
490-
memmove(
491-
leadLen + dstPtr, ringPtr->bufPtr, wrapLen);
500+
memmove(dstPtr, ringPtr->start + ringPtr->bufPtr, leadLen);
501+
memmove(leadLen + dstPtr, ringPtr->bufPtr, wrapLen);
492502
}
493503
ringPtr->start = wrapLen;
494504
}
@@ -539,7 +549,6 @@ ReadConsoleChars(
539549
Tcl_Size *nCharsReadPtr)
540550
{
541551
DWORD nRead;
542-
BOOL result;
543552

544553
/*
545554
* If user types a Ctrl-Break or Ctrl-C, ReadConsole will return success
@@ -561,17 +570,15 @@ ReadConsoleChars(
561570
* or https://github.com/microsoft/terminal/issues/12143
562571
*/
563572
nRead = (DWORD)-1;
564-
result = ReadConsoleW(hConsole, lpBuffer, nChars, &nRead, NULL);
565-
if (result) {
566-
if ((nRead == 0 || nRead == (DWORD)-1)
567-
&& GetLastError() == ERROR_OPERATION_ABORTED) {
568-
nRead = 0;
569-
}
570-
*nCharsReadPtr = nRead;
571-
return 0;
572-
} else {
573+
if (!ReadConsoleW(hConsole, lpBuffer, nChars, &nRead, NULL)) {
573574
return GetLastError();
574575
}
576+
if ((nRead == 0 || nRead == (DWORD)-1)
577+
&& GetLastError() == ERROR_OPERATION_ABORTED) {
578+
nRead = 0;
579+
}
580+
*nCharsReadPtr = nRead;
581+
return 0;
575582
}
576583

577584
/*
@@ -600,20 +607,17 @@ WriteConsoleChars(
600607
Tcl_Size *nCharsWrittenPtr)
601608
{
602609
DWORD nCharsWritten;
603-
BOOL result;
604610

605611
/* See comments in ReadConsoleChars, not sure that applies here */
606612
nCharsWritten = (DWORD)-1;
607-
result = WriteConsoleW(hConsole, lpBuffer, nChars, &nCharsWritten, NULL);
608-
if (result) {
609-
if (nCharsWritten == (DWORD) -1) {
610-
nCharsWritten = 0;
611-
}
612-
*nCharsWrittenPtr = nCharsWritten;
613-
return 0;
614-
} else {
613+
if (!WriteConsoleW(hConsole, lpBuffer, nChars, &nCharsWritten, NULL)) {
615614
return GetLastError();
616615
}
616+
if (nCharsWritten == (DWORD) -1) {
617+
nCharsWritten = 0;
618+
}
619+
*nCharsWrittenPtr = nCharsWritten;
620+
return 0;
617621
}
618622

619623
/*
@@ -787,8 +791,7 @@ ConsoleSetupProc(
787791

788792
for (chanInfoPtr = gWatchingChannelList; block && chanInfoPtr != NULL;
789793
chanInfoPtr = chanInfoPtr->nextWatchingChannelPtr) {
790-
ConsoleHandleInfo *handleInfoPtr;
791-
handleInfoPtr = FindConsoleInfo(chanInfoPtr);
794+
ConsoleHandleInfo *handleInfoPtr = FindConsoleInfo(chanInfoPtr);
792795
if (handleInfoPtr != NULL) {
793796
AcquireSRWLockShared(&handleInfoPtr->lock);
794797
/* Remember at most one of READABLE, WRITABLE set */
@@ -1562,10 +1565,9 @@ ConsoleGetHandleProc(
15621565

15631566
if (chanInfoPtr->handle == INVALID_HANDLE_VALUE) {
15641567
return TCL_ERROR;
1565-
} else {
1566-
*handlePtr = chanInfoPtr->handle;
1567-
return TCL_OK;
15681568
}
1569+
*handlePtr = chanInfoPtr->handle;
1570+
return TCL_OK;
15691571
}
15701572

15711573
/*
@@ -1583,8 +1585,8 @@ ConsoleGetHandleProc(
15831585
*
15841586
*------------------------------------------------------------------------
15851587
*/
1586-
static int
1587-
ConsoleDataAvailable(
1588+
static int
1589+
ConsoleDataAvailable(
15881590
HANDLE consoleHandle)
15891591
{
15901592
INPUT_RECORD input[10];
@@ -1617,7 +1619,7 @@ ConsoleGetHandleProc(
16171619
}
16181620
return 0;
16191621
}
1620-
1622+
16211623
/*
16221624
*----------------------------------------------------------------------
16231625
*
@@ -1634,7 +1636,6 @@ ConsoleGetHandleProc(
16341636
*
16351637
*----------------------------------------------------------------------
16361638
*/
1637-
16381639
static DWORD WINAPI
16391640
ConsoleReaderThread(
16401641
LPVOID arg)
@@ -1968,7 +1969,6 @@ ConsoleWriterThread(
19681969
ReleaseSRWLockExclusive(&gConsoleLock);
19691970

19701971
RingBufferClear(&handleInfoPtr->buffer);
1971-
19721972
Tcl_Free(handleInfoPtr);
19731973

19741974
return 0;
@@ -2023,12 +2023,12 @@ AllocateConsoleHandleInfo(
20232023
SetConsoleMode(consoleHandle, consoleMode);
20242024
}
20252025
handleInfoPtr->consoleThread = CreateThread(
2026-
NULL, /* default security descriptor */
2027-
2*CONSOLE_BUFFER_SIZE, /* Stack size - gets rounded up to granularity */
2028-
permissions == TCL_READABLE ? ConsoleReaderThread : ConsoleWriterThread,
2029-
handleInfoPtr, /* Pass to thread */
2030-
0, /* Flags - no special cases */
2031-
NULL); /* Don't care about thread id */
2026+
NULL, /* default security descriptor */
2027+
2*CONSOLE_BUFFER_SIZE, /* Stack size - gets rounded up to granularity */
2028+
permissions == TCL_READABLE ? ConsoleReaderThread : ConsoleWriterThread,
2029+
handleInfoPtr, /* Pass to thread */
2030+
0, /* Flags - no special cases */
2031+
NULL); /* Don't care about thread id */
20322032
if (handleInfoPtr->consoleThread == NULL) {
20332033
/* Note - SRWLock and condition variables do not need finalization */
20342034
RingBufferClear(&handleInfoPtr->buffer);
@@ -2071,14 +2071,15 @@ FindConsoleInfo(
20712071
const ConsoleChannelInfo *chanInfoPtr)
20722072
{
20732073
ConsoleHandleInfo *handleInfoPtr;
2074-
for (handleInfoPtr = gConsoleHandleInfoList; handleInfoPtr; handleInfoPtr = handleInfoPtr->nextPtr) {
2074+
for (handleInfoPtr = gConsoleHandleInfoList; handleInfoPtr;
2075+
handleInfoPtr = handleInfoPtr->nextPtr) {
20752076
if (handleInfoPtr->console == chanInfoPtr->handle) {
20762077
return handleInfoPtr;
20772078
}
20782079
}
20792080
return NULL;
20802081
}
2081-
2082+
20822083
/*
20832084
*----------------------------------------------------------------------
20842085
*
@@ -2258,7 +2259,7 @@ ConsoleThreadActionProc(
22582259
*/
22592260
static int
22602261
ConsoleSetOptionProc(
2261-
void *instanceData, /* File state. */
2262+
void *instanceData, /* File state. */
22622263
Tcl_Interp *interp, /* For error reporting - can be NULL. */
22632264
const char *optionName, /* Which option to set? */
22642265
const char *value) /* New value for option. */
@@ -2347,7 +2348,7 @@ ConsoleSetOptionProc(
23472348

23482349
static int
23492350
ConsoleGetOptionProc(
2350-
void *instanceData, /* File state. */
2351+
void *instanceData, /* File state. */
23512352
Tcl_Interp *interp, /* For error reporting - can be NULL. */
23522353
const char *optionName, /* Option to get. */
23532354
Tcl_DString *dsPtr) /* Where to store value(s). */

0 commit comments

Comments
 (0)