version 1.17, 2013/03/24 09:33:35 |
version 1.18, 2013/03/24 09:54:10 |
|
|
* Runs a command without a window. |
* Runs a command without a window. |
*/ |
*/ |
|
|
enum cmd_retval cmd_run_shell_exec(struct cmd *, struct cmd_ctx *); |
enum cmd_retval cmd_run_shell_exec(struct cmd *, struct cmd_q *); |
|
|
void cmd_run_shell_callback(struct job *); |
void cmd_run_shell_callback(struct job *); |
void cmd_run_shell_free(void *); |
void cmd_run_shell_free(void *); |
|
|
|
|
const struct cmd_entry cmd_run_shell_entry = { |
const struct cmd_entry cmd_run_shell_entry = { |
"run-shell", "run", |
"run-shell", "run", |
"t:", 1, 1, |
"bt:", 1, 1, |
CMD_TARGET_PANE_USAGE " command", |
"[-b] " CMD_TARGET_PANE_USAGE " shell-command", |
0, |
0, |
NULL, |
NULL, |
NULL, |
NULL, |
|
|
|
|
struct cmd_run_shell_data { |
struct cmd_run_shell_data { |
char *cmd; |
char *cmd; |
struct cmd_ctx *ctx; |
struct cmd_q *cmdq; |
|
int bflag; |
u_int wp_id; |
u_int wp_id; |
}; |
}; |
|
|
|
|
cmd_run_shell_print(struct job *job, const char *msg) |
cmd_run_shell_print(struct job *job, const char *msg) |
{ |
{ |
struct cmd_run_shell_data *cdata = job->data; |
struct cmd_run_shell_data *cdata = job->data; |
struct cmd_ctx *ctx = cdata->ctx; |
|
struct window_pane *wp; |
struct window_pane *wp; |
|
|
wp = window_pane_find_by_id(cdata->wp_id); |
wp = window_pane_find_by_id(cdata->wp_id); |
if (wp == NULL) { |
if (wp == NULL) { |
ctx->print(ctx, "%s", msg); |
cmdq_print(cdata->cmdq, "%s", msg); |
return; |
return; |
} |
} |
|
|
|
|
} |
} |
|
|
enum cmd_retval |
enum cmd_retval |
cmd_run_shell_exec(struct cmd *self, struct cmd_ctx *ctx) |
cmd_run_shell_exec(struct cmd *self, struct cmd_q *cmdq) |
{ |
{ |
struct args *args = self->args; |
struct args *args = self->args; |
struct cmd_run_shell_data *cdata; |
struct cmd_run_shell_data *cdata; |
|
|
struct window_pane *wp; |
struct window_pane *wp; |
struct format_tree *ft; |
struct format_tree *ft; |
|
|
wl = cmd_find_pane(ctx, args_get(args, 't'), &s, &wp); |
wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp); |
if (wl == NULL) |
if (wl == NULL) |
return (CMD_RETURN_ERROR); |
return (CMD_RETURN_ERROR); |
|
|
|
|
|
|
cdata = xmalloc(sizeof *cdata); |
cdata = xmalloc(sizeof *cdata); |
cdata->cmd = shellcmd; |
cdata->cmd = shellcmd; |
|
cdata->bflag = args_has(args, 'b'); |
cdata->wp_id = wp->id; |
cdata->wp_id = wp->id; |
|
|
cdata->ctx = ctx; |
cdata->cmdq = cmdq; |
cmd_ref_ctx(ctx); |
cmdq->references++; |
|
|
job_run(shellcmd, cmd_run_shell_callback, cmd_run_shell_free, cdata); |
job_run(shellcmd, cmd_run_shell_callback, cmd_run_shell_free, cdata); |
|
|
return (CMD_RETURN_YIELD); /* don't let client exit */ |
if (cdata->bflag) |
|
return (CMD_RETURN_NORMAL); |
|
return (CMD_RETURN_WAIT); |
} |
} |
|
|
void |
void |
cmd_run_shell_callback(struct job *job) |
cmd_run_shell_callback(struct job *job) |
{ |
{ |
struct cmd_run_shell_data *cdata = job->data; |
struct cmd_run_shell_data *cdata = job->data; |
struct cmd_ctx *ctx = cdata->ctx; |
struct cmd_q *cmdq = cdata->cmdq; |
char *cmd, *msg, *line; |
char *cmd, *msg, *line; |
size_t size; |
size_t size; |
int retcode; |
int retcode; |
u_int lines; |
u_int lines; |
|
|
if (ctx->cmdclient != NULL && ctx->cmdclient->flags & CLIENT_DEAD) |
if (cmdq->dead) |
return; |
return; |
if (ctx->curclient != NULL && ctx->curclient->flags & CLIENT_DEAD) |
cmd = cdata->cmd; |
return; |
|
|
|
lines = 0; |
lines = 0; |
do { |
do { |
if ((line = evbuffer_readline(job->event->input)) != NULL) { |
if ((line = evbuffer_readline(job->event->input)) != NULL) { |
cmd_run_shell_print (job, line); |
cmd_run_shell_print(job, line); |
free(line); |
free(line); |
lines++; |
lines++; |
} |
} |
|
|
free(line); |
free(line); |
} |
} |
|
|
cmd = cdata->cmd; |
|
|
|
msg = NULL; |
msg = NULL; |
if (WIFEXITED(job->status)) { |
if (WIFEXITED(job->status)) { |
if ((retcode = WEXITSTATUS(job->status)) != 0) |
if ((retcode = WEXITSTATUS(job->status)) != 0) |
|
|
} |
} |
if (msg != NULL) { |
if (msg != NULL) { |
if (lines == 0) |
if (lines == 0) |
ctx->info(ctx, "%s", msg); |
cmdq_info(cmdq, "%s", msg); |
else |
else |
cmd_run_shell_print(job, msg); |
cmd_run_shell_print(job, msg); |
free(msg); |
free(msg); |
|
|
cmd_run_shell_free(void *data) |
cmd_run_shell_free(void *data) |
{ |
{ |
struct cmd_run_shell_data *cdata = data; |
struct cmd_run_shell_data *cdata = data; |
struct cmd_ctx *ctx = cdata->ctx; |
struct cmd_q *cmdq = cdata->cmdq; |
|
|
if (ctx->cmdclient != NULL) |
if (!cmdq_free(cmdq) && !cdata->bflag) |
ctx->cmdclient->flags |= CLIENT_EXIT; |
cmdq_continue(cmdq); |
cmd_free_ctx(ctx); |
|
|
|
free(cdata->cmd); |
free(cdata->cmd); |
free(cdata); |
free(cdata); |