cmd-command-prompt.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 <ctype.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <time.h>
  22. #include "tmux.h"
  23. /*
  24. * Prompt for command in client.
  25. */
  26. enum cmd_retval cmd_command_prompt_exec(struct cmd *, struct cmd_q *);
  27. int cmd_command_prompt_callback(void *, const char *);
  28. void cmd_command_prompt_free(void *);
  29. const struct cmd_entry cmd_command_prompt_entry = {
  30. .name = "command-prompt",
  31. .alias = NULL,
  32. .args = { "I:p:t:", 0, 1 },
  33. .usage = "[-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE " "
  34. "[template]",
  35. .tflag = CMD_CLIENT,
  36. .flags = 0,
  37. .exec = cmd_command_prompt_exec
  38. };
  39. struct cmd_command_prompt_cdata {
  40. struct client *c;
  41. char *inputs;
  42. char *next_input;
  43. char *next_prompt;
  44. char *prompts;
  45. char *template;
  46. int idx;
  47. };
  48. enum cmd_retval
  49. cmd_command_prompt_exec(struct cmd *self, struct cmd_q *cmdq)
  50. {
  51. struct args *args = self->args;
  52. const char *inputs, *prompts;
  53. struct cmd_command_prompt_cdata *cdata;
  54. struct client *c = cmdq->state.c;
  55. char *prompt, *ptr, *input = NULL;
  56. size_t n;
  57. if (c->prompt_string != NULL)
  58. return (CMD_RETURN_NORMAL);
  59. cdata = xmalloc(sizeof *cdata);
  60. cdata->c = c;
  61. cdata->idx = 1;
  62. cdata->inputs = NULL;
  63. cdata->next_input = NULL;
  64. cdata->next_prompt = NULL;
  65. cdata->prompts = NULL;
  66. cdata->template = NULL;
  67. if (args->argc != 0)
  68. cdata->template = xstrdup(args->argv[0]);
  69. else
  70. cdata->template = xstrdup("%1");
  71. if ((prompts = args_get(args, 'p')) != NULL)
  72. cdata->prompts = xstrdup(prompts);
  73. else if (args->argc != 0) {
  74. n = strcspn(cdata->template, " ,");
  75. xasprintf(&cdata->prompts, "(%.*s) ", (int) n, cdata->template);
  76. } else
  77. cdata->prompts = xstrdup(":");
  78. /* Get first prompt. */
  79. cdata->next_prompt = cdata->prompts;
  80. ptr = strsep(&cdata->next_prompt, ",");
  81. if (prompts == NULL)
  82. prompt = xstrdup(ptr);
  83. else
  84. xasprintf(&prompt, "%s ", ptr);
  85. /* Get initial prompt input. */
  86. if ((inputs = args_get(args, 'I')) != NULL) {
  87. cdata->inputs = xstrdup(inputs);
  88. cdata->next_input = cdata->inputs;
  89. input = strsep(&cdata->next_input, ",");
  90. }
  91. status_prompt_set(c, prompt, input, cmd_command_prompt_callback,
  92. cmd_command_prompt_free, cdata, 0);
  93. free(prompt);
  94. return (CMD_RETURN_NORMAL);
  95. }
  96. int
  97. cmd_command_prompt_callback(void *data, const char *s)
  98. {
  99. struct cmd_command_prompt_cdata *cdata = data;
  100. struct client *c = cdata->c;
  101. struct cmd_list *cmdlist;
  102. char *cause, *new_template, *prompt, *ptr;
  103. char *input = NULL;
  104. if (s == NULL)
  105. return (0);
  106. new_template = cmd_template_replace(cdata->template, s, cdata->idx);
  107. free(cdata->template);
  108. cdata->template = new_template;
  109. /*
  110. * Check if there are more prompts; if so, get its respective input
  111. * and update the prompt data.
  112. */
  113. if ((ptr = strsep(&cdata->next_prompt, ",")) != NULL) {
  114. xasprintf(&prompt, "%s ", ptr);
  115. input = strsep(&cdata->next_input, ",");
  116. status_prompt_update(c, prompt, input);
  117. free(prompt);
  118. cdata->idx++;
  119. return (1);
  120. }
  121. if (cmd_string_parse(new_template, &cmdlist, NULL, 0, &cause) != 0) {
  122. if (cause != NULL) {
  123. *cause = toupper((u_char) *cause);
  124. status_message_set(c, "%s", cause);
  125. free(cause);
  126. }
  127. return (0);
  128. }
  129. cmdq_run(c->cmdq, cmdlist, NULL);
  130. cmd_list_free(cmdlist);
  131. if (c->prompt_callbackfn != (void *) &cmd_command_prompt_callback)
  132. return (1);
  133. return (0);
  134. }
  135. void
  136. cmd_command_prompt_free(void *data)
  137. {
  138. struct cmd_command_prompt_cdata *cdata = data;
  139. free(cdata->inputs);
  140. free(cdata->prompts);
  141. free(cdata->template);
  142. free(cdata);
  143. }