I'm trying to write a TCL script that reads from stdin channel 1 or 2 characters from user, discarding anything other from user input without reading it. Malicious user can create a very long string to try to overflow the buffer, that's why I want to read maximum 2 characters. I understand, to do it, something like read $chan 2 and chan pending input $chan can be used together, but I cannot figure out how to do it right.
I tried read $chan 2, but if the user enters 1 character, read just reads it and a newline character as well; if user enters more than 2 characters, they are still in input, I can check for them with chan pending input $chan. I expect just to read 2 characters from the user and discard everything else.
Any suggestions? Thanks!