home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / s920603.zip / TTYDRIV.C < prev    next >
C/C++ Source or Header  |  1992-05-29  |  2KB  |  111 lines

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