2011-07-03 23:52:50 +02:00
|
|
|
/* $Id$ */
|
2008-06-19 22:45:21 +02:00
|
|
|
|
|
|
|
/*
|
2008-06-25 22:43:14 +02:00
|
|
|
* Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
|
2008-06-19 22:45:21 +02: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-11 21:34:16 +02:00
|
|
|
#include <stdlib.h>
|
2009-01-13 02:08:40 +01:00
|
|
|
#include <string.h>
|
2011-07-03 23:52:50 +02:00
|
|
|
#include <time.h>
|
2008-06-19 22:45:21 +02:00
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prompt for command in client.
|
|
|
|
*/
|
|
|
|
|
2011-01-07 15:45:34 +01:00
|
|
|
void cmd_command_prompt_key_binding(struct cmd *, int);
|
|
|
|
int cmd_command_prompt_check(struct args *);
|
2012-07-11 21:37:32 +02:00
|
|
|
enum cmd_retval cmd_command_prompt_exec(struct cmd *, struct cmd_ctx *);
|
2008-06-19 22:45:21 +02:00
|
|
|
|
2011-01-07 15:45:34 +01:00
|
|
|
int cmd_command_prompt_callback(void *, const char *);
|
|
|
|
void cmd_command_prompt_free(void *);
|
2008-06-19 22:45:21 +02:00
|
|
|
|
|
|
|
const struct cmd_entry cmd_command_prompt_entry = {
|
|
|
|
"command-prompt", NULL,
|
2011-07-03 23:52:50 +02:00
|
|
|
"I:p:t:", 0, 1,
|
|
|
|
"[-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE " [template]",
|
2011-01-07 15:45:34 +01:00
|
|
|
0,
|
|
|
|
cmd_command_prompt_key_binding,
|
|
|
|
NULL,
|
|
|
|
cmd_command_prompt_exec
|
2009-08-20 13:51:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct cmd_command_prompt_cdata {
|
2009-01-13 02:08:40 +01:00
|
|
|
struct client *c;
|
2011-07-03 23:52:50 +02:00
|
|
|
char *inputs;
|
|
|
|
char *next_input;
|
2009-08-20 13:51:20 +02:00
|
|
|
char *next_prompt;
|
|
|
|
char *prompts;
|
2009-01-13 02:08:40 +01:00
|
|
|
char *template;
|
2009-08-20 13:51:20 +02:00
|
|
|
int idx;
|
2009-01-13 02:08:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2011-01-07 15:45:34 +01:00
|
|
|
cmd_command_prompt_key_binding(struct cmd *self, int key)
|
2009-01-13 02:08:40 +01:00
|
|
|
{
|
|
|
|
switch (key) {
|
2011-07-04 10:22:37 +02:00
|
|
|
case '$':
|
|
|
|
self->args = args_create(1, "rename-session '%%'");
|
|
|
|
args_set(self->args, 'I', "#S");
|
|
|
|
break;
|
2009-01-13 02:08:40 +01:00
|
|
|
case ',':
|
2011-01-07 15:45:34 +01:00
|
|
|
self->args = args_create(1, "rename-window '%%'");
|
2011-07-04 10:22:37 +02:00
|
|
|
args_set(self->args, 'I', "#W");
|
2009-01-13 02:08:40 +01:00
|
|
|
break;
|
2009-01-30 22:10:10 +01:00
|
|
|
case '.':
|
2011-01-07 15:45:34 +01:00
|
|
|
self->args = args_create(1, "move-window -t '%%'");
|
2009-01-30 22:10:10 +01:00
|
|
|
break;
|
2009-01-18 18:20:52 +01:00
|
|
|
case 'f':
|
2011-01-07 15:45:34 +01:00
|
|
|
self->args = args_create(1, "find-window '%%'");
|
2009-01-18 18:20:52 +01:00
|
|
|
break;
|
2010-05-14 16:33:39 +02:00
|
|
|
case '\'':
|
2011-01-07 15:45:34 +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-14 16:33:39 +02:00
|
|
|
break;
|
2009-01-13 02:08:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-11 21:37:32 +02:00
|
|
|
enum cmd_retval
|
2008-06-19 22:45:21 +02:00
|
|
|
cmd_command_prompt_exec(struct cmd *self, struct cmd_ctx *ctx)
|
|
|
|
{
|
2011-01-07 15:45:34 +01:00
|
|
|
struct args *args = self->args;
|
2011-07-03 23:52:50 +02:00
|
|
|
const char *inputs, *prompts;
|
2009-08-20 13:51:20 +02:00
|
|
|
struct cmd_command_prompt_cdata *cdata;
|
2009-01-13 02:08:40 +01:00
|
|
|
struct client *c;
|
2011-07-08 10:42:03 +02:00
|
|
|
char *prompt, *ptr, *input = NULL;
|
2009-08-20 13:51:20 +02:00
|
|
|
size_t n;
|
2008-06-19 22:45:21 +02:00
|
|
|
|
2011-01-07 15:45:34 +01:00
|
|
|
if ((c = cmd_find_client(ctx, args_get(args, 't'))) == NULL)
|
2012-07-11 21:37:32 +02:00
|
|
|
return (CMD_RETURN_ERROR);
|
2008-06-19 22:45:21 +02:00
|
|
|
|
|
|
|
if (c->prompt_string != NULL)
|
2012-07-11 21:37:32 +02:00
|
|
|
return (CMD_RETURN_NORMAL);
|
2008-06-19 22:45:21 +02:00
|
|
|
|
2009-01-13 02:08:40 +01:00
|
|
|
cdata = xmalloc(sizeof *cdata);
|
|
|
|
cdata->c = c;
|
2009-08-20 13:51:20 +02:00
|
|
|
cdata->idx = 1;
|
2011-07-03 23:52:50 +02:00
|
|
|
cdata->inputs = NULL;
|
|
|
|
cdata->next_input = NULL;
|
2009-08-20 13:51:20 +02:00
|
|
|
cdata->next_prompt = NULL;
|
|
|
|
cdata->prompts = NULL;
|
|
|
|
cdata->template = NULL;
|
|
|
|
|
2011-01-07 15:45:34 +01:00
|
|
|
if (args->argc != 0)
|
|
|
|
cdata->template = xstrdup(args->argv[0]);
|
2009-08-20 13:51:20 +02:00
|
|
|
else
|
|
|
|
cdata->template = xstrdup("%1");
|
2011-01-07 15:45:34 +01:00
|
|
|
|
2011-07-03 23:52:50 +02:00
|
|
|
if ((prompts = args_get(args, 'p')) != NULL)
|
2011-01-07 15:45:34 +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-20 13:51:20 +02:00
|
|
|
} else
|
|
|
|
cdata->prompts = xstrdup(":");
|
|
|
|
|
2011-07-03 23:52:50 +02:00
|
|
|
/* Get first prompt. */
|
2009-08-20 13:51:20 +02:00
|
|
|
cdata->next_prompt = cdata->prompts;
|
|
|
|
ptr = strsep(&cdata->next_prompt, ",");
|
2011-01-07 15:45:34 +01:00
|
|
|
if (prompts == NULL)
|
2009-08-20 13:51:20 +02:00
|
|
|
prompt = xstrdup(ptr);
|
2011-07-08 10:42:03 +02:00
|
|
|
else
|
|
|
|
xasprintf(&prompt, "%s ", ptr);
|
2011-07-03 23:52:50 +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 10:42:03 +02:00
|
|
|
input = strsep(&cdata->next_input, ",");
|
2011-07-03 23:52:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
status_prompt_set(c, prompt, input, cmd_command_prompt_callback,
|
2011-01-07 15:45:34 +01:00
|
|
|
cmd_command_prompt_free, cdata, 0);
|
2012-07-11 21:34:16 +02:00
|
|
|
free(prompt);
|
2008-06-19 22:45:21 +02:00
|
|
|
|
2012-07-11 21:37:32 +02:00
|
|
|
return (CMD_RETURN_NORMAL);
|
2008-06-19 22:45:21 +02:00
|
|
|
}
|
|
|
|
|
2009-01-11 01:48:42 +01:00
|
|
|
int
|
|
|
|
cmd_command_prompt_callback(void *data, const char *s)
|
2008-06-19 22:45:21 +02:00
|
|
|
{
|
2009-08-20 13:51:20 +02:00
|
|
|
struct cmd_command_prompt_cdata *cdata = data;
|
2009-01-13 02:08:40 +01:00
|
|
|
struct client *c = cdata->c;
|
2009-01-18 15:40:48 +01:00
|
|
|
struct cmd_list *cmdlist;
|
2009-08-20 13:51:20 +02:00
|
|
|
struct cmd_ctx ctx;
|
2011-07-08 10:42:03 +02:00
|
|
|
char *cause, *new_template, *prompt, *ptr;
|
|
|
|
char *input = NULL;
|
2008-06-19 22:45:21 +02:00
|
|
|
|
2009-08-20 13:51:20 +02:00
|
|
|
if (s == NULL)
|
2009-01-11 01:48:42 +01:00
|
|
|
return (0);
|
2009-01-13 02:08:40 +01:00
|
|
|
|
2011-07-03 23:52:50 +02:00
|
|
|
new_template = cmd_template_replace(cdata->template, s, cdata->idx);
|
2012-07-11 21:34:16 +02:00
|
|
|
free(cdata->template);
|
2011-07-03 23:52:50 +02:00
|
|
|
cdata->template = new_template;
|
2009-01-13 02:08:40 +01:00
|
|
|
|
2011-07-03 23:52:50 +02:00
|
|
|
/*
|
|
|
|
* Check if there are more prompts; if so, get its respective input
|
|
|
|
* and update the prompt data.
|
|
|
|
*/
|
2009-08-20 13:51:20 +02:00
|
|
|
if ((ptr = strsep(&cdata->next_prompt, ",")) != NULL) {
|
2011-07-08 10:42:03 +02:00
|
|
|
xasprintf(&prompt, "%s ", ptr);
|
|
|
|
input = strsep(&cdata->next_input, ",");
|
2011-07-03 23:52:50 +02:00
|
|
|
status_prompt_update(c, prompt, input);
|
|
|
|
|
2012-07-11 21:34:16 +02:00
|
|
|
free(prompt);
|
2009-08-20 13:51:20 +02:00
|
|
|
cdata->idx++;
|
|
|
|
return (1);
|
2009-01-13 02:08:40 +01:00
|
|
|
}
|
2008-06-19 22:45:21 +02:00
|
|
|
|
2011-07-03 23:52:50 +02:00
|
|
|
if (cmd_string_parse(new_template, &cmdlist, &cause) != 0) {
|
2009-08-20 13:51:20 +02:00
|
|
|
if (cause != NULL) {
|
|
|
|
*cause = toupper((u_char) *cause);
|
|
|
|
status_message_set(c, "%s", cause);
|
2012-07-11 21:34:16 +02:00
|
|
|
free(cause);
|
2009-08-20 13:51:20 +02:00
|
|
|
}
|
2009-01-11 01:48:42 +01:00
|
|
|
return (0);
|
2009-08-20 13:51:20 +02:00
|
|
|
}
|
2008-09-26 08:45:28 +02:00
|
|
|
|
2008-06-19 22:45:21 +02:00
|
|
|
ctx.msgdata = NULL;
|
|
|
|
ctx.curclient = c;
|
|
|
|
|
|
|
|
ctx.error = key_bindings_error;
|
|
|
|
ctx.print = key_bindings_print;
|
|
|
|
ctx.info = key_bindings_info;
|
|
|
|
|
|
|
|
ctx.cmdclient = NULL;
|
|
|
|
|
2009-01-18 15:40:48 +01:00
|
|
|
cmd_list_exec(cmdlist, &ctx);
|
2009-02-16 19:58:14 +01:00
|
|
|
cmd_list_free(cmdlist);
|
2009-01-11 01:48:42 +01:00
|
|
|
|
2009-07-17 11:26:21 +02:00
|
|
|
if (c->prompt_callbackfn != (void *) &cmd_command_prompt_callback)
|
2009-01-11 01:48:42 +01:00
|
|
|
return (1);
|
|
|
|
return (0);
|
2008-06-19 22:45:21 +02:00
|
|
|
}
|
2009-07-17 11:26:21 +02:00
|
|
|
|
|
|
|
void
|
2011-01-07 15:45:34 +01:00
|
|
|
cmd_command_prompt_free(void *data)
|
2009-07-17 11:26:21 +02:00
|
|
|
{
|
2009-08-20 13:51:20 +02:00
|
|
|
struct cmd_command_prompt_cdata *cdata = data;
|
2009-07-17 11:26:21 +02:00
|
|
|
|
2012-07-11 21:34:16 +02:00
|
|
|
free(cdata->inputs);
|
|
|
|
free(cdata->prompts);
|
|
|
|
free(cdata->template);
|
|
|
|
free(cdata);
|
2009-07-17 11:26:21 +02:00
|
|
|
}
|