Merge branch 'obsd-master'
[tmux.git] / format.c
blob1362486e27187bb1866f56da9efe6bde92f33b92
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;
1129 char *s;
1131 if (!ft->m.valid)
1132 return (NULL);
1133 wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1134 if (wp == NULL)
1135 return (NULL);
1136 if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0)
1137 return (NULL);
1139 if (!TAILQ_EMPTY(&wp->modes)) {
1140 if (TAILQ_FIRST(&wp->modes)->mode == &window_copy_mode ||
1141 TAILQ_FIRST(&wp->modes)->mode == &window_view_mode)
1142 return (s = window_copy_get_word(wp, x, y));
1143 return (NULL);
1145 gd = wp->base.grid;
1146 return (format_grid_word(gd, x, gd->hsize + y));
1149 /* Callback for mouse_hyperlink. */
1150 static void *
1151 format_cb_mouse_hyperlink(struct format_tree *ft)
1153 struct window_pane *wp;
1154 struct grid *gd;
1155 u_int x, y;
1157 if (!ft->m.valid)
1158 return (NULL);
1159 wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1160 if (wp == NULL)
1161 return (NULL);
1162 if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0)
1163 return (NULL);
1164 gd = wp->base.grid;
1165 return (format_grid_hyperlink(gd, x, gd->hsize + y, wp->screen));
1168 /* Callback for mouse_line. */
1169 static void *
1170 format_cb_mouse_line(struct format_tree *ft)
1172 struct window_pane *wp;
1173 struct grid *gd;
1174 u_int x, y;
1176 if (!ft->m.valid)
1177 return (NULL);
1178 wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1179 if (wp == NULL)
1180 return (NULL);
1181 if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0)
1182 return (NULL);
1184 if (!TAILQ_EMPTY(&wp->modes)) {
1185 if (TAILQ_FIRST(&wp->modes)->mode == &window_copy_mode ||
1186 TAILQ_FIRST(&wp->modes)->mode == &window_view_mode)
1187 return (window_copy_get_line(wp, y));
1188 return (NULL);
1190 gd = wp->base.grid;
1191 return (format_grid_line(gd, gd->hsize + y));
1194 /* Callback for mouse_status_line. */
1195 static void *
1196 format_cb_mouse_status_line(struct format_tree *ft)
1198 char *value;
1199 u_int y;
1201 if (!ft->m.valid)
1202 return (NULL);
1203 if (ft->c == NULL || (~ft->c->tty.flags & TTY_STARTED))
1204 return (NULL);
1206 if (ft->m.statusat == 0 && ft->m.y < ft->m.statuslines) {
1207 y = ft->m.y;
1208 } else if (ft->m.statusat > 0 && ft->m.y >= (u_int)ft->m.statusat) {
1209 y = ft->m.y - ft->m.statusat;
1210 } else
1211 return (NULL);
1212 xasprintf(&value, "%u", y);
1213 return (value);
1217 /* Callback for mouse_status_range. */
1218 static void *
1219 format_cb_mouse_status_range(struct format_tree *ft)
1221 struct style_range *sr;
1222 u_int x, y;
1224 if (!ft->m.valid)
1225 return (NULL);
1226 if (ft->c == NULL || (~ft->c->tty.flags & TTY_STARTED))
1227 return (NULL);
1229 if (ft->m.statusat == 0 && ft->m.y < ft->m.statuslines) {
1230 x = ft->m.x;
1231 y = ft->m.y;
1232 } else if (ft->m.statusat > 0 && ft->m.y >= (u_int)ft->m.statusat) {
1233 x = ft->m.x;
1234 y = ft->m.y - ft->m.statusat;
1235 } else
1236 return (NULL);
1238 sr = status_get_range(ft->c, x, y);
1239 if (sr == NULL)
1240 return (NULL);
1241 switch (sr->type) {
1242 case STYLE_RANGE_NONE:
1243 return (NULL);
1244 case STYLE_RANGE_LEFT:
1245 return (xstrdup("left"));
1246 case STYLE_RANGE_RIGHT:
1247 return (xstrdup("right"));
1248 case STYLE_RANGE_PANE:
1249 return (xstrdup("pane"));
1250 case STYLE_RANGE_WINDOW:
1251 return (xstrdup("window"));
1252 case STYLE_RANGE_SESSION:
1253 return (xstrdup("session"));
1254 case STYLE_RANGE_USER:
1255 return (xstrdup(sr->string));
1257 return (NULL);
1260 /* Callback for alternate_on. */
1261 static void *
1262 format_cb_alternate_on(struct format_tree *ft)
1264 if (ft->wp != NULL) {
1265 if (ft->wp->base.saved_grid != NULL)
1266 return (xstrdup("1"));
1267 return (xstrdup("0"));
1269 return (NULL);
1272 /* Callback for alternate_saved_x. */
1273 static void *
1274 format_cb_alternate_saved_x(struct format_tree *ft)
1276 if (ft->wp != NULL)
1277 return (format_printf("%u", ft->wp->base.saved_cx));
1278 return (NULL);
1281 /* Callback for alternate_saved_y. */
1282 static void *
1283 format_cb_alternate_saved_y(struct format_tree *ft)
1285 if (ft->wp != NULL)
1286 return (format_printf("%u", ft->wp->base.saved_cy));
1287 return (NULL);
1290 /* Callback for buffer_name. */
1291 static void *
1292 format_cb_buffer_name(struct format_tree *ft)
1294 if (ft->pb != NULL)
1295 return (xstrdup(paste_buffer_name(ft->pb)));
1296 return (NULL);
1299 /* Callback for buffer_sample. */
1300 static void *
1301 format_cb_buffer_sample(struct format_tree *ft)
1303 if (ft->pb != NULL)
1304 return (paste_make_sample(ft->pb));
1305 return (NULL);
1308 /* Callback for buffer_size. */
1309 static void *
1310 format_cb_buffer_size(struct format_tree *ft)
1312 size_t size;
1314 if (ft->pb != NULL) {
1315 paste_buffer_data(ft->pb, &size);
1316 return (format_printf("%zu", size));
1318 return (NULL);
1321 /* Callback for client_cell_height. */
1322 static void *
1323 format_cb_client_cell_height(struct format_tree *ft)
1325 if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED))
1326 return (format_printf("%u", ft->c->tty.ypixel));
1327 return (NULL);
1330 /* Callback for client_cell_width. */
1331 static void *
1332 format_cb_client_cell_width(struct format_tree *ft)
1334 if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED))
1335 return (format_printf("%u", ft->c->tty.xpixel));
1336 return (NULL);
1339 /* Callback for client_control_mode. */
1340 static void *
1341 format_cb_client_control_mode(struct format_tree *ft)
1343 if (ft->c != NULL) {
1344 if (ft->c->flags & CLIENT_CONTROL)
1345 return (xstrdup("1"));
1346 return (xstrdup("0"));
1348 return (NULL);
1351 /* Callback for client_discarded. */
1352 static void *
1353 format_cb_client_discarded(struct format_tree *ft)
1355 if (ft->c != NULL)
1356 return (format_printf("%zu", ft->c->discarded));
1357 return (NULL);
1360 /* Callback for client_flags. */
1361 static void *
1362 format_cb_client_flags(struct format_tree *ft)
1364 if (ft->c != NULL)
1365 return (xstrdup(server_client_get_flags(ft->c)));
1366 return (NULL);
1369 /* Callback for client_height. */
1370 static void *
1371 format_cb_client_height(struct format_tree *ft)
1373 if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED))
1374 return (format_printf("%u", ft->c->tty.sy));
1375 return (NULL);
1378 /* Callback for client_key_table. */
1379 static void *
1380 format_cb_client_key_table(struct format_tree *ft)
1382 if (ft->c != NULL)
1383 return (xstrdup(ft->c->keytable->name));
1384 return (NULL);
1387 /* Callback for client_last_session. */
1388 static void *
1389 format_cb_client_last_session(struct format_tree *ft)
1391 if (ft->c != NULL &&
1392 ft->c->last_session != NULL &&
1393 session_alive(ft->c->last_session))
1394 return (xstrdup(ft->c->last_session->name));
1395 return (NULL);
1398 /* Callback for client_name. */
1399 static void *
1400 format_cb_client_name(struct format_tree *ft)
1402 if (ft->c != NULL)
1403 return (xstrdup(ft->c->name));
1404 return (NULL);
1407 /* Callback for client_pid. */
1408 static void *
1409 format_cb_client_pid(struct format_tree *ft)
1411 if (ft->c != NULL)
1412 return (format_printf("%ld", (long)ft->c->pid));
1413 return (NULL);
1416 /* Callback for client_prefix. */
1417 static void *
1418 format_cb_client_prefix(struct format_tree *ft)
1420 const char *name;
1422 if (ft->c != NULL) {
1423 name = server_client_get_key_table(ft->c);
1424 if (strcmp(ft->c->keytable->name, name) == 0)
1425 return (xstrdup("0"));
1426 return (xstrdup("1"));
1428 return (NULL);
1431 /* Callback for client_readonly. */
1432 static void *
1433 format_cb_client_readonly(struct format_tree *ft)
1435 if (ft->c != NULL) {
1436 if (ft->c->flags & CLIENT_READONLY)
1437 return (xstrdup("1"));
1438 return (xstrdup("0"));
1440 return (NULL);
1443 /* Callback for client_session. */
1444 static void *
1445 format_cb_client_session(struct format_tree *ft)
1447 if (ft->c != NULL && ft->c->session != NULL)
1448 return (xstrdup(ft->c->session->name));
1449 return (NULL);
1452 /* Callback for client_termfeatures. */
1453 static void *
1454 format_cb_client_termfeatures(struct format_tree *ft)
1456 if (ft->c != NULL)
1457 return (xstrdup(tty_get_features(ft->c->term_features)));
1458 return (NULL);
1461 /* Callback for client_termname. */
1462 static void *
1463 format_cb_client_termname(struct format_tree *ft)
1465 if (ft->c != NULL)
1466 return (xstrdup(ft->c->term_name));
1467 return (NULL);
1470 /* Callback for client_termtype. */
1471 static void *
1472 format_cb_client_termtype(struct format_tree *ft)
1474 if (ft->c != NULL) {
1475 if (ft->c->term_type == NULL)
1476 return (xstrdup(""));
1477 return (xstrdup(ft->c->term_type));
1479 return (NULL);
1482 /* Callback for client_tty. */
1483 static void *
1484 format_cb_client_tty(struct format_tree *ft)
1486 if (ft->c != NULL)
1487 return (xstrdup(ft->c->ttyname));
1488 return (NULL);
1491 /* Callback for client_uid. */
1492 static void *
1493 format_cb_client_uid(struct format_tree *ft)
1495 uid_t uid;
1497 if (ft->c != NULL) {
1498 uid = proc_get_peer_uid(ft->c->peer);
1499 if (uid != (uid_t)-1)
1500 return (format_printf("%ld", (long)uid));
1502 return (NULL);
1505 /* Callback for client_user. */
1506 static void *
1507 format_cb_client_user(struct format_tree *ft)
1509 uid_t uid;
1510 struct passwd *pw;
1512 if (ft->c != NULL) {
1513 uid = proc_get_peer_uid(ft->c->peer);
1514 if (uid != (uid_t)-1 && (pw = getpwuid(uid)) != NULL)
1515 return (xstrdup(pw->pw_name));
1517 return (NULL);
1520 /* Callback for client_utf8. */
1521 static void *
1522 format_cb_client_utf8(struct format_tree *ft)
1524 if (ft->c != NULL) {
1525 if (ft->c->flags & CLIENT_UTF8)
1526 return (xstrdup("1"));
1527 return (xstrdup("0"));
1529 return (NULL);
1532 /* Callback for client_width. */
1533 static void *
1534 format_cb_client_width(struct format_tree *ft)
1536 if (ft->c != NULL)
1537 return (format_printf("%u", ft->c->tty.sx));
1538 return (NULL);
1541 /* Callback for client_written. */
1542 static void *
1543 format_cb_client_written(struct format_tree *ft)
1545 if (ft->c != NULL)
1546 return (format_printf("%zu", ft->c->written));
1547 return (NULL);
1550 /* Callback for config_files. */
1551 static void *
1552 format_cb_config_files(__unused struct format_tree *ft)
1554 char *s = NULL;
1555 size_t slen = 0;
1556 u_int i;
1557 size_t n;
1559 for (i = 0; i < cfg_nfiles; i++) {
1560 n = strlen(cfg_files[i]) + 1;
1561 s = xrealloc(s, slen + n + 1);
1562 slen += xsnprintf(s + slen, n + 1, "%s,", cfg_files[i]);
1564 if (s == NULL)
1565 return (xstrdup(""));
1566 s[slen - 1] = '\0';
1567 return (s);
1570 /* Callback for cursor_flag. */
1571 static void *
1572 format_cb_cursor_flag(struct format_tree *ft)
1574 if (ft->wp != NULL) {
1575 if (ft->wp->base.mode & MODE_CURSOR)
1576 return (xstrdup("1"));
1577 return (xstrdup("0"));
1579 return (NULL);
1582 /* Callback for cursor_x. */
1583 static void *
1584 format_cb_cursor_x(struct format_tree *ft)
1586 if (ft->wp != NULL)
1587 return (format_printf("%u", ft->wp->base.cx));
1588 return (NULL);
1591 /* Callback for cursor_y. */
1592 static void *
1593 format_cb_cursor_y(struct format_tree *ft)
1595 if (ft->wp != NULL)
1596 return (format_printf("%u", ft->wp->base.cy));
1597 return (NULL);
1600 /* Callback for history_limit. */
1601 static void *
1602 format_cb_history_limit(struct format_tree *ft)
1604 if (ft->wp != NULL)
1605 return (format_printf("%u", ft->wp->base.grid->hlimit));
1606 return (NULL);
1609 /* Callback for history_size. */
1610 static void *
1611 format_cb_history_size(struct format_tree *ft)
1613 if (ft->wp != NULL)
1614 return (format_printf("%u", ft->wp->base.grid->hsize));
1615 return (NULL);
1618 /* Callback for insert_flag. */
1619 static void *
1620 format_cb_insert_flag(struct format_tree *ft)
1622 if (ft->wp != NULL) {
1623 if (ft->wp->base.mode & MODE_INSERT)
1624 return (xstrdup("1"));
1625 return (xstrdup("0"));
1627 return (NULL);
1630 /* Callback for keypad_cursor_flag. */
1631 static void *
1632 format_cb_keypad_cursor_flag(struct format_tree *ft)
1634 if (ft->wp != NULL) {
1635 if (ft->wp->base.mode & MODE_KCURSOR)
1636 return (xstrdup("1"));
1637 return (xstrdup("0"));
1639 return (NULL);
1642 /* Callback for keypad_flag. */
1643 static void *
1644 format_cb_keypad_flag(struct format_tree *ft)
1646 if (ft->wp != NULL) {
1647 if (ft->wp->base.mode & MODE_KKEYPAD)
1648 return (xstrdup("1"));
1649 return (xstrdup("0"));
1651 return (NULL);
1654 /* Callback for mouse_all_flag. */
1655 static void *
1656 format_cb_mouse_all_flag(struct format_tree *ft)
1658 if (ft->wp != NULL) {
1659 if (ft->wp->base.mode & MODE_MOUSE_ALL)
1660 return (xstrdup("1"));
1661 return (xstrdup("0"));
1663 return (NULL);
1666 /* Callback for mouse_any_flag. */
1667 static void *
1668 format_cb_mouse_any_flag(struct format_tree *ft)
1670 if (ft->wp != NULL) {
1671 if (ft->wp->base.mode & ALL_MOUSE_MODES)
1672 return (xstrdup("1"));
1673 return (xstrdup("0"));
1675 return (NULL);
1678 /* Callback for mouse_button_flag. */
1679 static void *
1680 format_cb_mouse_button_flag(struct format_tree *ft)
1682 if (ft->wp != NULL) {
1683 if (ft->wp->base.mode & MODE_MOUSE_BUTTON)
1684 return (xstrdup("1"));
1685 return (xstrdup("0"));
1687 return (NULL);
1690 /* Callback for mouse_pane. */
1691 static void *
1692 format_cb_mouse_pane(struct format_tree *ft)
1694 struct window_pane *wp;
1696 if (ft->m.valid) {
1697 wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1698 if (wp != NULL)
1699 return (format_printf("%%%u", wp->id));
1700 return (NULL);
1702 return (NULL);
1705 /* Callback for mouse_sgr_flag. */
1706 static void *
1707 format_cb_mouse_sgr_flag(struct format_tree *ft)
1709 if (ft->wp != NULL) {
1710 if (ft->wp->base.mode & MODE_MOUSE_SGR)
1711 return (xstrdup("1"));
1712 return (xstrdup("0"));
1714 return (NULL);
1717 /* Callback for mouse_standard_flag. */
1718 static void *
1719 format_cb_mouse_standard_flag(struct format_tree *ft)
1721 if (ft->wp != NULL) {
1722 if (ft->wp->base.mode & MODE_MOUSE_STANDARD)
1723 return (xstrdup("1"));
1724 return (xstrdup("0"));
1726 return (NULL);
1729 /* Callback for mouse_utf8_flag. */
1730 static void *
1731 format_cb_mouse_utf8_flag(struct format_tree *ft)
1733 if (ft->wp != NULL) {
1734 if (ft->wp->base.mode & MODE_MOUSE_UTF8)
1735 return (xstrdup("1"));
1736 return (xstrdup("0"));
1738 return (NULL);
1741 /* Callback for mouse_x. */
1742 static void *
1743 format_cb_mouse_x(struct format_tree *ft)
1745 struct window_pane *wp;
1746 u_int x, y;
1748 if (!ft->m.valid)
1749 return (NULL);
1750 wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1751 if (wp != NULL && cmd_mouse_at(wp, &ft->m, &x, &y, 0) == 0)
1752 return (format_printf("%u", x));
1753 if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED)) {
1754 if (ft->m.statusat == 0 && ft->m.y < ft->m.statuslines)
1755 return (format_printf("%u", ft->m.x));
1756 if (ft->m.statusat > 0 && ft->m.y >= (u_int)ft->m.statusat)
1757 return (format_printf("%u", ft->m.x));
1759 return (NULL);
1762 /* Callback for mouse_y. */
1763 static void *
1764 format_cb_mouse_y(struct format_tree *ft)
1766 struct window_pane *wp;
1767 u_int x, y;
1769 if (!ft->m.valid)
1770 return (NULL);
1771 wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1772 if (wp != NULL && cmd_mouse_at(wp, &ft->m, &x, &y, 0) == 0)
1773 return (format_printf("%u", y));
1774 if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED)) {
1775 if (ft->m.statusat == 0 && ft->m.y < ft->m.statuslines)
1776 return (format_printf("%u", ft->m.y));
1777 if (ft->m.statusat > 0 && ft->m.y >= (u_int)ft->m.statusat)
1778 return (format_printf("%u", ft->m.y - ft->m.statusat));
1780 return (NULL);
1783 /* Callback for next_session_id. */
1784 static void *
1785 format_cb_next_session_id(__unused struct format_tree *ft)
1787 return (format_printf("$%u", next_session_id));
1790 /* Callback for origin_flag. */
1791 static void *
1792 format_cb_origin_flag(struct format_tree *ft)
1794 if (ft->wp != NULL) {
1795 if (ft->wp->base.mode & MODE_ORIGIN)
1796 return (xstrdup("1"));
1797 return (xstrdup("0"));
1799 return (NULL);
1802 /* Callback for pane_active. */
1803 static void *
1804 format_cb_pane_active(struct format_tree *ft)
1806 if (ft->wp != NULL) {
1807 if (ft->wp == ft->wp->window->active)
1808 return (xstrdup("1"));
1809 return (xstrdup("0"));
1811 return (NULL);
1814 /* Callback for pane_at_left. */
1815 static void *
1816 format_cb_pane_at_left(struct format_tree *ft)
1818 if (ft->wp != NULL) {
1819 if (ft->wp->xoff == 0)
1820 return (xstrdup("1"));
1821 return (xstrdup("0"));
1823 return (NULL);
1826 /* Callback for pane_at_right. */
1827 static void *
1828 format_cb_pane_at_right(struct format_tree *ft)
1830 if (ft->wp != NULL) {
1831 if (ft->wp->xoff + ft->wp->sx == ft->wp->window->sx)
1832 return (xstrdup("1"));
1833 return (xstrdup("0"));
1835 return (NULL);
1838 /* Callback for pane_bottom. */
1839 static void *
1840 format_cb_pane_bottom(struct format_tree *ft)
1842 if (ft->wp != NULL)
1843 return (format_printf("%u", ft->wp->yoff + ft->wp->sy - 1));
1844 return (NULL);
1847 /* Callback for pane_dead. */
1848 static void *
1849 format_cb_pane_dead(struct format_tree *ft)
1851 if (ft->wp != NULL) {
1852 if (ft->wp->fd == -1)
1853 return (xstrdup("1"));
1854 return (xstrdup("0"));
1856 return (NULL);
1859 /* Callback for pane_dead_signal. */
1860 static void *
1861 format_cb_pane_dead_signal(struct format_tree *ft)
1863 struct window_pane *wp = ft->wp;
1864 const char *name;
1866 if (wp != NULL) {
1867 if ((wp->flags & PANE_STATUSREADY) && WIFSIGNALED(wp->status)) {
1868 name = sig2name(WTERMSIG(wp->status));
1869 return (format_printf("%s", name));
1871 return (NULL);
1873 return (NULL);
1876 /* Callback for pane_dead_status. */
1877 static void *
1878 format_cb_pane_dead_status(struct format_tree *ft)
1880 struct window_pane *wp = ft->wp;
1882 if (wp != NULL) {
1883 if ((wp->flags & PANE_STATUSREADY) && WIFEXITED(wp->status))
1884 return (format_printf("%d", WEXITSTATUS(wp->status)));
1885 return (NULL);
1887 return (NULL);
1890 /* Callback for pane_dead_time. */
1891 static void *
1892 format_cb_pane_dead_time(struct format_tree *ft)
1894 struct window_pane *wp = ft->wp;
1896 if (wp != NULL) {
1897 if (wp->flags & PANE_STATUSDRAWN)
1898 return (&wp->dead_time);
1899 return (NULL);
1901 return (NULL);
1904 /* Callback for pane_format. */
1905 static void *
1906 format_cb_pane_format(struct format_tree *ft)
1908 if (ft->type == FORMAT_TYPE_PANE)
1909 return (xstrdup("1"));
1910 return (xstrdup("0"));
1913 /* Callback for pane_height. */
1914 static void *
1915 format_cb_pane_height(struct format_tree *ft)
1917 if (ft->wp != NULL)
1918 return (format_printf("%u", ft->wp->sy));
1919 return (NULL);
1922 /* Callback for pane_id. */
1923 static void *
1924 format_cb_pane_id(struct format_tree *ft)
1926 if (ft->wp != NULL)
1927 return (format_printf("%%%u", ft->wp->id));
1928 return (NULL);
1931 /* Callback for pane_index. */
1932 static void *
1933 format_cb_pane_index(struct format_tree *ft)
1935 u_int idx;
1937 if (ft->wp != NULL && window_pane_index(ft->wp, &idx) == 0)
1938 return (format_printf("%u", idx));
1939 return (NULL);
1942 /* Callback for pane_input_off. */
1943 static void *
1944 format_cb_pane_input_off(struct format_tree *ft)
1946 if (ft->wp != NULL) {
1947 if (ft->wp->flags & PANE_INPUTOFF)
1948 return (xstrdup("1"));
1949 return (xstrdup("0"));
1951 return (NULL);
1954 /* Callback for pane_unseen_changes. */
1955 static void *
1956 format_cb_pane_unseen_changes(struct format_tree *ft)
1958 if (ft->wp != NULL) {
1959 if (ft->wp->flags & PANE_UNSEENCHANGES)
1960 return (xstrdup("1"));
1961 return (xstrdup("0"));
1963 return (NULL);
1966 /* Callback for pane_last. */
1967 static void *
1968 format_cb_pane_last(struct format_tree *ft)
1970 if (ft->wp != NULL) {
1971 if (ft->wp == TAILQ_FIRST(&ft->wp->window->last_panes))
1972 return (xstrdup("1"));
1973 return (xstrdup("0"));
1975 return (NULL);
1978 /* Callback for pane_left. */
1979 static void *
1980 format_cb_pane_left(struct format_tree *ft)
1982 if (ft->wp != NULL)
1983 return (format_printf("%u", ft->wp->xoff));
1984 return (NULL);
1987 /* Callback for pane_marked. */
1988 static void *
1989 format_cb_pane_marked(struct format_tree *ft)
1991 if (ft->wp != NULL) {
1992 if (server_check_marked() && marked_pane.wp == ft->wp)
1993 return (xstrdup("1"));
1994 return (xstrdup("0"));
1996 return (NULL);
1999 /* Callback for pane_marked_set. */
2000 static void *
2001 format_cb_pane_marked_set(struct format_tree *ft)
2003 if (ft->wp != NULL) {
2004 if (server_check_marked())
2005 return (xstrdup("1"));
2006 return (xstrdup("0"));
2008 return (NULL);
2011 /* Callback for pane_mode. */
2012 static void *
2013 format_cb_pane_mode(struct format_tree *ft)
2015 struct window_mode_entry *wme;
2017 if (ft->wp != NULL) {
2018 wme = TAILQ_FIRST(&ft->wp->modes);
2019 if (wme != NULL)
2020 return (xstrdup(wme->mode->name));
2021 return (NULL);
2023 return (NULL);
2026 /* Callback for pane_path. */
2027 static void *
2028 format_cb_pane_path(struct format_tree *ft)
2030 if (ft->wp != NULL) {
2031 if (ft->wp->base.path == NULL)
2032 return (xstrdup(""));
2033 return (xstrdup(ft->wp->base.path));
2035 return (NULL);
2038 /* Callback for pane_pid. */
2039 static void *
2040 format_cb_pane_pid(struct format_tree *ft)
2042 if (ft->wp != NULL)
2043 return (format_printf("%ld", (long)ft->wp->pid));
2044 return (NULL);
2047 /* Callback for pane_pipe. */
2048 static void *
2049 format_cb_pane_pipe(struct format_tree *ft)
2051 if (ft->wp != NULL) {
2052 if (ft->wp->pipe_fd != -1)
2053 return (xstrdup("1"));
2054 return (xstrdup("0"));
2056 return (NULL);
2059 /* Callback for pane_right. */
2060 static void *
2061 format_cb_pane_right(struct format_tree *ft)
2063 if (ft->wp != NULL)
2064 return (format_printf("%u", ft->wp->xoff + ft->wp->sx - 1));
2065 return (NULL);
2068 /* Callback for pane_search_string. */
2069 static void *
2070 format_cb_pane_search_string(struct format_tree *ft)
2072 if (ft->wp != NULL) {
2073 if (ft->wp->searchstr == NULL)
2074 return (xstrdup(""));
2075 return (xstrdup(ft->wp->searchstr));
2077 return (NULL);
2080 /* Callback for pane_synchronized. */
2081 static void *
2082 format_cb_pane_synchronized(struct format_tree *ft)
2084 if (ft->wp != NULL) {
2085 if (options_get_number(ft->wp->options, "synchronize-panes"))
2086 return (xstrdup("1"));
2087 return (xstrdup("0"));
2089 return (NULL);
2092 /* Callback for pane_title. */
2093 static void *
2094 format_cb_pane_title(struct format_tree *ft)
2096 if (ft->wp != NULL)
2097 return (xstrdup(ft->wp->base.title));
2098 return (NULL);
2101 /* Callback for pane_top. */
2102 static void *
2103 format_cb_pane_top(struct format_tree *ft)
2105 if (ft->wp != NULL)
2106 return (format_printf("%u", ft->wp->yoff));
2107 return (NULL);
2110 /* Callback for pane_tty. */
2111 static void *
2112 format_cb_pane_tty(struct format_tree *ft)
2114 if (ft->wp != NULL)
2115 return (xstrdup(ft->wp->tty));
2116 return (NULL);
2119 /* Callback for pane_width. */
2120 static void *
2121 format_cb_pane_width(struct format_tree *ft)
2123 if (ft->wp != NULL)
2124 return (format_printf("%u", ft->wp->sx));
2125 return (NULL);
2128 /* Callback for scroll_region_lower. */
2129 static void *
2130 format_cb_scroll_region_lower(struct format_tree *ft)
2132 if (ft->wp != NULL)
2133 return (format_printf("%u", ft->wp->base.rlower));
2134 return (NULL);
2137 /* Callback for scroll_region_upper. */
2138 static void *
2139 format_cb_scroll_region_upper(struct format_tree *ft)
2141 if (ft->wp != NULL)
2142 return (format_printf("%u", ft->wp->base.rupper));
2143 return (NULL);
2146 /* Callback for server_sessions. */
2147 static void *
2148 format_cb_server_sessions(__unused struct format_tree *ft)
2150 struct session *s;
2151 u_int n = 0;
2153 RB_FOREACH(s, sessions, &sessions)
2154 n++;
2155 return (format_printf("%u", n));
2158 /* Callback for session_attached. */
2159 static void *
2160 format_cb_session_attached(struct format_tree *ft)
2162 if (ft->s != NULL)
2163 return (format_printf("%u", ft->s->attached));
2164 return (NULL);
2167 /* Callback for session_format. */
2168 static void *
2169 format_cb_session_format(struct format_tree *ft)
2171 if (ft->type == FORMAT_TYPE_SESSION)
2172 return (xstrdup("1"));
2173 return (xstrdup("0"));
2176 /* Callback for session_group. */
2177 static void *
2178 format_cb_session_group(struct format_tree *ft)
2180 struct session_group *sg;
2182 if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL)
2183 return (xstrdup(sg->name));
2184 return (NULL);
2187 /* Callback for session_group_attached. */
2188 static void *
2189 format_cb_session_group_attached(struct format_tree *ft)
2191 struct session_group *sg;
2193 if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL)
2194 return (format_printf("%u", session_group_attached_count (sg)));
2195 return (NULL);
2198 /* Callback for session_group_many_attached. */
2199 static void *
2200 format_cb_session_group_many_attached(struct format_tree *ft)
2202 struct session_group *sg;
2204 if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL) {
2205 if (session_group_attached_count (sg) > 1)
2206 return (xstrdup("1"));
2207 return (xstrdup("0"));
2209 return (NULL);
2212 /* Callback for session_group_size. */
2213 static void *
2214 format_cb_session_group_size(struct format_tree *ft)
2216 struct session_group *sg;
2218 if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL)
2219 return (format_printf("%u", session_group_count (sg)));
2220 return (NULL);
2223 /* Callback for session_grouped. */
2224 static void *
2225 format_cb_session_grouped(struct format_tree *ft)
2227 if (ft->s != NULL) {
2228 if (session_group_contains(ft->s) != NULL)
2229 return (xstrdup("1"));
2230 return (xstrdup("0"));
2232 return (NULL);
2235 /* Callback for session_id. */
2236 static void *
2237 format_cb_session_id(struct format_tree *ft)
2239 if (ft->s != NULL)
2240 return (format_printf("$%u", ft->s->id));
2241 return (NULL);
2244 /* Callback for session_many_attached. */
2245 static void *
2246 format_cb_session_many_attached(struct format_tree *ft)
2248 if (ft->s != NULL) {
2249 if (ft->s->attached > 1)
2250 return (xstrdup("1"));
2251 return (xstrdup("0"));
2253 return (NULL);
2256 /* Callback for session_marked. */
2257 static void *
2258 format_cb_session_marked(struct format_tree *ft)
2260 if (ft->s != NULL) {
2261 if (server_check_marked() && marked_pane.s == ft->s)
2262 return (xstrdup("1"));
2263 return (xstrdup("0"));
2265 return (NULL);
2268 /* Callback for session_name. */
2269 static void *
2270 format_cb_session_name(struct format_tree *ft)
2272 if (ft->s != NULL)
2273 return (xstrdup(ft->s->name));
2274 return (NULL);
2277 /* Callback for session_path. */
2278 static void *
2279 format_cb_session_path(struct format_tree *ft)
2281 if (ft->s != NULL)
2282 return (xstrdup(ft->s->cwd));
2283 return (NULL);
2286 /* Callback for session_windows. */
2287 static void *
2288 format_cb_session_windows(struct format_tree *ft)
2290 if (ft->s != NULL)
2291 return (format_printf("%u", winlink_count(&ft->s->windows)));
2292 return (NULL);
2295 /* Callback for socket_path. */
2296 static void *
2297 format_cb_socket_path(__unused struct format_tree *ft)
2299 return (xstrdup(socket_path));
2302 /* Callback for version. */
2303 static void *
2304 format_cb_version(__unused struct format_tree *ft)
2306 return (xstrdup(getversion()));
2309 /* Callback for active_window_index. */
2310 static void *
2311 format_cb_active_window_index(struct format_tree *ft)
2313 if (ft->s != NULL)
2314 return (format_printf("%u", ft->s->curw->idx));
2315 return (NULL);
2318 /* Callback for last_window_index. */
2319 static void *
2320 format_cb_last_window_index(struct format_tree *ft)
2322 struct winlink *wl;
2324 if (ft->s != NULL) {
2325 wl = RB_MAX(winlinks, &ft->s->windows);
2326 return (format_printf("%u", wl->idx));
2328 return (NULL);
2331 /* Callback for window_active. */
2332 static void *
2333 format_cb_window_active(struct format_tree *ft)
2335 if (ft->wl != NULL) {
2336 if (ft->wl == ft->wl->session->curw)
2337 return (xstrdup("1"));
2338 return (xstrdup("0"));
2340 return (NULL);
2343 /* Callback for window_activity_flag. */
2344 static void *
2345 format_cb_window_activity_flag(struct format_tree *ft)
2347 if (ft->wl != NULL) {
2348 if (ft->wl->flags & WINLINK_ACTIVITY)
2349 return (xstrdup("1"));
2350 return (xstrdup("0"));
2352 return (NULL);
2355 /* Callback for window_bell_flag. */
2356 static void *
2357 format_cb_window_bell_flag(struct format_tree *ft)
2359 if (ft->wl != NULL) {
2360 if (ft->wl->flags & WINLINK_BELL)
2361 return (xstrdup("1"));
2362 return (xstrdup("0"));
2364 return (NULL);
2367 /* Callback for window_bigger. */
2368 static void *
2369 format_cb_window_bigger(struct format_tree *ft)
2371 u_int ox, oy, sx, sy;
2373 if (ft->c != NULL) {
2374 if (tty_window_offset(&ft->c->tty, &ox, &oy, &sx, &sy))
2375 return (xstrdup("1"));
2376 return (xstrdup("0"));
2378 return (NULL);
2381 /* Callback for window_cell_height. */
2382 static void *
2383 format_cb_window_cell_height(struct format_tree *ft)
2385 if (ft->w != NULL)
2386 return (format_printf("%u", ft->w->ypixel));
2387 return (NULL);
2390 /* Callback for window_cell_width. */
2391 static void *
2392 format_cb_window_cell_width(struct format_tree *ft)
2394 if (ft->w != NULL)
2395 return (format_printf("%u", ft->w->xpixel));
2396 return (NULL);
2399 /* Callback for window_end_flag. */
2400 static void *
2401 format_cb_window_end_flag(struct format_tree *ft)
2403 if (ft->wl != NULL) {
2404 if (ft->wl == RB_MAX(winlinks, &ft->wl->session->windows))
2405 return (xstrdup("1"));
2406 return (xstrdup("0"));
2408 return (NULL);
2411 /* Callback for window_flags. */
2412 static void *
2413 format_cb_window_flags(struct format_tree *ft)
2415 if (ft->wl != NULL)
2416 return (xstrdup(window_printable_flags(ft->wl, 1)));
2417 return (NULL);
2420 /* Callback for window_format. */
2421 static void *
2422 format_cb_window_format(struct format_tree *ft)
2424 if (ft->type == FORMAT_TYPE_WINDOW)
2425 return (xstrdup("1"));
2426 return (xstrdup("0"));
2429 /* Callback for window_height. */
2430 static void *
2431 format_cb_window_height(struct format_tree *ft)
2433 if (ft->w != NULL)
2434 return (format_printf("%u", ft->w->sy));
2435 return (NULL);
2438 /* Callback for window_id. */
2439 static void *
2440 format_cb_window_id(struct format_tree *ft)
2442 if (ft->w != NULL)
2443 return (format_printf("@%u", ft->w->id));
2444 return (NULL);
2447 /* Callback for window_index. */
2448 static void *
2449 format_cb_window_index(struct format_tree *ft)
2451 if (ft->wl != NULL)
2452 return (format_printf("%d", ft->wl->idx));
2453 return (NULL);
2456 /* Callback for window_last_flag. */
2457 static void *
2458 format_cb_window_last_flag(struct format_tree *ft)
2460 if (ft->wl != NULL) {
2461 if (ft->wl == TAILQ_FIRST(&ft->wl->session->lastw))
2462 return (xstrdup("1"));
2463 return (xstrdup("0"));
2465 return (NULL);
2468 /* Callback for window_linked. */
2469 static void *
2470 format_cb_window_linked(struct format_tree *ft)
2472 if (ft->wl != NULL) {
2473 if (session_is_linked(ft->wl->session, ft->wl->window))
2474 return (xstrdup("1"));
2475 return (xstrdup("0"));
2477 return (NULL);
2480 /* Callback for window_linked_sessions. */
2481 static void *
2482 format_cb_window_linked_sessions(struct format_tree *ft)
2484 if (ft->wl != NULL)
2485 return (format_printf("%u", ft->wl->window->references));
2486 return (NULL);
2489 /* Callback for window_marked_flag. */
2490 static void *
2491 format_cb_window_marked_flag(struct format_tree *ft)
2493 if (ft->wl != NULL) {
2494 if (server_check_marked() && marked_pane.wl == ft->wl)
2495 return (xstrdup("1"));
2496 return (xstrdup("0"));
2498 return (NULL);
2501 /* Callback for window_name. */
2502 static void *
2503 format_cb_window_name(struct format_tree *ft)
2505 if (ft->w != NULL)
2506 return (format_printf("%s", ft->w->name));
2507 return (NULL);
2510 /* Callback for window_offset_x. */
2511 static void *
2512 format_cb_window_offset_x(struct format_tree *ft)
2514 u_int ox, oy, sx, sy;
2516 if (ft->c != NULL) {
2517 if (tty_window_offset(&ft->c->tty, &ox, &oy, &sx, &sy))
2518 return (format_printf("%u", ox));
2519 return (NULL);
2521 return (NULL);
2524 /* Callback for window_offset_y. */
2525 static void *
2526 format_cb_window_offset_y(struct format_tree *ft)
2528 u_int ox, oy, sx, sy;
2530 if (ft->c != NULL) {
2531 if (tty_window_offset(&ft->c->tty, &ox, &oy, &sx, &sy))
2532 return (format_printf("%u", oy));
2533 return (NULL);
2535 return (NULL);
2538 /* Callback for window_panes. */
2539 static void *
2540 format_cb_window_panes(struct format_tree *ft)
2542 if (ft->w != NULL)
2543 return (format_printf("%u", window_count_panes(ft->w)));
2544 return (NULL);
2547 /* Callback for window_raw_flags. */
2548 static void *
2549 format_cb_window_raw_flags(struct format_tree *ft)
2551 if (ft->wl != NULL)
2552 return (xstrdup(window_printable_flags(ft->wl, 0)));
2553 return (NULL);
2556 /* Callback for window_silence_flag. */
2557 static void *
2558 format_cb_window_silence_flag(struct format_tree *ft)
2560 if (ft->wl != NULL) {
2561 if (ft->wl->flags & WINLINK_SILENCE)
2562 return (xstrdup("1"));
2563 return (xstrdup("0"));
2565 return (NULL);
2568 /* Callback for window_start_flag. */
2569 static void *
2570 format_cb_window_start_flag(struct format_tree *ft)
2572 if (ft->wl != NULL) {
2573 if (ft->wl == RB_MIN(winlinks, &ft->wl->session->windows))
2574 return (xstrdup("1"));
2575 return (xstrdup("0"));
2577 return (NULL);
2580 /* Callback for window_width. */
2581 static void *
2582 format_cb_window_width(struct format_tree *ft)
2584 if (ft->w != NULL)
2585 return (format_printf("%u", ft->w->sx));
2586 return (NULL);
2589 /* Callback for window_zoomed_flag. */
2590 static void *
2591 format_cb_window_zoomed_flag(struct format_tree *ft)
2593 if (ft->w != NULL) {
2594 if (ft->w->flags & WINDOW_ZOOMED)
2595 return (xstrdup("1"));
2596 return (xstrdup("0"));
2598 return (NULL);
2601 /* Callback for wrap_flag. */
2602 static void *
2603 format_cb_wrap_flag(struct format_tree *ft)
2605 if (ft->wp != NULL) {
2606 if (ft->wp->base.mode & MODE_WRAP)
2607 return (xstrdup("1"));
2608 return (xstrdup("0"));
2610 return (NULL);
2613 /* Callback for buffer_created. */
2614 static void *
2615 format_cb_buffer_created(struct format_tree *ft)
2617 static struct timeval tv;
2619 if (ft->pb != NULL) {
2620 timerclear(&tv);
2621 tv.tv_sec = paste_buffer_created(ft->pb);
2622 return (&tv);
2624 return (NULL);
2627 /* Callback for client_activity. */
2628 static void *
2629 format_cb_client_activity(struct format_tree *ft)
2631 if (ft->c != NULL)
2632 return (&ft->c->activity_time);
2633 return (NULL);
2636 /* Callback for client_created. */
2637 static void *
2638 format_cb_client_created(struct format_tree *ft)
2640 if (ft->c != NULL)
2641 return (&ft->c->creation_time);
2642 return (NULL);
2645 /* Callback for session_activity. */
2646 static void *
2647 format_cb_session_activity(struct format_tree *ft)
2649 if (ft->s != NULL)
2650 return (&ft->s->activity_time);
2651 return (NULL);
2654 /* Callback for session_created. */
2655 static void *
2656 format_cb_session_created(struct format_tree *ft)
2658 if (ft->s != NULL)
2659 return (&ft->s->creation_time);
2660 return (NULL);
2663 /* Callback for session_last_attached. */
2664 static void *
2665 format_cb_session_last_attached(struct format_tree *ft)
2667 if (ft->s != NULL)
2668 return (&ft->s->last_attached_time);
2669 return (NULL);
2672 /* Callback for start_time. */
2673 static void *
2674 format_cb_start_time(__unused struct format_tree *ft)
2676 return (&start_time);
2679 /* Callback for window_activity. */
2680 static void *
2681 format_cb_window_activity(struct format_tree *ft)
2683 if (ft->w != NULL)
2684 return (&ft->w->activity_time);
2685 return (NULL);
2688 /* Callback for buffer_mode_format, */
2689 static void *
2690 format_cb_buffer_mode_format(__unused struct format_tree *ft)
2692 return (xstrdup(window_buffer_mode.default_format));
2695 /* Callback for client_mode_format, */
2696 static void *
2697 format_cb_client_mode_format(__unused struct format_tree *ft)
2699 return (xstrdup(window_client_mode.default_format));
2702 /* Callback for tree_mode_format, */
2703 static void *
2704 format_cb_tree_mode_format(__unused struct format_tree *ft)
2706 return (xstrdup(window_tree_mode.default_format));
2709 /* Callback for uid. */
2710 static void *
2711 format_cb_uid(__unused struct format_tree *ft)
2713 return (format_printf("%ld", (long)getuid()));
2716 /* Callback for user. */
2717 static void *
2718 format_cb_user(__unused struct format_tree *ft)
2720 struct passwd *pw;
2722 if ((pw = getpwuid(getuid())) != NULL)
2723 return (xstrdup(pw->pw_name));
2724 return (NULL);
2727 /* Format table type. */
2728 enum format_table_type {
2729 FORMAT_TABLE_STRING,
2730 FORMAT_TABLE_TIME
2733 /* Format table entry. */
2734 struct format_table_entry {
2735 const char *key;
2736 enum format_table_type type;
2737 format_cb cb;
2741 * Format table. Default format variables (that are almost always in the tree
2742 * and where the value is expanded by a callback in this file) are listed here.
2743 * Only variables which are added by the caller go into the tree.
2745 static const struct format_table_entry format_table[] = {
2746 { "active_window_index", FORMAT_TABLE_STRING,
2747 format_cb_active_window_index
2749 { "alternate_on", FORMAT_TABLE_STRING,
2750 format_cb_alternate_on
2752 { "alternate_saved_x", FORMAT_TABLE_STRING,
2753 format_cb_alternate_saved_x
2755 { "alternate_saved_y", FORMAT_TABLE_STRING,
2756 format_cb_alternate_saved_y
2758 { "buffer_created", FORMAT_TABLE_TIME,
2759 format_cb_buffer_created
2761 { "buffer_mode_format", FORMAT_TABLE_STRING,
2762 format_cb_buffer_mode_format
2764 { "buffer_name", FORMAT_TABLE_STRING,
2765 format_cb_buffer_name
2767 { "buffer_sample", FORMAT_TABLE_STRING,
2768 format_cb_buffer_sample
2770 { "buffer_size", FORMAT_TABLE_STRING,
2771 format_cb_buffer_size
2773 { "client_activity", FORMAT_TABLE_TIME,
2774 format_cb_client_activity
2776 { "client_cell_height", FORMAT_TABLE_STRING,
2777 format_cb_client_cell_height
2779 { "client_cell_width", FORMAT_TABLE_STRING,
2780 format_cb_client_cell_width
2782 { "client_control_mode", FORMAT_TABLE_STRING,
2783 format_cb_client_control_mode
2785 { "client_created", FORMAT_TABLE_TIME,
2786 format_cb_client_created
2788 { "client_discarded", FORMAT_TABLE_STRING,
2789 format_cb_client_discarded
2791 { "client_flags", FORMAT_TABLE_STRING,
2792 format_cb_client_flags
2794 { "client_height", FORMAT_TABLE_STRING,
2795 format_cb_client_height
2797 { "client_key_table", FORMAT_TABLE_STRING,
2798 format_cb_client_key_table
2800 { "client_last_session", FORMAT_TABLE_STRING,
2801 format_cb_client_last_session
2803 { "client_mode_format", FORMAT_TABLE_STRING,
2804 format_cb_client_mode_format
2806 { "client_name", FORMAT_TABLE_STRING,
2807 format_cb_client_name
2809 { "client_pid", FORMAT_TABLE_STRING,
2810 format_cb_client_pid
2812 { "client_prefix", FORMAT_TABLE_STRING,
2813 format_cb_client_prefix
2815 { "client_readonly", FORMAT_TABLE_STRING,
2816 format_cb_client_readonly
2818 { "client_session", FORMAT_TABLE_STRING,
2819 format_cb_client_session
2821 { "client_termfeatures", FORMAT_TABLE_STRING,
2822 format_cb_client_termfeatures
2824 { "client_termname", FORMAT_TABLE_STRING,
2825 format_cb_client_termname
2827 { "client_termtype", FORMAT_TABLE_STRING,
2828 format_cb_client_termtype
2830 { "client_tty", FORMAT_TABLE_STRING,
2831 format_cb_client_tty
2833 { "client_uid", FORMAT_TABLE_STRING,
2834 format_cb_client_uid
2836 { "client_user", FORMAT_TABLE_STRING,
2837 format_cb_client_user
2839 { "client_utf8", FORMAT_TABLE_STRING,
2840 format_cb_client_utf8
2842 { "client_width", FORMAT_TABLE_STRING,
2843 format_cb_client_width
2845 { "client_written", FORMAT_TABLE_STRING,
2846 format_cb_client_written
2848 { "config_files", FORMAT_TABLE_STRING,
2849 format_cb_config_files
2851 { "cursor_character", FORMAT_TABLE_STRING,
2852 format_cb_cursor_character
2854 { "cursor_flag", FORMAT_TABLE_STRING,
2855 format_cb_cursor_flag
2857 { "cursor_x", FORMAT_TABLE_STRING,
2858 format_cb_cursor_x
2860 { "cursor_y", FORMAT_TABLE_STRING,
2861 format_cb_cursor_y
2863 { "history_all_bytes", FORMAT_TABLE_STRING,
2864 format_cb_history_all_bytes
2866 { "history_bytes", FORMAT_TABLE_STRING,
2867 format_cb_history_bytes
2869 { "history_limit", FORMAT_TABLE_STRING,
2870 format_cb_history_limit
2872 { "history_size", FORMAT_TABLE_STRING,
2873 format_cb_history_size
2875 { "host", FORMAT_TABLE_STRING,
2876 format_cb_host
2878 { "host_short", FORMAT_TABLE_STRING,
2879 format_cb_host_short
2881 { "insert_flag", FORMAT_TABLE_STRING,
2882 format_cb_insert_flag
2884 { "keypad_cursor_flag", FORMAT_TABLE_STRING,
2885 format_cb_keypad_cursor_flag
2887 { "keypad_flag", FORMAT_TABLE_STRING,
2888 format_cb_keypad_flag
2890 { "last_window_index", FORMAT_TABLE_STRING,
2891 format_cb_last_window_index
2893 { "mouse_all_flag", FORMAT_TABLE_STRING,
2894 format_cb_mouse_all_flag
2896 { "mouse_any_flag", FORMAT_TABLE_STRING,
2897 format_cb_mouse_any_flag
2899 { "mouse_button_flag", FORMAT_TABLE_STRING,
2900 format_cb_mouse_button_flag
2902 { "mouse_hyperlink", FORMAT_TABLE_STRING,
2903 format_cb_mouse_hyperlink
2905 { "mouse_line", FORMAT_TABLE_STRING,
2906 format_cb_mouse_line
2908 { "mouse_pane", FORMAT_TABLE_STRING,
2909 format_cb_mouse_pane
2911 { "mouse_sgr_flag", FORMAT_TABLE_STRING,
2912 format_cb_mouse_sgr_flag
2914 { "mouse_standard_flag", FORMAT_TABLE_STRING,
2915 format_cb_mouse_standard_flag
2917 { "mouse_status_line", FORMAT_TABLE_STRING,
2918 format_cb_mouse_status_line
2920 { "mouse_status_range", FORMAT_TABLE_STRING,
2921 format_cb_mouse_status_range
2923 { "mouse_utf8_flag", FORMAT_TABLE_STRING,
2924 format_cb_mouse_utf8_flag
2926 { "mouse_word", FORMAT_TABLE_STRING,
2927 format_cb_mouse_word
2929 { "mouse_x", FORMAT_TABLE_STRING,
2930 format_cb_mouse_x
2932 { "mouse_y", FORMAT_TABLE_STRING,
2933 format_cb_mouse_y
2935 { "next_session_id", FORMAT_TABLE_STRING,
2936 format_cb_next_session_id
2938 { "origin_flag", FORMAT_TABLE_STRING,
2939 format_cb_origin_flag
2941 { "pane_active", FORMAT_TABLE_STRING,
2942 format_cb_pane_active
2944 { "pane_at_bottom", FORMAT_TABLE_STRING,
2945 format_cb_pane_at_bottom
2947 { "pane_at_left", FORMAT_TABLE_STRING,
2948 format_cb_pane_at_left
2950 { "pane_at_right", FORMAT_TABLE_STRING,
2951 format_cb_pane_at_right
2953 { "pane_at_top", FORMAT_TABLE_STRING,
2954 format_cb_pane_at_top
2956 { "pane_bg", FORMAT_TABLE_STRING,
2957 format_cb_pane_bg
2959 { "pane_bottom", FORMAT_TABLE_STRING,
2960 format_cb_pane_bottom
2962 { "pane_current_command", FORMAT_TABLE_STRING,
2963 format_cb_current_command
2965 { "pane_current_path", FORMAT_TABLE_STRING,
2966 format_cb_current_path
2968 { "pane_dead", FORMAT_TABLE_STRING,
2969 format_cb_pane_dead
2971 { "pane_dead_signal", FORMAT_TABLE_STRING,
2972 format_cb_pane_dead_signal
2974 { "pane_dead_status", FORMAT_TABLE_STRING,
2975 format_cb_pane_dead_status
2977 { "pane_dead_time", FORMAT_TABLE_TIME,
2978 format_cb_pane_dead_time
2980 { "pane_fg", FORMAT_TABLE_STRING,
2981 format_cb_pane_fg
2983 { "pane_format", FORMAT_TABLE_STRING,
2984 format_cb_pane_format
2986 { "pane_height", FORMAT_TABLE_STRING,
2987 format_cb_pane_height
2989 { "pane_id", FORMAT_TABLE_STRING,
2990 format_cb_pane_id
2992 { "pane_in_mode", FORMAT_TABLE_STRING,
2993 format_cb_pane_in_mode
2995 { "pane_index", FORMAT_TABLE_STRING,
2996 format_cb_pane_index
2998 { "pane_input_off", FORMAT_TABLE_STRING,
2999 format_cb_pane_input_off
3001 { "pane_last", FORMAT_TABLE_STRING,
3002 format_cb_pane_last
3004 { "pane_left", FORMAT_TABLE_STRING,
3005 format_cb_pane_left
3007 { "pane_marked", FORMAT_TABLE_STRING,
3008 format_cb_pane_marked
3010 { "pane_marked_set", FORMAT_TABLE_STRING,
3011 format_cb_pane_marked_set
3013 { "pane_mode", FORMAT_TABLE_STRING,
3014 format_cb_pane_mode
3016 { "pane_path", FORMAT_TABLE_STRING,
3017 format_cb_pane_path
3019 { "pane_pid", FORMAT_TABLE_STRING,
3020 format_cb_pane_pid
3022 { "pane_pipe", FORMAT_TABLE_STRING,
3023 format_cb_pane_pipe
3025 { "pane_right", FORMAT_TABLE_STRING,
3026 format_cb_pane_right
3028 { "pane_search_string", FORMAT_TABLE_STRING,
3029 format_cb_pane_search_string
3031 { "pane_start_command", FORMAT_TABLE_STRING,
3032 format_cb_start_command
3034 { "pane_start_path", FORMAT_TABLE_STRING,
3035 format_cb_start_path
3037 { "pane_synchronized", FORMAT_TABLE_STRING,
3038 format_cb_pane_synchronized
3040 { "pane_tabs", FORMAT_TABLE_STRING,
3041 format_cb_pane_tabs
3043 { "pane_title", FORMAT_TABLE_STRING,
3044 format_cb_pane_title
3046 { "pane_top", FORMAT_TABLE_STRING,
3047 format_cb_pane_top
3049 { "pane_tty", FORMAT_TABLE_STRING,
3050 format_cb_pane_tty
3052 { "pane_unseen_changes", FORMAT_TABLE_STRING,
3053 format_cb_pane_unseen_changes
3055 { "pane_width", FORMAT_TABLE_STRING,
3056 format_cb_pane_width
3058 { "pid", FORMAT_TABLE_STRING,
3059 format_cb_pid
3061 { "scroll_region_lower", FORMAT_TABLE_STRING,
3062 format_cb_scroll_region_lower
3064 { "scroll_region_upper", FORMAT_TABLE_STRING,
3065 format_cb_scroll_region_upper
3067 { "server_sessions", FORMAT_TABLE_STRING,
3068 format_cb_server_sessions
3070 { "session_activity", FORMAT_TABLE_TIME,
3071 format_cb_session_activity
3073 { "session_alerts", FORMAT_TABLE_STRING,
3074 format_cb_session_alerts
3076 { "session_attached", FORMAT_TABLE_STRING,
3077 format_cb_session_attached
3079 { "session_attached_list", FORMAT_TABLE_STRING,
3080 format_cb_session_attached_list
3082 { "session_created", FORMAT_TABLE_TIME,
3083 format_cb_session_created
3085 { "session_format", FORMAT_TABLE_STRING,
3086 format_cb_session_format
3088 { "session_group", FORMAT_TABLE_STRING,
3089 format_cb_session_group
3091 { "session_group_attached", FORMAT_TABLE_STRING,
3092 format_cb_session_group_attached
3094 { "session_group_attached_list", FORMAT_TABLE_STRING,
3095 format_cb_session_group_attached_list
3097 { "session_group_list", FORMAT_TABLE_STRING,
3098 format_cb_session_group_list
3100 { "session_group_many_attached", FORMAT_TABLE_STRING,
3101 format_cb_session_group_many_attached
3103 { "session_group_size", FORMAT_TABLE_STRING,
3104 format_cb_session_group_size
3106 { "session_grouped", FORMAT_TABLE_STRING,
3107 format_cb_session_grouped
3109 { "session_id", FORMAT_TABLE_STRING,
3110 format_cb_session_id
3112 { "session_last_attached", FORMAT_TABLE_TIME,
3113 format_cb_session_last_attached
3115 { "session_many_attached", FORMAT_TABLE_STRING,
3116 format_cb_session_many_attached
3118 { "session_marked", FORMAT_TABLE_STRING,
3119 format_cb_session_marked,
3121 { "session_name", FORMAT_TABLE_STRING,
3122 format_cb_session_name
3124 { "session_path", FORMAT_TABLE_STRING,
3125 format_cb_session_path
3127 { "session_stack", FORMAT_TABLE_STRING,
3128 format_cb_session_stack
3130 { "session_windows", FORMAT_TABLE_STRING,
3131 format_cb_session_windows
3133 { "socket_path", FORMAT_TABLE_STRING,
3134 format_cb_socket_path
3136 { "start_time", FORMAT_TABLE_TIME,
3137 format_cb_start_time
3139 { "tree_mode_format", FORMAT_TABLE_STRING,
3140 format_cb_tree_mode_format
3142 { "uid", FORMAT_TABLE_STRING,
3143 format_cb_uid
3145 { "user", FORMAT_TABLE_STRING,
3146 format_cb_user
3148 { "version", FORMAT_TABLE_STRING,
3149 format_cb_version
3151 { "window_active", FORMAT_TABLE_STRING,
3152 format_cb_window_active
3154 { "window_active_clients", FORMAT_TABLE_STRING,
3155 format_cb_window_active_clients
3157 { "window_active_clients_list", FORMAT_TABLE_STRING,
3158 format_cb_window_active_clients_list
3160 { "window_active_sessions", FORMAT_TABLE_STRING,
3161 format_cb_window_active_sessions
3163 { "window_active_sessions_list", FORMAT_TABLE_STRING,
3164 format_cb_window_active_sessions_list
3166 { "window_activity", FORMAT_TABLE_TIME,
3167 format_cb_window_activity
3169 { "window_activity_flag", FORMAT_TABLE_STRING,
3170 format_cb_window_activity_flag
3172 { "window_bell_flag", FORMAT_TABLE_STRING,
3173 format_cb_window_bell_flag
3175 { "window_bigger", FORMAT_TABLE_STRING,
3176 format_cb_window_bigger
3178 { "window_cell_height", FORMAT_TABLE_STRING,
3179 format_cb_window_cell_height
3181 { "window_cell_width", FORMAT_TABLE_STRING,
3182 format_cb_window_cell_width
3184 { "window_end_flag", FORMAT_TABLE_STRING,
3185 format_cb_window_end_flag
3187 { "window_flags", FORMAT_TABLE_STRING,
3188 format_cb_window_flags
3190 { "window_format", FORMAT_TABLE_STRING,
3191 format_cb_window_format
3193 { "window_height", FORMAT_TABLE_STRING,
3194 format_cb_window_height
3196 { "window_id", FORMAT_TABLE_STRING,
3197 format_cb_window_id
3199 { "window_index", FORMAT_TABLE_STRING,
3200 format_cb_window_index
3202 { "window_last_flag", FORMAT_TABLE_STRING,
3203 format_cb_window_last_flag
3205 { "window_layout", FORMAT_TABLE_STRING,
3206 format_cb_window_layout
3208 { "window_linked", FORMAT_TABLE_STRING,
3209 format_cb_window_linked
3211 { "window_linked_sessions", FORMAT_TABLE_STRING,
3212 format_cb_window_linked_sessions
3214 { "window_linked_sessions_list", FORMAT_TABLE_STRING,
3215 format_cb_window_linked_sessions_list
3217 { "window_marked_flag", FORMAT_TABLE_STRING,
3218 format_cb_window_marked_flag
3220 { "window_name", FORMAT_TABLE_STRING,
3221 format_cb_window_name
3223 { "window_offset_x", FORMAT_TABLE_STRING,
3224 format_cb_window_offset_x
3226 { "window_offset_y", FORMAT_TABLE_STRING,
3227 format_cb_window_offset_y
3229 { "window_panes", FORMAT_TABLE_STRING,
3230 format_cb_window_panes
3232 { "window_raw_flags", FORMAT_TABLE_STRING,
3233 format_cb_window_raw_flags
3235 { "window_silence_flag", FORMAT_TABLE_STRING,
3236 format_cb_window_silence_flag
3238 { "window_stack_index", FORMAT_TABLE_STRING,
3239 format_cb_window_stack_index
3241 { "window_start_flag", FORMAT_TABLE_STRING,
3242 format_cb_window_start_flag
3244 { "window_visible_layout", FORMAT_TABLE_STRING,
3245 format_cb_window_visible_layout
3247 { "window_width", FORMAT_TABLE_STRING,
3248 format_cb_window_width
3250 { "window_zoomed_flag", FORMAT_TABLE_STRING,
3251 format_cb_window_zoomed_flag
3253 { "wrap_flag", FORMAT_TABLE_STRING,
3254 format_cb_wrap_flag
3258 /* Compare format table entries. */
3259 static int
3260 format_table_compare(const void *key0, const void *entry0)
3262 const char *key = key0;
3263 const struct format_table_entry *entry = entry0;
3265 return (strcmp(key, entry->key));
3268 /* Get a format callback. */
3269 static struct format_table_entry *
3270 format_table_get(const char *key)
3272 return (bsearch(key, format_table, nitems(format_table),
3273 sizeof *format_table, format_table_compare));
3276 /* Merge one format tree into another. */
3277 void
3278 format_merge(struct format_tree *ft, struct format_tree *from)
3280 struct format_entry *fe;
3282 RB_FOREACH(fe, format_entry_tree, &from->tree) {
3283 if (fe->value != NULL)
3284 format_add(ft, fe->key, "%s", fe->value);
3288 /* Get format pane. */
3289 struct window_pane *
3290 format_get_pane(struct format_tree *ft)
3292 return (ft->wp);
3295 /* Add item bits to tree. */
3296 static void
3297 format_create_add_item(struct format_tree *ft, struct cmdq_item *item)
3299 struct key_event *event = cmdq_get_event(item);
3300 struct mouse_event *m = &event->m;
3302 cmdq_merge_formats(item, ft);
3303 memcpy(&ft->m, m, sizeof ft->m);
3306 /* Create a new tree. */
3307 struct format_tree *
3308 format_create(struct client *c, struct cmdq_item *item, int tag, int flags)
3310 struct format_tree *ft;
3312 ft = xcalloc(1, sizeof *ft);
3313 RB_INIT(&ft->tree);
3315 if (c != NULL) {
3316 ft->client = c;
3317 ft->client->references++;
3319 ft->item = item;
3321 ft->tag = tag;
3322 ft->flags = flags;
3324 if (item != NULL)
3325 format_create_add_item(ft, item);
3327 return (ft);
3330 /* Free a tree. */
3331 void
3332 format_free(struct format_tree *ft)
3334 struct format_entry *fe, *fe1;
3336 RB_FOREACH_SAFE(fe, format_entry_tree, &ft->tree, fe1) {
3337 RB_REMOVE(format_entry_tree, &ft->tree, fe);
3338 free(fe->value);
3339 free(fe->key);
3340 free(fe);
3343 if (ft->client != NULL)
3344 server_client_unref(ft->client);
3345 free(ft);
3348 /* Log each format. */
3349 static void
3350 format_log_debug_cb(const char *key, const char *value, void *arg)
3352 const char *prefix = arg;
3354 log_debug("%s: %s=%s", prefix, key, value);
3357 /* Log a format tree. */
3358 void
3359 format_log_debug(struct format_tree *ft, const char *prefix)
3361 format_each(ft, format_log_debug_cb, (void *)prefix);
3364 /* Walk each format. */
3365 void
3366 format_each(struct format_tree *ft, void (*cb)(const char *, const char *,
3367 void *), void *arg)
3369 const struct format_table_entry *fte;
3370 struct format_entry *fe;
3371 u_int i;
3372 char s[64];
3373 void *value;
3374 struct timeval *tv;
3376 for (i = 0; i < nitems(format_table); i++) {
3377 fte = &format_table[i];
3379 value = fte->cb(ft);
3380 if (value == NULL)
3381 continue;
3382 if (fte->type == FORMAT_TABLE_TIME) {
3383 tv = value;
3384 xsnprintf(s, sizeof s, "%lld", (long long)tv->tv_sec);
3385 cb(fte->key, s, arg);
3386 } else {
3387 cb(fte->key, value, arg);
3388 free(value);
3391 RB_FOREACH(fe, format_entry_tree, &ft->tree) {
3392 if (fe->time != 0) {
3393 xsnprintf(s, sizeof s, "%lld", (long long)fe->time);
3394 cb(fe->key, s, arg);
3395 } else {
3396 if (fe->value == NULL && fe->cb != NULL) {
3397 fe->value = fe->cb(ft);
3398 if (fe->value == NULL)
3399 fe->value = xstrdup("");
3401 cb(fe->key, fe->value, arg);
3406 /* Add a key-value pair. */
3407 void
3408 format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
3410 struct format_entry *fe;
3411 struct format_entry *fe_now;
3412 va_list ap;
3414 fe = xmalloc(sizeof *fe);
3415 fe->key = xstrdup(key);
3417 fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
3418 if (fe_now != NULL) {
3419 free(fe->key);
3420 free(fe);
3421 free(fe_now->value);
3422 fe = fe_now;
3425 fe->cb = NULL;
3426 fe->time = 0;
3428 va_start(ap, fmt);
3429 xvasprintf(&fe->value, fmt, ap);
3430 va_end(ap);
3433 /* Add a key and time. */
3434 void
3435 format_add_tv(struct format_tree *ft, const char *key, struct timeval *tv)
3437 struct format_entry *fe, *fe_now;
3439 fe = xmalloc(sizeof *fe);
3440 fe->key = xstrdup(key);
3442 fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
3443 if (fe_now != NULL) {
3444 free(fe->key);
3445 free(fe);
3446 free(fe_now->value);
3447 fe = fe_now;
3450 fe->cb = NULL;
3451 fe->time = tv->tv_sec;
3453 fe->value = NULL;
3456 /* Add a key and function. */
3457 void
3458 format_add_cb(struct format_tree *ft, const char *key, format_cb cb)
3460 struct format_entry *fe;
3461 struct format_entry *fe_now;
3463 fe = xmalloc(sizeof *fe);
3464 fe->key = xstrdup(key);
3466 fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
3467 if (fe_now != NULL) {
3468 free(fe->key);
3469 free(fe);
3470 free(fe_now->value);
3471 fe = fe_now;
3474 fe->cb = cb;
3475 fe->time = 0;
3477 fe->value = NULL;
3480 /* Quote shell special characters in string. */
3481 static char *
3482 format_quote_shell(const char *s)
3484 const char *cp;
3485 char *out, *at;
3487 at = out = xmalloc(strlen(s) * 2 + 1);
3488 for (cp = s; *cp != '\0'; cp++) {
3489 if (strchr("|&;<>()$`\\\"'*?[# =%", *cp) != NULL)
3490 *at++ = '\\';
3491 *at++ = *cp;
3493 *at = '\0';
3494 return (out);
3497 /* Quote #s in string. */
3498 static char *
3499 format_quote_style(const char *s)
3501 const char *cp;
3502 char *out, *at;
3504 at = out = xmalloc(strlen(s) * 2 + 1);
3505 for (cp = s; *cp != '\0'; cp++) {
3506 if (*cp == '#')
3507 *at++ = '#';
3508 *at++ = *cp;
3510 *at = '\0';
3511 return (out);
3514 /* Make a prettier time. */
3515 char *
3516 format_pretty_time(time_t t, int seconds)
3518 struct tm now_tm, tm;
3519 time_t now, age;
3520 char s[9];
3522 time(&now);
3523 if (now < t)
3524 now = t;
3525 age = now - t;
3527 localtime_r(&now, &now_tm);
3528 localtime_r(&t, &tm);
3530 /* Last 24 hours. */
3531 if (age < 24 * 3600) {
3532 if (seconds)
3533 strftime(s, sizeof s, "%H:%M:%S", &tm);
3534 else
3535 strftime(s, sizeof s, "%H:%M", &tm);
3536 return (xstrdup(s));
3539 /* This month or last 28 days. */
3540 if ((tm.tm_year == now_tm.tm_year && tm.tm_mon == now_tm.tm_mon) ||
3541 age < 28 * 24 * 3600) {
3542 strftime(s, sizeof s, "%a%d", &tm);
3543 return (xstrdup(s));
3546 /* Last 12 months. */
3547 if ((tm.tm_year == now_tm.tm_year && tm.tm_mon < now_tm.tm_mon) ||
3548 (tm.tm_year == now_tm.tm_year - 1 && tm.tm_mon > now_tm.tm_mon)) {
3549 strftime(s, sizeof s, "%d%b", &tm);
3550 return (xstrdup(s));
3553 /* Older than that. */
3554 strftime(s, sizeof s, "%h%y", &tm);
3555 return (xstrdup(s));
3558 /* Find a format entry. */
3559 static char *
3560 format_find(struct format_tree *ft, const char *key, int modifiers,
3561 const char *time_format)
3563 struct format_table_entry *fte;
3564 void *value;
3565 struct format_entry *fe, fe_find;
3566 struct environ_entry *envent;
3567 struct options_entry *o;
3568 int idx;
3569 char *found = NULL, *saved, s[512];
3570 const char *errstr;
3571 time_t t = 0;
3572 struct tm tm;
3574 o = options_parse_get(global_options, key, &idx, 0);
3575 if (o == NULL && ft->wp != NULL)
3576 o = options_parse_get(ft->wp->options, key, &idx, 0);
3577 if (o == NULL && ft->w != NULL)
3578 o = options_parse_get(ft->w->options, key, &idx, 0);
3579 if (o == NULL)
3580 o = options_parse_get(global_w_options, key, &idx, 0);
3581 if (o == NULL && ft->s != NULL)
3582 o = options_parse_get(ft->s->options, key, &idx, 0);
3583 if (o == NULL)
3584 o = options_parse_get(global_s_options, key, &idx, 0);
3585 if (o != NULL) {
3586 found = options_to_string(o, idx, 1);
3587 goto found;
3590 fte = format_table_get(key);
3591 if (fte != NULL) {
3592 value = fte->cb(ft);
3593 if (fte->type == FORMAT_TABLE_TIME && value != NULL)
3594 t = ((struct timeval *)value)->tv_sec;
3595 else
3596 found = value;
3597 goto found;
3599 fe_find.key = (char *)key;
3600 fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find);
3601 if (fe != NULL) {
3602 if (fe->time != 0) {
3603 t = fe->time;
3604 goto found;
3606 if (fe->value == NULL && fe->cb != NULL) {
3607 fe->value = fe->cb(ft);
3608 if (fe->value == NULL)
3609 fe->value = xstrdup("");
3611 found = xstrdup(fe->value);
3612 goto found;
3615 if (~modifiers & FORMAT_TIMESTRING) {
3616 envent = NULL;
3617 if (ft->s != NULL)
3618 envent = environ_find(ft->s->environ, key);
3619 if (envent == NULL)
3620 envent = environ_find(global_environ, key);
3621 if (envent != NULL && envent->value != NULL) {
3622 found = xstrdup(envent->value);
3623 goto found;
3627 return (NULL);
3629 found:
3630 if (modifiers & FORMAT_TIMESTRING) {
3631 if (t == 0 && found != NULL) {
3632 t = strtonum(found, 0, INT64_MAX, &errstr);
3633 if (errstr != NULL)
3634 t = 0;
3635 free(found);
3637 if (t == 0)
3638 return (NULL);
3639 if (modifiers & FORMAT_PRETTY)
3640 found = format_pretty_time(t, 0);
3641 else {
3642 if (time_format != NULL) {
3643 localtime_r(&t, &tm);
3644 strftime(s, sizeof s, time_format, &tm);
3645 } else {
3646 ctime_r(&t, s);
3647 s[strcspn(s, "\n")] = '\0';
3649 found = xstrdup(s);
3651 return (found);
3654 if (t != 0)
3655 xasprintf(&found, "%lld", (long long)t);
3656 else if (found == NULL)
3657 return (NULL);
3658 if (modifiers & FORMAT_BASENAME) {
3659 saved = found;
3660 found = xstrdup(basename(saved));
3661 free(saved);
3663 if (modifiers & FORMAT_DIRNAME) {
3664 saved = found;
3665 found = xstrdup(dirname(saved));
3666 free(saved);
3668 if (modifiers & FORMAT_QUOTE_SHELL) {
3669 saved = found;
3670 found = format_quote_shell(saved);
3671 free(saved);
3673 if (modifiers & FORMAT_QUOTE_STYLE) {
3674 saved = found;
3675 found = format_quote_style(saved);
3676 free(saved);
3678 return (found);
3681 /* Unescape escaped characters. */
3682 static char *
3683 format_unescape(const char *s)
3685 char *out, *cp;
3686 int brackets = 0;
3688 cp = out = xmalloc(strlen(s) + 1);
3689 for (; *s != '\0'; s++) {
3690 if (*s == '#' && s[1] == '{')
3691 brackets++;
3692 if (brackets == 0 &&
3693 *s == '#' &&
3694 strchr(",#{}:", s[1]) != NULL) {
3695 *cp++ = *++s;
3696 continue;
3698 if (*s == '}')
3699 brackets--;
3700 *cp++ = *s;
3702 *cp = '\0';
3703 return (out);
3706 /* Remove escaped characters. */
3707 static char *
3708 format_strip(const char *s)
3710 char *out, *cp;
3711 int brackets = 0;
3713 cp = out = xmalloc(strlen(s) + 1);
3714 for (; *s != '\0'; s++) {
3715 if (*s == '#' && s[1] == '{')
3716 brackets++;
3717 if (*s == '#' && strchr(",#{}:", s[1]) != NULL) {
3718 if (brackets != 0)
3719 *cp++ = *s;
3720 continue;
3722 if (*s == '}')
3723 brackets--;
3724 *cp++ = *s;
3726 *cp = '\0';
3727 return (out);
3730 /* Skip until end. */
3731 const char *
3732 format_skip(const char *s, const char *end)
3734 int brackets = 0;
3736 for (; *s != '\0'; s++) {
3737 if (*s == '#' && s[1] == '{')
3738 brackets++;
3739 if (*s == '#' &&
3740 s[1] != '\0' &&
3741 strchr(",#{}:", s[1]) != NULL) {
3742 s++;
3743 continue;
3745 if (*s == '}')
3746 brackets--;
3747 if (strchr(end, *s) != NULL && brackets == 0)
3748 break;
3750 if (*s == '\0')
3751 return (NULL);
3752 return (s);
3755 /* Return left and right alternatives separated by commas. */
3756 static int
3757 format_choose(struct format_expand_state *es, const char *s, char **left,
3758 char **right, int expand)
3760 const char *cp;
3761 char *left0, *right0;
3763 cp = format_skip(s, ",");
3764 if (cp == NULL)
3765 return (-1);
3766 left0 = xstrndup(s, cp - s);
3767 right0 = xstrdup(cp + 1);
3769 if (expand) {
3770 *left = format_expand1(es, left0);
3771 free(left0);
3772 *right = format_expand1(es, right0);
3773 free(right0);
3774 } else {
3775 *left = left0;
3776 *right = right0;
3778 return (0);
3781 /* Is this true? */
3783 format_true(const char *s)
3785 if (s != NULL && *s != '\0' && (s[0] != '0' || s[1] != '\0'))
3786 return (1);
3787 return (0);
3790 /* Check if modifier end. */
3791 static int
3792 format_is_end(char c)
3794 return (c == ';' || c == ':');
3797 /* Add to modifier list. */
3798 static void
3799 format_add_modifier(struct format_modifier **list, u_int *count,
3800 const char *c, size_t n, char **argv, int argc)
3802 struct format_modifier *fm;
3804 *list = xreallocarray(*list, (*count) + 1, sizeof **list);
3805 fm = &(*list)[(*count)++];
3807 memcpy(fm->modifier, c, n);
3808 fm->modifier[n] = '\0';
3809 fm->size = n;
3811 fm->argv = argv;
3812 fm->argc = argc;
3815 /* Free modifier list. */
3816 static void
3817 format_free_modifiers(struct format_modifier *list, u_int count)
3819 u_int i;
3821 for (i = 0; i < count; i++)
3822 cmd_free_argv(list[i].argc, list[i].argv);
3823 free(list);
3826 /* Build modifier list. */
3827 static struct format_modifier *
3828 format_build_modifiers(struct format_expand_state *es, const char **s,
3829 u_int *count)
3831 const char *cp = *s, *end;
3832 struct format_modifier *list = NULL;
3833 char c, last[] = "X;:", **argv, *value;
3834 int argc;
3837 * Modifiers are a ; separated list of the forms:
3838 * l,m,C,a,b,c,d,n,t,w,q,E,T,S,W,P,<,>
3839 * =a
3840 * =/a
3841 * =/a/
3842 * s/a/b/
3843 * s/a/b
3844 * ||,&&,!=,==,<=,>=
3847 *count = 0;
3849 while (*cp != '\0' && *cp != ':') {
3850 /* Skip any separator character. */
3851 if (*cp == ';')
3852 cp++;
3854 /* Check single character modifiers with no arguments. */
3855 if (strchr("labcdnwETSWPL<>", cp[0]) != NULL &&
3856 format_is_end(cp[1])) {
3857 format_add_modifier(&list, count, cp, 1, NULL, 0);
3858 cp++;
3859 continue;
3862 /* Then try double character with no arguments. */
3863 if ((memcmp("||", cp, 2) == 0 ||
3864 memcmp("&&", cp, 2) == 0 ||
3865 memcmp("!=", cp, 2) == 0 ||
3866 memcmp("==", cp, 2) == 0 ||
3867 memcmp("<=", cp, 2) == 0 ||
3868 memcmp(">=", cp, 2) == 0) &&
3869 format_is_end(cp[2])) {
3870 format_add_modifier(&list, count, cp, 2, NULL, 0);
3871 cp += 2;
3872 continue;
3875 /* Now try single character with arguments. */
3876 if (strchr("mCNst=peq", cp[0]) == NULL)
3877 break;
3878 c = cp[0];
3880 /* No arguments provided. */
3881 if (format_is_end(cp[1])) {
3882 format_add_modifier(&list, count, cp, 1, NULL, 0);
3883 cp++;
3884 continue;
3886 argv = NULL;
3887 argc = 0;
3889 /* Single argument with no wrapper character. */
3890 if (!ispunct((u_char)cp[1]) || cp[1] == '-') {
3891 end = format_skip(cp + 1, ":;");
3892 if (end == NULL)
3893 break;
3895 argv = xcalloc(1, sizeof *argv);
3896 value = xstrndup(cp + 1, end - (cp + 1));
3897 argv[0] = format_expand1(es, value);
3898 free(value);
3899 argc = 1;
3901 format_add_modifier(&list, count, &c, 1, argv, argc);
3902 cp = end;
3903 continue;
3906 /* Multiple arguments with a wrapper character. */
3907 last[0] = cp[1];
3908 cp++;
3909 do {
3910 if (cp[0] == last[0] && format_is_end(cp[1])) {
3911 cp++;
3912 break;
3914 end = format_skip(cp + 1, last);
3915 if (end == NULL)
3916 break;
3917 cp++;
3919 argv = xreallocarray(argv, argc + 1, sizeof *argv);
3920 value = xstrndup(cp, end - cp);
3921 argv[argc++] = format_expand1(es, value);
3922 free(value);
3924 cp = end;
3925 } while (!format_is_end(cp[0]));
3926 format_add_modifier(&list, count, &c, 1, argv, argc);
3928 if (*cp != ':') {
3929 format_free_modifiers(list, *count);
3930 *count = 0;
3931 return (NULL);
3933 *s = cp + 1;
3934 return (list);
3937 /* Match against an fnmatch(3) pattern or regular expression. */
3938 static char *
3939 format_match(struct format_modifier *fm, const char *pattern, const char *text)
3941 const char *s = "";
3942 regex_t r;
3943 int flags = 0;
3945 if (fm->argc >= 1)
3946 s = fm->argv[0];
3947 if (strchr(s, 'r') == NULL) {
3948 if (strchr(s, 'i') != NULL)
3949 flags |= FNM_CASEFOLD;
3950 if (fnmatch(pattern, text, flags) != 0)
3951 return (xstrdup("0"));
3952 } else {
3953 flags = REG_EXTENDED|REG_NOSUB;
3954 if (strchr(s, 'i') != NULL)
3955 flags |= REG_ICASE;
3956 if (regcomp(&r, pattern, flags) != 0)
3957 return (xstrdup("0"));
3958 if (regexec(&r, text, 0, NULL, 0) != 0) {
3959 regfree(&r);
3960 return (xstrdup("0"));
3962 regfree(&r);
3964 return (xstrdup("1"));
3967 /* Perform substitution in string. */
3968 static char *
3969 format_sub(struct format_modifier *fm, const char *text, const char *pattern,
3970 const char *with)
3972 char *value;
3973 int flags = REG_EXTENDED;
3975 if (fm->argc >= 3 && strchr(fm->argv[2], 'i') != NULL)
3976 flags |= REG_ICASE;
3977 value = regsub(pattern, with, text, flags);
3978 if (value == NULL)
3979 return (xstrdup(text));
3980 return (value);
3983 /* Search inside pane. */
3984 static char *
3985 format_search(struct format_modifier *fm, struct window_pane *wp, const char *s)
3987 int ignore = 0, regex = 0;
3988 char *value;
3990 if (fm->argc >= 1) {
3991 if (strchr(fm->argv[0], 'i') != NULL)
3992 ignore = 1;
3993 if (strchr(fm->argv[0], 'r') != NULL)
3994 regex = 1;
3996 xasprintf(&value, "%u", window_pane_search(wp, s, regex, ignore));
3997 return (value);
4000 /* Does session name exist? */
4001 static char *
4002 format_session_name(struct format_expand_state *es, const char *fmt)
4004 char *name;
4005 struct session *s;
4007 name = format_expand1(es, fmt);
4008 RB_FOREACH(s, sessions, &sessions) {
4009 if (strcmp(s->name, name) == 0) {
4010 free(name);
4011 return (xstrdup("1"));
4014 free(name);
4015 return (xstrdup("0"));
4018 /* Loop over sessions. */
4019 static char *
4020 format_loop_sessions(struct format_expand_state *es, const char *fmt)
4022 struct format_tree *ft = es->ft;
4023 struct client *c = ft->client;
4024 struct cmdq_item *item = ft->item;
4025 struct format_tree *nft;
4026 struct format_expand_state next;
4027 char *expanded, *value;
4028 size_t valuelen;
4029 struct session *s;
4031 value = xcalloc(1, 1);
4032 valuelen = 1;
4034 RB_FOREACH(s, sessions, &sessions) {
4035 format_log(es, "session loop: $%u", s->id);
4036 nft = format_create(c, item, FORMAT_NONE, ft->flags);
4037 format_defaults(nft, ft->c, s, NULL, NULL);
4038 format_copy_state(&next, es, 0);
4039 next.ft = nft;
4040 expanded = format_expand1(&next, fmt);
4041 format_free(next.ft);
4043 valuelen += strlen(expanded);
4044 value = xrealloc(value, valuelen);
4046 strlcat(value, expanded, valuelen);
4047 free(expanded);
4050 return (value);
4053 /* Does window name exist? */
4054 static char *
4055 format_window_name(struct format_expand_state *es, const char *fmt)
4057 struct format_tree *ft = es->ft;
4058 char *name;
4059 struct winlink *wl;
4061 if (ft->s == NULL) {
4062 format_log(es, "window name but no session");
4063 return (NULL);
4066 name = format_expand1(es, fmt);
4067 RB_FOREACH(wl, winlinks, &ft->s->windows) {
4068 if (strcmp(wl->window->name, name) == 0) {
4069 free(name);
4070 return (xstrdup("1"));
4073 free(name);
4074 return (xstrdup("0"));
4077 /* Loop over windows. */
4078 static char *
4079 format_loop_windows(struct format_expand_state *es, const char *fmt)
4081 struct format_tree *ft = es->ft;
4082 struct client *c = ft->client;
4083 struct cmdq_item *item = ft->item;
4084 struct format_tree *nft;
4085 struct format_expand_state next;
4086 char *all, *active, *use, *expanded, *value;
4087 size_t valuelen;
4088 struct winlink *wl;
4089 struct window *w;
4091 if (ft->s == NULL) {
4092 format_log(es, "window loop but no session");
4093 return (NULL);
4096 if (format_choose(es, fmt, &all, &active, 0) != 0) {
4097 all = xstrdup(fmt);
4098 active = NULL;
4101 value = xcalloc(1, 1);
4102 valuelen = 1;
4104 RB_FOREACH(wl, winlinks, &ft->s->windows) {
4105 w = wl->window;
4106 format_log(es, "window loop: %u @%u", wl->idx, w->id);
4107 if (active != NULL && wl == ft->s->curw)
4108 use = active;
4109 else
4110 use = all;
4111 nft = format_create(c, item, FORMAT_WINDOW|w->id, ft->flags);
4112 format_defaults(nft, ft->c, ft->s, wl, NULL);
4113 format_copy_state(&next, es, 0);
4114 next.ft = nft;
4115 expanded = format_expand1(&next, use);
4116 format_free(nft);
4118 valuelen += strlen(expanded);
4119 value = xrealloc(value, valuelen);
4121 strlcat(value, expanded, valuelen);
4122 free(expanded);
4125 free(active);
4126 free(all);
4128 return (value);
4131 /* Loop over panes. */
4132 static char *
4133 format_loop_panes(struct format_expand_state *es, const char *fmt)
4135 struct format_tree *ft = es->ft;
4136 struct client *c = ft->client;
4137 struct cmdq_item *item = ft->item;
4138 struct format_tree *nft;
4139 struct format_expand_state next;
4140 char *all, *active, *use, *expanded, *value;
4141 size_t valuelen;
4142 struct window_pane *wp;
4144 if (ft->w == NULL) {
4145 format_log(es, "pane loop but no window");
4146 return (NULL);
4149 if (format_choose(es, fmt, &all, &active, 0) != 0) {
4150 all = xstrdup(fmt);
4151 active = NULL;
4154 value = xcalloc(1, 1);
4155 valuelen = 1;
4157 TAILQ_FOREACH(wp, &ft->w->panes, entry) {
4158 format_log(es, "pane loop: %%%u", wp->id);
4159 if (active != NULL && wp == ft->w->active)
4160 use = active;
4161 else
4162 use = all;
4163 nft = format_create(c, item, FORMAT_PANE|wp->id, ft->flags);
4164 format_defaults(nft, ft->c, ft->s, ft->wl, wp);
4165 format_copy_state(&next, es, 0);
4166 next.ft = nft;
4167 expanded = format_expand1(&next, use);
4168 format_free(nft);
4170 valuelen += strlen(expanded);
4171 value = xrealloc(value, valuelen);
4173 strlcat(value, expanded, valuelen);
4174 free(expanded);
4177 free(active);
4178 free(all);
4180 return (value);
4183 /* Loop over clients. */
4184 static char *
4185 format_loop_clients(struct format_expand_state *es, const char *fmt)
4187 struct format_tree *ft = es->ft;
4188 struct client *c = ft->client;
4189 struct cmdq_item *item = ft->item;
4190 struct format_tree *nft;
4191 struct format_expand_state next;
4192 char *expanded, *value;
4193 size_t valuelen;
4195 value = xcalloc(1, 1);
4196 valuelen = 1;
4198 TAILQ_FOREACH(c, &clients, entry) {
4199 format_log(es, "client loop: %s", c->name);
4200 nft = format_create(c, item, 0, ft->flags);
4201 format_defaults(nft, c, ft->s, ft->wl, ft->wp);
4202 format_copy_state(&next, es, 0);
4203 next.ft = nft;
4204 expanded = format_expand1(&next, fmt);
4205 format_free(nft);
4207 valuelen += strlen(expanded);
4208 value = xrealloc(value, valuelen);
4210 strlcat(value, expanded, valuelen);
4211 free(expanded);
4214 return (value);
4217 static char *
4218 format_replace_expression(struct format_modifier *mexp,
4219 struct format_expand_state *es, const char *copy)
4221 int argc = mexp->argc;
4222 const char *errstr;
4223 char *endch, *value, *left = NULL, *right = NULL;
4224 int use_fp = 0;
4225 u_int prec = 0;
4226 double mleft, mright, result;
4227 enum { ADD,
4228 SUBTRACT,
4229 MULTIPLY,
4230 DIVIDE,
4231 MODULUS,
4232 EQUAL,
4233 NOT_EQUAL,
4234 GREATER_THAN,
4235 GREATER_THAN_EQUAL,
4236 LESS_THAN,
4237 LESS_THAN_EQUAL } operator;
4239 if (strcmp(mexp->argv[0], "+") == 0)
4240 operator = ADD;
4241 else if (strcmp(mexp->argv[0], "-") == 0)
4242 operator = SUBTRACT;
4243 else if (strcmp(mexp->argv[0], "*") == 0)
4244 operator = MULTIPLY;
4245 else if (strcmp(mexp->argv[0], "/") == 0)
4246 operator = DIVIDE;
4247 else if (strcmp(mexp->argv[0], "%") == 0 ||
4248 strcmp(mexp->argv[0], "m") == 0)
4249 operator = MODULUS;
4250 else if (strcmp(mexp->argv[0], "==") == 0)
4251 operator = EQUAL;
4252 else if (strcmp(mexp->argv[0], "!=") == 0)
4253 operator = NOT_EQUAL;
4254 else if (strcmp(mexp->argv[0], ">") == 0)
4255 operator = GREATER_THAN;
4256 else if (strcmp(mexp->argv[0], "<") == 0)
4257 operator = LESS_THAN;
4258 else if (strcmp(mexp->argv[0], ">=") == 0)
4259 operator = GREATER_THAN_EQUAL;
4260 else if (strcmp(mexp->argv[0], "<=") == 0)
4261 operator = LESS_THAN_EQUAL;
4262 else {
4263 format_log(es, "expression has no valid operator: '%s'",
4264 mexp->argv[0]);
4265 goto fail;
4268 /* The second argument may be flags. */
4269 if (argc >= 2 && strchr(mexp->argv[1], 'f') != NULL) {
4270 use_fp = 1;
4271 prec = 2;
4274 /* The third argument may be precision. */
4275 if (argc >= 3) {
4276 prec = strtonum(mexp->argv[2], INT_MIN, INT_MAX, &errstr);
4277 if (errstr != NULL) {
4278 format_log(es, "expression precision %s: %s", errstr,
4279 mexp->argv[2]);
4280 goto fail;
4284 if (format_choose(es, copy, &left, &right, 1) != 0) {
4285 format_log(es, "expression syntax error");
4286 goto fail;
4289 mleft = strtod(left, &endch);
4290 if (*endch != '\0') {
4291 format_log(es, "expression left side is invalid: %s", left);
4292 goto fail;
4295 mright = strtod(right, &endch);
4296 if (*endch != '\0') {
4297 format_log(es, "expression right side is invalid: %s", right);
4298 goto fail;
4301 if (!use_fp) {
4302 mleft = (long long)mleft;
4303 mright = (long long)mright;
4305 format_log(es, "expression left side is: %.*f", prec, mleft);
4306 format_log(es, "expression right side is: %.*f", prec, mright);
4308 switch (operator) {
4309 case ADD:
4310 result = mleft + mright;
4311 break;
4312 case SUBTRACT:
4313 result = mleft - mright;
4314 break;
4315 case MULTIPLY:
4316 result = mleft * mright;
4317 break;
4318 case DIVIDE:
4319 result = mleft / mright;
4320 break;
4321 case MODULUS:
4322 result = fmod(mleft, mright);
4323 break;
4324 case EQUAL:
4325 result = fabs(mleft - mright) < 1e-9;
4326 break;
4327 case NOT_EQUAL:
4328 result = fabs(mleft - mright) > 1e-9;
4329 break;
4330 case GREATER_THAN:
4331 result = (mleft > mright);
4332 break;
4333 case GREATER_THAN_EQUAL:
4334 result = (mleft >= mright);
4335 break;
4336 case LESS_THAN:
4337 result = (mleft < mright);
4338 break;
4339 case LESS_THAN_EQUAL:
4340 result = (mleft <= mright);
4341 break;
4343 if (use_fp)
4344 xasprintf(&value, "%.*f", prec, result);
4345 else
4346 xasprintf(&value, "%.*f", prec, (double)(long long)result);
4347 format_log(es, "expression result is %s", value);
4349 free(right);
4350 free(left);
4351 return (value);
4353 fail:
4354 free(right);
4355 free(left);
4356 return (NULL);
4359 /* Replace a key. */
4360 static int
4361 format_replace(struct format_expand_state *es, const char *key, size_t keylen,
4362 char **buf, size_t *len, size_t *off)
4364 struct format_tree *ft = es->ft;
4365 struct window_pane *wp = ft->wp;
4366 const char *errstr, *copy, *cp, *marker = NULL;
4367 const char *time_format = NULL;
4368 char *copy0, *condition, *found, *new;
4369 char *value, *left, *right;
4370 size_t valuelen;
4371 int modifiers = 0, limit = 0, width = 0;
4372 int j, c;
4373 struct format_modifier *list, *cmp = NULL, *search = NULL;
4374 struct format_modifier **sub = NULL, *mexp = NULL, *fm;
4375 u_int i, count, nsub = 0;
4376 struct format_expand_state next;
4378 /* Make a copy of the key. */
4379 copy = copy0 = xstrndup(key, keylen);
4381 /* Process modifier list. */
4382 list = format_build_modifiers(es, &copy, &count);
4383 for (i = 0; i < count; i++) {
4384 fm = &list[i];
4385 if (format_logging(ft)) {
4386 format_log(es, "modifier %u is %s", i, fm->modifier);
4387 for (j = 0; j < fm->argc; j++) {
4388 format_log(es, "modifier %u argument %d: %s", i,
4389 j, fm->argv[j]);
4392 if (fm->size == 1) {
4393 switch (fm->modifier[0]) {
4394 case 'm':
4395 case '<':
4396 case '>':
4397 cmp = fm;
4398 break;
4399 case 'C':
4400 search = fm;
4401 break;
4402 case 's':
4403 if (fm->argc < 2)
4404 break;
4405 sub = xreallocarray(sub, nsub + 1, sizeof *sub);
4406 sub[nsub++] = fm;
4407 break;
4408 case '=':
4409 if (fm->argc < 1)
4410 break;
4411 limit = strtonum(fm->argv[0], INT_MIN, INT_MAX,
4412 &errstr);
4413 if (errstr != NULL)
4414 limit = 0;
4415 if (fm->argc >= 2 && fm->argv[1] != NULL)
4416 marker = fm->argv[1];
4417 break;
4418 case 'p':
4419 if (fm->argc < 1)
4420 break;
4421 width = strtonum(fm->argv[0], INT_MIN, INT_MAX,
4422 &errstr);
4423 if (errstr != NULL)
4424 width = 0;
4425 break;
4426 case 'w':
4427 modifiers |= FORMAT_WIDTH;
4428 break;
4429 case 'e':
4430 if (fm->argc < 1 || fm->argc > 3)
4431 break;
4432 mexp = fm;
4433 break;
4434 case 'l':
4435 modifiers |= FORMAT_LITERAL;
4436 break;
4437 case 'a':
4438 modifiers |= FORMAT_CHARACTER;
4439 break;
4440 case 'b':
4441 modifiers |= FORMAT_BASENAME;
4442 break;
4443 case 'c':
4444 modifiers |= FORMAT_COLOUR;
4445 break;
4446 case 'd':
4447 modifiers |= FORMAT_DIRNAME;
4448 break;
4449 case 'n':
4450 modifiers |= FORMAT_LENGTH;
4451 break;
4452 case 't':
4453 modifiers |= FORMAT_TIMESTRING;
4454 if (fm->argc < 1)
4455 break;
4456 if (strchr(fm->argv[0], 'p') != NULL)
4457 modifiers |= FORMAT_PRETTY;
4458 else if (fm->argc >= 2 &&
4459 strchr(fm->argv[0], 'f') != NULL)
4460 time_format = format_strip(fm->argv[1]);
4461 break;
4462 case 'q':
4463 if (fm->argc < 1)
4464 modifiers |= FORMAT_QUOTE_SHELL;
4465 else if (strchr(fm->argv[0], 'e') != NULL ||
4466 strchr(fm->argv[0], 'h') != NULL)
4467 modifiers |= FORMAT_QUOTE_STYLE;
4468 break;
4469 case 'E':
4470 modifiers |= FORMAT_EXPAND;
4471 break;
4472 case 'T':
4473 modifiers |= FORMAT_EXPANDTIME;
4474 break;
4475 case 'N':
4476 if (fm->argc < 1 ||
4477 strchr(fm->argv[0], 'w') != NULL)
4478 modifiers |= FORMAT_WINDOW_NAME;
4479 else if (strchr(fm->argv[0], 's') != NULL)
4480 modifiers |= FORMAT_SESSION_NAME;
4481 break;
4482 case 'S':
4483 modifiers |= FORMAT_SESSIONS;
4484 break;
4485 case 'W':
4486 modifiers |= FORMAT_WINDOWS;
4487 break;
4488 case 'P':
4489 modifiers |= FORMAT_PANES;
4490 break;
4491 case 'L':
4492 modifiers |= FORMAT_CLIENTS;
4493 break;
4495 } else if (fm->size == 2) {
4496 if (strcmp(fm->modifier, "||") == 0 ||
4497 strcmp(fm->modifier, "&&") == 0 ||
4498 strcmp(fm->modifier, "==") == 0 ||
4499 strcmp(fm->modifier, "!=") == 0 ||
4500 strcmp(fm->modifier, ">=") == 0 ||
4501 strcmp(fm->modifier, "<=") == 0)
4502 cmp = fm;
4506 /* Is this a literal string? */
4507 if (modifiers & FORMAT_LITERAL) {
4508 format_log(es, "literal string is '%s'", copy);
4509 value = format_unescape(copy);
4510 goto done;
4513 /* Is this a character? */
4514 if (modifiers & FORMAT_CHARACTER) {
4515 new = format_expand1(es, copy);
4516 c = strtonum(new, 32, 126, &errstr);
4517 if (errstr != NULL)
4518 value = xstrdup("");
4519 else
4520 xasprintf(&value, "%c", c);
4521 free(new);
4522 goto done;
4525 /* Is this a colour? */
4526 if (modifiers & FORMAT_COLOUR) {
4527 new = format_expand1(es, copy);
4528 c = colour_fromstring(new);
4529 if (c == -1 || (c = colour_force_rgb(c)) == -1)
4530 value = xstrdup("");
4531 else
4532 xasprintf(&value, "%06x", c & 0xffffff);
4533 free(new);
4534 goto done;
4537 /* Is this a loop, comparison or condition? */
4538 if (modifiers & FORMAT_SESSIONS) {
4539 value = format_loop_sessions(es, copy);
4540 if (value == NULL)
4541 goto fail;
4542 } else if (modifiers & FORMAT_WINDOWS) {
4543 value = format_loop_windows(es, copy);
4544 if (value == NULL)
4545 goto fail;
4546 } else if (modifiers & FORMAT_PANES) {
4547 value = format_loop_panes(es, copy);
4548 if (value == NULL)
4549 goto fail;
4550 } else if (modifiers & FORMAT_CLIENTS) {
4551 value = format_loop_clients(es, copy);
4552 if (value == NULL)
4553 goto fail;
4554 } else if (modifiers & FORMAT_WINDOW_NAME) {
4555 value = format_window_name(es, copy);
4556 if (value == NULL)
4557 goto fail;
4558 } else if (modifiers & FORMAT_SESSION_NAME) {
4559 value = format_session_name(es, copy);
4560 if (value == NULL)
4561 goto fail;
4562 } else if (search != NULL) {
4563 /* Search in pane. */
4564 new = format_expand1(es, copy);
4565 if (wp == NULL) {
4566 format_log(es, "search '%s' but no pane", new);
4567 value = xstrdup("0");
4568 } else {
4569 format_log(es, "search '%s' pane %%%u", new, wp->id);
4570 value = format_search(search, wp, new);
4572 free(new);
4573 } else if (cmp != NULL) {
4574 /* Comparison of left and right. */
4575 if (format_choose(es, copy, &left, &right, 1) != 0) {
4576 format_log(es, "compare %s syntax error: %s",
4577 cmp->modifier, copy);
4578 goto fail;
4580 format_log(es, "compare %s left is: %s", cmp->modifier, left);
4581 format_log(es, "compare %s right is: %s", cmp->modifier, right);
4583 if (strcmp(cmp->modifier, "||") == 0) {
4584 if (format_true(left) || format_true(right))
4585 value = xstrdup("1");
4586 else
4587 value = xstrdup("0");
4588 } else if (strcmp(cmp->modifier, "&&") == 0) {
4589 if (format_true(left) && format_true(right))
4590 value = xstrdup("1");
4591 else
4592 value = xstrdup("0");
4593 } else if (strcmp(cmp->modifier, "==") == 0) {
4594 if (strcmp(left, right) == 0)
4595 value = xstrdup("1");
4596 else
4597 value = xstrdup("0");
4598 } else if (strcmp(cmp->modifier, "!=") == 0) {
4599 if (strcmp(left, right) != 0)
4600 value = xstrdup("1");
4601 else
4602 value = xstrdup("0");
4603 } else if (strcmp(cmp->modifier, "<") == 0) {
4604 if (strcmp(left, right) < 0)
4605 value = xstrdup("1");
4606 else
4607 value = xstrdup("0");
4608 } else if (strcmp(cmp->modifier, ">") == 0) {
4609 if (strcmp(left, right) > 0)
4610 value = xstrdup("1");
4611 else
4612 value = xstrdup("0");
4613 } else if (strcmp(cmp->modifier, "<=") == 0) {
4614 if (strcmp(left, right) <= 0)
4615 value = xstrdup("1");
4616 else
4617 value = xstrdup("0");
4618 } else if (strcmp(cmp->modifier, ">=") == 0) {
4619 if (strcmp(left, right) >= 0)
4620 value = xstrdup("1");
4621 else
4622 value = xstrdup("0");
4623 } else if (strcmp(cmp->modifier, "m") == 0)
4624 value = format_match(cmp, left, right);
4626 free(right);
4627 free(left);
4628 } else if (*copy == '?') {
4629 /* Conditional: check first and choose second or third. */
4630 cp = format_skip(copy + 1, ",");
4631 if (cp == NULL) {
4632 format_log(es, "condition syntax error: %s", copy + 1);
4633 goto fail;
4635 condition = xstrndup(copy + 1, cp - (copy + 1));
4636 format_log(es, "condition is: %s", condition);
4638 found = format_find(ft, condition, modifiers, time_format);
4639 if (found == NULL) {
4641 * If the condition not found, try to expand it. If
4642 * the expansion doesn't have any effect, then assume
4643 * false.
4645 found = format_expand1(es, condition);
4646 if (strcmp(found, condition) == 0) {
4647 free(found);
4648 found = xstrdup("");
4649 format_log(es,
4650 "condition '%s' not found; assuming false",
4651 condition);
4653 } else {
4654 format_log(es, "condition '%s' found: %s", condition,
4655 found);
4658 if (format_choose(es, cp + 1, &left, &right, 0) != 0) {
4659 format_log(es, "condition '%s' syntax error: %s",
4660 condition, cp + 1);
4661 free(found);
4662 goto fail;
4664 if (format_true(found)) {
4665 format_log(es, "condition '%s' is true", condition);
4666 value = format_expand1(es, left);
4667 } else {
4668 format_log(es, "condition '%s' is false", condition);
4669 value = format_expand1(es, right);
4671 free(right);
4672 free(left);
4674 free(condition);
4675 free(found);
4676 } else if (mexp != NULL) {
4677 value = format_replace_expression(mexp, es, copy);
4678 if (value == NULL)
4679 value = xstrdup("");
4680 } else {
4681 if (strstr(copy, "#{") != 0) {
4682 format_log(es, "expanding inner format '%s'", copy);
4683 value = format_expand1(es, copy);
4684 } else {
4685 value = format_find(ft, copy, modifiers, time_format);
4686 if (value == NULL) {
4687 format_log(es, "format '%s' not found", copy);
4688 value = xstrdup("");
4689 } else {
4690 format_log(es, "format '%s' found: %s", copy,
4691 value);
4696 done:
4697 /* Expand again if required. */
4698 if (modifiers & FORMAT_EXPAND) {
4699 new = format_expand1(es, value);
4700 free(value);
4701 value = new;
4702 } else if (modifiers & FORMAT_EXPANDTIME) {
4703 format_copy_state(&next, es, FORMAT_EXPAND_TIME);
4704 new = format_expand1(&next, value);
4705 free(value);
4706 value = new;
4709 /* Perform substitution if any. */
4710 for (i = 0; i < nsub; i++) {
4711 left = format_expand1(es, sub[i]->argv[0]);
4712 right = format_expand1(es, sub[i]->argv[1]);
4713 new = format_sub(sub[i], value, left, right);
4714 format_log(es, "substitute '%s' to '%s': %s", left, right, new);
4715 free(value);
4716 value = new;
4717 free(right);
4718 free(left);
4721 /* Truncate the value if needed. */
4722 if (limit > 0) {
4723 new = format_trim_left(value, limit);
4724 if (marker != NULL && strcmp(new, value) != 0) {
4725 free(value);
4726 xasprintf(&value, "%s%s", new, marker);
4727 } else {
4728 free(value);
4729 value = new;
4731 format_log(es, "applied length limit %d: %s", limit, value);
4732 } else if (limit < 0) {
4733 new = format_trim_right(value, -limit);
4734 if (marker != NULL && strcmp(new, value) != 0) {
4735 free(value);
4736 xasprintf(&value, "%s%s", marker, new);
4737 } else {
4738 free(value);
4739 value = new;
4741 format_log(es, "applied length limit %d: %s", limit, value);
4744 /* Pad the value if needed. */
4745 if (width > 0) {
4746 new = utf8_padcstr(value, width);
4747 free(value);
4748 value = new;
4749 format_log(es, "applied padding width %d: %s", width, value);
4750 } else if (width < 0) {
4751 new = utf8_rpadcstr(value, -width);
4752 free(value);
4753 value = new;
4754 format_log(es, "applied padding width %d: %s", width, value);
4757 /* Replace with the length or width if needed. */
4758 if (modifiers & FORMAT_LENGTH) {
4759 xasprintf(&new, "%zu", strlen(value));
4760 free(value);
4761 value = new;
4762 format_log(es, "replacing with length: %s", new);
4764 if (modifiers & FORMAT_WIDTH) {
4765 xasprintf(&new, "%u", format_width(value));
4766 free(value);
4767 value = new;
4768 format_log(es, "replacing with width: %s", new);
4771 /* Expand the buffer and copy in the value. */
4772 valuelen = strlen(value);
4773 while (*len - *off < valuelen + 1) {
4774 *buf = xreallocarray(*buf, 2, *len);
4775 *len *= 2;
4777 memcpy(*buf + *off, value, valuelen);
4778 *off += valuelen;
4780 format_log(es, "replaced '%s' with '%s'", copy0, value);
4781 free(value);
4783 free(sub);
4784 format_free_modifiers(list, count);
4785 free(copy0);
4786 return (0);
4788 fail:
4789 format_log(es, "failed %s", copy0);
4791 free(sub);
4792 format_free_modifiers(list, count);
4793 free(copy0);
4794 return (-1);
4797 /* Expand keys in a template. */
4798 static char *
4799 format_expand1(struct format_expand_state *es, const char *fmt)
4801 struct format_tree *ft = es->ft;
4802 char *buf, *out, *name;
4803 const char *ptr, *s, *style_end = NULL;
4804 size_t off, len, n, outlen;
4805 int ch, brackets;
4806 char expanded[8192];
4808 if (fmt == NULL || *fmt == '\0')
4809 return (xstrdup(""));
4811 if (es->loop == FORMAT_LOOP_LIMIT) {
4812 format_log(es, "reached loop limit (%u)", FORMAT_LOOP_LIMIT);
4813 return (xstrdup(""));
4815 es->loop++;
4817 format_log(es, "expanding format: %s", fmt);
4819 if ((es->flags & FORMAT_EXPAND_TIME) && strchr(fmt, '%') != NULL) {
4820 if (es->time == 0) {
4821 es->time = time(NULL);
4822 localtime_r(&es->time, &es->tm);
4824 if (strftime(expanded, sizeof expanded, fmt, &es->tm) == 0) {
4825 format_log(es, "format is too long");
4826 return (xstrdup(""));
4828 if (format_logging(ft) && strcmp(expanded, fmt) != 0)
4829 format_log(es, "after time expanded: %s", expanded);
4830 fmt = expanded;
4833 len = 64;
4834 buf = xmalloc(len);
4835 off = 0;
4837 while (*fmt != '\0') {
4838 if (*fmt != '#') {
4839 while (len - off < 2) {
4840 buf = xreallocarray(buf, 2, len);
4841 len *= 2;
4843 buf[off++] = *fmt++;
4844 continue;
4846 fmt++;
4848 ch = (u_char)*fmt++;
4849 switch (ch) {
4850 case '(':
4851 brackets = 1;
4852 for (ptr = fmt; *ptr != '\0'; ptr++) {
4853 if (*ptr == '(')
4854 brackets++;
4855 if (*ptr == ')' && --brackets == 0)
4856 break;
4858 if (*ptr != ')' || brackets != 0)
4859 break;
4860 n = ptr - fmt;
4862 name = xstrndup(fmt, n);
4863 format_log(es, "found #(): %s", name);
4865 if ((ft->flags & FORMAT_NOJOBS) ||
4866 (es->flags & FORMAT_EXPAND_NOJOBS)) {
4867 out = xstrdup("");
4868 format_log(es, "#() is disabled");
4869 } else {
4870 out = format_job_get(es, name);
4871 format_log(es, "#() result: %s", out);
4873 free(name);
4875 outlen = strlen(out);
4876 while (len - off < outlen + 1) {
4877 buf = xreallocarray(buf, 2, len);
4878 len *= 2;
4880 memcpy(buf + off, out, outlen);
4881 off += outlen;
4883 free(out);
4885 fmt += n + 1;
4886 continue;
4887 case '{':
4888 ptr = format_skip((char *)fmt - 2, "}");
4889 if (ptr == NULL)
4890 break;
4891 n = ptr - fmt;
4893 format_log(es, "found #{}: %.*s", (int)n, fmt);
4894 if (format_replace(es, fmt, n, &buf, &len, &off) != 0)
4895 break;
4896 fmt += n + 1;
4897 continue;
4898 case '[':
4899 case '#':
4901 * If ##[ (with two or more #s), then it is a style and
4902 * can be left for format_draw to handle.
4904 ptr = fmt - (ch == '[');
4905 n = 2 - (ch == '[');
4906 while (*ptr == '#') {
4907 ptr++;
4908 n++;
4910 if (*ptr == '[') {
4911 style_end = format_skip(fmt - 2, "]");
4912 format_log(es, "found #*%zu[", n);
4913 while (len - off < n + 2) {
4914 buf = xreallocarray(buf, 2, len);
4915 len *= 2;
4917 memcpy(buf + off, fmt - 2, n + 1);
4918 off += n + 1;
4919 fmt = ptr + 1;
4920 continue;
4922 /* FALLTHROUGH */
4923 case '}':
4924 case ',':
4925 format_log(es, "found #%c", ch);
4926 while (len - off < 2) {
4927 buf = xreallocarray(buf, 2, len);
4928 len *= 2;
4930 buf[off++] = ch;
4931 continue;
4932 default:
4933 s = NULL;
4934 if (fmt > style_end) { /* skip inside #[] */
4935 if (ch >= 'A' && ch <= 'Z')
4936 s = format_upper[ch - 'A'];
4937 else if (ch >= 'a' && ch <= 'z')
4938 s = format_lower[ch - 'a'];
4940 if (s == NULL) {
4941 while (len - off < 3) {
4942 buf = xreallocarray(buf, 2, len);
4943 len *= 2;
4945 buf[off++] = '#';
4946 buf[off++] = ch;
4947 continue;
4949 n = strlen(s);
4950 format_log(es, "found #%c: %s", ch, s);
4951 if (format_replace(es, s, n, &buf, &len, &off) != 0)
4952 break;
4953 continue;
4956 break;
4958 buf[off] = '\0';
4960 format_log(es, "result is: %s", buf);
4961 es->loop--;
4963 return (buf);
4966 /* Expand keys in a template, passing through strftime first. */
4967 char *
4968 format_expand_time(struct format_tree *ft, const char *fmt)
4970 struct format_expand_state es;
4972 memset(&es, 0, sizeof es);
4973 es.ft = ft;
4974 es.flags = FORMAT_EXPAND_TIME;
4975 return (format_expand1(&es, fmt));
4978 /* Expand keys in a template. */
4979 char *
4980 format_expand(struct format_tree *ft, const char *fmt)
4982 struct format_expand_state es;
4984 memset(&es, 0, sizeof es);
4985 es.ft = ft;
4986 es.flags = 0;
4987 return (format_expand1(&es, fmt));
4990 /* Expand a single string. */
4991 char *
4992 format_single(struct cmdq_item *item, const char *fmt, struct client *c,
4993 struct session *s, struct winlink *wl, struct window_pane *wp)
4995 struct format_tree *ft;
4996 char *expanded;
4998 ft = format_create_defaults(item, c, s, wl, wp);
4999 expanded = format_expand(ft, fmt);
5000 format_free(ft);
5001 return (expanded);
5004 /* Expand a single string using state. */
5005 char *
5006 format_single_from_state(struct cmdq_item *item, const char *fmt,
5007 struct client *c, struct cmd_find_state *fs)
5009 return (format_single(item, fmt, c, fs->s, fs->wl, fs->wp));
5012 /* Expand a single string using target. */
5013 char *
5014 format_single_from_target(struct cmdq_item *item, const char *fmt)
5016 struct client *tc = cmdq_get_target_client(item);
5018 return (format_single_from_state(item, fmt, tc, cmdq_get_target(item)));
5021 /* Create and add defaults. */
5022 struct format_tree *
5023 format_create_defaults(struct cmdq_item *item, struct client *c,
5024 struct session *s, struct winlink *wl, struct window_pane *wp)
5026 struct format_tree *ft;
5028 if (item != NULL)
5029 ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0);
5030 else
5031 ft = format_create(NULL, item, FORMAT_NONE, 0);
5032 format_defaults(ft, c, s, wl, wp);
5033 return (ft);
5036 /* Create and add defaults using state. */
5037 struct format_tree *
5038 format_create_from_state(struct cmdq_item *item, struct client *c,
5039 struct cmd_find_state *fs)
5041 return (format_create_defaults(item, c, fs->s, fs->wl, fs->wp));
5044 /* Create and add defaults using target. */
5045 struct format_tree *
5046 format_create_from_target(struct cmdq_item *item)
5048 struct client *tc = cmdq_get_target_client(item);
5050 return (format_create_from_state(item, tc, cmdq_get_target(item)));
5053 /* Set defaults for any of arguments that are not NULL. */
5054 void
5055 format_defaults(struct format_tree *ft, struct client *c, struct session *s,
5056 struct winlink *wl, struct window_pane *wp)
5058 struct paste_buffer *pb;
5060 if (c != NULL && c->name != NULL)
5061 log_debug("%s: c=%s", __func__, c->name);
5062 else
5063 log_debug("%s: c=none", __func__);
5064 if (s != NULL)
5065 log_debug("%s: s=$%u", __func__, s->id);
5066 else
5067 log_debug("%s: s=none", __func__);
5068 if (wl != NULL)
5069 log_debug("%s: wl=%u", __func__, wl->idx);
5070 else
5071 log_debug("%s: wl=none", __func__);
5072 if (wp != NULL)
5073 log_debug("%s: wp=%%%u", __func__, wp->id);
5074 else
5075 log_debug("%s: wp=none", __func__);
5077 if (c != NULL && s != NULL && c->session != s)
5078 log_debug("%s: session does not match", __func__);
5080 if (wp != NULL)
5081 ft->type = FORMAT_TYPE_PANE;
5082 else if (wl != NULL)
5083 ft->type = FORMAT_TYPE_WINDOW;
5084 else if (s != NULL)
5085 ft->type = FORMAT_TYPE_SESSION;
5086 else
5087 ft->type = FORMAT_TYPE_UNKNOWN;
5089 if (s == NULL && c != NULL)
5090 s = c->session;
5091 if (wl == NULL && s != NULL)
5092 wl = s->curw;
5093 if (wp == NULL && wl != NULL)
5094 wp = wl->window->active;
5096 if (c != NULL)
5097 format_defaults_client(ft, c);
5098 if (s != NULL)
5099 format_defaults_session(ft, s);
5100 if (wl != NULL)
5101 format_defaults_winlink(ft, wl);
5102 if (wp != NULL)
5103 format_defaults_pane(ft, wp);
5105 pb = paste_get_top(NULL);
5106 if (pb != NULL)
5107 format_defaults_paste_buffer(ft, pb);
5110 /* Set default format keys for a session. */
5111 static void
5112 format_defaults_session(struct format_tree *ft, struct session *s)
5114 ft->s = s;
5117 /* Set default format keys for a client. */
5118 static void
5119 format_defaults_client(struct format_tree *ft, struct client *c)
5121 if (ft->s == NULL)
5122 ft->s = c->session;
5123 ft->c = c;
5126 /* Set default format keys for a window. */
5127 void
5128 format_defaults_window(struct format_tree *ft, struct window *w)
5130 ft->w = w;
5133 /* Set default format keys for a winlink. */
5134 static void
5135 format_defaults_winlink(struct format_tree *ft, struct winlink *wl)
5137 if (ft->w == NULL)
5138 format_defaults_window(ft, wl->window);
5139 ft->wl = wl;
5142 /* Set default format keys for a window pane. */
5143 void
5144 format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
5146 struct window_mode_entry *wme;
5148 if (ft->w == NULL)
5149 format_defaults_window(ft, wp->window);
5150 ft->wp = wp;
5152 wme = TAILQ_FIRST(&wp->modes);
5153 if (wme != NULL && wme->mode->formats != NULL)
5154 wme->mode->formats(wme, ft);
5157 /* Set default format keys for paste buffer. */
5158 void
5159 format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
5161 ft->pb = pb;
5164 /* Return word at given coordinates. Caller frees. */
5165 char *
5166 format_grid_word(struct grid *gd, u_int x, u_int y)
5168 const struct grid_line *gl;
5169 struct grid_cell gc;
5170 const char *ws;
5171 struct utf8_data *ud = NULL;
5172 u_int end;
5173 size_t size = 0;
5174 int found = 0;
5175 char *s = NULL;
5177 ws = options_get_string(global_s_options, "word-separators");
5179 for (;;) {
5180 grid_get_cell(gd, x, y, &gc);
5181 if (gc.flags & GRID_FLAG_PADDING)
5182 break;
5183 if (utf8_cstrhas(ws, &gc.data) ||
5184 (gc.data.size == 1 && *gc.data.data == ' ')) {
5185 found = 1;
5186 break;
5189 if (x == 0) {
5190 if (y == 0)
5191 break;
5192 gl = grid_peek_line(gd, y - 1);
5193 if (~gl->flags & GRID_LINE_WRAPPED)
5194 break;
5195 y--;
5196 x = grid_line_length(gd, y);
5197 if (x == 0)
5198 break;
5200 x--;
5202 for (;;) {
5203 if (found) {
5204 end = grid_line_length(gd, y);
5205 if (end == 0 || x == end - 1) {
5206 if (y == gd->hsize + gd->sy - 1)
5207 break;
5208 gl = grid_peek_line(gd, y);
5209 if (~gl->flags & GRID_LINE_WRAPPED)
5210 break;
5211 y++;
5212 x = 0;
5213 } else
5214 x++;
5216 found = 1;
5218 grid_get_cell(gd, x, y, &gc);
5219 if (gc.flags & GRID_FLAG_PADDING)
5220 break;
5221 if (utf8_cstrhas(ws, &gc.data) ||
5222 (gc.data.size == 1 && *gc.data.data == ' '))
5223 break;
5225 ud = xreallocarray(ud, size + 2, sizeof *ud);
5226 memcpy(&ud[size++], &gc.data, sizeof *ud);
5228 if (size != 0) {
5229 ud[size].size = 0;
5230 s = utf8_tocstr(ud);
5231 free(ud);
5233 return (s);
5236 /* Return line at given coordinates. Caller frees. */
5237 char *
5238 format_grid_line(struct grid *gd, u_int y)
5240 struct grid_cell gc;
5241 struct utf8_data *ud = NULL;
5242 u_int x;
5243 size_t size = 0;
5244 char *s = NULL;
5246 for (x = 0; x < grid_line_length(gd, y); x++) {
5247 grid_get_cell(gd, x, y, &gc);
5248 if (gc.flags & GRID_FLAG_PADDING)
5249 break;
5251 ud = xreallocarray(ud, size + 2, sizeof *ud);
5252 memcpy(&ud[size++], &gc.data, sizeof *ud);
5254 if (size != 0) {
5255 ud[size].size = 0;
5256 s = utf8_tocstr(ud);
5257 free(ud);
5259 return (s);
5262 /* Return hyperlink at given coordinates. Caller frees. */
5263 char *
5264 format_grid_hyperlink(struct grid *gd, u_int x, u_int y, struct screen* s)
5266 const char *uri;
5267 struct grid_cell gc;
5269 grid_get_cell(gd, x, y, &gc);
5270 if (gc.flags & GRID_FLAG_PADDING)
5271 return (NULL);
5272 if (s->hyperlinks == NULL || gc.link == 0)
5273 return (NULL);
5274 if (!hyperlinks_get(s->hyperlinks, gc.link, &uri, NULL, NULL))
5275 return (NULL);
5276 return (xstrdup(uri));