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>
29 * Prompt for command in client.
32 static enum cmd_retval
cmd_command_prompt_exec(struct cmd
*,
35 static int cmd_command_prompt_callback(struct client
*, void *,
37 static void cmd_command_prompt_free(void *);
39 const struct cmd_entry cmd_command_prompt_entry
= {
40 .name
= "command-prompt",
43 .args
= { "1bFkiI:Np:t:T:", 0, 1, NULL
},
44 .usage
= "[-1bFkiN] [-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE
45 " [-T type] [template]",
47 .flags
= CMD_CLIENT_TFLAG
,
48 .exec
= cmd_command_prompt_exec
51 struct cmd_command_prompt_cdata
{
52 struct cmdq_item
*item
;
53 struct cmd_parse_input pi
;
56 enum prompt_type prompt_type
;
68 static enum cmd_retval
69 cmd_command_prompt_exec(struct cmd
*self
, struct cmdq_item
*item
)
71 struct args
*args
= cmd_get_args(self
);
72 struct client
*tc
= cmdq_get_target_client(item
);
73 struct cmd_find_state
*target
= cmdq_get_target(item
);
74 const char *inputs
, *prompts
, *type
, *s
;
75 struct cmd_command_prompt_cdata
*cdata
;
76 char *prompt
, *comma
, *input
= NULL
;
77 u_int count
= args_count(args
);
79 int wait
= !args_has(args
, 'b');
81 if (tc
->prompt_string
!= NULL
)
82 return (CMD_RETURN_NORMAL
);
83 if (args_has(args
, 'i'))
86 cdata
= xcalloc(1, sizeof *cdata
);
89 cmd_get_source(self
, &cdata
->pi
.file
, &cdata
->pi
.line
);
91 cdata
->pi
.item
= item
;
93 cmd_find_copy_state(&cdata
->pi
.fs
, target
);
99 s
= args_string(args
, 0);
100 if (args_has(args
, 'F'))
101 cdata
->template = format_single_from_target(item
, s
);
103 cdata
->template = xstrdup(s
);
105 cdata
->template = xstrdup("%1");
107 if ((prompts
= args_get(args
, 'p')) != NULL
)
108 cdata
->prompts
= xstrdup(prompts
);
109 else if (count
!= 0) {
110 n
= strcspn(cdata
->template, " ,");
111 xasprintf(&cdata
->prompts
, "(%.*s) ", (int)n
, cdata
->template);
113 cdata
->prompts
= xstrdup(":");
115 /* Get first prompt. */
116 cdata
->next_prompt
= cdata
->prompts
;
117 comma
= strsep(&cdata
->next_prompt
, ",");
119 prompt
= xstrdup(comma
);
121 xasprintf(&prompt
, "%s ", comma
);
123 /* Get initial prompt input. */
124 if ((inputs
= args_get(args
, 'I')) != NULL
) {
125 cdata
->inputs
= xstrdup(inputs
);
126 cdata
->next_input
= cdata
->inputs
;
127 input
= strsep(&cdata
->next_input
, ",");
130 /* Get prompt type. */
131 if ((type
= args_get(args
, 'T')) != NULL
) {
132 cdata
->prompt_type
= status_prompt_type(type
);
133 if (cdata
->prompt_type
== PROMPT_TYPE_INVALID
) {
134 cmdq_error(item
, "unknown type: %s", type
);
135 return (CMD_RETURN_ERROR
);
138 cdata
->prompt_type
= PROMPT_TYPE_COMMAND
;
140 if (args_has(args
, '1'))
141 cdata
->flags
|= PROMPT_SINGLE
;
142 else if (args_has(args
, 'N'))
143 cdata
->flags
|= PROMPT_NUMERIC
;
144 else if (args_has(args
, 'i'))
145 cdata
->flags
|= PROMPT_INCREMENTAL
;
146 else if (args_has(args
, 'k'))
147 cdata
->flags
|= PROMPT_KEY
;
148 status_prompt_set(tc
, target
, prompt
, input
,
149 cmd_command_prompt_callback
, cmd_command_prompt_free
, cdata
,
150 cdata
->flags
, cdata
->prompt_type
);
154 return (CMD_RETURN_NORMAL
);
155 return (CMD_RETURN_WAIT
);
159 cmd_command_prompt_callback(struct client
*c
, void *data
, const char *s
,
162 struct cmd_command_prompt_cdata
*cdata
= data
;
163 char *new_template
, *prompt
, *comma
, *error
;
165 struct cmdq_item
*item
= cdata
->item
;
166 enum cmd_parse_status status
;
170 if (done
&& (cdata
->flags
& PROMPT_INCREMENTAL
))
173 new_template
= cmd_template_replace(cdata
->template, s
, cdata
->idx
);
175 free(cdata
->template);
176 cdata
->template = new_template
;
180 * Check if there are more prompts; if so, get its respective input
181 * and update the prompt data.
183 if (done
&& (comma
= strsep(&cdata
->next_prompt
, ",")) != NULL
) {
184 xasprintf(&prompt
, "%s ", comma
);
185 input
= strsep(&cdata
->next_input
, ",");
186 status_prompt_update(c
, prompt
, input
);
194 status
= cmd_parse_and_insert(new_template
, &cdata
->pi
, item
,
195 cmdq_get_state(item
), &error
);
197 status
= cmd_parse_and_append(new_template
, &cdata
->pi
, c
, NULL
,
200 if (status
== CMD_PARSE_ERROR
) {
201 cmdq_append(c
, cmdq_get_error(error
));
207 if (c
->prompt_inputcb
!= cmd_command_prompt_callback
)
217 cmd_command_prompt_free(void *data
)
219 struct cmd_command_prompt_cdata
*cdata
= data
;
222 free(cdata
->prompts
);
223 free(cdata
->template);