1 /* $OpenBSD: status.c,v 1.118 2014/11/05 23:15:11 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER 15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/types.h> 20 #include <sys/time.h> 21 22 #include <errno.h> 23 #include <limits.h> 24 #include <stdarg.h> 25 #include <stdlib.h> 26 #include <string.h> 27 #include <time.h> 28 #include <unistd.h> 29 30 #include "tmux.h" 31 32 char *status_redraw_get_left( 33 struct client *, time_t, int, struct grid_cell *, size_t *); 34 char *status_redraw_get_right( 35 struct client *, time_t, int, struct grid_cell *, size_t *); 36 char *status_find_job(struct client *, char **); 37 void status_job_free(void *); 38 void status_job_callback(struct job *); 39 char *status_print( 40 struct client *, struct winlink *, time_t, struct grid_cell *); 41 void status_replace1(struct client *, char **, char **, char *, size_t, int); 42 void status_message_callback(int, short, void *); 43 44 const char *status_prompt_up_history(u_int *); 45 const char *status_prompt_down_history(u_int *); 46 void status_prompt_add_history(const char *); 47 char *status_prompt_complete(const char *); 48 49 /* Status prompt history. */ 50 ARRAY_DECL(, char *) status_prompt_history = ARRAY_INITIALIZER; 51 52 /* Status output tree. */ 53 RB_GENERATE(status_out_tree, status_out, entry, status_out_cmp); 54 55 /* Output tree comparison function. */ 56 int 57 status_out_cmp(struct status_out *so1, struct status_out *so2) 58 { 59 return (strcmp(so1->cmd, so2->cmd)); 60 } 61 62 /* Get screen line of status line. -1 means off. */ 63 int 64 status_at_line(struct client *c) 65 { 66 struct session *s = c->session; 67 68 if (!options_get_number(&s->options, "status")) 69 return (-1); 70 71 if (options_get_number(&s->options, "status-position") == 0) 72 return (0); 73 return (c->tty.sy - 1); 74 } 75 76 /* Retrieve options for left string. */ 77 char * 78 status_redraw_get_left(struct client *c, 79 time_t t, int utf8flag, struct grid_cell *gc, size_t *size) 80 { 81 struct session *s = c->session; 82 char *left; 83 size_t leftlen; 84 85 style_apply_update(gc, &s->options, "status-left-style"); 86 87 left = status_replace(c, NULL, 88 NULL, NULL, options_get_string(&s->options, "status-left"), t, 1); 89 90 *size = options_get_number(&s->options, "status-left-length"); 91 leftlen = screen_write_cstrlen(utf8flag, "%s", left); 92 if (leftlen < *size) 93 *size = leftlen; 94 return (left); 95 } 96 97 /* Retrieve options for right string. */ 98 char * 99 status_redraw_get_right(struct client *c, 100 time_t t, int utf8flag, struct grid_cell *gc, size_t *size) 101 { 102 struct session *s = c->session; 103 char *right; 104 size_t rightlen; 105 106 style_apply_update(gc, &s->options, "status-right-style"); 107 108 right = status_replace(c, NULL, 109 NULL, NULL, options_get_string(&s->options, "status-right"), t, 1); 110 111 *size = options_get_number(&s->options, "status-right-length"); 112 rightlen = screen_write_cstrlen(utf8flag, "%s", right); 113 if (rightlen < *size) 114 *size = rightlen; 115 return (right); 116 } 117 118 /* Set window at window list position. */ 119 void 120 status_set_window_at(struct client *c, u_int x) 121 { 122 struct session *s = c->session; 123 struct winlink *wl; 124 struct options *oo; 125 size_t len; 126 127 x += c->wlmouse; 128 RB_FOREACH(wl, winlinks, &s->windows) { 129 oo = &wl->window->options; 130 131 len = strlen(options_get_string(oo, "window-status-separator")); 132 if (x < wl->status_width && session_select(s, wl->idx) == 0) 133 server_redraw_session(s); 134 x -= wl->status_width + len; 135 } 136 } 137 138 /* Draw status for client on the last lines of given context. */ 139 int 140 status_redraw(struct client *c) 141 { 142 struct screen_write_ctx ctx; 143 struct session *s = c->session; 144 struct winlink *wl; 145 struct screen old_status, window_list; 146 struct grid_cell stdgc, lgc, rgc, gc; 147 struct options *oo; 148 time_t t; 149 char *left, *right, *sep; 150 u_int offset, needed; 151 u_int wlstart, wlwidth, wlavailable, wloffset, wlsize; 152 size_t llen, rlen, seplen; 153 int larrow, rarrow, utf8flag; 154 155 /* No status line? */ 156 if (c->tty.sy == 0 || !options_get_number(&s->options, "status")) 157 return (1); 158 left = right = NULL; 159 larrow = rarrow = 0; 160 161 /* Update status timer. */ 162 if (gettimeofday(&c->status_timer, NULL) != 0) 163 fatal("gettimeofday failed"); 164 t = c->status_timer.tv_sec; 165 166 /* Set up default colour. */ 167 style_apply(&stdgc, &s->options, "status-style"); 168 169 /* Create the target screen. */ 170 memcpy(&old_status, &c->status, sizeof old_status); 171 screen_init(&c->status, c->tty.sx, 1, 0); 172 screen_write_start(&ctx, NULL, &c->status); 173 for (offset = 0; offset < c->tty.sx; offset++) 174 screen_write_putc(&ctx, &stdgc, ' '); 175 screen_write_stop(&ctx); 176 177 /* If the height is one line, blank status line. */ 178 if (c->tty.sy <= 1) 179 goto out; 180 181 /* Get UTF-8 flag. */ 182 utf8flag = options_get_number(&s->options, "status-utf8"); 183 184 /* Work out left and right strings. */ 185 memcpy(&lgc, &stdgc, sizeof lgc); 186 left = status_redraw_get_left(c, t, utf8flag, &lgc, &llen); 187 memcpy(&rgc, &stdgc, sizeof rgc); 188 right = status_redraw_get_right(c, t, utf8flag, &rgc, &rlen); 189 190 /* 191 * Figure out how much space we have for the window list. If there 192 * isn't enough space, just show a blank status line. 193 */ 194 needed = 0; 195 if (llen != 0) 196 needed += llen; 197 if (rlen != 0) 198 needed += rlen; 199 if (c->tty.sx == 0 || c->tty.sx <= needed) 200 goto out; 201 wlavailable = c->tty.sx - needed; 202 203 /* Calculate the total size needed for the window list. */ 204 wlstart = wloffset = wlwidth = 0; 205 RB_FOREACH(wl, winlinks, &s->windows) { 206 free(wl->status_text); 207 memcpy(&wl->status_cell, &stdgc, sizeof wl->status_cell); 208 wl->status_text = status_print(c, wl, t, &wl->status_cell); 209 wl->status_width = 210 screen_write_cstrlen(utf8flag, "%s", wl->status_text); 211 212 if (wl == s->curw) 213 wloffset = wlwidth; 214 215 oo = &wl->window->options; 216 sep = options_get_string(oo, "window-status-separator"); 217 seplen = screen_write_strlen(utf8flag, "%s", sep); 218 wlwidth += wl->status_width + seplen; 219 } 220 221 /* Create a new screen for the window list. */ 222 screen_init(&window_list, wlwidth, 1, 0); 223 224 /* And draw the window list into it. */ 225 screen_write_start(&ctx, NULL, &window_list); 226 RB_FOREACH(wl, winlinks, &s->windows) { 227 screen_write_cnputs(&ctx, 228 -1, &wl->status_cell, utf8flag, "%s", wl->status_text); 229 230 oo = &wl->window->options; 231 sep = options_get_string(oo, "window-status-separator"); 232 screen_write_nputs(&ctx, -1, &stdgc, utf8flag, "%s", sep); 233 } 234 screen_write_stop(&ctx); 235 236 /* If there is enough space for the total width, skip to draw now. */ 237 if (wlwidth <= wlavailable) 238 goto draw; 239 240 /* Find size of current window text. */ 241 wlsize = s->curw->status_width; 242 243 /* 244 * If the current window is already on screen, good to draw from the 245 * start and just leave off the end. 246 */ 247 if (wloffset + wlsize < wlavailable) { 248 if (wlavailable > 0) { 249 rarrow = 1; 250 wlavailable--; 251 } 252 wlwidth = wlavailable; 253 } else { 254 /* 255 * Work out how many characters we need to omit from the 256 * start. There are wlavailable characters to fill, and 257 * wloffset + wlsize must be the last. So, the start character 258 * is wloffset + wlsize - wlavailable. 259 */ 260 if (wlavailable > 0) { 261 larrow = 1; 262 wlavailable--; 263 } 264 265 wlstart = wloffset + wlsize - wlavailable; 266 if (wlavailable > 0 && wlwidth > wlstart + wlavailable + 1) { 267 rarrow = 1; 268 wlstart++; 269 wlavailable--; 270 } 271 wlwidth = wlavailable; 272 } 273 274 /* Bail if anything is now too small too. */ 275 if (wlwidth == 0 || wlavailable == 0) { 276 screen_free(&window_list); 277 goto out; 278 } 279 280 /* 281 * Now the start position is known, work out the state of the left and 282 * right arrows. 283 */ 284 offset = 0; 285 RB_FOREACH(wl, winlinks, &s->windows) { 286 if (wl->flags & WINLINK_ALERTFLAGS && 287 larrow == 1 && offset < wlstart) 288 larrow = -1; 289 290 offset += wl->status_width; 291 292 if (wl->flags & WINLINK_ALERTFLAGS && 293 rarrow == 1 && offset > wlstart + wlwidth) 294 rarrow = -1; 295 } 296 297 draw: 298 /* Begin drawing. */ 299 screen_write_start(&ctx, NULL, &c->status); 300 301 /* Draw the left string and arrow. */ 302 screen_write_cursormove(&ctx, 0, 0); 303 if (llen != 0) 304 screen_write_cnputs(&ctx, llen, &lgc, utf8flag, "%s", left); 305 if (larrow != 0) { 306 memcpy(&gc, &stdgc, sizeof gc); 307 if (larrow == -1) 308 gc.attr ^= GRID_ATTR_REVERSE; 309 screen_write_putc(&ctx, &gc, '<'); 310 } 311 312 /* Draw the right string and arrow. */ 313 if (rarrow != 0) { 314 screen_write_cursormove(&ctx, c->tty.sx - rlen - 1, 0); 315 memcpy(&gc, &stdgc, sizeof gc); 316 if (rarrow == -1) 317 gc.attr ^= GRID_ATTR_REVERSE; 318 screen_write_putc(&ctx, &gc, '>'); 319 } else 320 screen_write_cursormove(&ctx, c->tty.sx - rlen, 0); 321 if (rlen != 0) 322 screen_write_cnputs(&ctx, rlen, &rgc, utf8flag, "%s", right); 323 324 /* Figure out the offset for the window list. */ 325 if (llen != 0) 326 wloffset = llen; 327 else 328 wloffset = 0; 329 if (wlwidth < wlavailable) { 330 switch (options_get_number(&s->options, "status-justify")) { 331 case 1: /* centered */ 332 wloffset += (wlavailable - wlwidth) / 2; 333 break; 334 case 2: /* right */ 335 wloffset += (wlavailable - wlwidth); 336 break; 337 } 338 } 339 if (larrow != 0) 340 wloffset++; 341 342 /* Copy the window list. */ 343 c->wlmouse = -wloffset + wlstart; 344 screen_write_cursormove(&ctx, wloffset, 0); 345 screen_write_copy(&ctx, &window_list, wlstart, 0, wlwidth, 1); 346 screen_free(&window_list); 347 348 screen_write_stop(&ctx); 349 350 out: 351 free(left); 352 free(right); 353 354 if (grid_compare(c->status.grid, old_status.grid) == 0) { 355 screen_free(&old_status); 356 return (0); 357 } 358 screen_free(&old_status); 359 return (1); 360 } 361 362 /* Replace a single special sequence (prefixed by #). */ 363 void 364 status_replace1(struct client *c, char **iptr, char **optr, char *out, 365 size_t outsize, int jobsflag) 366 { 367 char ch, tmp[256], *ptr, *endptr; 368 size_t ptrlen; 369 long limit; 370 371 errno = 0; 372 limit = strtol(*iptr, &endptr, 10); 373 if ((limit == 0 && errno != EINVAL) || 374 (limit == LONG_MIN && errno != ERANGE) || 375 (limit == LONG_MAX && errno != ERANGE) || 376 limit != 0) 377 *iptr = endptr; 378 if (limit <= 0) 379 limit = LONG_MAX; 380 381 switch (*(*iptr)++) { 382 case '(': 383 if (!jobsflag) { 384 ch = ')'; 385 goto skip_to; 386 } 387 if ((ptr = status_find_job(c, iptr)) == NULL) 388 return; 389 goto do_replace; 390 case '[': 391 /* 392 * Embedded style, handled at display time. Leave present and 393 * skip input until ]. 394 */ 395 ch = ']'; 396 goto skip_to; 397 case '{': 398 ptr = (char *) "#{"; 399 goto do_replace; 400 default: 401 xsnprintf(tmp, sizeof tmp, "#%c", *(*iptr - 1)); 402 ptr = tmp; 403 goto do_replace; 404 } 405 406 return; 407 408 do_replace: 409 ptrlen = strlen(ptr); 410 if ((size_t) limit < ptrlen) 411 ptrlen = limit; 412 413 if (*optr + ptrlen >= out + outsize - 1) 414 return; 415 while (ptrlen > 0 && *ptr != '\0') { 416 *(*optr)++ = *ptr++; 417 ptrlen--; 418 } 419 420 return; 421 422 skip_to: 423 *(*optr)++ = '#'; 424 425 (*iptr)--; /* include ch */ 426 while (**iptr != ch && **iptr != '\0') { 427 if (*optr >= out + outsize - 1) 428 break; 429 *(*optr)++ = *(*iptr)++; 430 } 431 } 432 433 /* Replace special sequences in fmt. */ 434 char * 435 status_replace(struct client *c, struct session *s, struct winlink *wl, 436 struct window_pane *wp, const char *fmt, time_t t, int jobsflag) 437 { 438 static char out[BUFSIZ]; 439 char in[BUFSIZ], ch, *iptr, *optr, *expanded; 440 size_t len; 441 struct format_tree *ft; 442 443 if (fmt == NULL) 444 return (xstrdup("")); 445 446 if (s == NULL && c != NULL) 447 s = c->session; 448 if (wl == NULL && s != NULL) 449 wl = s->curw; 450 if (wp == NULL && wl != NULL) 451 wp = wl->window->active; 452 453 len = strftime(in, sizeof in, fmt, localtime(&t)); 454 in[len] = '\0'; 455 456 iptr = in; 457 optr = out; 458 459 while (*iptr != '\0') { 460 if (optr >= out + (sizeof out) - 1) 461 break; 462 ch = *iptr++; 463 464 if (ch != '#' || *iptr == '\0') { 465 *optr++ = ch; 466 continue; 467 } 468 status_replace1(c, &iptr, &optr, out, sizeof out, jobsflag); 469 } 470 *optr = '\0'; 471 472 ft = format_create(); 473 if (c != NULL) 474 format_client(ft, c); 475 if (s != NULL) 476 format_session(ft, s); 477 if (s != NULL && wl != NULL) 478 format_winlink(ft, s, wl); 479 if (wp != NULL) 480 format_window_pane(ft, wp); 481 expanded = format_expand(ft, out); 482 format_free(ft); 483 return (expanded); 484 } 485 486 /* Figure out job name and get its result, starting it off if necessary. */ 487 char * 488 status_find_job(struct client *c, char **iptr) 489 { 490 struct status_out *so, so_find; 491 char *cmd; 492 int lastesc; 493 size_t len; 494 495 if (**iptr == '\0') 496 return (NULL); 497 if (**iptr == ')') { /* no command given */ 498 (*iptr)++; 499 return (NULL); 500 } 501 502 cmd = xmalloc(strlen(*iptr) + 1); 503 len = 0; 504 505 lastesc = 0; 506 for (; **iptr != '\0'; (*iptr)++) { 507 if (!lastesc && **iptr == ')') 508 break; /* unescaped ) is the end */ 509 if (!lastesc && **iptr == '\\') { 510 lastesc = 1; 511 continue; /* skip \ if not escaped */ 512 } 513 lastesc = 0; 514 cmd[len++] = **iptr; 515 } 516 if (**iptr == '\0') /* no terminating ) */ { 517 free(cmd); 518 return (NULL); 519 } 520 (*iptr)++; /* skip final ) */ 521 cmd[len] = '\0'; 522 523 /* First try in the new tree. */ 524 so_find.cmd = cmd; 525 so = RB_FIND(status_out_tree, &c->status_new, &so_find); 526 if (so != NULL && so->out != NULL) { 527 free(cmd); 528 return (so->out); 529 } 530 531 /* If not found at all, start the job and add to the tree. */ 532 if (so == NULL) { 533 job_run(cmd, NULL, status_job_callback, status_job_free, c); 534 c->references++; 535 536 so = xmalloc(sizeof *so); 537 so->cmd = xstrdup(cmd); 538 so->out = NULL; 539 RB_INSERT(status_out_tree, &c->status_new, so); 540 } 541 542 /* Lookup in the old tree. */ 543 so_find.cmd = cmd; 544 so = RB_FIND(status_out_tree, &c->status_old, &so_find); 545 free(cmd); 546 if (so != NULL) 547 return (so->out); 548 return (NULL); 549 } 550 551 /* Free job tree. */ 552 void 553 status_free_jobs(struct status_out_tree *sotree) 554 { 555 struct status_out *so, *so_next; 556 557 so_next = RB_MIN(status_out_tree, sotree); 558 while (so_next != NULL) { 559 so = so_next; 560 so_next = RB_NEXT(status_out_tree, sotree, so); 561 562 RB_REMOVE(status_out_tree, sotree, so); 563 free(so->out); 564 free(so->cmd); 565 free(so); 566 } 567 } 568 569 /* Update jobs on status interval. */ 570 void 571 status_update_jobs(struct client *c) 572 { 573 /* Free the old tree. */ 574 status_free_jobs(&c->status_old); 575 576 /* Move the new to old. */ 577 memcpy(&c->status_old, &c->status_new, sizeof c->status_old); 578 RB_INIT(&c->status_new); 579 } 580 581 /* Free status job. */ 582 void 583 status_job_free(void *data) 584 { 585 struct client *c = data; 586 587 c->references--; 588 } 589 590 /* Job has finished: save its result. */ 591 void 592 status_job_callback(struct job *job) 593 { 594 struct client *c = job->data; 595 struct status_out *so, so_find; 596 char *line, *buf; 597 size_t len; 598 599 if (c->flags & CLIENT_DEAD) 600 return; 601 602 so_find.cmd = job->cmd; 603 so = RB_FIND(status_out_tree, &c->status_new, &so_find); 604 if (so == NULL || so->out != NULL) 605 return; 606 607 buf = NULL; 608 if ((line = evbuffer_readline(job->event->input)) == NULL) { 609 len = EVBUFFER_LENGTH(job->event->input); 610 buf = xmalloc(len + 1); 611 if (len != 0) 612 memcpy(buf, EVBUFFER_DATA(job->event->input), len); 613 buf[len] = '\0'; 614 } else 615 buf = line; 616 617 so->out = buf; 618 server_status_client(c); 619 } 620 621 /* Return winlink status line entry and adjust gc as necessary. */ 622 char * 623 status_print( 624 struct client *c, struct winlink *wl, time_t t, struct grid_cell *gc) 625 { 626 struct options *oo = &wl->window->options; 627 struct session *s = c->session; 628 const char *fmt; 629 char *text; 630 631 style_apply_update(gc, oo, "window-status-style"); 632 fmt = options_get_string(oo, "window-status-format"); 633 if (wl == s->curw) { 634 style_apply_update(gc, oo, "window-status-current-style"); 635 fmt = options_get_string(oo, "window-status-current-format"); 636 } 637 if (wl == TAILQ_FIRST(&s->lastw)) 638 style_apply_update(gc, oo, "window-status-last-style"); 639 640 if (wl->flags & WINLINK_BELL) 641 style_apply_update(gc, oo, "window-status-bell-style"); 642 else if (wl->flags & (WINLINK_ACTIVITY|WINLINK_SILENCE)) 643 style_apply_update(gc, oo, "window-status-activity-style"); 644 645 text = status_replace(c, NULL, wl, NULL, fmt, t, 1); 646 return (text); 647 } 648 649 /* Set a status line message. */ 650 void 651 status_message_set(struct client *c, const char *fmt, ...) 652 { 653 struct timeval tv; 654 struct message_entry *msg; 655 va_list ap; 656 int delay; 657 u_int i, limit; 658 659 status_prompt_clear(c); 660 status_message_clear(c); 661 662 va_start(ap, fmt); 663 xvasprintf(&c->message_string, fmt, ap); 664 va_end(ap); 665 666 ARRAY_EXPAND(&c->message_log, 1); 667 msg = &ARRAY_LAST(&c->message_log); 668 msg->msg_time = time(NULL); 669 msg->msg = xstrdup(c->message_string); 670 671 limit = options_get_number(&global_options, "message-limit"); 672 if (ARRAY_LENGTH(&c->message_log) > limit) { 673 limit = ARRAY_LENGTH(&c->message_log) - limit; 674 for (i = 0; i < limit; i++) { 675 msg = &ARRAY_FIRST(&c->message_log); 676 free(msg->msg); 677 ARRAY_REMOVE(&c->message_log, 0); 678 } 679 } 680 681 delay = options_get_number(&c->session->options, "display-time"); 682 tv.tv_sec = delay / 1000; 683 tv.tv_usec = (delay % 1000) * 1000L; 684 685 if (event_initialized(&c->message_timer)) 686 evtimer_del(&c->message_timer); 687 evtimer_set(&c->message_timer, status_message_callback, c); 688 evtimer_add(&c->message_timer, &tv); 689 690 c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE); 691 c->flags |= CLIENT_STATUS; 692 } 693 694 /* Clear status line message. */ 695 void 696 status_message_clear(struct client *c) 697 { 698 if (c->message_string == NULL) 699 return; 700 701 free(c->message_string); 702 c->message_string = NULL; 703 704 c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE); 705 c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */ 706 707 screen_reinit(&c->status); 708 } 709 710 /* Clear status line message after timer expires. */ 711 void 712 status_message_callback(unused int fd, unused short event, void *data) 713 { 714 struct client *c = data; 715 716 status_message_clear(c); 717 } 718 719 /* Draw client message on status line of present else on last line. */ 720 int 721 status_message_redraw(struct client *c) 722 { 723 struct screen_write_ctx ctx; 724 struct session *s = c->session; 725 struct screen old_status; 726 size_t len; 727 struct grid_cell gc; 728 int utf8flag; 729 730 if (c->tty.sx == 0 || c->tty.sy == 0) 731 return (0); 732 memcpy(&old_status, &c->status, sizeof old_status); 733 screen_init(&c->status, c->tty.sx, 1, 0); 734 735 utf8flag = options_get_number(&s->options, "status-utf8"); 736 737 len = screen_write_strlen(utf8flag, "%s", c->message_string); 738 if (len > c->tty.sx) 739 len = c->tty.sx; 740 741 style_apply(&gc, &s->options, "message-style"); 742 743 screen_write_start(&ctx, NULL, &c->status); 744 745 screen_write_cursormove(&ctx, 0, 0); 746 screen_write_nputs(&ctx, len, &gc, utf8flag, "%s", c->message_string); 747 for (; len < c->tty.sx; len++) 748 screen_write_putc(&ctx, &gc, ' '); 749 750 screen_write_stop(&ctx); 751 752 if (grid_compare(c->status.grid, old_status.grid) == 0) { 753 screen_free(&old_status); 754 return (0); 755 } 756 screen_free(&old_status); 757 return (1); 758 } 759 760 /* Enable status line prompt. */ 761 void 762 status_prompt_set(struct client *c, const char *msg, const char *input, 763 int (*callbackfn)(void *, const char *), void (*freefn)(void *), 764 void *data, int flags) 765 { 766 int keys; 767 768 status_message_clear(c); 769 status_prompt_clear(c); 770 771 c->prompt_string = status_replace(c, NULL, NULL, NULL, msg, 772 time(NULL), 0); 773 774 c->prompt_buffer = status_replace(c, NULL, NULL, NULL, input, 775 time(NULL), 0); 776 c->prompt_index = strlen(c->prompt_buffer); 777 778 c->prompt_callbackfn = callbackfn; 779 c->prompt_freefn = freefn; 780 c->prompt_data = data; 781 782 c->prompt_hindex = 0; 783 784 c->prompt_flags = flags; 785 786 keys = options_get_number(&c->session->options, "status-keys"); 787 if (keys == MODEKEY_EMACS) 788 mode_key_init(&c->prompt_mdata, &mode_key_tree_emacs_edit); 789 else 790 mode_key_init(&c->prompt_mdata, &mode_key_tree_vi_edit); 791 792 c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE); 793 c->flags |= CLIENT_STATUS; 794 } 795 796 /* Remove status line prompt. */ 797 void 798 status_prompt_clear(struct client *c) 799 { 800 if (c->prompt_string == NULL) 801 return; 802 803 if (c->prompt_freefn != NULL && c->prompt_data != NULL) 804 c->prompt_freefn(c->prompt_data); 805 806 free(c->prompt_string); 807 c->prompt_string = NULL; 808 809 free(c->prompt_buffer); 810 c->prompt_buffer = NULL; 811 812 c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE); 813 c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */ 814 815 screen_reinit(&c->status); 816 } 817 818 /* Update status line prompt with a new prompt string. */ 819 void 820 status_prompt_update(struct client *c, const char *msg, const char *input) 821 { 822 free(c->prompt_string); 823 c->prompt_string = status_replace(c, NULL, NULL, NULL, msg, 824 time(NULL), 0); 825 826 free(c->prompt_buffer); 827 c->prompt_buffer = status_replace(c, NULL, NULL, NULL, input, 828 time(NULL), 0); 829 c->prompt_index = strlen(c->prompt_buffer); 830 831 c->prompt_hindex = 0; 832 833 c->flags |= CLIENT_STATUS; 834 } 835 836 /* Draw client prompt on status line of present else on last line. */ 837 int 838 status_prompt_redraw(struct client *c) 839 { 840 struct screen_write_ctx ctx; 841 struct session *s = c->session; 842 struct screen old_status; 843 size_t i, size, left, len, off; 844 struct grid_cell gc, *gcp; 845 int utf8flag; 846 847 if (c->tty.sx == 0 || c->tty.sy == 0) 848 return (0); 849 memcpy(&old_status, &c->status, sizeof old_status); 850 screen_init(&c->status, c->tty.sx, 1, 0); 851 852 utf8flag = options_get_number(&s->options, "status-utf8"); 853 854 len = screen_write_strlen(utf8flag, "%s", c->prompt_string); 855 if (len > c->tty.sx) 856 len = c->tty.sx; 857 off = 0; 858 859 /* Change colours for command mode. */ 860 if (c->prompt_mdata.mode == 1) 861 style_apply(&gc, &s->options, "message-command-style"); 862 else 863 style_apply(&gc, &s->options, "message-style"); 864 865 screen_write_start(&ctx, NULL, &c->status); 866 867 screen_write_cursormove(&ctx, 0, 0); 868 screen_write_nputs(&ctx, len, &gc, utf8flag, "%s", c->prompt_string); 869 870 left = c->tty.sx - len; 871 if (left != 0) { 872 size = screen_write_strlen(utf8flag, "%s", c->prompt_buffer); 873 if (c->prompt_index >= left) { 874 off = c->prompt_index - left + 1; 875 if (c->prompt_index == size) 876 left--; 877 size = left; 878 } 879 screen_write_nputs( 880 &ctx, left, &gc, utf8flag, "%s", c->prompt_buffer + off); 881 882 for (i = len + size; i < c->tty.sx; i++) 883 screen_write_putc(&ctx, &gc, ' '); 884 } 885 886 screen_write_stop(&ctx); 887 888 /* Apply fake cursor. */ 889 off = len + c->prompt_index - off; 890 gcp = grid_view_get_cell(c->status.grid, off, 0); 891 gcp->attr ^= GRID_ATTR_REVERSE; 892 893 if (grid_compare(c->status.grid, old_status.grid) == 0) { 894 screen_free(&old_status); 895 return (0); 896 } 897 screen_free(&old_status); 898 return (1); 899 } 900 901 /* Handle keys in prompt. */ 902 void 903 status_prompt_key(struct client *c, int key) 904 { 905 struct session *sess = c->session; 906 struct options *oo = &sess->options; 907 struct paste_buffer *pb; 908 char *s, *first, *last, word[64], swapc; 909 const char *histstr; 910 const char *wsep = NULL; 911 u_char ch; 912 size_t size, n, off, idx; 913 914 size = strlen(c->prompt_buffer); 915 switch (mode_key_lookup(&c->prompt_mdata, key, NULL)) { 916 case MODEKEYEDIT_CURSORLEFT: 917 if (c->prompt_index > 0) { 918 c->prompt_index--; 919 c->flags |= CLIENT_STATUS; 920 } 921 break; 922 case MODEKEYEDIT_SWITCHMODE: 923 c->flags |= CLIENT_STATUS; 924 break; 925 case MODEKEYEDIT_SWITCHMODEAPPEND: 926 c->flags |= CLIENT_STATUS; 927 /* FALLTHROUGH */ 928 case MODEKEYEDIT_CURSORRIGHT: 929 if (c->prompt_index < size) { 930 c->prompt_index++; 931 c->flags |= CLIENT_STATUS; 932 } 933 break; 934 case MODEKEYEDIT_SWITCHMODEBEGINLINE: 935 c->flags |= CLIENT_STATUS; 936 /* FALLTHROUGH */ 937 case MODEKEYEDIT_STARTOFLINE: 938 if (c->prompt_index != 0) { 939 c->prompt_index = 0; 940 c->flags |= CLIENT_STATUS; 941 } 942 break; 943 case MODEKEYEDIT_SWITCHMODEAPPENDLINE: 944 c->flags |= CLIENT_STATUS; 945 /* FALLTHROUGH */ 946 case MODEKEYEDIT_ENDOFLINE: 947 if (c->prompt_index != size) { 948 c->prompt_index = size; 949 c->flags |= CLIENT_STATUS; 950 } 951 break; 952 case MODEKEYEDIT_COMPLETE: 953 if (*c->prompt_buffer == '\0') 954 break; 955 956 idx = c->prompt_index; 957 if (idx != 0) 958 idx--; 959 960 /* Find the word we are in. */ 961 first = c->prompt_buffer + idx; 962 while (first > c->prompt_buffer && *first != ' ') 963 first--; 964 while (*first == ' ') 965 first++; 966 last = c->prompt_buffer + idx; 967 while (*last != '\0' && *last != ' ') 968 last++; 969 while (*last == ' ') 970 last--; 971 if (*last != '\0') 972 last++; 973 if (last <= first || 974 ((size_t) (last - first)) > (sizeof word) - 1) 975 break; 976 memcpy(word, first, last - first); 977 word[last - first] = '\0'; 978 979 /* And try to complete it. */ 980 if ((s = status_prompt_complete(word)) == NULL) 981 break; 982 983 /* Trim out word. */ 984 n = size - (last - c->prompt_buffer) + 1; /* with \0 */ 985 memmove(first, last, n); 986 size -= last - first; 987 988 /* Insert the new word. */ 989 size += strlen(s); 990 off = first - c->prompt_buffer; 991 c->prompt_buffer = xrealloc(c->prompt_buffer, size + 1); 992 first = c->prompt_buffer + off; 993 memmove(first + strlen(s), first, n); 994 memcpy(first, s, strlen(s)); 995 996 c->prompt_index = (first - c->prompt_buffer) + strlen(s); 997 free(s); 998 999 c->flags |= CLIENT_STATUS; 1000 break; 1001 case MODEKEYEDIT_BACKSPACE: 1002 if (c->prompt_index != 0) { 1003 if (c->prompt_index == size) 1004 c->prompt_buffer[--c->prompt_index] = '\0'; 1005 else { 1006 memmove(c->prompt_buffer + c->prompt_index - 1, 1007 c->prompt_buffer + c->prompt_index, 1008 size + 1 - c->prompt_index); 1009 c->prompt_index--; 1010 } 1011 c->flags |= CLIENT_STATUS; 1012 } 1013 break; 1014 case MODEKEYEDIT_DELETE: 1015 case MODEKEYEDIT_SWITCHMODESUBSTITUTE: 1016 if (c->prompt_index != size) { 1017 memmove(c->prompt_buffer + c->prompt_index, 1018 c->prompt_buffer + c->prompt_index + 1, 1019 size + 1 - c->prompt_index); 1020 c->flags |= CLIENT_STATUS; 1021 } 1022 break; 1023 case MODEKEYEDIT_DELETELINE: 1024 case MODEKEYEDIT_SWITCHMODESUBSTITUTELINE: 1025 *c->prompt_buffer = '\0'; 1026 c->prompt_index = 0; 1027 c->flags |= CLIENT_STATUS; 1028 break; 1029 case MODEKEYEDIT_DELETETOENDOFLINE: 1030 case MODEKEYEDIT_SWITCHMODECHANGELINE: 1031 if (c->prompt_index < size) { 1032 c->prompt_buffer[c->prompt_index] = '\0'; 1033 c->flags |= CLIENT_STATUS; 1034 } 1035 break; 1036 case MODEKEYEDIT_DELETEWORD: 1037 wsep = options_get_string(oo, "word-separators"); 1038 idx = c->prompt_index; 1039 1040 /* Find a non-separator. */ 1041 while (idx != 0) { 1042 idx--; 1043 if (!strchr(wsep, c->prompt_buffer[idx])) 1044 break; 1045 } 1046 1047 /* Find the separator at the beginning of the word. */ 1048 while (idx != 0) { 1049 idx--; 1050 if (strchr(wsep, c->prompt_buffer[idx])) { 1051 /* Go back to the word. */ 1052 idx++; 1053 break; 1054 } 1055 } 1056 1057 memmove(c->prompt_buffer + idx, 1058 c->prompt_buffer + c->prompt_index, 1059 size + 1 - c->prompt_index); 1060 memset(c->prompt_buffer + size - (c->prompt_index - idx), 1061 '\0', c->prompt_index - idx); 1062 c->prompt_index = idx; 1063 c->flags |= CLIENT_STATUS; 1064 break; 1065 case MODEKEYEDIT_NEXTSPACE: 1066 wsep = " "; 1067 /* FALLTHROUGH */ 1068 case MODEKEYEDIT_NEXTWORD: 1069 if (wsep == NULL) 1070 wsep = options_get_string(oo, "word-separators"); 1071 1072 /* Find a separator. */ 1073 while (c->prompt_index != size) { 1074 c->prompt_index++; 1075 if (strchr(wsep, c->prompt_buffer[c->prompt_index])) 1076 break; 1077 } 1078 1079 /* Find the word right after the separation. */ 1080 while (c->prompt_index != size) { 1081 c->prompt_index++; 1082 if (!strchr(wsep, c->prompt_buffer[c->prompt_index])) 1083 break; 1084 } 1085 1086 c->flags |= CLIENT_STATUS; 1087 break; 1088 case MODEKEYEDIT_NEXTSPACEEND: 1089 wsep = " "; 1090 /* FALLTHROUGH */ 1091 case MODEKEYEDIT_NEXTWORDEND: 1092 if (wsep == NULL) 1093 wsep = options_get_string(oo, "word-separators"); 1094 1095 /* Find a word. */ 1096 while (c->prompt_index != size) { 1097 c->prompt_index++; 1098 if (!strchr(wsep, c->prompt_buffer[c->prompt_index])) 1099 break; 1100 } 1101 1102 /* Find the separator at the end of the word. */ 1103 while (c->prompt_index != size) { 1104 c->prompt_index++; 1105 if (strchr(wsep, c->prompt_buffer[c->prompt_index])) 1106 break; 1107 } 1108 1109 /* Back up to the end-of-word like vi. */ 1110 if (options_get_number(oo, "status-keys") == MODEKEY_VI && 1111 c->prompt_index != 0) 1112 c->prompt_index--; 1113 1114 c->flags |= CLIENT_STATUS; 1115 break; 1116 case MODEKEYEDIT_PREVIOUSSPACE: 1117 wsep = " "; 1118 /* FALLTHROUGH */ 1119 case MODEKEYEDIT_PREVIOUSWORD: 1120 if (wsep == NULL) 1121 wsep = options_get_string(oo, "word-separators"); 1122 1123 /* Find a non-separator. */ 1124 while (c->prompt_index != 0) { 1125 c->prompt_index--; 1126 if (!strchr(wsep, c->prompt_buffer[c->prompt_index])) 1127 break; 1128 } 1129 1130 /* Find the separator at the beginning of the word. */ 1131 while (c->prompt_index != 0) { 1132 c->prompt_index--; 1133 if (strchr(wsep, c->prompt_buffer[c->prompt_index])) { 1134 /* Go back to the word. */ 1135 c->prompt_index++; 1136 break; 1137 } 1138 } 1139 1140 c->flags |= CLIENT_STATUS; 1141 break; 1142 case MODEKEYEDIT_HISTORYUP: 1143 histstr = status_prompt_up_history(&c->prompt_hindex); 1144 if (histstr == NULL) 1145 break; 1146 free(c->prompt_buffer); 1147 c->prompt_buffer = xstrdup(histstr); 1148 c->prompt_index = strlen(c->prompt_buffer); 1149 c->flags |= CLIENT_STATUS; 1150 break; 1151 case MODEKEYEDIT_HISTORYDOWN: 1152 histstr = status_prompt_down_history(&c->prompt_hindex); 1153 if (histstr == NULL) 1154 break; 1155 free(c->prompt_buffer); 1156 c->prompt_buffer = xstrdup(histstr); 1157 c->prompt_index = strlen(c->prompt_buffer); 1158 c->flags |= CLIENT_STATUS; 1159 break; 1160 case MODEKEYEDIT_PASTE: 1161 if ((pb = paste_get_top()) == NULL) 1162 break; 1163 for (n = 0; n < pb->size; n++) { 1164 ch = (u_char) pb->data[n]; 1165 if (ch < 32 || ch == 127) 1166 break; 1167 } 1168 1169 c->prompt_buffer = xrealloc(c->prompt_buffer, size + n + 1); 1170 if (c->prompt_index == size) { 1171 memcpy(c->prompt_buffer + c->prompt_index, pb->data, n); 1172 c->prompt_index += n; 1173 c->prompt_buffer[c->prompt_index] = '\0'; 1174 } else { 1175 memmove(c->prompt_buffer + c->prompt_index + n, 1176 c->prompt_buffer + c->prompt_index, 1177 size + 1 - c->prompt_index); 1178 memcpy(c->prompt_buffer + c->prompt_index, pb->data, n); 1179 c->prompt_index += n; 1180 } 1181 1182 c->flags |= CLIENT_STATUS; 1183 break; 1184 case MODEKEYEDIT_TRANSPOSECHARS: 1185 idx = c->prompt_index; 1186 if (idx < size) 1187 idx++; 1188 if (idx >= 2) { 1189 swapc = c->prompt_buffer[idx - 2]; 1190 c->prompt_buffer[idx - 2] = c->prompt_buffer[idx - 1]; 1191 c->prompt_buffer[idx - 1] = swapc; 1192 c->prompt_index = idx; 1193 c->flags |= CLIENT_STATUS; 1194 } 1195 break; 1196 case MODEKEYEDIT_ENTER: 1197 if (*c->prompt_buffer != '\0') 1198 status_prompt_add_history(c->prompt_buffer); 1199 if (c->prompt_callbackfn(c->prompt_data, c->prompt_buffer) == 0) 1200 status_prompt_clear(c); 1201 break; 1202 case MODEKEYEDIT_CANCEL: 1203 if (c->prompt_callbackfn(c->prompt_data, NULL) == 0) 1204 status_prompt_clear(c); 1205 break; 1206 case MODEKEY_OTHER: 1207 if ((key & 0xff00) != 0 || key < 32 || key == 127) 1208 break; 1209 c->prompt_buffer = xrealloc(c->prompt_buffer, size + 2); 1210 1211 if (c->prompt_index == size) { 1212 c->prompt_buffer[c->prompt_index++] = key; 1213 c->prompt_buffer[c->prompt_index] = '\0'; 1214 } else { 1215 memmove(c->prompt_buffer + c->prompt_index + 1, 1216 c->prompt_buffer + c->prompt_index, 1217 size + 1 - c->prompt_index); 1218 c->prompt_buffer[c->prompt_index++] = key; 1219 } 1220 1221 if (c->prompt_flags & PROMPT_SINGLE) { 1222 if (c->prompt_callbackfn( 1223 c->prompt_data, c->prompt_buffer) == 0) 1224 status_prompt_clear(c); 1225 } 1226 1227 c->flags |= CLIENT_STATUS; 1228 break; 1229 default: 1230 break; 1231 } 1232 } 1233 1234 /* Get previous line from the history. */ 1235 const char * 1236 status_prompt_up_history(u_int *idx) 1237 { 1238 u_int size; 1239 1240 /* 1241 * History runs from 0 to size - 1. 1242 * 1243 * Index is from 0 to size. Zero is empty. 1244 */ 1245 1246 size = ARRAY_LENGTH(&status_prompt_history); 1247 if (size == 0 || *idx == size) 1248 return (NULL); 1249 (*idx)++; 1250 return (ARRAY_ITEM(&status_prompt_history, size - *idx)); 1251 } 1252 1253 /* Get next line from the history. */ 1254 const char * 1255 status_prompt_down_history(u_int *idx) 1256 { 1257 u_int size; 1258 1259 size = ARRAY_LENGTH(&status_prompt_history); 1260 if (size == 0 || *idx == 0) 1261 return (""); 1262 (*idx)--; 1263 if (*idx == 0) 1264 return (""); 1265 return (ARRAY_ITEM(&status_prompt_history, size - *idx)); 1266 } 1267 1268 /* Add line to the history. */ 1269 void 1270 status_prompt_add_history(const char *line) 1271 { 1272 u_int size; 1273 1274 size = ARRAY_LENGTH(&status_prompt_history); 1275 if (size > 0 && strcmp(ARRAY_LAST(&status_prompt_history), line) == 0) 1276 return; 1277 1278 if (size == PROMPT_HISTORY) { 1279 free(ARRAY_FIRST(&status_prompt_history)); 1280 ARRAY_REMOVE(&status_prompt_history, 0); 1281 } 1282 1283 ARRAY_ADD(&status_prompt_history, xstrdup(line)); 1284 } 1285 1286 /* Complete word. */ 1287 char * 1288 status_prompt_complete(const char *s) 1289 { 1290 const struct cmd_entry **cmdent; 1291 const struct options_table_entry *oe; 1292 ARRAY_DECL(, const char *) list; 1293 char *prefix, *s2; 1294 u_int i; 1295 size_t j; 1296 1297 if (*s == '\0') 1298 return (NULL); 1299 1300 /* First, build a list of all the possible matches. */ 1301 ARRAY_INIT(&list); 1302 for (cmdent = cmd_table; *cmdent != NULL; cmdent++) { 1303 if (strncmp((*cmdent)->name, s, strlen(s)) == 0) 1304 ARRAY_ADD(&list, (*cmdent)->name); 1305 } 1306 for (oe = server_options_table; oe->name != NULL; oe++) { 1307 if (strncmp(oe->name, s, strlen(s)) == 0) 1308 ARRAY_ADD(&list, oe->name); 1309 } 1310 for (oe = session_options_table; oe->name != NULL; oe++) { 1311 if (strncmp(oe->name, s, strlen(s)) == 0) 1312 ARRAY_ADD(&list, oe->name); 1313 } 1314 for (oe = window_options_table; oe->name != NULL; oe++) { 1315 if (strncmp(oe->name, s, strlen(s)) == 0) 1316 ARRAY_ADD(&list, oe->name); 1317 } 1318 1319 /* If none, bail now. */ 1320 if (ARRAY_LENGTH(&list) == 0) { 1321 ARRAY_FREE(&list); 1322 return (NULL); 1323 } 1324 1325 /* If an exact match, return it, with a trailing space. */ 1326 if (ARRAY_LENGTH(&list) == 1) { 1327 xasprintf(&s2, "%s ", ARRAY_FIRST(&list)); 1328 ARRAY_FREE(&list); 1329 return (s2); 1330 } 1331 1332 /* Now loop through the list and find the longest common prefix. */ 1333 prefix = xstrdup(ARRAY_FIRST(&list)); 1334 for (i = 1; i < ARRAY_LENGTH(&list); i++) { 1335 s = ARRAY_ITEM(&list, i); 1336 1337 j = strlen(s); 1338 if (j > strlen(prefix)) 1339 j = strlen(prefix); 1340 for (; j > 0; j--) { 1341 if (prefix[j - 1] != s[j - 1]) 1342 prefix[j - 1] = '\0'; 1343 } 1344 } 1345 1346 ARRAY_FREE(&list); 1347 return (prefix); 1348 } 1349