diff options
author | Nicholas Marriott <nicm@cvs.openbsd.org> | 2020-04-13 18:59:42 +0000 |
---|---|---|
committer | Nicholas Marriott <nicm@cvs.openbsd.org> | 2020-04-13 18:59:42 +0000 |
commit | 526936994d11c3b8af12bd0feb50accc97e7914f (patch) | |
tree | a1c4da712a8675d00c89cf927180e1393213a16e /usr.bin/tmux | |
parent | 384cbe3b1bc3d6b900b7a8aebcd81c60cf3b06cc (diff) |
Add helpers for the simple case of parse string and add to command queue.
Diffstat (limited to 'usr.bin/tmux')
-rw-r--r-- | usr.bin/tmux/cmd-command-prompt.c | 26 | ||||
-rw-r--r-- | usr.bin/tmux/cmd-confirm-before.c | 25 | ||||
-rw-r--r-- | usr.bin/tmux/cmd-display-panes.c | 26 | ||||
-rw-r--r-- | usr.bin/tmux/cmd-if-shell.c | 23 | ||||
-rw-r--r-- | usr.bin/tmux/cmd-parse.y | 54 | ||||
-rw-r--r-- | usr.bin/tmux/control.c | 32 | ||||
-rw-r--r-- | usr.bin/tmux/menu.c | 42 | ||||
-rw-r--r-- | usr.bin/tmux/mode-tree.c | 42 | ||||
-rw-r--r-- | usr.bin/tmux/popup.c | 40 | ||||
-rw-r--r-- | usr.bin/tmux/tmux.h | 8 |
10 files changed, 145 insertions, 173 deletions
diff --git a/usr.bin/tmux/cmd-command-prompt.c b/usr.bin/tmux/cmd-command-prompt.c index 00de7d0a147..0cb37cddb48 100644 --- a/usr.bin/tmux/cmd-command-prompt.c +++ b/usr.bin/tmux/cmd-command-prompt.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-command-prompt.c,v 1.49 2020/04/13 15:55:51 nicm Exp $ */ +/* $OpenBSD: cmd-command-prompt.c,v 1.50 2020/04/13 18:59:41 nicm Exp $ */ /* * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com> @@ -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) diff --git a/usr.bin/tmux/cmd-confirm-before.c b/usr.bin/tmux/cmd-confirm-before.c index dfb9cee3974..9c8f127fc13 100644 --- a/usr.bin/tmux/cmd-confirm-before.c +++ b/usr.bin/tmux/cmd-confirm-before.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-confirm-before.c,v 1.39 2020/04/13 15:55:51 nicm Exp $ */ +/* $OpenBSD: cmd-confirm-before.c,v 1.40 2020/04/13 18:59:41 nicm Exp $ */ /* * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org> @@ -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); diff --git a/usr.bin/tmux/cmd-display-panes.c b/usr.bin/tmux/cmd-display-panes.c index 2835f54287c..43e0c2f0759 100644 --- a/usr.bin/tmux/cmd-display-panes.c +++ b/usr.bin/tmux/cmd-display-panes.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-display-panes.c,v 1.32 2020/04/13 15:55:51 nicm Exp $ */ +/* $OpenBSD: cmd-display-panes.c,v 1.33 2020/04/13 18:59:41 nicm Exp $ */ /* * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com> @@ -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); diff --git a/usr.bin/tmux/cmd-if-shell.c b/usr.bin/tmux/cmd-if-shell.c index e2956ab260d..c9b74642946 100644 --- a/usr.bin/tmux/cmd-if-shell.c +++ b/usr.bin/tmux/cmd-if-shell.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-if-shell.c,v 1.72 2020/04/13 15:55:51 nicm Exp $ */ +/* $OpenBSD: cmd-if-shell.c,v 1.73 2020/04/13 18:59:41 nicm Exp $ */ /* * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org> @@ -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); } diff --git a/usr.bin/tmux/cmd-parse.y b/usr.bin/tmux/cmd-parse.y index 93788c64c47..d2bb24fb808 100644 --- a/usr.bin/tmux/cmd-parse.y +++ b/usr.bin/tmux/cmd-parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-parse.y,v 1.25 2020/04/13 16:19:37 nicm Exp $ */ +/* $OpenBSD: cmd-parse.y,v 1.26 2020/04/13 18:59:41 nicm Exp $ */ /* * Copyright (c) 2019 Nicholas Marriott <nicholas.marriott@gmail.com> @@ -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) { diff --git a/usr.bin/tmux/control.c b/usr.bin/tmux/control.c index 195b76a4154..2d003d82111 100644 --- a/usr.bin/tmux/control.c +++ b/usr.bin/tmux/control.c @@ -1,4 +1,4 @@ -/* $OpenBSD: control.c,v 1.29 2020/04/13 15:55:51 nicm Exp $ */ +/* $OpenBSD: control.c,v 1.30 2020/04/13 18:59:41 nicm Exp $ */ /* * Copyright (c) 2012 Nicholas Marriott <nicholas.marriott@gmail.com> @@ -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); } diff --git a/usr.bin/tmux/menu.c b/usr.bin/tmux/menu.c index 1783a950669..822b11390e1 100644 --- a/usr.bin/tmux/menu.c +++ b/usr.bin/tmux/menu.c @@ -1,4 +1,4 @@ -/* $OpenBSD: menu.c,v 1.20 2020/04/13 15:55:51 nicm Exp $ */ +/* $OpenBSD: menu.c,v 1.21 2020/04/13 18:59:41 nicm Exp $ */ /* * Copyright (c) 2019 Nicholas Marriott <nicholas.marriott@gmail.com> @@ -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); } diff --git a/usr.bin/tmux/mode-tree.c b/usr.bin/tmux/mode-tree.c index 85a94c68cd8..11b660010b3 100644 --- a/usr.bin/tmux/mode-tree.c +++ b/usr.bin/tmux/mode-tree.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mode-tree.c,v 1.39 2020/04/13 15:55:51 nicm Exp $ */ +/* $OpenBSD: mode-tree.c,v 1.40 2020/04/13 18:59:41 nicm Exp $ */ /* * Copyright (c) 2017 Nicholas Marriott <nicholas.marriott@gmail.com> @@ -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); } diff --git a/usr.bin/tmux/popup.c b/usr.bin/tmux/popup.c index 529c8fa06e6..d6a9ef3fe70 100644 --- a/usr.bin/tmux/popup.c +++ b/usr.bin/tmux/popup.c @@ -1,4 +1,4 @@ -/* $OpenBSD: popup.c,v 1.12 2020/04/13 15:55:51 nicm Exp $ */ +/* $OpenBSD: popup.c,v 1.13 2020/04/13 18:59:41 nicm Exp $ */ /* * Copyright (c) 2020 Nicholas Marriott <nicholas.marriott@gmail.com> @@ -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: diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h index fd1f5ea12bd..1c8a51ef0f5 100644 --- a/usr.bin/tmux/tmux.h +++ b/usr.bin/tmux/tmux.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tmux.h,v 1.988 2020/04/13 16:19:37 nicm Exp $ */ +/* $OpenBSD: tmux.h,v 1.989 2020/04/13 18:59:41 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> @@ -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 **, |