summaryrefslogtreecommitdiffstats
path: root/usr.bin/tmux/server-client.c
diff options
context:
space:
mode:
authornicm <nicm@openbsd.org>2016-10-16 17:55:14 +0000
committernicm <nicm@openbsd.org>2016-10-16 17:55:14 +0000
commit765b9a582c208af75c7304abae7e7e4c757934cb (patch)
treec6ba4d8161097c3d648fe4a1d213628591795591 /usr.bin/tmux/server-client.c
parentUse the err(3) family of functions more consistently. (diff)
downloadwireguard-openbsd-765b9a582c208af75c7304abae7e7e4c757934cb.tar.xz
wireguard-openbsd-765b9a582c208af75c7304abae7e7e4c757934cb.zip
Rewrite command queue handling. Each client still has a command queue,
but there is also now a global command queue. Instead of command queues being dispatched on demand from wherever the command happens to be added, they are now all dispatched from the top level server loop. Command queues may now also include callbacks as well as commands, and items may be inserted after the current command as well as at the end. This all makes command queues significantly more predictable and easier to use, and avoids the complex multiple nested command queues used by source-file, if-shell and friends. A mass rename of struct cmdq to a better name (cmdq_item probably) is coming.
Diffstat (limited to 'usr.bin/tmux/server-client.c')
-rw-r--r--usr.bin/tmux/server-client.c46
1 files changed, 33 insertions, 13 deletions
diff --git a/usr.bin/tmux/server-client.c b/usr.bin/tmux/server-client.c
index 149fba0e2d3..243e8233ea4 100644
--- a/usr.bin/tmux/server-client.c
+++ b/usr.bin/tmux/server-client.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: server-client.c,v 1.193 2016/10/12 13:03:27 nicm Exp $ */
+/* $OpenBSD: server-client.c,v 1.194 2016/10/16 17:55:14 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -126,8 +126,7 @@ server_client_create(int fd)
c->fd = -1;
c->cwd = NULL;
- c->cmdq = cmdq_new(c);
- c->cmdq->client_exit = 1;
+ TAILQ_INIT(&c->queue);
c->stdin_data = evbuffer_new();
c->stdout_data = evbuffer_new();
@@ -244,10 +243,6 @@ server_client_lost(struct client *c)
free(c->prompt_string);
free(c->prompt_buffer);
- c->cmdq->flags |= CMD_Q_DEAD;
- cmdq_free(c->cmdq);
- c->cmdq = NULL;
-
environ_free(c->environ);
proc_remove_peer(c->peer);
@@ -281,6 +276,9 @@ server_client_free(__unused int fd, __unused short events, void *arg)
log_debug("free client %p (%d references)", c, c->references);
+ if (!TAILQ_EMPTY(&c->queue))
+ fatalx("queue not empty");
+
if (c->references == 0)
free(c);
}
@@ -1254,6 +1252,29 @@ server_client_dispatch(struct imsg *imsg, void *arg)
}
}
+/* Callback when command is done. */
+static enum cmd_retval
+server_client_command_done(struct cmd_q *cmdq, __unused void *data)
+{
+ struct client *c = cmdq->client;
+
+ if (~c->flags & CLIENT_ATTACHED)
+ c->flags |= CLIENT_EXIT;
+ return (CMD_RETURN_NORMAL);
+}
+
+/* Show an error message. */
+static enum cmd_retval
+server_client_command_error(struct cmd_q *cmdq, void *data)
+{
+ char *error = data;
+
+ cmdq_error(cmdq, "%s", error);
+ free(error);
+
+ return (CMD_RETURN_NORMAL);
+}
+
/* Handle command message. */
static void
server_client_dispatch_command(struct client *c, struct imsg *imsg)
@@ -1276,7 +1297,7 @@ server_client_dispatch_command(struct client *c, struct imsg *imsg)
argc = data.argc;
if (cmd_unpack_argv(buf, len, argc, &argv) != 0) {
- cmdq_error(c->cmdq, "command too long");
+ cause = xstrdup("command too long");
goto error;
}
@@ -1287,20 +1308,19 @@ server_client_dispatch_command(struct client *c, struct imsg *imsg)
}
if ((cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause)) == NULL) {
- cmdq_error(c->cmdq, "%s", cause);
cmd_free_argv(argc, argv);
goto error;
}
cmd_free_argv(argc, argv);
- if (c != cfg_client || cfg_finished)
- cmdq_run(c->cmdq, cmdlist, NULL);
- else
- cmdq_append(c->cmdq, cmdlist, NULL);
+ cmdq_append(c, cmdq_get_command(cmdlist, NULL, NULL, 0));
+ cmdq_append(c, cmdq_get_callback(server_client_command_done, NULL));
cmd_list_free(cmdlist);
return;
error:
+ cmdq_append(c, cmdq_get_callback(server_client_command_error, cause));
+
if (cmdlist != NULL)
cmd_list_free(cmdlist);