2011-08-26 10:53:16 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2011 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2011-08-26 10:53:16 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2014-12-09 19:23:35 +00:00
|
|
|
#include <sys/wait.h>
|
2011-08-26 10:53:16 +00:00
|
|
|
|
2018-06-27 17:36:10 +00:00
|
|
|
#include <ctype.h>
|
2013-10-10 11:50:20 +00:00
|
|
|
#include <errno.h>
|
2017-05-29 15:43:48 +00:00
|
|
|
#include <fnmatch.h>
|
2015-10-25 22:29:17 +00:00
|
|
|
#include <libgen.h>
|
2020-03-11 14:17:55 +00:00
|
|
|
#include <math.h>
|
2022-03-08 11:28:40 +00:00
|
|
|
#include <pwd.h>
|
2019-06-13 19:46:00 +00:00
|
|
|
#include <regex.h>
|
2011-08-26 10:53:16 +00:00
|
|
|
#include <stdarg.h>
|
2012-07-10 11:53:01 +00:00
|
|
|
#include <stdlib.h>
|
2011-08-26 10:53:16 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Build a list of key-value pairs and use them to expand #{key} entries in a
|
|
|
|
* string.
|
|
|
|
*/
|
|
|
|
|
2020-10-06 07:36:05 +00:00
|
|
|
struct format_expand_state;
|
2016-10-10 21:29:23 +00:00
|
|
|
|
2020-10-06 07:36:05 +00:00
|
|
|
static char *format_job_get(struct format_expand_state *, const char *);
|
|
|
|
static char *format_expand1(struct format_expand_state *, const char *);
|
|
|
|
static int format_replace(struct format_expand_state *, const char *,
|
|
|
|
size_t, char **, size_t *, size_t *);
|
2016-10-10 21:29:23 +00:00
|
|
|
static void format_defaults_session(struct format_tree *,
|
|
|
|
struct session *);
|
|
|
|
static void format_defaults_client(struct format_tree *, struct client *);
|
2020-10-06 07:36:05 +00:00
|
|
|
static void format_defaults_winlink(struct format_tree *,
|
|
|
|
struct winlink *);
|
2011-08-26 10:53:16 +00:00
|
|
|
|
2015-05-27 13:28:04 +00:00
|
|
|
/* Entry in format job tree. */
|
|
|
|
struct format_job {
|
2017-05-01 12:20:55 +00:00
|
|
|
struct client *client;
|
2017-02-03 11:57:27 +00:00
|
|
|
u_int tag;
|
2015-05-27 13:28:04 +00:00
|
|
|
const char *cmd;
|
2016-11-17 10:06:08 +00:00
|
|
|
const char *expanded;
|
2015-05-27 13:28:04 +00:00
|
|
|
|
|
|
|
time_t last;
|
|
|
|
char *out;
|
2017-04-20 09:20:22 +00:00
|
|
|
int updated;
|
2015-05-27 13:28:04 +00:00
|
|
|
|
|
|
|
struct job *job;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
RB_ENTRY(format_job) entry;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Format job tree. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static int format_job_cmp(struct format_job *, struct format_job *);
|
2016-10-11 13:45:47 +00:00
|
|
|
static RB_HEAD(format_job_tree, format_job) format_jobs = RB_INITIALIZER();
|
2016-10-10 21:29:23 +00:00
|
|
|
RB_GENERATE_STATIC(format_job_tree, format_job, entry, format_job_cmp);
|
2015-05-27 13:28:04 +00:00
|
|
|
|
|
|
|
/* Format job tree comparison function. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static int
|
2015-05-27 13:28:04 +00:00
|
|
|
format_job_cmp(struct format_job *fj1, struct format_job *fj2)
|
|
|
|
{
|
2017-02-03 11:57:27 +00:00
|
|
|
if (fj1->tag < fj2->tag)
|
|
|
|
return (-1);
|
|
|
|
if (fj1->tag > fj2->tag)
|
|
|
|
return (1);
|
2015-05-27 13:28:04 +00:00
|
|
|
return (strcmp(fj1->cmd, fj2->cmd));
|
|
|
|
}
|
|
|
|
|
2015-10-25 22:29:17 +00:00
|
|
|
/* Format modifiers. */
|
|
|
|
#define FORMAT_TIMESTRING 0x1
|
|
|
|
#define FORMAT_BASENAME 0x2
|
|
|
|
#define FORMAT_DIRNAME 0x4
|
2021-01-20 07:16:54 +00:00
|
|
|
#define FORMAT_QUOTE_SHELL 0x8
|
2019-03-13 14:10:34 +00:00
|
|
|
#define FORMAT_LITERAL 0x10
|
2019-03-13 14:19:54 +00:00
|
|
|
#define FORMAT_EXPAND 0x20
|
2019-03-14 21:31:43 +00:00
|
|
|
#define FORMAT_EXPANDTIME 0x40
|
|
|
|
#define FORMAT_SESSIONS 0x80
|
|
|
|
#define FORMAT_WINDOWS 0x100
|
|
|
|
#define FORMAT_PANES 0x200
|
2020-05-16 14:10:29 +00:00
|
|
|
#define FORMAT_PRETTY 0x400
|
2020-08-20 16:57:40 +00:00
|
|
|
#define FORMAT_LENGTH 0x800
|
2020-11-09 09:10:10 +00:00
|
|
|
#define FORMAT_WIDTH 0x1000
|
2021-01-20 07:16:54 +00:00
|
|
|
#define FORMAT_QUOTE_STYLE 0x2000
|
2021-02-05 12:23:49 +00:00
|
|
|
#define FORMAT_WINDOW_NAME 0x4000
|
|
|
|
#define FORMAT_SESSION_NAME 0x8000
|
2021-04-12 06:50:25 +00:00
|
|
|
#define FORMAT_CHARACTER 0x10000
|
2021-10-25 21:21:16 +00:00
|
|
|
#define FORMAT_COLOUR 0x20000
|
2023-02-07 10:21:01 +00:00
|
|
|
#define FORMAT_CLIENTS 0x40000
|
2015-10-25 22:29:17 +00:00
|
|
|
|
2019-03-14 23:34:41 +00:00
|
|
|
/* Limit on recursion. */
|
2021-06-10 07:58:08 +00:00
|
|
|
#define FORMAT_LOOP_LIMIT 100
|
2019-03-14 23:34:41 +00:00
|
|
|
|
2020-10-06 07:36:05 +00:00
|
|
|
/* Format expand flags. */
|
|
|
|
#define FORMAT_EXPAND_TIME 0x1
|
|
|
|
#define FORMAT_EXPAND_NOJOBS 0x2
|
|
|
|
|
2014-12-02 23:19:45 +00:00
|
|
|
/* Entry in format tree. */
|
|
|
|
struct format_entry {
|
2015-08-28 16:46:40 +00:00
|
|
|
char *key;
|
|
|
|
char *value;
|
2020-10-06 07:36:05 +00:00
|
|
|
time_t time;
|
2015-08-28 16:46:40 +00:00
|
|
|
format_cb cb;
|
|
|
|
RB_ENTRY(format_entry) entry;
|
2014-12-02 23:19:45 +00:00
|
|
|
};
|
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
/* Format type. */
|
|
|
|
enum format_type {
|
|
|
|
FORMAT_TYPE_UNKNOWN,
|
|
|
|
FORMAT_TYPE_SESSION,
|
|
|
|
FORMAT_TYPE_WINDOW,
|
|
|
|
FORMAT_TYPE_PANE
|
|
|
|
};
|
|
|
|
|
2014-12-02 23:19:45 +00:00
|
|
|
struct format_tree {
|
2021-02-22 07:09:06 +00:00
|
|
|
enum format_type type;
|
|
|
|
|
Support for windows larger than visible on the attached client. This has
been a limitation for a long time.
There are two new options, window-size and default-size, and a new
command, resize-window. The force-width and force-height options and the
session_width and session_height formats have been removed.
The new window-size option tells tmux how to work out the size of
windows: largest means it picks the size of the largest session,
smallest the smallest session (similar to the old behaviour) and manual
means that it does not automatically resize windows. The default is
currently largest but this may change. aggressive-resize modifies the
choice of session for largest and smallest as it did before.
If a window is in a session attached to a client that is too small, only
part of the window is shown. tmux attempts to keep the cursor visible,
so the part of the window displayed is changed as the cursor moves (with
a small delay, to try and avoid excess redrawing when applications
redraw status lines or similar that are not currently visible). The
offset of the visible portion of the window is shown in status-right.
Drawing windows which are larger than the client is not as efficient as
those which fit, particularly when the cursor moves, so it is
recommended to avoid using this on slow machines or networks (set
window-size to smallest or manual).
The resize-window command can be used to resize a window manually. If it
is used, the window-size option is automatically set to manual for the
window (undo this with "setw -u window-size"). resize-window works in a
similar way to resize-pane (-U -D -L -R -x -y flags) but also has -a and
-A flags. -a sets the window to the size of the smallest client (what it
would be if window-size was smallest) and -A the largest.
For the same behaviour as force-width or force-height, use resize-window
-x or -y, and "setw -u window-size" to revert to automatic sizing..
If the global window-size option is set to manual, the default-size
option is used for new windows. If -x or -y is used with new-session,
that sets the default-size option for the new session.
The maximum size of a window is 10000x10000. But expect applications to
complain and much higher memory use if making a window excessively
big. The minimum size is the size required for the current layout
including borders.
The refresh-client command can be used to pan around a window, -U -D -L
-R moves up, down, left or right and -c returns to automatic cursor
tracking. The position is reset when the current window is changed.
2018-10-18 08:38:01 +00:00
|
|
|
struct client *c;
|
2015-08-28 16:46:40 +00:00
|
|
|
struct session *s;
|
Support for windows larger than visible on the attached client. This has
been a limitation for a long time.
There are two new options, window-size and default-size, and a new
command, resize-window. The force-width and force-height options and the
session_width and session_height formats have been removed.
The new window-size option tells tmux how to work out the size of
windows: largest means it picks the size of the largest session,
smallest the smallest session (similar to the old behaviour) and manual
means that it does not automatically resize windows. The default is
currently largest but this may change. aggressive-resize modifies the
choice of session for largest and smallest as it did before.
If a window is in a session attached to a client that is too small, only
part of the window is shown. tmux attempts to keep the cursor visible,
so the part of the window displayed is changed as the cursor moves (with
a small delay, to try and avoid excess redrawing when applications
redraw status lines or similar that are not currently visible). The
offset of the visible portion of the window is shown in status-right.
Drawing windows which are larger than the client is not as efficient as
those which fit, particularly when the cursor moves, so it is
recommended to avoid using this on slow machines or networks (set
window-size to smallest or manual).
The resize-window command can be used to resize a window manually. If it
is used, the window-size option is automatically set to manual for the
window (undo this with "setw -u window-size"). resize-window works in a
similar way to resize-pane (-U -D -L -R -x -y flags) but also has -a and
-A flags. -a sets the window to the size of the smallest client (what it
would be if window-size was smallest) and -A the largest.
For the same behaviour as force-width or force-height, use resize-window
-x or -y, and "setw -u window-size" to revert to automatic sizing..
If the global window-size option is set to manual, the default-size
option is used for new windows. If -x or -y is used with new-session,
that sets the default-size option for the new session.
The maximum size of a window is 10000x10000. But expect applications to
complain and much higher memory use if making a window excessively
big. The minimum size is the size required for the current layout
including borders.
The refresh-client command can be used to pan around a window, -U -D -L
-R moves up, down, left or right and -c returns to automatic cursor
tracking. The position is reset when the current window is changed.
2018-10-18 08:38:01 +00:00
|
|
|
struct winlink *wl;
|
|
|
|
struct window *w;
|
2015-08-28 16:46:40 +00:00
|
|
|
struct window_pane *wp;
|
2021-02-22 07:09:06 +00:00
|
|
|
struct paste_buffer *pb;
|
2014-12-02 23:19:45 +00:00
|
|
|
|
2019-03-13 15:37:28 +00:00
|
|
|
struct cmdq_item *item;
|
2017-05-01 12:20:55 +00:00
|
|
|
struct client *client;
|
2015-09-14 10:25:52 +00:00
|
|
|
int flags;
|
2020-05-16 15:48:35 +00:00
|
|
|
u_int tag;
|
2014-12-02 23:19:45 +00:00
|
|
|
|
2019-05-26 17:34:45 +00:00
|
|
|
struct mouse_event m;
|
|
|
|
|
2015-05-27 13:28:04 +00:00
|
|
|
RB_HEAD(format_entry_tree, format_entry) tree;
|
|
|
|
};
|
2016-10-10 21:29:23 +00:00
|
|
|
static int format_entry_cmp(struct format_entry *, struct format_entry *);
|
|
|
|
RB_GENERATE_STATIC(format_entry_tree, format_entry, entry, format_entry_cmp);
|
2011-08-26 10:53:16 +00:00
|
|
|
|
2020-10-06 07:36:05 +00:00
|
|
|
/* Format expand state. */
|
|
|
|
struct format_expand_state {
|
|
|
|
struct format_tree *ft;
|
|
|
|
u_int loop;
|
|
|
|
time_t time;
|
2021-02-22 07:09:06 +00:00
|
|
|
struct tm tm;
|
2020-10-06 07:36:05 +00:00
|
|
|
int flags;
|
|
|
|
};
|
|
|
|
|
2019-03-15 10:07:24 +00:00
|
|
|
/* Format modifier. */
|
2019-03-13 14:10:34 +00:00
|
|
|
struct format_modifier {
|
|
|
|
char modifier[3];
|
|
|
|
u_int size;
|
|
|
|
|
|
|
|
char **argv;
|
|
|
|
int argc;
|
|
|
|
};
|
|
|
|
|
2015-05-27 13:28:04 +00:00
|
|
|
/* Format entry tree comparison function. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static int
|
2015-05-27 13:28:04 +00:00
|
|
|
format_entry_cmp(struct format_entry *fe1, struct format_entry *fe2)
|
2011-08-26 10:53:16 +00:00
|
|
|
{
|
|
|
|
return (strcmp(fe1->key, fe2->key));
|
|
|
|
}
|
|
|
|
|
2013-05-31 19:46:42 +00:00
|
|
|
/* Single-character uppercase aliases. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static const char *format_upper[] = {
|
2011-08-26 10:53:16 +00:00
|
|
|
NULL, /* A */
|
|
|
|
NULL, /* B */
|
|
|
|
NULL, /* C */
|
|
|
|
"pane_id", /* D */
|
|
|
|
NULL, /* E */
|
|
|
|
"window_flags", /* F */
|
|
|
|
NULL, /* G */
|
|
|
|
"host", /* H */
|
|
|
|
"window_index", /* I */
|
|
|
|
NULL, /* J */
|
|
|
|
NULL, /* K */
|
|
|
|
NULL, /* L */
|
|
|
|
NULL, /* M */
|
|
|
|
NULL, /* N */
|
|
|
|
NULL, /* O */
|
|
|
|
"pane_index", /* P */
|
|
|
|
NULL, /* Q */
|
|
|
|
NULL, /* R */
|
|
|
|
"session_name", /* S */
|
|
|
|
"pane_title", /* T */
|
|
|
|
NULL, /* U */
|
|
|
|
NULL, /* V */
|
|
|
|
"window_name", /* W */
|
|
|
|
NULL, /* X */
|
|
|
|
NULL, /* Y */
|
|
|
|
NULL /* Z */
|
|
|
|
};
|
|
|
|
|
2013-05-31 19:46:42 +00:00
|
|
|
/* Single-character lowercase aliases. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static const char *format_lower[] = {
|
2013-05-31 19:46:42 +00:00
|
|
|
NULL, /* a */
|
|
|
|
NULL, /* b */
|
|
|
|
NULL, /* c */
|
|
|
|
NULL, /* d */
|
|
|
|
NULL, /* e */
|
|
|
|
NULL, /* f */
|
|
|
|
NULL, /* g */
|
|
|
|
"host_short", /* h */
|
|
|
|
NULL, /* i */
|
|
|
|
NULL, /* j */
|
|
|
|
NULL, /* k */
|
|
|
|
NULL, /* l */
|
|
|
|
NULL, /* m */
|
|
|
|
NULL, /* n */
|
|
|
|
NULL, /* o */
|
|
|
|
NULL, /* p */
|
|
|
|
NULL, /* q */
|
|
|
|
NULL, /* r */
|
|
|
|
NULL, /* s */
|
|
|
|
NULL, /* t */
|
|
|
|
NULL, /* u */
|
|
|
|
NULL, /* v */
|
|
|
|
NULL, /* w */
|
|
|
|
NULL, /* x */
|
|
|
|
NULL, /* y */
|
|
|
|
NULL /* z */
|
|
|
|
};
|
|
|
|
|
2019-03-15 10:04:13 +00:00
|
|
|
/* Is logging enabled? */
|
|
|
|
static inline int
|
|
|
|
format_logging(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
return (log_get_level() != 0 || (ft->flags & FORMAT_VERBOSE));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Log a message if verbose. */
|
|
|
|
static void printflike(3, 4)
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log1(struct format_expand_state *es, const char *from, const char *fmt,
|
|
|
|
...)
|
2019-03-15 10:04:13 +00:00
|
|
|
{
|
2020-10-06 07:36:05 +00:00
|
|
|
struct format_tree *ft = es->ft;
|
2019-03-15 10:04:13 +00:00
|
|
|
va_list ap;
|
|
|
|
char *s;
|
|
|
|
static const char spaces[] = " ";
|
|
|
|
|
|
|
|
if (!format_logging(ft))
|
|
|
|
return;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2020-06-23 05:23:26 +00:00
|
|
|
xvasprintf(&s, fmt, ap);
|
2019-03-15 10:04:13 +00:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
log_debug("%s: %s", from, s);
|
2019-03-15 10:22:57 +00:00
|
|
|
if (ft->item != NULL && (ft->flags & FORMAT_VERBOSE))
|
2020-10-06 07:36:05 +00:00
|
|
|
cmdq_print(ft->item, "#%.*s%s", es->loop, spaces, s);
|
2019-03-15 10:04:13 +00:00
|
|
|
|
|
|
|
free(s);
|
|
|
|
}
|
2020-10-06 07:36:05 +00:00
|
|
|
#define format_log(es, fmt, ...) format_log1(es, __func__, fmt, ##__VA_ARGS__)
|
|
|
|
|
|
|
|
/* Copy expand state. */
|
|
|
|
static void
|
|
|
|
format_copy_state(struct format_expand_state *to,
|
|
|
|
struct format_expand_state *from, int flags)
|
|
|
|
{
|
|
|
|
to->ft = from->ft;
|
|
|
|
to->loop = from->loop;
|
|
|
|
to->time = from->time;
|
2021-02-22 07:09:06 +00:00
|
|
|
memcpy(&to->tm, &from->tm, sizeof to->tm);
|
2020-10-06 07:36:42 +00:00
|
|
|
to->flags = from->flags|flags;
|
2020-10-06 07:36:05 +00:00
|
|
|
}
|
2019-03-15 10:04:13 +00:00
|
|
|
|
2017-04-20 09:20:22 +00:00
|
|
|
/* Format job update callback. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2017-04-20 09:20:22 +00:00
|
|
|
format_job_update(struct job *job)
|
|
|
|
{
|
2018-08-23 15:45:05 +00:00
|
|
|
struct format_job *fj = job_get_data(job);
|
|
|
|
struct evbuffer *evb = job_get_event(job)->input;
|
2018-01-18 14:28:11 +00:00
|
|
|
char *line = NULL, *next;
|
2017-04-20 09:20:22 +00:00
|
|
|
time_t t;
|
|
|
|
|
2018-01-18 14:28:11 +00:00
|
|
|
while ((next = evbuffer_readline(evb)) != NULL) {
|
|
|
|
free(line);
|
|
|
|
line = next;
|
|
|
|
}
|
|
|
|
if (line == NULL)
|
2017-04-20 09:20:22 +00:00
|
|
|
return;
|
|
|
|
fj->updated = 1;
|
|
|
|
|
|
|
|
free(fj->out);
|
|
|
|
fj->out = line;
|
|
|
|
|
2017-05-12 13:27:57 +00:00
|
|
|
log_debug("%s: %p %s: %s", __func__, fj, fj->cmd, fj->out);
|
2017-04-20 09:20:22 +00:00
|
|
|
|
2017-05-31 17:56:48 +00:00
|
|
|
t = time(NULL);
|
2017-04-20 09:20:22 +00:00
|
|
|
if (fj->status && fj->last != t) {
|
2017-05-12 13:27:57 +00:00
|
|
|
if (fj->client != NULL)
|
|
|
|
server_status_client(fj->client);
|
2017-04-20 09:20:22 +00:00
|
|
|
fj->last = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Format job complete callback. */
|
|
|
|
static void
|
|
|
|
format_job_complete(struct job *job)
|
2015-05-27 13:28:04 +00:00
|
|
|
{
|
2018-08-23 15:45:05 +00:00
|
|
|
struct format_job *fj = job_get_data(job);
|
|
|
|
struct evbuffer *evb = job_get_event(job)->input;
|
2015-05-27 13:28:04 +00:00
|
|
|
char *line, *buf;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
fj->job = NULL;
|
|
|
|
|
|
|
|
buf = NULL;
|
2018-08-23 15:45:05 +00:00
|
|
|
if ((line = evbuffer_readline(evb)) == NULL) {
|
|
|
|
len = EVBUFFER_LENGTH(evb);
|
2015-05-27 13:28:04 +00:00
|
|
|
buf = xmalloc(len + 1);
|
|
|
|
if (len != 0)
|
2018-08-23 15:45:05 +00:00
|
|
|
memcpy(buf, EVBUFFER_DATA(evb), len);
|
2015-05-27 13:28:04 +00:00
|
|
|
buf[len] = '\0';
|
|
|
|
} else
|
|
|
|
buf = line;
|
2017-04-20 09:20:22 +00:00
|
|
|
|
2017-05-12 13:27:57 +00:00
|
|
|
log_debug("%s: %p %s: %s", __func__, fj, fj->cmd, buf);
|
|
|
|
|
2017-04-20 09:20:22 +00:00
|
|
|
if (*buf != '\0' || !fj->updated) {
|
|
|
|
free(fj->out);
|
|
|
|
fj->out = buf;
|
|
|
|
} else
|
|
|
|
free(buf);
|
2015-05-27 13:28:04 +00:00
|
|
|
|
|
|
|
if (fj->status) {
|
2017-05-01 12:20:55 +00:00
|
|
|
if (fj->client != NULL)
|
|
|
|
server_status_client(fj->client);
|
2015-05-27 13:28:04 +00:00
|
|
|
fj->status = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find a job. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static char *
|
2020-10-06 07:36:05 +00:00
|
|
|
format_job_get(struct format_expand_state *es, const char *cmd)
|
2015-05-27 13:28:04 +00:00
|
|
|
{
|
2020-10-06 07:36:05 +00:00
|
|
|
struct format_tree *ft = es->ft;
|
|
|
|
struct format_job_tree *jobs;
|
|
|
|
struct format_job fj0, *fj;
|
|
|
|
time_t t;
|
|
|
|
char *expanded;
|
|
|
|
int force;
|
|
|
|
struct format_expand_state next;
|
2015-05-27 13:28:04 +00:00
|
|
|
|
2017-05-01 12:20:55 +00:00
|
|
|
if (ft->client == NULL)
|
|
|
|
jobs = &format_jobs;
|
|
|
|
else if (ft->client->jobs != NULL)
|
|
|
|
jobs = ft->client->jobs;
|
|
|
|
else {
|
|
|
|
jobs = ft->client->jobs = xmalloc(sizeof *ft->client->jobs);
|
|
|
|
RB_INIT(jobs);
|
|
|
|
}
|
|
|
|
|
2017-02-03 11:57:27 +00:00
|
|
|
fj0.tag = ft->tag;
|
2015-05-27 13:28:04 +00:00
|
|
|
fj0.cmd = cmd;
|
2017-05-01 12:20:55 +00:00
|
|
|
if ((fj = RB_FIND(format_job_tree, jobs, &fj0)) == NULL) {
|
2015-05-27 13:28:04 +00:00
|
|
|
fj = xcalloc(1, sizeof *fj);
|
2017-05-01 12:20:55 +00:00
|
|
|
fj->client = ft->client;
|
2017-02-03 11:57:27 +00:00
|
|
|
fj->tag = ft->tag;
|
2015-05-27 13:28:04 +00:00
|
|
|
fj->cmd = xstrdup(cmd);
|
|
|
|
|
2017-05-01 12:20:55 +00:00
|
|
|
RB_INSERT(format_job_tree, jobs, fj);
|
2015-05-27 13:28:04 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:25:40 +00:00
|
|
|
format_copy_state(&next, es, FORMAT_EXPAND_NOJOBS);
|
|
|
|
next.flags &= ~FORMAT_EXPAND_TIME;
|
|
|
|
|
|
|
|
expanded = format_expand1(&next, cmd);
|
2016-11-17 10:06:08 +00:00
|
|
|
if (fj->expanded == NULL || strcmp(expanded, fj->expanded) != 0) {
|
|
|
|
free((void *)fj->expanded);
|
|
|
|
fj->expanded = xstrdup(expanded);
|
|
|
|
force = 1;
|
|
|
|
} else
|
|
|
|
force = (ft->flags & FORMAT_FORCE);
|
|
|
|
|
2015-09-14 10:25:52 +00:00
|
|
|
t = time(NULL);
|
2019-03-18 09:46:42 +00:00
|
|
|
if (force && fj->job != NULL)
|
|
|
|
job_free(fj->job);
|
|
|
|
if (force || (fj->job == NULL && fj->last != t)) {
|
2021-10-11 10:55:30 +00:00
|
|
|
fj->job = job_run(expanded, 0, NULL, NULL, NULL,
|
2018-09-27 07:43:18 +00:00
|
|
|
server_client_get_cwd(ft->client, NULL), format_job_update,
|
2020-03-19 13:43:18 +00:00
|
|
|
format_job_complete, NULL, fj, JOB_NOWAIT, -1, -1);
|
2015-05-27 13:28:04 +00:00
|
|
|
if (fj->job == NULL) {
|
|
|
|
free(fj->out);
|
|
|
|
xasprintf(&fj->out, "<'%s' didn't start>", fj->cmd);
|
|
|
|
}
|
2015-09-14 10:25:52 +00:00
|
|
|
fj->last = t;
|
2017-05-12 22:43:15 +00:00
|
|
|
fj->updated = 0;
|
2021-07-13 22:09:29 +00:00
|
|
|
} else if (fj->job != NULL && (t - fj->last) > 1 && fj->out == NULL)
|
|
|
|
xasprintf(&fj->out, "<'%s' not ready>", fj->cmd);
|
2020-10-06 07:36:05 +00:00
|
|
|
free(expanded);
|
2015-09-14 10:25:52 +00:00
|
|
|
|
|
|
|
if (ft->flags & FORMAT_STATUS)
|
|
|
|
fj->status = 1;
|
2021-07-13 22:09:29 +00:00
|
|
|
if (fj->out == NULL)
|
|
|
|
return (xstrdup(""));
|
2020-10-06 07:36:05 +00:00
|
|
|
return (format_expand1(&next, fj->out));
|
2015-05-27 13:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove old jobs. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2017-05-01 12:20:55 +00:00
|
|
|
format_job_tidy(struct format_job_tree *jobs, int force)
|
2015-05-27 13:28:04 +00:00
|
|
|
{
|
|
|
|
struct format_job *fj, *fj1;
|
|
|
|
time_t now;
|
|
|
|
|
|
|
|
now = time(NULL);
|
2017-05-01 12:20:55 +00:00
|
|
|
RB_FOREACH_SAFE(fj, format_job_tree, jobs, fj1) {
|
|
|
|
if (!force && (fj->last > now || now - fj->last < 3600))
|
2015-05-27 13:28:04 +00:00
|
|
|
continue;
|
2017-05-01 12:20:55 +00:00
|
|
|
RB_REMOVE(format_job_tree, jobs, fj);
|
2015-05-27 13:28:04 +00:00
|
|
|
|
2015-08-28 11:38:27 +00:00
|
|
|
log_debug("%s: %s", __func__, fj->cmd);
|
|
|
|
|
2015-05-27 13:28:04 +00:00
|
|
|
if (fj->job != NULL)
|
|
|
|
job_free(fj->job);
|
|
|
|
|
2016-11-17 10:06:08 +00:00
|
|
|
free((void *)fj->expanded);
|
2015-08-28 12:16:28 +00:00
|
|
|
free((void *)fj->cmd);
|
2015-05-27 13:28:04 +00:00
|
|
|
free(fj->out);
|
|
|
|
|
|
|
|
free(fj);
|
|
|
|
}
|
2017-05-01 12:20:55 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 07:08:18 +00:00
|
|
|
/* Tidy old jobs for all clients. */
|
2017-05-01 12:20:55 +00:00
|
|
|
void
|
2021-03-11 07:08:18 +00:00
|
|
|
format_tidy_jobs(void)
|
2017-05-01 12:20:55 +00:00
|
|
|
{
|
|
|
|
struct client *c;
|
|
|
|
|
|
|
|
format_job_tidy(&format_jobs, 0);
|
|
|
|
TAILQ_FOREACH(c, &clients, entry) {
|
|
|
|
if (c->jobs != NULL)
|
|
|
|
format_job_tidy(c->jobs, 0);
|
|
|
|
}
|
2021-03-11 07:08:18 +00:00
|
|
|
}
|
2015-08-28 11:38:27 +00:00
|
|
|
|
2021-03-11 07:08:18 +00:00
|
|
|
/* Remove old jobs for client. */
|
|
|
|
void
|
|
|
|
format_lost_client(struct client *c)
|
|
|
|
{
|
|
|
|
if (c->jobs != NULL)
|
|
|
|
format_job_tidy(c->jobs, 1);
|
|
|
|
free(c->jobs);
|
2015-05-27 13:28:04 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
/* Wrapper for asprintf. */
|
|
|
|
static char * printflike(1, 2)
|
|
|
|
format_printf(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
xvasprintf(&s, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:46:40 +00:00
|
|
|
/* Callback for host. */
|
2021-02-22 07:09:06 +00:00
|
|
|
static void *
|
2020-06-01 19:39:25 +00:00
|
|
|
format_cb_host(__unused struct format_tree *ft)
|
2015-08-28 16:46:40 +00:00
|
|
|
{
|
|
|
|
char host[HOST_NAME_MAX + 1];
|
|
|
|
|
|
|
|
if (gethostname(host, sizeof host) != 0)
|
2020-06-01 19:39:25 +00:00
|
|
|
return (xstrdup(""));
|
|
|
|
return (xstrdup(host));
|
2015-08-28 16:46:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for host_short. */
|
2021-02-22 07:09:06 +00:00
|
|
|
static void *
|
2020-06-01 19:39:25 +00:00
|
|
|
format_cb_host_short(__unused struct format_tree *ft)
|
2015-08-28 16:46:40 +00:00
|
|
|
{
|
|
|
|
char host[HOST_NAME_MAX + 1], *cp;
|
|
|
|
|
|
|
|
if (gethostname(host, sizeof host) != 0)
|
2020-06-01 19:39:25 +00:00
|
|
|
return (xstrdup(""));
|
|
|
|
if ((cp = strchr(host, '.')) != NULL)
|
|
|
|
*cp = '\0';
|
|
|
|
return (xstrdup(host));
|
2015-08-28 16:46:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pid. */
|
2021-02-22 07:09:06 +00:00
|
|
|
static void *
|
2020-06-01 19:39:25 +00:00
|
|
|
format_cb_pid(__unused struct format_tree *ft)
|
2015-08-28 16:46:40 +00:00
|
|
|
{
|
2020-06-01 19:39:25 +00:00
|
|
|
char *value;
|
|
|
|
|
|
|
|
xasprintf(&value, "%ld", (long)getpid());
|
|
|
|
return (value);
|
2015-08-28 16:46:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-26 11:04:58 +00:00
|
|
|
/* Callback for session_attached_list. */
|
2021-02-22 07:09:06 +00:00
|
|
|
static void *
|
2020-06-01 19:39:25 +00:00
|
|
|
format_cb_session_attached_list(struct format_tree *ft)
|
2019-12-26 11:04:58 +00:00
|
|
|
{
|
|
|
|
struct session *s = ft->s;
|
|
|
|
struct client *loop;
|
|
|
|
struct evbuffer *buffer;
|
|
|
|
int size;
|
2020-06-01 19:39:25 +00:00
|
|
|
char *value = NULL;
|
2019-12-26 11:04:58 +00:00
|
|
|
|
|
|
|
if (s == NULL)
|
2020-06-01 19:39:25 +00:00
|
|
|
return (NULL);
|
2019-12-26 11:04:58 +00:00
|
|
|
|
|
|
|
buffer = evbuffer_new();
|
|
|
|
if (buffer == NULL)
|
|
|
|
fatalx("out of memory");
|
|
|
|
|
|
|
|
TAILQ_FOREACH(loop, &clients, entry) {
|
|
|
|
if (loop->session == s) {
|
|
|
|
if (EVBUFFER_LENGTH(buffer) > 0)
|
|
|
|
evbuffer_add(buffer, ",", 1);
|
|
|
|
evbuffer_add_printf(buffer, "%s", loop->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((size = EVBUFFER_LENGTH(buffer)) != 0)
|
2020-06-01 19:39:25 +00:00
|
|
|
xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
|
2019-12-26 11:04:58 +00:00
|
|
|
evbuffer_free(buffer);
|
2020-06-01 19:39:25 +00:00
|
|
|
return (value);
|
2019-12-26 11:04:58 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 17:01:42 +00:00
|
|
|
/* Callback for session_alerts. */
|
2021-02-22 07:09:06 +00:00
|
|
|
static void *
|
2020-06-01 19:39:25 +00:00
|
|
|
format_cb_session_alerts(struct format_tree *ft)
|
2015-08-28 17:01:42 +00:00
|
|
|
{
|
|
|
|
struct session *s = ft->s;
|
|
|
|
struct winlink *wl;
|
2017-05-05 11:59:47 +00:00
|
|
|
char alerts[1024], tmp[16];
|
2015-08-28 17:01:42 +00:00
|
|
|
|
|
|
|
if (s == NULL)
|
2020-06-01 19:39:25 +00:00
|
|
|
return (NULL);
|
2015-08-28 17:01:42 +00:00
|
|
|
|
|
|
|
*alerts = '\0';
|
|
|
|
RB_FOREACH(wl, winlinks, &s->windows) {
|
|
|
|
if ((wl->flags & WINLINK_ALERTFLAGS) == 0)
|
|
|
|
continue;
|
|
|
|
xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
|
|
|
|
|
|
|
|
if (*alerts != '\0')
|
|
|
|
strlcat(alerts, ",", sizeof alerts);
|
|
|
|
strlcat(alerts, tmp, sizeof alerts);
|
|
|
|
if (wl->flags & WINLINK_ACTIVITY)
|
|
|
|
strlcat(alerts, "#", sizeof alerts);
|
|
|
|
if (wl->flags & WINLINK_BELL)
|
|
|
|
strlcat(alerts, "!", sizeof alerts);
|
|
|
|
if (wl->flags & WINLINK_SILENCE)
|
|
|
|
strlcat(alerts, "~", sizeof alerts);
|
|
|
|
}
|
2020-06-01 19:39:25 +00:00
|
|
|
return (xstrdup(alerts));
|
2015-08-28 17:01:42 +00:00
|
|
|
}
|
|
|
|
|
2017-05-05 11:59:47 +00:00
|
|
|
/* Callback for session_stack. */
|
2021-02-22 07:09:06 +00:00
|
|
|
static void *
|
2020-06-01 19:39:25 +00:00
|
|
|
format_cb_session_stack(struct format_tree *ft)
|
2017-05-05 11:59:47 +00:00
|
|
|
{
|
|
|
|
struct session *s = ft->s;
|
|
|
|
struct winlink *wl;
|
|
|
|
char result[1024], tmp[16];
|
|
|
|
|
|
|
|
if (s == NULL)
|
2020-06-01 19:39:25 +00:00
|
|
|
return (NULL);
|
2017-05-05 11:59:47 +00:00
|
|
|
|
|
|
|
xsnprintf(result, sizeof result, "%u", s->curw->idx);
|
|
|
|
TAILQ_FOREACH(wl, &s->lastw, sentry) {
|
|
|
|
xsnprintf(tmp, sizeof tmp, "%u", wl->idx);
|
|
|
|
|
|
|
|
if (*result != '\0')
|
|
|
|
strlcat(result, ",", sizeof result);
|
|
|
|
strlcat(result, tmp, sizeof result);
|
|
|
|
}
|
2020-06-01 19:39:25 +00:00
|
|
|
return (xstrdup(result));
|
2017-05-05 11:59:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_stack_index. */
|
2021-02-22 07:09:06 +00:00
|
|
|
static void *
|
2020-06-01 19:39:25 +00:00
|
|
|
format_cb_window_stack_index(struct format_tree *ft)
|
2017-05-05 11:59:47 +00:00
|
|
|
{
|
2021-02-22 07:09:06 +00:00
|
|
|
struct session *s;
|
2017-05-05 11:59:47 +00:00
|
|
|
struct winlink *wl;
|
|
|
|
u_int idx;
|
2020-06-01 19:39:25 +00:00
|
|
|
char *value = NULL;
|
2017-05-05 11:59:47 +00:00
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
if (ft->wl == NULL)
|
|
|
|
return (NULL);
|
|
|
|
s = ft->wl->session;
|
|
|
|
|
2017-05-05 11:59:47 +00:00
|
|
|
idx = 0;
|
|
|
|
TAILQ_FOREACH(wl, &s->lastw, sentry) {
|
|
|
|
idx++;
|
|
|
|
if (wl == ft->wl)
|
|
|
|
break;
|
|
|
|
}
|
2020-06-01 19:39:25 +00:00
|
|
|
if (wl == NULL)
|
|
|
|
return (xstrdup("0"));
|
|
|
|
xasprintf(&value, "%u", idx);
|
|
|
|
return (value);
|
2017-05-05 11:59:47 +00:00
|
|
|
}
|
|
|
|
|
2019-12-26 11:04:58 +00:00
|
|
|
/* Callback for window_linked_sessions_list. */
|
2021-02-22 07:09:06 +00:00
|
|
|
static void *
|
2020-06-01 19:39:25 +00:00
|
|
|
format_cb_window_linked_sessions_list(struct format_tree *ft)
|
2019-12-26 11:04:58 +00:00
|
|
|
{
|
2021-02-22 07:09:06 +00:00
|
|
|
struct window *w;
|
2019-12-26 11:04:58 +00:00
|
|
|
struct winlink *wl;
|
|
|
|
struct evbuffer *buffer;
|
|
|
|
int size;
|
2020-06-01 19:39:25 +00:00
|
|
|
char *value = NULL;
|
2019-12-26 11:04:58 +00:00
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
if (ft->wl == NULL)
|
|
|
|
return (NULL);
|
|
|
|
w = ft->wl->window;
|
|
|
|
|
2019-12-26 11:04:58 +00:00
|
|
|
buffer = evbuffer_new();
|
|
|
|
if (buffer == NULL)
|
|
|
|
fatalx("out of memory");
|
|
|
|
|
|
|
|
TAILQ_FOREACH(wl, &w->winlinks, wentry) {
|
|
|
|
if (EVBUFFER_LENGTH(buffer) > 0)
|
|
|
|
evbuffer_add(buffer, ",", 1);
|
|
|
|
evbuffer_add_printf(buffer, "%s", wl->session->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((size = EVBUFFER_LENGTH(buffer)) != 0)
|
2020-06-01 19:39:25 +00:00
|
|
|
xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
|
2019-12-26 11:04:58 +00:00
|
|
|
evbuffer_free(buffer);
|
2020-06-01 19:39:25 +00:00
|
|
|
return (value);
|
2019-12-26 11:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_active_sessions. */
|
2021-02-22 07:09:06 +00:00
|
|
|
static void *
|
2020-06-01 19:39:25 +00:00
|
|
|
format_cb_window_active_sessions(struct format_tree *ft)
|
2019-12-26 11:04:58 +00:00
|
|
|
{
|
2021-02-22 07:09:06 +00:00
|
|
|
struct window *w;
|
2019-12-26 11:04:58 +00:00
|
|
|
struct winlink *wl;
|
|
|
|
u_int n = 0;
|
2020-06-01 19:39:25 +00:00
|
|
|
char *value;
|
2019-12-26 11:04:58 +00:00
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
if (ft->wl == NULL)
|
|
|
|
return (NULL);
|
|
|
|
w = ft->wl->window;
|
|
|
|
|
2019-12-26 11:04:58 +00:00
|
|
|
TAILQ_FOREACH(wl, &w->winlinks, wentry) {
|
|
|
|
if (wl->session->curw == wl)
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
2020-06-01 19:39:25 +00:00
|
|
|
xasprintf(&value, "%u", n);
|
|
|
|
return (value);
|
2019-12-26 11:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_active_sessions_list. */
|
2021-02-22 07:09:06 +00:00
|
|
|
static void *
|
2020-06-01 19:39:25 +00:00
|
|
|
format_cb_window_active_sessions_list(struct format_tree *ft)
|
2019-12-26 11:04:58 +00:00
|
|
|
{
|
2021-02-22 07:09:06 +00:00
|
|
|
struct window *w;
|
2019-12-26 11:04:58 +00:00
|
|
|
struct winlink *wl;
|
|
|
|
struct evbuffer *buffer;
|
|
|
|
int size;
|
2020-06-01 19:39:25 +00:00
|
|
|
char *value = NULL;
|
2019-12-26 11:04:58 +00:00
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
if (ft->wl == NULL)
|
|
|
|
return (NULL);
|
|
|
|
w = ft->wl->window;
|
|
|
|
|
2019-12-26 11:04:58 +00:00
|
|
|
buffer = evbuffer_new();
|
|
|
|
if (buffer == NULL)
|
|
|
|
fatalx("out of memory");
|
|
|
|
|
|
|
|
TAILQ_FOREACH(wl, &w->winlinks, wentry) {
|
|
|
|
if (wl->session->curw == wl) {
|
|
|
|
if (EVBUFFER_LENGTH(buffer) > 0)
|
|
|
|
evbuffer_add(buffer, ",", 1);
|
|
|
|
evbuffer_add_printf(buffer, "%s", wl->session->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((size = EVBUFFER_LENGTH(buffer)) != 0)
|
2020-06-01 19:39:25 +00:00
|
|
|
xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
|
2019-12-26 11:04:58 +00:00
|
|
|
evbuffer_free(buffer);
|
2020-06-01 19:39:25 +00:00
|
|
|
return (value);
|
2019-12-26 11:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_active_clients. */
|
2021-02-22 07:09:06 +00:00
|
|
|
static void *
|
2020-06-01 19:39:25 +00:00
|
|
|
format_cb_window_active_clients(struct format_tree *ft)
|
2019-12-26 11:04:58 +00:00
|
|
|
{
|
2021-02-22 07:09:06 +00:00
|
|
|
struct window *w;
|
2019-12-26 11:04:58 +00:00
|
|
|
struct client *loop;
|
|
|
|
struct session *client_session;
|
|
|
|
u_int n = 0;
|
2020-06-01 19:39:25 +00:00
|
|
|
char *value;
|
2019-12-26 11:04:58 +00:00
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
if (ft->wl == NULL)
|
|
|
|
return (NULL);
|
|
|
|
w = ft->wl->window;
|
|
|
|
|
2019-12-26 11:04:58 +00:00
|
|
|
TAILQ_FOREACH(loop, &clients, entry) {
|
|
|
|
client_session = loop->session;
|
|
|
|
if (client_session == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (w == client_session->curw->window)
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
2020-06-01 19:39:25 +00:00
|
|
|
xasprintf(&value, "%u", n);
|
|
|
|
return (value);
|
2019-12-26 11:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_active_clients_list. */
|
2021-02-22 07:09:06 +00:00
|
|
|
static void *
|
2020-06-01 19:39:25 +00:00
|
|
|
format_cb_window_active_clients_list(struct format_tree *ft)
|
2019-12-26 11:04:58 +00:00
|
|
|
{
|
2021-02-22 07:09:06 +00:00
|
|
|
struct window *w;
|
2019-12-26 11:04:58 +00:00
|
|
|
struct client *loop;
|
|
|
|
struct session *client_session;
|
|
|
|
struct evbuffer *buffer;
|
|
|
|
int size;
|
2020-06-01 19:39:25 +00:00
|
|
|
char *value = NULL;
|
2019-12-26 11:04:58 +00:00
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
if (ft->wl == NULL)
|
|
|
|
return (NULL);
|
|
|
|
w = ft->wl->window;
|
|
|
|
|
2019-12-26 11:04:58 +00:00
|
|
|
buffer = evbuffer_new();
|
|
|
|
if (buffer == NULL)
|
|
|
|
fatalx("out of memory");
|
|
|
|
|
|
|
|
TAILQ_FOREACH(loop, &clients, entry) {
|
|
|
|
client_session = loop->session;
|
|
|
|
if (client_session == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (w == client_session->curw->window) {
|
|
|
|
if (EVBUFFER_LENGTH(buffer) > 0)
|
|
|
|
evbuffer_add(buffer, ",", 1);
|
|
|
|
evbuffer_add_printf(buffer, "%s", loop->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((size = EVBUFFER_LENGTH(buffer)) != 0)
|
2020-06-01 19:39:25 +00:00
|
|
|
xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
|
2019-12-26 11:04:58 +00:00
|
|
|
evbuffer_free(buffer);
|
2020-06-01 19:39:25 +00:00
|
|
|
return (value);
|
2019-12-26 11:04:58 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 17:01:42 +00:00
|
|
|
/* Callback for window_layout. */
|
2021-02-22 07:09:06 +00:00
|
|
|
static void *
|
2020-06-01 19:39:25 +00:00
|
|
|
format_cb_window_layout(struct format_tree *ft)
|
2015-08-28 17:01:42 +00:00
|
|
|
{
|
|
|
|
struct window *w = ft->w;
|
|
|
|
|
|
|
|
if (w == NULL)
|
2020-06-01 19:39:25 +00:00
|
|
|
return (NULL);
|
2015-08-28 17:01:42 +00:00
|
|
|
|
|
|
|
if (w->saved_layout_root != NULL)
|
2020-06-01 19:39:25 +00:00
|
|
|
return (layout_dump(w->saved_layout_root));
|
|
|
|
return (layout_dump(w->layout_root));
|
2015-08-28 17:01:42 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 10:00:26 +00:00
|
|
|
/* Callback for window_visible_layout. */
|
2021-02-22 07:09:06 +00:00
|
|
|
static void *
|
2020-06-01 19:39:25 +00:00
|
|
|
format_cb_window_visible_layout(struct format_tree *ft)
|
2015-11-13 10:00:26 +00:00
|
|
|
{
|
|
|
|
struct window *w = ft->w;
|
|
|
|
|
|
|
|
if (w == NULL)
|
2020-06-01 19:39:25 +00:00
|
|
|
return (NULL);
|
2015-11-13 10:00:26 +00:00
|
|
|
|
2020-06-01 19:39:25 +00:00
|
|
|
return (layout_dump(w->layout_root));
|
2015-11-13 10:00:26 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 16:46:40 +00:00
|
|
|
/* Callback for pane_start_command. */
|
2021-02-22 07:09:06 +00:00
|
|
|
static void *
|
2020-06-01 19:39:25 +00:00
|
|
|
format_cb_start_command(struct format_tree *ft)
|
2015-08-28 16:46:40 +00:00
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
|
|
|
|
|
|
|
if (wp == NULL)
|
2020-06-01 19:39:25 +00:00
|
|
|
return (NULL);
|
2015-08-28 16:46:40 +00:00
|
|
|
|
2020-06-01 19:39:25 +00:00
|
|
|
return (cmd_stringify_argv(wp->argc, wp->argv));
|
2015-08-28 16:46:40 +00:00
|
|
|
}
|
|
|
|
|
2022-05-30 13:07:06 +00:00
|
|
|
/* Callback for pane_start_path. */
|
|
|
|
static void *
|
|
|
|
format_cb_start_path(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
|
|
|
|
|
|
|
if (wp == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
if (wp->cwd == NULL)
|
|
|
|
return (xstrdup(""));
|
|
|
|
return (xstrdup(wp->cwd));
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:46:40 +00:00
|
|
|
/* Callback for pane_current_command. */
|
2021-02-22 07:09:06 +00:00
|
|
|
static void *
|
2020-06-01 19:39:25 +00:00
|
|
|
format_cb_current_command(struct format_tree *ft)
|
2015-08-28 16:46:40 +00:00
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
2020-06-01 19:39:25 +00:00
|
|
|
char *cmd, *value;
|
2015-08-28 16:46:40 +00:00
|
|
|
|
2019-10-14 09:24:06 +00:00
|
|
|
if (wp == NULL || wp->shell == NULL)
|
2020-06-01 19:39:25 +00:00
|
|
|
return (NULL);
|
2015-08-28 16:46:40 +00:00
|
|
|
|
|
|
|
cmd = get_proc_name(wp->fd, wp->tty);
|
|
|
|
if (cmd == NULL || *cmd == '\0') {
|
|
|
|
free(cmd);
|
|
|
|
cmd = cmd_stringify_argv(wp->argc, wp->argv);
|
|
|
|
if (cmd == NULL || *cmd == '\0') {
|
|
|
|
free(cmd);
|
|
|
|
cmd = xstrdup(wp->shell);
|
|
|
|
}
|
|
|
|
}
|
2020-06-01 19:39:25 +00:00
|
|
|
value = parse_window_name(cmd);
|
2015-08-28 16:46:40 +00:00
|
|
|
free(cmd);
|
2020-06-01 19:39:25 +00:00
|
|
|
return (value);
|
2015-08-28 16:46:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 11:26:07 +00:00
|
|
|
/* Callback for pane_current_path. */
|
2021-02-22 07:09:06 +00:00
|
|
|
static void *
|
2020-06-01 19:39:25 +00:00
|
|
|
format_cb_current_path(struct format_tree *ft)
|
2020-04-08 11:26:07 +00:00
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
|
|
|
char *cwd;
|
|
|
|
|
|
|
|
if (wp == NULL)
|
2020-06-01 19:39:25 +00:00
|
|
|
return (NULL);
|
2020-04-08 11:26:07 +00:00
|
|
|
|
|
|
|
cwd = get_proc_cwd(wp->fd);
|
2020-06-01 19:39:25 +00:00
|
|
|
if (cwd == NULL)
|
|
|
|
return (NULL);
|
|
|
|
return (xstrdup(cwd));
|
2020-04-08 11:26:07 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 17:01:42 +00:00
|
|
|
/* Callback for history_bytes. */
|
2021-02-22 07:09:06 +00:00
|
|
|
static void *
|
2020-06-01 19:39:25 +00:00
|
|
|
format_cb_history_bytes(struct format_tree *ft)
|
2015-08-28 17:01:42 +00:00
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
|
|
|
struct grid *gd;
|
|
|
|
struct grid_line *gl;
|
2020-05-25 15:02:25 +00:00
|
|
|
size_t size = 0;
|
2015-08-28 17:01:42 +00:00
|
|
|
u_int i;
|
2020-06-01 19:39:25 +00:00
|
|
|
char *value;
|
2015-08-28 17:01:42 +00:00
|
|
|
|
|
|
|
if (wp == NULL)
|
2020-06-01 19:39:25 +00:00
|
|
|
return (NULL);
|
2015-08-28 17:01:42 +00:00
|
|
|
gd = wp->base.grid;
|
|
|
|
|
2020-05-25 15:02:25 +00:00
|
|
|
for (i = 0; i < gd->hsize + gd->sy; i++) {
|
2018-07-04 09:44:07 +00:00
|
|
|
gl = grid_get_line(gd, i);
|
2015-08-28 17:01:42 +00:00
|
|
|
size += gl->cellsize * sizeof *gl->celldata;
|
2015-11-13 08:09:28 +00:00
|
|
|
size += gl->extdsize * sizeof *gl->extddata;
|
2015-08-28 17:01:42 +00:00
|
|
|
}
|
2020-05-25 15:02:25 +00:00
|
|
|
size += (gd->hsize + gd->sy) * sizeof *gl;
|
2015-08-28 17:01:42 +00:00
|
|
|
|
2020-06-01 19:39:25 +00:00
|
|
|
xasprintf(&value, "%zu", size);
|
|
|
|
return (value);
|
2020-05-25 15:02:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for history_all_bytes. */
|
2021-02-22 07:09:06 +00:00
|
|
|
static void *
|
2020-06-01 19:39:25 +00:00
|
|
|
format_cb_history_all_bytes(struct format_tree *ft)
|
2020-05-25 15:02:25 +00:00
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
|
|
|
struct grid *gd;
|
|
|
|
struct grid_line *gl;
|
|
|
|
u_int i, lines, cells = 0, extended_cells = 0;
|
2020-06-01 19:39:25 +00:00
|
|
|
char *value;
|
2020-05-25 15:02:25 +00:00
|
|
|
|
|
|
|
if (wp == NULL)
|
2020-06-01 19:39:25 +00:00
|
|
|
return (NULL);
|
2020-05-25 15:02:25 +00:00
|
|
|
gd = wp->base.grid;
|
|
|
|
|
|
|
|
lines = gd->hsize + gd->sy;
|
|
|
|
for (i = 0; i < lines; i++) {
|
|
|
|
gl = grid_get_line(gd, i);
|
|
|
|
cells += gl->cellsize;
|
|
|
|
extended_cells += gl->extdsize;
|
|
|
|
}
|
|
|
|
|
2020-06-01 19:39:25 +00:00
|
|
|
xasprintf(&value, "%u,%zu,%u,%zu,%u,%zu", lines,
|
2020-05-25 15:02:25 +00:00
|
|
|
lines * sizeof *gl, cells, cells * sizeof *gl->celldata,
|
|
|
|
extended_cells, extended_cells * sizeof *gl->extddata);
|
2020-06-01 19:39:25 +00:00
|
|
|
return (value);
|
2015-08-28 17:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_tabs. */
|
2021-02-22 07:09:06 +00:00
|
|
|
static void *
|
2020-06-01 19:39:25 +00:00
|
|
|
format_cb_pane_tabs(struct format_tree *ft)
|
2015-08-28 17:01:42 +00:00
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
|
|
|
struct evbuffer *buffer;
|
|
|
|
u_int i;
|
|
|
|
int size;
|
2020-06-01 19:39:25 +00:00
|
|
|
char *value = NULL;
|
2015-08-28 17:01:42 +00:00
|
|
|
|
|
|
|
if (wp == NULL)
|
2020-06-01 19:39:25 +00:00
|
|
|
return (NULL);
|
2015-08-28 17:01:42 +00:00
|
|
|
|
|
|
|
buffer = evbuffer_new();
|
2018-11-19 13:35:40 +00:00
|
|
|
if (buffer == NULL)
|
|
|
|
fatalx("out of memory");
|
2015-08-28 17:01:42 +00:00
|
|
|
for (i = 0; i < wp->base.grid->sx; i++) {
|
|
|
|
if (!bit_test(wp->base.tabs, i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (EVBUFFER_LENGTH(buffer) > 0)
|
|
|
|
evbuffer_add(buffer, ",", 1);
|
|
|
|
evbuffer_add_printf(buffer, "%u", i);
|
|
|
|
}
|
2017-11-02 18:27:35 +00:00
|
|
|
if ((size = EVBUFFER_LENGTH(buffer)) != 0)
|
2020-06-01 19:39:25 +00:00
|
|
|
xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
|
2017-11-02 18:27:35 +00:00
|
|
|
evbuffer_free(buffer);
|
2020-06-01 19:39:25 +00:00
|
|
|
return (value);
|
2017-11-02 18:27:35 +00:00
|
|
|
}
|
|
|
|
|
2021-02-15 09:39:37 +00:00
|
|
|
/* Callback for pane_fg. */
|
2021-02-22 07:09:06 +00:00
|
|
|
static void *
|
2021-02-15 09:39:37 +00:00
|
|
|
format_cb_pane_fg(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
|
|
|
struct grid_cell gc;
|
|
|
|
|
2021-08-12 08:05:11 +00:00
|
|
|
if (wp == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
2021-02-15 09:39:37 +00:00
|
|
|
tty_default_colours(&gc, wp);
|
|
|
|
return (xstrdup(colour_tostring(gc.fg)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_bg. */
|
2021-02-22 07:09:06 +00:00
|
|
|
static void *
|
2021-02-15 09:39:37 +00:00
|
|
|
format_cb_pane_bg(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
|
|
|
struct grid_cell gc;
|
|
|
|
|
2021-08-12 08:05:11 +00:00
|
|
|
if (wp == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
2021-02-15 09:39:37 +00:00
|
|
|
tty_default_colours(&gc, wp);
|
|
|
|
return (xstrdup(colour_tostring(gc.bg)));
|
|
|
|
}
|
|
|
|
|
2017-11-02 18:52:05 +00:00
|
|
|
/* Callback for session_group_list. */
|
2021-02-22 07:09:06 +00:00
|
|
|
static void *
|
2020-06-01 19:39:25 +00:00
|
|
|
format_cb_session_group_list(struct format_tree *ft)
|
2017-11-02 18:27:35 +00:00
|
|
|
{
|
|
|
|
struct session *s = ft->s;
|
|
|
|
struct session_group *sg;
|
|
|
|
struct session *loop;
|
|
|
|
struct evbuffer *buffer;
|
|
|
|
int size;
|
2020-06-01 19:39:25 +00:00
|
|
|
char *value = NULL;
|
2017-11-02 18:27:35 +00:00
|
|
|
|
|
|
|
if (s == NULL)
|
2020-06-01 19:39:25 +00:00
|
|
|
return (NULL);
|
2017-11-02 18:27:35 +00:00
|
|
|
sg = session_group_contains(s);
|
|
|
|
if (sg == NULL)
|
2020-06-01 19:39:25 +00:00
|
|
|
return (NULL);
|
2017-11-02 18:27:35 +00:00
|
|
|
|
|
|
|
buffer = evbuffer_new();
|
2018-11-19 13:35:40 +00:00
|
|
|
if (buffer == NULL)
|
|
|
|
fatalx("out of memory");
|
2019-12-26 11:04:58 +00:00
|
|
|
|
2017-11-02 18:27:35 +00:00
|
|
|
TAILQ_FOREACH(loop, &sg->sessions, gentry) {
|
|
|
|
if (EVBUFFER_LENGTH(buffer) > 0)
|
|
|
|
evbuffer_add(buffer, ",", 1);
|
|
|
|
evbuffer_add_printf(buffer, "%s", loop->name);
|
|
|
|
}
|
2019-12-26 11:04:58 +00:00
|
|
|
|
|
|
|
if ((size = EVBUFFER_LENGTH(buffer)) != 0)
|
2020-06-01 19:39:25 +00:00
|
|
|
xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
|
2019-12-26 11:04:58 +00:00
|
|
|
evbuffer_free(buffer);
|
2020-06-01 19:39:25 +00:00
|
|
|
return (value);
|
2019-12-26 11:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for session_group_attached_list. */
|
2021-02-22 07:09:06 +00:00
|
|
|
static void *
|
2020-06-01 19:39:25 +00:00
|
|
|
format_cb_session_group_attached_list(struct format_tree *ft)
|
2019-12-26 11:04:58 +00:00
|
|
|
{
|
|
|
|
struct session *s = ft->s, *client_session, *session_loop;
|
|
|
|
struct session_group *sg;
|
|
|
|
struct client *loop;
|
|
|
|
struct evbuffer *buffer;
|
|
|
|
int size;
|
2020-06-01 19:39:25 +00:00
|
|
|
char *value = NULL;
|
2019-12-26 11:04:58 +00:00
|
|
|
|
|
|
|
if (s == NULL)
|
2020-06-01 19:39:25 +00:00
|
|
|
return (NULL);
|
2019-12-26 11:04:58 +00:00
|
|
|
sg = session_group_contains(s);
|
|
|
|
if (sg == NULL)
|
2020-06-01 19:39:25 +00:00
|
|
|
return (NULL);
|
2019-12-26 11:04:58 +00:00
|
|
|
|
|
|
|
buffer = evbuffer_new();
|
|
|
|
if (buffer == NULL)
|
|
|
|
fatalx("out of memory");
|
|
|
|
|
|
|
|
TAILQ_FOREACH(loop, &clients, entry) {
|
|
|
|
client_session = loop->session;
|
|
|
|
if (client_session == NULL)
|
|
|
|
continue;
|
|
|
|
TAILQ_FOREACH(session_loop, &sg->sessions, gentry) {
|
|
|
|
if (session_loop == client_session){
|
|
|
|
if (EVBUFFER_LENGTH(buffer) > 0)
|
|
|
|
evbuffer_add(buffer, ",", 1);
|
|
|
|
evbuffer_add_printf(buffer, "%s", loop->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-02 18:27:35 +00:00
|
|
|
if ((size = EVBUFFER_LENGTH(buffer)) != 0)
|
2020-06-01 19:39:25 +00:00
|
|
|
xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
|
2015-08-28 17:01:42 +00:00
|
|
|
evbuffer_free(buffer);
|
2020-06-01 19:39:25 +00:00
|
|
|
return (value);
|
2015-08-28 17:01:42 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 11:16:49 +00:00
|
|
|
/* Callback for pane_in_mode. */
|
2021-02-22 07:09:06 +00:00
|
|
|
static void *
|
2020-06-01 19:39:25 +00:00
|
|
|
format_cb_pane_in_mode(struct format_tree *ft)
|
2019-03-12 11:16:49 +00:00
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
|
|
|
u_int n = 0;
|
|
|
|
struct window_mode_entry *wme;
|
2020-06-01 19:39:25 +00:00
|
|
|
char *value;
|
2019-03-12 11:16:49 +00:00
|
|
|
|
|
|
|
if (wp == NULL)
|
2020-06-01 19:39:25 +00:00
|
|
|
return (NULL);
|
2019-03-12 11:16:49 +00:00
|
|
|
|
|
|
|
TAILQ_FOREACH(wme, &wp->modes, entry)
|
2021-02-22 07:09:06 +00:00
|
|
|
n++;
|
2020-06-01 19:39:25 +00:00
|
|
|
xasprintf(&value, "%u", n);
|
|
|
|
return (value);
|
2019-03-12 11:16:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-14 13:57:58 +00:00
|
|
|
/* Callback for pane_at_top. */
|
2021-02-22 07:09:06 +00:00
|
|
|
static void *
|
2020-06-01 19:39:25 +00:00
|
|
|
format_cb_pane_at_top(struct format_tree *ft)
|
2020-02-14 13:57:58 +00:00
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
2020-04-09 13:53:50 +00:00
|
|
|
struct window *w;
|
2020-02-14 13:57:58 +00:00
|
|
|
int status, flag;
|
2020-06-01 19:39:25 +00:00
|
|
|
char *value;
|
2020-02-14 13:57:58 +00:00
|
|
|
|
|
|
|
if (wp == NULL)
|
2020-06-01 19:39:25 +00:00
|
|
|
return (NULL);
|
2020-04-09 13:53:50 +00:00
|
|
|
w = wp->window;
|
2020-02-14 13:57:58 +00:00
|
|
|
|
|
|
|
status = options_get_number(w->options, "pane-border-status");
|
|
|
|
if (status == PANE_STATUS_TOP)
|
|
|
|
flag = (wp->yoff == 1);
|
|
|
|
else
|
|
|
|
flag = (wp->yoff == 0);
|
2020-06-01 19:39:25 +00:00
|
|
|
xasprintf(&value, "%d", flag);
|
|
|
|
return (value);
|
2020-02-14 13:57:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_at_bottom. */
|
2021-02-22 07:09:06 +00:00
|
|
|
static void *
|
2020-06-01 19:39:25 +00:00
|
|
|
format_cb_pane_at_bottom(struct format_tree *ft)
|
2020-02-14 13:57:58 +00:00
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
2020-04-09 13:53:50 +00:00
|
|
|
struct window *w;
|
2020-02-14 13:57:58 +00:00
|
|
|
int status, flag;
|
2020-06-01 19:39:25 +00:00
|
|
|
char *value;
|
2020-02-14 13:57:58 +00:00
|
|
|
|
|
|
|
if (wp == NULL)
|
2020-06-01 19:39:25 +00:00
|
|
|
return (NULL);
|
2020-04-09 13:53:50 +00:00
|
|
|
w = wp->window;
|
2020-02-14 13:57:58 +00:00
|
|
|
|
|
|
|
status = options_get_number(w->options, "pane-border-status");
|
|
|
|
if (status == PANE_STATUS_BOTTOM)
|
|
|
|
flag = (wp->yoff + wp->sy == w->sy - 1);
|
|
|
|
else
|
|
|
|
flag = (wp->yoff + wp->sy == w->sy);
|
2020-06-01 19:39:25 +00:00
|
|
|
xasprintf(&value, "%d", flag);
|
|
|
|
return (value);
|
2020-02-14 13:57:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-19 19:01:50 +00:00
|
|
|
/* Callback for cursor_character. */
|
2021-02-22 07:09:06 +00:00
|
|
|
static void *
|
2020-06-01 19:39:25 +00:00
|
|
|
format_cb_cursor_character(struct format_tree *ft)
|
2019-03-19 19:01:50 +00:00
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
|
|
|
struct grid_cell gc;
|
2020-06-01 19:39:25 +00:00
|
|
|
char *value = NULL;
|
2019-03-19 19:01:50 +00:00
|
|
|
|
|
|
|
if (wp == NULL)
|
2020-06-01 19:39:25 +00:00
|
|
|
return (NULL);
|
2019-03-19 19:01:50 +00:00
|
|
|
|
|
|
|
grid_view_get_cell(wp->base.grid, wp->base.cx, wp->base.cy, &gc);
|
|
|
|
if (~gc.flags & GRID_FLAG_PADDING)
|
2020-06-01 19:39:25 +00:00
|
|
|
xasprintf(&value, "%.*s", (int)gc.data.size, gc.data.data);
|
|
|
|
return (value);
|
2019-03-19 19:01:50 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
/* Callback for mouse_word. */
|
|
|
|
static void *
|
|
|
|
format_cb_mouse_word(struct format_tree *ft)
|
2019-05-26 17:34:45 +00:00
|
|
|
{
|
2021-02-22 07:09:06 +00:00
|
|
|
struct window_pane *wp;
|
|
|
|
struct grid *gd;
|
|
|
|
u_int x, y;
|
2019-05-26 17:34:45 +00:00
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
if (!ft->m.valid)
|
|
|
|
return (NULL);
|
|
|
|
wp = cmd_mouse_pane(&ft->m, NULL, NULL);
|
|
|
|
if (wp == NULL)
|
|
|
|
return (NULL);
|
|
|
|
if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0)
|
|
|
|
return (NULL);
|
2019-05-26 17:34:45 +00:00
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
if (!TAILQ_EMPTY(&wp->modes)) {
|
|
|
|
if (TAILQ_FIRST(&wp->modes)->mode == &window_copy_mode ||
|
|
|
|
TAILQ_FIRST(&wp->modes)->mode == &window_view_mode)
|
2023-09-08 06:52:31 +00:00
|
|
|
return (window_copy_get_word(wp, x, y));
|
2021-02-22 07:09:06 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
gd = wp->base.grid;
|
|
|
|
return (format_grid_word(gd, x, gd->hsize + y));
|
|
|
|
}
|
2019-05-26 17:34:45 +00:00
|
|
|
|
2022-07-06 07:36:36 +00:00
|
|
|
/* Callback for mouse_hyperlink. */
|
|
|
|
static void *
|
|
|
|
format_cb_mouse_hyperlink(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
struct window_pane *wp;
|
|
|
|
struct grid *gd;
|
|
|
|
u_int x, y;
|
|
|
|
|
|
|
|
if (!ft->m.valid)
|
|
|
|
return (NULL);
|
|
|
|
wp = cmd_mouse_pane(&ft->m, NULL, NULL);
|
|
|
|
if (wp == NULL)
|
|
|
|
return (NULL);
|
|
|
|
if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0)
|
|
|
|
return (NULL);
|
|
|
|
gd = wp->base.grid;
|
|
|
|
return (format_grid_hyperlink(gd, x, gd->hsize + y, wp->screen));
|
|
|
|
}
|
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
/* Callback for mouse_line. */
|
|
|
|
static void *
|
|
|
|
format_cb_mouse_line(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
struct window_pane *wp;
|
|
|
|
struct grid *gd;
|
|
|
|
u_int x, y;
|
|
|
|
|
|
|
|
if (!ft->m.valid)
|
|
|
|
return (NULL);
|
|
|
|
wp = cmd_mouse_pane(&ft->m, NULL, NULL);
|
|
|
|
if (wp == NULL)
|
|
|
|
return (NULL);
|
|
|
|
if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
if (!TAILQ_EMPTY(&wp->modes)) {
|
|
|
|
if (TAILQ_FIRST(&wp->modes)->mode == &window_copy_mode ||
|
|
|
|
TAILQ_FIRST(&wp->modes)->mode == &window_view_mode)
|
|
|
|
return (window_copy_get_line(wp, y));
|
|
|
|
return (NULL);
|
2019-05-26 17:34:45 +00:00
|
|
|
}
|
2021-02-22 07:09:06 +00:00
|
|
|
gd = wp->base.grid;
|
|
|
|
return (format_grid_line(gd, gd->hsize + y));
|
|
|
|
}
|
2019-05-26 17:34:45 +00:00
|
|
|
|
2023-08-17 14:10:28 +00:00
|
|
|
/* Callback for mouse_status_line. */
|
|
|
|
static void *
|
|
|
|
format_cb_mouse_status_line(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
char *value;
|
|
|
|
u_int y;
|
|
|
|
|
|
|
|
if (!ft->m.valid)
|
|
|
|
return (NULL);
|
|
|
|
if (ft->c == NULL || (~ft->c->tty.flags & TTY_STARTED))
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
if (ft->m.statusat == 0 && ft->m.y < ft->m.statuslines) {
|
|
|
|
y = ft->m.y;
|
|
|
|
} else if (ft->m.statusat > 0 && ft->m.y >= (u_int)ft->m.statusat) {
|
|
|
|
y = ft->m.y - ft->m.statusat;
|
|
|
|
} else
|
|
|
|
return (NULL);
|
|
|
|
xasprintf(&value, "%u", y);
|
|
|
|
return (value);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for mouse_status_range. */
|
|
|
|
static void *
|
|
|
|
format_cb_mouse_status_range(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
struct style_range *sr;
|
|
|
|
u_int x, y;
|
|
|
|
|
|
|
|
if (!ft->m.valid)
|
|
|
|
return (NULL);
|
|
|
|
if (ft->c == NULL || (~ft->c->tty.flags & TTY_STARTED))
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
if (ft->m.statusat == 0 && ft->m.y < ft->m.statuslines) {
|
|
|
|
x = ft->m.x;
|
|
|
|
y = ft->m.y;
|
|
|
|
} else if (ft->m.statusat > 0 && ft->m.y >= (u_int)ft->m.statusat) {
|
|
|
|
x = ft->m.x;
|
|
|
|
y = ft->m.y - ft->m.statusat;
|
|
|
|
} else
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
sr = status_get_range(ft->c, x, y);
|
|
|
|
if (sr == NULL)
|
|
|
|
return (NULL);
|
|
|
|
switch (sr->type) {
|
|
|
|
case STYLE_RANGE_NONE:
|
|
|
|
return (NULL);
|
|
|
|
case STYLE_RANGE_LEFT:
|
|
|
|
return (xstrdup("left"));
|
|
|
|
case STYLE_RANGE_RIGHT:
|
|
|
|
return (xstrdup("right"));
|
|
|
|
case STYLE_RANGE_PANE:
|
|
|
|
return (xstrdup("pane"));
|
|
|
|
case STYLE_RANGE_WINDOW:
|
|
|
|
return (xstrdup("window"));
|
|
|
|
case STYLE_RANGE_SESSION:
|
|
|
|
return (xstrdup("session"));
|
|
|
|
case STYLE_RANGE_USER:
|
|
|
|
return (xstrdup(sr->string));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
/* Callback for alternate_on. */
|
|
|
|
static void *
|
|
|
|
format_cb_alternate_on(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL) {
|
|
|
|
if (ft->wp->base.saved_grid != NULL)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
2019-05-26 17:34:45 +00:00
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
/* Callback for alternate_saved_x. */
|
|
|
|
static void *
|
|
|
|
format_cb_alternate_saved_x(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL)
|
|
|
|
return (format_printf("%u", ft->wp->base.saved_cx));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for alternate_saved_y. */
|
|
|
|
static void *
|
|
|
|
format_cb_alternate_saved_y(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL)
|
|
|
|
return (format_printf("%u", ft->wp->base.saved_cy));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for buffer_name. */
|
|
|
|
static void *
|
|
|
|
format_cb_buffer_name(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->pb != NULL)
|
|
|
|
return (xstrdup(paste_buffer_name(ft->pb)));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for buffer_sample. */
|
|
|
|
static void *
|
|
|
|
format_cb_buffer_sample(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->pb != NULL)
|
|
|
|
return (paste_make_sample(ft->pb));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for buffer_size. */
|
|
|
|
static void *
|
|
|
|
format_cb_buffer_size(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
if (ft->pb != NULL) {
|
|
|
|
paste_buffer_data(ft->pb, &size);
|
|
|
|
return (format_printf("%zu", size));
|
2019-05-26 17:34:45 +00:00
|
|
|
}
|
2021-02-22 07:09:06 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for client_cell_height. */
|
|
|
|
static void *
|
|
|
|
format_cb_client_cell_height(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED))
|
|
|
|
return (format_printf("%u", ft->c->tty.ypixel));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for client_cell_width. */
|
|
|
|
static void *
|
|
|
|
format_cb_client_cell_width(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED))
|
|
|
|
return (format_printf("%u", ft->c->tty.xpixel));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for client_control_mode. */
|
|
|
|
static void *
|
|
|
|
format_cb_client_control_mode(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->c != NULL) {
|
|
|
|
if (ft->c->flags & CLIENT_CONTROL)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for client_discarded. */
|
|
|
|
static void *
|
|
|
|
format_cb_client_discarded(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->c != NULL)
|
|
|
|
return (format_printf("%zu", ft->c->discarded));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for client_flags. */
|
|
|
|
static void *
|
|
|
|
format_cb_client_flags(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->c != NULL)
|
|
|
|
return (xstrdup(server_client_get_flags(ft->c)));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for client_height. */
|
|
|
|
static void *
|
|
|
|
format_cb_client_height(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED))
|
|
|
|
return (format_printf("%u", ft->c->tty.sy));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for client_key_table. */
|
|
|
|
static void *
|
|
|
|
format_cb_client_key_table(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->c != NULL)
|
|
|
|
return (xstrdup(ft->c->keytable->name));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for client_last_session. */
|
|
|
|
static void *
|
|
|
|
format_cb_client_last_session(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->c != NULL &&
|
|
|
|
ft->c->last_session != NULL &&
|
|
|
|
session_alive(ft->c->last_session))
|
|
|
|
return (xstrdup(ft->c->last_session->name));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for client_name. */
|
|
|
|
static void *
|
|
|
|
format_cb_client_name(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->c != NULL)
|
|
|
|
return (xstrdup(ft->c->name));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for client_pid. */
|
|
|
|
static void *
|
|
|
|
format_cb_client_pid(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->c != NULL)
|
|
|
|
return (format_printf("%ld", (long)ft->c->pid));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for client_prefix. */
|
|
|
|
static void *
|
|
|
|
format_cb_client_prefix(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
|
|
|
|
if (ft->c != NULL) {
|
|
|
|
name = server_client_get_key_table(ft->c);
|
|
|
|
if (strcmp(ft->c->keytable->name, name) == 0)
|
2021-02-24 09:22:15 +00:00
|
|
|
return (xstrdup("0"));
|
|
|
|
return (xstrdup("1"));
|
2021-02-22 07:09:06 +00:00
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for client_readonly. */
|
|
|
|
static void *
|
|
|
|
format_cb_client_readonly(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->c != NULL) {
|
|
|
|
if (ft->c->flags & CLIENT_READONLY)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for client_session. */
|
|
|
|
static void *
|
|
|
|
format_cb_client_session(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->c != NULL && ft->c->session != NULL)
|
|
|
|
return (xstrdup(ft->c->session->name));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for client_termfeatures. */
|
|
|
|
static void *
|
|
|
|
format_cb_client_termfeatures(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->c != NULL)
|
|
|
|
return (xstrdup(tty_get_features(ft->c->term_features)));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for client_termname. */
|
|
|
|
static void *
|
|
|
|
format_cb_client_termname(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->c != NULL)
|
|
|
|
return (xstrdup(ft->c->term_name));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for client_termtype. */
|
|
|
|
static void *
|
|
|
|
format_cb_client_termtype(struct format_tree *ft)
|
|
|
|
{
|
2021-02-27 06:28:16 +00:00
|
|
|
if (ft->c != NULL) {
|
|
|
|
if (ft->c->term_type == NULL)
|
|
|
|
return (xstrdup(""));
|
2021-02-22 07:09:06 +00:00
|
|
|
return (xstrdup(ft->c->term_type));
|
2021-02-27 06:28:16 +00:00
|
|
|
}
|
2021-02-22 07:09:06 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for client_tty. */
|
|
|
|
static void *
|
|
|
|
format_cb_client_tty(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->c != NULL)
|
|
|
|
return (xstrdup(ft->c->ttyname));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2022-03-08 11:28:40 +00:00
|
|
|
/* Callback for client_uid. */
|
|
|
|
static void *
|
|
|
|
format_cb_client_uid(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
uid_t uid;
|
|
|
|
|
|
|
|
if (ft->c != NULL) {
|
|
|
|
uid = proc_get_peer_uid(ft->c->peer);
|
|
|
|
if (uid != (uid_t)-1)
|
|
|
|
return (format_printf("%ld", (long)uid));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for client_user. */
|
|
|
|
static void *
|
|
|
|
format_cb_client_user(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
uid_t uid;
|
|
|
|
struct passwd *pw;
|
|
|
|
|
|
|
|
if (ft->c != NULL) {
|
|
|
|
uid = proc_get_peer_uid(ft->c->peer);
|
|
|
|
if (uid != (uid_t)-1 && (pw = getpwuid(uid)) != NULL)
|
|
|
|
return (xstrdup(pw->pw_name));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
/* Callback for client_utf8. */
|
|
|
|
static void *
|
|
|
|
format_cb_client_utf8(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->c != NULL) {
|
|
|
|
if (ft->c->flags & CLIENT_UTF8)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for client_width. */
|
|
|
|
static void *
|
|
|
|
format_cb_client_width(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->c != NULL)
|
|
|
|
return (format_printf("%u", ft->c->tty.sx));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for client_written. */
|
|
|
|
static void *
|
|
|
|
format_cb_client_written(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->c != NULL)
|
|
|
|
return (format_printf("%zu", ft->c->written));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2021-02-22 08:18:13 +00:00
|
|
|
/* Callback for config_files. */
|
|
|
|
static void *
|
|
|
|
format_cb_config_files(__unused struct format_tree *ft)
|
|
|
|
{
|
|
|
|
char *s = NULL;
|
|
|
|
size_t slen = 0;
|
|
|
|
u_int i;
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
for (i = 0; i < cfg_nfiles; i++) {
|
|
|
|
n = strlen(cfg_files[i]) + 1;
|
|
|
|
s = xrealloc(s, slen + n + 1);
|
|
|
|
slen += xsnprintf(s + slen, n + 1, "%s,", cfg_files[i]);
|
|
|
|
}
|
|
|
|
if (s == NULL)
|
|
|
|
return (xstrdup(""));
|
|
|
|
s[slen - 1] = '\0';
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
/* Callback for cursor_flag. */
|
|
|
|
static void *
|
|
|
|
format_cb_cursor_flag(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL) {
|
|
|
|
if (ft->wp->base.mode & MODE_CURSOR)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for cursor_x. */
|
|
|
|
static void *
|
|
|
|
format_cb_cursor_x(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL)
|
|
|
|
return (format_printf("%u", ft->wp->base.cx));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for cursor_y. */
|
|
|
|
static void *
|
|
|
|
format_cb_cursor_y(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL)
|
|
|
|
return (format_printf("%u", ft->wp->base.cy));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for history_limit. */
|
|
|
|
static void *
|
|
|
|
format_cb_history_limit(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL)
|
|
|
|
return (format_printf("%u", ft->wp->base.grid->hlimit));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for history_size. */
|
|
|
|
static void *
|
|
|
|
format_cb_history_size(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL)
|
|
|
|
return (format_printf("%u", ft->wp->base.grid->hsize));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for insert_flag. */
|
|
|
|
static void *
|
|
|
|
format_cb_insert_flag(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL) {
|
|
|
|
if (ft->wp->base.mode & MODE_INSERT)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for keypad_cursor_flag. */
|
|
|
|
static void *
|
|
|
|
format_cb_keypad_cursor_flag(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL) {
|
|
|
|
if (ft->wp->base.mode & MODE_KCURSOR)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for keypad_flag. */
|
|
|
|
static void *
|
|
|
|
format_cb_keypad_flag(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL) {
|
|
|
|
if (ft->wp->base.mode & MODE_KKEYPAD)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for mouse_all_flag. */
|
|
|
|
static void *
|
|
|
|
format_cb_mouse_all_flag(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL) {
|
|
|
|
if (ft->wp->base.mode & MODE_MOUSE_ALL)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for mouse_any_flag. */
|
|
|
|
static void *
|
|
|
|
format_cb_mouse_any_flag(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL) {
|
|
|
|
if (ft->wp->base.mode & ALL_MOUSE_MODES)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for mouse_button_flag. */
|
|
|
|
static void *
|
|
|
|
format_cb_mouse_button_flag(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL) {
|
|
|
|
if (ft->wp->base.mode & MODE_MOUSE_BUTTON)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for mouse_pane. */
|
|
|
|
static void *
|
|
|
|
format_cb_mouse_pane(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
struct window_pane *wp;
|
|
|
|
|
|
|
|
if (ft->m.valid) {
|
|
|
|
wp = cmd_mouse_pane(&ft->m, NULL, NULL);
|
|
|
|
if (wp != NULL)
|
|
|
|
return (format_printf("%%%u", wp->id));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for mouse_sgr_flag. */
|
|
|
|
static void *
|
|
|
|
format_cb_mouse_sgr_flag(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL) {
|
|
|
|
if (ft->wp->base.mode & MODE_MOUSE_SGR)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for mouse_standard_flag. */
|
|
|
|
static void *
|
|
|
|
format_cb_mouse_standard_flag(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL) {
|
|
|
|
if (ft->wp->base.mode & MODE_MOUSE_STANDARD)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for mouse_utf8_flag. */
|
|
|
|
static void *
|
|
|
|
format_cb_mouse_utf8_flag(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL) {
|
|
|
|
if (ft->wp->base.mode & MODE_MOUSE_UTF8)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for mouse_x. */
|
|
|
|
static void *
|
|
|
|
format_cb_mouse_x(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
struct window_pane *wp;
|
|
|
|
u_int x, y;
|
|
|
|
|
2021-10-05 17:23:13 +00:00
|
|
|
if (!ft->m.valid)
|
2021-02-22 07:09:06 +00:00
|
|
|
return (NULL);
|
2021-10-05 17:23:13 +00:00
|
|
|
wp = cmd_mouse_pane(&ft->m, NULL, NULL);
|
|
|
|
if (wp != NULL && cmd_mouse_at(wp, &ft->m, &x, &y, 0) == 0)
|
|
|
|
return (format_printf("%u", x));
|
|
|
|
if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED)) {
|
|
|
|
if (ft->m.statusat == 0 && ft->m.y < ft->m.statuslines)
|
|
|
|
return (format_printf("%u", ft->m.x));
|
2021-10-05 20:15:16 +00:00
|
|
|
if (ft->m.statusat > 0 && ft->m.y >= (u_int)ft->m.statusat)
|
2021-10-05 17:23:13 +00:00
|
|
|
return (format_printf("%u", ft->m.x));
|
2021-02-22 07:09:06 +00:00
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for mouse_y. */
|
|
|
|
static void *
|
|
|
|
format_cb_mouse_y(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
struct window_pane *wp;
|
2021-10-05 20:15:16 +00:00
|
|
|
u_int x, y;
|
2021-02-22 07:09:06 +00:00
|
|
|
|
2021-10-05 17:23:13 +00:00
|
|
|
if (!ft->m.valid)
|
2021-02-22 07:09:06 +00:00
|
|
|
return (NULL);
|
2021-10-05 17:23:13 +00:00
|
|
|
wp = cmd_mouse_pane(&ft->m, NULL, NULL);
|
|
|
|
if (wp != NULL && cmd_mouse_at(wp, &ft->m, &x, &y, 0) == 0)
|
|
|
|
return (format_printf("%u", y));
|
|
|
|
if (ft->c != NULL && (ft->c->tty.flags & TTY_STARTED)) {
|
|
|
|
if (ft->m.statusat == 0 && ft->m.y < ft->m.statuslines)
|
|
|
|
return (format_printf("%u", ft->m.y));
|
2021-10-05 20:15:16 +00:00
|
|
|
if (ft->m.statusat > 0 && ft->m.y >= (u_int)ft->m.statusat)
|
2021-10-05 17:23:13 +00:00
|
|
|
return (format_printf("%u", ft->m.y - ft->m.statusat));
|
2021-02-22 07:09:06 +00:00
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:10:41 +00:00
|
|
|
/* Callback for next_session_id. */
|
|
|
|
static void *
|
|
|
|
format_cb_next_session_id(__unused struct format_tree *ft)
|
|
|
|
{
|
|
|
|
return (format_printf("$%u", next_session_id));
|
|
|
|
}
|
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
/* Callback for origin_flag. */
|
|
|
|
static void *
|
|
|
|
format_cb_origin_flag(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL) {
|
|
|
|
if (ft->wp->base.mode & MODE_ORIGIN)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_active. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_active(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL) {
|
|
|
|
if (ft->wp == ft->wp->window->active)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_at_left. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_at_left(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL) {
|
|
|
|
if (ft->wp->xoff == 0)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_at_right. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_at_right(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL) {
|
|
|
|
if (ft->wp->xoff + ft->wp->sx == ft->wp->window->sx)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_bottom. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_bottom(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL)
|
|
|
|
return (format_printf("%u", ft->wp->yoff + ft->wp->sy - 1));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_dead. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_dead(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL) {
|
|
|
|
if (ft->wp->fd == -1)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2022-03-08 18:31:46 +00:00
|
|
|
/* Callback for pane_dead_signal. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_dead_signal(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
|
|
|
const char *name;
|
|
|
|
|
|
|
|
if (wp != NULL) {
|
|
|
|
if ((wp->flags & PANE_STATUSREADY) && WIFSIGNALED(wp->status)) {
|
|
|
|
name = sig2name(WTERMSIG(wp->status));
|
|
|
|
return (format_printf("%s", name));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
/* Callback for pane_dead_status. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_dead_status(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
|
|
|
|
|
|
|
if (wp != NULL) {
|
|
|
|
if ((wp->flags & PANE_STATUSREADY) && WIFEXITED(wp->status))
|
|
|
|
return (format_printf("%d", WEXITSTATUS(wp->status)));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2022-03-08 18:31:46 +00:00
|
|
|
/* Callback for pane_dead_time. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_dead_time(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
|
|
|
|
|
|
|
if (wp != NULL) {
|
|
|
|
if (wp->flags & PANE_STATUSDRAWN)
|
|
|
|
return (&wp->dead_time);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
/* Callback for pane_format. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_format(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->type == FORMAT_TYPE_PANE)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_height. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_height(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL)
|
|
|
|
return (format_printf("%u", ft->wp->sy));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_id. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_id(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL)
|
|
|
|
return (format_printf("%%%u", ft->wp->id));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_index. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_index(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
u_int idx;
|
|
|
|
|
|
|
|
if (ft->wp != NULL && window_pane_index(ft->wp, &idx) == 0)
|
|
|
|
return (format_printf("%u", idx));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_input_off. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_input_off(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL) {
|
|
|
|
if (ft->wp->flags & PANE_INPUTOFF)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2023-03-27 08:47:57 +00:00
|
|
|
/* Callback for pane_unseen_changes. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_unseen_changes(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL) {
|
|
|
|
if (ft->wp->flags & PANE_UNSEENCHANGES)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2024-08-21 04:17:09 +00:00
|
|
|
/* Callback for pane_key_mode. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_key_mode(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL && ft->wp->screen != NULL) {
|
|
|
|
switch (ft->wp->screen->mode & EXTENDED_KEY_MODES) {
|
|
|
|
case MODE_KEYS_EXTENDED:
|
|
|
|
return (xstrdup("Ext 1"));
|
|
|
|
case MODE_KEYS_EXTENDED_2:
|
|
|
|
return (xstrdup("Ext 2"));
|
|
|
|
default:
|
|
|
|
return (xstrdup("VT10x"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
/* Callback for pane_last. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_last(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL) {
|
2023-07-10 09:24:53 +00:00
|
|
|
if (ft->wp == TAILQ_FIRST(&ft->wp->window->last_panes))
|
2021-02-22 07:09:06 +00:00
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_left. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_left(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL)
|
|
|
|
return (format_printf("%u", ft->wp->xoff));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_marked. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_marked(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL) {
|
|
|
|
if (server_check_marked() && marked_pane.wp == ft->wp)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_marked_set. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_marked_set(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL) {
|
|
|
|
if (server_check_marked())
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_mode. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_mode(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
struct window_mode_entry *wme;
|
|
|
|
|
|
|
|
if (ft->wp != NULL) {
|
|
|
|
wme = TAILQ_FIRST(&ft->wp->modes);
|
|
|
|
if (wme != NULL)
|
|
|
|
return (xstrdup(wme->mode->name));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_path. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_path(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL) {
|
|
|
|
if (ft->wp->base.path == NULL)
|
|
|
|
return (xstrdup(""));
|
|
|
|
return (xstrdup(ft->wp->base.path));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_pid. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_pid(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL)
|
|
|
|
return (format_printf("%ld", (long)ft->wp->pid));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_pipe. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_pipe(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL) {
|
|
|
|
if (ft->wp->pipe_fd != -1)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_right. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_right(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL)
|
|
|
|
return (format_printf("%u", ft->wp->xoff + ft->wp->sx - 1));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_search_string. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_search_string(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL) {
|
|
|
|
if (ft->wp->searchstr == NULL)
|
|
|
|
return (xstrdup(""));
|
|
|
|
return (xstrdup(ft->wp->searchstr));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_synchronized. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_synchronized(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL) {
|
|
|
|
if (options_get_number(ft->wp->options, "synchronize-panes"))
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_title. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_title(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL)
|
|
|
|
return (xstrdup(ft->wp->base.title));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_top. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_top(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL)
|
|
|
|
return (format_printf("%u", ft->wp->yoff));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_tty. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_tty(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL)
|
|
|
|
return (xstrdup(ft->wp->tty));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_width. */
|
|
|
|
static void *
|
|
|
|
format_cb_pane_width(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL)
|
|
|
|
return (format_printf("%u", ft->wp->sx));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for scroll_region_lower. */
|
|
|
|
static void *
|
|
|
|
format_cb_scroll_region_lower(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL)
|
|
|
|
return (format_printf("%u", ft->wp->base.rlower));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for scroll_region_upper. */
|
|
|
|
static void *
|
|
|
|
format_cb_scroll_region_upper(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL)
|
|
|
|
return (format_printf("%u", ft->wp->base.rupper));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2023-05-19 07:46:34 +00:00
|
|
|
/* Callback for server_sessions. */
|
|
|
|
static void *
|
|
|
|
format_cb_server_sessions(__unused struct format_tree *ft)
|
|
|
|
{
|
|
|
|
struct session *s;
|
|
|
|
u_int n = 0;
|
|
|
|
|
|
|
|
RB_FOREACH(s, sessions, &sessions)
|
|
|
|
n++;
|
|
|
|
return (format_printf("%u", n));
|
|
|
|
}
|
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
/* Callback for session_attached. */
|
|
|
|
static void *
|
|
|
|
format_cb_session_attached(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->s != NULL)
|
|
|
|
return (format_printf("%u", ft->s->attached));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for session_format. */
|
|
|
|
static void *
|
|
|
|
format_cb_session_format(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->type == FORMAT_TYPE_SESSION)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for session_group. */
|
|
|
|
static void *
|
|
|
|
format_cb_session_group(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
struct session_group *sg;
|
|
|
|
|
|
|
|
if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL)
|
|
|
|
return (xstrdup(sg->name));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for session_group_attached. */
|
|
|
|
static void *
|
|
|
|
format_cb_session_group_attached(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
struct session_group *sg;
|
|
|
|
|
|
|
|
if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL)
|
|
|
|
return (format_printf("%u", session_group_attached_count (sg)));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for session_group_many_attached. */
|
|
|
|
static void *
|
|
|
|
format_cb_session_group_many_attached(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
struct session_group *sg;
|
|
|
|
|
|
|
|
if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL) {
|
|
|
|
if (session_group_attached_count (sg) > 1)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for session_group_size. */
|
|
|
|
static void *
|
|
|
|
format_cb_session_group_size(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
struct session_group *sg;
|
|
|
|
|
|
|
|
if (ft->s != NULL && (sg = session_group_contains(ft->s)) != NULL)
|
|
|
|
return (format_printf("%u", session_group_count (sg)));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for session_grouped. */
|
|
|
|
static void *
|
|
|
|
format_cb_session_grouped(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->s != NULL) {
|
|
|
|
if (session_group_contains(ft->s) != NULL)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for session_id. */
|
|
|
|
static void *
|
|
|
|
format_cb_session_id(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->s != NULL)
|
|
|
|
return (format_printf("$%u", ft->s->id));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for session_many_attached. */
|
|
|
|
static void *
|
|
|
|
format_cb_session_many_attached(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->s != NULL) {
|
|
|
|
if (ft->s->attached > 1)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for session_marked. */
|
|
|
|
static void *
|
|
|
|
format_cb_session_marked(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->s != NULL) {
|
|
|
|
if (server_check_marked() && marked_pane.s == ft->s)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for session_name. */
|
|
|
|
static void *
|
|
|
|
format_cb_session_name(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->s != NULL)
|
|
|
|
return (xstrdup(ft->s->name));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for session_path. */
|
|
|
|
static void *
|
|
|
|
format_cb_session_path(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->s != NULL)
|
|
|
|
return (xstrdup(ft->s->cwd));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for session_windows. */
|
|
|
|
static void *
|
|
|
|
format_cb_session_windows(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->s != NULL)
|
2021-08-20 20:08:30 +00:00
|
|
|
return (format_printf("%u", winlink_count(&ft->s->windows)));
|
2021-02-22 07:09:06 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for socket_path. */
|
|
|
|
static void *
|
|
|
|
format_cb_socket_path(__unused struct format_tree *ft)
|
|
|
|
{
|
|
|
|
return (xstrdup(socket_path));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for version. */
|
|
|
|
static void *
|
|
|
|
format_cb_version(__unused struct format_tree *ft)
|
|
|
|
{
|
|
|
|
return (xstrdup(getversion()));
|
|
|
|
}
|
|
|
|
|
2021-02-26 07:53:26 +00:00
|
|
|
/* Callback for active_window_index. */
|
|
|
|
static void *
|
|
|
|
format_cb_active_window_index(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->s != NULL)
|
|
|
|
return (format_printf("%u", ft->s->curw->idx));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for last_window_index. */
|
|
|
|
static void *
|
|
|
|
format_cb_last_window_index(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
struct winlink *wl;
|
|
|
|
|
|
|
|
if (ft->s != NULL) {
|
|
|
|
wl = RB_MAX(winlinks, &ft->s->windows);
|
|
|
|
return (format_printf("%u", wl->idx));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
/* Callback for window_active. */
|
|
|
|
static void *
|
|
|
|
format_cb_window_active(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wl != NULL) {
|
|
|
|
if (ft->wl == ft->wl->session->curw)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_activity_flag. */
|
|
|
|
static void *
|
|
|
|
format_cb_window_activity_flag(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wl != NULL) {
|
|
|
|
if (ft->wl->flags & WINLINK_ACTIVITY)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_bell_flag. */
|
|
|
|
static void *
|
|
|
|
format_cb_window_bell_flag(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wl != NULL) {
|
|
|
|
if (ft->wl->flags & WINLINK_BELL)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_bigger. */
|
|
|
|
static void *
|
|
|
|
format_cb_window_bigger(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
u_int ox, oy, sx, sy;
|
|
|
|
|
|
|
|
if (ft->c != NULL) {
|
|
|
|
if (tty_window_offset(&ft->c->tty, &ox, &oy, &sx, &sy))
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_cell_height. */
|
|
|
|
static void *
|
|
|
|
format_cb_window_cell_height(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->w != NULL)
|
|
|
|
return (format_printf("%u", ft->w->ypixel));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_cell_width. */
|
|
|
|
static void *
|
|
|
|
format_cb_window_cell_width(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->w != NULL)
|
|
|
|
return (format_printf("%u", ft->w->xpixel));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_end_flag. */
|
|
|
|
static void *
|
|
|
|
format_cb_window_end_flag(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wl != NULL) {
|
|
|
|
if (ft->wl == RB_MAX(winlinks, &ft->wl->session->windows))
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_flags. */
|
|
|
|
static void *
|
|
|
|
format_cb_window_flags(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wl != NULL)
|
|
|
|
return (xstrdup(window_printable_flags(ft->wl, 1)));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_format. */
|
|
|
|
static void *
|
|
|
|
format_cb_window_format(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->type == FORMAT_TYPE_WINDOW)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_height. */
|
|
|
|
static void *
|
|
|
|
format_cb_window_height(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->w != NULL)
|
|
|
|
return (format_printf("%u", ft->w->sy));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_id. */
|
|
|
|
static void *
|
|
|
|
format_cb_window_id(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->w != NULL)
|
|
|
|
return (format_printf("@%u", ft->w->id));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_index. */
|
|
|
|
static void *
|
|
|
|
format_cb_window_index(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wl != NULL)
|
|
|
|
return (format_printf("%d", ft->wl->idx));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_last_flag. */
|
|
|
|
static void *
|
|
|
|
format_cb_window_last_flag(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wl != NULL) {
|
|
|
|
if (ft->wl == TAILQ_FIRST(&ft->wl->session->lastw))
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_linked. */
|
|
|
|
static void *
|
|
|
|
format_cb_window_linked(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wl != NULL) {
|
|
|
|
if (session_is_linked(ft->wl->session, ft->wl->window))
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_linked_sessions. */
|
|
|
|
static void *
|
|
|
|
format_cb_window_linked_sessions(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wl != NULL)
|
|
|
|
return (format_printf("%u", ft->wl->window->references));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_marked_flag. */
|
|
|
|
static void *
|
|
|
|
format_cb_window_marked_flag(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wl != NULL) {
|
|
|
|
if (server_check_marked() && marked_pane.wl == ft->wl)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_name. */
|
|
|
|
static void *
|
|
|
|
format_cb_window_name(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->w != NULL)
|
|
|
|
return (format_printf("%s", ft->w->name));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_offset_x. */
|
|
|
|
static void *
|
|
|
|
format_cb_window_offset_x(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
u_int ox, oy, sx, sy;
|
|
|
|
|
|
|
|
if (ft->c != NULL) {
|
|
|
|
if (tty_window_offset(&ft->c->tty, &ox, &oy, &sx, &sy))
|
|
|
|
return (format_printf("%u", ox));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_offset_y. */
|
|
|
|
static void *
|
|
|
|
format_cb_window_offset_y(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
u_int ox, oy, sx, sy;
|
|
|
|
|
|
|
|
if (ft->c != NULL) {
|
|
|
|
if (tty_window_offset(&ft->c->tty, &ox, &oy, &sx, &sy))
|
|
|
|
return (format_printf("%u", oy));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_panes. */
|
|
|
|
static void *
|
|
|
|
format_cb_window_panes(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->w != NULL)
|
|
|
|
return (format_printf("%u", window_count_panes(ft->w)));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_raw_flags. */
|
|
|
|
static void *
|
|
|
|
format_cb_window_raw_flags(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wl != NULL)
|
|
|
|
return (xstrdup(window_printable_flags(ft->wl, 0)));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_silence_flag. */
|
|
|
|
static void *
|
|
|
|
format_cb_window_silence_flag(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wl != NULL) {
|
|
|
|
if (ft->wl->flags & WINLINK_SILENCE)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_start_flag. */
|
|
|
|
static void *
|
|
|
|
format_cb_window_start_flag(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wl != NULL) {
|
|
|
|
if (ft->wl == RB_MIN(winlinks, &ft->wl->session->windows))
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_width. */
|
|
|
|
static void *
|
|
|
|
format_cb_window_width(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->w != NULL)
|
|
|
|
return (format_printf("%u", ft->w->sx));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_zoomed_flag. */
|
|
|
|
static void *
|
|
|
|
format_cb_window_zoomed_flag(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->w != NULL) {
|
|
|
|
if (ft->w->flags & WINDOW_ZOOMED)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for wrap_flag. */
|
|
|
|
static void *
|
|
|
|
format_cb_wrap_flag(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->wp != NULL) {
|
|
|
|
if (ft->wp->base.mode & MODE_WRAP)
|
|
|
|
return (xstrdup("1"));
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for buffer_created. */
|
|
|
|
static void *
|
|
|
|
format_cb_buffer_created(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
static struct timeval tv;
|
|
|
|
|
|
|
|
if (ft->pb != NULL) {
|
|
|
|
timerclear(&tv);
|
|
|
|
tv.tv_sec = paste_buffer_created(ft->pb);
|
|
|
|
return (&tv);
|
2019-05-26 17:34:45 +00:00
|
|
|
}
|
2021-02-22 07:09:06 +00:00
|
|
|
return (NULL);
|
2019-05-26 17:34:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
/* Callback for client_activity. */
|
|
|
|
static void *
|
|
|
|
format_cb_client_activity(struct format_tree *ft)
|
2019-05-26 17:34:45 +00:00
|
|
|
{
|
2021-02-22 07:09:06 +00:00
|
|
|
if (ft->c != NULL)
|
|
|
|
return (&ft->c->activity_time);
|
|
|
|
return (NULL);
|
|
|
|
}
|
2019-05-26 17:34:45 +00:00
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
/* Callback for client_created. */
|
|
|
|
static void *
|
|
|
|
format_cb_client_created(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->c != NULL)
|
|
|
|
return (&ft->c->creation_time);
|
|
|
|
return (NULL);
|
|
|
|
}
|
2019-10-23 07:42:05 +00:00
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
/* Callback for session_activity. */
|
|
|
|
static void *
|
|
|
|
format_cb_session_activity(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->s != NULL)
|
|
|
|
return (&ft->s->activity_time);
|
|
|
|
return (NULL);
|
2019-10-23 07:42:05 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
/* Callback for session_created. */
|
|
|
|
static void *
|
|
|
|
format_cb_session_created(struct format_tree *ft)
|
2019-10-23 07:42:05 +00:00
|
|
|
{
|
2021-02-22 07:09:06 +00:00
|
|
|
if (ft->s != NULL)
|
|
|
|
return (&ft->s->creation_time);
|
|
|
|
return (NULL);
|
|
|
|
}
|
2019-05-26 17:34:45 +00:00
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
/* Callback for session_last_attached. */
|
|
|
|
static void *
|
|
|
|
format_cb_session_last_attached(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
if (ft->s != NULL)
|
|
|
|
return (&ft->s->last_attached_time);
|
|
|
|
return (NULL);
|
|
|
|
}
|
2019-05-26 17:34:45 +00:00
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
/* Callback for start_time. */
|
|
|
|
static void *
|
|
|
|
format_cb_start_time(__unused struct format_tree *ft)
|
|
|
|
{
|
|
|
|
return (&start_time);
|
2019-10-23 07:42:05 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
/* Callback for window_activity. */
|
|
|
|
static void *
|
|
|
|
format_cb_window_activity(struct format_tree *ft)
|
2019-10-23 07:42:05 +00:00
|
|
|
{
|
2021-02-22 07:09:06 +00:00
|
|
|
if (ft->w != NULL)
|
|
|
|
return (&ft->w->activity_time);
|
|
|
|
return (NULL);
|
|
|
|
}
|
2019-10-23 07:42:05 +00:00
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
/* Callback for buffer_mode_format, */
|
|
|
|
static void *
|
|
|
|
format_cb_buffer_mode_format(__unused struct format_tree *ft)
|
|
|
|
{
|
|
|
|
return (xstrdup(window_buffer_mode.default_format));
|
|
|
|
}
|
2019-10-23 07:42:05 +00:00
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
/* Callback for client_mode_format, */
|
|
|
|
static void *
|
|
|
|
format_cb_client_mode_format(__unused struct format_tree *ft)
|
|
|
|
{
|
|
|
|
return (xstrdup(window_client_mode.default_format));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for tree_mode_format, */
|
|
|
|
static void *
|
|
|
|
format_cb_tree_mode_format(__unused struct format_tree *ft)
|
|
|
|
{
|
|
|
|
return (xstrdup(window_tree_mode.default_format));
|
|
|
|
}
|
|
|
|
|
2022-03-08 11:28:40 +00:00
|
|
|
/* Callback for uid. */
|
|
|
|
static void *
|
|
|
|
format_cb_uid(__unused struct format_tree *ft)
|
|
|
|
{
|
|
|
|
return (format_printf("%ld", (long)getuid()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for user. */
|
|
|
|
static void *
|
|
|
|
format_cb_user(__unused struct format_tree *ft)
|
|
|
|
{
|
|
|
|
struct passwd *pw;
|
|
|
|
|
|
|
|
if ((pw = getpwuid(getuid())) != NULL)
|
|
|
|
return (xstrdup(pw->pw_name));
|
2022-05-30 12:55:25 +00:00
|
|
|
return (NULL);
|
2022-03-08 11:28:40 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
/* Format table type. */
|
|
|
|
enum format_table_type {
|
|
|
|
FORMAT_TABLE_STRING,
|
|
|
|
FORMAT_TABLE_TIME
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Format table entry. */
|
|
|
|
struct format_table_entry {
|
|
|
|
const char *key;
|
|
|
|
enum format_table_type type;
|
|
|
|
format_cb cb;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Format table. Default format variables (that are almost always in the tree
|
|
|
|
* and where the value is expanded by a callback in this file) are listed here.
|
|
|
|
* Only variables which are added by the caller go into the tree.
|
|
|
|
*/
|
|
|
|
static const struct format_table_entry format_table[] = {
|
2021-02-26 07:53:26 +00:00
|
|
|
{ "active_window_index", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_active_window_index
|
|
|
|
},
|
2021-02-22 07:09:06 +00:00
|
|
|
{ "alternate_on", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_alternate_on
|
|
|
|
},
|
|
|
|
{ "alternate_saved_x", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_alternate_saved_x
|
|
|
|
},
|
|
|
|
{ "alternate_saved_y", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_alternate_saved_y
|
|
|
|
},
|
|
|
|
{ "buffer_created", FORMAT_TABLE_TIME,
|
|
|
|
format_cb_buffer_created
|
|
|
|
},
|
|
|
|
{ "buffer_mode_format", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_buffer_mode_format
|
|
|
|
},
|
|
|
|
{ "buffer_name", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_buffer_name
|
|
|
|
},
|
|
|
|
{ "buffer_sample", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_buffer_sample
|
|
|
|
},
|
|
|
|
{ "buffer_size", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_buffer_size
|
|
|
|
},
|
|
|
|
{ "client_activity", FORMAT_TABLE_TIME,
|
|
|
|
format_cb_client_activity
|
|
|
|
},
|
|
|
|
{ "client_cell_height", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_client_cell_height
|
|
|
|
},
|
|
|
|
{ "client_cell_width", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_client_cell_width
|
|
|
|
},
|
|
|
|
{ "client_control_mode", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_client_control_mode
|
|
|
|
},
|
|
|
|
{ "client_created", FORMAT_TABLE_TIME,
|
|
|
|
format_cb_client_created
|
|
|
|
},
|
|
|
|
{ "client_discarded", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_client_discarded
|
|
|
|
},
|
|
|
|
{ "client_flags", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_client_flags
|
|
|
|
},
|
|
|
|
{ "client_height", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_client_height
|
|
|
|
},
|
|
|
|
{ "client_key_table", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_client_key_table
|
|
|
|
},
|
|
|
|
{ "client_last_session", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_client_last_session
|
|
|
|
},
|
|
|
|
{ "client_mode_format", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_client_mode_format
|
|
|
|
},
|
|
|
|
{ "client_name", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_client_name
|
|
|
|
},
|
|
|
|
{ "client_pid", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_client_pid
|
|
|
|
},
|
|
|
|
{ "client_prefix", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_client_prefix
|
|
|
|
},
|
|
|
|
{ "client_readonly", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_client_readonly
|
|
|
|
},
|
|
|
|
{ "client_session", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_client_session
|
|
|
|
},
|
|
|
|
{ "client_termfeatures", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_client_termfeatures
|
|
|
|
},
|
|
|
|
{ "client_termname", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_client_termname
|
|
|
|
},
|
|
|
|
{ "client_termtype", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_client_termtype
|
|
|
|
},
|
|
|
|
{ "client_tty", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_client_tty
|
|
|
|
},
|
2022-03-08 11:28:40 +00:00
|
|
|
{ "client_uid", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_client_uid
|
|
|
|
},
|
|
|
|
{ "client_user", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_client_user
|
|
|
|
},
|
2021-02-22 07:09:06 +00:00
|
|
|
{ "client_utf8", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_client_utf8
|
|
|
|
},
|
|
|
|
{ "client_width", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_client_width
|
|
|
|
},
|
|
|
|
{ "client_written", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_client_written
|
|
|
|
},
|
2021-02-22 08:18:13 +00:00
|
|
|
{ "config_files", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_config_files
|
|
|
|
},
|
2021-02-22 07:09:06 +00:00
|
|
|
{ "cursor_character", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_cursor_character
|
|
|
|
},
|
|
|
|
{ "cursor_flag", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_cursor_flag
|
|
|
|
},
|
|
|
|
{ "cursor_x", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_cursor_x
|
|
|
|
},
|
|
|
|
{ "cursor_y", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_cursor_y
|
|
|
|
},
|
|
|
|
{ "history_all_bytes", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_history_all_bytes
|
|
|
|
},
|
|
|
|
{ "history_bytes", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_history_bytes
|
|
|
|
},
|
|
|
|
{ "history_limit", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_history_limit
|
|
|
|
},
|
|
|
|
{ "history_size", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_history_size
|
|
|
|
},
|
|
|
|
{ "host", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_host
|
|
|
|
},
|
|
|
|
{ "host_short", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_host_short
|
|
|
|
},
|
|
|
|
{ "insert_flag", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_insert_flag
|
|
|
|
},
|
|
|
|
{ "keypad_cursor_flag", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_keypad_cursor_flag
|
|
|
|
},
|
|
|
|
{ "keypad_flag", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_keypad_flag
|
|
|
|
},
|
2021-02-26 07:53:26 +00:00
|
|
|
{ "last_window_index", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_last_window_index
|
|
|
|
},
|
2021-02-22 07:09:06 +00:00
|
|
|
{ "mouse_all_flag", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_mouse_all_flag
|
|
|
|
},
|
|
|
|
{ "mouse_any_flag", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_mouse_any_flag
|
|
|
|
},
|
|
|
|
{ "mouse_button_flag", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_mouse_button_flag
|
|
|
|
},
|
2022-07-06 07:36:36 +00:00
|
|
|
{ "mouse_hyperlink", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_mouse_hyperlink
|
|
|
|
},
|
2021-02-22 07:09:06 +00:00
|
|
|
{ "mouse_line", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_mouse_line
|
|
|
|
},
|
|
|
|
{ "mouse_pane", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_mouse_pane
|
|
|
|
},
|
|
|
|
{ "mouse_sgr_flag", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_mouse_sgr_flag
|
|
|
|
},
|
|
|
|
{ "mouse_standard_flag", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_mouse_standard_flag
|
|
|
|
},
|
2023-08-17 14:10:28 +00:00
|
|
|
{ "mouse_status_line", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_mouse_status_line
|
|
|
|
},
|
|
|
|
{ "mouse_status_range", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_mouse_status_range
|
|
|
|
},
|
2021-02-22 07:09:06 +00:00
|
|
|
{ "mouse_utf8_flag", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_mouse_utf8_flag
|
|
|
|
},
|
|
|
|
{ "mouse_word", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_mouse_word
|
|
|
|
},
|
|
|
|
{ "mouse_x", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_mouse_x
|
|
|
|
},
|
|
|
|
{ "mouse_y", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_mouse_y
|
|
|
|
},
|
2022-02-22 11:10:41 +00:00
|
|
|
{ "next_session_id", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_next_session_id
|
|
|
|
},
|
2021-02-22 07:09:06 +00:00
|
|
|
{ "origin_flag", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_origin_flag
|
|
|
|
},
|
|
|
|
{ "pane_active", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_active
|
|
|
|
},
|
|
|
|
{ "pane_at_bottom", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_at_bottom
|
|
|
|
},
|
|
|
|
{ "pane_at_left", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_at_left
|
|
|
|
},
|
|
|
|
{ "pane_at_right", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_at_right
|
|
|
|
},
|
|
|
|
{ "pane_at_top", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_at_top
|
|
|
|
},
|
|
|
|
{ "pane_bg", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_bg
|
|
|
|
},
|
|
|
|
{ "pane_bottom", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_bottom
|
|
|
|
},
|
|
|
|
{ "pane_current_command", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_current_command
|
|
|
|
},
|
|
|
|
{ "pane_current_path", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_current_path
|
|
|
|
},
|
|
|
|
{ "pane_dead", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_dead
|
|
|
|
},
|
2022-03-08 18:31:46 +00:00
|
|
|
{ "pane_dead_signal", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_dead_signal
|
|
|
|
},
|
2021-02-22 07:09:06 +00:00
|
|
|
{ "pane_dead_status", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_dead_status
|
|
|
|
},
|
2022-03-08 18:31:46 +00:00
|
|
|
{ "pane_dead_time", FORMAT_TABLE_TIME,
|
|
|
|
format_cb_pane_dead_time
|
|
|
|
},
|
2021-02-22 07:09:06 +00:00
|
|
|
{ "pane_fg", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_fg
|
|
|
|
},
|
|
|
|
{ "pane_format", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_format
|
|
|
|
},
|
|
|
|
{ "pane_height", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_height
|
|
|
|
},
|
|
|
|
{ "pane_id", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_id
|
|
|
|
},
|
|
|
|
{ "pane_in_mode", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_in_mode
|
|
|
|
},
|
|
|
|
{ "pane_index", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_index
|
|
|
|
},
|
|
|
|
{ "pane_input_off", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_input_off
|
|
|
|
},
|
2024-08-21 04:17:09 +00:00
|
|
|
{ "pane_key_mode", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_key_mode
|
|
|
|
},
|
2021-02-22 07:09:06 +00:00
|
|
|
{ "pane_last", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_last
|
|
|
|
},
|
|
|
|
{ "pane_left", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_left
|
|
|
|
},
|
|
|
|
{ "pane_marked", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_marked
|
|
|
|
},
|
|
|
|
{ "pane_marked_set", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_marked_set
|
|
|
|
},
|
|
|
|
{ "pane_mode", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_mode
|
|
|
|
},
|
|
|
|
{ "pane_path", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_path
|
|
|
|
},
|
|
|
|
{ "pane_pid", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_pid
|
|
|
|
},
|
|
|
|
{ "pane_pipe", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_pipe
|
|
|
|
},
|
|
|
|
{ "pane_right", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_right
|
|
|
|
},
|
|
|
|
{ "pane_search_string", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_search_string
|
|
|
|
},
|
|
|
|
{ "pane_start_command", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_start_command
|
|
|
|
},
|
2022-05-30 13:07:06 +00:00
|
|
|
{ "pane_start_path", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_start_path
|
|
|
|
},
|
2021-02-22 07:09:06 +00:00
|
|
|
{ "pane_synchronized", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_synchronized
|
|
|
|
},
|
|
|
|
{ "pane_tabs", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_tabs
|
|
|
|
},
|
|
|
|
{ "pane_title", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_title
|
|
|
|
},
|
|
|
|
{ "pane_top", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_top
|
|
|
|
},
|
|
|
|
{ "pane_tty", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_tty
|
|
|
|
},
|
2023-03-27 08:47:57 +00:00
|
|
|
{ "pane_unseen_changes", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_unseen_changes
|
|
|
|
},
|
2021-02-22 07:09:06 +00:00
|
|
|
{ "pane_width", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pane_width
|
|
|
|
},
|
|
|
|
{ "pid", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_pid
|
|
|
|
},
|
|
|
|
{ "scroll_region_lower", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_scroll_region_lower
|
|
|
|
},
|
|
|
|
{ "scroll_region_upper", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_scroll_region_upper
|
|
|
|
},
|
2023-05-19 07:46:34 +00:00
|
|
|
{ "server_sessions", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_server_sessions
|
|
|
|
},
|
2021-02-22 07:09:06 +00:00
|
|
|
{ "session_activity", FORMAT_TABLE_TIME,
|
|
|
|
format_cb_session_activity
|
|
|
|
},
|
|
|
|
{ "session_alerts", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_session_alerts
|
|
|
|
},
|
|
|
|
{ "session_attached", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_session_attached
|
|
|
|
},
|
|
|
|
{ "session_attached_list", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_session_attached_list
|
|
|
|
},
|
|
|
|
{ "session_created", FORMAT_TABLE_TIME,
|
|
|
|
format_cb_session_created
|
|
|
|
},
|
|
|
|
{ "session_format", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_session_format
|
|
|
|
},
|
|
|
|
{ "session_group", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_session_group
|
|
|
|
},
|
|
|
|
{ "session_group_attached", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_session_group_attached
|
|
|
|
},
|
|
|
|
{ "session_group_attached_list", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_session_group_attached_list
|
|
|
|
},
|
|
|
|
{ "session_group_list", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_session_group_list
|
|
|
|
},
|
|
|
|
{ "session_group_many_attached", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_session_group_many_attached
|
|
|
|
},
|
|
|
|
{ "session_group_size", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_session_group_size
|
|
|
|
},
|
|
|
|
{ "session_grouped", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_session_grouped
|
|
|
|
},
|
|
|
|
{ "session_id", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_session_id
|
|
|
|
},
|
|
|
|
{ "session_last_attached", FORMAT_TABLE_TIME,
|
|
|
|
format_cb_session_last_attached
|
|
|
|
},
|
|
|
|
{ "session_many_attached", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_session_many_attached
|
|
|
|
},
|
|
|
|
{ "session_marked", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_session_marked,
|
|
|
|
},
|
|
|
|
{ "session_name", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_session_name
|
|
|
|
},
|
|
|
|
{ "session_path", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_session_path
|
|
|
|
},
|
|
|
|
{ "session_stack", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_session_stack
|
|
|
|
},
|
|
|
|
{ "session_windows", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_session_windows
|
|
|
|
},
|
|
|
|
{ "socket_path", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_socket_path
|
|
|
|
},
|
|
|
|
{ "start_time", FORMAT_TABLE_TIME,
|
|
|
|
format_cb_start_time
|
|
|
|
},
|
|
|
|
{ "tree_mode_format", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_tree_mode_format
|
|
|
|
},
|
2022-03-08 11:28:40 +00:00
|
|
|
{ "uid", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_uid
|
|
|
|
},
|
|
|
|
{ "user", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_user
|
|
|
|
},
|
2021-02-22 07:09:06 +00:00
|
|
|
{ "version", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_version
|
|
|
|
},
|
|
|
|
{ "window_active", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_active
|
|
|
|
},
|
|
|
|
{ "window_active_clients", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_active_clients
|
|
|
|
},
|
|
|
|
{ "window_active_clients_list", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_active_clients_list
|
|
|
|
},
|
|
|
|
{ "window_active_sessions", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_active_sessions
|
|
|
|
},
|
|
|
|
{ "window_active_sessions_list", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_active_sessions_list
|
|
|
|
},
|
|
|
|
{ "window_activity", FORMAT_TABLE_TIME,
|
|
|
|
format_cb_window_activity
|
|
|
|
},
|
|
|
|
{ "window_activity_flag", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_activity_flag
|
|
|
|
},
|
|
|
|
{ "window_bell_flag", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_bell_flag
|
|
|
|
},
|
|
|
|
{ "window_bigger", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_bigger
|
|
|
|
},
|
|
|
|
{ "window_cell_height", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_cell_height
|
|
|
|
},
|
|
|
|
{ "window_cell_width", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_cell_width
|
|
|
|
},
|
|
|
|
{ "window_end_flag", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_end_flag
|
|
|
|
},
|
|
|
|
{ "window_flags", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_flags
|
|
|
|
},
|
|
|
|
{ "window_format", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_format
|
|
|
|
},
|
|
|
|
{ "window_height", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_height
|
|
|
|
},
|
|
|
|
{ "window_id", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_id
|
|
|
|
},
|
|
|
|
{ "window_index", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_index
|
|
|
|
},
|
|
|
|
{ "window_last_flag", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_last_flag
|
|
|
|
},
|
|
|
|
{ "window_layout", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_layout
|
|
|
|
},
|
|
|
|
{ "window_linked", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_linked
|
|
|
|
},
|
|
|
|
{ "window_linked_sessions", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_linked_sessions
|
|
|
|
},
|
|
|
|
{ "window_linked_sessions_list", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_linked_sessions_list
|
|
|
|
},
|
|
|
|
{ "window_marked_flag", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_marked_flag
|
|
|
|
},
|
|
|
|
{ "window_name", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_name
|
|
|
|
},
|
|
|
|
{ "window_offset_x", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_offset_x
|
|
|
|
},
|
|
|
|
{ "window_offset_y", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_offset_y
|
|
|
|
},
|
|
|
|
{ "window_panes", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_panes
|
|
|
|
},
|
|
|
|
{ "window_raw_flags", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_raw_flags
|
|
|
|
},
|
|
|
|
{ "window_silence_flag", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_silence_flag
|
|
|
|
},
|
|
|
|
{ "window_stack_index", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_stack_index
|
|
|
|
},
|
|
|
|
{ "window_start_flag", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_start_flag
|
|
|
|
},
|
|
|
|
{ "window_visible_layout", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_visible_layout
|
|
|
|
},
|
|
|
|
{ "window_width", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_width
|
|
|
|
},
|
|
|
|
{ "window_zoomed_flag", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_window_zoomed_flag
|
|
|
|
},
|
|
|
|
{ "wrap_flag", FORMAT_TABLE_STRING,
|
|
|
|
format_cb_wrap_flag
|
2020-03-20 17:59:39 +00:00
|
|
|
}
|
2021-02-22 07:09:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Compare format table entries. */
|
|
|
|
static int
|
|
|
|
format_table_compare(const void *key0, const void *entry0)
|
|
|
|
{
|
|
|
|
const char *key = key0;
|
|
|
|
const struct format_table_entry *entry = entry0;
|
|
|
|
|
|
|
|
return (strcmp(key, entry->key));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a format callback. */
|
|
|
|
static struct format_table_entry *
|
|
|
|
format_table_get(const char *key)
|
|
|
|
{
|
|
|
|
return (bsearch(key, format_table, nitems(format_table),
|
|
|
|
sizeof *format_table, format_table_compare));
|
2019-05-26 17:34:45 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 10:59:58 +00:00
|
|
|
/* Merge one format tree into another. */
|
|
|
|
void
|
2016-10-16 19:36:37 +00:00
|
|
|
format_merge(struct format_tree *ft, struct format_tree *from)
|
|
|
|
{
|
|
|
|
struct format_entry *fe;
|
|
|
|
|
|
|
|
RB_FOREACH(fe, format_entry_tree, &from->tree) {
|
|
|
|
if (fe->value != NULL)
|
|
|
|
format_add(ft, fe->key, "%s", fe->value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 19:43:34 +00:00
|
|
|
/* Get format pane. */
|
|
|
|
struct window_pane *
|
|
|
|
format_get_pane(struct format_tree *ft)
|
|
|
|
{
|
|
|
|
return (ft->wp);
|
|
|
|
}
|
|
|
|
|
2019-05-26 12:02:42 +00:00
|
|
|
/* Add item bits to tree. */
|
|
|
|
static void
|
|
|
|
format_create_add_item(struct format_tree *ft, struct cmdq_item *item)
|
|
|
|
{
|
2020-04-13 14:46:04 +00:00
|
|
|
struct key_event *event = cmdq_get_event(item);
|
|
|
|
struct mouse_event *m = &event->m;
|
2019-05-26 17:34:45 +00:00
|
|
|
|
2020-04-13 10:59:58 +00:00
|
|
|
cmdq_merge_formats(item, ft);
|
2019-05-26 17:34:45 +00:00
|
|
|
memcpy(&ft->m, m, sizeof ft->m);
|
2019-05-26 12:02:42 +00:00
|
|
|
}
|
|
|
|
|
2011-08-26 10:53:16 +00:00
|
|
|
/* Create a new tree. */
|
|
|
|
struct format_tree *
|
2017-05-01 12:20:55 +00:00
|
|
|
format_create(struct client *c, struct cmdq_item *item, int tag, int flags)
|
2011-08-26 10:53:16 +00:00
|
|
|
{
|
2021-02-22 07:09:06 +00:00
|
|
|
struct format_tree *ft;
|
2011-08-26 10:53:16 +00:00
|
|
|
|
2014-12-02 23:19:45 +00:00
|
|
|
ft = xcalloc(1, sizeof *ft);
|
|
|
|
RB_INIT(&ft->tree);
|
2017-02-03 11:57:27 +00:00
|
|
|
|
2017-05-01 12:20:55 +00:00
|
|
|
if (c != NULL) {
|
|
|
|
ft->client = c;
|
|
|
|
ft->client->references++;
|
|
|
|
}
|
2019-03-13 15:37:28 +00:00
|
|
|
ft->item = item;
|
2017-05-01 12:20:55 +00:00
|
|
|
|
2017-02-03 11:57:27 +00:00
|
|
|
ft->tag = tag;
|
2015-09-14 10:25:52 +00:00
|
|
|
ft->flags = flags;
|
2011-08-26 10:53:16 +00:00
|
|
|
|
2019-05-26 12:02:42 +00:00
|
|
|
if (item != NULL)
|
|
|
|
format_create_add_item(ft, item);
|
2015-12-11 12:27:36 +00:00
|
|
|
|
2011-08-26 10:53:16 +00:00
|
|
|
return (ft);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free a tree. */
|
|
|
|
void
|
|
|
|
format_free(struct format_tree *ft)
|
|
|
|
{
|
2014-12-02 23:19:45 +00:00
|
|
|
struct format_entry *fe, *fe1;
|
2011-08-26 10:53:16 +00:00
|
|
|
|
2015-05-27 13:28:04 +00:00
|
|
|
RB_FOREACH_SAFE(fe, format_entry_tree, &ft->tree, fe1) {
|
|
|
|
RB_REMOVE(format_entry_tree, &ft->tree, fe);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(fe->value);
|
|
|
|
free(fe->key);
|
|
|
|
free(fe);
|
2011-08-26 10:53:16 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 12:20:55 +00:00
|
|
|
if (ft->client != NULL)
|
|
|
|
server_client_unref(ft->client);
|
2013-05-31 19:46:42 +00:00
|
|
|
free(ft);
|
2011-08-26 10:53:16 +00:00
|
|
|
}
|
|
|
|
|
2021-08-12 08:05:11 +00:00
|
|
|
/* Log each format. */
|
|
|
|
static void
|
|
|
|
format_log_debug_cb(const char *key, const char *value, void *arg)
|
|
|
|
{
|
|
|
|
const char *prefix = arg;
|
|
|
|
|
|
|
|
log_debug("%s: %s=%s", prefix, key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Log a format tree. */
|
|
|
|
void
|
|
|
|
format_log_debug(struct format_tree *ft, const char *prefix)
|
|
|
|
{
|
2021-08-12 08:10:20 +00:00
|
|
|
format_each(ft, format_log_debug_cb, (void *)prefix);
|
2021-08-12 08:05:11 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 14:10:25 +00:00
|
|
|
/* Walk each format. */
|
|
|
|
void
|
|
|
|
format_each(struct format_tree *ft, void (*cb)(const char *, const char *,
|
|
|
|
void *), void *arg)
|
|
|
|
{
|
2021-02-22 07:09:06 +00:00
|
|
|
const struct format_table_entry *fte;
|
|
|
|
struct format_entry *fe;
|
|
|
|
u_int i;
|
|
|
|
char s[64];
|
|
|
|
void *value;
|
|
|
|
struct timeval *tv;
|
|
|
|
|
|
|
|
for (i = 0; i < nitems(format_table); i++) {
|
|
|
|
fte = &format_table[i];
|
2019-03-18 14:10:25 +00:00
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
value = fte->cb(ft);
|
|
|
|
if (value == NULL)
|
|
|
|
continue;
|
|
|
|
if (fte->type == FORMAT_TABLE_TIME) {
|
|
|
|
tv = value;
|
|
|
|
xsnprintf(s, sizeof s, "%lld", (long long)tv->tv_sec);
|
|
|
|
cb(fte->key, s, arg);
|
|
|
|
} else {
|
|
|
|
cb(fte->key, value, arg);
|
|
|
|
free(value);
|
|
|
|
}
|
|
|
|
}
|
2019-03-18 14:10:25 +00:00
|
|
|
RB_FOREACH(fe, format_entry_tree, &ft->tree) {
|
2020-10-06 07:36:05 +00:00
|
|
|
if (fe->time != 0) {
|
|
|
|
xsnprintf(s, sizeof s, "%lld", (long long)fe->time);
|
2019-12-26 14:48:29 +00:00
|
|
|
cb(fe->key, s, arg);
|
2019-03-18 14:10:25 +00:00
|
|
|
} else {
|
|
|
|
if (fe->value == NULL && fe->cb != NULL) {
|
2020-06-01 19:39:25 +00:00
|
|
|
fe->value = fe->cb(ft);
|
2019-03-18 14:10:25 +00:00
|
|
|
if (fe->value == NULL)
|
|
|
|
fe->value = xstrdup("");
|
|
|
|
}
|
|
|
|
cb(fe->key, fe->value, arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-26 10:53:16 +00:00
|
|
|
/* Add a key-value pair. */
|
|
|
|
void
|
|
|
|
format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
struct format_entry *fe;
|
2013-10-10 11:47:52 +00:00
|
|
|
struct format_entry *fe_now;
|
2011-08-26 10:53:16 +00:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
fe = xmalloc(sizeof *fe);
|
|
|
|
fe->key = xstrdup(key);
|
|
|
|
|
2015-08-28 16:46:40 +00:00
|
|
|
fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
|
|
|
|
if (fe_now != NULL) {
|
|
|
|
free(fe->key);
|
|
|
|
free(fe);
|
|
|
|
free(fe_now->value);
|
|
|
|
fe = fe_now;
|
|
|
|
}
|
|
|
|
|
|
|
|
fe->cb = NULL;
|
2020-10-06 07:36:05 +00:00
|
|
|
fe->time = 0;
|
2015-08-28 16:46:40 +00:00
|
|
|
|
2011-08-26 10:53:16 +00:00
|
|
|
va_start(ap, fmt);
|
|
|
|
xvasprintf(&fe->value, fmt, ap);
|
|
|
|
va_end(ap);
|
2015-08-28 16:46:40 +00:00
|
|
|
}
|
|
|
|
|
2015-10-25 22:29:17 +00:00
|
|
|
/* Add a key and time. */
|
2020-05-16 15:47:22 +00:00
|
|
|
void
|
2015-10-25 22:29:17 +00:00
|
|
|
format_add_tv(struct format_tree *ft, const char *key, struct timeval *tv)
|
|
|
|
{
|
2019-12-26 14:48:29 +00:00
|
|
|
struct format_entry *fe, *fe_now;
|
2015-10-25 22:29:17 +00:00
|
|
|
|
|
|
|
fe = xmalloc(sizeof *fe);
|
|
|
|
fe->key = xstrdup(key);
|
|
|
|
|
|
|
|
fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
|
|
|
|
if (fe_now != NULL) {
|
|
|
|
free(fe->key);
|
|
|
|
free(fe);
|
|
|
|
free(fe_now->value);
|
|
|
|
fe = fe_now;
|
|
|
|
}
|
|
|
|
|
|
|
|
fe->cb = NULL;
|
2020-10-06 07:36:05 +00:00
|
|
|
fe->time = tv->tv_sec;
|
2015-10-25 22:29:17 +00:00
|
|
|
|
|
|
|
fe->value = NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:46:40 +00:00
|
|
|
/* Add a key and function. */
|
2020-06-01 19:39:25 +00:00
|
|
|
void
|
2015-08-28 16:46:40 +00:00
|
|
|
format_add_cb(struct format_tree *ft, const char *key, format_cb cb)
|
|
|
|
{
|
|
|
|
struct format_entry *fe;
|
|
|
|
struct format_entry *fe_now;
|
|
|
|
|
|
|
|
fe = xmalloc(sizeof *fe);
|
|
|
|
fe->key = xstrdup(key);
|
2011-08-26 10:53:16 +00:00
|
|
|
|
2015-05-27 13:28:04 +00:00
|
|
|
fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
|
2013-10-10 11:47:52 +00:00
|
|
|
if (fe_now != NULL) {
|
|
|
|
free(fe->key);
|
|
|
|
free(fe);
|
2015-08-28 16:46:40 +00:00
|
|
|
free(fe_now->value);
|
|
|
|
fe = fe_now;
|
2013-10-10 11:47:52 +00:00
|
|
|
}
|
2015-08-28 16:46:40 +00:00
|
|
|
|
|
|
|
fe->cb = cb;
|
2020-10-06 07:36:05 +00:00
|
|
|
fe->time = 0;
|
2015-08-28 16:46:40 +00:00
|
|
|
|
|
|
|
fe->value = NULL;
|
2011-08-26 10:53:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 07:16:54 +00:00
|
|
|
/* Quote shell special characters in string. */
|
2018-08-26 09:28:42 +00:00
|
|
|
static char *
|
2021-01-20 07:16:54 +00:00
|
|
|
format_quote_shell(const char *s)
|
2018-08-26 09:28:42 +00:00
|
|
|
{
|
|
|
|
const char *cp;
|
|
|
|
char *out, *at;
|
|
|
|
|
|
|
|
at = out = xmalloc(strlen(s) * 2 + 1);
|
|
|
|
for (cp = s; *cp != '\0'; cp++) {
|
|
|
|
if (strchr("|&;<>()$`\\\"'*?[# =%", *cp) != NULL)
|
|
|
|
*at++ = '\\';
|
|
|
|
*at++ = *cp;
|
|
|
|
}
|
|
|
|
*at = '\0';
|
|
|
|
return (out);
|
|
|
|
}
|
|
|
|
|
2021-01-20 07:16:54 +00:00
|
|
|
/* Quote #s in string. */
|
2020-12-01 08:12:58 +00:00
|
|
|
static char *
|
2021-01-20 07:16:54 +00:00
|
|
|
format_quote_style(const char *s)
|
2020-12-01 08:12:58 +00:00
|
|
|
{
|
|
|
|
const char *cp;
|
|
|
|
char *out, *at;
|
|
|
|
|
|
|
|
at = out = xmalloc(strlen(s) * 2 + 1);
|
|
|
|
for (cp = s; *cp != '\0'; cp++) {
|
|
|
|
if (*cp == '#')
|
|
|
|
*at++ = '#';
|
|
|
|
*at++ = *cp;
|
|
|
|
}
|
|
|
|
*at = '\0';
|
|
|
|
return (out);
|
|
|
|
}
|
|
|
|
|
2020-05-16 14:10:29 +00:00
|
|
|
/* Make a prettier time. */
|
2022-06-21 09:30:01 +00:00
|
|
|
char *
|
|
|
|
format_pretty_time(time_t t, int seconds)
|
2020-05-16 14:10:29 +00:00
|
|
|
{
|
|
|
|
struct tm now_tm, tm;
|
|
|
|
time_t now, age;
|
2022-06-21 09:30:01 +00:00
|
|
|
char s[9];
|
2020-05-16 14:10:29 +00:00
|
|
|
|
|
|
|
time(&now);
|
|
|
|
if (now < t)
|
|
|
|
now = t;
|
|
|
|
age = now - t;
|
|
|
|
|
|
|
|
localtime_r(&now, &now_tm);
|
|
|
|
localtime_r(&t, &tm);
|
|
|
|
|
|
|
|
/* Last 24 hours. */
|
|
|
|
if (age < 24 * 3600) {
|
2022-06-21 09:30:01 +00:00
|
|
|
if (seconds)
|
|
|
|
strftime(s, sizeof s, "%H:%M:%S", &tm);
|
|
|
|
else
|
|
|
|
strftime(s, sizeof s, "%H:%M", &tm);
|
2020-05-16 14:10:29 +00:00
|
|
|
return (xstrdup(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This month or last 28 days. */
|
|
|
|
if ((tm.tm_year == now_tm.tm_year && tm.tm_mon == now_tm.tm_mon) ||
|
|
|
|
age < 28 * 24 * 3600) {
|
|
|
|
strftime(s, sizeof s, "%a%d", &tm);
|
|
|
|
return (xstrdup(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Last 12 months. */
|
|
|
|
if ((tm.tm_year == now_tm.tm_year && tm.tm_mon < now_tm.tm_mon) ||
|
|
|
|
(tm.tm_year == now_tm.tm_year - 1 && tm.tm_mon > now_tm.tm_mon)) {
|
|
|
|
strftime(s, sizeof s, "%d%b", &tm);
|
|
|
|
return (xstrdup(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Older than that. */
|
|
|
|
strftime(s, sizeof s, "%h%y", &tm);
|
|
|
|
return (xstrdup(s));
|
|
|
|
}
|
|
|
|
|
2011-08-26 10:53:16 +00:00
|
|
|
/* Find a format entry. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static char *
|
2020-05-16 15:48:35 +00:00
|
|
|
format_find(struct format_tree *ft, const char *key, int modifiers,
|
|
|
|
const char *time_format)
|
2011-08-26 10:53:16 +00:00
|
|
|
{
|
2021-02-22 07:09:06 +00:00
|
|
|
struct format_table_entry *fte;
|
|
|
|
void *value;
|
|
|
|
struct format_entry *fe, fe_find;
|
|
|
|
struct environ_entry *envent;
|
|
|
|
struct options_entry *o;
|
|
|
|
int idx;
|
|
|
|
char *found = NULL, *saved, s[512];
|
|
|
|
const char *errstr;
|
|
|
|
time_t t = 0;
|
|
|
|
struct tm tm;
|
2020-05-16 14:10:29 +00:00
|
|
|
|
|
|
|
o = options_parse_get(global_options, key, &idx, 0);
|
|
|
|
if (o == NULL && ft->wp != NULL)
|
|
|
|
o = options_parse_get(ft->wp->options, key, &idx, 0);
|
|
|
|
if (o == NULL && ft->w != NULL)
|
|
|
|
o = options_parse_get(ft->w->options, key, &idx, 0);
|
|
|
|
if (o == NULL)
|
|
|
|
o = options_parse_get(global_w_options, key, &idx, 0);
|
|
|
|
if (o == NULL && ft->s != NULL)
|
|
|
|
o = options_parse_get(ft->s->options, key, &idx, 0);
|
|
|
|
if (o == NULL)
|
|
|
|
o = options_parse_get(global_s_options, key, &idx, 0);
|
|
|
|
if (o != NULL) {
|
2020-05-16 16:02:24 +00:00
|
|
|
found = options_to_string(o, idx, 1);
|
2020-05-16 14:10:29 +00:00
|
|
|
goto found;
|
2014-12-02 23:19:45 +00:00
|
|
|
}
|
2011-08-26 10:53:16 +00:00
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
fte = format_table_get(key);
|
|
|
|
if (fte != NULL) {
|
|
|
|
value = fte->cb(ft);
|
2021-10-05 12:45:02 +00:00
|
|
|
if (fte->type == FORMAT_TABLE_TIME && value != NULL)
|
2021-02-22 07:09:06 +00:00
|
|
|
t = ((struct timeval *)value)->tv_sec;
|
|
|
|
else
|
|
|
|
found = value;
|
|
|
|
goto found;
|
|
|
|
}
|
2020-05-16 14:10:29 +00:00
|
|
|
fe_find.key = (char *)key;
|
2015-05-27 13:28:04 +00:00
|
|
|
fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find);
|
2015-08-28 16:46:40 +00:00
|
|
|
if (fe != NULL) {
|
2020-10-06 07:36:05 +00:00
|
|
|
if (fe->time != 0) {
|
|
|
|
t = fe->time;
|
2015-10-25 22:29:17 +00:00
|
|
|
goto found;
|
|
|
|
}
|
2020-06-01 19:39:25 +00:00
|
|
|
if (fe->value == NULL && fe->cb != NULL) {
|
|
|
|
fe->value = fe->cb(ft);
|
|
|
|
if (fe->value == NULL)
|
|
|
|
fe->value = xstrdup("");
|
|
|
|
}
|
2019-04-25 18:18:55 +00:00
|
|
|
found = xstrdup(fe->value);
|
2015-10-25 22:29:17 +00:00
|
|
|
goto found;
|
2015-08-28 16:46:40 +00:00
|
|
|
}
|
2015-08-28 10:06:52 +00:00
|
|
|
|
2015-10-25 22:29:17 +00:00
|
|
|
if (~modifiers & FORMAT_TIMESTRING) {
|
|
|
|
envent = NULL;
|
|
|
|
if (ft->s != NULL)
|
2015-10-28 09:51:55 +00:00
|
|
|
envent = environ_find(ft->s->environ, key);
|
2015-10-25 22:29:17 +00:00
|
|
|
if (envent == NULL)
|
2015-10-28 09:51:55 +00:00
|
|
|
envent = environ_find(global_environ, key);
|
2019-06-13 21:24:09 +00:00
|
|
|
if (envent != NULL && envent->value != NULL) {
|
2019-04-25 18:18:55 +00:00
|
|
|
found = xstrdup(envent->value);
|
2015-10-25 22:29:17 +00:00
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
2015-08-28 10:06:52 +00:00
|
|
|
|
|
|
|
return (NULL);
|
2015-10-25 22:29:17 +00:00
|
|
|
|
|
|
|
found:
|
2020-05-16 14:10:29 +00:00
|
|
|
if (modifiers & FORMAT_TIMESTRING) {
|
|
|
|
if (t == 0 && found != NULL) {
|
|
|
|
t = strtonum(found, 0, INT64_MAX, &errstr);
|
|
|
|
if (errstr != NULL)
|
|
|
|
t = 0;
|
|
|
|
free(found);
|
|
|
|
}
|
|
|
|
if (t == 0)
|
|
|
|
return (NULL);
|
|
|
|
if (modifiers & FORMAT_PRETTY)
|
2022-06-21 09:30:01 +00:00
|
|
|
found = format_pretty_time(t, 0);
|
2020-05-16 14:10:29 +00:00
|
|
|
else {
|
2020-05-16 15:48:35 +00:00
|
|
|
if (time_format != NULL) {
|
|
|
|
localtime_r(&t, &tm);
|
|
|
|
strftime(s, sizeof s, time_format, &tm);
|
|
|
|
} else {
|
|
|
|
ctime_r(&t, s);
|
|
|
|
s[strcspn(s, "\n")] = '\0';
|
|
|
|
}
|
2020-05-16 14:10:29 +00:00
|
|
|
found = xstrdup(s);
|
|
|
|
}
|
|
|
|
return (found);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t != 0)
|
|
|
|
xasprintf(&found, "%lld", (long long)t);
|
|
|
|
else if (found == NULL)
|
2015-10-27 09:18:06 +00:00
|
|
|
return (NULL);
|
2015-10-25 22:29:17 +00:00
|
|
|
if (modifiers & FORMAT_BASENAME) {
|
2019-04-25 18:18:55 +00:00
|
|
|
saved = found;
|
|
|
|
found = xstrdup(basename(saved));
|
2015-10-25 22:29:17 +00:00
|
|
|
free(saved);
|
|
|
|
}
|
|
|
|
if (modifiers & FORMAT_DIRNAME) {
|
2019-04-25 18:18:55 +00:00
|
|
|
saved = found;
|
|
|
|
found = xstrdup(dirname(saved));
|
2015-10-25 22:29:17 +00:00
|
|
|
free(saved);
|
|
|
|
}
|
2021-01-20 07:16:54 +00:00
|
|
|
if (modifiers & FORMAT_QUOTE_SHELL) {
|
2019-04-25 18:18:55 +00:00
|
|
|
saved = found;
|
2022-07-19 06:46:57 +00:00
|
|
|
found = format_quote_shell(saved);
|
2018-08-26 09:28:42 +00:00
|
|
|
free(saved);
|
|
|
|
}
|
2021-01-20 07:16:54 +00:00
|
|
|
if (modifiers & FORMAT_QUOTE_STYLE) {
|
2020-12-01 08:12:58 +00:00
|
|
|
saved = found;
|
2022-07-19 06:46:57 +00:00
|
|
|
found = format_quote_style(saved);
|
2020-12-01 08:12:58 +00:00
|
|
|
free(saved);
|
|
|
|
}
|
2019-04-25 18:18:55 +00:00
|
|
|
return (found);
|
2011-08-26 10:53:16 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 08:03:23 +00:00
|
|
|
/* Unescape escaped characters. */
|
|
|
|
static char *
|
|
|
|
format_unescape(const char *s)
|
|
|
|
{
|
|
|
|
char *out, *cp;
|
|
|
|
int brackets = 0;
|
|
|
|
|
|
|
|
cp = out = xmalloc(strlen(s) + 1);
|
|
|
|
for (; *s != '\0'; s++) {
|
|
|
|
if (*s == '#' && s[1] == '{')
|
|
|
|
brackets++;
|
|
|
|
if (brackets == 0 &&
|
|
|
|
*s == '#' &&
|
|
|
|
strchr(",#{}:", s[1]) != NULL) {
|
|
|
|
*cp++ = *++s;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*s == '}')
|
|
|
|
brackets--;
|
|
|
|
*cp++ = *s;
|
|
|
|
}
|
|
|
|
*cp = '\0';
|
|
|
|
return (out);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove escaped characters. */
|
2020-05-16 15:48:35 +00:00
|
|
|
static char *
|
|
|
|
format_strip(const char *s)
|
|
|
|
{
|
|
|
|
char *out, *cp;
|
|
|
|
int brackets = 0;
|
|
|
|
|
|
|
|
cp = out = xmalloc(strlen(s) + 1);
|
|
|
|
for (; *s != '\0'; s++) {
|
|
|
|
if (*s == '#' && s[1] == '{')
|
|
|
|
brackets++;
|
|
|
|
if (*s == '#' && strchr(",#{}:", s[1]) != NULL) {
|
|
|
|
if (brackets != 0)
|
|
|
|
*cp++ = *s;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*s == '}')
|
|
|
|
brackets--;
|
|
|
|
*cp++ = *s;
|
|
|
|
}
|
|
|
|
*cp = '\0';
|
|
|
|
return (out);
|
|
|
|
}
|
|
|
|
|
2018-05-22 08:49:12 +00:00
|
|
|
/* Skip until end. */
|
2019-03-18 20:53:33 +00:00
|
|
|
const char *
|
2019-03-13 14:10:34 +00:00
|
|
|
format_skip(const char *s, const char *end)
|
2017-01-09 21:03:25 +00:00
|
|
|
{
|
|
|
|
int brackets = 0;
|
|
|
|
|
|
|
|
for (; *s != '\0'; s++) {
|
2018-05-22 08:49:12 +00:00
|
|
|
if (*s == '#' && s[1] == '{')
|
2017-01-09 21:03:25 +00:00
|
|
|
brackets++;
|
2023-07-03 10:48:26 +00:00
|
|
|
if (*s == '#' &&
|
|
|
|
s[1] != '\0' &&
|
|
|
|
strchr(",#{}:", s[1]) != NULL) {
|
2018-05-22 08:49:12 +00:00
|
|
|
s++;
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-09 21:03:25 +00:00
|
|
|
if (*s == '}')
|
|
|
|
brackets--;
|
2019-03-13 14:10:34 +00:00
|
|
|
if (strchr(end, *s) != NULL && brackets == 0)
|
2017-01-09 21:03:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*s == '\0')
|
|
|
|
return (NULL);
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return left and right alternatives separated by commas. */
|
|
|
|
static int
|
2020-10-06 07:36:05 +00:00
|
|
|
format_choose(struct format_expand_state *es, const char *s, char **left,
|
|
|
|
char **right, int expand)
|
2017-01-09 21:03:25 +00:00
|
|
|
{
|
2019-03-13 14:10:34 +00:00
|
|
|
const char *cp;
|
|
|
|
char *left0, *right0;
|
2017-01-09 21:03:25 +00:00
|
|
|
|
2019-03-13 14:10:34 +00:00
|
|
|
cp = format_skip(s, ",");
|
2017-01-09 21:03:25 +00:00
|
|
|
if (cp == NULL)
|
|
|
|
return (-1);
|
2019-03-13 14:10:34 +00:00
|
|
|
left0 = xstrndup(s, cp - s);
|
|
|
|
right0 = xstrdup(cp + 1);
|
2017-01-09 21:03:25 +00:00
|
|
|
|
2019-03-13 14:10:34 +00:00
|
|
|
if (expand) {
|
2020-10-06 07:36:05 +00:00
|
|
|
*left = format_expand1(es, left0);
|
2019-03-29 09:33:24 +00:00
|
|
|
free(left0);
|
2020-10-06 07:36:05 +00:00
|
|
|
*right = format_expand1(es, right0);
|
2019-03-29 09:33:24 +00:00
|
|
|
free(right0);
|
2019-03-13 14:10:34 +00:00
|
|
|
} else {
|
|
|
|
*left = left0;
|
|
|
|
*right = right0;
|
|
|
|
}
|
2017-01-09 21:03:25 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is this true? */
|
Rewrite of choose mode, both to simplify and tidy the code and to add
some modern features.
Now the common code is in mode-tree.c, which provides an API used by the
three modes now separated into window-{buffer,client,tree}.c. Buffer
mode shows buffers, client mode clients and tree mode a tree of
sessions, windows and panes.
Each mode has a common set of key bindings plus a few that are specific
to the mode. Other changes are:
- each mode has a preview pane: for buffers this is the buffer content
(very useful), for others it is a preview of the pane;
- items may be sorted in different ways ('O' key);
- multiple items may be tagged and an operation applied to all of them
(for example, to delete multiple buffers at once);
- in tree mode a command may be run on the selected item (session,
window, pane) or on tagged items (key ':');
- displayed items may be filtered in tree mode by using a format (this
is used to implement find-window) (key 'f');
- the custom format (-F) for the display is no longer available;
- shortcut keys change from 0-9, a-z, A-Z which was always a bit weird
with keys used for other uses to 0-9, M-a to M-z.
Now that the code is simpler, other improvements will come later.
Primary key bindings for each mode are documented under the commands in
the man page (choose-buffer, choose-client, choose-tree).
Parts written by Thomas Adam.
2017-05-30 21:44:59 +00:00
|
|
|
int
|
2017-01-09 21:03:25 +00:00
|
|
|
format_true(const char *s)
|
|
|
|
{
|
|
|
|
if (s != NULL && *s != '\0' && (s[0] != '0' || s[1] != '\0'))
|
|
|
|
return (1);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2019-03-13 14:10:34 +00:00
|
|
|
/* Check if modifier end. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static int
|
2019-03-13 14:10:34 +00:00
|
|
|
format_is_end(char c)
|
2011-08-26 10:53:16 +00:00
|
|
|
{
|
2019-03-13 14:10:34 +00:00
|
|
|
return (c == ';' || c == ':');
|
|
|
|
}
|
2011-08-26 10:53:16 +00:00
|
|
|
|
2019-03-13 14:10:34 +00:00
|
|
|
/* Add to modifier list. */
|
|
|
|
static void
|
|
|
|
format_add_modifier(struct format_modifier **list, u_int *count,
|
|
|
|
const char *c, size_t n, char **argv, int argc)
|
|
|
|
{
|
|
|
|
struct format_modifier *fm;
|
|
|
|
|
|
|
|
*list = xreallocarray(*list, (*count) + 1, sizeof **list);
|
|
|
|
fm = &(*list)[(*count)++];
|
|
|
|
|
|
|
|
memcpy(fm->modifier, c, n);
|
|
|
|
fm->modifier[n] = '\0';
|
|
|
|
fm->size = n;
|
|
|
|
|
|
|
|
fm->argv = argv;
|
|
|
|
fm->argc = argc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free modifier list. */
|
|
|
|
static void
|
|
|
|
format_free_modifiers(struct format_modifier *list, u_int count)
|
|
|
|
{
|
|
|
|
u_int i;
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
cmd_free_argv(list[i].argc, list[i].argv);
|
|
|
|
free(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Build modifier list. */
|
|
|
|
static struct format_modifier *
|
2020-10-06 07:36:05 +00:00
|
|
|
format_build_modifiers(struct format_expand_state *es, const char **s,
|
|
|
|
u_int *count)
|
2019-03-13 14:10:34 +00:00
|
|
|
{
|
|
|
|
const char *cp = *s, *end;
|
|
|
|
struct format_modifier *list = NULL;
|
|
|
|
char c, last[] = "X;:", **argv, *value;
|
|
|
|
int argc;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Modifiers are a ; separated list of the forms:
|
2021-10-25 21:21:16 +00:00
|
|
|
* l,m,C,a,b,c,d,n,t,w,q,E,T,S,W,P,<,>
|
2019-03-13 14:10:34 +00:00
|
|
|
* =a
|
|
|
|
* =/a
|
|
|
|
* =/a/
|
|
|
|
* s/a/b/
|
|
|
|
* s/a/b
|
2019-05-25 16:51:10 +00:00
|
|
|
* ||,&&,!=,==,<=,>=
|
2019-03-13 14:10:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
*count = 0;
|
|
|
|
|
|
|
|
while (*cp != '\0' && *cp != ':') {
|
2020-05-16 15:48:35 +00:00
|
|
|
/* Skip any separator character. */
|
2019-03-13 14:10:34 +00:00
|
|
|
if (*cp == ';')
|
|
|
|
cp++;
|
|
|
|
|
|
|
|
/* Check single character modifiers with no arguments. */
|
2023-02-07 10:21:01 +00:00
|
|
|
if (strchr("labcdnwETSWPL<>", cp[0]) != NULL &&
|
2019-03-13 15:37:28 +00:00
|
|
|
format_is_end(cp[1])) {
|
2019-03-13 14:10:34 +00:00
|
|
|
format_add_modifier(&list, count, cp, 1, NULL, 0);
|
|
|
|
cp++;
|
|
|
|
continue;
|
2017-01-09 21:03:25 +00:00
|
|
|
}
|
2019-03-13 14:10:34 +00:00
|
|
|
|
|
|
|
/* Then try double character with no arguments. */
|
|
|
|
if ((memcmp("||", cp, 2) == 0 ||
|
|
|
|
memcmp("&&", cp, 2) == 0 ||
|
|
|
|
memcmp("!=", cp, 2) == 0 ||
|
2019-05-25 16:51:10 +00:00
|
|
|
memcmp("==", cp, 2) == 0 ||
|
|
|
|
memcmp("<=", cp, 2) == 0 ||
|
|
|
|
memcmp(">=", cp, 2) == 0) &&
|
2019-03-13 14:10:34 +00:00
|
|
|
format_is_end(cp[2])) {
|
|
|
|
format_add_modifier(&list, count, cp, 2, NULL, 0);
|
|
|
|
cp += 2;
|
|
|
|
continue;
|
2017-01-09 21:03:25 +00:00
|
|
|
}
|
2019-03-13 14:10:34 +00:00
|
|
|
|
|
|
|
/* Now try single character with arguments. */
|
2021-02-05 12:23:49 +00:00
|
|
|
if (strchr("mCNst=peq", cp[0]) == NULL)
|
2015-11-18 14:13:55 +00:00
|
|
|
break;
|
2019-03-13 14:10:34 +00:00
|
|
|
c = cp[0];
|
|
|
|
|
|
|
|
/* No arguments provided. */
|
|
|
|
if (format_is_end(cp[1])) {
|
|
|
|
format_add_modifier(&list, count, cp, 1, NULL, 0);
|
|
|
|
cp++;
|
|
|
|
continue;
|
2015-11-18 14:13:55 +00:00
|
|
|
}
|
2019-03-13 14:10:34 +00:00
|
|
|
argv = NULL;
|
|
|
|
argc = 0;
|
|
|
|
|
|
|
|
/* Single argument with no wrapper character. */
|
2023-06-30 13:19:32 +00:00
|
|
|
if (!ispunct((u_char)cp[1]) || cp[1] == '-') {
|
2019-03-13 14:10:34 +00:00
|
|
|
end = format_skip(cp + 1, ":;");
|
|
|
|
if (end == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
argv = xcalloc(1, sizeof *argv);
|
|
|
|
value = xstrndup(cp + 1, end - (cp + 1));
|
2020-10-06 07:36:05 +00:00
|
|
|
argv[0] = format_expand1(es, value);
|
2019-03-13 14:10:34 +00:00
|
|
|
free(value);
|
|
|
|
argc = 1;
|
|
|
|
|
|
|
|
format_add_modifier(&list, count, &c, 1, argv, argc);
|
|
|
|
cp = end;
|
|
|
|
continue;
|
2015-11-18 14:13:55 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 14:10:34 +00:00
|
|
|
/* Multiple arguments with a wrapper character. */
|
|
|
|
last[0] = cp[1];
|
|
|
|
cp++;
|
|
|
|
do {
|
|
|
|
if (cp[0] == last[0] && format_is_end(cp[1])) {
|
|
|
|
cp++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
end = format_skip(cp + 1, last);
|
|
|
|
if (end == NULL)
|
|
|
|
break;
|
|
|
|
cp++;
|
|
|
|
|
2021-08-20 20:04:22 +00:00
|
|
|
argv = xreallocarray(argv, argc + 1, sizeof *argv);
|
2019-03-13 14:10:34 +00:00
|
|
|
value = xstrndup(cp, end - cp);
|
2020-10-06 07:36:05 +00:00
|
|
|
argv[argc++] = format_expand1(es, value);
|
2019-03-13 14:10:34 +00:00
|
|
|
free(value);
|
|
|
|
|
|
|
|
cp = end;
|
|
|
|
} while (!format_is_end(cp[0]));
|
|
|
|
format_add_modifier(&list, count, &c, 1, argv, argc);
|
|
|
|
}
|
|
|
|
if (*cp != ':') {
|
|
|
|
format_free_modifiers(list, *count);
|
|
|
|
*count = 0;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
*s = cp + 1;
|
2020-04-09 15:35:27 +00:00
|
|
|
return (list);
|
2019-03-13 14:10:34 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 19:46:00 +00:00
|
|
|
/* Match against an fnmatch(3) pattern or regular expression. */
|
2019-03-13 14:10:34 +00:00
|
|
|
static char *
|
2019-06-13 19:46:00 +00:00
|
|
|
format_match(struct format_modifier *fm, const char *pattern, const char *text)
|
2019-03-13 14:10:34 +00:00
|
|
|
{
|
2019-06-13 19:46:00 +00:00
|
|
|
const char *s = "";
|
|
|
|
regex_t r;
|
|
|
|
int flags = 0;
|
|
|
|
|
|
|
|
if (fm->argc >= 1)
|
|
|
|
s = fm->argv[0];
|
|
|
|
if (strchr(s, 'r') == NULL) {
|
|
|
|
if (strchr(s, 'i') != NULL)
|
|
|
|
flags |= FNM_CASEFOLD;
|
|
|
|
if (fnmatch(pattern, text, flags) != 0)
|
|
|
|
return (xstrdup("0"));
|
|
|
|
} else {
|
|
|
|
flags = REG_EXTENDED|REG_NOSUB;
|
|
|
|
if (strchr(s, 'i') != NULL)
|
|
|
|
flags |= REG_ICASE;
|
|
|
|
if (regcomp(&r, pattern, flags) != 0)
|
|
|
|
return (xstrdup("0"));
|
|
|
|
if (regexec(&r, text, 0, NULL, 0) != 0) {
|
|
|
|
regfree(&r);
|
|
|
|
return (xstrdup("0"));
|
2019-03-13 14:10:34 +00:00
|
|
|
}
|
2019-06-13 19:46:00 +00:00
|
|
|
regfree(&r);
|
|
|
|
}
|
|
|
|
return (xstrdup("1"));
|
|
|
|
}
|
2019-03-13 14:10:34 +00:00
|
|
|
|
2019-06-13 19:46:00 +00:00
|
|
|
/* Perform substitution in string. */
|
|
|
|
static char *
|
|
|
|
format_sub(struct format_modifier *fm, const char *text, const char *pattern,
|
|
|
|
const char *with)
|
|
|
|
{
|
|
|
|
char *value;
|
|
|
|
int flags = REG_EXTENDED;
|
|
|
|
|
|
|
|
if (fm->argc >= 3 && strchr(fm->argv[2], 'i') != NULL)
|
|
|
|
flags |= REG_ICASE;
|
|
|
|
value = regsub(pattern, with, text, flags);
|
|
|
|
if (value == NULL)
|
|
|
|
return (xstrdup(text));
|
|
|
|
return (value);
|
|
|
|
}
|
2019-03-13 14:10:34 +00:00
|
|
|
|
2019-06-13 19:46:00 +00:00
|
|
|
/* Search inside pane. */
|
|
|
|
static char *
|
|
|
|
format_search(struct format_modifier *fm, struct window_pane *wp, const char *s)
|
|
|
|
{
|
|
|
|
int ignore = 0, regex = 0;
|
|
|
|
char *value;
|
|
|
|
|
|
|
|
if (fm->argc >= 1) {
|
|
|
|
if (strchr(fm->argv[0], 'i') != NULL)
|
|
|
|
ignore = 1;
|
|
|
|
if (strchr(fm->argv[0], 'r') != NULL)
|
|
|
|
regex = 1;
|
2019-03-13 14:10:34 +00:00
|
|
|
}
|
2019-06-13 19:46:00 +00:00
|
|
|
xasprintf(&value, "%u", window_pane_search(wp, s, regex, ignore));
|
|
|
|
return (value);
|
2019-03-13 14:10:34 +00:00
|
|
|
}
|
|
|
|
|
2021-02-05 12:23:49 +00:00
|
|
|
/* Does session name exist? */
|
|
|
|
static char *
|
|
|
|
format_session_name(struct format_expand_state *es, const char *fmt)
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
struct session *s;
|
|
|
|
|
|
|
|
name = format_expand1(es, fmt);
|
|
|
|
RB_FOREACH(s, sessions, &sessions) {
|
|
|
|
if (strcmp(s->name, name) == 0) {
|
|
|
|
free(name);
|
|
|
|
return (xstrdup("1"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(name);
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
|
2019-03-13 15:37:28 +00:00
|
|
|
/* Loop over sessions. */
|
|
|
|
static char *
|
2020-10-06 07:36:05 +00:00
|
|
|
format_loop_sessions(struct format_expand_state *es, const char *fmt)
|
2019-03-13 15:37:28 +00:00
|
|
|
{
|
2020-10-06 07:36:05 +00:00
|
|
|
struct format_tree *ft = es->ft;
|
|
|
|
struct client *c = ft->client;
|
|
|
|
struct cmdq_item *item = ft->item;
|
|
|
|
struct format_tree *nft;
|
|
|
|
struct format_expand_state next;
|
|
|
|
char *expanded, *value;
|
|
|
|
size_t valuelen;
|
|
|
|
struct session *s;
|
2019-03-13 15:37:28 +00:00
|
|
|
|
|
|
|
value = xcalloc(1, 1);
|
|
|
|
valuelen = 1;
|
|
|
|
|
|
|
|
RB_FOREACH(s, sessions, &sessions) {
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "session loop: $%u", s->id);
|
2019-03-15 10:07:24 +00:00
|
|
|
nft = format_create(c, item, FORMAT_NONE, ft->flags);
|
2020-12-30 18:29:40 +00:00
|
|
|
format_defaults(nft, ft->c, s, NULL, NULL);
|
2020-10-06 07:36:05 +00:00
|
|
|
format_copy_state(&next, es, 0);
|
|
|
|
next.ft = nft;
|
|
|
|
expanded = format_expand1(&next, fmt);
|
|
|
|
format_free(next.ft);
|
2019-03-13 15:37:28 +00:00
|
|
|
|
|
|
|
valuelen += strlen(expanded);
|
|
|
|
value = xrealloc(value, valuelen);
|
|
|
|
|
|
|
|
strlcat(value, expanded, valuelen);
|
|
|
|
free(expanded);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (value);
|
|
|
|
}
|
|
|
|
|
2021-02-05 12:23:49 +00:00
|
|
|
/* Does window name exist? */
|
|
|
|
static char *
|
|
|
|
format_window_name(struct format_expand_state *es, const char *fmt)
|
|
|
|
{
|
|
|
|
struct format_tree *ft = es->ft;
|
|
|
|
char *name;
|
|
|
|
struct winlink *wl;
|
|
|
|
|
|
|
|
if (ft->s == NULL) {
|
|
|
|
format_log(es, "window name but no session");
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
name = format_expand1(es, fmt);
|
|
|
|
RB_FOREACH(wl, winlinks, &ft->s->windows) {
|
|
|
|
if (strcmp(wl->window->name, name) == 0) {
|
|
|
|
free(name);
|
|
|
|
return (xstrdup("1"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(name);
|
|
|
|
return (xstrdup("0"));
|
|
|
|
}
|
|
|
|
|
2019-03-13 15:37:28 +00:00
|
|
|
/* Loop over windows. */
|
|
|
|
static char *
|
2020-10-06 07:36:05 +00:00
|
|
|
format_loop_windows(struct format_expand_state *es, const char *fmt)
|
|
|
|
{
|
|
|
|
struct format_tree *ft = es->ft;
|
|
|
|
struct client *c = ft->client;
|
|
|
|
struct cmdq_item *item = ft->item;
|
|
|
|
struct format_tree *nft;
|
|
|
|
struct format_expand_state next;
|
|
|
|
char *all, *active, *use, *expanded, *value;
|
|
|
|
size_t valuelen;
|
|
|
|
struct winlink *wl;
|
|
|
|
struct window *w;
|
2019-03-13 15:37:28 +00:00
|
|
|
|
2019-03-15 10:04:13 +00:00
|
|
|
if (ft->s == NULL) {
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "window loop but no session");
|
2019-03-13 15:37:28 +00:00
|
|
|
return (NULL);
|
2019-03-15 10:04:13 +00:00
|
|
|
}
|
2019-03-13 15:37:28 +00:00
|
|
|
|
2020-10-06 07:36:05 +00:00
|
|
|
if (format_choose(es, fmt, &all, &active, 0) != 0) {
|
2019-03-13 15:37:28 +00:00
|
|
|
all = xstrdup(fmt);
|
|
|
|
active = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = xcalloc(1, 1);
|
|
|
|
valuelen = 1;
|
|
|
|
|
|
|
|
RB_FOREACH(wl, winlinks, &ft->s->windows) {
|
2019-03-15 10:07:24 +00:00
|
|
|
w = wl->window;
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "window loop: %u @%u", wl->idx, w->id);
|
2019-03-13 15:37:28 +00:00
|
|
|
if (active != NULL && wl == ft->s->curw)
|
|
|
|
use = active;
|
|
|
|
else
|
|
|
|
use = all;
|
2019-03-15 10:07:24 +00:00
|
|
|
nft = format_create(c, item, FORMAT_WINDOW|w->id, ft->flags);
|
|
|
|
format_defaults(nft, ft->c, ft->s, wl, NULL);
|
2020-10-06 07:36:05 +00:00
|
|
|
format_copy_state(&next, es, 0);
|
|
|
|
next.ft = nft;
|
|
|
|
expanded = format_expand1(&next, use);
|
2019-03-15 10:07:24 +00:00
|
|
|
format_free(nft);
|
2019-03-13 15:37:28 +00:00
|
|
|
|
|
|
|
valuelen += strlen(expanded);
|
|
|
|
value = xrealloc(value, valuelen);
|
|
|
|
|
|
|
|
strlcat(value, expanded, valuelen);
|
|
|
|
free(expanded);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(active);
|
|
|
|
free(all);
|
|
|
|
|
|
|
|
return (value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop over panes. */
|
|
|
|
static char *
|
2020-10-06 07:36:05 +00:00
|
|
|
format_loop_panes(struct format_expand_state *es, const char *fmt)
|
2019-03-13 15:37:28 +00:00
|
|
|
{
|
2020-10-06 07:36:05 +00:00
|
|
|
struct format_tree *ft = es->ft;
|
|
|
|
struct client *c = ft->client;
|
|
|
|
struct cmdq_item *item = ft->item;
|
|
|
|
struct format_tree *nft;
|
|
|
|
struct format_expand_state next;
|
|
|
|
char *all, *active, *use, *expanded, *value;
|
|
|
|
size_t valuelen;
|
|
|
|
struct window_pane *wp;
|
2019-03-13 15:37:28 +00:00
|
|
|
|
2019-03-15 10:04:13 +00:00
|
|
|
if (ft->w == NULL) {
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "pane loop but no window");
|
2019-03-13 15:37:28 +00:00
|
|
|
return (NULL);
|
2019-03-15 10:04:13 +00:00
|
|
|
}
|
2019-03-13 15:37:28 +00:00
|
|
|
|
2020-10-06 07:36:05 +00:00
|
|
|
if (format_choose(es, fmt, &all, &active, 0) != 0) {
|
2019-03-13 15:37:28 +00:00
|
|
|
all = xstrdup(fmt);
|
|
|
|
active = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = xcalloc(1, 1);
|
|
|
|
valuelen = 1;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(wp, &ft->w->panes, entry) {
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "pane loop: %%%u", wp->id);
|
2019-03-13 15:37:28 +00:00
|
|
|
if (active != NULL && wp == ft->w->active)
|
|
|
|
use = active;
|
|
|
|
else
|
|
|
|
use = all;
|
2019-03-15 10:07:24 +00:00
|
|
|
nft = format_create(c, item, FORMAT_PANE|wp->id, ft->flags);
|
|
|
|
format_defaults(nft, ft->c, ft->s, ft->wl, wp);
|
2020-10-06 07:36:05 +00:00
|
|
|
format_copy_state(&next, es, 0);
|
|
|
|
next.ft = nft;
|
|
|
|
expanded = format_expand1(&next, use);
|
2019-03-15 10:07:24 +00:00
|
|
|
format_free(nft);
|
2019-03-13 15:37:28 +00:00
|
|
|
|
|
|
|
valuelen += strlen(expanded);
|
|
|
|
value = xrealloc(value, valuelen);
|
|
|
|
|
|
|
|
strlcat(value, expanded, valuelen);
|
|
|
|
free(expanded);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(active);
|
|
|
|
free(all);
|
|
|
|
|
|
|
|
return (value);
|
|
|
|
}
|
|
|
|
|
2023-02-07 10:21:01 +00:00
|
|
|
/* Loop over clients. */
|
|
|
|
static char *
|
|
|
|
format_loop_clients(struct format_expand_state *es, const char *fmt)
|
|
|
|
{
|
|
|
|
struct format_tree *ft = es->ft;
|
2023-09-08 06:52:31 +00:00
|
|
|
struct client *c;
|
2023-02-07 10:21:01 +00:00
|
|
|
struct cmdq_item *item = ft->item;
|
|
|
|
struct format_tree *nft;
|
|
|
|
struct format_expand_state next;
|
|
|
|
char *expanded, *value;
|
|
|
|
size_t valuelen;
|
|
|
|
|
|
|
|
value = xcalloc(1, 1);
|
|
|
|
valuelen = 1;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(c, &clients, entry) {
|
|
|
|
format_log(es, "client loop: %s", c->name);
|
|
|
|
nft = format_create(c, item, 0, ft->flags);
|
|
|
|
format_defaults(nft, c, ft->s, ft->wl, ft->wp);
|
|
|
|
format_copy_state(&next, es, 0);
|
|
|
|
next.ft = nft;
|
|
|
|
expanded = format_expand1(&next, fmt);
|
|
|
|
format_free(nft);
|
|
|
|
|
|
|
|
valuelen += strlen(expanded);
|
|
|
|
value = xrealloc(value, valuelen);
|
|
|
|
|
|
|
|
strlcat(value, expanded, valuelen);
|
|
|
|
free(expanded);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (value);
|
|
|
|
}
|
|
|
|
|
2020-03-11 14:17:55 +00:00
|
|
|
static char *
|
2020-10-06 07:36:05 +00:00
|
|
|
format_replace_expression(struct format_modifier *mexp,
|
|
|
|
struct format_expand_state *es, const char *copy)
|
|
|
|
{
|
|
|
|
int argc = mexp->argc;
|
|
|
|
const char *errstr;
|
|
|
|
char *endch, *value, *left = NULL, *right = NULL;
|
|
|
|
int use_fp = 0;
|
|
|
|
u_int prec = 0;
|
|
|
|
double mleft, mright, result;
|
2020-11-02 08:21:30 +00:00
|
|
|
enum { ADD,
|
|
|
|
SUBTRACT,
|
|
|
|
MULTIPLY,
|
|
|
|
DIVIDE,
|
|
|
|
MODULUS,
|
|
|
|
EQUAL,
|
|
|
|
NOT_EQUAL,
|
|
|
|
GREATER_THAN,
|
|
|
|
GREATER_THAN_EQUAL,
|
|
|
|
LESS_THAN,
|
|
|
|
LESS_THAN_EQUAL } operator;
|
2020-03-11 14:17:55 +00:00
|
|
|
|
|
|
|
if (strcmp(mexp->argv[0], "+") == 0)
|
|
|
|
operator = ADD;
|
|
|
|
else if (strcmp(mexp->argv[0], "-") == 0)
|
|
|
|
operator = SUBTRACT;
|
|
|
|
else if (strcmp(mexp->argv[0], "*") == 0)
|
|
|
|
operator = MULTIPLY;
|
|
|
|
else if (strcmp(mexp->argv[0], "/") == 0)
|
|
|
|
operator = DIVIDE;
|
|
|
|
else if (strcmp(mexp->argv[0], "%") == 0 ||
|
|
|
|
strcmp(mexp->argv[0], "m") == 0)
|
|
|
|
operator = MODULUS;
|
2020-11-02 08:21:30 +00:00
|
|
|
else if (strcmp(mexp->argv[0], "==") == 0)
|
|
|
|
operator = EQUAL;
|
|
|
|
else if (strcmp(mexp->argv[0], "!=") == 0)
|
|
|
|
operator = NOT_EQUAL;
|
|
|
|
else if (strcmp(mexp->argv[0], ">") == 0)
|
|
|
|
operator = GREATER_THAN;
|
|
|
|
else if (strcmp(mexp->argv[0], "<") == 0)
|
|
|
|
operator = LESS_THAN;
|
|
|
|
else if (strcmp(mexp->argv[0], ">=") == 0)
|
|
|
|
operator = GREATER_THAN_EQUAL;
|
|
|
|
else if (strcmp(mexp->argv[0], "<=") == 0)
|
|
|
|
operator = LESS_THAN_EQUAL;
|
2020-03-11 14:17:55 +00:00
|
|
|
else {
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "expression has no valid operator: '%s'",
|
2020-03-11 14:17:55 +00:00
|
|
|
mexp->argv[0]);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The second argument may be flags. */
|
|
|
|
if (argc >= 2 && strchr(mexp->argv[1], 'f') != NULL) {
|
|
|
|
use_fp = 1;
|
|
|
|
prec = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The third argument may be precision. */
|
|
|
|
if (argc >= 3) {
|
|
|
|
prec = strtonum(mexp->argv[2], INT_MIN, INT_MAX, &errstr);
|
|
|
|
if (errstr != NULL) {
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "expression precision %s: %s", errstr,
|
2020-03-11 14:17:55 +00:00
|
|
|
mexp->argv[2]);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 07:36:05 +00:00
|
|
|
if (format_choose(es, copy, &left, &right, 1) != 0) {
|
|
|
|
format_log(es, "expression syntax error");
|
2020-03-11 14:17:55 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
mleft = strtod(left, &endch);
|
|
|
|
if (*endch != '\0') {
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "expression left side is invalid: %s", left);
|
2020-03-11 14:17:55 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
mright = strtod(right, &endch);
|
|
|
|
if (*endch != '\0') {
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "expression right side is invalid: %s", right);
|
2020-03-11 14:17:55 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!use_fp) {
|
|
|
|
mleft = (long long)mleft;
|
|
|
|
mright = (long long)mright;
|
|
|
|
}
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "expression left side is: %.*f", prec, mleft);
|
2021-04-12 06:50:25 +00:00
|
|
|
format_log(es, "expression right side is: %.*f", prec, mright);
|
2020-03-11 14:17:55 +00:00
|
|
|
|
|
|
|
switch (operator) {
|
|
|
|
case ADD:
|
|
|
|
result = mleft + mright;
|
|
|
|
break;
|
|
|
|
case SUBTRACT:
|
|
|
|
result = mleft - mright;
|
|
|
|
break;
|
|
|
|
case MULTIPLY:
|
|
|
|
result = mleft * mright;
|
|
|
|
break;
|
|
|
|
case DIVIDE:
|
|
|
|
result = mleft / mright;
|
|
|
|
break;
|
|
|
|
case MODULUS:
|
|
|
|
result = fmod(mleft, mright);
|
|
|
|
break;
|
2020-11-02 08:21:30 +00:00
|
|
|
case EQUAL:
|
|
|
|
result = fabs(mleft - mright) < 1e-9;
|
|
|
|
break;
|
|
|
|
case NOT_EQUAL:
|
|
|
|
result = fabs(mleft - mright) > 1e-9;
|
|
|
|
break;
|
|
|
|
case GREATER_THAN:
|
|
|
|
result = (mleft > mright);
|
|
|
|
break;
|
|
|
|
case GREATER_THAN_EQUAL:
|
|
|
|
result = (mleft >= mright);
|
|
|
|
break;
|
|
|
|
case LESS_THAN:
|
|
|
|
result = (mleft < mright);
|
|
|
|
break;
|
|
|
|
case LESS_THAN_EQUAL:
|
2021-06-10 07:58:08 +00:00
|
|
|
result = (mleft <= mright);
|
2020-11-02 08:21:30 +00:00
|
|
|
break;
|
2020-03-11 14:17:55 +00:00
|
|
|
}
|
|
|
|
if (use_fp)
|
|
|
|
xasprintf(&value, "%.*f", prec, result);
|
|
|
|
else
|
|
|
|
xasprintf(&value, "%.*f", prec, (double)(long long)result);
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "expression result is %s", value);
|
2020-03-11 14:17:55 +00:00
|
|
|
|
|
|
|
free(right);
|
|
|
|
free(left);
|
2020-04-09 15:35:27 +00:00
|
|
|
return (value);
|
2020-03-11 14:17:55 +00:00
|
|
|
|
|
|
|
fail:
|
|
|
|
free(right);
|
|
|
|
free(left);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2019-03-13 14:10:34 +00:00
|
|
|
/* Replace a key. */
|
|
|
|
static int
|
2020-10-06 07:36:05 +00:00
|
|
|
format_replace(struct format_expand_state *es, const char *key, size_t keylen,
|
2019-03-13 14:10:34 +00:00
|
|
|
char **buf, size_t *len, size_t *off)
|
|
|
|
{
|
2020-10-06 07:36:05 +00:00
|
|
|
struct format_tree *ft = es->ft;
|
|
|
|
struct window_pane *wp = ft->wp;
|
2021-04-12 06:50:25 +00:00
|
|
|
const char *errstr, *copy, *cp, *marker = NULL;
|
2020-10-06 07:36:05 +00:00
|
|
|
const char *time_format = NULL;
|
|
|
|
char *copy0, *condition, *found, *new;
|
2021-10-25 21:21:16 +00:00
|
|
|
char *value, *left, *right;
|
2020-10-06 07:36:05 +00:00
|
|
|
size_t valuelen;
|
|
|
|
int modifiers = 0, limit = 0, width = 0;
|
2021-10-25 21:21:16 +00:00
|
|
|
int j, c;
|
2020-10-06 07:36:05 +00:00
|
|
|
struct format_modifier *list, *cmp = NULL, *search = NULL;
|
|
|
|
struct format_modifier **sub = NULL, *mexp = NULL, *fm;
|
|
|
|
u_int i, count, nsub = 0;
|
|
|
|
struct format_expand_state next;
|
2019-03-13 14:10:34 +00:00
|
|
|
|
|
|
|
/* Make a copy of the key. */
|
|
|
|
copy = copy0 = xstrndup(key, keylen);
|
|
|
|
|
|
|
|
/* Process modifier list. */
|
2020-10-06 07:36:05 +00:00
|
|
|
list = format_build_modifiers(es, ©, &count);
|
2019-03-13 14:10:34 +00:00
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
fm = &list[i];
|
2019-03-15 10:04:13 +00:00
|
|
|
if (format_logging(ft)) {
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "modifier %u is %s", i, fm->modifier);
|
2019-03-15 10:04:13 +00:00
|
|
|
for (j = 0; j < fm->argc; j++) {
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "modifier %u argument %d: %s", i,
|
2019-03-15 10:04:13 +00:00
|
|
|
j, fm->argv[j]);
|
|
|
|
}
|
|
|
|
}
|
2019-03-13 14:10:34 +00:00
|
|
|
if (fm->size == 1) {
|
|
|
|
switch (fm->modifier[0]) {
|
|
|
|
case 'm':
|
2019-05-25 16:51:10 +00:00
|
|
|
case '<':
|
|
|
|
case '>':
|
2019-03-13 14:10:34 +00:00
|
|
|
cmp = fm;
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
search = fm;
|
|
|
|
break;
|
|
|
|
case 's':
|
2019-06-13 19:46:00 +00:00
|
|
|
if (fm->argc < 2)
|
2019-03-13 14:10:34 +00:00
|
|
|
break;
|
2021-08-20 17:50:42 +00:00
|
|
|
sub = xreallocarray(sub, nsub + 1, sizeof *sub);
|
2019-11-25 15:02:48 +00:00
|
|
|
sub[nsub++] = fm;
|
2019-03-13 14:10:34 +00:00
|
|
|
break;
|
|
|
|
case '=':
|
2019-06-13 19:46:00 +00:00
|
|
|
if (fm->argc < 1)
|
2019-03-13 14:10:34 +00:00
|
|
|
break;
|
|
|
|
limit = strtonum(fm->argv[0], INT_MIN, INT_MAX,
|
2021-04-12 06:50:25 +00:00
|
|
|
&errstr);
|
|
|
|
if (errstr != NULL)
|
2019-03-13 14:10:34 +00:00
|
|
|
limit = 0;
|
2019-06-13 19:46:00 +00:00
|
|
|
if (fm->argc >= 2 && fm->argv[1] != NULL)
|
2019-05-26 12:02:42 +00:00
|
|
|
marker = fm->argv[1];
|
2019-03-13 14:10:34 +00:00
|
|
|
break;
|
2019-11-25 15:04:15 +00:00
|
|
|
case 'p':
|
|
|
|
if (fm->argc < 1)
|
|
|
|
break;
|
|
|
|
width = strtonum(fm->argv[0], INT_MIN, INT_MAX,
|
2021-04-12 06:50:25 +00:00
|
|
|
&errstr);
|
|
|
|
if (errstr != NULL)
|
2019-11-25 15:04:15 +00:00
|
|
|
width = 0;
|
|
|
|
break;
|
2020-11-09 09:10:10 +00:00
|
|
|
case 'w':
|
|
|
|
modifiers |= FORMAT_WIDTH;
|
|
|
|
break;
|
2020-03-11 14:17:55 +00:00
|
|
|
case 'e':
|
|
|
|
if (fm->argc < 1 || fm->argc > 3)
|
|
|
|
break;
|
2020-05-16 14:10:29 +00:00
|
|
|
mexp = fm;
|
2020-03-11 14:17:55 +00:00
|
|
|
break;
|
2019-03-13 14:10:34 +00:00
|
|
|
case 'l':
|
|
|
|
modifiers |= FORMAT_LITERAL;
|
|
|
|
break;
|
2021-04-12 06:50:25 +00:00
|
|
|
case 'a':
|
|
|
|
modifiers |= FORMAT_CHARACTER;
|
|
|
|
break;
|
2019-03-13 14:10:34 +00:00
|
|
|
case 'b':
|
|
|
|
modifiers |= FORMAT_BASENAME;
|
|
|
|
break;
|
2021-10-25 21:21:16 +00:00
|
|
|
case 'c':
|
|
|
|
modifiers |= FORMAT_COLOUR;
|
|
|
|
break;
|
2019-03-13 14:10:34 +00:00
|
|
|
case 'd':
|
|
|
|
modifiers |= FORMAT_DIRNAME;
|
|
|
|
break;
|
2020-08-20 16:57:40 +00:00
|
|
|
case 'n':
|
|
|
|
modifiers |= FORMAT_LENGTH;
|
|
|
|
break;
|
2019-03-13 14:10:34 +00:00
|
|
|
case 't':
|
|
|
|
modifiers |= FORMAT_TIMESTRING;
|
2020-05-16 14:10:29 +00:00
|
|
|
if (fm->argc < 1)
|
|
|
|
break;
|
|
|
|
if (strchr(fm->argv[0], 'p') != NULL)
|
|
|
|
modifiers |= FORMAT_PRETTY;
|
2020-05-16 15:48:35 +00:00
|
|
|
else if (fm->argc >= 2 &&
|
|
|
|
strchr(fm->argv[0], 'f') != NULL)
|
|
|
|
time_format = format_strip(fm->argv[1]);
|
2019-03-13 14:10:34 +00:00
|
|
|
break;
|
|
|
|
case 'q':
|
2020-12-01 08:12:58 +00:00
|
|
|
if (fm->argc < 1)
|
2021-01-20 07:16:54 +00:00
|
|
|
modifiers |= FORMAT_QUOTE_SHELL;
|
|
|
|
else if (strchr(fm->argv[0], 'e') != NULL ||
|
|
|
|
strchr(fm->argv[0], 'h') != NULL)
|
|
|
|
modifiers |= FORMAT_QUOTE_STYLE;
|
2019-03-13 14:10:34 +00:00
|
|
|
break;
|
2019-03-13 14:19:54 +00:00
|
|
|
case 'E':
|
|
|
|
modifiers |= FORMAT_EXPAND;
|
|
|
|
break;
|
2019-03-14 21:31:43 +00:00
|
|
|
case 'T':
|
|
|
|
modifiers |= FORMAT_EXPANDTIME;
|
|
|
|
break;
|
2021-02-05 12:23:49 +00:00
|
|
|
case 'N':
|
|
|
|
if (fm->argc < 1 ||
|
|
|
|
strchr(fm->argv[0], 'w') != NULL)
|
|
|
|
modifiers |= FORMAT_WINDOW_NAME;
|
|
|
|
else if (strchr(fm->argv[0], 's') != NULL)
|
|
|
|
modifiers |= FORMAT_SESSION_NAME;
|
|
|
|
break;
|
2019-03-13 15:37:28 +00:00
|
|
|
case 'S':
|
|
|
|
modifiers |= FORMAT_SESSIONS;
|
|
|
|
break;
|
|
|
|
case 'W':
|
|
|
|
modifiers |= FORMAT_WINDOWS;
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
modifiers |= FORMAT_PANES;
|
|
|
|
break;
|
2023-02-07 10:21:01 +00:00
|
|
|
case 'L':
|
|
|
|
modifiers |= FORMAT_CLIENTS;
|
|
|
|
break;
|
2019-03-13 14:10:34 +00:00
|
|
|
}
|
|
|
|
} else if (fm->size == 2) {
|
|
|
|
if (strcmp(fm->modifier, "||") == 0 ||
|
|
|
|
strcmp(fm->modifier, "&&") == 0 ||
|
|
|
|
strcmp(fm->modifier, "==") == 0 ||
|
2019-05-25 16:51:10 +00:00
|
|
|
strcmp(fm->modifier, "!=") == 0 ||
|
|
|
|
strcmp(fm->modifier, ">=") == 0 ||
|
|
|
|
strcmp(fm->modifier, "<=") == 0)
|
2019-03-13 14:10:34 +00:00
|
|
|
cmp = fm;
|
|
|
|
}
|
2013-10-10 11:50:20 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 08:49:12 +00:00
|
|
|
/* Is this a literal string? */
|
2019-03-13 14:10:34 +00:00
|
|
|
if (modifiers & FORMAT_LITERAL) {
|
2022-11-04 08:03:23 +00:00
|
|
|
format_log(es, "literal string is '%s'", copy);
|
|
|
|
value = format_unescape(copy);
|
2018-05-22 08:49:12 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2021-04-12 06:50:25 +00:00
|
|
|
/* Is this a character? */
|
|
|
|
if (modifiers & FORMAT_CHARACTER) {
|
|
|
|
new = format_expand1(es, copy);
|
|
|
|
c = strtonum(new, 32, 126, &errstr);
|
|
|
|
if (errstr != NULL)
|
|
|
|
value = xstrdup("");
|
|
|
|
else
|
|
|
|
xasprintf(&value, "%c", c);
|
2021-08-20 17:50:42 +00:00
|
|
|
free(new);
|
2021-04-12 06:50:25 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2021-10-25 21:21:16 +00:00
|
|
|
/* Is this a colour? */
|
|
|
|
if (modifiers & FORMAT_COLOUR) {
|
|
|
|
new = format_expand1(es, copy);
|
|
|
|
c = colour_fromstring(new);
|
|
|
|
if (c == -1 || (c = colour_force_rgb(c)) == -1)
|
|
|
|
value = xstrdup("");
|
|
|
|
else
|
|
|
|
xasprintf(&value, "%06x", c & 0xffffff);
|
|
|
|
free(new);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2019-03-13 15:37:28 +00:00
|
|
|
/* Is this a loop, comparison or condition? */
|
|
|
|
if (modifiers & FORMAT_SESSIONS) {
|
2020-10-06 07:36:05 +00:00
|
|
|
value = format_loop_sessions(es, copy);
|
2019-03-13 15:37:28 +00:00
|
|
|
if (value == NULL)
|
|
|
|
goto fail;
|
|
|
|
} else if (modifiers & FORMAT_WINDOWS) {
|
2020-10-06 07:36:05 +00:00
|
|
|
value = format_loop_windows(es, copy);
|
2019-03-13 15:37:28 +00:00
|
|
|
if (value == NULL)
|
|
|
|
goto fail;
|
|
|
|
} else if (modifiers & FORMAT_PANES) {
|
2020-10-06 07:36:05 +00:00
|
|
|
value = format_loop_panes(es, copy);
|
2019-03-13 15:37:28 +00:00
|
|
|
if (value == NULL)
|
|
|
|
goto fail;
|
2023-02-07 10:21:01 +00:00
|
|
|
} else if (modifiers & FORMAT_CLIENTS) {
|
|
|
|
value = format_loop_clients(es, copy);
|
|
|
|
if (value == NULL)
|
|
|
|
goto fail;
|
2021-02-05 12:23:49 +00:00
|
|
|
} else if (modifiers & FORMAT_WINDOW_NAME) {
|
|
|
|
value = format_window_name(es, copy);
|
|
|
|
if (value == NULL)
|
|
|
|
goto fail;
|
|
|
|
} else if (modifiers & FORMAT_SESSION_NAME) {
|
|
|
|
value = format_session_name(es, copy);
|
|
|
|
if (value == NULL)
|
|
|
|
goto fail;
|
2019-03-13 15:37:28 +00:00
|
|
|
} else if (search != NULL) {
|
2017-05-29 18:06:34 +00:00
|
|
|
/* Search in pane. */
|
2020-10-06 07:36:05 +00:00
|
|
|
new = format_expand1(es, copy);
|
2019-03-15 10:04:13 +00:00
|
|
|
if (wp == NULL) {
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "search '%s' but no pane", new);
|
2017-05-29 18:06:34 +00:00
|
|
|
value = xstrdup("0");
|
2019-03-15 10:04:13 +00:00
|
|
|
} else {
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "search '%s' pane %%%u", new, wp->id);
|
2021-06-10 07:45:43 +00:00
|
|
|
value = format_search(search, wp, new);
|
2019-03-15 10:04:13 +00:00
|
|
|
}
|
2019-06-24 10:04:29 +00:00
|
|
|
free(new);
|
2019-03-13 14:10:34 +00:00
|
|
|
} else if (cmp != NULL) {
|
|
|
|
/* Comparison of left and right. */
|
2020-10-06 07:36:05 +00:00
|
|
|
if (format_choose(es, copy, &left, &right, 1) != 0) {
|
|
|
|
format_log(es, "compare %s syntax error: %s",
|
2019-03-15 10:04:13 +00:00
|
|
|
cmp->modifier, copy);
|
2017-01-09 21:03:25 +00:00
|
|
|
goto fail;
|
2019-03-15 10:04:13 +00:00
|
|
|
}
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "compare %s left is: %s", cmp->modifier, left);
|
|
|
|
format_log(es, "compare %s right is: %s", cmp->modifier, right);
|
2019-03-13 14:10:34 +00:00
|
|
|
|
|
|
|
if (strcmp(cmp->modifier, "||") == 0) {
|
|
|
|
if (format_true(left) || format_true(right))
|
|
|
|
value = xstrdup("1");
|
|
|
|
else
|
|
|
|
value = xstrdup("0");
|
|
|
|
} else if (strcmp(cmp->modifier, "&&") == 0) {
|
|
|
|
if (format_true(left) && format_true(right))
|
|
|
|
value = xstrdup("1");
|
|
|
|
else
|
|
|
|
value = xstrdup("0");
|
|
|
|
} else if (strcmp(cmp->modifier, "==") == 0) {
|
|
|
|
if (strcmp(left, right) == 0)
|
|
|
|
value = xstrdup("1");
|
|
|
|
else
|
|
|
|
value = xstrdup("0");
|
|
|
|
} else if (strcmp(cmp->modifier, "!=") == 0) {
|
|
|
|
if (strcmp(left, right) != 0)
|
|
|
|
value = xstrdup("1");
|
|
|
|
else
|
|
|
|
value = xstrdup("0");
|
2019-05-25 16:51:10 +00:00
|
|
|
} else if (strcmp(cmp->modifier, "<") == 0) {
|
|
|
|
if (strcmp(left, right) < 0)
|
|
|
|
value = xstrdup("1");
|
|
|
|
else
|
|
|
|
value = xstrdup("0");
|
|
|
|
} else if (strcmp(cmp->modifier, ">") == 0) {
|
|
|
|
if (strcmp(left, right) > 0)
|
|
|
|
value = xstrdup("1");
|
|
|
|
else
|
|
|
|
value = xstrdup("0");
|
|
|
|
} else if (strcmp(cmp->modifier, "<=") == 0) {
|
|
|
|
if (strcmp(left, right) <= 0)
|
|
|
|
value = xstrdup("1");
|
|
|
|
else
|
|
|
|
value = xstrdup("0");
|
|
|
|
} else if (strcmp(cmp->modifier, ">=") == 0) {
|
|
|
|
if (strcmp(left, right) >= 0)
|
|
|
|
value = xstrdup("1");
|
|
|
|
else
|
|
|
|
value = xstrdup("0");
|
2019-06-13 19:46:00 +00:00
|
|
|
} else if (strcmp(cmp->modifier, "m") == 0)
|
2019-06-15 06:33:48 +00:00
|
|
|
value = format_match(cmp, left, right);
|
2019-03-13 14:10:34 +00:00
|
|
|
|
2017-01-09 21:03:25 +00:00
|
|
|
free(right);
|
|
|
|
free(left);
|
|
|
|
} else if (*copy == '?') {
|
|
|
|
/* Conditional: check first and choose second or third. */
|
2019-03-13 14:10:34 +00:00
|
|
|
cp = format_skip(copy + 1, ",");
|
2019-03-15 10:04:13 +00:00
|
|
|
if (cp == NULL) {
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "condition syntax error: %s", copy + 1);
|
2011-08-26 10:53:16 +00:00
|
|
|
goto fail;
|
2019-03-15 10:04:13 +00:00
|
|
|
}
|
2019-03-13 14:10:34 +00:00
|
|
|
condition = xstrndup(copy + 1, cp - (copy + 1));
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "condition is: %s", condition);
|
2011-08-26 10:53:16 +00:00
|
|
|
|
2020-05-16 15:48:35 +00:00
|
|
|
found = format_find(ft, condition, modifiers, time_format);
|
2018-05-29 09:10:30 +00:00
|
|
|
if (found == NULL) {
|
|
|
|
/*
|
2019-03-13 14:10:34 +00:00
|
|
|
* If the condition not found, try to expand it. If
|
2018-05-29 09:10:30 +00:00
|
|
|
* the expansion doesn't have any effect, then assume
|
|
|
|
* false.
|
|
|
|
*/
|
2020-10-06 07:36:05 +00:00
|
|
|
found = format_expand1(es, condition);
|
2019-03-13 14:10:34 +00:00
|
|
|
if (strcmp(found, condition) == 0) {
|
2018-05-29 09:10:30 +00:00
|
|
|
free(found);
|
|
|
|
found = xstrdup("");
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es,
|
2019-03-15 10:04:13 +00:00
|
|
|
"condition '%s' not found; assuming false",
|
|
|
|
condition);
|
2018-05-29 09:10:30 +00:00
|
|
|
}
|
2021-08-12 19:47:05 +00:00
|
|
|
} else {
|
|
|
|
format_log(es, "condition '%s' found: %s", condition,
|
|
|
|
found);
|
|
|
|
}
|
2019-03-13 14:10:34 +00:00
|
|
|
|
2020-10-06 07:36:05 +00:00
|
|
|
if (format_choose(es, cp + 1, &left, &right, 0) != 0) {
|
|
|
|
format_log(es, "condition '%s' syntax error: %s",
|
2019-03-15 10:04:13 +00:00
|
|
|
condition, cp + 1);
|
2018-08-27 11:03:34 +00:00
|
|
|
free(found);
|
2015-10-27 09:28:31 +00:00
|
|
|
goto fail;
|
2018-08-27 11:03:34 +00:00
|
|
|
}
|
2019-03-15 10:04:13 +00:00
|
|
|
if (format_true(found)) {
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "condition '%s' is true", condition);
|
|
|
|
value = format_expand1(es, left);
|
2019-03-15 10:04:13 +00:00
|
|
|
} else {
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "condition '%s' is false", condition);
|
|
|
|
value = format_expand1(es, right);
|
2019-03-15 10:04:13 +00:00
|
|
|
}
|
2019-03-13 14:10:34 +00:00
|
|
|
free(right);
|
|
|
|
free(left);
|
|
|
|
|
2019-03-15 10:04:13 +00:00
|
|
|
free(condition);
|
2015-11-18 14:13:55 +00:00
|
|
|
free(found);
|
2020-03-11 14:17:55 +00:00
|
|
|
} else if (mexp != NULL) {
|
2020-10-06 07:36:05 +00:00
|
|
|
value = format_replace_expression(mexp, es, copy);
|
2020-03-11 14:17:55 +00:00
|
|
|
if (value == NULL)
|
|
|
|
value = xstrdup("");
|
2011-08-26 10:53:16 +00:00
|
|
|
} else {
|
2020-08-20 16:57:40 +00:00
|
|
|
if (strstr(copy, "#{") != 0) {
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "expanding inner format '%s'", copy);
|
|
|
|
value = format_expand1(es, copy);
|
2020-08-20 16:57:40 +00:00
|
|
|
} else {
|
|
|
|
value = format_find(ft, copy, modifiers, time_format);
|
|
|
|
if (value == NULL) {
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "format '%s' not found", copy);
|
2020-08-20 16:57:40 +00:00
|
|
|
value = xstrdup("");
|
2020-10-06 07:36:05 +00:00
|
|
|
} else {
|
|
|
|
format_log(es, "format '%s' found: %s", copy,
|
|
|
|
value);
|
|
|
|
}
|
2020-08-20 16:57:40 +00:00
|
|
|
}
|
2015-11-18 14:13:55 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 14:27:17 +00:00
|
|
|
done:
|
2019-03-13 14:19:54 +00:00
|
|
|
/* Expand again if required. */
|
|
|
|
if (modifiers & FORMAT_EXPAND) {
|
2020-10-06 07:36:05 +00:00
|
|
|
new = format_expand1(es, value);
|
2019-03-13 14:19:54 +00:00
|
|
|
free(value);
|
|
|
|
value = new;
|
2020-08-20 16:57:40 +00:00
|
|
|
} else if (modifiers & FORMAT_EXPANDTIME) {
|
2020-10-06 07:36:05 +00:00
|
|
|
format_copy_state(&next, es, FORMAT_EXPAND_TIME);
|
|
|
|
new = format_expand1(&next, value);
|
2019-03-14 21:31:43 +00:00
|
|
|
free(value);
|
|
|
|
value = new;
|
|
|
|
}
|
2019-03-13 14:19:54 +00:00
|
|
|
|
2015-11-18 14:13:55 +00:00
|
|
|
/* Perform substitution if any. */
|
2019-11-25 15:02:48 +00:00
|
|
|
for (i = 0; i < nsub; i++) {
|
2020-10-06 07:36:05 +00:00
|
|
|
left = format_expand1(es, sub[i]->argv[0]);
|
|
|
|
right = format_expand1(es, sub[i]->argv[1]);
|
2019-11-25 15:02:48 +00:00
|
|
|
new = format_sub(sub[i], value, left, right);
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "substitute '%s' to '%s': %s", left, right, new);
|
2015-11-18 14:13:55 +00:00
|
|
|
free(value);
|
2019-03-13 14:10:34 +00:00
|
|
|
value = new;
|
2019-06-24 10:04:29 +00:00
|
|
|
free(right);
|
|
|
|
free(left);
|
2011-08-26 10:53:16 +00:00
|
|
|
}
|
|
|
|
|
2013-10-10 11:50:20 +00:00
|
|
|
/* Truncate the value if needed. */
|
2016-01-31 09:54:46 +00:00
|
|
|
if (limit > 0) {
|
2019-03-18 20:53:33 +00:00
|
|
|
new = format_trim_left(value, limit);
|
2019-05-26 12:02:42 +00:00
|
|
|
if (marker != NULL && strcmp(new, value) != 0) {
|
|
|
|
free(value);
|
|
|
|
xasprintf(&value, "%s%s", new, marker);
|
|
|
|
} else {
|
|
|
|
free(value);
|
|
|
|
value = new;
|
|
|
|
}
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "applied length limit %d: %s", limit, value);
|
2016-01-31 09:54:46 +00:00
|
|
|
} else if (limit < 0) {
|
2019-03-18 20:53:33 +00:00
|
|
|
new = format_trim_right(value, -limit);
|
2019-05-26 12:02:42 +00:00
|
|
|
if (marker != NULL && strcmp(new, value) != 0) {
|
|
|
|
free(value);
|
|
|
|
xasprintf(&value, "%s%s", marker, new);
|
|
|
|
} else {
|
|
|
|
free(value);
|
|
|
|
value = new;
|
|
|
|
}
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "applied length limit %d: %s", limit, value);
|
2014-04-17 15:37:55 +00:00
|
|
|
}
|
2013-10-10 11:50:20 +00:00
|
|
|
|
2019-11-25 15:04:15 +00:00
|
|
|
/* Pad the value if needed. */
|
|
|
|
if (width > 0) {
|
|
|
|
new = utf8_padcstr(value, width);
|
|
|
|
free(value);
|
|
|
|
value = new;
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "applied padding width %d: %s", width, value);
|
2019-11-25 15:04:15 +00:00
|
|
|
} else if (width < 0) {
|
|
|
|
new = utf8_rpadcstr(value, -width);
|
|
|
|
free(value);
|
|
|
|
value = new;
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "applied padding width %d: %s", width, value);
|
2019-11-25 15:04:15 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 09:10:10 +00:00
|
|
|
/* Replace with the length or width if needed. */
|
2020-08-20 16:57:40 +00:00
|
|
|
if (modifiers & FORMAT_LENGTH) {
|
|
|
|
xasprintf(&new, "%zu", strlen(value));
|
|
|
|
free(value);
|
|
|
|
value = new;
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "replacing with length: %s", new);
|
2020-08-20 16:57:40 +00:00
|
|
|
}
|
2020-11-09 09:10:10 +00:00
|
|
|
if (modifiers & FORMAT_WIDTH) {
|
|
|
|
xasprintf(&new, "%u", format_width(value));
|
|
|
|
free(value);
|
|
|
|
value = new;
|
|
|
|
format_log(es, "replacing with width: %s", new);
|
|
|
|
}
|
2020-08-20 16:57:40 +00:00
|
|
|
|
2018-05-29 09:10:30 +00:00
|
|
|
/* Expand the buffer and copy in the value. */
|
2015-11-18 14:13:55 +00:00
|
|
|
valuelen = strlen(value);
|
2011-08-26 10:53:16 +00:00
|
|
|
while (*len - *off < valuelen + 1) {
|
2014-10-08 17:35:58 +00:00
|
|
|
*buf = xreallocarray(*buf, 2, *len);
|
2011-08-26 10:53:16 +00:00
|
|
|
*len *= 2;
|
|
|
|
}
|
|
|
|
memcpy(*buf + *off, value, valuelen);
|
|
|
|
*off += valuelen;
|
|
|
|
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "replaced '%s' with '%s'", copy0, value);
|
2015-11-18 14:13:55 +00:00
|
|
|
free(value);
|
2019-03-15 10:04:13 +00:00
|
|
|
|
2019-11-25 15:02:48 +00:00
|
|
|
free(sub);
|
2019-03-13 14:10:34 +00:00
|
|
|
format_free_modifiers(list, count);
|
2013-10-10 11:50:20 +00:00
|
|
|
free(copy0);
|
2011-08-26 10:53:16 +00:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
fail:
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "failed %s", copy0);
|
2019-11-25 15:02:48 +00:00
|
|
|
|
|
|
|
free(sub);
|
2019-03-13 14:10:34 +00:00
|
|
|
format_free_modifiers(list, count);
|
2013-10-10 11:50:20 +00:00
|
|
|
free(copy0);
|
2011-08-26 10:53:16 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Expand keys in a template. */
|
2019-03-15 10:04:13 +00:00
|
|
|
static char *
|
2020-10-06 07:36:05 +00:00
|
|
|
format_expand1(struct format_expand_state *es, const char *fmt)
|
2011-08-26 10:53:16 +00:00
|
|
|
{
|
2020-10-06 07:36:05 +00:00
|
|
|
struct format_tree *ft = es->ft;
|
|
|
|
char *buf, *out, *name;
|
2022-06-27 09:14:49 +00:00
|
|
|
const char *ptr, *s, *style_end = NULL;
|
2020-10-06 07:36:05 +00:00
|
|
|
size_t off, len, n, outlen;
|
|
|
|
int ch, brackets;
|
|
|
|
char expanded[8192];
|
2011-08-26 10:53:16 +00:00
|
|
|
|
2019-03-15 10:04:13 +00:00
|
|
|
if (fmt == NULL || *fmt == '\0')
|
2015-02-06 17:11:39 +00:00
|
|
|
return (xstrdup(""));
|
|
|
|
|
2021-06-10 07:58:08 +00:00
|
|
|
if (es->loop == FORMAT_LOOP_LIMIT) {
|
|
|
|
format_log(es, "reached loop limit (%u)", FORMAT_LOOP_LIMIT);
|
2019-03-14 23:34:41 +00:00
|
|
|
return (xstrdup(""));
|
2021-06-10 07:58:08 +00:00
|
|
|
}
|
2020-10-06 07:36:05 +00:00
|
|
|
es->loop++;
|
2019-03-14 23:34:41 +00:00
|
|
|
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "expanding format: %s", fmt);
|
2019-03-15 10:04:13 +00:00
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
if ((es->flags & FORMAT_EXPAND_TIME) && strchr(fmt, '%') != NULL) {
|
|
|
|
if (es->time == 0) {
|
2020-10-06 07:36:05 +00:00
|
|
|
es->time = time(NULL);
|
2021-02-22 07:09:06 +00:00
|
|
|
localtime_r(&es->time, &es->tm);
|
|
|
|
}
|
|
|
|
if (strftime(expanded, sizeof expanded, fmt, &es->tm) == 0) {
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "format is too long");
|
2019-03-15 10:04:13 +00:00
|
|
|
return (xstrdup(""));
|
|
|
|
}
|
|
|
|
if (format_logging(ft) && strcmp(expanded, fmt) != 0)
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "after time expanded: %s", expanded);
|
2019-03-15 10:04:13 +00:00
|
|
|
fmt = expanded;
|
|
|
|
}
|
|
|
|
|
2011-08-26 10:53:16 +00:00
|
|
|
len = 64;
|
|
|
|
buf = xmalloc(len);
|
|
|
|
off = 0;
|
|
|
|
|
|
|
|
while (*fmt != '\0') {
|
|
|
|
if (*fmt != '#') {
|
|
|
|
while (len - off < 2) {
|
2014-10-08 17:35:58 +00:00
|
|
|
buf = xreallocarray(buf, 2, len);
|
2011-08-26 10:53:16 +00:00
|
|
|
len *= 2;
|
|
|
|
}
|
|
|
|
buf[off++] = *fmt++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
fmt++;
|
|
|
|
|
2019-03-15 10:04:13 +00:00
|
|
|
ch = (u_char)*fmt++;
|
2011-08-26 10:53:16 +00:00
|
|
|
switch (ch) {
|
2015-05-27 13:28:04 +00:00
|
|
|
case '(':
|
|
|
|
brackets = 1;
|
|
|
|
for (ptr = fmt; *ptr != '\0'; ptr++) {
|
|
|
|
if (*ptr == '(')
|
|
|
|
brackets++;
|
|
|
|
if (*ptr == ')' && --brackets == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*ptr != ')' || brackets != 0)
|
|
|
|
break;
|
|
|
|
n = ptr - fmt;
|
|
|
|
|
2019-03-15 10:04:13 +00:00
|
|
|
name = xstrndup(fmt, n);
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "found #(): %s", name);
|
2019-03-15 10:04:13 +00:00
|
|
|
|
2020-10-06 07:36:05 +00:00
|
|
|
if ((ft->flags & FORMAT_NOJOBS) ||
|
|
|
|
(es->flags & FORMAT_EXPAND_NOJOBS)) {
|
2017-01-09 21:03:25 +00:00
|
|
|
out = xstrdup("");
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "#() is disabled");
|
2019-03-15 10:04:13 +00:00
|
|
|
} else {
|
2020-10-06 07:36:05 +00:00
|
|
|
out = format_job_get(es, name);
|
|
|
|
format_log(es, "#() result: %s", out);
|
2018-02-20 10:43:46 +00:00
|
|
|
}
|
2019-03-15 10:04:13 +00:00
|
|
|
free(name);
|
2015-05-27 13:28:04 +00:00
|
|
|
|
2019-03-15 10:04:13 +00:00
|
|
|
outlen = strlen(out);
|
2015-10-25 08:59:26 +00:00
|
|
|
while (len - off < outlen + 1) {
|
2015-05-27 13:28:04 +00:00
|
|
|
buf = xreallocarray(buf, 2, len);
|
|
|
|
len *= 2;
|
|
|
|
}
|
2015-10-25 08:59:26 +00:00
|
|
|
memcpy(buf + off, out, outlen);
|
|
|
|
off += outlen;
|
|
|
|
|
|
|
|
free(out);
|
2015-05-27 13:28:04 +00:00
|
|
|
|
|
|
|
fmt += n + 1;
|
|
|
|
continue;
|
2011-08-26 10:53:16 +00:00
|
|
|
case '{':
|
2019-03-13 14:10:34 +00:00
|
|
|
ptr = format_skip((char *)fmt - 2, "}");
|
2018-05-22 08:49:12 +00:00
|
|
|
if (ptr == NULL)
|
2011-08-26 10:53:16 +00:00
|
|
|
break;
|
|
|
|
n = ptr - fmt;
|
|
|
|
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "found #{}: %.*s", (int)n, fmt);
|
|
|
|
if (format_replace(es, fmt, n, &buf, &len, &off) != 0)
|
2011-08-26 10:53:16 +00:00
|
|
|
break;
|
|
|
|
fmt += n + 1;
|
|
|
|
continue;
|
2022-06-27 09:14:49 +00:00
|
|
|
case '[':
|
2013-11-24 11:29:09 +00:00
|
|
|
case '#':
|
2020-11-09 09:10:10 +00:00
|
|
|
/*
|
|
|
|
* If ##[ (with two or more #s), then it is a style and
|
|
|
|
* can be left for format_draw to handle.
|
|
|
|
*/
|
2022-06-27 09:14:49 +00:00
|
|
|
ptr = fmt - (ch == '[');
|
|
|
|
n = 2 - (ch == '[');
|
2020-11-09 09:10:10 +00:00
|
|
|
while (*ptr == '#') {
|
|
|
|
ptr++;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
if (*ptr == '[') {
|
2022-06-27 09:16:54 +00:00
|
|
|
style_end = format_skip(fmt - 2, "]");
|
2020-11-09 09:10:10 +00:00
|
|
|
format_log(es, "found #*%zu[", n);
|
|
|
|
while (len - off < n + 2) {
|
|
|
|
buf = xreallocarray(buf, 2, len);
|
|
|
|
len *= 2;
|
|
|
|
}
|
|
|
|
memcpy(buf + off, fmt - 2, n + 1);
|
|
|
|
off += n + 1;
|
|
|
|
fmt = ptr + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case '}':
|
2018-05-22 08:49:12 +00:00
|
|
|
case ',':
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "found #%c", ch);
|
2013-11-24 11:29:09 +00:00
|
|
|
while (len - off < 2) {
|
2014-10-08 17:35:58 +00:00
|
|
|
buf = xreallocarray(buf, 2, len);
|
2013-11-24 11:29:09 +00:00
|
|
|
len *= 2;
|
|
|
|
}
|
2018-05-22 08:49:12 +00:00
|
|
|
buf[off++] = ch;
|
2013-11-24 11:29:09 +00:00
|
|
|
continue;
|
2011-08-26 10:53:16 +00:00
|
|
|
default:
|
2013-05-31 19:46:42 +00:00
|
|
|
s = NULL;
|
2022-06-27 09:14:49 +00:00
|
|
|
if (fmt > style_end) { /* skip inside #[] */
|
|
|
|
if (ch >= 'A' && ch <= 'Z')
|
|
|
|
s = format_upper[ch - 'A'];
|
|
|
|
else if (ch >= 'a' && ch <= 'z')
|
|
|
|
s = format_lower[ch - 'a'];
|
|
|
|
}
|
2013-05-31 19:46:42 +00:00
|
|
|
if (s == NULL) {
|
|
|
|
while (len - off < 3) {
|
2014-10-08 17:35:58 +00:00
|
|
|
buf = xreallocarray(buf, 2, len);
|
2013-05-31 19:46:42 +00:00
|
|
|
len *= 2;
|
2011-08-26 10:53:16 +00:00
|
|
|
}
|
2013-05-31 19:46:42 +00:00
|
|
|
buf[off++] = '#';
|
|
|
|
buf[off++] = ch;
|
|
|
|
continue;
|
2011-08-26 10:53:16 +00:00
|
|
|
}
|
2013-05-31 19:46:42 +00:00
|
|
|
n = strlen(s);
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "found #%c: %s", ch, s);
|
|
|
|
if (format_replace(es, s, n, &buf, &len, &off) != 0)
|
2013-05-31 19:46:42 +00:00
|
|
|
break;
|
2011-08-26 10:53:16 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf[off] = '\0';
|
|
|
|
|
2020-10-06 07:36:05 +00:00
|
|
|
format_log(es, "result is: %s", buf);
|
|
|
|
es->loop--;
|
2019-03-15 10:04:13 +00:00
|
|
|
|
2011-08-26 10:53:16 +00:00
|
|
|
return (buf);
|
|
|
|
}
|
|
|
|
|
2019-03-15 10:04:13 +00:00
|
|
|
/* Expand keys in a template, passing through strftime first. */
|
|
|
|
char *
|
|
|
|
format_expand_time(struct format_tree *ft, const char *fmt)
|
|
|
|
{
|
2020-10-06 07:36:05 +00:00
|
|
|
struct format_expand_state es;
|
|
|
|
|
|
|
|
memset(&es, 0, sizeof es);
|
|
|
|
es.ft = ft;
|
|
|
|
es.flags = FORMAT_EXPAND_TIME;
|
|
|
|
return (format_expand1(&es, fmt));
|
2019-03-15 10:04:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Expand keys in a template. */
|
|
|
|
char *
|
|
|
|
format_expand(struct format_tree *ft, const char *fmt)
|
|
|
|
{
|
2020-10-06 07:36:05 +00:00
|
|
|
struct format_expand_state es;
|
|
|
|
|
|
|
|
memset(&es, 0, sizeof es);
|
|
|
|
es.ft = ft;
|
|
|
|
es.flags = 0;
|
|
|
|
return (format_expand1(&es, fmt));
|
2019-03-15 10:04:13 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:36:12 +00:00
|
|
|
/* Expand a single string. */
|
|
|
|
char *
|
|
|
|
format_single(struct cmdq_item *item, const char *fmt, struct client *c,
|
|
|
|
struct session *s, struct winlink *wl, struct window_pane *wp)
|
|
|
|
{
|
|
|
|
struct format_tree *ft;
|
|
|
|
char *expanded;
|
|
|
|
|
2020-05-16 14:55:38 +00:00
|
|
|
ft = format_create_defaults(item, c, s, wl, wp);
|
2017-03-08 13:36:12 +00:00
|
|
|
expanded = format_expand(ft, fmt);
|
|
|
|
format_free(ft);
|
|
|
|
return (expanded);
|
|
|
|
}
|
|
|
|
|
2020-05-16 14:55:38 +00:00
|
|
|
/* Expand a single string using state. */
|
|
|
|
char *
|
|
|
|
format_single_from_state(struct cmdq_item *item, const char *fmt,
|
|
|
|
struct client *c, struct cmd_find_state *fs)
|
|
|
|
{
|
|
|
|
return (format_single(item, fmt, c, fs->s, fs->wl, fs->wp));
|
|
|
|
}
|
|
|
|
|
2020-04-13 10:59:58 +00:00
|
|
|
/* Expand a single string using target. */
|
|
|
|
char *
|
2020-04-13 20:51:57 +00:00
|
|
|
format_single_from_target(struct cmdq_item *item, const char *fmt)
|
2020-04-13 10:59:58 +00:00
|
|
|
{
|
2020-05-16 14:55:38 +00:00
|
|
|
struct client *tc = cmdq_get_target_client(item);
|
2020-04-13 10:59:58 +00:00
|
|
|
|
2020-05-16 14:55:38 +00:00
|
|
|
return (format_single_from_state(item, fmt, tc, cmdq_get_target(item)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create and add defaults. */
|
|
|
|
struct format_tree *
|
|
|
|
format_create_defaults(struct cmdq_item *item, struct client *c,
|
|
|
|
struct session *s, struct winlink *wl, struct window_pane *wp)
|
|
|
|
{
|
|
|
|
struct format_tree *ft;
|
|
|
|
|
|
|
|
if (item != NULL)
|
|
|
|
ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0);
|
|
|
|
else
|
|
|
|
ft = format_create(NULL, item, FORMAT_NONE, 0);
|
|
|
|
format_defaults(ft, c, s, wl, wp);
|
|
|
|
return (ft);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create and add defaults using state. */
|
|
|
|
struct format_tree *
|
|
|
|
format_create_from_state(struct cmdq_item *item, struct client *c,
|
|
|
|
struct cmd_find_state *fs)
|
|
|
|
{
|
|
|
|
return (format_create_defaults(item, c, fs->s, fs->wl, fs->wp));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create and add defaults using target. */
|
|
|
|
struct format_tree *
|
|
|
|
format_create_from_target(struct cmdq_item *item)
|
|
|
|
{
|
|
|
|
struct client *tc = cmdq_get_target_client(item);
|
|
|
|
|
|
|
|
return (format_create_from_state(item, tc, cmdq_get_target(item)));
|
2020-04-13 10:59:58 +00:00
|
|
|
}
|
|
|
|
|
2015-02-05 10:29:43 +00:00
|
|
|
/* Set defaults for any of arguments that are not NULL. */
|
|
|
|
void
|
|
|
|
format_defaults(struct format_tree *ft, struct client *c, struct session *s,
|
|
|
|
struct winlink *wl, struct window_pane *wp)
|
|
|
|
{
|
2020-03-28 09:39:27 +00:00
|
|
|
struct paste_buffer *pb;
|
|
|
|
|
2019-06-20 06:51:36 +00:00
|
|
|
if (c != NULL && c->name != NULL)
|
2019-03-28 21:05:15 +00:00
|
|
|
log_debug("%s: c=%s", __func__, c->name);
|
|
|
|
else
|
2019-06-20 06:51:36 +00:00
|
|
|
log_debug("%s: c=none", __func__);
|
2019-03-28 21:05:15 +00:00
|
|
|
if (s != NULL)
|
|
|
|
log_debug("%s: s=$%u", __func__, s->id);
|
|
|
|
else
|
|
|
|
log_debug("%s: s=none", __func__);
|
|
|
|
if (wl != NULL)
|
2020-05-16 14:55:38 +00:00
|
|
|
log_debug("%s: wl=%u", __func__, wl->idx);
|
2019-03-28 21:05:15 +00:00
|
|
|
else
|
|
|
|
log_debug("%s: wl=none", __func__);
|
|
|
|
if (wp != NULL)
|
|
|
|
log_debug("%s: wp=%%%u", __func__, wp->id);
|
|
|
|
else
|
|
|
|
log_debug("%s: wp=none", __func__);
|
|
|
|
|
2018-04-18 14:35:37 +00:00
|
|
|
if (c != NULL && s != NULL && c->session != s)
|
|
|
|
log_debug("%s: session does not match", __func__);
|
|
|
|
|
2021-02-26 21:53:41 +00:00
|
|
|
if (wp != NULL)
|
|
|
|
ft->type = FORMAT_TYPE_PANE;
|
2021-02-22 07:09:06 +00:00
|
|
|
else if (wl != NULL)
|
|
|
|
ft->type = FORMAT_TYPE_WINDOW;
|
2021-02-26 21:53:41 +00:00
|
|
|
else if (s != NULL)
|
|
|
|
ft->type = FORMAT_TYPE_SESSION;
|
2021-02-22 07:09:06 +00:00
|
|
|
else
|
|
|
|
ft->type = FORMAT_TYPE_UNKNOWN;
|
2017-08-09 11:43:45 +00:00
|
|
|
|
2015-02-05 10:29:43 +00:00
|
|
|
if (s == NULL && c != NULL)
|
|
|
|
s = c->session;
|
|
|
|
if (wl == NULL && s != NULL)
|
|
|
|
wl = s->curw;
|
|
|
|
if (wp == NULL && wl != NULL)
|
|
|
|
wp = wl->window->active;
|
|
|
|
|
|
|
|
if (c != NULL)
|
|
|
|
format_defaults_client(ft, c);
|
|
|
|
if (s != NULL)
|
|
|
|
format_defaults_session(ft, s);
|
2017-04-20 09:43:45 +00:00
|
|
|
if (wl != NULL)
|
|
|
|
format_defaults_winlink(ft, wl);
|
2015-02-05 10:29:43 +00:00
|
|
|
if (wp != NULL)
|
|
|
|
format_defaults_pane(ft, wp);
|
2020-03-28 09:39:27 +00:00
|
|
|
|
2021-08-20 17:50:42 +00:00
|
|
|
pb = paste_get_top(NULL);
|
2020-03-28 09:39:27 +00:00
|
|
|
if (pb != NULL)
|
|
|
|
format_defaults_paste_buffer(ft, pb);
|
2015-02-05 10:29:43 +00:00
|
|
|
}
|
|
|
|
|
2011-08-26 10:53:16 +00:00
|
|
|
/* Set default format keys for a session. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-02-05 10:29:43 +00:00
|
|
|
format_defaults_session(struct format_tree *ft, struct session *s)
|
2011-08-26 10:53:16 +00:00
|
|
|
{
|
2014-12-02 23:19:45 +00:00
|
|
|
ft->s = s;
|
2011-08-26 10:53:16 +00:00
|
|
|
}
|
|
|
|
|
2011-10-23 01:12:46 +00:00
|
|
|
/* Set default format keys for a client. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-02-05 10:29:43 +00:00
|
|
|
format_defaults_client(struct format_tree *ft, struct client *c)
|
2011-10-23 01:12:46 +00:00
|
|
|
{
|
2014-12-02 23:19:45 +00:00
|
|
|
if (ft->s == NULL)
|
|
|
|
ft->s = c->session;
|
Support for windows larger than visible on the attached client. This has
been a limitation for a long time.
There are two new options, window-size and default-size, and a new
command, resize-window. The force-width and force-height options and the
session_width and session_height formats have been removed.
The new window-size option tells tmux how to work out the size of
windows: largest means it picks the size of the largest session,
smallest the smallest session (similar to the old behaviour) and manual
means that it does not automatically resize windows. The default is
currently largest but this may change. aggressive-resize modifies the
choice of session for largest and smallest as it did before.
If a window is in a session attached to a client that is too small, only
part of the window is shown. tmux attempts to keep the cursor visible,
so the part of the window displayed is changed as the cursor moves (with
a small delay, to try and avoid excess redrawing when applications
redraw status lines or similar that are not currently visible). The
offset of the visible portion of the window is shown in status-right.
Drawing windows which are larger than the client is not as efficient as
those which fit, particularly when the cursor moves, so it is
recommended to avoid using this on slow machines or networks (set
window-size to smallest or manual).
The resize-window command can be used to resize a window manually. If it
is used, the window-size option is automatically set to manual for the
window (undo this with "setw -u window-size"). resize-window works in a
similar way to resize-pane (-U -D -L -R -x -y flags) but also has -a and
-A flags. -a sets the window to the size of the smallest client (what it
would be if window-size was smallest) and -A the largest.
For the same behaviour as force-width or force-height, use resize-window
-x or -y, and "setw -u window-size" to revert to automatic sizing..
If the global window-size option is set to manual, the default-size
option is used for new windows. If -x or -y is used with new-session,
that sets the default-size option for the new session.
The maximum size of a window is 10000x10000. But expect applications to
complain and much higher memory use if making a window excessively
big. The minimum size is the size required for the current layout
including borders.
The refresh-client command can be used to pan around a window, -U -D -L
-R moves up, down, left or right and -c returns to automatic cursor
tracking. The position is reset when the current window is changed.
2018-10-18 08:38:01 +00:00
|
|
|
ft->c = c;
|
2011-10-23 01:12:46 +00:00
|
|
|
}
|
|
|
|
|
2013-10-10 12:04:01 +00:00
|
|
|
/* Set default format keys for a window. */
|
2011-08-26 10:53:16 +00:00
|
|
|
void
|
2015-02-05 10:29:43 +00:00
|
|
|
format_defaults_window(struct format_tree *ft, struct window *w)
|
2011-08-26 10:53:16 +00:00
|
|
|
{
|
2014-12-02 23:19:45 +00:00
|
|
|
ft->w = w;
|
2013-10-10 12:04:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set default format keys for a winlink. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2017-04-20 09:43:45 +00:00
|
|
|
format_defaults_winlink(struct format_tree *ft, struct winlink *wl)
|
2013-10-10 12:04:01 +00:00
|
|
|
{
|
2014-12-02 23:19:45 +00:00
|
|
|
if (ft->w == NULL)
|
2021-02-22 07:09:06 +00:00
|
|
|
format_defaults_window(ft, wl->window);
|
2017-05-05 11:59:47 +00:00
|
|
|
ft->wl = wl;
|
2011-08-26 10:53:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set default format keys for a window pane. */
|
|
|
|
void
|
2015-02-05 10:29:43 +00:00
|
|
|
format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
|
2011-08-26 10:53:16 +00:00
|
|
|
{
|
2019-03-12 11:16:49 +00:00
|
|
|
struct window_mode_entry *wme;
|
2011-08-26 10:53:16 +00:00
|
|
|
|
2014-12-02 23:19:45 +00:00
|
|
|
if (ft->w == NULL)
|
2021-02-22 07:09:06 +00:00
|
|
|
format_defaults_window(ft, wp->window);
|
2015-08-28 16:46:40 +00:00
|
|
|
ft->wp = wp;
|
2014-12-02 23:19:45 +00:00
|
|
|
|
2019-03-12 11:16:49 +00:00
|
|
|
wme = TAILQ_FIRST(&wp->modes);
|
2021-02-22 07:09:06 +00:00
|
|
|
if (wme != NULL && wme->mode->formats != NULL)
|
|
|
|
wme->mode->formats(wme, ft);
|
2011-08-26 10:53:16 +00:00
|
|
|
}
|
2012-05-22 11:35:37 +00:00
|
|
|
|
2013-03-24 09:29:40 +00:00
|
|
|
/* Set default format keys for paste buffer. */
|
2012-05-22 11:35:37 +00:00
|
|
|
void
|
2015-11-12 11:09:11 +00:00
|
|
|
format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
|
2012-05-22 11:35:37 +00:00
|
|
|
{
|
2021-02-22 07:09:06 +00:00
|
|
|
ft->pb = pb;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return word at given coordinates. Caller frees. */
|
|
|
|
char *
|
|
|
|
format_grid_word(struct grid *gd, u_int x, u_int y)
|
|
|
|
{
|
|
|
|
const struct grid_line *gl;
|
|
|
|
struct grid_cell gc;
|
|
|
|
const char *ws;
|
|
|
|
struct utf8_data *ud = NULL;
|
|
|
|
u_int end;
|
|
|
|
size_t size = 0;
|
|
|
|
int found = 0;
|
|
|
|
char *s = NULL;
|
2017-08-09 11:43:45 +00:00
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
ws = options_get_string(global_s_options, "word-separators");
|
2012-05-22 11:35:37 +00:00
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
for (;;) {
|
|
|
|
grid_get_cell(gd, x, y, &gc);
|
|
|
|
if (gc.flags & GRID_FLAG_PADDING)
|
|
|
|
break;
|
2021-07-08 11:14:53 +00:00
|
|
|
if (utf8_cstrhas(ws, &gc.data) ||
|
|
|
|
(gc.data.size == 1 && *gc.data.data == ' ')) {
|
2021-02-22 07:09:06 +00:00
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
2012-05-22 11:35:37 +00:00
|
|
|
|
2021-02-22 07:09:06 +00:00
|
|
|
if (x == 0) {
|
|
|
|
if (y == 0)
|
|
|
|
break;
|
|
|
|
gl = grid_peek_line(gd, y - 1);
|
|
|
|
if (~gl->flags & GRID_LINE_WRAPPED)
|
|
|
|
break;
|
|
|
|
y--;
|
|
|
|
x = grid_line_length(gd, y);
|
|
|
|
if (x == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
x--;
|
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
if (found) {
|
|
|
|
end = grid_line_length(gd, y);
|
|
|
|
if (end == 0 || x == end - 1) {
|
|
|
|
if (y == gd->hsize + gd->sy - 1)
|
|
|
|
break;
|
|
|
|
gl = grid_peek_line(gd, y);
|
|
|
|
if (~gl->flags & GRID_LINE_WRAPPED)
|
|
|
|
break;
|
|
|
|
y++;
|
|
|
|
x = 0;
|
|
|
|
} else
|
|
|
|
x++;
|
|
|
|
}
|
|
|
|
found = 1;
|
|
|
|
|
|
|
|
grid_get_cell(gd, x, y, &gc);
|
|
|
|
if (gc.flags & GRID_FLAG_PADDING)
|
|
|
|
break;
|
2021-07-08 11:14:53 +00:00
|
|
|
if (utf8_cstrhas(ws, &gc.data) ||
|
|
|
|
(gc.data.size == 1 && *gc.data.data == ' '))
|
2021-02-22 07:09:06 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
ud = xreallocarray(ud, size + 2, sizeof *ud);
|
|
|
|
memcpy(&ud[size++], &gc.data, sizeof *ud);
|
|
|
|
}
|
|
|
|
if (size != 0) {
|
|
|
|
ud[size].size = 0;
|
|
|
|
s = utf8_tocstr(ud);
|
|
|
|
free(ud);
|
|
|
|
}
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return line at given coordinates. Caller frees. */
|
|
|
|
char *
|
|
|
|
format_grid_line(struct grid *gd, u_int y)
|
|
|
|
{
|
|
|
|
struct grid_cell gc;
|
|
|
|
struct utf8_data *ud = NULL;
|
|
|
|
u_int x;
|
|
|
|
size_t size = 0;
|
|
|
|
char *s = NULL;
|
|
|
|
|
|
|
|
for (x = 0; x < grid_line_length(gd, y); x++) {
|
|
|
|
grid_get_cell(gd, x, y, &gc);
|
|
|
|
if (gc.flags & GRID_FLAG_PADDING)
|
|
|
|
break;
|
|
|
|
|
|
|
|
ud = xreallocarray(ud, size + 2, sizeof *ud);
|
|
|
|
memcpy(&ud[size++], &gc.data, sizeof *ud);
|
|
|
|
}
|
|
|
|
if (size != 0) {
|
|
|
|
ud[size].size = 0;
|
|
|
|
s = utf8_tocstr(ud);
|
|
|
|
free(ud);
|
|
|
|
}
|
|
|
|
return (s);
|
2012-05-22 11:35:37 +00:00
|
|
|
}
|
2022-07-06 07:36:36 +00:00
|
|
|
|
|
|
|
/* Return hyperlink at given coordinates. Caller frees. */
|
|
|
|
char *
|
|
|
|
format_grid_hyperlink(struct grid *gd, u_int x, u_int y, struct screen* s)
|
|
|
|
{
|
|
|
|
const char *uri;
|
|
|
|
struct grid_cell gc;
|
|
|
|
|
|
|
|
grid_get_cell(gd, x, y, &gc);
|
|
|
|
if (gc.flags & GRID_FLAG_PADDING)
|
|
|
|
return (NULL);
|
|
|
|
if (s->hyperlinks == NULL || gc.link == 0)
|
|
|
|
return (NULL);
|
|
|
|
if (!hyperlinks_get(s->hyperlinks, gc.link, &uri, NULL, NULL))
|
|
|
|
return (NULL);
|
|
|
|
return (xstrdup(uri));
|
|
|
|
}
|