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