home *** CD-ROM | disk | FTP | other *** search
/ Beijing Paradise BBS Backup / PARADISE.ISO / software / BBSDOORW / UUPC11XS.ZIP / LIB / SAFEIO.C < prev    next >
C/C++ Source or Header  |  1992-11-27  |  6KB  |  200 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    s a f e i o . c                                                 */
  3. /*                                                                    */
  4. /*    Console I/O functions for use during interrupt processing       */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------------*/
  8. /*    Since C I/O functions are not safe inside signal routines,      */
  9. /*    the code uses conditionals to use system-level DOS and OS/2     */
  10. /*    services.  Another option is to set global flags and do any     */
  11. /*    I/O operations outside the signal handler.                      */
  12. /*--------------------------------------------------------------------*/
  13.  
  14. #define __MSC                 /* Make Borland C++ 2.0 act like MS C  */
  15.  
  16. #include <stdio.h>
  17.  
  18. #if defined( FAMILYAPI )
  19. #if defined( WIN32 )
  20. #include <windows.h>
  21. #include <string.h>
  22. #else
  23.     #define INCL_NOCOMMON
  24.     #define INCL_NOPM
  25.     #define INCL_VIO
  26.     #define INCL_KBD
  27.     #include <os2.h>
  28.     #include <string.h>
  29. #endif
  30. #else /* FAMILYAPI */
  31. #include <conio.h>
  32.     #include <dos.h>
  33.     #include <bios.h>
  34. #endif
  35.  
  36. /*--------------------------------------------------------------------*/
  37. /*                    UUPC/extended include files                     */
  38. /*--------------------------------------------------------------------*/
  39.  
  40. #include "lib.h"
  41. #include "safeio.h"
  42.  
  43. /*--------------------------------------------------------------------*/
  44. /*                    Local Structures and defines                    */
  45. /*--------------------------------------------------------------------*/
  46.  
  47. #ifdef FAMILYAPI
  48. /* KBDKEYINFO structure, for KbdCharIn and KbdPeek */
  49.  
  50. #ifdef __GNUC__
  51. typedef struct _KBDKEYINFO {  /* kbci */
  52.    UCHAR chChar;
  53.    UCHAR chScan;
  54.    UCHAR fbStatus;
  55.    UCHAR bNlsShift;
  56.    USHORT   fsState;
  57.    ULONG time;
  58. }KBDKEYINFO;
  59.  
  60. #ifndef IO_WAIT
  61. #define IO_WAIT      0
  62. #endif
  63.  
  64. #ifndef FINAL_CHAR_IN
  65. #define FINAL_CHAR_IN       0x40
  66. #endif
  67.  
  68. #endif
  69. #endif
  70.  
  71. /*--------------------------------------------------------------------*/
  72. /*    s a f e i n                                                     */
  73. /*                                                                    */
  74. /*    Inputs a character using system level calls.  From MicroSoft    */
  75. /*    Programmer's Workbench QuickHelp samples                        */
  76. /*--------------------------------------------------------------------*/
  77.  
  78. int safein( void )
  79. {
  80. #ifdef _Windows
  81.    return getchar( );
  82. #else
  83. #if defined( FAMILYAPI )
  84. #if defined( WIN32 )
  85.    CHAR ch;
  86.    DWORD dwBytesRead;
  87.    HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
  88.  
  89.    ReadFile(hStdIn, &ch, 1, &dwBytesRead, NULL);      
  90.  
  91.    return ch;
  92. #else /* WIN32 */
  93.     KBDKEYINFO kki;
  94.  
  95.     KbdCharIn( &kki, IO_WAIT, 0 );
  96.     return kki.chChar;
  97. #endif /* WIN32 */
  98. #else /* FAMILYAPI */
  99.  
  100.     int c = (_bios_keybrd( _KEYBRD_READ ) & 0xff );
  101.     union REGS inregs, outregs;
  102.  
  103.     inregs.h.ah = 0x0e;
  104.     inregs.h.al = (char) c;
  105.     int86( 0x10, &inregs, &outregs );
  106.     return c;
  107.  
  108. #endif
  109. #endif
  110. } /* safein */
  111.  
  112. /*--------------------------------------------------------------------*/
  113. /*    s a f e p e e k                                                 */
  114. /*                                                                    */
  115. /*    Determine if a character is waiting at the keyboard for us.     */
  116. /*    Written by ahd based on safein (above).                         */
  117. /*--------------------------------------------------------------------*/
  118.  
  119. boolean safepeek( void )
  120. {
  121.  
  122. /*--------------------------------------------------------------------*/
  123. /*                         OS/2 keyboard peek                         */
  124. /*--------------------------------------------------------------------*/
  125. #ifdef _Windows
  126.    return 0;
  127. #else
  128. #if defined( FAMILYAPI )
  129. #ifdef WIN32
  130.    HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
  131.    INPUT_RECORD Buffer;
  132.    DWORD nEventsRead;
  133.  
  134.    PeekConsoleInput(hStdIn, &Buffer, 1, &nEventsRead);
  135.  
  136.    if (nEventsRead != 0 && Buffer.EventType == KEY_EVENT)
  137.       return TRUE;
  138.    return FALSE;
  139. #else
  140.     KBDKEYINFO kki;
  141.  
  142.     KbdPeek( &kki, 0 );
  143.     return (kki.fbStatus & FINAL_CHAR_IN);
  144. #endif /* WIN32 */
  145. #else
  146.  
  147. /*--------------------------------------------------------------------*/
  148. /*                         DOS Keyboard peek                          */
  149. /*--------------------------------------------------------------------*/
  150.  
  151.     return (_bios_keybrd( _KEYBRD_READY ) & 0xff );
  152. #endif
  153. #endif
  154.  
  155. } /* safepeek */
  156.  
  157. /*--------------------------------------------------------------------*/
  158. /*    s a f e f l u s h                                               */
  159. /*                                                                    */
  160. /*    Flush the keyboard look ahead buffer.                           */
  161. /*    Written by ahd based on safein (above).                         */
  162. /*--------------------------------------------------------------------*/
  163.  
  164. void safeflush( void )
  165. {
  166.  
  167. #ifdef _Windows
  168.    return;
  169. #else
  170.  
  171. /*--------------------------------------------------------------------*/
  172. /*                         OS/2 keyboard flush                        */
  173. /*--------------------------------------------------------------------*/
  174.  
  175. #if defined( FAMILYAPI )
  176. #ifdef WIN32
  177.    HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
  178.    FlushConsoleInputBuffer(hStdIn);
  179. #else
  180.     KbdFlushBuffer( 0 );      /* That's all!  (Makes you love rich
  181.                                  API's, doesn't it?)                 */
  182.  
  183. #endif /* WIN32 */
  184. #else
  185.  
  186. /*--------------------------------------------------------------------*/
  187. /*                         DOS Keyboard flush                         */
  188. /*--------------------------------------------------------------------*/
  189.  
  190.    union REGS regs;
  191.  
  192.    regs.h.ah = 0x0C;       /* Flush buffer, read keyboard            */
  193.    regs.h.al = 0x00;       /* Don't actually read keyboard           */
  194.    intdos( ®s, ®s ); /* Make it happen                         */
  195.  
  196. #endif
  197. #endif
  198.  
  199. } /* safeflush */
  200.