home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / games / backgmmn.zip / GETMS.C < prev    next >
Text File  |  1987-07-31  |  3KB  |  81 lines

  1.  
  2.  
  3. #include "ciao.h"
  4.  
  5.  
  6. /*========================================*/
  7. /*  getms(p, maxinput, wait, esc)         */
  8. /*  Simple getline function with special  */
  9. /*  input handling,  1 <= len <= maxinput */
  10. /*========================================*/ 
  11.  
  12. int getms( p, maxinput, wait, esc ) 
  13. char *p; 
  14. int maxinput; 
  15. void (* wait)();
  16. void (* esc)();
  17. {
  18.    static int len;
  19.    static int ch;
  20.  
  21.    len = -1;
  22.    if (maxinput < 1 || maxinput > 79) maxinput = 79;
  23.  
  24.    while (1)
  25.    {
  26.         ch = keyin( wait );  
  27.  
  28.         if (len < 0) len = 0;       /* don't destroy prompt by backing up */
  29.  
  30.         if (ch == '\n' || ch == '\r') 
  31.         {                           /* end of line?  don't store newline */
  32.                 *p = '\0';          /* mark it with a B for baby and me  */
  33.                 break;              /* and break out of while loop       */
  34.         }
  35.  
  36.         else if ((ch == '\b') || (ch == 127)) 
  37.         {                                /* backspace? rubout?  */
  38.                 if (len-- > 0) 
  39.                 {
  40.                         wink(127);       /* destructive bs      */
  41.                         p--;             /* delete from string  */
  42.                 }
  43.         }
  44.  
  45.         /*--------------------------------*/
  46.         /* SPECIAL ROUTINE FOR ESCAPE KEY */
  47.         /*--------------------------------*/
  48.  
  49.         else if (ch == '\033') 
  50.         {                                /* do ESC function */
  51.                 (* esc)();               /* routinely contains a longjmp */
  52.         }
  53.  
  54.         else if (ch == '\025' || ch == '\030') 
  55.         {                                /* Ctl-U, Ctl-X */
  56.                 while (len--) 
  57.                 {
  58.                         p--;
  59.                         wink(127);
  60.                 }
  61.         }
  62.         else if (len == maxinput) 
  63.         {                                /* test specials before testing len */
  64.                 bell();
  65.         }
  66.         else if (ch > 31 && ch < 127) 
  67.         {                                /* printable char? */
  68.                 wink(ch);                /* yes, echo it */
  69.                 *p++ = ch;               /* collect it */
  70.                 len++;                   /* keep track of it */
  71.         }
  72.         else 
  73.         {                                /* control chars? */
  74.                 bell();
  75.         }                
  76.    }
  77.    return( len );
  78.  
  79. } /* end: getms */
  80.  
  81.