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