cmd-new-session.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/types.h>
  18. #include <errno.h>
  19. #include <fcntl.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <termios.h>
  23. #include <unistd.h>
  24. #include "tmux.h"
  25. /*
  26. * Create a new session and attach to the current terminal unless -d is given.
  27. */
  28. #define NEW_SESSION_TEMPLATE "#{session_name}:"
  29. enum cmd_retval cmd_new_session_exec(struct cmd *, struct cmd_q *);
  30. const struct cmd_entry cmd_new_session_entry = {
  31. .name = "new-session",
  32. .alias = "new",
  33. .args = { "Ac:dDEF:n:Ps:t:x:y:", 0, -1 },
  34. .usage = "[-AdDEP] [-c start-directory] [-F format] [-n window-name] "
  35. "[-s session-name] " CMD_TARGET_SESSION_USAGE " [-x width] "
  36. "[-y height] [command]",
  37. .tflag = CMD_SESSION_CANFAIL,
  38. .flags = CMD_STARTSERVER,
  39. .exec = cmd_new_session_exec
  40. };
  41. const struct cmd_entry cmd_has_session_entry = {
  42. .name = "has-session",
  43. .alias = "has",
  44. .args = { "t:", 0, 0 },
  45. .usage = CMD_TARGET_SESSION_USAGE,
  46. .tflag = CMD_SESSION,
  47. .flags = 0,
  48. .exec = cmd_new_session_exec
  49. };
  50. enum cmd_retval
  51. cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq)
  52. {
  53. struct args *args = self->args;
  54. struct client *c = cmdq->client;
  55. struct session *s, *as;
  56. struct session *groupwith = cmdq->state.tflag.s;
  57. struct window *w;
  58. struct environ *env;
  59. struct termios tio, *tiop;
  60. const char *newname, *target, *update, *errstr, *template;
  61. const char *path, *cwd, *to_free = NULL;
  62. char **argv, *cmd, *cause, *cp;
  63. int detached, already_attached, idx, argc;
  64. u_int sx, sy;
  65. struct format_tree *ft;
  66. struct environ_entry *envent;
  67. if (self->entry == &cmd_has_session_entry) {
  68. /*
  69. * cmd_prepare() will fail if the session cannot be found,
  70. * hence always return success here.
  71. */
  72. return (CMD_RETURN_NORMAL);
  73. }
  74. if (args_has(args, 't') && (args->argc != 0 || args_has(args, 'n'))) {
  75. cmdq_error(cmdq, "command or window name given with target");
  76. return (CMD_RETURN_ERROR);
  77. }
  78. newname = args_get(args, 's');
  79. if (newname != NULL) {
  80. if (!session_check_name(newname)) {
  81. cmdq_error(cmdq, "bad session name: %s", newname);
  82. return (CMD_RETURN_ERROR);
  83. }
  84. if ((as = session_find(newname)) != NULL) {
  85. if (args_has(args, 'A')) {
  86. /*
  87. * This cmdq is now destined for
  88. * attach-session. Because attach-session
  89. * will have already been prepared, copy this
  90. * session into its tflag so it can be used.
  91. */
  92. cmd_find_from_session(&cmdq->state.tflag, as);
  93. return (cmd_attach_session(cmdq,
  94. args_has(args, 'D'), 0, NULL,
  95. args_has(args, 'E')));
  96. }
  97. cmdq_error(cmdq, "duplicate session: %s", newname);
  98. return (CMD_RETURN_ERROR);
  99. }
  100. }
  101. if ((target = args_get(args, 't')) != NULL) {
  102. if (groupwith == NULL) {
  103. cmdq_error(cmdq, "no such session: %s", target);
  104. goto error;
  105. }
  106. } else
  107. groupwith = NULL;
  108. /* Set -d if no client. */
  109. detached = args_has(args, 'd');
  110. if (c == NULL)
  111. detached = 1;
  112. /* Is this client already attached? */
  113. already_attached = 0;
  114. if (c != NULL && c->session != NULL)
  115. already_attached = 1;
  116. /* Get the new session working directory. */
  117. if (args_has(args, 'c')) {
  118. ft = format_create(cmdq, 0);
  119. format_defaults(ft, c, NULL, NULL, NULL);
  120. to_free = cwd = format_expand(ft, args_get(args, 'c'));
  121. format_free(ft);
  122. } else if (c != NULL && c->session == NULL && c->cwd != NULL)
  123. cwd = c->cwd;
  124. else
  125. cwd = ".";
  126. /*
  127. * If this is a new client, check for nesting and save the termios
  128. * settings (part of which is used for new windows in this session).
  129. *
  130. * tcgetattr() is used rather than using tty.tio since if the client is
  131. * detached, tty_open won't be called. It must be done before opening
  132. * the terminal as that calls tcsetattr() to prepare for tmux taking
  133. * over.
  134. */
  135. if (!detached && !already_attached && c->tty.fd != -1) {
  136. if (server_client_check_nested(cmdq->client)) {
  137. cmdq_error(cmdq, "sessions should be nested with care, "
  138. "unset $TMUX to force");
  139. return (CMD_RETURN_ERROR);
  140. }
  141. if (tcgetattr(c->tty.fd, &tio) != 0)
  142. fatal("tcgetattr failed");
  143. tiop = &tio;
  144. } else
  145. tiop = NULL;
  146. /* Open the terminal if necessary. */
  147. if (!detached && !already_attached) {
  148. if (server_client_open(c, &cause) != 0) {
  149. cmdq_error(cmdq, "open terminal failed: %s", cause);
  150. free(cause);
  151. goto error;
  152. }
  153. }
  154. /* Find new session size. */
  155. if (c != NULL) {
  156. sx = c->tty.sx;
  157. sy = c->tty.sy;
  158. } else {
  159. sx = 80;
  160. sy = 24;
  161. }
  162. if (detached && args_has(args, 'x')) {
  163. sx = strtonum(args_get(args, 'x'), 1, USHRT_MAX, &errstr);
  164. if (errstr != NULL) {
  165. cmdq_error(cmdq, "width %s", errstr);
  166. goto error;
  167. }
  168. }
  169. if (detached && args_has(args, 'y')) {
  170. sy = strtonum(args_get(args, 'y'), 1, USHRT_MAX, &errstr);
  171. if (errstr != NULL) {
  172. cmdq_error(cmdq, "height %s", errstr);
  173. goto error;
  174. }
  175. }
  176. if (sy > 0 && options_get_number(global_s_options, "status"))
  177. sy--;
  178. if (sx == 0)
  179. sx = 1;
  180. if (sy == 0)
  181. sy = 1;
  182. /* Figure out the command for the new window. */
  183. argc = -1;
  184. argv = NULL;
  185. if (!args_has(args, 't') && args->argc != 0) {
  186. argc = args->argc;
  187. argv = args->argv;
  188. } else if (groupwith == NULL) {
  189. cmd = options_get_string(global_s_options, "default-command");
  190. if (cmd != NULL && *cmd != '\0') {
  191. argc = 1;
  192. argv = &cmd;
  193. } else {
  194. argc = 0;
  195. argv = NULL;
  196. }
  197. }
  198. path = NULL;
  199. if (c != NULL && c->session == NULL)
  200. envent = environ_find(c->environ, "PATH");
  201. else
  202. envent = environ_find(global_environ, "PATH");
  203. if (envent != NULL)
  204. path = envent->value;
  205. /* Construct the environment. */
  206. env = environ_create();
  207. if (c != NULL && !args_has(args, 'E')) {
  208. update = options_get_string(global_s_options,
  209. "update-environment");
  210. environ_update(update, c->environ, env);
  211. }
  212. /* Create the new session. */
  213. idx = -1 - options_get_number(global_s_options, "base-index");
  214. s = session_create(newname, argc, argv, path, cwd, env, tiop, idx, sx,
  215. sy, &cause);
  216. environ_free(env);
  217. if (s == NULL) {
  218. cmdq_error(cmdq, "create session failed: %s", cause);
  219. free(cause);
  220. goto error;
  221. }
  222. /* Set the initial window name if one given. */
  223. if (argc >= 0 && args_has(args, 'n')) {
  224. w = s->curw->window;
  225. window_set_name(w, args_get(args, 'n'));
  226. options_set_number(w->options, "automatic-rename", 0);
  227. }
  228. /*
  229. * If a target session is given, this is to be part of a session group,
  230. * so add it to the group and synchronize.
  231. */
  232. if (groupwith != NULL) {
  233. session_group_add(groupwith, s);
  234. session_group_synchronize_to(s);
  235. session_select(s, RB_MIN(winlinks, &s->windows)->idx);
  236. }
  237. /*
  238. * Set the client to the new session. If a command client exists, it is
  239. * taking this session and needs to get MSG_READY and stay around.
  240. */
  241. if (!detached) {
  242. if (!already_attached) {
  243. if (~c->flags & CLIENT_CONTROL)
  244. proc_send(c->peer, MSG_READY, -1, NULL, 0);
  245. } else if (c->session != NULL)
  246. c->last_session = c->session;
  247. c->session = s;
  248. server_client_set_key_table(c, NULL);
  249. status_timer_start(c);
  250. notify_attached_session_changed(c);
  251. session_update_activity(s, NULL);
  252. gettimeofday(&s->last_attached_time, NULL);
  253. server_redraw_client(c);
  254. }
  255. recalculate_sizes();
  256. server_update_socket();
  257. /*
  258. * If there are still configuration file errors to display, put the new
  259. * session's current window into more mode and display them now.
  260. */
  261. if (cfg_finished)
  262. cfg_show_causes(s);
  263. /* Print if requested. */
  264. if (args_has(args, 'P')) {
  265. if ((template = args_get(args, 'F')) == NULL)
  266. template = NEW_SESSION_TEMPLATE;
  267. ft = format_create(cmdq, 0);
  268. format_defaults(ft, c, s, NULL, NULL);
  269. cp = format_expand(ft, template);
  270. cmdq_print(cmdq, "%s", cp);
  271. free(cp);
  272. format_free(ft);
  273. }
  274. if (!detached)
  275. cmdq->client_exit = 0;
  276. if (to_free != NULL)
  277. free((void *)to_free);
  278. return (CMD_RETURN_NORMAL);
  279. error:
  280. if (to_free != NULL)
  281. free((void *)to_free);
  282. return (CMD_RETURN_ERROR);
  283. }