Annotation of src/usr.bin/tmux/status.c, Revision 1.120
1.120 ! nicm 1: /* $OpenBSD: status.c,v 1.119 2015/01/20 10:57:10 sthen Exp $ */
1.1 nicm 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:
1.48 nicm 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 *);
1.71 nicm 36: char *status_find_job(struct client *, char **);
37: void status_job_free(void *);
1.38 nicm 38: void status_job_callback(struct job *);
1.47 nicm 39: char *status_print(
1.55 nicm 40: struct client *, struct winlink *, time_t, struct grid_cell *);
1.104 nicm 41: void status_replace1(struct client *, char **, char **, char *, size_t, int);
1.42 nicm 42: void status_message_callback(int, short, void *);
1.1 nicm 43:
1.65 nicm 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 *);
1.1 nicm 47: char *status_prompt_complete(const char *);
48:
1.65 nicm 49: /* Status prompt history. */
50: ARRAY_DECL(, char *) status_prompt_history = ARRAY_INITIALIZER;
51:
1.71 nicm 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));
1.87 nicm 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);
1.71 nicm 74: }
75:
1.48 nicm 76: /* Retrieve options for left string. */
77: char *
1.120 ! nicm 78: status_redraw_get_left(struct client *c, time_t t, int utf8flag,
! 79: struct grid_cell *gc, size_t *size)
1.48 nicm 80: {
81: struct session *s = c->session;
1.120 ! nicm 82: const char *template;
1.48 nicm 83: char *left;
84: size_t leftlen;
85:
1.108 nicm 86: style_apply_update(gc, &s->options, "status-left-style");
1.48 nicm 87:
1.120 ! nicm 88: template = options_get_string(&s->options, "status-left");
! 89: left = status_replace(c, NULL, template , t, 1);
1.48 nicm 90:
91: *size = options_get_number(&s->options, "status-left-length");
92: leftlen = screen_write_cstrlen(utf8flag, "%s", left);
93: if (leftlen < *size)
94: *size = leftlen;
95: return (left);
96: }
97:
98: /* Retrieve options for right string. */
99: char *
1.120 ! nicm 100: status_redraw_get_right(struct client *c, time_t t, int utf8flag,
! 101: struct grid_cell *gc, size_t *size)
1.48 nicm 102: {
103: struct session *s = c->session;
1.120 ! nicm 104: const char *template;
1.48 nicm 105: char *right;
106: size_t rightlen;
107:
1.108 nicm 108: style_apply_update(gc, &s->options, "status-right-style");
1.48 nicm 109:
1.120 ! nicm 110: template = options_get_string(&s->options, "status-right");
! 111: right = status_replace(c, NULL, template, t, 1);
1.48 nicm 112:
113: *size = options_get_number(&s->options, "status-right-length");
114: rightlen = screen_write_cstrlen(utf8flag, "%s", right);
115: if (rightlen < *size)
116: *size = rightlen;
117: return (right);
118: }
119:
1.73 nicm 120: /* Set window at window list position. */
121: void
122: status_set_window_at(struct client *c, u_int x)
123: {
124: struct session *s = c->session;
125: struct winlink *wl;
1.115 nicm 126: struct options *oo;
127: size_t len;
1.73 nicm 128:
1.88 nicm 129: x += c->wlmouse;
1.73 nicm 130: RB_FOREACH(wl, winlinks, &s->windows) {
1.115 nicm 131: oo = &wl->window->options;
132:
133: len = strlen(options_get_string(oo, "window-status-separator"));
1.107 nicm 134: if (x < wl->status_width && session_select(s, wl->idx) == 0)
1.73 nicm 135: server_redraw_session(s);
1.115 nicm 136: x -= wl->status_width + len;
1.73 nicm 137: }
138: }
139:
1.1 nicm 140: /* Draw status for client on the last lines of given context. */
141: int
142: status_redraw(struct client *c)
143: {
1.48 nicm 144: struct screen_write_ctx ctx;
145: struct session *s = c->session;
146: struct winlink *wl;
147: struct screen old_status, window_list;
148: struct grid_cell stdgc, lgc, rgc, gc;
1.91 nicm 149: struct options *oo;
1.48 nicm 150: time_t t;
1.91 nicm 151: char *left, *right, *sep;
1.48 nicm 152: u_int offset, needed;
153: u_int wlstart, wlwidth, wlavailable, wloffset, wlsize;
1.91 nicm 154: size_t llen, rlen, seplen;
1.48 nicm 155: int larrow, rarrow, utf8flag;
1.1 nicm 156:
1.49 nicm 157: /* No status line? */
1.7 nicm 158: if (c->tty.sy == 0 || !options_get_number(&s->options, "status"))
159: return (1);
1.48 nicm 160: left = right = NULL;
1.7 nicm 161: larrow = rarrow = 0;
1.1 nicm 162:
1.48 nicm 163: /* Update status timer. */
1.1 nicm 164: if (gettimeofday(&c->status_timer, NULL) != 0)
1.34 nicm 165: fatal("gettimeofday failed");
1.48 nicm 166: t = c->status_timer.tv_sec;
167:
168: /* Set up default colour. */
1.108 nicm 169: style_apply(&stdgc, &s->options, "status-style");
1.1 nicm 170:
1.48 nicm 171: /* Create the target screen. */
172: memcpy(&old_status, &c->status, sizeof old_status);
173: screen_init(&c->status, c->tty.sx, 1, 0);
174: screen_write_start(&ctx, NULL, &c->status);
175: for (offset = 0; offset < c->tty.sx; offset++)
176: screen_write_putc(&ctx, &stdgc, ' ');
177: screen_write_stop(&ctx);
178:
179: /* If the height is one line, blank status line. */
180: if (c->tty.sy <= 1)
181: goto out;
1.1 nicm 182:
1.48 nicm 183: /* Get UTF-8 flag. */
1.3 nicm 184: utf8flag = options_get_number(&s->options, "status-utf8");
185:
1.48 nicm 186: /* Work out left and right strings. */
187: memcpy(&lgc, &stdgc, sizeof lgc);
188: left = status_redraw_get_left(c, t, utf8flag, &lgc, &llen);
189: memcpy(&rgc, &stdgc, sizeof rgc);
190: right = status_redraw_get_right(c, t, utf8flag, &rgc, &rlen);
1.1 nicm 191:
192: /*
1.48 nicm 193: * Figure out how much space we have for the window list. If there
194: * isn't enough space, just show a blank status line.
1.1 nicm 195: */
1.55 nicm 196: needed = 0;
1.1 nicm 197: if (llen != 0)
1.118 nicm 198: needed += llen;
1.1 nicm 199: if (rlen != 0)
1.118 nicm 200: needed += rlen;
1.48 nicm 201: if (c->tty.sx == 0 || c->tty.sx <= needed)
202: goto out;
203: wlavailable = c->tty.sx - needed;
1.1 nicm 204:
1.48 nicm 205: /* Calculate the total size needed for the window list. */
206: wlstart = wloffset = wlwidth = 0;
1.1 nicm 207: RB_FOREACH(wl, winlinks, &s->windows) {
1.94 nicm 208: free(wl->status_text);
1.48 nicm 209: memcpy(&wl->status_cell, &stdgc, sizeof wl->status_cell);
210: wl->status_text = status_print(c, wl, t, &wl->status_cell);
1.55 nicm 211: wl->status_width =
1.48 nicm 212: screen_write_cstrlen(utf8flag, "%s", wl->status_text);
213:
1.1 nicm 214: if (wl == s->curw)
1.48 nicm 215: wloffset = wlwidth;
1.91 nicm 216:
217: oo = &wl->window->options;
218: sep = options_get_string(oo, "window-status-separator");
219: seplen = screen_write_strlen(utf8flag, "%s", sep);
220: wlwidth += wl->status_width + seplen;
1.48 nicm 221: }
222:
223: /* Create a new screen for the window list. */
224: screen_init(&window_list, wlwidth, 1, 0);
225:
226: /* And draw the window list into it. */
227: screen_write_start(&ctx, NULL, &window_list);
228: RB_FOREACH(wl, winlinks, &s->windows) {
1.55 nicm 229: screen_write_cnputs(&ctx,
1.48 nicm 230: -1, &wl->status_cell, utf8flag, "%s", wl->status_text);
1.91 nicm 231:
232: oo = &wl->window->options;
233: sep = options_get_string(oo, "window-status-separator");
234: screen_write_nputs(&ctx, -1, &stdgc, utf8flag, "%s", sep);
1.1 nicm 235: }
1.48 nicm 236: screen_write_stop(&ctx);
1.1 nicm 237:
1.48 nicm 238: /* If there is enough space for the total width, skip to draw now. */
239: if (wlwidth <= wlavailable)
1.1 nicm 240: goto draw;
241:
242: /* Find size of current window text. */
1.48 nicm 243: wlsize = s->curw->status_width;
1.1 nicm 244:
245: /*
1.48 nicm 246: * If the current window is already on screen, good to draw from the
1.1 nicm 247: * start and just leave off the end.
248: */
1.48 nicm 249: if (wloffset + wlsize < wlavailable) {
250: if (wlavailable > 0) {
251: rarrow = 1;
252: wlavailable--;
253: }
254: wlwidth = wlavailable;
255: } else {
256: /*
257: * Work out how many characters we need to omit from the
258: * start. There are wlavailable characters to fill, and
259: * wloffset + wlsize must be the last. So, the start character
260: * is wloffset + wlsize - wlavailable.
261: */
262: if (wlavailable > 0) {
263: larrow = 1;
264: wlavailable--;
265: }
1.55 nicm 266:
1.48 nicm 267: wlstart = wloffset + wlsize - wlavailable;
268: if (wlavailable > 0 && wlwidth > wlstart + wlavailable + 1) {
1.1 nicm 269: rarrow = 1;
1.48 nicm 270: wlstart++;
271: wlavailable--;
1.1 nicm 272: }
1.48 nicm 273: wlwidth = wlavailable;
274: }
1.1 nicm 275:
1.48 nicm 276: /* Bail if anything is now too small too. */
277: if (wlwidth == 0 || wlavailable == 0) {
278: screen_free(&window_list);
279: goto out;
1.1 nicm 280: }
281:
282: /*
1.48 nicm 283: * Now the start position is known, work out the state of the left and
284: * right arrows.
1.1 nicm 285: */
286: offset = 0;
287: RB_FOREACH(wl, winlinks, &s->windows) {
1.63 nicm 288: if (wl->flags & WINLINK_ALERTFLAGS &&
289: larrow == 1 && offset < wlstart)
290: larrow = -1;
1.1 nicm 291:
1.48 nicm 292: offset += wl->status_width;
1.1 nicm 293:
1.63 nicm 294: if (wl->flags & WINLINK_ALERTFLAGS &&
295: rarrow == 1 && offset > wlstart + wlwidth)
296: rarrow = -1;
1.1 nicm 297: }
298:
1.48 nicm 299: draw:
1.55 nicm 300: /* Begin drawing. */
1.48 nicm 301: screen_write_start(&ctx, NULL, &c->status);
1.1 nicm 302:
1.48 nicm 303: /* Draw the left string and arrow. */
304: screen_write_cursormove(&ctx, 0, 0);
1.118 nicm 305: if (llen != 0)
1.48 nicm 306: screen_write_cnputs(&ctx, llen, &lgc, utf8flag, "%s", left);
1.1 nicm 307: if (larrow != 0) {
308: memcpy(&gc, &stdgc, sizeof gc);
309: if (larrow == -1)
310: gc.attr ^= GRID_ATTR_REVERSE;
311: screen_write_putc(&ctx, &gc, '<');
312: }
1.48 nicm 313:
314: /* Draw the right string and arrow. */
1.1 nicm 315: if (rarrow != 0) {
1.118 nicm 316: screen_write_cursormove(&ctx, c->tty.sx - rlen - 1, 0);
1.1 nicm 317: memcpy(&gc, &stdgc, sizeof gc);
318: if (rarrow == -1)
319: gc.attr ^= GRID_ATTR_REVERSE;
320: screen_write_putc(&ctx, &gc, '>');
1.48 nicm 321: } else
1.118 nicm 322: screen_write_cursormove(&ctx, c->tty.sx - rlen, 0);
323: if (rlen != 0)
1.48 nicm 324: screen_write_cnputs(&ctx, rlen, &rgc, utf8flag, "%s", right);
1.1 nicm 325:
1.48 nicm 326: /* Figure out the offset for the window list. */
1.58 nicm 327: if (llen != 0)
1.118 nicm 328: wloffset = llen;
1.58 nicm 329: else
330: wloffset = 0;
1.48 nicm 331: if (wlwidth < wlavailable) {
332: switch (options_get_number(&s->options, "status-justify")) {
1.119 sthen 333: case 1: /* centred */
1.58 nicm 334: wloffset += (wlavailable - wlwidth) / 2;
1.48 nicm 335: break;
336: case 2: /* right */
1.58 nicm 337: wloffset += (wlavailable - wlwidth);
1.48 nicm 338: break;
339: }
340: }
341: if (larrow != 0)
342: wloffset++;
343:
344: /* Copy the window list. */
1.88 nicm 345: c->wlmouse = -wloffset + wlstart;
1.48 nicm 346: screen_write_cursormove(&ctx, wloffset, 0);
347: screen_write_copy(&ctx, &window_list, wlstart, 0, wlwidth, 1);
1.55 nicm 348: screen_free(&window_list);
1.1 nicm 349:
1.48 nicm 350: screen_write_stop(&ctx);
1.1 nicm 351:
352: out:
1.94 nicm 353: free(left);
354: free(right);
1.1 nicm 355:
356: if (grid_compare(c->status.grid, old_status.grid) == 0) {
357: screen_free(&old_status);
358: return (0);
359: }
360: screen_free(&old_status);
361: return (1);
362: }
363:
1.46 nicm 364: /* Replace a single special sequence (prefixed by #). */
365: void
1.104 nicm 366: status_replace1(struct client *c, char **iptr, char **optr, char *out,
1.72 nicm 367: size_t outsize, int jobsflag)
368: {
1.104 nicm 369: char ch, tmp[256], *ptr, *endptr;
1.72 nicm 370: size_t ptrlen;
371: long limit;
1.46 nicm 372:
373: errno = 0;
374: limit = strtol(*iptr, &endptr, 10);
375: if ((limit == 0 && errno != EINVAL) ||
376: (limit == LONG_MIN && errno != ERANGE) ||
1.55 nicm 377: (limit == LONG_MAX && errno != ERANGE) ||
1.46 nicm 378: limit != 0)
379: *iptr = endptr;
380: if (limit <= 0)
381: limit = LONG_MAX;
382:
383: switch (*(*iptr)++) {
384: case '(':
385: if (!jobsflag) {
386: ch = ')';
387: goto skip_to;
388: }
1.71 nicm 389: if ((ptr = status_find_job(c, iptr)) == NULL)
1.46 nicm 390: return;
391: goto do_replace;
392: case '[':
1.55 nicm 393: /*
1.46 nicm 394: * Embedded style, handled at display time. Leave present and
395: * skip input until ].
396: */
397: ch = ']';
398: goto skip_to;
1.96 nicm 399: case '{':
400: ptr = (char *) "#{";
401: goto do_replace;
1.104 nicm 402: default:
403: xsnprintf(tmp, sizeof tmp, "#%c", *(*iptr - 1));
404: ptr = tmp;
405: goto do_replace;
1.46 nicm 406: }
407:
408: return;
1.55 nicm 409:
1.46 nicm 410: do_replace:
411: ptrlen = strlen(ptr);
412: if ((size_t) limit < ptrlen)
413: ptrlen = limit;
414:
415: if (*optr + ptrlen >= out + outsize - 1)
1.104 nicm 416: return;
1.46 nicm 417: while (ptrlen > 0 && *ptr != '\0') {
418: *(*optr)++ = *ptr++;
419: ptrlen--;
420: }
421:
422: return;
423:
424: skip_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. */
436: char *
1.120 ! nicm 437: status_replace(struct client *c, struct winlink *wl, const char *fmt, time_t t,
! 438: int jobsflag)
1.46 nicm 439: {
1.120 ! nicm 440: struct session *s = NULL;
! 441: struct window_pane *wp = NULL;
1.96 nicm 442: static char out[BUFSIZ];
1.98 nicm 443: char in[BUFSIZ], ch, *iptr, *optr, *expanded;
1.96 nicm 444: size_t len;
445: struct format_tree *ft;
1.47 nicm 446:
1.93 nicm 447: if (fmt == NULL)
448: return (xstrdup(""));
449:
1.120 ! nicm 450: if (c != NULL)
1.96 nicm 451: s = c->session;
1.109 nicm 452: if (wl == NULL && s != NULL)
1.96 nicm 453: wl = s->curw;
1.120 ! nicm 454: if (wl != NULL)
1.96 nicm 455: wp = wl->window->active;
456:
1.86 nicm 457: len = strftime(in, sizeof in, fmt, localtime(&t));
458: in[len] = '\0';
1.1 nicm 459:
460: iptr = in;
461: optr = out;
462:
463: while (*iptr != '\0') {
464: if (optr >= out + (sizeof out) - 1)
465: break;
1.46 nicm 466: ch = *iptr++;
467:
1.70 nicm 468: if (ch != '#' || *iptr == '\0') {
1.1 nicm 469: *optr++ = ch;
1.46 nicm 470: continue;
1.1 nicm 471: }
1.104 nicm 472: status_replace1(c, &iptr, &optr, out, sizeof out, jobsflag);
1.1 nicm 473: }
474: *optr = '\0';
475:
1.96 nicm 476: ft = format_create();
1.109 nicm 477: if (c != NULL)
478: format_client(ft, c);
479: if (s != NULL)
480: format_session(ft, s);
481: if (s != NULL && wl != NULL)
482: format_winlink(ft, s, wl);
483: if (wp != NULL)
484: format_window_pane(ft, wp);
1.99 nicm 485: expanded = format_expand(ft, out);
486: format_free(ft);
487: return (expanded);
1.1 nicm 488: }
489:
1.46 nicm 490: /* Figure out job name and get its result, starting it off if necessary. */
1.1 nicm 491: char *
1.71 nicm 492: status_find_job(struct client *c, char **iptr)
1.1 nicm 493: {
1.71 nicm 494: struct status_out *so, so_find;
495: char *cmd;
496: int lastesc;
497: size_t len;
1.1 nicm 498:
499: if (**iptr == '\0')
500: return (NULL);
501: if (**iptr == ')') { /* no command given */
502: (*iptr)++;
503: return (NULL);
504: }
505:
506: cmd = xmalloc(strlen(*iptr) + 1);
507: len = 0;
508:
509: lastesc = 0;
510: for (; **iptr != '\0'; (*iptr)++) {
511: if (!lastesc && **iptr == ')')
512: break; /* unescaped ) is the end */
513: if (!lastesc && **iptr == '\\') {
514: lastesc = 1;
515: continue; /* skip \ if not escaped */
516: }
517: lastesc = 0;
518: cmd[len++] = **iptr;
519: }
1.38 nicm 520: if (**iptr == '\0') /* no terminating ) */ {
1.94 nicm 521: free(cmd);
1.38 nicm 522: return (NULL);
523: }
1.1 nicm 524: (*iptr)++; /* skip final ) */
525: cmd[len] = '\0';
526:
1.71 nicm 527: /* First try in the new tree. */
528: so_find.cmd = cmd;
529: so = RB_FIND(status_out_tree, &c->status_new, &so_find);
1.78 nicm 530: if (so != NULL && so->out != NULL) {
1.94 nicm 531: free(cmd);
1.71 nicm 532: return (so->out);
1.78 nicm 533: }
1.71 nicm 534:
535: /* If not found at all, start the job and add to the tree. */
536: if (so == NULL) {
1.103 nicm 537: job_run(cmd, NULL, status_job_callback, status_job_free, c);
1.71 nicm 538: c->references++;
539:
540: so = xmalloc(sizeof *so);
541: so->cmd = xstrdup(cmd);
542: so->out = NULL;
543: RB_INSERT(status_out_tree, &c->status_new, so);
544: }
545:
546: /* Lookup in the old tree. */
547: so_find.cmd = cmd;
548: so = RB_FIND(status_out_tree, &c->status_old, &so_find);
1.94 nicm 549: free(cmd);
1.71 nicm 550: if (so != NULL)
551: return (so->out);
552: return (NULL);
553: }
554:
555: /* Free job tree. */
556: void
557: status_free_jobs(struct status_out_tree *sotree)
558: {
559: struct status_out *so, *so_next;
560:
561: so_next = RB_MIN(status_out_tree, sotree);
562: while (so_next != NULL) {
563: so = so_next;
564: so_next = RB_NEXT(status_out_tree, sotree, so);
565:
566: RB_REMOVE(status_out_tree, sotree, so);
1.94 nicm 567: free(so->out);
568: free(so->cmd);
569: free(so);
1.38 nicm 570: }
1.71 nicm 571: }
572:
573: /* Update jobs on status interval. */
574: void
575: status_update_jobs(struct client *c)
576: {
577: /* Free the old tree. */
578: status_free_jobs(&c->status_old);
579:
580: /* Move the new to old. */
581: memcpy(&c->status_old, &c->status_new, sizeof c->status_old);
582: RB_INIT(&c->status_new);
583: }
584:
585: /* Free status job. */
586: void
587: status_job_free(void *data)
588: {
589: struct client *c = data;
590:
591: c->references--;
1.38 nicm 592: }
1.1 nicm 593:
1.46 nicm 594: /* Job has finished: save its result. */
1.38 nicm 595: void
596: status_job_callback(struct job *job)
597: {
1.71 nicm 598: struct client *c = job->data;
599: struct status_out *so, so_find;
600: char *line, *buf;
601: size_t len;
602:
603: if (c->flags & CLIENT_DEAD)
604: return;
605:
606: so_find.cmd = job->cmd;
607: so = RB_FIND(status_out_tree, &c->status_new, &so_find);
608: if (so == NULL || so->out != NULL)
609: return;
1.38 nicm 610:
1.41 nicm 611: buf = NULL;
612: if ((line = evbuffer_readline(job->event->input)) == NULL) {
613: len = EVBUFFER_LENGTH(job->event->input);
614: buf = xmalloc(len + 1);
615: if (len != 0)
616: memcpy(buf, EVBUFFER_DATA(job->event->input), len);
617: buf[len] = '\0';
1.71 nicm 618: } else
1.102 nicm 619: buf = line;
1.1 nicm 620:
1.71 nicm 621: so->out = buf;
1.75 nicm 622: server_status_client(c);
1.1 nicm 623: }
624:
1.46 nicm 625: /* Return winlink status line entry and adjust gc as necessary. */
1.1 nicm 626: char *
1.120 ! nicm 627: status_print(struct client *c, struct winlink *wl, time_t t,
! 628: struct grid_cell *gc)
1.1 nicm 629: {
1.33 nicm 630: struct options *oo = &wl->window->options;
1.47 nicm 631: struct session *s = c->session;
632: const char *fmt;
633: char *text;
1.1 nicm 634:
1.108 nicm 635: style_apply_update(gc, oo, "window-status-style");
1.47 nicm 636: fmt = options_get_string(oo, "window-status-format");
1.14 nicm 637: if (wl == s->curw) {
1.108 nicm 638: style_apply_update(gc, oo, "window-status-current-style");
1.47 nicm 639: fmt = options_get_string(oo, "window-status-current-format");
1.95 nicm 640: }
1.108 nicm 641: if (wl == TAILQ_FIRST(&s->lastw))
642: style_apply_update(gc, oo, "window-status-last-style");
643:
644: if (wl->flags & WINLINK_BELL)
645: style_apply_update(gc, oo, "window-status-bell-style");
646: else if (wl->flags & (WINLINK_ACTIVITY|WINLINK_SILENCE))
647: style_apply_update(gc, oo, "window-status-activity-style");
1.1 nicm 648:
1.120 ! nicm 649: text = status_replace(c, wl, fmt, t, 1);
1.1 nicm 650: return (text);
651: }
652:
1.46 nicm 653: /* Set a status line message. */
1.117 nicm 654: void
1.10 nicm 655: status_message_set(struct client *c, const char *fmt, ...)
1.1 nicm 656: {
1.44 nicm 657: struct timeval tv;
658: struct message_entry *msg;
659: va_list ap;
660: int delay;
661: u_int i, limit;
1.1 nicm 662:
1.12 nicm 663: status_prompt_clear(c);
664: status_message_clear(c);
665:
1.28 nicm 666: va_start(ap, fmt);
667: xvasprintf(&c->message_string, fmt, ap);
668: va_end(ap);
669:
1.44 nicm 670: ARRAY_EXPAND(&c->message_log, 1);
671: msg = &ARRAY_LAST(&c->message_log);
672: msg->msg_time = time(NULL);
673: msg->msg = xstrdup(c->message_string);
674:
1.111 nicm 675: limit = options_get_number(&global_options, "message-limit");
1.50 nicm 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);
1.94 nicm 680: free(msg->msg);
1.50 nicm 681: ARRAY_REMOVE(&c->message_log, 0);
682: }
1.44 nicm 683: }
684:
1.1 nicm 685: delay = options_get_number(&c->session->options, "display-time");
686: tv.tv_sec = delay / 1000;
687: tv.tv_usec = (delay % 1000) * 1000L;
1.44 nicm 688:
1.110 nicm 689: if (event_initialized(&c->message_timer))
1.90 nicm 690: evtimer_del(&c->message_timer);
1.42 nicm 691: evtimer_set(&c->message_timer, status_message_callback, c);
692: evtimer_add(&c->message_timer, &tv);
1.1 nicm 693:
694: c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
695: c->flags |= CLIENT_STATUS;
696: }
697:
1.46 nicm 698: /* Clear status line message. */
1.1 nicm 699: void
700: status_message_clear(struct client *c)
701: {
702: if (c->message_string == NULL)
703: return;
704:
1.94 nicm 705: free(c->message_string);
1.1 nicm 706: c->message_string = NULL;
707:
708: c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.12 nicm 709: c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */
1.7 nicm 710:
711: screen_reinit(&c->status);
1.42 nicm 712: }
713:
1.46 nicm 714: /* Clear status line message after timer expires. */
1.42 nicm 715: void
716: status_message_callback(unused int fd, unused short event, void *data)
717: {
718: struct client *c = data;
719:
720: status_message_clear(c);
1.1 nicm 721: }
722:
723: /* Draw client message on status line of present else on last line. */
724: int
725: status_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;
1.51 nicm 732: int utf8flag;
1.1 nicm 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:
1.51 nicm 739: utf8flag = options_get_number(&s->options, "status-utf8");
740:
741: len = screen_write_strlen(utf8flag, "%s", c->message_string);
1.1 nicm 742: if (len > c->tty.sx)
743: len = c->tty.sx;
744:
1.108 nicm 745: style_apply(&gc, &s->options, "message-style");
1.1 nicm 746:
747: screen_write_start(&ctx, NULL, &c->status);
748:
749: screen_write_cursormove(&ctx, 0, 0);
1.51 nicm 750: screen_write_nputs(&ctx, len, &gc, utf8flag, "%s", c->message_string);
1.1 nicm 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:
1.46 nicm 764: /* Enable status line prompt. */
1.1 nicm 765: void
1.76 nicm 766: status_prompt_set(struct client *c, const char *msg, const char *input,
1.12 nicm 767: int (*callbackfn)(void *, const char *), void (*freefn)(void *),
768: void *data, int flags)
1.1 nicm 769: {
1.20 nicm 770: int keys;
771:
1.12 nicm 772: status_message_clear(c);
773: status_prompt_clear(c);
774:
1.120 ! nicm 775: c->prompt_string = status_replace(c, NULL, msg, time(NULL), 0);
1.1 nicm 776:
1.120 ! nicm 777: c->prompt_buffer = status_replace(c, NULL, input, time(NULL), 0);
1.76 nicm 778: c->prompt_index = strlen(c->prompt_buffer);
1.1 nicm 779:
1.12 nicm 780: c->prompt_callbackfn = callbackfn;
781: c->prompt_freefn = freefn;
1.1 nicm 782: c->prompt_data = data;
783:
784: c->prompt_hindex = 0;
785:
786: c->prompt_flags = flags;
787:
1.20 nicm 788: keys = options_get_number(&c->session->options, "status-keys");
789: if (keys == MODEKEY_EMACS)
1.21 nicm 790: mode_key_init(&c->prompt_mdata, &mode_key_tree_emacs_edit);
1.20 nicm 791: else
1.21 nicm 792: mode_key_init(&c->prompt_mdata, &mode_key_tree_vi_edit);
1.1 nicm 793:
794: c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
795: c->flags |= CLIENT_STATUS;
796: }
797:
1.46 nicm 798: /* Remove status line prompt. */
1.1 nicm 799: void
800: status_prompt_clear(struct client *c)
801: {
1.55 nicm 802: if (c->prompt_string == NULL)
1.1 nicm 803: return;
804:
1.12 nicm 805: if (c->prompt_freefn != NULL && c->prompt_data != NULL)
806: c->prompt_freefn(c->prompt_data);
1.1 nicm 807:
1.94 nicm 808: free(c->prompt_string);
1.1 nicm 809: c->prompt_string = NULL;
810:
1.94 nicm 811: free(c->prompt_buffer);
1.1 nicm 812: c->prompt_buffer = NULL;
813:
814: c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.12 nicm 815: c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */
1.7 nicm 816:
817: screen_reinit(&c->status);
1.27 nicm 818: }
819:
1.46 nicm 820: /* Update status line prompt with a new prompt string. */
1.27 nicm 821: void
1.76 nicm 822: status_prompt_update(struct client *c, const char *msg, const char *input)
1.27 nicm 823: {
1.94 nicm 824: free(c->prompt_string);
1.120 ! nicm 825: c->prompt_string = status_replace(c, NULL, msg, time(NULL), 0);
1.27 nicm 826:
1.94 nicm 827: free(c->prompt_buffer);
1.120 ! nicm 828: c->prompt_buffer = status_replace(c, NULL, input, time(NULL), 0);
1.76 nicm 829: c->prompt_index = strlen(c->prompt_buffer);
1.27 nicm 830:
831: c->prompt_hindex = 0;
832:
833: c->flags |= CLIENT_STATUS;
1.1 nicm 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: {
1.51 nicm 840: struct screen_write_ctx ctx;
1.1 nicm 841: struct session *s = c->session;
842: struct screen old_status;
1.29 nicm 843: size_t i, size, left, len, off;
1.51 nicm 844: struct grid_cell gc, *gcp;
845: int utf8flag;
1.1 nicm 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:
1.51 nicm 852: utf8flag = options_get_number(&s->options, "status-utf8");
853:
854: len = screen_write_strlen(utf8flag, "%s", c->prompt_string);
1.1 nicm 855: if (len > c->tty.sx)
856: len = c->tty.sx;
1.51 nicm 857: off = 0;
1.1 nicm 858:
1.79 nicm 859: /* Change colours for command mode. */
1.108 nicm 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");
1.1 nicm 864:
865: screen_write_start(&ctx, NULL, &c->status);
866:
867: screen_write_cursormove(&ctx, 0, 0);
1.51 nicm 868: screen_write_nputs(&ctx, len, &gc, utf8flag, "%s", c->prompt_string);
1.1 nicm 869:
870: left = c->tty.sx - len;
871: if (left != 0) {
1.51 nicm 872: size = screen_write_strlen(utf8flag, "%s", c->prompt_buffer);
873: if (c->prompt_index >= left) {
1.17 nicm 874: off = c->prompt_index - left + 1;
1.51 nicm 875: if (c->prompt_index == size)
1.1 nicm 876: left--;
877: size = left;
878: }
1.51 nicm 879: screen_write_nputs(
880: &ctx, left, &gc, utf8flag, "%s", c->prompt_buffer + off);
1.1 nicm 881:
1.55 nicm 882: for (i = len + size; i < c->tty.sx; i++)
883: screen_write_putc(&ctx, &gc, ' ');
1.1 nicm 884: }
885:
886: screen_write_stop(&ctx);
1.51 nicm 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;
1.1 nicm 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: {
1.81 nicm 905: struct session *sess = c->session;
906: struct options *oo = &sess->options;
1.1 nicm 907: struct paste_buffer *pb;
1.81 nicm 908: char *s, *first, *last, word[64], swapc;
909: const char *histstr;
1.83 nicm 910: const char *wsep = NULL;
1.53 nicm 911: u_char ch;
1.1 nicm 912: size_t size, n, off, idx;
913:
914: size = strlen(c->prompt_buffer);
1.101 nicm 915: switch (mode_key_lookup(&c->prompt_mdata, key, NULL)) {
1.20 nicm 916: case MODEKEYEDIT_CURSORLEFT:
1.1 nicm 917: if (c->prompt_index > 0) {
918: c->prompt_index--;
919: c->flags |= CLIENT_STATUS;
920: }
921: break;
1.79 nicm 922: case MODEKEYEDIT_SWITCHMODE:
923: c->flags |= CLIENT_STATUS;
924: break;
1.20 nicm 925: case MODEKEYEDIT_SWITCHMODEAPPEND:
1.79 nicm 926: c->flags |= CLIENT_STATUS;
927: /* FALLTHROUGH */
1.20 nicm 928: case MODEKEYEDIT_CURSORRIGHT:
1.1 nicm 929: if (c->prompt_index < size) {
930: c->prompt_index++;
931: c->flags |= CLIENT_STATUS;
932: }
933: break;
1.89 nicm 934: case MODEKEYEDIT_SWITCHMODEBEGINLINE:
935: c->flags |= CLIENT_STATUS;
936: /* FALLTHROUGH */
1.20 nicm 937: case MODEKEYEDIT_STARTOFLINE:
1.1 nicm 938: if (c->prompt_index != 0) {
939: c->prompt_index = 0;
940: c->flags |= CLIENT_STATUS;
941: }
942: break;
1.89 nicm 943: case MODEKEYEDIT_SWITCHMODEAPPENDLINE:
944: c->flags |= CLIENT_STATUS;
945: /* FALLTHROUGH */
1.20 nicm 946: case MODEKEYEDIT_ENDOFLINE:
1.1 nicm 947: if (c->prompt_index != size) {
948: c->prompt_index = size;
949: c->flags |= CLIENT_STATUS;
950: }
951: break;
1.20 nicm 952: case MODEKEYEDIT_COMPLETE:
1.1 nicm 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. */
1.55 nicm 989: size += strlen(s);
1.1 nicm 990: off = first - c->prompt_buffer;
1.116 nicm 991: c->prompt_buffer = xrealloc(c->prompt_buffer, size + 1);
1.1 nicm 992: first = c->prompt_buffer + off;
1.55 nicm 993: memmove(first + strlen(s), first, n);
994: memcpy(first, s, strlen(s));
1.1 nicm 995:
996: c->prompt_index = (first - c->prompt_buffer) + strlen(s);
1.94 nicm 997: free(s);
1.1 nicm 998:
999: c->flags |= CLIENT_STATUS;
1000: break;
1.20 nicm 1001: case MODEKEYEDIT_BACKSPACE:
1.1 nicm 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;
1.55 nicm 1014: case MODEKEYEDIT_DELETE:
1.105 nicm 1015: case MODEKEYEDIT_SWITCHMODESUBSTITUTE:
1.1 nicm 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);
1.18 nicm 1020: c->flags |= CLIENT_STATUS;
1021: }
1.26 nicm 1022: break;
1023: case MODEKEYEDIT_DELETELINE:
1.105 nicm 1024: case MODEKEYEDIT_SWITCHMODESUBSTITUTELINE:
1.26 nicm 1025: *c->prompt_buffer = '\0';
1026: c->prompt_index = 0;
1027: c->flags |= CLIENT_STATUS;
1.18 nicm 1028: break;
1.20 nicm 1029: case MODEKEYEDIT_DELETETOENDOFLINE:
1.105 nicm 1030: case MODEKEYEDIT_SWITCHMODECHANGELINE:
1.18 nicm 1031: if (c->prompt_index < size) {
1032: c->prompt_buffer[c->prompt_index] = '\0';
1.1 nicm 1033: c->flags |= CLIENT_STATUS;
1034: }
1035: break;
1.81 nicm 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;
1.83 nicm 1065: case MODEKEYEDIT_NEXTSPACE:
1066: wsep = " ";
1067: /* FALLTHROUGH */
1.81 nicm 1068: case MODEKEYEDIT_NEXTWORD:
1.83 nicm 1069: if (wsep == NULL)
1070: wsep = options_get_string(oo, "word-separators");
1.81 nicm 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;
1.83 nicm 1088: case MODEKEYEDIT_NEXTSPACEEND:
1089: wsep = " ";
1090: /* FALLTHROUGH */
1.81 nicm 1091: case MODEKEYEDIT_NEXTWORDEND:
1.83 nicm 1092: if (wsep == NULL)
1093: wsep = options_get_string(oo, "word-separators");
1.81 nicm 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++;
1.82 nicm 1105: if (strchr(wsep, c->prompt_buffer[c->prompt_index]))
1.81 nicm 1106: break;
1107: }
1.106 nicm 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--;
1.81 nicm 1113:
1114: c->flags |= CLIENT_STATUS;
1115: break;
1.83 nicm 1116: case MODEKEYEDIT_PREVIOUSSPACE:
1117: wsep = " ";
1118: /* FALLTHROUGH */
1.81 nicm 1119: case MODEKEYEDIT_PREVIOUSWORD:
1.83 nicm 1120: if (wsep == NULL)
1121: wsep = options_get_string(oo, "word-separators");
1.81 nicm 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;
1.20 nicm 1142: case MODEKEYEDIT_HISTORYUP:
1.66 nicm 1143: histstr = status_prompt_up_history(&c->prompt_hindex);
1144: if (histstr == NULL)
1.1 nicm 1145: break;
1.94 nicm 1146: free(c->prompt_buffer);
1.66 nicm 1147: c->prompt_buffer = xstrdup(histstr);
1.1 nicm 1148: c->prompt_index = strlen(c->prompt_buffer);
1149: c->flags |= CLIENT_STATUS;
1150: break;
1.20 nicm 1151: case MODEKEYEDIT_HISTORYDOWN:
1.66 nicm 1152: histstr = status_prompt_down_history(&c->prompt_hindex);
1153: if (histstr == NULL)
1.65 nicm 1154: break;
1.94 nicm 1155: free(c->prompt_buffer);
1.66 nicm 1156: c->prompt_buffer = xstrdup(histstr);
1.1 nicm 1157: c->prompt_index = strlen(c->prompt_buffer);
1158: c->flags |= CLIENT_STATUS;
1159: break;
1.20 nicm 1160: case MODEKEYEDIT_PASTE:
1.114 nicm 1161: if ((pb = paste_get_top()) == NULL)
1.1 nicm 1162: break;
1.32 nicm 1163: for (n = 0; n < pb->size; n++) {
1.53 nicm 1164: ch = (u_char) pb->data[n];
1165: if (ch < 32 || ch == 127)
1.32 nicm 1166: break;
1167: }
1.1 nicm 1168:
1.116 nicm 1169: c->prompt_buffer = xrealloc(c->prompt_buffer, size + n + 1);
1.1 nicm 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;
1.30 nicm 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: }
1.1 nicm 1195: break;
1.55 nicm 1196: case MODEKEYEDIT_ENTER:
1.25 nicm 1197: if (*c->prompt_buffer != '\0')
1.65 nicm 1198: status_prompt_add_history(c->prompt_buffer);
1.25 nicm 1199: if (c->prompt_callbackfn(c->prompt_data, c->prompt_buffer) == 0)
1200: status_prompt_clear(c);
1201: break;
1.20 nicm 1202: case MODEKEYEDIT_CANCEL:
1.12 nicm 1203: if (c->prompt_callbackfn(c->prompt_data, NULL) == 0)
1.1 nicm 1204: status_prompt_clear(c);
1205: break;
1.20 nicm 1206: case MODEKEY_OTHER:
1.61 nicm 1207: if ((key & 0xff00) != 0 || key < 32 || key == 127)
1.1 nicm 1208: break;
1.116 nicm 1209: c->prompt_buffer = xrealloc(c->prompt_buffer, size + 2);
1.1 nicm 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) {
1.12 nicm 1222: if (c->prompt_callbackfn(
1.1 nicm 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:
1.65 nicm 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:
1.1 nicm 1268: /* Add line to the history. */
1269: void
1.65 nicm 1270: status_prompt_add_history(const char *line)
1.1 nicm 1271: {
1.65 nicm 1272: u_int size;
1273:
1274: size = ARRAY_LENGTH(&status_prompt_history);
1275: if (size > 0 && strcmp(ARRAY_LAST(&status_prompt_history), line) == 0)
1.1 nicm 1276: return;
1277:
1.65 nicm 1278: if (size == PROMPT_HISTORY) {
1.94 nicm 1279: free(ARRAY_FIRST(&status_prompt_history));
1.65 nicm 1280: ARRAY_REMOVE(&status_prompt_history, 0);
1.1 nicm 1281: }
1282:
1.65 nicm 1283: ARRAY_ADD(&status_prompt_history, xstrdup(line));
1.1 nicm 1284: }
1285:
1286: /* Complete word. */
1287: char *
1288: status_prompt_complete(const char *s)
1289: {
1.69 nicm 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;
1.1 nicm 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);
1.56 nicm 1305: }
1.69 nicm 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);
1.1 nicm 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: }