473,499 Members | 1,551 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Oct 22 '07 #1
7 6142
Markus Mayer wrote:
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.
However things become nasty when the user presses an key that sends an
escape sequence.
Any ideas? Or am I reinventing the wheel?
This is really a comp.unix.programming question, but it
sounds like ncurses is what you want.

--
imalone
Oct 22 '07 #2
Am 22.10.2007 13:24 postulierte Markus Mayer folgendes:
[...]
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.
D'oh ... that was a dumb one. By reading a maximum of n bytes into the
buffer I can check bytes 2..n if buffer[0] is 27. Okay - done.

Are there other solutions though?

Regards,
Markus
Oct 22 '07 #3
Am 22.10.2007 13:38 postulierte Ian Malone folgendes:
Markus Mayer wrote:
>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.
>However things become nasty when the user presses an key that sends an
escape sequence.

This is really a comp.unix.programming question, but it
sounds like ncurses is what you want.
Haven't seen that group, thanks! I'll have a deeper look at ncurses, but
from what I've see so far I think that I'm not allowed to use it; I
really have to do it 'on my own'. :/

Thanks anyway!
Markus
Oct 22 '07 #4
Markus Mayer wrote:

<about handling escape sequences and special characters in unbuffered
input>

Just use ncurses and ask in a UNIX group like
<news:comp.unix.programming>

Oct 22 '07 #5
Op Mon, 22 Oct 2007 18:26:28 +0530 schreef santosh:
Markus Mayer wrote:

<about handling escape sequences and special characters in unbuffered
input>

Just use ncurses and ask in a UNIX group like
<news:comp.unix.programming>
<news:comp.unix.programmer>
--
Coos
Oct 22 '07 #6
Ian Malone wrote:
Markus Mayer wrote:
>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.

However things become nasty when the user presses an key that
sends an escape sequence.

Any ideas? Or am I reinventing the wheel?

This is really a comp.unix.programming question, but it
sounds like ncurses is what you want.
No, he needs getc and ungetc. A simple routine can input numbers
from a stream with that, and ensure the terminating (non-numeric)
character is left in the stream.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Oct 23 '07 #7
#if offtopic == pet_peeve

On Mon, 22 Oct 2007 13:24:49 +0200, Markus Mayer
<co***********************@defx.dewrote:
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 <snip>
Aside: Personally I don't share your goal. I would rather _see_ what I
entered -- with any retained invisible bits made visible, e.g. caret,X
for control-X -- plus a statement of why it's wrong and/or what would
be permitted. The allegedly user-friendly interface in my experience
seems to encourage the approach of 'oh, just push keys (or click) at
random until something happens that appears more or less right'. But
this is personal taste; I don't mind _you_ wanting it the other way.
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?
Not really. Although I'm a bit surprised you can still find today many
different real terminals or even emulators, there used to be a huge
variety. There are some standards, notably ANSI X3.64 (I suppose now
INCITS 64), which was used on a large scale by DEC's VT100 and VT200
series and by the now prevalent Xwindows utility xterm. But even that
standard has portions that may be varied by each implementor/ation.

This is precisely why vanilla Unix tty drivers don't even try to deal
with it; they only support sequential, across-and-down-the-page,
printable plus CR LF/NL, probably BS, and maybe HT. And similarly
TELNET the protocol goes very little beyond this (although specific
TELNET _clients_ may do more), and also the CCITT equivalent X.3 if
you can find anyone who even knows of it much less uses it.
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.
The conventional approach is a short timeout, which in Unix would be
poll, select, or similar probably combined with nonblocking I/O.
But if your transmission path is anything more complicated than an
actual hardwired RS232 cable (which is limited to 50ft; well, or
RS42x/385IIRC up to maybe a thousand feet) then occasionally you will
get delays introduced to the middle of your input stream due to
encoding, routing, retransmission, congestion, or whatever and your
code will, like the villain in Indiana Jones (3?), 'choose badly'.
When this happens, the program and the terminal get out of sync, and
unless the user recognizes it quickly AND the program provides a way
to recover (such as vi ^L 're-draw') it can lead to corrupted files
and databases and even things like unintended stock trades.

The other alternative is just have people not use the ESC key. Some
people, or more commonly their organizations, used to physically
remove or block the ESC key on real physical terminals dedicated to an
application/system. Nowadays if you're using the same PC for your
terminal emulator and for other tasks you probably can't allow that.
Any ideas? Or am I reinventing the wheel?
Yes. As others have noted, the most widespread and established
solution for this, and probably most convenient for you, is (are?) the
various variants of curses/ncurses. There were plenty of other
attempts, although the ones I knew of were mostly tied to particular
systems or applications or both; but if you're interested I warn you
this can be a giant time sink -- in the '70s and into the '80s, there
were quite a few people whose fulltime job, or a major chunk of it,
was keeping track of and coping with different 'smart' terminal types
and in particular their protocols. The folks on alt.folklore.computers
probably can help you, but it's such hightraffic I don't have time to
follow it myself except for occasional crossposts.

#endif
- formerly david.thompson1 || achar(64) || worldnet.att.net
Nov 4 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
6116
by: Max Rabkin | last post by:
While java's System.in is, according to the javadocs, an InputStream, it is in fact an instance of java.io.BufferedInputStream. Is there any way to have unbuffered access to it without hacking the...
5
17406
by: richard | last post by:
I have a simple test to pass information from a client to a server using named pipe. what I really want is: when I type a line on the client, the server will output the line immediately. but to my...
11
3226
by: Lonnie Princehouse | last post by:
>From the cmd shell on both Windows 2k and XP, I'm getting this weird syntax error in conjunction with the unbuffered flag. It works fine without -u. Has anyone else encountered it? This didn't...
6
2459
by: Charlie Zender | last post by:
Hi, I have a program which takes the output filename argument from stdin. Once the program knows the output filename, it tries to open it. If the output file exists, the program asks the user to...
4
4147
by: pank7 | last post by:
hi everyone, I have a program here to test the file IO(actually output) with buffer turned on and off. What I want to see is that there will be obvious differece in time. Here I have an input...
19
5350
by: Lee Crabtree | last post by:
Is there a class in the framework that allows me read text from a file in an unbuffered manner? That is, I'd like to be able to read lines in the same manner as StreamReader.ReadLine(), but I also...
0
1196
by: Hamish Allan | last post by:
Hi, The man page for python says: "-u Force stdin, stdout and stderr to be totally unbuffered." However, when I try: $ ssh localhost python -u print 'hello, world'
3
11190
by: Paddy | last post by:
Hi, I am am falling at the first hurdle when trying to access a library using ctypes. I have a file libucdb.so which the file command says is shared object, but I cannot get it to load: Any...
27
3102
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I have a fully-portable C program (or at least I think I do). It works fine on Windows, but malfunctions on Linux. I suspect that there's something I don't know about the standard input stream...
0
7130
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3090
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.