[BACK]Return to cmd-if-shell.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cmd-if-shell.c, Revision 1.14

1.14    ! nicm        1: /* $OpenBSD: cmd-if-shell.c,v 1.13 2011/10/18 08:57:01 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
1.5       nicm        5:  * Copyright (c) 2009 Nicholas Marriott <nicm@openbsd.org>
1.1       nicm        6:  *
                      7:  * Permission to use, copy, modify, and distribute this software for any
                      8:  * purpose with or without fee is hereby granted, provided that the above
                      9:  * copyright notice and this permission notice appear in all copies.
                     10:  *
                     11:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     12:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     13:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     14:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     15:  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
                     16:  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                     17:  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     18:  */
                     19:
                     20: #include <sys/types.h>
1.5       nicm       21: #include <sys/wait.h>
1.1       nicm       22:
                     23: #include <string.h>
                     24:
                     25: #include "tmux.h"
                     26:
                     27: /*
1.14    ! nicm       28:  * Executes a tmux command if a shell command returns true or false.
1.1       nicm       29:  */
                     30:
                     31: int    cmd_if_shell_exec(struct cmd *, struct cmd_ctx *);
                     32:
1.5       nicm       33: void   cmd_if_shell_callback(struct job *);
                     34: void   cmd_if_shell_free(void *);
1.1       nicm       35:
                     36: const struct cmd_entry cmd_if_shell_entry = {
                     37:        "if-shell", "if",
1.14    ! nicm       38:        "", 2, 3,
        !            39:        "shell-command command [command]",
1.10      nicm       40:        0,
                     41:        NULL,
1.14    ! nicm       42:        NULL,
1.10      nicm       43:        cmd_if_shell_exec
1.1       nicm       44: };
                     45:
1.5       nicm       46: struct cmd_if_shell_data {
1.13      nicm       47:        char            *cmd_if;
                     48:        char            *cmd_else;
1.5       nicm       49:        struct cmd_ctx   ctx;
                     50: };
1.1       nicm       51:
                     52: int
1.5       nicm       53: cmd_if_shell_exec(struct cmd *self, struct cmd_ctx *ctx)
1.1       nicm       54: {
1.10      nicm       55:        struct args                     *args = self->args;
1.5       nicm       56:        struct cmd_if_shell_data        *cdata;
1.11      nicm       57:        const char                      *shellcmd = args->argv[0];
1.5       nicm       58:
                     59:        cdata = xmalloc(sizeof *cdata);
1.13      nicm       60:        cdata->cmd_if = xstrdup(args->argv[1]);
1.14    ! nicm       61:        if (args->argc == 3)
        !            62:                cdata->cmd_else = xstrdup(args->argv[2]);
1.13      nicm       63:        else
                     64:                cdata->cmd_else = NULL;
1.5       nicm       65:        memcpy(&cdata->ctx, ctx, sizeof cdata->ctx);
                     66:
                     67:        if (ctx->cmdclient != NULL)
                     68:                ctx->cmdclient->references++;
                     69:        if (ctx->curclient != NULL)
                     70:                ctx->curclient->references++;
                     71:
1.11      nicm       72:        job_run(shellcmd, cmd_if_shell_callback, cmd_if_shell_free, cdata);
1.1       nicm       73:
1.5       nicm       74:        return (1);     /* don't let client exit */
1.1       nicm       75: }
                     76:
1.5       nicm       77: void
                     78: cmd_if_shell_callback(struct job *job)
1.1       nicm       79: {
1.5       nicm       80:        struct cmd_if_shell_data        *cdata = job->data;
                     81:        struct cmd_ctx                  *ctx = &cdata->ctx;
1.1       nicm       82:        struct cmd_list                 *cmdlist;
1.14    ! nicm       83:        char                            *cause, *cmd;
1.1       nicm       84:
1.13      nicm       85:        if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0) {
                     86:                cmd = cdata->cmd_else;
                     87:                if (cmd == NULL)
                     88:                        return;
                     89:        } else
                     90:                cmd = cdata->cmd_if;
                     91:        if (cmd_string_parse(cmd, &cmdlist, &cause) != 0) {
1.1       nicm       92:                if (cause != NULL) {
                     93:                        ctx->error(ctx, "%s", cause);
                     94:                        xfree(cause);
                     95:                }
1.5       nicm       96:                return;
1.1       nicm       97:        }
                     98:
1.12      nicm       99:        cmd_list_exec(cmdlist, ctx);
1.1       nicm      100:        cmd_list_free(cmdlist);
                    101: }
                    102:
                    103: void
1.5       nicm      104: cmd_if_shell_free(void *data)
1.1       nicm      105: {
1.5       nicm      106:        struct cmd_if_shell_data        *cdata = data;
                    107:        struct cmd_ctx                  *ctx = &cdata->ctx;
1.8       nicm      108:        struct msg_exit_data             exitdata;
1.1       nicm      109:
1.5       nicm      110:        if (ctx->cmdclient != NULL) {
                    111:                ctx->cmdclient->references--;
1.8       nicm      112:                exitdata.retcode = ctx->cmdclient->retcode;
1.9       nicm      113:                ctx->cmdclient->flags |= CLIENT_EXIT;
1.5       nicm      114:        }
                    115:        if (ctx->curclient != NULL)
                    116:                ctx->curclient->references--;
1.1       nicm      117:
1.13      nicm      118:        if (cdata->cmd_else != NULL)
                    119:                xfree(cdata->cmd_else);
                    120:        xfree(cdata->cmd_if);
1.5       nicm      121:        xfree(cdata);
1.1       nicm      122: }