status.c revision 1.114
1/* $OpenBSD: status.c,v 1.114 2014/04/24 09:14:43 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 default: 400 xsnprintf(tmp, sizeof tmp, "#%c", *(*iptr - 1)); 401 ptr = tmp; 402 goto do_replace; 403 } 404 405 return; 406 407do_replace: 408 ptrlen = strlen(ptr); 409 if ((size_t) limit < ptrlen) 410 ptrlen = limit; 411 412 if (*optr + ptrlen >= out + outsize - 1) 413 return; 414 while (ptrlen > 0 && *ptr != '\0') { 415 *(*optr)++ = *ptr++; 416 ptrlen--; 417 } 418 419 return; 420 421skip_to: 422 *(*optr)++ = '#'; 423 424 (*iptr)--; /* include ch */ 425 while (**iptr != ch && **iptr != '\0') { 426 if (*optr >= out + outsize - 1) 427 break; 428 *(*optr)++ = *(*iptr)++; 429 } 430} 431 432/* Replace special sequences in fmt. */ 433char * 434status_replace(struct client *c, struct session *s, struct winlink *wl, 435 struct window_pane *wp, const char *fmt, time_t t, int jobsflag) 436{ 437 static char out[BUFSIZ]; 438 char in[BUFSIZ], ch, *iptr, *optr, *expanded; 439 size_t len; 440 struct format_tree *ft; 441 442 if (fmt == NULL) 443 return (xstrdup("")); 444 445 if (s == NULL && c != NULL) 446 s = c->session; 447 if (wl == NULL && s != NULL) 448 wl = s->curw; 449 if (wp == NULL && wl != NULL) 450 wp = wl->window->active; 451 452 len = strftime(in, sizeof in, fmt, localtime(&t)); 453 in[len] = '\0'; 454 455 iptr = in; 456 optr = out; 457 458 while (*iptr != '\0') { 459 if (optr >= out + (sizeof out) - 1) 460 break; 461 ch = *iptr++; 462 463 if (ch != '#' || *iptr == '\0') { 464 *optr++ = ch; 465 continue; 466 } 467 status_replace1(c, &iptr, &optr, out, sizeof out, jobsflag); 468 } 469 *optr = '\0'; 470 471 ft = format_create(); 472 if (c != NULL) 473 format_client(ft, c); 474 if (s != NULL) 475 format_session(ft, s); 476 if (s != NULL && wl != NULL) 477 format_winlink(ft, s, wl); 478 if (wp != NULL) 479 format_window_pane(ft, wp); 480 expanded = format_expand(ft, out); 481 format_free(ft); 482 return (expanded); 483} 484 485/* Figure out job name and get its result, starting it off if necessary. */ 486char * 487status_find_job(struct client *c, char **iptr) 488{ 489 struct status_out *so, so_find; 490 char *cmd; 491 int lastesc; 492 size_t len; 493 494 if (**iptr == '\0') 495 return (NULL); 496 if (**iptr == ')') { /* no command given */ 497 (*iptr)++; 498 return (NULL); 499 } 500 501 cmd = xmalloc(strlen(*iptr) + 1); 502 len = 0; 503 504 lastesc = 0; 505 for (; **iptr != '\0'; (*iptr)++) { 506 if (!lastesc && **iptr == ')') 507 break; /* unescaped ) is the end */ 508 if (!lastesc && **iptr == '\\') { 509 lastesc = 1; 510 continue; /* skip \ if not escaped */ 511 } 512 lastesc = 0; 513 cmd[len++] = **iptr; 514 } 515 if (**iptr == '\0') /* no terminating ) */ { 516 free(cmd); 517 return (NULL); 518 } 519 (*iptr)++; /* skip final ) */ 520 cmd[len] = '\0'; 521 522 /* First try in the new tree. */ 523 so_find.cmd = cmd; 524 so = RB_FIND(status_out_tree, &c->status_new, &so_find); 525 if (so != NULL && so->out != NULL) { 526 free(cmd); 527 return (so->out); 528 } 529 530 /* If not found at all, start the job and add to the tree. */ 531 if (so == NULL) { 532 job_run(cmd, NULL, status_job_callback, status_job_free, c); 533 c->references++; 534 535 so = xmalloc(sizeof *so); 536 so->cmd = xstrdup(cmd); 537 so->out = NULL; 538 RB_INSERT(status_out_tree, &c->status_new, so); 539 } 540 541 /* Lookup in the old tree. */ 542 so_find.cmd = cmd; 543 so = RB_FIND(status_out_tree, &c->status_old, &so_find); 544 free(cmd); 545 if (so != NULL) 546 return (so->out); 547 return (NULL); 548} 549 550/* Free job tree. */ 551void 552status_free_jobs(struct status_out_tree *sotree) 553{ 554 struct status_out *so, *so_next; 555 556 so_next = RB_MIN(status_out_tree, sotree); 557 while (so_next != NULL) { 558 so = so_next; 559 so_next = RB_NEXT(status_out_tree, sotree, so); 560 561 RB_REMOVE(status_out_tree, sotree, so); 562 free(so->out); 563 free(so->cmd); 564 free(so); 565 } 566} 567 568/* Update jobs on status interval. */ 569void 570status_update_jobs(struct client *c) 571{ 572 /* Free the old tree. */ 573 status_free_jobs(&c->status_old); 574 575 /* Move the new to old. */ 576 memcpy(&c->status_old, &c->status_new, sizeof c->status_old); 577 RB_INIT(&c->status_new); 578} 579 580/* Free status job. */ 581void 582status_job_free(void *data) 583{ 584 struct client *c = data; 585 586 c->references--; 587} 588 589/* Job has finished: save its result. */ 590void 591status_job_callback(struct job *job) 592{ 593 struct client *c = job->data; 594 struct status_out *so, so_find; 595 char *line, *buf; 596 size_t len; 597 598 if (c->flags & CLIENT_DEAD) 599 return; 600 601 so_find.cmd = job->cmd; 602 so = RB_FIND(status_out_tree, &c->status_new, &so_find); 603 if (so == NULL || so->out != NULL) 604 return; 605 606 buf = NULL; 607 if ((line = evbuffer_readline(job->event->input)) == NULL) { 608 len = EVBUFFER_LENGTH(job->event->input); 609 buf = xmalloc(len + 1); 610 if (len != 0) 611 memcpy(buf, EVBUFFER_DATA(job->event->input), len); 612 buf[len] = '\0'; 613 } else 614 buf = line; 615 616 so->out = buf; 617 server_status_client(c); 618} 619 620/* Return winlink status line entry and adjust gc as necessary. */ 621char * 622status_print( 623 struct client *c, struct winlink *wl, time_t t, struct grid_cell *gc) 624{ 625 struct options *oo = &wl->window->options; 626 struct session *s = c->session; 627 const char *fmt; 628 char *text; 629 630 style_apply_update(gc, oo, "window-status-style"); 631 fmt = options_get_string(oo, "window-status-format"); 632 if (wl == s->curw) { 633 style_apply_update(gc, oo, "window-status-current-style"); 634 fmt = options_get_string(oo, "window-status-current-format"); 635 } 636 if (wl == TAILQ_FIRST(&s->lastw)) 637 style_apply_update(gc, oo, "window-status-last-style"); 638 639 if (wl->flags & WINLINK_BELL) 640 style_apply_update(gc, oo, "window-status-bell-style"); 641 else if (wl->flags & (WINLINK_ACTIVITY|WINLINK_SILENCE)) 642 style_apply_update(gc, oo, "window-status-activity-style"); 643 644 text = status_replace(c, NULL, wl, NULL, fmt, t, 1); 645 return (text); 646} 647 648/* Set a status line message. */ 649void printflike2 650status_message_set(struct client *c, const char *fmt, ...) 651{ 652 struct timeval tv; 653 struct message_entry *msg; 654 va_list ap; 655 int delay; 656 u_int i, limit; 657 658 status_prompt_clear(c); 659 status_message_clear(c); 660 661 va_start(ap, fmt); 662 xvasprintf(&c->message_string, fmt, ap); 663 va_end(ap); 664 665 ARRAY_EXPAND(&c->message_log, 1); 666 msg = &ARRAY_LAST(&c->message_log); 667 msg->msg_time = time(NULL); 668 msg->msg = xstrdup(c->message_string); 669 670 limit = options_get_number(&global_options, "message-limit"); 671 if (ARRAY_LENGTH(&c->message_log) > limit) { 672 limit = ARRAY_LENGTH(&c->message_log) - limit; 673 for (i = 0; i < limit; i++) { 674 msg = &ARRAY_FIRST(&c->message_log); 675 free(msg->msg); 676 ARRAY_REMOVE(&c->message_log, 0); 677 } 678 } 679 680 delay = options_get_number(&c->session->options, "display-time"); 681 tv.tv_sec = delay / 1000; 682 tv.tv_usec = (delay % 1000) * 1000L; 683 684 if (event_initialized(&c->message_timer)) 685 evtimer_del(&c->message_timer); 686 evtimer_set(&c->message_timer, status_message_callback, c); 687 evtimer_add(&c->message_timer, &tv); 688 689 c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE); 690 c->flags |= CLIENT_STATUS; 691} 692 693/* Clear status line message. */ 694void 695status_message_clear(struct client *c) 696{ 697 if (c->message_string == NULL) 698 return; 699 700 free(c->message_string); 701 c->message_string = NULL; 702 703 c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE); 704 c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */ 705 706 screen_reinit(&c->status); 707} 708 709/* Clear status line message after timer expires. */ 710void 711status_message_callback(unused int fd, unused short event, void *data) 712{ 713 struct client *c = data; 714 715 status_message_clear(c); 716} 717 718/* Draw client message on status line of present else on last line. */ 719int 720status_message_redraw(struct client *c) 721{ 722 struct screen_write_ctx ctx; 723 struct session *s = c->session; 724 struct screen old_status; 725 size_t len; 726 struct grid_cell gc; 727 int utf8flag; 728 729 if (c->tty.sx == 0 || c->tty.sy == 0) 730 return (0); 731 memcpy(&old_status, &c->status, sizeof old_status); 732 screen_init(&c->status, c->tty.sx, 1, 0); 733 734 utf8flag = options_get_number(&s->options, "status-utf8"); 735 736 len = screen_write_strlen(utf8flag, "%s", c->message_string); 737 if (len > c->tty.sx) 738 len = c->tty.sx; 739 740 style_apply(&gc, &s->options, "message-style"); 741 742 screen_write_start(&ctx, NULL, &c->status); 743 744 screen_write_cursormove(&ctx, 0, 0); 745 screen_write_nputs(&ctx, len, &gc, utf8flag, "%s", c->message_string); 746 for (; len < c->tty.sx; len++) 747 screen_write_putc(&ctx, &gc, ' '); 748 749 screen_write_stop(&ctx); 750 751 if (grid_compare(c->status.grid, old_status.grid) == 0) { 752 screen_free(&old_status); 753 return (0); 754 } 755 screen_free(&old_status); 756 return (1); 757} 758 759/* Enable status line prompt. */ 760void 761status_prompt_set(struct client *c, const char *msg, const char *input, 762 int (*callbackfn)(void *, const char *), void (*freefn)(void *), 763 void *data, int flags) 764{ 765 int keys; 766 767 status_message_clear(c); 768 status_prompt_clear(c); 769 770 c->prompt_string = status_replace(c, NULL, NULL, NULL, msg, 771 time(NULL), 0); 772 773 c->prompt_buffer = status_replace(c, NULL, NULL, NULL, input, 774 time(NULL), 0); 775 c->prompt_index = strlen(c->prompt_buffer); 776 777 c->prompt_callbackfn = callbackfn; 778 c->prompt_freefn = freefn; 779 c->prompt_data = data; 780 781 c->prompt_hindex = 0; 782 783 c->prompt_flags = flags; 784 785 keys = options_get_number(&c->session->options, "status-keys"); 786 if (keys == MODEKEY_EMACS) 787 mode_key_init(&c->prompt_mdata, &mode_key_tree_emacs_edit); 788 else 789 mode_key_init(&c->prompt_mdata, &mode_key_tree_vi_edit); 790 791 c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE); 792 c->flags |= CLIENT_STATUS; 793} 794 795/* Remove status line prompt. */ 796void 797status_prompt_clear(struct client *c) 798{ 799 if (c->prompt_string == NULL) 800 return; 801 802 if (c->prompt_freefn != NULL && c->prompt_data != NULL) 803 c->prompt_freefn(c->prompt_data); 804 805 free(c->prompt_string); 806 c->prompt_string = NULL; 807 808 free(c->prompt_buffer); 809 c->prompt_buffer = NULL; 810 811 c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE); 812 c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */ 813 814 screen_reinit(&c->status); 815} 816 817/* Update status line prompt with a new prompt string. */ 818void 819status_prompt_update(struct client *c, const char *msg, const char *input) 820{ 821 free(c->prompt_string); 822 c->prompt_string = status_replace(c, NULL, NULL, NULL, msg, 823 time(NULL), 0); 824 825 free(c->prompt_buffer); 826 c->prompt_buffer = status_replace(c, NULL, NULL, NULL, input, 827 time(NULL), 0); 828 c->prompt_index = strlen(c->prompt_buffer); 829 830 c->prompt_hindex = 0; 831 832 c->flags |= CLIENT_STATUS; 833} 834 835/* Draw client prompt on status line of present else on last line. */ 836int 837status_prompt_redraw(struct client *c) 838{ 839 struct screen_write_ctx ctx; 840 struct session *s = c->session; 841 struct screen old_status; 842 size_t i, size, left, len, off; 843 struct grid_cell gc, *gcp; 844 int utf8flag; 845 846 if (c->tty.sx == 0 || c->tty.sy == 0) 847 return (0); 848 memcpy(&old_status, &c->status, sizeof old_status); 849 screen_init(&c->status, c->tty.sx, 1, 0); 850 851 utf8flag = options_get_number(&s->options, "status-utf8"); 852 853 len = screen_write_strlen(utf8flag, "%s", c->prompt_string); 854 if (len > c->tty.sx) 855 len = c->tty.sx; 856 off = 0; 857 858 /* Change colours for command mode. */ 859 if (c->prompt_mdata.mode == 1) 860 style_apply(&gc, &s->options, "message-command-style"); 861 else 862 style_apply(&gc, &s->options, "message-style"); 863 864 screen_write_start(&ctx, NULL, &c->status); 865 866 screen_write_cursormove(&ctx, 0, 0); 867 screen_write_nputs(&ctx, len, &gc, utf8flag, "%s", c->prompt_string); 868 869 left = c->tty.sx - len; 870 if (left != 0) { 871 size = screen_write_strlen(utf8flag, "%s", c->prompt_buffer); 872 if (c->prompt_index >= left) { 873 off = c->prompt_index - left + 1; 874 if (c->prompt_index == size) 875 left--; 876 size = left; 877 } 878 screen_write_nputs( 879 &ctx, left, &gc, utf8flag, "%s", c->prompt_buffer + off); 880 881 for (i = len + size; i < c->tty.sx; i++) 882 screen_write_putc(&ctx, &gc, ' '); 883 } 884 885 screen_write_stop(&ctx); 886 887 /* Apply fake cursor. */ 888 off = len + c->prompt_index - off; 889 gcp = grid_view_get_cell(c->status.grid, off, 0); 890 gcp->attr ^= GRID_ATTR_REVERSE; 891 892 if (grid_compare(c->status.grid, old_status.grid) == 0) { 893 screen_free(&old_status); 894 return (0); 895 } 896 screen_free(&old_status); 897 return (1); 898} 899 900/* Handle keys in prompt. */ 901void 902status_prompt_key(struct client *c, int key) 903{ 904 struct session *sess = c->session; 905 struct options *oo = &sess->options; 906 struct paste_buffer *pb; 907 char *s, *first, *last, word[64], swapc; 908 const char *histstr; 909 const char *wsep = NULL; 910 u_char ch; 911 size_t size, n, off, idx; 912 913 size = strlen(c->prompt_buffer); 914 switch (mode_key_lookup(&c->prompt_mdata, key, NULL)) { 915 case MODEKEYEDIT_CURSORLEFT: 916 if (c->prompt_index > 0) { 917 c->prompt_index--; 918 c->flags |= CLIENT_STATUS; 919 } 920 break; 921 case MODEKEYEDIT_SWITCHMODE: 922 c->flags |= CLIENT_STATUS; 923 break; 924 case MODEKEYEDIT_SWITCHMODEAPPEND: 925 c->flags |= CLIENT_STATUS; 926 /* FALLTHROUGH */ 927 case MODEKEYEDIT_CURSORRIGHT: 928 if (c->prompt_index < size) { 929 c->prompt_index++; 930 c->flags |= CLIENT_STATUS; 931 } 932 break; 933 case MODEKEYEDIT_SWITCHMODEBEGINLINE: 934 c->flags |= CLIENT_STATUS; 935 /* FALLTHROUGH */ 936 case MODEKEYEDIT_STARTOFLINE: 937 if (c->prompt_index != 0) { 938 c->prompt_index = 0; 939 c->flags |= CLIENT_STATUS; 940 } 941 break; 942 case MODEKEYEDIT_SWITCHMODEAPPENDLINE: 943 c->flags |= CLIENT_STATUS; 944 /* FALLTHROUGH */ 945 case MODEKEYEDIT_ENDOFLINE: 946 if (c->prompt_index != size) { 947 c->prompt_index = size; 948 c->flags |= CLIENT_STATUS; 949 } 950 break; 951 case MODEKEYEDIT_COMPLETE: 952 if (*c->prompt_buffer == '\0') 953 break; 954 955 idx = c->prompt_index; 956 if (idx != 0) 957 idx--; 958 959 /* Find the word we are in. */ 960 first = c->prompt_buffer + idx; 961 while (first > c->prompt_buffer && *first != ' ') 962 first--; 963 while (*first == ' ') 964 first++; 965 last = c->prompt_buffer + idx; 966 while (*last != '\0' && *last != ' ') 967 last++; 968 while (*last == ' ') 969 last--; 970 if (*last != '\0') 971 last++; 972 if (last <= first || 973 ((size_t) (last - first)) > (sizeof word) - 1) 974 break; 975 memcpy(word, first, last - first); 976 word[last - first] = '\0'; 977 978 /* And try to complete it. */ 979 if ((s = status_prompt_complete(word)) == NULL) 980 break; 981 982 /* Trim out word. */ 983 n = size - (last - c->prompt_buffer) + 1; /* with \0 */ 984 memmove(first, last, n); 985 size -= last - first; 986 987 /* Insert the new word. */ 988 size += strlen(s); 989 off = first - c->prompt_buffer; 990 c->prompt_buffer = xrealloc(c->prompt_buffer, 1, size + 1); 991 first = c->prompt_buffer + off; 992 memmove(first + strlen(s), first, n); 993 memcpy(first, s, strlen(s)); 994 995 c->prompt_index = (first - c->prompt_buffer) + strlen(s); 996 free(s); 997 998 c->flags |= CLIENT_STATUS; 999 break; 1000 case MODEKEYEDIT_BACKSPACE: 1001 if (c->prompt_index != 0) { 1002 if (c->prompt_index == size) 1003 c->prompt_buffer[--c->prompt_index] = '\0'; 1004 else { 1005 memmove(c->prompt_buffer + c->prompt_index - 1, 1006 c->prompt_buffer + c->prompt_index, 1007 size + 1 - c->prompt_index); 1008 c->prompt_index--; 1009 } 1010 c->flags |= CLIENT_STATUS; 1011 } 1012 break; 1013 case MODEKEYEDIT_DELETE: 1014 case MODEKEYEDIT_SWITCHMODESUBSTITUTE: 1015 if (c->prompt_index != size) { 1016 memmove(c->prompt_buffer + c->prompt_index, 1017 c->prompt_buffer + c->prompt_index + 1, 1018 size + 1 - c->prompt_index); 1019 c->flags |= CLIENT_STATUS; 1020 } 1021 break; 1022 case MODEKEYEDIT_DELETELINE: 1023 case MODEKEYEDIT_SWITCHMODESUBSTITUTELINE: 1024 *c->prompt_buffer = '\0'; 1025 c->prompt_index = 0; 1026 c->flags |= CLIENT_STATUS; 1027 break; 1028 case MODEKEYEDIT_DELETETOENDOFLINE: 1029 case MODEKEYEDIT_SWITCHMODECHANGELINE: 1030 if (c->prompt_index < size) { 1031 c->prompt_buffer[c->prompt_index] = '\0'; 1032 c->flags |= CLIENT_STATUS; 1033 } 1034 break; 1035 case MODEKEYEDIT_DELETEWORD: 1036 wsep = options_get_string(oo, "word-separators"); 1037 idx = c->prompt_index; 1038 1039 /* Find a non-separator. */ 1040 while (idx != 0) { 1041 idx--; 1042 if (!strchr(wsep, c->prompt_buffer[idx])) 1043 break; 1044 } 1045 1046 /* Find the separator at the beginning of the word. */ 1047 while (idx != 0) { 1048 idx--; 1049 if (strchr(wsep, c->prompt_buffer[idx])) { 1050 /* Go back to the word. */ 1051 idx++; 1052 break; 1053 } 1054 } 1055 1056 memmove(c->prompt_buffer + idx, 1057 c->prompt_buffer + c->prompt_index, 1058 size + 1 - c->prompt_index); 1059 memset(c->prompt_buffer + size - (c->prompt_index - idx), 1060 '\0', c->prompt_index - idx); 1061 c->prompt_index = idx; 1062 c->flags |= CLIENT_STATUS; 1063 break; 1064 case MODEKEYEDIT_NEXTSPACE: 1065 wsep = " "; 1066 /* FALLTHROUGH */ 1067 case MODEKEYEDIT_NEXTWORD: 1068 if (wsep == NULL) 1069 wsep = options_get_string(oo, "word-separators"); 1070 1071 /* Find a separator. */ 1072 while (c->prompt_index != size) { 1073 c->prompt_index++; 1074 if (strchr(wsep, c->prompt_buffer[c->prompt_index])) 1075 break; 1076 } 1077 1078 /* Find the word right after the separation. */ 1079 while (c->prompt_index != size) { 1080 c->prompt_index++; 1081 if (!strchr(wsep, c->prompt_buffer[c->prompt_index])) 1082 break; 1083 } 1084 1085 c->flags |= CLIENT_STATUS; 1086 break; 1087 case MODEKEYEDIT_NEXTSPACEEND: 1088 wsep = " "; 1089 /* FALLTHROUGH */ 1090 case MODEKEYEDIT_NEXTWORDEND: 1091 if (wsep == NULL) 1092 wsep = options_get_string(oo, "word-separators"); 1093 1094 /* Find a word. */ 1095 while (c->prompt_index != size) { 1096 c->prompt_index++; 1097 if (!strchr(wsep, c->prompt_buffer[c->prompt_index])) 1098 break; 1099 } 1100 1101 /* Find the separator at the end of the word. */ 1102 while (c->prompt_index != size) { 1103 c->prompt_index++; 1104 if (strchr(wsep, c->prompt_buffer[c->prompt_index])) 1105 break; 1106 } 1107 1108 /* Back up to the end-of-word like vi. */ 1109 if (options_get_number(oo, "status-keys") == MODEKEY_VI && 1110 c->prompt_index != 0) 1111 c->prompt_index--; 1112 1113 c->flags |= CLIENT_STATUS; 1114 break; 1115 case MODEKEYEDIT_PREVIOUSSPACE: 1116 wsep = " "; 1117 /* FALLTHROUGH */ 1118 case MODEKEYEDIT_PREVIOUSWORD: 1119 if (wsep == NULL) 1120 wsep = options_get_string(oo, "word-separators"); 1121 1122 /* Find a non-separator. */ 1123 while (c->prompt_index != 0) { 1124 c->prompt_index--; 1125 if (!strchr(wsep, c->prompt_buffer[c->prompt_index])) 1126 break; 1127 } 1128 1129 /* Find the separator at the beginning of the word. */ 1130 while (c->prompt_index != 0) { 1131 c->prompt_index--; 1132 if (strchr(wsep, c->prompt_buffer[c->prompt_index])) { 1133 /* Go back to the word. */ 1134 c->prompt_index++; 1135 break; 1136 } 1137 } 1138 1139 c->flags |= CLIENT_STATUS; 1140 break; 1141 case MODEKEYEDIT_HISTORYUP: 1142 histstr = status_prompt_up_history(&c->prompt_hindex); 1143 if (histstr == NULL) 1144 break; 1145 free(c->prompt_buffer); 1146 c->prompt_buffer = xstrdup(histstr); 1147 c->prompt_index = strlen(c->prompt_buffer); 1148 c->flags |= CLIENT_STATUS; 1149 break; 1150 case MODEKEYEDIT_HISTORYDOWN: 1151 histstr = status_prompt_down_history(&c->prompt_hindex); 1152 if (histstr == NULL) 1153 break; 1154 free(c->prompt_buffer); 1155 c->prompt_buffer = xstrdup(histstr); 1156 c->prompt_index = strlen(c->prompt_buffer); 1157 c->flags |= CLIENT_STATUS; 1158 break; 1159 case MODEKEYEDIT_PASTE: 1160 if ((pb = paste_get_top()) == NULL) 1161 break; 1162 for (n = 0; n < pb->size; n++) { 1163 ch = (u_char) pb->data[n]; 1164 if (ch < 32 || ch == 127) 1165 break; 1166 } 1167 1168 c->prompt_buffer = xrealloc(c->prompt_buffer, 1, size + n + 1); 1169 if (c->prompt_index == size) { 1170 memcpy(c->prompt_buffer + c->prompt_index, pb->data, n); 1171 c->prompt_index += n; 1172 c->prompt_buffer[c->prompt_index] = '\0'; 1173 } else { 1174 memmove(c->prompt_buffer + c->prompt_index + n, 1175 c->prompt_buffer + c->prompt_index, 1176 size + 1 - c->prompt_index); 1177 memcpy(c->prompt_buffer + c->prompt_index, pb->data, n); 1178 c->prompt_index += n; 1179 } 1180 1181 c->flags |= CLIENT_STATUS; 1182 break; 1183 case MODEKEYEDIT_TRANSPOSECHARS: 1184 idx = c->prompt_index; 1185 if (idx < size) 1186 idx++; 1187 if (idx >= 2) { 1188 swapc = c->prompt_buffer[idx - 2]; 1189 c->prompt_buffer[idx - 2] = c->prompt_buffer[idx - 1]; 1190 c->prompt_buffer[idx - 1] = swapc; 1191 c->prompt_index = idx; 1192 c->flags |= CLIENT_STATUS; 1193 } 1194 break; 1195 case MODEKEYEDIT_ENTER: 1196 if (*c->prompt_buffer != '\0') 1197 status_prompt_add_history(c->prompt_buffer); 1198 if (c->prompt_callbackfn(c->prompt_data, c->prompt_buffer) == 0) 1199 status_prompt_clear(c); 1200 break; 1201 case MODEKEYEDIT_CANCEL: 1202 if (c->prompt_callbackfn(c->prompt_data, NULL) == 0) 1203 status_prompt_clear(c); 1204 break; 1205 case MODEKEY_OTHER: 1206 if ((key & 0xff00) != 0 || key < 32 || key == 127) 1207 break; 1208 c->prompt_buffer = xrealloc(c->prompt_buffer, 1, size + 2); 1209 1210 if (c->prompt_index == size) { 1211 c->prompt_buffer[c->prompt_index++] = key; 1212 c->prompt_buffer[c->prompt_index] = '\0'; 1213 } else { 1214 memmove(c->prompt_buffer + c->prompt_index + 1, 1215 c->prompt_buffer + c->prompt_index, 1216 size + 1 - c->prompt_index); 1217 c->prompt_buffer[c->prompt_index++] = key; 1218 } 1219 1220 if (c->prompt_flags & PROMPT_SINGLE) { 1221 if (c->prompt_callbackfn( 1222 c->prompt_data, c->prompt_buffer) == 0) 1223 status_prompt_clear(c); 1224 } 1225 1226 c->flags |= CLIENT_STATUS; 1227 break; 1228 default: 1229 break; 1230 } 1231} 1232 1233/* Get previous line from the history. */ 1234const char * 1235status_prompt_up_history(u_int *idx) 1236{ 1237 u_int size; 1238 1239 /* 1240 * History runs from 0 to size - 1. 1241 * 1242 * Index is from 0 to size. Zero is empty. 1243 */ 1244 1245 size = ARRAY_LENGTH(&status_prompt_history); 1246 if (size == 0 || *idx == size) 1247 return (NULL); 1248 (*idx)++; 1249 return (ARRAY_ITEM(&status_prompt_history, size - *idx)); 1250} 1251 1252/* Get next line from the history. */ 1253const char * 1254status_prompt_down_history(u_int *idx) 1255{ 1256 u_int size; 1257 1258 size = ARRAY_LENGTH(&status_prompt_history); 1259 if (size == 0 || *idx == 0) 1260 return (""); 1261 (*idx)--; 1262 if (*idx == 0) 1263 return (""); 1264 return (ARRAY_ITEM(&status_prompt_history, size - *idx)); 1265} 1266 1267/* Add line to the history. */ 1268void 1269status_prompt_add_history(const char *line) 1270{ 1271 u_int size; 1272 1273 size = ARRAY_LENGTH(&status_prompt_history); 1274 if (size > 0 && strcmp(ARRAY_LAST(&status_prompt_history), line) == 0) 1275 return; 1276 1277 if (size == PROMPT_HISTORY) { 1278 free(ARRAY_FIRST(&status_prompt_history)); 1279 ARRAY_REMOVE(&status_prompt_history, 0); 1280 } 1281 1282 ARRAY_ADD(&status_prompt_history, xstrdup(line)); 1283} 1284 1285/* Complete word. */ 1286char * 1287status_prompt_complete(const char *s) 1288{ 1289 const struct cmd_entry **cmdent; 1290 const struct options_table_entry *oe; 1291 ARRAY_DECL(, const char *) list; 1292 char *prefix, *s2; 1293 u_int i; 1294 size_t j; 1295 1296 if (*s == '\0') 1297 return (NULL); 1298 1299 /* First, build a list of all the possible matches. */ 1300 ARRAY_INIT(&list); 1301 for (cmdent = cmd_table; *cmdent != NULL; cmdent++) { 1302 if (strncmp((*cmdent)->name, s, strlen(s)) == 0) 1303 ARRAY_ADD(&list, (*cmdent)->name); 1304 } 1305 for (oe = server_options_table; oe->name != NULL; oe++) { 1306 if (strncmp(oe->name, s, strlen(s)) == 0) 1307 ARRAY_ADD(&list, oe->name); 1308 } 1309 for (oe = session_options_table; oe->name != NULL; oe++) { 1310 if (strncmp(oe->name, s, strlen(s)) == 0) 1311 ARRAY_ADD(&list, oe->name); 1312 } 1313 for (oe = window_options_table; oe->name != NULL; oe++) { 1314 if (strncmp(oe->name, s, strlen(s)) == 0) 1315 ARRAY_ADD(&list, oe->name); 1316 } 1317 1318 /* If none, bail now. */ 1319 if (ARRAY_LENGTH(&list) == 0) { 1320 ARRAY_FREE(&list); 1321 return (NULL); 1322 } 1323 1324 /* If an exact match, return it, with a trailing space. */ 1325 if (ARRAY_LENGTH(&list) == 1) { 1326 xasprintf(&s2, "%s ", ARRAY_FIRST(&list)); 1327 ARRAY_FREE(&list); 1328 return (s2); 1329 } 1330 1331 /* Now loop through the list and find the longest common prefix. */ 1332 prefix = xstrdup(ARRAY_FIRST(&list)); 1333 for (i = 1; i < ARRAY_LENGTH(&list); i++) { 1334 s = ARRAY_ITEM(&list, i); 1335 1336 j = strlen(s); 1337 if (j > strlen(prefix)) 1338 j = strlen(prefix); 1339 for (; j > 0; j--) { 1340 if (prefix[j - 1] != s[j - 1]) 1341 prefix[j - 1] = '\0'; 1342 } 1343 } 1344 1345 ARRAY_FREE(&list); 1346 return (prefix); 1347} 1348