summaryrefslogtreecommitdiffstats
path: root/cmd-run-shell.c
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@openbsd.org>2009-09-20 19:15:01 +0000
committerNicholas Marriott <nicm@openbsd.org>2009-09-20 19:15:01 +0000
commit14ebcab5b016828c64c6d4564e50159f5dedf130 (patch)
tree759800678a620b6233c6d785cc4e04c617f963f4 /cmd-run-shell.c
parent105ce36792717a0916cab18b7dd0a11db1f7c84a (diff)
run-shell command to run a shell command without opening a window, sending
stdout to output mode.
Diffstat (limited to 'cmd-run-shell.c')
-rw-r--r--cmd-run-shell.c96
1 files changed, 96 insertions, 0 deletions
diff --git a/cmd-run-shell.c b/cmd-run-shell.c
new file mode 100644
index 00000000..71e5b96b
--- /dev/null
+++ b/cmd-run-shell.c
@@ -0,0 +1,96 @@
+/* $OpenBSD$ */
+
+/*
+ * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
+ * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
+ * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <string.h>
+
+#include "tmux.h"
+
+/*
+ * Runs a command without a window.
+ */
+
+int cmd_run_shell_exec(struct cmd *, struct cmd_ctx *);
+
+const struct cmd_entry cmd_run_shell_entry = {
+ "run-shell", "run",
+ "command",
+ CMD_ARG1, 0,
+ cmd_target_init,
+ cmd_target_parse,
+ cmd_run_shell_exec,
+ cmd_target_free,
+ cmd_target_print
+};
+
+int
+cmd_run_shell_exec(struct cmd *self, struct cmd_ctx *ctx)
+{
+ struct cmd_target_data *data = self->data;
+ FILE *fp;
+ char *buf, *lbuf, *msg;
+ size_t len;
+ int has_output, ret, status;
+
+ if ((fp = popen(data->arg, "r")) == NULL) {
+ ctx->error(ctx, "popen error");
+ return (-1);
+ }
+
+ has_output = 0;
+ lbuf = NULL;
+ while ((buf = fgetln(fp, &len)) != NULL) {
+ if (buf[len - 1] == '\n')
+ buf[len - 1] = '\0';
+ else {
+ lbuf = xmalloc(len + 1);
+ memcpy(lbuf, buf, len);
+ lbuf[len] = '\0';
+ buf = lbuf;
+ }
+ ctx->print(ctx, "%s", buf);
+ has_output = 1;
+ }
+ if (lbuf != NULL)
+ xfree(lbuf);
+
+ msg = NULL;
+ status = pclose(fp);
+
+ if (WIFEXITED(status)) {
+ if ((ret = WEXITSTATUS(status)) == 0)
+ return (0);
+ xasprintf(&msg, "'%s' returned %d", data->arg, ret);
+ } else if (WIFSIGNALED(status)) {
+ xasprintf(
+ &msg, "'%s' terminated by signal %d", data->arg,
+ WTERMSIG(status));
+ }
+
+ if (msg != NULL) {
+ if (has_output)
+ ctx->print(ctx, "%s", msg);
+ else
+ ctx->info(ctx, "%s", msg);
+ xfree(msg);
+ }
+
+ return (0);
+}