Merge branch 'obsd-master'
[tmux.git] / format.c
blob221550a1a3987294d77b0c5daf3c4cebdd17f4df
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2011 Nicholas Marriott <nicholas.marriott@gmail.com>
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.
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.
19 #include <sys/types.h>
20 #include <sys/wait.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <fnmatch.h>
25 #include <libgen.h>
26 #include <math.h>
27 #include <pwd.h>
28 #include <regex.h>
29 #include <stdarg.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <time.h>
33 #include <unistd.h>
35 #include "tmux.h"
38 * Build a list of key-value pairs and use them to expand #{key} entries in a
39 * string.
42 struct format_expand_state;
44 static char *format_job_get(struct format_expand_state *, const char *);
45 static char *format_expand1(struct format_expand_state *, const char *);
46 static int format_replace(struct format_expand_state *, const char *,
47 size_t, char **, size_t *, size_t *);
48 static void format_defaults_session(struct format_tree *,
49 struct session *);
50 static void format_defaults_client(struct format_tree *, struct client *);
51 static void format_defaults_winlink(struct format_tree *,
52 struct winlink *);
54 /* Entry in format job tree. */
55 struct format_job {
56 struct client *client;
57 u_int tag;
58 const char *cmd;
59 const char *expanded;
61 time_t last;
62 char *out;
63 int updated;
65 struct job *job;
66 int status;
68 RB_ENTRY(format_job) entry;
71 /* Format job tree. */
72 static int format_job_cmp(struct format_job *, struct format_job *);
73 static RB_HEAD(format_job_tree, format_job) format_jobs = RB_INITIALIZER();
74 RB_GENERATE_STATIC(format_job_tree, format_job, entry, format_job_cmp);
76 /* Format job tree comparison function. */
77 static int
78 format_job_cmp(struct format_job *fj1, struct format_job *fj2)
80 if (fj1->tag < fj2->tag)
81 return (-1);
82 if (fj1->tag > fj2->tag)
83 return (1);
84 return (strcmp(fj1->cmd, fj2->cmd));
87 /* Format modifiers. */
88 #define FORMAT_TIMESTRING 0x1
89 #define FORMAT_BASENAME 0x2
90 #define FORMAT_DIRNAME 0x4
91 #define FORMAT_QUOTE_SHELL 0x8
92 #define FORMAT_LITERAL 0x10
93 #define FORMAT_EXPAND 0x20
94 #define FORMAT_EXPANDTIME 0x40
95 #define FORMAT_SESSIONS 0x80
96 #define FORMAT_WINDOWS 0x100
97 #define FORMAT_PANES 0x200
98 #define FORMAT_PRETTY 0x400
99 #define FORMAT_LENGTH 0x800
100 #define FORMAT_WIDTH 0x1000
101 #define FORMAT_QUOTE_STYLE 0x2000
102 #define FORMAT_WINDOW_NAME 0x4000
103 #define FORMAT_SESSION_NAME 0x8000
104 #define FORMAT_CHARACTER 0x10000
105 #define FORMAT_COLOUR 0x20000
106 #define FORMAT_CLIENTS 0x40000
108 /* Limit on recursion. */
109 #define FORMAT_LOOP_LIMIT 100
111 /* Format expand flags. */
112 #define FORMAT_EXPAND_TIME 0x1
113 #define FORMAT_EXPAND_NOJOBS 0x2
115 /* Entry in format tree. */
116 struct format_entry {
117 char *key;
118 char *value;
119 time_t time;
120 format_cb cb;
121 RB_ENTRY(format_entry) entry;
124 /* Format type. */
125 enum format_type {
126 FORMAT_TYPE_UNKNOWN,
127 FORMAT_TYPE_SESSION,
128 FORMAT_TYPE_WINDOW,
129 FORMAT_TYPE_PANE
132 struct format_tree {
133 enum format_type type;
135 struct client *c;
136 struct session *s;
137 struct winlink *wl;
138 struct window *w;
139 struct window_pane *wp;
140 struct paste_buffer *pb;
142 struct cmdq_item *item;
143 struct client *client;
144 int flags;
145 u_int tag;
147 struct mouse_event m;
149 RB_HEAD(format_entry_tree, format_entry) tree;
151 static int format_entry_cmp(struct format_entry *, struct format_entry *);
152 RB_GENERATE_STATIC(format_entry_tree, format_entry, entry, format_entry_cmp);
154 /* Format expand state. */
155 struct format_expand_state {
156 struct format_tree *ft;
157 u_int loop;
158 time_t time;
159 struct tm tm;
160 int flags;
163 /* Format modifier. */
164 struct format_modifier {
165 char modifier[3];
166 u_int size;
168 char **argv;
169 int argc;
172 /* Format entry tree comparison function. */
173 static int
174 format_entry_cmp(struct format_entry *fe1, struct format_entry *fe2)
176 return (strcmp(fe1->key, fe2->key));
179 /* Single-character uppercase aliases. */
180 static const char *format_upper[] = {
181 NULL, /* A */
182 NULL, /* B */
183 NULL, /* C */
184 "pane_id", /* D */
185 NULL, /* E */
186 "window_flags", /* F */
187 NULL, /* G */
188 "host", /* H */
189 "window_index", /* I */
190 NULL, /* J */
191 NULL, /* K */
192 NULL, /* L */
193 NULL, /* M */
194 NULL, /* N */
195 NULL, /* O */
196 "pane_index", /* P */
197 NULL, /* Q */
198 NULL, /* R */
199 "session_name", /* S */
200 "pane_title", /* T */
201 NULL, /* U */
202 NULL, /* V */
203 "window_name", /* W */
204 NULL, /* X */
205 NULL, /* Y */
206 NULL /* Z */
209 /* Single-character lowercase aliases. */
210 static const char *format_lower[] = {
211 NULL, /* a */
212 NULL, /* b */
213 NULL, /* c */
214 NULL, /* d */
215 NULL, /* e */
216 NULL, /* f */
217 NULL, /* g */
218 "host_short", /* h */
219 NULL, /* i */
220 NULL, /* j */
221 NULL, /* k */
222 NULL, /* l */
223 NULL, /* m */
224 NULL, /* n */
225 NULL, /* o */
226 NULL, /* p */
227 NULL, /* q */
228 NULL, /* r */
229 NULL, /* s */
230 NULL, /* t */
231 NULL, /* u */
232 NULL, /* v */
233 NULL, /* w */
234 NULL, /* x */
235 NULL, /* y */
236 NULL /* z */
239 /* Is logging enabled? */
240 static inline int
241 format_logging(struct format_tree *ft)
243 return (log_get_level() != 0 || (ft->flags & FORMAT_VERBOSE));
246 /* Log a message if verbose. */
247 static void printflike(3, 4)
248 format_log1(struct format_expand_state *es, const char *from, const char *fmt,
249 ...)
251 struct format_tree *ft = es->ft;
252 va_list ap;
253 char *s;
254 static const char spaces[] = " ";
256 if (!format_logging(ft))
257 return;
259 va_start(ap, fmt);
260 xvasprintf(&s, fmt, ap);
261 va_end(ap);
263 log_debug("%s: %s", from, s);
264 if (ft->item != NULL && (ft->flags & FORMAT_VERBOSE))
265 cmdq_print(ft->item, "#%.*s%s", es->loop, spaces, s);
267 free(s);
269 #define format_log(es, fmt, ...) format_log1(es, __func__, fmt, ##__VA_ARGS__)
271 /* Copy expand state. */
272 static void
273 format_copy_state(struct format_expand_state *to,
274 struct format_expand_state *from, int flags)
276 to->ft = from->ft;
277 to->loop = from->loop;
278 to->time = from->time;
279 memcpy(&to->tm, &from->tm, sizeof to->tm);
280 to->flags = from->flags|flags;
283 /* Format job update callback. */
284 static void
285 format_job_update(struct job *job)
287 struct format_job *fj = job_get_data(job);
288 struct evbuffer *evb = job_get_event(job)->input;
289 char *line = NULL, *next;
290 time_t t;
292 while ((next = evbuffer_readline(evb)) != NULL) {
293 free(line);
294 line = next;
296 if (line == NULL)
297 return;
298 fj->updated = 1;
300 free(fj->out);
301 fj->out = line;
303 log_debug("%s: %p %s: %s", __func__, fj, fj->cmd, fj->out);
305 t = time(NULL);
306 if (fj->status && fj->last != t) {
307 if (fj->client != NULL)
308 server_status_client(fj->client);
309 fj->last = t;
313 /* Format job complete callback. */
314 static void
315 format_job_complete(struct job *job)
317 struct format_job *fj = job_get_data(job);
318 struct evbuffer *evb = job_get_event(job)->input;
319 char *line, *buf;
320 size_t len;
322 fj->job = NULL;
324 buf = NULL;
325 if ((line = evbuffer_readline(evb)) == NULL) {
326 len = EVBUFFER_LENGTH(evb);
327 buf = xmalloc(len + 1);
328 if (len != 0)
329 memcpy(buf, EVBUFFER_DATA(evb), len);
330 buf[len] = '\0';
331 } else
332 buf = line;
334 log_debug("%s: %p %s: %s", __func__, fj, fj->cmd, buf);
336 if (*buf != '\0' || !fj->updated) {
337 free(fj->out);
338 fj->out = buf;
339 } else
340 free(buf);
342 if (fj->status) {
343 if (fj->client != NULL)
344 server_status_client(fj->client);
345 fj->status = 0;
349 /* Find a job. */
350 static char *
351 format_job_get(struct format_expand_state *es, const char *cmd)
353 struct format_tree *ft = es->ft;
354 struct format_job_tree *jobs;
355 struct format_job fj0, *fj;
356 time_t t;
357 char *expanded;
358 int force;
359 struct format_expand_state next;
361 if (ft->client == NULL)
362 jobs = &format_jobs;
363 else if (ft->client->jobs != NULL)
364 jobs = ft->client->jobs;
365 else {
366 jobs = ft->client->jobs = xmalloc(sizeof *ft->client->jobs);
367 RB_INIT(jobs);
370 fj0.tag = ft->tag;
371 fj0.cmd = cmd;
372 if ((fj = RB_FIND(format_job_tree, jobs, &fj0)) == NULL) {
373 fj = xcalloc(1, sizeof *fj);
374 fj->client = ft->client;
375 fj->tag = ft->tag;
376 fj->cmd = xstrdup(cmd);
378 RB_INSERT(format_job_tree, jobs, fj);
381 format_copy_state(&next, es, FORMAT_EXPAND_NOJOBS);
382 next.flags &= ~FORMAT_EXPAND_TIME;
384 expanded = format_expand1(&next, cmd);
385 if (fj->expanded == NULL || strcmp(expanded, fj->expanded) != 0) {
386 free((void *)fj->expanded);
387 fj->expanded = xstrdup(expanded);
388 force = 1;
389 } else
390 force = (ft->flags & FORMAT_FORCE);
392 t = time(NULL);
393 if (force && fj->job != NULL)
394 job_free(fj->job);
395 if (force || (fj->job == NULL && fj->last != t)) {
396 fj->job = job_run(expanded, 0, NULL, NULL, NULL,
397 server_client_get_cwd(ft->client, NULL), format_job_update,
398 format_job_complete, NULL, fj, JOB_NOWAIT, -1, -1);
399 if (fj->job == NULL) {
400 free(fj->out);
401 xasprintf(&fj->out, "<'%s' didn't start>", fj->cmd);
403 fj->last = t;
404 fj->updated = 0;
405 } else if (fj->job != NULL && (t - fj->last) > 1 && fj->out == NULL)
406 xasprintf(&fj->out, "<'%s' not ready>", fj->cmd);
407 free(expanded);
409 if (ft->flags & FORMAT_STATUS)
410 fj->status = 1;
411 if (fj->out == NULL)
412 return (xstrdup(""));
413 return (format_expand1(&next, fj->out));
416 /* Remove old jobs. */
417 static void
418 format_job_tidy(struct format_job_tree *jobs, int force)
420 struct format_job *fj, *fj1;
421 time_t now;
423 now = time(NULL);
424 RB_FOREACH_SAFE(fj, format_job_tree, jobs, fj1) {
425 if (!force && (fj->last > now || now - fj->last < 3600))
426 continue;
427 RB_REMOVE(format_job_tree, jobs, fj);
429 log_debug("%s: %s", __func__, fj->cmd);
431 if (fj->job != NULL)
432 job_free(fj->job);
434 free((void *)fj->expanded);
435 free((void *)fj->cmd);
436 free(fj->out);
438 free(fj);
442 /* Tidy old jobs for all clients. */
443 void
444 format_tidy_jobs(void)
446 struct client *c;
448 format_job_tidy(&format_jobs, 0);
449 TAILQ_FOREACH(c, &clients, entry) {
450 if (c->jobs != NULL)
451 format_job_tidy(c->jobs, 0);
455 /* Remove old jobs for client. */
456 void
457 format_lost_client(struct client *c)
459 if (c->jobs != NULL)
460 format_job_tidy(c->jobs, 1);
461 free(c->jobs);
464 /* Wrapper for asprintf. */
465 static char * printflike(1, 2)
466 format_printf(const char *fmt, ...)
468 va_list ap;
469 char *s;
471 va_start(ap, fmt);
472 xvasprintf(&s, fmt, ap);
473 va_end(ap);
474 return (s);
477 /* Callback for host. */
478 static void *
479 format_cb_host(__unused struct format_tree *ft)
481 char host[HOST_NAME_MAX + 1];
483 if (gethostname(host, sizeof host) != 0)
484 return (xstrdup(""));
485 return (xstrdup(host));
488 /* Callback for host_short. */
489 static void *
490 format_cb_host_short(__unused struct format_tree *ft)
492 char host[HOST_NAME_MAX + 1], *cp;
494 if (gethostname(host, sizeof host) != 0)
495 return (xstrdup(""));
496 if ((cp = strchr(host, '.')) != NULL)
497 *cp = '\0';
498 return (xstrdup(host));
501 /* Callback for pid. */
502 static void *
503 format_cb_pid(__unused struct format_tree *ft)
505 char *value;
507 xasprintf(&value, "%ld", (long)getpid());
508 return (value);
511 /* Callback for session_attached_list. */
512 static void *
513 format_cb_session_attached_list(struct format_tree *ft)
515 struct session *s = ft->s;
516 struct client *loop;
517 struct evbuffer *buffer;
518 int size;
519 char *value = NULL;
521 if (s == NULL)
522 return (NULL);
524 buffer = evbuffer_new();
525 if (buffer == NULL)
526 fatalx("out of memory");
528 TAILQ_FOREACH(loop, &clients, entry) {
529 if (loop->session == s) {
530 if (EVBUFFER_LENGTH(buffer) > 0)
531 evbuffer_add(buffer, ",", 1);
532 evbuffer_add_printf(buffer, "%s", loop->name);
536 if ((size = EVBUFFER_LENGTH(buffer)) != 0)
537 xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
538 evbuffer_free(buffer);
539 return (value);
542 /* Callback for session_alerts. */
543 static void *
544 format_cb_session_alerts(struct format_tree *ft)
546 struct session *s = ft->s;
547 struct winlink *wl;
548 char alerts[1024], tmp[16];
550 if (s == NULL)
551 return (NULL);
553 *alerts = '\0';
554 RB_FOREACH(wl, winlinks, &s->windows) {
555 if ((wl->flags & WINLINK_ALERTFLAGS) == 0)
556 continue;
557 xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
559 if (*alerts != '\0')
560 strlcat(alerts, ",", sizeof alerts);
561 strlcat(alerts, tmp, sizeof alerts);
562 if (wl->flags & WINLINK_ACTIVITY)
563 strlcat(alerts, "#", sizeof alerts);
564 if (wl->flags & WINLINK_BELL)
565 strlcat(alerts, "!", sizeof alerts);
566 if (wl->flags & WINLINK_SILENCE)
567 strlcat(alerts, "~", sizeof alerts);
569 return (xstrdup(alerts));
572 /* Callback for session_stack. */
573 static void *
574 format_cb_session_stack(struct format_tree *ft)
576 struct session *s = ft->s;
577 struct winlink *wl;
578 char result[1024], tmp[16];
580 if (s == NULL)
581 return (NULL);
583 xsnprintf(result, sizeof result, "%u", s->curw->idx);
584 TAILQ_FOREACH(wl, &s->lastw, sentry) {
585 xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
587 if (*result != '\0')
588 strlcat(result, ",", sizeof result);
589 strlcat(result, tmp, sizeof result);
591 return (xstrdup(result));
594 /* Callback for window_stack_index. */
595 static void *
596 format_cb_window_stack_index(struct format_tree *ft)
598 struct session *s;
599 struct winlink *wl;
600 u_int idx;
601 char *value = NULL;
603 if (ft->wl == NULL)
604 return (NULL);
605 s = ft->wl->session;
607 idx = 0;
608 TAILQ_FOREACH(wl, &s->lastw, sentry) {
609 idx++;
610 if (wl == ft->wl)
611 break;
613 if (wl == NULL)
614 return (xstrdup("0"));
615 xasprintf(&value, "%u", idx);
616 return (value);
619 /* Callback for window_linked_sessions_list. */
620 static void *
621 format_cb_window_linked_sessions_list(struct format_tree *ft)
623 struct window *w;
624 struct winlink *wl;
625 struct evbuffer *buffer;
626 int size;
627 char *value = NULL;
629 if (ft->wl == NULL)
630 return (NULL);
631 w = ft->wl->window;
633 buffer = evbuffer_new();
634 if (buffer == NULL)
635 fatalx("out of memory");
637 TAILQ_FOREACH(wl, &w->winlinks, wentry) {
638 if (EVBUFFER_LENGTH(buffer) > 0)
639 evbuffer_add(buffer, ",", 1);
640 evbuffer_add_printf(buffer, "%s", wl->session->name);
643 if ((size = EVBUFFER_LENGTH(buffer)) != 0)
644 xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
645 evbuffer_free(buffer);
646 return (value);
649 /* Callback for window_active_sessions. */
650 static void *
651 format_cb_window_active_sessions(struct format_tree *ft)
653 struct window *w;
654 struct winlink *wl;
655 u_int n = 0;
656 char *value;
658 if (ft->wl == NULL)
659 return (NULL);
660 w = ft->wl->window;
662 TAILQ_FOREACH(wl, &w->winlinks, wentry) {
663 if (wl->session->curw == wl)
664 n++;
667 xasprintf(&value, "%u", n);
668 return (value);
671 /* Callback for window_active_sessions_list. */
672 static void *
673 format_cb_window_active_sessions_list(struct format_tree *ft)
675 struct window *w;
676 struct winlink *wl;
677 struct evbuffer *buffer;
678 int size;
679 char *value = NULL;
681 if (ft->wl == NULL)
682 return (NULL);
683 w = ft->wl->window;
685 buffer = evbuffer_new();
686 if (buffer == NULL)
687 fatalx("out of memory");
689 TAILQ_FOREACH(wl, &w->winlinks, wentry) {
690 if (wl->session->curw == wl) {
691 if (EVBUFFER_LENGTH(buffer) > 0)
692 evbuffer_add(buffer, ",", 1);
693 evbuffer_add_printf(buffer, "%s", wl->session->name);
697 if ((size = EVBUFFER_LENGTH(buffer)) != 0)
698 xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
699 evbuffer_free(buffer);
700 return (value);
703 /* Callback for window_active_clients. */
704 static void *
705 format_cb_window_active_clients(struct format_tree *ft)
707 struct window *w;
708 struct client *loop;
709 struct session *client_session;
710 u_int n = 0;
711 char *value;
713 if (ft->wl == NULL)
714 return (NULL);
715 w = ft->wl->window;
717 TAILQ_FOREACH(loop, &clients, entry) {
718 client_session = loop->session;
719 if (client_session == NULL)
720 continue;
722 if (w == client_session->curw->window)
723 n++;
726 xasprintf(&value, "%u", n);
727 return (value);
730 /* Callback for window_active_clients_list. */
731 static void *
732 format_cb_window_active_clients_list(struct format_tree *ft)
734 struct window *w;
735 struct client *loop;
736 struct session *client_session;
737 struct evbuffer *buffer;
738 int size;
739 char *value = NULL;
741 if (ft->wl == NULL)
742 return (NULL);
743 w = ft->wl->window;
745 buffer = evbuffer_new();
746 if (buffer == NULL)
747 fatalx("out of memory");
749 TAILQ_FOREACH(loop, &clients, entry) {
750 client_session = loop->session;
751 if (client_session == NULL)
752 continue;
754 if (w == client_session->curw->window) {
755 if (EVBUFFER_LENGTH(buffer) > 0)
756 evbuffer_add(buffer, ",", 1);
757 evbuffer_add_printf(buffer, "%s", loop->name);
761 if ((size = EVBUFFER_LENGTH(buffer)) != 0)
762 xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
763 evbuffer_free(buffer);
764 return (value);
767 /* Callback for window_layout. */
768 static void *
769 format_cb_window_layout(struct format_tree *ft)
771 struct window *w = ft->w;
773 if (w == NULL)
774 return (NULL);
776 if (w->saved_layout_root != NULL)
777 return (layout_dump(w->saved_layout_root));
778 return (layout_dump(w->layout_root));
781 /* Callback for window_visible_layout. */
782 static void *
783 format_cb_window_visible_layout(struct format_tree *ft)
785 struct window *w = ft->w;
787 if (w == NULL)
788 return (NULL);
790 return (layout_dump(w->layout_root));
793 /* Callback for pane_start_command. */
794 static void *
795 format_cb_start_command(struct format_tree *ft)
797 struct window_pane *wp = ft->wp;
799 if (wp == NULL)
800 return (NULL);
802 return (cmd_stringify_argv(wp->argc, wp->argv));
805 /* Callback for pane_start_path. */
806 static void *
807 format_cb_start_path(struct format_tree *ft)
809 struct window_pane *wp = ft->wp;
811 if (wp == NULL)
812 return (NULL);
814 if (wp->cwd == NULL)
815 return (xstrdup(""));
816 return (xstrdup(wp->cwd));
819 /* Callback for pane_current_command. */
820 static void *
821 format_cb_current_command(struct format_tree *ft)
823 struct window_pane *wp = ft->wp;
824 char *cmd, *value;
826 if (wp == NULL || wp->shell == NULL)
827 return (NULL);
829 cmd = osdep_get_name(wp->fd, wp->tty);
830 if (cmd == NULL || *cmd == '\0') {
831 free(cmd);
832 cmd = cmd_stringify_argv(wp->argc, wp->argv);
833 if (cmd == NULL || *cmd == '\0') {
834 free(cmd);
835 cmd = xstrdup(wp->shell);
838 value = parse_window_name(cmd);
839 free(cmd);
840 return (value);
843 /* Callback for pane_current_path. */
844 static void *
845 format_cb_current_path(struct format_tree *ft)
847 struct window_pane *wp = ft->wp;
848 char *cwd;
850 if (wp == NULL)
851 return (NULL);
853 cwd = osdep_get_cwd(wp->fd);
854 if (cwd == NULL)
855 return (NULL);
856 return (xstrdup(cwd));
859 /* Callback for history_bytes. */
860 static void *
861 format_cb_history_bytes(struct format_tree *ft)
863 struct window_pane *wp = ft->wp;
864 struct grid *gd;
865 struct grid_line *gl;
866 size_t size = 0;
867 u_int i;
868 char *value;
870 if (wp == NULL)
871 return (NULL);
872 gd = wp->base.grid;
874 for (i = 0; i < gd->hsize + gd->sy; i++) {
875 gl = grid_get_line(gd, i);
876 size += gl->cellsize * sizeof *gl->celldata;
877 size += gl->extdsize * sizeof *gl->extddata;
879 size += (gd->hsize + gd->sy) * sizeof *gl;
881 xasprintf(&value, "%zu", size);
882 return (value);
885 /* Callback for history_all_bytes. */
886 static void *
887 format_cb_history_all_bytes(struct format_tree *ft)
889 struct window_pane *wp = ft->wp;
890 struct grid *gd;
891 struct grid_line *gl;
892 u_int i, lines, cells = 0, extended_cells = 0;
893 char *value;
895 if (wp == NULL)
896 return (NULL);
897 gd = wp->base.grid;
899 lines = gd->hsize + gd->sy;
900 for (i = 0; i < lines; i++) {
901 gl = grid_get_line(gd, i);
902 cells += gl->cellsize;
903 extended_cells += gl->extdsize;
906 xasprintf(&value, "%u,%zu,%u,%zu,%u,%zu", lines,
907 lines * sizeof *gl, cells, cells * sizeof *gl->celldata,
908 extended_cells, extended_cells * sizeof *gl->extddata);
909 return (value);
912 /* Callback for pane_tabs. */
913 static void *
914 format_cb_pane_tabs(struct format_tree *ft)
916 struct window_pane *wp = ft->wp;
917 struct evbuffer *buffer;
918 u_int i;
919 int size;
920 char *value = NULL;
922 if (wp == NULL)
923 return (NULL);
925 buffer = evbuffer_new();
926 if (buffer == NULL)
927 fatalx("out of memory");
928 for (i = 0; i < wp->base.grid->sx; i++) {
929 if (!bit_test(wp->base.tabs, i))
930 continue;
932 if (EVBUFFER_LENGTH(buffer) > 0)
933 evbuffer_add(buffer, ",", 1);
934 evbuffer_add_printf(buffer, "%u", i);
936 if ((size = EVBUFFER_LENGTH(buffer)) != 0)
937 xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
938 evbuffer_free(buffer);
939 return (value);
942 /* Callback for pane_fg. */
943 static void *
944 format_cb_pane_fg(struct format_tree *ft)
946 struct window_pane *wp = ft->wp;
947 struct grid_cell gc;
949 if (wp == NULL)
950 return (NULL);
952 tty_default_colours(&gc, wp);
953 return (xstrdup(colour_tostring(gc.fg)));
956 /* Callback for pane_bg. */
957 static void *
958 format_cb_pane_bg(struct format_tree *ft)
960 struct window_pane *wp = ft->wp;
961 struct grid_cell gc;
963 if (wp == NULL)
964 return (NULL);
966 tty_default_colours(&gc, wp);
967 return (xstrdup(colour_tostring(gc.bg)));
970 /* Callback for session_group_list. */
971 static void *
972 format_cb_session_group_list(struct format_tree *ft)
974 struct session *s = ft->s;
975 struct session_group *sg;
976 struct session *loop;
977 struct evbuffer *buffer;
978 int size;
979 char *value = NULL;
981 if (s == NULL)
982 return (NULL);
983 sg = session_group_contains(s);
984 if (sg == NULL)
985 return (NULL);
987 buffer = evbuffer_new();
988 if (buffer == NULL)
989 fatalx("out of memory");
991 TAILQ_FOREACH(loop, &sg->sessions, gentry) {
992 if (EVBUFFER_LENGTH(buffer) > 0)
993 evbuffer_add(buffer, ",", 1);
994 evbuffer_add_printf(buffer, "%s", loop->name);
997 if ((size = EVBUFFER_LENGTH(buffer)) != 0)
998 xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
999 evbuffer_free(buffer);
1000 return (value);
1003 /* Callback for session_group_attached_list. */
1004 static void *
1005 format_cb_session_group_attached_list(struct format_tree *ft)
1007 struct session *s = ft->s, *client_session, *session_loop;
1008 struct session_group *sg;
1009 struct client *loop;
1010 struct evbuffer *buffer;
1011 int size;
1012 char *value = NULL;
1014 if (s == NULL)
1015 return (NULL);
1016 sg = session_group_contains(s);
1017 if (sg == NULL)
1018 return (NULL);
1020 buffer = evbuffer_new();
1021 if (buffer == NULL)
1022 fatalx("out of memory");
1024 TAILQ_FOREACH(loop, &clients, entry) {
1025 client_session = loop->session;
1026 if (client_session == NULL)
1027 continue;
1028 TAILQ_FOREACH(session_loop, &sg->sessions, gentry) {
1029 if (session_loop == client_session){
1030 if (EVBUFFER_LENGTH(buffer) > 0)
1031 evbuffer_add(buffer, ",", 1);
1032 evbuffer_add_printf(buffer, "%s", loop->name);
1037 if ((size = EVBUFFER_LENGTH(buffer)) != 0)
1038 xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
1039 evbuffer_free(buffer);
1040 return (value);
1043 /* Callback for pane_in_mode. */
1044 static void *
1045 format_cb_pane_in_mode(struct format_tree *ft)
1047 struct window_pane *wp = ft->wp;
1048 u_int n = 0;
1049 struct window_mode_entry *wme;
1050 char *value;
1052 if (wp == NULL)
1053 return (NULL);
1055 TAILQ_FOREACH(wme, &wp->modes, entry)
1056 n++;
1057 xasprintf(&value, "%u", n);
1058 return (value);
1061 /* Callback for pane_at_top. */
1062 static void *
1063 format_cb_pane_at_top(struct format_tree *ft)
1065 struct window_pane *wp = ft->wp;
1066 struct window *w;
1067 int status, flag;
1068 char *value;
1070 if (wp == NULL)
1071 return (NULL);
1072 w = wp->window;
1074 status = options_get_number(w->options, "pane-border-status");
1075 if (status == PANE_STATUS_TOP)
1076 flag = (wp->yoff == 1);
1077 else
1078 flag = (wp->yoff == 0);
1079 xasprintf(&value, "%d", flag);
1080 return (value);
1083 /* Callback for pane_at_bottom. */
1084 static void *
1085 format_cb_pane_at_bottom(struct format_tree *ft)
1087 struct window_pane *wp = ft->wp;
1088 struct window *w;
1089 int status, flag;
1090 char *value;
1092 if (wp == NULL)
1093 return (NULL);
1094 w = wp->window;
1096 status = options_get_number(w->options, "pane-border-status");
1097 if (status == PANE_STATUS_BOTTOM)
1098 flag = (wp->yoff + wp->sy == w->sy - 1);
1099 else
1100 flag = (wp->yoff + wp->sy == w->sy);
1101 xasprintf(&value, "%d", flag);
1102 return (value);
1105 /* Callback for cursor_character. */
1106 static void *
1107 format_cb_cursor_character(struct format_tree *ft)
1109 struct window_pane *wp = ft->wp;
1110 struct grid_cell gc;
1111 char *value = NULL;
1113 if (wp == NULL)
1114 return (NULL);
1116 grid_view_get_cell(wp->base.grid, wp->base.cx, wp->base.cy, &gc);
1117 if (~gc.flags & GRID_FLAG_PADDING)
1118 xasprintf(&value, "%.*s", (int)gc.data.size, gc.data.data);
1119 return (value);
1122 /* Callback for mouse_word. */
1123 static void *
1124 format_cb_mouse_word(struct format_tree *ft)
1126 struct window_pane *wp;
1127 struct grid *gd;
1128 u_int x, y;
1130 if (!ft->m.valid)
1131 return (NULL);
1132 wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1133 if (wp == NULL)
1134 return (NULL);
1135 if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0)
1136 return (NULL);
1138 if (!TAILQ_EMPTY(&wp->modes)) {
1139 if (window_pane_mode(wp) != WINDOW_PANE_NO_MODE)
1140 return (window_copy_get_word(wp, x, y));
1141 return (NULL);
1143 gd = wp->base.grid;
1144 return (format_grid_word(gd, x, gd->hsize + y));
1147 /* Callback for mouse_hyperlink. */
1148 static void *
1149 format_cb_mouse_hyperlink(struct format_tree *ft)
1151 struct window_pane *wp;
1152 struct grid *gd;
1153 u_int x, y;
1155 if (!ft->m.valid)
1156 return (NULL);
1157 wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1158 if (wp == NULL)
1159 return (NULL);
1160 if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0)
1161 return (NULL);
1162 gd = wp->base.grid;
1163 return (format_grid_hyperlink(gd, x, gd->hsize + y, wp->screen));
1166 /* Callback for mouse_line. */
1167 static void *
1168 format_cb_mouse_line(struct format_tree *ft)
1170 struct window_pane *wp;
1171 struct grid *gd;
1172 u_int x, y;
1174 if (!ft->m.valid)
1175 return (NULL);
1176 wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1177 if (wp == NULL)
1178 return (NULL);
1179 if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0)
1180 return (NULL);
1182 if (!TAILQ_EMPTY(&wp->modes)) {
1183 if (window_pane_mode(wp) != WINDOW_PANE_NO_MODE)
1184 return (window_copy_get_line(wp, y));
1185 return (NULL);
1187 gd = wp->base.grid;
1188 return (format_grid_line(gd, gd->hsize + y));
1191 /* Callback for mouse_status_line. */
1192 static void *
1193 format_cb_mouse_status_line(struct format_tree *ft)
1195 char *value;
1196 u_int y;
1198 if (!ft->m.valid)
1199 return (NULL);
1200 if (ft->c == NULL || (~ft->c->tty.flags & TTY_STARTED))
1201 return (NULL);
1203 if (ft->m.statusat == 0 && ft->m.y < ft->m.statuslines) {
1204 y = ft->m.y;
1205 } else if (ft->m.statusat > 0 && ft->m.y >= (u_int)ft->m.statusat) {
1206 y = ft->m.y - ft->m.statusat;
1207 } else
1208 return (NULL);
1209 xasprintf(&value, "%u", y);
1210 return (value);
1214 /* Callback for mouse_status_range. */
1215 static void *
1216 format_cb_mouse_status_range(struct format_tree *ft)
1218 struct style_range *sr;
1219 u_int x, y;
1221 if (!ft->m.valid)
1222 return (NULL);
1223 if (ft->c == NULL || (~ft->c->tty.flags & TTY_STARTED))
1224 return (NULL);
1226 if (ft->m.statusat == 0 && ft->m.y < ft->m.statuslines) {
1227 x = ft->m.x;
1228 y = ft->m.y;
1229 } else if (ft->m.statusat > 0 && ft->m.y >= (u_int)ft->m.statusat) {
1230 x = ft->m.x;
1231 y = ft->m.y - ft->m.statusat;
1232 } else
1233 return (NULL);
1235 sr = status_get_range(ft->c, x, y);
1236 if (sr == NULL)
1237 return (NULL);
1238 switch (sr->type) {
1239 case STYLE_RANGE_NONE:
1240 return (NULL);
1241 case STYLE_RANGE_LEFT:
1242 return (xstrdup("left"));
1243 case STYLE_RANGE_RIGHT:
1244 return (xstrdup("right"));
1245 case STYLE_RANGE_PANE:
1246 return (xstrdup("pane"));
1247 case STYLE_RANGE_WINDOW:
1248 return (xstrdup("window"));
1249 case STYLE_RANGE_SESSION:
1250 return (xstrdup("session"));
1251 case STYLE_RANGE_USER:
1252 return (xstrdup(sr->string));
1254 return (NULL);
1257 /* Callback for alternate_on. */
1258 static void *
1259 format_cb_alternate_on(struct format_tree *ft)
1261 if (ft->wp != NULL) {
1262 if (ft->wp->base.saved_grid != NULL)
1263 return (xstrdup("1"));
1264 return (xstrdup("0"));
1266 return (NULL);
1269 /* Callback for alternate_saved_x. */
1270 static void *
1271 format_cb_alternate_saved_x(struct format_tree *ft)
1273 if (ft->wp != NULL)
1274 return (format_printf("%u", ft->wp->base.saved_cx));
1275 return (NULL);
1278 /* Callback for alternate_saved_y. */
1279 static void *
1280 format_cb_alternate_saved_y(struct format_tree *ft)
1282 if (ft->wp != NULL)
1283 return (format_printf("%u", ft->wp->base.saved_cy));
1284 return (NULL);
1287 /* Callback for buffer_name. */
1288 static void *
1289 format_cb_buffer_name(struct format_tree *ft)
1291 if (ft->pb != NULL)
1292 return (xstrdup(paste_buffer_name(ft->pb)));
1293 return (NULL);
1296 /* Callback for buffer_sample. */
1297 static void *
1298 format_cb_buffer_sample(struct format_tree *ft)
1300 if (ft->pb != NULL)
1301 return (paste_make_sample(ft->pb));
1302 return (NULL);
1305 /* Callback for buffer_size. */
1306 static void *
1307 format_cb_buffer_size(struct format_tree *ft)
1309 size_t size;
1311 if (ft->pb != NULL) {
1312 paste_buffer_data(ft->pb, &size);
1313 return (format_printf("%zu", size));
1315 return (NULL);
1318 /* Callback for client_cell_height. */
1319 static void *
1320 format_cb_client_cell_height(struct format_tree *ft)
1322 if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED))
1323 return (format_printf("%u", ft->c->tty.ypixel));
1324 return (NULL);
1327 /* Callback for client_cell_width. */
1328 static void *
1329 format_cb_client_cell_width(struct format_tree *ft)
1331 if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED))
1332 return (format_printf("%u", ft->c->tty.xpixel));
1333 return (NULL);
1336 /* Callback for client_control_mode. */
1337 static void *
1338 format_cb_client_control_mode(struct format_tree *ft)
1340 if (ft->c != NULL) {
1341 if (ft->c->flags & CLIENT_CONTROL)
1342 return (xstrdup("1"));
1343 return (xstrdup("0"));
1345 return (NULL);
1348 /* Callback for client_discarded. */
1349 static void *
1350 format_cb_client_discarded(struct format_tree *ft)
1352 if (ft->c != NULL)
1353 return (format_printf("%zu", ft->c->discarded));
1354 return (NULL);
1357 /* Callback for client_flags. */
1358 static void *
1359 format_cb_client_flags(struct format_tree *ft)
1361 if (ft->c != NULL)
1362 return (xstrdup(server_client_get_flags(ft->c)));
1363 return (NULL);
1366 /* Callback for client_height. */
1367 static void *
1368 format_cb_client_height(struct format_tree *ft)
1370 if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED))
1371 return (format_printf("%u", ft->c->tty.sy));
1372 return (NULL);
1375 /* Callback for client_key_table. */
1376 static void *
1377 format_cb_client_key_table(struct format_tree *ft)
1379 if (ft->c != NULL)
1380 return (xstrdup(ft->c->keytable->name));
1381 return (NULL);
1384 /* Callback for client_last_session. */
1385 static void *
1386 format_cb_client_last_session(struct format_tree *ft)
1388 if (ft->c != NULL &&
1389 ft->c->last_session != NULL &&
1390 session_alive(ft->c->last_session))
1391 return (xstrdup(ft->c->last_session->name));
1392 return (NULL);
1395 /* Callback for client_name. */
1396 static void *
1397 format_cb_client_name(struct format_tree *ft)
1399 if (ft->c != NULL)
1400 return (xstrdup(ft->c->name));
1401 return (NULL);
1404 /* Callback for client_pid. */
1405 static void *
1406 format_cb_client_pid(struct format_tree *ft)
1408 if (ft->c != NULL)
1409 return (format_printf("%ld", (long)ft->c->pid));
1410 return (NULL);
1413 /* Callback for client_prefix. */
1414 static void *
1415 format_cb_client_prefix(struct format_tree *ft)
1417 const char *name;
1419 if (ft->c != NULL) {
1420 name = server_client_get_key_table(ft->c);
1421 if (strcmp(ft->c->keytable->name, name) == 0)
1422 return (xstrdup("0"));
1423 return (xstrdup("1"));
1425 return (NULL);
1428 /* Callback for client_readonly. */
1429 static void *
1430 format_cb_client_readonly(struct format_tree *ft)
1432 if (ft->c != NULL) {
1433 if (ft->c->flags & CLIENT_READONLY)
1434 return (xstrdup("1"));
1435 return (xstrdup("0"));
1437 return (NULL);
1440 /* Callback for client_session. */
1441 static void *
1442 format_cb_client_session(struct format_tree *ft)
1444 if (ft->c != NULL && ft->c->session != NULL)
1445 return (xstrdup(ft->c->session->name));
1446 return (NULL);
1449 /* Callback for client_termfeatures. */
1450 static void *
1451 format_cb_client_termfeatures(struct format_tree *ft)
1453 if (ft->c != NULL)
1454 return (xstrdup(tty_get_features(ft->c->term_features)));
1455 return (NULL);
1458 /* Callback for client_termname. */
1459 static void *
1460 format_cb_client_termname(struct format_tree *ft)
1462 if (ft->c != NULL)
1463 return (xstrdup(ft->c->term_name));
1464 return (NULL);
1467 /* Callback for client_termtype. */
1468 static void *
1469 format_cb_client_termtype(struct format_tree *ft)
1471 if (ft->c != NULL) {
1472 if (ft->c->term_type == NULL)
1473 return (xstrdup(""));
1474 return (xstrdup(ft->c->term_type));
1476 return (NULL);
1479 /* Callback for client_tty. */
1480 static void *
1481 format_cb_client_tty(struct format_tree *ft)
1483 if (ft->c != NULL)
1484 return (xstrdup(ft->c->ttyname));
1485 return (NULL);
1488 /* Callback for client_uid. */
1489 static void *
1490 format_cb_client_uid(struct format_tree *ft)
1492 uid_t uid;
1494 if (ft->c != NULL) {
1495 uid = proc_get_peer_uid(ft->c->peer);
1496 if (uid != (uid_t)-1)
1497 return (format_printf("%ld", (long)uid));
1499 return (NULL);
1502 /* Callback for client_user. */
1503 static void *
1504 format_cb_client_user(struct format_tree *ft)
1506 uid_t uid;
1507 struct passwd *pw;
1509 if (ft->c != NULL) {
1510 uid = proc_get_peer_uid(ft->c->peer);
1511 if (uid != (uid_t)-1 && (pw = getpwuid(uid)) != NULL)
1512 return (xstrdup(pw->pw_name));
1514 return (NULL);
1517 /* Callback for client_utf8. */
1518 static void *
1519 format_cb_client_utf8(struct format_tree *ft)
1521 if (ft->c != NULL) {
1522 if (ft->c->flags & CLIENT_UTF8)
1523 return (xstrdup("1"));
1524 return (xstrdup("0"));
1526 return (NULL);
1529 /* Callback for client_width. */
1530 static void *
1531 format_cb_client_width(struct format_tree *ft)
1533 if (ft->c != NULL)
1534 return (format_printf("%u", ft->c->tty.sx));
1535 return (NULL);
1538 /* Callback for client_written. */
1539 static void *
1540 format_cb_client_written(struct format_tree *ft)
1542 if (ft->c != NULL)
1543 return (format_printf("%zu", ft->c->written));
1544 return (NULL);
1547 /* Callback for config_files. */
1548 static void *
1549 format_cb_config_files(__unused struct format_tree *ft)
1551 char *s = NULL;
1552 size_t slen = 0;
1553 u_int i;
1554 size_t n;
1556 for (i = 0; i < cfg_nfiles; i++) {
1557 n = strlen(cfg_files[i]) + 1;
1558 s = xrealloc(s, slen + n + 1);
1559 slen += xsnprintf(s + slen, n + 1, "%s,", cfg_files[i]);
1561 if (s == NULL)
1562 return (xstrdup(""));
1563 s[slen - 1] = '\0';
1564 return (s);
1567 /* Callback for cursor_flag. */
1568 static void *
1569 format_cb_cursor_flag(struct format_tree *ft)
1571 if (ft->wp != NULL) {
1572 if (ft->wp->base.mode & MODE_CURSOR)
1573 return (xstrdup("1"));
1574 return (xstrdup("0"));
1576 return (NULL);
1579 /* Callback for cursor_x. */
1580 static void *
1581 format_cb_cursor_x(struct format_tree *ft)
1583 if (ft->wp != NULL)
1584 return (format_printf("%u", ft->wp->base.cx));
1585 return (NULL);
1588 /* Callback for cursor_y. */
1589 static void *
1590 format_cb_cursor_y(struct format_tree *ft)
1592 if (ft->wp != NULL)
1593 return (format_printf("%u", ft->wp->base.cy));
1594 return (NULL);
1597 /* Callback for history_limit. */
1598 static void *
1599 format_cb_history_limit(struct format_tree *ft)
1601 if (ft->wp != NULL)
1602 return (format_printf("%u", ft->wp->base.grid->hlimit));
1603 return (NULL);
1606 /* Callback for history_size. */
1607 static void *
1608 format_cb_history_size(struct format_tree *ft)
1610 if (ft->wp != NULL)
1611 return (format_printf("%u", ft->wp->base.grid->hsize));
1612 return (NULL);
1615 /* Callback for insert_flag. */
1616 static void *
1617 format_cb_insert_flag(struct format_tree *ft)
1619 if (ft->wp != NULL) {
1620 if (ft->wp->base.mode & MODE_INSERT)
1621 return (xstrdup("1"));
1622 return (xstrdup("0"));
1624 return (NULL);
1627 /* Callback for keypad_cursor_flag. */
1628 static void *
1629 format_cb_keypad_cursor_flag(struct format_tree *ft)
1631 if (ft->wp != NULL) {
1632 if (ft->wp->base.mode & MODE_KCURSOR)
1633 return (xstrdup("1"));
1634 return (xstrdup("0"));
1636 return (NULL);
1639 /* Callback for keypad_flag. */
1640 static void *
1641 format_cb_keypad_flag(struct format_tree *ft)
1643 if (ft->wp != NULL) {
1644 if (ft->wp->base.mode & MODE_KKEYPAD)
1645 return (xstrdup("1"));
1646 return (xstrdup("0"));
1648 return (NULL);
1651 /* Callback for mouse_all_flag. */
1652 static void *
1653 format_cb_mouse_all_flag(struct format_tree *ft)
1655 if (ft->wp != NULL) {
1656 if (ft->wp->base.mode & MODE_MOUSE_ALL)
1657 return (xstrdup("1"));
1658 return (xstrdup("0"));
1660 return (NULL);
1663 /* Callback for mouse_any_flag. */
1664 static void *
1665 format_cb_mouse_any_flag(struct format_tree *ft)
1667 if (ft->wp != NULL) {
1668 if (ft->wp->base.mode & ALL_MOUSE_MODES)
1669 return (xstrdup("1"));
1670 return (xstrdup("0"));
1672 return (NULL);
1675 /* Callback for mouse_button_flag. */
1676 static void *
1677 format_cb_mouse_button_flag(struct format_tree *ft)
1679 if (ft->wp != NULL) {
1680 if (ft->wp->base.mode & MODE_MOUSE_BUTTON)
1681 return (xstrdup("1"));
1682 return (xstrdup("0"));
1684 return (NULL);
1687 /* Callback for mouse_pane. */
1688 static void *
1689 format_cb_mouse_pane(struct format_tree *ft)
1691 struct window_pane *wp;
1693 if (ft->m.valid) {
1694 wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1695 if (wp != NULL)
1696 return (format_printf("%%%u", wp->id));
1697 return (NULL);
1699 return (NULL);
1702 /* Callback for mouse_sgr_flag. */
1703 static void *
1704 format_cb_mouse_sgr_flag(struct format_tree *ft)
1706 if (ft->wp != NULL) {
1707 if (ft->wp->base.mode & MODE_MOUSE_SGR)
1708 return (xstrdup("1"));
1709 return (xstrdup("0"));
1711 return (NULL);
1714 /* Callback for mouse_standard_flag. */
1715 static void *
1716 format_cb_mouse_standard_flag(struct format_tree *ft)
1718 if (ft->wp != NULL) {
1719 if (ft->wp->base.mode & MODE_MOUSE_STANDARD)
1720 return (xstrdup("1"));
1721 return (xstrdup("0"));
1723 return (NULL);
1726 /* Callback for mouse_utf8_flag. */
1727 static void *
1728 format_cb_mouse_utf8_flag(struct format_tree *ft)
1730 if (ft->wp != NULL) {
1731 if (ft->wp->base.mode & MODE_MOUSE_UTF8)
1732 return (xstrdup("1"));
1733 return (xstrdup("0"));
1735 return (NULL);
1738 /* Callback for mouse_x. */
1739 static void *
1740 format_cb_mouse_x(struct format_tree *ft)
1742 struct window_pane *wp;
1743 u_int x, y;
1745 if (!ft->m.valid)
1746 return (NULL);
1747 wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1748 if (wp != NULL && cmd_mouse_at(wp, &ft->m, &x, &y, 0) == 0)
1749 return (format_printf("%u", x));
1750 if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED)) {
1751 if (ft->m.statusat == 0 && ft->m.y < ft->m.statuslines)
1752 return (format_printf("%u", ft->m.x));
1753 if (ft->m.statusat > 0 && ft->m.y >= (u_int)ft->m.statusat)
1754 return (format_printf("%u", ft->m.x));
1756 return (NULL);
1759 /* Callback for mouse_y. */
1760 static void *
1761 format_cb_mouse_y(struct format_tree *ft)
1763 struct window_pane *wp;
1764 u_int x, y;
1766 if (!ft->m.valid)
1767 return (NULL);
1768 wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1769 if (wp != NULL && cmd_mouse_at(wp, &ft->m, &x, &y, 0) == 0)
1770 return (format_printf("%u", y));
1771 if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED)) {
1772 if (ft->m.statusat == 0 && ft->m.y < ft->m.statuslines)
1773 return (format_printf("%u", ft->m.y));
1774 if (ft->m.statusat > 0 && ft->m.y >= (u_int)ft->m.statusat)
1775 return (format_printf("%u", ft->m.y - ft->m.statusat));
1777 return (NULL);
1780 /* Callback for next_session_id. */
1781 static void *
1782 format_cb_next_session_id(__unused struct format_tree *ft)
1784 return (format_printf("$%u", next_session_id));
1787 /* Callback for origin_flag. */
1788 static void *
1789 format_cb_origin_flag(struct format_tree *ft)
1791 if (ft->wp != NULL) {
1792 if (ft->wp->base.mode & MODE_ORIGIN)
1793 return (xstrdup("1"));
1794 return (xstrdup("0"));
1796 return (NULL);
1799 /* Callback for pane_active. */
1800 static void *
1801 format_cb_pane_active(struct format_tree *ft)
1803 if (ft->wp != NULL) {
1804 if (ft->wp == ft->wp->window->active)
1805 return (xstrdup("1"));
1806 return (xstrdup("0"));
1808 return (NULL);
1811 /* Callback for pane_at_left. */
1812 static void *
1813 format_cb_pane_at_left(struct format_tree *ft)
1815 if (ft->wp != NULL) {
1816 if (ft->wp->xoff == 0)
1817 return (xstrdup("1"));
1818 return (xstrdup("0"));
1820 return (NULL);
1823 /* Callback for pane_at_right. */
1824 static void *
1825 format_cb_pane_at_right(struct format_tree *ft)
1827 if (ft->wp != NULL) {
1828 if (ft->wp->xoff + ft->wp->sx == ft->wp->window->sx)
1829 return (xstrdup("1"));
1830 return (xstrdup("0"));
1832 return (NULL);
1835 /* Callback for pane_bottom. */
1836 static void *
1837 format_cb_pane_bottom(struct format_tree *ft)
1839 if (ft->wp != NULL)
1840 return (format_printf("%u", ft->wp->yoff + ft->wp->sy - 1));
1841 return (NULL);
1844 /* Callback for pane_dead. */
1845 static void *
1846 format_cb_pane_dead(struct format_tree *ft)
1848 if (ft->wp != NULL) {
1849 if (ft->wp->fd == -1)
1850 return (xstrdup("1"));
1851 return (xstrdup("0"));
1853 return (NULL);
1856 /* Callback for pane_dead_signal. */
1857 static void *
1858 format_cb_pane_dead_signal(struct format_tree *ft)
1860 struct window_pane *wp = ft->wp;
1861 const char *name;
1863 if (wp != NULL) {
1864 if ((wp->flags & PANE_STATUSREADY) && WIFSIGNALED(wp->status)) {
1865 name = sig2name(WTERMSIG(wp->status));
1866 return (format_printf("%s", name));
1868 return (NULL);
1870 return (NULL);
1873 /* Callback for pane_dead_status. */
1874 static void *
1875 format_cb_pane_dead_status(struct format_tree *ft)
1877 struct window_pane *wp = ft->wp;
1879 if (wp != NULL) {
1880 if ((wp->flags & PANE_STATUSREADY) && WIFEXITED(wp->status))
1881 return (format_printf("%d", WEXITSTATUS(wp->status)));
1882 return (NULL);
1884 return (NULL);
1887 /* Callback for pane_dead_time. */
1888 static void *
1889 format_cb_pane_dead_time(struct format_tree *ft)
1891 struct window_pane *wp = ft->wp;
1893 if (wp != NULL) {
1894 if (wp->flags & PANE_STATUSDRAWN)
1895 return (&wp->dead_time);
1896 return (NULL);
1898 return (NULL);
1901 /* Callback for pane_format. */
1902 static void *
1903 format_cb_pane_format(struct format_tree *ft)
1905 if (ft->type == FORMAT_TYPE_PANE)
1906 return (xstrdup("1"));
1907 return (xstrdup("0"));
1910 /* Callback for pane_height. */
1911 static void *
1912 format_cb_pane_height(struct format_tree *ft)
1914 if (ft->wp != NULL)
1915 return (format_printf("%u", ft->wp->sy));
1916 return (NULL);
1919 /* Callback for pane_id. */
1920 static void *
1921 format_cb_pane_id(struct format_tree *ft)
1923 if (ft->wp != NULL)
1924 return (format_printf("%%%u", ft->wp->id));
1925 return (NULL);
1928 /* Callback for pane_index. */
1929 static void *
1930 format_cb_pane_index(struct format_tree *ft)
1932 u_int idx;
1934 if (ft->wp != NULL && window_pane_index(ft->wp, &idx) == 0)
1935 return (format_printf("%u", idx));
1936 return (NULL);
1939 /* Callback for pane_input_off. */
1940 static void *
1941 format_cb_pane_input_off(struct format_tree *ft)
1943 if (ft->wp != NULL) {
1944 if (ft->wp->flags & PANE_INPUTOFF)
1945 return (xstrdup("1"));
1946 return (xstrdup("0"));
1948 return (NULL);
1951 /* Callback for pane_unseen_changes. */
1952 static void *
1953 format_cb_pane_unseen_changes(struct format_tree *ft)
1955 if (ft->wp != NULL) {
1956 if (ft->wp->flags & PANE_UNSEENCHANGES)
1957 return (xstrdup("1"));
1958 return (xstrdup("0"));
1960 return (NULL);
1963 /* Callback for pane_key_mode. */
1964 static void *
1965 format_cb_pane_key_mode(struct format_tree *ft)
1967 if (ft->wp != NULL && ft->wp->screen != NULL) {
1968 switch (ft->wp->screen->mode & EXTENDED_KEY_MODES) {
1969 case MODE_KEYS_EXTENDED:
1970 return (xstrdup("Ext 1"));
1971 case MODE_KEYS_EXTENDED_2:
1972 return (xstrdup("Ext 2"));
1973 default:
1974 return (xstrdup("VT10x"));
1977 return (NULL);
1980 /* Callback for pane_last. */
1981 static void *
1982 format_cb_pane_last(struct format_tree *ft)
1984 if (ft->wp != NULL) {
1985 if (ft->wp == TAILQ_FIRST(&ft->wp->window->last_panes))
1986 return (xstrdup("1"));
1987 return (xstrdup("0"));
1989 return (NULL);
1992 /* Callback for pane_left. */
1993 static void *
1994 format_cb_pane_left(struct format_tree *ft)
1996 if (ft->wp != NULL)
1997 return (format_printf("%u", ft->wp->xoff));
1998 return (NULL);
2001 /* Callback for pane_marked. */
2002 static void *
2003 format_cb_pane_marked(struct format_tree *ft)
2005 if (ft->wp != NULL) {
2006 if (server_check_marked() && marked_pane.wp == ft->wp)
2007 return (xstrdup("1"));
2008 return (xstrdup("0"));
2010 return (NULL);
2013 /* Callback for pane_marked_set. */
2014 static void *
2015 format_cb_pane_marked_set(struct format_tree *ft)
2017 if (ft->wp != NULL) {
2018 if (server_check_marked())
2019 return (xstrdup("1"));
2020 return (xstrdup("0"));
2022 return (NULL);
2025 /* Callback for pane_mode. */
2026 static void *
2027 format_cb_pane_mode(struct format_tree *ft)
2029 struct window_mode_entry *wme;
2031 if (ft->wp != NULL) {
2032 wme = TAILQ_FIRST(&ft->wp->modes);
2033 if (wme != NULL)
2034 return (xstrdup(wme->mode->name));
2035 return (NULL);
2037 return (NULL);
2040 /* Callback for pane_path. */
2041 static void *
2042 format_cb_pane_path(struct format_tree *ft)
2044 if (ft->wp != NULL) {
2045 if (ft->wp->base.path == NULL)
2046 return (xstrdup(""));
2047 return (xstrdup(ft->wp->base.path));
2049 return (NULL);
2052 /* Callback for pane_pid. */
2053 static void *
2054 format_cb_pane_pid(struct format_tree *ft)
2056 if (ft->wp != NULL)
2057 return (format_printf("%ld", (long)ft->wp->pid));
2058 return (NULL);
2061 /* Callback for pane_pipe. */
2062 static void *
2063 format_cb_pane_pipe(struct format_tree *ft)
2065 if (ft->wp != NULL) {
2066 if (ft->wp->pipe_fd != -1)
2067 return (xstrdup("1"));
2068 return (xstrdup("0"));
2070 return (NULL);
2073 /* Callback for pane_right. */
2074 static void *
2075 format_cb_pane_right(struct format_tree *ft)
2077 if (ft->wp != NULL)
2078 return (format_printf("%u", ft->wp->xoff + ft->wp->sx - 1));
2079 return (NULL);
2082 /* Callback for pane_search_string. */
2083 static void *
2084 format_cb_pane_search_string(struct format_tree *ft)
2086 if (ft->wp != NULL) {
2087 if (ft->wp->searchstr == NULL)
2088 return (xstrdup(""));
2089 return (xstrdup(ft->wp->searchstr));
2091 return (NULL);
2094 /* Callback for pane_synchronized. */
2095 static void *
2096 format_cb_pane_synchronized(struct format_tree *ft)
2098 if (ft->wp != NULL) {
2099 if (options_get_number(ft->wp->options, "synchronize-panes"))
2100 return (xstrdup("1"));
2101 return (xstrdup("0"));
2103 return (NULL);
2106 /* Callback for pane_title. */
2107 static void *
2108 format_cb_pane_title(struct format_tree *ft)
2110 if (ft->wp != NULL)
2111 return (xstrdup(ft->wp->base.title));
2112 return (NULL);
2115 /* Callback for pane_top. */
2116 static void *
2117 format_cb_pane_top(struct format_tree *ft)
2119 if (ft->wp != NULL)
2120 return (format_printf("%u", ft->wp->yoff));
2121 return (NULL);
2124 /* Callback for pane_tty. */
2125 static void *
2126 format_cb_pane_tty(struct format_tree *ft)
2128 if (ft->wp != NULL)
2129 return (xstrdup(ft->wp->tty));
2130 return (NULL);
2133 /* Callback for pane_width. */
2134 static void *
2135 format_cb_pane_width(struct format_tree *ft)
2137 if (ft->wp != NULL)
2138 return (format_printf("%u", ft->wp->sx));
2139 return (NULL);
2142 /* Callback for scroll_region_lower. */
2143 static void *
2144 format_cb_scroll_region_lower(struct format_tree *ft)
2146 if (ft->wp != NULL)
2147 return (format_printf("%u", ft->wp->base.rlower));
2148 return (NULL);
2151 /* Callback for scroll_region_upper. */
2152 static void *
2153 format_cb_scroll_region_upper(struct format_tree *ft)
2155 if (ft->wp != NULL)
2156 return (format_printf("%u", ft->wp->base.rupper));
2157 return (NULL);
2160 /* Callback for server_sessions. */
2161 static void *
2162 format_cb_server_sessions(__unused struct format_tree *ft)
2164 struct session *s;
2165 u_int n = 0;
2167 RB_FOREACH(s, sessions, &sessions)
2168 n++;
2169 return (format_printf("%u", n));
2172 /* Callback for session_attached. */
2173 static void *
2174 format_cb_session_attached(struct format_tree *ft)
2176 if (ft->s != NULL)
2177 return (format_printf("%u", ft->s->attached));
2178 return (NULL);
2181 /* Callback for session_format. */
2182 static void *
2183 format_cb_session_format(struct format_tree *ft)
2185 if (ft->type == FORMAT_TYPE_SESSION)
2186 return (xstrdup("1"));
2187 return (xstrdup("0"));
2190 /* Callback for session_group. */
2191 static void *
2192 format_cb_session_group(struct format_tree *ft)
2194 struct session_group *sg;
2196 if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL)
2197 return (xstrdup(sg->name));
2198 return (NULL);
2201 /* Callback for session_group_attached. */
2202 static void *
2203 format_cb_session_group_attached(struct format_tree *ft)
2205 struct session_group *sg;
2207 if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL)
2208 return (format_printf("%u", session_group_attached_count (sg)));
2209 return (NULL);
2212 /* Callback for session_group_many_attached. */
2213 static void *
2214 format_cb_session_group_many_attached(struct format_tree *ft)
2216 struct session_group *sg;
2218 if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL) {
2219 if (session_group_attached_count (sg) > 1)
2220 return (xstrdup("1"));
2221 return (xstrdup("0"));
2223 return (NULL);
2226 /* Callback for session_group_size. */
2227 static void *
2228 format_cb_session_group_size(struct format_tree *ft)
2230 struct session_group *sg;
2232 if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL)
2233 return (format_printf("%u", session_group_count (sg)));
2234 return (NULL);
2237 /* Callback for session_grouped. */
2238 static void *
2239 format_cb_session_grouped(struct format_tree *ft)
2241 if (ft->s != NULL) {
2242 if (session_group_contains(ft->s) != NULL)
2243 return (xstrdup("1"));
2244 return (xstrdup("0"));
2246 return (NULL);
2249 /* Callback for session_id. */
2250 static void *
2251 format_cb_session_id(struct format_tree *ft)
2253 if (ft->s != NULL)
2254 return (format_printf("$%u", ft->s->id));
2255 return (NULL);
2258 /* Callback for session_many_attached. */
2259 static void *
2260 format_cb_session_many_attached(struct format_tree *ft)
2262 if (ft->s != NULL) {
2263 if (ft->s->attached > 1)
2264 return (xstrdup("1"));
2265 return (xstrdup("0"));
2267 return (NULL);
2270 /* Callback for session_marked. */
2271 static void *
2272 format_cb_session_marked(struct format_tree *ft)
2274 if (ft->s != NULL) {
2275 if (server_check_marked() && marked_pane.s == ft->s)
2276 return (xstrdup("1"));
2277 return (xstrdup("0"));
2279 return (NULL);
2282 /* Callback for session_name. */
2283 static void *
2284 format_cb_session_name(struct format_tree *ft)
2286 if (ft->s != NULL)
2287 return (xstrdup(ft->s->name));
2288 return (NULL);
2291 /* Callback for session_path. */
2292 static void *
2293 format_cb_session_path(struct format_tree *ft)
2295 if (ft->s != NULL)
2296 return (xstrdup(ft->s->cwd));
2297 return (NULL);
2300 /* Callback for session_windows. */
2301 static void *
2302 format_cb_session_windows(struct format_tree *ft)
2304 if (ft->s != NULL)
2305 return (format_printf("%u", winlink_count(&ft->s->windows)));
2306 return (NULL);
2309 /* Callback for socket_path. */
2310 static void *
2311 format_cb_socket_path(__unused struct format_tree *ft)
2313 return (xstrdup(socket_path));
2316 /* Callback for version. */
2317 static void *
2318 format_cb_version(__unused struct format_tree *ft)
2320 return (xstrdup(getversion()));
2323 /* Callback for sixel_support. */
2324 static void *
2325 format_cb_sixel_support(__unused struct format_tree *ft)
2327 #ifdef ENABLE_SIXEL
2328 return (xstrdup("1"));
2329 #else
2330 return (xstrdup("0"));
2331 #endif
2334 /* Callback for active_window_index. */
2335 static void *
2336 format_cb_active_window_index(struct format_tree *ft)
2338 if (ft->s != NULL)
2339 return (format_printf("%u", ft->s->curw->idx));
2340 return (NULL);
2343 /* Callback for last_window_index. */
2344 static void *
2345 format_cb_last_window_index(struct format_tree *ft)
2347 struct winlink *wl;
2349 if (ft->s != NULL) {
2350 wl = RB_MAX(winlinks, &ft->s->windows);
2351 return (format_printf("%u", wl->idx));
2353 return (NULL);
2356 /* Callback for window_active. */
2357 static void *
2358 format_cb_window_active(struct format_tree *ft)
2360 if (ft->wl != NULL) {
2361 if (ft->wl == ft->wl->session->curw)
2362 return (xstrdup("1"));
2363 return (xstrdup("0"));
2365 return (NULL);
2368 /* Callback for window_activity_flag. */
2369 static void *
2370 format_cb_window_activity_flag(struct format_tree *ft)
2372 if (ft->wl != NULL) {
2373 if (ft->wl->flags & WINLINK_ACTIVITY)
2374 return (xstrdup("1"));
2375 return (xstrdup("0"));
2377 return (NULL);
2380 /* Callback for window_bell_flag. */
2381 static void *
2382 format_cb_window_bell_flag(struct format_tree *ft)
2384 if (ft->wl != NULL) {
2385 if (ft->wl->flags & WINLINK_BELL)
2386 return (xstrdup("1"));
2387 return (xstrdup("0"));
2389 return (NULL);
2392 /* Callback for window_bigger. */
2393 static void *
2394 format_cb_window_bigger(struct format_tree *ft)
2396 u_int ox, oy, sx, sy;
2398 if (ft->c != NULL) {
2399 if (tty_window_offset(&ft->c->tty, &ox, &oy, &sx, &sy))
2400 return (xstrdup("1"));
2401 return (xstrdup("0"));
2403 return (NULL);
2406 /* Callback for window_cell_height. */
2407 static void *
2408 format_cb_window_cell_height(struct format_tree *ft)
2410 if (ft->w != NULL)
2411 return (format_printf("%u", ft->w->ypixel));
2412 return (NULL);
2415 /* Callback for window_cell_width. */
2416 static void *
2417 format_cb_window_cell_width(struct format_tree *ft)
2419 if (ft->w != NULL)
2420 return (format_printf("%u", ft->w->xpixel));
2421 return (NULL);
2424 /* Callback for window_end_flag. */
2425 static void *
2426 format_cb_window_end_flag(struct format_tree *ft)
2428 if (ft->wl != NULL) {
2429 if (ft->wl == RB_MAX(winlinks, &ft->wl->session->windows))
2430 return (xstrdup("1"));
2431 return (xstrdup("0"));
2433 return (NULL);
2436 /* Callback for window_flags. */
2437 static void *
2438 format_cb_window_flags(struct format_tree *ft)
2440 if (ft->wl != NULL)
2441 return (xstrdup(window_printable_flags(ft->wl, 1)));
2442 return (NULL);
2445 /* Callback for window_format. */
2446 static void *
2447 format_cb_window_format(struct format_tree *ft)
2449 if (ft->type == FORMAT_TYPE_WINDOW)
2450 return (xstrdup("1"));
2451 return (xstrdup("0"));
2454 /* Callback for window_height. */
2455 static void *
2456 format_cb_window_height(struct format_tree *ft)
2458 if (ft->w != NULL)
2459 return (format_printf("%u", ft->w->sy));
2460 return (NULL);
2463 /* Callback for window_id. */
2464 static void *
2465 format_cb_window_id(struct format_tree *ft)
2467 if (ft->w != NULL)
2468 return (format_printf("@%u", ft->w->id));
2469 return (NULL);
2472 /* Callback for window_index. */
2473 static void *
2474 format_cb_window_index(struct format_tree *ft)
2476 if (ft->wl != NULL)
2477 return (format_printf("%d", ft->wl->idx));
2478 return (NULL);
2481 /* Callback for window_last_flag. */
2482 static void *
2483 format_cb_window_last_flag(struct format_tree *ft)
2485 if (ft->wl != NULL) {
2486 if (ft->wl == TAILQ_FIRST(&ft->wl->session->lastw))
2487 return (xstrdup("1"));
2488 return (xstrdup("0"));
2490 return (NULL);
2493 /* Callback for window_linked. */
2494 static void *
2495 format_cb_window_linked(struct format_tree *ft)
2497 struct winlink *wl;
2498 struct session *s;
2499 int found = 0;
2501 if (ft->wl != NULL) {
2502 RB_FOREACH(s, sessions, &sessions) {
2503 RB_FOREACH(wl, winlinks, &s->windows) {
2504 if (wl->window == ft->wl->window) {
2505 if (found)
2506 return (xstrdup("1"));
2507 found = 1;
2511 return (xstrdup("0"));
2513 return (NULL);
2516 /* Callback for window_linked_sessions. */
2517 static void *
2518 format_cb_window_linked_sessions(struct format_tree *ft)
2520 struct window *w;
2521 struct session_group *sg;
2522 struct session *s;
2523 u_int n = 0;
2525 if (ft->wl == NULL)
2526 return (NULL);
2527 w = ft->wl->window;
2529 RB_FOREACH(sg, session_groups, &session_groups) {
2530 s = TAILQ_FIRST(&sg->sessions);
2531 if (winlink_find_by_window(&s->windows, w) != NULL)
2532 n++;
2534 RB_FOREACH(s, sessions, &sessions) {
2535 if (session_group_contains(s) != NULL)
2536 continue;
2537 if (winlink_find_by_window(&s->windows, w) != NULL)
2538 n++;
2540 return (format_printf("%u", n));
2543 /* Callback for window_marked_flag. */
2544 static void *
2545 format_cb_window_marked_flag(struct format_tree *ft)
2547 if (ft->wl != NULL) {
2548 if (server_check_marked() && marked_pane.wl == ft->wl)
2549 return (xstrdup("1"));
2550 return (xstrdup("0"));
2552 return (NULL);
2555 /* Callback for window_name. */
2556 static void *
2557 format_cb_window_name(struct format_tree *ft)
2559 if (ft->w != NULL)
2560 return (format_printf("%s", ft->w->name));
2561 return (NULL);
2564 /* Callback for window_offset_x. */
2565 static void *
2566 format_cb_window_offset_x(struct format_tree *ft)
2568 u_int ox, oy, sx, sy;
2570 if (ft->c != NULL) {
2571 if (tty_window_offset(&ft->c->tty, &ox, &oy, &sx, &sy))
2572 return (format_printf("%u", ox));
2573 return (NULL);
2575 return (NULL);
2578 /* Callback for window_offset_y. */
2579 static void *
2580 format_cb_window_offset_y(struct format_tree *ft)
2582 u_int ox, oy, sx, sy;
2584 if (ft->c != NULL) {
2585 if (tty_window_offset(&ft->c->tty, &ox, &oy, &sx, &sy))
2586 return (format_printf("%u", oy));
2587 return (NULL);
2589 return (NULL);
2592 /* Callback for window_panes. */
2593 static void *
2594 format_cb_window_panes(struct format_tree *ft)
2596 if (ft->w != NULL)
2597 return (format_printf("%u", window_count_panes(ft->w)));
2598 return (NULL);
2601 /* Callback for window_raw_flags. */
2602 static void *
2603 format_cb_window_raw_flags(struct format_tree *ft)
2605 if (ft->wl != NULL)
2606 return (xstrdup(window_printable_flags(ft->wl, 0)));
2607 return (NULL);
2610 /* Callback for window_silence_flag. */
2611 static void *
2612 format_cb_window_silence_flag(struct format_tree *ft)
2614 if (ft->wl != NULL) {
2615 if (ft->wl->flags & WINLINK_SILENCE)
2616 return (xstrdup("1"));
2617 return (xstrdup("0"));
2619 return (NULL);
2622 /* Callback for window_start_flag. */
2623 static void *
2624 format_cb_window_start_flag(struct format_tree *ft)
2626 if (ft->wl != NULL) {
2627 if (ft->wl == RB_MIN(winlinks, &ft->wl->session->windows))
2628 return (xstrdup("1"));
2629 return (xstrdup("0"));
2631 return (NULL);
2634 /* Callback for window_width. */
2635 static void *
2636 format_cb_window_width(struct format_tree *ft)
2638 if (ft->w != NULL)
2639 return (format_printf("%u", ft->w->sx));
2640 return (NULL);
2643 /* Callback for window_zoomed_flag. */
2644 static void *
2645 format_cb_window_zoomed_flag(struct format_tree *ft)
2647 if (ft->w != NULL) {
2648 if (ft->w->flags & WINDOW_ZOOMED)
2649 return (xstrdup("1"));
2650 return (xstrdup("0"));
2652 return (NULL);
2655 /* Callback for wrap_flag. */
2656 static void *
2657 format_cb_wrap_flag(struct format_tree *ft)
2659 if (ft->wp != NULL) {
2660 if (ft->wp->base.mode & MODE_WRAP)
2661 return (xstrdup("1"));
2662 return (xstrdup("0"));
2664 return (NULL);
2667 /* Callback for buffer_created. */
2668 static void *
2669 format_cb_buffer_created(struct format_tree *ft)
2671 static struct timeval tv;
2673 if (ft->pb != NULL) {
2674 timerclear(&tv);
2675 tv.tv_sec = paste_buffer_created(ft->pb);
2676 return (&tv);
2678 return (NULL);
2681 /* Callback for client_activity. */
2682 static void *
2683 format_cb_client_activity(struct format_tree *ft)
2685 if (ft->c != NULL)
2686 return (&ft->c->activity_time);
2687 return (NULL);
2690 /* Callback for client_created. */
2691 static void *
2692 format_cb_client_created(struct format_tree *ft)
2694 if (ft->c != NULL)
2695 return (&ft->c->creation_time);
2696 return (NULL);
2699 /* Callback for session_activity. */
2700 static void *
2701 format_cb_session_activity(struct format_tree *ft)
2703 if (ft->s != NULL)
2704 return (&ft->s->activity_time);
2705 return (NULL);
2708 /* Callback for session_created. */
2709 static void *
2710 format_cb_session_created(struct format_tree *ft)
2712 if (ft->s != NULL)
2713 return (&ft->s->creation_time);
2714 return (NULL);
2717 /* Callback for session_last_attached. */
2718 static void *
2719 format_cb_session_last_attached(struct format_tree *ft)
2721 if (ft->s != NULL)
2722 return (&ft->s->last_attached_time);
2723 return (NULL);
2726 /* Callback for start_time. */
2727 static void *
2728 format_cb_start_time(__unused struct format_tree *ft)
2730 return (&start_time);
2733 /* Callback for window_activity. */
2734 static void *
2735 format_cb_window_activity(struct format_tree *ft)
2737 if (ft->w != NULL)
2738 return (&ft->w->activity_time);
2739 return (NULL);
2742 /* Callback for buffer_mode_format, */
2743 static void *
2744 format_cb_buffer_mode_format(__unused struct format_tree *ft)
2746 return (xstrdup(window_buffer_mode.default_format));
2749 /* Callback for client_mode_format, */
2750 static void *
2751 format_cb_client_mode_format(__unused struct format_tree *ft)
2753 return (xstrdup(window_client_mode.default_format));
2756 /* Callback for tree_mode_format, */
2757 static void *
2758 format_cb_tree_mode_format(__unused struct format_tree *ft)
2760 return (xstrdup(window_tree_mode.default_format));
2763 /* Callback for uid. */
2764 static void *
2765 format_cb_uid(__unused struct format_tree *ft)
2767 return (format_printf("%ld", (long)getuid()));
2770 /* Callback for user. */
2771 static void *
2772 format_cb_user(__unused struct format_tree *ft)
2774 struct passwd *pw;
2776 if ((pw = getpwuid(getuid())) != NULL)
2777 return (xstrdup(pw->pw_name));
2778 return (NULL);
2781 /* Format table type. */
2782 enum format_table_type {
2783 FORMAT_TABLE_STRING,
2784 FORMAT_TABLE_TIME
2787 /* Format table entry. */
2788 struct format_table_entry {
2789 const char *key;
2790 enum format_table_type type;
2791 format_cb cb;
2795 * Format table. Default format variables (that are almost always in the tree
2796 * and where the value is expanded by a callback in this file) are listed here.
2797 * Only variables which are added by the caller go into the tree.
2799 static const struct format_table_entry format_table[] = {
2800 { "active_window_index", FORMAT_TABLE_STRING,
2801 format_cb_active_window_index
2803 { "alternate_on", FORMAT_TABLE_STRING,
2804 format_cb_alternate_on
2806 { "alternate_saved_x", FORMAT_TABLE_STRING,
2807 format_cb_alternate_saved_x
2809 { "alternate_saved_y", FORMAT_TABLE_STRING,
2810 format_cb_alternate_saved_y
2812 { "buffer_created", FORMAT_TABLE_TIME,
2813 format_cb_buffer_created
2815 { "buffer_mode_format", FORMAT_TABLE_STRING,
2816 format_cb_buffer_mode_format
2818 { "buffer_name", FORMAT_TABLE_STRING,
2819 format_cb_buffer_name
2821 { "buffer_sample", FORMAT_TABLE_STRING,
2822 format_cb_buffer_sample
2824 { "buffer_size", FORMAT_TABLE_STRING,
2825 format_cb_buffer_size
2827 { "client_activity", FORMAT_TABLE_TIME,
2828 format_cb_client_activity
2830 { "client_cell_height", FORMAT_TABLE_STRING,
2831 format_cb_client_cell_height
2833 { "client_cell_width", FORMAT_TABLE_STRING,
2834 format_cb_client_cell_width
2836 { "client_control_mode", FORMAT_TABLE_STRING,
2837 format_cb_client_control_mode
2839 { "client_created", FORMAT_TABLE_TIME,
2840 format_cb_client_created
2842 { "client_discarded", FORMAT_TABLE_STRING,
2843 format_cb_client_discarded
2845 { "client_flags", FORMAT_TABLE_STRING,
2846 format_cb_client_flags
2848 { "client_height", FORMAT_TABLE_STRING,
2849 format_cb_client_height
2851 { "client_key_table", FORMAT_TABLE_STRING,
2852 format_cb_client_key_table
2854 { "client_last_session", FORMAT_TABLE_STRING,
2855 format_cb_client_last_session
2857 { "client_mode_format", FORMAT_TABLE_STRING,
2858 format_cb_client_mode_format
2860 { "client_name", FORMAT_TABLE_STRING,
2861 format_cb_client_name
2863 { "client_pid", FORMAT_TABLE_STRING,
2864 format_cb_client_pid
2866 { "client_prefix", FORMAT_TABLE_STRING,
2867 format_cb_client_prefix
2869 { "client_readonly", FORMAT_TABLE_STRING,
2870 format_cb_client_readonly
2872 { "client_session", FORMAT_TABLE_STRING,
2873 format_cb_client_session
2875 { "client_termfeatures", FORMAT_TABLE_STRING,
2876 format_cb_client_termfeatures
2878 { "client_termname", FORMAT_TABLE_STRING,
2879 format_cb_client_termname
2881 { "client_termtype", FORMAT_TABLE_STRING,
2882 format_cb_client_termtype
2884 { "client_tty", FORMAT_TABLE_STRING,
2885 format_cb_client_tty
2887 { "client_uid", FORMAT_TABLE_STRING,
2888 format_cb_client_uid
2890 { "client_user", FORMAT_TABLE_STRING,
2891 format_cb_client_user
2893 { "client_utf8", FORMAT_TABLE_STRING,
2894 format_cb_client_utf8
2896 { "client_width", FORMAT_TABLE_STRING,
2897 format_cb_client_width
2899 { "client_written", FORMAT_TABLE_STRING,
2900 format_cb_client_written
2902 { "config_files", FORMAT_TABLE_STRING,
2903 format_cb_config_files
2905 { "cursor_character", FORMAT_TABLE_STRING,
2906 format_cb_cursor_character
2908 { "cursor_flag", FORMAT_TABLE_STRING,
2909 format_cb_cursor_flag
2911 { "cursor_x", FORMAT_TABLE_STRING,
2912 format_cb_cursor_x
2914 { "cursor_y", FORMAT_TABLE_STRING,
2915 format_cb_cursor_y
2917 { "history_all_bytes", FORMAT_TABLE_STRING,
2918 format_cb_history_all_bytes
2920 { "history_bytes", FORMAT_TABLE_STRING,
2921 format_cb_history_bytes
2923 { "history_limit", FORMAT_TABLE_STRING,
2924 format_cb_history_limit
2926 { "history_size", FORMAT_TABLE_STRING,
2927 format_cb_history_size
2929 { "host", FORMAT_TABLE_STRING,
2930 format_cb_host
2932 { "host_short", FORMAT_TABLE_STRING,
2933 format_cb_host_short
2935 { "insert_flag", FORMAT_TABLE_STRING,
2936 format_cb_insert_flag
2938 { "keypad_cursor_flag", FORMAT_TABLE_STRING,
2939 format_cb_keypad_cursor_flag
2941 { "keypad_flag", FORMAT_TABLE_STRING,
2942 format_cb_keypad_flag
2944 { "last_window_index", FORMAT_TABLE_STRING,
2945 format_cb_last_window_index
2947 { "mouse_all_flag", FORMAT_TABLE_STRING,
2948 format_cb_mouse_all_flag
2950 { "mouse_any_flag", FORMAT_TABLE_STRING,
2951 format_cb_mouse_any_flag
2953 { "mouse_button_flag", FORMAT_TABLE_STRING,
2954 format_cb_mouse_button_flag
2956 { "mouse_hyperlink", FORMAT_TABLE_STRING,
2957 format_cb_mouse_hyperlink
2959 { "mouse_line", FORMAT_TABLE_STRING,
2960 format_cb_mouse_line
2962 { "mouse_pane", FORMAT_TABLE_STRING,
2963 format_cb_mouse_pane
2965 { "mouse_sgr_flag", FORMAT_TABLE_STRING,
2966 format_cb_mouse_sgr_flag
2968 { "mouse_standard_flag", FORMAT_TABLE_STRING,
2969 format_cb_mouse_standard_flag
2971 { "mouse_status_line", FORMAT_TABLE_STRING,
2972 format_cb_mouse_status_line
2974 { "mouse_status_range", FORMAT_TABLE_STRING,
2975 format_cb_mouse_status_range
2977 { "mouse_utf8_flag", FORMAT_TABLE_STRING,
2978 format_cb_mouse_utf8_flag
2980 { "mouse_word", FORMAT_TABLE_STRING,
2981 format_cb_mouse_word
2983 { "mouse_x", FORMAT_TABLE_STRING,
2984 format_cb_mouse_x
2986 { "mouse_y", FORMAT_TABLE_STRING,
2987 format_cb_mouse_y
2989 { "next_session_id", FORMAT_TABLE_STRING,
2990 format_cb_next_session_id
2992 { "origin_flag", FORMAT_TABLE_STRING,
2993 format_cb_origin_flag
2995 { "pane_active", FORMAT_TABLE_STRING,
2996 format_cb_pane_active
2998 { "pane_at_bottom", FORMAT_TABLE_STRING,
2999 format_cb_pane_at_bottom
3001 { "pane_at_left", FORMAT_TABLE_STRING,
3002 format_cb_pane_at_left
3004 { "pane_at_right", FORMAT_TABLE_STRING,
3005 format_cb_pane_at_right
3007 { "pane_at_top", FORMAT_TABLE_STRING,
3008 format_cb_pane_at_top
3010 { "pane_bg", FORMAT_TABLE_STRING,
3011 format_cb_pane_bg
3013 { "pane_bottom", FORMAT_TABLE_STRING,
3014 format_cb_pane_bottom
3016 { "pane_current_command", FORMAT_TABLE_STRING,
3017 format_cb_current_command
3019 { "pane_current_path", FORMAT_TABLE_STRING,
3020 format_cb_current_path
3022 { "pane_dead", FORMAT_TABLE_STRING,
3023 format_cb_pane_dead
3025 { "pane_dead_signal", FORMAT_TABLE_STRING,
3026 format_cb_pane_dead_signal
3028 { "pane_dead_status", FORMAT_TABLE_STRING,
3029 format_cb_pane_dead_status
3031 { "pane_dead_time", FORMAT_TABLE_TIME,
3032 format_cb_pane_dead_time
3034 { "pane_fg", FORMAT_TABLE_STRING,
3035 format_cb_pane_fg
3037 { "pane_format", FORMAT_TABLE_STRING,
3038 format_cb_pane_format
3040 { "pane_height", FORMAT_TABLE_STRING,
3041 format_cb_pane_height
3043 { "pane_id", FORMAT_TABLE_STRING,
3044 format_cb_pane_id
3046 { "pane_in_mode", FORMAT_TABLE_STRING,
3047 format_cb_pane_in_mode
3049 { "pane_index", FORMAT_TABLE_STRING,
3050 format_cb_pane_index
3052 { "pane_input_off", FORMAT_TABLE_STRING,
3053 format_cb_pane_input_off
3055 { "pane_key_mode", FORMAT_TABLE_STRING,
3056 format_cb_pane_key_mode
3058 { "pane_last", FORMAT_TABLE_STRING,
3059 format_cb_pane_last
3061 { "pane_left", FORMAT_TABLE_STRING,
3062 format_cb_pane_left
3064 { "pane_marked", FORMAT_TABLE_STRING,
3065 format_cb_pane_marked
3067 { "pane_marked_set", FORMAT_TABLE_STRING,
3068 format_cb_pane_marked_set
3070 { "pane_mode", FORMAT_TABLE_STRING,
3071 format_cb_pane_mode
3073 { "pane_path", FORMAT_TABLE_STRING,
3074 format_cb_pane_path
3076 { "pane_pid", FORMAT_TABLE_STRING,
3077 format_cb_pane_pid
3079 { "pane_pipe", FORMAT_TABLE_STRING,
3080 format_cb_pane_pipe
3082 { "pane_right", FORMAT_TABLE_STRING,
3083 format_cb_pane_right
3085 { "pane_search_string", FORMAT_TABLE_STRING,
3086 format_cb_pane_search_string
3088 { "pane_start_command", FORMAT_TABLE_STRING,
3089 format_cb_start_command
3091 { "pane_start_path", FORMAT_TABLE_STRING,
3092 format_cb_start_path
3094 { "pane_synchronized", FORMAT_TABLE_STRING,
3095 format_cb_pane_synchronized
3097 { "pane_tabs", FORMAT_TABLE_STRING,
3098 format_cb_pane_tabs
3100 { "pane_title", FORMAT_TABLE_STRING,
3101 format_cb_pane_title
3103 { "pane_top", FORMAT_TABLE_STRING,
3104 format_cb_pane_top
3106 { "pane_tty", FORMAT_TABLE_STRING,
3107 format_cb_pane_tty
3109 { "pane_unseen_changes", FORMAT_TABLE_STRING,
3110 format_cb_pane_unseen_changes
3112 { "pane_width", FORMAT_TABLE_STRING,
3113 format_cb_pane_width
3115 { "pid", FORMAT_TABLE_STRING,
3116 format_cb_pid
3118 { "scroll_region_lower", FORMAT_TABLE_STRING,
3119 format_cb_scroll_region_lower
3121 { "scroll_region_upper", FORMAT_TABLE_STRING,
3122 format_cb_scroll_region_upper
3124 { "server_sessions", FORMAT_TABLE_STRING,
3125 format_cb_server_sessions
3127 { "session_activity", FORMAT_TABLE_TIME,
3128 format_cb_session_activity
3130 { "session_alerts", FORMAT_TABLE_STRING,
3131 format_cb_session_alerts
3133 { "session_attached", FORMAT_TABLE_STRING,
3134 format_cb_session_attached
3136 { "session_attached_list", FORMAT_TABLE_STRING,
3137 format_cb_session_attached_list
3139 { "session_created", FORMAT_TABLE_TIME,
3140 format_cb_session_created
3142 { "session_format", FORMAT_TABLE_STRING,
3143 format_cb_session_format
3145 { "session_group", FORMAT_TABLE_STRING,
3146 format_cb_session_group
3148 { "session_group_attached", FORMAT_TABLE_STRING,
3149 format_cb_session_group_attached
3151 { "session_group_attached_list", FORMAT_TABLE_STRING,
3152 format_cb_session_group_attached_list
3154 { "session_group_list", FORMAT_TABLE_STRING,
3155 format_cb_session_group_list
3157 { "session_group_many_attached", FORMAT_TABLE_STRING,
3158 format_cb_session_group_many_attached
3160 { "session_group_size", FORMAT_TABLE_STRING,
3161 format_cb_session_group_size
3163 { "session_grouped", FORMAT_TABLE_STRING,
3164 format_cb_session_grouped
3166 { "session_id", FORMAT_TABLE_STRING,
3167 format_cb_session_id
3169 { "session_last_attached", FORMAT_TABLE_TIME,
3170 format_cb_session_last_attached
3172 { "session_many_attached", FORMAT_TABLE_STRING,
3173 format_cb_session_many_attached
3175 { "session_marked", FORMAT_TABLE_STRING,
3176 format_cb_session_marked,
3178 { "session_name", FORMAT_TABLE_STRING,
3179 format_cb_session_name
3181 { "session_path", FORMAT_TABLE_STRING,
3182 format_cb_session_path
3184 { "session_stack", FORMAT_TABLE_STRING,
3185 format_cb_session_stack
3187 { "session_windows", FORMAT_TABLE_STRING,
3188 format_cb_session_windows
3190 { "sixel_support", FORMAT_TABLE_STRING,
3191 format_cb_sixel_support
3193 { "socket_path", FORMAT_TABLE_STRING,
3194 format_cb_socket_path
3196 { "start_time", FORMAT_TABLE_TIME,
3197 format_cb_start_time
3199 { "tree_mode_format", FORMAT_TABLE_STRING,
3200 format_cb_tree_mode_format
3202 { "uid", FORMAT_TABLE_STRING,
3203 format_cb_uid
3205 { "user", FORMAT_TABLE_STRING,
3206 format_cb_user
3208 { "version", FORMAT_TABLE_STRING,
3209 format_cb_version
3211 { "window_active", FORMAT_TABLE_STRING,
3212 format_cb_window_active
3214 { "window_active_clients", FORMAT_TABLE_STRING,
3215 format_cb_window_active_clients
3217 { "window_active_clients_list", FORMAT_TABLE_STRING,
3218 format_cb_window_active_clients_list
3220 { "window_active_sessions", FORMAT_TABLE_STRING,
3221 format_cb_window_active_sessions
3223 { "window_active_sessions_list", FORMAT_TABLE_STRING,
3224 format_cb_window_active_sessions_list
3226 { "window_activity", FORMAT_TABLE_TIME,
3227 format_cb_window_activity
3229 { "window_activity_flag", FORMAT_TABLE_STRING,
3230 format_cb_window_activity_flag
3232 { "window_bell_flag", FORMAT_TABLE_STRING,
3233 format_cb_window_bell_flag
3235 { "window_bigger", FORMAT_TABLE_STRING,
3236 format_cb_window_bigger
3238 { "window_cell_height", FORMAT_TABLE_STRING,
3239 format_cb_window_cell_height
3241 { "window_cell_width", FORMAT_TABLE_STRING,
3242 format_cb_window_cell_width
3244 { "window_end_flag", FORMAT_TABLE_STRING,
3245 format_cb_window_end_flag
3247 { "window_flags", FORMAT_TABLE_STRING,
3248 format_cb_window_flags
3250 { "window_format", FORMAT_TABLE_STRING,
3251 format_cb_window_format
3253 { "window_height", FORMAT_TABLE_STRING,
3254 format_cb_window_height
3256 { "window_id", FORMAT_TABLE_STRING,
3257 format_cb_window_id
3259 { "window_index", FORMAT_TABLE_STRING,
3260 format_cb_window_index
3262 { "window_last_flag", FORMAT_TABLE_STRING,
3263 format_cb_window_last_flag
3265 { "window_layout", FORMAT_TABLE_STRING,
3266 format_cb_window_layout
3268 { "window_linked", FORMAT_TABLE_STRING,
3269 format_cb_window_linked
3271 { "window_linked_sessions", FORMAT_TABLE_STRING,
3272 format_cb_window_linked_sessions
3274 { "window_linked_sessions_list", FORMAT_TABLE_STRING,
3275 format_cb_window_linked_sessions_list
3277 { "window_marked_flag", FORMAT_TABLE_STRING,
3278 format_cb_window_marked_flag
3280 { "window_name", FORMAT_TABLE_STRING,
3281 format_cb_window_name
3283 { "window_offset_x", FORMAT_TABLE_STRING,
3284 format_cb_window_offset_x
3286 { "window_offset_y", FORMAT_TABLE_STRING,
3287 format_cb_window_offset_y
3289 { "window_panes", FORMAT_TABLE_STRING,
3290 format_cb_window_panes
3292 { "window_raw_flags", FORMAT_TABLE_STRING,
3293 format_cb_window_raw_flags
3295 { "window_silence_flag", FORMAT_TABLE_STRING,
3296 format_cb_window_silence_flag
3298 { "window_stack_index", FORMAT_TABLE_STRING,
3299 format_cb_window_stack_index
3301 { "window_start_flag", FORMAT_TABLE_STRING,
3302 format_cb_window_start_flag
3304 { "window_visible_layout", FORMAT_TABLE_STRING,
3305 format_cb_window_visible_layout
3307 { "window_width", FORMAT_TABLE_STRING,
3308 format_cb_window_width
3310 { "window_zoomed_flag", FORMAT_TABLE_STRING,
3311 format_cb_window_zoomed_flag
3313 { "wrap_flag", FORMAT_TABLE_STRING,
3314 format_cb_wrap_flag
3318 /* Compare format table entries. */
3319 static int
3320 format_table_compare(const void *key0, const void *entry0)
3322 const char *key = key0;
3323 const struct format_table_entry *entry = entry0;
3325 return (strcmp(key, entry->key));
3328 /* Get a format callback. */
3329 static struct format_table_entry *
3330 format_table_get(const char *key)
3332 return (bsearch(key, format_table, nitems(format_table),
3333 sizeof *format_table, format_table_compare));
3336 /* Merge one format tree into another. */
3337 void
3338 format_merge(struct format_tree *ft, struct format_tree *from)
3340 struct format_entry *fe;
3342 RB_FOREACH(fe, format_entry_tree, &from->tree) {
3343 if (fe->value != NULL)
3344 format_add(ft, fe->key, "%s", fe->value);
3348 /* Get format pane. */
3349 struct window_pane *
3350 format_get_pane(struct format_tree *ft)
3352 return (ft->wp);
3355 /* Add item bits to tree. */
3356 static void
3357 format_create_add_item(struct format_tree *ft, struct cmdq_item *item)
3359 struct key_event *event = cmdq_get_event(item);
3360 struct mouse_event *m = &event->m;
3362 cmdq_merge_formats(item, ft);
3363 memcpy(&ft->m, m, sizeof ft->m);
3366 /* Create a new tree. */
3367 struct format_tree *
3368 format_create(struct client *c, struct cmdq_item *item, int tag, int flags)
3370 struct format_tree *ft;
3372 ft = xcalloc(1, sizeof *ft);
3373 RB_INIT(&ft->tree);
3375 if (c != NULL) {
3376 ft->client = c;
3377 ft->client->references++;
3379 ft->item = item;
3381 ft->tag = tag;
3382 ft->flags = flags;
3384 if (item != NULL)
3385 format_create_add_item(ft, item);
3387 return (ft);
3390 /* Free a tree. */
3391 void
3392 format_free(struct format_tree *ft)
3394 struct format_entry *fe, *fe1;
3396 RB_FOREACH_SAFE(fe, format_entry_tree, &ft->tree, fe1) {
3397 RB_REMOVE(format_entry_tree, &ft->tree, fe);
3398 free(fe->value);
3399 free(fe->key);
3400 free(fe);
3403 if (ft->client != NULL)
3404 server_client_unref(ft->client);
3405 free(ft);
3408 /* Log each format. */
3409 static void
3410 format_log_debug_cb(const char *key, const char *value, void *arg)
3412 const char *prefix = arg;
3414 log_debug("%s: %s=%s", prefix, key, value);
3417 /* Log a format tree. */
3418 void
3419 format_log_debug(struct format_tree *ft, const char *prefix)
3421 format_each(ft, format_log_debug_cb, (void *)prefix);
3424 /* Walk each format. */
3425 void
3426 format_each(struct format_tree *ft, void (*cb)(const char *, const char *,
3427 void *), void *arg)
3429 const struct format_table_entry *fte;
3430 struct format_entry *fe;
3431 u_int i;
3432 char s[64];
3433 void *value;
3434 struct timeval *tv;
3436 for (i = 0; i < nitems(format_table); i++) {
3437 fte = &format_table[i];
3439 value = fte->cb(ft);
3440 if (value == NULL)
3441 continue;
3442 if (fte->type == FORMAT_TABLE_TIME) {
3443 tv = value;
3444 xsnprintf(s, sizeof s, "%lld", (long long)tv->tv_sec);
3445 cb(fte->key, s, arg);
3446 } else {
3447 cb(fte->key, value, arg);
3448 free(value);
3451 RB_FOREACH(fe, format_entry_tree, &ft->tree) {
3452 if (fe->time != 0) {
3453 xsnprintf(s, sizeof s, "%lld", (long long)fe->time);
3454 cb(fe->key, s, arg);
3455 } else {
3456 if (fe->value == NULL && fe->cb != NULL) {
3457 fe->value = fe->cb(ft);
3458 if (fe->value == NULL)
3459 fe->value = xstrdup("");
3461 cb(fe->key, fe->value, arg);
3466 /* Add a key-value pair. */
3467 void
3468 format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
3470 struct format_entry *fe;
3471 struct format_entry *fe_now;
3472 va_list ap;
3474 fe = xmalloc(sizeof *fe);
3475 fe->key = xstrdup(key);
3477 fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
3478 if (fe_now != NULL) {
3479 free(fe->key);
3480 free(fe);
3481 free(fe_now->value);
3482 fe = fe_now;
3485 fe->cb = NULL;
3486 fe->time = 0;
3488 va_start(ap, fmt);
3489 xvasprintf(&fe->value, fmt, ap);
3490 va_end(ap);
3493 /* Add a key and time. */
3494 void
3495 format_add_tv(struct format_tree *ft, const char *key, struct timeval *tv)
3497 struct format_entry *fe, *fe_now;
3499 fe = xmalloc(sizeof *fe);
3500 fe->key = xstrdup(key);
3502 fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
3503 if (fe_now != NULL) {
3504 free(fe->key);
3505 free(fe);
3506 free(fe_now->value);
3507 fe = fe_now;
3510 fe->cb = NULL;
3511 fe->time = tv->tv_sec;
3513 fe->value = NULL;
3516 /* Add a key and function. */
3517 void
3518 format_add_cb(struct format_tree *ft, const char *key, format_cb cb)
3520 struct format_entry *fe;
3521 struct format_entry *fe_now;
3523 fe = xmalloc(sizeof *fe);
3524 fe->key = xstrdup(key);
3526 fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
3527 if (fe_now != NULL) {
3528 free(fe->key);
3529 free(fe);
3530 free(fe_now->value);
3531 fe = fe_now;
3534 fe->cb = cb;
3535 fe->time = 0;
3537 fe->value = NULL;
3540 /* Quote shell special characters in string. */
3541 static char *
3542 format_quote_shell(const char *s)
3544 const char *cp;
3545 char *out, *at;
3547 at = out = xmalloc(strlen(s) * 2 + 1);
3548 for (cp = s; *cp != '\0'; cp++) {
3549 if (strchr("|&;<>()$`\\\"'*?[# =%", *cp) != NULL)
3550 *at++ = '\\';
3551 *at++ = *cp;
3553 *at = '\0';
3554 return (out);
3557 /* Quote #s in string. */
3558 static char *
3559 format_quote_style(const char *s)
3561 const char *cp;
3562 char *out, *at;
3564 at = out = xmalloc(strlen(s) * 2 + 1);
3565 for (cp = s; *cp != '\0'; cp++) {
3566 if (*cp == '#')
3567 *at++ = '#';
3568 *at++ = *cp;
3570 *at = '\0';
3571 return (out);
3574 /* Make a prettier time. */
3575 char *
3576 format_pretty_time(time_t t, int seconds)
3578 struct tm now_tm, tm;
3579 time_t now, age;
3580 char s[9];
3582 time(&now);
3583 if (now < t)
3584 now = t;
3585 age = now - t;
3587 localtime_r(&now, &now_tm);
3588 localtime_r(&t, &tm);
3590 /* Last 24 hours. */
3591 if (age < 24 * 3600) {
3592 if (seconds)
3593 strftime(s, sizeof s, "%H:%M:%S", &tm);
3594 else
3595 strftime(s, sizeof s, "%H:%M", &tm);
3596 return (xstrdup(s));
3599 /* This month or last 28 days. */
3600 if ((tm.tm_year == now_tm.tm_year && tm.tm_mon == now_tm.tm_mon) ||
3601 age < 28 * 24 * 3600) {
3602 strftime(s, sizeof s, "%a%d", &tm);
3603 return (xstrdup(s));
3606 /* Last 12 months. */
3607 if ((tm.tm_year == now_tm.tm_year && tm.tm_mon < now_tm.tm_mon) ||
3608 (tm.tm_year == now_tm.tm_year - 1 && tm.tm_mon > now_tm.tm_mon)) {
3609 strftime(s, sizeof s, "%d%b", &tm);
3610 return (xstrdup(s));
3613 /* Older than that. */
3614 strftime(s, sizeof s, "%h%y", &tm);
3615 return (xstrdup(s));
3618 /* Find a format entry. */
3619 static char *
3620 format_find(struct format_tree *ft, const char *key, int modifiers,
3621 const char *time_format)
3623 struct format_table_entry *fte;
3624 void *value;
3625 struct format_entry *fe, fe_find;
3626 struct environ_entry *envent;
3627 struct options_entry *o;
3628 int idx;
3629 char *found = NULL, *saved, s[512];
3630 const char *errstr;
3631 time_t t = 0;
3632 struct tm tm;
3634 o = options_parse_get(global_options, key, &idx, 0);
3635 if (o == NULL && ft->wp != NULL)
3636 o = options_parse_get(ft->wp->options, key, &idx, 0);
3637 if (o == NULL && ft->w != NULL)
3638 o = options_parse_get(ft->w->options, key, &idx, 0);
3639 if (o == NULL)
3640 o = options_parse_get(global_w_options, key, &idx, 0);
3641 if (o == NULL && ft->s != NULL)
3642 o = options_parse_get(ft->s->options, key, &idx, 0);
3643 if (o == NULL)
3644 o = options_parse_get(global_s_options, key, &idx, 0);
3645 if (o != NULL) {
3646 found = options_to_string(o, idx, 1);
3647 goto found;
3650 fte = format_table_get(key);
3651 if (fte != NULL) {
3652 value = fte->cb(ft);
3653 if (fte->type == FORMAT_TABLE_TIME && value != NULL)
3654 t = ((struct timeval *)value)->tv_sec;
3655 else
3656 found = value;
3657 goto found;
3659 fe_find.key = (char *)key;
3660 fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find);
3661 if (fe != NULL) {
3662 if (fe->time != 0) {
3663 t = fe->time;
3664 goto found;
3666 if (fe->value == NULL && fe->cb != NULL) {
3667 fe->value = fe->cb(ft);
3668 if (fe->value == NULL)
3669 fe->value = xstrdup("");
3671 found = xstrdup(fe->value);
3672 goto found;
3675 if (~modifiers & FORMAT_TIMESTRING) {
3676 envent = NULL;
3677 if (ft->s != NULL)
3678 envent = environ_find(ft->s->environ, key);
3679 if (envent == NULL)
3680 envent = environ_find(global_environ, key);
3681 if (envent != NULL && envent->value != NULL) {
3682 found = xstrdup(envent->value);
3683 goto found;
3687 return (NULL);
3689 found:
3690 if (modifiers & FORMAT_TIMESTRING) {
3691 if (t == 0 && found != NULL) {
3692 t = strtonum(found, 0, INT64_MAX, &errstr);
3693 if (errstr != NULL)
3694 t = 0;
3695 free(found);
3697 if (t == 0)
3698 return (NULL);
3699 if (modifiers & FORMAT_PRETTY)
3700 found = format_pretty_time(t, 0);
3701 else {
3702 if (time_format != NULL) {
3703 localtime_r(&t, &tm);
3704 strftime(s, sizeof s, time_format, &tm);
3705 } else {
3706 ctime_r(&t, s);
3707 s[strcspn(s, "\n")] = '\0';
3709 found = xstrdup(s);
3711 return (found);
3714 if (t != 0)
3715 xasprintf(&found, "%lld", (long long)t);
3716 else if (found == NULL)
3717 return (NULL);
3718 if (modifiers & FORMAT_BASENAME) {
3719 saved = found;
3720 found = xstrdup(basename(saved));
3721 free(saved);
3723 if (modifiers & FORMAT_DIRNAME) {
3724 saved = found;
3725 found = xstrdup(dirname(saved));
3726 free(saved);
3728 if (modifiers & FORMAT_QUOTE_SHELL) {
3729 saved = found;
3730 found = format_quote_shell(saved);
3731 free(saved);
3733 if (modifiers & FORMAT_QUOTE_STYLE) {
3734 saved = found;
3735 found = format_quote_style(saved);
3736 free(saved);
3738 return (found);
3741 /* Unescape escaped characters. */
3742 static char *
3743 format_unescape(const char *s)
3745 char *out, *cp;
3746 int brackets = 0;
3748 cp = out = xmalloc(strlen(s) + 1);
3749 for (; *s != '\0'; s++) {
3750 if (*s == '#' && s[1] == '{')
3751 brackets++;
3752 if (brackets == 0 &&
3753 *s == '#' &&
3754 strchr(",#{}:", s[1]) != NULL) {
3755 *cp++ = *++s;
3756 continue;
3758 if (*s == '}')
3759 brackets--;
3760 *cp++ = *s;
3762 *cp = '\0';
3763 return (out);
3766 /* Remove escaped characters. */
3767 static char *
3768 format_strip(const char *s)
3770 char *out, *cp;
3771 int brackets = 0;
3773 cp = out = xmalloc(strlen(s) + 1);
3774 for (; *s != '\0'; s++) {
3775 if (*s == '#' && s[1] == '{')
3776 brackets++;
3777 if (*s == '#' && strchr(",#{}:", s[1]) != NULL) {
3778 if (brackets != 0)
3779 *cp++ = *s;
3780 continue;
3782 if (*s == '}')
3783 brackets--;
3784 *cp++ = *s;
3786 *cp = '\0';
3787 return (out);
3790 /* Skip until end. */
3791 const char *
3792 format_skip(const char *s, const char *end)
3794 int brackets = 0;
3796 for (; *s != '\0'; s++) {
3797 if (*s == '#' && s[1] == '{')
3798 brackets++;
3799 if (*s == '#' &&
3800 s[1] != '\0' &&
3801 strchr(",#{}:", s[1]) != NULL) {
3802 s++;
3803 continue;
3805 if (*s == '}')
3806 brackets--;
3807 if (strchr(end, *s) != NULL && brackets == 0)
3808 break;
3810 if (*s == '\0')
3811 return (NULL);
3812 return (s);
3815 /* Return left and right alternatives separated by commas. */
3816 static int
3817 format_choose(struct format_expand_state *es, const char *s, char **left,
3818 char **right, int expand)
3820 const char *cp;
3821 char *left0, *right0;
3823 cp = format_skip(s, ",");
3824 if (cp == NULL)
3825 return (-1);
3826 left0 = xstrndup(s, cp - s);
3827 right0 = xstrdup(cp + 1);
3829 if (expand) {
3830 *left = format_expand1(es, left0);
3831 free(left0);
3832 *right = format_expand1(es, right0);
3833 free(right0);
3834 } else {
3835 *left = left0;
3836 *right = right0;
3838 return (0);
3841 /* Is this true? */
3843 format_true(const char *s)
3845 if (s != NULL && *s != '\0' && (s[0] != '0' || s[1] != '\0'))
3846 return (1);
3847 return (0);
3850 /* Check if modifier end. */
3851 static int
3852 format_is_end(char c)
3854 return (c == ';' || c == ':');
3857 /* Add to modifier list. */
3858 static void
3859 format_add_modifier(struct format_modifier **list, u_int *count,
3860 const char *c, size_t n, char **argv, int argc)
3862 struct format_modifier *fm;
3864 *list = xreallocarray(*list, (*count) + 1, sizeof **list);
3865 fm = &(*list)[(*count)++];
3867 memcpy(fm->modifier, c, n);
3868 fm->modifier[n] = '\0';
3869 fm->size = n;
3871 fm->argv = argv;
3872 fm->argc = argc;
3875 /* Free modifier list. */
3876 static void
3877 format_free_modifiers(struct format_modifier *list, u_int count)
3879 u_int i;
3881 for (i = 0; i < count; i++)
3882 cmd_free_argv(list[i].argc, list[i].argv);
3883 free(list);
3886 /* Build modifier list. */
3887 static struct format_modifier *
3888 format_build_modifiers(struct format_expand_state *es, const char **s,
3889 u_int *count)
3891 const char *cp = *s, *end;
3892 struct format_modifier *list = NULL;
3893 char c, last[] = "X;:", **argv, *value;
3894 int argc;
3897 * Modifiers are a ; separated list of the forms:
3898 * l,m,C,a,b,c,d,n,t,w,q,E,T,S,W,P,<,>
3899 * =a
3900 * =/a
3901 * =/a/
3902 * s/a/b/
3903 * s/a/b
3904 * ||,&&,!=,==,<=,>=
3907 *count = 0;
3909 while (*cp != '\0' && *cp != ':') {
3910 /* Skip any separator character. */
3911 if (*cp == ';')
3912 cp++;
3914 /* Check single character modifiers with no arguments. */
3915 if (strchr("labcdnwETSWPL<>", cp[0]) != NULL &&
3916 format_is_end(cp[1])) {
3917 format_add_modifier(&list, count, cp, 1, NULL, 0);
3918 cp++;
3919 continue;
3922 /* Then try double character with no arguments. */
3923 if ((memcmp("||", cp, 2) == 0 ||
3924 memcmp("&&", cp, 2) == 0 ||
3925 memcmp("!=", cp, 2) == 0 ||
3926 memcmp("==", cp, 2) == 0 ||
3927 memcmp("<=", cp, 2) == 0 ||
3928 memcmp(">=", cp, 2) == 0) &&
3929 format_is_end(cp[2])) {
3930 format_add_modifier(&list, count, cp, 2, NULL, 0);
3931 cp += 2;
3932 continue;
3935 /* Now try single character with arguments. */
3936 if (strchr("mCNst=peq", cp[0]) == NULL)
3937 break;
3938 c = cp[0];
3940 /* No arguments provided. */
3941 if (format_is_end(cp[1])) {
3942 format_add_modifier(&list, count, cp, 1, NULL, 0);
3943 cp++;
3944 continue;
3946 argv = NULL;
3947 argc = 0;
3949 /* Single argument with no wrapper character. */
3950 if (!ispunct((u_char)cp[1]) || cp[1] == '-') {
3951 end = format_skip(cp + 1, ":;");
3952 if (end == NULL)
3953 break;
3955 argv = xcalloc(1, sizeof *argv);
3956 value = xstrndup(cp + 1, end - (cp + 1));
3957 argv[0] = format_expand1(es, value);
3958 free(value);
3959 argc = 1;
3961 format_add_modifier(&list, count, &c, 1, argv, argc);
3962 cp = end;
3963 continue;
3966 /* Multiple arguments with a wrapper character. */
3967 last[0] = cp[1];
3968 cp++;
3969 do {
3970 if (cp[0] == last[0] && format_is_end(cp[1])) {
3971 cp++;
3972 break;
3974 end = format_skip(cp + 1, last);
3975 if (end == NULL)
3976 break;
3977 cp++;
3979 argv = xreallocarray(argv, argc + 1, sizeof *argv);
3980 value = xstrndup(cp, end - cp);
3981 argv[argc++] = format_expand1(es, value);
3982 free(value);
3984 cp = end;
3985 } while (!format_is_end(cp[0]));
3986 format_add_modifier(&list, count, &c, 1, argv, argc);
3988 if (*cp != ':') {
3989 format_free_modifiers(list, *count);
3990 *count = 0;
3991 return (NULL);
3993 *s = cp + 1;
3994 return (list);
3997 /* Match against an fnmatch(3) pattern or regular expression. */
3998 static char *
3999 format_match(struct format_modifier *fm, const char *pattern, const char *text)
4001 const char *s = "";
4002 regex_t r;
4003 int flags = 0;
4005 if (fm->argc >= 1)
4006 s = fm->argv[0];
4007 if (strchr(s, 'r') == NULL) {
4008 if (strchr(s, 'i') != NULL)
4009 flags |= FNM_CASEFOLD;
4010 if (fnmatch(pattern, text, flags) != 0)
4011 return (xstrdup("0"));
4012 } else {
4013 flags = REG_EXTENDED|REG_NOSUB;
4014 if (strchr(s, 'i') != NULL)
4015 flags |= REG_ICASE;
4016 if (regcomp(&r, pattern, flags) != 0)
4017 return (xstrdup("0"));
4018 if (regexec(&r, text, 0, NULL, 0) != 0) {
4019 regfree(&r);
4020 return (xstrdup("0"));
4022 regfree(&r);
4024 return (xstrdup("1"));
4027 /* Perform substitution in string. */
4028 static char *
4029 format_sub(struct format_modifier *fm, const char *text, const char *pattern,
4030 const char *with)
4032 char *value;
4033 int flags = REG_EXTENDED;
4035 if (fm->argc >= 3 && strchr(fm->argv[2], 'i') != NULL)
4036 flags |= REG_ICASE;
4037 value = regsub(pattern, with, text, flags);
4038 if (value == NULL)
4039 return (xstrdup(text));
4040 return (value);
4043 /* Search inside pane. */
4044 static char *
4045 format_search(struct format_modifier *fm, struct window_pane *wp, const char *s)
4047 int ignore = 0, regex = 0;
4048 char *value;
4050 if (fm->argc >= 1) {
4051 if (strchr(fm->argv[0], 'i') != NULL)
4052 ignore = 1;
4053 if (strchr(fm->argv[0], 'r') != NULL)
4054 regex = 1;
4056 xasprintf(&value, "%u", window_pane_search(wp, s, regex, ignore));
4057 return (value);
4060 /* Does session name exist? */
4061 static char *
4062 format_session_name(struct format_expand_state *es, const char *fmt)
4064 char *name;
4065 struct session *s;
4067 name = format_expand1(es, fmt);
4068 RB_FOREACH(s, sessions, &sessions) {
4069 if (strcmp(s->name, name) == 0) {
4070 free(name);
4071 return (xstrdup("1"));
4074 free(name);
4075 return (xstrdup("0"));
4078 /* Loop over sessions. */
4079 static char *
4080 format_loop_sessions(struct format_expand_state *es, const char *fmt)
4082 struct format_tree *ft = es->ft;
4083 struct client *c = ft->client;
4084 struct cmdq_item *item = ft->item;
4085 struct format_tree *nft;
4086 struct format_expand_state next;
4087 char *expanded, *value;
4088 size_t valuelen;
4089 struct session *s;
4091 value = xcalloc(1, 1);
4092 valuelen = 1;
4094 RB_FOREACH(s, sessions, &sessions) {
4095 format_log(es, "session loop: $%u", s->id);
4096 nft = format_create(c, item, FORMAT_NONE, ft->flags);
4097 format_defaults(nft, ft->c, s, NULL, NULL);
4098 format_copy_state(&next, es, 0);
4099 next.ft = nft;
4100 expanded = format_expand1(&next, fmt);
4101 format_free(next.ft);
4103 valuelen += strlen(expanded);
4104 value = xrealloc(value, valuelen);
4106 strlcat(value, expanded, valuelen);
4107 free(expanded);
4110 return (value);
4113 /* Does window name exist? */
4114 static char *
4115 format_window_name(struct format_expand_state *es, const char *fmt)
4117 struct format_tree *ft = es->ft;
4118 char *name;
4119 struct winlink *wl;
4121 if (ft->s == NULL) {
4122 format_log(es, "window name but no session");
4123 return (NULL);
4126 name = format_expand1(es, fmt);
4127 RB_FOREACH(wl, winlinks, &ft->s->windows) {
4128 if (strcmp(wl->window->name, name) == 0) {
4129 free(name);
4130 return (xstrdup("1"));
4133 free(name);
4134 return (xstrdup("0"));
4137 /* Loop over windows. */
4138 static char *
4139 format_loop_windows(struct format_expand_state *es, const char *fmt)
4141 struct format_tree *ft = es->ft;
4142 struct client *c = ft->client;
4143 struct cmdq_item *item = ft->item;
4144 struct format_tree *nft;
4145 struct format_expand_state next;
4146 char *all, *active, *use, *expanded, *value;
4147 size_t valuelen;
4148 struct winlink *wl;
4149 struct window *w;
4151 if (ft->s == NULL) {
4152 format_log(es, "window loop but no session");
4153 return (NULL);
4156 if (format_choose(es, fmt, &all, &active, 0) != 0) {
4157 all = xstrdup(fmt);
4158 active = NULL;
4161 value = xcalloc(1, 1);
4162 valuelen = 1;
4164 RB_FOREACH(wl, winlinks, &ft->s->windows) {
4165 w = wl->window;
4166 format_log(es, "window loop: %u @%u", wl->idx, w->id);
4167 if (active != NULL && wl == ft->s->curw)
4168 use = active;
4169 else
4170 use = all;
4171 nft = format_create(c, item, FORMAT_WINDOW|w->id, ft->flags);
4172 format_defaults(nft, ft->c, ft->s, wl, NULL);
4173 format_copy_state(&next, es, 0);
4174 next.ft = nft;
4175 expanded = format_expand1(&next, use);
4176 format_free(nft);
4178 valuelen += strlen(expanded);
4179 value = xrealloc(value, valuelen);
4181 strlcat(value, expanded, valuelen);
4182 free(expanded);
4185 free(active);
4186 free(all);
4188 return (value);
4191 /* Loop over panes. */
4192 static char *
4193 format_loop_panes(struct format_expand_state *es, const char *fmt)
4195 struct format_tree *ft = es->ft;
4196 struct client *c = ft->client;
4197 struct cmdq_item *item = ft->item;
4198 struct format_tree *nft;
4199 struct format_expand_state next;
4200 char *all, *active, *use, *expanded, *value;
4201 size_t valuelen;
4202 struct window_pane *wp;
4204 if (ft->w == NULL) {
4205 format_log(es, "pane loop but no window");
4206 return (NULL);
4209 if (format_choose(es, fmt, &all, &active, 0) != 0) {
4210 all = xstrdup(fmt);
4211 active = NULL;
4214 value = xcalloc(1, 1);
4215 valuelen = 1;
4217 TAILQ_FOREACH(wp, &ft->w->panes, entry) {
4218 format_log(es, "pane loop: %%%u", wp->id);
4219 if (active != NULL && wp == ft->w->active)
4220 use = active;
4221 else
4222 use = all;
4223 nft = format_create(c, item, FORMAT_PANE|wp->id, ft->flags);
4224 format_defaults(nft, ft->c, ft->s, ft->wl, wp);
4225 format_copy_state(&next, es, 0);
4226 next.ft = nft;
4227 expanded = format_expand1(&next, use);
4228 format_free(nft);
4230 valuelen += strlen(expanded);
4231 value = xrealloc(value, valuelen);
4233 strlcat(value, expanded, valuelen);
4234 free(expanded);
4237 free(active);
4238 free(all);
4240 return (value);
4243 /* Loop over clients. */
4244 static char *
4245 format_loop_clients(struct format_expand_state *es, const char *fmt)
4247 struct format_tree *ft = es->ft;
4248 struct client *c;
4249 struct cmdq_item *item = ft->item;
4250 struct format_tree *nft;
4251 struct format_expand_state next;
4252 char *expanded, *value;
4253 size_t valuelen;
4255 value = xcalloc(1, 1);
4256 valuelen = 1;
4258 TAILQ_FOREACH(c, &clients, entry) {
4259 format_log(es, "client loop: %s", c->name);
4260 nft = format_create(c, item, 0, ft->flags);
4261 format_defaults(nft, c, ft->s, ft->wl, ft->wp);
4262 format_copy_state(&next, es, 0);
4263 next.ft = nft;
4264 expanded = format_expand1(&next, fmt);
4265 format_free(nft);
4267 valuelen += strlen(expanded);
4268 value = xrealloc(value, valuelen);
4270 strlcat(value, expanded, valuelen);
4271 free(expanded);
4274 return (value);
4277 static char *
4278 format_replace_expression(struct format_modifier *mexp,
4279 struct format_expand_state *es, const char *copy)
4281 int argc = mexp->argc;
4282 const char *errstr;
4283 char *endch, *value, *left = NULL, *right = NULL;
4284 int use_fp = 0;
4285 u_int prec = 0;
4286 double mleft, mright, result;
4287 enum { ADD,
4288 SUBTRACT,
4289 MULTIPLY,
4290 DIVIDE,
4291 MODULUS,
4292 EQUAL,
4293 NOT_EQUAL,
4294 GREATER_THAN,
4295 GREATER_THAN_EQUAL,
4296 LESS_THAN,
4297 LESS_THAN_EQUAL } operator;
4299 if (strcmp(mexp->argv[0], "+") == 0)
4300 operator = ADD;
4301 else if (strcmp(mexp->argv[0], "-") == 0)
4302 operator = SUBTRACT;
4303 else if (strcmp(mexp->argv[0], "*") == 0)
4304 operator = MULTIPLY;
4305 else if (strcmp(mexp->argv[0], "/") == 0)
4306 operator = DIVIDE;
4307 else if (strcmp(mexp->argv[0], "%") == 0 ||
4308 strcmp(mexp->argv[0], "m") == 0)
4309 operator = MODULUS;
4310 else if (strcmp(mexp->argv[0], "==") == 0)
4311 operator = EQUAL;
4312 else if (strcmp(mexp->argv[0], "!=") == 0)
4313 operator = NOT_EQUAL;
4314 else if (strcmp(mexp->argv[0], ">") == 0)
4315 operator = GREATER_THAN;
4316 else if (strcmp(mexp->argv[0], "<") == 0)
4317 operator = LESS_THAN;
4318 else if (strcmp(mexp->argv[0], ">=") == 0)
4319 operator = GREATER_THAN_EQUAL;
4320 else if (strcmp(mexp->argv[0], "<=") == 0)
4321 operator = LESS_THAN_EQUAL;
4322 else {
4323 format_log(es, "expression has no valid operator: '%s'",
4324 mexp->argv[0]);
4325 goto fail;
4328 /* The second argument may be flags. */
4329 if (argc >= 2 && strchr(mexp->argv[1], 'f') != NULL) {
4330 use_fp = 1;
4331 prec = 2;
4334 /* The third argument may be precision. */
4335 if (argc >= 3) {
4336 prec = strtonum(mexp->argv[2], INT_MIN, INT_MAX, &errstr);
4337 if (errstr != NULL) {
4338 format_log(es, "expression precision %s: %s", errstr,
4339 mexp->argv[2]);
4340 goto fail;
4344 if (format_choose(es, copy, &left, &right, 1) != 0) {
4345 format_log(es, "expression syntax error");
4346 goto fail;
4349 mleft = strtod(left, &endch);
4350 if (*endch != '\0') {
4351 format_log(es, "expression left side is invalid: %s", left);
4352 goto fail;
4355 mright = strtod(right, &endch);
4356 if (*endch != '\0') {
4357 format_log(es, "expression right side is invalid: %s", right);
4358 goto fail;
4361 if (!use_fp) {
4362 mleft = (long long)mleft;
4363 mright = (long long)mright;
4365 format_log(es, "expression left side is: %.*f", prec, mleft);
4366 format_log(es, "expression right side is: %.*f", prec, mright);
4368 switch (operator) {
4369 case ADD:
4370 result = mleft + mright;
4371 break;
4372 case SUBTRACT:
4373 result = mleft - mright;
4374 break;
4375 case MULTIPLY:
4376 result = mleft * mright;
4377 break;
4378 case DIVIDE:
4379 result = mleft / mright;
4380 break;
4381 case MODULUS:
4382 result = fmod(mleft, mright);
4383 break;
4384 case EQUAL:
4385 result = fabs(mleft - mright) < 1e-9;
4386 break;
4387 case NOT_EQUAL:
4388 result = fabs(mleft - mright) > 1e-9;
4389 break;
4390 case GREATER_THAN:
4391 result = (mleft > mright);
4392 break;
4393 case GREATER_THAN_EQUAL:
4394 result = (mleft >= mright);
4395 break;
4396 case LESS_THAN:
4397 result = (mleft < mright);
4398 break;
4399 case LESS_THAN_EQUAL:
4400 result = (mleft <= mright);
4401 break;
4403 if (use_fp)
4404 xasprintf(&value, "%.*f", prec, result);
4405 else
4406 xasprintf(&value, "%.*f", prec, (double)(long long)result);
4407 format_log(es, "expression result is %s", value);
4409 free(right);
4410 free(left);
4411 return (value);
4413 fail:
4414 free(right);
4415 free(left);
4416 return (NULL);
4419 /* Replace a key. */
4420 static int
4421 format_replace(struct format_expand_state *es, const char *key, size_t keylen,
4422 char **buf, size_t *len, size_t *off)
4424 struct format_tree *ft = es->ft;
4425 struct window_pane *wp = ft->wp;
4426 const char *errstr, *copy, *cp, *marker = NULL;
4427 const char *time_format = NULL;
4428 char *copy0, *condition, *found, *new;
4429 char *value, *left, *right;
4430 size_t valuelen;
4431 int modifiers = 0, limit = 0, width = 0;
4432 int j, c;
4433 struct format_modifier *list, *cmp = NULL, *search = NULL;
4434 struct format_modifier **sub = NULL, *mexp = NULL, *fm;
4435 u_int i, count, nsub = 0;
4436 struct format_expand_state next;
4438 /* Make a copy of the key. */
4439 copy = copy0 = xstrndup(key, keylen);
4441 /* Process modifier list. */
4442 list = format_build_modifiers(es, &copy, &count);
4443 for (i = 0; i < count; i++) {
4444 fm = &list[i];
4445 if (format_logging(ft)) {
4446 format_log(es, "modifier %u is %s", i, fm->modifier);
4447 for (j = 0; j < fm->argc; j++) {
4448 format_log(es, "modifier %u argument %d: %s", i,
4449 j, fm->argv[j]);
4452 if (fm->size == 1) {
4453 switch (fm->modifier[0]) {
4454 case 'm':
4455 case '<':
4456 case '>':
4457 cmp = fm;
4458 break;
4459 case 'C':
4460 search = fm;
4461 break;
4462 case 's':
4463 if (fm->argc < 2)
4464 break;
4465 sub = xreallocarray(sub, nsub + 1, sizeof *sub);
4466 sub[nsub++] = fm;
4467 break;
4468 case '=':
4469 if (fm->argc < 1)
4470 break;
4471 limit = strtonum(fm->argv[0], INT_MIN, INT_MAX,
4472 &errstr);
4473 if (errstr != NULL)
4474 limit = 0;
4475 if (fm->argc >= 2 && fm->argv[1] != NULL)
4476 marker = fm->argv[1];
4477 break;
4478 case 'p':
4479 if (fm->argc < 1)
4480 break;
4481 width = strtonum(fm->argv[0], INT_MIN, INT_MAX,
4482 &errstr);
4483 if (errstr != NULL)
4484 width = 0;
4485 break;
4486 case 'w':
4487 modifiers |= FORMAT_WIDTH;
4488 break;
4489 case 'e':
4490 if (fm->argc < 1 || fm->argc > 3)
4491 break;
4492 mexp = fm;
4493 break;
4494 case 'l':
4495 modifiers |= FORMAT_LITERAL;
4496 break;
4497 case 'a':
4498 modifiers |= FORMAT_CHARACTER;
4499 break;
4500 case 'b':
4501 modifiers |= FORMAT_BASENAME;
4502 break;
4503 case 'c':
4504 modifiers |= FORMAT_COLOUR;
4505 break;
4506 case 'd':
4507 modifiers |= FORMAT_DIRNAME;
4508 break;
4509 case 'n':
4510 modifiers |= FORMAT_LENGTH;
4511 break;
4512 case 't':
4513 modifiers |= FORMAT_TIMESTRING;
4514 if (fm->argc < 1)
4515 break;
4516 if (strchr(fm->argv[0], 'p') != NULL)
4517 modifiers |= FORMAT_PRETTY;
4518 else if (fm->argc >= 2 &&
4519 strchr(fm->argv[0], 'f') != NULL)
4520 time_format = format_strip(fm->argv[1]);
4521 break;
4522 case 'q':
4523 if (fm->argc < 1)
4524 modifiers |= FORMAT_QUOTE_SHELL;
4525 else if (strchr(fm->argv[0], 'e') != NULL ||
4526 strchr(fm->argv[0], 'h') != NULL)
4527 modifiers |= FORMAT_QUOTE_STYLE;
4528 break;
4529 case 'E':
4530 modifiers |= FORMAT_EXPAND;
4531 break;
4532 case 'T':
4533 modifiers |= FORMAT_EXPANDTIME;
4534 break;
4535 case 'N':
4536 if (fm->argc < 1 ||
4537 strchr(fm->argv[0], 'w') != NULL)
4538 modifiers |= FORMAT_WINDOW_NAME;
4539 else if (strchr(fm->argv[0], 's') != NULL)
4540 modifiers |= FORMAT_SESSION_NAME;
4541 break;
4542 case 'S':
4543 modifiers |= FORMAT_SESSIONS;
4544 break;
4545 case 'W':
4546 modifiers |= FORMAT_WINDOWS;
4547 break;
4548 case 'P':
4549 modifiers |= FORMAT_PANES;
4550 break;
4551 case 'L':
4552 modifiers |= FORMAT_CLIENTS;
4553 break;
4555 } else if (fm->size == 2) {
4556 if (strcmp(fm->modifier, "||") == 0 ||
4557 strcmp(fm->modifier, "&&") == 0 ||
4558 strcmp(fm->modifier, "==") == 0 ||
4559 strcmp(fm->modifier, "!=") == 0 ||
4560 strcmp(fm->modifier, ">=") == 0 ||
4561 strcmp(fm->modifier, "<=") == 0)
4562 cmp = fm;
4566 /* Is this a literal string? */
4567 if (modifiers & FORMAT_LITERAL) {
4568 format_log(es, "literal string is '%s'", copy);
4569 value = format_unescape(copy);
4570 goto done;
4573 /* Is this a character? */
4574 if (modifiers & FORMAT_CHARACTER) {
4575 new = format_expand1(es, copy);
4576 c = strtonum(new, 32, 126, &errstr);
4577 if (errstr != NULL)
4578 value = xstrdup("");
4579 else
4580 xasprintf(&value, "%c", c);
4581 free(new);
4582 goto done;
4585 /* Is this a colour? */
4586 if (modifiers & FORMAT_COLOUR) {
4587 new = format_expand1(es, copy);
4588 c = colour_fromstring(new);
4589 if (c == -1 || (c = colour_force_rgb(c)) == -1)
4590 value = xstrdup("");
4591 else
4592 xasprintf(&value, "%06x", c & 0xffffff);
4593 free(new);
4594 goto done;
4597 /* Is this a loop, comparison or condition? */
4598 if (modifiers & FORMAT_SESSIONS) {
4599 value = format_loop_sessions(es, copy);
4600 if (value == NULL)
4601 goto fail;
4602 } else if (modifiers & FORMAT_WINDOWS) {
4603 value = format_loop_windows(es, copy);
4604 if (value == NULL)
4605 goto fail;
4606 } else if (modifiers & FORMAT_PANES) {
4607 value = format_loop_panes(es, copy);
4608 if (value == NULL)
4609 goto fail;
4610 } else if (modifiers & FORMAT_CLIENTS) {
4611 value = format_loop_clients(es, copy);
4612 if (value == NULL)
4613 goto fail;
4614 } else if (modifiers & FORMAT_WINDOW_NAME) {
4615 value = format_window_name(es, copy);
4616 if (value == NULL)
4617 goto fail;
4618 } else if (modifiers & FORMAT_SESSION_NAME) {
4619 value = format_session_name(es, copy);
4620 if (value == NULL)
4621 goto fail;
4622 } else if (search != NULL) {
4623 /* Search in pane. */
4624 new = format_expand1(es, copy);
4625 if (wp == NULL) {
4626 format_log(es, "search '%s' but no pane", new);
4627 value = xstrdup("0");
4628 } else {
4629 format_log(es, "search '%s' pane %%%u", new, wp->id);
4630 value = format_search(search, wp, new);
4632 free(new);
4633 } else if (cmp != NULL) {
4634 /* Comparison of left and right. */
4635 if (format_choose(es, copy, &left, &right, 1) != 0) {
4636 format_log(es, "compare %s syntax error: %s",
4637 cmp->modifier, copy);
4638 goto fail;
4640 format_log(es, "compare %s left is: %s", cmp->modifier, left);
4641 format_log(es, "compare %s right is: %s", cmp->modifier, right);
4643 if (strcmp(cmp->modifier, "||") == 0) {
4644 if (format_true(left) || format_true(right))
4645 value = xstrdup("1");
4646 else
4647 value = xstrdup("0");
4648 } else if (strcmp(cmp->modifier, "&&") == 0) {
4649 if (format_true(left) && format_true(right))
4650 value = xstrdup("1");
4651 else
4652 value = xstrdup("0");
4653 } else if (strcmp(cmp->modifier, "==") == 0) {
4654 if (strcmp(left, right) == 0)
4655 value = xstrdup("1");
4656 else
4657 value = xstrdup("0");
4658 } else if (strcmp(cmp->modifier, "!=") == 0) {
4659 if (strcmp(left, right) != 0)
4660 value = xstrdup("1");
4661 else
4662 value = xstrdup("0");
4663 } else if (strcmp(cmp->modifier, "<") == 0) {
4664 if (strcmp(left, right) < 0)
4665 value = xstrdup("1");
4666 else
4667 value = xstrdup("0");
4668 } else if (strcmp(cmp->modifier, ">") == 0) {
4669 if (strcmp(left, right) > 0)
4670 value = xstrdup("1");
4671 else
4672 value = xstrdup("0");
4673 } else if (strcmp(cmp->modifier, "<=") == 0) {
4674 if (strcmp(left, right) <= 0)
4675 value = xstrdup("1");
4676 else
4677 value = xstrdup("0");
4678 } else if (strcmp(cmp->modifier, ">=") == 0) {
4679 if (strcmp(left, right) >= 0)
4680 value = xstrdup("1");
4681 else
4682 value = xstrdup("0");
4683 } else if (strcmp(cmp->modifier, "m") == 0)
4684 value = format_match(cmp, left, right);
4686 free(right);
4687 free(left);
4688 } else if (*copy == '?') {
4689 /* Conditional: check first and choose second or third. */
4690 cp = format_skip(copy + 1, ",");
4691 if (cp == NULL) {
4692 format_log(es, "condition syntax error: %s", copy + 1);
4693 goto fail;
4695 condition = xstrndup(copy + 1, cp - (copy + 1));
4696 format_log(es, "condition is: %s", condition);
4698 found = format_find(ft, condition, modifiers, time_format);
4699 if (found == NULL) {
4701 * If the condition not found, try to expand it. If
4702 * the expansion doesn't have any effect, then assume
4703 * false.
4705 found = format_expand1(es, condition);
4706 if (strcmp(found, condition) == 0) {
4707 free(found);
4708 found = xstrdup("");
4709 format_log(es,
4710 "condition '%s' not found; assuming false",
4711 condition);
4713 } else {
4714 format_log(es, "condition '%s' found: %s", condition,
4715 found);
4718 if (format_choose(es, cp + 1, &left, &right, 0) != 0) {
4719 format_log(es, "condition '%s' syntax error: %s",
4720 condition, cp + 1);
4721 free(found);
4722 goto fail;
4724 if (format_true(found)) {
4725 format_log(es, "condition '%s' is true", condition);
4726 value = format_expand1(es, left);
4727 } else {
4728 format_log(es, "condition '%s' is false", condition);
4729 value = format_expand1(es, right);
4731 free(right);
4732 free(left);
4734 free(condition);
4735 free(found);
4736 } else if (mexp != NULL) {
4737 value = format_replace_expression(mexp, es, copy);
4738 if (value == NULL)
4739 value = xstrdup("");
4740 } else {
4741 if (strstr(copy, "#{") != 0) {
4742 format_log(es, "expanding inner format '%s'", copy);
4743 value = format_expand1(es, copy);
4744 } else {
4745 value = format_find(ft, copy, modifiers, time_format);
4746 if (value == NULL) {
4747 format_log(es, "format '%s' not found", copy);
4748 value = xstrdup("");
4749 } else {
4750 format_log(es, "format '%s' found: %s", copy,
4751 value);
4756 done:
4757 /* Expand again if required. */
4758 if (modifiers & FORMAT_EXPAND) {
4759 new = format_expand1(es, value);
4760 free(value);
4761 value = new;
4762 } else if (modifiers & FORMAT_EXPANDTIME) {
4763 format_copy_state(&next, es, FORMAT_EXPAND_TIME);
4764 new = format_expand1(&next, value);
4765 free(value);
4766 value = new;
4769 /* Perform substitution if any. */
4770 for (i = 0; i < nsub; i++) {
4771 left = format_expand1(es, sub[i]->argv[0]);
4772 right = format_expand1(es, sub[i]->argv[1]);
4773 new = format_sub(sub[i], value, left, right);
4774 format_log(es, "substitute '%s' to '%s': %s", left, right, new);
4775 free(value);
4776 value = new;
4777 free(right);
4778 free(left);
4781 /* Truncate the value if needed. */
4782 if (limit > 0) {
4783 new = format_trim_left(value, limit);
4784 if (marker != NULL && strcmp(new, value) != 0) {
4785 free(value);
4786 xasprintf(&value, "%s%s", new, marker);
4787 } else {
4788 free(value);
4789 value = new;
4791 format_log(es, "applied length limit %d: %s", limit, value);
4792 } else if (limit < 0) {
4793 new = format_trim_right(value, -limit);
4794 if (marker != NULL && strcmp(new, value) != 0) {
4795 free(value);
4796 xasprintf(&value, "%s%s", marker, new);
4797 } else {
4798 free(value);
4799 value = new;
4801 format_log(es, "applied length limit %d: %s", limit, value);
4804 /* Pad the value if needed. */
4805 if (width > 0) {
4806 new = utf8_padcstr(value, width);
4807 free(value);
4808 value = new;
4809 format_log(es, "applied padding width %d: %s", width, value);
4810 } else if (width < 0) {
4811 new = utf8_rpadcstr(value, -width);
4812 free(value);
4813 value = new;
4814 format_log(es, "applied padding width %d: %s", width, value);
4817 /* Replace with the length or width if needed. */
4818 if (modifiers & FORMAT_LENGTH) {
4819 xasprintf(&new, "%zu", strlen(value));
4820 free(value);
4821 value = new;
4822 format_log(es, "replacing with length: %s", new);
4824 if (modifiers & FORMAT_WIDTH) {
4825 xasprintf(&new, "%u", format_width(value));
4826 free(value);
4827 value = new;
4828 format_log(es, "replacing with width: %s", new);
4831 /* Expand the buffer and copy in the value. */
4832 valuelen = strlen(value);
4833 while (*len - *off < valuelen + 1) {
4834 *buf = xreallocarray(*buf, 2, *len);
4835 *len *= 2;
4837 memcpy(*buf + *off, value, valuelen);
4838 *off += valuelen;
4840 format_log(es, "replaced '%s' with '%s'", copy0, value);
4841 free(value);
4843 free(sub);
4844 format_free_modifiers(list, count);
4845 free(copy0);
4846 return (0);
4848 fail:
4849 format_log(es, "failed %s", copy0);
4851 free(sub);
4852 format_free_modifiers(list, count);
4853 free(copy0);
4854 return (-1);
4857 /* Expand keys in a template. */
4858 static char *
4859 format_expand1(struct format_expand_state *es, const char *fmt)
4861 struct format_tree *ft = es->ft;
4862 char *buf, *out, *name;
4863 const char *ptr, *s, *style_end = NULL;
4864 size_t off, len, n, outlen;
4865 int ch, brackets;
4866 char expanded[8192];
4868 if (fmt == NULL || *fmt == '\0')
4869 return (xstrdup(""));
4871 if (es->loop == FORMAT_LOOP_LIMIT) {
4872 format_log(es, "reached loop limit (%u)", FORMAT_LOOP_LIMIT);
4873 return (xstrdup(""));
4875 es->loop++;
4877 format_log(es, "expanding format: %s", fmt);
4879 if ((es->flags & FORMAT_EXPAND_TIME) && strchr(fmt, '%') != NULL) {
4880 if (es->time == 0) {
4881 es->time = time(NULL);
4882 localtime_r(&es->time, &es->tm);
4884 if (strftime(expanded, sizeof expanded, fmt, &es->tm) == 0) {
4885 format_log(es, "format is too long");
4886 return (xstrdup(""));
4888 if (format_logging(ft) && strcmp(expanded, fmt) != 0)
4889 format_log(es, "after time expanded: %s", expanded);
4890 fmt = expanded;
4893 len = 64;
4894 buf = xmalloc(len);
4895 off = 0;
4897 while (*fmt != '\0') {
4898 if (*fmt != '#') {
4899 while (len - off < 2) {
4900 buf = xreallocarray(buf, 2, len);
4901 len *= 2;
4903 buf[off++] = *fmt++;
4904 continue;
4906 fmt++;
4908 ch = (u_char)*fmt++;
4909 switch (ch) {
4910 case '(':
4911 brackets = 1;
4912 for (ptr = fmt; *ptr != '\0'; ptr++) {
4913 if (*ptr == '(')
4914 brackets++;
4915 if (*ptr == ')' && --brackets == 0)
4916 break;
4918 if (*ptr != ')' || brackets != 0)
4919 break;
4920 n = ptr - fmt;
4922 name = xstrndup(fmt, n);
4923 format_log(es, "found #(): %s", name);
4925 if ((ft->flags & FORMAT_NOJOBS) ||
4926 (es->flags & FORMAT_EXPAND_NOJOBS)) {
4927 out = xstrdup("");
4928 format_log(es, "#() is disabled");
4929 } else {
4930 out = format_job_get(es, name);
4931 format_log(es, "#() result: %s", out);
4933 free(name);
4935 outlen = strlen(out);
4936 while (len - off < outlen + 1) {
4937 buf = xreallocarray(buf, 2, len);
4938 len *= 2;
4940 memcpy(buf + off, out, outlen);
4941 off += outlen;
4943 free(out);
4945 fmt += n + 1;
4946 continue;
4947 case '{':
4948 ptr = format_skip((char *)fmt - 2, "}");
4949 if (ptr == NULL)
4950 break;
4951 n = ptr - fmt;
4953 format_log(es, "found #{}: %.*s", (int)n, fmt);
4954 if (format_replace(es, fmt, n, &buf, &len, &off) != 0)
4955 break;
4956 fmt += n + 1;
4957 continue;
4958 case '[':
4959 case '#':
4961 * If ##[ (with two or more #s), then it is a style and
4962 * can be left for format_draw to handle.
4964 ptr = fmt - (ch == '[');
4965 n = 2 - (ch == '[');
4966 while (*ptr == '#') {
4967 ptr++;
4968 n++;
4970 if (*ptr == '[') {
4971 style_end = format_skip(fmt - 2, "]");
4972 format_log(es, "found #*%zu[", n);
4973 while (len - off < n + 2) {
4974 buf = xreallocarray(buf, 2, len);
4975 len *= 2;
4977 memcpy(buf + off, fmt - 2, n + 1);
4978 off += n + 1;
4979 fmt = ptr + 1;
4980 continue;
4982 /* FALLTHROUGH */
4983 case '}':
4984 case ',':
4985 format_log(es, "found #%c", ch);
4986 while (len - off < 2) {
4987 buf = xreallocarray(buf, 2, len);
4988 len *= 2;
4990 buf[off++] = ch;
4991 continue;
4992 default:
4993 s = NULL;
4994 if (fmt > style_end) { /* skip inside #[] */
4995 if (ch >= 'A' && ch <= 'Z')
4996 s = format_upper[ch - 'A'];
4997 else if (ch >= 'a' && ch <= 'z')
4998 s = format_lower[ch - 'a'];
5000 if (s == NULL) {
5001 while (len - off < 3) {
5002 buf = xreallocarray(buf, 2, len);
5003 len *= 2;
5005 buf[off++] = '#';
5006 buf[off++] = ch;
5007 continue;
5009 n = strlen(s);
5010 format_log(es, "found #%c: %s", ch, s);
5011 if (format_replace(es, s, n, &buf, &len, &off) != 0)
5012 break;
5013 continue;
5016 break;
5018 buf[off] = '\0';
5020 format_log(es, "result is: %s", buf);
5021 es->loop--;
5023 return (buf);
5026 /* Expand keys in a template, passing through strftime first. */
5027 char *
5028 format_expand_time(struct format_tree *ft, const char *fmt)
5030 struct format_expand_state es;
5032 memset(&es, 0, sizeof es);
5033 es.ft = ft;
5034 es.flags = FORMAT_EXPAND_TIME;
5035 return (format_expand1(&es, fmt));
5038 /* Expand keys in a template. */
5039 char *
5040 format_expand(struct format_tree *ft, const char *fmt)
5042 struct format_expand_state es;
5044 memset(&es, 0, sizeof es);
5045 es.ft = ft;
5046 es.flags = 0;
5047 return (format_expand1(&es, fmt));
5050 /* Expand a single string. */
5051 char *
5052 format_single(struct cmdq_item *item, const char *fmt, struct client *c,
5053 struct session *s, struct winlink *wl, struct window_pane *wp)
5055 struct format_tree *ft;
5056 char *expanded;
5058 ft = format_create_defaults(item, c, s, wl, wp);
5059 expanded = format_expand(ft, fmt);
5060 format_free(ft);
5061 return (expanded);
5064 /* Expand a single string using state. */
5065 char *
5066 format_single_from_state(struct cmdq_item *item, const char *fmt,
5067 struct client *c, struct cmd_find_state *fs)
5069 return (format_single(item, fmt, c, fs->s, fs->wl, fs->wp));
5072 /* Expand a single string using target. */
5073 char *
5074 format_single_from_target(struct cmdq_item *item, const char *fmt)
5076 struct client *tc = cmdq_get_target_client(item);
5078 return (format_single_from_state(item, fmt, tc, cmdq_get_target(item)));
5081 /* Create and add defaults. */
5082 struct format_tree *
5083 format_create_defaults(struct cmdq_item *item, struct client *c,
5084 struct session *s, struct winlink *wl, struct window_pane *wp)
5086 struct format_tree *ft;
5088 if (item != NULL)
5089 ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0);
5090 else
5091 ft = format_create(NULL, item, FORMAT_NONE, 0);
5092 format_defaults(ft, c, s, wl, wp);
5093 return (ft);
5096 /* Create and add defaults using state. */
5097 struct format_tree *
5098 format_create_from_state(struct cmdq_item *item, struct client *c,
5099 struct cmd_find_state *fs)
5101 return (format_create_defaults(item, c, fs->s, fs->wl, fs->wp));
5104 /* Create and add defaults using target. */
5105 struct format_tree *
5106 format_create_from_target(struct cmdq_item *item)
5108 struct client *tc = cmdq_get_target_client(item);
5110 return (format_create_from_state(item, tc, cmdq_get_target(item)));
5113 /* Set defaults for any of arguments that are not NULL. */
5114 void
5115 format_defaults(struct format_tree *ft, struct client *c, struct session *s,
5116 struct winlink *wl, struct window_pane *wp)
5118 struct paste_buffer *pb;
5120 if (c != NULL && c->name != NULL)
5121 log_debug("%s: c=%s", __func__, c->name);
5122 else
5123 log_debug("%s: c=none", __func__);
5124 if (s != NULL)
5125 log_debug("%s: s=$%u", __func__, s->id);
5126 else
5127 log_debug("%s: s=none", __func__);
5128 if (wl != NULL)
5129 log_debug("%s: wl=%u", __func__, wl->idx);
5130 else
5131 log_debug("%s: wl=none", __func__);
5132 if (wp != NULL)
5133 log_debug("%s: wp=%%%u", __func__, wp->id);
5134 else
5135 log_debug("%s: wp=none", __func__);
5137 if (c != NULL && s != NULL && c->session != s)
5138 log_debug("%s: session does not match", __func__);
5140 if (wp != NULL)
5141 ft->type = FORMAT_TYPE_PANE;
5142 else if (wl != NULL)
5143 ft->type = FORMAT_TYPE_WINDOW;
5144 else if (s != NULL)
5145 ft->type = FORMAT_TYPE_SESSION;
5146 else
5147 ft->type = FORMAT_TYPE_UNKNOWN;
5149 if (s == NULL && c != NULL)
5150 s = c->session;
5151 if (wl == NULL && s != NULL)
5152 wl = s->curw;
5153 if (wp == NULL && wl != NULL)
5154 wp = wl->window->active;
5156 if (c != NULL)
5157 format_defaults_client(ft, c);
5158 if (s != NULL)
5159 format_defaults_session(ft, s);
5160 if (wl != NULL)
5161 format_defaults_winlink(ft, wl);
5162 if (wp != NULL)
5163 format_defaults_pane(ft, wp);
5165 pb = paste_get_top(NULL);
5166 if (pb != NULL)
5167 format_defaults_paste_buffer(ft, pb);
5170 /* Set default format keys for a session. */
5171 static void
5172 format_defaults_session(struct format_tree *ft, struct session *s)
5174 ft->s = s;
5177 /* Set default format keys for a client. */
5178 static void
5179 format_defaults_client(struct format_tree *ft, struct client *c)
5181 if (ft->s == NULL)
5182 ft->s = c->session;
5183 ft->c = c;
5186 /* Set default format keys for a window. */
5187 void
5188 format_defaults_window(struct format_tree *ft, struct window *w)
5190 ft->w = w;
5193 /* Set default format keys for a winlink. */
5194 static void
5195 format_defaults_winlink(struct format_tree *ft, struct winlink *wl)
5197 if (ft->w == NULL)
5198 format_defaults_window(ft, wl->window);
5199 ft->wl = wl;
5202 /* Set default format keys for a window pane. */
5203 void
5204 format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
5206 struct window_mode_entry *wme;
5208 if (ft->w == NULL)
5209 format_defaults_window(ft, wp->window);
5210 ft->wp = wp;
5212 wme = TAILQ_FIRST(&wp->modes);
5213 if (wme != NULL && wme->mode->formats != NULL)
5214 wme->mode->formats(wme, ft);
5217 /* Set default format keys for paste buffer. */
5218 void
5219 format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
5221 ft->pb = pb;
5224 static int
5225 format_is_word_separator(const char *ws, const struct grid_cell *gc)
5227 if (utf8_cstrhas(ws, &gc->data))
5228 return (1);
5229 if (gc->flags & GRID_FLAG_TAB)
5230 return (1);
5231 return gc->data.size == 1 && *gc->data.data == ' ';
5234 /* Return word at given coordinates. Caller frees. */
5235 char *
5236 format_grid_word(struct grid *gd, u_int x, u_int y)
5238 const struct grid_line *gl;
5239 struct grid_cell gc;
5240 const char *ws;
5241 struct utf8_data *ud = NULL;
5242 u_int end;
5243 size_t size = 0;
5244 int found = 0;
5245 char *s = NULL;
5247 ws = options_get_string(global_s_options, "word-separators");
5249 for (;;) {
5250 grid_get_cell(gd, x, y, &gc);
5251 if (gc.flags & GRID_FLAG_PADDING)
5252 break;
5253 if (format_is_word_separator(ws, &gc)) {
5254 found = 1;
5255 break;
5258 if (x == 0) {
5259 if (y == 0)
5260 break;
5261 gl = grid_peek_line(gd, y - 1);
5262 if (~gl->flags & GRID_LINE_WRAPPED)
5263 break;
5264 y--;
5265 x = grid_line_length(gd, y);
5266 if (x == 0)
5267 break;
5269 x--;
5271 for (;;) {
5272 if (found) {
5273 end = grid_line_length(gd, y);
5274 if (end == 0 || x == end - 1) {
5275 if (y == gd->hsize + gd->sy - 1)
5276 break;
5277 gl = grid_peek_line(gd, y);
5278 if (~gl->flags & GRID_LINE_WRAPPED)
5279 break;
5280 y++;
5281 x = 0;
5282 } else
5283 x++;
5285 found = 1;
5287 grid_get_cell(gd, x, y, &gc);
5288 if (gc.flags & GRID_FLAG_PADDING)
5289 break;
5290 if (format_is_word_separator(ws, &gc))
5291 break;
5293 ud = xreallocarray(ud, size + 2, sizeof *ud);
5294 memcpy(&ud[size++], &gc.data, sizeof *ud);
5296 if (size != 0) {
5297 ud[size].size = 0;
5298 s = utf8_tocstr(ud);
5299 free(ud);
5301 return (s);
5304 /* Return line at given coordinates. Caller frees. */
5305 char *
5306 format_grid_line(struct grid *gd, u_int y)
5308 struct grid_cell gc;
5309 struct utf8_data *ud = NULL;
5310 u_int x;
5311 size_t size = 0;
5312 char *s = NULL;
5314 for (x = 0; x < grid_line_length(gd, y); x++) {
5315 grid_get_cell(gd, x, y, &gc);
5316 if (gc.flags & GRID_FLAG_PADDING)
5317 continue;
5319 ud = xreallocarray(ud, size + 2, sizeof *ud);
5320 if (gc.flags & GRID_FLAG_TAB)
5321 utf8_set(&ud[size++], '\t');
5322 else
5323 memcpy(&ud[size++], &gc.data, sizeof *ud);
5325 if (size != 0) {
5326 ud[size].size = 0;
5327 s = utf8_tocstr(ud);
5328 free(ud);
5330 return (s);
5333 /* Return hyperlink at given coordinates. Caller frees. */
5334 char *
5335 format_grid_hyperlink(struct grid *gd, u_int x, u_int y, struct screen* s)
5337 const char *uri;
5338 struct grid_cell gc;
5340 grid_get_cell(gd, x, y, &gc);
5341 if (gc.flags & GRID_FLAG_PADDING)
5342 return (NULL);
5343 if (s->hyperlinks == NULL || gc.link == 0)
5344 return (NULL);
5345 if (!hyperlinks_get(s->hyperlinks, gc.link, &uri, NULL, NULL))
5346 return (NULL);
5347 return (xstrdup(uri));