Annotation of src/usr.bin/tmux/format.c, Revision 1.134
1.134 ! nicm 1: /* $OpenBSD: format.c,v 1.133 2017/05/05 11:59:47 nicm Exp $ */
1.1 nicm 2:
3: /*
1.104 nicm 4: * Copyright (c) 2011 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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.87 nicm 24: #include <libgen.h>
1.1 nicm 25: #include <netdb.h>
26: #include <stdarg.h>
1.9 nicm 27: #include <stdlib.h>
1.1 nicm 28: #include <string.h>
29: #include <time.h>
30: #include <unistd.h>
31:
32: #include "tmux.h"
33:
34: /*
35: * Build a list of key-value pairs and use them to expand #{key} entries in a
36: * string.
37: */
38:
1.79 nicm 39: struct format_entry;
40: typedef void (*format_cb)(struct format_tree *, struct format_entry *);
41:
1.108 nicm 42: static char *format_job_get(struct format_tree *, const char *);
43: static void format_job_timer(int, short, void *);
44:
45: static char *format_find(struct format_tree *, const char *, int);
46: static void format_add_cb(struct format_tree *, const char *, format_cb);
47: static void format_add_tv(struct format_tree *, const char *,
48: struct timeval *);
49: static int format_replace(struct format_tree *, const char *, size_t,
50: char **, size_t *, size_t *);
51:
52: static void format_defaults_session(struct format_tree *,
53: struct session *);
54: static void format_defaults_client(struct format_tree *, struct client *);
1.129 nicm 55: static void format_defaults_winlink(struct format_tree *,
1.108 nicm 56: struct winlink *);
1.1 nicm 57:
1.68 nicm 58: /* Entry in format job tree. */
59: struct format_job {
1.131 nicm 60: struct client *client;
1.120 nicm 61: u_int tag;
1.68 nicm 62: const char *cmd;
1.113 nicm 63: const char *expanded;
1.68 nicm 64:
65: time_t last;
66: char *out;
1.127 nicm 67: int updated;
1.68 nicm 68:
69: struct job *job;
70: int status;
71:
72: RB_ENTRY(format_job) entry;
73: };
74:
75: /* Format job tree. */
1.108 nicm 76: static struct event format_job_event;
77: static int format_job_cmp(struct format_job *, struct format_job *);
1.109 nicm 78: static RB_HEAD(format_job_tree, format_job) format_jobs = RB_INITIALIZER();
1.108 nicm 79: RB_GENERATE_STATIC(format_job_tree, format_job, entry, format_job_cmp);
1.68 nicm 80:
81: /* Format job tree comparison function. */
1.108 nicm 82: static int
1.68 nicm 83: format_job_cmp(struct format_job *fj1, struct format_job *fj2)
84: {
1.120 nicm 85: if (fj1->tag < fj2->tag)
86: return (-1);
87: if (fj1->tag > fj2->tag)
88: return (1);
1.68 nicm 89: return (strcmp(fj1->cmd, fj2->cmd));
90: }
91:
1.87 nicm 92: /* Format modifiers. */
93: #define FORMAT_TIMESTRING 0x1
94: #define FORMAT_BASENAME 0x2
95: #define FORMAT_DIRNAME 0x4
1.98 nicm 96: #define FORMAT_SUBSTITUTE 0x8
1.87 nicm 97:
1.54 nicm 98: /* Entry in format tree. */
99: struct format_entry {
1.79 nicm 100: char *key;
101: char *value;
1.87 nicm 102: time_t t;
1.79 nicm 103: format_cb cb;
104: RB_ENTRY(format_entry) entry;
1.54 nicm 105: };
106:
1.68 nicm 107: /* Format entry tree. */
1.54 nicm 108: struct format_tree {
1.79 nicm 109: struct window *w;
1.133 nicm 110: struct winlink *wl;
1.79 nicm 111: struct session *s;
112: struct window_pane *wp;
1.54 nicm 113:
1.131 nicm 114: struct client *client;
1.120 nicm 115: u_int tag;
1.84 nicm 116: int flags;
1.68 nicm 117:
118: RB_HEAD(format_entry_tree, format_entry) tree;
1.54 nicm 119: };
1.108 nicm 120: static int format_entry_cmp(struct format_entry *, struct format_entry *);
121: RB_GENERATE_STATIC(format_entry_tree, format_entry, entry, format_entry_cmp);
1.54 nicm 122:
1.68 nicm 123: /* Format entry tree comparison function. */
1.108 nicm 124: static int
1.68 nicm 125: format_entry_cmp(struct format_entry *fe1, struct format_entry *fe2)
1.1 nicm 126: {
127: return (strcmp(fe1->key, fe2->key));
128: }
129:
1.25 nicm 130: /* Single-character uppercase aliases. */
1.108 nicm 131: static const char *format_upper[] = {
1.1 nicm 132: NULL, /* A */
133: NULL, /* B */
134: NULL, /* C */
135: "pane_id", /* D */
136: NULL, /* E */
137: "window_flags", /* F */
138: NULL, /* G */
139: "host", /* H */
140: "window_index", /* I */
141: NULL, /* J */
142: NULL, /* K */
143: NULL, /* L */
144: NULL, /* M */
145: NULL, /* N */
146: NULL, /* O */
147: "pane_index", /* P */
148: NULL, /* Q */
149: NULL, /* R */
150: "session_name", /* S */
151: "pane_title", /* T */
152: NULL, /* U */
153: NULL, /* V */
154: "window_name", /* W */
155: NULL, /* X */
156: NULL, /* Y */
157: NULL /* Z */
158: };
159:
1.25 nicm 160: /* Single-character lowercase aliases. */
1.108 nicm 161: static const char *format_lower[] = {
1.25 nicm 162: NULL, /* a */
163: NULL, /* b */
164: NULL, /* c */
165: NULL, /* d */
166: NULL, /* e */
167: NULL, /* f */
168: NULL, /* g */
169: "host_short", /* h */
170: NULL, /* i */
171: NULL, /* j */
172: NULL, /* k */
173: NULL, /* l */
174: NULL, /* m */
175: NULL, /* n */
176: NULL, /* o */
177: NULL, /* p */
178: NULL, /* q */
179: NULL, /* r */
180: NULL, /* s */
181: NULL, /* t */
182: NULL, /* u */
183: NULL, /* v */
184: NULL, /* w */
185: NULL, /* x */
186: NULL, /* y */
187: NULL /* z */
188: };
189:
1.127 nicm 190: /* Format job update callback. */
1.108 nicm 191: static void
1.127 nicm 192: format_job_update(struct job *job)
193: {
194: struct format_job *fj = job->data;
195: char *line;
196: time_t t;
197: struct client *c;
198:
199: if ((line = evbuffer_readline(job->event->input)) == NULL)
200: return;
201: fj->updated = 1;
202:
203: free(fj->out);
204: fj->out = line;
205:
206: log_debug("%s: %s: %s", __func__, fj->cmd, fj->out);
207:
208: t = time (NULL);
209: if (fj->status && fj->last != t) {
210: TAILQ_FOREACH(c, &clients, entry)
211: server_status_client(c);
212: fj->last = t;
213: }
214: }
215:
216: /* Format job complete callback. */
217: static void
218: format_job_complete(struct job *job)
1.68 nicm 219: {
220: struct format_job *fj = job->data;
221: char *line, *buf;
222: size_t len;
223:
224: fj->job = NULL;
225:
226: buf = NULL;
227: if ((line = evbuffer_readline(job->event->input)) == NULL) {
228: len = EVBUFFER_LENGTH(job->event->input);
229: buf = xmalloc(len + 1);
230: if (len != 0)
231: memcpy(buf, EVBUFFER_DATA(job->event->input), len);
232: buf[len] = '\0';
233: } else
234: buf = line;
1.127 nicm 235:
236: if (*buf != '\0' || !fj->updated) {
237: free(fj->out);
238: fj->out = buf;
239: log_debug("%s: %s: %s", __func__, fj->cmd, fj->out);
240: } else
241: free(buf);
1.68 nicm 242:
243: if (fj->status) {
1.131 nicm 244: if (fj->client != NULL)
245: server_status_client(fj->client);
1.68 nicm 246: fj->status = 0;
247: }
248: }
249:
250: /* Find a job. */
1.108 nicm 251: static char *
1.68 nicm 252: format_job_get(struct format_tree *ft, const char *cmd)
253: {
1.131 nicm 254: struct format_job_tree *jobs;
1.113 nicm 255: struct format_job fj0, *fj;
256: time_t t;
257: char *expanded;
258: int force;
1.68 nicm 259:
1.131 nicm 260: if (ft->client == NULL)
261: jobs = &format_jobs;
262: else if (ft->client->jobs != NULL)
263: jobs = ft->client->jobs;
264: else {
265: jobs = ft->client->jobs = xmalloc(sizeof *ft->client->jobs);
266: RB_INIT(jobs);
267: }
268:
1.120 nicm 269: fj0.tag = ft->tag;
1.68 nicm 270: fj0.cmd = cmd;
1.131 nicm 271: if ((fj = RB_FIND(format_job_tree, jobs, &fj0)) == NULL) {
1.68 nicm 272: fj = xcalloc(1, sizeof *fj);
1.131 nicm 273: fj->client = ft->client;
1.120 nicm 274: fj->tag = ft->tag;
1.68 nicm 275: fj->cmd = xstrdup(cmd);
1.113 nicm 276: fj->expanded = NULL;
1.68 nicm 277:
278: xasprintf(&fj->out, "<'%s' not ready>", fj->cmd);
279:
1.131 nicm 280: RB_INSERT(format_job_tree, jobs, fj);
1.68 nicm 281: }
282:
1.113 nicm 283: expanded = format_expand(ft, cmd);
284: if (fj->expanded == NULL || strcmp(expanded, fj->expanded) != 0) {
285: free((void *)fj->expanded);
286: fj->expanded = xstrdup(expanded);
287: force = 1;
288: } else
289: force = (ft->flags & FORMAT_FORCE);
290:
1.84 nicm 291: t = time(NULL);
1.113 nicm 292: if (fj->job == NULL && (force || fj->last != t)) {
1.127 nicm 293: fj->job = job_run(expanded, NULL, NULL, format_job_update,
294: format_job_complete, NULL, fj);
1.68 nicm 295: if (fj->job == NULL) {
296: free(fj->out);
297: xasprintf(&fj->out, "<'%s' didn't start>", fj->cmd);
298: }
1.84 nicm 299: fj->last = t;
1.68 nicm 300: }
1.84 nicm 301:
302: if (ft->flags & FORMAT_STATUS)
303: fj->status = 1;
1.68 nicm 304:
1.113 nicm 305: free(expanded);
1.86 nicm 306: return (format_expand(ft, fj->out));
1.68 nicm 307: }
308:
309: /* Remove old jobs. */
1.108 nicm 310: static void
1.131 nicm 311: format_job_tidy(struct format_job_tree *jobs, int force)
1.68 nicm 312: {
313: struct format_job *fj, *fj1;
314: time_t now;
315:
316: now = time(NULL);
1.131 nicm 317: RB_FOREACH_SAFE(fj, format_job_tree, jobs, fj1) {
318: if (!force && (fj->last > now || now - fj->last < 3600))
1.68 nicm 319: continue;
1.131 nicm 320: RB_REMOVE(format_job_tree, jobs, fj);
1.68 nicm 321:
1.77 nicm 322: log_debug("%s: %s", __func__, fj->cmd);
323:
1.68 nicm 324: if (fj->job != NULL)
325: job_free(fj->job);
326:
1.113 nicm 327: free((void *)fj->expanded);
1.78 nicm 328: free((void *)fj->cmd);
1.68 nicm 329: free(fj->out);
330:
331: free(fj);
332: }
1.131 nicm 333: }
334:
335: /* Remove old jobs for client. */
336: void
337: format_lost_client(struct client *c)
338: {
339: if (c->jobs != NULL)
340: format_job_tidy(c->jobs, 1);
341: free(c->jobs);
342: }
343:
344: /* Remove old jobs periodically. */
345: static void
346: format_job_timer(__unused int fd, __unused short events, __unused void *arg)
347: {
348: struct client *c;
349: struct timeval tv = { .tv_sec = 60 };
350:
351: format_job_tidy(&format_jobs, 0);
352: TAILQ_FOREACH(c, &clients, entry) {
353: if (c->jobs != NULL)
354: format_job_tidy(c->jobs, 0);
355: }
1.77 nicm 356:
357: evtimer_del(&format_job_event);
358: evtimer_add(&format_job_event, &tv);
1.68 nicm 359: }
360:
1.79 nicm 361: /* Callback for host. */
1.108 nicm 362: static void
1.99 nicm 363: format_cb_host(__unused struct format_tree *ft, struct format_entry *fe)
1.79 nicm 364: {
365: char host[HOST_NAME_MAX + 1];
366:
367: if (gethostname(host, sizeof host) != 0)
368: fe->value = xstrdup("");
369: else
370: fe->value = xstrdup(host);
371: }
372:
373: /* Callback for host_short. */
1.108 nicm 374: static void
1.99 nicm 375: format_cb_host_short(__unused struct format_tree *ft, struct format_entry *fe)
1.79 nicm 376: {
377: char host[HOST_NAME_MAX + 1], *cp;
378:
379: if (gethostname(host, sizeof host) != 0)
380: fe->value = xstrdup("");
381: else {
382: if ((cp = strchr(host, '.')) != NULL)
383: *cp = '\0';
384: fe->value = xstrdup(host);
385: }
386: }
387:
388: /* Callback for pid. */
1.108 nicm 389: static void
1.99 nicm 390: format_cb_pid(__unused struct format_tree *ft, struct format_entry *fe)
1.79 nicm 391: {
392: xasprintf(&fe->value, "%ld", (long)getpid());
393: }
394:
1.80 nicm 395: /* Callback for session_alerts. */
1.108 nicm 396: static void
1.80 nicm 397: format_cb_session_alerts(struct format_tree *ft, struct format_entry *fe)
398: {
399: struct session *s = ft->s;
400: struct winlink *wl;
1.133 nicm 401: char alerts[1024], tmp[16];
1.80 nicm 402:
403: if (s == NULL)
404: return;
405:
406: *alerts = '\0';
407: RB_FOREACH(wl, winlinks, &s->windows) {
408: if ((wl->flags & WINLINK_ALERTFLAGS) == 0)
409: continue;
410: xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
411:
412: if (*alerts != '\0')
413: strlcat(alerts, ",", sizeof alerts);
414: strlcat(alerts, tmp, sizeof alerts);
415: if (wl->flags & WINLINK_ACTIVITY)
416: strlcat(alerts, "#", sizeof alerts);
417: if (wl->flags & WINLINK_BELL)
418: strlcat(alerts, "!", sizeof alerts);
419: if (wl->flags & WINLINK_SILENCE)
420: strlcat(alerts, "~", sizeof alerts);
421: }
422: fe->value = xstrdup(alerts);
423: }
424:
1.133 nicm 425: /* Callback for session_stack. */
426: static void
427: format_cb_session_stack(struct format_tree *ft, struct format_entry *fe)
428: {
429: struct session *s = ft->s;
430: struct winlink *wl;
431: char result[1024], tmp[16];
432:
433: if (s == NULL)
434: return;
435:
436: xsnprintf(result, sizeof result, "%u", s->curw->idx);
437: TAILQ_FOREACH(wl, &s->lastw, sentry) {
438: xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
439:
440: if (*result != '\0')
441: strlcat(result, ",", sizeof result);
442: strlcat(result, tmp, sizeof result);
443: }
444: fe->value = xstrdup(result);
445: }
446:
447: /* Callback for window_stack_index. */
448: static void
449: format_cb_window_stack_index(struct format_tree *ft, struct format_entry *fe)
450: {
451: struct session *s = ft->wl->session;
452: struct winlink *wl;
453: u_int idx;
454:
455: idx = 0;
456: TAILQ_FOREACH(wl, &s->lastw, sentry) {
457: idx++;
458: if (wl == ft->wl)
459: break;
460: }
461: if (wl != NULL)
462: xasprintf(&fe->value, "%u", idx);
463: else
464: fe->value = xstrdup("0");
465: }
466:
1.80 nicm 467: /* Callback for window_layout. */
1.108 nicm 468: static void
1.80 nicm 469: format_cb_window_layout(struct format_tree *ft, struct format_entry *fe)
470: {
471: struct window *w = ft->w;
472:
473: if (w == NULL)
474: return;
475:
476: if (w->saved_layout_root != NULL)
477: fe->value = layout_dump(w->saved_layout_root);
478: else
479: fe->value = layout_dump(w->layout_root);
480: }
481:
1.96 nicm 482: /* Callback for window_visible_layout. */
1.108 nicm 483: static void
1.96 nicm 484: format_cb_window_visible_layout(struct format_tree *ft, struct format_entry *fe)
485: {
486: struct window *w = ft->w;
487:
488: if (w == NULL)
489: return;
490:
491: fe->value = layout_dump(w->layout_root);
492: }
493:
1.79 nicm 494: /* Callback for pane_start_command. */
1.108 nicm 495: static void
1.79 nicm 496: format_cb_start_command(struct format_tree *ft, struct format_entry *fe)
497: {
498: struct window_pane *wp = ft->wp;
499:
500: if (wp == NULL)
501: return;
502:
503: fe->value = cmd_stringify_argv(wp->argc, wp->argv);
504: }
505:
506: /* Callback for pane_current_command. */
1.108 nicm 507: static void
1.79 nicm 508: format_cb_current_command(struct format_tree *ft, struct format_entry *fe)
509: {
510: struct window_pane *wp = ft->wp;
511: char *cmd;
512:
513: if (wp == NULL)
514: return;
515:
516: cmd = get_proc_name(wp->fd, wp->tty);
517: if (cmd == NULL || *cmd == '\0') {
518: free(cmd);
519: cmd = cmd_stringify_argv(wp->argc, wp->argv);
520: if (cmd == NULL || *cmd == '\0') {
521: free(cmd);
522: cmd = xstrdup(wp->shell);
523: }
524: }
525: fe->value = parse_window_name(cmd);
526: free(cmd);
527: }
528:
1.80 nicm 529: /* Callback for history_bytes. */
1.108 nicm 530: static void
1.80 nicm 531: format_cb_history_bytes(struct format_tree *ft, struct format_entry *fe)
532: {
533: struct window_pane *wp = ft->wp;
534: struct grid *gd;
535: struct grid_line *gl;
536: unsigned long long size;
537: u_int i;
538:
539: if (wp == NULL)
540: return;
541: gd = wp->base.grid;
542:
543: size = 0;
544: for (i = 0; i < gd->hsize; i++) {
545: gl = &gd->linedata[i];
546: size += gl->cellsize * sizeof *gl->celldata;
1.95 nicm 547: size += gl->extdsize * sizeof *gl->extddata;
1.80 nicm 548: }
549: size += gd->hsize * sizeof *gd->linedata;
550:
551: xasprintf(&fe->value, "%llu", size);
552: }
553:
554: /* Callback for pane_tabs. */
1.108 nicm 555: static void
1.80 nicm 556: format_cb_pane_tabs(struct format_tree *ft, struct format_entry *fe)
557: {
558: struct window_pane *wp = ft->wp;
559: struct evbuffer *buffer;
560: u_int i;
561: int size;
562:
563: if (wp == NULL)
564: return;
565:
566: buffer = evbuffer_new();
567: for (i = 0; i < wp->base.grid->sx; i++) {
568: if (!bit_test(wp->base.tabs, i))
569: continue;
570:
571: if (EVBUFFER_LENGTH(buffer) > 0)
572: evbuffer_add(buffer, ",", 1);
573: evbuffer_add_printf(buffer, "%u", i);
574: }
575: size = EVBUFFER_LENGTH(buffer);
576: xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer));
577: evbuffer_free(buffer);
578: }
579:
1.112 nicm 580: /* Merge a format tree. */
581: static void
582: format_merge(struct format_tree *ft, struct format_tree *from)
583: {
584: struct format_entry *fe;
585:
586: RB_FOREACH(fe, format_entry_tree, &from->tree) {
587: if (fe->value != NULL)
588: format_add(ft, fe->key, "%s", fe->value);
589: }
590: }
591:
1.1 nicm 592: /* Create a new tree. */
593: struct format_tree *
1.131 nicm 594: format_create(struct client *c, struct cmdq_item *item, int tag, int flags)
1.68 nicm 595: {
1.1 nicm 596: struct format_tree *ft;
1.77 nicm 597:
598: if (!event_initialized(&format_job_event)) {
599: evtimer_set(&format_job_event, format_job_timer, NULL);
600: format_job_timer(-1, 0, NULL);
601: }
1.1 nicm 602:
1.54 nicm 603: ft = xcalloc(1, sizeof *ft);
604: RB_INIT(&ft->tree);
1.120 nicm 605:
1.131 nicm 606: if (c != NULL) {
607: ft->client = c;
608: ft->client->references++;
609: }
610:
1.120 nicm 611: ft->tag = tag;
1.84 nicm 612: ft->flags = flags;
1.1 nicm 613:
1.79 nicm 614: format_add_cb(ft, "host", format_cb_host);
615: format_add_cb(ft, "host_short", format_cb_host_short);
616: format_add_cb(ft, "pid", format_cb_pid);
1.100 nicm 617: format_add(ft, "socket_path", "%s", socket_path);
618: format_add_tv(ft, "start_time", &start_time);
1.102 nicm 619:
1.130 nicm 620: if (item != NULL) {
621: if (item->cmd != NULL)
622: format_add(ft, "command", "%s", item->cmd->entry->name);
623: if (item->shared != NULL && item->shared->formats != NULL)
624: format_merge(ft, item->shared->formats);
625: }
1.1 nicm 626:
627: return (ft);
628: }
629:
630: /* Free a tree. */
631: void
632: format_free(struct format_tree *ft)
633: {
1.54 nicm 634: struct format_entry *fe, *fe1;
1.1 nicm 635:
1.68 nicm 636: RB_FOREACH_SAFE(fe, format_entry_tree, &ft->tree, fe1) {
637: RB_REMOVE(format_entry_tree, &ft->tree, fe);
1.9 nicm 638: free(fe->value);
639: free(fe->key);
640: free(fe);
1.1 nicm 641: }
642:
1.131 nicm 643: if (ft->client != NULL)
644: server_client_unref(ft->client);
1.25 nicm 645: free(ft);
1.1 nicm 646: }
647:
648: /* Add a key-value pair. */
649: void
650: format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
651: {
652: struct format_entry *fe;
1.28 nicm 653: struct format_entry *fe_now;
1.1 nicm 654: va_list ap;
655:
656: fe = xmalloc(sizeof *fe);
657: fe->key = xstrdup(key);
658:
1.79 nicm 659: fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
660: if (fe_now != NULL) {
661: free(fe->key);
662: free(fe);
663: free(fe_now->value);
664: fe = fe_now;
665: }
666:
667: fe->cb = NULL;
1.87 nicm 668: fe->t = 0;
1.79 nicm 669:
1.1 nicm 670: va_start(ap, fmt);
671: xvasprintf(&fe->value, fmt, ap);
672: va_end(ap);
1.79 nicm 673: }
674:
1.87 nicm 675: /* Add a key and time. */
1.108 nicm 676: static void
1.87 nicm 677: format_add_tv(struct format_tree *ft, const char *key, struct timeval *tv)
678: {
679: struct format_entry *fe;
680: struct format_entry *fe_now;
681:
682: fe = xmalloc(sizeof *fe);
683: fe->key = xstrdup(key);
684:
685: fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
686: if (fe_now != NULL) {
687: free(fe->key);
688: free(fe);
689: free(fe_now->value);
690: fe = fe_now;
691: }
692:
693: fe->cb = NULL;
694: fe->t = tv->tv_sec;
695:
696: fe->value = NULL;
697: }
698:
1.79 nicm 699: /* Add a key and function. */
1.108 nicm 700: static void
1.79 nicm 701: format_add_cb(struct format_tree *ft, const char *key, format_cb cb)
702: {
703: struct format_entry *fe;
704: struct format_entry *fe_now;
705:
706: fe = xmalloc(sizeof *fe);
707: fe->key = xstrdup(key);
1.1 nicm 708:
1.68 nicm 709: fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
1.28 nicm 710: if (fe_now != NULL) {
711: free(fe->key);
712: free(fe);
1.79 nicm 713: free(fe_now->value);
714: fe = fe_now;
1.28 nicm 715: }
1.79 nicm 716:
717: fe->cb = cb;
1.87 nicm 718: fe->t = 0;
1.79 nicm 719:
720: fe->value = NULL;
1.1 nicm 721: }
722:
723: /* Find a format entry. */
1.108 nicm 724: static char *
1.87 nicm 725: format_find(struct format_tree *ft, const char *key, int modifiers)
1.1 nicm 726: {
727: struct format_entry *fe, fe_find;
1.76 nicm 728: struct environ_entry *envent;
1.87 nicm 729: static char s[64];
1.117 nicm 730: struct options_entry *o;
1.87 nicm 731: const char *found;
1.116 nicm 732: int idx;
1.87 nicm 733: char *copy, *saved;
734:
735: if (~modifiers & FORMAT_TIMESTRING) {
1.116 nicm 736: o = options_parse_get(global_options, key, &idx, 0);
1.87 nicm 737: if (o == NULL && ft->w != NULL)
1.116 nicm 738: o = options_parse_get(ft->w->options, key, &idx, 0);
1.87 nicm 739: if (o == NULL)
1.116 nicm 740: o = options_parse_get(global_w_options, key, &idx, 0);
1.87 nicm 741: if (o == NULL && ft->s != NULL)
1.116 nicm 742: o = options_parse_get(ft->s->options, key, &idx, 0);
1.87 nicm 743: if (o == NULL)
1.116 nicm 744: o = options_parse_get(global_s_options, key, &idx, 0);
1.87 nicm 745: if (o != NULL) {
1.118 nicm 746: found = options_tostring(o, idx, 1);
1.116 nicm 747: goto found;
1.54 nicm 748: }
749: }
1.116 nicm 750: found = NULL;
1.1 nicm 751:
752: fe_find.key = (char *) key;
1.68 nicm 753: fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find);
1.79 nicm 754: if (fe != NULL) {
1.87 nicm 755: if (modifiers & FORMAT_TIMESTRING) {
756: if (fe->t == 0)
757: return (NULL);
758: ctime_r(&fe->t, s);
759: s[strcspn(s, "\n")] = '\0';
760: found = s;
761: goto found;
762: }
763: if (fe->t != 0) {
764: xsnprintf(s, sizeof s, "%lld", (long long)fe->t);
765: found = s;
766: goto found;
767: }
1.79 nicm 768: if (fe->value == NULL && fe->cb != NULL)
769: fe->cb(ft, fe);
1.87 nicm 770: found = fe->value;
771: goto found;
1.79 nicm 772: }
1.76 nicm 773:
1.87 nicm 774: if (~modifiers & FORMAT_TIMESTRING) {
775: envent = NULL;
776: if (ft->s != NULL)
1.91 nicm 777: envent = environ_find(ft->s->environ, key);
1.87 nicm 778: if (envent == NULL)
1.91 nicm 779: envent = environ_find(global_environ, key);
1.87 nicm 780: if (envent != NULL) {
781: found = envent->value;
782: goto found;
783: }
784: }
1.76 nicm 785:
786: return (NULL);
1.87 nicm 787:
788: found:
1.88 nicm 789: if (found == NULL)
790: return (NULL);
1.87 nicm 791: copy = xstrdup(found);
792: if (modifiers & FORMAT_BASENAME) {
793: saved = copy;
794: copy = xstrdup(basename(saved));
795: free(saved);
796: }
797: if (modifiers & FORMAT_DIRNAME) {
798: saved = copy;
799: copy = xstrdup(dirname(saved));
800: free(saved);
801: }
802: return (copy);
1.1 nicm 803: }
804:
1.114 nicm 805: /* Skip until comma. */
806: static char *
807: format_skip(char *s)
808: {
809: int brackets = 0;
810:
811: for (; *s != '\0'; s++) {
812: if (*s == '{')
813: brackets++;
814: if (*s == '}')
815: brackets--;
816: if (*s == ',' && brackets == 0)
817: break;
818: }
819: if (*s == '\0')
820: return (NULL);
821: return (s);
822: }
823:
824: /* Return left and right alternatives separated by commas. */
825: static int
826: format_choose(char *s, char **left, char **right)
827: {
828: char *cp;
829:
830: cp = format_skip(s);
831: if (cp == NULL)
832: return (-1);
833: *cp = '\0';
834:
835: *left = s;
836: *right = cp + 1;
837: return (0);
838: }
839:
840: /* Is this true? */
841: static int
842: format_true(const char *s)
843: {
844: if (s != NULL && *s != '\0' && (s[0] != '0' || s[1] != '\0'))
845: return (1);
846: return (0);
847: }
848:
1.1 nicm 849: /*
850: * Replace a key/value pair in buffer. #{blah} is expanded directly,
851: * #{?blah,a,b} is replace with a if blah exists and is nonzero else b.
852: */
1.108 nicm 853: static int
1.30 nicm 854: format_replace(struct format_tree *ft, const char *key, size_t keylen,
855: char **buf, size_t *len, size_t *off)
1.1 nicm 856: {
1.98 nicm 857: char *copy, *copy0, *endptr, *ptr, *found, *new, *value;
1.114 nicm 858: char *from = NULL, *to = NULL, *left, *right;
1.98 nicm 859: size_t valuelen, newlen, fromlen, tolen, used;
1.105 nicm 860: long limit = 0;
1.114 nicm 861: int modifiers = 0, compare = 0;
1.1 nicm 862:
863: /* Make a copy of the key. */
1.30 nicm 864: copy0 = copy = xmalloc(keylen + 1);
1.1 nicm 865: memcpy(copy, key, keylen);
866: copy[keylen] = '\0';
867:
1.30 nicm 868: /* Is there a length limit or whatnot? */
1.87 nicm 869: switch (copy[0]) {
1.114 nicm 870: case '!':
871: if (copy[1] == '=' && copy[2] == ':') {
872: compare = -1;
873: copy += 3;
874: break;
875: }
876: break;
1.87 nicm 877: case '=':
1.114 nicm 878: if (copy[1] == '=' && copy[2] == ':') {
879: compare = 1;
880: copy += 3;
881: break;
882: }
1.87 nicm 883: errno = 0;
1.105 nicm 884: limit = strtol(copy + 1, &endptr, 10);
885: if (errno == ERANGE && (limit == LONG_MIN || limit == LONG_MAX))
1.87 nicm 886: break;
887: if (*endptr != ':')
888: break;
889: copy = endptr + 1;
890: break;
891: case 'b':
892: if (copy[1] != ':')
893: break;
894: modifiers |= FORMAT_BASENAME;
895: copy += 2;
896: break;
897: case 'd':
898: if (copy[1] != ':')
899: break;
900: modifiers |= FORMAT_DIRNAME;
901: copy += 2;
902: break;
903: case 't':
904: if (copy[1] != ':')
905: break;
906: modifiers |= FORMAT_TIMESTRING;
907: copy += 2;
908: break;
1.98 nicm 909: case 's':
910: if (copy[1] != '/')
911: break;
912: from = copy + 2;
913: for (copy = from; *copy != '\0' && *copy != '/'; copy++)
914: /* nothing */;
915: if (copy[0] != '/' || copy == from) {
916: copy = copy0;
917: break;
918: }
919: copy[0] = '\0';
920: to = copy + 1;
921: for (copy = to; *copy != '\0' && *copy != '/'; copy++)
922: /* nothing */;
923: if (copy[0] != '/' || copy[1] != ':') {
924: copy = copy0;
925: break;
926: }
927: copy[0] = '\0';
928:
929: modifiers |= FORMAT_SUBSTITUTE;
930: copy += 2;
931: break;
1.30 nicm 932: }
933:
1.114 nicm 934: /* Is this a comparison or a conditional? */
935: if (compare != 0) {
936: /* Comparison: compare comma-separated left and right. */
937: if (format_choose(copy, &left, &right) != 0)
938: goto fail;
939: left = format_expand(ft, left);
940: right = format_expand(ft, right);
941: if (compare == 1 && strcmp(left, right) == 0)
942: value = xstrdup("1");
943: else if (compare == -1 && strcmp(left, right) != 0)
944: value = xstrdup("1");
945: else
946: value = xstrdup("0");
947: free(right);
948: free(left);
949: } else if (*copy == '?') {
950: /* Conditional: check first and choose second or third. */
951: ptr = format_skip(copy);
1.1 nicm 952: if (ptr == NULL)
953: goto fail;
954: *ptr = '\0';
955:
1.98 nicm 956: found = format_find(ft, copy + 1, modifiers);
1.121 nicm 957: if (found == NULL)
958: found = format_expand(ft, copy + 1);
1.114 nicm 959: if (format_choose(ptr + 1, &left, &right) != 0)
1.89 nicm 960: goto fail;
961:
1.114 nicm 962: if (format_true(found))
963: value = format_expand(ft, left);
964: else
965: value = format_expand(ft, right);
1.98 nicm 966: free(found);
1.1 nicm 967: } else {
1.114 nicm 968: /* Neither: look up directly. */
1.98 nicm 969: value = format_find(ft, copy, modifiers);
1.1 nicm 970: if (value == NULL)
1.98 nicm 971: value = xstrdup("");
972: }
973:
974: /* Perform substitution if any. */
975: if (modifiers & FORMAT_SUBSTITUTE) {
976: fromlen = strlen(from);
977: tolen = strlen(to);
978:
979: newlen = strlen(value) + 1;
980: copy = new = xmalloc(newlen);
981: for (ptr = value; *ptr != '\0'; /* nothing */) {
982: if (strncmp(ptr, from, fromlen) != 0) {
983: *new++ = *ptr++;
984: continue;
985: }
986: used = new - copy;
987:
988: newlen += tolen;
989: copy = xrealloc(copy, newlen);
990:
991: new = copy + used;
992: memcpy(new, to, tolen);
993:
994: new += tolen;
995: ptr += fromlen;
996: }
997: *new = '\0';
998: free(value);
999: value = copy;
1.1 nicm 1000: }
1001:
1.30 nicm 1002: /* Truncate the value if needed. */
1.105 nicm 1003: if (limit > 0) {
1.98 nicm 1004: new = utf8_trimcstr(value, limit);
1.105 nicm 1005: free(value);
1006: value = new;
1007: } else if (limit < 0) {
1008: new = utf8_rtrimcstr(value, -limit);
1.98 nicm 1009: free(value);
1010: value = new;
1.44 nicm 1011: }
1.30 nicm 1012:
1.1 nicm 1013: /* Expand the buffer and copy in the value. */
1.98 nicm 1014: valuelen = strlen(value);
1.1 nicm 1015: while (*len - *off < valuelen + 1) {
1.50 nicm 1016: *buf = xreallocarray(*buf, 2, *len);
1.1 nicm 1017: *len *= 2;
1018: }
1019: memcpy(*buf + *off, value, valuelen);
1020: *off += valuelen;
1021:
1.98 nicm 1022: free(value);
1.30 nicm 1023: free(copy0);
1.1 nicm 1024: return (0);
1025:
1026: fail:
1.30 nicm 1027: free(copy0);
1.1 nicm 1028: return (-1);
1029: }
1030:
1.58 nicm 1031: /* Expand keys in a template, passing through strftime first. */
1032: char *
1033: format_expand_time(struct format_tree *ft, const char *fmt, time_t t)
1034: {
1035: struct tm *tm;
1.107 nicm 1036: char s[2048];
1.58 nicm 1037:
1.67 nicm 1038: if (fmt == NULL || *fmt == '\0')
1.58 nicm 1039: return (xstrdup(""));
1040:
1041: tm = localtime(&t);
1042:
1.107 nicm 1043: if (strftime(s, sizeof s, fmt, tm) == 0)
1044: return (xstrdup(""));
1.58 nicm 1045:
1.107 nicm 1046: return (format_expand(ft, s));
1.58 nicm 1047: }
1048:
1.1 nicm 1049: /* Expand keys in a template. */
1050: char *
1051: format_expand(struct format_tree *ft, const char *fmt)
1052: {
1.113 nicm 1053: char *buf, *out;
1.79 nicm 1054: const char *ptr, *s, *saved = fmt;
1.86 nicm 1055: size_t off, len, n, outlen;
1.31 nicm 1056: int ch, brackets;
1.58 nicm 1057:
1058: if (fmt == NULL)
1059: return (xstrdup(""));
1.1 nicm 1060:
1061: len = 64;
1062: buf = xmalloc(len);
1063: off = 0;
1064:
1065: while (*fmt != '\0') {
1066: if (*fmt != '#') {
1067: while (len - off < 2) {
1.50 nicm 1068: buf = xreallocarray(buf, 2, len);
1.1 nicm 1069: len *= 2;
1070: }
1071: buf[off++] = *fmt++;
1072: continue;
1073: }
1074: fmt++;
1075:
1076: ch = (u_char) *fmt++;
1077: switch (ch) {
1.68 nicm 1078: case '(':
1079: brackets = 1;
1080: for (ptr = fmt; *ptr != '\0'; ptr++) {
1081: if (*ptr == '(')
1082: brackets++;
1083: if (*ptr == ')' && --brackets == 0)
1084: break;
1085: }
1086: if (*ptr != ')' || brackets != 0)
1087: break;
1088: n = ptr - fmt;
1089:
1.114 nicm 1090: if (ft->flags & FORMAT_NOJOBS)
1091: out = xstrdup("");
1092: else
1093: out = format_job_get(ft, xstrndup(fmt, n));
1.86 nicm 1094: outlen = strlen(out);
1.68 nicm 1095:
1.86 nicm 1096: while (len - off < outlen + 1) {
1.68 nicm 1097: buf = xreallocarray(buf, 2, len);
1098: len *= 2;
1099: }
1.86 nicm 1100: memcpy(buf + off, out, outlen);
1101: off += outlen;
1102:
1103: free(out);
1.68 nicm 1104:
1105: fmt += n + 1;
1106: continue;
1.1 nicm 1107: case '{':
1.31 nicm 1108: brackets = 1;
1109: for (ptr = fmt; *ptr != '\0'; ptr++) {
1110: if (*ptr == '{')
1111: brackets++;
1112: if (*ptr == '}' && --brackets == 0)
1113: break;
1114: }
1115: if (*ptr != '}' || brackets != 0)
1.1 nicm 1116: break;
1117: n = ptr - fmt;
1118:
1119: if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
1120: break;
1121: fmt += n + 1;
1.40 nicm 1122: continue;
1123: case '#':
1124: while (len - off < 2) {
1.50 nicm 1125: buf = xreallocarray(buf, 2, len);
1.40 nicm 1126: len *= 2;
1127: }
1128: buf[off++] = '#';
1.1 nicm 1129: continue;
1130: default:
1.25 nicm 1131: s = NULL;
1132: if (ch >= 'A' && ch <= 'Z')
1133: s = format_upper[ch - 'A'];
1134: else if (ch >= 'a' && ch <= 'z')
1135: s = format_lower[ch - 'a'];
1136: if (s == NULL) {
1137: while (len - off < 3) {
1.50 nicm 1138: buf = xreallocarray(buf, 2, len);
1.25 nicm 1139: len *= 2;
1.1 nicm 1140: }
1.25 nicm 1141: buf[off++] = '#';
1142: buf[off++] = ch;
1143: continue;
1.1 nicm 1144: }
1.25 nicm 1145: n = strlen(s);
1146: if (format_replace(ft, s, n, &buf, &len, &off) != 0)
1147: break;
1.1 nicm 1148: continue;
1149: }
1150:
1151: break;
1152: }
1153: buf[off] = '\0';
1154:
1.79 nicm 1155: log_debug("format '%s' -> '%s'", saved, buf);
1.1 nicm 1156: return (buf);
1.123 nicm 1157: }
1158:
1159: /* Expand a single string. */
1160: char *
1161: format_single(struct cmdq_item *item, const char *fmt, struct client *c,
1162: struct session *s, struct winlink *wl, struct window_pane *wp)
1163: {
1164: struct format_tree *ft;
1165: char *expanded;
1166:
1.131 nicm 1167: if (item != NULL)
1168: ft = format_create(item->client, item, FORMAT_NONE, 0);
1169: else
1170: ft = format_create(NULL, item, FORMAT_NONE, 0);
1.123 nicm 1171: format_defaults(ft, c, s, wl, wp);
1172:
1173: expanded = format_expand(ft, fmt);
1174: format_free(ft);
1175: return (expanded);
1.1 nicm 1176: }
1177:
1.57 nicm 1178: /* Set defaults for any of arguments that are not NULL. */
1179: void
1180: format_defaults(struct format_tree *ft, struct client *c, struct session *s,
1181: struct winlink *wl, struct window_pane *wp)
1182: {
1183: if (s == NULL && c != NULL)
1184: s = c->session;
1185: if (wl == NULL && s != NULL)
1186: wl = s->curw;
1187: if (wp == NULL && wl != NULL)
1188: wp = wl->window->active;
1189:
1190: if (c != NULL)
1191: format_defaults_client(ft, c);
1192: if (s != NULL)
1193: format_defaults_session(ft, s);
1.129 nicm 1194: if (wl != NULL)
1195: format_defaults_winlink(ft, wl);
1.57 nicm 1196: if (wp != NULL)
1197: format_defaults_pane(ft, wp);
1198: }
1199:
1.1 nicm 1200: /* Set default format keys for a session. */
1.108 nicm 1201: static void
1.57 nicm 1202: format_defaults_session(struct format_tree *ft, struct session *s)
1.1 nicm 1203: {
1204: struct session_group *sg;
1205:
1.54 nicm 1206: ft->s = s;
1207:
1.1 nicm 1208: format_add(ft, "session_name", "%s", s->name);
1209: format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
1210: format_add(ft, "session_width", "%u", s->sx);
1211: format_add(ft, "session_height", "%u", s->sy);
1.23 nicm 1212: format_add(ft, "session_id", "$%u", s->id);
1.1 nicm 1213:
1.122 nicm 1214: sg = session_group_contains(s);
1.1 nicm 1215: format_add(ft, "session_grouped", "%d", sg != NULL);
1216: if (sg != NULL)
1.122 nicm 1217: format_add(ft, "session_group", "%s", sg->name);
1.1 nicm 1218:
1.87 nicm 1219: format_add_tv(ft, "session_created", &s->creation_time);
1220: format_add_tv(ft, "session_last_attached", &s->last_attached_time);
1221: format_add_tv(ft, "session_activity", &s->activity_time);
1.1 nicm 1222:
1.41 nicm 1223: format_add(ft, "session_attached", "%u", s->attached);
1.59 nicm 1224: format_add(ft, "session_many_attached", "%d", s->attached > 1);
1.66 nicm 1225:
1.80 nicm 1226: format_add_cb(ft, "session_alerts", format_cb_session_alerts);
1.133 nicm 1227: format_add_cb(ft, "session_stack", format_cb_session_stack);
1.3 nicm 1228: }
1229:
1230: /* Set default format keys for a client. */
1.108 nicm 1231: static void
1.57 nicm 1232: format_defaults_client(struct format_tree *ft, struct client *c)
1.3 nicm 1233: {
1.60 nicm 1234: struct session *s;
1.103 nicm 1235: const char *name;
1.115 nicm 1236: struct tty *tty = &c->tty;
1237: const char *types[] = TTY_TYPES;
1.3 nicm 1238:
1.54 nicm 1239: if (ft->s == NULL)
1240: ft->s = c->session;
1241:
1.124 nicm 1242: format_add(ft, "client_name", "%s", c->name);
1.72 nicm 1243: format_add(ft, "client_pid", "%ld", (long) c->pid);
1.115 nicm 1244: format_add(ft, "client_height", "%u", tty->sy);
1245: format_add(ft, "client_width", "%u", tty->sx);
1.124 nicm 1246: format_add(ft, "client_tty", "%s", c->ttyname);
1.75 nicm 1247: format_add(ft, "client_control_mode", "%d",
1248: !!(c->flags & CLIENT_CONTROL));
1.3 nicm 1249:
1.115 nicm 1250: if (tty->term_name != NULL)
1251: format_add(ft, "client_termname", "%s", tty->term_name);
1252: if (tty->term_name != NULL)
1253: format_add(ft, "client_termtype", "%s", types[tty->term_type]);
1254:
1.87 nicm 1255: format_add_tv(ft, "client_created", &c->creation_time);
1256: format_add_tv(ft, "client_activity", &c->activity_time);
1.126 nicm 1257:
1258: format_add(ft, "client_written", "%zu", c->written);
1259: format_add(ft, "client_discarded", "%zu", c->discarded);
1.14 nicm 1260:
1.103 nicm 1261: name = server_client_get_key_table(c);
1262: if (strcmp(c->keytable->name, name) == 0)
1.61 nicm 1263: format_add(ft, "client_prefix", "%d", 0);
1264: else
1265: format_add(ft, "client_prefix", "%d", 1);
1266: format_add(ft, "client_key_table", "%s", c->keytable->name);
1.3 nicm 1267:
1.115 nicm 1268: if (tty->flags & TTY_UTF8)
1.3 nicm 1269: format_add(ft, "client_utf8", "%d", 1);
1270: else
1271: format_add(ft, "client_utf8", "%d", 0);
1272:
1273: if (c->flags & CLIENT_READONLY)
1274: format_add(ft, "client_readonly", "%d", 1);
1275: else
1276: format_add(ft, "client_readonly", "%d", 0);
1.15 nicm 1277:
1278: s = c->session;
1279: if (s != NULL)
1280: format_add(ft, "client_session", "%s", s->name);
1281: s = c->last_session;
1282: if (s != NULL && session_alive(s))
1283: format_add(ft, "client_last_session", "%s", s->name);
1.1 nicm 1284: }
1285:
1.32 nicm 1286: /* Set default format keys for a window. */
1287: void
1.57 nicm 1288: format_defaults_window(struct format_tree *ft, struct window *w)
1.32 nicm 1289: {
1.54 nicm 1290: ft->w = w;
1291:
1.87 nicm 1292: format_add_tv(ft, "window_activity", &w->activity_time);
1.32 nicm 1293: format_add(ft, "window_id", "@%u", w->id);
1294: format_add(ft, "window_name", "%s", w->name);
1295: format_add(ft, "window_width", "%u", w->sx);
1296: format_add(ft, "window_height", "%u", w->sy);
1.80 nicm 1297: format_add_cb(ft, "window_layout", format_cb_window_layout);
1.96 nicm 1298: format_add_cb(ft, "window_visible_layout",
1299: format_cb_window_visible_layout);
1.32 nicm 1300: format_add(ft, "window_panes", "%u", window_count_panes(w));
1.59 nicm 1301: format_add(ft, "window_zoomed_flag", "%d",
1.53 nicm 1302: !!(w->flags & WINDOW_ZOOMED));
1.32 nicm 1303: }
1304:
1.1 nicm 1305: /* Set default format keys for a winlink. */
1.108 nicm 1306: static void
1.129 nicm 1307: format_defaults_winlink(struct format_tree *ft, struct winlink *wl)
1.1 nicm 1308: {
1.129 nicm 1309: struct session *s = wl->session;
1.1 nicm 1310: struct window *w = wl->window;
1311:
1.54 nicm 1312: if (ft->w == NULL)
1313: ft->w = wl->window;
1.133 nicm 1314: ft->wl = wl;
1.54 nicm 1315:
1.57 nicm 1316: format_defaults_window(ft, w);
1.32 nicm 1317:
1.1 nicm 1318: format_add(ft, "window_index", "%d", wl->idx);
1.133 nicm 1319: format_add_cb(ft, "window_stack_index", format_cb_window_stack_index);
1.129 nicm 1320: format_add(ft, "window_flags", "%s", window_printable_flags(wl));
1.1 nicm 1321: format_add(ft, "window_active", "%d", wl == s->curw);
1.29 nicm 1322:
1.59 nicm 1323: format_add(ft, "window_bell_flag", "%d",
1.29 nicm 1324: !!(wl->flags & WINLINK_BELL));
1.59 nicm 1325: format_add(ft, "window_activity_flag", "%d",
1.29 nicm 1326: !!(wl->flags & WINLINK_ACTIVITY));
1.59 nicm 1327: format_add(ft, "window_silence_flag", "%d",
1.29 nicm 1328: !!(wl->flags & WINLINK_SILENCE));
1.59 nicm 1329: format_add(ft, "window_last_flag", "%d",
1.49 nicm 1330: !!(wl == TAILQ_FIRST(&s->lastw)));
1.64 nicm 1331: format_add(ft, "window_linked", "%d", session_is_linked(s, wl->window));
1.1 nicm 1332: }
1333:
1334: /* Set default format keys for a window pane. */
1335: void
1.57 nicm 1336: format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
1.1 nicm 1337: {
1.80 nicm 1338: struct grid *gd = wp->base.grid;
1339: u_int idx;
1.85 nicm 1340: int status, scroll_position;
1.54 nicm 1341:
1342: if (ft->w == NULL)
1343: ft->w = wp->window;
1.79 nicm 1344: ft->wp = wp;
1.1 nicm 1345:
1.16 nicm 1346: format_add(ft, "history_size", "%u", gd->hsize);
1347: format_add(ft, "history_limit", "%u", gd->hlimit);
1.80 nicm 1348: format_add_cb(ft, "history_bytes", format_cb_history_bytes);
1.1 nicm 1349:
1.4 nicm 1350: if (window_pane_index(wp, &idx) != 0)
1351: fatalx("index not found");
1.16 nicm 1352: format_add(ft, "pane_index", "%u", idx);
1.4 nicm 1353:
1.1 nicm 1354: format_add(ft, "pane_width", "%u", wp->sx);
1355: format_add(ft, "pane_height", "%u", wp->sy);
1356: format_add(ft, "pane_title", "%s", wp->base.title);
1357: format_add(ft, "pane_id", "%%%u", wp->id);
1358: format_add(ft, "pane_active", "%d", wp == wp->window->active);
1.55 nicm 1359: format_add(ft, "pane_input_off", "%d", !!(wp->flags & PANE_INPUTOFF));
1360:
1361: status = wp->status;
1362: if (wp->fd == -1 && WIFEXITED(status))
1363: format_add(ft, "pane_dead_status", "%d", WEXITSTATUS(status));
1.1 nicm 1364: format_add(ft, "pane_dead", "%d", wp->fd == -1);
1.47 nicm 1365:
1366: if (window_pane_visible(wp)) {
1367: format_add(ft, "pane_left", "%u", wp->xoff);
1368: format_add(ft, "pane_top", "%u", wp->yoff);
1369: format_add(ft, "pane_right", "%u", wp->xoff + wp->sx - 1);
1370: format_add(ft, "pane_bottom", "%u", wp->yoff + wp->sy - 1);
1371: }
1.16 nicm 1372:
1373: format_add(ft, "pane_in_mode", "%d", wp->screen != &wp->base);
1.134 ! nicm 1374: if (wp->mode != NULL)
! 1375: format_add(ft, "pane_mode", "%s", wp->mode->name);
! 1376:
1.26 nicm 1377: format_add(ft, "pane_synchronized", "%d",
1.90 nicm 1378: !!options_get_number(wp->window->options, "synchronize-panes"));
1.132 nicm 1379: format_add(ft, "pane_search_string", "%s",
1380: window_copy_search_string(wp));
1.16 nicm 1381:
1.71 nicm 1382: format_add(ft, "pane_tty", "%s", wp->tty);
1.16 nicm 1383: format_add(ft, "pane_pid", "%ld", (long) wp->pid);
1.79 nicm 1384: format_add_cb(ft, "pane_start_command", format_cb_start_command);
1385: format_add_cb(ft, "pane_current_command", format_cb_current_command);
1.16 nicm 1386:
1.59 nicm 1387: format_add(ft, "cursor_x", "%u", wp->base.cx);
1388: format_add(ft, "cursor_y", "%u", wp->base.cy);
1389: format_add(ft, "scroll_region_upper", "%u", wp->base.rupper);
1390: format_add(ft, "scroll_region_lower", "%u", wp->base.rlower);
1.85 nicm 1391:
1392: scroll_position = window_copy_scroll_position(wp);
1393: if (scroll_position != -1)
1394: format_add(ft, "scroll_position", "%d", scroll_position);
1.16 nicm 1395:
1396: format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0);
1.59 nicm 1397: format_add(ft, "alternate_saved_x", "%u", wp->saved_cx);
1398: format_add(ft, "alternate_saved_y", "%u", wp->saved_cy);
1.16 nicm 1399:
1400: format_add(ft, "cursor_flag", "%d",
1401: !!(wp->base.mode & MODE_CURSOR));
1402: format_add(ft, "insert_flag", "%d",
1403: !!(wp->base.mode & MODE_INSERT));
1404: format_add(ft, "keypad_cursor_flag", "%d",
1405: !!(wp->base.mode & MODE_KCURSOR));
1406: format_add(ft, "keypad_flag", "%d",
1407: !!(wp->base.mode & MODE_KKEYPAD));
1408: format_add(ft, "wrap_flag", "%d",
1409: !!(wp->base.mode & MODE_WRAP));
1410:
1.62 nicm 1411: format_add(ft, "mouse_any_flag", "%d",
1.119 nicm 1412: !!(wp->base.mode & ALL_MOUSE_MODES));
1.16 nicm 1413: format_add(ft, "mouse_standard_flag", "%d",
1414: !!(wp->base.mode & MODE_MOUSE_STANDARD));
1415: format_add(ft, "mouse_button_flag", "%d",
1416: !!(wp->base.mode & MODE_MOUSE_BUTTON));
1.119 nicm 1417: format_add(ft, "mouse_all_flag", "%d",
1418: !!(wp->base.mode & MODE_MOUSE_ALL));
1.19 nicm 1419:
1.80 nicm 1420: format_add_cb(ft, "pane_tabs", format_cb_pane_tabs);
1.8 nicm 1421: }
1422:
1.19 nicm 1423: /* Set default format keys for paste buffer. */
1.8 nicm 1424: void
1.94 nicm 1425: format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
1.8 nicm 1426: {
1.81 nicm 1427: size_t bufsize;
1.42 nicm 1428: char *s;
1.8 nicm 1429:
1.81 nicm 1430: paste_buffer_data(pb, &bufsize);
1431: format_add(ft, "buffer_size", "%zu", bufsize);
1432: format_add(ft, "buffer_name", "%s", paste_buffer_name(pb));
1.8 nicm 1433:
1.94 nicm 1434: s = paste_make_sample(pb);
1.42 nicm 1435: format_add(ft, "buffer_sample", "%s", s);
1436: free(s);
1.1 nicm 1437: }