Add a way for lines added to copy mode to be passed through the parser

to handle escape sequences and use it for run-shell, GitHub issue 3156.
This commit is contained in:
nicm 2022-05-30 13:00:18 +00:00
parent 20b0b38cf4
commit cd89000c1d
6 changed files with 103 additions and 36 deletions

2
cfg.c
View File

@ -251,7 +251,7 @@ cfg_show_causes(struct session *s)
if (wme == NULL || wme->mode != &window_view_mode) if (wme == NULL || wme->mode != &window_view_mode)
window_pane_set_mode(wp, NULL, &window_view_mode, NULL, NULL); window_pane_set_mode(wp, NULL, &window_view_mode, NULL, NULL);
for (i = 0; i < cfg_ncauses; i++) { for (i = 0; i < cfg_ncauses; i++) {
window_copy_add(wp, "%s", cfg_causes[i]); window_copy_add(wp, 0, "%s", cfg_causes[i]);
free(cfg_causes[i]); free(cfg_causes[i]);
} }

View File

@ -19,9 +19,11 @@
#include <sys/types.h> #include <sys/types.h>
#include <ctype.h> #include <ctype.h>
#include <pwd.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <unistd.h>
#include "tmux.h" #include "tmux.h"
@ -558,17 +560,31 @@ cmdq_add_message(struct cmdq_item *item)
{ {
struct client *c = item->client; struct client *c = item->client;
struct cmdq_state *state = item->state; struct cmdq_state *state = item->state;
const char *name, *key; const char *key;
char *tmp; char *tmp;
uid_t uid;
struct passwd *pw;
char *user = NULL;
tmp = cmd_print(item->cmd); tmp = cmd_print(item->cmd);
if (c != NULL) { if (c != NULL) {
name = c->name; uid = proc_get_peer_uid(c->peer);
if (uid != (uid_t)-1 && uid != getuid()) {
if ((pw = getpwuid(uid)) != NULL)
xasprintf(&user, "[%s]", pw->pw_name);
else
user = xstrdup("[unknown]");
} else
user = xstrdup("");
if (c->session != NULL && state->event.key != KEYC_NONE) { if (c->session != NULL && state->event.key != KEYC_NONE) {
key = key_string_lookup_key(state->event.key, 0); key = key_string_lookup_key(state->event.key, 0);
server_add_message("%s key %s: %s", name, key, tmp); server_add_message("%s%s key %s: %s", c->name, user,
} else key, tmp);
server_add_message("%s command: %s", name, tmp); } else {
server_add_message("%s%s command: %s", c->name, user,
tmp);
}
free(user);
} else } else
server_add_message("command: %s", tmp); server_add_message("command: %s", tmp);
free(tmp); free(tmp);
@ -840,7 +856,7 @@ cmdq_print(struct cmdq_item *item, const char *fmt, ...)
window_pane_set_mode(wp, NULL, &window_view_mode, NULL, window_pane_set_mode(wp, NULL, &window_view_mode, NULL,
NULL); NULL);
} }
window_copy_add(wp, "%s", msg); window_copy_add(wp, 0, "%s", msg);
} }
free(msg); free(msg);

View File

