cmd-if-shell.c revision 1.3 1 1.3 christos /* Id */
2 1.1 jmmv
3 1.1 jmmv /*
4 1.1 jmmv * Copyright (c) 2009 Tiago Cunha <me (at) tiagocunha.org>
5 1.1 jmmv * Copyright (c) 2009 Nicholas Marriott <nicm (at) openbsd.org>
6 1.1 jmmv *
7 1.1 jmmv * Permission to use, copy, modify, and distribute this software for any
8 1.1 jmmv * purpose with or without fee is hereby granted, provided that the above
9 1.1 jmmv * copyright notice and this permission notice appear in all copies.
10 1.1 jmmv *
11 1.1 jmmv * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.1 jmmv * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1 jmmv * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.1 jmmv * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1 jmmv * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
16 1.1 jmmv * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
17 1.1 jmmv * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1 jmmv */
19 1.1 jmmv
20 1.1 jmmv #include <sys/types.h>
21 1.1 jmmv #include <sys/wait.h>
22 1.1 jmmv
23 1.3 christos #include <stdlib.h>
24 1.1 jmmv #include <string.h>
25 1.1 jmmv
26 1.1 jmmv #include "tmux.h"
27 1.1 jmmv
28 1.1 jmmv /*
29 1.3 christos * Executes a tmux command if a shell command returns true or false.
30 1.1 jmmv */
31 1.1 jmmv
32 1.3 christos enum cmd_retval cmd_if_shell_exec(struct cmd *, struct cmd_q *);
33 1.1 jmmv
34 1.1 jmmv void cmd_if_shell_callback(struct job *);
35 1.3 christos void cmd_if_shell_done(struct cmd_q *);
36 1.1 jmmv void cmd_if_shell_free(void *);
37 1.1 jmmv
38 1.1 jmmv const struct cmd_entry cmd_if_shell_entry = {
39 1.1 jmmv "if-shell", "if",
40 1.3 christos "bt:", 2, 3,
41 1.3 christos "[-b] " CMD_TARGET_PANE_USAGE " shell-command command [command]",
42 1.2 christos 0,
43 1.2 christos NULL,
44 1.2 christos cmd_if_shell_exec
45 1.1 jmmv };
46 1.1 jmmv
47 1.1 jmmv struct cmd_if_shell_data {
48 1.3 christos char *cmd_if;
49 1.3 christos char *cmd_else;
50 1.3 christos struct cmd_q *cmdq;
51 1.3 christos int bflag;
52 1.3 christos int started;
53 1.1 jmmv };
54 1.1 jmmv
55 1.3 christos enum cmd_retval
56 1.3 christos cmd_if_shell_exec(struct cmd *self, struct cmd_q *cmdq)
57 1.1 jmmv {
58 1.2 christos struct args *args = self->args;
59 1.1 jmmv struct cmd_if_shell_data *cdata;
60 1.3 christos char *shellcmd;
61 1.3 christos struct client *c;
62 1.3 christos struct session *s = NULL;
63 1.3 christos struct winlink *wl = NULL;
64 1.3 christos struct window_pane *wp = NULL;
65 1.3 christos struct format_tree *ft;
66 1.3 christos
67 1.3 christos if (args_has(args, 't'))
68 1.3 christos wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp);
69 1.3 christos else {
70 1.3 christos c = cmd_find_client(cmdq, NULL, 1);
71 1.3 christos if (c != NULL && c->session != NULL) {
72 1.3 christos s = c->session;
73 1.3 christos wl = s->curw;
74 1.3 christos wp = wl->window->active;
75 1.3 christos }
76 1.3 christos }
77 1.3 christos
78 1.3 christos ft = format_create();
79 1.3 christos if (s != NULL)
80 1.3 christos format_session(ft, s);
81 1.3 christos if (s != NULL && wl != NULL)
82 1.3 christos format_winlink(ft, s, wl);
83 1.3 christos if (wp != NULL)
84 1.3 christos format_window_pane(ft, wp);
85 1.3 christos shellcmd = format_expand(ft, args->argv[0]);
86 1.3 christos format_free(ft);
87 1.1 jmmv
88 1.1 jmmv cdata = xmalloc(sizeof *cdata);
89 1.3 christos cdata->cmd_if = xstrdup(args->argv[1]);
90 1.3 christos if (args->argc == 3)
91 1.3 christos cdata->cmd_else = xstrdup(args->argv[2]);
92 1.3 christos else
93 1.3 christos cdata->cmd_else = NULL;
94 1.3 christos cdata->bflag = args_has(args, 'b');
95 1.3 christos
96 1.3 christos cdata->started = 0;
97 1.3 christos cdata->cmdq = cmdq;
98 1.3 christos cmdq->references++;
99 1.3 christos
100 1.3 christos job_run(shellcmd, s, cmd_if_shell_callback, cmd_if_shell_free, cdata);
101 1.3 christos free(shellcmd);
102 1.3 christos
103 1.3 christos if (cdata->bflag)
104 1.3 christos return (CMD_RETURN_NORMAL);
105 1.3 christos return (CMD_RETURN_WAIT);
106 1.1 jmmv }
107 1.1 jmmv
108 1.1 jmmv void
109 1.1 jmmv cmd_if_shell_callback(struct job *job)
110 1.1 jmmv {
111 1.1 jmmv struct cmd_if_shell_data *cdata = job->data;
112 1.3 christos struct cmd_q *cmdq = cdata->cmdq, *cmdq1;
113 1.1 jmmv struct cmd_list *cmdlist;
114 1.3 christos char *cause, *cmd;
115 1.3 christos
116 1.3 christos if (cmdq->dead)
117 1.3 christos return;
118 1.1 jmmv
119 1.1 jmmv if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0)
120 1.3 christos cmd = cdata->cmd_else;
121 1.3 christos else
122 1.3 christos cmd = cdata->cmd_if;
123 1.3 christos if (cmd == NULL)
124 1.1 jmmv return;
125 1.1 jmmv
126 1.3 christos if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) {
127 1.1 jmmv if (cause != NULL) {
128 1.3 christos cmdq_error(cmdq, "%s", cause);
129 1.3 christos free(cause);
130 1.1 jmmv }
131 1.1 jmmv return;
132 1.1 jmmv }
133 1.1 jmmv
134 1.3 christos cdata->started = 1;
135 1.3 christos
136 1.3 christos cmdq1 = cmdq_new(cmdq->client);
137 1.3 christos cmdq1->emptyfn = cmd_if_shell_done;
138 1.3 christos cmdq1->data = cdata;
139 1.3 christos
140 1.3 christos cmdq_run(cmdq1, cmdlist);
141 1.1 jmmv cmd_list_free(cmdlist);
142 1.1 jmmv }
143 1.1 jmmv
144 1.1 jmmv void
145 1.3 christos cmd_if_shell_done(struct cmd_q *cmdq1)
146 1.3 christos {
147 1.3 christos struct cmd_if_shell_data *cdata = cmdq1->data;
148 1.3 christos struct cmd_q *cmdq = cdata->cmdq;
149 1.3 christos
150 1.3 christos if (cmdq1->client_exit >= 0)
151 1.3 christos cmdq->client_exit = cmdq1->client_exit;
152 1.3 christos
153 1.3 christos if (!cmdq_free(cmdq) && !cdata->bflag)
154 1.3 christos cmdq_continue(cmdq);
155 1.3 christos
156 1.3 christos cmdq_free(cmdq1);
157 1.3 christos
158 1.3 christos free(cdata->cmd_else);
159 1.3 christos free(cdata->cmd_if);
160 1.3 christos free(cdata);
161 1.3 christos }
162 1.3 christos
163 1.3 christos void
164 1.1 jmmv cmd_if_shell_free(void *data)
165 1.1 jmmv {
166 1.1 jmmv struct cmd_if_shell_data *cdata = data;
167 1.3 christos struct cmd_q *cmdq = cdata->cmdq;
168 1.1 jmmv
169 1.3 christos if (cdata->started)
170 1.3 christos return;
171 1.3 christos
172 1.3 christos if (!cmdq_free(cmdq) && !cdata->bflag)
173 1.3 christos cmdq_continue(cmdq);
174 1.1 jmmv
175 1.3 christos free(cdata->cmd_else);
176 1.3 christos free(cdata->cmd_if);
177 1.3 christos free(cdata);
178 1.1 jmmv }
179