1 /* $OpenBSD: cl_funcs.c,v 1.17 2014/11/12 16:29:04 millert Exp $ */ 2 3 /*- 4 * Copyright (c) 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * Copyright (c) 1993, 1994, 1995, 1996 7 * Keith Bostic. All rights reserved. 8 * 9 * See the LICENSE file for redistribution information. 10 */ 11 12 #include "config.h" 13 14 #include <sys/types.h> 15 #include <sys/queue.h> 16 #include <sys/time.h> 17 18 #include <bitstring.h> 19 #include <ctype.h> 20 #include <curses.h> 21 #include <signal.h> 22 #include <stdio.h> 23 #include <stdlib.h> 24 #include <string.h> 25 #include <term.h> 26 #include <termios.h> 27 #include <unistd.h> 28 29 #include "../common/common.h" 30 #include "../vi/vi.h" 31 #include "cl.h" 32 33 /* 34 * cl_addstr -- 35 * Add len bytes from the string at the cursor, advancing the cursor. 36 * 37 * PUBLIC: int cl_addstr(SCR *, const char *, size_t); 38 */ 39 int 40 cl_addstr(SCR *sp, const char *str, size_t len) 41 { 42 size_t oldy, oldx; 43 int iv; 44 45 /* 46 * If ex isn't in control, it's the last line of the screen and 47 * it's a split screen, use inverse video. 48 */ 49 iv = 0; 50 getyx(stdscr, oldy, oldx); 51 if (!F_ISSET(sp, SC_SCR_EXWROTE) && 52 oldy == RLNO(sp, LASTLINE(sp)) && IS_SPLIT(sp)) { 53 iv = 1; 54 (void)standout(); 55 } 56 57 if (addnstr(str, len) == ERR) 58 return (1); 59 60 if (iv) 61 (void)standend(); 62 return (0); 63 } 64 65 /* 66 * cl_attr -- 67 * Toggle a screen attribute on/off. 68 * 69 * PUBLIC: int cl_attr(SCR *, scr_attr_t, int); 70 */ 71 int 72 cl_attr(SCR *sp, scr_attr_t attribute, int on) 73 { 74 CL_PRIVATE *clp; 75 76 clp = CLP(sp); 77 78 switch (attribute) { 79 case SA_ALTERNATE: 80 /* 81 * !!! 82 * There's a major layering violation here. The problem is that the 83 * X11 xterm screen has what's known as an "alternate" screen. Some 84 * xterm termcap/terminfo entries include sequences to switch to/from 85 * that alternate screen as part of the ti/te (smcup/rmcup) strings. 86 * Vi runs in the alternate screen, so that you are returned to the 87 * same screen contents on exit from vi that you had when you entered 88 * vi. Further, when you run :shell, or :!date or similar ex commands, 89 * you also see the original screen contents. This wasn't deliberate 90 * on vi's part, it's just that it historically sent terminal init/end 91 * sequences at those times, and the addition of the alternate screen 92 * sequences to the strings changed the behavior of vi. The problem 93 * caused by this is that we don't want to switch back to the alternate 94 * screen while getting a new command from the user, when the user is 95 * continuing to enter ex commands, e.g.: 96 * 97 * :!date <<< switch to original screen 98 * [Hit return to continue] <<< prompt user to continue 99 * :command <<< get command from user 100 * 101 * Note that the :command input is a true vi input mode, e.g., input 102 * maps and abbreviations are being done. So, we need to be able to 103 * switch back into the vi screen mode, without flashing the screen. 104 * 105 * To make matters worse, the curses initscr() and endwin() calls will 106 * do this automatically -- so, this attribute isn't as controlled by 107 * the higher level screen as closely as one might like. 108 */ 109 if (on) { 110 if (clp->ti_te != TI_SENT) { 111 clp->ti_te = TI_SENT; 112 if (clp->smcup == NULL) 113 (void)cl_getcap(sp, "smcup", &clp->smcup); 114 if (clp->smcup != NULL) 115 (void)tputs(clp->smcup, 1, cl_putchar); 116 } 117 } else 118 if (clp->ti_te != TE_SENT) { 119 clp->ti_te = TE_SENT; 120 if (clp->rmcup == NULL) 121 (void)cl_getcap(sp, "rmcup", &clp->rmcup); 122 if (clp->rmcup != NULL) 123 (void)tputs(clp->rmcup, 1, cl_putchar); 124 (void)fflush(stdout); 125 } 126 (void)fflush(stdout); 127 break; 128 case SA_INVERSE: 129 if (F_ISSET(sp, SC_EX | SC_SCR_EXWROTE)) { 130 if (clp->smso == NULL) 131 return (1); 132 if (on) 133 (void)tputs(clp->smso, 1, cl_putchar); 134 else 135 (void)tputs(clp->rmso, 1, cl_putchar); 136 (void)fflush(stdout); 137 } else { 138 if (on) 139 (void)standout(); 140 else 141 (void)standend(); 142 } 143 break; 144 default: 145 abort(); 146 } 147 return (0); 148 } 149 150 /* 151 * cl_baud -- 152 * Return the baud rate. 153 * 154 * PUBLIC: int cl_baud(SCR *, u_long *); 155 */ 156 int 157 cl_baud(SCR *sp, u_long *ratep) 158 { 159 CL_PRIVATE *clp; 160 161 /* 162 * XXX 163 * There's no portable way to get a "baud rate" -- cfgetospeed(3) 164 * returns the value associated with some #define, which we may 165 * never have heard of, or which may be a purely local speed. Vi 166 * only cares if it's SLOW (w300), slow (w1200) or fast (w9600). 167 * Try and detect the slow ones, and default to fast. 168 */ 169 clp = CLP(sp); 170 switch (cfgetospeed(&clp->orig)) { 171 case B50: 172 case B75: 173 case B110: 174 case B134: 175 case B150: 176 case B200: 177 case B300: 178 case B600: 179 *ratep = 600; 180 break; 181 case B1200: 182 *ratep = 1200; 183 break; 184 default: 185 *ratep = 9600; 186 break; 187 } 188 return (0); 189 } 190 191 /* 192 * cl_bell -- 193 * Ring the bell/flash the screen. 194 * 195 * PUBLIC: int cl_bell(SCR *); 196 */ 197 int 198 cl_bell(SCR *sp) 199 { 200 if (F_ISSET(sp, SC_EX | SC_SCR_EXWROTE)) 201 (void)write(STDOUT_FILENO, "\07", 1); /* \a */ 202 else { 203 /* 204 * If the screen has not been setup we cannot call 205 * curses routines yet. 206 */ 207 if (F_ISSET(sp, SC_SCR_VI)) { 208 /* 209 * Vi has an edit option which determines if the 210 * terminal should be beeped or the screen flashed. 211 */ 212 if (O_ISSET(sp, O_FLASH)) 213 (void)flash(); 214 else 215 (void)beep(); 216 } else if (!O_ISSET(sp, O_FLASH)) 217 (void)write(STDOUT_FILENO, "\07", 1); 218 } 219 return (0); 220 } 221 222 /* 223 * cl_clrtoeol -- 224 * Clear from the current cursor to the end of the line. 225 * 226 * PUBLIC: int cl_clrtoeol(SCR *); 227 */ 228 int 229 cl_clrtoeol(SCR *sp) 230 { 231 return (clrtoeol() == ERR); 232 } 233 234 /* 235 * cl_cursor -- 236 * Return the current cursor position. 237 * 238 * PUBLIC: int cl_cursor(SCR *, size_t *, size_t *); 239 */ 240 int 241 cl_cursor(SCR *sp, size_t *yp, size_t *xp) 242 { 243 /* 244 * The curses screen support splits a single underlying curses screen 245 * into multiple screens to support split screen semantics. For this 246 * reason the returned value must be adjusted to be relative to the 247 * current screen, and not absolute. Screens that implement the split 248 * using physically distinct screens won't need this hack. 249 */ 250 getyx(stdscr, *yp, *xp); 251 *yp -= sp->woff; 252 return (0); 253 } 254 255 /* 256 * cl_deleteln -- 257 * Delete the current line, scrolling all lines below it. 258 * 259 * PUBLIC: int cl_deleteln(SCR *); 260 */ 261 int 262 cl_deleteln(SCR *sp) 263 { 264 #ifndef mvchgat 265 CHAR_T ch; 266 size_t col, lno, spcnt; 267 #endif 268 size_t oldy, oldx; 269 270 /* 271 * This clause is required because the curses screen uses reverse 272 * video to delimit split screens. If the screen does not do this, 273 * this code won't be necessary. 274 * 275 * If the bottom line was in reverse video, rewrite it in normal 276 * video before it's scrolled. 277 * 278 * Check for the existence of a chgat function; XSI requires it, but 279 * historic implementations of System V curses don't. If it's not 280 * a #define, we'll fall back to doing it by hand, which is slow but 281 * acceptable. 282 * 283 * By hand means walking through the line, retrieving and rewriting 284 * each character. Curses has no EOL marker, so track strings of 285 * spaces, and copy the trailing spaces only if there's a non-space 286 * character following. 287 */ 288 if (!F_ISSET(sp, SC_SCR_EXWROTE) && IS_SPLIT(sp)) { 289 getyx(stdscr, oldy, oldx); 290 mvchgat(RLNO(sp, LASTLINE(sp)), 0, -1, A_NORMAL, 0, NULL); 291 (void)move(oldy, oldx); 292 } 293 294 /* 295 * The bottom line is expected to be blank after this operation, 296 * and other screens must support that semantic. 297 */ 298 return (deleteln() == ERR); 299 } 300 301 /* 302 * cl_ex_adjust -- 303 * Adjust the screen for ex. This routine is purely for standalone 304 * ex programs. All special purpose, all special case. 305 * 306 * PUBLIC: int cl_ex_adjust(SCR *, exadj_t); 307 */ 308 int 309 cl_ex_adjust(SCR *sp, exadj_t action) 310 { 311 CL_PRIVATE *clp; 312 int cnt; 313 314 clp = CLP(sp); 315 switch (action) { 316 case EX_TERM_SCROLL: 317 /* Move the cursor up one line if that's possible. */ 318 if (clp->cuu1 != NULL) 319 (void)tputs(clp->cuu1, 1, cl_putchar); 320 else if (clp->cup != NULL) 321 (void)tputs(tgoto(clp->cup, 322 0, LINES - 2), 1, cl_putchar); 323 else 324 return (0); 325 /* FALLTHROUGH */ 326 case EX_TERM_CE: 327 /* Clear the line. */ 328 if (clp->el != NULL) { 329 (void)putchar('\r'); 330 (void)tputs(clp->el, 1, cl_putchar); 331 } else { 332 /* 333 * Historically, ex didn't erase the line, so, if the 334 * displayed line was only a single glyph, and <eof> 335 * was more than one glyph, the output would not fully 336 * overwrite the user's input. To fix this, output 337 * the maxiumum character number of spaces. Note, 338 * this won't help if the user entered extra prompt 339 * or <blank> characters before the command character. 340 * We'd have to do a lot of work to make that work, and 341 * it's almost certainly not worth the effort. 342 */ 343 for (cnt = 0; cnt < MAX_CHARACTER_COLUMNS; ++cnt) 344 (void)putchar('\b'); 345 for (cnt = 0; cnt < MAX_CHARACTER_COLUMNS; ++cnt) 346 (void)putchar(' '); 347 (void)putchar('\r'); 348 (void)fflush(stdout); 349 } 350 break; 351 default: 352 abort(); 353 } 354 return (0); 355 } 356 357 /* 358 * cl_insertln -- 359 * Push down the current line, discarding the bottom line. 360 * 361 * PUBLIC: int cl_insertln(SCR *); 362 */ 363 int 364 cl_insertln(SCR *sp) 365 { 366 /* 367 * The current line is expected to be blank after this operation, 368 * and the screen must support that semantic. 369 */ 370 return (insertln() == ERR); 371 } 372 373 /* 374 * cl_keyval -- 375 * Return the value for a special key. 376 * 377 * PUBLIC: int cl_keyval(SCR *, scr_keyval_t, CHAR_T *, int *); 378 */ 379 int 380 cl_keyval(SCR *sp, scr_keyval_t val, CHAR_T *chp, int *dnep) 381 { 382 CL_PRIVATE *clp; 383 384 /* 385 * VEOF, VERASE and VKILL are required by POSIX 1003.1-1990, 386 * VWERASE is a 4BSD extension. 387 */ 388 clp = CLP(sp); 389 switch (val) { 390 case KEY_VEOF: 391 *dnep = (*chp = clp->orig.c_cc[VEOF]) == _POSIX_VDISABLE; 392 break; 393 case KEY_VERASE: 394 *dnep = (*chp = clp->orig.c_cc[VERASE]) == _POSIX_VDISABLE; 395 break; 396 case KEY_VKILL: 397 *dnep = (*chp = clp->orig.c_cc[VKILL]) == _POSIX_VDISABLE; 398 break; 399 case KEY_VWERASE: 400 *dnep = (*chp = clp->orig.c_cc[VWERASE]) == _POSIX_VDISABLE; 401 break; 402 default: 403 *dnep = 1; 404 break; 405 } 406 return (0); 407 } 408 409 /* 410 * cl_move -- 411 * Move the cursor. 412 * 413 * PUBLIC: int cl_move(SCR *, size_t, size_t); 414 */ 415 int 416 cl_move(SCR *sp, size_t lno, size_t cno) 417 { 418 /* See the comment in cl_cursor. */ 419 if (move(RLNO(sp, lno), cno) == ERR) { 420 msgq(sp, M_ERR, 421 "Error: move: l(%u) c(%u) o(%u)", lno, cno, sp->woff); 422 return (1); 423 } 424 return (0); 425 } 426 427 /* 428 * cl_refresh -- 429 * Refresh the screen. 430 * 431 * PUBLIC: int cl_refresh(SCR *, int); 432 */ 433 int 434 cl_refresh(SCR *sp, int repaint) 435 { 436 CL_PRIVATE *clp; 437 438 clp = CLP(sp); 439 440 /* 441 * If we received a killer signal, we're done, there's no point 442 * in refreshing the screen. 443 */ 444 if (clp->killersig) 445 return (0); 446 447 /* 448 * If repaint is set, the editor is telling us that we don't know 449 * what's on the screen, so we have to repaint from scratch. 450 * 451 * In the curses library, doing wrefresh(curscr) is okay, but the 452 * screen flashes when we then apply the refresh() to bring it up 453 * to date. So, use clearok(). 454 */ 455 if (repaint) 456 clearok(curscr, 1); 457 return (refresh() == ERR); 458 } 459 460 /* 461 * cl_rename -- 462 * Rename the file. 463 * 464 * PUBLIC: int cl_rename(SCR *, char *, int); 465 */ 466 int 467 cl_rename(SCR *sp, char *name, int on) 468 { 469 GS *gp; 470 CL_PRIVATE *clp; 471 char *ttype; 472 473 gp = sp->gp; 474 clp = CLP(sp); 475 476 ttype = OG_STR(gp, GO_TERM); 477 478 /* 479 * XXX 480 * We can only rename windows for xterm. 481 */ 482 if (on) { 483 if (F_ISSET(clp, CL_RENAME_OK) && 484 !strncmp(ttype, "xterm", sizeof("xterm") - 1)) { 485 F_SET(clp, CL_RENAME); 486 (void)printf(XTERM_RENAME, name); 487 (void)fflush(stdout); 488 } 489 } else 490 if (F_ISSET(clp, CL_RENAME)) { 491 F_CLR(clp, CL_RENAME); 492 (void)printf(XTERM_RENAME, ttype); 493 (void)fflush(stdout); 494 } 495 return (0); 496 } 497 498 /* 499 * cl_suspend -- 500 * Suspend a screen. 501 * 502 * PUBLIC: int cl_suspend(SCR *, int *); 503 */ 504 int 505 cl_suspend(SCR *sp, int *allowedp) 506 { 507 struct termios t; 508 CL_PRIVATE *clp; 509 size_t oldy, oldx; 510 int changed; 511 512 clp = CLP(sp); 513 *allowedp = 1; 514 515 /* 516 * The ex implementation of this function isn't needed by screens not 517 * supporting ex commands that require full terminal canonical mode 518 * (e.g. :suspend). 519 * 520 * The vi implementation of this function isn't needed by screens not 521 * supporting vi process suspension, i.e. any screen that isn't backed 522 * by a UNIX shell. 523 * 524 * Setting allowedp to 0 will cause the editor to reject the command. 525 */ 526 if (F_ISSET(sp, SC_EX)) { 527 /* Save the terminal settings, and restore the original ones. */ 528 if (F_ISSET(clp, CL_STDIN_TTY)) { 529 (void)tcgetattr(STDIN_FILENO, &t); 530 (void)tcsetattr(STDIN_FILENO, 531 TCSASOFT | TCSADRAIN, &clp->orig); 532 } 533 534 /* Stop the process group. */ 535 (void)kill(0, SIGTSTP); 536 537 /* Time passes ... */ 538 539 /* Restore terminal settings. */ 540 if (F_ISSET(clp, CL_STDIN_TTY)) 541 (void)tcsetattr(STDIN_FILENO, TCSASOFT | TCSADRAIN, &t); 542 return (0); 543 } 544 545 /* 546 * Move to the lower left-hand corner of the screen. 547 * 548 * XXX 549 * Not sure this is necessary in System V implementations, but it 550 * shouldn't hurt. 551 */ 552 getyx(stdscr, oldy, oldx); 553 (void)move(LINES - 1, 0); 554 (void)refresh(); 555 556 /* 557 * Temporarily end the screen. System V introduced a semantic where 558 * endwin() could be restarted. We use it because restarting curses 559 * from scratch often fails in System V. 560 */ 561 562 /* Restore the cursor keys to normal mode. */ 563 (void)keypad(stdscr, FALSE); 564 565 /* Restore the window name. */ 566 (void)cl_rename(sp, NULL, 0); 567 568 (void)endwin(); 569 570 /* 571 * XXX 572 * Restore the original terminal settings. This is bad -- the 573 * reset can cause character loss from the tty queue. However, 574 * we can't call endwin() in BSD curses implementations, and too 575 * many System V curses implementations don't get it right. 576 */ 577 (void)tcsetattr(STDIN_FILENO, TCSADRAIN | TCSASOFT, &clp->orig); 578 579 /* Stop the process group. */ 580 (void)kill(0, SIGTSTP); 581 582 /* Time passes ... */ 583 584 /* 585 * If we received a killer signal, we're done. Leave everything 586 * unchanged. In addition, the terminal has already been reset 587 * correctly, so leave it alone. 588 */ 589 if (clp->killersig) { 590 F_CLR(clp, CL_SCR_EX_INIT | CL_SCR_VI_INIT); 591 return (0); 592 } 593 594 /* Set the window name. */ 595 (void)cl_rename(sp, sp->frp->name, 1); 596 597 /* Put the cursor keys into application mode. */ 598 (void)keypad(stdscr, TRUE); 599 600 /* Refresh and repaint the screen. */ 601 (void)move(oldy, oldx); 602 (void)cl_refresh(sp, 1); 603 604 /* If the screen changed size, set the SIGWINCH bit. */ 605 if (cl_ssize(sp, 1, NULL, NULL, &changed)) 606 return (1); 607 if (changed) 608 F_SET(CLP(sp), CL_SIGWINCH); 609 610 return (0); 611 } 612 613 /* 614 * cl_usage -- 615 * Print out the curses usage messages. 616 * 617 * PUBLIC: void cl_usage(void); 618 */ 619 void 620 cl_usage() 621 { 622 switch (pmode) { 623 case MODE_EX: 624 (void)fprintf(stderr, "usage: " 625 "ex [-FRrSsv] [-c cmd] [-t tag] [-w size] [file ...]\n"); 626 break; 627 case MODE_VI: 628 (void)fprintf(stderr, "usage: " 629 "vi [-eFRrS] [-c cmd] [-t tag] [-w size] [file ...]\n"); 630 break; 631 case MODE_VIEW: 632 (void)fprintf(stderr, "usage: " 633 "view [-eFrS] [-c cmd] [-t tag] [-w size] [file ...]\n"); 634 break; 635 } 636 } 637 638 #ifdef DEBUG 639 /* 640 * gdbrefresh -- 641 * Stub routine so can flush out curses screen changes using gdb. 642 */ 643 int 644 gdbrefresh() 645 { 646 refresh(); 647 return (0); /* XXX Convince gdb to run it. */ 648 } 649 #endif 650