1 /* $OpenBSD: cmd-command-prompt.c,v 1.30 2015/12/13 14:32:38 nicm Exp $ */ 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 CMD_CLIENT_T, 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 = cmdq->state.c; 62 char *prompt, *ptr, *input = NULL; 63 size_t n; 64 65 if (c->prompt_string != NULL) 66 return (CMD_RETURN_NORMAL); 67 68 cdata = xmalloc(sizeof *cdata); 69 cdata->c = c; 70 cdata->idx = 1; 71 cdata->inputs = NULL; 72 cdata->next_input = NULL; 73 cdata->next_prompt = NULL; 74 cdata->prompts = NULL; 75 cdata->template = NULL; 76 77 if (args->argc != 0) 78 cdata->template = xstrdup(args->argv[0]); 79 else 80 cdata->template = xstrdup("%1"); 81 82 if ((prompts = args_get(args, 'p')) != NULL) 83 cdata->prompts = xstrdup(prompts); 84 else if (args->argc != 0) { 85 n = strcspn(cdata->template, " ,"); 86 xasprintf(&cdata->prompts, "(%.*s) ", (int) n, cdata->template); 87 } else 88 cdata->prompts = xstrdup(":"); 89 90 /* Get first prompt. */ 91 cdata->next_prompt = cdata->prompts; 92 ptr = strsep(&cdata->next_prompt, ","); 93 if (prompts == NULL) 94 prompt = xstrdup(ptr); 95 else 96 xasprintf(&prompt, "%s ", ptr); 97 98 /* Get initial prompt input. */ 99 if ((inputs = args_get(args, 'I')) != NULL) { 100 cdata->inputs = xstrdup(inputs); 101 cdata->next_input = cdata->inputs; 102 input = strsep(&cdata->next_input, ","); 103 } 104 105 status_prompt_set(c, prompt, input, cmd_command_prompt_callback, 106 cmd_command_prompt_free, cdata, 0); 107 free(prompt); 108 109 return (CMD_RETURN_NORMAL); 110 } 111 112 int 113 cmd_command_prompt_callback(void *data, const char *s) 114 { 115 struct cmd_command_prompt_cdata *cdata = data; 116 struct client *c = cdata->c; 117 struct cmd_list *cmdlist; 118 char *cause, *new_template, *prompt, *ptr; 119 char *input = NULL; 120 121 if (s == NULL) 122 return (0); 123 124 new_template = cmd_template_replace(cdata->template, s, cdata->idx); 125 free(cdata->template); 126 cdata->template = new_template; 127 128 /* 129 * Check if there are more prompts; if so, get its respective input 130 * and update the prompt data. 131 */ 132 if ((ptr = strsep(&cdata->next_prompt, ",")) != NULL) { 133 xasprintf(&prompt, "%s ", ptr); 134 input = strsep(&cdata->next_input, ","); 135 status_prompt_update(c, prompt, input); 136 137 free(prompt); 138 cdata->idx++; 139 return (1); 140 } 141 142 if (cmd_string_parse(new_template, &cmdlist, NULL, 0, &cause) != 0) { 143 if (cause != NULL) { 144 *cause = toupper((u_char) *cause); 145 status_message_set(c, "%s", cause); 146 free(cause); 147 } 148 return (0); 149 } 150 151 cmdq_run(c->cmdq, cmdlist, NULL); 152 cmd_list_free(cmdlist); 153 154 if (c->prompt_callbackfn != (void *) &cmd_command_prompt_callback) 155 return (1); 156 return (0); 157 } 158 159 void 160 cmd_command_prompt_free(void *data) 161 { 162 struct cmd_command_prompt_cdata *cdata = data; 163 164 free(cdata->inputs); 165 free(cdata->prompts); 166 free(cdata->template); 167 free(cdata); 168 } 169