home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / L-M / mini / processchar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-09-26  |  6.7 KB  |  207 lines  |  [TEXT/MPS ]

  1. /* 
  2.     This File contains routines which process incoming characters
  3.     and user keystrokes. It requires the screen library
  4.     Author: Jerry LeVan
  5.             325 Boone Trail
  6.             Richmond Ky 40475
  7.             
  8.             May 1987
  9.             
  10.     Started an investigation of B protocol
  11.     Aug 29,1987: Add Read_Modem
  12. */
  13.  
  14. #include <events.h>    
  15. #include <devices.h>
  16. #include <serial.h>
  17. #include <dialogs.h>
  18. #include <OSUtils.h>
  19. #include <Menus.h>
  20.  
  21. #define Stats_ID 129
  22.  
  23. #define LF 0xa
  24. #define CR 0xd
  25.  
  26. extern char doCrLf;
  27. extern short BSflag;
  28. extern short localEcho;
  29. extern short recRef;
  30. extern Boolean asciiReceive;
  31. extern MenuHandle FileMenu;
  32. extern Boolean keypad_app;  /* defined in vt100 */
  33.  
  34.  
  35. static long maxRead=0;            /* largest supply of chars in Serial buffer */
  36. static long avgRead=0;            /* average size of read */
  37. static long cntRead=0;            /* number of reads this session */
  38. static long totRead=0;            /* Total chars read to date */
  39.  
  40. #define LINEBUFSZ 80        /* Maximum number of characters in line buffer */
  41. static long lineBufCnt=0;    /* Number of Characters in local buffer */
  42.  
  43. /*
  44.  * The following procedure returns True and the "next" character from 
  45.  * the modem if one is available,otherwise false. The routine will
  46.  * buffer upto LINBUFSZ characters to try to reduce ROM calls. The
  47.  * variable lineBuffCnt will be the count of chars in the local buffer
  48.  */
  49.  
  50. Boolean Read_Modem(ptr,refNum)
  51.   char *ptr;        /* Where to leave the character */
  52.   short refNum;        /* Which channel to use */
  53.  {
  54.    static char lineBuff[LINEBUFSZ]; /* local buffer */
  55.    static char *pos;         /* local pointer into the buffer */
  56.    
  57.    if(lineBufCnt <= 0)   /* zero means out of characters */
  58.      { SerGetBuf(refNum,&lineBufCnt);  /* find out if any chars are avail */
  59.        if (lineBufCnt > 0)        /* not zero means we have some */
  60.          { /* collect some statistics */
  61.          
  62.               if (lineBufCnt > maxRead) maxRead = lineBufCnt;
  63.                cntRead++;
  64.        
  65.                /* get some characters */
  66.                if(lineBufCnt > LINEBUFSZ) lineBufCnt = LINEBUFSZ; /* don't get too many characters */
  67.        
  68.                FSRead(refNum,&lineBufCnt,lineBuff);
  69.             pos = (char *)lineBuff;    /* reset pointer */
  70.        
  71.                totRead +=lineBufCnt;
  72.                avgRead = totRead/cntRead;
  73.           }
  74.          else return (false); /* no characters where in the Port Buffer */
  75.        }  /* the local buffer was empty */
  76.     lineBufCnt--;  /* decrement the count */
  77.     *ptr = *pos++;  /* copy the character */
  78.     return (true) ;   /* return success */
  79.   }
  80.   
  81. /* called my main to process characters from modem */ 
  82. void ReadPort(refNum)
  83.   short refNum;           /* input refnum */
  84.   { 
  85.     unsigned char ch;    /* character under consideration */
  86.     long lcnt;
  87.     OSErr err;
  88.     do
  89.       if(Read_Modem(&ch,refNum))
  90.        {
  91.         ch &= 0x7f ; /* mask to seven bits */
  92.         /* check to see if we are recording */
  93.         if(asciiReceive)
  94.           if(ch !=0xa) {
  95.              lcnt = 1;
  96.              if(err = FSWrite(recRef,&lcnt,&ch)){
  97.                ErrorMessage("Error During Write",err,nil,nil);
  98.                FSClose(recRef);recRef = 0;
  99.                asciiReceive = false;
  100.                SetItem(FileMenu,1,"Start Text Capture...");
  101.             }  /* if err */
  102.            } /* if asciiReceive */
  103.           Put_Char(ch);
  104.           if( doCrLf &&(ch == CR)) Put_Char(LF);
  105.         } /* there was a character */
  106.     while (lineBufCnt > 0);  /* process all characters in local buffer */
  107.     flushbuf();
  108.   }
  109.  
  110. /* This routine processes Mac KeyStrokes */  
  111. void ProcessChar(ch,code,refNum,modifiers)
  112.   unsigned char ch;        /* the received character */
  113.   unsigned char code;    /* the key number */
  114.   short refNum;            /* output driver refnum */
  115.   short modifiers;        /* modifier field */
  116.   
  117.   
  118. {   long cnt;
  119.     SerStaRec statRec;
  120.     
  121.     /* if the keyboard is locked beep and ignore character */
  122.     SerStatus(refNum,&statRec);
  123.      if (statRec.xOffHold){ SysBeep(7); return;}
  124.      
  125.     /* if the local echo flag is up we must locally draw the character */
  126.     
  127.     if (localEcho) Put_Char(ch);
  128.     
  129.     /* handle vax specific keystrokes */
  130.     
  131.     cnt = 1;
  132.     if (ch == 0x08){ /* if backspace */
  133.        if(!(modifiers & optionKey))  /* and option key not down */
  134.             if(!BSflag)ch = 0x7f; /* backspace -> del for vax */
  135.        FSWrite(refNum,&cnt,&ch);
  136.        return;
  137.      }
  138.    /* process the arrow and pf1..pf4 keys */
  139.    cnt = 3;
  140.    switch(ch){
  141.     case 0x1c: FSWrite(refNum,&cnt,"\033OD"); return; /* right arrow */  
  142.     case 0x1d: FSWrite(refNum,&cnt,"\033OC"); return; /* left arrow */
  143.     case 0x1e: FSWrite(refNum,&cnt,"\033OA"); return; /* up arrow */
  144.     case 0x1f: FSWrite(refNum,&cnt,"\033OB"); return; /* down arrow */
  145.     case 0x1b: if (code ==71) { /* clear key on keypad  PF1*/
  146.             FSWrite(refNum,&cnt,"\033OP");return;
  147.            } else break;
  148.     case 0x3d: if(code == 72) { /* "=" key on keypad PF2 */
  149.             FSWrite(refNum,&cnt,"\033OQ"); return;
  150.            } else break;
  151.     case 0x2f: if (code == 77) { /* "/" key on keypad PF3 */
  152.             FSWrite(refNum,&cnt,"\033OR"); return;
  153.            } else break;
  154.     case 0x2a: if (code == 66) { /* "*" key on keypad PF4 */
  155.             FSWrite(refNum,&cnt,"\033OS"); return;
  156.            } else break;
  157.     } /* finished with arrow and pf keys */
  158.     
  159.     /* now map the other keys on the keypad if the keypad_app flag is up */
  160.     if (keypad_app)
  161.       switch(ch){
  162.       case 0x37: if(code == 89) {FSWrite(refNum,&cnt,"\033Ow");return;} /* 7 */
  163.                    else break;
  164.       case 0x38: if(code == 91) {FSWrite(refNum,&cnt,"\033Ox");return;} /* 8 */
  165.                       else break;
  166.       case 0x39: if(code == 92) {FSWrite(refNum,&cnt,"\033Oy");return;} /* 9 */
  167.                       else break;
  168.       case 0x2d: if(code == 78) {FSWrite(refNum,&cnt,"\033Om");return;} /* - */
  169.                       else break;
  170.       case 0x34: if(code == 86) {FSWrite(refNum,&cnt,"\033Ot");return;} /* 4 */
  171.                       else break;
  172.       case 0x35: if(code == 87) {FSWrite(refNum,&cnt,"\033Ou");return;} /* 5 */
  173.                       else break;
  174.       case 0x36: if(code == 88) {FSWrite(refNum,&cnt,"\033Ov");return;} /* 6 */
  175.                       else break;
  176.       case 0x2b: if(code == 70) {FSWrite(refNum,&cnt,"\033Ol");return;} /* + */
  177.                       else break;
  178.       case 0x31: if(code == 83) {FSWrite(refNum,&cnt,"\033Oq");return;} /* 1 */
  179.                       else break;
  180.       case 0x32: if(code == 84) {FSWrite(refNum,&cnt,"\033Or");return;} /* 2 */
  181.                       else break;
  182.       case 0x33: if(code == 85) {FSWrite(refNum,&cnt,"\033Os");return;} /* 3 */
  183.                       else break;
  184.       case 0x30: if(code == 82) {FSWrite(refNum,&cnt,"\033Op");return;} /* 0 */
  185.                       else break;
  186.       case 0x2e: if(code == 65) {FSWrite(refNum,&cnt,"\033On");return;} /* . */
  187.                       else break;
  188.       case 0x03: if(code == 76) {FSWrite(refNum,&cnt,"\033OM");return;} /* ent */
  189.                       else break;
  190.      } /* end of keypad processing */ 
  191.     
  192.    /* run of the mill character */
  193.    cnt = 1;
  194.    FSWrite(refNum,&cnt,&ch);
  195.  
  196.   
  197. }
  198.  
  199. void ShowReadStats() /* display info on serial port */
  200.   {  char temp1[10],temp2[10]; /* buffers for numeric conversion */
  201.      NumToString(maxRead,temp1);
  202.      NumToString(avgRead,temp2);
  203.      ParamText(temp1,temp2,nil,nil);
  204.      Alert(Stats_ID,nil);
  205.      /* reset counters to zero */
  206.      maxRead = 0; totRead = 0; cntRead = 0;
  207.    }