cmd-if-shell.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
  4. * Copyright (c) 2009 Nicholas Marriott <nicm@openbsd.org>
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  15. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  16. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <sys/types.h>
  19. #include <sys/wait.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "tmux.h"
  23. /*
  24. * Executes a tmux command if a shell command returns true or false.
  25. */
  26. enum cmd_retval cmd_if_shell_exec(struct cmd *, struct cmd_q *);
  27. void cmd_if_shell_callback(struct job *);
  28. void cmd_if_shell_done(struct cmd_q *);
  29. void cmd_if_shell_free(void *);
  30. const struct cmd_entry cmd_if_shell_entry = {
  31. .name = "if-shell",
  32. .alias = "if",
  33. .args = { "bFt:", 2, 3 },
  34. .usage = "[-bF] " CMD_TARGET_PANE_USAGE " shell-command command "
  35. "[command]",
  36. .tflag = CMD_PANE_CANFAIL,
  37. .flags = 0,
  38. .exec = cmd_if_shell_exec
  39. };
  40. struct cmd_if_shell_data {
  41. char *cmd_if;
  42. char *cmd_else;
  43. struct cmd_q *cmdq;
  44. struct mouse_event mouse;
  45. int bflag;
  46. int references;
  47. };
  48. enum cmd_retval
  49. cmd_if_shell_exec(struct cmd *self, struct cmd_q *cmdq)
  50. {
  51. struct args *args = self->args;
  52. struct cmd_if_shell_data *cdata;
  53. char *shellcmd, *cmd, *cause;
  54. struct cmd_list *cmdlist;
  55. struct session *s = cmdq->state.tflag.s;
  56. struct winlink *wl = cmdq->state.tflag.wl;
  57. struct window_pane *wp = cmdq->state.tflag.wp;
  58. struct format_tree *ft;
  59. const char *cwd;
  60. if (cmdq->client != NULL && cmdq->client->session == NULL)
  61. cwd = cmdq->client->cwd;
  62. else if (s != NULL)
  63. cwd = s->cwd;
  64. else
  65. cwd = NULL;
  66. ft = format_create(cmdq, 0);
  67. format_defaults(ft, NULL, s, wl, wp);
  68. shellcmd = format_expand(ft, args->argv[0]);
  69. format_free(ft);
  70. if (args_has(args, 'F')) {
  71. cmd = NULL;
  72. if (*shellcmd != '0' && *shellcmd != '\0')
  73. cmd = args->argv[1];
  74. else if (args->argc == 3)
  75. cmd = args->argv[2];
  76. free(shellcmd);
  77. if (cmd == NULL)
  78. return (CMD_RETURN_NORMAL);
  79. if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) {
  80. if (cause != NULL) {
  81. cmdq_error(cmdq, "%s", cause);
  82. free(cause);
  83. }
  84. return (CMD_RETURN_ERROR);
  85. }
  86. cmdq_run(cmdq, cmdlist, &cmdq->item->mouse);
  87. cmd_list_free(cmdlist);
  88. return (CMD_RETURN_NORMAL);
  89. }
  90. cdata = xmalloc(sizeof *cdata);
  91. cdata->cmd_if = xstrdup(args->argv[1]);
  92. if (args->argc == 3)
  93. cdata->cmd_else = xstrdup(args->argv[2]);
  94. else
  95. cdata->cmd_else = NULL;
  96. cdata->bflag = args_has(args, 'b');
  97. cdata->cmdq = cmdq;
  98. memcpy(&cdata->mouse, &cmdq->item->mouse, sizeof cdata->mouse);
  99. cmdq->references++;
  100. cdata->references = 1;
  101. job_run(shellcmd, s, cwd, cmd_if_shell_callback, cmd_if_shell_free,
  102. cdata);
  103. free(shellcmd);
  104. if (cdata->bflag)
  105. return (CMD_RETURN_NORMAL);
  106. return (CMD_RETURN_WAIT);
  107. }
  108. void
  109. cmd_if_shell_callback(struct job *job)
  110. {
  111. struct cmd_if_shell_data *cdata = job->data;
  112. struct cmd_q *cmdq = cdata->cmdq, *cmdq1;
  113. struct cmd_list *cmdlist;
  114. char *cause, *cmd;
  115. if (cmdq->flags & CMD_Q_DEAD)
  116. return;
  117. if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0)
  118. cmd = cdata->cmd_else;
  119. else
  120. cmd = cdata->cmd_if;
  121. if (cmd == NULL)
  122. return;
  123. if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) {
  124. if (cause != NULL) {
  125. cmdq_error(cmdq, "%s", cause);
  126. free(cause);
  127. }
  128. return;
  129. }
  130. cmdq1 = cmdq_new(cmdq->client);
  131. cmdq1->emptyfn = cmd_if_shell_done;
  132. cmdq1->data = cdata;
  133. cdata->references++;
  134. cmdq_run(cmdq1, cmdlist, &cdata->mouse);
  135. cmd_list_free(cmdlist);
  136. }
  137. void
  138. cmd_if_shell_done(struct cmd_q *cmdq1)
  139. {
  140. struct cmd_if_shell_data *cdata = cmdq1->data;
  141. struct cmd_q *cmdq = cdata->cmdq;
  142. if (cmdq1->client_exit >= 0)
  143. cmdq->client_exit = cmdq1->client_exit;
  144. cmdq_free(cmdq1);
  145. if (--cdata->references != 0)
  146. return;
  147. if (!cmdq_free(cmdq) && !cdata->bflag)
  148. cmdq_continue(cmdq);
  149. free(cdata->cmd_else);
  150. free(cdata->cmd_if);
  151. free(cdata);
  152. }
  153. void
  154. cmd_if_shell_free(void *data)
  155. {
  156. struct cmd_if_shell_data *cdata = data;
  157. struct cmd_q *cmdq = cdata->cmdq;
  158. if (--cdata->references != 0)
  159. return;
  160. if (!cmdq_free(cmdq) && !cdata->bflag)
  161. cmdq_continue(cmdq);
  162. free(cdata->cmd_else);
  163. free(cdata->cmd_if);
  164. free(cdata);
  165. }