This is the mail archive of the
ecos-discuss@sourceware.org
mailing list for the eCos project.
Re: Termios and waiting for character input
- From: Gary Thomas <gary at mlbassoc dot com>
- To: Andre-John Mas <ajmas at sympatico dot ca>
- Cc: ecos-discuss at ecos dot sourceware dot org, Jonathan Larmour <jifl at eCosCentric dot com>
- Date: Thu, 24 Aug 2006 17:24:30 -0600
- Subject: Re: [ECOS] Termios and waiting for character input
- References: <20060823231358.YHRH10262.tomts22-srv.bellnexxia.net@smtp1.sympatico.ca>
Andre-John Mas wrote:
Hi,
I have the code below. I want to be able to block until something
is inputted and then continue. The only problem is am finding that
it continues even without anything being inputed. What should I be
doing differently:
It looks like a bug :-( It seems when ICANON is turned off, the
channel becomes non-blocking and will return EOF (or the equivalent)
when read. I tried the attached program - on Linux it works as
expected, but on eCos, it gets EOF.
Jonathan - you worked the termios code originally - any ideas?
--
------------------------------------------------------------
Gary Thomas | Consulting for the
MLB Associates | Embedded world
------------------------------------------------------------
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <errno.h>
int
main(int argc, char *argv[])
{
struct termios term;
struct termios termsave;
int c;
int fd = STDIN_FILENO;
// INFO: make sure this is a tty
if (!isatty(fd)) {
fprintf(stderr,"isatty: STDIN_FILENO not a tty\n");
exit(1);
}
// INFO: get term i/o flags for this tty
if (tcgetattr(fd, &termsave) < 0) {
perror("tcgetattr");
exit(1);
}
term = termsave; /* termsave contains old termios */
term.c_lflag &= ~ECHO; /* turn off echo if it's set */
term.c_lflag &= ~ECHOE; /* turn off echoe if it's set */
term.c_lflag &= ~ICANON; /* turn off echoe if it's set */
// INFO: propagate changes
if (tcsetattr(fd, TCSAFLUSH, &term) < 0) {
perror("tcsetattr");
exit(1);
}
if ((c = fgetc(stdin)) == EOF) {
fprintf(stderr, "\nCan't read: %s\n", strerror(errno));
} else {
fprintf(stderr, "\nYou typed 0x%02x\n", (int)c);
}
// INFO: restore old state
if (tcsetattr(fd, TCSAFLUSH, &termsave) < 0) {
perror("tcsetattr");
exit(1);
}
}
--
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss