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