From d1e6ce26722e3a0774d240e39bbc69b07e93c48b Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Fri, 22 Mar 2013 15:49:55 +0000 Subject: Add functions to allocate and free command contexts rather than doing it all on the stack. --- cmd-run-shell.c | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) (limited to 'cmd-run-shell.c') diff --git a/cmd-run-shell.c b/cmd-run-shell.c index 0fe7620b..ea7d1dcb 100644 --- a/cmd-run-shell.c +++ b/cmd-run-shell.c @@ -47,7 +47,7 @@ const struct cmd_entry cmd_run_shell_entry = { struct cmd_run_shell_data { char *cmd; - struct cmd_ctx ctx; + struct cmd_ctx *ctx; u_int wp_id; }; @@ -55,7 +55,7 @@ void cmd_run_shell_print(struct job *job, const char *msg) { struct cmd_run_shell_data *cdata = job->data; - struct cmd_ctx *ctx = &cdata->ctx; + struct cmd_ctx *ctx = cdata->ctx; struct window_pane *wp; wp = window_pane_find_by_id(cdata->wp_id); @@ -84,12 +84,9 @@ cmd_run_shell_exec(struct cmd *self, struct cmd_ctx *ctx) cdata = xmalloc(sizeof *cdata); cdata->cmd = xstrdup(args->argv[0]); cdata->wp_id = wp->id; - memcpy(&cdata->ctx, ctx, sizeof cdata->ctx); - if (ctx->cmdclient != NULL) - ctx->cmdclient->references++; - if (ctx->curclient != NULL) - ctx->curclient->references++; + cdata->ctx = ctx; + cmd_ref_ctx(ctx); job_run(shellcmd, cmd_run_shell_callback, cmd_run_shell_free, cdata); @@ -100,7 +97,7 @@ void cmd_run_shell_callback(struct job *job) { struct cmd_run_shell_data *cdata = job->data; - struct cmd_ctx *ctx = &cdata->ctx; + struct cmd_ctx *ctx = cdata->ctx; char *cmd, *msg, *line; size_t size; int retcode; @@ -154,14 +151,11 @@ void cmd_run_shell_free(void *data) { struct cmd_run_shell_data *cdata = data; - struct cmd_ctx *ctx = &cdata->ctx; + struct cmd_ctx *ctx = cdata->ctx; - if (ctx->cmdclient != NULL) { - ctx->cmdclient->references--; + if (ctx->cmdclient != NULL) ctx->cmdclient->flags |= CLIENT_EXIT; - } - if (ctx->curclient != NULL) - ctx->curclient->references--; + cmd_free_ctx(ctx); free(cdata->cmd); free(cdata); -- cgit From c519f9a84cd9fc3fdec8b61afaf42995f9e48b46 Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Fri, 22 Mar 2013 15:55:22 +0000 Subject: evbuffer_readline returns allocated storage, don't leak it. --- cmd-run-shell.c | 1 + 1 file changed, 1 insertion(+) (limited to 'cmd-run-shell.c') diff --git a/cmd-run-shell.c b/cmd-run-shell.c index ea7d1dcb..0d19a8e6 100644 --- a/cmd-run-shell.c +++ b/cmd-run-shell.c @@ -112,6 +112,7 @@ cmd_run_shell_callback(struct job *job) do { if ((line = evbuffer_readline(job->event->input)) != NULL) { cmd_run_shell_print (job, line); + free(line); lines++; } } while (line != NULL); -- cgit From 66edb3392b234ccd9a940039936edb34258c2102 Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Sun, 24 Mar 2013 09:33:35 +0000 Subject: Expand format variables in the run-shell and if-shell shell commands, from Thiago Padilha. --- cmd-run-shell.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'cmd-run-shell.c') diff --git a/cmd-run-shell.c b/cmd-run-shell.c index 0d19a8e6..3942fa11 100644 --- a/cmd-run-shell.c +++ b/cmd-run-shell.c @@ -75,14 +75,25 @@ cmd_run_shell_exec(struct cmd *self, struct cmd_ctx *ctx) { struct args *args = self->args; struct cmd_run_shell_data *cdata; - const char *shellcmd = args->argv[0]; + char *shellcmd; + struct session *s; + struct winlink *wl; struct window_pane *wp; + struct format_tree *ft; - if (cmd_find_pane(ctx, args_get(args, 't'), NULL, &wp) == NULL) + wl = cmd_find_pane(ctx, args_get(args, 't'), &s, &wp); + if (wl == NULL) return (CMD_RETURN_ERROR); + ft = format_create(); + format_session(ft, s); + format_winlink(ft, s, wl); + format_window_pane(ft, wp); + shellcmd = format_expand(ft, args->argv[0]); + format_free(ft); + cdata = xmalloc(sizeof *cdata); - cdata->cmd = xstrdup(args->argv[0]); + cdata->cmd = shellcmd; cdata->wp_id = wp->id; cdata->ctx = ctx; -- cgit From 20636d956dd36c1f14152569a4d44a50eea9083d Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Sun, 24 Mar 2013 09:54:10 +0000 Subject: Add a command queue to standardize and simplify commands that call other commands and allow a command to block execution of subsequent commands. This allows run-shell and if-shell to be synchronous which has been much requested. Each client has a default command queue and commands are consumed one at a time from it. A command may suspend execution from the queue by returning CMD_RETURN_WAIT and then resume it by calling cmd_continue() - for example run-shell does this from the callback that is fired after the job is freed. When the command queue becomes empty, command clients are automatically exited (unless attaching). A callback is also fired - this is used for nested commands in, for example, if-shell which can block execution of the client's cmdq until a new cmdq becomes empty. Also merge all the old error/info/print functions together and lose the old curclient/cmdclient distinction - a cmdq is bound to one client (or none if in the configuration file), this is a command client if c->session is NULL otherwise an attached client. --- cmd-run-shell.c | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) (limited to 'cmd-run-shell.c') diff --git a/cmd-run-shell.c b/cmd-run-shell.c index 3942fa11..5452ceff 100644 --- a/cmd-run-shell.c +++ b/cmd-run-shell.c @@ -29,7 +29,7 @@ * 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_free(void *); @@ -37,8 +37,8 @@ void cmd_run_shell_print(struct job *, const char *); const struct cmd_entry cmd_run_shell_entry = { "run-shell", "run", - "t:", 1, 1, - CMD_TARGET_PANE_USAGE " command", + "bt:", 1, 1, + "[-b] " CMD_TARGET_PANE_USAGE " shell-command", 0, NULL, NULL, @@ -47,7 +47,8 @@ const struct cmd_entry cmd_run_shell_entry = { struct cmd_run_shell_data { char *cmd; - struct cmd_ctx *ctx; + struct cmd_q *cmdq; + int bflag; u_int wp_id; }; @@ -55,12 +56,11 @@ void cmd_run_shell_print(struct job *job, const char *msg) { struct cmd_run_shell_data *cdata = job->data; - struct cmd_ctx *ctx = cdata->ctx; struct window_pane *wp; wp = window_pane_find_by_id(cdata->wp_id); if (wp == NULL) { - ctx->print(ctx, "%s", msg); + cmdq_print(cdata->cmdq, "%s", msg); return; } @@ -71,7 +71,7 @@ cmd_run_shell_print(struct job *job, const char *msg) } 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 cmd_run_shell_data *cdata; @@ -81,7 +81,7 @@ cmd_run_shell_exec(struct cmd *self, struct cmd_ctx *ctx) struct window_pane *wp; 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) return (CMD_RETURN_ERROR); @@ -94,35 +94,37 @@ cmd_run_shell_exec(struct cmd *self, struct cmd_ctx *ctx) cdata = xmalloc(sizeof *cdata); cdata->cmd = shellcmd; + cdata->bflag = args_has(args, 'b'); cdata->wp_id = wp->id; - cdata->ctx = ctx; - cmd_ref_ctx(ctx); + cdata->cmdq = cmdq; + cmdq->references++; 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 cmd_run_shell_callback(struct job *job) { struct cmd_run_shell_data *cdata = job->data; - struct cmd_ctx *ctx = cdata->ctx; + struct cmd_q *cmdq = cdata->cmdq; char *cmd, *msg, *line; size_t size; int retcode; u_int lines; - if (ctx->cmdclient != NULL && ctx->cmdclient->flags & CLIENT_DEAD) - return; - if (ctx->curclient != NULL && ctx->curclient->flags & CLIENT_DEAD) + if (cmdq->dead) return; + cmd = cdata->cmd; lines = 0; do { if ((line = evbuffer_readline(job->event->input)) != NULL) { - cmd_run_shell_print (job, line); + cmd_run_shell_print(job, line); free(line); lines++; } @@ -140,8 +142,6 @@ cmd_run_shell_callback(struct job *job) free(line); } - cmd = cdata->cmd; - msg = NULL; if (WIFEXITED(job->status)) { if ((retcode = WEXITSTATUS(job->status)) != 0) @@ -152,7 +152,7 @@ cmd_run_shell_callback(struct job *job) } if (msg != NULL) { if (lines == 0) - ctx->info(ctx, "%s", msg); + cmdq_info(cmdq, "%s", msg); else cmd_run_shell_print(job, msg); free(msg); @@ -163,11 +163,10 @@ void cmd_run_shell_free(void *data) { struct cmd_run_shell_data *cdata = data; - struct cmd_ctx *ctx = cdata->ctx; + struct cmd_q *cmdq = cdata->cmdq; - if (ctx->cmdclient != NULL) - ctx->cmdclient->flags |= CLIENT_EXIT; - cmd_free_ctx(ctx); + if (!cmdq_free(cmdq) && !cdata->bflag) + cmdq_continue(cmdq); free(cdata->cmd); free(cdata); -- cgit From d28a39d01de47786a703e630a4f8930485cba7a1 Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Mon, 25 Mar 2013 11:43:01 +0000 Subject: Extend jobs to support writing and use that for copy-pipe instead of popen, from Chris Johnsen. --- cmd-run-shell.c | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) (limited to 'cmd-run-shell.c') diff --git a/cmd-run-shell.c b/cmd-run-shell.c index 5452ceff..e78d0e40 100644 --- a/cmd-run-shell.c +++ b/cmd-run-shell.c @@ -49,16 +49,17 @@ struct cmd_run_shell_data { char *cmd; struct cmd_q *cmdq; int bflag; - u_int wp_id; + int wp_id; }; void cmd_run_shell_print(struct job *job, const char *msg) { struct cmd_run_shell_data *cdata = job->data; - struct window_pane *wp; + struct window_pane *wp = NULL; - wp = window_pane_find_by_id(cdata->wp_id); + if (cdata->wp_id != -1) + wp = window_pane_find_by_id(cdata->wp_id); if (wp == NULL) { cmdq_print(cdata->cmdq, "%s", msg); return; @@ -76,31 +77,33 @@ cmd_run_shell_exec(struct cmd *self, struct cmd_q *cmdq) struct args *args = self->args; struct cmd_run_shell_data *cdata; char *shellcmd; - struct session *s; - struct winlink *wl; - struct window_pane *wp; + struct session *s = NULL; + struct winlink *wl = NULL; + struct window_pane *wp = NULL; struct format_tree *ft; - wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp); - if (wl == NULL) - return (CMD_RETURN_ERROR); + if (args_has(args, 't')) + wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp); ft = format_create(); - format_session(ft, s); - format_winlink(ft, s, wl); - format_window_pane(ft, wp); + if (s != NULL) + format_session(ft, s); + if (s != NULL && wl != NULL) + format_winlink(ft, s, wl); + if (wp != NULL) + format_window_pane(ft, wp); shellcmd = format_expand(ft, args->argv[0]); format_free(ft); cdata = xmalloc(sizeof *cdata); cdata->cmd = shellcmd; cdata->bflag = args_has(args, 'b'); - cdata->wp_id = wp->id; + cdata->wp_id = wp != NULL ? (int) wp->id : -1; cdata->cmdq = cmdq; cmdq->references++; - job_run(shellcmd, cmd_run_shell_callback, cmd_run_shell_free, cdata); + job_run(shellcmd, s, cmd_run_shell_callback, cmd_run_shell_free, cdata); if (cdata->bflag) return (CMD_RETURN_NORMAL); -- cgit