2009-06-01 22:58:49 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2009-06-01 22:58:49 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
2012-07-10 11:53:01 +00:00
|
|
|
#include <stdlib.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
#include <string.h>
|
2011-07-02 21:05:44 +00:00
|
|
|
#include <time.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prompt for command in client.
|
|
|
|
*/
|
|
|
|
|
2021-08-25 08:51:55 +00:00
|
|
|
static enum args_parse_type cmd_command_prompt_args_parse(struct args *,
|
|
|
|
u_int, char **);
|
|
|
|
static enum cmd_retval cmd_command_prompt_exec(struct cmd *,
|
|
|
|
struct cmdq_item *);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2017-05-17 15:20:23 +00:00
|
|
|
static int cmd_command_prompt_callback(struct client *, void *,
|
|
|
|
const char *, int);
|
2016-10-10 21:51:39 +00:00
|
|
|
static void cmd_command_prompt_free(void *);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
const struct cmd_entry cmd_command_prompt_entry = {
|
2015-12-13 21:53:57 +00:00
|
|
|
.name = "command-prompt",
|
|
|
|
.alias = NULL,
|
|
|
|
|
2021-08-25 08:51:55 +00:00
|
|
|
.args = { "1bFkiI:Np:t:T:", 0, 1, cmd_command_prompt_args_parse },
|
2021-08-13 06:50:42 +00:00
|
|
|
.usage = "[-1bFkiN] [-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE
|
2021-06-10 07:50:03 +00:00
|
|
|
" [-T type] [template]",
|
2015-12-13 21:53:57 +00:00
|
|
|
|
2020-04-13 20:51:57 +00:00
|
|
|
.flags = CMD_CLIENT_TFLAG,
|
2015-12-13 21:53:57 +00:00
|
|
|
.exec = cmd_command_prompt_exec
|
2009-08-19 10:39:50 +00:00
|
|
|
};
|
|
|
|
|
2021-08-23 12:33:55 +00:00
|
|
|
struct cmd_command_prompt_prompt {
|
|
|
|
char *input;
|
|
|
|
char *prompt;
|
|
|
|
};
|
2017-01-06 11:57:03 +00:00
|
|
|
|
2021-08-23 12:33:55 +00:00
|
|
|
struct cmd_command_prompt_cdata {
|
|
|
|
struct cmdq_item *item;
|
|
|
|
struct args_command_state *state;
|
2017-01-06 11:57:03 +00:00
|
|
|
|
2021-08-23 12:33:55 +00:00
|
|
|
int flags;
|
|
|
|
enum prompt_type prompt_type;
|
2017-01-06 11:57:03 +00:00
|
|
|
|
2021-08-23 12:33:55 +00:00
|
|
|
struct cmd_command_prompt_prompt *prompts;
|
|
|
|
u_int count;
|
|
|
|
u_int current;
|
2021-08-13 06:50:42 +00:00
|
|
|
|
2021-08-23 12:33:55 +00:00
|
|
|
int argc;
|
|
|
|
char **argv;
|
2009-06-01 22:58:49 +00:00
|
|
|
};
|
|
|
|
|
2021-08-25 08:51:55 +00:00
|
|
|
static enum args_parse_type
|
|
|
|
cmd_command_prompt_args_parse(__unused struct args *args, __unused u_int idx,
|
|
|
|
__unused char **cause)
|
|
|
|
{
|
|
|
|
return (ARGS_PARSE_COMMANDS_OR_STRING);
|
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_command_prompt_exec(struct cmd *self, struct cmdq_item *item)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2020-04-13 08:26:27 +00:00
|
|
|
struct args *args = cmd_get_args(self);
|
2020-04-13 20:51:57 +00:00
|
|
|
struct client *tc = cmdq_get_target_client(item);
|
2020-05-16 16:16:07 +00:00
|
|
|
struct cmd_find_state *target = cmdq_get_target(item);
|
2021-08-23 12:33:55 +00:00
|
|
|
const char *type, *s, *input;
|
2009-08-19 10:39:50 +00:00
|
|
|
struct cmd_command_prompt_cdata *cdata;
|
2021-08-23 12:33:55 +00:00
|
|
|
char *tmp, *prompts, *prompt, *next_prompt;
|
2021-08-25 06:36:05 +00:00
|
|
|
char *inputs = NULL, *next_input;
|
2021-08-20 19:50:16 +00:00
|
|
|
u_int count = args_count(args);
|
2021-08-23 12:33:55 +00:00
|
|
|
int wait = !args_has(args, 'b'), space = 1;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2020-04-13 20:51:57 +00:00
|
|
|
if (tc->prompt_string != NULL)
|
2012-07-11 07:10:15 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
2021-08-17 19:26:42 +00:00
|
|
|
if (args_has(args, 'i'))
|
|
|
|
wait = 0;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2017-01-06 11:57:03 +00:00
|
|
|
cdata = xcalloc(1, sizeof *cdata);
|
2021-08-13 06:50:42 +00:00
|
|
|
if (wait)
|
|
|
|
cdata->item = item;
|
2021-08-23 12:33:55 +00:00
|
|
|
cdata->state = args_make_commands_prepare(self, item, 0, "%1", wait,
|
|
|
|
args_has(args, 'F'));
|
|
|
|
|
|
|
|
if ((s = args_get(args, 'p')) == NULL) {
|
|
|
|
if (count != 0) {
|
|
|
|
tmp = args_make_commands_get_command(cdata->state);
|
|
|
|
xasprintf(&prompts, "(%s)", tmp);
|
|
|
|
free(tmp);
|
|
|
|
} else {
|
|
|
|
prompts = xstrdup(":");
|
|
|
|
space = 0;
|
|
|
|
}
|
|
|
|
next_prompt = prompts;
|
2009-08-19 10:39:50 +00:00
|
|
|
} else
|
2022-05-30 12:55:25 +00:00
|
|
|
next_prompt = prompts = xstrdup(s);
|
2021-08-23 12:33:55 +00:00
|
|
|
if ((s = args_get(args, 'I')) != NULL)
|
|
|
|
next_input = inputs = xstrdup(s);
|
2011-07-08 06:37:57 +00:00
|
|
|
else
|
2021-08-23 12:33:55 +00:00
|
|
|
next_input = NULL;
|
|
|
|
while ((prompt = strsep(&next_prompt, ",")) != NULL) {
|
|
|
|
cdata->prompts = xreallocarray(cdata->prompts, cdata->count + 1,
|
|
|
|
sizeof *cdata->prompts);
|
|
|
|
if (!space)
|
|
|
|
tmp = xstrdup(prompt);
|
|
|
|
else
|
|
|
|
xasprintf(&tmp, "%s ", prompt);
|
|
|
|
cdata->prompts[cdata->count].prompt = tmp;
|
|
|
|
|
|
|
|
if (next_input != NULL) {
|
|
|
|
input = strsep(&next_input, ",");
|
|
|
|
if (input == NULL)
|
|
|
|
input = "";
|
|
|
|
} else
|
|
|
|
input = "";
|
|
|
|
cdata->prompts[cdata->count].input = xstrdup(input);
|
|
|
|
|
|
|
|
cdata->count++;
|
2011-07-02 21:05:44 +00:00
|
|
|
}
|
2021-08-23 12:33:55 +00:00
|
|
|
free(inputs);
|
|
|
|
free(prompts);
|
2011-07-02 21:05:44 +00:00
|
|
|
|
2021-06-10 07:50:03 +00:00
|
|
|
if ((type = args_get(args, 'T')) != NULL) {
|
|
|
|
cdata->prompt_type = status_prompt_type(type);
|
|
|
|
if (cdata->prompt_type == PROMPT_TYPE_INVALID) {
|
|
|
|
cmdq_error(item, "unknown type: %s", type);
|
2024-04-15 08:19:55 +00:00
|
|
|
cmd_command_prompt_free(cdata);
|
2021-06-10 07:50:03 +00:00
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
cdata->prompt_type = PROMPT_TYPE_COMMAND;
|
|
|
|
|
2016-10-11 07:23:34 +00:00
|
|
|
if (args_has(args, '1'))
|
2017-01-06 11:57:03 +00:00
|
|
|
cdata->flags |= PROMPT_SINGLE;
|
2016-10-12 13:03:27 +00:00
|
|
|
else if (args_has(args, 'N'))
|
2017-01-06 11:57:03 +00:00
|
|
|
cdata->flags |= PROMPT_NUMERIC;
|
|
|
|
else if (args_has(args, 'i'))
|
|
|
|
cdata->flags |= PROMPT_INCREMENTAL;
|
2020-01-27 08:53:13 +00:00
|
|
|
else if (args_has(args, 'k'))
|
|
|
|
cdata->flags |= PROMPT_KEY;
|
2021-08-23 12:33:55 +00:00
|
|
|
status_prompt_set(tc, target, cdata->prompts[0].prompt,
|
|
|
|
cdata->prompts[0].input, cmd_command_prompt_callback,
|
|
|
|
cmd_command_prompt_free, cdata, cdata->flags, cdata->prompt_type);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2021-08-13 06:50:42 +00:00
|
|
|
if (!wait)
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
return (CMD_RETURN_WAIT);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static int
|
2017-05-17 15:20:23 +00:00
|
|
|
cmd_command_prompt_callback(struct client *c, void *data, const char *s,
|
|
|
|
int done)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2021-08-27 17:25:55 +00:00
|
|
|
struct cmd_command_prompt_cdata *cdata = data;
|
|
|
|
char *error;
|
|
|
|
struct cmdq_item *item = cdata->item, *new_item;
|
|
|
|
struct cmd_list *cmdlist;
|
|
|
|
struct cmd_command_prompt_prompt *prompt;
|
|
|
|
int argc = 0;
|
|
|
|
char **argv = NULL;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-08-19 10:39:50 +00:00
|
|
|
if (s == NULL)
|
2021-08-13 06:50:42 +00:00
|
|
|
goto out;
|
2023-03-15 08:15:39 +00:00
|
|
|
|
2017-01-06 11:57:03 +00:00
|
|
|
if (done) {
|
2021-08-23 12:33:55 +00:00
|
|
|
if (cdata->flags & PROMPT_INCREMENTAL)
|
|
|
|
goto out;
|
2021-09-22 15:21:44 +00:00
|
|
|
cmd_append_argv(&cdata->argc, &cdata->argv, s);
|
2021-08-23 12:33:55 +00:00
|
|
|
if (++cdata->current != cdata->count) {
|
|
|
|
prompt = &cdata->prompts[cdata->current];
|
|
|
|
status_prompt_update(c, prompt->prompt, prompt->input);
|
|
|
|
return (1);
|
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2021-08-27 17:25:55 +00:00
|
|
|
argc = cdata->argc;
|
|
|
|
argv = cmd_copy_argv(cdata->argc, cdata->argv);
|
2023-03-15 08:15:39 +00:00
|
|
|
if (!done)
|
|
|
|
cmd_append_argv(&argc, &argv, s);
|
|
|
|
|
2021-08-27 17:25:55 +00:00
|
|
|
if (done) {
|
2023-03-15 08:15:39 +00:00
|
|
|
cmd_free_argv(cdata->argc, cdata->argv);
|
2021-08-27 17:25:55 +00:00
|
|
|
cdata->argc = argc;
|
|
|
|
cdata->argv = cmd_copy_argv(argc, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdlist = args_make_commands(cdata->state, argc, argv, &error);
|
2021-08-23 12:33:55 +00:00
|
|
|
if (cmdlist == NULL) {
|
2020-04-13 18:59:41 +00:00
|
|
|
cmdq_append(c, cmdq_get_error(error));
|
|
|
|
free(error);
|
2021-08-23 12:33:55 +00:00
|
|
|
} else if (item == NULL) {
|
|
|
|
new_item = cmdq_get_command(cmdlist, NULL);
|
|
|
|
cmdq_append(c, new_item);
|
|
|
|
} else {
|
|
|
|
new_item = cmdq_get_command(cmdlist, cmdq_get_state(item));
|
|
|
|
cmdq_insert_after(item, new_item);
|
2019-05-23 11:13:30 +00:00
|
|
|
}
|
2021-08-27 17:25:55 +00:00
|
|
|
cmd_free_argv(argc, argv);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2017-05-17 15:20:23 +00:00
|
|
|
if (c->prompt_inputcb != cmd_command_prompt_callback)
|
2009-06-01 22:58:49 +00:00
|
|
|
return (1);
|
2021-08-13 06:50:42 +00:00
|
|
|
|
|
|
|
out:
|
2021-08-25 08:51:55 +00:00
|
|
|
if (item != NULL)
|
|
|
|
cmdq_continue(item);
|
2009-06-01 22:58:49 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2009-07-17 06:13:27 +00:00
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static void
|
2011-01-04 00:42:46 +00:00
|
|
|
cmd_command_prompt_free(void *data)
|
2009-07-17 06:13:27 +00:00
|
|
|
{
|
2009-08-19 10:39:50 +00:00
|
|
|
struct cmd_command_prompt_cdata *cdata = data;
|
2021-08-23 12:33:55 +00:00
|
|
|
u_int i;
|
2009-07-17 06:13:27 +00:00
|
|
|
|
2021-08-23 12:33:55 +00:00
|
|
|
for (i = 0; i < cdata->count; i++) {
|
|
|
|
free(cdata->prompts[i].prompt);
|
|
|
|
free(cdata->prompts[i].input);
|
|
|
|
}
|
2012-07-10 11:53:01 +00:00
|
|
|
free(cdata->prompts);
|
2021-08-23 12:33:55 +00:00
|
|
|
cmd_free_argv(cdata->argc, cdata->argv);
|
|
|
|
args_make_commands_free(cdata->state);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(cdata);
|
2009-07-17 06:13:27 +00:00
|
|
|
}
|