mirror of
https://github.com/tmux/tmux.git
synced 2025-01-12 11:18:48 +00:00
Add an else clause for if-shell, from "arno-" on SourceForge.
This commit is contained in:
parent
49ef0b4af3
commit
6821ccc882
@ -28,6 +28,7 @@
|
|||||||
* Executes a tmux command if a shell command returns true.
|
* Executes a tmux command if a shell command returns true.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
int cmd_if_shell_check(struct args *);
|
||||||
int cmd_if_shell_exec(struct cmd *, struct cmd_ctx *);
|
int cmd_if_shell_exec(struct cmd *, struct cmd_ctx *);
|
||||||
|
|
||||||
void cmd_if_shell_callback(struct job *);
|
void cmd_if_shell_callback(struct job *);
|
||||||
@ -35,19 +36,30 @@ void cmd_if_shell_free(void *);
|
|||||||
|
|
||||||
const struct cmd_entry cmd_if_shell_entry = {
|
const struct cmd_entry cmd_if_shell_entry = {
|
||||||
"if-shell", "if",
|
"if-shell", "if",
|
||||||
"", 2, 2,
|
"", 2, 4,
|
||||||
"shell-command command",
|
"shell-command command [else command]",
|
||||||
0,
|
0,
|
||||||
NULL,
|
NULL,
|
||||||
NULL,
|
cmd_if_shell_check,
|
||||||
cmd_if_shell_exec
|
cmd_if_shell_exec
|
||||||
};
|
};
|
||||||
|
|
||||||
struct cmd_if_shell_data {
|
struct cmd_if_shell_data {
|
||||||
char *cmd;
|
char *cmd_if;
|
||||||
|
char *cmd_else;
|
||||||
struct cmd_ctx ctx;
|
struct cmd_ctx ctx;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
int
|
||||||
|
cmd_if_shell_check(struct args *args)
|
||||||
|
{
|
||||||
|
if (args->argc == 3)
|
||||||
|
return (-1);
|
||||||
|
if (args->argc == 4 && strcmp(args->argv[2], "else") != 0)
|
||||||
|
return (-1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
cmd_if_shell_exec(struct cmd *self, struct cmd_ctx *ctx)
|
cmd_if_shell_exec(struct cmd *self, struct cmd_ctx *ctx)
|
||||||
{
|
{
|
||||||
@ -56,7 +68,11 @@ cmd_if_shell_exec(struct cmd *self, struct cmd_ctx *ctx)
|
|||||||
const char *shellcmd = args->argv[0];
|
const char *shellcmd = args->argv[0];
|
||||||
|
|
||||||
cdata = xmalloc(sizeof *cdata);
|
cdata = xmalloc(sizeof *cdata);
|
||||||
cdata->cmd = xstrdup(args->argv[1]);
|
cdata->cmd_if = xstrdup(args->argv[1]);
|
||||||
|
if (args->argc == 4)
|
||||||
|
cdata->cmd_else = xstrdup(args->argv[3]);
|
||||||
|
else
|
||||||
|
cdata->cmd_else = NULL;
|
||||||
memcpy(&cdata->ctx, ctx, sizeof cdata->ctx);
|
memcpy(&cdata->ctx, ctx, sizeof cdata->ctx);
|
||||||
|
|
||||||
if (ctx->cmdclient != NULL)
|
if (ctx->cmdclient != NULL)
|
||||||
@ -75,12 +91,16 @@ cmd_if_shell_callback(struct job *job)
|
|||||||
struct cmd_if_shell_data *cdata = job->data;
|
struct cmd_if_shell_data *cdata = job->data;
|
||||||
struct cmd_ctx *ctx = &cdata->ctx;
|
struct cmd_ctx *ctx = &cdata->ctx;
|
||||||
struct cmd_list *cmdlist;
|
struct cmd_list *cmdlist;
|
||||||
|
char *cmd;
|
||||||
char *cause;
|
char *cause;
|
||||||
|
|
||||||
if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0)
|
if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0) {
|
||||||
|
cmd = cdata->cmd_else;
|
||||||
|
if (cmd == NULL)
|
||||||
return;
|
return;
|
||||||
|
} else
|
||||||
if (cmd_string_parse(cdata->cmd, &cmdlist, &cause) != 0) {
|
cmd = cdata->cmd_if;
|
||||||
|
if (cmd_string_parse(cmd, &cmdlist, &cause) != 0) {
|
||||||
if (cause != NULL) {
|
if (cause != NULL) {
|
||||||
ctx->error(ctx, "%s", cause);
|
ctx->error(ctx, "%s", cause);
|
||||||
xfree(cause);
|
xfree(cause);
|
||||||
@ -107,6 +127,8 @@ cmd_if_shell_free(void *data)
|
|||||||
if (ctx->curclient != NULL)
|
if (ctx->curclient != NULL)
|
||||||
ctx->curclient->references--;
|
ctx->curclient->references--;
|
||||||
|
|
||||||
xfree(cdata->cmd);
|
if (cdata->cmd_else != NULL)
|
||||||
|
xfree(cdata->cmd_else);
|
||||||
|
xfree(cdata->cmd_if);
|
||||||
xfree(cdata);
|
xfree(cdata);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user