summaryrefslogtreecommitdiffstats
path: root/cmd.c
diff options
context:
space:
mode:
authorThomas Adam <thomas@xteddy.org>2021-08-27 20:01:11 +0100
committerThomas Adam <thomas@xteddy.org>2021-08-27 20:01:11 +0100
commit609baea95e30201919a173ea1de1f750a15a9f8c (patch)
tree68225d4bd3ba7e258cddb8e6b40f0764408a6e32 /cmd.c
parentc6375a0d4003d1008bb64e96e9c0c4433e4a5d13 (diff)
parentdaec63e5e6eb3390d53f4bf7f8a327df77e46c95 (diff)
Merge branch 'obsd-master' into master
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 4b1dbbbf..44952d11 100644
--- a/cmd.c
+++ b/cmd.c
@@ -543,6 +543,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)
@@ -617,6 +634,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)