Annotation of src/usr.bin/tmux/format.c, Revision 1.76
1.76 ! nicm 1: /* $OpenBSD: format.c,v 1.75 2015/07/13 15:37:26 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;
1.76 ! nicm 325: struct environ_entry *envent;
1.54 nicm 326: static char s[16];
327:
328: o = options_find(&global_options, key);
329: if (o == NULL && ft->w != NULL)
330: o = options_find(&ft->w->options, key);
331: if (o == NULL)
332: o = options_find(&global_w_options, key);
333: if (o == NULL && ft->s != NULL)
334: o = options_find(&ft->s->options, key);
335: if (o == NULL)
336: o = options_find(&global_s_options, key);
337: if (o != NULL) {
338: switch (o->type) {
339: case OPTIONS_STRING:
340: return (o->str);
341: case OPTIONS_NUMBER:
1.74 nicm 342: xsnprintf(s, sizeof s, "%lld", o->num);
1.54 nicm 343: return (s);
344: case OPTIONS_STYLE:
345: return (style_tostring(&o->style));
346: }
347: }
1.1 nicm 348:
349: fe_find.key = (char *) key;
1.68 nicm 350: fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find);
1.76 ! nicm 351: if (fe != NULL)
! 352: return (fe->value);
! 353:
! 354: envent = NULL;
! 355: if (ft->s != NULL)
! 356: envent = environ_find(&ft->s->environ, key);
! 357: if (envent == NULL)
! 358: envent = environ_find(&global_environ, key);
! 359: if (envent != NULL)
! 360: return (envent->value);
! 361:
! 362: return (NULL);
1.1 nicm 363: }
364:
365: /*
366: * Replace a key/value pair in buffer. #{blah} is expanded directly,
367: * #{?blah,a,b} is replace with a if blah exists and is nonzero else b.
368: */
369: int
1.30 nicm 370: format_replace(struct format_tree *ft, const char *key, size_t keylen,
371: char **buf, size_t *len, size_t *off)
1.1 nicm 372: {
1.44 nicm 373: char *copy, *copy0, *endptr, *ptr, *saved, *trimmed;
1.1 nicm 374: const char *value;
375: size_t valuelen;
1.44 nicm 376: u_long limit = 0;
1.1 nicm 377:
378: /* Make a copy of the key. */
1.30 nicm 379: copy0 = copy = xmalloc(keylen + 1);
1.1 nicm 380: memcpy(copy, key, keylen);
381: copy[keylen] = '\0';
382:
1.30 nicm 383: /* Is there a length limit or whatnot? */
1.76 ! nicm 384: if (!isalpha((u_char) *copy) && *copy != '@' && *copy != '?') {
1.30 nicm 385: while (*copy != ':' && *copy != '\0') {
386: switch (*copy) {
387: case '=':
388: errno = 0;
389: limit = strtoul(copy + 1, &endptr, 10);
390: if (errno == ERANGE && limit == ULONG_MAX)
391: goto fail;
392: copy = endptr;
393: break;
394: default:
395: copy++;
396: break;
397: }
398: }
399: if (*copy != ':')
400: goto fail;
401: copy++;
402: }
403:
1.1 nicm 404: /*
405: * Is this a conditional? If so, check it exists and extract either the
406: * first or second element. If not, look up the key directly.
407: */
408: if (*copy == '?') {
409: ptr = strchr(copy, ',');
410: if (ptr == NULL)
411: goto fail;
412: *ptr = '\0';
413:
414: value = format_find(ft, copy + 1);
1.52 nicm 415: if (value != NULL && *value != '\0' &&
416: (value[0] != '0' || value[1] != '\0')) {
1.1 nicm 417: value = ptr + 1;
418: ptr = strchr(value, ',');
419: if (ptr == NULL)
420: goto fail;
421: *ptr = '\0';
422: } else {
423: ptr = strchr(ptr + 1, ',');
424: if (ptr == NULL)
425: goto fail;
426: value = ptr + 1;
427: }
1.31 nicm 428: saved = format_expand(ft, value);
429: value = saved;
1.1 nicm 430: } else {
431: value = format_find(ft, copy);
432: if (value == NULL)
433: value = "";
1.31 nicm 434: saved = NULL;
1.1 nicm 435: }
436:
1.30 nicm 437: /* Truncate the value if needed. */
1.44 nicm 438: if (limit != 0) {
439: value = trimmed = utf8_trimcstr(value, limit);
440: free(saved);
441: saved = trimmed;
442: }
443: valuelen = strlen(value);
1.30 nicm 444:
1.1 nicm 445: /* Expand the buffer and copy in the value. */
446: while (*len - *off < valuelen + 1) {
1.50 nicm 447: *buf = xreallocarray(*buf, 2, *len);
1.1 nicm 448: *len *= 2;
449: }
450: memcpy(*buf + *off, value, valuelen);
451: *off += valuelen;
452:
1.31 nicm 453: free(saved);
1.30 nicm 454: free(copy0);
1.1 nicm 455: return (0);
456:
457: fail:
1.30 nicm 458: free(copy0);
1.1 nicm 459: return (-1);
460: }
461:
1.58 nicm 462: /* Expand keys in a template, passing through strftime first. */
463: char *
464: format_expand_time(struct format_tree *ft, const char *fmt, time_t t)
465: {
466: char *tmp, *expanded;
467: size_t tmplen;
468: struct tm *tm;
469:
1.67 nicm 470: if (fmt == NULL || *fmt == '\0')
1.58 nicm 471: return (xstrdup(""));
472:
473: tm = localtime(&t);
474:
475: tmp = NULL;
476: tmplen = strlen(fmt);
477:
478: do {
479: tmp = xreallocarray(tmp, 2, tmplen);
480: tmplen *= 2;
481: } while (strftime(tmp, tmplen, fmt, tm) == 0);
482:
483: expanded = format_expand(ft, tmp);
484: free(tmp);
485:
486: return (expanded);
487: }
488:
1.1 nicm 489: /* Expand keys in a template. */
490: char *
491: format_expand(struct format_tree *ft, const char *fmt)
492: {
1.69 nicm 493: char *buf, *tmp, *cmd;
1.31 nicm 494: const char *ptr, *s;
1.68 nicm 495: size_t off, len, n, slen;
1.31 nicm 496: int ch, brackets;
1.58 nicm 497:
498: if (fmt == NULL)
499: return (xstrdup(""));
1.1 nicm 500:
501: len = 64;
502: buf = xmalloc(len);
503: off = 0;
504:
505: while (*fmt != '\0') {
506: if (*fmt != '#') {
507: while (len - off < 2) {
1.50 nicm 508: buf = xreallocarray(buf, 2, len);
1.1 nicm 509: len *= 2;
510: }
511: buf[off++] = *fmt++;
512: continue;
513: }
514: fmt++;
515:
516: ch = (u_char) *fmt++;
517: switch (ch) {
1.68 nicm 518: case '(':
519: brackets = 1;
520: for (ptr = fmt; *ptr != '\0'; ptr++) {
521: if (*ptr == '(')
522: brackets++;
523: if (*ptr == ')' && --brackets == 0)
524: break;
525: }
526: if (*ptr != ')' || brackets != 0)
527: break;
528: n = ptr - fmt;
529:
530: tmp = xmalloc(n + 1);
531: memcpy(tmp, fmt, n);
532: tmp[n] = '\0';
1.69 nicm 533: cmd = format_expand(ft, tmp);
1.68 nicm 534:
1.69 nicm 535: s = format_job_get(ft, cmd);
1.68 nicm 536: slen = strlen(s);
1.69 nicm 537:
538: free(cmd);
539: free(tmp);
1.68 nicm 540:
541: while (len - off < slen + 1) {
542: buf = xreallocarray(buf, 2, len);
543: len *= 2;
544: }
545: memcpy(buf + off, s, slen);
546: off += slen;
547:
548: fmt += n + 1;
549: continue;
1.1 nicm 550: case '{':
1.31 nicm 551: brackets = 1;
552: for (ptr = fmt; *ptr != '\0'; ptr++) {
553: if (*ptr == '{')
554: brackets++;
555: if (*ptr == '}' && --brackets == 0)
556: break;
557: }
558: if (*ptr != '}' || brackets != 0)
1.1 nicm 559: break;
560: n = ptr - fmt;
561:
562: if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
563: break;
564: fmt += n + 1;
1.40 nicm 565: continue;
566: case '#':
567: while (len - off < 2) {
1.50 nicm 568: buf = xreallocarray(buf, 2, len);
1.40 nicm 569: len *= 2;
570: }
571: buf[off++] = '#';
1.1 nicm 572: continue;
573: default:
1.25 nicm 574: s = NULL;
575: if (ch >= 'A' && ch <= 'Z')
576: s = format_upper[ch - 'A'];
577: else if (ch >= 'a' && ch <= 'z')
578: s = format_lower[ch - 'a'];
579: if (s == NULL) {
580: while (len - off < 3) {
1.50 nicm 581: buf = xreallocarray(buf, 2, len);
1.25 nicm 582: len *= 2;
1.1 nicm 583: }
1.25 nicm 584: buf[off++] = '#';
585: buf[off++] = ch;
586: continue;
1.1 nicm 587: }
1.25 nicm 588: n = strlen(s);
589: if (format_replace(ft, s, n, &buf, &len, &off) != 0)
590: break;
1.1 nicm 591: continue;
592: }
593:
594: break;
595: }
596: buf[off] = '\0';
597:
598: return (buf);
599: }
600:
1.32 nicm 601: /* Get command name for format. */
602: char *
603: format_get_command(struct window_pane *wp)
604: {
1.37 nicm 605: char *cmd, *out;
1.32 nicm 606:
607: cmd = get_proc_name(wp->fd, wp->tty);
608: if (cmd == NULL || *cmd == '\0') {
1.39 nicm 609: free(cmd);
1.46 nicm 610: cmd = cmd_stringify_argv(wp->argc, wp->argv);
1.39 nicm 611: if (cmd == NULL || *cmd == '\0') {
612: free(cmd);
1.38 nicm 613: cmd = xstrdup(wp->shell);
1.39 nicm 614: }
1.32 nicm 615: }
1.37 nicm 616: out = parse_window_name(cmd);
617: free(cmd);
618: return (out);
1.32 nicm 619: }
620:
1.60 nicm 621: /* Get time as a string. */
622: char *
623: format_time_string(time_t t)
624: {
625: char *tim;
626:
627: tim = ctime(&t);
628: *strchr(tim, '\n') = '\0';
629:
630: return (tim);
631: }
632:
1.57 nicm 633: /* Set defaults for any of arguments that are not NULL. */
634: void
635: format_defaults(struct format_tree *ft, struct client *c, struct session *s,
636: struct winlink *wl, struct window_pane *wp)
637: {
638: if (s == NULL && c != NULL)
639: s = c->session;
640: if (wl == NULL && s != NULL)
641: wl = s->curw;
642: if (wp == NULL && wl != NULL)
643: wp = wl->window->active;
644:
645: if (c != NULL)
646: format_defaults_client(ft, c);
647: if (s != NULL)
648: format_defaults_session(ft, s);
649: if (s != NULL && wl != NULL)
650: format_defaults_winlink(ft, s, wl);
651: if (wp != NULL)
652: format_defaults_pane(ft, wp);
653: }
654:
1.1 nicm 655: /* Set default format keys for a session. */
656: void
1.57 nicm 657: format_defaults_session(struct format_tree *ft, struct session *s)
1.1 nicm 658: {
659: struct session_group *sg;
660: time_t t;
1.66 nicm 661: struct winlink *wl;
662: char alerts[256], tmp[16];
1.1 nicm 663:
1.54 nicm 664: ft->s = s;
665:
1.1 nicm 666: format_add(ft, "session_name", "%s", s->name);
667: format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
668: format_add(ft, "session_width", "%u", s->sx);
669: format_add(ft, "session_height", "%u", s->sy);
1.23 nicm 670: format_add(ft, "session_id", "$%u", s->id);
1.1 nicm 671:
672: sg = session_group_find(s);
673: format_add(ft, "session_grouped", "%d", sg != NULL);
674: if (sg != NULL)
675: format_add(ft, "session_group", "%u", session_group_index(sg));
676:
677: t = s->creation_time.tv_sec;
1.24 deraadt 678: format_add(ft, "session_created", "%lld", (long long) t);
1.60 nicm 679: format_add(ft, "session_created_string", "%s", format_time_string(t));
680:
681: t = s->activity_time.tv_sec;
682: format_add(ft, "session_activity", "%lld", (long long) t);
683: format_add(ft, "session_activity_string", "%s", format_time_string(t));
1.1 nicm 684:
1.41 nicm 685: format_add(ft, "session_attached", "%u", s->attached);
1.59 nicm 686: format_add(ft, "session_many_attached", "%d", s->attached > 1);
1.66 nicm 687:
688: *alerts = '\0';
689: RB_FOREACH (wl, winlinks, &s->windows) {
690: if ((wl->flags & WINLINK_ALERTFLAGS) == 0)
691: continue;
1.74 nicm 692: xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
1.66 nicm 693:
694: if (*alerts != '\0')
695: strlcat(alerts, ",", sizeof alerts);
696: strlcat(alerts, tmp, sizeof alerts);
697: if (wl->flags & WINLINK_ACTIVITY)
698: strlcat(alerts, "#", sizeof alerts);
699: if (wl->flags & WINLINK_BELL)
700: strlcat(alerts, "!", sizeof alerts);
701: if (wl->flags & WINLINK_SILENCE)
702: strlcat(alerts, "~", sizeof alerts);
703: }
704: format_add(ft, "session_alerts", "%s", alerts);
1.3 nicm 705: }
706:
707: /* Set default format keys for a client. */
708: void
1.57 nicm 709: format_defaults_client(struct format_tree *ft, struct client *c)
1.3 nicm 710: {
1.60 nicm 711: struct session *s;
1.15 nicm 712: time_t t;
1.3 nicm 713:
1.54 nicm 714: if (ft->s == NULL)
715: ft->s = c->session;
716:
1.72 nicm 717: format_add(ft, "client_pid", "%ld", (long) c->pid);
1.6 nicm 718: format_add(ft, "client_height", "%u", c->tty.sy);
719: format_add(ft, "client_width", "%u", c->tty.sx);
1.27 nicm 720: if (c->tty.path != NULL)
721: format_add(ft, "client_tty", "%s", c->tty.path);
722: if (c->tty.termname != NULL)
723: format_add(ft, "client_termname", "%s", c->tty.termname);
1.75 nicm 724: format_add(ft, "client_control_mode", "%d",
725: !!(c->flags & CLIENT_CONTROL));
1.3 nicm 726:
727: t = c->creation_time.tv_sec;
1.24 deraadt 728: format_add(ft, "client_created", "%lld", (long long) t);
1.60 nicm 729: format_add(ft, "client_created_string", "%s", format_time_string(t));
1.3 nicm 730:
731: t = c->activity_time.tv_sec;
1.24 deraadt 732: format_add(ft, "client_activity", "%lld", (long long) t);
1.60 nicm 733: format_add(ft, "client_activity_string", "%s", format_time_string(t));
1.14 nicm 734:
1.61 nicm 735: if (strcmp(c->keytable->name, "root") == 0)
736: format_add(ft, "client_prefix", "%d", 0);
737: else
738: format_add(ft, "client_prefix", "%d", 1);
739: format_add(ft, "client_key_table", "%s", c->keytable->name);
1.3 nicm 740:
741: if (c->tty.flags & TTY_UTF8)
742: format_add(ft, "client_utf8", "%d", 1);
743: else
744: format_add(ft, "client_utf8", "%d", 0);
745:
746: if (c->flags & CLIENT_READONLY)
747: format_add(ft, "client_readonly", "%d", 1);
748: else
749: format_add(ft, "client_readonly", "%d", 0);
1.15 nicm 750:
751: s = c->session;
752: if (s != NULL)
753: format_add(ft, "client_session", "%s", s->name);
754: s = c->last_session;
755: if (s != NULL && session_alive(s))
756: format_add(ft, "client_last_session", "%s", s->name);
1.1 nicm 757: }
758:
1.32 nicm 759: /* Set default format keys for a window. */
760: void
1.57 nicm 761: format_defaults_window(struct format_tree *ft, struct window *w)
1.32 nicm 762: {
763: char *layout;
1.73 nicm 764: time_t t;
1.32 nicm 765:
1.54 nicm 766: ft->w = w;
767:
1.63 nicm 768: if (w->saved_layout_root != NULL)
769: layout = layout_dump(w->saved_layout_root);
770: else
771: layout = layout_dump(w->layout_root);
1.73 nicm 772:
773: t = w->activity_time.tv_sec;
774: format_add(ft, "window_activity", "%lld", (long long) t);
775: format_add(ft, "window_activity_string", "%s", format_time_string(t));
1.32 nicm 776:
777: format_add(ft, "window_id", "@%u", w->id);
778: format_add(ft, "window_name", "%s", w->name);
779: format_add(ft, "window_width", "%u", w->sx);
780: format_add(ft, "window_height", "%u", w->sy);
781: format_add(ft, "window_layout", "%s", layout);
782: format_add(ft, "window_panes", "%u", window_count_panes(w));
1.59 nicm 783: format_add(ft, "window_zoomed_flag", "%d",
1.53 nicm 784: !!(w->flags & WINDOW_ZOOMED));
1.32 nicm 785:
786: free(layout);
787: }
788:
1.1 nicm 789: /* Set default format keys for a winlink. */
790: void
1.57 nicm 791: format_defaults_winlink(struct format_tree *ft, struct session *s,
792: struct winlink *wl)
1.1 nicm 793: {
794: struct window *w = wl->window;
1.32 nicm 795: char *flags;
1.1 nicm 796:
1.54 nicm 797: if (ft->w == NULL)
798: ft->w = wl->window;
799:
1.1 nicm 800: flags = window_printable_flags(s, wl);
801:
1.57 nicm 802: format_defaults_window(ft, w);
1.32 nicm 803:
1.1 nicm 804: format_add(ft, "window_index", "%d", wl->idx);
805: format_add(ft, "window_flags", "%s", flags);
806: format_add(ft, "window_active", "%d", wl == s->curw);
1.29 nicm 807:
1.59 nicm 808: format_add(ft, "window_bell_flag", "%d",
1.29 nicm 809: !!(wl->flags & WINLINK_BELL));
1.59 nicm 810: format_add(ft, "window_activity_flag", "%d",
1.29 nicm 811: !!(wl->flags & WINLINK_ACTIVITY));
1.59 nicm 812: format_add(ft, "window_silence_flag", "%d",
1.29 nicm 813: !!(wl->flags & WINLINK_SILENCE));
1.59 nicm 814: format_add(ft, "window_last_flag", "%d",
1.49 nicm 815: !!(wl == TAILQ_FIRST(&s->lastw)));
1.64 nicm 816: format_add(ft, "window_linked", "%d", session_is_linked(s, wl->window));
1.32 nicm 817:
1.9 nicm 818: free(flags);
1.1 nicm 819: }
820:
1.19 nicm 821: /* Add window pane tabs. */
822: void
1.57 nicm 823: format_defaults_pane_tabs(struct format_tree *ft, struct window_pane *wp)
1.19 nicm 824: {
825: struct evbuffer *buffer;
826: u_int i;
827:
828: buffer = evbuffer_new();
829: for (i = 0; i < wp->base.grid->sx; i++) {
830: if (!bit_test(wp->base.tabs, i))
831: continue;
832:
833: if (EVBUFFER_LENGTH(buffer) > 0)
834: evbuffer_add(buffer, ",", 1);
1.59 nicm 835: evbuffer_add_printf(buffer, "%u", i);
1.19 nicm 836: }
837:
838: format_add(ft, "pane_tabs", "%.*s", (int) EVBUFFER_LENGTH(buffer),
839: EVBUFFER_DATA(buffer));
840: evbuffer_free(buffer);
841: }
842:
1.1 nicm 843: /* Set default format keys for a window pane. */
844: void
1.57 nicm 845: format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
1.1 nicm 846: {
847: struct grid *gd = wp->base.grid;
848: struct grid_line *gl;
849: unsigned long long size;
1.20 nicm 850: u_int i, idx;
1.21 nicm 851: char *cmd;
1.55 nicm 852: int status;
1.54 nicm 853:
854: if (ft->w == NULL)
855: ft->w = wp->window;
1.1 nicm 856:
857: size = 0;
858: for (i = 0; i < gd->hsize; i++) {
859: gl = &gd->linedata[i];
860: size += gl->cellsize * sizeof *gl->celldata;
861: }
862: size += gd->hsize * sizeof *gd->linedata;
1.16 nicm 863: format_add(ft, "history_size", "%u", gd->hsize);
864: format_add(ft, "history_limit", "%u", gd->hlimit);
865: format_add(ft, "history_bytes", "%llu", size);
1.1 nicm 866:
1.4 nicm 867: if (window_pane_index(wp, &idx) != 0)
868: fatalx("index not found");
1.16 nicm 869: format_add(ft, "pane_index", "%u", idx);
1.4 nicm 870:
1.1 nicm 871: format_add(ft, "pane_width", "%u", wp->sx);
872: format_add(ft, "pane_height", "%u", wp->sy);
873: format_add(ft, "pane_title", "%s", wp->base.title);
874: format_add(ft, "pane_id", "%%%u", wp->id);
875: format_add(ft, "pane_active", "%d", wp == wp->window->active);
1.55 nicm 876: format_add(ft, "pane_input_off", "%d", !!(wp->flags & PANE_INPUTOFF));
877:
878: status = wp->status;
879: if (wp->fd == -1 && WIFEXITED(status))
880: format_add(ft, "pane_dead_status", "%d", WEXITSTATUS(status));
1.1 nicm 881: format_add(ft, "pane_dead", "%d", wp->fd == -1);
1.47 nicm 882:
883: if (window_pane_visible(wp)) {
884: format_add(ft, "pane_left", "%u", wp->xoff);
885: format_add(ft, "pane_top", "%u", wp->yoff);
886: format_add(ft, "pane_right", "%u", wp->xoff + wp->sx - 1);
887: format_add(ft, "pane_bottom", "%u", wp->yoff + wp->sy - 1);
888: }
1.16 nicm 889:
890: format_add(ft, "pane_in_mode", "%d", wp->screen != &wp->base);
1.26 nicm 891: format_add(ft, "pane_synchronized", "%d",
892: !!options_get_number(&wp->window->options, "synchronize-panes"));
1.16 nicm 893:
1.71 nicm 894: format_add(ft, "pane_tty", "%s", wp->tty);
1.16 nicm 895: format_add(ft, "pane_pid", "%ld", (long) wp->pid);
1.46 nicm 896: if ((cmd = cmd_stringify_argv(wp->argc, wp->argv)) != NULL) {
897: format_add(ft, "pane_start_command", "%s", cmd);
898: free(cmd);
899: }
1.35 nicm 900: if ((cmd = format_get_command(wp)) != NULL) {
1.17 nicm 901: format_add(ft, "pane_current_command", "%s", cmd);
1.21 nicm 902: free(cmd);
903: }
1.16 nicm 904:
1.59 nicm 905: format_add(ft, "cursor_x", "%u", wp->base.cx);
906: format_add(ft, "cursor_y", "%u", wp->base.cy);
907: format_add(ft, "scroll_region_upper", "%u", wp->base.rupper);
908: format_add(ft, "scroll_region_lower", "%u", wp->base.rlower);
1.16 nicm 909:
910: format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0);
1.59 nicm 911: format_add(ft, "alternate_saved_x", "%u", wp->saved_cx);
912: format_add(ft, "alternate_saved_y", "%u", wp->saved_cy);
1.16 nicm 913:
914: format_add(ft, "cursor_flag", "%d",
915: !!(wp->base.mode & MODE_CURSOR));
916: format_add(ft, "insert_flag", "%d",
917: !!(wp->base.mode & MODE_INSERT));
918: format_add(ft, "keypad_cursor_flag", "%d",
919: !!(wp->base.mode & MODE_KCURSOR));
920: format_add(ft, "keypad_flag", "%d",
921: !!(wp->base.mode & MODE_KKEYPAD));
922: format_add(ft, "wrap_flag", "%d",
923: !!(wp->base.mode & MODE_WRAP));
924:
1.62 nicm 925: format_add(ft, "mouse_any_flag", "%d",
926: !!(wp->base.mode & (MODE_MOUSE_STANDARD|MODE_MOUSE_BUTTON)));
1.16 nicm 927: format_add(ft, "mouse_standard_flag", "%d",
928: !!(wp->base.mode & MODE_MOUSE_STANDARD));
929: format_add(ft, "mouse_button_flag", "%d",
930: !!(wp->base.mode & MODE_MOUSE_BUTTON));
931: format_add(ft, "mouse_utf8_flag", "%d",
932: !!(wp->base.mode & MODE_MOUSE_UTF8));
1.19 nicm 933:
1.57 nicm 934: format_defaults_pane_tabs(ft, wp);
1.8 nicm 935: }
936:
1.19 nicm 937: /* Set default format keys for paste buffer. */
1.8 nicm 938: void
1.57 nicm 939: format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb,
1.42 nicm 940: int utf8flag)
1.8 nicm 941: {
1.42 nicm 942: char *s;
1.8 nicm 943:
944: format_add(ft, "buffer_size", "%zu", pb->size);
1.45 nicm 945: format_add(ft, "buffer_name", "%s", pb->name);
1.8 nicm 946:
1.42 nicm 947: s = paste_make_sample(pb, utf8flag);
948: format_add(ft, "buffer_sample", "%s", s);
949: free(s);
1.1 nicm 950: }