Merge cmd_list_parse into cmd-parse.y so it can use the new alias

processing code.
This commit is contained in:
nicm 2019-05-25 07:18:20 +00:00
parent 6b0fa14470
commit f8d3d247d8
6 changed files with 159 additions and 160 deletions

View File

@ -215,14 +215,13 @@ client_exit_message(void)
int
client_main(struct event_base *base, int argc, char **argv, int flags)
{
struct cmd_parse_result *pr;
struct cmd *cmd;
struct cmd_list *cmdlist;
struct msg_command_data *data;
int cmdflags, fd, i;
const char *ttynam, *cwd;
pid_t ppid;
enum msgtype msg;
char *cause;
struct termios tio, saved_tio;
size_t size;
@ -248,14 +247,15 @@ client_main(struct event_base *base, int argc, char **argv, int flags)
* later in server) but it is necessary to get the start server
* flag.
*/
cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause);
if (cmdlist != NULL) {
TAILQ_FOREACH(cmd, &cmdlist->list, qentry) {
pr = cmd_parse_from_arguments(argc, argv, NULL);
if (pr->status == CMD_PARSE_SUCCESS) {
TAILQ_FOREACH(cmd, &pr->cmdlist->list, qentry) {
if (cmd->entry->flags & CMD_STARTSERVER)
cmdflags |= CMD_STARTSERVER;
}
cmd_list_free(cmdlist);
}
cmd_list_free(pr->cmdlist);
} else
free(pr->error);
}
/* Create client process structure (starts logging). */

View File

