cmd-set-hook.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2012 Thomas Adam <thomas@xteddy.org>
  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 <string.h>
  20. #include "tmux.h"
  21. /*
  22. * Set or show global or session hooks.
  23. */
  24. enum cmd_retval cmd_set_hook_exec(struct cmd *, struct cmd_q *);
  25. const struct cmd_entry cmd_set_hook_entry = {
  26. .name = "set-hook",
  27. .alias = NULL,
  28. .args = { "gt:u", 1, 2 },
  29. .usage = "[-gu] " CMD_TARGET_SESSION_USAGE " hook-name [command]",
  30. .tflag = CMD_SESSION,
  31. .flags = 0,
  32. .exec = cmd_set_hook_exec
  33. };
  34. const struct cmd_entry cmd_show_hooks_entry = {
  35. .name = "show-hooks",
  36. .alias = NULL,
  37. .args = { "gt:", 0, 1 },
  38. .usage = "[-g] " CMD_TARGET_SESSION_USAGE,
  39. .tflag = CMD_SESSION,
  40. .flags = 0,
  41. .exec = cmd_set_hook_exec
  42. };
  43. enum cmd_retval
  44. cmd_set_hook_exec(struct cmd *self, struct cmd_q *cmdq)
  45. {
  46. struct args *args = self->args;
  47. struct cmd_list *cmdlist;
  48. struct hooks *hooks;
  49. struct hook *hook;
  50. char *cause, *tmp;
  51. const char *name, *cmd;
  52. if (args_has(args, 'g'))
  53. hooks = global_hooks;
  54. else
  55. hooks = cmdq->state.tflag.s->hooks;
  56. if (self->entry == &cmd_show_hooks_entry) {
  57. hook = hooks_first(hooks);
  58. while (hook != NULL) {
  59. tmp = cmd_list_print(hook->cmdlist);
  60. cmdq_print(cmdq, "%s -> %s", hook->name, tmp);
  61. free(tmp);
  62. hook = hooks_next(hook);
  63. }
  64. return (CMD_RETURN_NORMAL);
  65. }
  66. name = args->argv[0];
  67. if (*name == '\0') {
  68. cmdq_error(cmdq, "invalid hook name");
  69. return (CMD_RETURN_ERROR);
  70. }
  71. if (args->argc < 2)
  72. cmd = NULL;
  73. else
  74. cmd = args->argv[1];
  75. if (args_has(args, 'u')) {
  76. if (cmd != NULL) {
  77. cmdq_error(cmdq, "command passed to unset hook: %s",
  78. name);
  79. return (CMD_RETURN_ERROR);
  80. }
  81. hooks_remove(hooks, name);
  82. return (CMD_RETURN_NORMAL);
  83. }
  84. if (cmd == NULL) {
  85. cmdq_error(cmdq, "no command to set hook: %s", name);
  86. return (CMD_RETURN_ERROR);
  87. }
  88. if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) {
  89. if (cause != NULL) {
  90. cmdq_error(cmdq, "%s", cause);
  91. free(cause);
  92. }
  93. return (CMD_RETURN_ERROR);
  94. }
  95. hooks_add(hooks, name, cmdlist);
  96. cmd_list_free(cmdlist);
  97. return (CMD_RETURN_NORMAL);
  98. }