Revision: 2615
http://tmux.svn.sourceforge.net/tmux/?rev=2615&view=rev
Author: tcunha
Date: 2011-10-23 15:00:41 +0000 (Sun, 23 Oct 2011)
Log Message:
-----------
Sync OpenBSD patchset 963:
Add an else clause for if-shell, from "arno-" on SourceForge.
Modified Paths:
--------------
trunk/cmd-if-shell.c
Modified: trunk/cmd-if-shell.c
===================================================================
--- trunk/cmd-if-shell.c 2011-10-23 10:21:02 UTC (rev 2614)
+++ trunk/cmd-if-shell.c 2011-10-23 15:00:41 UTC (rev 2615)
@@ -28,6 +28,7 @@
* 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 *);
void cmd_if_shell_callback(struct job *);
@@ -35,20 +36,31 @@
const struct cmd_entry cmd_if_shell_entry = {
"if-shell", "if",
- "", 2, 2,
- "shell-command command",
+ "", 2, 4,
+ "shell-command command [else command]",
0,
NULL,
- NULL,
+ cmd_if_shell_check,
cmd_if_shell_exec
};
struct cmd_if_shell_data {
- char *cmd;
+ char *cmd_if;
+ char *cmd_else;
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
cmd_if_shell_exec(struct cmd *self, struct cmd_ctx *ctx)
{
struct args *args = self->args;
@@ -56,7 +68,11 @@
const char *shellcmd = args->argv[0];
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);
if (ctx->cmdclient != NULL)
@@ -75,12 +91,16 @@
struct cmd_if_shell_data *cdata = job->data;
struct cmd_ctx *ctx = &cdata->ctx;
struct cmd_list *cmdlist;
+ char *cmd;
char *cause;
- if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0)
- return;
-
- if (cmd_string_parse(cdata->cmd, &cmdlist, &cause) != 0) {
+ if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0) {
+ cmd = cdata->cmd_else;
+ if (cmd == NULL)
+ return;
+ } else
+ cmd = cdata->cmd_if;
+ if (cmd_string_parse(cmd, &cmdlist, &cause) != 0) {
if (cause != NULL) {
ctx->error(ctx, "%s", cause);
xfree(cause);
@@ -107,6 +127,8 @@
if (ctx->curclient != NULL)
ctx->curclient->references--;
- xfree(cdata->cmd);
+ if (cdata->cmd_else != NULL)
+ xfree(cdata->cmd_else);
+ xfree(cdata->cmd_if);
xfree(cdata);
}
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
------------------------------------------------------------------------------
The demand for IT networking professionals continues to grow, and the
demand for specialized networking skills is growing even more rapidly.
Take a complimentary Learning@Cisco Self-Assessment and learn
about Cisco certifications, training, and career opportunities.
http://p.sf.net/sfu/cisco-dev2dev
_______________________________________________
tmux-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tmux-cvs