Add helpers for the simple case of parse string and add to command queue.

This commit is contained in:
nicm 2020-04-13 18:59:41 +00:00
parent 34804f2709
commit 187277eaad
10 changed files with 135 additions and 163 deletions

View File

@ -136,10 +136,9 @@ cmd_command_prompt_callback(struct client *c, void *data, const char *s,
int done)
{
struct cmd_command_prompt_cdata *cdata = data;
struct cmdq_item *new_item;
char *new_template, *prompt, *ptr;
char *new_template, *prompt, *ptr, *error;
char *input = NULL;
struct cmd_parse_result *pr;
enum cmd_parse_status status;
if (s == NULL)
return (0);
@ -166,21 +165,10 @@ cmd_command_prompt_callback(struct client *c, void *data, const char *s,
return (1);
}
pr = cmd_parse_from_string(new_template, NULL);
switch (pr->status) {
case CMD_PARSE_EMPTY:
new_item = NULL;
break;
case CMD_PARSE_ERROR:
new_item = cmdq_get_error(pr->error);
free(pr->error);
cmdq_append(c, new_item);
break;
case CMD_PARSE_SUCCESS:
new_item = cmdq_get_command(pr->cmdlist, NULL);
cmd_list_free(pr->cmdlist);
cmdq_append(c, new_item);
break;
status = cmd_parse_and_append(new_template, NULL, c, NULL, &error);
if (status == CMD_PARSE_ERROR) {
cmdq_append(c, cmdq_get_error(error));
free(error);
}
if (!done)

View File

@ -87,8 +87,8 @@ cmd_confirm_before_callback(struct client *c, void *data, const char *s,
__unused int done)
{
struct cmd_confirm_before_data *cdata = data;
struct cmdq_item *new_item;
struct cmd_parse_result *pr;
char *error;
enum cmd_parse_status status;
if (c->flags & CLIENT_DEAD)
return (0);
@ -98,21 +98,10 @@ cmd_confirm_before_callback(struct client *c, void *data, const char *s,
if (tolower((u_char)s[0]) != 'y' || s[1] != '\0')
return (0);
pr = cmd_parse_from_string(cdata->cmd, NULL);
switch (pr->status) {
case CMD_PARSE_EMPTY:
new_item = NULL;
break;
case CMD_PARSE_ERROR:
new_item = cmdq_get_error(pr->error);
free(pr->error);
cmdq_append(c, new_item);
break;
case CMD_PARSE_SUCCESS:
new_item = cmdq_get_command(pr->cmdlist, NULL);
cmd_list_free(pr->cmdlist);
cmdq_append(c, new_item);
break;
status = cmd_parse_and_append(cdata->cmd, NULL, c, NULL, &error);
if (status == CMD_PARSE_ERROR) {
cmdq_append(c, cmdq_get_error(error));
free(error);
}
return (0);

View File

@ -197,11 +197,10 @@ static int
cmd_display_panes_key(struct client *c, struct key_event *event)
{
struct cmd_display_panes_data *cdata = c->overlay_data;
struct cmdq_item *new_item;
char *cmd, *expanded;
char *cmd, *expanded, *error;
struct window *w = c->session->curw->window;
struct window_pane *wp;
struct cmd_parse_result *pr;
enum cmd_parse_status status;
if (event->key < '0' || event->key > '9')
return (-1);
@ -214,21 +213,10 @@ cmd_display_panes_key(struct client *c, struct key_event *event)
xasprintf(&expanded, "%%%u", wp->id);
cmd = cmd_template_replace(cdata->command, expanded, 1);
pr = cmd_parse_from_string(cmd, NULL);
switch (pr->status) {
case CMD_PARSE_EMPTY:
new_item = NULL;
break;
case CMD_PARSE_ERROR:
new_item = cmdq_get_error(pr->error);
free(pr->error);
cmdq_append(c, new_item);
break;
case CMD_PARSE_SUCCESS:
new_item = cmdq_get_command(pr->cmdlist, NULL);
cmd_list_free(pr->cmdlist);
cmdq_append(c, new_item);
break;
status = cmd_parse_and_append(cmd, NULL, c, NULL, &error);
if (status == CMD_PARSE_ERROR) {
cmdq_append(c, cmdq_get_error(error));
free(error);
}
free(cmd);

View File

@ -65,13 +65,12 @@ cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)
struct cmd_find_state *target = cmdq_get_target(item);
struct cmdq_state *state = cmdq_get_state(item);
struct cmd_if_shell_data *cdata;
char *shellcmd, *cmd;
char *shellcmd, *cmd, *error;
const char *file;
struct cmdq_item *new_item;
struct client *c = cmd_find_client(item, NULL, 1);
struct session *s = target->s;
struct cmd_parse_input pi;
struct cmd_parse_result *pr;
enum cmd_parse_status status;
shellcmd = format_single_from_target(item, args->argv[0], c);
if (args_has(args, 'F')) {
@ -91,19 +90,11 @@ cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)
pi.c = c;
cmd_find_copy_state(&pi.fs, target);
pr = cmd_parse_from_string(cmd, &pi);
switch (pr->status) {
case CMD_PARSE_EMPTY:
break;
case CMD_PARSE_ERROR:
cmdq_error(item, "%s", pr->error);
free(pr->error);
status = cmd_parse_and_insert(cmd, &pi, item, state, &error);
if (status == CMD_PARSE_ERROR) {
cmdq_error(item, "%s", error);
free(error);
return (CMD_RETURN_ERROR);
case CMD_PARSE_SUCCESS:
new_item = cmdq_get_command(pr->cmdlist, state);
cmdq_insert_after(item, new_item);
cmd_list_free(pr->cmdlist);
break;
}
return (CMD_RETURN_NORMAL);
}

View File

@ -793,6 +793,58 @@ cmd_parse_from_string(const char *s, struct cmd_parse_input *pi)
return (cmd_parse_from_buffer(s, strlen(s), pi));
}
enum cmd_parse_status
cmd_parse_and_insert(const char *s, struct cmd_parse_input *pi,
struct cmdq_item *after, struct cmdq_state *state, char **error)
{
struct cmd_parse_result *pr;
struct cmdq_item *item;
pr = cmd_parse_from_string(s, pi);
switch (pr->status) {
case CMD_PARSE_EMPTY:
break;
case CMD_PARSE_ERROR:
if (error != NULL)
*error = pr->error;
else
free(pr->error);
break;
case CMD_PARSE_SUCCESS:
item = cmdq_get_command(pr->cmdlist, state);
cmdq_insert_after(after, item);
cmd_list_free(pr->cmdlist);
break;
}
return (pr->status);
}
enum cmd_parse_status
cmd_parse_and_append(const char *s, struct cmd_parse_input *pi,
struct client *c, struct cmdq_state *state, char **error)
{
struct cmd_parse_result *pr;
struct cmdq_item *item;
pr = cmd_parse_from_string(s, pi);
switch (pr->status) {
case CMD_PARSE_EMPTY:
break;
case CMD_PARSE_ERROR:
if (error != NULL)
*error = pr->error;
else
free(pr->error);
break;
case CMD_PARSE_SUCCESS:
item = cmdq_get_command(pr->cmdlist, state);
cmdq_append(c, item);
cmd_list_free(pr->cmdlist);
break;
}
return (pr->status);
}
struct cmd_parse_result *
cmd_parse_from_buffer(const void *buf, size_t len, struct cmd_parse_input *pi)
{

View File

@ -56,14 +56,13 @@ control_error(struct cmdq_item *item, void *data)
/* Control input callback. Read lines and fire commands. */
static void
control_callback(__unused struct client *c, __unused const char *path,
int error, int closed, struct evbuffer *buffer, __unused void *data)
int read_error, int closed, struct evbuffer *buffer, __unused void *data)
{
char *line;
struct cmdq_item *item;
char *line, *error;
struct cmdq_state *state;
struct cmd_parse_result *pr;
enum cmd_parse_status status;
if (closed || error != 0)
if (closed || read_error != 0)
c->flags |= CLIENT_EXIT;
for (;;) {
@ -77,22 +76,11 @@ control_callback(__unused struct client *c, __unused const char *path,
break;
}
pr = cmd_parse_from_string(line, NULL);
switch (pr->status) {
case CMD_PARSE_EMPTY:
break;
case CMD_PARSE_ERROR:
item = cmdq_get_callback(control_error, pr->error);
cmdq_append(c, item);
break;
case CMD_PARSE_SUCCESS:
state = cmdq_new_state(NULL, NULL, CMDQ_STATE_CONTROL);
item = cmdq_get_command(pr->cmdlist, state);
cmdq_append(c, item);
cmdq_free_state(state);
cmd_list_free(pr->cmdlist);
break;
}
state = cmdq_new_state(NULL, NULL, CMDQ_STATE_CONTROL);
status = cmd_parse_and_append(line, NULL, c, state, &error);
if (status == CMD_PARSE_ERROR)
cmdq_append(c, cmdq_get_callback(control_error, error));
cmdq_free_state(state);
free(line);
}

40
menu.c
View File

@ -183,11 +183,11 @@ menu_key_cb(struct client *c, struct key_event *event)
struct mouse_event *m = &event->m;
u_int i;
int count = menu->count, old = md->choice;
const struct menu_item *item;
struct cmdq_item *new_item;
struct cmdq_state *new_state;
struct cmd_parse_result *pr;
const char *name;
const struct menu_item *item;
struct cmdq_state *state;
enum cmd_parse_status status;
char *error;
if (KEYC_IS_MOUSE(event->key)) {
if (md->flags & MENU_NOMOUSE) {
@ -272,27 +272,19 @@ chosen:
return (1);
}
pr = cmd_parse_from_string(item->command, NULL);
switch (pr->status) {
case CMD_PARSE_EMPTY:
break;
case CMD_PARSE_ERROR:
new_item = cmdq_get_error(pr->error);
free(pr->error);
cmdq_append(c, new_item);
break;
case CMD_PARSE_SUCCESS:
if (md->item != NULL)
event = cmdq_get_event(md->item);
else
event = NULL;
new_state = cmdq_new_state(&md->fs, event, 0);
new_item = cmdq_get_command(pr->cmdlist, new_state);
cmdq_free_state(new_state);
cmd_list_free(pr->cmdlist);
cmdq_append(c, new_item);
break;
if (md->item != NULL)
event = cmdq_get_event(md->item);
else
event = NULL;
state = cmdq_new_state(&md->fs, event, 0);
status = cmd_parse_and_append(item->command, NULL, c, state, &error);
if (status == CMD_PARSE_ERROR) {
cmdq_append(c, cmdq_get_error(error));
free(error);
}
cmdq_free_state(state);
return (1);
}

View File

@ -1063,36 +1063,22 @@ void
mode_tree_run_command(struct client *c, struct cmd_find_state *fs,
const char *template, const char *name)
{
struct cmdq_item *new_item;
struct cmdq_state *new_state;
char *command;
struct cmd_parse_result *pr;
struct cmdq_state *state;
char *command, *error;
enum cmd_parse_status status;
command = cmd_template_replace(template, name, 1);
if (command == NULL || *command == '\0') {
free(command);
return;
}
pr = cmd_parse_from_string(command, NULL);
switch (pr->status) {
case CMD_PARSE_EMPTY:
break;
case CMD_PARSE_ERROR:
if (c != NULL) {
*pr->error = toupper((u_char)*pr->error);
status_message_set(c, "%s", pr->error);
if (command != NULL && *command != '\0') {
state = cmdq_new_state(fs, NULL, 0);
status = cmd_parse_and_append(command, NULL, c, state, &error);
if (status == CMD_PARSE_ERROR) {
if (c != NULL) {
*error = toupper((u_char)*error);
status_message_set(c, "%s", error);
}
free(error);
}
free(pr->error);
break;
case CMD_PARSE_SUCCESS:
new_state = cmdq_new_state(fs, NULL, 0);
new_item = cmdq_get_command(pr->cmdlist, new_state);
cmdq_free_state(new_state);
cmdq_append(c, new_item);
cmd_list_free(pr->cmdlist);
break;
cmdq_free_state(state);
}
free(command);
}

38
popup.c
View File

@ -222,12 +222,12 @@ popup_key_cb(struct client *c, struct key_event *event)
struct popup_data *pd = c->overlay_data;
struct mouse_event *m = &event->m;
struct cmd_find_state *fs = &pd->fs;
struct cmdq_item *new_item;
struct cmdq_state *new_state;
struct cmd_parse_result *pr;
struct format_tree *ft;
const char *cmd, *buf;
size_t len;
struct cmdq_state *state;
enum cmd_parse_status status;
char *error;
if (KEYC_IS_MOUSE(event->key)) {
if (pd->dragging != OFF) {
@ -295,27 +295,19 @@ popup_key_cb(struct client *c, struct key_event *event)
cmd = format_expand(ft, pd->cmd);
format_free(ft);
pr = cmd_parse_from_string(cmd, NULL);
switch (pr->status) {
case CMD_PARSE_EMPTY:
break;
case CMD_PARSE_ERROR:
new_item = cmdq_get_error(pr->error);
free(pr->error);
cmdq_append(c, new_item);
break;
case CMD_PARSE_SUCCESS:
if (pd->item != NULL)
event = cmdq_get_event(pd->item);
else
event = NULL;
new_state = cmdq_new_state(&pd->fs, event, 0);
new_item = cmdq_get_command(pr->cmdlist, new_state);
cmdq_free_state(new_state);
cmd_list_free(pr->cmdlist);
cmdq_append(c, new_item);
break;
if (pd->item != NULL)
event = cmdq_get_event(pd->item);
else
event = NULL;
state = cmdq_new_state(&pd->fs, event, 0);
status = cmd_parse_and_append(cmd, NULL, c, state, &error);
if (status == CMD_PARSE_ERROR) {
cmdq_append(c, cmdq_get_error(error));
free(error);
}
cmdq_free_state(state);
return (1);
out:

6
tmux.h
View File

@ -2089,6 +2089,12 @@ 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 *);
enum cmd_parse_status cmd_parse_and_insert(const char *,
struct cmd_parse_input *, struct cmdq_item *,
struct cmdq_state *, char **);
enum cmd_parse_status cmd_parse_and_append(const char *,
struct cmd_parse_input *, struct client *,
struct cmdq_state *, char **);
struct cmd_parse_result *cmd_parse_from_buffer(const void *, size_t,
struct cmd_parse_input *);
struct cmd_parse_result *cmd_parse_from_arguments(int, char **,