1*a51dead1Snicm /* $OpenBSD: cmd-rename-session.c,v 1.35 2021/08/21 10:22:39 nicm Exp $ */
2311827fbSnicm
3311827fbSnicm /*
498ca8272Snicm * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5311827fbSnicm *
6311827fbSnicm * Permission to use, copy, modify, and distribute this software for any
7311827fbSnicm * purpose with or without fee is hereby granted, provided that the above
8311827fbSnicm * copyright notice and this permission notice appear in all copies.
9311827fbSnicm *
10311827fbSnicm * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11311827fbSnicm * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12311827fbSnicm * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13311827fbSnicm * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14311827fbSnicm * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15311827fbSnicm * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16311827fbSnicm * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17311827fbSnicm */
18311827fbSnicm
19311827fbSnicm #include <sys/types.h>
20311827fbSnicm
21311827fbSnicm #include <stdlib.h>
227c0e80b6Snicm #include <string.h>
23311827fbSnicm
24311827fbSnicm #include "tmux.h"
25311827fbSnicm
26311827fbSnicm /*
27311827fbSnicm * Change session name.
28311827fbSnicm */
29311827fbSnicm
3068e0a7f2Snicm static enum cmd_retval cmd_rename_session_exec(struct cmd *,
3168e0a7f2Snicm struct cmdq_item *);
32311827fbSnicm
33311827fbSnicm const struct cmd_entry cmd_rename_session_entry = {
34c057646bSnicm .name = "rename-session",
35c057646bSnicm .alias = "rename",
36c057646bSnicm
37*a51dead1Snicm .args = { "t:", 1, 1, NULL },
38c057646bSnicm .usage = CMD_TARGET_SESSION_USAGE " new-name",
39c057646bSnicm
40bf0d297eSnicm .target = { 't', CMD_FIND_SESSION, 0 },
418d471e80Snicm
427a61a8ddSnicm .flags = CMD_AFTERHOOK,
43c057646bSnicm .exec = cmd_rename_session_exec
44311827fbSnicm };
45311827fbSnicm
46dc1f0f5fSnicm static enum cmd_retval
cmd_rename_session_exec(struct cmd * self,struct cmdq_item * item)4768e0a7f2Snicm cmd_rename_session_exec(struct cmd *self, struct cmdq_item *item)
48311827fbSnicm {
4990d7ba38Snicm struct args *args = cmd_get_args(self);
50040343aeSnicm struct cmd_find_state *target = cmdq_get_target(item);
51040343aeSnicm struct session *s = target->s;
52c27246d4Snicm char *newname, *tmp;
53311827fbSnicm
541693b10bSnicm tmp = format_single_from_target(item, args_string(args, 0));
55c27246d4Snicm newname = session_check_name(tmp);
56998b9ee1Snicm if (newname == NULL) {
57998b9ee1Snicm cmdq_error(item, "invalid session: %s", tmp);
58998b9ee1Snicm free(tmp);
59998b9ee1Snicm return (CMD_RETURN_ERROR);
60998b9ee1Snicm }
61c27246d4Snicm free(tmp);
6289b49179Snicm if (strcmp(newname, s->name) == 0) {
6389b49179Snicm free(newname);
647c0e80b6Snicm return (CMD_RETURN_NORMAL);
6589b49179Snicm }
66ca7befccSnicm if (session_find(newname) != NULL) {
6768e0a7f2Snicm cmdq_error(item, "duplicate session: %s", newname);
6889b49179Snicm free(newname);
69a224d0d3Snicm return (CMD_RETURN_ERROR);
7016f2f808Snicm }
7116f2f808Snicm
7259996dc3Snicm RB_REMOVE(sessions, &sessions, s);
737d053cf9Snicm free(s->name);
7489b49179Snicm s->name = newname;
7559996dc3Snicm RB_INSERT(sessions, &sessions, s);
76311827fbSnicm
774afc3893Snicm server_status_session(s);
782ae124feSnicm notify_session("session-renamed", s);
794afc3893Snicm
80a224d0d3Snicm return (CMD_RETURN_NORMAL);
81311827fbSnicm }
82