Merge branch 'obsd-master'
[tmux.git] / cmd-command-prompt.c
blob60f7ca37dc6df6ff3eedd5d55548955920c292e6
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
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.
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.
19 #include <sys/types.h>
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <time.h>
26 #include "tmux.h"
29 * Prompt for command in client.
32 static enum args_parse_type cmd_command_prompt_args_parse(struct args *,
33 u_int, char **);
34 static enum cmd_retval cmd_command_prompt_exec(struct cmd *,
35 struct cmdq_item *);
37 static int cmd_command_prompt_callback(struct client *, void *,
38 const char *, int);
39 static void cmd_command_prompt_free(void *);
41 const struct cmd_entry cmd_command_prompt_entry = {
42 .name = "command-prompt",
43 .alias = NULL,
45 .args = { "1bFkiI:Np:t:T:", 0, 1, cmd_command_prompt_args_parse },
46 .usage = "[-1bFkiN] [-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE
47 " [-T type] [template]",
49 .flags = CMD_CLIENT_TFLAG,
50 .exec = cmd_command_prompt_exec
53 struct cmd_command_prompt_prompt {
54 char *input;
55 char *prompt;
58 struct cmd_command_prompt_cdata {
59 struct cmdq_item *item;
60 struct args_command_state *state;
62 int flags;
63 enum prompt_type prompt_type;
65 struct cmd_command_prompt_prompt *prompts;
66 u_int count;
67 u_int current;
69 int argc;
70 char **argv;
73 static enum args_parse_type
74 cmd_command_prompt_args_parse(__unused struct args *args, __unused u_int idx,
75 __unused char **cause)
77 return (ARGS_PARSE_COMMANDS_OR_STRING);
80 static enum cmd_retval
81 cmd_command_prompt_exec(struct cmd *self, struct cmdq_item *item)
83 struct args *args = cmd_get_args(self);
84 struct client *tc = cmdq_get_target_client(item);
85 struct cmd_find_state *target = cmdq_get_target(item);
86 const char *type, *s, *input;
87 struct cmd_command_prompt_cdata *cdata;
88 char *tmp, *prompts, *prompt, *next_prompt;
89 char *inputs = NULL, *next_input;
90 u_int count = args_count(args);
91 int wait = !args_has(args, 'b'), space = 1;
93 if (tc->prompt_string != NULL)
94 return (CMD_RETURN_NORMAL);
95 if (args_has(args, 'i'))
96 wait = 0;
98 cdata = xcalloc(1, sizeof *cdata);
99 if (wait)
100 cdata->item = item;
101 cdata->state = args_make_commands_prepare(self, item, 0, "%1", wait,
102 args_has(args, 'F'));
104 if ((s = args_get(args, 'p')) == NULL) {
105 if (count != 0) {
106 tmp = args_make_commands_get_command(cdata->state);
107 xasprintf(&prompts, "(%s)", tmp);
108 free(tmp);
109 } else {
110 prompts = xstrdup(":");
111 space = 0;
113 next_prompt = prompts;
114 } else
115 next_prompt = prompts = xstrdup(s);
116 if ((s = args_get(args, 'I')) != NULL)
117 next_input = inputs = xstrdup(s);
118 else
119 next_input = NULL;
120 while ((prompt = strsep(&next_prompt, ",")) != NULL) {
121 cdata->prompts = xreallocarray(cdata->prompts, cdata->count + 1,
122 sizeof *cdata->prompts);
123 if (!space)
124 tmp = xstrdup(prompt);
125 else
126 xasprintf(&tmp, "%s ", prompt);
127 cdata->prompts[cdata->count].prompt = tmp;
129 if (next_input != NULL) {
130 input = strsep(&next_input, ",");
131 if (input == NULL)
132 input = "";
133 } else
134 input = "";
135 cdata->prompts[cdata->count].input = xstrdup(input);
137 cdata->count++;
139 free(inputs);
140 free(prompts);
142 if ((type = args_get(args, 'T')) != NULL) {
143 cdata->prompt_type = status_prompt_type(type);
144 if (cdata->prompt_type == PROMPT_TYPE_INVALID) {
145 cmdq_error(item, "unknown type: %s", type);
146 cmd_command_prompt_free(cdata);
147 return (CMD_RETURN_ERROR);
149 } else
150 cdata->prompt_type = PROMPT_TYPE_COMMAND;
152 if (args_has(args, '1'))
153 cdata->flags |= PROMPT_SINGLE;
154 else if (args_has(args, 'N'))
155 cdata->flags |= PROMPT_NUMERIC;
156 else if (args_has(args, 'i'))
157 cdata->flags |= PROMPT_INCREMENTAL;
158 else if (args_has(args, 'k'))
159 cdata->flags |= PROMPT_KEY;
160 status_prompt_set(tc, target, cdata->prompts[0].prompt,
161 cdata->prompts[0].input, cmd_command_prompt_callback,
162 cmd_command_prompt_free, cdata, cdata->flags, cdata->prompt_type);
164 if (!wait)
165 return (CMD_RETURN_NORMAL);
166 return (CMD_RETURN_WAIT);
169 static int
170 cmd_command_prompt_callback(struct client *c, void *data, const char *s,
171 int done)
173 struct cmd_command_prompt_cdata *cdata = data;
174 char *error;
175 struct cmdq_item *item = cdata->item, *new_item;
176 struct cmd_list *cmdlist;
177 struct cmd_command_prompt_prompt *prompt;
178 int argc = 0;
179 char **argv = NULL;
181 if (s == NULL)
182 goto out;
184 if (done) {
185 if (cdata->flags & PROMPT_INCREMENTAL)
186 goto out;
187 cmd_append_argv(&cdata->argc, &cdata->argv, s);
188 if (++cdata->current != cdata->count) {
189 prompt = &cdata->prompts[cdata->current];
190 status_prompt_update(c, prompt->prompt, prompt->input);
191 return (1);
195 argc = cdata->argc;
196 argv = cmd_copy_argv(cdata->argc, cdata->argv);
197 if (!done)
198 cmd_append_argv(&argc, &argv, s);
200 if (done) {
201 cmd_free_argv(cdata->argc, cdata->argv);
202 cdata->argc = argc;
203 cdata->argv = cmd_copy_argv(argc, argv);
206 cmdlist = args_make_commands(cdata->state, argc, argv, &error);
207 if (cmdlist == NULL) {
208 cmdq_append(c, cmdq_get_error(error));
209 free(error);
210 } else if (item == NULL) {
211 new_item = cmdq_get_command(cmdlist, NULL);
212 cmdq_append(c, new_item);
213 } else {
214 new_item = cmdq_get_command(cmdlist, cmdq_get_state(item));
215 cmdq_insert_after(item, new_item);
217 cmd_free_argv(argc, argv);
219 if (c->prompt_inputcb != cmd_command_prompt_callback)
220 return (1);
222 out:
223 if (item != NULL)
224 cmdq_continue(item);
225 return (0);
228 static void
229 cmd_command_prompt_free(void *data)
231 struct cmd_command_prompt_cdata *cdata = data;
232 u_int i;
234 for (i = 0; i < cdata->count; i++) {
235 free(cdata->prompts[i].prompt);
236 free(cdata->prompts[i].input);
238 free(cdata->prompts);
239 cmd_free_argv(cdata->argc, cdata->argv);
240 args_make_commands_free(cdata->state);
241 free(cdata);