Move file and flags into parser.

This commit is contained in:
Nicholas Marriott
2026-06-30 22:11:25 +01:00
parent 8ca7434d65
commit 6d56dd9e7b
3 changed files with 41 additions and 21 deletions

View File

@@ -45,8 +45,6 @@ struct cmd_invoke_state {
int argc; int argc;
char **argv; char **argv;
int parse_flags;
char *file;
}; };
static void cmd_invoke_push(struct cmd_invoke_state *, 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 *); struct cmd_invoke_state *, struct cmd_parse_node *);
static int cmd_invoke_if(struct cmdq_item *, struct cmd_invoke_state *, static int cmd_invoke_if(struct cmdq_item *, struct cmd_invoke_state *,
struct cmd_parse_node *); struct cmd_parse_node *);
static struct cmd *cmd_invoke_build_command(struct cmdq_item *, static struct cmd *cmd_invoke_build_command(struct cmdq_item *,
struct cmd_invoke_state *, struct cmd_parse_node *); struct cmd_invoke_state *, struct cmd_parse_node *);
static int cmd_invoke_read_only(struct cmdq_item *, struct cmd *); static int cmd_invoke_read_only(struct cmdq_item *, struct cmd *);
/* Push a node's child range onto the traversal stack. */ /* Push a node's child range onto the traversal stack. */
static void static void
@@ -321,15 +319,16 @@ static void
cmd_invoke_error(struct cmdq_item *item, struct cmd_invoke_state *is, cmd_invoke_error(struct cmdq_item *item, struct cmd_invoke_state *is,
struct cmd_parse_node *node, const char *cause) 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) { if (cmdq_get_client(item) != NULL) {
cmdq_error(item, "%s", cause); cmdq_error(item, "%s", cause);
return; return;
} }
if (is->file != NULL) if (file != NULL)
cfg_add_cause("%s:%u: %s", is->file, line, cause); cfg_add_cause("%s:%u: %s", file, line, cause);
else else
cfg_add_cause("%s", cause); 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, cmd_invoke_build_command(struct cmdq_item *item, struct cmd_invoke_state *is,
struct cmd_parse_node *node) struct cmd_parse_node *node)
{ {
struct cmd_parse_tree *tree = is->tree;
struct cmd_parse_node *child; struct cmd_parse_node *child;
struct args_value *values = NULL; struct args_value *values = NULL;
struct cmd *cmd; 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; char *cause = NULL;
u_int count = 0; u_int count = 0;
@@ -358,7 +361,7 @@ cmd_invoke_build_command(struct cmdq_item *item, struct cmd_invoke_state *is,
break; break;
case CMD_PARSE_COMMANDS: case CMD_PARSE_COMMANDS:
values[count].type = ARGS_COMMANDS; values[count].type = ARGS_COMMANDS;
values[count].cmd = cmd_parse_from_node(child); values[count].cmd = cmd_parse_from_node(tree, child);
break; break;
default: default:
fatalx("unexpected node type in command"); 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); child = cmd_parse_node_next(child);
} }
cmd = cmd_parse(values, count, is->file, cmd_parse_node_line(node), cmd = cmd_parse(values, count, file, line, flags, &cause);
is->parse_flags, &cause);
if (cmd == NULL) { if (cmd == NULL) {
cmd_invoke_error(item, is, node, cause); cmd_invoke_error(item, is, node, cause);
free(cause); free(cause);
@@ -416,10 +418,6 @@ cmd_invoke_get(struct cmd_parse_tree *tree, struct cmdq_state *state,
is->references = 1; is->references = 1;
is->tree = cmd_parse_add_ref(tree); 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; is->argc = input->argc;
if (input->argc != 0) { if (input->argc != 0) {
is->argv = xreallocarray(NULL, input->argc, sizeof *is->argv); 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); free(is->argv);
cmd_parse_free(is->tree); cmd_parse_free(is->tree);
free(is->file);
free(is->stack); free(is->stack);
free(is); free(is);
} }

View File

@@ -57,6 +57,8 @@ struct cmd_parse_node {
struct cmd_parse_tree { struct cmd_parse_tree {
int references; int references;
char *file;
int flags;
struct cmd_parse_node *root; struct cmd_parse_node *root;
}; };
@@ -619,6 +621,8 @@ cmd_parse_run_parser(char **cause)
tree = xcalloc(1, sizeof *tree); tree = xcalloc(1, sizeof *tree);
tree->references = 1; 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; tree->root = root;
return (tree); return (tree);
} }
@@ -694,7 +698,7 @@ cmd_parse_from_string(const char *s, struct cmd_parse_input *pi, char **cause)
} }
struct cmd_parse_tree * 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_tree *new;
struct cmd_parse_node *root, *child, *copy; 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 = xcalloc(1, sizeof *new);
new->references = 1; new->references = 1;
new->file = tree->file != NULL ? xstrdup(tree->file) : NULL;
new->flags = tree->flags;
new->root = root; new->root = root;
return (new); return (new);
} }
@@ -825,6 +831,7 @@ cmd_parse_free(struct cmd_parse_tree *tree)
{ {
if (tree != NULL && --tree->references == 0) { if (tree != NULL && --tree->references == 0) {
cmd_parse_free_node(tree->root); cmd_parse_free_node(tree->root);
free(tree->file);
free(tree); free(tree);
} }
} }
@@ -835,6 +842,18 @@ cmd_parse_root(struct cmd_parse_tree *tree)
return (tree->root); 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 enum cmd_parse_node_type
cmd_parse_node_type(struct cmd_parse_node *node) cmd_parse_node_type(struct cmd_parse_node *node)
{ {
@@ -1262,7 +1281,8 @@ cmd_parse_make_string(struct cmd_parse_node *node)
} }
static int 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 cmd_parse_node *child;
struct args_value *values = NULL; struct args_value *values = NULL;
@@ -1289,7 +1309,7 @@ cmd_parse_command_any_have(struct cmd_parse_node *node, int flag)
break; break;
case CMD_PARSE_COMMANDS: case CMD_PARSE_COMMANDS:
values[count].type = ARGS_COMMANDS; values[count].type = ARGS_COMMANDS;
values[count].cmd = cmd_parse_from_node(child); values[count].cmd = cmd_parse_from_node(tree, child);
break; break;
default: default:
fatalx("unexpected node type in command"); 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) { TAILQ_FOREACH(node, &seq->children, entry) {
if (node->type != CMD_PARSE_COMMAND) if (node->type != CMD_PARSE_COMMAND)
continue; continue;
r = cmd_parse_command_any_have(node, flag); r = cmd_parse_command_any_have(tree, node, flag);
if (r < 0) if (r < 0)
return (0); return (0);
if (r) if (r)

View File

@@ -77,12 +77,15 @@ struct cmd_parse_tree *cmd_parse_from_buffer(const void *, size_t,
struct cmd_parse_input *, char **); struct cmd_parse_input *, char **);
struct cmd_parse_tree *cmd_parse_from_string(const char *, struct cmd_parse_tree *cmd_parse_from_string(const char *,
struct cmd_parse_input *, 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_from_arguments(struct args_value *, u_int);
struct cmd_parse_tree *cmd_parse_add_ref(struct cmd_parse_tree *); struct cmd_parse_tree *cmd_parse_add_ref(struct cmd_parse_tree *);
int cmd_parse_any_have(struct cmd_parse_tree *, int); int cmd_parse_any_have(struct cmd_parse_tree *, int);
void cmd_parse_free(struct cmd_parse_tree *); void cmd_parse_free(struct cmd_parse_tree *);
struct cmd_parse_node *cmd_parse_root(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 *); char *cmd_parse_print(struct cmd_parse_tree *);
void cmd_parse_log(const char *, struct cmd_parse_tree *); void cmd_parse_log(const char *, struct cmd_parse_tree *);
void cmd_parse_log_node(const char *, void cmd_parse_log_node(const char *,