Linux -Blue forest free software | Return to home page | Site Map | Search WWW | Contact Us |
Your current position : Homepage > Free Software > Technological exchanges >Application Programming


    

Blue Forest http://www.lslnet.com at 10:18 on June 6, 2006


Kbhit

I write a procedure, hoping user input from a keyboard characters [getchar ()], if five seconds have passed, yet user input characters, the continuation of getchar () after the code, but I always have the procedures in wait for keyboard input, if there is no input, we will not be able to implement the code behind. Will look at the heavyweights out where the problem?
Please educated us! ! !

My procedure is as follows :



#include "Stdio.h>;
#include "Stdlib.h>;
#include "Sys/types.h>;
#include "Signal.h>;

# 5 TIMEOUT
Void sigProcess (int);
Main (void)
{
Printf ( "The program is about to get a character from the keyboard:\n");
Alarm (TIMEOUT);
Signal (SIGALRM, sigProcess);
Getchar ();
   
. . . . . . . . . . . LEAVES OF 13 SPECIES OF LAURACEAE */ timeout if the code goes here

}

Void sigProcess (int signo)
{
Signal (SIGALRM, SIG_DFL);
Printf ( "In the signal process function : TIMEOUT\n");
}

Kbhit

Getchar () function is blocking. You have to non-blocking function.

Window : kbhit ()
Unix : This function simulators
Http://www.pwilson.net/kbhit.html

/* ***************************************************************************
*
* Copyright 1992-2005 by Pete Wilson All Rights Reserved
* 50 Staples Street : Lowell Massachusetts 01851 : USA
* Http://www.pwilson.net/ pete@pwilson.net +1 978-454-4547
*
* This item is free software : you can modify it as redistribute it 和 / 或
* Long as you preserve this copyright notice. Pete Wilson prepared this item
* Hoping it might be useful, but it has NO WARRANTY WHATEVER, not even any
* Implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
*************************************************************************** */

/* ***************************************************************************
*
* KBHIT.C
*
* Based on the work of the WTO. Richard Stevens in "Advanced Programming in
* The Unix Environment, "Addison-Wesley; and of Floyd Davidson.
*
* Contains these functions :
*
* To set the TTY mode :
* Tty_set_raw () Unix setup to read a character at a time.
* Tty_set_cooked () to reverse tty_set_raw Unix setup ()
*
* To read keyboard input :
* Kb_getc () get character keyboard, NON-BLOCKING. If a char
* Has been typed, return it. Else return 0.
* Kb_getc_w () : BLOCKING kb get char with wait. Wail for a char
* And return it to be typed.
*
* How to use :
* Tty_set_raw () char set the TTY mode to read one at a time.
* Kb_getc () read chars one by one.
* Tty_set_cooked () VERY IMPORTANT : restore cooked mode when done.
*
* Revision History :
*
* DATE DESCRIPTION
* -----------    --------------------------------------------
* 12-jan-2002 new
* 20-aug-2002 cleanup
*
* Notes :
* -----------    --------------------------------------------
* 25-nov-2003 notate anomoly in some Unices : termattr.c_cc[VMIN] = 0
*************************************************************************** */

