Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 15c42a2e211dcfcea77aaeeb7d509d3ce9f29431f92bce614cda4fcc098d1063
Ticket: 1d0618000674abdf701021d69eaa858cbccf14a4
channel reads prematurely from stdin on Windows
User & Date: apnadkarni on 2025-11-15 11:29:51
Changes

  1. icomment:
    I have fixed the [apn-winconsole-noreadahead](https://core.tcl-lang.org/tcl/info/e70736d4962003d8)  branch to filter out the Alt keypresses. That was just a plain oversight.
    
    It also turns out that to enable per-character read() calls, in addition to `SetConsoleMode` you also need to call `_setmode()` on stdin with value `_O_BINARY`. Otherwise, carraige-return is not handled properly. This is independent of Tcl channels.
    
    The program below demonstrates the working (pass `line` or `char` to enable the two modes). There are still two issues:
    
    - While other control characters are handled correctly, for some reason Ctrl-J and Ctrl-Z are not, possibly special handling in the CRT? The callback is still invoked but the `read()` fails.
    
    - Unicode characters are not handled by the `read()` call. Non iso8859-1 (on my system) read `?` as they are not encodable.
    
    Both above may possibly be fixed by using `ReadFileW` or `ReadConsoleW`, not sure, but don't think it has anything to do with Tcl.
    
    Confusing things will probably happen if any Tcl code reads from stdin in addition to tkinter. I doubt that can be avoided.
    
    I suspect you are right that it might be better for Python to implement its own event loop but I also suspect it would be considerably more complex.
    
    As a completely unrelated aside, it would be better if any code that works with Tcl 9 calls TclZipfs_Apphook as the very first call to initialize Tcl.
    
    Anyways, below is the modified version of your sample code.
    
    ```
    // cl -Zi -I../../generic -I. ../x.c tcl91.lib
    // gcc -o x -I/usr/include/tcl8.6 x.c -ltcl8.6
    #include <tcl.h>
    #include <stdio.h>
    #ifdef _WIN32
    #include <fcntl.h>
    #include <windows.h>
    #else
    #include <unistd.h>
    #endif
    
    void printHex(const char *label, const char *data, int length) {
        for (int i = 0; i < length; i++) {
    	printf("%s%02x", label, (unsigned char)data[i]);
    	label = ",";
        }
        printf("\n");
        fflush(stdout);
    }
    
    void StdinCallback(ClientData clientData, int mask) {
        char buffer[16];
    
        // Read a line from stdin
        int n;
        n = read(0, buffer, sizeof(buffer) - 1);
        if (n > 0) {
            buffer[n] = '\0';
    	if (buffer[0] == 'q')
    	    Tcl_Exit(0);
    	printHex("Got input: ", buffer, n);
        }
    }
    
    int main(int argc, char *argv[]) {
        int charMode = 0; // default to line mode
        if (argc > 1) {
    	if (!strcmp(argv[1], "char")) {
    	    charMode = _O_BINARY;
    	} else if (strcmp(argv[1], "line")) {
    	    fprintf(stderr, "Usage: %s [char|line]\n", argv[0]);
    	    exit(1);
    	}
        }
        Tcl_Interp *interp = Tcl_CreateInterp();
    
        if (Tcl_Init(interp) != TCL_OK) {
            fprintf(stderr, "Tcl_Init failed: %s\n", Tcl_GetStringResult(interp));
            return 1;
        }
    
        Tcl_Channel in = Tcl_GetStdChannel(TCL_STDIN);
        if (!in) {
            fprintf(stderr, "No stdin channel available\n");
            return 1;
        }
    
        if (charMode) {
    	HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
    	DWORD mode;
    	GetConsoleMode(hStdin, &mode);
    	SetConsoleMode(hStdin,
    		       mode & ~(ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT));
    	_setmode(_fileno(stdin), _O_BINARY);
        }
    
        Tcl_CreateChannelHandler(in, TCL_READABLE, StdinCallback, NULL);
    
        printf("Type something and press Enter:\n");
    
        // Run the event loop
        while (1) {
            Tcl_DoOneEvent(0);
        }
    
        return 0;
    }
    
    ```
    
  2. login: "apnadkarni"
  3. mimetype: "text/x-markdown"