1 /* $OpenBSD$ */ 2 3 /* 4 * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net> 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 enum cmd_retval cmd_command_prompt_exec(struct cmd *, struct cmd_q *); 33 34 int cmd_command_prompt_callback(void *, const char *); 35 void cmd_command_prompt_free(void *); 36 37 const struct cmd_entry cmd_command_prompt_entry = { 38 "command-prompt", NULL, 39 "I:p:t:", 0, 1, 40 "[-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE " [template]", 41 0, 42 cmd_command_prompt_exec 43 }; 44 45 struct cmd_command_prompt_cdata { 46 struct client *c; 47 char *inputs; 48 char *next_input; 49 char *next_prompt; 50 char *prompts; 51 char *template; 52 int idx; 53 }; 54 55 enum cmd_retval 56 cmd_command_prompt_exec(struct cmd *self, struct cmd_q *cmdq) 57 { 58 struct args *args = self->args; 59 const char *inputs, *prompts; 60 struct cmd_command_prompt_cdata *cdata; 61 struct client *c; 62 char *prompt, *ptr, *input = NULL; 63 size_t n; 64 65 if ((c = cmd_find_client(cmdq, args_get(args, 't'), 0)) == NULL) 66 return (CMD_RETURN_ERROR); 67 68 if (c->prompt_string != NULL) 69 return (CMD_RETURN_NORMAL); 70 71 cdata = xmalloc(sizeof *cdata); 72 cdata->c = c; 73 cdata->idx = 1; 74 cdata->inputs = NULL; 75 cdata->next_input = NULL; 76 cdata->next_prompt = NULL; 77 cdata->prompts = NULL; 78 cdata->template = NULL; 79 80 if (args->argc != 0) 81 cdata->template = xstrdup(args->argv[0]); 82 else 83 cdata->template = xstrdup("%1"); 84 85 if ((prompts = args_get(args, 'p')) != NULL) 86 cdata->prompts = xstrdup(prompts); 87 else if (args->argc != 0) { 88 n = strcspn(cdata->template, " ,"); 89 xasprintf(&cdata->prompts, "(%.*s) ", (int) n, cdata->template); 90 } else 91 cdata->prompts = xstrdup(":"); 92 93 /* Get first prompt. */ 94 cdata->next_prompt = cdata->prompts; 95 ptr = strsep(&cdata->next_prompt, ","); 96 if (prompts == NULL) 97 prompt = xstrdup(ptr); 98 else 99 xasprintf(&prompt, "%s ", ptr); 100 101 /* Get initial prompt input. */ 102 if ((inputs = args_get(args, 'I')) != NULL) { 103 cdata->inputs = xstrdup(inputs); 104 cdata->next_input = cdata->inputs; 105 input = strsep(&cdata->next_input, ","); 106 } 107 108 status_prompt_set(c, prompt, input, cmd_command_prompt_callback, 109 cmd_command_prompt_free, cdata, 0); 110 free(prompt); 111 112 return (CMD_RETURN_NORMAL); 113 } 114 115 int 116 cmd_command_prompt_callback(void *data, const char *s) 117 { 118 struct cmd_command_prompt_cdata *cdata = data; 119 struct client *c = cdata->c; 120 struct cmd_list *cmdlist; 121 char *cause, *new_template, *prompt, *ptr; 122 char *input = NULL; 123 124 if (s == NULL) 125 return (0); 126 127 new_template = cmd_template_replace(cdata->template, s, cdata->idx); 128 free(cdata->template); 129 cdata->template = new_template; 130 131 /* 132 * Check if there are more prompts; if so, get its respective input 133 * and update the prompt data. 134 */ 135 if ((ptr = strsep(&cdata->next_prompt, ",")) != NULL) { 136 xasprintf(&prompt, "%s ", ptr); 137 input = strsep(&cdata->next_input, ","); 138 status_prompt_update(c, prompt, input); 139 140 free(prompt); 141 cdata->idx++; 142 return (1); 143 } 144 145 if (cmd_string_parse(new_template, &cmdlist, NULL, 0, &cause) != 0) { 146 if (cause != NULL) { 147 *cause = toupper((u_char) *cause); 148 status_message_set(c, "%s", cause); 149 free(cause); 150 } 151 return (0); 152 } 153 154 cmdq_run(c->cmdq, cmdlist, NULL); 155 cmd_list_free(cmdlist); 156 157 if (c->prompt_callbackfn != (void *) &cmd_command_prompt_callback) 158 return (1); 159 return (0); 160 } 161 162 void 163 cmd_command_prompt_free(void *data) 164 { 165 struct cmd_command_prompt_cdata *cdata = data; 166 167 free(cdata->inputs); 168 free(cdata->prompts); 169 free(cdata->template); 170 free(cdata); 171 } 172