home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 414_02 / private / _sysgetc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-17  |  3.0 KB  |  144 lines

  1. #define    CURSES_LIBRARY    1
  2. #include <curses.h>
  3.  
  4. #ifdef UNIX
  5. #include <signal.h>
  6. #endif
  7.  
  8. #ifdef PDCDEBUG
  9. char *rcsid__sysgetc = "$Header: C:\CURSES\private\RCS\_sysgetc.c 2.1 1993/06/18 20:22:44 MH Rel MH $";
  10. #endif
  11.  
  12.  
  13.  
  14.  
  15. /*man-start*********************************************************************
  16.  
  17.   PDC_sysgetch()    - Return a character using default system routines.
  18.  
  19.   PDCurses Description:
  20.      This is a private PDCurses function.
  21.  
  22.      Gets a character without normal ^S, ^Q, ^P and ^C interpretation
  23.      and returns it.  If keypad mode is active for the designated
  24.      window, function key translation will be performed. Otherwise,
  25.      function keys are ignored. If nodelay mode is active in the
  26.      window, then sysgetch() returns -1 if no character is
  27.      available.
  28.  
  29.   PDCurses Return Value:
  30.      This function returns OK upon success otherwise ERR is returned.
  31.  
  32.   PDCurses Errors:
  33.      No errors are defined for this routine.
  34.  
  35.   Portability:
  36.      PDCurses    int    PDC_sysgetch( void );
  37.  
  38. **man-end**********************************************************************/
  39.  
  40. #ifdef UNIX
  41. static bool alarmed;
  42. #endif
  43.  
  44. int    PDC_sysgetch(void)
  45. {
  46. extern    WINDOW*    _getch_win_;
  47. /* extern    WINDOW*    w;*/   /* w defined in wgetch() as static - _getch_win_ */
  48.                         /* is the same window - all references to w changed*/
  49.                         /* to _getch_win_ - marked with @@ */
  50.  
  51.     signed int c;
  52.  
  53. #ifdef UNIX
  54.     void (*oldsig)();
  55.     void sigalrm();
  56.     char keybuf[10];
  57.     char *bufp=keybuf;
  58.     int val;
  59.     register int i=0;
  60. #define MATCH_FALSE 0
  61. #define MATCH_PARTIAL 1
  62. #define MATCH_TRUE 2
  63.     char match;
  64. #endif
  65.  
  66. #ifdef PDCDEBUG
  67.     if (trace_on) PDC_debug("PDC_sysgetch() - called\n");
  68. #endif
  69.  
  70.     if (_getch_win_ == (WINDOW *)NULL)  /* @@ */
  71.         return (-1);
  72.  
  73. #ifdef UNIX
  74. /* INCOMPLETE */
  75.     oldsig = signal(SIGALRM,sigalrm);
  76.     alarmed = FALSE;
  77.     while(1)
  78.         {
  79.         c = getchar();
  80.         if (c != EOF)
  81.             {
  82.             *(bufp+i++) = c;
  83.             *(bufp+i) = '\0';
  84.             }
  85.         if (alarmed)
  86.             break;
  87.         val = find_key(keybuf,i);
  88.         if (val == MATCH_FALSE)
  89.             {
  90.             alarm(0);
  91.             signal(SIGALRM,oldsig);
  92.             return(keybuf[0]);
  93.             }
  94.         if (val != MATCH_PARTIAL)
  95.             {
  96.             alarm(0);
  97.             signal(SIGALRM,oldsig);
  98.             return(val);
  99.             }
  100.         alarm(1);
  101.         }
  102.     alarm(0);
  103.     signal(SIGALRM,oldsig);
  104. /* INCOMPLETE */
  105.     return(c);
  106. #else
  107.     while (1)
  108.     {
  109.         c = PDC_get_bios_key();
  110. #if    defined (DOS) || defined (OS2)
  111.         /*
  112.          * Return the key if it is not a special key.
  113.          */
  114.         if (c & A_CHARTEXT)
  115.             return (c & A_CHARTEXT);
  116. #endif
  117.         if ((c = PDC_validchar(c)) >= 0)
  118.         {
  119.             return (c);        /* get & check next char */
  120.         }
  121.     }
  122. #endif
  123. }
  124. #ifdef UNIX
  125. static void sigalrm()
  126. {
  127.     alarmed = TRUE;
  128.     signal(SIGALRM,sigalrm);
  129. }
  130. static int find_key(char *seq,int len)
  131. {
  132.     register int i;
  133.  
  134.     for (i=0;i<_cursvar.number_keys;i++)
  135.         {
  136.         if (strcmp(seq,_cursvar.key_seq[i]) == 0)
  137.             return(_cursvar.key_num[i]);
  138.         if (memcmp(seq,_cursvar.key_seq[i],len) == 0)
  139.             return(MATCH_PARTIAL);
  140.         }
  141.     return(MATCH_FALSE);
  142. }
  143. #endif
  144.