2009-06-01 22:58:49 +00: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 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.
|
|
|
|
*/
|
|
|
|
|
2011-01-04 00:42:46 +00:00
|
|
|
void cmd_command_prompt_key_binding(struct cmd *, int);
|
|
|
|
int cmd_command_prompt_check(struct args *);
|
2012-07-11 07:10:15 +00:00
|
|
|
enum cmd_retval cmd_command_prompt_exec(struct cmd *, struct cmd_ctx *);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2011-01-04 00:42:46 +00:00
|
|
|
int cmd_command_prompt_callback(void *, const char *);
|
|
|
|
void cmd_command_prompt_free(void *);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
const struct cmd_entry cmd_command_prompt_entry = {
|
|
|
|
"command-prompt", NULL,
|
2011-07-02 21:05:44 +00:00
|
|
|
"I:p:t:", 0, 1,
|
|
|
|
"[-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE " [template]",
|
2011-01-04 00:42:46 +00:00
|
|
|
0,
|
|
|
|
cmd_command_prompt_key_binding,
|
|
|
|
NULL,
|
|
|
|
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;
|
2011-07-02 21:05:44 +00:00
|
|
|
char *inputs;
|
|
|
|
char *next_input;
|
2009-08-19 10:39:50 +00:00
|
|
|
char *next_prompt;
|
|
|
|
char *prompts;
|
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
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2011-01-04 00:42:46 +00:00
|
|
|
cmd_command_prompt_key_binding(struct cmd *self, int key)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
|
|
|
switch (key) {
|
2011-07-03 18:18:15 +00:00
|
|
|
case '$':
|
|
|
|
self->args = args_create(1, "rename-session '%%'");
|
|
|
|
args_set(self->args, 'I', "#S");
|
|
|
|
break;
|
2009-06-01 22:58:49 +00:00
|
|
|
case ',':
|
2011-01-04 00:42:46 +00:00
|
|
|
self->args = args_create(1, "rename-window '%%'");
|
2011-07-03 18:18:15 +00:00
|
|
|
args_set(self->args, 'I', "#W");
|
2009-06-01 22:58:49 +00:00
|
|
|
break;
|
|
|
|
case '.':
|
2011-01-04 00:42:46 +00:00
|
|
|
self->args = args_create(1, "move-window -t '%%'");
|
2009-06-01 22:58:49 +00:00
|
|
|
break;
|
|
|
|
case 'f':
|
2011-01-04 00:42:46 +00:00
|
|
|
self->args = args_create(1, "find-window '%%'");
|
2009-06-01 22:58:49 +00:00
|
|
|
break;
|
2010-05-05 23:24:23 +00:00
|
|
|
case '\'':
|
2011-01-04 00:42:46 +00:00
|
|
|
self->args = args_create(1, "select-window -t ':%%'");
|
|
|
|
args_set(self->args, 'p', "index");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
self->args = args_create(0);
|
2010-05-05 23:24:23 +00:00
|
|
|
break;
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-11 07:10:15 +00:00
|
|
|
enum cmd_retval
|
2009-06-01 22:58:49 +00:00
|
|
|
cmd_command_prompt_exec(struct cmd *self, struct cmd_ctx *ctx)
|
|
|
|
{
|
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;
|
2009-06-01 22:58:49 +00:00
|
|
|
struct client *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
|
|
|
|
2011-01-04 00:42:46 +00:00
|
|
|
if ((c = cmd_find_client(ctx, args_get(args, 't'))) == NULL)
|
2012-07-11 07:10:15 +00:00
|
|
|
return (CMD_RETURN_ERROR);
|
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
|
|
|
|
|
|
|
cdata = xmalloc(sizeof *cdata);
|
|
|
|
cdata->c = c;
|
2009-08-19 10:39:50 +00:00
|
|
|
cdata->idx = 1;
|
2011-07-02 21:05:44 +00:00
|
|
|
cdata->inputs = NULL;
|
|
|
|
cdata->next_input = NULL;
|
2009-08-19 10:39:50 +00:00
|
|
|
cdata->next_prompt = NULL;
|
|
|
|
cdata->prompts = NULL;
|
|
|
|
cdata->template = NULL;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
status_prompt_set(c, prompt, input, cmd_command_prompt_callback,
|
2011-01-04 00:42:46 +00:00
|
|
|
cmd_command_prompt_free, cdata, 0);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cmd_command_prompt_callback(void *data, const char *s)
|
|
|
|
{
|
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;
|
2009-08-19 10:39:50 +00:00
|
|
|
struct cmd_ctx ctx;
|
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);
|
|
|
|
|
2011-07-02 21:05:44 +00:00
|
|
|
new_template = cmd_template_replace(cdata->template, s, cdata->idx);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(cdata->template);
|
2011-07-02 21:05:44 +00:00
|
|
|
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.
|
|
|
|
*/
|
2009-08-19 10:39:50 +00:00
|
|
|
if ((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
|
|
|
}
|
|
|
|
|
2011-07-02 21:05:44 +00:00
|
|
|
if (cmd_string_parse(new_template, &cmdlist, &cause) != 0) {
|
2009-08-19 10:39:50 +00:00
|
|
|
if (cause != NULL) {
|
|
|
|
*cause = toupper((u_char) *cause);
|
|
|
|
status_message_set(c, "%s", cause);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(cause);
|
2009-08-19 10:39:50 +00:00
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
return (0);
|
2009-08-19 10:39:50 +00:00
|
|
|
}
|
2009-06-01 22:58:49 +00: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;
|
|
|
|
|
|
|
|
cmd_list_exec(cmdlist, &ctx);
|
|
|
|
cmd_list_free(cmdlist);
|
|
|
|
|
2009-07-17 06:13:27 +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
|
|
|
|
|
|
|
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
|
|
|
}
|