[comp.lang.c] 4.3 curses - how to discard "premature" input?

xwu@pollux.usc.edu (Xinhua Wu) (02/14/89)

For the following program, how can I discard any premature input typed in
before the "Press any character: " prompt appears?

------------
#include <curses.h>

main()
{
  int ch;

  initscr();
  crmode();
  addstr("Press any character: ");
sleep(3);
  refresh();
  ch = getch();
  printw("\n\n\nThe character entered was a '%c'.\n", ch);
  refresh();
  endwin();
}
-------------

"sleep(3)" is added just to illustrate the point.  In practice, one often
has to wait for a menu to come up, etc.  But during the waiting one might
inadvertently type something (or hit a return) which should be discarded.

Thanks in advance for any help/comments.

------
Xinhua Wu
xwu@cse.usc.edu

xwu@pollux.usc.edu (Xinhua Wu) (02/16/89)

For the following program, how can I discard any premature input typed in
before the "Press any character: " prompt appears?

------------
#include <curses.h>

main()
{
  int ch;

  initscr();
  crmode();
  addstr("Press any character: ");
sleep(3);
  refresh();
  ch = getch();
  printw("\n\n\nThe character entered was a '%c'.\n", ch);
  refresh();
  endwin();
}
-------------

"sleep(3)" has been added just to illustrate the point.  In practice, one often
has to wait for a menu to come up, etc.  But during the waiting one might
inadvertently type something (or hit a return) which often should be discarded.

Thanks in advance for any help/comments.

------
Xinhua Wu
xwu@cse.usc.edu

bkbarret@sactoh0.UUCP (Brent K. Barrett) (02/18/89)

In article <15336@oberon.USC.EDU>, xwu@pollux.usc.edu (Xinhua Wu) writes:
> For the following program, how can I discard any premature input typed in
> before the "Press any character: " prompt appears?
  {gobble, gobble}
> inadvertently type something (or hit a return) which should be discarded.
> 
> Thanks in advance for any help/comments.

 Have you tried flushing the input buffer? Under MSDOS, it would be
something like fflush(stdin); to remove unwanted characters in the
input buffer. If this was placed right before the getch() call, or
where ever you wanted to dump excess input, the characters in the
input buffer would be removed.

-- 
 "Somebody help me! I'm trapped in this computer!"
  
 Brent Barrett ..pacbell!sactoh0!bkbarret GEMAIL: B.K.BARRETT

guy@auspex.UUCP (Guy Harris) (02/19/89)

> Have you tried flushing the input buffer? Under MSDOS, it would be
>something like fflush(stdin); to remove unwanted characters in the
>input buffer.

Unfortunately, he's doing that stuff under UNIX, not MS-DOS, and:

	1) "fflush" on an input stream, on some UNIX C implementations,
	   doesn't do a damn thing; the May 13, 1988 dpANS indicates that the
	   action of "fflush" on something other than "an output stream
	   or an update stream in which the most recent operation was
	   output" is undefined.  (It appears to do what you want in
	   the S5R3 implementation - it throws away the stuff in the
	   input buffer.  It does the same in the SunOS 4.0
	   implementation.  It does not, however, do so in the 4.3BSD
	   implementation.)

	2) Tere's more than one input buffer that he'd want to flush. 
	   There's the one maintained by standard I/O; there's also the
	   one maintained by the terminal driver software, and flushing
	   the latter one requires that you perform an "ioctl" function
	   specific to terminals.  The standard I/O library doesn't
	   perform that function.

karl@haddock.ima.isc.com (Karl Heuer) (02/22/89)

In article <1047@auspex.UUCP> guy@auspex.UUCP (Guy Harris) writes:
>[fflush(stdin) doesn't work in 4.3BSD or ANSI C, and in any case doesn't
>touch the characters buffered at the driver level.]

Since there is no standard way to do this, I created my own interface (named
`clrbfi', after the TOPS-10 ttcall with the same functionality) which serves
as a portable wrapper around a non-portable operation.  The enclosed code
should work on BSD, USG, and V8 systems.  I'd like to include the conditional
code for MSDOS, too, but I don't know how to do it there.  (Suggestions by
e-mail, please.)

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint
-------- cut here --------
/*
 * Clear input buffer on terminal.  Result is 0 if successful, else -1 (e.g.
 * if the specified descriptor or stream is not a tty).
 */
#if defined(_S_USG)
#include <sys/termio.h>
#define	ioarg	struct termio
#define	GET	TCGETA
#define	SET	TCSETAF
#else
#include <sgtty.h>
#define	ioarg	struct sgttyb
#define	GET	TIOCGETP
#define	SET	TIOCSETP
#endif

extern int ioctl();

int clrbfi(f) int f; {
    ioarg io;
    return ((ioctl(f, GET, &io) >= 0 && ioctl(f, SET, &io) >= 0) ? 0 : -1);
}

#include <stdio.h>
int fclrbfi(fp) FILE *fp; {
    fp->_cnt = 0;
    return (clrbfi(fileno(fp)));
}