home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / macutils.lzh / MACUTILS / COMM / tty.c < prev    next >
Text File  |  1996-02-02  |  2KB  |  150 lines

  1. #ifndef OSK
  2. #include <stdio.h>
  3. #endif
  4.  
  5. #include <signal.h>
  6. #ifndef TERMIOS_H
  7. #include <sgtty.h>
  8. #else /* TERMIOS_H */
  9. #include <termios.h>
  10. #endif /* TERMIOS_H */
  11. #include <setjmp.h>
  12. #include "../util/masks.h"
  13. #include "protocol.h"
  14. #include "globals.h"
  15.  
  16. void cleanup();
  17. void timedout();
  18. int tgetc();
  19. void tputc();
  20.  
  21. static jmp_buf timobuf;
  22.  
  23. #ifndef TERMIOS_H
  24. static struct sgttyb otty, ntty;
  25. #else /* TERMIOS_H */
  26. static struct termios otty, ntty;
  27. #endif /* TERMIOS_H */
  28. static int ttyfd;
  29. static int signal_set;
  30.  
  31. void setup_tty()
  32. {
  33.     ttyfd = fileno(stderr);
  34.     if(!signal_set) {
  35.     (void)signal(SIGHUP, cleanup);
  36.     (void)signal(SIGINT, cleanup);
  37.     (void)signal(SIGQUIT, cleanup);
  38.     (void)signal(SIGTERM, cleanup);
  39.     if(time_out) {
  40.         (void)signal(SIGALRM, timedout);
  41.     }
  42.     signal_set = 1;
  43.     }
  44. #ifndef TERMIOS_H
  45.     (void)ioctl(ttyfd, TIOCGETP, &otty);
  46.     ntty = otty;
  47.     ntty.sg_flags = RAW | ANYP;
  48.     (void)ioctl(ttyfd, TIOCSETP, &ntty);
  49. #else /* TERMIOS_H */
  50.     (void)tcgetattr(ttyfd, &otty);
  51.     ntty = otty;
  52.     ntty.c_lflag &= ~(ICANON | ISIG | ECHO);
  53.     ntty.c_iflag &= IXOFF;
  54.     ntty.c_oflag &= ~(OPOST);
  55.     ntty.c_cflag &= ~(PARENB | PARODD);
  56.     ntty.c_cc[VMIN] = 1;
  57.     ntty.c_cc[VTIME] = 0;
  58.     (void)tcsetattr(ttyfd, TCSAFLUSH, &ntty);
  59. #endif /* TERMIOS_H */
  60. }
  61.  
  62. void reset_tty()
  63. {
  64.     (void)sleep(1); /* Wait for output to drain */
  65. #ifndef TERMIOS_H
  66.     (void)ioctl(ttyfd, TIOCSETP, &otty);
  67. #else /* TERMIOS_H */
  68.     (void)tcsetattr(ttyfd, TCSAFLUSH, &otty);
  69. #endif /* TERMIOS_H */
  70. }
  71.  
  72. void cleanup(sig) int sig;
  73. {
  74.     reset_tty();
  75.     exit(sig);
  76. }
  77.  
  78. void timedout()
  79. {
  80.     (void)signal(SIGALRM, timedout);
  81.     longjmp(timobuf, 1);
  82. }
  83.  
  84. int tgetc(timeout)
  85. int timeout;
  86. {
  87. char c;
  88. int i;
  89.  
  90.     if(time_out) {
  91.     if(setjmp(timobuf)) {
  92.         return TMO;
  93.     }
  94.     (void)alarm(timeout);
  95.     }
  96.     i = read(ttyfd, &c, 1);
  97.     if(time_out) {
  98.     (void)alarm(0);
  99.     }
  100.     if(i == 0) {
  101.     return EOT;
  102.     } else {
  103.     return c & BYTEMASK;
  104.     }
  105. }
  106.  
  107. tgetrec(buf, count, timeout)
  108. char *buf;
  109. int count, timeout;
  110. {
  111. int i, tot = 0, cc = count;
  112.  
  113.     if(time_out) {
  114.     if(setjmp(timobuf)) {
  115.         return TMO;
  116.     }
  117.     (void)alarm(timeout);
  118.     }
  119.     while(tot < count) {
  120.     i = read(ttyfd, buf, cc);
  121.     if(i < 0) {
  122.         continue;
  123.     }
  124.     tot += i;
  125.     cc -= i;
  126.     buf += i;
  127.     }
  128.     if(time_out) {
  129.     (void)alarm(0);
  130.     }
  131.     return 0;
  132. }
  133.  
  134. void tputc(c)
  135. int c;
  136. {
  137. char cc;
  138.  
  139.     cc = c & BYTEMASK;
  140.     (void)write(ttyfd, &cc, 1);
  141. }
  142.  
  143. void tputrec(buf, count)
  144. char *buf;
  145. int count;
  146. {
  147.     (void)write(ttyfd, buf, count);
  148. }
  149.  
  150.