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-01-06 11:57:03 +00:00
|
|
|
static int cmd_command_prompt_callback(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,
|
|
|
|
|
2017-01-06 11:57:03 +00:00
|
|
|
.args = { "1iI:Np:t:", 0, 1 },
|
|
|
|
.usage = "[-1Ni] [-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE " "
|
2015-12-13 21:53:57 +00:00
|
|
|
"[template]",
|
|
|
|
|
2015-12-14 00:31:54 +00:00
|
|
|
.tflag = CMD_CLIENT,
|
|
|
|
|
|
|
|
.flags = 0,
|
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 {
|
2009-06-01 22:58:49 +00:00
|
|
|
struct client *c;
|
2017-01-06 11:57:03 +00:00
|
|
|
int flags;
|
|
|
|
|
2011-07-02 21:05:44 +00:00
|
|
|
char *inputs;
|
|
|
|
char *next_input;
|
2017-01-06 11:57:03 +00:00
|
|
|
|
2009-08-19 10:39:50 +00:00
|
|
|
char *prompts;
|
2017-01-06 11:57:03 +00:00
|
|
|
char *next_prompt;
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
char *template;
|
2009-08-19 10:39:50 +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
|
|
|
{
|
2011-01-04 00:42:46 +00:00
|
|
|
struct args *args = self->args;
|
2011-07-02 21:05:44 +00:00
|
|
|
const char *inputs, *prompts;
|
2009-08-19 10:39:50 +00:00
|
|
|
struct cmd_command_prompt_cdata *cdata;
|
2016-10-16 19:04:05 +00:00
|
|
|
struct client *c = item->state.c;
|
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
|
|
|
|
|
|
|
if (c->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);
|
2009-06-01 22:58:49 +00:00
|
|
|
cdata->c = c;
|
2017-01-06 11:57:03 +00:00
|
|
|
|
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
|
|
|
|
2011-01-04 00:42:46 +00:00
|
|
|
if (args->argc != 0)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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;
|
2011-07-02 21:05:44 +00:00
|
|
|
status_prompt_set(c, prompt, input, cmd_command_prompt_callback,
|
2017-01-06 11:57:03 +00:00
|
|
|
cmd_command_prompt_free, cdata, cdata->flags);
|
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-16 17:55:14 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_command_prompt_error(struct cmdq_item *item, void *data)
|
2016-10-16 17:55:14 +00:00
|
|
|
{
|
|
|
|
char *error = data;
|
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_error(item, "%s", error);
|
2016-10-16 17:55:14 +00:00
|
|
|
free(error);
|
|
|
|
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static int
|
2017-01-06 11:57:03 +00:00
|
|
|
cmd_command_prompt_callback(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;
|
2009-06-01 22:58:49 +00:00
|
|
|
struct client *c = cdata->c;
|
|
|
|
struct cmd_list *cmdlist;
|
2016-10-16 19:04:05 +00:00
|
|
|
struct cmdq_item *new_item;
|
2011-07-08 06:37:57 +00:00
|
|
|
char *cause, *new_template, *prompt, *ptr;
|
|
|
|
char *input = NULL;
|
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
|
|
|
}
|
|
|
|
|
2017-01-15 22:00:56 +00:00
|
|
|
cmdlist = cmd_string_parse(new_template, NULL, 0, &cause);
|
|
|
|
if (cmdlist == NULL) {
|
2009-08-19 10:39:50 +00:00
|
|
|
if (cause != NULL) {
|
2016-10-16 19:04:05 +00:00
|
|
|
new_item = cmdq_get_callback(cmd_command_prompt_error,
|
2016-10-16 17:55:14 +00:00
|
|
|
cause);
|
|
|
|
} else
|
2016-10-16 19:04:05 +00:00
|
|
|
new_item = NULL;
|
2016-10-16 17:55:14 +00:00
|
|
|
} else {
|
2016-10-16 19:04:05 +00:00
|
|
|
new_item = cmdq_get_command(cmdlist, NULL, NULL, 0);
|
2016-10-16 17:55:14 +00:00
|
|
|
cmd_list_free(cmdlist);
|
2009-08-19 10:39:50 +00:00
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
if (new_item != NULL)
|
|
|
|
cmdq_append(c, new_item);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2017-01-06 11:57:03 +00:00
|
|
|
if (!done)
|
|
|
|
free(new_template);
|
2016-10-16 17:55:14 +00:00
|
|
|
if (c->prompt_callbackfn != (void *)&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
|
|
|
}
|