mirror of
				https://github.com/tmux/tmux.git
				synced 2025-11-04 00:56:10 +00:00 
			
		
		
		
	Add a command queue to standardize and simplify commands that call other
commands and allow a command to block execution of subsequent commands. This allows run-shell and if-shell to be synchronous which has been much requested. Each client has a default command queue and commands are consumed one at a time from it. A command may suspend execution from the queue by returning CMD_RETURN_WAIT and then resume it by calling cmd_continue() - for example run-shell does this from the callback that is fired after the job is freed. When the command queue becomes empty, command clients are automatically exited (unless attaching). A callback is also fired - this is used for nested commands in, for example, if-shell which can block execution of the client's cmdq until a new cmdq becomes empty. Also merge all the old error/info/print functions together and lose the old curclient/cmdclient distinction - a cmdq is bound to one client (or none if in the configuration file), this is a command client if c->session is NULL otherwise an attached client.
This commit is contained in:
		@@ -31,7 +31,7 @@
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
enum cmd_retval	 cmd_new_session_check(struct args *);
 | 
			
		||||
enum cmd_retval	 cmd_new_session_exec(struct cmd *, struct cmd_ctx *);
 | 
			
		||||
enum cmd_retval	 cmd_new_session_exec(struct cmd *, struct cmd_q *);
 | 
			
		||||
 | 
			
		||||
const struct cmd_entry cmd_new_session_entry = {
 | 
			
		||||
	"new-session", "new",
 | 
			
		||||
@@ -53,62 +53,52 @@ cmd_new_session_check(struct args *args)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
enum cmd_retval
 | 
			
		||||
cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx)
 | 
			
		||||
cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq)
 | 
			
		||||
{
 | 
			
		||||
	struct args		*args = self->args;
 | 
			
		||||
	struct session		*s, *old_s, *groupwith;
 | 
			
		||||
	struct window		*w;
 | 
			
		||||
	struct environ		 env;
 | 
			
		||||
	struct termios		 tio, *tiop;
 | 
			
		||||
	struct passwd		*pw;
 | 
			
		||||
	const char		*newname, *target, *update, *cwd, *errstr;
 | 
			
		||||
	char			*cmd, *cause;
 | 
			
		||||
	int			 detached, idx;
 | 
			
		||||
	u_int			 sx, sy;
 | 
			
		||||
	struct args	*args = self->args;
 | 
			
		||||
	struct client	*c = cmdq->client;
 | 
			
		||||
	struct session	*s, *groupwith;
 | 
			
		||||
	struct window	*w;
 | 
			
		||||
	struct environ	 env;
 | 
			
		||||
	struct termios	 tio, *tiop;
 | 
			
		||||
	struct passwd	*pw;
 | 
			
		||||
	const char	*newname, *target, *update, *cwd, *errstr;
 | 
			
		||||
	char		*cmd, *cause;
 | 
			
		||||
	int		 detached, idx;
 | 
			
		||||
	u_int		 sx, sy;
 | 
			
		||||
	int		 already_attached;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	newname = args_get(args, 's');
 | 
			
		||||
	if (newname != NULL) {
 | 
			
		||||
		if (!session_check_name(newname)) {
 | 
			
		||||
			ctx->error(ctx, "bad session name: %s", newname);
 | 
			
		||||
			cmdq_error(cmdq, "bad session name: %s", newname);
 | 
			
		||||
			return (CMD_RETURN_ERROR);
 | 
			
		||||
		}
 | 
			
		||||
		if (session_find(newname) != NULL) {
 | 
			
		||||
			ctx->error(ctx, "duplicate session: %s", newname);
 | 
			
		||||
			cmdq_error(cmdq, "duplicate session: %s", newname);
 | 
			
		||||
			return (CMD_RETURN_ERROR);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	target = args_get(args, 't');
 | 
			
		||||
	if (target != NULL) {
 | 
			
		||||
		groupwith = cmd_find_session(ctx, target, 0);
 | 
			
		||||
		groupwith = cmd_find_session(cmdq, target, 0);
 | 
			
		||||
		if (groupwith == NULL)
 | 
			
		||||
			return (CMD_RETURN_ERROR);
 | 
			
		||||
	} else
 | 
			
		||||
		groupwith = NULL;
 | 
			
		||||
 | 
			
		||||
	/*
 | 
			
		||||
	 * There are three cases:
 | 
			
		||||
	 *
 | 
			
		||||
	 * 1. If cmdclient is non-NULL, new-session has been called from the
 | 
			
		||||
	 *    command-line - cmdclient is to become a new attached, interactive
 | 
			
		||||
	 *    client. Unless -d is given, the terminal must be opened and then
 | 
			
		||||
	 *    the client sent MSG_READY.
 | 
			
		||||
	 *
 | 
			
		||||
	 * 2. If cmdclient is NULL, new-session has been called from an
 | 
			
		||||
	 *    existing client (such as a key binding).
 | 
			
		||||
	 *
 | 
			
		||||
	 * 3. Both are NULL, the command was in the configuration file. Treat
 | 
			
		||||
	 *    this as if -d was given even if it was not.
 | 
			
		||||
	 *
 | 
			
		||||
	 * In all cases, a new additional session needs to be created and
 | 
			
		||||
	 * (unless -d) set as the current session for the client.
 | 
			
		||||
	 */
 | 
			
		||||
 | 
			
		||||
	/* Set -d if no client. */
 | 
			
		||||
	detached = args_has(args, 'd');
 | 
			
		||||
	if (ctx->cmdclient == NULL && ctx->curclient == NULL)
 | 
			
		||||
	if (c == NULL)
 | 
			
		||||
		detached = 1;
 | 
			
		||||
 | 
			
		||||
	/* Is this client already attached? */
 | 
			
		||||
	already_attached = 0;
 | 
			
		||||
	if (c != NULL && c->session != NULL)
 | 
			
		||||
		already_attached = 1;
 | 
			
		||||
 | 
			
		||||
	/*
 | 
			
		||||
	 * Save the termios settings, part of which is used for new windows in
 | 
			
		||||
	 * this session.
 | 
			
		||||
@@ -118,25 +108,25 @@ cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx)
 | 
			
		||||
	 * before opening the terminal as that calls tcsetattr() to prepare for
 | 
			
		||||
	 * tmux taking over.
 | 
			
		||||
	 */
 | 
			
		||||
	if (ctx->cmdclient != NULL && ctx->cmdclient->tty.fd != -1) {
 | 
			
		||||
		if (tcgetattr(ctx->cmdclient->tty.fd, &tio) != 0)
 | 
			
		||||
	if (!detached && !already_attached && c->tty.fd != -1) {
 | 
			
		||||
		if (tcgetattr(c->tty.fd, &tio) != 0)
 | 
			
		||||
			fatal("tcgetattr failed");
 | 
			
		||||
		tiop = &tio;
 | 
			
		||||
	} else
 | 
			
		||||
		tiop = NULL;
 | 
			
		||||
 | 
			
		||||
	/* Open the terminal if necessary. */
 | 
			
		||||
	if (!detached && ctx->cmdclient != NULL) {
 | 
			
		||||
		if (server_client_open(ctx->cmdclient, NULL, &cause) != 0) {
 | 
			
		||||
			ctx->error(ctx, "open terminal failed: %s", cause);
 | 
			
		||||
	if (!detached && !already_attached) {
 | 
			
		||||
		if (server_client_open(c, NULL, &cause) != 0) {
 | 
			
		||||
			cmdq_error(cmdq, "open terminal failed: %s", cause);
 | 
			
		||||
			free(cause);
 | 
			
		||||
			return (CMD_RETURN_ERROR);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/* Get the new session working directory. */
 | 
			
		||||
	if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL)
 | 
			
		||||
		cwd = ctx->cmdclient->cwd;
 | 
			
		||||
	if (c != NULL && c->cwd != NULL)
 | 
			
		||||
		cwd = c->cwd;
 | 
			
		||||
	else {
 | 
			
		||||
		pw = getpwuid(getuid());
 | 
			
		||||
		if (pw->pw_dir != NULL && *pw->pw_dir != '\0')
 | 
			
		||||
@@ -146,32 +136,25 @@ cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/* Find new session size. */
 | 
			
		||||
	if (ctx->cmdclient != NULL) {
 | 
			
		||||
		sx = ctx->cmdclient->tty.sx;
 | 
			
		||||
		sy = ctx->cmdclient->tty.sy;
 | 
			
		||||
	} else if (ctx->curclient != NULL) {
 | 
			
		||||
		sx = ctx->curclient->tty.sx;
 | 
			
		||||
		sy = ctx->curclient->tty.sy;
 | 
			
		||||
	if (c != NULL) {
 | 
			
		||||
		sx = c->tty.sx;
 | 
			
		||||
		sy = c->tty.sy;
 | 
			
		||||
	} else {
 | 
			
		||||
		sx = 80;
 | 
			
		||||
		sy = 24;
 | 
			
		||||
	}
 | 
			
		||||
	if (detached) {
 | 
			
		||||
		if (args_has(args, 'x')) {
 | 
			
		||||
			sx = strtonum(
 | 
			
		||||
			    args_get(args, 'x'), 1, USHRT_MAX, &errstr);
 | 
			
		||||
			if (errstr != NULL) {
 | 
			
		||||
				ctx->error(ctx, "width %s", errstr);
 | 
			
		||||
				return (CMD_RETURN_ERROR);
 | 
			
		||||
			}
 | 
			
		||||
	if (detached && args_has(args, 'x')) {
 | 
			
		||||
		sx = strtonum(args_get(args, 'x'), 1, USHRT_MAX, &errstr);
 | 
			
		||||
		if (errstr != NULL) {
 | 
			
		||||
			cmdq_error(cmdq, "width %s", errstr);
 | 
			
		||||
			return (CMD_RETURN_ERROR);
 | 
			
		||||
		}
 | 
			
		||||
		if (args_has(args, 'y')) {
 | 
			
		||||
			sy = strtonum(
 | 
			
		||||
			    args_get(args, 'y'), 1, USHRT_MAX, &errstr);
 | 
			
		||||
			if (errstr != NULL) {
 | 
			
		||||
				ctx->error(ctx, "height %s", errstr);
 | 
			
		||||
				return (CMD_RETURN_ERROR);
 | 
			
		||||
			}
 | 
			
		||||
	}
 | 
			
		||||
	if (detached && args_has(args, 'y')) {
 | 
			
		||||
		sy = strtonum(args_get(args, 'y'), 1, USHRT_MAX, &errstr);
 | 
			
		||||
		if (errstr != NULL) {
 | 
			
		||||
			cmdq_error(cmdq, "height %s", errstr);
 | 
			
		||||
			return (CMD_RETURN_ERROR);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	if (sy > 0 && options_get_number(&global_s_options, "status"))
 | 
			
		||||
@@ -192,14 +175,14 @@ cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx)
 | 
			
		||||
	/* Construct the environment. */
 | 
			
		||||
	environ_init(&env);
 | 
			
		||||
	update = options_get_string(&global_s_options, "update-environment");
 | 
			
		||||
	if (ctx->cmdclient != NULL)
 | 
			
		||||
		environ_update(update, &ctx->cmdclient->environ, &env);
 | 
			
		||||
	if (c != NULL)
 | 
			
		||||
		environ_update(update, &c->environ, &env);
 | 
			
		||||
 | 
			
		||||
	/* Create the new session. */
 | 
			
		||||
	idx = -1 - options_get_number(&global_s_options, "base-index");
 | 
			
		||||
	s = session_create(newname, cmd, cwd, &env, tiop, idx, sx, sy, &cause);
 | 
			
		||||
	if (s == NULL) {
 | 
			
		||||
		ctx->error(ctx, "create session failed: %s", cause);
 | 
			
		||||
		cmdq_error(cmdq, "create session failed: %s", cause);
 | 
			
		||||
		free(cause);
 | 
			
		||||
		return (CMD_RETURN_ERROR);
 | 
			
		||||
	}
 | 
			
		||||
@@ -208,9 +191,7 @@ cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx)
 | 
			
		||||
	/* Set the initial window name if one given. */
 | 
			
		||||
	if (cmd != NULL && args_has(args, 'n')) {
 | 
			
		||||
		w = s->curw->window;
 | 
			
		||||
 | 
			
		||||
		window_set_name(w, args_get(args, 'n'));
 | 
			
		||||
 | 
			
		||||
		options_set_number(&w->options, "automatic-rename", 0);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -229,25 +210,14 @@ cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx)
 | 
			
		||||
	 * taking this session and needs to get MSG_READY and stay around.
 | 
			
		||||
	 */
 | 
			
		||||
	if (!detached) {
 | 
			
		||||
		if (ctx->cmdclient != NULL) {
 | 
			
		||||
			server_write_ready(ctx->cmdclient);
 | 
			
		||||
 | 
			
		||||
			old_s = ctx->cmdclient->session;
 | 
			
		||||
			if (old_s != NULL)
 | 
			
		||||
				ctx->cmdclient->last_session = old_s;
 | 
			
		||||
			ctx->cmdclient->session = s;
 | 
			
		||||
			notify_attached_session_changed(ctx->cmdclient);
 | 
			
		||||
			session_update_activity(s);
 | 
			
		||||
			server_redraw_client(ctx->cmdclient);
 | 
			
		||||
		} else {
 | 
			
		||||
			old_s = ctx->curclient->session;
 | 
			
		||||
			if (old_s != NULL)
 | 
			
		||||
				ctx->curclient->last_session = old_s;
 | 
			
		||||
			ctx->curclient->session = s;
 | 
			
		||||
			notify_attached_session_changed(ctx->curclient);
 | 
			
		||||
			session_update_activity(s);
 | 
			
		||||
			server_redraw_client(ctx->curclient);
 | 
			
		||||
		}
 | 
			
		||||
		if (!already_attached)
 | 
			
		||||
			server_write_ready(c);
 | 
			
		||||
		else if (c->session != NULL)
 | 
			
		||||
			c->last_session = c->session;
 | 
			
		||||
		c->session = s;
 | 
			
		||||
		notify_attached_session_changed(c);
 | 
			
		||||
		session_update_activity(s);
 | 
			
		||||
		server_redraw_client(c);
 | 
			
		||||
	}
 | 
			
		||||
	recalculate_sizes();
 | 
			
		||||
	server_update_socket();
 | 
			
		||||
@@ -257,7 +227,9 @@ cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx)
 | 
			
		||||
	 * session's current window into more mode and display them now.
 | 
			
		||||
	 */
 | 
			
		||||
	if (cfg_finished)
 | 
			
		||||
		show_cfg_causes(s);
 | 
			
		||||
		cfg_show_causes(s);
 | 
			
		||||
 | 
			
		||||
	return (detached ? CMD_RETURN_NORMAL : CMD_RETURN_ATTACH);
 | 
			
		||||
	if (!detached)
 | 
			
		||||
		cmdq->client_exit = 0;
 | 
			
		||||
	return (CMD_RETURN_NORMAL);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user