home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / EXTRAS / UUCODE / UUPC / TEST / UPC12ES1.ZIP / LIB / safeio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-30  |  8.6 KB  |  261 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. /*    Changes Copyright (c) 1989-1993 by Kendra Electronic            */
  9. /*    Wonderworks.                                                    */
  10. /*                                                                    */
  11. /*    All rights reserved except those explicitly granted by the      */
  12. /*    UUPC/extended license agreement.                                */
  13. /*--------------------------------------------------------------------*/
  14.  
  15. /*--------------------------------------------------------------------*/
  16. /*                          RCS Information                           */
  17. /*--------------------------------------------------------------------*/
  18.  
  19. /*
  20.  *    $Id: safeio.c 1.7 1993/10/30 22:27:57 rommel Exp $
  21.  *
  22.  *    Revision history:
  23.  *    $Log: safeio.c $
  24.  *     Revision 1.7  1993/10/30  22:27:57  rommel
  25.  *     Handle missing define
  26.  *
  27.  *     Revision 1.6  1993/10/12  00:49:39  ahd
  28.  *     Normalize comments
  29.  *
  30.  *     Revision 1.5  1993/10/03  00:03:45  ahd
  31.  *     Only use currentfile() under Windows NT
  32.  *
  33.  *     Revision 1.4  1993/09/20  04:39:51  ahd
  34.  *     OS/2 2.x support
  35.  *
  36.  *     Revision 1.3  1993/07/20  21:42:43  dmwatt
  37.  *     Don't rely on standard I/O under Windows/NT
  38.  *
  39.  */
  40.  
  41. /*--------------------------------------------------------------------*/
  42. /*    Since C I/O functions are not safe inside signal routines,      */
  43. /*    the code uses conditionals to use system-level DOS and OS/2     */
  44. /*    services.  Another option is to set global flags and do any     */
  45. /*    I/O operations outside the signal handler.                      */
  46. /*--------------------------------------------------------------------*/
  47.  
  48. #define __MSC                 /* Make Borland C++ 2.0 act like MS C   */
  49.  
  50. #include <stdio.h>
  51.  
  52. #if defined( WIN32 )
  53.     #include <windows.h>
  54.     #include <string.h>
  55. #elif defined( FAMILYAPI ) || defined(__OS2__)
  56.     #define INCL_NOCOMMON
  57.     #define INCL_NOPM
  58.     #define INCL_VIO
  59.     #define INCL_KBD
  60.     #include <os2.h>
  61.     #include <string.h>
  62. #else /* FAMILYAPI */
  63.     #include <conio.h>
  64.     #include <dos.h>
  65.     #include <bios.h>
  66. #endif
  67.  
  68. /*--------------------------------------------------------------------*/
  69. /*                    UUPC/extended include files                     */
  70. /*--------------------------------------------------------------------*/
  71.  
  72. #include "lib.h"
  73. #include "safeio.h"
  74.  
  75. /*--------------------------------------------------------------------*/
  76. /*                          Global variables                          */
  77. /*--------------------------------------------------------------------*/
  78.  
  79. #if defined( WIN32 )
  80. currentfile();
  81. #endif
  82.  
  83. /*--------------------------------------------------------------------*/
  84. /*    s a f e i n                                                     */
  85. /*                                                                    */
  86. /*    Inputs a character using system level calls.  From MicroSoft    */
  87. /*    Programmer's Workbench QuickHelp samples                        */
  88. /*--------------------------------------------------------------------*/
  89.  
  90. #if defined(WIN32)
  91.  
  92. static HANDLE hConsoleIn = INVALID_HANDLE_VALUE;
  93.  
  94. /*--------------------------------------------------------------------*/
  95. /*       I n i t C o n s o l e I n p u t H a n d l e                  */
  96. /*                                                                    */
  97. /*       Initialize Window NT console handle allow reading            */
  98. /*       from console when stdin is redirected.                       */
  99. /*--------------------------------------------------------------------*/
  100.  
  101. void InitConsoleInputHandle(void)
  102. {
  103.    hConsoleIn = CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE, 0, NULL,
  104.       OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  105.  
  106.    if (hConsoleIn == INVALID_HANDLE_VALUE) {
  107.       printmsg(0, "InitConsoleInputHandle:  could not open console handles!");
  108.       panic();
  109.    }
  110. }
  111. #endif
  112.  
  113.  
  114. int safein( void )
  115. {
  116. #ifdef _Windows
  117.  
  118. /*--------------------------------------------------------------------*/
  119. /*                       Windows get character                        */
  120. /*--------------------------------------------------------------------*/
  121.  
  122.    return getchar( );
  123.  
  124. #elif defined( WIN32 )
  125.  
  126. /*--------------------------------------------------------------------*/
  127. /*                      Windows NT get character                      */
  128. /*--------------------------------------------------------------------*/
  129.  
  130.    CHAR ch;
  131.    DWORD dwBytesRead;
  132.  
  133.    if (hConsoleIn == INVALID_HANDLE_VALUE)
  134.       InitConsoleInputHandle();
  135.  
  136.    ReadFile(hConsoleIn, &ch, 1, &dwBytesRead, NULL);
  137.  
  138.    return ch;
  139.  
  140. #elif defined( FAMILYAPI ) || defined( __OS2__ )
  141.  
  142. /*--------------------------------------------------------------------*/
  143. /*                         OS/2 Get character                         */
  144. /*--------------------------------------------------------------------*/
  145.  
  146.     KBDKEYINFO kki;
  147.  
  148.     KbdCharIn( &kki, IO_WAIT, 0 );
  149.     return kki.chChar;
  150.  
  151. #else /* FAMILYAPI */
  152.  
  153. /*--------------------------------------------------------------------*/
  154. /*                         DOS get character                          */
  155. /*--------------------------------------------------------------------*/
  156.  
  157.     int c = (_bios_keybrd( _KEYBRD_READ ) & 0xff );
  158.     union REGS inregs, outregs;
  159.  
  160.     inregs.h.ah = 0x0e;
  161.     inregs.h.al = (char) c;
  162.     int86( 0x10, &inregs, &outregs );
  163.     return c;
  164.  
  165. #endif
  166.  
  167. } /* safein */
  168.  
  169. /*--------------------------------------------------------------------*/
  170. /*    s a f e p e e k                                                 */
  171. /*                                                                    */
  172. /*    Determine if a character is waiting at the keyboard for us.     */
  173. /*    Written by ahd based on safein (above).                         */
  174. /*--------------------------------------------------------------------*/
  175.  
  176. boolean safepeek( void )
  177. {
  178.  
  179. #ifdef _Windows
  180.  
  181.    return 0;
  182.  
  183. #elif defined(WIN32)
  184.  
  185.    INPUT_RECORD Buffer;
  186.    DWORD nEventsRead;
  187.  
  188.    if (hConsoleIn == INVALID_HANDLE_VALUE)
  189.       InitConsoleInputHandle();
  190.  
  191.    PeekConsoleInput(hConsoleIn, &Buffer, 1, &nEventsRead);
  192.  
  193.    if (nEventsRead != 0 && Buffer.EventType == KEY_EVENT)
  194.       return TRUE;
  195.    return FALSE;
  196.  
  197. #elif defined( FAMILYAPI ) || defined(__OS2__)
  198.  
  199.     KBDKEYINFO kki;
  200.  
  201.     KbdPeek( &kki, 0 );
  202.  
  203. #if defined(KBDTRF_FINAL_CHAR_IN)
  204.     #define FINAL_CHAR_IN KBDTRF_FINAL_CHAR_IN
  205. #endif
  206.     return (kki.fbStatus & FINAL_CHAR_IN);
  207.  
  208. #else /* FAMILYAPI */
  209.  
  210. /*--------------------------------------------------------------------*/
  211. /*                         DOS Keyboard peek                          */
  212. /*--------------------------------------------------------------------*/
  213.  
  214.     return (_bios_keybrd( _KEYBRD_READY ) & 0xff );
  215.  
  216. #endif /* _Windows */
  217.  
  218. } /* safepeek */
  219.  
  220. /*--------------------------------------------------------------------*/
  221. /*    s a f e f l u s h                                               */
  222. /*                                                                    */
  223. /*    Flush the keyboard look ahead buffer.                           */
  224. /*    Written by ahd based on safein (above).                         */
  225. /*--------------------------------------------------------------------*/
  226.  
  227. void safeflush( void )
  228. {
  229.  
  230. #ifdef _Windows
  231.  
  232.    return;
  233.  
  234. #elif defined(WIN32)
  235.  
  236.    if (hConsoleIn == INVALID_HANDLE_VALUE)
  237.       InitConsoleInputHandle();
  238.  
  239.    FlushConsoleInputBuffer(hConsoleIn);
  240.  
  241. #elif defined( FAMILYAPI ) || defined(__OS2__)
  242.  
  243.     KbdFlushBuffer( 0 );      /* That's all!  (Makes you love rich
  244.                                  API's, doesn't it?)                  */
  245.  
  246. #else
  247.  
  248. /*--------------------------------------------------------------------*/
  249. /*                         DOS Keyboard flush                         */
  250. /*--------------------------------------------------------------------*/
  251.  
  252.    union REGS regs;
  253.  
  254.    regs.h.ah = 0x0C;       /* Flush buffer, read keyboard             */
  255.    regs.h.al = 0x00;       /* Don't actually read keyboard            */
  256.    intdos( ®s, ®s ); /* Make it happen                          */
  257.  
  258. #endif /* _Windows */
  259.  
  260. } /* safeflush */
  261.