2014-11-08 12:27:43 +00:00
|
|
|
/* $OpenBSD$ */
|
2011-09-21 16:31:15 +00:00
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2011 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2011-09-21 16:31:15 +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-09-21 16:31:15 +00:00
|
|
|
|
2018-06-27 17:36:10 +00:00
|
|
|
#include <ctype.h>
|
2013-08-03 20:06:38 +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>
|
2019-06-13 19:46:00 +00:00
|
|
|
#include <regex.h>
|
2011-09-21 16:31:15 +00:00
|
|
|
#include <stdarg.h>
|
2012-07-11 19:34:16 +00:00
|
|
|
#include <stdlib.h>
|
2011-09-21 16:31:15 +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.
|
|
|
|
*/
|
|
|
|
|
2015-08-28 16:46:40 +00:00
|
|
|
struct format_entry;
|
|
|
|
typedef void (*format_cb)(struct format_tree *, struct format_entry *);
|
|
|
|
|
2016-10-10 21:29:23 +00:00
|
|
|
static char *format_job_get(struct format_tree *, const char *);
|
|
|
|
static void format_job_timer(int, short, void *);
|
|
|
|
|
|
|
|
static char *format_find(struct format_tree *, const char *, int);
|
|
|
|
static void format_add_cb(struct format_tree *, const char *, format_cb);
|
|
|
|
static void format_add_tv(struct format_tree *, const char *,
|
|
|
|
struct timeval *);
|
|
|
|
static int format_replace(struct format_tree *, const char *, size_t,
|
|
|
|
char **, size_t *, size_t *);
|
|
|
|
|
|
|
|
static void format_defaults_session(struct format_tree *,
|
|
|
|
struct session *);
|
|
|
|
static void format_defaults_client(struct format_tree *, struct client *);
|
2018-09-26 15:49:03 +00:00
|
|
|
static void format_defaults_winlink(struct format_tree *, struct winlink *);
|
2011-09-21 16:31:15 +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 struct event format_job_event;
|
|
|
|
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
|
2019-03-13 14:10:34 +00:00
|
|
|
#define FORMAT_QUOTE 0x8
|
|
|
|
#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
|
2015-10-25 22:29:17 +00:00
|
|
|
|
2019-03-14 23:34:41 +00:00
|
|
|
/* Limit on recursion. */
|
|
|
|
#define FORMAT_LOOP_LIMIT 10
|
|
|
|
|
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;
|
2015-10-25 22:29:17 +00:00
|
|
|
time_t t;
|
2015-08-28 16:46:40 +00:00
|
|
|
format_cb cb;
|
|
|
|
RB_ENTRY(format_entry) entry;
|
2014-12-02 23:19:45 +00:00
|
|
|
};
|
|
|
|
|
2015-05-27 13:28:04 +00:00
|
|
|
/* Format entry tree. */
|
2014-12-02 23:19:45 +00:00
|
|
|
struct format_tree {
|
2018-09-26 17:41:18 +00:00
|
|
|
struct client *c;
|
2015-08-28 16:46:40 +00:00
|
|
|
struct session *s;
|
2018-09-26 17:41:18 +00:00
|
|
|
struct winlink *wl;
|
|
|
|
struct window *w;
|
2015-08-28 16:46:40 +00:00
|
|
|
struct window_pane *wp;
|
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;
|
2017-02-03 11:57:27 +00:00
|
|
|
u_int tag;
|
2015-09-14 10:25:52 +00:00
|
|
|
int flags;
|
2019-03-14 23:14:27 +00:00
|
|
|
time_t time;
|
2019-03-14 23:34:41 +00:00
|
|
|
u_int loop;
|
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-09-21 16:31:15 +00:00
|
|
|
|
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-09-21 16:31:15 +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-09-21 16:31:15 +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)
|
|
|
|
format_log1(struct format_tree *ft, const char *from, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char *s;
|
|
|
|
static const char spaces[] = " ";
|
|
|
|
|
|
|
|
if (!format_logging(ft))
|
|
|
|
return;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vasprintf(&s, fmt, ap);
|
|
|
|
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))
|
2019-03-15 10:04:13 +00:00
|
|
|
cmdq_print(ft->item, "#%.*s%s", ft->loop, spaces, s);
|
|
|
|
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
#define format_log(ft, fmt, ...) format_log1(ft, __func__, fmt, ##__VA_ARGS__)
|
|
|
|
|
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 *
|
2015-05-27 13:28:04 +00:00
|
|
|
format_job_get(struct format_tree *ft, const char *cmd)
|
|
|
|
{
|
2017-05-01 12:20:55 +00:00
|
|
|
struct format_job_tree *jobs;
|
2016-11-17 10:06:08 +00:00
|
|
|
struct format_job fj0, *fj;
|
|
|
|
time_t t;
|
|
|
|
char *expanded;
|
|
|
|
int force;
|
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);
|
2016-11-17 10:06:08 +00:00
|
|
|
fj->expanded = NULL;
|
2015-05-27 13:28:04 +00:00
|
|
|
|
|
|
|
xasprintf(&fj->out, "<'%s' not ready>", fj->cmd);
|
|
|
|
|
2017-05-01 12:20:55 +00:00
|
|
|
RB_INSERT(format_job_tree, jobs, fj);
|
2015-05-27 13:28:04 +00:00
|
|
|
}
|
|
|
|
|
2016-11-17 10:06:08 +00:00
|
|
|
expanded = format_expand(ft, cmd);
|
|
|
|
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)) {
|
2018-09-27 07:43:18 +00:00
|
|
|
fj->job = job_run(expanded, NULL,
|
|
|
|
server_client_get_cwd(ft->client, NULL), format_job_update,
|
2018-03-08 08:09:10 +00:00
|
|
|
format_job_complete, NULL, fj, JOB_NOWAIT);
|
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;
|
2015-05-27 13:28:04 +00:00
|
|
|
}
|
2015-09-14 10:25:52 +00:00
|
|
|
|
|
|
|
if (ft->flags & FORMAT_STATUS)
|
|
|
|
fj->status = 1;
|
2015-05-27 13:28:04 +00:00
|
|
|
|
2016-11-17 10:06:08 +00:00
|
|
|
free(expanded);
|
2015-10-25 08:59:26 +00:00
|
|
|
return (format_expand(ft, 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
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove old jobs periodically. */
|
|
|
|
static void
|
|
|
|
format_job_timer(__unused int fd, __unused short events, __unused void *arg)
|
|
|
|
{
|
|
|
|
struct client *c;
|
|
|
|
struct timeval tv = { .tv_sec = 60 };
|
|
|
|
|
|
|
|
format_job_tidy(&format_jobs, 0);
|
|
|
|
TAILQ_FOREACH(c, &clients, entry) {
|
|
|
|
if (c->jobs != NULL)
|
|
|
|
format_job_tidy(c->jobs, 0);
|
|
|
|
}
|
2015-08-28 11:38:27 +00:00
|
|
|
|
|
|
|
evtimer_del(&format_job_event);
|
|
|
|
evtimer_add(&format_job_event, &tv);
|
2015-05-27 13:28:04 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 16:46:40 +00:00
|
|
|
/* Callback for host. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-11-18 14:27:44 +00:00
|
|
|
format_cb_host(__unused struct format_tree *ft, struct format_entry *fe)
|
2015-08-28 16:46:40 +00:00
|
|
|
{
|
|
|
|
char host[HOST_NAME_MAX + 1];
|
|
|
|
|
|
|
|
if (gethostname(host, sizeof host) != 0)
|
|
|
|
fe->value = xstrdup("");
|
|
|
|
else
|
|
|
|
fe->value = xstrdup(host);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for host_short. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-11-18 14:27:44 +00:00
|
|
|
format_cb_host_short(__unused struct format_tree *ft, struct format_entry *fe)
|
2015-08-28 16:46:40 +00:00
|
|
|
{
|
|
|
|
char host[HOST_NAME_MAX + 1], *cp;
|
|
|
|
|
|
|
|
if (gethostname(host, sizeof host) != 0)
|
|
|
|
fe->value = xstrdup("");
|
|
|
|
else {
|
|
|
|
if ((cp = strchr(host, '.')) != NULL)
|
|
|
|
*cp = '\0';
|
|
|
|
fe->value = xstrdup(host);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pid. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-11-18 14:27:44 +00:00
|
|
|
format_cb_pid(__unused struct format_tree *ft, struct format_entry *fe)
|
2015-08-28 16:46:40 +00:00
|
|
|
{
|
|
|
|
xasprintf(&fe->value, "%ld", (long)getpid());
|
|
|
|
}
|
|
|
|
|
2015-08-28 17:01:42 +00:00
|
|
|
/* Callback for session_alerts. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-08-28 17:01:42 +00:00
|
|
|
format_cb_session_alerts(struct format_tree *ft, struct format_entry *fe)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
return;
|
|
|
|
|
|
|
|
*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);
|
|
|
|
}
|
|
|
|
fe->value = xstrdup(alerts);
|
|
|
|
}
|
|
|
|
|
2017-05-05 11:59:47 +00:00
|
|
|
/* Callback for session_stack. */
|
|
|
|
static void
|
|
|
|
format_cb_session_stack(struct format_tree *ft, struct format_entry *fe)
|
|
|
|
{
|
|
|
|
struct session *s = ft->s;
|
|
|
|
struct winlink *wl;
|
|
|
|
char result[1024], tmp[16];
|
|
|
|
|
|
|
|
if (s == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
fe->value = xstrdup(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_stack_index. */
|
|
|
|
static void
|
|
|
|
format_cb_window_stack_index(struct format_tree *ft, struct format_entry *fe)
|
|
|
|
{
|
|
|
|
struct session *s = ft->wl->session;
|
|
|
|
struct winlink *wl;
|
|
|
|
u_int idx;
|
|
|
|
|
|
|
|
idx = 0;
|
|
|
|
TAILQ_FOREACH(wl, &s->lastw, sentry) {
|
|
|
|
idx++;
|
|
|
|
if (wl == ft->wl)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (wl != NULL)
|
|
|
|
xasprintf(&fe->value, "%u", idx);
|
|
|
|
else
|
|
|
|
fe->value = xstrdup("0");
|
|
|
|
}
|
|
|
|
|
2015-08-28 17:01:42 +00:00
|
|
|
/* Callback for window_layout. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-08-28 17:01:42 +00:00
|
|
|
format_cb_window_layout(struct format_tree *ft, struct format_entry *fe)
|
|
|
|
{
|
|
|
|
struct window *w = ft->w;
|
|
|
|
|
|
|
|
if (w == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (w->saved_layout_root != NULL)
|
|
|
|
fe->value = layout_dump(w->saved_layout_root);
|
|
|
|
else
|
|
|
|
fe->value = layout_dump(w->layout_root);
|
|
|
|
}
|
|
|
|
|
2015-11-13 10:00:26 +00:00
|
|
|
/* Callback for window_visible_layout. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-11-13 10:00:26 +00:00
|
|
|
format_cb_window_visible_layout(struct format_tree *ft, struct format_entry *fe)
|
|
|
|
{
|
|
|
|
struct window *w = ft->w;
|
|
|
|
|
|
|
|
if (w == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fe->value = layout_dump(w->layout_root);
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:46:40 +00:00
|
|
|
/* Callback for pane_start_command. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-08-28 16:46:40 +00:00
|
|
|
format_cb_start_command(struct format_tree *ft, struct format_entry *fe)
|
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
|
|
|
|
|
|
|
if (wp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fe->value = cmd_stringify_argv(wp->argc, wp->argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_current_command. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-08-28 16:46:40 +00:00
|
|
|
format_cb_current_command(struct format_tree *ft, struct format_entry *fe)
|
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
|
|
|
char *cmd;
|
|
|
|
|
|
|
|
if (wp == NULL)
|
|
|
|
return;
|
|
|
|
|
2015-08-30 20:47:50 +00:00
|
|
|
cmd = osdep_get_name(wp->fd, wp->tty);
|
2015-08-28 16:46:40 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fe->value = parse_window_name(cmd);
|
|
|
|
free(cmd);
|
|
|
|
}
|
|
|
|
|
2015-08-31 18:57:37 +00:00
|
|
|
/* Callback for pane_current_path. */
|
2017-05-31 15:29:07 +00:00
|
|
|
static void
|
2015-08-31 18:57:37 +00:00
|
|
|
format_cb_current_path(struct format_tree *ft, struct format_entry *fe)
|
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
|
|
|
char *cwd;
|
|
|
|
|
|
|
|
if (wp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
cwd = osdep_get_cwd(wp->fd);
|
|
|
|
if (cwd != NULL)
|
|
|
|
fe->value = xstrdup(cwd);
|
|
|
|
}
|
|
|
|
|
2015-08-28 17:01:42 +00:00
|
|
|
/* Callback for history_bytes. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-08-28 17:01:42 +00:00
|
|
|
format_cb_history_bytes(struct format_tree *ft, struct format_entry *fe)
|
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
|
|
|
struct grid *gd;
|
|
|
|
struct grid_line *gl;
|
|
|
|
unsigned long long size;
|
|
|
|
u_int i;
|
|
|
|
|
|
|
|
if (wp == NULL)
|
|
|
|
return;
|
|
|
|
gd = wp->base.grid;
|
|
|
|
|
|
|
|
size = 0;
|
|
|
|
for (i = 0; i < gd->hsize; 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
|
|
|
}
|
2018-07-04 09:44:07 +00:00
|
|
|
size += gd->hsize * sizeof *gl;
|
2015-08-28 17:01:42 +00:00
|
|
|
|
|
|
|
xasprintf(&fe->value, "%llu", size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_tabs. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-08-28 17:01:42 +00:00
|
|
|
format_cb_pane_tabs(struct format_tree *ft, struct format_entry *fe)
|
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
|
|
|
struct evbuffer *buffer;
|
|
|
|
u_int i;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
if (wp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
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)
|
|
|
|
xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer));
|
|
|
|
evbuffer_free(buffer);
|
|
|
|
}
|
|
|
|
|
2017-11-02 18:52:05 +00:00
|
|
|
/* Callback for session_group_list. */
|
2017-11-02 18:27:35 +00:00
|
|
|
static void
|
2017-11-02 18:52:05 +00:00
|
|
|
format_cb_session_group_list(struct format_tree *ft, struct format_entry *fe)
|
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;
|
|
|
|
|
|
|
|
if (s == NULL)
|
|
|
|
return;
|
|
|
|
sg = session_group_contains(s);
|
|
|
|
if (sg == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
buffer = evbuffer_new();
|
2018-11-19 13:35:40 +00:00
|
|
|
if (buffer == NULL)
|
|
|
|
fatalx("out of memory");
|
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);
|
|
|
|
}
|
|
|
|
if ((size = EVBUFFER_LENGTH(buffer)) != 0)
|
|
|
|
xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer));
|
2015-08-28 17:01:42 +00:00
|
|
|
evbuffer_free(buffer);
|
|
|
|
}
|
|
|
|
|
2019-03-12 11:16:49 +00:00
|
|
|
/* Callback for pane_in_mode. */
|
|
|
|
static void
|
|
|
|
format_cb_pane_in_mode(struct format_tree *ft, struct format_entry *fe)
|
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
|
|
|
u_int n = 0;
|
|
|
|
struct window_mode_entry *wme;
|
|
|
|
|
|
|
|
if (wp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(wme, &wp->modes, entry)
|
|
|
|
n++;
|
|
|
|
xasprintf(&fe->value, "%u", n);
|
|
|
|
}
|
|
|
|
|
2019-03-19 19:01:50 +00:00
|
|
|
/* Callback for cursor_character. */
|
|
|
|
static void
|
|
|
|
format_cb_cursor_character(struct format_tree *ft, struct format_entry *fe)
|
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
|
|
|
struct grid_cell gc;
|
|
|
|
|
|
|
|
if (wp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
grid_view_get_cell(wp->base.grid, wp->base.cx, wp->base.cy, &gc);
|
|
|
|
if (~gc.flags & GRID_FLAG_PADDING)
|
|
|
|
xasprintf(&fe->value, "%.*s", (int)gc.data.size, gc.data.data);
|
|
|
|
}
|
|
|
|
|
2019-05-26 17:34:45 +00:00
|
|
|
/* Callback for mouse_word. */
|
|
|
|
static void
|
|
|
|
format_cb_mouse_word(struct format_tree *ft, struct format_entry *fe)
|
|
|
|
{
|
|
|
|
struct window_pane *wp;
|
|
|
|
u_int x, y, end;
|
|
|
|
struct grid *gd;
|
|
|
|
struct grid_line *gl;
|
|
|
|
struct grid_cell gc;
|
|
|
|
const char *ws;
|
|
|
|
struct utf8_data *ud = NULL;
|
|
|
|
size_t size = 0;
|
|
|
|
int found = 0;
|
|
|
|
|
|
|
|
if (!ft->m.valid)
|
|
|
|
return;
|
|
|
|
wp = cmd_mouse_pane(&ft->m, NULL, NULL);
|
|
|
|
if (wp == NULL)
|
|
|
|
return;
|
|
|
|
if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0)
|
|
|
|
return;
|
|
|
|
gd = wp->base.grid;
|
|
|
|
ws = options_get_string(global_s_options, "word-separators");
|
|
|
|
|
|
|
|
y = gd->hsize + y;
|
|
|
|
for (;;) {
|
|
|
|
grid_get_cell(gd, x, y, &gc);
|
|
|
|
if (gc.flags & GRID_FLAG_PADDING)
|
|
|
|
break;
|
|
|
|
if (utf8_cstrhas(ws, &gc.data)) {
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x == 0) {
|
|
|
|
if (y == 0)
|
|
|
|
break;
|
|
|
|
gl = &gd->linedata[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 = &gd->linedata[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;
|
|
|
|
if (utf8_cstrhas(ws, &gc.data))
|
|
|
|
break;
|
|
|
|
|
|
|
|
ud = xreallocarray(ud, size + 2, sizeof *ud);
|
|
|
|
memcpy(&ud[size++], &gc.data, sizeof *ud);
|
|
|
|
}
|
|
|
|
if (size != 0) {
|
|
|
|
ud[size].size = 0;
|
|
|
|
fe->value = utf8_tocstr(ud);
|
|
|
|
free(ud);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for mouse_line. */
|
|
|
|
static void
|
|
|
|
format_cb_mouse_line(struct format_tree *ft, struct format_entry *fe)
|
|
|
|
{
|
|
|
|
struct window_pane *wp;
|
|
|
|
u_int x, y;
|
|
|
|
struct grid *gd;
|
|
|
|
struct grid_cell gc;
|
|
|
|
struct utf8_data *ud = NULL;
|
|
|
|
size_t size = 0;
|
|
|
|
|
|
|
|
if (!ft->m.valid)
|
|
|
|
return;
|
|
|
|
wp = cmd_mouse_pane(&ft->m, NULL, NULL);
|
|
|
|
if (wp == NULL)
|
|
|
|
return;
|
|
|
|
if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0)
|
|
|
|
return;
|
|
|
|
gd = wp->base.grid;
|
|
|
|
|
|
|
|
y = gd->hsize + y;
|
|
|
|
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;
|
|
|
|
fe->value = utf8_tocstr(ud);
|
|
|
|
free(ud);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-16 19:36:37 +00:00
|
|
|
/* Merge a format tree. */
|
|
|
|
static void
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2019-05-26 17:34:45 +00:00
|
|
|
struct mouse_event *m;
|
|
|
|
struct window_pane *wp;
|
|
|
|
u_int x, y;
|
|
|
|
|
2019-05-26 12:02:42 +00:00
|
|
|
if (item->cmd != NULL)
|
|
|
|
format_add(ft, "command", "%s", item->cmd->entry->name);
|
2019-05-26 17:34:45 +00:00
|
|
|
|
|
|
|
if (item->shared == NULL)
|
|
|
|
return;
|
|
|
|
if (item->shared->formats != NULL)
|
2019-05-26 12:02:42 +00:00
|
|
|
format_merge(ft, item->shared->formats);
|
2019-05-26 17:34:45 +00:00
|
|
|
|
|
|
|
m = &item->shared->mouse;
|
|
|
|
if (m->valid && ((wp = cmd_mouse_pane(m, NULL, NULL)) != NULL)) {
|
|
|
|
format_add(ft, "mouse_pane", "%%%u", wp->id);
|
|
|
|
if (cmd_mouse_at(wp, m, &x, &y, 0) == 0) {
|
|
|
|
format_add(ft, "mouse_x", "%u", x);
|
|
|
|
format_add(ft, "mouse_y", "%u", y);
|
|
|
|
format_add_cb(ft, "mouse_word", format_cb_mouse_word);
|
|
|
|
format_add_cb(ft, "mouse_line", format_cb_mouse_line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memcpy(&ft->m, m, sizeof ft->m);
|
2019-05-26 12:02:42 +00:00
|
|
|
}
|
|
|
|
|
2011-09-21 16:31:15 +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-09-21 16:31:15 +00:00
|
|
|
{
|
2019-03-18 14:10:25 +00:00
|
|
|
struct format_tree *ft;
|
|
|
|
const struct window_mode **wm;
|
|
|
|
char tmp[64];
|
2011-09-21 16:31:15 +00:00
|
|
|
|
2015-08-28 11:38:27 +00:00
|
|
|
if (!event_initialized(&format_job_event)) {
|
|
|
|
evtimer_set(&format_job_event, format_job_timer, NULL);
|
|
|
|
format_job_timer(-1, 0, NULL);
|
|
|
|
}
|
|
|
|
|
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;
|
2019-03-14 23:14:27 +00:00
|
|
|
ft->time = time(NULL);
|
2011-09-21 16:31:15 +00:00
|
|
|
|
2017-01-09 21:44:19 +00:00
|
|
|
format_add(ft, "version", "%s", VERSION);
|
2015-08-28 16:46:40 +00:00
|
|
|
format_add_cb(ft, "host", format_cb_host);
|
|
|
|
format_add_cb(ft, "host_short", format_cb_host_short);
|
|
|
|
format_add_cb(ft, "pid", format_cb_pid);
|
2015-11-24 21:52:06 +00:00
|
|
|
format_add(ft, "socket_path", "%s", socket_path);
|
|
|
|
format_add_tv(ft, "start_time", &start_time);
|
2011-09-21 16:31:15 +00:00
|
|
|
|
2019-03-18 14:10:25 +00:00
|
|
|
for (wm = all_window_modes; *wm != NULL; wm++) {
|
|
|
|
if ((*wm)->default_format != NULL) {
|
|
|
|
xsnprintf(tmp, sizeof tmp, "%s_format", (*wm)->name);
|
|
|
|
tmp[strcspn(tmp, "-")] = '_';
|
|
|
|
format_add(ft, tmp, "%s", (*wm)->default_format);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-09-21 16:31:15 +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-09-21 16:31:15 +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-11 19:34:16 +00:00
|
|
|
free(fe->value);
|
|
|
|
free(fe->key);
|
|
|
|
free(fe);
|
2011-09-21 16:31:15 +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-09-21 16:31:15 +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)
|
|
|
|
{
|
|
|
|
struct format_entry *fe;
|
|
|
|
static char s[64];
|
|
|
|
|
|
|
|
RB_FOREACH(fe, format_entry_tree, &ft->tree) {
|
|
|
|
if (fe->t != 0) {
|
|
|
|
xsnprintf(s, sizeof s, "%lld", (long long)fe->t);
|
|
|
|
cb(fe->key, fe->value, s);
|
|
|
|
} else {
|
|
|
|
if (fe->value == NULL && fe->cb != NULL) {
|
|
|
|
fe->cb(ft, fe);
|
|
|
|
if (fe->value == NULL)
|
|
|
|
fe->value = xstrdup("");
|
|
|
|
}
|
|
|
|
cb(fe->key, fe->value, arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-21 16:31:15 +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-08-01 22:40:44 +00:00
|
|
|
struct format_entry *fe_now;
|
2011-09-21 16:31:15 +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;
|
2015-10-25 22:29:17 +00:00
|
|
|
fe->t = 0;
|
2015-08-28 16:46:40 +00:00
|
|
|
|
2011-09-21 16:31:15 +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. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2015-10-25 22:29:17 +00:00
|
|
|
format_add_tv(struct format_tree *ft, const char *key, struct timeval *tv)
|
|
|
|
{
|
|
|
|
struct format_entry *fe;
|
|
|
|
struct format_entry *fe_now;
|
|
|
|
|
|
|
|
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;
|
|
|
|
fe->t = tv->tv_sec;
|
|
|
|
|
|
|
|
fe->value = NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:46:40 +00:00
|
|
|
/* Add a key and function. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static 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-09-21 16:31:15 +00:00
|
|
|
|
2015-05-27 13:28:04 +00:00
|
|
|
fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
|
2013-08-01 22:40:44 +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-08-01 22:40:44 +00:00
|
|
|
}
|
2015-08-28 16:46:40 +00:00
|
|
|
|
|
|
|
fe->cb = cb;
|
2015-10-25 22:29:17 +00:00
|
|
|
fe->t = 0;
|
2015-08-28 16:46:40 +00:00
|
|
|
|
|
|
|
fe->value = NULL;
|
2011-09-21 16:31:15 +00:00
|
|
|
}
|
|
|
|
|
2018-08-26 09:28:42 +00:00
|
|
|
/* Quote special characters in string. */
|
|
|
|
static char *
|
|
|
|
format_quote(const char *s)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-09-21 16:31:15 +00:00
|
|
|
/* Find a format entry. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static char *
|
2015-10-25 22:29:17 +00:00
|
|
|
format_find(struct format_tree *ft, const char *key, int modifiers)
|
2011-09-21 16:31:15 +00:00
|
|
|
{
|
|
|
|
struct format_entry *fe, fe_find;
|
2015-08-28 10:06:52 +00:00
|
|
|
struct environ_entry *envent;
|
2015-10-25 22:29:17 +00:00
|
|
|
static char s[64];
|
2017-01-16 14:49:14 +00:00
|
|
|
struct options_entry *o;
|
2017-01-15 20:48:41 +00:00
|
|
|
int idx;
|
2019-04-25 18:18:55 +00:00
|
|
|
char *found, *saved;
|
2015-10-25 22:29:17 +00:00
|
|
|
|
|
|
|
if (~modifiers & FORMAT_TIMESTRING) {
|
2017-01-15 20:48:41 +00:00
|
|
|
o = options_parse_get(global_options, key, &idx, 0);
|
2019-06-20 11:59:59 +00:00
|
|
|
if (o == NULL && ft->wp != NULL)
|
|
|
|
o = options_parse_get(ft->wp->options, key, &idx, 0);
|
2015-10-25 22:29:17 +00:00
|
|
|
if (o == NULL && ft->w != NULL)
|
2017-01-15 20:48:41 +00:00
|
|
|
o = options_parse_get(ft->w->options, key, &idx, 0);
|
2015-10-25 22:29:17 +00:00
|
|
|
if (o == NULL)
|
2017-01-15 20:48:41 +00:00
|
|
|
o = options_parse_get(global_w_options, key, &idx, 0);
|
2015-10-25 22:29:17 +00:00
|
|
|
if (o == NULL && ft->s != NULL)
|
2017-01-15 20:48:41 +00:00
|
|
|
o = options_parse_get(ft->s->options, key, &idx, 0);
|
2015-10-25 22:29:17 +00:00
|
|
|
if (o == NULL)
|
2017-01-15 20:48:41 +00:00
|
|
|
o = options_parse_get(global_s_options, key, &idx, 0);
|
2015-10-25 22:29:17 +00:00
|
|
|
if (o != NULL) {
|
2017-01-30 21:41:17 +00:00
|
|
|
found = options_tostring(o, idx, 1);
|
2017-01-15 20:48:41 +00:00
|
|
|
goto found;
|
2014-12-02 23:19:45 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-15 20:48:41 +00:00
|
|
|
found = NULL;
|
2011-09-21 16:31:15 +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) {
|
2015-10-25 22:29:17 +00:00
|
|
|
if (modifiers & FORMAT_TIMESTRING) {
|
|
|
|
if (fe->t == 0)
|
|
|
|
return (NULL);
|
|
|
|
ctime_r(&fe->t, s);
|
|
|
|
s[strcspn(s, "\n")] = '\0';
|
2019-04-25 18:18:55 +00:00
|
|
|
found = xstrdup(s);
|
2015-10-25 22:29:17 +00:00
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
if (fe->t != 0) {
|
2019-04-25 18:18:55 +00:00
|
|
|
xasprintf(&found, "%lld", (long long)fe->t);
|
2015-10-25 22:29:17 +00:00
|
|
|
goto found;
|
|
|
|
}
|
2017-11-02 18:43:51 +00:00
|
|
|
if (fe->value == NULL && fe->cb != NULL) {
|
2015-08-28 16:46:40 +00:00
|
|
|
fe->cb(ft, fe);
|
2017-11-02 18:43:51 +00:00
|
|
|
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:
|
2015-10-27 09:18:06 +00:00
|
|
|
if (found == NULL)
|
|
|
|
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);
|
|
|
|
}
|
2018-08-26 09:28:42 +00:00
|
|
|
if (modifiers & FORMAT_QUOTE) {
|
2019-04-25 18:18:55 +00:00
|
|
|
saved = found;
|
|
|
|
found = xstrdup(format_quote(saved));
|
2018-08-26 09:28:42 +00:00
|
|
|
free(saved);
|
|
|
|
}
|
2019-04-25 18:18:55 +00:00
|
|
|
return (found);
|
2011-09-21 16:31:15 +00:00
|
|
|
}
|
|
|
|
|
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++;
|
2018-05-22 08:49:12 +00:00
|
|
|
if (*s == '#' && strchr(",#{}", s[1]) != NULL) {
|
|
|
|
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
|
2019-03-13 14:10:34 +00:00
|
|
|
format_choose(struct format_tree *ft, 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) {
|
|
|
|
*left = format_expand(ft, left0);
|
2019-03-29 09:33:24 +00:00
|
|
|
free(left0);
|
2019-03-13 14:10:34 +00:00
|
|
|
*right = format_expand(ft, 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-09-21 16:31:15 +00:00
|
|
|
{
|
2019-03-13 14:10:34 +00:00
|
|
|
return (c == ';' || c == ':');
|
|
|
|
}
|
2011-09-21 16:31:15 +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 *
|
|
|
|
format_build_modifiers(struct format_tree *ft, const char **s, u_int *count)
|
|
|
|
{
|
|
|
|
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:
|
2019-05-25 16:51:10 +00:00
|
|
|
* l,m,C,b,d,t,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 != ':') {
|
|
|
|
/* Skip and separator character. */
|
|
|
|
if (*cp == ';')
|
|
|
|
cp++;
|
|
|
|
|
|
|
|
/* Check single character modifiers with no arguments. */
|
2019-06-13 19:46:00 +00:00
|
|
|
if (strchr("lbdtqETSWP<>", 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. */
|
2019-06-13 19:46:00 +00:00
|
|
|
if (strchr("mCs=", 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. */
|
|
|
|
if (!ispunct(cp[1]) || cp[1] == '-') {
|
|
|
|
end = format_skip(cp + 1, ":;");
|
|
|
|
if (end == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
argv = xcalloc(1, sizeof *argv);
|
|
|
|
value = xstrndup(cp + 1, end - (cp + 1));
|
|
|
|
argv[0] = format_expand(ft, value);
|
|
|
|
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++;
|
|
|
|
|
|
|
|
argv = xreallocarray (argv, argc + 1, sizeof *argv);
|
|
|
|
value = xstrndup(cp, end - cp);
|
|
|
|
argv[argc++] = format_expand(ft, value);
|
|
|
|
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;
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-03-13 15:37:28 +00:00
|
|
|
/* Loop over sessions. */
|
|
|
|
static char *
|
|
|
|
format_loop_sessions(struct format_tree *ft, const char *fmt)
|
|
|
|
{
|
2019-03-15 10:07:24 +00:00
|
|
|
struct client *c = ft->client;
|
2019-03-13 15:37:28 +00:00
|
|
|
struct cmdq_item *item = ft->item;
|
2019-03-15 10:07:24 +00:00
|
|
|
struct format_tree *nft;
|
2019-03-13 15:37:28 +00:00
|
|
|
char *expanded, *value;
|
|
|
|
size_t valuelen;
|
|
|
|
struct session *s;
|
|
|
|
|
|
|
|
value = xcalloc(1, 1);
|
|
|
|
valuelen = 1;
|
|
|
|
|
|
|
|
RB_FOREACH(s, sessions, &sessions) {
|
2019-03-15 10:04:13 +00:00
|
|
|
format_log(ft, "session loop: $%u", s->id);
|
2019-03-15 10:07:24 +00:00
|
|
|
nft = format_create(c, item, FORMAT_NONE, ft->flags);
|
2019-03-15 15:02:25 +00:00
|
|
|
nft->loop = ft->loop;
|
2019-03-15 10:07:24 +00:00
|
|
|
format_defaults(nft, ft->c, s, NULL, NULL);
|
|
|
|
expanded = format_expand(nft, fmt);
|
|
|
|
format_free(nft);
|
2019-03-13 15:37:28 +00:00
|
|
|
|
|
|
|
valuelen += strlen(expanded);
|
|
|
|
value = xrealloc(value, valuelen);
|
|
|
|
|
|
|
|
strlcat(value, expanded, valuelen);
|
|
|
|
free(expanded);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop over windows. */
|
|
|
|
static char *
|
|
|
|
format_loop_windows(struct format_tree *ft, const char *fmt)
|
|
|
|
{
|
2019-03-15 10:07:24 +00:00
|
|
|
struct client *c = ft->client;
|
2019-03-13 15:37:28 +00:00
|
|
|
struct cmdq_item *item = ft->item;
|
2019-03-15 10:07:24 +00:00
|
|
|
struct format_tree *nft;
|
2019-03-13 15:37:28 +00:00
|
|
|
char *all, *active, *use, *expanded, *value;
|
|
|
|
size_t valuelen;
|
|
|
|
struct winlink *wl;
|
2019-03-15 10:07:24 +00:00
|
|
|
struct window *w;
|
2019-03-13 15:37:28 +00:00
|
|
|
|
2019-03-15 10:04:13 +00:00
|
|
|
if (ft->s == NULL) {
|
|
|
|
format_log(ft, "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
|
|
|
|
|
|
|
if (format_choose(ft, fmt, &all, &active, 0) != 0) {
|
|
|
|
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;
|
|
|
|
format_log(ft, "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);
|
2019-03-15 15:02:25 +00:00
|
|
|
nft->loop = ft->loop;
|
2019-03-15 10:07:24 +00:00
|
|
|
format_defaults(nft, ft->c, ft->s, wl, NULL);
|
|
|
|
expanded = format_expand(nft, use);
|
|
|
|
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 *
|
|
|
|
format_loop_panes(struct format_tree *ft, const char *fmt)
|
|
|
|
{
|
2019-03-15 10:07:24 +00:00
|
|
|
struct client *c = ft->client;
|
2019-03-13 15:37:28 +00:00
|
|
|
struct cmdq_item *item = ft->item;
|
2019-03-15 10:07:24 +00:00
|
|
|
struct format_tree *nft;
|
2019-03-13 15:37:28 +00:00
|
|
|
char *all, *active, *use, *expanded, *value;
|
|
|
|
size_t valuelen;
|
|
|
|
struct window_pane *wp;
|
|
|
|
|
2019-03-15 10:04:13 +00:00
|
|
|
if (ft->w == NULL) {
|
|
|
|
format_log(ft, "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
|
|
|
|
|
|
|
if (format_choose(ft, fmt, &all, &active, 0) != 0) {
|
|
|
|
all = xstrdup(fmt);
|
|
|
|
active = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = xcalloc(1, 1);
|
|
|
|
valuelen = 1;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(wp, &ft->w->panes, entry) {
|
2019-03-15 10:04:13 +00:00
|
|
|
format_log(ft, "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);
|
2019-03-15 15:02:25 +00:00
|
|
|
nft->loop = ft->loop;
|
2019-03-15 10:07:24 +00:00
|
|
|
format_defaults(nft, ft->c, ft->s, ft->wl, wp);
|
|
|
|
expanded = format_expand(nft, use);
|
|
|
|
format_free(nft);
|
2019-03-13 15:37:28 +00:00
|
|
|
|
|
|
|
valuelen += strlen(expanded);
|
|
|
|
value = xrealloc(value, valuelen);
|
|
|
|
|
|
|
|
strlcat(value, expanded, valuelen);
|
|
|
|
free(expanded);
|
2013-08-03 20:06:38 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 15:37:28 +00:00
|
|
|
free(active);
|
|
|
|
free(all);
|
|
|
|
|
|
|
|
return (value);
|
|
|
|
}
|
|
|
|
|
2019-03-13 14:10:34 +00:00
|
|
|
/* Replace a key. */
|
|
|
|
static int
|
|
|
|
format_replace(struct format_tree *ft, const char *key, size_t keylen,
|
|
|
|
char **buf, size_t *len, size_t *off)
|
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
2019-05-31 21:41:17 +00:00
|
|
|
const char *errptr, *copy, *cp, *marker = NULL;
|
2019-03-13 14:10:34 +00:00
|
|
|
char *copy0, *condition, *found, *new;
|
|
|
|
char *value, *left, *right;
|
|
|
|
size_t valuelen;
|
2019-06-13 19:46:00 +00:00
|
|
|
int modifiers = 0, limit = 0, j;
|
2019-03-13 14:10:34 +00:00
|
|
|
struct format_modifier *list, *fm, *cmp = NULL, *search = NULL;
|
|
|
|
struct format_modifier *sub = NULL;
|
|
|
|
u_int i, count;
|
|
|
|
|
|
|
|
/* Make a copy of the key. */
|
|
|
|
copy = copy0 = xstrndup(key, keylen);
|
|
|
|
|
|
|
|
/* Process modifier list. */
|
|
|
|
list = format_build_modifiers(ft, ©, &count);
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
fm = &list[i];
|
2019-03-15 10:04:13 +00:00
|
|
|
if (format_logging(ft)) {
|
|
|
|
format_log(ft, "modifier %u is %s", i, fm->modifier);
|
|
|
|
for (j = 0; j < fm->argc; j++) {
|
|
|
|
format_log(ft, "modifier %u argument %d: %s", i,
|
|
|
|
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;
|
|
|
|
sub = fm;
|
|
|
|
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,
|
|
|
|
&errptr);
|
|
|
|
if (errptr != NULL)
|
|
|
|
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;
|
|
|
|
case 'l':
|
|
|
|
modifiers |= FORMAT_LITERAL;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
modifiers |= FORMAT_BASENAME;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
modifiers |= FORMAT_DIRNAME;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
modifiers |= FORMAT_TIMESTRING;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
modifiers |= FORMAT_QUOTE;
|
|
|
|
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;
|
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;
|
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) {
|
2018-05-22 08:49:12 +00:00
|
|
|
value = xstrdup(copy);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2019-03-13 15:37:28 +00:00
|
|
|
/* Is this a loop, comparison or condition? */
|
|
|
|
if (modifiers & FORMAT_SESSIONS) {
|
|
|
|
value = format_loop_sessions(ft, copy);
|
|
|
|
if (value == NULL)
|
|
|
|
goto fail;
|
|
|
|
} else if (modifiers & FORMAT_WINDOWS) {
|
|
|
|
value = format_loop_windows(ft, copy);
|
|
|
|
if (value == NULL)
|
|
|
|
goto fail;
|
|
|
|
} else if (modifiers & FORMAT_PANES) {
|
|
|
|
value = format_loop_panes(ft, copy);
|
|
|
|
if (value == NULL)
|
|
|
|
goto fail;
|
|
|
|
} else if (search != NULL) {
|
2017-05-29 18:06:34 +00:00
|
|
|
/* Search in pane. */
|
2019-06-24 10:04:29 +00:00
|
|
|
new = format_expand(ft, copy);
|
2019-03-15 10:04:13 +00:00
|
|
|
if (wp == NULL) {
|
2019-06-24 10:04:29 +00:00
|
|
|
format_log(ft, "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 {
|
2019-06-24 10:04:29 +00:00
|
|
|
format_log(ft, "search '%s' pane %%%u", new, wp->id);
|
|
|
|
value = format_search(fm, 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. */
|
2019-03-15 10:04:13 +00:00
|
|
|
if (format_choose(ft, copy, &left, &right, 1) != 0) {
|
|
|
|
format_log(ft, "compare %s syntax error: %s",
|
|
|
|
cmp->modifier, copy);
|
2017-01-09 21:03:25 +00:00
|
|
|
goto fail;
|
2019-03-15 10:04:13 +00:00
|
|
|
}
|
|
|
|
format_log(ft, "compare %s left is: %s", cmp->modifier, left);
|
|
|
|
format_log(ft, "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) {
|
|
|
|
format_log(ft, "condition syntax error: %s", copy + 1);
|
2011-09-21 16:31:15 +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));
|
2019-03-15 10:04:13 +00:00
|
|
|
format_log(ft, "condition is: %s", condition);
|
2011-09-21 16:31:15 +00:00
|
|
|
|
2019-03-13 14:10:34 +00:00
|
|
|
found = format_find(ft, condition, modifiers);
|
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.
|
|
|
|
*/
|
2019-03-13 14:10:34 +00:00
|
|
|
found = format_expand(ft, condition);
|
|
|
|
if (strcmp(found, condition) == 0) {
|
2018-05-29 09:10:30 +00:00
|
|
|
free(found);
|
|
|
|
found = xstrdup("");
|
2019-03-15 10:04:13 +00:00
|
|
|
format_log(ft, "condition '%s' found: %s",
|
|
|
|
condition, found);
|
|
|
|
} else {
|
|
|
|
format_log(ft,
|
|
|
|
"condition '%s' not found; assuming false",
|
|
|
|
condition);
|
2018-05-29 09:10:30 +00:00
|
|
|
}
|
2019-03-15 10:04:13 +00:00
|
|
|
} else
|
|
|
|
format_log(ft, "condition '%s' found", condition);
|
2019-03-13 14:10:34 +00:00
|
|
|
|
|
|
|
if (format_choose(ft, cp + 1, &left, &right, 0) != 0) {
|
2019-03-15 10:04:13 +00:00
|
|
|
format_log(ft, "condition '%s' syntax error: %s",
|
|
|
|
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)) {
|
|
|
|
format_log(ft, "condition '%s' is true", condition);
|
2017-01-09 21:03:25 +00:00
|
|
|
value = format_expand(ft, left);
|
2019-03-15 10:04:13 +00:00
|
|
|
} else {
|
|
|
|
format_log(ft, "condition '%s' is false", condition);
|
2017-01-09 21:03:25 +00:00
|
|
|
value = format_expand(ft, 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);
|
2011-09-21 16:31:15 +00:00
|
|
|
} else {
|
2017-01-09 21:03:25 +00:00
|
|
|
/* Neither: look up directly. */
|
2015-11-18 14:13:55 +00:00
|
|
|
value = format_find(ft, copy, modifiers);
|
2019-03-15 10:04:13 +00:00
|
|
|
if (value == NULL) {
|
|
|
|
format_log(ft, "format '%s' not found", copy);
|
2015-11-18 14:13:55 +00:00
|
|
|
value = xstrdup("");
|
2019-03-15 10:04:13 +00:00
|
|
|
} else
|
|
|
|
format_log(ft, "format '%s' found: %s", copy, value);
|
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) {
|
|
|
|
new = format_expand(ft, value);
|
|
|
|
free(value);
|
|
|
|
value = new;
|
|
|
|
}
|
2019-03-14 21:31:43 +00:00
|
|
|
else if (modifiers & FORMAT_EXPANDTIME) {
|
2019-03-14 23:14:27 +00:00
|
|
|
new = format_expand_time(ft, value);
|
2019-03-14 21:31:43 +00:00
|
|
|
free(value);
|
|
|
|
value = new;
|
|
|
|
}
|
2015-11-18 14:13:55 +00:00
|
|
|
|
|
|
|
/* Perform substitution if any. */
|
2019-03-13 14:10:34 +00:00
|
|
|
if (sub != NULL) {
|
2019-06-24 10:04:29 +00:00
|
|
|
left = format_expand(ft, sub->argv[0]);
|
|
|
|
right = format_expand(ft, sub->argv[1]);
|
|
|
|
new = format_sub(sub, value, left, right);
|
|
|
|
format_log(ft, "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-09-21 16:31:15 +00:00
|
|
|
}
|
|
|
|
|
2013-08-03 20:06:38 +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;
|
|
|
|
}
|
|
|
|
format_log(ft, "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;
|
|
|
|
}
|
|
|
|
format_log(ft, "applied length limit %d: %s", limit, value);
|
2014-04-17 15:37:55 +00:00
|
|
|
}
|
2013-08-03 20:06:38 +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-09-21 16:31:15 +00:00
|
|
|
while (*len - *off < valuelen + 1) {
|
2014-10-08 17:35:58 +00:00
|
|
|
*buf = xreallocarray(*buf, 2, *len);
|
2011-09-21 16:31:15 +00:00
|
|
|
*len *= 2;
|
|
|
|
}
|
|
|
|
memcpy(*buf + *off, value, valuelen);
|
|
|
|
*off += valuelen;
|
|
|
|
|
2019-03-15 10:04:13 +00:00
|
|
|
format_log(ft, "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-03-13 14:10:34 +00:00
|
|
|
format_free_modifiers(list, count);
|
2013-08-03 20:06:38 +00:00
|
|
|
free(copy0);
|
2011-09-21 16:31:15 +00:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
fail:
|
2019-03-15 10:04:13 +00:00
|
|
|
format_log(ft, "failed %s", copy0);
|
2019-03-13 14:10:34 +00:00
|
|
|
format_free_modifiers(list, count);
|
2013-08-03 20:06:38 +00:00
|
|
|
free(copy0);
|
2011-09-21 16:31:15 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2011-08-26 10:53:16 +00:00
|
|
|
/* Expand keys in a template. */
|
2019-03-15 10:04:13 +00:00
|
|
|
static char *
|
|
|
|
format_expand1(struct format_tree *ft, const char *fmt, int time)
|
2015-02-06 17:11:39 +00:00
|
|
|
{
|
2018-02-20 10:43:46 +00:00
|
|
|
char *buf, *out, *name;
|
2019-03-15 10:04:13 +00:00
|
|
|
const char *ptr, *s;
|
2015-10-25 08:59:26 +00:00
|
|
|
size_t off, len, n, outlen;
|
2013-10-10 11:50:36 +00:00
|
|
|
int ch, brackets;
|
2015-02-06 17:11:39 +00:00
|
|
|
struct tm *tm;
|
2019-03-15 10:04:13 +00:00
|
|
|
char expanded[8192];
|
2015-02-06 17:11:39 +00:00
|
|
|
|
2015-05-20 06:39:02 +00:00
|
|
|
if (fmt == NULL || *fmt == '\0')
|
2015-02-06 17:11:39 +00:00
|
|
|
return (xstrdup(""));
|
|
|
|
|
2019-03-14 23:34:41 +00:00
|
|
|
if (ft->loop == FORMAT_LOOP_LIMIT)
|
2016-05-23 20:39:26 +00:00
|
|
|
return (xstrdup(""));
|
2019-03-14 23:34:41 +00:00
|
|
|
ft->loop++;
|
2015-02-06 17:11:39 +00:00
|
|
|
|
2019-03-15 10:04:13 +00:00
|
|
|
format_log(ft, "expanding format: %s", fmt);
|
2011-09-21 16:31:15 +00:00
|
|
|
|
2019-03-15 10:04:13 +00:00
|
|
|
if (time) {
|
|
|
|
tm = localtime(&ft->time);
|
|
|
|
if (strftime(expanded, sizeof expanded, fmt, tm) == 0) {
|
|
|
|
format_log(ft, "format is too long");
|
|
|
|
return (xstrdup(""));
|
|
|
|
}
|
|
|
|
if (format_logging(ft) && strcmp(expanded, fmt) != 0)
|
|
|
|
format_log(ft, "after time expanded: %s", expanded);
|
|
|
|
fmt = expanded;
|
|
|
|
}
|
2015-02-06 17:11:39 +00:00
|
|
|
|
2011-09-21 16:31:15 +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-09-21 16:31:15 +00:00
|
|
|
len *= 2;
|
|
|
|
}
|
|
|
|
buf[off++] = *fmt++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
fmt++;
|
|
|
|
|
2019-03-15 10:04:13 +00:00
|
|
|
ch = (u_char)*fmt++;
|
2011-09-21 16:31:15 +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);
|
|
|
|
format_log(ft, "found #(): %s", name);
|
|
|
|
|
|
|
|
if (ft->flags & FORMAT_NOJOBS) {
|
2017-01-09 21:03:25 +00:00
|
|
|
out = xstrdup("");
|
2019-03-15 10:04:13 +00:00
|
|
|
format_log(ft, "#() is disabled");
|
|
|
|
} else {
|
2018-02-20 10:43:46 +00:00
|
|
|
out = format_job_get(ft, name);
|
2019-03-15 10:04:13 +00:00
|
|
|
format_log(ft, "#() 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-09-21 16:31:15 +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-09-21 16:31:15 +00:00
|
|
|
break;
|
|
|
|
n = ptr - fmt;
|
|
|
|
|
2019-03-15 10:04:13 +00:00
|
|
|
format_log(ft, "found #{}: %.*s", (int)n, fmt);
|
2011-09-21 16:31:15 +00:00
|
|
|
if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
|
|
|
|
break;
|
|
|
|
fmt += n + 1;
|
|
|
|
continue;
|
2018-05-22 08:49:12 +00:00
|
|
|
case '}':
|
2013-11-24 11:29:09 +00:00
|
|
|
case '#':
|
2018-05-22 08:49:12 +00:00
|
|
|
case ',':
|
2019-03-15 10:04:13 +00:00
|
|
|
format_log(ft, "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-09-21 16:31:15 +00:00
|
|
|
default:
|
2013-05-31 19:46:42 +00:00
|
|
|
s = NULL;
|
|
|
|
if (ch >= 'A' && ch <= 'Z')
|
|
|
|
s = format_upper[ch - 'A'];
|
|
|
|
else if (ch >= 'a' && ch <= 'z')
|
|
|
|
s = format_lower[ch - 'a'];
|
|
|
|
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-09-21 16:31:15 +00:00
|
|
|
}
|
2013-05-31 19:46:42 +00:00
|
|
|
buf[off++] = '#';
|
|
|
|
buf[off++] = ch;
|
|
|
|
continue;
|
2011-09-21 16:31:15 +00:00
|
|
|
}
|
2013-05-31 19:46:42 +00:00
|
|
|
n = strlen(s);
|
2019-03-15 10:04:13 +00:00
|
|
|
format_log(ft, "found #%c: %s", ch, s);
|
2013-05-31 19:46:42 +00:00
|
|
|
if (format_replace(ft, s, n, &buf, &len, &off) != 0)
|
|
|
|
break;
|
2011-09-21 16:31:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf[off] = '\0';
|
|
|
|
|
2019-03-15 10:04:13 +00:00
|
|
|
format_log(ft, "result is: %s", buf);
|
2019-03-14 23:34:41 +00:00
|
|
|
ft->loop--;
|
2019-03-15 10:04:13 +00:00
|
|
|
|
2011-09-21 16:31:15 +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)
|
|
|
|
{
|
|
|
|
return (format_expand1(ft, fmt, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Expand keys in a template. */
|
|
|
|
char *
|
|
|
|
format_expand(struct format_tree *ft, const char *fmt)
|
|
|
|
{
|
|
|
|
return (format_expand1(ft, fmt, 0));
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2017-05-01 12:20:55 +00:00
|
|
|
if (item != NULL)
|
|
|
|
ft = format_create(item->client, item, FORMAT_NONE, 0);
|
|
|
|
else
|
|
|
|
ft = format_create(NULL, item, FORMAT_NONE, 0);
|
2017-03-08 13:36:12 +00:00
|
|
|
format_defaults(ft, c, s, wl, wp);
|
|
|
|
|
|
|
|
expanded = format_expand(ft, fmt);
|
|
|
|
format_free(ft);
|
|
|
|
return (expanded);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
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)
|
|
|
|
log_debug("%s: wl=%u w=@%u", __func__, wl->idx, wl->window->id);
|
|
|
|
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__);
|
|
|
|
|
2017-08-09 11:43:45 +00:00
|
|
|
format_add(ft, "session_format", "%d", s != NULL);
|
|
|
|
format_add(ft, "window_format", "%d", wl != NULL);
|
|
|
|
format_add(ft, "pane_format", "%d", wp != NULL);
|
|
|
|
|
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)
|
2018-09-26 15:49:03 +00:00
|
|
|
format_defaults_winlink(ft, wl);
|
2015-02-05 10:29:43 +00:00
|
|
|
if (wp != NULL)
|
|
|
|
format_defaults_pane(ft, wp);
|
|
|
|
}
|
|
|
|
|
2011-09-21 16:31:15 +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-09-21 16:31:15 +00:00
|
|
|
{
|
|
|
|
struct session_group *sg;
|
|
|
|
|
2014-12-02 23:19:45 +00:00
|
|
|
ft->s = s;
|
|
|
|
|
2011-09-21 16:31:15 +00:00
|
|
|
format_add(ft, "session_name", "%s", s->name);
|
|
|
|
format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
|
2013-03-13 07:28:12 +00:00
|
|
|
format_add(ft, "session_id", "$%u", s->id);
|
2011-09-21 16:31:15 +00:00
|
|
|
|
2017-02-09 15:04:53 +00:00
|
|
|
sg = session_group_contains(s);
|
2011-09-21 16:31:15 +00:00
|
|
|
format_add(ft, "session_grouped", "%d", sg != NULL);
|
2017-11-02 18:27:35 +00:00
|
|
|
if (sg != NULL) {
|
2017-02-09 15:04:53 +00:00
|
|
|
format_add(ft, "session_group", "%s", sg->name);
|
2017-11-02 18:27:35 +00:00
|
|
|
format_add(ft, "session_group_size", "%u",
|
|
|
|
session_group_count (sg));
|
2017-11-02 18:52:05 +00:00
|
|
|
format_add_cb(ft, "session_group_list",
|
|
|
|
format_cb_session_group_list);
|
2017-11-02 18:27:35 +00:00
|
|
|
}
|
2011-09-21 16:31:15 +00:00
|
|
|
|
2015-10-25 22:29:17 +00:00
|
|
|
format_add_tv(ft, "session_created", &s->creation_time);
|
|
|
|
format_add_tv(ft, "session_last_attached", &s->last_attached_time);
|
|
|
|
format_add_tv(ft, "session_activity", &s->activity_time);
|
2011-09-21 16:31:15 +00:00
|
|
|
|
2014-03-06 11:25:27 +00:00
|
|
|
format_add(ft, "session_attached", "%u", s->attached);
|
2015-03-31 17:45:10 +00:00
|
|
|
format_add(ft, "session_many_attached", "%d", s->attached > 1);
|
2015-05-12 15:29:29 +00:00
|
|
|
|
2015-08-28 17:01:42 +00:00
|
|
|
format_add_cb(ft, "session_alerts", format_cb_session_alerts);
|
2017-05-05 11:59:47 +00:00
|
|
|
format_add_cb(ft, "session_stack", format_cb_session_stack);
|
2011-09-21 16:31:15 +00:00
|
|
|
}
|
|
|
|
|
2011-10-23 15:03:50 +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 15:03:50 +00:00
|
|
|
{
|
2013-02-18 14:23:40 +00:00
|
|
|
struct session *s;
|
2015-12-12 18:32:24 +00:00
|
|
|
const char *name;
|
2017-01-11 16:09:57 +00:00
|
|
|
struct tty *tty = &c->tty;
|
|
|
|
const char *types[] = TTY_TYPES;
|
2011-10-23 15:03:50 +00:00
|
|
|
|
2014-12-02 23:19:45 +00:00
|
|
|
if (ft->s == NULL)
|
|
|
|
ft->s = c->session;
|
2018-09-26 17:41:18 +00:00
|
|
|
ft->c = c;
|
2014-12-02 23:19:45 +00:00
|
|
|
|
2017-04-05 10:49:46 +00:00
|
|
|
format_add(ft, "client_name", "%s", c->name);
|
2015-06-14 10:07:44 +00:00
|
|
|
format_add(ft, "client_pid", "%ld", (long) c->pid);
|
2017-01-11 16:09:57 +00:00
|
|
|
format_add(ft, "client_height", "%u", tty->sy);
|
|
|
|
format_add(ft, "client_width", "%u", tty->sx);
|
2017-04-05 10:49:46 +00:00
|
|
|
format_add(ft, "client_tty", "%s", c->ttyname);
|
2015-07-13 15:37:26 +00:00
|
|
|
format_add(ft, "client_control_mode", "%d",
|
|
|
|
!!(c->flags & CLIENT_CONTROL));
|
2011-10-23 15:03:50 +00:00
|
|
|
|
2017-01-11 16:09:57 +00:00
|
|
|
if (tty->term_name != NULL)
|
|
|
|
format_add(ft, "client_termname", "%s", tty->term_name);
|
|
|
|
if (tty->term_name != NULL)
|
|
|
|
format_add(ft, "client_termtype", "%s", types[tty->term_type]);
|
|
|
|
|
2015-10-25 22:29:17 +00:00
|
|
|
format_add_tv(ft, "client_created", &c->creation_time);
|
|
|
|
format_add_tv(ft, "client_activity", &c->activity_time);
|
2017-04-19 06:52:27 +00:00
|
|
|
|
|
|
|
format_add(ft, "client_written", "%zu", c->written);
|
|
|
|
format_add(ft, "client_discarded", "%zu", c->discarded);
|
2011-10-23 15:03:50 +00:00
|
|
|
|
2015-12-12 18:32:24 +00:00
|
|
|
name = server_client_get_key_table(c);
|
|
|
|
if (strcmp(c->keytable->name, name) == 0)
|
2015-04-20 15:34:56 +00:00
|
|
|
format_add(ft, "client_prefix", "%d", 0);
|
|
|
|
else
|
|
|
|
format_add(ft, "client_prefix", "%d", 1);
|
|
|
|
format_add(ft, "client_key_table", "%s", c->keytable->name);
|
2013-02-10 18:58:05 +00:00
|
|
|
|
2017-01-11 16:09:57 +00:00
|
|
|
if (tty->flags & TTY_UTF8)
|
2011-10-23 15:03:50 +00:00
|
|
|
format_add(ft, "client_utf8", "%d", 1);
|
|
|
|
else
|
|
|
|
format_add(ft, "client_utf8", "%d", 0);
|
|
|
|
|
|
|
|
if (c->flags & CLIENT_READONLY)
|
|
|
|
format_add(ft, "client_readonly", "%d", 1);
|
|
|
|
else
|
|
|
|
format_add(ft, "client_readonly", "%d", 0);
|
2013-02-18 14:23:40 +00:00
|
|
|
|
|
|
|
s = c->session;
|
|
|
|
if (s != NULL)
|
|
|
|
format_add(ft, "client_session", "%s", s->name);
|
|
|
|
s = c->last_session;
|
|
|
|
if (s != NULL && session_alive(s))
|
|
|
|
format_add(ft, "client_last_session", "%s", s->name);
|
2011-10-23 15:03:50 +00:00
|
|
|
}
|
|
|
|
|
2013-08-19 21:16:11 +00:00
|
|
|
/* Set default format keys for a window. */
|
2011-09-21 16:31:15 +00:00
|
|
|
void
|
2015-02-05 10:29:43 +00:00
|
|
|
format_defaults_window(struct format_tree *ft, struct window *w)
|
2011-09-21 16:31:15 +00:00
|
|
|
{
|
2014-12-02 23:19:45 +00:00
|
|
|
ft->w = w;
|
|
|
|
|
2015-10-25 22:29:17 +00:00
|
|
|
format_add_tv(ft, "window_activity", &w->activity_time);
|
2012-01-31 12:01:43 +00:00
|
|
|
format_add(ft, "window_id", "@%u", w->id);
|
2011-09-21 16:31:15 +00:00
|
|
|
format_add(ft, "window_name", "%s", w->name);
|
|
|
|
format_add(ft, "window_width", "%u", w->sx);
|
|
|
|
format_add(ft, "window_height", "%u", w->sy);
|
2015-08-28 17:01:42 +00:00
|
|
|
format_add_cb(ft, "window_layout", format_cb_window_layout);
|
2015-11-13 10:00:26 +00:00
|
|
|
format_add_cb(ft, "window_visible_layout",
|
|
|
|
format_cb_window_visible_layout);
|
2012-05-22 21:03:25 +00:00
|
|
|
format_add(ft, "window_panes", "%u", window_count_panes(w));
|
2015-03-31 17:45:10 +00:00
|
|
|
format_add(ft, "window_zoomed_flag", "%d",
|
2014-12-01 14:30:18 +00:00
|
|
|
!!(w->flags & WINDOW_ZOOMED));
|
2013-08-19 21:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set default format keys for a winlink. */
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2018-09-26 15:49:03 +00:00
|
|
|
format_defaults_winlink(struct format_tree *ft, struct winlink *wl)
|
2013-08-19 21:16:11 +00:00
|
|
|
{
|
2018-09-26 17:41:18 +00:00
|
|
|
struct client *c = ft->c;
|
2017-04-20 09:43:45 +00:00
|
|
|
struct session *s = wl->session;
|
2013-08-19 21:16:11 +00:00
|
|
|
struct window *w = wl->window;
|
2018-09-26 17:41:18 +00:00
|
|
|
int flag;
|
|
|
|
u_int ox, oy, sx, sy;
|
2013-08-19 21:16:11 +00:00
|
|
|
|
2014-12-02 23:19:45 +00:00
|
|
|
if (ft->w == NULL)
|
|
|
|
ft->w = wl->window;
|
2017-05-05 11:59:47 +00:00
|
|
|
ft->wl = wl;
|
2014-12-02 23:19:45 +00:00
|
|
|
|
2015-02-05 10:29:43 +00:00
|
|
|
format_defaults_window(ft, w);
|
2013-08-19 21:16:11 +00:00
|
|
|
|
2018-09-26 17:41:18 +00:00
|
|
|
if (c != NULL) {
|
|
|
|
flag = tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
|
|
|
|
format_add(ft, "window_bigger", "%d", flag);
|
|
|
|
if (flag) {
|
|
|
|
format_add(ft, "window_offset_x", "%u", ox);
|
|
|
|
format_add(ft, "window_offset_y", "%u", oy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-19 21:16:11 +00:00
|
|
|
format_add(ft, "window_index", "%d", wl->idx);
|
2017-05-05 11:59:47 +00:00
|
|
|
format_add_cb(ft, "window_stack_index", format_cb_window_stack_index);
|
2018-09-26 15:49:03 +00:00
|
|
|
format_add(ft, "window_flags", "%s", window_printable_flags(wl));
|
2013-08-19 21:16:11 +00:00
|
|
|
format_add(ft, "window_active", "%d", wl == s->curw);
|
|
|
|
|
2019-03-14 21:41:30 +00:00
|
|
|
format_add(ft, "window_start_flag", "%d",
|
|
|
|
!!(wl == RB_MIN(winlinks, &s->windows)));
|
|
|
|
format_add(ft, "window_end_flag", "%d",
|
|
|
|
!!(wl == RB_MAX(winlinks, &s->windows)));
|
|
|
|
|
2019-09-11 06:43:17 +00:00
|
|
|
if (server_check_marked() && marked_pane.wl == wl)
|
|
|
|
format_add(ft, "window_marked_flag", "1");
|
|
|
|
else
|
|
|
|
format_add(ft, "window_marked_flag", "0");
|
|
|
|
|
2015-03-31 17:45:10 +00:00
|
|
|
format_add(ft, "window_bell_flag", "%d",
|
2013-08-01 22:41:39 +00:00
|
|
|
!!(wl->flags & WINLINK_BELL));
|
2015-03-31 17:45:10 +00:00
|
|
|
format_add(ft, "window_activity_flag", "%d",
|
2013-08-01 22:41:39 +00:00
|
|
|
!!(wl->flags & WINLINK_ACTIVITY));
|
2015-03-31 17:45:10 +00:00
|
|
|
format_add(ft, "window_silence_flag", "%d",
|
2013-08-01 22:41:39 +00:00
|
|
|
!!(wl->flags & WINLINK_SILENCE));
|
2015-03-31 17:45:10 +00:00
|
|
|
format_add(ft, "window_last_flag", "%d",
|
2014-09-08 14:29:05 +00:00
|
|
|
!!(wl == TAILQ_FIRST(&s->lastw)));
|
2015-05-06 08:35:39 +00:00
|
|
|
format_add(ft, "window_linked", "%d", session_is_linked(s, wl->window));
|
2011-09-21 16:31:15 +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-09-21 16:31:15 +00:00
|
|
|
{
|
2019-03-12 11:16:49 +00:00
|
|
|
struct window *w = wp->window;
|
|
|
|
struct grid *gd = wp->base.grid;
|
|
|
|
int status = wp->status;
|
|
|
|
u_int idx;
|
|
|
|
struct window_mode_entry *wme;
|
2011-09-21 16:31:15 +00:00
|
|
|
|
2014-12-02 23:19:45 +00:00
|
|
|
if (ft->w == NULL)
|
2018-08-18 16:14:03 +00:00
|
|
|
ft->w = w;
|
2015-08-28 16:46:40 +00:00
|
|
|
ft->wp = wp;
|
2014-12-02 23:19:45 +00:00
|
|
|
|
2013-02-21 19:44:27 +00:00
|
|
|
format_add(ft, "history_size", "%u", gd->hsize);
|
|
|
|
format_add(ft, "history_limit", "%u", gd->hlimit);
|
2015-08-28 17:01:42 +00:00
|
|
|
format_add_cb(ft, "history_bytes", format_cb_history_bytes);
|
2011-09-21 16:31:15 +00:00
|
|
|
|
2011-11-25 13:31:56 +00:00
|
|
|
if (window_pane_index(wp, &idx) != 0)
|
|
|
|
fatalx("index not found");
|
2013-02-21 19:44:27 +00:00
|
|
|
format_add(ft, "pane_index", "%u", idx);
|
2011-11-25 13:31:56 +00:00
|
|
|
|
2011-09-21 16:31:15 +00:00
|
|
|
format_add(ft, "pane_width", "%u", wp->sx);
|
|
|
|
format_add(ft, "pane_height", "%u", wp->sy);
|
|
|
|
format_add(ft, "pane_title", "%s", wp->base.title);
|
|
|
|
format_add(ft, "pane_id", "%%%u", wp->id);
|
2018-08-18 16:14:03 +00:00
|
|
|
format_add(ft, "pane_active", "%d", wp == w->active);
|
2014-10-25 08:47:04 +00:00
|
|
|
format_add(ft, "pane_input_off", "%d", !!(wp->flags & PANE_INPUTOFF));
|
2017-07-07 14:39:45 +00:00
|
|
|
format_add(ft, "pane_pipe", "%d", wp->pipe_fd != -1);
|
2013-02-21 19:44:27 +00:00
|
|
|
|
2017-10-12 11:32:27 +00:00
|
|
|
if ((wp->flags & PANE_STATUSREADY) && WIFEXITED(status))
|
2014-12-09 19:23:35 +00:00
|
|
|
format_add(ft, "pane_dead_status", "%d", WEXITSTATUS(status));
|
2019-05-03 20:44:24 +00:00
|
|
|
if (~wp->flags & PANE_EMPTY)
|
|
|
|
format_add(ft, "pane_dead", "%d", wp->fd == -1);
|
|
|
|
else
|
|
|
|
format_add(ft, "pane_dead", "0");
|
2014-12-09 19:23:35 +00:00
|
|
|
|
2019-05-09 08:38:13 +00:00
|
|
|
if (server_check_marked() && marked_pane.wp == wp)
|
|
|
|
format_add(ft, "pane_marked", "1");
|
|
|
|
else
|
|
|
|
format_add(ft, "pane_marked", "0");
|
|
|
|
format_add(ft, "pane_marked_set", "%d", server_check_marked());
|
|
|
|
|
Support for windows larger than the client.
This adds 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. 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).
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-width -x or -y.
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 higher memory use if you make a window that big. The
minimum size is the size required for the current layout including
borders.
This change allows some code improvements, most notably that since
windows can now never be cropped, that code can be removed from the
layout code, and since panes can now never be outside the size of the
window, window_pane_visible can be removed.
2018-08-20 14:22:14 +00:00
|
|
|
format_add(ft, "pane_left", "%u", wp->xoff);
|
|
|
|
format_add(ft, "pane_top", "%u", wp->yoff);
|
|
|
|
format_add(ft, "pane_right", "%u", wp->xoff + wp->sx - 1);
|
|
|
|
format_add(ft, "pane_bottom", "%u", wp->yoff + wp->sy - 1);
|
|
|
|
format_add(ft, "pane_at_left", "%d", wp->xoff == 0);
|
|
|
|
format_add(ft, "pane_at_top", "%d", wp->yoff == 0);
|
|
|
|
format_add(ft, "pane_at_right", "%d", wp->xoff + wp->sx == w->sx);
|
|
|
|
format_add(ft, "pane_at_bottom", "%d", wp->yoff + wp->sy == w->sy);
|
2014-05-27 12:49:36 +00:00
|
|
|
|
2019-03-12 11:16:49 +00:00
|
|
|
wme = TAILQ_FIRST(&wp->modes);
|
|
|
|
if (wme != NULL) {
|
|
|
|
format_add(ft, "pane_mode", "%s", wme->mode->name);
|
|
|
|
if (wme->mode->formats != NULL)
|
|
|
|
wme->mode->formats(wme, ft);
|
|
|
|
}
|
|
|
|
format_add_cb(ft, "pane_in_mode", format_cb_pane_in_mode);
|
2017-05-07 22:27:57 +00:00
|
|
|
|
2013-07-05 15:27:14 +00:00
|
|
|
format_add(ft, "pane_synchronized", "%d",
|
2018-08-18 16:14:03 +00:00
|
|
|
!!options_get_number(w->options, "synchronize-panes"));
|
2017-05-12 10:45:38 +00:00
|
|
|
if (wp->searchstr != NULL)
|
|
|
|
format_add(ft, "pane_search_string", "%s", wp->searchstr);
|
2013-02-21 19:44:27 +00:00
|
|
|
|
2015-06-10 12:56:04 +00:00
|
|
|
format_add(ft, "pane_tty", "%s", wp->tty);
|
2013-02-21 19:44:27 +00:00
|
|
|
format_add(ft, "pane_pid", "%ld", (long) wp->pid);
|
2015-08-28 16:46:40 +00:00
|
|
|
format_add_cb(ft, "pane_start_command", format_cb_start_command);
|
|
|
|
format_add_cb(ft, "pane_current_command", format_cb_current_command);
|
2015-08-31 18:57:37 +00:00
|
|
|
format_add_cb(ft, "pane_current_path", format_cb_current_path);
|
2013-02-21 19:44:27 +00:00
|
|
|
|
2015-03-31 17:45:10 +00:00
|
|
|
format_add(ft, "cursor_x", "%u", wp->base.cx);
|
|
|
|
format_add(ft, "cursor_y", "%u", wp->base.cy);
|
2019-03-19 19:01:50 +00:00
|
|
|
format_add_cb(ft, "cursor_character", format_cb_cursor_character);
|
|
|
|
|
2015-03-31 17:45:10 +00:00
|
|
|
format_add(ft, "scroll_region_upper", "%u", wp->base.rupper);
|
|
|
|
format_add(ft, "scroll_region_lower", "%u", wp->base.rlower);
|
2013-02-21 19:44:27 +00:00
|
|
|
|
|
|
|
format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0);
|
2015-03-31 17:45:10 +00:00
|
|
|
format_add(ft, "alternate_saved_x", "%u", wp->saved_cx);
|
|
|
|
format_add(ft, "alternate_saved_y", "%u", wp->saved_cy);
|
2013-02-21 19:44:27 +00:00
|
|
|
|
|
|
|
format_add(ft, "cursor_flag", "%d",
|
|
|
|
!!(wp->base.mode & MODE_CURSOR));
|
|
|
|
format_add(ft, "insert_flag", "%d",
|
|
|
|
!!(wp->base.mode & MODE_INSERT));
|
|
|
|
format_add(ft, "keypad_cursor_flag", "%d",
|
|
|
|
!!(wp->base.mode & MODE_KCURSOR));
|
|
|
|
format_add(ft, "keypad_flag", "%d",
|
|
|
|
!!(wp->base.mode & MODE_KKEYPAD));
|
|
|
|
format_add(ft, "wrap_flag", "%d",
|
|
|
|
!!(wp->base.mode & MODE_WRAP));
|
2019-07-09 14:03:12 +00:00
|
|
|
format_add(ft, "origin_flag", "%d",
|
|
|
|
!!(wp->base.mode & MODE_ORIGIN));
|
2013-02-21 19:44:27 +00:00
|
|
|
|
2015-04-21 15:18:38 +00:00
|
|
|
format_add(ft, "mouse_any_flag", "%d",
|
2017-02-01 09:55:07 +00:00
|
|
|
!!(wp->base.mode & ALL_MOUSE_MODES));
|
2013-02-21 19:44:27 +00:00
|
|
|
format_add(ft, "mouse_standard_flag", "%d",
|
|
|
|
!!(wp->base.mode & MODE_MOUSE_STANDARD));
|
|
|
|
format_add(ft, "mouse_button_flag", "%d",
|
|
|
|
!!(wp->base.mode & MODE_MOUSE_BUTTON));
|
2017-02-01 09:55:07 +00:00
|
|
|
format_add(ft, "mouse_all_flag", "%d",
|
|
|
|
!!(wp->base.mode & MODE_MOUSE_ALL));
|
2019-07-09 14:03:12 +00:00
|
|
|
format_add(ft, "mouse_utf8_flag", "%d",
|
|
|
|
!!(wp->base.mode & MODE_MOUSE_UTF8));
|
|
|
|
format_add(ft, "mouse_sgr_flag", "%d",
|
|
|
|
!!(wp->base.mode & MODE_MOUSE_SGR));
|
2013-02-23 13:02:52 +00:00
|
|
|
|
2015-08-28 17:01:42 +00:00
|
|
|
format_add_cb(ft, "pane_tabs", format_cb_pane_tabs);
|
2011-09-21 16:31:15 +00:00
|
|
|
}
|
2012-05-22 21:03:25 +00:00
|
|
|
|
2013-02-23 13:02:52 +00:00
|
|
|
/* Set default format keys for paste buffer. */
|
2012-05-22 21:03:25 +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 21:03:25 +00:00
|
|
|
{
|
2017-08-09 11:43:45 +00:00
|
|
|
struct timeval tv;
|
|
|
|
size_t size;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
timerclear(&tv);
|
|
|
|
tv.tv_sec = paste_buffer_created(pb);
|
|
|
|
paste_buffer_data(pb, &size);
|
2012-05-22 21:03:25 +00:00
|
|
|
|
2017-08-09 11:43:45 +00:00
|
|
|
format_add(ft, "buffer_size", "%zu", size);
|
2015-08-29 09:25:00 +00:00
|
|
|
format_add(ft, "buffer_name", "%s", paste_buffer_name(pb));
|
2017-08-09 11:43:45 +00:00
|
|
|
format_add_tv(ft, "buffer_created", &tv);
|
2012-05-22 21:03:25 +00:00
|
|
|
|
2015-11-12 11:09:11 +00:00
|
|
|
s = paste_make_sample(pb);
|
2014-04-02 18:12:18 +00:00
|
|
|
format_add(ft, "buffer_sample", "%s", s);
|
|
|
|
free(s);
|
2012-05-22 21:03:25 +00:00
|
|
|
}
|