cmd-command-prompt.c revision 1.1.1.8 1 1.1.1.5 christos /* $OpenBSD$ */
2 1.1 jmmv
3 1.1 jmmv /*
4 1.1.1.6 christos * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott (at) gmail.com>
5 1.1 jmmv *
6 1.1 jmmv * Permission to use, copy, modify, and distribute this software for any
7 1.1 jmmv * purpose with or without fee is hereby granted, provided that the above
8 1.1 jmmv * copyright notice and this permission notice appear in all copies.
9 1.1 jmmv *
10 1.1 jmmv * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 1.1 jmmv * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 1.1 jmmv * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 1.1 jmmv * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 1.1 jmmv * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 1.1 jmmv * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 1.1 jmmv * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1.1 jmmv */
18 1.1 jmmv
19 1.1 jmmv #include <sys/types.h>
20 1.1 jmmv
21 1.1 jmmv #include <ctype.h>
22 1.1.1.3 christos #include <stdlib.h>
23 1.1 jmmv #include <string.h>
24 1.1.1.2 jmmv #include <time.h>
25 1.1 jmmv
26 1.1 jmmv #include "tmux.h"
27 1.1 jmmv
28 1.1 jmmv /*
29 1.1 jmmv * Prompt for command in client.
30 1.1 jmmv */
31 1.1 jmmv
32 1.1.1.7 christos static enum cmd_retval cmd_command_prompt_exec(struct cmd *,
33 1.1.1.7 christos struct cmdq_item *);
34 1.1 jmmv
35 1.1.1.8 christos static int cmd_command_prompt_callback(struct client *, void *,
36 1.1.1.8 christos const char *, int);
37 1.1.1.7 christos static void cmd_command_prompt_free(void *);
38 1.1 jmmv
39 1.1 jmmv const struct cmd_entry cmd_command_prompt_entry = {
40 1.1.1.6 christos .name = "command-prompt",
41 1.1.1.6 christos .alias = NULL,
42 1.1.1.6 christos
43 1.1.1.7 christos .args = { "1iI:Np:t:", 0, 1 },
44 1.1.1.7 christos .usage = "[-1Ni] [-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE " "
45 1.1.1.6 christos "[template]",
46 1.1.1.6 christos
47 1.1.1.6 christos .flags = 0,
48 1.1.1.6 christos .exec = cmd_command_prompt_exec
49 1.1 jmmv };
50 1.1 jmmv
51 1.1 jmmv struct cmd_command_prompt_cdata {
52 1.1.1.8 christos int flags;
53 1.1.1.7 christos
54 1.1.1.8 christos char *inputs;
55 1.1.1.8 christos char *next_input;
56 1.1.1.7 christos
57 1.1.1.8 christos char *prompts;
58 1.1.1.8 christos char *next_prompt;
59 1.1.1.7 christos
60 1.1.1.8 christos char *template;
61 1.1.1.8 christos int idx;
62 1.1 jmmv };
63 1.1 jmmv
64 1.1.1.7 christos static enum cmd_retval
65 1.1.1.7 christos cmd_command_prompt_exec(struct cmd *self, struct cmdq_item *item)
66 1.1 jmmv {
67 1.1.1.2 jmmv struct args *args = self->args;
68 1.1.1.2 jmmv const char *inputs, *prompts;
69 1.1 jmmv struct cmd_command_prompt_cdata *cdata;
70 1.1.1.8 christos struct client *c;
71 1.1.1.2 jmmv char *prompt, *ptr, *input = NULL;
72 1.1 jmmv size_t n;
73 1.1 jmmv
74 1.1.1.8 christos if ((c = cmd_find_client(item, args_get(args, 't'), 0)) == NULL)
75 1.1.1.8 christos return (CMD_RETURN_ERROR);
76 1.1.1.8 christos
77 1.1 jmmv if (c->prompt_string != NULL)
78 1.1.1.3 christos return (CMD_RETURN_NORMAL);
79 1.1 jmmv
80 1.1.1.7 christos cdata = xcalloc(1, sizeof *cdata);
81 1.1.1.7 christos
82 1.1.1.2 jmmv cdata->inputs = NULL;
83 1.1.1.2 jmmv cdata->next_input = NULL;
84 1.1.1.7 christos
85 1.1 jmmv cdata->prompts = NULL;
86 1.1.1.7 christos cdata->next_prompt = NULL;
87 1.1.1.7 christos
88 1.1 jmmv cdata->template = NULL;
89 1.1.1.7 christos cdata->idx = 1;
90 1.1 jmmv
91 1.1.1.2 jmmv if (args->argc != 0)
92 1.1.1.2 jmmv cdata->template = xstrdup(args->argv[0]);
93 1.1 jmmv else
94 1.1 jmmv cdata->template = xstrdup("%1");
95 1.1.1.2 jmmv
96 1.1.1.2 jmmv if ((prompts = args_get(args, 'p')) != NULL)
97 1.1.1.2 jmmv cdata->prompts = xstrdup(prompts);
98 1.1.1.2 jmmv else if (args->argc != 0) {
99 1.1.1.2 jmmv n = strcspn(cdata->template, " ,");
100 1.1.1.2 jmmv xasprintf(&cdata->prompts, "(%.*s) ", (int) n, cdata->template);
101 1.1 jmmv } else
102 1.1 jmmv cdata->prompts = xstrdup(":");
103 1.1 jmmv
104 1.1.1.2 jmmv /* Get first prompt. */
105 1.1 jmmv cdata->next_prompt = cdata->prompts;
106 1.1 jmmv ptr = strsep(&cdata->next_prompt, ",");
107 1.1.1.2 jmmv if (prompts == NULL)
108 1.1 jmmv prompt = xstrdup(ptr);
109 1.1 jmmv else
110 1.1 jmmv xasprintf(&prompt, "%s ", ptr);
111 1.1 jmmv
112 1.1.1.2 jmmv /* Get initial prompt input. */
113 1.1.1.2 jmmv if ((inputs = args_get(args, 'I')) != NULL) {
114 1.1.1.2 jmmv cdata->inputs = xstrdup(inputs);
115 1.1.1.2 jmmv cdata->next_input = cdata->inputs;
116 1.1.1.2 jmmv input = strsep(&cdata->next_input, ",");
117 1.1.1.2 jmmv }
118 1.1 jmmv
119 1.1.1.7 christos if (args_has(args, '1'))
120 1.1.1.7 christos cdata->flags |= PROMPT_SINGLE;
121 1.1.1.7 christos else if (args_has(args, 'N'))
122 1.1.1.7 christos cdata->flags |= PROMPT_NUMERIC;
123 1.1.1.7 christos else if (args_has(args, 'i'))
124 1.1.1.7 christos cdata->flags |= PROMPT_INCREMENTAL;
125 1.1.1.2 jmmv status_prompt_set(c, prompt, input, cmd_command_prompt_callback,
126 1.1.1.7 christos cmd_command_prompt_free, cdata, cdata->flags);
127 1.1.1.3 christos free(prompt);
128 1.1 jmmv
129 1.1.1.3 christos return (CMD_RETURN_NORMAL);
130 1.1 jmmv }
131 1.1 jmmv
132 1.1.1.7 christos static enum cmd_retval
133 1.1.1.7 christos cmd_command_prompt_error(struct cmdq_item *item, void *data)
134 1.1.1.7 christos {
135 1.1.1.7 christos char *error = data;
136 1.1.1.7 christos
137 1.1.1.7 christos cmdq_error(item, "%s", error);
138 1.1.1.7 christos free(error);
139 1.1.1.7 christos
140 1.1.1.7 christos return (CMD_RETURN_NORMAL);
141 1.1.1.7 christos }
142 1.1.1.7 christos
143 1.1.1.7 christos static int
144 1.1.1.8 christos cmd_command_prompt_callback(struct client *c, void *data, const char *s,
145 1.1.1.8 christos int done)
146 1.1 jmmv {
147 1.1 jmmv struct cmd_command_prompt_cdata *cdata = data;
148 1.1 jmmv struct cmd_list *cmdlist;
149 1.1.1.7 christos struct cmdq_item *new_item;
150 1.1.1.2 jmmv char *cause, *new_template, *prompt, *ptr;
151 1.1.1.2 jmmv char *input = NULL;
152 1.1 jmmv
153 1.1 jmmv if (s == NULL)
154 1.1 jmmv return (0);
155 1.1.1.7 christos if (done && (cdata->flags & PROMPT_INCREMENTAL))
156 1.1.1.7 christos return (0);
157 1.1 jmmv
158 1.1.1.2 jmmv new_template = cmd_template_replace(cdata->template, s, cdata->idx);
159 1.1.1.7 christos if (done) {
160 1.1.1.7 christos free(cdata->template);
161 1.1.1.7 christos cdata->template = new_template;
162 1.1.1.7 christos }
163 1.1 jmmv
164 1.1.1.2 jmmv /*
165 1.1.1.2 jmmv * Check if there are more prompts; if so, get its respective input
166 1.1.1.2 jmmv * and update the prompt data.
167 1.1.1.2 jmmv */
168 1.1.1.7 christos if (done && (ptr = strsep(&cdata->next_prompt, ",")) != NULL) {
169 1.1 jmmv xasprintf(&prompt, "%s ", ptr);
170 1.1.1.2 jmmv input = strsep(&cdata->next_input, ",");
171 1.1.1.2 jmmv status_prompt_update(c, prompt, input);
172 1.1.1.2 jmmv
173 1.1.1.3 christos free(prompt);
174 1.1 jmmv cdata->idx++;
175 1.1 jmmv return (1);
176 1.1 jmmv }
177 1.1 jmmv
178 1.1.1.7 christos cmdlist = cmd_string_parse(new_template, NULL, 0, &cause);
179 1.1.1.7 christos if (cmdlist == NULL) {
180 1.1 jmmv if (cause != NULL) {
181 1.1.1.7 christos new_item = cmdq_get_callback(cmd_command_prompt_error,
182 1.1.1.7 christos cause);
183 1.1.1.7 christos } else
184 1.1.1.7 christos new_item = NULL;
185 1.1.1.7 christos } else {
186 1.1.1.7 christos new_item = cmdq_get_command(cmdlist, NULL, NULL, 0);
187 1.1.1.7 christos cmd_list_free(cmdlist);
188 1.1 jmmv }
189 1.1 jmmv
190 1.1.1.7 christos if (new_item != NULL)
191 1.1.1.7 christos cmdq_append(c, new_item);
192 1.1 jmmv
193 1.1.1.7 christos if (!done)
194 1.1.1.7 christos free(new_template);
195 1.1.1.8 christos if (c->prompt_inputcb != cmd_command_prompt_callback)
196 1.1 jmmv return (1);
197 1.1 jmmv return (0);
198 1.1 jmmv }
199 1.1 jmmv
200 1.1.1.7 christos static void
201 1.1.1.2 jmmv cmd_command_prompt_free(void *data)
202 1.1 jmmv {
203 1.1 jmmv struct cmd_command_prompt_cdata *cdata = data;
204 1.1 jmmv
205 1.1.1.3 christos free(cdata->inputs);
206 1.1.1.3 christos free(cdata->prompts);
207 1.1.1.3 christos free(cdata->template);
208 1.1.1.3 christos free(cdata);
209 1.1 jmmv }
210