xref: /openbsd/usr.bin/tmux/format.c (revision 7a5fb1a8)
1 /* $OpenBSD: format.c,v 1.323 2024/10/28 08:16:06 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2011 Nicholas Marriott <nicholas.marriott@gmail.com>
5  *
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.
9  *
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.
17  */
18 
19 #include <sys/types.h>
20 #include <sys/wait.h>
21 
22 #include <ctype.h>
23 #include <errno.h>
24 #include <fnmatch.h>
25 #include <libgen.h>
26 #include <math.h>
27 #include <pwd.h>
28 #include <regex.h>
29 #include <stdarg.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <time.h>
33 #include <unistd.h>
34 
35 #include "tmux.h"
36 
37 /*
38  * Build a list of key-value pairs and use them to expand #{key} entries in a
39  * string.
40  */
41 
42 struct format_expand_state;
43 
44 static char	*format_job_get(struct format_expand_state *, const char *);
45 static char	*format_expand1(struct format_expand_state *, const char *);
46 static int	 format_replace(struct format_expand_state *, const char *,
47 		     size_t, char **, size_t *, size_t *);
48 static void	 format_defaults_session(struct format_tree *,
49 		     struct session *);
50 static void	 format_defaults_client(struct format_tree *, struct client *);
51 static void	 format_defaults_winlink(struct format_tree *,
52 		     struct winlink *);
53 
54 /* Entry in format job tree. */
55 struct format_job {
56 	struct client		*client;
57 	u_int			 tag;
58 	const char		*cmd;
59 	const char		*expanded;
60 
61 	time_t			 last;
62 	char			*out;
63 	int			 updated;
64 
65 	struct job		*job;
66 	int			 status;
67 
68 	RB_ENTRY(format_job)	 entry;
69 };
70 
71 /* Format job tree. */
72 static int format_job_cmp(struct format_job *, struct format_job *);
73 static RB_HEAD(format_job_tree, format_job) format_jobs = RB_INITIALIZER();
74 RB_GENERATE_STATIC(format_job_tree, format_job, entry, format_job_cmp);
75 
76 /* Format job tree comparison function. */
77 static int
format_job_cmp(struct format_job * fj1,struct format_job * fj2)78 format_job_cmp(struct format_job *fj1, struct format_job *fj2)
79 {
80 	if (fj1->tag < fj2->tag)
81 		return (-1);
82 	if (fj1->tag > fj2->tag)
83 		return (1);
84 	return (strcmp(fj1->cmd, fj2->cmd));
85 }
86 
87 /* Format modifiers. */
88 #define FORMAT_TIMESTRING 0x1
89 #define FORMAT_BASENAME 0x2
90 #define FORMAT_DIRNAME 0x4
91 #define FORMAT_QUOTE_SHELL 0x8
92 #define FORMAT_LITERAL 0x10
93 #define FORMAT_EXPAND 0x20
94 #define FORMAT_EXPANDTIME 0x40
95 #define FORMAT_SESSIONS 0x80
96 #define FORMAT_WINDOWS 0x100
97 #define FORMAT_PANES 0x200
98 #define FORMAT_PRETTY 0x400
99 #define FORMAT_LENGTH 0x800
100 #define FORMAT_WIDTH 0x1000
101 #define FORMAT_QUOTE_STYLE 0x2000
102 #define FORMAT_WINDOW_NAME 0x4000
103 #define FORMAT_SESSION_NAME 0x8000
104 #define FORMAT_CHARACTER 0x10000
105 #define FORMAT_COLOUR 0x20000
106 #define FORMAT_CLIENTS 0x40000
107 
108 /* Limit on recursion. */
109 #define FORMAT_LOOP_LIMIT 100
110 
111 /* Format expand flags. */
112 #define FORMAT_EXPAND_TIME 0x1
113 #define FORMAT_EXPAND_NOJOBS 0x2
114 
115 /* Entry in format tree. */
116 struct format_entry {
117 	char			*key;
118 	char			*value;
119 	time_t			 time;
120 	format_cb		 cb;
121 	RB_ENTRY(format_entry)	 entry;
122 };
123 
124 /* Format type. */
125 enum format_type {
126 	FORMAT_TYPE_UNKNOWN,
127 	FORMAT_TYPE_SESSION,
128 	FORMAT_TYPE_WINDOW,
129 	FORMAT_TYPE_PANE
130 };
131 
132 struct format_tree {
133 	enum format_type	 type;
134 
135 	struct client		*c;
136 	struct session		*s;
137 	struct winlink		*wl;
138 	struct window		*w;
139 	struct window_pane	*wp;
140 	struct paste_buffer	*pb;
141 
142 	struct cmdq_item	*item;
143 	struct client		*client;
144 	int			 flags;
145 	u_int			 tag;
146 
147 	struct mouse_event	 m;
148 
149 	RB_HEAD(format_entry_tree, format_entry) tree;
150 };
151 static int format_entry_cmp(struct format_entry *, struct format_entry *);
152 RB_GENERATE_STATIC(format_entry_tree, format_entry, entry, format_entry_cmp);
153 
154 /* Format expand state. */
155 struct format_expand_state {
156 	struct format_tree	*ft;
157 	u_int			 loop;
158 	time_t			 time;
159 	struct tm		 tm;
160 	int			 flags;
161 };
162 
163 /* Format modifier. */
164 struct format_modifier {
165 	char	  modifier[3];
166 	u_int	  size;
167 
168 	char	**argv;
169 	int	  argc;
170 };
171 
172 /* Format entry tree comparison function. */
173 static int
format_entry_cmp(struct format_entry * fe1,struct format_entry * fe2)174 format_entry_cmp(struct format_entry *fe1, struct format_entry *fe2)
175 {
176 	return (strcmp(fe1->key, fe2->key));
177 }
178 
179 /* Single-character uppercase aliases. */
180 static const char *format_upper[] = {
181 	NULL,		/* A */
182 	NULL,		/* B */
183 	NULL,		/* C */
184 	"pane_id",	/* D */
185 	NULL,		/* E */
186 	"window_flags",	/* F */
187 	NULL,		/* G */
188 	"host",		/* H */
189 	"window_index",	/* I */
190 	NULL,		/* J */
191 	NULL,		/* K */
192 	NULL,		/* L */
193 	NULL,		/* M */
194 	NULL,		/* N */
195 	NULL,		/* O */
196 	"pane_index",	/* P */
197 	NULL,		/* Q */
198 	NULL,		/* R */
199 	"session_name",	/* S */
200 	"pane_title",	/* T */
201 	NULL,		/* U */
202 	NULL,		/* V */
203 	"window_name",	/* W */
204 	NULL,		/* X */
205 	NULL,		/* Y */
206 	NULL 		/* Z */
207 };
208 
209 /* Single-character lowercase aliases. */
210 static const char *format_lower[] = {
211 	NULL,		/* a */
212 	NULL,		/* b */
213 	NULL,		/* c */
214 	NULL,		/* d */
215 	NULL,		/* e */
216 	NULL,		/* f */
217 	NULL,		/* g */
218 	"host_short",	/* h */
219 	NULL,		/* i */
220 	NULL,		/* j */
221 	NULL,		/* k */
222 	NULL,		/* l */
223 	NULL,		/* m */
224 	NULL,		/* n */
225 	NULL,		/* o */
226 	NULL,		/* p */
227 	NULL,		/* q */
228 	NULL,		/* r */
229 	NULL,		/* s */
230 	NULL,		/* t */
231 	NULL,		/* u */
232 	NULL,		/* v */
233 	NULL,		/* w */
234 	NULL,		/* x */
235 	NULL,		/* y */
236 	NULL		/* z */
237 };
238 
239 /* Is logging enabled? */
240 static inline int
format_logging(struct format_tree * ft)241 format_logging(struct format_tree *ft)
242 {
243 	return (log_get_level() != 0 || (ft->flags & FORMAT_VERBOSE));
244 }
245 
246 /* Log a message if verbose. */
247 static void printflike(3, 4)
format_log1(struct format_expand_state * es,const char * from,const char * fmt,...)248 format_log1(struct format_expand_state *es, const char *from, const char *fmt,
249     ...)
250 {
251 	struct format_tree	*ft = es->ft;
252 	va_list			 ap;
253 	char			*s;
254 	static const char	 spaces[] = "          ";
255 
256 	if (!format_logging(ft))
257 		return;
258 
259 	va_start(ap, fmt);
260 	xvasprintf(&s, fmt, ap);
261 	va_end(ap);
262 
263 	log_debug("%s: %s", from, s);
264 	if (ft->item != NULL && (ft->flags & FORMAT_VERBOSE))
265 		cmdq_print(ft->item, "#%.*s%s", es->loop, spaces, s);
266 
267 	free(s);
268 }
269 #define format_log(es, fmt, ...) format_log1(es, __func__, fmt, ##__VA_ARGS__)
270 
271 /* Copy expand state. */
272 static void
format_copy_state(struct format_expand_state * to,struct format_expand_state * from,int flags)273 format_copy_state(struct format_expand_state *to,
274     struct format_expand_state *from, int flags)
275 {
276 	to->ft = from->ft;
277 	to->loop = from->loop;
278 	to->time = from->time;
279 	memcpy(&to->tm, &from->tm, sizeof to->tm);
280 	to->flags = from->flags|flags;
281 }
282 
283 /* Format job update callback. */
284 static void
format_job_update(struct job * job)285 format_job_update(struct job *job)
286 {
287 	struct format_job	*fj = job_get_data(job);
288 	struct evbuffer		*evb = job_get_event(job)->input;
289 	char			*line = NULL, *next;
290 	time_t			 t;
291 
292 	while ((next = evbuffer_readline(evb)) != NULL) {
293 		free(line);
294 		line = next;
295 	}
296 	if (line == NULL)
297 		return;
298 	fj->updated = 1;
299 
300 	free(fj->out);
301 	fj->out = line;
302 
303 	log_debug("%s: %p %s: %s", __func__, fj, fj->cmd, fj->out);
304 
305 	t = time(NULL);
306 	if (fj->status && fj->last != t) {
307 		if (fj->client != NULL)
308 			server_status_client(fj->client);
309 		fj->last = t;
310 	}
311 }
312 
313 /* Format job complete callback. */
314 static void
format_job_complete(struct job * job)315 format_job_complete(struct job *job)
316 {
317 	struct format_job	*fj = job_get_data(job);
318 	struct evbuffer		*evb = job_get_event(job)->input;
319 	char			*line, *buf;
320 	size_t			 len;
321 
322 	fj->job = NULL;
323 
324 	buf = NULL;
325 	if ((line = evbuffer_readline(evb)) == NULL) {
326 		len = EVBUFFER_LENGTH(evb);
327 		buf = xmalloc(len + 1);
328 		if (len != 0)
329 			memcpy(buf, EVBUFFER_DATA(evb), len);
330 		buf[len] = '\0';
331 	} else
332 		buf = line;
333 
334 	log_debug("%s: %p %s: %s", __func__, fj, fj->cmd, buf);
335 
336 	if (*buf != '\0' || !fj->updated) {
337 		free(fj->out);
338 		fj->out = buf;
339 	} else
340 		free(buf);
341 
342 	if (fj->status) {
343 		if (fj->client != NULL)
344 			server_status_client(fj->client);
345 		fj->status = 0;
346 	}
347 }
348 
349 /* Find a job. */
350 static char *
format_job_get(struct format_expand_state * es,const char * cmd)351 format_job_get(struct format_expand_state *es, const char *cmd)
352 {
353 	struct format_tree		*ft = es->ft;
354 	struct format_job_tree		*jobs;
355 	struct format_job		 fj0, *fj;
356 	time_t				 t;
357 	char				*expanded;
358 	int				 force;
359 	struct format_expand_state	 next;
360 
361 	if (ft->client == NULL)
362 		jobs = &format_jobs;
363 	else if (ft->client->jobs != NULL)
364 		jobs = ft->client->jobs;
365 	else {
366 		jobs = ft->client->jobs = xmalloc(sizeof *ft->client->jobs);
367 		RB_INIT(jobs);
368 	}
369 
370 	fj0.tag = ft->tag;
371 	fj0.cmd = cmd;
372 	if ((fj = RB_FIND(format_job_tree, jobs, &fj0)) == NULL) {
373 		fj = xcalloc(1, sizeof *fj);
374 		fj->client = ft->client;
375 		fj->tag = ft->tag;
376 		fj->cmd = xstrdup(cmd);
377 
378 		RB_INSERT(format_job_tree, jobs, fj);
379 	}
380 
381 	format_copy_state(&next, es, FORMAT_EXPAND_NOJOBS);
382 	next.flags &= ~FORMAT_EXPAND_TIME;
383 
384 	expanded = format_expand1(&next, cmd);
385 	if (fj->expanded == NULL || strcmp(expanded, fj->expanded) != 0) {
386 		free((void *)fj->expanded);
387 		fj->expanded = xstrdup(expanded);
388 		force = 1;
389 	} else
390 		force = (ft->flags & FORMAT_FORCE);
391 
392 	t = time(NULL);
393 	if (force && fj->job != NULL)
394 	       job_free(fj->job);
395 	if (force || (fj->job == NULL && fj->last != t)) {
396 		fj->job = job_run(expanded, 0, NULL, NULL, NULL,
397 		    server_client_get_cwd(ft->client, NULL), format_job_update,
398 		    format_job_complete, NULL, fj, JOB_NOWAIT, -1, -1);
399 		if (fj->job == NULL) {
400 			free(fj->out);
401 			xasprintf(&fj->out, "<'%s' didn't start>", fj->cmd);
402 		}
403 		fj->last = t;
404 		fj->updated = 0;
405 	} else if (fj->job != NULL && (t - fj->last) > 1 && fj->out == NULL)
406 		xasprintf(&fj->out, "<'%s' not ready>", fj->cmd);
407 	free(expanded);
408 
409 	if (ft->flags & FORMAT_STATUS)
410 		fj->status = 1;
411 	if (fj->out == NULL)
412 		return (xstrdup(""));
413 	return (format_expand1(&next, fj->out));
414 }
415 
416 /* Remove old jobs. */
417 static void
format_job_tidy(struct format_job_tree * jobs,int force)418 format_job_tidy(struct format_job_tree *jobs, int force)
419 {
420 	struct format_job	*fj, *fj1;
421 	time_t			 now;
422 
423 	now = time(NULL);
424 	RB_FOREACH_SAFE(fj, format_job_tree, jobs, fj1) {
425 		if (!force && (fj->last > now || now - fj->last < 3600))
426 			continue;
427 		RB_REMOVE(format_job_tree, jobs, fj);
428 
429 		log_debug("%s: %s", __func__, fj->cmd);
430 
431 		if (fj->job != NULL)
432 			job_free(fj->job);
433 
434 		free((void *)fj->expanded);
435 		free((void *)fj->cmd);
436 		free(fj->out);
437 
438 		free(fj);
439 	}
440 }
441 
442 /* Tidy old jobs for all clients. */
443 void
format_tidy_jobs(void)444 format_tidy_jobs(void)
445 {
446 	struct client	*c;
447 
448 	format_job_tidy(&format_jobs, 0);
449 	TAILQ_FOREACH(c, &clients, entry) {
450 		if (c->jobs != NULL)
451 			format_job_tidy(c->jobs, 0);
452 	}
453 }
454 
455 /* Remove old jobs for client. */
456 void
format_lost_client(struct client * c)457 format_lost_client(struct client *c)
458 {
459 	if (c->jobs != NULL)
460 		format_job_tidy(c->jobs, 1);
461 	free(c->jobs);
462 }
463 
464 /* Wrapper for asprintf. */
465 static char * printflike(1, 2)
format_printf(const char * fmt,...)466 format_printf(const char *fmt, ...)
467 {
468 	va_list	 ap;
469 	char	*s;
470 
471 	va_start(ap, fmt);
472 	xvasprintf(&s, fmt, ap);
473 	va_end(ap);
474 	return (s);
475 }
476 
477 /* Callback for host. */
478 static void *
format_cb_host(__unused struct format_tree * ft)479 format_cb_host(__unused struct format_tree *ft)
480 {
481 	char host[HOST_NAME_MAX + 1];
482 
483 	if (gethostname(host, sizeof host) != 0)
484 		return (xstrdup(""));
485 	return (xstrdup(host));
486 }
487 
488 /* Callback for host_short. */
489 static void *
format_cb_host_short(__unused struct format_tree * ft)490 format_cb_host_short(__unused struct format_tree *ft)
491 {
492 	char host[HOST_NAME_MAX + 1], *cp;
493 
494 	if (gethostname(host, sizeof host) != 0)
495 		return (xstrdup(""));
496 	if ((cp = strchr(host, '.')) != NULL)
497 		*cp = '\0';
498 	return (xstrdup(host));
499 }
500 
501 /* Callback for pid. */
502 static void *
format_cb_pid(__unused struct format_tree * ft)503 format_cb_pid(__unused struct format_tree *ft)
504 {
505 	char	*value;
506 
507 	xasprintf(&value, "%ld", (long)getpid());
508 	return (value);
509 }
510 
511 /* Callback for session_attached_list. */
512 static void *
format_cb_session_attached_list(struct format_tree * ft)513 format_cb_session_attached_list(struct format_tree *ft)
514 {
515 	struct session	*s = ft->s;
516 	struct client	*loop;
517 	struct evbuffer	*buffer;
518 	int		 size;
519 	char		*value = NULL;
520 
521 	if (s == NULL)
522 		return (NULL);
523 
524 	buffer = evbuffer_new();
525 	if (buffer == NULL)
526 		fatalx("out of memory");
527 
528 	TAILQ_FOREACH(loop, &clients, entry) {
529 		if (loop->session == s) {
530 			if (EVBUFFER_LENGTH(buffer) > 0)
531 				evbuffer_add(buffer, ",", 1);
532 			evbuffer_add_printf(buffer, "%s", loop->name);
533 		}
534 	}
535 
536 	if ((size = EVBUFFER_LENGTH(buffer)) != 0)
537 		xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
538 	evbuffer_free(buffer);
539 	return (value);
540 }
541 
542 /* Callback for session_alerts. */
543 static void *
format_cb_session_alerts(struct format_tree * ft)544 format_cb_session_alerts(struct format_tree *ft)
545 {
546 	struct session	*s = ft->s;
547 	struct winlink	*wl;
548 	char		 alerts[1024], tmp[16];
549 
550 	if (s == NULL)
551 		return (NULL);
552 
553 	*alerts = '\0';
554 	RB_FOREACH(wl, winlinks, &s->windows) {
555 		if ((wl->flags & WINLINK_ALERTFLAGS) == 0)
556 			continue;
557 		xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
558 
559 		if (*alerts != '\0')
560 			strlcat(alerts, ",", sizeof alerts);
561 		strlcat(alerts, tmp, sizeof alerts);
562 		if (wl->flags & WINLINK_ACTIVITY)
563 			strlcat(alerts, "#", sizeof alerts);
564 		if (wl->flags & WINLINK_BELL)
565 			strlcat(alerts, "!", sizeof alerts);
566 		if (wl->flags & WINLINK_SILENCE)
567 			strlcat(alerts, "~", sizeof alerts);
568 	}
569 	return (xstrdup(alerts));
570 }
571 
572 /* Callback for session_stack. */
573 static void *
format_cb_session_stack(struct format_tree * ft)574 format_cb_session_stack(struct format_tree *ft)
575 {
576 	struct session	*s = ft->s;
577 	struct winlink	*wl;
578 	char		 result[1024], tmp[16];
579 
580 	if (s == NULL)
581 		return (NULL);
582 
583 	xsnprintf(result, sizeof result, "%u", s->curw->idx);
584 	TAILQ_FOREACH(wl, &s->lastw, sentry) {
585 		xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
586 
587 		if (*result != '\0')
588 			strlcat(result, ",", sizeof result);
589 		strlcat(result, tmp, sizeof result);
590 	}
591 	return (xstrdup(result));
592 }
593 
594 /* Callback for window_stack_index. */
595 static void *
format_cb_window_stack_index(struct format_tree * ft)596 format_cb_window_stack_index(struct format_tree *ft)
597 {
598 	struct session	*s;
599 	struct winlink	*wl;
600 	u_int		 idx;
601 	char		*value = NULL;
602 
603 	if (ft->wl == NULL)
604 		return (NULL);
605 	s = ft->wl->session;
606 
607 	idx = 0;
608 	TAILQ_FOREACH(wl, &s->lastw, sentry) {
609 		idx++;
610 		if (wl == ft->wl)
611 			break;
612 	}
613 	if (wl == NULL)
614 		return (xstrdup("0"));
615 	xasprintf(&value, "%u", idx);
616 	return (value);
617 }
618 
619 /* Callback for window_linked_sessions_list. */
620 static void *
format_cb_window_linked_sessions_list(struct format_tree * ft)621 format_cb_window_linked_sessions_list(struct format_tree *ft)
622 {
623 	struct window	*w;
624 	struct winlink	*wl;
625 	struct evbuffer	*buffer;
626 	int		 size;
627 	char		*value = NULL;
628 
629 	if (ft->wl == NULL)
630 		return (NULL);
631 	w = ft->wl->window;
632 
633 	buffer = evbuffer_new();
634 	if (buffer == NULL)
635 		fatalx("out of memory");
636 
637 	TAILQ_FOREACH(wl, &w->winlinks, wentry) {
638 		if (EVBUFFER_LENGTH(buffer) > 0)
639 			evbuffer_add(buffer, ",", 1);
640 		evbuffer_add_printf(buffer, "%s", wl->session->name);
641 	}
642 
643 	if ((size = EVBUFFER_LENGTH(buffer)) != 0)
644 		xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
645 	evbuffer_free(buffer);
646 	return (value);
647 }
648 
649 /* Callback for window_active_sessions. */
650 static void *
format_cb_window_active_sessions(struct format_tree * ft)651 format_cb_window_active_sessions(struct format_tree *ft)
652 {
653 	struct window	*w;
654 	struct winlink	*wl;
655 	u_int		 n = 0;
656 	char		*value;
657 
658 	if (ft->wl == NULL)
659 		return (NULL);
660 	w = ft->wl->window;
661 
662 	TAILQ_FOREACH(wl, &w->winlinks, wentry) {
663 		if (wl->session->curw == wl)
664 			n++;
665 	}
666 
667 	xasprintf(&value, "%u", n);
668 	return (value);
669 }
670 
671 /* Callback for window_active_sessions_list. */
672 static void *
format_cb_window_active_sessions_list(struct format_tree * ft)673 format_cb_window_active_sessions_list(struct format_tree *ft)
674 {
675 	struct window	*w;
676 	struct winlink	*wl;
677 	struct evbuffer	*buffer;
678 	int		 size;
679 	char		*value = NULL;
680 
681 	if (ft->wl == NULL)
682 		return (NULL);
683 	w = ft->wl->window;
684 
685 	buffer = evbuffer_new();
686 	if (buffer == NULL)
687 		fatalx("out of memory");
688 
689 	TAILQ_FOREACH(wl, &w->winlinks, wentry) {
690 		if (wl->session->curw == wl) {
691 			if (EVBUFFER_LENGTH(buffer) > 0)
692 				evbuffer_add(buffer, ",", 1);
693 			evbuffer_add_printf(buffer, "%s", wl->session->name);
694 		}
695 	}
696 
697 	if ((size = EVBUFFER_LENGTH(buffer)) != 0)
698 		xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
699 	evbuffer_free(buffer);
700 	return (value);
701 }
702 
703 /* Callback for window_active_clients. */
704 static void *
format_cb_window_active_clients(struct format_tree * ft)705 format_cb_window_active_clients(struct format_tree *ft)
706 {
707 	struct window	*w;
708 	struct client	*loop;
709 	struct session	*client_session;
710 	u_int		 n = 0;
711 	char		*value;
712 
713 	if (ft->wl == NULL)
714 		return (NULL);
715 	w = ft->wl->window;
716 
717 	TAILQ_FOREACH(loop, &clients, entry) {
718 		client_session = loop->session;
719 		if (client_session == NULL)
720 			continue;
721 
722 		if (w == client_session->curw->window)
723 			n++;
724 	}
725 
726 	xasprintf(&value, "%u", n);
727 	return (value);
728 }
729 
730 /* Callback for window_active_clients_list. */
731 static void *
format_cb_window_active_clients_list(struct format_tree * ft)732 format_cb_window_active_clients_list(struct format_tree *ft)
733 {
734 	struct window	*w;
735 	struct client	*loop;
736 	struct session	*client_session;
737 	struct evbuffer	*buffer;
738 	int		 size;
739 	char		*value = NULL;
740 
741 	if (ft->wl == NULL)
742 		return (NULL);
743 	w = ft->wl->window;
744 
745 	buffer = evbuffer_new();
746 	if (buffer == NULL)
747 		fatalx("out of memory");
748 
749 	TAILQ_FOREACH(loop, &clients, entry) {
750 		client_session = loop->session;
751 		if (client_session == NULL)
752 			continue;
753 
754 		if (w == client_session->curw->window) {
755 			if (EVBUFFER_LENGTH(buffer) > 0)
756 				evbuffer_add(buffer, ",", 1);
757 			evbuffer_add_printf(buffer, "%s", loop->name);
758 		}
759 	}
760 
761 	if ((size = EVBUFFER_LENGTH(buffer)) != 0)
762 		xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
763 	evbuffer_free(buffer);
764 	return (value);
765 }
766 
767 /* Callback for window_layout. */
768 static void *
format_cb_window_layout(struct format_tree * ft)769 format_cb_window_layout(struct format_tree *ft)
770 {
771 	struct window	*w = ft->w;
772 
773 	if (w == NULL)
774 		return (NULL);
775 
776 	if (w->saved_layout_root != NULL)
777 		return (layout_dump(w->saved_layout_root));
778 	return (layout_dump(w->layout_root));
779 }
780 
781 /* Callback for window_visible_layout. */
782 static void *
format_cb_window_visible_layout(struct format_tree * ft)783 format_cb_window_visible_layout(struct format_tree *ft)
784 {
785 	struct window	*w = ft->w;
786 
787 	if (w == NULL)
788 		return (NULL);
789 
790 	return (layout_dump(w->layout_root));
791 }
792 
793 /* Callback for pane_start_command. */
794 static void *
format_cb_start_command(struct format_tree * ft)795 format_cb_start_command(struct format_tree *ft)
796 {
797 	struct window_pane	*wp = ft->wp;
798 
799 	if (wp == NULL)
800 		return (NULL);
801 
802 	return (cmd_stringify_argv(wp->argc, wp->argv));
803 }
804 
805 /* Callback for pane_start_path. */
806 static void *
format_cb_start_path(struct format_tree * ft)807 format_cb_start_path(struct format_tree *ft)
808 {
809 	struct window_pane	*wp = ft->wp;
810 
811 	if (wp == NULL)
812 		return (NULL);
813 
814 	if (wp->cwd == NULL)
815 		return (xstrdup(""));
816 	return (xstrdup(wp->cwd));
817 }
818 
819 /* Callback for pane_current_command. */
820 static void *
format_cb_current_command(struct format_tree * ft)821 format_cb_current_command(struct format_tree *ft)
822 {
823 	struct window_pane	*wp = ft->wp;
824 	char			*cmd, *value;
825 
826 	if (wp == NULL || wp->shell == NULL)
827 		return (NULL);
828 
829 	cmd = get_proc_name(wp->fd, wp->tty);
830 	if (cmd == NULL || *cmd == '\0') {
831 		free(cmd);
832 		cmd = cmd_stringify_argv(wp->argc, wp->argv);
833 		if (cmd == NULL || *cmd == '\0') {
834 			free(cmd);
835 			cmd = xstrdup(wp->shell);
836 		}
837 	}
838 	value = parse_window_name(cmd);
839 	free(cmd);
840 	return (value);
841 }
842 
843 /* Callback for pane_current_path. */
844 static void *
format_cb_current_path(struct format_tree * ft)845 format_cb_current_path(struct format_tree *ft)
846 {
847 	struct window_pane	*wp = ft->wp;
848 	char			*cwd;
849 
850 	if (wp == NULL)
851 		return (NULL);
852 
853 	cwd = get_proc_cwd(wp->fd);
854 	if (cwd == NULL)
855 		return (NULL);
856 	return (xstrdup(cwd));
857 }
858 
859 /* Callback for history_bytes. */
860 static void *
format_cb_history_bytes(struct format_tree * ft)861 format_cb_history_bytes(struct format_tree *ft)
862 {
863 	struct window_pane	*wp = ft->wp;
864 	struct grid		*gd;
865 	struct grid_line	*gl;
866 	size_t		         size = 0;
867 	u_int			 i;
868 	char			*value;
869 
870 	if (wp == NULL)
871 		return (NULL);
872 	gd = wp->base.grid;
873 
874 	for (i = 0; i < gd->hsize + gd->sy; i++) {
875 		gl = grid_get_line(gd, i);
876 		size += gl->cellsize * sizeof *gl->celldata;
877 		size += gl->extdsize * sizeof *gl->extddata;
878 	}
879 	size += (gd->hsize + gd->sy) * sizeof *gl;
880 
881 	xasprintf(&value, "%zu", size);
882 	return (value);
883 }
884 
885 /* Callback for history_all_bytes. */
886 static void *
format_cb_history_all_bytes(struct format_tree * ft)887 format_cb_history_all_bytes(struct format_tree *ft)
888 {
889 	struct window_pane	*wp = ft->wp;
890 	struct grid		*gd;
891 	struct grid_line	*gl;
892 	u_int			 i, lines, cells = 0, extended_cells = 0;
893 	char			*value;
894 
895 	if (wp == NULL)
896 		return (NULL);
897 	gd = wp->base.grid;
898 
899 	lines = gd->hsize + gd->sy;
900 	for (i = 0; i < lines; i++) {
901 		gl = grid_get_line(gd, i);
902 		cells += gl->cellsize;
903 		extended_cells += gl->extdsize;
904 	}
905 
906 	xasprintf(&value, "%u,%zu,%u,%zu,%u,%zu", lines,
907 	    lines * sizeof *gl, cells, cells * sizeof *gl->celldata,
908 	    extended_cells, extended_cells * sizeof *gl->extddata);
909 	return (value);
910 }
911 
912 /* Callback for pane_tabs. */
913 static void *
format_cb_pane_tabs(struct format_tree * ft)914 format_cb_pane_tabs(struct format_tree *ft)
915 {
916 	struct window_pane	*wp = ft->wp;
917 	struct evbuffer		*buffer;
918 	u_int			 i;
919 	int			 size;
920 	char			*value = NULL;
921 
922 	if (wp == NULL)
923 		return (NULL);
924 
925 	buffer = evbuffer_new();
926 	if (buffer == NULL)
927 		fatalx("out of memory");
928 	for (i = 0; i < wp->base.grid->sx; i++) {
929 		if (!bit_test(wp->base.tabs, i))
930 			continue;
931 
932 		if (EVBUFFER_LENGTH(buffer) > 0)
933 			evbuffer_add(buffer, ",", 1);
934 		evbuffer_add_printf(buffer, "%u", i);
935 	}
936 	if ((size = EVBUFFER_LENGTH(buffer)) != 0)
937 		xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
938 	evbuffer_free(buffer);
939 	return (value);
940 }
941 
942 /* Callback for pane_fg. */
943 static void *
format_cb_pane_fg(struct format_tree * ft)944 format_cb_pane_fg(struct format_tree *ft)
945 {
946 	struct window_pane	*wp = ft->wp;
947 	struct grid_cell	 gc;
948 
949 	if (wp == NULL)
950 		return (NULL);
951 
952 	tty_default_colours(&gc, wp);
953 	return (xstrdup(colour_tostring(gc.fg)));
954 }
955 
956 /* Callback for pane_bg. */
957 static void *
format_cb_pane_bg(struct format_tree * ft)958 format_cb_pane_bg(struct format_tree *ft)
959 {
960 	struct window_pane	*wp = ft->wp;
961 	struct grid_cell	 gc;
962 
963 	if (wp == NULL)
964 		return (NULL);
965 
966 	tty_default_colours(&gc, wp);
967 	return (xstrdup(colour_tostring(gc.bg)));
968 }
969 
970 /* Callback for session_group_list. */
971 static void *
format_cb_session_group_list(struct format_tree * ft)972 format_cb_session_group_list(struct format_tree *ft)
973 {
974 	struct session		*s = ft->s;
975 	struct session_group	*sg;
976 	struct session		*loop;
977 	struct evbuffer		*buffer;
978 	int			 size;
979 	char			*value = NULL;
980 
981 	if (s == NULL)
982 		return (NULL);
983 	sg = session_group_contains(s);
984 	if (sg == NULL)
985 		return (NULL);
986 
987 	buffer = evbuffer_new();
988 	if (buffer == NULL)
989 		fatalx("out of memory");
990 
991 	TAILQ_FOREACH(loop, &sg->sessions, gentry) {
992 		if (EVBUFFER_LENGTH(buffer) > 0)
993 			evbuffer_add(buffer, ",", 1);
994 		evbuffer_add_printf(buffer, "%s", loop->name);
995 	}
996 
997 	if ((size = EVBUFFER_LENGTH(buffer)) != 0)
998 		xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
999 	evbuffer_free(buffer);
1000 	return (value);
1001 }
1002 
1003 /* Callback for session_group_attached_list. */
1004 static void *
format_cb_session_group_attached_list(struct format_tree * ft)1005 format_cb_session_group_attached_list(struct format_tree *ft)
1006 {
1007 	struct session		*s = ft->s, *client_session, *session_loop;
1008 	struct session_group	*sg;
1009 	struct client		*loop;
1010 	struct evbuffer		*buffer;
1011 	int			 size;
1012 	char			*value = NULL;
1013 
1014 	if (s == NULL)
1015 		return (NULL);
1016 	sg = session_group_contains(s);
1017 	if (sg == NULL)
1018 		return (NULL);
1019 
1020 	buffer = evbuffer_new();
1021 	if (buffer == NULL)
1022 		fatalx("out of memory");
1023 
1024 	TAILQ_FOREACH(loop, &clients, entry) {
1025 		client_session = loop->session;
1026 		if (client_session == NULL)
1027 			continue;
1028 		TAILQ_FOREACH(session_loop, &sg->sessions, gentry) {
1029 			if (session_loop == client_session){
1030 				if (EVBUFFER_LENGTH(buffer) > 0)
1031 					evbuffer_add(buffer, ",", 1);
1032 				evbuffer_add_printf(buffer, "%s", loop->name);
1033 			}
1034 		}
1035 	}
1036 
1037 	if ((size = EVBUFFER_LENGTH(buffer)) != 0)
1038 		xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
1039 	evbuffer_free(buffer);
1040 	return (value);
1041 }
1042 
1043 /* Callback for pane_in_mode. */
1044 static void *
format_cb_pane_in_mode(struct format_tree * ft)1045 format_cb_pane_in_mode(struct format_tree *ft)
1046 {
1047 	struct window_pane		*wp = ft->wp;
1048 	u_int				 n = 0;
1049 	struct window_mode_entry	*wme;
1050 	char				*value;
1051 
1052 	if (wp == NULL)
1053 		return (NULL);
1054 
1055 	TAILQ_FOREACH(wme, &wp->modes, entry)
1056 		n++;
1057 	xasprintf(&value, "%u", n);
1058 	return (value);
1059 }
1060 
1061 /* Callback for pane_at_top. */
1062 static void *
format_cb_pane_at_top(struct format_tree * ft)1063 format_cb_pane_at_top(struct format_tree *ft)
1064 {
1065 	struct window_pane	*wp = ft->wp;
1066 	struct window		*w;
1067 	int			 status, flag;
1068 	char			*value;
1069 
1070 	if (wp == NULL)
1071 		return (NULL);
1072 	w = wp->window;
1073 
1074 	status = options_get_number(w->options, "pane-border-status");
1075 	if (status == PANE_STATUS_TOP)
1076 		flag = (wp->yoff == 1);
1077 	else
1078 		flag = (wp->yoff == 0);
1079 	xasprintf(&value, "%d", flag);
1080 	return (value);
1081 }
1082 
1083 /* Callback for pane_at_bottom. */
1084 static void *
format_cb_pane_at_bottom(struct format_tree * ft)1085 format_cb_pane_at_bottom(struct format_tree *ft)
1086 {
1087 	struct window_pane	*wp = ft->wp;
1088 	struct window		*w;
1089 	int			 status, flag;
1090 	char			*value;
1091 
1092 	if (wp == NULL)
1093 		return (NULL);
1094 	w = wp->window;
1095 
1096 	status = options_get_number(w->options, "pane-border-status");
1097 	if (status == PANE_STATUS_BOTTOM)
1098 		flag = (wp->yoff + wp->sy == w->sy - 1);
1099 	else
1100 		flag = (wp->yoff + wp->sy == w->sy);
1101 	xasprintf(&value, "%d", flag);
1102 	return (value);
1103 }
1104 
1105 /* Callback for cursor_character. */
1106 static void *
format_cb_cursor_character(struct format_tree * ft)1107 format_cb_cursor_character(struct format_tree *ft)
1108 {
1109 	struct window_pane	*wp = ft->wp;
1110 	struct grid_cell	 gc;
1111 	char			*value = NULL;
1112 
1113 	if (wp == NULL)
1114 		return (NULL);
1115 
1116 	grid_view_get_cell(wp->base.grid, wp->base.cx, wp->base.cy, &gc);
1117 	if (~gc.flags & GRID_FLAG_PADDING)
1118 		xasprintf(&value, "%.*s", (int)gc.data.size, gc.data.data);
1119 	return (value);
1120 }
1121 
1122 /* Callback for mouse_word. */
1123 static void *
format_cb_mouse_word(struct format_tree * ft)1124 format_cb_mouse_word(struct format_tree *ft)
1125 {
1126 	struct window_pane	*wp;
1127 	struct grid		*gd;
1128 	u_int			 x, y;
1129 
1130 	if (!ft->m.valid)
1131 		return (NULL);
1132 	wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1133 	if (wp == NULL)
1134 		return (NULL);
1135 	if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0)
1136 		return (NULL);
1137 
1138 	if (!TAILQ_EMPTY(&wp->modes)) {
1139 		if (window_pane_mode(wp) != WINDOW_PANE_NO_MODE)
1140 			return (window_copy_get_word(wp, x, y));
1141 		return (NULL);
1142 	}
1143 	gd = wp->base.grid;
1144 	return (format_grid_word(gd, x, gd->hsize + y));
1145 }
1146 
1147 /* Callback for mouse_hyperlink. */
1148 static void *
format_cb_mouse_hyperlink(struct format_tree * ft)1149 format_cb_mouse_hyperlink(struct format_tree *ft)
1150 {
1151 	struct window_pane	*wp;
1152 	struct grid		*gd;
1153 	u_int			 x, y;
1154 
1155 	if (!ft->m.valid)
1156 		return (NULL);
1157 	wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1158 	if (wp == NULL)
1159 		return (NULL);
1160 	if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0)
1161 		return (NULL);
1162 	gd = wp->base.grid;
1163 	return (format_grid_hyperlink(gd, x, gd->hsize + y, wp->screen));
1164 }
1165 
1166 /* Callback for mouse_line. */
1167 static void *
format_cb_mouse_line(struct format_tree * ft)1168 format_cb_mouse_line(struct format_tree *ft)
1169 {
1170 	struct window_pane	*wp;
1171 	struct grid		*gd;
1172 	u_int			 x, y;
1173 
1174 	if (!ft->m.valid)
1175 		return (NULL);
1176 	wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1177 	if (wp == NULL)
1178 		return (NULL);
1179 	if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0)
1180 		return (NULL);
1181 
1182 	if (!TAILQ_EMPTY(&wp->modes)) {
1183 		if (window_pane_mode(wp) != WINDOW_PANE_NO_MODE)
1184 			return (window_copy_get_line(wp, y));
1185 		return (NULL);
1186 	}
1187 	gd = wp->base.grid;
1188 	return (format_grid_line(gd, gd->hsize + y));
1189 }
1190 
1191 /* Callback for mouse_status_line. */
1192 static void *
format_cb_mouse_status_line(struct format_tree * ft)1193 format_cb_mouse_status_line(struct format_tree *ft)
1194 {
1195 	char	*value;
1196 	u_int	 y;
1197 
1198 	if (!ft->m.valid)
1199 		return (NULL);
1200 	if (ft->c == NULL || (~ft->c->tty.flags & TTY_STARTED))
1201 		return (NULL);
1202 
1203 	if (ft->m.statusat == 0 && ft->m.y < ft->m.statuslines) {
1204 		y = ft->m.y;
1205 	} else if (ft->m.statusat > 0 && ft->m.y >= (u_int)ft->m.statusat) {
1206 		y = ft->m.y - ft->m.statusat;
1207 	} else
1208 		return (NULL);
1209 	xasprintf(&value, "%u", y);
1210 	return (value);
1211 
1212 }
1213 
1214 /* Callback for mouse_status_range. */
1215 static void *
format_cb_mouse_status_range(struct format_tree * ft)1216 format_cb_mouse_status_range(struct format_tree *ft)
1217 {
1218 	struct style_range	*sr;
1219 	u_int			 x, y;
1220 
1221 	if (!ft->m.valid)
1222 		return (NULL);
1223 	if (ft->c == NULL || (~ft->c->tty.flags & TTY_STARTED))
1224 		return (NULL);
1225 
1226 	if (ft->m.statusat == 0 && ft->m.y < ft->m.statuslines) {
1227 		x = ft->m.x;
1228 		y = ft->m.y;
1229 	} else if (ft->m.statusat > 0 && ft->m.y >= (u_int)ft->m.statusat) {
1230 		x = ft->m.x;
1231 		y = ft->m.y - ft->m.statusat;
1232 	} else
1233 		return (NULL);
1234 
1235 	sr = status_get_range(ft->c, x, y);
1236 	if (sr == NULL)
1237 		return (NULL);
1238 	switch (sr->type) {
1239 	case STYLE_RANGE_NONE:
1240 		return (NULL);
1241 	case STYLE_RANGE_LEFT:
1242 		return (xstrdup("left"));
1243 	case STYLE_RANGE_RIGHT:
1244 		return (xstrdup("right"));
1245 	case STYLE_RANGE_PANE:
1246 		return (xstrdup("pane"));
1247 	case STYLE_RANGE_WINDOW:
1248 		return (xstrdup("window"));
1249 	case STYLE_RANGE_SESSION:
1250 		return (xstrdup("session"));
1251 	case STYLE_RANGE_USER:
1252 		return (xstrdup(sr->string));
1253 	}
1254 	return (NULL);
1255 }
1256 
1257 /* Callback for alternate_on. */
1258 static void *
format_cb_alternate_on(struct format_tree * ft)1259 format_cb_alternate_on(struct format_tree *ft)
1260 {
1261 	if (ft->wp != NULL) {
1262 		if (ft->wp->base.saved_grid != NULL)
1263 			return (xstrdup("1"));
1264 		return (xstrdup("0"));
1265 	}
1266 	return (NULL);
1267 }
1268 
1269 /* Callback for alternate_saved_x. */
1270 static void *
format_cb_alternate_saved_x(struct format_tree * ft)1271 format_cb_alternate_saved_x(struct format_tree *ft)
1272 {
1273 	if (ft->wp != NULL)
1274 		return (format_printf("%u", ft->wp->base.saved_cx));
1275 	return (NULL);
1276 }
1277 
1278 /* Callback for alternate_saved_y. */
1279 static void *
format_cb_alternate_saved_y(struct format_tree * ft)1280 format_cb_alternate_saved_y(struct format_tree *ft)
1281 {
1282 	if (ft->wp != NULL)
1283 		return (format_printf("%u", ft->wp->base.saved_cy));
1284 	return (NULL);
1285 }
1286 
1287 /* Callback for buffer_name. */
1288 static void *
format_cb_buffer_name(struct format_tree * ft)1289 format_cb_buffer_name(struct format_tree *ft)
1290 {
1291 	if (ft->pb != NULL)
1292 		return (xstrdup(paste_buffer_name(ft->pb)));
1293 	return (NULL);
1294 }
1295 
1296 /* Callback for buffer_sample. */
1297 static void *
format_cb_buffer_sample(struct format_tree * ft)1298 format_cb_buffer_sample(struct format_tree *ft)
1299 {
1300 	if (ft->pb != NULL)
1301 		return (paste_make_sample(ft->pb));
1302 	return (NULL);
1303 }
1304 
1305 /* Callback for buffer_size. */
1306 static void *
format_cb_buffer_size(struct format_tree * ft)1307 format_cb_buffer_size(struct format_tree *ft)
1308 {
1309 	size_t	size;
1310 
1311 	if (ft->pb != NULL) {
1312 		paste_buffer_data(ft->pb, &size);
1313 		return (format_printf("%zu", size));
1314 	}
1315 	return (NULL);
1316 }
1317 
1318 /* Callback for client_cell_height. */
1319 static void *
format_cb_client_cell_height(struct format_tree * ft)1320 format_cb_client_cell_height(struct format_tree *ft)
1321 {
1322 	if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED))
1323 		return (format_printf("%u", ft->c->tty.ypixel));
1324 	return (NULL);
1325 }
1326 
1327 /* Callback for client_cell_width. */
1328 static void *
format_cb_client_cell_width(struct format_tree * ft)1329 format_cb_client_cell_width(struct format_tree *ft)
1330 {
1331 	if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED))
1332 		return (format_printf("%u", ft->c->tty.xpixel));
1333 	return (NULL);
1334 }
1335 
1336 /* Callback for client_control_mode. */
1337 static void *
format_cb_client_control_mode(struct format_tree * ft)1338 format_cb_client_control_mode(struct format_tree *ft)
1339 {
1340 	if (ft->c != NULL) {
1341 		if (ft->c->flags & CLIENT_CONTROL)
1342 			return (xstrdup("1"));
1343 		return (xstrdup("0"));
1344 	}
1345 	return (NULL);
1346 }
1347 
1348 /* Callback for client_discarded. */
1349 static void *
format_cb_client_discarded(struct format_tree * ft)1350 format_cb_client_discarded(struct format_tree *ft)
1351 {
1352 	if (ft->c != NULL)
1353 		return (format_printf("%zu", ft->c->discarded));
1354 	return (NULL);
1355 }
1356 
1357 /* Callback for client_flags. */
1358 static void *
format_cb_client_flags(struct format_tree * ft)1359 format_cb_client_flags(struct format_tree *ft)
1360 {
1361 	if (ft->c != NULL)
1362 		return (xstrdup(server_client_get_flags(ft->c)));
1363 	return (NULL);
1364 }
1365 
1366 /* Callback for client_height. */
1367 static void *
format_cb_client_height(struct format_tree * ft)1368 format_cb_client_height(struct format_tree *ft)
1369 {
1370 	if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED))
1371 		return (format_printf("%u", ft->c->tty.sy));
1372 	return (NULL);
1373 }
1374 
1375 /* Callback for client_key_table. */
1376 static void *
format_cb_client_key_table(struct format_tree * ft)1377 format_cb_client_key_table(struct format_tree *ft)
1378 {
1379 	if (ft->c != NULL)
1380 		return (xstrdup(ft->c->keytable->name));
1381 	return (NULL);
1382 }
1383 
1384 /* Callback for client_last_session. */
1385 static void *
format_cb_client_last_session(struct format_tree * ft)1386 format_cb_client_last_session(struct format_tree *ft)
1387 {
1388 	if (ft->c != NULL &&
1389 	    ft->c->last_session != NULL &&
1390 	    session_alive(ft->c->last_session))
1391 		return (xstrdup(ft->c->last_session->name));
1392 	return (NULL);
1393 }
1394 
1395 /* Callback for client_name. */
1396 static void *
format_cb_client_name(struct format_tree * ft)1397 format_cb_client_name(struct format_tree *ft)
1398 {
1399 	if (ft->c != NULL)
1400 		return (xstrdup(ft->c->name));
1401 	return (NULL);
1402 }
1403 
1404 /* Callback for client_pid. */
1405 static void *
format_cb_client_pid(struct format_tree * ft)1406 format_cb_client_pid(struct format_tree *ft)
1407 {
1408 	if (ft->c != NULL)
1409 		return (format_printf("%ld", (long)ft->c->pid));
1410 	return (NULL);
1411 }
1412 
1413 /* Callback for client_prefix. */
1414 static void *
format_cb_client_prefix(struct format_tree * ft)1415 format_cb_client_prefix(struct format_tree *ft)
1416 {
1417 	const char	*name;
1418 
1419 	if (ft->c != NULL) {
1420 		name = server_client_get_key_table(ft->c);
1421 		if (strcmp(ft->c->keytable->name, name) == 0)
1422 			return (xstrdup("0"));
1423 		return (xstrdup("1"));
1424 	}
1425 	return (NULL);
1426 }
1427 
1428 /* Callback for client_readonly. */
1429 static void *
format_cb_client_readonly(struct format_tree * ft)1430 format_cb_client_readonly(struct format_tree *ft)
1431 {
1432 	if (ft->c != NULL) {
1433 		if (ft->c->flags & CLIENT_READONLY)
1434 			return (xstrdup("1"));
1435 		return (xstrdup("0"));
1436 	}
1437 	return (NULL);
1438 }
1439 
1440 /* Callback for client_session. */
1441 static void *
format_cb_client_session(struct format_tree * ft)1442 format_cb_client_session(struct format_tree *ft)
1443 {
1444 	if (ft->c != NULL && ft->c->session != NULL)
1445 		return (xstrdup(ft->c->session->name));
1446 	return (NULL);
1447 }
1448 
1449 /* Callback for client_termfeatures. */
1450 static void *
format_cb_client_termfeatures(struct format_tree * ft)1451 format_cb_client_termfeatures(struct format_tree *ft)
1452 {
1453 	if (ft->c != NULL)
1454 		return (xstrdup(tty_get_features(ft->c->term_features)));
1455 	return (NULL);
1456 }
1457 
1458 /* Callback for client_termname. */
1459 static void *
format_cb_client_termname(struct format_tree * ft)1460 format_cb_client_termname(struct format_tree *ft)
1461 {
1462 	if (ft->c != NULL)
1463 		return (xstrdup(ft->c->term_name));
1464 	return (NULL);
1465 }
1466 
1467 /* Callback for client_termtype. */
1468 static void *
format_cb_client_termtype(struct format_tree * ft)1469 format_cb_client_termtype(struct format_tree *ft)
1470 {
1471 	if (ft->c != NULL) {
1472 		if (ft->c->term_type == NULL)
1473 			return (xstrdup(""));
1474 		return (xstrdup(ft->c->term_type));
1475 	}
1476 	return (NULL);
1477 }
1478 
1479 /* Callback for client_tty. */
1480 static void *
format_cb_client_tty(struct format_tree * ft)1481 format_cb_client_tty(struct format_tree *ft)
1482 {
1483 	if (ft->c != NULL)
1484 		return (xstrdup(ft->c->ttyname));
1485 	return (NULL);
1486 }
1487 
1488 /* Callback for client_uid. */
1489 static void *
format_cb_client_uid(struct format_tree * ft)1490 format_cb_client_uid(struct format_tree *ft)
1491 {
1492 	uid_t	uid;
1493 
1494 	if (ft->c != NULL) {
1495 		uid = proc_get_peer_uid(ft->c->peer);
1496 		if (uid != (uid_t)-1)
1497 			return (format_printf("%ld", (long)uid));
1498 	}
1499 	return (NULL);
1500 }
1501 
1502 /* Callback for client_user. */
1503 static void *
format_cb_client_user(struct format_tree * ft)1504 format_cb_client_user(struct format_tree *ft)
1505 {
1506 	uid_t		 uid;
1507 	struct passwd	*pw;
1508 
1509 	if (ft->c != NULL) {
1510 		uid = proc_get_peer_uid(ft->c->peer);
1511 		if (uid != (uid_t)-1 && (pw = getpwuid(uid)) != NULL)
1512 			return (xstrdup(pw->pw_name));
1513 	}
1514 	return (NULL);
1515 }
1516 
1517 /* Callback for client_utf8. */
1518 static void *
format_cb_client_utf8(struct format_tree * ft)1519 format_cb_client_utf8(struct format_tree *ft)
1520 {
1521 	if (ft->c != NULL) {
1522 		if (ft->c->flags & CLIENT_UTF8)
1523 			return (xstrdup("1"));
1524 		return (xstrdup("0"));
1525 	}
1526 	return (NULL);
1527 }
1528 
1529 /* Callback for client_width. */
1530 static void *
format_cb_client_width(struct format_tree * ft)1531 format_cb_client_width(struct format_tree *ft)
1532 {
1533 	if (ft->c != NULL)
1534 		return (format_printf("%u", ft->c->tty.sx));
1535 	return (NULL);
1536 }
1537 
1538 /* Callback for client_written. */
1539 static void *
format_cb_client_written(struct format_tree * ft)1540 format_cb_client_written(struct format_tree *ft)
1541 {
1542 	if (ft->c != NULL)
1543 		return (format_printf("%zu", ft->c->written));
1544 	return (NULL);
1545 }
1546 
1547 /* Callback for config_files. */
1548 static void *
format_cb_config_files(__unused struct format_tree * ft)1549 format_cb_config_files(__unused struct format_tree *ft)
1550 {
1551 	char	*s = NULL;
1552 	size_t	 slen = 0;
1553 	u_int	 i;
1554 	size_t	 n;
1555 
1556 	for (i = 0; i < cfg_nfiles; i++) {
1557 		n = strlen(cfg_files[i]) + 1;
1558 		s = xrealloc(s, slen + n + 1);
1559 		slen += xsnprintf(s + slen, n + 1, "%s,", cfg_files[i]);
1560 	}
1561 	if (s == NULL)
1562 		return (xstrdup(""));
1563 	s[slen - 1] = '\0';
1564 	return (s);
1565 }
1566 
1567 /* Callback for cursor_flag. */
1568 static void *
format_cb_cursor_flag(struct format_tree * ft)1569 format_cb_cursor_flag(struct format_tree *ft)
1570 {
1571 	if (ft->wp != NULL) {
1572 		if (ft->wp->base.mode & MODE_CURSOR)
1573 			return (xstrdup("1"));
1574 		return (xstrdup("0"));
1575 	}
1576 	return (NULL);
1577 }
1578 
1579 /* Callback for cursor_x. */
1580 static void *
format_cb_cursor_x(struct format_tree * ft)1581 format_cb_cursor_x(struct format_tree *ft)
1582 {
1583 	if (ft->wp != NULL)
1584 		return (format_printf("%u", ft->wp->base.cx));
1585 	return (NULL);
1586 }
1587 
1588 /* Callback for cursor_y. */
1589 static void *
format_cb_cursor_y(struct format_tree * ft)1590 format_cb_cursor_y(struct format_tree *ft)
1591 {
1592 	if (ft->wp != NULL)
1593 		return (format_printf("%u", ft->wp->base.cy));
1594 	return (NULL);
1595 }
1596 
1597 /* Callback for history_limit. */
1598 static void *
format_cb_history_limit(struct format_tree * ft)1599 format_cb_history_limit(struct format_tree *ft)
1600 {
1601 	if (ft->wp != NULL)
1602 		return (format_printf("%u", ft->wp->base.grid->hlimit));
1603 	return (NULL);
1604 }
1605 
1606 /* Callback for history_size. */
1607 static void *
format_cb_history_size(struct format_tree * ft)1608 format_cb_history_size(struct format_tree *ft)
1609 {
1610 	if (ft->wp != NULL)
1611 		return (format_printf("%u", ft->wp->base.grid->hsize));
1612 	return (NULL);
1613 }
1614 
1615 /* Callback for insert_flag. */
1616 static void *
format_cb_insert_flag(struct format_tree * ft)1617 format_cb_insert_flag(struct format_tree *ft)
1618 {
1619 	if (ft->wp != NULL) {
1620 		if (ft->wp->base.mode & MODE_INSERT)
1621 			return (xstrdup("1"));
1622 		return (xstrdup("0"));
1623 	}
1624 	return (NULL);
1625 }
1626 
1627 /* Callback for keypad_cursor_flag. */
1628 static void *
format_cb_keypad_cursor_flag(struct format_tree * ft)1629 format_cb_keypad_cursor_flag(struct format_tree *ft)
1630 {
1631 	if (ft->wp != NULL) {
1632 		if (ft->wp->base.mode & MODE_KCURSOR)
1633 			return (xstrdup("1"));
1634 		return (xstrdup("0"));
1635 	}
1636 	return (NULL);
1637 }
1638 
1639 /* Callback for keypad_flag. */
1640 static void *
format_cb_keypad_flag(struct format_tree * ft)1641 format_cb_keypad_flag(struct format_tree *ft)
1642 {
1643 	if (ft->wp != NULL) {
1644 		if (ft->wp->base.mode & MODE_KKEYPAD)
1645 			return (xstrdup("1"));
1646 		return (xstrdup("0"));
1647 	}
1648 	return (NULL);
1649 }
1650 
1651 /* Callback for mouse_all_flag. */
1652 static void *
format_cb_mouse_all_flag(struct format_tree * ft)1653 format_cb_mouse_all_flag(struct format_tree *ft)
1654 {
1655 	if (ft->wp != NULL) {
1656 		if (ft->wp->base.mode & MODE_MOUSE_ALL)
1657 			return (xstrdup("1"));
1658 		return (xstrdup("0"));
1659 	}
1660 	return (NULL);
1661 }
1662 
1663 /* Callback for mouse_any_flag. */
1664 static void *
format_cb_mouse_any_flag(struct format_tree * ft)1665 format_cb_mouse_any_flag(struct format_tree *ft)
1666 {
1667 	if (ft->wp != NULL) {
1668 		if (ft->wp->base.mode & ALL_MOUSE_MODES)
1669 			return (xstrdup("1"));
1670 		return (xstrdup("0"));
1671 	}
1672 	return (NULL);
1673 }
1674 
1675 /* Callback for mouse_button_flag. */
1676 static void *
format_cb_mouse_button_flag(struct format_tree * ft)1677 format_cb_mouse_button_flag(struct format_tree *ft)
1678 {
1679 	if (ft->wp != NULL) {
1680 		if (ft->wp->base.mode & MODE_MOUSE_BUTTON)
1681 			return (xstrdup("1"));
1682 		return (xstrdup("0"));
1683 	}
1684 	return (NULL);
1685 }
1686 
1687 /* Callback for mouse_pane. */
1688 static void *
format_cb_mouse_pane(struct format_tree * ft)1689 format_cb_mouse_pane(struct format_tree *ft)
1690 {
1691 	struct window_pane	*wp;
1692 
1693 	if (ft->m.valid) {
1694 		wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1695 		if (wp != NULL)
1696 			return (format_printf("%%%u", wp->id));
1697 		return (NULL);
1698 	}
1699 	return (NULL);
1700 }
1701 
1702 /* Callback for mouse_sgr_flag. */
1703 static void *
format_cb_mouse_sgr_flag(struct format_tree * ft)1704 format_cb_mouse_sgr_flag(struct format_tree *ft)
1705 {
1706 	if (ft->wp != NULL) {
1707 		if (ft->wp->base.mode & MODE_MOUSE_SGR)
1708 			return (xstrdup("1"));
1709 		return (xstrdup("0"));
1710 	}
1711 	return (NULL);
1712 }
1713 
1714 /* Callback for mouse_standard_flag. */
1715 static void *
format_cb_mouse_standard_flag(struct format_tree * ft)1716 format_cb_mouse_standard_flag(struct format_tree *ft)
1717 {
1718 	if (ft->wp != NULL) {
1719 		if (ft->wp->base.mode & MODE_MOUSE_STANDARD)
1720 			return (xstrdup("1"));
1721 		return (xstrdup("0"));
1722 	}
1723 	return (NULL);
1724 }
1725 
1726 /* Callback for mouse_utf8_flag. */
1727 static void *
format_cb_mouse_utf8_flag(struct format_tree * ft)1728 format_cb_mouse_utf8_flag(struct format_tree *ft)
1729 {
1730 	if (ft->wp != NULL) {
1731 		if (ft->wp->base.mode & MODE_MOUSE_UTF8)
1732 			return (xstrdup("1"));
1733 		return (xstrdup("0"));
1734 	}
1735 	return (NULL);
1736 }
1737 
1738 /* Callback for mouse_x. */
1739 static void *
format_cb_mouse_x(struct format_tree * ft)1740 format_cb_mouse_x(struct format_tree *ft)
1741 {
1742 	struct window_pane	*wp;
1743 	u_int			 x, y;
1744 
1745 	if (!ft->m.valid)
1746 		return (NULL);
1747 	wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1748 	if (wp != NULL && cmd_mouse_at(wp, &ft->m, &x, &y, 0) == 0)
1749 		return (format_printf("%u", x));
1750 	if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED)) {
1751 		if (ft->m.statusat == 0 && ft->m.y < ft->m.statuslines)
1752 			return (format_printf("%u", ft->m.x));
1753 		if (ft->m.statusat > 0 && ft->m.y >= (u_int)ft->m.statusat)
1754 			return (format_printf("%u", ft->m.x));
1755 	}
1756 	return (NULL);
1757 }
1758 
1759 /* Callback for mouse_y. */
1760 static void *
format_cb_mouse_y(struct format_tree * ft)1761 format_cb_mouse_y(struct format_tree *ft)
1762 {
1763 	struct window_pane	*wp;
1764 	u_int			 x, y;
1765 
1766 	if (!ft->m.valid)
1767 		return (NULL);
1768 	wp = cmd_mouse_pane(&ft->m, NULL, NULL);
1769 	if (wp != NULL && cmd_mouse_at(wp, &ft->m, &x, &y, 0) == 0)
1770 		return (format_printf("%u", y));
1771 	if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED)) {
1772 		if (ft->m.statusat == 0 && ft->m.y < ft->m.statuslines)
1773 			return (format_printf("%u", ft->m.y));
1774 		if (ft->m.statusat > 0 && ft->m.y >= (u_int)ft->m.statusat)
1775 			return (format_printf("%u", ft->m.y - ft->m.statusat));
1776 	}
1777 	return (NULL);
1778 }
1779 
1780 /* Callback for next_session_id. */
1781 static void *
format_cb_next_session_id(__unused struct format_tree * ft)1782 format_cb_next_session_id(__unused struct format_tree *ft)
1783 {
1784 	return (format_printf("$%u", next_session_id));
1785 }
1786 
1787 /* Callback for origin_flag. */
1788 static void *
format_cb_origin_flag(struct format_tree * ft)1789 format_cb_origin_flag(struct format_tree *ft)
1790 {
1791 	if (ft->wp != NULL) {
1792 		if (ft->wp->base.mode & MODE_ORIGIN)
1793 			return (xstrdup("1"));
1794 		return (xstrdup("0"));
1795 	}
1796 	return (NULL);
1797 }
1798 
1799 /* Callback for pane_active. */
1800 static void *
format_cb_pane_active(struct format_tree * ft)1801 format_cb_pane_active(struct format_tree *ft)
1802 {
1803 	if (ft->wp != NULL) {
1804 		if (ft->wp == ft->wp->window->active)
1805 			return (xstrdup("1"));
1806 		return (xstrdup("0"));
1807 	}
1808 	return (NULL);
1809 }
1810 
1811 /* Callback for pane_at_left. */
1812 static void *
format_cb_pane_at_left(struct format_tree * ft)1813 format_cb_pane_at_left(struct format_tree *ft)
1814 {
1815 	if (ft->wp != NULL) {
1816 		if (ft->wp->xoff == 0)
1817 			return (xstrdup("1"));
1818 		return (xstrdup("0"));
1819 	}
1820 	return (NULL);
1821 }
1822 
1823 /* Callback for pane_at_right. */
1824 static void *
format_cb_pane_at_right(struct format_tree * ft)1825 format_cb_pane_at_right(struct format_tree *ft)
1826 {
1827 	if (ft->wp != NULL) {
1828 		if (ft->wp->xoff + ft->wp->sx == ft->wp->window->sx)
1829 			return (xstrdup("1"));
1830 		return (xstrdup("0"));
1831 	}
1832 	return (NULL);
1833 }
1834 
1835 /* Callback for pane_bottom. */
1836 static void *
format_cb_pane_bottom(struct format_tree * ft)1837 format_cb_pane_bottom(struct format_tree *ft)
1838 {
1839 	if (ft->wp != NULL)
1840 		return (format_printf("%u", ft->wp->yoff + ft->wp->sy - 1));
1841 	return (NULL);
1842 }
1843 
1844 /* Callback for pane_dead. */
1845 static void *
format_cb_pane_dead(struct format_tree * ft)1846 format_cb_pane_dead(struct format_tree *ft)
1847 {
1848 	if (ft->wp != NULL) {
1849 		if (ft->wp->fd == -1)
1850 			return (xstrdup("1"));
1851 		return (xstrdup("0"));
1852 	}
1853 	return (NULL);
1854 }
1855 
1856 /* Callback for pane_dead_signal. */
1857 static void *
format_cb_pane_dead_signal(struct format_tree * ft)1858 format_cb_pane_dead_signal(struct format_tree *ft)
1859 {
1860 	struct window_pane	*wp = ft->wp;
1861 	const char		*name;
1862 
1863 	if (wp != NULL) {
1864 		if ((wp->flags & PANE_STATUSREADY) && WIFSIGNALED(wp->status)) {
1865 			name = sig2name(WTERMSIG(wp->status));
1866 			return (format_printf("%s", name));
1867 		}
1868 		return (NULL);
1869 	}
1870 	return (NULL);
1871 }
1872 
1873 /* Callback for pane_dead_status. */
1874 static void *
format_cb_pane_dead_status(struct format_tree * ft)1875 format_cb_pane_dead_status(struct format_tree *ft)
1876 {
1877 	struct window_pane	*wp = ft->wp;
1878 
1879 	if (wp != NULL) {
1880 		if ((wp->flags & PANE_STATUSREADY) && WIFEXITED(wp->status))
1881 			return (format_printf("%d", WEXITSTATUS(wp->status)));
1882 		return (NULL);
1883 	}
1884 	return (NULL);
1885 }
1886 
1887 /* Callback for pane_dead_time. */
1888 static void *
format_cb_pane_dead_time(struct format_tree * ft)1889 format_cb_pane_dead_time(struct format_tree *ft)
1890 {
1891 	struct window_pane	*wp = ft->wp;
1892 
1893 	if (wp != NULL) {
1894 		if (wp->flags & PANE_STATUSDRAWN)
1895 			return (&wp->dead_time);
1896 		return (NULL);
1897 	}
1898 	return (NULL);
1899 }
1900 
1901 /* Callback for pane_format. */
1902 static void *
format_cb_pane_format(struct format_tree * ft)1903 format_cb_pane_format(struct format_tree *ft)
1904 {
1905 	if (ft->type == FORMAT_TYPE_PANE)
1906 		return (xstrdup("1"));
1907 	return (xstrdup("0"));
1908 }
1909 
1910 /* Callback for pane_height. */
1911 static void *
format_cb_pane_height(struct format_tree * ft)1912 format_cb_pane_height(struct format_tree *ft)
1913 {
1914 	if (ft->wp != NULL)
1915 		return (format_printf("%u", ft->wp->sy));
1916 	return (NULL);
1917 }
1918 
1919 /* Callback for pane_id. */
1920 static void *
format_cb_pane_id(struct format_tree * ft)1921 format_cb_pane_id(struct format_tree *ft)
1922 {
1923 	if (ft->wp != NULL)
1924 		return (format_printf("%%%u", ft->wp->id));
1925 	return (NULL);
1926 }
1927 
1928 /* Callback for pane_index. */
1929 static void *
format_cb_pane_index(struct format_tree * ft)1930 format_cb_pane_index(struct format_tree *ft)
1931 {
1932 	u_int	idx;
1933 
1934 	if (ft->wp != NULL && window_pane_index(ft->wp, &idx) == 0)
1935 		return (format_printf("%u", idx));
1936 	return (NULL);
1937 }
1938 
1939 /* Callback for pane_input_off. */
1940 static void *
format_cb_pane_input_off(struct format_tree * ft)1941 format_cb_pane_input_off(struct format_tree *ft)
1942 {
1943 	if (ft->wp != NULL) {
1944 		if (ft->wp->flags & PANE_INPUTOFF)
1945 			return (xstrdup("1"));
1946 		return (xstrdup("0"));
1947 	}
1948 	return (NULL);
1949 }
1950 
1951 /* Callback for pane_unseen_changes. */
1952 static void *
format_cb_pane_unseen_changes(struct format_tree * ft)1953 format_cb_pane_unseen_changes(struct format_tree *ft)
1954 {
1955 	if (ft->wp != NULL) {
1956 		if (ft->wp->flags & PANE_UNSEENCHANGES)
1957 			return (xstrdup("1"));
1958 		return (xstrdup("0"));
1959 	}
1960 	return (NULL);
1961 }
1962 
1963 /* Callback for pane_key_mode. */
1964 static void *
format_cb_pane_key_mode(struct format_tree * ft)1965 format_cb_pane_key_mode(struct format_tree *ft)
1966 {
1967 	if (ft->wp != NULL && ft->wp->screen != NULL) {
1968 		switch (ft->wp->screen->mode & EXTENDED_KEY_MODES) {
1969 		case MODE_KEYS_EXTENDED:
1970 			return (xstrdup("Ext 1"));
1971 		case MODE_KEYS_EXTENDED_2:
1972 			return (xstrdup("Ext 2"));
1973 		default:
1974 			return (xstrdup("VT10x"));
1975 		}
1976 	}
1977 	return (NULL);
1978 }
1979 
1980 /* Callback for pane_last. */
1981 static void *
format_cb_pane_last(struct format_tree * ft)1982 format_cb_pane_last(struct format_tree *ft)
1983 {
1984 	if (ft->wp != NULL) {
1985 		if (ft->wp == TAILQ_FIRST(&ft->wp->window->last_panes))
1986 			return (xstrdup("1"));
1987 		return (xstrdup("0"));
1988 	}
1989 	return (NULL);
1990 }
1991 
1992 /* Callback for pane_left. */
1993 static void *
format_cb_pane_left(struct format_tree * ft)1994 format_cb_pane_left(struct format_tree *ft)
1995 {
1996 	if (ft->wp != NULL)
1997 		return (format_printf("%u", ft->wp->xoff));
1998 	return (NULL);
1999 }
2000 
2001 /* Callback for pane_marked. */
2002 static void *
format_cb_pane_marked(struct format_tree * ft)2003 format_cb_pane_marked(struct format_tree *ft)
2004 {
2005 	if (ft->wp != NULL) {
2006 		if (server_check_marked() && marked_pane.wp == ft->wp)
2007 			return (xstrdup("1"));
2008 		return (xstrdup("0"));
2009 	}
2010 	return (NULL);
2011 }
2012 
2013 /* Callback for pane_marked_set. */
2014 static void *
format_cb_pane_marked_set(struct format_tree * ft)2015 format_cb_pane_marked_set(struct format_tree *ft)
2016 {
2017 	if (ft->wp != NULL) {
2018 		if (server_check_marked())
2019 			return (xstrdup("1"));
2020 		return (xstrdup("0"));
2021 	}
2022 	return (NULL);
2023 }
2024 
2025 /* Callback for pane_mode. */
2026 static void *
format_cb_pane_mode(struct format_tree * ft)2027 format_cb_pane_mode(struct format_tree *ft)
2028 {
2029 	struct window_mode_entry	*wme;
2030 
2031 	if (ft->wp != NULL) {
2032 		wme = TAILQ_FIRST(&ft->wp->modes);
2033 		if (wme != NULL)
2034 			return (xstrdup(wme->mode->name));
2035 		return (NULL);
2036 	}
2037 	return (NULL);
2038 }
2039 
2040 /* Callback for pane_path. */
2041 static void *
format_cb_pane_path(struct format_tree * ft)2042 format_cb_pane_path(struct format_tree *ft)
2043 {
2044 	if (ft->wp != NULL) {
2045 		if (ft->wp->base.path == NULL)
2046 			return (xstrdup(""));
2047 		return (xstrdup(ft->wp->base.path));
2048 	}
2049 	return (NULL);
2050 }
2051 
2052 /* Callback for pane_pid. */
2053 static void *
format_cb_pane_pid(struct format_tree * ft)2054 format_cb_pane_pid(struct format_tree *ft)
2055 {
2056 	if (ft->wp != NULL)
2057 		return (format_printf("%ld", (long)ft->wp->pid));
2058 	return (NULL);
2059 }
2060 
2061 /* Callback for pane_pipe. */
2062 static void *
format_cb_pane_pipe(struct format_tree * ft)2063 format_cb_pane_pipe(struct format_tree *ft)
2064 {
2065 	if (ft->wp != NULL) {
2066 		if (ft->wp->pipe_fd != -1)
2067 			return (xstrdup("1"));
2068 		return (xstrdup("0"));
2069 	}
2070 	return (NULL);
2071 }
2072 
2073 /* Callback for pane_right. */
2074 static void *
format_cb_pane_right(struct format_tree * ft)2075 format_cb_pane_right(struct format_tree *ft)
2076 {
2077 	if (ft->wp != NULL)
2078 		return (format_printf("%u", ft->wp->xoff + ft->wp->sx - 1));
2079 	return (NULL);
2080 }
2081 
2082 /* Callback for pane_search_string. */
2083 static void *
format_cb_pane_search_string(struct format_tree * ft)2084 format_cb_pane_search_string(struct format_tree *ft)
2085 {
2086 	if (ft->wp != NULL) {
2087 		if (ft->wp->searchstr == NULL)
2088 			return (xstrdup(""));
2089 		return (xstrdup(ft->wp->searchstr));
2090 	}
2091 	return (NULL);
2092 }
2093 
2094 /* Callback for pane_synchronized. */
2095 static void *
format_cb_pane_synchronized(struct format_tree * ft)2096 format_cb_pane_synchronized(struct format_tree *ft)
2097 {
2098 	if (ft->wp != NULL) {
2099 		if (options_get_number(ft->wp->options, "synchronize-panes"))
2100 			return (xstrdup("1"));
2101 		return (xstrdup("0"));
2102 	}
2103 	return (NULL);
2104 }
2105 
2106 /* Callback for pane_title. */
2107 static void *
format_cb_pane_title(struct format_tree * ft)2108 format_cb_pane_title(struct format_tree *ft)
2109 {
2110 	if (ft->wp != NULL)
2111 		return (xstrdup(ft->wp->base.title));
2112 	return (NULL);
2113 }
2114 
2115 /* Callback for pane_top. */
2116 static void *
format_cb_pane_top(struct format_tree * ft)2117 format_cb_pane_top(struct format_tree *ft)
2118 {
2119 	if (ft->wp != NULL)
2120 		return (format_printf("%u", ft->wp->yoff));
2121 	return (NULL);
2122 }
2123 
2124 /* Callback for pane_tty. */
2125 static void *
format_cb_pane_tty(struct format_tree * ft)2126 format_cb_pane_tty(struct format_tree *ft)
2127 {
2128 	if (ft->wp != NULL)
2129 		return (xstrdup(ft->wp->tty));
2130 	return (NULL);
2131 }
2132 
2133 /* Callback for pane_width. */
2134 static void *
format_cb_pane_width(struct format_tree * ft)2135 format_cb_pane_width(struct format_tree *ft)
2136 {
2137 	if (ft->wp != NULL)
2138 		return (format_printf("%u", ft->wp->sx));
2139 	return (NULL);
2140 }
2141 
2142 /* Callback for scroll_region_lower. */
2143 static void *
format_cb_scroll_region_lower(struct format_tree * ft)2144 format_cb_scroll_region_lower(struct format_tree *ft)
2145 {
2146 	if (ft->wp != NULL)
2147 		return (format_printf("%u", ft->wp->base.rlower));
2148 	return (NULL);
2149 }
2150 
2151 /* Callback for scroll_region_upper. */
2152 static void *
format_cb_scroll_region_upper(struct format_tree * ft)2153 format_cb_scroll_region_upper(struct format_tree *ft)
2154 {
2155 	if (ft->wp != NULL)
2156 		return (format_printf("%u", ft->wp->base.rupper));
2157 	return (NULL);
2158 }
2159 
2160 /* Callback for server_sessions. */
2161 static void *
format_cb_server_sessions(__unused struct format_tree * ft)2162 format_cb_server_sessions(__unused struct format_tree *ft)
2163 {
2164 	struct session	*s;
2165 	u_int		 n = 0;
2166 
2167 	RB_FOREACH(s, sessions, &sessions)
2168 		n++;
2169 	return (format_printf("%u", n));
2170 }
2171 
2172 /* Callback for session_attached. */
2173 static void *
format_cb_session_attached(struct format_tree * ft)2174 format_cb_session_attached(struct format_tree *ft)
2175 {
2176 	if (ft->s != NULL)
2177 		return (format_printf("%u", ft->s->attached));
2178 	return (NULL);
2179 }
2180 
2181 /* Callback for session_format. */
2182 static void *
format_cb_session_format(struct format_tree * ft)2183 format_cb_session_format(struct format_tree *ft)
2184 {
2185 	if (ft->type == FORMAT_TYPE_SESSION)
2186 		return (xstrdup("1"));
2187 	return (xstrdup("0"));
2188 }
2189 
2190 /* Callback for session_group. */
2191 static void *
format_cb_session_group(struct format_tree * ft)2192 format_cb_session_group(struct format_tree *ft)
2193 {
2194 	struct session_group	*sg;
2195 
2196 	if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL)
2197 		return (xstrdup(sg->name));
2198 	return (NULL);
2199 }
2200 
2201 /* Callback for session_group_attached. */
2202 static void *
format_cb_session_group_attached(struct format_tree * ft)2203 format_cb_session_group_attached(struct format_tree *ft)
2204 {
2205 	struct session_group	*sg;
2206 
2207 	if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL)
2208 		return (format_printf("%u", session_group_attached_count (sg)));
2209 	return (NULL);
2210 }
2211 
2212 /* Callback for session_group_many_attached. */
2213 static void *
format_cb_session_group_many_attached(struct format_tree * ft)2214 format_cb_session_group_many_attached(struct format_tree *ft)
2215 {
2216 	struct session_group	*sg;
2217 
2218 	if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL) {
2219 		if (session_group_attached_count (sg) > 1)
2220 			return (xstrdup("1"));
2221 		return (xstrdup("0"));
2222 	}
2223 	return (NULL);
2224 }
2225 
2226 /* Callback for session_group_size. */
2227 static void *
format_cb_session_group_size(struct format_tree * ft)2228 format_cb_session_group_size(struct format_tree *ft)
2229 {
2230 	struct session_group	*sg;
2231 
2232 	if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL)
2233 		return (format_printf("%u", session_group_count (sg)));
2234 	return (NULL);
2235 }
2236 
2237 /* Callback for session_grouped. */
2238 static void *
format_cb_session_grouped(struct format_tree * ft)2239 format_cb_session_grouped(struct format_tree *ft)
2240 {
2241 	if (ft->s != NULL) {
2242 		if (session_group_contains(ft->s) != NULL)
2243 			return (xstrdup("1"));
2244 		return (xstrdup("0"));
2245 	}
2246 	return (NULL);
2247 }
2248 
2249 /* Callback for session_id. */
2250 static void *
format_cb_session_id(struct format_tree * ft)2251 format_cb_session_id(struct format_tree *ft)
2252 {
2253 	if (ft->s != NULL)
2254 		return (format_printf("$%u", ft->s->id));
2255 	return (NULL);
2256 }
2257 
2258 /* Callback for session_many_attached. */
2259 static void *
format_cb_session_many_attached(struct format_tree * ft)2260 format_cb_session_many_attached(struct format_tree *ft)
2261 {
2262 	if (ft->s != NULL) {
2263 		if (ft->s->attached > 1)
2264 			return (xstrdup("1"));
2265 		return (xstrdup("0"));
2266 	}
2267 	return (NULL);
2268 }
2269 
2270 /* Callback for session_marked. */
2271 static void *
format_cb_session_marked(struct format_tree * ft)2272 format_cb_session_marked(struct format_tree *ft)
2273 {
2274 	if (ft->s != NULL) {
2275 		if (server_check_marked() && marked_pane.s == ft->s)
2276 			return (xstrdup("1"));
2277 		return (xstrdup("0"));
2278 	}
2279 	return (NULL);
2280 }
2281 
2282 /* Callback for session_name. */
2283 static void *
format_cb_session_name(struct format_tree * ft)2284 format_cb_session_name(struct format_tree *ft)
2285 {
2286 	if (ft->s != NULL)
2287 		return (xstrdup(ft->s->name));
2288 	return (NULL);
2289 }
2290 
2291 /* Callback for session_path. */
2292 static void *
format_cb_session_path(struct format_tree * ft)2293 format_cb_session_path(struct format_tree *ft)
2294 {
2295 	if (ft->s != NULL)
2296 		return (xstrdup(ft->s->cwd));
2297 	return (NULL);
2298 }
2299 
2300 /* Callback for session_windows. */
2301 static void *
format_cb_session_windows(struct format_tree * ft)2302 format_cb_session_windows(struct format_tree *ft)
2303 {
2304 	if (ft->s != NULL)
2305 		return (format_printf("%u", winlink_count(&ft->s->windows)));
2306 	return (NULL);
2307 }
2308 
2309 /* Callback for socket_path. */
2310 static void *
format_cb_socket_path(__unused struct format_tree * ft)2311 format_cb_socket_path(__unused struct format_tree *ft)
2312 {
2313 	return (xstrdup(socket_path));
2314 }
2315 
2316 /* Callback for version. */
2317 static void *
format_cb_version(__unused struct format_tree * ft)2318 format_cb_version(__unused struct format_tree *ft)
2319 {
2320 	return (xstrdup(getversion()));
2321 }
2322 
2323 /* Callback for sixel_support. */
2324 static void *
format_cb_sixel_support(__unused struct format_tree * ft)2325 format_cb_sixel_support(__unused struct format_tree *ft)
2326 {
2327 	return (xstrdup("0"));
2328 }
2329 
2330 /* Callback for active_window_index. */
2331 static void *
format_cb_active_window_index(struct format_tree * ft)2332 format_cb_active_window_index(struct format_tree *ft)
2333 {
2334 	if (ft->s != NULL)
2335 		return (format_printf("%u", ft->s->curw->idx));
2336 	return (NULL);
2337 }
2338 
2339 /* Callback for last_window_index. */
2340 static void *
format_cb_last_window_index(struct format_tree * ft)2341 format_cb_last_window_index(struct format_tree *ft)
2342 {
2343 	struct winlink	*wl;
2344 
2345 	if (ft->s != NULL) {
2346 		wl = RB_MAX(winlinks, &ft->s->windows);
2347 		return (format_printf("%u", wl->idx));
2348 	}
2349 	return (NULL);
2350 }
2351 
2352 /* Callback for window_active. */
2353 static void *
format_cb_window_active(struct format_tree * ft)2354 format_cb_window_active(struct format_tree *ft)
2355 {
2356 	if (ft->wl != NULL) {
2357 		if (ft->wl == ft->wl->session->curw)
2358 			return (xstrdup("1"));
2359 		return (xstrdup("0"));
2360 	}
2361 	return (NULL);
2362 }
2363 
2364 /* Callback for window_activity_flag. */
2365 static void *
format_cb_window_activity_flag(struct format_tree * ft)2366 format_cb_window_activity_flag(struct format_tree *ft)
2367 {
2368 	if (ft->wl != NULL) {
2369 		if (ft->wl->flags & WINLINK_ACTIVITY)
2370 			return (xstrdup("1"));
2371 		return (xstrdup("0"));
2372 	}
2373 	return (NULL);
2374 }
2375 
2376 /* Callback for window_bell_flag. */
2377 static void *
format_cb_window_bell_flag(struct format_tree * ft)2378 format_cb_window_bell_flag(struct format_tree *ft)
2379 {
2380 	if (ft->wl != NULL) {
2381 		if (ft->wl->flags & WINLINK_BELL)
2382 			return (xstrdup("1"));
2383 		return (xstrdup("0"));
2384 	}
2385 	return (NULL);
2386 }
2387 
2388 /* Callback for window_bigger. */
2389 static void *
format_cb_window_bigger(struct format_tree * ft)2390 format_cb_window_bigger(struct format_tree *ft)
2391 {
2392 	u_int	ox, oy, sx, sy;
2393 
2394 	if (ft->c != NULL) {
2395 		if (tty_window_offset(&ft->c->tty, &ox, &oy, &sx, &sy))
2396 			return (xstrdup("1"));
2397 		return (xstrdup("0"));
2398 	}
2399 	return (NULL);
2400 }
2401 
2402 /* Callback for window_cell_height. */
2403 static void *
format_cb_window_cell_height(struct format_tree * ft)2404 format_cb_window_cell_height(struct format_tree *ft)
2405 {
2406 	if (ft->w != NULL)
2407 		return (format_printf("%u", ft->w->ypixel));
2408 	return (NULL);
2409 }
2410 
2411 /* Callback for window_cell_width. */
2412 static void *
format_cb_window_cell_width(struct format_tree * ft)2413 format_cb_window_cell_width(struct format_tree *ft)
2414 {
2415 	if (ft->w != NULL)
2416 		return (format_printf("%u", ft->w->xpixel));
2417 	return (NULL);
2418 }
2419 
2420 /* Callback for window_end_flag. */
2421 static void *
format_cb_window_end_flag(struct format_tree * ft)2422 format_cb_window_end_flag(struct format_tree *ft)
2423 {
2424 	if (ft->wl != NULL) {
2425 		if (ft->wl == RB_MAX(winlinks, &ft->wl->session->windows))
2426 			return (xstrdup("1"));
2427 		return (xstrdup("0"));
2428 	}
2429 	return (NULL);
2430 }
2431 
2432 /* Callback for window_flags. */
2433 static void *
format_cb_window_flags(struct format_tree * ft)2434 format_cb_window_flags(struct format_tree *ft)
2435 {
2436 	if (ft->wl != NULL)
2437 		return (xstrdup(window_printable_flags(ft->wl, 1)));
2438 	return (NULL);
2439 }
2440 
2441 /* Callback for window_format. */
2442 static void *
format_cb_window_format(struct format_tree * ft)2443 format_cb_window_format(struct format_tree *ft)
2444 {
2445 	if (ft->type == FORMAT_TYPE_WINDOW)
2446 		return (xstrdup("1"));
2447 	return (xstrdup("0"));
2448 }
2449 
2450 /* Callback for window_height. */
2451 static void *
format_cb_window_height(struct format_tree * ft)2452 format_cb_window_height(struct format_tree *ft)
2453 {
2454 	if (ft->w != NULL)
2455 		return (format_printf("%u", ft->w->sy));
2456 	return (NULL);
2457 }
2458 
2459 /* Callback for window_id. */
2460 static void *
format_cb_window_id(struct format_tree * ft)2461 format_cb_window_id(struct format_tree *ft)
2462 {
2463 	if (ft->w != NULL)
2464 		return (format_printf("@%u", ft->w->id));
2465 	return (NULL);
2466 }
2467 
2468 /* Callback for window_index. */
2469 static void *
format_cb_window_index(struct format_tree * ft)2470 format_cb_window_index(struct format_tree *ft)
2471 {
2472 	if (ft->wl != NULL)
2473 		return (format_printf("%d", ft->wl->idx));
2474 	return (NULL);
2475 }
2476 
2477 /* Callback for window_last_flag. */
2478 static void *
format_cb_window_last_flag(struct format_tree * ft)2479 format_cb_window_last_flag(struct format_tree *ft)
2480 {
2481 	if (ft->wl != NULL) {
2482 		if (ft->wl == TAILQ_FIRST(&ft->wl->session->lastw))
2483 			return (xstrdup("1"));
2484 		return (xstrdup("0"));
2485 	}
2486 	return (NULL);
2487 }
2488 
2489 /* Callback for window_linked. */
2490 static void *
format_cb_window_linked(struct format_tree * ft)2491 format_cb_window_linked(struct format_tree *ft)
2492 {
2493 	if (ft->wl != NULL) {
2494 		if (session_is_linked(ft->wl->session, ft->wl->window))
2495 			return (xstrdup("1"));
2496 		return (xstrdup("0"));
2497 	}
2498 	return (NULL);
2499 }
2500 
2501 /* Callback for window_linked_sessions. */
2502 static void *
format_cb_window_linked_sessions(struct format_tree * ft)2503 format_cb_window_linked_sessions(struct format_tree *ft)
2504 {
2505 	if (ft->wl != NULL)
2506 		return (format_printf("%u", ft->wl->window->references));
2507 	return (NULL);
2508 }
2509 
2510 /* Callback for window_marked_flag. */
2511 static void *
format_cb_window_marked_flag(struct format_tree * ft)2512 format_cb_window_marked_flag(struct format_tree *ft)
2513 {
2514 	if (ft->wl != NULL) {
2515 		if (server_check_marked() && marked_pane.wl == ft->wl)
2516 			return (xstrdup("1"));
2517 		return (xstrdup("0"));
2518 	}
2519 	return (NULL);
2520 }
2521 
2522 /* Callback for window_name. */
2523 static void *
format_cb_window_name(struct format_tree * ft)2524 format_cb_window_name(struct format_tree *ft)
2525 {
2526 	if (ft->w != NULL)
2527 		return (format_printf("%s", ft->w->name));
2528 	return (NULL);
2529 }
2530 
2531 /* Callback for window_offset_x. */
2532 static void *
format_cb_window_offset_x(struct format_tree * ft)2533 format_cb_window_offset_x(struct format_tree *ft)
2534 {
2535 	u_int	ox, oy, sx, sy;
2536 
2537 	if (ft->c != NULL) {
2538 		if (tty_window_offset(&ft->c->tty, &ox, &oy, &sx, &sy))
2539 			return (format_printf("%u", ox));
2540 		return (NULL);
2541 	}
2542 	return (NULL);
2543 }
2544 
2545 /* Callback for window_offset_y. */
2546 static void *
format_cb_window_offset_y(struct format_tree * ft)2547 format_cb_window_offset_y(struct format_tree *ft)
2548 {
2549 	u_int	ox, oy, sx, sy;
2550 
2551 	if (ft->c != NULL) {
2552 		if (tty_window_offset(&ft->c->tty, &ox, &oy, &sx, &sy))
2553 			return (format_printf("%u", oy));
2554 		return (NULL);
2555 	}
2556 	return (NULL);
2557 }
2558 
2559 /* Callback for window_panes. */
2560 static void *
format_cb_window_panes(struct format_tree * ft)2561 format_cb_window_panes(struct format_tree *ft)
2562 {
2563 	if (ft->w != NULL)
2564 		return (format_printf("%u", window_count_panes(ft->w)));
2565 	return (NULL);
2566 }
2567 
2568 /* Callback for window_raw_flags. */
2569 static void *
format_cb_window_raw_flags(struct format_tree * ft)2570 format_cb_window_raw_flags(struct format_tree *ft)
2571 {
2572 	if (ft->wl != NULL)
2573 		return (xstrdup(window_printable_flags(ft->wl, 0)));
2574 	return (NULL);
2575 }
2576 
2577 /* Callback for window_silence_flag. */
2578 static void *
format_cb_window_silence_flag(struct format_tree * ft)2579 format_cb_window_silence_flag(struct format_tree *ft)
2580 {
2581 	if (ft->wl != NULL) {
2582 		if (ft->wl->flags & WINLINK_SILENCE)
2583 			return (xstrdup("1"));
2584 		return (xstrdup("0"));
2585 	}
2586 	return (NULL);
2587 }
2588 
2589 /* Callback for window_start_flag. */
2590 static void *
format_cb_window_start_flag(struct format_tree * ft)2591 format_cb_window_start_flag(struct format_tree *ft)
2592 {
2593 	if (ft->wl != NULL) {
2594 		if (ft->wl == RB_MIN(winlinks, &ft->wl->session->windows))
2595 			return (xstrdup("1"));
2596 		return (xstrdup("0"));
2597 	}
2598 	return (NULL);
2599 }
2600 
2601 /* Callback for window_width. */
2602 static void *
format_cb_window_width(struct format_tree * ft)2603 format_cb_window_width(struct format_tree *ft)
2604 {
2605 	if (ft->w != NULL)
2606 		return (format_printf("%u", ft->w->sx));
2607 	return (NULL);
2608 }
2609 
2610 /* Callback for window_zoomed_flag. */
2611 static void *
format_cb_window_zoomed_flag(struct format_tree * ft)2612 format_cb_window_zoomed_flag(struct format_tree *ft)
2613 {
2614 	if (ft->w != NULL) {
2615 		if (ft->w->flags & WINDOW_ZOOMED)
2616 			return (xstrdup("1"));
2617 		return (xstrdup("0"));
2618 	}
2619 	return (NULL);
2620 }
2621 
2622 /* Callback for wrap_flag. */
2623 static void *
format_cb_wrap_flag(struct format_tree * ft)2624 format_cb_wrap_flag(struct format_tree *ft)
2625 {
2626 	if (ft->wp != NULL) {
2627 		if (ft->wp->base.mode & MODE_WRAP)
2628 			return (xstrdup("1"));
2629 		return (xstrdup("0"));
2630 	}
2631 	return (NULL);
2632 }
2633 
2634 /* Callback for buffer_created. */
2635 static void *
format_cb_buffer_created(struct format_tree * ft)2636 format_cb_buffer_created(struct format_tree *ft)
2637 {
2638 	static struct timeval	 tv;
2639 
2640 	if (ft->pb != NULL) {
2641 		timerclear(&tv);
2642 		tv.tv_sec = paste_buffer_created(ft->pb);
2643 		return (&tv);
2644 	}
2645 	return (NULL);
2646 }
2647 
2648 /* Callback for client_activity. */
2649 static void *
format_cb_client_activity(struct format_tree * ft)2650 format_cb_client_activity(struct format_tree *ft)
2651 {
2652 	if (ft->c != NULL)
2653 		return (&ft->c->activity_time);
2654 	return (NULL);
2655 }
2656 
2657 /* Callback for client_created. */
2658 static void *
format_cb_client_created(struct format_tree * ft)2659 format_cb_client_created(struct format_tree *ft)
2660 {
2661 	if (ft->c != NULL)
2662 		return (&ft->c->creation_time);
2663 	return (NULL);
2664 }
2665 
2666 /* Callback for session_activity. */
2667 static void *
format_cb_session_activity(struct format_tree * ft)2668 format_cb_session_activity(struct format_tree *ft)
2669 {
2670 	if (ft->s != NULL)
2671 		return (&ft->s->activity_time);
2672 	return (NULL);
2673 }
2674 
2675 /* Callback for session_created. */
2676 static void *
format_cb_session_created(struct format_tree * ft)2677 format_cb_session_created(struct format_tree *ft)
2678 {
2679 	if (ft->s != NULL)
2680 		return (&ft->s->creation_time);
2681 	return (NULL);
2682 }
2683 
2684 /* Callback for session_last_attached. */
2685 static void *
format_cb_session_last_attached(struct format_tree * ft)2686 format_cb_session_last_attached(struct format_tree *ft)
2687 {
2688 	if (ft->s != NULL)
2689 		return (&ft->s->last_attached_time);
2690 	return (NULL);
2691 }
2692 
2693 /* Callback for start_time. */
2694 static void *
format_cb_start_time(__unused struct format_tree * ft)2695 format_cb_start_time(__unused struct format_tree *ft)
2696 {
2697 	return (&start_time);
2698 }
2699 
2700 /* Callback for window_activity. */
2701 static void *
format_cb_window_activity(struct format_tree * ft)2702 format_cb_window_activity(struct format_tree *ft)
2703 {
2704 	if (ft->w != NULL)
2705 		return (&ft->w->activity_time);
2706 	return (NULL);
2707 }
2708 
2709 /* Callback for buffer_mode_format, */
2710 static void *
format_cb_buffer_mode_format(__unused struct format_tree * ft)2711 format_cb_buffer_mode_format(__unused struct format_tree *ft)
2712 {
2713 	return (xstrdup(window_buffer_mode.default_format));
2714 }
2715 
2716 /* Callback for client_mode_format, */
2717 static void *
format_cb_client_mode_format(__unused struct format_tree * ft)2718 format_cb_client_mode_format(__unused struct format_tree *ft)
2719 {
2720 	return (xstrdup(window_client_mode.default_format));
2721 }
2722 
2723 /* Callback for tree_mode_format, */
2724 static void *
format_cb_tree_mode_format(__unused struct format_tree * ft)2725 format_cb_tree_mode_format(__unused struct format_tree *ft)
2726 {
2727 	return (xstrdup(window_tree_mode.default_format));
2728 }
2729 
2730 /* Callback for uid. */
2731 static void *
format_cb_uid(__unused struct format_tree * ft)2732 format_cb_uid(__unused struct format_tree *ft)
2733 {
2734 	return (format_printf("%ld", (long)getuid()));
2735 }
2736 
2737 /* Callback for user. */
2738 static void *
format_cb_user(__unused struct format_tree * ft)2739 format_cb_user(__unused struct format_tree *ft)
2740 {
2741 	struct passwd	*pw;
2742 
2743 	if ((pw = getpwuid(getuid())) != NULL)
2744 		return (xstrdup(pw->pw_name));
2745 	return (NULL);
2746 }
2747 
2748 /* Format table type. */
2749 enum format_table_type {
2750 	FORMAT_TABLE_STRING,
2751 	FORMAT_TABLE_TIME
2752 };
2753 
2754 /* Format table entry. */
2755 struct format_table_entry {
2756 	const char		*key;
2757 	enum format_table_type	 type;
2758 	format_cb		 cb;
2759 };
2760 
2761 /*
2762  * Format table. Default format variables (that are almost always in the tree
2763  * and where the value is expanded by a callback in this file) are listed here.
2764  * Only variables which are added by the caller go into the tree.
2765  */
2766 static const struct format_table_entry format_table[] = {
2767 	{ "active_window_index", FORMAT_TABLE_STRING,
2768 	  format_cb_active_window_index
2769 	},
2770 	{ "alternate_on", FORMAT_TABLE_STRING,
2771 	  format_cb_alternate_on
2772 	},
2773 	{ "alternate_saved_x", FORMAT_TABLE_STRING,
2774 	  format_cb_alternate_saved_x
2775 	},
2776 	{ "alternate_saved_y", FORMAT_TABLE_STRING,
2777 	  format_cb_alternate_saved_y
2778 	},
2779 	{ "buffer_created", FORMAT_TABLE_TIME,
2780 	  format_cb_buffer_created
2781 	},
2782 	{ "buffer_mode_format", FORMAT_TABLE_STRING,
2783 	  format_cb_buffer_mode_format
2784 	},
2785 	{ "buffer_name", FORMAT_TABLE_STRING,
2786 	  format_cb_buffer_name
2787 	},
2788 	{ "buffer_sample", FORMAT_TABLE_STRING,
2789 	  format_cb_buffer_sample
2790 	},
2791 	{ "buffer_size", FORMAT_TABLE_STRING,
2792 	  format_cb_buffer_size
2793 	},
2794 	{ "client_activity", FORMAT_TABLE_TIME,
2795 	  format_cb_client_activity
2796 	},
2797 	{ "client_cell_height", FORMAT_TABLE_STRING,
2798 	  format_cb_client_cell_height
2799 	},
2800 	{ "client_cell_width", FORMAT_TABLE_STRING,
2801 	  format_cb_client_cell_width
2802 	},
2803 	{ "client_control_mode", FORMAT_TABLE_STRING,
2804 	  format_cb_client_control_mode
2805 	},
2806 	{ "client_created", FORMAT_TABLE_TIME,
2807 	  format_cb_client_created
2808 	},
2809 	{ "client_discarded", FORMAT_TABLE_STRING,
2810 	  format_cb_client_discarded
2811 	},
2812 	{ "client_flags", FORMAT_TABLE_STRING,
2813 	  format_cb_client_flags
2814 	},
2815 	{ "client_height", FORMAT_TABLE_STRING,
2816 	  format_cb_client_height
2817 	},
2818 	{ "client_key_table", FORMAT_TABLE_STRING,
2819 	  format_cb_client_key_table
2820 	},
2821 	{ "client_last_session", FORMAT_TABLE_STRING,
2822 	  format_cb_client_last_session
2823 	},
2824 	{ "client_mode_format", FORMAT_TABLE_STRING,
2825 	  format_cb_client_mode_format
2826 	},
2827 	{ "client_name", FORMAT_TABLE_STRING,
2828 	  format_cb_client_name
2829 	},
2830 	{ "client_pid", FORMAT_TABLE_STRING,
2831 	  format_cb_client_pid
2832 	},
2833 	{ "client_prefix", FORMAT_TABLE_STRING,
2834 	  format_cb_client_prefix
2835 	},
2836 	{ "client_readonly", FORMAT_TABLE_STRING,
2837 	  format_cb_client_readonly
2838 	},
2839 	{ "client_session", FORMAT_TABLE_STRING,
2840 	  format_cb_client_session
2841 	},
2842 	{ "client_termfeatures", FORMAT_TABLE_STRING,
2843 	  format_cb_client_termfeatures
2844 	},
2845 	{ "client_termname", FORMAT_TABLE_STRING,
2846 	  format_cb_client_termname
2847 	},
2848 	{ "client_termtype", FORMAT_TABLE_STRING,
2849 	  format_cb_client_termtype
2850 	},
2851 	{ "client_tty", FORMAT_TABLE_STRING,
2852 	  format_cb_client_tty
2853 	},
2854 	{ "client_uid", FORMAT_TABLE_STRING,
2855 	  format_cb_client_uid
2856 	},
2857 	{ "client_user", FORMAT_TABLE_STRING,
2858 	  format_cb_client_user
2859 	},
2860 	{ "client_utf8", FORMAT_TABLE_STRING,
2861 	  format_cb_client_utf8
2862 	},
2863 	{ "client_width", FORMAT_TABLE_STRING,
2864 	  format_cb_client_width
2865 	},
2866 	{ "client_written", FORMAT_TABLE_STRING,
2867 	  format_cb_client_written
2868 	},
2869 	{ "config_files", FORMAT_TABLE_STRING,
2870 	  format_cb_config_files
2871 	},
2872 	{ "cursor_character", FORMAT_TABLE_STRING,
2873 	  format_cb_cursor_character
2874 	},
2875 	{ "cursor_flag", FORMAT_TABLE_STRING,
2876 	  format_cb_cursor_flag
2877 	},
2878 	{ "cursor_x", FORMAT_TABLE_STRING,
2879 	  format_cb_cursor_x
2880 	},
2881 	{ "cursor_y", FORMAT_TABLE_STRING,
2882 	  format_cb_cursor_y
2883 	},
2884 	{ "history_all_bytes", FORMAT_TABLE_STRING,
2885 	  format_cb_history_all_bytes
2886 	},
2887 	{ "history_bytes", FORMAT_TABLE_STRING,
2888 	  format_cb_history_bytes
2889 	},
2890 	{ "history_limit", FORMAT_TABLE_STRING,
2891 	  format_cb_history_limit
2892 	},
2893 	{ "history_size", FORMAT_TABLE_STRING,
2894 	  format_cb_history_size
2895 	},
2896 	{ "host", FORMAT_TABLE_STRING,
2897 	  format_cb_host
2898 	},
2899 	{ "host_short", FORMAT_TABLE_STRING,
2900 	  format_cb_host_short
2901 	},
2902 	{ "insert_flag", FORMAT_TABLE_STRING,
2903 	  format_cb_insert_flag
2904 	},
2905 	{ "keypad_cursor_flag", FORMAT_TABLE_STRING,
2906 	  format_cb_keypad_cursor_flag
2907 	},
2908 	{ "keypad_flag", FORMAT_TABLE_STRING,
2909 	  format_cb_keypad_flag
2910 	},
2911 	{ "last_window_index", FORMAT_TABLE_STRING,
2912 	  format_cb_last_window_index
2913 	},
2914 	{ "mouse_all_flag", FORMAT_TABLE_STRING,
2915 	  format_cb_mouse_all_flag
2916 	},
2917 	{ "mouse_any_flag", FORMAT_TABLE_STRING,
2918 	  format_cb_mouse_any_flag
2919 	},
2920 	{ "mouse_button_flag", FORMAT_TABLE_STRING,
2921 	  format_cb_mouse_button_flag
2922 	},
2923 	{ "mouse_hyperlink", FORMAT_TABLE_STRING,
2924 	  format_cb_mouse_hyperlink
2925 	},
2926 	{ "mouse_line", FORMAT_TABLE_STRING,
2927 	  format_cb_mouse_line
2928 	},
2929 	{ "mouse_pane", FORMAT_TABLE_STRING,
2930 	  format_cb_mouse_pane
2931 	},
2932 	{ "mouse_sgr_flag", FORMAT_TABLE_STRING,
2933 	  format_cb_mouse_sgr_flag
2934 	},
2935 	{ "mouse_standard_flag", FORMAT_TABLE_STRING,
2936 	  format_cb_mouse_standard_flag
2937 	},
2938 	{ "mouse_status_line", FORMAT_TABLE_STRING,
2939 	  format_cb_mouse_status_line
2940 	},
2941 	{ "mouse_status_range", FORMAT_TABLE_STRING,
2942 	  format_cb_mouse_status_range
2943 	},
2944 	{ "mouse_utf8_flag", FORMAT_TABLE_STRING,
2945 	  format_cb_mouse_utf8_flag
2946 	},
2947 	{ "mouse_word", FORMAT_TABLE_STRING,
2948 	  format_cb_mouse_word
2949 	},
2950 	{ "mouse_x", FORMAT_TABLE_STRING,
2951 	  format_cb_mouse_x
2952 	},
2953 	{ "mouse_y", FORMAT_TABLE_STRING,
2954 	  format_cb_mouse_y
2955 	},
2956 	{ "next_session_id", FORMAT_TABLE_STRING,
2957 	  format_cb_next_session_id
2958 	},
2959 	{ "origin_flag", FORMAT_TABLE_STRING,
2960 	  format_cb_origin_flag
2961 	},
2962 	{ "pane_active", FORMAT_TABLE_STRING,
2963 	  format_cb_pane_active
2964 	},
2965 	{ "pane_at_bottom", FORMAT_TABLE_STRING,
2966 	  format_cb_pane_at_bottom
2967 	},
2968 	{ "pane_at_left", FORMAT_TABLE_STRING,
2969 	  format_cb_pane_at_left
2970 	},
2971 	{ "pane_at_right", FORMAT_TABLE_STRING,
2972 	  format_cb_pane_at_right
2973 	},
2974 	{ "pane_at_top", FORMAT_TABLE_STRING,
2975 	  format_cb_pane_at_top
2976 	},
2977 	{ "pane_bg", FORMAT_TABLE_STRING,
2978 	  format_cb_pane_bg
2979 	},
2980 	{ "pane_bottom", FORMAT_TABLE_STRING,
2981 	  format_cb_pane_bottom
2982 	},
2983 	{ "pane_current_command", FORMAT_TABLE_STRING,
2984 	  format_cb_current_command
2985 	},
2986 	{ "pane_current_path", FORMAT_TABLE_STRING,
2987 	  format_cb_current_path
2988 	},
2989 	{ "pane_dead", FORMAT_TABLE_STRING,
2990 	  format_cb_pane_dead
2991 	},
2992 	{ "pane_dead_signal", FORMAT_TABLE_STRING,
2993 	  format_cb_pane_dead_signal
2994 	},
2995 	{ "pane_dead_status", FORMAT_TABLE_STRING,
2996 	  format_cb_pane_dead_status
2997 	},
2998 	{ "pane_dead_time", FORMAT_TABLE_TIME,
2999 	  format_cb_pane_dead_time
3000 	},
3001 	{ "pane_fg", FORMAT_TABLE_STRING,
3002 	  format_cb_pane_fg
3003 	},
3004 	{ "pane_format", FORMAT_TABLE_STRING,
3005 	  format_cb_pane_format
3006 	},
3007 	{ "pane_height", FORMAT_TABLE_STRING,
3008 	  format_cb_pane_height
3009 	},
3010 	{ "pane_id", FORMAT_TABLE_STRING,
3011 	  format_cb_pane_id
3012 	},
3013 	{ "pane_in_mode", FORMAT_TABLE_STRING,
3014 	  format_cb_pane_in_mode
3015 	},
3016 	{ "pane_index", FORMAT_TABLE_STRING,
3017 	  format_cb_pane_index
3018 	},
3019 	{ "pane_input_off", FORMAT_TABLE_STRING,
3020 	  format_cb_pane_input_off
3021 	},
3022 	{ "pane_key_mode", FORMAT_TABLE_STRING,
3023 	  format_cb_pane_key_mode
3024 	},
3025 	{ "pane_last", FORMAT_TABLE_STRING,
3026 	  format_cb_pane_last
3027 	},
3028 	{ "pane_left", FORMAT_TABLE_STRING,
3029 	  format_cb_pane_left
3030 	},
3031 	{ "pane_marked", FORMAT_TABLE_STRING,
3032 	  format_cb_pane_marked
3033 	},
3034 	{ "pane_marked_set", FORMAT_TABLE_STRING,
3035 	  format_cb_pane_marked_set
3036 	},
3037 	{ "pane_mode", FORMAT_TABLE_STRING,
3038 	  format_cb_pane_mode
3039 	},
3040 	{ "pane_path", FORMAT_TABLE_STRING,
3041 	  format_cb_pane_path
3042 	},
3043 	{ "pane_pid", FORMAT_TABLE_STRING,
3044 	  format_cb_pane_pid
3045 	},
3046 	{ "pane_pipe", FORMAT_TABLE_STRING,
3047 	  format_cb_pane_pipe
3048 	},
3049 	{ "pane_right", FORMAT_TABLE_STRING,
3050 	  format_cb_pane_right
3051 	},
3052 	{ "pane_search_string", FORMAT_TABLE_STRING,
3053 	  format_cb_pane_search_string
3054 	},
3055 	{ "pane_start_command", FORMAT_TABLE_STRING,
3056 	  format_cb_start_command
3057 	},
3058 	{ "pane_start_path", FORMAT_TABLE_STRING,
3059 	  format_cb_start_path
3060 	},
3061 	{ "pane_synchronized", FORMAT_TABLE_STRING,
3062 	  format_cb_pane_synchronized
3063 	},
3064 	{ "pane_tabs", FORMAT_TABLE_STRING,
3065 	  format_cb_pane_tabs
3066 	},
3067 	{ "pane_title", FORMAT_TABLE_STRING,
3068 	  format_cb_pane_title
3069 	},
3070 	{ "pane_top", FORMAT_TABLE_STRING,
3071 	  format_cb_pane_top
3072 	},
3073 	{ "pane_tty", FORMAT_TABLE_STRING,
3074 	  format_cb_pane_tty
3075 	},
3076 	{ "pane_unseen_changes", FORMAT_TABLE_STRING,
3077 	  format_cb_pane_unseen_changes
3078 	},
3079 	{ "pane_width", FORMAT_TABLE_STRING,
3080 	  format_cb_pane_width
3081 	},
3082 	{ "pid", FORMAT_TABLE_STRING,
3083 	  format_cb_pid
3084 	},
3085 	{ "scroll_region_lower", FORMAT_TABLE_STRING,
3086 	  format_cb_scroll_region_lower
3087 	},
3088 	{ "scroll_region_upper", FORMAT_TABLE_STRING,
3089 	  format_cb_scroll_region_upper
3090 	},
3091 	{ "server_sessions", FORMAT_TABLE_STRING,
3092 	  format_cb_server_sessions
3093 	},
3094 	{ "session_activity", FORMAT_TABLE_TIME,
3095 	  format_cb_session_activity
3096 	},
3097 	{ "session_alerts", FORMAT_TABLE_STRING,
3098 	  format_cb_session_alerts
3099 	},
3100 	{ "session_attached", FORMAT_TABLE_STRING,
3101 	  format_cb_session_attached
3102 	},
3103 	{ "session_attached_list", FORMAT_TABLE_STRING,
3104 	  format_cb_session_attached_list
3105 	},
3106 	{ "session_created", FORMAT_TABLE_TIME,
3107 	  format_cb_session_created
3108 	},
3109 	{ "session_format", FORMAT_TABLE_STRING,
3110 	  format_cb_session_format
3111 	},
3112 	{ "session_group", FORMAT_TABLE_STRING,
3113 	  format_cb_session_group
3114 	},
3115 	{ "session_group_attached", FORMAT_TABLE_STRING,
3116 	  format_cb_session_group_attached
3117 	},
3118 	{ "session_group_attached_list", FORMAT_TABLE_STRING,
3119 	  format_cb_session_group_attached_list
3120 	},
3121 	{ "session_group_list", FORMAT_TABLE_STRING,
3122 	  format_cb_session_group_list
3123 	},
3124 	{ "session_group_many_attached", FORMAT_TABLE_STRING,
3125 	  format_cb_session_group_many_attached
3126 	},
3127 	{ "session_group_size", FORMAT_TABLE_STRING,
3128 	  format_cb_session_group_size
3129 	},
3130 	{ "session_grouped", FORMAT_TABLE_STRING,
3131 	  format_cb_session_grouped
3132 	},
3133 	{ "session_id", FORMAT_TABLE_STRING,
3134 	  format_cb_session_id
3135 	},
3136 	{ "session_last_attached", FORMAT_TABLE_TIME,
3137 	  format_cb_session_last_attached
3138 	},
3139 	{ "session_many_attached", FORMAT_TABLE_STRING,
3140 	  format_cb_session_many_attached
3141 	},
3142 	{ "session_marked", FORMAT_TABLE_STRING,
3143 	  format_cb_session_marked,
3144 	},
3145 	{ "session_name", FORMAT_TABLE_STRING,
3146 	  format_cb_session_name
3147 	},
3148 	{ "session_path", FORMAT_TABLE_STRING,
3149 	  format_cb_session_path
3150 	},
3151 	{ "session_stack", FORMAT_TABLE_STRING,
3152 	  format_cb_session_stack
3153 	},
3154 	{ "session_windows", FORMAT_TABLE_STRING,
3155 	  format_cb_session_windows
3156 	},
3157 	{ "sixel_support", FORMAT_TABLE_STRING,
3158 	  format_cb_sixel_support
3159 	},
3160 	{ "socket_path", FORMAT_TABLE_STRING,
3161 	  format_cb_socket_path
3162 	},
3163 	{ "start_time", FORMAT_TABLE_TIME,
3164 	  format_cb_start_time
3165 	},
3166 	{ "tree_mode_format", FORMAT_TABLE_STRING,
3167 	  format_cb_tree_mode_format
3168 	},
3169 	{ "uid", FORMAT_TABLE_STRING,
3170 	  format_cb_uid
3171 	},
3172 	{ "user", FORMAT_TABLE_STRING,
3173 	  format_cb_user
3174 	},
3175 	{ "version", FORMAT_TABLE_STRING,
3176 	  format_cb_version
3177 	},
3178 	{ "window_active", FORMAT_TABLE_STRING,
3179 	  format_cb_window_active
3180 	},
3181 	{ "window_active_clients", FORMAT_TABLE_STRING,
3182 	  format_cb_window_active_clients
3183 	},
3184 	{ "window_active_clients_list", FORMAT_TABLE_STRING,
3185 	  format_cb_window_active_clients_list
3186 	},
3187 	{ "window_active_sessions", FORMAT_TABLE_STRING,
3188 	  format_cb_window_active_sessions
3189 	},
3190 	{ "window_active_sessions_list", FORMAT_TABLE_STRING,
3191 	  format_cb_window_active_sessions_list
3192 	},
3193 	{ "window_activity", FORMAT_TABLE_TIME,
3194 	  format_cb_window_activity
3195 	},
3196 	{ "window_activity_flag", FORMAT_TABLE_STRING,
3197 	  format_cb_window_activity_flag
3198 	},
3199 	{ "window_bell_flag", FORMAT_TABLE_STRING,
3200 	  format_cb_window_bell_flag
3201 	},
3202 	{ "window_bigger", FORMAT_TABLE_STRING,
3203 	  format_cb_window_bigger
3204 	},
3205 	{ "window_cell_height", FORMAT_TABLE_STRING,
3206 	  format_cb_window_cell_height
3207 	},
3208 	{ "window_cell_width", FORMAT_TABLE_STRING,
3209 	  format_cb_window_cell_width
3210 	},
3211 	{ "window_end_flag", FORMAT_TABLE_STRING,
3212 	  format_cb_window_end_flag
3213 	},
3214 	{ "window_flags", FORMAT_TABLE_STRING,
3215 	  format_cb_window_flags
3216 	},
3217 	{ "window_format", FORMAT_TABLE_STRING,
3218 	  format_cb_window_format
3219 	},
3220 	{ "window_height", FORMAT_TABLE_STRING,
3221 	  format_cb_window_height
3222 	},
3223 	{ "window_id", FORMAT_TABLE_STRING,
3224 	  format_cb_window_id
3225 	},
3226 	{ "window_index", FORMAT_TABLE_STRING,
3227 	  format_cb_window_index
3228 	},
3229 	{ "window_last_flag", FORMAT_TABLE_STRING,
3230 	  format_cb_window_last_flag
3231 	},
3232 	{ "window_layout", FORMAT_TABLE_STRING,
3233 	  format_cb_window_layout
3234 	},
3235 	{ "window_linked", FORMAT_TABLE_STRING,
3236 	  format_cb_window_linked
3237 	},
3238 	{ "window_linked_sessions", FORMAT_TABLE_STRING,
3239 	  format_cb_window_linked_sessions
3240 	},
3241 	{ "window_linked_sessions_list", FORMAT_TABLE_STRING,
3242 	  format_cb_window_linked_sessions_list
3243 	},
3244 	{ "window_marked_flag", FORMAT_TABLE_STRING,
3245 	  format_cb_window_marked_flag
3246 	},
3247 	{ "window_name", FORMAT_TABLE_STRING,
3248 	  format_cb_window_name
3249 	},
3250 	{ "window_offset_x", FORMAT_TABLE_STRING,
3251 	  format_cb_window_offset_x
3252 	},
3253 	{ "window_offset_y", FORMAT_TABLE_STRING,
3254 	  format_cb_window_offset_y
3255 	},
3256 	{ "window_panes", FORMAT_TABLE_STRING,
3257 	  format_cb_window_panes
3258 	},
3259 	{ "window_raw_flags", FORMAT_TABLE_STRING,
3260 	  format_cb_window_raw_flags
3261 	},
3262 	{ "window_silence_flag", FORMAT_TABLE_STRING,
3263 	  format_cb_window_silence_flag
3264 	},
3265 	{ "window_stack_index", FORMAT_TABLE_STRING,
3266 	  format_cb_window_stack_index
3267 	},
3268 	{ "window_start_flag", FORMAT_TABLE_STRING,
3269 	  format_cb_window_start_flag
3270 	},
3271 	{ "window_visible_layout", FORMAT_TABLE_STRING,
3272 	  format_cb_window_visible_layout
3273 	},
3274 	{ "window_width", FORMAT_TABLE_STRING,
3275 	  format_cb_window_width
3276 	},
3277 	{ "window_zoomed_flag", FORMAT_TABLE_STRING,
3278 	  format_cb_window_zoomed_flag
3279 	},
3280 	{ "wrap_flag", FORMAT_TABLE_STRING,
3281 	  format_cb_wrap_flag
3282 	}
3283 };
3284 
3285 /* Compare format table entries. */
3286 static int
format_table_compare(const void * key0,const void * entry0)3287 format_table_compare(const void *key0, const void *entry0)
3288 {
3289 	const char			*key = key0;
3290 	const struct format_table_entry	*entry = entry0;
3291 
3292 	return (strcmp(key, entry->key));
3293 }
3294 
3295 /* Get a format callback. */
3296 static struct format_table_entry *
format_table_get(const char * key)3297 format_table_get(const char *key)
3298 {
3299 	return (bsearch(key, format_table, nitems(format_table),
3300 	    sizeof *format_table, format_table_compare));
3301 }
3302 
3303 /* Merge one format tree into another. */
3304 void
format_merge(struct format_tree * ft,struct format_tree * from)3305 format_merge(struct format_tree *ft, struct format_tree *from)
3306 {
3307 	struct format_entry	*fe;
3308 
3309 	RB_FOREACH(fe, format_entry_tree, &from->tree) {
3310 		if (fe->value != NULL)
3311 			format_add(ft, fe->key, "%s", fe->value);
3312 	}
3313 }
3314 
3315 /* Get format pane. */
3316 struct window_pane *
format_get_pane(struct format_tree * ft)3317 format_get_pane(struct format_tree *ft)
3318 {
3319 	return (ft->wp);
3320 }
3321 
3322 /* Add item bits to tree. */
3323 static void
format_create_add_item(struct format_tree * ft,struct cmdq_item * item)3324 format_create_add_item(struct format_tree *ft, struct cmdq_item *item)
3325 {
3326 	struct key_event	*event = cmdq_get_event(item);
3327 	struct mouse_event	*m = &event->m;
3328 
3329 	cmdq_merge_formats(item, ft);
3330 	memcpy(&ft->m, m, sizeof ft->m);
3331 }
3332 
3333 /* Create a new tree. */
3334 struct format_tree *
format_create(struct client * c,struct cmdq_item * item,int tag,int flags)3335 format_create(struct client *c, struct cmdq_item *item, int tag, int flags)
3336 {
3337 	struct format_tree	*ft;
3338 
3339 	ft = xcalloc(1, sizeof *ft);
3340 	RB_INIT(&ft->tree);
3341 
3342 	if (c != NULL) {
3343 		ft->client = c;
3344 		ft->client->references++;
3345 	}
3346 	ft->item = item;
3347 
3348 	ft->tag = tag;
3349 	ft->flags = flags;
3350 
3351 	if (item != NULL)
3352 		format_create_add_item(ft, item);
3353 
3354 	return (ft);
3355 }
3356 
3357 /* Free a tree. */
3358 void
format_free(struct format_tree * ft)3359 format_free(struct format_tree *ft)
3360 {
3361 	struct format_entry	*fe, *fe1;
3362 
3363 	RB_FOREACH_SAFE(fe, format_entry_tree, &ft->tree, fe1) {
3364 		RB_REMOVE(format_entry_tree, &ft->tree, fe);
3365 		free(fe->value);
3366 		free(fe->key);
3367 		free(fe);
3368 	}
3369 
3370 	if (ft->client != NULL)
3371 		server_client_unref(ft->client);
3372 	free(ft);
3373 }
3374 
3375 /* Log each format. */
3376 static void
format_log_debug_cb(const char * key,const char * value,void * arg)3377 format_log_debug_cb(const char *key, const char *value, void *arg)
3378 {
3379 	const char	*prefix = arg;
3380 
3381 	log_debug("%s: %s=%s", prefix, key, value);
3382 }
3383 
3384 /* Log a format tree. */
3385 void
format_log_debug(struct format_tree * ft,const char * prefix)3386 format_log_debug(struct format_tree *ft, const char *prefix)
3387 {
3388 	format_each(ft, format_log_debug_cb, (void *)prefix);
3389 }
3390 
3391 /* Walk each format. */
3392 void
format_each(struct format_tree * ft,void (* cb)(const char *,const char *,void *),void * arg)3393 format_each(struct format_tree *ft, void (*cb)(const char *, const char *,
3394     void *), void *arg)
3395 {
3396 	const struct format_table_entry	*fte;
3397 	struct format_entry		*fe;
3398 	u_int				 i;
3399 	char				 s[64];
3400 	void				*value;
3401 	struct timeval			*tv;
3402 
3403 	for (i = 0; i < nitems(format_table); i++) {
3404 		fte = &format_table[i];
3405 
3406 		value = fte->cb(ft);
3407 		if (value == NULL)
3408 			continue;
3409 		if (fte->type == FORMAT_TABLE_TIME) {
3410 			tv = value;
3411 			xsnprintf(s, sizeof s, "%lld", (long long)tv->tv_sec);
3412 			cb(fte->key, s, arg);
3413 		} else {
3414 			cb(fte->key, value, arg);
3415 			free(value);
3416 		}
3417 	}
3418 	RB_FOREACH(fe, format_entry_tree, &ft->tree) {
3419 		if (fe->time != 0) {
3420 			xsnprintf(s, sizeof s, "%lld", (long long)fe->time);
3421 			cb(fe->key, s, arg);
3422 		} else {
3423 			if (fe->value == NULL && fe->cb != NULL) {
3424 				fe->value = fe->cb(ft);
3425 				if (fe->value == NULL)
3426 					fe->value = xstrdup("");
3427 			}
3428 			cb(fe->key, fe->value, arg);
3429 		}
3430 	}
3431 }
3432 
3433 /* Add a key-value pair. */
3434 void
format_add(struct format_tree * ft,const char * key,const char * fmt,...)3435 format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
3436 {
3437 	struct format_entry	*fe;
3438 	struct format_entry	*fe_now;
3439 	va_list			 ap;
3440 
3441 	fe = xmalloc(sizeof *fe);
3442 	fe->key = xstrdup(key);
3443 
3444 	fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
3445 	if (fe_now != NULL) {
3446 		free(fe->key);
3447 		free(fe);
3448 		free(fe_now->value);
3449 		fe = fe_now;
3450 	}
3451 
3452 	fe->cb = NULL;
3453 	fe->time = 0;
3454 
3455 	va_start(ap, fmt);
3456 	xvasprintf(&fe->value, fmt, ap);
3457 	va_end(ap);
3458 }
3459 
3460 /* Add a key and time. */
3461 void
format_add_tv(struct format_tree * ft,const char * key,struct timeval * tv)3462 format_add_tv(struct format_tree *ft, const char *key, struct timeval *tv)
3463 {
3464 	struct format_entry	*fe, *fe_now;
3465 
3466 	fe = xmalloc(sizeof *fe);
3467 	fe->key = xstrdup(key);
3468 
3469 	fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
3470 	if (fe_now != NULL) {
3471 		free(fe->key);
3472 		free(fe);
3473 		free(fe_now->value);
3474 		fe = fe_now;
3475 	}
3476 
3477 	fe->cb = NULL;
3478 	fe->time = tv->tv_sec;
3479 
3480 	fe->value = NULL;
3481 }
3482 
3483 /* Add a key and function. */
3484 void
format_add_cb(struct format_tree * ft,const char * key,format_cb cb)3485 format_add_cb(struct format_tree *ft, const char *key, format_cb cb)
3486 {
3487 	struct format_entry	*fe;
3488 	struct format_entry	*fe_now;
3489 
3490 	fe = xmalloc(sizeof *fe);
3491 	fe->key = xstrdup(key);
3492 
3493 	fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
3494 	if (fe_now != NULL) {
3495 		free(fe->key);
3496 		free(fe);
3497 		free(fe_now->value);
3498 		fe = fe_now;
3499 	}
3500 
3501 	fe->cb = cb;
3502 	fe->time = 0;
3503 
3504 	fe->value = NULL;
3505 }
3506 
3507 /* Quote shell special characters in string. */
3508 static char *
format_quote_shell(const char * s)3509 format_quote_shell(const char *s)
3510 {
3511 	const char	*cp;
3512 	char		*out, *at;
3513 
3514 	at = out = xmalloc(strlen(s) * 2 + 1);
3515 	for (cp = s; *cp != '\0'; cp++) {
3516 		if (strchr("|&;<>()$`\\\"'*?[# =%", *cp) != NULL)
3517 			*at++ = '\\';
3518 		*at++ = *cp;
3519 	}
3520 	*at = '\0';
3521 	return (out);
3522 }
3523 
3524 /* Quote #s in string. */
3525 static char *
format_quote_style(const char * s)3526 format_quote_style(const char *s)
3527 {
3528 	const char	*cp;
3529 	char		*out, *at;
3530 
3531 	at = out = xmalloc(strlen(s) * 2 + 1);
3532 	for (cp = s; *cp != '\0'; cp++) {
3533 		if (*cp == '#')
3534 			*at++ = '#';
3535 		*at++ = *cp;
3536 	}
3537 	*at = '\0';
3538 	return (out);
3539 }
3540 
3541 /* Make a prettier time. */
3542 char *
format_pretty_time(time_t t,int seconds)3543 format_pretty_time(time_t t, int seconds)
3544 {
3545 	struct tm       now_tm, tm;
3546 	time_t		now, age;
3547 	char		s[9];
3548 
3549 	time(&now);
3550 	if (now < t)
3551 		now = t;
3552 	age = now - t;
3553 
3554 	localtime_r(&now, &now_tm);
3555 	localtime_r(&t, &tm);
3556 
3557 	/* Last 24 hours. */
3558 	if (age < 24 * 3600) {
3559 		if (seconds)
3560 			strftime(s, sizeof s, "%H:%M:%S", &tm);
3561 		else
3562 			strftime(s, sizeof s, "%H:%M", &tm);
3563 		return (xstrdup(s));
3564 	}
3565 
3566 	/* This month or last 28 days. */
3567 	if ((tm.tm_year == now_tm.tm_year && tm.tm_mon == now_tm.tm_mon) ||
3568 	    age < 28 * 24 * 3600) {
3569 		strftime(s, sizeof s, "%a%d", &tm);
3570 		return (xstrdup(s));
3571 	}
3572 
3573 	/* Last 12 months. */
3574 	if ((tm.tm_year == now_tm.tm_year && tm.tm_mon < now_tm.tm_mon) ||
3575 	    (tm.tm_year == now_tm.tm_year - 1 && tm.tm_mon > now_tm.tm_mon)) {
3576 		strftime(s, sizeof s, "%d%b", &tm);
3577 		return (xstrdup(s));
3578 	}
3579 
3580 	/* Older than that. */
3581 	strftime(s, sizeof s, "%h%y", &tm);
3582 	return (xstrdup(s));
3583 }
3584 
3585 /* Find a format entry. */
3586 static char *
format_find(struct format_tree * ft,const char * key,int modifiers,const char * time_format)3587 format_find(struct format_tree *ft, const char *key, int modifiers,
3588     const char *time_format)
3589 {
3590 	struct format_table_entry	*fte;
3591 	void				*value;
3592 	struct format_entry		*fe, fe_find;
3593 	struct environ_entry		*envent;
3594 	struct options_entry		*o;
3595 	int				 idx;
3596 	char				*found = NULL, *saved, s[512];
3597 	const char			*errstr;
3598 	time_t				 t = 0;
3599 	struct tm			 tm;
3600 
3601 	o = options_parse_get(global_options, key, &idx, 0);
3602 	if (o == NULL && ft->wp != NULL)
3603 		o = options_parse_get(ft->wp->options, key, &idx, 0);
3604 	if (o == NULL && ft->w != NULL)
3605 		o = options_parse_get(ft->w->options, key, &idx, 0);
3606 	if (o == NULL)
3607 		o = options_parse_get(global_w_options, key, &idx, 0);
3608 	if (o == NULL && ft->s != NULL)
3609 		o = options_parse_get(ft->s->options, key, &idx, 0);
3610 	if (o == NULL)
3611 		o = options_parse_get(global_s_options, key, &idx, 0);
3612 	if (o != NULL) {
3613 		found = options_to_string(o, idx, 1);
3614 		goto found;
3615 	}
3616 
3617 	fte = format_table_get(key);
3618 	if (fte != NULL) {
3619 		value = fte->cb(ft);
3620 		if (fte->type == FORMAT_TABLE_TIME && value != NULL)
3621 			t = ((struct timeval *)value)->tv_sec;
3622 		else
3623 			found = value;
3624 		goto found;
3625 	}
3626 	fe_find.key = (char *)key;
3627 	fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find);
3628 	if (fe != NULL) {
3629 		if (fe->time != 0) {
3630 			t = fe->time;
3631 			goto found;
3632 		}
3633 		if (fe->value == NULL && fe->cb != NULL) {
3634 			fe->value = fe->cb(ft);
3635 			if (fe->value == NULL)
3636 				fe->value = xstrdup("");
3637 		}
3638 		found = xstrdup(fe->value);
3639 		goto found;
3640 	}
3641 
3642 	if (~modifiers & FORMAT_TIMESTRING) {
3643 		envent = NULL;
3644 		if (ft->s != NULL)
3645 			envent = environ_find(ft->s->environ, key);
3646 		if (envent == NULL)
3647 			envent = environ_find(global_environ, key);
3648 		if (envent != NULL && envent->value != NULL) {
3649 			found = xstrdup(envent->value);
3650 			goto found;
3651 		}
3652 	}
3653 
3654 	return (NULL);
3655 
3656 found:
3657 	if (modifiers & FORMAT_TIMESTRING) {
3658 		if (t == 0 && found != NULL) {
3659 			t = strtonum(found, 0, INT64_MAX, &errstr);
3660 			if (errstr != NULL)
3661 				t = 0;
3662 			free(found);
3663 		}
3664 		if (t == 0)
3665 			return (NULL);
3666 		if (modifiers & FORMAT_PRETTY)
3667 			found = format_pretty_time(t, 0);
3668 		else {
3669 			if (time_format != NULL) {
3670 				localtime_r(&t, &tm);
3671 				strftime(s, sizeof s, time_format, &tm);
3672 			} else {
3673 				ctime_r(&t, s);
3674 				s[strcspn(s, "\n")] = '\0';
3675 			}
3676 			found = xstrdup(s);
3677 		}
3678 		return (found);
3679 	}
3680 
3681 	if (t != 0)
3682 		xasprintf(&found, "%lld", (long long)t);
3683 	else if (found == NULL)
3684 		return (NULL);
3685 	if (modifiers & FORMAT_BASENAME) {
3686 		saved = found;
3687 		found = xstrdup(basename(saved));
3688 		free(saved);
3689 	}
3690 	if (modifiers & FORMAT_DIRNAME) {
3691 		saved = found;
3692 		found = xstrdup(dirname(saved));
3693 		free(saved);
3694 	}
3695 	if (modifiers & FORMAT_QUOTE_SHELL) {
3696 		saved = found;
3697 		found = format_quote_shell(saved);
3698 		free(saved);
3699 	}
3700 	if (modifiers & FORMAT_QUOTE_STYLE) {
3701 		saved = found;
3702 		found = format_quote_style(saved);
3703 		free(saved);
3704 	}
3705 	return (found);
3706 }
3707 
3708 /* Unescape escaped characters. */
3709 static char *
format_unescape(const char * s)3710 format_unescape(const char *s)
3711 {
3712 	char	*out, *cp;
3713 	int	 brackets = 0;
3714 
3715 	cp = out = xmalloc(strlen(s) + 1);
3716 	for (; *s != '\0'; s++) {
3717 		if (*s == '#' && s[1] == '{')
3718 			brackets++;
3719 		if (brackets == 0 &&
3720 		    *s == '#' &&
3721 		    strchr(",#{}:", s[1]) != NULL) {
3722 			*cp++ = *++s;
3723  			continue;
3724 		}
3725 		if (*s == '}')
3726 			brackets--;
3727 		*cp++ = *s;
3728 	}
3729 	*cp = '\0';
3730 	return (out);
3731 }
3732 
3733 /* Remove escaped characters. */
3734 static char *
format_strip(const char * s)3735 format_strip(const char *s)
3736 {
3737 	char	*out, *cp;
3738 	int	 brackets = 0;
3739 
3740 	cp = out = xmalloc(strlen(s) + 1);
3741 	for (; *s != '\0'; s++) {
3742 		if (*s == '#' && s[1] == '{')
3743 			brackets++;
3744 		if (*s == '#' && strchr(",#{}:", s[1]) != NULL) {
3745 			if (brackets != 0)
3746 				*cp++ = *s;
3747 			continue;
3748 		}
3749 		if (*s == '}')
3750 			brackets--;
3751 		*cp++ = *s;
3752 	}
3753 	*cp = '\0';
3754 	return (out);
3755 }
3756 
3757 /* Skip until end. */
3758 const char *
format_skip(const char * s,const char * end)3759 format_skip(const char *s, const char *end)
3760 {
3761 	int	brackets = 0;
3762 
3763 	for (; *s != '\0'; s++) {
3764 		if (*s == '#' && s[1] == '{')
3765 			brackets++;
3766 		if (*s == '#' &&
3767 		    s[1] != '\0' &&
3768 		    strchr(",#{}:", s[1]) != NULL) {
3769 			s++;
3770 			continue;
3771 		}
3772 		if (*s == '}')
3773 			brackets--;
3774 		if (strchr(end, *s) != NULL && brackets == 0)
3775 			break;
3776 	}
3777 	if (*s == '\0')
3778 		return (NULL);
3779 	return (s);
3780 }
3781 
3782 /* Return left and right alternatives separated by commas. */
3783 static int
format_choose(struct format_expand_state * es,const char * s,char ** left,char ** right,int expand)3784 format_choose(struct format_expand_state *es, const char *s, char **left,
3785     char **right, int expand)
3786 {
3787 	const char	*cp;
3788 	char		*left0, *right0;
3789 
3790 	cp = format_skip(s, ",");
3791 	if (cp == NULL)
3792 		return (-1);
3793 	left0 = xstrndup(s, cp - s);
3794 	right0 = xstrdup(cp + 1);
3795 
3796 	if (expand) {
3797 		*left = format_expand1(es, left0);
3798 		free(left0);
3799 		*right = format_expand1(es, right0);
3800 		free(right0);
3801 	} else {
3802 		*left = left0;
3803 		*right = right0;
3804 	}
3805 	return (0);
3806 }
3807 
3808 /* Is this true? */
3809 int
format_true(const char * s)3810 format_true(const char *s)
3811 {
3812 	if (s != NULL && *s != '\0' && (s[0] != '0' || s[1] != '\0'))
3813 		return (1);
3814 	return (0);
3815 }
3816 
3817 /* Check if modifier end. */
3818 static int
format_is_end(char c)3819 format_is_end(char c)
3820 {
3821 	return (c == ';' || c == ':');
3822 }
3823 
3824 /* Add to modifier list. */
3825 static void
format_add_modifier(struct format_modifier ** list,u_int * count,const char * c,size_t n,char ** argv,int argc)3826 format_add_modifier(struct format_modifier **list, u_int *count,
3827     const char *c, size_t n, char **argv, int argc)
3828 {
3829 	struct format_modifier *fm;
3830 
3831 	*list = xreallocarray(*list, (*count) + 1, sizeof **list);
3832 	fm = &(*list)[(*count)++];
3833 
3834 	memcpy(fm->modifier, c, n);
3835 	fm->modifier[n] = '\0';
3836 	fm->size = n;
3837 
3838 	fm->argv = argv;
3839 	fm->argc = argc;
3840 }
3841 
3842 /* Free modifier list. */
3843 static void
format_free_modifiers(struct format_modifier * list,u_int count)3844 format_free_modifiers(struct format_modifier *list, u_int count)
3845 {
3846 	u_int	i;
3847 
3848 	for (i = 0; i < count; i++)
3849 		cmd_free_argv(list[i].argc, list[i].argv);
3850 	free(list);
3851 }
3852 
3853 /* Build modifier list. */
3854 static struct format_modifier *
format_build_modifiers(struct format_expand_state * es,const char ** s,u_int * count)3855 format_build_modifiers(struct format_expand_state *es, const char **s,
3856     u_int *count)
3857 {
3858 	const char		*cp = *s, *end;
3859 	struct format_modifier	*list = NULL;
3860 	char			 c, last[] = "X;:", **argv, *value;
3861 	int			 argc;
3862 
3863 	/*
3864 	 * Modifiers are a ; separated list of the forms:
3865 	 *      l,m,C,a,b,c,d,n,t,w,q,E,T,S,W,P,<,>
3866 	 *	=a
3867 	 *	=/a
3868 	 *      =/a/
3869 	 *	s/a/b/
3870 	 *	s/a/b
3871 	 *	||,&&,!=,==,<=,>=
3872 	 */
3873 
3874 	*count = 0;
3875 
3876 	while (*cp != '\0' && *cp != ':') {
3877 		/* Skip any separator character. */
3878 		if (*cp == ';')
3879 			cp++;
3880 
3881 		/* Check single character modifiers with no arguments. */
3882 		if (strchr("labcdnwETSWPL<>", cp[0]) != NULL &&
3883 		    format_is_end(cp[1])) {
3884 			format_add_modifier(&list, count, cp, 1, NULL, 0);
3885 			cp++;
3886 			continue;
3887 		}
3888 
3889 		/* Then try double character with no arguments. */
3890 		if ((memcmp("||", cp, 2) == 0 ||
3891 		    memcmp("&&", cp, 2) == 0 ||
3892 		    memcmp("!=", cp, 2) == 0 ||
3893 		    memcmp("==", cp, 2) == 0 ||
3894 		    memcmp("<=", cp, 2) == 0 ||
3895 		    memcmp(">=", cp, 2) == 0) &&
3896 		    format_is_end(cp[2])) {
3897 			format_add_modifier(&list, count, cp, 2, NULL, 0);
3898 			cp += 2;
3899 			continue;
3900 		}
3901 
3902 		/* Now try single character with arguments. */
3903 		if (strchr("mCNst=peq", cp[0]) == NULL)
3904 			break;
3905 		c = cp[0];
3906 
3907 		/* No arguments provided. */
3908 		if (format_is_end(cp[1])) {
3909 			format_add_modifier(&list, count, cp, 1, NULL, 0);
3910 			cp++;
3911 			continue;
3912 		}
3913 		argv = NULL;
3914 		argc = 0;
3915 
3916 		/* Single argument with no wrapper character. */
3917 		if (!ispunct((u_char)cp[1]) || cp[1] == '-') {
3918 			end = format_skip(cp + 1, ":;");
3919 			if (end == NULL)
3920 				break;
3921 
3922 			argv = xcalloc(1, sizeof *argv);
3923 			value = xstrndup(cp + 1, end - (cp + 1));
3924 			argv[0] = format_expand1(es, value);
3925 			free(value);
3926 			argc = 1;
3927 
3928 			format_add_modifier(&list, count, &c, 1, argv, argc);
3929 			cp = end;
3930 			continue;
3931 		}
3932 
3933 		/* Multiple arguments with a wrapper character. */
3934 		last[0] = cp[1];
3935 		cp++;
3936 		do {
3937 			if (cp[0] == last[0] && format_is_end(cp[1])) {
3938 				cp++;
3939 				break;
3940 			}
3941 			end = format_skip(cp + 1, last);
3942 			if (end == NULL)
3943 				break;
3944 			cp++;
3945 
3946 			argv = xreallocarray(argv, argc + 1, sizeof *argv);
3947 			value = xstrndup(cp, end - cp);
3948 			argv[argc++] = format_expand1(es, value);
3949 			free(value);
3950 
3951 			cp = end;
3952 		} while (!format_is_end(cp[0]));
3953 		format_add_modifier(&list, count, &c, 1, argv, argc);
3954 	}
3955 	if (*cp != ':') {
3956 		format_free_modifiers(list, *count);
3957 		*count = 0;
3958 		return (NULL);
3959 	}
3960 	*s = cp + 1;
3961 	return (list);
3962 }
3963 
3964 /* Match against an fnmatch(3) pattern or regular expression. */
3965 static char *
format_match(struct format_modifier * fm,const char * pattern,const char * text)3966 format_match(struct format_modifier *fm, const char *pattern, const char *text)
3967 {
3968 	const char	*s = "";
3969 	regex_t		 r;
3970 	int		 flags = 0;
3971 
3972 	if (fm->argc >= 1)
3973 		s = fm->argv[0];
3974 	if (strchr(s, 'r') == NULL) {
3975 		if (strchr(s, 'i') != NULL)
3976 			flags |= FNM_CASEFOLD;
3977 		if (fnmatch(pattern, text, flags) != 0)
3978 			return (xstrdup("0"));
3979 	} else {
3980 		flags = REG_EXTENDED|REG_NOSUB;
3981 		if (strchr(s, 'i') != NULL)
3982 			flags |= REG_ICASE;
3983 		if (regcomp(&r, pattern, flags) != 0)
3984 			return (xstrdup("0"));
3985 		if (regexec(&r, text, 0, NULL, 0) != 0) {
3986 			regfree(&r);
3987 			return (xstrdup("0"));
3988 		}
3989 		regfree(&r);
3990 	}
3991 	return (xstrdup("1"));
3992 }
3993 
3994 /* Perform substitution in string. */
3995 static char *
format_sub(struct format_modifier * fm,const char * text,const char * pattern,const char * with)3996 format_sub(struct format_modifier *fm, const char *text, const char *pattern,
3997     const char *with)
3998 {
3999 	char	*value;
4000 	int	 flags = REG_EXTENDED;
4001 
4002 	if (fm->argc >= 3 && strchr(fm->argv[2], 'i') != NULL)
4003 		flags |= REG_ICASE;
4004 	value = regsub(pattern, with, text, flags);
4005 	if (value == NULL)
4006 		return (xstrdup(text));
4007 	return (value);
4008 }
4009 
4010 /* Search inside pane. */
4011 static char *
format_search(struct format_modifier * fm,struct window_pane * wp,const char * s)4012 format_search(struct format_modifier *fm, struct window_pane *wp, const char *s)
4013 {
4014 	int	 ignore = 0, regex = 0;
4015 	char	*value;
4016 
4017 	if (fm->argc >= 1) {
4018 		if (strchr(fm->argv[0], 'i') != NULL)
4019 			ignore = 1;
4020 		if (strchr(fm->argv[0], 'r') != NULL)
4021 			regex = 1;
4022 	}
4023 	xasprintf(&value, "%u", window_pane_search(wp, s, regex, ignore));
4024 	return (value);
4025 }
4026 
4027 /* Does session name exist? */
4028 static char *
format_session_name(struct format_expand_state * es,const char * fmt)4029 format_session_name(struct format_expand_state *es, const char *fmt)
4030 {
4031 	char		*name;
4032 	struct session	*s;
4033 
4034 	name = format_expand1(es, fmt);
4035 	RB_FOREACH(s, sessions, &sessions) {
4036 		if (strcmp(s->name, name) == 0) {
4037 			free(name);
4038 			return (xstrdup("1"));
4039 		}
4040 	}
4041 	free(name);
4042 	return (xstrdup("0"));
4043 }
4044 
4045 /* Loop over sessions. */
4046 static char *
format_loop_sessions(struct format_expand_state * es,const char * fmt)4047 format_loop_sessions(struct format_expand_state *es, const char *fmt)
4048 {
4049 	struct format_tree		*ft = es->ft;
4050 	struct client			*c = ft->client;
4051 	struct cmdq_item		*item = ft->item;
4052 	struct format_tree		*nft;
4053 	struct format_expand_state	 next;
4054 	char				*expanded, *value;
4055 	size_t				 valuelen;
4056 	struct session			*s;
4057 
4058 	value = xcalloc(1, 1);
4059 	valuelen = 1;
4060 
4061 	RB_FOREACH(s, sessions, &sessions) {
4062 		format_log(es, "session loop: $%u", s->id);
4063 		nft = format_create(c, item, FORMAT_NONE, ft->flags);
4064 		format_defaults(nft, ft->c, s, NULL, NULL);
4065 		format_copy_state(&next, es, 0);
4066 		next.ft = nft;
4067 		expanded = format_expand1(&next, fmt);
4068 		format_free(next.ft);
4069 
4070 		valuelen += strlen(expanded);
4071 		value = xrealloc(value, valuelen);
4072 
4073 		strlcat(value, expanded, valuelen);
4074 		free(expanded);
4075 	}
4076 
4077 	return (value);
4078 }
4079 
4080 /* Does window name exist? */
4081 static char *
format_window_name(struct format_expand_state * es,const char * fmt)4082 format_window_name(struct format_expand_state *es, const char *fmt)
4083 {
4084 	struct format_tree	*ft = es->ft;
4085 	char			*name;
4086 	struct winlink		*wl;
4087 
4088 	if (ft->s == NULL) {
4089 		format_log(es, "window name but no session");
4090 		return (NULL);
4091 	}
4092 
4093 	name = format_expand1(es, fmt);
4094 	RB_FOREACH(wl, winlinks, &ft->s->windows) {
4095 		if (strcmp(wl->window->name, name) == 0) {
4096 			free(name);
4097 			return (xstrdup("1"));
4098 		}
4099 	}
4100 	free(name);
4101 	return (xstrdup("0"));
4102 }
4103 
4104 /* Loop over windows. */
4105 static char *
format_loop_windows(struct format_expand_state * es,const char * fmt)4106 format_loop_windows(struct format_expand_state *es, const char *fmt)
4107 {
4108 	struct format_tree		*ft = es->ft;
4109 	struct client			*c = ft->client;
4110 	struct cmdq_item		*item = ft->item;
4111 	struct format_tree		*nft;
4112 	struct format_expand_state	 next;
4113 	char				*all, *active, *use, *expanded, *value;
4114 	size_t				 valuelen;
4115 	struct winlink			*wl;
4116 	struct window			*w;
4117 
4118 	if (ft->s == NULL) {
4119 		format_log(es, "window loop but no session");
4120 		return (NULL);
4121 	}
4122 
4123 	if (format_choose(es, fmt, &all, &active, 0) != 0) {
4124 		all = xstrdup(fmt);
4125 		active = NULL;
4126 	}
4127 
4128 	value = xcalloc(1, 1);
4129 	valuelen = 1;
4130 
4131 	RB_FOREACH(wl, winlinks, &ft->s->windows) {
4132 		w = wl->window;
4133 		format_log(es, "window loop: %u @%u", wl->idx, w->id);
4134 		if (active != NULL && wl == ft->s->curw)
4135 			use = active;
4136 		else
4137 			use = all;
4138 		nft = format_create(c, item, FORMAT_WINDOW|w->id, ft->flags);
4139 		format_defaults(nft, ft->c, ft->s, wl, NULL);
4140 		format_copy_state(&next, es, 0);
4141 		next.ft = nft;
4142 		expanded = format_expand1(&next, use);
4143 		format_free(nft);
4144 
4145 		valuelen += strlen(expanded);
4146 		value = xrealloc(value, valuelen);
4147 
4148 		strlcat(value, expanded, valuelen);
4149 		free(expanded);
4150 	}
4151 
4152 	free(active);
4153 	free(all);
4154 
4155 	return (value);
4156 }
4157 
4158 /* Loop over panes. */
4159 static char *
format_loop_panes(struct format_expand_state * es,const char * fmt)4160 format_loop_panes(struct format_expand_state *es, const char *fmt)
4161 {
4162 	struct format_tree		*ft = es->ft;
4163 	struct client			*c = ft->client;
4164 	struct cmdq_item		*item = ft->item;
4165 	struct format_tree		*nft;
4166 	struct format_expand_state	 next;
4167 	char				*all, *active, *use, *expanded, *value;
4168 	size_t				 valuelen;
4169 	struct window_pane		*wp;
4170 
4171 	if (ft->w == NULL) {
4172 		format_log(es, "pane loop but no window");
4173 		return (NULL);
4174 	}
4175 
4176 	if (format_choose(es, fmt, &all, &active, 0) != 0) {
4177 		all = xstrdup(fmt);
4178 		active = NULL;
4179 	}
4180 
4181 	value = xcalloc(1, 1);
4182 	valuelen = 1;
4183 
4184 	TAILQ_FOREACH(wp, &ft->w->panes, entry) {
4185 		format_log(es, "pane loop: %%%u", wp->id);
4186 		if (active != NULL && wp == ft->w->active)
4187 			use = active;
4188 		else
4189 			use = all;
4190 		nft = format_create(c, item, FORMAT_PANE|wp->id, ft->flags);
4191 		format_defaults(nft, ft->c, ft->s, ft->wl, wp);
4192 		format_copy_state(&next, es, 0);
4193 		next.ft = nft;
4194 		expanded = format_expand1(&next, use);
4195 		format_free(nft);
4196 
4197 		valuelen += strlen(expanded);
4198 		value = xrealloc(value, valuelen);
4199 
4200 		strlcat(value, expanded, valuelen);
4201 		free(expanded);
4202 	}
4203 
4204 	free(active);
4205 	free(all);
4206 
4207 	return (value);
4208 }
4209 
4210 /* Loop over clients. */
4211 static char *
format_loop_clients(struct format_expand_state * es,const char * fmt)4212 format_loop_clients(struct format_expand_state *es, const char *fmt)
4213 {
4214 	struct format_tree		*ft = es->ft;
4215 	struct client			*c;
4216 	struct cmdq_item		*item = ft->item;
4217 	struct format_tree		*nft;
4218 	struct format_expand_state	 next;
4219 	char				*expanded, *value;
4220 	size_t				 valuelen;
4221 
4222 	value = xcalloc(1, 1);
4223 	valuelen = 1;
4224 
4225 	TAILQ_FOREACH(c, &clients, entry) {
4226 		format_log(es, "client loop: %s", c->name);
4227 		nft = format_create(c, item, 0, ft->flags);
4228 		format_defaults(nft, c, ft->s, ft->wl, ft->wp);
4229 		format_copy_state(&next, es, 0);
4230 		next.ft = nft;
4231 		expanded = format_expand1(&next, fmt);
4232 		format_free(nft);
4233 
4234 		valuelen += strlen(expanded);
4235 		value = xrealloc(value, valuelen);
4236 
4237 		strlcat(value, expanded, valuelen);
4238 		free(expanded);
4239 	}
4240 
4241 	return (value);
4242 }
4243 
4244 static char *
format_replace_expression(struct format_modifier * mexp,struct format_expand_state * es,const char * copy)4245 format_replace_expression(struct format_modifier *mexp,
4246     struct format_expand_state *es, const char *copy)
4247 {
4248 	int			 argc = mexp->argc;
4249 	const char		*errstr;
4250 	char			*endch, *value, *left = NULL, *right = NULL;
4251 	int			 use_fp = 0;
4252 	u_int			 prec = 0;
4253 	double			 mleft, mright, result;
4254 	enum { ADD,
4255 	       SUBTRACT,
4256 	       MULTIPLY,
4257 	       DIVIDE,
4258 	       MODULUS,
4259 	       EQUAL,
4260 	       NOT_EQUAL,
4261 	       GREATER_THAN,
4262 	       GREATER_THAN_EQUAL,
4263 	       LESS_THAN,
4264 	       LESS_THAN_EQUAL } operator;
4265 
4266 	if (strcmp(mexp->argv[0], "+") == 0)
4267 		operator = ADD;
4268 	else if (strcmp(mexp->argv[0], "-") == 0)
4269 		operator = SUBTRACT;
4270 	else if (strcmp(mexp->argv[0], "*") == 0)
4271 		operator = MULTIPLY;
4272 	else if (strcmp(mexp->argv[0], "/") == 0)
4273 		operator = DIVIDE;
4274 	else if (strcmp(mexp->argv[0], "%") == 0 ||
4275 	    strcmp(mexp->argv[0], "m") == 0)
4276 		operator = MODULUS;
4277 	else if (strcmp(mexp->argv[0], "==") == 0)
4278 		operator = EQUAL;
4279 	else if (strcmp(mexp->argv[0], "!=") == 0)
4280 		operator = NOT_EQUAL;
4281 	else if (strcmp(mexp->argv[0], ">") == 0)
4282 		operator = GREATER_THAN;
4283 	else if (strcmp(mexp->argv[0], "<") == 0)
4284 		operator = LESS_THAN;
4285 	else if (strcmp(mexp->argv[0], ">=") == 0)
4286 		operator = GREATER_THAN_EQUAL;
4287 	else if (strcmp(mexp->argv[0], "<=") == 0)
4288 		operator = LESS_THAN_EQUAL;
4289 	else {
4290 		format_log(es, "expression has no valid operator: '%s'",
4291 		    mexp->argv[0]);
4292 		goto fail;
4293 	}
4294 
4295 	/* The second argument may be flags. */
4296 	if (argc >= 2 && strchr(mexp->argv[1], 'f') != NULL) {
4297 		use_fp = 1;
4298 		prec = 2;
4299 	}
4300 
4301 	/* The third argument may be precision. */
4302 	if (argc >= 3) {
4303 		prec = strtonum(mexp->argv[2], INT_MIN, INT_MAX, &errstr);
4304 		if (errstr != NULL) {
4305 			format_log(es, "expression precision %s: %s", errstr,
4306 			    mexp->argv[2]);
4307 			goto fail;
4308 		}
4309 	}
4310 
4311 	if (format_choose(es, copy, &left, &right, 1) != 0) {
4312 		format_log(es, "expression syntax error");
4313 		goto fail;
4314 	}
4315 
4316 	mleft = strtod(left, &endch);
4317 	if (*endch != '\0') {
4318 		format_log(es, "expression left side is invalid: %s", left);
4319 		goto fail;
4320 	}
4321 
4322 	mright = strtod(right, &endch);
4323 	if (*endch != '\0') {
4324 		format_log(es, "expression right side is invalid: %s", right);
4325 		goto fail;
4326 	}
4327 
4328 	if (!use_fp) {
4329 		mleft = (long long)mleft;
4330 		mright = (long long)mright;
4331 	}
4332 	format_log(es, "expression left side is: %.*f", prec, mleft);
4333 	format_log(es, "expression right side is: %.*f", prec, mright);
4334 
4335 	switch (operator) {
4336 	case ADD:
4337 		result = mleft + mright;
4338 		break;
4339 	case SUBTRACT:
4340 		result = mleft - mright;
4341 		break;
4342 	case MULTIPLY:
4343 		result = mleft * mright;
4344 		break;
4345 	case DIVIDE:
4346 		result = mleft / mright;
4347 		break;
4348 	case MODULUS:
4349 		result = fmod(mleft, mright);
4350 		break;
4351 	case EQUAL:
4352 		result = fabs(mleft - mright) < 1e-9;
4353 		break;
4354 	case NOT_EQUAL:
4355 		result = fabs(mleft - mright) > 1e-9;
4356 		break;
4357 	case GREATER_THAN:
4358 		result = (mleft > mright);
4359 		break;
4360 	case GREATER_THAN_EQUAL:
4361 		result = (mleft >= mright);
4362 		break;
4363 	case LESS_THAN:
4364 		result = (mleft < mright);
4365 		break;
4366 	case LESS_THAN_EQUAL:
4367 		result = (mleft <= mright);
4368 		break;
4369 	}
4370 	if (use_fp)
4371 		xasprintf(&value, "%.*f", prec, result);
4372 	else
4373 		xasprintf(&value, "%.*f", prec, (double)(long long)result);
4374 	format_log(es, "expression result is %s", value);
4375 
4376 	free(right);
4377 	free(left);
4378 	return (value);
4379 
4380 fail:
4381 	free(right);
4382 	free(left);
4383 	return (NULL);
4384 }
4385 
4386 /* Replace a key. */
4387 static int
format_replace(struct format_expand_state * es,const char * key,size_t keylen,char ** buf,size_t * len,size_t * off)4388 format_replace(struct format_expand_state *es, const char *key, size_t keylen,
4389     char **buf, size_t *len, size_t *off)
4390 {
4391 	struct format_tree		 *ft = es->ft;
4392 	struct window_pane		 *wp = ft->wp;
4393 	const char			 *errstr, *copy, *cp, *marker = NULL;
4394 	const char			 *time_format = NULL;
4395 	char				 *copy0, *condition, *found, *new;
4396 	char				 *value, *left, *right;
4397 	size_t				  valuelen;
4398 	int				  modifiers = 0, limit = 0, width = 0;
4399 	int				  j, c;
4400 	struct format_modifier		 *list, *cmp = NULL, *search = NULL;
4401 	struct format_modifier		**sub = NULL, *mexp = NULL, *fm;
4402 	u_int				  i, count, nsub = 0;
4403 	struct format_expand_state	  next;
4404 
4405 	/* Make a copy of the key. */
4406 	copy = copy0 = xstrndup(key, keylen);
4407 
4408 	/* Process modifier list. */
4409 	list = format_build_modifiers(es, &copy, &count);
4410 	for (i = 0; i < count; i++) {
4411 		fm = &list[i];
4412 		if (format_logging(ft)) {
4413 			format_log(es, "modifier %u is %s", i, fm->modifier);
4414 			for (j = 0; j < fm->argc; j++) {
4415 				format_log(es, "modifier %u argument %d: %s", i,
4416 				    j, fm->argv[j]);
4417 			}
4418 		}
4419 		if (fm->size == 1) {
4420 			switch (fm->modifier[0]) {
4421 			case 'm':
4422 			case '<':
4423 			case '>':
4424 				cmp = fm;
4425 				break;
4426 			case 'C':
4427 				search = fm;
4428 				break;
4429 			case 's':
4430 				if (fm->argc < 2)
4431 					break;
4432 				sub = xreallocarray(sub, nsub + 1, sizeof *sub);
4433 				sub[nsub++] = fm;
4434 				break;
4435 			case '=':
4436 				if (fm->argc < 1)
4437 					break;
4438 				limit = strtonum(fm->argv[0], INT_MIN, INT_MAX,
4439 				    &errstr);
4440 				if (errstr != NULL)
4441 					limit = 0;
4442 				if (fm->argc >= 2 && fm->argv[1] != NULL)
4443 					marker = fm->argv[1];
4444 				break;
4445 			case 'p':
4446 				if (fm->argc < 1)
4447 					break;
4448 				width = strtonum(fm->argv[0], INT_MIN, INT_MAX,
4449 				    &errstr);
4450 				if (errstr != NULL)
4451 					width = 0;
4452 				break;
4453 			case 'w':
4454 				modifiers |= FORMAT_WIDTH;
4455 				break;
4456 			case 'e':
4457 				if (fm->argc < 1 || fm->argc > 3)
4458 					break;
4459 				mexp = fm;
4460 				break;
4461 			case 'l':
4462 				modifiers |= FORMAT_LITERAL;
4463 				break;
4464 			case 'a':
4465 				modifiers |= FORMAT_CHARACTER;
4466 				break;
4467 			case 'b':
4468 				modifiers |= FORMAT_BASENAME;
4469 				break;
4470 			case 'c':
4471 				modifiers |= FORMAT_COLOUR;
4472 				break;
4473 			case 'd':
4474 				modifiers |= FORMAT_DIRNAME;
4475 				break;
4476 			case 'n':
4477 				modifiers |= FORMAT_LENGTH;
4478 				break;
4479 			case 't':
4480 				modifiers |= FORMAT_TIMESTRING;
4481 				if (fm->argc < 1)
4482 					break;
4483 				if (strchr(fm->argv[0], 'p') != NULL)
4484 					modifiers |= FORMAT_PRETTY;
4485 				else if (fm->argc >= 2 &&
4486 				    strchr(fm->argv[0], 'f') != NULL)
4487 					time_format = format_strip(fm->argv[1]);
4488 				break;
4489 			case 'q':
4490 				if (fm->argc < 1)
4491 					modifiers |= FORMAT_QUOTE_SHELL;
4492 				else if (strchr(fm->argv[0], 'e') != NULL ||
4493 				    strchr(fm->argv[0], 'h') != NULL)
4494 					modifiers |= FORMAT_QUOTE_STYLE;
4495 				break;
4496 			case 'E':
4497 				modifiers |= FORMAT_EXPAND;
4498 				break;
4499 			case 'T':
4500 				modifiers |= FORMAT_EXPANDTIME;
4501 				break;
4502 			case 'N':
4503 				if (fm->argc < 1 ||
4504 				    strchr(fm->argv[0], 'w') != NULL)
4505 					modifiers |= FORMAT_WINDOW_NAME;
4506 				else if (strchr(fm->argv[0], 's') != NULL)
4507 					modifiers |= FORMAT_SESSION_NAME;
4508 				break;
4509 			case 'S':
4510 				modifiers |= FORMAT_SESSIONS;
4511 				break;
4512 			case 'W':
4513 				modifiers |= FORMAT_WINDOWS;
4514 				break;
4515 			case 'P':
4516 				modifiers |= FORMAT_PANES;
4517 				break;
4518 			case 'L':
4519 				modifiers |= FORMAT_CLIENTS;
4520 				break;
4521 			}
4522 		} else if (fm->size == 2) {
4523 			if (strcmp(fm->modifier, "||") == 0 ||
4524 			    strcmp(fm->modifier, "&&") == 0 ||
4525 			    strcmp(fm->modifier, "==") == 0 ||
4526 			    strcmp(fm->modifier, "!=") == 0 ||
4527 			    strcmp(fm->modifier, ">=") == 0 ||
4528 			    strcmp(fm->modifier, "<=") == 0)
4529 				cmp = fm;
4530 		}
4531 	}
4532 
4533 	/* Is this a literal string? */
4534 	if (modifiers & FORMAT_LITERAL) {
4535 		format_log(es, "literal string is '%s'", copy);
4536 		value = format_unescape(copy);
4537 		goto done;
4538 	}
4539 
4540 	/* Is this a character? */
4541 	if (modifiers & FORMAT_CHARACTER) {
4542 		new = format_expand1(es, copy);
4543 		c = strtonum(new, 32, 126, &errstr);
4544 		if (errstr != NULL)
4545 			value = xstrdup("");
4546 		else
4547 			xasprintf(&value, "%c", c);
4548 		free(new);
4549 		goto done;
4550 	}
4551 
4552 	/* Is this a colour? */
4553 	if (modifiers & FORMAT_COLOUR) {
4554 		new = format_expand1(es, copy);
4555 		c = colour_fromstring(new);
4556 		if (c == -1 || (c = colour_force_rgb(c)) == -1)
4557 			value = xstrdup("");
4558 		else
4559 			xasprintf(&value, "%06x", c & 0xffffff);
4560 		free(new);
4561 		goto done;
4562 	}
4563 
4564 	/* Is this a loop, comparison or condition? */
4565 	if (modifiers & FORMAT_SESSIONS) {
4566 		value = format_loop_sessions(es, copy);
4567 		if (value == NULL)
4568 			goto fail;
4569 	} else if (modifiers & FORMAT_WINDOWS) {
4570 		value = format_loop_windows(es, copy);
4571 		if (value == NULL)
4572 			goto fail;
4573 	} else if (modifiers & FORMAT_PANES) {
4574 		value = format_loop_panes(es, copy);
4575 		if (value == NULL)
4576 			goto fail;
4577 	} else if (modifiers & FORMAT_CLIENTS) {
4578 		value = format_loop_clients(es, copy);
4579 		if (value == NULL)
4580 			goto fail;
4581 	} else if (modifiers & FORMAT_WINDOW_NAME) {
4582 		value = format_window_name(es, copy);
4583 		if (value == NULL)
4584 			goto fail;
4585 	} else if (modifiers & FORMAT_SESSION_NAME) {
4586 		value = format_session_name(es, copy);
4587 		if (value == NULL)
4588 			goto fail;
4589 	} else if (search != NULL) {
4590 		/* Search in pane. */
4591 		new = format_expand1(es, copy);
4592 		if (wp == NULL) {
4593 			format_log(es, "search '%s' but no pane", new);
4594 			value = xstrdup("0");
4595 		} else {
4596 			format_log(es, "search '%s' pane %%%u", new, wp->id);
4597 			value = format_search(search, wp, new);
4598 		}
4599 		free(new);
4600 	} else if (cmp != NULL) {
4601 		/* Comparison of left and right. */
4602 		if (format_choose(es, copy, &left, &right, 1) != 0) {
4603 			format_log(es, "compare %s syntax error: %s",
4604 			    cmp->modifier, copy);
4605 			goto fail;
4606 		}
4607 		format_log(es, "compare %s left is: %s", cmp->modifier, left);
4608 		format_log(es, "compare %s right is: %s", cmp->modifier, right);
4609 
4610 		if (strcmp(cmp->modifier, "||") == 0) {
4611 			if (format_true(left) || format_true(right))
4612 				value = xstrdup("1");
4613 			else
4614 				value = xstrdup("0");
4615 		} else if (strcmp(cmp->modifier, "&&") == 0) {
4616 			if (format_true(left) && format_true(right))
4617 				value = xstrdup("1");
4618 			else
4619 				value = xstrdup("0");
4620 		} else if (strcmp(cmp->modifier, "==") == 0) {
4621 			if (strcmp(left, right) == 0)
4622 				value = xstrdup("1");
4623 			else
4624 				value = xstrdup("0");
4625 		} else if (strcmp(cmp->modifier, "!=") == 0) {
4626 			if (strcmp(left, right) != 0)
4627 				value = xstrdup("1");
4628 			else
4629 				value = xstrdup("0");
4630 		} else if (strcmp(cmp->modifier, "<") == 0) {
4631 			if (strcmp(left, right) < 0)
4632 				value = xstrdup("1");
4633 			else
4634 				value = xstrdup("0");
4635 		} else if (strcmp(cmp->modifier, ">") == 0) {
4636 			if (strcmp(left, right) > 0)
4637 				value = xstrdup("1");
4638 			else
4639 				value = xstrdup("0");
4640 		} else if (strcmp(cmp->modifier, "<=") == 0) {
4641 			if (strcmp(left, right) <= 0)
4642 				value = xstrdup("1");
4643 			else
4644 				value = xstrdup("0");
4645 		} else if (strcmp(cmp->modifier, ">=") == 0) {
4646 			if (strcmp(left, right) >= 0)
4647 				value = xstrdup("1");
4648 			else
4649 				value = xstrdup("0");
4650 		} else if (strcmp(cmp->modifier, "m") == 0)
4651 			value = format_match(cmp, left, right);
4652 
4653 		free(right);
4654 		free(left);
4655 	} else if (*copy == '?') {
4656 		/* Conditional: check first and choose second or third. */
4657 		cp = format_skip(copy + 1, ",");
4658 		if (cp == NULL) {
4659 			format_log(es, "condition syntax error: %s", copy + 1);
4660 			goto fail;
4661 		}
4662 		condition = xstrndup(copy + 1, cp - (copy + 1));
4663 		format_log(es, "condition is: %s", condition);
4664 
4665 		found = format_find(ft, condition, modifiers, time_format);
4666 		if (found == NULL) {
4667 			/*
4668 			 * If the condition not found, try to expand it. If
4669 			 * the expansion doesn't have any effect, then assume
4670 			 * false.
4671 			 */
4672 			found = format_expand1(es, condition);
4673 			if (strcmp(found, condition) == 0) {
4674 				free(found);
4675 				found = xstrdup("");
4676 				format_log(es,
4677 				    "condition '%s' not found; assuming false",
4678 				    condition);
4679 			}
4680 		} else {
4681 			format_log(es, "condition '%s' found: %s", condition,
4682 			    found);
4683 		}
4684 
4685 		if (format_choose(es, cp + 1, &left, &right, 0) != 0) {
4686 			format_log(es, "condition '%s' syntax error: %s",
4687 			    condition, cp + 1);
4688 			free(found);
4689 			goto fail;
4690 		}
4691 		if (format_true(found)) {
4692 			format_log(es, "condition '%s' is true", condition);
4693 			value = format_expand1(es, left);
4694 		} else {
4695 			format_log(es, "condition '%s' is false", condition);
4696 			value = format_expand1(es, right);
4697 		}
4698 		free(right);
4699 		free(left);
4700 
4701 		free(condition);
4702 		free(found);
4703 	} else if (mexp != NULL) {
4704 		value = format_replace_expression(mexp, es, copy);
4705 		if (value == NULL)
4706 			value = xstrdup("");
4707 	} else {
4708 		if (strstr(copy, "#{") != 0) {
4709 			format_log(es, "expanding inner format '%s'", copy);
4710 			value = format_expand1(es, copy);
4711 		} else {
4712 			value = format_find(ft, copy, modifiers, time_format);
4713 			if (value == NULL) {
4714 				format_log(es, "format '%s' not found", copy);
4715 				value = xstrdup("");
4716 			} else {
4717 				format_log(es, "format '%s' found: %s", copy,
4718 				    value);
4719 			}
4720 		}
4721 	}
4722 
4723 done:
4724 	/* Expand again if required. */
4725 	if (modifiers & FORMAT_EXPAND) {
4726 		new = format_expand1(es, value);
4727 		free(value);
4728 		value = new;
4729 	} else if (modifiers & FORMAT_EXPANDTIME) {
4730 		format_copy_state(&next, es, FORMAT_EXPAND_TIME);
4731 		new = format_expand1(&next, value);
4732 		free(value);
4733 		value = new;
4734 	}
4735 
4736 	/* Perform substitution if any. */
4737 	for (i = 0; i < nsub; i++) {
4738 		left = format_expand1(es, sub[i]->argv[0]);
4739 		right = format_expand1(es, sub[i]->argv[1]);
4740 		new = format_sub(sub[i], value, left, right);
4741 		format_log(es, "substitute '%s' to '%s': %s", left, right, new);
4742 		free(value);
4743 		value = new;
4744 		free(right);
4745 		free(left);
4746 	}
4747 
4748 	/* Truncate the value if needed. */
4749 	if (limit > 0) {
4750 		new = format_trim_left(value, limit);
4751 		if (marker != NULL && strcmp(new, value) != 0) {
4752 			free(value);
4753 			xasprintf(&value, "%s%s", new, marker);
4754 		} else {
4755 			free(value);
4756 			value = new;
4757 		}
4758 		format_log(es, "applied length limit %d: %s", limit, value);
4759 	} else if (limit < 0) {
4760 		new = format_trim_right(value, -limit);
4761 		if (marker != NULL && strcmp(new, value) != 0) {
4762 			free(value);
4763 			xasprintf(&value, "%s%s", marker, new);
4764 		} else {
4765 			free(value);
4766 			value = new;
4767 		}
4768 		format_log(es, "applied length limit %d: %s", limit, value);
4769 	}
4770 
4771 	/* Pad the value if needed. */
4772 	if (width > 0) {
4773 		new = utf8_padcstr(value, width);
4774 		free(value);
4775 		value = new;
4776 		format_log(es, "applied padding width %d: %s", width, value);
4777 	} else if (width < 0) {
4778 		new = utf8_rpadcstr(value, -width);
4779 		free(value);
4780 		value = new;
4781 		format_log(es, "applied padding width %d: %s", width, value);
4782 	}
4783 
4784 	/* Replace with the length or width if needed. */
4785 	if (modifiers & FORMAT_LENGTH) {
4786 		xasprintf(&new, "%zu", strlen(value));
4787 		free(value);
4788 		value = new;
4789 		format_log(es, "replacing with length: %s", new);
4790 	}
4791 	if (modifiers & FORMAT_WIDTH) {
4792 		xasprintf(&new, "%u", format_width(value));
4793 		free(value);
4794 		value = new;
4795 		format_log(es, "replacing with width: %s", new);
4796 	}
4797 
4798 	/* Expand the buffer and copy in the value. */
4799 	valuelen = strlen(value);
4800 	while (*len - *off < valuelen + 1) {
4801 		*buf = xreallocarray(*buf, 2, *len);
4802 		*len *= 2;
4803 	}
4804 	memcpy(*buf + *off, value, valuelen);
4805 	*off += valuelen;
4806 
4807 	format_log(es, "replaced '%s' with '%s'", copy0, value);
4808 	free(value);
4809 
4810 	free(sub);
4811 	format_free_modifiers(list, count);
4812 	free(copy0);
4813 	return (0);
4814 
4815 fail:
4816 	format_log(es, "failed %s", copy0);
4817 
4818 	free(sub);
4819 	format_free_modifiers(list, count);
4820 	free(copy0);
4821 	return (-1);
4822 }
4823 
4824 /* Expand keys in a template. */
4825 static char *
format_expand1(struct format_expand_state * es,const char * fmt)4826 format_expand1(struct format_expand_state *es, const char *fmt)
4827 {
4828 	struct format_tree	*ft = es->ft;
4829 	char			*buf, *out, *name;
4830 	const char		*ptr, *s, *style_end = NULL;
4831 	size_t			 off, len, n, outlen;
4832 	int     		 ch, brackets;
4833 	char			 expanded[8192];
4834 
4835 	if (fmt == NULL || *fmt == '\0')
4836 		return (xstrdup(""));
4837 
4838 	if (es->loop == FORMAT_LOOP_LIMIT) {
4839 		format_log(es, "reached loop limit (%u)", FORMAT_LOOP_LIMIT);
4840 		return (xstrdup(""));
4841 	}
4842 	es->loop++;
4843 
4844 	format_log(es, "expanding format: %s", fmt);
4845 
4846 	if ((es->flags & FORMAT_EXPAND_TIME) && strchr(fmt, '%') != NULL) {
4847 		if (es->time == 0) {
4848 			es->time = time(NULL);
4849 			localtime_r(&es->time, &es->tm);
4850 		}
4851 		if (strftime(expanded, sizeof expanded, fmt, &es->tm) == 0) {
4852 			format_log(es, "format is too long");
4853 			return (xstrdup(""));
4854 		}
4855 		if (format_logging(ft) && strcmp(expanded, fmt) != 0)
4856 			format_log(es, "after time expanded: %s", expanded);
4857 		fmt = expanded;
4858 	}
4859 
4860 	len = 64;
4861 	buf = xmalloc(len);
4862 	off = 0;
4863 
4864 	while (*fmt != '\0') {
4865 		if (*fmt != '#') {
4866 			while (len - off < 2) {
4867 				buf = xreallocarray(buf, 2, len);
4868 				len *= 2;
4869 			}
4870 			buf[off++] = *fmt++;
4871 			continue;
4872 		}
4873 		fmt++;
4874 
4875 		ch = (u_char)*fmt++;
4876 		switch (ch) {
4877 		case '(':
4878 			brackets = 1;
4879 			for (ptr = fmt; *ptr != '\0'; ptr++) {
4880 				if (*ptr == '(')
4881 					brackets++;
4882 				if (*ptr == ')' && --brackets == 0)
4883 					break;
4884 			}
4885 			if (*ptr != ')' || brackets != 0)
4886 				break;
4887 			n = ptr - fmt;
4888 
4889 			name = xstrndup(fmt, n);
4890 			format_log(es, "found #(): %s", name);
4891 
4892 			if ((ft->flags & FORMAT_NOJOBS) ||
4893 			    (es->flags & FORMAT_EXPAND_NOJOBS)) {
4894 				out = xstrdup("");
4895 				format_log(es, "#() is disabled");
4896 			} else {
4897 				out = format_job_get(es, name);
4898 				format_log(es, "#() result: %s", out);
4899 			}
4900 			free(name);
4901 
4902 			outlen = strlen(out);
4903 			while (len - off < outlen + 1) {
4904 				buf = xreallocarray(buf, 2, len);
4905 				len *= 2;
4906 			}
4907 			memcpy(buf + off, out, outlen);
4908 			off += outlen;
4909 
4910 			free(out);
4911 
4912 			fmt += n + 1;
4913 			continue;
4914 		case '{':
4915 			ptr = format_skip((char *)fmt - 2, "}");
4916 			if (ptr == NULL)
4917 				break;
4918 			n = ptr - fmt;
4919 
4920 			format_log(es, "found #{}: %.*s", (int)n, fmt);
4921 			if (format_replace(es, fmt, n, &buf, &len, &off) != 0)
4922 				break;
4923 			fmt += n + 1;
4924 			continue;
4925 		case '[':
4926 		case '#':
4927 			/*
4928 			 * If ##[ (with two or more #s), then it is a style and
4929 			 * can be left for format_draw to handle.
4930 			 */
4931 			ptr = fmt - (ch == '[');
4932 			n = 2 - (ch == '[');
4933 			while (*ptr == '#') {
4934 				ptr++;
4935 				n++;
4936 			}
4937 			if (*ptr == '[') {
4938 				style_end = format_skip(fmt - 2, "]");
4939 				format_log(es, "found #*%zu[", n);
4940 				while (len - off < n + 2) {
4941 					buf = xreallocarray(buf, 2, len);
4942 					len *= 2;
4943 				}
4944 				memcpy(buf + off, fmt - 2, n + 1);
4945 				off += n + 1;
4946 				fmt = ptr + 1;
4947 				continue;
4948 			}
4949 			/* FALLTHROUGH */
4950 		case '}':
4951 		case ',':
4952 			format_log(es, "found #%c", ch);
4953 			while (len - off < 2) {
4954 				buf = xreallocarray(buf, 2, len);
4955 				len *= 2;
4956 			}
4957 			buf[off++] = ch;
4958 			continue;
4959 		default:
4960 			s = NULL;
4961 			if (fmt > style_end) { /* skip inside #[] */
4962 				if (ch >= 'A' && ch <= 'Z')
4963 					s = format_upper[ch - 'A'];
4964 				else if (ch >= 'a' && ch <= 'z')
4965 					s = format_lower[ch - 'a'];
4966 			}
4967 			if (s == NULL) {
4968 				while (len - off < 3) {
4969 					buf = xreallocarray(buf, 2, len);
4970 					len *= 2;
4971 				}
4972 				buf[off++] = '#';
4973 				buf[off++] = ch;
4974 				continue;
4975 			}
4976 			n = strlen(s);
4977 			format_log(es, "found #%c: %s", ch, s);
4978 			if (format_replace(es, s, n, &buf, &len, &off) != 0)
4979 				break;
4980 			continue;
4981 		}
4982 
4983 		break;
4984 	}
4985 	buf[off] = '\0';
4986 
4987 	format_log(es, "result is: %s", buf);
4988 	es->loop--;
4989 
4990 	return (buf);
4991 }
4992 
4993 /* Expand keys in a template, passing through strftime first. */
4994 char *
format_expand_time(struct format_tree * ft,const char * fmt)4995 format_expand_time(struct format_tree *ft, const char *fmt)
4996 {
4997 	struct format_expand_state	es;
4998 
4999 	memset(&es, 0, sizeof es);
5000 	es.ft = ft;
5001 	es.flags = FORMAT_EXPAND_TIME;
5002 	return (format_expand1(&es, fmt));
5003 }
5004 
5005 /* Expand keys in a template. */
5006 char *
format_expand(struct format_tree * ft,const char * fmt)5007 format_expand(struct format_tree *ft, const char *fmt)
5008 {
5009 	struct format_expand_state	es;
5010 
5011 	memset(&es, 0, sizeof es);
5012 	es.ft = ft;
5013 	es.flags = 0;
5014 	return (format_expand1(&es, fmt));
5015 }
5016 
5017 /* Expand a single string. */
5018 char *
format_single(struct cmdq_item * item,const char * fmt,struct client * c,struct session * s,struct winlink * wl,struct window_pane * wp)5019 format_single(struct cmdq_item *item, const char *fmt, struct client *c,
5020     struct session *s, struct winlink *wl, struct window_pane *wp)
5021 {
5022 	struct format_tree	*ft;
5023 	char			*expanded;
5024 
5025 	ft = format_create_defaults(item, c, s, wl, wp);
5026 	expanded = format_expand(ft, fmt);
5027 	format_free(ft);
5028 	return (expanded);
5029 }
5030 
5031 /* Expand a single string using state. */
5032 char *
format_single_from_state(struct cmdq_item * item,const char * fmt,struct client * c,struct cmd_find_state * fs)5033 format_single_from_state(struct cmdq_item *item, const char *fmt,
5034     struct client *c, struct cmd_find_state *fs)
5035 {
5036 	return (format_single(item, fmt, c, fs->s, fs->wl, fs->wp));
5037 }
5038 
5039 /* Expand a single string using target. */
5040 char *
format_single_from_target(struct cmdq_item * item,const char * fmt)5041 format_single_from_target(struct cmdq_item *item, const char *fmt)
5042 {
5043 	struct client	*tc = cmdq_get_target_client(item);
5044 
5045 	return (format_single_from_state(item, fmt, tc, cmdq_get_target(item)));
5046 }
5047 
5048 /* Create and add defaults. */
5049 struct format_tree *
format_create_defaults(struct cmdq_item * item,struct client * c,struct session * s,struct winlink * wl,struct window_pane * wp)5050 format_create_defaults(struct cmdq_item *item, struct client *c,
5051     struct session *s, struct winlink *wl, struct window_pane *wp)
5052 {
5053 	struct format_tree	*ft;
5054 
5055 	if (item != NULL)
5056 		ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0);
5057 	else
5058 		ft = format_create(NULL, item, FORMAT_NONE, 0);
5059 	format_defaults(ft, c, s, wl, wp);
5060 	return (ft);
5061 }
5062 
5063 /* Create and add defaults using state. */
5064 struct format_tree *
format_create_from_state(struct cmdq_item * item,struct client * c,struct cmd_find_state * fs)5065 format_create_from_state(struct cmdq_item *item, struct client *c,
5066     struct cmd_find_state *fs)
5067 {
5068 	return (format_create_defaults(item, c, fs->s, fs->wl, fs->wp));
5069 }
5070 
5071 /* Create and add defaults using target. */
5072 struct format_tree *
format_create_from_target(struct cmdq_item * item)5073 format_create_from_target(struct cmdq_item *item)
5074 {
5075 	struct client	*tc = cmdq_get_target_client(item);
5076 
5077 	return (format_create_from_state(item, tc, cmdq_get_target(item)));
5078 }
5079 
5080 /* Set defaults for any of arguments that are not NULL. */
5081 void
format_defaults(struct format_tree * ft,struct client * c,struct session * s,struct winlink * wl,struct window_pane * wp)5082 format_defaults(struct format_tree *ft, struct client *c, struct session *s,
5083     struct winlink *wl, struct window_pane *wp)
5084 {
5085 	struct paste_buffer	*pb;
5086 
5087 	if (c != NULL && c->name != NULL)
5088 		log_debug("%s: c=%s", __func__, c->name);
5089 	else
5090 		log_debug("%s: c=none", __func__);
5091 	if (s != NULL)
5092 		log_debug("%s: s=$%u", __func__, s->id);
5093 	else
5094 		log_debug("%s: s=none", __func__);
5095 	if (wl != NULL)
5096 		log_debug("%s: wl=%u", __func__, wl->idx);
5097 	else
5098 		log_debug("%s: wl=none", __func__);
5099 	if (wp != NULL)
5100 		log_debug("%s: wp=%%%u", __func__, wp->id);
5101 	else
5102 		log_debug("%s: wp=none", __func__);
5103 
5104 	if (c != NULL && s != NULL && c->session != s)
5105 		log_debug("%s: session does not match", __func__);
5106 
5107 	if (wp != NULL)
5108 		ft->type = FORMAT_TYPE_PANE;
5109 	else if (wl != NULL)
5110 		ft->type = FORMAT_TYPE_WINDOW;
5111 	else if (s != NULL)
5112 		ft->type = FORMAT_TYPE_SESSION;
5113 	else
5114 		ft->type = FORMAT_TYPE_UNKNOWN;
5115 
5116 	if (s == NULL && c != NULL)
5117 		s = c->session;
5118 	if (wl == NULL && s != NULL)
5119 		wl = s->curw;
5120 	if (wp == NULL && wl != NULL)
5121 		wp = wl->window->active;
5122 
5123 	if (c != NULL)
5124 		format_defaults_client(ft, c);
5125 	if (s != NULL)
5126 		format_defaults_session(ft, s);
5127 	if (wl != NULL)
5128 		format_defaults_winlink(ft, wl);
5129 	if (wp != NULL)
5130 		format_defaults_pane(ft, wp);
5131 
5132 	pb = paste_get_top(NULL);
5133 	if (pb != NULL)
5134 		format_defaults_paste_buffer(ft, pb);
5135 }
5136 
5137 /* Set default format keys for a session. */
5138 static void
format_defaults_session(struct format_tree * ft,struct session * s)5139 format_defaults_session(struct format_tree *ft, struct session *s)
5140 {
5141 	ft->s = s;
5142 }
5143 
5144 /* Set default format keys for a client. */
5145 static void
format_defaults_client(struct format_tree * ft,struct client * c)5146 format_defaults_client(struct format_tree *ft, struct client *c)
5147 {
5148 	if (ft->s == NULL)
5149 		ft->s = c->session;
5150 	ft->c = c;
5151 }
5152 
5153 /* Set default format keys for a window. */
5154 void
format_defaults_window(struct format_tree * ft,struct window * w)5155 format_defaults_window(struct format_tree *ft, struct window *w)
5156 {
5157 	ft->w = w;
5158 }
5159 
5160 /* Set default format keys for a winlink. */
5161 static void
format_defaults_winlink(struct format_tree * ft,struct winlink * wl)5162 format_defaults_winlink(struct format_tree *ft, struct winlink *wl)
5163 {
5164 	if (ft->w == NULL)
5165 		format_defaults_window(ft, wl->window);
5166 	ft->wl = wl;
5167 }
5168 
5169 /* Set default format keys for a window pane. */
5170 void
format_defaults_pane(struct format_tree * ft,struct window_pane * wp)5171 format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
5172 {
5173 	struct window_mode_entry	*wme;
5174 
5175 	if (ft->w == NULL)
5176 		format_defaults_window(ft, wp->window);
5177 	ft->wp = wp;
5178 
5179 	wme = TAILQ_FIRST(&wp->modes);
5180 	if (wme != NULL && wme->mode->formats != NULL)
5181 		wme->mode->formats(wme, ft);
5182 }
5183 
5184 /* Set default format keys for paste buffer. */
5185 void
format_defaults_paste_buffer(struct format_tree * ft,struct paste_buffer * pb)5186 format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
5187 {
5188 	ft->pb = pb;
5189 }
5190 
5191 static int
format_is_word_separator(const char * ws,const struct grid_cell * gc)5192 format_is_word_separator(const char *ws, const struct grid_cell *gc)
5193 {
5194 	if (utf8_cstrhas(ws, &gc->data))
5195 		return (1);
5196 	if (gc->flags & GRID_FLAG_TAB)
5197 		return (1);
5198 	return gc->data.size == 1 && *gc->data.data == ' ';
5199 }
5200 
5201 /* Return word at given coordinates. Caller frees. */
5202 char *
format_grid_word(struct grid * gd,u_int x,u_int y)5203 format_grid_word(struct grid *gd, u_int x, u_int y)
5204 {
5205 	const struct grid_line	*gl;
5206 	struct grid_cell	 gc;
5207 	const char		*ws;
5208 	struct utf8_data	*ud = NULL;
5209 	u_int			 end;
5210 	size_t			 size = 0;
5211 	int			 found = 0;
5212 	char			*s = NULL;
5213 
5214 	ws = options_get_string(global_s_options, "word-separators");
5215 
5216 	for (;;) {
5217 		grid_get_cell(gd, x, y, &gc);
5218 		if (gc.flags & GRID_FLAG_PADDING)
5219 			break;
5220 		if (format_is_word_separator(ws, &gc)) {
5221 			found = 1;
5222 			break;
5223 		}
5224 
5225 		if (x == 0) {
5226 			if (y == 0)
5227 				break;
5228 			gl = grid_peek_line(gd, y - 1);
5229 			if (~gl->flags & GRID_LINE_WRAPPED)
5230 				break;
5231 			y--;
5232 			x = grid_line_length(gd, y);
5233 			if (x == 0)
5234 				break;
5235 		}
5236 		x--;
5237 	}
5238 	for (;;) {
5239 		if (found) {
5240 			end = grid_line_length(gd, y);
5241 			if (end == 0 || x == end - 1) {
5242 				if (y == gd->hsize + gd->sy - 1)
5243 					break;
5244 				gl = grid_peek_line(gd, y);
5245 				if (~gl->flags & GRID_LINE_WRAPPED)
5246 					break;
5247 				y++;
5248 				x = 0;
5249 			} else
5250 				x++;
5251 		}
5252 		found = 1;
5253 
5254 		grid_get_cell(gd, x, y, &gc);
5255 		if (gc.flags & GRID_FLAG_PADDING)
5256 			break;
5257 		if (format_is_word_separator(ws, &gc))
5258 			break;
5259 
5260 		ud = xreallocarray(ud, size + 2, sizeof *ud);
5261 		memcpy(&ud[size++], &gc.data, sizeof *ud);
5262 	}
5263 	if (size != 0) {
5264 		ud[size].size = 0;
5265 		s = utf8_tocstr(ud);
5266 		free(ud);
5267 	}
5268 	return (s);
5269 }
5270 
5271 /* Return line at given coordinates. Caller frees. */
5272 char *
format_grid_line(struct grid * gd,u_int y)5273 format_grid_line(struct grid *gd, u_int y)
5274 {
5275 	struct grid_cell	 gc;
5276 	struct utf8_data	*ud = NULL;
5277 	u_int			 x;
5278 	size_t			 size = 0;
5279 	char			*s = NULL;
5280 
5281 	for (x = 0; x < grid_line_length(gd, y); x++) {
5282 		grid_get_cell(gd, x, y, &gc);
5283 		if (gc.flags & GRID_FLAG_PADDING)
5284 			continue;
5285 
5286 		ud = xreallocarray(ud, size + 2, sizeof *ud);
5287 		if (gc.flags & GRID_FLAG_TAB)
5288 			utf8_set(&ud[size++], '\t');
5289 		else
5290 			memcpy(&ud[size++], &gc.data, sizeof *ud);
5291 	}
5292 	if (size != 0) {
5293 		ud[size].size = 0;
5294 		s = utf8_tocstr(ud);
5295 		free(ud);
5296 	}
5297 	return (s);
5298 }
5299 
5300 /* Return hyperlink at given coordinates. Caller frees. */
5301 char *
format_grid_hyperlink(struct grid * gd,u_int x,u_int y,struct screen * s)5302 format_grid_hyperlink(struct grid *gd, u_int x, u_int y, struct screen* s)
5303 {
5304 	const char		*uri;
5305 	struct grid_cell	 gc;
5306 
5307 	grid_get_cell(gd, x, y, &gc);
5308 	if (gc.flags & GRID_FLAG_PADDING)
5309 		return (NULL);
5310 	if (s->hyperlinks == NULL || gc.link == 0)
5311 		return (NULL);
5312 	if (!hyperlinks_get(s->hyperlinks, gc.link, &uri, NULL, NULL))
5313 		return (NULL);
5314 	return (xstrdup(uri));
5315 }
5316