Home | History | Annotate | Line # | Download | only in dist
cmd-if-shell.c revision 1.6
      1  1.4  christos /* $OpenBSD$ */
      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.6  christos static enum cmd_retval	cmd_if_shell_exec(struct cmd *, struct cmdq_item *);
     33  1.1      jmmv 
     34  1.6  christos static enum cmd_retval	cmd_if_shell_error(struct cmdq_item *, void *);
     35  1.6  christos static void		cmd_if_shell_callback(struct job *);
     36  1.6  christos static void		cmd_if_shell_free(void *);
     37  1.1      jmmv 
     38  1.1      jmmv const struct cmd_entry cmd_if_shell_entry = {
     39  1.5  christos 	.name = "if-shell",
     40  1.5  christos 	.alias = "if",
     41  1.5  christos 
     42  1.5  christos 	.args = { "bFt:", 2, 3 },
     43  1.5  christos 	.usage = "[-bF] " CMD_TARGET_PANE_USAGE " shell-command command "
     44  1.5  christos 		 "[command]",
     45  1.5  christos 
     46  1.5  christos 	.tflag = CMD_PANE_CANFAIL,
     47  1.5  christos 
     48  1.5  christos 	.flags = 0,
     49  1.5  christos 	.exec = cmd_if_shell_exec
     50  1.1      jmmv };
     51  1.1      jmmv 
     52  1.1      jmmv struct cmd_if_shell_data {
     53  1.6  christos 	char			*file;
     54  1.6  christos 	u_int			 line;
     55  1.6  christos 
     56  1.4  christos 	char			*cmd_if;
     57  1.4  christos 	char			*cmd_else;
     58  1.4  christos 
     59  1.6  christos 	struct client		*client;
     60  1.6  christos 	struct cmdq_item	*item;
     61  1.4  christos 	struct mouse_event	 mouse;
     62  1.1      jmmv };
     63  1.1      jmmv 
     64  1.6  christos static enum cmd_retval
     65  1.6  christos cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)
     66  1.1      jmmv {
     67  1.2  christos 	struct args			*args = self->args;
     68  1.1      jmmv 	struct cmd_if_shell_data	*cdata;
     69  1.4  christos 	char				*shellcmd, *cmd, *cause;
     70  1.4  christos 	struct cmd_list			*cmdlist;
     71  1.6  christos 	struct cmdq_item		*new_item;
     72  1.6  christos 	struct client			*c = item->state.c;
     73  1.6  christos 	struct session			*s = item->state.tflag.s;
     74  1.6  christos 	struct winlink			*wl = item->state.tflag.wl;
     75  1.6  christos 	struct window_pane		*wp = item->state.tflag.wp;
     76  1.5  christos 	const char			*cwd;
     77  1.3  christos 
     78  1.6  christos 	if (item->client != NULL && item->client->session == NULL)
     79  1.6  christos 		cwd = item->client->cwd;
     80  1.5  christos 	else if (s != NULL)
     81  1.5  christos 		cwd = s->cwd;
     82  1.5  christos 	else
     83  1.5  christos 		cwd = NULL;
     84  1.3  christos 
     85  1.6  christos 	shellcmd = format_single(item, args->argv[0], c, s, wl, wp);
     86  1.4  christos 	if (args_has(args, 'F')) {
     87  1.4  christos 		cmd = NULL;
     88  1.4  christos 		if (*shellcmd != '0' && *shellcmd != '\0')
     89  1.4  christos 			cmd = args->argv[1];
     90  1.4  christos 		else if (args->argc == 3)
     91  1.4  christos 			cmd = args->argv[2];
     92  1.5  christos 		free(shellcmd);
     93  1.4  christos 		if (cmd == NULL)
     94  1.4  christos 			return (CMD_RETURN_NORMAL);
     95  1.6  christos 		cmdlist = cmd_string_parse(cmd, NULL, 0, &cause);
     96  1.6  christos 		if (cmdlist == NULL) {
     97  1.4  christos 			if (cause != NULL) {
     98  1.6  christos 				cmdq_error(item, "%s", cause);
     99  1.4  christos 				free(cause);
    100  1.4  christos 			}
    101  1.4  christos 			return (CMD_RETURN_ERROR);
    102  1.4  christos 		}
    103  1.6  christos 		new_item = cmdq_get_command(cmdlist, NULL, &item->mouse, 0);
    104  1.6  christos 		cmdq_insert_after(item, new_item);
    105  1.4  christos 		cmd_list_free(cmdlist);
    106  1.4  christos 		return (CMD_RETURN_NORMAL);
    107  1.4  christos 	}
    108  1.4  christos 
    109  1.6  christos 	cdata = xcalloc(1, sizeof *cdata);
    110  1.6  christos 	if (self->file != NULL) {
    111  1.6  christos 		cdata->file = xstrdup(self->file);
    112  1.6  christos 		cdata->line = self->line;
    113  1.6  christos 	}
    114  1.4  christos 
    115  1.3  christos 	cdata->cmd_if = xstrdup(args->argv[1]);
    116  1.3  christos 	if (args->argc == 3)
    117  1.3  christos 		cdata->cmd_else = xstrdup(args->argv[2]);
    118  1.3  christos 	else
    119  1.3  christos 		cdata->cmd_else = NULL;
    120  1.4  christos 
    121  1.6  christos 	cdata->client = item->client;
    122  1.6  christos 	cdata->client->references++;
    123  1.3  christos 
    124  1.6  christos 	if (!args_has(args, 'b'))
    125  1.6  christos 		cdata->item = item;
    126  1.6  christos 	else
    127  1.6  christos 		cdata->item = NULL;
    128  1.6  christos 	memcpy(&cdata->mouse, &item->mouse, sizeof cdata->mouse);
    129  1.3  christos 
    130  1.4  christos 	job_run(shellcmd, s, cwd, cmd_if_shell_callback, cmd_if_shell_free,
    131  1.4  christos 	    cdata);
    132  1.3  christos 	free(shellcmd);
    133  1.3  christos 
    134  1.6  christos 	if (args_has(args, 'b'))
    135  1.3  christos 		return (CMD_RETURN_NORMAL);
    136  1.3  christos 	return (CMD_RETURN_WAIT);
    137  1.1      jmmv }
    138  1.1      jmmv 
    139  1.6  christos static enum cmd_retval
    140  1.6  christos cmd_if_shell_error(struct cmdq_item *item, void *data)
    141  1.6  christos {
    142  1.6  christos 	char	*error = data;
    143  1.6  christos 
    144  1.6  christos 	cmdq_error(item, "%s", error);
    145  1.6  christos 	free(error);
    146  1.6  christos 
    147  1.6  christos 	return (CMD_RETURN_NORMAL);
    148  1.6  christos }
    149  1.6  christos 
    150  1.6  christos static void
    151  1.1      jmmv cmd_if_shell_callback(struct job *job)
    152  1.1      jmmv {
    153  1.1      jmmv 	struct cmd_if_shell_data	*cdata = job->data;
    154  1.6  christos 	struct client			*c = cdata->client;
    155  1.1      jmmv 	struct cmd_list			*cmdlist;
    156  1.6  christos 	struct cmdq_item		*new_item;
    157  1.6  christos 	char				*cause, *cmd, *file = cdata->file;
    158  1.6  christos 	u_int				 line = cdata->line;
    159  1.1      jmmv 
    160  1.1      jmmv 	if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0)
    161  1.3  christos 		cmd = cdata->cmd_else;
    162  1.3  christos 	else
    163  1.3  christos 		cmd = cdata->cmd_if;
    164  1.3  christos 	if (cmd == NULL)
    165  1.6  christos 		goto out;
    166  1.1      jmmv 
    167  1.6  christos 	cmdlist = cmd_string_parse(cmd, file, line, &cause);
    168  1.6  christos 	if (cmdlist == NULL) {
    169  1.6  christos 		if (cause != NULL)
    170  1.6  christos 			new_item = cmdq_get_callback(cmd_if_shell_error, cause);
    171  1.6  christos 		else
    172  1.6  christos 			new_item = NULL;
    173  1.6  christos 	} else {
    174  1.6  christos 		new_item = cmdq_get_command(cmdlist, NULL, &cdata->mouse, 0);
    175  1.6  christos 		cmd_list_free(cmdlist);
    176  1.1      jmmv 	}
    177  1.1      jmmv 
    178  1.6  christos 	if (new_item != NULL) {
    179  1.6  christos 		if (cdata->item == NULL)
    180  1.6  christos 			cmdq_append(c, new_item);
    181  1.6  christos 		else
    182  1.6  christos 			cmdq_insert_after(cdata->item, new_item);
    183  1.6  christos 	}
    184  1.3  christos 
    185  1.6  christos out:
    186  1.6  christos 	if (cdata->item != NULL)
    187  1.6  christos 		cdata->item->flags &= ~CMDQ_WAITING;
    188  1.3  christos }
    189  1.3  christos 
    190  1.6  christos static void
    191  1.1      jmmv cmd_if_shell_free(void *data)
    192  1.1      jmmv {
    193  1.1      jmmv 	struct cmd_if_shell_data	*cdata = data;
    194  1.3  christos 
    195  1.6  christos 	server_client_unref(cdata->client);
    196  1.1      jmmv 
    197  1.3  christos 	free(cdata->cmd_else);
    198  1.3  christos 	free(cdata->cmd_if);
    199  1.6  christos 
    200  1.6  christos 	free(cdata->file);
    201  1.3  christos 	free(cdata);
    202  1.1      jmmv }
    203