home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff225.lzh / AmigaTCP / src / ttydriv.c < prev    next >
C/C++ Source or Header  |  1989-06-24  |  2KB  |  100 lines

  1. #include <stdio.h>
  2. #include "machdep.h"
  3.  
  4. /* TTY input driver */
  5. #define    NULLCHAR    (char *)NULL
  6.  
  7. int ttymode;
  8. #define TTY_COOKED    0
  9. #define    TTY_RAW    1
  10.  
  11. #define    LINESIZE    256
  12.  
  13. #ifdef    AMIGA
  14. #define    CTLX    24
  15. #endif
  16.  
  17. #define    CTLU    21
  18.  
  19. raw()
  20. {
  21.     ttymode = TTY_RAW;
  22. }
  23.  
  24. cooked()
  25. {
  26.     ttymode = TTY_COOKED;
  27. }
  28.  
  29. /* Accept characters from the incoming tty buffer and process them
  30.  * (if in cooked mode) or just pass them directly (if in raw mode).
  31.  * Returns the number of characters available for use; if non-zero,
  32.  * also stashes a pointer to the character(s) in the "buf" argument.
  33.  */
  34. int
  35. ttydriv(c,buf)
  36. char c;
  37. char **buf;
  38. {
  39.     static char linebuf[LINESIZE];
  40.     static char *cp = linebuf;
  41.     int cnt;
  42.  
  43.     if(buf == (char **)NULL)
  44.         return 0;    /* paranoia check */
  45.  
  46.     cnt = 0;
  47.     switch(ttymode){
  48.     case TTY_RAW:
  49.         *cp++ = c;
  50.         cnt = cp - linebuf;
  51.         cp = linebuf;
  52.         break;
  53.     case TTY_COOKED:
  54.         /* Perform cooked-mode line editing */
  55.         switch(c & 0x7f){
  56.         case '\r':    /* CR and LF are equivalent */
  57.         case '\n':
  58.             *cp++ = '\r';
  59.             *cp++ = '\n';
  60.             printf("\r\n");
  61.             cnt = cp - linebuf;
  62.             cp = linebuf;
  63.             break;
  64.         case '\b':        /* Backspace */
  65.             if(cp != linebuf){
  66.                 cp--;
  67.                 printf("\b \b");
  68.             }
  69.             break;
  70.         case CTLU:    /* Line kill */
  71. #ifdef    AMIGA
  72.         case CTLX:
  73. #endif
  74.             while(cp != linebuf){
  75.                 cp--;
  76.                 printf("\b \b");
  77.             }
  78.             break;
  79.         default:    /* Ordinary character */
  80.             *cp++ = c;
  81. #ifdef    AMIGA
  82.             printf("%c", c);
  83. #else
  84.             putchar(c);
  85. #endif
  86.             if(cp >= &linebuf[LINESIZE]){
  87.                 cnt = cp - linebuf;
  88.                 cp = linebuf;
  89.             }
  90.             break;
  91.         }
  92.     }
  93.     if(cnt != 0)
  94.         *buf = linebuf;
  95.     else
  96.         *buf = NULLCHAR;
  97.     fflush(stdout);
  98.     return cnt;
  99. }
  100.