home *** CD-ROM | disk | FTP | other *** search
- /*
- This File contains routines which process incoming characters
- and user keystrokes. It requires the screen library
- Author: Jerry LeVan
- 325 Boone Trail
- Richmond Ky 40475
-
- May 1987
-
- Started an investigation of B protocol
- Aug 29,1987: Add Read_Modem
- */
-
- #include <events.h>
- #include <devices.h>
- #include <serial.h>
- #include <dialogs.h>
- #include <OSUtils.h>
- #include <Menus.h>
-
- #define Stats_ID 129
-
- #define LF 0xa
- #define CR 0xd
-
- extern char doCrLf;
- extern short BSflag;
- extern short localEcho;
- extern short recRef;
- extern Boolean asciiReceive;
- extern MenuHandle FileMenu;
- extern Boolean keypad_app; /* defined in vt100 */
-
-
- static long maxRead=0; /* largest supply of chars in Serial buffer */
- static long avgRead=0; /* average size of read */
- static long cntRead=0; /* number of reads this session */
- static long totRead=0; /* Total chars read to date */
-
- #define LINEBUFSZ 80 /* Maximum number of characters in line buffer */
- static long lineBufCnt=0; /* Number of Characters in local buffer */
-
- /*
- * The following procedure returns True and the "next" character from
- * the modem if one is available,otherwise false. The routine will
- * buffer upto LINBUFSZ characters to try to reduce ROM calls. The
- * variable lineBuffCnt will be the count of chars in the local buffer
- */
-
- Boolean Read_Modem(ptr,refNum)
- char *ptr; /* Where to leave the character */
- short refNum; /* Which channel to use */
- {
- static char lineBuff[LINEBUFSZ]; /* local buffer */
- static char *pos; /* local pointer into the buffer */
-
- if(lineBufCnt <= 0) /* zero means out of characters */
- { SerGetBuf(refNum,&lineBufCnt); /* find out if any chars are avail */
- if (lineBufCnt > 0) /* not zero means we have some */
- { /* collect some statistics */
-
- if (lineBufCnt > maxRead) maxRead = lineBufCnt;
- cntRead++;
-
- /* get some characters */
- if(lineBufCnt > LINEBUFSZ) lineBufCnt = LINEBUFSZ; /* don't get too many characters */
-
- FSRead(refNum,&lineBufCnt,lineBuff);
- pos = (char *)lineBuff; /* reset pointer */
-
- totRead +=lineBufCnt;
- avgRead = totRead/cntRead;
- }
- else return (false); /* no characters where in the Port Buffer */
- } /* the local buffer was empty */
- lineBufCnt--; /* decrement the count */
- *ptr = *pos++; /* copy the character */
- return (true) ; /* return success */
- }
-
- /* called my main to process characters from modem */
- void ReadPort(refNum)
- short refNum; /* input refnum */
- {
- unsigned char ch; /* character under consideration */
- long lcnt;
- OSErr err;
- do
- if(Read_Modem(&ch,refNum))
- {
- ch &= 0x7f ; /* mask to seven bits */
- /* check to see if we are recording */
- if(asciiReceive)
- if(ch !=0xa) {
- lcnt = 1;
- if(err = FSWrite(recRef,&lcnt,&ch)){
- ErrorMessage("Error During Write",err,nil,nil);
- FSClose(recRef);recRef = 0;
- asciiReceive = false;
- SetItem(FileMenu,1,"Start Text Capture...");
- } /* if err */
- } /* if asciiReceive */
- Put_Char(ch);
- if( doCrLf &&(ch == CR)) Put_Char(LF);
- } /* there was a character */
- while (lineBufCnt > 0); /* process all characters in local buffer */
- flushbuf();
- }
-
- /* This routine processes Mac KeyStrokes */
- void ProcessChar(ch,code,refNum,modifiers)
- unsigned char ch; /* the received character */
- unsigned char code; /* the key number */
- short refNum; /* output driver refnum */
- short modifiers; /* modifier field */
-
-
- { long cnt;
- SerStaRec statRec;
-
- /* if the keyboard is locked beep and ignore character */
- SerStatus(refNum,&statRec);
- if (statRec.xOffHold){ SysBeep(7); return;}
-
- /* if the local echo flag is up we must locally draw the character */
-
- if (localEcho) Put_Char(ch);
-
- /* handle vax specific keystrokes */
-
- cnt = 1;
- if (ch == 0x08){ /* if backspace */
- if(!(modifiers & optionKey)) /* and option key not down */
- if(!BSflag)ch = 0x7f; /* backspace -> del for vax */
- FSWrite(refNum,&cnt,&ch);
- return;
- }
- /* process the arrow and pf1..pf4 keys */
- cnt = 3;
- switch(ch){
- case 0x1c: FSWrite(refNum,&cnt,"\033OD"); return; /* right arrow */
- case 0x1d: FSWrite(refNum,&cnt,"\033OC"); return; /* left arrow */
- case 0x1e: FSWrite(refNum,&cnt,"\033OA"); return; /* up arrow */
- case 0x1f: FSWrite(refNum,&cnt,"\033OB"); return; /* down arrow */
- case 0x1b: if (code ==71) { /* clear key on keypad PF1*/
- FSWrite(refNum,&cnt,"\033OP");return;
- } else break;
- case 0x3d: if(code == 72) { /* "=" key on keypad PF2 */
- FSWrite(refNum,&cnt,"\033OQ"); return;
- } else break;
- case 0x2f: if (code == 77) { /* "/" key on keypad PF3 */
- FSWrite(refNum,&cnt,"\033OR"); return;
- } else break;
- case 0x2a: if (code == 66) { /* "*" key on keypad PF4 */
- FSWrite(refNum,&cnt,"\033OS"); return;
- } else break;
- } /* finished with arrow and pf keys */
-
- /* now map the other keys on the keypad if the keypad_app flag is up */
- if (keypad_app)
- switch(ch){
- case 0x37: if(code == 89) {FSWrite(refNum,&cnt,"\033Ow");return;} /* 7 */
- else break;
- case 0x38: if(code == 91) {FSWrite(refNum,&cnt,"\033Ox");return;} /* 8 */
- else break;
- case 0x39: if(code == 92) {FSWrite(refNum,&cnt,"\033Oy");return;} /* 9 */
- else break;
- case 0x2d: if(code == 78) {FSWrite(refNum,&cnt,"\033Om");return;} /* - */
- else break;
- case 0x34: if(code == 86) {FSWrite(refNum,&cnt,"\033Ot");return;} /* 4 */
- else break;
- case 0x35: if(code == 87) {FSWrite(refNum,&cnt,"\033Ou");return;} /* 5 */
- else break;
- case 0x36: if(code == 88) {FSWrite(refNum,&cnt,"\033Ov");return;} /* 6 */
- else break;
- case 0x2b: if(code == 70) {FSWrite(refNum,&cnt,"\033Ol");return;} /* + */
- else break;
- case 0x31: if(code == 83) {FSWrite(refNum,&cnt,"\033Oq");return;} /* 1 */
- else break;
- case 0x32: if(code == 84) {FSWrite(refNum,&cnt,"\033Or");return;} /* 2 */
- else break;
- case 0x33: if(code == 85) {FSWrite(refNum,&cnt,"\033Os");return;} /* 3 */
- else break;
- case 0x30: if(code == 82) {FSWrite(refNum,&cnt,"\033Op");return;} /* 0 */
- else break;
- case 0x2e: if(code == 65) {FSWrite(refNum,&cnt,"\033On");return;} /* . */
- else break;
- case 0x03: if(code == 76) {FSWrite(refNum,&cnt,"\033OM");return;} /* ent */
- else break;
- } /* end of keypad processing */
-
- /* run of the mill character */
- cnt = 1;
- FSWrite(refNum,&cnt,&ch);
-
-
- }
-
- void ShowReadStats() /* display info on serial port */
- { char temp1[10],temp2[10]; /* buffers for numeric conversion */
- NumToString(maxRead,temp1);
- NumToString(avgRead,temp2);
- ParamText(temp1,temp2,nil,nil);
- Alert(Stats_ID,nil);
- /* reset counters to zero */
- maxRead = 0; totRead = 0; cntRead = 0;
- }