Move command argument parsing common functions and don't bother to parse

again if given a command rather than a string.
This commit is contained in:
nicm
2021-08-23 12:33:55 +00:00
parent 03b83a5a34
commit 210e71edf3
7 changed files with 330 additions and 246 deletions

View File

@@ -53,14 +53,13 @@ const struct cmd_entry cmd_run_shell_entry = {
struct cmd_run_shell_data {
struct client *client;
char *cmd;
int shell;
struct cmd_list *cmdlist;
char *cwd;
struct cmdq_item *item;
struct session *s;
int wp_id;
struct event timer;
int flags;
struct cmd_parse_input pi;
};
static void
@@ -100,11 +99,10 @@ cmd_run_shell_exec(struct cmd *self, struct cmdq_item *item)
struct client *tc = cmdq_get_target_client(item);
struct session *s = target->s;
struct window_pane *wp = target->wp;
const char *delay;
const char *delay, *cmd;
double d;
struct timeval tv;
char *end;
const char *cmd = args_string(args, 0);
int wait = !args_has(args, 'b');
if ((delay = args_get(args, 'd')) != NULL) {
@@ -117,16 +115,14 @@ cmd_run_shell_exec(struct cmd *self, struct cmdq_item *item)
return (CMD_RETURN_NORMAL);
cdata = xcalloc(1, sizeof *cdata);
if (cmd != NULL)
cdata->cmd = format_single_from_target(item, cmd);
cdata->shell = !args_has(args, 'C');
if (!cdata->shell) {
cmd_get_source(self, &cdata->pi.file, &cdata->pi.line);
if (wait)
cdata->pi.item = item;
cdata->pi.c = tc;
cmd_find_copy_state(&cdata->pi.fs, target);
if (!args_has(args, 'C')) {
cmd = args_string(args, 0);
if (cmd != NULL)
cdata->cmd = format_single_from_target(item, cmd);
} else {
cdata->cmdlist = args_make_commands_now(self, item, 0);
if (cdata->cmdlist == NULL)
return (CMD_RETURN_ERROR);
}
if (args_has(args, 't') && wp != NULL)
@@ -170,11 +166,9 @@ cmd_run_shell_timer(__unused int fd, __unused short events, void* arg)
struct cmd_run_shell_data *cdata = arg;
struct client *c = cdata->client;
const char *cmd = cdata->cmd;
char *error;
struct cmdq_item *item = cdata->item;
enum cmd_parse_status status;
struct cmdq_item *item = cdata->item, *new_item;
if (cmd != NULL && cdata->shell) {
if (cdata->cmdlist == NULL && cmd != NULL) {
if (job_run(cmd, 0, NULL, cdata->s, cdata->cwd, NULL,
cmd_run_shell_callback, cmd_run_shell_free, cdata,
cdata->flags, -1, -1) == NULL)
@@ -182,21 +176,14 @@ cmd_run_shell_timer(__unused int fd, __unused short events, void* arg)
return;
}
if (cmd != NULL) {
if (item != NULL) {
status = cmd_parse_and_insert(cmd, &cdata->pi, item,
cmdq_get_state(item), &error);
if (cdata->cmdlist != NULL) {
if (item == NULL) {
new_item = cmdq_get_command(cdata->cmdlist, NULL);
cmdq_append(c, new_item);
} else {
status = cmd_parse_and_append(cmd, &cdata->pi, c, NULL,
&error);
}
if (status == CMD_PARSE_ERROR) {
if (cdata->item == NULL) {
*error = toupper((u_char)*error);
status_message_set(c, -1, 1, 0, "%s", error);
} else
cmdq_error(cdata->item, "%s", error);
free(error);
new_item = cmdq_get_command(cdata->cmdlist,
cmdq_get_state(item));
cmdq_insert_after(item, new_item);
}
}
@@ -265,6 +252,8 @@ cmd_run_shell_free(void *data)
session_remove_ref(cdata->s, __func__);
if (cdata->client != NULL)
server_client_unref(cdata->client);
if (cdata->cmdlist != NULL)
cmd_list_free(cdata->cmdlist);
free(cdata->cwd);
free(cdata->cmd);
free(cdata);