Merge branch 'obsd-master'
[tmux.git] / format.c
blobd9da0fd741d7e1ed4bc3aa906ef358608e0f0af3
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 if (ft->wl != NULL) {
2498 if (session_is_linked(ft->wl->session, ft->wl->window))
2499 return (xstrdup("1"));
2500 return (xstrdup("0"));
2502 return (NULL);
2505 /* Callback for window_linked_sessions. */
2506 static void *
2507 format_cb_window_linked_sessions(struct format_tree *ft)
2509 if (ft->wl != NULL)
2510 return (format_printf("%u", ft->wl->window->references));
2511 return (NULL);
2514 /* Callback for window_marked_flag. */
2515 static void *
2516 format_cb_window_marked_flag(struct format_tree *ft)
2518 if (ft->wl != NULL) {
2519 if (server_check_marked() && marked_pane.wl == ft->wl)
2520 return (xstrdup("1"));
2521 return (xstrdup("0"));
2523 return (NULL);
2526 /* Callback for window_name. */
2527 static void *
2528 format_cb_window_name(struct format_tree *ft)
2530 if (ft->w != NULL)
2531 return (format_printf("%s", ft->w->name));
2532 return (NULL);
2535 /* Callback for window_offset_x. */
2536 static void *
2537 format_cb_window_offset_x(struct format_tree *ft)
2539 u_int ox, oy, sx, sy;
2541 if (ft->c != NULL) {
2542 if (tty_window_offset(&ft->c->tty, &ox, &oy, &sx, &sy))
2543 return (format_printf("%u", ox));
2544 return (NULL);
2546 return (NULL);
2549 /* Callback for window_offset_y. */
2550 static void *
2551 format_cb_window_offset_y(struct format_tree *ft)
2553 u_int ox, oy, sx, sy;
2555 if (ft->c != NULL) {
2556 if (tty_window_offset(&ft->c->tty, &ox, &oy, &sx, &sy))
2557 return (format_printf("%u", oy));
2558 return (NULL);
2560 return (NULL);
2563 /* Callback for window_panes. */
2564 static void *
2565 format_cb_window_panes(struct format_tree *ft)
2567 if (ft->w != NULL)
2568 return (format_printf("%u", window_count_panes(ft->w)));
2569 return (NULL);
2572 /* Callback for window_raw_flags. */
2573 static void *
2574 format_cb_window_raw_flags(struct format_tree *ft)
2576 if (ft->wl != NULL)
2577 return (xstrdup(window_printable_flags(ft->wl, 0)));
2578 return (NULL);
2581 /* Callback for window_silence_flag. */
2582 static void *
2583 format_cb_window_silence_flag(struct format_tree *ft)
2585 if (ft->wl != NULL) {
2586 if (ft->wl->flags & WINLINK_SILENCE)
2587 return (xstrdup("1"));
2588 return (xstrdup("0"));
2590 return (NULL);
2593 /* Callback for window_start_flag. */
2594 static void *
2595 format_cb_window_start_flag(struct format_tree *ft)
2597 if (ft->wl != NULL) {
2598 if (ft->wl == RB_MIN(winlinks, &ft->wl->session->windows))
2599 return (xstrdup("1"));
2600 return (xstrdup("0"));
2602 return (NULL);
2605 /* Callback for window_width. */
2606 static void *
2607 format_cb_window_width(struct format_tree *ft)
2609 if (ft->w != NULL)
2610 return (format_printf("%u", ft->w->sx));
2611 return (NULL);
2614 /* Callback for window_zoomed_flag. */
2615 static void *
2616 format_cb_window_zoomed_flag(struct format_tree *ft)
2618 if (ft->w != NULL) {
2619 if (ft->w->flags & WINDOW_ZOOMED)
2620 return (xstrdup("1"));
2621 return (xstrdup("0"));
2623 return (NULL);
2626 /* Callback for wrap_flag. */
2627 static void *
2628 format_cb_wrap_flag(struct format_tree *ft)
2630 if (ft->wp != NULL) {
2631 if (ft->wp->base.mode & MODE_WRAP)
2632 return (xstrdup("1"));
2633 return (xstrdup("0"));
2635 return (NULL);
2638 /* Callback for buffer_created. */
2639 static void *
2640 format_cb_buffer_created(struct format_tree *ft)
2642 static struct timeval tv;
2644 if (ft->pb != NULL) {
2645 timerclear(&tv);
2646 tv.tv_sec = paste_buffer_created(ft->pb);
2647 return (&tv);
2649 return (NULL);
2652 /* Callback for client_activity. */
2653 static void *
2654 format_cb_client_activity(struct format_tree *ft)
2656 if (ft->c != NULL)
2657 return (&ft->c->activity_time);
2658 return (NULL);
2661 /* Callback for client_created. */
2662 static void *
2663 format_cb_client_created(struct format_tree *ft)
2665 if (ft->c != NULL)
2666 return (&ft->c->creation_time);
2667 return (NULL);
2670 /* Callback for session_activity. */
2671 static void *
2672 format_cb_session_activity(struct format_tree *ft)
2674 if (ft->s != NULL)
2675 return (&ft->s->activity_time);
2676 return (NULL);
2679 /* Callback for session_created. */
2680 static void *
2681 format_cb_session_created(struct format_tree *ft)
2683 if (ft->s != NULL)
2684 return (&ft->s->creation_time);
2685 return (NULL);
2688 /* Callback for session_last_attached. */
2689 static void *
2690 format_cb_session_last_attached(struct format_tree *ft)
2692 if (ft->s != NULL)
2693 return (&ft->s->last_attached_time);
2694 return (NULL);
2697 /* Callback for start_time. */
2698 static void *
2699 format_cb_start_time(__unused struct format_tree *ft)
2701 return (&start_time);
2704 /* Callback for window_activity. */
2705 static void *
2706 format_cb_window_activity(struct format_tree *ft)
2708 if (ft->w != NULL)
2709 return (&ft->w->activity_time);
2710 return (NULL);
2713 /* Callback for buffer_mode_format, */
2714 static void *
2715 format_cb_buffer_mode_format(__unused struct format_tree *ft)
2717 return (xstrdup(window_buffer_mode.default_format));
2720 /* Callback for client_mode_format, */
2721 static void *
2722 format_cb_client_mode_format(__unused struct format_tree *ft)
2724 return (xstrdup(window_client_mode.default_format));
2727 /* Callback for tree_mode_format, */
2728 static void *
2729 format_cb_tree_mode_format(__unused struct format_tree *ft)
2731 return (xstrdup(window_tree_mode.default_format));
2734 /* Callback for uid. */
2735 static void *
2736 format_cb_uid(__unused struct format_tree *ft)
2738 return (format_printf("%ld", (long)getuid()));
2741 /* Callback for user. */
2742 static void *
2743 format_cb_user(__unused struct format_tree *ft)
2745 struct passwd *pw;
2747 if ((pw = getpwuid(getuid())) != NULL)
2748 return (xstrdup(pw->pw_name));
2749 return (NULL);
2752 /* Format table type. */
2753 enum format_table_type {
2754 FORMAT_TABLE_STRING,
2755 FORMAT_TABLE_TIME
2758 /* Format table entry. */
2759 struct format_table_entry {
2760 const char *key;
2761 enum format_table_type type;
2762 format_cb cb;
2766 * Format table. Default format variables (that are almost always in the tree
2767 * and where the value is expanded by a callback in this file) are listed here.
2768 * Only variables which are added by the caller go into the tree.
2770 static const struct format_table_entry format_table[] = {
2771 { "active_window_index", FORMAT_TABLE_STRING,
2772 format_cb_active_window_index
2774 { "alternate_on", FORMAT_TABLE_STRING,
2775 format_cb_alternate_on
2777 { "alternate_saved_x", FORMAT_TABLE_STRING,
2778 format_cb_alternate_saved_x
2780 { "alternate_saved_y", FORMAT_TABLE_STRING,
2781 format_cb_alternate_saved_y
2783 { "buffer_created", FORMAT_TABLE_TIME,
2784 format_cb_buffer_created
2786 { "buffer_mode_format", FORMAT_TABLE_STRING,
2787 format_cb_buffer_mode_format
2789 { "buffer_name", FORMAT_TABLE_STRING,
2790 format_cb_buffer_name
2792 { "buffer_sample", FORMAT_TABLE_STRING,
2793 format_cb_buffer_sample
2795 { "buffer_size", FORMAT_TABLE_STRING,
2796 format_cb_buffer_size
2798 { "client_activity", FORMAT_TABLE_TIME,
2799 format_cb_client_activity
2801 { "client_cell_height", FORMAT_TABLE_STRING,
2802 format_cb_client_cell_height
2804 { "client_cell_width", FORMAT_TABLE_STRING,
2805 format_cb_client_cell_width
2807 { "client_control_mode", FORMAT_TABLE_STRING,
2808 format_cb_client_control_mode
2810 { "client_created", FORMAT_TABLE_TIME,
2811 format_cb_client_created
2813 { "client_discarded", FORMAT_TABLE_STRING,
2814 format_cb_client_discarded
2816 { "client_flags", FORMAT_TABLE_STRING,
2817 format_cb_client_flags
2819 { "client_height", FORMAT_TABLE_STRING,
2820 format_cb_client_height
2822 { "client_key_table", FORMAT_TABLE_STRING,
2823 format_cb_client_key_table
2825 { "client_last_session", FORMAT_TABLE_STRING,
2826 format_cb_client_last_session
2828 { "client_mode_format", FORMAT_TABLE_STRING,
2829 format_cb_client_mode_format
2831 { "client_name", FORMAT_TABLE_STRING,
2832 format_cb_client_name
2834 { "client_pid", FORMAT_TABLE_STRING,
2835 format_cb_client_pid
2837 { "client_prefix", FORMAT_TABLE_STRING,
2838 format_cb_client_prefix
2840 { "client_readonly", FORMAT_TABLE_STRING,
2841 format_cb_client_readonly
2843 { "client_session", FORMAT_TABLE_STRING,
2844 format_cb_client_session
2846 { "client_termfeatures", FORMAT_TABLE_STRING,
2847 format_cb_client_termfeatures
2849 { "client_termname", FORMAT_TABLE_STRING,
2850 format_cb_client_termname
2852 { "client_termtype", FORMAT_TABLE_STRING,
2853 format_cb_client_termtype
2855 { "client_tty", FORMAT_TABLE_STRING,
2856 format_cb_client_tty
2858 { "client_uid", FORMAT_TABLE_STRING,
2859 format_cb_client_uid
2861 { "client_user", FORMAT_TABLE_STRING,
2862 format_cb_client_user
2864 { "client_utf8", FORMAT_TABLE_STRING,
2865 format_cb_client_utf8
2867 { "client_width", FORMAT_TABLE_STRING,
2868 format_cb_client_width
2870 { "client_written", FORMAT_TABLE_STRING,
2871 format_cb_client_written
2873 { "config_files", FORMAT_TABLE_STRING,
2874 format_cb_config_files
2876 { "cursor_character", FORMAT_TABLE_STRING,
2877 format_cb_cursor_character
2879 { "cursor_flag", FORMAT_TABLE_STRING,
2880 format_cb_cursor_flag
2882 { "cursor_x", FORMAT_TABLE_STRING,
2883 format_cb_cursor_x
2885 { "cursor_y", FORMAT_TABLE_STRING,
2886 format_cb_cursor_y
2888 { "history_all_bytes", FORMAT_TABLE_STRING,
2889 format_cb_history_all_bytes
2891 { "history_bytes", FORMAT_TABLE_STRING,
2892 format_cb_history_bytes
2894 { "history_limit", FORMAT_TABLE_STRING,
2895 format_cb_history_limit
2897 { "history_size", FORMAT_TABLE_STRING,
2898 format_cb_history_size
2900 { "host", FORMAT_TABLE_STRING,
2901 format_cb_host
2903 { "host_short", FORMAT_TABLE_STRING,
2904 format_cb_host_short
2906 { "insert_flag", FORMAT_TABLE_STRING,
2907 format_cb_insert_flag
2909 { "keypad_cursor_flag", FORMAT_TABLE_STRING,
2910 format_cb_keypad_cursor_flag
2912 { "keypad_flag", FORMAT_TABLE_STRING,
2913 format_cb_keypad_flag
2915 { "last_window_index", FORMAT_TABLE_STRING,
2916 format_cb_last_window_index
2918 { "mouse_all_flag", FORMAT_TABLE_STRING,
2919 format_cb_mouse_all_flag
2921 { "mouse_any_flag", FORMAT_TABLE_STRING,
2922 format_cb_mouse_any_flag
2924 { "mouse_button_flag", FORMAT_TABLE_STRING,
2925 format_cb_mouse_button_flag
2927 { "mouse_hyperlink", FORMAT_TABLE_STRING,
2928 format_cb_mouse_hyperlink
2930 { "mouse_line", FORMAT_TABLE_STRING,
2931 format_cb_mouse_line
2933 { "mouse_pane", FORMAT_TABLE_STRING,
2934 format_cb_mouse_pane
2936 { "mouse_sgr_flag", FORMAT_TABLE_STRING,
2937 format_cb_mouse_sgr_flag
2939 { "mouse_standard_flag", FORMAT_TABLE_STRING,
2940 format_cb_mouse_standard_flag
2942 { "mouse_status_line", FORMAT_TABLE_STRING,
2943 format_cb_mouse_status_line
2945 { "mouse_status_range", FORMAT_TABLE_STRING,
2946 format_cb_mouse_status_range
2948 { "mouse_utf8_flag", FORMAT_TABLE_STRING,
2949 format_cb_mouse_utf8_flag
2951 { "mouse_word", FORMAT_TABLE_STRING,
2952 format_cb_mouse_word
2954 { "mouse_x", FORMAT_TABLE_STRING,
2955 format_cb_mouse_x
2957 { "mouse_y", FORMAT_TABLE_STRING,
2958 format_cb_mouse_y
2960 { "next_session_id", FORMAT_TABLE_STRING,
2961 format_cb_next_session_id
2963 { "origin_flag", FORMAT_TABLE_STRING,
2964 format_cb_origin_flag
2966 { "pane_active", FORMAT_TABLE_STRING,
2967 format_cb_pane_active
2969 { "pane_at_bottom", FORMAT_TABLE_STRING,
2970 format_cb_pane_at_bottom
2972 { "pane_at_left", FORMAT_TABLE_STRING,
2973 format_cb_pane_at_left
2975 { "pane_at_right", FORMAT_TABLE_STRING,
2976 format_cb_pane_at_right
2978 { "pane_at_top", FORMAT_TABLE_STRING,
2979 format_cb_pane_at_top
2981 { "pane_bg", FORMAT_TABLE_STRING,
2982 format_cb_pane_bg
2984 { "pane_bottom", FORMAT_TABLE_STRING,
2985 format_cb_pane_bottom
2987 { "pane_current_command", FORMAT_TABLE_STRING,
2988 format_cb_current_command
2990 { "pane_current_path", FORMAT_TABLE_STRING,
2991 format_cb_current_path
2993 { "pane_dead", FORMAT_TABLE_STRING,
2994 format_cb_pane_dead
2996 { "pane_dead_signal", FORMAT_TABLE_STRING,
2997 format_cb_pane_dead_signal
2999 { "pane_dead_status", FORMAT_TABLE_STRING,
3000 format_cb_pane_dead_status
3002 { "pane_dead_time", FORMAT_TABLE_TIME,
3003 format_cb_pane_dead_time
3005 { "pane_fg", FORMAT_TABLE_STRING,
3006 format_cb_pane_fg
3008 { "pane_format", FORMAT_TABLE_STRING,
3009 format_cb_pane_format
3011 { "pane_height", FORMAT_TABLE_STRING,
3012 format_cb_pane_height
3014 { "pane_id", FORMAT_TABLE_STRING,
3015 format_cb_pane_id
3017 { "pane_in_mode", FORMAT_TABLE_STRING,
3018 format_cb_pane_in_mode
3020 { "pane_index", FORMAT_TABLE_STRING,
3021 format_cb_pane_index
3023 { "pane_input_off", FORMAT_TABLE_STRING,
3024 format_cb_pane_input_off
3026 { "pane_key_mode", FORMAT_TABLE_STRING,
3027 format_cb_pane_key_mode
3029 { "pane_last", FORMAT_TABLE_STRING,
3030 format_cb_pane_last
3032 { "pane_left", FORMAT_TABLE_STRING,
3033 format_cb_pane_left
3035 { "pane_marked", FORMAT_TABLE_STRING,
3036 format_cb_pane_marked
3038 { "pane_marked_set", FORMAT_TABLE_STRING,
3039 format_cb_pane_marked_set
3041 { "pane_mode", FORMAT_TABLE_STRING,
3042 format_cb_pane_mode
3044 { "pane_path", FORMAT_TABLE_STRING,
3045 format_cb_pane_path
3047 { "pane_pid", FORMAT_TABLE_STRING,
3048 format_cb_pane_pid
3050 { "pane_pipe", FORMAT_TABLE_STRING,
3051 format_cb_pane_pipe
3053 { "pane_right", FORMAT_TABLE_STRING,
3054 format_cb_pane_right
3056 { "pane_search_string", FORMAT_TABLE_STRING,
3057 format_cb_pane_search_string
3059 { "pane_start_command", FORMAT_TABLE_STRING,
3060 format_cb_start_command
3062 { "pane_start_path", FORMAT_TABLE_STRING,
3063 format_cb_start_path
3065 { "pane_synchronized", FORMAT_TABLE_STRING,
3066 format_cb_pane_synchronized
3068 { "pane_tabs", FORMAT_TABLE_STRING,
3069 format_cb_pane_tabs
3071 { "pane_title", FORMAT_TABLE_STRING,
3072 format_cb_pane_title
3074 { "pane_top", FORMAT_TABLE_STRING,
3075 format_cb_pane_top
3077 { "pane_tty", FORMAT_TABLE_STRING,
3078 format_cb_pane_tty
3080 { "pane_unseen_changes", FORMAT_TABLE_STRING,
3081 format_cb_pane_unseen_changes
3083 { "pane_width", FORMAT_TABLE_STRING,
3084 format_cb_pane_width
3086 { "pid", FORMAT_TABLE_STRING,
3087 format_cb_pid
3089 { "scroll_region_lower", FORMAT_TABLE_STRING,
3090 format_cb_scroll_region_lower
3092 { "scroll_region_upper", FORMAT_TABLE_STRING,
3093 format_cb_scroll_region_upper
3095 { "server_sessions", FORMAT_TABLE_STRING,
3096 format_cb_server_sessions
3098 { "session_activity", FORMAT_TABLE_TIME,
3099 format_cb_session_activity
3101 { "session_alerts", FORMAT_TABLE_STRING,
3102 format_cb_session_alerts
3104 { "session_attached", FORMAT_TABLE_STRING,
3105 format_cb_session_attached
3107 { "session_attached_list", FORMAT_TABLE_STRING,
3108 format_cb_session_attached_list
3110 { "session_created", FORMAT_TABLE_TIME,
3111 format_cb_session_created
3113 { "session_format", FORMAT_TABLE_STRING,
3114 format_cb_session_format
3116 { "session_group", FORMAT_TABLE_STRING,
3117 format_cb_session_group
3119 { "session_group_attached", FORMAT_TABLE_STRING,
3120 format_cb_session_group_attached
3122 { "session_group_attached_list", FORMAT_TABLE_STRING,
3123 format_cb_session_group_attached_list
3125 { "session_group_list", FORMAT_TABLE_STRING,
3126 format_cb_session_group_list
3128 { "session_group_many_attached", FORMAT_TABLE_STRING,
3129 format_cb_session_group_many_attached
3131 { "session_group_size", FORMAT_TABLE_STRING,
3132 format_cb_session_group_size
3134 { "session_grouped", FORMAT_TABLE_STRING,
3135 format_cb_session_grouped
3137 { "session_id", FORMAT_TABLE_STRING,
3138 format_cb_session_id
3140 { "session_last_attached", FORMAT_TABLE_TIME,
3141 format_cb_session_last_attached
3143 { "session_many_attached", FORMAT_TABLE_STRING,
3144 format_cb_session_many_attached
3146 { "session_marked", FORMAT_TABLE_STRING,
3147 format_cb_session_marked,
3149 { "session_name", FORMAT_TABLE_STRING,
3150 format_cb_session_name
3152 { "session_path", FORMAT_TABLE_STRING,
3153 format_cb_session_path
3155 { "session_stack", FORMAT_TABLE_STRING,
3156 format_cb_session_stack
3158 { "session_windows", FORMAT_TABLE_STRING,
3159 format_cb_session_windows
3161 { "sixel_support", FORMAT_TABLE_STRING,
3162 format_cb_sixel_support
3164 { "socket_path", FORMAT_TABLE_STRING,
3165 format_cb_socket_path
3167 { "start_time", FORMAT_TABLE_TIME,
3168 format_cb_start_time
3170 { "tree_mode_format", FORMAT_TABLE_STRING,
3171 format_cb_tree_mode_format
3173 { "uid", FORMAT_TABLE_STRING,
3174 format_cb_uid
3176 { "user", FORMAT_TABLE_STRING,
3177 format_cb_user
3179 { "version", FORMAT_TABLE_STRING,
3180 format_cb_version
3182 { "window_active", FORMAT_TABLE_STRING,
3183 format_cb_window_active
3185 { "window_active_clients", FORMAT_TABLE_STRING,
3186 format_cb_window_active_clients
3188 { "window_active_clients_list", FORMAT_TABLE_STRING,
3189 format_cb_window_active_clients_list
3191 { "window_active_sessions", FORMAT_TABLE_STRING,
3192 format_cb_window_active_sessions
3194 { "window_active_sessions_list", FORMAT_TABLE_STRING,
3195 format_cb_window_active_sessions_list
3197 { "window_activity", FORMAT_TABLE_TIME,
3198 format_cb_window_activity
3200 { "window_activity_flag", FORMAT_TABLE_STRING,
3201 format_cb_window_activity_flag
3203 { "window_bell_flag", FORMAT_TABLE_STRING,
3204 format_cb_window_bell_flag
3206 { "window_bigger", FORMAT_TABLE_STRING,
3207 format_cb_window_bigger
3209 { "window_cell_height", FORMAT_TABLE_STRING,
3210 format_cb_window_cell_height
3212 { "window_cell_width", FORMAT_TABLE_STRING,
3213 format_cb_window_cell_width
3215 { "window_end_flag", FORMAT_TABLE_STRING,
3216 format_cb_window_end_flag
3218 { "window_flags", FORMAT_TABLE_STRING,
3219 format_cb_window_flags
3221 { "window_format", FORMAT_TABLE_STRING,
3222 format_cb_window_format
3224 { "window_height", FORMAT_TABLE_STRING,
3225 format_cb_window_height
3227 { "window_id", FORMAT_TABLE_STRING,
3228 format_cb_window_id
3230 { "window_index", FORMAT_TABLE_STRING,
3231 format_cb_window_index
3233 { "window_last_flag", FORMAT_TABLE_STRING,
3234 format_cb_window_last_flag
3236 { "window_layout", FORMAT_TABLE_STRING,
3237 format_cb_window_layout
3239 { "window_linked", FORMAT_TABLE_STRING,
3240 format_cb_window_linked
3242 { "window_linked_sessions", FORMAT_TABLE_STRING,
3243 format_cb_window_linked_sessions
3245 { "window_linked_sessions_list", FORMAT_TABLE_STRING,
3246 format_cb_window_linked_sessions_list
3248 { "window_marked_flag", FORMAT_TABLE_STRING,
3249 format_cb_window_marked_flag
3251 { "window_name", FORMAT_TABLE_STRING,
3252 format_cb_window_name
3254 { "window_offset_x", FORMAT_TABLE_STRING,
3255 format_cb_window_offset_x
3257 { "window_offset_y", FORMAT_TABLE_STRING,
3258 format_cb_window_offset_y
3260 { "window_panes", FORMAT_TABLE_STRING,
3261 format_cb_window_panes
3263 { "window_raw_flags", FORMAT_TABLE_STRING,
3264 format_cb_window_raw_flags
3266 { "window_silence_flag", FORMAT_TABLE_STRING,
3267 format_cb_window_silence_flag
3269 { "window_stack_index", FORMAT_TABLE_STRING,
3270 format_cb_window_stack_index
3272 { "window_start_flag", FORMAT_TABLE_STRING,
3273 format_cb_window_start_flag
3275 { "window_visible_layout", FORMAT_TABLE_STRING,
3276 format_cb_window_visible_layout
3278 { "window_width", FORMAT_TABLE_STRING,
3279 format_cb_window_width
3281 { "window_zoomed_flag", FORMAT_TABLE_STRING,
3282 format_cb_window_zoomed_flag
3284 { "wrap_flag", FORMAT_TABLE_STRING,
3285 format_cb_wrap_flag
3289 /* Compare format table entries. */
3290 static int
3291 format_table_compare(const void *key0, const void *entry0)
3293 const char *key = key0;
3294 const struct format_table_entry *entry = entry0;
3296 return (strcmp(key, entry->key));
3299 /* Get a format callback. */
3300 static struct format_table_entry *
3301 format_table_get(const char *key)
3303 return (bsearch(key, format_table, nitems(format_table),
3304 sizeof *format_table, format_table_compare));
3307 /* Merge one format tree into another. */
3308 void
3309 format_merge(struct format_tree *ft, struct format_tree *from)
3311 struct format_entry *fe;
3313 RB_FOREACH(fe, format_entry_tree, &from->tree) {
3314 if (fe->value != NULL)
3315 format_add(ft, fe->key, "%s", fe->value);
3319 /* Get format pane. */
3320 struct window_pane *
3321 format_get_pane(struct format_tree *ft)
3323 return (ft->wp);
3326 /* Add item bits to tree. */
3327 static void
3328 format_create_add_item(struct format_tree *ft, struct cmdq_item *item)
3330 struct key_event *event = cmdq_get_event(item);
3331 struct mouse_event *m = &event->m;
3333 cmdq_merge_formats(item, ft);
3334 memcpy(&ft->m, m, sizeof ft->m);
3337 /* Create a new tree. */
3338 struct format_tree *
3339 format_create(struct client *c, struct cmdq_item *item, int tag, int flags)
3341 struct format_tree *ft;
3343 ft = xcalloc(1, sizeof *ft);
3344 RB_INIT(&ft->tree);
3346 if (c != NULL) {
3347 ft->client = c;
3348 ft->client->references++;
3350 ft->item = item;
3352 ft->tag = tag;
3353 ft->flags = flags;
3355 if (item != NULL)
3356 format_create_add_item(ft, item);
3358 return (ft);
3361 /* Free a tree. */
3362 void
3363 format_free(struct format_tree *ft)
3365 struct format_entry *fe, *fe1;
3367 RB_FOREACH_SAFE(fe, format_entry_tree, &ft->tree, fe1) {
3368 RB_REMOVE(format_entry_tree, &ft->tree, fe);
3369 free(fe->value);
3370 free(fe->key);
3371 free(fe);
3374 if (ft->client != NULL)
3375 server_client_unref(ft->client);
3376 free(ft);
3379 /* Log each format. */
3380 static void
3381 format_log_debug_cb(const char *key, const char *value, void *arg)
3383 const char *prefix = arg;
3385 log_debug("%s: %s=%s", prefix, key, value);
3388 /* Log a format tree. */
3389 void
3390 format_log_debug(struct format_tree *ft, const char *prefix)
3392 format_each(ft, format_log_debug_cb, (void *)prefix);
3395 /* Walk each format. */
3396 void
3397 format_each(struct format_tree *ft, void (*cb)(const char *, const char *,
3398 void *), void *arg)
3400 const struct format_table_entry *fte;
3401 struct format_entry *fe;
3402 u_int i;
3403 char s[64];
3404 void *value;
3405 struct timeval *tv;
3407 for (i = 0; i < nitems(format_table); i++) {
3408 fte = &format_table[i];
3410 value = fte->cb(ft);
3411 if (value == NULL)
3412 continue;
3413 if (fte->type == FORMAT_TABLE_TIME) {
3414 tv = value;
3415 xsnprintf(s, sizeof s, "%lld", (long long)tv->tv_sec);
3416 cb(fte->key, s, arg);
3417 } else {
3418 cb(fte->key, value, arg);
3419 free(value);
3422 RB_FOREACH(fe, format_entry_tree, &ft->tree) {
3423 if (fe->time != 0) {
3424 xsnprintf(s, sizeof s, "%lld", (long long)fe->time);
3425 cb(fe->key, s, arg);
3426 } else {
3427 if (fe->value == NULL && fe->cb != NULL) {
3428 fe->value = fe->cb(ft);
3429 if (fe->value == NULL)
3430 fe->value = xstrdup("");
3432 cb(fe->key, fe->value, arg);
3437 /* Add a key-value pair. */
3438 void
3439 format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
3441 struct format_entry *fe;
3442 struct format_entry *fe_now;
3443 va_list ap;
3445 fe = xmalloc(sizeof *fe);
3446 fe->key = xstrdup(key);
3448 fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
3449 if (fe_now != NULL) {
3450 free(fe->key);
3451 free(fe);
3452 free(fe_now->value);
3453 fe = fe_now;
3456 fe->cb = NULL;
3457 fe->time = 0;
3459 va_start(ap, fmt);
3460 xvasprintf(&fe->value, fmt, ap);
3461 va_end(ap);
3464 /* Add a key and time. */
3465 void
3466 format_add_tv(struct format_tree *ft, const char *key, struct timeval *tv)
3468 struct format_entry *fe, *fe_now;
3470 fe = xmalloc(sizeof *fe);
3471 fe->key = xstrdup(key);
3473 fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
3474 if (fe_now != NULL) {
3475 free(fe->key);
3476 free(fe);
3477 free(fe_now->value);
3478 fe = fe_now;
3481 fe->cb = NULL;
3482 fe->time = tv->tv_sec;
3484 fe->value = NULL;
3487 /* Add a key and function. */
3488 void
3489 format_add_cb(struct format_tree *ft, const char *key, format_cb cb)
3491 struct format_entry *fe;
3492 struct format_entry *fe_now;
3494 fe = xmalloc(sizeof *fe);
3495 fe->key = xstrdup(key);
3497 fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
3498 if (fe_now != NULL) {
3499 free(fe->key);
3500 free(fe);
3501 free(fe_now->value);
3502 fe = fe_now;
3505 fe->cb = cb;
3506 fe->time = 0;
3508 fe->value = NULL;
3511 /* Quote shell special characters in string. */
3512 static char *
3513 format_quote_shell(const char *s)
3515 const char *cp;
3516 char *out, *at;
3518 at = out = xmalloc(strlen(s) * 2 + 1);
3519 for (cp = s; *cp != '\0'; cp++) {
3520 if (strchr("|&;<>()$`\\\"'*?[# =%", *cp) != NULL)
3521 *at++ = '\\';
3522 *at++ = *cp;
3524 *at = '\0';
3525 return (out);
3528 /* Quote #s in string. */
3529 static char *
3530 format_quote_style(const char *s)
3532 const char *cp;
3533 char *out, *at;
3535 at = out = xmalloc(strlen(s) * 2 + 1);
3536 for (cp = s; *cp != '\0'; cp++) {
3537 if (*cp == '#')
3538 *at++ = '#';
3539 *at++ = *cp;
3541 *at = '\0';
3542 return (out);
3545 /* Make a prettier time. */
3546 char *
3547 format_pretty_time(time_t t, int seconds)
3549 struct tm now_tm, tm;
3550 time_t now, age;
3551 char s[9];
3553 time(&now);
3554 if (now < t)
3555 now = t;
3556 age = now - t;
3558 localtime_r(&now, &now_tm);
3559 localtime_r(&t, &tm);
3561 /* Last 24 hours. */
3562 if (age < 24 * 3600) {
3563 if (seconds)
3564 strftime(s, sizeof s, "%H:%M:%S", &tm);
3565 else
3566 strftime(s, sizeof s, "%H:%M", &tm);
3567 return (xstrdup(s));
3570 /* This month or last 28 days. */
3571 if ((tm.tm_year == now_tm.tm_year && tm.tm_mon == now_tm.tm_mon) ||
3572 age < 28 * 24 * 3600) {
3573 strftime(s, sizeof s, "%a%d", &tm);
3574 return (xstrdup(s));
3577 /* Last 12 months. */
3578 if ((tm.tm_year == now_tm.tm_year && tm.tm_mon < now_tm.tm_mon) ||
3579 (tm.tm_year == now_tm.tm_year - 1 && tm.tm_mon > now_tm.tm_mon)) {
3580 strftime(s, sizeof s, "%d%b", &tm);
3581 return (xstrdup(s));
3584 /* Older than that. */
3585 strftime(s, sizeof s, "%h%y", &tm);
3586 return (xstrdup(s));
3589 /* Find a format entry. */
3590 static char *
3591 format_find(struct format_tree *ft, const char *key, int modifiers,
3592 const char *time_format)
3594 struct format_table_entry *fte;
3595 void *value;
3596 struct format_entry *fe, fe_find;
3597 struct environ_entry *envent;
3598 struct options_entry *o;
3599 int idx;
3600 char *found = NULL, *saved, s[512];
3601 const char *errstr;
3602 time_t t = 0;
3603 struct tm tm;
3605 o = options_parse_get(global_options, key, &idx, 0);
3606 if (o == NULL && ft->wp != NULL)
3607 o = options_parse_get(ft->wp->options, key, &idx, 0);
3608 if (o == NULL && ft->w != NULL)
3609 o = options_parse_get(ft->w->options, key, &idx, 0);
3610 if (o == NULL)
3611 o = options_parse_get(global_w_options, key, &idx, 0);
3612 if (o == NULL && ft->s != NULL)
3613 o = options_parse_get(ft->s->options, key, &idx, 0);
3614 if (o == NULL)
3615 o = options_parse_get(global_s_options, key, &idx, 0);
3616 if (o != NULL) {
3617 found = options_to_string(o, idx, 1);
3618 goto found;
3621 fte = format_table_get(key);
3622 if (fte != NULL) {
3623 value = fte->cb(ft);
3624 if (fte->type == FORMAT_TABLE_TIME && value != NULL)
3625 t = ((struct timeval *)value)->tv_sec;
3626 else
3627 found = value;
3628 goto found;
3630 fe_find.key = (char *)key;
3631 fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find);
3632 if (fe != NULL) {
3633 if (fe->time != 0) {
3634 t = fe->time;
3635 goto found;
3637 if (fe->value == NULL && fe->cb != NULL) {
3638 fe->value = fe->cb(ft);
3639 if (fe->value == NULL)
3640 fe->value = xstrdup("");
3642 found = xstrdup(fe->value);
3643 goto found;
3646 if (~modifiers & FORMAT_TIMESTRING) {
3647 envent = NULL;
3648 if (ft->s != NULL)
3649 envent = environ_find(ft->s->environ, key);
3650 if (envent == NULL)
3651 envent = environ_find(global_environ, key);
3652 if (envent != NULL && envent->value != NULL) {
3653 found = xstrdup(envent->value);
3654 goto found;
3658 return (NULL);
3660 found:
3661 if (modifiers & FORMAT_TIMESTRING) {
3662 if (t == 0 && found != NULL) {
3663 t = strtonum(found, 0, INT64_MAX, &errstr);
3664 if (errstr != NULL)
3665 t = 0;
3666 free(found);
3668 if (t == 0)
3669 return (NULL);
3670 if (modifiers & FORMAT_PRETTY)
3671 found = format_pretty_time(t, 0);
3672 else {
3673 if (time_format != NULL) {
3674 localtime_r(&t, &tm);
3675 strftime(s, sizeof s, time_format, &tm);
3676 } else {
3677 ctime_r(&t, s);
3678 s[strcspn(s, "\n")] = '\0';
3680 found = xstrdup(s);
3682 return (found);
3685 if (t != 0)
3686 xasprintf(&found, "%lld", (long long)t);
3687 else if (found == NULL)
3688 return (NULL);
3689 if (modifiers & FORMAT_BASENAME) {
3690 saved = found;
3691 found = xstrdup(basename(saved));
3692 free(saved);
3694 if (modifiers & FORMAT_DIRNAME) {
3695 saved = found;
3696 found = xstrdup(dirname(saved));
3697 free(saved);
3699 if (modifiers & FORMAT_QUOTE_SHELL) {
3700 saved = found;
3701 found = format_quote_shell(saved);
3702 free(saved);
3704 if (modifiers & FORMAT_QUOTE_STYLE) {
3705 saved = found;
3706 found = format_quote_style(saved);
3707 free(saved);
3709 return (found);
3712 /* Unescape escaped characters. */
3713 static char *
3714 format_unescape(const char *s)
3716 char *out, *cp;
3717 int brackets = 0;
3719 cp = out = xmalloc(strlen(s) + 1);
3720 for (; *s != '\0'; s++) {
3721 if (*s == '#' && s[1] == '{')
3722 brackets++;
3723 if (brackets == 0 &&
3724 *s == '#' &&
3725 strchr(",#{}:", s[1]) != NULL) {
3726 *cp++ = *++s;
3727 continue;
3729 if (*s == '}')
3730 brackets--;
3731 *cp++ = *s;
3733 *cp = '\0';
3734 return (out);
3737 /* Remove escaped characters. */
3738 static char *
3739 format_strip(const char *s)
3741 char *out, *cp;
3742 int brackets = 0;
3744 cp = out = xmalloc(strlen(s) + 1);
3745 for (; *s != '\0'; s++) {
3746 if (*s == '#' && s[1] == '{')
3747 brackets++;
3748 if (*s == '#' && strchr(",#{}:", s[1]) != NULL) {
3749 if (brackets != 0)
3750 *cp++ = *s;
3751 continue;
3753 if (*s == '}')
3754 brackets--;
3755 *cp++ = *s;
3757 *cp = '\0';
3758 return (out);
3761 /* Skip until end. */
3762 const char *
3763 format_skip(const char *s, const char *end)
3765 int brackets = 0;
3767 for (; *s != '\0'; s++) {
3768 if (*s == '#' && s[1] == '{')
3769 brackets++;
3770 if (*s == '#' &&
3771 s[1] != '\0' &&
3772 strchr(",#{}:", s[1]) != NULL) {
3773 s++;
3774 continue;
3776 if (*s == '}')
3777 brackets--;
3778 if (strchr(end, *s) != NULL && brackets == 0)
3779 break;
3781 if (*s == '\0')
3782 return (NULL);
3783 return (s);
3786 /* Return left and right alternatives separated by commas. */
3787 static int
3788 format_choose(struct format_expand_state *es, const char *s, char **left,
3789 char **right, int expand)
3791 const char *cp;
3792 char *left0, *right0;
3794 cp = format_skip(s, ",");
3795 if (cp == NULL)
3796 return (-1);
3797 left0 = xstrndup(s, cp - s);
3798 right0 = xstrdup(cp + 1);
3800 if (expand) {
3801 *left = format_expand1(es, left0);
3802 free(left0);
3803 *right = format_expand1(es, right0);
3804 free(right0);
3805 } else {
3806 *left = left0;
3807 *right = right0;
3809 return (0);
3812 /* Is this true? */
3814 format_true(const char *s)
3816 if (s != NULL && *s != '\0' && (s[0] != '0' || s[1] != '\0'))
3817 return (1);
3818 return (0);
3821 /* Check if modifier end. */
3822 static int
3823 format_is_end(char c)
3825 return (c == ';' || c == ':');
3828 /* Add to modifier list. */
3829 static void
3830 format_add_modifier(struct format_modifier **list, u_int *count,
3831 const char *c, size_t n, char **argv, int argc)
3833 struct format_modifier *fm;
3835 *list = xreallocarray(*list, (*count) + 1, sizeof **list);
3836 fm = &(*list)[(*count)++];
3838 memcpy(fm->modifier, c, n);
3839 fm->modifier[n] = '\0';
3840 fm->size = n;
3842 fm->argv = argv;
3843 fm->argc = argc;
3846 /* Free modifier list. */
3847 static void
3848 format_free_modifiers(struct format_modifier *list, u_int count)
3850 u_int i;
3852 for (i = 0; i < count; i++)
3853 cmd_free_argv(list[i].argc, list[i].argv);
3854 free(list);
3857 /* Build modifier list. */
3858 static struct format_modifier *
3859 format_build_modifiers(struct format_expand_state *es, const char **s,
3860 u_int *count)
3862 const char *cp = *s, *end;
3863 struct format_modifier *list = NULL;
3864 char c, last[] = "X;:", **argv, *value;
3865 int argc;
3868 * Modifiers are a ; separated list of the forms:
3869 * l,m,C,a,b,c,d,n,t,w,q,E,T,S,W,P,<,>
3870 * =a
3871 * =/a
3872 * =/a/
3873 * s/a/b/
3874 * s/a/b
3875 * ||,&&,!=,==,<=,>=
3878 *count = 0;
3880 while (*cp != '\0' && *cp != ':') {
3881 /* Skip any separator character. */
3882 if (*cp == ';')
3883 cp++;
3885 /* Check single character modifiers with no arguments. */
3886 if (strchr("labcdnwETSWPL<>", cp[0]) != NULL &&
3887 format_is_end(cp[1])) {
3888 format_add_modifier(&list, count, cp, 1, NULL, 0);
3889 cp++;
3890 continue;
3893 /* Then try double character with no arguments. */
3894 if ((memcmp("||", cp, 2) == 0 ||
3895 memcmp("&&", cp, 2) == 0 ||
3896 memcmp("!=", cp, 2) == 0 ||
3897 memcmp("==", cp, 2) == 0 ||
3898 memcmp("<=", cp, 2) == 0 ||
3899 memcmp(">=", cp, 2) == 0) &&
3900 format_is_end(cp[2])) {
3901 format_add_modifier(&list, count, cp, 2, NULL, 0);
3902 cp += 2;
3903 continue;
3906 /* Now try single character with arguments. */
3907 if (strchr("mCNst=peq", cp[0]) == NULL)
3908 break;
3909 c = cp[0];
3911 /* No arguments provided. */
3912 if (format_is_end(cp[1])) {
3913 format_add_modifier(&list, count, cp, 1, NULL, 0);
3914 cp++;
3915 continue;
3917 argv = NULL;
3918 argc = 0;
3920 /* Single argument with no wrapper character. */
3921 if (!ispunct((u_char)cp[1]) || cp[1] == '-') {
3922 end = format_skip(cp + 1, ":;");
3923 if (end == NULL)
3924 break;
3926 argv = xcalloc(1, sizeof *argv);
3927 value = xstrndup(cp + 1, end - (cp + 1));
3928 argv[0] = format_expand1(es, value);
3929 free(value);
3930 argc = 1;
3932 format_add_modifier(&list, count, &c, 1, argv, argc);
3933 cp = end;
3934 continue;
3937 /* Multiple arguments with a wrapper character. */
3938 last[0] = cp[1];
3939 cp++;
3940 do {
3941 if (cp[0] == last[0] && format_is_end(cp[1])) {
3942 cp++;
3943 break;
3945 end = format_skip(cp + 1, last);
3946 if (end == NULL)
3947 break;
3948 cp++;
3950 argv = xreallocarray(argv, argc + 1, sizeof *argv);
3951 value = xstrndup(cp, end - cp);
3952 argv[argc++] = format_expand1(es, value);
3953 free(value);
3955 cp = end;
3956 } while (!format_is_end(cp[0]));
3957 format_add_modifier(&list, count, &c, 1, argv, argc);
3959 if (*cp != ':') {
3960 format_free_modifiers(list, *count);
3961 *count = 0;
3962 return (NULL);
3964 *s = cp + 1;
3965 return (list);
3968 /* Match against an fnmatch(3) pattern or regular expression. */
3969 static char *
3970 format_match(struct format_modifier *fm, const char *pattern, const char *text)
3972 const char *s = "";
3973 regex_t r;
3974 int flags = 0;
3976 if (fm->argc >= 1)
3977 s = fm->argv[0];
3978 if (strchr(s, 'r') == NULL) {
3979 if (strchr(s, 'i') != NULL)
3980 flags |= FNM_CASEFOLD;
3981 if (fnmatch(pattern, text, flags) != 0)
3982 return (xstrdup("0"));
3983 } else {
3984 flags = REG_EXTENDED|REG_NOSUB;
3985 if (strchr(s, 'i') != NULL)
3986 flags |= REG_ICASE;
3987 if (regcomp(&r, pattern, flags) != 0)
3988 return (xstrdup("0"));
3989 if (regexec(&r, text, 0, NULL, 0) != 0) {
3990 regfree(&r);
3991 return (xstrdup("0"));
3993 regfree(&r);
3995 return (xstrdup("1"));
3998 /* Perform substitution in string. */
3999 static char *
4000 format_sub(struct format_modifier *fm, const char *text, const char *pattern,
4001 const char *with)
4003 char *value;
4004 int flags = REG_EXTENDED;
4006 if (fm->argc >= 3 && strchr(fm->argv[2], 'i') != NULL)
4007 flags |= REG_ICASE;
4008 value = regsub(pattern, with, text, flags);
4009 if (value == NULL)
4010 return (xstrdup(text));
4011 return (value);
4014 /* Search inside pane. */
4015 static char *
4016 format_search(struct format_modifier *fm, struct window_pane *wp, const char *s)
4018 int ignore = 0, regex = 0;
4019 char *value;
4021 if (fm->argc >= 1) {
4022 if (strchr(fm->argv[0], 'i') != NULL)
4023 ignore = 1;
4024 if (strchr(fm->argv[0], 'r') != NULL)
4025 regex = 1;
4027 xasprintf(&value, "%u", window_pane_search(wp, s, regex, ignore));
4028 return (value);
4031 /* Does session name exist? */
4032 static char *
4033 format_session_name(struct format_expand_state *es, const char *fmt)
4035 char *name;
4036 struct session *s;
4038 name = format_expand1(es, fmt);
4039 RB_FOREACH(s, sessions, &sessions) {
4040 if (strcmp(s->name, name) == 0) {
4041 free(name);
4042 return (xstrdup("1"));
4045 free(name);
4046 return (xstrdup("0"));
4049 /* Loop over sessions. */
4050 static char *
4051 format_loop_sessions(struct format_expand_state *es, const char *fmt)
4053 struct format_tree *ft = es->ft;
4054 struct client *c = ft->client;
4055 struct cmdq_item *item = ft->item;
4056 struct format_tree *nft;
4057 struct format_expand_state next;
4058 char *expanded, *value;
4059 size_t valuelen;
4060 struct session *s;
4062 value = xcalloc(1, 1);
4063 valuelen = 1;
4065 RB_FOREACH(s, sessions, &sessions) {
4066 format_log(es, "session loop: $%u", s->id);
4067 nft = format_create(c, item, FORMAT_NONE, ft->flags);
4068 format_defaults(nft, ft->c, s, NULL, NULL);
4069 format_copy_state(&next, es, 0);
4070 next.ft = nft;
4071 expanded = format_expand1(&next, fmt);
4072 format_free(next.ft);
4074 valuelen += strlen(expanded);
4075 value = xrealloc(value, valuelen);
4077 strlcat(value, expanded, valuelen);
4078 free(expanded);
4081 return (value);
4084 /* Does window name exist? */
4085 static char *
4086 format_window_name(struct format_expand_state *es, const char *fmt)
4088 struct format_tree *ft = es->ft;
4089 char *name;
4090 struct winlink *wl;
4092 if (ft->s == NULL) {
4093 format_log(es, "window name but no session");
4094 return (NULL);
4097 name = format_expand1(es, fmt);
4098 RB_FOREACH(wl, winlinks, &ft->s->windows) {
4099 if (strcmp(wl->window->name, name) == 0) {
4100 free(name);
4101 return (xstrdup("1"));
4104 free(name);
4105 return (xstrdup("0"));
4108 /* Loop over windows. */
4109 static char *
4110 format_loop_windows(struct format_expand_state *es, const char *fmt)
4112 struct format_tree *ft = es->ft;
4113 struct client *c = ft->client;
4114 struct cmdq_item *item = ft->item;
4115 struct format_tree *nft;
4116 struct format_expand_state next;
4117 char *all, *active, *use, *expanded, *value;
4118 size_t valuelen;
4119 struct winlink *wl;
4120 struct window *w;
4122 if (ft->s == NULL) {
4123 format_log(es, "window loop but no session");
4124 return (NULL);
4127 if (format_choose(es, fmt, &all, &active, 0) != 0) {
4128 all = xstrdup(fmt);
4129 active = NULL;
4132 value = xcalloc(1, 1);
4133 valuelen = 1;
4135 RB_FOREACH(wl, winlinks, &ft->s->windows) {
4136 w = wl->window;
4137 format_log(es, "window loop: %u @%u", wl->idx, w->id);
4138 if (active != NULL && wl == ft->s->curw)
4139 use = active;
4140 else
4141 use = all;
4142 nft = format_create(c, item, FORMAT_WINDOW|w->id, ft->flags);
4143 format_defaults(nft, ft->c, ft->s, wl, NULL);
4144 format_copy_state(&next, es, 0);
4145 next.ft = nft;
4146 expanded = format_expand1(&next, use);
4147 format_free(nft);
4149 valuelen += strlen(expanded);
4150 value = xrealloc(value, valuelen);
4152 strlcat(value, expanded, valuelen);
4153 free(expanded);
4156 free(active);
4157 free(all);
4159 return (value);
4162 /* Loop over panes. */
4163 static char *
4164 format_loop_panes(struct format_expand_state *es, const char *fmt)
4166 struct format_tree *ft = es->ft;
4167 struct client *c = ft->client;
4168 struct cmdq_item *item = ft->item;
4169 struct format_tree *nft;
4170 struct format_expand_state next;
4171 char *all, *active, *use, *expanded, *value;
4172 size_t valuelen;
4173 struct window_pane *wp;
4175 if (ft->w == NULL) {
4176 format_log(es, "pane loop but no window");
4177 return (NULL);
4180 if (format_choose(es, fmt, &all, &active, 0) != 0) {
4181 all = xstrdup(fmt);
4182 active = NULL;
4185 value = xcalloc(1, 1);
4186 valuelen = 1;
4188 TAILQ_FOREACH(wp, &ft->w->panes, entry) {
4189 format_log(es, "pane loop: %%%u", wp->id);
4190 if (active != NULL && wp == ft->w->active)
4191 use = active;
4192 else
4193 use = all;
4194 nft = format_create(c, item, FORMAT_PANE|wp->id, ft->flags);
4195 format_defaults(nft, ft->c, ft->s, ft->wl, wp);
4196 format_copy_state(&next, es, 0);
4197 next.ft = nft;
4198 expanded = format_expand1(&next, use);
4199 format_free(nft);
4201 valuelen += strlen(expanded);
4202 value = xrealloc(value, valuelen);
4204 strlcat(value, expanded, valuelen);
4205 free(expanded);
4208 free(active);
4209 free(all);
4211 return (value);
4214 /* Loop over clients. */
4215 static char *
4216 format_loop_clients(struct format_expand_state *es, const char *fmt)
4218 struct format_tree *ft = es->ft;
4219 struct client *c;
4220 struct cmdq_item *item = ft->item;
4221 struct format_tree *nft;
4222 struct format_expand_state next;
4223 char *expanded, *value;
4224 size_t valuelen;
4226 value = xcalloc(1, 1);
4227 valuelen = 1;
4229 TAILQ_FOREACH(c, &clients, entry) {
4230 format_log(es, "client loop: %s", c->name);
4231 nft = format_create(c, item, 0, ft->flags);
4232 format_defaults(nft, c, ft->s, ft->wl, ft->wp);
4233 format_copy_state(&next, es, 0);
4234 next.ft = nft;
4235 expanded = format_expand1(&next, fmt);
4236 format_free(nft);
4238 valuelen += strlen(expanded);
4239 value = xrealloc(value, valuelen);
4241 strlcat(value, expanded, valuelen);
4242 free(expanded);
4245 return (value);
4248 static char *
4249 format_replace_expression(struct format_modifier *mexp,
4250 struct format_expand_state *es, const char *copy)
4252 int argc = mexp->argc;
4253 const char *errstr;
4254 char *endch, *value, *left = NULL, *right = NULL;
4255 int use_fp = 0;
4256 u_int prec = 0;
4257 double mleft, mright, result;
4258 enum { ADD,
4259 SUBTRACT,
4260 MULTIPLY,
4261 DIVIDE,
4262 MODULUS,
4263 EQUAL,
4264 NOT_EQUAL,
4265 GREATER_THAN,
4266 GREATER_THAN_EQUAL,
4267 LESS_THAN,
4268 LESS_THAN_EQUAL } operator;
4270 if (strcmp(mexp->argv[0], "+") == 0)
4271 operator = ADD;
4272 else if (strcmp(mexp->argv[0], "-") == 0)
4273 operator = SUBTRACT;
4274 else if (strcmp(mexp->argv[0], "*") == 0)
4275 operator = MULTIPLY;
4276 else if (strcmp(mexp->argv[0], "/") == 0)
4277 operator = DIVIDE;
4278 else if (strcmp(mexp->argv[0], "%") == 0 ||
4279 strcmp(mexp->argv[0], "m") == 0)
4280 operator = MODULUS;
4281 else if (strcmp(mexp->argv[0], "==") == 0)
4282 operator = EQUAL;
4283 else if (strcmp(mexp->argv[0], "!=") == 0)
4284 operator = NOT_EQUAL;
4285 else if (strcmp(mexp->argv[0], ">") == 0)
4286 operator = GREATER_THAN;
4287 else if (strcmp(mexp->argv[0], "<") == 0)
4288 operator = LESS_THAN;
4289 else if (strcmp(mexp->argv[0], ">=") == 0)
4290 operator = GREATER_THAN_EQUAL;
4291 else if (strcmp(mexp->argv[0], "<=") == 0)
4292 operator = LESS_THAN_EQUAL;
4293 else {
4294 format_log(es, "expression has no valid operator: '%s'",
4295 mexp->argv[0]);
4296 goto fail;
4299 /* The second argument may be flags. */
4300 if (argc >= 2 && strchr(mexp->argv[1], 'f') != NULL) {
4301 use_fp = 1;
4302 prec = 2;
4305 /* The third argument may be precision. */
4306 if (argc >= 3) {
4307 prec = strtonum(mexp->argv[2], INT_MIN, INT_MAX, &errstr);
4308 if (errstr != NULL) {
4309 format_log(es, "expression precision %s: %s", errstr,
4310 mexp->argv[2]);
4311 goto fail;
4315 if (format_choose(es, copy, &left, &right, 1) != 0) {
4316 format_log(es, "expression syntax error");
4317 goto fail;
4320 mleft = strtod(left, &endch);
4321 if (*endch != '\0') {
4322 format_log(es, "expression left side is invalid: %s", left);
4323 goto fail;
4326 mright = strtod(right, &endch);
4327 if (*endch != '\0') {
4328 format_log(es, "expression right side is invalid: %s", right);
4329 goto fail;
4332 if (!use_fp) {
4333 mleft = (long long)mleft;
4334 mright = (long long)mright;
4336 format_log(es, "expression left side is: %.*f", prec, mleft);
4337 format_log(es, "expression right side is: %.*f", prec, mright);
4339 switch (operator) {
4340 case ADD:
4341 result = mleft + mright;
4342 break;
4343 case SUBTRACT:
4344 result = mleft - mright;
4345 break;
4346 case MULTIPLY:
4347 result = mleft * mright;
4348 break;
4349 case DIVIDE:
4350 result = mleft / mright;
4351 break;
4352 case MODULUS:
4353 result = fmod(mleft, mright);
4354 break;
4355 case EQUAL:
4356 result = fabs(mleft - mright) < 1e-9;
4357 break;
4358 case NOT_EQUAL:
4359 result = fabs(mleft - mright) > 1e-9;
4360 break;
4361 case GREATER_THAN:
4362 result = (mleft > mright);
4363 break;
4364 case GREATER_THAN_EQUAL:
4365 result = (mleft >= mright);
4366 break;
4367 case LESS_THAN:
4368 result = (mleft < mright);
4369 break;
4370 case LESS_THAN_EQUAL:
4371 result = (mleft <= mright);
4372 break;
4374 if (use_fp)
4375 xasprintf(&value, "%.*f", prec, result);
4376 else
4377 xasprintf(&value, "%.*f", prec, (double)(long long)result);
4378 format_log(es, "expression result is %s", value);
4380 free(right);
4381 free(left);
4382 return (value);
4384 fail:
4385 free(right);
4386 free(left);
4387 return (NULL);
4390 /* Replace a key. */
4391 static int
4392 format_replace(struct format_expand_state *es, const char *key, size_t keylen,
4393 char **buf, size_t *len, size_t *off)
4395 struct format_tree *ft = es->ft;
4396 struct window_pane *wp = ft->wp;
4397 const char *errstr, *copy, *cp, *marker = NULL;
4398 const char *time_format = NULL;
4399 char *copy0, *condition, *found, *new;
4400 char *value, *left, *right;
4401 size_t valuelen;
4402 int modifiers = 0, limit = 0, width = 0;
4403 int j, c;
4404 struct format_modifier *list, *cmp = NULL, *search = NULL;
4405 struct format_modifier **sub = NULL, *mexp = NULL, *fm;
4406 u_int i, count, nsub = 0;
4407 struct format_expand_state next;
4409 /* Make a copy of the key. */
4410 copy = copy0 = xstrndup(key, keylen);
4412 /* Process modifier list. */
4413 list = format_build_modifiers(es, &copy, &count);
4414 for (i = 0; i < count; i++) {
4415 fm = &list[i];
4416 if (format_logging(ft)) {
4417 format_log(es, "modifier %u is %s", i, fm->modifier);
4418 for (j = 0; j < fm->argc; j++) {
4419 format_log(es, "modifier %u argument %d: %s", i,
4420 j, fm->argv[j]);
4423 if (fm->size == 1) {
4424 switch (fm->modifier[0]) {
4425 case 'm':
4426 case '<':
4427 case '>':
4428 cmp = fm;
4429 break;
4430 case 'C':
4431 search = fm;
4432 break;
4433 case 's':
4434 if (fm->argc < 2)
4435 break;
4436 sub = xreallocarray(sub, nsub + 1, sizeof *sub);
4437 sub[nsub++] = fm;
4438 break;
4439 case '=':
4440 if (fm->argc < 1)
4441 break;
4442 limit = strtonum(fm->argv[0], INT_MIN, INT_MAX,
4443 &errstr);
4444 if (errstr != NULL)
4445 limit = 0;
4446 if (fm->argc >= 2 && fm->argv[1] != NULL)
4447 marker = fm->argv[1];
4448 break;
4449 case 'p':
4450 if (fm->argc < 1)
4451 break;
4452 width = strtonum(fm->argv[0], INT_MIN, INT_MAX,
4453 &errstr);
4454 if (errstr != NULL)
4455 width = 0;
4456 break;
4457 case 'w':
4458 modifiers |= FORMAT_WIDTH;
4459 break;
4460 case 'e':
4461 if (fm->argc < 1 || fm->argc > 3)
4462 break;
4463 mexp = fm;
4464 break;
4465 case 'l':
4466 modifiers |= FORMAT_LITERAL;
4467 break;
4468 case 'a':
4469 modifiers |= FORMAT_CHARACTER;
4470 break;
4471 case 'b':
4472 modifiers |= FORMAT_BASENAME;
4473 break;
4474 case 'c':
4475 modifiers |= FORMAT_COLOUR;
4476 break;
4477 case 'd':
4478 modifiers |= FORMAT_DIRNAME;
4479 break;
4480 case 'n':
4481 modifiers |= FORMAT_LENGTH;
4482 break;
4483 case 't':
4484 modifiers |= FORMAT_TIMESTRING;
4485 if (fm->argc < 1)
4486 break;
4487 if (strchr(fm->argv[0], 'p') != NULL)
4488 modifiers |= FORMAT_PRETTY;
4489 else if (fm->argc >= 2 &&
4490 strchr(fm->argv[0], 'f') != NULL)
4491 time_format = format_strip(fm->argv[1]);
4492 break;
4493 case 'q':
4494 if (fm->argc < 1)
4495 modifiers |= FORMAT_QUOTE_SHELL;
4496 else if (strchr(fm->argv[0], 'e') != NULL ||
4497 strchr(fm->argv[0], 'h') != NULL)
4498 modifiers |= FORMAT_QUOTE_STYLE;
4499 break;
4500 case 'E':
4501 modifiers |= FORMAT_EXPAND;
4502 break;
4503 case 'T':
4504 modifiers |= FORMAT_EXPANDTIME;
4505 break;
4506 case 'N':
4507 if (fm->argc < 1 ||
4508 strchr(fm->argv[0], 'w') != NULL)
4509 modifiers |= FORMAT_WINDOW_NAME;
4510 else if (strchr(fm->argv[0], 's') != NULL)
4511 modifiers |= FORMAT_SESSION_NAME;
4512 break;
4513 case 'S':
4514 modifiers |= FORMAT_SESSIONS;
4515 break;
4516 case 'W':
4517 modifiers |= FORMAT_WINDOWS;
4518 break;
4519 case 'P':
4520 modifiers |= FORMAT_PANES;
4521 break;
4522 case 'L':
4523 modifiers |= FORMAT_CLIENTS;
4524 break;
4526 } else if (fm->size == 2) {
4527 if (strcmp(fm->modifier, "||") == 0 ||
4528 strcmp(fm->modifier, "&&") == 0 ||
4529 strcmp(fm->modifier, "==") == 0 ||
4530 strcmp(fm->modifier, "!=") == 0 ||
4531 strcmp(fm->modifier, ">=") == 0 ||
4532 strcmp(fm->modifier, "<=") == 0)
4533 cmp = fm;
4537 /* Is this a literal string? */
4538 if (modifiers & FORMAT_LITERAL) {
4539 format_log(es, "literal string is '%s'", copy);
4540 value = format_unescape(copy);
4541 goto done;
4544 /* Is this a character? */
4545 if (modifiers & FORMAT_CHARACTER) {
4546 new = format_expand1(es, copy);
4547 c = strtonum(new, 32, 126, &errstr);
4548 if (errstr != NULL)
4549 value = xstrdup("");
4550 else
4551 xasprintf(&value, "%c", c);
4552 free(new);
4553 goto done;
4556 /* Is this a colour? */
4557 if (modifiers & FORMAT_COLOUR) {
4558 new = format_expand1(es, copy);
4559 c = colour_fromstring(new);
4560 if (c == -1 || (c = colour_force_rgb(c)) == -1)
4561 value = xstrdup("");
4562 else
4563 xasprintf(&value, "%06x", c & 0xffffff);
4564 free(new);
4565 goto done;
4568 /* Is this a loop, comparison or condition? */
4569 if (modifiers & FORMAT_SESSIONS) {
4570 value = format_loop_sessions(es, copy);
4571 if (value == NULL)
4572 goto fail;
4573 } else if (modifiers & FORMAT_WINDOWS) {
4574 value = format_loop_windows(es, copy);
4575 if (value == NULL)
4576 goto fail;
4577 } else if (modifiers & FORMAT_PANES) {
4578 value = format_loop_panes(es, copy);
4579 if (value == NULL)
4580 goto fail;
4581 } else if (modifiers & FORMAT_CLIENTS) {
4582 value = format_loop_clients(es, copy);
4583 if (value == NULL)
4584 goto fail;
4585 } else if (modifiers & FORMAT_WINDOW_NAME) {
4586 value = format_window_name(es, copy);
4587 if (value == NULL)
4588 goto fail;
4589 } else if (modifiers & FORMAT_SESSION_NAME) {
4590 value = format_session_name(es, copy);
4591 if (value == NULL)
4592 goto fail;
4593 } else if (search != NULL) {
4594 /* Search in pane. */
4595 new = format_expand1(es, copy);
4596 if (wp == NULL) {
4597 format_log(es, "search '%s' but no pane", new);
4598 value = xstrdup("0");
4599 } else {
4600 format_log(es, "search '%s' pane %%%u", new, wp->id);
4601 value = format_search(search, wp, new);
4603 free(new);
4604 } else if (cmp != NULL) {
4605 /* Comparison of left and right. */
4606 if (format_choose(es, copy, &left, &right, 1) != 0) {
4607 format_log(es, "compare %s syntax error: %s",
4608 cmp->modifier, copy);
4609 goto fail;
4611 format_log(es, "compare %s left is: %s", cmp->modifier, left);
4612 format_log(es, "compare %s right is: %s", cmp->modifier, right);
4614 if (strcmp(cmp->modifier, "||") == 0) {
4615 if (format_true(left) || format_true(right))
4616 value = xstrdup("1");
4617 else
4618 value = xstrdup("0");
4619 } else if (strcmp(cmp->modifier, "&&") == 0) {
4620 if (format_true(left) && format_true(right))
4621 value = xstrdup("1");
4622 else
4623 value = xstrdup("0");
4624 } else if (strcmp(cmp->modifier, "==") == 0) {
4625 if (strcmp(left, right) == 0)
4626 value = xstrdup("1");
4627 else
4628 value = xstrdup("0");
4629 } else if (strcmp(cmp->modifier, "!=") == 0) {
4630 if (strcmp(left, right) != 0)
4631 value = xstrdup("1");
4632 else
4633 value = xstrdup("0");
4634 } else if (strcmp(cmp->modifier, "<") == 0) {
4635 if (strcmp(left, right) < 0)
4636 value = xstrdup("1");
4637 else
4638 value = xstrdup("0");
4639 } else if (strcmp(cmp->modifier, ">") == 0) {
4640 if (strcmp(left, right) > 0)
4641 value = xstrdup("1");
4642 else
4643 value = xstrdup("0");
4644 } else if (strcmp(cmp->modifier, "<=") == 0) {
4645 if (strcmp(left, right) <= 0)
4646 value = xstrdup("1");
4647 else
4648 value = xstrdup("0");
4649 } else if (strcmp(cmp->modifier, ">=") == 0) {
4650 if (strcmp(left, right) >= 0)
4651 value = xstrdup("1");
4652 else
4653 value = xstrdup("0");
4654 } else if (strcmp(cmp->modifier, "m") == 0)
4655 value = format_match(cmp, left, right);
4657 free(right);
4658 free(left);
4659 } else if (*copy == '?') {
4660 /* Conditional: check first and choose second or third. */
4661 cp = format_skip(copy + 1, ",");
4662 if (cp == NULL) {
4663 format_log(es, "condition syntax error: %s", copy + 1);
4664 goto fail;
4666 condition = xstrndup(copy + 1, cp - (copy + 1));
4667 format_log(es, "condition is: %s", condition);
4669 found = format_find(ft, condition, modifiers, time_format);
4670 if (found == NULL) {
4672 * If the condition not found, try to expand it. If
4673 * the expansion doesn't have any effect, then assume
4674 * false.
4676 found = format_expand1(es, condition);
4677 if (strcmp(found, condition) == 0) {
4678 free(found);
4679 found = xstrdup("");
4680 format_log(es,
4681 "condition '%s' not found; assuming false",
4682 condition);
4684 } else {
4685 format_log(es, "condition '%s' found: %s", condition,
4686 found);
4689 if (format_choose(es, cp + 1, &left, &right, 0) != 0) {
4690 format_log(es, "condition '%s' syntax error: %s",
4691 condition, cp + 1);
4692 free(found);
4693 goto fail;
4695 if (format_true(found)) {
4696 format_log(es, "condition '%s' is true", condition);
4697 value = format_expand1(es, left);
4698 } else {
4699 format_log(es, "condition '%s' is false", condition);
4700 value = format_expand1(es, right);
4702 free(right);
4703 free(left);
4705 free(condition);
4706 free(found);
4707 } else if (mexp != NULL) {
4708 value = format_replace_expression(mexp, es, copy);
4709 if (value == NULL)
4710 value = xstrdup("");
4711 } else {
4712 if (strstr(copy, "#{") != 0) {
4713 format_log(es, "expanding inner format '%s'", copy);
4714 value = format_expand1(es, copy);
4715 } else {
4716 value = format_find(ft, copy, modifiers, time_format);
4717 if (value == NULL) {
4718 format_log(es, "format '%s' not found", copy);
4719 value = xstrdup("");
4720 } else {
4721 format_log(es, "format '%s' found: %s", copy,
4722 value);
4727 done:
4728 /* Expand again if required. */
4729 if (modifiers & FORMAT_EXPAND) {
4730 new = format_expand1(es, value);
4731 free(value);
4732 value = new;
4733 } else if (modifiers & FORMAT_EXPANDTIME) {
4734 format_copy_state(&next, es, FORMAT_EXPAND_TIME);
4735 new = format_expand1(&next, value);
4736 free(value);
4737 value = new;
4740 /* Perform substitution if any. */
4741 for (i = 0; i < nsub; i++) {
4742 left = format_expand1(es, sub[i]->argv[0]);
4743 right = format_expand1(es, sub[i]->argv[1]);
4744 new = format_sub(sub[i], value, left, right);
4745 format_log(es, "substitute '%s' to '%s': %s", left, right, new);
4746 free(value);
4747 value = new;
4748 free(right);
4749 free(left);
4752 /* Truncate the value if needed. */
4753 if (limit > 0) {
4754 new = format_trim_left(value, limit);
4755 if (marker != NULL && strcmp(new, value) != 0) {
4756 free(value);
4757 xasprintf(&value, "%s%s", new, marker);
4758 } else {
4759 free(value);
4760 value = new;
4762 format_log(es, "applied length limit %d: %s", limit, value);
4763 } else if (limit < 0) {
4764 new = format_trim_right(value, -limit);
4765 if (marker != NULL && strcmp(new, value) != 0) {
4766 free(value);
4767 xasprintf(&value, "%s%s", marker, new);
4768 } else {
4769 free(value);
4770 value = new;
4772 format_log(es, "applied length limit %d: %s", limit, value);
4775 /* Pad the value if needed. */
4776 if (width > 0) {
4777 new = utf8_padcstr(value, width);
4778 free(value);
4779 value = new;
4780 format_log(es, "applied padding width %d: %s", width, value);
4781 } else if (width < 0) {
4782 new = utf8_rpadcstr(value, -width);
4783 free(value);
4784 value = new;
4785 format_log(es, "applied padding width %d: %s", width, value);
4788 /* Replace with the length or width if needed. */
4789 if (modifiers & FORMAT_LENGTH) {
4790 xasprintf(&new, "%zu", strlen(value));
4791 free(value);
4792 value = new;
4793 format_log(es, "replacing with length: %s", new);
4795 if (modifiers & FORMAT_WIDTH) {
4796 xasprintf(&new, "%u", format_width(value));
4797 free(value);
4798 value = new;
4799 format_log(es, "replacing with width: %s", new);
4802 /* Expand the buffer and copy in the value. */
4803 valuelen = strlen(value);
4804 while (*len - *off < valuelen + 1) {
4805 *buf = xreallocarray(*buf, 2, *len);
4806 *len *= 2;
4808 memcpy(*buf + *off, value, valuelen);
4809 *off += valuelen;
4811 format_log(es, "replaced '%s' with '%s'", copy0, value);
4812 free(value);
4814 free(sub);
4815 format_free_modifiers(list, count);
4816 free(copy0);
4817 return (0);
4819 fail:
4820 format_log(es, "failed %s", copy0);
4822 free(sub);
4823 format_free_modifiers(list, count);
4824 free(copy0);
4825 return (-1);
4828 /* Expand keys in a template. */
4829 static char *
4830 format_expand1(struct format_expand_state *es, const char *fmt)
4832 struct format_tree *ft = es->ft;
4833 char *buf, *out, *name;
4834 const char *ptr, *s, *style_end = NULL;
4835 size_t off, len, n, outlen;
4836 int ch, brackets;
4837 char expanded[8192];
4839 if (fmt == NULL || *fmt == '\0')
4840 return (xstrdup(""));
4842 if (es->loop == FORMAT_LOOP_LIMIT) {
4843 format_log(es, "reached loop limit (%u)", FORMAT_LOOP_LIMIT);
4844 return (xstrdup(""));
4846 es->loop++;
4848 format_log(es, "expanding format: %s", fmt);
4850 if ((es->flags & FORMAT_EXPAND_TIME) && strchr(fmt, '%') != NULL) {
4851 if (es->time == 0) {
4852 es->time = time(NULL);
4853 localtime_r(&es->time, &es->tm);
4855 if (strftime(expanded, sizeof expanded, fmt, &es->tm) == 0) {
4856 format_log(es, "format is too long");
4857 return (xstrdup(""));
4859 if (format_logging(ft) && strcmp(expanded, fmt) != 0)
4860 format_log(es, "after time expanded: %s", expanded);
4861 fmt = expanded;
4864 len = 64;
4865 buf = xmalloc(len);
4866 off = 0;
4868 while (*fmt != '\0') {
4869 if (*fmt != '#') {
4870 while (len - off < 2) {
4871 buf = xreallocarray(buf, 2, len);
4872 len *= 2;
4874 buf[off++] = *fmt++;
4875 continue;
4877 fmt++;
4879 ch = (u_char)*fmt++;
4880 switch (ch) {
4881 case '(':
4882 brackets = 1;
4883 for (ptr = fmt; *ptr != '\0'; ptr++) {
4884 if (*ptr == '(')
4885 brackets++;
4886 if (*ptr == ')' && --brackets == 0)
4887 break;
4889 if (*ptr != ')' || brackets != 0)
4890 break;
4891 n = ptr - fmt;
4893 name = xstrndup(fmt, n);
4894 format_log(es, "found #(): %s", name);
4896 if ((ft->flags & FORMAT_NOJOBS) ||
4897 (es->flags & FORMAT_EXPAND_NOJOBS)) {
4898 out = xstrdup("");
4899 format_log(es, "#() is disabled");
4900 } else {
4901 out = format_job_get(es, name);
4902 format_log(es, "#() result: %s", out);
4904 free(name);
4906 outlen = strlen(out);
4907 while (len - off < outlen + 1) {
4908 buf = xreallocarray(buf, 2, len);
4909 len *= 2;
4911 memcpy(buf + off, out, outlen);
4912 off += outlen;
4914 free(out);
4916 fmt += n + 1;
4917 continue;
4918 case '{':
4919 ptr = format_skip((char *)fmt - 2, "}");
4920 if (ptr == NULL)
4921 break;
4922 n = ptr - fmt;
4924 format_log(es, "found #{}: %.*s", (int)n, fmt);
4925 if (format_replace(es, fmt, n, &buf, &len, &off) != 0)
4926 break;
4927 fmt += n + 1;
4928 continue;
4929 case '[':
4930 case '#':
4932 * If ##[ (with two or more #s), then it is a style and
4933 * can be left for format_draw to handle.
4935 ptr = fmt - (ch == '[');
4936 n = 2 - (ch == '[');
4937 while (*ptr == '#') {
4938 ptr++;
4939 n++;
4941 if (*ptr == '[') {
4942 style_end = format_skip(fmt - 2, "]");
4943 format_log(es, "found #*%zu[", n);
4944 while (len - off < n + 2) {
4945 buf = xreallocarray(buf, 2, len);
4946 len *= 2;
4948 memcpy(buf + off, fmt - 2, n + 1);
4949 off += n + 1;
4950 fmt = ptr + 1;
4951 continue;
4953 /* FALLTHROUGH */
4954 case '}':
4955 case ',':
4956 format_log(es, "found #%c", ch);
4957 while (len - off < 2) {
4958 buf = xreallocarray(buf, 2, len);
4959 len *= 2;
4961 buf[off++] = ch;
4962 continue;
4963 default:
4964 s = NULL;
4965 if (fmt > style_end) { /* skip inside #[] */
4966 if (ch >= 'A' && ch <= 'Z')
4967 s = format_upper[ch - 'A'];
4968 else if (ch >= 'a' && ch <= 'z')
4969 s = format_lower[ch - 'a'];
4971 if (s == NULL) {
4972 while (len - off < 3) {
4973 buf = xreallocarray(buf, 2, len);
4974 len *= 2;
4976 buf[off++] = '#';
4977 buf[off++] = ch;
4978 continue;
4980 n = strlen(s);
4981 format_log(es, "found #%c: %s", ch, s);
4982 if (format_replace(es, s, n, &buf, &len, &off) != 0)
4983 break;
4984 continue;
4987 break;
4989 buf[off] = '\0';
4991 format_log(es, "result is: %s", buf);
4992 es->loop--;
4994 return (buf);
4997 /* Expand keys in a template, passing through strftime first. */
4998 char *
4999 format_expand_time(struct format_tree *ft, const char *fmt)
5001 struct format_expand_state es;
5003 memset(&es, 0, sizeof es);
5004 es.ft = ft;
5005 es.flags = FORMAT_EXPAND_TIME;
5006 return (format_expand1(&es, fmt));
5009 /* Expand keys in a template. */
5010 char *
5011 format_expand(struct format_tree *ft, const char *fmt)
5013 struct format_expand_state es;
5015 memset(&es, 0, sizeof es);
5016 es.ft = ft;
5017 es.flags = 0;
5018 return (format_expand1(&es, fmt));
5021 /* Expand a single string. */
5022 char *
5023 format_single(struct cmdq_item *item, const char *fmt, struct client *c,
5024 struct session *s, struct winlink *wl, struct window_pane *wp)
5026 struct format_tree *ft;
5027 char *expanded;
5029 ft = format_create_defaults(item, c, s, wl, wp);
5030 expanded = format_expand(ft, fmt);
5031 format_free(ft);
5032 return (expanded);
5035 /* Expand a single string using state. */
5036 char *
5037 format_single_from_state(struct cmdq_item *item, const char *fmt,
5038 struct client *c, struct cmd_find_state *fs)
5040 return (format_single(item, fmt, c, fs->s, fs->wl, fs->wp));
5043 /* Expand a single string using target. */
5044 char *
5045 format_single_from_target(struct cmdq_item *item, const char *fmt)
5047 struct client *tc = cmdq_get_target_client(item);
5049 return (format_single_from_state(item, fmt, tc, cmdq_get_target(item)));
5052 /* Create and add defaults. */
5053 struct format_tree *
5054 format_create_defaults(struct cmdq_item *item, struct client *c,
5055 struct session *s, struct winlink *wl, struct window_pane *wp)
5057 struct format_tree *ft;
5059 if (item != NULL)
5060 ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0);
5061 else
5062 ft = format_create(NULL, item, FORMAT_NONE, 0);
5063 format_defaults(ft, c, s, wl, wp);
5064 return (ft);
5067 /* Create and add defaults using state. */
5068 struct format_tree *
5069 format_create_from_state(struct cmdq_item *item, struct client *c,
5070 struct cmd_find_state *fs)
5072 return (format_create_defaults(item, c, fs->s, fs->wl, fs->wp));
5075 /* Create and add defaults using target. */
5076 struct format_tree *
5077 format_create_from_target(struct cmdq_item *item)
5079 struct client *tc = cmdq_get_target_client(item);
5081 return (format_create_from_state(item, tc, cmdq_get_target(item)));
5084 /* Set defaults for any of arguments that are not NULL. */
5085 void
5086 format_defaults(struct format_tree *ft, struct client *c, struct session *s,
5087 struct winlink *wl, struct window_pane *wp)
5089 struct paste_buffer *pb;
5091 if (c != NULL && c->name != NULL)
5092 log_debug("%s: c=%s", __func__, c->name);
5093 else
5094 log_debug("%s: c=none", __func__);
5095 if (s != NULL)
5096 log_debug("%s: s=$%u", __func__, s->id);
5097 else
5098 log_debug("%s: s=none", __func__);
5099 if (wl != NULL)
5100 log_debug("%s: wl=%u", __func__, wl->idx);
5101 else
5102 log_debug("%s: wl=none", __func__);
5103 if (wp != NULL)
5104 log_debug("%s: wp=%%%u", __func__, wp->id);
5105 else
5106 log_debug("%s: wp=none", __func__);
5108 if (c != NULL && s != NULL && c->session != s)
5109 log_debug("%s: session does not match", __func__);
5111 if (wp != NULL)
5112 ft->type = FORMAT_TYPE_PANE;
5113 else if (wl != NULL)
5114 ft->type = FORMAT_TYPE_WINDOW;
5115 else if (s != NULL)
5116 ft->type = FORMAT_TYPE_SESSION;
5117 else
5118 ft->type = FORMAT_TYPE_UNKNOWN;
5120 if (s == NULL && c != NULL)
5121 s = c->session;
5122 if (wl == NULL && s != NULL)
5123 wl = s->curw;
5124 if (wp == NULL && wl != NULL)
5125 wp = wl->window->active;
5127 if (c != NULL)
5128 format_defaults_client(ft, c);
5129 if (s != NULL)
5130 format_defaults_session(ft, s);
5131 if (wl != NULL)
5132 format_defaults_winlink(ft, wl);
5133 if (wp != NULL)
5134 format_defaults_pane(ft, wp);
5136 pb = paste_get_top(NULL);
5137 if (pb != NULL)
5138 format_defaults_paste_buffer(ft, pb);
5141 /* Set default format keys for a session. */
5142 static void
5143 format_defaults_session(struct format_tree *ft, struct session *s)
5145 ft->s = s;
5148 /* Set default format keys for a client. */
5149 static void
5150 format_defaults_client(struct format_tree *ft, struct client *c)
5152 if (ft->s == NULL)
5153 ft->s = c->session;
5154 ft->c = c;
5157 /* Set default format keys for a window. */
5158 void
5159 format_defaults_window(struct format_tree *ft, struct window *w)
5161 ft->w = w;
5164 /* Set default format keys for a winlink. */
5165 static void
5166 format_defaults_winlink(struct format_tree *ft, struct winlink *wl)
5168 if (ft->w == NULL)
5169 format_defaults_window(ft, wl->window);
5170 ft->wl = wl;
5173 /* Set default format keys for a window pane. */
5174 void
5175 format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
5177 struct window_mode_entry *wme;
5179 if (ft->w == NULL)
5180 format_defaults_window(ft, wp->window);
5181 ft->wp = wp;
5183 wme = TAILQ_FIRST(&wp->modes);
5184 if (wme != NULL && wme->mode->formats != NULL)
5185 wme->mode->formats(wme, ft);
5188 /* Set default format keys for paste buffer. */
5189 void
5190 format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
5192 ft->pb = pb;
5195 static int
5196 format_is_word_separator(const char *ws, const struct grid_cell *gc)
5198 if (utf8_cstrhas(ws, &gc->data))
5199 return (1);
5200 if (gc->flags & GRID_FLAG_TAB)
5201 return (1);
5202 return gc->data.size == 1 && *gc->data.data == ' ';
5205 /* Return word at given coordinates. Caller frees. */
5206 char *
5207 format_grid_word(struct grid *gd, u_int x, u_int y)
5209 const struct grid_line *gl;
5210 struct grid_cell gc;
5211 const char *ws;
5212 struct utf8_data *ud = NULL;
5213 u_int end;
5214 size_t size = 0;
5215 int found = 0;
5216 char *s = NULL;
5218 ws = options_get_string(global_s_options, "word-separators");
5220 for (;;) {
5221 grid_get_cell(gd, x, y, &gc);
5222 if (gc.flags & GRID_FLAG_PADDING)
5223 break;
5224 if (format_is_word_separator(ws, &gc)) {
5225 found = 1;
5226 break;
5229 if (x == 0) {
5230 if (y == 0)
5231 break;
5232 gl = grid_peek_line(gd, y - 1);
5233 if (~gl->flags & GRID_LINE_WRAPPED)
5234 break;
5235 y--;
5236 x = grid_line_length(gd, y);
5237 if (x == 0)
5238 break;
5240 x--;
5242 for (;;) {
5243 if (found) {
5244 end = grid_line_length(gd, y);
5245 if (end == 0 || x == end - 1) {
5246 if (y == gd->hsize + gd->sy - 1)
5247 break;
5248 gl = grid_peek_line(gd, y);
5249 if (~gl->flags & GRID_LINE_WRAPPED)
5250 break;
5251 y++;
5252 x = 0;
5253 } else
5254 x++;
5256 found = 1;
5258 grid_get_cell(gd, x, y, &gc);
5259 if (gc.flags & GRID_FLAG_PADDING)
5260 break;
5261 if (format_is_word_separator(ws, &gc))
5262 break;
5264 ud = xreallocarray(ud, size + 2, sizeof *ud);
5265 memcpy(&ud[size++], &gc.data, sizeof *ud);
5267 if (size != 0) {
5268 ud[size].size = 0;
5269 s = utf8_tocstr(ud);
5270 free(ud);
5272 return (s);
5275 /* Return line at given coordinates. Caller frees. */
5276 char *
5277 format_grid_line(struct grid *gd, u_int y)
5279 struct grid_cell gc;
5280 struct utf8_data *ud = NULL;
5281 u_int x;
5282 size_t size = 0;
5283 char *s = NULL;
5285 for (x = 0; x < grid_line_length(gd, y); x++) {
5286 grid_get_cell(gd, x, y, &gc);
5287 if (gc.flags & GRID_FLAG_PADDING)
5288 continue;
5290 ud = xreallocarray(ud, size + 2, sizeof *ud);
5291 if (gc.flags & GRID_FLAG_TAB)
5292 utf8_set(&ud[size++], '\t');
5293 else
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 hyperlink at given coordinates. Caller frees. */
5305 char *
5306 format_grid_hyperlink(struct grid *gd, u_int x, u_int y, struct screen* s)
5308 const char *uri;
5309 struct grid_cell gc;
5311 grid_get_cell(gd, x, y, &gc);
5312 if (gc.flags & GRID_FLAG_PADDING)
5313 return (NULL);
5314 if (s->hyperlinks == NULL || gc.link == 0)
5315 return (NULL);
5316 if (!hyperlinks_get(s->hyperlinks, gc.link, &uri, NULL, NULL))
5317 return (NULL);
5318 return (xstrdup(uri));