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