home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / network / src_1218.zip / TTYDRIV.C < prev    next >
C/C++ Source or Header  |  1991-01-27  |  2KB  |  116 lines

  1. /* TTY input line editing
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include <stdio.h>
  5. #include "global.h"
  6. #include "mbuf.h"
  7. #include "session.h"
  8. #include "tty.h"
  9. #include "socket.h"
  10.  
  11. extern FILE *Rawterm;
  12.  
  13. #define    OFF    0
  14. #define    ON    1
  15.  
  16. #define    LINESIZE    256
  17.  
  18. #define    CTLU    21
  19. #define CTLR    18
  20. #define    CTLZ    26
  21. #define    DEL    0x7f
  22.  
  23. /* Accept characters from the incoming tty buffer and process them
  24.  * (if in cooked mode) or just pass them directly (if in raw mode).
  25.  *
  26.  * Echoing (if enabled) is direct to the raw terminal. This requires
  27.  * recording (if enabled) of locally typed info to be done by the session
  28.  * itself so that edited output instead of raw input is recorded.
  29.  */
  30. struct mbuf *
  31. ttydriv(sp,c)
  32. struct session *sp;
  33. char c;
  34. {
  35.     struct mbuf *bp;
  36.     char *cp,*rp;
  37.  
  38.     switch(sp->ttystate.edit){
  39.     case OFF:
  40.         bp = ambufw(1);
  41.         *bp->data = c;
  42.         bp->cnt = 1;
  43.         if(sp->ttystate.echo)
  44.             putc(c,Rawterm);
  45.  
  46.         return bp;
  47.     case ON:
  48.         if(sp->ttystate.line == NULLBUF)
  49.             sp->ttystate.line = ambufw(LINESIZE);
  50.  
  51.         bp = sp->ttystate.line;
  52.         cp = bp->data + bp->cnt;
  53.         /* Perform cooked-mode line editing */
  54.         switch(c & 0x7f){
  55.         case '\r':    /* CR and LF both terminate the line */
  56.         case '\n':
  57.             if(sp->ttystate.crnl)
  58.                 *cp = '\n';
  59.             else
  60.                 *cp = c;
  61.             if(sp->ttystate.echo)
  62.                 fputs(Eol,Rawterm);
  63.  
  64.             bp->cnt += 1;
  65.             sp->ttystate.line = NULLBUF;
  66.             return bp;
  67.         case DEL:
  68.         case '\b':    /* Character delete */
  69.             if(bp->cnt != 0){
  70.                 bp->cnt--;
  71.                 if(sp->ttystate.echo)
  72.                     fputs("\b \b",Rawterm);
  73.             }
  74.             break;
  75.         case CTLR:    /* print line buffer */
  76.             if(sp->ttystate.echo){
  77.                 fprintf(Rawterm,"^R%s",Eol) ;
  78.                 rp = bp->data;
  79.                 while (rp < cp)
  80.                     putc(*rp++,Rawterm) ;
  81.             }
  82.             break ;
  83.         case CTLU:    /* Line kill */
  84.             while(bp->cnt != 0){
  85.                 bp->cnt--;
  86.                 if(sp->ttystate.echo){
  87.                     fputs("\b \b",Rawterm);
  88.                 }
  89.             }
  90.             break;
  91.         default:    /* Ordinary character */
  92.             *cp = c;
  93.             bp->cnt++;
  94.  
  95.             /* ^Z apparently hangs the terminal emulators under
  96.              * DoubleDos and Desqview. I REALLY HATE having to patch
  97.              * around other people's bugs like this!!!
  98.              */
  99.             if(sp->ttystate.echo &&
  100. #ifndef    AMIGA
  101.              c != CTLZ &&
  102. #endif
  103.              bp->cnt < LINESIZE-1){
  104.                 putc(c,Rawterm);
  105.  
  106.             } else if(bp->cnt >= LINESIZE-1){
  107.                 putc('\007',Rawterm);    /* Beep */
  108.                 bp->cnt--;
  109.             }
  110.             break;
  111.         }
  112.         break;
  113.     }
  114.     return NULLBUF;
  115. }
  116.