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