Merge branch 'obsd-master'
[tmux.git] / format.c
blob136360aeca803a1c8dc2eb1a09e71ddd008c6815
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 (TAILQ_FIRST(&wp->modes)->mode == &window_copy_mode ||
1140 TAILQ_FIRST(&wp->modes)->mode == &window_view_mode)
1141 return (window_copy_get_word(wp, x, y));
1142 return (NULL);
1144 gd = wp->base.grid;
1145 return (format_grid_word(gd, x, gd->hsize + y));
1148 /* Callback for mouse_hyperlink. */
1149 static void *
1150 format_cb_mouse_hyperlink(struct format_tree *ft)
1152 struct window_pane *wp;
1153 struct grid *gd;
1154 u_int x, y;
1156 if (!ft->m.valid)
1157 return (NULL);
1158 wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1159 if (wp == NULL)
1160 return (NULL);
1161 if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0)
1162 return (NULL);
1163 gd = wp->base.grid;
1164 return (format_grid_hyperlink(gd, x, gd->hsize + y, wp->screen));
1167 /* Callback for mouse_line. */
1168 static void *
1169 format_cb_mouse_line(struct format_tree *ft)
1171 struct window_pane *wp;
1172 struct grid *gd;
1173 u_int x, y;
1175 if (!ft->m.valid)
1176 return (NULL);
1177 wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1178 if (wp == NULL)
1179 return (NULL);
1180 if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0)
1181 return (NULL);
1183 if (!TAILQ_EMPTY(&wp->modes)) {
1184 if (TAILQ_FIRST(&wp->modes)->mode == &window_copy_mode ||
1185 TAILQ_FIRST(&wp->modes)->mode == &window_view_mode)
1186 return (window_copy_get_line(wp, y));
1187 return (NULL);
1189 gd = wp->base.grid;
1190 return (format_grid_line(gd, gd->hsize + y));
1193 /* Callback for mouse_status_line. */
1194 static void *
1195 format_cb_mouse_status_line(struct format_tree *ft)
1197 char *value;
1198 u_int y;
1200 if (!ft->m.valid)
1201 return (NULL);
1202 if (ft->c == NULL || (~ft->c->tty.flags & TTY_STARTED))
1203 return (NULL);
1205 if (ft->m.statusat == 0 && ft->m.y < ft->m.statuslines) {
1206 y = ft->m.y;
1207 } else if (ft->m.statusat > 0 && ft->m.y >= (u_int)ft->m.statusat) {
1208 y = ft->m.y - ft->m.statusat;
1209 } else
1210 return (NULL);
1211 xasprintf(&value, "%u", y);
1212 return (value);
1216 /* Callback for mouse_status_range. */
1217 static void *
1218 format_cb_mouse_status_range(struct format_tree *ft)
1220 struct style_range *sr;
1221 u_int x, y;
1223 if (!ft->m.valid)
1224 return (NULL);
1225 if (ft->c == NULL || (~ft->c->tty.flags & TTY_STARTED))
1226 return (NULL);
1228 if (ft->m.statusat == 0 && ft->m.y < ft->m.statuslines) {
1229 x = ft->m.x;
1230 y = ft->m.y;
1231 } else if (ft->m.statusat > 0 && ft->m.y >= (u_int)ft->m.statusat) {
1232 x = ft->m.x;
1233 y = ft->m.y - ft->m.statusat;
1234 } else
1235 return (NULL);
1237 sr = status_get_range(ft->c, x, y);
1238 if (sr == NULL)
1239 return (NULL);
1240 switch (sr->type) {
1241 case STYLE_RANGE_NONE:
1242 return (NULL);
1243 case STYLE_RANGE_LEFT:
1244 return (xstrdup("left"));
1245 case STYLE_RANGE_RIGHT:
1246 return (xstrdup("right"));
1247 case STYLE_RANGE_PANE:
1248 return (xstrdup("pane"));
1249 case STYLE_RANGE_WINDOW:
1250 return (xstrdup("window"));
1251 case STYLE_RANGE_SESSION:
1252 return (xstrdup("session"));
1253 case STYLE_RANGE_USER:
1254 return (xstrdup(sr->string));
1256 return (NULL);
1259 /* Callback for alternate_on. */
1260 static void *
1261 format_cb_alternate_on(struct format_tree *ft)
1263 if (ft->wp != NULL) {
1264 if (ft->wp->base.saved_grid != NULL)
1265 return (xstrdup("1"));
1266 return (xstrdup("0"));
1268 return (NULL);
1271 /* Callback for alternate_saved_x. */
1272 static void *
1273 format_cb_alternate_saved_x(struct format_tree *ft)
1275 if (ft->wp != NULL)
1276 return (format_printf("%u", ft->wp->base.saved_cx));
1277 return (NULL);
1280 /* Callback for alternate_saved_y. */
1281 static void *
1282 format_cb_alternate_saved_y(struct format_tree *ft)
1284 if (ft->wp != NULL)
1285 return (format_printf("%u", ft->wp->base.saved_cy));
1286 return (NULL);
1289 /* Callback for buffer_name. */
1290 static void *
1291 format_cb_buffer_name(struct format_tree *ft)
1293 if (ft->pb != NULL)
1294 return (xstrdup(paste_buffer_name(ft->pb)));
1295 return (NULL);
1298 /* Callback for buffer_sample. */
1299 static void *
1300 format_cb_buffer_sample(struct format_tree *ft)
1302 if (ft->pb != NULL)
1303 return (paste_make_sample(ft->pb));
1304 return (NULL);
1307 /* Callback for buffer_size. */
1308 static void *
1309 format_cb_buffer_size(struct format_tree *ft)
1311 size_t size;
1313 if (ft->pb != NULL) {
1314 paste_buffer_data(ft->pb, &size);
1315 return (format_printf("%zu", size));
1317 return (NULL);
1320 /* Callback for client_cell_height. */
1321 static void *
1322 format_cb_client_cell_height(struct format_tree *ft)
1324 if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED))
1325 return (format_printf("%u", ft->c->tty.ypixel));
1326 return (NULL);
1329 /* Callback for client_cell_width. */
1330 static void *
1331 format_cb_client_cell_width(struct format_tree *ft)
1333 if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED))
1334 return (format_printf("%u", ft->c->tty.xpixel));
1335 return (NULL);
1338 /* Callback for client_control_mode. */
1339 static void *
1340 format_cb_client_control_mode(struct format_tree *ft)
1342 if (ft->c != NULL) {
1343 if (ft->c->flags & CLIENT_CONTROL)
1344 return (xstrdup("1"));
1345 return (xstrdup("0"));
1347 return (NULL);
1350 /* Callback for client_discarded. */
1351 static void *
1352 format_cb_client_discarded(struct format_tree *ft)
1354 if (ft->c != NULL)
1355 return (format_printf("%zu", ft->c->discarded));
1356 return (NULL);
1359 /* Callback for client_flags. */
1360 static void *
1361 format_cb_client_flags(struct format_tree *ft)
1363 if (ft->c != NULL)
1364 return (xstrdup(server_client_get_flags(ft->c)));
1365 return (NULL);
1368 /* Callback for client_height. */
1369 static void *
1370 format_cb_client_height(struct format_tree *ft)
1372 if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED))
1373 return (format_printf("%u", ft->c->tty.sy));
1374 return (NULL);
1377 /* Callback for client_key_table. */
1378 static void *
1379 format_cb_client_key_table(struct format_tree *ft)
1381 if (ft->c != NULL)
1382 return (xstrdup(ft->c->keytable->name));
1383 return (NULL);
1386 /* Callback for client_last_session. */
1387 static void *
1388 format_cb_client_last_session(struct format_tree *ft)
1390 if (ft->c != NULL &&
1391 ft->c->last_session != NULL &&
1392 session_alive(ft->c->last_session))
1393 return (xstrdup(ft->c->last_session->name));
1394 return (NULL);
1397 /* Callback for client_name. */
1398 static void *
1399 format_cb_client_name(struct format_tree *ft)
1401 if (ft->c != NULL)
1402 return (xstrdup(ft->c->name));
1403 return (NULL);
1406 /* Callback for client_pid. */
1407 static void *
1408 format_cb_client_pid(struct format_tree *ft)
1410 if (ft->c != NULL)
1411 return (format_printf("%ld", (long)ft->c->pid));
1412 return (NULL);
1415 /* Callback for client_prefix. */
1416 static void *
1417 format_cb_client_prefix(struct format_tree *ft)
1419 const char *name;
1421 if (ft->c != NULL) {
1422 name = server_client_get_key_table(ft->c);
1423 if (strcmp(ft->c->keytable->name, name) == 0)
1424 return (xstrdup("0"));
1425 return (xstrdup("1"));
1427 return (NULL);
1430 /* Callback for client_readonly. */
1431 static void *
1432 format_cb_client_readonly(struct format_tree *ft)
1434 if (ft->c != NULL) {
1435 if (ft->c->flags & CLIENT_READONLY)
1436 return (xstrdup("1"));
1437 return (xstrdup("0"));
1439 return (NULL);
1442 /* Callback for client_session. */
1443 static void *
1444 format_cb_client_session(struct format_tree *ft)
1446 if (ft->c != NULL && ft->c->session != NULL)
1447 return (xstrdup(ft->c->session->name));
1448 return (NULL);
1451 /* Callback for client_termfeatures. */
1452 static void *
1453 format_cb_client_termfeatures(struct format_tree *ft)
1455 if (ft->c != NULL)
1456 return (xstrdup(tty_get_features(ft->c->term_features)));
1457 return (NULL);
1460 /* Callback for client_termname. */
1461 static void *
1462 format_cb_client_termname(struct format_tree *ft)
1464 if (ft->c != NULL)
1465 return (xstrdup(ft->c->term_name));
1466 return (NULL);
1469 /* Callback for client_termtype. */
1470 static void *
1471 format_cb_client_termtype(struct format_tree *ft)
1473 if (ft->c != NULL) {
1474 if (ft->c->term_type == NULL)
1475 return (xstrdup(""));
1476 return (xstrdup(ft->c->term_type));
1478 return (NULL);
1481 /* Callback for client_tty. */
1482 static void *
1483 format_cb_client_tty(struct format_tree *ft)
1485 if (ft->c != NULL)
1486 return (xstrdup(ft->c->ttyname));
1487 return (NULL);
1490 /* Callback for client_uid. */
1491 static void *
1492 format_cb_client_uid(struct format_tree *ft)
1494 uid_t uid;
1496 if (ft->c != NULL) {
1497 uid = proc_get_peer_uid(ft->c->peer);
1498 if (uid != (uid_t)-1)
1499 return (format_printf("%ld", (long)uid));
1501 return (NULL);
1504 /* Callback for client_user. */
1505 static void *
1506 format_cb_client_user(struct format_tree *ft)
1508 uid_t uid;
1509 struct passwd *pw;
1511 if (ft->c != NULL) {
1512 uid = proc_get_peer_uid(ft->c->peer);
1513 if (uid != (uid_t)-1 && (pw = getpwuid(uid)) != NULL)
1514 return (xstrdup(pw->pw_name));
1516 return (NULL);
1519 /* Callback for client_utf8. */
1520 static void *
1521 format_cb_client_utf8(struct format_tree *ft)
1523 if (ft->c != NULL) {
1524 if (ft->c->flags & CLIENT_UTF8)
1525 return (xstrdup("1"));
1526 return (xstrdup("0"));
1528 return (NULL);
1531 /* Callback for client_width. */
1532 static void *
1533 format_cb_client_width(struct format_tree *ft)
1535 if (ft->c != NULL)
1536 return (format_printf("%u", ft->c->tty.sx));
1537 return (NULL);
1540 /* Callback for client_written. */
1541 static void *
1542 format_cb_client_written(struct format_tree *ft)
1544 if (ft->c != NULL)
1545 return (format_printf("%zu", ft->c->written));
1546 return (NULL);
1549 /* Callback for config_files. */
1550 static void *
1551 format_cb_config_files(__unused struct format_tree *ft)
1553 char *s = NULL;
1554 size_t slen = 0;
1555 u_int i;
1556 size_t n;
1558 for (i = 0; i < cfg_nfiles; i++) {
1559 n = strlen(cfg_files[i]) + 1;
1560 s = xrealloc(s, slen + n + 1);
1561 slen += xsnprintf(s + slen, n + 1, "%s,", cfg_files[i]);
1563 if (s == NULL)
1564 return (xstrdup(""));
1565 s[slen - 1] = '\0';
1566 return (s);
1569 /* Callback for cursor_flag. */
1570 static void *
1571 format_cb_cursor_flag(struct format_tree *ft)
1573 if (ft->wp != NULL) {
1574 if (ft->wp->base.mode & MODE_CURSOR)
1575 return (xstrdup("1"));
1576 return (xstrdup("0"));
1578 return (NULL);
1581 /* Callback for cursor_x. */
1582 static void *
1583 format_cb_cursor_x(struct format_tree *ft)
1585 if (ft->wp != NULL)
1586 return (format_printf("%u", ft->wp->base.cx));
1587 return (NULL);
1590 /* Callback for cursor_y. */
1591 static void *
1592 format_cb_cursor_y(struct format_tree *ft)
1594 if (ft->wp != NULL)
1595 return (format_printf("%u", ft->wp->base.cy));
1596 return (NULL);
1599 /* Callback for history_limit. */
1600 static void *
1601 format_cb_history_limit(struct format_tree *ft)
1603 if (ft->wp != NULL)
1604 return (format_printf("%u", ft->wp->base.grid->hlimit));
1605 return (NULL);
1608 /* Callback for history_size. */
1609 static void *
1610 format_cb_history_size(struct format_tree *ft)
1612 if (ft->wp != NULL)
1613 return (format_printf("%u", ft->wp->base.grid->hsize));
1614 return (NULL);
1617 /* Callback for insert_flag. */
1618 static void *
1619 format_cb_insert_flag(struct format_tree *ft)
1621 if (ft->wp != NULL) {
1622 if (ft->wp->base.mode & MODE_INSERT)
1623 return (xstrdup("1"));
1624 return (xstrdup("0"));
1626 return (NULL);
1629 /* Callback for keypad_cursor_flag. */
1630 static void *
1631 format_cb_keypad_cursor_flag(struct format_tree *ft)
1633 if (ft->wp != NULL) {
1634 if (ft->wp->base.mode & MODE_KCURSOR)
1635 return (xstrdup("1"));
1636 return (xstrdup("0"));
1638 return (NULL);
1641 /* Callback for keypad_flag. */
1642 static void *
1643 format_cb_keypad_flag(struct format_tree *ft)
1645 if (ft->wp != NULL) {
1646 if (ft->wp->base.mode & MODE_KKEYPAD)
1647 return (xstrdup("1"));
1648 return (xstrdup("0"));
1650 return (NULL);
1653 /* Callback for mouse_all_flag. */
1654 static void *
1655 format_cb_mouse_all_flag(struct format_tree *ft)
1657 if (ft->wp != NULL) {
1658 if (ft->wp->base.mode & MODE_MOUSE_ALL)
1659 return (xstrdup("1"));
1660 return (xstrdup("0"));
1662 return (NULL);
1665 /* Callback for mouse_any_flag. */
1666 static void *
1667 format_cb_mouse_any_flag(struct format_tree *ft)
1669 if (ft->wp != NULL) {
1670 if (ft->wp->base.mode & ALL_MOUSE_MODES)
1671 return (xstrdup("1"));
1672 return (xstrdup("0"));
1674 return (NULL);
1677 /* Callback for mouse_button_flag. */
1678 static void *
1679 format_cb_mouse_button_flag(struct format_tree *ft)
1681 if (ft->wp != NULL) {
1682 if (ft->wp->base.mode & MODE_MOUSE_BUTTON)
1683 return (xstrdup("1"));
1684 return (xstrdup("0"));
1686 return (NULL);
1689 /* Callback for mouse_pane. */
1690 static void *
1691 format_cb_mouse_pane(struct format_tree *ft)
1693 struct window_pane *wp;
1695 if (ft->m.valid) {
1696 wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1697 if (wp != NULL)
1698 return (format_printf("%%%u", wp->id));
1699 return (NULL);
1701 return (NULL);
1704 /* Callback for mouse_sgr_flag. */
1705 static void *
1706 format_cb_mouse_sgr_flag(struct format_tree *ft)
1708 if (ft->wp != NULL) {
1709 if (ft->wp->base.mode & MODE_MOUSE_SGR)
1710 return (xstrdup("1"));
1711 return (xstrdup("0"));
1713 return (NULL);
1716 /* Callback for mouse_standard_flag. */
1717 static void *
1718 format_cb_mouse_standard_flag(struct format_tree *ft)
1720 if (ft->wp != NULL) {
1721 if (ft->wp->base.mode & MODE_MOUSE_STANDARD)
1722 return (xstrdup("1"));
1723 return (xstrdup("0"));
1725 return (NULL);
1728 /* Callback for mouse_utf8_flag. */
1729 static void *
1730 format_cb_mouse_utf8_flag(struct format_tree *ft)
1732 if (ft->wp != NULL) {
1733 if (ft->wp->base.mode & MODE_MOUSE_UTF8)
1734 return (xstrdup("1"));
1735 return (xstrdup("0"));
1737 return (NULL);
1740 /* Callback for mouse_x. */
1741 static void *
1742 format_cb_mouse_x(struct format_tree *ft)
1744 struct window_pane *wp;
1745 u_int x, y;
1747 if (!ft->m.valid)
1748 return (NULL);
1749 wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1750 if (wp != NULL && cmd_mouse_at(wp, &ft->m, &x, &y, 0) == 0)
1751 return (format_printf("%u", x));
1752 if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED)) {
1753 if (ft->m.statusat == 0 && ft->m.y < ft->m.statuslines)
1754 return (format_printf("%u", ft->m.x));
1755 if (ft->m.statusat > 0 && ft->m.y >= (u_int)ft->m.statusat)
1756 return (format_printf("%u", ft->m.x));
1758 return (NULL);
1761 /* Callback for mouse_y. */
1762 static void *
1763 format_cb_mouse_y(struct format_tree *ft)
1765 struct window_pane *wp;
1766 u_int x, y;
1768 if (!ft->m.valid)
1769 return (NULL);
1770 wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1771 if (wp != NULL && cmd_mouse_at(wp, &ft->m, &x, &y, 0) == 0)
1772 return (format_printf("%u", y));
1773 if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED)) {
1774 if (ft->m.statusat == 0 && ft->m.y < ft->m.statuslines)
1775 return (format_printf("%u", ft->m.y));
1776 if (ft->m.statusat > 0 && ft->m.y >= (u_int)ft->m.statusat)
1777 return (format_printf("%u", ft->m.y - ft->m.statusat));
1779 return (NULL);
1782 /* Callback for next_session_id. */
1783 static void *
1784 format_cb_next_session_id(__unused struct format_tree *ft)
1786 return (format_printf("$%u", next_session_id));
1789 /* Callback for origin_flag. */
1790 static void *
1791 format_cb_origin_flag(struct format_tree *ft)
1793 if (ft->wp != NULL) {
1794 if (ft->wp->base.mode & MODE_ORIGIN)
1795 return (xstrdup("1"));
1796 return (xstrdup("0"));
1798 return (NULL);
1801 /* Callback for pane_active. */
1802 static void *
1803 format_cb_pane_active(struct format_tree *ft)
1805 if (ft->wp != NULL) {
1806 if (ft->wp == ft->wp->window->active)
1807 return (xstrdup("1"));
1808 return (xstrdup("0"));
1810 return (NULL);
1813 /* Callback for pane_at_left. */
1814 static void *
1815 format_cb_pane_at_left(struct format_tree *ft)
1817 if (ft->wp != NULL) {
1818 if (ft->wp->xoff == 0)
1819 return (xstrdup("1"));
1820 return (xstrdup("0"));
1822 return (NULL);
1825 /* Callback for pane_at_right. */
1826 static void *
1827 format_cb_pane_at_right(struct format_tree *ft)
1829 if (ft->wp != NULL) {
1830 if (ft->wp->xoff + ft->wp->sx == ft->wp->window->sx)
1831 return (xstrdup("1"));
1832 return (xstrdup("0"));
1834 return (NULL);
1837 /* Callback for pane_bottom. */
1838 static void *
1839 format_cb_pane_bottom(struct format_tree *ft)
1841 if (ft->wp != NULL)
1842 return (format_printf("%u", ft->wp->yoff + ft->wp->sy - 1));
1843 return (NULL);
1846 /* Callback for pane_dead. */
1847 static void *
1848 format_cb_pane_dead(struct format_tree *ft)
1850 if (ft->wp != NULL) {
1851 if (ft->wp->fd == -1)
1852 return (xstrdup("1"));
1853 return (xstrdup("0"));
1855 return (NULL);
1858 /* Callback for pane_dead_signal. */
1859 static void *
1860 format_cb_pane_dead_signal(struct format_tree *ft)
1862 struct window_pane *wp = ft->wp;
1863 const char *name;
1865 if (wp != NULL) {
1866 if ((wp->flags & PANE_STATUSREADY) && WIFSIGNALED(wp->status)) {
1867 name = sig2name(WTERMSIG(wp->status));
1868 return (format_printf("%s", name));
1870 return (NULL);
1872 return (NULL);
1875 /* Callback for pane_dead_status. */
1876 static void *
1877 format_cb_pane_dead_status(struct format_tree *ft)
1879 struct window_pane *wp = ft->wp;
1881 if (wp != NULL) {
1882 if ((wp->flags & PANE_STATUSREADY) && WIFEXITED(wp->status))
1883 return (format_printf("%d", WEXITSTATUS(wp->status)));
1884 return (NULL);
1886 return (NULL);
1889 /* Callback for pane_dead_time. */
1890 static void *
1891 format_cb_pane_dead_time(struct format_tree *ft)
1893 struct window_pane *wp = ft->wp;
1895 if (wp != NULL) {
1896 if (wp->flags & PANE_STATUSDRAWN)
1897 return (&wp->dead_time);
1898 return (NULL);
1900 return (NULL);
1903 /* Callback for pane_format. */
1904 static void *
1905 format_cb_pane_format(struct format_tree *ft)
1907 if (ft->type == FORMAT_TYPE_PANE)
1908 return (xstrdup("1"));
1909 return (xstrdup("0"));
1912 /* Callback for pane_height. */
1913 static void *
1914 format_cb_pane_height(struct format_tree *ft)
1916 if (ft->wp != NULL)
1917 return (format_printf("%u", ft->wp->sy));
1918 return (NULL);
1921 /* Callback for pane_id. */
1922 static void *
1923 format_cb_pane_id(struct format_tree *ft)
1925 if (ft->wp != NULL)
1926 return (format_printf("%%%u", ft->wp->id));
1927 return (NULL);
1930 /* Callback for pane_index. */
1931 static void *
1932 format_cb_pane_index(struct format_tree *ft)
1934 u_int idx;
1936 if (ft->wp != NULL && window_pane_index(ft->wp, &idx) == 0)
1937 return (format_printf("%u", idx));
1938 return (NULL);
1941 /* Callback for pane_input_off. */
1942 static void *
1943 format_cb_pane_input_off(struct format_tree *ft)
1945 if (ft->wp != NULL) {
1946 if (ft->wp->flags & PANE_INPUTOFF)
1947 return (xstrdup("1"));
1948 return (xstrdup("0"));
1950 return (NULL);
1953 /* Callback for pane_unseen_changes. */
1954 static void *
1955 format_cb_pane_unseen_changes(struct format_tree *ft)
1957 if (ft->wp != NULL) {
1958 if (ft->wp->flags & PANE_UNSEENCHANGES)
1959 return (xstrdup("1"));
1960 return (xstrdup("0"));
1962 return (NULL);
1965 /* Callback for pane_key_mode. */
1966 static void *
1967 format_cb_pane_key_mode(struct format_tree *ft)
1969 if (ft->wp != NULL && ft->wp->screen != NULL) {
1970 switch (ft->wp->screen->mode & EXTENDED_KEY_MODES) {
1971 case MODE_KEYS_EXTENDED:
1972 return (xstrdup("Ext 1"));
1973 case MODE_KEYS_EXTENDED_2:
1974 return (xstrdup("Ext 2"));
1975 default:
1976 return (xstrdup("VT10x"));
1979 return (NULL);
1982 /* Callback for pane_last. */
1983 static void *
1984 format_cb_pane_last(struct format_tree *ft)
1986 if (ft->wp != NULL) {
1987 if (ft->wp == TAILQ_FIRST(&ft->wp->window->last_panes))
1988 return (xstrdup("1"));
1989 return (xstrdup("0"));
1991 return (NULL);
1994 /* Callback for pane_left. */
1995 static void *
1996 format_cb_pane_left(struct format_tree *ft)
1998 if (ft->wp != NULL)
1999 return (format_printf("%u", ft->wp->xoff));
2000 return (NULL);
2003 /* Callback for pane_marked. */
2004 static void *
2005 format_cb_pane_marked(struct format_tree *ft)
2007 if (ft->wp != NULL) {
2008 if (server_check_marked() && marked_pane.wp == ft->wp)
2009 return (xstrdup("1"));
2010 return (xstrdup("0"));
2012 return (NULL);
2015 /* Callback for pane_marked_set. */
2016 static void *
2017 format_cb_pane_marked_set(struct format_tree *ft)
2019 if (ft->wp != NULL) {
2020 if (server_check_marked())
2021 return (xstrdup("1"));
2022 return (xstrdup("0"));
2024 return (NULL);
2027 /* Callback for pane_mode. */
2028 static void *
2029 format_cb_pane_mode(struct format_tree *ft)
2031 struct window_mode_entry *wme;
2033 if (ft->wp != NULL) {
2034 wme = TAILQ_FIRST(&ft->wp->modes);
2035 if (wme != NULL)
2036 return (xstrdup(wme->mode->name));
2037 return (NULL);
2039 return (NULL);
2042 /* Callback for pane_path. */
2043 static void *
2044 format_cb_pane_path(struct format_tree *ft)
2046 if (ft->wp != NULL) {
2047 if (ft->wp->base.path == NULL)
2048 return (xstrdup(""));
2049 return (xstrdup(ft->wp->base.path));
2051 return (NULL);
2054 /* Callback for pane_pid. */
2055 static void *
2056 format_cb_pane_pid(struct format_tree *ft)
2058 if (ft->wp != NULL)
2059 return (format_printf("%ld", (long)ft->wp->pid));
2060 return (NULL);
2063 /* Callback for pane_pipe. */
2064 static void *
2065 format_cb_pane_pipe(struct format_tree *ft)
2067 if (ft->wp != NULL) {
2068 if (ft->wp->pipe_fd != -1)
2069 return (xstrdup("1"));
2070 return (xstrdup("0"));
2072 return (NULL);
2075 /* Callback for pane_right. */
2076 static void *
2077 format_cb_pane_right(struct format_tree *ft)
2079 if (ft->wp != NULL)
2080 return (format_printf("%u", ft->wp->xoff + ft->wp->sx - 1));
2081 return (NULL);
2084 /* Callback for pane_search_string. */
2085 static void *
2086 format_cb_pane_search_string(struct format_tree *ft)
2088 if (ft->wp != NULL) {
2089 if (ft->wp->searchstr == NULL)
2090 return (xstrdup(""));
2091 return (xstrdup(ft->wp->searchstr));
2093 return (NULL);
2096 /* Callback for pane_synchronized. */
2097 static void *
2098 format_cb_pane_synchronized(struct format_tree *ft)
2100 if (ft->wp != NULL) {
2101 if (options_get_number(ft->wp->options, "synchronize-panes"))
2102 return (xstrdup("1"));
2103 return (xstrdup("0"));
2105 return (NULL);
2108 /* Callback for pane_title. */
2109 static void *
2110 format_cb_pane_title(struct format_tree *ft)
2112 if (ft->wp != NULL)
2113 return (xstrdup(ft->wp->base.title));
2114 return (NULL);
2117 /* Callback for pane_top. */
2118 static void *
2119 format_cb_pane_top(struct format_tree *ft)
2121 if (ft->wp != NULL)
2122 return (format_printf("%u", ft->wp->yoff));
2123 return (NULL);
2126 /* Callback for pane_tty. */
2127 static void *
2128 format_cb_pane_tty(struct format_tree *ft)
2130 if (ft->wp != NULL)
2131 return (xstrdup(ft->wp->tty));
2132 return (NULL);
2135 /* Callback for pane_width. */
2136 static void *
2137 format_cb_pane_width(struct format_tree *ft)
2139 if (ft->wp != NULL)
2140 return (format_printf("%u", ft->wp->sx));
2141 return (NULL);
2144 /* Callback for scroll_region_lower. */
2145 static void *
2146 format_cb_scroll_region_lower(struct format_tree *ft)
2148 if (ft->wp != NULL)
2149 return (format_printf("%u", ft->wp->base.rlower));
2150 return (NULL);
2153 /* Callback for scroll_region_upper. */
2154 static void *
2155 format_cb_scroll_region_upper(struct format_tree *ft)
2157 if (ft->wp != NULL)
2158 return (format_printf("%u", ft->wp->base.rupper));
2159 return (NULL);
2162 /* Callback for server_sessions. */
2163 static void *
2164 format_cb_server_sessions(__unused struct format_tree *ft)
2166 struct session *s;
2167 u_int n = 0;
2169 RB_FOREACH(s, sessions, &sessions)
2170 n++;
2171 return (format_printf("%u", n));
2174 /* Callback for session_attached. */
2175 static void *
2176 format_cb_session_attached(struct format_tree *ft)
2178 if (ft->s != NULL)
2179 return (format_printf("%u", ft->s->attached));
2180 return (NULL);
2183 /* Callback for session_format. */
2184 static void *
2185 format_cb_session_format(struct format_tree *ft)
2187 if (ft->type == FORMAT_TYPE_SESSION)
2188 return (xstrdup("1"));
2189 return (xstrdup("0"));
2192 /* Callback for session_group. */
2193 static void *
2194 format_cb_session_group(struct format_tree *ft)
2196 struct session_group *sg;
2198 if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL)
2199 return (xstrdup(sg->name));
2200 return (NULL);
2203 /* Callback for session_group_attached. */
2204 static void *
2205 format_cb_session_group_attached(struct format_tree *ft)
2207 struct session_group *sg;
2209 if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL)
2210 return (format_printf("%u", session_group_attached_count (sg)));
2211 return (NULL);
2214 /* Callback for session_group_many_attached. */
2215 static void *
2216 format_cb_session_group_many_attached(struct format_tree *ft)
2218 struct session_group *sg;
2220 if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL) {
2221 if (session_group_attached_count (sg) > 1)
2222 return (xstrdup("1"));
2223 return (xstrdup("0"));
2225 return (NULL);
2228 /* Callback for session_group_size. */
2229 static void *
2230 format_cb_session_group_size(struct format_tree *ft)
2232 struct session_group *sg;
2234 if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL)
2235 return (format_printf("%u", session_group_count (sg)));
2236 return (NULL);
2239 /* Callback for session_grouped. */
2240 static void *
2241 format_cb_session_grouped(struct format_tree *ft)
2243 if (ft->s != NULL) {
2244 if (session_group_contains(ft->s) != NULL)
2245 return (xstrdup("1"));
2246 return (xstrdup("0"));
2248 return (NULL);
2251 /* Callback for session_id. */
2252 static void *
2253 format_cb_session_id(struct format_tree *ft)
2255 if (ft->s != NULL)
2256 return (format_printf("$%u", ft->s->id));
2257 return (NULL);
2260 /* Callback for session_many_attached. */
2261 static void *
2262 format_cb_session_many_attached(struct format_tree *ft)
2264 if (ft->s != NULL) {
2265 if (ft->s->attached > 1)
2266 return (xstrdup("1"));
2267 return (xstrdup("0"));
2269 return (NULL);
2272 /* Callback for session_marked. */
2273 static void *
2274 format_cb_session_marked(struct format_tree *ft)
2276 if (ft->s != NULL) {
2277 if (server_check_marked() && marked_pane.s == ft->s)
2278 return (xstrdup("1"));
2279 return (xstrdup("0"));
2281 return (NULL);
2284 /* Callback for session_name. */
2285 static void *
2286 format_cb_session_name(struct format_tree *ft)
2288 if (ft->s != NULL)
2289 return (xstrdup(ft->s->name));
2290 return (NULL);
2293 /* Callback for session_path. */
2294 static void *
2295 format_cb_session_path(struct format_tree *ft)
2297 if (ft->s != NULL)
2298 return (xstrdup(ft->s->cwd));
2299 return (NULL);
2302 /* Callback for session_windows. */
2303 static void *
2304 format_cb_session_windows(struct format_tree *ft)
2306 if (ft->s != NULL)
2307 return (format_printf("%u", winlink_count(&ft->s->windows)));
2308 return (NULL);
2311 /* Callback for socket_path. */
2312 static void *
2313 format_cb_socket_path(__unused struct format_tree *ft)
2315 return (xstrdup(socket_path));
2318 /* Callback for version. */
2319 static void *
2320 format_cb_version(__unused struct format_tree *ft)
2322 return (xstrdup(getversion()));
2325 /* Callback for active_window_index. */
2326 static void *
2327 format_cb_active_window_index(struct format_tree *ft)
2329 if (ft->s != NULL)
2330 return (format_printf("%u", ft->s->curw->idx));
2331 return (NULL);
2334 /* Callback for last_window_index. */
2335 static void *
2336 format_cb_last_window_index(struct format_tree *ft)
2338 struct winlink *wl;
2340 if (ft->s != NULL) {
2341 wl = RB_MAX(winlinks, &ft->s->windows);
2342 return (format_printf("%u", wl->idx));
2344 return (NULL);
2347 /* Callback for window_active. */
2348 static void *
2349 format_cb_window_active(struct format_tree *ft)
2351 if (ft->wl != NULL) {
2352 if (ft->wl == ft->wl->session->curw)
2353 return (xstrdup("1"));
2354 return (xstrdup("0"));
2356 return (NULL);
2359 /* Callback for window_activity_flag. */
2360 static void *
2361 format_cb_window_activity_flag(struct format_tree *ft)
2363 if (ft->wl != NULL) {
2364 if (ft->wl->flags & WINLINK_ACTIVITY)
2365 return (xstrdup("1"));
2366 return (xstrdup("0"));
2368 return (NULL);
2371 /* Callback for window_bell_flag. */
2372 static void *
2373 format_cb_window_bell_flag(struct format_tree *ft)
2375 if (ft->wl != NULL) {
2376 if (ft->wl->flags & WINLINK_BELL)
2377 return (xstrdup("1"));
2378 return (xstrdup("0"));
2380 return (NULL);
2383 /* Callback for window_bigger. */
2384 static void *
2385 format_cb_window_bigger(struct format_tree *ft)
2387 u_int ox, oy, sx, sy;
2389 if (ft->c != NULL) {
2390 if (tty_window_offset(&ft->c->tty, &ox, &oy, &sx, &sy))
2391 return (xstrdup("1"));
2392 return (xstrdup("0"));
2394 return (NULL);
2397 /* Callback for window_cell_height. */
2398 static void *
2399 format_cb_window_cell_height(struct format_tree *ft)
2401 if (ft->w != NULL)
2402 return (format_printf("%u", ft->w->ypixel));
2403 return (NULL);
2406 /* Callback for window_cell_width. */
2407 static void *
2408 format_cb_window_cell_width(struct format_tree *ft)
2410 if (ft->w != NULL)
2411 return (format_printf("%u", ft->w->xpixel));
2412 return (NULL);
2415 /* Callback for window_end_flag. */
2416 static void *
2417 format_cb_window_end_flag(struct format_tree *ft)
2419 if (ft->wl != NULL) {
2420 if (ft->wl == RB_MAX(winlinks, &ft->wl->session->windows))
2421 return (xstrdup("1"));
2422 return (xstrdup("0"));
2424 return (NULL);
2427 /* Callback for window_flags. */
2428 static void *
2429 format_cb_window_flags(struct format_tree *ft)
2431 if (ft->wl != NULL)
2432 return (xstrdup(window_printable_flags(ft->wl, 1)));
2433 return (NULL);
2436 /* Callback for window_format. */
2437 static void *
2438 format_cb_window_format(struct format_tree *ft)
2440 if (ft->type == FORMAT_TYPE_WINDOW)
2441 return (xstrdup("1"));
2442 return (xstrdup("0"));
2445 /* Callback for window_height. */
2446 static void *
2447 format_cb_window_height(struct format_tree *ft)
2449 if (ft->w != NULL)
2450 return (format_printf("%u", ft->w->sy));
2451 return (NULL);
2454 /* Callback for window_id. */
2455 static void *
2456 format_cb_window_id(struct format_tree *ft)
2458 if (ft->w != NULL)
2459 return (format_printf("@%u", ft->w->id));
2460 return (NULL);
2463 /* Callback for window_index. */
2464 static void *
2465 format_cb_window_index(struct format_tree *ft)
2467 if (ft->wl != NULL)
2468 return (format_printf("%d", ft->wl->idx));
2469 return (NULL);
2472 /* Callback for window_last_flag. */
2473 static void *
2474 format_cb_window_last_flag(struct format_tree *ft)
2476 if (ft->wl != NULL) {
2477 if (ft->wl == TAILQ_FIRST(&ft->wl->session->lastw))
2478 return (xstrdup("1"));
2479 return (xstrdup("0"));
2481 return (NULL);
2484 /* Callback for window_linked. */
2485 static void *
2486 format_cb_window_linked(struct format_tree *ft)
2488 if (ft->wl != NULL) {
2489 if (session_is_linked(ft->wl->session, ft->wl->window))
2490 return (xstrdup("1"));
2491 return (xstrdup("0"));
2493 return (NULL);
2496 /* Callback for window_linked_sessions. */
2497 static void *
2498 format_cb_window_linked_sessions(struct format_tree *ft)
2500 if (ft->wl != NULL)
2501 return (format_printf("%u", ft->wl->window->references));
2502 return (NULL);
2505 /* Callback for window_marked_flag. */
2506 static void *
2507 format_cb_window_marked_flag(struct format_tree *ft)
2509 if (ft->wl != NULL) {
2510 if (server_check_marked() && marked_pane.wl == ft->wl)
2511 return (xstrdup("1"));
2512 return (xstrdup("0"));
2514 return (NULL);
2517 /* Callback for window_name. */
2518 static void *
2519 format_cb_window_name(struct format_tree *ft)
2521 if (ft->w != NULL)
2522 return (format_printf("%s", ft->w->name));
2523 return (NULL);
2526 /* Callback for window_offset_x. */
2527 static void *
2528 format_cb_window_offset_x(struct format_tree *ft)
2530 u_int ox, oy, sx, sy;
2532 if (ft->c != NULL) {
2533 if (tty_window_offset(&ft->c->tty, &ox, &oy, &sx, &sy))
2534 return (format_printf("%u", ox));
2535 return (NULL);
2537 return (NULL);
2540 /* Callback for window_offset_y. */
2541 static void *
2542 format_cb_window_offset_y(struct format_tree *ft)
2544 u_int ox, oy, sx, sy;
2546 if (ft->c != NULL) {
2547 if (tty_window_offset(&ft->c->tty, &ox, &oy, &sx, &sy))
2548 return (format_printf("%u", oy));
2549 return (NULL);
2551 return (NULL);
2554 /* Callback for window_panes. */
2555 static void *
2556 format_cb_window_panes(struct format_tree *ft)
2558 if (ft->w != NULL)
2559 return (format_printf("%u", window_count_panes(ft->w)));
2560 return (NULL);
2563 /* Callback for window_raw_flags. */
2564 static void *
2565 format_cb_window_raw_flags(struct format_tree *ft)
2567 if (ft->wl != NULL)
2568 return (xstrdup(window_printable_flags(ft->wl, 0)));
2569 return (NULL);
2572 /* Callback for window_silence_flag. */
2573 static void *
2574 format_cb_window_silence_flag(struct format_tree *ft)
2576 if (ft->wl != NULL) {
2577 if (ft->wl->flags & WINLINK_SILENCE)
2578 return (xstrdup("1"));
2579 return (xstrdup("0"));
2581 return (NULL);
2584 /* Callback for window_start_flag. */
2585 static void *
2586 format_cb_window_start_flag(struct format_tree *ft)
2588 if (ft->wl != NULL) {
2589 if (ft->wl == RB_MIN(winlinks, &ft->wl->session->windows))
2590 return (xstrdup("1"));
2591 return (xstrdup("0"));
2593 return (NULL);
2596 /* Callback for window_width. */
2597 static void *
2598 format_cb_window_width(struct format_tree *ft)
2600 if (ft->w != NULL)
2601 return (format_printf("%u", ft->w->sx));
2602 return (NULL);
2605 /* Callback for window_zoomed_flag. */
2606 static void *
2607 format_cb_window_zoomed_flag(struct format_tree *ft)
2609 if (ft->w != NULL) {
2610 if (ft->w->flags & WINDOW_ZOOMED)
2611 return (xstrdup("1"));
2612 return (xstrdup("0"));
2614 return (NULL);
2617 /* Callback for wrap_flag. */
2618 static void *
2619 format_cb_wrap_flag(struct format_tree *ft)
2621 if (ft->wp != NULL) {
2622 if (ft->wp->base.mode & MODE_WRAP)
2623 return (xstrdup("1"));
2624 return (xstrdup("0"));
2626 return (NULL);
2629 /* Callback for buffer_created. */
2630 static void *
2631 format_cb_buffer_created(struct format_tree *ft)
2633 static struct timeval tv;
2635 if (ft->pb != NULL) {
2636 timerclear(&tv);
2637 tv.tv_sec = paste_buffer_created(ft->pb);
2638 return (&tv);
2640 return (NULL);
2643 /* Callback for client_activity. */
2644 static void *
2645 format_cb_client_activity(struct format_tree *ft)
2647 if (ft->c != NULL)
2648 return (&ft->c->activity_time);
2649 return (NULL);
2652 /* Callback for client_created. */
2653 static void *
2654 format_cb_client_created(struct format_tree *ft)
2656 if (ft->c != NULL)
2657 return (&ft->c->creation_time);
2658 return (NULL);
2661 /* Callback for session_activity. */
2662 static void *
2663 format_cb_session_activity(struct format_tree *ft)
2665 if (ft->s != NULL)
2666 return (&ft->s->activity_time);
2667 return (NULL);
2670 /* Callback for session_created. */
2671 static void *
2672 format_cb_session_created(struct format_tree *ft)
2674 if (ft->s != NULL)
2675 return (&ft->s->creation_time);
2676 return (NULL);
2679 /* Callback for session_last_attached. */
2680 static void *
2681 format_cb_session_last_attached(struct format_tree *ft)
2683 if (ft->s != NULL)
2684 return (&ft->s->last_attached_time);
2685 return (NULL);
2688 /* Callback for start_time. */
2689 static void *
2690 format_cb_start_time(__unused struct format_tree *ft)
2692 return (&start_time);
2695 /* Callback for window_activity. */
2696 static void *
2697 format_cb_window_activity(struct format_tree *ft)
2699 if (ft->w != NULL)
2700 return (&ft->w->activity_time);
2701 return (NULL);
2704 /* Callback for buffer_mode_format, */
2705 static void *
2706 format_cb_buffer_mode_format(__unused struct format_tree *ft)
2708 return (xstrdup(window_buffer_mode.default_format));
2711 /* Callback for client_mode_format, */
2712 static void *
2713 format_cb_client_mode_format(__unused struct format_tree *ft)
2715 return (xstrdup(window_client_mode.default_format));
2718 /* Callback for tree_mode_format, */
2719 static void *
2720 format_cb_tree_mode_format(__unused struct format_tree *ft)
2722 return (xstrdup(window_tree_mode.default_format));
2725 /* Callback for uid. */
2726 static void *
2727 format_cb_uid(__unused struct format_tree *ft)
2729 return (format_printf("%ld", (long)getuid()));
2732 /* Callback for user. */
2733 static void *
2734 format_cb_user(__unused struct format_tree *ft)
2736 struct passwd *pw;
2738 if ((pw = getpwuid(getuid())) != NULL)
2739 return (xstrdup(pw->pw_name));
2740 return (NULL);
2743 /* Format table type. */
2744 enum format_table_type {
2745 FORMAT_TABLE_STRING,
2746 FORMAT_TABLE_TIME
2749 /* Format table entry. */
2750 struct format_table_entry {
2751 const char *key;
2752 enum format_table_type type;
2753 format_cb cb;
2757 * Format table. Default format variables (that are almost always in the tree
2758 * and where the value is expanded by a callback in this file) are listed here.
2759 * Only variables which are added by the caller go into the tree.
2761 static const struct format_table_entry format_table[] = {
2762 { "active_window_index", FORMAT_TABLE_STRING,
2763 format_cb_active_window_index
2765 { "alternate_on", FORMAT_TABLE_STRING,
2766 format_cb_alternate_on
2768 { "alternate_saved_x", FORMAT_TABLE_STRING,
2769 format_cb_alternate_saved_x
2771 { "alternate_saved_y", FORMAT_TABLE_STRING,
2772 format_cb_alternate_saved_y
2774 { "buffer_created", FORMAT_TABLE_TIME,
2775 format_cb_buffer_created
2777 { "buffer_mode_format", FORMAT_TABLE_STRING,
2778 format_cb_buffer_mode_format
2780 { "buffer_name", FORMAT_TABLE_STRING,
2781 format_cb_buffer_name
2783 { "buffer_sample", FORMAT_TABLE_STRING,
2784 format_cb_buffer_sample
2786 { "buffer_size", FORMAT_TABLE_STRING,
2787 format_cb_buffer_size
2789 { "client_activity", FORMAT_TABLE_TIME,
2790 format_cb_client_activity
2792 { "client_cell_height", FORMAT_TABLE_STRING,
2793 format_cb_client_cell_height
2795 { "client_cell_width", FORMAT_TABLE_STRING,
2796 format_cb_client_cell_width
2798 { "client_control_mode", FORMAT_TABLE_STRING,
2799 format_cb_client_control_mode
2801 { "client_created", FORMAT_TABLE_TIME,
2802 format_cb_client_created
2804 { "client_discarded", FORMAT_TABLE_STRING,
2805 format_cb_client_discarded
2807 { "client_flags", FORMAT_TABLE_STRING,
2808 format_cb_client_flags
2810 { "client_height", FORMAT_TABLE_STRING,
2811 format_cb_client_height
2813 { "client_key_table", FORMAT_TABLE_STRING,
2814 format_cb_client_key_table
2816 { "client_last_session", FORMAT_TABLE_STRING,
2817 format_cb_client_last_session
2819 { "client_mode_format", FORMAT_TABLE_STRING,
2820 format_cb_client_mode_format
2822 { "client_name", FORMAT_TABLE_STRING,
2823 format_cb_client_name
2825 { "client_pid", FORMAT_TABLE_STRING,
2826 format_cb_client_pid
2828 { "client_prefix", FORMAT_TABLE_STRING,
2829 format_cb_client_prefix
2831 { "client_readonly", FORMAT_TABLE_STRING,
2832 format_cb_client_readonly
2834 { "client_session", FORMAT_TABLE_STRING,
2835 format_cb_client_session
2837 { "client_termfeatures", FORMAT_TABLE_STRING,
2838 format_cb_client_termfeatures
2840 { "client_termname", FORMAT_TABLE_STRING,
2841 format_cb_client_termname
2843 { "client_termtype", FORMAT_TABLE_STRING,
2844 format_cb_client_termtype
2846 { "client_tty", FORMAT_TABLE_STRING,
2847 format_cb_client_tty
2849 { "client_uid", FORMAT_TABLE_STRING,
2850 format_cb_client_uid
2852 { "client_user", FORMAT_TABLE_STRING,
2853 format_cb_client_user
2855 { "client_utf8", FORMAT_TABLE_STRING,
2856 format_cb_client_utf8
2858 { "client_width", FORMAT_TABLE_STRING,
2859 format_cb_client_width
2861 { "client_written", FORMAT_TABLE_STRING,
2862 format_cb_client_written
2864 { "config_files", FORMAT_TABLE_STRING,
2865 format_cb_config_files
2867 { "cursor_character", FORMAT_TABLE_STRING,
2868 format_cb_cursor_character
2870 { "cursor_flag", FORMAT_TABLE_STRING,
2871 format_cb_cursor_flag
2873 { "cursor_x", FORMAT_TABLE_STRING,
2874 format_cb_cursor_x
2876 { "cursor_y", FORMAT_TABLE_STRING,
2877 format_cb_cursor_y
2879 { "history_all_bytes", FORMAT_TABLE_STRING,
2880 format_cb_history_all_bytes
2882 { "history_bytes", FORMAT_TABLE_STRING,
2883 format_cb_history_bytes
2885 { "history_limit", FORMAT_TABLE_STRING,
2886 format_cb_history_limit
2888 { "history_size", FORMAT_TABLE_STRING,
2889 format_cb_history_size
2891 { "host", FORMAT_TABLE_STRING,
2892 format_cb_host
2894 { "host_short", FORMAT_TABLE_STRING,
2895 format_cb_host_short
2897 { "insert_flag", FORMAT_TABLE_STRING,
2898 format_cb_insert_flag
2900 { "keypad_cursor_flag", FORMAT_TABLE_STRING,
2901 format_cb_keypad_cursor_flag
2903 { "keypad_flag", FORMAT_TABLE_STRING,
2904 format_cb_keypad_flag
2906 { "last_window_index", FORMAT_TABLE_STRING,
2907 format_cb_last_window_index
2909 { "mouse_all_flag", FORMAT_TABLE_STRING,
2910 format_cb_mouse_all_flag
2912 { "mouse_any_flag", FORMAT_TABLE_STRING,
2913 format_cb_mouse_any_flag
2915 { "mouse_button_flag", FORMAT_TABLE_STRING,
2916 format_cb_mouse_button_flag
2918 { "mouse_hyperlink", FORMAT_TABLE_STRING,
2919 format_cb_mouse_hyperlink
2921 { "mouse_line", FORMAT_TABLE_STRING,
2922 format_cb_mouse_line
2924 { "mouse_pane", FORMAT_TABLE_STRING,
2925 format_cb_mouse_pane
2927 { "mouse_sgr_flag", FORMAT_TABLE_STRING,
2928 format_cb_mouse_sgr_flag
2930 { "mouse_standard_flag", FORMAT_TABLE_STRING,
2931 format_cb_mouse_standard_flag
2933 { "mouse_status_line", FORMAT_TABLE_STRING,
2934 format_cb_mouse_status_line
2936 { "mouse_status_range", FORMAT_TABLE_STRING,
2937 format_cb_mouse_status_range
2939 { "mouse_utf8_flag", FORMAT_TABLE_STRING,
2940 format_cb_mouse_utf8_flag
2942 { "mouse_word", FORMAT_TABLE_STRING,
2943 format_cb_mouse_word
2945 { "mouse_x", FORMAT_TABLE_STRING,
2946 format_cb_mouse_x
2948 { "mouse_y", FORMAT_TABLE_STRING,
2949 format_cb_mouse_y
2951 { "next_session_id", FORMAT_TABLE_STRING,
2952 format_cb_next_session_id
2954 { "origin_flag", FORMAT_TABLE_STRING,
2955 format_cb_origin_flag
2957 { "pane_active", FORMAT_TABLE_STRING,
2958 format_cb_pane_active
2960 { "pane_at_bottom", FORMAT_TABLE_STRING,
2961 format_cb_pane_at_bottom
2963 { "pane_at_left", FORMAT_TABLE_STRING,
2964 format_cb_pane_at_left
2966 { "pane_at_right", FORMAT_TABLE_STRING,
2967 format_cb_pane_at_right
2969 { "pane_at_top", FORMAT_TABLE_STRING,
2970 format_cb_pane_at_top
2972 { "pane_bg", FORMAT_TABLE_STRING,
2973 format_cb_pane_bg
2975 { "pane_bottom", FORMAT_TABLE_STRING,
2976 format_cb_pane_bottom
2978 { "pane_current_command", FORMAT_TABLE_STRING,
2979 format_cb_current_command
2981 { "pane_current_path", FORMAT_TABLE_STRING,
2982 format_cb_current_path
2984 { "pane_dead", FORMAT_TABLE_STRING,
2985 format_cb_pane_dead
2987 { "pane_dead_signal", FORMAT_TABLE_STRING,
2988 format_cb_pane_dead_signal
2990 { "pane_dead_status", FORMAT_TABLE_STRING,
2991 format_cb_pane_dead_status
2993 { "pane_dead_time", FORMAT_TABLE_TIME,
2994 format_cb_pane_dead_time
2996 { "pane_fg", FORMAT_TABLE_STRING,
2997 format_cb_pane_fg
2999 { "pane_format", FORMAT_TABLE_STRING,
3000 format_cb_pane_format
3002 { "pane_height", FORMAT_TABLE_STRING,
3003 format_cb_pane_height
3005 { "pane_id", FORMAT_TABLE_STRING,
3006 format_cb_pane_id
3008 { "pane_in_mode", FORMAT_TABLE_STRING,
3009 format_cb_pane_in_mode
3011 { "pane_index", FORMAT_TABLE_STRING,
3012 format_cb_pane_index
3014 { "pane_input_off", FORMAT_TABLE_STRING,
3015 format_cb_pane_input_off
3017 { "pane_key_mode", FORMAT_TABLE_STRING,
3018 format_cb_pane_key_mode
3020 { "pane_last", FORMAT_TABLE_STRING,
3021 format_cb_pane_last
3023 { "pane_left", FORMAT_TABLE_STRING,
3024 format_cb_pane_left
3026 { "pane_marked", FORMAT_TABLE_STRING,
3027 format_cb_pane_marked
3029 { "pane_marked_set", FORMAT_TABLE_STRING,
3030 format_cb_pane_marked_set
3032 { "pane_mode", FORMAT_TABLE_STRING,
3033 format_cb_pane_mode
3035 { "pane_path", FORMAT_TABLE_STRING,
3036 format_cb_pane_path
3038 { "pane_pid", FORMAT_TABLE_STRING,
3039 format_cb_pane_pid
3041 { "pane_pipe", FORMAT_TABLE_STRING,
3042 format_cb_pane_pipe
3044 { "pane_right", FORMAT_TABLE_STRING,
3045 format_cb_pane_right
3047 { "pane_search_string", FORMAT_TABLE_STRING,
3048 format_cb_pane_search_string
3050 { "pane_start_command", FORMAT_TABLE_STRING,
3051 format_cb_start_command
3053 { "pane_start_path", FORMAT_TABLE_STRING,
3054 format_cb_start_path
3056 { "pane_synchronized", FORMAT_TABLE_STRING,
3057 format_cb_pane_synchronized
3059 { "pane_tabs", FORMAT_TABLE_STRING,
3060 format_cb_pane_tabs
3062 { "pane_title", FORMAT_TABLE_STRING,
3063 format_cb_pane_title
3065 { "pane_top", FORMAT_TABLE_STRING,
3066 format_cb_pane_top
3068 { "pane_tty", FORMAT_TABLE_STRING,
3069 format_cb_pane_tty
3071 { "pane_unseen_changes", FORMAT_TABLE_STRING,
3072 format_cb_pane_unseen_changes
3074 { "pane_width", FORMAT_TABLE_STRING,
3075 format_cb_pane_width
3077 { "pid", FORMAT_TABLE_STRING,
3078 format_cb_pid
3080 { "scroll_region_lower", FORMAT_TABLE_STRING,
3081 format_cb_scroll_region_lower
3083 { "scroll_region_upper", FORMAT_TABLE_STRING,
3084 format_cb_scroll_region_upper
3086 { "server_sessions", FORMAT_TABLE_STRING,
3087 format_cb_server_sessions
3089 { "session_activity", FORMAT_TABLE_TIME,
3090 format_cb_session_activity
3092 { "session_alerts", FORMAT_TABLE_STRING,
3093 format_cb_session_alerts
3095 { "session_attached", FORMAT_TABLE_STRING,
3096 format_cb_session_attached
3098 { "session_attached_list", FORMAT_TABLE_STRING,
3099 format_cb_session_attached_list
3101 { "session_created", FORMAT_TABLE_TIME,
3102 format_cb_session_created
3104 { "session_format", FORMAT_TABLE_STRING,
3105 format_cb_session_format
3107 { "session_group", FORMAT_TABLE_STRING,
3108 format_cb_session_group
3110 { "session_group_attached", FORMAT_TABLE_STRING,
3111 format_cb_session_group_attached
3113 { "session_group_attached_list", FORMAT_TABLE_STRING,
3114 format_cb_session_group_attached_list
3116 { "session_group_list", FORMAT_TABLE_STRING,
3117 format_cb_session_group_list
3119 { "session_group_many_attached", FORMAT_TABLE_STRING,
3120 format_cb_session_group_many_attached
3122 { "session_group_size", FORMAT_TABLE_STRING,
3123 format_cb_session_group_size
3125 { "session_grouped", FORMAT_TABLE_STRING,
3126 format_cb_session_grouped
3128 { "session_id", FORMAT_TABLE_STRING,
3129 format_cb_session_id
3131 { "session_last_attached", FORMAT_TABLE_TIME,
3132 format_cb_session_last_attached
3134 { "session_many_attached", FORMAT_TABLE_STRING,
3135 format_cb_session_many_attached
3137 { "session_marked", FORMAT_TABLE_STRING,
3138 format_cb_session_marked,
3140 { "session_name", FORMAT_TABLE_STRING,
3141 format_cb_session_name
3143 { "session_path", FORMAT_TABLE_STRING,
3144 format_cb_session_path
3146 { "session_stack", FORMAT_TABLE_STRING,
3147 format_cb_session_stack
3149 { "session_windows", FORMAT_TABLE_STRING,
3150 format_cb_session_windows
3152 { "socket_path", FORMAT_TABLE_STRING,
3153 format_cb_socket_path
3155 { "start_time", FORMAT_TABLE_TIME,
3156 format_cb_start_time
3158 { "tree_mode_format", FORMAT_TABLE_STRING,
3159 format_cb_tree_mode_format
3161 { "uid", FORMAT_TABLE_STRING,
3162 format_cb_uid
3164 { "user", FORMAT_TABLE_STRING,
3165 format_cb_user
3167 { "version", FORMAT_TABLE_STRING,
3168 format_cb_version
3170 { "window_active", FORMAT_TABLE_STRING,
3171 format_cb_window_active
3173 { "window_active_clients", FORMAT_TABLE_STRING,
3174 format_cb_window_active_clients
3176 { "window_active_clients_list", FORMAT_TABLE_STRING,
3177 format_cb_window_active_clients_list
3179 { "window_active_sessions", FORMAT_TABLE_STRING,
3180 format_cb_window_active_sessions
3182 { "window_active_sessions_list", FORMAT_TABLE_STRING,
3183 format_cb_window_active_sessions_list
3185 { "window_activity", FORMAT_TABLE_TIME,
3186 format_cb_window_activity
3188 { "window_activity_flag", FORMAT_TABLE_STRING,
3189 format_cb_window_activity_flag
3191 { "window_bell_flag", FORMAT_TABLE_STRING,
3192 format_cb_window_bell_flag
3194 { "window_bigger", FORMAT_TABLE_STRING,
3195 format_cb_window_bigger
3197 { "window_cell_height", FORMAT_TABLE_STRING,
3198 format_cb_window_cell_height
3200 { "window_cell_width", FORMAT_TABLE_STRING,
3201 format_cb_window_cell_width
3203 { "window_end_flag", FORMAT_TABLE_STRING,
3204 format_cb_window_end_flag
3206 { "window_flags", FORMAT_TABLE_STRING,
3207 format_cb_window_flags
3209 { "window_format", FORMAT_TABLE_STRING,
3210 format_cb_window_format
3212 { "window_height", FORMAT_TABLE_STRING,
3213 format_cb_window_height
3215 { "window_id", FORMAT_TABLE_STRING,
3216 format_cb_window_id
3218 { "window_index", FORMAT_TABLE_STRING,
3219 format_cb_window_index
3221 { "window_last_flag", FORMAT_TABLE_STRING,
3222 format_cb_window_last_flag
3224 { "window_layout", FORMAT_TABLE_STRING,
3225 format_cb_window_layout
3227 { "window_linked", FORMAT_TABLE_STRING,
3228 format_cb_window_linked
3230 { "window_linked_sessions", FORMAT_TABLE_STRING,
3231 format_cb_window_linked_sessions
3233 { "window_linked_sessions_list", FORMAT_TABLE_STRING,
3234 format_cb_window_linked_sessions_list
3236 { "window_marked_flag", FORMAT_TABLE_STRING,
3237 format_cb_window_marked_flag
3239 { "window_name", FORMAT_TABLE_STRING,
3240 format_cb_window_name
3242 { "window_offset_x", FORMAT_TABLE_STRING,
3243 format_cb_window_offset_x
3245 { "window_offset_y", FORMAT_TABLE_STRING,
3246 format_cb_window_offset_y
3248 { "window_panes", FORMAT_TABLE_STRING,
3249 format_cb_window_panes
3251 { "window_raw_flags", FORMAT_TABLE_STRING,
3252 format_cb_window_raw_flags
3254 { "window_silence_flag", FORMAT_TABLE_STRING,
3255 format_cb_window_silence_flag
3257 { "window_stack_index", FORMAT_TABLE_STRING,
3258 format_cb_window_stack_index
3260 { "window_start_flag", FORMAT_TABLE_STRING,
3261 format_cb_window_start_flag
3263 { "window_visible_layout", FORMAT_TABLE_STRING,
3264 format_cb_window_visible_layout
3266 { "window_width", FORMAT_TABLE_STRING,
3267 format_cb_window_width
3269 { "window_zoomed_flag", FORMAT_TABLE_STRING,
3270 format_cb_window_zoomed_flag
3272 { "wrap_flag", FORMAT_TABLE_STRING,
3273 format_cb_wrap_flag
3277 /* Compare format table entries. */
3278 static int
3279 format_table_compare(const void *key0, const void *entry0)
3281 const char *key = key0;
3282 const struct format_table_entry *entry = entry0;
3284 return (strcmp(key, entry->key));
3287 /* Get a format callback. */
3288 static struct format_table_entry *
3289 format_table_get(const char *key)
3291 return (bsearch(key, format_table, nitems(format_table),
3292 sizeof *format_table, format_table_compare));
3295 /* Merge one format tree into another. */
3296 void
3297 format_merge(struct format_tree *ft, struct format_tree *from)
3299 struct format_entry *fe;
3301 RB_FOREACH(fe, format_entry_tree, &from->tree) {
3302 if (fe->value != NULL)
3303 format_add(ft, fe->key, "%s", fe->value);
3307 /* Get format pane. */
3308 struct window_pane *
3309 format_get_pane(struct format_tree *ft)
3311 return (ft->wp);
3314 /* Add item bits to tree. */
3315 static void
3316 format_create_add_item(struct format_tree *ft, struct cmdq_item *item)
3318 struct key_event *event = cmdq_get_event(item);
3319 struct mouse_event *m = &event->m;
3321 cmdq_merge_formats(item, ft);
3322 memcpy(&ft->m, m, sizeof ft->m);
3325 /* Create a new tree. */
3326 struct format_tree *
3327 format_create(struct client *c, struct cmdq_item *item, int tag, int flags)
3329 struct format_tree *ft;
3331 ft = xcalloc(1, sizeof *ft);
3332 RB_INIT(&ft->tree);
3334 if (c != NULL) {
3335 ft->client = c;
3336 ft->client->references++;
3338 ft->item = item;
3340 ft->tag = tag;
3341 ft->flags = flags;
3343 if (item != NULL)
3344 format_create_add_item(ft, item);
3346 return (ft);
3349 /* Free a tree. */
3350 void
3351 format_free(struct format_tree *ft)
3353 struct format_entry *fe, *fe1;
3355 RB_FOREACH_SAFE(fe, format_entry_tree, &ft->tree, fe1) {
3356 RB_REMOVE(format_entry_tree, &ft->tree, fe);
3357 free(fe->value);
3358 free(fe->key);
3359 free(fe);
3362 if (ft->client != NULL)
3363 server_client_unref(ft->client);
3364 free(ft);
3367 /* Log each format. */
3368 static void
3369 format_log_debug_cb(const char *key, const char *value, void *arg)
3371 const char *prefix = arg;
3373 log_debug("%s: %s=%s", prefix, key, value);
3376 /* Log a format tree. */
3377 void
3378 format_log_debug(struct format_tree *ft, const char *prefix)
3380 format_each(ft, format_log_debug_cb, (void *)prefix);
3383 /* Walk each format. */
3384 void
3385 format_each(struct format_tree *ft, void (*cb)(const char *, const char *,
3386 void *), void *arg)
3388 const struct format_table_entry *fte;
3389 struct format_entry *fe;
3390 u_int i;
3391 char s[64];
3392 void *value;
3393 struct timeval *tv;
3395 for (i = 0; i < nitems(format_table); i++) {
3396 fte = &format_table[i];
3398 value = fte->cb(ft);
3399 if (value == NULL)
3400 continue;
3401 if (fte->type == FORMAT_TABLE_TIME) {
3402 tv = value;
3403 xsnprintf(s, sizeof s, "%lld", (long long)tv->tv_sec);
3404 cb(fte->key, s, arg);
3405 } else {
3406 cb(fte->key, value, arg);
3407 free(value);
3410 RB_FOREACH(fe, format_entry_tree, &ft->tree) {
3411 if (fe->time != 0) {
3412 xsnprintf(s, sizeof s, "%lld", (long long)fe->time);
3413 cb(fe->key, s, arg);
3414 } else {
3415 if (fe->value == NULL && fe->cb != NULL) {
3416 fe->value = fe->cb(ft);
3417 if (fe->value == NULL)
3418 fe->value = xstrdup("");
3420 cb(fe->key, fe->value, arg);
3425 /* Add a key-value pair. */
3426 void
3427 format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
3429 struct format_entry *fe;
3430 struct format_entry *fe_now;
3431 va_list ap;
3433 fe = xmalloc(sizeof *fe);
3434 fe->key = xstrdup(key);
3436 fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
3437 if (fe_now != NULL) {
3438 free(fe->key);
3439 free(fe);
3440 free(fe_now->value);
3441 fe = fe_now;
3444 fe->cb = NULL;
3445 fe->time = 0;
3447 va_start(ap, fmt);
3448 xvasprintf(&fe->value, fmt, ap);
3449 va_end(ap);
3452 /* Add a key and time. */
3453 void
3454 format_add_tv(struct format_tree *ft, const char *key, struct timeval *tv)
3456 struct format_entry *fe, *fe_now;
3458 fe = xmalloc(sizeof *fe);
3459 fe->key = xstrdup(key);
3461 fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
3462 if (fe_now != NULL) {
3463 free(fe->key);
3464 free(fe);
3465 free(fe_now->value);
3466 fe = fe_now;
3469 fe->cb = NULL;
3470 fe->time = tv->tv_sec;
3472 fe->value = NULL;
3475 /* Add a key and function. */
3476 void
3477 format_add_cb(struct format_tree *ft, const char *key, format_cb cb)
3479 struct format_entry *fe;
3480 struct format_entry *fe_now;
3482 fe = xmalloc(sizeof *fe);
3483 fe->key = xstrdup(key);
3485 fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
3486 if (fe_now != NULL) {
3487 free(fe->key);
3488 free(fe);
3489 free(fe_now->value);
3490 fe = fe_now;
3493 fe->cb = cb;
3494 fe->time = 0;
3496 fe->value = NULL;
3499 /* Quote shell special characters in string. */
3500 static char *
3501 format_quote_shell(const char *s)
3503 const char *cp;
3504 char *out, *at;
3506 at = out = xmalloc(strlen(s) * 2 + 1);
3507 for (cp = s; *cp != '\0'; cp++) {
3508 if (strchr("|&;<>()$`\\\"'*?[# =%", *cp) != NULL)
3509 *at++ = '\\';
3510 *at++ = *cp;
3512 *at = '\0';
3513 return (out);
3516 /* Quote #s in string. */
3517 static char *
3518 format_quote_style(const char *s)
3520 const char *cp;
3521 char *out, *at;
3523 at = out = xmalloc(strlen(s) * 2 + 1);
3524 for (cp = s; *cp != '\0'; cp++) {
3525 if (*cp == '#')
3526 *at++ = '#';
3527 *at++ = *cp;
3529 *at = '\0';
3530 return (out);
3533 /* Make a prettier time. */
3534 char *
3535 format_pretty_time(time_t t, int seconds)
3537 struct tm now_tm, tm;
3538 time_t now, age;
3539 char s[9];
3541 time(&now);
3542 if (now < t)
3543 now = t;
3544 age = now - t;
3546 localtime_r(&now, &now_tm);
3547 localtime_r(&t, &tm);
3549 /* Last 24 hours. */
3550 if (age < 24 * 3600) {
3551 if (seconds)
3552 strftime(s, sizeof s, "%H:%M:%S", &tm);
3553 else
3554 strftime(s, sizeof s, "%H:%M", &tm);
3555 return (xstrdup(s));
3558 /* This month or last 28 days. */
3559 if ((tm.tm_year == now_tm.tm_year && tm.tm_mon == now_tm.tm_mon) ||
3560 age < 28 * 24 * 3600) {
3561 strftime(s, sizeof s, "%a%d", &tm);
3562 return (xstrdup(s));
3565 /* Last 12 months. */
3566 if ((tm.tm_year == now_tm.tm_year && tm.tm_mon < now_tm.tm_mon) ||
3567 (tm.tm_year == now_tm.tm_year - 1 && tm.tm_mon > now_tm.tm_mon)) {
3568 strftime(s, sizeof s, "%d%b", &tm);
3569 return (xstrdup(s));
3572 /* Older than that. */
3573 strftime(s, sizeof s, "%h%y", &tm);
3574 return (xstrdup(s));
3577 /* Find a format entry. */
3578 static char *
3579 format_find(struct format_tree *ft, const char *key, int modifiers,
3580 const char *time_format)
3582 struct format_table_entry *fte;
3583 void *value;
3584 struct format_entry *fe, fe_find;
3585 struct environ_entry *envent;
3586 struct options_entry *o;
3587 int idx;
3588 char *found = NULL, *saved, s[512];
3589 const char *errstr;
3590 time_t t = 0;
3591 struct tm tm;
3593 o = options_parse_get(global_options, key, &idx, 0);
3594 if (o == NULL && ft->wp != NULL)
3595 o = options_parse_get(ft->wp->options, key, &idx, 0);
3596 if (o == NULL && ft->w != NULL)
3597 o = options_parse_get(ft->w->options, key, &idx, 0);
3598 if (o == NULL)
3599 o = options_parse_get(global_w_options, key, &idx, 0);
3600 if (o == NULL && ft->s != NULL)
3601 o = options_parse_get(ft->s->options, key, &idx, 0);
3602 if (o == NULL)
3603 o = options_parse_get(global_s_options, key, &idx, 0);
3604 if (o != NULL) {
3605 found = options_to_string(o, idx, 1);
3606 goto found;
3609 fte = format_table_get(key);
3610 if (fte != NULL) {
3611 value = fte->cb(ft);
3612 if (fte->type == FORMAT_TABLE_TIME && value != NULL)
3613 t = ((struct timeval *)value)->tv_sec;
3614 else
3615 found = value;
3616 goto found;
3618 fe_find.key = (char *)key;
3619 fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find);
3620 if (fe != NULL) {
3621 if (fe->time != 0) {
3622 t = fe->time;
3623 goto found;
3625 if (fe->value == NULL && fe->cb != NULL) {
3626 fe->value = fe->cb(ft);
3627 if (fe->value == NULL)
3628 fe->value = xstrdup("");
3630 found = xstrdup(fe->value);
3631 goto found;
3634 if (~modifiers & FORMAT_TIMESTRING) {
3635 envent = NULL;
3636 if (ft->s != NULL)
3637 envent = environ_find(ft->s->environ, key);
3638 if (envent == NULL)
3639 envent = environ_find(global_environ, key);
3640 if (envent != NULL && envent->value != NULL) {
3641 found = xstrdup(envent->value);
3642 goto found;
3646 return (NULL);
3648 found:
3649 if (modifiers & FORMAT_TIMESTRING) {
3650 if (t == 0 && found != NULL) {
3651 t = strtonum(found, 0, INT64_MAX, &errstr);
3652 if (errstr != NULL)
3653 t = 0;
3654 free(found);
3656 if (t == 0)
3657 return (NULL);
3658 if (modifiers & FORMAT_PRETTY)
3659 found = format_pretty_time(t, 0);
3660 else {
3661 if (time_format != NULL) {
3662 localtime_r(&t, &tm);
3663 strftime(s, sizeof s, time_format, &tm);
3664 } else {
3665 ctime_r(&t, s);
3666 s[strcspn(s, "\n")] = '\0';
3668 found = xstrdup(s);
3670 return (found);
3673 if (t != 0)
3674 xasprintf(&found, "%lld", (long long)t);
3675 else if (found == NULL)
3676 return (NULL);
3677 if (modifiers & FORMAT_BASENAME) {
3678 saved = found;
3679 found = xstrdup(basename(saved));
3680 free(saved);
3682 if (modifiers & FORMAT_DIRNAME) {
3683 saved = found;
3684 found = xstrdup(dirname(saved));
3685 free(saved);
3687 if (modifiers & FORMAT_QUOTE_SHELL) {
3688 saved = found;
3689 found = format_quote_shell(saved);
3690 free(saved);
3692 if (modifiers & FORMAT_QUOTE_STYLE) {
3693 saved = found;
3694 found = format_quote_style(saved);
3695 free(saved);
3697 return (found);
3700 /* Unescape escaped characters. */
3701 static char *
3702 format_unescape(const char *s)
3704 char *out, *cp;
3705 int brackets = 0;
3707 cp = out = xmalloc(strlen(s) + 1);
3708 for (; *s != '\0'; s++) {
3709 if (*s == '#' && s[1] == '{')
3710 brackets++;
3711 if (brackets == 0 &&
3712 *s == '#' &&
3713 strchr(",#{}:", s[1]) != NULL) {
3714 *cp++ = *++s;
3715 continue;
3717 if (*s == '}')
3718 brackets--;
3719 *cp++ = *s;
3721 *cp = '\0';
3722 return (out);
3725 /* Remove escaped characters. */
3726 static char *
3727 format_strip(const char *s)
3729 char *out, *cp;
3730 int brackets = 0;
3732 cp = out = xmalloc(strlen(s) + 1);
3733 for (; *s != '\0'; s++) {
3734 if (*s == '#' && s[1] == '{')
3735 brackets++;
3736 if (*s == '#' && strchr(",#{}:", s[1]) != NULL) {
3737 if (brackets != 0)
3738 *cp++ = *s;
3739 continue;
3741 if (*s == '}')
3742 brackets--;
3743 *cp++ = *s;
3745 *cp = '\0';
3746 return (out);
3749 /* Skip until end. */
3750 const char *
3751 format_skip(const char *s, const char *end)
3753 int brackets = 0;
3755 for (; *s != '\0'; s++) {
3756 if (*s == '#' && s[1] == '{')
3757 brackets++;
3758 if (*s == '#' &&
3759 s[1] != '\0' &&
3760 strchr(",#{}:", s[1]) != NULL) {
3761 s++;
3762 continue;
3764 if (*s == '}')
3765 brackets--;
3766 if (strchr(end, *s) != NULL && brackets == 0)
3767 break;
3769 if (*s == '\0')
3770 return (NULL);
3771 return (s);
3774 /* Return left and right alternatives separated by commas. */
3775 static int
3776 format_choose(struct format_expand_state *es, const char *s, char **left,
3777 char **right, int expand)
3779 const char *cp;
3780 char *left0, *right0;
3782 cp = format_skip(s, ",");
3783 if (cp == NULL)
3784 return (-1);
3785 left0 = xstrndup(s, cp - s);
3786 right0 = xstrdup(cp + 1);
3788 if (expand) {
3789 *left = format_expand1(es, left0);
3790 free(left0);
3791 *right = format_expand1(es, right0);
3792 free(right0);
3793 } else {
3794 *left = left0;
3795 *right = right0;
3797 return (0);
3800 /* Is this true? */
3802 format_true(const char *s)
3804 if (s != NULL && *s != '\0' && (s[0] != '0' || s[1] != '\0'))
3805 return (1);
3806 return (0);
3809 /* Check if modifier end. */
3810 static int
3811 format_is_end(char c)
3813 return (c == ';' || c == ':');
3816 /* Add to modifier list. */
3817 static void
3818 format_add_modifier(struct format_modifier **list, u_int *count,
3819 const char *c, size_t n, char **argv, int argc)
3821 struct format_modifier *fm;
3823 *list = xreallocarray(*list, (*count) + 1, sizeof **list);
3824 fm = &(*list)[(*count)++];
3826 memcpy(fm->modifier, c, n);
3827 fm->modifier[n] = '\0';
3828 fm->size = n;
3830 fm->argv = argv;
3831 fm->argc = argc;
3834 /* Free modifier list. */
3835 static void
3836 format_free_modifiers(struct format_modifier *list, u_int count)
3838 u_int i;
3840 for (i = 0; i < count; i++)
3841 cmd_free_argv(list[i].argc, list[i].argv);
3842 free(list);
3845 /* Build modifier list. */
3846 static struct format_modifier *
3847 format_build_modifiers(struct format_expand_state *es, const char **s,
3848 u_int *count)
3850 const char *cp = *s, *end;
3851 struct format_modifier *list = NULL;
3852 char c, last[] = "X;:", **argv, *value;
3853 int argc;
3856 * Modifiers are a ; separated list of the forms:
3857 * l,m,C,a,b,c,d,n,t,w,q,E,T,S,W,P,<,>
3858 * =a
3859 * =/a
3860 * =/a/
3861 * s/a/b/
3862 * s/a/b
3863 * ||,&&,!=,==,<=,>=
3866 *count = 0;
3868 while (*cp != '\0' && *cp != ':') {
3869 /* Skip any separator character. */
3870 if (*cp == ';')
3871 cp++;
3873 /* Check single character modifiers with no arguments. */
3874 if (strchr("labcdnwETSWPL<>", cp[0]) != NULL &&
3875 format_is_end(cp[1])) {
3876 format_add_modifier(&list, count, cp, 1, NULL, 0);
3877 cp++;
3878 continue;
3881 /* Then try double character with no arguments. */
3882 if ((memcmp("||", cp, 2) == 0 ||
3883 memcmp("&&", cp, 2) == 0 ||
3884 memcmp("!=", cp, 2) == 0 ||
3885 memcmp("==", cp, 2) == 0 ||
3886 memcmp("<=", cp, 2) == 0 ||
3887 memcmp(">=", cp, 2) == 0) &&
3888 format_is_end(cp[2])) {
3889 format_add_modifier(&list, count, cp, 2, NULL, 0);
3890 cp += 2;
3891 continue;
3894 /* Now try single character with arguments. */
3895 if (strchr("mCNst=peq", cp[0]) == NULL)
3896 break;
3897 c = cp[0];
3899 /* No arguments provided. */
3900 if (format_is_end(cp[1])) {
3901 format_add_modifier(&list, count, cp, 1, NULL, 0);
3902 cp++;
3903 continue;
3905 argv = NULL;
3906 argc = 0;
3908 /* Single argument with no wrapper character. */
3909 if (!ispunct((u_char)cp[1]) || cp[1] == '-') {
3910 end = format_skip(cp + 1, ":;");
3911 if (end == NULL)
3912 break;
3914 argv = xcalloc(1, sizeof *argv);
3915 value = xstrndup(cp + 1, end - (cp + 1));
3916 argv[0] = format_expand1(es, value);
3917 free(value);
3918 argc = 1;
3920 format_add_modifier(&list, count, &c, 1, argv, argc);
3921 cp = end;
3922 continue;
3925 /* Multiple arguments with a wrapper character. */
3926 last[0] = cp[1];
3927 cp++;
3928 do {
3929 if (cp[0] == last[0] && format_is_end(cp[1])) {
3930 cp++;
3931 break;
3933 end = format_skip(cp + 1, last);
3934 if (end == NULL)
3935 break;
3936 cp++;
3938 argv = xreallocarray(argv, argc + 1, sizeof *argv);
3939 value = xstrndup(cp, end - cp);
3940 argv[argc++] = format_expand1(es, value);
3941 free(value);
3943 cp = end;
3944 } while (!format_is_end(cp[0]));
3945 format_add_modifier(&list, count, &c, 1, argv, argc);
3947 if (*cp != ':') {
3948 format_free_modifiers(list, *count);
3949 *count = 0;
3950 return (NULL);
3952 *s = cp + 1;
3953 return (list);
3956 /* Match against an fnmatch(3) pattern or regular expression. */
3957 static char *
3958 format_match(struct format_modifier *fm, const char *pattern, const char *text)
3960 const char *s = "";
3961 regex_t r;
3962 int flags = 0;
3964 if (fm->argc >= 1)
3965 s = fm->argv[0];
3966 if (strchr(s, 'r') == NULL) {
3967 if (strchr(s, 'i') != NULL)
3968 flags |= FNM_CASEFOLD;
3969 if (fnmatch(pattern, text, flags) != 0)
3970 return (xstrdup("0"));
3971 } else {
3972 flags = REG_EXTENDED|REG_NOSUB;
3973 if (strchr(s, 'i') != NULL)
3974 flags |= REG_ICASE;
3975 if (regcomp(&r, pattern, flags) != 0)
3976 return (xstrdup("0"));
3977 if (regexec(&r, text, 0, NULL, 0) != 0) {
3978 regfree(&r);
3979 return (xstrdup("0"));
3981 regfree(&r);
3983 return (xstrdup("1"));
3986 /* Perform substitution in string. */
3987 static char *
3988 format_sub(struct format_modifier *fm, const char *text, const char *pattern,
3989 const char *with)
3991 char *value;
3992 int flags = REG_EXTENDED;
3994 if (fm->argc >= 3 && strchr(fm->argv[2], 'i') != NULL)
3995 flags |= REG_ICASE;
3996 value = regsub(pattern, with, text, flags);
3997 if (value == NULL)
3998 return (xstrdup(text));
3999 return (value);
4002 /* Search inside pane. */
4003 static char *
4004 format_search(struct format_modifier *fm, struct window_pane *wp, const char *s)
4006 int ignore = 0, regex = 0;
4007 char *value;
4009 if (fm->argc >= 1) {
4010 if (strchr(fm->argv[0], 'i') != NULL)
4011 ignore = 1;
4012 if (strchr(fm->argv[0], 'r') != NULL)
4013 regex = 1;
4015 xasprintf(&value, "%u", window_pane_search(wp, s, regex, ignore));
4016 return (value);
4019 /* Does session name exist? */
4020 static char *
4021 format_session_name(struct format_expand_state *es, const char *fmt)
4023 char *name;
4024 struct session *s;
4026 name = format_expand1(es, fmt);
4027 RB_FOREACH(s, sessions, &sessions) {
4028 if (strcmp(s->name, name) == 0) {
4029 free(name);
4030 return (xstrdup("1"));
4033 free(name);
4034 return (xstrdup("0"));
4037 /* Loop over sessions. */
4038 static char *
4039 format_loop_sessions(struct format_expand_state *es, const char *fmt)
4041 struct format_tree *ft = es->ft;
4042 struct client *c = ft->client;
4043 struct cmdq_item *item = ft->item;
4044 struct format_tree *nft;
4045 struct format_expand_state next;
4046 char *expanded, *value;
4047 size_t valuelen;
4048 struct session *s;
4050 value = xcalloc(1, 1);
4051 valuelen = 1;
4053 RB_FOREACH(s, sessions, &sessions) {
4054 format_log(es, "session loop: $%u", s->id);
4055 nft = format_create(c, item, FORMAT_NONE, ft->flags);
4056 format_defaults(nft, ft->c, s, NULL, NULL);
4057 format_copy_state(&next, es, 0);
4058 next.ft = nft;
4059 expanded = format_expand1(&next, fmt);
4060 format_free(next.ft);
4062 valuelen += strlen(expanded);
4063 value = xrealloc(value, valuelen);
4065 strlcat(value, expanded, valuelen);
4066 free(expanded);
4069 return (value);
4072 /* Does window name exist? */
4073 static char *
4074 format_window_name(struct format_expand_state *es, const char *fmt)
4076 struct format_tree *ft = es->ft;
4077 char *name;
4078 struct winlink *wl;
4080 if (ft->s == NULL) {
4081 format_log(es, "window name but no session");
4082 return (NULL);
4085 name = format_expand1(es, fmt);
4086 RB_FOREACH(wl, winlinks, &ft->s->windows) {
4087 if (strcmp(wl->window->name, name) == 0) {
4088 free(name);
4089 return (xstrdup("1"));
4092 free(name);
4093 return (xstrdup("0"));
4096 /* Loop over windows. */
4097 static char *
4098 format_loop_windows(struct format_expand_state *es, const char *fmt)
4100 struct format_tree *ft = es->ft;
4101 struct client *c = ft->client;
4102 struct cmdq_item *item = ft->item;
4103 struct format_tree *nft;
4104 struct format_expand_state next;
4105 char *all, *active, *use, *expanded, *value;
4106 size_t valuelen;
4107 struct winlink *wl;
4108 struct window *w;
4110 if (ft->s == NULL) {
4111 format_log(es, "window loop but no session");
4112 return (NULL);
4115 if (format_choose(es, fmt, &all, &active, 0) != 0) {
4116 all = xstrdup(fmt);
4117 active = NULL;
4120 value = xcalloc(1, 1);
4121 valuelen = 1;
4123 RB_FOREACH(wl, winlinks, &ft->s->windows) {
4124 w = wl->window;
4125 format_log(es, "window loop: %u @%u", wl->idx, w->id);
4126 if (active != NULL && wl == ft->s->curw)
4127 use = active;
4128 else
4129 use = all;
4130 nft = format_create(c, item, FORMAT_WINDOW|w->id, ft->flags);
4131 format_defaults(nft, ft->c, ft->s, wl, NULL);
4132 format_copy_state(&next, es, 0);
4133 next.ft = nft;
4134 expanded = format_expand1(&next, use);
4135 format_free(nft);
4137 valuelen += strlen(expanded);
4138 value = xrealloc(value, valuelen);
4140 strlcat(value, expanded, valuelen);
4141 free(expanded);
4144 free(active);
4145 free(all);
4147 return (value);
4150 /* Loop over panes. */
4151 static char *
4152 format_loop_panes(struct format_expand_state *es, const char *fmt)
4154 struct format_tree *ft = es->ft;
4155 struct client *c = ft->client;
4156 struct cmdq_item *item = ft->item;
4157 struct format_tree *nft;
4158 struct format_expand_state next;
4159 char *all, *active, *use, *expanded, *value;
4160 size_t valuelen;
4161 struct window_pane *wp;
4163 if (ft->w == NULL) {
4164 format_log(es, "pane loop but no window");
4165 return (NULL);
4168 if (format_choose(es, fmt, &all, &active, 0) != 0) {
4169 all = xstrdup(fmt);
4170 active = NULL;
4173 value = xcalloc(1, 1);
4174 valuelen = 1;
4176 TAILQ_FOREACH(wp, &ft->w->panes, entry) {
4177 format_log(es, "pane loop: %%%u", wp->id);
4178 if (active != NULL && wp == ft->w->active)
4179 use = active;
4180 else
4181 use = all;
4182 nft = format_create(c, item, FORMAT_PANE|wp->id, ft->flags);
4183 format_defaults(nft, ft->c, ft->s, ft->wl, wp);
4184 format_copy_state(&next, es, 0);
4185 next.ft = nft;
4186 expanded = format_expand1(&next, use);
4187 format_free(nft);
4189 valuelen += strlen(expanded);
4190 value = xrealloc(value, valuelen);
4192 strlcat(value, expanded, valuelen);
4193 free(expanded);
4196 free(active);
4197 free(all);
4199 return (value);
4202 /* Loop over clients. */
4203 static char *
4204 format_loop_clients(struct format_expand_state *es, const char *fmt)
4206 struct format_tree *ft = es->ft;
4207 struct client *c;
4208 struct cmdq_item *item = ft->item;
4209 struct format_tree *nft;
4210 struct format_expand_state next;
4211 char *expanded, *value;
4212 size_t valuelen;
4214 value = xcalloc(1, 1);
4215 valuelen = 1;
4217 TAILQ_FOREACH(c, &clients, entry) {
4218 format_log(es, "client loop: %s", c->name);
4219 nft = format_create(c, item, 0, ft->flags);
4220 format_defaults(nft, c, ft->s, ft->wl, ft->wp);
4221 format_copy_state(&next, es, 0);
4222 next.ft = nft;
4223 expanded = format_expand1(&next, fmt);
4224 format_free(nft);
4226 valuelen += strlen(expanded);
4227 value = xrealloc(value, valuelen);
4229 strlcat(value, expanded, valuelen);
4230 free(expanded);
4233 return (value);
4236 static char *
4237 format_replace_expression(struct format_modifier *mexp,
4238 struct format_expand_state *es, const char *copy)
4240 int argc = mexp->argc;
4241 const char *errstr;
4242 char *endch, *value, *left = NULL, *right = NULL;
4243 int use_fp = 0;
4244 u_int prec = 0;
4245 double mleft, mright, result;
4246 enum { ADD,
4247 SUBTRACT,
4248 MULTIPLY,
4249 DIVIDE,
4250 MODULUS,
4251 EQUAL,
4252 NOT_EQUAL,
4253 GREATER_THAN,
4254 GREATER_THAN_EQUAL,
4255 LESS_THAN,
4256 LESS_THAN_EQUAL } operator;
4258 if (strcmp(mexp->argv[0], "+") == 0)
4259 operator = ADD;
4260 else if (strcmp(mexp->argv[0], "-") == 0)
4261 operator = SUBTRACT;
4262 else if (strcmp(mexp->argv[0], "*") == 0)
4263 operator = MULTIPLY;
4264 else if (strcmp(mexp->argv[0], "/") == 0)
4265 operator = DIVIDE;
4266 else if (strcmp(mexp->argv[0], "%") == 0 ||
4267 strcmp(mexp->argv[0], "m") == 0)
4268 operator = MODULUS;
4269 else if (strcmp(mexp->argv[0], "==") == 0)
4270 operator = EQUAL;
4271 else if (strcmp(mexp->argv[0], "!=") == 0)
4272 operator = NOT_EQUAL;
4273 else if (strcmp(mexp->argv[0], ">") == 0)
4274 operator = GREATER_THAN;
4275 else if (strcmp(mexp->argv[0], "<") == 0)
4276 operator = LESS_THAN;
4277 else if (strcmp(mexp->argv[0], ">=") == 0)
4278 operator = GREATER_THAN_EQUAL;
4279 else if (strcmp(mexp->argv[0], "<=") == 0)
4280 operator = LESS_THAN_EQUAL;
4281 else {
4282 format_log(es, "expression has no valid operator: '%s'",
4283 mexp->argv[0]);
4284 goto fail;
4287 /* The second argument may be flags. */
4288 if (argc >= 2 && strchr(mexp->argv[1], 'f') != NULL) {
4289 use_fp = 1;
4290 prec = 2;
4293 /* The third argument may be precision. */
4294 if (argc >= 3) {
4295 prec = strtonum(mexp->argv[2], INT_MIN, INT_MAX, &errstr);
4296 if (errstr != NULL) {
4297 format_log(es, "expression precision %s: %s", errstr,
4298 mexp->argv[2]);
4299 goto fail;
4303 if (format_choose(es, copy, &left, &right, 1) != 0) {
4304 format_log(es, "expression syntax error");
4305 goto fail;
4308 mleft = strtod(left, &endch);
4309 if (*endch != '\0') {
4310 format_log(es, "expression left side is invalid: %s", left);
4311 goto fail;
4314 mright = strtod(right, &endch);
4315 if (*endch != '\0') {
4316 format_log(es, "expression right side is invalid: %s", right);
4317 goto fail;
4320 if (!use_fp) {
4321 mleft = (long long)mleft;
4322 mright = (long long)mright;
4324 format_log(es, "expression left side is: %.*f", prec, mleft);
4325 format_log(es, "expression right side is: %.*f", prec, mright);
4327 switch (operator) {
4328 case ADD:
4329 result = mleft + mright;
4330 break;
4331 case SUBTRACT:
4332 result = mleft - mright;
4333 break;
4334 case MULTIPLY:
4335 result = mleft * mright;
4336 break;
4337 case DIVIDE:
4338 result = mleft / mright;
4339 break;
4340 case MODULUS:
4341 result = fmod(mleft, mright);
4342 break;
4343 case EQUAL:
4344 result = fabs(mleft - mright) < 1e-9;
4345 break;
4346 case NOT_EQUAL:
4347 result = fabs(mleft - mright) > 1e-9;
4348 break;
4349 case GREATER_THAN:
4350 result = (mleft > mright);
4351 break;
4352 case GREATER_THAN_EQUAL:
4353 result = (mleft >= mright);
4354 break;
4355 case LESS_THAN:
4356 result = (mleft < mright);
4357 break;
4358 case LESS_THAN_EQUAL:
4359 result = (mleft <= mright);
4360 break;
4362 if (use_fp)
4363 xasprintf(&value, "%.*f", prec, result);
4364 else
4365 xasprintf(&value, "%.*f", prec, (double)(long long)result);
4366 format_log(es, "expression result is %s", value);
4368 free(right);
4369 free(left);
4370 return (value);
4372 fail:
4373 free(right);
4374 free(left);
4375 return (NULL);
4378 /* Replace a key. */
4379 static int
4380 format_replace(struct format_expand_state *es, const char *key, size_t keylen,
4381 char **buf, size_t *len, size_t *off)
4383 struct format_tree *ft = es->ft;
4384 struct window_pane *wp = ft->wp;
4385 const char *errstr, *copy, *cp, *marker = NULL;
4386 const char *time_format = NULL;
4387 char *copy0, *condition, *found, *new;
4388 char *value, *left, *right;
4389 size_t valuelen;
4390 int modifiers = 0, limit = 0, width = 0;
4391 int j, c;
4392 struct format_modifier *list, *cmp = NULL, *search = NULL;
4393 struct format_modifier **sub = NULL, *mexp = NULL, *fm;
4394 u_int i, count, nsub = 0;
4395 struct format_expand_state next;
4397 /* Make a copy of the key. */
4398 copy = copy0 = xstrndup(key, keylen);
4400 /* Process modifier list. */
4401 list = format_build_modifiers(es, &copy, &count);
4402 for (i = 0; i < count; i++) {
4403 fm = &list[i];
4404 if (format_logging(ft)) {
4405 format_log(es, "modifier %u is %s", i, fm->modifier);
4406 for (j = 0; j < fm->argc; j++) {
4407 format_log(es, "modifier %u argument %d: %s", i,
4408 j, fm->argv[j]);
4411 if (fm->size == 1) {
4412 switch (fm->modifier[0]) {
4413 case 'm':
4414 case '<':
4415 case '>':
4416 cmp = fm;
4417 break;
4418 case 'C':
4419 search = fm;
4420 break;
4421 case 's':
4422 if (fm->argc < 2)
4423 break;
4424 sub = xreallocarray(sub, nsub + 1, sizeof *sub);
4425 sub[nsub++] = fm;
4426 break;
4427 case '=':
4428 if (fm->argc < 1)
4429 break;
4430 limit = strtonum(fm->argv[0], INT_MIN, INT_MAX,
4431 &errstr);
4432 if (errstr != NULL)
4433 limit = 0;
4434 if (fm->argc >= 2 && fm->argv[1] != NULL)
4435 marker = fm->argv[1];
4436 break;
4437 case 'p':
4438 if (fm->argc < 1)
4439 break;
4440 width = strtonum(fm->argv[0], INT_MIN, INT_MAX,
4441 &errstr);
4442 if (errstr != NULL)
4443 width = 0;
4444 break;
4445 case 'w':
4446 modifiers |= FORMAT_WIDTH;
4447 break;
4448 case 'e':
4449 if (fm->argc < 1 || fm->argc > 3)
4450 break;
4451 mexp = fm;
4452 break;
4453 case 'l':
4454 modifiers |= FORMAT_LITERAL;
4455 break;
4456 case 'a':
4457 modifiers |= FORMAT_CHARACTER;
4458 break;
4459 case 'b':
4460 modifiers |= FORMAT_BASENAME;
4461 break;
4462 case 'c':
4463 modifiers |= FORMAT_COLOUR;
4464 break;
4465 case 'd':
4466 modifiers |= FORMAT_DIRNAME;
4467 break;
4468 case 'n':
4469 modifiers |= FORMAT_LENGTH;
4470 break;
4471 case 't':
4472 modifiers |= FORMAT_TIMESTRING;
4473 if (fm->argc < 1)
4474 break;
4475 if (strchr(fm->argv[0], 'p') != NULL)
4476 modifiers |= FORMAT_PRETTY;
4477 else if (fm->argc >= 2 &&
4478 strchr(fm->argv[0], 'f') != NULL)
4479 time_format = format_strip(fm->argv[1]);
4480 break;
4481 case 'q':
4482 if (fm->argc < 1)
4483 modifiers |= FORMAT_QUOTE_SHELL;
4484 else if (strchr(fm->argv[0], 'e') != NULL ||
4485 strchr(fm->argv[0], 'h') != NULL)
4486 modifiers |= FORMAT_QUOTE_STYLE;
4487 break;
4488 case 'E':
4489 modifiers |= FORMAT_EXPAND;
4490 break;
4491 case 'T':
4492 modifiers |= FORMAT_EXPANDTIME;
4493 break;
4494 case 'N':
4495 if (fm->argc < 1 ||
4496 strchr(fm->argv[0], 'w') != NULL)
4497 modifiers |= FORMAT_WINDOW_NAME;
4498 else if (strchr(fm->argv[0], 's') != NULL)
4499 modifiers |= FORMAT_SESSION_NAME;
4500 break;
4501 case 'S':
4502 modifiers |= FORMAT_SESSIONS;
4503 break;
4504 case 'W':
4505 modifiers |= FORMAT_WINDOWS;
4506 break;
4507 case 'P':
4508 modifiers |= FORMAT_PANES;
4509 break;
4510 case 'L':
4511 modifiers |= FORMAT_CLIENTS;
4512 break;
4514 } else if (fm->size == 2) {
4515 if (strcmp(fm->modifier, "||") == 0 ||
4516 strcmp(fm->modifier, "&&") == 0 ||
4517 strcmp(fm->modifier, "==") == 0 ||
4518 strcmp(fm->modifier, "!=") == 0 ||
4519 strcmp(fm->modifier, ">=") == 0 ||
4520 strcmp(fm->modifier, "<=") == 0)
4521 cmp = fm;
4525 /* Is this a literal string? */
4526 if (modifiers & FORMAT_LITERAL) {
4527 format_log(es, "literal string is '%s'", copy);
4528 value = format_unescape(copy);
4529 goto done;
4532 /* Is this a character? */
4533 if (modifiers & FORMAT_CHARACTER) {
4534 new = format_expand1(es, copy);
4535 c = strtonum(new, 32, 126, &errstr);
4536 if (errstr != NULL)
4537 value = xstrdup("");
4538 else
4539 xasprintf(&value, "%c", c);
4540 free(new);
4541 goto done;
4544 /* Is this a colour? */
4545 if (modifiers & FORMAT_COLOUR) {
4546 new = format_expand1(es, copy);
4547 c = colour_fromstring(new);
4548 if (c == -1 || (c = colour_force_rgb(c)) == -1)
4549 value = xstrdup("");
4550 else
4551 xasprintf(&value, "%06x", c & 0xffffff);
4552 free(new);
4553 goto done;
4556 /* Is this a loop, comparison or condition? */
4557 if (modifiers & FORMAT_SESSIONS) {
4558 value = format_loop_sessions(es, copy);
4559 if (value == NULL)
4560 goto fail;
4561 } else if (modifiers & FORMAT_WINDOWS) {
4562 value = format_loop_windows(es, copy);
4563 if (value == NULL)
4564 goto fail;
4565 } else if (modifiers & FORMAT_PANES) {
4566 value = format_loop_panes(es, copy);
4567 if (value == NULL)
4568 goto fail;
4569 } else if (modifiers & FORMAT_CLIENTS) {
4570 value = format_loop_clients(es, copy);
4571 if (value == NULL)
4572 goto fail;
4573 } else if (modifiers & FORMAT_WINDOW_NAME) {
4574 value = format_window_name(es, copy);
4575 if (value == NULL)
4576 goto fail;
4577 } else if (modifiers & FORMAT_SESSION_NAME) {
4578 value = format_session_name(es, copy);
4579 if (value == NULL)
4580 goto fail;
4581 } else if (search != NULL) {
4582 /* Search in pane. */
4583 new = format_expand1(es, copy);
4584 if (wp == NULL) {
4585 format_log(es, "search '%s' but no pane", new);
4586 value = xstrdup("0");
4587 } else {
4588 format_log(es, "search '%s' pane %%%u", new, wp->id);
4589 value = format_search(search, wp, new);
4591 free(new);
4592 } else if (cmp != NULL) {
4593 /* Comparison of left and right. */
4594 if (format_choose(es, copy, &left, &right, 1) != 0) {
4595 format_log(es, "compare %s syntax error: %s",
4596 cmp->modifier, copy);
4597 goto fail;
4599 format_log(es, "compare %s left is: %s", cmp->modifier, left);
4600 format_log(es, "compare %s right is: %s", cmp->modifier, right);
4602 if (strcmp(cmp->modifier, "||") == 0) {
4603 if (format_true(left) || format_true(right))
4604 value = xstrdup("1");
4605 else
4606 value = xstrdup("0");
4607 } else if (strcmp(cmp->modifier, "&&") == 0) {
4608 if (format_true(left) && format_true(right))
4609 value = xstrdup("1");
4610 else
4611 value = xstrdup("0");
4612 } else if (strcmp(cmp->modifier, "==") == 0) {
4613 if (strcmp(left, right) == 0)
4614 value = xstrdup("1");
4615 else
4616 value = xstrdup("0");
4617 } else if (strcmp(cmp->modifier, "!=") == 0) {
4618 if (strcmp(left, right) != 0)
4619 value = xstrdup("1");
4620 else
4621 value = xstrdup("0");
4622 } else if (strcmp(cmp->modifier, "<") == 0) {
4623 if (strcmp(left, right) < 0)
4624 value = xstrdup("1");
4625 else
4626 value = xstrdup("0");
4627 } else if (strcmp(cmp->modifier, ">") == 0) {
4628 if (strcmp(left, right) > 0)
4629 value = xstrdup("1");
4630 else
4631 value = xstrdup("0");
4632 } else if (strcmp(cmp->modifier, "<=") == 0) {
4633 if (strcmp(left, right) <= 0)
4634 value = xstrdup("1");
4635 else
4636 value = xstrdup("0");
4637 } else if (strcmp(cmp->modifier, ">=") == 0) {
4638 if (strcmp(left, right) >= 0)
4639 value = xstrdup("1");
4640 else
4641 value = xstrdup("0");
4642 } else if (strcmp(cmp->modifier, "m") == 0)
4643 value = format_match(cmp, left, right);
4645 free(right);
4646 free(left);
4647 } else if (*copy == '?') {
4648 /* Conditional: check first and choose second or third. */
4649 cp = format_skip(copy + 1, ",");
4650 if (cp == NULL) {
4651 format_log(es, "condition syntax error: %s", copy + 1);
4652 goto fail;
4654 condition = xstrndup(copy + 1, cp - (copy + 1));
4655 format_log(es, "condition is: %s", condition);
4657 found = format_find(ft, condition, modifiers, time_format);
4658 if (found == NULL) {
4660 * If the condition not found, try to expand it. If
4661 * the expansion doesn't have any effect, then assume
4662 * false.
4664 found = format_expand1(es, condition);
4665 if (strcmp(found, condition) == 0) {
4666 free(found);
4667 found = xstrdup("");
4668 format_log(es,
4669 "condition '%s' not found; assuming false",
4670 condition);
4672 } else {
4673 format_log(es, "condition '%s' found: %s", condition,
4674 found);
4677 if (format_choose(es, cp + 1, &left, &right, 0) != 0) {
4678 format_log(es, "condition '%s' syntax error: %s",
4679 condition, cp + 1);
4680 free(found);
4681 goto fail;
4683 if (format_true(found)) {
4684 format_log(es, "condition '%s' is true", condition);
4685 value = format_expand1(es, left);
4686 } else {
4687 format_log(es, "condition '%s' is false", condition);
4688 value = format_expand1(es, right);
4690 free(right);
4691 free(left);
4693 free(condition);
4694 free(found);
4695 } else if (mexp != NULL) {
4696 value = format_replace_expression(mexp, es, copy);
4697 if (value == NULL)
4698 value = xstrdup("");
4699 } else {
4700 if (strstr(copy, "#{") != 0) {
4701 format_log(es, "expanding inner format '%s'", copy);
4702 value = format_expand1(es, copy);
4703 } else {
4704 value = format_find(ft, copy, modifiers, time_format);
4705 if (value == NULL) {
4706 format_log(es, "format '%s' not found", copy);
4707 value = xstrdup("");
4708 } else {
4709 format_log(es, "format '%s' found: %s", copy,
4710 value);
4715 done:
4716 /* Expand again if required. */
4717 if (modifiers & FORMAT_EXPAND) {
4718 new = format_expand1(es, value);
4719 free(value);
4720 value = new;
4721 } else if (modifiers & FORMAT_EXPANDTIME) {
4722 format_copy_state(&next, es, FORMAT_EXPAND_TIME);
4723 new = format_expand1(&next, value);
4724 free(value);
4725 value = new;
4728 /* Perform substitution if any. */
4729 for (i = 0; i < nsub; i++) {
4730 left = format_expand1(es, sub[i]->argv[0]);
4731 right = format_expand1(es, sub[i]->argv[1]);
4732 new = format_sub(sub[i], value, left, right);
4733 format_log(es, "substitute '%s' to '%s': %s", left, right, new);
4734 free(value);
4735 value = new;
4736 free(right);
4737 free(left);
4740 /* Truncate the value if needed. */
4741 if (limit > 0) {
4742 new = format_trim_left(value, limit);
4743 if (marker != NULL && strcmp(new, value) != 0) {
4744 free(value);
4745 xasprintf(&value, "%s%s", new, marker);
4746 } else {
4747 free(value);
4748 value = new;
4750 format_log(es, "applied length limit %d: %s", limit, value);
4751 } else if (limit < 0) {
4752 new = format_trim_right(value, -limit);
4753 if (marker != NULL && strcmp(new, value) != 0) {
4754 free(value);
4755 xasprintf(&value, "%s%s", marker, new);
4756 } else {
4757 free(value);
4758 value = new;
4760 format_log(es, "applied length limit %d: %s", limit, value);
4763 /* Pad the value if needed. */
4764 if (width > 0) {
4765 new = utf8_padcstr(value, width);
4766 free(value);
4767 value = new;
4768 format_log(es, "applied padding width %d: %s", width, value);
4769 } else if (width < 0) {
4770 new = utf8_rpadcstr(value, -width);
4771 free(value);
4772 value = new;
4773 format_log(es, "applied padding width %d: %s", width, value);
4776 /* Replace with the length or width if needed. */
4777 if (modifiers & FORMAT_LENGTH) {
4778 xasprintf(&new, "%zu", strlen(value));
4779 free(value);
4780 value = new;
4781 format_log(es, "replacing with length: %s", new);
4783 if (modifiers & FORMAT_WIDTH) {
4784 xasprintf(&new, "%u", format_width(value));
4785 free(value);
4786 value = new;
4787 format_log(es, "replacing with width: %s", new);
4790 /* Expand the buffer and copy in the value. */
4791 valuelen = strlen(value);
4792 while (*len - *off < valuelen + 1) {
4793 *buf = xreallocarray(*buf, 2, *len);
4794 *len *= 2;
4796 memcpy(*buf + *off, value, valuelen);
4797 *off += valuelen;
4799 format_log(es, "replaced '%s' with '%s'", copy0, value);
4800 free(value);
4802 free(sub);
4803 format_free_modifiers(list, count);
4804 free(copy0);
4805 return (0);
4807 fail:
4808 format_log(es, "failed %s", copy0);
4810 free(sub);
4811 format_free_modifiers(list, count);
4812 free(copy0);
4813 return (-1);
4816 /* Expand keys in a template. */
4817 static char *
4818 format_expand1(struct format_expand_state *es, const char *fmt)
4820 struct format_tree *ft = es->ft;
4821 char *buf, *out, *name;
4822 const char *ptr, *s, *style_end = NULL;
4823 size_t off, len, n, outlen;
4824 int ch, brackets;
4825 char expanded[8192];
4827 if (fmt == NULL || *fmt == '\0')
4828 return (xstrdup(""));
4830 if (es->loop == FORMAT_LOOP_LIMIT) {
4831 format_log(es, "reached loop limit (%u)", FORMAT_LOOP_LIMIT);
4832 return (xstrdup(""));
4834 es->loop++;
4836 format_log(es, "expanding format: %s", fmt);
4838 if ((es->flags & FORMAT_EXPAND_TIME) && strchr(fmt, '%') != NULL) {
4839 if (es->time == 0) {
4840 es->time = time(NULL);
4841 localtime_r(&es->time, &es->tm);
4843 if (strftime(expanded, sizeof expanded, fmt, &es->tm) == 0) {
4844 format_log(es, "format is too long");
4845 return (xstrdup(""));
4847 if (format_logging(ft) && strcmp(expanded, fmt) != 0)
4848 format_log(es, "after time expanded: %s", expanded);
4849 fmt = expanded;
4852 len = 64;
4853 buf = xmalloc(len);
4854 off = 0;
4856 while (*fmt != '\0') {
4857 if (*fmt != '#') {
4858 while (len - off < 2) {
4859 buf = xreallocarray(buf, 2, len);
4860 len *= 2;
4862 buf[off++] = *fmt++;
4863 continue;
4865 fmt++;
4867 ch = (u_char)*fmt++;
4868 switch (ch) {
4869 case '(':
4870 brackets = 1;
4871 for (ptr = fmt; *ptr != '\0'; ptr++) {
4872 if (*ptr == '(')
4873 brackets++;
4874 if (*ptr == ')' && --brackets == 0)
4875 break;
4877 if (*ptr != ')' || brackets != 0)
4878 break;
4879 n = ptr - fmt;
4881 name = xstrndup(fmt, n);
4882 format_log(es, "found #(): %s", name);
4884 if ((ft->flags & FORMAT_NOJOBS) ||
4885 (es->flags & FORMAT_EXPAND_NOJOBS)) {
4886 out = xstrdup("");
4887 format_log(es, "#() is disabled");
4888 } else {
4889 out = format_job_get(es, name);
4890 format_log(es, "#() result: %s", out);
4892 free(name);
4894 outlen = strlen(out);
4895 while (len - off < outlen + 1) {
4896 buf = xreallocarray(buf, 2, len);
4897 len *= 2;
4899 memcpy(buf + off, out, outlen);
4900 off += outlen;
4902 free(out);
4904 fmt += n + 1;
4905 continue;
4906 case '{':
4907 ptr = format_skip((char *)fmt - 2, "}");
4908 if (ptr == NULL)
4909 break;
4910 n = ptr - fmt;
4912 format_log(es, "found #{}: %.*s", (int)n, fmt);
4913 if (format_replace(es, fmt, n, &buf, &len, &off) != 0)
4914 break;
4915 fmt += n + 1;
4916 continue;
4917 case '[':
4918 case '#':
4920 * If ##[ (with two or more #s), then it is a style and
4921 * can be left for format_draw to handle.
4923 ptr = fmt - (ch == '[');
4924 n = 2 - (ch == '[');
4925 while (*ptr == '#') {
4926 ptr++;
4927 n++;
4929 if (*ptr == '[') {
4930 style_end = format_skip(fmt - 2, "]");
4931 format_log(es, "found #*%zu[", n);
4932 while (len - off < n + 2) {
4933 buf = xreallocarray(buf, 2, len);
4934 len *= 2;
4936 memcpy(buf + off, fmt - 2, n + 1);
4937 off += n + 1;
4938 fmt = ptr + 1;
4939 continue;
4941 /* FALLTHROUGH */
4942 case '}':
4943 case ',':
4944 format_log(es, "found #%c", ch);
4945 while (len - off < 2) {
4946 buf = xreallocarray(buf, 2, len);
4947 len *= 2;
4949 buf[off++] = ch;
4950 continue;
4951 default:
4952 s = NULL;
4953 if (fmt > style_end) { /* skip inside #[] */
4954 if (ch >= 'A' && ch <= 'Z')
4955 s = format_upper[ch - 'A'];
4956 else if (ch >= 'a' && ch <= 'z')
4957 s = format_lower[ch - 'a'];
4959 if (s == NULL) {
4960 while (len - off < 3) {
4961 buf = xreallocarray(buf, 2, len);
4962 len *= 2;
4964 buf[off++] = '#';
4965 buf[off++] = ch;
4966 continue;
4968 n = strlen(s);
4969 format_log(es, "found #%c: %s", ch, s);
4970 if (format_replace(es, s, n, &buf, &len, &off) != 0)
4971 break;
4972 continue;
4975 break;
4977 buf[off] = '\0';
4979 format_log(es, "result is: %s", buf);
4980 es->loop--;
4982 return (buf);
4985 /* Expand keys in a template, passing through strftime first. */
4986 char *
4987 format_expand_time(struct format_tree *ft, const char *fmt)
4989 struct format_expand_state es;
4991 memset(&es, 0, sizeof es);
4992 es.ft = ft;
4993 es.flags = FORMAT_EXPAND_TIME;
4994 return (format_expand1(&es, fmt));
4997 /* Expand keys in a template. */
4998 char *
4999 format_expand(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 = 0;
5006 return (format_expand1(&es, fmt));
5009 /* Expand a single string. */
5010 char *
5011 format_single(struct cmdq_item *item, const char *fmt, struct client *c,
5012 struct session *s, struct winlink *wl, struct window_pane *wp)
5014 struct format_tree *ft;
5015 char *expanded;
5017 ft = format_create_defaults(item, c, s, wl, wp);
5018 expanded = format_expand(ft, fmt);
5019 format_free(ft);
5020 return (expanded);
5023 /* Expand a single string using state. */
5024 char *
5025 format_single_from_state(struct cmdq_item *item, const char *fmt,
5026 struct client *c, struct cmd_find_state *fs)
5028 return (format_single(item, fmt, c, fs->s, fs->wl, fs->wp));
5031 /* Expand a single string using target. */
5032 char *
5033 format_single_from_target(struct cmdq_item *item, const char *fmt)
5035 struct client *tc = cmdq_get_target_client(item);
5037 return (format_single_from_state(item, fmt, tc, cmdq_get_target(item)));
5040 /* Create and add defaults. */
5041 struct format_tree *
5042 format_create_defaults(struct cmdq_item *item, struct client *c,
5043 struct session *s, struct winlink *wl, struct window_pane *wp)
5045 struct format_tree *ft;
5047 if (item != NULL)
5048 ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0);
5049 else
5050 ft = format_create(NULL, item, FORMAT_NONE, 0);
5051 format_defaults(ft, c, s, wl, wp);
5052 return (ft);
5055 /* Create and add defaults using state. */
5056 struct format_tree *
5057 format_create_from_state(struct cmdq_item *item, struct client *c,
5058 struct cmd_find_state *fs)
5060 return (format_create_defaults(item, c, fs->s, fs->wl, fs->wp));
5063 /* Create and add defaults using target. */
5064 struct format_tree *
5065 format_create_from_target(struct cmdq_item *item)
5067 struct client *tc = cmdq_get_target_client(item);
5069 return (format_create_from_state(item, tc, cmdq_get_target(item)));
5072 /* Set defaults for any of arguments that are not NULL. */
5073 void
5074 format_defaults(struct format_tree *ft, struct client *c, struct session *s,
5075 struct winlink *wl, struct window_pane *wp)
5077 struct paste_buffer *pb;
5079 if (c != NULL && c->name != NULL)
5080 log_debug("%s: c=%s", __func__, c->name);
5081 else
5082 log_debug("%s: c=none", __func__);
5083 if (s != NULL)
5084 log_debug("%s: s=$%u", __func__, s->id);
5085 else
5086 log_debug("%s: s=none", __func__);
5087 if (wl != NULL)
5088 log_debug("%s: wl=%u", __func__, wl->idx);
5089 else
5090 log_debug("%s: wl=none", __func__);
5091 if (wp != NULL)
5092 log_debug("%s: wp=%%%u", __func__, wp->id);
5093 else
5094 log_debug("%s: wp=none", __func__);
5096 if (c != NULL && s != NULL && c->session != s)
5097 log_debug("%s: session does not match", __func__);
5099 if (wp != NULL)
5100 ft->type = FORMAT_TYPE_PANE;
5101 else if (wl != NULL)
5102 ft->type = FORMAT_TYPE_WINDOW;
5103 else if (s != NULL)
5104 ft->type = FORMAT_TYPE_SESSION;
5105 else
5106 ft->type = FORMAT_TYPE_UNKNOWN;
5108 if (s == NULL && c != NULL)
5109 s = c->session;
5110 if (wl == NULL && s != NULL)
5111 wl = s->curw;
5112 if (wp == NULL && wl != NULL)
5113 wp = wl->window->active;
5115 if (c != NULL)
5116 format_defaults_client(ft, c);
5117 if (s != NULL)
5118 format_defaults_session(ft, s);
5119 if (wl != NULL)
5120 format_defaults_winlink(ft, wl);
5121 if (wp != NULL)
5122 format_defaults_pane(ft, wp);
5124 pb = paste_get_top(NULL);
5125 if (pb != NULL)
5126 format_defaults_paste_buffer(ft, pb);
5129 /* Set default format keys for a session. */
5130 static void
5131 format_defaults_session(struct format_tree *ft, struct session *s)
5133 ft->s = s;
5136 /* Set default format keys for a client. */
5137 static void
5138 format_defaults_client(struct format_tree *ft, struct client *c)
5140 if (ft->s == NULL)
5141 ft->s = c->session;
5142 ft->c = c;
5145 /* Set default format keys for a window. */
5146 void
5147 format_defaults_window(struct format_tree *ft, struct window *w)
5149 ft->w = w;
5152 /* Set default format keys for a winlink. */
5153 static void
5154 format_defaults_winlink(struct format_tree *ft, struct winlink *wl)
5156 if (ft->w == NULL)
5157 format_defaults_window(ft, wl->window);
5158 ft->wl = wl;
5161 /* Set default format keys for a window pane. */
5162 void
5163 format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
5165 struct window_mode_entry *wme;
5167 if (ft->w == NULL)
5168 format_defaults_window(ft, wp->window);
5169 ft->wp = wp;
5171 wme = TAILQ_FIRST(&wp->modes);
5172 if (wme != NULL && wme->mode->formats != NULL)
5173 wme->mode->formats(wme, ft);
5176 /* Set default format keys for paste buffer. */
5177 void
5178 format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
5180 ft->pb = pb;
5183 /* Return word at given coordinates. Caller frees. */
5184 char *
5185 format_grid_word(struct grid *gd, u_int x, u_int y)
5187 const struct grid_line *gl;
5188 struct grid_cell gc;
5189 const char *ws;
5190 struct utf8_data *ud = NULL;
5191 u_int end;
5192 size_t size = 0;
5193 int found = 0;
5194 char *s = NULL;
5196 ws = options_get_string(global_s_options, "word-separators");
5198 for (;;) {
5199 grid_get_cell(gd, x, y, &gc);
5200 if (gc.flags & GRID_FLAG_PADDING)
5201 break;
5202 if (utf8_cstrhas(ws, &gc.data) ||
5203 (gc.data.size == 1 && *gc.data.data == ' ')) {
5204 found = 1;
5205 break;
5208 if (x == 0) {
5209 if (y == 0)
5210 break;
5211 gl = grid_peek_line(gd, y - 1);
5212 if (~gl->flags & GRID_LINE_WRAPPED)
5213 break;
5214 y--;
5215 x = grid_line_length(gd, y);
5216 if (x == 0)
5217 break;
5219 x--;
5221 for (;;) {
5222 if (found) {
5223 end = grid_line_length(gd, y);
5224 if (end == 0 || x == end - 1) {
5225 if (y == gd->hsize + gd->sy - 1)
5226 break;
5227 gl = grid_peek_line(gd, y);
5228 if (~gl->flags & GRID_LINE_WRAPPED)
5229 break;
5230 y++;
5231 x = 0;
5232 } else
5233 x++;
5235 found = 1;
5237 grid_get_cell(gd, x, y, &gc);
5238 if (gc.flags & GRID_FLAG_PADDING)
5239 break;
5240 if (utf8_cstrhas(ws, &gc.data) ||
5241 (gc.data.size == 1 && *gc.data.data == ' '))
5242 break;
5244 ud = xreallocarray(ud, size + 2, sizeof *ud);
5245 memcpy(&ud[size++], &gc.data, sizeof *ud);
5247 if (size != 0) {
5248 ud[size].size = 0;
5249 s = utf8_tocstr(ud);
5250 free(ud);
5252 return (s);
5255 /* Return line at given coordinates. Caller frees. */
5256 char *
5257 format_grid_line(struct grid *gd, u_int y)
5259 struct grid_cell gc;
5260 struct utf8_data *ud = NULL;
5261 u_int x;
5262 size_t size = 0;
5263 char *s = NULL;
5265 for (x = 0; x < grid_line_length(gd, y); x++) {
5266 grid_get_cell(gd, x, y, &gc);
5267 if (gc.flags & GRID_FLAG_PADDING)
5268 break;
5270 ud = xreallocarray(ud, size + 2, sizeof *ud);
5271 memcpy(&ud[size++], &gc.data, sizeof *ud);
5273 if (size != 0) {
5274 ud[size].size = 0;
5275 s = utf8_tocstr(ud);
5276 free(ud);
5278 return (s);
5281 /* Return hyperlink at given coordinates. Caller frees. */
5282 char *
5283 format_grid_hyperlink(struct grid *gd, u_int x, u_int y, struct screen* s)
5285 const char *uri;
5286 struct grid_cell gc;
5288 grid_get_cell(gd, x, y, &gc);
5289 if (gc.flags & GRID_FLAG_PADDING)
5290 return (NULL);
5291 if (s->hyperlinks == NULL || gc.link == 0)
5292 return (NULL);
5293 if (!hyperlinks_get(s->hyperlinks, gc.link, &uri, NULL, NULL))
5294 return (NULL);
5295 return (xstrdup(uri));