home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 5.ddi / CLIBSRC2.ZIP / GETCH.CAS < prev    next >
Encoding:
Text File  |  1992-06-10  |  3.9 KB  |  142 lines

  1. /*---------------------------------------------------------------------------
  2.  * filename - getch.cas
  3.  *
  4.  * function(s)
  5.  *        getch   - gets character from console
  6.  *        getche  - gets character from console
  7.  *        ungetch - ungets character to console
  8.  *--------------------------------------------------------------------------*/
  9.  
  10. /*
  11.  *      C/C++ Run Time Library - Version 5.0
  12.  *
  13.  *      Copyright (c) 1987, 1992 by Borland International
  14.  *      All Rights Reserved.
  15.  *
  16.  */
  17.  
  18.  
  19. #pragma inline
  20. #include <conio.h>
  21. #include <stdio.h>
  22. #include <dos.h>
  23. #include <asmrules.h>
  24.  
  25. /* The following two variables are now located in kbhit.cas. */
  26.  
  27. extern unsigned char _cFlag;    /* Flag presence of un-gotten char */
  28. extern unsigned char _cChar;    /* The ungotten char               */
  29.  
  30. /*
  31.    if these routines are split into separate files, each
  32.    must extern char _unflag & _unchar, _unflag & _unchar must not be static.
  33. */
  34.  
  35. /*--------------------------------------------------------------------------*
  36.  
  37. Name            getch - gets character from console
  38.  
  39. Usage           int getch(void);
  40.  
  41. Prototype in    conio.h
  42.  
  43. Description     getch is a function that reads a single character directly
  44.                 from the console, without echoing.
  45.  
  46. Return value    getch and getche return the character read. There is no
  47.                 error return for these two functions.
  48.  
  49. *---------------------------------------------------------------------------*/
  50. int getch(void)
  51. {
  52.         if (_cFlag)
  53.         {
  54.                 _cFlag = 0;     /* Reset the flag       */
  55.                 _AL = _cChar;   /* Get the un-got char  */
  56.         }
  57.       else
  58.         {
  59.                 _AX = 0x0700;           /* Console input (no echo)      */
  60.                 geninterrupt(0x21);
  61.         }
  62.  
  63.         _AH = 0;                        /* Force it to be unsigned      */
  64.         return _AX;
  65. }
  66.  
  67.  
  68. /*--------------------------------------------------------------------------*
  69.  
  70. Name            getche - gets character from console
  71.  
  72. Usage           int getche(void);
  73.  
  74. Prototype in    conio.h
  75.  
  76. Description     getche is a function that reads and echoes a single
  77.                 character from the console.
  78.  
  79. Return value    getch and getche return the character read. There is no
  80.                 error return for these two functions.
  81.  
  82. *---------------------------------------------------------------------------*/
  83. asm _TEXT       segment
  84. asm             public __Ngetche
  85. asm __Ngetche   label near
  86. #if LPROG
  87. asm             pop     ax
  88. asm             push    cs
  89. asm             push    ax
  90. #endif
  91. asm             ends
  92.  
  93. int getche(void)
  94. {
  95.         int ch;
  96.  
  97.         if (_cFlag)                     /* Prevent possible double echoing */
  98.                 ch = getch();
  99.         else
  100.                 putch(ch = getch());
  101.  
  102.         return ch;
  103. }
  104.  
  105.  
  106. /*--------------------------------------------------------------------------*
  107.  
  108. Name            ungetch - ungets character to console
  109.  
  110. Usage           int ungetch(int c);
  111.  
  112. Prototype in    conio.h
  113.  
  114. Description     ungetch pushes the character c back to the console,
  115.                 causing c to be the next character read. The ungetch
  116.                 function fails if it is called more than once before
  117.                 the next read.
  118.  
  119. Return value    ungetch returns the character c if it is successful.
  120.                 return value of EOF indicates an error.
  121.  
  122. *---------------------------------------------------------------------------*/
  123. asm _TEXT       segment
  124. asm             public __Nungetch
  125. asm __Nungetch  label near
  126. #if LPROG
  127. asm             pop     ax
  128. asm             push    cs
  129. asm             push    ax
  130. #endif
  131. asm             ends
  132.  
  133. int ungetch(int c)
  134. {
  135.         if (_cFlag)
  136.                 return(EOF);
  137.  
  138.         _cFlag = 1;
  139.         _cChar = (unsigned char) c;
  140.         return _cChar;
  141. }
  142.