cmd-rename-session.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 <stdlib.h>
  19. #include "tmux.h"
  20. /*
  21. * Change session name.
  22. */
  23. enum cmd_retval cmd_rename_session_exec(struct cmd *, struct cmd_q *);
  24. const struct cmd_entry cmd_rename_session_entry = {
  25. .name = "rename-session",
  26. .alias = "rename",
  27. .args = { "t:", 1, 1 },
  28. .usage = CMD_TARGET_SESSION_USAGE " new-name",
  29. .tflag = CMD_SESSION,
  30. .flags = 0,
  31. .exec = cmd_rename_session_exec
  32. };
  33. enum cmd_retval
  34. cmd_rename_session_exec(struct cmd *self, struct cmd_q *cmdq)
  35. {
  36. struct args *args = self->args;
  37. struct session *s = cmdq->state.tflag.s;
  38. const char *newname;
  39. newname = args->argv[0];
  40. if (!session_check_name(newname)) {
  41. cmdq_error(cmdq, "bad session name: %s", newname);
  42. return (CMD_RETURN_ERROR);
  43. }
  44. if (session_find(newname) != NULL) {
  45. cmdq_error(cmdq, "duplicate session: %s", newname);
  46. return (CMD_RETURN_ERROR);
  47. }
  48. RB_REMOVE(sessions, &sessions, s);
  49. free(s->name);
  50. s->name = xstrdup(newname);
  51. RB_INSERT(sessions, &sessions, s);
  52. server_status_session(s);
  53. notify_session_renamed(s);
  54. return (CMD_RETURN_NORMAL);
  55. }