From 6d56dd9e7becdf2307cad322a3cae0e095f35919 Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Tue, 30 Jun 2026 22:11:25 +0100 Subject: [PATCH] Move file and flags into parser. --- cmd-invoke.c | 29 +++++++++++++---------------- cmd-parse.y | 28 ++++++++++++++++++++++++---- tmux-parser.h | 5 ++++- 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/cmd-invoke.c b/cmd-invoke.c index 7d456f968..97ffb7e5f 100644 --- a/cmd-invoke.c +++ b/cmd-invoke.c @@ -45,8 +45,6 @@ struct cmd_invoke_state { int argc; char **argv; - int parse_flags; - char *file; }; static void cmd_invoke_push(struct cmd_invoke_state *, @@ -61,9 +59,9 @@ static int cmd_invoke_assignment(struct cmdq_item *, struct cmd_invoke_state *, struct cmd_parse_node *); static int cmd_invoke_if(struct cmdq_item *, struct cmd_invoke_state *, struct cmd_parse_node *); -static struct cmd *cmd_invoke_build_command(struct cmdq_item *, - struct cmd_invoke_state *, struct cmd_parse_node *); -static int cmd_invoke_read_only(struct cmdq_item *, struct cmd *); +static struct cmd *cmd_invoke_build_command(struct cmdq_item *, + struct cmd_invoke_state *, struct cmd_parse_node *); +static int cmd_invoke_read_only(struct cmdq_item *, struct cmd *); /* Push a node's child range onto the traversal stack. */ static void @@ -321,15 +319,16 @@ static void cmd_invoke_error(struct cmdq_item *item, struct cmd_invoke_state *is, struct cmd_parse_node *node, const char *cause) { - u_int line = cmd_parse_node_line(node); + const char *file = cmd_parse_file(is->tree); + u_int line = cmd_parse_node_line(node); if (cmdq_get_client(item) != NULL) { cmdq_error(item, "%s", cause); return; } - if (is->file != NULL) - cfg_add_cause("%s:%u: %s", is->file, line, cause); + if (file != NULL) + cfg_add_cause("%s:%u: %s", file, line, cause); else cfg_add_cause("%s", cause); } @@ -339,9 +338,13 @@ static struct cmd * cmd_invoke_build_command(struct cmdq_item *item, struct cmd_invoke_state *is, struct cmd_parse_node *node) { + struct cmd_parse_tree *tree = is->tree; struct cmd_parse_node *child; struct args_value *values = NULL; struct cmd *cmd; + const char *file = cmd_parse_file(is->tree); + u_int line = cmd_parse_node_line(node); + int flags = cmd_parse_flags(is->tree); char *cause = NULL; u_int count = 0; @@ -358,7 +361,7 @@ cmd_invoke_build_command(struct cmdq_item *item, struct cmd_invoke_state *is, break; case CMD_PARSE_COMMANDS: values[count].type = ARGS_COMMANDS; - values[count].cmd = cmd_parse_from_node(child); + values[count].cmd = cmd_parse_from_node(tree, child); break; default: fatalx("unexpected node type in command"); @@ -367,8 +370,7 @@ cmd_invoke_build_command(struct cmdq_item *item, struct cmd_invoke_state *is, child = cmd_parse_node_next(child); } - cmd = cmd_parse(values, count, is->file, cmd_parse_node_line(node), - is->parse_flags, &cause); + cmd = cmd_parse(values, count, file, line, flags, &cause); if (cmd == NULL) { cmd_invoke_error(item, is, node, cause); free(cause); @@ -416,10 +418,6 @@ cmd_invoke_get(struct cmd_parse_tree *tree, struct cmdq_state *state, is->references = 1; is->tree = cmd_parse_add_ref(tree); - if (input->file != NULL) - is->file = xstrdup(input->file); - is->parse_flags = input->flags; - is->argc = input->argc; if (input->argc != 0) { is->argv = xreallocarray(NULL, input->argc, sizeof *is->argv); @@ -459,7 +457,6 @@ cmd_invoke_state_free(struct cmd_invoke_state *is) free(is->argv); cmd_parse_free(is->tree); - free(is->file); free(is->stack); free(is); } diff --git a/cmd-parse.y b/cmd-parse.y index 64bce3a3b..4da4a7042 100644 --- a/cmd-parse.y +++ b/cmd-parse.y @@ -57,6 +57,8 @@ struct cmd_parse_node { struct cmd_parse_tree { int references; + char *file; + int flags; struct cmd_parse_node *root; }; @@ -619,6 +621,8 @@ cmd_parse_run_parser(char **cause) tree = xcalloc(1, sizeof *tree); tree->references = 1; + tree->file = ps->input->file != NULL ? xstrdup(ps->input->file) : NULL; + tree->flags = (ps->input->flags & ~CMD_PARSE_ONEGROUP); tree->root = root; return (tree); } @@ -694,7 +698,7 @@ cmd_parse_from_string(const char *s, struct cmd_parse_input *pi, char **cause) } struct cmd_parse_tree * -cmd_parse_from_node(struct cmd_parse_node *node) +cmd_parse_from_node(struct cmd_parse_tree *tree, struct cmd_parse_node *node) { struct cmd_parse_tree *new; struct cmd_parse_node *root, *child, *copy; @@ -708,6 +712,8 @@ cmd_parse_from_node(struct cmd_parse_node *node) new = xcalloc(1, sizeof *new); new->references = 1; + new->file = tree->file != NULL ? xstrdup(tree->file) : NULL; + new->flags = tree->flags; new->root = root; return (new); } @@ -825,6 +831,7 @@ cmd_parse_free(struct cmd_parse_tree *tree) { if (tree != NULL && --tree->references == 0) { cmd_parse_free_node(tree->root); + free(tree->file); free(tree); } } @@ -835,6 +842,18 @@ cmd_parse_root(struct cmd_parse_tree *tree) return (tree->root); } +const char * +cmd_parse_file(struct cmd_parse_tree *tree) +{ + return (tree->file); +} + +int +cmd_parse_flags(struct cmd_parse_tree *tree) +{ + return (tree->flags); +} + enum cmd_parse_node_type cmd_parse_node_type(struct cmd_parse_node *node) { @@ -1262,7 +1281,8 @@ cmd_parse_make_string(struct cmd_parse_node *node) } static int -cmd_parse_command_any_have(struct cmd_parse_node *node, int flag) +cmd_parse_command_any_have(struct cmd_parse_tree *tree, + struct cmd_parse_node *node, int flag) { struct cmd_parse_node *child; struct args_value *values = NULL; @@ -1289,7 +1309,7 @@ cmd_parse_command_any_have(struct cmd_parse_node *node, int flag) break; case CMD_PARSE_COMMANDS: values[count].type = ARGS_COMMANDS; - values[count].cmd = cmd_parse_from_node(child); + values[count].cmd = cmd_parse_from_node(tree, child); break; default: fatalx("unexpected node type in command"); @@ -1326,7 +1346,7 @@ cmd_parse_any_have(struct cmd_parse_tree *tree, int flag) TAILQ_FOREACH(node, &seq->children, entry) { if (node->type != CMD_PARSE_COMMAND) continue; - r = cmd_parse_command_any_have(node, flag); + r = cmd_parse_command_any_have(tree, node, flag); if (r < 0) return (0); if (r) diff --git a/tmux-parser.h b/tmux-parser.h index bb59020ef..a6d6c31ff 100644 --- a/tmux-parser.h +++ b/tmux-parser.h @@ -77,12 +77,15 @@ struct cmd_parse_tree *cmd_parse_from_buffer(const void *, size_t, struct cmd_parse_input *, char **); struct cmd_parse_tree *cmd_parse_from_string(const char *, struct cmd_parse_input *, char **); -struct cmd_parse_tree *cmd_parse_from_node(struct cmd_parse_node *); +struct cmd_parse_tree *cmd_parse_from_node(struct cmd_parse_tree *, + struct cmd_parse_node *); struct cmd_parse_tree *cmd_parse_from_arguments(struct args_value *, u_int); struct cmd_parse_tree *cmd_parse_add_ref(struct cmd_parse_tree *); int cmd_parse_any_have(struct cmd_parse_tree *, int); void cmd_parse_free(struct cmd_parse_tree *); struct cmd_parse_node *cmd_parse_root(struct cmd_parse_tree *); +const char *cmd_parse_file(struct cmd_parse_tree *); +int cmd_parse_flags(struct cmd_parse_tree *); char *cmd_parse_print(struct cmd_parse_tree *); void cmd_parse_log(const char *, struct cmd_parse_tree *); void cmd_parse_log_node(const char *,