int cmd_swap_window_exec(struct cmd *self, struct cmd_ctx *ctx) { struct cmd_srcdst_data *data = self->data; struct session *src, *dst; struct winlink *wl_src, *wl_dst; struct window *w; if ((wl_src = cmd_find_window(ctx, data->src, &src)) == NULL) return (-1); if ((wl_dst = cmd_find_window(ctx, data->dst, &dst)) == NULL) return (-1); if (wl_dst->window == wl_src->window) return (0); w = wl_dst->window; wl_dst->window = wl_src->window; wl_src->window = w; if (!(data->chflags & CMD_CHFLAG('d'))) { session_select(dst, wl_dst->idx); if (src != dst) session_select(src, wl_src->idx); } server_redraw_session(src); if (src != dst) server_redraw_session(dst); recalculate_sizes(); return (0); }
int cmd_select_prompt_callback(void *data, const char *s) { struct client *c = data; const char *errstr; char msg[128]; u_int idx; if (s == NULL || *s == '\0') return (0); idx = strtonum(s, 0, UINT_MAX, &errstr); if (errstr != NULL) { xsnprintf(msg, sizeof msg, "Index %s: %s", errstr, s); status_message_set(c, "%s", msg); return (0); } if (winlink_find_by_index(&c->session->windows, idx) == NULL) { xsnprintf(msg, sizeof msg, "Window not found: %s:%d", c->session->name, idx); status_message_set(c, "%s", msg); return (0); } if (session_select(c->session, idx) == 0) server_redraw_session(c->session); recalculate_sizes(); return (0); }
int cmd_swap_window_exec(struct cmd *self, struct cmd_ctx *ctx) { struct args *args = self->args; const char *target_src, *target_dst; struct session *src, *dst; struct session_group *sg_src, *sg_dst; struct winlink *wl_src, *wl_dst; struct window *w; target_src = args_get(args, 's'); if ((wl_src = cmd_find_window(ctx, target_src, &src)) == NULL) return (-1); target_dst = args_get(args, 't'); if ((wl_dst = cmd_find_window(ctx, target_dst, &dst)) == NULL) return (-1); sg_src = session_group_find(src); sg_dst = session_group_find(dst); if (src != dst && sg_src != NULL && sg_dst != NULL && sg_src == sg_dst) { ctx->error(ctx, "can't move window, sessions are grouped"); return (-1); } if (wl_dst->window == wl_src->window) return (0); w = wl_dst->window; wl_dst->window = wl_src->window; wl_src->window = w; if (!args_has(self->args, 'd')) { session_select(dst, wl_dst->idx); if (src != dst) session_select(src, wl_src->idx); } session_group_synchronize_from(src); server_redraw_session_group(src); if (src != dst) { session_group_synchronize_from(dst); server_redraw_session_group(dst); } recalculate_sizes(); return (0); }
int cmd_break_pane_exec(struct cmd *self, struct cmd_ctx *ctx) { struct args *args = self->args; struct winlink *wl; struct session *s; struct window_pane *wp; struct window *w; char *name; char *cause; int base_idx; struct client *c; if ((wl = cmd_find_pane(ctx, args_get(args, 't'), &s, &wp)) == NULL) return (-1); if (window_count_panes(wl->window) == 1) { ctx->error(ctx, "can't break with only one pane"); return (-1); } w = wl->window; TAILQ_REMOVE(&w->panes, wp, entry); if (wp == w->active) { w->active = w->last; w->last = NULL; if (w->active == NULL) { w->active = TAILQ_PREV(wp, window_panes, entry); if (w->active == NULL) w->active = TAILQ_NEXT(wp, entry); } } else if (wp == w->last) w->last = NULL; layout_close_pane(wp); w = wp->window = window_create1(s->sx, s->sy); TAILQ_INSERT_HEAD(&w->panes, wp, entry); w->active = wp; name = default_window_name(w); window_set_name(w, name); xfree(name); layout_init(w); base_idx = options_get_number(&s->options, "base-index"); wl = session_attach(s, w, -1 - base_idx, &cause); /* can't fail */ if (!args_has(self->args, 'd')) session_select(s, wl->idx); server_redraw_session(s); server_status_session_group(s); /* Output the window ID for control clients. */ c = cmd_find_client(ctx, NULL); if (c->flags & CLIENT_CONTROL) { ctx->print(ctx, "%d", w->id); } return (0); }
enum cmd_retval cmd_break_pane_exec(struct cmd *self, struct cmd_q *cmdq) { #ifdef TMATE_SLAVE return (CMD_RETURN_ERROR); #else struct args *args = self->args; struct winlink *wl = cmdq->state.sflag.wl; struct session *src_s = cmdq->state.sflag.s; struct session *dst_s = cmdq->state.tflag.s; struct window_pane *wp = cmdq->state.sflag.wp; struct window *w = wl->window; char *name; char *cause; int idx = cmdq->state.tflag.idx; struct format_tree *ft; const char *template; char *cp; if (idx != -1 && winlink_find_by_index(&dst_s->windows, idx) != NULL) { cmdq_error(cmdq, "index %d already in use", idx); return (CMD_RETURN_ERROR); } if (window_count_panes(w) == 1) { cmdq_error(cmdq, "can't break with only one pane"); return (CMD_RETURN_ERROR); } server_unzoom_window(w); TAILQ_REMOVE(&w->panes, wp, entry); window_lost_pane(w, wp); layout_close_pane(wp); w = wp->window = window_create1(dst_s->sx, dst_s->sy); TAILQ_INSERT_HEAD(&w->panes, wp, entry); w->active = wp; name = default_window_name(w); window_set_name(w, name); free(name); layout_init(w, wp); wp->flags |= PANE_CHANGED; if (idx == -1) idx = -1 - options_get_number(dst_s->options, "base-index"); wl = session_attach(dst_s, w, idx, &cause); /* can't fail */ if (!args_has(self->args, 'd')) session_select(dst_s, wl->idx); server_redraw_session(src_s); if (src_s != dst_s) server_redraw_session(dst_s); server_status_session_group(src_s); if (src_s != dst_s) server_status_session_group(dst_s); if (args_has(args, 'P')) { if ((template = args_get(args, 'F')) == NULL)
enum cmd_retval cmd_break_pane_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; struct winlink *wl; struct session *s; struct window_pane *wp; struct window *w; char *name; char *cause; int base_idx; struct client *c; struct format_tree *ft; const char *template; char *cp; if ((wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp)) == NULL) return (CMD_RETURN_ERROR); if (window_count_panes(wl->window) == 1) { cmdq_error(cmdq, "can't break with only one pane"); return (CMD_RETURN_ERROR); } w = wl->window; server_unzoom_window(w); TAILQ_REMOVE(&w->panes, wp, entry); if (wp == w->active) { w->active = w->last; w->last = NULL; if (w->active == NULL) { w->active = TAILQ_PREV(wp, window_panes, entry); if (w->active == NULL) w->active = TAILQ_NEXT(wp, entry); } } else if (wp == w->last) w->last = NULL; layout_close_pane(wp); w = wp->window = window_create1(s->sx, s->sy); TAILQ_INSERT_HEAD(&w->panes, wp, entry); w->active = wp; name = default_window_name(w); window_set_name(w, name); free(name); layout_init(w, wp); base_idx = options_get_number(&s->options, "base-index"); wl = session_attach(s, w, -1 - base_idx, &cause); /* can't fail */ if (!args_has(self->args, 'd')) session_select(s, wl->idx); server_redraw_session(s); server_status_session_group(s); if (args_has(args, 'P')) { if ((template = args_get(args, 'F')) == NULL)
enum cmd_retval cmd_swap_window_exec(struct cmd *self, struct cmd_q *cmdq) { struct session *src, *dst; struct session_group *sg_src, *sg_dst; struct winlink *wl_src, *wl_dst; struct window *w; wl_src = cmdq->state.sflag.wl; src = cmdq->state.sflag.s; sg_src = session_group_find(src); wl_dst = cmdq->state.tflag.wl; dst = cmdq->state.tflag.s; sg_dst = session_group_find(dst); if (src != dst && sg_src != NULL && sg_dst != NULL && sg_src == sg_dst) { cmdq_error(cmdq, "can't move window, sessions are grouped"); return (CMD_RETURN_ERROR); } if (wl_dst->window == wl_src->window) return (CMD_RETURN_NORMAL); w = wl_dst->window; wl_dst->window = wl_src->window; wl_src->window = w; if (!args_has(self->args, 'd')) { session_select(dst, wl_dst->idx); if (src != dst) session_select(src, wl_src->idx); } session_group_synchronize_from(src); server_redraw_session_group(src); if (src != dst) { session_group_synchronize_from(dst); server_redraw_session_group(dst); } recalculate_sizes(); return (CMD_RETURN_NORMAL); }
int cmd_swap_window_exec(struct cmd *self, struct cmd_ctx *ctx) { struct cmd_srcdst_data *data = self->data; struct session *src, *dst; struct session_group *sg_src, *sg_dst; struct winlink *wl_src, *wl_dst; struct window *w; if ((wl_src = cmd_find_window(ctx, data->src, &src)) == NULL) return (-1); if ((wl_dst = cmd_find_window(ctx, data->dst, &dst)) == NULL) return (-1); sg_src = session_group_find(src); sg_dst = session_group_find(dst); if (src != dst && sg_src != NULL && sg_dst != NULL && sg_src == sg_dst) { ctx->error(ctx, "can't move window, sessions are grouped"); return (-1); } if (wl_dst->window == wl_src->window) return (0); w = wl_dst->window; wl_dst->window = wl_src->window; wl_src->window = w; if (!cmd_check_flag(data->chflags, 'd')) { session_select(dst, wl_dst->idx); if (src != dst) session_select(src, wl_src->idx); } session_group_synchronize_from(src); server_redraw_session_group(src); if (src != dst) { session_group_synchronize_from(dst); server_redraw_session_group(dst); } recalculate_sizes(); return (0); }
enum cmd_retval cmd_select_window_exec(struct cmd *self, struct cmd_q *cmdq) { struct winlink *wl = cmdq->state.tflag.wl; struct session *s = cmdq->state.tflag.s; int next, previous, last, activity; next = self->entry == &cmd_next_window_entry; if (args_has(self->args, 'n')) next = 1; previous = self->entry == &cmd_previous_window_entry; if (args_has(self->args, 'p')) previous = 1; last = self->entry == &cmd_last_window_entry; if (args_has(self->args, 'l')) last = 1; if (next || previous || last) { activity = args_has(self->args, 'a'); if (next) { if (session_next(s, activity) != 0) { cmdq_error(cmdq, "no next window"); return (CMD_RETURN_ERROR); } } else if (previous) { if (session_previous(s, activity) != 0) { cmdq_error(cmdq, "no previous window"); return (CMD_RETURN_ERROR); } } else { if (session_last(s) != 0) { cmdq_error(cmdq, "no last window"); return (CMD_RETURN_ERROR); } } server_redraw_session(s); } else { /* * If -T and select-window is invoked on same window as * current, switch to previous window. */ if (args_has(self->args, 'T') && wl == s->curw) { if (session_last(s) != 0) { cmdq_error(cmdq, "no last window"); return (-1); } server_redraw_session(s); } else if (session_select(s, wl->idx) == 0) server_redraw_session(s); } recalculate_sizes(); return (CMD_RETURN_NORMAL); }
int cmd_select_window_exec(struct cmd *self, struct cmd_ctx *ctx) { struct args *args = self->args; struct winlink *wl; struct session *s; int next, previous, last, activity; next = self->entry == &cmd_next_window_entry; if (args_has(self->args, 'n')) next = 1; previous = self->entry == &cmd_previous_window_entry; if (args_has(self->args, 'p')) previous = 1; last = self->entry == &cmd_last_window_entry; if (args_has(self->args, 'l')) last = 1; if (next || previous || last) { s = cmd_find_session(ctx, args_get(args, 't'), 0); if (s == NULL) return (-1); activity = args_has(self->args, 'a'); if (next) { if (session_next(s, activity) != 0) { ctx->error(ctx, "no next window"); return (-1); } } else if (previous) { if (session_previous(s, activity) != 0) { ctx->error(ctx, "no previous window"); return (-1); } } else { if (session_last(s) != 0) { ctx->error(ctx, "no last window"); return (-1); } } server_redraw_session(s); } else { wl = cmd_find_window(ctx, args_get(args, 't'), &s); if (wl == NULL) return (-1); if (session_select(s, wl->idx) == 0) server_redraw_session(s); } recalculate_sizes(); return (0); }
int cmd_split_window_exec(struct cmd *self, struct cmd_ctx *ctx) { struct cmd_split_window_data *data = self->data; struct session *s; struct winlink *wl; struct window *w; struct window_pane *wp, *new_wp = NULL; struct environ env; char *cmd, *cwd, *cause; const char *shell; u_int hlimit; int size; enum layout_type type; struct layout_cell *lc; if ((wl = cmd_find_pane(ctx, data->target, &s, &wp)) == NULL) return (-1); w = wl->window; environ_init(&env); environ_copy(&global_environ, &env); environ_copy(&s->environ, &env); server_fill_environ(s, &env); cmd = data->cmd; if (cmd == NULL) cmd = options_get_string(&s->options, "default-command"); if (ctx->cmdclient == NULL || ctx->cmdclient->cwd == NULL) cwd = options_get_string(&s->options, "default-path"); else cwd = ctx->cmdclient->cwd; type = LAYOUT_TOPBOTTOM; if (data->flag_horizontal) type = LAYOUT_LEFTRIGHT; size = -1; if (data->size != -1) size = data->size; else if (data->percentage != -1) { if (type == LAYOUT_TOPBOTTOM) size = (wp->sy * data->percentage) / 100; else size = (wp->sx * data->percentage) / 100; } hlimit = options_get_number(&s->options, "history-limit"); shell = options_get_string(&s->options, "default-shell"); if (*shell == '\0' || areshell(shell)) shell = _PATH_BSHELL; if ((lc = layout_split_pane(wp, type, size)) == NULL) { cause = xstrdup("pane too small"); goto error; } new_wp = window_add_pane(w, hlimit); if (window_pane_spawn( new_wp, cmd, shell, cwd, &env, s->tio, &cause) != 0) goto error; layout_assign_pane(lc, new_wp); server_redraw_window(w); if (!data->flag_detached) { window_set_active_pane(w, new_wp); session_select(s, wl->idx); server_redraw_session(s); } else server_status_session(s); environ_free(&env); return (0); error: environ_free(&env); if (new_wp != NULL) window_remove_pane(w, new_wp); ctx->error(ctx, "create pane failed: %s", cause); xfree(cause); return (-1); }
int cmd_join_pane_exec(struct cmd *self, struct cmd_ctx *ctx) { struct args *args = self->args; struct session *dst_s; struct winlink *src_wl, *dst_wl; struct window *src_w, *dst_w; struct window_pane *src_wp, *dst_wp; char *cause; int size, percentage, dst_idx; enum layout_type type; struct layout_cell *lc; dst_wl = cmd_find_pane(ctx, args_get(args, 't'), &dst_s, &dst_wp); if (dst_wl == NULL) return (-1); dst_w = dst_wl->window; dst_idx = dst_wl->idx; src_wl = cmd_find_pane(ctx, args_get(args, 's'), NULL, &src_wp); if (src_wl == NULL) return (-1); src_w = src_wl->window; if (src_w == dst_w) { ctx->error(ctx, "can't join a pane to its own window"); return (-1); } type = LAYOUT_TOPBOTTOM; if (args_has(args, 'h')) type = LAYOUT_LEFTRIGHT; size = -1; if (args_has(args, 'l')) { size = args_strtonum(args, 'l', 0, INT_MAX, &cause); if (cause != NULL) { ctx->error(ctx, "size %s", cause); xfree(cause); return (-1); } } else if (args_has(args, 'p')) { percentage = args_strtonum(args, 'p', 0, 100, &cause); if (cause != NULL) { ctx->error(ctx, "percentage %s", cause); xfree(cause); return (-1); } if (type == LAYOUT_TOPBOTTOM) size = (dst_wp->sy * percentage) / 100; else size = (dst_wp->sx * percentage) / 100; } if ((lc = layout_split_pane(dst_wp, type, size)) == NULL) { ctx->error(ctx, "create pane failed: pane too small"); return (-1); } layout_close_pane(src_wp); if (src_w->active == src_wp) { src_w->active = TAILQ_PREV(src_wp, window_panes, entry); if (src_w->active == NULL) src_w->active = TAILQ_NEXT(src_wp, entry); } TAILQ_REMOVE(&src_w->panes, src_wp, entry); if (window_count_panes(src_w) == 0) server_kill_window(src_w); src_wp->window = dst_w; TAILQ_INSERT_AFTER(&dst_w->panes, dst_wp, src_wp, entry); layout_assign_pane(lc, src_wp); recalculate_sizes(); server_redraw_window(src_w); server_redraw_window(dst_w); if (!args_has(args, 'd')) { window_set_active_pane(dst_w, src_wp); session_select(dst_s, dst_idx); server_redraw_session(dst_s); } else server_status_session(dst_s); return (0); }
int cmd_split_window_exec(struct cmd *self, struct cmd_ctx *ctx) { struct args *args = self->args; struct session *s; struct winlink *wl; struct window *w; struct window_pane *wp, *new_wp = NULL; struct environ env; char *cmd, *cwd, *cause; const char *shell; u_int hlimit, paneidx; int size, percentage; enum layout_type type; struct layout_cell *lc; if ((wl = cmd_find_pane(ctx, args_get(args, 't'), &s, &wp)) == NULL) return (-1); w = wl->window; environ_init(&env); environ_copy(&global_environ, &env); environ_copy(&s->environ, &env); server_fill_environ(s, &env); if (args->argc == 0) cmd = options_get_string(&s->options, "default-command"); else cmd = args->argv[0]; cwd = options_get_string(&s->options, "default-path"); if (*cwd == '\0') { if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL) cwd = ctx->cmdclient->cwd; else cwd = s->cwd; } type = LAYOUT_TOPBOTTOM; if (args_has(args, 'h')) type = LAYOUT_LEFTRIGHT; size = -1; if (args_has(args, 's')) { size = args_strtonum(args, 's', 0, INT_MAX, &cause); if (cause != NULL) { ctx->error(ctx, "size %s", cause); xfree(cause); return (-1); } } else if (args_has(args, 'p')) { percentage = args_strtonum(args, 'p', 0, INT_MAX, &cause); if (cause != NULL) { ctx->error(ctx, "percentage %s", cause); xfree(cause); return (-1); } if (type == LAYOUT_TOPBOTTOM) size = (wp->sy * percentage) / 100; else size = (wp->sx * percentage) / 100; } hlimit = options_get_number(&s->options, "history-limit"); shell = options_get_string(&s->options, "default-shell"); if (*shell == '\0' || areshell(shell)) shell = _PATH_BSHELL; if ((lc = layout_split_pane(wp, type, size)) == NULL) { cause = xstrdup("pane too small"); goto error; } new_wp = window_add_pane(w, hlimit); if (window_pane_spawn( new_wp, cmd, shell, cwd, &env, s->tio, &cause) != 0) goto error; layout_assign_pane(lc, new_wp); server_redraw_window(w); if (!args_has(args, 'd')) { window_set_active_pane(w, new_wp); session_select(s, wl->idx); server_redraw_session(s); } else server_status_session(s); environ_free(&env); if (args_has(args, 'P')) { paneidx = window_pane_index(wl->window, new_wp); ctx->print(ctx, "%s:%u.%u", s->name, wl->idx, paneidx); } return (0); error: environ_free(&env); if (new_wp != NULL) window_remove_pane(w, new_wp); ctx->error(ctx, "create pane failed: %s", cause); xfree(cause); return (-1); }
int cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx) { struct args *args = self->args; struct session *s, *old_s, *groupwith; struct window *w; struct window_pane *wp; struct environ env; struct termios tio, *tiop; struct passwd *pw; const char *newname, *target, *update, *cwd; char *overrides, *cmd, *cause; int detached, idx; u_int sx, sy, i; newname = args_get(args, 's'); if (newname != NULL && session_find(newname) != NULL) { ctx->error(ctx, "duplicate session: %s", newname); return (-1); } target = args_get(args, 't'); if (target != NULL) { groupwith = cmd_find_session(ctx, target); if (groupwith == NULL) return (-1); } 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) detached = 1; /* * Save the termios settings, part of which is used for new windows in * this session. * * This is read again with tcgetattr() rather than using tty.tio as if * detached, tty_open won't be called. Because of this, it must be done * 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) fatal("tcgetattr failed"); tiop = &tio; } else tiop = NULL; /* Open the terminal if necessary. */ if (!detached && ctx->cmdclient != NULL) { if (!(ctx->cmdclient->flags & CLIENT_TERMINAL)) { ctx->error(ctx, "not a terminal"); return (-1); } overrides = options_get_string(&global_s_options, "terminal-overrides"); if (tty_open(&ctx->cmdclient->tty, overrides, &cause) != 0) { ctx->error(ctx, "open terminal failed: %s", cause); xfree(cause); return (-1); } } /* Get the new session working directory. */ if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL) cwd = ctx->cmdclient->cwd; else { pw = getpwuid(getuid()); if (pw->pw_dir != NULL && *pw->pw_dir != '\0') cwd = pw->pw_dir; else cwd = "/"; } /* Find new session size. */ if (detached) { sx = 80; sy = 24; } else if (ctx->cmdclient != NULL) { sx = ctx->cmdclient->tty.sx; sy = ctx->cmdclient->tty.sy; } else { sx = ctx->curclient->tty.sx; sy = ctx->curclient->tty.sy; } if (sy > 0 && options_get_number(&global_s_options, "status")) sy--; if (sx == 0) sx = 1; if (sy == 0) sy = 1; /* Figure out the command for the new window. */ if (target != NULL) cmd = NULL; else if (args->argc != 0) cmd = args->argv[0]; else cmd = options_get_string(&global_s_options, "default-command"); /* 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); /* 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); xfree(cause); return (-1); } environ_free(&env); /* Set the initial window name if one given. */ if (cmd != NULL && args_has(args, 'n')) { w = s->curw->window; xfree(w->name); w->name = xstrdup(args_get(args, 'n')); options_set_number(&w->options, "automatic-rename", 0); } /* * If a target session is given, this is to be part of a session group, * so add it to the group and synchronize. */ if (groupwith != NULL) { session_group_add(groupwith, s); session_group_synchronize_to(s); session_select(s, RB_ROOT(&s->windows)->idx); } /* * Set the client to the new session. If a command client exists, it is * taking this session and needs to get MSG_READY and stay around. */ if (!detached) { if (ctx->cmdclient != NULL) { server_write_client(ctx->cmdclient, MSG_READY, NULL, 0); old_s = ctx->cmdclient->session; if (old_s != NULL) ctx->cmdclient->last_session = old_s; ctx->cmdclient->session = s; 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; session_update_activity(s); server_redraw_client(ctx->curclient); } } recalculate_sizes(); server_update_socket(); /* * If there are still configuration file errors to display, put the new * session's current window into more mode and display them now. */ if (cfg_finished && !ARRAY_EMPTY(&cfg_causes)) { wp = s->curw->window->active; window_pane_set_mode(wp, &window_copy_mode); window_copy_init_for_output(wp); for (i = 0; i < ARRAY_LENGTH(&cfg_causes); i++) { cause = ARRAY_ITEM(&cfg_causes, i); window_copy_add(wp, "%s", cause); xfree(cause); } ARRAY_FREE(&cfg_causes); } return (!detached); /* 1 means don't tell command client to exit */ }
enum cmd_retval cmd_split_window_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; struct session *s; struct winlink *wl; struct window *w; struct window_pane *wp, *new_wp = NULL; struct environ env; const char *cmd, *shell, *template; char *cause, *new_cause, *cp; u_int hlimit; int size, percentage, cwd, fd = -1; enum layout_type type; struct layout_cell *lc; struct client *c; struct format_tree *ft; if ((wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp)) == NULL) return (CMD_RETURN_ERROR); w = wl->window; server_unzoom_window(w); environ_init(&env); environ_copy(&global_environ, &env); environ_copy(&s->environ, &env); server_fill_environ(s, &env); if (args->argc == 0) cmd = options_get_string(&s->options, "default-command"); else cmd = args->argv[0]; if (args_has(args, 'c')) { ft = format_create(); if ((c = cmd_find_client(cmdq, NULL, 1)) != NULL) format_client(ft, c); format_session(ft, s); format_winlink(ft, s, s->curw); format_window_pane(ft, s->curw->window->active); cp = format_expand(ft, args_get(args, 'c')); format_free(ft); fd = open(cp, O_RDONLY|O_DIRECTORY); free(cp); if (fd == -1) { cmdq_error(cmdq, "bad working directory: %s", strerror(errno)); return (CMD_RETURN_ERROR); } cwd = fd; } else if (cmdq->client != NULL && cmdq->client->session == NULL) cwd = cmdq->client->cwd; else cwd = s->cwd; type = LAYOUT_TOPBOTTOM; if (args_has(args, 'h')) type = LAYOUT_LEFTRIGHT; size = -1; if (args_has(args, 'l')) { size = args_strtonum(args, 'l', 0, INT_MAX, &cause); if (cause != NULL) { xasprintf(&new_cause, "size %s", cause); free(cause); cause = new_cause; goto error; } } else if (args_has(args, 'p')) { percentage = args_strtonum(args, 'p', 0, INT_MAX, &cause); if (cause != NULL) { xasprintf(&new_cause, "percentage %s", cause); free(cause); cause = new_cause; goto error; } if (type == LAYOUT_TOPBOTTOM) size = (wp->sy * percentage) / 100; else size = (wp->sx * percentage) / 100; } hlimit = options_get_number(&s->options, "history-limit"); shell = options_get_string(&s->options, "default-shell"); if (*shell == '\0' || areshell(shell)) shell = _PATH_BSHELL; if ((lc = layout_split_pane(wp, type, size, 0)) == NULL) { cause = xstrdup("pane too small"); goto error; } new_wp = window_add_pane(w, hlimit); if (window_pane_spawn( new_wp, cmd, shell, cwd, &env, s->tio, &cause) != 0) goto error; layout_assign_pane(lc, new_wp); server_redraw_window(w); if (!args_has(args, 'd')) { window_set_active_pane(w, new_wp); session_select(s, wl->idx); server_redraw_session(s); } else server_status_session(s); environ_free(&env); if (args_has(args, 'P')) { if ((template = args_get(args, 'F')) == NULL)
enum cmd_retval join_pane(struct cmd *self, struct cmd_q *cmdq, int not_same_window) { struct args *args = self->args; struct session *dst_s; struct winlink *src_wl, *dst_wl; struct window *src_w, *dst_w; struct window_pane *src_wp, *dst_wp; char *cause; int size, percentage, dst_idx; enum layout_type type; struct layout_cell *lc; dst_wl = cmd_find_pane(cmdq, args_get(args, 't'), &dst_s, &dst_wp); if (dst_wl == NULL) return (CMD_RETURN_ERROR); dst_w = dst_wl->window; dst_idx = dst_wl->idx; server_unzoom_window(dst_w); src_wl = cmd_find_pane(cmdq, args_get(args, 's'), NULL, &src_wp); if (src_wl == NULL) return (CMD_RETURN_ERROR); src_w = src_wl->window; server_unzoom_window(src_w); if (not_same_window && src_w == dst_w) { cmdq_error(cmdq, "can't join a pane to its own window"); return (CMD_RETURN_ERROR); } if (!not_same_window && src_wp == dst_wp) { cmdq_error(cmdq, "source and target panes must be different"); return (CMD_RETURN_ERROR); } type = LAYOUT_TOPBOTTOM; if (args_has(args, 'h')) type = LAYOUT_LEFTRIGHT; size = -1; if (args_has(args, 'l')) { size = args_strtonum(args, 'l', 0, INT_MAX, &cause); if (cause != NULL) { cmdq_error(cmdq, "size %s", cause); free(cause); return (CMD_RETURN_ERROR); } } else if (args_has(args, 'p')) { percentage = args_strtonum(args, 'p', 0, 100, &cause); if (cause != NULL) { cmdq_error(cmdq, "percentage %s", cause); free(cause); return (CMD_RETURN_ERROR); } if (type == LAYOUT_TOPBOTTOM) size = (dst_wp->sy * percentage) / 100; else size = (dst_wp->sx * percentage) / 100; } lc = layout_split_pane(dst_wp, type, size, args_has(args, 'b')); if (lc == NULL) { cmdq_error(cmdq, "create pane failed: pane too small"); return (CMD_RETURN_ERROR); } layout_close_pane(src_wp); window_lost_pane(src_w, src_wp); TAILQ_REMOVE(&src_w->panes, src_wp, entry); if (window_count_panes(src_w) == 0) server_kill_window(src_w); else notify_window_layout_changed(src_w); src_wp->window = dst_w; TAILQ_INSERT_AFTER(&dst_w->panes, dst_wp, src_wp, entry); layout_assign_pane(lc, src_wp); recalculate_sizes(); server_redraw_window(src_w); server_redraw_window(dst_w); if (!args_has(args, 'd')) { window_set_active_pane(dst_w, src_wp); session_select(dst_s, dst_idx); server_redraw_session(dst_s); } else server_status_session(dst_s); notify_window_layout_changed(dst_w); return (CMD_RETURN_NORMAL); }
enum cmd_retval cmd_split_window_exec(struct cmd *self, struct cmd_ctx *ctx) { struct args *args = self->args; struct session *s; struct winlink *wl; struct window *w; struct window_pane *wp, *new_wp = NULL; struct environ env; const char *cmd, *cwd, *shell; char *cause, *new_cause; u_int hlimit; int size, percentage; enum layout_type type; struct layout_cell *lc; const char *template; struct client *c; struct format_tree *ft; char *cp; if ((wl = cmd_find_pane(ctx, args_get(args, 't'), &s, &wp)) == NULL) return (CMD_RETURN_ERROR); w = wl->window; environ_init(&env); environ_copy(&global_environ, &env); environ_copy(&s->environ, &env); server_fill_environ(s, &env); if (args->argc == 0) cmd = options_get_string(&s->options, "default-command"); else cmd = args->argv[0]; cwd = cmd_get_default_path(ctx, args_get(args, 'c')); type = LAYOUT_TOPBOTTOM; if (args_has(args, 'h')) type = LAYOUT_LEFTRIGHT; size = -1; if (args_has(args, 'l')) { size = args_strtonum(args, 'l', 0, INT_MAX, &cause); if (cause != NULL) { xasprintf(&new_cause, "size %s", cause); free(cause); cause = new_cause; goto error; } } else if (args_has(args, 'p')) { percentage = args_strtonum(args, 'p', 0, INT_MAX, &cause); if (cause != NULL) { xasprintf(&new_cause, "percentage %s", cause); free(cause); cause = new_cause; goto error; } if (type == LAYOUT_TOPBOTTOM) size = (wp->sy * percentage) / 100; else size = (wp->sx * percentage) / 100; } hlimit = options_get_number(&s->options, "history-limit"); shell = options_get_string(&s->options, "default-shell"); if (*shell == '\0' || areshell(shell)) shell = _PATH_BSHELL; if ((lc = layout_split_pane(wp, type, size, 0)) == NULL) { cause = xstrdup("pane too small"); goto error; } new_wp = window_add_pane(w, hlimit); if (window_pane_spawn( new_wp, cmd, shell, cwd, &env, s->tio, &cause) != 0) goto error; layout_assign_pane(lc, new_wp); server_redraw_window(w); if (!args_has(args, 'd')) { window_set_active_pane(w, new_wp); session_select(s, wl->idx); server_redraw_session(s); } else server_status_session(s); environ_free(&env); if (args_has(args, 'P')) { if ((template = args_get(args, 'F')) == NULL)
enum cmd_retval cmd_new_window_exec(struct cmd *self, struct cmd_ctx *ctx) { struct args *args = self->args; struct session *s; struct winlink *wl; struct client *c; const char *cmd, *cwd; const char *template; char *cause; int idx, last, detached; struct format_tree *ft; char *cp; if (args_has(args, 'a')) { wl = cmd_find_window(ctx, args_get(args, 't'), &s); if (wl == NULL) return (CMD_RETURN_ERROR); idx = wl->idx + 1; /* Find the next free index. */ for (last = idx; last < INT_MAX; last++) { if (winlink_find_by_index(&s->windows, last) == NULL) break; } if (last == INT_MAX) { ctx->error(ctx, "no free window indexes"); return (CMD_RETURN_ERROR); } /* Move everything from last - 1 to idx up a bit. */ for (; last > idx; last--) { wl = winlink_find_by_index(&s->windows, last - 1); server_link_window(s, wl, s, last, 0, 0, NULL); server_unlink_window(s, wl); } } else { if ((idx = cmd_find_index(ctx, args_get(args, 't'), &s)) == -2) return (CMD_RETURN_ERROR); } detached = args_has(args, 'd'); wl = NULL; if (idx != -1) wl = winlink_find_by_index(&s->windows, idx); if (wl != NULL && args_has(args, 'k')) { /* * Can't use session_detach as it will destroy session if this * makes it empty. */ notify_window_unlinked(s, wl->window); wl->flags &= ~WINLINK_ALERTFLAGS; winlink_stack_remove(&s->lastw, wl); winlink_remove(&s->windows, wl); /* Force select/redraw if current. */ if (wl == s->curw) { detached = 0; s->curw = NULL; } } if (args->argc == 0) cmd = options_get_string(&s->options, "default-command"); else cmd = args->argv[0]; cwd = cmd_get_default_path(ctx, args_get(args, 'c')); if (idx == -1) idx = -1 - options_get_number(&s->options, "base-index"); wl = session_new(s, args_get(args, 'n'), cmd, cwd, idx, &cause); if (wl == NULL) { ctx->error(ctx, "create window failed: %s", cause); free(cause); return (CMD_RETURN_ERROR); } if (!detached) { session_select(s, wl->idx); server_redraw_session_group(s); } else server_status_session_group(s); if (args_has(args, 'P')) { if ((template = args_get(args, 'F')) == NULL)
int cmd_new_window_exec(struct cmd *self, struct cmd_ctx *ctx) { struct cmd_new_window_data *data = self->data; struct session *s; struct winlink *wl; char *cmd, *cwd, *cause; int idx; if (data == NULL) return (0); if (arg_parse_window(data->target, &s, &idx) != 0) { ctx->error(ctx, "bad window: %s", data->target); return (-1); } if (s == NULL) s = ctx->cursession; if (s == NULL) s = cmd_current_session(ctx); if (s == NULL) { ctx->error(ctx, "session not found: %s", data->target); return (-1); } wl = NULL; if (idx != -1) wl = winlink_find_by_index(&s->windows, idx); if (wl != NULL) { if (data->flag_kill) { /* * Can't use session_detach as it will destroy session * if this makes it empty. */ session_alert_cancel(s, wl); winlink_stack_remove(&s->lastw, wl); winlink_remove(&s->windows, wl); /* Force select/redraw if current. */ if (wl == s->curw) { data->flag_detached = 0; s->curw = NULL; } } } cmd = data->cmd; if (cmd == NULL) cmd = options_get_string(&s->options, "default-command"); if (ctx->cmdclient == NULL || ctx->cmdclient->cwd == NULL) cwd = options_get_string(&global_options, "default-path"); else cwd = ctx->cmdclient->cwd; wl = session_new(s, data->name, cmd, cwd, idx, &cause); if (wl == NULL) { ctx->error(ctx, "create window failed: %s", cause); xfree(cause); return (-1); } if (!data->flag_detached) { session_select(s, wl->idx); server_redraw_session(s); } else server_status_session(s); return (0); }
static enum cmd_retval cmd_new_session_exec(struct cmd *self, struct cmdq_item *item) { struct args *args = self->args; struct client *c = item->client; struct session *s, *as; struct session *groupwith = item->state.tflag.s; struct window *w; struct environ *env; struct termios tio, *tiop; const char *newname, *target, *update, *errstr, *template; const char *path, *cwd, *to_free = NULL; char **argv, *cmd, *cause, *cp; int detached, already_attached, idx, argc; u_int sx, sy; struct format_tree *ft; struct environ_entry *envent; struct cmd_find_state fs; if (self->entry == &cmd_has_session_entry) { /* * cmd_prepare() will fail if the session cannot be found, * hence always return success here. */ return (CMD_RETURN_NORMAL); } if (args_has(args, 't') && (args->argc != 0 || args_has(args, 'n'))) { cmdq_error(item, "command or window name given with target"); return (CMD_RETURN_ERROR); } newname = args_get(args, 's'); if (newname != NULL) { if (!session_check_name(newname)) { cmdq_error(item, "bad session name: %s", newname); return (CMD_RETURN_ERROR); } if ((as = session_find(newname)) != NULL) { if (args_has(args, 'A')) { /* * This item is now destined for * attach-session. Because attach-session will * have already been prepared, copy this * session into its tflag so it can be used. */ cmd_find_from_session(&item->state.tflag, as); return (cmd_attach_session(item, args_has(args, 'D'), 0, NULL, args_has(args, 'E'))); } cmdq_error(item, "duplicate session: %s", newname); return (CMD_RETURN_ERROR); } } if ((target = args_get(args, 't')) != NULL) { if (groupwith == NULL) { cmdq_error(item, "no such session: %s", target); goto error; } } else groupwith = NULL; /* Set -d if no client. */ detached = args_has(args, 'd'); if (c == NULL) detached = 1; /* Is this client already attached? */ already_attached = 0; if (c != NULL && c->session != NULL) already_attached = 1; /* Get the new session working directory. */ if (args_has(args, 'c')) { ft = format_create(item, 0); format_defaults(ft, c, NULL, NULL, NULL); to_free = cwd = format_expand(ft, args_get(args, 'c')); format_free(ft); } else if (c != NULL && c->session == NULL && c->cwd != NULL) cwd = c->cwd; else cwd = "."; /* * If this is a new client, check for nesting and save the termios * settings (part of which is used for new windows in this session). * * tcgetattr() is used rather than using tty.tio since if the client is * detached, tty_open won't be called. It must be done before opening * the terminal as that calls tcsetattr() to prepare for tmux taking * over. */ if (!detached && !already_attached && c->tty.fd != -1) { if (server_client_check_nested(item->client)) { cmdq_error(item, "sessions should be nested with care, " "unset $TMUX to force"); return (CMD_RETURN_ERROR); } if (tcgetattr(c->tty.fd, &tio) != 0) fatal("tcgetattr failed"); tiop = &tio; } else tiop = NULL; /* Open the terminal if necessary. */ if (!detached && !already_attached) { if (server_client_open(c, &cause) != 0) { cmdq_error(item, "open terminal failed: %s", cause); free(cause); goto error; } } /* Find new session size. */ if (c != NULL) { sx = c->tty.sx; sy = c->tty.sy; } else { sx = 80; sy = 24; } if (detached && args_has(args, 'x')) { sx = strtonum(args_get(args, 'x'), 1, USHRT_MAX, &errstr); if (errstr != NULL) { cmdq_error(item, "width %s", errstr); goto error; } } if (detached && args_has(args, 'y')) { sy = strtonum(args_get(args, 'y'), 1, USHRT_MAX, &errstr); if (errstr != NULL) { cmdq_error(item, "height %s", errstr); goto error; } } if (sy > 0 && options_get_number(global_s_options, "status")) sy--; if (sx == 0) sx = 1; if (sy == 0) sy = 1; /* Figure out the command for the new window. */ argc = -1; argv = NULL; if (!args_has(args, 't') && args->argc != 0) { argc = args->argc; argv = args->argv; } else if (groupwith == NULL) { cmd = options_get_string(global_s_options, "default-command"); if (cmd != NULL && *cmd != '\0') { argc = 1; argv = &cmd; } else { argc = 0; argv = NULL; } } path = NULL; if (c != NULL && c->session == NULL) envent = environ_find(c->environ, "PATH"); else envent = environ_find(global_environ, "PATH"); if (envent != NULL) path = envent->value; /* Construct the environment. */ env = environ_create(); if (c != NULL && !args_has(args, 'E')) { update = options_get_string(global_s_options, "update-environment"); environ_update(update, c->environ, env); } /* Create the new session. */ idx = -1 - options_get_number(global_s_options, "base-index"); s = session_create(newname, argc, argv, path, cwd, env, tiop, idx, sx, sy, &cause); environ_free(env); if (s == NULL) { cmdq_error(item, "create session failed: %s", cause); free(cause); goto error; } /* Set the initial window name if one given. */ if (argc >= 0 && args_has(args, 'n')) { w = s->curw->window; window_set_name(w, args_get(args, 'n')); options_set_number(w->options, "automatic-rename", 0); } /* * If a target session is given, this is to be part of a session group, * so add it to the group and synchronize. */ if (groupwith != NULL) { session_group_add(groupwith, s); session_group_synchronize_to(s); session_select(s, RB_MIN(winlinks, &s->windows)->idx); } notify_session("session-created", s); /* * Set the client to the new session. If a command client exists, it is * taking this session and needs to get MSG_READY and stay around. */ if (!detached) { if (!already_attached) { if (~c->flags & CLIENT_CONTROL) proc_send(c->peer, MSG_READY, -1, NULL, 0); } else if (c->session != NULL) c->last_session = c->session; c->session = s; server_client_set_key_table(c, NULL); status_timer_start(c); notify_client("client-session-changed", c); session_update_activity(s, NULL); gettimeofday(&s->last_attached_time, NULL); server_redraw_client(c); } recalculate_sizes(); server_update_socket(); /* * If there are still configuration file errors to display, put the new * session's current window into more mode and display them now. */ if (cfg_finished) cfg_show_causes(s); /* Print if requested. */ if (args_has(args, 'P')) { if ((template = args_get(args, 'F')) == NULL)
static enum cmd_retval cmd_join_pane_exec(struct cmd *self, struct cmdq_item *item) { struct args *args = self->args; struct cmd_find_state *current = &item->shared->current; struct session *dst_s; struct winlink *src_wl, *dst_wl; struct window *src_w, *dst_w; struct window_pane *src_wp, *dst_wp; char *cause; int size, percentage, dst_idx; enum layout_type type; struct layout_cell *lc; int not_same_window, flags; if (self->entry == &cmd_join_pane_entry) not_same_window = 1; else not_same_window = 0; dst_s = item->target.s; dst_wl = item->target.wl; dst_wp = item->target.wp; dst_w = dst_wl->window; dst_idx = dst_wl->idx; server_unzoom_window(dst_w); src_wl = item->source.wl; src_wp = item->source.wp; src_w = src_wl->window; server_unzoom_window(src_w); if (not_same_window && src_w == dst_w) { cmdq_error(item, "can't join a pane to its own window"); return (CMD_RETURN_ERROR); } if (!not_same_window && src_wp == dst_wp) { cmdq_error(item, "source and target panes must be different"); return (CMD_RETURN_ERROR); } type = LAYOUT_TOPBOTTOM; if (args_has(args, 'h')) type = LAYOUT_LEFTRIGHT; size = -1; if (args_has(args, 'l')) { size = args_strtonum(args, 'l', 0, INT_MAX, &cause); if (cause != NULL) { cmdq_error(item, "size %s", cause); free(cause); return (CMD_RETURN_ERROR); } } else if (args_has(args, 'p')) { percentage = args_strtonum(args, 'p', 0, 100, &cause); if (cause != NULL) { cmdq_error(item, "percentage %s", cause); free(cause); return (CMD_RETURN_ERROR); } if (type == LAYOUT_TOPBOTTOM) size = (dst_wp->sy * percentage) / 100; else size = (dst_wp->sx * percentage) / 100; } if (args_has(args, 'b')) flags = SPAWN_BEFORE; else flags = 0; lc = layout_split_pane(dst_wp, type, size, flags); if (lc == NULL) { cmdq_error(item, "create pane failed: pane too small"); return (CMD_RETURN_ERROR); } layout_close_pane(src_wp); window_lost_pane(src_w, src_wp); TAILQ_REMOVE(&src_w->panes, src_wp, entry); src_wp->window = dst_w; TAILQ_INSERT_AFTER(&dst_w->panes, dst_wp, src_wp, entry); layout_assign_pane(lc, src_wp); recalculate_sizes(); server_redraw_window(src_w); server_redraw_window(dst_w); if (!args_has(args, 'd')) { window_set_active_pane(dst_w, src_wp, 1); session_select(dst_s, dst_idx); cmd_find_from_session(current, dst_s, 0); server_redraw_session(dst_s); } else server_status_session(dst_s); if (window_count_panes(src_w) == 0) server_kill_window(src_w); else notify_window("window-layout-changed", src_w); notify_window("window-layout-changed", dst_w); return (CMD_RETURN_NORMAL); }
static enum cmd_retval cmd_break_pane_exec(struct cmd *self, struct cmdq_item *item) { struct args *args = self->args; struct cmd_find_state *current = &item->shared->current; struct client *c = cmd_find_client(item, NULL, 1); struct winlink *wl = item->source.wl; struct session *src_s = item->source.s; struct session *dst_s = item->target.s; struct window_pane *wp = item->source.wp; struct window *w = wl->window; char *name, *cause; int idx = item->target.idx; const char *template; char *cp; if (idx != -1 && winlink_find_by_index(&dst_s->windows, idx) != NULL) { cmdq_error(item, "index %d already in use", idx); return (CMD_RETURN_ERROR); } if (window_count_panes(w) == 1) { cmdq_error(item, "can't break with only one pane"); return (CMD_RETURN_ERROR); } server_unzoom_window(w); TAILQ_REMOVE(&w->panes, wp, entry); window_lost_pane(w, wp); layout_close_pane(wp); w = wp->window = window_create(w->sx, w->sy); TAILQ_INSERT_HEAD(&w->panes, wp, entry); w->active = wp; if (!args_has(args, 'n')) { name = default_window_name(w); window_set_name(w, name); free(name); } else { window_set_name(w, args_get(args, 'n')); options_set_number(w->options, "automatic-rename", 0); } layout_init(w, wp); wp->flags |= PANE_CHANGED; if (idx == -1) idx = -1 - options_get_number(dst_s->options, "base-index"); wl = session_attach(dst_s, w, idx, &cause); /* can't fail */ if (!args_has(self->args, 'd')) { session_select(dst_s, wl->idx); cmd_find_from_session(current, dst_s, 0); } server_redraw_session(src_s); if (src_s != dst_s) server_redraw_session(dst_s); server_status_session_group(src_s); if (src_s != dst_s) server_status_session_group(dst_s); if (args_has(args, 'P')) { if ((template = args_get(args, 'F')) == NULL)
enum cmd_retval cmd_new_window_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; struct session *s; struct winlink *wl; struct client *c; const char *cmd, *template; char *cause, *cp; int idx, last, detached, cwd, fd = -1; struct format_tree *ft; if (args_has(args, 'a')) { wl = cmd_find_window(cmdq, args_get(args, 't'), &s); if (wl == NULL) return (CMD_RETURN_ERROR); idx = wl->idx + 1; /* Find the next free index. */ for (last = idx; last < INT_MAX; last++) { if (winlink_find_by_index(&s->windows, last) == NULL) break; } if (last == INT_MAX) { cmdq_error(cmdq, "no free window indexes"); return (CMD_RETURN_ERROR); } /* Move everything from last - 1 to idx up a bit. */ for (; last > idx; last--) { wl = winlink_find_by_index(&s->windows, last - 1); server_link_window(s, wl, s, last, 0, 0, NULL); server_unlink_window(s, wl); } } else { if ((idx = cmd_find_index(cmdq, args_get(args, 't'), &s)) == -2) return (CMD_RETURN_ERROR); } detached = args_has(args, 'd'); wl = NULL; if (idx != -1) wl = winlink_find_by_index(&s->windows, idx); if (wl != NULL && args_has(args, 'k')) { /* * Can't use session_detach as it will destroy session if this * makes it empty. */ notify_window_unlinked(s, wl->window); wl->flags &= ~WINLINK_ALERTFLAGS; winlink_stack_remove(&s->lastw, wl); winlink_remove(&s->windows, wl); /* Force select/redraw if current. */ if (wl == s->curw) { detached = 0; s->curw = NULL; } } if (args->argc == 0) cmd = options_get_string(&s->options, "default-command"); else cmd = args->argv[0]; if (args_has(args, 'c')) { ft = format_create(); if ((c = cmd_find_client(cmdq, NULL, 1)) != NULL) format_client(ft, c); format_session(ft, s); format_winlink(ft, s, s->curw); format_window_pane(ft, s->curw->window->active); cp = format_expand(ft, args_get(args, 'c')); format_free(ft); fd = open(cp, O_RDONLY|O_DIRECTORY); free(cp); if (fd == -1) { cmdq_error(cmdq, "bad working directory: %s", strerror(errno)); return (CMD_RETURN_ERROR); } cwd = fd; } else if (cmdq->client != NULL && cmdq->client->session == NULL) cwd = cmdq->client->cwd; else cwd = s->cwd; if (idx == -1) idx = -1 - options_get_number(&s->options, "base-index"); wl = session_new(s, args_get(args, 'n'), cmd, cwd, idx, &cause); if (wl == NULL) { cmdq_error(cmdq, "create window failed: %s", cause); free(cause); goto error; } if (!detached) { session_select(s, wl->idx); server_redraw_session_group(s); } else server_status_session_group(s); if (args_has(args, 'P')) { if ((template = args_get(args, 'F')) == NULL)
enum cmd_retval cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; struct client *c = cmdq->client, *c0; struct session *s, *groupwith; struct window *w; struct environ env; struct termios tio, *tiop; const char *newname, *target, *update, *errstr, *template; const char *path; char **argv, *cmd, *cause, *cp; int detached, already_attached, idx, cwd, fd = -1; int argc; u_int sx, sy; struct format_tree *ft; struct environ_entry *envent; if (self->entry == &cmd_has_session_entry) { if (cmd_find_session(cmdq, args_get(args, 't'), 0) == NULL) return (CMD_RETURN_ERROR); return (CMD_RETURN_NORMAL); } if (args_has(args, 't') && (args->argc != 0 || args_has(args, 'n'))) { cmdq_error(cmdq, "command or window name given with target"); return (CMD_RETURN_ERROR); } newname = args_get(args, 's'); if (newname != NULL) { if (!session_check_name(newname)) { cmdq_error(cmdq, "bad session name: %s", newname); return (CMD_RETURN_ERROR); } if (session_find(newname) != NULL) { if (args_has(args, 'A')) { return (cmd_attach_session(cmdq, newname, args_has(args, 'D'), 0, NULL, args_has(args, 'E'))); } cmdq_error(cmdq, "duplicate session: %s", newname); return (CMD_RETURN_ERROR); } } target = args_get(args, 't'); if (target != NULL) { groupwith = cmd_find_session(cmdq, target, 0); if (groupwith == NULL) return (CMD_RETURN_ERROR); } else groupwith = NULL; /* Set -d if no client. */ detached = args_has(args, 'd'); if (c == NULL) detached = 1; /* Is this client already attached? */ already_attached = 0; if (c != NULL && c->session != NULL) already_attached = 1; /* Get the new session working directory. */ if (args_has(args, 'c')) { ft = format_create(); format_defaults(ft, cmd_find_client(cmdq, NULL, 1), NULL, NULL, NULL); cp = format_expand(ft, args_get(args, 'c')); format_free(ft); if (cp != NULL && *cp != '\0') { fd = open(cp, O_RDONLY|O_DIRECTORY); free(cp); if (fd == -1) { cmdq_error(cmdq, "bad working directory: %s", strerror(errno)); return (CMD_RETURN_ERROR); } } else if (cp != NULL) free(cp); cwd = fd; } else if (c != NULL && c->session == NULL) cwd = c->cwd; else if ((c0 = cmd_find_client(cmdq, NULL, 1)) != NULL) cwd = c0->session->cwd; else { fd = open(".", O_RDONLY); cwd = fd; } /* * If this is a new client, check for nesting and save the termios * settings (part of which is used for new windows in this session). * * tcgetattr() is used rather than using tty.tio since if the client is * detached, tty_open won't be called. It must be done before opening * the terminal as that calls tcsetattr() to prepare for tmux taking * over. */ if (!detached && !already_attached && c->tty.fd != -1) { if (server_client_check_nested(cmdq->client)) { cmdq_error(cmdq, "sessions should be nested with care, " "unset $TMUX to force"); return (CMD_RETURN_ERROR); } if (tcgetattr(c->tty.fd, &tio) != 0) fatal("tcgetattr failed"); tiop = &tio; } else tiop = NULL; /* Open the terminal if necessary. */ if (!detached && !already_attached) { if (server_client_open(c, &cause) != 0) { cmdq_error(cmdq, "open terminal failed: %s", cause); free(cause); goto error; } } /* Find new session size. */ if (c != NULL) { sx = c->tty.sx; sy = c->tty.sy; } else { sx = 80; sy = 24; } 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); goto 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); goto error; } } if (sy > 0 && options_get_number(&global_s_options, "status")) sy--; if (sx == 0) sx = 1; if (sy == 0) sy = 1; /* Figure out the command for the new window. */ argc = -1; argv = NULL; if (target == NULL && args->argc != 0) { argc = args->argc; argv = args->argv; } else if (target == NULL) { cmd = options_get_string(&global_s_options, "default-command"); if (cmd != NULL && *cmd != '\0') { argc = 1; argv = &cmd; } else { argc = 0; argv = NULL; } } path = NULL; if (c != NULL && c->session == NULL) envent = environ_find(&c->environ, "PATH"); else envent = environ_find(&global_environ, "PATH"); if (envent != NULL) path = envent->value; /* Construct the environment. */ environ_init(&env); if (c != NULL && !args_has(args, 'E')) { update = options_get_string(&global_s_options, "update-environment"); environ_update(update, &c->environ, &env); } /* Create the new session. */ idx = -1 - options_get_number(&global_s_options, "base-index"); s = session_create(newname, argc, argv, path, cwd, &env, tiop, idx, sx, sy, &cause); if (s == NULL) { cmdq_error(cmdq, "create session failed: %s", cause); free(cause); goto error; } environ_free(&env); /* Set the initial window name if one given. */ if (argc >= 0 && args_has(args, 'n')) { w = s->curw->window; window_set_name(w, args_get(args, 'n')); options_set_number(&w->options, "automatic-rename", 0); } /* * If a target session is given, this is to be part of a session group, * so add it to the group and synchronize. */ if (groupwith != NULL) { session_group_add(groupwith, s); session_group_synchronize_to(s); session_select(s, RB_MIN(winlinks, &s->windows)->idx); } /* * Set the client to the new session. If a command client exists, it is * taking this session and needs to get MSG_READY and stay around. */ if (!detached) { if (!already_attached) server_write_ready(c); else if (c->session != NULL) c->last_session = c->session; c->session = s; status_timer_start(c); notify_attached_session_changed(c); session_update_activity(s); server_redraw_client(c); } recalculate_sizes(); server_update_socket(); /* * If there are still configuration file errors to display, put the new * session's current window into more mode and display them now. */ if (cfg_finished) cfg_show_causes(s); /* Print if requested. */ if (args_has(args, 'P')) { if ((template = args_get(args, 'F')) == NULL)
int cmd_new_window_exec(struct cmd *self, struct cmd_ctx *ctx) { struct cmd_new_window_data *data = self->data; struct session *s; struct winlink *wl; char *cmd, *cwd, *cause; int idx, last; if (data == NULL) return (0); if (data->flag_insert_after) { if ((wl = cmd_find_window(ctx, data->target, &s)) == NULL) return (-1); idx = wl->idx + 1; /* Find the next free index. */ for (last = idx; last < INT_MAX; last++) { if (winlink_find_by_index(&s->windows, last) == NULL) break; } if (last == INT_MAX) { ctx->error(ctx, "no free window indexes"); return (-1); } /* Move everything from last - 1 to idx up a bit. */ for (; last > idx; last--) { wl = winlink_find_by_index(&s->windows, last - 1); server_link_window(s, wl, s, last, 0, 0, NULL); server_unlink_window(s, wl); } } else { if ((idx = cmd_find_index(ctx, data->target, &s)) == -2) return (-1); } wl = NULL; if (idx != -1) wl = winlink_find_by_index(&s->windows, idx); if (wl != NULL && data->flag_kill) { /* * Can't use session_detach as it will destroy session if this * makes it empty. */ wl->flags &= ~WINLINK_ALERTFLAGS; winlink_stack_remove(&s->lastw, wl); winlink_remove(&s->windows, wl); /* Force select/redraw if current. */ if (wl == s->curw) { data->flag_detached = 0; s->curw = NULL; } } cmd = data->cmd; if (cmd == NULL) cmd = options_get_string(&s->options, "default-command"); cwd = options_get_string(&s->options, "default-path"); if (*cwd == '\0') { if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL) cwd = ctx->cmdclient->cwd; else cwd = s->cwd; } if (idx == -1) idx = -1 - options_get_number(&s->options, "base-index"); wl = session_new(s, data->name, cmd, cwd, idx, &cause); if (wl == NULL) { ctx->error(ctx, "create window failed: %s", cause); xfree(cause); return (-1); } if (!data->flag_detached) { session_select(s, wl->idx); server_redraw_session_group(s); } else server_status_session_group(s); if (data->flag_print) ctx->print(ctx, "%s:%u", s->name, wl->idx); return (0); }