1 /* $OpenBSD: cmd-command-prompt.c,v 1.61 2021/08/25 06:36:05 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, NULL }, 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_prompt { 52 char *input; 53 char *prompt; 54 }; 55 56 struct cmd_command_prompt_cdata { 57 struct cmdq_item *item; 58 struct args_command_state *state; 59 60 int flags; 61 enum prompt_type prompt_type; 62 63 struct cmd_command_prompt_prompt *prompts; 64 u_int count; 65 u_int current; 66 67 int argc; 68 char **argv; 69 }; 70 71 static enum cmd_retval 72 cmd_command_prompt_exec(struct cmd *self, struct cmdq_item *item) 73 { 74 struct args *args = cmd_get_args(self); 75 struct client *tc = cmdq_get_target_client(item); 76 struct cmd_find_state *target = cmdq_get_target(item); 77 const char *type, *s, *input; 78 struct cmd_command_prompt_cdata *cdata; 79 char *tmp, *prompts, *prompt, *next_prompt; 80 char *inputs = NULL, *next_input; 81 u_int count = args_count(args); 82 int wait = !args_has(args, 'b'), space = 1; 83 84 if (tc->prompt_string != NULL) 85 return (CMD_RETURN_NORMAL); 86 if (args_has(args, 'i')) 87 wait = 0; 88 89 cdata = xcalloc(1, sizeof *cdata); 90 if (wait) 91 cdata->item = item; 92 cdata->state = args_make_commands_prepare(self, item, 0, "%1", wait, 93 args_has(args, 'F')); 94 95 if ((s = args_get(args, 'p')) == NULL) { 96 if (count != 0) { 97 tmp = args_make_commands_get_command(cdata->state); 98 xasprintf(&prompts, "(%s)", tmp); 99 free(tmp); 100 } else { 101 prompts = xstrdup(":"); 102 space = 0; 103 } 104 next_prompt = prompts; 105 } else 106 next_prompt = prompts = xstrdup (s); 107 if ((s = args_get(args, 'I')) != NULL) 108 next_input = inputs = xstrdup(s); 109 else 110 next_input = NULL; 111 while ((prompt = strsep(&next_prompt, ",")) != NULL) { 112 cdata->prompts = xreallocarray(cdata->prompts, cdata->count + 1, 113 sizeof *cdata->prompts); 114 if (!space) 115 tmp = xstrdup(prompt); 116 else 117 xasprintf(&tmp, "%s ", prompt); 118 cdata->prompts[cdata->count].prompt = tmp; 119 120 if (next_input != NULL) { 121 input = strsep(&next_input, ","); 122 if (input == NULL) 123 input = ""; 124 } else 125 input = ""; 126 cdata->prompts[cdata->count].input = xstrdup(input); 127 128 cdata->count++; 129 } 130 free(inputs); 131 free(prompts); 132 133 if ((type = args_get(args, 'T')) != NULL) { 134 cdata->prompt_type = status_prompt_type(type); 135 if (cdata->prompt_type == PROMPT_TYPE_INVALID) { 136 cmdq_error(item, "unknown type: %s", type); 137 return (CMD_RETURN_ERROR); 138 } 139 } else 140 cdata->prompt_type = PROMPT_TYPE_COMMAND; 141 142 if (args_has(args, '1')) 143 cdata->flags |= PROMPT_SINGLE; 144 else if (args_has(args, 'N')) 145 cdata->flags |= PROMPT_NUMERIC; 146 else if (args_has(args, 'i')) 147 cdata->flags |= PROMPT_INCREMENTAL; 148 else if (args_has(args, 'k')) 149 cdata->flags |= PROMPT_KEY; 150 status_prompt_set(tc, target, cdata->prompts[0].prompt, 151 cdata->prompts[0].input, cmd_command_prompt_callback, 152 cmd_command_prompt_free, cdata, cdata->flags, cdata->prompt_type); 153 154 if (!wait) 155 return (CMD_RETURN_NORMAL); 156 return (CMD_RETURN_WAIT); 157 } 158 159 static int 160 cmd_command_prompt_callback(struct client *c, void *data, const char *s, 161 int done) 162 { 163 struct cmd_command_prompt_cdata *cdata = data; 164 char *error; 165 struct cmdq_item *item = cdata->item, *new_item; 166 struct cmd_list *cmdlist; 167 struct cmd_command_prompt_prompt *prompt; 168 169 if (s == NULL) 170 goto out; 171 if (done) { 172 if (cdata->flags & PROMPT_INCREMENTAL) 173 goto out; 174 175 cmd_append_argv(&cdata->argc, &cdata->argv, s); 176 if (++cdata->current != cdata->count) { 177 prompt = &cdata->prompts[cdata->current]; 178 status_prompt_update(c, prompt->prompt, prompt->input); 179 return (1); 180 } 181 } 182 183 cmdlist = args_make_commands(cdata->state, cdata->argc, cdata->argv, 184 &error); 185 if (cmdlist == NULL) { 186 cmdq_append(c, cmdq_get_error(error)); 187 free(error); 188 } else if (item == NULL) { 189 new_item = cmdq_get_command(cmdlist, NULL); 190 cmdq_append(c, new_item); 191 } else { 192 new_item = cmdq_get_command(cmdlist, cmdq_get_state(item)); 193 cmdq_insert_after(item, new_item); 194 } 195 196 if (c->prompt_inputcb != cmd_command_prompt_callback) 197 return (1); 198 199 out: 200 if (item != NULL) 201 cmdq_continue(item); 202 return (0); 203 } 204 205 static void 206 cmd_command_prompt_free(void *data) 207 { 208 struct cmd_command_prompt_cdata *cdata = data; 209 u_int i; 210 211 for (i = 0; i < cdata->count; i++) { 212 free(cdata->prompts[i].prompt); 213 free(cdata->prompts[i].input); 214 } 215 free(cdata->prompts); 216 cmd_free_argv(cdata->argc, cdata->argv); 217 args_make_commands_free(cdata->state); 218 free(cdata); 219 } 220