Annotation of src/usr.bin/tmux/cmd-new-session.c, Revision 1.48
1.48 ! nicm 1: /* $OpenBSD: cmd-new-session.c,v 1.47 2012/12/09 23:17:35 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18:
19: #include <sys/types.h>
20:
1.30 nicm 21: #include <pwd.h>
1.35 nicm 22: #include <stdlib.h>
1.11 nicm 23: #include <string.h>
24: #include <termios.h>
1.30 nicm 25: #include <unistd.h>
1.11 nicm 26:
1.1 nicm 27: #include "tmux.h"
28:
29: /*
30: * Create a new session and attach to the current terminal unless -d is given.
31: */
32:
1.45 nicm 33: enum cmd_retval cmd_new_session_check(struct args *);
1.48 ! nicm 34: enum cmd_retval cmd_new_session_exec(struct cmd *, struct cmd_q *);
1.1 nicm 35:
36: const struct cmd_entry cmd_new_session_entry = {
37: "new-session", "new",
1.35 nicm 38: "dn:s:t:x:y:", 0, 1,
1.47 nicm 39: "[-d] [-n window-name] [-s session-name] " CMD_TARGET_SESSION_USAGE
40: " [-x width] [-y height] [command]",
1.34 nicm 41: CMD_STARTSERVER|CMD_CANTNEST|CMD_SENDENVIRON,
42: NULL,
43: cmd_new_session_check,
44: cmd_new_session_exec
1.1 nicm 45: };
46:
1.45 nicm 47: enum cmd_retval
1.34 nicm 48: cmd_new_session_check(struct args *args)
1.1 nicm 49: {
1.34 nicm 50: if (args_has(args, 't') && (args->argc != 0 || args_has(args, 'n')))
1.45 nicm 51: return (CMD_RETURN_ERROR);
52: return (CMD_RETURN_NORMAL);
1.1 nicm 53: }
54:
1.45 nicm 55: enum cmd_retval
1.48 ! nicm 56: cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq)
1.1 nicm 57: {
1.48 ! nicm 58: struct args *args = self->args;
! 59: struct client *c = cmdq->client;
! 60: struct session *s, *groupwith;
! 61: struct window *w;
! 62: struct environ env;
! 63: struct termios tio, *tiop;
! 64: struct passwd *pw;
! 65: const char *newname, *target, *update, *cwd, *errstr;
! 66: char *cmd, *cause;
! 67: int detached, idx;
! 68: u_int sx, sy;
! 69: int already_attached;
! 70:
1.34 nicm 71:
72: newname = args_get(args, 's');
1.38 nicm 73: if (newname != NULL) {
74: if (!session_check_name(newname)) {
1.48 ! nicm 75: cmdq_error(cmdq, "bad session name: %s", newname);
1.45 nicm 76: return (CMD_RETURN_ERROR);
1.38 nicm 77: }
78: if (session_find(newname) != NULL) {
1.48 ! nicm 79: cmdq_error(cmdq, "duplicate session: %s", newname);
1.45 nicm 80: return (CMD_RETURN_ERROR);
1.38 nicm 81: }
1.4 nicm 82: }
1.1 nicm 83:
1.34 nicm 84: target = args_get(args, 't');
85: if (target != NULL) {
1.48 ! nicm 86: groupwith = cmd_find_session(cmdq, target, 0);
1.34 nicm 87: if (groupwith == NULL)
1.45 nicm 88: return (CMD_RETURN_ERROR);
1.34 nicm 89: } else
90: groupwith = NULL;
1.21 nicm 91:
1.6 nicm 92: /* Set -d if no client. */
1.34 nicm 93: detached = args_has(args, 'd');
1.48 ! nicm 94: if (c == NULL)
1.6 nicm 95: detached = 1;
96:
1.48 ! nicm 97: /* Is this client already attached? */
! 98: already_attached = 0;
! 99: if (c != NULL && c->session != NULL)
! 100: already_attached = 1;
! 101:
1.15 nicm 102: /*
1.19 nicm 103: * Save the termios settings, part of which is used for new windows in
104: * this session.
1.15 nicm 105: *
106: * This is read again with tcgetattr() rather than using tty.tio as if
107: * detached, tty_open won't be called. Because of this, it must be done
108: * before opening the terminal as that calls tcsetattr() to prepare for
109: * tmux taking over.
110: */
1.48 ! nicm 111: if (!detached && !already_attached && c->tty.fd != -1) {
! 112: if (tcgetattr(c->tty.fd, &tio) != 0)
1.15 nicm 113: fatal("tcgetattr failed");
1.19 nicm 114: tiop = &tio;
1.15 nicm 115: } else
1.19 nicm 116: tiop = NULL;
1.15 nicm 117:
1.4 nicm 118: /* Open the terminal if necessary. */
1.48 ! nicm 119: if (!detached && !already_attached) {
! 120: if (server_client_open(c, NULL, &cause) != 0) {
! 121: cmdq_error(cmdq, "open terminal failed: %s", cause);
1.44 nicm 122: free(cause);
1.45 nicm 123: return (CMD_RETURN_ERROR);
1.1 nicm 124: }
125: }
126:
1.16 nicm 127: /* Get the new session working directory. */
1.48 ! nicm 128: if (c != NULL && c->cwd != NULL)
! 129: cwd = c->cwd;
1.30 nicm 130: else {
131: pw = getpwuid(getuid());
132: if (pw->pw_dir != NULL && *pw->pw_dir != '\0')
133: cwd = pw->pw_dir;
134: else
135: cwd = "/";
136: }
1.16 nicm 137:
138: /* Find new session size. */
1.48 ! nicm 139: if (c != NULL) {
! 140: sx = c->tty.sx;
! 141: sy = c->tty.sy;
1.39 nicm 142: } else {
1.4 nicm 143: sx = 80;
1.18 nicm 144: sy = 24;
1.39 nicm 145: }
1.48 ! nicm 146: if (detached && args_has(args, 'x')) {
! 147: sx = strtonum(args_get(args, 'x'), 1, USHRT_MAX, &errstr);
! 148: if (errstr != NULL) {
! 149: cmdq_error(cmdq, "width %s", errstr);
! 150: return (CMD_RETURN_ERROR);
1.35 nicm 151: }
1.48 ! nicm 152: }
! 153: if (detached && args_has(args, 'y')) {
! 154: sy = strtonum(args_get(args, 'y'), 1, USHRT_MAX, &errstr);
! 155: if (errstr != NULL) {
! 156: cmdq_error(cmdq, "height %s", errstr);
! 157: return (CMD_RETURN_ERROR);
1.35 nicm 158: }
1.1 nicm 159: }
1.7 nicm 160: if (sy > 0 && options_get_number(&global_s_options, "status"))
1.4 nicm 161: sy--;
162: if (sx == 0)
163: sx = 1;
164: if (sy == 0)
165: sy = 1;
1.16 nicm 166:
167: /* Figure out the command for the new window. */
1.34 nicm 168: if (target != NULL)
1.21 nicm 169: cmd = NULL;
1.34 nicm 170: else if (args->argc != 0)
171: cmd = args->argv[0];
1.1 nicm 172: else
1.7 nicm 173: cmd = options_get_string(&global_s_options, "default-command");
1.1 nicm 174:
1.10 nicm 175: /* Construct the environment. */
176: environ_init(&env);
177: update = options_get_string(&global_s_options, "update-environment");
1.48 ! nicm 178: if (c != NULL)
! 179: environ_update(update, &c->environ, &env);
1.11 nicm 180:
1.4 nicm 181: /* Create the new session. */
1.13 nicm 182: idx = -1 - options_get_number(&global_s_options, "base-index");
1.34 nicm 183: s = session_create(newname, cmd, cwd, &env, tiop, idx, sx, sy, &cause);
1.1 nicm 184: if (s == NULL) {
1.48 ! nicm 185: cmdq_error(cmdq, "create session failed: %s", cause);
1.44 nicm 186: free(cause);
1.45 nicm 187: return (CMD_RETURN_ERROR);
1.1 nicm 188: }
1.10 nicm 189: environ_free(&env);
1.4 nicm 190:
1.16 nicm 191: /* Set the initial window name if one given. */
1.34 nicm 192: if (cmd != NULL && args_has(args, 'n')) {
1.16 nicm 193: w = s->curw->window;
1.40 nicm 194: window_set_name(w, args_get(args, 'n'));
1.16 nicm 195: options_set_number(&w->options, "automatic-rename", 0);
1.21 nicm 196: }
197:
198: /*
199: * If a target session is given, this is to be part of a session group,
200: * so add it to the group and synchronize.
201: */
202: if (groupwith != NULL) {
203: session_group_add(groupwith, s);
204: session_group_synchronize_to(s);
205: session_select(s, RB_ROOT(&s->windows)->idx);
1.1 nicm 206: }
207:
1.16 nicm 208: /*
209: * Set the client to the new session. If a command client exists, it is
210: * taking this session and needs to get MSG_READY and stay around.
1.4 nicm 211: */
1.25 nicm 212: if (!detached) {
1.48 ! nicm 213: if (!already_attached)
! 214: server_write_ready(c);
! 215: else if (c->session != NULL)
! 216: c->last_session = c->session;
! 217: c->session = s;
! 218: notify_attached_session_changed(c);
! 219: session_update_activity(s);
! 220: server_redraw_client(c);
1.1 nicm 221: }
222: recalculate_sizes();
1.22 nicm 223: server_update_socket();
1.26 nicm 224:
225: /*
226: * If there are still configuration file errors to display, put the new
227: * session's current window into more mode and display them now.
228: */
1.46 nicm 229: if (cfg_finished)
1.48 ! nicm 230: cfg_show_causes(s);
1.1 nicm 231:
1.48 ! nicm 232: if (!detached)
! 233: cmdq->client_exit = 0;
! 234: return (CMD_RETURN_NORMAL);
1.1 nicm 235: }