Annotation of src/usr.bin/tmux/format.c, Revision 1.75
1.75 ! nicm 1: /* $OpenBSD: format.c,v 1.74 2015/06/18 23:55:24 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>
1.55 nicm 20: #include <sys/wait.h>
1.1 nicm 21:
1.30 nicm 22: #include <ctype.h>
23: #include <errno.h>
1.1 nicm 24: #include <netdb.h>
25: #include <stdarg.h>
1.9 nicm 26: #include <stdlib.h>
1.1 nicm 27: #include <string.h>
28: #include <time.h>
29: #include <unistd.h>
30:
31: #include "tmux.h"
32:
33: /*
34: * Build a list of key-value pairs and use them to expand #{key} entries in a
35: * string.
36: */
37:
1.68 nicm 38: void format_job_callback(struct job *);
39: const char *format_job_get(struct format_tree *, const char *);
40:
1.32 nicm 41: int format_replace(struct format_tree *, const char *, size_t, char **,
42: size_t *, size_t *);
1.60 nicm 43: char *format_time_string(time_t);
1.32 nicm 44: char *format_get_command(struct window_pane *);
1.57 nicm 45:
46: void format_defaults_pane_tabs(struct format_tree *, struct window_pane *);
47: void format_defaults_session(struct format_tree *, struct session *);
48: void format_defaults_client(struct format_tree *, struct client *);
49: void format_defaults_winlink(struct format_tree *, struct session *,
50: struct winlink *);
1.1 nicm 51:
1.68 nicm 52: /* Entry in format job tree. */
53: struct format_job {
54: const char *cmd;
55:
56: time_t last;
57: char *out;
58:
59: struct job *job;
60: int status;
61:
62: RB_ENTRY(format_job) entry;
63: };
64:
65: /* Format job tree. */
66: int format_job_cmp(struct format_job *, struct format_job *);
67: RB_HEAD(format_job_tree, format_job) format_jobs = RB_INITIALIZER();
68: RB_PROTOTYPE(format_job_tree, format_job, entry, format_job_cmp);
69: RB_GENERATE(format_job_tree, format_job, entry, format_job_cmp);
70:
71: /* Format job tree comparison function. */
72: int
73: format_job_cmp(struct format_job *fj1, struct format_job *fj2)
74: {
75: return (strcmp(fj1->cmd, fj2->cmd));
76: }
77:
1.54 nicm 78: /* Entry in format tree. */
79: struct format_entry {
80: char *key;
81: char *value;
82:
83: RB_ENTRY(format_entry) entry;
84: };
85:
1.68 nicm 86: /* Format entry tree. */
1.54 nicm 87: struct format_tree {
88: struct window *w;
89: struct session *s;
90:
1.68 nicm 91: int status;
92:
93: RB_HEAD(format_entry_tree, format_entry) tree;
1.54 nicm 94: };
1.68 nicm 95: int format_entry_cmp(struct format_entry *, struct format_entry *);
96: RB_PROTOTYPE(format_entry_tree, format_entry, entry, format_entry_cmp);
97: RB_GENERATE(format_entry_tree, format_entry, entry, format_entry_cmp);
1.54 nicm 98:
1.68 nicm 99: /* Format entry tree comparison function. */
1.1 nicm 100: int
1.68 nicm 101: format_entry_cmp(struct format_entry *fe1, struct format_entry *fe2)
1.1 nicm 102: {
103: return (strcmp(fe1->key, fe2->key));
104: }
105:
1.25 nicm 106: /* Single-character uppercase aliases. */
107: const char *format_upper[] = {
1.1 nicm 108: NULL, /* A */
109: NULL, /* B */
110: NULL, /* C */
111: "pane_id", /* D */
112: NULL, /* E */
113: "window_flags", /* F */
114: NULL, /* G */
115: "host", /* H */
116: "window_index", /* I */
117: NULL, /* J */
118: NULL, /* K */
119: NULL, /* L */
120: NULL, /* M */
121: NULL, /* N */
122: NULL, /* O */
123: "pane_index", /* P */
124: NULL, /* Q */
125: NULL, /* R */
126: "session_name", /* S */
127: "pane_title", /* T */
128: NULL, /* U */
129: NULL, /* V */
130: "window_name", /* W */
131: NULL, /* X */
132: NULL, /* Y */
133: NULL /* Z */
134: };
135:
1.25 nicm 136: /* Single-character lowercase aliases. */
137: const char *format_lower[] = {
138: NULL, /* a */
139: NULL, /* b */
140: NULL, /* c */
141: NULL, /* d */
142: NULL, /* e */
143: NULL, /* f */
144: NULL, /* g */
145: "host_short", /* h */
146: NULL, /* i */
147: NULL, /* j */
148: NULL, /* k */
149: NULL, /* l */
150: NULL, /* m */
151: NULL, /* n */
152: NULL, /* o */
153: NULL, /* p */
154: NULL, /* q */
155: NULL, /* r */
156: NULL, /* s */
157: NULL, /* t */
158: NULL, /* u */
159: NULL, /* v */
160: NULL, /* w */
161: NULL, /* x */
162: NULL, /* y */
163: NULL /* z */
164: };
165:
1.68 nicm 166: /* Format job callback. */
167: void
168: format_job_callback(struct job *job)
169: {
170: struct format_job *fj = job->data;
171: char *line, *buf;
172: size_t len;
173: struct client *c;
174:
175: fj->job = NULL;
176: free(fj->out);
177:
178: buf = NULL;
179: if ((line = evbuffer_readline(job->event->input)) == NULL) {
180: len = EVBUFFER_LENGTH(job->event->input);
181: buf = xmalloc(len + 1);
182: if (len != 0)
183: memcpy(buf, EVBUFFER_DATA(job->event->input), len);
184: buf[len] = '\0';
185: } else
186: buf = line;
187: fj->out = buf;
188:
189: if (fj->status) {
190: TAILQ_FOREACH(c, &clients, entry)
191: server_status_client(c);
192: fj->status = 0;
193: }
194: }
195:
196: /* Find a job. */
197: const char *
198: format_job_get(struct format_tree *ft, const char *cmd)
199: {
200: struct format_job fj0, *fj;
201:
202: fj0.cmd = cmd;
203: if ((fj = RB_FIND(format_job_tree, &format_jobs, &fj0)) == NULL)
204: {
205: fj = xcalloc(1, sizeof *fj);
206: fj->cmd = xstrdup(cmd);
207: fj->status = ft->status;
208:
209: xasprintf(&fj->out, "<'%s' not ready>", fj->cmd);
210:
211: RB_INSERT(format_job_tree, &format_jobs, fj);
212: }
213:
214: if (fj->job == NULL && fj->last != time(NULL)) {
215: fj->job = job_run(fj->cmd, NULL, -1, format_job_callback,
216: NULL, fj);
217: if (fj->job == NULL) {
218: free(fj->out);
219: xasprintf(&fj->out, "<'%s' didn't start>", fj->cmd);
220: }
221: }
222: fj->last = time(NULL);
223:
224: return (fj->out);
225: }
226:
227: /* Remove old jobs. */
228: void
229: format_clean(void)
230: {
231: struct format_job *fj, *fj1;
232: time_t now;
233:
234: now = time(NULL);
235: RB_FOREACH_SAFE(fj, format_job_tree, &format_jobs, fj1) {
236: if (fj->last > now || now - fj->last < 3600)
237: continue;
238: RB_REMOVE(format_job_tree, &format_jobs, fj);
239:
240: if (fj->job != NULL)
241: job_free(fj->job);
242:
243: free((void*)fj->cmd);
244: free(fj->out);
245:
246: free(fj);
247: }
248: }
249:
1.1 nicm 250: /* Create a new tree. */
251: struct format_tree *
252: format_create(void)
253: {
1.68 nicm 254: return (format_create_status(0));
255: }
256:
257: /* Create a new tree for the status line. */
258: struct format_tree *
259: format_create_status(int status)
260: {
1.1 nicm 261: struct format_tree *ft;
1.68 nicm 262: char host[HOST_NAME_MAX + 1], *ptr;
1.1 nicm 263:
1.54 nicm 264: ft = xcalloc(1, sizeof *ft);
265: RB_INIT(&ft->tree);
1.68 nicm 266: ft->status = status;
1.1 nicm 267:
1.25 nicm 268: if (gethostname(host, sizeof host) == 0) {
1.1 nicm 269: format_add(ft, "host", "%s", host);
1.33 nicm 270: if ((ptr = strchr(host, '.')) != NULL)
1.25 nicm 271: *ptr = '\0';
272: format_add(ft, "host_short", "%s", host);
273: }
1.72 nicm 274: format_add(ft, "pid", "%ld", (long) getpid());
1.1 nicm 275:
276: return (ft);
277: }
278:
279: /* Free a tree. */
280: void
281: format_free(struct format_tree *ft)
282: {
1.54 nicm 283: struct format_entry *fe, *fe1;
1.1 nicm 284:
1.68 nicm 285: RB_FOREACH_SAFE(fe, format_entry_tree, &ft->tree, fe1) {
286: RB_REMOVE(format_entry_tree, &ft->tree, fe);
1.9 nicm 287: free(fe->value);
288: free(fe->key);
289: free(fe);
1.1 nicm 290: }
291:
1.25 nicm 292: free(ft);
1.1 nicm 293: }
294:
295: /* Add a key-value pair. */
296: void
297: format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
298: {
299: struct format_entry *fe;
1.28 nicm 300: struct format_entry *fe_now;
1.1 nicm 301: va_list ap;
302:
303: fe = xmalloc(sizeof *fe);
304: fe->key = xstrdup(key);
305:
306: va_start(ap, fmt);
307: xvasprintf(&fe->value, fmt, ap);
308: va_end(ap);
309:
1.68 nicm 310: fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
1.28 nicm 311: if (fe_now != NULL) {
312: free(fe_now->value);
313: fe_now->value = fe->value;
314: free(fe->key);
315: free(fe);
316: }
1.1 nicm 317: }
318:
319: /* Find a format entry. */
320: const char *
321: format_find(struct format_tree *ft, const char *key)
322: {
323: struct format_entry *fe, fe_find;
1.54 nicm 324: struct options_entry *o;
325: static char s[16];
326:
327: o = options_find(&global_options, key);
328: if (o == NULL && ft->w != NULL)
329: o = options_find(&ft->w->options, key);
330: if (o == NULL)
331: o = options_find(&global_w_options, key);
332: if (o == NULL && ft->s != NULL)
333: o = options_find(&ft->s->options, key);
334: if (o == NULL)
335: o = options_find(&global_s_options, key);
336: if (o != NULL) {
337: switch (o->type) {
338: case OPTIONS_STRING:
339: return (o->str);
340: case OPTIONS_NUMBER:
1.74 nicm 341: xsnprintf(s, sizeof s, "%lld", o->num);
1.54 nicm 342: return (s);
343: case OPTIONS_STYLE:
344: return (style_tostring(&o->style));
345: }
346: }
1.1 nicm 347:
348: fe_find.key = (char *) key;
1.68 nicm 349: fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find);
1.1 nicm 350: if (fe == NULL)
351: return (NULL);
352: return (fe->value);
353: }
354:
355: /*
356: * Replace a key/value pair in buffer. #{blah} is expanded directly,
357: * #{?blah,a,b} is replace with a if blah exists and is nonzero else b.
358: */
359: int
1.30 nicm 360: format_replace(struct format_tree *ft, const char *key, size_t keylen,
361: char **buf, size_t *len, size_t *off)
1.1 nicm 362: {
1.44 nicm 363: char *copy, *copy0, *endptr, *ptr, *saved, *trimmed;
1.1 nicm 364: const char *value;
365: size_t valuelen;
1.44 nicm 366: u_long limit = 0;
1.1 nicm 367:
368: /* Make a copy of the key. */
1.30 nicm 369: copy0 = copy = xmalloc(keylen + 1);
1.1 nicm 370: memcpy(copy, key, keylen);
371: copy[keylen] = '\0';
372:
1.30 nicm 373: /* Is there a length limit or whatnot? */
1.54 nicm 374: if (!islower((u_char) *copy) && *copy != '@' && *copy != '?') {
1.30 nicm 375: while (*copy != ':' && *copy != '\0') {
376: switch (*copy) {
377: case '=':
378: errno = 0;
379: limit = strtoul(copy + 1, &endptr, 10);
380: if (errno == ERANGE && limit == ULONG_MAX)
381: goto fail;
382: copy = endptr;
383: break;
384: default:
385: copy++;
386: break;
387: }
388: }
389: if (*copy != ':')
390: goto fail;
391: copy++;
392: }
393:
1.1 nicm 394: /*
395: * Is this a conditional? If so, check it exists and extract either the
396: * first or second element. If not, look up the key directly.
397: */
398: if (*copy == '?') {
399: ptr = strchr(copy, ',');
400: if (ptr == NULL)
401: goto fail;
402: *ptr = '\0';
403:
404: value = format_find(ft, copy + 1);
1.52 nicm 405: if (value != NULL && *value != '\0' &&
406: (value[0] != '0' || value[1] != '\0')) {
1.1 nicm 407: value = ptr + 1;
408: ptr = strchr(value, ',');
409: if (ptr == NULL)
410: goto fail;
411: *ptr = '\0';
412: } else {
413: ptr = strchr(ptr + 1, ',');
414: if (ptr == NULL)
415: goto fail;
416: value = ptr + 1;
417: }
1.31 nicm 418: saved = format_expand(ft, value);
419: value = saved;
1.1 nicm 420: } else {
421: value = format_find(ft, copy);
422: if (value == NULL)
423: value = "";
1.31 nicm 424: saved = NULL;
1.1 nicm 425: }
426:
1.30 nicm 427: /* Truncate the value if needed. */
1.44 nicm 428: if (limit != 0) {
429: value = trimmed = utf8_trimcstr(value, limit);
430: free(saved);
431: saved = trimmed;
432: }
433: valuelen = strlen(value);
1.30 nicm 434:
1.1 nicm 435: /* Expand the buffer and copy in the value. */
436: while (*len - *off < valuelen + 1) {
1.50 nicm 437: *buf = xreallocarray(*buf, 2, *len);
1.1 nicm 438: *len *= 2;
439: }
440: memcpy(*buf + *off, value, valuelen);
441: *off += valuelen;
442:
1.31 nicm 443: free(saved);
1.30 nicm 444: free(copy0);
1.1 nicm 445: return (0);
446:
447: fail:
1.30 nicm 448: free(copy0);
1.1 nicm 449: return (-1);
450: }
451:
1.58 nicm 452: /* Expand keys in a template, passing through strftime first. */
453: char *
454: format_expand_time(struct format_tree *ft, const char *fmt, time_t t)
455: {
456: char *tmp, *expanded;
457: size_t tmplen;
458: struct tm *tm;
459:
1.67 nicm 460: if (fmt == NULL || *fmt == '\0')
1.58 nicm 461: return (xstrdup(""));
462:
463: tm = localtime(&t);
464:
465: tmp = NULL;
466: tmplen = strlen(fmt);
467:
468: do {
469: tmp = xreallocarray(tmp, 2, tmplen);
470: tmplen *= 2;
471: } while (strftime(tmp, tmplen, fmt, tm) == 0);
472:
473: expanded = format_expand(ft, tmp);
474: free(tmp);
475:
476: return (expanded);
477: }
478:
1.1 nicm 479: /* Expand keys in a template. */
480: char *
481: format_expand(struct format_tree *ft, const char *fmt)
482: {
1.69 nicm 483: char *buf, *tmp, *cmd;
1.31 nicm 484: const char *ptr, *s;
1.68 nicm 485: size_t off, len, n, slen;
1.31 nicm 486: int ch, brackets;
1.58 nicm 487:
488: if (fmt == NULL)
489: return (xstrdup(""));
1.1 nicm 490:
491: len = 64;
492: buf = xmalloc(len);
493: off = 0;
494:
495: while (*fmt != '\0') {
496: if (*fmt != '#') {
497: while (len - off < 2) {
1.50 nicm 498: buf = xreallocarray(buf, 2, len);
1.1 nicm 499: len *= 2;
500: }
501: buf[off++] = *fmt++;
502: continue;
503: }
504: fmt++;
505:
506: ch = (u_char) *fmt++;
507: switch (ch) {
1.68 nicm 508: case '(':
509: brackets = 1;
510: for (ptr = fmt; *ptr != '\0'; ptr++) {
511: if (*ptr == '(')
512: brackets++;
513: if (*ptr == ')' && --brackets == 0)
514: break;
515: }
516: if (*ptr != ')' || brackets != 0)
517: break;
518: n = ptr - fmt;
519:
520: tmp = xmalloc(n + 1);
521: memcpy(tmp, fmt, n);
522: tmp[n] = '\0';
1.69 nicm 523: cmd = format_expand(ft, tmp);
1.68 nicm 524:
1.69 nicm 525: s = format_job_get(ft, cmd);
1.68 nicm 526: slen = strlen(s);
1.69 nicm 527:
528: free(cmd);
529: free(tmp);
1.68 nicm 530:
531: while (len - off < slen + 1) {
532: buf = xreallocarray(buf, 2, len);
533: len *= 2;
534: }
535: memcpy(buf + off, s, slen);
536: off += slen;
537:
538: fmt += n + 1;
539: continue;
1.1 nicm 540: case '{':
1.31 nicm 541: brackets = 1;
542: for (ptr = fmt; *ptr != '\0'; ptr++) {
543: if (*ptr == '{')
544: brackets++;
545: if (*ptr == '}' && --brackets == 0)
546: break;
547: }
548: if (*ptr != '}' || brackets != 0)
1.1 nicm 549: break;
550: n = ptr - fmt;
551:
552: if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
553: break;
554: fmt += n + 1;
1.40 nicm 555: continue;
556: case '#':
557: while (len - off < 2) {
1.50 nicm 558: buf = xreallocarray(buf, 2, len);
1.40 nicm 559: len *= 2;
560: }
561: buf[off++] = '#';
1.1 nicm 562: continue;
563: default:
1.25 nicm 564: s = NULL;
565: if (ch >= 'A' && ch <= 'Z')
566: s = format_upper[ch - 'A'];
567: else if (ch >= 'a' && ch <= 'z')
568: s = format_lower[ch - 'a'];
569: if (s == NULL) {
570: while (len - off < 3) {
1.50 nicm 571: buf = xreallocarray(buf, 2, len);
1.25 nicm 572: len *= 2;
1.1 nicm 573: }
1.25 nicm 574: buf[off++] = '#';
575: buf[off++] = ch;
576: continue;
1.1 nicm 577: }
1.25 nicm 578: n = strlen(s);
579: if (format_replace(ft, s, n, &buf, &len, &off) != 0)
580: break;
1.1 nicm 581: continue;
582: }
583:
584: break;
585: }
586: buf[off] = '\0';
587:
588: return (buf);
589: }
590:
1.32 nicm 591: /* Get command name for format. */
592: char *
593: format_get_command(struct window_pane *wp)
594: {
1.37 nicm 595: char *cmd, *out;
1.32 nicm 596:
597: cmd = get_proc_name(wp->fd, wp->tty);
598: if (cmd == NULL || *cmd == '\0') {
1.39 nicm 599: free(cmd);
1.46 nicm 600: cmd = cmd_stringify_argv(wp->argc, wp->argv);
1.39 nicm 601: if (cmd == NULL || *cmd == '\0') {
602: free(cmd);
1.38 nicm 603: cmd = xstrdup(wp->shell);
1.39 nicm 604: }
1.32 nicm 605: }
1.37 nicm 606: out = parse_window_name(cmd);
607: free(cmd);
608: return (out);
1.32 nicm 609: }
610:
1.60 nicm 611: /* Get time as a string. */
612: char *
613: format_time_string(time_t t)
614: {
615: char *tim;
616:
617: tim = ctime(&t);
618: *strchr(tim, '\n') = '\0';
619:
620: return (tim);
621: }
622:
1.57 nicm 623: /* Set defaults for any of arguments that are not NULL. */
624: void
625: format_defaults(struct format_tree *ft, struct client *c, struct session *s,
626: struct winlink *wl, struct window_pane *wp)
627: {
628: if (s == NULL && c != NULL)
629: s = c->session;
630: if (wl == NULL && s != NULL)
631: wl = s->curw;
632: if (wp == NULL && wl != NULL)
633: wp = wl->window->active;
634:
635: if (c != NULL)
636: format_defaults_client(ft, c);
637: if (s != NULL)
638: format_defaults_session(ft, s);
639: if (s != NULL && wl != NULL)
640: format_defaults_winlink(ft, s, wl);
641: if (wp != NULL)
642: format_defaults_pane(ft, wp);
643: }
644:
1.1 nicm 645: /* Set default format keys for a session. */
646: void
1.57 nicm 647: format_defaults_session(struct format_tree *ft, struct session *s)
1.1 nicm 648: {
649: struct session_group *sg;
650: time_t t;
1.66 nicm 651: struct winlink *wl;
652: char alerts[256], tmp[16];
1.1 nicm 653:
1.54 nicm 654: ft->s = s;
655:
1.1 nicm 656: format_add(ft, "session_name", "%s", s->name);
657: format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
658: format_add(ft, "session_width", "%u", s->sx);
659: format_add(ft, "session_height", "%u", s->sy);
1.23 nicm 660: format_add(ft, "session_id", "$%u", s->id);
1.1 nicm 661:
662: sg = session_group_find(s);
663: format_add(ft, "session_grouped", "%d", sg != NULL);
664: if (sg != NULL)
665: format_add(ft, "session_group", "%u", session_group_index(sg));
666:
667: t = s->creation_time.tv_sec;
1.24 deraadt 668: format_add(ft, "session_created", "%lld", (long long) t);
1.60 nicm 669: format_add(ft, "session_created_string", "%s", format_time_string(t));
670:
671: t = s->activity_time.tv_sec;
672: format_add(ft, "session_activity", "%lld", (long long) t);
673: format_add(ft, "session_activity_string", "%s", format_time_string(t));
1.1 nicm 674:
1.41 nicm 675: format_add(ft, "session_attached", "%u", s->attached);
1.59 nicm 676: format_add(ft, "session_many_attached", "%d", s->attached > 1);
1.66 nicm 677:
678: *alerts = '\0';
679: RB_FOREACH (wl, winlinks, &s->windows) {
680: if ((wl->flags & WINLINK_ALERTFLAGS) == 0)
681: continue;
1.74 nicm 682: xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
1.66 nicm 683:
684: if (*alerts != '\0')
685: strlcat(alerts, ",", sizeof alerts);
686: strlcat(alerts, tmp, sizeof alerts);
687: if (wl->flags & WINLINK_ACTIVITY)
688: strlcat(alerts, "#", sizeof alerts);
689: if (wl->flags & WINLINK_BELL)
690: strlcat(alerts, "!", sizeof alerts);
691: if (wl->flags & WINLINK_SILENCE)
692: strlcat(alerts, "~", sizeof alerts);
693: }
694: format_add(ft, "session_alerts", "%s", alerts);
1.3 nicm 695: }
696:
697: /* Set default format keys for a client. */
698: void
1.57 nicm 699: format_defaults_client(struct format_tree *ft, struct client *c)
1.3 nicm 700: {
1.60 nicm 701: struct session *s;
1.15 nicm 702: time_t t;
1.3 nicm 703:
1.54 nicm 704: if (ft->s == NULL)
705: ft->s = c->session;
706:
1.72 nicm 707: format_add(ft, "client_pid", "%ld", (long) c->pid);
1.6 nicm 708: format_add(ft, "client_height", "%u", c->tty.sy);
709: format_add(ft, "client_width", "%u", c->tty.sx);
1.27 nicm 710: if (c->tty.path != NULL)
711: format_add(ft, "client_tty", "%s", c->tty.path);
712: if (c->tty.termname != NULL)
713: format_add(ft, "client_termname", "%s", c->tty.termname);
1.75 ! nicm 714: format_add(ft, "client_control_mode", "%d",
! 715: !!(c->flags & CLIENT_CONTROL));
1.3 nicm 716:
717: t = c->creation_time.tv_sec;
1.24 deraadt 718: format_add(ft, "client_created", "%lld", (long long) t);
1.60 nicm 719: format_add(ft, "client_created_string", "%s", format_time_string(t));
1.3 nicm 720:
721: t = c->activity_time.tv_sec;
1.24 deraadt 722: format_add(ft, "client_activity", "%lld", (long long) t);
1.60 nicm 723: format_add(ft, "client_activity_string", "%s", format_time_string(t));
1.14 nicm 724:
1.61 nicm 725: if (strcmp(c->keytable->name, "root") == 0)
726: format_add(ft, "client_prefix", "%d", 0);
727: else
728: format_add(ft, "client_prefix", "%d", 1);
729: format_add(ft, "client_key_table", "%s", c->keytable->name);
1.3 nicm 730:
731: if (c->tty.flags & TTY_UTF8)
732: format_add(ft, "client_utf8", "%d", 1);
733: else
734: format_add(ft, "client_utf8", "%d", 0);
735:
736: if (c->flags & CLIENT_READONLY)
737: format_add(ft, "client_readonly", "%d", 1);
738: else
739: format_add(ft, "client_readonly", "%d", 0);
1.15 nicm 740:
741: s = c->session;
742: if (s != NULL)
743: format_add(ft, "client_session", "%s", s->name);
744: s = c->last_session;
745: if (s != NULL && session_alive(s))
746: format_add(ft, "client_last_session", "%s", s->name);
1.1 nicm 747: }
748:
1.32 nicm 749: /* Set default format keys for a window. */
750: void
1.57 nicm 751: format_defaults_window(struct format_tree *ft, struct window *w)
1.32 nicm 752: {
753: char *layout;
1.73 nicm 754: time_t t;
1.32 nicm 755:
1.54 nicm 756: ft->w = w;
757:
1.63 nicm 758: if (w->saved_layout_root != NULL)
759: layout = layout_dump(w->saved_layout_root);
760: else
761: layout = layout_dump(w->layout_root);
1.73 nicm 762:
763: t = w->activity_time.tv_sec;
764: format_add(ft, "window_activity", "%lld", (long long) t);
765: format_add(ft, "window_activity_string", "%s", format_time_string(t));
1.32 nicm 766:
767: format_add(ft, "window_id", "@%u", w->id);
768: format_add(ft, "window_name", "%s", w->name);
769: format_add(ft, "window_width", "%u", w->sx);
770: format_add(ft, "window_height", "%u", w->sy);
771: format_add(ft, "window_layout", "%s", layout);
772: format_add(ft, "window_panes", "%u", window_count_panes(w));
1.59 nicm 773: format_add(ft, "window_zoomed_flag", "%d",
1.53 nicm 774: !!(w->flags & WINDOW_ZOOMED));
1.32 nicm 775:
776: free(layout);
777: }
778:
1.1 nicm 779: /* Set default format keys for a winlink. */
780: void
1.57 nicm 781: format_defaults_winlink(struct format_tree *ft, struct session *s,
782: struct winlink *wl)
1.1 nicm 783: {
784: struct window *w = wl->window;
1.32 nicm 785: char *flags;
1.1 nicm 786:
1.54 nicm 787: if (ft->w == NULL)
788: ft->w = wl->window;
789:
1.1 nicm 790: flags = window_printable_flags(s, wl);
791:
1.57 nicm 792: format_defaults_window(ft, w);
1.32 nicm 793:
1.1 nicm 794: format_add(ft, "window_index", "%d", wl->idx);
795: format_add(ft, "window_flags", "%s", flags);
796: format_add(ft, "window_active", "%d", wl == s->curw);
1.29 nicm 797:
1.59 nicm 798: format_add(ft, "window_bell_flag", "%d",
1.29 nicm 799: !!(wl->flags & WINLINK_BELL));
1.59 nicm 800: format_add(ft, "window_activity_flag", "%d",
1.29 nicm 801: !!(wl->flags & WINLINK_ACTIVITY));
1.59 nicm 802: format_add(ft, "window_silence_flag", "%d",
1.29 nicm 803: !!(wl->flags & WINLINK_SILENCE));
1.59 nicm 804: format_add(ft, "window_last_flag", "%d",
1.49 nicm 805: !!(wl == TAILQ_FIRST(&s->lastw)));
1.64 nicm 806: format_add(ft, "window_linked", "%d", session_is_linked(s, wl->window));
1.32 nicm 807:
1.9 nicm 808: free(flags);
1.1 nicm 809: }
810:
1.19 nicm 811: /* Add window pane tabs. */
812: void
1.57 nicm 813: format_defaults_pane_tabs(struct format_tree *ft, struct window_pane *wp)
1.19 nicm 814: {
815: struct evbuffer *buffer;
816: u_int i;
817:
818: buffer = evbuffer_new();
819: for (i = 0; i < wp->base.grid->sx; i++) {
820: if (!bit_test(wp->base.tabs, i))
821: continue;
822:
823: if (EVBUFFER_LENGTH(buffer) > 0)
824: evbuffer_add(buffer, ",", 1);
1.59 nicm 825: evbuffer_add_printf(buffer, "%u", i);
1.19 nicm 826: }
827:
828: format_add(ft, "pane_tabs", "%.*s", (int) EVBUFFER_LENGTH(buffer),
829: EVBUFFER_DATA(buffer));
830: evbuffer_free(buffer);
831: }
832:
1.1 nicm 833: /* Set default format keys for a window pane. */
834: void
1.57 nicm 835: format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
1.1 nicm 836: {
837: struct grid *gd = wp->base.grid;
838: struct grid_line *gl;
839: unsigned long long size;
1.20 nicm 840: u_int i, idx;
1.21 nicm 841: char *cmd;
1.55 nicm 842: int status;
1.54 nicm 843:
844: if (ft->w == NULL)
845: ft->w = wp->window;
1.1 nicm 846:
847: size = 0;
848: for (i = 0; i < gd->hsize; i++) {
849: gl = &gd->linedata[i];
850: size += gl->cellsize * sizeof *gl->celldata;
851: }
852: size += gd->hsize * sizeof *gd->linedata;
1.16 nicm 853: format_add(ft, "history_size", "%u", gd->hsize);
854: format_add(ft, "history_limit", "%u", gd->hlimit);
855: format_add(ft, "history_bytes", "%llu", size);
1.1 nicm 856:
1.4 nicm 857: if (window_pane_index(wp, &idx) != 0)
858: fatalx("index not found");
1.16 nicm 859: format_add(ft, "pane_index", "%u", idx);
1.4 nicm 860:
1.1 nicm 861: format_add(ft, "pane_width", "%u", wp->sx);
862: format_add(ft, "pane_height", "%u", wp->sy);
863: format_add(ft, "pane_title", "%s", wp->base.title);
864: format_add(ft, "pane_id", "%%%u", wp->id);
865: format_add(ft, "pane_active", "%d", wp == wp->window->active);
1.55 nicm 866: format_add(ft, "pane_input_off", "%d", !!(wp->flags & PANE_INPUTOFF));
867:
868: status = wp->status;
869: if (wp->fd == -1 && WIFEXITED(status))
870: format_add(ft, "pane_dead_status", "%d", WEXITSTATUS(status));
1.1 nicm 871: format_add(ft, "pane_dead", "%d", wp->fd == -1);
1.47 nicm 872:
873: if (window_pane_visible(wp)) {
874: format_add(ft, "pane_left", "%u", wp->xoff);
875: format_add(ft, "pane_top", "%u", wp->yoff);
876: format_add(ft, "pane_right", "%u", wp->xoff + wp->sx - 1);
877: format_add(ft, "pane_bottom", "%u", wp->yoff + wp->sy - 1);
878: }
1.16 nicm 879:
880: format_add(ft, "pane_in_mode", "%d", wp->screen != &wp->base);
1.26 nicm 881: format_add(ft, "pane_synchronized", "%d",
882: !!options_get_number(&wp->window->options, "synchronize-panes"));
1.16 nicm 883:
1.71 nicm 884: format_add(ft, "pane_tty", "%s", wp->tty);
1.16 nicm 885: format_add(ft, "pane_pid", "%ld", (long) wp->pid);
1.46 nicm 886: if ((cmd = cmd_stringify_argv(wp->argc, wp->argv)) != NULL) {
887: format_add(ft, "pane_start_command", "%s", cmd);
888: free(cmd);
889: }
1.35 nicm 890: if ((cmd = format_get_command(wp)) != NULL) {
1.17 nicm 891: format_add(ft, "pane_current_command", "%s", cmd);
1.21 nicm 892: free(cmd);
893: }
1.16 nicm 894:
1.59 nicm 895: format_add(ft, "cursor_x", "%u", wp->base.cx);
896: format_add(ft, "cursor_y", "%u", wp->base.cy);
897: format_add(ft, "scroll_region_upper", "%u", wp->base.rupper);
898: format_add(ft, "scroll_region_lower", "%u", wp->base.rlower);
1.16 nicm 899:
900: format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0);
1.59 nicm 901: format_add(ft, "alternate_saved_x", "%u", wp->saved_cx);
902: format_add(ft, "alternate_saved_y", "%u", wp->saved_cy);
1.16 nicm 903:
904: format_add(ft, "cursor_flag", "%d",
905: !!(wp->base.mode & MODE_CURSOR));
906: format_add(ft, "insert_flag", "%d",
907: !!(wp->base.mode & MODE_INSERT));
908: format_add(ft, "keypad_cursor_flag", "%d",
909: !!(wp->base.mode & MODE_KCURSOR));
910: format_add(ft, "keypad_flag", "%d",
911: !!(wp->base.mode & MODE_KKEYPAD));
912: format_add(ft, "wrap_flag", "%d",
913: !!(wp->base.mode & MODE_WRAP));
914:
1.62 nicm 915: format_add(ft, "mouse_any_flag", "%d",
916: !!(wp->base.mode & (MODE_MOUSE_STANDARD|MODE_MOUSE_BUTTON)));
1.16 nicm 917: format_add(ft, "mouse_standard_flag", "%d",
918: !!(wp->base.mode & MODE_MOUSE_STANDARD));
919: format_add(ft, "mouse_button_flag", "%d",
920: !!(wp->base.mode & MODE_MOUSE_BUTTON));
921: format_add(ft, "mouse_utf8_flag", "%d",
922: !!(wp->base.mode & MODE_MOUSE_UTF8));
1.19 nicm 923:
1.57 nicm 924: format_defaults_pane_tabs(ft, wp);
1.8 nicm 925: }
926:
1.19 nicm 927: /* Set default format keys for paste buffer. */
1.8 nicm 928: void
1.57 nicm 929: format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb,
1.42 nicm 930: int utf8flag)
1.8 nicm 931: {
1.42 nicm 932: char *s;
1.8 nicm 933:
934: format_add(ft, "buffer_size", "%zu", pb->size);
1.45 nicm 935: format_add(ft, "buffer_name", "%s", pb->name);
1.8 nicm 936:
1.42 nicm 937: s = paste_make_sample(pb, utf8flag);
938: format_add(ft, "buffer_sample", "%s", s);
939: free(s);
1.1 nicm 940: }