home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / comms / network / telnet.lzh / handlers.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-22  |  2.0 KB  |  94 lines

  1. #include <intuition/intuition.h>
  2.  
  3. #define LF 0x0A
  4. #define CR 0x0D
  5. #define BS 0x08
  6. #define DEL 0x7f
  7. #define CSI 0x9b
  8. #define ESC 0x1b
  9.  
  10. #define DELCHAR "\033[P"
  11.  
  12. void handle_console_read(struct MsgPort *, char *);
  13. void handle_window(struct Window *, int *);
  14. int send_to_host(struct MsgPort *, char *, int);
  15.  
  16. extern struct IOStdReq    *writeReq;
  17. extern struct MsgPort *wait_port;
  18. extern int local_echo;
  19.  
  20. char sendbuf[200];  /* collect the line we are typing in this array */
  21. char pos = 0;
  22.  
  23. void handle_console_read(struct MsgPort *readport, char *inputbuffer)
  24. {
  25.     unsigned char ch;    /* the char read */
  26.     char obuf[80];    /* string to write on console */
  27.     static int in_control = 0;
  28.  
  29.     if((ch = ConMayGetChar(readport, inputbuffer)) != -1) {
  30.  
  31.     /* handle escape sequences */
  32.     if(in_control == 1) {
  33.         if(ch =='[')
  34.         in_control = 2;
  35.         else
  36.         in_control = -2;
  37.     }
  38.  
  39.     if((ch == CSI) || (ch == ESC)) /* control sequence start */
  40.         in_control = (ch == ESC) ? 1 : 2;
  41.  
  42.     if(!in_control) {
  43.         if(ch == CR) {
  44.         sprintf(obuf,"%c%c", LF, CR);   /* move cursor to next line */
  45.         ConPuts(writeReq,obuf);
  46.         sendbuf[pos] = LF;
  47.         pos++;
  48.         send_to_host( wait_port, sendbuf, pos);
  49.         pos = 0;
  50.         }
  51.         else if(ch == BS) {
  52.         if(pos > 0) {
  53.             pos--;
  54.             sprintf(obuf,"%c%s", BS, DELCHAR);
  55.             ConPuts(writeReq,obuf);
  56.             }
  57.         }
  58.         else if(( (ch >= 0x1f) && (ch <= 0x7e) || (ch >= 0xa0) )) {
  59.         sprintf(obuf,"%c", ch);
  60.         if(local_echo)
  61.             ConPuts(writeReq,obuf);
  62.         sendbuf[pos] = obuf[0];
  63.         pos++;
  64.         }
  65.     }
  66.  
  67.     if((in_control == 3) && ((ch >= 0x40) && (ch <= 0x7e)))
  68.         in_control = -1;
  69.  
  70.     if(in_control == 2)
  71.         in_control = 3;    /* we are inside a escape sequence */
  72.  
  73.     if(in_control < 0)
  74.         in_control = 0;    /* ESC sequence ended */
  75.     }
  76. }
  77.  
  78.  
  79. void handle_window(struct Window *win, int *abort)
  80. {
  81.     struct IntuiMessage *winmsg;
  82.  
  83.     while(winmsg = (struct IntuiMessage *)GetMsg(win->UserPort)) {
  84.     switch(winmsg->Class) {
  85.         case CLOSEWINDOW:
  86.         *abort = TRUE;
  87.         break;
  88.         default:
  89.         break;
  90.         } /* end switch */
  91.     ReplyMsg((struct Message *)winmsg);
  92.     }
  93. }
  94.