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.
|
|
|
|
*/
|
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
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-06-10 07:52:56 +00:00
|
|
|
.args = { "1FkiI:Np:t:T:", 0, 1 },
|
|
|
|
.usage = "[-1FkiN] [-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
|
|
|
};
|
|
|
|
|
|
|
|
struct cmd_command_prompt_cdata {
|
2021-06-10 07:50:03 +00:00
|
|
|
int flags;
|
|
|
|
enum prompt_type prompt_type;
|
2017-01-06 11:57:03 +00:00
|
|
|
|
2021-06-10 07:50:03 +00:00
|
|
|
char *inputs;
|
|
|
|
char *next_input;
|
2017-01-06 11:57:03 +00:00
|
|
|
|
2021-06-10 07:50:03 +00:00
|
|
|
char *prompts;
|
|
|
|
char *next_prompt;
|
2017-01-06 11:57:03 +00:00
|
|
|
|
2021-06-10 07:50:03 +00:00
|
|
|
char *template;
|
2021-06-10 07:52:56 +00:00
|
|
|
int idx;
|
2009-06-01 22:58:49 +00:00
|
|
|
};
|
|
|
|
|
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-06-10 07:50:03 +00:00
|
|
|
const char *inputs, *prompts, *type;
|
2009-08-19 10:39:50 +00:00
|
|
|
struct cmd_command_prompt_cdata *cdata;
|
2011-07-08 06:37:57 +00:00
|
|
|
char *prompt, *ptr, *input = NULL;
|
2009-08-19 10:39:50 +00:00
|
|
|
size_t n;
|
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);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2017-01-06 11:57:03 +00:00
|
|
|
cdata = xcalloc(1, sizeof *cdata);
|
|
|
|
|
2011-07-02 21:05:44 +00:00
|
|
|
cdata->inputs = NULL;
|
|
|
|
cdata->next_input = NULL;
|
2017-01-06 11:57:03 +00:00
|
|
|
|
2009-08-19 10:39:50 +00:00
|
|
|
cdata->prompts = NULL;
|
2017-01-06 11:57:03 +00:00
|
|
|
cdata->next_prompt = NULL;
|
|
|
|
|
2009-08-19 10:39:50 +00:00
|
|
|
cdata->template = NULL;
|
2017-01-06 11:57:03 +00:00
|
|
|
cdata->idx = 1;
|
2009-08-19 10:39:50 +00:00
|
|
|
|
2021-06-10 07:52:56 +00:00
|
|
|
if (args->argc != 0 && args_has(args, 'F'))
|
|
|
|
cdata->template = format_single_from_target(item, args->argv[0]);
|
|
|
|
else if (args->argc != 0)
|
2011-01-04 00:42:46 +00:00
|
|
|
cdata->template = xstrdup(args->argv[0]);
|
2009-08-19 10:39:50 +00:00
|
|
|
else
|
|
|
|
cdata->template = xstrdup("%1");
|
2011-01-04 00:42:46 +00:00
|
|
|
|
2011-07-02 21:05:44 +00:00
|
|
|
if ((prompts = args_get(args, 'p')) != NULL)
|
2011-01-04 00:42:46 +00:00
|
|
|
cdata->prompts = xstrdup(prompts);
|
|
|
|
else if (args->argc != 0) {
|
|
|
|
n = strcspn(cdata->template, " ,");
|
|
|
|
xasprintf(&cdata->prompts, "(%.*s) ", (int) n, cdata->template);
|
2009-08-19 10:39:50 +00:00
|
|
|
} else
|
|
|
|
cdata->prompts = xstrdup(":");
|
|
|
|
|
2011-07-02 21:05:44 +00:00
|
|
|
/* Get first prompt. */
|
2009-08-19 10:39:50 +00:00
|
|
|
cdata->next_prompt = cdata->prompts;
|
|
|
|
ptr = strsep(&cdata->next_prompt, ",");
|
2011-01-04 00:42:46 +00:00
|
|
|
if (prompts == NULL)
|
2009-08-19 10:39:50 +00:00
|
|
|
prompt = xstrdup(ptr);
|
2011-07-08 06:37:57 +00:00
|
|
|
else
|
|
|
|
xasprintf(&prompt, "%s ", ptr);
|
2011-07-02 21:05:44 +00:00
|
|
|
|
|
|
|
/* Get initial prompt input. */
|
|
|
|
if ((inputs = args_get(args, 'I')) != NULL) {
|
|
|
|
cdata->inputs = xstrdup(inputs);
|
|
|
|
cdata->next_input = cdata->inputs;
|
2011-07-08 06:37:57 +00:00
|
|
|
input = strsep(&cdata->next_input, ",");
|
2011-07-02 21:05:44 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 07:50:03 +00:00
|
|
|
/* Get prompt type. */
|
|
|
|
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);
|
|
|
|
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;
|
2020-05-16 16:16:07 +00:00
|
|
|
status_prompt_set(tc, target, prompt, input,
|
|
|
|
cmd_command_prompt_callback, cmd_command_prompt_free, cdata,
|
2021-06-10 07:50:03 +00:00
|
|
|
cdata->flags, cdata->prompt_type);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(prompt);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2012-07-11 07:10:15 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
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
|
|
|
{
|
2009-08-19 10:39:50 +00:00
|
|
|
struct cmd_command_prompt_cdata *cdata = data;
|
2020-04-13 18:59:41 +00:00
|
|
|
char *new_template, *prompt, *ptr, *error;
|
2011-07-08 06:37:57 +00:00
|
|
|
char *input = NULL;
|
2020-04-13 18:59:41 +00:00
|
|
|
enum cmd_parse_status status;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-08-19 10:39:50 +00:00
|
|
|
if (s == NULL)
|
2009-06-01 22:58:49 +00:00
|
|
|
return (0);
|
2017-01-06 11:57:03 +00:00
|
|
|
if (done && (cdata->flags & PROMPT_INCREMENTAL))
|
|
|
|
return (0);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2011-07-02 21:05:44 +00:00
|
|
|
new_template = cmd_template_replace(cdata->template, s, cdata->idx);
|
2017-01-06 11:57:03 +00:00
|
|
|
if (done) {
|
|
|
|
free(cdata->template);
|
|
|
|
cdata->template = new_template;
|
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2011-07-02 21:05:44 +00:00
|
|
|
/*
|
|
|
|
* Check if there are more prompts; if so, get its respective input
|
|
|
|
* and update the prompt data.
|
|
|
|
*/
|
2017-01-06 11:57:03 +00:00
|
|
|
if (done && (ptr = strsep(&cdata->next_prompt, ",")) != NULL) {
|
2011-07-08 06:37:57 +00:00
|
|
|
xasprintf(&prompt, "%s ", ptr);
|
|
|
|
input = strsep(&cdata->next_input, ",");
|
2011-07-02 21:05:44 +00:00
|
|
|
status_prompt_update(c, prompt, input);
|
|
|
|
|
2012-07-10 11:53:01 +00:00
|
|
|
free(prompt);
|
2009-08-19 10:39:50 +00:00
|
|
|
cdata->idx++;
|
|
|
|
return (1);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 18:59:41 +00:00
|
|
|
status = cmd_parse_and_append(new_template, NULL, c, NULL, &error);
|
|
|
|
if (status == CMD_PARSE_ERROR) {
|
|
|
|
cmdq_append(c, cmdq_get_error(error));
|
|
|
|
free(error);
|
2019-05-23 11:13:30 +00:00
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2017-01-06 11:57:03 +00:00
|
|
|
if (!done)
|
|
|
|
free(new_template);
|
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);
|
|
|
|
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;
|
2009-07-17 06:13:27 +00:00
|
|
|
|
2012-07-10 11:53:01 +00:00
|
|
|
free(cdata->inputs);
|
|
|
|
free(cdata->prompts);
|
|
|
|
free(cdata->template);
|
|
|
|
free(cdata);
|
2009-07-17 06:13:27 +00:00
|
|
|
}
|