Add a flag to display-menu to select the manu item chosen first, GitHub
[tmux.git] / format.c
blob5b08a7a4ff8864e24383d3c912267453b7d29b10
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
107 /* Limit on recursion. */
108 #define FORMAT_LOOP_LIMIT 100
110 /* Format expand flags. */
111 #define FORMAT_EXPAND_TIME 0x1
112 #define FORMAT_EXPAND_NOJOBS 0x2
114 /* Entry in format tree. */
115 struct format_entry {
116 char *key;
117 char *value;
118 time_t time;
119 format_cb cb;
120 RB_ENTRY(format_entry) entry;
123 /* Format type. */
124 enum format_type {
125 FORMAT_TYPE_UNKNOWN,
126 FORMAT_TYPE_SESSION,
127 FORMAT_TYPE_WINDOW,
128 FORMAT_TYPE_PANE
131 struct format_tree {
132 enum format_type type;
134 struct client *c;
135 struct session *s;
136 struct winlink *wl;
137 struct window *w;
138 struct window_pane *wp;
139 struct paste_buffer *pb;
141 struct cmdq_item *item;
142 struct client *client;
143 int flags;
144 u_int tag;
146 struct mouse_event m;
148 RB_HEAD(format_entry_tree, format_entry) tree;
150 static int format_entry_cmp(struct format_entry *, struct format_entry *);
151 RB_GENERATE_STATIC(format_entry_tree, format_entry, entry, format_entry_cmp);
153 /* Format expand state. */
154 struct format_expand_state {
155 struct format_tree *ft;
156 u_int loop;
157 time_t time;
158 struct tm tm;
159 int flags;
162 /* Format modifier. */
163 struct format_modifier {
164 char modifier[3];
165 u_int size;
167 char **argv;
168 int argc;
171 /* Format entry tree comparison function. */
172 static int
173 format_entry_cmp(struct format_entry *fe1, struct format_entry *fe2)
175 return (strcmp(fe1->key, fe2->key));
178 /* Single-character uppercase aliases. */
179 static const char *format_upper[] = {
180 NULL, /* A */
181 NULL, /* B */
182 NULL, /* C */
183 "pane_id", /* D */
184 NULL, /* E */
185 "window_flags", /* F */
186 NULL, /* G */
187 "host", /* H */
188 "window_index", /* I */
189 NULL, /* J */
190 NULL, /* K */
191 NULL, /* L */
192 NULL, /* M */
193 NULL, /* N */
194 NULL, /* O */
195 "pane_index", /* P */
196 NULL, /* Q */
197 NULL, /* R */
198 "session_name", /* S */
199 "pane_title", /* T */
200 NULL, /* U */
201 NULL, /* V */
202 "window_name", /* W */
203 NULL, /* X */
204 NULL, /* Y */
205 NULL /* Z */
208 /* Single-character lowercase aliases. */
209 static const char *format_lower[] = {
210 NULL, /* a */
211 NULL, /* b */
212 NULL, /* c */
213 NULL, /* d */
214 NULL, /* e */
215 NULL, /* f */
216 NULL, /* g */
217 "host_short", /* h */
218 NULL, /* i */
219 NULL, /* j */
220 NULL, /* k */
221 NULL, /* l */
222 NULL, /* m */
223 NULL, /* n */
224 NULL, /* o */
225 NULL, /* p */
226 NULL, /* q */
227 NULL, /* r */
228 NULL, /* s */
229 NULL, /* t */
230 NULL, /* u */
231 NULL, /* v */
232 NULL, /* w */
233 NULL, /* x */
234 NULL, /* y */
235 NULL /* z */
238 /* Is logging enabled? */
239 static inline int
240 format_logging(struct format_tree *ft)
242 return (log_get_level() != 0 || (ft->flags & FORMAT_VERBOSE));
245 /* Log a message if verbose. */
246 static void printflike(3, 4)
247 format_log1(struct format_expand_state *es, const char *from, const char *fmt,
248 ...)
250 struct format_tree *ft = es->ft;
251 va_list ap;
252 char *s;
253 static const char spaces[] = " ";
255 if (!format_logging(ft))
256 return;
258 va_start(ap, fmt);
259 xvasprintf(&s, fmt, ap);
260 va_end(ap);
262 log_debug("%s: %s", from, s);
263 if (ft->item != NULL && (ft->flags & FORMAT_VERBOSE))
264 cmdq_print(ft->item, "#%.*s%s", es->loop, spaces, s);
266 free(s);
268 #define format_log(es, fmt, ...) format_log1(es, __func__, fmt, ##__VA_ARGS__)
270 /* Copy expand state. */
271 static void
272 format_copy_state(struct format_expand_state *to,
273 struct format_expand_state *from, int flags)
275 to->ft = from->ft;
276 to->loop = from->loop;
277 to->time = from->time;
278 memcpy(&to->tm, &from->tm, sizeof to->tm);
279 to->flags = from->flags|flags;
282 /* Format job update callback. */
283 static void
284 format_job_update(struct job *job)
286 struct format_job *fj = job_get_data(job);
287 struct evbuffer *evb = job_get_event(job)->input;
288 char *line = NULL, *next;
289 time_t t;
291 while ((next = evbuffer_readline(evb)) != NULL) {
292 free(line);
293 line = next;
295 if (line == NULL)
296 return;
297 fj->updated = 1;
299 free(fj->out);
300 fj->out = line;
302 log_debug("%s: %p %s: %s", __func__, fj, fj->cmd, fj->out);
304 t = time(NULL);
305 if (fj->status && fj->last != t) {
306 if (fj->client != NULL)
307 server_status_client(fj->client);
308 fj->last = t;
312 /* Format job complete callback. */
313 static void
314 format_job_complete(struct job *job)
316 struct format_job *fj = job_get_data(job);
317 struct evbuffer *evb = job_get_event(job)->input;
318 char *line, *buf;
319 size_t len;
321 fj->job = NULL;
323 buf = NULL;
324 if ((line = evbuffer_readline(evb)) == NULL) {
325 len = EVBUFFER_LENGTH(evb);
326 buf = xmalloc(len + 1);
327 if (len != 0)
328 memcpy(buf, EVBUFFER_DATA(evb), len);
329 buf[len] = '\0';
330 } else
331 buf = line;
333 log_debug("%s: %p %s: %s", __func__, fj, fj->cmd, buf);
335 if (*buf != '\0' || !fj->updated) {
336 free(fj->out);
337 fj->out = buf;
338 } else
339 free(buf);
341 if (fj->status) {
342 if (fj->client != NULL)
343 server_status_client(fj->client);
344 fj->status = 0;
348 /* Find a job. */
349 static char *
350 format_job_get(struct format_expand_state *es, const char *cmd)
352 struct format_tree *ft = es->ft;
353 struct format_job_tree *jobs;
354 struct format_job fj0, *fj;
355 time_t t;
356 char *expanded;
357 int force;
358 struct format_expand_state next;
360 if (ft->client == NULL)
361 jobs = &format_jobs;
362 else if (ft->client->jobs != NULL)
363 jobs = ft->client->jobs;
364 else {
365 jobs = ft->client->jobs = xmalloc(sizeof *ft->client->jobs);
366 RB_INIT(jobs);
369 fj0.tag = ft->tag;
370 fj0.cmd = cmd;
371 if ((fj = RB_FIND(format_job_tree, jobs, &fj0)) == NULL) {
372 fj = xcalloc(1, sizeof *fj);
373 fj->client = ft->client;
374 fj->tag = ft->tag;
375 fj->cmd = xstrdup(cmd);
377 RB_INSERT(format_job_tree, jobs, fj);
380 format_copy_state(&next, es, FORMAT_EXPAND_NOJOBS);
381 next.flags &= ~FORMAT_EXPAND_TIME;
383 expanded = format_expand1(&next, cmd);
384 if (fj->expanded == NULL || strcmp(expanded, fj->expanded) != 0) {
385 free((void *)fj->expanded);
386 fj->expanded = xstrdup(expanded);
387 force = 1;
388 } else
389 force = (ft->flags & FORMAT_FORCE);
391 t = time(NULL);
392 if (force && fj->job != NULL)
393 job_free(fj->job);
394 if (force || (fj->job == NULL && fj->last != t)) {
395 fj->job = job_run(expanded, 0, NULL, NULL, NULL,
396 server_client_get_cwd(ft->client, NULL), format_job_update,
397 format_job_complete, NULL, fj, JOB_NOWAIT, -1, -1);
398 if (fj->job == NULL) {
399 free(fj->out);
400 xasprintf(&fj->out, "<'%s' didn't start>", fj->cmd);
402 fj->last = t;
403 fj->updated = 0;
404 } else if (fj->job != NULL && (t - fj->last) > 1 && fj->out == NULL)
405 xasprintf(&fj->out, "<'%s' not ready>", fj->cmd);
406 free(expanded);
408 if (ft->flags & FORMAT_STATUS)
409 fj->status = 1;
410 if (fj->out == NULL)
411 return (xstrdup(""));
412 return (format_expand1(&next, fj->out));
415 /* Remove old jobs. */
416 static void
417 format_job_tidy(struct format_job_tree *jobs, int force)
419 struct format_job *fj, *fj1;
420 time_t now;
422 now = time(NULL);
423 RB_FOREACH_SAFE(fj, format_job_tree, jobs, fj1) {
424 if (!force && (fj->last > now || now - fj->last < 3600))
425 continue;
426 RB_REMOVE(format_job_tree, jobs, fj);
428 log_debug("%s: %s", __func__, fj->cmd);
430 if (fj->job != NULL)
431 job_free(fj->job);
433 free((void *)fj->expanded);
434 free((void *)fj->cmd);
435 free(fj->out);
437 free(fj);
441 /* Tidy old jobs for all clients. */
442 void
443 format_tidy_jobs(void)
445 struct client *c;
447 format_job_tidy(&format_jobs, 0);
448 TAILQ_FOREACH(c, &clients, entry) {
449 if (c->jobs != NULL)
450 format_job_tidy(c->jobs, 0);
454 /* Remove old jobs for client. */
455 void
456 format_lost_client(struct client *c)
458 if (c->jobs != NULL)
459 format_job_tidy(c->jobs, 1);
460 free(c->jobs);
463 /* Wrapper for asprintf. */
464 static char * printflike(1, 2)
465 format_printf(const char *fmt, ...)
467 va_list ap;
468 char *s;
470 va_start(ap, fmt);
471 xvasprintf(&s, fmt, ap);
472 va_end(ap);
473 return (s);
476 /* Callback for host. */
477 static void *
478 format_cb_host(__unused struct format_tree *ft)
480 char host[HOST_NAME_MAX + 1];
482 if (gethostname(host, sizeof host) != 0)
483 return (xstrdup(""));
484 return (xstrdup(host));
487 /* Callback for host_short. */
488 static void *
489 format_cb_host_short(__unused struct format_tree *ft)
491 char host[HOST_NAME_MAX + 1], *cp;
493 if (gethostname(host, sizeof host) != 0)
494 return (xstrdup(""));
495 if ((cp = strchr(host, '.')) != NULL)
496 *cp = '\0';
497 return (xstrdup(host));
500 /* Callback for pid. */
501 static void *
502 format_cb_pid(__unused struct format_tree *ft)
504 char *value;
506 xasprintf(&value, "%ld", (long)getpid());
507 return (value);
510 /* Callback for session_attached_list. */
511 static void *
512 format_cb_session_attached_list(struct format_tree *ft)
514 struct session *s = ft->s;
515 struct client *loop;
516 struct evbuffer *buffer;
517 int size;
518 char *value = NULL;
520 if (s == NULL)
521 return (NULL);
523 buffer = evbuffer_new();
524 if (buffer == NULL)
525 fatalx("out of memory");
527 TAILQ_FOREACH(loop, &clients, entry) {
528 if (loop->session == s) {
529 if (EVBUFFER_LENGTH(buffer) > 0)
530 evbuffer_add(buffer, ",", 1);
531 evbuffer_add_printf(buffer, "%s", loop->name);
535 if ((size = EVBUFFER_LENGTH(buffer)) != 0)
536 xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
537 evbuffer_free(buffer);
538 return (value);
541 /* Callback for session_alerts. */
542 static void *
543 format_cb_session_alerts(struct format_tree *ft)
545 struct session *s = ft->s;
546 struct winlink *wl;
547 char alerts[1024], tmp[16];
549 if (s == NULL)
550 return (NULL);
552 *alerts = '\0';
553 RB_FOREACH(wl, winlinks, &s->windows) {
554 if ((wl->flags & WINLINK_ALERTFLAGS) == 0)
555 continue;
556 xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
558 if (*alerts != '\0')
559 strlcat(alerts, ",", sizeof alerts);
560 strlcat(alerts, tmp, sizeof alerts);
561 if (wl->flags & WINLINK_ACTIVITY)
562 strlcat(alerts, "#", sizeof alerts);
563 if (wl->flags & WINLINK_BELL)
564 strlcat(alerts, "!", sizeof alerts);
565 if (wl->flags & WINLINK_SILENCE)
566 strlcat(alerts, "~", sizeof alerts);
568 return (xstrdup(alerts));
571 /* Callback for session_stack. */
572 static void *
573 format_cb_session_stack(struct format_tree *ft)
575 struct session *s = ft->s;
576 struct winlink *wl;
577 char result[1024], tmp[16];
579 if (s == NULL)
580 return (NULL);
582 xsnprintf(result, sizeof result, "%u", s->curw->idx);
583 TAILQ_FOREACH(wl, &s->lastw, sentry) {
584 xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
586 if (*result != '\0')
587 strlcat(result, ",", sizeof result);
588 strlcat(result, tmp, sizeof result);
590 return (xstrdup(result));
593 /* Callback for window_stack_index. */
594 static void *
595 format_cb_window_stack_index(struct format_tree *ft)
597 struct session *s;
598 struct winlink *wl;
599 u_int idx;
600 char *value = NULL;
602 if (ft->wl == NULL)
603 return (NULL);
604 s = ft->wl->session;
606 idx = 0;
607 TAILQ_FOREACH(wl, &s->lastw, sentry) {
608 idx++;
609 if (wl == ft->wl)
610 break;
612 if (wl == NULL)
613 return (xstrdup("0"));
614 xasprintf(&value, "%u", idx);
615 return (value);
618 /* Callback for window_linked_sessions_list. */
619 static void *
620 format_cb_window_linked_sessions_list(struct format_tree *ft)
622 struct window *w;
623 struct winlink *wl;
624 struct evbuffer *buffer;
625 int size;
626 char *value = NULL;
628 if (ft->wl == NULL)
629 return (NULL);
630 w = ft->wl->window;
632 buffer = evbuffer_new();
633 if (buffer == NULL)
634 fatalx("out of memory");
636 TAILQ_FOREACH(wl, &w->winlinks, wentry) {
637 if (EVBUFFER_LENGTH(buffer) > 0)
638 evbuffer_add(buffer, ",", 1);
639 evbuffer_add_printf(buffer, "%s", wl->session->name);
642 if ((size = EVBUFFER_LENGTH(buffer)) != 0)
643 xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
644 evbuffer_free(buffer);
645 return (value);
648 /* Callback for window_active_sessions. */
649 static void *
650 format_cb_window_active_sessions(struct format_tree *ft)
652 struct window *w;
653 struct winlink *wl;
654 u_int n = 0;
655 char *value;
657 if (ft->wl == NULL)
658 return (NULL);
659 w = ft->wl->window;
661 TAILQ_FOREACH(wl, &w->winlinks, wentry) {
662 if (wl->session->curw == wl)
663 n++;
666 xasprintf(&value, "%u", n);
667 return (value);
670 /* Callback for window_active_sessions_list. */
671 static void *
672 format_cb_window_active_sessions_list(struct format_tree *ft)
674 struct window *w;
675 struct winlink *wl;
676 struct evbuffer *buffer;
677 int size;
678 char *value = NULL;
680 if (ft->wl == NULL)
681 return (NULL);
682 w = ft->wl->window;
684 buffer = evbuffer_new();
685 if (buffer == NULL)
686 fatalx("out of memory");
688 TAILQ_FOREACH(wl, &w->winlinks, wentry) {
689 if (wl->session->curw == wl) {
690 if (EVBUFFER_LENGTH(buffer) > 0)
691 evbuffer_add(buffer, ",", 1);
692 evbuffer_add_printf(buffer, "%s", wl->session->name);
696 if ((size = EVBUFFER_LENGTH(buffer)) != 0)
697 xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
698 evbuffer_free(buffer);
699 return (value);
702 /* Callback for window_active_clients. */
703 static void *
704 format_cb_window_active_clients(struct format_tree *ft)
706 struct window *w;
707 struct client *loop;
708 struct session *client_session;
709 u_int n = 0;
710 char *value;
712 if (ft->wl == NULL)
713 return (NULL);
714 w = ft->wl->window;
716 TAILQ_FOREACH(loop, &clients, entry) {
717 client_session = loop->session;
718 if (client_session == NULL)
719 continue;
721 if (w == client_session->curw->window)
722 n++;
725 xasprintf(&value, "%u", n);
726 return (value);
729 /* Callback for window_active_clients_list. */
730 static void *
731 format_cb_window_active_clients_list(struct format_tree *ft)
733 struct window *w;
734 struct client *loop;
735 struct session *client_session;
736 struct evbuffer *buffer;
737 int size;
738 char *value = NULL;
740 if (ft->wl == NULL)
741 return (NULL);
742 w = ft->wl->window;
744 buffer = evbuffer_new();
745 if (buffer == NULL)
746 fatalx("out of memory");
748 TAILQ_FOREACH(loop, &clients, entry) {
749 client_session = loop->session;
750 if (client_session == NULL)
751 continue;
753 if (w == client_session->curw->window) {
754 if (EVBUFFER_LENGTH(buffer) > 0)
755 evbuffer_add(buffer, ",", 1);
756 evbuffer_add_printf(buffer, "%s", loop->name);
760 if ((size = EVBUFFER_LENGTH(buffer)) != 0)
761 xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
762 evbuffer_free(buffer);
763 return (value);
766 /* Callback for window_layout. */
767 static void *
768 format_cb_window_layout(struct format_tree *ft)
770 struct window *w = ft->w;
772 if (w == NULL)
773 return (NULL);
775 if (w->saved_layout_root != NULL)
776 return (layout_dump(w->saved_layout_root));
777 return (layout_dump(w->layout_root));
780 /* Callback for window_visible_layout. */
781 static void *
782 format_cb_window_visible_layout(struct format_tree *ft)
784 struct window *w = ft->w;
786 if (w == NULL)
787 return (NULL);
789 return (layout_dump(w->layout_root));
792 /* Callback for pane_start_command. */
793 static void *
794 format_cb_start_command(struct format_tree *ft)
796 struct window_pane *wp = ft->wp;
798 if (wp == NULL)
799 return (NULL);
801 return (cmd_stringify_argv(wp->argc, wp->argv));
804 /* Callback for pane_start_path. */
805 static void *
806 format_cb_start_path(struct format_tree *ft)
808 struct window_pane *wp = ft->wp;
810 if (wp == NULL)
811 return (NULL);
813 if (wp->cwd == NULL)
814 return (xstrdup(""));
815 return (xstrdup(wp->cwd));
818 /* Callback for pane_current_command. */
819 static void *
820 format_cb_current_command(struct format_tree *ft)
822 struct window_pane *wp = ft->wp;
823 char *cmd, *value;
825 if (wp == NULL || wp->shell == NULL)
826 return (NULL);
828 cmd = get_proc_name(wp->fd, wp->tty);
829 if (cmd == NULL || *cmd == '\0') {
830 free(cmd);
831 cmd = cmd_stringify_argv(wp->argc, wp->argv);
832 if (cmd == NULL || *cmd == '\0') {
833 free(cmd);
834 cmd = xstrdup(wp->shell);
837 value = parse_window_name(cmd);
838 free(cmd);
839 return (value);
842 /* Callback for pane_current_path. */
843 static void *
844 format_cb_current_path(struct format_tree *ft)
846 struct window_pane *wp = ft->wp;
847 char *cwd;
849 if (wp == NULL)
850 return (NULL);
852 cwd = get_proc_cwd(wp->fd);
853 if (cwd == NULL)
854 return (NULL);
855 return (xstrdup(cwd));
858 /* Callback for history_bytes. */
859 static void *
860 format_cb_history_bytes(struct format_tree *ft)
862 struct window_pane *wp = ft->wp;
863 struct grid *gd;
864 struct grid_line *gl;
865 size_t size = 0;
866 u_int i;
867 char *value;
869 if (wp == NULL)
870 return (NULL);
871 gd = wp->base.grid;
873 for (i = 0; i < gd->hsize + gd->sy; i++) {
874 gl = grid_get_line(gd, i);
875 size += gl->cellsize * sizeof *gl->celldata;
876 size += gl->extdsize * sizeof *gl->extddata;
878 size += (gd->hsize + gd->sy) * sizeof *gl;
880 xasprintf(&value, "%zu", size);
881 return (value);
884 /* Callback for history_all_bytes. */
885 static void *
886 format_cb_history_all_bytes(struct format_tree *ft)
888 struct window_pane *wp = ft->wp;
889 struct grid *gd;
890 struct grid_line *gl;
891 u_int i, lines, cells = 0, extended_cells = 0;
892 char *value;
894 if (wp == NULL)
895 return (NULL);
896 gd = wp->base.grid;
898 lines = gd->hsize + gd->sy;
899 for (i = 0; i < lines; i++) {
900 gl = grid_get_line(gd, i);
901 cells += gl->cellsize;
902 extended_cells += gl->extdsize;
905 xasprintf(&value, "%u,%zu,%u,%zu,%u,%zu", lines,
906 lines * sizeof *gl, cells, cells * sizeof *gl->celldata,
907 extended_cells, extended_cells * sizeof *gl->extddata);
908 return (value);
911 /* Callback for pane_tabs. */
912 static void *
913 format_cb_pane_tabs(struct format_tree *ft)
915 struct window_pane *wp = ft->wp;
916 struct evbuffer *buffer;
917 u_int i;
918 int size;
919 char *value = NULL;
921 if (wp == NULL)
922 return (NULL);
924 buffer = evbuffer_new();
925 if (buffer == NULL)
926 fatalx("out of memory");
927 for (i = 0; i < wp->base.grid->sx; i++) {
928 if (!bit_test(wp->base.tabs, i))
929 continue;
931 if (EVBUFFER_LENGTH(buffer) > 0)
932 evbuffer_add(buffer, ",", 1);
933 evbuffer_add_printf(buffer, "%u", i);
935 if ((size = EVBUFFER_LENGTH(buffer)) != 0)
936 xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
937 evbuffer_free(buffer);
938 return (value);
941 /* Callback for pane_fg. */
942 static void *
943 format_cb_pane_fg(struct format_tree *ft)
945 struct window_pane *wp = ft->wp;
946 struct grid_cell gc;
948 if (wp == NULL)
949 return (NULL);
951 tty_default_colours(&gc, wp);
952 return (xstrdup(colour_tostring(gc.fg)));
955 /* Callback for pane_bg. */
956 static void *
957 format_cb_pane_bg(struct format_tree *ft)
959 struct window_pane *wp = ft->wp;
960 struct grid_cell gc;
962 if (wp == NULL)
963 return (NULL);
965 tty_default_colours(&gc, wp);
966 return (xstrdup(colour_tostring(gc.bg)));
969 /* Callback for session_group_list. */
970 static void *
971 format_cb_session_group_list(struct format_tree *ft)
973 struct session *s = ft->s;
974 struct session_group *sg;
975 struct session *loop;
976 struct evbuffer *buffer;
977 int size;
978 char *value = NULL;
980 if (s == NULL)
981 return (NULL);
982 sg = session_group_contains(s);
983 if (sg == NULL)
984 return (NULL);
986 buffer = evbuffer_new();
987 if (buffer == NULL)
988 fatalx("out of memory");
990 TAILQ_FOREACH(loop, &sg->sessions, gentry) {
991 if (EVBUFFER_LENGTH(buffer) > 0)
992 evbuffer_add(buffer, ",", 1);
993 evbuffer_add_printf(buffer, "%s", loop->name);
996 if ((size = EVBUFFER_LENGTH(buffer)) != 0)
997 xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
998 evbuffer_free(buffer);
999 return (value);
1002 /* Callback for session_group_attached_list. */
1003 static void *
1004 format_cb_session_group_attached_list(struct format_tree *ft)
1006 struct session *s = ft->s, *client_session, *session_loop;
1007 struct session_group *sg;
1008 struct client *loop;
1009 struct evbuffer *buffer;
1010 int size;
1011 char *value = NULL;
1013 if (s == NULL)
1014 return (NULL);
1015 sg = session_group_contains(s);
1016 if (sg == NULL)
1017 return (NULL);
1019 buffer = evbuffer_new();
1020 if (buffer == NULL)
1021 fatalx("out of memory");
1023 TAILQ_FOREACH(loop, &clients, entry) {
1024 client_session = loop->session;
1025 if (client_session == NULL)
1026 continue;
1027 TAILQ_FOREACH(session_loop, &sg->sessions, gentry) {
1028 if (session_loop == client_session){
1029 if (EVBUFFER_LENGTH(buffer) > 0)
1030 evbuffer_add(buffer, ",", 1);
1031 evbuffer_add_printf(buffer, "%s", loop->name);
1036 if ((size = EVBUFFER_LENGTH(buffer)) != 0)
1037 xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
1038 evbuffer_free(buffer);
1039 return (value);
1042 /* Callback for pane_in_mode. */
1043 static void *
1044 format_cb_pane_in_mode(struct format_tree *ft)
1046 struct window_pane *wp = ft->wp;
1047 u_int n = 0;
1048 struct window_mode_entry *wme;
1049 char *value;
1051 if (wp == NULL)
1052 return (NULL);
1054 TAILQ_FOREACH(wme, &wp->modes, entry)
1055 n++;
1056 xasprintf(&value, "%u", n);
1057 return (value);
1060 /* Callback for pane_at_top. */
1061 static void *
1062 format_cb_pane_at_top(struct format_tree *ft)
1064 struct window_pane *wp = ft->wp;
1065 struct window *w;
1066 int status, flag;
1067 char *value;
1069 if (wp == NULL)
1070 return (NULL);
1071 w = wp->window;
1073 status = options_get_number(w->options, "pane-border-status");
1074 if (status == PANE_STATUS_TOP)
1075 flag = (wp->yoff == 1);
1076 else
1077 flag = (wp->yoff == 0);
1078 xasprintf(&value, "%d", flag);
1079 return (value);
1082 /* Callback for pane_at_bottom. */
1083 static void *
1084 format_cb_pane_at_bottom(struct format_tree *ft)
1086 struct window_pane *wp = ft->wp;
1087 struct window *w;
1088 int status, flag;
1089 char *value;
1091 if (wp == NULL)
1092 return (NULL);
1093 w = wp->window;
1095 status = options_get_number(w->options, "pane-border-status");
1096 if (status == PANE_STATUS_BOTTOM)
1097 flag = (wp->yoff + wp->sy == w->sy - 1);
1098 else
1099 flag = (wp->yoff + wp->sy == w->sy);
1100 xasprintf(&value, "%d", flag);
1101 return (value);
1104 /* Callback for cursor_character. */
1105 static void *
1106 format_cb_cursor_character(struct format_tree *ft)
1108 struct window_pane *wp = ft->wp;
1109 struct grid_cell gc;
1110 char *value = NULL;
1112 if (wp == NULL)
1113 return (NULL);
1115 grid_view_get_cell(wp->base.grid, wp->base.cx, wp->base.cy, &gc);
1116 if (~gc.flags & GRID_FLAG_PADDING)
1117 xasprintf(&value, "%.*s", (int)gc.data.size, gc.data.data);
1118 return (value);
1121 /* Callback for mouse_word. */
1122 static void *
1123 format_cb_mouse_word(struct format_tree *ft)
1125 struct window_pane *wp;
1126 struct grid *gd;
1127 u_int x, y;
1128 char *s;
1130 if (!ft->m.valid)
1131 return (NULL);
1132 wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1133 if (wp == NULL)
1134 return (NULL);
1135 if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0)
1136 return (NULL);
1138 if (!TAILQ_EMPTY(&wp->modes)) {
1139 if (TAILQ_FIRST(&wp->modes)->mode == &window_copy_mode ||
1140 TAILQ_FIRST(&wp->modes)->mode == &window_view_mode)
1141 return (s = window_copy_get_word(wp, x, y));
1142 return (NULL);
1144 gd = wp->base.grid;
1145 return (format_grid_word(gd, x, gd->hsize + y));
1148 /* Callback for mouse_hyperlink. */
1149 static void *
1150 format_cb_mouse_hyperlink(struct format_tree *ft)
1152 struct window_pane *wp;
1153 struct grid *gd;
1154 u_int x, y;
1156 if (!ft->m.valid)
1157 return (NULL);
1158 wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1159 if (wp == NULL)
1160 return (NULL);
1161 if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0)
1162 return (NULL);
1163 gd = wp->base.grid;
1164 return (format_grid_hyperlink(gd, x, gd->hsize + y, wp->screen));
1167 /* Callback for mouse_line. */
1168 static void *
1169 format_cb_mouse_line(struct format_tree *ft)
1171 struct window_pane *wp;
1172 struct grid *gd;
1173 u_int x, y;
1175 if (!ft->m.valid)
1176 return (NULL);
1177 wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1178 if (wp == NULL)
1179 return (NULL);
1180 if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0)
1181 return (NULL);
1183 if (!TAILQ_EMPTY(&wp->modes)) {
1184 if (TAILQ_FIRST(&wp->modes)->mode == &window_copy_mode ||
1185 TAILQ_FIRST(&wp->modes)->mode == &window_view_mode)
1186 return (window_copy_get_line(wp, y));
1187 return (NULL);
1189 gd = wp->base.grid;
1190 return (format_grid_line(gd, gd->hsize + y));
1193 /* Callback for alternate_on. */
1194 static void *
1195 format_cb_alternate_on(struct format_tree *ft)
1197 if (ft->wp != NULL) {
1198 if (ft->wp->base.saved_grid != NULL)
1199 return (xstrdup("1"));
1200 return (xstrdup("0"));
1202 return (NULL);
1205 /* Callback for alternate_saved_x. */
1206 static void *
1207 format_cb_alternate_saved_x(struct format_tree *ft)
1209 if (ft->wp != NULL)
1210 return (format_printf("%u", ft->wp->base.saved_cx));
1211 return (NULL);
1214 /* Callback for alternate_saved_y. */
1215 static void *
1216 format_cb_alternate_saved_y(struct format_tree *ft)
1218 if (ft->wp != NULL)
1219 return (format_printf("%u", ft->wp->base.saved_cy));
1220 return (NULL);
1223 /* Callback for buffer_name. */
1224 static void *
1225 format_cb_buffer_name(struct format_tree *ft)
1227 if (ft->pb != NULL)
1228 return (xstrdup(paste_buffer_name(ft->pb)));
1229 return (NULL);
1232 /* Callback for buffer_sample. */
1233 static void *
1234 format_cb_buffer_sample(struct format_tree *ft)
1236 if (ft->pb != NULL)
1237 return (paste_make_sample(ft->pb));
1238 return (NULL);
1241 /* Callback for buffer_size. */
1242 static void *
1243 format_cb_buffer_size(struct format_tree *ft)
1245 size_t size;
1247 if (ft->pb != NULL) {
1248 paste_buffer_data(ft->pb, &size);
1249 return (format_printf("%zu", size));
1251 return (NULL);
1254 /* Callback for client_cell_height. */
1255 static void *
1256 format_cb_client_cell_height(struct format_tree *ft)
1258 if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED))
1259 return (format_printf("%u", ft->c->tty.ypixel));
1260 return (NULL);
1263 /* Callback for client_cell_width. */
1264 static void *
1265 format_cb_client_cell_width(struct format_tree *ft)
1267 if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED))
1268 return (format_printf("%u", ft->c->tty.xpixel));
1269 return (NULL);
1272 /* Callback for client_control_mode. */
1273 static void *
1274 format_cb_client_control_mode(struct format_tree *ft)
1276 if (ft->c != NULL) {
1277 if (ft->c->flags & CLIENT_CONTROL)
1278 return (xstrdup("1"));
1279 return (xstrdup("0"));
1281 return (NULL);
1284 /* Callback for client_discarded. */
1285 static void *
1286 format_cb_client_discarded(struct format_tree *ft)
1288 if (ft->c != NULL)
1289 return (format_printf("%zu", ft->c->discarded));
1290 return (NULL);
1293 /* Callback for client_flags. */
1294 static void *
1295 format_cb_client_flags(struct format_tree *ft)
1297 if (ft->c != NULL)
1298 return (xstrdup(server_client_get_flags(ft->c)));
1299 return (NULL);
1302 /* Callback for client_height. */
1303 static void *
1304 format_cb_client_height(struct format_tree *ft)
1306 if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED))
1307 return (format_printf("%u", ft->c->tty.sy));
1308 return (NULL);
1311 /* Callback for client_key_table. */
1312 static void *
1313 format_cb_client_key_table(struct format_tree *ft)
1315 if (ft->c != NULL)
1316 return (xstrdup(ft->c->keytable->name));
1317 return (NULL);
1320 /* Callback for client_last_session. */
1321 static void *
1322 format_cb_client_last_session(struct format_tree *ft)
1324 if (ft->c != NULL &&
1325 ft->c->last_session != NULL &&
1326 session_alive(ft->c->last_session))
1327 return (xstrdup(ft->c->last_session->name));
1328 return (NULL);
1331 /* Callback for client_name. */
1332 static void *
1333 format_cb_client_name(struct format_tree *ft)
1335 if (ft->c != NULL)
1336 return (xstrdup(ft->c->name));
1337 return (NULL);
1340 /* Callback for client_pid. */
1341 static void *
1342 format_cb_client_pid(struct format_tree *ft)
1344 if (ft->c != NULL)
1345 return (format_printf("%ld", (long)ft->c->pid));
1346 return (NULL);
1349 /* Callback for client_prefix. */
1350 static void *
1351 format_cb_client_prefix(struct format_tree *ft)
1353 const char *name;
1355 if (ft->c != NULL) {
1356 name = server_client_get_key_table(ft->c);
1357 if (strcmp(ft->c->keytable->name, name) == 0)
1358 return (xstrdup("0"));
1359 return (xstrdup("1"));
1361 return (NULL);
1364 /* Callback for client_readonly. */
1365 static void *
1366 format_cb_client_readonly(struct format_tree *ft)
1368 if (ft->c != NULL) {
1369 if (ft->c->flags & CLIENT_READONLY)
1370 return (xstrdup("1"));
1371 return (xstrdup("0"));
1373 return (NULL);
1376 /* Callback for client_session. */
1377 static void *
1378 format_cb_client_session(struct format_tree *ft)
1380 if (ft->c != NULL && ft->c->session != NULL)
1381 return (xstrdup(ft->c->session->name));
1382 return (NULL);
1385 /* Callback for client_termfeatures. */
1386 static void *
1387 format_cb_client_termfeatures(struct format_tree *ft)
1389 if (ft->c != NULL)
1390 return (xstrdup(tty_get_features(ft->c->term_features)));
1391 return (NULL);
1394 /* Callback for client_termname. */
1395 static void *
1396 format_cb_client_termname(struct format_tree *ft)
1398 if (ft->c != NULL)
1399 return (xstrdup(ft->c->term_name));
1400 return (NULL);
1403 /* Callback for client_termtype. */
1404 static void *
1405 format_cb_client_termtype(struct format_tree *ft)
1407 if (ft->c != NULL) {
1408 if (ft->c->term_type == NULL)
1409 return (xstrdup(""));
1410 return (xstrdup(ft->c->term_type));
1412 return (NULL);
1415 /* Callback for client_tty. */
1416 static void *
1417 format_cb_client_tty(struct format_tree *ft)
1419 if (ft->c != NULL)
1420 return (xstrdup(ft->c->ttyname));
1421 return (NULL);
1424 /* Callback for client_uid. */
1425 static void *
1426 format_cb_client_uid(struct format_tree *ft)
1428 uid_t uid;
1430 if (ft->c != NULL) {
1431 uid = proc_get_peer_uid(ft->c->peer);
1432 if (uid != (uid_t)-1)
1433 return (format_printf("%ld", (long)uid));
1435 return (NULL);
1438 /* Callback for client_user. */
1439 static void *
1440 format_cb_client_user(struct format_tree *ft)
1442 uid_t uid;
1443 struct passwd *pw;
1445 if (ft->c != NULL) {
1446 uid = proc_get_peer_uid(ft->c->peer);
1447 if (uid != (uid_t)-1 && (pw = getpwuid(uid)) != NULL)
1448 return (xstrdup(pw->pw_name));
1450 return (NULL);
1453 /* Callback for client_utf8. */
1454 static void *
1455 format_cb_client_utf8(struct format_tree *ft)
1457 if (ft->c != NULL) {
1458 if (ft->c->flags & CLIENT_UTF8)
1459 return (xstrdup("1"));
1460 return (xstrdup("0"));
1462 return (NULL);
1465 /* Callback for client_width. */
1466 static void *
1467 format_cb_client_width(struct format_tree *ft)
1469 if (ft->c != NULL)
1470 return (format_printf("%u", ft->c->tty.sx));
1471 return (NULL);
1474 /* Callback for client_written. */
1475 static void *
1476 format_cb_client_written(struct format_tree *ft)
1478 if (ft->c != NULL)
1479 return (format_printf("%zu", ft->c->written));
1480 return (NULL);
1483 /* Callback for config_files. */
1484 static void *
1485 format_cb_config_files(__unused struct format_tree *ft)
1487 char *s = NULL;
1488 size_t slen = 0;
1489 u_int i;
1490 size_t n;
1492 for (i = 0; i < cfg_nfiles; i++) {
1493 n = strlen(cfg_files[i]) + 1;
1494 s = xrealloc(s, slen + n + 1);
1495 slen += xsnprintf(s + slen, n + 1, "%s,", cfg_files[i]);
1497 if (s == NULL)
1498 return (xstrdup(""));
1499 s[slen - 1] = '\0';
1500 return (s);
1503 /* Callback for cursor_flag. */
1504 static void *
1505 format_cb_cursor_flag(struct format_tree *ft)
1507 if (ft->wp != NULL) {
1508 if (ft->wp->base.mode & MODE_CURSOR)
1509 return (xstrdup("1"));
1510 return (xstrdup("0"));
1512 return (NULL);
1515 /* Callback for cursor_x. */
1516 static void *
1517 format_cb_cursor_x(struct format_tree *ft)
1519 if (ft->wp != NULL)
1520 return (format_printf("%u", ft->wp->base.cx));
1521 return (NULL);
1524 /* Callback for cursor_y. */
1525 static void *
1526 format_cb_cursor_y(struct format_tree *ft)
1528 if (ft->wp != NULL)
1529 return (format_printf("%u", ft->wp->base.cy));
1530 return (NULL);
1533 /* Callback for history_limit. */
1534 static void *
1535 format_cb_history_limit(struct format_tree *ft)
1537 if (ft->wp != NULL)
1538 return (format_printf("%u", ft->wp->base.grid->hlimit));
1539 return (NULL);
1542 /* Callback for history_size. */
1543 static void *
1544 format_cb_history_size(struct format_tree *ft)
1546 if (ft->wp != NULL)
1547 return (format_printf("%u", ft->wp->base.grid->hsize));
1548 return (NULL);
1551 /* Callback for insert_flag. */
1552 static void *
1553 format_cb_insert_flag(struct format_tree *ft)
1555 if (ft->wp != NULL) {
1556 if (ft->wp->base.mode & MODE_INSERT)
1557 return (xstrdup("1"));
1558 return (xstrdup("0"));
1560 return (NULL);
1563 /* Callback for keypad_cursor_flag. */
1564 static void *
1565 format_cb_keypad_cursor_flag(struct format_tree *ft)
1567 if (ft->wp != NULL) {
1568 if (ft->wp->base.mode & MODE_KCURSOR)
1569 return (xstrdup("1"));
1570 return (xstrdup("0"));
1572 return (NULL);
1575 /* Callback for keypad_flag. */
1576 static void *
1577 format_cb_keypad_flag(struct format_tree *ft)
1579 if (ft->wp != NULL) {
1580 if (ft->wp->base.mode & MODE_KKEYPAD)
1581 return (xstrdup("1"));
1582 return (xstrdup("0"));
1584 return (NULL);
1587 /* Callback for mouse_all_flag. */
1588 static void *
1589 format_cb_mouse_all_flag(struct format_tree *ft)
1591 if (ft->wp != NULL) {
1592 if (ft->wp->base.mode & MODE_MOUSE_ALL)
1593 return (xstrdup("1"));
1594 return (xstrdup("0"));
1596 return (NULL);
1599 /* Callback for mouse_any_flag. */
1600 static void *
1601 format_cb_mouse_any_flag(struct format_tree *ft)
1603 if (ft->wp != NULL) {
1604 if (ft->wp->base.mode & ALL_MOUSE_MODES)
1605 return (xstrdup("1"));
1606 return (xstrdup("0"));
1608 return (NULL);
1611 /* Callback for mouse_button_flag. */
1612 static void *
1613 format_cb_mouse_button_flag(struct format_tree *ft)
1615 if (ft->wp != NULL) {
1616 if (ft->wp->base.mode & MODE_MOUSE_BUTTON)
1617 return (xstrdup("1"));
1618 return (xstrdup("0"));
1620 return (NULL);
1623 /* Callback for mouse_pane. */
1624 static void *
1625 format_cb_mouse_pane(struct format_tree *ft)
1627 struct window_pane *wp;
1629 if (ft->m.valid) {
1630 wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1631 if (wp != NULL)
1632 return (format_printf("%%%u", wp->id));
1633 return (NULL);
1635 return (NULL);
1638 /* Callback for mouse_sgr_flag. */
1639 static void *
1640 format_cb_mouse_sgr_flag(struct format_tree *ft)
1642 if (ft->wp != NULL) {
1643 if (ft->wp->base.mode & MODE_MOUSE_SGR)
1644 return (xstrdup("1"));
1645 return (xstrdup("0"));
1647 return (NULL);
1650 /* Callback for mouse_standard_flag. */
1651 static void *
1652 format_cb_mouse_standard_flag(struct format_tree *ft)
1654 if (ft->wp != NULL) {
1655 if (ft->wp->base.mode & MODE_MOUSE_STANDARD)
1656 return (xstrdup("1"));
1657 return (xstrdup("0"));
1659 return (NULL);
1662 /* Callback for mouse_utf8_flag. */
1663 static void *
1664 format_cb_mouse_utf8_flag(struct format_tree *ft)
1666 if (ft->wp != NULL) {
1667 if (ft->wp->base.mode & MODE_MOUSE_UTF8)
1668 return (xstrdup("1"));
1669 return (xstrdup("0"));
1671 return (NULL);
1674 /* Callback for mouse_x. */
1675 static void *
1676 format_cb_mouse_x(struct format_tree *ft)
1678 struct window_pane *wp;
1679 u_int x, y;
1681 if (!ft->m.valid)
1682 return (NULL);
1683 wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1684 if (wp != NULL && cmd_mouse_at(wp, &ft->m, &x, &y, 0) == 0)
1685 return (format_printf("%u", x));
1686 if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED)) {
1687 if (ft->m.statusat == 0 && ft->m.y < ft->m.statuslines)
1688 return (format_printf("%u", ft->m.x));
1689 if (ft->m.statusat > 0 && ft->m.y >= (u_int)ft->m.statusat)
1690 return (format_printf("%u", ft->m.x));
1692 return (NULL);
1695 /* Callback for mouse_y. */
1696 static void *
1697 format_cb_mouse_y(struct format_tree *ft)
1699 struct window_pane *wp;
1700 u_int x, y;
1702 if (!ft->m.valid)
1703 return (NULL);
1704 wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1705 if (wp != NULL && cmd_mouse_at(wp, &ft->m, &x, &y, 0) == 0)
1706 return (format_printf("%u", y));
1707 if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED)) {
1708 if (ft->m.statusat == 0 && ft->m.y < ft->m.statuslines)
1709 return (format_printf("%u", ft->m.y));
1710 if (ft->m.statusat > 0 && ft->m.y >= (u_int)ft->m.statusat)
1711 return (format_printf("%u", ft->m.y - ft->m.statusat));
1713 return (NULL);
1716 /* Callback for next_session_id. */
1717 static void *
1718 format_cb_next_session_id(__unused struct format_tree *ft)
1720 return (format_printf("$%u", next_session_id));
1723 /* Callback for origin_flag. */
1724 static void *
1725 format_cb_origin_flag(struct format_tree *ft)
1727 if (ft->wp != NULL) {
1728 if (ft->wp->base.mode & MODE_ORIGIN)
1729 return (xstrdup("1"));
1730 return (xstrdup("0"));
1732 return (NULL);
1735 /* Callback for pane_active. */
1736 static void *
1737 format_cb_pane_active(struct format_tree *ft)
1739 if (ft->wp != NULL) {
1740 if (ft->wp == ft->wp->window->active)
1741 return (xstrdup("1"));
1742 return (xstrdup("0"));
1744 return (NULL);
1747 /* Callback for pane_at_left. */
1748 static void *
1749 format_cb_pane_at_left(struct format_tree *ft)
1751 if (ft->wp != NULL) {
1752 if (ft->wp->xoff == 0)
1753 return (xstrdup("1"));
1754 return (xstrdup("0"));
1756 return (NULL);
1759 /* Callback for pane_at_right. */
1760 static void *
1761 format_cb_pane_at_right(struct format_tree *ft)
1763 if (ft->wp != NULL) {
1764 if (ft->wp->xoff + ft->wp->sx == ft->wp->window->sx)
1765 return (xstrdup("1"));
1766 return (xstrdup("0"));
1768 return (NULL);
1771 /* Callback for pane_bottom. */
1772 static void *
1773 format_cb_pane_bottom(struct format_tree *ft)
1775 if (ft->wp != NULL)
1776 return (format_printf("%u", ft->wp->yoff + ft->wp->sy - 1));
1777 return (NULL);
1780 /* Callback for pane_dead. */
1781 static void *
1782 format_cb_pane_dead(struct format_tree *ft)
1784 if (ft->wp != NULL) {
1785 if (ft->wp->fd == -1)
1786 return (xstrdup("1"));
1787 return (xstrdup("0"));
1789 return (NULL);
1792 /* Callback for pane_dead_signal. */
1793 static void *
1794 format_cb_pane_dead_signal(struct format_tree *ft)
1796 struct window_pane *wp = ft->wp;
1797 const char *name;
1799 if (wp != NULL) {
1800 if ((wp->flags & PANE_STATUSREADY) && WIFSIGNALED(wp->status)) {
1801 name = sig2name(WTERMSIG(wp->status));
1802 return (format_printf("%s", name));
1804 return (NULL);
1806 return (NULL);
1809 /* Callback for pane_dead_status. */
1810 static void *
1811 format_cb_pane_dead_status(struct format_tree *ft)
1813 struct window_pane *wp = ft->wp;
1815 if (wp != NULL) {
1816 if ((wp->flags & PANE_STATUSREADY) && WIFEXITED(wp->status))
1817 return (format_printf("%d", WEXITSTATUS(wp->status)));
1818 return (NULL);
1820 return (NULL);
1823 /* Callback for pane_dead_time. */
1824 static void *
1825 format_cb_pane_dead_time(struct format_tree *ft)
1827 struct window_pane *wp = ft->wp;
1829 if (wp != NULL) {
1830 if (wp->flags & PANE_STATUSDRAWN)
1831 return (&wp->dead_time);
1832 return (NULL);
1834 return (NULL);
1837 /* Callback for pane_format. */
1838 static void *
1839 format_cb_pane_format(struct format_tree *ft)
1841 if (ft->type == FORMAT_TYPE_PANE)
1842 return (xstrdup("1"));
1843 return (xstrdup("0"));
1846 /* Callback for pane_height. */
1847 static void *
1848 format_cb_pane_height(struct format_tree *ft)
1850 if (ft->wp != NULL)
1851 return (format_printf("%u", ft->wp->sy));
1852 return (NULL);
1855 /* Callback for pane_id. */
1856 static void *
1857 format_cb_pane_id(struct format_tree *ft)
1859 if (ft->wp != NULL)
1860 return (format_printf("%%%u", ft->wp->id));
1861 return (NULL);
1864 /* Callback for pane_index. */
1865 static void *
1866 format_cb_pane_index(struct format_tree *ft)
1868 u_int idx;
1870 if (ft->wp != NULL && window_pane_index(ft->wp, &idx) == 0)
1871 return (format_printf("%u", idx));
1872 return (NULL);
1875 /* Callback for pane_input_off. */
1876 static void *
1877 format_cb_pane_input_off(struct format_tree *ft)
1879 if (ft->wp != NULL) {
1880 if (ft->wp->flags & PANE_INPUTOFF)
1881 return (xstrdup("1"));
1882 return (xstrdup("0"));
1884 return (NULL);
1887 /* Callback for pane_last. */
1888 static void *
1889 format_cb_pane_last(struct format_tree *ft)
1891 if (ft->wp != NULL) {
1892 if (ft->wp == ft->wp->window->last)
1893 return (xstrdup("1"));
1894 return (xstrdup("0"));
1896 return (NULL);
1899 /* Callback for pane_left. */
1900 static void *
1901 format_cb_pane_left(struct format_tree *ft)
1903 if (ft->wp != NULL)
1904 return (format_printf("%u", ft->wp->xoff));
1905 return (NULL);
1908 /* Callback for pane_marked. */
1909 static void *
1910 format_cb_pane_marked(struct format_tree *ft)
1912 if (ft->wp != NULL) {
1913 if (server_check_marked() && marked_pane.wp == ft->wp)
1914 return (xstrdup("1"));
1915 return (xstrdup("0"));
1917 return (NULL);
1920 /* Callback for pane_marked_set. */
1921 static void *
1922 format_cb_pane_marked_set(struct format_tree *ft)
1924 if (ft->wp != NULL) {
1925 if (server_check_marked())
1926 return (xstrdup("1"));
1927 return (xstrdup("0"));
1929 return (NULL);
1932 /* Callback for pane_mode. */
1933 static void *
1934 format_cb_pane_mode(struct format_tree *ft)
1936 struct window_mode_entry *wme;
1938 if (ft->wp != NULL) {
1939 wme = TAILQ_FIRST(&ft->wp->modes);
1940 if (wme != NULL)
1941 return (xstrdup(wme->mode->name));
1942 return (NULL);
1944 return (NULL);
1947 /* Callback for pane_path. */
1948 static void *
1949 format_cb_pane_path(struct format_tree *ft)
1951 if (ft->wp != NULL) {
1952 if (ft->wp->base.path == NULL)
1953 return (xstrdup(""));
1954 return (xstrdup(ft->wp->base.path));
1956 return (NULL);
1959 /* Callback for pane_pid. */
1960 static void *
1961 format_cb_pane_pid(struct format_tree *ft)
1963 if (ft->wp != NULL)
1964 return (format_printf("%ld", (long)ft->wp->pid));
1965 return (NULL);
1968 /* Callback for pane_pipe. */
1969 static void *
1970 format_cb_pane_pipe(struct format_tree *ft)
1972 if (ft->wp != NULL) {
1973 if (ft->wp->pipe_fd != -1)
1974 return (xstrdup("1"));
1975 return (xstrdup("0"));
1977 return (NULL);
1980 /* Callback for pane_right. */
1981 static void *
1982 format_cb_pane_right(struct format_tree *ft)
1984 if (ft->wp != NULL)
1985 return (format_printf("%u", ft->wp->xoff + ft->wp->sx - 1));
1986 return (NULL);
1989 /* Callback for pane_search_string. */
1990 static void *
1991 format_cb_pane_search_string(struct format_tree *ft)
1993 if (ft->wp != NULL) {
1994 if (ft->wp->searchstr == NULL)
1995 return (xstrdup(""));
1996 return (xstrdup(ft->wp->searchstr));
1998 return (NULL);
2001 /* Callback for pane_synchronized. */
2002 static void *
2003 format_cb_pane_synchronized(struct format_tree *ft)
2005 if (ft->wp != NULL) {
2006 if (options_get_number(ft->wp->options, "synchronize-panes"))
2007 return (xstrdup("1"));
2008 return (xstrdup("0"));
2010 return (NULL);
2013 /* Callback for pane_title. */
2014 static void *
2015 format_cb_pane_title(struct format_tree *ft)
2017 if (ft->wp != NULL)
2018 return (xstrdup(ft->wp->base.title));
2019 return (NULL);
2022 /* Callback for pane_top. */
2023 static void *
2024 format_cb_pane_top(struct format_tree *ft)
2026 if (ft->wp != NULL)
2027 return (format_printf("%u", ft->wp->yoff));
2028 return (NULL);
2031 /* Callback for pane_tty. */
2032 static void *
2033 format_cb_pane_tty(struct format_tree *ft)
2035 if (ft->wp != NULL)
2036 return (xstrdup(ft->wp->tty));
2037 return (NULL);
2040 /* Callback for pane_width. */
2041 static void *
2042 format_cb_pane_width(struct format_tree *ft)
2044 if (ft->wp != NULL)
2045 return (format_printf("%u", ft->wp->sx));
2046 return (NULL);
2049 /* Callback for scroll_region_lower. */
2050 static void *
2051 format_cb_scroll_region_lower(struct format_tree *ft)
2053 if (ft->wp != NULL)
2054 return (format_printf("%u", ft->wp->base.rlower));
2055 return (NULL);
2058 /* Callback for scroll_region_upper. */
2059 static void *
2060 format_cb_scroll_region_upper(struct format_tree *ft)
2062 if (ft->wp != NULL)
2063 return (format_printf("%u", ft->wp->base.rupper));
2064 return (NULL);
2067 /* Callback for session_attached. */
2068 static void *
2069 format_cb_session_attached(struct format_tree *ft)
2071 if (ft->s != NULL)
2072 return (format_printf("%u", ft->s->attached));
2073 return (NULL);
2076 /* Callback for session_format. */
2077 static void *
2078 format_cb_session_format(struct format_tree *ft)
2080 if (ft->type == FORMAT_TYPE_SESSION)
2081 return (xstrdup("1"));
2082 return (xstrdup("0"));
2085 /* Callback for session_group. */
2086 static void *
2087 format_cb_session_group(struct format_tree *ft)
2089 struct session_group *sg;
2091 if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL)
2092 return (xstrdup(sg->name));
2093 return (NULL);
2096 /* Callback for session_group_attached. */
2097 static void *
2098 format_cb_session_group_attached(struct format_tree *ft)
2100 struct session_group *sg;
2102 if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL)
2103 return (format_printf("%u", session_group_attached_count (sg)));
2104 return (NULL);
2107 /* Callback for session_group_many_attached. */
2108 static void *
2109 format_cb_session_group_many_attached(struct format_tree *ft)
2111 struct session_group *sg;
2113 if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL) {
2114 if (session_group_attached_count (sg) > 1)
2115 return (xstrdup("1"));
2116 return (xstrdup("0"));
2118 return (NULL);
2121 /* Callback for session_group_size. */
2122 static void *
2123 format_cb_session_group_size(struct format_tree *ft)
2125 struct session_group *sg;
2127 if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL)
2128 return (format_printf("%u", session_group_count (sg)));
2129 return (NULL);
2132 /* Callback for session_grouped. */
2133 static void *
2134 format_cb_session_grouped(struct format_tree *ft)
2136 if (ft->s != NULL) {
2137 if (session_group_contains(ft->s) != NULL)
2138 return (xstrdup("1"));
2139 return (xstrdup("0"));
2141 return (NULL);
2144 /* Callback for session_id. */
2145 static void *
2146 format_cb_session_id(struct format_tree *ft)
2148 if (ft->s != NULL)
2149 return (format_printf("$%u", ft->s->id));
2150 return (NULL);
2153 /* Callback for session_many_attached. */
2154 static void *
2155 format_cb_session_many_attached(struct format_tree *ft)
2157 if (ft->s != NULL) {
2158 if (ft->s->attached > 1)
2159 return (xstrdup("1"));
2160 return (xstrdup("0"));
2162 return (NULL);
2165 /* Callback for session_marked. */
2166 static void *
2167 format_cb_session_marked(struct format_tree *ft)
2169 if (ft->s != NULL) {
2170 if (server_check_marked() && marked_pane.s == ft->s)
2171 return (xstrdup("1"));
2172 return (xstrdup("0"));
2174 return (NULL);
2177 /* Callback for session_name. */
2178 static void *
2179 format_cb_session_name(struct format_tree *ft)
2181 if (ft->s != NULL)
2182 return (xstrdup(ft->s->name));
2183 return (NULL);
2186 /* Callback for session_path. */
2187 static void *
2188 format_cb_session_path(struct format_tree *ft)
2190 if (ft->s != NULL)
2191 return (xstrdup(ft->s->cwd));
2192 return (NULL);
2195 /* Callback for session_windows. */
2196 static void *
2197 format_cb_session_windows(struct format_tree *ft)
2199 if (ft->s != NULL)
2200 return (format_printf("%u", winlink_count(&ft->s->windows)));
2201 return (NULL);
2204 /* Callback for socket_path. */
2205 static void *
2206 format_cb_socket_path(__unused struct format_tree *ft)
2208 return (xstrdup(socket_path));
2211 /* Callback for version. */
2212 static void *
2213 format_cb_version(__unused struct format_tree *ft)
2215 return (xstrdup(getversion()));
2218 /* Callback for active_window_index. */
2219 static void *
2220 format_cb_active_window_index(struct format_tree *ft)
2222 if (ft->s != NULL)
2223 return (format_printf("%u", ft->s->curw->idx));
2224 return (NULL);
2227 /* Callback for last_window_index. */
2228 static void *
2229 format_cb_last_window_index(struct format_tree *ft)
2231 struct winlink *wl;
2233 if (ft->s != NULL) {
2234 wl = RB_MAX(winlinks, &ft->s->windows);
2235 return (format_printf("%u", wl->idx));
2237 return (NULL);
2240 /* Callback for window_active. */
2241 static void *
2242 format_cb_window_active(struct format_tree *ft)
2244 if (ft->wl != NULL) {
2245 if (ft->wl == ft->wl->session->curw)
2246 return (xstrdup("1"));
2247 return (xstrdup("0"));
2249 return (NULL);
2252 /* Callback for window_activity_flag. */
2253 static void *
2254 format_cb_window_activity_flag(struct format_tree *ft)
2256 if (ft->wl != NULL) {
2257 if (ft->wl->flags & WINLINK_ACTIVITY)
2258 return (xstrdup("1"));
2259 return (xstrdup("0"));
2261 return (NULL);
2264 /* Callback for window_bell_flag. */
2265 static void *
2266 format_cb_window_bell_flag(struct format_tree *ft)
2268 if (ft->wl != NULL) {
2269 if (ft->wl->flags & WINLINK_BELL)
2270 return (xstrdup("1"));
2271 return (xstrdup("0"));
2273 return (NULL);
2276 /* Callback for window_bigger. */
2277 static void *
2278 format_cb_window_bigger(struct format_tree *ft)
2280 u_int ox, oy, sx, sy;
2282 if (ft->c != NULL) {
2283 if (tty_window_offset(&ft->c->tty, &ox, &oy, &sx, &sy))
2284 return (xstrdup("1"));
2285 return (xstrdup("0"));
2287 return (NULL);
2290 /* Callback for window_cell_height. */
2291 static void *
2292 format_cb_window_cell_height(struct format_tree *ft)
2294 if (ft->w != NULL)
2295 return (format_printf("%u", ft->w->ypixel));
2296 return (NULL);
2299 /* Callback for window_cell_width. */
2300 static void *
2301 format_cb_window_cell_width(struct format_tree *ft)
2303 if (ft->w != NULL)
2304 return (format_printf("%u", ft->w->xpixel));
2305 return (NULL);
2308 /* Callback for window_end_flag. */
2309 static void *
2310 format_cb_window_end_flag(struct format_tree *ft)
2312 if (ft->wl != NULL) {
2313 if (ft->wl == RB_MAX(winlinks, &ft->wl->session->windows))
2314 return (xstrdup("1"));
2315 return (xstrdup("0"));
2317 return (NULL);
2320 /* Callback for window_flags. */
2321 static void *
2322 format_cb_window_flags(struct format_tree *ft)
2324 if (ft->wl != NULL)
2325 return (xstrdup(window_printable_flags(ft->wl, 1)));
2326 return (NULL);
2329 /* Callback for window_format. */
2330 static void *
2331 format_cb_window_format(struct format_tree *ft)
2333 if (ft->type == FORMAT_TYPE_WINDOW)
2334 return (xstrdup("1"));
2335 return (xstrdup("0"));
2338 /* Callback for window_height. */
2339 static void *
2340 format_cb_window_height(struct format_tree *ft)
2342 if (ft->w != NULL)
2343 return (format_printf("%u", ft->w->sy));
2344 return (NULL);
2347 /* Callback for window_id. */
2348 static void *
2349 format_cb_window_id(struct format_tree *ft)
2351 if (ft->w != NULL)
2352 return (format_printf("@%u", ft->w->id));
2353 return (NULL);
2356 /* Callback for window_index. */
2357 static void *
2358 format_cb_window_index(struct format_tree *ft)
2360 if (ft->wl != NULL)
2361 return (format_printf("%d", ft->wl->idx));
2362 return (NULL);
2365 /* Callback for window_last_flag. */
2366 static void *
2367 format_cb_window_last_flag(struct format_tree *ft)
2369 if (ft->wl != NULL) {
2370 if (ft->wl == TAILQ_FIRST(&ft->wl->session->lastw))
2371 return (xstrdup("1"));
2372 return (xstrdup("0"));
2374 return (NULL);
2377 /* Callback for window_linked. */
2378 static void *
2379 format_cb_window_linked(struct format_tree *ft)
2381 if (ft->wl != NULL) {
2382 if (session_is_linked(ft->wl->session, ft->wl->window))
2383 return (xstrdup("1"));
2384 return (xstrdup("0"));
2386 return (NULL);
2389 /* Callback for window_linked_sessions. */
2390 static void *
2391 format_cb_window_linked_sessions(struct format_tree *ft)
2393 if (ft->wl != NULL)
2394 return (format_printf("%u", ft->wl->window->references));
2395 return (NULL);
2398 /* Callback for window_marked_flag. */
2399 static void *
2400 format_cb_window_marked_flag(struct format_tree *ft)
2402 if (ft->wl != NULL) {
2403 if (server_check_marked() && marked_pane.wl == ft->wl)
2404 return (xstrdup("1"));
2405 return (xstrdup("0"));
2407 return (NULL);
2410 /* Callback for window_name. */
2411 static void *
2412 format_cb_window_name(struct format_tree *ft)
2414 if (ft->w != NULL)
2415 return (format_printf("%s", ft->w->name));
2416 return (NULL);
2419 /* Callback for window_offset_x. */
2420 static void *
2421 format_cb_window_offset_x(struct format_tree *ft)
2423 u_int ox, oy, sx, sy;
2425 if (ft->c != NULL) {
2426 if (tty_window_offset(&ft->c->tty, &ox, &oy, &sx, &sy))
2427 return (format_printf("%u", ox));
2428 return (NULL);
2430 return (NULL);
2433 /* Callback for window_offset_y. */
2434 static void *
2435 format_cb_window_offset_y(struct format_tree *ft)
2437 u_int ox, oy, sx, sy;
2439 if (ft->c != NULL) {
2440 if (tty_window_offset(&ft->c->tty, &ox, &oy, &sx, &sy))
2441 return (format_printf("%u", oy));
2442 return (NULL);
2444 return (NULL);
2447 /* Callback for window_panes. */
2448 static void *
2449 format_cb_window_panes(struct format_tree *ft)
2451 if (ft->w != NULL)
2452 return (format_printf("%u", window_count_panes(ft->w)));
2453 return (NULL);
2456 /* Callback for window_raw_flags. */
2457 static void *
2458 format_cb_window_raw_flags(struct format_tree *ft)
2460 if (ft->wl != NULL)
2461 return (xstrdup(window_printable_flags(ft->wl, 0)));
2462 return (NULL);
2465 /* Callback for window_silence_flag. */
2466 static void *
2467 format_cb_window_silence_flag(struct format_tree *ft)
2469 if (ft->wl != NULL) {
2470 if (ft->wl->flags & WINLINK_SILENCE)
2471 return (xstrdup("1"));
2472 return (xstrdup("0"));
2474 return (NULL);
2477 /* Callback for window_start_flag. */
2478 static void *
2479 format_cb_window_start_flag(struct format_tree *ft)
2481 if (ft->wl != NULL) {
2482 if (ft->wl == RB_MIN(winlinks, &ft->wl->session->windows))
2483 return (xstrdup("1"));
2484 return (xstrdup("0"));
2486 return (NULL);
2489 /* Callback for window_width. */
2490 static void *
2491 format_cb_window_width(struct format_tree *ft)
2493 if (ft->w != NULL)
2494 return (format_printf("%u", ft->w->sx));
2495 return (NULL);
2498 /* Callback for window_zoomed_flag. */
2499 static void *
2500 format_cb_window_zoomed_flag(struct format_tree *ft)
2502 if (ft->w != NULL) {
2503 if (ft->w->flags & WINDOW_ZOOMED)
2504 return (xstrdup("1"));
2505 return (xstrdup("0"));
2507 return (NULL);
2510 /* Callback for wrap_flag. */
2511 static void *
2512 format_cb_wrap_flag(struct format_tree *ft)
2514 if (ft->wp != NULL) {
2515 if (ft->wp->base.mode & MODE_WRAP)
2516 return (xstrdup("1"));
2517 return (xstrdup("0"));
2519 return (NULL);
2522 /* Callback for buffer_created. */
2523 static void *
2524 format_cb_buffer_created(struct format_tree *ft)
2526 static struct timeval tv;
2528 if (ft->pb != NULL) {
2529 timerclear(&tv);
2530 tv.tv_sec = paste_buffer_created(ft->pb);
2531 return (&tv);
2533 return (NULL);
2536 /* Callback for client_activity. */
2537 static void *
2538 format_cb_client_activity(struct format_tree *ft)
2540 if (ft->c != NULL)
2541 return (&ft->c->activity_time);
2542 return (NULL);
2545 /* Callback for client_created. */
2546 static void *
2547 format_cb_client_created(struct format_tree *ft)
2549 if (ft->c != NULL)
2550 return (&ft->c->creation_time);
2551 return (NULL);
2554 /* Callback for session_activity. */
2555 static void *
2556 format_cb_session_activity(struct format_tree *ft)
2558 if (ft->s != NULL)
2559 return (&ft->s->activity_time);
2560 return (NULL);
2563 /* Callback for session_created. */
2564 static void *
2565 format_cb_session_created(struct format_tree *ft)
2567 if (ft->s != NULL)
2568 return (&ft->s->creation_time);
2569 return (NULL);
2572 /* Callback for session_last_attached. */
2573 static void *
2574 format_cb_session_last_attached(struct format_tree *ft)
2576 if (ft->s != NULL)
2577 return (&ft->s->last_attached_time);
2578 return (NULL);
2581 /* Callback for start_time. */
2582 static void *
2583 format_cb_start_time(__unused struct format_tree *ft)
2585 return (&start_time);
2588 /* Callback for window_activity. */
2589 static void *
2590 format_cb_window_activity(struct format_tree *ft)
2592 if (ft->w != NULL)
2593 return (&ft->w->activity_time);
2594 return (NULL);
2597 /* Callback for buffer_mode_format, */
2598 static void *
2599 format_cb_buffer_mode_format(__unused struct format_tree *ft)
2601 return (xstrdup(window_buffer_mode.default_format));
2604 /* Callback for client_mode_format, */
2605 static void *
2606 format_cb_client_mode_format(__unused struct format_tree *ft)
2608 return (xstrdup(window_client_mode.default_format));
2611 /* Callback for tree_mode_format, */
2612 static void *
2613 format_cb_tree_mode_format(__unused struct format_tree *ft)
2615 return (xstrdup(window_tree_mode.default_format));
2618 /* Callback for uid. */
2619 static void *
2620 format_cb_uid(__unused struct format_tree *ft)
2622 return (format_printf("%ld", (long)getuid()));
2625 /* Callback for user. */
2626 static void *
2627 format_cb_user(__unused struct format_tree *ft)
2629 struct passwd *pw;
2631 if ((pw = getpwuid(getuid())) != NULL)
2632 return (xstrdup(pw->pw_name));
2633 return (NULL);
2636 /* Format table type. */
2637 enum format_table_type {
2638 FORMAT_TABLE_STRING,
2639 FORMAT_TABLE_TIME
2642 /* Format table entry. */
2643 struct format_table_entry {
2644 const char *key;
2645 enum format_table_type type;
2646 format_cb cb;
2650 * Format table. Default format variables (that are almost always in the tree
2651 * and where the value is expanded by a callback in this file) are listed here.
2652 * Only variables which are added by the caller go into the tree.
2654 static const struct format_table_entry format_table[] = {
2655 { "active_window_index", FORMAT_TABLE_STRING,
2656 format_cb_active_window_index
2658 { "alternate_on", FORMAT_TABLE_STRING,
2659 format_cb_alternate_on
2661 { "alternate_saved_x", FORMAT_TABLE_STRING,
2662 format_cb_alternate_saved_x
2664 { "alternate_saved_y", FORMAT_TABLE_STRING,
2665 format_cb_alternate_saved_y
2667 { "buffer_created", FORMAT_TABLE_TIME,
2668 format_cb_buffer_created
2670 { "buffer_mode_format", FORMAT_TABLE_STRING,
2671 format_cb_buffer_mode_format
2673 { "buffer_name", FORMAT_TABLE_STRING,
2674 format_cb_buffer_name
2676 { "buffer_sample", FORMAT_TABLE_STRING,
2677 format_cb_buffer_sample
2679 { "buffer_size", FORMAT_TABLE_STRING,
2680 format_cb_buffer_size
2682 { "client_activity", FORMAT_TABLE_TIME,
2683 format_cb_client_activity
2685 { "client_cell_height", FORMAT_TABLE_STRING,
2686 format_cb_client_cell_height
2688 { "client_cell_width", FORMAT_TABLE_STRING,
2689 format_cb_client_cell_width
2691 { "client_control_mode", FORMAT_TABLE_STRING,
2692 format_cb_client_control_mode
2694 { "client_created", FORMAT_TABLE_TIME,
2695 format_cb_client_created
2697 { "client_discarded", FORMAT_TABLE_STRING,
2698 format_cb_client_discarded
2700 { "client_flags", FORMAT_TABLE_STRING,
2701 format_cb_client_flags
2703 { "client_height", FORMAT_TABLE_STRING,
2704 format_cb_client_height
2706 { "client_key_table", FORMAT_TABLE_STRING,
2707 format_cb_client_key_table
2709 { "client_last_session", FORMAT_TABLE_STRING,
2710 format_cb_client_last_session
2712 { "client_mode_format", FORMAT_TABLE_STRING,
2713 format_cb_client_mode_format
2715 { "client_name", FORMAT_TABLE_STRING,
2716 format_cb_client_name
2718 { "client_pid", FORMAT_TABLE_STRING,
2719 format_cb_client_pid
2721 { "client_prefix", FORMAT_TABLE_STRING,
2722 format_cb_client_prefix
2724 { "client_readonly", FORMAT_TABLE_STRING,
2725 format_cb_client_readonly
2727 { "client_session", FORMAT_TABLE_STRING,
2728 format_cb_client_session
2730 { "client_termfeatures", FORMAT_TABLE_STRING,
2731 format_cb_client_termfeatures
2733 { "client_termname", FORMAT_TABLE_STRING,
2734 format_cb_client_termname
2736 { "client_termtype", FORMAT_TABLE_STRING,
2737 format_cb_client_termtype
2739 { "client_tty", FORMAT_TABLE_STRING,
2740 format_cb_client_tty
2742 { "client_uid", FORMAT_TABLE_STRING,
2743 format_cb_client_uid
2745 { "client_user", FORMAT_TABLE_STRING,
2746 format_cb_client_user
2748 { "client_utf8", FORMAT_TABLE_STRING,
2749 format_cb_client_utf8
2751 { "client_width", FORMAT_TABLE_STRING,
2752 format_cb_client_width
2754 { "client_written", FORMAT_TABLE_STRING,
2755 format_cb_client_written
2757 { "config_files", FORMAT_TABLE_STRING,
2758 format_cb_config_files
2760 { "cursor_character", FORMAT_TABLE_STRING,
2761 format_cb_cursor_character
2763 { "cursor_flag", FORMAT_TABLE_STRING,
2764 format_cb_cursor_flag
2766 { "cursor_x", FORMAT_TABLE_STRING,
2767 format_cb_cursor_x
2769 { "cursor_y", FORMAT_TABLE_STRING,
2770 format_cb_cursor_y
2772 { "history_all_bytes", FORMAT_TABLE_STRING,
2773 format_cb_history_all_bytes
2775 { "history_bytes", FORMAT_TABLE_STRING,
2776 format_cb_history_bytes
2778 { "history_limit", FORMAT_TABLE_STRING,
2779 format_cb_history_limit
2781 { "history_size", FORMAT_TABLE_STRING,
2782 format_cb_history_size
2784 { "host", FORMAT_TABLE_STRING,
2785 format_cb_host
2787 { "host_short", FORMAT_TABLE_STRING,
2788 format_cb_host_short
2790 { "insert_flag", FORMAT_TABLE_STRING,
2791 format_cb_insert_flag
2793 { "keypad_cursor_flag", FORMAT_TABLE_STRING,
2794 format_cb_keypad_cursor_flag
2796 { "keypad_flag", FORMAT_TABLE_STRING,
2797 format_cb_keypad_flag
2799 { "last_window_index", FORMAT_TABLE_STRING,
2800 format_cb_last_window_index
2802 { "mouse_all_flag", FORMAT_TABLE_STRING,
2803 format_cb_mouse_all_flag
2805 { "mouse_any_flag", FORMAT_TABLE_STRING,
2806 format_cb_mouse_any_flag
2808 { "mouse_button_flag", FORMAT_TABLE_STRING,
2809 format_cb_mouse_button_flag
2811 { "mouse_hyperlink", FORMAT_TABLE_STRING,
2812 format_cb_mouse_hyperlink
2814 { "mouse_line", FORMAT_TABLE_STRING,
2815 format_cb_mouse_line
2817 { "mouse_pane", FORMAT_TABLE_STRING,
2818 format_cb_mouse_pane
2820 { "mouse_sgr_flag", FORMAT_TABLE_STRING,
2821 format_cb_mouse_sgr_flag
2823 { "mouse_standard_flag", FORMAT_TABLE_STRING,
2824 format_cb_mouse_standard_flag
2826 { "mouse_utf8_flag", FORMAT_TABLE_STRING,
2827 format_cb_mouse_utf8_flag
2829 { "mouse_word", FORMAT_TABLE_STRING,
2830 format_cb_mouse_word
2832 { "mouse_x", FORMAT_TABLE_STRING,
2833 format_cb_mouse_x
2835 { "mouse_y", FORMAT_TABLE_STRING,
2836 format_cb_mouse_y
2838 { "next_session_id", FORMAT_TABLE_STRING,
2839 format_cb_next_session_id
2841 { "origin_flag", FORMAT_TABLE_STRING,
2842 format_cb_origin_flag
2844 { "pane_active", FORMAT_TABLE_STRING,
2845 format_cb_pane_active
2847 { "pane_at_bottom", FORMAT_TABLE_STRING,
2848 format_cb_pane_at_bottom
2850 { "pane_at_left", FORMAT_TABLE_STRING,
2851 format_cb_pane_at_left
2853 { "pane_at_right", FORMAT_TABLE_STRING,
2854 format_cb_pane_at_right
2856 { "pane_at_top", FORMAT_TABLE_STRING,
2857 format_cb_pane_at_top
2859 { "pane_bg", FORMAT_TABLE_STRING,
2860 format_cb_pane_bg
2862 { "pane_bottom", FORMAT_TABLE_STRING,
2863 format_cb_pane_bottom
2865 { "pane_current_command", FORMAT_TABLE_STRING,
2866 format_cb_current_command
2868 { "pane_current_path", FORMAT_TABLE_STRING,
2869 format_cb_current_path
2871 { "pane_dead", FORMAT_TABLE_STRING,
2872 format_cb_pane_dead
2874 { "pane_dead_signal", FORMAT_TABLE_STRING,
2875 format_cb_pane_dead_signal
2877 { "pane_dead_status", FORMAT_TABLE_STRING,
2878 format_cb_pane_dead_status
2880 { "pane_dead_time", FORMAT_TABLE_TIME,
2881 format_cb_pane_dead_time
2883 { "pane_fg", FORMAT_TABLE_STRING,
2884 format_cb_pane_fg
2886 { "pane_format", FORMAT_TABLE_STRING,
2887 format_cb_pane_format
2889 { "pane_height", FORMAT_TABLE_STRING,
2890 format_cb_pane_height
2892 { "pane_id", FORMAT_TABLE_STRING,
2893 format_cb_pane_id
2895 { "pane_in_mode", FORMAT_TABLE_STRING,
2896 format_cb_pane_in_mode
2898 { "pane_index", FORMAT_TABLE_STRING,
2899 format_cb_pane_index
2901 { "pane_input_off", FORMAT_TABLE_STRING,
2902 format_cb_pane_input_off
2904 { "pane_last", FORMAT_TABLE_STRING,
2905 format_cb_pane_last
2907 { "pane_left", FORMAT_TABLE_STRING,
2908 format_cb_pane_left
2910 { "pane_marked", FORMAT_TABLE_STRING,
2911 format_cb_pane_marked
2913 { "pane_marked_set", FORMAT_TABLE_STRING,
2914 format_cb_pane_marked_set
2916 { "pane_mode", FORMAT_TABLE_STRING,
2917 format_cb_pane_mode
2919 { "pane_path", FORMAT_TABLE_STRING,
2920 format_cb_pane_path
2922 { "pane_pid", FORMAT_TABLE_STRING,
2923 format_cb_pane_pid
2925 { "pane_pipe", FORMAT_TABLE_STRING,
2926 format_cb_pane_pipe
2928 { "pane_right", FORMAT_TABLE_STRING,
2929 format_cb_pane_right
2931 { "pane_search_string", FORMAT_TABLE_STRING,
2932 format_cb_pane_search_string
2934 { "pane_start_command", FORMAT_TABLE_STRING,
2935 format_cb_start_command
2937 { "pane_start_path", FORMAT_TABLE_STRING,
2938 format_cb_start_path
2940 { "pane_synchronized", FORMAT_TABLE_STRING,
2941 format_cb_pane_synchronized
2943 { "pane_tabs", FORMAT_TABLE_STRING,
2944 format_cb_pane_tabs
2946 { "pane_title", FORMAT_TABLE_STRING,
2947 format_cb_pane_title
2949 { "pane_top", FORMAT_TABLE_STRING,
2950 format_cb_pane_top
2952 { "pane_tty", FORMAT_TABLE_STRING,
2953 format_cb_pane_tty
2955 { "pane_width", FORMAT_TABLE_STRING,
2956 format_cb_pane_width
2958 { "pid", FORMAT_TABLE_STRING,
2959 format_cb_pid
2961 { "scroll_region_lower", FORMAT_TABLE_STRING,
2962 format_cb_scroll_region_lower
2964 { "scroll_region_upper", FORMAT_TABLE_STRING,
2965 format_cb_scroll_region_upper
2967 { "session_activity", FORMAT_TABLE_TIME,
2968 format_cb_session_activity
2970 { "session_alerts", FORMAT_TABLE_STRING,
2971 format_cb_session_alerts
2973 { "session_attached", FORMAT_TABLE_STRING,
2974 format_cb_session_attached
2976 { "session_attached_list", FORMAT_TABLE_STRING,
2977 format_cb_session_attached_list
2979 { "session_created", FORMAT_TABLE_TIME,
2980 format_cb_session_created
2982 { "session_format", FORMAT_TABLE_STRING,
2983 format_cb_session_format
2985 { "session_group", FORMAT_TABLE_STRING,
2986 format_cb_session_group
2988 { "session_group_attached", FORMAT_TABLE_STRING,
2989 format_cb_session_group_attached
2991 { "session_group_attached_list", FORMAT_TABLE_STRING,
2992 format_cb_session_group_attached_list
2994 { "session_group_list", FORMAT_TABLE_STRING,
2995 format_cb_session_group_list
2997 { "session_group_many_attached", FORMAT_TABLE_STRING,
2998 format_cb_session_group_many_attached
3000 { "session_group_size", FORMAT_TABLE_STRING,
3001 format_cb_session_group_size
3003 { "session_grouped", FORMAT_TABLE_STRING,
3004 format_cb_session_grouped
3006 { "session_id", FORMAT_TABLE_STRING,
3007 format_cb_session_id
3009 { "session_last_attached", FORMAT_TABLE_TIME,
3010 format_cb_session_last_attached
3012 { "session_many_attached", FORMAT_TABLE_STRING,
3013 format_cb_session_many_attached
3015 { "session_marked", FORMAT_TABLE_STRING,
3016 format_cb_session_marked,
3018 { "session_name", FORMAT_TABLE_STRING,
3019 format_cb_session_name
3021 { "session_path", FORMAT_TABLE_STRING,
3022 format_cb_session_path
3024 { "session_stack", FORMAT_TABLE_STRING,
3025 format_cb_session_stack
3027 { "session_windows", FORMAT_TABLE_STRING,
3028 format_cb_session_windows
3030 { "socket_path", FORMAT_TABLE_STRING,
3031 format_cb_socket_path
3033 { "start_time", FORMAT_TABLE_TIME,
3034 format_cb_start_time
3036 { "tree_mode_format", FORMAT_TABLE_STRING,
3037 format_cb_tree_mode_format
3039 { "uid", FORMAT_TABLE_STRING,
3040 format_cb_uid
3042 { "user", FORMAT_TABLE_STRING,
3043 format_cb_user
3045 { "version", FORMAT_TABLE_STRING,
3046 format_cb_version
3048 { "window_active", FORMAT_TABLE_STRING,
3049 format_cb_window_active
3051 { "window_active_clients", FORMAT_TABLE_STRING,
3052 format_cb_window_active_clients
3054 { "window_active_clients_list", FORMAT_TABLE_STRING,
3055 format_cb_window_active_clients_list
3057 { "window_active_sessions", FORMAT_TABLE_STRING,
3058 format_cb_window_active_sessions
3060 { "window_active_sessions_list", FORMAT_TABLE_STRING,
3061 format_cb_window_active_sessions_list
3063 { "window_activity", FORMAT_TABLE_TIME,
3064 format_cb_window_activity
3066 { "window_activity_flag", FORMAT_TABLE_STRING,
3067 format_cb_window_activity_flag
3069 { "window_bell_flag", FORMAT_TABLE_STRING,
3070 format_cb_window_bell_flag
3072 { "window_bigger", FORMAT_TABLE_STRING,
3073 format_cb_window_bigger
3075 { "window_cell_height", FORMAT_TABLE_STRING,
3076 format_cb_window_cell_height
3078 { "window_cell_width", FORMAT_TABLE_STRING,
3079 format_cb_window_cell_width
3081 { "window_end_flag", FORMAT_TABLE_STRING,
3082 format_cb_window_end_flag
3084 { "window_flags", FORMAT_TABLE_STRING,
3085 format_cb_window_flags
3087 { "window_format", FORMAT_TABLE_STRING,
3088 format_cb_window_format
3090 { "window_height", FORMAT_TABLE_STRING,
3091 format_cb_window_height
3093 { "window_id", FORMAT_TABLE_STRING,
3094 format_cb_window_id
3096 { "window_index", FORMAT_TABLE_STRING,
3097 format_cb_window_index
3099 { "window_last_flag", FORMAT_TABLE_STRING,
3100 format_cb_window_last_flag
3102 { "window_layout", FORMAT_TABLE_STRING,
3103 format_cb_window_layout
3105 { "window_linked", FORMAT_TABLE_STRING,
3106 format_cb_window_linked
3108 { "window_linked_sessions", FORMAT_TABLE_STRING,
3109 format_cb_window_linked_sessions
3111 { "window_linked_sessions_list", FORMAT_TABLE_STRING,
3112 format_cb_window_linked_sessions_list
3114 { "window_marked_flag", FORMAT_TABLE_STRING,
3115 format_cb_window_marked_flag
3117 { "window_name", FORMAT_TABLE_STRING,
3118 format_cb_window_name
3120 { "window_offset_x", FORMAT_TABLE_STRING,
3121 format_cb_window_offset_x
3123 { "window_offset_y", FORMAT_TABLE_STRING,
3124 format_cb_window_offset_y
3126 { "window_panes", FORMAT_TABLE_STRING,
3127 format_cb_window_panes
3129 { "window_raw_flags", FORMAT_TABLE_STRING,
3130 format_cb_window_raw_flags
3132 { "window_silence_flag", FORMAT_TABLE_STRING,
3133 format_cb_window_silence_flag
3135 { "window_stack_index", FORMAT_TABLE_STRING,
3136 format_cb_window_stack_index
3138 { "window_start_flag", FORMAT_TABLE_STRING,
3139 format_cb_window_start_flag
3141 { "window_visible_layout", FORMAT_TABLE_STRING,
3142 format_cb_window_visible_layout
3144 { "window_width", FORMAT_TABLE_STRING,
3145 format_cb_window_width
3147 { "window_zoomed_flag", FORMAT_TABLE_STRING,
3148 format_cb_window_zoomed_flag
3150 { "wrap_flag", FORMAT_TABLE_STRING,
3151 format_cb_wrap_flag
3155 /* Compare format table entries. */
3156 static int
3157 format_table_compare(const void *key0, const void *entry0)
3159 const char *key = key0;
3160 const struct format_table_entry *entry = entry0;
3162 return (strcmp(key, entry->key));
3165 /* Get a format callback. */
3166 static struct format_table_entry *
3167 format_table_get(const char *key)
3169 return (bsearch(key, format_table, nitems(format_table),
3170 sizeof *format_table, format_table_compare));
3173 /* Merge one format tree into another. */
3174 void
3175 format_merge(struct format_tree *ft, struct format_tree *from)
3177 struct format_entry *fe;
3179 RB_FOREACH(fe, format_entry_tree, &from->tree) {
3180 if (fe->value != NULL)
3181 format_add(ft, fe->key, "%s", fe->value);
3185 /* Get format pane. */
3186 struct window_pane *
3187 format_get_pane(struct format_tree *ft)
3189 return (ft->wp);
3192 /* Add item bits to tree. */
3193 static void
3194 format_create_add_item(struct format_tree *ft, struct cmdq_item *item)
3196 struct key_event *event = cmdq_get_event(item);
3197 struct mouse_event *m = &event->m;
3199 cmdq_merge_formats(item, ft);
3200 memcpy(&ft->m, m, sizeof ft->m);
3203 /* Create a new tree. */
3204 struct format_tree *
3205 format_create(struct client *c, struct cmdq_item *item, int tag, int flags)
3207 struct format_tree *ft;
3209 ft = xcalloc(1, sizeof *ft);
3210 RB_INIT(&ft->tree);
3212 if (c != NULL) {
3213 ft->client = c;
3214 ft->client->references++;
3216 ft->item = item;
3218 ft->tag = tag;
3219 ft->flags = flags;
3221 if (item != NULL)
3222 format_create_add_item(ft, item);
3224 return (ft);
3227 /* Free a tree. */
3228 void
3229 format_free(struct format_tree *ft)
3231 struct format_entry *fe, *fe1;
3233 RB_FOREACH_SAFE(fe, format_entry_tree, &ft->tree, fe1) {
3234 RB_REMOVE(format_entry_tree, &ft->tree, fe);
3235 free(fe->value);
3236 free(fe->key);
3237 free(fe);
3240 if (ft->client != NULL)
3241 server_client_unref(ft->client);
3242 free(ft);
3245 /* Log each format. */
3246 static void
3247 format_log_debug_cb(const char *key, const char *value, void *arg)
3249 const char *prefix = arg;
3251 log_debug("%s: %s=%s", prefix, key, value);
3254 /* Log a format tree. */
3255 void
3256 format_log_debug(struct format_tree *ft, const char *prefix)
3258 format_each(ft, format_log_debug_cb, (void *)prefix);
3261 /* Walk each format. */
3262 void
3263 format_each(struct format_tree *ft, void (*cb)(const char *, const char *,
3264 void *), void *arg)
3266 const struct format_table_entry *fte;
3267 struct format_entry *fe;
3268 u_int i;
3269 char s[64];
3270 void *value;
3271 struct timeval *tv;
3273 for (i = 0; i < nitems(format_table); i++) {
3274 fte = &format_table[i];
3276 value = fte->cb(ft);
3277 if (value == NULL)
3278 continue;
3279 if (fte->type == FORMAT_TABLE_TIME) {
3280 tv = value;
3281 xsnprintf(s, sizeof s, "%lld", (long long)tv->tv_sec);
3282 cb(fte->key, s, arg);
3283 } else {
3284 cb(fte->key, value, arg);
3285 free(value);
3288 RB_FOREACH(fe, format_entry_tree, &ft->tree) {
3289 if (fe->time != 0) {
3290 xsnprintf(s, sizeof s, "%lld", (long long)fe->time);
3291 cb(fe->key, s, arg);
3292 } else {
3293 if (fe->value == NULL && fe->cb != NULL) {
3294 fe->value = fe->cb(ft);
3295 if (fe->value == NULL)
3296 fe->value = xstrdup("");
3298 cb(fe->key, fe->value, arg);
3303 /* Add a key-value pair. */
3304 void
3305 format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
3307 struct format_entry *fe;
3308 struct format_entry *fe_now;
3309 va_list ap;
3311 fe = xmalloc(sizeof *fe);
3312 fe->key = xstrdup(key);
3314 fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
3315 if (fe_now != NULL) {
3316 free(fe->key);
3317 free(fe);
3318 free(fe_now->value);
3319 fe = fe_now;
3322 fe->cb = NULL;
3323 fe->time = 0;
3325 va_start(ap, fmt);
3326 xvasprintf(&fe->value, fmt, ap);
3327 va_end(ap);
3330 /* Add a key and time. */
3331 void
3332 format_add_tv(struct format_tree *ft, const char *key, struct timeval *tv)
3334 struct format_entry *fe, *fe_now;
3336 fe = xmalloc(sizeof *fe);
3337 fe->key = xstrdup(key);
3339 fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
3340 if (fe_now != NULL) {
3341 free(fe->key);
3342 free(fe);
3343 free(fe_now->value);
3344 fe = fe_now;
3347 fe->cb = NULL;
3348 fe->time = tv->tv_sec;
3350 fe->value = NULL;
3353 /* Add a key and function. */
3354 void
3355 format_add_cb(struct format_tree *ft, const char *key, format_cb cb)
3357 struct format_entry *fe;
3358 struct format_entry *fe_now;
3360 fe = xmalloc(sizeof *fe);
3361 fe->key = xstrdup(key);
3363 fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
3364 if (fe_now != NULL) {
3365 free(fe->key);
3366 free(fe);
3367 free(fe_now->value);
3368 fe = fe_now;
3371 fe->cb = cb;
3372 fe->time = 0;
3374 fe->value = NULL;
3377 /* Quote shell special characters in string. */
3378 static char *
3379 format_quote_shell(const char *s)
3381 const char *cp;
3382 char *out, *at;
3384 at = out = xmalloc(strlen(s) * 2 + 1);
3385 for (cp = s; *cp != '\0'; cp++) {
3386 if (strchr("|&;<>()$`\\\"'*?[# =%", *cp) != NULL)
3387 *at++ = '\\';
3388 *at++ = *cp;
3390 *at = '\0';
3391 return (out);
3394 /* Quote #s in string. */
3395 static char *
3396 format_quote_style(const char *s)
3398 const char *cp;
3399 char *out, *at;
3401 at = out = xmalloc(strlen(s) * 2 + 1);
3402 for (cp = s; *cp != '\0'; cp++) {
3403 if (*cp == '#')
3404 *at++ = '#';
3405 *at++ = *cp;
3407 *at = '\0';
3408 return (out);
3411 /* Make a prettier time. */
3412 char *
3413 format_pretty_time(time_t t, int seconds)
3415 struct tm now_tm, tm;
3416 time_t now, age;
3417 char s[9];
3419 time(&now);
3420 if (now < t)
3421 now = t;
3422 age = now - t;
3424 localtime_r(&now, &now_tm);
3425 localtime_r(&t, &tm);
3427 /* Last 24 hours. */
3428 if (age < 24 * 3600) {
3429 if (seconds)
3430 strftime(s, sizeof s, "%H:%M:%S", &tm);
3431 else
3432 strftime(s, sizeof s, "%H:%M", &tm);
3433 return (xstrdup(s));
3436 /* This month or last 28 days. */
3437 if ((tm.tm_year == now_tm.tm_year && tm.tm_mon == now_tm.tm_mon) ||
3438 age < 28 * 24 * 3600) {
3439 strftime(s, sizeof s, "%a%d", &tm);
3440 return (xstrdup(s));
3443 /* Last 12 months. */
3444 if ((tm.tm_year == now_tm.tm_year && tm.tm_mon < now_tm.tm_mon) ||
3445 (tm.tm_year == now_tm.tm_year - 1 && tm.tm_mon > now_tm.tm_mon)) {
3446 strftime(s, sizeof s, "%d%b", &tm);
3447 return (xstrdup(s));
3450 /* Older than that. */
3451 strftime(s, sizeof s, "%h%y", &tm);
3452 return (xstrdup(s));
3455 /* Find a format entry. */
3456 static char *
3457 format_find(struct format_tree *ft, const char *key, int modifiers,
3458 const char *time_format)
3460 struct format_table_entry *fte;
3461 void *value;
3462 struct format_entry *fe, fe_find;
3463 struct environ_entry *envent;
3464 struct options_entry *o;
3465 int idx;
3466 char *found = NULL, *saved, s[512];
3467 const char *errstr;
3468 time_t t = 0;
3469 struct tm tm;
3471 o = options_parse_get(global_options, key, &idx, 0);
3472 if (o == NULL && ft->wp != NULL)
3473 o = options_parse_get(ft->wp->options, key, &idx, 0);
3474 if (o == NULL && ft->w != NULL)
3475 o = options_parse_get(ft->w->options, key, &idx, 0);
3476 if (o == NULL)
3477 o = options_parse_get(global_w_options, key, &idx, 0);
3478 if (o == NULL && ft->s != NULL)
3479 o = options_parse_get(ft->s->options, key, &idx, 0);
3480 if (o == NULL)
3481 o = options_parse_get(global_s_options, key, &idx, 0);
3482 if (o != NULL) {
3483 found = options_to_string(o, idx, 1);
3484 goto found;
3487 fte = format_table_get(key);
3488 if (fte != NULL) {
3489 value = fte->cb(ft);
3490 if (fte->type == FORMAT_TABLE_TIME && value != NULL)
3491 t = ((struct timeval *)value)->tv_sec;
3492 else
3493 found = value;
3494 goto found;
3496 fe_find.key = (char *)key;
3497 fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find);
3498 if (fe != NULL) {
3499 if (fe->time != 0) {
3500 t = fe->time;
3501 goto found;
3503 if (fe->value == NULL && fe->cb != NULL) {
3504 fe->value = fe->cb(ft);
3505 if (fe->value == NULL)
3506 fe->value = xstrdup("");
3508 found = xstrdup(fe->value);
3509 goto found;
3512 if (~modifiers & FORMAT_TIMESTRING) {
3513 envent = NULL;
3514 if (ft->s != NULL)
3515 envent = environ_find(ft->s->environ, key);
3516 if (envent == NULL)
3517 envent = environ_find(global_environ, key);
3518 if (envent != NULL && envent->value != NULL) {
3519 found = xstrdup(envent->value);
3520 goto found;
3524 return (NULL);
3526 found:
3527 if (modifiers & FORMAT_TIMESTRING) {
3528 if (t == 0 && found != NULL) {
3529 t = strtonum(found, 0, INT64_MAX, &errstr);
3530 if (errstr != NULL)
3531 t = 0;
3532 free(found);
3534 if (t == 0)
3535 return (NULL);
3536 if (modifiers & FORMAT_PRETTY)
3537 found = format_pretty_time(t, 0);
3538 else {
3539 if (time_format != NULL) {
3540 localtime_r(&t, &tm);
3541 strftime(s, sizeof s, time_format, &tm);
3542 } else {
3543 ctime_r(&t, s);
3544 s[strcspn(s, "\n")] = '\0';
3546 found = xstrdup(s);
3548 return (found);
3551 if (t != 0)
3552 xasprintf(&found, "%lld", (long long)t);
3553 else if (found == NULL)
3554 return (NULL);
3555 if (modifiers & FORMAT_BASENAME) {
3556 saved = found;
3557 found = xstrdup(basename(saved));
3558 free(saved);
3560 if (modifiers & FORMAT_DIRNAME) {
3561 saved = found;
3562 found = xstrdup(dirname(saved));
3563 free(saved);
3565 if (modifiers & FORMAT_QUOTE_SHELL) {
3566 saved = found;
3567 found = format_quote_shell(saved);
3568 free(saved);
3570 if (modifiers & FORMAT_QUOTE_STYLE) {
3571 saved = found;
3572 found = format_quote_style(saved);
3573 free(saved);
3575 return (found);
3578 /* Unescape escaped characters. */
3579 static char *
3580 format_unescape(const char *s)
3582 char *out, *cp;
3583 int brackets = 0;
3585 cp = out = xmalloc(strlen(s) + 1);
3586 for (; *s != '\0'; s++) {
3587 if (*s == '#' && s[1] == '{')
3588 brackets++;
3589 if (brackets == 0 &&
3590 *s == '#' &&
3591 strchr(",#{}:", s[1]) != NULL) {
3592 *cp++ = *++s;
3593 continue;
3595 if (*s == '}')
3596 brackets--;
3597 *cp++ = *s;
3599 *cp = '\0';
3600 return (out);
3603 /* Remove escaped characters. */
3604 static char *
3605 format_strip(const char *s)
3607 char *out, *cp;
3608 int brackets = 0;
3610 cp = out = xmalloc(strlen(s) + 1);
3611 for (; *s != '\0'; s++) {
3612 if (*s == '#' && s[1] == '{')
3613 brackets++;
3614 if (*s == '#' && strchr(",#{}:", s[1]) != NULL) {
3615 if (brackets != 0)
3616 *cp++ = *s;
3617 continue;
3619 if (*s == '}')
3620 brackets--;
3621 *cp++ = *s;
3623 *cp = '\0';
3624 return (out);
3627 /* Skip until end. */
3628 const char *
3629 format_skip(const char *s, const char *end)
3631 int brackets = 0;
3633 for (; *s != '\0'; s++) {
3634 if (*s == '#' && s[1] == '{')
3635 brackets++;
3636 if (*s == '#' && strchr(",#{}:", s[1]) != NULL) {
3637 s++;
3638 continue;
3640 if (*s == '}')
3641 brackets--;
3642 if (strchr(end, *s) != NULL && brackets == 0)
3643 break;
3645 if (*s == '\0')
3646 return (NULL);
3647 return (s);
3650 /* Return left and right alternatives separated by commas. */
3651 static int
3652 format_choose(struct format_expand_state *es, const char *s, char **left,
3653 char **right, int expand)
3655 const char *cp;
3656 char *left0, *right0;
3658 cp = format_skip(s, ",");
3659 if (cp == NULL)
3660 return (-1);
3661 left0 = xstrndup(s, cp - s);
3662 right0 = xstrdup(cp + 1);
3664 if (expand) {
3665 *left = format_expand1(es, left0);
3666 free(left0);
3667 *right = format_expand1(es, right0);
3668 free(right0);
3669 } else {
3670 *left = left0;
3671 *right = right0;
3673 return (0);
3676 /* Is this true? */
3678 format_true(const char *s)
3680 if (s != NULL && *s != '\0' && (s[0] != '0' || s[1] != '\0'))
3681 return (1);
3682 return (0);
3685 /* Check if modifier end. */
3686 static int
3687 format_is_end(char c)
3689 return (c == ';' || c == ':');
3692 /* Add to modifier list. */
3693 static void
3694 format_add_modifier(struct format_modifier **list, u_int *count,
3695 const char *c, size_t n, char **argv, int argc)
3697 struct format_modifier *fm;
3699 *list = xreallocarray(*list, (*count) + 1, sizeof **list);
3700 fm = &(*list)[(*count)++];
3702 memcpy(fm->modifier, c, n);
3703 fm->modifier[n] = '\0';
3704 fm->size = n;
3706 fm->argv = argv;
3707 fm->argc = argc;
3710 /* Free modifier list. */
3711 static void
3712 format_free_modifiers(struct format_modifier *list, u_int count)
3714 u_int i;
3716 for (i = 0; i < count; i++)
3717 cmd_free_argv(list[i].argc, list[i].argv);
3718 free(list);
3721 /* Build modifier list. */
3722 static struct format_modifier *
3723 format_build_modifiers(struct format_expand_state *es, const char **s,
3724 u_int *count)
3726 const char *cp = *s, *end;
3727 struct format_modifier *list = NULL;
3728 char c, last[] = "X;:", **argv, *value;
3729 int argc;
3732 * Modifiers are a ; separated list of the forms:
3733 * l,m,C,a,b,c,d,n,t,w,q,E,T,S,W,P,<,>
3734 * =a
3735 * =/a
3736 * =/a/
3737 * s/a/b/
3738 * s/a/b
3739 * ||,&&,!=,==,<=,>=
3742 *count = 0;
3744 while (*cp != '\0' && *cp != ':') {
3745 /* Skip any separator character. */
3746 if (*cp == ';')
3747 cp++;
3749 /* Check single character modifiers with no arguments. */
3750 if (strchr("labcdnwETSWP<>", cp[0]) != NULL &&
3751 format_is_end(cp[1])) {
3752 format_add_modifier(&list, count, cp, 1, NULL, 0);
3753 cp++;
3754 continue;
3757 /* Then try double character with no arguments. */
3758 if ((memcmp("||", cp, 2) == 0 ||
3759 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 format_is_end(cp[2])) {
3765 format_add_modifier(&list, count, cp, 2, NULL, 0);
3766 cp += 2;
3767 continue;
3770 /* Now try single character with arguments. */
3771 if (strchr("mCNst=peq", cp[0]) == NULL)
3772 break;
3773 c = cp[0];
3775 /* No arguments provided. */
3776 if (format_is_end(cp[1])) {
3777 format_add_modifier(&list, count, cp, 1, NULL, 0);
3778 cp++;
3779 continue;
3781 argv = NULL;
3782 argc = 0;
3784 /* Single argument with no wrapper character. */
3785 if (!ispunct(cp[1]) || cp[1] == '-') {
3786 end = format_skip(cp + 1, ":;");
3787 if (end == NULL)
3788 break;
3790 argv = xcalloc(1, sizeof *argv);
3791 value = xstrndup(cp + 1, end - (cp + 1));
3792 argv[0] = format_expand1(es, value);
3793 free(value);
3794 argc = 1;
3796 format_add_modifier(&list, count, &c, 1, argv, argc);
3797 cp = end;
3798 continue;
3801 /* Multiple arguments with a wrapper character. */
3802 last[0] = cp[1];
3803 cp++;
3804 do {
3805 if (cp[0] == last[0] && format_is_end(cp[1])) {
3806 cp++;
3807 break;
3809 end = format_skip(cp + 1, last);
3810 if (end == NULL)
3811 break;
3812 cp++;
3814 argv = xreallocarray(argv, argc + 1, sizeof *argv);
3815 value = xstrndup(cp, end - cp);
3816 argv[argc++] = format_expand1(es, value);
3817 free(value);
3819 cp = end;
3820 } while (!format_is_end(cp[0]));
3821 format_add_modifier(&list, count, &c, 1, argv, argc);
3823 if (*cp != ':') {
3824 format_free_modifiers(list, *count);
3825 *count = 0;
3826 return (NULL);
3828 *s = cp + 1;
3829 return (list);
3832 /* Match against an fnmatch(3) pattern or regular expression. */
3833 static char *
3834 format_match(struct format_modifier *fm, const char *pattern, const char *text)
3836 const char *s = "";
3837 regex_t r;
3838 int flags = 0;
3840 if (fm->argc >= 1)
3841 s = fm->argv[0];
3842 if (strchr(s, 'r') == NULL) {
3843 if (strchr(s, 'i') != NULL)
3844 flags |= FNM_CASEFOLD;
3845 if (fnmatch(pattern, text, flags) != 0)
3846 return (xstrdup("0"));
3847 } else {
3848 flags = REG_EXTENDED|REG_NOSUB;
3849 if (strchr(s, 'i') != NULL)
3850 flags |= REG_ICASE;
3851 if (regcomp(&r, pattern, flags) != 0)
3852 return (xstrdup("0"));
3853 if (regexec(&r, text, 0, NULL, 0) != 0) {
3854 regfree(&r);
3855 return (xstrdup("0"));
3857 regfree(&r);
3859 return (xstrdup("1"));
3862 /* Perform substitution in string. */
3863 static char *
3864 format_sub(struct format_modifier *fm, const char *text, const char *pattern,
3865 const char *with)
3867 char *value;
3868 int flags = REG_EXTENDED;
3870 if (fm->argc >= 3 && strchr(fm->argv[2], 'i') != NULL)
3871 flags |= REG_ICASE;
3872 value = regsub(pattern, with, text, flags);
3873 if (value == NULL)
3874 return (xstrdup(text));
3875 return (value);
3878 /* Search inside pane. */
3879 static char *
3880 format_search(struct format_modifier *fm, struct window_pane *wp, const char *s)
3882 int ignore = 0, regex = 0;
3883 char *value;
3885 if (fm->argc >= 1) {
3886 if (strchr(fm->argv[0], 'i') != NULL)
3887 ignore = 1;
3888 if (strchr(fm->argv[0], 'r') != NULL)
3889 regex = 1;
3891 xasprintf(&value, "%u", window_pane_search(wp, s, regex, ignore));
3892 return (value);
3895 /* Does session name exist? */
3896 static char *
3897 format_session_name(struct format_expand_state *es, const char *fmt)
3899 char *name;
3900 struct session *s;
3902 name = format_expand1(es, fmt);
3903 RB_FOREACH(s, sessions, &sessions) {
3904 if (strcmp(s->name, name) == 0) {
3905 free(name);
3906 return (xstrdup("1"));
3909 free(name);
3910 return (xstrdup("0"));
3913 /* Loop over sessions. */
3914 static char *
3915 format_loop_sessions(struct format_expand_state *es, const char *fmt)
3917 struct format_tree *ft = es->ft;
3918 struct client *c = ft->client;
3919 struct cmdq_item *item = ft->item;
3920 struct format_tree *nft;
3921 struct format_expand_state next;
3922 char *expanded, *value;
3923 size_t valuelen;
3924 struct session *s;
3926 value = xcalloc(1, 1);
3927 valuelen = 1;
3929 RB_FOREACH(s, sessions, &sessions) {
3930 format_log(es, "session loop: $%u", s->id);
3931 nft = format_create(c, item, FORMAT_NONE, ft->flags);
3932 format_defaults(nft, ft->c, s, NULL, NULL);
3933 format_copy_state(&next, es, 0);
3934 next.ft = nft;
3935 expanded = format_expand1(&next, fmt);
3936 format_free(next.ft);
3938 valuelen += strlen(expanded);
3939 value = xrealloc(value, valuelen);
3941 strlcat(value, expanded, valuelen);
3942 free(expanded);
3945 return (value);
3948 /* Does window name exist? */
3949 static char *
3950 format_window_name(struct format_expand_state *es, const char *fmt)
3952 struct format_tree *ft = es->ft;
3953 char *name;
3954 struct winlink *wl;
3956 if (ft->s == NULL) {
3957 format_log(es, "window name but no session");
3958 return (NULL);
3961 name = format_expand1(es, fmt);
3962 RB_FOREACH(wl, winlinks, &ft->s->windows) {
3963 if (strcmp(wl->window->name, name) == 0) {
3964 free(name);
3965 return (xstrdup("1"));
3968 free(name);
3969 return (xstrdup("0"));
3972 /* Loop over windows. */
3973 static char *
3974 format_loop_windows(struct format_expand_state *es, const char *fmt)
3976 struct format_tree *ft = es->ft;
3977 struct client *c = ft->client;
3978 struct cmdq_item *item = ft->item;
3979 struct format_tree *nft;
3980 struct format_expand_state next;
3981 char *all, *active, *use, *expanded, *value;
3982 size_t valuelen;
3983 struct winlink *wl;
3984 struct window *w;
3986 if (ft->s == NULL) {
3987 format_log(es, "window loop but no session");
3988 return (NULL);
3991 if (format_choose(es, fmt, &all, &active, 0) != 0) {
3992 all = xstrdup(fmt);
3993 active = NULL;
3996 value = xcalloc(1, 1);
3997 valuelen = 1;
3999 RB_FOREACH(wl, winlinks, &ft->s->windows) {
4000 w = wl->window;
4001 format_log(es, "window loop: %u @%u", wl->idx, w->id);
4002 if (active != NULL && wl == ft->s->curw)
4003 use = active;
4004 else
4005 use = all;
4006 nft = format_create(c, item, FORMAT_WINDOW|w->id, ft->flags);
4007 format_defaults(nft, ft->c, ft->s, wl, NULL);
4008 format_copy_state(&next, es, 0);
4009 next.ft = nft;
4010 expanded = format_expand1(&next, use);
4011 format_free(nft);
4013 valuelen += strlen(expanded);
4014 value = xrealloc(value, valuelen);
4016 strlcat(value, expanded, valuelen);
4017 free(expanded);
4020 free(active);
4021 free(all);
4023 return (value);
4026 /* Loop over panes. */
4027 static char *
4028 format_loop_panes(struct format_expand_state *es, const char *fmt)
4030 struct format_tree *ft = es->ft;
4031 struct client *c = ft->client;
4032 struct cmdq_item *item = ft->item;
4033 struct format_tree *nft;
4034 struct format_expand_state next;
4035 char *all, *active, *use, *expanded, *value;
4036 size_t valuelen;
4037 struct window_pane *wp;
4039 if (ft->w == NULL) {
4040 format_log(es, "pane loop but no window");
4041 return (NULL);
4044 if (format_choose(es, fmt, &all, &active, 0) != 0) {
4045 all = xstrdup(fmt);
4046 active = NULL;
4049 value = xcalloc(1, 1);
4050 valuelen = 1;
4052 TAILQ_FOREACH(wp, &ft->w->panes, entry) {
4053 format_log(es, "pane loop: %%%u", wp->id);
4054 if (active != NULL && wp == ft->w->active)
4055 use = active;
4056 else
4057 use = all;
4058 nft = format_create(c, item, FORMAT_PANE|wp->id, ft->flags);
4059 format_defaults(nft, ft->c, ft->s, ft->wl, wp);
4060 format_copy_state(&next, es, 0);
4061 next.ft = nft;
4062 expanded = format_expand1(&next, use);
4063 format_free(nft);
4065 valuelen += strlen(expanded);
4066 value = xrealloc(value, valuelen);
4068 strlcat(value, expanded, valuelen);
4069 free(expanded);
4072 free(active);
4073 free(all);
4075 return (value);
4078 static char *
4079 format_replace_expression(struct format_modifier *mexp,
4080 struct format_expand_state *es, const char *copy)
4082 int argc = mexp->argc;
4083 const char *errstr;
4084 char *endch, *value, *left = NULL, *right = NULL;
4085 int use_fp = 0;
4086 u_int prec = 0;
4087 double mleft, mright, result;
4088 enum { ADD,
4089 SUBTRACT,
4090 MULTIPLY,
4091 DIVIDE,
4092 MODULUS,
4093 EQUAL,
4094 NOT_EQUAL,
4095 GREATER_THAN,
4096 GREATER_THAN_EQUAL,
4097 LESS_THAN,
4098 LESS_THAN_EQUAL } operator;
4100 if (strcmp(mexp->argv[0], "+") == 0)
4101 operator = ADD;
4102 else if (strcmp(mexp->argv[0], "-") == 0)
4103 operator = SUBTRACT;
4104 else if (strcmp(mexp->argv[0], "*") == 0)
4105 operator = MULTIPLY;
4106 else if (strcmp(mexp->argv[0], "/") == 0)
4107 operator = DIVIDE;
4108 else if (strcmp(mexp->argv[0], "%") == 0 ||
4109 strcmp(mexp->argv[0], "m") == 0)
4110 operator = MODULUS;
4111 else if (strcmp(mexp->argv[0], "==") == 0)
4112 operator = EQUAL;
4113 else if (strcmp(mexp->argv[0], "!=") == 0)
4114 operator = NOT_EQUAL;
4115 else if (strcmp(mexp->argv[0], ">") == 0)
4116 operator = GREATER_THAN;
4117 else if (strcmp(mexp->argv[0], "<") == 0)
4118 operator = LESS_THAN;
4119 else if (strcmp(mexp->argv[0], ">=") == 0)
4120 operator = GREATER_THAN_EQUAL;
4121 else if (strcmp(mexp->argv[0], "<=") == 0)
4122 operator = LESS_THAN_EQUAL;
4123 else {
4124 format_log(es, "expression has no valid operator: '%s'",
4125 mexp->argv[0]);
4126 goto fail;
4129 /* The second argument may be flags. */
4130 if (argc >= 2 && strchr(mexp->argv[1], 'f') != NULL) {
4131 use_fp = 1;
4132 prec = 2;
4135 /* The third argument may be precision. */
4136 if (argc >= 3) {
4137 prec = strtonum(mexp->argv[2], INT_MIN, INT_MAX, &errstr);
4138 if (errstr != NULL) {
4139 format_log(es, "expression precision %s: %s", errstr,
4140 mexp->argv[2]);
4141 goto fail;
4145 if (format_choose(es, copy, &left, &right, 1) != 0) {
4146 format_log(es, "expression syntax error");
4147 goto fail;
4150 mleft = strtod(left, &endch);
4151 if (*endch != '\0') {
4152 format_log(es, "expression left side is invalid: %s", left);
4153 goto fail;
4156 mright = strtod(right, &endch);
4157 if (*endch != '\0') {
4158 format_log(es, "expression right side is invalid: %s", right);
4159 goto fail;
4162 if (!use_fp) {
4163 mleft = (long long)mleft;
4164 mright = (long long)mright;
4166 format_log(es, "expression left side is: %.*f", prec, mleft);
4167 format_log(es, "expression right side is: %.*f", prec, mright);
4169 switch (operator) {
4170 case ADD:
4171 result = mleft + mright;
4172 break;
4173 case SUBTRACT:
4174 result = mleft - mright;
4175 break;
4176 case MULTIPLY:
4177 result = mleft * mright;
4178 break;
4179 case DIVIDE:
4180 result = mleft / mright;
4181 break;
4182 case MODULUS:
4183 result = fmod(mleft, mright);
4184 break;
4185 case EQUAL:
4186 result = fabs(mleft - mright) < 1e-9;
4187 break;
4188 case NOT_EQUAL:
4189 result = fabs(mleft - mright) > 1e-9;
4190 break;
4191 case GREATER_THAN:
4192 result = (mleft > mright);
4193 break;
4194 case GREATER_THAN_EQUAL:
4195 result = (mleft >= mright);
4196 break;
4197 case LESS_THAN:
4198 result = (mleft < mright);
4199 break;
4200 case LESS_THAN_EQUAL:
4201 result = (mleft <= mright);
4202 break;
4204 if (use_fp)
4205 xasprintf(&value, "%.*f", prec, result);
4206 else
4207 xasprintf(&value, "%.*f", prec, (double)(long long)result);
4208 format_log(es, "expression result is %s", value);
4210 free(right);
4211 free(left);
4212 return (value);
4214 fail:
4215 free(right);
4216 free(left);
4217 return (NULL);
4220 /* Replace a key. */
4221 static int
4222 format_replace(struct format_expand_state *es, const char *key, size_t keylen,
4223 char **buf, size_t *len, size_t *off)
4225 struct format_tree *ft = es->ft;
4226 struct window_pane *wp = ft->wp;
4227 const char *errstr, *copy, *cp, *marker = NULL;
4228 const char *time_format = NULL;
4229 char *copy0, *condition, *found, *new;
4230 char *value, *left, *right;
4231 size_t valuelen;
4232 int modifiers = 0, limit = 0, width = 0;
4233 int j, c;
4234 struct format_modifier *list, *cmp = NULL, *search = NULL;
4235 struct format_modifier **sub = NULL, *mexp = NULL, *fm;
4236 u_int i, count, nsub = 0;
4237 struct format_expand_state next;
4239 /* Make a copy of the key. */
4240 copy = copy0 = xstrndup(key, keylen);
4242 /* Process modifier list. */
4243 list = format_build_modifiers(es, &copy, &count);
4244 for (i = 0; i < count; i++) {
4245 fm = &list[i];
4246 if (format_logging(ft)) {
4247 format_log(es, "modifier %u is %s", i, fm->modifier);
4248 for (j = 0; j < fm->argc; j++) {
4249 format_log(es, "modifier %u argument %d: %s", i,
4250 j, fm->argv[j]);
4253 if (fm->size == 1) {
4254 switch (fm->modifier[0]) {
4255 case 'm':
4256 case '<':
4257 case '>':
4258 cmp = fm;
4259 break;
4260 case 'C':
4261 search = fm;
4262 break;
4263 case 's':
4264 if (fm->argc < 2)
4265 break;
4266 sub = xreallocarray(sub, nsub + 1, sizeof *sub);
4267 sub[nsub++] = fm;
4268 break;
4269 case '=':
4270 if (fm->argc < 1)
4271 break;
4272 limit = strtonum(fm->argv[0], INT_MIN, INT_MAX,
4273 &errstr);
4274 if (errstr != NULL)
4275 limit = 0;
4276 if (fm->argc >= 2 && fm->argv[1] != NULL)
4277 marker = fm->argv[1];
4278 break;
4279 case 'p':
4280 if (fm->argc < 1)
4281 break;
4282 width = strtonum(fm->argv[0], INT_MIN, INT_MAX,
4283 &errstr);
4284 if (errstr != NULL)
4285 width = 0;
4286 break;
4287 case 'w':
4288 modifiers |= FORMAT_WIDTH;
4289 break;
4290 case 'e':
4291 if (fm->argc < 1 || fm->argc > 3)
4292 break;
4293 mexp = fm;
4294 break;
4295 case 'l':
4296 modifiers |= FORMAT_LITERAL;
4297 break;
4298 case 'a':
4299 modifiers |= FORMAT_CHARACTER;
4300 break;
4301 case 'b':
4302 modifiers |= FORMAT_BASENAME;
4303 break;
4304 case 'c':
4305 modifiers |= FORMAT_COLOUR;
4306 break;
4307 case 'd':
4308 modifiers |= FORMAT_DIRNAME;
4309 break;
4310 case 'n':
4311 modifiers |= FORMAT_LENGTH;
4312 break;
4313 case 't':
4314 modifiers |= FORMAT_TIMESTRING;
4315 if (fm->argc < 1)
4316 break;
4317 if (strchr(fm->argv[0], 'p') != NULL)
4318 modifiers |= FORMAT_PRETTY;
4319 else if (fm->argc >= 2 &&
4320 strchr(fm->argv[0], 'f') != NULL)
4321 time_format = format_strip(fm->argv[1]);
4322 break;
4323 case 'q':
4324 if (fm->argc < 1)
4325 modifiers |= FORMAT_QUOTE_SHELL;
4326 else if (strchr(fm->argv[0], 'e') != NULL ||
4327 strchr(fm->argv[0], 'h') != NULL)
4328 modifiers |= FORMAT_QUOTE_STYLE;
4329 break;
4330 case 'E':
4331 modifiers |= FORMAT_EXPAND;
4332 break;
4333 case 'T':
4334 modifiers |= FORMAT_EXPANDTIME;
4335 break;
4336 case 'N':
4337 if (fm->argc < 1 ||
4338 strchr(fm->argv[0], 'w') != NULL)
4339 modifiers |= FORMAT_WINDOW_NAME;
4340 else if (strchr(fm->argv[0], 's') != NULL)
4341 modifiers |= FORMAT_SESSION_NAME;
4342 break;
4343 case 'S':
4344 modifiers |= FORMAT_SESSIONS;
4345 break;
4346 case 'W':
4347 modifiers |= FORMAT_WINDOWS;
4348 break;
4349 case 'P':
4350 modifiers |= FORMAT_PANES;
4351 break;
4353 } else if (fm->size == 2) {
4354 if (strcmp(fm->modifier, "||") == 0 ||
4355 strcmp(fm->modifier, "&&") == 0 ||
4356 strcmp(fm->modifier, "==") == 0 ||
4357 strcmp(fm->modifier, "!=") == 0 ||
4358 strcmp(fm->modifier, ">=") == 0 ||
4359 strcmp(fm->modifier, "<=") == 0)
4360 cmp = fm;
4364 /* Is this a literal string? */
4365 if (modifiers & FORMAT_LITERAL) {
4366 format_log(es, "literal string is '%s'", copy);
4367 value = format_unescape(copy);
4368 goto done;
4371 /* Is this a character? */
4372 if (modifiers & FORMAT_CHARACTER) {
4373 new = format_expand1(es, copy);
4374 c = strtonum(new, 32, 126, &errstr);
4375 if (errstr != NULL)
4376 value = xstrdup("");
4377 else
4378 xasprintf(&value, "%c", c);
4379 free(new);
4380 goto done;
4383 /* Is this a colour? */
4384 if (modifiers & FORMAT_COLOUR) {
4385 new = format_expand1(es, copy);
4386 c = colour_fromstring(new);
4387 if (c == -1 || (c = colour_force_rgb(c)) == -1)
4388 value = xstrdup("");
4389 else
4390 xasprintf(&value, "%06x", c & 0xffffff);
4391 free(new);
4392 goto done;
4395 /* Is this a loop, comparison or condition? */
4396 if (modifiers & FORMAT_SESSIONS) {
4397 value = format_loop_sessions(es, copy);
4398 if (value == NULL)
4399 goto fail;
4400 } else if (modifiers & FORMAT_WINDOWS) {
4401 value = format_loop_windows(es, copy);
4402 if (value == NULL)
4403 goto fail;
4404 } else if (modifiers & FORMAT_PANES) {
4405 value = format_loop_panes(es, copy);
4406 if (value == NULL)
4407 goto fail;
4408 } else if (modifiers & FORMAT_WINDOW_NAME) {
4409 value = format_window_name(es, copy);
4410 if (value == NULL)
4411 goto fail;
4412 } else if (modifiers & FORMAT_SESSION_NAME) {
4413 value = format_session_name(es, copy);
4414 if (value == NULL)
4415 goto fail;
4416 } else if (search != NULL) {
4417 /* Search in pane. */
4418 new = format_expand1(es, copy);
4419 if (wp == NULL) {
4420 format_log(es, "search '%s' but no pane", new);
4421 value = xstrdup("0");
4422 } else {
4423 format_log(es, "search '%s' pane %%%u", new, wp->id);
4424 value = format_search(search, wp, new);
4426 free(new);
4427 } else if (cmp != NULL) {
4428 /* Comparison of left and right. */
4429 if (format_choose(es, copy, &left, &right, 1) != 0) {
4430 format_log(es, "compare %s syntax error: %s",
4431 cmp->modifier, copy);
4432 goto fail;
4434 format_log(es, "compare %s left is: %s", cmp->modifier, left);
4435 format_log(es, "compare %s right is: %s", cmp->modifier, right);
4437 if (strcmp(cmp->modifier, "||") == 0) {
4438 if (format_true(left) || format_true(right))
4439 value = xstrdup("1");
4440 else
4441 value = xstrdup("0");
4442 } else if (strcmp(cmp->modifier, "&&") == 0) {
4443 if (format_true(left) && format_true(right))
4444 value = xstrdup("1");
4445 else
4446 value = xstrdup("0");
4447 } else if (strcmp(cmp->modifier, "==") == 0) {
4448 if (strcmp(left, right) == 0)
4449 value = xstrdup("1");
4450 else
4451 value = xstrdup("0");
4452 } else if (strcmp(cmp->modifier, "!=") == 0) {
4453 if (strcmp(left, right) != 0)
4454 value = xstrdup("1");
4455 else
4456 value = xstrdup("0");
4457 } else if (strcmp(cmp->modifier, "<") == 0) {
4458 if (strcmp(left, right) < 0)
4459 value = xstrdup("1");
4460 else
4461 value = xstrdup("0");
4462 } else if (strcmp(cmp->modifier, ">") == 0) {
4463 if (strcmp(left, right) > 0)
4464 value = xstrdup("1");
4465 else
4466 value = xstrdup("0");
4467 } else if (strcmp(cmp->modifier, "<=") == 0) {
4468 if (strcmp(left, right) <= 0)
4469 value = xstrdup("1");
4470 else
4471 value = xstrdup("0");
4472 } else if (strcmp(cmp->modifier, ">=") == 0) {
4473 if (strcmp(left, right) >= 0)
4474 value = xstrdup("1");
4475 else
4476 value = xstrdup("0");
4477 } else if (strcmp(cmp->modifier, "m") == 0)
4478 value = format_match(cmp, left, right);
4480 free(right);
4481 free(left);
4482 } else if (*copy == '?') {
4483 /* Conditional: check first and choose second or third. */
4484 cp = format_skip(copy + 1, ",");
4485 if (cp == NULL) {
4486 format_log(es, "condition syntax error: %s", copy + 1);
4487 goto fail;
4489 condition = xstrndup(copy + 1, cp - (copy + 1));
4490 format_log(es, "condition is: %s", condition);
4492 found = format_find(ft, condition, modifiers, time_format);
4493 if (found == NULL) {
4495 * If the condition not found, try to expand it. If
4496 * the expansion doesn't have any effect, then assume
4497 * false.
4499 found = format_expand1(es, condition);
4500 if (strcmp(found, condition) == 0) {
4501 free(found);
4502 found = xstrdup("");
4503 format_log(es,
4504 "condition '%s' not found; assuming false",
4505 condition);
4507 } else {
4508 format_log(es, "condition '%s' found: %s", condition,
4509 found);
4512 if (format_choose(es, cp + 1, &left, &right, 0) != 0) {
4513 format_log(es, "condition '%s' syntax error: %s",
4514 condition, cp + 1);
4515 free(found);
4516 goto fail;
4518 if (format_true(found)) {
4519 format_log(es, "condition '%s' is true", condition);
4520 value = format_expand1(es, left);
4521 } else {
4522 format_log(es, "condition '%s' is false", condition);
4523 value = format_expand1(es, right);
4525 free(right);
4526 free(left);
4528 free(condition);
4529 free(found);
4530 } else if (mexp != NULL) {
4531 value = format_replace_expression(mexp, es, copy);
4532 if (value == NULL)
4533 value = xstrdup("");
4534 } else {
4535 if (strstr(copy, "#{") != 0) {
4536 format_log(es, "expanding inner format '%s'", copy);
4537 value = format_expand1(es, copy);
4538 } else {
4539 value = format_find(ft, copy, modifiers, time_format);
4540 if (value == NULL) {
4541 format_log(es, "format '%s' not found", copy);
4542 value = xstrdup("");
4543 } else {
4544 format_log(es, "format '%s' found: %s", copy,
4545 value);
4550 done:
4551 /* Expand again if required. */
4552 if (modifiers & FORMAT_EXPAND) {
4553 new = format_expand1(es, value);
4554 free(value);
4555 value = new;
4556 } else if (modifiers & FORMAT_EXPANDTIME) {
4557 format_copy_state(&next, es, FORMAT_EXPAND_TIME);
4558 new = format_expand1(&next, value);
4559 free(value);
4560 value = new;
4563 /* Perform substitution if any. */
4564 for (i = 0; i < nsub; i++) {
4565 left = format_expand1(es, sub[i]->argv[0]);
4566 right = format_expand1(es, sub[i]->argv[1]);
4567 new = format_sub(sub[i], value, left, right);
4568 format_log(es, "substitute '%s' to '%s': %s", left, right, new);
4569 free(value);
4570 value = new;
4571 free(right);
4572 free(left);
4575 /* Truncate the value if needed. */
4576 if (limit > 0) {
4577 new = format_trim_left(value, limit);
4578 if (marker != NULL && strcmp(new, value) != 0) {
4579 free(value);
4580 xasprintf(&value, "%s%s", new, marker);
4581 } else {
4582 free(value);
4583 value = new;
4585 format_log(es, "applied length limit %d: %s", limit, value);
4586 } else if (limit < 0) {
4587 new = format_trim_right(value, -limit);
4588 if (marker != NULL && strcmp(new, value) != 0) {
4589 free(value);
4590 xasprintf(&value, "%s%s", marker, new);
4591 } else {
4592 free(value);
4593 value = new;
4595 format_log(es, "applied length limit %d: %s", limit, value);
4598 /* Pad the value if needed. */
4599 if (width > 0) {
4600 new = utf8_padcstr(value, width);
4601 free(value);
4602 value = new;
4603 format_log(es, "applied padding width %d: %s", width, value);
4604 } else if (width < 0) {
4605 new = utf8_rpadcstr(value, -width);
4606 free(value);
4607 value = new;
4608 format_log(es, "applied padding width %d: %s", width, value);
4611 /* Replace with the length or width if needed. */
4612 if (modifiers & FORMAT_LENGTH) {
4613 xasprintf(&new, "%zu", strlen(value));
4614 free(value);
4615 value = new;
4616 format_log(es, "replacing with length: %s", new);
4618 if (modifiers & FORMAT_WIDTH) {
4619 xasprintf(&new, "%u", format_width(value));
4620 free(value);
4621 value = new;
4622 format_log(es, "replacing with width: %s", new);
4625 /* Expand the buffer and copy in the value. */
4626 valuelen = strlen(value);
4627 while (*len - *off < valuelen + 1) {
4628 *buf = xreallocarray(*buf, 2, *len);
4629 *len *= 2;
4631 memcpy(*buf + *off, value, valuelen);
4632 *off += valuelen;
4634 format_log(es, "replaced '%s' with '%s'", copy0, value);
4635 free(value);
4637 free(sub);
4638 format_free_modifiers(list, count);
4639 free(copy0);
4640 return (0);
4642 fail:
4643 format_log(es, "failed %s", copy0);
4645 free(sub);
4646 format_free_modifiers(list, count);
4647 free(copy0);
4648 return (-1);
4651 /* Expand keys in a template. */
4652 static char *
4653 format_expand1(struct format_expand_state *es, const char *fmt)
4655 struct format_tree *ft = es->ft;
4656 char *buf, *out, *name;
4657 const char *ptr, *s, *style_end = NULL;
4658 size_t off, len, n, outlen;
4659 int ch, brackets;
4660 char expanded[8192];
4662 if (fmt == NULL || *fmt == '\0')
4663 return (xstrdup(""));
4665 if (es->loop == FORMAT_LOOP_LIMIT) {
4666 format_log(es, "reached loop limit (%u)", FORMAT_LOOP_LIMIT);
4667 return (xstrdup(""));
4669 es->loop++;
4671 format_log(es, "expanding format: %s", fmt);
4673 if ((es->flags & FORMAT_EXPAND_TIME) && strchr(fmt, '%') != NULL) {
4674 if (es->time == 0) {
4675 es->time = time(NULL);
4676 localtime_r(&es->time, &es->tm);
4678 if (strftime(expanded, sizeof expanded, fmt, &es->tm) == 0) {
4679 format_log(es, "format is too long");
4680 return (xstrdup(""));
4682 if (format_logging(ft) && strcmp(expanded, fmt) != 0)
4683 format_log(es, "after time expanded: %s", expanded);
4684 fmt = expanded;
4687 len = 64;
4688 buf = xmalloc(len);
4689 off = 0;
4691 while (*fmt != '\0') {
4692 if (*fmt != '#') {
4693 while (len - off < 2) {
4694 buf = xreallocarray(buf, 2, len);
4695 len *= 2;
4697 buf[off++] = *fmt++;
4698 continue;
4700 fmt++;
4702 ch = (u_char)*fmt++;
4703 switch (ch) {
4704 case '(':
4705 brackets = 1;
4706 for (ptr = fmt; *ptr != '\0'; ptr++) {
4707 if (*ptr == '(')
4708 brackets++;
4709 if (*ptr == ')' && --brackets == 0)
4710 break;
4712 if (*ptr != ')' || brackets != 0)
4713 break;
4714 n = ptr - fmt;
4716 name = xstrndup(fmt, n);
4717 format_log(es, "found #(): %s", name);
4719 if ((ft->flags & FORMAT_NOJOBS) ||
4720 (es->flags & FORMAT_EXPAND_NOJOBS)) {
4721 out = xstrdup("");
4722 format_log(es, "#() is disabled");
4723 } else {
4724 out = format_job_get(es, name);
4725 format_log(es, "#() result: %s", out);
4727 free(name);
4729 outlen = strlen(out);
4730 while (len - off < outlen + 1) {
4731 buf = xreallocarray(buf, 2, len);
4732 len *= 2;
4734 memcpy(buf + off, out, outlen);
4735 off += outlen;
4737 free(out);
4739 fmt += n + 1;
4740 continue;
4741 case '{':
4742 ptr = format_skip((char *)fmt - 2, "}");
4743 if (ptr == NULL)
4744 break;
4745 n = ptr - fmt;
4747 format_log(es, "found #{}: %.*s", (int)n, fmt);
4748 if (format_replace(es, fmt, n, &buf, &len, &off) != 0)
4749 break;
4750 fmt += n + 1;
4751 continue;
4752 case '[':
4753 case '#':
4755 * If ##[ (with two or more #s), then it is a style and
4756 * can be left for format_draw to handle.
4758 ptr = fmt - (ch == '[');
4759 n = 2 - (ch == '[');
4760 while (*ptr == '#') {
4761 ptr++;
4762 n++;
4764 if (*ptr == '[') {
4765 style_end = format_skip(fmt - 2, "]");
4766 format_log(es, "found #*%zu[", n);
4767 while (len - off < n + 2) {
4768 buf = xreallocarray(buf, 2, len);
4769 len *= 2;
4771 memcpy(buf + off, fmt - 2, n + 1);
4772 off += n + 1;
4773 fmt = ptr + 1;
4774 continue;
4776 /* FALLTHROUGH */
4777 case '}':
4778 case ',':
4779 format_log(es, "found #%c", ch);
4780 while (len - off < 2) {
4781 buf = xreallocarray(buf, 2, len);
4782 len *= 2;
4784 buf[off++] = ch;
4785 continue;
4786 default:
4787 s = NULL;
4788 if (fmt > style_end) { /* skip inside #[] */
4789 if (ch >= 'A' && ch <= 'Z')
4790 s = format_upper[ch - 'A'];
4791 else if (ch >= 'a' && ch <= 'z')
4792 s = format_lower[ch - 'a'];
4794 if (s == NULL) {
4795 while (len - off < 3) {
4796 buf = xreallocarray(buf, 2, len);
4797 len *= 2;
4799 buf[off++] = '#';
4800 buf[off++] = ch;
4801 continue;
4803 n = strlen(s);
4804 format_log(es, "found #%c: %s", ch, s);
4805 if (format_replace(es, s, n, &buf, &len, &off) != 0)
4806 break;
4807 continue;
4810 break;
4812 buf[off] = '\0';
4814 format_log(es, "result is: %s", buf);
4815 es->loop--;
4817 return (buf);
4820 /* Expand keys in a template, passing through strftime first. */
4821 char *
4822 format_expand_time(struct format_tree *ft, const char *fmt)
4824 struct format_expand_state es;
4826 memset(&es, 0, sizeof es);
4827 es.ft = ft;
4828 es.flags = FORMAT_EXPAND_TIME;
4829 return (format_expand1(&es, fmt));
4832 /* Expand keys in a template. */
4833 char *
4834 format_expand(struct format_tree *ft, const char *fmt)
4836 struct format_expand_state es;
4838 memset(&es, 0, sizeof es);
4839 es.ft = ft;
4840 es.flags = 0;
4841 return (format_expand1(&es, fmt));
4844 /* Expand a single string. */
4845 char *
4846 format_single(struct cmdq_item *item, const char *fmt, struct client *c,
4847 struct session *s, struct winlink *wl, struct window_pane *wp)
4849 struct format_tree *ft;
4850 char *expanded;
4852 ft = format_create_defaults(item, c, s, wl, wp);
4853 expanded = format_expand(ft, fmt);
4854 format_free(ft);
4855 return (expanded);
4858 /* Expand a single string using state. */
4859 char *
4860 format_single_from_state(struct cmdq_item *item, const char *fmt,
4861 struct client *c, struct cmd_find_state *fs)
4863 return (format_single(item, fmt, c, fs->s, fs->wl, fs->wp));
4866 /* Expand a single string using target. */
4867 char *
4868 format_single_from_target(struct cmdq_item *item, const char *fmt)
4870 struct client *tc = cmdq_get_target_client(item);
4872 return (format_single_from_state(item, fmt, tc, cmdq_get_target(item)));
4875 /* Create and add defaults. */
4876 struct format_tree *
4877 format_create_defaults(struct cmdq_item *item, struct client *c,
4878 struct session *s, struct winlink *wl, struct window_pane *wp)
4880 struct format_tree *ft;
4882 if (item != NULL)
4883 ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0);
4884 else
4885 ft = format_create(NULL, item, FORMAT_NONE, 0);
4886 format_defaults(ft, c, s, wl, wp);
4887 return (ft);
4890 /* Create and add defaults using state. */
4891 struct format_tree *
4892 format_create_from_state(struct cmdq_item *item, struct client *c,
4893 struct cmd_find_state *fs)
4895 return (format_create_defaults(item, c, fs->s, fs->wl, fs->wp));
4898 /* Create and add defaults using target. */
4899 struct format_tree *
4900 format_create_from_target(struct cmdq_item *item)
4902 struct client *tc = cmdq_get_target_client(item);
4904 return (format_create_from_state(item, tc, cmdq_get_target(item)));
4907 /* Set defaults for any of arguments that are not NULL. */
4908 void
4909 format_defaults(struct format_tree *ft, struct client *c, struct session *s,
4910 struct winlink *wl, struct window_pane *wp)
4912 struct paste_buffer *pb;
4914 if (c != NULL && c->name != NULL)
4915 log_debug("%s: c=%s", __func__, c->name);
4916 else
4917 log_debug("%s: c=none", __func__);
4918 if (s != NULL)
4919 log_debug("%s: s=$%u", __func__, s->id);
4920 else
4921 log_debug("%s: s=none", __func__);
4922 if (wl != NULL)
4923 log_debug("%s: wl=%u", __func__, wl->idx);
4924 else
4925 log_debug("%s: wl=none", __func__);
4926 if (wp != NULL)
4927 log_debug("%s: wp=%%%u", __func__, wp->id);
4928 else
4929 log_debug("%s: wp=none", __func__);
4931 if (c != NULL && s != NULL && c->session != s)
4932 log_debug("%s: session does not match", __func__);
4934 if (wp != NULL)
4935 ft->type = FORMAT_TYPE_PANE;
4936 else if (wl != NULL)
4937 ft->type = FORMAT_TYPE_WINDOW;
4938 else if (s != NULL)
4939 ft->type = FORMAT_TYPE_SESSION;
4940 else
4941 ft->type = FORMAT_TYPE_UNKNOWN;
4943 if (s == NULL && c != NULL)
4944 s = c->session;
4945 if (wl == NULL && s != NULL)
4946 wl = s->curw;
4947 if (wp == NULL && wl != NULL)
4948 wp = wl->window->active;
4950 if (c != NULL)
4951 format_defaults_client(ft, c);
4952 if (s != NULL)
4953 format_defaults_session(ft, s);
4954 if (wl != NULL)
4955 format_defaults_winlink(ft, wl);
4956 if (wp != NULL)
4957 format_defaults_pane(ft, wp);
4959 pb = paste_get_top(NULL);
4960 if (pb != NULL)
4961 format_defaults_paste_buffer(ft, pb);
4964 /* Set default format keys for a session. */
4965 static void
4966 format_defaults_session(struct format_tree *ft, struct session *s)
4968 ft->s = s;
4971 /* Set default format keys for a client. */
4972 static void
4973 format_defaults_client(struct format_tree *ft, struct client *c)
4975 if (ft->s == NULL)
4976 ft->s = c->session;
4977 ft->c = c;
4980 /* Set default format keys for a window. */
4981 void
4982 format_defaults_window(struct format_tree *ft, struct window *w)
4984 ft->w = w;
4987 /* Set default format keys for a winlink. */
4988 static void
4989 format_defaults_winlink(struct format_tree *ft, struct winlink *wl)
4991 if (ft->w == NULL)
4992 format_defaults_window(ft, wl->window);
4993 ft->wl = wl;
4996 /* Set default format keys for a window pane. */
4997 void
4998 format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
5000 struct window_mode_entry *wme;
5002 if (ft->w == NULL)
5003 format_defaults_window(ft, wp->window);
5004 ft->wp = wp;
5006 wme = TAILQ_FIRST(&wp->modes);
5007 if (wme != NULL && wme->mode->formats != NULL)
5008 wme->mode->formats(wme, ft);
5011 /* Set default format keys for paste buffer. */
5012 void
5013 format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
5015 ft->pb = pb;
5018 /* Return word at given coordinates. Caller frees. */
5019 char *
5020 format_grid_word(struct grid *gd, u_int x, u_int y)
5022 const struct grid_line *gl;
5023 struct grid_cell gc;
5024 const char *ws;
5025 struct utf8_data *ud = NULL;
5026 u_int end;
5027 size_t size = 0;
5028 int found = 0;
5029 char *s = NULL;
5031 ws = options_get_string(global_s_options, "word-separators");
5033 for (;;) {
5034 grid_get_cell(gd, x, y, &gc);
5035 if (gc.flags & GRID_FLAG_PADDING)
5036 break;
5037 if (utf8_cstrhas(ws, &gc.data) ||
5038 (gc.data.size == 1 && *gc.data.data == ' ')) {
5039 found = 1;
5040 break;
5043 if (x == 0) {
5044 if (y == 0)
5045 break;
5046 gl = grid_peek_line(gd, y - 1);
5047 if (~gl->flags & GRID_LINE_WRAPPED)
5048 break;
5049 y--;
5050 x = grid_line_length(gd, y);
5051 if (x == 0)
5052 break;
5054 x--;
5056 for (;;) {
5057 if (found) {
5058 end = grid_line_length(gd, y);
5059 if (end == 0 || x == end - 1) {
5060 if (y == gd->hsize + gd->sy - 1)
5061 break;
5062 gl = grid_peek_line(gd, y);
5063 if (~gl->flags & GRID_LINE_WRAPPED)
5064 break;
5065 y++;
5066 x = 0;
5067 } else
5068 x++;
5070 found = 1;
5072 grid_get_cell(gd, x, y, &gc);
5073 if (gc.flags & GRID_FLAG_PADDING)
5074 break;
5075 if (utf8_cstrhas(ws, &gc.data) ||
5076 (gc.data.size == 1 && *gc.data.data == ' '))
5077 break;
5079 ud = xreallocarray(ud, size + 2, sizeof *ud);
5080 memcpy(&ud[size++], &gc.data, sizeof *ud);
5082 if (size != 0) {
5083 ud[size].size = 0;
5084 s = utf8_tocstr(ud);
5085 free(ud);
5087 return (s);
5090 /* Return line at given coordinates. Caller frees. */
5091 char *
5092 format_grid_line(struct grid *gd, u_int y)
5094 struct grid_cell gc;
5095 struct utf8_data *ud = NULL;
5096 u_int x;
5097 size_t size = 0;
5098 char *s = NULL;
5100 for (x = 0; x < grid_line_length(gd, y); x++) {
5101 grid_get_cell(gd, x, y, &gc);
5102 if (gc.flags & GRID_FLAG_PADDING)
5103 break;
5105 ud = xreallocarray(ud, size + 2, sizeof *ud);
5106 memcpy(&ud[size++], &gc.data, sizeof *ud);
5108 if (size != 0) {
5109 ud[size].size = 0;
5110 s = utf8_tocstr(ud);
5111 free(ud);
5113 return (s);
5116 /* Return hyperlink at given coordinates. Caller frees. */
5117 char *
5118 format_grid_hyperlink(struct grid *gd, u_int x, u_int y, struct screen* s)
5120 const char *uri;
5121 struct grid_cell gc;
5123 grid_get_cell(gd, x, y, &gc);
5124 if (gc.flags & GRID_FLAG_PADDING)
5125 return (NULL);
5126 if (s->hyperlinks == NULL || gc.link == 0)
5127 return (NULL);
5128 if (!hyperlinks_get(s->hyperlinks, gc.link, &uri, NULL, NULL))
5129 return (NULL);
5130 return (xstrdup(uri));