summaryrefslogtreecommitdiffstats
path: root/cmd.c
diff options
context:
space:
mode:
authornicm <nicm>2021-08-27 17:25:55 +0000
committernicm <nicm>2021-08-27 17:25:55 +0000
commitdaec63e5e6eb3390d53f4bf7f8a327df77e46c95 (patch)
treecf78624cb0b4a80a6eeb8e29b368ea4f9beb5610 /cmd.c
parentfd756a150b43d319d08ac4117f34edef9e0438c4 (diff)
Replace %% in command lists (by copying them) for template arguments ,
this means they can be used with {} as well. Also make argument processing from an existing vector preserve commands. GitHub issue 2858.
Diffstat (limited to 'cmd.c')
-rw-r--r--cmd.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/cmd.c b/cmd.c
index 62c8b9ab..5647a861 100644
--- a/cmd.c
+++ b/cmd.c
@@ -544,6 +544,23 @@ cmd_free(struct cmd *cmd)
free(cmd);
}
+/* Copy a command. */
+struct cmd *
+cmd_copy(struct cmd *cmd, int argc, char **argv)
+{
+ struct cmd *new_cmd;
+
+ new_cmd = xcalloc(1, sizeof *new_cmd);
+ new_cmd->entry = cmd->entry;
+ new_cmd->args = args_copy(cmd->args, argc, argv);
+
+ if (cmd->file != NULL)
+ new_cmd->file = xstrdup(cmd->file);
+ new_cmd->line = cmd->line;
+
+ return (new_cmd);
+}
+
/* Get a command as a string. */
char *
cmd_print(struct cmd *cmd)
@@ -618,6 +635,37 @@ cmd_list_free(struct cmd_list *cmdlist)
free(cmdlist);
}
+/* Copy a command list, expanding %s in arguments. */
+struct cmd_list *
+cmd_list_copy(struct cmd_list *cmdlist, int argc, char **argv)
+{
+ struct cmd *cmd;
+ struct cmd_list *new_cmdlist;
+ struct cmd *new_cmd;
+ u_int group = cmdlist->group;
+ char *s;
+
+ s = cmd_list_print(cmdlist, 0);
+ log_debug("%s: %s", __func__, s);
+ free(s);
+
+ new_cmdlist = cmd_list_new();
+ TAILQ_FOREACH(cmd, cmdlist->list, qentry) {
+ if (cmd->group != group) {
+ new_cmdlist->group = cmd_list_next_group++;
+ group = cmd->group;
+ }
+ new_cmd = cmd_copy(cmd, argc, argv);
+ cmd_list_append(new_cmdlist, new_cmd);
+ }
+
+ s = cmd_list_print(new_cmdlist, 0);
+ log_debug("%s: %s", __func__, s);
+ free(s);
+
+ return (new_cmdlist);
+}
+
/* Get a command list as a string. */
char *
cmd_list_print(struct cmd_list *cmdlist, int escaped)