@ -24,20 +24,17 @@
#include "tmux.h"
/*
* Bind a key to a command, this recurses through cmd_*.
* Bind a key to a command.
*/
static enum cmd_retval cmd_bind_key_exec(struct cmd *, struct cmdq_item *);
static enum cmd_retval cmd_bind_key_mode_table(struct cmd *,
struct cmdq_item *, key_code);
const struct cmd_entry cmd_bind_key_entry = {
.name = "bind-key",
.alias = "bind",
.args = { "cnrt:T:", 1, -1 },
.usage = "[-cnr] [-t mode-table] [-T key-table] key "
.args = { "cnrT:", 2, -1 },
.usage = "[-cnr] [-T key-table] key "
"command [arguments]",
.flags = CMD_AFTERHOOK,
@ -47,23 +44,10 @@ const struct cmd_entry cmd_bind_key_entry = {
static enum cmd_retval
cmd_bind_key_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = self->args;
char *cause;
struct cmd_list *cmdlist;
key_code key;
const char *tablename;
if (args_has(args, 't')) {
if (args->argc != 2 && args->argc != 3) {
cmdq_error(item, "not enough arguments");
return (CMD_RETURN_ERROR);
}
} else {
if (args->argc < 2) {
cmdq_error(item, "not enough arguments");
return (CMD_RETURN_ERROR);
}
}
struct args *args = self->args;
key_code key;
const char *tablename;
struct cmd_parse_result *pr;
key = key_string_lookup_string(args->argv[0]);
if (key == KEYC_NONE || key == KEYC_UNKNOWN) {
@ -71,9 +55,6 @@ cmd_bind_key_exec(struct cmd *self, struct cmdq_item *item)
return (CMD_RETURN_ERROR);
}
if (args_has(args, 't'))
return (cmd_bind_key_mode_table(self, item, key));
if (args_has(args, 'T'))
tablename = args_get(args, 'T');
else if (args_has(args, 'n'))
@ -81,50 +62,18 @@ cmd_bind_key_exec(struct cmd *self, struct cmdq_item *item)
else
tablename = "prefix";
cmdlist = cmd_list_parse(args->argc - 1, args->argv + 1, NULL, 0,
&cause);
if (cmdlist == NULL) {
cmdq_error(item, "%s", cause);
free(cause);
pr = cmd_parse_from_arguments(args->argc - 1, args->argv + 1, NULL);
switch (pr->status) {
case CMD_PARSE_EMPTY:
cmdq_error(item, "empty command");
return (CMD_RETURN_ERROR);
case CMD_PARSE_ERROR:
cmdq_error(item, "%s", pr->error);
free(pr->error);
return (CMD_RETURN_ERROR);
case CMD_PARSE_SUCCESS:
break;
}
key_bindings_add(tablename, key, args_has(args, 'r'), cmdlist);
return (CMD_RETURN_NORMAL);
}
static enum cmd_retval
cmd_bind_key_mode_table(struct cmd *self, struct cmdq_item *item, key_code key)
{
struct args *args = self->args;
const char *tablename;
const struct mode_key_table *mtab;
struct mode_key_binding *mbind, mtmp;
enum mode_key_cmd cmd;
tablename = args_get(args, 't');
if ((mtab = mode_key_findtable(tablename)) == NULL) {
cmdq_error(item, "unknown key table: %s", tablename);
return (CMD_RETURN_ERROR);
}
cmd = mode_key_fromstring(mtab->cmdstr, args->argv[1]);
if (cmd == MODEKEY_NONE) {
cmdq_error(item, "unknown command: %s", args->argv[1]);
return (CMD_RETURN_ERROR);
}
if (args->argc != 2) {
cmdq_error(item, "no argument allowed");
return (CMD_RETURN_ERROR);
}
mtmp.key = key;
if ((mbind = RB_FIND(mode_key_tree, mtab->tree, &mtmp)) == NULL) {
mbind = xmalloc(sizeof *mbind);
mbind->key = mtmp.key;
RB_INSERT(mode_key_tree, mtab->tree, mbind);
}
mbind->cmd = cmd;
key_bindings_add(tablename, key, args_has(args, 'r'), pr->cmdlist);
return (CMD_RETURN_NORMAL);
}

View File

@ -56,62 +56,6 @@ cmd_list_move(struct cmd_list *cmdlist, struct cmd_list *from)
cmdlist->group = cmd_list_next_group++;
}
struct cmd_list *
cmd_list_parse(int argc, char **argv, const char *file, u_int line,
char **cause)
{
struct cmd_list *cmdlist;
struct cmd *cmd;
int i, lastsplit;
size_t arglen, new_argc;
char **copy_argv, **new_argv;
copy_argv = cmd_copy_argv(argc, argv);
cmdlist = cmd_list_new();
lastsplit = 0;
for (i = 0; i < argc; i++) {
arglen = strlen(copy_argv[i]);
if (arglen == 0 || copy_argv[i][arglen - 1] != ';')
continue;
copy_argv[i][arglen - 1] = '\0';
if (arglen > 1 && copy_argv[i][arglen - 2] == '\\') {
copy_argv[i][arglen - 2] = ';';
continue;
}
new_argc = i - lastsplit;
new_argv = copy_argv + lastsplit;
if (arglen != 1)
new_argc++;
cmd = cmd_parse(new_argc, new_argv, file, line, cause);
if (cmd == NULL)
goto bad;
TAILQ_INSERT_TAIL(&cmdlist->list, cmd, qentry);
lastsplit = i + 1;
}
if (lastsplit != argc) {
cmd = cmd_parse(argc - lastsplit, copy_argv + lastsplit,
file, line, cause);
if (cmd == NULL)
goto bad;
TAILQ_INSERT_TAIL(&cmdlist->list, cmd, qentry);
}
cmd_free_argv(argc, copy_argv);
return (cmdlist);
bad:
cmd_list_free(cmdlist);
cmd_free_argv(argc, copy_argv);
return (NULL);
}
void
cmd_list_free(struct cmd_list *cmdlist)
{

View File

@ -541,34 +541,21 @@ cmd_parse_run_parser(FILE *f, struct cmd_parse_input *pi, char **cause)
return (cmds);
}
struct cmd_parse_result *
cmd_parse_from_file(FILE *f, struct cmd_parse_input *pi)
static struct cmd_parse_result *
cmd_parse_build_commands(struct cmd_parse_commands *cmds,
struct cmd_parse_input *pi)
{
static struct cmd_parse_result pr;
struct cmd_parse_input input;
struct cmd_parse_commands *cmds, *cmds2;
struct cmd_parse_commands *cmds2;
struct cmd_parse_command *cmd, *cmd2, *next, *next2, *after;
FILE *f;
u_int line = UINT_MAX;
int i;
struct cmd_list *cmdlist = NULL, *result;
struct cmd *add;
char *alias, *cause, *s;
if (pi == NULL) {
memset(&input, 0, sizeof input);
pi = &input;
}
memset(&pr, 0, sizeof pr);
/*
* Parse the file into a list of commands.
*/
cmds = cmd_parse_run_parser(f, pi, &cause);
if (cmds == NULL) {
pr.status = CMD_PARSE_ERROR;
pr.error = cause;
return (&pr);
}
/* Check for an empty list. */
if (TAILQ_EMPTY(cmds)) {
free(cmds);
pr.status = CMD_PARSE_EMPTY;
@ -677,13 +664,46 @@ out:
return (&pr);
}
struct cmd_parse_result *
cmd_parse_from_file(FILE *f, struct cmd_parse_input *pi)
{
static struct cmd_parse_result pr;
struct cmd_parse_input input;
struct cmd_parse_commands *cmds;
char *cause;
if (pi == NULL) {
memset(&input, 0, sizeof input);
pi = &input;
}
memset(&pr, 0, sizeof pr);
/*
* Parse the file into a list of commands.
*/
cmds = cmd_parse_run_parser(f, pi, &cause);
if (cmds == NULL) {
pr.status = CMD_PARSE_ERROR;
pr.error = cause;
return (&pr);
}
return (cmd_parse_build_commands(cmds, pi));
}
struct cmd_parse_result *
cmd_parse_from_string(const char *s, struct cmd_parse_input *pi)
{
static struct cmd_parse_result pr;
struct cmd_parse_result *prp;
struct cmd_parse_input input;
FILE *f;
if (pi == NULL) {
memset(&input, 0, sizeof input);
pi = &input;
}
memset(&pr, 0, sizeof pr);
if (*s == '\0') {
pr.status = CMD_PARSE_EMPTY;
pr.cmdlist = NULL;
@ -703,6 +723,84 @@ cmd_parse_from_string(const char *s, struct cmd_parse_input *pi)
return (prp);
}
struct cmd_parse_result *
cmd_parse_from_arguments(int argc, char **argv, struct cmd_parse_input *pi)
{
struct cmd_parse_input input;
struct cmd_parse_commands *cmds;
struct cmd_parse_command *cmd;
char **copy, **new_argv;
size_t size;
int i, last, new_argc;
/*
* The commands are already split up into arguments, so just separate
* into a set of commands by ';'.
*/
if (pi == NULL) {
memset(&input, 0, sizeof input);
pi = &input;
}
cmd_log_argv(argc, argv, "%s", __func__);
cmds = xmalloc(sizeof *cmds);
TAILQ_INIT(cmds);
copy = cmd_copy_argv(argc, argv);
last = 0;
for (i = 0; i < argc; i++) {
size = strlen(copy[i]);
if (size == 0 || copy[i][size - 1] != ';')
continue;
copy[i][--size] = '\0';
if (size > 0 && copy[i][size - 1] == '\\') {
copy[i][size - 1] = ';';
continue;
}
new_argc = i - last;
new_argv = copy + last;
if (size != 0)
new_argc++;
if (new_argc != 0) {
cmd_log_argv(new_argc, new_argv, "%s: at %u", __func__,
i);
cmd = xcalloc(1, sizeof *cmd);
cmd->name = xstrdup(new_argv[0]);
cmd->line = pi->line;
cmd->argc = new_argc - 1;
cmd->argv = cmd_copy_argv(new_argc - 1, new_argv + 1);
TAILQ_INSERT_TAIL(cmds, cmd, entry);
}
last = i + 1;
}
if (last != argc) {
new_argv = copy + last;
new_argc = argc - last;
if (new_argc != 0) {
cmd_log_argv(new_argc, new_argv, "%s: at %u", __func__,
last);
cmd = xcalloc(1, sizeof *cmd);
cmd->name = xstrdup(new_argv[0]);
cmd->line = pi->line;
cmd->argc = new_argc - 1;
cmd->argv = cmd_copy_argv(new_argc - 1, new_argv + 1);
TAILQ_INSERT_TAIL(cmds, cmd, entry);
}
}
return (cmd_parse_build_commands(cmds, pi));
}
static int printflike(1, 2)
yyerror(const char *fmt, ...)
{

View File

@ -1775,9 +1775,9 @@ server_client_dispatch_command(struct client *c, struct imsg *imsg)
struct msg_command_data data;
char *buf;
size_t len;
struct cmd_list *cmdlist = NULL;
int argc;
char **argv, *cause;
struct cmd_parse_result *pr;
if (c->flags & CLIENT_EXIT)
return;
@ -1803,24 +1803,31 @@ server_client_dispatch_command(struct client *c, struct imsg *imsg)
*argv = xstrdup("new-session");
}
if ((cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause)) == NULL) {
cmd_free_argv(argc, argv);
pr = cmd_parse_from_arguments(argc, argv, NULL);
switch (pr->status) {
case CMD_PARSE_EMPTY:
cause = xstrdup("empty command");
goto error;
case CMD_PARSE_ERROR:
cause = pr->error;
goto error;
case CMD_PARSE_SUCCESS:
break;
}
cmd_free_argv(argc, argv);
cmdq_append(c, cmdq_get_command(cmdlist, NULL, NULL, 0));
cmdq_append(c, cmdq_get_command(pr->cmdlist, NULL, NULL, 0));
cmdq_append(c, cmdq_get_callback(server_client_command_done, NULL));
cmd_list_free(cmdlist);
cmd_list_free(pr->cmdlist);
return;
error:
cmd_free_argv(argc, argv);
cmdq_append(c, cmdq_get_error(cause));
free(cause);
if (cmdlist != NULL)
cmd_list_free(cmdlist);
c->flags |= CLIENT_EXIT;
}

3
tmux.h
View File

@ -2016,12 +2016,13 @@ void cmd_parse_empty(struct cmd_parse_input *);
struct cmd_parse_result *cmd_parse_from_file(FILE *, struct cmd_parse_input *);
struct cmd_parse_result *cmd_parse_from_string(const char *,
struct cmd_parse_input *);
struct cmd_parse_result *cmd_parse_from_arguments(int, char **,
struct cmd_parse_input *);
/* cmd-list.c */
struct cmd_list *cmd_list_new(void);
void cmd_list_append(struct cmd_list *, struct cmd *);
void cmd_list_move(struct cmd_list *, struct cmd_list *);
struct cmd_list *cmd_list_parse(int, char **, const char *, u_int, char **);
void cmd_list_free(struct cmd_list *);
char *cmd_list_print(struct cmd_list *, int);