Annotation of src/usr.bin/tmux/format.c, Revision 1.68
1.68 ! nicm 1: /* $OpenBSD: format.c,v 1.67 2015/05/20 06:39:02 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: if (WIFEXITED(job->status) && WEXITSTATUS(job->status) != 0) {
! 179: xasprintf(&fj->out, "<'%s' exited with %d>", fj->cmd,
! 180: WEXITSTATUS(job->status));
! 181: return;
! 182: }
! 183: if (WIFSIGNALED(job->status)) {
! 184: xasprintf(&fj->out, "<'%s' got signal %d>", fj->cmd,
! 185: WTERMSIG(job->status));
! 186: return;
! 187: }
! 188:
! 189: buf = NULL;
! 190: if ((line = evbuffer_readline(job->event->input)) == NULL) {
! 191: len = EVBUFFER_LENGTH(job->event->input);
! 192: buf = xmalloc(len + 1);
! 193: if (len != 0)
! 194: memcpy(buf, EVBUFFER_DATA(job->event->input), len);
! 195: buf[len] = '\0';
! 196: } else
! 197: buf = line;
! 198: fj->out = buf;
! 199:
! 200: if (fj->status) {
! 201: TAILQ_FOREACH(c, &clients, entry)
! 202: server_status_client(c);
! 203: fj->status = 0;
! 204: }
! 205: }
! 206:
! 207: /* Find a job. */
! 208: const char *
! 209: format_job_get(struct format_tree *ft, const char *cmd)
! 210: {
! 211: struct format_job fj0, *fj;
! 212:
! 213: fj0.cmd = cmd;
! 214: if ((fj = RB_FIND(format_job_tree, &format_jobs, &fj0)) == NULL)
! 215: {
! 216: fj = xcalloc(1, sizeof *fj);
! 217: fj->cmd = xstrdup(cmd);
! 218: fj->status = ft->status;
! 219:
! 220: xasprintf(&fj->out, "<'%s' not ready>", fj->cmd);
! 221:
! 222: RB_INSERT(format_job_tree, &format_jobs, fj);
! 223: }
! 224:
! 225: if (fj->job == NULL && fj->last != time(NULL)) {
! 226: fj->job = job_run(fj->cmd, NULL, -1, format_job_callback,
! 227: NULL, fj);
! 228: if (fj->job == NULL) {
! 229: free(fj->out);
! 230: xasprintf(&fj->out, "<'%s' didn't start>", fj->cmd);
! 231: }
! 232: }
! 233: fj->last = time(NULL);
! 234:
! 235: return (fj->out);
! 236: }
! 237:
! 238: /* Remove old jobs. */
! 239: void
! 240: format_clean(void)
! 241: {
! 242: struct format_job *fj, *fj1;
! 243: time_t now;
! 244:
! 245: now = time(NULL);
! 246: RB_FOREACH_SAFE(fj, format_job_tree, &format_jobs, fj1) {
! 247: if (fj->last > now || now - fj->last < 3600)
! 248: continue;
! 249: RB_REMOVE(format_job_tree, &format_jobs, fj);
! 250:
! 251: if (fj->job != NULL)
! 252: job_free(fj->job);
! 253:
! 254: free((void*)fj->cmd);
! 255: free(fj->out);
! 256:
! 257: free(fj);
! 258: }
! 259: }
! 260:
1.1 nicm 261: /* Create a new tree. */
262: struct format_tree *
263: format_create(void)
264: {
1.68 ! nicm 265: return (format_create_status(0));
! 266: }
! 267:
! 268: /* Create a new tree for the status line. */
! 269: struct format_tree *
! 270: format_create_status(int status)
! 271: {
1.1 nicm 272: struct format_tree *ft;
1.68 ! nicm 273: char host[HOST_NAME_MAX + 1], *ptr;
1.1 nicm 274:
1.54 nicm 275: ft = xcalloc(1, sizeof *ft);
276: RB_INIT(&ft->tree);
1.68 ! nicm 277: ft->status = status;
1.1 nicm 278:
1.25 nicm 279: if (gethostname(host, sizeof host) == 0) {
1.1 nicm 280: format_add(ft, "host", "%s", host);
1.33 nicm 281: if ((ptr = strchr(host, '.')) != NULL)
1.25 nicm 282: *ptr = '\0';
283: format_add(ft, "host_short", "%s", host);
284: }
1.1 nicm 285:
286: return (ft);
287: }
288:
289: /* Free a tree. */
290: void
291: format_free(struct format_tree *ft)
292: {
1.54 nicm 293: struct format_entry *fe, *fe1;
1.1 nicm 294:
1.68 ! nicm 295: RB_FOREACH_SAFE(fe, format_entry_tree, &ft->tree, fe1) {
! 296: RB_REMOVE(format_entry_tree, &ft->tree, fe);
1.9 nicm 297: free(fe->value);
298: free(fe->key);
299: free(fe);
1.1 nicm 300: }
301:
1.25 nicm 302: free(ft);
1.1 nicm 303: }
304:
305: /* Add a key-value pair. */
306: void
307: format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
308: {
309: struct format_entry *fe;
1.28 nicm 310: struct format_entry *fe_now;
1.1 nicm 311: va_list ap;
312:
313: fe = xmalloc(sizeof *fe);
314: fe->key = xstrdup(key);
315:
316: va_start(ap, fmt);
317: xvasprintf(&fe->value, fmt, ap);
318: va_end(ap);
319:
1.68 ! nicm 320: fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
1.28 nicm 321: if (fe_now != NULL) {
322: free(fe_now->value);
323: fe_now->value = fe->value;
324: free(fe->key);
325: free(fe);
326: }
1.1 nicm 327: }
328:
329: /* Find a format entry. */
330: const char *
331: format_find(struct format_tree *ft, const char *key)
332: {
333: struct format_entry *fe, fe_find;
1.54 nicm 334: struct options_entry *o;
335: static char s[16];
336:
337: o = options_find(&global_options, key);
338: if (o == NULL && ft->w != NULL)
339: o = options_find(&ft->w->options, key);
340: if (o == NULL)
341: o = options_find(&global_w_options, key);
342: if (o == NULL && ft->s != NULL)
343: o = options_find(&ft->s->options, key);
344: if (o == NULL)
345: o = options_find(&global_s_options, key);
346: if (o != NULL) {
347: switch (o->type) {
348: case OPTIONS_STRING:
349: return (o->str);
350: case OPTIONS_NUMBER:
351: snprintf(s, sizeof s, "%lld", o->num);
352: return (s);
353: case OPTIONS_STYLE:
354: return (style_tostring(&o->style));
355: }
356: }
1.1 nicm 357:
358: fe_find.key = (char *) key;
1.68 ! nicm 359: fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find);
1.1 nicm 360: if (fe == NULL)
361: return (NULL);
362: return (fe->value);
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.54 nicm 384: if (!islower((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.68 ! nicm 493: char *buf, *tmp;
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';
! 533:
! 534: s = format_job_get(ft, tmp);
! 535: slen = strlen(s);
! 536:
! 537: while (len - off < slen + 1) {
! 538: buf = xreallocarray(buf, 2, len);
! 539: len *= 2;
! 540: }
! 541: memcpy(buf + off, s, slen);
! 542: off += slen;
! 543:
! 544: fmt += n + 1;
! 545: continue;
1.1 nicm 546: case '{':
1.31 nicm 547: brackets = 1;
548: for (ptr = fmt; *ptr != '\0'; ptr++) {
549: if (*ptr == '{')
550: brackets++;
551: if (*ptr == '}' && --brackets == 0)
552: break;
553: }
554: if (*ptr != '}' || brackets != 0)
1.1 nicm 555: break;
556: n = ptr - fmt;
557:
558: if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
559: break;
560: fmt += n + 1;
1.40 nicm 561: continue;
562: case '#':
563: while (len - off < 2) {
1.50 nicm 564: buf = xreallocarray(buf, 2, len);
1.40 nicm 565: len *= 2;
566: }
567: buf[off++] = '#';
1.1 nicm 568: continue;
569: default:
1.25 nicm 570: s = NULL;
571: if (ch >= 'A' && ch <= 'Z')
572: s = format_upper[ch - 'A'];
573: else if (ch >= 'a' && ch <= 'z')
574: s = format_lower[ch - 'a'];
575: if (s == NULL) {
576: while (len - off < 3) {
1.50 nicm 577: buf = xreallocarray(buf, 2, len);
1.25 nicm 578: len *= 2;
1.1 nicm 579: }
1.25 nicm 580: buf[off++] = '#';
581: buf[off++] = ch;
582: continue;
1.1 nicm 583: }
1.25 nicm 584: n = strlen(s);
585: if (format_replace(ft, s, n, &buf, &len, &off) != 0)
586: break;
1.1 nicm 587: continue;
588: }
589:
590: break;
591: }
592: buf[off] = '\0';
593:
594: return (buf);
595: }
596:
1.32 nicm 597: /* Get command name for format. */
598: char *
599: format_get_command(struct window_pane *wp)
600: {
1.37 nicm 601: char *cmd, *out;
1.32 nicm 602:
603: cmd = get_proc_name(wp->fd, wp->tty);
604: if (cmd == NULL || *cmd == '\0') {
1.39 nicm 605: free(cmd);
1.46 nicm 606: cmd = cmd_stringify_argv(wp->argc, wp->argv);
1.39 nicm 607: if (cmd == NULL || *cmd == '\0') {
608: free(cmd);
1.38 nicm 609: cmd = xstrdup(wp->shell);
1.39 nicm 610: }
1.32 nicm 611: }
1.37 nicm 612: out = parse_window_name(cmd);
613: free(cmd);
614: return (out);
1.32 nicm 615: }
616:
1.60 nicm 617: /* Get time as a string. */
618: char *
619: format_time_string(time_t t)
620: {
621: char *tim;
622:
623: tim = ctime(&t);
624: *strchr(tim, '\n') = '\0';
625:
626: return (tim);
627: }
628:
1.57 nicm 629: /* Set defaults for any of arguments that are not NULL. */
630: void
631: format_defaults(struct format_tree *ft, struct client *c, struct session *s,
632: struct winlink *wl, struct window_pane *wp)
633: {
634: if (s == NULL && c != NULL)
635: s = c->session;
636: if (wl == NULL && s != NULL)
637: wl = s->curw;
638: if (wp == NULL && wl != NULL)
639: wp = wl->window->active;
640:
641: if (c != NULL)
642: format_defaults_client(ft, c);
643: if (s != NULL)
644: format_defaults_session(ft, s);
645: if (s != NULL && wl != NULL)
646: format_defaults_winlink(ft, s, wl);
647: if (wp != NULL)
648: format_defaults_pane(ft, wp);
649: }
650:
1.1 nicm 651: /* Set default format keys for a session. */
652: void
1.57 nicm 653: format_defaults_session(struct format_tree *ft, struct session *s)
1.1 nicm 654: {
655: struct session_group *sg;
656: time_t t;
1.66 nicm 657: struct winlink *wl;
658: char alerts[256], tmp[16];
1.1 nicm 659:
1.54 nicm 660: ft->s = s;
661:
1.1 nicm 662: format_add(ft, "session_name", "%s", s->name);
663: format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
664: format_add(ft, "session_width", "%u", s->sx);
665: format_add(ft, "session_height", "%u", s->sy);
1.23 nicm 666: format_add(ft, "session_id", "$%u", s->id);
1.1 nicm 667:
668: sg = session_group_find(s);
669: format_add(ft, "session_grouped", "%d", sg != NULL);
670: if (sg != NULL)
671: format_add(ft, "session_group", "%u", session_group_index(sg));
672:
673: t = s->creation_time.tv_sec;
1.24 deraadt 674: format_add(ft, "session_created", "%lld", (long long) t);
1.60 nicm 675: format_add(ft, "session_created_string", "%s", format_time_string(t));
676:
677: t = s->activity_time.tv_sec;
678: format_add(ft, "session_activity", "%lld", (long long) t);
679: format_add(ft, "session_activity_string", "%s", format_time_string(t));
1.1 nicm 680:
1.41 nicm 681: format_add(ft, "session_attached", "%u", s->attached);
1.59 nicm 682: format_add(ft, "session_many_attached", "%d", s->attached > 1);
1.66 nicm 683:
684: *alerts = '\0';
685: RB_FOREACH (wl, winlinks, &s->windows) {
686: if ((wl->flags & WINLINK_ALERTFLAGS) == 0)
687: continue;
688: snprintf(tmp, sizeof tmp, "%u", wl->idx);
689:
690: if (*alerts != '\0')
691: strlcat(alerts, ",", sizeof alerts);
692: strlcat(alerts, tmp, sizeof alerts);
693: if (wl->flags & WINLINK_ACTIVITY)
694: strlcat(alerts, "#", sizeof alerts);
695: if (wl->flags & WINLINK_BELL)
696: strlcat(alerts, "!", sizeof alerts);
697: if (wl->flags & WINLINK_SILENCE)
698: strlcat(alerts, "~", sizeof alerts);
699: }
700: format_add(ft, "session_alerts", "%s", alerts);
1.3 nicm 701: }
702:
703: /* Set default format keys for a client. */
704: void
1.57 nicm 705: format_defaults_client(struct format_tree *ft, struct client *c)
1.3 nicm 706: {
1.60 nicm 707: struct session *s;
1.15 nicm 708: time_t t;
1.3 nicm 709:
1.54 nicm 710: if (ft->s == NULL)
711: ft->s = c->session;
712:
1.6 nicm 713: format_add(ft, "client_height", "%u", c->tty.sy);
714: format_add(ft, "client_width", "%u", c->tty.sx);
1.27 nicm 715: if (c->tty.path != NULL)
716: format_add(ft, "client_tty", "%s", c->tty.path);
717: if (c->tty.termname != NULL)
718: format_add(ft, "client_termname", "%s", c->tty.termname);
1.3 nicm 719:
720: t = c->creation_time.tv_sec;
1.24 deraadt 721: format_add(ft, "client_created", "%lld", (long long) t);
1.60 nicm 722: format_add(ft, "client_created_string", "%s", format_time_string(t));
1.3 nicm 723:
724: t = c->activity_time.tv_sec;
1.24 deraadt 725: format_add(ft, "client_activity", "%lld", (long long) t);
1.60 nicm 726: format_add(ft, "client_activity_string", "%s", format_time_string(t));
1.14 nicm 727:
1.61 nicm 728: if (strcmp(c->keytable->name, "root") == 0)
729: format_add(ft, "client_prefix", "%d", 0);
730: else
731: format_add(ft, "client_prefix", "%d", 1);
732: format_add(ft, "client_key_table", "%s", c->keytable->name);
1.3 nicm 733:
734: if (c->tty.flags & TTY_UTF8)
735: format_add(ft, "client_utf8", "%d", 1);
736: else
737: format_add(ft, "client_utf8", "%d", 0);
738:
739: if (c->flags & CLIENT_READONLY)
740: format_add(ft, "client_readonly", "%d", 1);
741: else
742: format_add(ft, "client_readonly", "%d", 0);
1.15 nicm 743:
744: s = c->session;
745: if (s != NULL)
746: format_add(ft, "client_session", "%s", s->name);
747: s = c->last_session;
748: if (s != NULL && session_alive(s))
749: format_add(ft, "client_last_session", "%s", s->name);
1.1 nicm 750: }
751:
1.32 nicm 752: /* Set default format keys for a window. */
753: void
1.57 nicm 754: format_defaults_window(struct format_tree *ft, struct window *w)
1.32 nicm 755: {
756: char *layout;
757:
1.54 nicm 758: ft->w = w;
759:
1.63 nicm 760: if (w->saved_layout_root != NULL)
761: layout = layout_dump(w->saved_layout_root);
762: else
763: layout = layout_dump(w->layout_root);
1.32 nicm 764:
765: format_add(ft, "window_id", "@%u", w->id);
766: format_add(ft, "window_name", "%s", w->name);
767: format_add(ft, "window_width", "%u", w->sx);
768: format_add(ft, "window_height", "%u", w->sy);
769: format_add(ft, "window_layout", "%s", layout);
770: format_add(ft, "window_panes", "%u", window_count_panes(w));
1.59 nicm 771: format_add(ft, "window_zoomed_flag", "%d",
1.53 nicm 772: !!(w->flags & WINDOW_ZOOMED));
1.32 nicm 773:
774: free(layout);
775: }
776:
1.1 nicm 777: /* Set default format keys for a winlink. */
778: void
1.57 nicm 779: format_defaults_winlink(struct format_tree *ft, struct session *s,
780: struct winlink *wl)
1.1 nicm 781: {
782: struct window *w = wl->window;
1.32 nicm 783: char *flags;
1.1 nicm 784:
1.54 nicm 785: if (ft->w == NULL)
786: ft->w = wl->window;
787:
1.1 nicm 788: flags = window_printable_flags(s, wl);
789:
1.57 nicm 790: format_defaults_window(ft, w);
1.32 nicm 791:
1.1 nicm 792: format_add(ft, "window_index", "%d", wl->idx);
793: format_add(ft, "window_flags", "%s", flags);
794: format_add(ft, "window_active", "%d", wl == s->curw);
1.29 nicm 795:
1.59 nicm 796: format_add(ft, "window_bell_flag", "%d",
1.29 nicm 797: !!(wl->flags & WINLINK_BELL));
1.59 nicm 798: format_add(ft, "window_activity_flag", "%d",
1.29 nicm 799: !!(wl->flags & WINLINK_ACTIVITY));
1.59 nicm 800: format_add(ft, "window_silence_flag", "%d",
1.29 nicm 801: !!(wl->flags & WINLINK_SILENCE));
1.59 nicm 802: format_add(ft, "window_last_flag", "%d",
1.49 nicm 803: !!(wl == TAILQ_FIRST(&s->lastw)));
1.64 nicm 804: format_add(ft, "window_linked", "%d", session_is_linked(s, wl->window));
1.32 nicm 805:
1.9 nicm 806: free(flags);
1.1 nicm 807: }
808:
1.19 nicm 809: /* Add window pane tabs. */
810: void
1.57 nicm 811: format_defaults_pane_tabs(struct format_tree *ft, struct window_pane *wp)
1.19 nicm 812: {
813: struct evbuffer *buffer;
814: u_int i;
815:
816: buffer = evbuffer_new();
817: for (i = 0; i < wp->base.grid->sx; i++) {
818: if (!bit_test(wp->base.tabs, i))
819: continue;
820:
821: if (EVBUFFER_LENGTH(buffer) > 0)
822: evbuffer_add(buffer, ",", 1);
1.59 nicm 823: evbuffer_add_printf(buffer, "%u", i);
1.19 nicm 824: }
825:
826: format_add(ft, "pane_tabs", "%.*s", (int) EVBUFFER_LENGTH(buffer),
827: EVBUFFER_DATA(buffer));
828: evbuffer_free(buffer);
829: }
830:
1.1 nicm 831: /* Set default format keys for a window pane. */
832: void
1.57 nicm 833: format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
1.1 nicm 834: {
835: struct grid *gd = wp->base.grid;
836: struct grid_line *gl;
837: unsigned long long size;
1.20 nicm 838: u_int i, idx;
1.21 nicm 839: char *cmd;
1.55 nicm 840: int status;
1.54 nicm 841:
842: if (ft->w == NULL)
843: ft->w = wp->window;
1.1 nicm 844:
845: size = 0;
846: for (i = 0; i < gd->hsize; i++) {
847: gl = &gd->linedata[i];
848: size += gl->cellsize * sizeof *gl->celldata;
849: }
850: size += gd->hsize * sizeof *gd->linedata;
1.16 nicm 851: format_add(ft, "history_size", "%u", gd->hsize);
852: format_add(ft, "history_limit", "%u", gd->hlimit);
853: format_add(ft, "history_bytes", "%llu", size);
1.1 nicm 854:
1.4 nicm 855: if (window_pane_index(wp, &idx) != 0)
856: fatalx("index not found");
1.16 nicm 857: format_add(ft, "pane_index", "%u", idx);
1.4 nicm 858:
1.1 nicm 859: format_add(ft, "pane_width", "%u", wp->sx);
860: format_add(ft, "pane_height", "%u", wp->sy);
861: format_add(ft, "pane_title", "%s", wp->base.title);
862: format_add(ft, "pane_id", "%%%u", wp->id);
863: format_add(ft, "pane_active", "%d", wp == wp->window->active);
1.55 nicm 864: format_add(ft, "pane_input_off", "%d", !!(wp->flags & PANE_INPUTOFF));
865:
866: status = wp->status;
867: if (wp->fd == -1 && WIFEXITED(status))
868: format_add(ft, "pane_dead_status", "%d", WEXITSTATUS(status));
1.1 nicm 869: format_add(ft, "pane_dead", "%d", wp->fd == -1);
1.47 nicm 870:
871: if (window_pane_visible(wp)) {
872: format_add(ft, "pane_left", "%u", wp->xoff);
873: format_add(ft, "pane_top", "%u", wp->yoff);
874: format_add(ft, "pane_right", "%u", wp->xoff + wp->sx - 1);
875: format_add(ft, "pane_bottom", "%u", wp->yoff + wp->sy - 1);
876: }
1.16 nicm 877:
878: format_add(ft, "pane_in_mode", "%d", wp->screen != &wp->base);
1.26 nicm 879: format_add(ft, "pane_synchronized", "%d",
880: !!options_get_number(&wp->window->options, "synchronize-panes"));
1.16 nicm 881:
882: if (wp->tty != NULL)
883: format_add(ft, "pane_tty", "%s", wp->tty);
884: format_add(ft, "pane_pid", "%ld", (long) wp->pid);
1.46 nicm 885: if ((cmd = cmd_stringify_argv(wp->argc, wp->argv)) != NULL) {
886: format_add(ft, "pane_start_command", "%s", cmd);
887: free(cmd);
888: }
1.35 nicm 889: if ((cmd = format_get_command(wp)) != NULL) {
1.17 nicm 890: format_add(ft, "pane_current_command", "%s", cmd);
1.21 nicm 891: free(cmd);
892: }
1.16 nicm 893:
1.59 nicm 894: format_add(ft, "cursor_x", "%u", wp->base.cx);
895: format_add(ft, "cursor_y", "%u", wp->base.cy);
896: format_add(ft, "scroll_region_upper", "%u", wp->base.rupper);
897: format_add(ft, "scroll_region_lower", "%u", wp->base.rlower);
1.16 nicm 898:
899: format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0);
1.59 nicm 900: format_add(ft, "alternate_saved_x", "%u", wp->saved_cx);
901: format_add(ft, "alternate_saved_y", "%u", wp->saved_cy);
1.16 nicm 902:
903: format_add(ft, "cursor_flag", "%d",
904: !!(wp->base.mode & MODE_CURSOR));
905: format_add(ft, "insert_flag", "%d",
906: !!(wp->base.mode & MODE_INSERT));
907: format_add(ft, "keypad_cursor_flag", "%d",
908: !!(wp->base.mode & MODE_KCURSOR));
909: format_add(ft, "keypad_flag", "%d",
910: !!(wp->base.mode & MODE_KKEYPAD));
911: format_add(ft, "wrap_flag", "%d",
912: !!(wp->base.mode & MODE_WRAP));
913:
1.62 nicm 914: format_add(ft, "mouse_any_flag", "%d",
915: !!(wp->base.mode & (MODE_MOUSE_STANDARD|MODE_MOUSE_BUTTON)));
1.16 nicm 916: format_add(ft, "mouse_standard_flag", "%d",
917: !!(wp->base.mode & MODE_MOUSE_STANDARD));
918: format_add(ft, "mouse_button_flag", "%d",
919: !!(wp->base.mode & MODE_MOUSE_BUTTON));
920: format_add(ft, "mouse_utf8_flag", "%d",
921: !!(wp->base.mode & MODE_MOUSE_UTF8));
1.19 nicm 922:
1.57 nicm 923: format_defaults_pane_tabs(ft, wp);
1.8 nicm 924: }
925:
1.19 nicm 926: /* Set default format keys for paste buffer. */
1.8 nicm 927: void
1.57 nicm 928: format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb,
1.42 nicm 929: int utf8flag)
1.8 nicm 930: {
1.42 nicm 931: char *s;
1.8 nicm 932:
933: format_add(ft, "buffer_size", "%zu", pb->size);
1.45 nicm 934: format_add(ft, "buffer_name", "%s", pb->name);
1.8 nicm 935:
1.42 nicm 936: s = paste_make_sample(pb, utf8flag);
937: format_add(ft, "buffer_sample", "%s", s);
938: free(s);
1.1 nicm 939: }