aboutsummaryrefslogtreecommitdiff
path: root/cmd-new-session.c
diff options
context:
space:
mode:
authorThomas <thomas@xteddy.org>2013-10-11 14:33:29 +0100
committerThomas <thomas@xteddy.org>2013-10-11 14:33:29 +0100
commit7f479ffdce8f49f6c57d575b708c4cbe8cbe18df (patch)
treec0b1ddfb340c9bd59bbc02e83f30bf34aeb29a88 /cmd-new-session.c
parentc190c73240576aa2a8274a305844f1fa7fcb0e71 (diff)
parent4901d9ddc8d8c33ecdca363dcb67e66482745fa5 (diff)
downloadrtmux-7f479ffdce8f49f6c57d575b708c4cbe8cbe18df.tar.gz
rtmux-7f479ffdce8f49f6c57d575b708c4cbe8cbe18df.tar.bz2
rtmux-7f479ffdce8f49f6c57d575b708c4cbe8cbe18df.zip
Merge branch 'obsd-master' into mtemp
Diffstat (limited to 'cmd-new-session.c')
-rw-r--r--cmd-new-session.c93
1 files changed, 55 insertions, 38 deletions
diff --git a/cmd-new-session.c b/cmd-new-session.c
index 4eebe632..ad083a44 100644
--- a/cmd-new-session.c
+++ b/cmd-new-session.c
@@ -18,6 +18,8 @@
#include <sys/types.h>
+#include <errno.h>
+#include <fcntl.h>
#include <pwd.h>
#include <stdlib.h>
#include <string.h>
@@ -30,46 +32,39 @@
* Create a new session and attach to the current terminal unless -d is given.
*/
-enum cmd_retval cmd_new_session_check(struct args *);
enum cmd_retval cmd_new_session_exec(struct cmd *, struct cmd_q *);
const struct cmd_entry cmd_new_session_entry = {
"new-session", "new",
- "AdDF:n:Ps:t:x:y:", 0, 1,
- "[-AdDP] [-F format] [-n window-name] [-s session-name] "
- CMD_TARGET_SESSION_USAGE " [-x width] [-y height] [command]",
- CMD_STARTSERVER|CMD_CANTNEST|CMD_SENDENVIRON,
+ "Ac:dDF:n:Ps:t:x:y:", 0, 1,
+ "[-AdDP] [-c start-directory] [-F format] [-n window-name] "
+ "[-s session-name] " CMD_TARGET_SESSION_USAGE " [-x width] [-y height] "
+ "[command]",
+ CMD_STARTSERVER|CMD_CANTNEST,
NULL,
- cmd_new_session_check,
cmd_new_session_exec
};
enum cmd_retval
-cmd_new_session_check(struct args *args)
-{
- if (args_has(args, 't') && (args->argc != 0 || args_has(args, 'n')))
- return (CMD_RETURN_ERROR);
- 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;
+ struct client *c = cmdq->client, *c0;
struct session *s, *groupwith;
struct window *w;
struct environ env;
struct termios tio, *tiop;
- struct passwd *pw;
- const char *newname, *target, *update, *cwd, *errstr;
- const char *template;
+ const char *newname, *target, *update, *errstr, *template;
char *cmd, *cause, *cp;
- int detached, idx;
+ int detached, already_attached, idx, cwd, fd = -1;
u_int sx, sy;
- int already_attached;
struct format_tree *ft;
+ 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)) {
@@ -79,7 +74,7 @@ cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq)
if (session_find(newname) != NULL) {
if (args_has(args, 'A')) {
return (cmd_attach_session(cmdq, newname,
- args_has(args, 'D'), 0));
+ args_has(args, 'D'), 0, NULL));
}
cmdq_error(cmdq, "duplicate session: %s", newname);
return (CMD_RETURN_ERROR);
@@ -104,6 +99,31 @@ cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq)
if (c != NULL && c->session != NULL)
already_attached = 1;
+ /* Get the new session working directory. */
+ if (args_has(args, 'c')) {
+ ft = format_create();
+ if ((c0 = cmd_find_client(cmdq, NULL, 1)) != NULL)
+ format_client(ft, c0);
+ 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 (c != NULL && c->session == NULL)
+ cwd = c->cwd;
+ else if ((c0 = cmd_current_client(cmdq)) != NULL)
+ cwd = c0->session->cwd;
+ else {
+ fd = open(".", O_RDONLY);
+ cwd = fd;
+ }
+
/*
* Save the termios settings, part of which is used for new windows in
* this session.
@@ -125,21 +145,10 @@ cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq)
if (server_client_open(c, NULL, &cause) != 0) {
cmdq_error(cmdq, "open terminal failed: %s", cause);
free(cause);
- return (CMD_RETURN_ERROR);
+ goto error;
}
}
- /* Get the new session working directory. */
- if (c != NULL && c->cwd != NULL)
- cwd = c->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 (c != NULL) {
sx = c->tty.sx;
@@ -152,14 +161,14 @@ cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq)
sx = strtonum(args_get(args, 'x'), 1, USHRT_MAX, &errstr);
if (errstr != NULL) {
cmdq_error(cmdq, "width %s", errstr);
- return (CMD_RETURN_ERROR);
+ 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);
- return (CMD_RETURN_ERROR);
+ goto error;
}
}
if (sy > 0 && options_get_number(&global_s_options, "status"))
@@ -189,7 +198,7 @@ cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq)
if (s == NULL) {
cmdq_error(cmdq, "create session failed: %s", cause);
free(cause);
- return (CMD_RETURN_ERROR);
+ goto error;
}
environ_free(&env);
@@ -240,8 +249,8 @@ cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq)
template = NEW_SESSION_TEMPLATE;
ft = format_create();
- if ((c = cmd_find_client(cmdq, NULL, 1)) != NULL)
- format_client(ft, c);
+ if ((c0 = cmd_find_client(cmdq, NULL, 1)) != NULL)
+ format_client(ft, c0);
format_session(ft, s);
cp = format_expand(ft, template);
@@ -253,5 +262,13 @@ cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq)
if (!detached)
cmdq->client_exit = 0;
+
+ if (fd != -1)
+ close(fd);
return (CMD_RETURN_NORMAL);
+
+error:
+ if (fd != -1)
+ close(fd);
+ return (CMD_RETURN_ERROR);
}