144301Smarc /*- 238597Skarels * Copyright (c) 1989 The Regents of the University of California. 338597Skarels * All rights reserved. 438597Skarels * 544301Smarc * %sccs.include.redist.c% 638597Skarels */ 738597Skarels 838597Skarels #if defined(LIBC_SCCS) && !defined(lint) 9*52295Sbostic static char sccsid[] = "@(#)termios.c 5.10 (Berkeley) 02/03/92"; 1038597Skarels #endif /* LIBC_SCCS and not lint */ 1138597Skarels 1235766Smarc #include <sys/types.h> 1335766Smarc #include <sys/errno.h> 1444301Smarc #include <sys/ioctl.h> 1544301Smarc #include <sys/tty.h> 16*52295Sbostic #define KERNEL /* XXX - FREAD and FWRITE ifdef'd KERNEL*/ 1747868Smarc #include <sys/fcntl.h> 1847868Smarc #undef KERNEL 1946597Sdonn #include <termios.h> 2035766Smarc #include <stdio.h> 2146597Sdonn #include <unistd.h> 2235766Smarc 2346597Sdonn int 2435766Smarc tcgetattr(fd, t) 2535766Smarc int fd; 2635766Smarc struct termios *t; 2735766Smarc { 2835766Smarc 29*52295Sbostic return (ioctl(fd, TIOCGETA, t)); 3035766Smarc } 3135766Smarc 3246597Sdonn int 3335766Smarc tcsetattr(fd, opt, t) 3435766Smarc int fd, opt; 3546597Sdonn const struct termios *t; 3635766Smarc { 3744301Smarc struct termios localterm; 3835766Smarc 3944301Smarc if (opt & TCSASOFT) { 4044301Smarc localterm = *t; 4144301Smarc localterm.c_cflag |= CIGNORE; 4244301Smarc t = &localterm; 4335766Smarc } 44*52295Sbostic switch(opt & ~TCSASOFT) { 45*52295Sbostic case TCSANOW: 4644301Smarc return (ioctl(fd, TIOCSETA, t)); 47*52295Sbostic case TCSADRAIN: 4844301Smarc return (ioctl(fd, TIOCSETAW, t)); 49*52295Sbostic case TIOCSETAF: 50*52295Sbostic return (ioctl(fd, TIOCSETAF, t)); 51*52295Sbostic default: 52*52295Sbostic errno = EINVAL; 53*52295Sbostic return (-1); 54*52295Sbostic } 5535766Smarc } 5635766Smarc 5746597Sdonn int 5846597Sdonn #if __STDC__ 5946597Sdonn tcsetpgrp(int fd, pid_t pgrp) 6046597Sdonn #else 6135766Smarc tcsetpgrp(fd, pgrp) 6246597Sdonn int fd; 6346597Sdonn pid_t pgrp; 6446597Sdonn #endif 6535766Smarc { 6649822Sbostic int s; 6747868Smarc 6849822Sbostic s = pgrp; 69*52295Sbostic return (ioctl(fd, TIOCSPGRP, &s)); 7035766Smarc } 7135766Smarc 7246597Sdonn pid_t 7335766Smarc tcgetpgrp(fd) 7435766Smarc { 7547731Sbostic int s; 7635766Smarc 7747731Sbostic if (ioctl(fd, TIOCGPGRP, &s) < 0) 78*52295Sbostic return ((pid_t)-1); 7947868Smarc 80*52295Sbostic return ((pid_t)s); 8135766Smarc } 8235766Smarc 8346597Sdonn speed_t 8435766Smarc cfgetospeed(t) 8546597Sdonn const struct termios *t; 8635766Smarc { 8747868Smarc 88*52295Sbostic return (t->c_ospeed); 8935766Smarc } 9035766Smarc 9146597Sdonn speed_t 9235766Smarc cfgetispeed(t) 9346597Sdonn const struct termios *t; 9435766Smarc { 9547868Smarc 96*52295Sbostic return (t->c_ispeed); 9735766Smarc } 9835766Smarc 9946597Sdonn int 10035766Smarc cfsetospeed(t, speed) 10135766Smarc struct termios *t; 10246597Sdonn speed_t speed; 10335766Smarc { 10435766Smarc t->c_ospeed = speed; 10547868Smarc return (0); 10635766Smarc } 10735766Smarc 10846597Sdonn int 10935766Smarc cfsetispeed(t, speed) 11035766Smarc struct termios *t; 11146597Sdonn speed_t speed; 11235766Smarc { 11335766Smarc t->c_ispeed = speed; 11447868Smarc return (0); 11535766Smarc } 11635766Smarc 117*52295Sbostic int 11835766Smarc cfsetspeed(t, speed) 11935766Smarc struct termios *t; 12046597Sdonn speed_t speed; 12135766Smarc { 12235766Smarc t->c_ispeed = t->c_ospeed = speed; 123*52295Sbostic return (0); 12435766Smarc } 12535766Smarc 12646248Skarels /* 127*52295Sbostic * Make a pre-existing termios structure into "raw" mode: character-at-a-time 128*52295Sbostic * mode with no characters interpreted, 8-bit data path. 12946248Skarels */ 13046597Sdonn void 13135766Smarc cfmakeraw(t) 13235766Smarc struct termios *t; 13335766Smarc { 13446248Skarels t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); 13546248Skarels t->c_oflag &= ~OPOST; 13638596Skarels t->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); 13746248Skarels t->c_cflag &= ~(CSIZE|PARENB); 13846248Skarels t->c_cflag |= CS8; 139*52295Sbostic /* XXX set MIN/TIME */ 14035766Smarc } 14147868Smarc 14247868Smarc tcsendbreak(fd, len) 14347868Smarc int fd, len; 14447868Smarc { 14547868Smarc struct timeval sleepytime; 14647868Smarc 14747868Smarc sleepytime.tv_sec = 0; 14847868Smarc sleepytime.tv_usec = 400000; 14947868Smarc if (ioctl(fd, TIOCSBRK, 0) == -1) 15047868Smarc return (-1); 151*52295Sbostic (void)select(0, 0, 0, 0, &sleepytime); 15247868Smarc if (ioctl(fd, TIOCCBRK, 0) == -1) 15347868Smarc return (-1); 15447868Smarc return (0); 15547868Smarc } 15647868Smarc 15747868Smarc tcdrain(fd) 15847868Smarc int fd; 15947868Smarc { 160*52295Sbostic return (ioctl(fd, TIOCDRAIN, 0) == -1 ? -1 : 0); 16147868Smarc } 16247868Smarc 16347868Smarc tcflush(fd, which) 16447868Smarc int fd, which; 16547868Smarc { 16647868Smarc int com; 16747868Smarc 16847868Smarc switch (which) { 16947868Smarc case TCIFLUSH: 17047868Smarc com = FREAD; 17147868Smarc break; 17247868Smarc case TCOFLUSH: 17347868Smarc com = FWRITE; 17447868Smarc break; 17547868Smarc case TCIOFLUSH: 17647868Smarc com = FREAD | FWRITE; 17747868Smarc break; 17847868Smarc default: 17947868Smarc errno = EINVAL; 18047868Smarc return (-1); 18147868Smarc } 182*52295Sbostic return (ioctl(fd, TIOCFLUSH, &com) == -1 ? -1 : 0); 18347868Smarc } 18447868Smarc 18547868Smarc tcflow(fd, action) 18647868Smarc int fd, action; 18747868Smarc { 188*52295Sbostic struct termios term; 189*52295Sbostic u_char c; 190*52295Sbostic 19147868Smarc switch (action) { 19247868Smarc case TCOOFF: 193*52295Sbostic return (ioctl(fd, TIOCSTOP, 0) == -1 ? -1 : 0); 19447868Smarc case TCOON: 195*52295Sbostic return (ioctl(fd, TIOCSTART, 0) == -1 ? -1 : 0); 196*52295Sbostic case TCION: 19747868Smarc case TCIOFF: 19848860Smarc if (tcgetattr(fd, &term) == -1) 19947868Smarc return (-1); 20047868Smarc c = term.c_cc[action == TCIOFF ? VSTOP : VSTART]; 201*52295Sbostic if (c != _POSIX_VDISABLE && write(fd, &c, sizeof(c)) == -1) 20247868Smarc return (-1); 203*52295Sbostic return (0); 20447868Smarc default: 20547868Smarc errno = EINVAL; 20647868Smarc return (-1); 20747868Smarc } 208*52295Sbostic /* NOTREACHED */ 20947868Smarc } 210