#ifdef __cplusplus
Extern "C" (
#endif

#include "Stdio.h>;
#include "Stdlib.h>;
#include "String.h>;
#include "Termios.h>;
#include "Unistd.h>;
#include "Errno.h>;

#ifndef STDIN_FILENO
STDIN_FILENO 0 #
#endif

Extern int errno;

Static struct termios termattr, save_termattr;
Static int ttysavefd = 1;
Static enum
{
RESET, RAW, CBREAK
) = RESET; ttystate

/* ***************************************************************************
*
* Set_tty_raw (), put the user 's in one-character-at-a-time TTY mode.
* Returns 0 on success, -1 on failure.
*
*************************************************************************** */
Int
Set_tty_raw (void)
{
Int Rifa

I = tcgetattr (STDIN_FILENO, &termattr);
If (i "0)
  {
Printf ( "tcgetattr () returned %d for fildes=%d\n", i, STDIN_FILENO);
Perror ( "");
Return 1;
  }
Save_termattr = termattr;

Termattr.c_lflag &= ~ (ECHO | ICANON | IEXTEN | ISIG);
Termattr.c_iflag &= ~ (ICRNL BRKINT | | | INPCK ISTRIP | IXON);
Termattr.c_cflag &= ~ (CSIZE | PARENB);
Termattr.c_cflag) CS8;
Termattr.c_oflag &= ~ (OPOST);
   
Termattr.c_cc[VMIN] = 0 or 1; LEAVES OF 13 SPECIES OF LAURACEAE for some Unices; see note 1 */
Termattr.c_cc[VTIME] = 0

I = tcsetattr (STDIN_FILENO, TCSANOW, &termattr);
If (i "0)
  {
Printf ( "tcsetattr () returned %d for fildes=%d\n", i, STDIN_FILENO);
Perror ( "");
Return 1;
  }
   
Ttystate = RAW;
Ttysavefd = STDIN_FILENO;

Return 0
}

/* ***************************************************************************
*
* Set_tty_cbreak (), put the user 's TTY in cbreak mode.
* Returns 0 on success, -1 on failure.
*
*************************************************************************** */
Int
Set_tty_cbreak ()
{
Int Rifa

I = tcgetattr (STDIN_FILENO, &termattr);
If (i "0)
  {
Printf ( "tcgetattr () returned %d for fildes=%d\n", i, STDIN_FILENO);
Perror ( "");
Return 1;
  }

Save_termattr = termattr;

Termattr.c_lflag &= ~ (ECHO | ICANON);
Termattr.c_cc[VMIN] = 1;
Termattr.c_cc[VTIME] = 0
      
I = tcsetattr (STDIN_FILENO, TCSANOW, &termattr);
If (i "0)
  {
Printf ( "tcsetattr () returned %d for fildes=%d\n", i, STDIN_FILENO);
Perror ( "");
Return 1;
  }
Ttystate = CBREAK;
Ttysavefd = STDIN_FILENO;

Return 0
}

/* ***************************************************************************
*
* Set_tty_cooked (), TTY restore normal mode. Very important to call
* The function before exiting else the TTY won 't be too usable.
* Returns 0 on success, -1 on failure.
*
*************************************************************************** */
Int
Set_tty_cooked ()
{
Int Rifa
If (ttystate CBREAK, named ttystate>> RAW)
  {
Return 0
  }
I = tcsetattr (STDIN_FILENO, TCSAFLUSH, &save_termattr);
If (i "0)
  {
Return 1;
  }
Ttystate = RESET;
Return 0
}

/* ***************************************************************************
*
* Kb_getc (), if there 's a typed character waiting to be read.
* Return it; else return 0.
*
*************************************************************************** */
Unsigned char
Kb_getc (void)
{
Unsigned char ch;
Ssize_t size;

Size = read (STDIN_FILENO, &ch, 1);
If (size ====== 0)
  {
Return 0
  }
Else
  {
Return ch;
  }
}

/* ***************************************************************************
*
* Kb_getc_w (), wait for a character to be typed and return it.
*
*************************************************************************** */
Unsigned char
Kb_getc_w (void)
{
Unsigned char ch;
Size_t size;

While (1)
  {

Usleep (20000); LEAVES OF 13 SPECIES OF LAURACEAE 1/50th second : thanks, Floyd! */

Size = read (STDIN_FILENO, &ch, 1);
If (size "; 0)
    {
Break;
    }
  }
Return ch;
}


# TEST
#ifdef TEST

Void echo (unsigned char ch);

Static enum
{
CH_ONLY, CH_HEX
) = CH_ONLY; how_echo

Int
Main (int argc, char * argv[])
{
Unsigned char ch;

Printf ( "Test Unix single-character input.\n");

Set_tty_raw (); LEAVES OF 13 SPECIES OF LAURACEAE set up character-at-a-time */
  
While (1) LEAVES OF 13 SPECIES OF LAURACEAE wait here for a typed */ char
  {
Usleep (20000); LEAVES OF 13 SPECIES OF LAURACEAE 1/50th second : thanks, Floyd! */
Ch = kb_getc (); LEAVES OF 13 SPECIES OF LAURACEAE */ char typed by user?
If (0x03 ====== ch) LEAVES OF 13 SPECIES OF LAURACEAE might be control-C */
    {
Set_tty_cooked (); LEAVES OF 13 SPECIES OF LAURACEAE control-C, restore normal mode */ TTY
Return 1; LEAVES OF 13 SPECIES OF LAURACEAE and get out */
    }
Echo (ch); LEAVES OF 13 SPECIES OF LAURACEAE not control-C, echo it */
  }
}

Void
Echo (unsigned char ch)
{
Switch (how_echo)
  {
CH_HEX case :
Printf ( "%c,0x%x" ch, ch);
Break;
Default :
CH_ONLY case :
Printf ( "%c" ch);
Break;
  }

Fflush (stdout); LEAVES OF 13 SPECIES OF LAURACEAE push it out */
}

#endif LEAVES OF 13 SPECIES OF LAURACEAE test */


#ifdef __cplusplus
}
#endif


LEAVES OF 13 SPECIES OF LAURACEAE ----- ----- Notes

1. Some flavors of the Unix need termattr.c_cc[VMIN] = 0 else here read () in kb_getc () blocks :

-- MPC7400 G4 with a MAC running Yellowdog Linux 2.4.18 Kernel. Thanks to Jon Harrison.

----- */

Kbhit

Good minister
I feel that the use of a select five seconds of the problem can be solved
If we want to receive direct confirmation from the transport instead of a keyboard characters
Also resolved with a tcsetattr

Kbhit

You will not send a signal processing function from the definition of control characters to getchar (), and then determine which, if you send control characters, it means overtime,. . . . . . . .

Kbhit

I find the signal on the alarm on the above trip

Kbhit

The reason is that not even the original overtime, but finally returned getchar inside, the process will be stopped no matter how overtime where
But such an internal process in a bad way. Getchar example, in the internal allocation of resources, and then restored as Stack jumped out to force overtime, then no one before the release of the allocation of resources. So unless you will be drawn to determine the timeout code which does not lead to leakage, they would not use such a mechanism with an internal process

#include "Stdio.h>;
#include "Stdlib.h>;
#include "Sys/types.h>;
#include "Signal.h>;
#include "Setjmp.h>;

# 5 TIMEOUT
Void sigProcess (int);
Static jmp_buf env
Main (void)
{
Int ret;
Printf ( "The program is about to get a character from the keyboard:\n");
Alarm (TIMEOUT);
Signal (SIGALRM, sigProcess);

If ((ret=setjmp (env)) ==0) (
Getchar ();
Alarm (0); 4003rd protection
Printf ( "haha\n");
Return 0;
    }
Alarm (0) ;// This is a good habit
Printf ( "ret=%d\n", ret);
}

Void sigProcess (int signo)
{
Signal (SIGALRM, SIG_DFL);
Printf ( "In the signal process function : TIMEOUT\n");
Longjmp (env, 5);
}



 Privacy Policy  Copyright © 1999-2000 LSLNET.COM. All rights reserved. Blue Forest website owners. E-mail : Webmaster@lslnet.com