tcgetattr.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <termios.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include <errno.h>
  9. #include "sys9.h"
  10. #include "lib.h"
  11. #include "dir.h"
  12. #define CESC '\\'
  13. #define CINTR 0177 /* DEL */
  14. #define CQUIT 034 /* FS, cntl | */
  15. #define CERASE 010 /* BS */
  16. #define CKILL 025 /* cntl u */
  17. #define CEOF 04 /* cntl d */
  18. #define CSTART 021 /* cntl q */
  19. #define CSTOP 023 /* cntl s */
  20. #define CSWTCH 032 /* cntl z */
  21. #define CEOL 000
  22. #define CNSWTCH 0
  23. static int
  24. isptty(int fd)
  25. {
  26. Dir *d;
  27. int rv;
  28. if((d = _dirfstat(fd)) == nil)
  29. return 0;
  30. rv = (strncmp(d->name, "ptty", 4) == 0);
  31. free(d);
  32. return rv;
  33. }
  34. int
  35. tcgetattr(int fd, struct termios *t)
  36. {
  37. int n;
  38. char buf[60];
  39. if(!isptty(fd)) {
  40. if(isatty(fd)) {
  41. /* If there is no emulation return sensible defaults */
  42. t->c_iflag = ISTRIP|ICRNL|IXON|IXOFF;
  43. t->c_oflag = OPOST|TAB3|ONLCR;
  44. t->c_cflag = B9600;
  45. t->c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK;
  46. t->c_cc[VINTR] = CINTR;
  47. t->c_cc[VQUIT] = CQUIT;
  48. t->c_cc[VERASE] = CERASE;
  49. t->c_cc[VKILL] = CKILL;
  50. t->c_cc[VEOF] = CEOF;
  51. t->c_cc[VEOL] = CEOL;
  52. t->c_cc[VSTART] = CSTART;
  53. t->c_cc[VSTOP] = CSTOP;
  54. return 0;
  55. } else {
  56. errno = ENOTTY;
  57. return -1;
  58. }
  59. }
  60. if(_SEEK(fd, -2, 0) != -2) {
  61. _syserrno();
  62. return -1;
  63. }
  64. n = _READ(fd, buf, 57);
  65. if(n < 0) {
  66. _syserrno();
  67. return -1;
  68. }
  69. t->c_iflag = strtoul(buf+4, 0, 16);
  70. t->c_oflag = strtoul(buf+9, 0, 16);
  71. t->c_cflag = strtoul(buf+14, 0, 16);
  72. t->c_lflag = strtoul(buf+19, 0, 16);
  73. for(n = 0; n < NCCS; n++)
  74. t->c_cc[n] = strtoul(buf+24+(n*3), 0, 16);
  75. return 0;
  76. }
  77. /* BUG: ignores optional actions */
  78. int
  79. tcsetattr(int fd, int optactions, const struct termios *t)
  80. {
  81. int n, i;
  82. char buf[100];
  83. if(!isptty(fd)) {
  84. if(!isatty(fd)) {
  85. errno = ENOTTY;
  86. return -1;
  87. } else
  88. return 0;
  89. }
  90. n = sprintf(buf, "IOW %4.4x %4.4x %4.4x %4.4x ",
  91. t->c_iflag, t->c_oflag, t->c_cflag, t->c_lflag);
  92. for(i = 0; i < NCCS; i++)
  93. n += sprintf(buf+n, "%2.2x ", t->c_cc[i]);
  94. if(_SEEK(fd, -2, 0) != -2) {
  95. _syserrno();
  96. return -1;
  97. }
  98. n = _WRITE(fd, buf, n);
  99. if(n < 0) {
  100. _syserrno();
  101. return -1;
  102. }
  103. return 0;
  104. }
  105. int
  106. tcsetpgrp(int fd, pid_t pgrpid)
  107. {
  108. int n;
  109. char buf[30];
  110. if(!isptty(fd)) {
  111. if(!isatty(fd)) {
  112. errno = ENOTTY;
  113. return -1;
  114. } else
  115. return 0;
  116. }
  117. n = sprintf(buf, "IOW note %d", pgrpid);
  118. if(_SEEK(fd, -2, 0) != -2) {
  119. _syserrno();
  120. return -1;
  121. }
  122. n = _WRITE(fd, buf, n);
  123. if(n < 0) {
  124. _syserrno();
  125. return -1;
  126. }
  127. }
  128. pid_t
  129. tcgetpgrp(int fd)
  130. {
  131. int n;
  132. pid_t pgrp;
  133. char buf[100];
  134. if(!isptty(fd)) {
  135. errno = ENOTTY;
  136. return -1;
  137. }
  138. if(_SEEK(fd, -2, 0) != -2) {
  139. _syserrno();
  140. return -1;
  141. }
  142. n = _READ(fd, buf, sizeof(buf));
  143. if(n < 0) {
  144. _syserrno();
  145. return -1;
  146. }
  147. pgrp = atoi(buf+24+(NCCS*3));
  148. return pgrp;
  149. }
  150. /* should do a better job here */
  151. int
  152. tcdrain(int)
  153. {
  154. errno = ENOTTY;
  155. return -1;
  156. }
  157. int
  158. tcflush(int, int)
  159. {
  160. errno = ENOTTY;
  161. return -1;
  162. }
  163. int
  164. tcflow(int, int)
  165. {
  166. errno = ENOTTY;
  167. return -1;
  168. }
  169. int
  170. tcsendbreak(int)
  171. {
  172. errno = ENOTTY;
  173. return -1;
  174. }