Alter how tmux handles the working directory to internally use file
[tmux-openbsd.git] / cmd-attach-session.c
blob8dcc59976c97b11033d5bb87e97cdd76412f9959
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
27 #include "tmux.h"
30 * Attach existing session to the current terminal.
33 enum cmd_retval cmd_attach_session_exec(struct cmd *, struct cmd_q *);
35 const struct cmd_entry cmd_attach_session_entry = {
36 "attach-session", "attach",
37 "c:drt:", 0, 0,
38 "[-dr] [-c working-directory] " CMD_TARGET_SESSION_USAGE,
39 CMD_CANTNEST|CMD_STARTSERVER,
40 NULL,
41 cmd_attach_session_exec
44 enum cmd_retval
45 cmd_attach_session(struct cmd_q *cmdq, const char *tflag, int dflag, int rflag,
46 const char *cflag)
48 struct session *s;
49 struct client *c;
50 const char *update;
51 char *cause;
52 u_int i;
53 int fd;
54 struct format_tree *ft;
55 char *cp;
57 if (RB_EMPTY(&sessions)) {
58 cmdq_error(cmdq, "no sessions");
59 return (CMD_RETURN_ERROR);
62 if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL)
63 return (CMD_RETURN_ERROR);
65 if (cmdq->client == NULL)
66 return (CMD_RETURN_NORMAL);
68 if (cmdq->client->session != NULL) {
69 if (dflag) {
71 * Can't use server_write_session in case attaching to
72 * the same session as currently attached to.
74 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
75 c = ARRAY_ITEM(&clients, i);
76 if (c == NULL || c->session != s)
77 continue;
78 if (c == cmdq->client)
79 continue;
80 server_write_client(c, MSG_DETACH, NULL, 0);
84 if (cflag != NULL) {
85 ft = format_create();
86 if ((c = cmd_find_client(cmdq, NULL, 1)) != NULL)
87 format_client(ft, c);
88 format_session(ft, s);
89 format_winlink(ft, s, s->curw);
90 format_window_pane(ft, s->curw->window->active);
91 cp = format_expand(ft, cflag);
92 format_free(ft);
94 fd = open(cp, O_RDONLY|O_DIRECTORY);
95 free(cp);
96 if (fd == -1) {
97 cmdq_error(cmdq, "bad working directory: %s",
98 strerror(errno));
99 return (CMD_RETURN_ERROR);
101 close(s->cwd);
102 s->cwd = fd;
105 cmdq->client->session = s;
106 notify_attached_session_changed(cmdq->client);
107 session_update_activity(s);
108 server_redraw_client(cmdq->client);
109 s->curw->flags &= ~WINLINK_ALERTFLAGS;
110 } else {
111 if (server_client_open(cmdq->client, s, &cause) != 0) {
112 cmdq_error(cmdq, "open terminal failed: %s", cause);
113 free(cause);
114 return (CMD_RETURN_ERROR);
117 if (cflag != NULL) {
118 ft = format_create();
119 if ((c = cmd_find_client(cmdq, NULL, 1)) != NULL)
120 format_client(ft, c);
121 format_session(ft, s);
122 format_winlink(ft, s, s->curw);
123 format_window_pane(ft, s->curw->window->active);
124 cp = format_expand(ft, cflag);
125 format_free(ft);
127 fd = open(cp, O_RDONLY|O_DIRECTORY);
128 free(cp);
129 if (fd == -1) {
130 cmdq_error(cmdq, "bad working directory: %s",
131 strerror(errno));
132 return (CMD_RETURN_ERROR);
134 close(s->cwd);
135 s->cwd = fd;
138 if (rflag)
139 cmdq->client->flags |= CLIENT_READONLY;
141 if (dflag)
142 server_write_session(s, MSG_DETACH, NULL, 0);
144 update = options_get_string(&s->options, "update-environment");
145 environ_update(update, &cmdq->client->environ, &s->environ);
147 cmdq->client->session = s;
148 notify_attached_session_changed(cmdq->client);
149 session_update_activity(s);
150 server_redraw_client(cmdq->client);
151 s->curw->flags &= ~WINLINK_ALERTFLAGS;
153 server_write_ready(cmdq->client);
154 cmdq->client_exit = 0;
156 recalculate_sizes();
157 server_update_socket();
159 return (CMD_RETURN_NORMAL);
162 enum cmd_retval
163 cmd_attach_session_exec(struct cmd *self, struct cmd_q *cmdq)
165 struct args *args = self->args;
167 return (cmd_attach_session(cmdq, args_get(args, 't'),
168 args_has(args, 'd'), args_has(args, 'r'), args_get(args, 'c')));