Add an L modifier like P, W, S to loop over clients. Also fix some long
[tmux.git] / format.c
blob2e1787efccade022e6e80bd761faf06d014c7b58
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 = get_proc_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 = get_proc_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 alternate_on. */
1195 static void *
1196 format_cb_alternate_on(struct format_tree *ft)
1198 if (ft->wp != NULL) {
1199 if (ft->wp->base.saved_grid != NULL)
1200 return (xstrdup("1"));
1201 return (xstrdup("0"));
1203 return (NULL);
1206 /* Callback for alternate_saved_x. */
1207 static void *
1208 format_cb_alternate_saved_x(struct format_tree *ft)
1210 if (ft->wp != NULL)
1211 return (format_printf("%u", ft->wp->base.saved_cx));
1212 return (NULL);
1215 /* Callback for alternate_saved_y. */
1216 static void *
1217 format_cb_alternate_saved_y(struct format_tree *ft)
1219 if (ft->wp != NULL)
1220 return (format_printf("%u", ft->wp->base.saved_cy));
1221 return (NULL);
1224 /* Callback for buffer_name. */
1225 static void *
1226 format_cb_buffer_name(struct format_tree *ft)
1228 if (ft->pb != NULL)
1229 return (xstrdup(paste_buffer_name(ft->pb)));
1230 return (NULL);
1233 /* Callback for buffer_sample. */
1234 static void *
1235 format_cb_buffer_sample(struct format_tree *ft)
1237 if (ft->pb != NULL)
1238 return (paste_make_sample(ft->pb));
1239 return (NULL);
1242 /* Callback for buffer_size. */
1243 static void *
1244 format_cb_buffer_size(struct format_tree *ft)
1246 size_t size;
1248 if (ft->pb != NULL) {
1249 paste_buffer_data(ft->pb, &size);
1250 return (format_printf("%zu", size));
1252 return (NULL);
1255 /* Callback for client_cell_height. */
1256 static void *
1257 format_cb_client_cell_height(struct format_tree *ft)
1259 if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED))
1260 return (format_printf("%u", ft->c->tty.ypixel));
1261 return (NULL);
1264 /* Callback for client_cell_width. */
1265 static void *
1266 format_cb_client_cell_width(struct format_tree *ft)
1268 if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED))
1269 return (format_printf("%u", ft->c->tty.xpixel));
1270 return (NULL);
1273 /* Callback for client_control_mode. */
1274 static void *
1275 format_cb_client_control_mode(struct format_tree *ft)
1277 if (ft->c != NULL) {
1278 if (ft->c->flags & CLIENT_CONTROL)
1279 return (xstrdup("1"));
1280 return (xstrdup("0"));
1282 return (NULL);
1285 /* Callback for client_discarded. */
1286 static void *
1287 format_cb_client_discarded(struct format_tree *ft)
1289 if (ft->c != NULL)
1290 return (format_printf("%zu", ft->c->discarded));
1291 return (NULL);
1294 /* Callback for client_flags. */
1295 static void *
1296 format_cb_client_flags(struct format_tree *ft)
1298 if (ft->c != NULL)
1299 return (xstrdup(server_client_get_flags(ft->c)));
1300 return (NULL);
1303 /* Callback for client_height. */
1304 static void *
1305 format_cb_client_height(struct format_tree *ft)
1307 if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED))
1308 return (format_printf("%u", ft->c->tty.sy));
1309 return (NULL);
1312 /* Callback for client_key_table. */
1313 static void *
1314 format_cb_client_key_table(struct format_tree *ft)
1316 if (ft->c != NULL)
1317 return (xstrdup(ft->c->keytable->name));
1318 return (NULL);
1321 /* Callback for client_last_session. */
1322 static void *
1323 format_cb_client_last_session(struct format_tree *ft)
1325 if (ft->c != NULL &&
1326 ft->c->last_session != NULL &&
1327 session_alive(ft->c->last_session))
1328 return (xstrdup(ft->c->last_session->name));
1329 return (NULL);
1332 /* Callback for client_name. */
1333 static void *
1334 format_cb_client_name(struct format_tree *ft)
1336 if (ft->c != NULL)
1337 return (xstrdup(ft->c->name));
1338 return (NULL);
1341 /* Callback for client_pid. */
1342 static void *
1343 format_cb_client_pid(struct format_tree *ft)
1345 if (ft->c != NULL)
1346 return (format_printf("%ld", (long)ft->c->pid));
1347 return (NULL);
1350 /* Callback for client_prefix. */
1351 static void *
1352 format_cb_client_prefix(struct format_tree *ft)
1354 const char *name;
1356 if (ft->c != NULL) {
1357 name = server_client_get_key_table(ft->c);
1358 if (strcmp(ft->c->keytable->name, name) == 0)
1359 return (xstrdup("0"));
1360 return (xstrdup("1"));
1362 return (NULL);
1365 /* Callback for client_readonly. */
1366 static void *
1367 format_cb_client_readonly(struct format_tree *ft)
1369 if (ft->c != NULL) {
1370 if (ft->c->flags & CLIENT_READONLY)
1371 return (xstrdup("1"));
1372 return (xstrdup("0"));
1374 return (NULL);
1377 /* Callback for client_session. */
1378 static void *
1379 format_cb_client_session(struct format_tree *ft)
1381 if (ft->c != NULL && ft->c->session != NULL)
1382 return (xstrdup(ft->c->session->name));
1383 return (NULL);
1386 /* Callback for client_termfeatures. */
1387 static void *
1388 format_cb_client_termfeatures(struct format_tree *ft)
1390 if (ft->c != NULL)
1391 return (xstrdup(tty_get_features(ft->c->term_features)));
1392 return (NULL);
1395 /* Callback for client_termname. */
1396 static void *
1397 format_cb_client_termname(struct format_tree *ft)
1399 if (ft->c != NULL)
1400 return (xstrdup(ft->c->term_name));
1401 return (NULL);
1404 /* Callback for client_termtype. */
1405 static void *
1406 format_cb_client_termtype(struct format_tree *ft)
1408 if (ft->c != NULL) {
1409 if (ft->c->term_type == NULL)
1410 return (xstrdup(""));
1411 return (xstrdup(ft->c->term_type));
1413 return (NULL);
1416 /* Callback for client_tty. */
1417 static void *
1418 format_cb_client_tty(struct format_tree *ft)
1420 if (ft->c != NULL)
1421 return (xstrdup(ft->c->ttyname));
1422 return (NULL);
1425 /* Callback for client_uid. */
1426 static void *
1427 format_cb_client_uid(struct format_tree *ft)
1429 uid_t uid;
1431 if (ft->c != NULL) {
1432 uid = proc_get_peer_uid(ft->c->peer);
1433 if (uid != (uid_t)-1)
1434 return (format_printf("%ld", (long)uid));
1436 return (NULL);
1439 /* Callback for client_user. */
1440 static void *
1441 format_cb_client_user(struct format_tree *ft)
1443 uid_t uid;
1444 struct passwd *pw;
1446 if (ft->c != NULL) {
1447 uid = proc_get_peer_uid(ft->c->peer);
1448 if (uid != (uid_t)-1 && (pw = getpwuid(uid)) != NULL)
1449 return (xstrdup(pw->pw_name));
1451 return (NULL);
1454 /* Callback for client_utf8. */
1455 static void *
1456 format_cb_client_utf8(struct format_tree *ft)
1458 if (ft->c != NULL) {
1459 if (ft->c->flags & CLIENT_UTF8)
1460 return (xstrdup("1"));
1461 return (xstrdup("0"));
1463 return (NULL);
1466 /* Callback for client_width. */
1467 static void *
1468 format_cb_client_width(struct format_tree *ft)
1470 if (ft->c != NULL)
1471 return (format_printf("%u", ft->c->tty.sx));
1472 return (NULL);
1475 /* Callback for client_written. */
1476 static void *
1477 format_cb_client_written(struct format_tree *ft)
1479 if (ft->c != NULL)
1480 return (format_printf("%zu", ft->c->written));
1481 return (NULL);
1484 /* Callback for config_files. */
1485 static void *
1486 format_cb_config_files(__unused struct format_tree *ft)
1488 char *s = NULL;
1489 size_t slen = 0;
1490 u_int i;
1491 size_t n;
1493 for (i = 0; i < cfg_nfiles; i++) {
1494 n = strlen(cfg_files[i]) + 1;
1495 s = xrealloc(s, slen + n + 1);
1496 slen += xsnprintf(s + slen, n + 1, "%s,", cfg_files[i]);
1498 if (s == NULL)
1499 return (xstrdup(""));
1500 s[slen - 1] = '\0';
1501 return (s);
1504 /* Callback for cursor_flag. */
1505 static void *
1506 format_cb_cursor_flag(struct format_tree *ft)
1508 if (ft->wp != NULL) {
1509 if (ft->wp->base.mode & MODE_CURSOR)
1510 return (xstrdup("1"));
1511 return (xstrdup("0"));
1513 return (NULL);
1516 /* Callback for cursor_x. */
1517 static void *
1518 format_cb_cursor_x(struct format_tree *ft)
1520 if (ft->wp != NULL)
1521 return (format_printf("%u", ft->wp->base.cx));
1522 return (NULL);
1525 /* Callback for cursor_y. */
1526 static void *
1527 format_cb_cursor_y(struct format_tree *ft)
1529 if (ft->wp != NULL)
1530 return (format_printf("%u", ft->wp->base.cy));
1531 return (NULL);
1534 /* Callback for history_limit. */
1535 static void *
1536 format_cb_history_limit(struct format_tree *ft)
1538 if (ft->wp != NULL)
1539 return (format_printf("%u", ft->wp->base.grid->hlimit));
1540 return (NULL);
1543 /* Callback for history_size. */
1544 static void *
1545 format_cb_history_size(struct format_tree *ft)
1547 if (ft->wp != NULL)
1548 return (format_printf("%u", ft->wp->base.grid->hsize));
1549 return (NULL);
1552 /* Callback for insert_flag. */
1553 static void *
1554 format_cb_insert_flag(struct format_tree *ft)
1556 if (ft->wp != NULL) {
1557 if (ft->wp->base.mode & MODE_INSERT)
1558 return (xstrdup("1"));
1559 return (xstrdup("0"));
1561 return (NULL);
1564 /* Callback for keypad_cursor_flag. */
1565 static void *
1566 format_cb_keypad_cursor_flag(struct format_tree *ft)
1568 if (ft->wp != NULL) {
1569 if (ft->wp->base.mode & MODE_KCURSOR)
1570 return (xstrdup("1"));
1571 return (xstrdup("0"));
1573 return (NULL);
1576 /* Callback for keypad_flag. */
1577 static void *
1578 format_cb_keypad_flag(struct format_tree *ft)
1580 if (ft->wp != NULL) {
1581 if (ft->wp->base.mode & MODE_KKEYPAD)
1582 return (xstrdup("1"));
1583 return (xstrdup("0"));
1585 return (NULL);
1588 /* Callback for mouse_all_flag. */
1589 static void *
1590 format_cb_mouse_all_flag(struct format_tree *ft)
1592 if (ft->wp != NULL) {
1593 if (ft->wp->base.mode & MODE_MOUSE_ALL)
1594 return (xstrdup("1"));
1595 return (xstrdup("0"));
1597 return (NULL);
1600 /* Callback for mouse_any_flag. */
1601 static void *
1602 format_cb_mouse_any_flag(struct format_tree *ft)
1604 if (ft->wp != NULL) {
1605 if (ft->wp->base.mode & ALL_MOUSE_MODES)
1606 return (xstrdup("1"));
1607 return (xstrdup("0"));
1609 return (NULL);
1612 /* Callback for mouse_button_flag. */
1613 static void *
1614 format_cb_mouse_button_flag(struct format_tree *ft)
1616 if (ft->wp != NULL) {
1617 if (ft->wp->base.mode & MODE_MOUSE_BUTTON)
1618 return (xstrdup("1"));
1619 return (xstrdup("0"));
1621 return (NULL);
1624 /* Callback for mouse_pane. */
1625 static void *
1626 format_cb_mouse_pane(struct format_tree *ft)
1628 struct window_pane *wp;
1630 if (ft->m.valid) {
1631 wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1632 if (wp != NULL)
1633 return (format_printf("%%%u", wp->id));
1634 return (NULL);
1636 return (NULL);
1639 /* Callback for mouse_sgr_flag. */
1640 static void *
1641 format_cb_mouse_sgr_flag(struct format_tree *ft)
1643 if (ft->wp != NULL) {
1644 if (ft->wp->base.mode & MODE_MOUSE_SGR)
1645 return (xstrdup("1"));
1646 return (xstrdup("0"));
1648 return (NULL);
1651 /* Callback for mouse_standard_flag. */
1652 static void *
1653 format_cb_mouse_standard_flag(struct format_tree *ft)
1655 if (ft->wp != NULL) {
1656 if (ft->wp->base.mode & MODE_MOUSE_STANDARD)
1657 return (xstrdup("1"));
1658 return (xstrdup("0"));
1660 return (NULL);
1663 /* Callback for mouse_utf8_flag. */
1664 static void *
1665 format_cb_mouse_utf8_flag(struct format_tree *ft)
1667 if (ft->wp != NULL) {
1668 if (ft->wp->base.mode & MODE_MOUSE_UTF8)
1669 return (xstrdup("1"));
1670 return (xstrdup("0"));
1672 return (NULL);
1675 /* Callback for mouse_x. */
1676 static void *
1677 format_cb_mouse_x(struct format_tree *ft)
1679 struct window_pane *wp;
1680 u_int x, y;
1682 if (!ft->m.valid)
1683 return (NULL);
1684 wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1685 if (wp != NULL && cmd_mouse_at(wp, &ft->m, &x, &y, 0) == 0)
1686 return (format_printf("%u", x));
1687 if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED)) {
1688 if (ft->m.statusat == 0 && ft->m.y < ft->m.statuslines)
1689 return (format_printf("%u", ft->m.x));
1690 if (ft->m.statusat > 0 && ft->m.y >= (u_int)ft->m.statusat)
1691 return (format_printf("%u", ft->m.x));
1693 return (NULL);
1696 /* Callback for mouse_y. */
1697 static void *
1698 format_cb_mouse_y(struct format_tree *ft)
1700 struct window_pane *wp;
1701 u_int x, y;
1703 if (!ft->m.valid)
1704 return (NULL);
1705 wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1706 if (wp != NULL && cmd_mouse_at(wp, &ft->m, &x, &y, 0) == 0)
1707 return (format_printf("%u", y));
1708 if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED)) {
1709 if (ft->m.statusat == 0 && ft->m.y < ft->m.statuslines)
1710 return (format_printf("%u", ft->m.y));
1711 if (ft->m.statusat > 0 && ft->m.y >= (u_int)ft->m.statusat)
1712 return (format_printf("%u", ft->m.y - ft->m.statusat));
1714 return (NULL);
1717 /* Callback for next_session_id. */
1718 static void *
1719 format_cb_next_session_id(__unused struct format_tree *ft)
1721 return (format_printf("$%u", next_session_id));
1724 /* Callback for origin_flag. */
1725 static void *
1726 format_cb_origin_flag(struct format_tree *ft)
1728 if (ft->wp != NULL) {
1729 if (ft->wp->base.mode & MODE_ORIGIN)
1730 return (xstrdup("1"));
1731 return (xstrdup("0"));
1733 return (NULL);
1736 /* Callback for pane_active. */
1737 static void *
1738 format_cb_pane_active(struct format_tree *ft)
1740 if (ft->wp != NULL) {
1741 if (ft->wp == ft->wp->window->active)
1742 return (xstrdup("1"));
1743 return (xstrdup("0"));
1745 return (NULL);
1748 /* Callback for pane_at_left. */
1749 static void *
1750 format_cb_pane_at_left(struct format_tree *ft)
1752 if (ft->wp != NULL) {
1753 if (ft->wp->xoff == 0)
1754 return (xstrdup("1"));
1755 return (xstrdup("0"));
1757 return (NULL);
1760 /* Callback for pane_at_right. */
1761 static void *
1762 format_cb_pane_at_right(struct format_tree *ft)
1764 if (ft->wp != NULL) {
1765 if (ft->wp->xoff + ft->wp->sx == ft->wp->window->sx)
1766 return (xstrdup("1"));
1767 return (xstrdup("0"));
1769 return (NULL);
1772 /* Callback for pane_bottom. */
1773 static void *
1774 format_cb_pane_bottom(struct format_tree *ft)
1776 if (ft->wp != NULL)
1777 return (format_printf("%u", ft->wp->yoff + ft->wp->sy - 1));
1778 return (NULL);
1781 /* Callback for pane_dead. */
1782 static void *
1783 format_cb_pane_dead(struct format_tree *ft)
1785 if (ft->wp != NULL) {
1786 if (ft->wp->fd == -1)
1787 return (xstrdup("1"));
1788 return (xstrdup("0"));
1790 return (NULL);
1793 /* Callback for pane_dead_signal. */
1794 static void *
1795 format_cb_pane_dead_signal(struct format_tree *ft)
1797 struct window_pane *wp = ft->wp;
1798 const char *name;
1800 if (wp != NULL) {
1801 if ((wp->flags & PANE_STATUSREADY) && WIFSIGNALED(wp->status)) {
1802 name = sig2name(WTERMSIG(wp->status));
1803 return (format_printf("%s", name));
1805 return (NULL);
1807 return (NULL);
1810 /* Callback for pane_dead_status. */
1811 static void *
1812 format_cb_pane_dead_status(struct format_tree *ft)
1814 struct window_pane *wp = ft->wp;
1816 if (wp != NULL) {
1817 if ((wp->flags & PANE_STATUSREADY) && WIFEXITED(wp->status))
1818 return (format_printf("%d", WEXITSTATUS(wp->status)));
1819 return (NULL);
1821 return (NULL);
1824 /* Callback for pane_dead_time. */
1825 static void *
1826 format_cb_pane_dead_time(struct format_tree *ft)
1828 struct window_pane *wp = ft->wp;
1830 if (wp != NULL) {
1831 if (wp->flags & PANE_STATUSDRAWN)
1832 return (&wp->dead_time);
1833 return (NULL);
1835 return (NULL);
1838 /* Callback for pane_format. */
1839 static void *
1840 format_cb_pane_format(struct format_tree *ft)
1842 if (ft->type == FORMAT_TYPE_PANE)
1843 return (xstrdup("1"));
1844 return (xstrdup("0"));
1847 /* Callback for pane_height. */
1848 static void *
1849 format_cb_pane_height(struct format_tree *ft)
1851 if (ft->wp != NULL)
1852 return (format_printf("%u", ft->wp->sy));
1853 return (NULL);
1856 /* Callback for pane_id. */
1857 static void *
1858 format_cb_pane_id(struct format_tree *ft)
1860 if (ft->wp != NULL)
1861 return (format_printf("%%%u", ft->wp->id));
1862 return (NULL);
1865 /* Callback for pane_index. */
1866 static void *
1867 format_cb_pane_index(struct format_tree *ft)
1869 u_int idx;
1871 if (ft->wp != NULL && window_pane_index(ft->wp, &idx) == 0)
1872 return (format_printf("%u", idx));
1873 return (NULL);
1876 /* Callback for pane_input_off. */
1877 static void *
1878 format_cb_pane_input_off(struct format_tree *ft)
1880 if (ft->wp != NULL) {
1881 if (ft->wp->flags & PANE_INPUTOFF)
1882 return (xstrdup("1"));
1883 return (xstrdup("0"));
1885 return (NULL);
1888 /* Callback for pane_last. */
1889 static void *
1890 format_cb_pane_last(struct format_tree *ft)
1892 if (ft->wp != NULL) {
1893 if (ft->wp == ft->wp->window->last)
1894 return (xstrdup("1"));
1895 return (xstrdup("0"));
1897 return (NULL);
1900 /* Callback for pane_left. */
1901 static void *
1902 format_cb_pane_left(struct format_tree *ft)
1904 if (ft->wp != NULL)
1905 return (format_printf("%u", ft->wp->xoff));
1906 return (NULL);
1909 /* Callback for pane_marked. */
1910 static void *
1911 format_cb_pane_marked(struct format_tree *ft)
1913 if (ft->wp != NULL) {
1914 if (server_check_marked() && marked_pane.wp == ft->wp)
1915 return (xstrdup("1"));
1916 return (xstrdup("0"));
1918 return (NULL);
1921 /* Callback for pane_marked_set. */
1922 static void *
1923 format_cb_pane_marked_set(struct format_tree *ft)
1925 if (ft->wp != NULL) {
1926 if (server_check_marked())
1927 return (xstrdup("1"));
1928 return (xstrdup("0"));
1930 return (NULL);
1933 /* Callback for pane_mode. */
1934 static void *
1935 format_cb_pane_mode(struct format_tree *ft)
1937 struct window_mode_entry *wme;
1939 if (ft->wp != NULL) {
1940 wme = TAILQ_FIRST(&ft->wp->modes);
1941 if (wme != NULL)
1942 return (xstrdup(wme->mode->name));
1943 return (NULL);
1945 return (NULL);
1948 /* Callback for pane_path. */
1949 static void *
1950 format_cb_pane_path(struct format_tree *ft)
1952 if (ft->wp != NULL) {
1953 if (ft->wp->base.path == NULL)
1954 return (xstrdup(""));
1955 return (xstrdup(ft->wp->base.path));
1957 return (NULL);
1960 /* Callback for pane_pid. */
1961 static void *
1962 format_cb_pane_pid(struct format_tree *ft)
1964 if (ft->wp != NULL)
1965 return (format_printf("%ld", (long)ft->wp->pid));
1966 return (NULL);
1969 /* Callback for pane_pipe. */
1970 static void *
1971 format_cb_pane_pipe(struct format_tree *ft)
1973 if (ft->wp != NULL) {
1974 if (ft->wp->pipe_fd != -1)
1975 return (xstrdup("1"));
1976 return (xstrdup("0"));
1978 return (NULL);
1981 /* Callback for pane_right. */
1982 static void *
1983 format_cb_pane_right(struct format_tree *ft)
1985 if (ft->wp != NULL)
1986 return (format_printf("%u", ft->wp->xoff + ft->wp->sx - 1));
1987 return (NULL);
1990 /* Callback for pane_search_string. */
1991 static void *
1992 format_cb_pane_search_string(struct format_tree *ft)
1994 if (ft->wp != NULL) {
1995 if (ft->wp->searchstr == NULL)
1996 return (xstrdup(""));
1997 return (xstrdup(ft->wp->searchstr));
1999 return (NULL);
2002 /* Callback for pane_synchronized. */
2003 static void *
2004 format_cb_pane_synchronized(struct format_tree *ft)
2006 if (ft->wp != NULL) {
2007 if (options_get_number(ft->wp->options, "synchronize-panes"))
2008 return (xstrdup("1"));
2009 return (xstrdup("0"));
2011 return (NULL);
2014 /* Callback for pane_title. */
2015 static void *
2016 format_cb_pane_title(struct format_tree *ft)
2018 if (ft->wp != NULL)
2019 return (xstrdup(ft->wp->base.title));
2020 return (NULL);
2023 /* Callback for pane_top. */
2024 static void *
2025 format_cb_pane_top(struct format_tree *ft)
2027 if (ft->wp != NULL)
2028 return (format_printf("%u", ft->wp->yoff));
2029 return (NULL);
2032 /* Callback for pane_tty. */
2033 static void *
2034 format_cb_pane_tty(struct format_tree *ft)
2036 if (ft->wp != NULL)
2037 return (xstrdup(ft->wp->tty));
2038 return (NULL);
2041 /* Callback for pane_width. */
2042 static void *
2043 format_cb_pane_width(struct format_tree *ft)
2045 if (ft->wp != NULL)
2046 return (format_printf("%u", ft->wp->sx));
2047 return (NULL);
2050 /* Callback for scroll_region_lower. */
2051 static void *
2052 format_cb_scroll_region_lower(struct format_tree *ft)
2054 if (ft->wp != NULL)
2055 return (format_printf("%u", ft->wp->base.rlower));
2056 return (NULL);
2059 /* Callback for scroll_region_upper. */
2060 static void *
2061 format_cb_scroll_region_upper(struct format_tree *ft)
2063 if (ft->wp != NULL)
2064 return (format_printf("%u", ft->wp->base.rupper));
2065 return (NULL);
2068 /* Callback for session_attached. */
2069 static void *
2070 format_cb_session_attached(struct format_tree *ft)
2072 if (ft->s != NULL)
2073 return (format_printf("%u", ft->s->attached));
2074 return (NULL);
2077 /* Callback for session_format. */
2078 static void *
2079 format_cb_session_format(struct format_tree *ft)
2081 if (ft->type == FORMAT_TYPE_SESSION)
2082 return (xstrdup("1"));
2083 return (xstrdup("0"));
2086 /* Callback for session_group. */
2087 static void *
2088 format_cb_session_group(struct format_tree *ft)
2090 struct session_group *sg;
2092 if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL)
2093 return (xstrdup(sg->name));
2094 return (NULL);
2097 /* Callback for session_group_attached. */
2098 static void *
2099 format_cb_session_group_attached(struct format_tree *ft)
2101 struct session_group *sg;
2103 if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL)
2104 return (format_printf("%u", session_group_attached_count (sg)));
2105 return (NULL);
2108 /* Callback for session_group_many_attached. */
2109 static void *
2110 format_cb_session_group_many_attached(struct format_tree *ft)
2112 struct session_group *sg;
2114 if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL) {
2115 if (session_group_attached_count (sg) > 1)
2116 return (xstrdup("1"));
2117 return (xstrdup("0"));
2119 return (NULL);
2122 /* Callback for session_group_size. */
2123 static void *
2124 format_cb_session_group_size(struct format_tree *ft)
2126 struct session_group *sg;
2128 if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL)
2129 return (format_printf("%u", session_group_count (sg)));
2130 return (NULL);
2133 /* Callback for session_grouped. */
2134 static void *
2135 format_cb_session_grouped(struct format_tree *ft)
2137 if (ft->s != NULL) {
2138 if (session_group_contains(ft->s) != NULL)
2139 return (xstrdup("1"));
2140 return (xstrdup("0"));
2142 return (NULL);
2145 /* Callback for session_id. */
2146 static void *
2147 format_cb_session_id(struct format_tree *ft)
2149 if (ft->s != NULL)
2150 return (format_printf("$%u", ft->s->id));
2151 return (NULL);
2154 /* Callback for session_many_attached. */
2155 static void *
2156 format_cb_session_many_attached(struct format_tree *ft)
2158 if (ft->s != NULL) {
2159 if (ft->s->attached > 1)
2160 return (xstrdup("1"));
2161 return (xstrdup("0"));
2163 return (NULL);
2166 /* Callback for session_marked. */
2167 static void *
2168 format_cb_session_marked(struct format_tree *ft)
2170 if (ft->s != NULL) {
2171 if (server_check_marked() && marked_pane.s == ft->s)
2172 return (xstrdup("1"));
2173 return (xstrdup("0"));
2175 return (NULL);
2178 /* Callback for session_name. */
2179 static void *
2180 format_cb_session_name(struct format_tree *ft)
2182 if (ft->s != NULL)
2183 return (xstrdup(ft->s->name));
2184 return (NULL);
2187 /* Callback for session_path. */
2188 static void *
2189 format_cb_session_path(struct format_tree *ft)
2191 if (ft->s != NULL)
2192 return (xstrdup(ft->s->cwd));
2193 return (NULL);
2196 /* Callback for session_windows. */
2197 static void *
2198 format_cb_session_windows(struct format_tree *ft)
2200 if (ft->s != NULL)
2201 return (format_printf("%u", winlink_count(&ft->s->windows)));
2202 return (NULL);
2205 /* Callback for socket_path. */
2206 static void *
2207 format_cb_socket_path(__unused struct format_tree *ft)
2209 return (xstrdup(socket_path));
2212 /* Callback for version. */
2213 static void *
2214 format_cb_version(__unused struct format_tree *ft)
2216 return (xstrdup(getversion()));
2219 /* Callback for active_window_index. */
2220 static void *
2221 format_cb_active_window_index(struct format_tree *ft)
2223 if (ft->s != NULL)
2224 return (format_printf("%u", ft->s->curw->idx));
2225 return (NULL);
2228 /* Callback for last_window_index. */
2229 static void *
2230 format_cb_last_window_index(struct format_tree *ft)
2232 struct winlink *wl;
2234 if (ft->s != NULL) {
2235 wl = RB_MAX(winlinks, &ft->s->windows);
2236 return (format_printf("%u", wl->idx));
2238 return (NULL);
2241 /* Callback for window_active. */
2242 static void *
2243 format_cb_window_active(struct format_tree *ft)
2245 if (ft->wl != NULL) {
2246 if (ft->wl == ft->wl->session->curw)
2247 return (xstrdup("1"));
2248 return (xstrdup("0"));
2250 return (NULL);
2253 /* Callback for window_activity_flag. */
2254 static void *
2255 format_cb_window_activity_flag(struct format_tree *ft)
2257 if (ft->wl != NULL) {
2258 if (ft->wl->flags & WINLINK_ACTIVITY)
2259 return (xstrdup("1"));
2260 return (xstrdup("0"));
2262 return (NULL);
2265 /* Callback for window_bell_flag. */
2266 static void *
2267 format_cb_window_bell_flag(struct format_tree *ft)
2269 if (ft->wl != NULL) {
2270 if (ft->wl->flags & WINLINK_BELL)
2271 return (xstrdup("1"));
2272 return (xstrdup("0"));
2274 return (NULL);
2277 /* Callback for window_bigger. */
2278 static void *
2279 format_cb_window_bigger(struct format_tree *ft)
2281 u_int ox, oy, sx, sy;
2283 if (ft->c != NULL) {
2284 if (tty_window_offset(&ft->c->tty, &ox, &oy, &sx, &sy))
2285 return (xstrdup("1"));
2286 return (xstrdup("0"));
2288 return (NULL);
2291 /* Callback for window_cell_height. */
2292 static void *
2293 format_cb_window_cell_height(struct format_tree *ft)
2295 if (ft->w != NULL)
2296 return (format_printf("%u", ft->w->ypixel));
2297 return (NULL);
2300 /* Callback for window_cell_width. */
2301 static void *
2302 format_cb_window_cell_width(struct format_tree *ft)
2304 if (ft->w != NULL)
2305 return (format_printf("%u", ft->w->xpixel));
2306 return (NULL);
2309 /* Callback for window_end_flag. */
2310 static void *
2311 format_cb_window_end_flag(struct format_tree *ft)
2313 if (ft->wl != NULL) {
2314 if (ft->wl == RB_MAX(winlinks, &ft->wl->session->windows))
2315 return (xstrdup("1"));
2316 return (xstrdup("0"));
2318 return (NULL);
2321 /* Callback for window_flags. */
2322 static void *
2323 format_cb_window_flags(struct format_tree *ft)
2325 if (ft->wl != NULL)
2326 return (xstrdup(window_printable_flags(ft->wl, 1)));
2327 return (NULL);
2330 /* Callback for window_format. */
2331 static void *
2332 format_cb_window_format(struct format_tree *ft)
2334 if (ft->type == FORMAT_TYPE_WINDOW)
2335 return (xstrdup("1"));
2336 return (xstrdup("0"));
2339 /* Callback for window_height. */
2340 static void *
2341 format_cb_window_height(struct format_tree *ft)
2343 if (ft->w != NULL)
2344 return (format_printf("%u", ft->w->sy));
2345 return (NULL);
2348 /* Callback for window_id. */
2349 static void *
2350 format_cb_window_id(struct format_tree *ft)
2352 if (ft->w != NULL)
2353 return (format_printf("@%u", ft->w->id));
2354 return (NULL);
2357 /* Callback for window_index. */
2358 static void *
2359 format_cb_window_index(struct format_tree *ft)
2361 if (ft->wl != NULL)
2362 return (format_printf("%d", ft->wl->idx));
2363 return (NULL);
2366 /* Callback for window_last_flag. */
2367 static void *
2368 format_cb_window_last_flag(struct format_tree *ft)
2370 if (ft->wl != NULL) {
2371 if (ft->wl == TAILQ_FIRST(&ft->wl->session->lastw))
2372 return (xstrdup("1"));
2373 return (xstrdup("0"));
2375 return (NULL);
2378 /* Callback for window_linked. */
2379 static void *
2380 format_cb_window_linked(struct format_tree *ft)
2382 if (ft->wl != NULL) {
2383 if (session_is_linked(ft->wl->session, ft->wl->window))
2384 return (xstrdup("1"));
2385 return (xstrdup("0"));
2387 return (NULL);
2390 /* Callback for window_linked_sessions. */
2391 static void *
2392 format_cb_window_linked_sessions(struct format_tree *ft)
2394 if (ft->wl != NULL)
2395 return (format_printf("%u", ft->wl->window->references));
2396 return (NULL);
2399 /* Callback for window_marked_flag. */
2400 static void *
2401 format_cb_window_marked_flag(struct format_tree *ft)
2403 if (ft->wl != NULL) {
2404 if (server_check_marked() && marked_pane.wl == ft->wl)
2405 return (xstrdup("1"));
2406 return (xstrdup("0"));
2408 return (NULL);
2411 /* Callback for window_name. */
2412 static void *
2413 format_cb_window_name(struct format_tree *ft)
2415 if (ft->w != NULL)
2416 return (format_printf("%s", ft->w->name));
2417 return (NULL);
2420 /* Callback for window_offset_x. */
2421 static void *
2422 format_cb_window_offset_x(struct format_tree *ft)
2424 u_int ox, oy, sx, sy;
2426 if (ft->c != NULL) {
2427 if (tty_window_offset(&ft->c->tty, &ox, &oy, &sx, &sy))
2428 return (format_printf("%u", ox));
2429 return (NULL);
2431 return (NULL);
2434 /* Callback for window_offset_y. */
2435 static void *
2436 format_cb_window_offset_y(struct format_tree *ft)
2438 u_int ox, oy, sx, sy;
2440 if (ft->c != NULL) {
2441 if (tty_window_offset(&ft->c->tty, &ox, &oy, &sx, &sy))
2442 return (format_printf("%u", oy));
2443 return (NULL);
2445 return (NULL);
2448 /* Callback for window_panes. */
2449 static void *
2450 format_cb_window_panes(struct format_tree *ft)
2452 if (ft->w != NULL)
2453 return (format_printf("%u", window_count_panes(ft->w)));
2454 return (NULL);
2457 /* Callback for window_raw_flags. */
2458 static void *
2459 format_cb_window_raw_flags(struct format_tree *ft)
2461 if (ft->wl != NULL)
2462 return (xstrdup(window_printable_flags(ft->wl, 0)));
2463 return (NULL);
2466 /* Callback for window_silence_flag. */
2467 static void *
2468 format_cb_window_silence_flag(struct format_tree *ft)
2470 if (ft->wl != NULL) {
2471 if (ft->wl->flags & WINLINK_SILENCE)
2472 return (xstrdup("1"));
2473 return (xstrdup("0"));
2475 return (NULL);
2478 /* Callback for window_start_flag. */
2479 static void *
2480 format_cb_window_start_flag(struct format_tree *ft)
2482 if (ft->wl != NULL) {
2483 if (ft->wl == RB_MIN(winlinks, &ft->wl->session->windows))
2484 return (xstrdup("1"));
2485 return (xstrdup("0"));
2487 return (NULL);
2490 /* Callback for window_width. */
2491 static void *
2492 format_cb_window_width(struct format_tree *ft)
2494 if (ft->w != NULL)
2495 return (format_printf("%u", ft->w->sx));
2496 return (NULL);
2499 /* Callback for window_zoomed_flag. */
2500 static void *
2501 format_cb_window_zoomed_flag(struct format_tree *ft)
2503 if (ft->w != NULL) {
2504 if (ft->w->flags & WINDOW_ZOOMED)
2505 return (xstrdup("1"));
2506 return (xstrdup("0"));
2508 return (NULL);
2511 /* Callback for wrap_flag. */
2512 static void *
2513 format_cb_wrap_flag(struct format_tree *ft)
2515 if (ft->wp != NULL) {
2516 if (ft->wp->base.mode & MODE_WRAP)
2517 return (xstrdup("1"));
2518 return (xstrdup("0"));
2520 return (NULL);
2523 /* Callback for buffer_created. */
2524 static void *
2525 format_cb_buffer_created(struct format_tree *ft)
2527 static struct timeval tv;
2529 if (ft->pb != NULL) {
2530 timerclear(&tv);
2531 tv.tv_sec = paste_buffer_created(ft->pb);
2532 return (&tv);
2534 return (NULL);
2537 /* Callback for client_activity. */
2538 static void *
2539 format_cb_client_activity(struct format_tree *ft)
2541 if (ft->c != NULL)
2542 return (&ft->c->activity_time);
2543 return (NULL);
2546 /* Callback for client_created. */
2547 static void *
2548 format_cb_client_created(struct format_tree *ft)
2550 if (ft->c != NULL)
2551 return (&ft->c->creation_time);
2552 return (NULL);
2555 /* Callback for session_activity. */
2556 static void *
2557 format_cb_session_activity(struct format_tree *ft)
2559 if (ft->s != NULL)
2560 return (&ft->s->activity_time);
2561 return (NULL);
2564 /* Callback for session_created. */
2565 static void *
2566 format_cb_session_created(struct format_tree *ft)
2568 if (ft->s != NULL)
2569 return (&ft->s->creation_time);
2570 return (NULL);
2573 /* Callback for session_last_attached. */
2574 static void *
2575 format_cb_session_last_attached(struct format_tree *ft)
2577 if (ft->s != NULL)
2578 return (&ft->s->last_attached_time);
2579 return (NULL);
2582 /* Callback for start_time. */
2583 static void *
2584 format_cb_start_time(__unused struct format_tree *ft)
2586 return (&start_time);
2589 /* Callback for window_activity. */
2590 static void *
2591 format_cb_window_activity(struct format_tree *ft)
2593 if (ft->w != NULL)
2594 return (&ft->w->activity_time);
2595 return (NULL);
2598 /* Callback for buffer_mode_format, */
2599 static void *
2600 format_cb_buffer_mode_format(__unused struct format_tree *ft)
2602 return (xstrdup(window_buffer_mode.default_format));
2605 /* Callback for client_mode_format, */
2606 static void *
2607 format_cb_client_mode_format(__unused struct format_tree *ft)
2609 return (xstrdup(window_client_mode.default_format));
2612 /* Callback for tree_mode_format, */
2613 static void *
2614 format_cb_tree_mode_format(__unused struct format_tree *ft)
2616 return (xstrdup(window_tree_mode.default_format));
2619 /* Callback for uid. */
2620 static void *
2621 format_cb_uid(__unused struct format_tree *ft)
2623 return (format_printf("%ld", (long)getuid()));
2626 /* Callback for user. */
2627 static void *
2628 format_cb_user(__unused struct format_tree *ft)
2630 struct passwd *pw;
2632 if ((pw = getpwuid(getuid())) != NULL)
2633 return (xstrdup(pw->pw_name));
2634 return (NULL);
2637 /* Format table type. */
2638 enum format_table_type {
2639 FORMAT_TABLE_STRING,
2640 FORMAT_TABLE_TIME
2643 /* Format table entry. */
2644 struct format_table_entry {
2645 const char *key;
2646 enum format_table_type type;
2647 format_cb cb;
2651 * Format table. Default format variables (that are almost always in the tree
2652 * and where the value is expanded by a callback in this file) are listed here.
2653 * Only variables which are added by the caller go into the tree.
2655 static const struct format_table_entry format_table[] = {
2656 { "active_window_index", FORMAT_TABLE_STRING,
2657 format_cb_active_window_index
2659 { "alternate_on", FORMAT_TABLE_STRING,
2660 format_cb_alternate_on
2662 { "alternate_saved_x", FORMAT_TABLE_STRING,
2663 format_cb_alternate_saved_x
2665 { "alternate_saved_y", FORMAT_TABLE_STRING,
2666 format_cb_alternate_saved_y
2668 { "buffer_created", FORMAT_TABLE_TIME,
2669 format_cb_buffer_created
2671 { "buffer_mode_format", FORMAT_TABLE_STRING,
2672 format_cb_buffer_mode_format
2674 { "buffer_name", FORMAT_TABLE_STRING,
2675 format_cb_buffer_name
2677 { "buffer_sample", FORMAT_TABLE_STRING,
2678 format_cb_buffer_sample
2680 { "buffer_size", FORMAT_TABLE_STRING,
2681 format_cb_buffer_size
2683 { "client_activity", FORMAT_TABLE_TIME,
2684 format_cb_client_activity
2686 { "client_cell_height", FORMAT_TABLE_STRING,
2687 format_cb_client_cell_height
2689 { "client_cell_width", FORMAT_TABLE_STRING,
2690 format_cb_client_cell_width
2692 { "client_control_mode", FORMAT_TABLE_STRING,
2693 format_cb_client_control_mode
2695 { "client_created", FORMAT_TABLE_TIME,
2696 format_cb_client_created
2698 { "client_discarded", FORMAT_TABLE_STRING,
2699 format_cb_client_discarded
2701 { "client_flags", FORMAT_TABLE_STRING,
2702 format_cb_client_flags
2704 { "client_height", FORMAT_TABLE_STRING,
2705 format_cb_client_height
2707 { "client_key_table", FORMAT_TABLE_STRING,
2708 format_cb_client_key_table
2710 { "client_last_session", FORMAT_TABLE_STRING,
2711 format_cb_client_last_session
2713 { "client_mode_format", FORMAT_TABLE_STRING,
2714 format_cb_client_mode_format
2716 { "client_name", FORMAT_TABLE_STRING,
2717 format_cb_client_name
2719 { "client_pid", FORMAT_TABLE_STRING,
2720 format_cb_client_pid
2722 { "client_prefix", FORMAT_TABLE_STRING,
2723 format_cb_client_prefix
2725 { "client_readonly", FORMAT_TABLE_STRING,
2726 format_cb_client_readonly
2728 { "client_session", FORMAT_TABLE_STRING,
2729 format_cb_client_session
2731 { "client_termfeatures", FORMAT_TABLE_STRING,
2732 format_cb_client_termfeatures
2734 { "client_termname", FORMAT_TABLE_STRING,
2735 format_cb_client_termname
2737 { "client_termtype", FORMAT_TABLE_STRING,
2738 format_cb_client_termtype
2740 { "client_tty", FORMAT_TABLE_STRING,
2741 format_cb_client_tty
2743 { "client_uid", FORMAT_TABLE_STRING,
2744 format_cb_client_uid
2746 { "client_user", FORMAT_TABLE_STRING,
2747 format_cb_client_user
2749 { "client_utf8", FORMAT_TABLE_STRING,
2750 format_cb_client_utf8
2752 { "client_width", FORMAT_TABLE_STRING,
2753 format_cb_client_width
2755 { "client_written", FORMAT_TABLE_STRING,
2756 format_cb_client_written
2758 { "config_files", FORMAT_TABLE_STRING,
2759 format_cb_config_files
2761 { "cursor_character", FORMAT_TABLE_STRING,
2762 format_cb_cursor_character
2764 { "cursor_flag", FORMAT_TABLE_STRING,
2765 format_cb_cursor_flag
2767 { "cursor_x", FORMAT_TABLE_STRING,
2768 format_cb_cursor_x
2770 { "cursor_y", FORMAT_TABLE_STRING,
2771 format_cb_cursor_y
2773 { "history_all_bytes", FORMAT_TABLE_STRING,
2774 format_cb_history_all_bytes
2776 { "history_bytes", FORMAT_TABLE_STRING,
2777 format_cb_history_bytes
2779 { "history_limit", FORMAT_TABLE_STRING,
2780 format_cb_history_limit
2782 { "history_size", FORMAT_TABLE_STRING,
2783 format_cb_history_size
2785 { "host", FORMAT_TABLE_STRING,
2786 format_cb_host
2788 { "host_short", FORMAT_TABLE_STRING,
2789 format_cb_host_short
2791 { "insert_flag", FORMAT_TABLE_STRING,
2792 format_cb_insert_flag
2794 { "keypad_cursor_flag", FORMAT_TABLE_STRING,
2795 format_cb_keypad_cursor_flag
2797 { "keypad_flag", FORMAT_TABLE_STRING,
2798 format_cb_keypad_flag
2800 { "last_window_index", FORMAT_TABLE_STRING,
2801 format_cb_last_window_index
2803 { "mouse_all_flag", FORMAT_TABLE_STRING,
2804 format_cb_mouse_all_flag
2806 { "mouse_any_flag", FORMAT_TABLE_STRING,
2807 format_cb_mouse_any_flag
2809 { "mouse_button_flag", FORMAT_TABLE_STRING,
2810 format_cb_mouse_button_flag
2812 { "mouse_hyperlink", FORMAT_TABLE_STRING,
2813 format_cb_mouse_hyperlink
2815 { "mouse_line", FORMAT_TABLE_STRING,
2816 format_cb_mouse_line
2818 { "mouse_pane", FORMAT_TABLE_STRING,
2819 format_cb_mouse_pane
2821 { "mouse_sgr_flag", FORMAT_TABLE_STRING,
2822 format_cb_mouse_sgr_flag
2824 { "mouse_standard_flag", FORMAT_TABLE_STRING,
2825 format_cb_mouse_standard_flag
2827 { "mouse_utf8_flag", FORMAT_TABLE_STRING,
2828 format_cb_mouse_utf8_flag
2830 { "mouse_word", FORMAT_TABLE_STRING,
2831 format_cb_mouse_word
2833 { "mouse_x", FORMAT_TABLE_STRING,
2834 format_cb_mouse_x
2836 { "mouse_y", FORMAT_TABLE_STRING,
2837 format_cb_mouse_y
2839 { "next_session_id", FORMAT_TABLE_STRING,
2840 format_cb_next_session_id
2842 { "origin_flag", FORMAT_TABLE_STRING,
2843 format_cb_origin_flag
2845 { "pane_active", FORMAT_TABLE_STRING,
2846 format_cb_pane_active
2848 { "pane_at_bottom", FORMAT_TABLE_STRING,
2849 format_cb_pane_at_bottom
2851 { "pane_at_left", FORMAT_TABLE_STRING,
2852 format_cb_pane_at_left
2854 { "pane_at_right", FORMAT_TABLE_STRING,
2855 format_cb_pane_at_right
2857 { "pane_at_top", FORMAT_TABLE_STRING,
2858 format_cb_pane_at_top
2860 { "pane_bg", FORMAT_TABLE_STRING,
2861 format_cb_pane_bg
2863 { "pane_bottom", FORMAT_TABLE_STRING,
2864 format_cb_pane_bottom
2866 { "pane_current_command", FORMAT_TABLE_STRING,
2867 format_cb_current_command
2869 { "pane_current_path", FORMAT_TABLE_STRING,
2870 format_cb_current_path
2872 { "pane_dead", FORMAT_TABLE_STRING,
2873 format_cb_pane_dead
2875 { "pane_dead_signal", FORMAT_TABLE_STRING,
2876 format_cb_pane_dead_signal
2878 { "pane_dead_status", FORMAT_TABLE_STRING,
2879 format_cb_pane_dead_status
2881 { "pane_dead_time", FORMAT_TABLE_TIME,
2882 format_cb_pane_dead_time
2884 { "pane_fg", FORMAT_TABLE_STRING,
2885 format_cb_pane_fg
2887 { "pane_format", FORMAT_TABLE_STRING,
2888 format_cb_pane_format
2890 { "pane_height", FORMAT_TABLE_STRING,
2891 format_cb_pane_height
2893 { "pane_id", FORMAT_TABLE_STRING,
2894 format_cb_pane_id
2896 { "pane_in_mode", FORMAT_TABLE_STRING,
2897 format_cb_pane_in_mode
2899 { "pane_index", FORMAT_TABLE_STRING,
2900 format_cb_pane_index
2902 { "pane_input_off", FORMAT_TABLE_STRING,
2903 format_cb_pane_input_off
2905 { "pane_last", FORMAT_TABLE_STRING,
2906 format_cb_pane_last
2908 { "pane_left", FORMAT_TABLE_STRING,
2909 format_cb_pane_left
2911 { "pane_marked", FORMAT_TABLE_STRING,
2912 format_cb_pane_marked
2914 { "pane_marked_set", FORMAT_TABLE_STRING,
2915 format_cb_pane_marked_set
2917 { "pane_mode", FORMAT_TABLE_STRING,
2918 format_cb_pane_mode
2920 { "pane_path", FORMAT_TABLE_STRING,
2921 format_cb_pane_path
2923 { "pane_pid", FORMAT_TABLE_STRING,
2924 format_cb_pane_pid
2926 { "pane_pipe", FORMAT_TABLE_STRING,
2927 format_cb_pane_pipe
2929 { "pane_right", FORMAT_TABLE_STRING,
2930 format_cb_pane_right
2932 { "pane_search_string", FORMAT_TABLE_STRING,
2933 format_cb_pane_search_string
2935 { "pane_start_command", FORMAT_TABLE_STRING,
2936 format_cb_start_command
2938 { "pane_start_path", FORMAT_TABLE_STRING,
2939 format_cb_start_path
2941 { "pane_synchronized", FORMAT_TABLE_STRING,
2942 format_cb_pane_synchronized
2944 { "pane_tabs", FORMAT_TABLE_STRING,
2945 format_cb_pane_tabs
2947 { "pane_title", FORMAT_TABLE_STRING,
2948 format_cb_pane_title
2950 { "pane_top", FORMAT_TABLE_STRING,
2951 format_cb_pane_top
2953 { "pane_tty", FORMAT_TABLE_STRING,
2954 format_cb_pane_tty
2956 { "pane_width", FORMAT_TABLE_STRING,
2957 format_cb_pane_width
2959 { "pid", FORMAT_TABLE_STRING,
2960 format_cb_pid
2962 { "scroll_region_lower", FORMAT_TABLE_STRING,
2963 format_cb_scroll_region_lower
2965 { "scroll_region_upper", FORMAT_TABLE_STRING,
2966 format_cb_scroll_region_upper
2968 { "session_activity", FORMAT_TABLE_TIME,
2969 format_cb_session_activity
2971 { "session_alerts", FORMAT_TABLE_STRING,
2972 format_cb_session_alerts
2974 { "session_attached", FORMAT_TABLE_STRING,
2975 format_cb_session_attached
2977 { "session_attached_list", FORMAT_TABLE_STRING,
2978 format_cb_session_attached_list
2980 { "session_created", FORMAT_TABLE_TIME,
2981 format_cb_session_created
2983 { "session_format", FORMAT_TABLE_STRING,
2984 format_cb_session_format
2986 { "session_group", FORMAT_TABLE_STRING,
2987 format_cb_session_group
2989 { "session_group_attached", FORMAT_TABLE_STRING,
2990 format_cb_session_group_attached
2992 { "session_group_attached_list", FORMAT_TABLE_STRING,
2993 format_cb_session_group_attached_list
2995 { "session_group_list", FORMAT_TABLE_STRING,
2996 format_cb_session_group_list
2998 { "session_group_many_attached", FORMAT_TABLE_STRING,
2999 format_cb_session_group_many_attached
3001 { "session_group_size", FORMAT_TABLE_STRING,
3002 format_cb_session_group_size
3004 { "session_grouped", FORMAT_TABLE_STRING,
3005 format_cb_session_grouped
3007 { "session_id", FORMAT_TABLE_STRING,
3008 format_cb_session_id
3010 { "session_last_attached", FORMAT_TABLE_TIME,
3011 format_cb_session_last_attached
3013 { "session_many_attached", FORMAT_TABLE_STRING,
3014 format_cb_session_many_attached
3016 { "session_marked", FORMAT_TABLE_STRING,
3017 format_cb_session_marked,
3019 { "session_name", FORMAT_TABLE_STRING,
3020 format_cb_session_name
3022 { "session_path", FORMAT_TABLE_STRING,
3023 format_cb_session_path
3025 { "session_stack", FORMAT_TABLE_STRING,
3026 format_cb_session_stack
3028 { "session_windows", FORMAT_TABLE_STRING,
3029 format_cb_session_windows
3031 { "socket_path", FORMAT_TABLE_STRING,
3032 format_cb_socket_path
3034 { "start_time", FORMAT_TABLE_TIME,
3035 format_cb_start_time
3037 { "tree_mode_format", FORMAT_TABLE_STRING,
3038 format_cb_tree_mode_format
3040 { "uid", FORMAT_TABLE_STRING,
3041 format_cb_uid
3043 { "user", FORMAT_TABLE_STRING,
3044 format_cb_user
3046 { "version", FORMAT_TABLE_STRING,
3047 format_cb_version
3049 { "window_active", FORMAT_TABLE_STRING,
3050 format_cb_window_active
3052 { "window_active_clients", FORMAT_TABLE_STRING,
3053 format_cb_window_active_clients
3055 { "window_active_clients_list", FORMAT_TABLE_STRING,
3056 format_cb_window_active_clients_list
3058 { "window_active_sessions", FORMAT_TABLE_STRING,
3059 format_cb_window_active_sessions
3061 { "window_active_sessions_list", FORMAT_TABLE_STRING,
3062 format_cb_window_active_sessions_list
3064 { "window_activity", FORMAT_TABLE_TIME,
3065 format_cb_window_activity
3067 { "window_activity_flag", FORMAT_TABLE_STRING,
3068 format_cb_window_activity_flag
3070 { "window_bell_flag", FORMAT_TABLE_STRING,
3071 format_cb_window_bell_flag
3073 { "window_bigger", FORMAT_TABLE_STRING,
3074 format_cb_window_bigger
3076 { "window_cell_height", FORMAT_TABLE_STRING,
3077 format_cb_window_cell_height
3079 { "window_cell_width", FORMAT_TABLE_STRING,
3080 format_cb_window_cell_width
3082 { "window_end_flag", FORMAT_TABLE_STRING,
3083 format_cb_window_end_flag
3085 { "window_flags", FORMAT_TABLE_STRING,
3086 format_cb_window_flags
3088 { "window_format", FORMAT_TABLE_STRING,
3089 format_cb_window_format
3091 { "window_height", FORMAT_TABLE_STRING,
3092 format_cb_window_height
3094 { "window_id", FORMAT_TABLE_STRING,
3095 format_cb_window_id
3097 { "window_index", FORMAT_TABLE_STRING,
3098 format_cb_window_index
3100 { "window_last_flag", FORMAT_TABLE_STRING,
3101 format_cb_window_last_flag
3103 { "window_layout", FORMAT_TABLE_STRING,
3104 format_cb_window_layout
3106 { "window_linked", FORMAT_TABLE_STRING,
3107 format_cb_window_linked
3109 { "window_linked_sessions", FORMAT_TABLE_STRING,
3110 format_cb_window_linked_sessions
3112 { "window_linked_sessions_list", FORMAT_TABLE_STRING,
3113 format_cb_window_linked_sessions_list
3115 { "window_marked_flag", FORMAT_TABLE_STRING,
3116 format_cb_window_marked_flag
3118 { "window_name", FORMAT_TABLE_STRING,
3119 format_cb_window_name
3121 { "window_offset_x", FORMAT_TABLE_STRING,
3122 format_cb_window_offset_x
3124 { "window_offset_y", FORMAT_TABLE_STRING,
3125 format_cb_window_offset_y
3127 { "window_panes", FORMAT_TABLE_STRING,
3128 format_cb_window_panes
3130 { "window_raw_flags", FORMAT_TABLE_STRING,
3131 format_cb_window_raw_flags
3133 { "window_silence_flag", FORMAT_TABLE_STRING,
3134 format_cb_window_silence_flag
3136 { "window_stack_index", FORMAT_TABLE_STRING,
3137 format_cb_window_stack_index
3139 { "window_start_flag", FORMAT_TABLE_STRING,
3140 format_cb_window_start_flag
3142 { "window_visible_layout", FORMAT_TABLE_STRING,
3143 format_cb_window_visible_layout
3145 { "window_width", FORMAT_TABLE_STRING,
3146 format_cb_window_width
3148 { "window_zoomed_flag", FORMAT_TABLE_STRING,
3149 format_cb_window_zoomed_flag
3151 { "wrap_flag", FORMAT_TABLE_STRING,
3152 format_cb_wrap_flag
3156 /* Compare format table entries. */
3157 static int
3158 format_table_compare(const void *key0, const void *entry0)
3160 const char *key = key0;
3161 const struct format_table_entry *entry = entry0;
3163 return (strcmp(key, entry->key));
3166 /* Get a format callback. */
3167 static struct format_table_entry *
3168 format_table_get(const char *key)
3170 return (bsearch(key, format_table, nitems(format_table),
3171 sizeof *format_table, format_table_compare));
3174 /* Merge one format tree into another. */
3175 void
3176 format_merge(struct format_tree *ft, struct format_tree *from)
3178 struct format_entry *fe;
3180 RB_FOREACH(fe, format_entry_tree, &from->tree) {
3181 if (fe->value != NULL)
3182 format_add(ft, fe->key, "%s", fe->value);
3186 /* Get format pane. */
3187 struct window_pane *
3188 format_get_pane(struct format_tree *ft)
3190 return (ft->wp);
3193 /* Add item bits to tree. */
3194 static void
3195 format_create_add_item(struct format_tree *ft, struct cmdq_item *item)
3197 struct key_event *event = cmdq_get_event(item);
3198 struct mouse_event *m = &event->m;
3200 cmdq_merge_formats(item, ft);
3201 memcpy(&ft->m, m, sizeof ft->m);
3204 /* Create a new tree. */
3205 struct format_tree *
3206 format_create(struct client *c, struct cmdq_item *item, int tag, int flags)
3208 struct format_tree *ft;
3210 ft = xcalloc(1, sizeof *ft);
3211 RB_INIT(&ft->tree);
3213 if (c != NULL) {
3214 ft->client = c;
3215 ft->client->references++;
3217 ft->item = item;
3219 ft->tag = tag;
3220 ft->flags = flags;
3222 if (item != NULL)
3223 format_create_add_item(ft, item);
3225 return (ft);
3228 /* Free a tree. */
3229 void
3230 format_free(struct format_tree *ft)
3232 struct format_entry *fe, *fe1;
3234 RB_FOREACH_SAFE(fe, format_entry_tree, &ft->tree, fe1) {
3235 RB_REMOVE(format_entry_tree, &ft->tree, fe);
3236 free(fe->value);
3237 free(fe->key);
3238 free(fe);
3241 if (ft->client != NULL)
3242 server_client_unref(ft->client);
3243 free(ft);
3246 /* Log each format. */
3247 static void
3248 format_log_debug_cb(const char *key, const char *value, void *arg)
3250 const char *prefix = arg;
3252 log_debug("%s: %s=%s", prefix, key, value);
3255 /* Log a format tree. */
3256 void
3257 format_log_debug(struct format_tree *ft, const char *prefix)
3259 format_each(ft, format_log_debug_cb, (void *)prefix);
3262 /* Walk each format. */
3263 void
3264 format_each(struct format_tree *ft, void (*cb)(const char *, const char *,
3265 void *), void *arg)
3267 const struct format_table_entry *fte;
3268 struct format_entry *fe;
3269 u_int i;
3270 char s[64];
3271 void *value;
3272 struct timeval *tv;
3274 for (i = 0; i < nitems(format_table); i++) {
3275 fte = &format_table[i];
3277 value = fte->cb(ft);
3278 if (value == NULL)
3279 continue;
3280 if (fte->type == FORMAT_TABLE_TIME) {
3281 tv = value;
3282 xsnprintf(s, sizeof s, "%lld", (long long)tv->tv_sec);
3283 cb(fte->key, s, arg);
3284 } else {
3285 cb(fte->key, value, arg);
3286 free(value);
3289 RB_FOREACH(fe, format_entry_tree, &ft->tree) {
3290 if (fe->time != 0) {
3291 xsnprintf(s, sizeof s, "%lld", (long long)fe->time);
3292 cb(fe->key, s, arg);
3293 } else {
3294 if (fe->value == NULL && fe->cb != NULL) {
3295 fe->value = fe->cb(ft);
3296 if (fe->value == NULL)
3297 fe->value = xstrdup("");
3299 cb(fe->key, fe->value, arg);
3304 /* Add a key-value pair. */
3305 void
3306 format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
3308 struct format_entry *fe;
3309 struct format_entry *fe_now;
3310 va_list ap;
3312 fe = xmalloc(sizeof *fe);
3313 fe->key = xstrdup(key);
3315 fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
3316 if (fe_now != NULL) {
3317 free(fe->key);
3318 free(fe);
3319 free(fe_now->value);
3320 fe = fe_now;
3323 fe->cb = NULL;
3324 fe->time = 0;
3326 va_start(ap, fmt);
3327 xvasprintf(&fe->value, fmt, ap);
3328 va_end(ap);
3331 /* Add a key and time. */
3332 void
3333 format_add_tv(struct format_tree *ft, const char *key, struct timeval *tv)
3335 struct format_entry *fe, *fe_now;
3337 fe = xmalloc(sizeof *fe);
3338 fe->key = xstrdup(key);
3340 fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
3341 if (fe_now != NULL) {
3342 free(fe->key);
3343 free(fe);
3344 free(fe_now->value);
3345 fe = fe_now;
3348 fe->cb = NULL;
3349 fe->time = tv->tv_sec;
3351 fe->value = NULL;
3354 /* Add a key and function. */
3355 void
3356 format_add_cb(struct format_tree *ft, const char *key, format_cb cb)
3358 struct format_entry *fe;
3359 struct format_entry *fe_now;
3361 fe = xmalloc(sizeof *fe);
3362 fe->key = xstrdup(key);
3364 fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
3365 if (fe_now != NULL) {
3366 free(fe->key);
3367 free(fe);
3368 free(fe_now->value);
3369 fe = fe_now;
3372 fe->cb = cb;
3373 fe->time = 0;
3375 fe->value = NULL;
3378 /* Quote shell special characters in string. */
3379 static char *
3380 format_quote_shell(const char *s)
3382 const char *cp;
3383 char *out, *at;
3385 at = out = xmalloc(strlen(s) * 2 + 1);
3386 for (cp = s; *cp != '\0'; cp++) {
3387 if (strchr("|&;<>()$`\\\"'*?[# =%", *cp) != NULL)
3388 *at++ = '\\';
3389 *at++ = *cp;
3391 *at = '\0';
3392 return (out);
3395 /* Quote #s in string. */
3396 static char *
3397 format_quote_style(const char *s)
3399 const char *cp;
3400 char *out, *at;
3402 at = out = xmalloc(strlen(s) * 2 + 1);
3403 for (cp = s; *cp != '\0'; cp++) {
3404 if (*cp == '#')
3405 *at++ = '#';
3406 *at++ = *cp;
3408 *at = '\0';
3409 return (out);
3412 /* Make a prettier time. */
3413 char *
3414 format_pretty_time(time_t t, int seconds)
3416 struct tm now_tm, tm;
3417 time_t now, age;
3418 char s[9];
3420 time(&now);
3421 if (now < t)
3422 now = t;
3423 age = now - t;
3425 localtime_r(&now, &now_tm);
3426 localtime_r(&t, &tm);
3428 /* Last 24 hours. */
3429 if (age < 24 * 3600) {
3430 if (seconds)
3431 strftime(s, sizeof s, "%H:%M:%S", &tm);
3432 else
3433 strftime(s, sizeof s, "%H:%M", &tm);
3434 return (xstrdup(s));
3437 /* This month or last 28 days. */
3438 if ((tm.tm_year == now_tm.tm_year && tm.tm_mon == now_tm.tm_mon) ||
3439 age < 28 * 24 * 3600) {
3440 strftime(s, sizeof s, "%a%d", &tm);
3441 return (xstrdup(s));
3444 /* Last 12 months. */
3445 if ((tm.tm_year == now_tm.tm_year && tm.tm_mon < now_tm.tm_mon) ||
3446 (tm.tm_year == now_tm.tm_year - 1 && tm.tm_mon > now_tm.tm_mon)) {
3447 strftime(s, sizeof s, "%d%b", &tm);
3448 return (xstrdup(s));
3451 /* Older than that. */
3452 strftime(s, sizeof s, "%h%y", &tm);
3453 return (xstrdup(s));
3456 /* Find a format entry. */
3457 static char *
3458 format_find(struct format_tree *ft, const char *key, int modifiers,
3459 const char *time_format)
3461 struct format_table_entry *fte;
3462 void *value;
3463 struct format_entry *fe, fe_find;
3464 struct environ_entry *envent;
3465 struct options_entry *o;
3466 int idx;
3467 char *found = NULL, *saved, s[512];
3468 const char *errstr;
3469 time_t t = 0;
3470 struct tm tm;
3472 o = options_parse_get(global_options, key, &idx, 0);
3473 if (o == NULL && ft->wp != NULL)
3474 o = options_parse_get(ft->wp->options, key, &idx, 0);
3475 if (o == NULL && ft->w != NULL)
3476 o = options_parse_get(ft->w->options, key, &idx, 0);
3477 if (o == NULL)
3478 o = options_parse_get(global_w_options, key, &idx, 0);
3479 if (o == NULL && ft->s != NULL)
3480 o = options_parse_get(ft->s->options, key, &idx, 0);
3481 if (o == NULL)
3482 o = options_parse_get(global_s_options, key, &idx, 0);
3483 if (o != NULL) {
3484 found = options_to_string(o, idx, 1);
3485 goto found;
3488 fte = format_table_get(key);
3489 if (fte != NULL) {
3490 value = fte->cb(ft);
3491 if (fte->type == FORMAT_TABLE_TIME && value != NULL)
3492 t = ((struct timeval *)value)->tv_sec;
3493 else
3494 found = value;
3495 goto found;
3497 fe_find.key = (char *)key;
3498 fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find);
3499 if (fe != NULL) {
3500 if (fe->time != 0) {
3501 t = fe->time;
3502 goto found;
3504 if (fe->value == NULL && fe->cb != NULL) {
3505 fe->value = fe->cb(ft);
3506 if (fe->value == NULL)
3507 fe->value = xstrdup("");
3509 found = xstrdup(fe->value);
3510 goto found;
3513 if (~modifiers & FORMAT_TIMESTRING) {
3514 envent = NULL;
3515 if (ft->s != NULL)
3516 envent = environ_find(ft->s->environ, key);
3517 if (envent == NULL)
3518 envent = environ_find(global_environ, key);
3519 if (envent != NULL && envent->value != NULL) {
3520 found = xstrdup(envent->value);
3521 goto found;
3525 return (NULL);
3527 found:
3528 if (modifiers & FORMAT_TIMESTRING) {
3529 if (t == 0 && found != NULL) {
3530 t = strtonum(found, 0, INT64_MAX, &errstr);
3531 if (errstr != NULL)
3532 t = 0;
3533 free(found);
3535 if (t == 0)
3536 return (NULL);
3537 if (modifiers & FORMAT_PRETTY)
3538 found = format_pretty_time(t, 0);
3539 else {
3540 if (time_format != NULL) {
3541 localtime_r(&t, &tm);
3542 strftime(s, sizeof s, time_format, &tm);
3543 } else {
3544 ctime_r(&t, s);
3545 s[strcspn(s, "\n")] = '\0';
3547 found = xstrdup(s);
3549 return (found);
3552 if (t != 0)
3553 xasprintf(&found, "%lld", (long long)t);
3554 else if (found == NULL)
3555 return (NULL);
3556 if (modifiers & FORMAT_BASENAME) {
3557 saved = found;
3558 found = xstrdup(basename(saved));
3559 free(saved);
3561 if (modifiers & FORMAT_DIRNAME) {
3562 saved = found;
3563 found = xstrdup(dirname(saved));
3564 free(saved);
3566 if (modifiers & FORMAT_QUOTE_SHELL) {
3567 saved = found;
3568 found = format_quote_shell(saved);
3569 free(saved);
3571 if (modifiers & FORMAT_QUOTE_STYLE) {
3572 saved = found;
3573 found = format_quote_style(saved);
3574 free(saved);
3576 return (found);
3579 /* Unescape escaped characters. */
3580 static char *
3581 format_unescape(const char *s)
3583 char *out, *cp;
3584 int brackets = 0;
3586 cp = out = xmalloc(strlen(s) + 1);
3587 for (; *s != '\0'; s++) {
3588 if (*s == '#' && s[1] == '{')
3589 brackets++;
3590 if (brackets == 0 &&
3591 *s == '#' &&
3592 strchr(",#{}:", s[1]) != NULL) {
3593 *cp++ = *++s;
3594 continue;
3596 if (*s == '}')
3597 brackets--;
3598 *cp++ = *s;
3600 *cp = '\0';
3601 return (out);
3604 /* Remove escaped characters. */
3605 static char *
3606 format_strip(const char *s)
3608 char *out, *cp;
3609 int brackets = 0;
3611 cp = out = xmalloc(strlen(s) + 1);
3612 for (; *s != '\0'; s++) {
3613 if (*s == '#' && s[1] == '{')
3614 brackets++;
3615 if (*s == '#' && strchr(",#{}:", s[1]) != NULL) {
3616 if (brackets != 0)
3617 *cp++ = *s;
3618 continue;
3620 if (*s == '}')
3621 brackets--;
3622 *cp++ = *s;
3624 *cp = '\0';
3625 return (out);
3628 /* Skip until end. */
3629 const char *
3630 format_skip(const char *s, const char *end)
3632 int brackets = 0;
3634 for (; *s != '\0'; s++) {
3635 if (*s == '#' && s[1] == '{')
3636 brackets++;
3637 if (*s == '#' && strchr(",#{}:", s[1]) != NULL) {
3638 s++;
3639 continue;
3641 if (*s == '}')
3642 brackets--;
3643 if (strchr(end, *s) != NULL && brackets == 0)
3644 break;
3646 if (*s == '\0')
3647 return (NULL);
3648 return (s);
3651 /* Return left and right alternatives separated by commas. */
3652 static int
3653 format_choose(struct format_expand_state *es, const char *s, char **left,
3654 char **right, int expand)
3656 const char *cp;
3657 char *left0, *right0;
3659 cp = format_skip(s, ",");
3660 if (cp == NULL)
3661 return (-1);
3662 left0 = xstrndup(s, cp - s);
3663 right0 = xstrdup(cp + 1);
3665 if (expand) {
3666 *left = format_expand1(es, left0);
3667 free(left0);
3668 *right = format_expand1(es, right0);
3669 free(right0);
3670 } else {
3671 *left = left0;
3672 *right = right0;
3674 return (0);
3677 /* Is this true? */
3679 format_true(const char *s)
3681 if (s != NULL && *s != '\0' && (s[0] != '0' || s[1] != '\0'))
3682 return (1);
3683 return (0);
3686 /* Check if modifier end. */
3687 static int
3688 format_is_end(char c)
3690 return (c == ';' || c == ':');
3693 /* Add to modifier list. */
3694 static void
3695 format_add_modifier(struct format_modifier **list, u_int *count,
3696 const char *c, size_t n, char **argv, int argc)
3698 struct format_modifier *fm;
3700 *list = xreallocarray(*list, (*count) + 1, sizeof **list);
3701 fm = &(*list)[(*count)++];
3703 memcpy(fm->modifier, c, n);
3704 fm->modifier[n] = '\0';
3705 fm->size = n;
3707 fm->argv = argv;
3708 fm->argc = argc;
3711 /* Free modifier list. */
3712 static void
3713 format_free_modifiers(struct format_modifier *list, u_int count)
3715 u_int i;
3717 for (i = 0; i < count; i++)
3718 cmd_free_argv(list[i].argc, list[i].argv);
3719 free(list);
3722 /* Build modifier list. */
3723 static struct format_modifier *
3724 format_build_modifiers(struct format_expand_state *es, const char **s,
3725 u_int *count)
3727 const char *cp = *s, *end;
3728 struct format_modifier *list = NULL;
3729 char c, last[] = "X;:", **argv, *value;
3730 int argc;
3733 * Modifiers are a ; separated list of the forms:
3734 * l,m,C,a,b,c,d,n,t,w,q,E,T,S,W,P,<,>
3735 * =a
3736 * =/a
3737 * =/a/
3738 * s/a/b/
3739 * s/a/b
3740 * ||,&&,!=,==,<=,>=
3743 *count = 0;
3745 while (*cp != '\0' && *cp != ':') {
3746 /* Skip any separator character. */
3747 if (*cp == ';')
3748 cp++;
3750 /* Check single character modifiers with no arguments. */
3751 if (strchr("labcdnwETSWPL<>", cp[0]) != NULL &&
3752 format_is_end(cp[1])) {
3753 format_add_modifier(&list, count, cp, 1, NULL, 0);
3754 cp++;
3755 continue;
3758 /* Then try double character with no arguments. */
3759 if ((memcmp("||", cp, 2) == 0 ||
3760 memcmp("&&", cp, 2) == 0 ||
3761 memcmp("!=", cp, 2) == 0 ||
3762 memcmp("==", cp, 2) == 0 ||
3763 memcmp("<=", cp, 2) == 0 ||
3764 memcmp(">=", cp, 2) == 0) &&
3765 format_is_end(cp[2])) {
3766 format_add_modifier(&list, count, cp, 2, NULL, 0);
3767 cp += 2;
3768 continue;
3771 /* Now try single character with arguments. */
3772 if (strchr("mCNst=peq", cp[0]) == NULL)
3773 break;
3774 c = cp[0];
3776 /* No arguments provided. */
3777 if (format_is_end(cp[1])) {
3778 format_add_modifier(&list, count, cp, 1, NULL, 0);
3779 cp++;
3780 continue;
3782 argv = NULL;
3783 argc = 0;
3785 /* Single argument with no wrapper character. */
3786 if (!ispunct(cp[1]) || cp[1] == '-') {
3787 end = format_skip(cp + 1, ":;");
3788 if (end == NULL)
3789 break;
3791 argv = xcalloc(1, sizeof *argv);
3792 value = xstrndup(cp + 1, end - (cp + 1));
3793 argv[0] = format_expand1(es, value);
3794 free(value);
3795 argc = 1;
3797 format_add_modifier(&list, count, &c, 1, argv, argc);
3798 cp = end;
3799 continue;
3802 /* Multiple arguments with a wrapper character. */
3803 last[0] = cp[1];
3804 cp++;
3805 do {
3806 if (cp[0] == last[0] && format_is_end(cp[1])) {
3807 cp++;
3808 break;
3810 end = format_skip(cp + 1, last);
3811 if (end == NULL)
3812 break;
3813 cp++;
3815 argv = xreallocarray(argv, argc + 1, sizeof *argv);
3816 value = xstrndup(cp, end - cp);
3817 argv[argc++] = format_expand1(es, value);
3818 free(value);
3820 cp = end;
3821 } while (!format_is_end(cp[0]));
3822 format_add_modifier(&list, count, &c, 1, argv, argc);
3824 if (*cp != ':') {
3825 format_free_modifiers(list, *count);
3826 *count = 0;
3827 return (NULL);
3829 *s = cp + 1;
3830 return (list);
3833 /* Match against an fnmatch(3) pattern or regular expression. */
3834 static char *
3835 format_match(struct format_modifier *fm, const char *pattern, const char *text)
3837 const char *s = "";
3838 regex_t r;
3839 int flags = 0;
3841 if (fm->argc >= 1)
3842 s = fm->argv[0];
3843 if (strchr(s, 'r') == NULL) {
3844 if (strchr(s, 'i') != NULL)
3845 flags |= FNM_CASEFOLD;
3846 if (fnmatch(pattern, text, flags) != 0)
3847 return (xstrdup("0"));
3848 } else {
3849 flags = REG_EXTENDED|REG_NOSUB;
3850 if (strchr(s, 'i') != NULL)
3851 flags |= REG_ICASE;
3852 if (regcomp(&r, pattern, flags) != 0)
3853 return (xstrdup("0"));
3854 if (regexec(&r, text, 0, NULL, 0) != 0) {
3855 regfree(&r);
3856 return (xstrdup("0"));
3858 regfree(&r);
3860 return (xstrdup("1"));
3863 /* Perform substitution in string. */
3864 static char *
3865 format_sub(struct format_modifier *fm, const char *text, const char *pattern,
3866 const char *with)
3868 char *value;
3869 int flags = REG_EXTENDED;
3871 if (fm->argc >= 3 && strchr(fm->argv[2], 'i') != NULL)
3872 flags |= REG_ICASE;
3873 value = regsub(pattern, with, text, flags);
3874 if (value == NULL)
3875 return (xstrdup(text));
3876 return (value);
3879 /* Search inside pane. */
3880 static char *
3881 format_search(struct format_modifier *fm, struct window_pane *wp, const char *s)
3883 int ignore = 0, regex = 0;
3884 char *value;
3886 if (fm->argc >= 1) {
3887 if (strchr(fm->argv[0], 'i') != NULL)
3888 ignore = 1;
3889 if (strchr(fm->argv[0], 'r') != NULL)
3890 regex = 1;
3892 xasprintf(&value, "%u", window_pane_search(wp, s, regex, ignore));
3893 return (value);
3896 /* Does session name exist? */
3897 static char *
3898 format_session_name(struct format_expand_state *es, const char *fmt)
3900 char *name;
3901 struct session *s;
3903 name = format_expand1(es, fmt);
3904 RB_FOREACH(s, sessions, &sessions) {
3905 if (strcmp(s->name, name) == 0) {
3906 free(name);
3907 return (xstrdup("1"));
3910 free(name);
3911 return (xstrdup("0"));
3914 /* Loop over sessions. */
3915 static char *
3916 format_loop_sessions(struct format_expand_state *es, const char *fmt)
3918 struct format_tree *ft = es->ft;
3919 struct client *c = ft->client;
3920 struct cmdq_item *item = ft->item;
3921 struct format_tree *nft;
3922 struct format_expand_state next;
3923 char *expanded, *value;
3924 size_t valuelen;
3925 struct session *s;
3927 value = xcalloc(1, 1);
3928 valuelen = 1;
3930 RB_FOREACH(s, sessions, &sessions) {
3931 format_log(es, "session loop: $%u", s->id);
3932 nft = format_create(c, item, FORMAT_NONE, ft->flags);
3933 format_defaults(nft, ft->c, s, NULL, NULL);
3934 format_copy_state(&next, es, 0);
3935 next.ft = nft;
3936 expanded = format_expand1(&next, fmt);
3937 format_free(next.ft);
3939 valuelen += strlen(expanded);
3940 value = xrealloc(value, valuelen);
3942 strlcat(value, expanded, valuelen);
3943 free(expanded);
3946 return (value);
3949 /* Does window name exist? */
3950 static char *
3951 format_window_name(struct format_expand_state *es, const char *fmt)
3953 struct format_tree *ft = es->ft;
3954 char *name;
3955 struct winlink *wl;
3957 if (ft->s == NULL) {
3958 format_log(es, "window name but no session");
3959 return (NULL);
3962 name = format_expand1(es, fmt);
3963 RB_FOREACH(wl, winlinks, &ft->s->windows) {
3964 if (strcmp(wl->window->name, name) == 0) {
3965 free(name);
3966 return (xstrdup("1"));
3969 free(name);
3970 return (xstrdup("0"));
3973 /* Loop over windows. */
3974 static char *
3975 format_loop_windows(struct format_expand_state *es, const char *fmt)
3977 struct format_tree *ft = es->ft;
3978 struct client *c = ft->client;
3979 struct cmdq_item *item = ft->item;
3980 struct format_tree *nft;
3981 struct format_expand_state next;
3982 char *all, *active, *use, *expanded, *value;
3983 size_t valuelen;
3984 struct winlink *wl;
3985 struct window *w;
3987 if (ft->s == NULL) {
3988 format_log(es, "window loop but no session");
3989 return (NULL);
3992 if (format_choose(es, fmt, &all, &active, 0) != 0) {
3993 all = xstrdup(fmt);
3994 active = NULL;
3997 value = xcalloc(1, 1);
3998 valuelen = 1;
4000 RB_FOREACH(wl, winlinks, &ft->s->windows) {
4001 w = wl->window;
4002 format_log(es, "window loop: %u @%u", wl->idx, w->id);
4003 if (active != NULL && wl == ft->s->curw)
4004 use = active;
4005 else
4006 use = all;
4007 nft = format_create(c, item, FORMAT_WINDOW|w->id, ft->flags);
4008 format_defaults(nft, ft->c, ft->s, wl, NULL);
4009 format_copy_state(&next, es, 0);
4010 next.ft = nft;
4011 expanded = format_expand1(&next, use);
4012 format_free(nft);
4014 valuelen += strlen(expanded);
4015 value = xrealloc(value, valuelen);
4017 strlcat(value, expanded, valuelen);
4018 free(expanded);
4021 free(active);
4022 free(all);
4024 return (value);
4027 /* Loop over panes. */
4028 static char *
4029 format_loop_panes(struct format_expand_state *es, const char *fmt)
4031 struct format_tree *ft = es->ft;
4032 struct client *c = ft->client;
4033 struct cmdq_item *item = ft->item;
4034 struct format_tree *nft;
4035 struct format_expand_state next;
4036 char *all, *active, *use, *expanded, *value;
4037 size_t valuelen;
4038 struct window_pane *wp;
4040 if (ft->w == NULL) {
4041 format_log(es, "pane loop but no window");
4042 return (NULL);
4045 if (format_choose(es, fmt, &all, &active, 0) != 0) {
4046 all = xstrdup(fmt);
4047 active = NULL;
4050 value = xcalloc(1, 1);
4051 valuelen = 1;
4053 TAILQ_FOREACH(wp, &ft->w->panes, entry) {
4054 format_log(es, "pane loop: %%%u", wp->id);
4055 if (active != NULL && wp == ft->w->active)
4056 use = active;
4057 else
4058 use = all;
4059 nft = format_create(c, item, FORMAT_PANE|wp->id, ft->flags);
4060 format_defaults(nft, ft->c, ft->s, ft->wl, wp);
4061 format_copy_state(&next, es, 0);
4062 next.ft = nft;
4063 expanded = format_expand1(&next, use);
4064 format_free(nft);
4066 valuelen += strlen(expanded);
4067 value = xrealloc(value, valuelen);
4069 strlcat(value, expanded, valuelen);
4070 free(expanded);
4073 free(active);
4074 free(all);
4076 return (value);
4079 /* Loop over clients. */
4080 static char *
4081 format_loop_clients(struct format_expand_state *es, const char *fmt)
4083 struct format_tree *ft = es->ft;
4084 struct client *c = ft->client;
4085 struct cmdq_item *item = ft->item;
4086 struct format_tree *nft;
4087 struct format_expand_state next;
4088 char *expanded, *value;
4089 size_t valuelen;
4091 value = xcalloc(1, 1);
4092 valuelen = 1;
4094 TAILQ_FOREACH(c, &clients, entry) {
4095 format_log(es, "client loop: %s", c->name);
4096 nft = format_create(c, item, 0, ft->flags);
4097 format_defaults(nft, c, ft->s, ft->wl, ft->wp);
4098 format_copy_state(&next, es, 0);
4099 next.ft = nft;
4100 expanded = format_expand1(&next, fmt);
4101 format_free(nft);
4103 valuelen += strlen(expanded);
4104 value = xrealloc(value, valuelen);
4106 strlcat(value, expanded, valuelen);
4107 free(expanded);
4110 return (value);
4113 static char *
4114 format_replace_expression(struct format_modifier *mexp,
4115 struct format_expand_state *es, const char *copy)
4117 int argc = mexp->argc;
4118 const char *errstr;
4119 char *endch, *value, *left = NULL, *right = NULL;
4120 int use_fp = 0;
4121 u_int prec = 0;
4122 double mleft, mright, result;
4123 enum { ADD,
4124 SUBTRACT,
4125 MULTIPLY,
4126 DIVIDE,
4127 MODULUS,
4128 EQUAL,
4129 NOT_EQUAL,
4130 GREATER_THAN,
4131 GREATER_THAN_EQUAL,
4132 LESS_THAN,
4133 LESS_THAN_EQUAL } operator;
4135 if (strcmp(mexp->argv[0], "+") == 0)
4136 operator = ADD;
4137 else if (strcmp(mexp->argv[0], "-") == 0)
4138 operator = SUBTRACT;
4139 else if (strcmp(mexp->argv[0], "*") == 0)
4140 operator = MULTIPLY;
4141 else if (strcmp(mexp->argv[0], "/") == 0)
4142 operator = DIVIDE;
4143 else if (strcmp(mexp->argv[0], "%") == 0 ||
4144 strcmp(mexp->argv[0], "m") == 0)
4145 operator = MODULUS;
4146 else if (strcmp(mexp->argv[0], "==") == 0)
4147 operator = EQUAL;
4148 else if (strcmp(mexp->argv[0], "!=") == 0)
4149 operator = NOT_EQUAL;
4150 else if (strcmp(mexp->argv[0], ">") == 0)
4151 operator = GREATER_THAN;
4152 else if (strcmp(mexp->argv[0], "<") == 0)
4153 operator = LESS_THAN;
4154 else if (strcmp(mexp->argv[0], ">=") == 0)
4155 operator = GREATER_THAN_EQUAL;
4156 else if (strcmp(mexp->argv[0], "<=") == 0)
4157 operator = LESS_THAN_EQUAL;
4158 else {
4159 format_log(es, "expression has no valid operator: '%s'",
4160 mexp->argv[0]);
4161 goto fail;
4164 /* The second argument may be flags. */
4165 if (argc >= 2 && strchr(mexp->argv[1], 'f') != NULL) {
4166 use_fp = 1;
4167 prec = 2;
4170 /* The third argument may be precision. */
4171 if (argc >= 3) {
4172 prec = strtonum(mexp->argv[2], INT_MIN, INT_MAX, &errstr);
4173 if (errstr != NULL) {
4174 format_log(es, "expression precision %s: %s", errstr,
4175 mexp->argv[2]);
4176 goto fail;
4180 if (format_choose(es, copy, &left, &right, 1) != 0) {
4181 format_log(es, "expression syntax error");
4182 goto fail;
4185 mleft = strtod(left, &endch);
4186 if (*endch != '\0') {
4187 format_log(es, "expression left side is invalid: %s", left);
4188 goto fail;
4191 mright = strtod(right, &endch);
4192 if (*endch != '\0') {
4193 format_log(es, "expression right side is invalid: %s", right);
4194 goto fail;
4197 if (!use_fp) {
4198 mleft = (long long)mleft;
4199 mright = (long long)mright;
4201 format_log(es, "expression left side is: %.*f", prec, mleft);
4202 format_log(es, "expression right side is: %.*f", prec, mright);
4204 switch (operator) {
4205 case ADD:
4206 result = mleft + mright;
4207 break;
4208 case SUBTRACT:
4209 result = mleft - mright;
4210 break;
4211 case MULTIPLY:
4212 result = mleft * mright;
4213 break;
4214 case DIVIDE:
4215 result = mleft / mright;
4216 break;
4217 case MODULUS:
4218 result = fmod(mleft, mright);
4219 break;
4220 case EQUAL:
4221 result = fabs(mleft - mright) < 1e-9;
4222 break;
4223 case NOT_EQUAL:
4224 result = fabs(mleft - mright) > 1e-9;
4225 break;
4226 case GREATER_THAN:
4227 result = (mleft > mright);
4228 break;
4229 case GREATER_THAN_EQUAL:
4230 result = (mleft >= mright);
4231 break;
4232 case LESS_THAN:
4233 result = (mleft < mright);
4234 break;
4235 case LESS_THAN_EQUAL:
4236 result = (mleft <= mright);
4237 break;
4239 if (use_fp)
4240 xasprintf(&value, "%.*f", prec, result);
4241 else
4242 xasprintf(&value, "%.*f", prec, (double)(long long)result);
4243 format_log(es, "expression result is %s", value);
4245 free(right);
4246 free(left);
4247 return (value);
4249 fail:
4250 free(right);
4251 free(left);
4252 return (NULL);
4255 /* Replace a key. */
4256 static int
4257 format_replace(struct format_expand_state *es, const char *key, size_t keylen,
4258 char **buf, size_t *len, size_t *off)
4260 struct format_tree *ft = es->ft;
4261 struct window_pane *wp = ft->wp;
4262 const char *errstr, *copy, *cp, *marker = NULL;
4263 const char *time_format = NULL;
4264 char *copy0, *condition, *found, *new;
4265 char *value, *left, *right;
4266 size_t valuelen;
4267 int modifiers = 0, limit = 0, width = 0;
4268 int j, c;
4269 struct format_modifier *list, *cmp = NULL, *search = NULL;
4270 struct format_modifier **sub = NULL, *mexp = NULL, *fm;
4271 u_int i, count, nsub = 0;
4272 struct format_expand_state next;
4274 /* Make a copy of the key. */
4275 copy = copy0 = xstrndup(key, keylen);
4277 /* Process modifier list. */
4278 list = format_build_modifiers(es, &copy, &count);
4279 for (i = 0; i < count; i++) {
4280 fm = &list[i];
4281 if (format_logging(ft)) {
4282 format_log(es, "modifier %u is %s", i, fm->modifier);
4283 for (j = 0; j < fm->argc; j++) {
4284 format_log(es, "modifier %u argument %d: %s", i,
4285 j, fm->argv[j]);
4288 if (fm->size == 1) {
4289 switch (fm->modifier[0]) {
4290 case 'm':
4291 case '<':
4292 case '>':
4293 cmp = fm;
4294 break;
4295 case 'C':
4296 search = fm;
4297 break;
4298 case 's':
4299 if (fm->argc < 2)
4300 break;
4301 sub = xreallocarray(sub, nsub + 1, sizeof *sub);
4302 sub[nsub++] = fm;
4303 break;
4304 case '=':
4305 if (fm->argc < 1)
4306 break;
4307 limit = strtonum(fm->argv[0], INT_MIN, INT_MAX,
4308 &errstr);
4309 if (errstr != NULL)
4310 limit = 0;
4311 if (fm->argc >= 2 && fm->argv[1] != NULL)
4312 marker = fm->argv[1];
4313 break;
4314 case 'p':
4315 if (fm->argc < 1)
4316 break;
4317 width = strtonum(fm->argv[0], INT_MIN, INT_MAX,
4318 &errstr);
4319 if (errstr != NULL)
4320 width = 0;
4321 break;
4322 case 'w':
4323 modifiers |= FORMAT_WIDTH;
4324 break;
4325 case 'e':
4326 if (fm->argc < 1 || fm->argc > 3)
4327 break;
4328 mexp = fm;
4329 break;
4330 case 'l':
4331 modifiers |= FORMAT_LITERAL;
4332 break;
4333 case 'a':
4334 modifiers |= FORMAT_CHARACTER;
4335 break;
4336 case 'b':
4337 modifiers |= FORMAT_BASENAME;
4338 break;
4339 case 'c':
4340 modifiers |= FORMAT_COLOUR;
4341 break;
4342 case 'd':
4343 modifiers |= FORMAT_DIRNAME;
4344 break;
4345 case 'n':
4346 modifiers |= FORMAT_LENGTH;
4347 break;
4348 case 't':
4349 modifiers |= FORMAT_TIMESTRING;
4350 if (fm->argc < 1)
4351 break;
4352 if (strchr(fm->argv[0], 'p') != NULL)
4353 modifiers |= FORMAT_PRETTY;
4354 else if (fm->argc >= 2 &&
4355 strchr(fm->argv[0], 'f') != NULL)
4356 time_format = format_strip(fm->argv[1]);
4357 break;
4358 case 'q':
4359 if (fm->argc < 1)
4360 modifiers |= FORMAT_QUOTE_SHELL;
4361 else if (strchr(fm->argv[0], 'e') != NULL ||
4362 strchr(fm->argv[0], 'h') != NULL)
4363 modifiers |= FORMAT_QUOTE_STYLE;
4364 break;
4365 case 'E':
4366 modifiers |= FORMAT_EXPAND;
4367 break;
4368 case 'T':
4369 modifiers |= FORMAT_EXPANDTIME;
4370 break;
4371 case 'N':
4372 if (fm->argc < 1 ||
4373 strchr(fm->argv[0], 'w') != NULL)
4374 modifiers |= FORMAT_WINDOW_NAME;
4375 else if (strchr(fm->argv[0], 's') != NULL)
4376 modifiers |= FORMAT_SESSION_NAME;
4377 break;
4378 case 'S':
4379 modifiers |= FORMAT_SESSIONS;
4380 break;
4381 case 'W':
4382 modifiers |= FORMAT_WINDOWS;
4383 break;
4384 case 'P':
4385 modifiers |= FORMAT_PANES;
4386 break;
4387 case 'L':
4388 modifiers |= FORMAT_CLIENTS;
4389 break;
4391 } else if (fm->size == 2) {
4392 if (strcmp(fm->modifier, "||") == 0 ||
4393 strcmp(fm->modifier, "&&") == 0 ||
4394 strcmp(fm->modifier, "==") == 0 ||
4395 strcmp(fm->modifier, "!=") == 0 ||
4396 strcmp(fm->modifier, ">=") == 0 ||
4397 strcmp(fm->modifier, "<=") == 0)
4398 cmp = fm;
4402 /* Is this a literal string? */
4403 if (modifiers & FORMAT_LITERAL) {
4404 format_log(es, "literal string is '%s'", copy);
4405 value = format_unescape(copy);
4406 goto done;
4409 /* Is this a character? */
4410 if (modifiers & FORMAT_CHARACTER) {
4411 new = format_expand1(es, copy);
4412 c = strtonum(new, 32, 126, &errstr);
4413 if (errstr != NULL)
4414 value = xstrdup("");
4415 else
4416 xasprintf(&value, "%c", c);
4417 free(new);
4418 goto done;
4421 /* Is this a colour? */
4422 if (modifiers & FORMAT_COLOUR) {
4423 new = format_expand1(es, copy);
4424 c = colour_fromstring(new);
4425 if (c == -1 || (c = colour_force_rgb(c)) == -1)
4426 value = xstrdup("");
4427 else
4428 xasprintf(&value, "%06x", c & 0xffffff);
4429 free(new);
4430 goto done;
4433 /* Is this a loop, comparison or condition? */
4434 if (modifiers & FORMAT_SESSIONS) {
4435 value = format_loop_sessions(es, copy);
4436 if (value == NULL)
4437 goto fail;
4438 } else if (modifiers & FORMAT_WINDOWS) {
4439 value = format_loop_windows(es, copy);
4440 if (value == NULL)
4441 goto fail;
4442 } else if (modifiers & FORMAT_PANES) {
4443 value = format_loop_panes(es, copy);
4444 if (value == NULL)
4445 goto fail;
4446 } else if (modifiers & FORMAT_CLIENTS) {
4447 value = format_loop_clients(es, copy);
4448 if (value == NULL)
4449 goto fail;
4450 } else if (modifiers & FORMAT_WINDOW_NAME) {
4451 value = format_window_name(es, copy);
4452 if (value == NULL)
4453 goto fail;
4454 } else if (modifiers & FORMAT_SESSION_NAME) {
4455 value = format_session_name(es, copy);
4456 if (value == NULL)
4457 goto fail;
4458 } else if (search != NULL) {
4459 /* Search in pane. */
4460 new = format_expand1(es, copy);
4461 if (wp == NULL) {
4462 format_log(es, "search '%s' but no pane", new);
4463 value = xstrdup("0");
4464 } else {
4465 format_log(es, "search '%s' pane %%%u", new, wp->id);
4466 value = format_search(search, wp, new);
4468 free(new);
4469 } else if (cmp != NULL) {
4470 /* Comparison of left and right. */
4471 if (format_choose(es, copy, &left, &right, 1) != 0) {
4472 format_log(es, "compare %s syntax error: %s",
4473 cmp->modifier, copy);
4474 goto fail;
4476 format_log(es, "compare %s left is: %s", cmp->modifier, left);
4477 format_log(es, "compare %s right is: %s", cmp->modifier, right);
4479 if (strcmp(cmp->modifier, "||") == 0) {
4480 if (format_true(left) || format_true(right))
4481 value = xstrdup("1");
4482 else
4483 value = xstrdup("0");
4484 } else if (strcmp(cmp->modifier, "&&") == 0) {
4485 if (format_true(left) && format_true(right))
4486 value = xstrdup("1");
4487 else
4488 value = xstrdup("0");
4489 } else if (strcmp(cmp->modifier, "==") == 0) {
4490 if (strcmp(left, right) == 0)
4491 value = xstrdup("1");
4492 else
4493 value = xstrdup("0");
4494 } else if (strcmp(cmp->modifier, "!=") == 0) {
4495 if (strcmp(left, right) != 0)
4496 value = xstrdup("1");
4497 else
4498 value = xstrdup("0");
4499 } else if (strcmp(cmp->modifier, "<") == 0) {
4500 if (strcmp(left, right) < 0)
4501 value = xstrdup("1");
4502 else
4503 value = xstrdup("0");
4504 } else if (strcmp(cmp->modifier, ">") == 0) {
4505 if (strcmp(left, right) > 0)
4506 value = xstrdup("1");
4507 else
4508 value = xstrdup("0");
4509 } else if (strcmp(cmp->modifier, "<=") == 0) {
4510 if (strcmp(left, right) <= 0)
4511 value = xstrdup("1");
4512 else
4513 value = xstrdup("0");
4514 } else if (strcmp(cmp->modifier, ">=") == 0) {
4515 if (strcmp(left, right) >= 0)
4516 value = xstrdup("1");
4517 else
4518 value = xstrdup("0");
4519 } else if (strcmp(cmp->modifier, "m") == 0)
4520 value = format_match(cmp, left, right);
4522 free(right);
4523 free(left);
4524 } else if (*copy == '?') {
4525 /* Conditional: check first and choose second or third. */
4526 cp = format_skip(copy + 1, ",");
4527 if (cp == NULL) {
4528 format_log(es, "condition syntax error: %s", copy + 1);
4529 goto fail;
4531 condition = xstrndup(copy + 1, cp - (copy + 1));
4532 format_log(es, "condition is: %s", condition);
4534 found = format_find(ft, condition, modifiers, time_format);
4535 if (found == NULL) {
4537 * If the condition not found, try to expand it. If
4538 * the expansion doesn't have any effect, then assume
4539 * false.
4541 found = format_expand1(es, condition);
4542 if (strcmp(found, condition) == 0) {
4543 free(found);
4544 found = xstrdup("");
4545 format_log(es,
4546 "condition '%s' not found; assuming false",
4547 condition);
4549 } else {
4550 format_log(es, "condition '%s' found: %s", condition,
4551 found);
4554 if (format_choose(es, cp + 1, &left, &right, 0) != 0) {
4555 format_log(es, "condition '%s' syntax error: %s",
4556 condition, cp + 1);
4557 free(found);
4558 goto fail;
4560 if (format_true(found)) {
4561 format_log(es, "condition '%s' is true", condition);
4562 value = format_expand1(es, left);
4563 } else {
4564 format_log(es, "condition '%s' is false", condition);
4565 value = format_expand1(es, right);
4567 free(right);
4568 free(left);
4570 free(condition);
4571 free(found);
4572 } else if (mexp != NULL) {
4573 value = format_replace_expression(mexp, es, copy);
4574 if (value == NULL)
4575 value = xstrdup("");
4576 } else {
4577 if (strstr(copy, "#{") != 0) {
4578 format_log(es, "expanding inner format '%s'", copy);
4579 value = format_expand1(es, copy);
4580 } else {
4581 value = format_find(ft, copy, modifiers, time_format);
4582 if (value == NULL) {
4583 format_log(es, "format '%s' not found", copy);
4584 value = xstrdup("");
4585 } else {
4586 format_log(es, "format '%s' found: %s", copy,
4587 value);
4592 done:
4593 /* Expand again if required. */
4594 if (modifiers & FORMAT_EXPAND) {
4595 new = format_expand1(es, value);
4596 free(value);
4597 value = new;
4598 } else if (modifiers & FORMAT_EXPANDTIME) {
4599 format_copy_state(&next, es, FORMAT_EXPAND_TIME);
4600 new = format_expand1(&next, value);
4601 free(value);
4602 value = new;
4605 /* Perform substitution if any. */
4606 for (i = 0; i < nsub; i++) {
4607 left = format_expand1(es, sub[i]->argv[0]);
4608 right = format_expand1(es, sub[i]->argv[1]);
4609 new = format_sub(sub[i], value, left, right);
4610 format_log(es, "substitute '%s' to '%s': %s", left, right, new);
4611 free(value);
4612 value = new;
4613 free(right);
4614 free(left);
4617 /* Truncate the value if needed. */
4618 if (limit > 0) {
4619 new = format_trim_left(value, limit);
4620 if (marker != NULL && strcmp(new, value) != 0) {
4621 free(value);
4622 xasprintf(&value, "%s%s", new, marker);
4623 } else {
4624 free(value);
4625 value = new;
4627 format_log(es, "applied length limit %d: %s", limit, value);
4628 } else if (limit < 0) {
4629 new = format_trim_right(value, -limit);
4630 if (marker != NULL && strcmp(new, value) != 0) {
4631 free(value);
4632 xasprintf(&value, "%s%s", marker, new);
4633 } else {
4634 free(value);
4635 value = new;
4637 format_log(es, "applied length limit %d: %s", limit, value);
4640 /* Pad the value if needed. */
4641 if (width > 0) {
4642 new = utf8_padcstr(value, width);
4643 free(value);
4644 value = new;
4645 format_log(es, "applied padding width %d: %s", width, value);
4646 } else if (width < 0) {
4647 new = utf8_rpadcstr(value, -width);
4648 free(value);
4649 value = new;
4650 format_log(es, "applied padding width %d: %s", width, value);
4653 /* Replace with the length or width if needed. */
4654 if (modifiers & FORMAT_LENGTH) {
4655 xasprintf(&new, "%zu", strlen(value));
4656 free(value);
4657 value = new;
4658 format_log(es, "replacing with length: %s", new);
4660 if (modifiers & FORMAT_WIDTH) {
4661 xasprintf(&new, "%u", format_width(value));
4662 free(value);
4663 value = new;
4664 format_log(es, "replacing with width: %s", new);
4667 /* Expand the buffer and copy in the value. */
4668 valuelen = strlen(value);
4669 while (*len - *off < valuelen + 1) {
4670 *buf = xreallocarray(*buf, 2, *len);
4671 *len *= 2;
4673 memcpy(*buf + *off, value, valuelen);
4674 *off += valuelen;
4676 format_log(es, "replaced '%s' with '%s'", copy0, value);
4677 free(value);
4679 free(sub);
4680 format_free_modifiers(list, count);
4681 free(copy0);
4682 return (0);
4684 fail:
4685 format_log(es, "failed %s", copy0);
4687 free(sub);
4688 format_free_modifiers(list, count);
4689 free(copy0);
4690 return (-1);
4693 /* Expand keys in a template. */
4694 static char *
4695 format_expand1(struct format_expand_state *es, const char *fmt)
4697 struct format_tree *ft = es->ft;
4698 char *buf, *out, *name;
4699 const char *ptr, *s, *style_end = NULL;
4700 size_t off, len, n, outlen;
4701 int ch, brackets;
4702 char expanded[8192];
4704 if (fmt == NULL || *fmt == '\0')
4705 return (xstrdup(""));
4707 if (es->loop == FORMAT_LOOP_LIMIT) {
4708 format_log(es, "reached loop limit (%u)", FORMAT_LOOP_LIMIT);
4709 return (xstrdup(""));
4711 es->loop++;
4713 format_log(es, "expanding format: %s", fmt);
4715 if ((es->flags & FORMAT_EXPAND_TIME) && strchr(fmt, '%') != NULL) {
4716 if (es->time == 0) {
4717 es->time = time(NULL);
4718 localtime_r(&es->time, &es->tm);
4720 if (strftime(expanded, sizeof expanded, fmt, &es->tm) == 0) {
4721 format_log(es, "format is too long");
4722 return (xstrdup(""));
4724 if (format_logging(ft) && strcmp(expanded, fmt) != 0)
4725 format_log(es, "after time expanded: %s", expanded);
4726 fmt = expanded;
4729 len = 64;
4730 buf = xmalloc(len);
4731 off = 0;
4733 while (*fmt != '\0') {
4734 if (*fmt != '#') {
4735 while (len - off < 2) {
4736 buf = xreallocarray(buf, 2, len);
4737 len *= 2;
4739 buf[off++] = *fmt++;
4740 continue;
4742 fmt++;
4744 ch = (u_char)*fmt++;
4745 switch (ch) {
4746 case '(':
4747 brackets = 1;
4748 for (ptr = fmt; *ptr != '\0'; ptr++) {
4749 if (*ptr == '(')
4750 brackets++;
4751 if (*ptr == ')' && --brackets == 0)
4752 break;
4754 if (*ptr != ')' || brackets != 0)
4755 break;
4756 n = ptr - fmt;
4758 name = xstrndup(fmt, n);
4759 format_log(es, "found #(): %s", name);
4761 if ((ft->flags & FORMAT_NOJOBS) ||
4762 (es->flags & FORMAT_EXPAND_NOJOBS)) {
4763 out = xstrdup("");
4764 format_log(es, "#() is disabled");
4765 } else {
4766 out = format_job_get(es, name);
4767 format_log(es, "#() result: %s", out);
4769 free(name);
4771 outlen = strlen(out);
4772 while (len - off < outlen + 1) {
4773 buf = xreallocarray(buf, 2, len);
4774 len *= 2;
4776 memcpy(buf + off, out, outlen);
4777 off += outlen;
4779 free(out);
4781 fmt += n + 1;
4782 continue;
4783 case '{':
4784 ptr = format_skip((char *)fmt - 2, "}");
4785 if (ptr == NULL)
4786 break;
4787 n = ptr - fmt;
4789 format_log(es, "found #{}: %.*s", (int)n, fmt);
4790 if (format_replace(es, fmt, n, &buf, &len, &off) != 0)
4791 break;
4792 fmt += n + 1;
4793 continue;
4794 case '[':
4795 case '#':
4797 * If ##[ (with two or more #s), then it is a style and
4798 * can be left for format_draw to handle.
4800 ptr = fmt - (ch == '[');
4801 n = 2 - (ch == '[');
4802 while (*ptr == '#') {
4803 ptr++;
4804 n++;
4806 if (*ptr == '[') {
4807 style_end = format_skip(fmt - 2, "]");
4808 format_log(es, "found #*%zu[", n);
4809 while (len - off < n + 2) {
4810 buf = xreallocarray(buf, 2, len);
4811 len *= 2;
4813 memcpy(buf + off, fmt - 2, n + 1);
4814 off += n + 1;
4815 fmt = ptr + 1;
4816 continue;
4818 /* FALLTHROUGH */
4819 case '}':
4820 case ',':
4821 format_log(es, "found #%c", ch);
4822 while (len - off < 2) {
4823 buf = xreallocarray(buf, 2, len);
4824 len *= 2;
4826 buf[off++] = ch;
4827 continue;
4828 default:
4829 s = NULL;
4830 if (fmt > style_end) { /* skip inside #[] */
4831 if (ch >= 'A' && ch <= 'Z')
4832 s = format_upper[ch - 'A'];
4833 else if (ch >= 'a' && ch <= 'z')
4834 s = format_lower[ch - 'a'];
4836 if (s == NULL) {
4837 while (len - off < 3) {
4838 buf = xreallocarray(buf, 2, len);
4839 len *= 2;
4841 buf[off++] = '#';
4842 buf[off++] = ch;
4843 continue;
4845 n = strlen(s);
4846 format_log(es, "found #%c: %s", ch, s);
4847 if (format_replace(es, s, n, &buf, &len, &off) != 0)
4848 break;
4849 continue;
4852 break;
4854 buf[off] = '\0';
4856 format_log(es, "result is: %s", buf);
4857 es->loop--;
4859 return (buf);
4862 /* Expand keys in a template, passing through strftime first. */
4863 char *
4864 format_expand_time(struct format_tree *ft, const char *fmt)
4866 struct format_expand_state es;
4868 memset(&es, 0, sizeof es);
4869 es.ft = ft;
4870 es.flags = FORMAT_EXPAND_TIME;
4871 return (format_expand1(&es, fmt));
4874 /* Expand keys in a template. */
4875 char *
4876 format_expand(struct format_tree *ft, const char *fmt)
4878 struct format_expand_state es;
4880 memset(&es, 0, sizeof es);
4881 es.ft = ft;
4882 es.flags = 0;
4883 return (format_expand1(&es, fmt));
4886 /* Expand a single string. */
4887 char *
4888 format_single(struct cmdq_item *item, const char *fmt, struct client *c,
4889 struct session *s, struct winlink *wl, struct window_pane *wp)
4891 struct format_tree *ft;
4892 char *expanded;
4894 ft = format_create_defaults(item, c, s, wl, wp);
4895 expanded = format_expand(ft, fmt);
4896 format_free(ft);
4897 return (expanded);
4900 /* Expand a single string using state. */
4901 char *
4902 format_single_from_state(struct cmdq_item *item, const char *fmt,
4903 struct client *c, struct cmd_find_state *fs)
4905 return (format_single(item, fmt, c, fs->s, fs->wl, fs->wp));
4908 /* Expand a single string using target. */
4909 char *
4910 format_single_from_target(struct cmdq_item *item, const char *fmt)
4912 struct client *tc = cmdq_get_target_client(item);
4914 return (format_single_from_state(item, fmt, tc, cmdq_get_target(item)));
4917 /* Create and add defaults. */
4918 struct format_tree *
4919 format_create_defaults(struct cmdq_item *item, struct client *c,
4920 struct session *s, struct winlink *wl, struct window_pane *wp)
4922 struct format_tree *ft;
4924 if (item != NULL)
4925 ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0);
4926 else
4927 ft = format_create(NULL, item, FORMAT_NONE, 0);
4928 format_defaults(ft, c, s, wl, wp);
4929 return (ft);
4932 /* Create and add defaults using state. */
4933 struct format_tree *
4934 format_create_from_state(struct cmdq_item *item, struct client *c,
4935 struct cmd_find_state *fs)
4937 return (format_create_defaults(item, c, fs->s, fs->wl, fs->wp));
4940 /* Create and add defaults using target. */
4941 struct format_tree *
4942 format_create_from_target(struct cmdq_item *item)
4944 struct client *tc = cmdq_get_target_client(item);
4946 return (format_create_from_state(item, tc, cmdq_get_target(item)));
4949 /* Set defaults for any of arguments that are not NULL. */
4950 void
4951 format_defaults(struct format_tree *ft, struct client *c, struct session *s,
4952 struct winlink *wl, struct window_pane *wp)
4954 struct paste_buffer *pb;
4956 if (c != NULL && c->name != NULL)
4957 log_debug("%s: c=%s", __func__, c->name);
4958 else
4959 log_debug("%s: c=none", __func__);
4960 if (s != NULL)
4961 log_debug("%s: s=$%u", __func__, s->id);
4962 else
4963 log_debug("%s: s=none", __func__);
4964 if (wl != NULL)
4965 log_debug("%s: wl=%u", __func__, wl->idx);
4966 else
4967 log_debug("%s: wl=none", __func__);
4968 if (wp != NULL)
4969 log_debug("%s: wp=%%%u", __func__, wp->id);
4970 else
4971 log_debug("%s: wp=none", __func__);
4973 if (c != NULL && s != NULL && c->session != s)
4974 log_debug("%s: session does not match", __func__);
4976 if (wp != NULL)
4977 ft->type = FORMAT_TYPE_PANE;
4978 else if (wl != NULL)
4979 ft->type = FORMAT_TYPE_WINDOW;
4980 else if (s != NULL)
4981 ft->type = FORMAT_TYPE_SESSION;
4982 else
4983 ft->type = FORMAT_TYPE_UNKNOWN;
4985 if (s == NULL && c != NULL)
4986 s = c->session;
4987 if (wl == NULL && s != NULL)
4988 wl = s->curw;
4989 if (wp == NULL && wl != NULL)
4990 wp = wl->window->active;
4992 if (c != NULL)
4993 format_defaults_client(ft, c);
4994 if (s != NULL)
4995 format_defaults_session(ft, s);
4996 if (wl != NULL)
4997 format_defaults_winlink(ft, wl);
4998 if (wp != NULL)
4999 format_defaults_pane(ft, wp);
5001 pb = paste_get_top(NULL);
5002 if (pb != NULL)
5003 format_defaults_paste_buffer(ft, pb);
5006 /* Set default format keys for a session. */
5007 static void
5008 format_defaults_session(struct format_tree *ft, struct session *s)
5010 ft->s = s;
5013 /* Set default format keys for a client. */
5014 static void
5015 format_defaults_client(struct format_tree *ft, struct client *c)
5017 if (ft->s == NULL)
5018 ft->s = c->session;
5019 ft->c = c;
5022 /* Set default format keys for a window. */
5023 void
5024 format_defaults_window(struct format_tree *ft, struct window *w)
5026 ft->w = w;
5029 /* Set default format keys for a winlink. */
5030 static void
5031 format_defaults_winlink(struct format_tree *ft, struct winlink *wl)
5033 if (ft->w == NULL)
5034 format_defaults_window(ft, wl->window);
5035 ft->wl = wl;
5038 /* Set default format keys for a window pane. */
5039 void
5040 format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
5042 struct window_mode_entry *wme;
5044 if (ft->w == NULL)
5045 format_defaults_window(ft, wp->window);
5046 ft->wp = wp;
5048 wme = TAILQ_FIRST(&wp->modes);
5049 if (wme != NULL && wme->mode->formats != NULL)
5050 wme->mode->formats(wme, ft);
5053 /* Set default format keys for paste buffer. */
5054 void
5055 format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
5057 ft->pb = pb;
5060 /* Return word at given coordinates. Caller frees. */
5061 char *
5062 format_grid_word(struct grid *gd, u_int x, u_int y)
5064 const struct grid_line *gl;
5065 struct grid_cell gc;
5066 const char *ws;
5067 struct utf8_data *ud = NULL;
5068 u_int end;
5069 size_t size = 0;
5070 int found = 0;
5071 char *s = NULL;
5073 ws = options_get_string(global_s_options, "word-separators");
5075 for (;;) {
5076 grid_get_cell(gd, x, y, &gc);
5077 if (gc.flags & GRID_FLAG_PADDING)
5078 break;
5079 if (utf8_cstrhas(ws, &gc.data) ||
5080 (gc.data.size == 1 && *gc.data.data == ' ')) {
5081 found = 1;
5082 break;
5085 if (x == 0) {
5086 if (y == 0)
5087 break;
5088 gl = grid_peek_line(gd, y - 1);
5089 if (~gl->flags & GRID_LINE_WRAPPED)
5090 break;
5091 y--;
5092 x = grid_line_length(gd, y);
5093 if (x == 0)
5094 break;
5096 x--;
5098 for (;;) {
5099 if (found) {
5100 end = grid_line_length(gd, y);
5101 if (end == 0 || x == end - 1) {
5102 if (y == gd->hsize + gd->sy - 1)
5103 break;
5104 gl = grid_peek_line(gd, y);
5105 if (~gl->flags & GRID_LINE_WRAPPED)
5106 break;
5107 y++;
5108 x = 0;
5109 } else
5110 x++;
5112 found = 1;
5114 grid_get_cell(gd, x, y, &gc);
5115 if (gc.flags & GRID_FLAG_PADDING)
5116 break;
5117 if (utf8_cstrhas(ws, &gc.data) ||
5118 (gc.data.size == 1 && *gc.data.data == ' '))
5119 break;
5121 ud = xreallocarray(ud, size + 2, sizeof *ud);
5122 memcpy(&ud[size++], &gc.data, sizeof *ud);
5124 if (size != 0) {
5125 ud[size].size = 0;
5126 s = utf8_tocstr(ud);
5127 free(ud);
5129 return (s);
5132 /* Return line at given coordinates. Caller frees. */
5133 char *
5134 format_grid_line(struct grid *gd, u_int y)
5136 struct grid_cell gc;
5137 struct utf8_data *ud = NULL;
5138 u_int x;
5139 size_t size = 0;
5140 char *s = NULL;
5142 for (x = 0; x < grid_line_length(gd, y); x++) {
5143 grid_get_cell(gd, x, y, &gc);
5144 if (gc.flags & GRID_FLAG_PADDING)
5145 break;
5147 ud = xreallocarray(ud, size + 2, sizeof *ud);
5148 memcpy(&ud[size++], &gc.data, sizeof *ud);
5150 if (size != 0) {
5151 ud[size].size = 0;
5152 s = utf8_tocstr(ud);
5153 free(ud);
5155 return (s);
5158 /* Return hyperlink at given coordinates. Caller frees. */
5159 char *
5160 format_grid_hyperlink(struct grid *gd, u_int x, u_int y, struct screen* s)
5162 const char *uri;
5163 struct grid_cell gc;
5165 grid_get_cell(gd, x, y, &gc);
5166 if (gc.flags & GRID_FLAG_PADDING)
5167 return (NULL);
5168 if (s->hyperlinks == NULL || gc.link == 0)
5169 return (NULL);
5170 if (!hyperlinks_get(s->hyperlinks, gc.link, &uri, NULL, NULL))
5171 return (NULL);
5172 return (xstrdup(uri));