@ -84,22 +84,17 @@ cmd_run_shell_print(struct job *job, const char *msg)
if (cdata->wp_id != -1) if (cdata->wp_id != -1)
wp = window_pane_find_by_id(cdata->wp_id); wp = window_pane_find_by_id(cdata->wp_id);
if (wp == NULL) { if (wp == NULL && cdata->item != NULL)
if (cdata->item != NULL) { wp = server_client_get_pane(cdata->client);
cmdq_print(cdata->item, "%s", msg); if (wp == NULL && cmd_find_from_nothing(&fs, 0) == 0)
return;
}
if (cmd_find_from_nothing(&fs, 0) != 0)
return;
wp = fs.wp; wp = fs.wp;
if (wp == NULL) if (wp == NULL)
return; return;
}
wme = TAILQ_FIRST(&wp->modes); wme = TAILQ_FIRST(&wp->modes);
if (wme == NULL || wme->mode != &window_view_mode) if (wme == NULL || wme->mode != &window_view_mode)
window_pane_set_mode(wp, NULL, &window_view_mode, NULL, NULL); window_pane_set_mode(wp, NULL, &window_view_mode, NULL, NULL);
window_copy_add(wp, "%s", msg); window_copy_add(wp, 1, "%s", msg);
} }
static enum cmd_retval static enum cmd_retval
@ -227,7 +222,8 @@ cmd_run_shell_callback(struct job *job)
int retcode, status; int retcode, status;
do { do {
if ((line = evbuffer_readline(event->input)) != NULL) { line = evbuffer_readln(event->input, NULL, EVBUFFER_EOL_LF);
if (line != NULL) {
cmd_run_shell_print(job, line); cmd_run_shell_print(job, line);
free(line); free(line);
} }

View File

@ -1078,6 +1078,9 @@ input_reply(struct input_ctx *ictx, const char *fmt, ...)
va_list ap; va_list ap;
char *reply; char *reply;
if (bev == NULL)
return;
va_start(ap, fmt); va_start(ap, fmt);
xvasprintf(&reply, fmt, ap); xvasprintf(&reply, fmt, ap);
va_end(ap); va_end(ap);
@ -1798,6 +1801,8 @@ input_csi_dispatch_sm_private(struct input_ctx *ictx)
screen_write_mode_set(sctx, MODE_FOCUSON); screen_write_mode_set(sctx, MODE_FOCUSON);
if (wp == NULL) if (wp == NULL)
break; break;
if (!options_get_number(global_options, "focus-events"))
break;
if (wp->flags & PANE_FOCUSED) if (wp->flags & PANE_FOCUSED)
bufferevent_write(wp->event, "\033[I", 3); bufferevent_write(wp->event, "\033[I", 3);
else else

19
tmux.h
View File

@ -2024,6 +2024,7 @@ struct tmuxpeer *proc_add_peer(struct tmuxproc *, int,
void (*)(struct imsg *, void *), void *); void (*)(struct imsg *, void *), void *);
void proc_remove_peer(struct tmuxpeer *); void proc_remove_peer(struct tmuxpeer *);
void proc_kill_peer(struct tmuxpeer *); void proc_kill_peer(struct tmuxpeer *);
void proc_flush_peer(struct tmuxpeer *);
void proc_toggle_log(struct tmuxproc *); void proc_toggle_log(struct tmuxproc *);
pid_t proc_fork_and_daemon(int *); pid_t proc_fork_and_daemon(int *);
uid_t proc_get_peer_uid(struct tmuxpeer *); uid_t proc_get_peer_uid(struct tmuxpeer *);
@ -3017,7 +3018,7 @@ void layout_spread_out(struct window_pane *);
/* layout-custom.c */ /* layout-custom.c */
char *layout_dump(struct layout_cell *); char *layout_dump(struct layout_cell *);
int layout_parse(struct window *, const char *); int layout_parse(struct window *, const char *, char **);
/* layout-set.c */ /* layout-set.c */
int layout_set_lookup(const char *); int layout_set_lookup(const char *);
@ -3082,8 +3083,9 @@ extern const struct window_mode window_client_mode;
/* window-copy.c */ /* window-copy.c */
extern const struct window_mode window_copy_mode; extern const struct window_mode window_copy_mode;
extern const struct window_mode window_view_mode; extern const struct window_mode window_view_mode;
void printflike(2, 3) window_copy_add(struct window_pane *, const char *, ...); void printflike(3, 4) window_copy_add(struct window_pane *, int, const char *,
void printflike(2, 0) window_copy_vadd(struct window_pane *, const char *, ...);
void printflike(3, 0) window_copy_vadd(struct window_pane *, int, const char *,
va_list); va_list);
void window_copy_pageup(struct window_pane *, int); void window_copy_pageup(struct window_pane *, int);
void window_copy_start_drag(struct client *, struct mouse_event *); void window_copy_start_drag(struct client *, struct mouse_event *);
@ -3266,4 +3268,15 @@ struct window_pane *spawn_pane(struct spawn_context *, char **);
/* regsub.c */ /* regsub.c */
char *regsub(const char *, const char *, const char *, int); char *regsub(const char *, const char *, const char *, int);
/* server-acl.c */
void server_acl_init(void);
struct server_acl_user *server_acl_user_find(uid_t);
void server_acl_display(struct cmdq_item *);
void server_acl_user_allow(uid_t);
void server_acl_user_deny(uid_t);
void server_acl_user_allow_write(uid_t);
void server_acl_user_deny_write(uid_t);
int server_acl_join(struct client *);
uid_t server_acl_get_uid(struct server_acl_user *);
#endif /* TMUX_H */ #endif /* TMUX_H */

View File

@ -222,6 +222,8 @@ struct window_copy_mode_data {
struct screen *backing; struct screen *backing;
int backing_written; /* backing display started */ int backing_written; /* backing display started */
struct screen *writing;
struct input_ctx *ictx;
int viewmode; /* view mode entered */ int viewmode; /* view mode entered */
@ -467,13 +469,16 @@ window_copy_view_init(struct window_mode_entry *wme,
struct window_pane *wp = wme->wp; struct window_pane *wp = wme->wp;
struct window_copy_mode_data *data; struct window_copy_mode_data *data;
struct screen *base = &wp->base; struct screen *base = &wp->base;
struct screen *s; u_int sx = screen_size_x(base);
data = window_copy_common_init(wme); data = window_copy_common_init(wme);
data->viewmode = 1; data->viewmode = 1;
data->backing = s = xmalloc(sizeof *data->backing); data->backing = xmalloc(sizeof *data->backing);
screen_init(s, screen_size_x(base), screen_size_y(base), UINT_MAX); screen_init(data->backing, sx, screen_size_y(base), UINT_MAX);
data->writing = xmalloc(sizeof *data->writing);
screen_init(data->writing, sx, screen_size_y(base), 0);
data->ictx = input_init(NULL, NULL, NULL);
data->mx = data->cx; data->mx = data->cx;
data->my = screen_hsize(data->backing) + data->cy - data->oy; data->my = screen_hsize(data->backing) + data->cy - data->oy;
data->showmark = 0; data->showmark = 0;
@ -492,6 +497,12 @@ window_copy_free(struct window_mode_entry *wme)
free(data->searchstr); free(data->searchstr);
free(data->jumpchar); free(data->jumpchar);
if (data->writing != NULL) {
screen_free(data->writing);
free(data->writing);
}
if (data->ictx != NULL)
input_free(data->ictx);
screen_free(data->backing); screen_free(data->backing);
free(data->backing); free(data->backing);
@ -500,41 +511,67 @@ window_copy_free(struct window_mode_entry *wme)
} }
void void
window_copy_add(struct window_pane *wp, const char *fmt, ...) window_copy_add(struct window_pane *wp, int parse, const char *fmt, ...)
{ {
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
window_copy_vadd(wp, fmt, ap); window_copy_vadd(wp, parse, fmt, ap);
va_end(ap); va_end(ap);
} }
static void
window_copy_init_ctx_cb(__unused struct screen_write_ctx *ctx,
struct tty_ctx *ttyctx)
{
memcpy(&ttyctx->defaults, &grid_default_cell, sizeof ttyctx->defaults);
ttyctx->palette = NULL;
ttyctx->redraw_cb = NULL;
ttyctx->set_client_cb = NULL;
ttyctx->arg = NULL;
}
void void
window_copy_vadd(struct window_pane *wp, const char *fmt, va_list ap) window_copy_vadd(struct window_pane *wp, int parse, const char *fmt, va_list ap)
{ {
struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes); struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes);
struct window_copy_mode_data *data = wme->data; struct window_copy_mode_data *data = wme->data;
struct screen *backing = data->backing; struct screen *backing = data->backing;
struct screen_write_ctx back_ctx, ctx; struct screen *writing = data->writing;
struct screen_write_ctx writing_ctx, backing_ctx, ctx;
struct grid_cell gc; struct grid_cell gc;
u_int old_hsize, old_cy; u_int old_hsize, old_cy;
u_int sx = screen_size_x(backing);
char *text;
memcpy(&gc, &grid_default_cell, sizeof gc); if (parse) {
vasprintf(&text, fmt, ap);
screen_write_start(&writing_ctx, writing);
screen_write_reset(&writing_ctx);
input_parse_screen(data->ictx, writing, window_copy_init_ctx_cb,
data, text, strlen(text));
free(text);
}
old_hsize = screen_hsize(data->backing); old_hsize = screen_hsize(data->backing);
screen_write_start(&back_ctx, backing); screen_write_start(&backing_ctx, backing);
if (data->backing_written) { if (data->backing_written) {
/* /*
* On the second or later line, do a CRLF before writing * On the second or later line, do a CRLF before writing
* (so it's on a new line). * (so it's on a new line).
*/ */
screen_write_carriagereturn(&back_ctx); screen_write_carriagereturn(&backing_ctx);
screen_write_linefeed(&back_ctx, 0, 8); screen_write_linefeed(&backing_ctx, 0, 8);
} else } else
data->backing_written = 1; data->backing_written = 1;
old_cy = backing->cy; old_cy = backing->cy;
screen_write_vnputs(&back_ctx, 0, &gc, fmt, ap); if (parse)
screen_write_stop(&back_ctx); screen_write_fast_copy(&backing_ctx, writing, 0, 0, sx, 1);
else {
memcpy(&gc, &grid_default_cell, sizeof gc);
screen_write_vnputs(&backing_ctx, 0, &gc, fmt, ap);
}
screen_write_stop(&backing_ctx);
data->oy += screen_hsize(data->backing) - old_hsize; data->oy += screen_hsize(data->backing) - old_hsize;