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