Link to home
Start Free TrialLog in
Avatar of AustinKalb
AustinKalb

asked on

Unbuffered read in Solaris

I cant seem to get the code below to read the getchar statement without buffering and waiting for a CR on Solaris 2.6. This has to be a common problem, for example, how would one read a function key pressed if one had to wait for a cr. I'm sure there is a simple solution.

BTW, curses can easily perform this, but is not thread safe as the normal terminal I/O calls are.

#include <stdio.h>

main()      
{
   int c;
   printf("Hit any character to continue\n");            

   /* This next call should return without a first typing a CR */
   c = getchar();            
   /* but it doesn't */

   printf("Thank you for typing %c.\n", c);
   exit(0);
}
Avatar of seedy
seedy

Simple solution use curses as shown below.  Or use termios - read man pages.

#include <curses.h>

main()
{
        initscr();
        cbreak();
        noecho();
        mvprintw(10, 10, "Hit any character to continue");
        refresh();
        mvprintw(11, 10, "Thank you for typing %c", getch());
        refresh();
        endwin();
}  

Avatar of AustinKalb

ASKER

I realize curses will work, but it is not thread safe, which I need. I am looging for the simple solution using termio. The termio man pages read like stereo instructions. How bout an example that will compile under solaris 2.6 using termio. Thanks.
ASKER CERTIFIED SOLUTION
Avatar of seedy
seedy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Much better. Compiled fine. Worked great. Easy to follow. Thanks!
On Solaris 2.6, I am using the code based on the above. I have wwritten a character based application for speeed. However, it seems that only some of the keys really get to the application. Others, like PageUp, make the terminal scroll. How do I get keys like PageUp and PageDown to the application.

Also, using the above code, after presumably getting the keyboard to function the way I want, I would like to capture keys like pageup and pagedown, while simultaniously still allowing text input on a command line. That is, I want to use the function keys to scroll, but have other input read, kinda like a word processor, but character based.

Thanks.
> How do I get keys like PageUp and PageDown to the application
This largely depends on the terminal that you use and NOT on the application.  Make sure that these keys are readable by any other applications (shell or 'od -c', etc).
> simultaniously still allowing text input on a command line
What do you mean here?  Who will be "reading" the "command" line?

Also, most of these keys generate a multi character sequence.  For example on my Win 95 terminal application, the F1 key generates three characters: <ESC>OP

Though I would like to continue our conversation here, that will not be very appropriate.  Why don't you post a new question; this will also be seen by many experts, increasing your chance of getting a correct answer fast.