From e3dcc5327a8a126c13568268ba28813aa567beec Mon Sep 17 00:00:00 2001 From: Tiago Cunha Date: Sun, 20 Sep 2009 22:20:10 +0000 Subject: [PATCH] Sync OpenBSD patchset 335: run-shell command to run a shell command without opening a window, sending stdout to output mode. --- cmd-run-shell.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++ cmd.c | 3 +- tmux.1 | 13 +++++-- tmux.h | 3 +- 4 files changed, 111 insertions(+), 4 deletions(-) create mode 100644 cmd-run-shell.c diff --git a/cmd-run-shell.c b/cmd-run-shell.c new file mode 100644 index 00000000..3936fff8 --- /dev/null +++ b/cmd-run-shell.c @@ -0,0 +1,96 @@ +/* $Id: cmd-run-shell.c,v 1.1 2009-09-20 22:20:10 tcunha Exp $ */ + +/* + * Copyright (c) 2009 Tiago Cunha + * + * 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 +#include + +#include + +#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/cmd.c b/cmd.c index 8ffe4af8..f30919fe 100644 --- a/cmd.c +++ b/cmd.c @@ -1,4 +1,4 @@ -/* $Id: cmd.c,v 1.115 2009-08-31 22:30:15 tcunha Exp $ */ +/* $Id: cmd.c,v 1.116 2009-09-20 22:20:10 tcunha Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -75,6 +75,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/tmux.1 b/tmux.1 index 4997f226..a5564986 100644 --- a/tmux.1 +++ b/tmux.1 @@ -1,4 +1,4 @@ -.\" $Id: tmux.1,v 1.168 2009-09-19 18:53:01 tcunha Exp $ +.\" $Id: tmux.1,v 1.169 2009-09-20 22:20:10 tcunha Exp $ .\" .\" Copyright (c) 2007 Nicholas Marriott .\" @@ -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/tmux.h b/tmux.h index d7b324fe..da782eec 100644 --- a/tmux.h +++ b/tmux.h @@ -1,4 +1,4 @@ -/* $Id: tmux.h,v 1.444 2009-09-20 22:15:32 tcunha Exp $ */ +/* $Id: tmux.h,v 1.445 2009-09-20 22:20:10 tcunha Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -1336,6 +1336,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;