2011-08-26 12:53:16 +02:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2011 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* 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 20:23:35 +01:00
|
|
|
#include <sys/wait.h>
|
2011-08-26 12:53:16 +02:00
|
|
|
|
2013-10-10 13:50:20 +02:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
2015-10-25 23:29:17 +01:00
|
|
|
#include <libgen.h>
|
2011-08-26 12:53:16 +02:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <stdarg.h>
|
2012-07-10 13:53:01 +02:00
|
|
|
#include <stdlib.h>
|
2011-08-26 12:53:16 +02: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 18:46:40 +02:00
|
|
|
struct format_entry;
|
|
|
|
typedef void (*format_cb)(struct format_tree *, struct format_entry *);
|
|
|
|
|
2015-05-27 15:28:04 +02:00
|
|
|
void format_job_callback(struct job *);
|
2015-10-25 09:59:26 +01:00
|
|
|
char *format_job_get(struct format_tree *, const char *);
|
2015-08-28 13:38:27 +02:00
|
|
|
void format_job_timer(int, short, void *);
|
2015-05-27 15:28:04 +02:00
|
|
|
|
2015-08-28 18:46:40 +02:00
|
|
|
void format_cb_host(struct format_tree *, struct format_entry *);
|
|
|
|
void format_cb_host_short(struct format_tree *, struct format_entry *);
|
|
|
|
void format_cb_pid(struct format_tree *, struct format_entry *);
|
2015-08-28 19:01:42 +02:00
|
|
|
void format_cb_session_alerts(struct format_tree *, struct format_entry *);
|
|
|
|
void format_cb_window_layout(struct format_tree *, struct format_entry *);
|
2015-11-13 13:18:52 +01:00
|
|
|
void format_cb_window_visible_layout(struct format_tree *,
|
|
|
|
struct format_entry *);
|
2015-08-28 18:46:40 +02:00
|
|
|
void format_cb_start_command(struct format_tree *, struct format_entry *);
|
|
|
|
void format_cb_current_command(struct format_tree *, struct format_entry *);
|
2015-08-28 19:01:42 +02:00
|
|
|
void format_cb_history_bytes(struct format_tree *, struct format_entry *);
|
|
|
|
void format_cb_pane_tabs(struct format_tree *, struct format_entry *);
|
2015-08-28 18:46:40 +02:00
|
|
|
|
2015-10-25 23:29:17 +01:00
|
|
|
char *format_find(struct format_tree *, const char *, int);
|
2015-08-28 18:46:40 +02:00
|
|
|
void format_add_cb(struct format_tree *, const char *, format_cb);
|
2015-10-25 23:29:17 +01:00
|
|
|
void format_add_tv(struct format_tree *, const char *, struct timeval *);
|
2013-10-10 14:04:01 +02:00
|
|
|
int format_replace(struct format_tree *, const char *, size_t, char **,
|
|
|
|
size_t *, size_t *);
|
2015-03-31 19:58:36 +02:00
|
|
|
char *format_time_string(time_t);
|
2015-02-05 11:29:43 +01:00
|
|
|
|
|
|
|
void format_defaults_pane_tabs(struct format_tree *, struct window_pane *);
|
|
|
|
void format_defaults_session(struct format_tree *, struct session *);
|
|
|
|
void format_defaults_client(struct format_tree *, struct client *);
|
|
|
|
void format_defaults_winlink(struct format_tree *, struct session *,
|
|
|
|
struct winlink *);
|
2011-08-26 12:53:16 +02:00
|
|
|
|
2015-05-27 15:28:04 +02:00
|
|
|
/* Entry in format job tree. */
|
|
|
|
struct format_job {
|
|
|
|
const char *cmd;
|
|
|
|
|
|
|
|
time_t last;
|
|
|
|
char *out;
|
|
|
|
|
|
|
|
struct job *job;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
RB_ENTRY(format_job) entry;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Format job tree. */
|
2015-08-28 13:38:27 +02:00
|
|
|
struct event format_job_event;
|
2015-05-27 15:28:04 +02:00
|
|
|
int format_job_cmp(struct format_job *, struct format_job *);
|
|
|
|
RB_HEAD(format_job_tree, format_job) format_jobs = RB_INITIALIZER();
|
|
|
|
RB_PROTOTYPE(format_job_tree, format_job, entry, format_job_cmp);
|
|
|
|
RB_GENERATE(format_job_tree, format_job, entry, format_job_cmp);
|
|
|
|
|
|
|
|
/* Format job tree comparison function. */
|
|
|
|
int
|
|
|
|
format_job_cmp(struct format_job *fj1, struct format_job *fj2)
|
|
|
|
{
|
|
|
|
return (strcmp(fj1->cmd, fj2->cmd));
|
|
|
|
}
|
|
|
|
|
2015-10-25 23:29:17 +01:00
|
|
|
/* Format modifiers. */
|
|
|
|
#define FORMAT_TIMESTRING 0x1
|
|
|
|
#define FORMAT_BASENAME 0x2
|
|
|
|
#define FORMAT_DIRNAME 0x4
|
|
|
|
|
2014-12-03 00:19:45 +01:00
|
|
|
/* Entry in format tree. */
|
|
|
|
struct format_entry {
|
2015-08-28 18:46:40 +02:00
|
|
|
char *key;
|
|
|
|
char *value;
|
2015-10-25 23:29:17 +01:00
|
|
|
time_t t;
|
2015-08-28 18:46:40 +02:00
|
|
|
format_cb cb;
|
|
|
|
RB_ENTRY(format_entry) entry;
|
2014-12-03 00:19:45 +01:00
|
|
|
};
|
|
|
|
|
2015-05-27 15:28:04 +02:00
|
|
|
/* Format entry tree. */
|
2014-12-03 00:19:45 +01:00
|
|
|
struct format_tree {
|
2015-08-28 18:46:40 +02:00
|
|
|
struct window *w;
|
|
|
|
struct session *s;
|
|
|
|
struct window_pane *wp;
|
2014-12-03 00:19:45 +01:00
|
|
|
|
2015-09-14 12:25:52 +02:00
|
|
|
int flags;
|
2014-12-03 00:19:45 +01:00
|
|
|
|
2015-05-27 15:28:04 +02:00
|
|
|
RB_HEAD(format_entry_tree, format_entry) tree;
|
|
|
|
};
|
|
|
|
int format_entry_cmp(struct format_entry *, struct format_entry *);
|
|
|
|
RB_PROTOTYPE(format_entry_tree, format_entry, entry, format_entry_cmp);
|
|
|
|
RB_GENERATE(format_entry_tree, format_entry, entry, format_entry_cmp);
|
2011-08-26 12:53:16 +02:00
|
|
|
|
2015-05-27 15:28:04 +02:00
|
|
|
/* Format entry tree comparison function. */
|
2011-08-26 12:53:16 +02:00
|
|
|
int
|
2015-05-27 15:28:04 +02:00
|
|
|
format_entry_cmp(struct format_entry *fe1, struct format_entry *fe2)
|
2011-08-26 12:53:16 +02:00
|
|
|
{
|
|
|
|
return (strcmp(fe1->key, fe2->key));
|
|
|
|
}
|
|
|
|
|
2013-05-31 21:46:42 +02:00
|
|
|
/* Single-character uppercase aliases. */
|
|
|
|
const char *format_upper[] = {
|
2011-08-26 12:53:16 +02: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 21:46:42 +02:00
|
|
|
/* Single-character lowercase aliases. */
|
|
|
|
const char *format_lower[] = {
|
|
|
|
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 */
|
|
|
|
};
|
|
|
|
|
2015-05-27 15:28:04 +02:00
|
|
|
/* Format job callback. */
|
|
|
|
void
|
|
|
|
format_job_callback(struct job *job)
|
|
|
|
{
|
|
|
|
struct format_job *fj = job->data;
|
|
|
|
char *line, *buf;
|
|
|
|
size_t len;
|
|
|
|
struct client *c;
|
|
|
|
|
|
|
|
fj->job = NULL;
|
|
|
|
free(fj->out);
|
|
|
|
|
|
|
|
buf = NULL;
|
|
|
|
if ((line = evbuffer_readline(job->event->input)) == NULL) {
|
|
|
|
len = EVBUFFER_LENGTH(job->event->input);
|
|
|
|
buf = xmalloc(len + 1);
|
|
|
|
if (len != 0)
|
|
|
|
memcpy(buf, EVBUFFER_DATA(job->event->input), len);
|
|
|
|
buf[len] = '\0';
|
|
|
|
} else
|
|
|
|
buf = line;
|
|
|
|
fj->out = buf;
|
|
|
|
|
|
|
|
if (fj->status) {
|
|
|
|
TAILQ_FOREACH(c, &clients, entry)
|
|
|
|
server_status_client(c);
|
|
|
|
fj->status = 0;
|
|
|
|
}
|
2015-08-28 13:38:27 +02:00
|
|
|
|
|
|
|
log_debug("%s: %s: %s", __func__, fj->cmd, fj->out);
|
2015-05-27 15:28:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Find a job. */
|
2015-10-25 09:59:26 +01:00
|
|
|
char *
|
2015-05-27 15:28:04 +02:00
|
|
|
format_job_get(struct format_tree *ft, const char *cmd)
|
|
|
|
{
|
|
|
|
struct format_job fj0, *fj;
|
2015-09-14 12:25:52 +02:00
|
|
|
time_t t;
|
2015-05-27 15:28:04 +02:00
|
|
|
|
|
|
|
fj0.cmd = cmd;
|
2015-09-03 16:30:23 +02:00
|
|
|
if ((fj = RB_FIND(format_job_tree, &format_jobs, &fj0)) == NULL) {
|
2015-05-27 15:28:04 +02:00
|
|
|
fj = xcalloc(1, sizeof *fj);
|
|
|
|
fj->cmd = xstrdup(cmd);
|
|
|
|
|
|
|
|
xasprintf(&fj->out, "<'%s' not ready>", fj->cmd);
|
|
|
|
|
|
|
|
RB_INSERT(format_job_tree, &format_jobs, fj);
|
|
|
|
}
|
|
|
|
|
2015-09-14 12:25:52 +02:00
|
|
|
t = time(NULL);
|
|
|
|
if (fj->job == NULL && ((ft->flags & FORMAT_FORCE) || fj->last != t)) {
|
2015-10-31 09:13:58 +01:00
|
|
|
fj->job = job_run(fj->cmd, NULL, NULL, format_job_callback,
|
2015-05-27 15:28:04 +02:00
|
|
|
NULL, fj);
|
|
|
|
if (fj->job == NULL) {
|
|
|
|
free(fj->out);
|
|
|
|
xasprintf(&fj->out, "<'%s' didn't start>", fj->cmd);
|
|
|
|
}
|
2015-09-14 12:25:52 +02:00
|
|
|
fj->last = t;
|
2015-05-27 15:28:04 +02:00
|
|
|
}
|
2015-09-14 12:25:52 +02:00
|
|
|
|
|
|
|
if (ft->flags & FORMAT_STATUS)
|
|
|
|
fj->status = 1;
|
2015-05-27 15:28:04 +02:00
|
|
|
|
2015-10-25 09:59:26 +01:00
|
|
|
return (format_expand(ft, fj->out));
|
2015-05-27 15:28:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove old jobs. */
|
|
|
|
void
|
2015-08-28 13:38:27 +02:00
|
|
|
format_job_timer(unused int fd, unused short events, unused void *arg)
|
2015-05-27 15:28:04 +02:00
|
|
|
{
|
|
|
|
struct format_job *fj, *fj1;
|
|
|
|
time_t now;
|
2015-08-28 13:38:27 +02:00
|
|
|
struct timeval tv = { .tv_sec = 60 };
|
2015-05-27 15:28:04 +02:00
|
|
|
|
|
|
|
now = time(NULL);
|
|
|
|
RB_FOREACH_SAFE(fj, format_job_tree, &format_jobs, fj1) {
|
|
|
|
if (fj->last > now || now - fj->last < 3600)
|
|
|
|
continue;
|
|
|
|
RB_REMOVE(format_job_tree, &format_jobs, fj);
|
|
|
|
|
2015-08-28 13:38:27 +02:00
|
|
|
log_debug("%s: %s", __func__, fj->cmd);
|
|
|
|
|
2015-05-27 15:28:04 +02:00
|
|
|
if (fj->job != NULL)
|
|
|
|
job_free(fj->job);
|
|
|
|
|
2015-08-28 14:16:28 +02:00
|
|
|
free((void *)fj->cmd);
|
2015-05-27 15:28:04 +02:00
|
|
|
free(fj->out);
|
|
|
|
|
|
|
|
free(fj);
|
|
|
|
}
|
2015-08-28 13:38:27 +02:00
|
|
|
|
|
|
|
evtimer_del(&format_job_event);
|
|
|
|
evtimer_add(&format_job_event, &tv);
|
2015-05-27 15:28:04 +02:00
|
|
|
}
|
|
|
|
|
2015-08-28 18:46:40 +02:00
|
|
|
/* Callback for host. */
|
|
|
|
void
|
|
|
|
format_cb_host(unused struct format_tree *ft, struct format_entry *fe)
|
|
|
|
{
|
|
|
|
char host[HOST_NAME_MAX + 1];
|
|
|
|
|
|
|
|
if (gethostname(host, sizeof host) != 0)
|
|
|
|
fe->value = xstrdup("");
|
|
|
|
else
|
|
|
|
fe->value = xstrdup(host);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for host_short. */
|
|
|
|
void
|
|
|
|
format_cb_host_short(unused struct format_tree *ft, struct format_entry *fe)
|
|
|
|
{
|
|
|
|
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. */
|
|
|
|
void
|
|
|
|
format_cb_pid(unused struct format_tree *ft, struct format_entry *fe)
|
|
|
|
{
|
|
|
|
xasprintf(&fe->value, "%ld", (long)getpid());
|
|
|
|
}
|
|
|
|
|
2015-08-28 19:01:42 +02:00
|
|
|
/* Callback for session_alerts. */
|
|
|
|
void
|
|
|
|
format_cb_session_alerts(struct format_tree *ft, struct format_entry *fe)
|
|
|
|
{
|
|
|
|
struct session *s = ft->s;
|
|
|
|
struct winlink *wl;
|
|
|
|
char alerts[256], tmp[16];
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for window_layout. */
|
|
|
|
void
|
|
|
|
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 11:00:26 +01:00
|
|
|
/* Callback for window_visible_layout. */
|
|
|
|
void
|
|
|
|
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 18:46:40 +02:00
|
|
|
/* Callback for pane_start_command. */
|
|
|
|
void
|
|
|
|
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. */
|
|
|
|
void
|
|
|
|
format_cb_current_command(struct format_tree *ft, struct format_entry *fe)
|
|
|
|
{
|
|
|
|
struct window_pane *wp = ft->wp;
|
|
|
|
char *cmd;
|
|
|
|
|
|
|
|
if (wp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
cmd = get_proc_name(wp->fd, wp->tty);
|
|
|
|
if (cmd == NULL || *cmd == '\0') {
|
|
|
|
free(cmd);
|
|
|
|
cmd = cmd_stringify_argv(wp->argc, wp->argv);
|
|
|
|
if (cmd == NULL || *cmd == '\0') {
|
|
|
|
free(cmd);
|
|
|
|
cmd = xstrdup(wp->shell);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fe->value = parse_window_name(cmd);
|
|
|
|
free(cmd);
|
|
|
|
}
|
|
|
|
|
2015-08-28 19:01:42 +02:00
|
|
|
/* Callback for history_bytes. */
|
|
|
|
void
|
|
|
|
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++) {
|
|
|
|
gl = &gd->linedata[i];
|
|
|
|
size += gl->cellsize * sizeof *gl->celldata;
|
2015-11-13 09:09:28 +01:00
|
|
|
size += gl->extdsize * sizeof *gl->extddata;
|
2015-08-28 19:01:42 +02:00
|
|
|
}
|
|
|
|
size += gd->hsize * sizeof *gd->linedata;
|
|
|
|
|
|
|
|
xasprintf(&fe->value, "%llu", size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for pane_tabs. */
|
|
|
|
void
|
|
|
|
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();
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
size = EVBUFFER_LENGTH(buffer);
|
|
|
|
xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer));
|
|
|
|
evbuffer_free(buffer);
|
|
|
|
}
|
|
|
|
|
2011-08-26 12:53:16 +02:00
|
|
|
/* Create a new tree. */
|
|
|
|
struct format_tree *
|
|
|
|
format_create(void)
|
2015-05-27 15:28:04 +02:00
|
|
|
{
|
2015-09-14 12:25:52 +02:00
|
|
|
return (format_create_flags(0));
|
2015-05-27 15:28:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a new tree for the status line. */
|
|
|
|
struct format_tree *
|
2015-09-14 12:25:52 +02:00
|
|
|
format_create_flags(int flags)
|
2011-08-26 12:53:16 +02:00
|
|
|
{
|
|
|
|
struct format_tree *ft;
|
|
|
|
|
2015-08-28 13:38:27 +02:00
|
|
|
if (!event_initialized(&format_job_event)) {
|
|
|
|
evtimer_set(&format_job_event, format_job_timer, NULL);
|
|
|
|
format_job_timer(-1, 0, NULL);
|
|
|
|
}
|
|
|
|
|
2014-12-03 00:19:45 +01:00
|
|
|
ft = xcalloc(1, sizeof *ft);
|
|
|
|
RB_INIT(&ft->tree);
|
2015-09-14 12:25:52 +02:00
|
|
|
ft->flags = flags;
|
2011-08-26 12:53:16 +02:00
|
|
|
|
2015-08-28 18:46:40 +02: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);
|
2011-08-26 12:53:16 +02:00
|
|
|
|
|
|
|
return (ft);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free a tree. */
|
|
|
|
void
|
|
|
|
format_free(struct format_tree *ft)
|
|
|
|
{
|
2014-12-03 00:19:45 +01:00
|
|
|
struct format_entry *fe, *fe1;
|
2011-08-26 12:53:16 +02:00
|
|
|
|
2015-05-27 15:28:04 +02:00
|
|
|
RB_FOREACH_SAFE(fe, format_entry_tree, &ft->tree, fe1) {
|
|
|
|
RB_REMOVE(format_entry_tree, &ft->tree, fe);
|
2012-07-10 13:53:01 +02:00
|
|
|
free(fe->value);
|
|
|
|
free(fe->key);
|
|
|
|
free(fe);
|
2011-08-26 12:53:16 +02:00
|
|
|
}
|
|
|
|
|
2013-05-31 21:46:42 +02:00
|
|
|
free(ft);
|
2011-08-26 12:53:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add a key-value pair. */
|
|
|
|
void
|
|
|
|
format_add(struct format_tree *ft, const char *key, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
struct format_entry *fe;
|
2013-10-10 13:47:52 +02:00
|
|
|
struct format_entry *fe_now;
|
2011-08-26 12:53:16 +02:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
fe = xmalloc(sizeof *fe);
|
|
|
|
fe->key = xstrdup(key);
|
|
|
|
|
2015-08-28 18:46:40 +02: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 23:29:17 +01:00
|
|
|
fe->t = 0;
|
2015-08-28 18:46:40 +02:00
|
|
|
|
2011-08-26 12:53:16 +02:00
|
|
|
va_start(ap, fmt);
|
|
|
|
xvasprintf(&fe->value, fmt, ap);
|
|
|
|
va_end(ap);
|
2015-08-28 18:46:40 +02:00
|
|
|
}
|
|
|
|
|
2015-10-25 23:29:17 +01:00
|
|
|
/* Add a key and time. */
|
|
|
|
void
|
|
|
|
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 18:46:40 +02:00
|
|
|
/* Add a key and function. */
|
|
|
|
void
|
|
|
|
format_add_cb(struct format_tree *ft, const char *key, format_cb cb)
|
|
|
|
{
|
|
|
|
struct format_entry *fe;
|
|
|
|
struct format_entry *fe_now;
|
|
|
|
|
|
|
|
fe = xmalloc(sizeof *fe);
|
|
|
|
fe->key = xstrdup(key);
|
2011-08-26 12:53:16 +02:00
|
|
|
|
2015-05-27 15:28:04 +02:00
|
|
|
fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe);
|
2013-10-10 13:47:52 +02:00
|
|
|
if (fe_now != NULL) {
|
|
|
|
free(fe->key);
|
|
|
|
free(fe);
|
2015-08-28 18:46:40 +02:00
|
|
|
free(fe_now->value);
|
|
|
|
fe = fe_now;
|
2013-10-10 13:47:52 +02:00
|
|
|
}
|
2015-08-28 18:46:40 +02:00
|
|
|
|
|
|
|
fe->cb = cb;
|
2015-10-25 23:29:17 +01:00
|
|
|
fe->t = 0;
|
2015-08-28 18:46:40 +02:00
|
|
|
|
|
|
|
fe->value = NULL;
|
2011-08-26 12:53:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Find a format entry. */
|
2015-10-25 23:29:17 +01:00
|
|
|
char *
|
|
|
|
format_find(struct format_tree *ft, const char *key, int modifiers)
|
2011-08-26 12:53:16 +02:00
|
|
|
{
|
|
|
|
struct format_entry *fe, fe_find;
|
2014-12-03 00:19:45 +01:00
|
|
|
struct options_entry *o;
|
2015-08-28 12:06:52 +02:00
|
|
|
struct environ_entry *envent;
|
2015-10-25 23:29:17 +01:00
|
|
|
static char s[64];
|
|
|
|
const char *found;
|
|
|
|
char *copy, *saved;
|
|
|
|
|
|
|
|
found = NULL;
|
|
|
|
|
|
|
|
if (~modifiers & FORMAT_TIMESTRING) {
|
2015-10-27 16:58:42 +01:00
|
|
|
o = options_find(global_options, key);
|
2015-10-25 23:29:17 +01:00
|
|
|
if (o == NULL && ft->w != NULL)
|
2015-10-27 16:58:42 +01:00
|
|
|
o = options_find(ft->w->options, key);
|
2015-10-25 23:29:17 +01:00
|
|
|
if (o == NULL)
|
2015-10-27 16:58:42 +01:00
|
|
|
o = options_find(global_w_options, key);
|
2015-10-25 23:29:17 +01:00
|
|
|
if (o == NULL && ft->s != NULL)
|
2015-10-27 16:58:42 +01:00
|
|
|
o = options_find(ft->s->options, key);
|
2015-10-25 23:29:17 +01:00
|
|
|
if (o == NULL)
|
2015-10-27 16:58:42 +01:00
|
|
|
o = options_find(global_s_options, key);
|
2015-10-25 23:29:17 +01:00
|
|
|
if (o != NULL) {
|
|
|
|
switch (o->type) {
|
|
|
|
case OPTIONS_STRING:
|
|
|
|
found = o->str;
|
|
|
|
goto found;
|
|
|
|
case OPTIONS_NUMBER:
|
|
|
|
xsnprintf(s, sizeof s, "%lld", o->num);
|
|
|
|
found = s;
|
|
|
|
goto found;
|
|
|
|
case OPTIONS_STYLE:
|
|
|
|
found = style_tostring(&o->style);
|
|
|
|
goto found;
|
|
|
|
}
|
2014-12-03 00:19:45 +01:00
|
|
|
}
|
|
|
|
}
|
2011-08-26 12:53:16 +02:00
|
|
|
|
|
|
|
fe_find.key = (char *) key;
|
2015-05-27 15:28:04 +02:00
|
|
|
fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find);
|
2015-08-28 18:46:40 +02:00
|
|
|
if (fe != NULL) {
|
2015-10-25 23:29:17 +01:00
|
|
|
if (modifiers & FORMAT_TIMESTRING) {
|
|
|
|
if (fe->t == 0)
|
|
|
|
return (NULL);
|
|
|
|
ctime_r(&fe->t, s);
|
|
|
|
s[strcspn(s, "\n")] = '\0';
|
|
|
|
found = s;
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
if (fe->t != 0) {
|
|
|
|
xsnprintf(s, sizeof s, "%lld", (long long)fe->t);
|
|
|
|
found = s;
|
|
|
|
goto found;
|
|
|
|
}
|
2015-08-28 18:46:40 +02:00
|
|
|
if (fe->value == NULL && fe->cb != NULL)
|
|
|
|
fe->cb(ft, fe);
|
2015-10-25 23:29:17 +01:00
|
|
|
found = fe->value;
|
|
|
|
goto found;
|
2015-08-28 18:46:40 +02:00
|
|
|
}
|
2015-08-28 12:06:52 +02:00
|
|
|
|
2015-10-25 23:29:17 +01:00
|
|
|
if (~modifiers & FORMAT_TIMESTRING) {
|
|
|
|
envent = NULL;
|
|
|
|
if (ft->s != NULL)
|
2015-10-28 10:51:55 +01:00
|
|
|
envent = environ_find(ft->s->environ, key);
|
2015-10-25 23:29:17 +01:00
|
|
|
if (envent == NULL)
|
2015-10-28 10:51:55 +01:00
|
|
|
envent = environ_find(global_environ, key);
|
2015-10-25 23:29:17 +01:00
|
|
|
if (envent != NULL) {
|
|
|
|
found = envent->value;
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
2015-08-28 12:06:52 +02:00
|
|
|
|
|
|
|
return (NULL);
|
2015-10-25 23:29:17 +01:00
|
|
|
|
|
|
|
found:
|
2015-10-27 10:18:06 +01:00
|
|
|
if (found == NULL)
|
|
|
|
return (NULL);
|
2015-10-25 23:29:17 +01:00
|
|
|
copy = xstrdup(found);
|
|
|
|
if (modifiers & FORMAT_BASENAME) {
|
|
|
|
saved = copy;
|
|
|
|
copy = xstrdup(basename(saved));
|
|
|
|
free(saved);
|
|
|
|
}
|
|
|
|
if (modifiers & FORMAT_DIRNAME) {
|
|
|
|
saved = copy;
|
|
|
|
copy = xstrdup(dirname(saved));
|
|
|
|
free(saved);
|
|
|
|
}
|
|
|
|
return (copy);
|
2011-08-26 12:53:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Replace a key/value pair in buffer. #{blah} is expanded directly,
|
|
|
|
* #{?blah,a,b} is replace with a if blah exists and is nonzero else b.
|
|
|
|
*/
|
|
|
|
int
|
2013-10-10 13:50:20 +02:00
|
|
|
format_replace(struct format_tree *ft, const char *key, size_t keylen,
|
|
|
|
char **buf, size_t *len, size_t *off)
|
2011-08-26 12:53:16 +02:00
|
|
|
{
|
2015-10-25 23:29:17 +01:00
|
|
|
char *copy, *copy0, *endptr, *ptr, *saved, *trimmed, *value;
|
2011-08-26 12:53:16 +02:00
|
|
|
size_t valuelen;
|
2014-04-17 17:37:55 +02:00
|
|
|
u_long limit = 0;
|
2015-10-27 10:28:31 +01:00
|
|
|
int modifiers = 0, brackets;
|
2011-08-26 12:53:16 +02:00
|
|
|
|
|
|
|
/* Make a copy of the key. */
|
2013-10-10 13:50:20 +02:00
|
|
|
copy0 = copy = xmalloc(keylen + 1);
|
2011-08-26 12:53:16 +02:00
|
|
|
memcpy(copy, key, keylen);
|
|
|
|
copy[keylen] = '\0';
|
|
|
|
|
2013-10-10 13:50:20 +02:00
|
|
|
/* Is there a length limit or whatnot? */
|
2015-10-25 23:29:17 +01:00
|
|
|
switch (copy[0]) {
|
|
|
|
case '=':
|
|
|
|
errno = 0;
|
|
|
|
limit = strtoul(copy + 1, &endptr, 10);
|
|
|
|
if (errno == ERANGE && limit == ULONG_MAX)
|
|
|
|
break;
|
|
|
|
if (*endptr != ':')
|
|
|
|
break;
|
|
|
|
copy = endptr + 1;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
if (copy[1] != ':')
|
|
|
|
break;
|
|
|
|
modifiers |= FORMAT_BASENAME;
|
|
|
|
copy += 2;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
if (copy[1] != ':')
|
|
|
|
break;
|
|
|
|
modifiers |= FORMAT_DIRNAME;
|
|
|
|
copy += 2;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
if (copy[1] != ':')
|
|
|
|
break;
|
|
|
|
modifiers |= FORMAT_TIMESTRING;
|
|
|
|
copy += 2;
|
|
|
|
break;
|
2013-10-10 13:50:20 +02:00
|
|
|
}
|
|
|
|
|
2011-08-26 12:53:16 +02:00
|
|
|
/*
|
|
|
|
* Is this a conditional? If so, check it exists and extract either the
|
|
|
|
* first or second element. If not, look up the key directly.
|
|
|
|
*/
|
|
|
|
if (*copy == '?') {
|
|
|
|
ptr = strchr(copy, ',');
|
|
|
|
if (ptr == NULL)
|
|
|
|
goto fail;
|
|
|
|
*ptr = '\0';
|
|
|
|
|
2015-10-27 10:28:31 +01:00
|
|
|
value = ptr + 1;
|
|
|
|
saved = format_find(ft, copy + 1, modifiers);
|
|
|
|
|
|
|
|
brackets = 0;
|
|
|
|
for (ptr = ptr + 1; *ptr != '\0'; ptr++) {
|
|
|
|
if (*ptr == '{')
|
|
|
|
brackets++;
|
|
|
|
if (*ptr == '}')
|
|
|
|
brackets--;
|
|
|
|
if (*ptr == ',' && brackets == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*ptr == '\0')
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (saved != NULL && *saved != '\0' &&
|
|
|
|
(saved[0] != '0' || saved[1] != '\0')) {
|
2011-08-26 12:53:16 +02:00
|
|
|
*ptr = '\0';
|
2015-10-27 10:28:31 +01:00
|
|
|
} else
|
2011-08-26 12:53:16 +02:00
|
|
|
value = ptr + 1;
|
2015-10-25 23:29:17 +01:00
|
|
|
value = format_expand(ft, value);
|
|
|
|
free(saved);
|
|
|
|
saved = value;
|
2011-08-26 12:53:16 +02:00
|
|
|
} else {
|
2015-10-25 23:29:17 +01:00
|
|
|
saved = value = format_find(ft, copy, modifiers);
|
2011-08-26 12:53:16 +02:00
|
|
|
if (value == NULL)
|
2015-10-25 23:29:17 +01:00
|
|
|
saved = value = xstrdup("");
|
2011-08-26 12:53:16 +02:00
|
|
|
}
|
|
|
|
|
2013-10-10 13:50:20 +02:00
|
|
|
/* Truncate the value if needed. */
|
2014-04-17 17:37:55 +02:00
|
|
|
if (limit != 0) {
|
|
|
|
value = trimmed = utf8_trimcstr(value, limit);
|
|
|
|
free(saved);
|
|
|
|
saved = trimmed;
|
|
|
|
}
|
|
|
|
valuelen = strlen(value);
|
2013-10-10 13:50:20 +02:00
|
|
|
|
2011-08-26 12:53:16 +02:00
|
|
|
/* Expand the buffer and copy in the value. */
|
|
|
|
while (*len - *off < valuelen + 1) {
|
2014-10-08 19:35:58 +02:00
|
|
|
*buf = xreallocarray(*buf, 2, *len);
|
2011-08-26 12:53:16 +02:00
|
|
|
*len *= 2;
|
|
|
|
}
|
|
|
|
memcpy(*buf + *off, value, valuelen);
|
|
|
|
*off += valuelen;
|
|
|
|
|
2013-10-10 13:50:36 +02:00
|
|
|
free(saved);
|
2013-10-10 13:50:20 +02:00
|
|
|
free(copy0);
|
2011-08-26 12:53:16 +02:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
fail:
|
2013-10-10 13:50:20 +02:00
|
|
|
free(copy0);
|
2011-08-26 12:53:16 +02:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2015-02-06 18:11:39 +01:00
|
|
|
/* Expand keys in a template, passing through strftime first. */
|
|
|
|
char *
|
|
|
|
format_expand_time(struct format_tree *ft, const char *fmt, time_t t)
|
|
|
|
{
|
|
|
|
char *tmp, *expanded;
|
|
|
|
size_t tmplen;
|
|
|
|
struct tm *tm;
|
|
|
|
|
2015-05-20 08:39:02 +02:00
|
|
|
if (fmt == NULL || *fmt == '\0')
|
2015-02-06 18:11:39 +01:00
|
|
|
return (xstrdup(""));
|
|
|
|
|
|
|
|
tm = localtime(&t);
|
|
|
|
|
|
|
|
tmp = NULL;
|
|
|
|
tmplen = strlen(fmt);
|
|
|
|
|
|
|
|
do {
|
|
|
|
tmp = xreallocarray(tmp, 2, tmplen);
|
|
|
|
tmplen *= 2;
|
|
|
|
} while (strftime(tmp, tmplen, fmt, tm) == 0);
|
|
|
|
|
|
|
|
expanded = format_expand(ft, tmp);
|
|
|
|
free(tmp);
|
|
|
|
|
|
|
|
return (expanded);
|
|
|
|
}
|
|
|
|
|
2011-08-26 12:53:16 +02:00
|
|
|
/* Expand keys in a template. */
|
|
|
|
char *
|
|
|
|
format_expand(struct format_tree *ft, const char *fmt)
|
|
|
|
{
|
2015-10-25 09:59:26 +01:00
|
|
|
char *buf, *tmp, *cmd, *out;
|
2015-08-28 18:46:40 +02:00
|
|
|
const char *ptr, *s, *saved = fmt;
|
2015-10-25 09:59:26 +01:00
|
|
|
size_t off, len, n, outlen;
|
2013-10-10 13:50:36 +02:00
|
|
|
int ch, brackets;
|
2011-08-26 12:53:16 +02:00
|
|
|
|
2015-02-06 18:11:39 +01:00
|
|
|
if (fmt == NULL)
|
|
|
|
return (xstrdup(""));
|
|
|
|
|
2011-08-26 12:53:16 +02:00
|
|
|
len = 64;
|
|
|
|
buf = xmalloc(len);
|
|
|
|
off = 0;
|
|
|
|
|
|
|
|
while (*fmt != '\0') {
|
|
|
|
if (*fmt != '#') {
|
|
|
|
while (len - off < 2) {
|
2014-10-08 19:35:58 +02:00
|
|
|
buf = xreallocarray(buf, 2, len);
|
2011-08-26 12:53:16 +02:00
|
|
|
len *= 2;
|
|
|
|
}
|
|
|
|
buf[off++] = *fmt++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
fmt++;
|
|
|
|
|
|
|
|
ch = (u_char) *fmt++;
|
|
|
|
switch (ch) {
|
2015-05-27 15:28:04 +02: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;
|
|
|
|
|
|
|
|
tmp = xmalloc(n + 1);
|
|
|
|
memcpy(tmp, fmt, n);
|
|
|
|
tmp[n] = '\0';
|
2015-05-30 01:12:38 +02:00
|
|
|
cmd = format_expand(ft, tmp);
|
2015-05-27 15:28:04 +02:00
|
|
|
|
2015-10-25 09:59:26 +01:00
|
|
|
out = format_job_get(ft, cmd);
|
|
|
|
outlen = strlen(out);
|
2015-05-27 15:28:04 +02:00
|
|
|
|
2015-05-30 01:12:38 +02:00
|
|
|
free(cmd);
|
|
|
|
free(tmp);
|
|
|
|
|
2015-10-25 09:59:26 +01:00
|
|
|
while (len - off < outlen + 1) {
|
2015-05-27 15:28:04 +02:00
|
|
|
buf = xreallocarray(buf, 2, len);
|
|
|
|
len *= 2;
|
|
|
|
}
|
2015-10-25 09:59:26 +01:00
|
|
|
memcpy(buf + off, out, outlen);
|
|
|
|
off += outlen;
|
|
|
|
|
|
|
|
free(out);
|
2015-05-27 15:28:04 +02:00
|
|
|
|
|
|
|
fmt += n + 1;
|
|
|
|
continue;
|
2011-08-26 12:53:16 +02:00
|
|
|
case '{':
|
2013-10-10 13:50:36 +02:00
|
|
|
brackets = 1;
|
|
|
|
for (ptr = fmt; *ptr != '\0'; ptr++) {
|
|
|
|
if (*ptr == '{')
|
|
|
|
brackets++;
|
|
|
|
if (*ptr == '}' && --brackets == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*ptr != '}' || brackets != 0)
|
2011-08-26 12:53:16 +02:00
|
|
|
break;
|
|
|
|
n = ptr - fmt;
|
|
|
|
|
|
|
|
if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
|
|
|
|
break;
|
|
|
|
fmt += n + 1;
|
|
|
|
continue;
|
2013-11-24 12:29:09 +01:00
|
|
|
case '#':
|
|
|
|
while (len - off < 2) {
|
2014-10-08 19:35:58 +02:00
|
|
|
buf = xreallocarray(buf, 2, len);
|
2013-11-24 12:29:09 +01:00
|
|
|
len *= 2;
|
|
|
|
}
|
|
|
|
buf[off++] = '#';
|
|
|
|
continue;
|
2011-08-26 12:53:16 +02:00
|
|
|
default:
|
2013-05-31 21:46:42 +02: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 19:35:58 +02:00
|
|
|
buf = xreallocarray(buf, 2, len);
|
2013-05-31 21:46:42 +02:00
|
|
|
len *= 2;
|
2011-08-26 12:53:16 +02:00
|
|
|
}
|
2013-05-31 21:46:42 +02:00
|
|
|
buf[off++] = '#';
|
|
|
|
buf[off++] = ch;
|
|
|
|
continue;
|
2011-08-26 12:53:16 +02:00
|
|
|
}
|
2013-05-31 21:46:42 +02:00
|
|
|
n = strlen(s);
|
|
|
|
if (format_replace(ft, s, n, &buf, &len, &off) != 0)
|
|
|
|
break;
|
2011-08-26 12:53:16 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf[off] = '\0';
|
|
|
|
|
2015-08-28 18:46:40 +02:00
|
|
|
log_debug("format '%s' -> '%s'", saved, buf);
|
2011-08-26 12:53:16 +02:00
|
|
|
return (buf);
|
|
|
|
}
|
|
|
|
|
2015-02-05 11:29:43 +01: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)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
if (s != NULL && wl != NULL)
|
|
|
|
format_defaults_winlink(ft, s, wl);
|
|
|
|
if (wp != NULL)
|
|
|
|
format_defaults_pane(ft, wp);
|
|
|
|
}
|
|
|
|
|
2011-08-26 12:53:16 +02:00
|
|
|
/* Set default format keys for a session. */
|
|
|
|
void
|
2015-02-05 11:29:43 +01:00
|
|
|
format_defaults_session(struct format_tree *ft, struct session *s)
|
2011-08-26 12:53:16 +02:00
|
|
|
{
|
|
|
|
struct session_group *sg;
|
|
|
|
|
2014-12-03 00:19:45 +01:00
|
|
|
ft->s = s;
|
|
|
|
|
2011-08-26 12:53:16 +02:00
|
|
|
format_add(ft, "session_name", "%s", s->name);
|
|
|
|
format_add(ft, "session_windows", "%u", winlink_count(&s->windows));
|
|
|
|
format_add(ft, "session_width", "%u", s->sx);
|
|
|
|
format_add(ft, "session_height", "%u", s->sy);
|
2013-03-25 12:40:54 +01:00
|
|
|
format_add(ft, "session_id", "$%u", s->id);
|
2011-08-26 12:53:16 +02:00
|
|
|
|
|
|
|
sg = session_group_find(s);
|
|
|
|
format_add(ft, "session_grouped", "%d", sg != NULL);
|
|
|
|
if (sg != NULL)
|
|
|
|
format_add(ft, "session_group", "%u", session_group_index(sg));
|
|
|
|
|
2015-10-25 23:29:17 +01: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-08-26 12:53:16 +02:00
|
|
|
|
2014-03-31 23:37:55 +02:00
|
|
|
format_add(ft, "session_attached", "%u", s->attached);
|
2015-03-31 19:45:10 +02:00
|
|
|
format_add(ft, "session_many_attached", "%d", s->attached > 1);
|
2015-05-12 17:29:29 +02:00
|
|
|
|
2015-08-28 19:01:42 +02:00
|
|
|
format_add_cb(ft, "session_alerts", format_cb_session_alerts);
|
2011-08-26 12:53:16 +02:00
|
|
|
}
|
|
|
|
|
2011-10-23 03:12:46 +02:00
|
|
|
/* Set default format keys for a client. */
|
|
|
|
void
|
2015-02-05 11:29:43 +01:00
|
|
|
format_defaults_client(struct format_tree *ft, struct client *c)
|
2011-10-23 03:12:46 +02:00
|
|
|
{
|
2013-03-22 11:32:36 +01:00
|
|
|
struct session *s;
|
2011-10-23 03:12:46 +02:00
|
|
|
|
2014-12-03 00:19:45 +01:00
|
|
|
if (ft->s == NULL)
|
|
|
|
ft->s = c->session;
|
|
|
|
|
2015-06-14 12:07:44 +02:00
|
|
|
format_add(ft, "client_pid", "%ld", (long) c->pid);
|
2012-02-02 01:03:45 +01:00
|
|
|
format_add(ft, "client_height", "%u", c->tty.sy);
|
|
|
|
format_add(ft, "client_width", "%u", c->tty.sx);
|
2013-10-10 13:47:52 +02:00
|
|
|
if (c->tty.path != NULL)
|
|
|
|
format_add(ft, "client_tty", "%s", c->tty.path);
|
|
|
|
if (c->tty.termname != NULL)
|
|
|
|
format_add(ft, "client_termname", "%s", c->tty.termname);
|
2015-07-13 17:37:26 +02:00
|
|
|
format_add(ft, "client_control_mode", "%d",
|
|
|
|
!!(c->flags & CLIENT_CONTROL));
|
2011-10-23 03:12:46 +02:00
|
|
|
|
2015-10-25 23:29:17 +01:00
|
|
|
format_add_tv(ft, "client_created", &c->creation_time);
|
|
|
|
format_add_tv(ft, "client_activity", &c->activity_time);
|
2011-10-23 03:12:46 +02:00
|
|
|
|
2015-04-20 17:34:56 +02:00
|
|
|
if (strcmp(c->keytable->name, "root") == 0)
|
|
|
|
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-03-21 17:14:09 +01:00
|
|
|
|
2011-10-23 03:12:46 +02:00
|
|
|
if (c->tty.flags & TTY_UTF8)
|
|
|
|
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-03-22 11:32:36 +01: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 03:12:46 +02:00
|
|
|
}
|
|
|
|
|
2013-10-10 14:04:01 +02:00
|
|
|
/* Set default format keys for a window. */
|
2011-08-26 12:53:16 +02:00
|
|
|
void
|
2015-02-05 11:29:43 +01:00
|
|
|
format_defaults_window(struct format_tree *ft, struct window *w)
|
2011-08-26 12:53:16 +02:00
|
|
|
{
|
2014-12-03 00:19:45 +01:00
|
|
|
ft->w = w;
|
|
|
|
|
2015-10-25 23:29:17 +01:00
|
|
|
format_add_tv(ft, "window_activity", &w->activity_time);
|
2012-01-30 10:39:34 +01:00
|
|
|
format_add(ft, "window_id", "@%u", w->id);
|
2011-08-26 12:53:16 +02: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 19:01:42 +02:00
|
|
|
format_add_cb(ft, "window_layout", format_cb_window_layout);
|
2015-11-13 11:00:26 +01:00
|
|
|
format_add_cb(ft, "window_visible_layout",
|
|
|
|
format_cb_window_visible_layout);
|
2012-05-22 13:35:37 +02:00
|
|
|
format_add(ft, "window_panes", "%u", window_count_panes(w));
|
2015-03-31 19:45:10 +02:00
|
|
|
format_add(ft, "window_zoomed_flag", "%d",
|
2014-12-01 15:30:18 +01:00
|
|
|
!!(w->flags & WINDOW_ZOOMED));
|
2013-10-10 14:04:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set default format keys for a winlink. */
|
|
|
|
void
|
2015-02-05 11:29:43 +01:00
|
|
|
format_defaults_winlink(struct format_tree *ft, struct session *s,
|
|
|
|
struct winlink *wl)
|
2013-10-10 14:04:01 +02:00
|
|
|
{
|
|
|
|
struct window *w = wl->window;
|
|
|
|
char *flags;
|
|
|
|
|
2014-12-03 00:19:45 +01:00
|
|
|
if (ft->w == NULL)
|
|
|
|
ft->w = wl->window;
|
|
|
|
|
2013-10-10 14:04:01 +02:00
|
|
|
flags = window_printable_flags(s, wl);
|
|
|
|
|
2015-02-05 11:29:43 +01:00
|
|
|
format_defaults_window(ft, w);
|
2013-10-10 14:04:01 +02:00
|
|
|
|
|
|
|
format_add(ft, "window_index", "%d", wl->idx);
|
|
|
|
format_add(ft, "window_flags", "%s", flags);
|
|
|
|
format_add(ft, "window_active", "%d", wl == s->curw);
|
|
|
|
|
2015-03-31 19:45:10 +02:00
|
|
|
format_add(ft, "window_bell_flag", "%d",
|
2013-10-10 13:47:52 +02:00
|
|
|
!!(wl->flags & WINLINK_BELL));
|
2015-03-31 19:45:10 +02:00
|
|
|
format_add(ft, "window_activity_flag", "%d",
|
2013-10-10 13:47:52 +02:00
|
|
|
!!(wl->flags & WINLINK_ACTIVITY));
|
2015-03-31 19:45:10 +02:00
|
|
|
format_add(ft, "window_silence_flag", "%d",
|
2013-10-10 13:47:52 +02:00
|
|
|
!!(wl->flags & WINLINK_SILENCE));
|
2015-03-31 19:45:10 +02:00
|
|
|
format_add(ft, "window_last_flag", "%d",
|
2014-09-08 16:29:05 +02:00
|
|
|
!!(wl == TAILQ_FIRST(&s->lastw)));
|
2015-05-06 10:35:39 +02:00
|
|
|
format_add(ft, "window_linked", "%d", session_is_linked(s, wl->window));
|
2013-10-10 14:04:01 +02:00
|
|
|
|
2012-07-10 13:53:01 +02:00
|
|
|
free(flags);
|
2011-08-26 12:53:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set default format keys for a window pane. */
|
|
|
|
void
|
2015-02-05 11:29:43 +01:00
|
|
|
format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
|
2011-08-26 12:53:16 +02:00
|
|
|
{
|
2015-08-28 19:01:42 +02:00
|
|
|
struct grid *gd = wp->base.grid;
|
|
|
|
u_int idx;
|
2015-10-23 18:02:21 +02:00
|
|
|
int status, scroll_position;
|
2011-08-26 12:53:16 +02:00
|
|
|
|
2014-12-03 00:19:45 +01:00
|
|
|
if (ft->w == NULL)
|
|
|
|
ft->w = wp->window;
|
2015-08-28 18:46:40 +02:00
|
|
|
ft->wp = wp;
|
2014-12-03 00:19:45 +01:00
|
|
|
|
2013-03-22 17:03:35 +01:00
|
|
|
format_add(ft, "history_size", "%u", gd->hsize);
|
|
|
|
format_add(ft, "history_limit", "%u", gd->hlimit);
|
2015-08-28 19:01:42 +02:00
|
|
|
format_add_cb(ft, "history_bytes", format_cb_history_bytes);
|
2011-08-26 12:53:16 +02:00
|
|
|
|
2011-11-16 00:21:52 +01:00
|
|
|
if (window_pane_index(wp, &idx) != 0)
|
|
|
|
fatalx("index not found");
|
2013-03-22 17:03:35 +01:00
|
|
|
format_add(ft, "pane_index", "%u", idx);
|
2011-11-16 00:21:52 +01:00
|
|
|
|
2011-08-26 12:53:16 +02: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);
|
|
|
|
format_add(ft, "pane_active", "%d", wp == wp->window->active);
|
2014-10-25 10:47:04 +02:00
|
|
|
format_add(ft, "pane_input_off", "%d", !!(wp->flags & PANE_INPUTOFF));
|
2013-03-22 17:03:35 +01:00
|
|
|
|
2014-12-09 20:23:35 +01:00
|
|
|
status = wp->status;
|
|
|
|
if (wp->fd == -1 && WIFEXITED(status))
|
|
|
|
format_add(ft, "pane_dead_status", "%d", WEXITSTATUS(status));
|
|
|
|
format_add(ft, "pane_dead", "%d", wp->fd == -1);
|
|
|
|
|
2014-05-27 14:49:36 +02:00
|
|
|
if (window_pane_visible(wp)) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-03-22 17:03:35 +01:00
|
|
|
format_add(ft, "pane_in_mode", "%d", wp->screen != &wp->base);
|
2013-07-05 17:27:14 +02:00
|
|
|
format_add(ft, "pane_synchronized", "%d",
|
2015-10-27 16:58:42 +01:00
|
|
|
!!options_get_number(wp->window->options, "synchronize-panes"));
|
2013-03-22 17:03:35 +01:00
|
|
|
|
2015-06-10 14:56:04 +02:00
|
|
|
format_add(ft, "pane_tty", "%s", wp->tty);
|
2013-03-22 17:03:35 +01:00
|
|
|
format_add(ft, "pane_pid", "%ld", (long) wp->pid);
|
2015-08-28 18:46:40 +02:00
|
|
|
format_add_cb(ft, "pane_start_command", format_cb_start_command);
|
|
|
|
format_add_cb(ft, "pane_current_command", format_cb_current_command);
|
2013-03-22 17:03:35 +01:00
|
|
|
|
2015-03-31 19:45:10 +02:00
|
|
|
format_add(ft, "cursor_x", "%u", wp->base.cx);
|
|
|
|
format_add(ft, "cursor_y", "%u", wp->base.cy);
|
|
|
|
format_add(ft, "scroll_region_upper", "%u", wp->base.rupper);
|
|
|
|
format_add(ft, "scroll_region_lower", "%u", wp->base.rlower);
|
2013-03-22 17:03:35 +01:00
|
|
|
|
2015-10-23 18:02:21 +02:00
|
|
|
scroll_position = window_copy_scroll_position(wp);
|
|
|
|
if (scroll_position != -1)
|
|
|
|
format_add(ft, "scroll_position", "%d", scroll_position);
|
|
|
|
|
2013-03-22 17:03:35 +01:00
|
|
|
format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0);
|
2015-03-31 19:45:10 +02:00
|
|
|
format_add(ft, "alternate_saved_x", "%u", wp->saved_cx);
|
|
|
|
format_add(ft, "alternate_saved_y", "%u", wp->saved_cy);
|
2013-03-22 17:03:35 +01: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));
|
|
|
|
|
2015-04-21 17:18:38 +02:00
|
|
|
format_add(ft, "mouse_any_flag", "%d",
|
|
|
|
!!(wp->base.mode & (MODE_MOUSE_STANDARD|MODE_MOUSE_BUTTON)));
|
2013-03-22 17:03:35 +01: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));
|
2013-03-24 10:29:40 +01:00
|
|
|
|
2015-08-28 19:01:42 +02:00
|
|
|
format_add_cb(ft, "pane_tabs", format_cb_pane_tabs);
|
2011-08-26 12:53:16 +02:00
|
|
|
}
|
2012-05-22 13:35:37 +02:00
|
|
|
|
2013-03-24 10:29:40 +01:00
|
|
|
/* Set default format keys for paste buffer. */
|
2012-05-22 13:35:37 +02:00
|
|
|
void
|
2015-11-12 12:09:11 +01:00
|
|
|
format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
|
2012-05-22 13:35:37 +02:00
|
|
|
{
|
2015-08-29 11:25:00 +02:00
|
|
|
size_t bufsize;
|
2014-04-02 20:12:18 +02:00
|
|
|
char *s;
|
2012-05-22 13:35:37 +02:00
|
|
|
|
2015-08-29 11:25:00 +02:00
|
|
|
paste_buffer_data(pb, &bufsize);
|
|
|
|
format_add(ft, "buffer_size", "%zu", bufsize);
|
|
|
|
format_add(ft, "buffer_name", "%s", paste_buffer_name(pb));
|
2012-05-22 13:35:37 +02:00
|
|
|
|
2015-11-12 12:09:11 +01:00
|
|
|
s = paste_make_sample(pb);
|
2014-04-02 20:12:18 +02:00
|
|
|
format_add(ft, "buffer_sample", "%s", s);
|
|
|
|
free(s);
|
2012-05-22 13:35:37 +02:00
|
|
|
}
|