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>
37 * Build a list of key-value pairs and use them to expand #{key} entries in a
41 struct format_expand_state
;
43 static char *format_job_get(struct format_expand_state
*, const char *);
44 static char *format_expand1(struct format_expand_state
*, const char *);
45 static int format_replace(struct format_expand_state
*, const char *,
46 size_t, char **, size_t *, size_t *);
47 static void format_defaults_session(struct format_tree
*,
49 static void format_defaults_client(struct format_tree
*, struct client
*);
50 static void format_defaults_winlink(struct format_tree
*,
53 /* Entry in format job tree. */
55 struct client
*client
;
67 RB_ENTRY(format_job
) entry
;
70 /* Format job tree. */
71 static int format_job_cmp(struct format_job
*, struct format_job
*);
72 static RB_HEAD(format_job_tree
, format_job
) format_jobs
= RB_INITIALIZER();
73 RB_GENERATE_STATIC(format_job_tree
, format_job
, entry
, format_job_cmp
);
75 /* Format job tree comparison function. */
77 format_job_cmp(struct format_job
*fj1
, struct format_job
*fj2
)
79 if (fj1
->tag
< fj2
->tag
)
81 if (fj1
->tag
> fj2
->tag
)
83 return (strcmp(fj1
->cmd
, fj2
->cmd
));
86 /* Format modifiers. */
87 #define FORMAT_TIMESTRING 0x1
88 #define FORMAT_BASENAME 0x2
89 #define FORMAT_DIRNAME 0x4
90 #define FORMAT_QUOTE_SHELL 0x8
91 #define FORMAT_LITERAL 0x10
92 #define FORMAT_EXPAND 0x20
93 #define FORMAT_EXPANDTIME 0x40
94 #define FORMAT_SESSIONS 0x80
95 #define FORMAT_WINDOWS 0x100
96 #define FORMAT_PANES 0x200
97 #define FORMAT_PRETTY 0x400
98 #define FORMAT_LENGTH 0x800
99 #define FORMAT_WIDTH 0x1000
100 #define FORMAT_QUOTE_STYLE 0x2000
101 #define FORMAT_WINDOW_NAME 0x4000
102 #define FORMAT_SESSION_NAME 0x8000
103 #define FORMAT_CHARACTER 0x10000
104 #define FORMAT_COLOUR 0x20000
106 /* Limit on recursion. */
107 #define FORMAT_LOOP_LIMIT 100
109 /* Format expand flags. */
110 #define FORMAT_EXPAND_TIME 0x1
111 #define FORMAT_EXPAND_NOJOBS 0x2
113 /* Entry in format tree. */
114 struct format_entry
{
119 RB_ENTRY(format_entry
) entry
;
131 enum format_type type
;
137 struct window_pane
*wp
;
138 struct paste_buffer
*pb
;
140 struct cmdq_item
*item
;
141 struct client
*client
;
145 struct mouse_event m
;
147 RB_HEAD(format_entry_tree
, format_entry
) tree
;
149 static int format_entry_cmp(struct format_entry
*, struct format_entry
*);
150 RB_GENERATE_STATIC(format_entry_tree
, format_entry
, entry
, format_entry_cmp
);
152 /* Format expand state. */
153 struct format_expand_state
{
154 struct format_tree
*ft
;
161 /* Format modifier. */
162 struct format_modifier
{
170 /* Format entry tree comparison function. */
172 format_entry_cmp(struct format_entry
*fe1
, struct format_entry
*fe2
)
174 return (strcmp(fe1
->key
, fe2
->key
));
177 /* Single-character uppercase aliases. */
178 static const char *format_upper
[] = {
184 "window_flags", /* F */
187 "window_index", /* I */
194 "pane_index", /* P */
197 "session_name", /* S */
198 "pane_title", /* T */
201 "window_name", /* W */
207 /* Single-character lowercase aliases. */
208 static const char *format_lower
[] = {
216 "host_short", /* h */
237 /* Is logging enabled? */
239 format_logging(struct format_tree
*ft
)
241 return (log_get_level() != 0 || (ft
->flags
& FORMAT_VERBOSE
));
244 /* Log a message if verbose. */
245 static void printflike(3, 4)
246 format_log1(struct format_expand_state
*es
, const char *from
, const char *fmt
,
249 struct format_tree
*ft
= es
->ft
;
252 static const char spaces
[] = " ";
254 if (!format_logging(ft
))
258 xvasprintf(&s
, fmt
, ap
);
261 log_debug("%s: %s", from
, s
);
262 if (ft
->item
!= NULL
&& (ft
->flags
& FORMAT_VERBOSE
))
263 cmdq_print(ft
->item
, "#%.*s%s", es
->loop
, spaces
, s
);
267 #define format_log(es, fmt, ...) format_log1(es, __func__, fmt, ##__VA_ARGS__)
269 /* Copy expand state. */
271 format_copy_state(struct format_expand_state
*to
,
272 struct format_expand_state
*from
, int flags
)
275 to
->loop
= from
->loop
;
276 to
->time
= from
->time
;
277 memcpy(&to
->tm
, &from
->tm
, sizeof to
->tm
);
278 to
->flags
= from
->flags
|flags
;
281 /* Format job update callback. */
283 format_job_update(struct job
*job
)
285 struct format_job
*fj
= job_get_data(job
);
286 struct evbuffer
*evb
= job_get_event(job
)->input
;
287 char *line
= NULL
, *next
;
290 while ((next
= evbuffer_readline(evb
)) != NULL
) {
301 log_debug("%s: %p %s: %s", __func__
, fj
, fj
->cmd
, fj
->out
);
304 if (fj
->status
&& fj
->last
!= t
) {
305 if (fj
->client
!= NULL
)
306 server_status_client(fj
->client
);
311 /* Format job complete callback. */
313 format_job_complete(struct job
*job
)
315 struct format_job
*fj
= job_get_data(job
);
316 struct evbuffer
*evb
= job_get_event(job
)->input
;
323 if ((line
= evbuffer_readline(evb
)) == NULL
) {
324 len
= EVBUFFER_LENGTH(evb
);
325 buf
= xmalloc(len
+ 1);
327 memcpy(buf
, EVBUFFER_DATA(evb
), len
);
332 log_debug("%s: %p %s: %s", __func__
, fj
, fj
->cmd
, buf
);
334 if (*buf
!= '\0' || !fj
->updated
) {
341 if (fj
->client
!= NULL
)
342 server_status_client(fj
->client
);
349 format_job_get(struct format_expand_state
*es
, const char *cmd
)
351 struct format_tree
*ft
= es
->ft
;
352 struct format_job_tree
*jobs
;
353 struct format_job fj0
, *fj
;
357 struct format_expand_state next
;
359 if (ft
->client
== NULL
)
361 else if (ft
->client
->jobs
!= NULL
)
362 jobs
= ft
->client
->jobs
;
364 jobs
= ft
->client
->jobs
= xmalloc(sizeof *ft
->client
->jobs
);
370 if ((fj
= RB_FIND(format_job_tree
, jobs
, &fj0
)) == NULL
) {
371 fj
= xcalloc(1, sizeof *fj
);
372 fj
->client
= ft
->client
;
374 fj
->cmd
= xstrdup(cmd
);
376 RB_INSERT(format_job_tree
, jobs
, fj
);
379 format_copy_state(&next
, es
, FORMAT_EXPAND_NOJOBS
);
380 next
.flags
&= ~FORMAT_EXPAND_TIME
;
382 expanded
= format_expand1(&next
, cmd
);
383 if (fj
->expanded
== NULL
|| strcmp(expanded
, fj
->expanded
) != 0) {
384 free((void *)fj
->expanded
);
385 fj
->expanded
= xstrdup(expanded
);
388 force
= (ft
->flags
& FORMAT_FORCE
);
391 if (force
&& fj
->job
!= NULL
)
393 if (force
|| (fj
->job
== NULL
&& fj
->last
!= t
)) {
394 fj
->job
= job_run(expanded
, 0, NULL
, NULL
, NULL
,
395 server_client_get_cwd(ft
->client
, NULL
), format_job_update
,
396 format_job_complete
, NULL
, fj
, JOB_NOWAIT
, -1, -1);
397 if (fj
->job
== NULL
) {
399 xasprintf(&fj
->out
, "<'%s' didn't start>", fj
->cmd
);
403 } else if (fj
->job
!= NULL
&& (t
- fj
->last
) > 1 && fj
->out
== NULL
)
404 xasprintf(&fj
->out
, "<'%s' not ready>", fj
->cmd
);
407 if (ft
->flags
& FORMAT_STATUS
)
410 return (xstrdup(""));
411 return (format_expand1(&next
, fj
->out
));
414 /* Remove old jobs. */
416 format_job_tidy(struct format_job_tree
*jobs
, int force
)
418 struct format_job
*fj
, *fj1
;
422 RB_FOREACH_SAFE(fj
, format_job_tree
, jobs
, fj1
) {
423 if (!force
&& (fj
->last
> now
|| now
- fj
->last
< 3600))
425 RB_REMOVE(format_job_tree
, jobs
, fj
);
427 log_debug("%s: %s", __func__
, fj
->cmd
);
432 free((void *)fj
->expanded
);
433 free((void *)fj
->cmd
);
440 /* Tidy old jobs for all clients. */
442 format_tidy_jobs(void)
446 format_job_tidy(&format_jobs
, 0);
447 TAILQ_FOREACH(c
, &clients
, entry
) {
449 format_job_tidy(c
->jobs
, 0);
453 /* Remove old jobs for client. */
455 format_lost_client(struct client
*c
)
458 format_job_tidy(c
->jobs
, 1);
462 /* Wrapper for asprintf. */
463 static char * printflike(1, 2)
464 format_printf(const char *fmt
, ...)
470 xvasprintf(&s
, fmt
, ap
);
475 /* Callback for host. */
477 format_cb_host(__unused
struct format_tree
*ft
)
479 char host
[HOST_NAME_MAX
+ 1];
481 if (gethostname(host
, sizeof host
) != 0)
482 return (xstrdup(""));
483 return (xstrdup(host
));
486 /* Callback for host_short. */
488 format_cb_host_short(__unused
struct format_tree
*ft
)
490 char host
[HOST_NAME_MAX
+ 1], *cp
;
492 if (gethostname(host
, sizeof host
) != 0)
493 return (xstrdup(""));
494 if ((cp
= strchr(host
, '.')) != NULL
)
496 return (xstrdup(host
));
499 /* Callback for pid. */
501 format_cb_pid(__unused
struct format_tree
*ft
)
505 xasprintf(&value
, "%ld", (long)getpid());
509 /* Callback for session_attached_list. */
511 format_cb_session_attached_list(struct format_tree
*ft
)
513 struct session
*s
= ft
->s
;
515 struct evbuffer
*buffer
;
522 buffer
= evbuffer_new();
524 fatalx("out of memory");
526 TAILQ_FOREACH(loop
, &clients
, entry
) {
527 if (loop
->session
== s
) {
528 if (EVBUFFER_LENGTH(buffer
) > 0)
529 evbuffer_add(buffer
, ",", 1);
530 evbuffer_add_printf(buffer
, "%s", loop
->name
);
534 if ((size
= EVBUFFER_LENGTH(buffer
)) != 0)
535 xasprintf(&value
, "%.*s", size
, EVBUFFER_DATA(buffer
));
536 evbuffer_free(buffer
);
540 /* Callback for session_alerts. */
542 format_cb_session_alerts(struct format_tree
*ft
)
544 struct session
*s
= ft
->s
;
546 char alerts
[1024], tmp
[16];
552 RB_FOREACH(wl
, winlinks
, &s
->windows
) {
553 if ((wl
->flags
& WINLINK_ALERTFLAGS
) == 0)
555 xsnprintf(tmp
, sizeof tmp
, "%u", wl
->idx
);
558 strlcat(alerts
, ",", sizeof alerts
);
559 strlcat(alerts
, tmp
, sizeof alerts
);
560 if (wl
->flags
& WINLINK_ACTIVITY
)
561 strlcat(alerts
, "#", sizeof alerts
);
562 if (wl
->flags
& WINLINK_BELL
)
563 strlcat(alerts
, "!", sizeof alerts
);
564 if (wl
->flags
& WINLINK_SILENCE
)
565 strlcat(alerts
, "~", sizeof alerts
);
567 return (xstrdup(alerts
));
570 /* Callback for session_stack. */
572 format_cb_session_stack(struct format_tree
*ft
)
574 struct session
*s
= ft
->s
;
576 char result
[1024], tmp
[16];
581 xsnprintf(result
, sizeof result
, "%u", s
->curw
->idx
);
582 TAILQ_FOREACH(wl
, &s
->lastw
, sentry
) {
583 xsnprintf(tmp
, sizeof tmp
, "%u", wl
->idx
);
586 strlcat(result
, ",", sizeof result
);
587 strlcat(result
, tmp
, sizeof result
);
589 return (xstrdup(result
));
592 /* Callback for window_stack_index. */
594 format_cb_window_stack_index(struct format_tree
*ft
)
606 TAILQ_FOREACH(wl
, &s
->lastw
, sentry
) {
612 return (xstrdup("0"));
613 xasprintf(&value
, "%u", idx
);
617 /* Callback for window_linked_sessions_list. */
619 format_cb_window_linked_sessions_list(struct format_tree
*ft
)
623 struct evbuffer
*buffer
;
631 buffer
= evbuffer_new();
633 fatalx("out of memory");
635 TAILQ_FOREACH(wl
, &w
->winlinks
, wentry
) {
636 if (EVBUFFER_LENGTH(buffer
) > 0)
637 evbuffer_add(buffer
, ",", 1);
638 evbuffer_add_printf(buffer
, "%s", wl
->session
->name
);
641 if ((size
= EVBUFFER_LENGTH(buffer
)) != 0)
642 xasprintf(&value
, "%.*s", size
, EVBUFFER_DATA(buffer
));
643 evbuffer_free(buffer
);
647 /* Callback for window_active_sessions. */
649 format_cb_window_active_sessions(struct format_tree
*ft
)
660 TAILQ_FOREACH(wl
, &w
->winlinks
, wentry
) {
661 if (wl
->session
->curw
== wl
)
665 xasprintf(&value
, "%u", n
);
669 /* Callback for window_active_sessions_list. */
671 format_cb_window_active_sessions_list(struct format_tree
*ft
)
675 struct evbuffer
*buffer
;
683 buffer
= evbuffer_new();
685 fatalx("out of memory");
687 TAILQ_FOREACH(wl
, &w
->winlinks
, wentry
) {
688 if (wl
->session
->curw
== wl
) {
689 if (EVBUFFER_LENGTH(buffer
) > 0)
690 evbuffer_add(buffer
, ",", 1);
691 evbuffer_add_printf(buffer
, "%s", wl
->session
->name
);
695 if ((size
= EVBUFFER_LENGTH(buffer
)) != 0)
696 xasprintf(&value
, "%.*s", size
, EVBUFFER_DATA(buffer
));
697 evbuffer_free(buffer
);
701 /* Callback for window_active_clients. */
703 format_cb_window_active_clients(struct format_tree
*ft
)
707 struct session
*client_session
;
715 TAILQ_FOREACH(loop
, &clients
, entry
) {
716 client_session
= loop
->session
;
717 if (client_session
== NULL
)
720 if (w
== client_session
->curw
->window
)
724 xasprintf(&value
, "%u", n
);
728 /* Callback for window_active_clients_list. */
730 format_cb_window_active_clients_list(struct format_tree
*ft
)
734 struct session
*client_session
;
735 struct evbuffer
*buffer
;
743 buffer
= evbuffer_new();
745 fatalx("out of memory");
747 TAILQ_FOREACH(loop
, &clients
, entry
) {
748 client_session
= loop
->session
;
749 if (client_session
== NULL
)
752 if (w
== client_session
->curw
->window
) {
753 if (EVBUFFER_LENGTH(buffer
) > 0)
754 evbuffer_add(buffer
, ",", 1);
755 evbuffer_add_printf(buffer
, "%s", loop
->name
);
759 if ((size
= EVBUFFER_LENGTH(buffer
)) != 0)
760 xasprintf(&value
, "%.*s", size
, EVBUFFER_DATA(buffer
));
761 evbuffer_free(buffer
);
765 /* Callback for window_layout. */
767 format_cb_window_layout(struct format_tree
*ft
)
769 struct window
*w
= ft
->w
;
774 if (w
->saved_layout_root
!= NULL
)
775 return (layout_dump(w
->saved_layout_root
));
776 return (layout_dump(w
->layout_root
));
779 /* Callback for window_visible_layout. */
781 format_cb_window_visible_layout(struct format_tree
*ft
)
783 struct window
*w
= ft
->w
;
788 return (layout_dump(w
->layout_root
));
791 /* Callback for pane_start_command. */
793 format_cb_start_command(struct format_tree
*ft
)
795 struct window_pane
*wp
= ft
->wp
;
800 return (cmd_stringify_argv(wp
->argc
, wp
->argv
));
803 /* Callback for pane_current_command. */
805 format_cb_current_command(struct format_tree
*ft
)
807 struct window_pane
*wp
= ft
->wp
;
810 if (wp
== NULL
|| wp
->shell
== NULL
)
813 cmd
= osdep_get_name(wp
->fd
, wp
->tty
);
814 if (cmd
== NULL
|| *cmd
== '\0') {
816 cmd
= cmd_stringify_argv(wp
->argc
, wp
->argv
);
817 if (cmd
== NULL
|| *cmd
== '\0') {
819 cmd
= xstrdup(wp
->shell
);
822 value
= parse_window_name(cmd
);
827 /* Callback for pane_current_path. */
829 format_cb_current_path(struct format_tree
*ft
)
831 struct window_pane
*wp
= ft
->wp
;
837 cwd
= osdep_get_cwd(wp
->fd
);
840 return (xstrdup(cwd
));
843 /* Callback for history_bytes. */
845 format_cb_history_bytes(struct format_tree
*ft
)
847 struct window_pane
*wp
= ft
->wp
;
849 struct grid_line
*gl
;
858 for (i
= 0; i
< gd
->hsize
+ gd
->sy
; i
++) {
859 gl
= grid_get_line(gd
, i
);
860 size
+= gl
->cellsize
* sizeof *gl
->celldata
;
861 size
+= gl
->extdsize
* sizeof *gl
->extddata
;
863 size
+= (gd
->hsize
+ gd
->sy
) * sizeof *gl
;
865 xasprintf(&value
, "%zu", size
);
869 /* Callback for history_all_bytes. */
871 format_cb_history_all_bytes(struct format_tree
*ft
)
873 struct window_pane
*wp
= ft
->wp
;
875 struct grid_line
*gl
;
876 u_int i
, lines
, cells
= 0, extended_cells
= 0;
883 lines
= gd
->hsize
+ gd
->sy
;
884 for (i
= 0; i
< lines
; i
++) {
885 gl
= grid_get_line(gd
, i
);
886 cells
+= gl
->cellsize
;
887 extended_cells
+= gl
->extdsize
;
890 xasprintf(&value
, "%u,%zu,%u,%zu,%u,%zu", lines
,
891 lines
* sizeof *gl
, cells
, cells
* sizeof *gl
->celldata
,
892 extended_cells
, extended_cells
* sizeof *gl
->extddata
);
896 /* Callback for pane_tabs. */
898 format_cb_pane_tabs(struct format_tree
*ft
)
900 struct window_pane
*wp
= ft
->wp
;
901 struct evbuffer
*buffer
;
909 buffer
= evbuffer_new();
911 fatalx("out of memory");
912 for (i
= 0; i
< wp
->base
.grid
->sx
; i
++) {
913 if (!bit_test(wp
->base
.tabs
, i
))
916 if (EVBUFFER_LENGTH(buffer
) > 0)
917 evbuffer_add(buffer
, ",", 1);
918 evbuffer_add_printf(buffer
, "%u", i
);
920 if ((size
= EVBUFFER_LENGTH(buffer
)) != 0)
921 xasprintf(&value
, "%.*s", size
, EVBUFFER_DATA(buffer
));
922 evbuffer_free(buffer
);
926 /* Callback for pane_fg. */
928 format_cb_pane_fg(struct format_tree
*ft
)
930 struct window_pane
*wp
= ft
->wp
;
936 tty_default_colours(&gc
, wp
);
937 return (xstrdup(colour_tostring(gc
.fg
)));
940 /* Callback for pane_bg. */
942 format_cb_pane_bg(struct format_tree
*ft
)
944 struct window_pane
*wp
= ft
->wp
;
950 tty_default_colours(&gc
, wp
);
951 return (xstrdup(colour_tostring(gc
.bg
)));
954 /* Callback for session_group_list. */
956 format_cb_session_group_list(struct format_tree
*ft
)
958 struct session
*s
= ft
->s
;
959 struct session_group
*sg
;
960 struct session
*loop
;
961 struct evbuffer
*buffer
;
967 sg
= session_group_contains(s
);
971 buffer
= evbuffer_new();
973 fatalx("out of memory");
975 TAILQ_FOREACH(loop
, &sg
->sessions
, gentry
) {
976 if (EVBUFFER_LENGTH(buffer
) > 0)
977 evbuffer_add(buffer
, ",", 1);
978 evbuffer_add_printf(buffer
, "%s", loop
->name
);
981 if ((size
= EVBUFFER_LENGTH(buffer
)) != 0)
982 xasprintf(&value
, "%.*s", size
, EVBUFFER_DATA(buffer
));
983 evbuffer_free(buffer
);
987 /* Callback for session_group_attached_list. */
989 format_cb_session_group_attached_list(struct format_tree
*ft
)
991 struct session
*s
= ft
->s
, *client_session
, *session_loop
;
992 struct session_group
*sg
;
994 struct evbuffer
*buffer
;
1000 sg
= session_group_contains(s
);
1004 buffer
= evbuffer_new();
1006 fatalx("out of memory");
1008 TAILQ_FOREACH(loop
, &clients
, entry
) {
1009 client_session
= loop
->session
;
1010 if (client_session
== NULL
)
1012 TAILQ_FOREACH(session_loop
, &sg
->sessions
, gentry
) {
1013 if (session_loop
== client_session
){
1014 if (EVBUFFER_LENGTH(buffer
) > 0)
1015 evbuffer_add(buffer
, ",", 1);
1016 evbuffer_add_printf(buffer
, "%s", loop
->name
);
1021 if ((size
= EVBUFFER_LENGTH(buffer
)) != 0)
1022 xasprintf(&value
, "%.*s", size
, EVBUFFER_DATA(buffer
));
1023 evbuffer_free(buffer
);
1027 /* Callback for pane_in_mode. */
1029 format_cb_pane_in_mode(struct format_tree
*ft
)
1031 struct window_pane
*wp
= ft
->wp
;
1033 struct window_mode_entry
*wme
;
1039 TAILQ_FOREACH(wme
, &wp
->modes
, entry
)
1041 xasprintf(&value
, "%u", n
);
1045 /* Callback for pane_at_top. */
1047 format_cb_pane_at_top(struct format_tree
*ft
)
1049 struct window_pane
*wp
= ft
->wp
;
1058 status
= options_get_number(w
->options
, "pane-border-status");
1059 if (status
== PANE_STATUS_TOP
)
1060 flag
= (wp
->yoff
== 1);
1062 flag
= (wp
->yoff
== 0);
1063 xasprintf(&value
, "%d", flag
);
1067 /* Callback for pane_at_bottom. */
1069 format_cb_pane_at_bottom(struct format_tree
*ft
)
1071 struct window_pane
*wp
= ft
->wp
;
1080 status
= options_get_number(w
->options
, "pane-border-status");
1081 if (status
== PANE_STATUS_BOTTOM
)
1082 flag
= (wp
->yoff
+ wp
->sy
== w
->sy
- 1);
1084 flag
= (wp
->yoff
+ wp
->sy
== w
->sy
);
1085 xasprintf(&value
, "%d", flag
);
1089 /* Callback for cursor_character. */
1091 format_cb_cursor_character(struct format_tree
*ft
)
1093 struct window_pane
*wp
= ft
->wp
;
1094 struct grid_cell gc
;
1100 grid_view_get_cell(wp
->base
.grid
, wp
->base
.cx
, wp
->base
.cy
, &gc
);
1101 if (~gc
.flags
& GRID_FLAG_PADDING
)
1102 xasprintf(&value
, "%.*s", (int)gc
.data
.size
, gc
.data
.data
);
1106 /* Callback for mouse_word. */
1108 format_cb_mouse_word(struct format_tree
*ft
)
1110 struct window_pane
*wp
;
1117 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1120 if (cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) != 0)
1123 if (!TAILQ_EMPTY(&wp
->modes
)) {
1124 if (TAILQ_FIRST(&wp
->modes
)->mode
== &window_copy_mode
||
1125 TAILQ_FIRST(&wp
->modes
)->mode
== &window_view_mode
)
1126 return (s
= window_copy_get_word(wp
, x
, y
));
1130 return (format_grid_word(gd
, x
, gd
->hsize
+ y
));
1133 /* Callback for mouse_line. */
1135 format_cb_mouse_line(struct format_tree
*ft
)
1137 struct window_pane
*wp
;
1143 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1146 if (cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) != 0)
1149 if (!TAILQ_EMPTY(&wp
->modes
)) {
1150 if (TAILQ_FIRST(&wp
->modes
)->mode
== &window_copy_mode
||
1151 TAILQ_FIRST(&wp
->modes
)->mode
== &window_view_mode
)
1152 return (window_copy_get_line(wp
, y
));
1156 return (format_grid_line(gd
, gd
->hsize
+ y
));
1159 /* Callback for alternate_on. */
1161 format_cb_alternate_on(struct format_tree
*ft
)
1163 if (ft
->wp
!= NULL
) {
1164 if (ft
->wp
->base
.saved_grid
!= NULL
)
1165 return (xstrdup("1"));
1166 return (xstrdup("0"));
1171 /* Callback for alternate_saved_x. */
1173 format_cb_alternate_saved_x(struct format_tree
*ft
)
1176 return (format_printf("%u", ft
->wp
->base
.saved_cx
));
1180 /* Callback for alternate_saved_y. */
1182 format_cb_alternate_saved_y(struct format_tree
*ft
)
1185 return (format_printf("%u", ft
->wp
->base
.saved_cy
));
1189 /* Callback for buffer_name. */
1191 format_cb_buffer_name(struct format_tree
*ft
)
1194 return (xstrdup(paste_buffer_name(ft
->pb
)));
1198 /* Callback for buffer_sample. */
1200 format_cb_buffer_sample(struct format_tree
*ft
)
1203 return (paste_make_sample(ft
->pb
));
1207 /* Callback for buffer_size. */
1209 format_cb_buffer_size(struct format_tree
*ft
)
1213 if (ft
->pb
!= NULL
) {
1214 paste_buffer_data(ft
->pb
, &size
);
1215 return (format_printf("%zu", size
));
1220 /* Callback for client_cell_height. */
1222 format_cb_client_cell_height(struct format_tree
*ft
)
1224 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
))
1225 return (format_printf("%u", ft
->c
->tty
.ypixel
));
1229 /* Callback for client_cell_width. */
1231 format_cb_client_cell_width(struct format_tree
*ft
)
1233 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
))
1234 return (format_printf("%u", ft
->c
->tty
.xpixel
));
1238 /* Callback for client_control_mode. */
1240 format_cb_client_control_mode(struct format_tree
*ft
)
1242 if (ft
->c
!= NULL
) {
1243 if (ft
->c
->flags
& CLIENT_CONTROL
)
1244 return (xstrdup("1"));
1245 return (xstrdup("0"));
1250 /* Callback for client_discarded. */
1252 format_cb_client_discarded(struct format_tree
*ft
)
1255 return (format_printf("%zu", ft
->c
->discarded
));
1259 /* Callback for client_flags. */
1261 format_cb_client_flags(struct format_tree
*ft
)
1264 return (xstrdup(server_client_get_flags(ft
->c
)));
1268 /* Callback for client_height. */
1270 format_cb_client_height(struct format_tree
*ft
)
1272 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
))
1273 return (format_printf("%u", ft
->c
->tty
.sy
));
1277 /* Callback for client_key_table. */
1279 format_cb_client_key_table(struct format_tree
*ft
)
1282 return (xstrdup(ft
->c
->keytable
->name
));
1286 /* Callback for client_last_session. */
1288 format_cb_client_last_session(struct format_tree
*ft
)
1290 if (ft
->c
!= NULL
&&
1291 ft
->c
->last_session
!= NULL
&&
1292 session_alive(ft
->c
->last_session
))
1293 return (xstrdup(ft
->c
->last_session
->name
));
1297 /* Callback for client_name. */
1299 format_cb_client_name(struct format_tree
*ft
)
1302 return (xstrdup(ft
->c
->name
));
1306 /* Callback for client_pid. */
1308 format_cb_client_pid(struct format_tree
*ft
)
1311 return (format_printf("%ld", (long)ft
->c
->pid
));
1315 /* Callback for client_prefix. */
1317 format_cb_client_prefix(struct format_tree
*ft
)
1321 if (ft
->c
!= NULL
) {
1322 name
= server_client_get_key_table(ft
->c
);
1323 if (strcmp(ft
->c
->keytable
->name
, name
) == 0)
1324 return (xstrdup("0"));
1325 return (xstrdup("1"));
1330 /* Callback for client_readonly. */
1332 format_cb_client_readonly(struct format_tree
*ft
)
1334 if (ft
->c
!= NULL
) {
1335 if (ft
->c
->flags
& CLIENT_READONLY
)
1336 return (xstrdup("1"));
1337 return (xstrdup("0"));
1342 /* Callback for client_session. */
1344 format_cb_client_session(struct format_tree
*ft
)
1346 if (ft
->c
!= NULL
&& ft
->c
->session
!= NULL
)
1347 return (xstrdup(ft
->c
->session
->name
));
1351 /* Callback for client_termfeatures. */
1353 format_cb_client_termfeatures(struct format_tree
*ft
)
1356 return (xstrdup(tty_get_features(ft
->c
->term_features
)));
1360 /* Callback for client_termname. */
1362 format_cb_client_termname(struct format_tree
*ft
)
1365 return (xstrdup(ft
->c
->term_name
));
1369 /* Callback for client_termtype. */
1371 format_cb_client_termtype(struct format_tree
*ft
)
1373 if (ft
->c
!= NULL
) {
1374 if (ft
->c
->term_type
== NULL
)
1375 return (xstrdup(""));
1376 return (xstrdup(ft
->c
->term_type
));
1381 /* Callback for client_tty. */
1383 format_cb_client_tty(struct format_tree
*ft
)
1386 return (xstrdup(ft
->c
->ttyname
));
1390 /* Callback for client_utf8. */
1392 format_cb_client_utf8(struct format_tree
*ft
)
1394 if (ft
->c
!= NULL
) {
1395 if (ft
->c
->flags
& CLIENT_UTF8
)
1396 return (xstrdup("1"));
1397 return (xstrdup("0"));
1402 /* Callback for client_width. */
1404 format_cb_client_width(struct format_tree
*ft
)
1407 return (format_printf("%u", ft
->c
->tty
.sx
));
1411 /* Callback for client_written. */
1413 format_cb_client_written(struct format_tree
*ft
)
1416 return (format_printf("%zu", ft
->c
->written
));
1420 /* Callback for config_files. */
1422 format_cb_config_files(__unused
struct format_tree
*ft
)
1429 for (i
= 0; i
< cfg_nfiles
; i
++) {
1430 n
= strlen(cfg_files
[i
]) + 1;
1431 s
= xrealloc(s
, slen
+ n
+ 1);
1432 slen
+= xsnprintf(s
+ slen
, n
+ 1, "%s,", cfg_files
[i
]);
1435 return (xstrdup(""));
1440 /* Callback for cursor_flag. */
1442 format_cb_cursor_flag(struct format_tree
*ft
)
1444 if (ft
->wp
!= NULL
) {
1445 if (ft
->wp
->base
.mode
& MODE_CURSOR
)
1446 return (xstrdup("1"));
1447 return (xstrdup("0"));
1452 /* Callback for cursor_x. */
1454 format_cb_cursor_x(struct format_tree
*ft
)
1457 return (format_printf("%u", ft
->wp
->base
.cx
));
1461 /* Callback for cursor_y. */
1463 format_cb_cursor_y(struct format_tree
*ft
)
1466 return (format_printf("%u", ft
->wp
->base
.cy
));
1470 /* Callback for history_limit. */
1472 format_cb_history_limit(struct format_tree
*ft
)
1475 return (format_printf("%u", ft
->wp
->base
.grid
->hlimit
));
1479 /* Callback for history_size. */
1481 format_cb_history_size(struct format_tree
*ft
)
1484 return (format_printf("%u", ft
->wp
->base
.grid
->hsize
));
1488 /* Callback for insert_flag. */
1490 format_cb_insert_flag(struct format_tree
*ft
)
1492 if (ft
->wp
!= NULL
) {
1493 if (ft
->wp
->base
.mode
& MODE_INSERT
)
1494 return (xstrdup("1"));
1495 return (xstrdup("0"));
1500 /* Callback for keypad_cursor_flag. */
1502 format_cb_keypad_cursor_flag(struct format_tree
*ft
)
1504 if (ft
->wp
!= NULL
) {
1505 if (ft
->wp
->base
.mode
& MODE_KCURSOR
)
1506 return (xstrdup("1"));
1507 return (xstrdup("0"));
1512 /* Callback for keypad_flag. */
1514 format_cb_keypad_flag(struct format_tree
*ft
)
1516 if (ft
->wp
!= NULL
) {
1517 if (ft
->wp
->base
.mode
& MODE_KKEYPAD
)
1518 return (xstrdup("1"));
1519 return (xstrdup("0"));
1524 /* Callback for mouse_all_flag. */
1526 format_cb_mouse_all_flag(struct format_tree
*ft
)
1528 if (ft
->wp
!= NULL
) {
1529 if (ft
->wp
->base
.mode
& MODE_MOUSE_ALL
)
1530 return (xstrdup("1"));
1531 return (xstrdup("0"));
1536 /* Callback for mouse_any_flag. */
1538 format_cb_mouse_any_flag(struct format_tree
*ft
)
1540 if (ft
->wp
!= NULL
) {
1541 if (ft
->wp
->base
.mode
& ALL_MOUSE_MODES
)
1542 return (xstrdup("1"));
1543 return (xstrdup("0"));
1548 /* Callback for mouse_button_flag. */
1550 format_cb_mouse_button_flag(struct format_tree
*ft
)
1552 if (ft
->wp
!= NULL
) {
1553 if (ft
->wp
->base
.mode
& MODE_MOUSE_BUTTON
)
1554 return (xstrdup("1"));
1555 return (xstrdup("0"));
1560 /* Callback for mouse_pane. */
1562 format_cb_mouse_pane(struct format_tree
*ft
)
1564 struct window_pane
*wp
;
1567 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1569 return (format_printf("%%%u", wp
->id
));
1575 /* Callback for mouse_sgr_flag. */
1577 format_cb_mouse_sgr_flag(struct format_tree
*ft
)
1579 if (ft
->wp
!= NULL
) {
1580 if (ft
->wp
->base
.mode
& MODE_MOUSE_SGR
)
1581 return (xstrdup("1"));
1582 return (xstrdup("0"));
1587 /* Callback for mouse_standard_flag. */
1589 format_cb_mouse_standard_flag(struct format_tree
*ft
)
1591 if (ft
->wp
!= NULL
) {
1592 if (ft
->wp
->base
.mode
& MODE_MOUSE_STANDARD
)
1593 return (xstrdup("1"));
1594 return (xstrdup("0"));
1599 /* Callback for mouse_utf8_flag. */
1601 format_cb_mouse_utf8_flag(struct format_tree
*ft
)
1603 if (ft
->wp
!= NULL
) {
1604 if (ft
->wp
->base
.mode
& MODE_MOUSE_UTF8
)
1605 return (xstrdup("1"));
1606 return (xstrdup("0"));
1611 /* Callback for mouse_x. */
1613 format_cb_mouse_x(struct format_tree
*ft
)
1615 struct window_pane
*wp
;
1620 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1621 if (wp
!= NULL
&& cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) == 0)
1622 return (format_printf("%u", x
));
1623 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
)) {
1624 if (ft
->m
.statusat
== 0 && ft
->m
.y
< ft
->m
.statuslines
)
1625 return (format_printf("%u", ft
->m
.x
));
1626 if (ft
->m
.statusat
> 0 && ft
->m
.y
>= (u_int
)ft
->m
.statusat
)
1627 return (format_printf("%u", ft
->m
.x
));
1632 /* Callback for mouse_y. */
1634 format_cb_mouse_y(struct format_tree
*ft
)
1636 struct window_pane
*wp
;
1641 wp
= cmd_mouse_pane(&ft
->m
, NULL
, NULL
);
1642 if (wp
!= NULL
&& cmd_mouse_at(wp
, &ft
->m
, &x
, &y
, 0) == 0)
1643 return (format_printf("%u", y
));
1644 if (ft
->c
!= NULL
&& (ft
->c
->tty
.flags
& TTY_STARTED
)) {
1645 if (ft
->m
.statusat
== 0 && ft
->m
.y
< ft
->m
.statuslines
)
1646 return (format_printf("%u", ft
->m
.y
));
1647 if (ft
->m
.statusat
> 0 && ft
->m
.y
>= (u_int
)ft
->m
.statusat
)
1648 return (format_printf("%u", ft
->m
.y
- ft
->m
.statusat
));
1653 /* Callback for origin_flag. */
1655 format_cb_origin_flag(struct format_tree
*ft
)
1657 if (ft
->wp
!= NULL
) {
1658 if (ft
->wp
->base
.mode
& MODE_ORIGIN
)
1659 return (xstrdup("1"));
1660 return (xstrdup("0"));
1665 /* Callback for pane_active. */
1667 format_cb_pane_active(struct format_tree
*ft
)
1669 if (ft
->wp
!= NULL
) {
1670 if (ft
->wp
== ft
->wp
->window
->active
)
1671 return (xstrdup("1"));
1672 return (xstrdup("0"));
1677 /* Callback for pane_at_left. */
1679 format_cb_pane_at_left(struct format_tree
*ft
)
1681 if (ft
->wp
!= NULL
) {
1682 if (ft
->wp
->xoff
== 0)
1683 return (xstrdup("1"));
1684 return (xstrdup("0"));
1689 /* Callback for pane_at_right. */
1691 format_cb_pane_at_right(struct format_tree
*ft
)
1693 if (ft
->wp
!= NULL
) {
1694 if (ft
->wp
->xoff
+ ft
->wp
->sx
== ft
->wp
->window
->sx
)
1695 return (xstrdup("1"));
1696 return (xstrdup("0"));
1701 /* Callback for pane_bottom. */
1703 format_cb_pane_bottom(struct format_tree
*ft
)
1706 return (format_printf("%u", ft
->wp
->yoff
+ ft
->wp
->sy
- 1));
1710 /* Callback for pane_dead. */
1712 format_cb_pane_dead(struct format_tree
*ft
)
1714 if (ft
->wp
!= NULL
) {
1715 if (ft
->wp
->fd
== -1)
1716 return (xstrdup("1"));
1717 return (xstrdup("0"));
1722 /* Callback for pane_dead_status. */
1724 format_cb_pane_dead_status(struct format_tree
*ft
)
1726 struct window_pane
*wp
= ft
->wp
;
1729 if ((wp
->flags
& PANE_STATUSREADY
) && WIFEXITED(wp
->status
))
1730 return (format_printf("%d", WEXITSTATUS(wp
->status
)));
1736 /* Callback for pane_format. */
1738 format_cb_pane_format(struct format_tree
*ft
)
1740 if (ft
->type
== FORMAT_TYPE_PANE
)
1741 return (xstrdup("1"));
1742 return (xstrdup("0"));
1745 /* Callback for pane_height. */
1747 format_cb_pane_height(struct format_tree
*ft
)
1750 return (format_printf("%u", ft
->wp
->sy
));
1754 /* Callback for pane_id. */
1756 format_cb_pane_id(struct format_tree
*ft
)
1759 return (format_printf("%%%u", ft
->wp
->id
));
1763 /* Callback for pane_index. */
1765 format_cb_pane_index(struct format_tree
*ft
)
1769 if (ft
->wp
!= NULL
&& window_pane_index(ft
->wp
, &idx
) == 0)
1770 return (format_printf("%u", idx
));
1774 /* Callback for pane_input_off. */
1776 format_cb_pane_input_off(struct format_tree
*ft
)
1778 if (ft
->wp
!= NULL
) {
1779 if (ft
->wp
->flags
& PANE_INPUTOFF
)
1780 return (xstrdup("1"));
1781 return (xstrdup("0"));
1786 /* Callback for pane_last. */
1788 format_cb_pane_last(struct format_tree
*ft
)
1790 if (ft
->wp
!= NULL
) {
1791 if (ft
->wp
== ft
->wp
->window
->last
)
1792 return (xstrdup("1"));
1793 return (xstrdup("0"));
1798 /* Callback for pane_left. */
1800 format_cb_pane_left(struct format_tree
*ft
)
1803 return (format_printf("%u", ft
->wp
->xoff
));
1807 /* Callback for pane_marked. */
1809 format_cb_pane_marked(struct format_tree
*ft
)
1811 if (ft
->wp
!= NULL
) {
1812 if (server_check_marked() && marked_pane
.wp
== ft
->wp
)
1813 return (xstrdup("1"));
1814 return (xstrdup("0"));
1819 /* Callback for pane_marked_set. */
1821 format_cb_pane_marked_set(struct format_tree
*ft
)
1823 if (ft
->wp
!= NULL
) {
1824 if (server_check_marked())
1825 return (xstrdup("1"));
1826 return (xstrdup("0"));
1831 /* Callback for pane_mode. */
1833 format_cb_pane_mode(struct format_tree
*ft
)
1835 struct window_mode_entry
*wme
;
1837 if (ft
->wp
!= NULL
) {
1838 wme
= TAILQ_FIRST(&ft
->wp
->modes
);
1840 return (xstrdup(wme
->mode
->name
));
1846 /* Callback for pane_path. */
1848 format_cb_pane_path(struct format_tree
*ft
)
1850 if (ft
->wp
!= NULL
) {
1851 if (ft
->wp
->base
.path
== NULL
)
1852 return (xstrdup(""));
1853 return (xstrdup(ft
->wp
->base
.path
));
1858 /* Callback for pane_pid. */
1860 format_cb_pane_pid(struct format_tree
*ft
)
1863 return (format_printf("%ld", (long)ft
->wp
->pid
));
1867 /* Callback for pane_pipe. */
1869 format_cb_pane_pipe(struct format_tree
*ft
)
1871 if (ft
->wp
!= NULL
) {
1872 if (ft
->wp
->pipe_fd
!= -1)
1873 return (xstrdup("1"));
1874 return (xstrdup("0"));
1879 /* Callback for pane_right. */
1881 format_cb_pane_right(struct format_tree
*ft
)
1884 return (format_printf("%u", ft
->wp
->xoff
+ ft
->wp
->sx
- 1));
1888 /* Callback for pane_search_string. */
1890 format_cb_pane_search_string(struct format_tree
*ft
)
1892 if (ft
->wp
!= NULL
) {
1893 if (ft
->wp
->searchstr
== NULL
)
1894 return (xstrdup(""));
1895 return (xstrdup(ft
->wp
->searchstr
));
1900 /* Callback for pane_synchronized. */
1902 format_cb_pane_synchronized(struct format_tree
*ft
)
1904 if (ft
->wp
!= NULL
) {
1905 if (options_get_number(ft
->wp
->options
, "synchronize-panes"))
1906 return (xstrdup("1"));
1907 return (xstrdup("0"));
1912 /* Callback for pane_title. */
1914 format_cb_pane_title(struct format_tree
*ft
)
1917 return (xstrdup(ft
->wp
->base
.title
));
1921 /* Callback for pane_top. */
1923 format_cb_pane_top(struct format_tree
*ft
)
1926 return (format_printf("%u", ft
->wp
->yoff
));
1930 /* Callback for pane_tty. */
1932 format_cb_pane_tty(struct format_tree
*ft
)
1935 return (xstrdup(ft
->wp
->tty
));
1939 /* Callback for pane_width. */
1941 format_cb_pane_width(struct format_tree
*ft
)
1944 return (format_printf("%u", ft
->wp
->sx
));
1948 /* Callback for scroll_region_lower. */
1950 format_cb_scroll_region_lower(struct format_tree
*ft
)
1953 return (format_printf("%u", ft
->wp
->base
.rlower
));
1957 /* Callback for scroll_region_upper. */
1959 format_cb_scroll_region_upper(struct format_tree
*ft
)
1962 return (format_printf("%u", ft
->wp
->base
.rupper
));
1966 /* Callback for session_attached. */
1968 format_cb_session_attached(struct format_tree
*ft
)
1971 return (format_printf("%u", ft
->s
->attached
));
1975 /* Callback for session_format. */
1977 format_cb_session_format(struct format_tree
*ft
)
1979 if (ft
->type
== FORMAT_TYPE_SESSION
)
1980 return (xstrdup("1"));
1981 return (xstrdup("0"));
1984 /* Callback for session_group. */
1986 format_cb_session_group(struct format_tree
*ft
)
1988 struct session_group
*sg
;
1990 if (ft
->s
!= NULL
&& (sg
= session_group_contains(ft
->s
)) != NULL
)
1991 return (xstrdup(sg
->name
));
1995 /* Callback for session_group_attached. */
1997 format_cb_session_group_attached(struct format_tree
*ft
)
1999 struct session_group
*sg
;
2001 if (ft
->s
!= NULL
&& (sg
= session_group_contains(ft
->s
)) != NULL
)
2002 return (format_printf("%u", session_group_attached_count (sg
)));
2006 /* Callback for session_group_many_attached. */
2008 format_cb_session_group_many_attached(struct format_tree
*ft
)
2010 struct session_group
*sg
;
2012 if (ft
->s
!= NULL
&& (sg
= session_group_contains(ft
->s
)) != NULL
) {
2013 if (session_group_attached_count (sg
) > 1)
2014 return (xstrdup("1"));
2015 return (xstrdup("0"));
2020 /* Callback for session_group_size. */
2022 format_cb_session_group_size(struct format_tree
*ft
)
2024 struct session_group
*sg
;
2026 if (ft
->s
!= NULL
&& (sg
= session_group_contains(ft
->s
)) != NULL
)
2027 return (format_printf("%u", session_group_count (sg
)));
2031 /* Callback for session_grouped. */
2033 format_cb_session_grouped(struct format_tree
*ft
)
2035 if (ft
->s
!= NULL
) {
2036 if (session_group_contains(ft
->s
) != NULL
)
2037 return (xstrdup("1"));
2038 return (xstrdup("0"));
2043 /* Callback for session_id. */
2045 format_cb_session_id(struct format_tree
*ft
)
2048 return (format_printf("$%u", ft
->s
->id
));
2052 /* Callback for session_many_attached. */
2054 format_cb_session_many_attached(struct format_tree
*ft
)
2056 if (ft
->s
!= NULL
) {
2057 if (ft
->s
->attached
> 1)
2058 return (xstrdup("1"));
2059 return (xstrdup("0"));
2064 /* Callback for session_marked. */
2066 format_cb_session_marked(struct format_tree
*ft
)
2068 if (ft
->s
!= NULL
) {
2069 if (server_check_marked() && marked_pane
.s
== ft
->s
)
2070 return (xstrdup("1"));
2071 return (xstrdup("0"));
2076 /* Callback for session_name. */
2078 format_cb_session_name(struct format_tree
*ft
)
2081 return (xstrdup(ft
->s
->name
));
2085 /* Callback for session_path. */
2087 format_cb_session_path(struct format_tree
*ft
)
2090 return (xstrdup(ft
->s
->cwd
));
2094 /* Callback for session_windows. */
2096 format_cb_session_windows(struct format_tree
*ft
)
2099 return (format_printf("%u", winlink_count(&ft
->s
->windows
)));
2103 /* Callback for socket_path. */
2105 format_cb_socket_path(__unused
struct format_tree
*ft
)
2107 return (xstrdup(socket_path
));
2110 /* Callback for version. */
2112 format_cb_version(__unused
struct format_tree
*ft
)
2114 return (xstrdup(getversion()));
2117 /* Callback for active_window_index. */
2119 format_cb_active_window_index(struct format_tree
*ft
)
2122 return (format_printf("%u", ft
->s
->curw
->idx
));
2126 /* Callback for last_window_index. */
2128 format_cb_last_window_index(struct format_tree
*ft
)
2132 if (ft
->s
!= NULL
) {
2133 wl
= RB_MAX(winlinks
, &ft
->s
->windows
);
2134 return (format_printf("%u", wl
->idx
));
2139 /* Callback for window_active. */
2141 format_cb_window_active(struct format_tree
*ft
)
2143 if (ft
->wl
!= NULL
) {
2144 if (ft
->wl
== ft
->wl
->session
->curw
)
2145 return (xstrdup("1"));
2146 return (xstrdup("0"));
2151 /* Callback for window_activity_flag. */
2153 format_cb_window_activity_flag(struct format_tree
*ft
)
2155 if (ft
->wl
!= NULL
) {
2156 if (ft
->wl
->flags
& WINLINK_ACTIVITY
)
2157 return (xstrdup("1"));
2158 return (xstrdup("0"));
2163 /* Callback for window_bell_flag. */
2165 format_cb_window_bell_flag(struct format_tree
*ft
)
2167 if (ft
->wl
!= NULL
) {
2168 if (ft
->wl
->flags
& WINLINK_BELL
)
2169 return (xstrdup("1"));
2170 return (xstrdup("0"));
2175 /* Callback for window_bigger. */
2177 format_cb_window_bigger(struct format_tree
*ft
)
2179 u_int ox
, oy
, sx
, sy
;
2181 if (ft
->c
!= NULL
) {
2182 if (tty_window_offset(&ft
->c
->tty
, &ox
, &oy
, &sx
, &sy
))
2183 return (xstrdup("1"));
2184 return (xstrdup("0"));
2189 /* Callback for window_cell_height. */
2191 format_cb_window_cell_height(struct format_tree
*ft
)
2194 return (format_printf("%u", ft
->w
->ypixel
));
2198 /* Callback for window_cell_width. */
2200 format_cb_window_cell_width(struct format_tree
*ft
)
2203 return (format_printf("%u", ft
->w
->xpixel
));
2207 /* Callback for window_end_flag. */
2209 format_cb_window_end_flag(struct format_tree
*ft
)
2211 if (ft
->wl
!= NULL
) {
2212 if (ft
->wl
== RB_MAX(winlinks
, &ft
->wl
->session
->windows
))
2213 return (xstrdup("1"));
2214 return (xstrdup("0"));
2219 /* Callback for window_flags. */
2221 format_cb_window_flags(struct format_tree
*ft
)
2224 return (xstrdup(window_printable_flags(ft
->wl
, 1)));
2228 /* Callback for window_format. */
2230 format_cb_window_format(struct format_tree
*ft
)
2232 if (ft
->type
== FORMAT_TYPE_WINDOW
)
2233 return (xstrdup("1"));
2234 return (xstrdup("0"));
2237 /* Callback for window_height. */
2239 format_cb_window_height(struct format_tree
*ft
)
2242 return (format_printf("%u", ft
->w
->sy
));
2246 /* Callback for window_id. */
2248 format_cb_window_id(struct format_tree
*ft
)
2251 return (format_printf("@%u", ft
->w
->id
));
2255 /* Callback for window_index. */
2257 format_cb_window_index(struct format_tree
*ft
)
2260 return (format_printf("%d", ft
->wl
->idx
));
2264 /* Callback for window_last_flag. */
2266 format_cb_window_last_flag(struct format_tree
*ft
)
2268 if (ft
->wl
!= NULL
) {
2269 if (ft
->wl
== TAILQ_FIRST(&ft
->wl
->session
->lastw
))
2270 return (xstrdup("1"));
2271 return (xstrdup("0"));
2276 /* Callback for window_linked. */
2278 format_cb_window_linked(struct format_tree
*ft
)
2280 if (ft
->wl
!= NULL
) {
2281 if (session_is_linked(ft
->wl
->session
, ft
->wl
->window
))
2282 return (xstrdup("1"));
2283 return (xstrdup("0"));
2288 /* Callback for window_linked_sessions. */
2290 format_cb_window_linked_sessions(struct format_tree
*ft
)
2293 return (format_printf("%u", ft
->wl
->window
->references
));
2297 /* Callback for window_marked_flag. */
2299 format_cb_window_marked_flag(struct format_tree
*ft
)
2301 if (ft
->wl
!= NULL
) {
2302 if (server_check_marked() && marked_pane
.wl
== ft
->wl
)
2303 return (xstrdup("1"));
2304 return (xstrdup("0"));
2309 /* Callback for window_name. */
2311 format_cb_window_name(struct format_tree
*ft
)
2314 return (format_printf("%s", ft
->w
->name
));
2318 /* Callback for window_offset_x. */
2320 format_cb_window_offset_x(struct format_tree
*ft
)
2322 u_int ox
, oy
, sx
, sy
;
2324 if (ft
->c
!= NULL
) {
2325 if (tty_window_offset(&ft
->c
->tty
, &ox
, &oy
, &sx
, &sy
))
2326 return (format_printf("%u", ox
));
2332 /* Callback for window_offset_y. */
2334 format_cb_window_offset_y(struct format_tree
*ft
)
2336 u_int ox
, oy
, sx
, sy
;
2338 if (ft
->c
!= NULL
) {
2339 if (tty_window_offset(&ft
->c
->tty
, &ox
, &oy
, &sx
, &sy
))
2340 return (format_printf("%u", oy
));
2346 /* Callback for window_panes. */
2348 format_cb_window_panes(struct format_tree
*ft
)
2351 return (format_printf("%u", window_count_panes(ft
->w
)));
2355 /* Callback for window_raw_flags. */
2357 format_cb_window_raw_flags(struct format_tree
*ft
)
2360 return (xstrdup(window_printable_flags(ft
->wl
, 0)));
2364 /* Callback for window_silence_flag. */
2366 format_cb_window_silence_flag(struct format_tree
*ft
)
2368 if (ft
->wl
!= NULL
) {
2369 if (ft
->wl
->flags
& WINLINK_SILENCE
)
2370 return (xstrdup("1"));
2371 return (xstrdup("0"));
2376 /* Callback for window_start_flag. */
2378 format_cb_window_start_flag(struct format_tree
*ft
)
2380 if (ft
->wl
!= NULL
) {
2381 if (ft
->wl
== RB_MIN(winlinks
, &ft
->wl
->session
->windows
))
2382 return (xstrdup("1"));
2383 return (xstrdup("0"));
2388 /* Callback for window_width. */
2390 format_cb_window_width(struct format_tree
*ft
)
2393 return (format_printf("%u", ft
->w
->sx
));
2397 /* Callback for window_zoomed_flag. */
2399 format_cb_window_zoomed_flag(struct format_tree
*ft
)
2401 if (ft
->w
!= NULL
) {
2402 if (ft
->w
->flags
& WINDOW_ZOOMED
)
2403 return (xstrdup("1"));
2404 return (xstrdup("0"));
2409 /* Callback for wrap_flag. */
2411 format_cb_wrap_flag(struct format_tree
*ft
)
2413 if (ft
->wp
!= NULL
) {
2414 if (ft
->wp
->base
.mode
& MODE_WRAP
)
2415 return (xstrdup("1"));
2416 return (xstrdup("0"));
2421 /* Callback for buffer_created. */
2423 format_cb_buffer_created(struct format_tree
*ft
)
2425 static struct timeval tv
;
2427 if (ft
->pb
!= NULL
) {
2429 tv
.tv_sec
= paste_buffer_created(ft
->pb
);
2435 /* Callback for client_activity. */
2437 format_cb_client_activity(struct format_tree
*ft
)
2440 return (&ft
->c
->activity_time
);
2444 /* Callback for client_created. */
2446 format_cb_client_created(struct format_tree
*ft
)
2449 return (&ft
->c
->creation_time
);
2453 /* Callback for session_activity. */
2455 format_cb_session_activity(struct format_tree
*ft
)
2458 return (&ft
->s
->activity_time
);
2462 /* Callback for session_created. */
2464 format_cb_session_created(struct format_tree
*ft
)
2467 return (&ft
->s
->creation_time
);
2471 /* Callback for session_last_attached. */
2473 format_cb_session_last_attached(struct format_tree
*ft
)
2476 return (&ft
->s
->last_attached_time
);
2480 /* Callback for start_time. */
2482 format_cb_start_time(__unused
struct format_tree
*ft
)
2484 return (&start_time
);
2487 /* Callback for window_activity. */
2489 format_cb_window_activity(struct format_tree
*ft
)
2492 return (&ft
->w
->activity_time
);
2496 /* Callback for buffer_mode_format, */
2498 format_cb_buffer_mode_format(__unused
struct format_tree
*ft
)
2500 return (xstrdup(window_buffer_mode
.default_format
));
2503 /* Callback for client_mode_format, */
2505 format_cb_client_mode_format(__unused
struct format_tree
*ft
)
2507 return (xstrdup(window_client_mode
.default_format
));
2510 /* Callback for tree_mode_format, */
2512 format_cb_tree_mode_format(__unused
struct format_tree
*ft
)
2514 return (xstrdup(window_tree_mode
.default_format
));
2517 /* Format table type. */
2518 enum format_table_type
{
2519 FORMAT_TABLE_STRING
,
2523 /* Format table entry. */
2524 struct format_table_entry
{
2526 enum format_table_type type
;
2531 * Format table. Default format variables (that are almost always in the tree
2532 * and where the value is expanded by a callback in this file) are listed here.
2533 * Only variables which are added by the caller go into the tree.
2535 static const struct format_table_entry format_table
[] = {
2536 { "active_window_index", FORMAT_TABLE_STRING
,
2537 format_cb_active_window_index
2539 { "alternate_on", FORMAT_TABLE_STRING
,
2540 format_cb_alternate_on
2542 { "alternate_saved_x", FORMAT_TABLE_STRING
,
2543 format_cb_alternate_saved_x
2545 { "alternate_saved_y", FORMAT_TABLE_STRING
,
2546 format_cb_alternate_saved_y
2548 { "buffer_created", FORMAT_TABLE_TIME
,
2549 format_cb_buffer_created
2551 { "buffer_mode_format", FORMAT_TABLE_STRING
,
2552 format_cb_buffer_mode_format
2554 { "buffer_name", FORMAT_TABLE_STRING
,
2555 format_cb_buffer_name
2557 { "buffer_sample", FORMAT_TABLE_STRING
,
2558 format_cb_buffer_sample
2560 { "buffer_size", FORMAT_TABLE_STRING
,
2561 format_cb_buffer_size
2563 { "client_activity", FORMAT_TABLE_TIME
,
2564 format_cb_client_activity
2566 { "client_cell_height", FORMAT_TABLE_STRING
,
2567 format_cb_client_cell_height
2569 { "client_cell_width", FORMAT_TABLE_STRING
,
2570 format_cb_client_cell_width
2572 { "client_control_mode", FORMAT_TABLE_STRING
,
2573 format_cb_client_control_mode
2575 { "client_created", FORMAT_TABLE_TIME
,
2576 format_cb_client_created
2578 { "client_discarded", FORMAT_TABLE_STRING
,
2579 format_cb_client_discarded
2581 { "client_flags", FORMAT_TABLE_STRING
,
2582 format_cb_client_flags
2584 { "client_height", FORMAT_TABLE_STRING
,
2585 format_cb_client_height
2587 { "client_key_table", FORMAT_TABLE_STRING
,
2588 format_cb_client_key_table
2590 { "client_last_session", FORMAT_TABLE_STRING
,
2591 format_cb_client_last_session
2593 { "client_mode_format", FORMAT_TABLE_STRING
,
2594 format_cb_client_mode_format
2596 { "client_name", FORMAT_TABLE_STRING
,
2597 format_cb_client_name
2599 { "client_pid", FORMAT_TABLE_STRING
,
2600 format_cb_client_pid
2602 { "client_prefix", FORMAT_TABLE_STRING
,
2603 format_cb_client_prefix
2605 { "client_readonly", FORMAT_TABLE_STRING
,
2606 format_cb_client_readonly
2608 { "client_session", FORMAT_TABLE_STRING
,
2609 format_cb_client_session
2611 { "client_termfeatures", FORMAT_TABLE_STRING
,
2612 format_cb_client_termfeatures
2614 { "client_termname", FORMAT_TABLE_STRING
,
2615 format_cb_client_termname
2617 { "client_termtype", FORMAT_TABLE_STRING
,
2618 format_cb_client_termtype
2620 { "client_tty", FORMAT_TABLE_STRING
,
2621 format_cb_client_tty
2623 { "client_utf8", FORMAT_TABLE_STRING
,
2624 format_cb_client_utf8
2626 { "client_width", FORMAT_TABLE_STRING
,
2627 format_cb_client_width
2629 { "client_written", FORMAT_TABLE_STRING
,
2630 format_cb_client_written
2632 { "config_files", FORMAT_TABLE_STRING
,
2633 format_cb_config_files
2635 { "cursor_character", FORMAT_TABLE_STRING
,
2636 format_cb_cursor_character
2638 { "cursor_flag", FORMAT_TABLE_STRING
,
2639 format_cb_cursor_flag
2641 { "cursor_x", FORMAT_TABLE_STRING
,
2644 { "cursor_y", FORMAT_TABLE_STRING
,
2647 { "history_all_bytes", FORMAT_TABLE_STRING
,
2648 format_cb_history_all_bytes
2650 { "history_bytes", FORMAT_TABLE_STRING
,
2651 format_cb_history_bytes
2653 { "history_limit", FORMAT_TABLE_STRING
,
2654 format_cb_history_limit
2656 { "history_size", FORMAT_TABLE_STRING
,
2657 format_cb_history_size
2659 { "host", FORMAT_TABLE_STRING
,
2662 { "host_short", FORMAT_TABLE_STRING
,
2663 format_cb_host_short
2665 { "insert_flag", FORMAT_TABLE_STRING
,
2666 format_cb_insert_flag
2668 { "keypad_cursor_flag", FORMAT_TABLE_STRING
,
2669 format_cb_keypad_cursor_flag
2671 { "keypad_flag", FORMAT_TABLE_STRING
,
2672 format_cb_keypad_flag
2674 { "last_window_index", FORMAT_TABLE_STRING
,
2675 format_cb_last_window_index
2677 { "mouse_all_flag", FORMAT_TABLE_STRING
,
2678 format_cb_mouse_all_flag
2680 { "mouse_any_flag", FORMAT_TABLE_STRING
,
2681 format_cb_mouse_any_flag
2683 { "mouse_button_flag", FORMAT_TABLE_STRING
,
2684 format_cb_mouse_button_flag
2686 { "mouse_line", FORMAT_TABLE_STRING
,
2687 format_cb_mouse_line
2689 { "mouse_pane", FORMAT_TABLE_STRING
,
2690 format_cb_mouse_pane
2692 { "mouse_sgr_flag", FORMAT_TABLE_STRING
,
2693 format_cb_mouse_sgr_flag
2695 { "mouse_standard_flag", FORMAT_TABLE_STRING
,
2696 format_cb_mouse_standard_flag
2698 { "mouse_utf8_flag", FORMAT_TABLE_STRING
,
2699 format_cb_mouse_utf8_flag
2701 { "mouse_word", FORMAT_TABLE_STRING
,
2702 format_cb_mouse_word
2704 { "mouse_x", FORMAT_TABLE_STRING
,
2707 { "mouse_y", FORMAT_TABLE_STRING
,
2710 { "origin_flag", FORMAT_TABLE_STRING
,
2711 format_cb_origin_flag
2713 { "pane_active", FORMAT_TABLE_STRING
,
2714 format_cb_pane_active
2716 { "pane_at_bottom", FORMAT_TABLE_STRING
,
2717 format_cb_pane_at_bottom
2719 { "pane_at_left", FORMAT_TABLE_STRING
,
2720 format_cb_pane_at_left
2722 { "pane_at_right", FORMAT_TABLE_STRING
,
2723 format_cb_pane_at_right
2725 { "pane_at_top", FORMAT_TABLE_STRING
,
2726 format_cb_pane_at_top
2728 { "pane_bg", FORMAT_TABLE_STRING
,
2731 { "pane_bottom", FORMAT_TABLE_STRING
,
2732 format_cb_pane_bottom
2734 { "pane_current_command", FORMAT_TABLE_STRING
,
2735 format_cb_current_command
2737 { "pane_current_path", FORMAT_TABLE_STRING
,
2738 format_cb_current_path
2740 { "pane_dead", FORMAT_TABLE_STRING
,
2743 { "pane_dead_status", FORMAT_TABLE_STRING
,
2744 format_cb_pane_dead_status
2746 { "pane_fg", FORMAT_TABLE_STRING
,
2749 { "pane_format", FORMAT_TABLE_STRING
,
2750 format_cb_pane_format
2752 { "pane_height", FORMAT_TABLE_STRING
,
2753 format_cb_pane_height
2755 { "pane_id", FORMAT_TABLE_STRING
,
2758 { "pane_in_mode", FORMAT_TABLE_STRING
,
2759 format_cb_pane_in_mode
2761 { "pane_index", FORMAT_TABLE_STRING
,
2762 format_cb_pane_index
2764 { "pane_input_off", FORMAT_TABLE_STRING
,
2765 format_cb_pane_input_off
2767 { "pane_last", FORMAT_TABLE_STRING
,
2770 { "pane_left", FORMAT_TABLE_STRING
,
2773 { "pane_marked", FORMAT_TABLE_STRING
,
2774 format_cb_pane_marked
2776 { "pane_marked_set", FORMAT_TABLE_STRING
,
2777 format_cb_pane_marked_set
2779 { "pane_mode", FORMAT_TABLE_STRING
,
2782 { "pane_path", FORMAT_TABLE_STRING
,
2785 { "pane_pid", FORMAT_TABLE_STRING
,
2788 { "pane_pipe", FORMAT_TABLE_STRING
,
2791 { "pane_right", FORMAT_TABLE_STRING
,
2792 format_cb_pane_right
2794 { "pane_search_string", FORMAT_TABLE_STRING
,
2795 format_cb_pane_search_string
2797 { "pane_start_command", FORMAT_TABLE_STRING
,
2798 format_cb_start_command
2800 { "pane_synchronized", FORMAT_TABLE_STRING
,
2801 format_cb_pane_synchronized
2803 { "pane_tabs", FORMAT_TABLE_STRING
,
2806 { "pane_title", FORMAT_TABLE_STRING
,
2807 format_cb_pane_title
2809 { "pane_top", FORMAT_TABLE_STRING
,
2812 { "pane_tty", FORMAT_TABLE_STRING
,
2815 { "pane_width", FORMAT_TABLE_STRING
,
2816 format_cb_pane_width
2818 { "pid", FORMAT_TABLE_STRING
,
2821 { "scroll_region_lower", FORMAT_TABLE_STRING
,
2822 format_cb_scroll_region_lower
2824 { "scroll_region_upper", FORMAT_TABLE_STRING
,
2825 format_cb_scroll_region_upper
2827 { "session_activity", FORMAT_TABLE_TIME
,
2828 format_cb_session_activity
2830 { "session_alerts", FORMAT_TABLE_STRING
,
2831 format_cb_session_alerts
2833 { "session_attached", FORMAT_TABLE_STRING
,
2834 format_cb_session_attached
2836 { "session_attached_list", FORMAT_TABLE_STRING
,
2837 format_cb_session_attached_list
2839 { "session_created", FORMAT_TABLE_TIME
,
2840 format_cb_session_created
2842 { "session_format", FORMAT_TABLE_STRING
,
2843 format_cb_session_format
2845 { "session_group", FORMAT_TABLE_STRING
,
2846 format_cb_session_group
2848 { "session_group_attached", FORMAT_TABLE_STRING
,
2849 format_cb_session_group_attached
2851 { "session_group_attached_list", FORMAT_TABLE_STRING
,
2852 format_cb_session_group_attached_list
2854 { "session_group_list", FORMAT_TABLE_STRING
,
2855 format_cb_session_group_list
2857 { "session_group_many_attached", FORMAT_TABLE_STRING
,
2858 format_cb_session_group_many_attached
2860 { "session_group_size", FORMAT_TABLE_STRING
,
2861 format_cb_session_group_size
2863 { "session_grouped", FORMAT_TABLE_STRING
,
2864 format_cb_session_grouped
2866 { "session_id", FORMAT_TABLE_STRING
,
2867 format_cb_session_id
2869 { "session_last_attached", FORMAT_TABLE_TIME
,
2870 format_cb_session_last_attached
2872 { "session_many_attached", FORMAT_TABLE_STRING
,
2873 format_cb_session_many_attached
2875 { "session_marked", FORMAT_TABLE_STRING
,
2876 format_cb_session_marked
,
2878 { "session_name", FORMAT_TABLE_STRING
,
2879 format_cb_session_name
2881 { "session_path", FORMAT_TABLE_STRING
,
2882 format_cb_session_path
2884 { "session_stack", FORMAT_TABLE_STRING
,
2885 format_cb_session_stack
2887 { "session_windows", FORMAT_TABLE_STRING
,
2888 format_cb_session_windows
2890 { "socket_path", FORMAT_TABLE_STRING
,
2891 format_cb_socket_path
2893 { "start_time", FORMAT_TABLE_TIME
,
2894 format_cb_start_time
2896 { "tree_mode_format", FORMAT_TABLE_STRING
,
2897 format_cb_tree_mode_format
2899 { "version", FORMAT_TABLE_STRING
,
2902 { "window_active", FORMAT_TABLE_STRING
,
2903 format_cb_window_active
2905 { "window_active_clients", FORMAT_TABLE_STRING
,
2906 format_cb_window_active_clients
2908 { "window_active_clients_list", FORMAT_TABLE_STRING
,
2909 format_cb_window_active_clients_list
2911 { "window_active_sessions", FORMAT_TABLE_STRING
,
2912 format_cb_window_active_sessions
2914 { "window_active_sessions_list", FORMAT_TABLE_STRING
,
2915 format_cb_window_active_sessions_list
2917 { "window_activity", FORMAT_TABLE_TIME
,
2918 format_cb_window_activity
2920 { "window_activity_flag", FORMAT_TABLE_STRING
,
2921 format_cb_window_activity_flag
2923 { "window_bell_flag", FORMAT_TABLE_STRING
,
2924 format_cb_window_bell_flag
2926 { "window_bigger", FORMAT_TABLE_STRING
,
2927 format_cb_window_bigger
2929 { "window_cell_height", FORMAT_TABLE_STRING
,
2930 format_cb_window_cell_height
2932 { "window_cell_width", FORMAT_TABLE_STRING
,
2933 format_cb_window_cell_width
2935 { "window_end_flag", FORMAT_TABLE_STRING
,
2936 format_cb_window_end_flag
2938 { "window_flags", FORMAT_TABLE_STRING
,
2939 format_cb_window_flags
2941 { "window_format", FORMAT_TABLE_STRING
,
2942 format_cb_window_format
2944 { "window_height", FORMAT_TABLE_STRING
,
2945 format_cb_window_height
2947 { "window_id", FORMAT_TABLE_STRING
,
2950 { "window_index", FORMAT_TABLE_STRING
,
2951 format_cb_window_index
2953 { "window_last_flag", FORMAT_TABLE_STRING
,
2954 format_cb_window_last_flag
2956 { "window_layout", FORMAT_TABLE_STRING
,
2957 format_cb_window_layout
2959 { "window_linked", FORMAT_TABLE_STRING
,
2960 format_cb_window_linked
2962 { "window_linked_sessions", FORMAT_TABLE_STRING
,
2963 format_cb_window_linked_sessions
2965 { "window_linked_sessions_list", FORMAT_TABLE_STRING
,
2966 format_cb_window_linked_sessions_list
2968 { "window_marked_flag", FORMAT_TABLE_STRING
,
2969 format_cb_window_marked_flag
2971 { "window_name", FORMAT_TABLE_STRING
,
2972 format_cb_window_name
2974 { "window_offset_x", FORMAT_TABLE_STRING
,
2975 format_cb_window_offset_x
2977 { "window_offset_y", FORMAT_TABLE_STRING
,
2978 format_cb_window_offset_y
2980 { "window_panes", FORMAT_TABLE_STRING
,
2981 format_cb_window_panes
2983 { "window_raw_flags", FORMAT_TABLE_STRING
,
2984 format_cb_window_raw_flags
2986 { "window_silence_flag", FORMAT_TABLE_STRING
,
2987 format_cb_window_silence_flag
2989 { "window_stack_index", FORMAT_TABLE_STRING
,
2990 format_cb_window_stack_index
2992 { "window_start_flag", FORMAT_TABLE_STRING
,
2993 format_cb_window_start_flag
2995 { "window_visible_layout", FORMAT_TABLE_STRING
,
2996 format_cb_window_visible_layout
2998 { "window_width", FORMAT_TABLE_STRING
,
2999 format_cb_window_width
3001 { "window_zoomed_flag", FORMAT_TABLE_STRING
,
3002 format_cb_window_zoomed_flag
3004 { "wrap_flag", FORMAT_TABLE_STRING
,
3009 /* Compare format table entries. */
3011 format_table_compare(const void *key0
, const void *entry0
)
3013 const char *key
= key0
;
3014 const struct format_table_entry
*entry
= entry0
;
3016 return (strcmp(key
, entry
->key
));
3019 /* Get a format callback. */
3020 static struct format_table_entry
*
3021 format_table_get(const char *key
)
3023 return (bsearch(key
, format_table
, nitems(format_table
),
3024 sizeof *format_table
, format_table_compare
));
3027 /* Merge one format tree into another. */
3029 format_merge(struct format_tree
*ft
, struct format_tree
*from
)
3031 struct format_entry
*fe
;
3033 RB_FOREACH(fe
, format_entry_tree
, &from
->tree
) {
3034 if (fe
->value
!= NULL
)
3035 format_add(ft
, fe
->key
, "%s", fe
->value
);
3039 /* Get format pane. */
3040 struct window_pane
*
3041 format_get_pane(struct format_tree
*ft
)
3046 /* Add item bits to tree. */
3048 format_create_add_item(struct format_tree
*ft
, struct cmdq_item
*item
)
3050 struct key_event
*event
= cmdq_get_event(item
);
3051 struct mouse_event
*m
= &event
->m
;
3053 cmdq_merge_formats(item
, ft
);
3054 memcpy(&ft
->m
, m
, sizeof ft
->m
);
3057 /* Create a new tree. */
3058 struct format_tree
*
3059 format_create(struct client
*c
, struct cmdq_item
*item
, int tag
, int flags
)
3061 struct format_tree
*ft
;
3063 ft
= xcalloc(1, sizeof *ft
);
3068 ft
->client
->references
++;
3076 format_create_add_item(ft
, item
);
3083 format_free(struct format_tree
*ft
)
3085 struct format_entry
*fe
, *fe1
;
3087 RB_FOREACH_SAFE(fe
, format_entry_tree
, &ft
->tree
, fe1
) {
3088 RB_REMOVE(format_entry_tree
, &ft
->tree
, fe
);
3094 if (ft
->client
!= NULL
)
3095 server_client_unref(ft
->client
);
3099 /* Log each format. */
3101 format_log_debug_cb(const char *key
, const char *value
, void *arg
)
3103 const char *prefix
= arg
;
3105 log_debug("%s: %s=%s", prefix
, key
, value
);
3108 /* Log a format tree. */
3110 format_log_debug(struct format_tree
*ft
, const char *prefix
)
3112 format_each(ft
, format_log_debug_cb
, (void *)prefix
);
3115 /* Walk each format. */
3117 format_each(struct format_tree
*ft
, void (*cb
)(const char *, const char *,
3120 const struct format_table_entry
*fte
;
3121 struct format_entry
*fe
;
3127 for (i
= 0; i
< nitems(format_table
); i
++) {
3128 fte
= &format_table
[i
];
3130 value
= fte
->cb(ft
);
3133 if (fte
->type
== FORMAT_TABLE_TIME
) {
3135 xsnprintf(s
, sizeof s
, "%lld", (long long)tv
->tv_sec
);
3136 cb(fte
->key
, s
, arg
);
3138 cb(fte
->key
, value
, arg
);
3142 RB_FOREACH(fe
, format_entry_tree
, &ft
->tree
) {
3143 if (fe
->time
!= 0) {
3144 xsnprintf(s
, sizeof s
, "%lld", (long long)fe
->time
);
3145 cb(fe
->key
, s
, arg
);
3147 if (fe
->value
== NULL
&& fe
->cb
!= NULL
) {
3148 fe
->value
= fe
->cb(ft
);
3149 if (fe
->value
== NULL
)
3150 fe
->value
= xstrdup("");
3152 cb(fe
->key
, fe
->value
, arg
);
3157 /* Add a key-value pair. */
3159 format_add(struct format_tree
*ft
, const char *key
, const char *fmt
, ...)
3161 struct format_entry
*fe
;
3162 struct format_entry
*fe_now
;
3165 fe
= xmalloc(sizeof *fe
);
3166 fe
->key
= xstrdup(key
);
3168 fe_now
= RB_INSERT(format_entry_tree
, &ft
->tree
, fe
);
3169 if (fe_now
!= NULL
) {
3172 free(fe_now
->value
);
3180 xvasprintf(&fe
->value
, fmt
, ap
);
3184 /* Add a key and time. */
3186 format_add_tv(struct format_tree
*ft
, const char *key
, struct timeval
*tv
)
3188 struct format_entry
*fe
, *fe_now
;
3190 fe
= xmalloc(sizeof *fe
);
3191 fe
->key
= xstrdup(key
);
3193 fe_now
= RB_INSERT(format_entry_tree
, &ft
->tree
, fe
);
3194 if (fe_now
!= NULL
) {
3197 free(fe_now
->value
);
3202 fe
->time
= tv
->tv_sec
;
3207 /* Add a key and function. */
3209 format_add_cb(struct format_tree
*ft
, const char *key
, format_cb cb
)
3211 struct format_entry
*fe
;
3212 struct format_entry
*fe_now
;
3214 fe
= xmalloc(sizeof *fe
);
3215 fe
->key
= xstrdup(key
);
3217 fe_now
= RB_INSERT(format_entry_tree
, &ft
->tree
, fe
);
3218 if (fe_now
!= NULL
) {
3221 free(fe_now
->value
);
3231 /* Quote shell special characters in string. */
3233 format_quote_shell(const char *s
)
3238 at
= out
= xmalloc(strlen(s
) * 2 + 1);
3239 for (cp
= s
; *cp
!= '\0'; cp
++) {
3240 if (strchr("|&;<>()$`\\\"'*?[# =%", *cp
) != NULL
)
3248 /* Quote #s in string. */
3250 format_quote_style(const char *s
)
3255 at
= out
= xmalloc(strlen(s
) * 2 + 1);
3256 for (cp
= s
; *cp
!= '\0'; cp
++) {
3265 /* Make a prettier time. */
3267 format_pretty_time(time_t t
)
3269 struct tm now_tm
, tm
;
3278 localtime_r(&now
, &now_tm
);
3279 localtime_r(&t
, &tm
);
3281 /* Last 24 hours. */
3282 if (age
< 24 * 3600) {
3283 strftime(s
, sizeof s
, "%H:%M", &tm
);
3284 return (xstrdup(s
));
3287 /* This month or last 28 days. */
3288 if ((tm
.tm_year
== now_tm
.tm_year
&& tm
.tm_mon
== now_tm
.tm_mon
) ||
3289 age
< 28 * 24 * 3600) {
3290 strftime(s
, sizeof s
, "%a%d", &tm
);
3291 return (xstrdup(s
));
3294 /* Last 12 months. */
3295 if ((tm
.tm_year
== now_tm
.tm_year
&& tm
.tm_mon
< now_tm
.tm_mon
) ||
3296 (tm
.tm_year
== now_tm
.tm_year
- 1 && tm
.tm_mon
> now_tm
.tm_mon
)) {
3297 strftime(s
, sizeof s
, "%d%b", &tm
);
3298 return (xstrdup(s
));
3301 /* Older than that. */
3302 strftime(s
, sizeof s
, "%h%y", &tm
);
3303 return (xstrdup(s
));
3306 /* Find a format entry. */
3308 format_find(struct format_tree
*ft
, const char *key
, int modifiers
,
3309 const char *time_format
)
3311 struct format_table_entry
*fte
;
3313 struct format_entry
*fe
, fe_find
;
3314 struct environ_entry
*envent
;
3315 struct options_entry
*o
;
3317 char *found
= NULL
, *saved
, s
[512];
3322 o
= options_parse_get(global_options
, key
, &idx
, 0);
3323 if (o
== NULL
&& ft
->wp
!= NULL
)
3324 o
= options_parse_get(ft
->wp
->options
, key
, &idx
, 0);
3325 if (o
== NULL
&& ft
->w
!= NULL
)
3326 o
= options_parse_get(ft
->w
->options
, key
, &idx
, 0);
3328 o
= options_parse_get(global_w_options
, key
, &idx
, 0);
3329 if (o
== NULL
&& ft
->s
!= NULL
)
3330 o
= options_parse_get(ft
->s
->options
, key
, &idx
, 0);
3332 o
= options_parse_get(global_s_options
, key
, &idx
, 0);
3334 found
= options_to_string(o
, idx
, 1);
3338 fte
= format_table_get(key
);
3340 value
= fte
->cb(ft
);
3341 if (fte
->type
== FORMAT_TABLE_TIME
&& value
!= NULL
)
3342 t
= ((struct timeval
*)value
)->tv_sec
;
3347 fe_find
.key
= (char *)key
;
3348 fe
= RB_FIND(format_entry_tree
, &ft
->tree
, &fe_find
);
3350 if (fe
->time
!= 0) {
3354 if (fe
->value
== NULL
&& fe
->cb
!= NULL
) {
3355 fe
->value
= fe
->cb(ft
);
3356 if (fe
->value
== NULL
)
3357 fe
->value
= xstrdup("");
3359 found
= xstrdup(fe
->value
);
3363 if (~modifiers
& FORMAT_TIMESTRING
) {
3366 envent
= environ_find(ft
->s
->environ
, key
);
3368 envent
= environ_find(global_environ
, key
);
3369 if (envent
!= NULL
&& envent
->value
!= NULL
) {
3370 found
= xstrdup(envent
->value
);
3378 if (modifiers
& FORMAT_TIMESTRING
) {
3379 if (t
== 0 && found
!= NULL
) {
3380 t
= strtonum(found
, 0, INT64_MAX
, &errstr
);
3387 if (modifiers
& FORMAT_PRETTY
)
3388 found
= format_pretty_time(t
);
3390 if (time_format
!= NULL
) {
3391 localtime_r(&t
, &tm
);
3392 strftime(s
, sizeof s
, time_format
, &tm
);
3395 s
[strcspn(s
, "\n")] = '\0';
3403 xasprintf(&found
, "%lld", (long long)t
);
3404 else if (found
== NULL
)
3406 if (modifiers
& FORMAT_BASENAME
) {
3408 found
= xstrdup(basename(saved
));
3411 if (modifiers
& FORMAT_DIRNAME
) {
3413 found
= xstrdup(dirname(saved
));
3416 if (modifiers
& FORMAT_QUOTE_SHELL
) {
3418 found
= xstrdup(format_quote_shell(saved
));
3421 if (modifiers
& FORMAT_QUOTE_STYLE
) {
3423 found
= xstrdup(format_quote_style(saved
));
3429 /* Remove escaped characters from string. */
3431 format_strip(const char *s
)
3436 cp
= out
= xmalloc(strlen(s
) + 1);
3437 for (; *s
!= '\0'; s
++) {
3438 if (*s
== '#' && s
[1] == '{')
3440 if (*s
== '#' && strchr(",#{}:", s
[1]) != NULL
) {
3453 /* Skip until end. */
3455 format_skip(const char *s
, const char *end
)
3459 for (; *s
!= '\0'; s
++) {
3460 if (*s
== '#' && s
[1] == '{')
3462 if (*s
== '#' && strchr(",#{}:", s
[1]) != NULL
) {
3468 if (strchr(end
, *s
) != NULL
&& brackets
== 0)
3476 /* Return left and right alternatives separated by commas. */
3478 format_choose(struct format_expand_state
*es
, const char *s
, char **left
,
3479 char **right
, int expand
)
3482 char *left0
, *right0
;
3484 cp
= format_skip(s
, ",");
3487 left0
= xstrndup(s
, cp
- s
);
3488 right0
= xstrdup(cp
+ 1);
3491 *left
= format_expand1(es
, left0
);
3493 *right
= format_expand1(es
, right0
);
3504 format_true(const char *s
)
3506 if (s
!= NULL
&& *s
!= '\0' && (s
[0] != '0' || s
[1] != '\0'))
3511 /* Check if modifier end. */
3513 format_is_end(char c
)
3515 return (c
== ';' || c
== ':');
3518 /* Add to modifier list. */
3520 format_add_modifier(struct format_modifier
**list
, u_int
*count
,
3521 const char *c
, size_t n
, char **argv
, int argc
)
3523 struct format_modifier
*fm
;
3525 *list
= xreallocarray(*list
, (*count
) + 1, sizeof **list
);
3526 fm
= &(*list
)[(*count
)++];
3528 memcpy(fm
->modifier
, c
, n
);
3529 fm
->modifier
[n
] = '\0';
3536 /* Free modifier list. */
3538 format_free_modifiers(struct format_modifier
*list
, u_int count
)
3542 for (i
= 0; i
< count
; i
++)
3543 cmd_free_argv(list
[i
].argc
, list
[i
].argv
);
3547 /* Build modifier list. */
3548 static struct format_modifier
*
3549 format_build_modifiers(struct format_expand_state
*es
, const char **s
,
3552 const char *cp
= *s
, *end
;
3553 struct format_modifier
*list
= NULL
;
3554 char c
, last
[] = "X;:", **argv
, *value
;
3558 * Modifiers are a ; separated list of the forms:
3559 * l,m,C,a,b,c,d,n,t,w,q,E,T,S,W,P,<,>
3570 while (*cp
!= '\0' && *cp
!= ':') {
3571 /* Skip any separator character. */
3575 /* Check single character modifiers with no arguments. */
3576 if (strchr("labcdnwETSWP<>", cp
[0]) != NULL
&&
3577 format_is_end(cp
[1])) {
3578 format_add_modifier(&list
, count
, cp
, 1, NULL
, 0);
3583 /* Then try double character with no arguments. */
3584 if ((memcmp("||", cp
, 2) == 0 ||
3585 memcmp("&&", cp
, 2) == 0 ||
3586 memcmp("!=", cp
, 2) == 0 ||
3587 memcmp("==", cp
, 2) == 0 ||
3588 memcmp("<=", cp
, 2) == 0 ||
3589 memcmp(">=", cp
, 2) == 0) &&
3590 format_is_end(cp
[2])) {
3591 format_add_modifier(&list
, count
, cp
, 2, NULL
, 0);
3596 /* Now try single character with arguments. */
3597 if (strchr("mCNst=peq", cp
[0]) == NULL
)
3601 /* No arguments provided. */
3602 if (format_is_end(cp
[1])) {
3603 format_add_modifier(&list
, count
, cp
, 1, NULL
, 0);
3610 /* Single argument with no wrapper character. */
3611 if (!ispunct(cp
[1]) || cp
[1] == '-') {
3612 end
= format_skip(cp
+ 1, ":;");
3616 argv
= xcalloc(1, sizeof *argv
);
3617 value
= xstrndup(cp
+ 1, end
- (cp
+ 1));
3618 argv
[0] = format_expand1(es
, value
);
3622 format_add_modifier(&list
, count
, &c
, 1, argv
, argc
);
3627 /* Multiple arguments with a wrapper character. */
3631 if (cp
[0] == last
[0] && format_is_end(cp
[1])) {
3635 end
= format_skip(cp
+ 1, last
);
3640 argv
= xreallocarray(argv
, argc
+ 1, sizeof *argv
);
3641 value
= xstrndup(cp
, end
- cp
);
3642 argv
[argc
++] = format_expand1(es
, value
);
3646 } while (!format_is_end(cp
[0]));
3647 format_add_modifier(&list
, count
, &c
, 1, argv
, argc
);
3650 format_free_modifiers(list
, *count
);
3658 /* Match against an fnmatch(3) pattern or regular expression. */
3660 format_match(struct format_modifier
*fm
, const char *pattern
, const char *text
)
3668 if (strchr(s
, 'r') == NULL
) {
3669 if (strchr(s
, 'i') != NULL
)
3670 flags
|= FNM_CASEFOLD
;
3671 if (fnmatch(pattern
, text
, flags
) != 0)
3672 return (xstrdup("0"));
3674 flags
= REG_EXTENDED
|REG_NOSUB
;
3675 if (strchr(s
, 'i') != NULL
)
3677 if (regcomp(&r
, pattern
, flags
) != 0)
3678 return (xstrdup("0"));
3679 if (regexec(&r
, text
, 0, NULL
, 0) != 0) {
3681 return (xstrdup("0"));
3685 return (xstrdup("1"));
3688 /* Perform substitution in string. */
3690 format_sub(struct format_modifier
*fm
, const char *text
, const char *pattern
,
3694 int flags
= REG_EXTENDED
;
3696 if (fm
->argc
>= 3 && strchr(fm
->argv
[2], 'i') != NULL
)
3698 value
= regsub(pattern
, with
, text
, flags
);
3700 return (xstrdup(text
));
3704 /* Search inside pane. */
3706 format_search(struct format_modifier
*fm
, struct window_pane
*wp
, const char *s
)
3708 int ignore
= 0, regex
= 0;
3711 if (fm
->argc
>= 1) {
3712 if (strchr(fm
->argv
[0], 'i') != NULL
)
3714 if (strchr(fm
->argv
[0], 'r') != NULL
)
3717 xasprintf(&value
, "%u", window_pane_search(wp
, s
, regex
, ignore
));
3721 /* Does session name exist? */
3723 format_session_name(struct format_expand_state
*es
, const char *fmt
)
3728 name
= format_expand1(es
, fmt
);
3729 RB_FOREACH(s
, sessions
, &sessions
) {
3730 if (strcmp(s
->name
, name
) == 0) {
3732 return (xstrdup("1"));
3736 return (xstrdup("0"));
3739 /* Loop over sessions. */
3741 format_loop_sessions(struct format_expand_state
*es
, const char *fmt
)
3743 struct format_tree
*ft
= es
->ft
;
3744 struct client
*c
= ft
->client
;
3745 struct cmdq_item
*item
= ft
->item
;
3746 struct format_tree
*nft
;
3747 struct format_expand_state next
;
3748 char *expanded
, *value
;
3752 value
= xcalloc(1, 1);
3755 RB_FOREACH(s
, sessions
, &sessions
) {
3756 format_log(es
, "session loop: $%u", s
->id
);
3757 nft
= format_create(c
, item
, FORMAT_NONE
, ft
->flags
);
3758 format_defaults(nft
, ft
->c
, s
, NULL
, NULL
);
3759 format_copy_state(&next
, es
, 0);
3761 expanded
= format_expand1(&next
, fmt
);
3762 format_free(next
.ft
);
3764 valuelen
+= strlen(expanded
);
3765 value
= xrealloc(value
, valuelen
);
3767 strlcat(value
, expanded
, valuelen
);
3774 /* Does window name exist? */
3776 format_window_name(struct format_expand_state
*es
, const char *fmt
)
3778 struct format_tree
*ft
= es
->ft
;
3782 if (ft
->s
== NULL
) {
3783 format_log(es
, "window name but no session");
3787 name
= format_expand1(es
, fmt
);
3788 RB_FOREACH(wl
, winlinks
, &ft
->s
->windows
) {
3789 if (strcmp(wl
->window
->name
, name
) == 0) {
3791 return (xstrdup("1"));
3795 return (xstrdup("0"));
3798 /* Loop over windows. */
3800 format_loop_windows(struct format_expand_state
*es
, const char *fmt
)
3802 struct format_tree
*ft
= es
->ft
;
3803 struct client
*c
= ft
->client
;
3804 struct cmdq_item
*item
= ft
->item
;
3805 struct format_tree
*nft
;
3806 struct format_expand_state next
;
3807 char *all
, *active
, *use
, *expanded
, *value
;
3812 if (ft
->s
== NULL
) {
3813 format_log(es
, "window loop but no session");
3817 if (format_choose(es
, fmt
, &all
, &active
, 0) != 0) {
3822 value
= xcalloc(1, 1);
3825 RB_FOREACH(wl
, winlinks
, &ft
->s
->windows
) {
3827 format_log(es
, "window loop: %u @%u", wl
->idx
, w
->id
);
3828 if (active
!= NULL
&& wl
== ft
->s
->curw
)
3832 nft
= format_create(c
, item
, FORMAT_WINDOW
|w
->id
, ft
->flags
);
3833 format_defaults(nft
, ft
->c
, ft
->s
, wl
, NULL
);
3834 format_copy_state(&next
, es
, 0);
3836 expanded
= format_expand1(&next
, use
);
3839 valuelen
+= strlen(expanded
);
3840 value
= xrealloc(value
, valuelen
);
3842 strlcat(value
, expanded
, valuelen
);
3852 /* Loop over panes. */
3854 format_loop_panes(struct format_expand_state
*es
, const char *fmt
)
3856 struct format_tree
*ft
= es
->ft
;
3857 struct client
*c
= ft
->client
;
3858 struct cmdq_item
*item
= ft
->item
;
3859 struct format_tree
*nft
;
3860 struct format_expand_state next
;
3861 char *all
, *active
, *use
, *expanded
, *value
;
3863 struct window_pane
*wp
;
3865 if (ft
->w
== NULL
) {
3866 format_log(es
, "pane loop but no window");
3870 if (format_choose(es
, fmt
, &all
, &active
, 0) != 0) {
3875 value
= xcalloc(1, 1);
3878 TAILQ_FOREACH(wp
, &ft
->w
->panes
, entry
) {
3879 format_log(es
, "pane loop: %%%u", wp
->id
);
3880 if (active
!= NULL
&& wp
== ft
->w
->active
)
3884 nft
= format_create(c
, item
, FORMAT_PANE
|wp
->id
, ft
->flags
);
3885 format_defaults(nft
, ft
->c
, ft
->s
, ft
->wl
, wp
);
3886 format_copy_state(&next
, es
, 0);
3888 expanded
= format_expand1(&next
, use
);
3891 valuelen
+= strlen(expanded
);
3892 value
= xrealloc(value
, valuelen
);
3894 strlcat(value
, expanded
, valuelen
);
3905 format_replace_expression(struct format_modifier
*mexp
,
3906 struct format_expand_state
*es
, const char *copy
)
3908 int argc
= mexp
->argc
;
3910 char *endch
, *value
, *left
= NULL
, *right
= NULL
;
3913 double mleft
, mright
, result
;
3924 LESS_THAN_EQUAL
} operator;
3926 if (strcmp(mexp
->argv
[0], "+") == 0)
3928 else if (strcmp(mexp
->argv
[0], "-") == 0)
3929 operator = SUBTRACT
;
3930 else if (strcmp(mexp
->argv
[0], "*") == 0)
3931 operator = MULTIPLY
;
3932 else if (strcmp(mexp
->argv
[0], "/") == 0)
3934 else if (strcmp(mexp
->argv
[0], "%") == 0 ||
3935 strcmp(mexp
->argv
[0], "m") == 0)
3937 else if (strcmp(mexp
->argv
[0], "==") == 0)
3939 else if (strcmp(mexp
->argv
[0], "!=") == 0)
3940 operator = NOT_EQUAL
;
3941 else if (strcmp(mexp
->argv
[0], ">") == 0)
3942 operator = GREATER_THAN
;
3943 else if (strcmp(mexp
->argv
[0], "<") == 0)
3944 operator = LESS_THAN
;
3945 else if (strcmp(mexp
->argv
[0], ">=") == 0)
3946 operator = GREATER_THAN_EQUAL
;
3947 else if (strcmp(mexp
->argv
[0], "<=") == 0)
3948 operator = LESS_THAN_EQUAL
;
3950 format_log(es
, "expression has no valid operator: '%s'",
3955 /* The second argument may be flags. */
3956 if (argc
>= 2 && strchr(mexp
->argv
[1], 'f') != NULL
) {
3961 /* The third argument may be precision. */
3963 prec
= strtonum(mexp
->argv
[2], INT_MIN
, INT_MAX
, &errstr
);
3964 if (errstr
!= NULL
) {
3965 format_log(es
, "expression precision %s: %s", errstr
,
3971 if (format_choose(es
, copy
, &left
, &right
, 1) != 0) {
3972 format_log(es
, "expression syntax error");
3976 mleft
= strtod(left
, &endch
);
3977 if (*endch
!= '\0') {
3978 format_log(es
, "expression left side is invalid: %s", left
);
3982 mright
= strtod(right
, &endch
);
3983 if (*endch
!= '\0') {
3984 format_log(es
, "expression right side is invalid: %s", right
);
3989 mleft
= (long long)mleft
;
3990 mright
= (long long)mright
;
3992 format_log(es
, "expression left side is: %.*f", prec
, mleft
);
3993 format_log(es
, "expression right side is: %.*f", prec
, mright
);
3997 result
= mleft
+ mright
;
4000 result
= mleft
- mright
;
4003 result
= mleft
* mright
;
4006 result
= mleft
/ mright
;
4009 result
= fmod(mleft
, mright
);
4012 result
= fabs(mleft
- mright
) < 1e-9;
4015 result
= fabs(mleft
- mright
) > 1e-9;
4018 result
= (mleft
> mright
);
4020 case GREATER_THAN_EQUAL
:
4021 result
= (mleft
>= mright
);
4024 result
= (mleft
< mright
);
4026 case LESS_THAN_EQUAL
:
4027 result
= (mleft
<= mright
);
4031 xasprintf(&value
, "%.*f", prec
, result
);
4033 xasprintf(&value
, "%.*f", prec
, (double)(long long)result
);
4034 format_log(es
, "expression result is %s", value
);
4046 /* Replace a key. */
4048 format_replace(struct format_expand_state
*es
, const char *key
, size_t keylen
,
4049 char **buf
, size_t *len
, size_t *off
)
4051 struct format_tree
*ft
= es
->ft
;
4052 struct window_pane
*wp
= ft
->wp
;
4053 const char *errstr
, *copy
, *cp
, *marker
= NULL
;
4054 const char *time_format
= NULL
;
4055 char *copy0
, *condition
, *found
, *new;
4056 char *value
, *left
, *right
;
4058 int modifiers
= 0, limit
= 0, width
= 0;
4060 struct format_modifier
*list
, *cmp
= NULL
, *search
= NULL
;
4061 struct format_modifier
**sub
= NULL
, *mexp
= NULL
, *fm
;
4062 u_int i
, count
, nsub
= 0;
4063 struct format_expand_state next
;
4065 /* Make a copy of the key. */
4066 copy
= copy0
= xstrndup(key
, keylen
);
4068 /* Process modifier list. */
4069 list
= format_build_modifiers(es
, ©
, &count
);
4070 for (i
= 0; i
< count
; i
++) {
4072 if (format_logging(ft
)) {
4073 format_log(es
, "modifier %u is %s", i
, fm
->modifier
);
4074 for (j
= 0; j
< fm
->argc
; j
++) {
4075 format_log(es
, "modifier %u argument %d: %s", i
,
4079 if (fm
->size
== 1) {
4080 switch (fm
->modifier
[0]) {
4092 sub
= xreallocarray(sub
, nsub
+ 1, sizeof *sub
);
4098 limit
= strtonum(fm
->argv
[0], INT_MIN
, INT_MAX
,
4102 if (fm
->argc
>= 2 && fm
->argv
[1] != NULL
)
4103 marker
= fm
->argv
[1];
4108 width
= strtonum(fm
->argv
[0], INT_MIN
, INT_MAX
,
4114 modifiers
|= FORMAT_WIDTH
;
4117 if (fm
->argc
< 1 || fm
->argc
> 3)
4122 modifiers
|= FORMAT_LITERAL
;
4125 modifiers
|= FORMAT_CHARACTER
;
4128 modifiers
|= FORMAT_BASENAME
;
4131 modifiers
|= FORMAT_COLOUR
;
4134 modifiers
|= FORMAT_DIRNAME
;
4137 modifiers
|= FORMAT_LENGTH
;
4140 modifiers
|= FORMAT_TIMESTRING
;
4143 if (strchr(fm
->argv
[0], 'p') != NULL
)
4144 modifiers
|= FORMAT_PRETTY
;
4145 else if (fm
->argc
>= 2 &&
4146 strchr(fm
->argv
[0], 'f') != NULL
)
4147 time_format
= format_strip(fm
->argv
[1]);
4151 modifiers
|= FORMAT_QUOTE_SHELL
;
4152 else if (strchr(fm
->argv
[0], 'e') != NULL
||
4153 strchr(fm
->argv
[0], 'h') != NULL
)
4154 modifiers
|= FORMAT_QUOTE_STYLE
;
4157 modifiers
|= FORMAT_EXPAND
;
4160 modifiers
|= FORMAT_EXPANDTIME
;
4164 strchr(fm
->argv
[0], 'w') != NULL
)
4165 modifiers
|= FORMAT_WINDOW_NAME
;
4166 else if (strchr(fm
->argv
[0], 's') != NULL
)
4167 modifiers
|= FORMAT_SESSION_NAME
;
4170 modifiers
|= FORMAT_SESSIONS
;
4173 modifiers
|= FORMAT_WINDOWS
;
4176 modifiers
|= FORMAT_PANES
;
4179 } else if (fm
->size
== 2) {
4180 if (strcmp(fm
->modifier
, "||") == 0 ||
4181 strcmp(fm
->modifier
, "&&") == 0 ||
4182 strcmp(fm
->modifier
, "==") == 0 ||
4183 strcmp(fm
->modifier
, "!=") == 0 ||
4184 strcmp(fm
->modifier
, ">=") == 0 ||
4185 strcmp(fm
->modifier
, "<=") == 0)
4190 /* Is this a literal string? */
4191 if (modifiers
& FORMAT_LITERAL
) {
4192 value
= xstrdup(copy
);
4196 /* Is this a character? */
4197 if (modifiers
& FORMAT_CHARACTER
) {
4198 new = format_expand1(es
, copy
);
4199 c
= strtonum(new, 32, 126, &errstr
);
4201 value
= xstrdup("");
4203 xasprintf(&value
, "%c", c
);
4208 /* Is this a colour? */
4209 if (modifiers
& FORMAT_COLOUR
) {
4210 new = format_expand1(es
, copy
);
4211 c
= colour_fromstring(new);
4212 if (c
== -1 || (c
= colour_force_rgb(c
)) == -1)
4213 value
= xstrdup("");
4215 xasprintf(&value
, "%06x", c
& 0xffffff);
4220 /* Is this a loop, comparison or condition? */
4221 if (modifiers
& FORMAT_SESSIONS
) {
4222 value
= format_loop_sessions(es
, copy
);
4225 } else if (modifiers
& FORMAT_WINDOWS
) {
4226 value
= format_loop_windows(es
, copy
);
4229 } else if (modifiers
& FORMAT_PANES
) {
4230 value
= format_loop_panes(es
, copy
);
4233 } else if (modifiers
& FORMAT_WINDOW_NAME
) {
4234 value
= format_window_name(es
, copy
);
4237 } else if (modifiers
& FORMAT_SESSION_NAME
) {
4238 value
= format_session_name(es
, copy
);
4241 } else if (search
!= NULL
) {
4242 /* Search in pane. */
4243 new = format_expand1(es
, copy
);
4245 format_log(es
, "search '%s' but no pane", new);
4246 value
= xstrdup("0");
4248 format_log(es
, "search '%s' pane %%%u", new, wp
->id
);
4249 value
= format_search(search
, wp
, new);
4252 } else if (cmp
!= NULL
) {
4253 /* Comparison of left and right. */
4254 if (format_choose(es
, copy
, &left
, &right
, 1) != 0) {
4255 format_log(es
, "compare %s syntax error: %s",
4256 cmp
->modifier
, copy
);
4259 format_log(es
, "compare %s left is: %s", cmp
->modifier
, left
);
4260 format_log(es
, "compare %s right is: %s", cmp
->modifier
, right
);
4262 if (strcmp(cmp
->modifier
, "||") == 0) {
4263 if (format_true(left
) || format_true(right
))
4264 value
= xstrdup("1");
4266 value
= xstrdup("0");
4267 } else if (strcmp(cmp
->modifier
, "&&") == 0) {
4268 if (format_true(left
) && format_true(right
))
4269 value
= xstrdup("1");
4271 value
= xstrdup("0");
4272 } else if (strcmp(cmp
->modifier
, "==") == 0) {
4273 if (strcmp(left
, right
) == 0)
4274 value
= xstrdup("1");
4276 value
= xstrdup("0");
4277 } else if (strcmp(cmp
->modifier
, "!=") == 0) {
4278 if (strcmp(left
, right
) != 0)
4279 value
= xstrdup("1");
4281 value
= xstrdup("0");
4282 } else if (strcmp(cmp
->modifier
, "<") == 0) {
4283 if (strcmp(left
, right
) < 0)
4284 value
= xstrdup("1");
4286 value
= xstrdup("0");
4287 } else if (strcmp(cmp
->modifier
, ">") == 0) {
4288 if (strcmp(left
, right
) > 0)
4289 value
= xstrdup("1");
4291 value
= xstrdup("0");
4292 } else if (strcmp(cmp
->modifier
, "<=") == 0) {
4293 if (strcmp(left
, right
) <= 0)
4294 value
= xstrdup("1");
4296 value
= xstrdup("0");
4297 } else if (strcmp(cmp
->modifier
, ">=") == 0) {
4298 if (strcmp(left
, right
) >= 0)
4299 value
= xstrdup("1");
4301 value
= xstrdup("0");
4302 } else if (strcmp(cmp
->modifier
, "m") == 0)
4303 value
= format_match(cmp
, left
, right
);
4307 } else if (*copy
== '?') {
4308 /* Conditional: check first and choose second or third. */
4309 cp
= format_skip(copy
+ 1, ",");
4311 format_log(es
, "condition syntax error: %s", copy
+ 1);
4314 condition
= xstrndup(copy
+ 1, cp
- (copy
+ 1));
4315 format_log(es
, "condition is: %s", condition
);
4317 found
= format_find(ft
, condition
, modifiers
, time_format
);
4318 if (found
== NULL
) {
4320 * If the condition not found, try to expand it. If
4321 * the expansion doesn't have any effect, then assume
4324 found
= format_expand1(es
, condition
);
4325 if (strcmp(found
, condition
) == 0) {
4327 found
= xstrdup("");
4329 "condition '%s' not found; assuming false",
4333 format_log(es
, "condition '%s' found: %s", condition
,
4337 if (format_choose(es
, cp
+ 1, &left
, &right
, 0) != 0) {
4338 format_log(es
, "condition '%s' syntax error: %s",
4343 if (format_true(found
)) {
4344 format_log(es
, "condition '%s' is true", condition
);
4345 value
= format_expand1(es
, left
);
4347 format_log(es
, "condition '%s' is false", condition
);
4348 value
= format_expand1(es
, right
);
4355 } else if (mexp
!= NULL
) {
4356 value
= format_replace_expression(mexp
, es
, copy
);
4358 value
= xstrdup("");
4360 if (strstr(copy
, "#{") != 0) {
4361 format_log(es
, "expanding inner format '%s'", copy
);
4362 value
= format_expand1(es
, copy
);
4364 value
= format_find(ft
, copy
, modifiers
, time_format
);
4365 if (value
== NULL
) {
4366 format_log(es
, "format '%s' not found", copy
);
4367 value
= xstrdup("");
4369 format_log(es
, "format '%s' found: %s", copy
,
4376 /* Expand again if required. */
4377 if (modifiers
& FORMAT_EXPAND
) {
4378 new = format_expand1(es
, value
);
4381 } else if (modifiers
& FORMAT_EXPANDTIME
) {
4382 format_copy_state(&next
, es
, FORMAT_EXPAND_TIME
);
4383 new = format_expand1(&next
, value
);
4388 /* Perform substitution if any. */
4389 for (i
= 0; i
< nsub
; i
++) {
4390 left
= format_expand1(es
, sub
[i
]->argv
[0]);
4391 right
= format_expand1(es
, sub
[i
]->argv
[1]);
4392 new = format_sub(sub
[i
], value
, left
, right
);
4393 format_log(es
, "substitute '%s' to '%s': %s", left
, right
, new);
4400 /* Truncate the value if needed. */
4402 new = format_trim_left(value
, limit
);
4403 if (marker
!= NULL
&& strcmp(new, value
) != 0) {
4405 xasprintf(&value
, "%s%s", new, marker
);
4410 format_log(es
, "applied length limit %d: %s", limit
, value
);
4411 } else if (limit
< 0) {
4412 new = format_trim_right(value
, -limit
);
4413 if (marker
!= NULL
&& strcmp(new, value
) != 0) {
4415 xasprintf(&value
, "%s%s", marker
, new);
4420 format_log(es
, "applied length limit %d: %s", limit
, value
);
4423 /* Pad the value if needed. */
4425 new = utf8_padcstr(value
, width
);
4428 format_log(es
, "applied padding width %d: %s", width
, value
);
4429 } else if (width
< 0) {
4430 new = utf8_rpadcstr(value
, -width
);
4433 format_log(es
, "applied padding width %d: %s", width
, value
);
4436 /* Replace with the length or width if needed. */
4437 if (modifiers
& FORMAT_LENGTH
) {
4438 xasprintf(&new, "%zu", strlen(value
));
4441 format_log(es
, "replacing with length: %s", new);
4443 if (modifiers
& FORMAT_WIDTH
) {
4444 xasprintf(&new, "%u", format_width(value
));
4447 format_log(es
, "replacing with width: %s", new);
4450 /* Expand the buffer and copy in the value. */
4451 valuelen
= strlen(value
);
4452 while (*len
- *off
< valuelen
+ 1) {
4453 *buf
= xreallocarray(*buf
, 2, *len
);
4456 memcpy(*buf
+ *off
, value
, valuelen
);
4459 format_log(es
, "replaced '%s' with '%s'", copy0
, value
);
4463 format_free_modifiers(list
, count
);
4468 format_log(es
, "failed %s", copy0
);
4471 format_free_modifiers(list
, count
);
4476 /* Expand keys in a template. */
4478 format_expand1(struct format_expand_state
*es
, const char *fmt
)
4480 struct format_tree
*ft
= es
->ft
;
4481 char *buf
, *out
, *name
;
4482 const char *ptr
, *s
;
4483 size_t off
, len
, n
, outlen
;
4485 char expanded
[8192];
4487 if (fmt
== NULL
|| *fmt
== '\0')
4488 return (xstrdup(""));
4490 if (es
->loop
== FORMAT_LOOP_LIMIT
) {
4491 format_log(es
, "reached loop limit (%u)", FORMAT_LOOP_LIMIT
);
4492 return (xstrdup(""));
4496 format_log(es
, "expanding format: %s", fmt
);
4498 if ((es
->flags
& FORMAT_EXPAND_TIME
) && strchr(fmt
, '%') != NULL
) {
4499 if (es
->time
== 0) {
4500 es
->time
= time(NULL
);
4501 localtime_r(&es
->time
, &es
->tm
);
4503 if (strftime(expanded
, sizeof expanded
, fmt
, &es
->tm
) == 0) {
4504 format_log(es
, "format is too long");
4505 return (xstrdup(""));
4507 if (format_logging(ft
) && strcmp(expanded
, fmt
) != 0)
4508 format_log(es
, "after time expanded: %s", expanded
);
4516 while (*fmt
!= '\0') {
4518 while (len
- off
< 2) {
4519 buf
= xreallocarray(buf
, 2, len
);
4522 buf
[off
++] = *fmt
++;
4527 ch
= (u_char
)*fmt
++;
4531 for (ptr
= fmt
; *ptr
!= '\0'; ptr
++) {
4534 if (*ptr
== ')' && --brackets
== 0)
4537 if (*ptr
!= ')' || brackets
!= 0)
4541 name
= xstrndup(fmt
, n
);
4542 format_log(es
, "found #(): %s", name
);
4544 if ((ft
->flags
& FORMAT_NOJOBS
) ||
4545 (es
->flags
& FORMAT_EXPAND_NOJOBS
)) {
4547 format_log(es
, "#() is disabled");
4549 out
= format_job_get(es
, name
);
4550 format_log(es
, "#() result: %s", out
);
4554 outlen
= strlen(out
);
4555 while (len
- off
< outlen
+ 1) {
4556 buf
= xreallocarray(buf
, 2, len
);
4559 memcpy(buf
+ off
, out
, outlen
);
4567 ptr
= format_skip((char *)fmt
- 2, "}");
4572 format_log(es
, "found #{}: %.*s", (int)n
, fmt
);
4573 if (format_replace(es
, fmt
, n
, &buf
, &len
, &off
) != 0)
4579 * If ##[ (with two or more #s), then it is a style and
4580 * can be left for format_draw to handle.
4584 while (*ptr
== '#') {
4589 format_log(es
, "found #*%zu[", n
);
4590 while (len
- off
< n
+ 2) {
4591 buf
= xreallocarray(buf
, 2, len
);
4594 memcpy(buf
+ off
, fmt
- 2, n
+ 1);
4602 format_log(es
, "found #%c", ch
);
4603 while (len
- off
< 2) {
4604 buf
= xreallocarray(buf
, 2, len
);
4611 if (ch
>= 'A' && ch
<= 'Z')
4612 s
= format_upper
[ch
- 'A'];
4613 else if (ch
>= 'a' && ch
<= 'z')
4614 s
= format_lower
[ch
- 'a'];
4616 while (len
- off
< 3) {
4617 buf
= xreallocarray(buf
, 2, len
);
4625 format_log(es
, "found #%c: %s", ch
, s
);
4626 if (format_replace(es
, s
, n
, &buf
, &len
, &off
) != 0)
4635 format_log(es
, "result is: %s", buf
);
4641 /* Expand keys in a template, passing through strftime first. */
4643 format_expand_time(struct format_tree
*ft
, const char *fmt
)
4645 struct format_expand_state es
;
4647 memset(&es
, 0, sizeof es
);
4649 es
.flags
= FORMAT_EXPAND_TIME
;
4650 return (format_expand1(&es
, fmt
));
4653 /* Expand keys in a template. */
4655 format_expand(struct format_tree
*ft
, const char *fmt
)
4657 struct format_expand_state es
;
4659 memset(&es
, 0, sizeof es
);
4662 return (format_expand1(&es
, fmt
));
4665 /* Expand a single string. */
4667 format_single(struct cmdq_item
*item
, const char *fmt
, struct client
*c
,
4668 struct session
*s
, struct winlink
*wl
, struct window_pane
*wp
)
4670 struct format_tree
*ft
;
4673 ft
= format_create_defaults(item
, c
, s
, wl
, wp
);
4674 expanded
= format_expand(ft
, fmt
);
4679 /* Expand a single string using state. */
4681 format_single_from_state(struct cmdq_item
*item
, const char *fmt
,
4682 struct client
*c
, struct cmd_find_state
*fs
)
4684 return (format_single(item
, fmt
, c
, fs
->s
, fs
->wl
, fs
->wp
));
4687 /* Expand a single string using target. */
4689 format_single_from_target(struct cmdq_item
*item
, const char *fmt
)
4691 struct client
*tc
= cmdq_get_target_client(item
);
4693 return (format_single_from_state(item
, fmt
, tc
, cmdq_get_target(item
)));
4696 /* Create and add defaults. */
4697 struct format_tree
*
4698 format_create_defaults(struct cmdq_item
*item
, struct client
*c
,
4699 struct session
*s
, struct winlink
*wl
, struct window_pane
*wp
)
4701 struct format_tree
*ft
;
4704 ft
= format_create(cmdq_get_client(item
), item
, FORMAT_NONE
, 0);
4706 ft
= format_create(NULL
, item
, FORMAT_NONE
, 0);
4707 format_defaults(ft
, c
, s
, wl
, wp
);
4711 /* Create and add defaults using state. */
4712 struct format_tree
*
4713 format_create_from_state(struct cmdq_item
*item
, struct client
*c
,
4714 struct cmd_find_state
*fs
)
4716 return (format_create_defaults(item
, c
, fs
->s
, fs
->wl
, fs
->wp
));
4719 /* Create and add defaults using target. */
4720 struct format_tree
*
4721 format_create_from_target(struct cmdq_item
*item
)
4723 struct client
*tc
= cmdq_get_target_client(item
);
4725 return (format_create_from_state(item
, tc
, cmdq_get_target(item
)));
4728 /* Set defaults for any of arguments that are not NULL. */
4730 format_defaults(struct format_tree
*ft
, struct client
*c
, struct session
*s
,
4731 struct winlink
*wl
, struct window_pane
*wp
)
4733 struct paste_buffer
*pb
;
4735 if (c
!= NULL
&& c
->name
!= NULL
)
4736 log_debug("%s: c=%s", __func__
, c
->name
);
4738 log_debug("%s: c=none", __func__
);
4740 log_debug("%s: s=$%u", __func__
, s
->id
);
4742 log_debug("%s: s=none", __func__
);
4744 log_debug("%s: wl=%u", __func__
, wl
->idx
);
4746 log_debug("%s: wl=none", __func__
);
4748 log_debug("%s: wp=%%%u", __func__
, wp
->id
);
4750 log_debug("%s: wp=none", __func__
);
4752 if (c
!= NULL
&& s
!= NULL
&& c
->session
!= s
)
4753 log_debug("%s: session does not match", __func__
);
4756 ft
->type
= FORMAT_TYPE_PANE
;
4757 else if (wl
!= NULL
)
4758 ft
->type
= FORMAT_TYPE_WINDOW
;
4760 ft
->type
= FORMAT_TYPE_SESSION
;
4762 ft
->type
= FORMAT_TYPE_UNKNOWN
;
4764 if (s
== NULL
&& c
!= NULL
)
4766 if (wl
== NULL
&& s
!= NULL
)
4768 if (wp
== NULL
&& wl
!= NULL
)
4769 wp
= wl
->window
->active
;
4772 format_defaults_client(ft
, c
);
4774 format_defaults_session(ft
, s
);
4776 format_defaults_winlink(ft
, wl
);
4778 format_defaults_pane(ft
, wp
);
4780 pb
= paste_get_top(NULL
);
4782 format_defaults_paste_buffer(ft
, pb
);
4785 /* Set default format keys for a session. */
4787 format_defaults_session(struct format_tree
*ft
, struct session
*s
)
4792 /* Set default format keys for a client. */
4794 format_defaults_client(struct format_tree
*ft
, struct client
*c
)
4801 /* Set default format keys for a window. */
4803 format_defaults_window(struct format_tree
*ft
, struct window
*w
)
4808 /* Set default format keys for a winlink. */
4810 format_defaults_winlink(struct format_tree
*ft
, struct winlink
*wl
)
4813 format_defaults_window(ft
, wl
->window
);
4817 /* Set default format keys for a window pane. */
4819 format_defaults_pane(struct format_tree
*ft
, struct window_pane
*wp
)
4821 struct window_mode_entry
*wme
;
4824 format_defaults_window(ft
, wp
->window
);
4827 wme
= TAILQ_FIRST(&wp
->modes
);
4828 if (wme
!= NULL
&& wme
->mode
->formats
!= NULL
)
4829 wme
->mode
->formats(wme
, ft
);
4832 /* Set default format keys for paste buffer. */
4834 format_defaults_paste_buffer(struct format_tree
*ft
, struct paste_buffer
*pb
)
4839 /* Return word at given coordinates. Caller frees. */
4841 format_grid_word(struct grid
*gd
, u_int x
, u_int y
)
4843 const struct grid_line
*gl
;
4844 struct grid_cell gc
;
4846 struct utf8_data
*ud
= NULL
;
4852 ws
= options_get_string(global_s_options
, "word-separators");
4855 grid_get_cell(gd
, x
, y
, &gc
);
4856 if (gc
.flags
& GRID_FLAG_PADDING
)
4858 if (utf8_cstrhas(ws
, &gc
.data
) ||
4859 (gc
.data
.size
== 1 && *gc
.data
.data
== ' ')) {
4867 gl
= grid_peek_line(gd
, y
- 1);
4868 if (~gl
->flags
& GRID_LINE_WRAPPED
)
4871 x
= grid_line_length(gd
, y
);
4879 end
= grid_line_length(gd
, y
);
4880 if (end
== 0 || x
== end
- 1) {
4881 if (y
== gd
->hsize
+ gd
->sy
- 1)
4883 gl
= grid_peek_line(gd
, y
);
4884 if (~gl
->flags
& GRID_LINE_WRAPPED
)
4893 grid_get_cell(gd
, x
, y
, &gc
);
4894 if (gc
.flags
& GRID_FLAG_PADDING
)
4896 if (utf8_cstrhas(ws
, &gc
.data
) ||
4897 (gc
.data
.size
== 1 && *gc
.data
.data
== ' '))
4900 ud
= xreallocarray(ud
, size
+ 2, sizeof *ud
);
4901 memcpy(&ud
[size
++], &gc
.data
, sizeof *ud
);
4905 s
= utf8_tocstr(ud
);
4911 /* Return line at given coordinates. Caller frees. */
4913 format_grid_line(struct grid
*gd
, u_int y
)
4915 struct grid_cell gc
;
4916 struct utf8_data
*ud
= NULL
;
4921 for (x
= 0; x
< grid_line_length(gd
, y
); x
++) {
4922 grid_get_cell(gd
, x
, y
, &gc
);
4923 if (gc
.flags
& GRID_FLAG_PADDING
)
4926 ud
= xreallocarray(ud
, size
+ 2, sizeof *ud
);
4927 memcpy(&ud
[size
++], &gc
.data
, sizeof *ud
);
4931 s
= utf8_tocstr(ud
);