From 8d4b5849f21dc4115ad66944dc11df838b3faa52 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Tue, 11 Mar 2014 16:06:23 -0700 Subject: [PATCH] Clean up our termios implementation. It's safe to fix our constant definitions because we know we never had symbols before, so can't be passing the bad old constants to the new functions, or the correct new constants to the old inlines. Change-Id: I858fc680df39bdd3ba471e867833bdfa71f6224e --- libc/Android.mk | 3 +- libc/bionic/tcgetpgrp.c | 33 ------------ libc/bionic/tcsetpgrp.c | 32 ----------- libc/bionic/termios.cpp | 117 ++++++++++++++++++++++++++++++++++++++++ libc/include/termios.h | 90 +++++-------------------------- 5 files changed, 131 insertions(+), 144 deletions(-) delete mode 100644 libc/bionic/tcgetpgrp.c delete mode 100644 libc/bionic/tcsetpgrp.c create mode 100644 libc/bionic/termios.cpp diff --git a/libc/Android.mk b/libc/Android.mk index 472241fde..bbba7bfda 100644 --- a/libc/Android.mk +++ b/libc/Android.mk @@ -73,8 +73,6 @@ libc_common_src_files := \ bionic/strntoumax.c \ bionic/strtotimeval.c \ bionic/system_properties_compat.c \ - bionic/tcgetpgrp.c \ - bionic/tcsetpgrp.c \ bionic/time64.c \ bionic/unlockpt.c \ stdio/findfp.c \ @@ -216,6 +214,7 @@ libc_bionic_src_files := \ bionic/sys_siglist.c \ bionic/sys_signame.c \ bionic/tdestroy.cpp \ + bionic/termios.cpp \ bionic/thread_atexit.cpp \ bionic/tmpfile.cpp \ bionic/umount.cpp \ diff --git a/libc/bionic/tcgetpgrp.c b/libc/bionic/tcgetpgrp.c deleted file mode 100644 index ebff66aac..000000000 --- a/libc/bionic/tcgetpgrp.c +++ /dev/null @@ -1,33 +0,0 @@ -/* -** Copyright 2006, The Android Open Source Project -** -** Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in the -** documentation and/or other materials provided with the distribution. -** * Neither the name of Google Inc. nor the names of its contributors may -** be used to endorse or promote products derived from this software -** without specific prior written permission. -** -** THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR -** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO -** EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; -** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ -#include -#include - -pid_t tcgetpgrp(int fd) -{ - pid_t _pid; - return ioctl(fd, TIOCGPGRP, &_pid) ? (pid_t)-1 : _pid; -} diff --git a/libc/bionic/tcsetpgrp.c b/libc/bionic/tcsetpgrp.c deleted file mode 100644 index 06d9cd0a7..000000000 --- a/libc/bionic/tcsetpgrp.c +++ /dev/null @@ -1,32 +0,0 @@ -/* -** Copyright 2006, The Android Open Source Project -** -** Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in the -** documentation and/or other materials provided with the distribution. -** * Neither the name of Google Inc. nor the names of its contributors may -** be used to endorse or promote products derived from this software -** without specific prior written permission. -** -** THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR -** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO -** EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; -** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ -#include -#include - -int tcsetpgrp(int fd, pid_t _pid) -{ - return ioctl(fd, TIOCSPGRP, &_pid); -} diff --git a/libc/bionic/termios.cpp b/libc/bionic/termios.cpp new file mode 100644 index 000000000..082dcdc82 --- /dev/null +++ b/libc/bionic/termios.cpp @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include + +static speed_t cfgetspeed(const termios* s) { + return (s->c_cflag & CBAUD); +} + +speed_t cfgetispeed(const termios* s) { + return cfgetspeed(s); +} + +speed_t cfgetospeed(const termios* s) { + return cfgetspeed(s); +} + +void cfmakeraw(termios* s) { + s->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); + s->c_oflag &= ~OPOST; + s->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); + s->c_cflag &= ~(CSIZE|PARENB); + s->c_cflag |= CS8; +} + +int cfsetispeed(termios* s, speed_t speed) { + return cfsetspeed(s, speed); +} + +int cfsetospeed(termios* s, speed_t speed) { + return cfsetspeed(s, speed); +} + +int cfsetspeed(termios* s, speed_t speed) { + // TODO: check 'speed' is valid. + s->c_cflag = (s->c_cflag & ~CBAUD) | (speed & CBAUD); + return 0; +} + +int tcdrain(int fd) { + // A non-zero argument to TCSBRK means "don't send a break". + // The drain is a side-effect of the ioctl! + return ioctl(fd, TCSBRK, static_cast(1)); +} + +int tcflow(int fd, int action) { + return ioctl(fd, TCXONC, static_cast(action)); +} + +int tcflush(int fd, int queue) { + return ioctl(fd, TCFLSH, static_cast(queue)); +} + +int tcgetattr(int fd, termios* s) { + return ioctl(fd, TCGETS, s); +} + +pid_t tcgetsid(int fd) { + pid_t sid; + if (ioctl(fd, TIOCGSID, &sid) == -1) { + return -1; + } + return sid; +} + +int tcsendbreak(int fd, int duration) { + return ioctl(fd, TCSBRKP, static_cast(duration)); +} + +int tcsetattr(int fd, int optional_actions, const termios* s) { + int cmd; + switch (optional_actions) { + case TCSANOW: cmd = TCSETS; break; + case TCSADRAIN: cmd = TCSETSW; break; + case TCSAFLUSH: cmd = TCSETSF; break; + default: errno = EINVAL; return -1; + } + return ioctl(fd, cmd, s); +} + +pid_t tcgetpgrp(int fd) { + pid_t pid; + if (ioctl(fd, TIOCGPGRP, &pid) == -1) { + return -1; + } + return pid; +} + +int tcsetpgrp(int fd, pid_t pid) { + return ioctl(fd, TIOCSPGRP, &pid); +} diff --git a/libc/include/termios.h b/libc/include/termios.h index 0d4435559..b9685ca7a 100644 --- a/libc/include/termios.h +++ b/libc/include/termios.h @@ -31,87 +31,23 @@ #include #include #include -#include #include __BEGIN_DECLS -/* Redefine these to match their ioctl number */ -#undef TCSANOW -#define TCSANOW TCSETS - -#undef TCSADRAIN -#define TCSADRAIN TCSETSW - -#undef TCSAFLUSH -#define TCSAFLUSH TCSETSF - -static __inline__ int tcgetattr(int fd, struct termios *s) -{ - return ioctl(fd, TCGETS, s); -} - -static __inline__ int tcsetattr(int fd, int __opt, const struct termios *s) -{ - return ioctl(fd, __opt, (void *)s); -} - -static __inline__ int tcflow(int fd, int action) -{ - return ioctl(fd, TCXONC, (void *)(intptr_t)action); -} - -static __inline__ int tcflush(int fd, int __queue) -{ - return ioctl(fd, TCFLSH, (void *)(intptr_t)__queue); -} - -static __inline__ int tcdrain(int fd) -{ - return ioctl(fd, TCSBRK, (void *)(intptr_t)1); -} - -static __inline__ pid_t tcgetsid(int fd) -{ - pid_t _pid; - return ioctl(fd, TIOCGSID, &_pid) ? (pid_t)-1 : _pid; -} - -static __inline__ int tcsendbreak(int fd, int __duration) -{ - return ioctl(fd, TCSBRKP, (void *)(uintptr_t)__duration); -} - -static __inline__ speed_t cfgetospeed(const struct termios *s) -{ - return (speed_t)(s->c_cflag & CBAUD); -} - -static __inline__ int cfsetospeed(struct termios *s, speed_t speed) -{ - s->c_cflag = (s->c_cflag & ~CBAUD) | (speed & CBAUD); - return 0; -} - -static __inline__ speed_t cfgetispeed(const struct termios *s) -{ - return (speed_t)(s->c_cflag & CBAUD); -} - -static __inline__ int cfsetispeed(struct termios *s, speed_t speed) -{ - s->c_cflag = (s->c_cflag & ~CBAUD) | (speed & CBAUD); - return 0; -} - -static __inline__ void cfmakeraw(struct termios *s) -{ - s->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); - s->c_oflag &= ~OPOST; - s->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); - s->c_cflag &= ~(CSIZE|PARENB); - s->c_cflag |= CS8; -} +speed_t cfgetispeed(const struct termios*); +speed_t cfgetospeed(const struct termios*); +void cfmakeraw(struct termios*); +int cfsetispeed(struct termios*, speed_t); +int cfsetospeed(struct termios*, speed_t); +int cfsetspeed(struct termios*, speed_t); +int tcdrain(int); +int tcflow(int, int); +int tcflush(int, int); +int tcgetattr(int, struct termios*); +pid_t tcgetsid(int); +int tcsendbreak(int, int); +int tcsetattr(int, int, const struct termios*); __END_DECLS