1 /* $OpenBSD: cmd-command-prompt.c,v 1.38 2016/10/16 19:04: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(void *, const char *); 36 static void cmd_command_prompt_free(void *); 37 38 const struct cmd_entry cmd_command_prompt_entry = { 39 .name = "command-prompt", 40 .alias = NULL, 41 42 .args = { "1I:Np:t:", 0, 1 }, 43 .usage = "[-1N] [-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE " " 44 "[template]", 45 46 .tflag = CMD_CLIENT, 47 48 .flags = 0, 49 .exec = cmd_command_prompt_exec 50 }; 51 52 struct cmd_command_prompt_cdata { 53 struct client *c; 54 char *inputs; 55 char *next_input; 56 char *next_prompt; 57 char *prompts; 58 char *template; 59 int idx; 60 }; 61 62 static enum cmd_retval 63 cmd_command_prompt_exec(struct cmd *self, struct cmdq_item *item) 64 { 65 struct args *args = self->args; 66 const char *inputs, *prompts; 67 struct cmd_command_prompt_cdata *cdata; 68 struct client *c = item->state.c; 69 char *prompt, *ptr, *input = NULL; 70 size_t n; 71 int flags; 72 73 if (c->prompt_string != NULL) 74 return (CMD_RETURN_NORMAL); 75 76 cdata = xmalloc(sizeof *cdata); 77 cdata->c = c; 78 cdata->idx = 1; 79 cdata->inputs = NULL; 80 cdata->next_input = NULL; 81 cdata->next_prompt = NULL; 82 cdata->prompts = NULL; 83 cdata->template = NULL; 84 85 if (args->argc != 0) 86 cdata->template = xstrdup(args->argv[0]); 87 else 88 cdata->template = xstrdup("%1"); 89 90 if ((prompts = args_get(args, 'p')) != NULL) 91 cdata->prompts = xstrdup(prompts); 92 else if (args->argc != 0) { 93 n = strcspn(cdata->template, " ,"); 94 xasprintf(&cdata->prompts, "(%.*s) ", (int) n, cdata->template); 95 } else 96 cdata->prompts = xstrdup(":"); 97 98 /* Get first prompt. */ 99 cdata->next_prompt = cdata->prompts; 100 ptr = strsep(&cdata->next_prompt, ","); 101 if (prompts == NULL) 102 prompt = xstrdup(ptr); 103 else 104 xasprintf(&prompt, "%s ", ptr); 105 106 /* Get initial prompt input. */ 107 if ((inputs = args_get(args, 'I')) != NULL) { 108 cdata->inputs = xstrdup(inputs); 109 cdata->next_input = cdata->inputs; 110 input = strsep(&cdata->next_input, ","); 111 } 112 113 flags = 0; 114 if (args_has(args, '1')) 115 flags |= PROMPT_SINGLE; 116 else if (args_has(args, 'N')) 117 flags |= PROMPT_NUMERIC; 118 status_prompt_set(c, prompt, input, cmd_command_prompt_callback, 119 cmd_command_prompt_free, cdata, flags); 120 free(prompt); 121 122 return (CMD_RETURN_NORMAL); 123 } 124 125 static enum cmd_retval 126 cmd_command_prompt_error(struct cmdq_item *item, void *data) 127 { 128 char *error = data; 129 130 cmdq_error(item, "%s", error); 131 free(error); 132 133 return (CMD_RETURN_NORMAL); 134 } 135 136 static int 137 cmd_command_prompt_callback(void *data, const char *s) 138 { 139 struct cmd_command_prompt_cdata *cdata = data; 140 struct client *c = cdata->c; 141 struct cmd_list *cmdlist; 142 struct cmdq_item *new_item; 143 char *cause, *new_template, *prompt, *ptr; 144 char *input = NULL; 145 146 if (s == NULL) 147 return (0); 148 149 new_template = cmd_template_replace(cdata->template, s, cdata->idx); 150 free(cdata->template); 151 cdata->template = new_template; 152 153 /* 154 * Check if there are more prompts; if so, get its respective input 155 * and update the prompt data. 156 */ 157 if ((ptr = strsep(&cdata->next_prompt, ",")) != NULL) { 158 xasprintf(&prompt, "%s ", ptr); 159 input = strsep(&cdata->next_input, ","); 160 status_prompt_update(c, prompt, input); 161 162 free(prompt); 163 cdata->idx++; 164 return (1); 165 } 166 167 if (cmd_string_parse(new_template, &cmdlist, NULL, 0, &cause) != 0) { 168 if (cause != NULL) { 169 new_item = cmdq_get_callback(cmd_command_prompt_error, 170 cause); 171 } else 172 new_item = NULL; 173 } else { 174 new_item = cmdq_get_command(cmdlist, NULL, NULL, 0); 175 cmd_list_free(cmdlist); 176 } 177 178 if (new_item != NULL) 179 cmdq_append(c, new_item); 180 181 if (c->prompt_callbackfn != (void *)&cmd_command_prompt_callback) 182 return (1); 183 return (0); 184 } 185 186 static void 187 cmd_command_prompt_free(void *data) 188 { 189 struct cmd_command_prompt_cdata *cdata = data; 190 191 free(cdata->inputs); 192 free(cdata->prompts); 193 free(cdata->template); 194 free(cdata); 195 } 196