version 1.47, 2016/10/16 17:55:14 |
version 1.48, 2016/10/16 19:04:05 |
|
|
* Executes a tmux command if a shell command returns true or false. |
* Executes a tmux command if a shell command returns true or false. |
*/ |
*/ |
|
|
static enum cmd_retval cmd_if_shell_exec(struct cmd *, struct cmd_q *); |
static enum cmd_retval cmd_if_shell_exec(struct cmd *, struct cmdq_item *); |
|
|
static enum cmd_retval cmd_if_shell_error(struct cmd_q *, void *); |
static enum cmd_retval cmd_if_shell_error(struct cmdq_item *, void *); |
static void cmd_if_shell_callback(struct job *); |
static void cmd_if_shell_callback(struct job *); |
static void cmd_if_shell_free(void *); |
static void cmd_if_shell_free(void *); |
|
|
const struct cmd_entry cmd_if_shell_entry = { |
const struct cmd_entry cmd_if_shell_entry = { |
.name = "if-shell", |
.name = "if-shell", |
|
|
char *cmd_else; |
char *cmd_else; |
|
|
struct client *client; |
struct client *client; |
struct cmd_q *cmdq; |
struct cmdq_item *item; |
struct mouse_event mouse; |
struct mouse_event mouse; |
}; |
}; |
|
|
static enum cmd_retval |
static enum cmd_retval |
cmd_if_shell_exec(struct cmd *self, struct cmd_q *cmdq) |
cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item) |
{ |
{ |
struct args *args = self->args; |
struct args *args = self->args; |
struct cmd_if_shell_data *cdata; |
struct cmd_if_shell_data *cdata; |
char *shellcmd, *cmd, *cause; |
char *shellcmd, *cmd, *cause; |
struct cmd_list *cmdlist; |
struct cmd_list *cmdlist; |
struct cmd_q *new_cmdq; |
struct cmdq_item *new_item; |
struct session *s = cmdq->state.tflag.s; |
struct session *s = item->state.tflag.s; |
struct winlink *wl = cmdq->state.tflag.wl; |
struct winlink *wl = item->state.tflag.wl; |
struct window_pane *wp = cmdq->state.tflag.wp; |
struct window_pane *wp = item->state.tflag.wp; |
struct format_tree *ft; |
struct format_tree *ft; |
const char *cwd; |
const char *cwd; |
|
|
if (cmdq->client != NULL && cmdq->client->session == NULL) |
if (item->client != NULL && item->client->session == NULL) |
cwd = cmdq->client->cwd; |
cwd = item->client->cwd; |
else if (s != NULL) |
else if (s != NULL) |
cwd = s->cwd; |
cwd = s->cwd; |
else |
else |
cwd = NULL; |
cwd = NULL; |
|
|
ft = format_create(cmdq, 0); |
ft = format_create(item, 0); |
format_defaults(ft, cmdq->state.c, s, wl, wp); |
format_defaults(ft, item->state.c, s, wl, wp); |
shellcmd = format_expand(ft, args->argv[0]); |
shellcmd = format_expand(ft, args->argv[0]); |
format_free(ft); |
format_free(ft); |
|
|
|
|
return (CMD_RETURN_NORMAL); |
return (CMD_RETURN_NORMAL); |
if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) { |
if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) { |
if (cause != NULL) { |
if (cause != NULL) { |
cmdq_error(cmdq, "%s", cause); |
cmdq_error(item, "%s", cause); |
free(cause); |
free(cause); |
} |
} |
return (CMD_RETURN_ERROR); |
return (CMD_RETURN_ERROR); |
} |
} |
new_cmdq = cmdq_get_command(cmdlist, NULL, &cmdq->mouse, 0); |
new_item = cmdq_get_command(cmdlist, NULL, &item->mouse, 0); |
cmdq_insert_after(cmdq, new_cmdq); |
cmdq_insert_after(item, new_item); |
cmd_list_free(cmdlist); |
cmd_list_free(cmdlist); |
return (CMD_RETURN_NORMAL); |
return (CMD_RETURN_NORMAL); |
} |
} |
|
|
else |
else |
cdata->cmd_else = NULL; |
cdata->cmd_else = NULL; |
|
|
cdata->client = cmdq->client; |
cdata->client = item->client; |
cdata->client->references++; |
cdata->client->references++; |
|
|
if (!args_has(args, 'b')) |
if (!args_has(args, 'b')) |
cdata->cmdq = cmdq; |
cdata->item = item; |
else |
else |
cdata->cmdq = NULL; |
cdata->item = NULL; |
memcpy(&cdata->mouse, &cmdq->mouse, sizeof cdata->mouse); |
memcpy(&cdata->mouse, &item->mouse, sizeof cdata->mouse); |
|
|
job_run(shellcmd, s, cwd, cmd_if_shell_callback, cmd_if_shell_free, |
job_run(shellcmd, s, cwd, cmd_if_shell_callback, cmd_if_shell_free, |
cdata); |
cdata); |
|
|
} |
} |
|
|
static enum cmd_retval |
static enum cmd_retval |
cmd_if_shell_error(struct cmd_q *cmdq, void *data) |
cmd_if_shell_error(struct cmdq_item *item, void *data) |
{ |
{ |
char *error = data; |
char *error = data; |
|
|
cmdq_error(cmdq, "%s", error); |
cmdq_error(item, "%s", error); |
free(error); |
free(error); |
|
|
return (CMD_RETURN_NORMAL); |
return (CMD_RETURN_NORMAL); |
|
|
struct cmd_if_shell_data *cdata = job->data; |
struct cmd_if_shell_data *cdata = job->data; |
struct client *c = cdata->client; |
struct client *c = cdata->client; |
struct cmd_list *cmdlist; |
struct cmd_list *cmdlist; |
struct cmd_q *new_cmdq; |
struct cmdq_item *new_item; |
char *cause, *cmd, *file = cdata->file; |
char *cause, *cmd, *file = cdata->file; |
u_int line = cdata->line; |
u_int line = cdata->line; |
|
|
|
|
|
|
if (cmd_string_parse(cmd, &cmdlist, file, line, &cause) != 0) { |
if (cmd_string_parse(cmd, &cmdlist, file, line, &cause) != 0) { |
if (cause != NULL) |
if (cause != NULL) |
new_cmdq = cmdq_get_callback(cmd_if_shell_error, cause); |
new_item = cmdq_get_callback(cmd_if_shell_error, cause); |
else |
else |
new_cmdq = NULL; |
new_item = NULL; |
} else { |
} else { |
new_cmdq = cmdq_get_command(cmdlist, NULL, &cdata->mouse, 0); |
new_item = cmdq_get_command(cmdlist, NULL, &cdata->mouse, 0); |
cmd_list_free(cmdlist); |
cmd_list_free(cmdlist); |
} |
} |
|
|
if (new_cmdq != NULL) { |
if (new_item != NULL) { |
if (cdata->cmdq == NULL) |
if (cdata->item == NULL) |
cmdq_append(c, new_cmdq); |
cmdq_append(c, new_item); |
else |
else |
cmdq_insert_after(cdata->cmdq, new_cmdq); |
cmdq_insert_after(cdata->item, new_item); |
} |
} |
|
|
out: |
out: |
if (cdata->cmdq != NULL) |
if (cdata->item != NULL) |
cdata->cmdq->flags &= ~CMD_Q_WAITING; |
cdata->item->flags &= ~CMDQ_WAITING; |
} |
} |
|
|
static void |
static void |