2009-08-16 21:29:24 +02:00
|
|
|
/* $Id: cmd-command-prompt.c,v 1.22 2009-08-16 19:29:24 tcunha Exp $ */
|
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>
|
2009-01-13 02:08:40 +01:00
|
|
|
#include <string.h>
|
2008-06-19 22:45:21 +02:00
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prompt for command in client.
|
|
|
|
*/
|
|
|
|
|
2009-01-13 02:08:40 +01:00
|
|
|
void cmd_command_prompt_init(struct cmd *, int);
|
2009-01-19 19:23:40 +01:00
|
|
|
int cmd_command_prompt_exec(struct cmd *, struct cmd_ctx *);
|
2008-06-19 22:45:21 +02:00
|
|
|
|
2009-01-11 01:48:42 +01:00
|
|
|
int cmd_command_prompt_callback(void *, const char *);
|
2009-07-17 11:26:21 +02:00
|
|
|
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,
|
2009-01-13 02:08:40 +01:00
|
|
|
CMD_TARGET_CLIENT_USAGE " [template]",
|
2009-07-14 08:43:33 +02:00
|
|
|
CMD_ARG01, 0,
|
2009-01-13 02:08:40 +01:00
|
|
|
cmd_command_prompt_init,
|
2008-06-19 22:45:21 +02:00
|
|
|
cmd_target_parse,
|
|
|
|
cmd_command_prompt_exec,
|
|
|
|
cmd_target_free,
|
|
|
|
cmd_target_print
|
|
|
|
};
|
|
|
|
|
2009-01-13 02:08:40 +01:00
|
|
|
struct cmd_command_prompt_data {
|
|
|
|
struct client *c;
|
|
|
|
char *template;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
cmd_command_prompt_init(struct cmd *self, int key)
|
|
|
|
{
|
|
|
|
struct cmd_target_data *data;
|
|
|
|
|
|
|
|
cmd_target_init(self, key);
|
|
|
|
data = self->data;
|
|
|
|
|
|
|
|
switch (key) {
|
|
|
|
case ',':
|
2009-01-18 15:40:48 +01:00
|
|
|
data->arg = xstrdup("rename-window '%%'");
|
2009-01-13 02:08:40 +01:00
|
|
|
break;
|
2009-01-30 22:10:10 +01:00
|
|
|
case '.':
|
|
|
|
data->arg = xstrdup("move-window -t '%%'");
|
|
|
|
break;
|
2009-01-18 18:20:52 +01:00
|
|
|
case 'f':
|
|
|
|
data->arg = xstrdup("find-window '%%'");
|
|
|
|
break;
|
2009-01-13 02:08:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-19 19:23:40 +01:00
|
|
|
int
|
2008-06-19 22:45:21 +02:00
|
|
|
cmd_command_prompt_exec(struct cmd *self, struct cmd_ctx *ctx)
|
|
|
|
{
|
2009-01-13 02:08:40 +01:00
|
|
|
struct cmd_target_data *data = self->data;
|
|
|
|
struct cmd_command_prompt_data *cdata;
|
|
|
|
struct client *c;
|
|
|
|
char *hdr, *ptr;
|
2008-06-19 22:45:21 +02:00
|
|
|
|
|
|
|
if ((c = cmd_find_client(ctx, data->target)) == NULL)
|
2009-01-19 19:23:40 +01:00
|
|
|
return (-1);
|
2008-06-19 22:45:21 +02:00
|
|
|
|
|
|
|
if (c->prompt_string != NULL)
|
2009-01-19 19:23:40 +01:00
|
|
|
return (0);
|
2008-06-19 22:45:21 +02:00
|
|
|
|
2009-01-13 02:08:40 +01:00
|
|
|
cdata = xmalloc(sizeof *cdata);
|
|
|
|
cdata->c = c;
|
|
|
|
if (data->arg != NULL) {
|
|
|
|
cdata->template = xstrdup(data->arg);
|
|
|
|
if ((ptr = strchr(data->arg, ' ')) == NULL)
|
|
|
|
ptr = strchr(data->arg, '\0');
|
|
|
|
xasprintf(&hdr, "(%.*s) ", (int) (ptr - data->arg), data->arg);
|
|
|
|
} else {
|
|
|
|
cdata->template = NULL;
|
|
|
|
hdr = xstrdup(":");
|
|
|
|
}
|
2009-07-17 11:26:21 +02:00
|
|
|
status_prompt_set(c, hdr,
|
|
|
|
cmd_command_prompt_callback, cmd_command_prompt_free, cdata, 0);
|
2009-01-13 02:08:40 +01:00
|
|
|
xfree(hdr);
|
2008-06-19 22:45:21 +02:00
|
|
|
|
2009-01-19 19:23:40 +01:00
|
|
|
return (0);
|
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-01-13 02:08:40 +01:00
|
|
|
struct cmd_command_prompt_data *cdata = data;
|
|
|
|
struct client *c = cdata->c;
|
2009-01-18 15:40:48 +01:00
|
|
|
struct cmd_list *cmdlist;
|
2009-01-13 02:08:40 +01:00
|
|
|
struct cmd_ctx ctx;
|
|
|
|
char *cause, *ptr, *buf, ch;
|
|
|
|
size_t len, slen;
|
2008-06-19 22:45:21 +02:00
|
|
|
|
2009-08-16 21:29:24 +02:00
|
|
|
if (s == NULL || *s == '\0')
|
2009-01-11 01:48:42 +01:00
|
|
|
return (0);
|
2009-01-13 02:08:40 +01:00
|
|
|
slen = strlen(s);
|
|
|
|
|
|
|
|
len = 0;
|
|
|
|
buf = NULL;
|
|
|
|
if (cdata->template != NULL) {
|
|
|
|
ptr = cdata->template;
|
|
|
|
while (*ptr != '\0') {
|
|
|
|
switch (ch = *ptr++) {
|
|
|
|
case '%':
|
|
|
|
if (*ptr != '%')
|
|
|
|
break;
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
buf = xrealloc(buf, 1, len + slen + 1);
|
|
|
|
memcpy(buf + len, s, slen);
|
|
|
|
len += slen;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
buf = xrealloc(buf, 1, len + 2);
|
|
|
|
buf[len++] = ch;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-20 17:51:32 +02:00
|
|
|
if (buf == NULL)
|
|
|
|
return (0);
|
2009-01-13 02:08:40 +01:00
|
|
|
buf[len] = '\0';
|
|
|
|
s = buf;
|
|
|
|
}
|
2008-06-19 22:45:21 +02:00
|
|
|
|
2009-01-18 15:40:48 +01:00
|
|
|
if (cmd_string_parse(s, &cmdlist, &cause) != 0) {
|
2008-06-19 23:20:27 +02:00
|
|
|
if (cause == NULL)
|
2009-01-11 01:48:42 +01:00
|
|
|
return (0);
|
2008-06-19 22:45:21 +02:00
|
|
|
*cause = toupper((u_char) *cause);
|
2009-07-15 19:50:11 +02:00
|
|
|
status_message_set(c, "%s", cause);
|
2008-06-19 22:45:21 +02:00
|
|
|
xfree(cause);
|
2009-01-18 15:40:48 +01:00
|
|
|
cmdlist = NULL;
|
2008-06-19 22:45:21 +02:00
|
|
|
}
|
2009-01-13 02:08:40 +01:00
|
|
|
if (buf != NULL)
|
|
|
|
xfree(buf);
|
2009-01-18 15:40:48 +01:00
|
|
|
if (cmdlist == NULL)
|
2009-01-11 01:48:42 +01:00
|
|
|
return (0);
|
2008-09-26 08:45:28 +02:00
|
|
|
|
2008-06-19 22:45:21 +02:00
|
|
|
ctx.msgdata = NULL;
|
|
|
|
ctx.cursession = c->session;
|
|
|
|
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
|
|
|
|
cmd_command_prompt_free(void *data)
|
|
|
|
{
|
|
|
|
struct cmd_command_prompt_data *cdata = data;
|
|
|
|
|
|
|
|
if (cdata->template != NULL)
|
|
|
|
xfree(cdata->template);
|
|
|
|
xfree(cdata);
|
|
|
|
}
|