2009-06-02 00:58:49 +02:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* 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 13:53:01 +02:00
|
|
|
#include <stdlib.h>
|
2009-06-02 00:58:49 +02:00
|
|
|
#include <string.h>
|
2011-07-02 23:05:44 +02:00
|
|
|
#include <time.h>
|
2009-06-02 00:58:49 +02:00
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prompt for command in client.
|
|
|
|
*/
|
|
|
|
|
2011-01-04 01:42:46 +01:00
|
|
|
void cmd_command_prompt_key_binding(struct cmd *, int);
|
|
|
|
int cmd_command_prompt_check(struct args *);
|
2013-03-24 10:54:10 +01:00
|
|
|
enum cmd_retval cmd_command_prompt_exec(struct cmd *, struct cmd_q *);
|
2009-06-02 00:58:49 +02:00
|
|
|
|
2011-01-04 01:42:46 +01:00
|
|
|
int cmd_command_prompt_callback(void *, const char *);
|
|
|
|
void cmd_command_prompt_free(void *);
|
2009-06-02 00:58:49 +02:00
|
|
|
|
|
|
|
const struct cmd_entry cmd_command_prompt_entry = {
|
|
|
|
"command-prompt", NULL,
|
2011-07-02 23:05:44 +02:00
|
|
|
"I:p:t:", 0, 1,
|
|
|
|
"[-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE " [template]",
|
2011-01-04 01:42:46 +01:00
|
|
|
0,
|
|
|
|
cmd_command_prompt_key_binding,
|
|
|
|
NULL,
|
|
|
|
cmd_command_prompt_exec
|
2009-08-19 12:39:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct cmd_command_prompt_cdata {
|
2009-06-02 00:58:49 +02:00
|
|
|
struct client *c;
|
2011-07-02 23:05:44 +02:00
|
|
|
char *inputs;
|
|
|
|
char *next_input;
|
2009-08-19 12:39:50 +02:00
|
|
|
char *next_prompt;
|
|
|
|
char *prompts;
|
2009-06-02 00:58:49 +02:00
|
|
|
char *template;
|
2009-08-19 12:39:50 +02:00
|
|
|
int idx;
|
2009-06-02 00:58:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2011-01-04 01:42:46 +01:00
|
|
|
cmd_command_prompt_key_binding(struct cmd *self, int key)
|
2009-06-02 00:58:49 +02:00
|
|
|
{
|
|
|
|
switch (key) {
|
2011-07-03 20:18:15 +02:00
|
|
|
case '$':
|
|
|
|
self->args = args_create(1, "rename-session '%%'");
|
|
|
|
args_set(self->args, 'I', "#S");
|
|
|
|
break;
|
2009-06-02 00:58:49 +02:00
|
|
|
case ',':
|
2011-01-04 01:42:46 +01:00
|
|
|
self->args = args_create(1, "rename-window '%%'");
|
2011-07-03 20:18:15 +02:00
|
|
|
args_set(self->args, 'I', "#W");
|
2009-06-02 00:58:49 +02:00
|
|
|
break;
|
|
|
|
case '.':
|
2011-01-04 01:42:46 +01:00
|
|
|
self->args = args_create(1, "move-window -t '%%'");
|
2009-06-02 00:58:49 +02:00
|
|
|
break;
|
|
|
|
case 'f':
|
2011-01-04 01:42:46 +01:00
|
|
|
self->args = args_create(1, "find-window '%%'");
|
2009-06-02 00:58:49 +02:00
|
|
|
break;
|
2010-05-06 01:24:23 +02:00
|
|
|
case '\'':
|
2011-01-04 01:42:46 +01:00
|
|
|
self->args = args_create(1, "select-window -t ':%%'");
|
|
|
|
args_set(self->args, 'p', "index");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
self->args = args_create(0);
|
2010-05-06 01:24:23 +02:00
|
|
|
break;
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-11 09:10:15 +02:00
|
|
|
enum cmd_retval
|
2013-03-24 10:54:10 +01:00
|
|
|
cmd_command_prompt_exec(struct cmd *self, struct cmd_q *cmdq)
|
2009-06-02 00:58:49 +02:00
|
|
|
{
|
2011-01-04 01:42:46 +01:00
|
|
|
struct args *args = self->args;
|
2011-07-02 23:05:44 +02:00
|
|
|
const char *inputs, *prompts;
|
2009-08-19 12:39:50 +02:00
|
|
|
struct cmd_command_prompt_cdata *cdata;
|
2009-06-02 00:58:49 +02:00
|
|
|
struct client *c;
|
2011-07-08 08:37:57 +02:00
|
|
|
char *prompt, *ptr, *input = NULL;
|
2009-08-19 12:39:50 +02:00
|
|
|
size_t n;
|
2009-06-02 00:58:49 +02:00
|
|
|
|
2013-03-24 10:54:10 +01:00
|
|
|
if ((c = cmd_find_client(cmdq, args_get(args, 't'), 0)) == NULL)
|
2012-07-11 09:10:15 +02:00
|
|
|
return (CMD_RETURN_ERROR);
|
2009-06-02 00:58:49 +02:00
|
|
|
|
|
|
|
if (c->prompt_string != NULL)
|
2012-07-11 09:10:15 +02:00
|
|
|
return (CMD_RETURN_NORMAL);
|
2009-06-02 00:58:49 +02:00
|
|
|
|
|
|
|
cdata = xmalloc(sizeof *cdata);
|
|
|
|
cdata->c = c;
|
2009-08-19 12:39:50 +02:00
|
|
|
cdata->idx = 1;
|
2011-07-02 23:05:44 +02:00
|
|
|
cdata->inputs = NULL;
|
|
|
|
cdata->next_input = NULL;
|
2009-08-19 12:39:50 +02:00
|
|
|
cdata->next_prompt = NULL;
|
|
|
|
cdata->prompts = NULL;
|
|
|
|
cdata->template = NULL;
|
|
|
|
|
2011-01-04 01:42:46 +01:00
|
|
|
if (args->argc != 0)
|
|
|
|
cdata->template = xstrdup(args->argv[0]);
|
2009-08-19 12:39:50 +02:00
|
|
|
else
|
|
|
|
cdata->template = xstrdup("%1");
|
2011-01-04 01:42:46 +01:00
|
|
|
|
2011-07-02 23:05:44 +02:00
|
|
|
if ((prompts = args_get(args, 'p')) != NULL)
|
2011-01-04 01:42:46 +01: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 12:39:50 +02:00
|
|
|
} else
|
|
|
|
cdata->prompts = xstrdup(":");
|
|
|
|
|
2011-07-02 23:05:44 +02:00
|
|
|
/* Get first prompt. */
|
2009-08-19 12:39:50 +02:00
|
|
|
cdata->next_prompt = cdata->prompts;
|
|
|
|
ptr = strsep(&cdata->next_prompt, ",");
|
2011-01-04 01:42:46 +01:00
|
|
|
if (prompts == NULL)
|
2009-08-19 12:39:50 +02:00
|
|
|
prompt = xstrdup(ptr);
|
2011-07-08 08:37:57 +02:00
|
|
|
else
|
|
|
|
xasprintf(&prompt, "%s ", ptr);
|
2011-07-02 23:05:44 +02:00
|
|
|
|
|
|
|
/* Get initial prompt input. */
|
|
|
|
if ((inputs = args_get(args, 'I')) != NULL) {
|
|
|
|
cdata->inputs = xstrdup(inputs);
|
|
|
|
cdata->next_input = cdata->inputs;
|
2011-07-08 08:37:57 +02:00
|
|
|
input = strsep(&cdata->next_input, ",");
|
2011-07-02 23:05:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
status_prompt_set(c, prompt, input, cmd_command_prompt_callback,
|
2011-01-04 01:42:46 +01:00
|
|
|
cmd_command_prompt_free, cdata, 0);
|
2012-07-10 13:53:01 +02:00
|
|
|
free(prompt);
|
2009-06-02 00:58:49 +02:00
|
|
|
|
2012-07-11 09:10:15 +02:00
|
|
|
return (CMD_RETURN_NORMAL);
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cmd_command_prompt_callback(void *data, const char *s)
|
|
|
|
{
|
2009-08-19 12:39:50 +02:00
|
|
|
struct cmd_command_prompt_cdata *cdata = data;
|
2009-06-02 00:58:49 +02:00
|
|
|
struct client *c = cdata->c;
|
|
|
|
struct cmd_list *cmdlist;
|
2011-07-08 08:37:57 +02:00
|
|
|
char *cause, *new_template, *prompt, *ptr;
|
|
|
|
char *input = NULL;
|
2009-06-02 00:58:49 +02:00
|
|
|
|
2009-08-19 12:39:50 +02:00
|
|
|
if (s == NULL)
|
2009-06-02 00:58:49 +02:00
|
|
|
return (0);
|
|
|
|
|
2011-07-02 23:05:44 +02:00
|
|
|
new_template = cmd_template_replace(cdata->template, s, cdata->idx);
|
2012-07-10 13:53:01 +02:00
|
|
|
free(cdata->template);
|
2011-07-02 23:05:44 +02:00
|
|
|
cdata->template = new_template;
|
2009-06-02 00:58:49 +02:00
|
|
|
|
2011-07-02 23:05:44 +02:00
|
|
|
/*
|
|
|
|
* Check if there are more prompts; if so, get its respective input
|
|
|
|
* and update the prompt data.
|
|
|
|
*/
|
2009-08-19 12:39:50 +02:00
|
|
|
if ((ptr = strsep(&cdata->next_prompt, ",")) != NULL) {
|
2011-07-08 08:37:57 +02:00
|
|
|
xasprintf(&prompt, "%s ", ptr);
|
|
|
|
input = strsep(&cdata->next_input, ",");
|
2011-07-02 23:05:44 +02:00
|
|
|
status_prompt_update(c, prompt, input);
|
|
|
|
|
2012-07-10 13:53:01 +02:00
|
|
|
free(prompt);
|
2009-08-19 12:39:50 +02:00
|
|
|
cdata->idx++;
|
|
|
|
return (1);
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|
|
|
|
|
2013-03-24 10:54:10 +01:00
|
|
|
if (cmd_string_parse(new_template, &cmdlist, NULL, 0, &cause) != 0) {
|
2009-08-19 12:39:50 +02:00
|
|
|
if (cause != NULL) {
|
|
|
|
*cause = toupper((u_char) *cause);
|
|
|
|
status_message_set(c, "%s", cause);
|
2012-07-10 13:53:01 +02:00
|
|
|
free(cause);
|
2009-08-19 12:39:50 +02:00
|
|
|
}
|
2009-06-02 00:58:49 +02:00
|
|
|
return (0);
|
2009-08-19 12:39:50 +02:00
|
|
|
}
|
2009-06-02 00:58:49 +02:00
|
|
|
|
2013-03-24 10:54:10 +01:00
|
|
|
cmdq_run(c->cmdq, cmdlist);
|
2009-06-02 00:58:49 +02:00
|
|
|
cmd_list_free(cmdlist);
|
|
|
|
|
2009-07-17 08:13:27 +02:00
|
|
|
if (c->prompt_callbackfn != (void *) &cmd_command_prompt_callback)
|
2009-06-02 00:58:49 +02:00
|
|
|
return (1);
|
|
|
|
return (0);
|
|
|
|
}
|
2009-07-17 08:13:27 +02:00
|
|
|
|
|
|
|
void
|
2011-01-04 01:42:46 +01:00
|
|
|
cmd_command_prompt_free(void *data)
|
2009-07-17 08:13:27 +02:00
|
|
|
{
|
2009-08-19 12:39:50 +02:00
|
|
|
struct cmd_command_prompt_cdata *cdata = data;
|
2009-07-17 08:13:27 +02:00
|
|
|
|
2012-07-10 13:53:01 +02:00
|
|
|
free(cdata->inputs);
|
|
|
|
free(cdata->prompts);
|
|
|
|
free(cdata->template);
|
|
|
|
free(cdata);
|
2009-07-17 08:13:27 +02:00
|
|
|
}
|