Linux: Unbuffered reading from stdin



Hi folks.

I'm somewhat new to *nix programming and just ran into a problem. I have
to take user input from the terminal but like to constrain that to some
rules given, i.e. "numbers only" or "alphanumeric only" etc.
scanf would do, but I don't like the fact that wrong ('disallowed')
characters are visible and all that stuff. For that I need unbuffered
input. Since there seems to be no atomic function to do that I tried to
fiddle around with ioctl() and that seems to work fine. Currently I flag
out ICANON and ECHO from the tty descriptor, set a minimum input of 1
byte, getch() or read() a byte, check, printf(), fine.
However things become nasty when the user presses an key that sends an
escape sequence. I've seen some code on the web that does the following
approach: Check for character==27. If so, read() 4 bytes into a buffer,
zero terminate the buffer accordingly then strcmp() the buffer against
the actual escape sequences.

But then there are two problems:

1) The escape sequences seem to change with every terminal type. On my
terminal the codes for F1 to F5 are ^OP, ^OQ, ^OR, ^OS, ^[15~ (etc)
while on the code given in the web they used to be [[A, [[B etc.
Are there some defines?

2) What happens when the user presses just ESC? Sounds easy to me, just
check if there are no more chars in stdin - but how do you do that? By
just read()ing STDIN_FILENO after an ESC the terminal is blocked until
another key is pressed - that is not really what I want.

Any ideas? Or am I reinventing the wheel?
Regards,
Markus
.



Relevant Pages

  • I dont mind bug: BinToHex implementation/description do not match !
    ... The BinToHex procedure occurding to Delphi's help would return a 'null' ... to terminate it with a #0 in the first place. ... Call BinToHex to convert the binary value in a buffer into a string that is ... Buffer is a buffer of bytes that contains the binary value. ...
    (alt.comp.lang.borland-delphi)
  • Re: reading string from a text file from vc++ without MFC support
    ... You could terminate it using the return from freadby writing a '\0' into the character array. ... You should also call ReleaseBuffer() since you've modified the buffer contents and you could just pass the length returned in that call. ... It's also suspicious that you allocate CString strTitle1 on the stack then proceed to call GetLength on it before anything is assigned to it. ... That could cause you some grief as well:o) If you are reading a text file you could use CStdioFile and that will be a lot easier to manage. ...
    (microsoft.public.vc.mfc)
  • Re: proper way to determine string length
    ... If you call reador freadthe buffer is not guaranteed to be ... string, it probably won't be null-terminated and will probably ... forgetting to null terminate a string" unless you're going to try ... strings into a null-termination problem. ...
    (comp.unix.programmer)
  • Re: Why cant i write to a file with this code?
    ... value from read is used as an array subscript to terminate the buffer? ... On success, the number of bytes read is returned (zero ... Thus, after reading n bytes from the file, he puts a terminating nul at the end of them. ... if the read fails for any reason, it'll return -1 and so he'll be splatting a nul over the byte *BEFORE* the buffer - he may be lucky in that it does no damage, but then again, he may not... ...
    (alt.os.linux)
  • Re: I dont mind bug: BinToHex implementation/description do not match !
    ... assigning it to another string you may see this problem. ... > forgets to add a null terminated character. ... > to terminate it with a #0 in the first place. ... > Buffer is a buffer of bytes that contains the binary value. ...
    (alt.comp.lang.borland-delphi)