[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.3

1.3     ! nicm        1: /* $OpenBSD: cmd-if-shell.c,v 1.2 2009/07/13 23:11:35 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
                      5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
                     15:  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                     16:  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18:
                     19: #include <sys/types.h>
                     20:
                     21: #include <errno.h>
                     22: #include <stdlib.h>
                     23: #include <string.h>
                     24:
                     25: #include "tmux.h"
                     26:
                     27: /*
                     28:  * Executes a tmux command if a shell command returns true.
                     29:  */
                     30:
                     31: int    cmd_if_shell_parse(struct cmd *, int, char **, char **);
                     32: int    cmd_if_shell_exec(struct cmd *, struct cmd_ctx *);
                     33: void   cmd_if_shell_free(struct cmd *);
                     34: void   cmd_if_shell_init(struct cmd *, int);
                     35: size_t cmd_if_shell_print(struct cmd *, char *, size_t);
                     36:
                     37: struct cmd_if_shell_data {
                     38:        char *cmd;
                     39:        char *sh_cmd;
                     40: };
                     41:
                     42: const struct cmd_entry cmd_if_shell_entry = {
                     43:        "if-shell", "if",
                     44:        "shell-command command",
1.2       nicm       45:        0, 0,
1.1       nicm       46:        cmd_if_shell_init,
                     47:        cmd_if_shell_parse,
                     48:        cmd_if_shell_exec,
                     49:        cmd_if_shell_free,
                     50:        cmd_if_shell_print
                     51: };
                     52:
                     53: void
                     54: cmd_if_shell_init(struct cmd *self, unused int arg)
                     55: {
                     56:        struct cmd_if_shell_data        *data;
                     57:
                     58:        self->data = data = xmalloc(sizeof *data);
                     59:        data->cmd = NULL;
                     60:        data->sh_cmd = NULL;
                     61: }
                     62:
                     63: int
                     64: cmd_if_shell_parse(struct cmd *self, int argc, char **argv, char **cause)
                     65: {
                     66:        struct cmd_if_shell_data        *data;
                     67:        int                              opt;
                     68:
                     69:        self->entry->init(self, 0);
                     70:        data = self->data;
                     71:
                     72:        while ((opt = getopt(argc, argv, "")) != -1) {
                     73:                switch (opt) {
                     74:                default:
                     75:                        goto usage;
                     76:                }
                     77:        }
                     78:        argc -= optind;
                     79:        argv += optind;
                     80:        if (argc != 2)
                     81:                goto usage;
                     82:
                     83:        data->sh_cmd = xstrdup(argv[0]);
                     84:        data->cmd = xstrdup(argv[1]);
                     85:        return (0);
                     86:
                     87: usage:
                     88:        xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
                     89:
                     90:        self->entry->free(self);
                     91:        return (-1);
                     92: }
                     93:
                     94: int
                     95: cmd_if_shell_exec(struct cmd *self, struct cmd_ctx *ctx)
                     96: {
                     97:        struct cmd_if_shell_data        *data = self->data;
                     98:        struct cmd_list                 *cmdlist;
                     99:        char                            *cause;
                    100:        int                              ret;
                    101:
                    102:        if ((ret = system(data->sh_cmd)) < 0) {
                    103:                ctx->error(ctx, "system error: %s", strerror(errno));
                    104:                return (-1);
                    105:        } else if (ret != 0)
                    106:                return (0);
                    107:
                    108:        if (cmd_string_parse(data->cmd, &cmdlist, &cause) != 0) {
                    109:                if (cause != NULL) {
                    110:                        ctx->error(ctx, "%s", cause);
                    111:                        xfree(cause);
                    112:                }
                    113:                return (-1);
                    114:        }
                    115:
                    116:        if (cmd_list_exec(cmdlist, ctx) < 0) {
                    117:                cmd_list_free(cmdlist);
                    118:                return (-1);
                    119:        }
                    120:
                    121:        cmd_list_free(cmdlist);
                    122:        return (0);
                    123: }
                    124:
                    125: void
                    126: cmd_if_shell_free(struct cmd *self)
                    127: {
                    128:        struct cmd_if_shell_data        *data = self->data;
                    129:
                    130:        if (data->cmd != NULL)
                    131:                xfree(data->cmd);
                    132:        if (data->sh_cmd != NULL)
                    133:                xfree(data->sh_cmd);
                    134:        xfree(data);
                    135: }
                    136:
                    137: size_t
                    138: cmd_if_shell_print(struct cmd *self, char *buf, size_t len)
                    139: {
                    140:        struct cmd_if_shell_data        *data = self->data;
                    141:        size_t                          off = 0;
                    142:
                    143:        off += xsnprintf(buf, len, "%s", self->entry->name);
                    144:        if (data == NULL)
                    145:                return (off);
                    146:        if (off < len && data->sh_cmd != NULL)
                    147:                off += cmd_prarg(buf + off, len - off, " ", data->sh_cmd);
                    148:        if (off < len && data->cmd != NULL)
                    149:                off += cmd_prarg(buf + off, len - off, " ", data->cmd);
                    150:        return (off);
                    151: }