1 /* $OpenBSD: cmd-command-prompt.c,v 1.58 2021/08/20 19:50:16 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com> 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 19 #include <sys/types.h> 20 21 #include <ctype.h> 22 #include <stdlib.h> 23 #include <string.h> 24 #include <time.h> 25 26 #include "tmux.h" 27 28 /* 29 * Prompt for command in client. 30 */ 31 32 static enum cmd_retval cmd_command_prompt_exec(struct cmd *, 33 struct cmdq_item *); 34 35 static int cmd_command_prompt_callback(struct client *, void *, 36 const char *, int); 37 static void cmd_command_prompt_free(void *); 38 39 const struct cmd_entry cmd_command_prompt_entry = { 40 .name = "command-prompt", 41 .alias = NULL, 42 43 .args = { "1bFkiI:Np:t:T:", 0, 1 }, 44 .usage = "[-1bFkiN] [-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE 45 " [-T type] [template]", 46 47 .flags = CMD_CLIENT_TFLAG, 48 .exec = cmd_command_prompt_exec 49 }; 50 51 struct cmd_command_prompt_cdata { 52 struct cmdq_item *item; 53 struct cmd_parse_input pi; 54 55 int flags; 56 enum prompt_type prompt_type; 57 58 char *inputs; 59 char *next_input; 60 61 char *prompts; 62 char *next_prompt; 63 64 char *template; 65 int idx; 66 }; 67 68 static enum cmd_retval 69 cmd_command_prompt_exec(struct cmd *self, struct cmdq_item *item) 70 { 71 struct args *args = cmd_get_args(self); 72 struct client *tc = cmdq_get_target_client(item); 73 struct cmd_find_state *target = cmdq_get_target(item); 74 const char *inputs, *prompts, *type, *s; 75 struct cmd_command_prompt_cdata *cdata; 76 char *prompt, *comma, *input = NULL; 77 u_int count = args_count(args); 78 size_t n; 79 int wait = !args_has(args, 'b'); 80 81 if (tc->prompt_string != NULL) 82 return (CMD_RETURN_NORMAL); 83 if (args_has(args, 'i')) 84 wait = 0; 85 86 cdata = xcalloc(1, sizeof *cdata); 87 cdata->idx = 1; 88 89 cmd_get_source(self, &cdata->pi.file, &cdata->pi.line); 90 if (wait) 91 cdata->pi.item = item; 92 cdata->pi.c = tc; 93 cmd_find_copy_state(&cdata->pi.fs, target); 94 95 if (wait) 96 cdata->item = item; 97 98 if (count != 0) { 99 s = args_string(args, 0); 100 if (args_has(args, 'F')) 101 cdata->template = format_single_from_target(item, s); 102 else 103 cdata->template = xstrdup(s); 104 } else 105 cdata->template = xstrdup("%1"); 106 107 if ((prompts = args_get(args, 'p')) != NULL) 108 cdata->prompts = xstrdup(prompts); 109 else if (count != 0) { 110 n = strcspn(cdata->template, " ,"); 111 xasprintf(&cdata->prompts, "(%.*s) ", (int)n, cdata->template); 112 } else 113 cdata->prompts = xstrdup(":"); 114 115 /* Get first prompt. */ 116 cdata->next_prompt = cdata->prompts; 117 comma = strsep(&cdata->next_prompt, ","); 118 if (prompts == NULL) 119 prompt = xstrdup(comma); 120 else 121 xasprintf(&prompt, "%s ", comma); 122 123 /* Get initial prompt input. */ 124 if ((inputs = args_get(args, 'I')) != NULL) { 125 cdata->inputs = xstrdup(inputs); 126 cdata->next_input = cdata->inputs; 127 input = strsep(&cdata->next_input, ","); 128 } 129 130 /* Get prompt type. */ 131 if ((type = args_get(args, 'T')) != NULL) { 132 cdata->prompt_type = status_prompt_type(type); 133 if (cdata->prompt_type == PROMPT_TYPE_INVALID) { 134 cmdq_error(item, "unknown type: %s", type); 135 return (CMD_RETURN_ERROR); 136 } 137 } else 138 cdata->prompt_type = PROMPT_TYPE_COMMAND; 139 140 if (args_has(args, '1')) 141 cdata->flags |= PROMPT_SINGLE; 142 else if (args_has(args, 'N')) 143 cdata->flags |= PROMPT_NUMERIC; 144 else if (args_has(args, 'i')) 145 cdata->flags |= PROMPT_INCREMENTAL; 146 else if (args_has(args, 'k')) 147 cdata->flags |= PROMPT_KEY; 148 status_prompt_set(tc, target, prompt, input, 149 cmd_command_prompt_callback, cmd_command_prompt_free, cdata, 150 cdata->flags, cdata->prompt_type); 151 free(prompt); 152 153 if (!wait) 154 return (CMD_RETURN_NORMAL); 155 return (CMD_RETURN_WAIT); 156 } 157 158 static int 159 cmd_command_prompt_callback(struct client *c, void *data, const char *s, 160 int done) 161 { 162 struct cmd_command_prompt_cdata *cdata = data; 163 char *new_template, *prompt, *comma, *error; 164 char *input = NULL; 165 struct cmdq_item *item = cdata->item; 166 enum cmd_parse_status status; 167 168 if (s == NULL) 169 goto out; 170 if (done && (cdata->flags & PROMPT_INCREMENTAL)) 171 goto out; 172 173 new_template = cmd_template_replace(cdata->template, s, cdata->idx); 174 if (done) { 175 free(cdata->template); 176 cdata->template = new_template; 177 } 178 179 /* 180 * Check if there are more prompts; if so, get its respective input 181 * and update the prompt data. 182 */ 183 if (done && (comma = strsep(&cdata->next_prompt, ",")) != NULL) { 184 xasprintf(&prompt, "%s ", comma); 185 input = strsep(&cdata->next_input, ","); 186 status_prompt_update(c, prompt, input); 187 188 free(prompt); 189 cdata->idx++; 190 return (1); 191 } 192 193 if (item != NULL) { 194 status = cmd_parse_and_insert(new_template, &cdata->pi, item, 195 cmdq_get_state(item), &error); 196 } else { 197 status = cmd_parse_and_append(new_template, &cdata->pi, c, NULL, 198 &error); 199 } 200 if (status == CMD_PARSE_ERROR) { 201 cmdq_append(c, cmdq_get_error(error)); 202 free(error); 203 } 204 205 if (!done) 206 free(new_template); 207 if (c->prompt_inputcb != cmd_command_prompt_callback) 208 return (1); 209 210 out: 211 if (item != NULL) 212 cmdq_continue(item); 213 return (0); 214 } 215 216 static void 217 cmd_command_prompt_free(void *data) 218 { 219 struct cmd_command_prompt_cdata *cdata = data; 220 221 free(cdata->inputs); 222 free(cdata->prompts); 223 free(cdata->template); 224 free(cdata); 225 } 226