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