home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / mytinfo / part02 / tty.c < prev   
Encoding:
C/C++ Source or Header  |  1992-12-26  |  7.5 KB  |  397 lines

  1. /*
  2.  * tty.c
  3.  *
  4.  * By Ross Ridge
  5.  * Public Domain
  6.  * 92/02/01 07:30:49
  7.  *
  8.  * Do some tty related things
  9.  *
  10.  */
  11.  
  12. #include "defs.h"
  13. #include "term.h"
  14.  
  15. #ifdef USE_SCCS_IDS
  16. static const char SCCSid[] = "@(#) mytinfo tty.c 3.2 92/02/01 public domain, By Ross Ridge";
  17. #endif
  18. #ifndef EXTA_IS
  19. #define EXTA_IS 19200
  20. #endif
  21.  
  22. #ifndef EXTB_IS
  23. #define EXTB_IS 38400
  24. #endif
  25.  
  26. #ifdef lint
  27. #define ioctl _ioctl
  28. /* shutup lint */
  29. /* ARGSUSED */
  30. /* VARARGS1 */
  31. static int ioctl(a, b, p) int a; long b; anyptr *p;  { return 0; }
  32. #endif
  33.  
  34. #ifdef USE_TERMIO
  35.  
  36. #ifdef USE_SMALLMEM
  37. unsigned short _baud_tbl[] = {0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
  38.                   1800, 2400, 4800, 9600, EXTA_IS, EXTB_IS};
  39. #else
  40. long _baud_tbl[] = {0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800,
  41.             2400, 4800, 9600, EXTA_IS, EXTB_IS};
  42. #endif
  43.  
  44. static struct termio old;
  45.  
  46. int
  47. _lit_output() {
  48.     struct termio tmp;
  49.     if (ioctl(cur_term->fd, TCGETA, &old) == -1)
  50.         return 0;
  51.     if (!(old.c_oflag & OPOST) || !(old.c_oflag & ONLCR)) 
  52.         return 0;
  53.     memcpy((anyptr)&tmp, (anyptr)&old, sizeof(old));
  54.     tmp.c_oflag &= ~OPOST;
  55.     ioctl(cur_term->fd, TCSETA, &tmp);
  56.     return 1;
  57. }
  58.  
  59. void
  60. _norm_output() {
  61.     ioctl(cur_term->fd, TCSETA, &old);
  62. }
  63.     
  64. int
  65. _check_tty() {
  66.     if ((cur_term->prog_mode.c_iflag & IXON) && cur_term->xon)
  67.         cur_term->pad = 0;
  68.     else
  69.         cur_term->pad = 1;
  70.     cur_term->baudrate = _baud_tbl[cur_term->prog_mode.c_cflag & CBAUD];
  71.     return OK;
  72. }
  73.  
  74. int
  75. def_prog_mode() {
  76. #ifdef USE_WINSZ
  77. #ifdef TIOCGWINSZ
  78.     if (ioctl(cur_term->fd, TIOCGWINSZ, &cur_term->prog_winsz) < 0)
  79.         return ERR;
  80. #endif
  81. #endif
  82.     if (ioctl(cur_term->fd, TCGETA, &cur_term->prog_mode) == 0
  83.             && _check_tty() == OK)
  84.         return OK;
  85.     return ERR;
  86. }
  87.  
  88. int
  89. def_shell_mode() {
  90.     cur_term->termcap = 0;
  91. #ifdef USE_WINSZ
  92. #ifdef TIOCGWINSZ
  93.     if (ioctl(cur_term->fd, TIOCGWINSZ, &cur_term->shell_winsz) < 0)
  94.         return ERR;
  95. #endif
  96. #endif
  97.     return ioctl(cur_term->fd, TCGETA, &cur_term->shell_mode)==0 ? OK : ERR;
  98. }
  99.  
  100. int
  101. reset_prog_mode() {
  102.     return ioctl(cur_term->fd, TCSETA, &cur_term->prog_mode)==0 ? OK : ERR;
  103. }
  104.  
  105. int
  106. reset_shell_mode() {
  107.     return ioctl(cur_term->fd, TCSETA, &cur_term->shell_mode)==0 ? OK : ERR;
  108. }
  109.  
  110. int
  111. _init_tty() {
  112.     cur_term->true_lines = lines;
  113.     cur_term->true_columns = columns;
  114.     if (pad_char == NULL)
  115.         cur_term->padch = '\000';
  116.     else
  117.         cur_term->padch = pad_char[0];
  118.     if (def_shell_mode() == ERR || def_prog_mode() == ERR) {
  119.         cur_term->pad = 0;
  120.         cur_term->baudrate = 1;
  121.         cur_term->xon = 0;
  122.         return OK;
  123.     }
  124.     cur_term->xon = (xoff_character == NULL || xoff_character[0] == '\021')
  125.             && (xon_character == NULL || xon_character[0] == '\023')
  126.             && xon_xoff;
  127. #ifdef USE_WINSZ
  128. #ifdef TIOCGWINSZ
  129.     if (cur_term->prog_winsz.ws_row != 0
  130.         && cur_term->prog_winsz.ws_col != 0) {
  131.         lines = cur_term->prog_winsz.ws_row;
  132.         columns = cur_term->prog_winsz.ws_col;
  133.     }
  134. #endif
  135. #endif
  136.     return OK;
  137. }
  138.  
  139. #else
  140.  
  141. #ifdef USE_SGTTY
  142.  
  143. #ifdef USE_SMALLMEM
  144. unsigned short _baud_tbl[] = {0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
  145.                   1800, 2400, 4800, 9600, EXTA_IS, EXTB_IS};
  146. #else
  147. long _baud_tbl[] = {0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
  148.             1800, 2400, 4800, 9600, EXTA_IS, EXTB_IS};
  149. #endif
  150.  
  151.  
  152. #ifdef TIOCLGET
  153.  
  154. static int old;
  155.  
  156. int
  157. _lit_output() {
  158.     struct sgttyb buf;
  159.     int tmp;
  160.  
  161.     ioctl(cur_term->fd, TIOCGETP, &buf);
  162.     if (buf.sg_flags & RAW)
  163.         return 0;
  164.     ioctl(cur_term->fd, TIOCLGET, &old);
  165.     if (old & LLITOUT)
  166.         return 0;
  167.     tmp = old | LLITOUT;
  168.     ioctl(cur_term->fd, TIOCLSET, &tmp);
  169.     return 1;
  170. }
  171.  
  172. void
  173. _norm_output() {
  174.     ioctl(cur_term->fd, TIOCLSET, &old);
  175. }
  176.  
  177. #else
  178.  
  179. static struct sgttyb old;
  180.  
  181. int
  182. _lit_output() {
  183.     struct sgttyb tmp;
  184.     ioctl(cur_term->fd, TIOCGETP, &old);
  185.     if (old.sg_flags & RAW)
  186.         return 0;
  187.     memcpy((anyptr)&tmp, (anyptr)&old, sizeof(old));
  188.     tmp.sg_flags |= RAW;
  189.     ioctl(cur_term->fd, TIOCSETP, &tmp);
  190.     return 1;
  191. }
  192.  
  193. void
  194. _norm_output() {
  195.     ioctl(cur_term->fd, TIOCSETP, &old);
  196. }
  197.  
  198. #endif 
  199.  
  200. int
  201. _check_tty() {
  202.     if (!(cur_term->prog_mode.v6.sg_flags & RAW) && cur_term->xon)
  203.         cur_term->pad = 0;
  204.     else
  205.         cur_term->pad = 1;
  206.     cur_term->baudrate = _baud_tbl[cur_term->prog_mode.v6.sg_ospeed & 0xf];
  207.     return OK;
  208. }
  209.  
  210. int
  211. def_shell_mode() {
  212.     if (ioctl(cur_term->fd, TIOCGETP, &cur_term->shell_mode.v6) < 0)
  213.         return ERR;
  214. #ifdef TIOCGETC
  215.     if (ioctl(cur_term->fd, TIOCGETC, &cur_term->shell_mode.v7) < 0)
  216.         return ERR;
  217. #endif
  218. #ifdef TIOCLGET
  219.     if (ioctl(cur_term->fd, TIOCLGET, &cur_term->shell_mode.bsd) < 0)
  220.         return ERR;
  221. #endif
  222. #ifdef TIOCGLTC
  223.     if (ioctl(cur_term->fd, TIOCGLTC, &cur_term->shell_mode.bsd_new) < 0)
  224.         return ERR;
  225. #endif
  226. #ifdef USE_WINSZ
  227. #ifdef TIOCGWINSZ
  228.     if (ioctl(cur_term->fd, TIOCGWINSZ, &cur_term->shell_winsz)<0)
  229.         return ERR;
  230. #endif
  231. #endif
  232.     cur_term->termcap = 0;
  233.     return OK;
  234. }
  235.  
  236. int
  237. def_prog_mode() {
  238.     if (ioctl(cur_term->fd, TIOCGETP, &cur_term->prog_mode.v6) < 0)
  239.         return ERR;
  240. #ifdef TIOCGETC
  241.     if (ioctl(cur_term->fd, TIOCGETC, &cur_term->prog_mode.v7) < 0)
  242.         return ERR;
  243. #endif
  244. #ifdef TIOCLGET
  245.     if (ioctl(cur_term->fd, TIOCLGET, &cur_term->prog_mode.bsd) < 0)
  246.         return ERR;
  247. #endif
  248. #ifdef TIOCGLTC
  249.     if (ioctl(cur_term->fd, TIOCGLTC, &cur_term->prog_mode.bsd_new) < 0)
  250.         return ERR;
  251. #endif
  252. #ifdef USE_WINSZ
  253. #ifdef TIOCGWINSZ
  254.     if (ioctl(cur_term->fd, TIOCGWINSZ, &cur_term->prog_winsz)<0)
  255.         return ERR;
  256. #endif
  257. #endif
  258.     return _check_tty();
  259. }
  260.  
  261. int
  262. reset_shell_mode() {
  263.     if (ioctl(cur_term->fd, TIOCSETP, &cur_term->shell_mode.v6) < 0)
  264.         return ERR;
  265. #ifdef TIOCGETC
  266.     if (ioctl(cur_term->fd, TIOCSETC, &cur_term->shell_mode.v7) < 0)
  267.         return ERR;
  268. #endif
  269. #ifdef TIOCLGET
  270.     if (ioctl(cur_term->fd, TIOCLSET, &cur_term->shell_mode.bsd) < 0)
  271.         return ERR;
  272. #endif
  273. #ifdef TIOCGLTC
  274.     if (ioctl(cur_term->fd, TIOCSLTC, &cur_term->shell_mode.bsd_new) < 0)
  275.         return ERR;
  276. #endif
  277.     return OK;
  278. }
  279.  
  280. int
  281. reset_prog_mode() {
  282.     if (ioctl(cur_term->fd, TIOCSETP, &cur_term->prog_mode.v6) < 0)
  283.         return ERR;
  284. #ifdef TIOCGETC
  285.     if (ioctl(cur_term->fd, TIOCSETC, &cur_term->prog_mode.v7) < 0)
  286.         return ERR;
  287. #endif
  288. #ifdef TIOCLGET
  289.     if (ioctl(cur_term->fd, TIOCLSET, &cur_term->prog_mode.bsd) < 0)
  290.         return ERR;
  291. #endif
  292. #ifdef TIOCGLTC
  293.     if (ioctl(cur_term->fd, TIOCSLTC, &cur_term->prog_mode.bsd_new) < 0)
  294.         return ERR;
  295. #endif
  296.     return OK;
  297. }
  298.  
  299. int
  300. _init_tty() {
  301.     cur_term->true_lines = lines;
  302.     cur_term->true_columns = columns;
  303.     if (pad_char == NULL)
  304.         cur_term->padch = '\000';
  305.     else
  306.         cur_term->padch = pad_char[0];
  307.     if (def_shell_mode() == ERR || def_prog_mode() == ERR) {
  308.         cur_term->pad = 0;
  309.         cur_term->baudrate = 1;
  310.         cur_term->xon = 0;
  311.         return OK;
  312.     }
  313. #ifndef TIOCGETC
  314.     cur_term->xon = (xoff_character == NULL || xoff_character[0] == '\021')
  315.             && (xon_character == NULL || xon_character[0] == '\023')
  316.                 && xon_xoff;
  317. #else
  318.     if (xon_xoff) {
  319.         if (xon_character != NULL) {
  320.             cur_term->prog_mode.v7.t_startc = xon_character[0];
  321.             if (ioctl(cur_term->fd, TIOCSETC,
  322.                 &cur_term->prog_mode.v7) < 0)
  323.                 return ERR;
  324.         }
  325.         if (xoff_character != NULL) {
  326.             cur_term->prog_mode.v7.t_stopc = xoff_character[0];
  327.             if (ioctl(cur_term->fd, TIOCSETC,
  328.                       &cur_term->prog_mode.v7) < 0)
  329.                 return ERR;
  330.         }
  331.     }
  332.     cur_term->xon = xon_xoff;
  333. #endif
  334. #ifdef USE_WINSZ
  335. #ifdef TIOCGWINSZ
  336.     if (cur_term->prog_winsz.ws_row != 0
  337.         && cur_term->prog_winsz.ws_col != 0) {
  338.         lines = cur_term->prog_winsz.ws_row;
  339.         columns = cur_term->prog_winsz.ws_col;
  340.     }
  341. #endif
  342. #endif
  343.     return OK;
  344. }
  345.  
  346. #else
  347.  
  348. int
  349. _lit_output() {
  350.     return 0;
  351. }
  352.  
  353. void
  354. _norm_output() {
  355.     return;
  356. }
  357.  
  358. int
  359. _check_tty() {
  360.     return OK;
  361. }
  362.  
  363. int
  364. def_prog_mode() {
  365.     return OK;
  366. }
  367.  
  368. int
  369. reset_prog_mode() {
  370.     return OK;
  371. }
  372.  
  373. int
  374. def_shell_mode() {
  375.     return OK;
  376. }
  377.  
  378. int
  379. reset_shell_mode() {
  380.     return OK;
  381. }
  382.  
  383. int
  384. _init_tty() {
  385.     cur_term->pad = 1;
  386.     cur_term->padch = 0;
  387.     cur_term->baudrate = 1200;
  388.     cur_term->xon = 0;
  389.     cur_term->termcap = 0;
  390.     cur_term->true_lines = lines;
  391.     cur_term->true_columns = columns;
  392. }
  393.  
  394. #endif
  395.  
  396. #endif
  397.