summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@cvs.openbsd.org>2009-11-04 22:43:12 +0000
committerNicholas Marriott <nicm@cvs.openbsd.org>2009-11-04 22:43:12 +0000
commitdfb9b83ba668ae18082113a9efa8908a3c0c304a (patch)
treea0c75dd6f3030ea7c3ce4a622a78a5163d5d8904 /usr.bin
parent7c41588f63dd0e0c60b445558f675ed033d96e4d (diff)
Convert the window pane (pty master side) fd over to use a bufferevent.
The evbuffer API is very similar to the existing tmux buffer API so this was remarkably painless. Not many possible ways to do it, I suppose.
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/tmux/cmd-paste-buffer.c16
-rw-r--r--usr.bin/tmux/cmd-pipe-pane.c4
-rw-r--r--usr.bin/tmux/input-keys.c24
-rw-r--r--usr.bin/tmux/input.c26
-rw-r--r--usr.bin/tmux/server-window.c56
-rw-r--r--usr.bin/tmux/server.c3
-rw-r--r--usr.bin/tmux/tmux.h8
-rw-r--r--usr.bin/tmux/window.c53
8 files changed, 89 insertions, 101 deletions
diff --git a/usr.bin/tmux/cmd-paste-buffer.c b/usr.bin/tmux/cmd-paste-buffer.c
index f00e7ac38e1..d52f5307f02 100644
--- a/usr.bin/tmux/cmd-paste-buffer.c
+++ b/usr.bin/tmux/cmd-paste-buffer.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: cmd-paste-buffer.c,v 1.6 2009/09/07 18:50:45 nicm Exp $ */
+/* $OpenBSD: cmd-paste-buffer.c,v 1.7 2009/11/04 22:43:11 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -27,7 +27,7 @@
*/
int cmd_paste_buffer_exec(struct cmd *, struct cmd_ctx *);
-void cmd_paste_buffer_lf2cr(struct buffer *, const char *, size_t);
+void cmd_paste_buffer_lf2cr(struct window_pane *, const char *, size_t);
const struct cmd_entry cmd_paste_buffer_entry = {
"paste-buffer", "pasteb",
@@ -65,9 +65,9 @@ cmd_paste_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
if (pb != NULL && *pb->data != '\0') {
/* -r means raw data without LF->CR conversion. */
if (data->chflags & CMD_CHFLAG('r'))
- buffer_write(wp->out, pb->data, pb->size);
+ bufferevent_write(wp->event, pb->data, pb->size);
else
- cmd_paste_buffer_lf2cr(wp->out, pb->data, pb->size);
+ cmd_paste_buffer_lf2cr(wp, pb->data, pb->size);
}
/* Delete the buffer if -d. */
@@ -83,18 +83,18 @@ cmd_paste_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
/* Add bytes to a buffer but change every '\n' to '\r'. */
void
-cmd_paste_buffer_lf2cr(struct buffer *b, const char *data, size_t size)
+cmd_paste_buffer_lf2cr(struct window_pane *wp, const char *data, size_t size)
{
const char *end = data + size;
const char *lf;
while ((lf = memchr(data, '\n', end - data)) != NULL) {
if (lf != data)
- buffer_write(b, data, lf - data);
- buffer_write8(b, '\r');
+ bufferevent_write(wp->event, data, lf - data);
+ bufferevent_write(wp->event, "\r", 1);
data = lf + 1;
}
if (end != data)
- buffer_write(b, data, end - data);
+ bufferevent_write(wp->event, data, end - data);
}
diff --git a/usr.bin/tmux/cmd-pipe-pane.c b/usr.bin/tmux/cmd-pipe-pane.c
index bcd835657ed..38c967f11c3 100644
--- a/usr.bin/tmux/cmd-pipe-pane.c
+++ b/usr.bin/tmux/cmd-pipe-pane.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: cmd-pipe-pane.c,v 1.4 2009/11/04 22:02:38 nicm Exp $ */
+/* $OpenBSD: cmd-pipe-pane.c,v 1.5 2009/11/04 22:43:11 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -113,7 +113,7 @@ cmd_pipe_pane_exec(struct cmd *self, struct cmd_ctx *ctx)
close(pipe_fd[1]);
wp->pipe_fd = pipe_fd[0];
- wp->pipe_off = BUFFER_USED(wp->in);
+ wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
wp->pipe_event = bufferevent_new(wp->pipe_fd,
NULL, NULL, cmd_pipe_pane_error_callback, wp);
diff --git a/usr.bin/tmux/input-keys.c b/usr.bin/tmux/input-keys.c
index 285eac3d75c..61e15b298a9 100644
--- a/usr.bin/tmux/input-keys.c
+++ b/usr.bin/tmux/input-keys.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: input-keys.c,v 1.13 2009/10/26 17:59:46 nicm Exp $ */
+/* $OpenBSD: input-keys.c,v 1.14 2009/11/04 22:43:11 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -163,6 +163,7 @@ input_key(struct window_pane *wp, int key)
u_int i;
size_t dlen;
char *out;
+ u_char ch;
log_debug2("writing key 0x%x", key);
@@ -172,8 +173,10 @@ input_key(struct window_pane *wp, int key)
*/
if (key != KEYC_NONE && (key & ~KEYC_ESCAPE) < 0x100) {
if (key & KEYC_ESCAPE)
- buffer_write8(wp->out, '\033');
- buffer_write8(wp->out, (uint8_t) (key & ~KEYC_ESCAPE));
+ ch = '\033';
+ else
+ ch = key & ~KEYC_ESCAPE;
+ bufferevent_write(wp->event, &ch, 1);
return;
}
@@ -183,7 +186,7 @@ input_key(struct window_pane *wp, int key)
*/
if (options_get_number(&wp->window->options, "xterm-keys")) {
if ((out = xterm_keys_lookup(key)) != NULL) {
- buffer_write(wp->out, out, strlen(out));
+ bufferevent_write(wp->event, out, strlen(out));
xfree(out);
return;
}
@@ -214,18 +217,19 @@ input_key(struct window_pane *wp, int key)
/* Prefix a \033 for escape. */
if (key & KEYC_ESCAPE)
- buffer_write8(wp->out, '\033');
- buffer_write(wp->out, ike->data, dlen);
+ bufferevent_write(wp->event, "\033", 1);
+ bufferevent_write(wp->event, ike->data, dlen);
}
/* Translate mouse and output. */
void
input_mouse(struct window_pane *wp, struct mouse_event *m)
{
+ char out[8];
+
if (wp->screen->mode & MODE_MOUSE) {
- buffer_write(wp->out, "\033[M", 3);
- buffer_write8(wp->out, m->b + 32);
- buffer_write8(wp->out, m->x + 33);
- buffer_write8(wp->out, m->y + 33);
+ xsnprintf(out, sizeof out,
+ "\033[M%c%c%c", m->b + 32, m->x + 33, m->y + 33);
+ bufferevent_write(wp->event, out, strlen(out));
}
}
diff --git a/usr.bin/tmux/input.c b/usr.bin/tmux/input.c
index 2e6a4d5da5e..f393b093d02 100644
--- a/usr.bin/tmux/input.c
+++ b/usr.bin/tmux/input.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: input.c,v 1.21 2009/10/26 21:42:04 deraadt Exp $ */
+/* $OpenBSD: input.c,v 1.22 2009/11/04 22:43:11 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -256,12 +256,12 @@ input_parse(struct window_pane *wp)
struct input_ctx *ictx = &wp->ictx;
u_char ch;
- if (BUFFER_USED(wp->in) == ictx->was)
+ if (EVBUFFER_LENGTH(wp->event->input) == ictx->was)
return;
wp->window->flags |= WINDOW_ACTIVITY;
- ictx->buf = BUFFER_OUT(wp->in);
- ictx->len = BUFFER_USED(wp->in);
+ ictx->buf = EVBUFFER_DATA(wp->event->input);
+ ictx->len = EVBUFFER_LENGTH(wp->event->input);
ictx->off = 0;
ictx->wp = wp;
@@ -278,8 +278,8 @@ input_parse(struct window_pane *wp)
screen_write_stop(&ictx->ctx);
- buffer_remove(wp->in, ictx->len);
- ictx->was = BUFFER_USED(wp->in);
+ evbuffer_drain(wp->event->input, ictx->len);
+ ictx->was = EVBUFFER_LENGTH(wp->event->input);
}
void
@@ -932,7 +932,8 @@ input_handle_sequence_cbt(struct input_ctx *ictx)
void
input_handle_sequence_da(struct input_ctx *ictx)
{
- uint16_t n;
+ struct window_pane *wp = ictx->wp;
+ uint16_t n;
if (ictx->private != '\0')
return;
@@ -944,7 +945,7 @@ input_handle_sequence_da(struct input_ctx *ictx)
if (n != 0)
return;
- buffer_write(ictx->wp->out, "\033[?1;2c", (sizeof "\033[?1;2c") - 1);
+ bufferevent_write(wp->event, "\033[?1;2c", (sizeof "\033[?1;2c") - 1);
}
void
@@ -1314,9 +1315,10 @@ input_handle_sequence_rm(struct input_ctx *ictx)
void
input_handle_sequence_dsr(struct input_ctx *ictx)
{
- struct screen *s = ictx->ctx.s;
- uint16_t n;
- char reply[32];
+ struct window_pane *wp = ictx->wp;
+ struct screen *s = ictx->ctx.s;
+ uint16_t n;
+ char reply[32];
if (ARRAY_LENGTH(&ictx->args) > 1)
return;
@@ -1329,7 +1331,7 @@ input_handle_sequence_dsr(struct input_ctx *ictx)
xsnprintf(reply, sizeof reply,
"\033[%u;%uR", s->cy + 1, s->cx + 1);
log_debug("cursor request, reply: %s", reply);
- buffer_write(ictx->wp->out, reply, strlen(reply));
+ bufferevent_write(wp->event, reply, strlen(reply));
break;
}
}
diff --git a/usr.bin/tmux/server-window.c b/usr.bin/tmux/server-window.c
index 868437650eb..f618fa2f6ab 100644
--- a/usr.bin/tmux/server-window.c
+++ b/usr.bin/tmux/server-window.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: server-window.c,v 1.8 2009/11/04 22:02:38 nicm Exp $ */
+/* $OpenBSD: server-window.c,v 1.9 2009/11/04 22:43:11 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -30,35 +30,6 @@ int server_window_check_content(
struct session *, struct window *, struct window_pane *);
void server_window_check_alive(struct window *);
-/* Register windows for poll. */
-void
-server_window_prepare(void)
-{
- struct window *w;
- struct window_pane *wp;
- u_int i;
- int events;
-
- for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
- if ((w = ARRAY_ITEM(&windows, i)) == NULL)
- continue;
-
- TAILQ_FOREACH(wp, &w->panes, entry) {
- if (wp->fd == -1)
- continue;
- events = 0;
- if (!server_window_backoff(wp))
- events |= EV_READ;
- if (BUFFER_USED(wp->out) > 0)
- events |= EV_WRITE;
- event_del(&wp->event);
- event_set(&wp->event,
- wp->fd, events, server_window_callback, wp);
- event_add(&wp->event, NULL);
- }
- }
-}
-
/* Check if this window should suspend reading. */
int
server_window_backoff(struct window_pane *wp)
@@ -84,24 +55,6 @@ server_window_backoff(struct window_pane *wp)
return (0);
}
-/* Process a single window pane event. */
-void
-server_window_callback(int fd, short events, void *data)
-{
- struct window_pane *wp = data;
-
- if (wp->fd == -1)
- return;
-
- if (fd == wp->fd) {
- if (buffer_poll(fd, events, wp->in, wp->out) != 0) {
- close(wp->fd);
- wp->fd = -1;
- } else
- window_pane_parse(wp);
- }
-}
-
/* Window functions that need to happen every loop. */
void
server_window_loop(void)
@@ -116,6 +69,13 @@ server_window_loop(void)
if (w == NULL)
continue;
+ TAILQ_FOREACH(wp, &w->panes, entry) {
+ if (server_window_backoff(wp))
+ bufferevent_disable(wp->event, EV_READ);
+ else
+ bufferevent_enable(wp->event, EV_READ);
+ }
+
for (j = 0; j < ARRAY_LENGTH(&sessions); j++) {
s = ARRAY_ITEM(&sessions, j);
if (s == NULL || !session_has(s, w))
diff --git a/usr.bin/tmux/server.c b/usr.bin/tmux/server.c
index 033edd7b5e2..19468ef4cbf 100644
--- a/usr.bin/tmux/server.c
+++ b/usr.bin/tmux/server.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: server.c,v 1.68 2009/11/04 22:40:36 nicm Exp $ */
+/* $OpenBSD: server.c,v 1.69 2009/11/04 22:43:11 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -214,7 +214,6 @@ server_loop(void)
while (!server_should_shutdown()) {
server_update_socket();
- server_window_prepare();
server_client_prepare();
event_loopexit(&tv);
diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h
index 9761eeab479..2df39ccca7b 100644
--- a/usr.bin/tmux/tmux.h
+++ b/usr.bin/tmux/tmux.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: tmux.h,v 1.162 2009/11/04 22:02:38 nicm Exp $ */
+/* $OpenBSD: tmux.h,v 1.163 2009/11/04 22:43:11 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -798,9 +798,7 @@ struct window_pane {
char tty[TTY_NAME_MAX];
int fd;
- struct event event;
- struct buffer *in;
- struct buffer *out;
+ struct bufferevent *event;
struct input_ctx ictx;
@@ -1586,8 +1584,6 @@ void server_client_callback(int, short, void *);
void server_client_loop(void);
/* server-window.c */
-void server_window_prepare(void);
-void server_window_callback(int, short, void *);
void server_window_loop(void);
/* server-fn.c */
diff --git a/usr.bin/tmux/window.c b/usr.bin/tmux/window.c
index 2417f06dcc5..ac388eba8be 100644
--- a/usr.bin/tmux/window.c
+++ b/usr.bin/tmux/window.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: window.c,v 1.36 2009/11/04 22:02:38 nicm Exp $ */
+/* $OpenBSD: window.c,v 1.37 2009/11/04 22:43:11 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -56,6 +56,9 @@
/* Global window list. */
struct windows windows;
+void window_pane_read_callback(struct bufferevent *, void *);
+void window_pane_error_callback(struct bufferevent *, short, void *);
+
RB_GENERATE(winlinks, winlink, entry, winlink_cmp);
int
@@ -412,8 +415,7 @@ window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
wp->cwd = NULL;
wp->fd = -1;
- wp->in = buffer_create(BUFSIZ);
- wp->out = buffer_create(BUFSIZ);
+ wp->event = NULL;
wp->mode = NULL;
@@ -442,8 +444,10 @@ window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
void
window_pane_destroy(struct window_pane *wp)
{
- if (wp->fd != -1)
+ if (wp->fd != -1) {
close(wp->fd);
+ bufferevent_free(wp->event);
+ }
input_free(wp);
@@ -457,10 +461,6 @@ window_pane_destroy(struct window_pane *wp)
bufferevent_free(wp->pipe_event);
}
- buffer_destroy(wp->in);
- buffer_destroy(wp->out);
- event_del(&wp->event);
-
if (wp->cwd != NULL)
xfree(wp->cwd);
if (wp->shell != NULL)
@@ -484,8 +484,10 @@ window_pane_spawn(struct window_pane *wp, const char *cmd, const char *shell,
struct termios tio2;
u_int i;
- if (wp->fd != -1)
+ if (wp->fd != -1) {
close(wp->fd);
+ bufferevent_free(wp->event);
+ }
if (cmd != NULL) {
if (wp->cmd != NULL)
xfree(wp->cmd);
@@ -574,11 +576,33 @@ window_pane_spawn(struct window_pane *wp, const char *cmd, const char *shell,
fatal("fcntl failed");
if (fcntl(wp->fd, F_SETFD, FD_CLOEXEC) == -1)
fatal("fcntl failed");
+ wp->event = bufferevent_new(wp->fd,
+ window_pane_read_callback, NULL, window_pane_error_callback, wp);
+ bufferevent_enable(wp->event, EV_READ|EV_WRITE);
return (0);
}
void
+window_pane_read_callback(unused struct bufferevent *bufev, void *data)
+{
+ struct window_pane *wp = data;
+
+ window_pane_parse(wp);
+}
+
+void
+window_pane_error_callback(
+ unused struct bufferevent *bufev, unused short what, void *data)
+{
+ struct window_pane *wp = data;
+
+ close(wp->fd);
+ bufferevent_free(wp->event);
+ wp->fd = -1;
+}
+
+void
window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
{
struct winsize ws;
@@ -631,18 +655,21 @@ window_pane_reset_mode(struct window_pane *wp)
void
window_pane_parse(struct window_pane *wp)
{
+ char *data;
size_t new_size;
if (wp->mode != NULL)
return;
- new_size = BUFFER_USED(wp->in) - wp->pipe_off;
- if (wp->pipe_fd != -1 && new_size > 0)
- bufferevent_write(wp->pipe_event, BUFFER_OUT(wp->in), new_size);
+ new_size = EVBUFFER_LENGTH(wp->event->input) - wp->pipe_off;
+ if (wp->pipe_fd != -1 && new_size > 0) {
+ data = EVBUFFER_DATA(wp->event->input);
+ bufferevent_write(wp->pipe_event, data, new_size);
+ }
input_parse(wp);
- wp->pipe_off = BUFFER_USED(wp->in);
+ wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
}
void