144301Smarc /*- 2*61111Sbostic * Copyright (c) 1989, 1993 3*61111Sbostic * The Regents of the University of California. All rights reserved. 438597Skarels * 544301Smarc * %sccs.include.redist.c% 638597Skarels */ 738597Skarels 838597Skarels #if defined(LIBC_SCCS) && !defined(lint) 9*61111Sbostic static char sccsid[] = "@(#)termios.c 8.1 (Berkeley) 06/04/93"; 1038597Skarels #endif /* LIBC_SCCS and not lint */ 1138597Skarels 1235766Smarc #include <sys/types.h> 1344301Smarc #include <sys/ioctl.h> 1444301Smarc #include <sys/tty.h> 1552295Sbostic #define KERNEL /* XXX - FREAD and FWRITE ifdef'd KERNEL*/ 1647868Smarc #include <sys/fcntl.h> 1747868Smarc #undef KERNEL 1856412Sbostic 1956412Sbostic #include <errno.h> 2056412Sbostic #include <stdio.h> 2146597Sdonn #include <termios.h> 2246597Sdonn #include <unistd.h> 2335766Smarc 2446597Sdonn int 2535766Smarc tcgetattr(fd, t) 2635766Smarc int fd; 2735766Smarc struct termios *t; 2835766Smarc { 2935766Smarc 3052295Sbostic return (ioctl(fd, TIOCGETA, t)); 3135766Smarc } 3235766Smarc 3346597Sdonn int 3435766Smarc tcsetattr(fd, opt, t) 3535766Smarc int fd, opt; 3646597Sdonn const struct termios *t; 3735766Smarc { 3844301Smarc struct termios localterm; 3935766Smarc 4044301Smarc if (opt & TCSASOFT) { 4144301Smarc localterm = *t; 4244301Smarc localterm.c_cflag |= CIGNORE; 4344301Smarc t = &localterm; 4435766Smarc } 4552489Skarels switch (opt & ~TCSASOFT) { 4652295Sbostic case TCSANOW: 4744301Smarc return (ioctl(fd, TIOCSETA, t)); 4852295Sbostic case TCSADRAIN: 4944301Smarc return (ioctl(fd, TIOCSETAW, t)); 5052489Skarels case TCSAFLUSH: 5152295Sbostic return (ioctl(fd, TIOCSETAF, t)); 5252295Sbostic default: 5352295Sbostic errno = EINVAL; 5452295Sbostic return (-1); 5552295Sbostic } 5635766Smarc } 5735766Smarc 5846597Sdonn int 5946597Sdonn #if __STDC__ 6046597Sdonn tcsetpgrp(int fd, pid_t pgrp) 6146597Sdonn #else 6235766Smarc tcsetpgrp(fd, pgrp) 6346597Sdonn int fd; 6446597Sdonn pid_t pgrp; 6546597Sdonn #endif 6635766Smarc { 6749822Sbostic int s; 6847868Smarc 6949822Sbostic s = pgrp; 7052295Sbostic return (ioctl(fd, TIOCSPGRP, &s)); 7135766Smarc } 7235766Smarc 7346597Sdonn pid_t 7435766Smarc tcgetpgrp(fd) 7535766Smarc { 7647731Sbostic int s; 7735766Smarc 7847731Sbostic if (ioctl(fd, TIOCGPGRP, &s) < 0) 7952295Sbostic return ((pid_t)-1); 8047868Smarc 8152295Sbostic return ((pid_t)s); 8235766Smarc } 8335766Smarc 8446597Sdonn speed_t 8535766Smarc cfgetospeed(t) 8646597Sdonn const struct termios *t; 8735766Smarc { 8847868Smarc 8952295Sbostic return (t->c_ospeed); 9035766Smarc } 9135766Smarc 9246597Sdonn speed_t 9335766Smarc cfgetispeed(t) 9446597Sdonn const struct termios *t; 9535766Smarc { 9647868Smarc 9752295Sbostic return (t->c_ispeed); 9835766Smarc } 9935766Smarc 10046597Sdonn int 10135766Smarc cfsetospeed(t, speed) 10235766Smarc struct termios *t; 10346597Sdonn speed_t speed; 10435766Smarc { 10552492Skarels 10635766Smarc t->c_ospeed = speed; 10747868Smarc return (0); 10835766Smarc } 10935766Smarc 11046597Sdonn int 11135766Smarc cfsetispeed(t, speed) 11235766Smarc struct termios *t; 11346597Sdonn speed_t speed; 11435766Smarc { 11552492Skarels 11635766Smarc t->c_ispeed = speed; 11747868Smarc return (0); 11835766Smarc } 11935766Smarc 12052295Sbostic int 12135766Smarc cfsetspeed(t, speed) 12235766Smarc struct termios *t; 12346597Sdonn speed_t speed; 12435766Smarc { 12552492Skarels 12635766Smarc t->c_ispeed = t->c_ospeed = speed; 12752295Sbostic return (0); 12835766Smarc } 12935766Smarc 13046248Skarels /* 13152295Sbostic * Make a pre-existing termios structure into "raw" mode: character-at-a-time 13252295Sbostic * mode with no characters interpreted, 8-bit data path. 13346248Skarels */ 13446597Sdonn void 13535766Smarc cfmakeraw(t) 13635766Smarc struct termios *t; 13735766Smarc { 13852492Skarels 13946248Skarels t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); 14046248Skarels t->c_oflag &= ~OPOST; 14138596Skarels t->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); 14246248Skarels t->c_cflag &= ~(CSIZE|PARENB); 14346248Skarels t->c_cflag |= CS8; 14452295Sbostic /* XXX set MIN/TIME */ 14535766Smarc } 14647868Smarc 14747868Smarc tcsendbreak(fd, len) 14847868Smarc int fd, len; 14947868Smarc { 15047868Smarc struct timeval sleepytime; 15147868Smarc 15247868Smarc sleepytime.tv_sec = 0; 15347868Smarc sleepytime.tv_usec = 400000; 15447868Smarc if (ioctl(fd, TIOCSBRK, 0) == -1) 15547868Smarc return (-1); 15652295Sbostic (void)select(0, 0, 0, 0, &sleepytime); 15747868Smarc if (ioctl(fd, TIOCCBRK, 0) == -1) 15847868Smarc return (-1); 15947868Smarc return (0); 16047868Smarc } 16147868Smarc 16247868Smarc tcdrain(fd) 16347868Smarc int fd; 16447868Smarc { 16552492Skarels 16652492Skarels return (ioctl(fd, TIOCDRAIN, 0)); 16747868Smarc } 16847868Smarc 16947868Smarc tcflush(fd, which) 17047868Smarc int fd, which; 17147868Smarc { 17247868Smarc int com; 17347868Smarc 17447868Smarc switch (which) { 17547868Smarc case TCIFLUSH: 17647868Smarc com = FREAD; 17747868Smarc break; 17847868Smarc case TCOFLUSH: 17947868Smarc com = FWRITE; 18047868Smarc break; 18147868Smarc case TCIOFLUSH: 18247868Smarc com = FREAD | FWRITE; 18347868Smarc break; 18447868Smarc default: 18547868Smarc errno = EINVAL; 18647868Smarc return (-1); 18747868Smarc } 18852492Skarels return (ioctl(fd, TIOCFLUSH, &com)); 18947868Smarc } 19047868Smarc 19147868Smarc tcflow(fd, action) 19247868Smarc int fd, action; 19347868Smarc { 19452295Sbostic struct termios term; 19552295Sbostic u_char c; 19652295Sbostic 19747868Smarc switch (action) { 19847868Smarc case TCOOFF: 19952492Skarels return (ioctl(fd, TIOCSTOP, 0)); 20047868Smarc case TCOON: 20152492Skarels return (ioctl(fd, TIOCSTART, 0)); 20252295Sbostic case TCION: 20347868Smarc case TCIOFF: 20448860Smarc if (tcgetattr(fd, &term) == -1) 20547868Smarc return (-1); 20647868Smarc c = term.c_cc[action == TCIOFF ? VSTOP : VSTART]; 20752295Sbostic if (c != _POSIX_VDISABLE && write(fd, &c, sizeof(c)) == -1) 20847868Smarc return (-1); 20952295Sbostic return (0); 21047868Smarc default: 21147868Smarc errno = EINVAL; 21247868Smarc return (-1); 21347868Smarc } 21452295Sbostic /* NOTREACHED */ 21547868Smarc } 216