home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1621 / tty.c < prev    next >
C/C++ Source or Header  |  1990-12-28  |  8KB  |  266 lines

  1. /* Copyright 1990, Daniel J. Bernstein. All rights reserved. */
  2.  
  3. #include "config.h"
  4. #include <sys/ioctl.h>
  5. #include "file.h"
  6. #include "err.h"
  7. #include "tty.h"
  8.  
  9. #define generic GENERIC *
  10.  
  11. static int ioc(fd,req,arg) /* non-interruptable ioctl */
  12. int fd;
  13. unsigned long req;
  14. generic arg;
  15. {
  16.  int result;
  17.  
  18.  do
  19.    result = ioctl(fd,req,(char *) arg);
  20.  while ((result == -1) && (errno == EINTR));
  21.  return result;
  22. }
  23.  
  24. int tty_getctrl()
  25. {
  26.  int fd;
  27.  int dummy;
  28.  
  29. #define ISTTY(f) (ioc(f,(unsigned long) TIOCGPGRP,(generic) &dummy) == 0)
  30.  
  31.  if (ISTTY(3))
  32.    return 3;
  33.  if (((fd = open("/dev/tty",O_RDWR)) != -1) && ISTTY(fd))
  34.    return fd;
  35.  if (((fd = dup(0)) != -1) && ISTTY(fd))
  36.    return fd;
  37.  if (((fd = dup(1)) != -1) && ISTTY(fd))
  38.    return fd;
  39.  return -1;
  40. }
  41.  
  42. tty_setexcl(fd)
  43. int fd;
  44. {
  45.  return ioc(fd,(unsigned long) TIOCEXCL,(generic) 0);
  46.  /* setting exclusive use is a bit unusual but it works */
  47.  /* opening /dev/tty should still be allowed, though */
  48. }
  49.  
  50. tty_setpgrp(fd,pgrp)
  51. int fd;
  52. int pgrp;
  53. {
  54.  return ioc(fd,(unsigned long) TIOCSPGRP,(generic) &pgrp);
  55. }
  56.  
  57. tty_dissoc(fd)
  58. int fd;
  59. {
  60.  return ioc(fd,(unsigned long) TIOCNOTTY,(generic) 0);
  61. }
  62.  
  63. tty_getmodes(fd,tmo)
  64. int fd;
  65. struct ttymodes *tmo;
  66. {
  67.  if (ioc(fd,(unsigned long) TIOCGETD,(generic) &(tmo->di)) == -1) return -1;
  68.  if (ioc(fd,(unsigned long) TIOCGETP,(generic) &(tmo->sg)) == -1) return -1;
  69.  if (ioc(fd,(unsigned long) TIOCGETC,(generic) &(tmo->tc)) == -1) return -1;
  70.  if (ioc(fd,(unsigned long) TIOCLGET,(generic) &(tmo->lb)) == -1) return -1;
  71.  if (ioc(fd,(unsigned long) TIOCGLTC,(generic) &(tmo->lt)) == -1) return -1;
  72. #ifdef TTY_WINDOWS
  73.  if (ioc(fd,(unsigned long) TIOCGWINSZ,(generic) &(tmo->ws)) == -1) return -1;
  74. #endif
  75. #ifdef TTY_AUXCHARS
  76.  if (ioc(fd,(unsigned long) TIOCGAUXC,(generic) &(tmo->au)) == -1) return -1;
  77. #endif
  78.  return 0;
  79. }
  80.  
  81. tty_setmodes(fd,tmo)
  82. int fd;
  83. struct ttymodes *tmo;
  84. {
  85.  if (ioc(fd,(unsigned long) TIOCSETD,(generic) &(tmo->di)) == -1) return -1;
  86.  if (ioc(fd,(unsigned long) TIOCSETP,(generic) &(tmo->sg)) == -1) return -1;
  87.  if (ioc(fd,(unsigned long) TIOCSETC,(generic) &(tmo->tc)) == -1) return -1;
  88.  if (ioc(fd,(unsigned long) TIOCLSET,(generic) &(tmo->lb)) == -1) return -1;
  89.  if (ioc(fd,(unsigned long) TIOCSLTC,(generic) &(tmo->lt)) == -1) return -1;
  90. #ifdef TTY_WINDOWS
  91.  if (ioc(fd,(unsigned long) TIOCSWINSZ,(generic) &(tmo->ws)) == -1) return -1;
  92. #endif
  93. #ifdef TTY_AUXCHARS
  94.  if (ioc(fd,(unsigned long) TIOCSAUXC,(generic) &(tmo->au)) == -1) return -1;
  95. #endif
  96.  return 0;
  97. }
  98.  
  99. tty_modifymodes(fd,tmonew,tmoold)
  100. int fd;
  101. struct ttymodes *tmonew;
  102. struct ttymodes *tmoold;
  103. {
  104.  if (tmonew->di != tmoold->di)
  105.    if (ioc(fd,(unsigned long) TIOCSETD,(generic) &(tmonew->di)) == -1) return -1;
  106.  if ((tmonew->sg.sg_flags != tmoold->sg.sg_flags)
  107.    ||(tmonew->sg.sg_ispeed != tmoold->sg.sg_ispeed)
  108.    ||(tmonew->sg.sg_ospeed != tmoold->sg.sg_ospeed)
  109.    ||(tmonew->sg.sg_erase != tmoold->sg.sg_erase)
  110.    ||(tmonew->sg.sg_kill != tmoold->sg.sg_kill))
  111.    if (ioc(fd,(unsigned long) TIOCSETP,(generic) &(tmonew->sg)) == -1) return -1;
  112.  if ((tmonew->tc.t_intrc != tmoold->tc.t_intrc)
  113.    ||(tmonew->tc.t_quitc != tmoold->tc.t_quitc)
  114.    ||(tmonew->tc.t_startc != tmoold->tc.t_startc)
  115.    ||(tmonew->tc.t_stopc != tmoold->tc.t_stopc)
  116.    ||(tmonew->tc.t_eofc != tmoold->tc.t_eofc)
  117.    ||(tmonew->tc.t_brkc != tmoold->tc.t_brkc))
  118.    if (ioc(fd,(unsigned long) TIOCSETC,(generic) &(tmonew->tc)) == -1) return -1;
  119.  if (tmonew->lb != tmoold->lb)
  120.    if (ioc(fd,(unsigned long) TIOCLSET,(generic) &(tmonew->lb)) == -1) return -1;
  121.  if ((tmonew->lt.t_suspc != tmoold->lt.t_suspc)
  122.    ||(tmonew->lt.t_dsuspc != tmoold->lt.t_dsuspc)
  123.    ||(tmonew->lt.t_rprntc != tmoold->lt.t_rprntc)
  124.    ||(tmonew->lt.t_flushc != tmoold->lt.t_flushc)
  125.    ||(tmonew->lt.t_werasc != tmoold->lt.t_werasc)
  126.    ||(tmonew->lt.t_lnextc != tmoold->lt.t_lnextc))
  127.    if (ioc(fd,(unsigned long) TIOCSLTC,(generic) &(tmonew->lt)) == -1) return -1;
  128. #ifdef TTY_WINDOWS
  129.  if ((tmonew->ws.ws_xpixel != tmoold->ws.ws_xpixel)
  130.    ||(tmonew->ws.ws_ypixel != tmoold->ws.ws_ypixel)
  131.    ||(tmonew->ws.ws_row != tmoold->ws.ws_row)
  132.    ||(tmonew->ws.ws_col != tmoold->ws.ws_col))
  133.    if (ioc(fd,(unsigned long)TIOCSWINSZ,(generic)&(tmonew->ws))==-1) return -1;
  134. #endif
  135. #ifdef TTY_AUXCHARS
  136.  if ((tmonew->au.t_usemap != tmoold->au.t_usemap)
  137.    ||(tmonew->au.t_usest != tmoold->au.t_usest))
  138.    if (ioc(fd,(unsigned long)TIOCSAUXC,(generic)&(tmonew->au))==-1) return -1;
  139. #endif
  140.  return 0;
  141. }
  142.  
  143. /* XXX: Should use copy() for this. */
  144. void tty_copymodes(tmonew,tmoold)
  145. struct ttymodes *tmonew;
  146. struct ttymodes *tmoold;
  147. {
  148.  tmonew->di = tmoold->di;
  149.  tmonew->sg.sg_ispeed = tmoold->sg.sg_ispeed;
  150.  tmonew->sg.sg_ospeed = tmoold->sg.sg_ospeed;
  151.  tmonew->sg.sg_erase = tmoold->sg.sg_erase;
  152.  tmonew->sg.sg_kill = tmoold->sg.sg_kill;
  153.  tmonew->sg.sg_flags = tmoold->sg.sg_flags;
  154.  tmonew->tc.t_intrc = tmoold->tc.t_intrc;
  155.  tmonew->tc.t_quitc = tmoold->tc.t_quitc;
  156.  tmonew->tc.t_startc = tmoold->tc.t_startc;
  157.  tmonew->tc.t_stopc = tmoold->tc.t_stopc;
  158.  tmonew->tc.t_eofc = tmoold->tc.t_eofc;
  159.  tmonew->tc.t_brkc = tmoold->tc.t_brkc;
  160.  tmonew->lb = tmoold->lb;
  161.  tmonew->lt.t_suspc = tmoold->lt.t_suspc;
  162.  tmonew->lt.t_dsuspc = tmoold->lt.t_dsuspc;
  163.  tmonew->lt.t_rprntc = tmoold->lt.t_rprntc;
  164.  tmonew->lt.t_flushc = tmoold->lt.t_flushc;
  165.  tmonew->lt.t_werasc = tmoold->lt.t_werasc;
  166.  tmonew->lt.t_lnextc = tmoold->lt.t_lnextc;
  167. #ifdef TTY_WINDOWS
  168.  tmonew->ws.ws_xpixel = tmoold->ws.ws_xpixel;
  169.  tmonew->ws.ws_ypixel = tmoold->ws.ws_ypixel;
  170.  tmonew->ws.ws_row = tmoold->ws.ws_row;
  171.  tmonew->ws.ws_col = tmoold->ws.ws_col;
  172. #endif
  173. #ifdef TTY_AUXCHARS
  174.  tmonew->au.t_usest = tmoold->au.t_usest;
  175.  tmonew->au.t_usemap = tmoold->au.t_usemap;
  176. #endif
  177. }
  178.  
  179. void tty_copywin(tmonew,tmoold)
  180. struct ttymodes *tmonew;
  181. struct ttymodes *tmoold;
  182. {
  183.  ;
  184. #ifdef TTY_WINDOWS
  185.  tmonew->ws.ws_xpixel = tmoold->ws.ws_xpixel;
  186.  tmonew->ws.ws_ypixel = tmoold->ws.ws_ypixel;
  187.  tmonew->ws.ws_row = tmoold->ws.ws_row;
  188.  tmonew->ws.ws_col = tmoold->ws.ws_col;
  189. #endif
  190. }
  191.  
  192. void tty_charmode(tmo)
  193. struct ttymodes *tmo;
  194. {
  195.  tty_mungemodes(tmo,3,0,2,0,3,0);
  196. }
  197.  
  198. void tty_mungemodes(tmo,cbreak,new,echo,crmod,raw,crt)
  199. struct ttymodes *tmo;
  200. int cbreak;
  201. int new;
  202. int echo;
  203. int crmod;
  204. int raw;
  205. int crt;
  206. {
  207.  if (new >= 2)
  208.    tmo->di = ((new == 3) ? NTTYDISC : OTTYDISC);
  209.  if (crmod >= 2)
  210.    tmo->sg.sg_flags = (tmo->sg.sg_flags & ~CRMOD) | (CRMOD * (crmod == 3));
  211.  if (echo >= 2)
  212.    tmo->sg.sg_flags = (tmo->sg.sg_flags & ~ECHO) | (ECHO * (echo == 3));
  213.  if (raw >= 2)
  214.    tmo->sg.sg_flags = (tmo->sg.sg_flags & ~RAW) | (RAW * (raw == 3));
  215.  if (cbreak >= 2)
  216.    tmo->sg.sg_flags = (tmo->sg.sg_flags & ~CBREAK) | (CBREAK * (cbreak == 3));
  217.  if (crt >= 2)
  218.    tmo->lb = (tmo->lb & ~(CRTBS | CRTERA | CRTKIL | CTLECH))
  219.                       | ((CRTBS | CRTERA | CRTKIL | CTLECH) * (crt == 3));
  220. }
  221.  
  222. void tty_initmodes(tmo,cbreak,new,echo,crmod,raw,crt)
  223. struct ttymodes *tmo;
  224. int cbreak;
  225. int new;
  226. int echo;
  227. int crmod;
  228. int raw;
  229. int crt;
  230. {
  231.  /* Here we specify Ye Standard BSD Terminal Settings. */
  232.  
  233.  tmo->di = ((new % 2) ? NTTYDISC : OTTYDISC);
  234.  tmo->sg.sg_ispeed = EXTB;
  235.  tmo->sg.sg_ospeed = EXTB;
  236.  tmo->sg.sg_erase = 127; /* del */
  237.  tmo->sg.sg_kill = 21; /* ^U */
  238.  tmo->sg.sg_flags = EVENP | ODDP | XTABS
  239.    | (CRMOD * (crmod % 2)) | (ECHO * (echo % 2))
  240.    | (RAW * (raw % 2)) | (CBREAK * (cbreak % 2));
  241.  tmo->tc.t_intrc = 3; /* ^C */
  242.  tmo->tc.t_quitc = 28; /* ^\ */
  243.  tmo->tc.t_startc = 17; /* ^Q */
  244.  tmo->tc.t_stopc = 19; /* ^S */
  245.  tmo->tc.t_eofc = 4; /* ^D */
  246.  tmo->tc.t_brkc = -1; /* undef */
  247.  tmo->lb = ((CRTBS | CRTERA | CRTKIL | CTLECH) * (crt % 2)) | DECCTQ;
  248.  tmo->lt.t_suspc = 26; /* ^Z */
  249.  tmo->lt.t_dsuspc = 25; /* ^Y */
  250.  tmo->lt.t_rprntc = 18; /* ^R */
  251.  tmo->lt.t_flushc = 15; /* ^O */
  252.  tmo->lt.t_werasc = 23; /* ^W */
  253.  tmo->lt.t_lnextc = 22; /* ^V */
  254. #ifdef TTY_WINDOWS
  255.  tmo->ws.ws_xpixel = 0; /* Or read from TERMCAP? Hmmm */
  256.  tmo->ws.ws_ypixel = 0;
  257.  tmo->ws.ws_row = 0;
  258.  tmo->ws.ws_col = 0;
  259. #endif
  260. #ifdef TTY_AUXCHARS
  261.  tmo->au.t_usest = 20; /* ^T */
  262.  tmo->au.t_usemap = UST_LOAD1 | UST_LOAD5 | UST_LOAD15 | UST_RAWCPU
  263.    | UST_UPTIME | UST_PGRP | UST_CHILDS | UST_PCPU | UST_STATE;
  264. #endif
  265. }
  266.