Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * shtty.c -- abstract interface to the terminal, focusing on capabilities. |
| 3 | */ |
| 4 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 5 | /* Copyright (C) 1999 Free Software Foundation, Inc. |
| 6 | |
| 7 | This file is part of GNU Bash, the Bourne Again SHell. |
| 8 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 9 | Bash is free software: you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation, either version 3 of the License, or |
| 12 | (at your option) any later version. |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 13 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 14 | Bash is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 18 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 19 | You should have received a copy of the GNU General Public License |
| 20 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
| 21 | */ |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | # include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #ifdef HAVE_UNISTD_H |
| 28 | # include <unistd.h> |
| 29 | #endif |
| 30 | |
| 31 | #include <shtty.h> |
| 32 | |
| 33 | static TTYSTRUCT ttin, ttout; |
| 34 | static int ttsaved = 0; |
| 35 | |
| 36 | int |
| 37 | ttgetattr(fd, ttp) |
| 38 | int fd; |
| 39 | TTYSTRUCT *ttp; |
| 40 | { |
| 41 | #ifdef TERMIOS_TTY_DRIVER |
| 42 | return tcgetattr(fd, ttp); |
| 43 | #else |
| 44 | # ifdef TERMIO_TTY_DRIVER |
| 45 | return ioctl(fd, TCGETA, ttp); |
| 46 | # else |
| 47 | return ioctl(fd, TIOCGETP, ttp); |
| 48 | # endif |
| 49 | #endif |
| 50 | } |
| 51 | |
| 52 | int |
| 53 | ttsetattr(fd, ttp) |
| 54 | int fd; |
| 55 | TTYSTRUCT *ttp; |
| 56 | { |
| 57 | #ifdef TERMIOS_TTY_DRIVER |
| 58 | return tcsetattr(fd, TCSADRAIN, ttp); |
| 59 | #else |
| 60 | # ifdef TERMIO_TTY_DRIVER |
| 61 | return ioctl(fd, TCSETAW, ttp); |
| 62 | # else |
| 63 | return ioctl(fd, TIOCSETN, ttp); |
| 64 | # endif |
| 65 | #endif |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | ttsave() |
| 70 | { |
| 71 | if (ttsaved) |
| 72 | return; |
| 73 | ttgetattr (0, &ttin); |
| 74 | ttgetattr (1, &ttout); |
| 75 | ttsaved = 1; |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | ttrestore() |
| 80 | { |
| 81 | if (ttsaved == 0) |
| 82 | return; |
| 83 | ttsetattr (0, &ttin); |
| 84 | ttsetattr (1, &ttout); |
| 85 | ttsaved = 0; |
| 86 | } |
| 87 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 88 | /* Retrieve the internally-saved attributes associated with tty fd FD. */ |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 89 | TTYSTRUCT * |
| 90 | ttattr (fd) |
| 91 | int fd; |
| 92 | { |
| 93 | if (ttsaved == 0) |
| 94 | return ((TTYSTRUCT *)0); |
| 95 | if (fd == 0) |
| 96 | return &ttin; |
| 97 | else if (fd == 1) |
| 98 | return &ttout; |
| 99 | else |
| 100 | return ((TTYSTRUCT *)0); |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * Change attributes in ttp so that when it is installed using |
| 105 | * ttsetattr, the terminal will be in one-char-at-a-time mode. |
| 106 | */ |
| 107 | int |
| 108 | tt_setonechar(ttp) |
| 109 | TTYSTRUCT *ttp; |
| 110 | { |
| 111 | #if defined (TERMIOS_TTY_DRIVER) || defined (TERMIO_TTY_DRIVER) |
| 112 | |
| 113 | /* XXX - might not want this -- it disables erase and kill processing. */ |
| 114 | ttp->c_lflag &= ~ICANON; |
| 115 | |
| 116 | ttp->c_lflag |= ISIG; |
| 117 | # ifdef IEXTEN |
| 118 | ttp->c_lflag |= IEXTEN; |
| 119 | # endif |
| 120 | |
| 121 | ttp->c_iflag |= ICRNL; /* make sure we get CR->NL on input */ |
| 122 | ttp->c_iflag &= ~INLCR; /* but no NL->CR */ |
| 123 | |
| 124 | # ifdef OPOST |
| 125 | ttp->c_oflag |= OPOST; |
| 126 | # endif |
| 127 | # ifdef ONLCR |
| 128 | ttp->c_oflag |= ONLCR; |
| 129 | # endif |
| 130 | # ifdef OCRNL |
| 131 | ttp->c_oflag &= ~OCRNL; |
| 132 | # endif |
| 133 | # ifdef ONOCR |
| 134 | ttp->c_oflag &= ~ONOCR; |
| 135 | # endif |
| 136 | # ifdef ONLRET |
| 137 | ttp->c_oflag &= ~ONLRET; |
| 138 | # endif |
| 139 | |
| 140 | ttp->c_cc[VMIN] = 1; |
| 141 | ttp->c_cc[VTIME] = 0; |
| 142 | |
| 143 | #else |
| 144 | |
| 145 | ttp->sg_flags |= CBREAK; |
| 146 | |
| 147 | #endif |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 152 | /* Set the tty associated with FD and TTP into one-character-at-a-time mode */ |
| 153 | int |
| 154 | ttfd_onechar (fd, ttp) |
| 155 | int fd; |
| 156 | TTYSTRUCT *ttp; |
| 157 | { |
| 158 | if (tt_setonechar(ttp) < 0) |
| 159 | return -1; |
| 160 | return (ttsetattr (fd, ttp)); |
| 161 | } |
| 162 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 163 | /* Set the terminal into one-character-at-a-time mode */ |
| 164 | int |
| 165 | ttonechar () |
| 166 | { |
| 167 | TTYSTRUCT tt; |
| 168 | |
| 169 | if (ttsaved == 0) |
| 170 | return -1; |
| 171 | tt = ttin; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 172 | return (ttfd_onechar (0, &tt)); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Change attributes in ttp so that when it is installed using |
| 177 | * ttsetattr, the terminal will be in no-echo mode. |
| 178 | */ |
| 179 | int |
| 180 | tt_setnoecho(ttp) |
| 181 | TTYSTRUCT *ttp; |
| 182 | { |
| 183 | #if defined (TERMIOS_TTY_DRIVER) || defined (TERMIO_TTY_DRIVER) |
| 184 | ttp->c_lflag &= ~(ECHO|ECHOK|ECHONL); |
| 185 | #else |
| 186 | ttp->sg_flags &= ~ECHO; |
| 187 | #endif |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 192 | /* Set the tty associated with FD and TTP into no-echo mode */ |
| 193 | int |
| 194 | ttfd_noecho (fd, ttp) |
| 195 | int fd; |
| 196 | TTYSTRUCT *ttp; |
| 197 | { |
| 198 | if (tt_setnoecho (ttp) < 0) |
| 199 | return -1; |
| 200 | return (ttsetattr (fd, ttp)); |
| 201 | } |
| 202 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 203 | /* Set the terminal into no-echo mode */ |
| 204 | int |
| 205 | ttnoecho () |
| 206 | { |
| 207 | TTYSTRUCT tt; |
| 208 | |
| 209 | if (ttsaved == 0) |
| 210 | return -1; |
| 211 | tt = ttin; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 212 | return (ttfd_noecho (0, &tt)); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Change attributes in ttp so that when it is installed using |
| 217 | * ttsetattr, the terminal will be in eight-bit mode (pass8). |
| 218 | */ |
| 219 | int |
| 220 | tt_seteightbit (ttp) |
| 221 | TTYSTRUCT *ttp; |
| 222 | { |
| 223 | #if defined (TERMIOS_TTY_DRIVER) || defined (TERMIO_TTY_DRIVER) |
| 224 | ttp->c_iflag &= ~ISTRIP; |
| 225 | ttp->c_cflag |= CS8; |
| 226 | ttp->c_cflag &= ~PARENB; |
| 227 | #else |
| 228 | ttp->sg_flags |= ANYP; |
| 229 | #endif |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 234 | /* Set the tty associated with FD and TTP into eight-bit mode */ |
| 235 | int |
| 236 | ttfd_eightbit (fd, ttp) |
| 237 | int fd; |
| 238 | TTYSTRUCT *ttp; |
| 239 | { |
| 240 | if (tt_seteightbit (ttp) < 0) |
| 241 | return -1; |
| 242 | return (ttsetattr (fd, ttp)); |
| 243 | } |
| 244 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 245 | /* Set the terminal into eight-bit mode */ |
| 246 | int |
| 247 | tteightbit () |
| 248 | { |
| 249 | TTYSTRUCT tt; |
| 250 | |
| 251 | if (ttsaved == 0) |
| 252 | return -1; |
| 253 | tt = ttin; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 254 | return (ttfd_eightbit (0, &tt)); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /* |
| 258 | * Change attributes in ttp so that when it is installed using |
| 259 | * ttsetattr, the terminal will be in non-canonical input mode. |
| 260 | */ |
| 261 | int |
| 262 | tt_setnocanon (ttp) |
| 263 | TTYSTRUCT *ttp; |
| 264 | { |
| 265 | #if defined (TERMIOS_TTY_DRIVER) || defined (TERMIO_TTY_DRIVER) |
| 266 | ttp->c_lflag &= ~ICANON; |
| 267 | #endif |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 272 | /* Set the tty associated with FD and TTP into non-canonical mode */ |
| 273 | int |
| 274 | ttfd_nocanon (fd, ttp) |
| 275 | int fd; |
| 276 | TTYSTRUCT *ttp; |
| 277 | { |
| 278 | if (tt_setnocanon (ttp) < 0) |
| 279 | return -1; |
| 280 | return (ttsetattr (fd, ttp)); |
| 281 | } |
| 282 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 283 | /* Set the terminal into non-canonical mode */ |
| 284 | int |
| 285 | ttnocanon () |
| 286 | { |
| 287 | TTYSTRUCT tt; |
| 288 | |
| 289 | if (ttsaved == 0) |
| 290 | return -1; |
| 291 | tt = ttin; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 292 | return (ttfd_nocanon (0, &tt)); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /* |
| 296 | * Change attributes in ttp so that when it is installed using |
| 297 | * ttsetattr, the terminal will be in cbreak, no-echo mode. |
| 298 | */ |
| 299 | int |
| 300 | tt_setcbreak(ttp) |
| 301 | TTYSTRUCT *ttp; |
| 302 | { |
| 303 | if (tt_setonechar (ttp) < 0) |
| 304 | return -1; |
| 305 | return (tt_setnoecho (ttp)); |
| 306 | } |
| 307 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 308 | /* Set the tty associated with FD and TTP into cbreak (no-echo, |
| 309 | one-character-at-a-time) mode */ |
| 310 | int |
| 311 | ttfd_cbreak (fd, ttp) |
| 312 | int fd; |
| 313 | TTYSTRUCT *ttp; |
| 314 | { |
| 315 | if (tt_setcbreak (ttp) < 0) |
| 316 | return -1; |
| 317 | return (ttsetattr (fd, ttp)); |
| 318 | } |
| 319 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 320 | /* Set the terminal into cbreak (no-echo, one-character-at-a-time) mode */ |
| 321 | int |
| 322 | ttcbreak () |
| 323 | { |
| 324 | TTYSTRUCT tt; |
| 325 | |
| 326 | if (ttsaved == 0) |
| 327 | return -1; |
| 328 | tt = ttin; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 329 | return (ttfd_cbreak (0, &tt)); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 330 | } |