?
Solved

Unbuffered read in Solaris

Posted on 1998-07-23
6
Medium Priority
?
564 Views
Last Modified: 2013-12-21
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);
}
0
Comment
Question by:AustinKalb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 2

Expert Comment

by:seedy
ID: 2009138
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();
}  

0
 

Author Comment

by:AustinKalb
ID: 2009139
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.
0
 
LVL 2

Accepted Solution

by:
seedy earned 200 total points
ID: 2009140
Code Compiled under SunOS 5.x.  Should work under Solaris 2.6 too.
--- Code segment start ---
#include <stdio.h>
#include <termios.h>
#include <unistd.h>

main()
{
        struct termios  tTermios, tTermioSav;
        int     ii;

        if ( (tcgetattr(STDOUT_FILENO, &tTermios) < 0) ||
             (tcgetattr(STDOUT_FILENO, &tTermioSav) < 0)  ) {
                perror("tcgetattr");
                exit(1);
        }

        tTermios.c_lflag  &= (!ICANON);/* Turn off canonical processing */
        tTermios.c_cc[VMIN] = 1;        /* Get at least a char  */
        tTermios.c_cc[VTIME] = 0;       /* block till a char is read */
        if (tcsetattr(STDOUT_FILENO, TCSANOW, &tTermios) < 0 ) {
                perror("tcsetattr");
        }
        printf("Hit any character to continue ");

        if ( (ii = fgetc(stdin)) == EOF ) {
                perror("fgetc");
        }

        printf("\nThank you for typing %c\n", ii);
        if (tcsetattr(STDOUT_FILENO, TCSANOW, &tTermioSav) < 0 ) {
                perror("tcsetattr");
        }
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:AustinKalb
ID: 2009141
Much better. Compiled fine. Worked great. Easy to follow. Thanks!
0
 

Author Comment

by:AustinKalb
ID: 2009142
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.
0
 
LVL 2

Expert Comment

by:seedy
ID: 2009143
> 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.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses
Course of the Month12 days, 10 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question