home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c222 / 1.ddi / SOURCE / CLIB / KB_FIELD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-17  |  3.8 KB  |  199 lines

  1. /*********************
  2.  *
  3.  *  kb_field.c - keyboard input field functions.
  4.  *
  5.  *  Purpose: This file contains input routines for the console keyboard.
  6.  *           interrupts of the IBM PC.
  7.  *
  8.  *  Blackstar C Function Library
  9.  *  (c) Copyright 1985,1989 Sterling Castle Software
  10.  *
  11.  *******/
  12.  
  13. #include <string.h>
  14. #include <ctype.h>
  15. #include "blackstr.h"
  16. #include "kb_head.h"
  17. #include "sc_head.h"
  18.  
  19.  
  20. /********
  21.  *
  22.  *   kb_getf(str,n) - get string from field width n
  23.  *
  24.  **/
  25.  
  26. int kb_getf(char *str, int n)
  27. {
  28.     int c,ret;
  29.     extern char chinf_,insmodef_;
  30.     char *inptr,*ptr,inp[MAX_LN];
  31.  
  32.     inptr = inp;
  33.     memset(inp,NUL,MAX_LN);
  34.     *inptr = NUL;
  35.     ret=FALSE;
  36.     ut_push(wrapf_);    /* turn wrap off for kb_getf */
  37.     wrapf_ = FALSE;
  38.     sc_winpush();
  39.     sc_windo(sc_col_,sc_row_,sc_col_+n-1,sc_row_);
  40.     while(!ret) {       /* till we're done*/
  41.     ut_push(echof_);
  42.     echof_ = FALSE;
  43.     c = kb_getch();               /* get character from keyboard */
  44.     echof_ = ut_pop();
  45.     switch (c) {
  46.       case CUR_RT:
  47.       case CTRL_D:
  48.           if((sc_col_==colen_)&& (kb_isfun(c)))
  49.           ret=TRUE;
  50.           else {
  51.           sc_movcur(1,0);
  52.           ++inptr;
  53.           }
  54.           break;
  55.       case BKSP:
  56.       case CUR_LF:
  57.       case CTRL_S:
  58.           if((sc_col_==colst_)&&(kb_isfun(c)))
  59.           ret=TRUE;
  60.           else {
  61.           sc_movcur(-1,0);
  62.           --inptr;
  63.           inptr=max(inp,inptr);
  64.           }
  65.           break;
  66.       case DEL:
  67.           if(strlen(inp)<n)
  68.           sc_movcur(-1,0);
  69.           --inptr;
  70.           inptr= max(inp,inptr);
  71.       case CTRL_G:
  72.           st_movl(inptr,1);       /* move string left */
  73.           ut_push(sc_col_);
  74.           sc_putsfl(inptr,n);     /* put field w clear*/
  75.           sc_col_=ut_pop();
  76.           sc_setcur(sc_col_,sc_row_);
  77.           chinf_=TRUE;
  78.           break;
  79.       case CTRL_V:
  80.       case INS:
  81.           insmodef_= (insmodef_-1) ? TRUE:FALSE;
  82.           break;
  83.       case HOME:
  84.           sc_setcur(colst_,rowst_);
  85.           break;
  86.       case END_KY:
  87.           sc_setcur(colen_,rowen_);
  88.           break;
  89.       default:
  90.           if(kb_isfun(c)) {
  91.           ret=TRUE;       /* return function key */
  92.           break;
  93.           } else  if(!kb_iskey(c))
  94.           break;
  95.  
  96.           if(strlen(inp)<n)
  97.           break;          /* keep cursor inside field */
  98.           if(insmodef_ && echof_) {
  99.           st_movr(inptr,1);/* mov string over 1 space */
  100.           ut_push(sc_col_);
  101.           sc_putsf(inptr,n);
  102.           sc_col_=ut_pop();
  103.           sc_setcur(sc_col_,sc_row_);
  104.           }
  105.           if(echof_)
  106.           sc_putc(c);             /* display character */
  107.           ptr = inp -1;
  108.           while(++ptr<inptr)
  109.           if(*ptr==NUL)
  110.               *ptr=BLANK;     /* blank fill*/
  111.           *inptr++=c;
  112.           chinf_=TRUE;
  113.           break;
  114.     }/* switch c */
  115.  
  116.     }/* while !ret */
  117.     strcpy(str,inp);
  118.     sc_winpop();
  119.     wrapf_ = ut_pop();                 /* restore old wrap flag */
  120.     return(c);
  121. }
  122.  
  123.  
  124. /********
  125.  *
  126.  *   kb_isfun(c) - see if c is function byte
  127.  *
  128.  **/
  129.  
  130. int kb_isfun(char c)
  131. {
  132.     char *ptr;
  133.  
  134.     ptr = keyftbl_;         /* point to function key table */
  135.     while(*ptr)
  136.     if(*ptr++==c)
  137.         return(TRUE);
  138.     return(FALSE);
  139. }
  140.  
  141.  
  142. /********
  143.  *
  144.  *   kb_iskey(c) - see if c is valid printable key
  145.  *
  146.  **/
  147.  
  148. int kb_iskey(char c)
  149. {
  150.     return(isprint(c));
  151. }
  152.  
  153.  
  154. /********
  155.  *
  156.  *   kb_isbrk(c) - see if c is break key
  157.  *
  158.  **/
  159.  
  160. int kb_isbrk(char c)
  161. {
  162.     char *ptr;
  163.  
  164.     ptr = keybrktbl_;
  165.     while(*ptr)
  166.     if(*ptr++==c)
  167.         return(TRUE);
  168.     return(FALSE);
  169. }
  170.  
  171.  
  172. /********
  173.  *
  174.  *   kb_input(msg,str) - input string with message
  175.  *
  176.  **/
  177.  
  178. int kb_input(char *msg, char *str)
  179. {
  180.     sc_puts(msg);                           /* output message */
  181.     return(kb_getf(str,colen_-sc_col_));    /* get from field to eol*/
  182. }
  183.  
  184.  
  185. /********
  186.  *
  187.  *   kb_inyorn(prompt) - input yes or no
  188.  *
  189.  **/
  190.  
  191. int kb_inyorn(char *prompt)
  192. {
  193.     char c;
  194.  
  195.     sc_puts(prompt);
  196.     c = kb_getch();
  197.     return ((toupper(c)=='Y')? TRUE : FALSE);
  198. }
  199.