diff options
author | Nicholas Marriott <nicm@cvs.openbsd.org> | 2009-09-20 19:15:03 +0000 |
---|---|---|
committer | Nicholas Marriott <nicm@cvs.openbsd.org> | 2009-09-20 19:15:03 +0000 |
commit | e601ed12b43bb32766ac97ad62494a09c1a2f27c (patch) | |
tree | 9b1a6a50e23cb85b847ac7852bffc5f3a48366cf /usr.bin | |
parent | 57cdee59adeb162697500b613793171a7c34041a (diff) |
run-shell command to run a shell command without opening a window, sending
stdout to output mode.
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/tmux/Makefile | 4 | ||||
-rw-r--r-- | usr.bin/tmux/cmd-run-shell.c | 96 | ||||
-rw-r--r-- | usr.bin/tmux/cmd.c | 3 | ||||
-rw-r--r-- | usr.bin/tmux/tmux.1 | 13 | ||||
-rw-r--r-- | usr.bin/tmux/tmux.h | 3 |
5 files changed, 113 insertions, 6 deletions
diff --git a/usr.bin/tmux/Makefile b/usr.bin/tmux/Makefile index 5fd92bad2be..933c263b4a1 100644 --- a/usr.bin/tmux/Makefile +++ b/usr.bin/tmux/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.15 2009/08/31 20:46:19 nicm Exp $ +# $OpenBSD: Makefile,v 1.16 2009/09/20 19:15:01 nicm Exp $ PROG= tmux SRCS= attributes.c buffer-poll.c buffer.c cfg.c client-fn.c \ @@ -23,7 +23,7 @@ SRCS= attributes.c buffer-poll.c buffer.c cfg.c client-fn.c \ cmd-set-password.c cmd-set-window-option.c cmd-show-buffer.c \ cmd-show-options.c cmd-show-window-options.c cmd-source-file.c \ cmd-split-window.c cmd-start-server.c cmd-string.c cmd-if-shell.c \ - cmd-suspend-client.c cmd-swap-pane.c cmd-swap-window.c \ + cmd-run-shell.c cmd-suspend-client.c cmd-swap-pane.c cmd-swap-window.c \ cmd-switch-client.c cmd-unbind-key.c cmd-unlink-window.c \ cmd-set-environment.c cmd-show-environment.c cmd-choose-client.c \ cmd-up-pane.c cmd-display-message.c cmd-display-panes.c cmd.c \ diff --git a/usr.bin/tmux/cmd-run-shell.c b/usr.bin/tmux/cmd-run-shell.c new file mode 100644 index 00000000000..4dde3d8f75d --- /dev/null +++ b/usr.bin/tmux/cmd-run-shell.c @@ -0,0 +1,96 @@ +/* $OpenBSD: cmd-run-shell.c,v 1.1 2009/09/20 19:15:01 nicm Exp $ */ + +/* + * 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); +} diff --git a/usr.bin/tmux/cmd.c b/usr.bin/tmux/cmd.c index e897a56e16e..f4025d037d4 100644 --- a/usr.bin/tmux/cmd.c +++ b/usr.bin/tmux/cmd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd.c,v 1.16 2009/08/31 20:46:19 nicm Exp $ */ +/* $OpenBSD: cmd.c,v 1.17 2009/09/20 19:15:01 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -76,6 +76,7 @@ const struct cmd_entry *cmd_table[] = { &cmd_resize_pane_entry, &cmd_respawn_window_entry, &cmd_rotate_window_entry, + &cmd_run_shell_entry, &cmd_save_buffer_entry, &cmd_scroll_mode_entry, &cmd_select_layout_entry, diff --git a/usr.bin/tmux/tmux.1 b/usr.bin/tmux/tmux.1 index fc219a3765b..9bf12defaff 100644 --- a/usr.bin/tmux/tmux.1 +++ b/usr.bin/tmux/tmux.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: tmux.1,v 1.86 2009/09/18 15:19:27 nicm Exp $ +.\" $OpenBSD: tmux.1,v 1.87 2009/09/20 19:15:02 nicm Exp $ .\" .\" Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> .\" @@ -14,7 +14,7 @@ .\" IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING .\" OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: September 18 2009 $ +.Dd $Mdocdate: September 20 2009 $ .Dt TMUX 1 .Os .Sh NAME @@ -1959,6 +1959,15 @@ returns success. .It Ic lock-server .D1 (alias: Ic lock ) Lock the server until a password is entered. +.It Ic run-shell Ar command +.D1 (alias: Ic run ) +Execute +.Ar command +without creating a window. +Any output to stdout is displayed in output mode. +If +.Ar command +doesn't return success, the exit status is also displayed. .It Ic server-info .D1 (alias: Ic info ) Show server information and terminal details. diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h index 324dcc17a2c..2377d6e3200 100644 --- a/usr.bin/tmux/tmux.h +++ b/usr.bin/tmux/tmux.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tmux.h,v 1.106 2009/09/20 17:27:18 nicm Exp $ */ +/* $OpenBSD: tmux.h,v 1.107 2009/09/20 19:15:02 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -1338,6 +1338,7 @@ extern const struct cmd_entry cmd_rename_window_entry; extern const struct cmd_entry cmd_resize_pane_entry; extern const struct cmd_entry cmd_respawn_window_entry; extern const struct cmd_entry cmd_rotate_window_entry; +extern const struct cmd_entry cmd_run_shell_entry; extern const struct cmd_entry cmd_save_buffer_entry; extern const struct cmd_entry cmd_scroll_mode_entry; extern const struct cmd_entry cmd_select_layout_entry; |