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