Annotation of src/usr.bin/tmux/format.c, Revision 1.188
1.188 ! nicm 1: /* $OpenBSD: format.c,v 1.187 2019/03/28 21:05: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.157 nicm 22: #include <ctype.h>
1.30 nicm 23: #include <errno.h>
1.139 nicm 24: #include <fnmatch.h>
1.87 nicm 25: #include <libgen.h>
1.1 nicm 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.164 nicm 55: static void format_defaults_winlink(struct format_tree *, struct winlink *);
1.1 nicm 56:
1.68 nicm 57: /* Entry in format job tree. */
58: struct format_job {
1.131 nicm 59: struct client *client;
1.120 nicm 60: u_int tag;
1.68 nicm 61: const char *cmd;
1.113 nicm 62: const char *expanded;
1.68 nicm 63:
64: time_t last;
65: char *out;
1.127 nicm 66: int updated;
1.68 nicm 67:
68: struct job *job;
69: int status;
70:
71: RB_ENTRY(format_job) entry;
72: };
73:
74: /* Format job tree. */
1.108 nicm 75: static struct event format_job_event;
76: static int format_job_cmp(struct format_job *, struct format_job *);
1.109 nicm 77: static RB_HEAD(format_job_tree, format_job) format_jobs = RB_INITIALIZER();
1.108 nicm 78: RB_GENERATE_STATIC(format_job_tree, format_job, entry, format_job_cmp);
1.68 nicm 79:
80: /* Format job tree comparison function. */
1.108 nicm 81: static int
1.68 nicm 82: format_job_cmp(struct format_job *fj1, struct format_job *fj2)
83: {
1.120 nicm 84: if (fj1->tag < fj2->tag)
85: return (-1);
86: if (fj1->tag > fj2->tag)
87: return (1);
1.68 nicm 88: return (strcmp(fj1->cmd, fj2->cmd));
89: }
90:
1.87 nicm 91: /* Format modifiers. */
92: #define FORMAT_TIMESTRING 0x1
93: #define FORMAT_BASENAME 0x2
94: #define FORMAT_DIRNAME 0x4
1.169 nicm 95: #define FORMAT_QUOTE 0x8
96: #define FORMAT_LITERAL 0x10
1.170 nicm 97: #define FORMAT_EXPAND 0x20
1.175 nicm 98: #define FORMAT_EXPANDTIME 0x40
99: #define FORMAT_SESSIONS 0x80
100: #define FORMAT_WINDOWS 0x100
101: #define FORMAT_PANES 0x200
1.87 nicm 102:
1.178 nicm 103: /* Limit on recursion. */
104: #define FORMAT_LOOP_LIMIT 10
105:
1.54 nicm 106: /* Entry in format tree. */
107: struct format_entry {
1.79 nicm 108: char *key;
109: char *value;
1.87 nicm 110: time_t t;
1.79 nicm 111: format_cb cb;
112: RB_ENTRY(format_entry) entry;
1.54 nicm 113: };
114:
1.68 nicm 115: /* Format entry tree. */
1.54 nicm 116: struct format_tree {
1.164 nicm 117: struct client *c;
118: struct session *s;
119: struct winlink *wl;
1.79 nicm 120: struct window *w;
121: struct window_pane *wp;
1.54 nicm 122:
1.172 nicm 123: struct cmdq_item *item;
1.131 nicm 124: struct client *client;
1.120 nicm 125: u_int tag;
1.84 nicm 126: int flags;
1.177 nicm 127: time_t time;
1.178 nicm 128: u_int loop;
1.68 nicm 129:
130: RB_HEAD(format_entry_tree, format_entry) tree;
1.54 nicm 131: };
1.108 nicm 132: static int format_entry_cmp(struct format_entry *, struct format_entry *);
133: RB_GENERATE_STATIC(format_entry_tree, format_entry, entry, format_entry_cmp);
1.54 nicm 134:
1.180 nicm 135: /* Format modifier. */
1.169 nicm 136: struct format_modifier {
137: char modifier[3];
138: u_int size;
139:
140: char **argv;
141: int argc;
142: };
143:
1.68 nicm 144: /* Format entry tree comparison function. */
1.108 nicm 145: static int
1.68 nicm 146: format_entry_cmp(struct format_entry *fe1, struct format_entry *fe2)
1.1 nicm 147: {
148: return (strcmp(fe1->key, fe2->key));
149: }
150:
1.25 nicm 151: /* Single-character uppercase aliases. */
1.108 nicm 152: static const char *format_upper[] = {
1.1 nicm 153: NULL, /* A */
154: NULL, /* B */
155: NULL, /* C */
156: "pane_id", /* D */
157: NULL, /* E */
158: "window_flags", /* F */
159: NULL, /* G */
160: "host", /* H */
161: "window_index", /* I */
162: NULL, /* J */
163: NULL, /* K */
164: NULL, /* L */
165: NULL, /* M */
166: NULL, /* N */
167: NULL, /* O */
168: "pane_index", /* P */
169: NULL, /* Q */
170: NULL, /* R */
171: "session_name", /* S */
172: "pane_title", /* T */
173: NULL, /* U */
174: NULL, /* V */
175: "window_name", /* W */
176: NULL, /* X */
177: NULL, /* Y */
178: NULL /* Z */
179: };
180:
1.25 nicm 181: /* Single-character lowercase aliases. */
1.108 nicm 182: static const char *format_lower[] = {
1.25 nicm 183: NULL, /* a */
184: NULL, /* b */
185: NULL, /* c */
186: NULL, /* d */
187: NULL, /* e */
188: NULL, /* f */
189: NULL, /* g */
190: "host_short", /* h */
191: NULL, /* i */
192: NULL, /* j */
193: NULL, /* k */
194: NULL, /* l */
195: NULL, /* m */
196: NULL, /* n */
197: NULL, /* o */
198: NULL, /* p */
199: NULL, /* q */
200: NULL, /* r */
201: NULL, /* s */
202: NULL, /* t */
203: NULL, /* u */
204: NULL, /* v */
205: NULL, /* w */
206: NULL, /* x */
207: NULL, /* y */
208: NULL /* z */
209: };
210:
1.179 nicm 211: /* Is logging enabled? */
212: static inline int
213: format_logging(struct format_tree *ft)
214: {
215: return (log_get_level() != 0 || (ft->flags & FORMAT_VERBOSE));
216: }
217:
218: /* Log a message if verbose. */
219: static void printflike(3, 4)
220: format_log1(struct format_tree *ft, const char *from, const char *fmt, ...)
221: {
222: va_list ap;
223: char *s;
224: static const char spaces[] = " ";
225:
226: if (!format_logging(ft))
227: return;
228:
229: va_start(ap, fmt);
230: vasprintf(&s, fmt, ap);
231: va_end(ap);
232:
233: log_debug("%s: %s", from, s);
1.181 nicm 234: if (ft->item != NULL && (ft->flags & FORMAT_VERBOSE))
1.179 nicm 235: cmdq_print(ft->item, "#%.*s%s", ft->loop, spaces, s);
236:
237: free(s);
238: }
239: #define format_log(ft, fmt, ...) format_log1(ft, __func__, fmt, ##__VA_ARGS__)
240:
1.127 nicm 241: /* Format job update callback. */
1.108 nicm 242: static void
1.127 nicm 243: format_job_update(struct job *job)
244: {
1.160 nicm 245: struct format_job *fj = job_get_data(job);
246: struct evbuffer *evb = job_get_event(job)->input;
1.151 nicm 247: char *line = NULL, *next;
1.127 nicm 248: time_t t;
249:
1.151 nicm 250: while ((next = evbuffer_readline(evb)) != NULL) {
251: free(line);
252: line = next;
253: }
254: if (line == NULL)
1.127 nicm 255: return;
256: fj->updated = 1;
257:
258: free(fj->out);
259: fj->out = line;
260:
1.136 nicm 261: log_debug("%s: %p %s: %s", __func__, fj, fj->cmd, fj->out);
1.127 nicm 262:
1.142 nicm 263: t = time(NULL);
1.127 nicm 264: if (fj->status && fj->last != t) {
1.136 nicm 265: if (fj->client != NULL)
266: server_status_client(fj->client);
1.127 nicm 267: fj->last = t;
268: }
269: }
270:
271: /* Format job complete callback. */
272: static void
273: format_job_complete(struct job *job)
1.68 nicm 274: {
1.160 nicm 275: struct format_job *fj = job_get_data(job);
276: struct evbuffer *evb = job_get_event(job)->input;
1.68 nicm 277: char *line, *buf;
278: size_t len;
279:
280: fj->job = NULL;
281:
282: buf = NULL;
1.160 nicm 283: if ((line = evbuffer_readline(evb)) == NULL) {
284: len = EVBUFFER_LENGTH(evb);
1.68 nicm 285: buf = xmalloc(len + 1);
286: if (len != 0)
1.160 nicm 287: memcpy(buf, EVBUFFER_DATA(evb), len);
1.68 nicm 288: buf[len] = '\0';
289: } else
290: buf = line;
1.127 nicm 291:
1.136 nicm 292: log_debug("%s: %p %s: %s", __func__, fj, fj->cmd, buf);
293:
1.127 nicm 294: if (*buf != '\0' || !fj->updated) {
295: free(fj->out);
296: fj->out = buf;
297: } else
298: free(buf);
1.68 nicm 299:
300: if (fj->status) {
1.131 nicm 301: if (fj->client != NULL)
302: server_status_client(fj->client);
1.68 nicm 303: fj->status = 0;
304: }
305: }
306:
307: /* Find a job. */
1.108 nicm 308: static char *
1.68 nicm 309: format_job_get(struct format_tree *ft, const char *cmd)
310: {
1.131 nicm 311: struct format_job_tree *jobs;
1.113 nicm 312: struct format_job fj0, *fj;
313: time_t t;
314: char *expanded;
315: int force;
1.68 nicm 316:
1.131 nicm 317: if (ft->client == NULL)
318: jobs = &format_jobs;
319: else if (ft->client->jobs != NULL)
320: jobs = ft->client->jobs;
321: else {
322: jobs = ft->client->jobs = xmalloc(sizeof *ft->client->jobs);
323: RB_INIT(jobs);
324: }
325:
1.120 nicm 326: fj0.tag = ft->tag;
1.68 nicm 327: fj0.cmd = cmd;
1.131 nicm 328: if ((fj = RB_FIND(format_job_tree, jobs, &fj0)) == NULL) {
1.68 nicm 329: fj = xcalloc(1, sizeof *fj);
1.131 nicm 330: fj->client = ft->client;
1.120 nicm 331: fj->tag = ft->tag;
1.68 nicm 332: fj->cmd = xstrdup(cmd);
1.113 nicm 333: fj->expanded = NULL;
1.68 nicm 334:
335: xasprintf(&fj->out, "<'%s' not ready>", fj->cmd);
336:
1.131 nicm 337: RB_INSERT(format_job_tree, jobs, fj);
1.68 nicm 338: }
339:
1.113 nicm 340: expanded = format_expand(ft, cmd);
341: if (fj->expanded == NULL || strcmp(expanded, fj->expanded) != 0) {
342: free((void *)fj->expanded);
343: fj->expanded = xstrdup(expanded);
344: force = 1;
345: } else
346: force = (ft->flags & FORMAT_FORCE);
347:
1.84 nicm 348: t = time(NULL);
1.183 nicm 349: if (force && fj->job != NULL)
350: job_free(fj->job);
351: if (force || (fj->job == NULL && fj->last != t)) {
1.163 nicm 352: fj->job = job_run(expanded, NULL,
353: server_client_get_cwd(ft->client, NULL), format_job_update,
1.153 nicm 354: format_job_complete, NULL, fj, JOB_NOWAIT);
1.68 nicm 355: if (fj->job == NULL) {
356: free(fj->out);
357: xasprintf(&fj->out, "<'%s' didn't start>", fj->cmd);
358: }
1.84 nicm 359: fj->last = t;
1.137 nicm 360: fj->updated = 0;
1.68 nicm 361: }
1.84 nicm 362:
363: if (ft->flags & FORMAT_STATUS)
364: fj->status = 1;
1.68 nicm 365:
1.113 nicm 366: free(expanded);
1.86 nicm 367: return (format_expand(ft, fj->out));
1.68 nicm 368: }
369:
370: /* Remove old jobs. */
1.108 nicm 371: static void
1.131 nicm 372: format_job_tidy(struct format_job_tree *jobs, int force)
1.68 nicm 373: {
374: struct format_job *fj, *fj1;
375: time_t now;
376:
377: now = time(NULL);
1.131 nicm 378: RB_FOREACH_SAFE(fj, format_job_tree, jobs, fj1) {
379: if (!force && (fj->last > now || now - fj->last < 3600))
1.68 nicm 380: continue;
1.131 nicm 381: RB_REMOVE(format_job_tree, jobs, fj);
1.68 nicm 382:
1.77 nicm 383: log_debug("%s: %s", __func__, fj->cmd);
384:
1.68 nicm 385: if (fj->job != NULL)
386: job_free(fj->job);
387:
1.113 nicm 388: free((void *)fj->expanded);
1.78 nicm 389: free((void *)fj->cmd);
1.68 nicm 390: free(fj->out);
391:
392: free(fj);
393: }
1.131 nicm 394: }
395:
396: /* Remove old jobs for client. */
397: void
398: format_lost_client(struct client *c)
399: {
400: if (c->jobs != NULL)
401: format_job_tidy(c->jobs, 1);
402: free(c->jobs);
403: }
404:
405: /* Remove old jobs periodically. */
406: static void
407: format_job_timer(__unused int fd, __unused short events, __unused void *arg)
408: {
409: struct client *c;
410: struct timeval tv = { .tv_sec = 60 };
411:
412: format_job_tidy(&format_jobs, 0);
413: TAILQ_FOREACH(c, &clients, entry) {
414: if (c->jobs != NULL)
415: format_job_tidy(c->jobs, 0);
416: }
1.77 nicm 417:
418: evtimer_del(&format_job_event);
419: evtimer_add(&format_job_event, &tv);
1.68 nicm 420: }
421:
1.79 nicm 422: /* Callback for host. */
1.108 nicm 423: static void
1.99 nicm 424: format_cb_host(__unused struct format_tree *ft, struct format_entry *fe)
1.79 nicm 425: {
426: char host[HOST_NAME_MAX + 1];
427:
428: if (gethostname(host, sizeof host) != 0)
429: fe->value = xstrdup("");
430: else
431: fe->value = xstrdup(host);
432: }
433:
434: /* Callback for host_short. */
1.108 nicm 435: static void
1.99 nicm 436: format_cb_host_short(__unused struct format_tree *ft, struct format_entry *fe)
1.79 nicm 437: {
438: char host[HOST_NAME_MAX + 1], *cp;
439:
440: if (gethostname(host, sizeof host) != 0)
441: fe->value = xstrdup("");
442: else {
443: if ((cp = strchr(host, '.')) != NULL)
444: *cp = '\0';
445: fe->value = xstrdup(host);
446: }
447: }
448:
449: /* Callback for pid. */
1.108 nicm 450: static void
1.99 nicm 451: format_cb_pid(__unused struct format_tree *ft, struct format_entry *fe)
1.79 nicm 452: {
453: xasprintf(&fe->value, "%ld", (long)getpid());
454: }
455:
1.80 nicm 456: /* Callback for session_alerts. */
1.108 nicm 457: static void
1.80 nicm 458: format_cb_session_alerts(struct format_tree *ft, struct format_entry *fe)
459: {
460: struct session *s = ft->s;
461: struct winlink *wl;
1.133 nicm 462: char alerts[1024], tmp[16];
1.80 nicm 463:
464: if (s == NULL)
465: return;
466:
467: *alerts = '\0';
468: RB_FOREACH(wl, winlinks, &s->windows) {
469: if ((wl->flags & WINLINK_ALERTFLAGS) == 0)
470: continue;
471: xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
472:
473: if (*alerts != '\0')
474: strlcat(alerts, ",", sizeof alerts);
475: strlcat(alerts, tmp, sizeof alerts);
476: if (wl->flags & WINLINK_ACTIVITY)
477: strlcat(alerts, "#", sizeof alerts);
478: if (wl->flags & WINLINK_BELL)
479: strlcat(alerts, "!", sizeof alerts);
480: if (wl->flags & WINLINK_SILENCE)
481: strlcat(alerts, "~", sizeof alerts);
482: }
483: fe->value = xstrdup(alerts);
484: }
485:
1.133 nicm 486: /* Callback for session_stack. */
487: static void
488: format_cb_session_stack(struct format_tree *ft, struct format_entry *fe)
489: {
490: struct session *s = ft->s;
491: struct winlink *wl;
492: char result[1024], tmp[16];
493:
494: if (s == NULL)
495: return;
496:
497: xsnprintf(result, sizeof result, "%u", s->curw->idx);
498: TAILQ_FOREACH(wl, &s->lastw, sentry) {
499: xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
500:
501: if (*result != '\0')
502: strlcat(result, ",", sizeof result);
503: strlcat(result, tmp, sizeof result);
504: }
505: fe->value = xstrdup(result);
506: }
507:
508: /* Callback for window_stack_index. */
509: static void
510: format_cb_window_stack_index(struct format_tree *ft, struct format_entry *fe)
511: {
512: struct session *s = ft->wl->session;
513: struct winlink *wl;
514: u_int idx;
515:
516: idx = 0;
517: TAILQ_FOREACH(wl, &s->lastw, sentry) {
518: idx++;
519: if (wl == ft->wl)
520: break;
521: }
522: if (wl != NULL)
523: xasprintf(&fe->value, "%u", idx);
524: else
525: fe->value = xstrdup("0");
526: }
527:
1.80 nicm 528: /* Callback for window_layout. */
1.108 nicm 529: static void
1.80 nicm 530: format_cb_window_layout(struct format_tree *ft, struct format_entry *fe)
531: {
532: struct window *w = ft->w;
533:
534: if (w == NULL)
535: return;
536:
537: if (w->saved_layout_root != NULL)
538: fe->value = layout_dump(w->saved_layout_root);
539: else
540: fe->value = layout_dump(w->layout_root);
541: }
542:
1.96 nicm 543: /* Callback for window_visible_layout. */
1.108 nicm 544: static void
1.96 nicm 545: format_cb_window_visible_layout(struct format_tree *ft, struct format_entry *fe)
546: {
547: struct window *w = ft->w;
548:
549: if (w == NULL)
550: return;
551:
552: fe->value = layout_dump(w->layout_root);
553: }
554:
1.79 nicm 555: /* Callback for pane_start_command. */
1.108 nicm 556: static void
1.79 nicm 557: format_cb_start_command(struct format_tree *ft, struct format_entry *fe)
558: {
559: struct window_pane *wp = ft->wp;
560:
561: if (wp == NULL)
562: return;
563:
564: fe->value = cmd_stringify_argv(wp->argc, wp->argv);
565: }
566:
567: /* Callback for pane_current_command. */
1.108 nicm 568: static void
1.79 nicm 569: format_cb_current_command(struct format_tree *ft, struct format_entry *fe)
570: {
571: struct window_pane *wp = ft->wp;
572: char *cmd;
573:
574: if (wp == NULL)
575: return;
576:
577: cmd = get_proc_name(wp->fd, wp->tty);
578: if (cmd == NULL || *cmd == '\0') {
579: free(cmd);
580: cmd = cmd_stringify_argv(wp->argc, wp->argv);
581: if (cmd == NULL || *cmd == '\0') {
582: free(cmd);
583: cmd = xstrdup(wp->shell);
584: }
585: }
586: fe->value = parse_window_name(cmd);
587: free(cmd);
588: }
589:
1.80 nicm 590: /* Callback for history_bytes. */
1.108 nicm 591: static void
1.80 nicm 592: format_cb_history_bytes(struct format_tree *ft, struct format_entry *fe)
593: {
594: struct window_pane *wp = ft->wp;
595: struct grid *gd;
596: struct grid_line *gl;
597: unsigned long long size;
598: u_int i;
599:
600: if (wp == NULL)
601: return;
602: gd = wp->base.grid;
603:
604: size = 0;
605: for (i = 0; i < gd->hsize; i++) {
1.158 nicm 606: gl = grid_get_line(gd, i);
1.80 nicm 607: size += gl->cellsize * sizeof *gl->celldata;
1.95 nicm 608: size += gl->extdsize * sizeof *gl->extddata;
1.80 nicm 609: }
1.158 nicm 610: size += gd->hsize * sizeof *gl;
1.80 nicm 611:
612: xasprintf(&fe->value, "%llu", size);
613: }
614:
615: /* Callback for pane_tabs. */
1.108 nicm 616: static void
1.80 nicm 617: format_cb_pane_tabs(struct format_tree *ft, struct format_entry *fe)
618: {
619: struct window_pane *wp = ft->wp;
620: struct evbuffer *buffer;
621: u_int i;
622: int size;
623:
624: if (wp == NULL)
625: return;
626:
627: buffer = evbuffer_new();
1.165 nicm 628: if (buffer == NULL)
629: fatalx("out of memory");
1.80 nicm 630: for (i = 0; i < wp->base.grid->sx; i++) {
631: if (!bit_test(wp->base.tabs, i))
632: continue;
633:
634: if (EVBUFFER_LENGTH(buffer) > 0)
635: evbuffer_add(buffer, ",", 1);
636: evbuffer_add_printf(buffer, "%u", i);
637: }
1.148 nicm 638: if ((size = EVBUFFER_LENGTH(buffer)) != 0)
639: xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer));
640: evbuffer_free(buffer);
641: }
642:
1.150 nicm 643: /* Callback for session_group_list. */
1.148 nicm 644: static void
1.150 nicm 645: format_cb_session_group_list(struct format_tree *ft, struct format_entry *fe)
1.148 nicm 646: {
647: struct session *s = ft->s;
648: struct session_group *sg;
649: struct session *loop;
650: struct evbuffer *buffer;
651: int size;
652:
653: if (s == NULL)
654: return;
655: sg = session_group_contains(s);
656: if (sg == NULL)
657: return;
658:
659: buffer = evbuffer_new();
1.165 nicm 660: if (buffer == NULL)
661: fatalx("out of memory");
1.148 nicm 662: TAILQ_FOREACH(loop, &sg->sessions, gentry) {
663: if (EVBUFFER_LENGTH(buffer) > 0)
664: evbuffer_add(buffer, ",", 1);
665: evbuffer_add_printf(buffer, "%s", loop->name);
666: }
667: if ((size = EVBUFFER_LENGTH(buffer)) != 0)
668: xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer));
1.80 nicm 669: evbuffer_free(buffer);
670: }
671:
1.168 nicm 672: /* Callback for pane_in_mode. */
673: static void
674: format_cb_pane_in_mode(struct format_tree *ft, struct format_entry *fe)
675: {
676: struct window_pane *wp = ft->wp;
677: u_int n = 0;
678: struct window_mode_entry *wme;
679:
680: if (wp == NULL)
681: return;
682:
683: TAILQ_FOREACH(wme, &wp->modes, entry)
684: n++;
685: xasprintf(&fe->value, "%u", n);
686: }
687:
1.186 nicm 688: /* Callback for cursor_character. */
689: static void
690: format_cb_cursor_character(struct format_tree *ft, struct format_entry *fe)
691: {
692: struct window_pane *wp = ft->wp;
693: struct grid_cell gc;
694:
695: if (wp == NULL)
696: return;
697:
698: grid_view_get_cell(wp->base.grid, wp->base.cx, wp->base.cy, &gc);
699: if (~gc.flags & GRID_FLAG_PADDING)
700: xasprintf(&fe->value, "%.*s", (int)gc.data.size, gc.data.data);
701: }
702:
1.112 nicm 703: /* Merge a format tree. */
704: static void
705: format_merge(struct format_tree *ft, struct format_tree *from)
706: {
707: struct format_entry *fe;
708:
709: RB_FOREACH(fe, format_entry_tree, &from->tree) {
710: if (fe->value != NULL)
711: format_add(ft, fe->key, "%s", fe->value);
712: }
713: }
714:
1.1 nicm 715: /* Create a new tree. */
716: struct format_tree *
1.131 nicm 717: format_create(struct client *c, struct cmdq_item *item, int tag, int flags)
1.68 nicm 718: {
1.184 nicm 719: struct format_tree *ft;
720: const struct window_mode **wm;
721: char tmp[64];
1.77 nicm 722:
723: if (!event_initialized(&format_job_event)) {
724: evtimer_set(&format_job_event, format_job_timer, NULL);
725: format_job_timer(-1, 0, NULL);
726: }
1.1 nicm 727:
1.54 nicm 728: ft = xcalloc(1, sizeof *ft);
729: RB_INIT(&ft->tree);
1.120 nicm 730:
1.131 nicm 731: if (c != NULL) {
732: ft->client = c;
733: ft->client->references++;
734: }
1.172 nicm 735: ft->item = item;
1.131 nicm 736:
1.120 nicm 737: ft->tag = tag;
1.84 nicm 738: ft->flags = flags;
1.177 nicm 739: ft->time = time(NULL);
1.1 nicm 740:
1.79 nicm 741: format_add_cb(ft, "host", format_cb_host);
742: format_add_cb(ft, "host_short", format_cb_host_short);
743: format_add_cb(ft, "pid", format_cb_pid);
1.100 nicm 744: format_add(ft, "socket_path", "%s", socket_path);
745: format_add_tv(ft, "start_time", &start_time);
1.102 nicm 746:
1.184 nicm 747: for (wm = all_window_modes; *wm != NULL; wm++) {
748: if ((*wm)->default_format != NULL) {
749: xsnprintf(tmp, sizeof tmp, "%s_format", (*wm)->name);
750: tmp[strcspn(tmp, "-")] = '_';
751: format_add(ft, tmp, "%s", (*wm)->default_format);
752: }
753: }
754:
1.130 nicm 755: if (item != NULL) {
756: if (item->cmd != NULL)
757: format_add(ft, "command", "%s", item->cmd->entry->name);
758: if (item->shared != NULL && item->shared->formats != NULL)
759: format_merge(ft, item->shared->formats);
760: }
1.1 nicm 761:
762: return (ft);
763: }
764:
765: /* Free a tree. */
766: void
767: format_free(struct format_tree *ft)
768: {
1.54 nicm 769: struct format_entry *fe, *fe1;
1.1 nicm 770:
1.68 nicm 771: RB_FOREACH_SAFE(fe, format_entry_tree, &ft->tree, fe1) {
772: RB_REMOVE(format_entry_tree, &ft->tree, fe);
1.9 nicm 773: free(fe->value);
774: free(fe->key);
775: free(fe);
1.1 nicm 776: }
777:
1.131 nicm 778: if (ft->client != NULL)
779: server_client_unref(ft->client);
1.25 nicm 780: free(ft);
1.1 nicm 781: }
1.184 nicm 782:
783: /* Walk each format. */
784: void
785: format_each(struct format_tree *ft, void (*cb)(const char *, const char *,
786: void *), void *arg)
787: {
788: struct format_entry *fe;
789: static char s[64];
790:
791: RB_FOREACH(fe, format_entry_tree, &ft->tree) {
792: if (fe->t != 0) {
793: xsnprintf(s, sizeof s, "%lld", (long long)fe->t);
794: cb(fe->key, fe->value, s);
795: } else {
796: if (fe->value == NULL && fe->cb != NULL) {
797: fe->cb(ft, fe);
798: if (fe->value == NULL)
799: fe->value = xstrdup("");
800: }
801: cb(fe->key, fe->value, arg);
802: }
803: }
804: }
805:
1.1 nicm 806:
807: /* Add a key-value pair. */
808: void
809: format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
810: {
811: struct format_entry *fe;
1.28 nicm 812: struct format_entry *fe_now;
1.1 nicm 813: va_list ap;
814:
815: fe = xmalloc(sizeof *fe);
816: fe->key = xstrdup(key);
817:
1.79 nicm 818: fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
819: if (fe_now != NULL) {
820: free(fe->key);
821: free(fe);
822: free(fe_now->value);
823: fe = fe_now;
824: }
825:
826: fe->cb = NULL;
1.87 nicm 827: fe->t = 0;
1.79 nicm 828:
1.1 nicm 829: va_start(ap, fmt);
830: xvasprintf(&fe->value, fmt, ap);
831: va_end(ap);
1.79 nicm 832: }
833:
1.87 nicm 834: /* Add a key and time. */
1.108 nicm 835: static void
1.87 nicm 836: format_add_tv(struct format_tree *ft, const char *key, struct timeval *tv)
837: {
838: struct format_entry *fe;
839: struct format_entry *fe_now;
840:
841: fe = xmalloc(sizeof *fe);
842: fe->key = xstrdup(key);
843:
844: fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
845: if (fe_now != NULL) {
846: free(fe->key);
847: free(fe);
848: free(fe_now->value);
849: fe = fe_now;
850: }
851:
852: fe->cb = NULL;
853: fe->t = tv->tv_sec;
854:
855: fe->value = NULL;
856: }
857:
1.79 nicm 858: /* Add a key and function. */
1.108 nicm 859: static void
1.79 nicm 860: format_add_cb(struct format_tree *ft, const char *key, format_cb cb)
861: {
862: struct format_entry *fe;
863: struct format_entry *fe_now;
864:
865: fe = xmalloc(sizeof *fe);
866: fe->key = xstrdup(key);
1.1 nicm 867:
1.68 nicm 868: fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
1.28 nicm 869: if (fe_now != NULL) {
870: free(fe->key);
871: free(fe);
1.79 nicm 872: free(fe_now->value);
873: fe = fe_now;
1.28 nicm 874: }
1.79 nicm 875:
876: fe->cb = cb;
1.87 nicm 877: fe->t = 0;
1.79 nicm 878:
879: fe->value = NULL;
1.1 nicm 880: }
881:
1.161 nicm 882: /* Quote special characters in string. */
883: static char *
884: format_quote(const char *s)
885: {
886: const char *cp;
887: char *out, *at;
888:
889: at = out = xmalloc(strlen(s) * 2 + 1);
890: for (cp = s; *cp != '\0'; cp++) {
891: if (strchr("|&;<>()$`\\\"'*?[# =%", *cp) != NULL)
892: *at++ = '\\';
893: *at++ = *cp;
894: }
895: *at = '\0';
896: return (out);
897: }
898:
1.1 nicm 899: /* Find a format entry. */
1.108 nicm 900: static char *
1.87 nicm 901: format_find(struct format_tree *ft, const char *key, int modifiers)
1.1 nicm 902: {
903: struct format_entry *fe, fe_find;
1.76 nicm 904: struct environ_entry *envent;
1.87 nicm 905: static char s[64];
1.117 nicm 906: struct options_entry *o;
1.87 nicm 907: const char *found;
1.116 nicm 908: int idx;
1.87 nicm 909: char *copy, *saved;
910:
911: if (~modifiers & FORMAT_TIMESTRING) {
1.116 nicm 912: o = options_parse_get(global_options, key, &idx, 0);
1.87 nicm 913: if (o == NULL && ft->w != NULL)
1.116 nicm 914: o = options_parse_get(ft->w->options, key, &idx, 0);
1.87 nicm 915: if (o == NULL)
1.116 nicm 916: o = options_parse_get(global_w_options, key, &idx, 0);
1.87 nicm 917: if (o == NULL && ft->s != NULL)
1.116 nicm 918: o = options_parse_get(ft->s->options, key, &idx, 0);
1.87 nicm 919: if (o == NULL)
1.116 nicm 920: o = options_parse_get(global_s_options, key, &idx, 0);
1.87 nicm 921: if (o != NULL) {
1.118 nicm 922: found = options_tostring(o, idx, 1);
1.116 nicm 923: goto found;
1.54 nicm 924: }
925: }
1.116 nicm 926: found = NULL;
1.1 nicm 927:
928: fe_find.key = (char *) key;
1.68 nicm 929: fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find);
1.79 nicm 930: if (fe != NULL) {
1.87 nicm 931: if (modifiers & FORMAT_TIMESTRING) {
932: if (fe->t == 0)
933: return (NULL);
934: ctime_r(&fe->t, s);
935: s[strcspn(s, "\n")] = '\0';
936: found = s;
937: goto found;
938: }
939: if (fe->t != 0) {
940: xsnprintf(s, sizeof s, "%lld", (long long)fe->t);
941: found = s;
942: goto found;
943: }
1.149 nicm 944: if (fe->value == NULL && fe->cb != NULL) {
1.79 nicm 945: fe->cb(ft, fe);
1.149 nicm 946: if (fe->value == NULL)
947: fe->value = xstrdup("");
948: }
1.87 nicm 949: found = fe->value;
950: goto found;
1.79 nicm 951: }
1.76 nicm 952:
1.87 nicm 953: if (~modifiers & FORMAT_TIMESTRING) {
954: envent = NULL;
955: if (ft->s != NULL)
1.91 nicm 956: envent = environ_find(ft->s->environ, key);
1.87 nicm 957: if (envent == NULL)
1.91 nicm 958: envent = environ_find(global_environ, key);
1.87 nicm 959: if (envent != NULL) {
960: found = envent->value;
961: goto found;
962: }
963: }
1.76 nicm 964:
965: return (NULL);
1.87 nicm 966:
967: found:
1.88 nicm 968: if (found == NULL)
969: return (NULL);
1.87 nicm 970: copy = xstrdup(found);
971: if (modifiers & FORMAT_BASENAME) {
972: saved = copy;
973: copy = xstrdup(basename(saved));
974: free(saved);
975: }
976: if (modifiers & FORMAT_DIRNAME) {
977: saved = copy;
978: copy = xstrdup(dirname(saved));
979: free(saved);
980: }
1.161 nicm 981: if (modifiers & FORMAT_QUOTE) {
982: saved = copy;
983: copy = xstrdup(format_quote(saved));
984: free(saved);
985: }
1.87 nicm 986: return (copy);
1.1 nicm 987: }
988:
1.155 nicm 989: /* Skip until end. */
1.185 nicm 990: const char *
1.169 nicm 991: format_skip(const char *s, const char *end)
1.114 nicm 992: {
993: int brackets = 0;
994:
995: for (; *s != '\0'; s++) {
1.155 nicm 996: if (*s == '#' && s[1] == '{')
1.114 nicm 997: brackets++;
1.155 nicm 998: if (*s == '#' && strchr(",#{}", s[1]) != NULL) {
999: s++;
1000: continue;
1001: }
1.114 nicm 1002: if (*s == '}')
1003: brackets--;
1.169 nicm 1004: if (strchr(end, *s) != NULL && brackets == 0)
1.114 nicm 1005: break;
1006: }
1007: if (*s == '\0')
1008: return (NULL);
1009: return (s);
1010: }
1011:
1012: /* Return left and right alternatives separated by commas. */
1013: static int
1.169 nicm 1014: format_choose(struct format_tree *ft, const char *s, char **left, char **right,
1015: int expand)
1.114 nicm 1016: {
1.169 nicm 1017: const char *cp;
1018: char *left0, *right0;
1.114 nicm 1019:
1.169 nicm 1020: cp = format_skip(s, ",");
1.114 nicm 1021: if (cp == NULL)
1022: return (-1);
1.169 nicm 1023: left0 = xstrndup(s, cp - s);
1024: right0 = xstrdup(cp + 1);
1.114 nicm 1025:
1.169 nicm 1026: if (expand) {
1027: *left = format_expand(ft, left0);
1.188 ! nicm 1028: free(left0);
1.169 nicm 1029: *right = format_expand(ft, right0);
1.188 ! nicm 1030: free(right0);
1.169 nicm 1031: } else {
1032: *left = left0;
1033: *right = right0;
1034: }
1.114 nicm 1035: return (0);
1036: }
1037:
1038: /* Is this true? */
1.141 nicm 1039: int
1.114 nicm 1040: format_true(const char *s)
1041: {
1042: if (s != NULL && *s != '\0' && (s[0] != '0' || s[1] != '\0'))
1043: return (1);
1044: return (0);
1045: }
1046:
1.169 nicm 1047: /* Check if modifier end. */
1048: static int
1049: format_is_end(char c)
1050: {
1051: return (c == ';' || c == ':');
1052: }
1053:
1054: /* Add to modifier list. */
1055: static void
1056: format_add_modifier(struct format_modifier **list, u_int *count,
1057: const char *c, size_t n, char **argv, int argc)
1058: {
1059: struct format_modifier *fm;
1060:
1061: *list = xreallocarray(*list, (*count) + 1, sizeof **list);
1062: fm = &(*list)[(*count)++];
1063:
1064: memcpy(fm->modifier, c, n);
1065: fm->modifier[n] = '\0';
1066: fm->size = n;
1067:
1068: fm->argv = argv;
1069: fm->argc = argc;
1070: }
1071:
1072: /* Free modifier list. */
1073: static void
1074: format_free_modifiers(struct format_modifier *list, u_int count)
1075: {
1076: u_int i;
1077:
1078: for (i = 0; i < count; i++)
1079: cmd_free_argv(list[i].argc, list[i].argv);
1080: free(list);
1081: }
1082:
1083: /* Build modifier list. */
1084: static struct format_modifier *
1085: format_build_modifiers(struct format_tree *ft, const char **s, u_int *count)
1086: {
1087: const char *cp = *s, *end;
1088: struct format_modifier *list = NULL;
1089: char c, last[] = "X;:", **argv, *value;
1090: int argc;
1091:
1092: /*
1093: * Modifiers are a ; separated list of the forms:
1.175 nicm 1094: * l,m,C,b,d,t,q,E,T,S,W,P
1.169 nicm 1095: * =a
1096: * =/a
1097: * =/a/
1098: * s/a/b/
1099: * s/a/b
1100: * ||,&&,!=,==
1101: */
1102:
1103: *count = 0;
1104:
1105: while (*cp != '\0' && *cp != ':') {
1106: /* Skip and separator character. */
1107: if (*cp == ';')
1108: cp++;
1109:
1110: /* Check single character modifiers with no arguments. */
1.175 nicm 1111: if (strchr("lmCbdtqETSWP", cp[0]) != NULL &&
1.172 nicm 1112: format_is_end(cp[1])) {
1.169 nicm 1113: format_add_modifier(&list, count, cp, 1, NULL, 0);
1114: cp++;
1115: continue;
1116: }
1117:
1118: /* Then try double character with no arguments. */
1119: if ((memcmp("||", cp, 2) == 0 ||
1120: memcmp("&&", cp, 2) == 0 ||
1121: memcmp("!=", cp, 2) == 0 ||
1122: memcmp("==", cp, 2) == 0) &&
1123: format_is_end(cp[2])) {
1124: format_add_modifier(&list, count, cp, 2, NULL, 0);
1125: cp += 2;
1126: continue;
1127: }
1128:
1129: /* Now try single character with arguments. */
1130: if (strchr("s=", cp[0]) == NULL)
1131: break;
1132: c = cp[0];
1133:
1134: /* No arguments provided. */
1135: if (format_is_end(cp[1])) {
1136: format_add_modifier(&list, count, cp, 1, NULL, 0);
1137: cp++;
1138: continue;
1139: }
1140: argv = NULL;
1141: argc = 0;
1142:
1143: /* Single argument with no wrapper character. */
1144: if (!ispunct(cp[1]) || cp[1] == '-') {
1145: end = format_skip(cp + 1, ":;");
1146: if (end == NULL)
1147: break;
1148:
1149: argv = xcalloc(1, sizeof *argv);
1150: value = xstrndup(cp + 1, end - (cp + 1));
1151: argv[0] = format_expand(ft, value);
1152: free(value);
1153: argc = 1;
1154:
1155: format_add_modifier(&list, count, &c, 1, argv, argc);
1156: cp = end;
1157: continue;
1158: }
1159:
1160: /* Multiple arguments with a wrapper character. */
1161: last[0] = cp[1];
1162: cp++;
1163: do {
1164: if (cp[0] == last[0] && format_is_end(cp[1])) {
1165: cp++;
1166: break;
1167: }
1168: end = format_skip(cp + 1, last);
1169: if (end == NULL)
1170: break;
1171: cp++;
1172:
1173: argv = xreallocarray (argv, argc + 1, sizeof *argv);
1174: value = xstrndup(cp, end - cp);
1175: argv[argc++] = format_expand(ft, value);
1176: free(value);
1177:
1178: cp = end;
1179: } while (!format_is_end(cp[0]));
1180: format_add_modifier(&list, count, &c, 1, argv, argc);
1181: }
1182: if (*cp != ':') {
1183: format_free_modifiers(list, *count);
1184: *count = 0;
1185: return (NULL);
1186: }
1187: *s = cp + 1;
1188: return list;
1189: }
1190:
1191: /* Perform substitution in string. */
1192: static char *
1193: format_substitute(const char *source, const char *from, const char *to)
1194: {
1195: char *copy, *new;
1196: const char *cp;
1197: size_t fromlen, tolen, newlen, used;
1198:
1199: fromlen = strlen(from);
1200: tolen = strlen(to);
1201:
1202: newlen = strlen(source) + 1;
1203: copy = new = xmalloc(newlen);
1204:
1205: for (cp = source; *cp != '\0'; /* nothing */) {
1206: if (strncmp(cp, from, fromlen) != 0) {
1207: *new++ = *cp++;
1208: continue;
1209: }
1210: used = new - copy;
1211:
1212: newlen += tolen;
1213: copy = xrealloc(copy, newlen);
1214:
1215: new = copy + used;
1216: memcpy(new, to, tolen);
1217:
1218: new += tolen;
1219: cp += fromlen;
1220: }
1221:
1222: *new = '\0';
1223: return (copy);
1224: }
1225:
1.172 nicm 1226: /* Loop over sessions. */
1227: static char *
1228: format_loop_sessions(struct format_tree *ft, const char *fmt)
1229: {
1.180 nicm 1230: struct client *c = ft->client;
1.172 nicm 1231: struct cmdq_item *item = ft->item;
1.180 nicm 1232: struct format_tree *nft;
1.172 nicm 1233: char *expanded, *value;
1234: size_t valuelen;
1235: struct session *s;
1236:
1237: value = xcalloc(1, 1);
1238: valuelen = 1;
1239:
1240: RB_FOREACH(s, sessions, &sessions) {
1.179 nicm 1241: format_log(ft, "session loop: $%u", s->id);
1.180 nicm 1242: nft = format_create(c, item, FORMAT_NONE, ft->flags);
1.182 nicm 1243: nft->loop = ft->loop;
1.180 nicm 1244: format_defaults(nft, ft->c, s, NULL, NULL);
1245: expanded = format_expand(nft, fmt);
1246: format_free(nft);
1.172 nicm 1247:
1248: valuelen += strlen(expanded);
1249: value = xrealloc(value, valuelen);
1250:
1251: strlcat(value, expanded, valuelen);
1252: free(expanded);
1253: }
1254:
1255: return (value);
1256: }
1257:
1258: /* Loop over windows. */
1259: static char *
1260: format_loop_windows(struct format_tree *ft, const char *fmt)
1261: {
1.180 nicm 1262: struct client *c = ft->client;
1.172 nicm 1263: struct cmdq_item *item = ft->item;
1.180 nicm 1264: struct format_tree *nft;
1.172 nicm 1265: char *all, *active, *use, *expanded, *value;
1266: size_t valuelen;
1267: struct winlink *wl;
1.180 nicm 1268: struct window *w;
1.172 nicm 1269:
1.179 nicm 1270: if (ft->s == NULL) {
1271: format_log(ft, "window loop but no session");
1.172 nicm 1272: return (NULL);
1.179 nicm 1273: }
1.172 nicm 1274:
1275: if (format_choose(ft, fmt, &all, &active, 0) != 0) {
1276: all = xstrdup(fmt);
1277: active = NULL;
1278: }
1279:
1280: value = xcalloc(1, 1);
1281: valuelen = 1;
1282:
1283: RB_FOREACH(wl, winlinks, &ft->s->windows) {
1.180 nicm 1284: w = wl->window;
1285: format_log(ft, "window loop: %u @%u", wl->idx, w->id);
1.172 nicm 1286: if (active != NULL && wl == ft->s->curw)
1287: use = active;
1288: else
1289: use = all;
1.180 nicm 1290: nft = format_create(c, item, FORMAT_WINDOW|w->id, ft->flags);
1.182 nicm 1291: nft->loop = ft->loop;
1.180 nicm 1292: format_defaults(nft, ft->c, ft->s, wl, NULL);
1293: expanded = format_expand(nft, use);
1294: format_free(nft);
1.172 nicm 1295:
1296: valuelen += strlen(expanded);
1297: value = xrealloc(value, valuelen);
1298:
1299: strlcat(value, expanded, valuelen);
1300: free(expanded);
1301: }
1302:
1303: free(active);
1304: free(all);
1305:
1306: return (value);
1307: }
1308:
1309: /* Loop over panes. */
1310: static char *
1311: format_loop_panes(struct format_tree *ft, const char *fmt)
1312: {
1.180 nicm 1313: struct client *c = ft->client;
1.172 nicm 1314: struct cmdq_item *item = ft->item;
1.180 nicm 1315: struct format_tree *nft;
1.172 nicm 1316: char *all, *active, *use, *expanded, *value;
1317: size_t valuelen;
1318: struct window_pane *wp;
1319:
1.179 nicm 1320: if (ft->w == NULL) {
1321: format_log(ft, "pane loop but no window");
1.172 nicm 1322: return (NULL);
1.179 nicm 1323: }
1.172 nicm 1324:
1325: if (format_choose(ft, fmt, &all, &active, 0) != 0) {
1326: all = xstrdup(fmt);
1327: active = NULL;
1328: }
1329:
1330: value = xcalloc(1, 1);
1331: valuelen = 1;
1332:
1333: TAILQ_FOREACH(wp, &ft->w->panes, entry) {
1.179 nicm 1334: format_log(ft, "pane loop: %%%u", wp->id);
1.172 nicm 1335: if (active != NULL && wp == ft->w->active)
1336: use = active;
1337: else
1338: use = all;
1.180 nicm 1339: nft = format_create(c, item, FORMAT_PANE|wp->id, ft->flags);
1.182 nicm 1340: nft->loop = ft->loop;
1.180 nicm 1341: format_defaults(nft, ft->c, ft->s, ft->wl, wp);
1342: expanded = format_expand(nft, use);
1343: format_free(nft);
1.172 nicm 1344:
1345: valuelen += strlen(expanded);
1346: value = xrealloc(value, valuelen);
1347:
1348: strlcat(value, expanded, valuelen);
1349: free(expanded);
1350: }
1351:
1352: free(active);
1353: free(all);
1354:
1355: return (value);
1356: }
1357:
1.140 nicm 1358: /* Replace a key. */
1.108 nicm 1359: static int
1.30 nicm 1360: format_replace(struct format_tree *ft, const char *key, size_t keylen,
1361: char **buf, size_t *len, size_t *off)
1.1 nicm 1362: {
1.140 nicm 1363: struct window_pane *wp = ft->wp;
1.169 nicm 1364: const char *errptr, *copy, *cp;
1365: char *copy0, *condition, *found, *new;
1366: char *value, *left, *right;
1367: size_t valuelen;
1368: int modifiers = 0, limit = 0;
1369: struct format_modifier *list, *fm, *cmp = NULL, *search = NULL;
1370: struct format_modifier *sub = NULL;
1371: u_int i, count;
1.179 nicm 1372: int j;
1.1 nicm 1373:
1374: /* Make a copy of the key. */
1.169 nicm 1375: copy = copy0 = xstrndup(key, keylen);
1376:
1377: /* Process modifier list. */
1378: list = format_build_modifiers(ft, ©, &count);
1379: for (i = 0; i < count; i++) {
1380: fm = &list[i];
1.179 nicm 1381: if (format_logging(ft)) {
1382: format_log(ft, "modifier %u is %s", i, fm->modifier);
1383: for (j = 0; j < fm->argc; j++) {
1384: format_log(ft, "modifier %u argument %d: %s", i,
1385: j, fm->argv[j]);
1386: }
1387: }
1.169 nicm 1388: if (fm->size == 1) {
1389: switch (fm->modifier[0]) {
1390: case 'm':
1391: cmp = fm;
1392: break;
1393: case 'C':
1394: search = fm;
1395: break;
1396: case 's':
1397: if (fm->argc != 2)
1398: break;
1399: sub = fm;
1400: break;
1401: case '=':
1402: if (fm->argc != 1)
1403: break;
1404: limit = strtonum(fm->argv[0], INT_MIN, INT_MAX,
1405: &errptr);
1406: if (errptr != NULL)
1407: limit = 0;
1408: break;
1409: case 'l':
1410: modifiers |= FORMAT_LITERAL;
1411: break;
1412: case 'b':
1413: modifiers |= FORMAT_BASENAME;
1414: break;
1415: case 'd':
1416: modifiers |= FORMAT_DIRNAME;
1417: break;
1418: case 't':
1419: modifiers |= FORMAT_TIMESTRING;
1420: break;
1421: case 'q':
1422: modifiers |= FORMAT_QUOTE;
1423: break;
1.170 nicm 1424: case 'E':
1425: modifiers |= FORMAT_EXPAND;
1426: break;
1.175 nicm 1427: case 'T':
1428: modifiers |= FORMAT_EXPANDTIME;
1429: break;
1.172 nicm 1430: case 'S':
1431: modifiers |= FORMAT_SESSIONS;
1432: break;
1433: case 'W':
1434: modifiers |= FORMAT_WINDOWS;
1435: break;
1436: case 'P':
1437: modifiers |= FORMAT_PANES;
1438: break;
1.169 nicm 1439: }
1440: } else if (fm->size == 2) {
1441: if (strcmp(fm->modifier, "||") == 0 ||
1442: strcmp(fm->modifier, "&&") == 0 ||
1443: strcmp(fm->modifier, "==") == 0 ||
1444: strcmp(fm->modifier, "!=") == 0)
1445: cmp = fm;
1.98 nicm 1446: }
1.30 nicm 1447: }
1448:
1.155 nicm 1449: /* Is this a literal string? */
1.169 nicm 1450: if (modifiers & FORMAT_LITERAL) {
1.155 nicm 1451: value = xstrdup(copy);
1452: goto done;
1453: }
1454:
1.172 nicm 1455: /* Is this a loop, comparison or condition? */
1456: if (modifiers & FORMAT_SESSIONS) {
1457: value = format_loop_sessions(ft, copy);
1458: if (value == NULL)
1459: goto fail;
1460: } else if (modifiers & FORMAT_WINDOWS) {
1461: value = format_loop_windows(ft, copy);
1462: if (value == NULL)
1463: goto fail;
1464: } else if (modifiers & FORMAT_PANES) {
1465: value = format_loop_panes(ft, copy);
1466: if (value == NULL)
1467: goto fail;
1468: } else if (search != NULL) {
1.140 nicm 1469: /* Search in pane. */
1.179 nicm 1470: if (wp == NULL) {
1471: format_log(ft, "search '%s' but no pane", copy);
1.140 nicm 1472: value = xstrdup("0");
1.179 nicm 1473: } else {
1474: format_log(ft, "search '%s' pane %%%u", copy, wp->id);
1.140 nicm 1475: xasprintf(&value, "%u", window_pane_search(wp, copy));
1.179 nicm 1476: }
1.169 nicm 1477: } else if (cmp != NULL) {
1478: /* Comparison of left and right. */
1.179 nicm 1479: if (format_choose(ft, copy, &left, &right, 1) != 0) {
1480: format_log(ft, "compare %s syntax error: %s",
1481: cmp->modifier, copy);
1.114 nicm 1482: goto fail;
1.179 nicm 1483: }
1484: format_log(ft, "compare %s left is: %s", cmp->modifier, left);
1485: format_log(ft, "compare %s right is: %s", cmp->modifier, right);
1.169 nicm 1486:
1487: if (strcmp(cmp->modifier, "||") == 0) {
1488: if (format_true(left) || format_true(right))
1489: value = xstrdup("1");
1490: else
1491: value = xstrdup("0");
1492: } else if (strcmp(cmp->modifier, "&&") == 0) {
1493: if (format_true(left) && format_true(right))
1494: value = xstrdup("1");
1495: else
1496: value = xstrdup("0");
1497: } else if (strcmp(cmp->modifier, "==") == 0) {
1498: if (strcmp(left, right) == 0)
1499: value = xstrdup("1");
1500: else
1501: value = xstrdup("0");
1502: } else if (strcmp(cmp->modifier, "!=") == 0) {
1503: if (strcmp(left, right) != 0)
1504: value = xstrdup("1");
1505: else
1506: value = xstrdup("0");
1507: }
1508: else if (strcmp(cmp->modifier, "m") == 0) {
1509: if (fnmatch(left, right, 0) == 0)
1510: value = xstrdup("1");
1511: else
1512: value = xstrdup("0");
1513: }
1514:
1.114 nicm 1515: free(right);
1516: free(left);
1517: } else if (*copy == '?') {
1518: /* Conditional: check first and choose second or third. */
1.169 nicm 1519: cp = format_skip(copy + 1, ",");
1.179 nicm 1520: if (cp == NULL) {
1521: format_log(ft, "condition syntax error: %s", copy + 1);
1.1 nicm 1522: goto fail;
1.179 nicm 1523: }
1.169 nicm 1524: condition = xstrndup(copy + 1, cp - (copy + 1));
1.179 nicm 1525: format_log(ft, "condition is: %s", condition);
1.1 nicm 1526:
1.169 nicm 1527: found = format_find(ft, condition, modifiers);
1.156 nicm 1528: if (found == NULL) {
1529: /*
1.169 nicm 1530: * If the condition not found, try to expand it. If
1.156 nicm 1531: * the expansion doesn't have any effect, then assume
1532: * false.
1533: */
1.169 nicm 1534: found = format_expand(ft, condition);
1535: if (strcmp(found, condition) == 0) {
1.156 nicm 1536: free(found);
1537: found = xstrdup("");
1.179 nicm 1538: format_log(ft, "condition '%s' found: %s",
1539: condition, found);
1540: } else {
1541: format_log(ft,
1542: "condition '%s' not found; assuming false",
1543: condition);
1.156 nicm 1544: }
1.179 nicm 1545: } else
1546: format_log(ft, "condition '%s' found", condition);
1.169 nicm 1547:
1548: if (format_choose(ft, cp + 1, &left, &right, 0) != 0) {
1.179 nicm 1549: format_log(ft, "condition '%s' syntax error: %s",
1550: condition, cp + 1);
1.162 nicm 1551: free(found);
1.89 nicm 1552: goto fail;
1.162 nicm 1553: }
1.179 nicm 1554: if (format_true(found)) {
1555: format_log(ft, "condition '%s' is true", condition);
1.114 nicm 1556: value = format_expand(ft, left);
1.179 nicm 1557: } else {
1558: format_log(ft, "condition '%s' is false", condition);
1.114 nicm 1559: value = format_expand(ft, right);
1.179 nicm 1560: }
1.169 nicm 1561: free(right);
1562: free(left);
1563:
1.179 nicm 1564: free(condition);
1.98 nicm 1565: free(found);
1.1 nicm 1566: } else {
1.114 nicm 1567: /* Neither: look up directly. */
1.98 nicm 1568: value = format_find(ft, copy, modifiers);
1.179 nicm 1569: if (value == NULL) {
1570: format_log(ft, "format '%s' not found", copy);
1.98 nicm 1571: value = xstrdup("");
1.179 nicm 1572: } else
1573: format_log(ft, "format '%s' found: %s", copy, value);
1.170 nicm 1574: }
1575:
1.171 nicm 1576: done:
1.170 nicm 1577: /* Expand again if required. */
1578: if (modifiers & FORMAT_EXPAND) {
1579: new = format_expand(ft, value);
1.175 nicm 1580: free(value);
1581: value = new;
1582: }
1583: else if (modifiers & FORMAT_EXPANDTIME) {
1.177 nicm 1584: new = format_expand_time(ft, value);
1.170 nicm 1585: free(value);
1586: value = new;
1.98 nicm 1587: }
1588:
1589: /* Perform substitution if any. */
1.169 nicm 1590: if (sub != NULL) {
1591: new = format_substitute(value, sub->argv[0], sub->argv[1]);
1.179 nicm 1592: format_log(ft, "substituted '%s' to '%s: %s", sub->argv[0],
1593: sub->argv[1], new);
1.98 nicm 1594: free(value);
1.169 nicm 1595: value = new;
1.1 nicm 1596: }
1597:
1.30 nicm 1598: /* Truncate the value if needed. */
1.105 nicm 1599: if (limit > 0) {
1.185 nicm 1600: new = format_trim_left(value, limit);
1.179 nicm 1601: format_log(ft, "applied length limit %d: %s", limit, new);
1.105 nicm 1602: free(value);
1603: value = new;
1604: } else if (limit < 0) {
1.185 nicm 1605: new = format_trim_right(value, -limit);
1.179 nicm 1606: format_log(ft, "applied length limit %d: %s", limit, new);
1.98 nicm 1607: free(value);
1608: value = new;
1.44 nicm 1609: }
1.30 nicm 1610:
1.1 nicm 1611: /* Expand the buffer and copy in the value. */
1.98 nicm 1612: valuelen = strlen(value);
1.1 nicm 1613: while (*len - *off < valuelen + 1) {
1.50 nicm 1614: *buf = xreallocarray(*buf, 2, *len);
1.1 nicm 1615: *len *= 2;
1616: }
1617: memcpy(*buf + *off, value, valuelen);
1618: *off += valuelen;
1619:
1.179 nicm 1620: format_log(ft, "replaced '%s' with '%s'", copy0, value);
1.98 nicm 1621: free(value);
1.179 nicm 1622:
1.169 nicm 1623: format_free_modifiers(list, count);
1.30 nicm 1624: free(copy0);
1.1 nicm 1625: return (0);
1626:
1627: fail:
1.179 nicm 1628: format_log(ft, "failed %s", copy0);
1.169 nicm 1629: format_free_modifiers(list, count);
1.30 nicm 1630: free(copy0);
1.1 nicm 1631: return (-1);
1632: }
1633:
1634: /* Expand keys in a template. */
1.179 nicm 1635: static char *
1636: format_expand1(struct format_tree *ft, const char *fmt, int time)
1.1 nicm 1637: {
1.152 nicm 1638: char *buf, *out, *name;
1.179 nicm 1639: const char *ptr, *s;
1.86 nicm 1640: size_t off, len, n, outlen;
1.31 nicm 1641: int ch, brackets;
1.179 nicm 1642: struct tm *tm;
1643: char expanded[8192];
1.58 nicm 1644:
1.179 nicm 1645: if (fmt == NULL || *fmt == '\0')
1.58 nicm 1646: return (xstrdup(""));
1.1 nicm 1647:
1.178 nicm 1648: if (ft->loop == FORMAT_LOOP_LIMIT)
1649: return (xstrdup(""));
1650: ft->loop++;
1651:
1.179 nicm 1652: format_log(ft, "expanding format: %s", fmt);
1653:
1654: if (time) {
1655: tm = localtime(&ft->time);
1656: if (strftime(expanded, sizeof expanded, fmt, tm) == 0) {
1657: format_log(ft, "format is too long");
1658: return (xstrdup(""));
1659: }
1660: if (format_logging(ft) && strcmp(expanded, fmt) != 0)
1661: format_log(ft, "after time expanded: %s", expanded);
1662: fmt = expanded;
1663: }
1664:
1.1 nicm 1665: len = 64;
1666: buf = xmalloc(len);
1667: off = 0;
1668:
1669: while (*fmt != '\0') {
1670: if (*fmt != '#') {
1671: while (len - off < 2) {
1.50 nicm 1672: buf = xreallocarray(buf, 2, len);
1.1 nicm 1673: len *= 2;
1674: }
1675: buf[off++] = *fmt++;
1676: continue;
1677: }
1678: fmt++;
1679:
1.179 nicm 1680: ch = (u_char)*fmt++;
1.1 nicm 1681: switch (ch) {
1.68 nicm 1682: case '(':
1683: brackets = 1;
1684: for (ptr = fmt; *ptr != '\0'; ptr++) {
1685: if (*ptr == '(')
1686: brackets++;
1687: if (*ptr == ')' && --brackets == 0)
1688: break;
1689: }
1690: if (*ptr != ')' || brackets != 0)
1691: break;
1692: n = ptr - fmt;
1693:
1.179 nicm 1694: name = xstrndup(fmt, n);
1695: format_log(ft, "found #(): %s", name);
1696:
1697: if (ft->flags & FORMAT_NOJOBS) {
1.114 nicm 1698: out = xstrdup("");
1.179 nicm 1699: format_log(ft, "#() is disabled");
1700: } else {
1.152 nicm 1701: out = format_job_get(ft, name);
1.179 nicm 1702: format_log(ft, "#() result: %s", out);
1.152 nicm 1703: }
1.179 nicm 1704: free(name);
1705:
1.86 nicm 1706: outlen = strlen(out);
1707: while (len - off < outlen + 1) {
1.68 nicm 1708: buf = xreallocarray(buf, 2, len);
1709: len *= 2;
1710: }
1.86 nicm 1711: memcpy(buf + off, out, outlen);
1712: off += outlen;
1713:
1714: free(out);
1.68 nicm 1715:
1716: fmt += n + 1;
1717: continue;
1.1 nicm 1718: case '{':
1.169 nicm 1719: ptr = format_skip((char *)fmt - 2, "}");
1.155 nicm 1720: if (ptr == NULL)
1.1 nicm 1721: break;
1722: n = ptr - fmt;
1723:
1.179 nicm 1724: format_log(ft, "found #{}: %.*s", (int)n, fmt);
1.1 nicm 1725: if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
1726: break;
1727: fmt += n + 1;
1.40 nicm 1728: continue;
1.155 nicm 1729: case '}':
1.40 nicm 1730: case '#':
1.155 nicm 1731: case ',':
1.179 nicm 1732: format_log(ft, "found #%c", ch);
1.40 nicm 1733: while (len - off < 2) {
1.50 nicm 1734: buf = xreallocarray(buf, 2, len);
1.40 nicm 1735: len *= 2;
1736: }
1.155 nicm 1737: buf[off++] = ch;
1.1 nicm 1738: continue;
1739: default:
1.25 nicm 1740: s = NULL;
1741: if (ch >= 'A' && ch <= 'Z')
1742: s = format_upper[ch - 'A'];
1743: else if (ch >= 'a' && ch <= 'z')
1744: s = format_lower[ch - 'a'];
1745: if (s == NULL) {
1746: while (len - off < 3) {
1.50 nicm 1747: buf = xreallocarray(buf, 2, len);
1.25 nicm 1748: len *= 2;
1.1 nicm 1749: }
1.25 nicm 1750: buf[off++] = '#';
1751: buf[off++] = ch;
1752: continue;
1.1 nicm 1753: }
1.25 nicm 1754: n = strlen(s);
1.179 nicm 1755: format_log(ft, "found #%c: %s", ch, s);
1.25 nicm 1756: if (format_replace(ft, s, n, &buf, &len, &off) != 0)
1757: break;
1.1 nicm 1758: continue;
1759: }
1760:
1761: break;
1762: }
1763: buf[off] = '\0';
1764:
1.179 nicm 1765: format_log(ft, "result is: %s", buf);
1766: ft->loop--;
1.178 nicm 1767:
1.1 nicm 1768: return (buf);
1.179 nicm 1769: }
1770:
1771: /* Expand keys in a template, passing through strftime first. */
1772: char *
1773: format_expand_time(struct format_tree *ft, const char *fmt)
1774: {
1775: return (format_expand1(ft, fmt, 1));
1776: }
1777:
1778: /* Expand keys in a template. */
1779: char *
1780: format_expand(struct format_tree *ft, const char *fmt)
1781: {
1782: return (format_expand1(ft, fmt, 0));
1.123 nicm 1783: }
1784:
1785: /* Expand a single string. */
1786: char *
1787: format_single(struct cmdq_item *item, const char *fmt, struct client *c,
1788: struct session *s, struct winlink *wl, struct window_pane *wp)
1789: {
1790: struct format_tree *ft;
1791: char *expanded;
1792:
1.131 nicm 1793: if (item != NULL)
1794: ft = format_create(item->client, item, FORMAT_NONE, 0);
1795: else
1796: ft = format_create(NULL, item, FORMAT_NONE, 0);
1.123 nicm 1797: format_defaults(ft, c, s, wl, wp);
1798:
1799: expanded = format_expand(ft, fmt);
1800: format_free(ft);
1801: return (expanded);
1.1 nicm 1802: }
1803:
1.57 nicm 1804: /* Set defaults for any of arguments that are not NULL. */
1805: void
1806: format_defaults(struct format_tree *ft, struct client *c, struct session *s,
1807: struct winlink *wl, struct window_pane *wp)
1808: {
1.187 nicm 1809: if (c != NULL)
1810: log_debug("%s: c=%s", __func__, c->name);
1811: else
1812: log_debug("%s: s=none", __func__);
1813: if (s != NULL)
1814: log_debug("%s: s=$%u", __func__, s->id);
1815: else
1816: log_debug("%s: s=none", __func__);
1817: if (wl != NULL)
1818: log_debug("%s: wl=%u w=@%u", __func__, wl->idx, wl->window->id);
1819: else
1820: log_debug("%s: wl=none", __func__);
1821: if (wp != NULL)
1822: log_debug("%s: wp=%%%u", __func__, wp->id);
1823: else
1824: log_debug("%s: wp=none", __func__);
1825:
1.154 nicm 1826: if (c != NULL && s != NULL && c->session != s)
1827: log_debug("%s: session does not match", __func__);
1828:
1.146 nicm 1829: format_add(ft, "session_format", "%d", s != NULL);
1830: format_add(ft, "window_format", "%d", wl != NULL);
1831: format_add(ft, "pane_format", "%d", wp != NULL);
1832:
1.57 nicm 1833: if (s == NULL && c != NULL)
1834: s = c->session;
1835: if (wl == NULL && s != NULL)
1836: wl = s->curw;
1837: if (wp == NULL && wl != NULL)
1838: wp = wl->window->active;
1839:
1840: if (c != NULL)
1841: format_defaults_client(ft, c);
1842: if (s != NULL)
1843: format_defaults_session(ft, s);
1.129 nicm 1844: if (wl != NULL)
1845: format_defaults_winlink(ft, wl);
1.57 nicm 1846: if (wp != NULL)
1847: format_defaults_pane(ft, wp);
1848: }
1849:
1.1 nicm 1850: /* Set default format keys for a session. */
1.108 nicm 1851: static void
1.57 nicm 1852: format_defaults_session(struct format_tree *ft, struct session *s)
1.1 nicm 1853: {
1854: struct session_group *sg;
1855:
1.54 nicm 1856: ft->s = s;
1857:
1.1 nicm 1858: format_add(ft, "session_name", "%s", s->name);
1859: format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
1.23 nicm 1860: format_add(ft, "session_id", "$%u", s->id);
1.1 nicm 1861:
1.122 nicm 1862: sg = session_group_contains(s);
1.1 nicm 1863: format_add(ft, "session_grouped", "%d", sg != NULL);
1.148 nicm 1864: if (sg != NULL) {
1.122 nicm 1865: format_add(ft, "session_group", "%s", sg->name);
1.148 nicm 1866: format_add(ft, "session_group_size", "%u",
1867: session_group_count (sg));
1.150 nicm 1868: format_add_cb(ft, "session_group_list",
1869: format_cb_session_group_list);
1.148 nicm 1870: }
1.1 nicm 1871:
1.87 nicm 1872: format_add_tv(ft, "session_created", &s->creation_time);
1873: format_add_tv(ft, "session_last_attached", &s->last_attached_time);
1874: format_add_tv(ft, "session_activity", &s->activity_time);
1.1 nicm 1875:
1.41 nicm 1876: format_add(ft, "session_attached", "%u", s->attached);
1.59 nicm 1877: format_add(ft, "session_many_attached", "%d", s->attached > 1);
1.66 nicm 1878:
1.80 nicm 1879: format_add_cb(ft, "session_alerts", format_cb_session_alerts);
1.133 nicm 1880: format_add_cb(ft, "session_stack", format_cb_session_stack);
1.3 nicm 1881: }
1882:
1883: /* Set default format keys for a client. */
1.108 nicm 1884: static void
1.57 nicm 1885: format_defaults_client(struct format_tree *ft, struct client *c)
1.3 nicm 1886: {
1.60 nicm 1887: struct session *s;
1.103 nicm 1888: const char *name;
1.115 nicm 1889: struct tty *tty = &c->tty;
1890: const char *types[] = TTY_TYPES;
1.3 nicm 1891:
1.54 nicm 1892: if (ft->s == NULL)
1893: ft->s = c->session;
1.164 nicm 1894: ft->c = c;
1.54 nicm 1895:
1.124 nicm 1896: format_add(ft, "client_name", "%s", c->name);
1.72 nicm 1897: format_add(ft, "client_pid", "%ld", (long) c->pid);
1.115 nicm 1898: format_add(ft, "client_height", "%u", tty->sy);
1899: format_add(ft, "client_width", "%u", tty->sx);
1.124 nicm 1900: format_add(ft, "client_tty", "%s", c->ttyname);
1.75 nicm 1901: format_add(ft, "client_control_mode", "%d",
1902: !!(c->flags & CLIENT_CONTROL));
1.3 nicm 1903:
1.115 nicm 1904: if (tty->term_name != NULL)
1905: format_add(ft, "client_termname", "%s", tty->term_name);
1906: if (tty->term_name != NULL)
1907: format_add(ft, "client_termtype", "%s", types[tty->term_type]);
1908:
1.87 nicm 1909: format_add_tv(ft, "client_created", &c->creation_time);
1910: format_add_tv(ft, "client_activity", &c->activity_time);
1.126 nicm 1911:
1912: format_add(ft, "client_written", "%zu", c->written);
1913: format_add(ft, "client_discarded", "%zu", c->discarded);
1.14 nicm 1914:
1.103 nicm 1915: name = server_client_get_key_table(c);
1916: if (strcmp(c->keytable->name, name) == 0)
1.61 nicm 1917: format_add(ft, "client_prefix", "%d", 0);
1918: else
1919: format_add(ft, "client_prefix", "%d", 1);
1920: format_add(ft, "client_key_table", "%s", c->keytable->name);
1.3 nicm 1921:
1.115 nicm 1922: if (tty->flags & TTY_UTF8)
1.3 nicm 1923: format_add(ft, "client_utf8", "%d", 1);
1924: else
1925: format_add(ft, "client_utf8", "%d", 0);
1926:
1927: if (c->flags & CLIENT_READONLY)
1928: format_add(ft, "client_readonly", "%d", 1);
1929: else
1930: format_add(ft, "client_readonly", "%d", 0);
1.15 nicm 1931:
1932: s = c->session;
1933: if (s != NULL)
1934: format_add(ft, "client_session", "%s", s->name);
1935: s = c->last_session;
1936: if (s != NULL && session_alive(s))
1937: format_add(ft, "client_last_session", "%s", s->name);
1.1 nicm 1938: }
1939:
1.32 nicm 1940: /* Set default format keys for a window. */
1941: void
1.57 nicm 1942: format_defaults_window(struct format_tree *ft, struct window *w)
1.32 nicm 1943: {
1.54 nicm 1944: ft->w = w;
1945:
1.87 nicm 1946: format_add_tv(ft, "window_activity", &w->activity_time);
1.32 nicm 1947: format_add(ft, "window_id", "@%u", w->id);
1948: format_add(ft, "window_name", "%s", w->name);
1949: format_add(ft, "window_width", "%u", w->sx);
1950: format_add(ft, "window_height", "%u", w->sy);
1.80 nicm 1951: format_add_cb(ft, "window_layout", format_cb_window_layout);
1.96 nicm 1952: format_add_cb(ft, "window_visible_layout",
1953: format_cb_window_visible_layout);
1.32 nicm 1954: format_add(ft, "window_panes", "%u", window_count_panes(w));
1.59 nicm 1955: format_add(ft, "window_zoomed_flag", "%d",
1.53 nicm 1956: !!(w->flags & WINDOW_ZOOMED));
1.32 nicm 1957: }
1958:
1.1 nicm 1959: /* Set default format keys for a winlink. */
1.108 nicm 1960: static void
1.129 nicm 1961: format_defaults_winlink(struct format_tree *ft, struct winlink *wl)
1.1 nicm 1962: {
1.164 nicm 1963: struct client *c = ft->c;
1.129 nicm 1964: struct session *s = wl->session;
1.1 nicm 1965: struct window *w = wl->window;
1.164 nicm 1966: int flag;
1967: u_int ox, oy, sx, sy;
1.1 nicm 1968:
1.54 nicm 1969: if (ft->w == NULL)
1970: ft->w = wl->window;
1.133 nicm 1971: ft->wl = wl;
1.54 nicm 1972:
1.57 nicm 1973: format_defaults_window(ft, w);
1.32 nicm 1974:
1.164 nicm 1975: if (c != NULL) {
1976: flag = tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
1977: format_add(ft, "window_bigger", "%d", flag);
1978: if (flag) {
1979: format_add(ft, "window_offset_x", "%u", ox);
1980: format_add(ft, "window_offset_y", "%u", oy);
1981: }
1982: }
1983:
1.1 nicm 1984: format_add(ft, "window_index", "%d", wl->idx);
1.133 nicm 1985: format_add_cb(ft, "window_stack_index", format_cb_window_stack_index);
1.129 nicm 1986: format_add(ft, "window_flags", "%s", window_printable_flags(wl));
1.1 nicm 1987: format_add(ft, "window_active", "%d", wl == s->curw);
1.176 nicm 1988:
1989: format_add(ft, "window_start_flag", "%d",
1990: !!(wl == RB_MIN(winlinks, &s->windows)));
1991: format_add(ft, "window_end_flag", "%d",
1992: !!(wl == RB_MAX(winlinks, &s->windows)));
1.29 nicm 1993:
1.59 nicm 1994: format_add(ft, "window_bell_flag", "%d",
1.29 nicm 1995: !!(wl->flags & WINLINK_BELL));
1.59 nicm 1996: format_add(ft, "window_activity_flag", "%d",
1.29 nicm 1997: !!(wl->flags & WINLINK_ACTIVITY));
1.59 nicm 1998: format_add(ft, "window_silence_flag", "%d",
1.29 nicm 1999: !!(wl->flags & WINLINK_SILENCE));
1.59 nicm 2000: format_add(ft, "window_last_flag", "%d",
1.49 nicm 2001: !!(wl == TAILQ_FIRST(&s->lastw)));
1.64 nicm 2002: format_add(ft, "window_linked", "%d", session_is_linked(s, wl->window));
1.1 nicm 2003: }
2004:
2005: /* Set default format keys for a window pane. */
2006: void
1.57 nicm 2007: format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
1.1 nicm 2008: {
1.168 nicm 2009: struct window *w = wp->window;
2010: struct grid *gd = wp->base.grid;
2011: int status = wp->status;
2012: u_int idx;
2013: struct window_mode_entry *wme;
1.54 nicm 2014:
2015: if (ft->w == NULL)
1.159 nicm 2016: ft->w = w;
1.79 nicm 2017: ft->wp = wp;
1.1 nicm 2018:
1.16 nicm 2019: format_add(ft, "history_size", "%u", gd->hsize);
2020: format_add(ft, "history_limit", "%u", gd->hlimit);
1.80 nicm 2021: format_add_cb(ft, "history_bytes", format_cb_history_bytes);
1.1 nicm 2022:
1.4 nicm 2023: if (window_pane_index(wp, &idx) != 0)
2024: fatalx("index not found");
1.16 nicm 2025: format_add(ft, "pane_index", "%u", idx);
1.4 nicm 2026:
1.1 nicm 2027: format_add(ft, "pane_width", "%u", wp->sx);
2028: format_add(ft, "pane_height", "%u", wp->sy);
2029: format_add(ft, "pane_title", "%s", wp->base.title);
2030: format_add(ft, "pane_id", "%%%u", wp->id);
1.159 nicm 2031: format_add(ft, "pane_active", "%d", wp == w->active);
1.55 nicm 2032: format_add(ft, "pane_input_off", "%d", !!(wp->flags & PANE_INPUTOFF));
1.143 nicm 2033: format_add(ft, "pane_pipe", "%d", wp->pipe_fd != -1);
1.55 nicm 2034:
1.147 nicm 2035: if ((wp->flags & PANE_STATUSREADY) && WIFEXITED(status))
1.55 nicm 2036: format_add(ft, "pane_dead_status", "%d", WEXITSTATUS(status));
1.1 nicm 2037: format_add(ft, "pane_dead", "%d", wp->fd == -1);
1.47 nicm 2038:
1.164 nicm 2039: format_add(ft, "pane_left", "%u", wp->xoff);
2040: format_add(ft, "pane_top", "%u", wp->yoff);
2041: format_add(ft, "pane_right", "%u", wp->xoff + wp->sx - 1);
2042: format_add(ft, "pane_bottom", "%u", wp->yoff + wp->sy - 1);
2043: format_add(ft, "pane_at_left", "%d", wp->xoff == 0);
2044: format_add(ft, "pane_at_top", "%d", wp->yoff == 0);
2045: format_add(ft, "pane_at_right", "%d", wp->xoff + wp->sx == w->sx);
2046: format_add(ft, "pane_at_bottom", "%d", wp->yoff + wp->sy == w->sy);
1.16 nicm 2047:
1.168 nicm 2048: wme = TAILQ_FIRST(&wp->modes);
2049: if (wme != NULL) {
2050: format_add(ft, "pane_mode", "%s", wme->mode->name);
2051: if (wme->mode->formats != NULL)
2052: wme->mode->formats(wme, ft);
2053: }
2054: format_add_cb(ft, "pane_in_mode", format_cb_pane_in_mode);
1.134 nicm 2055:
1.26 nicm 2056: format_add(ft, "pane_synchronized", "%d",
1.159 nicm 2057: !!options_get_number(w->options, "synchronize-panes"));
1.135 nicm 2058: if (wp->searchstr != NULL)
2059: format_add(ft, "pane_search_string", "%s", wp->searchstr);
1.16 nicm 2060:
1.71 nicm 2061: format_add(ft, "pane_tty", "%s", wp->tty);
1.16 nicm 2062: format_add(ft, "pane_pid", "%ld", (long) wp->pid);
1.79 nicm 2063: format_add_cb(ft, "pane_start_command", format_cb_start_command);
2064: format_add_cb(ft, "pane_current_command", format_cb_current_command);
1.16 nicm 2065:
1.59 nicm 2066: format_add(ft, "cursor_x", "%u", wp->base.cx);
2067: format_add(ft, "cursor_y", "%u", wp->base.cy);
1.186 nicm 2068: format_add_cb(ft, "cursor_character", format_cb_cursor_character);
2069:
1.59 nicm 2070: format_add(ft, "scroll_region_upper", "%u", wp->base.rupper);
2071: format_add(ft, "scroll_region_lower", "%u", wp->base.rlower);
1.16 nicm 2072:
2073: format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0);
1.59 nicm 2074: format_add(ft, "alternate_saved_x", "%u", wp->saved_cx);
2075: format_add(ft, "alternate_saved_y", "%u", wp->saved_cy);
1.16 nicm 2076:
2077: format_add(ft, "cursor_flag", "%d",
2078: !!(wp->base.mode & MODE_CURSOR));
2079: format_add(ft, "insert_flag", "%d",
2080: !!(wp->base.mode & MODE_INSERT));
2081: format_add(ft, "keypad_cursor_flag", "%d",
2082: !!(wp->base.mode & MODE_KCURSOR));
2083: format_add(ft, "keypad_flag", "%d",
2084: !!(wp->base.mode & MODE_KKEYPAD));
2085: format_add(ft, "wrap_flag", "%d",
2086: !!(wp->base.mode & MODE_WRAP));
2087:
1.62 nicm 2088: format_add(ft, "mouse_any_flag", "%d",
1.119 nicm 2089: !!(wp->base.mode & ALL_MOUSE_MODES));
1.16 nicm 2090: format_add(ft, "mouse_standard_flag", "%d",
2091: !!(wp->base.mode & MODE_MOUSE_STANDARD));
2092: format_add(ft, "mouse_button_flag", "%d",
2093: !!(wp->base.mode & MODE_MOUSE_BUTTON));
1.119 nicm 2094: format_add(ft, "mouse_all_flag", "%d",
2095: !!(wp->base.mode & MODE_MOUSE_ALL));
1.19 nicm 2096:
1.80 nicm 2097: format_add_cb(ft, "pane_tabs", format_cb_pane_tabs);
1.8 nicm 2098: }
2099:
1.19 nicm 2100: /* Set default format keys for paste buffer. */
1.8 nicm 2101: void
1.94 nicm 2102: format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
1.8 nicm 2103: {
1.146 nicm 2104: struct timeval tv;
2105: size_t size;
2106: char *s;
2107:
2108: timerclear(&tv);
2109: tv.tv_sec = paste_buffer_created(pb);
2110: paste_buffer_data(pb, &size);
1.8 nicm 2111:
1.146 nicm 2112: format_add(ft, "buffer_size", "%zu", size);
1.81 nicm 2113: format_add(ft, "buffer_name", "%s", paste_buffer_name(pb));
1.146 nicm 2114: format_add_tv(ft, "buffer_created", &tv);
1.8 nicm 2115:
1.94 nicm 2116: s = paste_make_sample(pb);
1.42 nicm 2117: format_add(ft, "buffer_sample", "%s", s);
2118: free(s);
1.1 nicm 2119: }