1  | 
     | 
     | 
    /* $OpenBSD: cmd-if-shell.c,v 1.56 2017/04/25 11:49:35 nicm Exp $ */  | 
    
    
    2  | 
     | 
     | 
     | 
    
    
    3  | 
     | 
     | 
    /*  | 
    
    
    4  | 
     | 
     | 
     * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>  | 
    
    
    5  | 
     | 
     | 
     * Copyright (c) 2009 Nicholas Marriott <nicm@openbsd.org>  | 
    
    
    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>  | 
    
    
    21  | 
     | 
     | 
    #include <sys/wait.h>  | 
    
    
    22  | 
     | 
     | 
     | 
    
    
    23  | 
     | 
     | 
    #include <stdlib.h>  | 
    
    
    24  | 
     | 
     | 
    #include <string.h>  | 
    
    
    25  | 
     | 
     | 
     | 
    
    
    26  | 
     | 
     | 
    #include "tmux.h"  | 
    
    
    27  | 
     | 
     | 
     | 
    
    
    28  | 
     | 
     | 
    /*  | 
    
    
    29  | 
     | 
     | 
     * Executes a tmux command if a shell command returns true or false.  | 
    
    
    30  | 
     | 
     | 
     */  | 
    
    
    31  | 
     | 
     | 
     | 
    
    
    32  | 
     | 
     | 
    static enum cmd_retval	cmd_if_shell_exec(struct cmd *, struct cmdq_item *);  | 
    
    
    33  | 
     | 
     | 
     | 
    
    
    34  | 
     | 
     | 
    static void		cmd_if_shell_callback(struct job *);  | 
    
    
    35  | 
     | 
     | 
    static void		cmd_if_shell_free(void *);  | 
    
    
    36  | 
     | 
     | 
     | 
    
    
    37  | 
     | 
     | 
    const struct cmd_entry cmd_if_shell_entry = { | 
    
    
    38  | 
     | 
     | 
    	.name = "if-shell",  | 
    
    
    39  | 
     | 
     | 
    	.alias = "if",  | 
    
    
    40  | 
     | 
     | 
     | 
    
    
    41  | 
     | 
     | 
    	.args = { "bFt:", 2, 3 }, | 
    
    
    42  | 
     | 
     | 
    	.usage = "[-bF] " CMD_TARGET_PANE_USAGE " shell-command command "  | 
    
    
    43  | 
     | 
     | 
    		 "[command]",  | 
    
    
    44  | 
     | 
     | 
     | 
    
    
    45  | 
     | 
     | 
    	.target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL }, | 
    
    
    46  | 
     | 
     | 
     | 
    
    
    47  | 
     | 
     | 
    	.flags = 0,  | 
    
    
    48  | 
     | 
     | 
    	.exec = cmd_if_shell_exec  | 
    
    
    49  | 
     | 
     | 
    };  | 
    
    
    50  | 
     | 
     | 
     | 
    
    
    51  | 
     | 
     | 
    struct cmd_if_shell_data { | 
    
    
    52  | 
     | 
     | 
    	char			*file;  | 
    
    
    53  | 
     | 
     | 
    	u_int			 line;  | 
    
    
    54  | 
     | 
     | 
     | 
    
    
    55  | 
     | 
     | 
    	char			*cmd_if;  | 
    
    
    56  | 
     | 
     | 
    	char			*cmd_else;  | 
    
    
    57  | 
     | 
     | 
     | 
    
    
    58  | 
     | 
     | 
    	struct client		*client;  | 
    
    
    59  | 
     | 
     | 
    	struct cmdq_item	*item;  | 
    
    
    60  | 
     | 
     | 
    	struct mouse_event	 mouse;  | 
    
    
    61  | 
     | 
     | 
    };  | 
    
    
    62  | 
     | 
     | 
     | 
    
    
    63  | 
     | 
     | 
    static enum cmd_retval  | 
    
    
    64  | 
     | 
     | 
    cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)  | 
    
    
    65  | 
     | 
     | 
    { | 
    
    
    66  | 
     | 
     | 
    	struct args			*args = self->args;  | 
    
    
    67  | 
     | 
     | 
    	struct cmdq_shared		*shared = item->shared;  | 
    
    
    68  | 
     | 
     | 
    	struct cmd_if_shell_data	*cdata;  | 
    
    
    69  | 
     | 
     | 
    	char				*shellcmd, *cmd, *cause;  | 
    
    
    70  | 
     | 
     | 
    	struct cmd_list			*cmdlist;  | 
    
    
    71  | 
     | 
     | 
    	struct cmdq_item		*new_item;  | 
    
    
    72  | 
     | 
     | 
    	struct client			*c = cmd_find_client(item, NULL, 1);  | 
    
    
    73  | 
     | 
     | 
    	struct session			*s = item->target.s;  | 
    
    
    74  | 
     | 
     | 
    	struct winlink			*wl = item->target.wl;  | 
    
    
    75  | 
     | 
     | 
    	struct window_pane		*wp = item->target.wp;  | 
    
    
    76  | 
     | 
     | 
    	const char			*cwd;  | 
    
    
    77  | 
     | 
     | 
     | 
    
    
    78  | 
     | 
     | 
    	if (item->client != NULL && item->client->session == NULL)  | 
    
    
    79  | 
     | 
     | 
    		cwd = item->client->cwd;  | 
    
    
    80  | 
     | 
     | 
    	else if (s != NULL)  | 
    
    
    81  | 
     | 
     | 
    		cwd = s->cwd;  | 
    
    
    82  | 
     | 
     | 
    	else  | 
    
    
    83  | 
     | 
     | 
    		cwd = NULL;  | 
    
    
    84  | 
     | 
     | 
     | 
    
    
    85  | 
     | 
     | 
    	shellcmd = format_single(item, args->argv[0], c, s, wl, wp);  | 
    
    
    86  | 
     | 
     | 
    	if (args_has(args, 'F')) { | 
    
    
    87  | 
     | 
     | 
    		cmd = NULL;  | 
    
    
    88  | 
     | 
     | 
    		if (*shellcmd != '0' && *shellcmd != '\0')  | 
    
    
    89  | 
     | 
     | 
    			cmd = args->argv[1];  | 
    
    
    90  | 
     | 
     | 
    		else if (args->argc == 3)  | 
    
    
    91  | 
     | 
     | 
    			cmd = args->argv[2];  | 
    
    
    92  | 
     | 
     | 
    		free(shellcmd);  | 
    
    
    93  | 
     | 
     | 
    		if (cmd == NULL)  | 
    
    
    94  | 
     | 
     | 
    			return (CMD_RETURN_NORMAL);  | 
    
    
    95  | 
     | 
     | 
    		cmdlist = cmd_string_parse(cmd, NULL, 0, &cause);  | 
    
    
    96  | 
     | 
     | 
    		if (cmdlist == NULL) { | 
    
    
    97  | 
     | 
     | 
    			if (cause != NULL) { | 
    
    
    98  | 
     | 
     | 
    				cmdq_error(item, "%s", cause);  | 
    
    
    99  | 
     | 
     | 
    				free(cause);  | 
    
    
    100  | 
     | 
     | 
    			}  | 
    
    
    101  | 
     | 
     | 
    			return (CMD_RETURN_ERROR);  | 
    
    
    102  | 
     | 
     | 
    		}  | 
    
    
    103  | 
     | 
     | 
    		new_item = cmdq_get_command(cmdlist, NULL, &shared->mouse, 0);  | 
    
    
    104  | 
     | 
     | 
    		cmdq_insert_after(item, new_item);  | 
    
    
    105  | 
     | 
     | 
    		cmd_list_free(cmdlist);  | 
    
    
    106  | 
     | 
     | 
    		return (CMD_RETURN_NORMAL);  | 
    
    
    107  | 
     | 
     | 
    	}  | 
    
    
    108  | 
     | 
     | 
     | 
    
    
    109  | 
     | 
     | 
    	cdata = xcalloc(1, sizeof *cdata);  | 
    
    
    110  | 
     | 
     | 
    	if (self->file != NULL) { | 
    
    
    111  | 
     | 
     | 
    		cdata->file = xstrdup(self->file);  | 
    
    
    112  | 
     | 
     | 
    		cdata->line = self->line;  | 
    
    
    113  | 
     | 
     | 
    	}  | 
    
    
    114  | 
     | 
     | 
     | 
    
    
    115  | 
     | 
     | 
    	cdata->cmd_if = xstrdup(args->argv[1]);  | 
    
    
    116  | 
     | 
     | 
    	if (args->argc == 3)  | 
    
    
    117  | 
     | 
     | 
    		cdata->cmd_else = xstrdup(args->argv[2]);  | 
    
    
    118  | 
     | 
     | 
    	else  | 
    
    
    119  | 
     | 
     | 
    		cdata->cmd_else = NULL;  | 
    
    
    120  | 
     | 
     | 
     | 
    
    
    121  | 
     | 
     | 
    	cdata->client = item->client;  | 
    
    
    122  | 
     | 
     | 
    	if (cdata->client != NULL)  | 
    
    
    123  | 
     | 
     | 
    		cdata->client->references++;  | 
    
    
    124  | 
     | 
     | 
     | 
    
    
    125  | 
     | 
     | 
    	if (!args_has(args, 'b'))  | 
    
    
    126  | 
     | 
     | 
    		cdata->item = item;  | 
    
    
    127  | 
     | 
     | 
    	else  | 
    
    
    128  | 
     | 
     | 
    		cdata->item = NULL;  | 
    
    
    129  | 
     | 
     | 
    	memcpy(&cdata->mouse, &shared->mouse, sizeof cdata->mouse);  | 
    
    
    130  | 
     | 
     | 
     | 
    
    
    131  | 
     | 
     | 
    	job_run(shellcmd, s, cwd, NULL, cmd_if_shell_callback,  | 
    
    
    132  | 
     | 
     | 
    	    cmd_if_shell_free, cdata);  | 
    
    
    133  | 
     | 
     | 
    	free(shellcmd);  | 
    
    
    134  | 
     | 
     | 
     | 
    
    
    135  | 
     | 
     | 
    	if (args_has(args, 'b'))  | 
    
    
    136  | 
     | 
     | 
    		return (CMD_RETURN_NORMAL);  | 
    
    
    137  | 
     | 
     | 
    	return (CMD_RETURN_WAIT);  | 
    
    
    138  | 
     | 
     | 
    }  | 
    
    
    139  | 
     | 
     | 
     | 
    
    
    140  | 
     | 
     | 
    static void  | 
    
    
    141  | 
     | 
     | 
    cmd_if_shell_callback(struct job *job)  | 
    
    
    142  | 
     | 
     | 
    { | 
    
    
    143  | 
     | 
     | 
    	struct cmd_if_shell_data	*cdata = job->data;  | 
    
    
    144  | 
     | 
     | 
    	struct client			*c = cdata->client;  | 
    
    
    145  | 
     | 
     | 
    	struct cmd_list			*cmdlist;  | 
    
    
    146  | 
     | 
     | 
    	struct cmdq_item		*new_item;  | 
    
    
    147  | 
     | 
     | 
    	char				*cause, *cmd, *file = cdata->file;  | 
    
    
    148  | 
     | 
     | 
    	u_int				 line = cdata->line;  | 
    
    
    149  | 
     | 
     | 
     | 
    
    
    150  | 
     | 
     | 
    	if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0)  | 
    
    
    151  | 
     | 
     | 
    		cmd = cdata->cmd_else;  | 
    
    
    152  | 
     | 
     | 
    	else  | 
    
    
    153  | 
     | 
     | 
    		cmd = cdata->cmd_if;  | 
    
    
    154  | 
     | 
     | 
    	if (cmd == NULL)  | 
    
    
    155  | 
     | 
     | 
    		goto out;  | 
    
    
    156  | 
     | 
     | 
     | 
    
    
    157  | 
     | 
     | 
    	cmdlist = cmd_string_parse(cmd, file, line, &cause);  | 
    
    
    158  | 
     | 
     | 
    	if (cmdlist == NULL) { | 
    
    
    159  | 
     | 
     | 
    		if (cause != NULL && cdata->item != NULL)  | 
    
    
    160  | 
     | 
     | 
    			cmdq_error(cdata->item, "%s", cause);  | 
    
    
    161  | 
     | 
     | 
    		free(cause);  | 
    
    
    162  | 
     | 
     | 
    		new_item = NULL;  | 
    
    
    163  | 
     | 
     | 
    	} else { | 
    
    
    164  | 
     | 
     | 
    		new_item = cmdq_get_command(cmdlist, NULL, &cdata->mouse, 0);  | 
    
    
    165  | 
     | 
     | 
    		cmd_list_free(cmdlist);  | 
    
    
    166  | 
     | 
     | 
    	}  | 
    
    
    167  | 
     | 
     | 
     | 
    
    
    168  | 
     | 
     | 
    	if (new_item != NULL) { | 
    
    
    169  | 
     | 
     | 
    		if (cdata->item == NULL)  | 
    
    
    170  | 
     | 
     | 
    			cmdq_append(c, new_item);  | 
    
    
    171  | 
     | 
     | 
    		else  | 
    
    
    172  | 
     | 
     | 
    			cmdq_insert_after(cdata->item, new_item);  | 
    
    
    173  | 
     | 
     | 
    	}  | 
    
    
    174  | 
     | 
     | 
     | 
    
    
    175  | 
     | 
     | 
    out:  | 
    
    
    176  | 
     | 
     | 
    	if (cdata->item != NULL)  | 
    
    
    177  | 
     | 
     | 
    		cdata->item->flags &= ~CMDQ_WAITING;  | 
    
    
    178  | 
     | 
     | 
    }  | 
    
    
    179  | 
     | 
     | 
     | 
    
    
    180  | 
     | 
     | 
    static void  | 
    
    
    181  | 
     | 
     | 
    cmd_if_shell_free(void *data)  | 
    
    
    182  | 
     | 
     | 
    { | 
    
    
    183  | 
     | 
     | 
    	struct cmd_if_shell_data	*cdata = data;  | 
    
    
    184  | 
     | 
     | 
     | 
    
    
    185  | 
     | 
     | 
    	if (cdata->client != NULL)  | 
    
    
    186  | 
     | 
     | 
    		server_client_unref(cdata->client);  | 
    
    
    187  | 
     | 
     | 
     | 
    
    
    188  | 
     | 
     | 
    	free(cdata->cmd_else);  | 
    
    
    189  | 
     | 
     | 
    	free(cdata->cmd_if);  | 
    
    
    190  | 
     | 
     | 
     | 
    
    
    191  | 
     | 
     | 
    	free(cdata->file);  | 
    
    
    192  | 
     | 
     | 
    	free(cdata);  | 
    
    
    193  | 
     | 
     | 
    }  |