1 1.51 blymn /* $NetBSD: tty.c,v 1.51 2024/12/23 02:58:04 blymn Exp $ */ 2 1.7 mikel 3 1.1 mycroft /*- 4 1.6 cgd * Copyright (c) 1992, 1993, 1994 5 1.2 cgd * The Regents of the University of California. All rights reserved. 6 1.1 mycroft * 7 1.1 mycroft * Redistribution and use in source and binary forms, with or without 8 1.1 mycroft * modification, are permitted provided that the following conditions 9 1.1 mycroft * are met: 10 1.1 mycroft * 1. Redistributions of source code must retain the above copyright 11 1.1 mycroft * notice, this list of conditions and the following disclaimer. 12 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 mycroft * notice, this list of conditions and the following disclaimer in the 14 1.1 mycroft * documentation and/or other materials provided with the distribution. 15 1.35 agc * 3. Neither the name of the University nor the names of its contributors 16 1.1 mycroft * may be used to endorse or promote products derived from this software 17 1.1 mycroft * without specific prior written permission. 18 1.1 mycroft * 19 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 1.1 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 1.1 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 1.1 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 1.1 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 1.1 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 1.1 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 1.1 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 1.1 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 1.1 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 1.1 mycroft * SUCH DAMAGE. 30 1.1 mycroft */ 31 1.1 mycroft 32 1.7 mikel #include <sys/cdefs.h> 33 1.1 mycroft #ifndef lint 34 1.7 mikel #if 0 35 1.10 perry static char sccsid[] = "@(#)tty.c 8.6 (Berkeley) 1/10/95"; 36 1.7 mikel #else 37 1.51 blymn __RCSID("$NetBSD: tty.c,v 1.51 2024/12/23 02:58:04 blymn Exp $"); 38 1.7 mikel #endif 39 1.11 mrg #endif /* not lint */ 40 1.11 mrg 41 1.47 roy #include <sys/fcntl.h> 42 1.47 roy #include <sys/ioctl.h> 43 1.47 roy #include <sys/param.h> 44 1.11 mrg #include <sys/types.h> 45 1.1 mycroft 46 1.6 cgd #include <stdlib.h> 47 1.1 mycroft #include <termios.h> 48 1.1 mycroft #include <unistd.h> 49 1.1 mycroft 50 1.6 cgd #include "curses.h" 51 1.14 jdc #include "curses_private.h" 52 1.6 cgd 53 1.2 cgd /* 54 1.2 cgd * In general, curses should leave tty hardware settings alone (speed, parity, 55 1.2 cgd * word size). This is most easily done in BSD by using TCSASOFT on all 56 1.2 cgd * tcsetattr calls. On other systems, it would be better to get and restore 57 1.2 cgd * those attributes at each change, or at least when stopped and restarted. 58 1.2 cgd * See also the comments in getterm(). 59 1.2 cgd */ 60 1.43 christos #ifndef TCSASOFT 61 1.43 christos #define TCSASOFT 0 62 1.2 cgd #endif 63 1.2 cgd 64 1.43 christos int __tcaction = TCSASOFT != 0; /* Ignore hardware settings */ 65 1.43 christos 66 1.2 cgd #ifndef OXTABS 67 1.2 cgd #ifdef XTABS /* SMI uses XTABS. */ 68 1.2 cgd #define OXTABS XTABS 69 1.2 cgd #else 70 1.2 cgd #define OXTABS 0 71 1.2 cgd #endif 72 1.2 cgd #endif 73 1.26 christos 74 1.26 christos /* 75 1.26 christos * baudrate -- 76 1.26 christos * Return the current baudrate 77 1.26 christos */ 78 1.26 christos int 79 1.26 christos baudrate(void) 80 1.26 christos { 81 1.46 roy 82 1.29 itojun if (_cursesi_screen->notty == TRUE) 83 1.29 itojun return 0; 84 1.29 itojun 85 1.29 itojun return cfgetospeed(&_cursesi_screen->baset); 86 1.26 christos } 87 1.2 cgd 88 1.1 mycroft /* 89 1.1 mycroft * gettmode -- 90 1.1 mycroft * Do terminal type initialization. 91 1.1 mycroft */ 92 1.1 mycroft int 93 1.15 blymn gettmode(void) 94 1.1 mycroft { 95 1.46 roy 96 1.25 blymn if (_cursesi_gettmode(_cursesi_screen) == ERR) 97 1.25 blymn return ERR; 98 1.11 mrg 99 1.25 blymn __GT = _cursesi_screen->GT; 100 1.25 blymn __NONL = _cursesi_screen->NONL; 101 1.25 blymn return OK; 102 1.25 blymn } 103 1.25 blymn 104 1.25 blymn /* 105 1.25 blymn * _cursesi_gettmode -- 106 1.25 blymn * Do the terminal type initialisation for the tty attached to the 107 1.25 blymn * given screen. 108 1.25 blymn */ 109 1.25 blymn int 110 1.25 blymn _cursesi_gettmode(SCREEN *screen) 111 1.25 blymn { 112 1.25 blymn screen->useraw = 0; 113 1.25 blymn 114 1.29 itojun if (tcgetattr(fileno(screen->infd), &screen->orig_termios)) { 115 1.29 itojun /* if the input fd is not a tty try the output */ 116 1.50 blymn if (tcgetattr(fileno(screen->outfd), &screen->orig_termios)) { 117 1.29 itojun /* not a tty ... we will disable tty related stuff */ 118 1.29 itojun screen->notty = TRUE; 119 1.29 itojun __GT = 0; 120 1.29 itojun __NONL = 0; 121 1.43 christos return OK; 122 1.29 itojun } 123 1.29 itojun } 124 1.2 cgd 125 1.25 blymn screen->baset = screen->orig_termios; 126 1.25 blymn screen->baset.c_oflag &= ~OXTABS; 127 1.28 blymn 128 1.25 blymn screen->GT = 0; /* historical. was used before we wired OXTABS off */ 129 1.25 blymn screen->NONL = (screen->baset.c_oflag & ONLCR) == 0; 130 1.1 mycroft 131 1.2 cgd /* 132 1.2 cgd * XXX 133 1.2 cgd * System V and SMI systems overload VMIN and VTIME, such that 134 1.2 cgd * VMIN is the same as the VEOF element, and VTIME is the same 135 1.2 cgd * as the VEOL element. This means that, if VEOF was ^D, the 136 1.2 cgd * default VMIN is 4. Majorly stupid. 137 1.2 cgd */ 138 1.25 blymn screen->cbreakt = screen->baset; 139 1.25 blymn screen->cbreakt.c_lflag &= ~(ECHO | ECHONL | ICANON); 140 1.25 blymn screen->cbreakt.c_cc[VMIN] = 1; 141 1.25 blymn screen->cbreakt.c_cc[VTIME] = 0; 142 1.25 blymn 143 1.25 blymn screen->rawt = screen->cbreakt; 144 1.25 blymn screen->rawt.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | INLCR | IGNCR | 145 1.25 blymn ICRNL | IXON); 146 1.25 blymn screen->rawt.c_oflag &= ~OPOST; 147 1.25 blymn screen->rawt.c_lflag &= ~(ISIG | IEXTEN); 148 1.2 cgd 149 1.43 christos #if TCSASOFT == 0 150 1.2 cgd /* 151 1.2 cgd * In general, curses should leave hardware-related settings alone. 152 1.2 cgd * This includes parity and word size. Older versions set the tty 153 1.2 cgd * to 8 bits, no parity in raw(), but this is considered to be an 154 1.2 cgd * artifact of the old tty interface. If it's desired to change 155 1.2 cgd * parity and word size, the TCSASOFT bit has to be removed from the 156 1.2 cgd * calls that switch to/from "raw" mode. 157 1.2 cgd */ 158 1.43 christos screen->rawt.c_iflag &= ~ISTRIP; 159 1.43 christos screen->rawt.c_cflag &= ~(CSIZE | PARENB); 160 1.43 christos screen->rawt.c_cflag |= CS8; 161 1.43 christos #endif 162 1.1 mycroft 163 1.25 blymn screen->curt = &screen->baset; 164 1.43 christos return tcsetattr(fileno(screen->infd), TCSASOFT | TCSADRAIN, 165 1.43 christos screen->curt) ? ERR : OK; 166 1.1 mycroft } 167 1.1 mycroft 168 1.36 wiz /* 169 1.36 wiz * raw -- 170 1.36 wiz * Put the terminal into raw mode 171 1.36 wiz */ 172 1.1 mycroft int 173 1.15 blymn raw(void) 174 1.1 mycroft { 175 1.39 jdc __CTRACE(__CTRACE_MISC, "raw()\n"); 176 1.9 phil /* Check if we need to restart ... */ 177 1.25 blymn if (_cursesi_screen->endwin) 178 1.23 jdc __restartwin(); 179 1.11 mrg 180 1.25 blymn _cursesi_screen->useraw = __pfast = __rawmode = 1; 181 1.25 blymn _cursesi_screen->curt = &_cursesi_screen->rawt; 182 1.29 itojun if (_cursesi_screen->notty == TRUE) 183 1.29 itojun return OK; 184 1.43 christos return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN, 185 1.43 christos _cursesi_screen->curt) ? ERR : OK; 186 1.1 mycroft } 187 1.1 mycroft 188 1.36 wiz /* 189 1.36 wiz * noraw -- 190 1.36 wiz * Put the terminal into cooked mode 191 1.36 wiz */ 192 1.1 mycroft int 193 1.15 blymn noraw(void) 194 1.1 mycroft { 195 1.39 jdc __CTRACE(__CTRACE_MISC, "noraw()\n"); 196 1.9 phil /* Check if we need to restart ... */ 197 1.25 blymn if (_cursesi_screen->endwin) 198 1.23 jdc __restartwin(); 199 1.11 mrg 200 1.25 blymn _cursesi_screen->useraw = __pfast = __rawmode = 0; 201 1.29 itojun if (_cursesi_screen->notty == TRUE) 202 1.29 itojun return OK; 203 1.25 blymn _cursesi_screen->curt = &_cursesi_screen->baset; 204 1.43 christos return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN, 205 1.43 christos _cursesi_screen->curt) ? ERR : OK; 206 1.1 mycroft } 207 1.1 mycroft 208 1.36 wiz /* 209 1.36 wiz * cbreak -- 210 1.36 wiz * Enable cbreak mode 211 1.36 wiz */ 212 1.1 mycroft int 213 1.15 blymn cbreak(void) 214 1.1 mycroft { 215 1.39 jdc __CTRACE(__CTRACE_MISC, "cbreak()\n"); 216 1.9 phil /* Check if we need to restart ... */ 217 1.25 blymn if (_cursesi_screen->endwin) 218 1.23 jdc __restartwin(); 219 1.11 mrg 220 1.1 mycroft __rawmode = 1; 221 1.29 itojun if (_cursesi_screen->notty == TRUE) 222 1.29 itojun return OK; 223 1.25 blymn _cursesi_screen->curt = _cursesi_screen->useraw ? 224 1.25 blymn &_cursesi_screen->rawt : &_cursesi_screen->cbreakt; 225 1.43 christos return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN, 226 1.43 christos _cursesi_screen->curt) ? ERR : OK; 227 1.1 mycroft } 228 1.1 mycroft 229 1.36 wiz /* 230 1.36 wiz * nocbreak -- 231 1.36 wiz * Disable cbreak mode 232 1.36 wiz */ 233 1.1 mycroft int 234 1.15 blymn nocbreak(void) 235 1.1 mycroft { 236 1.39 jdc __CTRACE(__CTRACE_MISC, "nocbreak()\n"); 237 1.9 phil /* Check if we need to restart ... */ 238 1.25 blymn if (_cursesi_screen->endwin) 239 1.23 jdc __restartwin(); 240 1.1 mycroft 241 1.1 mycroft __rawmode = 0; 242 1.29 itojun if (_cursesi_screen->notty == TRUE) 243 1.29 itojun return OK; 244 1.30 blymn /* if we were in halfdelay mode then nuke the timeout */ 245 1.48 blymn if ((stdscr->flags & __HALFDELAY) && 246 1.30 blymn (__notimeout() == ERR)) 247 1.30 blymn return ERR; 248 1.30 blymn 249 1.48 blymn stdscr->flags &= ~__HALFDELAY; 250 1.25 blymn _cursesi_screen->curt = _cursesi_screen->useraw ? 251 1.25 blymn &_cursesi_screen->rawt : &_cursesi_screen->baset; 252 1.43 christos return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN, 253 1.43 christos _cursesi_screen->curt) ? ERR : OK; 254 1.1 mycroft } 255 1.11 mrg 256 1.30 blymn /* 257 1.30 blymn * halfdelay -- 258 1.30 blymn * Put the terminal into cbreak mode with the specified timeout. 259 1.30 blymn * 260 1.30 blymn */ 261 1.30 blymn int 262 1.31 atatat halfdelay(int duration) 263 1.30 blymn { 264 1.31 atatat if ((duration < 1) || (duration > 255)) 265 1.30 blymn return ERR; 266 1.30 blymn 267 1.30 blymn if (cbreak() == ERR) 268 1.30 blymn return ERR; 269 1.30 blymn 270 1.48 blymn if (duration > 255) 271 1.48 blymn stdscr->delay = 255; 272 1.48 blymn else 273 1.48 blymn stdscr->delay = duration; 274 1.30 blymn 275 1.48 blymn stdscr->flags |= __HALFDELAY; 276 1.30 blymn return OK; 277 1.30 blymn } 278 1.46 roy 279 1.11 mrg int 280 1.15 blymn __delay(void) 281 1.11 mrg { 282 1.39 jdc __CTRACE(__CTRACE_MISC, "__delay()\n"); 283 1.11 mrg /* Check if we need to restart ... */ 284 1.25 blymn if (_cursesi_screen->endwin) 285 1.23 jdc __restartwin(); 286 1.11 mrg 287 1.29 itojun if (_cursesi_screen->notty == TRUE) 288 1.29 itojun return OK; 289 1.25 blymn _cursesi_screen->rawt.c_cc[VMIN] = 1; 290 1.25 blymn _cursesi_screen->rawt.c_cc[VTIME] = 0; 291 1.25 blymn _cursesi_screen->cbreakt.c_cc[VMIN] = 1; 292 1.25 blymn _cursesi_screen->cbreakt.c_cc[VTIME] = 0; 293 1.25 blymn _cursesi_screen->baset.c_cc[VMIN] = 1; 294 1.25 blymn _cursesi_screen->baset.c_cc[VTIME] = 0; 295 1.11 mrg 296 1.43 christos if (tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSANOW, 297 1.43 christos _cursesi_screen->curt)) { 298 1.41 dsl __restore_termios(); 299 1.41 dsl return ERR; 300 1.41 dsl } 301 1.41 dsl 302 1.41 dsl return OK; 303 1.11 mrg } 304 1.11 mrg 305 1.11 mrg int 306 1.15 blymn __nodelay(void) 307 1.11 mrg { 308 1.39 jdc __CTRACE(__CTRACE_MISC, "__nodelay()\n"); 309 1.11 mrg /* Check if we need to restart ... */ 310 1.25 blymn if (_cursesi_screen->endwin) 311 1.23 jdc __restartwin(); 312 1.11 mrg 313 1.29 itojun if (_cursesi_screen->notty == TRUE) 314 1.29 itojun return OK; 315 1.25 blymn _cursesi_screen->rawt.c_cc[VMIN] = 0; 316 1.25 blymn _cursesi_screen->rawt.c_cc[VTIME] = 0; 317 1.25 blymn _cursesi_screen->cbreakt.c_cc[VMIN] = 0; 318 1.25 blymn _cursesi_screen->cbreakt.c_cc[VTIME] = 0; 319 1.25 blymn _cursesi_screen->baset.c_cc[VMIN] = 0; 320 1.25 blymn _cursesi_screen->baset.c_cc[VTIME] = 0; 321 1.11 mrg 322 1.43 christos if (tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSANOW, 323 1.43 christos _cursesi_screen->curt)) { 324 1.41 dsl __restore_termios(); 325 1.41 dsl return ERR; 326 1.41 dsl } 327 1.41 dsl 328 1.41 dsl return OK; 329 1.11 mrg } 330 1.11 mrg 331 1.11 mrg void 332 1.15 blymn __save_termios(void) 333 1.11 mrg { 334 1.11 mrg /* Check if we need to restart ... */ 335 1.25 blymn if (_cursesi_screen->endwin) 336 1.23 jdc __restartwin(); 337 1.11 mrg 338 1.29 itojun if (_cursesi_screen->notty == TRUE) 339 1.29 itojun return; 340 1.25 blymn _cursesi_screen->ovmin = _cursesi_screen->cbreakt.c_cc[VMIN]; 341 1.25 blymn _cursesi_screen->ovtime = _cursesi_screen->cbreakt.c_cc[VTIME]; 342 1.11 mrg } 343 1.11 mrg 344 1.11 mrg void 345 1.15 blymn __restore_termios(void) 346 1.11 mrg { 347 1.11 mrg /* Check if we need to restart ... */ 348 1.25 blymn if (_cursesi_screen->endwin) 349 1.23 jdc __restartwin(); 350 1.11 mrg 351 1.29 itojun if (_cursesi_screen->notty == TRUE) 352 1.29 itojun return; 353 1.25 blymn _cursesi_screen->rawt.c_cc[VMIN] = _cursesi_screen->ovmin; 354 1.25 blymn _cursesi_screen->rawt.c_cc[VTIME] = _cursesi_screen->ovtime; 355 1.25 blymn _cursesi_screen->cbreakt.c_cc[VMIN] = _cursesi_screen->ovmin; 356 1.25 blymn _cursesi_screen->cbreakt.c_cc[VTIME] = _cursesi_screen->ovtime; 357 1.25 blymn _cursesi_screen->baset.c_cc[VMIN] = _cursesi_screen->ovmin; 358 1.25 blymn _cursesi_screen->baset.c_cc[VTIME] = _cursesi_screen->ovtime; 359 1.11 mrg } 360 1.11 mrg 361 1.11 mrg int 362 1.15 blymn __timeout(int delay) 363 1.11 mrg { 364 1.39 jdc __CTRACE(__CTRACE_MISC, "__timeout()\n"); 365 1.11 mrg /* Check if we need to restart ... */ 366 1.25 blymn if (_cursesi_screen->endwin) 367 1.23 jdc __restartwin(); 368 1.11 mrg 369 1.29 itojun if (_cursesi_screen->notty == TRUE) 370 1.29 itojun return OK; 371 1.25 blymn _cursesi_screen->ovmin = _cursesi_screen->cbreakt.c_cc[VMIN]; 372 1.25 blymn _cursesi_screen->ovtime = _cursesi_screen->cbreakt.c_cc[VTIME]; 373 1.25 blymn _cursesi_screen->rawt.c_cc[VMIN] = 0; 374 1.25 blymn _cursesi_screen->rawt.c_cc[VTIME] = delay; 375 1.25 blymn _cursesi_screen->cbreakt.c_cc[VMIN] = 0; 376 1.25 blymn _cursesi_screen->cbreakt.c_cc[VTIME] = delay; 377 1.25 blymn _cursesi_screen->baset.c_cc[VMIN] = 0; 378 1.25 blymn _cursesi_screen->baset.c_cc[VTIME] = delay; 379 1.25 blymn 380 1.43 christos if (tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSANOW, 381 1.43 christos _cursesi_screen->curt)) { 382 1.41 dsl __restore_termios(); 383 1.41 dsl return ERR; 384 1.41 dsl } 385 1.41 dsl 386 1.41 dsl return OK; 387 1.11 mrg } 388 1.11 mrg 389 1.11 mrg int 390 1.15 blymn __notimeout(void) 391 1.11 mrg { 392 1.39 jdc __CTRACE(__CTRACE_MISC, "__notimeout()\n"); 393 1.11 mrg /* Check if we need to restart ... */ 394 1.25 blymn if (_cursesi_screen->endwin) 395 1.23 jdc __restartwin(); 396 1.11 mrg 397 1.29 itojun if (_cursesi_screen->notty == TRUE) 398 1.29 itojun return OK; 399 1.25 blymn _cursesi_screen->rawt.c_cc[VMIN] = 1; 400 1.25 blymn _cursesi_screen->rawt.c_cc[VTIME] = 0; 401 1.25 blymn _cursesi_screen->cbreakt.c_cc[VMIN] = 1; 402 1.25 blymn _cursesi_screen->cbreakt.c_cc[VTIME] = 0; 403 1.25 blymn _cursesi_screen->baset.c_cc[VMIN] = 1; 404 1.25 blymn _cursesi_screen->baset.c_cc[VTIME] = 0; 405 1.25 blymn 406 1.43 christos return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSANOW, 407 1.43 christos _cursesi_screen->curt) ? ERR : OK; 408 1.11 mrg } 409 1.11 mrg 410 1.1 mycroft int 411 1.15 blymn echo(void) 412 1.1 mycroft { 413 1.39 jdc __CTRACE(__CTRACE_MISC, "echo()\n"); 414 1.9 phil /* Check if we need to restart ... */ 415 1.25 blymn if (_cursesi_screen->endwin) 416 1.23 jdc __restartwin(); 417 1.9 phil 418 1.1 mycroft __echoit = 1; 419 1.43 christos return OK; 420 1.1 mycroft } 421 1.1 mycroft 422 1.1 mycroft int 423 1.15 blymn noecho(void) 424 1.1 mycroft { 425 1.39 jdc __CTRACE(__CTRACE_MISC, "noecho()\n"); 426 1.9 phil /* Check if we need to restart ... */ 427 1.25 blymn if (_cursesi_screen->endwin) 428 1.23 jdc __restartwin(); 429 1.9 phil 430 1.1 mycroft __echoit = 0; 431 1.43 christos return OK; 432 1.1 mycroft } 433 1.1 mycroft 434 1.1 mycroft int 435 1.15 blymn nl(void) 436 1.1 mycroft { 437 1.39 jdc __CTRACE(__CTRACE_MISC, "nl()\n"); 438 1.9 phil /* Check if we need to restart ... */ 439 1.25 blymn if (_cursesi_screen->endwin) 440 1.23 jdc __restartwin(); 441 1.9 phil 442 1.29 itojun if (_cursesi_screen->notty == TRUE) 443 1.29 itojun return OK; 444 1.25 blymn _cursesi_screen->rawt.c_iflag |= ICRNL; 445 1.25 blymn _cursesi_screen->rawt.c_oflag |= ONLCR; 446 1.25 blymn _cursesi_screen->cbreakt.c_iflag |= ICRNL; 447 1.25 blymn _cursesi_screen->cbreakt.c_oflag |= ONLCR; 448 1.25 blymn _cursesi_screen->baset.c_iflag |= ICRNL; 449 1.25 blymn _cursesi_screen->baset.c_oflag |= ONLCR; 450 1.25 blymn 451 1.32 jdc _cursesi_screen->nl = 1; 452 1.25 blymn _cursesi_screen->pfast = _cursesi_screen->rawmode; 453 1.43 christos return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN, 454 1.43 christos _cursesi_screen->curt) ? ERR : OK; 455 1.1 mycroft } 456 1.1 mycroft 457 1.1 mycroft int 458 1.15 blymn nonl(void) 459 1.1 mycroft { 460 1.39 jdc __CTRACE(__CTRACE_MISC, "nonl()\n"); 461 1.9 phil /* Check if we need to restart ... */ 462 1.25 blymn if (_cursesi_screen->endwin) 463 1.23 jdc __restartwin(); 464 1.9 phil 465 1.29 itojun if (_cursesi_screen->notty == TRUE) 466 1.29 itojun return OK; 467 1.25 blymn _cursesi_screen->rawt.c_iflag &= ~ICRNL; 468 1.25 blymn _cursesi_screen->rawt.c_oflag &= ~ONLCR; 469 1.25 blymn _cursesi_screen->cbreakt.c_iflag &= ~ICRNL; 470 1.25 blymn _cursesi_screen->cbreakt.c_oflag &= ~ONLCR; 471 1.25 blymn _cursesi_screen->baset.c_iflag &= ~ICRNL; 472 1.25 blymn _cursesi_screen->baset.c_oflag &= ~ONLCR; 473 1.1 mycroft 474 1.32 jdc _cursesi_screen->nl = 0; 475 1.1 mycroft __pfast = 1; 476 1.43 christos return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN, 477 1.43 christos _cursesi_screen->curt) ? ERR : OK; 478 1.2 cgd } 479 1.33 jdc 480 1.33 jdc #ifndef _CURSES_USE_MACROS 481 1.33 jdc void 482 1.33 jdc noqiflush(void) 483 1.33 jdc { 484 1.46 roy 485 1.46 roy (void)intrflush(stdscr, FALSE); 486 1.33 jdc } 487 1.33 jdc 488 1.33 jdc void 489 1.33 jdc qiflush(void) 490 1.33 jdc { 491 1.46 roy 492 1.46 roy (void)intrflush(stdscr, TRUE); 493 1.33 jdc } 494 1.33 jdc #endif /* _CURSES_USE_MACROS */ 495 1.2 cgd 496 1.44 christos /*ARGSUSED*/ 497 1.14 jdc int 498 1.44 christos intrflush(WINDOW *win, bool bf) 499 1.14 jdc { 500 1.51 blymn if (__predict_false(win == NULL)) 501 1.51 blymn return ERR; 502 1.51 blymn 503 1.14 jdc /* Check if we need to restart ... */ 504 1.25 blymn if (_cursesi_screen->endwin) 505 1.23 jdc __restartwin(); 506 1.14 jdc 507 1.29 itojun if (_cursesi_screen->notty == TRUE) 508 1.29 itojun return OK; 509 1.14 jdc if (bf) { 510 1.25 blymn _cursesi_screen->rawt.c_lflag &= ~NOFLSH; 511 1.25 blymn _cursesi_screen->cbreakt.c_lflag &= ~NOFLSH; 512 1.25 blymn _cursesi_screen->baset.c_lflag &= ~NOFLSH; 513 1.14 jdc } else { 514 1.25 blymn _cursesi_screen->rawt.c_lflag |= NOFLSH; 515 1.25 blymn _cursesi_screen->cbreakt.c_lflag |= NOFLSH; 516 1.25 blymn _cursesi_screen->baset.c_lflag |= NOFLSH; 517 1.14 jdc } 518 1.14 jdc 519 1.14 jdc __pfast = 1; 520 1.43 christos return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN, 521 1.43 christos _cursesi_screen->curt) ? ERR : OK; 522 1.14 jdc } 523 1.14 jdc 524 1.2 cgd void 525 1.25 blymn __startwin(SCREEN *screen) 526 1.2 cgd { 527 1.6 cgd 528 1.46 roy (void)fflush(screen->infd); 529 1.6 cgd 530 1.47 roy #ifdef BSD 531 1.6 cgd /* 532 1.6 cgd * Some C libraries default to a 1K buffer when talking to a tty. 533 1.6 cgd * With a larger screen, especially across a network, we'd like 534 1.6 cgd * to get it to all flush in a single write. Make it twice as big 535 1.6 cgd * as just the characters (so that we have room for cursor motions 536 1.11 mrg * and attribute information) but no more than 8K. 537 1.47 roy * 538 1.47 roy * However, setvbuf may only be used after opening a stream and 539 1.47 roy * before any operations have been performed on it. 540 1.47 roy * This means we cannot work portably if an application wants 541 1.47 roy * to stop curses and start curses after a resize. 542 1.47 roy * Curses resizing is not standard, and thus not strictly portable 543 1.47 roy * even though all curses today support it. 544 1.47 roy * The BSD systems do not suffer from this limitation on setvbuf. 545 1.6 cgd */ 546 1.25 blymn if (screen->stdbuf == NULL) { 547 1.25 blymn screen->len = LINES * COLS * 2; 548 1.25 blymn if (screen->len > 8192) 549 1.25 blymn screen->len = 8192; 550 1.25 blymn if ((screen->stdbuf = malloc(screen->len)) == NULL) 551 1.25 blymn screen->len = 0; 552 1.6 cgd } 553 1.46 roy (void)setvbuf(screen->outfd, screen->stdbuf, _IOFBF, screen->len); 554 1.47 roy #endif 555 1.2 cgd 556 1.42 roy ti_puts(screen->term, t_enter_ca_mode(screen->term), 0, 557 1.42 roy __cputchar_args, (void *) screen->outfd); 558 1.42 roy ti_puts(screen->term, t_cursor_normal(screen->term), 0, 559 1.42 roy __cputchar_args, (void *) screen->outfd); 560 1.25 blymn if (screen->curscr->flags & __KEYPAD) 561 1.42 roy ti_puts(screen->term, t_keypad_xmit(screen->term), 0, 562 1.42 roy __cputchar_args, (void *) screen->outfd); 563 1.25 blymn screen->endwin = 0; 564 1.1 mycroft } 565 1.1 mycroft 566 1.1 mycroft int 567 1.15 blymn endwin(void) 568 1.1 mycroft { 569 1.39 jdc __CTRACE(__CTRACE_MISC, "endwin\n"); 570 1.8 phil return __stopwin(); 571 1.11 mrg } 572 1.11 mrg 573 1.13 blymn bool 574 1.15 blymn isendwin(void) 575 1.11 mrg { 576 1.46 roy 577 1.43 christos return _cursesi_screen->endwin ? TRUE : FALSE; 578 1.11 mrg } 579 1.11 mrg 580 1.11 mrg int 581 1.15 blymn flushinp(void) 582 1.11 mrg { 583 1.46 roy 584 1.46 roy (void)fpurge(_cursesi_screen->infd); 585 1.43 christos return OK; 586 1.14 jdc } 587 1.14 jdc 588 1.1 mycroft /* 589 1.1 mycroft * The following routines, savetty and resetty are completely useless and 590 1.1 mycroft * are left in only as stubs. If people actually use them they will almost 591 1.1 mycroft * certainly screw up the state of the world. 592 1.1 mycroft */ 593 1.25 blymn /*static struct termios savedtty;*/ 594 1.1 mycroft int 595 1.15 blymn savetty(void) 596 1.1 mycroft { 597 1.46 roy 598 1.29 itojun if (_cursesi_screen->notty == TRUE) 599 1.29 itojun return OK; 600 1.43 christos return tcgetattr(fileno(_cursesi_screen->infd), 601 1.46 roy &_cursesi_screen->savedtty) ? ERR : OK; 602 1.1 mycroft } 603 1.1 mycroft 604 1.1 mycroft int 605 1.15 blymn resetty(void) 606 1.1 mycroft { 607 1.46 roy 608 1.29 itojun if (_cursesi_screen->notty == TRUE) 609 1.29 itojun return OK; 610 1.43 christos return tcsetattr(fileno(_cursesi_screen->infd), TCSASOFT | TCSADRAIN, 611 1.46 roy &_cursesi_screen->savedtty) ? ERR : OK; 612 1.19 blymn } 613 1.19 blymn 614 1.19 blymn /* 615 1.19 blymn * erasechar -- 616 1.19 blymn * Return the character of the erase key. 617 1.19 blymn */ 618 1.19 blymn char 619 1.19 blymn erasechar(void) 620 1.19 blymn { 621 1.46 roy 622 1.29 itojun if (_cursesi_screen->notty == TRUE) 623 1.29 itojun return 0; 624 1.25 blymn return _cursesi_screen->baset.c_cc[VERASE]; 625 1.19 blymn } 626 1.19 blymn 627 1.19 blymn /* 628 1.19 blymn * killchar -- 629 1.19 blymn * Return the character of the kill key. 630 1.19 blymn */ 631 1.19 blymn char 632 1.19 blymn killchar(void) 633 1.19 blymn { 634 1.46 roy 635 1.29 itojun if (_cursesi_screen->notty == TRUE) 636 1.29 itojun return 0; 637 1.25 blymn return _cursesi_screen->baset.c_cc[VKILL]; 638 1.1 mycroft } 639 1.40 blymn 640 1.40 blymn /* 641 1.40 blymn * erasewchar -- 642 1.40 blymn * Return the wide character of the erase key. 643 1.40 blymn */ 644 1.40 blymn int 645 1.46 roy erasewchar(wchar_t *ch) 646 1.40 blymn { 647 1.46 roy 648 1.40 blymn #ifndef HAVE_WCHAR 649 1.40 blymn return ERR; 650 1.40 blymn #else 651 1.40 blymn if (_cursesi_screen->notty == TRUE) 652 1.40 blymn return ERR; 653 1.40 blymn *ch = _cursesi_screen->baset.c_cc[VERASE]; 654 1.40 blymn return OK; 655 1.40 blymn #endif /* HAVE_WCHAR */ 656 1.40 blymn } 657 1.40 blymn 658 1.40 blymn /* 659 1.40 blymn * killwchar -- 660 1.40 blymn * Return the wide character of the kill key. 661 1.40 blymn */ 662 1.40 blymn int 663 1.40 blymn killwchar( wchar_t *ch ) 664 1.40 blymn { 665 1.46 roy 666 1.40 blymn #ifndef HAVE_WCHAR 667 1.40 blymn return ERR; 668 1.40 blymn #else 669 1.40 blymn if (_cursesi_screen->notty == TRUE) 670 1.40 blymn return 0; 671 1.40 blymn *ch = _cursesi_screen->baset.c_cc[VKILL]; 672 1.40 blymn return OK; 673 1.40 blymn #endif /* HAVE_WCHAR */ 674 1.40 blymn } 675 1.45 roy 676 1.45 roy int 677 1.45 roy typeahead(int filedes) 678 1.45 roy { 679 1.45 roy 680 1.45 roy _cursesi_screen->checkfd = filedes; 681 1.45 roy return OK; 682 1.45 roy } 683