Annotation of src/usr.bin/tmux/format.c, Revision 1.31
1.31 ! nicm 1: /* $OpenBSD: format.c,v 1.30 2013/10/10 11:50:20 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2011 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:
1.30 nicm 21: #include <ctype.h>
22: #include <errno.h>
1.1 nicm 23: #include <netdb.h>
24: #include <stdarg.h>
1.9 nicm 25: #include <stdlib.h>
1.1 nicm 26: #include <string.h>
27: #include <time.h>
28: #include <unistd.h>
29:
30: #include "tmux.h"
31:
32: /*
33: * Build a list of key-value pairs and use them to expand #{key} entries in a
34: * string.
35: */
36:
1.19 nicm 37: int format_replace(struct format_tree *, const char *, size_t, char **,
38: size_t *, size_t *);
39: void format_window_pane_tabs(struct format_tree *, struct window_pane *);
1.1 nicm 40:
41: /* Format key-value replacement entry. */
42: RB_GENERATE(format_tree, format_entry, entry, format_cmp);
43:
44: /* Format tree comparison function. */
45: int
46: format_cmp(struct format_entry *fe1, struct format_entry *fe2)
47: {
48: return (strcmp(fe1->key, fe2->key));
49: }
50:
1.25 nicm 51: /* Single-character uppercase aliases. */
52: const char *format_upper[] = {
1.1 nicm 53: NULL, /* A */
54: NULL, /* B */
55: NULL, /* C */
56: "pane_id", /* D */
57: NULL, /* E */
58: "window_flags", /* F */
59: NULL, /* G */
60: "host", /* H */
61: "window_index", /* I */
62: NULL, /* J */
63: NULL, /* K */
64: NULL, /* L */
65: NULL, /* M */
66: NULL, /* N */
67: NULL, /* O */
68: "pane_index", /* P */
69: NULL, /* Q */
70: NULL, /* R */
71: "session_name", /* S */
72: "pane_title", /* T */
73: NULL, /* U */
74: NULL, /* V */
75: "window_name", /* W */
76: NULL, /* X */
77: NULL, /* Y */
78: NULL /* Z */
79: };
80:
1.25 nicm 81: /* Single-character lowercase aliases. */
82: const char *format_lower[] = {
83: NULL, /* a */
84: NULL, /* b */
85: NULL, /* c */
86: NULL, /* d */
87: NULL, /* e */
88: NULL, /* f */
89: NULL, /* g */
90: "host_short", /* h */
91: NULL, /* i */
92: NULL, /* j */
93: NULL, /* k */
94: NULL, /* l */
95: NULL, /* m */
96: NULL, /* n */
97: NULL, /* o */
98: NULL, /* p */
99: NULL, /* q */
100: NULL, /* r */
101: NULL, /* s */
102: NULL, /* t */
103: NULL, /* u */
104: NULL, /* v */
105: NULL, /* w */
106: NULL, /* x */
107: NULL, /* y */
108: NULL /* z */
109: };
110:
1.1 nicm 111: /* Create a new tree. */
112: struct format_tree *
113: format_create(void)
114: {
115: struct format_tree *ft;
1.25 nicm 116: char host[MAXHOSTNAMELEN], *ptr;
1.1 nicm 117:
118: ft = xmalloc(sizeof *ft);
119: RB_INIT(ft);
120:
1.25 nicm 121: if (gethostname(host, sizeof host) == 0) {
1.1 nicm 122: format_add(ft, "host", "%s", host);
1.25 nicm 123: if ((ptr = strrchr(host, '.')) != NULL)
124: *ptr = '\0';
125: format_add(ft, "host_short", "%s", host);
126: }
1.1 nicm 127:
128: return (ft);
129: }
130:
131: /* Free a tree. */
132: void
133: format_free(struct format_tree *ft)
134: {
135: struct format_entry *fe, *fe_next;
136:
137: fe_next = RB_MIN(format_tree, ft);
138: while (fe_next != NULL) {
139: fe = fe_next;
140: fe_next = RB_NEXT(format_tree, ft, fe);
141:
142: RB_REMOVE(format_tree, ft, fe);
1.9 nicm 143: free(fe->value);
144: free(fe->key);
145: free(fe);
1.1 nicm 146: }
147:
1.25 nicm 148: free(ft);
1.1 nicm 149: }
150:
151: /* Add a key-value pair. */
152: void
153: format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
154: {
155: struct format_entry *fe;
1.28 nicm 156: struct format_entry *fe_now;
1.1 nicm 157: va_list ap;
158:
159: fe = xmalloc(sizeof *fe);
160: fe->key = xstrdup(key);
161:
162: va_start(ap, fmt);
163: xvasprintf(&fe->value, fmt, ap);
164: va_end(ap);
165:
1.28 nicm 166: fe_now = RB_INSERT(format_tree, ft, fe);
167: if (fe_now != NULL) {
168: free(fe_now->value);
169: fe_now->value = fe->value;
170: free(fe->key);
171: free(fe);
172: }
1.1 nicm 173: }
174:
175: /* Find a format entry. */
176: const char *
177: format_find(struct format_tree *ft, const char *key)
178: {
179: struct format_entry *fe, fe_find;
180:
181: fe_find.key = (char *) key;
182: fe = RB_FIND(format_tree, ft, &fe_find);
183: if (fe == NULL)
184: return (NULL);
185: return (fe->value);
186: }
187:
188: /*
189: * Replace a key/value pair in buffer. #{blah} is expanded directly,
190: * #{?blah,a,b} is replace with a if blah exists and is nonzero else b.
191: */
192: int
1.30 nicm 193: format_replace(struct format_tree *ft, const char *key, size_t keylen,
194: char **buf, size_t *len, size_t *off)
1.1 nicm 195: {
1.31 ! nicm 196: char *copy, *copy0, *endptr, *ptr, *saved;
1.1 nicm 197: const char *value;
198: size_t valuelen;
1.30 nicm 199: u_long limit = ULONG_MAX;
1.1 nicm 200:
201: /* Make a copy of the key. */
1.30 nicm 202: copy0 = copy = xmalloc(keylen + 1);
1.1 nicm 203: memcpy(copy, key, keylen);
204: copy[keylen] = '\0';
205:
1.30 nicm 206: /* Is there a length limit or whatnot? */
207: if (!islower((u_char) *copy) && *copy != '?') {
208: while (*copy != ':' && *copy != '\0') {
209: switch (*copy) {
210: case '=':
211: errno = 0;
212: limit = strtoul(copy + 1, &endptr, 10);
213: if (errno == ERANGE && limit == ULONG_MAX)
214: goto fail;
215: copy = endptr;
216: break;
217: default:
218: copy++;
219: break;
220: }
221: }
222: if (*copy != ':')
223: goto fail;
224: copy++;
225: }
226:
1.1 nicm 227: /*
228: * Is this a conditional? If so, check it exists and extract either the
229: * first or second element. If not, look up the key directly.
230: */
231: if (*copy == '?') {
232: ptr = strchr(copy, ',');
233: if (ptr == NULL)
234: goto fail;
235: *ptr = '\0';
236:
237: value = format_find(ft, copy + 1);
238: if (value != NULL && (value[0] != '0' || value[1] != '\0')) {
239: value = ptr + 1;
240: ptr = strchr(value, ',');
241: if (ptr == NULL)
242: goto fail;
243: *ptr = '\0';
244: } else {
245: ptr = strchr(ptr + 1, ',');
246: if (ptr == NULL)
247: goto fail;
248: value = ptr + 1;
249: }
1.31 ! nicm 250: saved = format_expand(ft, value);
! 251: value = saved;
1.1 nicm 252: } else {
253: value = format_find(ft, copy);
254: if (value == NULL)
255: value = "";
1.31 ! nicm 256: saved = NULL;
1.1 nicm 257: }
258: valuelen = strlen(value);
259:
1.30 nicm 260: /* Truncate the value if needed. */
261: if (valuelen > limit)
262: valuelen = limit;
263:
1.1 nicm 264: /* Expand the buffer and copy in the value. */
265: while (*len - *off < valuelen + 1) {
266: *buf = xrealloc(*buf, 2, *len);
267: *len *= 2;
268: }
269: memcpy(*buf + *off, value, valuelen);
270: *off += valuelen;
271:
1.31 ! nicm 272: free(saved);
1.30 nicm 273: free(copy0);
1.1 nicm 274: return (0);
275:
276: fail:
1.30 nicm 277: free(copy0);
1.1 nicm 278: return (-1);
279: }
280:
281: /* Expand keys in a template. */
282: char *
283: format_expand(struct format_tree *ft, const char *fmt)
284: {
1.31 ! nicm 285: char *buf;
! 286: const char *ptr, *s;
1.1 nicm 287: size_t off, len, n;
1.31 ! nicm 288: int ch, brackets;
1.1 nicm 289:
290: len = 64;
291: buf = xmalloc(len);
292: off = 0;
293:
294: while (*fmt != '\0') {
295: if (*fmt != '#') {
296: while (len - off < 2) {
297: buf = xrealloc(buf, 2, len);
298: len *= 2;
299: }
300: buf[off++] = *fmt++;
301: continue;
302: }
303: fmt++;
304:
305: ch = (u_char) *fmt++;
306: switch (ch) {
307: case '{':
1.31 ! nicm 308: brackets = 1;
! 309: for (ptr = fmt; *ptr != '\0'; ptr++) {
! 310: if (*ptr == '{')
! 311: brackets++;
! 312: if (*ptr == '}' && --brackets == 0)
! 313: break;
! 314: }
! 315: if (*ptr != '}' || brackets != 0)
1.1 nicm 316: break;
317: n = ptr - fmt;
318:
319: if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
320: break;
321: fmt += n + 1;
322: continue;
323: default:
1.25 nicm 324: s = NULL;
325: if (ch >= 'A' && ch <= 'Z')
326: s = format_upper[ch - 'A'];
327: else if (ch >= 'a' && ch <= 'z')
328: s = format_lower[ch - 'a'];
329: if (s == NULL) {
330: while (len - off < 3) {
331: buf = xrealloc(buf, 2, len);
332: len *= 2;
1.1 nicm 333: }
1.25 nicm 334: buf[off++] = '#';
335: buf[off++] = ch;
336: continue;
1.1 nicm 337: }
1.25 nicm 338: n = strlen(s);
339: if (format_replace(ft, s, n, &buf, &len, &off) != 0)
340: break;
1.1 nicm 341: continue;
342: }
343:
344: break;
345: }
346: buf[off] = '\0';
347:
348: return (buf);
349: }
350:
351: /* Set default format keys for a session. */
352: void
353: format_session(struct format_tree *ft, struct session *s)
354: {
355: struct session_group *sg;
356: char *tim;
357: time_t t;
358:
359: format_add(ft, "session_name", "%s", s->name);
360: format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
361: format_add(ft, "session_width", "%u", s->sx);
362: format_add(ft, "session_height", "%u", s->sy);
1.23 nicm 363: format_add(ft, "session_id", "$%u", s->id);
1.1 nicm 364:
365: sg = session_group_find(s);
366: format_add(ft, "session_grouped", "%d", sg != NULL);
367: if (sg != NULL)
368: format_add(ft, "session_group", "%u", session_group_index(sg));
369:
370: t = s->creation_time.tv_sec;
1.24 deraadt 371: format_add(ft, "session_created", "%lld", (long long) t);
1.1 nicm 372: tim = ctime(&t);
373: *strchr(tim, '\n') = '\0';
374: format_add(ft, "session_created_string", "%s", tim);
375:
376: if (s->flags & SESSION_UNATTACHED)
377: format_add(ft, "session_attached", "%d", 0);
378: else
379: format_add(ft, "session_attached", "%d", 1);
1.3 nicm 380: }
381:
382: /* Set default format keys for a client. */
383: void
384: format_client(struct format_tree *ft, struct client *c)
385: {
1.15 nicm 386: char *tim;
387: time_t t;
388: struct session *s;
1.3 nicm 389:
390: format_add(ft, "client_cwd", "%s", c->cwd);
1.6 nicm 391: format_add(ft, "client_height", "%u", c->tty.sy);
392: format_add(ft, "client_width", "%u", c->tty.sx);
1.27 nicm 393: if (c->tty.path != NULL)
394: format_add(ft, "client_tty", "%s", c->tty.path);
395: if (c->tty.termname != NULL)
396: format_add(ft, "client_termname", "%s", c->tty.termname);
1.3 nicm 397:
398: t = c->creation_time.tv_sec;
1.24 deraadt 399: format_add(ft, "client_created", "%lld", (long long) t);
1.3 nicm 400: tim = ctime(&t);
401: *strchr(tim, '\n') = '\0';
402: format_add(ft, "client_created_string", "%s", tim);
403:
404: t = c->activity_time.tv_sec;
1.24 deraadt 405: format_add(ft, "client_activity", "%lld", (long long) t);
1.3 nicm 406: tim = ctime(&t);
407: *strchr(tim, '\n') = '\0';
408: format_add(ft, "client_activity_string", "%s", tim);
1.14 nicm 409:
410: format_add(ft, "client_prefix", "%d", !!(c->flags & CLIENT_PREFIX));
1.3 nicm 411:
412: if (c->tty.flags & TTY_UTF8)
413: format_add(ft, "client_utf8", "%d", 1);
414: else
415: format_add(ft, "client_utf8", "%d", 0);
416:
417: if (c->flags & CLIENT_READONLY)
418: format_add(ft, "client_readonly", "%d", 1);
419: else
420: format_add(ft, "client_readonly", "%d", 0);
1.15 nicm 421:
422: s = c->session;
423: if (s != NULL)
424: format_add(ft, "client_session", "%s", s->name);
425: s = c->last_session;
426: if (s != NULL && session_alive(s))
427: format_add(ft, "client_last_session", "%s", s->name);
1.1 nicm 428: }
429:
430: /* Set default format keys for a winlink. */
431: void
432: format_winlink(struct format_tree *ft, struct session *s, struct winlink *wl)
433: {
434: struct window *w = wl->window;
435: char *layout, *flags;
436:
437: layout = layout_dump(w);
438: flags = window_printable_flags(s, wl);
439:
1.5 nicm 440: format_add(ft, "window_id", "@%u", w->id);
1.1 nicm 441: format_add(ft, "window_index", "%d", wl->idx);
442: format_add(ft, "window_name", "%s", w->name);
443: format_add(ft, "window_width", "%u", w->sx);
444: format_add(ft, "window_height", "%u", w->sy);
445: format_add(ft, "window_flags", "%s", flags);
446: format_add(ft, "window_layout", "%s", layout);
447: format_add(ft, "window_active", "%d", wl == s->curw);
1.8 nicm 448: format_add(ft, "window_panes", "%u", window_count_panes(w));
1.29 nicm 449:
450: format_add(ft, "window_bell_flag", "%u",
451: !!(wl->flags & WINLINK_BELL));
452: format_add(ft, "window_content_flag", "%u",
453: !!(wl->flags & WINLINK_CONTENT));
454: format_add(ft, "window_activity_flag", "%u",
455: !!(wl->flags & WINLINK_ACTIVITY));
456: format_add(ft, "window_silence_flag", "%u",
457: !!(wl->flags & WINLINK_SILENCE));
1.1 nicm 458:
1.9 nicm 459: free(flags);
460: free(layout);
1.1 nicm 461: }
462:
1.19 nicm 463: /* Add window pane tabs. */
464: void
465: format_window_pane_tabs(struct format_tree *ft, struct window_pane *wp)
466: {
467: struct evbuffer *buffer;
468: u_int i;
469:
470: buffer = evbuffer_new();
471: for (i = 0; i < wp->base.grid->sx; i++) {
472: if (!bit_test(wp->base.tabs, i))
473: continue;
474:
475: if (EVBUFFER_LENGTH(buffer) > 0)
476: evbuffer_add(buffer, ",", 1);
477: evbuffer_add_printf(buffer, "%d", i);
478: }
479:
480: format_add(ft, "pane_tabs", "%.*s", (int) EVBUFFER_LENGTH(buffer),
481: EVBUFFER_DATA(buffer));
482: evbuffer_free(buffer);
483: }
484:
1.1 nicm 485: /* Set default format keys for a window pane. */
486: void
487: format_window_pane(struct format_tree *ft, struct window_pane *wp)
488: {
489: struct grid *gd = wp->base.grid;
490: struct grid_line *gl;
491: unsigned long long size;
1.20 nicm 492: u_int i, idx;
1.21 nicm 493: const char *cwd;
494: char *cmd;
1.1 nicm 495:
496: size = 0;
497: for (i = 0; i < gd->hsize; i++) {
498: gl = &gd->linedata[i];
499: size += gl->cellsize * sizeof *gl->celldata;
500: }
501: size += gd->hsize * sizeof *gd->linedata;
1.16 nicm 502: format_add(ft, "history_size", "%u", gd->hsize);
503: format_add(ft, "history_limit", "%u", gd->hlimit);
504: format_add(ft, "history_bytes", "%llu", size);
1.1 nicm 505:
1.4 nicm 506: if (window_pane_index(wp, &idx) != 0)
507: fatalx("index not found");
1.16 nicm 508: format_add(ft, "pane_index", "%u", idx);
1.4 nicm 509:
1.1 nicm 510: format_add(ft, "pane_width", "%u", wp->sx);
511: format_add(ft, "pane_height", "%u", wp->sy);
512: format_add(ft, "pane_title", "%s", wp->base.title);
513: format_add(ft, "pane_id", "%%%u", wp->id);
514: format_add(ft, "pane_active", "%d", wp == wp->window->active);
515: format_add(ft, "pane_dead", "%d", wp->fd == -1);
1.16 nicm 516:
517: format_add(ft, "pane_in_mode", "%d", wp->screen != &wp->base);
1.26 nicm 518: format_add(ft, "pane_synchronized", "%d",
519: !!options_get_number(&wp->window->options, "synchronize-panes"));
1.16 nicm 520:
521: if (wp->tty != NULL)
522: format_add(ft, "pane_tty", "%s", wp->tty);
523: format_add(ft, "pane_pid", "%ld", (long) wp->pid);
1.2 nicm 524: if (wp->cmd != NULL)
525: format_add(ft, "pane_start_command", "%s", wp->cmd);
526: if (wp->cwd != NULL)
527: format_add(ft, "pane_start_path", "%s", wp->cwd);
1.12 nicm 528: if ((cwd = get_proc_cwd(wp->fd)) != NULL)
529: format_add(ft, "pane_current_path", "%s", cwd);
1.21 nicm 530: if ((cmd = get_proc_name(wp->fd, wp->tty)) != NULL) {
1.17 nicm 531: format_add(ft, "pane_current_command", "%s", cmd);
1.21 nicm 532: free(cmd);
533: }
1.16 nicm 534:
535: format_add(ft, "cursor_x", "%d", wp->base.cx);
536: format_add(ft, "cursor_y", "%d", wp->base.cy);
537: format_add(ft, "scroll_region_upper", "%d", wp->base.rupper);
538: format_add(ft, "scroll_region_lower", "%d", wp->base.rlower);
539: format_add(ft, "saved_cursor_x", "%d", wp->ictx.old_cx);
540: format_add(ft, "saved_cursor_y", "%d", wp->ictx.old_cy);
541:
542: format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0);
543: format_add(ft, "alternate_saved_x", "%d", wp->saved_cx);
544: format_add(ft, "alternate_saved_y", "%d", wp->saved_cy);
545:
546: format_add(ft, "cursor_flag", "%d",
547: !!(wp->base.mode & MODE_CURSOR));
548: format_add(ft, "insert_flag", "%d",
549: !!(wp->base.mode & MODE_INSERT));
550: format_add(ft, "keypad_cursor_flag", "%d",
551: !!(wp->base.mode & MODE_KCURSOR));
552: format_add(ft, "keypad_flag", "%d",
553: !!(wp->base.mode & MODE_KKEYPAD));
554: format_add(ft, "wrap_flag", "%d",
555: !!(wp->base.mode & MODE_WRAP));
556:
557: format_add(ft, "mouse_standard_flag", "%d",
558: !!(wp->base.mode & MODE_MOUSE_STANDARD));
559: format_add(ft, "mouse_button_flag", "%d",
560: !!(wp->base.mode & MODE_MOUSE_BUTTON));
561: format_add(ft, "mouse_any_flag", "%d",
562: !!(wp->base.mode & MODE_MOUSE_ANY));
563: format_add(ft, "mouse_utf8_flag", "%d",
564: !!(wp->base.mode & MODE_MOUSE_UTF8));
1.19 nicm 565:
566: format_window_pane_tabs(ft, wp);
1.8 nicm 567: }
568:
1.19 nicm 569: /* Set default format keys for paste buffer. */
1.8 nicm 570: void
571: format_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
572: {
573: char *pb_print = paste_print(pb, 50);
574:
575: format_add(ft, "buffer_size", "%zu", pb->size);
576: format_add(ft, "buffer_sample", "%s", pb_print);
577:
1.9 nicm 578: free(pb_print);
1.1 nicm 579: }