cmd-respawn-window.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2008 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 <unistd.h>
  20. #include "tmux.h"
  21. /*
  22. * Respawn a window (restart the command). Kill existing if -k given.
  23. */
  24. enum cmd_retval cmd_respawn_window_exec(struct cmd *, struct cmd_q *);
  25. const struct cmd_entry cmd_respawn_window_entry = {
  26. .name = "respawn-window",
  27. .alias = "respawnw",
  28. .args = { "kt:", 0, -1 },
  29. .usage = "[-k] " CMD_TARGET_WINDOW_USAGE " [command]",
  30. .tflag = CMD_WINDOW,
  31. .flags = 0,
  32. .exec = cmd_respawn_window_exec
  33. };
  34. enum cmd_retval
  35. cmd_respawn_window_exec(struct cmd *self, struct cmd_q *cmdq)
  36. {
  37. struct args *args = self->args;
  38. struct session *s = cmdq->state.tflag.s;
  39. struct winlink *wl = cmdq->state.tflag.wl;
  40. struct window *w = wl->window;
  41. struct window_pane *wp;
  42. struct environ *env;
  43. const char *path;
  44. char *cause;
  45. struct environ_entry *envent;
  46. if (!args_has(self->args, 'k')) {
  47. TAILQ_FOREACH(wp, &w->panes, entry) {
  48. if (wp->fd == -1)
  49. continue;
  50. cmdq_error(cmdq, "window still active: %s:%d", s->name,
  51. wl->idx);
  52. return (CMD_RETURN_ERROR);
  53. }
  54. }
  55. env = environ_create();
  56. environ_copy(global_environ, env);
  57. environ_copy(s->environ, env);
  58. server_fill_environ(s, env);
  59. wp = TAILQ_FIRST(&w->panes);
  60. TAILQ_REMOVE(&w->panes, wp, entry);
  61. layout_free(w);
  62. window_destroy_panes(w);
  63. TAILQ_INSERT_HEAD(&w->panes, wp, entry);
  64. window_pane_resize(wp, w->sx, w->sy);
  65. path = NULL;
  66. if (cmdq->client != NULL && cmdq->client->session == NULL)
  67. envent = environ_find(cmdq->client->environ, "PATH");
  68. else
  69. envent = environ_find(s->environ, "PATH");
  70. if (envent != NULL)
  71. path = envent->value;
  72. if (window_pane_spawn(wp, args->argc, args->argv, path, NULL, NULL, env,
  73. s->tio, &cause) != 0) {
  74. cmdq_error(cmdq, "respawn window failed: %s", cause);
  75. free(cause);
  76. environ_free(env);
  77. server_destroy_pane(wp, 0);
  78. return (CMD_RETURN_ERROR);
  79. }
  80. layout_init(w, wp);
  81. window_pane_reset_mode(wp);
  82. screen_reinit(&wp->base);
  83. input_init(wp);
  84. window_set_active_pane(w, wp);
  85. recalculate_sizes();
  86. server_redraw_window(w);
  87. environ_free(env);
  88. return (CMD_RETURN_NORMAL);
  89. }