enum cmd_retval cmd_respawn_pane_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; struct winlink *wl; struct window *w; struct window_pane *wp; struct session *s; struct environ env; const char *cmd, *path; char *cause; u_int idx; struct environ_entry *envent; if ((wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp)) == NULL) return (CMD_RETURN_ERROR); w = wl->window; if (!args_has(self->args, 'k') && wp->fd != -1) { if (window_pane_index(wp, &idx) != 0) fatalx("index not found"); cmdq_error(cmdq, "pane still active: %s:%u.%u", s->name, wl->idx, idx); return (CMD_RETURN_ERROR); } environ_init(&env); environ_copy(&global_environ, &env); environ_copy(&s->environ, &env); server_fill_environ(s, &env); window_pane_reset_mode(wp); screen_reinit(&wp->base); input_init(wp); if (args->argc != 0) cmd = args->argv[0]; else cmd = NULL; path = NULL; if (cmdq->client != NULL && cmdq->client->session == NULL) envent = environ_find(&cmdq->client->environ, "PATH"); else envent = environ_find(&s->environ, "PATH"); if (envent != NULL) path = envent->value; if (window_pane_spawn(wp, cmd, path, NULL, -1, &env, s->tio, &cause) != 0) { cmdq_error(cmdq, "respawn pane failed: %s", cause); free(cause); environ_free(&env); return (CMD_RETURN_ERROR); } wp->flags |= PANE_REDRAW; server_status_window(w); environ_free(&env); return (CMD_RETURN_NORMAL); }
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)
enum cmd_retval cmd_save_buffer_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; struct client *c = cmdq->client; struct session *s; struct paste_buffer *pb; const char *path; char *cause, *start, *end, *msg; size_t size, used, msglen; int cwd, fd, buffer; FILE *f; if (!args_has(args, 'b')) { if ((pb = paste_get_top(&global_buffers)) == NULL) { cmdq_error(cmdq, "no buffers"); return (CMD_RETURN_ERROR); } } else { buffer = args_strtonum(args, 'b', 0, INT_MAX, &cause); if (cause != NULL) { cmdq_error(cmdq, "buffer %s", cause); free(cause); return (CMD_RETURN_ERROR); } pb = paste_get_index(&global_buffers, buffer); if (pb == NULL) { cmdq_error(cmdq, "no buffer %d", buffer); return (CMD_RETURN_ERROR); } } if (self->entry == &cmd_show_buffer_entry) path = "-"; else path = args->argv[0]; if (strcmp(path, "-") == 0) { if (c == NULL) { cmdq_error(cmdq, "can't write to stdout"); return (CMD_RETURN_ERROR); } if (c->session == NULL || (c->flags & CLIENT_CONTROL)) goto do_stdout; goto do_print; } if (c != NULL && c->session == NULL) cwd = c->cwd; else if ((s = cmd_current_session(cmdq, 0)) != NULL) cwd = s->cwd; else cwd = AT_FDCWD; f = NULL; if (args_has(self->args, 'a')) { fd = openat(cwd, path, O_CREAT|O_RDWR|O_APPEND, 0600); if (fd != -1) f = fdopen(fd, "ab"); } else { fd = openat(cwd, path, O_CREAT|O_RDWR, 0600); if (fd != -1) f = fdopen(fd, "wb"); } if (f == NULL) { if (fd != -1) close(fd); cmdq_error(cmdq, "%s: %s", path, strerror(errno)); return (CMD_RETURN_ERROR); } if (fwrite(pb->data, 1, pb->size, f) != pb->size) { cmdq_error(cmdq, "%s: fwrite error", path); fclose(f); return (CMD_RETURN_ERROR); } fclose(f); return (CMD_RETURN_NORMAL); do_stdout: evbuffer_add(c->stdout_data, pb->data, pb->size); server_push_stdout(c); return (CMD_RETURN_NORMAL); do_print: if (pb->size > (INT_MAX / 4) - 1) { cmdq_error(cmdq, "buffer too big"); return (CMD_RETURN_ERROR); } msg = NULL; msglen = 0; used = 0; while (used != pb->size) { start = pb->data + used; end = memchr(start, '\n', pb->size - used); if (end != NULL) size = end - start; else size = pb->size - used; msglen = size * 4 + 1; msg = xrealloc(msg, 1, msglen); strvisx(msg, start, size, VIS_OCTAL|VIS_TAB); cmdq_print(cmdq, "%s", msg); used += size + (end != NULL); } free(msg); return (CMD_RETURN_NORMAL); }
enum cmd_retval cmd_if_shell_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; struct cmd_if_shell_data *cdata; char *shellcmd, *cmd, *cause; struct cmd_list *cmdlist; struct session *s = cmdq->state.tflag.s; struct winlink *wl = cmdq->state.tflag.wl; struct window_pane *wp = cmdq->state.tflag.wp; struct format_tree *ft; const char *cwd; if (cmdq->client != NULL && cmdq->client->session == NULL) cwd = cmdq->client->cwd; else if (s != NULL) cwd = s->cwd; else cwd = NULL; ft = format_create(cmdq, 0); format_defaults(ft, cmdq->state.c, s, wl, wp); shellcmd = format_expand(ft, args->argv[0]); format_free(ft); if (args_has(args, 'F')) { cmd = NULL; if (*shellcmd != '0' && *shellcmd != '\0') cmd = args->argv[1]; else if (args->argc == 3) cmd = args->argv[2]; free(shellcmd); if (cmd == NULL) return (CMD_RETURN_NORMAL); if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) { if (cause != NULL) { cmdq_error(cmdq, "%s", cause); free(cause); } return (CMD_RETURN_ERROR); } cmdq_run(cmdq, cmdlist, &cmdq->item->mouse); cmd_list_free(cmdlist); return (CMD_RETURN_NORMAL); } cdata = xmalloc(sizeof *cdata); cdata->cmd_if = xstrdup(args->argv[1]); if (args->argc == 3) cdata->cmd_else = xstrdup(args->argv[2]); else cdata->cmd_else = NULL; cdata->bflag = args_has(args, 'b'); cdata->cmdq = cmdq; memcpy(&cdata->mouse, &cmdq->item->mouse, sizeof cdata->mouse); cmdq->references++; cdata->references = 1; job_run(shellcmd, s, cwd, cmd_if_shell_callback, cmd_if_shell_free, cdata); free(shellcmd); if (cdata->bflag) return (CMD_RETURN_NORMAL); return (CMD_RETURN_WAIT); }
char * cmd_capture_pane_history(struct args *args, struct cmd_q *cmdq, struct window_pane *wp, size_t *len) { struct grid *gd; const struct grid_line *gl; struct grid_cell *gc = NULL; int n, with_codes, escape_c0, join_lines; u_int i, sx, top, bottom, tmp; char *cause, *buf, *line; size_t linelen; sx = screen_size_x(&wp->base); if (args_has(args, 'a')) { gd = wp->saved_grid; if (gd == NULL) { if (!args_has(args, 'q')) { cmdq_error(cmdq, "no alternate screen"); return (NULL); } return (xstrdup("")); } } else gd = wp->base.grid; n = args_strtonum(args, 'S', INT_MIN, SHRT_MAX, &cause); if (cause != NULL) { top = gd->hsize; free(cause); } else if (n < 0 && (u_int) -n > gd->hsize) top = 0; else top = gd->hsize + n; if (top > gd->hsize + gd->sy - 1) top = gd->hsize + gd->sy - 1; n = args_strtonum(args, 'E', INT_MIN, SHRT_MAX, &cause); if (cause != NULL) { bottom = gd->hsize + gd->sy - 1; free(cause); } else if (n < 0 && (u_int) -n > gd->hsize) bottom = 0; else bottom = gd->hsize + n; if (bottom > gd->hsize + gd->sy - 1) bottom = gd->hsize + gd->sy - 1; if (bottom < top) { tmp = bottom; bottom = top; top = tmp; } with_codes = args_has(args, 'e'); escape_c0 = args_has(args, 'C'); join_lines = args_has(args, 'J'); buf = NULL; for (i = top; i <= bottom; i++) { line = grid_string_cells(gd, 0, i, sx, &gc, with_codes, escape_c0, !join_lines); linelen = strlen(line); buf = cmd_capture_pane_append(buf, len, line, linelen); gl = grid_peek_line(gd, i); if (!join_lines || !(gl->flags & GRID_LINE_WRAPPED)) buf[(*len)++] = '\n'; free(line); } return (buf); }
enum cmd_retval cmd_load_buffer_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; struct client *c = cmdq->client; struct session *s; FILE *f; const char *path; char *pdata, *new_pdata, *cause; size_t psize; u_int limit; int ch, error, buffer, *buffer_ptr, cwd, fd; if (!args_has(args, 'b')) buffer = -1; else { buffer = args_strtonum(args, 'b', 0, INT_MAX, &cause); if (cause != NULL) { cmdq_error(cmdq, "buffer %s", cause); free(cause); return (CMD_RETURN_ERROR); } } path = args->argv[0]; if (strcmp(path, "-") == 0) { buffer_ptr = xmalloc(sizeof *buffer_ptr); *buffer_ptr = buffer; error = server_set_stdin_callback(c, cmd_load_buffer_callback, buffer_ptr, &cause); if (error != 0) { cmdq_error(cmdq, "%s: %s", path, cause); free(cause); return (CMD_RETURN_ERROR); } return (CMD_RETURN_WAIT); } if (c != NULL && c->session == NULL) cwd = c->cwd; else if ((s = cmd_current_session(cmdq, 0)) != NULL) cwd = s->cwd; else cwd = AT_FDCWD; if ((fd = openat(cwd, path, O_RDONLY)) == -1 || (f = fdopen(fd, "rb")) == NULL) { if (fd != -1) close(fd); cmdq_error(cmdq, "%s: %s", path, strerror(errno)); return (CMD_RETURN_ERROR); } pdata = NULL; psize = 0; while ((ch = getc(f)) != EOF) { /* Do not let the server die due to memory exhaustion. */ if ((new_pdata = realloc(pdata, psize + 2)) == NULL) { cmdq_error(cmdq, "realloc error: %s", strerror(errno)); goto error; } pdata = new_pdata; pdata[psize++] = ch; } if (ferror(f)) { cmdq_error(cmdq, "%s: read error", path); goto error; } if (pdata != NULL) pdata[psize] = '\0'; fclose(f); limit = options_get_number(&global_options, "buffer-limit"); if (buffer == -1) { paste_add(&global_buffers, pdata, psize, limit); return (CMD_RETURN_NORMAL); } if (paste_replace(&global_buffers, buffer, pdata, psize) != 0) { cmdq_error(cmdq, "no buffer %d", buffer); free(pdata); return (CMD_RETURN_ERROR); } return (CMD_RETURN_NORMAL); error: free(pdata); if (f != NULL) fclose(f); return (CMD_RETURN_ERROR); }
enum cmd_retval cmd_switch_client_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; struct client *c; struct session *s = NULL; struct winlink *wl = NULL; struct window *w = NULL; struct window_pane *wp = NULL; const char *tflag; if ((c = cmd_find_client(cmdq, args_get(args, 'c'), 0)) == NULL) return (CMD_RETURN_ERROR); if (args_has(args, 'r')) { if (c->flags & CLIENT_READONLY) { c->flags &= ~CLIENT_READONLY; cmdq_info(cmdq, "made client writable"); } else { c->flags |= CLIENT_READONLY; cmdq_info(cmdq, "made client read-only"); } } tflag = args_get(args, 't'); if (args_has(args, 'n')) { if ((s = session_next_session(c->session)) == NULL) { cmdq_error(cmdq, "can't find next session"); return (CMD_RETURN_ERROR); } } else if (args_has(args, 'p')) { if ((s = session_previous_session(c->session)) == NULL) { cmdq_error(cmdq, "can't find previous session"); return (CMD_RETURN_ERROR); } } else if (args_has(args, 'l')) { if (c->last_session != NULL && session_alive(c->last_session)) s = c->last_session; if (s == NULL) { cmdq_error(cmdq, "can't find last session"); return (CMD_RETURN_ERROR); } } else { if (tflag == NULL) { if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL) return (CMD_RETURN_ERROR); } else if (tflag[strcspn(tflag, ":.")] != '\0') { if ((wl = cmd_find_pane(cmdq, tflag, &s, &wp)) == NULL) return (CMD_RETURN_ERROR); } else { if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL) return (CMD_RETURN_ERROR); w = cmd_lookup_windowid(tflag); if (w == NULL && (wp = cmd_lookup_paneid(tflag)) != NULL) w = wp->window; if (w != NULL) wl = winlink_find_by_window(&s->windows, w); } if (cmdq->client == NULL) return (CMD_RETURN_NORMAL); if (wl != NULL) { if (wp != NULL) window_set_active_pane(wp->window, wp); session_set_current(s, wl); } } if (c->session != NULL) c->last_session = c->session; c->session = s; session_update_activity(s); recalculate_sizes(); server_check_unattached(); server_redraw_client(c); s->curw->flags &= ~WINLINK_ALERTFLAGS; return (CMD_RETURN_NORMAL); }
enum cmd_retval cmd_select_window_exec(struct cmd *self, struct cmd_q *cmdq) { 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(cmdq, args_get(args, 't'), 0); if (s == NULL) return (CMD_RETURN_ERROR); 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 { wl = cmd_find_window(cmdq, args_get(args, 't'), &s); if (wl == NULL) return (CMD_RETURN_ERROR); /* * 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); }
enum cmd_retval cmd_save_buffer_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; struct client *c = cmdq->client; struct session *s; struct paste_buffer *pb; const char *path, *bufname, *bufdata, *start, *end, *cwd; const char *flags; char *msg, *file, resolved[PATH_MAX]; size_t size, used, msglen, bufsize; FILE *f; if (!args_has(args, 'b')) { if ((pb = paste_get_top(NULL)) == NULL) { cmdq_error(cmdq, "no buffers"); return (CMD_RETURN_ERROR); } } else { bufname = args_get(args, 'b'); pb = paste_get_name(bufname); if (pb == NULL) { cmdq_error(cmdq, "no buffer %s", bufname); return (CMD_RETURN_ERROR); } } bufdata = paste_buffer_data(pb, &bufsize); if (self->entry == &cmd_show_buffer_entry) path = "-"; else path = args->argv[0]; if (strcmp(path, "-") == 0) { if (c == NULL) { cmdq_error(cmdq, "can't write to stdout"); return (CMD_RETURN_ERROR); } if (c->session == NULL || (c->flags & CLIENT_CONTROL)) goto do_stdout; goto do_print; } if (c != NULL && c->session == NULL) cwd = c->cwd; else if ((s = cmd_find_current(cmdq)) != NULL) cwd = s->cwd; else cwd = "."; flags = "wb"; if (args_has(self->args, 'a')) flags = "ab"; xasprintf(&file, "%s/%s", cwd, path); if (realpath(file, resolved) == NULL) f = NULL; else f = fopen(resolved, flags); free(file); if (f == NULL) { cmdq_error(cmdq, "%s: %s", resolved, strerror(errno)); return (CMD_RETURN_ERROR); } if (fwrite(bufdata, 1, bufsize, f) != bufsize) { cmdq_error(cmdq, "%s: write error", resolved); fclose(f); return (CMD_RETURN_ERROR); } fclose(f); return (CMD_RETURN_NORMAL); do_stdout: evbuffer_add(c->stdout_data, bufdata, bufsize); server_push_stdout(c); return (CMD_RETURN_NORMAL); do_print: if (bufsize > (INT_MAX / 4) - 1) { cmdq_error(cmdq, "buffer too big"); return (CMD_RETURN_ERROR); } msg = NULL; used = 0; while (used != bufsize) { start = bufdata + used; end = memchr(start, '\n', bufsize - used); if (end != NULL) size = end - start; else size = bufsize - used; msglen = size * 4 + 1; msg = xrealloc(msg, msglen); strvisx(msg, start, size, VIS_OCTAL|VIS_TAB); cmdq_print(cmdq, "%s", msg); used += size + (end != NULL); } free(msg); return (CMD_RETURN_NORMAL); }
enum cmd_retval cmd_bind_key_mode_table(struct cmd *self, struct cmd_q *cmdq, key_code key) { struct args *args = self->args; const char *tablename; const struct mode_key_table *mtab; struct mode_key_binding *mbind, mtmp; enum mode_key_cmd cmd; const char *arg; tablename = args_get(args, 't'); if ((mtab = mode_key_findtable(tablename)) == NULL) { cmdq_error(cmdq, "unknown key table: %s", tablename); return (CMD_RETURN_ERROR); } cmd = mode_key_fromstring(mtab->cmdstr, args->argv[1]); if (cmd == MODEKEY_NONE) { cmdq_error(cmdq, "unknown command: %s", args->argv[1]); return (CMD_RETURN_ERROR); } switch (cmd) { case MODEKEYCOPY_APPENDSELECTION: case MODEKEYCOPY_COPYSELECTION: case MODEKEYCOPY_STARTNAMEDBUFFER: if (args->argc == 2) arg = NULL; else { arg = args->argv[2]; if (strcmp(arg, "-x") != 0) { cmdq_error(cmdq, "unknown argument"); return (CMD_RETURN_ERROR); } } break; case MODEKEYCOPY_COPYPIPE: if (args->argc != 3) { cmdq_error(cmdq, "no argument given"); return (CMD_RETURN_ERROR); } arg = args->argv[2]; break; default: if (args->argc != 2) { cmdq_error(cmdq, "no argument allowed"); return (CMD_RETURN_ERROR); } arg = NULL; break; } mtmp.key = key; mtmp.mode = !!args_has(args, 'c'); if ((mbind = RB_FIND(mode_key_tree, mtab->tree, &mtmp)) == NULL) { mbind = xmalloc(sizeof *mbind); mbind->key = mtmp.key; mbind->mode = mtmp.mode; RB_INSERT(mode_key_tree, mtab->tree, mbind); } mbind->cmd = cmd; mbind->arg = arg != NULL ? xstrdup(arg) : NULL; return (CMD_RETURN_NORMAL); }
static enum cmd_retval cmd_show_options_exec(struct cmd *self, struct cmdq_item *item) { struct args *args = self->args; struct cmd_find_state *fs = &item->target; struct client *c = cmd_find_client(item, NULL, 1); struct session *s = item->target.s; struct winlink *wl = item->target.wl; struct options *oo; enum options_table_scope scope; char *argument, *name = NULL, *cause; const char *target; int window, idx, ambiguous; struct options_entry *o; window = (self->entry == &cmd_show_window_options_entry); if (args->argc == 0) { scope = options_scope_from_flags(args, window, fs, &oo, &cause); return (cmd_show_options_all(self, item, oo)); } argument = format_single(item, args->argv[0], c, s, wl, NULL); name = options_match(argument, &idx, &ambiguous); if (name == NULL) { if (args_has(args, 'q')) goto fail; if (ambiguous) cmdq_error(item, "ambiguous option: %s", argument); else cmdq_error(item, "invalid option: %s", argument); goto fail; } if (*name == '@') scope = options_scope_from_flags(args, window, fs, &oo, &cause); else { if (options_get_only(global_options, name) != NULL) scope = OPTIONS_TABLE_SERVER; else if (options_get_only(global_s_options, name) != NULL) scope = OPTIONS_TABLE_SESSION; else if (options_get_only(global_w_options, name) != NULL) scope = OPTIONS_TABLE_WINDOW; else { scope = OPTIONS_TABLE_NONE; xasprintf(&cause, "unknown option: %s", argument); } if (scope == OPTIONS_TABLE_SERVER) oo = global_options; else if (scope == OPTIONS_TABLE_SESSION) { if (args_has(self->args, 'g')) oo = global_s_options; else if (s == NULL) { target = args_get(args, 't'); if (target != NULL) { cmdq_error(item, "no such session: %s", target); } else cmdq_error(item, "no current session"); goto fail; } else oo = s->options; } else if (scope == OPTIONS_TABLE_WINDOW) { if (args_has(self->args, 'g')) oo = global_w_options; else if (wl == NULL) { target = args_get(args, 't'); if (target != NULL) { cmdq_error(item, "no such window: %s", target); } else cmdq_error(item, "no current window"); goto fail; } else oo = wl->window->options; } } if (scope == OPTIONS_TABLE_NONE) { if (args_has(args, 'q')) goto fail; cmdq_error(item, "%s", cause); free(cause); goto fail; } o = options_get_only(oo, name); if (o != NULL) cmd_show_options_print(self, item, o, idx); free(name); free(argument); return (CMD_RETURN_NORMAL); fail: free(name); free(argument); return (CMD_RETURN_ERROR); }
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); }
enum cmd_retval cmd_pipe_pane_exec(struct cmd *self, struct cmd_q *cmdq) { #ifdef TMATE_SLAVE return (CMD_RETURN_ERROR); #else struct args *args = self->args; struct client *c = cmdq->state.c; struct window_pane *wp = cmdq->state.tflag.wp; struct session *s = cmdq->state.tflag.s; struct winlink *wl = cmdq->state.tflag.wl; char *cmd; int old_fd, pipe_fd[2], null_fd; struct format_tree *ft; /* Destroy the old pipe. */ old_fd = wp->pipe_fd; if (wp->pipe_fd != -1) { bufferevent_free(wp->pipe_event); close(wp->pipe_fd); wp->pipe_fd = -1; } /* If no pipe command, that is enough. */ if (args->argc == 0 || *args->argv[0] == '\0') return (CMD_RETURN_NORMAL); /* * With -o, only open the new pipe if there was no previous one. This * allows a pipe to be toggled with a single key, for example: * * bind ^p pipep -o 'cat >>~/output' */ if (args_has(self->args, 'o') && old_fd != -1) return (CMD_RETURN_NORMAL); /* Open the new pipe. */ if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_fd) != 0) { cmdq_error(cmdq, "socketpair error: %s", strerror(errno)); return (CMD_RETURN_ERROR); } /* Expand the command. */ ft = format_create(cmdq, 0); format_defaults(ft, c, s, wl, wp); cmd = format_expand_time(ft, args->argv[0], time(NULL)); format_free(ft); /* Fork the child. */ switch (fork()) { case -1: cmdq_error(cmdq, "fork error: %s", strerror(errno)); free(cmd); return (CMD_RETURN_ERROR); case 0: /* Child process. */ close(pipe_fd[0]); clear_signals(1); if (dup2(pipe_fd[1], STDIN_FILENO) == -1) _exit(1); if (pipe_fd[1] != STDIN_FILENO) close(pipe_fd[1]); null_fd = open(_PATH_DEVNULL, O_WRONLY, 0); if (dup2(null_fd, STDOUT_FILENO) == -1) _exit(1); if (dup2(null_fd, STDERR_FILENO) == -1) _exit(1); if (null_fd != STDOUT_FILENO && null_fd != STDERR_FILENO) close(null_fd); closefrom(STDERR_FILENO + 1); execl(_PATH_BSHELL, "sh", "-c", cmd, (char *) NULL); _exit(1); default: /* Parent process. */ close(pipe_fd[1]); wp->pipe_fd = pipe_fd[0]; wp->pipe_off = EVBUFFER_LENGTH(wp->event->input); wp->pipe_event = bufferevent_new(wp->pipe_fd, NULL, NULL, cmd_pipe_pane_error_callback, wp); bufferevent_enable(wp->pipe_event, EV_WRITE); setblocking(wp->pipe_fd, 0); free(cmd); return (CMD_RETURN_NORMAL); } #endif }
enum cmd_retval cmd_load_buffer_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; struct client *c = cmdq->client; struct session *s; FILE *f; const char *path, *bufname; char *pdata, *new_pdata, *cause; size_t psize; int ch, error, cwd, fd; bufname = NULL; if (args_has(args, 'b')) bufname = args_get(args, 'b'); path = args->argv[0]; if (strcmp(path, "-") == 0) { error = server_set_stdin_callback(c, cmd_load_buffer_callback, __UNCONST(bufname), &cause); if (error != 0) { cmdq_error(cmdq, "%s: %s", path, cause); free(cause); return (CMD_RETURN_ERROR); } return (CMD_RETURN_WAIT); } if (c != NULL && c->session == NULL) cwd = c->cwd; else if ((s = cmd_find_current(cmdq)) != NULL) cwd = s->cwd; else cwd = AT_FDCWD; if ((fd = openat(cwd, path, O_RDONLY)) == -1 || (f = fdopen(fd, "rb")) == NULL) { if (fd != -1) close(fd); cmdq_error(cmdq, "%s: %s", path, strerror(errno)); return (CMD_RETURN_ERROR); } pdata = NULL; psize = 0; while ((ch = getc(f)) != EOF) { /* Do not let the server die due to memory exhaustion. */ if ((new_pdata = realloc(pdata, psize + 2)) == NULL) { cmdq_error(cmdq, "realloc error: %s", strerror(errno)); goto error; } pdata = new_pdata; pdata[psize++] = ch; } if (ferror(f)) { cmdq_error(cmdq, "%s: read error", path); goto error; } if (pdata != NULL) pdata[psize] = '\0'; fclose(f); if (paste_set(pdata, psize, bufname, &cause) != 0) { cmdq_error(cmdq, "%s", cause); free(pdata); free(cause); return (CMD_RETURN_ERROR); } return (CMD_RETURN_NORMAL); error: free(pdata); if (f != NULL) fclose(f); return (CMD_RETURN_ERROR); }
enum cmd_retval cmd_resize_pane_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; struct winlink *wl; struct window *w; const char *errstr; char *cause; struct window_pane *wp; u_int adjust; int x, y; if ((wl = cmd_find_pane(cmdq, args_get(args, 't'), NULL, &wp)) == NULL) return (CMD_RETURN_ERROR); w = wl->window; if (args_has(args, 'Z')) { if (w->flags & WINDOW_ZOOMED) window_unzoom(w); else window_zoom(wp); server_redraw_window(w); server_status_window(w); return (CMD_RETURN_NORMAL); } server_unzoom_window(w); if (args->argc == 0) adjust = 1; else { adjust = strtonum(args->argv[0], 1, INT_MAX, &errstr); if (errstr != NULL) { cmdq_error(cmdq, "adjustment %s", errstr); return (CMD_RETURN_ERROR); } } if (args_has(self->args, 'x')) { x = args_strtonum(self->args, 'x', PANE_MINIMUM, INT_MAX, &cause); if (cause != NULL) { cmdq_error(cmdq, "width %s", cause); free(cause); return (CMD_RETURN_ERROR); } layout_resize_pane_to(wp, LAYOUT_LEFTRIGHT, x); } if (args_has(self->args, 'y')) { y = args_strtonum(self->args, 'y', PANE_MINIMUM, INT_MAX, &cause); if (cause != NULL) { cmdq_error(cmdq, "height %s", cause); free(cause); return (CMD_RETURN_ERROR); } layout_resize_pane_to(wp, LAYOUT_TOPBOTTOM, y); } if (args_has(self->args, 'L')) layout_resize_pane(wp, LAYOUT_LEFTRIGHT, -adjust); else if (args_has(self->args, 'R')) layout_resize_pane(wp, LAYOUT_LEFTRIGHT, adjust); else if (args_has(self->args, 'U')) layout_resize_pane(wp, LAYOUT_TOPBOTTOM, -adjust); else if (args_has(self->args, 'D')) layout_resize_pane(wp, LAYOUT_TOPBOTTOM, adjust); server_redraw_window(wl->window); return (CMD_RETURN_NORMAL); }
enum cmd_retval cmd_attach_session(struct cmd_q *cmdq, const char* tflag, int dflag, int rflag) { struct session *s; struct client *c; const char *update; char *cause; u_int i; if (RB_EMPTY(&sessions)) { cmdq_error(cmdq, "no sessions"); return (CMD_RETURN_ERROR); } if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL) return (CMD_RETURN_ERROR); if (cmdq->client == NULL) return (CMD_RETURN_NORMAL); if (cmdq->client->session != NULL) { if (dflag) { /* * Can't use server_write_session in case attaching to * the same session as currently attached to. */ for (i = 0; i < ARRAY_LENGTH(&clients); i++) { c = ARRAY_ITEM(&clients, i); if (c == NULL || c->session != s) continue; if (c == cmdq->client) continue; server_write_client(c, MSG_DETACH, NULL, 0); } } cmdq->client->session = s; notify_attached_session_changed(cmdq->client); session_update_activity(s); server_redraw_client(cmdq->client); s->curw->flags &= ~WINLINK_ALERTFLAGS; } else { if (server_client_open(cmdq->client, s, &cause) != 0) { cmdq_error(cmdq, "open terminal failed: %s", cause); free(cause); return (CMD_RETURN_ERROR); } if (rflag) cmdq->client->flags |= CLIENT_READONLY; if (dflag) server_write_session(s, MSG_DETACH, NULL, 0); update = options_get_string(&s->options, "update-environment"); environ_update(update, &cmdq->client->environ, &s->environ); cmdq->client->session = s; notify_attached_session_changed(cmdq->client); session_update_activity(s); server_redraw_client(cmdq->client); s->curw->flags &= ~WINLINK_ALERTFLAGS; server_write_ready(cmdq->client); cmdq->client_exit = 0; } recalculate_sizes(); server_update_socket(); return (CMD_RETURN_NORMAL); }
enum cmd_retval cmd_save_buffer_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; struct client *c; struct session *s; struct paste_buffer *pb; const char *path, *newpath, *wd; char *cause, *start, *end; size_t size, used; int buffer; mode_t mask; FILE *f; char *msg; size_t msglen; if (!args_has(args, 'b')) { if ((pb = paste_get_top(&global_buffers)) == NULL) { cmdq_error(cmdq, "no buffers"); return (CMD_RETURN_ERROR); } } else { buffer = args_strtonum(args, 'b', 0, INT_MAX, &cause); if (cause != NULL) { cmdq_error(cmdq, "buffer %s", cause); free(cause); return (CMD_RETURN_ERROR); } pb = paste_get_index(&global_buffers, buffer); if (pb == NULL) { cmdq_error(cmdq, "no buffer %d", buffer); return (CMD_RETURN_ERROR); } } if (self->entry == &cmd_show_buffer_entry) path = "-"; else path = args->argv[0]; if (strcmp(path, "-") == 0) { c = cmdq->client; if (c == NULL) { cmdq_error(cmdq, "can't write to stdout"); return (CMD_RETURN_ERROR); } if (c->session == NULL || (c->flags & CLIENT_CONTROL)) goto do_stdout; goto do_print; } c = cmdq->client; if (c != NULL) wd = c->cwd; else if ((s = cmd_current_session(cmdq, 0)) != NULL) { wd = options_get_string(&s->options, "default-path"); if (*wd == '\0') wd = s->cwd; } else wd = NULL; if (wd != NULL && *wd != '\0') { newpath = get_full_path(wd, path); if (newpath != NULL) path = newpath; } mask = umask(S_IRWXG | S_IRWXO); if (args_has(self->args, 'a')) f = fopen(path, "ab"); else f = fopen(path, "wb"); umask(mask); if (f == NULL) { cmdq_error(cmdq, "%s: %s", path, strerror(errno)); return (CMD_RETURN_ERROR); } if (fwrite(pb->data, 1, pb->size, f) != pb->size) { cmdq_error(cmdq, "%s: fwrite error", path); fclose(f); return (CMD_RETURN_ERROR); } fclose(f); return (CMD_RETURN_NORMAL); do_stdout: evbuffer_add(c->stdout_data, pb->data, pb->size); server_push_stdout(c); return (CMD_RETURN_NORMAL); do_print: if (pb->size > (INT_MAX / 4) - 1) { cmdq_error(cmdq, "buffer too big"); return (CMD_RETURN_ERROR); } msg = NULL; msglen = 0; used = 0; while (used != pb->size) { start = pb->data + used; end = memchr(start, '\n', pb->size - used); if (end != NULL) size = end - start; else size = pb->size - used; msglen = size * 4 + 1; msg = xrealloc(msg, 1, msglen); strvisx(msg, start, size, VIS_OCTAL|VIS_TAB); cmdq_print(cmdq, "%s", msg); used += size + (end != NULL); } free(msg); return (CMD_RETURN_NORMAL); }
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_switch_client_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; struct client *c; struct session *s = NULL; struct winlink *wl = NULL; struct window *w = NULL; struct window_pane *wp = NULL; const char *tflag, *tablename, *update; struct key_table *table; if ((c = cmd_find_client(cmdq, args_get(args, 'c'), 0)) == NULL) return (CMD_RETURN_ERROR); if (args_has(args, 'r')) { if (c->flags & CLIENT_READONLY) c->flags &= ~CLIENT_READONLY; else c->flags |= CLIENT_READONLY; } tablename = args_get(args, 'T'); if (tablename != NULL) { table = key_bindings_get_table(tablename, 0); if (table == NULL) { cmdq_error(cmdq, "table %s doesn't exist", tablename); return (CMD_RETURN_ERROR); } table->references++; key_bindings_unref_table(c->keytable); c->keytable = table; } tflag = args_get(args, 't'); if (args_has(args, 'n')) { if ((s = session_next_session(c->session)) == NULL) { cmdq_error(cmdq, "can't find next session"); return (CMD_RETURN_ERROR); } } else if (args_has(args, 'p')) { if ((s = session_previous_session(c->session)) == NULL) { cmdq_error(cmdq, "can't find previous session"); return (CMD_RETURN_ERROR); } } else if (args_has(args, 'l')) { if (c->last_session != NULL && session_alive(c->last_session)) s = c->last_session; if (s == NULL) { cmdq_error(cmdq, "can't find last session"); return (CMD_RETURN_ERROR); } } else { if (tflag == NULL) { if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL) return (CMD_RETURN_ERROR); } else if (tflag[strcspn(tflag, ":.")] != '\0') { if ((wl = cmd_find_pane(cmdq, tflag, &s, &wp)) == NULL) return (CMD_RETURN_ERROR); } else { if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL) return (CMD_RETURN_ERROR); w = window_find_by_id_str(tflag); if (w == NULL) { wp = window_pane_find_by_id_str(tflag); if (wp != NULL) w = wp->window; } if (w != NULL) wl = winlink_find_by_window(&s->windows, w); } if (cmdq->client == NULL) return (CMD_RETURN_NORMAL); if (wl != NULL) { if (wp != NULL) window_set_active_pane(wp->window, wp); session_set_current(s, wl); } } if (c != NULL && !args_has(args, 'E')) { update = options_get_string(&s->options, "update-environment"); environ_update(update, &c->environ, &s->environ); } if (c->session != NULL) c->last_session = c->session; c->session = s; session_update_activity(s); recalculate_sizes(); server_check_unattached(); server_redraw_client(c); s->curw->flags &= ~WINLINK_ALERTFLAGS; return (CMD_RETURN_NORMAL); }
/* Set user option. */ enum cmd_retval cmd_set_option_user(struct cmd *self, struct cmd_q *cmdq, const char *optstr, const char *valstr) { struct args *args = self->args; struct session *s; struct winlink *wl; struct options *oo; if (args_has(args, 's')) oo = &global_options; else if (args_has(self->args, 'w') || self->entry == &cmd_set_window_option_entry) { if (args_has(self->args, 'g')) oo = &global_w_options; else { wl = cmd_find_window(cmdq, args_get(args, 't'), NULL); if (wl == NULL) return (CMD_RETURN_ERROR); oo = &wl->window->options; } } else { if (args_has(self->args, 'g')) oo = &global_s_options; else { s = cmd_find_session(cmdq, args_get(args, 't'), 0); if (s == NULL) return (CMD_RETURN_ERROR); oo = &s->options; } } if (args_has(args, 'u')) { if (options_find1(oo, optstr) == NULL) { if (!args_has(args, 'q')) { cmdq_error(cmdq, "unknown option: %s", optstr); return (CMD_RETURN_ERROR); } return (CMD_RETURN_NORMAL); } if (valstr != NULL) { cmdq_error(cmdq, "value passed to unset option: %s", optstr); return (CMD_RETURN_ERROR); } options_remove(oo, optstr); } else { if (valstr == NULL) { cmdq_error(cmdq, "empty value"); return (CMD_RETURN_ERROR); } if (args_has(args, 'o') && options_find1(oo, optstr) != NULL) { if (!args_has(args, 'q')) { cmdq_error(cmdq, "already set: %s", optstr); return (CMD_RETURN_ERROR); } return (CMD_RETURN_NORMAL); } options_set_string(oo, optstr, "%s", valstr); } 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_set_option_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; const struct options_table_entry *table, *oe; struct session *s; struct winlink *wl; struct client *c; struct options *oo; struct window *w; const char *optstr, *valstr; u_int i; /* Get the option name and value. */ optstr = args->argv[0]; if (*optstr == '\0') { cmdq_error(cmdq, "invalid option"); return (CMD_RETURN_ERROR); } if (args->argc < 2) valstr = NULL; else valstr = args->argv[1]; /* Is this a user option? */ if (*optstr == '@') return (cmd_set_option_user(self, cmdq, optstr, valstr)); /* Find the option entry, try each table. */ table = oe = NULL; if (options_table_find(optstr, &table, &oe) != 0) { cmdq_error(cmdq, "ambiguous option: %s", optstr); return (CMD_RETURN_ERROR); } if (oe == NULL) { if (!args_has(args, 'q')) { cmdq_error(cmdq, "unknown option: %s", optstr); return (CMD_RETURN_ERROR); } return (CMD_RETURN_NORMAL); } /* Work out the tree from the table. */ if (table == server_options_table) oo = &global_options; else if (table == window_options_table) { if (args_has(self->args, 'g')) oo = &global_w_options; else { wl = cmd_find_window(cmdq, args_get(args, 't'), NULL); if (wl == NULL) { cmdq_error(cmdq, "couldn't set '%s'%s", optstr, (!args_has(args, 't') && !args_has(args, 'g')) ? " need target window or -g" : ""); return (CMD_RETURN_ERROR); } oo = &wl->window->options; } } else if (table == session_options_table) { if (args_has(self->args, 'g')) oo = &global_s_options; else { s = cmd_find_session(cmdq, args_get(args, 't'), 0); if (s == NULL) { cmdq_error(cmdq, "couldn't set '%s'%s", optstr, (!args_has(args, 't') && !args_has(args, 'g')) ? " need target session or -g" : ""); return (CMD_RETURN_ERROR); } oo = &s->options; } } else { cmdq_error(cmdq, "unknown table"); return (CMD_RETURN_ERROR); } /* Unset or set the option. */ if (args_has(args, 'u')) { if (cmd_set_option_unset(self, cmdq, oe, oo, valstr) != 0) return (CMD_RETURN_ERROR); } else { if (args_has(args, 'o') && options_find1(oo, optstr) != NULL) { if (!args_has(args, 'q')) { cmdq_error(cmdq, "already set: %s", optstr); return (CMD_RETURN_ERROR); } return (CMD_RETURN_NORMAL); } if (cmd_set_option_set(self, cmdq, oe, oo, valstr) != 0) return (CMD_RETURN_ERROR); } /* Start or stop timers when automatic-rename changed. */ if (strcmp(oe->name, "automatic-rename") == 0) { for (i = 0; i < ARRAY_LENGTH(&windows); i++) { if ((w = ARRAY_ITEM(&windows, i)) == NULL) continue; if (options_get_number(&w->options, "automatic-rename")) queue_window_name(w); else if (event_initialized(&w->name_timer)) evtimer_del(&w->name_timer); } } /* Update sizes and redraw. May not need it but meh. */ recalculate_sizes(); for (i = 0; i < ARRAY_LENGTH(&clients); i++) { c = ARRAY_ITEM(&clients, i); if (c != NULL && c->session != NULL) server_redraw_client(c); } return (CMD_RETURN_NORMAL); }
enum cmd_retval cmd_paste_buffer_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; struct window_pane *wp = cmdq->state.tflag.wp; struct paste_buffer *pb; const char *sepstr, *bufname, *bufdata, *bufend, *line; size_t seplen, bufsize; int bracket = args_has(args, 'p'); bufname = NULL; if (args_has(args, 'b')) bufname = args_get(args, 'b'); if (bufname == NULL) pb = paste_get_top(NULL); else { pb = paste_get_name(bufname); if (pb == NULL) { cmdq_error(cmdq, "no buffer %s", bufname); return (CMD_RETURN_ERROR); } } if (pb != NULL && ~wp->flags & PANE_INPUTOFF) { sepstr = args_get(args, 's'); if (sepstr == NULL) { if (args_has(args, 'r')) sepstr = "\n"; else sepstr = "\r"; } seplen = strlen(sepstr); if (bracket && (wp->screen->mode & MODE_BRACKETPASTE)) bufferevent_write(wp->event, "\033[200~", 6); bufdata = paste_buffer_data(pb, &bufsize); bufend = bufdata + bufsize; for (;;) { line = memchr(bufdata, '\n', bufend - bufdata); if (line == NULL) break; bufferevent_write(wp->event, bufdata, line - bufdata); bufferevent_write(wp->event, sepstr, seplen); bufdata = line + 1; } if (bufdata != bufend) bufferevent_write(wp->event, bufdata, bufend - bufdata); if (bracket && (wp->screen->mode & MODE_BRACKETPASTE)) bufferevent_write(wp->event, "\033[201~", 6); } if (pb != NULL && args_has(args, 'd')) paste_free(pb); return (CMD_RETURN_NORMAL); }
static enum cmd_retval cmd_select_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->target.wl; struct window *w = wl->window; struct session *s = item->target.s; struct window_pane *wp = item->target.wp, *lastwp, *markedwp; char *pane_title; const char *style; if (self->entry == &cmd_last_pane_entry || args_has(args, 'l')) { lastwp = w->last; if (lastwp == NULL) { cmdq_error(item, "no last pane"); return (CMD_RETURN_ERROR); } if (args_has(self->args, 'e')) lastwp->flags &= ~PANE_INPUTOFF; else if (args_has(self->args, 'd')) lastwp->flags |= PANE_INPUTOFF; else { server_unzoom_window(w); window_redraw_active_switch(w, lastwp); if (window_set_active_pane(w, lastwp)) { cmd_find_from_winlink(current, wl, 0); server_status_window(w); server_redraw_window_borders(w); } } return (CMD_RETURN_NORMAL); } if (args_has(args, 'm') || args_has(args, 'M')) { if (args_has(args, 'm') && !window_pane_visible(wp)) return (CMD_RETURN_NORMAL); lastwp = marked_pane.wp; if (args_has(args, 'M') || server_is_marked(s, wl, wp)) server_clear_marked(); else server_set_marked(s, wl, wp); markedwp = marked_pane.wp; if (lastwp != NULL) { server_redraw_window_borders(lastwp->window); server_status_window(lastwp->window); } if (markedwp != NULL) { server_redraw_window_borders(markedwp->window); server_status_window(markedwp->window); } return (CMD_RETURN_NORMAL); } if (args_has(self->args, 'P') || args_has(self->args, 'g')) { if (args_has(args, 'P')) { style = args_get(args, 'P'); if (style_parse(&grid_default_cell, &wp->colgc, style) == -1) { cmdq_error(item, "bad style: %s", style); return (CMD_RETURN_ERROR); } wp->flags |= PANE_REDRAW; } if (args_has(self->args, 'g')) cmdq_print(item, "%s", style_tostring(&wp->colgc)); return (CMD_RETURN_NORMAL); } if (args_has(self->args, 'L')) { server_unzoom_window(wp->window); wp = window_pane_find_left(wp); } else if (args_has(self->args, 'R')) { server_unzoom_window(wp->window); wp = window_pane_find_right(wp); } else if (args_has(self->args, 'U')) { server_unzoom_window(wp->window); wp = window_pane_find_up(wp); } else if (args_has(self->args, 'D')) { server_unzoom_window(wp->window); wp = window_pane_find_down(wp); } if (wp == NULL) return (CMD_RETURN_NORMAL); if (args_has(self->args, 'e')) { wp->flags &= ~PANE_INPUTOFF; return (CMD_RETURN_NORMAL); } if (args_has(self->args, 'd')) { wp->flags |= PANE_INPUTOFF; return (CMD_RETURN_NORMAL); } if (args_has(self->args, 'T')) { pane_title = format_single(item, args_get(self->args, 'T'), c, s, wl, wp); screen_set_title(&wp->base, pane_title); server_status_window(wp->window); free(pane_title); } if (wp == w->active) return (CMD_RETURN_NORMAL); server_unzoom_window(wp->window); if (!window_pane_visible(wp)) { cmdq_error(item, "pane not visible"); return (CMD_RETURN_ERROR); } window_redraw_active_switch(w, wp); if (window_set_active_pane(w, wp)) { cmd_find_from_winlink_pane(current, wl, wp, 0); hooks_insert(s->hooks, item, current, "after-select-pane"); server_status_window(w); server_redraw_window_borders(w); } return (CMD_RETURN_NORMAL); }
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'); 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); 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 (cmdq->client != NULL && cmdq->client->session == NULL) cwd = cmdq->client->cwd; else cwd = s->cwd; 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 (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)
static enum cmd_retval cmd_select_pane_exec(struct cmd *self, struct cmdq_item *item) { struct args *args = self->args; struct winlink *wl = item->state.tflag.wl; struct window *w = wl->window; struct session *s = item->state.tflag.s; struct window_pane *wp = item->state.tflag.wp, *lastwp, *markedwp; const char *style; if (self->entry == &cmd_last_pane_entry || args_has(args, 'l')) { if (wl->window->last == NULL) { cmdq_error(item, "no last pane"); return (CMD_RETURN_ERROR); } if (args_has(self->args, 'e')) w->last->flags &= ~PANE_INPUTOFF; else if (args_has(self->args, 'd')) w->last->flags |= PANE_INPUTOFF; else { server_unzoom_window(w); window_redraw_active_switch(w, w->last); if (window_set_active_pane(w, w->last)) { server_status_window(w); server_redraw_window_borders(w); } } return (CMD_RETURN_NORMAL); } if (args_has(args, 'm') || args_has(args, 'M')) { if (args_has(args, 'm') && !window_pane_visible(wp)) return (CMD_RETURN_NORMAL); lastwp = marked_pane.wp; if (args_has(args, 'M') || server_is_marked(s, wl, wp)) server_clear_marked(); else server_set_marked(s, wl, wp); markedwp = marked_pane.wp; if (lastwp != NULL) { server_redraw_window_borders(lastwp->window); server_status_window(lastwp->window); } if (markedwp != NULL) { server_redraw_window_borders(markedwp->window); server_status_window(markedwp->window); } return (CMD_RETURN_NORMAL); } if (args_has(self->args, 'P') || args_has(self->args, 'g')) { if (args_has(args, 'P')) { style = args_get(args, 'P'); if (style_parse(&grid_default_cell, &wp->colgc, style) == -1) { cmdq_error(item, "bad style: %s", style); return (CMD_RETURN_ERROR); } wp->flags |= PANE_REDRAW; } if (args_has(self->args, 'g')) cmdq_print(item, "%s", style_tostring(&wp->colgc)); return (CMD_RETURN_NORMAL); } if (args_has(self->args, 'L')) { server_unzoom_window(wp->window); wp = window_pane_find_left(wp); } else if (args_has(self->args, 'R')) { server_unzoom_window(wp->window); wp = window_pane_find_right(wp); } else if (args_has(self->args, 'U')) { server_unzoom_window(wp->window); wp = window_pane_find_up(wp); } else if (args_has(self->args, 'D')) { server_unzoom_window(wp->window); wp = window_pane_find_down(wp); } if (wp == NULL) return (CMD_RETURN_NORMAL); if (args_has(self->args, 'e')) { wp->flags &= ~PANE_INPUTOFF; return (CMD_RETURN_NORMAL); } if (args_has(self->args, 'd')) { wp->flags |= PANE_INPUTOFF; return (CMD_RETURN_NORMAL); } if (wp == w->active) return (CMD_RETURN_NORMAL); server_unzoom_window(wp->window); if (!window_pane_visible(wp)) { cmdq_error(item, "pane not visible"); return (CMD_RETURN_ERROR); } window_redraw_active_switch(w, wp); if (window_set_active_pane(w, wp)) { server_status_window(w); server_redraw_window_borders(w); } return (CMD_RETURN_NORMAL); }
enum cmd_retval cmd_swap_pane_exec(struct cmd *self, struct cmd_q *cmdq) { struct winlink *src_wl, *dst_wl; struct window *src_w, *dst_w; struct window_pane *tmp_wp, *src_wp, *dst_wp; struct layout_cell *src_lc, *dst_lc; u_int sx, sy, xoff, yoff; dst_wl = cmdq->state.tflag.wl; dst_w = dst_wl->window; dst_wp = cmdq->state.tflag.wp; src_wl = cmdq->state.sflag.wl; src_w = src_wl->window; src_wp = cmdq->state.sflag.wp; server_unzoom_window(dst_w); if (args_has(self->args, 'D')) { src_wl = dst_wl; src_w = dst_w; src_wp = TAILQ_NEXT(dst_wp, entry); if (src_wp == NULL) src_wp = TAILQ_FIRST(&dst_w->panes); } else if (args_has(self->args, 'U')) { src_wl = dst_wl; src_w = dst_w; src_wp = TAILQ_PREV(dst_wp, window_panes, entry); if (src_wp == NULL) src_wp = TAILQ_LAST(&dst_w->panes, window_panes); } server_unzoom_window(src_w); #ifdef TMATE if (src_w != dst_w) { cmdq_error(cmdq, "swap pane on different window is not supported with tmate"); return (CMD_RETURN_ERROR); } #endif if (src_wp == dst_wp) return (CMD_RETURN_NORMAL); tmp_wp = TAILQ_PREV(dst_wp, window_panes, entry); TAILQ_REMOVE(&dst_w->panes, dst_wp, entry); TAILQ_REPLACE(&src_w->panes, src_wp, dst_wp, entry); if (tmp_wp == src_wp) tmp_wp = dst_wp; if (tmp_wp == NULL) TAILQ_INSERT_HEAD(&dst_w->panes, src_wp, entry); else TAILQ_INSERT_AFTER(&dst_w->panes, tmp_wp, src_wp, entry); src_lc = src_wp->layout_cell; dst_lc = dst_wp->layout_cell; src_lc->wp = dst_wp; dst_wp->layout_cell = src_lc; dst_lc->wp = src_wp; src_wp->layout_cell = dst_lc; src_wp->window = dst_w; dst_wp->window = src_w; sx = src_wp->sx; sy = src_wp->sy; xoff = src_wp->xoff; yoff = src_wp->yoff; src_wp->xoff = dst_wp->xoff; src_wp->yoff = dst_wp->yoff; window_pane_resize(src_wp, dst_wp->sx, dst_wp->sy); dst_wp->xoff = xoff; dst_wp->yoff = yoff; window_pane_resize(dst_wp, sx, sy); if (!args_has(self->args, 'd')) { if (src_w != dst_w) { window_set_active_pane(src_w, dst_wp); window_set_active_pane(dst_w, src_wp); } else { tmp_wp = dst_wp; if (!window_pane_visible(tmp_wp)) tmp_wp = src_wp; window_set_active_pane(src_w, tmp_wp); } } else { if (src_w->active == src_wp) window_set_active_pane(src_w, dst_wp); if (dst_w->active == dst_wp) window_set_active_pane(dst_w, src_wp); } if (src_w != dst_w) { if (src_w->last == src_wp) src_w->last = NULL; if (dst_w->last == dst_wp) dst_w->last = NULL; } server_redraw_window(src_w); server_redraw_window(dst_w); return (CMD_RETURN_NORMAL); }
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; 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)
enum cmd_retval cmd_load_buffer_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; struct client *c = cmdq->client; struct session *s; FILE *f; const char *path, *bufname, *cwd; char *pdata, *new_pdata, *cause, *file, resolved[PATH_MAX]; size_t psize; int ch, error; bufname = NULL; if (args_has(args, 'b')) bufname = args_get(args, 'b'); path = args->argv[0]; if (strcmp(path, "-") == 0) { error = server_set_stdin_callback(c, cmd_load_buffer_callback, (void *)bufname, &cause); if (error != 0) { cmdq_error(cmdq, "%s: %s", path, cause); free(cause); return (CMD_RETURN_ERROR); } return (CMD_RETURN_WAIT); } if (c != NULL && c->session == NULL) cwd = c->cwd; else if ((s = cmd_find_current(cmdq)) != NULL) cwd = s->cwd; else cwd = "."; xasprintf(&file, "%s/%s", cwd, path); if (realpath(file, resolved) == NULL) f = NULL; else f = fopen(resolved, "rb"); free(file); if (f == NULL) { cmdq_error(cmdq, "%s: %s", resolved, strerror(errno)); return (CMD_RETURN_ERROR); } pdata = NULL; psize = 0; while ((ch = getc(f)) != EOF) { /* Do not let the server die due to memory exhaustion. */ if ((new_pdata = realloc(pdata, psize + 2)) == NULL) { cmdq_error(cmdq, "realloc error: %s", strerror(errno)); goto error; } pdata = new_pdata; pdata[psize++] = ch; } if (ferror(f)) { cmdq_error(cmdq, "%s: read error", resolved); goto error; } if (pdata != NULL) pdata[psize] = '\0'; fclose(f); if (paste_set(pdata, psize, bufname, &cause) != 0) { cmdq_error(cmdq, "%s", cause); free(pdata); free(cause); return (CMD_RETURN_ERROR); } return (CMD_RETURN_NORMAL); error: free(pdata); if (f != NULL) fclose(f); return (CMD_RETURN_ERROR); }
enum cmd_retval cmd_attach_session(struct cmd_q *cmdq, const char *tflag, int dflag, int rflag, const char *cflag) { struct session *s; struct client *c; struct winlink *wl = NULL; struct window *w = NULL; struct window_pane *wp = NULL; const char *update; char *cause; u_int i; int fd; struct format_tree *ft; char *cp; if (RB_EMPTY(&sessions)) { cmdq_error(cmdq, "no sessions"); return (CMD_RETURN_ERROR); } if (tflag == NULL) { if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL) return (CMD_RETURN_ERROR); } else if (tflag[strcspn(tflag, ":.")] != '\0') { if ((wl = cmd_find_pane(cmdq, tflag, &s, &wp)) == NULL) return (CMD_RETURN_ERROR); } else { if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL) return (CMD_RETURN_ERROR); w = cmd_lookup_windowid(tflag); if (w == NULL && (wp = cmd_lookup_paneid(tflag)) != NULL) w = wp->window; if (w != NULL) wl = winlink_find_by_window(&s->windows, w); } if (cmdq->client == NULL) return (CMD_RETURN_NORMAL); if (wl != NULL) { if (wp != NULL) window_set_active_pane(wp->window, wp); session_set_current(s, wl); } if (cmdq->client->session != NULL) { if (dflag) { /* * Can't use server_write_session in case attaching to * the same session as currently attached to. */ for (i = 0; i < ARRAY_LENGTH(&clients); i++) { c = ARRAY_ITEM(&clients, i); if (c == NULL || c->session != s) continue; if (c == cmdq->client) continue; server_write_client(c, MSG_DETACH, c->session->name, strlen(c->session->name) + 1); } } if (cflag != NULL) { ft = format_create(); format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s, NULL, NULL); cp = format_expand(ft, cflag); 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); } close(s->cwd); s->cwd = fd; } cmdq->client->session = s; notify_attached_session_changed(cmdq->client); session_update_activity(s); server_redraw_client(cmdq->client); s->curw->flags &= ~WINLINK_ALERTFLAGS; } else { if (server_client_open(cmdq->client, &cause) != 0) { cmdq_error(cmdq, "open terminal failed: %s", cause); free(cause); return (CMD_RETURN_ERROR); } if (cflag != NULL) { ft = format_create(); format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s, NULL, NULL); cp = format_expand(ft, cflag); 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); } close(s->cwd); s->cwd = fd; } if (rflag) cmdq->client->flags |= CLIENT_READONLY; if (dflag) { server_write_session(s, MSG_DETACH, s->name, strlen(s->name) + 1); } update = options_get_string(&s->options, "update-environment"); environ_update(update, &cmdq->client->environ, &s->environ); cmdq->client->session = s; notify_attached_session_changed(cmdq->client); session_update_activity(s); server_redraw_client(cmdq->client); s->curw->flags &= ~WINLINK_ALERTFLAGS; server_write_ready(cmdq->client); cmdq->client_exit = 0; } recalculate_sizes(); server_update_socket(); return (CMD_RETURN_NORMAL); }