Subject: Re: fgetc and getchar, how do I stop echo.
To: None <johnam@datastorm.com>
From: Mike Long <mike.long@analog.com>
List: netbsd-help
Date: 07/12/1996 15:17:02
>From: John Maier <johnam@datastorm.com>
>Date: Fri, 12 Jul 1996 12:43:56 -0500
>
>I have a question about fgetc and getchar.
>
>I want to be able to create a menu so the user needs only to press a letter
>to activate a function.
>
>The functions fgetc and getchar are suppose to perform functions like this.
>However I have found that a <CR> needs to entered before any more code is
>allowed to execute.

>Another problems is the echoing of characters to the screen.  For example,
>I want all of the data entered by the user to be upper case, regardless
>the state of the CapsLock key.  This would require me to:
>1) handle each character as it comes in.
>2) Echo the data to the console.

You can't use stdio for this; you need to turn off the ICANON and ECHO
flags on the TTY.  Try something like:

#include <unistd.h>
#include <termios.h>
#include <ctype.h>

int
main()
{
	struct termios old, new;
	char c;

	tcgetattr(0, &old);
	new = old;
	new.c_lflag &= ~(ICANON | ECHO | ECHONL);
	tcsetattr(0, TCSANOW, &new);

	read(0, &c, 1);
	if (islower(c))
		c = toupper(c);
	write(1, &c, 1);

	tcsetattr(0, TCSANOW, &old);
	return c;
}

See termios(4), ioctl(2), &c.
-- 
Mike Long <mike.long@analog.com>     <URL:http://www.shore.net/~mikel>
VLSI Design Engineer         finger mikel@shore.net for PGP public key
Analog Devices, CPD Division          CCBF225E7D3F7ECB2C8F7ABB15D9BE7B
Norwood, MA 02062 USA       (eq (opinion 'ADI) (opinion 'mike)) -> nil