1 /* $NetBSD: cl_screen.c,v 1.3 2013/11/25 22:43:46 christos Exp $ */ 2 /*- 3 * Copyright (c) 1993, 1994 4 * The Regents of the University of California. All rights reserved. 5 * Copyright (c) 1993, 1994, 1995, 1996 6 * Keith Bostic. All rights reserved. 7 * 8 * See the LICENSE file for redistribution information. 9 */ 10 11 #include "config.h" 12 13 #ifndef lint 14 static const char sccsid[] = "Id: cl_screen.c,v 10.56 2002/05/03 19:59:44 skimo Exp (Berkeley) Date: 2002/05/03 19:59:44 "; 15 #endif /* not lint */ 16 17 #include <sys/types.h> 18 #include <sys/queue.h> 19 20 #include <bitstring.h> 21 #include <errno.h> 22 #include <signal.h> 23 #include <stdio.h> 24 #include <stdlib.h> 25 #include <string.h> 26 #include <termios.h> 27 #include <unistd.h> 28 29 #include "../common/common.h" 30 #include "cl.h" 31 32 static int cl_ex_end __P((GS *)); 33 static int cl_ex_init __P((SCR *)); 34 static void cl_freecap __P((CL_PRIVATE *)); 35 static int cl_vi_end __P((GS *)); 36 static int cl_vi_init __P((SCR *)); 37 static int cl_putenv __P((SCR *, const char *, const char *, u_long)); 38 39 /* 40 * cl_screen -- 41 * Switch screen types. 42 * 43 * PUBLIC: int cl_screen __P((SCR *, u_int32_t)); 44 */ 45 int 46 cl_screen(SCR *sp, u_int32_t flags) 47 { 48 CL_PRIVATE *clp; 49 WINDOW *win; 50 GS *gp; 51 52 gp = sp->gp; 53 clp = CLP(sp); 54 win = CLSP(sp) ? CLSP(sp) : stdscr; 55 56 /* See if the current information is incorrect. */ 57 if (F_ISSET(gp, G_SRESTART)) { 58 if (CLSP(sp)) { 59 delwin(CLSP(sp)); 60 sp->cl_private = NULL; 61 } 62 if (cl_quit(gp)) 63 return (1); 64 F_CLR(gp, G_SRESTART); 65 } 66 67 /* See if we're already in the right mode. */ 68 if ((LF_ISSET(SC_EX) && F_ISSET(sp, SC_SCR_EX)) || 69 (LF_ISSET(SC_VI) && F_ISSET(sp, SC_SCR_VI))) 70 return (0); 71 72 /* 73 * Fake leaving ex mode. 74 * 75 * We don't actually exit ex or vi mode unless forced (e.g. by a window 76 * size change). This is because many curses implementations can't be 77 * called twice in a single program. Plus, it's faster. If the editor 78 * "leaves" vi to enter ex, when it exits ex we'll just fall back into 79 * vi. 80 */ 81 if (F_ISSET(sp, SC_SCR_EX)) 82 F_CLR(sp, SC_SCR_EX); 83 84 /* 85 * Fake leaving vi mode. 86 * 87 * Clear out the rest of the screen if we're in the middle of a split 88 * screen. Move to the last line in the current screen -- this makes 89 * terminal scrolling happen naturally. Note: *don't* move past the 90 * end of the screen, as there are ex commands (e.g., :read ! cat file) 91 * that don't want to. Don't clear the info line, its contents may be 92 * valid, e.g. :file|append. 93 */ 94 if (F_ISSET(sp, SC_SCR_VI)) { 95 F_CLR(sp, SC_SCR_VI); 96 97 if (TAILQ_NEXT(sp, q) != NULL) { 98 (void)wmove(win, RLNO(sp, sp->rows), 0); 99 wclrtobot(win); 100 } 101 (void)wmove(win, RLNO(sp, sp->rows) - 1, 0); 102 wrefresh(win); 103 } 104 105 /* Enter the requested mode. */ 106 if (LF_ISSET(SC_EX)) { 107 if (cl_ex_init(sp)) 108 return (1); 109 F_SET(clp, CL_IN_EX | CL_SCR_EX_INIT); 110 111 /* 112 * If doing an ex screen for ex mode, move to the last line 113 * on the screen. 114 */ 115 if (F_ISSET(sp, SC_EX) && clp->cup != NULL) 116 tputs(tgoto(clp->cup, 117 0, O_VAL(sp, O_LINES) - 1), 1, cl_putchar); 118 } else { 119 if (cl_vi_init(sp)) 120 return (1); 121 F_CLR(clp, CL_IN_EX); 122 F_SET(clp, CL_SCR_VI_INIT); 123 } 124 return (0); 125 } 126 127 /* 128 * cl_quit -- 129 * Shutdown the screens. 130 * 131 * PUBLIC: int cl_quit __P((GS *)); 132 */ 133 int 134 cl_quit(GS *gp) 135 { 136 CL_PRIVATE *clp; 137 int rval; 138 139 rval = 0; 140 clp = GCLP(gp); 141 142 /* 143 * If we weren't really running, ignore it. This happens if the 144 * screen changes size before we've called curses. 145 */ 146 if (!F_ISSET(clp, CL_SCR_EX_INIT | CL_SCR_VI_INIT)) 147 return (0); 148 149 /* Clean up the terminal mappings. */ 150 if (cl_term_end(gp)) 151 rval = 1; 152 153 /* Really leave vi mode. */ 154 if (F_ISSET(clp, CL_STDIN_TTY) && 155 F_ISSET(clp, CL_SCR_VI_INIT) && cl_vi_end(gp)) 156 rval = 1; 157 158 /* Really leave ex mode. */ 159 if (F_ISSET(clp, CL_STDIN_TTY) && 160 F_ISSET(clp, CL_SCR_EX_INIT) && cl_ex_end(gp)) 161 rval = 1; 162 163 /* 164 * If we were running ex when we quit, or we're using an implementation 165 * of curses where endwin() doesn't get this right, restore the original 166 * terminal modes. 167 * 168 * XXX 169 * We always do this because it's too hard to figure out what curses 170 * implementations get it wrong. It may discard type-ahead characters 171 * from the tty queue. 172 */ 173 (void)tcsetattr(STDIN_FILENO, TCSADRAIN | TCSASOFT, &clp->orig); 174 175 F_CLR(clp, CL_SCR_EX_INIT | CL_SCR_VI_INIT); 176 return (rval); 177 } 178 179 /* 180 * cl_vi_init -- 181 * Initialize the curses vi screen. 182 */ 183 static int 184 cl_vi_init(SCR *sp) 185 { 186 CL_PRIVATE *clp; 187 char *o_cols, *o_lines, *o_term; 188 const char *ttype; 189 190 clp = CLP(sp); 191 192 /* If already initialized, just set the terminal modes. */ 193 if (F_ISSET(clp, CL_SCR_VI_INIT)) 194 goto fast; 195 196 /* Curses vi always reads from (and writes to) a terminal. */ 197 if (!F_ISSET(clp, CL_STDIN_TTY) || !isatty(STDOUT_FILENO)) { 198 msgq(sp, M_ERR, 199 "016|Vi's standard input and output must be a terminal"); 200 return (1); 201 } 202 203 /* We'll need a terminal type. */ 204 if (opts_empty(sp, O_TERM, 0)) 205 return (1); 206 ttype = O_STR(sp, O_TERM); 207 208 /* 209 * XXX 210 * Changing the row/column and terminal values is done by putting them 211 * into the environment, which is then read by curses. What this loses 212 * in ugliness, it makes up for in stupidity. We can't simply put the 213 * values into the environment ourselves, because in the presence of a 214 * kernel mechanism for returning the window size, entering values into 215 * the environment will screw up future screen resizing events, e.g. if 216 * the user enters a :shell command and then resizes their window. So, 217 * if they weren't already in the environment, we make sure to delete 218 * them immediately after setting them. 219 * 220 * XXX 221 * Putting the TERM variable into the environment is necessary, even 222 * though we're using newterm() here. We may be using initscr() as 223 * the underlying function. 224 */ 225 o_term = getenv("TERM"); 226 cl_putenv(sp, "TERM", ttype, 0); 227 o_lines = getenv("LINES"); 228 cl_putenv(sp, "LINES", NULL, (u_long)O_VAL(sp, O_LINES)); 229 o_cols = getenv("COLUMNS"); 230 cl_putenv(sp, "COLUMNS", NULL, (u_long)O_VAL(sp, O_COLUMNS)); 231 232 /* 233 * We don't care about the SCREEN reference returned by newterm, we 234 * never have more than one SCREEN at a time. 235 * 236 * XXX 237 * The SunOS initscr() can't be called twice. Don't even think about 238 * using it. It fails in subtle ways (e.g. select(2) on fileno(stdin) 239 * stops working). (The SVID notes that applications should only call 240 * initscr() once.) 241 * 242 * XXX 243 * The HP/UX newterm doesn't support the NULL first argument, so we 244 * have to specify the terminal type. 245 */ 246 errno = 0; 247 if (newterm(__UNCONST(ttype), stdout, stdin) == NULL) { 248 if (errno) 249 msgq(sp, M_SYSERR, "%s", ttype); 250 else 251 msgq(sp, M_ERR, "%s: unknown terminal type", ttype); 252 return (1); 253 } 254 255 if (o_term == NULL) 256 cl_unsetenv(sp, "TERM"); 257 if (o_lines == NULL) 258 cl_unsetenv(sp, "LINES"); 259 if (o_cols == NULL) 260 cl_unsetenv(sp, "COLUMNS"); 261 262 /* 263 * XXX 264 * Someone got let out alone without adult supervision -- the SunOS 265 * newterm resets the signal handlers. There's a race, but it's not 266 * worth closing. 267 */ 268 (void)sig_init(sp->gp, sp); 269 270 /* 271 * We use raw mode. What we want is 8-bit clean, however, signals 272 * and flow control should continue to work. Admittedly, it sounds 273 * like cbreak, but it isn't. Using cbreak() can get you additional 274 * things like IEXTEN, which turns on flags like DISCARD and LNEXT. 275 * 276 * !!! 277 * If raw isn't turning off echo and newlines, something's wrong. 278 * However, it shouldn't hurt. 279 */ 280 noecho(); /* No character echo. */ 281 nonl(); /* No CR/NL translation. */ 282 raw(); /* 8-bit clean. */ 283 idlok(stdscr, 1); /* Use hardware insert/delete line. */ 284 285 /* Put the cursor keys into application mode. */ 286 (void)keypad(stdscr, TRUE); 287 288 /* 289 * XXX 290 * The screen TI sequence just got sent. See the comment in 291 * cl_funcs.c:cl_attr(). 292 */ 293 clp->ti_te = TI_SENT; 294 295 /* 296 * XXX 297 * Historic implementations of curses handled SIGTSTP signals 298 * in one of three ways. They either: 299 * 300 * 1: Set their own handler, regardless. 301 * 2: Did not set a handler if a handler was already installed. 302 * 3: Set their own handler, but then called any previously set 303 * handler after completing their own cleanup. 304 * 305 * We don't try and figure out which behavior is in place, we force 306 * it to SIG_DFL after initializing the curses interface, which means 307 * that curses isn't going to take the signal. Since curses isn't 308 * reentrant (i.e., the whole curses SIGTSTP interface is a fantasy), 309 * we're doing The Right Thing. 310 */ 311 (void)signal(SIGTSTP, SIG_DFL); 312 313 /* 314 * If flow control was on, turn it back on. Turn signals on. ISIG 315 * turns on VINTR, VQUIT, VDSUSP and VSUSP. The main curses code 316 * already installed a handler for VINTR. We're going to disable the 317 * other three. 318 * 319 * XXX 320 * We want to use ^Y as a vi scrolling command. If the user has the 321 * DSUSP character set to ^Y (common practice) clean it up. As it's 322 * equally possible that the user has VDSUSP set to 'a', we disable 323 * it regardless. It doesn't make much sense to suspend vi at read, 324 * so I don't think anyone will care. Alternatively, we could look 325 * it up in the table of legal command characters and turn it off if 326 * it matches one. VDSUSP wasn't in POSIX 1003.1-1990, so we test for 327 * it. 328 * 329 * XXX 330 * We don't check to see if the user had signals enabled originally. 331 * If they didn't, it's unclear what we're supposed to do here, but 332 * it's also pretty unlikely. 333 */ 334 if (tcgetattr(STDIN_FILENO, &clp->vi_enter)) { 335 msgq(sp, M_SYSERR, "tcgetattr"); 336 goto err; 337 } 338 if (clp->orig.c_iflag & IXON) 339 clp->vi_enter.c_iflag |= IXON; 340 if (clp->orig.c_iflag & IXOFF) 341 clp->vi_enter.c_iflag |= IXOFF; 342 343 clp->vi_enter.c_lflag |= ISIG; 344 #ifdef VDSUSP 345 clp->vi_enter.c_cc[VDSUSP] = _POSIX_VDISABLE; 346 #endif 347 clp->vi_enter.c_cc[VQUIT] = _POSIX_VDISABLE; 348 clp->vi_enter.c_cc[VSUSP] = _POSIX_VDISABLE; 349 350 /* 351 * XXX 352 * OSF/1 doesn't turn off the <discard>, <literal-next> or <status> 353 * characters when curses switches into raw mode. It should be OK 354 * to do it explicitly for everyone. 355 */ 356 #ifdef VDISCARD 357 clp->vi_enter.c_cc[VDISCARD] = _POSIX_VDISABLE; 358 #endif 359 #ifdef VLNEXT 360 clp->vi_enter.c_cc[VLNEXT] = _POSIX_VDISABLE; 361 #endif 362 #ifdef VSTATUS 363 clp->vi_enter.c_cc[VSTATUS] = _POSIX_VDISABLE; 364 #endif 365 366 /* Initialize terminal based information. */ 367 if (cl_term_init(sp)) 368 goto err; 369 370 fast: /* Set the terminal modes. */ 371 if (tcsetattr(STDIN_FILENO, TCSASOFT | TCSADRAIN, &clp->vi_enter)) { 372 if (errno == EINTR) 373 goto fast; 374 msgq(sp, M_SYSERR, "tcsetattr"); 375 err: (void)cl_vi_end(sp->gp); 376 return (1); 377 } 378 return (0); 379 } 380 381 /* 382 * cl_vi_end -- 383 * Shutdown the vi screen. 384 */ 385 static int 386 cl_vi_end(GS *gp) 387 { 388 CL_PRIVATE *clp; 389 390 clp = GCLP(gp); 391 392 /* Restore the cursor keys to normal mode. */ 393 (void)keypad(stdscr, FALSE); 394 395 /* 396 * If we were running vi when we quit, scroll the screen up a single 397 * line so we don't lose any information. 398 * 399 * Move to the bottom of the window (some endwin implementations don't 400 * do this for you). 401 */ 402 if (!F_ISSET(clp, CL_IN_EX)) { 403 (void)move(0, 0); 404 (void)deleteln(); 405 (void)move(LINES - 1, 0); 406 (void)refresh(); 407 } 408 409 cl_freecap(clp); 410 411 /* End curses window. */ 412 (void)endwin(); 413 414 /* 415 * XXX 416 * The screen TE sequence just got sent. See the comment in 417 * cl_funcs.c:cl_attr(). 418 */ 419 clp->ti_te = TE_SENT; 420 421 return (0); 422 } 423 424 /* 425 * cl_ex_init -- 426 * Initialize the ex screen. 427 */ 428 static int 429 cl_ex_init(SCR *sp) 430 { 431 CL_PRIVATE *clp; 432 433 clp = CLP(sp); 434 435 /* If already initialized, just set the terminal modes. */ 436 if (F_ISSET(clp, CL_SCR_EX_INIT)) 437 goto fast; 438 439 /* If not reading from a file, we're done. */ 440 if (!F_ISSET(clp, CL_STDIN_TTY)) 441 return (0); 442 443 /* Get the ex termcap/terminfo strings. */ 444 (void)cl_getcap(sp, "cup", &clp->cup); 445 (void)cl_getcap(sp, "smso", &clp->smso); 446 (void)cl_getcap(sp, "rmso", &clp->rmso); 447 (void)cl_getcap(sp, "el", &clp->el); 448 (void)cl_getcap(sp, "cuu1", &clp->cuu1); 449 450 /* Enter_standout_mode and exit_standout_mode are paired. */ 451 if (clp->smso == NULL || clp->rmso == NULL) { 452 if (clp->smso != NULL) { 453 free(clp->smso); 454 clp->smso = NULL; 455 } 456 if (clp->rmso != NULL) { 457 free(clp->rmso); 458 clp->rmso = NULL; 459 } 460 } 461 462 /* 463 * Turn on canonical mode, with normal input and output processing. 464 * Start with the original terminal settings as the user probably 465 * had them (including any local extensions) set correctly for the 466 * current terminal. 467 * 468 * !!! 469 * We can't get everything that we need portably; for example, ONLCR, 470 * mapping <newline> to <carriage-return> on output isn't required 471 * by POSIX 1003.1b-1993. If this turns out to be a problem, then 472 * we'll either have to play some games on the mapping, or we'll have 473 * to make all ex printf's output \r\n instead of \n. 474 */ 475 clp->ex_enter = clp->orig; 476 clp->ex_enter.c_lflag |= ECHO | ECHOE | ECHOK | ICANON | IEXTEN | ISIG; 477 #ifdef ECHOCTL 478 clp->ex_enter.c_lflag |= ECHOCTL; 479 #endif 480 #ifdef ECHOKE 481 clp->ex_enter.c_lflag |= ECHOKE; 482 #endif 483 clp->ex_enter.c_iflag |= ICRNL; 484 clp->ex_enter.c_oflag |= OPOST; 485 #ifdef ONLCR 486 clp->ex_enter.c_oflag |= ONLCR; 487 #endif 488 489 fast: if (tcsetattr(STDIN_FILENO, TCSADRAIN | TCSASOFT, &clp->ex_enter)) { 490 if (errno == EINTR) 491 goto fast; 492 msgq(sp, M_SYSERR, "tcsetattr"); 493 return (1); 494 } 495 return (0); 496 } 497 498 /* 499 * cl_ex_end -- 500 * Shutdown the ex screen. 501 */ 502 static int 503 cl_ex_end(GS *gp) 504 { 505 CL_PRIVATE *clp; 506 507 clp = GCLP(gp); 508 509 cl_freecap(clp); 510 511 return (0); 512 } 513 514 /* 515 * cl_getcap -- 516 * Retrieve termcap/terminfo strings. 517 * 518 * PUBLIC: int cl_getcap __P((SCR *, const char *, char **)); 519 */ 520 int 521 cl_getcap(SCR *sp, const char *name, char **elementp) 522 { 523 size_t len; 524 char *t; 525 526 if ((t = tigetstr(name)) != NULL && 527 t != (char *)-1 && (len = strlen(t)) != 0) { 528 MALLOC_RET(sp, *elementp, char *, len + 1); 529 memmove(*elementp, t, len + 1); 530 } 531 return (0); 532 } 533 534 /* 535 * cl_freecap -- 536 * Free any allocated termcap/terminfo strings. 537 */ 538 static void 539 cl_freecap(CL_PRIVATE *clp) 540 { 541 if (clp->el != NULL) { 542 free(clp->el); 543 clp->el = NULL; 544 } 545 if (clp->cup != NULL) { 546 free(clp->cup); 547 clp->cup = NULL; 548 } 549 if (clp->cuu1 != NULL) { 550 free(clp->cuu1); 551 clp->cuu1 = NULL; 552 } 553 if (clp->rmso != NULL) { 554 free(clp->rmso); 555 clp->rmso = NULL; 556 } 557 if (clp->smso != NULL) { 558 free(clp->smso); 559 clp->smso = NULL; 560 } 561 } 562 563 /* 564 * cl_putenv -- 565 * Put a value into the environment. 566 */ 567 static int 568 cl_putenv(SCR *sp, const char *name, const char *str, u_long value) 569 { 570 char buf[40]; 571 572 if (str == NULL) { 573 (void)snprintf(buf, sizeof(buf), "%lu", value); 574 return (cl_setenv(sp, name, buf)); 575 } else 576 return (cl_setenv(sp, name, str)); 577 } 578