From 326d2ef234cd8838700e914a0d780f46be50904c Mon Sep 17 00:00:00 2001 From: nicm Date: Sat, 21 Aug 2021 18:39:07 +0000 Subject: Pass typed arguments out of the parser into the arguments list and let it convert them into strings. --- cmd-parse.y | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) (limited to 'cmd-parse.y') diff --git a/cmd-parse.y b/cmd-parse.y index 6be5d8a0..57d0e84e 100644 --- a/cmd-parse.y +++ b/cmd-parse.y @@ -794,39 +794,48 @@ cmd_parse_build_command(struct cmd_parse_command *cmd, struct cmd_parse_input *pi, struct cmd_parse_result *pr) { struct cmd_parse_argument *arg; - struct cmd_list *cmdlist; + struct cmd_list *cmdlist = NULL; struct cmd *add; - char *s, **argv = NULL, *cause; - int argc = 0; + char *cause; + struct args_value *values = NULL; + u_int count = 0, idx; if (cmd_parse_expand_alias(cmd, pi, pr, &cmdlist)) return (cmdlist); TAILQ_FOREACH(arg, &cmd->arguments, entry) { + values = xreallocarray(values, count + 1, sizeof *values); switch (arg->type) { case CMD_PARSE_STRING: - cmd_append_argv(&argc, &argv, arg->string); + values[count].type = ARGS_STRING; + values[count].string = xstrdup(arg->string); break; case CMD_PARSE_COMMANDS: cmd_parse_build_commands(arg->commands, pi, pr); if (pr->status != CMD_PARSE_SUCCESS) - return (NULL); - s = cmd_list_print(pr->cmdlist, 0); - cmd_append_argv(&argc, &argv, s); - free(s); + goto out; + values[count].type = ARGS_COMMANDS; + values[count].cmdlist = pr->cmdlist; + values[count].cmdlist->references++; break; } + count++; } - add = cmd_parse(argc, argv, pi->file, pi->line, &cause); + add = cmd_parse(values, count, pi->file, pi->line, &cause); if (add == NULL) { pr->status = CMD_PARSE_ERROR; pr->error = cmd_parse_get_error(pi->file, pi->line, cause); free(cause); - return (NULL); + goto out; } cmdlist = cmd_list_new(); cmd_list_append(cmdlist, add); + +out: + for (idx = 0; idx < count; idx++) + args_free_value(&values[idx]); + free(values); return (cmdlist); } -- cgit From 069f5925af8bf70a99eec7f4baf5772707e62def Mon Sep 17 00:00:00 2001 From: nicm Date: Sat, 21 Aug 2021 20:46:43 +0000 Subject: Preserve argument type in command and convert to string on demand. --- cmd-parse.y | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'cmd-parse.y') diff --git a/cmd-parse.y b/cmd-parse.y index 57d0e84e..86917941 100644 --- a/cmd-parse.y +++ b/cmd-parse.y @@ -804,7 +804,8 @@ cmd_parse_build_command(struct cmd_parse_command *cmd, return (cmdlist); TAILQ_FOREACH(arg, &cmd->arguments, entry) { - values = xreallocarray(values, count + 1, sizeof *values); + values = xrecallocarray(values, count, count + 1, + sizeof *values); switch (arg->type) { case CMD_PARSE_STRING: values[count].type = ARGS_STRING; -- cgit