Annotation of src/usr.bin/tmux/cmd-if-shell.c, Revision 1.2
1.2 ! nicm 1: /* $OpenBSD: cmd-if-shell.c,v 1.1 2009/07/09 15:47:49 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_send(struct cmd *, struct buffer *);
34: void cmd_if_shell_recv(struct cmd *, struct buffer *);
35: void cmd_if_shell_free(struct cmd *);
36: void cmd_if_shell_init(struct cmd *, int);
37: size_t cmd_if_shell_print(struct cmd *, char *, size_t);
38:
39: struct cmd_if_shell_data {
40: char *cmd;
41: char *sh_cmd;
42: };
43:
44: const struct cmd_entry cmd_if_shell_entry = {
45: "if-shell", "if",
46: "shell-command command",
1.2 ! nicm 47: 0, 0,
1.1 nicm 48: cmd_if_shell_init,
49: cmd_if_shell_parse,
50: cmd_if_shell_exec,
51: cmd_if_shell_send,
52: cmd_if_shell_recv,
53: cmd_if_shell_free,
54: cmd_if_shell_print
55: };
56:
57: void
58: cmd_if_shell_init(struct cmd *self, unused int arg)
59: {
60: struct cmd_if_shell_data *data;
61:
62: self->data = data = xmalloc(sizeof *data);
63: data->cmd = NULL;
64: data->sh_cmd = NULL;
65: }
66:
67: int
68: cmd_if_shell_parse(struct cmd *self, int argc, char **argv, char **cause)
69: {
70: struct cmd_if_shell_data *data;
71: int opt;
72:
73: self->entry->init(self, 0);
74: data = self->data;
75:
76: while ((opt = getopt(argc, argv, "")) != -1) {
77: switch (opt) {
78: default:
79: goto usage;
80: }
81: }
82: argc -= optind;
83: argv += optind;
84: if (argc != 2)
85: goto usage;
86:
87: data->sh_cmd = xstrdup(argv[0]);
88: data->cmd = xstrdup(argv[1]);
89: return (0);
90:
91: usage:
92: xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
93:
94: self->entry->free(self);
95: return (-1);
96: }
97:
98: int
99: cmd_if_shell_exec(struct cmd *self, struct cmd_ctx *ctx)
100: {
101: struct cmd_if_shell_data *data = self->data;
102: struct cmd_list *cmdlist;
103: char *cause;
104: int ret;
105:
106: if ((ret = system(data->sh_cmd)) < 0) {
107: ctx->error(ctx, "system error: %s", strerror(errno));
108: return (-1);
109: } else if (ret != 0)
110: return (0);
111:
112: if (cmd_string_parse(data->cmd, &cmdlist, &cause) != 0) {
113: if (cause != NULL) {
114: ctx->error(ctx, "%s", cause);
115: xfree(cause);
116: }
117: return (-1);
118: }
119:
120: if (cmd_list_exec(cmdlist, ctx) < 0) {
121: cmd_list_free(cmdlist);
122: return (-1);
123: }
124:
125: cmd_list_free(cmdlist);
126: return (0);
127: }
128:
129: void
130: cmd_if_shell_send(struct cmd *self, struct buffer *b)
131: {
132: struct cmd_if_shell_data *data = self->data;
133:
134: buffer_write(b, data, sizeof *data);
135: cmd_send_string(b, data->cmd);
136: cmd_send_string(b, data->sh_cmd);
137: }
138:
139: void
140: cmd_if_shell_recv(struct cmd *self, struct buffer *b)
141: {
142: struct cmd_if_shell_data *data;
143:
144: self->data = data = xmalloc(sizeof *data);
145: buffer_read(b, data, sizeof *data);
146: data->cmd = cmd_recv_string(b);
147: data->sh_cmd = cmd_recv_string(b);
148: }
149:
150: void
151: cmd_if_shell_free(struct cmd *self)
152: {
153: struct cmd_if_shell_data *data = self->data;
154:
155: if (data->cmd != NULL)
156: xfree(data->cmd);
157: if (data->sh_cmd != NULL)
158: xfree(data->sh_cmd);
159: xfree(data);
160: }
161:
162: size_t
163: cmd_if_shell_print(struct cmd *self, char *buf, size_t len)
164: {
165: struct cmd_if_shell_data *data = self->data;
166: size_t off = 0;
167:
168: off += xsnprintf(buf, len, "%s", self->entry->name);
169: if (data == NULL)
170: return (off);
171: if (off < len && data->sh_cmd != NULL)
172: off += cmd_prarg(buf + off, len - off, " ", data->sh_cmd);
173: if (off < len && data->cmd != NULL)
174: off += cmd_prarg(buf + off, len - off, " ", data->cmd);
175: return (off);
176: }