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