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